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