-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscalls.c
246 lines (187 loc) · 5.07 KB
/
syscalls.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
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "kine.h"
#include "kstd.h"
extern struct config_t config;
extern struct k_state_t k_state;
typedef int32_t (*syscall_t)();
static void* get_user(uint32_t ptr) {
return (void*)(ptr + config.base);
}
static int32_t sys_write(uint32_t buf, size_t len) {
void* buf_ptr = get_user(buf);
if (config.strace)
fprintf(stderr, "write(%p)\n", buf_ptr);
return write(1, buf_ptr, len);
}
static int32_t sys_setvideo(int type) {
if (config.strace)
fprintf(stderr, "setvideo(%d)\n", type);
switch (type) {
case KVIDEO_TEXT:
case KVIDEO_GRAPHIC:
k_lock(&k_state);
k_state.video_mode = type;
k_unlock(&k_state);
return 0;
default:
return -KEINVAL;
}
}
static int32_t sys_open(uint32_t pathname, int flags) {
(void) flags;
char* pathname_ptr = get_user(pathname);
char path[2048];
snprintf(path, sizeof(path) - 1, "%s%s", config.path, pathname_ptr);
int ret = open(path, O_RDONLY);
if (ret < 0)
ret = -KENOENT;
struct stat st = { 0 };
fstat(ret, &st);
if (S_ISDIR(st.st_mode)) {
close(ret);
ret = -KENOENT;
}
if (config.strace)
fprintf(stderr, "open(%s) = %d\n", pathname_ptr, ret);
return ret;
}
static int32_t sys_close(int fd) {
if (config.strace)
fprintf(stderr, "close(%d)\n", fd);
close(fd);
return 0;
}
static int32_t sys_swap_frontbuffer(uint32_t buffer) {
if (config.strace)
fprintf(stderr, "swap_frontbuffer()\n");
k_state.render_state->swap_frontbuffer(k_state.render_state, get_user(buffer));
return 0;
}
static int32_t sys_read(int fd, uint32_t buf, uint32_t count) {
void* buf_ptr = get_user(buf);
int ret = read(fd, buf_ptr, count);
if (config.strace)
fprintf(stderr, "read(%d, %p, %u) = %d\n", fd, buf_ptr,
count, ret);
if (ret < 0) {
switch (ret) {
case EBADF:
ret = -KEBADF;
break;
case EINVAL:
default:
ret = -KEINVAL;
break;
}
}
return ret;
}
static uint32_t sys_sbrk(int32_t inc) {
if (inc < 0)
inc = 0;
uint32_t out = k_state.brk;
k_state.brk += inc;
if (config.strace)
fprintf(stderr, "sbrk(%d) = %#x\n", inc, out);
return out;
}
static uint32_t sys_gettick(void) {
uint32_t ticks = getms() - k_state.starttime;
if (config.strace)
fprintf(stderr, "gettick() = %u\n", ticks);
return ticks;
}
static int32_t sys_seek(int fd, int32_t off, int whence) {
if (config.strace)
fprintf(stderr, "seek(%d, %d, %d)\n", fd, off, whence);
switch (whence) {
case KSEEK_SET:
whence = SEEK_SET;
break;
case KSEEK_CUR:
whence = SEEK_CUR;
break;
case KSEEK_END:
whence = SEEK_END;
break;
default:
return -KEINVAL;
};
return lseek(fd, off, whence);
}
static int32_t sys_getkey(void) {
int32_t out = -1;
k_lock(&k_state);
out = k_state.key;
k_unlock(&k_state);
if (config.strace)
fprintf(stderr, "getkey() = %d\n", out);
return out;
}
static uint32_t sys_set_palette(uint32_t palette, size_t sze) {
uint32_t* arr = get_user(palette);
if (config.strace)
fprintf(stderr, "set_palette(%x, %zu)\n", palette, sze);
k_lock(&k_state);
k_state.render_state->set_palette(k_state.render_state, arr, sze);
k_unlock(&k_state);
return 0;
}
static int32_t sys_getkeymode(int released) {
int32_t out = -KEAGAIN;
struct ring* r = released ? &k_state.released : &k_state.pressed ;
k_lock(&k_state);
uint8_t c = 0;
if (ring_pop(r, &c) >= 0)
out = c;
k_unlock(&k_state);
if (config.strace)
fprintf(stderr, "getkeymode(%d) = %d\n", released, out);
return out;
}
#define not_implemented NULL
static syscall_t syscalls[] = {
[KSYSCALL_WRITE] = (syscall_t)sys_write,
[KSYSCALL_SBRK] = (syscall_t)sys_sbrk,
[KSYSCALL_GETKEY] = (syscall_t)sys_getkey,
[KSYSCALL_GETTICK] = (syscall_t)sys_gettick,
[KSYSCALL_OPEN] = (syscall_t)sys_open,
[KSYSCALL_READ] = (syscall_t)sys_read,
[KSYSCALL_SEEK] = (syscall_t)sys_seek,
[KSYSCALL_CLOSE] = (syscall_t)sys_close,
[KSYSCALL_SETVIDEO] = (syscall_t)sys_setvideo,
[KSYSCALL_SWAP_FRONTBUFFER] = (syscall_t)sys_swap_frontbuffer,
[KSYSCALL_PLAYSOUND] = (syscall_t)not_implemented, /* not implemented */
[KSYSCALL_SETPALETTE] = (syscall_t)sys_set_palette,
[KSYSCALL_GETMOUSE] = (syscall_t)not_implemented, /* not implemented */
[14] = (syscall_t)sys_getkeymode,
};
int32_t syscall_dispatch(uint32_t sysnr, uint32_t* args) {
if (sysnr > ARRSZE(syscalls) || !syscalls[sysnr]) {
fprintf(stderr, "unsupported syscall: %d\n", sysnr);
return -KENOSYS;
}
return syscalls[sysnr](args[0], args[1], args[2], args[3]);
}