]> Zhao Yanbai Git Server - kernel.git/commitdiff
support to execute user program on hard disk
authorAceVest <zhaoyanbai@126.com>
Fri, 18 Jul 2014 16:28:28 +0000 (00:28 +0800)
committerAceVest <zhaoyanbai@126.com>
Fri, 18 Jul 2014 16:28:28 +0000 (00:28 +0800)
12 files changed:
bin/shell.c
include/system.h
include/task.h
kernel/exec.c
kernel/fork.c
kernel/init.c
kernel/innerint.c
kernel/sched.c
kernel/syscall.S
lib/libc.S
lib/syscall.c
scripts/copy.sh

index cde33b06113b6e8ef92c63b88eea32e3fb219e8b..770862ef7b8f83a05839646241c66f9ec8b540d0 100644 (file)
 int systest();
 int main()
 {
+
     while(1)
     {
+#if 0
+    asm("movl $11, %eax;"   \
+        "pushl $1f;"        \
+        "pushl %ecx;"       \
+        "pushl %edx;"       \
+        "pushl %ebp;"       \
+        "movl  %esp,%ebp;"  \
+        "sysenter;"         \
+        "1:");
+#endif
         systest();
         asm("nop;nop;nop;");
     }
index 7cbcdb998ccd5bafc2ac71da42f4e2adf3a96918..7adc73e388be2776cde435f437166f86f4dad56c 100644 (file)
@@ -83,11 +83,13 @@ static inline void free_phys_pages(void *p)
     free_virt_pages((void*)va2pa(p));
 }
 
-#define panic(msg, ...) do {                                    \
-    asm("cli;");                                                \
-    printk("PANIC:\" # msg # \" file:%s function:%s line:%d\n",        \
-        ##__VA_ARGS__, __FILE__, __FUNCTION__, __LINE__);  \
-    while(1);                                                   \
+#define panic(msg, ...) do {                            \
+    asm("cli;");                                        \
+    printk("PANIC:"                                     \
+    msg                                                 \
+    " file:%s function:%s line:%d\n",                   \
+    ##__VA_ARGS__, __FILE__, __FUNCTION__, __LINE__);   \
+    while(1);                                           \
 } while(0);
 
 extern char etext, edata, end;
index 13351a97a0e755c2593532c4a2d5331ca9a71f85..35030797d8cc19e12386b755b5ade25a9c8d07b7 100644 (file)
@@ -30,6 +30,7 @@ enum
     TASK_UNUSED,
     TASK_RUNNING,
     TASK_WAIT,
+    TASK_EXEC,
     TASK_EXITING
 };
 
index a24ddab2e4302fbcff09c289d78923b29df1c737..3c69cb55c47a0ced7bfd46bb50e1caea7917d982 100644 (file)
@@ -21,6 +21,8 @@
 #include <fs.h>
 #include <ext2.h>
 
+extern void *syscall_exit;
+
 int sysc_exec(const char *path, char *const argv[])
 {
     assert(argv == NULL);    // unsupport now
@@ -100,9 +102,10 @@ int sysc_exec(const char *path, char *const argv[])
     u32    *pt;
     u32    pa_exe;
     u32    npd, npt;
-    
+    int    c;
     pa_exe  = va2pa(exe);
     npd     = get_npd(ehdr->e_entry);
+    npt     = get_npt(ehdr->e_entry);
     pt      = (u32*)va2pa(alloc_one_page(0));
     if(pt == NULL)
         panic("out of memory");
@@ -111,32 +114,16 @@ int sysc_exec(const char *path, char *const argv[])
     memset(pa2va(pt), 0, PAGE_SIZE);
     pd[npd]    = (u32) pt | 7;
     pt = pa2va(pt);
-    for(i=0; i<ehdr->e_phnum; i++)
+    for(i=npt, c=0; i<1024; i++, c++)
     {
-        pElf32_Phdr phdr;
-        phdr = (pElf32_Phdr)(buf+ehdr->e_phoff+(i*ehdr->e_phentsize));
-        if(phdr->p_type != PT_LOAD)
-            continue;
-
-        u32    npt_min, npt_max;
-
-        npt_min = get_npt(phdr->p_vaddr);
-        npt_max = get_npt(phdr->p_vaddr+phdr->p_memsz);
-        //printk("npt_min:%d npt_max:%d\n", npt_min, npt_max);
-        int j;
-        for(j=npt_min; j<=npt_max; j++)
-        {
-            pt[j] = (u32)(pa_exe | 7);    // 对于.text不能这样
-            //printk("pt[j] :%08x\n", pt[j]);
-            pa_exe = PAGE_SIZE+pa_exe;
-        }
+        pt[i] = va2pa(PAGE_ALIGN((unsigned long)exe)) + c * PAGE_SIZE;
+        pt[i] |= 7;
     }
     
+    load_cr3(current);
     printk("exe : %08x cr3:%08x\n", exe, pd);
 
-    /* 准备内核栈的数据并从ret_from_fork返回 */
-    pt_regs_t *    regs    = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1;
-    extern void ret_from_fork_user();
+    pt_regs_t *regs = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1;
     memset((void*)regs, 0, sizeof(pt_regs_t));
     regs->ss    = SELECTOR_USER_DS;
     regs->ds    = SELECTOR_USER_DS;
@@ -144,15 +131,15 @@ int sysc_exec(const char *path, char *const argv[])
     regs->fs    = SELECTOR_USER_DS;
     regs->gs    = SELECTOR_USER_DS;
     regs->esp   = (KRNLADDR-4*sizeof(unsigned long));
-    regs->eflags    = 0x200;
-    regs->cs        = SELECTOR_USER_CS;
-    regs->eip       = (unsigned long)ehdr->e_entry;
-    current->esp    = (unsigned long) regs;
-    current->eip    = (unsigned long)ret_from_fork_user;
-    *((unsigned long *)regs->esp) = (unsigned long)ehdr->e_entry;
+    regs->eflags= 0x200;
+    regs->cs    = SELECTOR_USER_CS;
+    regs->eip   = (unsigned long)ehdr->e_entry;
+    regs->edx   = regs->eip;
+    regs->ecx   = (0xC0000000 - 16);
 
     kfree(buf);
 
+    asm("movl $0, %%eax; movl %%ebx,%%ebp; movl %%ebp,%%esp;jmp syscall_exit;"::"b"((unsigned long)(regs)));
 
     return 0;
 }
index c406b8066a9ed196083734431f9f0aa53116cb3e..257eb6ea72a700a719ace3a23972c164ecbb267d 100644 (file)
@@ -33,7 +33,6 @@ int do_fork(pt_regs_t *regs, unsigned long flags)
 
     {
         tsk->cr3 = (unsigned long) alloc_one_page(0);
-        printl(MPL_TEST+1, "cr3 %08x", tsk->cr3);
         if(tsk->cr3 == 0)
             panic("failed init tsk cr3");
 
index 991331a2b14d2bece66beebe710595c10260a0eb..66b6bd5fc5c6f2104ee364ee1a63b5fbaa749446 100644 (file)
@@ -28,7 +28,11 @@ extern void *ring3_stack_top;
 void user_task_entry()
 {
     printk("user_task_entry: %08x %08x\n", ring3_stack_top, &ring3_stack_top);
+#if 0
     asm("sti;sysexit;"::"d"(&ring3), "c"(&ring3_stack_top));
+#else
+    sysc_exec("/bin/shell", 0);
+#endif
 }
 
 void init_task_entry()
index 1fce729cd6443bb911d5ed29fb5d7563f187b198..67053010b37cd9ab8d57848a07040dc6ff1fef9e 100644 (file)
@@ -98,7 +98,7 @@ void doPageFault(pt_regs_t regs)
 
     if(errcode & PAGE_US)
     {
-        panic("user program try to access a page and cause a protection fault. addr %08x", addr);
+        //panic("user program try to access a page and cause a protection fault. addr %08x", addr);
     }
 
     if((errcode & PAGE_P) == 0)
index 47114763c9205ccb589cf28bcd45f45ec210e2fc..d8acd7311f92a4bf8df1b02941e275e2c5f7baea 100644 (file)
@@ -139,13 +139,18 @@ unsigned long schedule()
         p = list_entry(pos, task_union, list);
 
         if(p->state != TASK_RUNNING)
+        {
+            if(p->state == TASK_EXEC)
+                p->state = TASK_RUNNING;
             continue;
+        }
 
         if(p->weight > max_weight)
         {
             max_weight = p->weight;
             sel = p;
         }
+
         else if(p->weight == 0)
         {
             p->weight = TASK_INIT_WEIGHT;
index b3abf9fbdf0b2e7bafd0714c89cb065a7185548a..c9fb5d6290adbac0e68bf342f3b144b61d7a2cc5 100644 (file)
@@ -27,6 +27,7 @@
 .global ret_from_fork_user
 .global ret_from_fork_krnl
 .global sysexit
+.global syscall_exit
 
 syscall_entry:
     movl    (%esp),%esp
@@ -53,16 +54,15 @@ syscall_entry:
 
     call    *sysc_handler_table(,%eax,4)
 
+    leal    sysexit, %edx
+    movl    %edx, PT_REGS_EDX(%esp)
+    movl    %ebp, PT_REGS_ECX(%esp)
+
 syscall_exit:
     movl    %eax, PT_REGS_EAX(%esp)
 
     RESTORE_REGS
 
-    # addl    $20, %esp # no need now
-    
-    leal    sysexit, %edx
-    movl    %ebp, %ecx
-
     sti        /* sysenter have cleared IF, and sysexit will not set IF. */
     sysexit
 
index 90807bafa4cf00da2598149af8b5223441d1ceb1..f4e14e2c0e1668945e1d8cbe8a6fc851731da6d7 100644 (file)
@@ -9,6 +9,21 @@ _start:
     nop
     nop
 
+/*
+    movl $11, %eax;
+
+    pushl $1f;
+    pushl %ecx;
+    pushl %edx;
+    pushl %ebp;
+    movl %esp,%ebp;
+    sysenter;
+    1:
+
+loop:
+    jmp loop
+    */
+
     pushl   $0  # env
     pushl   $0  # argv
     pushl   $1  # argc
@@ -25,5 +40,5 @@ _start:
     nop
     nop
     nop
-1:
-    jmp 1b
+die:
+    jmp die 
index d781aa99132be7e79071df09e9bd930ec59f9b91..b9dc9e210e95d7b7ae47851e844f5b6bb3b4c165 100644 (file)
@@ -9,6 +9,7 @@
 
 
 #define SYSENTER_ASM            \
+        "movl $11,%%eax;"     \
         "pushl  $1f;"           \
         "pushl    %%ecx;"       \
         "pushl    %%edx;"       \
index 44f2f07cf418a1af15db82ed397b1362827f1eaa..c10d0b0681a65d13c2117e5ac263cff43d8a93a5 100755 (executable)
@@ -18,6 +18,7 @@ md5sum /mnt/boot/Kernel
 mkdir -p /mnt/bin/
 cp ./bin/shell /mnt/bin/
 cp ./bin/hello /mnt/bin/
+md5sum /mnt/bin/*
 
 umount /mnt/