]> Zhao Yanbai Git Server - kernel.git/commitdiff
support create kernel process
authorAceVest <zhaoyanbai@126.com>
Tue, 6 May 2014 15:08:35 +0000 (23:08 +0800)
committerAceVest <zhaoyanbai@126.com>
Tue, 6 May 2014 15:08:35 +0000 (23:08 +0800)
12 files changed:
.gitignore
boot/multiboot.S
include/sched.h
include/system.h
kernel/exec.c
kernel/fork.c
kernel/init.c
kernel/interrupts.S
kernel/sched.c
kernel/syscall.S
mm/page.c
setup/setup.c

index c946c9e24e1b8c7e4b7557959804bc0b098e476e..8953fc460677a6391736260f5a8caf38d988e3b0 100644 (file)
@@ -25,3 +25,4 @@
 *.diff
 bin/hw
 bin/sh
+a.*
index 50a1fd8d2fbe933f4526bfe7d71d3c0844f2e5ce..7037f8dc020b7ee475080bf243d7aef39ca7ea4f 100644 (file)
@@ -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.
index 948c507f7f2e7fcccda77f83884530e26284a0ed..618f499d2670977fee146bc024237ed850c7d43e 100644 (file)
@@ -38,4 +38,6 @@ inline void sleep_on(pWaitQueue wq);
 
 extern task_union root_task;
 
+#define FORK_KRNL 1
+
 #endif //_SCHED_H
index 333b2ebae1ec211c7246e957cc9719dee5871ced..f0f66e96b8962fadfa230b37533ff04effd0caf5 100644 (file)
@@ -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;
 
index 0d4369c61fba882739023628cdfcde0e0c5bcc25..294e54403f15918116a77c46a2dbeb19f9ea0b92 100644 (file)
@@ -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    /* 写完之后发现貌似不用 */
     /* 准备用户栈数据 */
index 132c7357ffafdf7503611547f91a8dab4a0355c8..06f72f4a6945fbace52fa425fef1e26bbdc3502b 100644 (file)
@@ -17,11 +17,14 @@ int sysc_fork(pt_regs_t regs)
     return do_fork(&regs, 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_ITEMS; j++)
