chore: make things more general
[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/mm/mm.h>
7 #include <lunaix/signal.h>
8 #include <lunaix/timer.h>
9 #include <lunaix/types.h>
10 #include <stdint.h>
11
12 // 虽然内核不是进程,但为了区分,这里使用Pid=-1来指代内核。这主要是方便物理页所有权检查。
13 #define KERNEL_PID -1
14
15 #define PROC_STOPPED 0
16 #define PROC_RUNNING 1
17 #define PROC_TERMNAT 2
18 #define PROC_DESTROY 4
19 #define PROC_BLOCKED 8
20 #define PROC_CREATED 16
21
22 #define PROC_TERMMASK 0x6
23
24 struct proc_mm
25 {
26     heap_context_t u_heap;
27     struct mm_region* regions;
28 };
29
30 struct proc_sig
31 {
32     void* signal_handler;
33     int sig_num;
34     isr_param prev_context;
35 };
36
37 #define PROC_SIG_SIZE sizeof(struct proc_sig) // size=84
38
39 struct proc_info
40 {
41     /*
42         Any change to *critical section*, including layout, size
43         must be reflected in kernel/asm/x86/interrupt.S to avoid
44         disaster!
45      */
46
47     /* ---- critical section start ---- */
48
49     pid_t pid;
50     struct proc_info* parent;
51     isr_param intr_ctx; // size=76
52     uintptr_t ustack_top;
53     void* page_table;
54
55     /* ---- critical section end ---- */
56
57     struct llist_header siblings;
58     struct llist_header children;
59     struct llist_header grp_member;
60     struct proc_mm mm;
61     time_t created;
62     uint8_t state;
63     int32_t exit_code;
64     int32_t k_status;
65     sigset_t sig_pending;
66     sigset_t sig_mask;
67     void* sig_handler[_SIG_NUM];
68     pid_t pgid;
69     struct lx_timer* timer;
70 };
71
72 extern volatile struct proc_info* __current;
73
74 pid_t
75 alloc_pid();
76
77 void
78 init_proc(struct proc_info* pcb);
79
80 /**
81  * @brief 向系统发布一个进程,使其可以被调度。
82  *
83  * @param process
84  */
85 void
86 push_process(struct proc_info* process);
87
88 pid_t
89 destroy_process(pid_t pid);
90
91 void
92 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
93
94 /**
95  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
96  *
97  */
98 pid_t
99 dup_proc();
100
101 /**
102  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
103  *
104  */
105 void
106 new_proc();
107
108 /**
109  * @brief 终止(退出)当前进程
110  *
111  */
112 void
113 terminate_proc(int exit_code);
114
115 int
116 orphaned_proc(pid_t pid);
117
118 struct proc_info*
119 get_process(pid_t pid);
120
121 #endif /* __LUNAIX_PROCESS_H */