feat: asynchronized SATA IO
[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_sigstate
35 {
36     isr_param proc_regs;
37     char fxstate[512] __attribute__((aligned(16)));
38 };
39
40 struct proc_sig
41 {
42     void* signal_handler;
43     int sig_num;
44     struct proc_sigstate prev_context;
45 } __attribute__((packed));
46
47 struct proc_info
48 {
49     /*
50         Any change to *critical section*, including layout, size
51         must be reflected in kernel/asm/x86/interrupt.S to avoid
52         disaster!
53      */
54
55     /* ---- critical section start ---- */
56
57     pid_t pid;                // offset = 0
58     struct proc_info* parent; // offset = 4
59     isr_param intr_ctx;       // offset = 8
60     uintptr_t ustack_top;     // offset = 84
61     void* page_table;         // offset = 88
62     void* fxstate;            // offset = 92
63
64     /* ---- critical section end ---- */
65
66     struct llist_header tasks;
67     struct llist_header siblings;
68     struct llist_header children;
69     struct llist_header grp_member;
70     waitq_t waitqueue;
71
72     struct
73     {
74         struct llist_header sleepers;
75         time_t wakeup_time;
76         time_t alarm_time;
77     } sleep;
78
79     struct proc_mm mm;
80     time_t created;
81     uint8_t state;
82     int32_t exit_code;
83     int32_t k_status;
84     sigset_t sig_pending;
85     sigset_t sig_mask;
86     sigset_t sig_inprogress;
87     int flags;
88     void* sig_handler[_SIG_NUM];
89     struct v_fdtable* fdtable;
90     struct v_dnode* cwd;
91     pid_t pgid;
92 };
93
94 extern volatile struct proc_info* __current;
95
96 /**
97  * @brief 分配并初始化一个进程控制块
98  *
99  * @return struct proc_info*
100  */
101 struct proc_info*
102 alloc_process();
103
104 /**
105  * @brief 初始化进程用户空间
106  *
107  * @param pcb
108  */
109 void
110 init_proc_user_space(struct proc_info* pcb);
111
112 /**
113  * @brief 向系统发布一个进程,使其可以被调度。
114  *
115  * @param process
116  */
117 void
118 commit_process(struct proc_info* process);
119
120 pid_t
121 destroy_process(pid_t pid);
122
123 void
124 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
125
126 /**
127  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
128  *
129  */
130 pid_t
131 dup_proc();
132
133 /**
134  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
135  *
136  */
137 void
138 new_proc();
139
140 /**
141  * @brief 终止(退出)当前进程
142  *
143  */
144 void
145 terminate_proc(int exit_code);
146
147 int
148 orphaned_proc(pid_t pid);
149
150 struct proc_info*
151 get_process(pid_t pid);
152
153 #endif /* __LUNAIX_PROCESS_H */