refactor: full rewrite of signal feature
[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/ds/waitq.h>
7 #include <lunaix/fs.h>
8 #include <lunaix/mm/mm.h>
9 #include <lunaix/mm/region.h>
10 #include <lunaix/signal.h>
11 #include <lunaix/timer.h>
12 #include <lunaix/types.h>
13 #include <stdint.h>
14
15 // 虽然内核不是进程,但为了区分,这里使用Pid=-1来指代内核。这主要是方便物理页所有权检查。
16 #define KERNEL_PID -1
17
18 /*
19     |C|Bk|De|Tn|Pu|Rn|
20          \----/
21             Dt
22
23     Group Dt: whether this process is terminated.
24
25     Rn: Running
26     Tn: Terminated
27     De: Destoryed
28     Pu: Paused
29     Bk: Blocked
30     C : Created
31 */
32
33 #define PS_READY 0
34 #define PS_RUNNING 1
35 #define PS_TERMNAT 2
36 #define PS_DESTROY 4
37 #define PS_PAUSED 8
38 #define PS_BLOCKED 16
39 #define PS_CREATED 32
40
41 #define PS_GrBP (PS_PAUSED | PS_BLOCKED)
42 #define PS_GrDT (PS_TERMNAT | PS_DESTROY)
43
44 #define PROC_TERMINATED(state) ((state)&PS_GrDT)
45 #define PROC_HANGED(state) ((state)&PS_BLOCKED)
46 #define PROC_RUNNABLE(state) ((state)&PS_PAUSED)
47
48 #define PROC_FINPAUSE 1
49
50 struct proc_sigstate
51 {
52     isr_param proc_regs;
53     char fxstate[512] __attribute__((aligned(16)));
54 };
55
56 struct sigact
57 {
58     struct sigact* prev;
59     sigset_t sa_mask;
60     void* sa_actor;
61     void* sa_handler;
62     pid_t sender;
63 };
64
65 struct sighail
66 {
67     sigset_t sig_pending;
68     sigset_t sig_mask;
69     struct sigact* inprogress;
70     struct sigact signals[_SIG_NUM];
71 };
72
73 struct proc_sig
74 {
75     int sig_num;
76     void* sigact;
77     void* sighand;
78     struct proc_sigstate prev_context;
79 } __attribute__((packed));
80
81 struct proc_info
82 {
83     /*
84         Any change to *critical section*, including layout, size
85         must be reflected in kernel/asm/x86/interrupt.S to avoid
86         disaster!
87      */
88
89     /* ---- critical section start ---- */
90
91     pid_t pid;                // offset = 0
92     struct proc_info* parent; // offset = 4
93     isr_param intr_ctx;       // offset = 8
94     ptr_t ustack_top;         // offset = 84 -> 56 -> 60
95     ptr_t page_table;         // offset = 88 -> 60 -> 64
96     void* fxstate;            // offset = 92 -> 64 -> 68
97
98     /* ---- critical section end ---- */
99
100     struct llist_header tasks;
101     struct llist_header siblings;
102     struct llist_header children;
103     struct llist_header grp_member;
104     waitq_t waitqueue;
105
106     struct
107     {
108         struct llist_header sleepers;
109         time_t wakeup_time;
110         time_t alarm_time;
111     } sleep;
112
113     struct proc_mm mm;
114     time_t created;
115     u8_t state;
116     int32_t exit_code;
117     int32_t k_status;
118     int flags;
119     struct sighail sigctx;
120     struct v_fdtable* fdtable;
121     struct v_dnode* cwd;
122     pid_t pgid;
123 };
124
125 extern volatile struct proc_info* __current;
126
127 static inline void
128 block_current()
129 {
130     __current->state = PS_BLOCKED;
131 }
132
133 static inline void
134 pause_current()
135 {
136     __current->state = PS_PAUSED;
137 }
138
139 static inline void
140 resume_current()
141 {
142     __current->state = PS_RUNNING;
143 }
144
145 /**
146  * @brief 分配并初始化一个进程控制块
147  *
148  * @return struct proc_info*
149  */
150 struct proc_info*
151 alloc_process();
152
153 /**
154  * @brief 初始化进程用户空间
155  *
156  * @param pcb
157  */
158 void
159 init_proc_user_space(struct proc_info* pcb);
160
161 /**
162  * @brief 向系统发布一个进程,使其可以被调度。
163  *
164  * @param process
165  */
166 void
167 commit_process(struct proc_info* process);
168
169 pid_t
170 destroy_process(pid_t pid);
171
172 void
173 setup_proc_mem(struct proc_info* proc, ptr_t kstack_from);
174
175 /**
176  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
177  *
178  */
179 pid_t
180 dup_proc();
181
182 /**
183  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
184  *
185  */
186 void
187 new_proc();
188
189 /**
190  * @brief 终止(退出)当前进程
191  *
192  */
193 void
194 terminate_proc(int exit_code);
195
196 int
197 orphaned_proc(pid_t pid);
198
199 struct proc_info*
200 get_process(pid_t pid);
201
202 void
203 proc_setsignal(struct proc_info* proc, int signum);
204
205 #endif /* __LUNAIX_PROCESS_H */