summaryrefslogtreecommitdiffstats
path: root/src/userprog/syscall.c
blob: 5e5a6f12ec56902621d9429c999007770f6150d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "userprog/syscall.h"
#include <stdio.h>
#include <syscall-nr.h>
#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");
}

// 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
      power_off ();
      break;
    case 1:
      // exit
      break;
    case 4:
      // create
      ; // empty statement because c-grammar doesn't allow declarations following labels
      printf("kernel: create\n");
      char *filename = INTR_ESP(1, char *);
      printf("create: read filename '%s'\n", filename);
      off_t initial_size = INTR_ESP(2, off_t);
      printf("create: read initial_size '%d'\n", initial_size);
      f->eax = filesys_create(filename, initial_size);
      printf("create: result (%d) put in f->eax\n", f->eax);
      break;
    case 6:
      // open
      break;
    case 8:
      // read
      break;
    case 9:
      // write
      printf ("printf: %s", INTR_ESP(2, char *));
      break;
    case 12:
      // close
      break;
    default:
      printf ("kernel: unknown syscall '%d'\n", syscall_number);
      break;
  }
}