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