The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1#pragma once
2
3#include "common/stdint.h"
5#include "kernel/memory/vma.h"
7#include "arch/arch.h"
11
12// maximum number of concurrent processes.
13#define PROCESS_MAX 64
14
15// invalid PID sentinel.
16#define PID_NONE 0
17
18// the first process (/bin/init); the system halts if it exits.
19#define PID_INIT 1
20
21// size of per-process kernel stack (4 KiB).
22#define KERNEL_STACK_SIZE (2 * PAGE_SIZE)
23
24/**
25 * process states.
26 */
27typedef enum
28{
29 PROC_STATE_UNUSED = 0, // slot is free
30 PROC_STATE_NEW, // slot claimed by process_create, not yet runnable
31 PROC_STATE_RUNNING, // currently executing
32 PROC_STATE_READY, // runnable, waiting for CPU
33 PROC_STATE_BLOCKED, // waiting for I/O or event
34 PROC_STATE_ZOMBIE, // exited, waiting to be reaped
35 PROC_STATE_STOPPED, // stopped by a signal until SIGCONT
37
38/**
39 * a change of a child that waitpid has not reported yet.
40 */
41typedef enum
42{
44 WAIT_REPORT_STOPPED, // stopped (WUNTRACED)
45 WAIT_REPORT_CONTINUED, // continued (WCONTINUED)
47
48#define PATH_MAX 256
49
50// longest process name, NUL included
51#define PROCESS_NAME_MAX 32
52
53/**
54 * where paths start, and the mode bits new files lose. the threads of a
55 * process use one together; it is freed when the last lets it go.
56 */
57typedef struct fs_context
58{
59 volatile uint32_t users; // the processes (threads) that use it (atomic)
60 uint32_t umask; // permission bits taken off new files
61 char cwd[PATH_MAX]; // current working directory (the real path)
62 char root[PATH_MAX]; // the root directory (chroot): a path starts here
64
65/**
66 * process control block.
67 */
68typedef struct process
69{
70 uint32_t pid; // process identifier (the thread id)
71 uint32_t tgid; // the thread group: the pid of its first thread
72 // (the leader). getpid gives it.
73 struct process *leader; // the leader (itself for it); its slot stays
74 // until every thread of the group has ended
75 volatile uint32_t threads; // in the leader: the threads not ended yet (atomic)
76 bool group_exiting; // in the leader: every thread is to end
77 bool group_stopped; // in the leader: the process is stopped
78 struct process *exec_thread; // in the leader: the thread in execve,
79 // which ends the others; or NULL
80 int32_t group_status; // in the leader: the wait status they end with
81 uintptr_t tls; // the thread pointer (arch_set_tls), or 0
82 uintptr_t clear_tid; // a user address set to 0 when the thread ends
83 // (CLONE_CHILD_CLEARTID), or 0
84 uint32_t parent_pid; // the parent group's tgid; PID_NONE for PID 1 (and
85 // for orphans when there is no init, and for a
86 // thread that ended: the kernel reaps them)
87 uint32_t pgid; // process group (job control)
88 uint32_t sid; // session
89 uint32_t uid, euid, suid; // real, effective and saved user id
90 uint32_t gid, egid, sgid; // real, effective and saved group id
91 int32_t nice; // -20 (most CPU) to 19 (least): the time slice length
92 process_state_t state; // current state
93 user_space_t *space; // the user memory (shared by threads), or NULL
94 // once released
95 int32_t exit_status; // wait status (wait_status.h), valid in ZOMBIE state
96 uintptr_t kernel_stack; // lowest address of the kernel stack (kstack.h)
97 uintptr_t kernel_esp; // saved kernel ESP (for context switch)
98 uintptr_t entry; // ELF entry point (used by trampoline on first schedule)
99 wait_queue_t child_exit; // where this process waits for a child to exit
100 volatile uint32_t child_events; // changes before each wake of child_exit
101 bool reaped; // a waitpid or the orphan reap took this zombie and destroys it
102 volatile bool on_cpu; // a core runs it, or has not ended the switch away from it
103 uint32_t cpu; // the core it runs on, or whose run queue it is on, or last ran on
104 char **argv; // kernel-heap copy of argv (freed at exit)
105
106 // intrusive doubly-linked list for the scheduler ready queue.
109
110 // intrusive singly-linked list for a wait queue (Phase L3).
111 // valid only while state == PROC_STATE_BLOCKED and the process sits
112 // on some wait_queue_t. separate from the ready links so a process is
113 // never on both lists at once.
115 wait_queue_t *waiting_on; // the queue it sleeps on (BLOCKED), or NULL
116 bool interruptible; // a signal may end the sleep
117
118 // the futex it sleeps on (futex.c): its address space and user address
121 bool futex_woken; // a FUTEX_WAKE ended the sleep
122
123 signal_state_t signals; // pending and blocked
124 signal_handlers_t *handlers; // what each signal does (shared by threads);
125 // kept until the slot is freed
126 uint32_t syscall_nr; // the syscall in progress (for a restart)
127 uint64_t alarm_deadline; // in the leader: tick of the SIGALRM alarm, 0
128 // for none
129 uint8_t stop_signal; // the signal that stopped it (STOPPED); in
130 // the leader, the one that stopped the process
131 wait_report_t wait_report; // in the leader: a stop or continue for waitpid
132 uint32_t utime; // timer ticks spent in user code
133 uint32_t stime; // timer ticks spent in the kernel for it
134 uint32_t cutime; // utime of reaped children (and theirs)
135 uint32_t cstime; // stime of reaped children (and theirs)
136 uintptr_t fault_addr; // the last bad user access (for the report)
138
139 // the FPU/SSE registers while another process runs (arch_fpu_save)
140 uint8_t fpu_state[ARCH_FPU_STATE_SIZE] __attribute__((aligned(16)));
141 char name[PROCESS_NAME_MAX]; // the program's file name (procfs)
142 fs_context_t *fs; // cwd, root and umask (shared by threads), or
143 // NULL once released
144 fd_table_t *files; // the descriptors (shared by threads), or NULL
145 // once released
146 file_t *held_files[FD_MAX]; // files fd_get keeps open for the syscall in
147 uint32_t held_count; // progress, while the table is shared
149
150/**
151 * add a user to an fs context (a new thread that shares it).
152 * @return fs.
153 */
155
156/**
157 * remove a user from an fs context; the last user frees it.
158 * @param fs the context, or NULL (nothing is done).
159 */
161
162/**
163 * initialize the process subsystem.
164 * must be called once during kernel startup.
165 */
166void process_init(void);
167
168/**
169 * create a new process and return its PCB.
170 * allocates a PID and initializes the fd table with stdin/stdout/stderr.
171 * the process starts in PROC_STATE_NEW; scheduler_ready() makes it runnable.
172 * @return pointer to the new process, or NULL if the table is full or its
173 * kernel stack or address space could not be allocated.
174 */
176
177struct registers;
178
179/**
180 * fork the current process. the child is a copy of the caller: the same
181 * memory (shared copy-on-write), areas, fds, cwd and break. it is made
182 * runnable at once and resumes from the same syscall, which returns 0 in
183 * the child.
184 * @param regs the caller's saved user registers (its syscall frame).
185 * @return the child, or NULL if the process table, a kernel stack or
186 * memory for the page tables ran out.
187 */
188process_t *process_fork(const struct registers *regs);
189
190/**
191 * make a thread of the current process: a new task in its group that
192 * shares its memory, descriptors, fs context and handlers. it resumes from
193 * the same syscall, which returns 0 there, with stack as its user stack
194 * pointer. the caller makes it runnable.
195 * @param regs the caller's saved user registers (its syscall frame).
196 * @param stack the thread's user stack pointer.
197 * @return the thread, or NULL if the table or the memory ran out.
198 */
199process_t *process_clone_thread(const struct registers *regs, uintptr_t stack);
200
201/**
202 * send a signal to the threads of a group (signal_send to each).
203 * @param tgid the group.
204 * @param except a thread that is left out, or NULL.
205 * @param sig the signal.
206 */
207void process_signal_threads(uint32_t tgid, process_t *except, int sig);
208
209/**
210 * make a thread the leader of its group in place of the old leader, which
211 * has ended (a zombie) and is the only other member: the thread takes the
212 * leader's pid and its place for the parent, and the old slot is reaped
213 * by the kernel (execve from a thread that is not the leader).
214 * @param proc the thread (the current one).
215 */
217
218/**
219 * give every other thread of a process's group its user and group ids,
220 * its process group and its session (they belong to the process).
221 * @param proc the thread whose values the others take.
222 */
224
225/**
226 * choose the thread of a group that takes a signal sent to the process:
227 * the leader if it does not block mask, else a thread that does not,
228 * else any of them (the leader first).
229 * @param tgid the group.
230 * @param mask the signal's bit.
231 * @return the thread, or NULL if the group has none left.
232 */
234
235/**
236 * get the currently running process.
237 * @return pointer to the current process, or NULL if none is running.
238 */
240
241/**
242 * set the currently running process.
243 * @param proc the process to mark as current.
244 */
246
247/**
248 * destroy a process and free its slot in the process table.
249 * closes any open file descriptors.
250 * @param proc the process to destroy.
251 */
252void process_destroy(process_t *proc);
253
254/**
255 * release what a process owns apart from its kernel stack and table slot:
256 * close its fds, free its argv copy and destroy its address space (after
257 * switching away from it, when it is the current process). run by an
258 * exiting process in its own context, since closing a file may sleep.
259 * safe to call more than once.
260 * @param proc the process.
261 */
263
264/**
265 * detach the children of an exiting group (call when its last thread
266 * ends; parent is that thread): zombie children are destroyed
267 * now (nobody wants their status), running ones get init (PID 1) as their
268 * parent, which reaps them. without init they lose their parent
269 * (PID_NONE) and the kernel reaps them when they exit.
270 * @param parent the exiting process.
271 */
273
274/**
275 * record that an orphan (no parent) has become a zombie and must be reaped
276 * by the kernel. the reap waits until the zombie has left its core.
277 */
279
280/**
281 * destroy orphan zombies. called by the scheduler right after a context
282 * switch, on the next process's stack, so a dead process's own stack is
283 * never freed while in use. runs with interrupts masked.
284 */
285void process_reap_orphans(void);
286
287/**
288 * find a child of a process that has something for waitpid: it exited
289 * (a zombie), or, with WUNTRACED / WCONTINUED in options, it stopped or
290 * continued and that was not reported yet. a zombie that it returns is
291 * marked reaped, so no other waitpid takes it. call it with the signal
292 * state lock held (signal_state_lock), which guards the reports.
293 * @param parent the parent.
294 * @param pid a child's pid, or -1 for any child.
295 * @param options waitpid options (wait_status.h).
296 * @param has_child set to true if parent has at least one matching child.
297 * @return the child, or NULL if none has anything to report yet.
298 */
300 bool *has_child);
301
302/**
303 * list the pids of a process group.
304 * @param pgid the group.
305 * @param pids receives up to max pids.
306 * @param max capacity of pids.
307 * @return the number stored.
308 */
310
311/**
312 * true if some live process is in the group and the session.
313 */
315
316/**
317 * call fn for every process in the table, with the table locked and
318 * interrupts masked: fn must not sleep.
319 * @param fn the function.
320 * @param ctx passed to fn.
321 */
322void process_for_each(void (*fn)(process_t *proc, void *ctx), void *ctx);
323
324/**
325 * a copy of what procfs shows about a process.
326 */
327typedef struct
328{
333 uint32_t areas; // memory areas (VMAs)
337 uint32_t utime; // timer ticks in user code
338 uint32_t stime; // timer ticks in the kernel for it
339 uint32_t pages; // pages of RAM it has mapped (its resident memory)
341
342/**
343 * list the pids in use.
344 * @param pids receives up to max pids, in table order.
345 * @param max capacity of pids.
346 * @return the number stored.
347 */
349
350/**
351 * copy what procfs shows about a process, safely against its exit.
352 * @param pid the process.
353 * @param info receives the copy.
354 * @return false if no such process exists.
355 */
356bool process_info(uint32_t pid, process_info_t *info);
357
358/**
359 * set a process's name from a program path (its last component).
360 * @param proc the process.
361 * @param path the program's path.
362 */
363void process_set_name(process_t *proc, const char *path);
364
365/**
366 * the timer ticks that the cores spent in their idle loops since boot,
367 * added over all cores.
368 */
370
371/**
372 * charge one timer tick to the current process (timer interrupt).
373 * @param user true if user code was running.
374 */
375void process_account_tick(bool user);
376
377/**
378 * give a new process the user and group ids and umask of another.
379 */
380void process_copy_credentials(process_t *child, const process_t *parent);
381
382/**
383 * look up a process by PID. the pointer stays valid only while the process
384 * cannot be destroyed (it is the caller, or the caller's unreaped child).
385 * @param pid the process identifier.
386 * @return pointer to the process, or NULL if not found.
387 */
#define ARCH_FPU_STATE_SIZE
Definition arch.h:284
#define FD_MAX
Definition fd_table.h:16
struct fd_table fd_table_t
the descriptors of a process.
uint32_t fs
Definition gdt.c:22
struct signal_handlers signal_handlers_t
what a process does with each signal.
struct file file_t
Definition file.h:38
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
int int32_t
Definition stdint.h:14
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
void process_set_current(process_t *proc)
set the currently running process.
Definition process.c:270
void fs_context_put(fs_context_t *fs)
remove a user from an fs context; the last user frees it.
Definition process.c:327
bool process_group_exists(uint32_t pgid, uint32_t sid)
true if some live process is in the group and the session.
Definition process.c:540
void process_note_orphan_zombie(void)
record that an orphan (no parent) has become a zombie and must be reaped by the kernel.
Definition process.c:440
bool process_info(uint32_t pid, process_info_t *info)
copy what procfs shows about a process, safely against its exit.
Definition process.c:588
void process_sync_threads(process_t *proc)
give every other thread of a process's group its user and group ids, its process group and its sessio...
Definition process.c:773
void process_take_leader(process_t *proc)
make a thread the leader of its group in place of the old leader, which has ended (a zombie) and is t...
Definition process.c:796
process_t * process_clone_thread(const struct registers *regs, uintptr_t stack)
make a thread of the current process: a new task in its group that shares its memory,...
Definition process.c:167
uint64_t process_idle_ticks(void)
the timer ticks that the cores spent in their idle loops since boot, added over all cores.
Definition process.c:646
void process_orphan_children(process_t *parent)
detach the children of an exiting group (call when its last thread ends; parent is that thread): zomb...
Definition process.c:392
process_t * process_pick_thread(uint32_t tgid, uint32_t mask)
choose the thread of a group that takes a signal sent to the process: the leader if it does not block...
Definition process.c:746
process_t * process_fork(const struct registers *regs)
fork the current process.
Definition process.c:201
void process_release_resources(process_t *proc)
release what a process owns apart from its kernel stack and table slot: close its fds,...
Definition process.c:292
uint32_t process_list_group(uint32_t pgid, uint32_t *pids, uint32_t max)
list the pids of a process group.
Definition process.c:518
void process_signal_threads(uint32_t tgid, process_t *except, int sig)
send a signal to the threads of a group (signal_send to each).
Definition process.c:721
void process_copy_credentials(process_t *child, const process_t *parent)
give a new process the user and group ids and umask of another.
Definition process.c:685
void process_destroy(process_t *proc)
destroy a process and free its slot in the process table.
Definition process.c:339
void process_reap_orphans(void)
destroy orphan zombies.
Definition process.c:445
process_t * process_get(uint32_t pid)
look up a process by PID.
Definition process.c:697
void process_set_name(process_t *proc, const char *path)
set a process's name from a program path (its last component).
Definition process.c:627
void process_account_tick(bool user)
charge one timer tick to the current process (timer interrupt).
Definition process.c:657
void process_for_each(void(*fn)(process_t *proc, void *ctx), void *ctx)
call fn for every process in the table, with the table locked and interrupts masked: fn must not slee...
Definition process.c:558
void process_init(void)
initialize the process subsystem.
Definition process.c:98
fs_context_t * fs_context_get(fs_context_t *fs)
add a user to an fs context (a new thread that shares it).
Definition process.c:321
process_t * process_create(void)
create a new process and return its PCB.
Definition process.c:137
process_t * process_current(void)
get the currently running process.
Definition process.c:265
uint32_t process_list_pids(uint32_t *pids, uint32_t max)
list the pids in use.
Definition process.c:571
process_t * process_find_waitable_child(process_t *parent, int32_t pid, uint32_t options, bool *has_child)
find a child of a process that has something for waitpid: it exited (a zombie), or,...
Definition process.c:479
struct process process_t
process control block.
struct fs_context fs_context_t
where paths start, and the mode bits new files lose.
process_state_t
process states.
Definition process.h:28
@ PROC_STATE_BLOCKED
Definition process.h:33
@ PROC_STATE_UNUSED
Definition process.h:29
@ PROC_STATE_ZOMBIE
Definition process.h:34
@ PROC_STATE_READY
Definition process.h:32
@ PROC_STATE_RUNNING
Definition process.h:31
@ PROC_STATE_NEW
Definition process.h:30
@ PROC_STATE_STOPPED
Definition process.h:35
wait_report_t
a change of a child that waitpid has not reported yet.
Definition process.h:42
@ WAIT_REPORT_NONE
Definition process.h:43
@ WAIT_REPORT_CONTINUED
Definition process.h:45
@ WAIT_REPORT_STOPPED
Definition process.h:44
#define PATH_MAX
Definition process.h:48
#define PROCESS_NAME_MAX
Definition process.h:51
where paths start, and the mode bits new files lose.
Definition process.h:58
char root[PATH_MAX]
Definition process.h:62
char cwd[PATH_MAX]
Definition process.h:61
volatile uint32_t users
Definition process.h:59
uint32_t umask
Definition process.h:60
a copy of what procfs shows about a process.
Definition process.h:328
uint32_t utime
Definition process.h:337
uint32_t uid
Definition process.h:336
uintptr_t brk
Definition process.h:335
uint32_t pages
Definition process.h:339
uint32_t areas
Definition process.h:333
uint32_t pid
Definition process.h:329
process_state_t state
Definition process.h:331
char name[PROCESS_NAME_MAX]
Definition process.h:332
uint32_t stime
Definition process.h:338
uint32_t open_fds
Definition process.h:334
uint32_t parent_pid
Definition process.h:330
process control block.
Definition process.h:69
user_space_t * space
Definition process.h:93
int32_t exit_status
Definition process.h:95
uintptr_t entry
Definition process.h:98
uint32_t sgid
Definition process.h:90
struct process * leader
Definition process.h:73
uint8_t fpu_state[ARCH_FPU_STATE_SIZE] __attribute__((aligned(16)))
uint32_t parent_pid
Definition process.h:84
uint32_t stime
Definition process.h:133
uint32_t uid
Definition process.h:89
uintptr_t tls
Definition process.h:81
wait_report_t wait_report
Definition process.h:131
struct process * exec_thread
Definition process.h:78
uint32_t cstime
Definition process.h:135
uintptr_t kernel_esp
Definition process.h:97
file_t * held_files[FD_MAX]
Definition process.h:146
fs_context_t * fs
Definition process.h:142
fd_table_t * files
Definition process.h:144
uintptr_t kernel_stack
Definition process.h:96
uint32_t utime
Definition process.h:132
uintptr_t clear_tid
Definition process.h:82
uintptr_t fault_addr
Definition process.h:136
uint32_t held_count
Definition process.h:147
volatile uint32_t threads
Definition process.h:75
volatile bool on_cpu
Definition process.h:102
uint32_t pgid
Definition process.h:87
uint32_t gid
Definition process.h:90
bool group_exiting
Definition process.h:76
int32_t group_status
Definition process.h:80
uint32_t suid
Definition process.h:89
signal_state_t signals
Definition process.h:123
bool fault_write
Definition process.h:137
char name[PROCESS_NAME_MAX]
Definition process.h:141
uint32_t cpu
Definition process.h:103
uint32_t egid
Definition process.h:90
bool reaped
Definition process.h:101
struct process * ready_next
Definition process.h:107
wait_queue_t * waiting_on
Definition process.h:115
volatile uint32_t child_events
Definition process.h:100
uint32_t syscall_nr
Definition process.h:126
uint64_t alarm_deadline
Definition process.h:127
uint8_t stop_signal
Definition process.h:129
char ** argv
Definition process.h:104
uint32_t sid
Definition process.h:88
wait_queue_t child_exit
Definition process.h:99
uint32_t euid
Definition process.h:89
uint32_t pid
Definition process.h:70
bool futex_woken
Definition process.h:121
struct process * ready_prev
Definition process.h:108
signal_handlers_t * handlers
Definition process.h:124
struct user_space * futex_space
Definition process.h:119
bool group_stopped
Definition process.h:77
uint32_t cutime
Definition process.h:134
struct process * wait_next
Definition process.h:114
bool interruptible
Definition process.h:116
uintptr_t futex_addr
Definition process.h:120
int32_t nice
Definition process.h:91
uint32_t tgid
Definition process.h:71
process_state_t state
Definition process.h:92
saved register state pushed onto the stack by ISR stubs.
Definition isr.h:41
the signal state of a process (inside process_t).
Definition signal.h:78
the user half of an address space: the page tables, the areas (vma.h) and the program break.
Definition user_space.h:16
a queue of blocked processes, linked through process_t::wait_next.
Definition wait_queue.h:35
struct user_space user_space_t
the user half of an address space: the page tables, the areas (vma.h) and the program break.