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