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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#include "userprog/syscall.h"
#include <stdio.h>
#include <syscall-nr.h>
#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 "lib/pid_t.h"
#include "userprog/pagedir.h"
#include "userprog/process.h"
#include "threads/init.h"
#include "threads/malloc.h"
#include "threads/vaddr.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) {
t->fds = (struct file **) calloc (MAX_FDS, sizeof (struct file *));
}
struct file *file = filesys_open (filename);
if (!file) {
return -1;
}
for (int i = 0; i < MAX_FDS; i++) {
struct file **fd = t->fds + i;
if (*fd == NULL) {
*fd = file;
return i + 2; // 0 and 1 are reserved for stdin and stdout
}
}
free (file);
return -1;
}
static struct file **
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 void
exit (int status)
{
struct thread *thread = thread_current ();
if (thread->fds) {
for (int i = 0; i < MAX_FDS; i++) {
struct file **fd = thread->fds + i;
if (fd && *fd) {
file_close (*fd);
*fd = NULL;
}
}
free (thread->fds);
}
lock_acquire (&thread->parent->l);
thread->parent->exit_status = status;
lock_release (&thread->parent->l);
thread_exit ();
}
static int
wait (tid_t child_tid)
{
return process_wait (child_tid);
}
static int
read (int fd_i, void *buf, unsigned size)
{
struct thread *thread = thread_current ();
if (fd_i == 0) {
// stdin
unsigned i;
for (i = 0; i < size; i++) {
((char *)buf)[i] = input_getc ();
}
return i;
} else if (fd_i == 1) {
// can't read from stdout
return -1;
}
struct file **fd = get_fd (thread, fd_i);
if (fd && *fd) {
return file_read (*fd, buf, size);
} else {
return -1;
}
}
static int
write (int fd_i, const void *buf, unsigned size)
{
struct thread *thread = thread_current ();
if (fd_i == 1) {
// stdout
putbuf ((const char *)buf, size);
return size;
} else if (fd_i == 0) {
// can't write to stdout
return -1;
}
struct file **fd = get_fd (thread, fd_i);
if (fd && *fd) {
return file_write (*fd, buf, size);
} else {
return -1;
}
}
static void
close (int fd_i)
{
struct thread *thread = thread_current ();
struct file **fd = get_fd (thread, fd_i);
if (fd && *fd) {
file_close (*fd);
*fd = NULL;
}
}
static pid_t
exec (const char *filename)
{
return process_execute (filename);
}
static bool
ptr_is_valid (const void *ptr)
{
// uintptr_t ptr = _ptr;
if (!is_user_vaddr (ptr))
return false;
struct thread *t = thread_current ();
if (pagedir_get_page (t->pagedir, ptr) == NULL)
return false;
return true;
}
// cast argument N from f->esp to TYPE without dereferencing
#define INTR_ESP(N, TYPE) (TYPE *)(f->esp+(4*(N)))
#define CHECK_PTR_AND_MAYBE_EXIT(PTR) \
do { \
if (!ptr_is_valid (PTR)) { \
exit (-1); \
return; \
} \
} while (0)
static void
syscall_handler (struct intr_frame *f UNUSED)
{
// check esp
int *syscall_number = INTR_ESP (0, int);
CHECK_PTR_AND_MAYBE_EXIT (syscall_number);
char **filename;
int *status, *fd_i;
off_t *initial_size;
tid_t *child_tid;
unsigned *size;
void **buf;
switch (*syscall_number) {
case 0:
// halt
halt ();
break;
case 1:
// exit
status = INTR_ESP (1, int);
CHECK_PTR_AND_MAYBE_EXIT (status);
exit (*status);
break;
case 2:
// exec
filename = INTR_ESP (1, char *);
CHECK_PTR_AND_MAYBE_EXIT (filename);
CHECK_PTR_AND_MAYBE_EXIT (*filename);
f->eax = exec (*filename);
break;
case 3:
// wait
child_tid = INTR_ESP (1, tid_t);
CHECK_PTR_AND_MAYBE_EXIT (child_tid);
f->eax = wait (*child_tid);
break;
case 4:
// create
filename = INTR_ESP (1, char *);
initial_size = INTR_ESP (2, off_t);
CHECK_PTR_AND_MAYBE_EXIT (filename);
CHECK_PTR_AND_MAYBE_EXIT (*filename);
CHECK_PTR_AND_MAYBE_EXIT (initial_size);
f->eax = create (*filename, *initial_size);
break;
case 6:
// open
filename = INTR_ESP (1, char *);
CHECK_PTR_AND_MAYBE_EXIT (filename);
CHECK_PTR_AND_MAYBE_EXIT (*filename);
f->eax = open (*filename);
break;
case 8:
// read
fd_i = INTR_ESP (1, int);
buf = INTR_ESP (2, void *);
size = INTR_ESP (3, unsigned);
CHECK_PTR_AND_MAYBE_EXIT (fd_i);
CHECK_PTR_AND_MAYBE_EXIT (buf);
CHECK_PTR_AND_MAYBE_EXIT (*buf);
CHECK_PTR_AND_MAYBE_EXIT (size);
CHECK_PTR_AND_MAYBE_EXIT (*buf + *size);
f->eax = read (*fd_i, *buf, *size);
break;
case 9:
// write
fd_i = INTR_ESP (1, int);
buf = INTR_ESP (2, void *);
size = INTR_ESP (3, unsigned);
CHECK_PTR_AND_MAYBE_EXIT (fd_i);
CHECK_PTR_AND_MAYBE_EXIT (buf);
CHECK_PTR_AND_MAYBE_EXIT (*buf);
CHECK_PTR_AND_MAYBE_EXIT (size);
CHECK_PTR_AND_MAYBE_EXIT (*buf + *size);
f->eax = write (*fd_i, *buf, *size);
break;
case 12:
// close
fd_i = INTR_ESP (1, int);
close (*fd_i);
break;
default:
printf ("kernel: unknown syscall '%d'\n", *syscall_number);
break;
}
}
#undef INTR_ESP
|