From: acevest Date: Mon, 15 Nov 2021 04:28:03 +0000 (+0800) Subject: 代码整理 X-Git-Url: http://zhaoyanbai.com/repos/Bv9ARM.ch12.html?a=commitdiff_plain;h=36a9ed031d66ea41c5bb44d3e6f3ea2333608510;p=kernel.git 代码整理 --- diff --git a/kernel/setup.c b/kernel/setup.c index f951102..7c1991d 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -89,14 +89,12 @@ void setup_kernel() { extern tty_t monitor_tty; tty_switch(&monitor_tty); - - return; - - setup_fs(); } // 在开中断的情况下继续初始化的内容 void setup_under_irq() { void ide_init(); ide_init(); + return; + setup_fs(); } \ No newline at end of file diff --git a/kernel/system.c b/kernel/system.c index d42e98f..2cba453 100644 --- a/kernel/system.c +++ b/kernel/system.c @@ -184,3 +184,10 @@ int sysc_reboot(int mode) { return 0; } + +System system; +TSS tss; +Desc idt[NIDT] __attribute__((__aligned__(8))); +Desc gdt[NGDT] __attribute__((__aligned__(8))); +char gdtr[6] __attribute__((__aligned__(4))); +char idtr[6] __attribute__((__aligned__(4))); \ No newline at end of file diff --git a/kernel/task_disk.c b/kernel/task_disk.c new file mode 100644 index 0000000..6be1a2f --- /dev/null +++ b/kernel/task_disk.c @@ -0,0 +1,16 @@ +/* + * ------------------------------------------------------------------------ + * File Name: task_disk.c + * Author: Zhao Yanbai + * 2021-11-15 12:19:00 Monday CST + * Description: none + * ------------------------------------------------------------------------ + */ + +#include + +void disk_task_entry() { + while (1) { + schedule(); + } +} \ No newline at end of file diff --git a/kernel/task_init.c b/kernel/task_init.c new file mode 100644 index 0000000..b46df5e --- /dev/null +++ b/kernel/task_init.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void init_task_entry() { + current->priority = 100; + + // 继续内核未完成的初始化 + // 这些初始化在开中断的情况下完成 + void setup_under_irq(); + setup_under_irq(); + + while (1) { + asm("hlt;"); + } +} diff --git a/kernel/task_root.c b/kernel/task_root.c new file mode 100644 index 0000000..8a97414 --- /dev/null +++ b/kernel/task_root.c @@ -0,0 +1,70 @@ +/* + * ------------------------------------------------------------------------ + * File Name: task_root.c + * Author: Zhao Yanbai + * 2021-11-15 12:20:22 Monday CST + * Description: none + * ------------------------------------------------------------------------ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void disk_task_entry(); +void init_task_entry(); +void user_task_entry(); +int do_fork(pt_regs_t *regs, unsigned long flags); + +void kernel_task(char *name, void *entry) { + pt_regs_t regs; + + memset((void *)®s, 0, sizeof(regs)); + + // 内核任务入口 + regs.edx = (unsigned long)entry; + + regs.eax = (u32)name; + + // 创建内核任务的时候就直接指定其在fork后走的路径 + // 就不用走sysexit那个路径了 + extern void ret_from_fork_krnl(); + regs.eip = (unsigned long)ret_from_fork_krnl; + regs.cs = SELECTOR_KRNL_CS; + regs.ds = SELECTOR_KRNL_DS; + regs.es = SELECTOR_KRNL_DS; + regs.ss = SELECTOR_KRNL_DS; + regs.fs = SELECTOR_KRNL_DS; + regs.gs = SELECTOR_KRNL_DS; + + int pid = do_fork(®s, FORK_KRNL); + + printk("kernel[%s] task pid is %d\n", name, pid); +} + +// 从multiboot.S进入这里 +void root_task_entry() { + sti(); + + // 有一点点垃圾事情需要处理 + // 之前内核初始化都是在关中断下进行的 + // 这就段时间有可能按键盘,然而键盘不把数据读出来就不会触发下一次中断 + // 所以得先清空一下键盘 + inb(0x60); + + kernel_task("init", init_task_entry); + kernel_task("disk", disk_task_entry); + kernel_task("user", user_task_entry); + + while (1) { + asm("hlt;"); + } +} \ No newline at end of file diff --git a/kernel/init.c b/kernel/task_user.c similarity index 54% rename from kernel/init.c rename to kernel/task_user.c index 3abeb06..288f67a 100644 --- a/kernel/init.c +++ b/kernel/task_user.c @@ -1,3 +1,11 @@ +/* + * ------------------------------------------------------------------------ + * File Name: task_user.c + * Author: Zhao Yanbai + * 2021-11-15 12:21:31 Monday CST + * Description: none + * ------------------------------------------------------------------------ + */ #include #include #include @@ -10,20 +18,6 @@ #include #include -void root_task_entry(); -int do_fork(pt_regs_t *regs, unsigned long flags); - -System system; -TSS tss; -Desc idt[NIDT] __attribute__((__aligned__(8))); -Desc gdt[NGDT] __attribute__((__aligned__(8))); -char gdtr[6] __attribute__((__aligned__(4))); -char idtr[6] __attribute__((__aligned__(4))); - -// char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__((__aligned__(PAGE_SIZE))); - -// int debug_wait_queue_get(); - #define __ring3text__ __attribute__((__section__(".ring3.text"))) int ring3_sysctest(); @@ -89,84 +83,4 @@ void user_task_entry() { // eip --> edx // esp --> ecx asm volatile("sysexit;" ::"d"(0x08000000), "c"(0x30000000 + PAGE_SIZE - 100)); -} - -void init_task_entry() { - int cnt = 0; - pid_t id = sysc_getpid(); - - // 赋予不同的优先级 - current->priority = id * 30; - - if (current->priority <= 0) { - current->priority = 1; - } - if (current->priority > 100) { - current->priority = 100; - } - - while (1) { - // sysc_test(); - // printl(MPL_TASK_0 + id, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); - // printl(MPL_TASK_1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); - int v = 0; // debug_wait_queue_get(); - // printk("task:%d wait queue get %d\n", id, v); - asm("hlt;"); - } -} - -void disk_task_entry() { - while (1) { - schedule(); - } -} - -void kernel_task(char *name, void *entry) { - pt_regs_t regs; - - memset((void *)®s, 0, sizeof(regs)); - - // 内核任务入口 - regs.edx = (unsigned long)entry; - - regs.eax = (u32)name; - - // 创建内核任务的时候就直接指定其在fork后走的路径 - // 就不用走sysexit那个路径了 - extern void ret_from_fork_krnl(); - regs.eip = (unsigned long)ret_from_fork_krnl; - regs.cs = SELECTOR_KRNL_CS; - regs.ds = SELECTOR_KRNL_DS; - regs.es = SELECTOR_KRNL_DS; - regs.ss = SELECTOR_KRNL_DS; - regs.fs = SELECTOR_KRNL_DS; - regs.gs = SELECTOR_KRNL_DS; - - int pid = do_fork(®s, FORK_KRNL); - - printk("kernel[%s] task pid is %d\n", name, pid); -} - -// 从multiboot.S进入这里 -void root_task_entry() { - sti(); - - // 有一点点垃圾事情需要处理 - // 之前内核初始化都是在关中断下进行的 - // 这就段时间有可能按键盘,然而键盘不把数据读出来就不会触发下一次中断 - // 所以得先清空一下键盘 - inb(0x60); - - // 继续内核未完成的初始化 - // 这些初始化在开中断的情况下完成 - void setup_under_irq(); - setup_under_irq(); - - kernel_task("init", init_task_entry); - kernel_task("disk", disk_task_entry); - kernel_task("user", user_task_entry); - - while (1) { - asm("hlt;"); - } -} +} \ No newline at end of file