locks.c revision 1.66 1 1.66 pooka /* $NetBSD: locks.c,v 1.66 2013/12/09 16:54:20 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.54 pooka * Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.23 pooka #include <sys/cdefs.h>
29 1.66 pooka __KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.66 2013/12/09 16:54:20 pooka Exp $");
30 1.23 pooka
31 1.1 pooka #include <sys/param.h>
32 1.26 pooka #include <sys/kmem.h>
33 1.1 pooka #include <sys/mutex.h>
34 1.1 pooka #include <sys/rwlock.h>
35 1.1 pooka
36 1.18 pooka #include <rump/rumpuser.h>
37 1.18 pooka
38 1.2 pooka #include "rump_private.h"
39 1.2 pooka
40 1.22 pooka /*
41 1.45 pooka * Simple lockdebug. If it's compiled in, it's always active.
42 1.45 pooka * Currently available only for mtx/rwlock.
43 1.45 pooka */
44 1.45 pooka #ifdef LOCKDEBUG
45 1.45 pooka #include <sys/lockdebug.h>
46 1.45 pooka
47 1.45 pooka static lockops_t mutex_lockops = {
48 1.45 pooka "mutex",
49 1.45 pooka LOCKOPS_SLEEP,
50 1.45 pooka NULL
51 1.45 pooka };
52 1.45 pooka static lockops_t rw_lockops = {
53 1.46 pooka "rwlock",
54 1.45 pooka LOCKOPS_SLEEP,
55 1.45 pooka NULL
56 1.45 pooka };
57 1.45 pooka
58 1.66 pooka /* not used, but need the symbols for pointer comparisons */
59 1.66 pooka syncobj_t mutex_syncobj, rw_syncobj;
60 1.66 pooka
61 1.45 pooka #define ALLOCK(lock, ops) \
62 1.45 pooka lockdebug_alloc(lock, ops, (uintptr_t)__builtin_return_address(0))
63 1.45 pooka #define FREELOCK(lock) \
64 1.45 pooka lockdebug_free(lock)
65 1.65 njoly #define WANTLOCK(lock, shar) \
66 1.65 njoly lockdebug_wantlock(lock, (uintptr_t)__builtin_return_address(0), shar)
67 1.45 pooka #define LOCKED(lock, shar) \
68 1.45 pooka lockdebug_locked(lock, NULL, (uintptr_t)__builtin_return_address(0), shar)
69 1.45 pooka #define UNLOCKED(lock, shar) \
70 1.45 pooka lockdebug_unlocked(lock, (uintptr_t)__builtin_return_address(0), shar)
71 1.45 pooka #else
72 1.45 pooka #define ALLOCK(a, b)
73 1.45 pooka #define FREELOCK(a)
74 1.65 njoly #define WANTLOCK(a, b)
75 1.45 pooka #define LOCKED(a, b)
76 1.45 pooka #define UNLOCKED(a, b)
77 1.45 pooka #endif
78 1.45 pooka
79 1.45 pooka /*
80 1.22 pooka * We map locks to pthread routines. The difference between kernel
81 1.22 pooka * and rumpuser routines is that while the kernel uses static
82 1.22 pooka * storage, rumpuser allocates the object from the heap. This
83 1.22 pooka * indirection is necessary because we don't know the size of
84 1.38 snj * pthread objects here. It is also beneficial, since we can
85 1.22 pooka * be easily compatible with the kernel ABI because all kernel
86 1.22 pooka * objects regardless of machine architecture are always at least
87 1.22 pooka * the size of a pointer. The downside, of course, is a performance
88 1.22 pooka * penalty.
89 1.22 pooka */
90 1.22 pooka
91 1.22 pooka #define RUMPMTX(mtx) (*(struct rumpuser_mtx **)(mtx))
92 1.22 pooka
93 1.1 pooka void
94 1.1 pooka mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
95 1.1 pooka {
96 1.57 pooka int ruflags = RUMPUSER_MTX_KMUTEX;
97 1.56 pooka int isspin;
98 1.56 pooka
99 1.57 pooka CTASSERT(sizeof(kmutex_t) >= sizeof(void *));
100 1.57 pooka
101 1.56 pooka /*
102 1.56 pooka * Try to figure out if the caller wanted a spin mutex or
103 1.56 pooka * not with this easy set of conditionals. The difference
104 1.56 pooka * between a spin mutex and an adaptive mutex for a rump
105 1.56 pooka * kernel is that the hypervisor does not relinquish the
106 1.56 pooka * rump kernel CPU context for a spin mutex. The
107 1.56 pooka * hypervisor itself may block even when "spinning".
108 1.56 pooka */
109 1.56 pooka if (type == MUTEX_SPIN) {
110 1.56 pooka isspin = 1;
111 1.56 pooka } else if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK ||
112 1.56 pooka ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET ||
113 1.56 pooka ipl == IPL_SOFTSERIAL) {
114 1.56 pooka isspin = 0;
115 1.56 pooka } else {
116 1.56 pooka isspin = 1;
117 1.56 pooka }
118 1.1 pooka
119 1.57 pooka if (isspin)
120 1.57 pooka ruflags |= RUMPUSER_MTX_SPIN;
121 1.57 pooka rumpuser_mutex_init((struct rumpuser_mtx **)mtx, ruflags);
122 1.45 pooka ALLOCK(mtx, &mutex_lockops);
123 1.1 pooka }
124 1.1 pooka
125 1.1 pooka void
126 1.1 pooka mutex_destroy(kmutex_t *mtx)
127 1.1 pooka {
128 1.1 pooka
129 1.45 pooka FREELOCK(mtx);
130 1.22 pooka rumpuser_mutex_destroy(RUMPMTX(mtx));
131 1.1 pooka }
132 1.1 pooka
133 1.1 pooka void
134 1.1 pooka mutex_enter(kmutex_t *mtx)
135 1.1 pooka {
136 1.1 pooka
137 1.65 njoly WANTLOCK(mtx, 0);
138 1.22 pooka rumpuser_mutex_enter(RUMPMTX(mtx));
139 1.45 pooka LOCKED(mtx, false);
140 1.1 pooka }
141 1.56 pooka
142 1.56 pooka void
143 1.56 pooka mutex_spin_enter(kmutex_t *mtx)
144 1.56 pooka {
145 1.56 pooka
146 1.65 njoly WANTLOCK(mtx, 0);
147 1.61 pooka rumpuser_mutex_enter_nowrap(RUMPMTX(mtx));
148 1.56 pooka LOCKED(mtx, false);
149 1.56 pooka }
150 1.6 pooka
151 1.1 pooka int
152 1.1 pooka mutex_tryenter(kmutex_t *mtx)
153 1.1 pooka {
154 1.60 pooka int error;
155 1.1 pooka
156 1.60 pooka error = rumpuser_mutex_tryenter(RUMPMTX(mtx));
157 1.60 pooka if (error == 0) {
158 1.65 njoly WANTLOCK(mtx, 0);
159 1.45 pooka LOCKED(mtx, false);
160 1.45 pooka }
161 1.60 pooka return error == 0;
162 1.1 pooka }
163 1.1 pooka
164 1.1 pooka void
165 1.1 pooka mutex_exit(kmutex_t *mtx)
166 1.1 pooka {
167 1.1 pooka
168 1.45 pooka UNLOCKED(mtx, false);
169 1.22 pooka rumpuser_mutex_exit(RUMPMTX(mtx));
170 1.1 pooka }
171 1.45 pooka __strong_alias(mutex_spin_exit,mutex_exit);
172 1.6 pooka
173 1.1 pooka int
174 1.1 pooka mutex_owned(kmutex_t *mtx)
175 1.1 pooka {
176 1.1 pooka
177 1.44 pooka return mutex_owner(mtx) == curlwp;
178 1.44 pooka }
179 1.44 pooka
180 1.44 pooka struct lwp *
181 1.44 pooka mutex_owner(kmutex_t *mtx)
182 1.44 pooka {
183 1.60 pooka struct lwp *l;
184 1.44 pooka
185 1.60 pooka rumpuser_mutex_owner(RUMPMTX(mtx), &l);
186 1.60 pooka return l;
187 1.1 pooka }
188 1.1 pooka
189 1.22 pooka #define RUMPRW(rw) (*(struct rumpuser_rw **)(rw))
190 1.22 pooka
191 1.1 pooka /* reader/writer locks */
192 1.1 pooka
193 1.63 pooka static enum rumprwlock
194 1.63 pooka krw2rumprw(const krw_t op)
195 1.63 pooka {
196 1.63 pooka
197 1.63 pooka switch (op) {
198 1.63 pooka case RW_READER:
199 1.63 pooka return RUMPUSER_RW_READER;
200 1.63 pooka case RW_WRITER:
201 1.63 pooka return RUMPUSER_RW_WRITER;
202 1.63 pooka default:
203 1.63 pooka panic("unknown rwlock type");
204 1.63 pooka }
205 1.63 pooka }
206 1.63 pooka
207 1.1 pooka void
208 1.1 pooka rw_init(krwlock_t *rw)
209 1.1 pooka {
210 1.1 pooka
211 1.22 pooka CTASSERT(sizeof(krwlock_t) >= sizeof(void *));
212 1.22 pooka
213 1.22 pooka rumpuser_rw_init((struct rumpuser_rw **)rw);
214 1.45 pooka ALLOCK(rw, &rw_lockops);
215 1.1 pooka }
216 1.1 pooka
217 1.1 pooka void
218 1.1 pooka rw_destroy(krwlock_t *rw)
219 1.1 pooka {
220 1.1 pooka
221 1.45 pooka FREELOCK(rw);
222 1.22 pooka rumpuser_rw_destroy(RUMPRW(rw));
223 1.1 pooka }
224 1.1 pooka
225 1.1 pooka void
226 1.1 pooka rw_enter(krwlock_t *rw, const krw_t op)
227 1.1 pooka {
228 1.1 pooka
229 1.45 pooka
230 1.65 njoly WANTLOCK(rw, op == RW_READER);
231 1.64 pooka rumpuser_rw_enter(krw2rumprw(op), RUMPRW(rw));
232 1.45 pooka LOCKED(rw, op == RW_READER);
233 1.1 pooka }
234 1.1 pooka
235 1.1 pooka int
236 1.1 pooka rw_tryenter(krwlock_t *rw, const krw_t op)
237 1.1 pooka {
238 1.60 pooka int error;
239 1.1 pooka
240 1.64 pooka error = rumpuser_rw_tryenter(krw2rumprw(op), RUMPRW(rw));
241 1.60 pooka if (error == 0) {
242 1.65 njoly WANTLOCK(rw, op == RW_READER);
243 1.45 pooka LOCKED(rw, op == RW_READER);
244 1.45 pooka }
245 1.60 pooka return error == 0;
246 1.1 pooka }
247 1.1 pooka
248 1.1 pooka void
249 1.1 pooka rw_exit(krwlock_t *rw)
250 1.1 pooka {
251 1.1 pooka
252 1.45 pooka #ifdef LOCKDEBUG
253 1.45 pooka bool shared = !rw_write_held(rw);
254 1.45 pooka
255 1.45 pooka if (shared)
256 1.45 pooka KASSERT(rw_read_held(rw));
257 1.45 pooka UNLOCKED(rw, shared);
258 1.45 pooka #endif
259 1.22 pooka rumpuser_rw_exit(RUMPRW(rw));
260 1.1 pooka }
261 1.1 pooka
262 1.1 pooka int
263 1.1 pooka rw_tryupgrade(krwlock_t *rw)
264 1.1 pooka {
265 1.63 pooka int rv;
266 1.1 pooka
267 1.63 pooka rv = rumpuser_rw_tryupgrade(RUMPRW(rw));
268 1.63 pooka if (rv == 0) {
269 1.63 pooka UNLOCKED(rw, 1);
270 1.65 njoly WANTLOCK(rw, 0);
271 1.63 pooka LOCKED(rw, 0);
272 1.63 pooka }
273 1.63 pooka return rv == 0;
274 1.1 pooka }
275 1.1 pooka
276 1.48 haad void
277 1.48 haad rw_downgrade(krwlock_t *rw)
278 1.48 haad {
279 1.48 haad
280 1.63 pooka rumpuser_rw_downgrade(RUMPRW(rw));
281 1.63 pooka UNLOCKED(rw, 0);
282 1.65 njoly WANTLOCK(rw, 1);
283 1.63 pooka LOCKED(rw, 1);
284 1.48 haad }
285 1.48 haad
286 1.6 pooka int
287 1.63 pooka rw_read_held(krwlock_t *rw)
288 1.6 pooka {
289 1.60 pooka int rv;
290 1.6 pooka
291 1.64 pooka rumpuser_rw_held(RUMPUSER_RW_READER, RUMPRW(rw), &rv);
292 1.60 pooka return rv;
293 1.10 ad }
294 1.10 ad
295 1.10 ad int
296 1.63 pooka rw_write_held(krwlock_t *rw)
297 1.10 ad {
298 1.60 pooka int rv;
299 1.10 ad
300 1.64 pooka rumpuser_rw_held(RUMPUSER_RW_WRITER, RUMPRW(rw), &rv);
301 1.60 pooka return rv;
302 1.10 ad }
303 1.10 ad
304 1.10 ad int
305 1.10 ad rw_lock_held(krwlock_t *rw)
306 1.10 ad {
307 1.10 ad
308 1.63 pooka return rw_read_held(rw) || rw_write_held(rw);
309 1.6 pooka }
310 1.6 pooka
311 1.1 pooka /* curriculum vitaes */
312 1.1 pooka
313 1.24 pooka #define RUMPCV(cv) (*(struct rumpuser_cv **)(cv))
314 1.1 pooka
315 1.1 pooka void
316 1.1 pooka cv_init(kcondvar_t *cv, const char *msg)
317 1.1 pooka {
318 1.1 pooka
319 1.25 pooka CTASSERT(sizeof(kcondvar_t) >= sizeof(void *));
320 1.25 pooka
321 1.24 pooka rumpuser_cv_init((struct rumpuser_cv **)cv);
322 1.1 pooka }
323 1.1 pooka
324 1.1 pooka void
325 1.1 pooka cv_destroy(kcondvar_t *cv)
326 1.1 pooka {
327 1.1 pooka
328 1.1 pooka rumpuser_cv_destroy(RUMPCV(cv));
329 1.1 pooka }
330 1.1 pooka
331 1.47 pooka static int
332 1.47 pooka docvwait(kcondvar_t *cv, kmutex_t *mtx, struct timespec *ts)
333 1.47 pooka {
334 1.47 pooka struct lwp *l = curlwp;
335 1.47 pooka int rv;
336 1.47 pooka
337 1.51 pooka if (__predict_false(l->l_flag & LW_RUMP_QEXIT)) {
338 1.47 pooka /*
339 1.50 pooka * yield() here, someone might want the cpu
340 1.50 pooka * to set a condition. otherwise we'll just
341 1.50 pooka * loop forever.
342 1.47 pooka */
343 1.50 pooka yield();
344 1.47 pooka return EINTR;
345 1.47 pooka }
346 1.47 pooka
347 1.47 pooka UNLOCKED(mtx, false);
348 1.47 pooka
349 1.47 pooka l->l_private = cv;
350 1.47 pooka rv = 0;
351 1.47 pooka if (ts) {
352 1.47 pooka if (rumpuser_cv_timedwait(RUMPCV(cv), RUMPMTX(mtx),
353 1.47 pooka ts->tv_sec, ts->tv_nsec))
354 1.47 pooka rv = EWOULDBLOCK;
355 1.47 pooka } else {
356 1.47 pooka rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
357 1.47 pooka }
358 1.47 pooka
359 1.52 pooka LOCKED(mtx, false);
360 1.52 pooka
361 1.47 pooka /*
362 1.51 pooka * Check for QEXIT. if so, we need to wait here until we
363 1.47 pooka * are allowed to exit.
364 1.47 pooka */
365 1.51 pooka if (__predict_false(l->l_flag & LW_RUMP_QEXIT)) {
366 1.47 pooka struct proc *p = l->l_proc;
367 1.47 pooka
368 1.53 pooka UNLOCKED(mtx, false);
369 1.47 pooka mutex_exit(mtx); /* drop and retake later */
370 1.47 pooka
371 1.47 pooka mutex_enter(p->p_lock);
372 1.51 pooka while ((p->p_sflag & PS_RUMP_LWPEXIT) == 0) {
373 1.47 pooka /* avoid recursion */
374 1.47 pooka rumpuser_cv_wait(RUMPCV(&p->p_waitcv),
375 1.47 pooka RUMPMTX(p->p_lock));
376 1.47 pooka }
377 1.51 pooka KASSERT(p->p_sflag & PS_RUMP_LWPEXIT);
378 1.47 pooka mutex_exit(p->p_lock);
379 1.47 pooka
380 1.47 pooka /* ok, we can exit and remove "reference" to l->private */
381 1.47 pooka
382 1.47 pooka mutex_enter(mtx);
383 1.53 pooka LOCKED(mtx, false);
384 1.47 pooka rv = EINTR;
385 1.47 pooka }
386 1.47 pooka l->l_private = NULL;
387 1.47 pooka
388 1.47 pooka return rv;
389 1.47 pooka }
390 1.47 pooka
391 1.1 pooka void
392 1.1 pooka cv_wait(kcondvar_t *cv, kmutex_t *mtx)
393 1.1 pooka {
394 1.1 pooka
395 1.42 pooka if (__predict_false(rump_threads == 0))
396 1.28 pooka panic("cv_wait without threads");
397 1.47 pooka (void) docvwait(cv, mtx, NULL);
398 1.1 pooka }
399 1.1 pooka
400 1.3 pooka int
401 1.5 pooka cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
402 1.5 pooka {
403 1.5 pooka
404 1.42 pooka if (__predict_false(rump_threads == 0))
405 1.42 pooka panic("cv_wait without threads");
406 1.47 pooka return docvwait(cv, mtx, NULL);
407 1.5 pooka }
408 1.5 pooka
409 1.5 pooka int
410 1.3 pooka cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
411 1.3 pooka {
412 1.58 pooka struct timespec ts;
413 1.3 pooka extern int hz;
414 1.45 pooka int rv;
415 1.27 pooka
416 1.9 pooka if (ticks == 0) {
417 1.47 pooka rv = cv_wait_sig(cv, mtx);
418 1.9 pooka } else {
419 1.58 pooka ts.tv_sec = ticks / hz;
420 1.58 pooka ts.tv_nsec = (ticks % hz) * (1000000000/hz);
421 1.47 pooka rv = docvwait(cv, mtx, &ts);
422 1.9 pooka }
423 1.5 pooka
424 1.45 pooka return rv;
425 1.5 pooka }
426 1.45 pooka __strong_alias(cv_timedwait_sig,cv_timedwait);
427 1.5 pooka
428 1.1 pooka void
429 1.1 pooka cv_signal(kcondvar_t *cv)
430 1.1 pooka {
431 1.1 pooka
432 1.1 pooka rumpuser_cv_signal(RUMPCV(cv));
433 1.1 pooka }
434 1.2 pooka
435 1.4 pooka void
436 1.4 pooka cv_broadcast(kcondvar_t *cv)
437 1.4 pooka {
438 1.4 pooka
439 1.4 pooka rumpuser_cv_broadcast(RUMPCV(cv));
440 1.4 pooka }
441 1.4 pooka
442 1.17 pooka bool
443 1.17 pooka cv_has_waiters(kcondvar_t *cv)
444 1.17 pooka {
445 1.60 pooka int rv;
446 1.17 pooka
447 1.60 pooka rumpuser_cv_has_waiters(RUMPCV(cv), &rv);
448 1.60 pooka return rv != 0;
449 1.17 pooka }
450 1.17 pooka
451 1.35 pooka /* this is not much of an attempt, but ... */
452 1.35 pooka bool
453 1.35 pooka cv_is_valid(kcondvar_t *cv)
454 1.35 pooka {
455 1.35 pooka
456 1.35 pooka return RUMPCV(cv) != NULL;
457 1.35 pooka }
458