From 48bc88cf10e9284e45c418e3fbb68ba0753e99ad Mon Sep 17 00:00:00 2001 From: acevest Date: Mon, 15 Nov 2021 09:25:10 +0800 Subject: [PATCH] =?utf8?q?kernel=5Ftask=E4=B8=8D=E6=8A=8A=E5=90=8D?= =?utf8?q?=E5=AD=97=E4=BC=A0=E9=80=92=E7=BB=99=E5=86=85=E6=A0=B8=E5=87=BD?= =?utf8?q?=E6=95=B0=E5=85=A5=E5=8F=A3,=E7=9B=B4=E6=8E=A5=E5=9C=A8do=5Ffork?= =?utf8?q?=E9=87=8C=E8=B5=8B=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- kernel/fork.c | 7 ++++--- kernel/init.c | 28 +++++++++++----------------- kernel/sched.c | 8 +++++--- kernel/syscall.S | 2 -- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/kernel/fork.c b/kernel/fork.c index ea2848c..6291501 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -85,16 +85,17 @@ int do_fork(pt_regs_t *regs, unsigned long flags) { printk("child regs: %x %x\n", child_regs, regs); memcpy(child_regs, regs, sizeof(*regs)); - child_regs->eax = 0; - child_regs->eflags |= 0x200; // enable IF - tsk->esp0 = TASK_SIZE + (unsigned long)tsk; tsk->esp = (unsigned long)child_regs; tsk->eip = (unsigned long)ret_from_fork_user; if (flags & FORK_KRNL) { + strcpy(tsk->name, (char *)(child_regs->eax)); tsk->eip = (unsigned long)ret_from_fork_krnl; } + child_regs->eax = 0; + child_regs->eflags |= 0x200; // enable IF + printk("tsk %08x child_regs esp %08x esp0 %08x\n", tsk, tsk->esp, tsk->esp0); tsk->state = TASK_INITING; diff --git a/kernel/init.c b/kernel/init.c index 4d0707c..3abeb06 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -58,9 +58,7 @@ void __ring3text__ __attribute__((__aligned__(PAGE_SIZE))) ring3_entry() { } } -void user_task_entry(char *name) { - strcpy(current->name, name); - +void user_task_entry() { current->priority = 90; unsigned long ring3_text_page = va2pa(ring3_entry); @@ -93,12 +91,10 @@ void user_task_entry(char *name) { asm volatile("sysexit;" ::"d"(0x08000000), "c"(0x30000000 + PAGE_SIZE - 100)); } -void init_task_entry(char *name) { +void init_task_entry() { int cnt = 0; pid_t id = sysc_getpid(); - strcpy(current->name, name); - // 赋予不同的优先级 current->priority = id * 30; @@ -119,6 +115,12 @@ void init_task_entry(char *name) { } } +void disk_task_entry() { + while (1) { + schedule(); + } +} + void kernel_task(char *name, void *entry) { pt_regs_t regs; @@ -127,6 +129,8 @@ void kernel_task(char *name, void *entry) { // 内核任务入口 regs.edx = (unsigned long)entry; + regs.eax = (u32)name; + // 创建内核任务的时候就直接指定其在fork后走的路径 // 就不用走sysexit那个路径了 extern void ret_from_fork_krnl(); @@ -138,8 +142,6 @@ void kernel_task(char *name, void *entry) { regs.fs = SELECTOR_KRNL_DS; regs.gs = SELECTOR_KRNL_DS; - regs.ebx = (unsigned long)name; // 用ebx传递参数 - int pid = do_fork(®s, FORK_KRNL); printk("kernel[%s] task pid is %d\n", name, pid); @@ -161,18 +163,10 @@ void root_task_entry() { setup_under_irq(); kernel_task("init", init_task_entry); - kernel_task("test", init_task_entry); + kernel_task("disk", disk_task_entry); kernel_task("user", user_task_entry); - int cnt = 0; - while (1) { - // sysc_test(); - // printl(MPL_TASK_0, "root:0 [%08x] weight %d cnt %d", current, root_task.weight, cnt++); - // printk("root:0 [%08x] weight %d cnt %d", current, current->weight, cnt++); asm("hlt;"); - // asm("nop;nop;nop;"); - // sysc_test(); - // syscall0(SYSC_TEST); } } diff --git a/kernel/sched.c b/kernel/sched.c index f3ce8e3..fd5a8c7 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -49,7 +49,7 @@ LIST_HEAD(all_tasks); LIST_HEAD(delay_tasks); -void init_root_tsk() { +void init_root_task() { int i; root_task.preempt_cnt = 0; @@ -83,7 +83,7 @@ void setup_tasks() { INIT_LIST_HEAD(&all_tasks); INIT_LIST_HEAD(&delay_tasks); - init_root_tsk(); + init_root_task(); task_union_cache = kmem_cache_create("task_union", sizeof(task_union), PAGE_SIZE); if (0 == task_union_cache) { @@ -144,7 +144,9 @@ static const char *task_state(unsigned int state) { " ERROR", "RUNNING", " WAIT", "INITING", "EXITING", }; - if (state >= TASK_END) state = TASK_UNUSED; + if (state >= TASK_END) { + state = TASK_UNUSED; + } return s[state]; } diff --git a/kernel/syscall.S b/kernel/syscall.S index 76b3ad1..f813627 100644 --- a/kernel/syscall.S +++ b/kernel/syscall.S @@ -90,8 +90,6 @@ ret_from_fork_krnl: popfl addl $8, %esp - # 用ebx传递参数 - pushl %ebx call *%edx addl $4, %esp -- 2.44.0