forked from hooklift/xhyve
-
Notifications
You must be signed in to change notification settings - Fork 6
/
vmm_callout.c
381 lines (303 loc) · 8.67 KB
/
vmm_callout.c
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
/*-
* Copyright (c) 2015 xhyve developers
* 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 NETAPP, INC ``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 NETAPP, INC 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.
*
*/
/* makeshift callout implementation based on OSv and FreeBSD */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <xhyve/support/misc.h>
#include <xhyve/vmm/vmm_callout.h>
#define callout_cmp(a, b) ((a)->timeout < (b)->timeout)
static mach_timebase_info_data_t timebase_info;
static pthread_t callout_thread;
static pthread_mutex_t callout_mtx;
static pthread_cond_t callout_cnd;
static struct callout *callout_queue;
static bool work;
static bool initialized = false;
static inline uint64_t nanos_to_abs(uint64_t nanos) {
return (nanos * timebase_info.denom) / timebase_info.numer;
}
static inline uint64_t abs_to_nanos(uint64_t abs) {
return (abs * timebase_info.numer) / timebase_info.denom;
}
static inline uint64_t sbt2mat(sbintime_t sbt) {
uint64_t s, ns;
s = (((uint64_t) sbt) >> 32);
ns = (((uint64_t) 1000000000) * (uint32_t) sbt) >> 32;
return (nanos_to_abs((s * 1000000000) + ns));
}
static inline void mat_to_ts(uint64_t mat, struct timespec *ts) {
uint64_t ns;
ns = abs_to_nanos(mat);
ts->tv_sec = (ns / 1000000000);
ts->tv_nsec = (ns % 1000000000);
}
void binuptime(struct bintime *bt) {
uint64_t ns;
ns = abs_to_nanos(mach_absolute_time());
bt->sec = (ns / 1000000000);
bt->frac = (((ns % 1000000000) * (((uint64_t) 1 << 63) / 500000000)));
}
void getmicrotime(struct timeval *tv) {
uint64_t ns, sns;
ns = abs_to_nanos(mach_absolute_time());
sns = (ns / 1000000000);
tv->tv_sec = (long) sns;
tv->tv_usec = (int) ((ns - sns) / 1000);
}
static void callout_insert(struct callout *c) {
struct callout *node = callout_queue;
if (!node) {
callout_queue = c;
c->prev = NULL;
c->next = NULL;
c->queued = 1;
return;
}
if (callout_cmp(c, node)) {
node->prev = c;
c->prev = NULL;
c->next = node;
callout_queue = c;
c->queued = 1;
return;
}
while (node->next) {
if (callout_cmp(c, node->next)) {
c->prev = node;
c->next = node->next;
node->next->prev = c;
node->next = c;
c->queued = 1;
return;
}
node = node->next;
}
c->prev = node;
c->next = NULL;
node->next = c;
c->queued = 1;
}
static void callout_remove(struct callout *c) {
if (!c->queued) {
return;
}
if (c->prev) {
c->prev->next = c->next;
} else {
callout_queue = c->next;
}
if (c->next) {
c->next->prev = c->prev;
}
c->prev = NULL;
c->next = NULL;
c->queued = 0;
}
static void *callout_thread_func(UNUSED void *arg) {
struct callout *c;
struct timespec ts;
uint64_t delta, mat;
int ret;
pthread_setname_np("callout");
pthread_mutex_lock(&callout_mtx);
while (true) {
/* wait for work */
while (!callout_queue) {
pthread_cond_wait(&callout_cnd, &callout_mtx);
};
/* get the callout with the nearest timout */
c = callout_queue;
if (!(c->flags & (CALLOUT_ACTIVE | CALLOUT_PENDING))) {
abort();
}
/* wait for timeout */
ret = 0;
while ((ret != ETIMEDOUT) && !work) {
mat = mach_absolute_time();
if (mat >= c->timeout) {
/* XXX: it might not be worth sleeping for very short timeouts */
ret = ETIMEDOUT;
break;
}
delta = c->timeout - mat;
mat_to_ts(delta, &ts);
ret = pthread_cond_timedwait_relative_np(&callout_cnd, &callout_mtx, &ts);
};
work = false;
if (!(ret == ETIMEDOUT) || !c->queued) {
continue;
}
/* dispatch */
c->flags &= ~CALLOUT_PENDING;
pthread_mutex_unlock(&callout_mtx);
c->callout(c->argument);
pthread_mutex_lock(&callout_mtx);
/* note: after the handler has been invoked the callout structure can look
* much differently, the handler may have rescheduled the callout or
* even freed it.
*
* if the callout is still enqueued it means that it hasn't been
* freed by the user
*
* reset || drain || !stop
*/
if (c->queued) {
/* if the callout hasn't been rescheduled, remove it */
if (((c->flags & CALLOUT_PENDING) == 0) || (c->flags & CALLOUT_WAITING)) {
c->flags |= CALLOUT_COMPLETED;
callout_remove(c);
}
}
}
return NULL;
}
void callout_init(struct callout *c, int mpsafe) {
if (!mpsafe) {
abort();
}
memset(c, 0, sizeof(struct callout));
if (pthread_cond_init(&c->wait, NULL)) {
abort();
}
}
static int callout_stop_safe_locked(struct callout *c, int drain) {
int result = 0;
if ((drain) && (pthread_self() != callout_thread) && (callout_pending(c) ||
(callout_active(c) && !callout_completed(c))))
{
if (c->flags & CALLOUT_WAITING) {
abort();
}
/* wait for callout */
c->flags |= CALLOUT_WAITING;
work = true;
pthread_cond_signal(&callout_cnd);
while (!(c->flags & CALLOUT_COMPLETED)) {
pthread_cond_wait(&c->wait, &callout_mtx);
}
c->flags &= ~CALLOUT_WAITING;
result = 1;
}
callout_remove(c);
/* clear flags */
c->flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING | CALLOUT_COMPLETED |
CALLOUT_WAITING);
return (result);
}
int callout_stop_safe(struct callout *c, int drain) {
pthread_mutex_lock(&callout_mtx);
callout_stop_safe_locked(c, drain);
pthread_mutex_unlock(&callout_mtx);
return 0;
}
int callout_reset_sbt(struct callout *c, sbintime_t sbt,
UNUSED sbintime_t precision, void (*ftn)(void *), void *arg, int flags)
{
int result;
bool is_next_timeout;
is_next_timeout = false;
pthread_mutex_lock(&callout_mtx);
if (!((flags == 0) || (flags == C_ABSOLUTE)) || (c->flags !=0)) {
/* FIXME */
//printf("XHYVE: callout_reset_sbt 0x%08x 0x%08x\r\n", flags, c->flags);
//abort();
}
c->timeout = sbt2mat(sbt);
if (flags != C_ABSOLUTE) {
c->timeout += mach_absolute_time();
}
result = callout_stop_safe_locked(c, 0);
c->callout = ftn;
c->argument = arg;
c->flags |= (CALLOUT_PENDING | CALLOUT_ACTIVE);
callout_insert(c);
if (c == callout_queue) {
work = true;
is_next_timeout = true;
}
pthread_mutex_unlock(&callout_mtx);
if (is_next_timeout) {
pthread_cond_signal(&callout_cnd);
is_next_timeout = false;
}
return (result);
}
void callout_system_init(void) {
if (initialized) {
return;
}
mach_timebase_info(&timebase_info);
if (pthread_mutex_init(&callout_mtx, NULL)) {
abort();
}
if (pthread_cond_init(&callout_cnd, NULL)) {
abort();
}
callout_queue = NULL;
work = false;
if (pthread_create(&callout_thread, /*&attr*/ NULL, &callout_thread_func,
NULL))
{
abort();
}
initialized = true;
}
//static void callout_queue_print(void) {
// struct callout *node;
//
// pthread_mutex_lock(&callout_mtx);
// for (node = callout_queue; node; node = node->next) {
// printf("t:%llu -> ", abs_to_nanos(node->timeout));
// if (!node->next) {
// break;
// }
// }
// pthread_mutex_unlock(&callout_mtx);
// printf("NULL\n");
//}
//void fire (void *arg) {
// printf("fire!\n");
//}
//
//int main(void) {
// struct callout a;
// sbintime_t sbt;
// printf("xhyve_timer\n");
// callout_system_init();
// callout_init(&a, 1);
// sbt = ((sbintime_t) (((uint64_t) 3) << 32));
// callout_reset_sbt(&a, sbt, 0, &fire, NULL, 0);
// while (1);
// return 0;
//}