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"
- static void syscall_handler(struct intr_frame *);
- void syscall_init(void) {
- intr_register_int(0x30, 3, INTR_ON, syscall_handler, "syscall");
- }
- /*
- SYS_CREATE,
- SYS_REMOVE,
- SYS_OPEN,
- SYS_FILESIZE,
- SYS_READ,
- SYS_WRITE,
- SYS_SEEK,
- SYS_TELL,
- SYS_CLOSE,
- */
- static void syscall_handler(struct intr_frame *f UNUSED) {
- valid_address(f->esp);
- int * system_call = f->esp;
- int args[3];
- switch (system_call) {
- case SYS_CREATE:
- stack_args(f, &args[0], 2);
- acquire_file_lock();
- f->eax = filesys_create(&args[0], args[1]);
- release_file_lock();
- break;
- case SYS_REMOVE:
- stack_args(f, args, 1);
- acquire_filesys_lock();
- bool status = filesys_remove(*(args[0]));
- if (status)
- f->eax = true;
- else
- f->eax = false;
- release_filesys_lock();
- break;
- case SYS_OPEN:
- stack_args(f, &args[0], 1);
- acquire_file_lock();
- struct file* fptr = filesys_open(*args[0]);
- check_file(fptr);
- release_file_lock();
- break;
- case SYS_CLOSE:
- stack_args(f, &args[0], 1);
- acquire_file_lock();
- close_all_thread_files(thread_current()->files);
- file_close(&args[0]);
- release_file_lock();
- break;
- case SYS_SEEK:
- stack_args(f, &args[0], 2);
- acquire_file_lock();
- file_seek(list_search(&thread_current()->files, args[0]), args[1]);
- release_file_lock();
- break;
- case SYS_TELL:
- stack_args(f, &args[0], 1);
- acquire_file_lock();
- f->eax = file_tell(list_search(&thread_current()->files, args[0]));
- release_file_lock();
- break;
- case SYS_READ:
- stack_args(f, &args[0], 3);
- read(f, args);
- break;
- case SYS_WRITE:
- stack_args(f, &args[0], 3);
- write(f, args);
- break;
- case SYS_FILESIZE:
- stack_args(f, &args[0], 1);
- acquire_file_lock();
- f->eax = file_length(list_search(&thread_current()->files, args[0]));
- release_file_lock();
- break;
- }
- }
- void read(struct intr_frame *f, int *args) {
- if (*(args[0]) == 0) {
- int *buffer = *(args[1]);
- for (int i = 0; i < *(args[2]); i++)
- buffer[i] = input_getc();
- f->eax = *(args[2]);
- } else {
- struct file_struct* fptr = list_search(&thread_current()->files,
- *(args[0]));
- if (fptr == NULL)
- f->eax = -1;
- else {
- acquire_file_lock();
- f->eax = file_read(fptr->ptr, *(args[1]), *(args[2]));
- release_file_lock();
- }
- }
- }
- void write(struct intr_frame *f, int *args) {
- if (*(args[0]) == 1) {
- putbuf(*(args[1]), *(args[2]));
- f->eax = *(args[2]);
- } else {
- struct proc_file* fptr = list_search(&thread_current()->files,
- *args[0]);
- if (fptr == NULL)
- f->eax = -1;
- else {
- acquire_filesys_lock();
- f->eax = file_write(fptr->ptr, *args[1], *args[2]);
- release_filesys_lock();
- }
- }
- }
- struct file_struct* list_search(struct list* files, int fd) {
- struct list_elem *e;
- for (e = list_begin(files); e != list_end(files); e = list_next(e)) {
- struct file_struct *f = list_entry(e, struct file_struct, elem);
- if (f->fd == fd)
- return f;
- }
- return NULL;
- }
- check_file(struct intr_frame *f, struct file* fptr) {
- if (fptr == NULL)
- f->eax = -1;
- else {
- struct file_struct *file = malloc(sizeof(*file));
- file->ptr = fptr;
- file->fd = thread_current()->fd_count;
- thread_current()->fd_count++;
- list_push_back(&thread_current()->files, &file->elem);
- f->eax = file->fd;
- }
- }
- void stack_args(struct intr_frame *f, int *args, int num_of_args) {
- int * ptr = f->esp;
- for (int i = 1; i <= num_of_args; ++i) {
- vlid_address((const void *) (ptr + i));
- args[i] = *ptr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement