Home | History | Annotate | Line # | Download | only in rumpkern
ltsleep.c revision 1.34.24.1
      1  1.34.24.1     ad /*	$NetBSD: ltsleep.c,v 1.34.24.1 2020/02/29 20:21:09 ad Exp $	*/
      2        1.1  pooka 
      3        1.1  pooka /*
      4       1.27  pooka  * Copyright (c) 2009, 2010 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.22  pooka /*
     29       1.29  rmind  * Implementation of the tsleep/mtsleep kernel sleep interface.  There
     30       1.27  pooka  * are two sides to our implementation.  For historic spinlocks we
     31       1.27  pooka  * assume the kernel is giantlocked and use kernel giantlock as the
     32       1.27  pooka  * wait interlock.  For mtsleep, we use the interlock supplied by
     33       1.27  pooka  * the caller.  This duality leads to some if/else messiness in the code ...
     34       1.22  pooka  */
     35       1.22  pooka 
     36        1.9  pooka #include <sys/cdefs.h>
     37  1.34.24.1     ad __KERNEL_RCSID(0, "$NetBSD: ltsleep.c,v 1.34.24.1 2020/02/29 20:21:09 ad Exp $");
     38        1.9  pooka 
     39        1.1  pooka #include <sys/param.h>
     40       1.20  pooka #include <sys/kernel.h>
     41        1.1  pooka #include <sys/proc.h>
     42        1.1  pooka #include <sys/queue.h>
     43        1.1  pooka 
     44       1.34  pooka #include <rump-sys/kern.h>
     45       1.34  pooka 
     46        1.7  pooka #include <rump/rumpuser.h>
     47        1.7  pooka 
     48        1.1  pooka struct ltsleeper {
     49        1.1  pooka 	wchan_t id;
     50       1.27  pooka 	union {
     51       1.27  pooka 		struct rumpuser_cv *user;
     52       1.27  pooka 		kcondvar_t kern;
     53       1.27  pooka 	} u;
     54       1.27  pooka 	bool iskwait;
     55        1.1  pooka 	LIST_ENTRY(ltsleeper) entries;
     56        1.1  pooka };
     57       1.27  pooka #define ucv u.user
     58       1.27  pooka #define kcv u.kern
     59        1.1  pooka 
     60        1.1  pooka static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
     61       1.33  pooka static kmutex_t qlock;
     62        1.1  pooka 
     63       1.20  pooka static int
     64  1.34.24.1     ad sleeper(wchan_t ident, int timo, bool kinterlock)
     65       1.20  pooka {
     66       1.27  pooka 	struct ltsleeper lts;
     67       1.31  pooka 	struct timespec ts;
     68       1.27  pooka 	int rv;
     69       1.27  pooka 
     70       1.27  pooka 	lts.id = ident;
     71       1.27  pooka 	if (kinterlock) {
     72       1.27  pooka 		lts.iskwait = true;
     73       1.27  pooka 		cv_init(&lts.kcv, "mtsleep");
     74       1.27  pooka 	} else {
     75       1.27  pooka 		lts.iskwait = false;
     76       1.27  pooka 		rumpuser_cv_init(&lts.ucv);
     77       1.27  pooka 	}
     78       1.20  pooka 
     79       1.27  pooka 	LIST_INSERT_HEAD(&sleepers, &lts, entries);
     80       1.20  pooka 
     81       1.20  pooka 	if (timo) {
     82       1.27  pooka 		if (kinterlock) {
     83  1.34.24.1     ad 			rv = cv_timedwait(&lts.kcv, &qlock, timo);
     84       1.27  pooka 		} else {
     85       1.27  pooka 			/*
     86       1.27  pooka 			 * Calculate wakeup-time.
     87       1.27  pooka 			 */
     88       1.31  pooka 			ts.tv_sec = timo / hz;
     89       1.31  pooka 			ts.tv_nsec = (timo % hz) * (1000000000/hz);
     90  1.34.24.1     ad 			mutex_spin_exit(&qlock);
     91       1.27  pooka 			rv = rumpuser_cv_timedwait(lts.ucv, rump_giantlock,
     92       1.27  pooka 			    ts.tv_sec, ts.tv_nsec);
     93  1.34.24.1     ad 			mutex_spin_enter(&qlock);
     94       1.27  pooka 		}
     95       1.27  pooka 
     96       1.27  pooka 		if (rv != 0)
     97       1.21  pooka 			rv = EWOULDBLOCK;
     98       1.20  pooka 	} else {
     99       1.27  pooka 		if (kinterlock) {
    100  1.34.24.1     ad 			cv_wait(&lts.kcv, &qlock);
    101       1.27  pooka 		} else {
    102  1.34.24.1     ad 			mutex_spin_exit(&qlock);
    103       1.27  pooka 			rumpuser_cv_wait(lts.ucv, rump_giantlock);
    104  1.34.24.1     ad 			mutex_spin_enter(&qlock);
    105       1.27  pooka 		}
    106       1.20  pooka 		rv = 0;
    107       1.20  pooka 	}
    108       1.20  pooka 
    109       1.27  pooka 	LIST_REMOVE(&lts, entries);
    110  1.34.24.1     ad 	mutex_spin_exit(&qlock);
    111       1.27  pooka 
    112       1.27  pooka 	if (kinterlock)
    113       1.27  pooka 		cv_destroy(&lts.kcv);
    114       1.27  pooka 	else
    115       1.27  pooka 		rumpuser_cv_destroy(lts.ucv);
    116       1.20  pooka 
    117       1.20  pooka 	return rv;
    118       1.20  pooka }
    119       1.20  pooka 
    120        1.1  pooka int
    121       1.29  rmind tsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo)
    122        1.1  pooka {
    123       1.27  pooka 	int rv, nlocks;
    124        1.1  pooka 
    125       1.27  pooka 	/*
    126       1.27  pooka 	 * Since we cannot use slock as the rumpuser interlock,
    127       1.27  pooka 	 * require that everyone using this prehistoric interface
    128       1.28  pooka 	 * is biglocked.  Wrap around the biglock and drop lockcnt,
    129       1.28  pooka 	 * but retain the rumpuser mutex so that we can use it as an
    130       1.28  pooka 	 * interlock to rumpuser_cv_wait().
    131       1.27  pooka 	 */
    132       1.28  pooka 	rump_kernel_bigwrap(&nlocks);
    133  1.34.24.1     ad 	mutex_spin_enter(&qlock);
    134  1.34.24.1     ad 	rv = sleeper(ident, timo, false);
    135       1.28  pooka 	rump_kernel_bigunwrap(nlocks);
    136       1.16  pooka 
    137       1.20  pooka 	return rv;
    138        1.1  pooka }
    139        1.1  pooka 
    140        1.4     ad int
    141       1.29  rmind mtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo, kmutex_t *lock)
    142        1.4     ad {
    143       1.20  pooka 	int rv;
    144       1.15  pooka 
    145  1.34.24.1     ad 	mutex_spin_enter(&qlock);
    146  1.34.24.1     ad 	mutex_exit(lock);
    147  1.34.24.1     ad 	rv = sleeper(ident, timo, true);
    148  1.34.24.1     ad 	if ((prio & PNORELOCK) == 0)
    149  1.34.24.1     ad 		mutex_enter(lock);
    150  1.34.24.1     ad 
    151  1.34.24.1     ad 	return rv;
    152  1.34.24.1     ad }
    153  1.34.24.1     ad 
    154  1.34.24.1     ad int
    155  1.34.24.1     ad rwtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo, krwlock_t *lock)
    156  1.34.24.1     ad {
    157  1.34.24.1     ad 	krw_t op = rw_write_held(lock) ? RW_WRITER : RW_READER;
    158  1.34.24.1     ad 	int rv;
    159  1.34.24.1     ad 
    160  1.34.24.1     ad 	mutex_spin_enter(&qlock);
    161  1.34.24.1     ad 	rw_exit(lock);
    162  1.34.24.1     ad 	rv = sleeper(ident, timo, true);
    163  1.34.24.1     ad 	if ((prio & PNORELOCK) == 0)
    164  1.34.24.1     ad 		rw_enter(lock, op);
    165        1.4     ad 
    166       1.20  pooka 	return rv;
    167        1.4     ad }
    168        1.4     ad 
    169       1.29  rmind void
    170       1.29  rmind wakeup(wchan_t ident)
    171        1.1  pooka {
    172        1.1  pooka 	struct ltsleeper *ltsp;
    173        1.1  pooka 
    174       1.32  pooka 	mutex_spin_enter(&qlock);
    175       1.17  pooka 	LIST_FOREACH(ltsp, &sleepers, entries) {
    176       1.17  pooka 		if (ltsp->id == ident) {
    177       1.29  rmind 			if (ltsp->iskwait) {
    178       1.29  rmind 				cv_broadcast(&ltsp->kcv);
    179       1.27  pooka 			} else {
    180       1.29  rmind 				rumpuser_cv_broadcast(ltsp->ucv);
    181       1.27  pooka 			}
    182       1.17  pooka 		}
    183       1.17  pooka 	}
    184       1.32  pooka 	mutex_exit(&qlock);
    185        1.1  pooka }
    186        1.1  pooka 
    187        1.1  pooka void
    188       1.27  pooka rump_tsleep_init()
    189       1.27  pooka {
    190       1.27  pooka 
    191       1.32  pooka 	mutex_init(&qlock, MUTEX_SPIN, IPL_NONE);
    192       1.12  pooka }
    193