xref: /freebsd/sys/kern/kern_idle.c (revision 8a36da99deb0e19363ec04e4d3facd869c1028f5)
10384fff8SJason Evans /*-
2*8a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*8a36da99SPedro F. Giffuni  *
444116885SWarner Losh  * Copyright (C) 2000-2004 The FreeBSD Project. All rights reserved.
50384fff8SJason Evans  *
644116885SWarner Losh  * Redistribution and use in source and binary forms, with or without
744116885SWarner Losh  * modification, are permitted provided that the following conditions
844116885SWarner Losh  * are met:
944116885SWarner Losh  * 1. Redistributions of source code must retain the above copyright
1044116885SWarner Losh  *    notice, this list of conditions and the following disclaimer.
1144116885SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
1244116885SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
1344116885SWarner Losh  *    documentation and/or other materials provided with the distribution.
1444116885SWarner Losh  *
1544116885SWarner Losh  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1644116885SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1744116885SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1844116885SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1944116885SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2044116885SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2144116885SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2244116885SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2344116885SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2444116885SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2544116885SWarner Losh  * SUCH DAMAGE.
260384fff8SJason Evans  */
270384fff8SJason Evans 
28677b542eSDavid E. O'Brien #include <sys/cdefs.h>
29677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
30677b542eSDavid E. O'Brien 
310384fff8SJason Evans #include <sys/param.h>
320384fff8SJason Evans #include <sys/systm.h>
330384fff8SJason Evans #include <sys/kernel.h>
34ba228f6dSJohn Baldwin #include <sys/kthread.h>
35fb919e4dSMark Murray #include <sys/lock.h>
36fb919e4dSMark Murray #include <sys/mutex.h>
37fb919e4dSMark Murray #include <sys/proc.h>
380384fff8SJason Evans #include <sys/resourcevar.h>
39b43179fbSJeff Roberson #include <sys/sched.h>
400384fff8SJason Evans #include <sys/unistd.h>
416804a3abSJulian Elischer #ifdef SMP
426804a3abSJulian Elischer #include <sys/smp.h>
436804a3abSJulian Elischer #endif
440384fff8SJason Evans 
450384fff8SJason Evans static void idle_setup(void *dummy);
46237fdd78SRobert Watson SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL);
470384fff8SJason Evans 
480384fff8SJason Evans /*
497ecfc090SJohn Baldwin  * Set up per-cpu idle process contexts.  The AP's shouldn't be running or
507ecfc090SJohn Baldwin  * accessing their idle processes at this point, so don't bother with
517ecfc090SJohn Baldwin  * locking.
520384fff8SJason Evans  */
530384fff8SJason Evans static void
540384fff8SJason Evans idle_setup(void *dummy)
550384fff8SJason Evans {
566caa8a15SJohn Baldwin #ifdef SMP
570bbc8826SJohn Baldwin 	struct pcpu *pc;
586caa8a15SJohn Baldwin #endif
596caa8a15SJohn Baldwin 	struct proc *p;
60e602ba25SJulian Elischer 	struct thread *td;
610384fff8SJason Evans 	int error;
620384fff8SJason Evans 
63fbf70464SJulian Elischer 	p = NULL; /* start with no idle process */
640384fff8SJason Evans #ifdef SMP
65d098f930SNathan Whitehorn 	STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
660384fff8SJason Evans #endif
675f66cfcaSJulian Elischer #ifdef SMP
687ab24ea3SJulian Elischer 		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
699ef95d01SJulian Elischer 		    RFSTOPPED | RFHIGHPID, 0, "idle", "idle: cpu%d", pc->pc_cpuid);
707ab24ea3SJulian Elischer 		pc->pc_idlethread = td;
717ab24ea3SJulian Elischer #else
725f66cfcaSJulian Elischer 		error = kproc_kthread_add(sched_idletd, NULL, &p, &td,
739ef95d01SJulian Elischer 		    RFSTOPPED | RFHIGHPID, 0, "idle", "idle");
747ab24ea3SJulian Elischer 		PCPU_SET(idlethread, td);
757ab24ea3SJulian Elischer #endif
760384fff8SJason Evans 		if (error)
773745c395SJulian Elischer 			panic("idle_setup: kproc_create error %d\n", error);
780384fff8SJason Evans 
79982d11f8SJeff Roberson 		thread_lock(td);
80bf0acc27SJohn Baldwin 		TD_SET_CAN_RUN(td);
811b9d701fSAttilio Rao 		td->td_flags |= TDF_IDLETD | TDF_NOLOAD;
828460a577SJohn Birrell 		sched_class(td, PRI_IDLE);
83339a7e7fSSuleiman Souhlal 		sched_prio(td, PRI_MAX_IDLE);
84982d11f8SJeff Roberson 		thread_unlock(td);
856caa8a15SJohn Baldwin #ifdef SMP
860384fff8SJason Evans 	}
876caa8a15SJohn Baldwin #endif
880384fff8SJason Evans }
89