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$");
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);
44177253SrwatsonSYSINIT(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
61173050Sjulian	p = NULL; /* start with no idle process */
6276078Sjhb#ifdef SMP
63222531Snwhitehorn	STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
64173004Sjulian#endif
65173035Sjulian#ifdef SMP
66173004Sjulian		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
67173051Sjulian		    RFSTOPPED | RFHIGHPID, 0, "idle", "idle: cpu%d", pc->pc_cpuid);
68173004Sjulian		pc->pc_idlethread = td;
6965557Sjasone#else
70173035Sjulian		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
71173051Sjulian		    RFSTOPPED | RFHIGHPID, 0, "idle", "idle");
72173004Sjulian		PCPU_SET(idlethread, td);
7365557Sjasone#endif
7465557Sjasone		if (error)
75172836Sjulian			panic("idle_setup: kproc_create error %d\n", error);
7665557Sjasone
77170307Sjeff		thread_lock(td);
78131473Sjhb		TD_SET_CAN_RUN(td);
79198854Sattilio		td->td_flags |= TDF_IDLETD | TDF_NOLOAD;
80163709Sjb		sched_class(td, PRI_IDLE);
81141246Sssouhlal		sched_prio(td, PRI_MAX_IDLE);
82170307Sjeff		thread_unlock(td);
8376078Sjhb#ifdef SMP
8465557Sjasone	}
8576078Sjhb#endif
8665557Sjasone}
87