feat: input device subsystem to resolve race condition on polling input
[lunaix-os.git] / lunaix-os / kernel / sched.c
1 #include <arch/x86/interrupts.h>
2 #include <arch/x86/tss.h>
3
4 #include <hal/apic.h>
5 #include <hal/cpu.h>
6
7 #include <lunaix/mm/kalloc.h>
8 #include <lunaix/mm/pmm.h>
9 #include <lunaix/mm/valloc.h>
10 #include <lunaix/mm/vmm.h>
11 #include <lunaix/process.h>
12 #include <lunaix/sched.h>
13 #include <lunaix/signal.h>
14 #include <lunaix/spike.h>
15 #include <lunaix/status.h>
16 #include <lunaix/syscall.h>
17 #include <lunaix/syslog.h>
18
19 #define MAX_PROCESS 512
20
21 volatile struct proc_info* __current;
22
23 struct proc_info dummy;
24
25 struct scheduler sched_ctx;
26
27 LOG_MODULE("SCHED")
28
29 void
30 sched_init()
31 {
32     size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
33
34     for (size_t i = 0; i <= pg_size; i += 4096) {
35         uintptr_t pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
36         vmm_set_mapping(
37           PD_REFERENCED, PROC_START + i, pa, PG_PREM_RW, VMAP_NULL);
38     }
39
40     sched_ctx = (struct scheduler){ ._procs = (struct proc_info*)PROC_START,
41                                     .ptable_len = 0,
42                                     .procs_index = 0 };
43 }
44
45 void
46 run(struct proc_info* proc)
47 {
48     proc->state = PS_RUNNING;
49
50     /*
51         将tss.esp0设置为上次调度前的esp值。
52         当处理信号时,上下文信息是不会恢复的,而是保存在用户栈中,然后直接跳转进位于用户空间的sig_wrapper进行
53           信号的处理。当用户自定义的信号处理函数返回时,sigreturn的系统调用才开始进行上下文的恢复(或者说是进行
54           另一次调度。
55         由于这中间没有进行地址空间的交换,所以第二次跳转使用的是同一个内核栈,而之前默认tss.esp0的值是永远指向最顶部
56         这样一来就有可能会覆盖更早的上下文信息(比如嵌套的信号捕获函数)
57     */
58     tss_update_esp(proc->intr_ctx.registers.esp);
59
60     apic_done_servicing();
61
62     asm volatile("pushl %0\n"
63                  "jmp switch_to\n" ::"r"(proc)
64                  : "memory"); // kernel/asm/x86/interrupt.S
65 }
66
67 int
68 can_schedule(struct proc_info* proc)
69 {
70     if (__SIGTEST(proc->sig_pending, _SIGCONT)) {
71         __SIGCLEAR(proc->sig_pending, _SIGSTOP);
72     } else if (__SIGTEST(proc->sig_pending, _SIGSTOP)) {
73         // 如果进程受到SIGSTOP,则该进程不给予调度。
74         return 0;
75     }
76
77     return 1;
78 }
79
80 void
81 check_sleepers()
82 {
83     struct proc_info* leader = &sched_ctx._procs[0];
84     struct proc_info *pos, *n;
85     time_t now = clock_systime();
86     llist_for_each(pos, n, &leader->sleep.sleepers, sleep.sleepers)
87     {
88         if (PROC_TERMINATED(pos->state)) {
89             goto del;
90         }
91
92         time_t wtime = pos->sleep.wakeup_time;
93         time_t atime = pos->sleep.alarm_time;
94
95         if (wtime && now >= wtime) {
96             pos->sleep.wakeup_time = 0;
97             pos->state = PS_READY;
98         }
99
100         if (atime && now >= atime) {
101             pos->sleep.alarm_time = 0;
102             __SIGSET(pos->sig_pending, _SIGALRM);
103         }
104
105         if (!wtime && !atime) {
106         del:
107             llist_delete(&pos->sleep.sleepers);
108         }
109     }
110 }
111
112 void
113 schedule()
114 {
115     if (!sched_ctx.ptable_len) {
116         return;
117     }
118
119     // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
120     cpu_disable_interrupt();
121     struct proc_info* next;
122     int prev_ptr = sched_ctx.procs_index;
123     int ptr = prev_ptr;
124
125     if (!(__current->state & ~PS_RUNNING)) {
126         __current->state = PS_READY;
127     }
128
129     check_sleepers();
130
131     // round-robin scheduler
132 redo:
133     do {
134         ptr = (ptr + 1) % sched_ctx.ptable_len;
135         next = &sched_ctx._procs[ptr];
136     } while (next->state != PS_READY && ptr != prev_ptr);
137
138     sched_ctx.procs_index = ptr;
139
140     if (!can_schedule(next)) {
141         // 如果该进程不给予调度,则尝试重新选择
142         goto redo;
143     }
144
145     run(next);
146 }
147
148 void
149 sched_yieldk()
150 {
151     cpu_enable_interrupt();
152     cpu_int(LUNAIX_SCHED);
153 }
154
155 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
156 {
157     if (!seconds) {
158         return 0;
159     }
160
161     if (__current->sleep.wakeup_time) {
162         return (__current->sleep.wakeup_time - clock_systime()) / 1000U;
163     }
164
165     __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
166     llist_append(&sched_ctx._procs[0].sleep.sleepers,
167                  &__current->sleep.sleepers);
168
169     __current->intr_ctx.registers.eax = seconds;
170     __current->state = PS_BLOCKED;
171     schedule();
172 }
173
174 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
175 {
176     time_t prev_ddl = __current->sleep.alarm_time;
177     time_t now = clock_systime();
178
179     __current->sleep.alarm_time = seconds ? now + seconds * 1000 : 0;
180
181     if (llist_empty(&__current->sleep.sleepers)) {
182         llist_append(&sched_ctx._procs[0].sleep.sleepers,
183                      &__current->sleep.sleepers);
184     }
185
186     return prev_ddl ? (prev_ddl - now) / 1000 : 0;
187 }
188
189 __DEFINE_LXSYSCALL1(void, exit, int, status)
190 {
191     terminate_proc(status);
192     schedule();
193 }
194
195 __DEFINE_LXSYSCALL(void, yield)
196 {
197     schedule();
198 }
199
200 pid_t
201 _wait(pid_t wpid, int* status, int options);
202
203 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
204 {
205     return _wait(-1, status, 0);
206 }
207
208 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
209 {
210     return _wait(pid, status, options);
211 }
212
213 __DEFINE_LXSYSCALL(int, geterrno)
214 {
215     return __current->k_status;
216 }
217
218 pid_t
219 _wait(pid_t wpid, int* status, int options)
220 {
221     pid_t cur = __current->pid;
222     int status_flags = 0;
223     struct proc_info *proc, *n;
224     if (llist_empty(&__current->children)) {
225         return -1;
226     }
227
228     wpid = wpid ? wpid : -__current->pgid;
229 repeat:
230     llist_for_each(proc, n, &__current->children, siblings)
231     {
232         if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
233             if (proc->state == PS_TERMNAT && !options) {
234                 status_flags |= PEXITTERM;
235                 goto done;
236             }
237             if (proc->state == PS_READY && (options & WUNTRACED)) {
238                 status_flags |= PEXITSTOP;
239                 goto done;
240             }
241         }
242     }
243     if ((options & WNOHANG)) {
244         return 0;
245     }
246     // 放弃当前的运行机会
247     sched_yieldk();
248     goto repeat;
249
250 done:
251     status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
252     if (status) {
253         *status = proc->exit_code | status_flags;
254     }
255     return destroy_process(proc->pid);
256 }
257
258 struct proc_info*
259 alloc_process()
260 {
261     pid_t i = 0;
262     for (; i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PS_DESTROY;
263          i++)
264         ;
265
266     if (i == MAX_PROCESS) {
267         panick("Panic in Ponyville shimmer!");
268     }
269
270     if (i == sched_ctx.ptable_len) {
271         sched_ctx.ptable_len++;
272     }
273
274     struct proc_info* proc = &sched_ctx._procs[i];
275     memset(proc, 0, sizeof(*proc));
276
277     proc->state = PS_CREATED;
278     proc->pid = i;
279     proc->created = clock_systime();
280     proc->pgid = proc->pid;
281     proc->fdtable = vzalloc(sizeof(struct v_fdtable));
282
283     llist_init_head(&proc->mm.regions.head);
284     llist_init_head(&proc->children);
285     llist_init_head(&proc->grp_member);
286     llist_init_head(&proc->sleep.sleepers);
287     waitq_init(&proc->waitqueue);
288
289     return proc;
290 }
291
292 void
293 commit_process(struct proc_info* process)
294 {
295     assert(process == &sched_ctx._procs[process->pid]);
296
297     if (process->state != PS_CREATED) {
298         __current->k_status = EINVAL;
299         return;
300     }
301
302     // every process is the child of first process (pid=1)
303     if (!process->parent) {
304         process->parent = &sched_ctx._procs[1];
305     }
306
307     llist_append(&process->parent->children, &process->siblings);
308
309     process->state = PS_READY;
310 }
311
312 // from <kernel/process.c>
313 extern void
314 __del_pagetable(pid_t pid, uintptr_t mount_point);
315
316 pid_t
317 destroy_process(pid_t pid)
318 {
319     int index = pid;
320     if (index <= 0 || index > sched_ctx.ptable_len) {
321         __current->k_status = EINVAL;
322         return;
323     }
324     struct proc_info* proc = &sched_ctx._procs[index];
325     proc->state = PS_DESTROY;
326     llist_delete(&proc->siblings);
327
328     for (size_t i = 0; i < VFS_MAX_FD; i++) {
329         struct v_fd* fd = proc->fdtable->fds[i];
330         if (fd)
331             vfs_close(fd->file);
332     }
333
334     vfree(proc->fdtable);
335
336     struct mm_region *pos, *n;
337     llist_for_each(pos, n, &proc->mm.regions.head, head)
338     {
339         vfree(pos);
340     }
341
342     vmm_mount_pd(PD_MOUNT_1, proc->page_table);
343
344     __del_pagetable(pid, PD_MOUNT_1);
345
346     vmm_unmount_pd(PD_MOUNT_1);
347
348     return pid;
349 }
350
351 void
352 terminate_proc(int exit_code)
353 {
354     __current->state = PS_TERMNAT;
355     __current->exit_code = exit_code;
356
357     __SIGSET(__current->parent->sig_pending, _SIGCHLD);
358 }
359
360 struct proc_info*
361 get_process(pid_t pid)
362 {
363     int index = pid;
364     if (index < 0 || index > sched_ctx.ptable_len) {
365         return NULL;
366     }
367     return &sched_ctx._procs[index];
368 }
369
370 int
371 orphaned_proc(pid_t pid)
372 {
373     if (!pid)
374         return 0;
375     if (pid >= sched_ctx.ptable_len)
376         return 0;
377     struct proc_info* proc = &sched_ctx._procs[pid];
378     struct proc_info* parent = proc->parent;
379
380     // 如果其父进程的状态是terminated 或 destroy中的一种
381     // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
382     return PROC_TERMINATED(parent->state) || parent->created > proc->created;
383 }