changeset 367:650fac123133

...
author anatofuz
date Fri, 03 Jul 2020 21:21:38 +0900
parents dcf47cf23ecc
children dd720f057eb3
files src/CMakeLists.txt src/gearsTools/lib/Gears/Context/Template/XV6.pm src/gearsTools/static_gen_header.pl src/kernel.h src/main.c src/proc.cbc src/proc.h src/syscall.cbc src/trap.c src/trap.cbc
diffstat 10 files changed, 195 insertions(+), 186 deletions(-) [+]
line wrap: on
line diff
--- a/src/CMakeLists.txt	Fri Jul 03 16:17:47 2020 +0900
+++ b/src/CMakeLists.txt	Fri Jul 03 21:21:38 2020 +0900
@@ -126,10 +126,11 @@
   TARGET
       kernel
   SOURCES
-	string.c arm.c asm.S bio.c buddy.c console.cbc exec.c file.cbc fs.c log.c main.c memide.c pipe.cbc proc.cbc spinlock.cbc 
-	start.c swtch.S syscall.cbc sysfile.cbc sysproc.c trap_asm.S trap.c vm.c device/picirq.c device/timer.c device/uart.c 
+	string.c arm.c asm.S bio.c buddy.c console.cbc exec.c file.cbc fs.c log.c main.c memide.c pipe.cbc proc.cbc spinlock.cbc
+	start.c swtch.S syscall.cbc sysfile.cbc sysproc.c trap_asm.S trap.cbc vm.c device/picirq.c device/timer.c device/uart.c
   SingleLinkedStack.cbc entry.S impl/vm_impl.cbc impl/vm_impl_private.cbc
-  impl/fs_impl.cbc impl/fs_impl_private.cbc impl/KernelError.cbc
+  impl/fs_impl.cbc impl/fs_impl_private.cbc impl/KernelError.cbc impl/KernelRetImpl.cbc impl/SyscallImpl.cbc
+
 )
 
 # sys_read_impl.cbc
