PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1#pragma once
2
4#include <kernel/fs/cwd.h>
5#include <kernel/fs/devfs.h>
8#include <kernel/io/ring.h>
9#include <kernel/ipc/note.h>
10#include <kernel/mem/space.h>
11#include <kernel/proc/env.h>
12#include <kernel/proc/group.h>
13#include <kernel/sched/sched.h>
14#include <kernel/sched/thread.h>
15#include <kernel/sched/wait.h>
16#include <kernel/sync/futex.h>
17#include <kernel/sync/rcu.h>
18#include <kernel/utils/map.h>
19#include <kernel/utils/ref.h>
20
21#include <stdatomic.h>
22
23/**
24 * @brief Process management.
25 * @defgroup kernel_proc Process Subsystem
26 * @ingroup kernel
27 *
28 * Processes store the shared resources for threads of execution, for example the address space and open files.
29 *
30 * @{
31 */
32
33/**
34 * @brief Process flags enum.
35 * @enum process_flags_t
36 */
37typedef enum
38{
40 PROCESS_DYING = 1 << 0,
43
44/**
45 * @brief Represents the threads in a process.
46 * @struct process_threads_t
47 */
48typedef struct
49{
50 _Atomic(tid_t) newTid;
51 list_t list; ///< Reads are RCU protected, writes require the lock.
55
56/**
57 * @brief Maximum length of a process exit status.
58 */
59#define PROCESS_STATUS_MAX 256
60
61/**
62 * @brief Process exit status structure.
63 * @struct process_status_t
64 */
70
71/**
72 * @brief Process structure.
73 * @struct process_t
74 */
103
104/**
105 * @brief Global list of all processes.
106 *
107 * @warning Should only be read while in a RCU read-side critical section.
108 */
109extern list_t _processes;
110
111/**
112 * @brief Allocates and initializes a new process.
113 *
114 * It is the responsibility of the caller to `UNREF()` the returned process.
115 *
116 * @param priority The priority of the new process.
117 * @param group A member of the group to add the new process to, or `NULL` to create a new group for the process.
118 * @param ns The namespace to use for the new process.
119 * @return On success, the newly created process. On failure, `NULL` and `errno` is set.
120 */
122
123/**
124 * @brief Retrieves the process of the currently running thread.
125 *
126 * @note Will not increment the reference count of the returned process, as we consider the currently running thread to
127 * always be referencing its process.
128 *
129 * @return The process of the currently running thread.
130 */
131static inline process_t* process_current(void)
132{
133 CLI_SCOPE();
135}
136
137/**
138 * @brief Retrieves the process of the currently running thread without disabling interrupts.
139 *
140 * @note Will not increment the reference count of the returned process, as we consider the currently running thread to
141 * always be referencing its process.
142 *
143 * @return The process of the currently running thread.
144 */
146{
148}
149
150/**
151 * @brief Gets a process by its ID.
152 *
153 * It is the responsibility of the caller to `UNREF()` the returned process.
154 *
155 * @param id The ID of the process to get.
156 * @return A reference to the process with the specified ID or `NULL` if no such process exists.
157 */
159
160/**
161 * @brief Gets the namespace of a process.
162 *
163 * It is the responsibility of the caller to `UNREF()` the returned namespace.
164 *
165 * @param process The process to get the namespace of.
166 * @return On success, a reference to the namespace of the process. On failure, `NULL` and `errno` is set:
167 * - `EINVAL`: Invalid parameters.
168 */
170
171/**
172 * @brief Sets the namespace of a process.
173 *
174 * @param process The process to set the namespace of.
175 * @param ns The new namespace for the process.
176 */
177void process_set_ns(process_t* process, namespace_t* ns);
178
179/**
180 * @brief Kills a process, pushing it to the reaper.
181 *
182 * The process will still exist until the reaper removes it.
183 *
184 * @param process The process to kill.
185 * @param status The exit status of the process.
186 */
187void process_kill(process_t* process, const char* status);
188
189/**
190 * @brief Removes a process from the system.
191 *
192 * This should only be called by the reaper.
193 *
194 * @param process The process to remove.
195 */
196void process_remove(process_t* process);
197
198/**
199 * @brief Gets the first thread of a process.
200 *
201 * @warning Must be used within a RCU read-side critical section.
202 *
203 * @param process The process to get the first thread of.
204 * @return The first thread of the process, or `NULL` if the process has no threads.
205 */
207{
208 return CONTAINER_OF_SAFE(list_first(&process->threads.list), thread_t, processEntry);
209}
210
211/**
212 * @brief Gets the amount of threads in a process.
213 *
214 * @warning Must be used within a RCU read-side critical section.
215 *
216 * @param process The process to get the thread amount of.
217 * @return The amount of threads in the process.
218 */
220{
221 return process->threads.count;
222}
223
224/**
225 * @brief Macro to iterate over all threads in a process.
226 *
227 * @warning Must be used within a RCU read-side critical section.
228 *
229 * @param thread Loop variable, a pointer to `thread_t`.
230 * @param process The process to iterate the threads of.
231 */
232#define PROCESS_RCU_THREAD_FOR_EACH(thread, process) LIST_FOR_EACH(thread, &(process)->threads.list, processEntry)
233
234/**
235 * @brief Macro to iterate over all processes.
236 *
237 * @warning Must be used within a RCU read-side critical section.
238 *
239 * @param process Loop variable, a pointer to `process_t`.
240 */
241#define PROCESS_RCU_FOR_EACH(process) LIST_FOR_EACH(process, &_processes, entry)
242
243/**
244 * @brief Sets the command line arguments for a process.
245 *
246 * This value is only used for the `/proc/[pid]/cmdline` file.
247 *
248 * @param process The process to set the cmdline for.
249 * @param argv The array of argument strings.
250 * @param argc The number of arguments.
251 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
252 * - `EINVAL`: Invalid parameters.
253 * - `ENOMEM`: Out of memory.
254 */
255uint64_t process_set_cmdline(process_t* process, char** argv, uint64_t argc);
256
257/**
258 * @brief Checks if a process has a thread with the specified thread ID.
259 *
260 * @param process The process to check.
261 * @param tid The thread ID to look for.
262 * @return `true` if the process has a thread with the specified ID, `false` otherwise.
263 */
264bool process_has_thread(process_t* process, tid_t tid);
265
266/**
267 * @brief Gets the kernel process.
268 *
269 * The kernel process will be initalized lazily on the first call to this function, which should happen during early
270 * boot.
271 *
272 * Will never return `NULL` and will not increment the reference count of the returned process.
273 *
274 * @return The kernel process.
275 */
277
278/** @} */
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
#define CONFIG_MAX_RINGS
Maximum rings configuration.
Definition config.h:192
#define CLI_SCOPE()
Macro to increment CLI depth for the duration of the current scope.
Definition cli.h:56
void process_kill(process_t *process, const char *status)
Kills a process, pushing it to the reaper.
Definition process.c:240
void process_set_ns(process_t *process, namespace_t *ns)
Sets the namespace of a process.
Definition process.c:227
static process_t * process_current_unsafe(void)
Retrieves the process of the currently running thread without disabling interrupts.
Definition process.h:145
static uint64_t process_rcu_thread_count(process_t *process)
Gets the amount of threads in a process.
Definition process.h:219
process_t * process_get_kernel(void)
Gets the kernel process.
Definition process.c:374
static thread_t * process_rcu_first_thread(process_t *process)
Gets the first thread of a process.
Definition process.h:206
process_t * process_new(priority_t priority, group_member_t *group, namespace_t *ns)
Allocates and initializes a new process.
Definition process.c:125
bool process_has_thread(process_t *process, tid_t tid)
Checks if a process has a thread with the specified thread ID.
Definition process.c:358
#define PROCESS_STATUS_MAX
Maximum length of a process exit status.
Definition process.h:59
void process_remove(process_t *process)
Removes a process from the system.
Definition process.c:284
uint64_t process_set_cmdline(process_t *process, char **argv, uint64_t argc)
Sets the command line arguments for a process.
Definition process.c:294
static process_t * process_current(void)
Retrieves the process of the currently running thread.
Definition process.h:131
list_t _processes
Global list of all processes.
process_flags_t
Process flags enum.
Definition process.h:38
namespace_t * process_get_ns(process_t *process)
Gets the namespace of a process.
Definition process.c:206
process_t * process_get(pid_t id)
Gets a process by its ID.
Definition process.c:192
@ PROCESS_NONE
Definition process.h:39
@ PROCESS_DYING
Definition process.h:40
@ PROCESS_SUSPENDED
Definition process.h:41
sched_t PERCPU _pcpu_sched
The per CPU scheduler.
static list_entry_t * list_first(list_t *list)
Gets the first entry in the list without removing it.
Definition list.h:406
uint8_t priority_t
Priority type.
Definition proc.h:41
__UINT64_TYPE__ tid_t
Thread Identifier.
Definition tid_t.h:12
__UINT64_TYPE__ pid_t
Process Identifier.
Definition pid_t.h:11
#define CONTAINER_OF_SAFE(ptr, type, member)
Safe container of macro.
static const path_flag_t flags[]
Definition path.c:47
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Definition cwd.h:18
Environment structure.
Definition env.h:33
File table structure.
Definition file_table.h:24
Per-process futex context.
Definition futex.h:34
Group member structure.
Definition group.h:35
The kernel-side ring context structure.
Definition ring.h:182
A entry in a doubly linked list.
Definition list.h:37
A doubly linked list.
Definition list.h:46
A simple ticket lock implementation.
Definition lock.h:44
Map entry structure.
Definition map.h:69
Namespace structure.
Definition namespace.h:57
Per-process note handler.
Definition note.h:131
Process exit status structure.
Definition process.h:66
Process structure.
Definition process.h:76
file_table_t fileTable
Definition process.h:88
rcu_entry_t rcu
Definition process.h:101
group_member_t group
Definition process.h:100
uint64_t argc
Definition process.h:99
env_t env
Definition process.h:97
lock_t nspaceLock
Definition process.h:86
note_handler_t noteHandler
Definition process.h:92
char ** argv
Definition process.h:98
futex_ctx_t futexCtx
Definition process.h:89
map_entry_t mapEntry
Definition process.h:79
ref_t ref
Definition process.h:77
wait_queue_t suspendQueue
Definition process.h:93
perf_process_ctx_t perf
Definition process.h:90
_Atomic(process_flags_t) flags
space_t space
Definition process.h:84
list_entry_t zombieEntry
Definition process.h:80
process_status_t status
Definition process.h:83
namespace_t * nspace
Definition process.h:85
pid_t id
Definition process.h:81
cwd_t cwd
Definition process.h:87
process_threads_t threads
Definition process.h:96
list_entry_t entry
Definition process.h:78
wait_queue_t dyingQueue
Definition process.h:94
_Atomic(priority_t) priority
Represents the threads in a process.
Definition process.h:49
uint64_t count
Definition process.h:52
_Atomic(tid_t) new Tid
list_t list
Reads are RCU protected, writes require the lock.
Definition process.h:51
Intrusive RCU head structure.
Definition rcu.h:65
Reference counting structure.
Definition ref.h:52
thread_t *volatile runThread
The currently running thread on this CPU.
Definition sched.h:406
Virtual address space structure.
Definition space.h:78
Thread of execution structure.
Definition thread.h:61
process_t * process
The parent process that the thread executes within.
Definition thread.h:62
The primitive that threads block on.
Definition wait.h:185