Merge branch 'interrupt-rework' into prog-loader
[lunaix-os.git] / lunaix-os / kernel / process / sched.c
index 91932ca3dd5d7d4aa3ac3c7c9e170b89cc8d2cb2..ade13b31c6414f4e1e1588b26645f255475f2c6c 100644 (file)
@@ -6,7 +6,7 @@
 
 #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>
 #include <lunaix/mm/vmm.h>
@@ -18,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;
@@ -47,6 +49,8 @@ sched_init()
     sched_init_dummy();
 }
 
+#define DUMMY_STACK_SIZE 2048
+
 void
 sched_init_dummy()
 {
@@ -54,28 +58,30 @@ sched_init_dummy()
     // It is a living nightmare!
 
     extern void my_dummy();
-    static char dummy_stack[1024] __attribute__((aligned(16)));
+    static char dummy_stack[DUMMY_STACK_SIZE] __attribute__((aligned(16)));
+
+    struct exec_param* execp =
+      (void*)dummy_stack + DUMMY_STACK_SIZE - sizeof(struct exec_param);
+
+    *execp = (struct exec_param){
+        .cs = KCODE_SEG,
+        .eflags = cpu_reflags() | 0x0200,
+        .eip = (void*)my_dummy,
+        .ss = KDATA_SEG,
+    };
 
     // 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 + 1004 },
-                   .cs = KCODE_SEG,
-                   .eip = (void*)my_dummy,
-                   .ss = KDATA_SEG,
-                   .eflags = cpu_reflags() | 0x0200 };
-
-    *(u32_t*)(&dummy_stack[1020]) = dummy_proc.intr_ctx.eflags;
-    *(u32_t*)(&dummy_stack[1016]) = KCODE_SEG;
-    *(u32_t*)(&dummy_stack[1012]) = dummy_proc.intr_ctx.eip;
+    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;
     dummy_proc.parent = &dummy_proc;
+    dummy_proc.pid = KERNEL_PID;
 
     __current = &dummy_proc;
 }
@@ -93,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();
 
@@ -320,13 +326,14 @@ 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));
     proc->fxstate =
       vzalloc_dma(512); // FXSAVE需要十六位对齐地址,使用DMA块(128位对齐)
 
-    llist_init_head(&proc->mm.regions.head);
+    llist_init_head(&proc->mm.regions);
     llist_init_head(&proc->tasks);
     llist_init_head(&proc->children);
     llist_init_head(&proc->grp_member);
@@ -396,17 +403,18 @@ destroy_process(pid_t pid)
     vfree(proc->fdtable);
     vfree_dma(proc->fxstate);
 
+    vmm_mount_pd(VMS_MOUNT_1, proc->page_table);
+
     struct mm_region *pos, *n;
-    llist_for_each(pos, n, &proc->mm.regions.head, head)
+    llist_for_each(pos, n, &proc->mm.regions, head)
     {
-        vfree(pos);
+        mem_sync_pages(VMS_MOUNT_1, pos, pos->start, pos->end - pos->start, 0);
+        region_release(pos);
     }
 
-    vmm_mount_pd(PD_MOUNT_1, proc->page_table);
-
-    __del_pagetable(pid, PD_MOUNT_1);
+    __del_pagetable(pid, VMS_MOUNT_1);
 
-    vmm_unmount_pd(PD_MOUNT_1);
+    vmm_unmount_pd(VMS_MOUNT_1);
 
     cake_release(proc_pile, proc);