From: AceVest Date: Tue, 6 May 2014 15:40:56 +0000 (+0800) Subject: clean code X-Git-Tag: 0.3.0~67 X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=e4fa6b2f67b1b215c3f749b54a3e37e86f6f0166;p=kernel.git clean code --- diff --git a/boot/boot.c b/boot/boot.c index eed5e5f..59d4a26 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -17,10 +17,6 @@ #include #include -extern void parse_cmdline(const char *cmdline); -void init_free_area(u32 base, u32 len); - - struct boot_params boot_params __attribute__((aligned(32))); void init_boot_params(multiboot_info_t *p) @@ -46,7 +42,7 @@ void init_boot_params(multiboot_info_t *p) } } -void CheckKernel(unsigned long addr, unsigned long magic) +void check_kernel(unsigned long addr, unsigned long magic) { if(magic != MULTIBOOT_BOOTLOADER_MAGIC) { @@ -63,5 +59,4 @@ void CheckKernel(unsigned long addr, unsigned long magic) } init_boot_params(mbi); - } diff --git a/boot/multiboot.S b/boot/multiboot.S index 7037f8d..dcce87b 100644 --- a/boot/multiboot.S +++ b/boot/multiboot.S @@ -18,8 +18,7 @@ #include "system.h" #include "task.h" .global kernel_entry -.extern CheckKernel -.extern SetupKernel +.extern check_kernel .extern setup_kernel .extern init_pgd .extern init_pgt @@ -77,7 +76,7 @@ kernel_entry: # Init Page Directory movl %ebx,%edi movl $init_pgt-KRNLADDR,%eax - addl $7,%eax + addl $3,%eax movl $BOOT_INIT_PAGETBL_CNT,%ecx 1: stosl @@ -90,7 +89,7 @@ kernel_entry: movl %eax,%edi movl $init_pgt-KRNLADDR,%eax - addl $7,%eax + addl $3,%eax movl $BOOT_INIT_PAGETBL_CNT,%ecx 2: stosl @@ -101,7 +100,7 @@ kernel_entry: movl $init_pgt-KRNLADDR,%ebx movl %ebx,%edi movl $(BOOT_INIT_PAGETBL_CNT*1024),%ecx - movl $7,%eax + movl $3,%eax cld 3: stosl @@ -122,7 +121,7 @@ kernel_entry: ljmp $0x08,$Label Label: - call CheckKernel + call check_kernel addl $8,%esp movl $root_task + TASK_SIZE, %esp call setup_kernel diff --git a/kernel/sched.c b/kernel/sched.c index caaad4c..3afe834 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -21,22 +21,24 @@ task_union root_task __attribute__((__aligned__(PAGE_SIZE))); -pid_t get_next_pid() +pid_t get_next_pid() { static pid_t g_pid = ROOT_TSK_PID; - return g_pid++; + + pid_t pid = g_pid; + + g_pid += 123; + + return pid; } -inline void load_cr3(task_union * tsk) +inline void load_cr3(task_union * tsk) { - //printk("tsk %08x cr3: %08x\n",tsk, tsk->cr3); - // asm("movl %%eax,%%cr3;"::"a"(tsk->cr3)); - //int j=10000; while(j--); LOAD_CR3(tsk->cr3); } -void init_tsk_cr3(task_union * tsk) +void init_tsk_cr3(task_union * tsk) { tsk->cr3 = (unsigned long) pa2va(get_phys_pages(1)); @@ -48,8 +50,8 @@ 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() +extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE))); +void init_root_tsk() { int i; @@ -65,37 +67,11 @@ void init_root_tsk() root_task.cr3 = (unsigned long)init_pgd; printk("init_root_task tss.esp0 %08x\n", tss.esp0); - - //init_tsk_cr3(root_task); - //load_cr3(root_task); - - //current = &root_task; -/* - // 栈 - void *stack = kmalloc_old(PAGE_SIZE); - if(stack == NULL) - panic("stack"); - stack = va2pa(stack); - - printk("Stack : %08x\n", stack); - - u32 *pde = pa2va(current->cr3); - u32 *pte = (u32 *)kmalloc_old(PAGE_SIZE); - if(pte == NULL) - panic("root task stack pte"); - pte[1023] = stack + 7; - printk("pte: %08x\n", pte); - pde[(KRNLADDR>>22)-1] = va2pa(pte) + 7; - - - printk("CR3:%08x\n", current->cr3); - asm("movl %%eax,%%cr3;"::"a"(current->cr3)); -*/ } kmem_cache_t *task_union_cache; -void setup_tasks() +void setup_tasks() { init_root_tsk(); @@ -103,11 +79,6 @@ void setup_tasks() task_union_cache = kmem_cache_create("task_union", sizeof(task_union), PAGE_SIZE); if(0 == task_union_cache) panic("setup tasks failed. out of memory"); - -#if 0 - add_task(test_taskB); - add_task(test_taskA); -#endif } task_union *alloc_task_union() @@ -127,26 +98,6 @@ task_union *get_unused_task_pcb() inline task_union * get_next_tsk() { -#if 0 - static unsigned int inx = 0; - unsigned int i = 0; - task_union *tsk = root_task; - - for(i=0; istate == TASK_RUNNING) - { - tsk = p; - break; - } - } - - return tsk; -#endif return 0; } @@ -157,16 +108,13 @@ inline void set_esp0(task_union * tsk) inline void switch_to() { - //printk("current:%08x esp0:%08x\n", current, current->esp0); load_cr3(current); set_esp0(current); } inline void context_switch(task_union * prev, task_union * next) { - //task_union * last; unsigned long eax, ebx, ecx, edx, esi, edi; - //asm("xchg %bx, %bx"); asm volatile( "pushfl;" "pushl %%ebp;" @@ -199,35 +147,11 @@ unsigned long schedule() else p = &root_task; - asm("nop;nop;nop;nop;"); - printk("="); task_union *prev, *next; prev = current; next = p; - printk("%08x ", next); - context_switch(prev, next); - -#if 0 - task_union * tsk, prev, next; - - cli(); // For Safe. - tsk = current; - do - { - tsk = get_next_tsk(tsk); - }while(tsk->state == TASK_EXITING); /* 简单实现 */ - - if(current == tsk) - return; - - //tsk = current; - //printk("tsk:%08x\t", tsk); - //current = tsk; - prev = current; - current = next = tsk; context_switch(prev, next); -#endif } diff --git a/kernel/syscall.S b/kernel/syscall.S index 8098b27..d0b0c90 100644 --- a/kernel/syscall.S +++ b/kernel/syscall.S @@ -80,6 +80,7 @@ ret_from_fork_user: ret_from_fork_krnl: movl EDX(%esp), %edx call *%edx + #call do_exit diff --git a/mm/kmalloc.c b/mm/kmalloc.c index b6f7b9a..4de3411 100644 --- a/mm/kmalloc.c +++ b/mm/kmalloc.c @@ -36,7 +36,7 @@ void *kmalloc_old(size_t size) assert(0>PAGE_SHIFT); //printk("%08x\n", page->mapNR); - old_free_pages(page); + //old_free_pages(page); } diff --git a/mm/mm.c b/mm/mm.c index 0525017..537d854 100644 --- a/mm/mm.c +++ b/mm/mm.c @@ -324,11 +324,11 @@ void init_paging() memset((void *)pgtb_addr, 0, PAGE_SIZE); - init_pgd[get_npd(page_addr)] = (pde_t)(pgtb_addr | PAGE_P | PAGE_WR | PAGE_US); + init_pgd[get_npd(page_addr)] = (pde_t)(pgtb_addr | PAGE_P | PAGE_WR); } pte = ((pte_t *) pa2va(pgtb_addr)) + ti; - *pte = (pte_t) (page_addr | PAGE_P | PAGE_WR | PAGE_US); + *pte = (pte_t) (page_addr | PAGE_P | PAGE_WR); } @@ -341,7 +341,7 @@ void init_paging() } // paging for user space - // set_page_shared(sysexit); + set_page_shared(sysexit); LOAD_CR3(init_pgd); } diff --git a/mm/page.c b/mm/page.c index cea5a9a..caf7514 100644 --- a/mm/page.c +++ b/mm/page.c @@ -11,50 +11,40 @@ */ #include -#include #include #include #include #include -#include #include - - -#define get_page_from_list(pList) list_entry(pList, Page, list) -#define add_page2list(page, order) \ - list_add(&page->list, &freeArea[order].freeList) - -FreeArea freeArea[MAX_OLD_ORDER]; - -#if 1 void do_no_page(void *addr) { - u32 *pde = (u32 *)current->cr3; - u32 *pte; - void *page = (void *)alloc_one_page(0); - if(page == NULL) + pde_t *pde = (pde_t *)current->cr3; + pte_t *pte; + unsigned long page = alloc_one_page(0); + if(page == 0) panic("failed alloc page"); - int npde = ((u32)addr)>>22; - int npte = (((u32)addr)>>12) & 0x3FF; + int npde = get_npd(addr); + int npte = get_npt(addr); - if((pde[npde] & 0xFFFFF000)== 0) + if(PAGE_ALIGN(pde[npde]) == 0) { - pte = (u32 *) alloc_one_page(0); + pte = (pte_t*) alloc_one_page(0); memset((void*)pte, 0, PAGE_SIZE); if(pte == NULL) panic("failed alloc pte"); - pte[npte] = (u32) page | 7; - pde[npde] = va2pa(pte) | 7; + pte[npte] = (pte_t) page | PAGE_P | PAGE_WR | PAGE_US; + pde[npde] = va2pa(pte) | PAGE_P | PAGE_WR | PAGE_US; } else { - pte = (u32*)(pde[npde] & 0xFFFFF000); + pte = (pte_t*)(PAGE_ALIGN(pde[npde])); pte = pa2va(pte); - pte[npte] = (u32) page | 7; + pte[npte] = (u32) page | PAGE_P | PAGE_WR | PAGE_US; } + load_cr3(current); } @@ -101,244 +91,3 @@ void do_wp_page(void *addr) load_cr3(current); } - -inline pPage __old_alloc_pages(unsigned int order, unsigned int alloc_order) -{ - assert(0 <= order && orderorder = alloc_order; - page->count = 0; - assert((page->mapNR%(1UL<<(order))) == 0); - return page; - } - - assert(order >= 1); - - // 把这个页分成两半挂到小一级的队列上 - add_page2list(page, order-1); -#if 0 - if(page->mapNR == 0) - { - printk("[>1<%08x %d %d]\n", page, order, alloc_order); - while(1); - } -#endif - assert((page->mapNR%(1UL<<(order-1))) == 0); - page += (1 << (order-1)); - assert((page->mapNR%(1UL<<(order-1))) == 0); -#if 0 - if(page->mapNR == 0) - { - printk("[>2<%08x %d %d]\n", page, order, alloc_order); - while(1); - } -#endif - add_page2list(page, order-1); - - //int j=2000000;while(j--); - return __old_alloc_pages(order-1, alloc_order); -} - -pPage old_alloc_pages(unsigned int order) -{ - if(order<0 || order>=MAX_OLD_ORDER) - return NULL; - - int i; - for(i=order; imapNR, order, 1UL<mapNR%(1UL<mapNR; - - pListHead pos, tmp; - pPage p; - - nr += ((nr+off)/div == nr/div) ? off : -off; - - list_for_each_safe(pos, tmp, &freeArea[order].freeList) - { - p = get_page_from_list(pos); - if(p->mapNR == nr) - { - list_del_init(&p->list); - p->order = order; - return p; - } - } - - - return NULL; -} - -void old_free_pages(pPage page) -{ - assert(page != NULL); - unsigned int order = page->order; - assert(0<=order && ordermapNR, page->order); - assert((page->mapNR % (1UL<", bpage->mapNR, bpage->order); - - - if(bpage == NULL || order == MAX_OLD_ORDER-1) - { - add_page2list(page, order); - } - else - { - //list_del_init(&bpage->list); - page = (page->mapNR > bpage->mapNR) ? bpage : page; - page->order += 1; - assert((page->mapNR%(1UL<order)) == 0); -#if 0 - if((page->mapNR%(1UL<order)) != 0) - { - printk("*%d %d*", page->mapNR, 1UL<order); - panic("bug"); - } -#endif - old_free_pages(page); - } -} - -#else - -inline pPage __old_alloc_pages(unsigned int order, unsigned int alloc_order) -{ - assert(0 <= order && ordermapNR>>(order+1), (unsigned long *)freeArea[order].map); - - if(order == alloc_order) - { - return pg; - } - - // 把这个页分成两半挂到小一级的队列上 - assert(order >= 1); - list_add(&pg->list, &freeArea[order-1].freeList); - pg += (1 << (order-1)); - list_add(&pg->list, &freeArea[order-1].freeList); - - return __old_alloc_pages(order-1, alloc_order); -} - -pPage old_alloc_pages(unsigned int order) -{ - if(order >= MAX_OLD_ORDER) - return NULL; - - int i; - for(i=order; imapNR>>(order+1); - //printk("#########%d %d\n",page->mapNR, variable_test_bit( nr, (unsigned long *)freeArea[order].map)); - if(order == MAX_OLD_ORDER -1 - || !variable_test_bit( nr, (unsigned long *)freeArea[order].map)) - { - change_bit( nr, (unsigned long *)freeArea[order].map); - list_add(&page->list, &freeArea[order].freeList); - } - else - { - - pListHead pos, tmp; - - int offset = 1<mapNR; - printk("------------%d %d ", offset, nr); - nr += (((nr+offset)>>(order)) == (nr>>(order))) - ? offset : -offset; - printk("%d\n", nr); - list_for_each_safe(pos, tmp, &freeArea[order].freeList) - { - printk("[%d] ",list_entry(pos, Page, list)->mapNR); - if(list_entry(pos, Page, list)->mapNR == nr) - break; - } - - assert(pos != &freeArea[order].freeList); - list_del_init(pos); - - nr = page->mapNR>>(order+2); - change_bit( nr, (unsigned long *)freeArea[order+1].map); - pPage bpage = get_page_from_list(pos); - page = (page->mapNR > bpage->mapNR)? bpage: page; - list_add(&page->list, &freeArea[order+1].freeList); - } -} -#endif - -void disp_free_area() -{ - int i; - for(i=0; imapNR); - //printk("%08x %08x %08x %08x\n",pos, pos->prev, tmp, - // &(freeArea[i].freeList)); - } - printk("[%d %d]",i, count); - } - printk("\n"); - int j=100000; - //while(j--); -}