feat: signal support (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     /*
42         Any change to *critical section*, including layout, size
43         must be reflected in kernel/asm/x86/interrupt.S to avoid
44         disaster!
45      */
46
47     /* ---- critical section start ---- */
48
49     pid_t pid;
50     struct proc_info* parent;
51     isr_param intr_ctx; // size=76
52     uintptr_t ustack_top;
53     void* page_table;
54
55     /* ---- critical section end ---- */
56
57     struct llist_header siblings;
58     struct llist_header children;
59     struct llist_header grp_member;
60     struct proc_mm mm;
61     time_t created;
62     uint8_t state;
63     int32_t exit_code;
64     int32_t k_status;
65     sigset_t sig_pending;
66     sigset_t sig_mask;
67     void* sig_handler[_SIG_NUM];
68     pid_t pgid;
69     struct lx_timer* timer;
70 };
71
72 extern volatile struct proc_info* __current;
73
74 /**
75  * @brief 分配并初始化一个进程控制块
76  *
77  * @return struct proc_info*
78  */
79 struct proc_info*
80 alloc_process();
81
82 /**
83  * @brief 初始化进程用户空间
84  *
85  * @param pcb
86  */
87 void
88 init_proc_user_space(struct proc_info* pcb);
89
90 /**
91  * @brief 向系统发布一个进程,使其可以被调度。
92  *
93  * @param process
94  */
95 void
96 commit_process(struct proc_info* process);
97
98 pid_t
99 destroy_process(pid_t pid);
100
101 void
102 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
103
104 /**
105  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
106  *
107  */
108 pid_t
109 dup_proc();
110
111 /**
112  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
113  *
114  */
115 void
116 new_proc();
117
118 /**
119  * @brief 终止(退出)当前进程
120  *
121  */
122 void
123 terminate_proc(int exit_code);
124
125 int
126 orphaned_proc(pid_t pid);
127
128 struct proc_info*
129 get_process(pid_t pid);
130
131 #endif /* __LUNAIX_PROCESS_H */