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