From: AceVest Date: Sat, 3 May 2014 03:14:23 +0000 (+0800) Subject: alloc task union X-Git-Tag: 0.3.0~71 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/mult.png?a=commitdiff_plain;h=755e7e1fab4fd701220fe8699eec240a0705bb54;p=kernel.git alloc task union --- diff --git a/drivers/hd.c b/drivers/hd.c index 41b12f8..04c47dd 100644 --- a/drivers/hd.c +++ b/drivers/hd.c @@ -13,8 +13,8 @@ #include #include #include -//void hd_handler(pPtRegs regs, unsigned int irq) -void hd_handler(unsigned int irq, pPtRegs regs, void *dev_id) +//void hd_handler(pt_regs_t * regs, unsigned int irq) +void hd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) { printk("hd_handler:%d ", irq); } diff --git a/drivers/keyboard.c b/drivers/keyboard.c index 1d85d88..6dd8850 100644 --- a/drivers/keyboard.c +++ b/drivers/keyboard.c @@ -35,7 +35,7 @@ static struct extern void reboot(); extern void poweroff(); -void kbd_handler(unsigned int irq, pPtRegs regs, void *dev_id) +void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) { unsigned char ScanCode; //printk("%s\n", dev_id); diff --git a/include/irq.h b/include/irq.h index eaf90e4..48573e9 100644 --- a/include/irq.h +++ b/include/irq.h @@ -32,8 +32,8 @@ typedef struct irq_chip typedef struct irqaction { - //void (*handler)(pPtRegs regs, unsigned int irq); - void (*handler)(unsigned int irq, pPtRegs regs, void *dev_id); + //void (*handler)(pt_regs_t * regs, unsigned int irq); + void (*handler)(unsigned int irq, pt_regs_t * regs, void *dev_id); const char *dev_name; void *dev_id; struct irqaction *next; @@ -51,8 +51,8 @@ extern irq_chip_t i8259_chip; extern irq_desc_t irq_desc[]; extern irq_desc_t no_irq_desc; int request_irq(unsigned int irq, - //void (*handler)(pPtRegs, unsigned int), - void (*handler)(unsigned int, pPtRegs, void *), + //void (*handler)(pt_regs_t *, unsigned int), + void (*handler)(unsigned int, pt_regs_t *, void *), const char *devname, void *dev_id); diff --git a/include/mm.h b/include/mm.h index 2f6a75b..ec39222 100644 --- a/include/mm.h +++ b/include/mm.h @@ -20,3 +20,6 @@ unsigned long bootmem_page_state(unsigned long pfn); #define bootmem_alloc_pages(n) alloc_bootmem((n)*PAGE_SIZE, PAGE_SIZE) + +kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t align); +void *kmem_cache_alloc(kmem_cache_t *cache, gfp_t gfpflags); diff --git a/include/page.h b/include/page.h index 308da1a..851c75d 100644 --- a/include/page.h +++ b/include/page.h @@ -148,6 +148,7 @@ struct kmem_cache }; + // TODO Remove typedef struct page_ { diff --git a/include/sched.h b/include/sched.h index eb5ac77..1df4a71 100644 --- a/include/sched.h +++ b/include/sched.h @@ -19,16 +19,16 @@ #include #define NR_TASKS 3 -//pTask tTasks[NR_TASKS]; +//task_union * tTasks[NR_TASKS]; //void add_task(); -void SetupTasks(); +//void SetupTasks(); //void test_taskA(); //void test_taskB(); -//unsigned long schedule(pPtRegs regs); +//unsigned long schedule(pt_regs_t * regs); unsigned long schedule(); pid_t get_next_pid(); -void init_tsk_cr3(pTask); +void init_tsk_cr3(task_union *); inline void wake_up(pWaitQueue wq); diff --git a/include/system.h b/include/system.h index 4cd0b33..333b2eb 100644 --- a/include/system.h +++ b/include/system.h @@ -14,7 +14,7 @@ *-------------------------------------------------------------------------- */ -#ifndef _SYSTEM_H +#ifndef _SYSTEM_H #define _SYSTEM_H #include @@ -99,18 +99,17 @@ enum GDTSelectorIndex INDEX_EMP8, INDEX_TSS, }; -// pushad push eax, ecx, edx, ebx, esp, ebp, esi, edi -typedef struct +// pushal push eax, ecx, edx, ebx, esp, ebp, esi, edi +typedef struct pt_regs { u32 edi; u32 esi; u32 ebp; - u32 _esp; + u32 esp; u32 ebx; u32 edx; u32 ecx; - u32 eax; // 因为在系统调用中用来带调用号,就无法传送参数 - // 所以把eax放在这个位置 + u32 eax; u16 ds, _ds; u16 es, _es; u16 fs, _fs; @@ -123,9 +122,9 @@ typedef struct u32 eip; u16 cs, _cs; u32 eflags; - u32 esp; + u32 _esp; u16 ss, _ss; -} PtRegs, *pPtRegs; +} pt_regs_t; typedef unsigned long Dev, *pDev; @@ -170,28 +169,28 @@ extern System system; #endif -#define SAVE_REGS \ - cld; \ - pushl %gs; \ - pushl %fs; \ - pushl %es; \ - pushl %ds; \ +#define SAVE_REGS \ + cld; \ + pushl %gs; \ + pushl %fs; \ + pushl %es; \ + pushl %ds; \ pushal; #define RESTORE_REGS \ - popal; \ - popl %ds; \ - popl %es; \ - popl %fs; \ + popal; \ + popl %ds; \ + popl %es; \ + popl %fs; \ popl %gs; -#define PRIVILEGE_KRNL 0x0 -#define PRIVILEGE_USER 0x3 +#define PRIVILEGE_KRNL 0x0 +#define PRIVILEGE_USER 0x3 -#define INDEX_UCODE 3 -#define INDEX_UDATA 4 -/* *8 == <<3 .但要用于汇编文件 <<3 不行. */ +#define INDEX_UCODE 3 +#define INDEX_UDATA 4 +/* *8 == <<3 . but <<3 is not right for asm code. */ #define SELECTOR_KRNL_CS (INDEX_KCODE*8) #define SELECTOR_KRNL_DS (INDEX_KDATA*8) #define SELECTOR_KRNL_SS SELECTOR_KRNL_DS diff --git a/include/task.h b/include/task.h index 29ba18f..405ccd1 100644 --- a/include/task.h +++ b/include/task.h @@ -36,7 +36,7 @@ typedef union task_union { struct { - PtRegs regs; + pt_regs_t regs; unsigned long esp0; /* 指示发生在用户态的中断在进入 内核态后的栈位置 */ @@ -62,15 +62,22 @@ typedef union task_union }; unsigned char stack[TASK_SIZE]; -} task_struct; +} task_union; +task_union *alloc_task_union(); -typedef task_struct Task; -typedef task_struct *pTask; -#define ROOT_TSK_PID (1) +static inline task_union *get_current() +{ + task_union *tsk; + asm("andl %%esp, %0;":"=r"(tsk):"0"(~(TASK_SIZE-1))); + return tsk; +} + +#define current get_current() + +#define ROOT_TSK_PID (0) -extern pTask current; extern ListHead tsk_list; #define add_tsk2list(tsk) list_add_tail((&(tsk)->list), &tsk_list) diff --git a/kernel/clock.c b/kernel/clock.c index 44a314c..a7d3e96 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -15,7 +15,7 @@ static unsigned int jiffies = 0; -void clk_handler(unsigned int irq, pPtRegs regs, void *dev_id) +void clk_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) { jiffies++; diff --git a/kernel/exec.c b/kernel/exec.c index a3390ba..0d4369c 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -162,9 +162,9 @@ int sysc_exec(const char *path, char *const argv[]) /* 准备内核栈的数据并从ret_from_fork返回 */ - pPtRegs regs = ((pPtRegs)(TASK_SIZE+(unsigned long)current)) - 1; + pt_regs_t * regs = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1; extern void ret_from_fork(); - memset((void*)regs, 0, sizeof(PtRegs)); + memset((void*)regs, 0, sizeof(pt_regs_t)); regs->ss = SELECTOR_USER_DS; regs->ds = SELECTOR_USER_DS; regs->es = SELECTOR_USER_DS; diff --git a/kernel/fork.c b/kernel/fork.c index 89c7a33..b2941a2 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -11,22 +11,42 @@ */ #include -#if 1 -int sysc_fork() + +int sysc_fork(pt_regs_t regs) +{ + return do_fork(®s, 0); +} + +extern void ret_from_fork(); +int do_fork(pt_regs_t *regs, unsigned long flags) { - // 先分配一个进程控制结构 - task_struct *tsk; - // TODO tsk = get_unused_task_pcb(); + task_union *tsk; + tsk = alloc_task_union(); if(tsk == NULL) panic("can not malloc PCB"); - //printk("CHILD:%08x\n", tsk); - - memcpy(tsk, current, sizeof(task_struct)); + memcpy(tsk, current, sizeof(task_union)); tsk->pid = get_next_pid(); - tsk->ppid = current->pid; + tsk->ppid = current->pid; + + pt_regs_t *child_regs = ((pt_regs_t *) (TASK_SIZE+(unsigned long) tsk)) - 1; + + *child_regs = *regs; + + child_regs->eax = 0; + + 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; + + return (int)tsk->pid; +} +#if 0 init_tsk_cr3(tsk); int i, j; @@ -76,7 +96,7 @@ int sysc_fork() //tsk->regs = *regs; //tsk->regs.eax = 0x00; //tsk->regs.eflags |= 0x200; //enable IF - pPtRegs regs = ((pPtRegs)(TASK_SIZE+(unsigned long) tsk))-1; + //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; diff --git a/kernel/i8259.c b/kernel/i8259.c index b95e449..bb9d542 100644 --- a/kernel/i8259.c +++ b/kernel/i8259.c @@ -106,7 +106,7 @@ irq_chip_t i8259_chip = .ack = mask_ack_i8259_irq, }; -void do_i8259_IRQ(pPtRegs regs, unsigned int irq) +void do_i8259_IRQ(pt_regs_t * regs, unsigned int irq) { diff --git a/kernel/init.c b/kernel/init.c index f752ff0..ba83b30 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -17,6 +17,8 @@ void setup_kernel(); TSS tss; System system; +Desc idt[NIDT]; +Desc gdt[NGDT]; char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); @@ -82,6 +84,7 @@ void root_task_entry() while(1) { asm("hlt;"); + sysc_test(); //syscall0(SYSC_TEST); } pid_t pid; diff --git a/kernel/innerint.c b/kernel/innerint.c index abd75ea..a8f5150 100644 --- a/kernel/innerint.c +++ b/kernel/innerint.c @@ -26,63 +26,63 @@ while(1); \ }while(0); -void doDivideError(PtRegs regs) +void doDivideError(pt_regs_t regs) { DIE_MSG(); } -void doDebug(PtRegs regs) +void doDebug(pt_regs_t regs) { DIE_MSG(); } -void doNMI(PtRegs regs) +void doNMI(pt_regs_t regs) { DIE_MSG(); } -void doBreakPoint(PtRegs regs) +void doBreakPoint(pt_regs_t regs) { DIE_MSG(); } -void doOverFlow(PtRegs regs) +void doOverFlow(pt_regs_t regs) { DIE_MSG(); } -void doBoundsCheck(PtRegs regs) +void doBoundsCheck(pt_regs_t regs) { DIE_MSG(); } -void doInvalidOpcode(PtRegs regs) +void doInvalidOpcode(pt_regs_t regs) { DIE_MSG(); } -void doDeviceNotAvailable(PtRegs regs) +void doDeviceNotAvailable(pt_regs_t regs) { DIE_MSG(); } -void doDoubleFault(PtRegs regs) +void doDoubleFault(pt_regs_t regs) { DIE_MSG(); } -void doCoprocSegOverRun(PtRegs regs) +void doCoprocSegOverRun(pt_regs_t regs) { DIE_MSG(); } -void doInvalidTss(PtRegs regs) +void doInvalidTss(pt_regs_t regs) { DIE_MSG(); } -void doSegNotPresent(PtRegs regs) +void doSegNotPresent(pt_regs_t regs) { DIE_MSG(); } -void doStackFault(PtRegs regs) +void doStackFault(pt_regs_t regs) { DIE_MSG(); } -void doGeneralProtection(PtRegs regs) +void doGeneralProtection(pt_regs_t regs) { DIE_MSG(); } -void doPageFault(PtRegs regs) +void doPageFault(pt_regs_t regs) { //DIE_MSG(); void *addr; @@ -112,7 +112,7 @@ void doPageFault(PtRegs regs) do_wp_page(addr); } } -void doCoprocError(PtRegs regs) +void doCoprocError(pt_regs_t regs) { DIE_MSG(); } diff --git a/kernel/irq.c b/kernel/irq.c index ee4e25a..a37b205 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -35,7 +35,7 @@ irq_desc_t no_irq_desc = .status = 0, .depth = 0 }; -__attribute__ ((regparm(1))) void irq_handler(pPtRegs regs) +__attribute__ ((regparm(1))) void irq_handler(pt_regs_t * regs) { unsigned int irq = regs->irq; @@ -53,7 +53,7 @@ __attribute__ ((regparm(1))) void irq_handler(pPtRegs regs) int request_irq( unsigned int irq, - void (*handler)(unsigned int, pPtRegs, void *), + void (*handler)(unsigned int, pt_regs_t *, void *), const char *devname, void *dev_id) { diff --git a/kernel/processor.c b/kernel/processor.c deleted file mode 100644 index e3b0738..0000000 --- a/kernel/processor.c +++ /dev/null @@ -1,12 +0,0 @@ -/* - * ------------------------------------------------------------------------ - * File Name: processor.c - * Author: Zhao Yanbai - * Thu Mar 27 23:44:12 2014 - * Description: none - * ------------------------------------------------------------------------ - */ -#include "processor.h" - -Desc idt[NIDT]; -Desc gdt[NGDT]; diff --git a/kernel/sched.c b/kernel/sched.c index 18891eb..858509a 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -16,10 +16,9 @@ #include "sched.h" #include "assert.h" +#include "mm.h" -pTask current; - -task_struct root_task __attribute__((__aligned__(PAGE_SIZE))); +task_union root_task __attribute__((__aligned__(PAGE_SIZE))); pid_t get_next_pid() { @@ -28,14 +27,14 @@ pid_t get_next_pid() return g_pid++; } -inline void load_cr3(pTask 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--); } -void init_tsk_cr3(pTask tsk) +void init_tsk_cr3(task_union * tsk) { tsk->cr3 = pa2va(get_phys_pages(1)); @@ -89,19 +88,30 @@ void init_root_tsk() */ } +kmem_cache_t *task_union_cache; + void setup_tasks() { - /* 初始化第一个特殊的进程 */ init_root_tsk(); + + kmem_cache_t *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() +{ + return (task_union *) kmem_cache_alloc(task_union_cache, 0); +} + -task_struct *get_unused_task_pcb() +task_union *get_unused_task_pcb() { unsigned int i; for(i=0; istate == TASK_RUNNING) { @@ -135,7 +145,7 @@ inline pTask get_next_tsk() return 0; } -inline void set_esp0(pTask tsk) +inline void set_esp0(task_union * tsk) { tss.esp0 = tsk->esp0; } @@ -147,9 +157,9 @@ inline void switch_to() set_esp0(current); } -inline void context_switch(pTask prev, pTask next) +inline void context_switch(task_union * prev, task_union * next) { - //pTask last; + //task_union * last; unsigned long eax, ebx, ecx, edx, esi, edi; //asm("xchg %bx, %bx"); asm volatile( @@ -178,7 +188,7 @@ inline void context_switch(pTask prev, pTask next) unsigned long schedule() { #if 0 - pTask tsk, prev, next; + task_union * tsk, prev, next; cli(); // For Safe. tsk = current; diff --git a/kernel/syscall.S b/kernel/syscall.S index 9e9bf74..7a9dc49 100644 --- a/kernel/syscall.S +++ b/kernel/syscall.S @@ -32,9 +32,16 @@ syscall_entry: movl (%esp),%esp + pushl $(SELECTOR_USER_SS) + pushl %ebp + pushfl + pushl $(SELECTOR_USER_CS) + pushl $sysexit + pushl $0 + SAVE_REGS - pushl %eax + pushl %eax movw %ss, %ax movw %ax, %ds movw %ax, %es @@ -51,6 +58,8 @@ syscall_exit: movl %eax, EAX(%esp) RESTORE_REGS + + # addl $20, %esp # no need now leal sysexit, %edx movl %ebp, %ecx diff --git a/kernel/test_task.c b/kernel/test_task.c index 2dce581..1a15a16 100644 --- a/kernel/test_task.c +++ b/kernel/test_task.c @@ -15,7 +15,7 @@ #include #include #include -pTask tTasks[NR_TASKS]; +task_union * tTasks[NR_TASKS]; #if 0 void SetuptTasks() @@ -36,7 +36,7 @@ void add_task(void *fun) { #if 0 assert(fun != NULL); - pTask tsk = NULL; + task_union * tsk = NULL; tsk = kmalloc_old(sizeof(Task)); if(tsk == NULL) panic("shit happens"); @@ -47,9 +47,9 @@ void add_task(void *fun) tsk->ppid = 0; init_tsk_cr3(tsk); - pPtRegs r; + pt_regs_t * r; r = &tsk->regs; - memset((void *)r, 0, sizeof(PtRegs)); + memset((void *)r, 0, sizeof(pt_regs_t)); r->ds = r->es = r->fs = r->gs = SELECTOR_USER_DS; r->eip = (unsigned long)fun; r->cs = SELECTOR_USER_CS; @@ -65,7 +65,7 @@ void add_task(void *fun) void add_task(void *fun) { assert(fun != NULL); - pTask tsk = NULL; + task_union * tsk = NULL; int i=0; for(i=0; iregs;//(pPtRegs)(TASK_SIZE + (unsigned long)tsk); + pt_regs_t * r; + r = &tsk->regs;//(pt_regs_t *)(TASK_SIZE + (unsigned long)tsk); //printk("Add Tsk: tsk:%08x r:%08x ", tsk, r); //r--; - //printk("r:%08x sizeof regs:%x ", r, sizeof(PtRegs)); + //printk("r:%08x sizeof regs:%x ", r, sizeof(pt_regs_t)); - memset((void *)r, 0, sizeof(PtRegs)); + memset((void *)r, 0, sizeof(pt_regs_t)); //printk("USER CS: %x\n", SELECTOR_USER_CS); //printk("USER DS: %x\n", SELECTOR_USER_DS); r->ds = r->es = r->fs = r->gs = SELECTOR_USER_DS; diff --git a/mm/slub.c b/mm/slub.c index 9d99bfa..5221ad7 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -57,15 +57,6 @@ static bool calculate_params(kmem_cache_t *cache) return true; } -kmem_cache_t *kmem_cache_create(const char *name, - size_t size, - size_t align) -{ - - - return 0; -} - static bool kmem_cache_init(kmem_cache_t *cache, const char *name, @@ -275,6 +266,19 @@ void kfree(void *addr) slub_free(cache, page, addr); } +kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t align) +{ + kmem_cache_t *cache = kmalloc(sizeof(kmem_cache_t), 0); + if(cache == 0) + return 0; + + kmem_cache_init(cache, name, size, align); + + list_add(&(cache->list), &slub_caches); + + return cache; +} + void init_slub_system() { diff --git a/setup/system.c b/setup/system.c index f42501b..072bb6a 100644 --- a/setup/system.c +++ b/setup/system.c @@ -119,11 +119,11 @@ void setup_irqs() irq_desc[i].chip = &i8259_chip; } - //extern void kbd_handler(pPtRegs, unsigned int); //extern void clk_handler(pPtRegs, unsigned int); - //extern void hd_handler(pPtRegs, unsigned int); - void kbd_handler(unsigned int irq, pPtRegs regs, void *dev_id); - void clk_handler(unsigned int irq, pPtRegs regs, void *dev_id); - void hd_handler(unsigned int irq, pPtRegs regs, void *dev_id); + //extern void kbd_handler(pt_regs_t *, unsigned int); //extern void clk_handler(pt_regs_t *, unsigned int); + //extern void hd_handler(pt_regs_t *, unsigned int); + void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id); + void clk_handler(unsigned int irq, pt_regs_t * regs, void *dev_id); + void hd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id); request_irq(0x00, clk_handler, "Intel 8254", "Clock Chip"); request_irq(0x01, kbd_handler, "Intel 8042", "PS/2 Keyboard"); request_irq(0x0E, hd_handler, "IDE", "IDE");