]> Zhao Yanbai Git Server - kernel.git/commitdiff
fix root_task stack problem
authorAceVest <zhaoyanbai@126.com>
Sun, 4 May 2014 15:35:09 +0000 (23:35 +0800)
committerAceVest <zhaoyanbai@126.com>
Sun, 4 May 2014 15:35:09 +0000 (23:35 +0800)
boot/multiboot.S
include/sched.h
include/task.h
kernel/fork.c
kernel/init.c
kernel/interrupts.S
kernel/sched.c

index 4db6d524c58d59d53ed4cc31154e3f987df0c613..50a1fd8d2fbe933f4526bfe7d71d3c0844f2e5ce 100644 (file)
@@ -16,6 +16,7 @@
 #define ASM
 #include "boot/boot.h"
 #include "system.h"
+#include "task.h"
 .global kernel_entry
 .extern CheckKernel
 .extern SetupKernel
@@ -23,7 +24,8 @@
 .extern init_pgd
 .extern init_pgt
 .extern kernel_virtual_addr_start
-.extern kernel_init_stack
+.extern root_task
+.extern root_task_entry
 
 .section .multiboot_header
 .align 32
@@ -122,9 +124,18 @@ kernel_entry:
 Label:
     call    CheckKernel
     addl    $8,%esp
-    movl    $kernel_init_stack + KRNL_INIT_STACK_SIZE, %esp
+    movl    $root_task + TASK_SIZE, %esp
     call    KernelEntry
 
+    xorl    %eax, %eax
+    sti
+    pushfl
+    movw    %cs, %ax
+    pushl   %eax
+    movl    $root_task_entry, %eax
+    pushl   %eax
+    iret
+
 Die:
     jmp     Die    # Should never come to here.
 
index 1df4a71c0cbea77b1778c5738256d45043a2615b..948c507f7f2e7fcccda77f83884530e26284a0ed 100644 (file)
@@ -36,4 +36,6 @@ inline void sleep_on(pWaitQueue wq);
 
 #define TASK_CNT 64
 
+extern task_union root_task;
+
 #endif //_SCHED_H
index efbe8ec1316f207c6522d730296c2f286b5aa70e..73df36f7b1b452d09f66abaa361f8f4203684a5b 100644 (file)
  *--------------------------------------------------------------------------
  */
 
-#ifndef    _TASK_H
+#ifndef _TASK_H
 #define _TASK_H
 
+#define TASK_SIZE 4096
+
+#ifndef ASM
 #include <page.h>
 #include <list.h>
 #include <types.h>
@@ -20,8 +23,6 @@
 #include <system.h>
 #include <wait.h>
 #include <fs.h>
