Home | History | Annotate | Line # | Download | only in rumpkern
scheduler.c revision 1.13
      1  1.13  pooka /*      $NetBSD: scheduler.c,v 1.13 2010/04/28 00:42:16 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.13  pooka __KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.13 2010/04/28 00:42:16 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.1  pooka static struct rumpuser_mtx *schedmtx;
     64   1.3  pooka static struct rumpuser_cv *schedcv, *lwp0cv;
     65   1.3  pooka 
     66   1.3  pooka static bool lwp0busy = false;
     67   1.1  pooka 
     68   1.1  pooka struct cpu_info *
     69   1.1  pooka cpu_lookup(u_int index)
     70   1.1  pooka {
     71   1.1  pooka 
     72   1.1  pooka 	return &rump_cpus[index];
     73   1.1  pooka }
     74   1.1  pooka 
     75  1.12  pooka /* this could/should be mi_attach_cpu? */
     76  1.12  pooka void
     77  1.12  pooka rump_cpus_bootstrap(int num)
     78  1.12  pooka {
     79  1.12  pooka 	struct rumpcpu *rcpu;
     80  1.12  pooka 	struct cpu_info *ci;
     81  1.12  pooka 	int i;
     82  1.12  pooka 
     83  1.13  pooka 	if (num > MAXCPUS) {
     84  1.13  pooka 		aprint_verbose("CPU limit: %d wanted, %d (MAXCPUS) available\n",
     85  1.13  pooka 		    num, MAXCPUS);
     86  1.13  pooka 		num = MAXCPUS;
     87  1.13  pooka 	}
     88  1.13  pooka 
     89  1.12  pooka 	for (i = 0; i < num; i++) {
     90  1.12  pooka 		rcpu = &rcpu_storage[i];
     91  1.12  pooka 		ci = &rump_cpus[i];
     92  1.12  pooka 		ci->ci_index = i;
     93  1.12  pooka 		rump_cpu_attach(ci);
     94  1.12  pooka 		ncpu++;
     95  1.12  pooka 	}
     96  1.12  pooka }
     97  1.12  pooka 
     98   1.1  pooka void
     99   1.1  pooka rump_scheduler_init()
    100   1.1  pooka {
    101   1.1  pooka 	struct rumpcpu *rcpu;
    102   1.1  pooka 	struct cpu_info *ci;
    103   1.1  pooka 	int i;
    104   1.1  pooka 
    105   1.1  pooka 	rumpuser_mutex_init(&schedmtx);
    106   1.1  pooka 	rumpuser_cv_init(&schedcv);
    107   1.3  pooka 	rumpuser_cv_init(&lwp0cv);
    108   1.1  pooka 	for (i = 0; i < ncpu; i++) {
    109   1.1  pooka 		rcpu = &rcpu_storage[i];
    110   1.1  pooka 		ci = &rump_cpus[i];
    111  1.12  pooka 		rcpu->rcpu_ci = ci;
    112   1.4  pooka 		ci->ci_schedstate.spc_mutex =
    113   1.4  pooka 		    mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    114   1.9  pooka 		ci->ci_schedstate.spc_flags = SPCF_RUNNING;
    115   1.8  pooka 		LIST_INSERT_HEAD(&cpu_freelist, rcpu, rcpu_entries);
    116   1.8  pooka 		rcpu->rcpu_flags = RCPU_FREELIST;
    117   1.8  pooka 		rumpuser_cv_init(&rcpu->rcpu_cv);
    118   1.1  pooka 	}
    119   1.1  pooka }
    120   1.1  pooka 
    121   1.1  pooka void
    122   1.1  pooka rump_schedule()
    123   1.1  pooka {
    124   1.3  pooka 	struct lwp *l;
    125   1.2  pooka 
    126   1.2  pooka 	/*
    127   1.2  pooka 	 * If there is no dedicated lwp, allocate a temp one and
    128   1.3  pooka 	 * set it to be free'd upon unschedule().  Use lwp0 context
    129   1.3  pooka 	 * for reserving the necessary resources.
    130   1.2  pooka 	 */
    131   1.3  pooka 	l = rumpuser_get_curlwp();
    132   1.2  pooka 	if (l == NULL) {
    133   1.3  pooka 		/* busy lwp0 */
    134   1.3  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    135   1.3  pooka 		while (lwp0busy)
    136   1.3  pooka 			rumpuser_cv_wait_nowrap(lwp0cv, schedmtx);
    137   1.3  pooka 		lwp0busy = true;
    138   1.3  pooka 		rumpuser_mutex_exit(schedmtx);
    139   1.3  pooka 
    140   1.3  pooka 		/* schedule cpu and use lwp0 */
    141   1.4  pooka 		rump_schedule_cpu(&lwp0);
    142   1.3  pooka 		rumpuser_set_curlwp(&lwp0);
    143   1.2  pooka 		l = rump_lwp_alloc(0, rump_nextlid());
    144   1.3  pooka 
    145   1.3  pooka 		/* release lwp0 */
    146   1.3  pooka 		rump_lwp_switch(l);
    147   1.3  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    148   1.3  pooka 		lwp0busy = false;
    149   1.3  pooka 		rumpuser_cv_signal(lwp0cv);
    150   1.3  pooka 		rumpuser_mutex_exit(schedmtx);
    151   1.3  pooka 
    152   1.3  pooka 		/* mark new lwp as dead-on-exit */
    153   1.2  pooka 		rump_lwp_release(l);
    154   1.3  pooka 	} else {
    155   1.4  pooka 		rump_schedule_cpu(l);
    156   1.2  pooka 	}
    157   1.2  pooka }
    158   1.2  pooka 
    159   1.4  pooka void
    160   1.4  pooka rump_schedule_cpu(struct lwp *l)
    161   1.2  pooka {
    162   1.1  pooka 	struct rumpcpu *rcpu;
    163   1.1  pooka 
    164   1.1  pooka 	rumpuser_mutex_enter_nowrap(schedmtx);
    165   1.8  pooka 	if (l->l_pflag & LP_BOUND) {
    166   1.8  pooka 		KASSERT(l->l_cpu != NULL);
    167   1.8  pooka 		rcpu = &rcpu_storage[l->l_cpu-&rump_cpus[0]];
    168   1.8  pooka 		if (rcpu->rcpu_flags & RCPU_BUSY) {
    169   1.8  pooka 			KASSERT((rcpu->rcpu_flags & RCPU_FREELIST) == 0);
    170   1.8  pooka 			while (rcpu->rcpu_flags & RCPU_BUSY) {
    171   1.8  pooka 				rcpu->rcpu_flags |= RCPU_WANTED;
    172   1.8  pooka 				rumpuser_cv_wait_nowrap(rcpu->rcpu_cv,
    173   1.8  pooka 				    schedmtx);
    174   1.8  pooka 			}
    175   1.8  pooka 			rcpu->rcpu_flags &= ~RCPU_WANTED;
    176   1.8  pooka 		} else {
    177   1.8  pooka 			KASSERT(rcpu->rcpu_flags & (RCPU_FREELIST|RCPU_WANTED));
    178   1.8  pooka 		}
    179   1.8  pooka 		if (rcpu->rcpu_flags & RCPU_FREELIST) {
    180   1.8  pooka 			LIST_REMOVE(rcpu, rcpu_entries);
    181   1.8  pooka 			rcpu->rcpu_flags &= ~RCPU_FREELIST;
    182   1.8  pooka 		}
    183   1.8  pooka 	} else {
    184   1.8  pooka 		while ((rcpu = LIST_FIRST(&cpu_freelist)) == NULL) {
    185   1.8  pooka 			rumpuser_cv_wait_nowrap(schedcv, schedmtx);
    186   1.8  pooka 		}
    187   1.8  pooka 		KASSERT(rcpu->rcpu_flags & RCPU_FREELIST);
    188   1.8  pooka 		LIST_REMOVE(rcpu, rcpu_entries);
    189   1.8  pooka 		rcpu->rcpu_flags &= ~RCPU_FREELIST;
    190   1.8  pooka 		KASSERT(l->l_cpu == NULL);
    191   1.8  pooka 		l->l_cpu = rcpu->rcpu_ci;
    192   1.8  pooka 	}
    193   1.8  pooka 	rcpu->rcpu_flags |= RCPU_BUSY;
    194   1.1  pooka 	rumpuser_mutex_exit(schedmtx);
    195   1.4  pooka 	l->l_mutex = rcpu->rcpu_ci->ci_schedstate.spc_mutex;
    196   1.1  pooka }
    197   1.1  pooka 
    198   1.1  pooka void
    199   1.1  pooka rump_unschedule()
    200   1.1  pooka {
    201   1.2  pooka 	struct lwp *l;
    202   1.2  pooka 
    203   1.2  pooka 	l = rumpuser_get_curlwp();
    204   1.4  pooka 	KASSERT(l->l_mutex == l->l_cpu->ci_schedstate.spc_mutex);
    205   1.2  pooka 	rump_unschedule_cpu(l);
    206   1.4  pooka 	l->l_mutex = NULL;
    207   1.6  pooka 
    208   1.6  pooka 	/*
    209   1.6  pooka 	 * If we're using a temp lwp, need to take lwp0 for rump_lwp_free().
    210   1.6  pooka 	 * (we could maybe cache idle lwp's to avoid constant bouncing)
    211   1.6  pooka 	 */
    212   1.2  pooka 	if (l->l_flag & LW_WEXIT) {
    213   1.2  pooka 		rumpuser_set_curlwp(NULL);
    214   1.6  pooka 
    215   1.6  pooka 		/* busy lwp0 */
    216   1.6  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    217   1.6  pooka 		while (lwp0busy)
    218   1.6  pooka 			rumpuser_cv_wait_nowrap(lwp0cv, schedmtx);
    219   1.6  pooka 		lwp0busy = true;
    220   1.6  pooka 		rumpuser_mutex_exit(schedmtx);
    221   1.6  pooka 
    222   1.6  pooka 		rump_schedule_cpu(&lwp0);
    223   1.6  pooka 		rumpuser_set_curlwp(&lwp0);
    224   1.6  pooka 		rump_lwp_free(l);
    225   1.6  pooka 		rump_unschedule_cpu(&lwp0);
    226   1.6  pooka 		rumpuser_set_curlwp(NULL);
    227   1.6  pooka 
    228   1.6  pooka 		rumpuser_mutex_enter_nowrap(schedmtx);
    229   1.6  pooka 		lwp0busy = false;
    230   1.6  pooka 		rumpuser_cv_signal(lwp0cv);
    231   1.6  pooka 		rumpuser_mutex_exit(schedmtx);
    232   1.2  pooka 	}
    233   1.2  pooka }
    234   1.2  pooka 
    235   1.2  pooka void
    236   1.2  pooka rump_unschedule_cpu(struct lwp *l)
    237   1.2  pooka {
    238   1.8  pooka 
    239   1.8  pooka 	if ((l->l_pflag & LP_INTR) == 0)
    240   1.8  pooka 		rump_softint_run(l->l_cpu);
    241   1.8  pooka 	rump_unschedule_cpu1(l);
    242   1.8  pooka }
    243   1.8  pooka 
    244   1.8  pooka void
    245   1.8  pooka rump_unschedule_cpu1(struct lwp *l)
    246   1.8  pooka {
    247   1.1  pooka 	struct rumpcpu *rcpu;
    248   1.1  pooka 	struct cpu_info *ci;
    249   1.1  pooka 
    250   1.1  pooka 	ci = l->l_cpu;
    251   1.8  pooka 	if ((l->l_pflag & LP_BOUND) == 0) {
    252   1.8  pooka 		l->l_cpu = NULL;
    253   1.8  pooka 	}
    254   1.1  pooka 	rcpu = &rcpu_storage[ci-&rump_cpus[0]];
    255   1.1  pooka 	KASSERT(rcpu->rcpu_ci == ci);
    256   1.8  pooka 	KASSERT(rcpu->rcpu_flags & RCPU_BUSY);
    257   1.1  pooka 
    258   1.1  pooka 	rumpuser_mutex_enter_nowrap(schedmtx);
    259   1.8  pooka 	if (rcpu->rcpu_flags & RCPU_WANTED) {
    260   1.8  pooka 		/*
    261   1.8  pooka 		 * The assumption is that there will usually be max 1
    262   1.8  pooka 		 * thread waiting on the rcpu_cv, so broadcast is fine.
    263   1.8  pooka 		 * (and the current structure requires it because of
    264   1.8  pooka 		 * only a bitmask being used for wanting).
    265   1.8  pooka 		 */
    266   1.8  pooka 		rumpuser_cv_broadcast(rcpu->rcpu_cv);
    267   1.8  pooka 	} else {
    268   1.8  pooka 		LIST_INSERT_HEAD(&cpu_freelist, rcpu, rcpu_entries);
    269   1.8  pooka 		rcpu->rcpu_flags |= RCPU_FREELIST;
    270   1.8  pooka 		rumpuser_cv_signal(schedcv);
    271   1.8  pooka 	}
    272   1.8  pooka 	rcpu->rcpu_flags &= ~RCPU_BUSY;
    273   1.1  pooka 	rumpuser_mutex_exit(schedmtx);
    274   1.1  pooka }
    275   1.5  pooka 
    276   1.5  pooka /* Give up and retake CPU (perhaps a different one) */
    277   1.5  pooka void
    278   1.5  pooka yield()
    279   1.5  pooka {
    280   1.5  pooka 	struct lwp *l = curlwp;
    281   1.5  pooka 	int nlocks;
    282   1.5  pooka 
    283   1.5  pooka 	KERNEL_UNLOCK_ALL(l, &nlocks);
    284   1.5  pooka 	rump_unschedule_cpu(l);
    285   1.5  pooka 	rump_schedule_cpu(l);
    286   1.5  pooka 	KERNEL_LOCK(nlocks, l);
    287   1.5  pooka }
    288   1.5  pooka 
    289   1.5  pooka void
    290   1.5  pooka preempt()
    291   1.5  pooka {
    292   1.5  pooka 
    293   1.5  pooka 	yield();
    294   1.5  pooka }
    295  1.10  pooka 
    296  1.10  pooka bool
    297  1.10  pooka kpreempt(uintptr_t where)
    298  1.10  pooka {
    299  1.10  pooka 
    300  1.10  pooka 	return false;
    301  1.10  pooka }
    302  1.10  pooka 
    303  1.10  pooka /*
    304  1.10  pooka  * There is no kernel thread preemption in rump currently.  But call
    305  1.10  pooka  * the implementing macros anyway in case they grow some side-effects
    306  1.10  pooka  * down the road.
    307  1.10  pooka  */
    308  1.10  pooka void
    309  1.10  pooka kpreempt_disable(void)
    310  1.10  pooka {
    311  1.10  pooka 
    312  1.10  pooka 	KPREEMPT_DISABLE(curlwp);
    313  1.10  pooka }
    314  1.10  pooka 
    315  1.10  pooka void
    316  1.10  pooka kpreempt_enable(void)
    317  1.10  pooka {
    318  1.10  pooka 
    319  1.10  pooka 	KPREEMPT_ENABLE(curlwp);
    320  1.10  pooka }
    321  1.10  pooka 
    322  1.10  pooka void
    323  1.10  pooka suspendsched(void)
    324  1.10  pooka {
    325  1.10  pooka 
    326  1.10  pooka 	/*
    327  1.10  pooka 	 * Could wait until everyone is out and block further entries,
    328  1.10  pooka 	 * but skip that for now.
    329  1.10  pooka 	 */
    330  1.10  pooka }
    331  1.11  pooka 
    332  1.11  pooka void
    333  1.11  pooka sched_nice(struct proc *p, int level)
    334  1.11  pooka {
    335  1.11  pooka 
    336  1.11  pooka 	/* nothing to do for now */
    337  1.11  pooka }
    338