fix: sleeper issue #13
[lunaix-os.git] / lunaix-os / kernel / process / sched.c
index f58710ee347387eee1629214b8b3c270c25b1a62..5e882b7df8f4c32fa3ff7757f84be78d215efae4 100644 (file)
@@ -4,6 +4,7 @@
 #include <hal/apic.h>
 #include <hal/cpu.h>
 
+#include <lunaix/fs/taskfs.h>
 #include <lunaix/mm/cake.h>
 #include <lunaix/mm/kalloc.h>
 #include <lunaix/mm/pmm.h>
@@ -19,6 +20,8 @@
 
 volatile struct proc_info* __current;
 
+static struct proc_info dummy_proc;
+
 struct proc_info dummy;
 
 struct scheduler sched_ctx;
@@ -27,23 +30,58 @@ struct cake_pile* proc_pile;
 
 LOG_MODULE("SCHED")
 
+void
+sched_init_dummy();
+
 void
 sched_init()
 {
-    // size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
-
-    // for (size_t i = 0; i <= pg_size; i += 4096) {
-    //     uintptr_t pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
-    //     vmm_set_mapping(
-    //       PD_REFERENCED, PROC_START + i, pa, PG_PREM_RW, VMAP_NULL);
-    // }
-
     proc_pile = cake_new_pile("proc", sizeof(struct proc_info), 1, 0);
     cake_set_constructor(proc_pile, cake_ctor_zeroing);
 
     sched_ctx = (struct scheduler){ ._procs = vzalloc(PROC_TABLE_SIZE),
                                     .ptable_len = 0,
                                     .procs_index = 0 };
+
+    // TODO initialize dummy_proc
+    sched_init_dummy();
+}
+
+#define DUMMY_STACK_SIZE 2048
+
+void
+sched_init_dummy()
+{
+    // This surely need to be simplified or encapsulated!
+    // It is a living nightmare!
+
+    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 },
+        .cs = KCODE_SEG,
+        .eip = (void*)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;
+
+    dummy_proc.page_table = cpu_rcr3();
+    dummy_proc.state = PS_READY;
+    dummy_proc.parent = &dummy_proc;
+    dummy_proc.pid = KERNEL_PID;
+
+    __current = &dummy_proc;
 }
 
 void
@@ -141,11 +179,18 @@ redo:
 
     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 (!can_schedule(next)) {
         // 如果该进程不给予调度,则尝试重新选择
         goto redo;
     }
 
+done:
     run(next);
 }
 
@@ -168,10 +213,14 @@ __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;
-    __current->state = PS_BLOCKED;
+
+    block_current();
     schedule();
 }
 
@@ -281,6 +330,8 @@ alloc_process()
     proc->created = clock_systime();
     proc->pgid = proc->pid;
     proc->fdtable = vzalloc(sizeof(struct v_fdtable));
+    proc->fxstate =
+      vzalloc_dma(512); // FXSAVE需要十六位对齐地址,使用DMA块(128位对齐)
 
     llist_init_head(&proc->mm.regions.head);
     llist_init_head(&proc->tasks);
@@ -331,14 +382,26 @@ destroy_process(pid_t pid)
     sched_ctx._procs[index] = 0;
 
     llist_delete(&proc->siblings);
+    llist_delete(&proc->grp_member);
+    llist_delete(&proc->tasks);
+    llist_delete(&proc->sleep.sleepers);
+
+    taskfs_invalidate(pid);
+
+    if (proc->cwd) {
+        vfs_unref_dnode(proc->cwd);
+    }
 
     for (size_t i = 0; i < VFS_MAX_FD; i++) {
         struct v_fd* fd = proc->fdtable->fds[i];
-        if (fd)
-            vfs_close(fd->file);
+        if (fd) {
+            vfs_pclose(fd->file, pid);
+            vfs_free_fd(fd);
+        }
     }
 
     vfree(proc->fdtable);
+    vfree_dma(proc->fxstate);
 
     struct mm_region *pos, *n;
     llist_for_each(pos, n, &proc->mm.regions.head, head)