#include "userprog/syscall.h" #include #include #include "threads/interrupt.h" #include "threads/thread.h" #include "devices/input.h" #include "filesys/file.h" #include "filesys/filesys.h" #include "filesys/off_t.h" #include "threads/init.h" #include "threads/malloc.h" static void syscall_handler (struct intr_frame *); void syscall_init (void) { intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall"); } static void halt (void) { power_off(); } static bool create (const char *filename, off_t initial_size) { return filesys_create (filename, initial_size); } static int open (const char *filename) { struct thread *t = thread_current (); if (!t->fds) { printf("[%s] open: allocating %d file descriptors\n", t->name, MAX_FDS); t->fds = (struct fd *) calloc (MAX_FDS, sizeof (struct fd)); } struct file *file = filesys_open(filename); if (!file) { printf("[%s] open: couldn't find file %s\n", t->name, filename); return -1; } for (int i = 0; i < MAX_FDS; i++) { struct fd *fd = t->fds + i; if (!fd->active) { fd->active = true; fd->file = file; return i + 2; // 0 and 1 are reserved for stdin and stdout } } free(file); printf("[%s] open: unable to find empty file descriptor\n", t->name); return -1; } static struct fd * get_fd (struct thread *thread, int fd_i) { if (!thread->fds) { return NULL; } return thread->fds + fd_i - 2; // -2 since 0 and 1 are reserved and not present in the array } static struct file * get_file (struct thread *thread, int fd_i) { struct fd *fd = get_fd(thread, fd_i); if (!fd || !fd->active) { printf("[%s] get_file: invalid file descriptor\n", thread->name); return NULL; } return fd->file; } static int read (int fd_i, void *buf, unsigned size) { struct thread *thread = thread_current (); if (fd_i == 0) { // stdin int i; for (i = 0; i < size; i++) { ((char *)buf)[i] = input_getc (); } return i; } else if (fd_i == 1) { // can't read from stdout printf("[%s] read: tried to read from stdout\n", thread->name); return -1; } struct file *file = get_file(thread, fd_i); if (!file) { return -1; } int n = file_read (file, buf, size); printf("%d/%d: %s\n", n, size, (char *)buf); return n; } static int write (int fd_i, const void *buf, unsigned size) { printf ("write: %s", (char *)buf); return 0; } static void close (int fd_i) { struct thread *thread = thread_current (); struct fd *fd = get_fd(thread, fd_i); if (!fd) { return; } if (!fd->active) { printf("[%s] close: tried to close inactive file descriptor %d\n", thread->name, fd_i); return; } file_close (fd->file); fd->active = false; } // cast to TYPE and deref argument N from f->esp #define INTR_ESP(N, TYPE) *(TYPE *)(f->esp+(4*(N))) static void syscall_handler (struct intr_frame *f UNUSED) { int syscall_number = INTR_ESP (0, int); switch (syscall_number) { case 0: // halt halt (); break; case 1: // exit break; case 4: // create f->eax = create (INTR_ESP (1, char *), INTR_ESP (2, off_t)); break; case 6: // open f->eax = open (INTR_ESP (1, char *)); break; case 8: // read f->eax = read (INTR_ESP (1, int), INTR_ESP (2, void *), INTR_ESP (3, unsigned)); break; case 9: // write f->eax = write (INTR_ESP (1, int), INTR_ESP (2, const void *), INTR_ESP (3, unsigned)); break; case 12: // close close (INTR_ESP(1, int)); break; default: printf ("kernel: unknown syscall '%d'\n", syscall_number); break; } } #undef INTR_ESP