Home | History | Annotate | Line # | Download | only in rumpkern
scheduler.c revision 1.23
      1  1.23  pooka /*      $NetBSD: scheduler.c,v 1.23 2010/12/01 20:29:56 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4  1.15  pooka  * Copyright (c) 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.1  pooka #include <sys/cdefs.h>
     29  1.23  pooka __KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.23 2010/12/01 20:29:56 pooka Exp $");
     30   1.1  pooka 
     31   1.1  pooka #include <sys/param.h>
     32  1.16  pooka #include <sys/atomic.h>
     33   1.1  pooka #include <sys/cpu.h>
     34   1.2  pooka #include <sys/kmem.h>
     35   1.1  pooka #include <sys/mutex.h>
     36   1.8  pooka #include <sys/namei.h>
     37   1.1  pooka #include <sys/queue.h>
     38   1.1  pooka #include <sys/select.h>
     39  1.10  pooka #include <sys/systm.h>
     40   1.1  pooka 
     41   1.1  pooka #include <rump/rumpuser.h>
     42   1.1  pooka 
     43   1.1  pooka #include "rump_private.h"
     44   1.1  pooka 
     45   1.8  pooka static struct cpu_info rump_cpus[MAXCPUS];
     46   1.1  pooka static struct rumpcpu {
     47  1.15  pooka 	/* needed in fastpath */
     48   1.1  pooka 	struct cpu_info *rcpu_ci;
     49  1.15  pooka 	void *rcpu_prevlwp;
     50  1.15  pooka 
     51  1.15  pooka 	/* needed in slowpath */
     52  1.15  pooka 	struct rumpuser_mtx *rcpu_mtx;
     53   1.8  pooka 	struct rumpuser_cv *rcpu_cv;
     54  1.15  pooka 	int rcpu_wanted;
     55  1.15  pooka 
     56  1.15  pooka 	/* offset 20 (P=4) or 36 (P=8) here */
     57  1.15  pooka 
     58  1.15  pooka 	/*
     59  1.15  pooka 	 * Some stats.  Not really that necessary, but we should
     60  1.15  pooka 	 * have room.  Note that these overflow quite fast, so need
     61  1.15  pooka 	 * to be collected often.
     62  1.15  pooka 	 */
     63  1.15  pooka 	unsigned int rcpu_fastpath;
     64  1.15  pooka 	unsigned int rcpu_slowpath;
     65  1.15  pooka 	unsigned int rcpu_migrated;
     66  1.15  pooka 
     67  1.15  pooka 	/* offset 32 (P=4) or 50 (P=8) */
     68  1.15  pooka 
     69  1.15  pooka 	int rcpu_align[0] __aligned(CACHE_LINE_SIZE);
     70   1.8  pooka } rcpu_storage[MAXCPUS];
     71   1.1  pooka struct cpu_info *rump_cpu = &rump_cpus[0];
     72  1.12  pooka int ncpu;
     73   1.1  pooka 
     74  1.15  pooka #define RCPULWP_BUSY	((void *)-1)
     75  1.15  pooka #define RCPULWP_WANTED	((void *)-2)
     76   1.8  pooka 
     77  1.15  pooka static struct rumpuser_mtx *lwp0mtx;
     78  1.15  pooka static struct rumpuser_cv *lwp0cv;
     79  1.15  pooka static unsigned nextcpu;
     80  1.14  pooka 
     81  1.19  pooka static bool lwp0isbusy = false;
     82   1.3  pooka 
     83  1.15  pooka /*
     84  1.15  pooka  * Keep some stats.
     85  1.15  pooka  *
     86  1.15  pooka  * Keeping track of there is not really critical for speed, unless
     87  1.15  pooka  * stats happen to be on a different cache line (CACHE_LINE_SIZE is
     88  1.15  pooka  * really just a coarse estimate), so default for the performant case
     89  1.15  pooka  * (i.e. no stats).
     90  1.15  pooka  */
     91  1.15  pooka #ifdef RUMPSCHED_STATS
     92  1.15  pooka #define SCHED_FASTPATH(rcpu) rcpu->rcpu_fastpath++;
     93  1.15  pooka #define SCHED_SLOWPATH(rcpu) rcpu->rcpu_slowpath++;
     94  1.15  pooka #define SCHED_MIGRATED(rcpu) rcpu->rcpu_migrated++;
     95  1.15  pooka #else
     96  1.15  pooka #define SCHED_FASTPATH(rcpu)
     97  1.15  pooka #define SCHED_SLOWPATH(rcpu)
     98  1.15  pooka #define SCHED_MIGRATED(rcpu)
     99  1.15  pooka #endif
    100   1.1  pooka 
    101   1.1  pooka struct cpu_info *
    102   1.1  pooka cpu_lookup(u_int index)
    103   1.1  pooka {
    104   1.1  pooka 
    105   1.1  pooka 	return &rump_cpus[index];
    106   1.1  pooka }
    107   1.1  pooka 
    108  1.15  pooka static inline struct rumpcpu *
    109  1.15  pooka getnextcpu(void)
    110  1.15  pooka {
    111  1.15  pooka 	unsigned newcpu;
    112  1.15  pooka 
    113  1.15  pooka 	newcpu = atomic_inc_uint_nv(&nextcpu);
    114  1.15  pooka 	if (__predict_false(ncpu > UINT_MAX/2))
    115  1.15  pooka 		atomic_and_uint(&nextcpu, 0);
    116  1.15  pooka 	newcpu = newcpu % ncpu;
    117  1.15  pooka 
    118  1.15  pooka 	return &rcpu_storage[newcpu];
    119  1.15  pooka }
    120  1.15  pooka 
    121  1.12  pooka /* this could/should be mi_attach_cpu? */
    122  1.12  pooka void
    123  1.22  pooka rump_cpus_bootstrap(int *nump)
    124  1.12  pooka {
    125  1.12  pooka 	struct rumpcpu *rcpu;
    126  1.12  pooka 	struct cpu_info *ci;
    127  1.22  pooka 	int num = *nump;
    128  1.12  pooka 	int i;
    129  1.12  pooka 
    130  1.13  pooka 	if (num > MAXCPUS) {
    131  1.22  pooka 		aprint_verbose("CPU limit: %d wanted, %d (MAXCPUS) "
    132  1.22  pooka 		    "available (adjusted)\n", num, MAXCPUS);
    133  1.13  pooka 		num = MAXCPUS;
    134  1.13  pooka 	}
    135  1.13  pooka 
    136  1.12  pooka 	for (i = 0; i < num; i++) {
    137  1.12  pooka 		rcpu = &rcpu_storage[i];
    138  1.12  pooka 		ci = &rump_cpus[i];
    139  1.12  pooka 		ci->ci_index = i;
    140  1.12  pooka 	}
    141  1.20  pooka 
    142  1.20  pooka 	/* attach first cpu for bootstrap */
    143  1.20  pooka 	rump_cpu_attach(&rump_cpus[0]);
    144  1.20  pooka 	ncpu = 1;
    145  1.22  pooka 	*nump = num;
    146  1.12  pooka }
    147  1.12  pooka 
    148   1.1  pooka void
    149  1.20  pooka rump_scheduler_init(int numcpu)
    150   1.1  pooka {
    151   1.1  pooka 	struct rumpcpu *rcpu;
    152   1.1  pooka 	struct cpu_info *ci;
    153   1.1  pooka 	int i;
    154   1.1  pooka 
    155  1.15  pooka 	rumpuser_mutex_init(&lwp0mtx);
    156   1.3  pooka 	rumpuser_cv_init(&lwp0cv);
    157  1.20  pooka 	for (i = 0; i < numcpu; i++) {
    158   1.1  pooka 		rcpu = &rcpu_storage[i];
    159   1.1  pooka 		ci = &rump_cpus[i];
    160  1.12  pooka 		rcpu->rcpu_ci = ci;
    161   1.4  pooka 		ci->ci_schedstate.spc_mutex =
    162   1.4  pooka 		    mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    163   1.9  pooka 		ci->ci_schedstate.spc_flags = SPCF_RUNNING;
    164  1.15  pooka 		rcpu->rcpu_wanted = 0;
    165   1.8  pooka 		rumpuser_cv_init(&rcpu->rcpu_cv);
    166  1.15  pooka 		rumpuser_mutex_init(&rcpu->rcpu_mtx);
    167   1.1  pooka 	}
    168   1.1  pooka }
    169   1.1  pooka 
    170  1.14  pooka /*
    171  1.14  pooka  * condvar ops using scheduler lock as the rumpuser interlock.
    172  1.14  pooka  */
    173  1.14  pooka void
    174  1.14  pooka rump_schedlock_cv_wait(struct rumpuser_cv *cv)
    175  1.14  pooka {
    176  1.15  pooka 	struct lwp *l = curlwp;
    177  1.15  pooka 	struct rumpcpu *rcpu = &rcpu_storage[l->l_cpu-&rump_cpus[0]];
    178  1.14  pooka 
    179  1.15  pooka 	/* mutex will be taken and released in cpu schedule/unschedule */
    180  1.15  pooka 	rumpuser_cv_wait(cv, rcpu->rcpu_mtx);
    181  1.14  pooka }
    182  1.14  pooka 
    183  1.14  pooka int
    184  1.14  pooka rump_schedlock_cv_timedwait(struct rumpuser_cv *cv, const struct timespec *ts)
    185  1.14  pooka {
    186  1.15  pooka 	struct lwp *l = curlwp;
    187  1.15  pooka 	struct rumpcpu *rcpu = &rcpu_storage[l->l_cpu-&rump_cpus[0]];
    188  1.14  pooka 
    189  1.15  pooka 	/* mutex will be taken and released in cpu schedule/unschedule */
    190  1.15  pooka 	return rumpuser_cv_timedwait(cv, rcpu->rcpu_mtx,
    191  1.15  pooka 	    ts->tv_sec, ts->tv_nsec);
    192  1.14  pooka }
    193  1.14  pooka 
    194  1.19  pooka static void
    195  1.19  pooka lwp0busy(void)
    196  1.19  pooka {
    197  1.19  pooka 
    198  1.19  pooka 	/* busy lwp0 */
    199  1.19  pooka 	KASSERT(curlwp == NULL || curlwp->l_cpu == NULL);
    200  1.19  pooka 	rumpuser_mutex_enter_nowrap(lwp0mtx);
    201  1.19  pooka 	while (lwp0isbusy)
    202  1.19  pooka 		rumpuser_cv_wait_nowrap(lwp0cv, lwp0mtx);
    203  1.19  pooka 	lwp0isbusy = true;
    204  1.19  pooka 	rumpuser_mutex_exit(lwp0mtx);
    205  1.19  pooka }
    206  1.19  pooka 
    207  1.19  pooka static void
    208  1.19  pooka lwp0rele(void)
    209  1.19  pooka {
    210  1.19  pooka 
    211  1.19  pooka 	rumpuser_mutex_enter_nowrap(lwp0mtx);
    212  1.19  pooka 	KASSERT(lwp0isbusy == true);
    213  1.19  pooka 	lwp0isbusy = false;
    214  1.19  pooka 	rumpuser_cv_signal(lwp0cv);
    215  1.19  pooka 	rumpuser_mutex_exit(lwp0mtx);
    216  1.19  pooka }
    217  1.19  pooka 
    218   1.1  pooka void
    219   1.1  pooka rump_schedule()
    220   1.1  pooka {
    221   1.3  pooka 	struct lwp *l;
    222   1.2  pooka 
    223   1.2  pooka 	/*
    224   1.2  pooka 	 * If there is no dedicated lwp, allocate a temp one and
    225   1.3  pooka 	 * set it to be free'd upon unschedule().  Use lwp0 context
    226  1.15  pooka 	 * for reserving the necessary resources.  Don't optimize
    227  1.15  pooka 	 * for this case -- anyone who cares about performance will
    228  1.15  pooka 	 * start a real thread.
    229   1.2  pooka 	 */
    230  1.19  pooka 	if (__predict_true((l = rumpuser_get_curlwp()) != NULL)) {
    231  1.19  pooka 		rump_schedule_cpu(l);
    232  1.19  pooka 		LWP_CACHE_CREDS(l, l->l_proc);
    233  1.19  pooka 	} else {
    234  1.19  pooka 		lwp0busy();
    235   1.3  pooka 
    236   1.3  pooka 		/* schedule cpu and use lwp0 */
    237   1.4  pooka 		rump_schedule_cpu(&lwp0);
    238   1.3  pooka 		rumpuser_set_curlwp(&lwp0);
    239   1.3  pooka 
    240  1.19  pooka 		/* allocate thread, switch to it, and release lwp0 */
    241  1.21  pooka 		l = rump__lwproc_alloclwp(initproc);
    242  1.19  pooka 		rump_lwproc_switch(l);
    243  1.19  pooka 		lwp0rele();
    244   1.3  pooka 
    245  1.19  pooka 		/*
    246  1.19  pooka 		 * mark new thread dead-on-unschedule.  this
    247  1.19  pooka 		 * means that we'll be running with l_refcnt == 0.
    248  1.19  pooka 		 * relax, it's fine.
    249  1.19  pooka 		 */
    250  1.19  pooka 		rump_lwproc_releaselwp();
    251   1.2  pooka 	}
    252   1.2  pooka }
    253   1.2  pooka 
    254   1.4  pooka void
    255   1.4  pooka rump_schedule_cpu(struct lwp *l)
    256   1.2  pooka {
    257  1.14  pooka 
    258  1.14  pooka 	rump_schedule_cpu_interlock(l, NULL);
    259  1.14  pooka }
    260  1.14  pooka 
    261  1.15  pooka /*
    262  1.15  pooka  * Schedule a CPU.  This optimizes for the case where we schedule
    263  1.15  pooka  * the same thread often, and we have nCPU >= nFrequently-Running-Thread
    264  1.15  pooka  * (where CPU is virtual rump cpu, not host CPU).
    265  1.15  pooka  */
    266  1.14  pooka void
    267  1.14  pooka rump_schedule_cpu_interlock(struct lwp *l, void *interlock)
    268  1.14  pooka {
    269   1.1  pooka 	struct rumpcpu *rcpu;
    270  1.15  pooka 	void *old;
    271  1.15  pooka 	bool domigrate;
    272  1.15  pooka 	bool bound = l->l_pflag & LP_BOUND;
    273  1.15  pooka 
    274  1.15  pooka 	/*
    275  1.15  pooka 	 * First, try fastpath: if we were the previous user of the
    276  1.15  pooka 	 * CPU, everything is in order cachewise and we can just
    277  1.15  pooka 	 * proceed to use it.
    278  1.15  pooka 	 *
    279  1.15  pooka 	 * If we are a different thread (i.e. CAS fails), we must go
    280  1.15  pooka 	 * through a memory barrier to ensure we get a truthful
    281  1.15  pooka 	 * view of the world.
    282  1.15  pooka 	 */
    283  1.14  pooka 
    284  1.17  pooka 	KASSERT(l->l_target_cpu != NULL);
    285  1.15  pooka 	rcpu = &rcpu_storage[l->l_target_cpu-&rump_cpus[0]];
    286  1.15  pooka 	if (atomic_cas_ptr(&rcpu->rcpu_prevlwp, l, RCPULWP_BUSY) == l) {
    287  1.15  pooka 		if (__predict_true(interlock == rcpu->rcpu_mtx))
    288  1.15  pooka 			rumpuser_mutex_exit(rcpu->rcpu_mtx);
    289  1.15  pooka 		SCHED_FASTPATH(rcpu);
    290  1.15  pooka 		/* jones, you're the man */
    291  1.15  pooka 		goto fastlane;
    292  1.15  pooka 	}
    293   1.1  pooka 
    294  1.15  pooka 	/*
    295  1.15  pooka 	 * Else, it's the slowpath for us.  First, determine if we
    296  1.15  pooka 	 * can migrate.
    297  1.15  pooka 	 */
    298  1.15  pooka 	if (ncpu == 1)
    299  1.15  pooka 		domigrate = false;
    300  1.15  pooka 	else
    301  1.15  pooka 		domigrate = true;
    302  1.15  pooka 
    303  1.15  pooka 	/* Take lock.  This acts as a load barrier too. */
    304  1.15  pooka 	if (__predict_true(interlock != rcpu->rcpu_mtx))
    305  1.15  pooka 		rumpuser_mutex_enter_nowrap(rcpu->rcpu_mtx);
    306  1.15  pooka 
    307  1.15  pooka 	for (;;) {
    308  1.15  pooka 		SCHED_SLOWPATH(rcpu);
    309  1.15  pooka 		old = atomic_swap_ptr(&rcpu->rcpu_prevlwp, RCPULWP_WANTED);
    310  1.15  pooka 
    311  1.15  pooka 		/* CPU is free? */
    312  1.15  pooka 		if (old != RCPULWP_BUSY && old != RCPULWP_WANTED) {
    313  1.15  pooka 			if (atomic_cas_ptr(&rcpu->rcpu_prevlwp,
    314  1.15  pooka 			    RCPULWP_WANTED, RCPULWP_BUSY) == RCPULWP_WANTED) {
    315  1.15  pooka 				break;
    316   1.8  pooka 			}
    317   1.8  pooka 		}
    318  1.15  pooka 
    319  1.15  pooka 		/*
    320  1.15  pooka 		 * Do we want to migrate once?
    321  1.15  pooka 		 * This may need a slightly better algorithm, or we
    322  1.15  pooka 		 * might cache pingpong eternally for non-frequent
    323  1.15  pooka 		 * threads.
    324  1.15  pooka 		 */
    325  1.15  pooka 		if (domigrate && !bound) {
    326  1.15  pooka 			domigrate = false;
    327  1.15  pooka 			SCHED_MIGRATED(rcpu);
    328  1.15  pooka 			rumpuser_mutex_exit(rcpu->rcpu_mtx);
    329  1.15  pooka 			rcpu = getnextcpu();
    330  1.15  pooka 			rumpuser_mutex_enter_nowrap(rcpu->rcpu_mtx);
    331  1.15  pooka 			continue;
    332   1.8  pooka 		}
    333  1.15  pooka 
    334  1.15  pooka 		/* Want CPU, wait until it's released an retry */
    335  1.15  pooka 		rcpu->rcpu_wanted++;
    336  1.15  pooka 		rumpuser_cv_wait_nowrap(rcpu->rcpu_cv, rcpu->rcpu_mtx);
    337  1.15  pooka 		rcpu->rcpu_wanted--;
    338   1.8  pooka 	}
    339  1.15  pooka 	rumpuser_mutex_exit(rcpu->rcpu_mtx);
    340  1.15  pooka 
    341  1.15  pooka  fastlane:
    342  1.15  pooka 	l->l_cpu = l->l_target_cpu = rcpu->rcpu_ci;
    343   1.4  pooka 	l->l_mutex = rcpu->rcpu_ci->ci_schedstate.spc_mutex;
    344  1.18  pooka 	l->l_ncsw++;
    345  1.23  pooka 
    346  1.23  pooka 	rcpu->rcpu_ci->ci_curlwp = l;
    347   1.1  pooka }
    348   1.1  pooka 
    349   1.1  pooka void
    350   1.1  pooka rump_unschedule()
    351   1.1  pooka {
    352   1.2  pooka 	struct lwp *l;
    353   1.2  pooka 
    354   1.2  pooka 	l = rumpuser_get_curlwp();
    355   1.4  pooka 	KASSERT(l->l_mutex == l->l_cpu->ci_schedstate.spc_mutex);
    356   1.2  pooka 	rump_unschedule_cpu(l);
    357   1.4  pooka 	l->l_mutex = NULL;
    358   1.6  pooka 
    359   1.6  pooka 	/*
    360  1.19  pooka 	 * Check special conditions:
    361  1.19  pooka 	 *  1) do we need to free the lwp which just unscheduled?
    362  1.19  pooka 	 *     (locking order: lwp0, cpu)
    363  1.19  pooka 	 *  2) do we want to clear curlwp for the current host thread
    364   1.6  pooka 	 */
    365  1.19  pooka 	if (__predict_false(l->l_flag & LW_WEXIT)) {
    366  1.19  pooka 		lwp0busy();
    367  1.19  pooka 
    368  1.19  pooka 		/* Now that we have lwp0, we can schedule a CPU again */
    369  1.19  pooka 		rump_schedule_cpu(l);
    370   1.6  pooka 
    371  1.19  pooka 		/* switch to lwp0.  this frees the old thread */
    372  1.19  pooka 		KASSERT(l->l_flag & LW_WEXIT);
    373  1.19  pooka 		rump_lwproc_switch(&lwp0);
    374   1.6  pooka 
    375  1.19  pooka 		/* release lwp0 */
    376   1.6  pooka 		rump_unschedule_cpu(&lwp0);
    377  1.19  pooka 		lwp0.l_mutex = NULL;
    378  1.19  pooka 		lwp0.l_pflag &= ~LP_RUNNING;
    379  1.19  pooka 		lwp0rele();
    380   1.6  pooka 		rumpuser_set_curlwp(NULL);
    381   1.6  pooka 
    382  1.19  pooka 	} else if (__predict_false(l->l_flag & LW_RUMP_CLEAR)) {
    383  1.19  pooka 		rumpuser_set_curlwp(NULL);
    384  1.19  pooka 		l->l_flag &= ~LW_RUMP_CLEAR;
    385   1.2  pooka 	}
    386   1.2  pooka }
    387   1.2  pooka 
    388   1.2  pooka void
    389   1.2  pooka rump_unschedule_cpu(struct lwp *l)
    390   1.2  pooka {
    391   1.8  pooka 
    392  1.14  pooka 	rump_unschedule_cpu_interlock(l, NULL);
    393  1.14  pooka }
    394  1.14  pooka 
    395  1.14  pooka void
    396  1.14  pooka rump_unschedule_cpu_interlock(struct lwp *l, void *interlock)
    397  1.14  pooka {
    398  1.14  pooka 
    399   1.8  pooka 	if ((l->l_pflag & LP_INTR) == 0)
    400   1.8  pooka 		rump_softint_run(l->l_cpu);
    401  1.14  pooka 	rump_unschedule_cpu1(l, interlock);
    402   1.8  pooka }
    403   1.8  pooka 
    404   1.8  pooka void
    405  1.14  pooka rump_unschedule_cpu1(struct lwp *l, void *interlock)
    406   1.8  pooka {
    407   1.1  pooka 	struct rumpcpu *rcpu;
    408   1.1  pooka 	struct cpu_info *ci;
    409  1.15  pooka 	void *old;
    410   1.1  pooka 
    411   1.1  pooka 	ci = l->l_cpu;
    412  1.23  pooka 	ci->ci_curlwp = NULL;
    413  1.15  pooka 	l->l_cpu = NULL;
    414   1.1  pooka 	rcpu = &rcpu_storage[ci-&rump_cpus[0]];
    415  1.15  pooka 
    416   1.1  pooka 	KASSERT(rcpu->rcpu_ci == ci);
    417   1.1  pooka 
    418  1.15  pooka 	/*
    419  1.15  pooka 	 * Make sure all stores are seen before the CPU release.  This
    420  1.15  pooka 	 * is relevant only in the non-fastpath scheduling case, but
    421  1.15  pooka 	 * we don't know here if that's going to happen, so need to
    422  1.15  pooka 	 * expect the worst.
    423  1.15  pooka 	 */
    424  1.15  pooka 	membar_exit();
    425  1.15  pooka 
    426  1.15  pooka 	/* Release the CPU. */
    427  1.15  pooka 	old = atomic_swap_ptr(&rcpu->rcpu_prevlwp, l);
    428  1.15  pooka 
    429  1.15  pooka 	/* No waiters?  No problems.  We're outta here. */
    430  1.15  pooka 	if (old == RCPULWP_BUSY) {
    431  1.15  pooka 		/* Was the scheduler interlock requested? */
    432  1.15  pooka 		if (__predict_false(interlock == rcpu->rcpu_mtx))
    433  1.15  pooka 			rumpuser_mutex_enter_nowrap(rcpu->rcpu_mtx);
    434  1.15  pooka 		return;
    435  1.15  pooka 	}
    436  1.15  pooka 
    437  1.15  pooka 	KASSERT(old == RCPULWP_WANTED);
    438  1.15  pooka 
    439  1.15  pooka 	/*
    440  1.15  pooka 	 * Ok, things weren't so snappy.
    441  1.15  pooka 	 *
    442  1.15  pooka 	 * Snailpath: take lock and signal anyone waiting for this CPU.
    443  1.15  pooka 	 */
    444  1.14  pooka 
    445  1.15  pooka 	rumpuser_mutex_enter_nowrap(rcpu->rcpu_mtx);
    446  1.15  pooka 	if (rcpu->rcpu_wanted)
    447   1.8  pooka 		rumpuser_cv_broadcast(rcpu->rcpu_cv);
    448  1.14  pooka 
    449  1.15  pooka 	if (__predict_true(interlock != rcpu->rcpu_mtx))
    450  1.15  pooka 		rumpuser_mutex_exit(rcpu->rcpu_mtx);
    451   1.1  pooka }
    452   1.5  pooka 
    453   1.5  pooka /* Give up and retake CPU (perhaps a different one) */
    454   1.5  pooka void
    455   1.5  pooka yield()
    456   1.5  pooka {
    457   1.5  pooka 	struct lwp *l = curlwp;
    458   1.5  pooka 	int nlocks;
    459   1.5  pooka 
    460   1.5  pooka 	KERNEL_UNLOCK_ALL(l, &nlocks);
    461   1.5  pooka 	rump_unschedule_cpu(l);
    462   1.5  pooka 	rump_schedule_cpu(l);
    463   1.5  pooka 	KERNEL_LOCK(nlocks, l);
    464   1.5  pooka }
    465   1.5  pooka 
    466   1.5  pooka void
    467   1.5  pooka preempt()
    468   1.5  pooka {
    469   1.5  pooka 
    470   1.5  pooka 	yield();
    471   1.5  pooka }
    472  1.10  pooka 
    473  1.10  pooka bool
    474  1.10  pooka kpreempt(uintptr_t where)
    475  1.10  pooka {
    476  1.10  pooka 
    477  1.10  pooka 	return false;
    478  1.10  pooka }
    479  1.10  pooka 
    480  1.10  pooka /*
    481  1.10  pooka  * There is no kernel thread preemption in rump currently.  But call
    482  1.10  pooka  * the implementing macros anyway in case they grow some side-effects
    483  1.10  pooka  * down the road.
    484  1.10  pooka  */
    485  1.10  pooka void
    486  1.10  pooka kpreempt_disable(void)
    487  1.10  pooka {
    488  1.10  pooka 
    489  1.10  pooka 	KPREEMPT_DISABLE(curlwp);
    490  1.10  pooka }
    491  1.10  pooka 
    492  1.10  pooka void
    493  1.10  pooka kpreempt_enable(void)
    494  1.10  pooka {
    495  1.10  pooka 
    496  1.10  pooka 	KPREEMPT_ENABLE(curlwp);
    497  1.10  pooka }
    498  1.10  pooka 
    499  1.10  pooka void
    500  1.10  pooka suspendsched(void)
    501  1.10  pooka {
    502  1.10  pooka 
    503  1.10  pooka 	/*
    504  1.10  pooka 	 * Could wait until everyone is out and block further entries,
    505  1.10  pooka 	 * but skip that for now.
    506  1.10  pooka 	 */
    507  1.10  pooka }
    508  1.11  pooka 
    509  1.11  pooka void
    510  1.11  pooka sched_nice(struct proc *p, int level)
    511  1.11  pooka {
    512  1.11  pooka 
    513  1.11  pooka 	/* nothing to do for now */
    514  1.11  pooka }
    515