From 94eb856d417dc430b840bf0b3a7d7c79555bf4b3 Mon Sep 17 00:00:00 2001 From: acevest Date: Mon, 1 Nov 2021 15:41:28 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96=E4=B8=AD=E6=96=AD=E5=A4=84?= =?utf8?q?=E7=90=86=E5=87=BD=E6=95=B0;=E6=9B=B4=E6=94=B9=E8=BF=9B=E7=A8=8B?= =?utf8?q?=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- include/task.h | 1 + kernel/clock.c | 2 +- kernel/fork.c | 7 +++++-- kernel/init.c | 12 +++++++----- kernel/interrupts.S | 21 ++++----------------- kernel/irq.c | 2 ++ kernel/sched.c | 38 +++++++++++++++++++++++++------------- kernel/setup.c | 4 +++- kernel/system.c | 3 +++ mm/mm.c | 4 ++++ 10 files changed, 55 insertions(+), 39 deletions(-) diff --git a/include/task.h b/include/task.h index a29f682..aa0128d 100644 --- a/include/task.h +++ b/include/task.h @@ -54,6 +54,7 @@ typedef union task_union unsigned long eip; long weight; + long priority; pid_t pid; pid_t ppid; diff --git a/kernel/clock.c b/kernel/clock.c index 8526d1d..61a7772 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -25,5 +25,5 @@ void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) jiffies++; //printd("^"); - printl(MPL_CLOCK, "clock irq: %d", jiffies); + //printl(MPL_CLOCK, "clock irq: %d", jiffies); } diff --git a/kernel/fork.c b/kernel/fork.c index 041c3a2..bb72222 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -21,16 +21,19 @@ int sysc_fork(pt_regs_t regs) extern void ret_from_fork_user(); extern void ret_from_fork_krnl(); extern pid_t get_next_pid(); - +extern list_head_t all_tasks; int do_fork(pt_regs_t *regs, unsigned long flags) { task_union *tsk; tsk = alloc_task_union(); + printk("fork task %08x flags %08x\n", tsk, flags); if (tsk == NULL) panic("can not malloc PCB"); memcpy(tsk, current, sizeof(task_union)); + strcpy(tsk->name, "init"); + tsk->priority = 61; //{ tsk->cr3 = (unsigned long)alloc_one_page(0); @@ -118,7 +121,7 @@ int do_fork(pt_regs_t *regs, unsigned long flags) unsigned long iflags; irq_save(iflags); - list_add(&tsk->list, &root_task.list); + list_add(&tsk->list, &all_tasks); irq_restore(iflags); printk("%s:%d\n", __func__, __LINE__); diff --git a/kernel/init.c b/kernel/init.c index 8a0288c..3f9a0c1 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -42,7 +42,8 @@ void init_task_entry() while (1) { sysc_test(); - //printl(MPL_TASK_1+id-1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); + printl(MPL_TASK_1 + id - 1, "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); } @@ -61,15 +62,16 @@ void kernel_task(void *entry) void root_task_entry() { - kernel_task(init_task_entry); - kernel_task(user_task_entry); - kernel_task(init_task_entry); + //kernel_task(init_task_entry); + //kernel_task(user_task_entry); + //kernel_task(init_task_entry); int cnt = 0; while (1) { sysc_test(); - //printl(MPL_ROOT, "root:0 [%08x] weight %d cnt %d", current, root_task.weight, cnt++); + 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;"); sysc_test(); //syscall0(SYSC_TEST); diff --git a/kernel/interrupts.S b/kernel/interrupts.S index e459c7c..4a20308 100644 --- a/kernel/interrupts.S +++ b/kernel/interrupts.S @@ -56,6 +56,10 @@ DEF_IRQ(0,F) _irq_handler: SAVE_REGS + // ebp指向栈桢 + movl $-TASK_SIZE, %ebp + andl %esp, %ebp + movw %ss, %ax movw %ax, %ds movw %ax, %es @@ -65,29 +69,12 @@ _irq_handler: movl %esp, %eax call irq_handler - movl $-TASK_SIZE, %ebp - andl %esp, %ebp - - movl PT_REGS_CS(%esp), %eax - testl $0x0003, %eax - jz resume_kernel - -return: cmpl $0, TI_preempt_cnt(%ebp) jnz restore_regs -resched: - call schedule restore_regs: - RESTORE_REGS - addl $4, %esp - iret - -resume_kernel: - # ... - jmp return diff --git a/kernel/irq.c b/kernel/irq.c index 57f2e2f..c6579b7 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -61,11 +61,13 @@ __attribute__((regparm(1))) void irq_handler(pt_regs_t *regs) p->chip->ack(irq); sti(); + while (action && action->handler) { action->handler(irq, regs, action->dev_id); action = action->next; } + cli(); p->chip->enable(irq); diff --git a/kernel/sched.c b/kernel/sched.c index 7080735..af94d2e 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -25,7 +25,7 @@ pid_t get_next_pid() { static pid_t g_pid = ROOT_TSK_PID; - pid_t pid = g_pid++; + pid_t pid = ++g_pid; return pid; } @@ -36,6 +36,9 @@ void load_cr3(task_union *tsk) } extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE))); + +list_head_t all_tasks; + void init_root_tsk() { int i; @@ -49,8 +52,11 @@ void init_root_tsk() root_task.ppid = 0; root_task.state = TASK_RUNNING; root_task.weight = TASK_INIT_WEIGHT; - strcpy(root_task.name, "root_task"); - INIT_LIST_HEAD(&root_task.list); + root_task.priority = 100; + strcpy(root_task.name, "root"); + + INIT_LIST_HEAD(&all_tasks); + list_add(&root_task.list, &all_tasks); // TODO //for(i=0; ipid == pid) @@ -153,14 +159,17 @@ static const char *task_state(unsigned int state) unsigned long schedule() { static turn = 0; - task_union *sel = 0; + task_union *sel = &root_task; task_union *p = 0; list_head_t *pos = 0, *t = 0; unsigned long iflags; irq_save(iflags); printl(MPL_ROOT, "root:%d [%08x] cnt %u", root_task.pid, &root_task, root_task.cnt); - list_for_each_safe(pos, t, &root_task.list) + + unsigned int min_ratio = ~0U; + + list_for_each_safe(pos, t, &all_tasks) { p = list_entry(pos, task_union, list); @@ -173,19 +182,22 @@ unsigned long schedule() printd("%08x %s weight %d\n", p, p->name, p->weight); - if (sel == 0 && p->weight != turn) + unsigned int ratio = p->weight / p->priority; + if (ratio < min_ratio) { - p->weight = turn; sel = p; + min_ratio = ratio; + } + + p->weight++; + if (p->weight > p->priority) + { + p->weight = 0; } } irq_restore(iflags); - if (sel == 0) - { - sel = &root_task; - turn++; - } + sel = &root_task; task_union *prev = current; task_union *next = sel; diff --git a/kernel/setup.c b/kernel/setup.c index 04a0bf3..562feba 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -34,7 +34,7 @@ extern void reboot(); extern void cnsl_init(); extern void vga_init(); -#define HZ 10 +#define HZ 100 #define CLOCK_TICK_RATE 1193180 #define LATCH ((CLOCK_TICK_RATE + HZ / 2) / HZ) @@ -84,6 +84,8 @@ void setup_kernel() setup_tasks(); setup_irqs(); + return; + //switch_printk_screen(); setup_pci(); //switch_printk_screen(); diff --git a/kernel/system.c b/kernel/system.c index c9e9b9b..c10c86f 100644 --- a/kernel/system.c +++ b/kernel/system.c @@ -71,6 +71,9 @@ void setup_gate() set_sys_int(0x0E, TRAP_GATE, PRIVILEGE_KRNL, PageFault); set_sys_int(0x10, TRAP_GATE, PRIVILEGE_KRNL, CoprocError); + for (i = 0x11; i < 0x20; i++) + set_sys_int(i, INTR_GATE, PRIVILEGE_KRNL, no_irq_handler); + for (i = 0x20; i < 256; i++) set_sys_int(i, INTR_GATE, PRIVILEGE_KRNL, no_irq_handler); diff --git a/mm/mm.c b/mm/mm.c index 6f0adcf..115f915 100644 --- a/mm/mm.c +++ b/mm/mm.c @@ -311,6 +311,10 @@ void init_paging() unsigned long pfn = 0; pte_t *pte = 0; unsigned long pgtb_addr = 0; + + // 在multiboot.S是已经初始化了BOOT_INIT_PAGETBL_CNT个页 + // 这里接着初始化剩余的页 + // 最大限制内存1G for (pfn = pa2pfn(BOOT_INIT_PAGETBL_CNT << 22); pfn < bootmem_data.max_pfn; ++pfn) { unsigned long ti = pfn % PAGE_PTE_CNT; -- 2.44.0