refactor: full rewrite of signal feature
[lunaix-os.git] / lunaix-os / kernel / process / sched.c
index de1a4a59f6477184838eadc0e2f777a23d16c546..5e5db25e8ca20f22e6dac2d4c28fa4bec7fe614f 100644 (file)
@@ -6,7 +6,6 @@
 
 #include <lunaix/fs/taskfs.h>
 #include <lunaix/mm/cake.h>
-#include <lunaix/mm/kalloc.h>
 #include <lunaix/mm/mmap.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/valloc.h>
@@ -19,6 +18,8 @@
 #include <lunaix/syscall.h>
 #include <lunaix/syslog.h>
 
+#include <klibc/string.h>
+
 volatile struct proc_info* __current;
 
 static struct proc_info dummy_proc;
@@ -59,23 +60,23 @@ sched_init_dummy()
     extern void my_dummy();
     static char dummy_stack[DUMMY_STACK_SIZE] __attribute__((aligned(16)));
 
-    // memset to 0
-    dummy_proc = (struct proc_info){};
-    dummy_proc.intr_ctx = (isr_param){
-        .registers = { .ds = KDATA_SEG,
-                       .es = KDATA_SEG,
-                       .fs = KDATA_SEG,
-                       .gs = KDATA_SEG,
-                       .esp = (void*)dummy_stack + DUMMY_STACK_SIZE - 20 },
+    struct exec_param* execp =
+      (void*)dummy_stack + DUMMY_STACK_SIZE - sizeof(struct exec_param);
+
+    *execp = (struct exec_param){
         .cs = KCODE_SEG,
-        .eip = (void*)my_dummy,
+        .eflags = cpu_reflags() | 0x0200,
+        .eip = (ptr_t)my_dummy,
         .ss = KDATA_SEG,
-        .eflags = cpu_reflags() | 0x0200
     };
 
-    *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 4]) = dummy_proc.intr_ctx.eflags;
-    *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 8]) = KCODE_SEG;
-    *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 12]) = dummy_proc.intr_ctx.eip;
+    // memset to 0
+    dummy_proc = (struct proc_info){};
+    dummy_proc.intr_ctx = (isr_param){ .registers = { .ds = KDATA_SEG,
+                                                      .es = KDATA_SEG,
+                                                      .fs = KDATA_SEG,
+                                                      .gs = KDATA_SEG },
+                                       .execp = execp };
 
     dummy_proc.page_table = cpu_rcr3();
     dummy_proc.state = PS_READY;
@@ -98,7 +99,7 @@ run(struct proc_info* proc)
         由于这中间没有进行地址空间的交换,所以第二次跳转使用的是同一个内核栈,而之前默认tss.esp0的值是永远指向最顶部
         这样一来就有可能会覆盖更早的上下文信息(比如嵌套的信号捕获函数)
     */
-    tss_update_esp(proc->intr_ctx.registers.esp);
+    tss_update_esp(proc->intr_ctx.esp);
 
     apic_done_servicing();
 
