PatchworkOS
Loading...
Searching...
No Matches
Process management

Process management header. More...

Data Structures

struct  spawn_fd_t
 Stucture used to duplicate fds in spawn(). More...
 
struct  spawn_attr_t
 
struct  sync_t
 Synchronization object. More...
 

Macros

#define PRIORITY_MAX   64
 
#define PRIORITY_MAX_USER   32
 
#define PRIORITY_MIN   0
 
#define SPAWN_NONE   0
 
#define SPAWN_INHERIT_PRIORITY   (1 << 0)
 
#define SPAWN_FD_END
 Spawn fds termination constant.
 
#define PAGE_SIZE   0x1000
 Memory page size.
 
#define BYTES_TO_PAGES(amount)   (((amount) + PAGE_SIZE - 1) / PAGE_SIZE)
 Convert bytes to pages.
 
#define PAGE_SIZE_OF(object)   BYTES_TO_PAGES(sizeof(object))
 Size of an object in pages.
 
#define FUTEX_ALL   UINT64_MAX
 Futex wake all constant.
 

Typedefs

typedef uint8_t priority_t
 Priority type.
 
typedef uint64_t spawn_flags_t
 Spawn behaviour flags.
 

Enumerations

enum  prot_t {
  PROT_NONE = 0 ,
  PROT_READ = (1 << 0) ,
  PROT_WRITE = (1 << 1)
}
 Memory protection flags. More...
 
enum  futex_op_t {
  FUTEX_WAIT ,
  FUTEX_WAKE
}
 Futex operation enum. More...
 

Functions

pid_t spawn (const char **argv, const spawn_fd_t *fds, const char *cwd, spawn_attr_t *attr)
 System call for creating child processes.
 
pid_t getpid (void)
 System call to retrieve the current pid.
 
tid_t gettid (void)
 System call to retrieve the current tid.
 
void * mmap (fd_t fd, void *address, uint64_t length, prot_t prot)
 System call to map memory from a file.
 
uint64_t munmap (void *address, uint64_t length)
 System call to unmap mapped memory.
 
uint64_t mprotect (void *address, uint64_t length, prot_t prot)
 System call to change the protection flags of memory.
 
uint64_t futex (atomic_uint64_t *addr, uint64_t val, futex_op_t op, clock_t timeout)
 System call for fast user space mutual exclusion.
 
clock_t uptime (void)
 System call for retreving the time since boot.
 
uint64_t nanosleep (clock_t timeout)
 System call for sleeping.
 

Detailed Description

Process management header.

The sys/proc.h header handles process management, including process creation, managing a processes address space, scheduling, and similar.

Macro Definition Documentation

◆ BYTES_TO_PAGES

#define BYTES_TO_PAGES (   amount)    (((amount) + PAGE_SIZE - 1) / PAGE_SIZE)

Convert bytes to pages.

The BYTES_TO_PAGES() macro takes in a amount of bytes and returns the amount of pages in memory required to store that amount of bytes.

Parameters
amountThe amount of bytes.
Returns
The amount of pages.

Definition at line 151 of file proc.h.

◆ FUTEX_ALL

#define FUTEX_ALL   UINT64_MAX

Futex wake all constant.

The FUTEX_ALL constant can be used as the val argument when using the FUTEX_WAIT operating in the futex() function to wake upp all waiters.

Definition at line 238 of file proc.h.

◆ PAGE_SIZE

#define PAGE_SIZE   0x1000

Memory page size.

The PAGE_SIZE constant stores the size in bytes of one page in memory.

Definition at line 140 of file proc.h.

◆ PAGE_SIZE_OF

#define PAGE_SIZE_OF (   object)    BYTES_TO_PAGES(sizeof(object))

Size of an object in pages.

The PAGE_SIZE_OF() macro returns the amount of pages in memory required to store a given object.

Parameters
objectThe object to calculate the page size of.
Returns
The amount of pages.

Definition at line 161 of file proc.h.

◆ PRIORITY_MAX

#define PRIORITY_MAX   64

Definition at line 42 of file proc.h.

◆ PRIORITY_MAX_USER

