Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "userprog/syscall.h"
- #include <stdio.h>
- #include <syscall-nr.h>
- #include "threads/interrupt.h"
- #include "threads/thread.h"
- #include "devices/shutdown.h"
- #include "userprog/pagedir.h"
- #include "filesys/filesys.h"
- #include "threads/vaddr.h"
- static void syscall_handler(struct intr_frame *);
- typedef void syscall_fun(struct intr_frame *, uint32_t *);
- bool check_ptr(char *ptr);
- /* Projects 2 and later. */
- syscall_fun call_SYS_HALT, call_SYS_EXIT, call_SYS_EXEC, call_SYS_WAIT, call_SYS_CREATE,
- call_SYS_REMOVE, call_SYS_OPEN, call_SYS_FILESIZE, call_SYS_READ, call_SYS_WRITE,
- call_SYS_SEEK, call_SYS_TELL, call_SYS_CLOSE, call_SYS_PRACTICE;
- /* Project 3 and optionally project 4. */
- syscall_fun call_SYS_MMAP, call_SYS_MUNMAP;
- /* Project 4 only. */
- syscall_fun call_SYS_CHDIR, call_SYS_MKDIR, call_SYS_READDIR, call_SYS_ISDIR, call_SYS_INUMBER;
- /* System Call struct */
- typedef struct fun_desc
- {
- syscall_fun *fun;
- } fun_desc;
- /* System call lookup table */
- fun_desc sys_call_table[] = {
- {call_SYS_HALT},
- {call_SYS_EXIT},
- {call_SYS_EXEC},
- {call_SYS_WAIT},
- {call_SYS_CREATE},
- {call_SYS_REMOVE},
- {call_SYS_OPEN},
- {call_SYS_FILESIZE},
- {call_SYS_READ},
- {call_SYS_WRITE},
- {call_SYS_SEEK},
- {call_SYS_TELL},
- {call_SYS_CLOSE},
- {call_SYS_PRACTICE},
- {call_SYS_MMAP},
- {call_SYS_MUNMAP},
- {call_SYS_CHDIR},
- {call_SYS_MKDIR},
- {call_SYS_READDIR},
- {call_SYS_ISDIR},
- {call_SYS_INUMBER},
- };
- void syscall_init(void)
- {
- intr_register_int(0x30, 3, INTR_ON, syscall_handler, "syscall");
- }
- static void
- syscall_handler(struct intr_frame *f UNUSED)
- {
- uint32_t *args = ((uint32_t *)f->esp);
- int SC_NO = args[0];
- if (!(0 <= SC_NO && SC_NO <= SYS_INUMBER))
- {
- printf("Error: System call out of boundaries. SC_NO = %d\n", SC_NO);
- return;
- }
- sys_call_table[SC_NO].fun(f, args);
- }
- /* System Call Implementations */
- void call_SYS_HALT(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- shutdown_power_off();
- return;
- }
- void call_SYS_EXIT(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- f->eax = args[1];
- printf("%s: exit(%d)\n", thread_current()->name, args[1]);
- thread_exit();
- }
- void call_SYS_EXEC(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- return;
- }
- void call_SYS_WAIT(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- return;
- }
- void call_SYS_CREATE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- /* Arguments */
- char *file_name = (char *)args[1];
- int file_size = (int)args[2];
- /* check fo valid pointer */
- if (!check_ptr(file_name))
- {
- args[1] = -1;
- call_SYS_EXIT(f, args);
- return;
- }
- /* return value */
- f->eax = filesys_create(file_name, file_size);
- }
- void call_SYS_REMOVE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- return;
- }
- void call_SYS_OPEN(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- return;
- }
- void call_SYS_FILESIZE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- return;
- }
- void call_SYS_READ(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- f->eax = 1000;
- return;
- }
- void call_SYS_WRITE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- /* Arguments */
- int fd = args[1];
- void *buffer = (void *)args[2];
- int size = args[3];
- /* write in buffer */
- putbuf(buffer, size);
- /* save return value */
- f->eax = size;
- }
- void call_SYS_SEEK(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- return;
- }
- void call_SYS_TELL(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- return;
- }
- void call_SYS_CLOSE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- // TODO Project 2
- return;
- }
- void call_SYS_PRACTICE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- /* Arguments */
- int practice = args[1];
- practice += 1;
- /* return value */
- f->eax = practice;
- return;
- }
- void call_SYS_MMAP(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- return;
- }
- void call_SYS_MUNMAP(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- return;
- }
- void call_SYS_CHDIR(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- return;
- }
- void call_SYS_MKDIR(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- return;
- }
- void call_SYS_READDIR(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- return;
- }
- void call_SYS_ISDIR(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- return;
- }
- void call_SYS_INUMBER(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
- {
- return;
- }
- /* checks given pointer whether it's valid or not */
- bool check_ptr(char *ptr)
- {
- if (pagedir_get_page(thread_current()->pagedir, ptr) == NULL || !is_user_vaddr(ptr))
- {
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement