xref: /freebsd/sys/kern/kern_idle.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*-
2  * Copyright (c) 2000, All rights reserved.  See /usr/src/COPYRIGHT
3  *
4  */
5 
6 #include <sys/cdefs.h>
7 __FBSDID("$FreeBSD$");
8 
9 #include "opt_ktrace.h"
10 
11 #include <sys/param.h>
12 #include <sys/systm.h>
13 #include <sys/kernel.h>
14 #include <sys/ktr.h>
15 #include <sys/kthread.h>
16 #include <sys/lock.h>
17 #include <sys/mutex.h>
18 #include <sys/pcpu.h>
19 #include <sys/proc.h>
20 #include <sys/resourcevar.h>
21 #include <sys/sched.h>
22 #include <sys/smp.h>
23 #include <sys/unistd.h>
24 #ifdef KTRACE
25 #include <sys/uio.h>
26 #include <sys/ktrace.h>
27 #endif
28 
29 static void idle_setup(void *dummy);
30 SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL)
31 
32 static void idle_proc(void *dummy);
33 
34 /*
35  * Setup per-cpu idle process contexts.  The AP's shouldn't be running or
36  * accessing their idle processes at this point, so don't bother with
37  * locking.
38  */
39 static void
40 idle_setup(void *dummy)
41 {
42 #ifdef SMP
43 	struct pcpu *pc;
44 #endif
45 	struct proc *p;
46 	struct thread *td;
47 	int error;
48 
49 #ifdef SMP
50 	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
51 		error = kthread_create(idle_proc, NULL, &p,
52 		    RFSTOPPED | RFHIGHPID, 0, "idle: cpu%d", pc->pc_cpuid);
53 		pc->pc_idlethread = FIRST_THREAD_IN_PROC(p);
54 		if (pc->pc_curthread == NULL) {
55 			pc->pc_curthread = pc->pc_idlethread;
56 			pc->pc_idlethread->td_critnest = 0;
57 		}
58 #else
59 		error = kthread_create(idle_proc, NULL, &p,
60 		    RFSTOPPED | RFHIGHPID, 0, "idle");
61 		PCPU_SET(idlethread, FIRST_THREAD_IN_PROC(p));
62 #endif
63 		if (error)
64 			panic("idle_setup: kthread_create error %d\n", error);
65 
66 		PROC_LOCK(p);
67 		p->p_flag |= P_NOLOAD;
68 		mtx_lock_spin(&sched_lock);
69 		p->p_state = PRS_NORMAL;
70 		td = FIRST_THREAD_IN_PROC(p);
71 		td->td_state = TDS_CAN_RUN;
72 		td->td_flags |= TDF_IDLETD;
73 		mtx_unlock_spin(&sched_lock);
74 		PROC_UNLOCK(p);
75 #ifdef SMP
76 	}
77 #endif
78 }
79 
80 /*
81  * idle process context
82  */
83 static void
84 idle_proc(void *dummy)
85 {
86 #ifdef DIAGNOSTIC
87 	int count;
88 #endif
89 	struct thread *td;
90 	struct proc *p;
91 
92 	td = curthread;
93 	p = td->td_proc;
94 	for (;;) {
95 		mtx_assert(&Giant, MA_NOTOWNED);
96 
97 #ifdef DIAGNOSTIC
98 		count = 0;
99 
100 		while (count >= 0 && sched_runnable() == 0) {
101 #else
102 		while (sched_runnable() == 0) {
103 #endif
104 		/*
105 		 * This is a good place to put things to be done in
106 		 * the background, including sanity checks.
107 		 */
108 
109 #ifdef DIAGNOSTIC
110 			if (count++ < 0)
111 				CTR0(KTR_PROC, "idle_proc: timed out waiting"
112 				    " for a process");
113 #endif
114 
115 #ifdef __i386__
116 			cpu_idle();
117 #endif
118 		}
119 
120 		mtx_lock_spin(&sched_lock);
121 		p->p_stats->p_ru.ru_nvcsw++;
122 		td->td_state = TDS_CAN_RUN;
123 		mi_switch();
124 		mtx_unlock_spin(&sched_lock);
125 	}
126 }
127