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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Types.h>
#include <AK/Userspace.h>
#ifdef __serenity__
# include <LibC/fd_set.h>
#endif
constexpr int syscall_vector = 0x82;
extern "C" {
struct pollfd;
struct timeval;
struct timespec;
struct sockaddr;
struct siginfo;
typedef u32 socklen_t;
}
namespace Kernel {
#define ENUMERATE_SYSCALLS(S) \
S(sleep) \
S(yield) \
S(open) \
S(close) \
S(read) \
S(lseek) \
S(kill) \
S(getuid) \
S(exit) \
S(geteuid) \
S(getegid) \
S(getgid) \
S(getpid) \
S(getppid) \
S(getresuid) \
S(getresgid) \
S(waitid) \
S(mmap) \
S(munmap) \
S(get_dir_entries) \
S(getcwd) \
S(gettimeofday) \
S(gethostname) \
S(sethostname) \
S(chdir) \
S(uname) \
S(set_mmap_name) \
S(readlink) \
S(write) \
S(ttyname) \
S(stat) \
S(getsid) \
S(setsid) \
S(getpgid) \
S(setpgid) \
S(getpgrp) \
S(fork) \
S(execve) \
S(dup) \
S(dup2) \
S(sigaction) \
S(umask) \
S(getgroups) \
S(setgroups) \
S(sigreturn) \
S(sigprocmask) \
S(sigpending) \
S(pipe) \
S(killpg) \
S(seteuid) \
S(setegid) \
S(setuid) \
S(setgid) \
S(setresuid) \
S(setresgid) \
S(alarm) \
S(fstat) \
S(access) \
S(fcntl) \
S(ioctl) \
S(mkdir) \
S(times) \
S(utime) \
S(sync) \
S(ptsname) \
S(select) \
S(unlink) \
S(poll) \
S(rmdir) \
S(chmod) \
S(usleep) \
S(socket) \
S(bind) \
S(accept) \
S(listen) \
S(connect) \
S(shbuf_create) \
S(shbuf_allow_pid) \
S(shbuf_get) \
S(shbuf_release) \
S(link) \
S(chown) \
S(fchmod) \
S(symlink) \
S(shbuf_seal) \
S(sendto) \
S(recvfrom) \
S(getsockopt) \
S(setsockopt) \
S(create_thread) \
S(gettid) \
S(donate) \
S(rename) \
S(ftruncate) \
S(exit_thread) \
S(mknod) \
S(writev) \
S(beep) \
S(getsockname) \
S(getpeername) \
S(sched_setparam) \
S(sched_getparam) \
S(fchown) \
S(halt) \
S(reboot) \
S(mount) \
S(umount) \
S(dump_backtrace) \
S(dbgputch) \
S(dbgputstr) \
S(watch_file) \
S(shbuf_allow_all) \
S(set_process_icon) \
S(mprotect) \
S(realpath) \
S(get_process_name) \
S(fchdir) \
S(getrandom) \
S(setkeymap) \
S(clock_gettime) \
S(clock_settime) \
S(clock_nanosleep) \
S(join_thread) \
S(module_load) \
S(module_unload) \
S(detach_thread) \
S(set_thread_name) \
S(get_thread_name) \
S(madvise) \
S(purge) \
S(shbuf_set_volatile) \
S(profiling_enable) \
S(profiling_disable) \
S(futex) \
S(set_thread_boost) \
S(set_process_boost) \
S(chroot) \
S(pledge) \
S(unveil) \
S(perf_event) \
S(shutdown) \
S(get_stack_bounds) \
S(ptrace) \
S(minherit) \
S(sendfd) \
S(recvfd) \
S(sysconf) \
S(set_process_name) \
S(disown)
namespace Syscall {
enum Function {
#undef __ENUMERATE_SYSCALL
#define __ENUMERATE_SYSCALL(x) SC_##x,
ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
#undef __ENUMERATE_SYSCALL
__Count
};
inline constexpr const char* to_string(Function function)
{
switch (function) {
#undef __ENUMERATE_SYSCALL
#define __ENUMERATE_SYSCALL(x) \
case SC_##x: \
return #x;
ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
#undef __ENUMERATE_SYSCALL
default:
break;
}
return "Unknown";
}
#ifdef __serenity__
struct StringArgument {
Userspace<const char*> characters;
size_t length { 0 };
};
template<typename DataType, typename SizeType>
struct MutableBufferArgument {
DataType* data { nullptr };
SizeType size { 0 };
};
template<typename DataType, typename SizeType>
struct ImmutableBufferArgument {
const DataType* data { nullptr };
SizeType size { 0 };
};
struct StringListArgument {
StringArgument* strings { nullptr };
size_t length { 0 };
};
struct SC_mmap_params {
uint32_t addr;
uint32_t size;
uint32_t alignment;
int32_t prot;
int32_t flags;
int32_t fd;
int32_t offset; // FIXME: 64-bit off_t?
StringArgument name;
};
struct SC_open_params {
int dirfd;
StringArgument path;
int options;
u16 mode;
};
struct SC_select_params {
int nfds;
fd_set* readfds;
fd_set* writefds;
fd_set* exceptfds;
const struct timespec* timeout;
const u32* sigmask;
};
struct SC_poll_params {
struct pollfd* fds;
unsigned nfds;
Userspace<const struct timespec*> timeout;
Userspace<const u32*> sigmask;
};
struct SC_clock_nanosleep_params {
int clock_id;
int flags;
Userspace<const struct timespec*> requested_sleep;
Userspace<struct timespec*> remaining_sleep;
};
struct SC_sendto_params {
int sockfd;
ImmutableBufferArgument<void, size_t> data;
int flags;
const sockaddr* addr;
socklen_t addr_length;
};
struct SC_recvfrom_params {
int sockfd;
MutableBufferArgument<void, size_t> buffer;
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;
Userspace<const void*> value;
socklen_t value_size;
};
struct SC_getsockname_params {
int sockfd;
Userspace<sockaddr*> addr;
Userspace<socklen_t*> addrlen;
};
struct SC_getpeername_params {
int sockfd;
Userspace<sockaddr*> addr;
Userspace<socklen_t*> addrlen;
};
struct SC_futex_params {
Userspace<const i32*> userspace_address;
int futex_op;
i32 val;
Userspace<const timespec*> timeout;
};
struct SC_setkeymap_params {
Userspace<const u32*> map;
Userspace<const u32*> shift_map;
Userspace<const u32*> alt_map;
Userspace<const u32*> altgr_map;
StringArgument map_name;
};
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
Userspace<void*> m_stack_location; // nullptr means any, o.w. process virtual address
};
struct SC_realpath_params {
StringArgument path;
MutableBufferArgument<char, size_t> buffer;
};
struct SC_set_mmap_name_params {
void* addr;
size_t size;
StringArgument name;
};
struct SC_execve_params {
StringArgument path;
StringListArgument arguments;
StringListArgument environment;
};
struct SC_readlink_params {
StringArgument path;
MutableBufferArgument<char, size_t> buffer;
};
struct SC_link_params {
StringArgument old_path;
StringArgument new_path;
};
struct SC_chown_params {
StringArgument path;
u32 uid;
u32 gid;
};
struct SC_mknod_params {
StringArgument path;
u16 mode;
u32 dev;
};
struct SC_symlink_params {
StringArgument target;
StringArgument linkpath;
};
struct SC_rename_params {
StringArgument old_path;
StringArgument new_path;
};
struct SC_mount_params {
int source_fd;
StringArgument target;
StringArgument fs_type;
int flags;
};
struct SC_pledge_params {
StringArgument promises;
StringArgument execpromises;
};
struct SC_unveil_params {
StringArgument path;
StringArgument permissions;
};
struct SC_waitid_params {
int idtype;
int id;
Userspace<struct siginfo*> infop;
int options;
};
struct SC_stat_params {
StringArgument path;
Userspace<struct stat*> statbuf;
bool follow_symlinks;
};
struct SC_ptrace_params {
int request;
pid_t pid;
Userspace<u8*> addr;
int data;
};
struct SC_ptrace_peek_params {
Userspace<const u32*> address;
Userspace<u32*> out_data;
};
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;
ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
#undef __ENUMERATE_SYSCALL
#define syscall Syscall::invoke
}
using namespace Kernel;
|