-
-
Notifications
You must be signed in to change notification settings - Fork 605
/
linux.cc
758 lines (683 loc) · 25.8 KB
/
linux.cc
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
* Copyright (C) 2013-2014 Cloudius Systems, Ltd.
* Copyright (C) 2018-2024 Waldemar Kozaczuk
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
// linux syscalls
#include <osv/debug.hh>
#include <osv/sched.hh>
#include <osv/mutex.h>
#include <osv/waitqueue.hh>
#include <osv/stubbing.hh>
#include <osv/export.h>
#include <osv/trace.hh>
#include <memory>
#include <syscall.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/statx.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/unistd.h>
#include <sys/random.h>
#include <sys/vfs.h>
#include <sys/uio.h>
#include <sys/epoll.h>
#include <sys/sysinfo.h>
#include <sys/sendfile.h>
#include <sys/prctl.h>
#include <sys/timerfd.h>
#include <sys/resource.h>
#include <sys/shm.h>
#include <termios.h>
#include <poll.h>
#ifdef __x86_64__
#include "tls-switch.hh"
#endif
#include <unordered_map>
#include <musl/src/internal/ksigaction.h>
#include <osv/kernel_config_core_epoll.h>
#include <osv/kernel_config_networking_stack.h>
#include <osv/kernel_config_core_syscall.h>
#include <osv/syscalls_config.h>
extern "C" int eventfd2(unsigned int, int);
extern "C" OSV_LIBC_API long gettid()
{
return sched::thread::current()->id();
}
// We don't expect applications to use the Linux futex() system call (it is
// normally only used to implement higher-level synchronization mechanisms),
// but unfortunately gcc's C++ runtime uses a subset of futex in the
// __cxa__guard_* functions, which safeguard the concurrent initialization
// of function-scope static objects. We only implement here this subset.
// The __cxa_guard_* functions only call futex in the rare case of contention,
// in fact so rarely that OSv existed for a year before anyone noticed futex
// was missing. So the performance of this implementation is not critical.
static std::unordered_map<void*, waitqueue> queues;
static mutex queues_mutex;
#define FUTEX_BITSET_MATCH_ANY 0xffffffff
int futex(int *uaddr, int op, int val, const struct timespec *timeout,
int *uaddr2, uint32_t val3)
{
switch (op & FUTEX_CMD_MASK) {
case FUTEX_WAIT_BITSET:
if (val3 != FUTEX_BITSET_MATCH_ANY) {
abort("Unimplemented futex() operation %d\n", op);
}
case FUTEX_WAIT:
WITH_LOCK(queues_mutex) {
if (*uaddr == val) {
waitqueue &q = queues[uaddr];
if (timeout) {
sched::timer tmr(*sched::thread::current());
if ((op & FUTEX_CMD_MASK) == FUTEX_WAIT_BITSET) {
// If FUTEX_WAIT_BITSET we need to interpret timeout as an absolute
// time point. If futex operation FUTEX_CLOCK_REALTIME is set we will use
// real-time clock otherwise we will use monotonic clock
if (op & FUTEX_CLOCK_REALTIME) {
tmr.set(osv::clock::wall::time_point(std::chrono::seconds(timeout->tv_sec) +
std::chrono::nanoseconds(timeout->tv_nsec)));
} else {
tmr.set(osv::clock::uptime::time_point(std::chrono::seconds(timeout->tv_sec) +
std::chrono::nanoseconds(timeout->tv_nsec)));
}
} else {
tmr.set(std::chrono::seconds(timeout->tv_sec) +
std::chrono::nanoseconds(timeout->tv_nsec));
}
sched::thread::wait_for(queues_mutex, tmr, q);
// FIXME: testing if tmr was expired isn't quite right -
// we could have had both a wakeup and timer expiration
// racing. It would be more correct to check if we were
// waken by a FUTEX_WAKE. But how?
if (tmr.expired()) {
errno = ETIMEDOUT;
return -1;
}
} else {
q.wait(queues_mutex);
}
return 0;
} else {
errno = EWOULDBLOCK;
return -1;
}
}
case FUTEX_WAKE:
if(val < 0) {
errno = EINVAL;
return -1;
}
WITH_LOCK(queues_mutex) {
auto i = queues.find(uaddr);
if (i != queues.end()) {
int waken = 0;
while( (val > waken) && !(i->second.empty()) ) {
i->second.wake_one(queues_mutex);
waken++;
}
if(i->second.empty()) {
queues.erase(i);
}
return waken;
}
}
return 0;
default:
abort("Unimplemented futex() operation %d\n", op);
}
}
#if CONF_core_syscall
// We're not supposed to export the get_mempolicy() function, as this
// function is not part of glibc (which OSv emulates), but part of a
// separate library libnuma, which the user can simply load. libnuma's
// implementation of get_mempolicy() calls syscall(__NR_get_mempolicy,...),
// so this is what we need to expose, below.
#define MPOL_DEFAULT 0
#define MPOL_F_NODE (1<<0)
#define MPOL_F_ADDR (1<<1)
#define MPOL_F_MEMS_ALLOWED (1<<2)
#if CONF_syscall_get_mempolicy
static long get_mempolicy(int *policy, unsigned long *nmask,
unsigned long maxnode, void *addr, int flags)
{
// As OSv has no support for NUMA nodes, we do here the minimum possible,
// which is basically to return the same policy (MPOL_DEFAULT) and list
// of nodes (just node 0) no matter if the caller asked for the default
// policy, the allowed policy, or the policy for a specific address.
if ((flags & MPOL_F_NODE)) {
*policy = 0; // in this case, store a node id, not a policy
return 0;
}
if (policy) {
*policy = MPOL_DEFAULT;
}
if (nmask) {
if (maxnode < 1) {
errno = EINVAL;
return -1;
}
nmask[0] |= 1;
}
return 0;
}
#endif
#if CONF_syscall_set_mempolicy
static long set_mempolicy(int policy, unsigned long *nmask,
unsigned long maxnode)
{
// OSv has very minimal support for NUMA - merely exposes
// all cpus as a single node0 and cannot really apply any meaningful policy
// Therefore we implement this as noop, ignore all arguments and return success
return 0;
}
#endif
#if CONF_syscall_sys_sched_getaffinity
// As explained in the sched_getaffinity(2) manual page, the interface of the
// sched_getaffinity() function is slightly different than that of the actual
// system call we need to implement here.
#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
static int sys_sched_getaffinity(
pid_t pid, unsigned len, unsigned long *mask)
{
int ret = sched_getaffinity(
pid, len, reinterpret_cast<cpu_set_t *>(mask));
if (ret == 0) {
// The Linux system call doesn't zero the entire len bytes of the
// given mask - it only sets up to the configured maximum number of
// CPUs (e.g., 64) and returns the amount of bytes it set at mask.
// We don't have this limitation (our sched_getaffinity() does zero
// the whole len), but some user code (e.g., libnuma's
// set_numa_max_cpu()) expect a reasonably low number to be
// returned, even when len is unrealistically high, so let's
// return a lower length too.
ret = std::min(len, sched::max_cpus / 8);
}
return ret;
}
#endif
#if CONF_syscall_sys_sched_setaffinity
#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
static int sys_sched_setaffinity(
pid_t pid, unsigned len, unsigned long *mask)
{
return sched_setaffinity(
pid, len, reinterpret_cast<cpu_set_t *>(mask));
}
#endif
#define __NR_long_mmap __NR_mmap
#define __NR_long_shmat __NR_shmat
// Only void* return value of mmap is type casted, as syscall returns long.
long long_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
return (long) mmap(addr, length, prot, flags, fd, offset);
}
long long_shmat(int shmid, const void *shmaddr, int shmflg) {
return (long) shmat(shmid, shmaddr, shmflg);
}
#endif
#define SYSCALL0(fn) case (__NR_##fn): do { long ret = fn(); trace_syscall_##fn(ret); return ret; } while (0)
#define SYSCALL1(fn, __t1) \
case (__NR_##fn): do { \
va_list args; \
__t1 arg1; \
va_start(args, number); \
arg1 = va_arg(args, __t1); \
va_end(args); \
auto ret = fn(arg1); \
trace_syscall_##fn(ret, arg1); \
return ret; \
} while (0)
#define SYSCALL2(fn, __t1, __t2) \
case (__NR_##fn): do { \
va_list args; \
__t1 arg1; \
__t2 arg2; \
va_start(args, number); \
arg1 = va_arg(args, __t1); \
arg2 = va_arg(args, __t2); \
va_end(args); \
auto ret = fn(arg1, arg2); \
trace_syscall_##fn(ret, arg1, arg2);\
return ret; \
} while (0)
#define SYSCALL3(fn, __t1, __t2, __t3) \
case (__NR_##fn): do { \
va_list args; \
__t1 arg1; \
__t2 arg2; \
__t3 arg3; \
va_start(args, number); \
arg1 = va_arg(args, __t1); \
arg2 = va_arg(args, __t2); \
arg3 = va_arg(args, __t3); \
va_end(args); \
auto ret = fn(arg1, arg2, arg3); \
trace_syscall_##fn(ret, arg1, arg2, arg3); \
return ret; \
} while (0)
#define SYSCALL4(fn, __t1, __t2, __t3, __t4) \
case (__NR_##fn): do { \
va_list args; \
__t1 arg1; \
__t2 arg2; \
__t3 arg3; \
__t4 arg4; \
va_start(args, number); \
arg1 = va_arg(args, __t1); \
arg2 = va_arg(args, __t2); \
arg3 = va_arg(args, __t3); \
arg4 = va_arg(args, __t4); \
va_end(args); \
auto ret = fn(arg1, arg2, arg3, arg4); \
trace_syscall_##fn(ret, arg1, arg2, arg3, arg4); \
return ret; \
} while (0)
#define SYSCALL5(fn, __t1, __t2, __t3, __t4, __t5) \
case (__NR_##fn): do { \
va_list args; \
__t1 arg1; \
__t2 arg2; \
__t3 arg3; \
__t4 arg4; \
__t5 arg5; \
va_start(args, number); \
arg1 = va_arg(args, __t1); \
arg2 = va_arg(args, __t2); \
arg3 = va_arg(args, __t3); \
arg4 = va_arg(args, __t4); \
arg5 = va_arg(args, __t5); \
va_end(args); \
auto ret = fn(arg1, arg2, arg3, arg4, arg5); \
trace_syscall_##fn(ret, arg1, arg2, arg3, arg4, arg5); \
return ret; \
} while (0)
#define SYSCALL6(fn, __t1, __t2, __t3, __t4, __t5, __t6) \
case (__NR_##fn): do { \
va_list args; \
__t1 arg1; \
__t2 arg2; \
__t3 arg3; \
__t4 arg4; \
__t5 arg5; \
__t6 arg6; \
va_start(args, number); \
arg1 = va_arg(args, __t1); \
arg2 = va_arg(args, __t2); \
arg3 = va_arg(args, __t3); \
arg4 = va_arg(args, __t4); \
arg5 = va_arg(args, __t5); \
arg6 = va_arg(args, __t6); \
va_end(args); \
auto ret = fn(arg1, arg2, arg3, arg4, arg5, arg6); \
trace_syscall_##fn(ret, arg1, arg2, arg3, arg4, arg5, arg6); \
return ret; \
} while (0)
#if CONF_core_syscall
int rt_sigaction(int sig, const struct k_sigaction * act, struct k_sigaction * oact, size_t sigsetsize)
{
struct sigaction libc_act, libc_oact, *libc_act_p = nullptr;
memset(&libc_act, 0, sizeof(libc_act));
memset(&libc_oact, 0, sizeof(libc_act));
if (act) {
libc_act.sa_handler = act->handler;
libc_act.sa_flags = act->flags & ~SA_RESTORER;
libc_act.sa_restorer = nullptr;
memcpy(&libc_act.sa_mask, &act->mask, sizeof(libc_act.sa_mask));
libc_act_p = &libc_act;
}
int ret = sigaction(sig, libc_act_p, &libc_oact);
if (oact) {
oact->handler = libc_oact.sa_handler;
oact->flags = libc_oact.sa_flags;
oact->restorer = nullptr;
memcpy(oact->mask, &libc_oact.sa_mask, sizeof(oact->mask));
}
return ret;
}
int rt_sigprocmask(int how, sigset_t * nset, sigset_t * oset, size_t sigsetsize)
{
return sigprocmask(how, nset, oset);
}
int rt_sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout, size_t sigsetsize)
{
if (!timeout || (!timeout->tv_sec && !timeout->tv_nsec)) {
return sigwaitinfo(set, info);
} else {
errno = ENOSYS;
return -1;
}
}
#if CONF_syscall_sys_exit
#define __NR_sys_exit __NR_exit
static int sys_exit(int ret)
{
sched::thread::current()->exit();
return 0;
}
#endif
#if CONF_syscall_sys_exit_group
#define __NR_sys_exit_group __NR_exit_group
static int sys_exit_group(int ret)
{
exit(ret);
return 0;
}
#endif
#if CONF_syscall_sys_getcwd
#define __NR_sys_getcwd __NR_getcwd
static long sys_getcwd(char *buf, unsigned long size)
{
if (!buf) {
errno = EINVAL;
return -1;
}
auto ret = getcwd(buf, size);
if (!ret) {
return -1;
}
return strlen(ret) + 1;
}
#endif
#if CONF_syscall_sys_getcpu
#define __NR_sys_getcpu __NR_getcpu
static long sys_getcpu(unsigned int *cpu, unsigned int *node, void *tcache)
{
if (cpu) {
*cpu = sched::cpu::current()->id;
}
if (node) {
*node = 0;
}
return 0;
}
#endif
#if CONF_syscall_sys_set_robust_list
#define __NR_sys_set_robust_list __NR_set_robust_list
static long sys_set_robust_list(struct robust_list_head *head, size_t len)
{
sched::thread::current()->set_robust_list(head);
return 0;
}
#endif
#if CONF_syscall_sys_set_tid_address
#define __NR_sys_set_tid_address __NR_set_tid_address
static long sys_set_tid_address(int *tidptr)
{
sched::thread::current()->set_clear_id(tidptr);
return sched::thread::current()->id();
}
#endif
#define CLONE_THREAD 0x00010000
#define CLONE_CHILD_SETTID 0x01000000
#define CLONE_PARENT_SETTID 0x00100000
#define CLONE_CHILD_CLEARTID 0x00200000
extern sched::thread *clone_thread(unsigned long flags, void *child_stack, unsigned long newtls);
#define __NR_sys_clone __NR_clone
#ifdef __x86_64__
int sys_clone(unsigned long flags, void *child_stack, int *ptid, int *ctid, unsigned long newtls)
#endif
#ifdef __aarch64__
int sys_clone(unsigned long flags, void *child_stack, int *ptid, unsigned long newtls, int *ctid)
#endif
{ //
//We only support "cloning" of threads so fork() would fail but pthread_create() should
//succeed
if (!(flags & CLONE_THREAD)) {
errno = ENOSYS;
return -1;
}
//
//Validate we have non-empty stack
if (!child_stack) {
errno = EINVAL;
return -1;
}
//
//Validate ptid and ctid which we would be setting down if requested by these flags
if (((flags & CLONE_PARENT_SETTID) && !ptid) ||
((flags & CLONE_CHILD_SETTID) && !ctid) ||
((flags & CLONE_SETTLS) && !newtls)) {
errno = EFAULT;
return -1;
}
sched::thread *t = clone_thread(flags, child_stack, newtls);
//
//Store the child thread ID at the location pointed to by ptid
if ((flags & CLONE_PARENT_SETTID)) {
*ptid = t->id();
}
//
//Store the child thread ID at the location pointed to by ctid
if ((flags & CLONE_CHILD_SETTID)) {
*ctid = t->id();
}
//
//Clear (zero) the child thread ID at the location pointed to by child_tid
//in child memory when the child exits, and do a wakeup on the futex at that address
//See thread::complete()
if ((flags & CLONE_CHILD_CLEARTID)) {
t->set_clear_id(ctid);
}
t->start();
//
//The manual of sigprocmask has this to say about clone:
//"Each of the threads in a process has its own signal mask.
// A child created via fork(2) inherits a copy of its parent's
// signal mask; the signal mask is preserved across execve(2)."
//TODO: Does it mean new thread should inherit signal mask of the parent?
return t->id();
}
struct clone_args {
u64 flags;
u64 pidfd;
u64 child_tid;
u64 parent_tid;
u64 exit_signal;
u64 stack;
u64 stack_size;
u64 tls;
};
#if CONF_syscall_sys_clone3
#define __NR_sys_clone3 435
static int sys_clone3(struct clone_args *args, size_t size)
{
return sys_clone(
args->flags,
reinterpret_cast<void*>(args->stack) + args->stack_size,
reinterpret_cast<int*>(args->parent_tid),
#ifdef __x86_64__
reinterpret_cast<int*>(args->child_tid),
args->tls);
#endif
#ifdef __aarch64__
args->tls,
reinterpret_cast<int*>(args->child_tid));
#endif
}
#endif
#define __NR_sys_ioctl __NR_ioctl
//
// We need to define explicit sys_ioctl that takes these 3 parameters to conform
// to Linux signature of this system call. The underlying ioctl function which we delegate to
// is variadic and takes slightly different paremeters and therefore cannot be used directly
// as other system call implementations can.
#define KERNEL_NCCS 19
// This structure is exactly what glibc expects to receive when calling ioctl()
// with TCGET and is defined in sysdeps/unix/sysv/linux/kernel_termios.h.
struct __kernel_termios {
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_line;
cc_t c_cc[KERNEL_NCCS];
};
#if CONF_syscall_sys_ioctl
static int sys_ioctl(unsigned int fd, unsigned int command, unsigned long arg)
{
if (command == TCGETS) {
//The termios structure is slightly different from the version of it used
//by the syscall so let us translate it manually
termios _termios;
auto ret = ioctl(fd, command, &_termios);
if (!ret) {
__kernel_termios *ktermios = reinterpret_cast<__kernel_termios*>(arg);
ktermios->c_iflag = _termios.c_iflag;
ktermios->c_oflag = _termios.c_oflag;
ktermios->c_cflag = _termios.c_cflag;
ktermios->c_lflag = _termios.c_lflag;
ktermios->c_line = _termios.c_line;
memcpy(&ktermios->c_cc[0], &_termios.c_cc[0], KERNEL_NCCS * sizeof (cc_t));
}
return ret;
} else {
return ioctl(fd, command, arg);
}
}
#endif
struct sys_sigset {
const sigset_t *ss; /* Pointer to signal set */
size_t ss_len; /* Size (in bytes) of object pointed to by 'ss' */
};
#if CONF_syscall_pselect6
static int pselect6(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timespec *timeout_ts,
sys_sigset* sigmask)
{
// As explained in the pselect(2) manual page, the system call pselect accepts
// pointer to a structure holding pointer to sigset_t and its size which is different
// from the glibc version of pselect().
// On top of this, the Linux pselect6() system call modifies its timeout argument
// unlike the glibc pselect() function. Our implementation below is to great extent
// similar to that of pselect() in core/select.cc
sigset_t origmask;
struct timeval timeout;
if (timeout_ts) {
timeout.tv_sec = timeout_ts->tv_sec;
timeout.tv_usec = timeout_ts->tv_nsec / 1000;
}
if (sigmask) {
sigprocmask(SIG_SETMASK, sigmask->ss, &origmask);
}
auto ret = select(nfds, readfds, writefds, exceptfds,
timeout_ts == NULL? NULL : &timeout);
if (sigmask) {
sigprocmask(SIG_SETMASK, &origmask, NULL);
}
if (timeout_ts) {
timeout_ts->tv_sec = timeout.tv_sec;
timeout_ts->tv_nsec = timeout.tv_usec * 1000;
}
return ret;
}
#endif
#if CONF_syscall_tgkill
static int tgkill(int tgid, int tid, int sig)
{
//
// Given OSv supports sigle process only, we only support this syscall
// when thread group id is self (getpid()) or -1 (see https://linux.die.net/man/2/tgkill)
// AND tid points to the current thread (caller)
// Ideally we would want to delegate to pthread_kill() but there is no
// easy way to map tgid to pthread_t so we directly delegate to kill().
if ((tgid == -1 || tgid == getpid()) && (tid == gettid())) {
return kill(tgid, sig);
}
errno = ENOSYS;
return -1;
}
#endif
#define __NR_sys_getdents64 __NR_getdents64
extern "C" ssize_t sys_getdents64(int fd, void *dirp, size_t count);
extern long arch_prctl(int code, unsigned long addr);
#if CONF_syscall_sys_brk
#define __NR_sys_brk __NR_brk
void *get_program_break();
static long sys_brk(void *addr)
{
// The brk syscall is almost the same as the brk() function
// except it needs to return new program break on success
// and old one on failure
void *old_break = get_program_break();
if (!brk(addr)) {
return reinterpret_cast<long>(get_program_break());
} else {
return reinterpret_cast<long>(old_break);
}
}
#endif
#define __NR_utimensat4 __NR_utimensat
extern int utimensat4(int dirfd, const char *pathname, const struct timespec times[2], int flags);
#endif
TRACEPOINT(trace_syscall_futex, "%d <= %p %d %d %p %p %d", int, int *, int, int, const struct timespec *, int *, uint32_t);
#if CONF_core_syscall
#include <osv/syscall_tracepoints.cc>
#endif
OSV_LIBC_API long syscall(long number, ...)
{
// Save FPU state and restore it at the end of this function
sched::fpu_lock fpu;
SCOPE_LOCK(fpu);
switch (number) {
SYSCALL6(futex, int *, int, int, const struct timespec *, int *, uint32_t);
#if CONF_core_syscall
#include <osv/syscalls.cc>
#endif
}
debug_always("syscall(): unimplemented system call %d\n", number);
errno = ENOSYS;
return -1;
}
long __syscall(long number, ...) __attribute__((alias("syscall")));
#ifdef __x86_64__
// In x86-64, a SYSCALL instruction has exactly 6 parameters, because this is the number of registers
// alloted for passing them (additional parameters *cannot* be passed on the stack). So we can get
// 7 arguments to this function (syscall number plus its 6 parameters). Because in the x86-64 ABI the
// seventh argument is on the stack, we must pass the arguments explicitly to the syscall() function
// and can't just call it without any arguments and hope everything will be passed on
extern "C" long syscall_wrapper(long number, long p1, long p2, long p3, long p4, long p5, long p6)
#endif
#ifdef __aarch64__
// In aarch64, the first 8 parameters to a procedure call are passed in the x0-x7 registers and
// the parameters of syscall call (SVC intruction) in are passed in x0-x5 registers and syscall number
// in x8 register before. To avoid shuffling the arguments around we make syscall_wrapper()
// accept the syscall parameters as is but accept the syscall number as the last 7th argument which
// the code in entry.S arranges.
extern "C" long syscall_wrapper(long p1, long p2, long p3, long p4, long p5, long p6, long number)
#endif
{
#ifdef __x86_64__
// Switch TLS register if necessary
arch::tls_switch tls_switch;
#endif
int errno_backup = errno;
// syscall and function return value are in rax
auto ret = syscall(number, p1, p2, p3, p4, p5, p6);
int result = -errno;
errno = errno_backup;
if (ret < 0 && ret >= -4096) {
return result;
}
return ret;
}
extern "C" int is_selinux_enabled()
{
return 0;
}