1/* $OpenBSD: sched.h,v 1.78 2026/03/31 16:46:21 deraadt Exp $ */
2/* $NetBSD: sched.h,v 1.2 1999/02/28 18:14:58 ross Exp $ */
3
4/*-
5 * Copyright (c) 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Ross Harvey.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*-
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
67 */
68
69#ifndef _SYS_SCHED_H_
70#define _SYS_SCHED_H_
71
72/*
73 * Posix defines a <sched.h> which may want to include <sys/sched.h>
74 */
75
76/*
77 * CPU states.
78 * XXX Not really scheduler state, but no other good place to put
79 * it right now, and it really is per-CPU.
80 */
81#define CP_USER 0
82#define CP_NICE 1
83#define CP_SYS 2
84#define CP_SPIN 3
85#define CP_INTR 4
86#define CP_IDLE 5
87#define CPUSTATES 6
88
89struct cpustats {
90 uint64_t cs_time[CPUSTATES]; /* CPU state statistics */
91 uint64_t cs_flags; /* see below */
92};
93
94#define CPUSTATS_ONLINE 0x0001 /* CPU is schedulable */
95
96#ifdef _KERNEL
97
98#include <sys/clockintr.h>
99#include <sys/queue.h>
100#include <sys/pclock.h>
101
102#define SCHED_NQS 32 /* 32 run queues. */
103
104struct smr_entry;
105
106/*
107 * Per-CPU scheduler state.
108 * o owned (modified only) by this CPU
109 */
110struct schedstate_percpu {
111 struct proc *spc_idleproc; /* idle proc for this cpu */
112 TAILQ_HEAD(prochead, proc) spc_qs[SCHED_NQS];
113 TAILQ_HEAD(,proc) spc_deadproc;
114 struct timespec spc_runtime; /* time curproc started running */
115 volatile int spc_schedflags; /* flags; see below */
116 u_int spc_schedticks; /* ticks for schedclock() */
117 struct pc_lock spc_cp_time_lock;
118 u_int64_t spc_cp_time[CPUSTATES]; /* CPU state statistics */
119
120 struct clockintr spc_itimer; /* [o] itimer_update handle */
121 struct clockintr spc_profclock; /* [o] profclock handle */
122 struct clockintr spc_roundrobin;/* [o] roundrobin handle */
123 struct clockintr spc_statclock; /* [o] statclock handle */
124
125 u_int spc_nrun; /* procs on the run queues */
126
127 volatile uint32_t spc_whichqs;
128 volatile u_int spc_spinning; /* this cpu is currently spinning */
129
130 SIMPLEQ_HEAD(, smr_entry) spc_deferred; /* deferred smr calls */
131 u_int spc_ndeferred; /* number of deferred smr calls */
132 u_int spc_smrdepth; /* level of smr nesting */
133 u_char spc_smrexpedite; /* if set, dispatch smr entries
134 * without delay */
135 u_char spc_smrgp; /* this CPU's view of grace period */
136 volatile u_char spc_curpriority; /* [o] usrpri of curproc */
137};
138
139/* spc_flags */
140#define SPCF_SEENRR 0x0001 /* process has seen roundrobin() */
141#define SPCF_SHOULDYIELD 0x0002 /* process should yield the CPU */
142#define SPCF_SWITCHCLEAR (SPCF_SEENRR|SPCF_SHOULDYIELD)
143#define SPCF_SHOULDHALT 0x0004 /* CPU should be vacated */
144#define SPCF_HALTED 0x0008 /* CPU has been halted */
145#define SPCF_PROFCLOCK 0x0010 /* profclock() was started */
146#define SPCF_ITIMER 0x0020 /* itimer_update() was started */
147
148#define SCHED_PPQ (128 / SCHED_NQS) /* priorities per queue */
149#define NICE_WEIGHT 2 /* priorities per nice level */
150#define ESTCPULIM(e) min((e), NICE_WEIGHT * PRIO_MAX - SCHED_PPQ)
151
152extern uint64_t roundrobin_period;
153
154struct proc;
155void schedclock(struct proc *);
156struct clockrequest;
157void roundrobin(struct clockrequest *, void *, void *);
158void scheduler_start(void);
159void userret(struct proc *p);
160
161struct cpu_info;
162void sched_init(void);
163void sched_init_cpu(struct cpu_info *);
164void sched_idle(void *);
165void sched_exit(struct proc *);
166void sched_toidle(void);
167void mi_switch(void);
168void cpu_switchto(struct proc *, struct proc *);
169struct proc *sched_chooseproc(void);
170struct cpu_info *sched_choosecpu(struct proc *);
171struct cpu_info *sched_choosecpu_fork(struct proc *parent, int);
172void cpu_idle_enter(void);
173void cpu_idle_cycle(void);
174void cpu_idle_leave(void);
175void sched_peg_curproc(struct cpu_info *ci);
176void sched_unpeg_curproc(void);
177void sched_barrier(struct cpu_info *ci);
178
179int sysctl_hwsetperf(void *, size_t *, void *, size_t);
180int sysctl_hwperfpolicy(void *, size_t *, void *, size_t);
181int sysctl_hwsmt(void *, size_t *, void *, size_t);
182int sysctl_hwblockcpu(void *, size_t *, void *, size_t);
183int sysctl_hwncpuonline(void);
184
185#define CPUTYP_SMT 0x01 /* SMT cpu */
186#define CPUTYP_P 0x02 /* Performance core */
187#define CPUTYP_E 0x04 /* Efficiency core */
188#define CPUTYP_L 0x08 /* Lethargic, Low Power Efficiency core */
189extern int sched_blockcpu;
190
191#ifdef MULTIPROCESSOR
192void sched_start_secondary_cpus(void);
193void sched_stop_secondary_cpus(void);
194#endif
195
196#define cpu_is_idle(ci) ((ci)->ci_schedstate.spc_whichqs == 0)
197int cpu_is_online(struct cpu_info *);
198
199void setrunqueue(struct cpu_info *, struct proc *, uint8_t);
200void remrunqueue(struct proc *);
201
202/* Chargeback parents for the sins of their children. */
203#define scheduler_wait_hook(parent, child) do { \
204 (parent)->p_estcpu = ESTCPULIM((parent)->p_estcpu + (child)->p_estcpu);\
205} while (0)
206
207/* Allow other processes to progress */
208#define sched_pause(func) do { \
209 if (curcpu()->ci_schedstate.spc_schedflags & SPCF_SHOULDYIELD) \
210 func(); \
211} while (0)
212
213extern struct mutex sched_lock;
214
215#define SCHED_ASSERT_LOCKED() MUTEX_ASSERT_LOCKED(&sched_lock)
216#define SCHED_ASSERT_UNLOCKED() MUTEX_ASSERT_UNLOCKED(&sched_lock)
217
218#define SCHED_LOCK_INIT() mtx_init(&sched_lock, IPL_SCHED)
219#define SCHED_LOCK() mtx_enter(&sched_lock)
220#define SCHED_UNLOCK() mtx_leave(&sched_lock)
221
222#endif /* _KERNEL */
223#endif /* _SYS_SCHED_H_ */