PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
proc.h
Go to the documentation of this file.
1#ifndef _SYS_PROC_H
2#define _SYS_PROC_H 1
3
4#include <stdatomic.h>
5#include <stdint.h>
6
7#if defined(__cplusplus)
8extern "C"
9{
10#endif
11
12#include "_internal/ERR.h"
13#include "_internal/NULL.h"
14#include "_internal/clock_t.h"
15#include "_internal/config.h"
16#include "_internal/fd_t.h"
17#include "_internal/pid_t.h"
18#include "_internal/tid_t.h"
19
20/**
21 * @brief Process management header.
22 * @ingroup libstd
23 * @defgroup libstd_sys_proc Process management
24 *
25 * The `sys/proc.h` header handles process management, including process spawning, managing a processes address space,
26 * scheduling, and similar.
27 *
28 * @{
29 */
30
31/**
32 * @brief The environment variables of the current process.
33 *
34 * The `environ` variable is a NULL-terminated array of strings representing the environment variables of the current
35 * process in the format "KEY=VALUE".
36 */
37extern char** environ;
38
39/**
40 * @brief Priority type.
41 * @typedef priority_t
42 *
43 * The `priority_t` type is used to store the scheduling priority of a process.
44 *
45 */
47
48#define PRIORITY_MAX 63 ///< The maximum priority value, inclusive.
49#define PRIORITY_MAX_USER 31 ///< The maximum priority user space is allowed to specify, inclusive.
50#define PRIORITY_MIN 0 ///< The minimum priority value.
51
52/**
53 * @brief Spawn behaviour flags.
54 * @enum spawn_flags_t
55 */
56typedef enum
57{
58 SPAWN_DEFAULT = 0, ///< Default spawn behaviour.
59 /**
60 * Starts the spawned process in a suspended state. The process will not begin executing until a "start" note is
61 * received.
62 *
63 * The purpose of this flag is to allow the parent process to modify the child process before it starts executing,
64 * for example modifying its environment variables.
65 */
66 SPAWN_SUSPEND = 1 << 0,
67 SPAWN_EMPTY_FDS = 1 << 1, ///< Dont inherit the file descriptors of the parent process.
68 SPAWN_STDIO_FDS = 1 << 2, ///< Only inherit stdin, stdout and stderr from the parent process.
69 SPAWN_EMPTY_NS = 1 << 3, ///< Dont inherit the mountpoints of the parent's namespace.
70 SPAWN_EMPTY_ENV = 1 << 4, ///< Don't inherit the parent's environment variables.
71 SPAWN_EMPTY_CWD = 1 << 5, ///< Don't inherit the parent's current working directory, starts at root (/).
73
74/**
75 * @brief System call for spawning new processes.
76 *
77 * By default, the spawned process will inherit the file table, namespace, environment variables, priority and current working directory of the parent process.
78 *
79 * @param argv A NULL-terminated array of strings, where `argv[0]` is the filepath to the desired executable.
80 * @param flags Spawn behaviour flags.
81 * @return On success, the childs pid. On failure, `ERR` and `errno` is set.
82 */
83pid_t spawn(const char** argv, spawn_flags_t flags);
84
85/**
86 * @brief System call to retrieve the current pid.
87 *
88 * @return The running processes pid.
89 */
90pid_t getpid(void);
91
92/**
93 * @brief System call to retrieve the current tid.
94 *
95 * @return The running threads tid.
96 */
97tid_t gettid(void);
98
99/**
100 * @brief The size of a memory page in bytes.
101 */
102#define PAGE_SIZE 0x1000
103
104/**
105 * @brief Convert a size in bytes to pages.
106 *
107 * @param amount The amount of bytes.
108 * @return The amount of pages.
109 */
110#define BYTES_TO_PAGES(amount) (((amount) + PAGE_SIZE - 1) / PAGE_SIZE)
111
112/**
113 * @brief Size of an object in pages.
114 *
115 * @param object The object to calculate the page size of.
116 * @return The amount of pages.
117 */
118#define PAGE_SIZE_OF(object) BYTES_TO_PAGES(sizeof(object))
119
120/**
121 * @brief Memory protection flags.
122 * @typedef prot_t
123 */
124typedef enum
125{
126 PROT_NONE = 0, ///< Invalid memory, cannot be accessed.
127 PROT_READ = (1 << 0), ///< Readable memory.
128 PROT_WRITE = (1 << 1), ///< Writable memory.
129 PROT_EXECUTE = (1 << 2) ///< Executable memory.
131
132/**
133 * @brief System call to map memory from a file.
134 *
135 * The `mmap()` function maps memory to the currently running processes address space from a file, this is the only way
136 * to allocate virtual memory from userspace. An example usage would be to map the `/dev/zero` file which would allocate
137 * zeroed memory.
138 *
139 * @param fd The open file descriptor of the file to be mapped.
140 * @param address The desired virtual destination address, if equal to `NULL` the kernel will choose a available
141 * address, will be rounded down to the nearest page multiple.
142 * @param length The length of the segment to be mapped, note that this length will be rounded up to the nearest page
143 * multiple by the kernel factoring in page boundaries.
144 * @param prot Protection flags, must have at least `PROT_READ` set.
145 * @return On success, returns the address of the mapped memory, will always be page aligned, on failure returns `NULL`
146 * and errno is set.
147 */
148void* mmap(fd_t fd, void* address, uint64_t length, prot_t prot);
149
150/**
151 * @brief System call to unmap mapped memory.
152 *
153 * The `munmap()` function unmaps memory from the currently running processes address space.
154 *
155 * @param address The starting virtual address of the memory area to be unmapped.
156 * @param length The length of the memory area to be unmapped.
157 * @return On success, returns the address of the unmapped memory, on failure returns `NULL` and errno is set.
158 */
159void* munmap(void* address, uint64_t length);
160
161/**
162 * @brief System call to change the protection flags of memory.
163 *
164 * The `mprotect()` changes the protection flags of a virtual memory area in the currently running processes address
165 * space.
166 *
167 * @param address The starting virtual address of the memory area to be modified.
168 * @param length The length of the memory area to be modifed.
169 * @param prot The new protection flags of the memory area, if equal to `PROT_NONE` the memory area will be
170 * unmapped.
171 * @return On success, returns the address of the modified memory area, on failure returns `NULL` and errno is set.
172 */
173void* mprotect(void* address, uint64_t length, prot_t prot);
174
175/**
176 * @brief Futex operation enum.
177 *
178 * The `futex_op_t` enum is used to specify the desired futex operation in the `futex()` function.
179 *
180 */
181typedef enum
182{
183 /**
184 * @brief Wait until the timeout expires or the futex value changes.
185 *
186 * If the value at the futex address is not equal to `val`, the call returns immediately with `EAGAIN`.
187 * Otherwise, the calling thread is put to sleep until another thread wakes it up or the specified timeout expires.
188 */
190 /**
191 * @brief Wake up one or more threads waiting on the futex.
192 *
193 * Wakes up a maximum of `val` number of threads that are currently waiting on the futex at the specified address.
194 * If `val` is `FUTEX_ALL`, all waiting threads are woken up.
195 */
197} futex_op_t;
198
199/**
200 * @brief Futex wake all constant.
201 *
202 * The `FUTEX_ALL` constant can be used as the `val` argument when using the `FUTEX_WAIT` operating in the `futex()`
203 * function to wake upp all waiters.
204 *
205 */
206#define FUTEX_ALL UINT64_MAX
207
208/**
209 * @brief System call for fast user space mutual exclusion.
210 *
211 * The `futex()` function provides a fast user-space syncronization mechanism. It can be used to implement userspace
212 * mutexes, conditional variables, etc.
213 *
214 * @param addr A pointer to an atomic 64-bit unsigned integer.
215 * @param val The value used by the futex operation, its meaning depends on the operation.
216 * @param op The futex operation to perform (e.g., `FUTEX_WAIT` or `FUTEX_WAKE`).
217 * @param timeout An optional timeout for `FUTEX_WAIT`. If `CLOCKS_NEVER`, it waits forever.
218 * @return On success, depends on the operation. On failure, `ERR` and errno is set.
219 */
220uint64_t futex(atomic_uint64_t* addr, uint64_t val, futex_op_t op, clock_t timeout);
221
222/**
223 * @brief System call for retreving the time since boot.
224 *
225 * The `uptime()` function retrieves the system uptime since boot in clock ticks.
226 *
227 * @return The system uptime in clock ticks.
228 */
229clock_t uptime(void);
230
231/**
232 * @brief System call for sleeping.
233 *
234 * The `nanosleep()` function suspends the execution of the calling thread for a specified duration.
235 *
236 * @param timeout The duration in nanoseconds for which to sleep, if equal to `CLOCKS_NEVER` then it will sleep forever,
237 * not sure why you would want to do that but you can.
238 * @return On success, `0`. On failure, `ERR` and errno is set.
239 */
240uint64_t nanosleep(clock_t timeout);
241
242/**
243 * @brief Synchronization object.
244 *
245 * The `sync_t` structure is used to implement user space synchronization primitives. Its the object mapped when calling
246 * 'mmap()' on a opened sync file. For more information check the `sync.h` header.
247 *
248 * @see sync.h
249 */
250typedef struct
251{
252 atomic_uint64_t value; ///< The value of the sync object.
253} sync_t;
254
255#if defined(__cplusplus)
256}
257#endif
258
259#endif
260
261/** @} */
futex_op_t
Futex operation enum.
Definition proc.h:182
tid_t gettid(void)
System call to retrieve the current tid.
Definition gettid.c:6
void * mmap(fd_t fd, void *address, uint64_t length, prot_t prot)
System call to map memory from a file.
Definition mmap.c:6
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.
Definition futex.c:6
spawn_flags_t
Spawn behaviour flags.
Definition proc.h:57
pid_t spawn(const char **argv, spawn_flags_t flags)
System call for spawning new processes.
Definition spawn.c:6
char ** environ
The environment variables of the current process.
clock_t uptime(void)
System call for retreving the time since boot.
Definition uptime.c:6
prot_t
Memory protection flags.
Definition proc.h:125
void * mprotect(void *address, uint64_t length, prot_t prot)
System call to change the protection flags of memory.
Definition mprotect.c:6
pid_t getpid(void)
System call to retrieve the current pid.
Definition getpid.c:6
uint8_t priority_t
Priority type.
Definition proc.h:46
void * munmap(void *address, uint64_t length)
System call to unmap mapped memory.
Definition munmap.c:6
uint64_t nanosleep(clock_t timeout)
System call for sleeping.
Definition nanosleep.c:6
@ FUTEX_WAKE
Wake up one or more threads waiting on the futex.
Definition proc.h:196
@ FUTEX_WAIT
Wait until the timeout expires or the futex value changes.
Definition proc.h:189
@ SPAWN_EMPTY_FDS
Dont inherit the file descriptors of the parent process.
Definition proc.h:67
@ SPAWN_DEFAULT
Default spawn behaviour.
Definition proc.h:58
@ SPAWN_STDIO_FDS
Only inherit stdin, stdout and stderr from the parent process.
Definition proc.h:68
@ SPAWN_EMPTY_NS
Dont inherit the mountpoints of the parent's namespace.
Definition proc.h:69
@ SPAWN_SUSPEND
Definition proc.h:66
@ SPAWN_EMPTY_ENV
Don't inherit the parent's environment variables.
Definition proc.h:70
@ SPAWN_EMPTY_CWD
Don't inherit the parent's current working directory, starts at root (/).
Definition proc.h:71
@ PROT_READ
Readable memory.
Definition proc.h:127
@ PROT_EXECUTE
Executable memory.
Definition proc.h:129
@ PROT_WRITE
Writable memory.
Definition proc.h:128
@ PROT_NONE
Invalid memory, cannot be accessed.
Definition proc.h:126
__UINT64_TYPE__ tid_t
Thread Identifier.
Definition tid_t.h:12
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
__UINT64_TYPE__ pid_t
Process Identifier.
Definition pid_t.h:11
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:95
static const path_flag_t flags[]
Definition path.c:42
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
Synchronization object.
Definition proc.h:251
atomic_uint64_t value
The value of the sync object.
Definition proc.h:252