kern_idle.c revision 173035
165557Sjasone/*-
2132637Simp * Copyright (C) 2000-2004 The FreeBSD Project. All rights reserved.
365557Sjasone *
4132637Simp * Redistribution and use in source and binary forms, with or without
5132637Simp * modification, are permitted provided that the following conditions
6132637Simp * are met:
7132637Simp * 1. Redistributions of source code must retain the above copyright
8132637Simp *    notice, this list of conditions and the following disclaimer.
9132637Simp * 2. Redistributions in binary form must reproduce the above copyright
10132637Simp *    notice, this list of conditions and the following disclaimer in the
11132637Simp *    documentation and/or other materials provided with the distribution.
12132637Simp *
13132637Simp * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14132637Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15132637Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16132637Simp * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17132637Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18132637Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19132637Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20132637Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21132637Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22132637Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23132637Simp * SUCH DAMAGE.
2465557Sjasone */
2565557Sjasone
26116182Sobrien#include <sys/cdefs.h>
27116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_idle.c 173035 2007-10-26 20:32:33Z julian $");
28116182Sobrien
2965557Sjasone#include <sys/param.h>
3065557Sjasone#include <sys/systm.h>
3165557Sjasone#include <sys/kernel.h>
3276440Sjhb#include <sys/kthread.h>
3376166Smarkm#include <sys/lock.h>
3476166Smarkm#include <sys/mutex.h>
3576166Smarkm#include <sys/proc.h>
3676440Sjhb#include <sys/resourcevar.h>
37104964Sjeff#include <sys/sched.h>
3865557Sjasone#include <sys/unistd.h>
39134591Sjulian#ifdef SMP
40134591Sjulian#include <sys/smp.h>
41134591Sjulian#endif
4265557Sjasone
4365557Sjasonestatic void idle_setup(void *dummy);
4465557SjasoneSYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL)
4565557Sjasone
4665557Sjasone/*
47121238Speter * Set up per-cpu idle process contexts.  The AP's shouldn't be running or
4872222Sjhb * accessing their idle processes at this point, so don't bother with
4972222Sjhb * locking.
5065557Sjasone */
5165557Sjasonestatic void
5265557Sjasoneidle_setup(void *dummy)
5365557Sjasone{
5476078Sjhb#ifdef SMP
5587702Sjhb	struct pcpu *pc;
5676078Sjhb#endif
5776078Sjhb	struct proc *p;
5899072Sjulian	struct thread *td;
5965557Sjasone	int error;
6065557Sjasone
6176078Sjhb#ifdef SMP
6287702Sjhb	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
63173004Sjulian#endif
64173035Sjulian#ifdef SMP
65173004Sjulian		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
66173004Sjulian		    RFSTOPPED | RFHIGHPID, 0, "idled", "idle: cpu%d", pc->pc_cpuid);
67173004Sjulian		pc->pc_idlethread = td;
6865557Sjasone#else
69173035Sjulian		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
70173035Sjulian		    RFSTOPPED | RFHIGHPID, 0, "idled", "idle");
71173004Sjulian		PCPU_SET(idlethread, td);
7265557Sjasone#endif
73173004Sjulian		p = td->td_proc;
7465557Sjasone		if (error)
75172836Sjulian			panic("idle_setup: kproc_create error %d\n", error);
7665557Sjasone
7776078Sjhb		p->p_flag |= P_NOLOAD;
78170307Sjeff		thread_lock(td);
79131473Sjhb		TD_SET_CAN_RUN(td);
80114471Sjulian		td->td_flags |= TDF_IDLETD;
81163709Sjb		sched_class(td, PRI_IDLE);
82141246Sssouhlal		sched_prio(td, PRI_MAX_IDLE);
83170307Sjeff		thread_unlock(td);
8476078Sjhb#ifdef SMP
8565557Sjasone	}
8676078Sjhb#endif
8765557Sjasone}
88