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
|
#include "sys/syscall.h"
#include "arch/x86_64/hal/obj.h"
#include "hal/memory.h"
#include "hal/smp.h"
#include "proc/process.h"
#include "jove.h"
#include "lib/printf.h"
#include "sys/link.h"
#include "sys/message.h"
int __syscall_generic_objpath(jove_owned_obj_t *objdir, size_t payload_bytes, void *payload_data, jove_owned_obj_t **retobj)
{
size_t bytes_needed = sizeof(struct syscall_generic_objpath);
CHECK_TOOSMALL(bytes_needed, payload_bytes,
"Message too small for payload.");
struct syscall_generic_objpath *payload = payload_data;
bytes_needed += payload->pathlen << 1;
CHECK_TOOSMALL(bytes_needed, payload_bytes,
"Message too small for object path.");
errno = 0;
*retobj = objdir_getobj(objdir, payload->path, payload->pathlen);
return errno;
}
int
_syscall_handle_generic(syscall_context_t *ctx)
{
pcb_t *pcb = obj_deref(ctx->pcb);
klogf("Process %i encountered stub syscall on object %P\n", pcb_getpid(pcb), ctx->header->path, ctx->header->pathlen);
return -EBADOBJ;
}
static const syscall_handle_t s_syscall_handles[128] = {
[JOVE_OBJ_UNTYPED] = _syscall_handle_untyped,
[JOVE_OBJ_OBJDIR] = _syscall_handle_objdir,
[JOVE_OBJ_PAGEDIR] = _syscall_handle_pagedir,
[JOVE_OBJ_PROCESSOR] = _syscall_handle_generic,
[JOVE_OBJ_REGION] = _syscall_handle_region,
[JOVE_OBJ_SHMEMORY] = _syscall_handle_generic,
[JOVE_OBJ_PCB] = _syscall_handle_pcb,
[JOVE_OBJ_INT_HANDLE] = _syscall_handle_generic,
[JOVE_OBJ_INITRD_FILE] = _syscall_handle_initrd_file,
[JOVE_OBJ_FRAMEBUFFER] = _syscall_handle_generic,
[JOVE_OBJ_VIRTTERM] = _syscall_handle_vtt,
[JOVE_OBJ_MESSAGE] = _syscall_handle_generic,
[JOVE_OBJ_LINK] = _syscall_handle_generic,
};
int
_syscall_handle_switch(syscall_context_t *ctx)
{
uint8_t target_type = obj_gettype(ctx->target.obj);
if(target_type == JOVE_OBJ_NONE) {
klogf("Tried to call syscall on NULL object %P\n", ctx->header->path, ctx->header->pathlen);
return -EBADOBJ;
}
if(target_type > 0x7F) {
return _syscall_handle_arch(ctx);
}
else {
syscall_handle_t call_handle = s_syscall_handles[target_type];
if(call_handle == NULL) {
klogf("Tried to call syscall on object %P which doesn't support syscalls!\n",
ctx->header->path, ctx->header->pathlen);
return -EBADOBJ;
}
else {
return call_handle(ctx);
}
}
}
int
syscall_handler(uintmax_t message_path_raw, uint16_t message_pathlen)
{
uint16_t *message_path = (uint16_t*)&message_path_raw;
if(message_pathlen > sizeof(uintmax_t) >> 1) {
return -EBADPATH;
}
syscall_context_t ctx;
ctx.lockstack.depth = 0;
int sysret = 0;
ctx.pcb = process_current();
pcb_t *pcb = obj_deref(ctx.pcb);
ctx.objdir = lockstack_push(&ctx.lockstack, obj_take(pcb_getrootdir(pcb)));
ctx.message = link_try_traverse_objdir(
ctx.objdir, &ctx.lockstack, message_pathlen, message_path);
if(ctx.message.obj == NULL) {
klogf("Process %i failed to get message %P : %s\n",
pcb_getpid(pcb), message_path, message_pathlen, errstr(errno));
sysret = -errno;
goto release_return;
}
if(obj_gettype(ctx.message.obj) != JOVE_OBJ_MESSAGE) {
klogf("Process %i tried to call syscall with invalid object %P\n",
pcb_getpid(pcb), message_path, message_pathlen);
sysret = -EBADOBJ;
goto release_return;
}
if((ctx.message.permissions & JOVE_PERM_READ) == 0) {
klogf("Process %i tried to call syscall with message object %P without read permissions\n",
pcb_getpid(pcb), message_path, message_pathlen);
sysret = -EPERM;
goto release_return;
}
jove_owned_obj_t *pagedir = lockstack_push(&ctx.lockstack, pcb_getpagedir(ctx.objdir));
uintptr_t message_vptr = message_unmap(ctx.message.obj, pagedir);
if(message_vptr == 0) {
klogf("Process %i tried to call syscall with unmapped message %P\n",
pcb_getpid(pcb), message_path, message_pathlen);
sysret = -EBADOBJ;
goto release_return;
}
size_t messagelen = 0x1000;
void *messagedata = (void*)message_getdata(ctx.message.obj);
struct syscall_header *header = messagedata;
size_t bytes_needed = sizeof(struct syscall_header) + header->pathlen * 2;
if(bytes_needed > messagelen) {
//Target path exceeds message length.
klogf("Process %i tried to call syscall with target path too big for message.\n", pcb_getpid(pcb));
sysret = -ETOOSMALL;
goto remap_return;
}
ctx.header = header;
ctx.bytes_left = messagelen - bytes_needed;
ctx.payload = (void*)((uintptr_t)header + bytes_needed);
ctx.target = link_try_traverse_objdir(
ctx.objdir, &ctx.lockstack, ctx.header->pathlen, ctx.header->path);
if(ctx.target.obj == 0) {
//Check if we're trying to reference objects we've already locked in this function.
//If we are, we should set it as the target regardless of lock,
//since we're the ones with the lock already.
if(ctx.header->pathlen == 1) {
if(ctx.header->path[0] == PCB_ROOTDIR_SELF_I)
ctx.target = (jove_perm_obj_t) { .permissions = 0xFF, .obj = ctx.pcb };
else if(ctx.header->path[0] == PCB_ROOTDIR_PAGEDIR_I)
ctx.target = (jove_perm_obj_t) { .permissions = 0xFF, .obj = pagedir };
else
goto fail_target_busy;
}else{
fail_target_busy:
//Could not find payload target in pcb rootdir.
klogf("Process %i tried to call syscall with bad target path %P : %s\n",
pcb_getpid(pcb), header->path, header->pathlen, errstr(errno));
sysret = -errno;
goto remap_return;
}
}
sysret = _syscall_handle_switch(&ctx);
remap_return:
message_map(ctx.message.obj, pagedir, message_vptr);
release_return:
lockstack_popall(&ctx.lockstack);
return sysret;
}
|