feat: (vm) memory mapping support: mmap/munmap
[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 llist_header 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 static inline void
97 block_current()
98 {
99     __current->state = PS_BLOCKED;
100 }
101
102 /**
103  * @brief 分配并初始化一个进程控制块
104  *
105  * @return struct proc_info*
106  */
107 struct proc_info*
108 alloc_process();
109
110 /**
111  * @brief 初始化进程用户空间
112  *
113  * @param pcb
114  */
115 void
116 init_proc_user_space(struct proc_info* pcb);
117
118 /**
119  * @brief 向系统发布一个进程,使其可以被调度。
120  *
121  * @param process
122  */
123 void
124 commit_process(struct proc_info* process);
125
126 pid_t
127 destroy_process(pid_t pid);
128
129 void
130 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
131
132 /**
133  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
134  *
135  */
136 pid_t
137 dup_proc();
138
139 /**
140  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
141  *
142  */
143 void
144 new_proc();
145
146 /**
147  * @brief 终止(退出)当前进程
148  *
149  */
150 void
151 terminate_proc(int exit_code);
152
153 int
154 orphaned_proc(pid_t pid);
155
156 struct proc_info*
157 get_process(pid_t pid);
158
159 #endif /* __LUNAIX_PROCESS_H */