xref: /freebsd/sys/kern/kern_idle.c (revision 5f66cfca515d7183a3fe545fae6e65b8894b8550)
10384fff8SJason Evans /*-
244116885SWarner Losh  * Copyright (C) 2000-2004 The FreeBSD Project. All rights reserved.
30384fff8SJason Evans  *
444116885SWarner Losh  * Redistribution and use in source and binary forms, with or without
544116885SWarner Losh  * modification, are permitted provided that the following conditions
644116885SWarner Losh  * are met:
744116885SWarner Losh  * 1. Redistributions of source code must retain the above copyright
844116885SWarner Losh  *    notice, this list of conditions and the following disclaimer.
944116885SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
1044116885SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
1144116885SWarner Losh  *    documentation and/or other materials provided with the distribution.
1244116885SWarner Losh  *
1344116885SWarner Losh  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1444116885SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1544116885SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1644116885SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1744116885SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1844116885SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1944116885SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2044116885SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2144116885SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2244116885SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2344116885SWarner Losh  * SUCH DAMAGE.
240384fff8SJason Evans  */
250384fff8SJason Evans 
26677b542eSDavid E. O'Brien #include <sys/cdefs.h>
27677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
28677b542eSDavid E. O'Brien 
290384fff8SJason Evans #include <sys/param.h>
300384fff8SJason Evans #include <sys/systm.h>
310384fff8SJason Evans #include <sys/kernel.h>
32ba228f6dSJohn Baldwin #include <sys/kthread.h>
33fb919e4dSMark Murray #include <sys/lock.h>
34fb919e4dSMark Murray #include <sys/mutex.h>
35fb919e4dSMark Murray #include <sys/proc.h>
360384fff8SJason Evans #include <sys/resourcevar.h>
37b43179fbSJeff Roberson #include <sys/sched.h>
380384fff8SJason Evans #include <sys/unistd.h>
396804a3abSJulian Elischer #ifdef SMP
406804a3abSJulian Elischer #include <sys/smp.h>
416804a3abSJulian Elischer #endif
420384fff8SJason Evans 
430384fff8SJason Evans static void idle_setup(void *dummy);
440384fff8SJason Evans SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL)
450384fff8SJason Evans 
460384fff8SJason Evans /*
477ecfc090SJohn Baldwin  * Set up per-cpu idle process contexts.  The AP's shouldn't be running or
487ecfc090SJohn Baldwin  * accessing their idle processes at this point, so don't bother with
497ecfc090SJohn Baldwin  * locking.
500384fff8SJason Evans  */
510384fff8SJason Evans static void
520384fff8SJason Evans idle_setup(void *dummy)
530384fff8SJason Evans {
546caa8a15SJohn Baldwin #ifdef SMP
550bbc8826SJohn Baldwin 	struct pcpu *pc;
566caa8a15SJohn Baldwin #endif
576caa8a15SJohn Baldwin 	struct proc *p;
58e602ba25SJulian Elischer 	struct thread *td;
590384fff8SJason Evans 	int error;
600384fff8SJason Evans 
610384fff8SJason Evans #ifdef SMP
620bbc8826SJohn Baldwin 	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
630384fff8SJason Evans #endif
645f66cfcaSJulian Elischer #ifdef SMP
657ab24ea3SJulian Elischer 		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
667ab24ea3SJulian Elischer 		    RFSTOPPED | RFHIGHPID, 0, "idled", "idle: cpu%d", pc->pc_cpuid);
677ab24ea3SJulian Elischer 		pc->pc_idlethread = td;
687ab24ea3SJulian Elischer #else
695f66cfcaSJulian Elischer 		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
705f66cfcaSJulian Elischer 		    RFSTOPPED | RFHIGHPID, 0, "idled", "idle");
717ab24ea3SJulian Elischer 		PCPU_SET(idlethread, td);
727ab24ea3SJulian Elischer #endif
737ab24ea3SJulian Elischer 		p = td->td_proc;
740384fff8SJason Evans 		if (error)
753745c395SJulian Elischer 			panic("idle_setup: kproc_create error %d\n", error);
760384fff8SJason Evans 
776caa8a15SJohn Baldwin 		p->p_flag |= P_NOLOAD;
78982d11f8SJeff Roberson 		thread_lock(td);
79bf0acc27SJohn Baldwin 		TD_SET_CAN_RUN(td);
80b1ac98d8SJulian Elischer 		td->td_flags |= TDF_IDLETD;
818460a577SJohn Birrell 		sched_class(td, PRI_IDLE);
82339a7e7fSSuleiman Souhlal 		sched_prio(td, PRI_MAX_IDLE);
83982d11f8SJeff Roberson 		thread_unlock(td);
846caa8a15SJohn Baldwin #ifdef SMP
850384fff8SJason Evans 	}
866caa8a15SJohn Baldwin #endif
870384fff8SJason Evans }
88