X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/0067bc210e621ccda286092d081a7890d65e1c18..d342435e493c415e10042cfc8aab45c2b2aa0868:/lunaix-os/kernel/sched.c diff --git a/lunaix-os/kernel/sched.c b/lunaix-os/kernel/sched.c index f5c2110..8c43b14 100644 --- a/lunaix-os/kernel/sched.c +++ b/lunaix-os/kernel/sched.c @@ -1,18 +1,21 @@ -#include -#include -#include -#include #include +#include #include +#include +#include +#include +#include +#include #include #include -#include #include +#include #define MAX_PROCESS 512 -struct proc_info* __current; +volatile struct proc_info* __current; + struct proc_info dummy; extern void __proc_table; @@ -21,42 +24,54 @@ struct scheduler sched_ctx; LOG_MODULE("SCHED") -void sched_init() { +void +sched_init() +{ size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000); - assert_msg( - vmm_alloc_pages(KERNEL_PID, &__proc_table, pg_size, PG_PREM_RW, PP_FGPERSIST), - "Fail to allocate proc table" - ); - - sched_ctx = (struct scheduler) { - ._procs = (struct proc_info*) &__proc_table, - .ptable_len = 0, - .procs_index = 0 - }; + assert_msg(vmm_alloc_pages( + KERNEL_PID, &__proc_table, pg_size, PG_PREM_RW, PP_FGPERSIST), + "Fail to allocate proc table"); + + sched_ctx = (struct scheduler){ ._procs = (struct proc_info*)&__proc_table, + .ptable_len = 0, + .procs_index = 0 }; } -void run(struct proc_info* proc) { +void +run(struct proc_info* proc) +{ if (!(__current->state & ~PROC_RUNNING)) { __current->state = PROC_STOPPED; } proc->state = PROC_RUNNING; - - __current = proc; - cpu_lcr3(__current->page_table); + // 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"); + asm volatile("movl %0, %%eax\n" + "jmp soft_iret\n" ::"r"(&__current->intr_ctx) + : "eax", "memory"); } -void schedule() { +void +schedule() +{ if (!sched_ctx.ptable_len) { return; } + // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序…… + cpu_disable_interrupt(); struct proc_info* next; int prev_ptr = sched_ctx.procs_index; int ptr = prev_ptr; @@ -64,104 +79,210 @@ void schedule() { do { ptr = (ptr + 1) % sched_ctx.ptable_len; next = &sched_ctx._procs[ptr]; - } while(next->state != PROC_STOPPED && ptr != prev_ptr); - + } while (next->state != PROC_STOPPED && ptr != prev_ptr); + sched_ctx.procs_index = ptr; - + run(next); } -static void proc_timer_callback(struct proc_info* proc) { +static void +proc_timer_callback(struct proc_info* proc) +{ proc->timer = NULL; proc->state = PROC_STOPPED; } -__DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) { +__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); + 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) { +__DEFINE_LXSYSCALL1(void, exit, int, status) +{ terminate_proc(status); } -__DEFINE_LXSYSCALL(void, yield) { +__DEFINE_LXSYSCALL(void, yield) +{ schedule(); } -pid_t alloc_pid() { +pid_t +_wait(pid_t wpid, int* status, int options); + +__DEFINE_LXSYSCALL1(pid_t, wait, int*, status) +{ + return _wait(-1, status, 0); +} + +__DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options) +{ + return _wait(pid, status, options); +} + +pid_t +_wait(pid_t wpid, int* status, int options) +{ + pid_t cur = __current->pid; + int status_flags = 0; + struct proc_info *proc, *n; + if (llist_empty(&__current->children)) { + return -1; + } + + wpid = wpid ? wpid : -__current->pgid; + cpu_enable_interrupt(); +repeat: + llist_for_each(proc, n, &__current->children, siblings) + { + if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) { + if (proc->state == PROC_TERMNAT && !options) { + status_flags |= PROCTERM; + goto done; + } + if (proc->state == PROC_STOPPED && (options & WUNTRACED)) { + status_flags |= PROCSTOP; + goto done; + } + } + } + if ((options & WNOHANG)) { + return 0; + } + // 放弃当前的运行机会 + sched_yield(); + goto repeat; + +done: + cpu_disable_interrupt(); + *status = (proc->exit_code & 0xffff) | status_flags; + return destroy_process(proc->pid); +} + +pid_t +alloc_pid() +{ pid_t i = 0; - for (; i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY; i++); + 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("Panic in Ponyville shimmer!"); } - return i + 1; + return i; } -void push_process(struct proc_info* process) { - int index = process->pid - 1; +void +push_process(struct proc_info* process) +{ + int index = process->pid; if (index < 0 || index > sched_ctx.ptable_len) { __current->k_status = LXINVLDPID; return; } - + if (index == sched_ctx.ptable_len) { sched_ctx.ptable_len++; } - - // every process is the parent of first process (pid=1) - process->parent = process->parent ? process->parent : &sched_ctx._procs; - process->state = PROC_STOPPED; sched_ctx._procs[index] = *process; + + process = &sched_ctx._procs[index]; + + // make sure the reference is relative to process table + llist_init_head(&process->children); + llist_init_head(&process->grp_member); + + // 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) { - int index = pid - 1; +// from +extern void +__del_pagetable(pid_t pid, uintptr_t mount_point); + +pid_t +destroy_process(pid_t pid) +{ + int index = pid; if (index <= 0 || index > sched_ctx.ptable_len) { __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_proc(int exit_code) { - __current->state = exit_code < 0 ? PROC_SPOILED : PROC_TERMNAT; +void +terminate_proc(int exit_code) +{ + __current->state = PROC_TERMNAT; __current->exit_code = exit_code; schedule(); } -struct proc_info* get_process(pid_t pid) { - int index = pid - 1; +struct proc_info* +get_process(pid_t pid) +{ + int index = pid; if (index < 0 || index > sched_ctx.ptable_len) { 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]; +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]; struct proc_info* parent = proc->parent; - - // 如果其父进程的状态是terminated, spoiled 或 destroy中的一种 + + // 如果其父进程的状态是terminated 或 destroy中的一种 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程 - return (parent->state & 0xe) || parent->created > proc->created; + return (parent->state & PROC_TERMMASK) || parent->created > proc->created; } \ No newline at end of file