-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathsyscall.c
493 lines (426 loc) · 13 KB
/
syscall.c
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* rv32emu is freely redistributable under the MIT License. See the file
* "LICENSE" for information on usage and redistribution of this file.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "riscv.h"
#include "riscv_private.h"
#include "utils.h"
#define PREALLOC_SIZE 4096
/* newlib is a portable (not RISC-V specific) C library, which implements
* printf(3) and other functions described in C standards. Some system calls
* should be provided in conjunction with newlib.
*
* system call: name, number
*/
/* clang-format off */
#define SUPPORTED_SYSCALLS \
_(close, 57) \
_(lseek, 62) \
_(read, 63) \
_(write, 64) \
_(fstat, 80) \
_(exit, 93) \
_(gettimeofday, 169) \
_(brk, 214) \
_(clock_gettime, 403) \
_(open, 1024) \
IIF(RV32_HAS(SYSTEM))( \
_(sbi_base, 0x10) \
_(sbi_timer, 0x54494D45) \
_(sbi_rst, 0x53525354) \
) \
IIF(RV32_HAS(SDL))( \
_(draw_frame, 0xBEEF) \
_(setup_queue, 0xC0DE) \
_(submit_queue, 0xFEED) \
_(setup_audio, 0xBABE) \
_(control_audio, 0xD00D) \
)
/* clang-format on */
enum {
#define _(name, number) SYS_##name = number,
SUPPORTED_SYSCALLS
#undef _
};
enum {
O_RDONLY = 0,
O_WRONLY = 1,
O_RDWR = 2,
O_ACCMODE = 3,
};
static int find_free_fd(vm_attr_t *attr)
{
for (int i = 3;; ++i) {
map_iter_t it;
map_find(attr->fd_map, &it, &i);
if (map_at_end(attr->fd_map, &it))
return i;
}
}
static const char *get_mode_str(uint32_t flags, uint32_t mode UNUSED)
{
switch (flags & O_ACCMODE) {
case O_RDONLY:
return "rb";
case O_WRONLY:
return "wb";
case O_RDWR:
return "a+";
default:
return NULL;
}
}
static uint8_t tmp[PREALLOC_SIZE];
static void syscall_write(riscv_t *rv)
{
vm_attr_t *attr = PRIV(rv);
/* _write(fd, buffer, count) */
riscv_word_t fd = rv_get_reg(rv, rv_reg_a0);
riscv_word_t buffer = rv_get_reg(rv, rv_reg_a1);
riscv_word_t count = rv_get_reg(rv, rv_reg_a2);
/* lookup the file descriptor */
map_iter_t it;
map_find(attr->fd_map, &it, &fd);
if (map_at_end(attr->fd_map, &it))
goto error_handler;
uint32_t total_write = 0;
FILE *handle = map_iter_value(&it, FILE *);
while (count > PREALLOC_SIZE) {
memory_read(attr->mem, tmp, buffer + total_write, PREALLOC_SIZE);
/* write out the data */
size_t written = fwrite(tmp, 1, PREALLOC_SIZE, handle);
if (written != PREALLOC_SIZE && ferror(handle))
goto error_handler;
total_write += written;
count -= PREALLOC_SIZE;
}
memory_read(attr->mem, tmp, buffer + total_write, count);
/* write out the data */
size_t written = fwrite(tmp, 1, count, handle);
if (written != count && ferror(handle))
goto error_handler;
total_write += written;
assert(total_write == rv_get_reg(rv, rv_reg_a2));
/* return number of bytes written */
rv_set_reg(rv, rv_reg_a0, total_write);
return;
/* read the string being printed */
error_handler:
/* error */
rv_set_reg(rv, rv_reg_a0, -1);
}
static void syscall_exit(riscv_t *rv)
{
/* simply halt cpu and save exit code.
* the application decides the usage of exit code
*/
rv_halt(rv);
vm_attr_t *attr = PRIV(rv);
attr->exit_code = rv_get_reg(rv, rv_reg_a0);
}
/* brk(increment)
* Note:
* - 8 byte alignment for malloc chunks
* - 4 KiB aligned for sbrk blocks
*/
static void syscall_brk(riscv_t *rv)
{
vm_attr_t *attr = PRIV(rv);
/* get the increment parameter */
riscv_word_t increment = rv_get_reg(rv, rv_reg_a0);
if (increment)
attr->break_addr = increment;
/* return new break address */
rv_set_reg(rv, rv_reg_a0, attr->break_addr);
}
static void syscall_gettimeofday(riscv_t *rv)
{
/* get the parameters */
riscv_word_t tv = rv_get_reg(rv, rv_reg_a0);
riscv_word_t tz = rv_get_reg(rv, rv_reg_a1);
/* return the clock time */
if (tv) {
struct timeval tv_s;
rv_gettimeofday(&tv_s);
memory_write_w(tv + 0, (const uint8_t *) &tv_s.tv_sec);
memory_write_w(tv + 8, (const uint8_t *) &tv_s.tv_usec);
}
if (tz) {
/* FIXME: This parameter is ignored by the syscall handler in newlib. */
}
/* success */
rv_set_reg(rv, rv_reg_a0, 0);
}
static void syscall_clock_gettime(riscv_t *rv)
{
/* get the parameters */
riscv_word_t id = rv_get_reg(rv, rv_reg_a0);
riscv_word_t tp = rv_get_reg(rv, rv_reg_a1);
switch (id) {
case CLOCK_REALTIME:
#ifdef CLOCK_MONOTONIC
case CLOCK_MONOTONIC:
#endif
break;
default:
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
if (tp) {
struct timespec tp_s;
rv_clock_gettime(&tp_s);
memory_write_w(tp + 0, (const uint8_t *) &tp_s.tv_sec);
memory_write_w(tp + 8, (const uint8_t *) &tp_s.tv_nsec);
}
/* success */
rv_set_reg(rv, rv_reg_a0, 0);
}
static void syscall_close(riscv_t *rv)
{
vm_attr_t *attr = PRIV(rv);
/* _close(fd); */
uint32_t fd = rv_get_reg(rv, rv_reg_a0);
if (fd >= 3) { /* lookup the file descriptor */
map_iter_t it;
map_find(attr->fd_map, &it, &fd);
if (!map_at_end(attr->fd_map, &it)) {
if (fclose(map_iter_value(&it, FILE *))) {
/* error */
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
map_erase(attr->fd_map, &it);
/* success */
rv_set_reg(rv, rv_reg_a0, 0);
}
}
/* success */
rv_set_reg(rv, rv_reg_a0, 0);
}
/* lseek() repositions the file offset of the open file description associated
* with the file descriptor fd to the argument offset according to the
* directive whence.
*/
static void syscall_lseek(riscv_t *rv)
{
vm_attr_t *attr = PRIV(rv);
/* _lseek(fd, offset, whence); */
uint32_t fd = rv_get_reg(rv, rv_reg_a0);
uint32_t offset = rv_get_reg(rv, rv_reg_a1);
uint32_t whence = rv_get_reg(rv, rv_reg_a2);
/* find the file descriptor */
map_iter_t it;
map_find(attr->fd_map, &it, &fd);
if (map_at_end(attr->fd_map, &it)) {
/* error */
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
FILE *handle = map_iter_value(&it, FILE *);
if (fseek(handle, offset, whence)) {
/* error */
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
long pos = ftell(handle);
if (pos == -1) {
/* error */
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
/* success */
rv_set_reg(rv, rv_reg_a0, pos);
}
static void syscall_read(riscv_t *rv)
{
vm_attr_t *attr = PRIV(rv);
/* _read(fd, buf, count); */
uint32_t fd = rv_get_reg(rv, rv_reg_a0);
uint32_t buf = rv_get_reg(rv, rv_reg_a1);
uint32_t count = rv_get_reg(rv, rv_reg_a2);
/* lookup the file */
map_iter_t it;
map_find(attr->fd_map, &it, &fd);
if (map_at_end(attr->fd_map, &it)) {
/* error */
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
FILE *handle = map_iter_value(&it, FILE *);
uint32_t total_read = 0;
/* read the file into runtime memory */
while (count > PREALLOC_SIZE) {
size_t r = fread(tmp, 1, PREALLOC_SIZE, handle);
memory_write(attr->mem, buf + total_read, tmp, r);
count -= r;
total_read += r;
if (r != PREALLOC_SIZE)
break;
}
size_t r = fread(tmp, 1, count, handle);
memory_write(attr->mem, buf + total_read, tmp, r);
total_read += r;
if (total_read != rv_get_reg(rv, rv_reg_a2) && ferror(handle)) {
/* error */
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
/* success */
rv_set_reg(rv, rv_reg_a0, total_read);
}
static void syscall_fstat(riscv_t *rv UNUSED)
{
/* FIXME: fill real implementation */
}
static void syscall_open(riscv_t *rv)
{
vm_attr_t *attr = PRIV(rv);
/* _open(name, flags, mode); */
uint32_t name = rv_get_reg(rv, rv_reg_a0);
uint32_t flags = rv_get_reg(rv, rv_reg_a1);
uint32_t mode = rv_get_reg(rv, rv_reg_a2);
/* read name from runtime memory */
const size_t name_len = strlen((char *) attr->mem->mem_base + name);
char *name_str = malloc(name_len + 1);
assert(name_str);
name_str[name_len] = '\0';
memory_read(attr->mem, (uint8_t *) name_str, name, name_len);
/* open the file */
const char *mode_str = get_mode_str(flags, mode);
if (!mode_str) {
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
FILE *handle = fopen(name_str, mode_str);
if (!handle) {
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
free(name_str);
const int fd = find_free_fd(attr); /* find a free file descriptor */
/* insert into the file descriptor map */
map_insert(attr->fd_map, (void *) &fd, &handle);
/* return the file descriptor */
rv_set_reg(rv, rv_reg_a0, fd);
}
#if RV32_HAS(SDL)
extern void syscall_draw_frame(riscv_t *rv);
extern void syscall_setup_queue(riscv_t *rv);
extern void syscall_submit_queue(riscv_t *rv);
extern void syscall_setup_audio(riscv_t *rv);
extern void syscall_control_audio(riscv_t *rv);
#endif
#if RV32_HAS(SYSTEM)
/* SBI related system calls */
static void syscall_sbi_timer(riscv_t *rv)
{
vm_attr_t *attr = PRIV(rv);
const riscv_word_t fid = rv_get_reg(rv, rv_reg_a6);
const riscv_word_t a0 = rv_get_reg(rv, rv_reg_a0);
const riscv_word_t a1 = rv_get_reg(rv, rv_reg_a1);
switch (fid) {
case SBI_TIMER_SET_TIMER:
attr->timer = (((uint64_t) a1) << 32) | (uint64_t) (a0);
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, 0);
break;
default:
rv_set_reg(rv, rv_reg_a0, SBI_ERR_NOT_SUPPORTED);
rv_set_reg(rv, rv_reg_a1, 0);
break;
}
}
#define SBI_IMPL_ID 0x999
#define SBI_IMPL_VERSION 1
static void syscall_sbi_base(riscv_t *rv)
{
const riscv_word_t fid = rv_get_reg(rv, rv_reg_a6);
switch (fid) {
case SBI_BASE_GET_SBI_IMPL_ID:
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, SBI_IMPL_ID);
break;
case SBI_BASE_GET_SBI_IMPL_VERSION:
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, SBI_IMPL_VERSION);
break;
case SBI_BASE_GET_MVENDORID:
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, rv->csr_mvendorid);
break;
case SBI_BASE_GET_MARCHID:
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, rv->csr_marchid);
break;
case SBI_BASE_GET_MIMPID:
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, rv->csr_mimpid);
break;
case SBI_BASE_GET_SBI_SPEC_VERSION:
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, (0 << 24) | 3); /* version 0.3 */
break;
case SBI_BASE_PROBE_EXTENSION: {
const riscv_word_t eid = rv_get_reg(rv, rv_reg_a0);
bool available =
eid == SBI_EID_BASE || eid == SBI_EID_TIMER || eid == SBI_EID_RST;
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, available);
break;
}
default:
rv_set_reg(rv, rv_reg_a0, SBI_ERR_NOT_SUPPORTED);
rv_set_reg(rv, rv_reg_a1, 0);
break;
}
}
static void syscall_sbi_rst(riscv_t *rv)
{
const riscv_word_t fid = rv_get_reg(rv, rv_reg_a6);
const riscv_word_t a0 = rv_get_reg(rv, rv_reg_a0);
const riscv_word_t a1 = rv_get_reg(rv, rv_reg_a1);
switch (fid) {
case SBI_RST_SYSTEM_RESET:
fprintf(stderr, "system reset: type=%u, reason=%u\n", a0, a1);
rv_halt(rv);
rv_set_reg(rv, rv_reg_a0, SBI_SUCCESS);
rv_set_reg(rv, rv_reg_a1, 0);
break;
default:
rv_set_reg(rv, rv_reg_a0, SBI_ERR_NOT_SUPPORTED);
rv_set_reg(rv, rv_reg_a1, 0);
break;
}
}
#endif /* SYSTEM */
void syscall_handler(riscv_t *rv)
{
/* get the syscall number */
#if !RV32_HAS(RV32E)
riscv_word_t syscall = rv_get_reg(rv, rv_reg_a7);
#else
riscv_word_t syscall = rv_get_reg(rv, rv_reg_t0);
#endif
switch (syscall) { /* dispatch system call */
#define _(name, number) \
case SYS_##name: \
syscall_##name(rv); \
break;
SUPPORTED_SYSCALLS
#undef _
default:
fprintf(stderr, "unknown syscall %d\n", (int) syscall);
break;
}
/* save return code.
* the application decides the usage of the return code
*/
vm_attr_t *attr = PRIV(rv);
attr->error = rv_get_reg(rv, rv_reg_a0);
}