-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux.cc
143 lines (128 loc) · 5.22 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
/*
* Copyright (C) 2013-2014 Cloudius Systems, Ltd.
*
* 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 <boost/format.hpp>
#include <osv/sched.hh>
#include <osv/mutex.h>
#include <osv/waitqueue.hh>
#include <syscall.h>
#include <stdarg.h>
#include <time.h>
#include <unordered_map>
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;
enum {
FUTEX_WAIT = 0,
FUTEX_WAKE = 1,
};
int futex(int *uaddr, int op, int val, const struct timespec *timeout,
int *uaddr2, int val3)
{
switch (op) {
case FUTEX_WAIT:
assert(timeout == 0);
WITH_LOCK(queues_mutex) {
if (*uaddr == val) {
waitqueue &q = queues[uaddr];
q.wait(queues_mutex);
}
}
return 0;
case FUTEX_WAKE:
assert(val == INT_MAX);
WITH_LOCK(queues_mutex) {
auto i = queues.find(uaddr);
if (i != queues.end()) {
i->second.wake_all(queues_mutex);
queues.erase(i);
}
}
// FIXME: We are expected to return a count of woken threads, but
// wake_all doesn't have this feature, and the only user we care
// about, __cxa_guard_*, doesn't need this return value anyway.
return 0;
default:
abort("Unimplemented futex() operation %d\n", op);
}
}
#define SYSCALL0(fn) case (__NR_##fn): return fn()
#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); \
return fn(arg1); \
} 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); \
return fn(arg1, arg2); \
} 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); \
return fn(arg1, arg2, arg3); \
} 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); \
return fn(arg1, arg2, arg3, arg4, arg5, arg6); \
} while (0)
long syscall(long number, ...)
{
switch (number) {
SYSCALL3(write, int, const void *, size_t);
SYSCALL0(gettid);
SYSCALL2(clock_gettime, clockid_t, struct timespec *);
SYSCALL2(clock_getres, clockid_t, struct timespec *);
SYSCALL6(futex, int *, int, int, const struct timespec *, int *, int);
}
abort("syscall(): unimplemented system call %d. Aborting.\n", number);
}