feat: input device subsystem to resolve race condition on polling input
[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 mm_region regions;
32 };
33
34 struct proc_sig
35 {
36     void* signal_handler;
37     int sig_num;
38     isr_param prev_context;
39 };
40
41 #define PROC_SIG_SIZE sizeof(struct proc_sig) // size=84
42
43 struct proc_info
44 {
45     /*
46         Any change to *critical section*, including layout, size
47         must be reflected in kernel/asm/x86/interrupt.S to avoid
48         disaster!
49      */
50
51     /* ---- critical section start ---- */
52
53     pid_t pid;
54     struct proc_info* parent;
55     isr_param intr_ctx; // size=76
56     uintptr_t ustack_top;
57     void* page_table;
58
59     /* ---- critical section end ---- */
60
61     struct llist_header siblings;
62     struct llist_header children;
63     struct llist_header grp_member;
64     waitq_t waitqueue;
65
66     struct
67     {
68         struct llist_header sleepers;
69         time_t wakeup_time;
70         time_t alarm_time;
71     } sleep;
72
73     struct proc_mm mm;
74     time_t created;
75     uint8_t state;
76     int32_t exit_code;
77     int32_t k_status;
78     sigset_t sig_pending;
79     sigset_t sig_mask;
80     sigset_t sig_inprogress;
81     int flags;
82     void* sig_handler[_SIG_NUM];
83     struct v_fdtable* fdtable;
84     struct v_dnode* cwd;
85     pid_t pgid;
86 };
87
88 extern volatile struct proc_info* __current;
89
90 /**
91  * @brief 分配并初始化一个进程控制块
92  *
93  * @return struct proc_info*
94  */
95 struct proc_info*
96 alloc_process();
97
98 /**
99  * @brief 初始化进程用户空间
100  *
101  * @param pcb
102  */
103 void
104 init_proc_user_space(struct proc_info* pcb);
105
106 /**
107  * @brief 向系统发布一个进程,使其可以被调度。
108  *
109  * @param process
110  */
111 void
112 commit_process(struct proc_info* process);
113
114 pid_t
115 destroy_process(pid_t pid);
116
117 void
118 setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
119
120 /**
121  * @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
122  *
123  */
124 pid_t
125 dup_proc();
126
127 /**
128  * @brief 创建新进程(LunaixOS的类 CreateProcess (Windows) 实现)
129  *
130  */
131 void
132 new_proc();
133
134 /**
135  * @brief 终止(退出)当前进程
136  *
137  */
138 void
139 terminate_proc(int exit_code);
140
141 int
142 orphaned_proc(pid_t pid);
143
144 struct proc_info*
145 get_process(pid_t pid);
146
147 #endif /* __LUNAIX_PROCESS_H */