#define PRIORITY_MAX_USER   32

Definition at line 43 of file proc.h.

◆ PRIORITY_MIN

#define PRIORITY_MIN   0

Definition at line 44 of file proc.h.

◆ SPAWN_FD_END

#define SPAWN_FD_END
Value:
{ \
.child = FD_NONE, .parent = FD_NONE \
}
#define FD_NONE
No file descriptor constant.
Definition fd_t.h:22
Stucture used to duplicate fds in spawn().
Definition proc.h:55

Spawn fds termination constant.

Definition at line 82 of file proc.h.

◆ SPAWN_INHERIT_PRIORITY

#define SPAWN_INHERIT_PRIORITY   (1 << 0)

Definition at line 76 of file proc.h.

◆ SPAWN_NONE

#define SPAWN_NONE   0

Definition at line 75 of file proc.h.

Typedef Documentation

◆ priority_t

Priority type.

The priority_t type is used to store the scheduling priority of a process, we also define three constants PRIORITY_MIN, which represents the lowest priority a process can have, PRIORITY_MAX which defines the maximum value of a process priority (not inclusive) and PRIORITY_MAX_USER which defines the maximum value that user space is allowed to specify for a process (not inclusive). See the kernel sched_invoke() function for more info.

Definition at line 41 of file proc.h.

◆ spawn_flags_t

Spawn behaviour flags.

The spawn_flags_t type is used to modify the behaviour when spawning a new process or provide additional information. We use a 64 bit integer to allow more flags to be implemented. For more information check the spawn() function.

Available flags:

  • SPAWN_NONE - None
  • SPAWN_INHERIT_PRIORITY - Causes the new process to inherit the priority of the parent, the spawn_attr_t::priority field is ignored.

Definition at line 74 of file proc.h.

Enumeration Type Documentation

◆ futex_op_t

enum futex_op_t

Futex operation enum.

The futex_op_t enum is used to specify the desired futex operation in the futex() function.

Enumerator
FUTEX_WAIT 

The futex operating for waiting until the value pointed to by addr is not equal to val.

FUTEX_WAKE 

The futex operation for waking up a amount of threads specified by the val argument.

Definition at line 225 of file proc.h.

◆ prot_t

enum prot_t

Memory protection flags.

The prot_t enum is used to store the memory protection flags of a region in memory.

Enumerator
PROT_NONE 

None.

PROT_READ 

Memory can be read from.

PROT_WRITE 

Memory can be written to.

Definition at line 169 of file proc.h.

Function Documentation

◆ futex()

uint64_t futex ( atomic_uint64_t *  addr,
uint64_t  val,
futex_op_t  op,
clock_t  timeout 
)

System call for fast user space mutual exclusion.

The futex() function provides a fast user-space syncronization mechanism. It can be used to implement userspace mutexes, conditional variables, etc.

Parameters
addrA pointer to an atomic 64-bit unsigned integer.
valThe value to compare against for FUTEX_WAIT or the number of threads to wake for FUTEX_WAKE.
opThe futex operation to perform (e.g., FUTEX_WAIT or FUTEX_WAKE).
timeoutAn optional timeout for FUTEX_WAIT. If CLOCKS_NEVER, it waits forever.
Returns
On success, 0, except if using the FUTEX_WAKE operation then it returns the number of woken threads. On failure, ERR and errno is set.

Definition at line 6 of file futex.c.

References _syscall_errno(), _syscall_futex(), ERR, and errno.

Referenced by futex_ctx_deinit(), futex_ctx_get(), mtx_lock(), mtx_unlock(), SYSCALL_DEFINE(), thrd_exit(), and thrd_join().

◆ getpid()

pid_t getpid ( void  )

System call to retrieve the current pid.

The getpid() function retrieves the pid of the currently running process.

Returns
The running processes pid.

Definition at line 6 of file getpid.c.

References _syscall_errno(), _syscall_getpid(), ERR, and errno.

◆ gettid()

tid_t gettid ( void  )

System call to retrieve the current tid.

The gettid() function retrieves the tid of the currently running thread.

Returns
The running threads tid.

Definition at line 6 of file gettid.c.

