-
Notifications
You must be signed in to change notification settings - Fork 4
/
hypervisor-synch.c
577 lines (486 loc) · 11.4 KB
/
hypervisor-synch.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
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
/*
* Copyright (c) 2013 Antti Kantee.
*
* See LICENSE.
*
* This module contains hypercalls related to multithreading and
* synchronization.
*/
#include <linux/kernel.h>
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/time.h>
#include <rump/rumpuser.h>
#include "hypervisor.h"
struct rumpuser_mtx {
struct mutex lkmtx;
struct lwp *owner;
int flags;
};
#define RURW_AMWRITER(rw) (rw->writer == rumpuser_curlwp() \
&& atomic_read(&rw->readers) == -1)
#define RURW_HASREAD(rw) (atomic_read(&rw->readers) > 0)
#define RURW_SETWRITE(rw) \
do { \
BUG_ON(atomic_read(&rw->readers) != 0); \
rw->writer = rumpuser_curlwp(); \
atomic_set(&rw->readers, -1); \
} while (/*CONSTCOND*/0)
#define RURW_CLRWRITE(rw) \
do { \
BUG_ON(!(atomic_read(&rw->readers) == -1 && RURW_AMWRITER(rw)));\
atomic_set(&rw->readers, 0); \
} while (/*CONSTCOND*/0)
#define RURW_INCREAD(rw) \
do { \
BUG_ON(atomic_read(&rw->readers) < 0); \
atomic_inc(&rw->readers); \
} while (/*CONSTCOND*/0)
#define RURW_DECREAD(rw) \
do { \
BUG_ON(atomic_read(&rw->readers) <= 0); \
atomic_dec(&rw->readers); \
} while (/*CONSTCOND*/0)
struct rumpuser_rw {
rwlock_t lkrw;
atomic_t readers;
struct lwp *writer;
};
static struct mutex curlwpmtx;
void
rumpuser__thrinit(void)
{
mutex_init(&curlwpmtx);
}
int
rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname,
int joinable, int priority, int cpuidx, void **ptcookie)
{
struct task_struct *newtsk;
char thrbuf[128];
/* put thread into their own namespace to avoid collisions */
snprintf(thrbuf, sizeof(thrbuf), "rump-%s", thrname);
/* cast ok, we don't care about rv (at least not for now) */
newtsk = kthread_run((int (*)(void *))f, arg, thrbuf);
if (newtsk == ERR_PTR(-ENOMEM))
return ENOMEM;
if (joinable) {
BUG_ON(!ptcookie);
*ptcookie = newtsk;
}
return 0;
}
void
rumpuser_mutex_init(struct rumpuser_mtx **mtxp, int flags)
{
struct rumpuser_mtx *mtx;
mtx = kmalloc(sizeof(struct rumpuser_mtx), GFP_KERNEL);
BUG_ON(!mtx);
mutex_init(&mtx->lkmtx);
mtx->owner = NULL;
mtx->flags = flags;
*mtxp = mtx;
}
static void
mtxenter(struct rumpuser_mtx *mtx)
{
if (!(mtx->flags & RUMPUSER_MTX_KMUTEX))
return;
BUG_ON(mtx->owner);
mtx->owner = rumpuser_curlwp();
}
static void
mtxexit(struct rumpuser_mtx *mtx)
{
if (!(mtx->flags & RUMPUSER_MTX_KMUTEX))
return;
BUG_ON(!mtx->owner);
mtx->owner = NULL;
}
void
rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
{
if (mtx->flags & RUMPUSER_MTX_SPIN) {
rumpuser_mutex_enter_nowrap(mtx);
return;
}
if (mutex_trylock(&mtx->lkmtx) == 0)
KLOCK_WRAP(mutex_lock(&mtx->lkmtx));
mtxenter(mtx);
}
void
rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx)
{
BUG_ON(!(mtx->flags & RUMPUSER_MTX_SPIN));
mutex_lock(&mtx->lkmtx);
mtxenter(mtx);
}
int
rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx)
{
int rv;
rv = mutex_trylock(&mtx->lkmtx);
if (rv) {
mtxenter(mtx);
}
return rv ? 0 : EBUSY;
}
void
rumpuser_mutex_exit(struct rumpuser_mtx *mtx)
{
mtxexit(mtx);
mutex_unlock(&mtx->lkmtx);
}
void
rumpuser_mutex_destroy(struct rumpuser_mtx *mtx)
{
mutex_destroy(&mtx->lkmtx);
kfree(mtx);
}
void
rumpuser_mutex_owner(struct rumpuser_mtx *mtx, struct lwp **owner)
{
BUG_ON(!(mtx->flags & RUMPUSER_MTX_KMUTEX));
*owner = mtx->owner;
}
void
rumpuser_rw_init(struct rumpuser_rw **rw)
{
*rw = kmalloc(sizeof(struct rumpuser_rw), GFP_KERNEL);
rwlock_init(&((*rw)->lkrw));
atomic_set(&(*rw)->readers, 0);
(*rw)->writer = NULL;
}
void
rumpuser_rw_enter(int enum_rumprwlock, struct rumpuser_rw *rw)
{
enum rumprwlock lk = enum_rumprwlock;
switch (lk) {
case RUMPUSER_RW_WRITER:
if (!write_trylock(&rw->lkrw))
KLOCK_WRAP(write_lock(&rw->lkrw));
RURW_SETWRITE(rw);
break;
case RUMPUSER_RW_READER:
if (!read_trylock(&rw->lkrw))
KLOCK_WRAP(read_lock(&rw->lkrw));
RURW_INCREAD(rw);
break;
}
}
int
rumpuser_rw_tryenter(int enum_rumprwlock, struct rumpuser_rw *rw)
{
enum rumprwlock lk = enum_rumprwlock;
int rv = 0;
switch (lk) {
case RUMPUSER_RW_WRITER:
rv = write_trylock(&rw->lkrw);
if (rv)
RURW_SETWRITE(rw);
break;
case RUMPUSER_RW_READER:
rv = read_trylock(&rw->lkrw);
if (rv)
RURW_INCREAD(rw);
break;
}
return rv ? 0 : EBUSY;
}
int
rumpuser_rw_tryupgrade(struct rumpuser_rw *rw)
{
return EBUSY;
}
void
rumpuser_rw_downgrade(struct rumpuser_rw *rw)
{
/*
* XXX: wrong, but it'll do for now. see hypervisor in NetBSD
* for how to emulate this properly.
*/
rumpuser_rw_exit(rw);
KLOCK_WRAP(rumpuser_rw_enter(RUMPUSER_RW_READER, rw));
}
void
rumpuser_rw_exit(struct rumpuser_rw *rw)
{
/* i wonder why the underlying lock doesn't know this ... */
if (RURW_HASREAD(rw)) {
RURW_DECREAD(rw);
read_unlock(&rw->lkrw);
} else {
RURW_CLRWRITE(rw);
write_unlock(&rw->lkrw);
}
}
void
rumpuser_rw_destroy(struct rumpuser_rw *rw)
{
/* what's the opposite of rwlock_init() ? */
//rwlock_destroy(&rw->lkrw);
kfree(rw);
}
void
rumpuser_rw_held(int enum_rumprwlock, struct rumpuser_rw *rw, int *rv)
{
enum rumprwlock lk = enum_rumprwlock;
switch (lk) {
case RUMPUSER_RW_WRITER:
*rv = RURW_AMWRITER(rw);
break;
case RUMPUSER_RW_READER:
*rv = RURW_HASREAD(rw);
break;
}
}
/*
* Ok, condvar hypercall. As far as I've been able to figure out
* in a few hours, Linux does not offer condition variables. It offers
* completions, but they're conveniently just a little different. The wait
* side of a completion doesn't take an interlock (por que?!?), so to
* make up for this, the object itself has memory. So instead of being
* able to get away with something readily chewed, we emulate standard
* condition variables with a song and dance of waits and queues.
* We _almost_ get away with using completitions, but using them leads
* to races with the timedwait variant... duh.
*/
struct waitobj {
struct list_head entry;
wait_queue_head_t wq;
bool wakeupdone;
};
struct rumpuser_cv {
spinlock_t slock;
struct list_head waiters;
int nwaiters;
};
static void
addwaiter(struct rumpuser_cv *cv, struct waitobj *wo)
{
init_waitqueue_head(&wo->wq);
wo->wakeupdone = false;
cv->nwaiters++;
spin_lock(&cv->slock);
list_add_tail(&wo->entry, &cv->waiters);
spin_unlock(&cv->slock);
}
/*
* Remove waiter for waitee list.
*
* NOTE: this _must_ be called before wake_up() since the rump kernel
* interlock will not protect the waiter from going back to sleep [forever]
* if the waiter happens to be scheduled between wake_up() and rmwaiter().
*/
static void
rmwaiter(struct rumpuser_cv *cv, struct waitobj *wo)
{
spin_lock(&cv->slock);
if (!wo->wakeupdone) {
list_del(&wo->entry);
cv->nwaiters--;
wo->wakeupdone = true;
}
spin_unlock(&cv->slock);
}
void
rumpuser_cv_init(struct rumpuser_cv **cvp)
{
struct rumpuser_cv *cv;
cv = kmalloc(sizeof(struct rumpuser_cv), GFP_KERNEL);
INIT_LIST_HEAD(&cv->waiters);
cv->nwaiters = 0;
spin_lock_init(&cv->slock);
*cvp = cv;
}
void
rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *rv)
{
*rv = cv->nwaiters;
}
void
rumpuser_cv_destroy(struct rumpuser_cv *cv)
{
BUG_ON(cv->nwaiters);
kfree(cv);
}
static void
cv_resched(struct rumpuser_mtx *mtx, int nlocks)
{
/*
* uuh. I guess another mutex flag to make this more obvious.
* See the verbose comment in NetBSD lib/librumpuser for more info
*/
if ((mtx->flags & (RUMPUSER_MTX_KMUTEX | RUMPUSER_MTX_SPIN)) ==
(RUMPUSER_MTX_KMUTEX | RUMPUSER_MTX_SPIN)) {
rumpkern_sched(nlocks, mtx);
rumpuser_mutex_enter_nowrap(mtx);
} else {
mutex_lock(&mtx->lkmtx);
mtxenter(mtx);
rumpkern_sched(nlocks, mtx);
}
}
void
rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
{
struct waitobj wo;
int nlocks;
rumpkern_unsched(&nlocks, mtx);
addwaiter(cv, &wo);
rumpuser_mutex_exit(mtx);
wait_event(wo.wq, wo.wakeupdone);
cv_resched(mtx, nlocks);
BUG_ON(!wo.wakeupdone);
}
void
rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
{
struct waitobj wo;
addwaiter(cv, &wo);
rumpuser_mutex_exit(mtx);
wait_event(wo.wq, wo.wakeupdone);
rumpuser_mutex_enter_nowrap(mtx);
BUG_ON(!wo.wakeupdone);
}
int
rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx,
int64_t sec, int64_t nsec)
{
struct waitobj wo;
struct timespec ts;
unsigned long timo;
int rv, nlocks;
rumpkern_unsched(&nlocks, mtx);
addwaiter(cv, &wo);
rumpuser_mutex_exit(mtx);
ts.tv_sec = sec;
ts.tv_nsec = nsec;
timo = timespec_to_jiffies(&ts);
rv = wait_event_timeout(wo.wq, wo.wakeupdone, timo);
cv_resched(mtx, nlocks);
rmwaiter(cv, &wo);
return rv == 0 ? EWOULDBLOCK : 0;
}
void
rumpuser_cv_signal(struct rumpuser_cv *cv)
{
struct waitobj *wo;
if (!list_empty(&cv->waiters)) {
BUG_ON(cv->nwaiters < 1);
wo = list_first_entry(&cv->waiters, struct waitobj, entry);
rmwaiter(cv, wo);
wake_up(&wo->wq);
}
}
void
rumpuser_cv_broadcast(struct rumpuser_cv *cv)
{
struct waitobj *wo;
struct list_head *pos, *n;
list_for_each_safe(pos, n, &cv->waiters) {
BUG_ON(cv->nwaiters < 1);
wo = list_entry(pos, struct waitobj, entry);
rmwaiter(cv, wo);
wake_up(&wo->wq);
}
}
/*
* We don't do bio, so just prevent this thread for exiting.
* I'd have to spend a minute implementing rumpuser_thread_exit() ...
*/
void __dead
rumpuser_biothread(void *arg)
{
int nlocks;
rumpkern_unsched(&nlocks, NULL);
set_current_state(TASK_INTERRUPTIBLE);
for (;;)
schedule_timeout(MAX_SCHEDULE_TIMEOUT);
}
/*
* curlwp. No thread specific data in linux? seriously??
* alrighty then, we emulate it by "hashing". and by "hashing"
* I mean a O(n) lookup. in reality this routine would need to
* be [very] fast -- in demoality, not so such. Notably, this
* approach, as opposed to real thread specific data, changes one
* rule: the client is not allowed to bind a rump kernel context
* and then exit. I'm not sure we care about that "drawback".
*/
/* yea, i said "hashing" ;) */
#define MAXTASK 256
static struct tasklwp {
struct task_struct *tsk;
struct lwp *tsklwp;
} lwps[MAXTASK];
/*
* l != NULL: set current task (must not be set)
* l == NULL: unset current task (must be set)
*/
void
rumpuser_curlwpop(int enum_rumplwpop, struct lwp *l)
{
enum rumplwpop op = enum_rumplwpop;
struct tasklwp *t;
int i;
switch (op) {
case RUMPUSER_LWP_CREATE:
mutex_lock(&curlwpmtx);
for (i = 0; i < MAXTASK; i++) {
BUG_ON(lwps[i].tsklwp == l); /* half-way assert ... */
if (lwps[i].tsklwp == NULL) {
lwps[i].tsklwp = l;
break;
}
}
if (i == MAXTASK)
panic("i can't do it captain, i need more tasks!");
mutex_unlock(&curlwpmtx);
break;
case RUMPUSER_LWP_DESTROY:
mutex_lock(&curlwpmtx);
for (i = 0; i < MAXTASK; i++) {
if (lwps[i].tsklwp == l)
break;
}
BUG_ON(i == MAXTASK);
lwps[i].tsklwp = NULL;
mutex_unlock(&curlwpmtx);
break;
case RUMPUSER_LWP_SET:
case RUMPUSER_LWP_CLEAR:
/* no need to lock, current & l are guaranteed to be stable */
for (t = NULL, i = 0; i < MAXTASK; i++) {
if (lwps[i].tsklwp == l) {
t = &lwps[i];
break;
}
}
if (op == RUMPUSER_LWP_SET) {
BUG_ON(!t || t->tsk != NULL);
t->tsk = current;
} else {
BUG_ON(!t || t->tsk == NULL);
t->tsk = NULL;
}
break;
}
}
struct lwp *
rumpuser_curlwp(void)
{
struct task_struct *cur = current;
int i;
/* no need to lock, tsk is stable here */
for (i = 0; i < MAXTASK; i++) {
if (lwps[i].tsk == cur) {
return lwps[i].tsklwp;
}
}
return NULL;
}