My Kernel v0.1.0
process.h
Go to the documentation of this file.
1#pragma once
2
34#include <kernel/file.h>
35#include <kernel/interrupts.h>
36#include <kernel/types.h>
37#include <kernel/vmm.h>
38
39#include <libalgo/linked_list.h>
40#include <utils/compiler.h>
41
42#include <string.h>
43
44#if ARCH == i686
46#else
47#error Unsupported arcihtecture
48#endif
49
51#define PROCESS_NAME_MAX_LEN 32U
52
54#define PROCESS_FD_COUNT 32
55
56struct address_space;
57
64typedef void (*thread_entry_t)(void *data);
65
69typedef enum thread_state {
73} thread_state_t;
74
84struct process {
86 pid_t pid;
88 struct address_space *as;
91 size_t refcount;
103 thread_state_t state;
104 int exit_status;
107};
108
121typedef struct thread {
122
130 thread_state_t state;
132 struct process *process;
137
138 pid_t tid;
139 u32 flags;
141 node_t this;
144 union {
146 struct {
148 clock_t preempt;
149 } running;
151 struct {
152 clock_t wakeup;
153 } sleep;
154 };
155
156} thread_t;
157
159typedef enum thread_flags {
161} process_flags_t;
162
163/***/
164static ALWAYS_INLINE bool thread_is_kernel(thread_t *thread)
165{
166 return thread->flags & THREAD_KERNEL;
167}
168
172static ALWAYS_INLINE bool thread_is_initial(thread_t *thread)
173{
174 return thread->tid == thread->process->pid;
175}
176
178static inline void thread_set_stack_pointer(struct thread *thread, void *stack)
179{
180 arch_thread_set_stack_pointer(&thread->context, stack);
181}
182
184static inline void *thread_get_stack_pointer(struct thread *thread)
185{
186 return arch_thread_get_stack_pointer(&thread->context);
187}
188
190static inline void thread_set_kernel_stack(struct thread *thread, void *stack)
191{
192 arch_thread_set_kernel_stack_top(&thread->context,
193 stack + KERNEL_STACK_SIZE);
194}
195
197static inline void *thread_get_kernel_stack_top(const struct thread *thread)
198{
199 return arch_thread_get_kernel_stack_top(&thread->context);
200}
201
203static inline void *thread_get_kernel_stack(const struct thread *thread)
204{
206 if (!top)
207 return NULL;
208
209 return top - KERNEL_STACK_SIZE;
210}
211
213static inline void thread_set_user_stack(struct thread *thread, void *stack)
214{
215 arch_thread_set_user_stack_top(&thread->context, stack + USER_STACK_SIZE);
216}
217
219static inline void *thread_get_user_stack_top(const struct thread *thread)
220{
221 return arch_thread_get_user_stack_top(&thread->context);
222}
223
225static inline void *thread_get_user_stack(const struct thread *thread)
226{
228 if (!top)
229 return NULL;
230
231 return top - USER_STACK_SIZE;
232}
233
242extern struct process kernel_process;
243extern struct thread kernel_process_initial_thread;
244
252extern struct process *init_process;
253
261
265int process_register_file(struct process *, struct file *);
266
268error_t process_unregister_file(struct process *, int fd);
269
274struct file *process_file_get(struct process *, int fd);
275
277static inline void process_file_put(struct process *process, struct file *file)
278{
280 file_put(file);
281}
282
291struct thread *process_execute_in_userland(const char *exec_path);
292
293/***/
294static inline void process_set_name(struct process *process, const char *name,
295 size_t size)
296{
297 strlcpy(process->name, name, MIN(size + 1, PROCESS_NAME_MAX_LEN));
298}
299
301extern thread_t *current;
302
307bool thread_switch(thread_t *);
308
322thread_t *thread_spawn(struct process *, thread_entry_t, void *, void *, u32);
323
337NO_RETURN void thread_jump_to_userland(thread_entry_t, void *);
338
343void thread_set_mmu(struct thread *thread, paddr_t mmu);
344
350void thread_kill(thread_t *);
351
363struct thread *thread_fork(struct thread *, thread_entry_t, void *);
364
static void file_put(struct file *file)
Decrement an open file description's reference count.
Definition: file.h:55
#define USER_STACK_SIZE
Definition: memory.h:59
#define KERNEL_STACK_SIZE
Definition: memory.h:55
struct file * process_file_get(struct process *, int fd)
Definition: process.c:322
static ALWAYS_INLINE bool thread_is_initial(thread_t *thread)
The initial thread is the thread created along with the process.
Definition: process.h:172
static void thread_set_user_stack(struct thread *thread, void *stack)
Set the thread's user stack bottom address.
Definition: process.h:213
struct thread * thread_fork(struct thread *, thread_entry_t, void *)
Create a new fork of the given thread.
Definition: process.c:533
thread_t * thread_spawn(struct process *, thread_entry_t, void *, void *, u32)
Create and initialize a new thread.
Definition: process.c:339
void thread_set_mmu(struct thread *thread, paddr_t mmu)
Set the MMU address saved inside the thread's structure.
Definition: process.c:619
static void * thread_get_user_stack(const struct thread *thread)
Get the thread's user stack bottom address.
Definition: process.h:225
struct thread * process_execute_in_userland(const char *exec_path)
Run an executable.
Definition: process.c:515
static void * thread_get_stack_pointer(struct thread *thread)
Get the thread's current stack pointer.
Definition: process.h:184
thread_state
The different states a thread can be in.
Definition: process.h:69
struct process * init_process
The init process is the very first executed userland process.
Definition: process.c:43
#define PROCESS_NAME_MAX_LEN
The max length of a process's name.
Definition: process.h:51
static void * thread_get_kernel_stack(const struct thread *thread)
Get the thread's kernel stack bottom address.
Definition: process.h:203
error_t process_unregister_file(struct process *, int fd)
Remove an open file from the process's open file descriptor table.
Definition: process.c:304
static void thread_set_kernel_stack(struct thread *thread, void *stack)
Set the thread's kernel stack bottom address.
Definition: process.h:190
void(* thread_entry_t)(void *data)
A function used as an entry point when creating a new thread.
Definition: process.h:64
static void * thread_get_kernel_stack_top(const struct thread *thread)
Get the thread's kernel stack top address.
Definition: process.h:197
#define PROCESS_FD_COUNT
Maximum number of files that one process can have open at any one time.
Definition: process.h:54
static void thread_set_stack_pointer(struct thread *thread, void *stack)
Set the thread's current stack pointer.
Definition: process.h:178
bool thread_switch(thread_t *)
Switch the currently running thread.
Definition: process.c:439
struct process kernel_process
Process used when starting up the kernel.
Definition: process.c:35
NO_RETURN void thread_jump_to_userland(thread_entry_t, void *)
Start executing code in userland.
Definition: process.c:614
void thread_kill(thread_t *)
Effectively kill a thread.
Definition: process.c:484
thread_t * current
The currently running thread.
Definition: process.c:41
static void process_file_put(struct process *process, struct file *file)
Release a file description retreived using process_file_get().
Definition: process.h:277
thread_flags
Definition: process.h:159
static void * thread_get_user_stack_top(const struct thread *thread)
Get the thread's user stack top address.
Definition: process.h:219
void process_init_kernel_process(void)
Initialize the kernel process's address space.
Definition: process.c:231
int process_register_file(struct process *, struct file *)
Register an open file inside the process's open file descriptor table.
Definition: process.c:284
@ SCHED_RUNNING
Currently running (or ready to run)
Definition: process.h:70
@ SCHED_KILLED
The thread has been killed waiting to be destroyed.
Definition: process.h:72
@ SCHED_WAITING
Currently waiting for a resource (timer, lock ...)
Definition: process.h:71
@ THREAD_KERNEL
This thread runs in kernel mode.
Definition: process.h:160
#define BIT(_n)
Generate the nth power of 2 (nth bit set)
Definition: bits.h:20
#define MIN(_x, _y)
Compute the minimum value between 2 numbers.
Definition: math.h:27
#define UNUSED(_x)
Avoid compiler warning when not using a symbol.
Definition: macro.h:35
Address space.
Definition: vm.h:45
Opened file description.
Definition: file.h:29
Frame passed onto the interrupt handlers by our stub handler.
Definition: interrupts.h:121
The head of a doubly linked list.
Definition: linked_list.h:43
Intrusive doubly-linked list node.
Definition: linked_list.h:27
A single process.
Definition: process.h:84
llist_t threads
Definition: process.h:90
spinlock_t files_lock
Definition: process.h:101
char name[PROCESS_NAME_MAX_LEN]
Definition: process.h:85
struct address_space * as
Definition: process.h:88
size_t refcount
Definition: process.h:91
spinlock_t lock
Transmitted to the parent process during wait()
Definition: process.h:106
pid_t pid
Definition: process.h:86
struct file * files[PROCESS_FD_COUNT]
Open file descriptors table.
Definition: process.h:100
Spinlock.
Definition: spinlock.h:29
A single thread.
Definition: process.h:121
struct interrupt_frame frame
Frame pushed during the last userland -> kernel context switch.
Definition: process.h:136
node_t proc_this
Definition: process.h:133
thread_state_t state
Definition: process.h:130
struct process * process
Definition: process.h:132
pid_t tid
Definition: process.h:138
clock_t wakeup
Definition: process.h:152
clock_t preempt
End of the currently running thread's timeslice.
Definition: process.h:148
u32 flags
Definition: process.h:139
thread_context_t context
Arch specific thread context.
Definition: process.h:129
Contains all the system-level information about a task.
Definition: process.h:20