feat: add signal handling support (not tested!)
[lunaix-os.git] / lunaix-os / includes / lunaix / process.h
1 #ifndef __LUNAIX_PROCESS_H
2 #define __LUNAIX_PROCESS_H
3
4 #include <arch/x86/interrupts.h>
5 #include <lunaix/clock.h>
6 #include <lunaix/mm/mm.h>
7 #include <lunaix/signal.h>
8 #include <lunaix/timer.h>
9 #include <lunaix/types.h>
10 #include <stdint.h>
11
12 // 虽然内核不是进程,但为了区分,这里使用Pid=-1来指代内核。这主要是方便物理页所有权检查。
13 #define KERNEL_PID -1
14
15 #define PROC_STOPPED 0
16 #define PROC_RUNNING 1
17 #define PROC_TERMNAT 2
18 #define PROC_DESTROY 4
19 #define PROC_BLOCKED 8
20 #define PROC_CREATED 16
21
22 #define PROC_TERMMASK 0x6
23
24 struct proc_mm
25 {
26     heap_context_t u_heap;
27     struct mm_region* regions;
28 };
29
30 struct proc_sig
31 {
32     void* signal_handler;
33     int sig_num;
34     isr_param prev_context;
35 };
36
37 #define PROC_SIG_SIZE sizeof(struct proc_sig) // size=84
38
39 struct proc_info
40 {
41     pid_t pid;
42     struct proc_info* parent;
43     isr_param intr_ctx; // size=76
44     uintptr_t ustack_top;
45     struct llist_header siblings;
46     struct llist_header children;
47     struct proc_mm mm;
48     void* page_table;
49     time_t created;
50     uint8_t state;
51     int32_t exit_code;
52     int32_t k_status;
53     sigset_t sig_pending;
54     sigset_t sig_mask;
55     void* sig_handler[_SIG_NUM];
56     struct lx_timer* timer;
57 };
58
59 extern volatile struct proc_info* __current;
60
61 pid_t
62 alloc_pid();
63
64 void
65 init_proc(struct proc_info* pcb);
66
67 /**
68  * @brief 向系统发布一个进程,使其可以被调度。
69  *
70  * @param process
71  */
72 void
73 push_process(struct proc_info* process);
74
75 pid_t
76 destroy_process(pid_t pid);
77
78 void
79 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
80
81 /**
82  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
83  *
84  */
85 pid_t
86 dup_proc();
87
88 /**
89  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
90  *
91  */
92 void
93 new_proc();
94
95 /**
96  * @brief 终止(退出)当前进程
97  *
98  */
99 void
100 terminate_proc(int exit_code);
101
102 int
103 orphaned_proc(pid_t pid);
104
105 struct proc_info*
106 get_process(pid_t pid);
107
108 #endif /* __LUNAIX_PROCESS_H */