Home | History | Annotate | Line # | Download | only in rumpkern
scheduler.c revision 1.14
      1  1.14  pooka /*      $NetBSD: scheduler.c,v 1.14 2010/05/18 14:58:42 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4   1.1  pooka  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
      5   1.1  pooka  *
      6   1.1  pooka  * Development of this software was supported by
      7   1.1  pooka  * The 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.1  pooka #include <sys/cdefs.h>
     32  1.14  pooka __KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.14 2010/05/18 14:58:42 pooka Exp $");
     33   1.1  pooka 
     34   1.1  pooka #include <sys/param.h>
     35   1.1  pooka #include <sys/cpu.h>
     36   1.2  pooka #include <sys/kmem.h>
     37   1.1  pooka #include <sys/mutex.h>
     38   1.8  pooka #include <sys/namei.h>
     39   1.1  pooka #include <sys/queue.h>
     40   1.1  pooka #include <sys/select.h>
     41  1.10  pooka #include <sys/systm.h>
     42   1.1  pooka 
     43   1.1  pooka #include <rump/rumpuser.h>
     44   1.1  pooka 
     45   1.1  pooka #include "rump_private.h"
     46   1.1  pooka 
     47   1.1  pooka /* should go for MAXCPUS at some point */
     48   1.8  pooka static struct cpu_info rump_cpus[MAXCPUS];
     49   1.1  pooka static struct rumpcpu {
     50   1.1  pooka 	struct cpu_info *rcpu_ci;
     51   1.8  pooka 	int rcpu_flags;
     52   1.8  pooka 	struct rumpuser_cv *rcpu_cv;
     53   1.8  pooka 	LIST_ENTRY(rumpcpu) rcpu_entries;
     54   1.8  pooka } rcpu_storage[MAXCPUS];
     55   1.1  pooka struct cpu_info *rump_cpu = &rump_cpus[0];
     56  1.12  pooka int ncpu;
     57   1.1  pooka 
     58   1.8  pooka #define RCPU_WANTED	0x01	/* someone wants this specific CPU */
     59   1.8  pooka #define RCPU_BUSY	0x02	/* CPU is busy */
     60   1.8  pooka #define RCPU_FREELIST	0x04	/* CPU is on freelist */
     61   1.8  pooka 
     62   1.8  pooka static LIST_HEAD(,rumpcpu) cpu_freelist = LIST_HEAD_INITIALIZER(cpu_freelist);
     63  1.14  pooka 
     64   1.1  pooka static struct rumpuser_mtx *schedmtx;
     65   1.3  pooka static struct rumpuser_cv *schedcv, *lwp0cv;
     66   1.3  pooka 
     67   1.3  pooka static bool lwp0busy = false;
     68   1.1  pooka 
     69   1.1  pooka struct cpu_info *
     70   1.1  pooka cpu_lookup(u_int index)
     71   1.1  pooka {
     72   1.1  pooka 
     73   1.1  pooka 	return &rump_cpus[index];
     74   1.1  pooka }
     75   1.1  pooka 
     76  1.12  pooka /* this could/should be mi_attach_cpu? */
     77  1.12  pooka void
     78  1.12  pooka rump_cpus_bootstrap(int num)
     79  1.12  pooka {
     80  1.12  pooka 	struct rumpcpu *rcpu;
     81  1.12  pooka 	struct cpu_info *ci;
     82  1.12  pooka 	int i;
     83  1.12  pooka 
     84  1.13  pooka 	if (num > MAXCPUS) {
     85  1.13  pooka 		aprint_verbose("CPU limit: %d wanted, %d (MAXCPUS) available\n",
     86  1.13  pooka 		    num, MAXCPUS);
     87  1.13  pooka 		num = MAXCPUS;
     88  1.13  pooka 	}
     89  1.13  pooka 
     90  1.12  pooka 	for (i = 0; i < num; i++) {
     91  1.12  pooka 		rcpu = &rcpu_storage[i];
     92  1.12  pooka 		ci = &rump_cpus[i];
     93  1.12  pooka 		ci->ci_index = i;
     94  1.12  pooka 		rump_cpu_attach(ci);
     95  1.12  pooka 		ncpu++;
     96  1.12  pooka 	}
     97  1.12  pooka }
     98  1.12  pooka 
     99   1.1  pooka void
    100   1.1  pooka rump_scheduler_init()
    101   1.1  pooka {
    102   1.1  pooka 	struct rumpcpu *rcpu;
    103   1.1  pooka 	struct cpu_info *ci;
    104   1.1  pooka 	int i;
    105   1.1  pooka 
    106   1.1  pooka 	rumpuser_mutex_init(&schedmtx);
    107   1.1  pooka 	rumpuser_cv_init(&schedcv);
    108   1.3  pooka 	rumpuser_cv_init(&lwp0cv);
    109   1.1  pooka 	for (i = 0; i < ncpu; i++) {
    110   1.1  pooka 		rcpu = &rcpu_storage[i];
    111   1.1  pooka 		ci = &rump_cpus[i];
    112  1.12  pooka 		rcpu->rcpu_ci = ci;
    113   1.4  pooka 		ci->ci_schedstate.spc_mutex =
    114   1.4  pooka 		    mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    115   1.9  pooka 		ci->ci_schedstate.spc_flags = SPCF_RUNNING;
    116   1.8  pooka 		LIST_INSERT_HEAD(&cpu_freelist, rcpu, rcpu_entries);
    117   1.8  pooka 		rcpu->rcpu_flags = RCPU_FREELIST;
    118   1.8  pooka 		rumpuser_cv_init(&rcpu->rcpu_cv);
    119   1.1  pooka 	}
    120   1.1  pooka }
    121   1.1  pooka 
    122  1.14  pooka /*
    123  1.14  pooka  * condvar ops using scheduler lock as the rumpuser interlock.
    124  1.14  pooka  */
    125  1.14  pooka void
    126  1.14  pooka rump_schedlock_cv_wait(struct rumpuser_cv *cv)
    127  1.14  pooka {
    128  1.14  pooka 
    129  1.14  pooka 	rumpuser_cv_wait(cv, schedmtx);
    130  1.14  pooka }
    131  1.14  pooka 
    132  1.14  pooka int
    133  1.14  pooka rump_schedlock_cv_timedwait(struct rumpuser_cv *cv, const struct timespec *ts)
    134  1.14  pooka {
    135  1.14  pooka 
    136  1.14  pooka 	return rumpuser_cv_timedwait(cv, schedmtx, ts->tv_sec, ts->tv_nsec);
    137  1.14  pooka }
    138  1.14  pooka 
    139   1.1  pooka void
    140   1.1  pooka rump_schedule()
    141   1.1  pooka {
    142   1.3  pooka 	struct lwp *l;
    143   1.2  pooka 
    144   1.2  pooka 	/*
    145   1.2  pooka 	 * If there is no dedicated lwp, allocate a temp one and
    146   1.3  pooka 	 * set it to be free'd upon unschedule().  Use lwp0 context
    147   1.3  pooka 	 * for reserving the necessary resources.
    148   1.2  pooka 	 */
    149   1.3  pooka 	l = rumpuser_get_curlwp();
    150   1.2  pooka 	if (l == NULL) {
    151   1.3  pooka 		/* busy lwp0 */
    152   1.3  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    153   1.3  pooka 		while (lwp0busy)
    154   1.3  pooka 			rumpuser_cv_wait_nowrap(lwp0cv, schedmtx);
    155   1.3  pooka 		lwp0busy = true;
    156   1.3  pooka 		rumpuser_mutex_exit(schedmtx);
    157   1.3  pooka 
    158   1.3  pooka 		/* schedule cpu and use lwp0 */
    159   1.4  pooka 		rump_schedule_cpu(&lwp0);
    160   1.3  pooka 		rumpuser_set_curlwp(&lwp0);
    161   1.2  pooka 		l = rump_lwp_alloc(0, rump_nextlid());
    162   1.3  pooka 
    163   1.3  pooka 		/* release lwp0 */
    164   1.3  pooka 		rump_lwp_switch(l);
    165   1.3  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    166   1.3  pooka 		lwp0busy = false;
    167   1.3  pooka 		rumpuser_cv_signal(lwp0cv);
    168   1.3  pooka 		rumpuser_mutex_exit(schedmtx);
    169   1.3  pooka 
    170   1.3  pooka 		/* mark new lwp as dead-on-exit */
    171   1.2  pooka 		rump_lwp_release(l);
    172   1.3  pooka 	} else {
    173   1.4  pooka 		rump_schedule_cpu(l);
    174   1.2  pooka 	}
    175   1.2  pooka }
    176   1.2  pooka 
    177   1.4  pooka void
    178   1.4  pooka rump_schedule_cpu(struct lwp *l)
    179   1.2  pooka {
    180  1.14  pooka 
    181  1.14  pooka 	rump_schedule_cpu_interlock(l, NULL);
    182  1.14  pooka }
    183  1.14  pooka 
    184  1.14  pooka void
    185  1.14  pooka rump_schedule_cpu_interlock(struct lwp *l, void *interlock)
    186  1.14  pooka {
    187   1.1  pooka 	struct rumpcpu *rcpu;
    188  1.14  pooka 	bool schedlock = interlock == schedmtx;
    189  1.14  pooka 
    190  1.14  pooka 	if (!schedlock)
    191  1.14  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    192   1.1  pooka 
    193   1.8  pooka 	if (l->l_pflag & LP_BOUND) {
    194   1.8  pooka 		KASSERT(l->l_cpu != NULL);
    195   1.8  pooka 		rcpu = &rcpu_storage[l->l_cpu-&rump_cpus[0]];
    196   1.8  pooka 		if (rcpu->rcpu_flags & RCPU_BUSY) {
    197   1.8  pooka 			KASSERT((rcpu->rcpu_flags & RCPU_FREELIST) == 0);
    198   1.8  pooka 			while (rcpu->rcpu_flags & RCPU_BUSY) {
    199   1.8  pooka 				rcpu->rcpu_flags |= RCPU_WANTED;
    200   1.8  pooka 				rumpuser_cv_wait_nowrap(rcpu->rcpu_cv,
    201   1.8  pooka 				    schedmtx);
    202   1.8  pooka 			}
    203   1.8  pooka 			rcpu->rcpu_flags &= ~RCPU_WANTED;
    204   1.8  pooka 		} else {
    205   1.8  pooka 			KASSERT(rcpu->rcpu_flags & (RCPU_FREELIST|RCPU_WANTED));
    206   1.8  pooka 		}
    207   1.8  pooka 		if (rcpu->rcpu_flags & RCPU_FREELIST) {
    208   1.8  pooka 			LIST_REMOVE(rcpu, rcpu_entries);
    209   1.8  pooka 			rcpu->rcpu_flags &= ~RCPU_FREELIST;
    210   1.8  pooka 		}
    211   1.8  pooka 	} else {
    212   1.8  pooka 		while ((rcpu = LIST_FIRST(&cpu_freelist)) == NULL) {
    213   1.8  pooka 			rumpuser_cv_wait_nowrap(schedcv, schedmtx);
    214   1.8  pooka 		}
    215   1.8  pooka 		KASSERT(rcpu->rcpu_flags & RCPU_FREELIST);
    216   1.8  pooka 		LIST_REMOVE(rcpu, rcpu_entries);
    217   1.8  pooka 		rcpu->rcpu_flags &= ~RCPU_FREELIST;
    218   1.8  pooka 		KASSERT(l->l_cpu == NULL);
    219   1.8  pooka 		l->l_cpu = rcpu->rcpu_ci;
    220   1.8  pooka 	}
    221   1.8  pooka 	rcpu->rcpu_flags |= RCPU_BUSY;
    222   1.1  pooka 	rumpuser_mutex_exit(schedmtx);
    223   1.4  pooka 	l->l_mutex = rcpu->rcpu_ci->ci_schedstate.spc_mutex;
    224   1.1  pooka }
    225   1.1  pooka 
    226   1.1  pooka void
    227   1.1  pooka rump_unschedule()
    228   1.1  pooka {
    229   1.2  pooka 	struct lwp *l;
    230   1.2  pooka 
    231   1.2  pooka 	l = rumpuser_get_curlwp();
    232   1.4  pooka 	KASSERT(l->l_mutex == l->l_cpu->ci_schedstate.spc_mutex);
    233   1.2  pooka 	rump_unschedule_cpu(l);
    234   1.4  pooka 	l->l_mutex = NULL;
    235   1.6  pooka 
    236   1.6  pooka 	/*
    237   1.6  pooka 	 * If we're using a temp lwp, need to take lwp0 for rump_lwp_free().
    238   1.6  pooka 	 * (we could maybe cache idle lwp's to avoid constant bouncing)
    239   1.6  pooka 	 */
    240   1.2  pooka 	if (l->l_flag & LW_WEXIT) {
    241   1.2  pooka 		rumpuser_set_curlwp(NULL);
    242   1.6  pooka 
    243   1.6  pooka 		/* busy lwp0 */
    244   1.6  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    245   1.6  pooka 		while (lwp0busy)
    246   1.6  pooka 			rumpuser_cv_wait_nowrap(lwp0cv, schedmtx);
    247   1.6  pooka 		lwp0busy = true;
    248   1.6  pooka 		rumpuser_mutex_exit(schedmtx);
    249   1.6  pooka 
    250   1.6  pooka 		rump_schedule_cpu(&lwp0);
    251   1.6  pooka 		rumpuser_set_curlwp(&lwp0);
    252   1.6  pooka 		rump_lwp_free(l);
    253   1.6  pooka 		rump_unschedule_cpu(&lwp0);
    254   1.6  pooka 		rumpuser_set_curlwp(NULL);
    255   1.6  pooka 
    256   1.6  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    257   1.6  pooka 		lwp0busy = false;
    258   1.6  pooka 		rumpuser_cv_signal(lwp0cv);
    259   1.6  pooka 		rumpuser_mutex_exit(schedmtx);
    260   1.2  pooka 	}
    261   1.2  pooka }
    262   1.2  pooka 
    263   1.2  pooka void
    264   1.2  pooka rump_unschedule_cpu(struct lwp *l)
    265   1.2  pooka {
    266   1.8  pooka 
    267  1.14  pooka 	rump_unschedule_cpu_interlock(l, NULL);
    268  1.14  pooka }
    269  1.14  pooka 
    270  1.14  pooka void
    271  1.14  pooka rump_unschedule_cpu_interlock(struct lwp *l, void *interlock)
    272  1.14  pooka {
    273  1.14  pooka 
    274   1.8  pooka 	if ((l->l_pflag & LP_INTR) == 0)
    275   1.8  pooka 		rump_softint_run(l->l_cpu);
    276  1.14  pooka 	rump_unschedule_cpu1(l, interlock);
    277   1.8  pooka }
    278   1.8  pooka 
    279   1.8  pooka void
    280  1.14  pooka rump_unschedule_cpu1(struct lwp *l, void *interlock)
    281   1.8  pooka {
    282   1.1  pooka 	struct rumpcpu *rcpu;
    283   1.1  pooka 	struct cpu_info *ci;
    284  1.14  pooka 	bool schedlock = interlock == schedmtx;
    285   1.1  pooka 
    286   1.1  pooka 	ci = l->l_cpu;
    287   1.8  pooka 	if ((l->l_pflag & LP_BOUND) == 0) {
    288   1.8  pooka 		l->l_cpu = NULL;
    289   1.8  pooka 	}
    290   1.1  pooka 	rcpu = &rcpu_storage[ci-&rump_cpus[0]];
    291   1.1  pooka 	KASSERT(rcpu->rcpu_ci == ci);
    292   1.8  pooka 	KASSERT(rcpu->rcpu_flags & RCPU_BUSY);
    293   1.1  pooka 
    294   1.1  pooka 	rumpuser_mutex_enter_nowrap(schedmtx);
    295  1.14  pooka 
    296   1.8  pooka 	if (rcpu->rcpu_flags & RCPU_WANTED) {
    297   1.8  pooka 		/*
    298   1.8  pooka 		 * The assumption is that there will usually be max 1
    299   1.8  pooka 		 * thread waiting on the rcpu_cv, so broadcast is fine.
    300   1.8  pooka 		 * (and the current structure requires it because of
    301   1.8  pooka 		 * only a bitmask being used for wanting).
    302   1.8  pooka 		 */
    303   1.8  pooka 		rumpuser_cv_broadcast(rcpu->rcpu_cv);
    304   1.8  pooka 	} else {
    305   1.8  pooka 		LIST_INSERT_HEAD(&cpu_freelist, rcpu, rcpu_entries);
    306   1.8  pooka 		rcpu->rcpu_flags |= RCPU_FREELIST;
    307   1.8  pooka 		rumpuser_cv_signal(schedcv);
    308   1.8  pooka 	}
    309   1.8  pooka 	rcpu->rcpu_flags &= ~RCPU_BUSY;
    310  1.14  pooka 
    311  1.14  pooka 	if (!schedlock)
    312  1.14  pooka 		rumpuser_mutex_exit(schedmtx);
    313   1.1  pooka }
    314   1.5  pooka 
    315   1.5  pooka /* Give up and retake CPU (perhaps a different one) */
    316   1.5  pooka void
    317   1.5  pooka yield()
    318   1.5  pooka {
    319   1.5  pooka 	struct lwp *l = curlwp;
    320   1.5  pooka 	int nlocks;
    321   1.5  pooka 
    322   1.5  pooka 	KERNEL_UNLOCK_ALL(l, &nlocks);
    323   1.5  pooka 	rump_unschedule_cpu(l);
    324   1.5  pooka 	rump_schedule_cpu(l);
    325   1.5  pooka 	KERNEL_LOCK(nlocks, l);
    326   1.5  pooka }
    327   1.5  pooka 
    328   1.5  pooka void
    329   1.5  pooka preempt()
    330   1.5  pooka {
    331   1.5  pooka 
    332   1.5  pooka 	yield();
    333   1.5  pooka }
    334  1.10  pooka 
    335  1.10  pooka bool
    336  1.10  pooka kpreempt(uintptr_t where)
    337  1.10  pooka {
    338  1.10  pooka 
    339  1.10  pooka 	return false;
    340  1.10  pooka }
    341  1.10  pooka 
    342  1.10  pooka /*
    343  1.10  pooka  * There is no kernel thread preemption in rump currently.  But call
    344  1.10  pooka  * the implementing macros anyway in case they grow some side-effects
    345  1.10  pooka  * down the road.
    346  1.10  pooka  */
    347  1.10  pooka void
    348  1.10  pooka kpreempt_disable(void)
    349  1.10  pooka {
    350  1.10  pooka 
    351  1.10  pooka 	KPREEMPT_DISABLE(curlwp);
    352  1.10  pooka }
    353  1.10  pooka 
    354  1.10  pooka void
    355  1.10  pooka kpreempt_enable(void)
    356  1.10  pooka {
    357  1.10  pooka 
    358  1.10  pooka 	KPREEMPT_ENABLE(curlwp);
    359  1.10  pooka }
    360  1.10  pooka 
    361  1.10  pooka void
    362  1.10  pooka suspendsched(void)
    363  1.10  pooka {
    364  1.10  pooka 
    365  1.10  pooka 	/*
    366  1.10  pooka 	 * Could wait until everyone is out and block further entries,
    367  1.10  pooka 	 * but skip that for now.
    368  1.10  pooka 	 */
    369  1.10  pooka }
    370  1.11  pooka 
    371  1.11  pooka void
    372  1.11  pooka sched_nice(struct proc *p, int level)
    373  1.11  pooka {
    374  1.11  pooka 
    375  1.11  pooka 	/* nothing to do for now */
    376  1.11  pooka }
    377