--- a/src/gearsTools/lib/Gears/Context/Template/XV6.pm	Fri Jul 03 16:17:47 2020 +0900
+++ b/src/gearsTools/lib/Gears/Context/Template/XV6.pm	Fri Jul 03 21:21:38 2020 +0900
@@ -174,7 +174,7 @@
 };
 
 typedef struct KernelContext {
-    struct Context Context;
+    struct Context context;
     __code (*syscalls[10]) (void);
     struct Context* proc_contexts[NPROC];
     struct proc* now_proc;
--- a/src/gearsTools/static_gen_header.pl	Fri Jul 03 16:17:47 2020 +0900
+++ b/src/gearsTools/static_gen_header.pl	Fri Jul 03 21:21:38 2020 +0900
@@ -73,7 +73,7 @@
 
 sub emit_last {
   my $type = shift;
-  my $msg = "    __code next(....);\n";
+  my $msg = "  __code next(....);\n";
   $msg .= "} $type;\n";
   return $msg;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/kernel.h	Fri Jul 03 21:21:38 2020 +0900
@@ -0,0 +1,1 @@
+struct KernelContext* kernel_context;
--- a/src/main.c	Fri Jul 03 16:17:47 2020 +0900
+++ b/src/main.c	Fri Jul 03 21:21:38 2020 +0900
@@ -37,8 +37,8 @@
     kmem_init ();
     kmem_init2(P2V(INIT_KERNMAP), P2V(PHYSTOP));
 
-    kernel_context = calloc(sizeof(struct Context), 1);
-    initContext(kernel_context);
+    kernel_context = calloc(sizeof(struct KernelContext), 1);
+    initContext(&kernel_context->context);
 
     trap_init ();				// vector table and stacks for models
     pic_init (P2V(VIC_BASE));	// interrupt controller
--- a/src/proc.cbc	Fri Jul 03 16:17:47 2020 +0900
+++ b/src/proc.cbc	Fri Jul 03 21:21:38 2020 +0900
@@ -143,7 +143,7 @@
 
     p = allocproc();
     initContext(&p->cbc_context);
-    p->cbc_context.kernel_context = kernel_context;
+    p->cbc_context.kernel_context = &kernel_context->context;
 
     initproc = p;
 
@@ -222,7 +222,7 @@
         return -1;
     }
     initContext(&np->cbc_context);
-    np->cbc_context.kernel_context = kernel_context;
+    np->cbc_context.kernel_context = &kernel_context->context;
 
     // Copy process state from p.
     if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
--- a/src/proc.h	Fri Jul 03 16:17:47 2020 +0900
+++ b/src/proc.h	Fri Jul 03 21:21:38 2020 +0900
@@ -1,7 +1,11 @@
 #include "typedefData.h"
 #ifndef PROC_INCLUDE_
 #define PROC_INCLUDE_
+
+#ifndef CONTEXT_INCLUDED
+#define CONTEXT_INCLUDE  1
 #include "context.h"
+#endif
 
 
 // Per-CPU state, now we only support one CPU
--- a/src/syscall.cbc	Fri Jul 03 16:17:47 2020 +0900
+++ b/src/syscall.cbc	Fri Jul 03 21:21:38 2020 +0900
@@ -184,7 +184,8 @@
 	      num = 22;
     //cprintf ("syscall(%d) from %s(%d)\n", num, proc->name, proc->pid);
 
-    kernel_context->now_proc = proc;
+    KernelContext* kcontext = kernel_context;
+    kcontext->now_proc = proc;
     proc->cbc_context.proc = proc;
 
 
@@ -215,7 +216,8 @@
     if (num == 5)
         num = 22;
 
-    kernel_context->now_proc = proc;
+    KernelContext* kcontext = kernel_context;
+    kcontext->now_proc = proc;
     proc->cbc_context.proc = proc;
 
     if((num >= NELEM(syscalls)) && (num <= NELEM(cbccodes)) && cbccodes[num]) {
--- a/src/trap.c	Fri Jul 03 16:17:47 2020 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,175 +0,0 @@
-// The ARM UART, a memory mapped device
-#include "types.h"
-#include "defs.h"
-#include "param.h"
-#include "arm.h"
-#include "proc.h"
-
-
-#include "kernel.h"
-
-extern __code exit(void);
-
-__code cbc_swi_handler_stub(struct trapframe* r) {
-    struct Context proc_context = proc->cbc_context;
-    struct Context kernel       = kernel_context;
-    struct Syscall* syscall = Gearef(kernel, Syscall)->syscall;
-    goto cbc_swi_handler(proc_context, r, syscall);
-}
-
-__code cbc_swi_handler(struct trapframe* r, struct Syscall* syscall) {
-    if (proc->killed)
-        goto exit();
-    proc->tf = r;
-    goto syscall->perform();
-}
-
-// trap routine
-void swi_handler (struct trapframe *r)
-{
-    if (proc->killed)
-        exit();
-    proc->tf = r;
-    syscall ();
-    if (proc->killed)
-        exit();
-}
-
-// trap routine
-void irq_handler (struct trapframe *r)
-{
-    // proc points to the current process. If the kernel is
-    // running scheduler, proc is NULL.
-    if (proc != NULL) {
-        proc->tf = r;
-    }
-
-    pic_dispatch (r);
-}
-
-// trap routine
-void reset_handler (struct trapframe *r)
-{
-    cli();
-    cprintf ("reset at: 0x%x \n", r->pc);
-}
-
-// trap routine
-void und_handler (struct trapframe *r)
-{
-    cli();
-    cprintf ("und at: 0x%x \n", r->pc);
-}
-
-// trap routine
-void dabort_handler (struct trapframe *r)
-{
-    uint dfs, fa;
-
-    cli();
-
-    // read data fault status register
-    asm("MRC p15, 0, %[r], c5, c0, 0": [r]"=r" (dfs)::);
-
-    // read the fault address register
-    asm("MRC p15, 0, %[r], c6, c0, 0": [r]"=r" (fa)::);
-    
-    cprintf ("data abort: instruction 0x%x, fault addr 0x%x, reason 0x%x \n",
-             r->pc, fa, dfs);
-    
-    dump_trapframe (r);
-}
-
-// trap routine
-void iabort_handler (struct trapframe *r)
-{
-    uint ifs;
-    
-    // read fault status register
-    asm("MRC p15, 0, %[r], c5, c0, 0": [r]"=r" (ifs)::);
-
-    cli();
-    cprintf ("prefetch abort at: 0x%x (reason: 0x%x)\n", r->pc, ifs);
-    dump_trapframe (r);
-}
-
-// trap routine
-void na_handler (struct trapframe *r)
-{
-    cli();
-    cprintf ("n/a at: 0x%x \n", r->pc);
-}
-
-// trap routine
-void fiq_handler (struct trapframe *r)
-{
-    cli();
-    cprintf ("fiq at: 0x%x \n", r->pc);
-}
-
-// low-level init code: in real hardware, lower memory is usually mapped
-// to flash during startup, we need to remap it to SDRAM
-void trap_init ( )
-{
-    volatile uint32 *ram_start;
-    char *stk;
-    int i;
-    uint modes[] = {FIQ_MODE, IRQ_MODE, ABT_MODE, UND_MODE};
-
-    // the opcode of PC relative load (to PC) instruction LDR pc, [pc,...]
-    static uint32 const LDR_PCPC = 0xE59FF000U;
-
-    // create the excpetion vectors
-    ram_start = (uint32*)VEC_TBL;
-
-    ram_start[0] = LDR_PCPC | 0x18; // Reset (SVC)
-    ram_start[1] = LDR_PCPC | 0x18; // Undefine Instruction (UND)
-    ram_start[2] = LDR_PCPC | 0x18; // Software interrupt (SVC)
-    ram_start[3] = LDR_PCPC | 0x18; // Prefetch abort (ABT)
-    ram_start[4] = LDR_PCPC | 0x18; // Data abort (ABT)
-    ram_start[5] = LDR_PCPC | 0x18; // Not assigned (-)
-    ram_start[6] = LDR_PCPC | 0x18; // IRQ (IRQ)
-    ram_start[7] = LDR_PCPC | 0x18; // FIQ (FIQ)
-
-    ram_start[8]  = (uint32)trap_reset;
-    ram_start[9]  = (uint32)trap_und;
-    ram_start[10] = (uint32)trap_swi;
-    ram_start[11] = (uint32)trap_iabort;
-    ram_start[12] = (uint32)trap_dabort;
-    ram_start[13] = (uint32)trap_na;
-    ram_start[14] = (uint32)trap_irq;
-    ram_start[15] = (uint32)trap_fiq;
-
-    // initialize the stacks for different mode
-    for (i = 0; i < sizeof(modes)/sizeof(uint); i++) {
-        stk = alloc_page ();
-
-        if (stk == NULL) {
-            panic("failed to alloc memory for irq stack");
-        }
-
-        set_stk (modes[i], (uint)stk);
-    }
-}
-
-void dump_trapframe (struct trapframe *tf)
-{
-    cprintf ("r14_svc: 0x%x\n", tf->r14_svc);
-    cprintf ("   spsr: 0x%x\n", tf->spsr);
-    cprintf ("     r0: 0x%x\n", tf->r0);
-    cprintf ("     r1: 0x%x\n", tf->r1);
-    cprintf ("     r2: 0x%x\n", tf->r2);
-    cprintf ("     r3: 0x%x\n", tf->r3);
-    cprintf ("     r4: 0x%x\n", tf->r4);
-    cprintf ("     r5: 0x%x\n", tf->r5);
-    cprintf ("     r6: 0x%x\n", tf->r6);
-    cprintf ("     r7: 0x%x\n", tf->r7);
-    cprintf ("     r8: 0x%x\n", tf->r8);
-    cprintf ("     r9: 0x%x\n", tf->r9);
-    cprintf ("    r10: 0x%x\n", tf->r10);
-    cprintf ("    r11: 0x%x\n", tf->r11);
-    cprintf ("    r12: 0x%x\n", tf->r12);
-    cprintf ("     pc: 0x%x\n", tf->pc);
-}
-void raise()
-{}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/trap.cbc	Fri Jul 03 21:21:38 2020 +0900
@@ -0,0 +1,176 @@
+// The ARM UART, a memory mapped device
+#include "types.h"
+#include "defs.h"
+#include "param.h"
+#include "arm.h"
+#include "proc.h"
+
+
+#include "kernel.h"
+
+#define __ncode __code
+
+extern __code exit(void);
+
+
+__ncode cbc_swi_handler(struct trapframe* r) {
+    struct Context* kernel       = &kernel_context->context;
+    struct Syscall* syscall      = Gearef(kernel, Syscall)->syscall;
+
+    if (proc->killed) {
+        struct KernelRet* kernelret = Gearef(kernel, KernelRet)->kernel_ret;
+        goto meta(kernel, kernelret->exit);
+    }
+    proc->tf = r;
+    goto meta(kernel, syscall->dispatch);
+}
+
+// trap routine
+void swi_handler (struct trapframe *r)
+{
+    if (proc->killed)
+        exit();
+    proc->tf = r;
+    syscall ();
+    if (proc->killed)
+        exit();
+}
+
+// trap routine
+void irq_handler (struct trapframe *r)
+{
+    // proc points to the current process. If the kernel is
+    // running scheduler, proc is NULL.
+    if (proc != NULL) {
+        proc->tf = r;
+    }
+
+    pic_dispatch (r);
+}
+
+// trap routine
+void reset_handler (struct trapframe *r)
+{
+    cli();
+    cprintf ("reset at: 0x%x \n", r->pc);
+}
+
+// trap routine
+void und_handler (struct trapframe *r)
+{
+    cli();
+    cprintf ("und at: 0x%x \n", r->pc);
+}
+
+// trap routine
+void dabort_handler (struct trapframe *r)
+{
+    uint dfs, fa;
+
+    cli();
+
+    // read data fault status register
+    asm("MRC p15, 0, %[r], c5, c0, 0": [r]"=r" (dfs)::);
+
+    // read the fault address register
+    asm("MRC p15, 0, %[r], c6, c0, 0": [r]"=r" (fa)::);
+    
+    cprintf ("data abort: instruction 0x%x, fault addr 0x%x, reason 0x%x \n",
+             r->pc, fa, dfs);
+    
+    dump_trapframe (r);
+}
+
+// trap routine
+void iabort_handler (struct trapframe *r)
+{
+    uint ifs;
+    
+    // read fault status register
+    asm("MRC p15, 0, %[r], c5, c0, 0": [r]"=r" (ifs)::);
+
+    cli();
+    cprintf ("prefetch abort at: 0x%x (reason: 0x%x)\n", r->pc, ifs);
+    dump_trapframe (r);
+}
+
+// trap routine
+void na_handler (struct trapframe *r)
+{
+    cli();
+    cprintf ("n/a at: 0x%x \n", r->pc);
+}
+
+// trap routine
+void fiq_handler (struct trapframe *r)
+{
+    cli();
+    cprintf ("fiq at: 0x%x \n", r->pc);
+}
+
+// low-level init code: in real hardware, lower memory is usually mapped
+// to flash during startup, we need to remap it to SDRAM
+void trap_init ( )
+{
+    volatile uint32 *ram_start;
+    char *stk;
+    int i;
+    uint modes[] = {FIQ_MODE, IRQ_MODE, ABT_MODE, UND_MODE};
+
+    // the opcode of PC relative load (to PC) instruction LDR pc, [pc,...]
+    static uint32 const LDR_PCPC = 0xE59FF000U;
+
+    // create the excpetion vectors
+    ram_start = (uint32*)VEC_TBL;
+
+    ram_start[0] = LDR_PCPC | 0x18; // Reset (SVC)
+    ram_start[1] = LDR_PCPC | 0x18; // Undefine Instruction (UND)
+    ram_start[2] = LDR_PCPC | 0x18; // Software interrupt (SVC)
+    ram_start[3] = LDR_PCPC | 0x18; // Prefetch abort (ABT)
+    ram_start[4] = LDR_PCPC | 0x18; // Data abort (ABT)
+    ram_start[5] = LDR_PCPC | 0x18; // Not assigned (-)
+    ram_start[6] = LDR_PCPC | 0x18; // IRQ (IRQ)
+    ram_start[7] = LDR_PCPC | 0x18; // FIQ (FIQ)
+
+    ram_start[8]  = (uint32)trap_reset;
+    ram_start[9]  = (uint32)trap_und;
+    ram_start[10] = (uint32)trap_swi;
+    ram_start[11] = (uint32)trap_iabort;
+    ram_start[12] = (uint32)trap_dabort;
+    ram_start[13] = (uint32)trap_na;
+    ram_start[14] = (uint32)trap_irq;
+    ram_start[15] = (uint32)trap_fiq;
+
+    // initialize the stacks for different mode
+    for (i = 0; i < sizeof(modes)/sizeof(uint); i++) {
+        stk = alloc_page ();
+
+        if (stk == NULL) {
+            panic("failed to alloc memory for irq stack");
+        }
+
+        set_stk (modes[i], (uint)stk);
+    }
+}
+
+void dump_trapframe (struct trapframe *tf)
+{
+    cprintf ("r14_svc: 0x%x\n", tf->r14_svc);
+    cprintf ("   spsr: 0x%x\n", tf->spsr);
+    cprintf ("     r0: 0x%x\n", tf->r0);
+    cprintf ("     r1: 0x%x\n", tf->r1);
+    cprintf ("     r2: 0x%x\n", tf->r2);
+    cprintf ("     r3: 0x%x\n", tf->r3);
+    cprintf ("     r4: 0x%x\n", tf->r4);
+    cprintf ("     r5: 0x%x\n", tf->r5);
+    cprintf ("     r6: 0x%x\n", tf->r6);
+    cprintf ("     r7: 0x%x\n", tf->r7);
+    cprintf ("     r8: 0x%x\n", tf->r8);
+    cprintf ("     r9: 0x%x\n", tf->r9);
+    cprintf ("    r10: 0x%x\n", tf->r10);
+    cprintf ("    r11: 0x%x\n", tf->r11);
+    cprintf ("    r12: 0x%x\n", tf->r12);
+    cprintf ("     pc: 0x%x\n", tf->pc);
+}
+void raise()
+{}