-
Notifications
You must be signed in to change notification settings - Fork 4
/
syscall.c
79 lines (65 loc) · 1.51 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
#include "syscall.h"
#include "print.h"
#include "process.h"
#include "keyboard.h"
#include "memory.h"
#include "debug.h"
#include "stddef.h"
static SYSTEMCALL system_calls[10];
static int sys_write(int64_t *argptr)
{
write_screen((char*)argptr[0], (int)argptr[1], 0xf);
return (int)argptr[1];
}
static int sys_sleep(int64_t* argptr)
{
uint64_t old_ticks;
uint64_t ticks;
uint64_t sleep_ticks = argptr[0];
ticks = get_ticks();
old_ticks = ticks;
while (ticks - old_ticks < sleep_ticks) {
sleep(-1);
ticks = get_ticks();
}
return 0;
}
static int sys_exit(int64_t *argptr)
{
exit();
return 0;
}
static int sys_wait(int64_t *argptr)
{
wait();
return 0;
}
static int sys_keyboard_read(int64_t *argptr)
{
return read_key_buffer();
}
static int sys_get_total_memory(int64_t *argptr)
{
return get_total_memory();
}
void init_system_call(void)
{
system_calls[0] = sys_write;
system_calls[1] = sys_sleep;
system_calls[2] = sys_exit;
system_calls[3] = sys_wait;
system_calls[4] = sys_keyboard_read;
system_calls[5] = sys_get_total_memory;
}
void system_call(struct TrapFrame *tf)
{
int64_t i = tf->rax;
int64_t param_count = tf->rdi;
int64_t *argptr = (int64_t*)tf->rsi;
if (param_count < 0 || i > 5 || i < 0) {
tf->rax = -1;
return;
}
ASSERT(system_calls[i] != NULL);
tf->rax = system_calls[i](argptr);
}