From: AceVest Date: Thu, 1 May 2014 15:13:24 +0000 (+0800) Subject: support new simple syscall X-Git-Tag: 0.3.0~75 X-Git-Url: http://zhaoyanbai.com/repos/%22../static/gitweb.js?a=commitdiff_plain;h=0fac9f57dd315b40d9e1c20011c82150187cc34f;p=kernel.git support new simple syscall --- diff --git a/.bochsrc b/.bochsrc index 22d3edc..61c5227 100644 --- a/.bochsrc +++ b/.bochsrc @@ -1,6 +1,6 @@ # configuration file generated by Bochs megs: 128 -plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, iodebug=1 +plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, iodebug=0 config_interface: textconfig display_library: x memory: host=128, guest=128 @@ -25,8 +25,8 @@ com4: enabled=0 cpuid: vendor_string="GenuineIntel" cpuid: brand_string=" Intel(R) Pentium(R) 4 CPU " -usb_uhci: enabled=0 -usb_ohci: enabled=0 +#usb_uhci: enabled=0 +#usb_ohci: enabled=0 print_timestamps: enabled=0 debugger_log: - magic_break: enabled=1 @@ -34,9 +34,9 @@ port_e9_hack: enabled=0 private_colormap: enabled=0 clock: sync=none, time0=local # no cmosimage -ne2k: enabled=0 -pnic: enabled=0 -sb16: enabled=0 +#ne2k: enabled=0 +#pnic: enabled=0 +#sb16: enabled=0 # no loader log: - logprefix: %t%e%d @@ -49,5 +49,5 @@ keyboard_serial_delay: 250 keyboard_paste_delay: 100000 keyboard_mapping: enabled=1, map=/usr/local/share/bochs/keymaps/x11-pc-us.map user_shortcut: keys=none -mouse: enabled=0 +#mouse: enabled=0 #mouse: enabled=0, type=ps2, toggle=ctrl+mbutton diff --git a/include/task.h b/include/task.h index 7724ab1..29ba18f 100644 --- a/include/task.h +++ b/include/task.h @@ -32,7 +32,7 @@ enum TASK_EXITING }; -typedef union +typedef union task_union { struct { @@ -62,14 +62,15 @@ typedef union }; unsigned char stack[TASK_SIZE]; -} Task, *pTask; +} task_struct; -typedef Task task_struct; + +typedef task_struct Task; +typedef task_struct *pTask; #define ROOT_TSK_PID (1) extern pTask current; -extern Task RootTsk; extern ListHead tsk_list; #define add_tsk2list(tsk) list_add_tail((&(tsk)->list), &tsk_list) diff --git a/kernel/exit.c b/kernel/exit.c index 05835bd..791a633 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -16,8 +16,8 @@ int sysc_exit(int status) { - if(current == &RootTsk) - panic("Root Task is Exiting..."); + //if(current == &RootTsk) + // panic("Root Task is Exiting..."); /* 先简要实现 */ current->state = TASK_EXITING; diff --git a/kernel/init.c b/kernel/init.c index 29f50e9..f452e81 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -9,17 +9,17 @@ #include #include #include +#include #define KRNL_STACK_SIZE 4096 -extern void root_task(); -extern void setup_kernel(); +void root_task_entry(); +void setup_kernel(); TSS tss; System system; -static char kernel_stack[KRNL_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); -static char root_task_stack[PAGE_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); +static char __initdata kernel_stack[KRNL_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); int KernelEntry() { @@ -29,6 +29,8 @@ int KernelEntry() setup_kernel(); + char *root_task_user_space_stack = (char *) alloc_pages(0, 0); + asm("movl $0x23,%%eax; \ movw %%ax,%%ds; \ movw %%ax,%%es; \ @@ -38,14 +40,14 @@ int KernelEntry() pushl %%ebx; \ pushl $0x282; \ pushl $0x1B; \ - leal root_task,%%eax; \ + leal root_task_entry,%%eax; \ pushl %%eax; \ - iret;"::"b"(root_task_stack+PAGE_SIZE)); + iret;"::"b"(root_task_user_space_stack+PAGE_SIZE)); return 0; } #if 0 -void root_task() +void root_task_entry() { pid_t pid; pid = fork(); @@ -68,11 +70,11 @@ void root_task() } } #else -void root_task() +void root_task_entry() { while(1) { - //asm("hlt;"); + syscall0(SYSC_TEST); } pid_t pid; /* diff --git a/kernel/sched.c b/kernel/sched.c index f60ef17..18891eb 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -17,15 +17,9 @@ #include "sched.h" #include "assert.h" - - pTask current; -Task RootTsk __attribute__((__aligned__(PAGE_SIZE))); -task_struct* task[TASK_CNT]; - -#define root_task task[0] -#define first_task task[0] +task_struct root_task __attribute__((__aligned__(PAGE_SIZE))); pid_t get_next_pid() { @@ -57,19 +51,21 @@ void init_root_tsk() { int i; - root_task->pid = get_next_pid(); - root_task->ppid = 0; + root_task.pid = get_next_pid(); + root_task.ppid = 0; for(i=0; ifps[i] = 0; + root_task.fps[i] = 0; + + tss.esp0 = ((unsigned long)&root_task) + sizeof(root_task); + root_task.esp0 = tss.esp0; - /* 这个时候还没有进程开始 */ - root_task->esp0 = tss.esp0; + printk("init_root_task tss.esp0 %08x\n", tss.esp0); - init_tsk_cr3(root_task); - load_cr3(root_task); + //init_tsk_cr3(root_task); + //load_cr3(root_task); - current = root_task; + //current = &root_task; /* // 栈 void *stack = kmalloc_old(PAGE_SIZE); @@ -116,6 +112,7 @@ task_struct *get_unused_task_pcb() inline pTask get_next_tsk() { +#if 0 static unsigned int inx = 0; unsigned int i = 0; task_struct *tsk = root_task; @@ -134,23 +131,24 @@ inline pTask get_next_tsk() } return tsk; +#endif + return 0; } -#if 1 -inline void set_esp0(pTask tsk) +inline void set_esp0(pTask tsk) { tss.esp0 = tsk->esp0; } + inline void switch_to() { - //printk("current:%08x esp0:%08x\n", current, current->esp0); load_cr3(current); set_esp0(current); } + inline void context_switch(pTask prev, pTask next) { -#if 1 //pTask last; unsigned long eax, ebx, ecx, edx, esi, edi; //asm("xchg %bx, %bx"); @@ -165,21 +163,21 @@ inline void context_switch(pTask prev, pTask next) "1:" "popl %%ebp;" "popfl;" - : [prev_esp] "=m" (prev->esp), + : [prev_esp] "=m" (prev->esp), [prev_eip] "=m" (prev->eip), "=a" (prev), "=b" (ebx), "=c" (ecx), "=d" (edx), "=S" (esi), "=D" (edi) - : [next_esp] "m" (next->esp), + : [next_esp] "m" (next->esp), [next_eip] "m" (next->eip), [prev] "a" (prev), [next] "d" (next) : "memory" ); -#endif } unsigned long schedule() { +#if 0 pTask tsk, prev, next; cli(); // For Safe. @@ -198,8 +196,8 @@ unsigned long schedule() prev = current; current = next = tsk; context_switch(prev, next); -} #endif +} inline void wake_up(pWaitQueue wq) diff --git a/kernel/syscall.S b/kernel/syscall.S index a2c1ee2..da10d88 100644 --- a/kernel/syscall.S +++ b/kernel/syscall.S @@ -25,29 +25,50 @@ .extern sysc_handler_table #endif .text -.global syscall_entry -.global ret_from_fork +.global syscall_entry +.global ret_from_fork +.global sysexit -/* - * 0x40 -- %ss - * 0x3C -- %esp - * 0x38 -- %eflags - * 0x34 -- %cs - * 0x30 -- %eip - * 0x2C -- sysc_nr - * 0x28 -- %gs - * 0x24 -- %fs - * 0x20 -- %es - * 0x1C -- %ds - * 0x18 -- %eax - * 0x14 -- %ebp - * 0x10 -- %esi - * 0x0C -- %edi - * 0x08 -- %ecx - * 0x04 -- %edx - * 0x00 -- %ebx - */ #if 1 +syscall_entry: + movl (%esp),%esp + + SAVE_REGS + + pushfl + pushl %ebp + pushl %eax + + movw %ss, %ax + movw %ax, %ds + movw %ax, %es + movw %ax, %gs + movw %ax, %fs + + popl %eax + + cmpl $SYSC_NUM, %eax + jae bad_syscnr + + call *sysc_handler_table(,%eax,4) + +ret_from_bad_syscnr: + popl %ebp + popfl + RESTORE_REGS + + leal sysexit, %edx + movl %ebp, %ecx + + sti /* sysenter会自动清除IF.貌似sysexit不会自动置位 */ + sysexit + +ret_from_fork: /* for compiler now */ +bad_syscnr: + call sysc_bad_syscnr + jmp ret_from_bad_syscnr + +#else syscall_entry: /* 此时%esp存的是current的地址(¤t) */ movl (%esp),%esp /* 获得current的值 */ @@ -106,7 +127,6 @@ bad_syscnr: # no need to pay attention on the return address .section .sysexit .align 0x1000 -.global sysexit sysexit: popl %ebp; popl %edx; diff --git a/kernel/syscall.c b/kernel/syscall.c index 08054dd..85d4657 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -27,7 +27,7 @@ void setup_sysc() { wrmsr(MSR_SYSENTER_CS, SELECTOR_KRNL_CS, 0); wrmsr(MSR_SYSENTER_EIP, syscall_entry, 0); - wrmsr(MSR_SYSENTER_ESP, ¤t, 0); + wrmsr(MSR_SYSENTER_ESP, &(tss.esp0), 0); init_sysc_handler_table(); } diff --git a/kernel/test.c b/kernel/test.c index 0603899..45f467f 100644 --- a/kernel/test.c +++ b/kernel/test.c @@ -33,7 +33,9 @@ void dump_fd() int sysc_test() { - dump_fd(); + //dump_fd(); + + printk("."); return 0; } diff --git a/mm/mm.c b/mm/mm.c index 5c9fa1f..8521bb8 100644 --- a/mm/mm.c +++ b/mm/mm.c @@ -290,12 +290,9 @@ find_block: } - pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE))); pte_t __initdata init_pgt[PTECNT_PER_PAGE*BOOT_INIT_PAGETBL_CNT] __attribute__((__aligned__(PAGE_SIZE))); -extern void sysexit(); - void set_page_shared(void *x) { unsigned long addr = (unsigned long) x; @@ -307,6 +304,8 @@ void set_page_shared(void *x) pte[get_npt(addr)] |= PAGE_US; } +extern void sysexit(); + void init_paging() { unsigned int i; @@ -338,7 +337,6 @@ void init_paging() for(i=delta; iesp0 = TASK_SIZE + (unsigned long)&RootTsk; - p->ss0 = SELECTOR_KRNL_DS; + p->esp0 = 0; // delay to init root_task + p->ss0 = SELECTOR_KRNL_DS; p->ss = SELECTOR_KRNL_DS; p->gs = SELECTOR_KRNL_DS; p->fs = SELECTOR_KRNL_DS;