feat: simple read/write lock 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/ds/waitq.h>
7 #include <lunaix/fs.h>
8 #include <lunaix/mm/mm.h>
9 #include <lunaix/signal.h>
10 #include <lunaix/timer.h>
11 #include <lunaix/types.h>
12 #include <stdint.h>
13
14 // 虽然内核不是进程,但为了区分,这里使用Pid=-1来指代内核。这主要是方便物理页所有权检查。
15 #define KERNEL_PID -1
16
17 #define PS_READY 0
18 #define PS_RUNNING 1
19 #define PS_TERMNAT 2
20 #define PS_DESTROY 4
21 #define PS_BLOCKED 8
22 #define PS_CREATED 16
23
24 #define PROC_TERMINATED(state) (state & 0x6)
25
26 #define PROC_FINPAUSE 1
27
28 struct proc_mm
29 {
30     heap_context_t u_heap;
31     struct mm_region regions;
32 };
33
34 struct proc_sig
35 {
36     void* signal_handler;
37     int sig_num;
38     isr_param prev_context;
39 };
40
41 #define PROC_SIG_SIZE sizeof(struct proc_sig) // size=84
42
43 struct proc_info
44 {
45     /*
46         Any change to *critical section*, including layout, size
47         must be reflected in kernel/asm/x86/interrupt.S to avoid
48         disaster!
49      */
50
51     /* ---- critical section start ---- */
52
53     pid_t pid;
54     struct proc_info* parent;
55     isr_param intr_ctx; // size=76
56     uintptr_t ustack_top;
57     void* page_table;
58
59     /* ---- critical section end ---- */
60
61     struct llist_header tasks;
62     struct llist_header siblings;
63     struct llist_header children;
64     struct llist_header grp_member;
65     waitq_t waitqueue;
66
67     struct
68     {
69         struct llist_header sleepers;
70         time_t wakeup_time;
71         time_t alarm_time;
72     } sleep;
73
74     struct proc_mm mm;
75     time_t created;
76     uint8_t state;
77     int32_t exit_code;
78     int32_t k_status;
79     sigset_t sig_pending;
80     sigset_t sig_mask;
81     sigset_t sig_inprogress;
82     int flags;
83     void* sig_handler[_SIG_NUM];
84     struct v_fdtable* fdtable;
85     struct v_dnode* cwd;
86     pid_t pgid;
87 };
88
89 extern volatile struct proc_info* __current;
90
91 /**
92  * @brief 分配并初始化一个进程控制块
93  *
94  * @return struct proc_info*
95  */
96 struct proc_info*
97 alloc_process();
98
99 /**
100  * @brief 初始化进程用户空间
101  *
102  * @param pcb
103  */
104 void
105 init_proc_user_space(struct proc_info* pcb);
106
107 /**
108  * @brief 向系统发布一个进程,使其可以被调度。
109  *
110  * @param process
111  */
112 void
113 commit_process(struct proc_info* process);
114
115 pid_t
116 destroy_process(pid_t pid);
117
118 void
119 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
120
121 /**
122  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
123  *
124  */
125 pid_t
126 dup_proc();
127
128 /**
129  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
130  *
131  */
132 void
133 new_proc();
134
135 /**
136  * @brief 终止(退出)当前进程
137  *
138  */
139 void
140 terminate_proc(int exit_code);
141
142 int
143 orphaned_proc(pid_t pid);
144
145 struct proc_info*
146 get_process(pid_t pid);
147
148 #endif /* __LUNAIX_PROCESS_H */