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