From eae0475758d2945884808df951d8748f9300cbe2 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 23 Apr 2019 16:14:41 -0700 Subject: [PATCH] Seemingly implement system calls (still appears to have a bug). --- kern/syscall.c | 30 +++++++++++++++++++++++++++--- kern/trap.c | 7 +++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/kern/syscall.c b/kern/syscall.c index 414d489..59adfa7 100644 --- a/kern/syscall.c +++ b/kern/syscall.c @@ -19,8 +19,25 @@ sys_cputs(const char *s, size_t len) { // Check that the user has permission to read memory [s, s+len). // Destroy the environment if not. + const char* bottom = ROUNDDOWN(s, PGSIZE); + size_t aligned_count = ROUNDUP(len, PGSIZE) / PGSIZE; + while(aligned_count--) { + if (!(curenv->env_pgdir[PDX(bottom)] & PTE_P)) { + cprintf("[%08x] Tried to print unmapped address\n", curenv->env_id); + env_destroy(curenv); + return; + } - // LAB 3: Your code here. + pde_t pgdir_allowed = curenv->env_pgdir[PDX(bottom)] & PTE_U; + pde_t pt_allowed = *pgdir_walk(curenv->env_pgdir, bottom, 0) & PTE_U; + if(((curenv->env_tf.tf_cs & 0b11) > 1) && !(pt_allowed & pgdir_allowed)) { + cprintf("[%08x] Tried to print forbidden memory\n", curenv->env_id); + env_destroy(curenv); + return; + } + + bottom += PGSIZE; + } // Print the string supplied by the user. cprintf("%.*s", len, s); @@ -70,9 +87,16 @@ syscall(uint32_t syscallno, uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4, // Return any appropriate return value. // LAB 3: Your code here. - panic("syscall not implemented"); - switch (syscallno) { + case SYS_cputs: + sys_cputs((const char*) a1, a2); + return 0; + case SYS_cgetc: + return sys_cgetc(); + case SYS_getenvid: + return sys_getenvid(); + case SYS_env_destroy: + return sys_env_destroy(a1); default: return -E_INVAL; } diff --git a/kern/trap.c b/kern/trap.c index e7b6a1a..2a4abef 100644 --- a/kern/trap.c +++ b/kern/trap.c @@ -200,6 +200,13 @@ trap_dispatch(struct Trapframe *tf) page_fault_handler(tf); } else if (tf->tf_trapno == T_BRKPT) { monitor(tf); + } else if (tf->tf_trapno == T_SYSCALL) { + syscall(tf->tf_regs.reg_eax, + tf->tf_regs.reg_edx, + tf->tf_regs.reg_ecx, + tf->tf_regs.reg_ebx, + tf->tf_regs.reg_edi, + tf->tf_regs.reg_esi); } // Unexpected trap: The user process or the kernel has a bug.