@@ -110,14 +111,24 @@ run(struct proc_info* proc)
 int
 can_schedule(struct proc_info* proc)
 {
-    if (__SIGTEST(proc->sig_pending, _SIGCONT)) {
-        __SIGCLEAR(proc->sig_pending, _SIGSTOP);
-    } else if (__SIGTEST(proc->sig_pending, _SIGSTOP)) {
+    if (!proc) {
+        return 0;
+    }
+
+    struct sighail* sh = &proc->sigctx;
+
+    if ((proc->state & PS_PAUSED)) {
+        return !!(sh->sig_pending & ~1);
+    }
+
+    if (sigset_test(sh->sig_pending, _SIGCONT)) {
+        sigset_clear(sh->sig_pending, _SIGSTOP);
+    } else if (sigset_test(sh->sig_pending, _SIGSTOP)) {
         // 如果进程受到SIGSTOP,则该进程不给予调度。
         return 0;
     }
 
-    return 1;
+    return (proc->state == PS_READY);
 }
 
 void
@@ -142,7 +153,7 @@ check_sleepers()
 
         if (atime && now >= atime) {
             pos->sleep.alarm_time = 0;
-            __SIGSET(pos->sig_pending, _SIGALRM);
+            proc_setsignal(pos, _SIGALRM);
         }
 
         if (!wtime && !atime) {
@@ -164,6 +175,7 @@ schedule()
     struct proc_info* next;
     int prev_ptr = sched_ctx.procs_index;
     int ptr = prev_ptr;
+    int found = 0;
 
     if (!(__current->state & ~PS_RUNNING)) {
         __current->state = PS_READY;
@@ -173,23 +185,20 @@ schedule()
 
     // round-robin scheduler
 redo:
+
     do {
         ptr = (ptr + 1) % sched_ctx.ptable_len;
         next = sched_ctx._procs[ptr];
-    } while (!next || (next->state != PS_READY && ptr != prev_ptr));
-
-    sched_ctx.procs_index = ptr;
 
-    if (next->state != PS_READY) {
-        // schedule the dummy process if we're out of choice
-        next = &dummy_proc;
-        goto done;
-    }
+        if (!(found = can_schedule(next))) {
+            if (ptr == prev_ptr) {
+                next = &dummy_proc;
+                goto done;
+            }
+        }
+    } while (!found);
 
-    if (!can_schedule(next)) {
-        // 如果该进程不给予调度,则尝试重新选择
-        goto redo;
-    }
+    sched_ctx.procs_index = ptr;
 
 done:
     run(next);
@@ -214,12 +223,17 @@ __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
 
     struct proc_info* root_proc = sched_ctx._procs[0];
     __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
-    llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
+
+    if (llist_empty(&__current->sleep.sleepers)) {
+        llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
+    }
 
     __current->intr_ctx.registers.eax = seconds;
 
     block_current();
     schedule();
+
+    return 0;
 }
 
 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
@@ -299,7 +313,6 @@ repeat:
     goto repeat;
 
 done:
-    status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
     if (status) {
         *status = proc->exit_code | status_flags;
     }
@@ -325,6 +338,7 @@ alloc_process()
 
     proc->state = PS_CREATED;
     proc->pid = i;
+    proc->mm.pid = i;
     proc->created = clock_systime();
     proc->pgid = proc->pid;
     proc->fdtable = vzalloc(sizeof(struct v_fdtable));
@@ -366,7 +380,7 @@ commit_process(struct proc_info* process)
 
 // from <kernel/process.c>
 extern void
-__del_pagetable(pid_t pid, uintptr_t mount_point);
+__del_pagetable(pid_t pid, ptr_t mount_point);
 
 pid_t
 destroy_process(pid_t pid)
@@ -374,8 +388,9 @@ destroy_process(pid_t pid)
     int index = pid;
     if (index <= 0 || index > sched_ctx.ptable_len) {
         __current->k_status = EINVAL;
-        return;
+        return -1;
     }
+
     struct proc_info* proc = sched_ctx._procs[index];
     sched_ctx._procs[index] = 0;
 
@@ -407,7 +422,7 @@ destroy_process(pid_t pid)
     llist_for_each(pos, n, &proc->mm.regions, head)
     {
         mem_sync_pages(VMS_MOUNT_1, pos, pos->start, pos->end - pos->start, 0);
-        region_release(pid, pos);
+        region_release(pos);
     }
 
     __del_pagetable(pid, VMS_MOUNT_1);
@@ -425,7 +440,7 @@ terminate_proc(int exit_code)
     __current->state = PS_TERMNAT;
     __current->exit_code = exit_code;
 
-    __SIGSET(__current->parent->sig_pending, _SIGCHLD);
+    proc_setsignal(__current->parent, _SIGCHLD);
 }
 
 struct proc_info*