1 /*- 2 * Copyright (c) 2000, All rights reserved. See /usr/src/COPYRIGHT 3 * 4 * $FreeBSD$ 5 */ 6 7 #include "opt_ktrace.h" 8 9 #include <sys/param.h> 10 #include <sys/systm.h> 11 #include <sys/proc.h> 12 #include <sys/kernel.h> 13 #include <sys/ktr.h> 14 #include <sys/signalvar.h> 15 #include <sys/resourcevar.h> 16 #include <sys/vmmeter.h> 17 #include <sys/sysctl.h> 18 #include <sys/unistd.h> 19 #include <sys/ipl.h> 20 #include <sys/kthread.h> 21 #include <sys/mutex.h> 22 #include <sys/queue.h> 23 #include <sys/eventhandler.h> 24 #include <vm/vm.h> 25 #include <vm/vm_extern.h> 26 #ifdef KTRACE 27 #include <sys/uio.h> 28 #include <sys/ktrace.h> 29 #endif 30 31 #include <machine/cpu.h> 32 #include <machine/md_var.h> 33 #include <machine/smp.h> 34 35 #include <machine/globaldata.h> 36 #include <machine/globals.h> 37 38 static void idle_setup(void *dummy); 39 SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL) 40 41 static void idle_proc(void *dummy); 42 43 /* 44 * Setup per-cpu idle process contexts. The AP's shouldn't be running or 45 * accessing their idle processes at this point, so don't bother with 46 * locking. 47 */ 48 static void 49 idle_setup(void *dummy) 50 { 51 struct globaldata *gd; 52 int error; 53 54 SLIST_FOREACH(gd, &cpuhead, gd_allcpu) { 55 #ifdef SMP 56 error = kthread_create(idle_proc, NULL, &gd->gd_idleproc, 57 RFSTOPPED|RFHIGHPID, "idle: cpu%d", 58 gd->gd_cpuid); 59 #else 60 error = kthread_create(idle_proc, NULL, &gd->gd_idleproc, 61 RFSTOPPED|RFHIGHPID, "idle"); 62 #endif 63 if (error) 64 panic("idle_setup: kthread_create error %d\n", error); 65 66 gd->gd_idleproc->p_flag |= P_NOLOAD; 67 gd->gd_idleproc->p_stat = SRUN; 68 if (gd->gd_curproc == NULL) 69 gd->gd_curproc = gd->gd_idleproc; 70 } 71 } 72 73 /* 74 * idle process context 75 */ 76 static void 77 idle_proc(void *dummy) 78 { 79 #ifdef DIAGNOSTIC 80 int count; 81 #endif 82 83 for (;;) { 84 mtx_assert(&Giant, MA_NOTOWNED); 85 86 #ifdef DIAGNOSTIC 87 count = 0; 88 89 while (count >= 0 && procrunnable() == 0) { 90 #else 91 while (procrunnable() == 0) { 92 #endif 93 /* 94 * This is a good place to put things to be done in 95 * the background, including sanity checks. 96 */ 97 98 #ifdef DIAGNOSTIC 99 if (count++ < 0) 100 CTR0(KTR_PROC, "idle_proc: timed out waiting" 101 " for a process"); 102 #endif 103 104 #if 0 105 if (vm_page_zero_idle() != 0) 106 continue; 107 #endif 108 109 #ifdef __i386__ 110 cpu_idle(); 111 #endif 112 } 113 114 mtx_lock_spin(&sched_lock); 115 curproc->p_stats->p_ru.ru_nvcsw++; 116 mi_switch(); 117 mtx_unlock_spin(&sched_lock); 118 } 119 } 120