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