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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#include "userprog/syscall.h"
#include <stdio.h>
#include <syscall-nr.h>
#include "threads/interrupt.h"
#include "threads/thread.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 void
write (const char *buf)
{
printf ("printf: %s", buf);
}
static void
close (int i)
{
struct thread *t = thread_current ();
if (!t->fds) {
printf("[%s] close: no files have been opened\n", t->name);
return;
}
struct fd *fd = t->fds + i - 2; // -2 since 0 and 1 are reserved and not present in the array
if (!fd->active) {
printf("[%s] close: tried to close inactive file descriptor %d\n", t->name, i);
return;
}
free(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
break;
case 9:
// write
write (INTR_ESP(2, char *));
break;
case 12:
// close
close (INTR_ESP(1, int));
break;
default:
printf ("kernel: unknown syscall '%d'\n", syscall_number);
break;
}
}
#undef INTR_ESP
|