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