From: AceVest Date: Sun, 4 May 2014 15:35:09 +0000 (+0800) Subject: fix root_task stack problem X-Git-Tag: 0.3.0~69 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/man.host.html?a=commitdiff_plain;h=18c567719cf46dc5d9ad27b599e5bf1133d399f8;p=kernel.git fix root_task stack problem --- diff --git a/boot/multiboot.S b/boot/multiboot.S index 4db6d52..50a1fd8 100644 --- a/boot/multiboot.S +++ b/boot/multiboot.S @@ -16,6 +16,7 @@ #define ASM #include "boot/boot.h" #include "system.h" +#include "task.h" .global kernel_entry .extern CheckKernel .extern SetupKernel @@ -23,7 +24,8 @@ .extern init_pgd .extern init_pgt .extern kernel_virtual_addr_start -.extern kernel_init_stack +.extern root_task +.extern root_task_entry .section .multiboot_header .align 32 @@ -122,9 +124,18 @@ kernel_entry: Label: call CheckKernel addl $8,%esp - movl $kernel_init_stack + KRNL_INIT_STACK_SIZE, %esp + movl $root_task + TASK_SIZE, %esp call KernelEntry + xorl %eax, %eax + sti + pushfl + movw %cs, %ax + pushl %eax + movl $root_task_entry, %eax + pushl %eax + iret + Die: jmp Die # Should never come to here. diff --git a/include/sched.h b/include/sched.h index 1df4a71..948c507 100644 --- a/include/sched.h +++ b/include/sched.h @@ -36,4 +36,6 @@ inline void sleep_on(pWaitQueue wq); #define TASK_CNT 64 +extern task_union root_task; + #endif //_SCHED_H diff --git a/include/task.h b/include/task.h index efbe8ec..73df36f 100644 --- a/include/task.h +++ b/include/task.h @@ -10,9 +10,12 @@ *-------------------------------------------------------------------------- */ -#ifndef _TASK_H +#ifndef _TASK_H #define _TASK_H +#define TASK_SIZE 4096 + +#ifndef ASM #include #include #include @@ -20,8 +23,6 @@ #include #include #include -#define TASK_PAGES (2) -#define TASK_SIZE (TASK_PAGES<list), &tsk_list) #define get_tsk_from_list(p) list_entry((p), Task, list) #define del_tsk_from_list(tsk) list_del((&tsk->list)) +#endif #endif //_TASK_H diff --git a/kernel/fork.c b/kernel/fork.c index 24c7221..132c735 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -30,11 +30,14 @@ int do_fork(pt_regs_t *regs, unsigned long flags) if(tsk->cr3 == 0) panic("failed init tsk cr3"); - memcpy((void *)tsk->cr3, (void*)current->cr3, PAGE_SIZE); + task_union *t = current; unsigned int i, j; pde_t *pde_src = (pde_t*) current->cr3; pde_t *pde_dst = (pde_t*) tsk->cr3; + + memcpy((void *)tsk->cr3, (void*)current->cr3, PAGE_SIZE); + for(i=0; istate = TASK_RUNNING; + + INIT_LIST_HEAD(&tsk->list); + list_add(&tsk->list, &root_task.list); + return (int)tsk->pid; } diff --git a/kernel/init.c b/kernel/init.c index ba83b30..e30a43d 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -42,6 +42,7 @@ int KernelEntry() pushl %%eax; \ iret;"::"b"(root_task_user_space_stack+PAGE_SIZE)); #else +#if 0 asm("xorl %eax, %eax; \ sti;\ pushfl; \ @@ -49,7 +50,8 @@ int KernelEntry() pushl %eax;\ leal root_task_entry,%eax; \ pushl %eax; \ - iret;"); + iret;"::"b"(root_task.cr3 + TASK_SIZE)); +#endif #endif return 0; /* never come to here */ @@ -81,13 +83,29 @@ void root_task_entry() #else void root_task_entry() { - while(1) + pt_regs_t regs; + int pid = do_fork(regs, 0); + + printk("pid is %d\n", pid); + + if(pid > 0) { - asm("hlt;"); - sysc_test(); - //syscall0(SYSC_TEST); + while(1) + { + asm("hlt;"); + sysc_test(); + //syscall0(SYSC_TEST); + } } - pid_t pid; + else if(pid == 0) + { + + } + else + { + printk("err\n"); + } + //pid_t pid; /* int fd = open("/boot/grub/grub.conf", O_RDONLY); //int fd = open("/bin/hw", O_RDONLY); diff --git a/kernel/interrupts.S b/kernel/interrupts.S index 13ddf46..60c4c8b 100644 --- a/kernel/interrupts.S +++ b/kernel/interrupts.S @@ -66,6 +66,7 @@ DEF_IRQ(0,F) .extern irq_handler .extern schedule _irq_handler: + cli #FOR TEST ONLY SAVE_REGS movw %ss,%ax @@ -77,7 +78,7 @@ _irq_handler: movl %esp, %eax call irq_handler - # call schedule + call schedule # movl current, %esp diff --git a/kernel/sched.c b/kernel/sched.c index 2970fe7..2f73613 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -17,6 +17,7 @@ #include "sched.h" #include "assert.h" #include "mm.h" +#include "init.h" task_union root_task __attribute__((__aligned__(PAGE_SIZE))); @@ -47,18 +48,21 @@ void init_tsk_cr3(task_union * tsk) tsk->cr3 = va2pa(tsk->cr3); } +extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE))); void init_root_tsk() { int i; root_task.pid = get_next_pid(); root_task.ppid = 0; + INIT_LIST_HEAD(&root_task.list); for(i=0; iesp), [prev_eip] "=m" (prev->eip), @@ -188,6 +192,22 @@ inline void context_switch(task_union * prev, task_union * next) unsigned long schedule() { + static task_union *p = &root_task; + + if(p == &root_task) + p = list_entry(root_task.list.next, task_union, list); + else + p = &root_task; + + if(p == &root_task) + return ; + + task_union *prev, *next; + prev = current; + next = p; + + context_switch(prev, next); + #if 0 task_union * tsk, prev, next;