feat: heap support and re-worked
[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_sigstate
30 {
31     isr_param proc_regs;
32     char fxstate[512] __attribute__((aligned(16)));
33 };
34
35 struct proc_sig
36 {
37     void* signal_handler;
38     int sig_num;
39     struct proc_sigstate prev_context;
40 } __attribute__((packed));
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;                // offset = 0
53     struct proc_info* parent; // offset = 4
54     isr_param intr_ctx;       // offset = 8
55     uintptr_t ustack_top;     // offset = 84
56     void* page_table;         // offset = 88
57     void* fxstate;            // offset = 92
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 static inline void
92 block_current()
93 {
94     __current->state = PS_BLOCKED;
95 }
96
97 /**
98  * @brief 分配并初始化一个进程控制块
99  *
100  * @return struct proc_info*
101  */
102 struct proc_info*
103 alloc_process();
104
105 /**
106  * @brief 初始化进程用户空间
107  *
108  * @param pcb
109  */
110 void
111 init_proc_user_space(struct proc_info* pcb);
112
113 /**
114  * @brief 向系统发布一个进程,使其可以被调度。
115  *
116  * @param process
117  */
118 void
119 commit_process(struct proc_info* process);
120
121 pid_t
122 destroy_process(pid_t pid);
123
124 void
125 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
126
127 /**
128  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
129  *
130  */
131 pid_t
132 dup_proc();
133
134 /**
135  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
136  *
137  */
138 void
139 new_proc();
140
141 /**
142  * @brief 终止(退出)当前进程
143  *
144  */
145 void
146 terminate_proc(int exit_code);
147
148 int
149 orphaned_proc(pid_t pid);
150
151 struct proc_info*
152 get_process(pid_t pid);
153
154 #endif /* __LUNAIX_PROCESS_H */