/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (C) 2000-2004 The FreeBSD Project. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/systm.h>29#include <sys/kernel.h>30#include <sys/kthread.h>31#include <sys/lock.h>32#include <sys/mutex.h>33#include <sys/proc.h>34#include <sys/resourcevar.h>35#include <sys/sched.h>36#include <sys/unistd.h>37#ifdef SMP38#include <sys/smp.h>39#endif4041static void idle_setup(void *dummy);42SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL);4344/*45* Set up per-cpu idle process contexts. The AP's shouldn't be running or46* accessing their idle processes at this point, so don't bother with47* locking.48*/49static void50idle_setup(void *dummy)51{52#ifdef SMP53struct pcpu *pc;54#endif55struct proc *p;56struct thread *td;57int error;5859p = NULL; /* start with no idle process */60#ifdef SMP61STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {62#endif63#ifdef SMP64error = kproc_kthread_add(sched_idletd, NULL, &p, &td,65RFSTOPPED | RFHIGHPID, 0, "idle", "idle: cpu%d", pc->pc_cpuid);66pc->pc_idlethread = td;67#else68error = kproc_kthread_add(sched_idletd, NULL, &p, &td,69RFSTOPPED | RFHIGHPID, 0, "idle", "idle");70PCPU_SET(idlethread, td);71#endif72if (error)73panic("idle_setup: kproc_create error %d\n", error);7475thread_lock(td);76TD_SET_CAN_RUN(td);77td->td_flags |= TDF_IDLETD | TDF_NOLOAD;78sched_class(td, PRI_IDLE);79sched_prio(td, PRI_MAX_IDLE);80thread_unlock(td);81#ifdef SMP82}83#endif84PROC_LOCK(p);85p->p_flag |= P_IDLEPROC;86PROC_UNLOCK(p);87}888990