-#define TASK_PAGES    (2)
-#define TASK_SIZE    (TASK_PAGES<<PAGE_SHIFT)
 
 enum
 {
@@ -53,7 +54,7 @@ typedef union task_union
 
         long        tty;
 
-        ListHead     list;
+        list_head_t list;
 
         WaitQueue    wait;
 
@@ -83,5 +84,6 @@ extern    ListHead    tsk_list;
 #define add_tsk2list(tsk)    list_add_tail((&(tsk)->list), &tsk_list)
 #define get_tsk_from_list(p)    list_entry((p), Task, list)
 #define del_tsk_from_list(tsk)    list_del((&tsk->list))
+#endif
 
 #endif //_TASK_H
index 24c722120bb58ee39366441dc510ed796a3c9be9..132c7357ffafdf7503611547f91a8dab4a0355c8 100644 (file)
@@ -30,11 +30,14 @@ int do_fork(pt_regs_t *regs, unsigned long flags)
         if(tsk->cr3 == 0)
             panic("failed init tsk cr3");
 
-        memcpy((void *)tsk->cr3, (void*)current->cr3, PAGE_SIZE);
+        task_union *t = current;
 
         unsigned int i, j;
         pde_t *pde_src = (pde_t*) current->cr3;
         pde_t *pde_dst = (pde_t*) tsk->cr3;
+
+        memcpy((void *)tsk->cr3, (void*)current->cr3, PAGE_SIZE);
+
         for(i=0; i<PAGE_PDE_CNT; ++i)
         {
             unsigned long spde = (unsigned long) pde_src[i];
@@ -75,6 +78,10 @@ int do_fork(pt_regs_t *regs, unsigned long flags)
 
     tsk->state = TASK_RUNNING;
 
+
+    INIT_LIST_HEAD(&tsk->list);
+    list_add(&tsk->list, &root_task.list);
+
     return (int)tsk->pid;
 }
 
index ba83b3087391c326938ec72197dd1c226c4feb1a..e30a43d6679a1c24c307f42c15c68ce07235c749 100644 (file)
@@ -42,6 +42,7 @@ int KernelEntry()
         pushl   %%eax;              \
         iret;"::"b"(root_task_user_space_stack+PAGE_SIZE));
 #else
+#if 0
     asm("xorl  %eax, %eax; \
         sti;\
         pushfl;  \
@@ -49,7 +50,8 @@ int KernelEntry()
         pushl   %eax;\
         leal    root_task_entry,%eax;    \
         pushl   %eax;              \
-        iret;");
+        iret;"::"b"(root_task.cr3 + TASK_SIZE));
+#endif
 #endif
 
     return 0; /* never come to here */
@@ -81,13 +83,29 @@ void root_task_entry()
 #else
 void root_task_entry()
 {
-    while(1)
+    pt_regs_t regs;
+    int pid = do_fork(regs, 0);
+
+    printk("pid is %d\n", pid);
+
+    if(pid > 0)
     {
-        asm("hlt;");
-        sysc_test();
-        //syscall0(SYSC_TEST);
+        while(1)
+        {
+            asm("hlt;");
+            sysc_test();
+            //syscall0(SYSC_TEST);
+        }
     }
-    pid_t pid;
+    else if(pid == 0)
+    {
+
+    }
+    else
+    {
+        printk("err\n");
+    }
+    //pid_t pid;
 /*
     int fd = open("/boot/grub/grub.conf", O_RDONLY);
     //int fd = open("/bin/hw", O_RDONLY);
index 13ddf46b3a5e3f53b4adcda9c82881f381de2838..60c4c8bdc348eb1637f15c012bbc639a368bb914 100644 (file)
@@ -66,6 +66,7 @@ DEF_IRQ(0,F)
 .extern irq_handler
 .extern    schedule
 _irq_handler:
+    cli #FOR TEST ONLY
     SAVE_REGS
 
     movw    %ss,%ax
@@ -77,7 +78,7 @@ _irq_handler:
     movl    %esp, %eax
     call    irq_handler
 
-    call    schedule
+    call    schedule
 
     # movl    current, %esp
         
index 2970fe72ad358a88367c785e0b0df086a9c42f05..2f73613ae2d994e933bdc44f55c96266fed084ed 100644 (file)
@@ -17,6 +17,7 @@
 #include "sched.h"
 #include "assert.h"
 #include "mm.h"
+#include "init.h"
 
 task_union root_task __attribute__((__aligned__(PAGE_SIZE)));
 
@@ -47,18 +48,21 @@ 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()
 {
     int i;
 
     root_task.pid    = get_next_pid();
     root_task.ppid    = 0;
+    INIT_LIST_HEAD(&root_task.list);
 
     for(i=0; i<NR_OPENS; i++)
         root_task.fps[i] = 0;
 
     tss.esp0        = ((unsigned long)&root_task) + sizeof(root_task);
     root_task.esp0  = tss.esp0;
+    root_task.cr3   = (unsigned long)init_pgd;
 
     printk("init_root_task tss.esp0 %08x\n", tss.esp0);
 
@@ -96,7 +100,7 @@ void    setup_tasks()
 
     init_root_tsk();
 
-    kmem_cache_t *task_union_cache = kmem_cache_create("task_union", sizeof(task_union), PAGE_SIZE);
+    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");
 
@@ -165,14 +169,14 @@ inline void context_switch(task_union * prev, task_union * next)
     //asm("xchg %bx, %bx");
     asm volatile(
     "pushfl;"
-    "pushl    %%ebp;"
-    "movl    %%esp,%[prev_esp];"
-    "movl    %[next_esp],%%esp;"
-    "movl    $1f,%[prev_eip];"
-    "pushl    %[next_eip];"
+    "pushl  %%ebp;"
+    "movl   %%esp,%[prev_esp];"
+    "movl   %[next_esp],%%esp;"
+    "movl   $1f,%[prev_eip];"
+    "pushl  %[next_eip];"
     "jmp    switch_to;"
     "1:"
-    "popl    %%ebp;"
+    "popl   %%ebp;"
     "popfl;"
     :   [prev_esp] "=m"    (prev->esp),
         [prev_eip] "=m"    (prev->eip),
@@ -188,6 +192,22 @@ inline void context_switch(task_union * prev, task_union * next)
 
 unsigned long    schedule()
 {
+    static task_union *p = &root_task;
+
+    if(p == &root_task)
+        p = list_entry(root_task.list.next, task_union, list);
+    else
+        p = &root_task;
+
+    if(p == &root_task)
+        return ;
+    
+    task_union *prev, *next;
+    prev = current;
+    next = p;
+
+    context_switch(prev, next);
+
 #if 0
     task_union *    tsk, prev, next;