-        {
-            p_pt[j] &= (~2UL);
-            c_pt[j] = p_pt[j];
-            /* 增加此页的共享计数 */
-            pgmap[p_pt[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
index e30a43d6679a1c24c307f42c15c68ce07235c749..2a8c29423da4a2ae4b14495eeb23a5b14a18d295 100644 (file)
@@ -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; i<filesz; i++)
-        printf("%c", buf[i]);
-    execv("/bin/hw", NULL);
-    while(0)
-    {
-        printf("r");
-        int d=100000; while(d--);
-    }
-*/
-
-
-//#define    SHOW    
-
-    pid = fork();
-#ifdef    SHOW
-    printf("PID: %d\n", pid);
-#endif
-    if(pid < 0)
-    {
-        printf("failed create child\n");
-        while(1);
-    }
-    else if(pid == 0)
-    {
-        pid_t ccid;
-        ccid = fork();
-
-#ifdef    SHOW
-        printf("CCID: %d\n", ccid);
 #endif
+    pt_regs_t regs;
+    memset((void*)&regs, 0, sizeof(regs));
+    regs.edx = (unsigned long) init_task_entry;
+    cli();
+    int pid = do_fork(&regs, FORK_KRNL);
+    sti();
 
-        if(ccid < 0)
-        {
-            printf("child faild to create child\n");
-            while(1);
-        }
-        else if(ccid == 0)
-        {
-            execv("/bin/sh", NULL);
-            while(1)
-            {
-#ifdef    SHOW
-                printf("a");
-#endif
-                delay(100);
-            }
-        }
-        else
-        {
-            while(1)
-            {
-#ifdef    SHOW
-                printf("b");
-#endif
-                delay(200);
-            }
-        }
-    }
-    else
+    printk("pid is %d\n", pid);
+    while(1)
     {
-        pid_t pcid;
-        pcid = fork();
-    
-#ifdef    SHOW
-        printf("PCID: %d\n", pcid);
-#endif
-        if(pcid < 0)
-        {
-            printf("parent faild to create child\n");
-            while(1);
-        }
-        else if(pcid == 0)
-        {
-            while(1)
-            {
-#ifdef    SHOW
-                printf("c");
-#endif
-                delay(300);
-            }
-        }
-        else
-        {
-            while(1)
-            {
-#if 0
-                int k;
-                extern    void    ParseKbdInput(int k);
-                k = read_kbd();
-                ParseKbdInput(k);
-#else
-#ifdef    SHOW
-                printf("d");
-#endif
-                delay(400);
-#endif
-            }
-        }
+        printk("r ");
+        asm("sti;hlt;");
+        //sysc_test();
+        //syscall0(SYSC_TEST);
     }
 }
-#endif
index 60c4c8bdc348eb1637f15c012bbc639a368bb914..942f8cf58bbc4795d5722b2a28318b31a8e6610b 100644 (file)
@@ -66,7 +66,6 @@ DEF_IRQ(0,F)
 .extern irq_handler
 .extern    schedule
 _irq_handler:
-    cli #FOR TEST ONLY
     SAVE_REGS
 
     movw    %ss,%ax
@@ -85,5 +84,4 @@ _irq_handler:
     RESTORE_REGS
     
     addl    $4,%esp    /* 跳过中断号. */
-
     iret
index 2f73613ae2d994e933bdc44f55c96266fed084ed..caaad4cf2264316f69b5167120f9e3fa6bf0bdf2 100644 (file)
@@ -199,13 +199,13 @@ unsigned long    schedule()
     else
         p = &root_task;
 
-    if(p == &root_task)
-        return ;
-    
+    asm("nop;nop;nop;nop;");
+    printk("=");
     task_union *prev, *next;
     prev = current;
     next = p;
 
+    printk("%08x ", next);
     context_switch(prev, next);
 
 #if 0
index 7a9dc49a36496d470fbedc24bed5aaf359e67395..8098b273f62ecbc9f9bbbd97a98000ac42704631 100644 (file)
 .code32
 .text
 .global syscall_entry
-.global ret_from_fork
+.global ret_from_fork_user
+.global ret_from_fork_krnl
 .global sysexit
 
-#define EAX 32
+#define EAX 28
+#define EDX 20
 
 syscall_entry:
     movl    (%esp),%esp
@@ -69,11 +71,17 @@ syscall_exit:
 
 bad_sysc_nr:
     call    sysc_bad_syscnr
-    jmp     sysc_exit
+    jmp     syscall_exit
 
-ret_from_fork
+ret_from_fork_user:
     xorl    %eax, %eax
-    jmp     sysc_exit
+    jmp     syscall_exit
+
+ret_from_fork_krnl:
+    movl    EDX(%esp), %edx
+    call    *%edx
+
+    
     
 
 # this routine should be load align by PAGE_SIZE
index 9fd6fb5231d52af91ebfd62b7344be95cf52127c..cea5a9a92e8608ff80c32853f59e6efcd2f5cc85 100644 (file)
--- a/mm/page.c
+++ b/mm/page.c
@@ -30,10 +30,9 @@ FreeArea freeArea[MAX_OLD_ORDER];
 #if 1
 void    do_no_page(void *addr)
 {
-    //printk("%s ", __FUNCTION__);
-    u32    *pde = (u32*)pa2va(current->cr3);
+    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
 }
 
 
index 099109f230c43b2dfd66edf1308cfa1dc9916269..6cfa18f50f79ed6f753302a519e242096ab9759c 100644 (file)
@@ -54,7 +54,7 @@ void setup_kernel()
     set_tss();
 
     setup_sysc();
-    setup_pci();
+    //setup_pci();
 
     setup_irqs();