changeset 360:3d7e1c9a852e

...
author anatofuz
date Fri, 26 Jun 2020 15:30:42 +0900
parents 87a28b02c88f
children 0106e700d237
files .hgignore src/gearsTools/lib/Gears/Context/Template/XV6.pm src/gearsTools/lib/Gears/Util.pm src/main.c src/proc.cbc src/syscall.cbc src/trap.c src/usr/CMakeLists.txt
diffstat 8 files changed, 92 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Jun 23 12:17:25 2020 +0900
+++ b/.hgignore	Fri Jun 26 15:30:42 2020 +0900
@@ -18,3 +18,5 @@
 .gdb_history
 
 .vscode/
+
+*.swp
--- a/src/gearsTools/lib/Gears/Context/Template/XV6.pm	Tue Jun 23 12:17:25 2020 +0900
+++ b/src/gearsTools/lib/Gears/Context/Template/XV6.pm	Fri Jun 26 15:30:42 2020 +0900
@@ -131,12 +131,20 @@
 my $str = << 'EOFEOF';
 #define NDIRECT 12 //fs.h
 
+#define NPROC 64
+
 
 struct Context {
     enum Code next;
     struct Worker* worker;
     struct TaskManager* taskManager;
     int codeNum;
+
+    struct Context* kernel_context;
+    struct proc* proc;
+
+
+
     __code (**code) (struct Context*);
     union Data **data;
     void* heapStart;
@@ -162,8 +170,16 @@
     int iterate;
     struct Iterator* iterator;
     enum Code before;
+
 };
 
+typedef struct KernelContext {
+    struct Context Context;
+    __code (*syscalls[10]) (void);
+    struct Context* proc_contexts[NPROC];
+    struct proc* now_proc;
+} KernelContext;
+
 #include "spinlock.h"
 typedef int Int;
 #ifndef USE_CUDAWorker
--- a/src/gearsTools/lib/Gears/Util.pm	Tue Jun 23 12:17:25 2020 +0900
+++ b/src/gearsTools/lib/Gears/Util.pm	Fri Jun 26 15:30:42 2020 +0900
@@ -202,7 +202,7 @@
           next;
        }
 
-       if ($line =~ /^\/\/\s*data_gear\s*"(.*)\.(?:h|dg)?"/) {
+       if ($line =~ /^\/\/\s*include\s*"(.*)\.(?:h|dg)?"/) {
           push(@{$include_pool{$1}->{$cbc_file}},$.);
           next;
        }
--- a/src/main.c	Tue Jun 23 12:17:25 2020 +0900
+++ b/src/main.c	Fri Jun 26 15:30:42 2020 +0900
@@ -7,11 +7,14 @@
 #include "memlayout.h"
 #include "mmu.h"
 
+#include "kernel.h"
+
 extern void* end;
 
 struct cpu	cpus[NCPU];
 struct cpu	*cpu;
 
+
 #define MB (1024*1024)
 
 void kmain (void)
@@ -33,7 +36,10 @@
     
     kmem_init ();
     kmem_init2(P2V(INIT_KERNMAP), P2V(PHYSTOP));
-    
+
+    kernel_context = calloc(sizeof(struct Context), 1);
+    initContext(kernel_context);
+
     trap_init ();				// vector table and stacks for models
     pic_init (P2V(VIC_BASE));	// interrupt controller
     uart_enable_rx ();			// interrupt for uart
--- a/src/proc.cbc	Tue Jun 23 12:17:25 2020 +0900
+++ b/src/proc.cbc	Fri Jun 26 15:30:42 2020 +0900
@@ -8,6 +8,8 @@
 #include "spinlock.h"
 #interface "vm.h"
 
+#include "kernel.h"
+
 #define __ncode __code
 
 //
@@ -141,6 +143,7 @@
 
     p = allocproc();
     initContext(&p->cbc_context);
+    p->cbc_context.kernel_context = kernel_context;
 
     initproc = p;
 
@@ -219,6 +222,7 @@
         return -1;
     }
     initContext(&np->cbc_context);
+    np->cbc_context.kernel_context = kernel_context;
 
     // Copy process state from p.
     if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
--- a/src/syscall.cbc	Tue Jun 23 12:17:25 2020 +0900
+++ b/src/syscall.cbc	Fri Jun 26 15:30:42 2020 +0900
@@ -7,7 +7,11 @@
 #include "arm.h"
 #include "syscall.h"
 
+#include "kernel.h"
 #define __ncode __code
+
+//include "Trapframe.h"
+
 #
 // User code makes a system call with INT T_SYSCALL. System call number
 // in r0. Arguments on the stack, from the user call to the C library
@@ -153,8 +157,12 @@
        [SYS_cbc_read]  = cbc_read, 
 };
 
+extern __code swtch(struct context **old, struct context *new);
+
+extern __code trapret(void);
+
 __ncode cbc_trap_return(){
-    return; 
+   goto trapret();
 }
 
 __ncode cbc_ret(int ret){
@@ -176,6 +184,10 @@
 	      num = 22;
     //cprintf ("syscall(%d) from %s(%d)\n", num, proc->name, proc->pid);
 
+    kernel_context->now_proc = proc;
+    proc->cbc_context.proc = proc;
+
+
     if((num >= NELEM(syscalls)) && (num <= NELEM(cbccodes)) && cbccodes[num]) {
         proc->cbc_arg.cbc_console_arg.num = num;	
         goto (cbccodes[num])(cbc_ret);
@@ -197,3 +209,32 @@
         proc->tf->r0 = -1;
     }
 }
+
+__ncode cbc_syscall(struct Trapframe* trapframe) {
+    int num = trapframe->syscall_number;
+    if (num == 5)
+        num = 22;
+
+    kernel_context->now_proc = proc;
+    proc->cbc_context.proc = proc;
+
+    if((num >= NELEM(syscalls)) && (num <= NELEM(cbccodes)) && cbccodes[num]) {
+        proc->cbc_arg.cbc_console_arg.num = num;
+        goto (cbccodes[num])(cbc_ret);
+        //goto meta(&proc->cbc_context, cbccodes[num]);
+    }
+
+    if((num > 0) && (num < NELEM(syscalls)) && syscalls[num]) {
+        int ret = syscalls[num]();
+
+        // in ARM, parameters to main (argc, argv) are passed in r0 and r1
+        // do not set the return value if it is SYS_exec (the user program
+        // anyway does not expect us to return anything).
+
+        goto cbc_ret(ret);
+
+    }
+
+    cprintf("%d %s: unknown sys call %d\n", proc->pid, proc->name, num);
+    goto cbc_ret(-1);
+}
--- a/src/trap.c	Tue Jun 23 12:17:25 2020 +0900
+++ b/src/trap.c	Fri Jun 26 15:30:42 2020 +0900
@@ -5,6 +5,25 @@
 #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)
 {
--- a/src/usr/CMakeLists.txt	Tue Jun 23 12:17:25 2020 +0900
+++ b/src/usr/CMakeLists.txt	Fri Jun 26 15:30:42 2020 +0900
@@ -74,7 +74,7 @@
 #add_library(syslib string.c)
 
 # set(USR_COMMANDS cat echo grep init kill ln ls mkdir rm sh stressfs usertests wc zombie hello)
-set(USR_COMMANDS cat echo grep init kill ln ls mkdir rm sh hello)
+set(USR_COMMANDS cat echo grep init kill ln ls mkdir rm sh hello usertests)
 
 foreach(cmd ${USR_COMMANDS}) 
     GearsCommand (TARGET _${cmd} SOURCES ${cmd}.c)