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
|
#include <comus/cpu.h>
#include <comus/syscalls.h>
#include <comus/drivers/acpi.h>
#include <comus/drivers/gpu.h>
#include <comus/drivers/pit.h>
#include <comus/memory.h>
#include <comus/procs.h>
#include <stdint.h>
#define RET(type, name) type *name = (type *)(¤t_pcb->regs->rax)
#define ARG1(type, name) type name = (type)(current_pcb->regs->rdi)
#define ARG2(type, name) type name = (type)(current_pcb->regs->rsi)
#define ARG3(type, name) type name = (type)(current_pcb->regs->rdx)
#define ARG4(type, name) type name = (type)(current_pcb->regs->rcx)
static int sys_exit(void)
{
ARG1(int, status);
(void)status;
// FIXME: schedule somthing else
while (1)
;
return 1;
}
static int sys_write(void)
{
ARG1(int, fd);
ARG2(const void *, buffer);
ARG3(size_t, nbytes);
const char *map_buf = kmapuseraddr(current_pcb->memctx, buffer, nbytes);
if (map_buf == NULL)
return 0;
// cannot write to stdin
if (fd == 0)
nbytes = 0;
// write to stdout
else if (fd == 1) {
for (size_t i = 0; i < nbytes; i++)
kputc(map_buf[i]);
}
// files
else {
// TODO: write to files
nbytes = 0;
}
kunmapaddr(map_buf);
return nbytes;
}
static int sys_poweroff(void)
{
// TODO: we should probably
// kill all user processes
// and then sync the fs
acpi_shutdown();
return 1;
}
static int sys_drm(void)
{
ARG1(void **, res_fb);
ARG2(int *, res_width);
ARG3(int *, res_height);
ARG4(int *, res_bpp);
void *pADDR, *vADDR;
int width, height, bpp;
size_t len;
if (gpu_dev == NULL)
return 1;
len = gpu_dev->width * gpu_dev->height * gpu_dev->bit_depth / 8;
pADDR =
mem_get_phys(kernel_mem_ctx, (void *)(uintptr_t)gpu_dev->framebuffer);
if (pADDR == NULL)
return 1;
vADDR = mem_mapaddr(current_pcb->memctx, pADDR, (void *)0x1000000000, len,
F_PRESENT | F_WRITEABLE | F_UNPRIVILEGED);
if (vADDR == NULL)
return 1;
width = gpu_dev->width;
height = gpu_dev->height;
bpp = gpu_dev->bit_depth;
mem_ctx_switch(current_pcb->memctx);
*res_fb = vADDR;
*res_width = width;
*res_height = height;
*res_bpp = bpp;
mem_ctx_switch(kernel_mem_ctx);
return 0;
}
static int sys_ticks(void)
{
RET(uint64_t, res_ticks);
*res_ticks = ticks;
return 0;
}
static int (*syscall_tbl[N_SYSCALLS])(void) = {
[SYS_exit] = sys_exit, [SYS_waitpid] = NULL,
[SYS_fork] = NULL, [SYS_exec] = NULL,
[SYS_open] = NULL, [SYS_close] = NULL,
[SYS_read] = NULL, [SYS_write] = sys_write,
[SYS_getpid] = NULL, [SYS_getppid] = NULL,
[SYS_gettime] = NULL, [SYS_getprio] = NULL,
[SYS_setprio] = NULL, [SYS_kill] = NULL,
[SYS_sleep] = NULL, [SYS_brk] = NULL,
[SYS_sbrk] = NULL, [SYS_poweroff] = sys_poweroff,
[SYS_drm] = sys_drm, [SYS_ticks] = sys_ticks,
};
void syscall_handler(struct cpu_regs *regs)
{
uint64_t num;
int (*handler)(void);
int ret = 1;
// make sure were in the kernel memory context
mem_ctx_switch(kernel_mem_ctx);
// update data
current_pcb->regs = regs;
num = current_pcb->regs->rax;
current_pcb->regs->rax = 0;
// syscall number
// check for invalid syscall
if (num >= N_SYSCALLS) {
// invalid syscall
// FIXME: kill user process
while (1)
;
}
// run syscall handler
handler = syscall_tbl[num];
if (handler != NULL)
ret = handler();
// on failure, set rax
if (ret)
current_pcb->regs->rax = ret;
}
|