References _syscall_errno(), _syscall_gettid(), ERR, and errno.

Referenced by _errno_get(), mtx_lock(), mtx_unlock(), thrd_current(), and thrd_exit().

◆ mmap()

void * mmap ( fd_t  fd,
void *  address,
uint64_t  length,
prot_t  prot 
)

System call to map memory from a file.

The mmap() function maps memory to the currently running processes address space from a file, this is the only way to allocate virtual memory from userspace. An example usage would be to map the /dev/zero file which would allocate zeroed memory.

Parameters
fdThe open file descriptor of the file to be mapped.
addressThe desired virtual destination address, if equal to NULL the kernel will choose a available address, will be rounded down to the nearest page multiple.
lengthThe length of the segment to be mapped, note that this length will be rounded up to the nearest page multiple by the kernel factoring in page boundaries.
protProtection flags, must have at least PROT_READ set.
Returns
On success, returns the address of the mapped memory, will always be page aligned, on failure returns NULL and errno is set.

Definition at line 6 of file mmap.c.

References _syscall_errno(), _syscall_mmap(), address, errno, and NULL.

Referenced by _heap_map_memory(), fb_new(), frontbuffer_init(), mmap_generic(), surface_new(), and window_new().

◆ mprotect()

uint64_t mprotect ( void *  address,
uint64_t  length,
prot_t  prot 
)

System call to change the protection flags of memory.

The mprotect() changes the protection flags of a virtual memory area in the currently running processes address space.

Parameters
addressThe starting virtual address of the memory area to be modified.
lengthThe length of the memory area to be modifed.
protThe new protection flags of the memory area, if equal to PROT_NONE the memory area will be unmapped.
Returns
On success, 0, on failure returns ERR and errno is set.

Definition at line 6 of file mprotect.c.

References _syscall_errno(), _syscall_mprotect(), address, ERR, and errno.

◆ munmap()

uint64_t munmap ( void *  address,
uint64_t  length 
)

System call to unmap mapped memory.

The munmap() function unmaps memory from the currently running processes address space.

Parameters
addressThe starting virtual address of the memory area to be unmapped.
lengthThe length of the memory area to be unmapped.
Returns
On success, 0, on failure returns ERR and errno is set.

Definition at line 6 of file munmap.c.

References _syscall_errno(), _syscall_munmap(), address, ERR, and errno.

Referenced by _heap_unmap_memory(), munmap_generic(), screen_deinit(), surface_free(), and window_free().

◆ nanosleep()

uint64_t nanosleep ( clock_t  timeout)

System call for sleeping.

The nanosleep() function suspends the execution of the calling thread for a specified duration.

Parameters
timeoutThe duration in nanoseconds for which to sleep, if equal to CLOCKS_NEVER then it will sleep forever, not sure why you would want to do that but you can.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 6 of file nanosleep.c.

References _syscall_errno(), _syscall_nanosleep(), ERR, and errno.

Referenced by stats_update(), and thrd_sleep().

◆ spawn()

pid_t spawn ( const char **  argv,
const spawn_fd_t fds,
const char *  cwd,
spawn_attr_t attr 
)

System call for creating child processes.

Parameters
argvA NULL-terminated array of strings, where argv[0] is the filepath to the desired executable. This array will be pushed to the child stack and the child can find a pointer to this array in its rsi register, along with its length in the rdi register.
fdsA array of file descriptors to be duplicated to the child process. Each spawn_fd_t in the array specifies a source file descriptor in the parent (.parent) and its destination in the child (.child). The array must be terminated by SPAWN_FD_END.
cwdThe working directory for the child process. If NULL, the child inherits the parent's current working directory.
attrThe spawn attributes, allows for specifying additional information for the new process, if equal to NULL then use defaults.
Returns
On success, returns the childs pid, on failure returns ERR and errno is set.

Definition at line 6 of file spawn.c.

References _syscall_errno(), _syscall_spawn(), ERR, and errno.

Referenced by pipeline_execute_cmd(), spawn_program(), startmenu_procedure(), system(), and terminal_procedure().

◆ uptime()

clock_t uptime ( void  )