Home | History | Annotate | Line # | Download | only in kern
kern_idle.c revision 1.3.2.2
      1  1.3.2.2  mjf /*	$NetBSD: kern_idle.c,v 1.3.2.2 2007/07/11 20:09:48 mjf Exp $	*/
      2  1.3.2.2  mjf 
      3  1.3.2.2  mjf /*-
      4  1.3.2.2  mjf  * Copyright (c)2002, 2006, 2007 YAMAMOTO Takashi,
      5  1.3.2.2  mjf  * All rights reserved.
      6  1.3.2.2  mjf  *
      7  1.3.2.2  mjf  * Redistribution and use in source and binary forms, with or without
      8  1.3.2.2  mjf  * modification, are permitted provided that the following conditions
      9  1.3.2.2  mjf  * are met:
     10  1.3.2.2  mjf  * 1. Redistributions of source code must retain the above copyright
     11  1.3.2.2  mjf  *    notice, this list of conditions and the following disclaimer.
     12  1.3.2.2  mjf  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3.2.2  mjf  *    notice, this list of conditions and the following disclaimer in the
     14  1.3.2.2  mjf  *    documentation and/or other materials provided with the distribution.
     15  1.3.2.2  mjf  *
     16  1.3.2.2  mjf  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.3.2.2  mjf  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.3.2.2  mjf  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.3.2.2  mjf  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.3.2.2  mjf  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.3.2.2  mjf  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.3.2.2  mjf  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.3.2.2  mjf  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.3.2.2  mjf  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.3.2.2  mjf  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.3.2.2  mjf  * SUCH DAMAGE.
     27  1.3.2.2  mjf  */
     28  1.3.2.2  mjf 
     29  1.3.2.2  mjf #include <sys/cdefs.h>
     30  1.3.2.2  mjf 
     31  1.3.2.2  mjf __KERNEL_RCSID(0, "$NetBSD: kern_idle.c,v 1.3.2.2 2007/07/11 20:09:48 mjf Exp $");
     32  1.3.2.2  mjf 
     33  1.3.2.2  mjf #include <sys/param.h>
     34  1.3.2.2  mjf #include <sys/cpu.h>
     35  1.3.2.2  mjf #include <sys/idle.h>
     36  1.3.2.2  mjf #include <sys/lwp.h>
     37  1.3.2.2  mjf #include <sys/lockdebug.h>
     38  1.3.2.2  mjf #include <sys/kmem.h>
     39  1.3.2.2  mjf 
     40  1.3.2.2  mjf #include <uvm/uvm.h>
     41  1.3.2.2  mjf #include <uvm/uvm_extern.h>
     42  1.3.2.2  mjf 
     43  1.3.2.2  mjf #define	PIDLELWP	(MAXPRI + 1)	/* lowest priority */
     44  1.3.2.2  mjf 
     45  1.3.2.2  mjf void
     46  1.3.2.2  mjf idle_loop(void *dummy)
     47  1.3.2.2  mjf {
     48  1.3.2.2  mjf #if defined(DIAGNOSTIC)
     49  1.3.2.2  mjf 	struct cpu_info *ci = curcpu();
     50  1.3.2.2  mjf #endif /* defined(DIAGNOSTIC) */
     51  1.3.2.2  mjf 	struct lwp *l = curlwp;
     52  1.3.2.2  mjf 
     53  1.3.2.2  mjf 	KERNEL_UNLOCK_ALL(l, NULL);
     54  1.3.2.2  mjf 	l->l_usrpri = PIDLELWP;
     55  1.3.2.2  mjf 	l->l_priority = l->l_usrpri;
     56  1.3.2.2  mjf 	l->l_stat = LSONPROC;
     57  1.3.2.2  mjf 	while (1 /* CONSTCOND */) {
     58  1.3.2.2  mjf 		KERNEL_LOCK_ASSERT_UNLOCKED();
     59  1.3.2.2  mjf 		LOCKDEBUG_BARRIER(NULL, 0);
     60  1.3.2.2  mjf 		KASSERT((l->l_flag & LW_IDLE) != 0);
     61  1.3.2.2  mjf 		KASSERT(ci == curcpu());
     62  1.3.2.2  mjf 		KASSERT(l == curlwp);
     63  1.3.2.2  mjf 		KASSERT(CURCPU_IDLE_P());
     64  1.3.2.2  mjf 		KASSERT(l->l_usrpri == PIDLELWP);
     65  1.3.2.2  mjf 
     66  1.3.2.2  mjf 		if (uvm.page_idle_zero) {
     67  1.3.2.2  mjf 			if (sched_curcpu_runnable_p()) {
     68  1.3.2.2  mjf 				goto schedule;
     69  1.3.2.2  mjf 			}
     70  1.3.2.2  mjf 			uvm_pageidlezero();
     71  1.3.2.2  mjf 		}
     72  1.3.2.2  mjf 		if (!sched_curcpu_runnable_p()) {
     73  1.3.2.2  mjf 			cpu_idle();
     74  1.3.2.2  mjf 			if (!sched_curcpu_runnable_p()) {
     75  1.3.2.2  mjf 				continue;
     76  1.3.2.2  mjf 			}
     77  1.3.2.2  mjf 		}
     78  1.3.2.2  mjf schedule:
     79  1.3.2.2  mjf 		KASSERT(l->l_mutex == &l->l_cpu->ci_schedstate.spc_lwplock);
     80  1.3.2.2  mjf 		lwp_lock(l);
     81  1.3.2.2  mjf 		mi_switch(l);
     82  1.3.2.2  mjf 		KASSERT(curlwp == l);
     83  1.3.2.2  mjf 		KASSERT(l->l_stat == LSONPROC);
     84  1.3.2.2  mjf 	}
     85  1.3.2.2  mjf }
     86  1.3.2.2  mjf 
     87  1.3.2.2  mjf int
     88  1.3.2.2  mjf create_idle_lwp(struct cpu_info *ci)
     89  1.3.2.2  mjf {
     90  1.3.2.2  mjf 	struct proc *p = &proc0;
     91  1.3.2.2  mjf 	struct lwp *l;
     92  1.3.2.2  mjf 	char *name;
     93  1.3.2.2  mjf 	vaddr_t uaddr;
     94  1.3.2.2  mjf 	bool inmem;
     95  1.3.2.2  mjf 	cpuid_t cpuid;
     96  1.3.2.2  mjf 	int error;
     97  1.3.2.2  mjf 
     98  1.3.2.2  mjf 	KASSERT(ci->ci_data.cpu_idlelwp == NULL);
     99  1.3.2.2  mjf 	inmem = uvm_uarea_alloc(&uaddr);
    100  1.3.2.2  mjf 	if (uaddr == 0) {
    101  1.3.2.2  mjf 		return ENOMEM;
    102  1.3.2.2  mjf 	}
    103  1.3.2.2  mjf 	error = newlwp(&lwp0, p, uaddr, inmem, 0, NULL, 0, idle_loop, NULL, &l);
    104  1.3.2.2  mjf 	if (error != 0) {
    105  1.3.2.2  mjf 		panic("create_idle_lwp: newlwp failed");
    106  1.3.2.2  mjf 	}
    107  1.3.2.2  mjf 	uvm_lwp_hold(l);
    108  1.3.2.2  mjf 	l->l_flag |= (LW_IDLE | LW_BOUND);
    109  1.3.2.2  mjf 	l->l_cpu = ci;
    110  1.3.2.2  mjf 	l->l_mutex = &ci->ci_schedstate.spc_lwplock;
    111  1.3.2.2  mjf 	ci->ci_data.cpu_idlelwp = l;
    112  1.3.2.2  mjf 	name = kmem_alloc(MAXCOMLEN, KM_NOSLEEP);
    113  1.3.2.2  mjf 	if (name != NULL) {
    114  1.3.2.2  mjf #ifdef MULTIPROCESSOR		/* XXX should be mandatory */
    115  1.3.2.2  mjf 		cpuid = ci->ci_cpuid;
    116  1.3.2.2  mjf #else
    117  1.3.2.2  mjf 		cpuid = 0;
    118  1.3.2.2  mjf #endif
    119  1.3.2.2  mjf 		snprintf(name, MAXCOMLEN, "idle/%d", (int)cpuid);
    120  1.3.2.2  mjf 		lwp_lock(l);
    121  1.3.2.2  mjf 		l->l_name = name;
    122  1.3.2.2  mjf 		lwp_unlock(l);
    123  1.3.2.2  mjf 	}
    124  1.3.2.2  mjf 	return error;
    125  1.3.2.2  mjf }
    126