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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
#pragma once
#include <AK/Types.h>
#ifdef __serenity__
# include <LibC/fd_set.h>
#endif
extern "C" {
struct timeval;
struct timespec;
struct sockaddr;
typedef u32 socklen_t;
}
#define ENUMERATE_SYSCALLS \
__ENUMERATE_SYSCALL(sleep) \
__ENUMERATE_SYSCALL(yield) \
__ENUMERATE_SYSCALL(putch) \
__ENUMERATE_SYSCALL(open) \
__ENUMERATE_SYSCALL(close) \
__ENUMERATE_SYSCALL(read) \
__ENUMERATE_SYSCALL(lseek) \
__ENUMERATE_SYSCALL(kill) \
__ENUMERATE_SYSCALL(getuid) \
__ENUMERATE_SYSCALL(exit) \
__ENUMERATE_SYSCALL(getgid) \
__ENUMERATE_SYSCALL(getpid) \
__ENUMERATE_SYSCALL(waitpid) \
__ENUMERATE_SYSCALL(mmap) \
__ENUMERATE_SYSCALL(munmap) \
__ENUMERATE_SYSCALL(get_dir_entries) \
__ENUMERATE_SYSCALL(lstat) \
__ENUMERATE_SYSCALL(getcwd) \
__ENUMERATE_SYSCALL(gettimeofday) \
__ENUMERATE_SYSCALL(gethostname) \
__ENUMERATE_SYSCALL(chdir) \
__ENUMERATE_SYSCALL(uname) \
__ENUMERATE_SYSCALL(set_mmap_name) \
__ENUMERATE_SYSCALL(readlink) \
__ENUMERATE_SYSCALL(write) \
__ENUMERATE_SYSCALL(ttyname_r) \
__ENUMERATE_SYSCALL(stat) \
__ENUMERATE_SYSCALL(getsid) \
__ENUMERATE_SYSCALL(setsid) \
__ENUMERATE_SYSCALL(getpgid) \
__ENUMERATE_SYSCALL(setpgid) \
__ENUMERATE_SYSCALL(getpgrp) \
__ENUMERATE_SYSCALL(fork) \
__ENUMERATE_SYSCALL(execve) \
__ENUMERATE_SYSCALL(geteuid) \
__ENUMERATE_SYSCALL(getegid) \
__ENUMERATE_REMOVED_SYSCALL(isatty) \
__ENUMERATE_SYSCALL(getdtablesize) \
__ENUMERATE_SYSCALL(dup) \
__ENUMERATE_SYSCALL(dup2) \
__ENUMERATE_SYSCALL(sigaction) \
__ENUMERATE_SYSCALL(getppid) \
__ENUMERATE_SYSCALL(umask) \
__ENUMERATE_SYSCALL(getgroups) \
__ENUMERATE_SYSCALL(setgroups) \
__ENUMERATE_SYSCALL(sigreturn) \
__ENUMERATE_SYSCALL(sigprocmask) \
__ENUMERATE_SYSCALL(sigpending) \
__ENUMERATE_SYSCALL(pipe) \
__ENUMERATE_SYSCALL(killpg) \
__ENUMERATE_SYSCALL(setuid) \
__ENUMERATE_SYSCALL(setgid) \
__ENUMERATE_SYSCALL(alarm) \
__ENUMERATE_SYSCALL(fstat) \
__ENUMERATE_SYSCALL(access) \
__ENUMERATE_SYSCALL(fcntl) \
__ENUMERATE_SYSCALL(ioctl) \
__ENUMERATE_SYSCALL(mkdir) \
__ENUMERATE_SYSCALL(times) \
__ENUMERATE_SYSCALL(utime) \
__ENUMERATE_SYSCALL(sync) \
__ENUMERATE_SYSCALL(ptsname_r) \
__ENUMERATE_SYSCALL(select) \
__ENUMERATE_SYSCALL(unlink) \
__ENUMERATE_SYSCALL(poll) \
__ENUMERATE_SYSCALL(read_tsc) \
__ENUMERATE_SYSCALL(rmdir) \
__ENUMERATE_SYSCALL(chmod) \
__ENUMERATE_SYSCALL(usleep) \
__ENUMERATE_SYSCALL(socket) \
__ENUMERATE_SYSCALL(bind) \
__ENUMERATE_SYSCALL(accept) \
__ENUMERATE_SYSCALL(listen) \
__ENUMERATE_SYSCALL(connect) \
__ENUMERATE_SYSCALL(create_shared_buffer) \
__ENUMERATE_SYSCALL(share_buffer_with) \
__ENUMERATE_SYSCALL(get_shared_buffer) \
__ENUMERATE_SYSCALL(release_shared_buffer) \
__ENUMERATE_SYSCALL(link) \
__ENUMERATE_SYSCALL(chown) \
__ENUMERATE_SYSCALL(fchmod) \
__ENUMERATE_SYSCALL(symlink) \
__ENUMERATE_SYSCALL(restore_signal_mask) \
__ENUMERATE_SYSCALL(get_shared_buffer_size) \
__ENUMERATE_SYSCALL(seal_shared_buffer) \
__ENUMERATE_SYSCALL(sendto) \
__ENUMERATE_SYSCALL(recvfrom) \
__ENUMERATE_SYSCALL(getsockopt) \
__ENUMERATE_SYSCALL(setsockopt) \
__ENUMERATE_SYSCALL(create_thread) \
__ENUMERATE_SYSCALL(gettid) \
__ENUMERATE_SYSCALL(donate) \
__ENUMERATE_SYSCALL(rename) \
__ENUMERATE_SYSCALL(shm_open) \
__ENUMERATE_SYSCALL(shm_unlink) \
__ENUMERATE_SYSCALL(ftruncate) \
__ENUMERATE_SYSCALL(systrace) \
__ENUMERATE_SYSCALL(exit_thread) \
__ENUMERATE_SYSCALL(mknod) \
__ENUMERATE_SYSCALL(writev) \
__ENUMERATE_SYSCALL(beep) \
__ENUMERATE_SYSCALL(getsockname) \
__ENUMERATE_SYSCALL(getpeername) \
__ENUMERATE_SYSCALL(sched_setparam) \
__ENUMERATE_SYSCALL(sched_getparam) \
__ENUMERATE_SYSCALL(fchown) \
__ENUMERATE_SYSCALL(halt) \
__ENUMERATE_SYSCALL(reboot) \
__ENUMERATE_SYSCALL(mount) \
__ENUMERATE_SYSCALL(umount) \
__ENUMERATE_SYSCALL(dump_backtrace) \
__ENUMERATE_SYSCALL(dbgputch) \
__ENUMERATE_SYSCALL(dbgputstr) \
__ENUMERATE_SYSCALL(watch_file) \
__ENUMERATE_SYSCALL(share_buffer_globally) \
__ENUMERATE_SYSCALL(set_process_icon) \
__ENUMERATE_SYSCALL(mprotect) \
__ENUMERATE_SYSCALL(realpath) \
__ENUMERATE_SYSCALL(get_process_name) \
__ENUMERATE_SYSCALL(fchdir) \
__ENUMERATE_SYSCALL(getrandom) \
__ENUMERATE_SYSCALL(setkeymap) \
__ENUMERATE_SYSCALL(clock_gettime) \
__ENUMERATE_SYSCALL(clock_nanosleep) \
__ENUMERATE_SYSCALL(openat) \
__ENUMERATE_SYSCALL(join_thread) \
__ENUMERATE_SYSCALL(module_load) \
__ENUMERATE_SYSCALL(module_unload) \
__ENUMERATE_SYSCALL(detach_thread) \
__ENUMERATE_SYSCALL(set_thread_name) \
__ENUMERATE_SYSCALL(get_thread_name) \
__ENUMERATE_SYSCALL(madvise) \
__ENUMERATE_SYSCALL(purge) \
__ENUMERATE_SYSCALL(set_shared_buffer_volatile) \
__ENUMERATE_SYSCALL(profiling_enable) \
__ENUMERATE_SYSCALL(profiling_disable) \
__ENUMERATE_SYSCALL(get_kernel_info_page) \
__ENUMERATE_SYSCALL(futex) \
__ENUMERATE_SYSCALL(set_thread_boost)
namespace Syscall {
enum Function {
#undef __ENUMERATE_SYSCALL
#undef __ENUMERATE_REMOVED_SYSCALL
#define __ENUMERATE_REMOVED_SYSCALL(x) SC_##x,
#define __ENUMERATE_SYSCALL(x) SC_##x,
ENUMERATE_SYSCALLS
#undef __ENUMERATE_SYSCALL
#undef __ENUMERATE_REMOVED_SYSCALL
__Count
};
inline constexpr const char* to_string(Function function)
{
switch (function) {
#undef __ENUMERATE_SYSCALL
#undef __ENUMERATE_REMOVED_SYSCALL
#define __ENUMERATE_REMOVED_SYSCALL(x) \
case SC_##x: \
return #x " (removed)";
#define __ENUMERATE_SYSCALL(x) \
case SC_##x: \
return #x;
ENUMERATE_SYSCALLS
#undef __ENUMERATE_SYSCALL
#undef __ENUMERATE_REMOVED_SYSCALL
default:
break;
}
return "Unknown";
}
#ifdef __serenity__
struct SC_mmap_params {
uint32_t addr;
uint32_t size;
int32_t prot;
int32_t flags;
int32_t fd;
int32_t offset; // FIXME: 64-bit off_t?
const char* name { nullptr };
};
struct SC_open_params {
const char* path;
int path_length;
int options;
u16 mode;
};
struct SC_openat_params {
int dirfd;
const char* path;
int path_length;
int options;
u16 mode;
};
struct SC_select_params {
int nfds;
fd_set* readfds;
fd_set* writefds;
fd_set* exceptfds;
struct timeval* timeout;
};
struct SC_clock_nanosleep_params {
int clock_id;
int flags;
const struct timespec* requested_sleep;
struct timespec* remaining_sleep;
};
struct SC_sendto_params {
int sockfd;
const void* data;
size_t data_length;
int flags;
const sockaddr* addr;
socklen_t addr_length;
};
struct SC_recvfrom_params {
int sockfd;
void* buffer;
size_t buffer_length;
int flags;
sockaddr* addr;
socklen_t* addr_length;
};
struct SC_getsockopt_params {
int sockfd;
int level;
int option;
void* value;
socklen_t* value_size;
};
struct SC_setsockopt_params {
int sockfd;
int level;
int option;
const void* value;
socklen_t value_size;
};
struct SC_futex_params {
i32* userspace_address;
int futex_op;
i32 val;
const timespec* timeout;
};
struct SC_create_thread_params {
unsigned int m_detach_state = 0; // JOINABLE or DETACHED
int m_schedule_priority = 30; // THREAD_PRIORITY_NORMAL
// FIXME: Implment guard pages in create_thread (unreadable pages at "overflow" end of stack)
// "If an implementation rounds up the value of guardsize to a multiple of {PAGESIZE},
// a call to pthread_attr_getguardsize() specifying attr shall store in the guardsize
// parameter the guard size specified by the previous pthread_attr_setguardsize() function call"
// ... ok, if you say so posix. Guess we get to lie to people about guard page size
unsigned int m_guard_page_size = 0; // Rounded up to PAGE_SIZE
unsigned int m_reported_guard_page_size = 0; // The lie we tell callers
unsigned int m_stack_size = 4 * MB; // Default PTHREAD_STACK_MIN
void* m_stack_location = nullptr; // nullptr means any, o.w. process virtual address
};
void initialize();
int sync();
inline u32 invoke(Function function)
{
u32 result;
asm volatile("int $0x82"
: "=a"(result)
: "a"(function)
: "memory");
return result;
}
template<typename T1>
inline u32 invoke(Function function, T1 arg1)
{
u32 result;
asm volatile("int $0x82"
: "=a"(result)
: "a"(function), "d"((u32)arg1)
: "memory");
return result;
}
template<typename T1, typename T2>
inline u32 invoke(Function function, T1 arg1, T2 arg2)
{
u32 result;
asm volatile("int $0x82"
: "=a"(result)
: "a"(function), "d"((u32)arg1), "c"((u32)arg2)
: "memory");
return result;
}
template<typename T1, typename T2, typename T3>
inline u32 invoke(Function function, T1 arg1, T2 arg2, T3 arg3)
{
u32 result;
asm volatile("int $0x82"
: "=a"(result)
: "a"(function), "d"((u32)arg1), "c"((u32)arg2), "b"((u32)arg3)
: "memory");
return result;
}
#endif
}
#undef __ENUMERATE_SYSCALL
#define __ENUMERATE_SYSCALL(x) using Syscall::SC_##x;
#define __ENUMERATE_REMOVED_SYSCALL(x)
ENUMERATE_SYSCALLS
#undef __ENUMERATE_SYSCALL
#undef __ENUMERATE_REMOVED_SYSCALL
#define syscall Syscall::invoke
|