]> Zhao Yanbai Git Server - kernel.git/commitdiff
support new simple syscall
authorAceVest <zhaoyanbai@126.com>
Thu, 1 May 2014 15:13:24 +0000 (23:13 +0800)
committerAceVest <zhaoyanbai@126.com>
Thu, 1 May 2014 15:13:24 +0000 (23:13 +0800)
12 files changed:
.bochsrc
include/task.h
kernel/exit.c
kernel/init.c
kernel/sched.c
kernel/syscall.S
kernel/syscall.c
kernel/test.c
mm/mm.c
scripts/link.ld
setup/setup.c
setup/system.c

index 22d3edc3e3abac702858a05ed67f9d7f1ef75a92..61c5227c417781f060e5165cada8e16a0e71cf1a 100644 (file)
--- 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
index 7724ab197d0dcfa85af9c111b44fbeda3543e383..29ba18fd4e09fa8f6afcef01d8325c9acf147180 100644 (file)
@@ -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)
index 05835bde91f02a2dc495f3b1256139a36be6ea9d..791a63382d3b8f50d87bdb8d1bae6a68bae9edd7 100644 (file)
@@ -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;
index 29f50e9a313d8c59f88d542549554cb6824efb78..f452e81151e8bfe9f44d5a0210b948cc051a1427 100644 (file)
@@ -9,17 +9,17 @@
 #include <irq.h>
 #include <fcntl.h>
 #include <stat.h>
+#include <init.h>
 
 #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;
 /*
index f60ef1758843ff6ca19f8ef428bbe47e4189f885..18891ebc43324c814a0c6e0c692e70d4d38902d5 100644 (file)
 #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; i<NR_OPENS; i++)
-        root_task->fps[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)
index a2c1ee2311bd40dac18ca982d0cdf2f75414d660..da10d88fb716aa25e3cb8d41089b77ffe035c1bb 100644 (file)
 .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的地址(&current) */
     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;
index 08054dd3964cb99fb2e838604b6ce71ead02d02c..85d4657f8b78a6d83ced1e8db219c984b66b7cd7 100644 (file)
@@ -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, &current,           0);
+    wrmsr(MSR_SYSENTER_ESP, &(tss.esp0),        0);
 
     init_sysc_handler_table();
 }
index 060389978a2ffde20e1a7ecb14e53e1406f09e09..45f467f8920dfc0d239ec6e256f07a4cbf21292e 100644 (file)
@@ -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 5c9fa1fb7fa7cc3d7d8b225fc477e77b23d6d879..8521bb8b26c5b2139fbcbda50beecd7fe96146d8 100644 (file)
--- 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; i<PDECNT_PER_PAGE; ++i)
     {
         init_pgd[i] = init_pgd[i-delta];
-        init_pgd[i] |= PAGE_US;
     }
 
     // paging for user space
index a6abddad125bcbeeb3fb7517a3a0b9f07fb8ce02..a1bbc7c0fa06a5d83b9f09d4f7557161c2f64765 100644 (file)
@@ -28,7 +28,8 @@ SECTIONS
         phys_addr = . - kernel_virtual_addr_start;
         *(.multiboot_header)
         *(.text)
-        *(.sysexit)
+
+        *(.sysexit) /* last */
     }
     etext = .;
     .data : AT(phys_addr) ALIGN(0x1000) 
index 5edbc17fecf771cb5df425cb3873dcb8250dee75..099109f230c43b2dfd66edf1308cfa1dc9916269 100644 (file)
@@ -58,10 +58,11 @@ void setup_kernel()
 
     setup_irqs();
 
+    setup_tasks();
+
     return;
     while(1); // TODO MODIFY CODE BELOW
 
-    setup_tasks();
 
     setup_root_dev();
     setup_hd();
index 5cd44be3c10258050a30affc362d100bb4f5ee13..62ebba0f958973820c87aaab433a338e82daba82 100644 (file)
@@ -162,13 +162,12 @@ void    setup_irqs()
 
 }
 
-
 void    set_tss()
 {
     pTSS p = &tss;
     memset((void *)p, sizeof(TSS), 0);
-    p->esp0        = 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;