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
|
#include <kernel/arch/generic.h>
#include <kernel/mem/alloc.h>
#include <kernel/mem/virt.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/vfs/path.h>
#include <shared/flags.h>
#include <shared/mem.h>
#include <shared/syscalls.h>
#include <stdint.h>
_Noreturn void _syscall_exit(int ret) {
process_kill(process_current, ret);
process_switch_any();
}
int _syscall_await(void) {
bool has_children = false;
process_transition(process_current, PS_WAITS4CHILDDEATH);
// find any already dead children
for (struct process *iter = process_current->child;
iter; iter = iter->sibling) {
if (iter->state == PS_DEAD)
return process_try2collect(iter);
if (iter->state != PS_DEADER)
has_children = true;
}
if (!has_children) {
process_transition(process_current, PS_RUNNING);
return ~0; // TODO errno
} else {
return -1;
}
}
int _syscall_fork(void) {
struct process *child = process_fork(process_current);
regs_savereturn(&child->regs, 0);
return 1;
}
handle_t _syscall_open(const char __user *path, int len) {
struct vfs_mount *mount;
char *path_buf = NULL;
if (PATH_MAX < len)
return -1;
if (process_find_handle(process_current) < 0)
return -1;
path_buf = virt_cpy2kmalloc(process_current->pages, path, len);
if (!path_buf) goto fail;
len = path_simplify(path_buf, path_buf, len);
if (len < 0) goto fail;
mount = vfs_mount_resolve(process_current->mount, path_buf, len);
if (!mount) goto fail;
if (mount->prefix_len > 0) { // strip prefix
len -= mount->prefix_len;
// i can't just adjust path_buf, because it needs to be passed to free()
// later on
memcpy(path_buf, path_buf + mount->prefix_len, len);
}
return vfs_request_create((struct vfs_request) {
.type = VFSOP_OPEN,
.input = {
.kern = true,
.buf_kern = path_buf,
.len = len,
},
.caller = process_current,
.backend = mount->backend,
});
fail:
kfree(path_buf);
return -1;
}
int _syscall_mount(handle_t handle, const char __user *path, int len) {
struct vfs_mount *mount = NULL;
struct vfs_backend *backend = NULL;
char *path_buf = NULL;
if (PATH_MAX < len) return -1;
// copy the path to the kernel to simplify it
path_buf = virt_cpy2kmalloc(process_current->pages, path, len);
if (!path_buf) goto fail;
len = path_simplify(path_buf, path_buf, len);
if (len < 0) goto fail;
// remove trailing slash
// mounting something under `/this` and `/this/` should be equalivent
if (path_buf[len - 1] == '/') {
if (len == 0) goto fail;
len--;
}
if (handle >= 0) { // mounting a real backend?
if (handle >= HANDLE_MAX)
goto fail;
if (process_current->handles[handle].type != HANDLE_FS_FRONT)
goto fail;
backend = process_current->handles[handle].fs.backend;
} // otherwise backend == NULL
// append to mount list
mount = kmalloc(sizeof *mount);
mount->prev = process_current->mount;
mount->prefix = path_buf;
mount->prefix_len = len;
mount->backend = backend;
process_current->mount = mount;
return 0;
fail:
kfree(path_buf);
kfree(mount);
return -1;
}
int _syscall_read(handle_t handle_num, void __user *buf, size_t len, int offset) {
struct handle *handle = &process_current->handles[handle_num];
if (handle_num < 0 || handle_num >= HANDLE_MAX) return -1;
if (handle->type != HANDLE_FILE) return -1;
return vfs_request_create((struct vfs_request) {
.type = VFSOP_READ,
.output = {
.buf = (userptr_t) buf,
.len = len,
},
.id = handle->file.id,
.offset = offset,
.caller = process_current,
.backend = handle->file.backend,
});
}
int _syscall_write(handle_t handle_num, const void __user *buf, size_t len, int offset) {
struct handle *handle = &process_current->handles[handle_num];
if (handle_num < 0 || handle_num >= HANDLE_MAX) return -1;
if (handle->type != HANDLE_FILE) return -1;
return vfs_request_create((struct vfs_request) {
.type = VFSOP_WRITE,
.input = {
.buf = (userptr_t) buf,
.len = len,
},
.id = handle->file.id,
.offset = offset,
.caller = process_current,
.backend = handle->file.backend,
});
}
int _syscall_close(handle_t handle) {
if (handle < 0 || handle >= HANDLE_MAX) return -1;
return -1;
}
handle_t _syscall_fs_fork2(void) {
struct vfs_backend *backend;
struct process *child;
handle_t front;
front = process_find_handle(process_current);
if (front < 0) return -1;
process_current->handles[front].type = HANDLE_FS_FRONT;
/* so this can't return 0 in the parent or it will make it think that it's
* the child. i could make fork()s return -1 in the child but that's weird.
*
* also there's this whole thing with handling errors here properly and
* errno
* TODO figure this out */
if (front == 0) {
// dumb hack
front = process_find_handle(process_current);
if (front < 0) return -1;
process_current->handles[front].type = HANDLE_FS_FRONT;
process_current->handles[0].type = HANDLE_EMPTY;
}
backend = kmalloc(sizeof *backend); // TODO never freed
backend->type = VFS_BACK_USER;
backend->handler = NULL;
backend->queue = NULL;
child = process_fork(process_current);
child->controlled = backend;
regs_savereturn(&child->regs, 0);
process_current->handles[front].fs.backend = backend;
return front;
}
int _syscall_fs_wait(char __user *buf, int max_len, struct fs_wait_response __user *res) {
struct vfs_backend *backend = process_current->controlled;
if (!backend) return -1;
process_transition(process_current, PS_WAITS4REQUEST);
backend->handler = process_current;
/* checking the validity of those pointers here would make
* vfs_request_accept simpler. TODO? */
process_current->awaited_req.buf = buf;
process_current->awaited_req.max_len = max_len;
process_current->awaited_req.res = res;
if (backend->queue) {
// handle queued requests
struct process *queued = backend->queue;
backend->queue = queued->waits4fs.queue_next;
return vfs_request_accept(&queued->waits4fs.req);
}
return -1;
}
int _syscall_fs_respond(char __user *buf, int ret) {
struct vfs_request *req = process_current->handled_req;
if (!req) return -1;
if (req->output.len > 0 && ret > 0) {
// if this vfsop outputs data and ret is positive, it's the length of the buffer
// TODO document
ret = min(ret, capped_cast32(req->output.len));
if (!virt_cpy(req->caller->pages, req->output.buf,
process_current->pages, buf, ret)) {
// how should this error even be handled? TODO
}
}
process_current->handled_req = NULL;
vfs_request_finish(req, ret);
return 0;
}
int _syscall_memflag(void __user *addr, size_t len, int flags) {
userptr_t goal = addr + len;
struct pagedir *pages = process_current->pages;
if (flags != MEMFLAG_PRESENT) panic_unimplemented(); // TODO
addr = (userptr_t)((int __force)addr & ~PAGE_MASK); // align to page boundary
for (; addr < goal; addr += PAGE_SIZE) {
if (pagedir_virt2phys(pages, addr, false, false)) {
// allocated page, currently a no-op
// if you'll be changing this - remember to check if the pages are owned by the kernel!
} else {
// allocate the new pages
pagedir_map(pages, addr, page_alloc(1), true, true);
}
}
return -1;
}
int _syscall(int num, int a, int b, int c, int d) {
switch (num) {
case _SYSCALL_EXIT:
_syscall_exit(a);
case _SYSCALL_AWAIT:
return _syscall_await();
case _SYSCALL_FORK:
return _syscall_fork();
case _SYSCALL_OPEN:
return _syscall_open((userptr_t)a, b);
case _SYSCALL_MOUNT:
return _syscall_mount(a, (userptr_t)b, c);
case _SYSCALL_READ:
return _syscall_read(a, (userptr_t)b, c, d);
case _SYSCALL_WRITE:
return _syscall_write(a, (userptr_t)b, c, d);
case _SYSCALL_CLOSE:
return _syscall_close(a);
case _SYSCALL_FS_FORK2:
return _syscall_fs_fork2();
case _SYSCALL_FS_WAIT:
return _syscall_fs_wait((userptr_t)a, b, (userptr_t)c);
case _SYSCALL_FS_RESPOND:
return _syscall_fs_respond((userptr_t)a, b);
case _SYSCALL_MEMFLAG:
return _syscall_memflag((userptr_t)a, b, c);
default:
kprintf("unknown syscall ");
panic_unimplemented(); // TODO fail gracefully
}
}
|