diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index d400fafebd809ed5ccef266951eaaf1a133163d7..24d52f093e92506926f57b12dbcdb961507179f3 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -1,96 +1,119 @@ #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*/ - +bool isValid (void * vaddr); void -syscall_init (void) +syscall_init (void) { - intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall"); -} - -static uint32_t load_stack(struct intr_frame *f, int offset) +// initialize lock for any function that accesses/uses any file +lock_init(&mainLock); +intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall"); +} s +tatic void +syscall_handler (struct intr_frame *f) { - return *((uint32_t*)(f->esp + offset)); -} - - -static void -syscall_handler (struct intr_frame *f UNUSED) +// Extrating Stack pointer +int *stack_ptr = f->esp; +/* If stack pointer is not valid, kill */ +if(!isValid (stack_ptr)) +exit(-1); +int syscall_number = *stack_ptr; +switch (syscall_number) { - 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) +case SYS_HALT: +halt(); +break; +case SYS_EXIT: +if(!isValid(stack_ptr+1)) +exit(-1); +exit(*(stack_ptr+1)); +break; +case SYS_CREATE: +if(!isValid(stack_ptr+4) || !isValid(stack_ptr+5)) +exit(-1); +lock_acquire(&mainLock); +f- +>eax = create ((void*)(*((int*)f->esp + 4)), *((unsigned*)((int*)f->esp + 5))); +lock_release(&mainLock); +break; +case SYS_REMOVE: +if(!isValid(stack_ptr+1)) +exit(-1); +lock_acquire(&mainLock); +f- +>eax = remove((const char *)*(stack_ptr+1)); +lock_release(&mainLock); +break; +case SYS_READ: +if (!isValid(stack_ptr+5) || !isValid (stack_ptr+6) || !isValid (stack_ptr+7)) +exit(-1); +lock_acquire(&mainLock); +f- +>eax=read(*(stack_ptr+5),(void *)*(stack_ptr+6),*(stack_ptr+7)); +lock_release(&mainLock); +break; +case SYS_WRITE: +if (!isValid(stack_ptr+5) || !isValid(stack_ptr+6) || +!isValid (stack_ptr+7)) +exit(-1); +lock_acquire(&mainLock); +f- +>eax = write(*(stack_ptr+5),(void*)*(stack_ptr+6),*(stack_ptr+7)); +lock_release(&mainLock); +break; +default: +exit(-1); +break; +} // +printf("syscall \n"); +} // +shutdown +void halt (void) { - shutdown_power_off(); -} - -void syscall_exit (int status) +shutdown_power_off(); +} b +ool create (const char * file, unsigned initial_size) { - struct thread *cur = thread_current (); - cur->exit_status = status; - thread_exit(); -} - -int syscall_wait (pid_p pid) +if(file == NULL) exit(-1); +return filesys_create(file, initial_size); +} b +ool remove (const char * file) { - return process_wait (pid); -} - -bool syscall_create (const char *file, unsigned initial_size) +if(file == NULL) exit(-1); +return filesys_remove(file); +} in +t read (int fd, void * buffer, unsigned size) +{ +unsigned len =0; +//read input from terminal +if (fd == STDIN_FILENO) +{ +while (len < size) +{ +*((char *)buffer+len) = input_getc(); +len++; +}r +eturn len; +} r +eturn len; +} // +Writes size bytes from buffer to the open file fd +int write (int fd, const void *buffer, unsigned size) +{ +if (fd == STDOUT_FILENO) +{ +putbuf(buffer,size); +return size; +} r +eturn -1; +} v +oid exit (int status) +{ +thread_current()->exit_code = status; +thread_exit(); +} / +* Checks for validity of a user addresses */ +bool isValid(void * vaddr) { - lock_acquire(&f_lock); - bool success = filesys_create(file, initial_size); - lock_release(&f_lock); - return success; +return (is_user_vaddr(vaddr) && +pagedir_get_page(thread_current()->pagedir,vaddr)!=NULL); } -