From d626b7a392aca74c5327daf493d5183a24571106 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Sun, 3 Nov 2024 23:47:22 +0000 Subject: [PATCH] Implement basic syscall_handler using the lookup table, w/ E --- src/userprog/syscall.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 9164142..5e53cd3 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -17,6 +17,8 @@ typedef uintptr_t (*syscall_function) (uintptr_t, uintptr_t, uintptr_t); static void halt (void); static void exit (int status); +static void *validate_user_pointer(void *ptr, size_t size); + /* A struct defining a pair of a syscall_function along with its arity. */ typedef struct { syscall_function function; @@ -29,6 +31,9 @@ static const syscall_arguments syscall_lookup[] = { [SYS_EXIT] = {(syscall_function) exit, 1}, }; +static const int lookup_size + = sizeof (syscall_lookup) / sizeof (syscall_arguments); + void syscall_init (void) { @@ -36,10 +41,23 @@ syscall_init (void) } static void -syscall_handler (struct intr_frame *f UNUSED) +syscall_handler (struct intr_frame *f) { - printf ("system call!\n"); - thread_exit (); + validate_user_pointer(f->esp, 1); + int syscall_number = *(int *) f->esp; + + if (syscall_number < 0 || syscall_number >= lookup_size) + thread_exit (); + + syscall_arguments syscall = syscall_lookup[syscall_number]; + + validate_user_pointer (f->esp, syscall.arity); + uintptr_t args[3]; + + for (int i=0; i < syscall.arity; i++) + args[i] = *(uintptr_t *) (f->esp + 1 + i); + + f->eax = syscall.function(args[0], args[1], args[2]); } static void