From: acevest Date: Wed, 3 Nov 2021 08:27:57 +0000 (+0800) Subject: 三个不同优先级的内核进程开始运行 X-Git-Url: http://zhaoyanbai.com/repos/man.dnssec-signzone.html?a=commitdiff_plain;h=749087653c06f130bd68f6619b34ef73832343c3;p=kernel.git 三个不同优先级的内核进程开始运行 --- diff --git a/kernel/init.c b/kernel/init.c index bd699fa..7f1bcf8 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -37,6 +37,15 @@ 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_1 + id - 1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); @@ -63,16 +72,20 @@ void kernel_task(char *name, void *entry) { 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 task pid is %d\n", pid); + printk("kernel[%s] task pid is %d\n", name, pid); } void root_task_entry() { + sti(); + kernel_task("init", init_task_entry); + kernel_task("test", init_task_entry); // kernel_task(user_task_entry); - // kernel_task(init_task_entry); int cnt = 0; while (1) { @@ -80,6 +93,7 @@ void root_task_entry() { printl(MPL_ROOT, "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("sti;hlt;"); + // asm("nop;nop;nop;"); sysc_test(); // syscall0(SYSC_TEST); } diff --git a/kernel/sched.c b/kernel/sched.c index 2781836..8e210f1 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -20,6 +20,7 @@ #include "init.h" #include "mm.h" #include "msr.h" +#include "system.h" task_union root_task __attribute__((__aligned__(PAGE_SIZE))); @@ -47,10 +48,6 @@ list_head_t all_tasks; void init_root_tsk() { int i; - // never use memset to init root_task - // because the stack is at top of the root_task - // memset((char*)&root_task, 0, sizeof(root_task)); - root_task.preempt_cnt = 0; root_task.pid = get_next_pid(); root_task.ppid = 0; @@ -70,6 +67,7 @@ void init_root_tsk() { root_task.cr3 = (unsigned long)init_pgd; tss.esp0 = root_task.esp0; + wrmsr(MSR_SYSENTER_ESP, root_task.esp0, 0); printk("init_root_task tss.esp0 %08x\n", tss.esp0); @@ -130,10 +128,7 @@ task_union *find_task(pid_t pid) { static const char *task_state(unsigned int state) { static const char s[][16] = { - " ERROR", - "RUNNING", - " WAIT", - "EXITING", + " ERROR", "RUNNING", " WAIT", "INITING", "EXITING", }; if (state >= TASK_END) state = TASK_UNUSED; @@ -201,6 +196,7 @@ unsigned long schedule() { task_union *next = sel; if (prev != next) { + // printk("switch to: %s:%d\n", next->name, next->pid); context_switch(prev, next); } } diff --git a/kernel/system.c b/kernel/system.c index 9a82df8..a4bc4e4 100644 --- a/kernel/system.c +++ b/kernel/system.c @@ -101,11 +101,12 @@ void setup_irqs() { extern void init_i8259(); init_i8259(); - int i; - for (i = 0; i < NR_IRQS; i++) { + for (int i = 0; i < NR_IRQS; i++) { irq_desc[i] = no_irq_desc; - if (i < 16) irq_desc[i].chip = &i8259_chip; + if (i < 16) { + irq_desc[i].chip = &i8259_chip; + } } void kbd_handler(unsigned int irq, pt_regs_t *regs, void *dev_id); @@ -115,13 +116,20 @@ void setup_irqs() { request_irq(0x01, kbd_handler, "Intel 8042", "PS/2 Keyboard"); request_irq(0x0A, default_ide_irq_handler, "hard", "IDE"); request_irq(0x0E, default_ide_irq_handler, "hard", "IDE"); - for (i = 0; i < 16; i++) { - if (i != 0 && i != 1 && i != 10 && i != 14) request_irq(i, default_irq_handler, "default", "default"); + for (int i = 0; i < 16; i++) { + if (i != 0 && i != 1 && i != 10 && i != 14) { + request_irq(i, default_irq_handler, "default", "default"); + } } - for (i = 0; i < 16; i++) open_irq(i); + for (int i = 0; i < 16; i++) { + close_irq(i); + } - enable_irq(); + open_irq(0x00); + open_irq(0x01); + open_irq(0x0A); + open_irq(0x0E); } void set_tss() { pTSS p = &tss; diff --git a/lib/string.c b/lib/string.c index af0b26d..4c538bf 100644 --- a/lib/string.c +++ b/lib/string.c @@ -64,8 +64,11 @@ void *memcpy(void *dest, const void *src, size_t size) { } void memset(void *dest, char ch, size_t size) { - char *d = (char *)dest; - while (size--) *d++ = ch; + char *p = (char *)dest; + for (size_t i = 0; i < size; i++) { + *p = ch; + p++; + } } int memcmp(const void *a, const void *b, size_t count) {