Merge branch 'master' into signal-dev
[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     pid_t pid;
42     struct proc_info* parent;
43     isr_param intr_ctx; // size=76
44     uintptr_t ustack_top;
45     struct llist_header siblings;
46     struct llist_header children;
47     struct llist_header grp_member;
48     struct proc_mm mm;
49     void* page_table;
50     time_t created;
51     uint8_t state;
52     int32_t exit_code;
53     int32_t k_status;
54     sigset_t sig_pending;
55     sigset_t sig_mask;
56     void* sig_handler[_SIG_NUM];
57     pid_t pgid;
58     struct lx_timer* timer;
59 };
60
61 extern volatile struct proc_info* __current;
62
63 pid_t
64 alloc_pid();
65
66 void
67 init_proc(struct proc_info* pcb);
68
69 /**
70  * @brief 向系统发布一个进程,使其可以被调度。
71  *
72  * @param process
73  */
74 void
75 push_process(struct proc_info* process);
76
77 pid_t
78 destroy_process(pid_t pid);
79
80 void
81 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
82
83 /**
84  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
85  *
86  */
87 pid_t
88 dup_proc();
89
90 /**
91  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
92  *
93  */
94 void
95 new_proc();
96
97 /**
98  * @brief 终止(退出)当前进程
99  *
100  */
101 void
102 terminate_proc(int exit_code);
103
104 int
105 orphaned_proc(pid_t pid);
106
107 struct proc_info*
108 get_process(pid_t pid);
109
110 #endif /* __LUNAIX_PROCESS_H */