trap.c (1177B)
1 #include <errno.h> 2 #include <setjmp.h> 3 #include <stdint.h> 4 5 #include <libk.h> 6 #include <os9/os9.h> 7 8 #include "ec.h" 9 #include "arch.h" 10 11 static void 12 syscall(Context *ctx) 13 { 14 int nsys, r; 15 struct syscall *sys; 16 uintmax_t arg1, arg2, arg3, arg4; 17 18 nsys = ctx->r[X0]; 19 if (nsys < 0 || nsys >= NR_SYSCALLS) { 20 kprint("trap: %llu: invalid syscall %d number\n", 21 ctx->r[X29], nsys); 22 seterror(EINVAL); 23 return; 24 } 25 26 nsys = ctx->r[X0]; 27 arg1 = ctx->r[X1]; 28 arg2 = ctx->r[X2]; 29 arg3 = ctx->r[X3]; 30 arg4 = ctx->r[X4]; 31 32 sys = &systab[nsys]; 33 if (!sys->fn) 34 panic("null syscall entry"); 35 r = (*sys->fn)(sys->nargs, arg1, arg2, arg3, arg4); 36 seterror(r); 37 } 38 39 void 40 trap(Context *ctx) 41 { 42 enum ecvals ec; 43 const char *msg; 44 int numsvc; 45 46 ec = (ctx->r[ESR] >> 26) & 0x3f; 47 48 if (ec != SVC || inlowmem) { 49 msg = (ec < NR_EC_VALS) ? ecstr[ec] : "unknown reason"; 50 if (inlowmem) 51 msg = PMEM(msg); 52 fault(msg, ctx); 53 } 54 55 numsvc = ctx->r[ESR] & 0xff; 56 if (numsvc == 0) { 57 syscall(ctx); 58 } else { 59 kprint("trap: %llu: invalid svc %d number\n", 60 ctx->r[X29], numsvc); 61 seterror(EINVAL); 62 seterror(-1); 63 } 64 65 ctx->r[X0] = proc->ret; 66 ctx->r[X1] = proc->errno; 67 68 swtch(ctx); 69 }