feat: No more kernel page table switching upon interrupt.
[lunaix-os.git] / lunaix-os / kernel / sched.c
index f4fb021acccfdbf04cc6ab72d936f1202af3cbac..758b157befb68a2b48656ab90f9887067c0f4997 100644 (file)
@@ -1,17 +1,21 @@
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/mm/vmm.h>
+#include <lunaix/mm/kalloc.h>
 #include <hal/cpu.h>
 #include <arch/x86/interrupts.h>
 #include <hal/cpu.h>
 #include <arch/x86/interrupts.h>
+#include <arch/x86/tss.h>
 #include <hal/apic.h>
 
 #include <lunaix/spike.h>
 #include <lunaix/status.h>
 #include <lunaix/syslog.h>
 #include <hal/apic.h>
 
 #include <lunaix/spike.h>
 #include <lunaix/status.h>
 #include <lunaix/syslog.h>
+#include <lunaix/syscall.h>
 
 #define MAX_PROCESS 512
 
 
 #define MAX_PROCESS 512
 
-struct proc_info* __current;
+volatile struct proc_info* __current;
+
 struct proc_info dummy;
 
 extern void __proc_table;
 struct proc_info dummy;
 
 extern void __proc_table;
@@ -34,6 +38,31 @@ void sched_init() {
     };
 }
 
     };
 }
 
+void run(struct proc_info* proc) {
+    if (!(__current->state & ~PROC_RUNNING)) {
+        __current->state = PROC_STOPPED;
+    }
+    proc->state = PROC_RUNNING;
+    
+    // FIXME: 这里还是得再考虑一下。
+    // tss_update_esp(__current->intr_ctx.esp);
+
+    if (__current->page_table != proc->page_table) {
+        __current = proc;
+        cpu_lcr3(__current->page_table);
+        // from now on, the we are in the kstack of another process
+    }
+    else {
+        __current = proc;
+    }
+
+    apic_done_servicing();
+
+    asm volatile (
+        "pushl %0\n"
+        "jmp soft_iret\n"::"r"(&__current->intr_ctx): "memory");
+}
+
 void schedule() {
     if (!sched_ctx.ptable_len) {
         return;
 void schedule() {
     if (!sched_ctx.ptable_len) {
         return;
@@ -46,22 +75,61 @@ void schedule() {
     do {
         ptr = (ptr + 1) % sched_ctx.ptable_len;
         next = &sched_ctx._procs[ptr];
     do {
         ptr = (ptr + 1) % sched_ctx.ptable_len;
         next = &sched_ctx._procs[ptr];
-    } while((next->state != PROC_STOPPED && next->state != PROC_CREATED) && ptr != prev_ptr);
+    } while(next->state != PROC_STOPPED && ptr != prev_ptr);
     
     sched_ctx.procs_index = ptr;
     
     sched_ctx.procs_index = ptr;
-    
-    __current->state = PROC_STOPPED;
-    next->state = PROC_RUNNING;
-    
-    __current = next;
 
 
-    cpu_lcr3(__current->page_table);
 
 
-    apic_done_servicing();
+    run(next);
+}
 
 
-    asm volatile (
-        "pushl %0\n"
-        "jmp soft_iret\n"::"r"(&__current->intr_ctx): "memory");
+static void proc_timer_callback(struct proc_info* proc) {
+    proc->timer = NULL;
+    proc->state = PROC_STOPPED;
+}
+
+__DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) {
+    // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
+    if (!seconds) {
+        return 0;
+    }
+    if (__current->timer) {
+        return __current->timer->counter / timer_context()->running_frequency;
+    }
+
+    struct lx_timer* timer = timer_run_second(seconds, proc_timer_callback, __current, 0);
+    __current->timer = timer;
+    __current->intr_ctx.registers.eax = seconds;
+    __current->state = PROC_BLOCKED;
+    schedule();
+}
+
+__DEFINE_LXSYSCALL1(void, exit, int, status) {
+    terminate_proc(status);
+}
+
+__DEFINE_LXSYSCALL(void, yield) {
+    schedule();
+}
+
+__DEFINE_LXSYSCALL1(pid_t, wait, int*, status) {
+    pid_t cur = __current->pid;
+    struct proc_info *proc, *n;
+    if (llist_empty(&__current->children)) {
+        return -1;
+    }
+repeat:
+    llist_for_each(proc, n, &__current->children, siblings) {
+        if (proc->state == PROC_TERMNAT) {
+            goto done;
+        }
+    }
+    // FIXME: 除了循环,也许有更高效的办法…… (在这里进行schedule,需要重写context switch!)
+    goto repeat;
+
+done:
+    *status = proc->exit_code;
+    return destroy_process(proc->pid);
 }
 
 pid_t alloc_pid() {
 }
 
 pid_t alloc_pid() {
@@ -69,8 +137,7 @@ pid_t alloc_pid() {
     for (; i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY; i++);
 
     if (i == MAX_PROCESS) {
     for (; i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY; i++);
 
     if (i == MAX_PROCESS) {
-        __current->k_status = LXPROCFULL;
-        return -1;
+        panick("Process table is full");
     }
     return i + 1;
 }
     }
     return i + 1;
 }
@@ -86,25 +153,53 @@ void push_process(struct proc_info* process) {
         sched_ctx.ptable_len++;
     }
     
         sched_ctx.ptable_len++;
     }
     
-    process->parent = __current->pid;
-    process->state = PROC_CREATED;
-
     sched_ctx._procs[index] = *process;
     sched_ctx._procs[index] = *process;
+
+    process = &sched_ctx._procs[index];
+
+    // make sure the address is in the range of process table
+    llist_init_head(&process->children);
+    // every process is the child of first process (pid=1)
+    if (process->parent) {
+        llist_append(&process->parent->children, &process->siblings);
+    }
+    else {
+        process->parent = &sched_ctx._procs[0];
+    }
+
+    process->state = PROC_STOPPED;    
 }
 
 }
 
-void destroy_process(pid_t pid) {
+// from <kernel/process.c>
+extern void __del_pagetable(pid_t pid, uintptr_t mount_point);
+
+pid_t destroy_process(pid_t pid) {
     int index = pid - 1;
     int index = pid - 1;
-    if (index < 0 || index > sched_ctx.ptable_len) {
+    if (index <= 0 || index > sched_ctx.ptable_len) {
         __current->k_status = LXINVLDPID;
         return;
     }
         __current->k_status = LXINVLDPID;
         return;
     }
+    struct proc_info *proc = &sched_ctx._procs[index];
+    proc->state = PROC_DESTROY;
+    llist_delete(&proc->siblings);
+
+    if (proc->mm.regions) {
+        struct mm_region *pos, *n;
+        llist_for_each(pos, n, &proc->mm.regions->head, head) {
+            lxfree(pos);
+        }
+    }
+
+    vmm_mount_pd(PD_MOUNT_2, proc->page_table);
+
+    __del_pagetable(pid, PD_MOUNT_2);
 
 
-    sched_ctx._procs[index].state = PROC_DESTROY;
+    vmm_unmount_pd(PD_MOUNT_2);
 
 
-    // TODO: recycle the physical pages used by page tables
+    return pid;
 }
 
 }
 
-void terminate_process(int exit_code) {
+void terminate_proc(int exit_code) {
     __current->state = PROC_TERMNAT;
     __current->exit_code = exit_code;
 
     __current->state = PROC_TERMNAT;
     __current->exit_code = exit_code;
 
@@ -117,4 +212,15 @@ struct proc_info* get_process(pid_t pid) {
         return NULL;
     }
     return &sched_ctx._procs[index];
         return NULL;
     }
     return &sched_ctx._procs[index];
+}
+
+int orphaned_proc(pid_t pid) {
+    if(!pid) return 0;
+    if(pid >= sched_ctx.ptable_len) return 0;
+    struct proc_info* proc = &sched_ctx._procs[pid-1];
+    struct proc_info* parent = proc->parent;
+    
+    // 如果其父进程的状态是terminated 或 destroy中的一种
+    // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
+    return (parent->state & PROC_TERMMASK) || parent->created > proc->created;
 }
\ No newline at end of file
 }
\ No newline at end of file