kern_idle.c revision 173004
1279264Sdelphij/*-
2110010Smarkm * Copyright (C) 2000-2004 The FreeBSD Project. All rights reserved.
3110010Smarkm *
4142429Snectar * Redistribution and use in source and binary forms, with or without
5110010Smarkm * modification, are permitted provided that the following conditions
6110010Smarkm * are met:
7110010Smarkm * 1. Redistributions of source code must retain the above copyright
8110010Smarkm *    notice, this list of conditions and the following disclaimer.
9110010Smarkm * 2. Redistributions in binary form must reproduce the above copyright
10110010Smarkm *    notice, this list of conditions and the following disclaimer in the
11110010Smarkm *    documentation and/or other materials provided with the distribution.
12110010Smarkm *
13110010Smarkm * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14110010Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15110010Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16110010Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17110010Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18110010Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19110010Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20215698Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21215698Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22215698Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23215698Ssimon * SUCH DAMAGE.
24215698Ssimon */
25110010Smarkm
26110010Smarkm#include <sys/cdefs.h>
27110010Smarkm__FBSDID("$FreeBSD: head/sys/kern/kern_idle.c 173004 2007-10-26 08:00:41Z julian $");
28110010Smarkm
29110010Smarkm#include <sys/param.h>
30110010Smarkm#include <sys/systm.h>
31110010Smarkm#include <sys/kernel.h>
32110010Smarkm#include <sys/kthread.h>
33110010Smarkm#include <sys/lock.h>
34110010Smarkm#include <sys/mutex.h>
35110010Smarkm#include <sys/proc.h>
36110010Smarkm#include <sys/resourcevar.h>
37110010Smarkm#include <sys/sched.h>
38110010Smarkm#include <sys/unistd.h>
39110010Smarkm#ifdef SMP
40110010Smarkm#include <sys/smp.h>
41279264Sdelphij#endif
42279264Sdelphij
43110010Smarkmstatic void idle_setup(void *dummy);
44110010SmarkmSYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL)
45215698Ssimon
46215698Ssimon/*
47215698Ssimon * Set up per-cpu idle process contexts.  The AP's shouldn't be running or
48215698Ssimon * accessing their idle processes at this point, so don't bother with
49142429Snectar * locking.
50215698Ssimon */
51142429Snectarstatic void
52142429Snectaridle_setup(void *dummy)
53279264Sdelphij{
54279264Sdelphij#ifdef SMP
55279264Sdelphij	struct pcpu *pc;
56110010Smarkm#endif
57279264Sdelphij	struct proc *p;
58279264Sdelphij	struct thread *td;
59279264Sdelphij	int error;
60279264Sdelphij
61279264Sdelphij#ifdef SMP
62279264Sdelphij	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
63215698Ssimon#endif
64279264Sdelphij		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
65279264Sdelphij		    RFSTOPPED | RFHIGHPID, 0, "idled", "idle: cpu%d", pc->pc_cpuid);
66279264Sdelphij#ifdef SMP
67279264Sdelphij		pc->pc_idlethread = td;
68279264Sdelphij#else
69215698Ssimon		PCPU_SET(idlethread, td);
70279264Sdelphij#endif
71110010Smarkm		p = td->td_proc;
72110010Smarkm		if (error)
73110010Smarkm			panic("idle_setup: kproc_create error %d\n", error);
74110010Smarkm
75110010Smarkm		p->p_flag |= P_NOLOAD;
76110010Smarkm		thread_lock(td);
77110010Smarkm		TD_SET_CAN_RUN(td);
78110010Smarkm		td->td_flags |= TDF_IDLETD;
79110010Smarkm		sched_class(td, PRI_IDLE);
80110010Smarkm		sched_prio(td, PRI_MAX_IDLE);
81110010Smarkm		thread_unlock(td);
82110010Smarkm#ifdef SMP
83110010Smarkm	}
84110010Smarkm#endif
85110010Smarkm}
86110010Smarkm