#include "userprog/syscall.h" #include #include #include "threads/interrupt.h" #include "threads/thread.h" #include "threads/init.h" #include "filesys/filesys.h" #include "filesys/off_t.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 void write (const char *buf) { printf ("printf: %s", buf); } // 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 break; case 8: // read break; case 9: // write write(INTR_ESP(2, char *)); break; case 12: // close break; default: printf ("kernel: unknown syscall '%d'\n", syscall_number); break; } }