Home | History | Annotate | Line # | Download | only in rumpkern
locks.c revision 1.14.4.3
      1  1.14.4.3      yamt /*	$NetBSD: locks.c,v 1.14.4.3 2010/03/11 15:04:38 yamt Exp $	*/
      2       1.1     pooka 
      3       1.1     pooka /*
      4  1.14.4.2      yamt  * Copyright (c) 2007, 2008 Antti Kantee.  All Rights Reserved.
      5       1.1     pooka  *
      6       1.1     pooka  * Development of this software was supported by the
      7       1.1     pooka  * Finnish Cultural Foundation.
      8       1.1     pooka  *
      9       1.1     pooka  * Redistribution and use in source and binary forms, with or without
     10       1.1     pooka  * modification, are permitted provided that the following conditions
     11       1.1     pooka  * are met:
     12       1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     13       1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     14       1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     16       1.1     pooka  *    documentation and/or other materials provided with the distribution.
     17       1.1     pooka  *
     18       1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19       1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20       1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21       1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22       1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23       1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24       1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25       1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26       1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27       1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28       1.1     pooka  * SUCH DAMAGE.
     29       1.1     pooka  */
     30       1.1     pooka 
     31  1.14.4.2      yamt #include <sys/cdefs.h>
     32  1.14.4.3      yamt __KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.14.4.3 2010/03/11 15:04:38 yamt Exp $");
     33  1.14.4.2      yamt 
     34       1.1     pooka #include <sys/param.h>
     35  1.14.4.2      yamt #include <sys/kmem.h>
     36       1.1     pooka #include <sys/mutex.h>
     37       1.1     pooka #include <sys/rwlock.h>
     38  1.14.4.2      yamt 
     39  1.14.4.2      yamt #include <rump/rumpuser.h>
     40       1.1     pooka 
     41       1.2     pooka #include "rump_private.h"
     42       1.2     pooka 
     43  1.14.4.2      yamt /*
     44  1.14.4.2      yamt  * We map locks to pthread routines.  The difference between kernel
     45  1.14.4.2      yamt  * and rumpuser routines is that while the kernel uses static
     46  1.14.4.2      yamt  * storage, rumpuser allocates the object from the heap.  This
     47  1.14.4.2      yamt  * indirection is necessary because we don't know the size of
     48  1.14.4.3      yamt  * pthread objects here.  It is also beneficial, since we can
     49  1.14.4.2      yamt  * be easily compatible with the kernel ABI because all kernel
     50  1.14.4.2      yamt  * objects regardless of machine architecture are always at least
     51  1.14.4.2      yamt  * the size of a pointer.  The downside, of course, is a performance
     52  1.14.4.2      yamt  * penalty.
     53  1.14.4.2      yamt  */
     54  1.14.4.2      yamt 
     55  1.14.4.2      yamt #define RUMPMTX(mtx) (*(struct rumpuser_mtx **)(mtx))
     56       1.1     pooka 
     57       1.1     pooka void
     58       1.1     pooka mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
     59       1.1     pooka {
     60       1.1     pooka 
     61  1.14.4.2      yamt 	CTASSERT(sizeof(kmutex_t) >= sizeof(void *));
     62  1.14.4.2      yamt 
     63  1.14.4.2      yamt 	rumpuser_mutex_init((struct rumpuser_mtx **)mtx);
     64       1.1     pooka }
     65       1.1     pooka 
     66       1.1     pooka void
     67       1.1     pooka mutex_destroy(kmutex_t *mtx)
     68       1.1     pooka {
     69       1.1     pooka 
     70  1.14.4.2      yamt 	rumpuser_mutex_destroy(RUMPMTX(mtx));
     71       1.1     pooka }
     72       1.1     pooka 
     73       1.1     pooka void
     74       1.1     pooka mutex_enter(kmutex_t *mtx)
     75       1.1     pooka {
     76       1.1     pooka 
     77  1.14.4.2      yamt 	rumpuser_mutex_enter(RUMPMTX(mtx));
     78       1.1     pooka }
     79       1.1     pooka 
     80       1.6     pooka void
     81       1.6     pooka mutex_spin_enter(kmutex_t *mtx)
     82       1.6     pooka {
     83       1.6     pooka 
     84  1.14.4.3      yamt 	mutex_enter(mtx);
     85       1.6     pooka }
     86       1.6     pooka 
     87       1.1     pooka int
     88       1.1     pooka mutex_tryenter(kmutex_t *mtx)
     89       1.1     pooka {
     90       1.1     pooka 
     91  1.14.4.2      yamt 	return rumpuser_mutex_tryenter(RUMPMTX(mtx));
     92       1.1     pooka }
     93       1.1     pooka 
     94       1.1     pooka void
     95       1.1     pooka mutex_exit(kmutex_t *mtx)
     96       1.1     pooka {
     97       1.1     pooka 
     98  1.14.4.2      yamt 	rumpuser_mutex_exit(RUMPMTX(mtx));
     99       1.1     pooka }
    100       1.1     pooka 
    101       1.6     pooka void
    102       1.6     pooka mutex_spin_exit(kmutex_t *mtx)
    103       1.6     pooka {
    104       1.6     pooka 
    105  1.14.4.3      yamt 	mutex_exit(mtx);
    106       1.6     pooka }
    107       1.6     pooka 
    108       1.1     pooka int
    109       1.1     pooka mutex_owned(kmutex_t *mtx)
    110       1.1     pooka {
    111       1.1     pooka 
    112  1.14.4.2      yamt 	return rumpuser_mutex_held(RUMPMTX(mtx));
    113       1.1     pooka }
    114       1.1     pooka 
    115  1.14.4.2      yamt #define RUMPRW(rw) (*(struct rumpuser_rw **)(rw))
    116  1.14.4.2      yamt 
    117       1.1     pooka /* reader/writer locks */
    118       1.1     pooka 
    119       1.1     pooka void
    120       1.1     pooka rw_init(krwlock_t *rw)
    121       1.1     pooka {
    122       1.1     pooka 
    123  1.14.4.2      yamt 	CTASSERT(sizeof(krwlock_t) >= sizeof(void *));
    124  1.14.4.2      yamt 
    125  1.14.4.2      yamt 	rumpuser_rw_init((struct rumpuser_rw **)rw);
    126       1.1     pooka }
    127       1.1     pooka 
    128       1.1     pooka void
    129       1.1     pooka rw_destroy(krwlock_t *rw)
    130       1.1     pooka {
    131       1.1     pooka 
    132  1.14.4.2      yamt 	rumpuser_rw_destroy(RUMPRW(rw));
    133       1.1     pooka }
    134       1.1     pooka 
    135       1.1     pooka void
    136       1.1     pooka rw_enter(krwlock_t *rw, const krw_t op)
    137       1.1     pooka {
    138       1.1     pooka 
    139  1.14.4.2      yamt 	rumpuser_rw_enter(RUMPRW(rw), op == RW_WRITER);
    140       1.1     pooka }
    141       1.1     pooka 
    142       1.1     pooka int
    143       1.1     pooka rw_tryenter(krwlock_t *rw, const krw_t op)
    144       1.1     pooka {
    145       1.1     pooka 
    146  1.14.4.2      yamt 	return rumpuser_rw_tryenter(RUMPRW(rw), op == RW_WRITER);
    147       1.1     pooka }
    148       1.1     pooka 
    149       1.1     pooka void
    150       1.1     pooka rw_exit(krwlock_t *rw)
    151       1.1     pooka {
    152       1.1     pooka 
    153  1.14.4.2      yamt 	rumpuser_rw_exit(RUMPRW(rw));
    154       1.1     pooka }
    155       1.1     pooka 
    156       1.1     pooka /* always fails */
    157       1.1     pooka int
    158       1.1     pooka rw_tryupgrade(krwlock_t *rw)
    159       1.1     pooka {
    160       1.1     pooka 
    161       1.1     pooka 	return 0;
    162       1.1     pooka }
    163       1.1     pooka 
    164       1.6     pooka int
    165       1.6     pooka rw_write_held(krwlock_t *rw)
    166       1.6     pooka {
    167       1.6     pooka 
    168  1.14.4.2      yamt 	return rumpuser_rw_wrheld(RUMPRW(rw));
    169      1.10        ad }
    170      1.10        ad 
    171      1.10        ad int
    172      1.10        ad rw_read_held(krwlock_t *rw)
    173      1.10        ad {
    174      1.10        ad 
    175  1.14.4.2      yamt 	return rumpuser_rw_rdheld(RUMPRW(rw));
    176      1.10        ad }
    177      1.10        ad 
    178      1.10        ad int
    179      1.10        ad rw_lock_held(krwlock_t *rw)
    180      1.10        ad {
    181      1.10        ad 
    182  1.14.4.2      yamt 	return rumpuser_rw_held(RUMPRW(rw));
    183       1.6     pooka }
    184       1.6     pooka 
    185       1.1     pooka /* curriculum vitaes */
    186       1.1     pooka 
    187  1.14.4.2      yamt #define RUMPCV(cv) (*(struct rumpuser_cv **)(cv))
    188       1.1     pooka 
    189       1.1     pooka void
    190       1.1     pooka cv_init(kcondvar_t *cv, const char *msg)
    191       1.1     pooka {
    192       1.1     pooka 
    193  1.14.4.2      yamt 	CTASSERT(sizeof(kcondvar_t) >= sizeof(void *));
    194  1.14.4.2      yamt 
    195  1.14.4.2      yamt 	rumpuser_cv_init((struct rumpuser_cv **)cv);
    196       1.1     pooka }
    197       1.1     pooka 
    198       1.1     pooka void
    199       1.1     pooka cv_destroy(kcondvar_t *cv)
    200       1.1     pooka {
    201       1.1     pooka 
    202       1.1     pooka 	rumpuser_cv_destroy(RUMPCV(cv));
    203       1.1     pooka }
    204       1.1     pooka 
    205       1.1     pooka void
    206       1.1     pooka cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    207       1.1     pooka {
    208       1.1     pooka 
    209  1.14.4.3      yamt 	if (rump_threads == 0)
    210  1.14.4.3      yamt 		panic("cv_wait without threads");
    211  1.14.4.2      yamt 	rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
    212       1.1     pooka }
    213       1.1     pooka 
    214       1.3     pooka int
    215       1.5     pooka cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    216       1.5     pooka {
    217       1.5     pooka 
    218  1.14.4.2      yamt 	rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
    219       1.5     pooka 	return 0;
    220       1.5     pooka }
    221       1.5     pooka 
    222       1.5     pooka int
    223       1.3     pooka cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
    224       1.3     pooka {
    225  1.14.4.2      yamt 	struct timespec ts, tick;
    226       1.3     pooka 	extern int hz;
    227       1.3     pooka 
    228  1.14.4.2      yamt 	nanotime(&ts);
    229  1.14.4.2      yamt 	tick.tv_sec = ticks / hz;
    230  1.14.4.2      yamt 	tick.tv_nsec = (ticks % hz) * (1000000000/hz);
    231  1.14.4.2      yamt 	timespecadd(&ts, &tick, &ts);
    232  1.14.4.2      yamt 
    233       1.9     pooka 	if (ticks == 0) {
    234       1.9     pooka 		cv_wait(cv, mtx);
    235       1.9     pooka 		return 0;
    236       1.9     pooka 	} else {
    237  1.14.4.3      yamt 		if (rumpuser_cv_timedwait(RUMPCV(cv), RUMPMTX(mtx),
    238  1.14.4.3      yamt 		    ts.tv_sec, ts.tv_nsec))
    239  1.14.4.3      yamt 			return EWOULDBLOCK;
    240  1.14.4.3      yamt 		else
    241  1.14.4.3      yamt 			return 0;
    242       1.9     pooka 	}
    243       1.3     pooka }
    244       1.3     pooka 
    245       1.5     pooka int
    246       1.5     pooka cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks)
    247       1.5     pooka {
    248       1.5     pooka 
    249       1.9     pooka 	return cv_timedwait(cv, mtx, ticks);
    250       1.5     pooka }
    251       1.5     pooka 
    252       1.1     pooka void
    253       1.1     pooka cv_signal(kcondvar_t *cv)
    254       1.1     pooka {
    255       1.1     pooka 
    256       1.1     pooka 	rumpuser_cv_signal(RUMPCV(cv));
    257       1.1     pooka }
    258       1.2     pooka 
    259       1.4     pooka void
    260       1.4     pooka cv_broadcast(kcondvar_t *cv)
    261       1.4     pooka {
    262       1.4     pooka 
    263       1.4     pooka 	rumpuser_cv_broadcast(RUMPCV(cv));
    264       1.4     pooka }
    265       1.4     pooka 
    266  1.14.4.2      yamt bool
    267  1.14.4.2      yamt cv_has_waiters(kcondvar_t *cv)
    268  1.14.4.2      yamt {
    269  1.14.4.2      yamt 
    270  1.14.4.2      yamt 	return rumpuser_cv_has_waiters(RUMPCV(cv));
    271  1.14.4.2      yamt }
    272  1.14.4.2      yamt 
    273  1.14.4.3      yamt /* this is not much of an attempt, but ... */
    274  1.14.4.3      yamt bool
    275  1.14.4.3      yamt cv_is_valid(kcondvar_t *cv)
    276  1.14.4.3      yamt {
    277  1.14.4.3      yamt 
    278  1.14.4.3      yamt 	return RUMPCV(cv) != NULL;
    279  1.14.4.3      yamt }
    280  1.14.4.3      yamt 
    281  1.14.4.2      yamt /*
    282  1.14.4.2      yamt  * giant lock
    283  1.14.4.2      yamt  */
    284       1.2     pooka 
    285  1.14.4.2      yamt static volatile int lockcnt;
    286  1.14.4.3      yamt 
    287  1.14.4.3      yamt bool
    288  1.14.4.3      yamt kernel_biglocked()
    289  1.14.4.3      yamt {
    290  1.14.4.3      yamt 
    291  1.14.4.3      yamt 	return rumpuser_mutex_held(rump_giantlock) && lockcnt > 0;
    292  1.14.4.3      yamt }
    293  1.14.4.3      yamt 
    294  1.14.4.3      yamt void
    295  1.14.4.3      yamt kernel_unlock_allbutone(int *countp)
    296  1.14.4.3      yamt {
    297  1.14.4.3      yamt 	int minusone = lockcnt-1;
    298  1.14.4.3      yamt 
    299  1.14.4.3      yamt 	KASSERT(kernel_biglocked());
    300  1.14.4.3      yamt 	if (minusone) {
    301  1.14.4.3      yamt 		_kernel_unlock(minusone, countp);
    302  1.14.4.3      yamt 	}
    303  1.14.4.3      yamt 	KASSERT(lockcnt == 1);
    304  1.14.4.3      yamt 	*countp = minusone;
    305  1.14.4.3      yamt 
    306  1.14.4.3      yamt 	/*
    307  1.14.4.3      yamt 	 * We drop lockcnt to 0 since rumpuser doesn't know that the
    308  1.14.4.3      yamt 	 * kernel biglock is being used as the interlock for cv in
    309  1.14.4.3      yamt 	 * tsleep.
    310  1.14.4.3      yamt 	 */
    311  1.14.4.3      yamt 	lockcnt = 0;
    312  1.14.4.3      yamt }
    313  1.14.4.3      yamt 
    314  1.14.4.3      yamt void
    315  1.14.4.3      yamt kernel_ununlock_allbutone(int nlocks)
    316  1.14.4.3      yamt {
    317  1.14.4.3      yamt 
    318  1.14.4.3      yamt 	KASSERT(rumpuser_mutex_held(rump_giantlock) && lockcnt == 0);
    319  1.14.4.3      yamt 	lockcnt = 1;
    320  1.14.4.3      yamt 	_kernel_lock(nlocks);
    321  1.14.4.3      yamt }
    322  1.14.4.3      yamt 
    323       1.2     pooka void
    324      1.13  drochner _kernel_lock(int nlocks)
    325       1.2     pooka {
    326       1.2     pooka 
    327  1.14.4.2      yamt 	while (nlocks--) {
    328  1.14.4.3      yamt 		if (!rumpuser_mutex_tryenter(rump_giantlock)) {
    329  1.14.4.3      yamt 			struct lwp *l = curlwp;
    330  1.14.4.3      yamt 
    331  1.14.4.3      yamt 			rump_unschedule_cpu1(l);
    332  1.14.4.3      yamt 			rumpuser_mutex_enter_nowrap(rump_giantlock);
    333  1.14.4.3      yamt 			rump_schedule_cpu(l);
    334  1.14.4.3      yamt 		}
    335  1.14.4.2      yamt 		lockcnt++;
    336  1.14.4.2      yamt 	}
    337       1.2     pooka }
    338       1.2     pooka 
    339       1.2     pooka void
    340      1.13  drochner _kernel_unlock(int nlocks, int *countp)
    341       1.2     pooka {
    342       1.2     pooka 
    343  1.14.4.2      yamt 	if (!rumpuser_mutex_held(rump_giantlock)) {
    344  1.14.4.2      yamt 		KASSERT(nlocks == 0);
    345  1.14.4.2      yamt 		if (countp)
    346  1.14.4.2      yamt 			*countp = 0;
    347  1.14.4.2      yamt 		return;
    348  1.14.4.2      yamt 	}
    349  1.14.4.2      yamt 
    350       1.2     pooka 	if (countp)
    351  1.14.4.2      yamt 		*countp = lockcnt;
    352  1.14.4.2      yamt 	if (nlocks == 0)
    353  1.14.4.2      yamt 		nlocks = lockcnt;
    354  1.14.4.2      yamt 	if (nlocks == -1) {
    355  1.14.4.2      yamt 		KASSERT(lockcnt == 1);
    356  1.14.4.2      yamt 		nlocks = 1;
    357  1.14.4.2      yamt 	}
    358  1.14.4.2      yamt 	KASSERT(nlocks <= lockcnt);
    359  1.14.4.2      yamt 	while (nlocks--) {
    360  1.14.4.2      yamt 		lockcnt--;
    361  1.14.4.2      yamt 		rumpuser_mutex_exit(rump_giantlock);
    362  1.14.4.2      yamt 	}
    363       1.2     pooka }
    364      1.14        ad 
    365  1.14.4.3      yamt void
    366  1.14.4.3      yamt rump_user_unschedule(int nlocks, int *countp)
    367      1.14        ad {
    368      1.14        ad 
    369  1.14.4.3      yamt 	_kernel_unlock(nlocks, countp);
    370  1.14.4.3      yamt 	/*
    371  1.14.4.3      yamt 	 * XXX: technically we should unschedule_cpu1() here, but that
    372  1.14.4.3      yamt 	 * requires rump_intr_enter/exit to be implemented.
    373  1.14.4.3      yamt 	 */
    374  1.14.4.3      yamt 	rump_unschedule_cpu(curlwp);
    375      1.14        ad }
    376      1.14        ad 
    377      1.14        ad void
    378  1.14.4.3      yamt rump_user_schedule(int nlocks)
    379      1.14        ad {
    380      1.14        ad 
    381  1.14.4.3      yamt 	rump_schedule_cpu(curlwp);
    382      1.14        ad 
    383  1.14.4.3      yamt 	if (nlocks)
    384  1.14.4.3      yamt 		_kernel_lock(nlocks);
    385      1.14        ad }
    386