From: AceVest Date: Tue, 6 May 2014 15:08:35 +0000 (+0800) Subject: support create kernel process X-Git-Tag: 0.3.0~68 X-Git-Url: http://zhaoyanbai.com/repos/nsupdate.html?a=commitdiff_plain;h=540ce480a9af24ad49642480643c719ed48f1a0b;p=kernel.git support create kernel process --- diff --git a/.gitignore b/.gitignore index c946c9e..8953fc4 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ *.diff bin/hw bin/sh +a.* diff --git a/boot/multiboot.S b/boot/multiboot.S index 50a1fd8..7037f8d 100644 --- a/boot/multiboot.S +++ b/boot/multiboot.S @@ -20,7 +20,7 @@ .global kernel_entry .extern CheckKernel .extern SetupKernel -.extern KernelEntry +.extern setup_kernel .extern init_pgd .extern init_pgt .extern kernel_virtual_addr_start @@ -125,16 +125,26 @@ Label: call CheckKernel addl $8,%esp movl $root_task + TASK_SIZE, %esp - call KernelEntry + call setup_kernel - xorl %eax, %eax - sti - pushfl - movw %cs, %ax + +#if 0 + movl $0x23, %eax + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + movw %ax, %gs pushl %eax + pushl $KRNLADDR + pushl $0x282 + pushl $0x1B movl $root_task_entry, %eax pushl %eax iret +#else + movl $root_task_entry, %eax + jmpl *%eax +#endif Die: jmp Die # Should never come to here. diff --git a/include/sched.h b/include/sched.h index 948c507..618f499 100644 --- a/include/sched.h +++ b/include/sched.h @@ -38,4 +38,6 @@ inline void sleep_on(pWaitQueue wq); extern task_union root_task; +#define FORK_KRNL 1 + #endif //_SCHED_H diff --git a/include/system.h b/include/system.h index 333b2eb..f0f66e9 100644 --- a/include/system.h +++ b/include/system.h @@ -124,7 +124,7 @@ typedef struct pt_regs u32 eflags; u32 _esp; u16 ss, _ss; -} pt_regs_t; +} __attribute__((packed)) pt_regs_t; typedef unsigned long Dev, *pDev; diff --git a/kernel/exec.c b/kernel/exec.c index 0d4369c..294e544 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -163,7 +163,7 @@ int sysc_exec(const char *path, char *const argv[]) /* 准备内核栈的数据并从ret_from_fork返回 */ pt_regs_t * regs = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1; - extern void ret_from_fork(); + extern void ret_from_fork_user(); memset((void*)regs, 0, sizeof(pt_regs_t)); regs->ss = SELECTOR_USER_DS; regs->ds = SELECTOR_USER_DS; @@ -175,7 +175,7 @@ int sysc_exec(const char *path, char *const argv[]) regs->cs = SELECTOR_USER_CS; regs->eip = (unsigned long)ehdr->e_entry; current->esp = (unsigned long) regs; - current->eip = (unsigned long)ret_from_fork; + current->eip = (unsigned long)ret_from_fork_user; #if 0 /* 写完之后发现貌似不用 */ /* 准备用户栈数据 */ diff --git a/kernel/fork.c b/kernel/fork.c index 132c735..06f72f4 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -17,11 +17,14 @@ int sysc_fork(pt_regs_t regs) return do_fork(®s, 0); } -extern void ret_from_fork(); +extern void ret_from_fork_user(); +extern void ret_from_fork_krnl(); + int do_fork(pt_regs_t *regs, unsigned long flags) { task_union *tsk; tsk = alloc_task_union(); + printk("another task %08x flags %08x\n", tsk, flags); if(tsk == NULL) panic("can not malloc PCB"); @@ -70,83 +73,26 @@ int do_fork(pt_regs_t *regs, unsigned long flags) *child_regs = *regs; child_regs->eax = 0; + child_regs->eflags |= 0x200; //enable IF + - regs->eax = 0x00; tsk->esp0 = TASK_SIZE + (unsigned long) tsk; tsk->esp = (unsigned long) child_regs; - tsk->eip = (unsigned long) ret_from_fork; - - tsk->state = TASK_RUNNING; - - - INIT_LIST_HEAD(&tsk->list); - list_add(&tsk->list, &root_task.list); + tsk->eip = (unsigned long) ret_from_fork_user; - return (int)tsk->pid; -} - -#if 0 - init_tsk_cr3(tsk); - - int i, j; - u32 *p_pd = (u32 *) current->cr3; // parent's page dir - u32 p_pde; // parent's page dir entry - u32 *p_pt = NULL; - u32 *c_pd = (u32 *) tsk->cr3; - u32 c_pde; - u32 *c_pt = NULL; - - p_pd = pa2va(p_pd); - c_pd = pa2va(c_pd); - - for(i = (KRNLADDR>>22); i>=0; i--) + if(flags & FORK_KRNL) { - p_pde = p_pd[i] & 0xFFFFF000; - if(p_pde == 0) - continue; - - //printk("i:%d p_pde:%08x ", i, p_pde); - - // 分配页表 - c_pde = (u32) get_phys_pages(1); - - //printk("c_pde:%08x ", c_pde); - - c_pt = pa2va(c_pde); - p_pt = pa2va(p_pde); - - //printk("c_pt:%08x p_pt:%08x\n", c_pt, p_pt); - - for(j=0; j>PAGE_SHIFT].count++; - } - - c_pd[i] = c_pde | 7; + printk("--dsfsdafdsaf----\n"); + tsk->eip= (unsigned long) ret_from_fork_krnl; } + printk("tsk %08x child_regs esp %08x esp0 %08x\n", tsk, tsk->esp, tsk->esp0); - load_cr3(current); - - //pPtRegs regs = ((pPtRegs)(TASK_SIZE+(unsigned long) current))-1; - //tsk->regs = *regs; - //tsk->regs.eax = 0x00; - //tsk->regs.eflags |= 0x200; //enable IF - //TODO pPtRegs regs = ((pPtRegs)(TASK_SIZE+(unsigned long) tsk))-1; - extern void ret_from_fork(); - regs->eax = 0x00; - tsk->esp0 = TASK_SIZE + (unsigned long) tsk; - tsk->esp = (unsigned long) regs; - tsk->eip = (unsigned long) ret_from_fork; - + tsk->state = TASK_RUNNING; - //printk("FORK:%08x\n", tsk); - tsk->state = TASK_RUNNING; + INIT_LIST_HEAD(&tsk->list); + list_add(&tsk->list, &root_task.list); return (int)tsk->pid; } -#endif diff --git a/kernel/init.c b/kernel/init.c index e30a43d..2a8c294 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -13,7 +13,6 @@ void root_task_entry(); -void setup_kernel(); TSS tss; System system; @@ -22,199 +21,37 @@ Desc gdt[NGDT]; char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); -int KernelEntry() +void init_task_entry() { - setup_kernel(); - -#if 0 - char *root_task_user_space_stack = (char *) alloc_pages(0, 0); - - asm("movl $0x23,%%eax; \ - movw %%ax,%%ds; \ - movw %%ax,%%es; \ - movw %%ax,%%fs; \ - movw %%ax,%%gs; \ - pushl $0x23; \ - pushl %%ebx; \ - pushl $0x282; \ - pushl $0x1B; \ - leal root_task_entry,%%eax; \ - pushl %%eax; \ - iret;"::"b"(root_task_user_space_stack+PAGE_SIZE)); -#else -#if 0 - asm("xorl %eax, %eax; \ - sti;\ - pushfl; \ - movw %cs, %ax; \ - pushl %eax;\ - leal root_task_entry,%eax; \ - pushl %eax; \ - iret;"::"b"(root_task.cr3 + TASK_SIZE)); -#endif -#endif - - return 0; /* never come to here */ -} - -#if 0 -void root_task_entry() -{ - pid_t pid; - pid = fork(); - if(pid < 0) - { - printf("failed create child\n"); - while(1); - } - else if(pid == 0) - { - execv("/bin/sh", NULL); - while(1); - } - else + printk("hahahha %s\n", __func__); + while(1) { - while(1) - { - - } + printk("i "); + asm("sti;hlt;"); } } -#else -void root_task_entry() -{ - pt_regs_t regs; - int pid = do_fork(regs, 0); - printk("pid is %d\n", pid); - - if(pid > 0) - { - while(1) - { - asm("hlt;"); - sysc_test(); - //syscall0(SYSC_TEST); - } - } - else if(pid == 0) - { +void root_task_entry() +{ +#if 0 + while(1) { + asm("sti;hlt;"); } - else - { - printk("err\n"); - } - //pid_t pid; -/* - int fd = open("/boot/grub/grub.conf", O_RDONLY); - //int fd = open("/bin/hw", O_RDONLY); - printf("FD: %d\n", fd); - char buf[1024]; - int filesz, i; - Stat stat; - fstat(fd, &stat); - filesz = stat.st_size; - printf("file size: %d bytes\n", filesz); - read(fd, buf, filesz); - for(i=0; icr3); + u32 *pde = (u32 *)current->cr3; u32 *pte; - void *page = get_phys_pages(1); + void *page = (void *)alloc_one_page(0); if(page == NULL) panic("failed alloc page"); @@ -42,8 +41,7 @@ void do_no_page(void *addr) if((pde[npde] & 0xFFFFF000)== 0) { - //printk("*a*\n"); - pte = (u32 *) pa2va(get_phys_pages(1)); + pte = (u32 *) alloc_one_page(0); memset((void*)pte, 0, PAGE_SIZE); if(pte == NULL) panic("failed alloc pte"); @@ -53,42 +51,11 @@ void do_no_page(void *addr) } else { - //printk("*b* : %08x\n", page); - //printk("*b* : %08x %08x\n", pde[npde], page); pte = (u32*)(pde[npde] & 0xFFFFF000); pte = pa2va(pte); pte[npte] = (u32) page | 7; } load_cr3(current); -#if 0 - u32 *pde = (u32*)pa2va(current->cr3); - u32 *pte; - void *page = (void*)va2pa(kmalloc_old(PAGE_SIZE)); - if(page == NULL) - panic("failed alloc page"); - - int npde = ((u32)addr)>>22; - int npte = (((u32)addr)>>12) & 0x3FF; - - if(pde[npde] == 0) - { - printk("*a*"); - pte = (u32 *) kmalloc_old(PAGE_SIZE); - memset((void*)pte, 0, PAGE_SIZE); - if(pte == NULL) - panic("failed alloc pte"); - - pte[npte] = (u32) page | 7; - pde[npde] = va2pa(pte) | 7; - } - else - { - printk("*b*"); - pte = pde[npde] & 0xFFFFF000; - pte = pa2va(pte); - pte[npte] = (u32) page | 7; - } -#endif } diff --git a/setup/setup.c b/setup/setup.c index 099109f..6cfa18f 100644 --- a/setup/setup.c +++ b/setup/setup.c @@ -54,7 +54,7 @@ void setup_kernel() set_tss(); setup_sysc(); - setup_pci(); + //setup_pci(); setup_irqs();