10384fff8SJason Evans /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 38a36da99SPedro 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 280384fff8SJason Evans #include <sys/param.h> 290384fff8SJason Evans #include <sys/systm.h> 300384fff8SJason Evans #include <sys/kernel.h> 31ba228f6dSJohn Baldwin #include <sys/kthread.h> 32fb919e4dSMark Murray #include <sys/lock.h> 33fb919e4dSMark Murray #include <sys/mutex.h> 34fb919e4dSMark Murray #include <sys/proc.h> 350384fff8SJason Evans #include <sys/resourcevar.h> 36b43179fbSJeff Roberson #include <sys/sched.h> 370384fff8SJason Evans #include <sys/unistd.h> 386804a3abSJulian Elischer #ifdef SMP 396804a3abSJulian Elischer #include <sys/smp.h> 406804a3abSJulian Elischer #endif 410384fff8SJason Evans 420384fff8SJason Evans static void idle_setup(void *dummy); 43237fdd78SRobert Watson SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL); 440384fff8SJason Evans 450384fff8SJason Evans /* 467ecfc090SJohn Baldwin * Set up per-cpu idle process contexts. The AP's shouldn't be running or 477ecfc090SJohn Baldwin * accessing their idle processes at this point, so don't bother with 487ecfc090SJohn Baldwin * locking. 490384fff8SJason Evans */ 500384fff8SJason Evans static void 510384fff8SJason Evans idle_setup(void *dummy) 520384fff8SJason Evans { 536caa8a15SJohn Baldwin #ifdef SMP 540bbc8826SJohn Baldwin struct pcpu *pc; 556caa8a15SJohn Baldwin #endif 566caa8a15SJohn Baldwin struct proc *p; 57e602ba25SJulian Elischer struct thread *td; 580384fff8SJason Evans int error; 590384fff8SJason Evans 60fbf70464SJulian Elischer p = NULL; /* start with no idle process */ 610384fff8SJason Evans #ifdef SMP 62d098f930SNathan Whitehorn STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) { 630384fff8SJason Evans #endif 645f66cfcaSJulian Elischer #ifdef SMP 657ab24ea3SJulian Elischer error = kproc_kthread_add(sched_idletd, NULL, &p, &td, 669ef95d01SJulian Elischer RFSTOPPED | RFHIGHPID, 0, "idle", "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, 709ef95d01SJulian Elischer RFSTOPPED | RFHIGHPID, 0, "idle", "idle"); 717ab24ea3SJulian Elischer PCPU_SET(idlethread, td); 727ab24ea3SJulian Elischer #endif 730384fff8SJason Evans if (error) 743745c395SJulian Elischer panic("idle_setup: kproc_create error %d\n", error); 750384fff8SJason Evans 76982d11f8SJeff Roberson thread_lock(td); 77bf0acc27SJohn Baldwin TD_SET_CAN_RUN(td); 781b9d701fSAttilio Rao td->td_flags |= TDF_IDLETD | TDF_NOLOAD; 798460a577SJohn Birrell sched_class(td, PRI_IDLE); 80339a7e7fSSuleiman Souhlal sched_prio(td, PRI_MAX_IDLE); 81982d11f8SJeff Roberson thread_unlock(td); 826caa8a15SJohn Baldwin #ifdef SMP 830384fff8SJason Evans } 846caa8a15SJohn Baldwin #endif 850384fff8SJason Evans } 86