changeset 368:dd720f057eb3

starting sh
author anatofuz
date Sun, 05 Jul 2020 14:02:31 +0900
parents 650fac123133
children d67449aae7cd
files src/CMakeLists.txt src/impl/KernelRetImpl.cbc src/impl/KernelRetImpl.h src/impl/SyscallImpl.cbc src/impl/SyscallImpl.h src/interface/KernelRet.h src/interface/Syscall.h src/trap.cbc src/trap_asm.S
diffstat 9 files changed, 191 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/CMakeLists.txt	Fri Jul 03 21:21:38 2020 +0900
+++ b/src/CMakeLists.txt	Sun Jul 05 14:02:31 2020 +0900
@@ -129,7 +129,7 @@
 	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/KernelRetImpl.cbc impl/SyscallImpl.cbc
+  impl/fs_impl.cbc impl/fs_impl_private.cbc impl/KernelRetImpl.cbc impl/KernelError.cbc impl/SyscallImpl.cbc
 
 )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/impl/KernelRetImpl.cbc	Sun Jul 05 14:02:31 2020 +0900
@@ -0,0 +1,47 @@
+#include "types.h"
+#include "arm.h"
+#include "param.h"
+#include "syscall.h"
+#include "proc.h"
+#interface "KernelRet.h"
+
+// ----
+// typedef struct KernelRetImpl <Type, Isa> impl KernelRet {
+//   __code next(....);
+// } KernelRetImpl;
+// ----
+
+KernelRet* createKernelRetImpl(struct Context* cbc_context) {
+    struct KernelRet* kernelRet  = new KernelRet();
+    struct KernelRetImpl* kernelRet_impl = new KernelRetImpl();
+    kernelRet->kernelRet = (union Data*)kernelRet_impl;
+    kernelRet->cbc_return = C_cbc_returnKernelRetImpl;
+    kernelRet->exit = C_exitKernelRetImpl;
+    kernelRet->swtch = C_swtchKernelRetImpl;
+    return kernelRet;
+}
+
+extern __code trapret(void);
+
+__code cbc_returnKernelRetImpl(struct KernelRetImpl* kernelRet, int ret, __code next(...)) {
+    int num = proc->cbc_arg.cbc_console_arg.num;
+    if (num != SYS_exec) {
+        proc->tf->r0 = ret;
+    }
+    goto trapret();
+}
+
+extern __code exit(void);
+
+
+__code exitKernelRetImpl(struct KernelRetImpl* kernelRet, __code next(...)) {
+    goto exit(); 
+}
+
+extern __code swtch(void);
+
+__code swtchKernelRetImpl(struct KernelRetImpl* kernelRet, __code next(...)) {
+    goto swtch();
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/impl/KernelRetImpl.h	Sun Jul 05 14:02:31 2020 +0900
@@ -0,0 +1,3 @@
+typedef struct KernelRetImpl <Type, Isa> impl KernelRet {
+  __code next(....);
+} KernelRetImpl;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/impl/SyscallImpl.cbc	Sun Jul 05 14:02:31 2020 +0900
@@ -0,0 +1,121 @@
+#include "types.h"
+#include "defs.h"
+#include "param.h"
+#include "memlayout.h"
+#include "mmu.h"
+#include "proc.h"
+#include "arm.h"
+#include "syscall.h"
+
+#include "../../kernel.h"
+#interface "Syscall.h"
+#interface "KernelRet.h"
+
+
+extern int sys_chdir(void);
+extern int sys_close(void);
+extern int sys_dup(void);
+extern int sys_exec(void);
+extern int sys_exit(void);
+extern int sys_fork(void);
+extern int sys_fstat(void);
+extern int sys_getpid(void);
+extern int sys_kill(void);
+extern int sys_link(void);
+extern int sys_mkdir(void);
+extern int sys_mknod(void);
+extern int sys_open(void);
+extern int sys_pipe(void);
+extern int sys_read(void);
+extern int sys_sbrk(void);
+extern int sys_sleep(void);
+extern int sys_unlink(void);
+extern int sys_wait(void);
+extern int sys_write(void);
+extern int sys_uptime(void);
+
+extern __code cbc_read(__code(*)(int));
+
+
+static int (*syscalls[])(void) = {
+        [SYS_fork]    =sys_fork,
+        [SYS_exit]    =sys_exit,
+        [SYS_wait]    =sys_wait,
+        [SYS_pipe]    =sys_pipe,
+        [SYS_read]    =sys_read,
+        [SYS_kill]    =sys_kill,
+        [SYS_exec]    =sys_exec,
+        [SYS_fstat]   =sys_fstat,
+        [SYS_chdir]   =sys_chdir,
+        [SYS_dup]     =sys_dup,
+        [SYS_getpid]  =sys_getpid,
+        [SYS_sbrk]    =sys_sbrk,
+        [SYS_sleep]   =sys_sleep,
+        [SYS_uptime]  =sys_uptime,
+        [SYS_open]    =sys_open,
+        [SYS_write]   =sys_write,
+        [SYS_mknod]   =sys_mknod,
+        [SYS_unlink]  =sys_unlink,
+        [SYS_link]    =sys_link,
+        [SYS_mkdir]   =sys_mkdir,
+        [SYS_close]   =sys_close,
+};
+
+
+
+static __code (*cbccodes[])(__code (*)(int)) = {
+       [SYS_cbc_read]  = cbc_read,
+};
+
+
+
+// ----
+// typedef struct SyscallImpl <Type, Isa> impl Syscall {
+//   __code next(....);
+// } SyscallImpl;
+// ----
+
+Syscall* createSyscallImpl(struct Context* cbc_context) {
+    struct Syscall* syscall  = new Syscall();
+    struct SyscallImpl* syscall_impl = new SyscallImpl();
+    syscall->syscall = (union Data*)syscall_impl;
+    syscall->dispatch = C_dispatchSyscallImpl;
+    return syscall;
+}
+
+__code dispatchSyscallImpl(struct SyscallImpl* syscall, __code next(...)) {
+    KernelContext* kcontext = kernel_context;
+    kcontext->now_proc = proc;
+    proc->cbc_context.proc = proc;
+
+    int num = proc->tf->r0;
+
+    if (num == 5)
+        num = 22;
+
+
+    if((num >= NELEM(syscalls)) && (num <= NELEM(cbccodes)) && cbccodes[num]) {
+        proc->cbc_arg.cbc_console_arg.num = num;
+        __code *syscalls = kcontext->syscalls;
+
+        goto (cbccodes[num])(cbc_ret);
+        //goto meta(&proc->cbc_context, cbccodes[num]);
+    }
+
+    struct KernelRet* kernelRet = createKernelRetImpl(cbc_context);
+
+    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 kernelRet->cbc_return(ret);
+
+    }
+
+    cprintf("%d %s: unknown sys call %d\n", proc->pid, proc->name, num);
+    goto kernelRet->cbc_return(-1);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/impl/SyscallImpl.h	Sun Jul 05 14:02:31 2020 +0900
@@ -0,0 +1,3 @@
+typedef struct SyscallImpl <Type, Isa> impl Syscall {
+  __code next(....);
+} SyscallImpl;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/interface/KernelRet.h	Sun Jul 05 14:02:31 2020 +0900
@@ -0,0 +1,6 @@
+typedef struct KernelRet <Type, Impl> {
+  __code cbc_return(Impl* kernelRet, int ret, __code next(...));
+  __code exit(Impl* kernelRet, __code next(...));
+  __code swtch(Impl* kernelRet, __code next(...));
+  __code next(....);
+} KernelRet;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/interface/Syscall.h	Sun Jul 05 14:02:31 2020 +0900
@@ -0,0 +1,4 @@
+typedef struct Syscall <Type, Impl> {
+    __code dispatch(Impl* syscall, __code next(...));
+    __code next(....);
+} Syscall;
--- a/src/trap.cbc	Fri Jul 03 21:21:38 2020 +0900
+++ b/src/trap.cbc	Sun Jul 05 14:02:31 2020 +0900
@@ -7,18 +7,21 @@
 
 
 #include "kernel.h"
+#interface "Syscall.h"
+#interface "KernelRet.h"
 
 #define __ncode __code
 
 extern __code exit(void);
 
+extern Syscall* createSyscallImpl(struct Context*);
 
 __ncode cbc_swi_handler(struct trapframe* r) {
     struct Context* kernel       = &kernel_context->context;
-    struct Syscall* syscall      = Gearef(kernel, Syscall)->syscall;
+    struct Syscall* syscall      = createSyscallImpl(kernel);
 
     if (proc->killed) {
-        struct KernelRet* kernelret = Gearef(kernel, KernelRet)->kernel_ret;
+        struct KernelRet* kernelret = createKernelRetImpl(kernel);
         goto meta(kernel, kernelret->exit);
     }
     proc->tf = r;
--- a/src/trap_asm.S	Fri Jul 03 21:21:38 2020 +0900
+++ b/src/trap_asm.S	Sun Jul 05 14:02:31 2020 +0900
@@ -27,7 +27,7 @@
 
     # call traps (trapframe *fp)
     MOV     r0, r13             // copy r13_svc to r0
-    BL      swi_handler         // branch to the isr_swi
+    BL      cbc_swi_handler         // branch to the isr_swi
 
     # restore states
 trapret: