diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 04635159d30520fa11296dc922e622f2ad2553d5..d400fafebd809ed5ccef266951eaaf1a133163d7 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -1,10 +1,24 @@ #include "userprog/syscall.h" #include <stdio.h> #include <syscall-nr.h> +#include "devices/shutdown.h" +#include "threads/synch.h" #include "threads/interrupt.h" #include "threads/thread.h" +#include "userprog/process.h" +#include "filesys/filesys.h" +typedef int pid_p; +static struct lock f_lock; static void syscall_handler (struct intr_frame *); +static uint32_t load_stack(struct intr_frame *f, int offset); + +/*Function for system call*/ +void syscall_halt(void); +void syscall_exit(int); +int syscall_wait (pid_p pid); +bool syscall_create (const char *file, unsigned initial_size); +/*End of system call function*/ void syscall_init (void) @@ -12,10 +26,71 @@ syscall_init (void) intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall"); } +static uint32_t load_stack(struct intr_frame *f, int offset) +{ + return *((uint32_t*)(f->esp + offset)); +} + static void syscall_handler (struct intr_frame *f UNUSED) { - printf ("system call!\n"); - thread_exit (); + int code = (int)load_stack(f, 0); + lock_init(&f_lock); + switch (code) + { + case SYS_HALT: + { + syscall_halt(); + break; + } + case SYS_EXIT: + { + syscall_exit((int)load_stack(f,4)); + break; + } + + case SYS_WAIT: + { + f->eax = syscall_wait((int)load_stack(f,4)); + break; + } + case SYS_CREATE: + { + f->eax = syscall_create((const char*)load_stack(f,4), (unsigned)load_stack(f,8)); + break; + } + + default: + { + printf ("system call (%d) not implemented!\n", code); + thread_exit (); + } + } } + +void syscall_halt (void) +{ + shutdown_power_off(); +} + +void syscall_exit (int status) +{ + struct thread *cur = thread_current (); + cur->exit_status = status; + thread_exit(); +} + +int syscall_wait (pid_p pid) +{ + return process_wait (pid); +} + +bool syscall_create (const char *file, unsigned initial_size) +{ + lock_acquire(&f_lock); + bool success = filesys_create(file, initial_size); + lock_release(&f_lock); + return success; +} +