]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/processor.h
xnu-6153.61.1.tar.gz
[apple/xnu.git] / osfmk / kern / processor.h
1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 /*
60 * processor.h: Processor and processor-related definitions.
61 */
62
63 #ifndef _KERN_PROCESSOR_H_
64 #define _KERN_PROCESSOR_H_
65
66 #include <mach/boolean.h>
67 #include <mach/kern_return.h>
68 #include <kern/kern_types.h>
69
70 #include <sys/cdefs.h>
71
72 #ifdef MACH_KERNEL_PRIVATE
73
74 #include <mach/mach_types.h>
75 #include <kern/ast.h>
76 #include <kern/cpu_number.h>
77 #include <kern/smp.h>
78 #include <kern/simple_lock.h>
79 #include <kern/locks.h>
80 #include <kern/queue.h>
81 #include <kern/sched.h>
82 #include <kern/sched_urgency.h>
83 #include <mach/sfi_class.h>
84 #include <kern/processor_data.h>
85 #include <kern/cpu_quiesce.h>
86 #include <kern/sched_clutch.h>
87 #include <kern/assert.h>
88 #include <machine/limits.h>
89
90 /*
91 * Processor state is accessed by locking the scheduling lock
92 * for the assigned processor set.
93 *
94 * -------------------- SHUTDOWN
95 * / ^ ^
96 * _/ | \
97 * OFF_LINE ---> START ---> RUNNING ---> IDLE ---> DISPATCHING
98 * \_________________^ ^ ^______/ /
99 * \__________________/
100 *
101 * Most of these state transitions are externally driven as a
102 * a directive (for instance telling an IDLE processor to start
103 * coming out of the idle state to run a thread). However these
104 * are typically paired with a handshake by the processor itself
105 * to indicate that it has completed a transition of indeterminate
106 * length (for example, the DISPATCHING->RUNNING or START->RUNNING
107 * transitions must occur on the processor itself).
108 *
109 * The boot processor has some special cases, and skips the START state,
110 * since it has already bootstrapped and is ready to context switch threads.
111 *
112 * When a processor is in DISPATCHING or RUNNING state, the current_pri,
113 * current_thmode, and deadline fields should be set, so that other
114 * processors can evaluate if it is an appropriate candidate for preemption.
115 */
116 #if defined(CONFIG_SCHED_DEFERRED_AST)
117 /*
118 * -------------------- SHUTDOWN
119 * / ^ ^
120 * _/ | \
121 * OFF_LINE ---> START ---> RUNNING ---> IDLE ---> DISPATCHING
122 * \_________________^ ^ ^______/ ^_____ / /
123 * \__________________/
124 *
125 * A DISPATCHING processor may be put back into IDLE, if another
126 * processor determines that the target processor will have nothing to do
127 * upon reaching the RUNNING state. This is racy, but if the target
128 * responds and becomes RUNNING, it will not break the processor state
129 * machine.
130 *
131 * This change allows us to cancel an outstanding signal/AST on a processor
132 * (if such an operation is supported through hardware or software), and
133 * push the processor back into the IDLE state as a power optimization.
134 */
135 #endif
136
137 typedef enum {
138 PROCESSOR_OFF_LINE = 0, /* Not available */
139 PROCESSOR_SHUTDOWN = 1, /* Going off-line */
140 PROCESSOR_START = 2, /* Being started */
141 PROCESSOR_UNUSED = 3, /* Formerly Inactive (unavailable) */
142 PROCESSOR_IDLE = 4, /* Idle (available) */
143 PROCESSOR_DISPATCHING = 5, /* Dispatching (idle -> active) */
144 PROCESSOR_RUNNING = 6, /* Normal execution */
145 PROCESSOR_STATE_LEN = (PROCESSOR_RUNNING + 1)
146 } processor_state_t;
147
148 typedef enum {
149 PSET_SMP,
150 } pset_cluster_type_t;
151
152 typedef bitmap_t cpumap_t;
153
154 struct processor_set {
155 int online_processor_count;
156 int load_average;
157
158 int cpu_set_low, cpu_set_hi;
159 int cpu_set_count;
160 int last_chosen;
161 cpumap_t cpu_bitmask;
162 cpumap_t recommended_bitmask;
163 cpumap_t cpu_state_map[PROCESSOR_STATE_LEN];
164 cpumap_t primary_map;
165 #define SCHED_PSET_TLOCK (1)
166 #if __SMP__
167 #if defined(SCHED_PSET_TLOCK)
168 /* TODO: reorder struct for temporal cache locality */
169 __attribute__((aligned(128))) lck_ticket_t sched_lock;
170 #else /* SCHED_PSET_TLOCK*/
171 __attribute__((aligned(128))) lck_spin_t sched_lock; /* lock for above */
172 #endif /* SCHED_PSET_TLOCK*/
173 #endif
174
175 #if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_MULTIQ)
176 struct run_queue pset_runq; /* runq for this processor set */
177 #endif
178 struct rt_queue rt_runq; /* realtime runq for this processor set */
179 #if CONFIG_SCHED_CLUTCH
180 struct sched_clutch_root pset_clutch_root; /* clutch hierarchy root */
181 #endif /* CONFIG_SCHED_CLUTCH */
182
183 #if defined(CONFIG_SCHED_TRADITIONAL)
184 int pset_runq_bound_count;
185 /* # of threads in runq bound to any processor in pset */
186 #endif
187
188 /* CPUs that have been sent an unacknowledged remote AST for scheduling purposes */
189 cpumap_t pending_AST_URGENT_cpu_mask;
190 cpumap_t pending_AST_PREEMPT_cpu_mask;
191 #if defined(CONFIG_SCHED_DEFERRED_AST)
192 /*
193 * A separate mask, for ASTs that we may be able to cancel. This is dependent on
194 * some level of support for requesting an AST on a processor, and then quashing
195 * that request later.
196 *
197 * The purpose of this field (and the associated codepaths) is to infer when we
198 * no longer need a processor that is DISPATCHING to come up, and to prevent it
199 * from coming out of IDLE if possible. This should serve to decrease the number
200 * of spurious ASTs in the system, and let processors spend longer periods in
201 * IDLE.
202 */
203 cpumap_t pending_deferred_AST_cpu_mask;
204 #endif
205 cpumap_t pending_spill_cpu_mask;
206
207 struct ipc_port * pset_self; /* port for operations */
208 struct ipc_port * pset_name_self; /* port for information */
209
210 processor_set_t pset_list; /* chain of associated psets */
211 pset_node_t node;
212 uint32_t pset_cluster_id;
213 pset_cluster_type_t pset_cluster_type;
214 };
215
216 extern struct processor_set pset0;
217
218 struct pset_node {
219 processor_set_t psets; /* list of associated psets */
220 uint32_t pset_count; /* count of associated psets */
221
222 pset_node_t nodes; /* list of associated subnodes */
223 pset_node_t node_list; /* chain of associated nodes */
224
225 pset_node_t parent;
226 };
227
228 extern struct pset_node pset_node0;
229
230 extern queue_head_t tasks, terminated_tasks, threads, corpse_tasks; /* Terminated tasks are ONLY for stackshot */
231 extern int tasks_count, terminated_tasks_count, threads_count;
232 decl_lck_mtx_data(extern, tasks_threads_lock);
233 decl_lck_mtx_data(extern, tasks_corpse_lock);
234
235 struct processor {
236 processor_state_t state; /* See above */
237 bool is_SMT;
238 bool is_recommended;
239 struct thread *active_thread; /* thread running on processor */
240 struct thread *idle_thread; /* this processor's idle thread. */
241 struct thread *startup_thread;
242
243 processor_set_t processor_set; /* assigned set */
244
245 int current_pri; /* priority of current thread */
246 sfi_class_id_t current_sfi_class; /* SFI class of current thread */
247 perfcontrol_class_t current_perfctl_class; /* Perfcontrol class for current thread */
248 pset_cluster_type_t current_recommended_pset_type; /* Cluster type recommended for current thread */
249 thread_urgency_t current_urgency; /* cached urgency of current thread */
250 bool current_is_NO_SMT; /* cached TH_SFLAG_NO_SMT of current thread */
251 bool current_is_bound; /* current thread is bound to this processor */
252
253 int starting_pri; /* priority of current thread as it was when scheduled */
254 int cpu_id; /* platform numeric id */
255 cpu_quiescent_state_t cpu_quiesce_state;
256 uint64_t cpu_quiesce_last_checkin;
257
258 timer_call_data_t quantum_timer; /* timer for quantum expiration */
259 uint64_t quantum_end; /* time when current quantum ends */
260 uint64_t last_dispatch; /* time of last dispatch */
261
262 uint64_t kperf_last_sample_time; /* time of last kperf sample */
263
264 uint64_t deadline; /* current deadline */
265 bool first_timeslice; /* has the quantum expired since context switch */
266 bool processor_offlined; /* has the processor been explicitly processor_offline'ed */
267 bool must_idle; /* Needs to be forced idle as next selected thread is allowed on this processor */
268
269 processor_t processor_primary; /* pointer to primary processor for
270 * secondary SMT processors, or a pointer
271 * to ourselves for primaries or non-SMT */
272 processor_t processor_secondary;
273
274 #if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_MULTIQ)
275 struct run_queue runq; /* runq for this processor */
276 #endif
277
278 #if defined(CONFIG_SCHED_TRADITIONAL)
279 int runq_bound_count; /* # of threads bound to this processor */
280 #endif
281 #if defined(CONFIG_SCHED_GRRR)
282 struct grrr_run_queue grrr_runq; /* Group Ratio Round-Robin runq */
283 #endif
284 struct ipc_port * processor_self; /* port for operations */
285
286 processor_t processor_list; /* all existing processors */
287 processor_data_t processor_data; /* per-processor data */
288 };
289
290 extern processor_t processor_list;
291 decl_simple_lock_data(extern, processor_list_lock);
292
293 #define MAX_SCHED_CPUS 64 /* Maximum number of CPUs supported by the scheduler. bits.h:bitmap_*() macros need to be used to support greater than 64 */
294 extern processor_t processor_array[MAX_SCHED_CPUS]; /* array indexed by cpuid */
295
296 extern uint32_t processor_avail_count;
297 extern uint32_t processor_avail_count_user;
298
299 extern processor_t master_processor;
300
301 extern boolean_t sched_stats_active;
302
303 extern processor_t current_processor(void);
304
305 /* Lock macros, always acquired and released with interrupts disabled (splsched()) */
306
307 extern lck_grp_t pset_lck_grp;
308
309 #if __SMP__
310 #if defined(SCHED_PSET_TLOCK)
311 #define pset_lock_init(p) lck_ticket_init(&(p)->sched_lock)
312 #define pset_lock(p) lck_ticket_lock(&(p)->sched_lock)
313 #define pset_unlock(p) lck_ticket_unlock(&(p)->sched_lock)
314 #define pset_assert_locked(p) lck_ticket_assert_owned(&(p)->sched_lock)
315 #else /* SCHED_PSET_TLOCK*/
316 #define pset_lock_init(p) lck_spin_init(&(p)->sched_lock, &pset_lck_grp, NULL)
317 #define pset_lock(p) lck_spin_lock_grp(&(p)->sched_lock, &pset_lck_grp)
318 #define pset_unlock(p) lck_spin_unlock(&(p)->sched_lock)
319 #define pset_assert_locked(p) LCK_SPIN_ASSERT(&(p)->sched_lock, LCK_ASSERT_OWNED)
320 #endif /*!SCHED_PSET_TLOCK*/
321
322 #define rt_lock_lock(p) simple_lock(&SCHED(rt_runq)(p)->rt_lock, &pset_lck_grp)
323 #define rt_lock_unlock(p) simple_unlock(&SCHED(rt_runq)(p)->rt_lock)
324 #define rt_lock_init(p) simple_lock_init(&SCHED(rt_runq)(p)->rt_lock, 0)
325 #else
326 #define pset_lock(p) do { (void)p; } while(0)
327 #define pset_unlock(p) do { (void)p; } while(0)
328 #define pset_lock_init(p) do { (void)p; } while(0)
329 #define pset_assert_locked(p) do { (void)p; } while(0)
330
331 #define rt_lock_lock(p) do { (void)p; } while(0)
332 #define rt_lock_unlock(p) do { (void)p; } while(0)
333 #define rt_lock_init(p) do { (void)p; } while(0)
334 #endif /* SMP */
335
336 extern void processor_bootstrap(void);
337
338 extern void processor_init(
339 processor_t processor,
340 int cpu_id,
341 processor_set_t processor_set);
342
343 extern void processor_set_primary(
344 processor_t processor,
345 processor_t primary);
346
347 extern kern_return_t processor_shutdown(
348 processor_t processor);
349
350 extern kern_return_t processor_start_from_user(
351 processor_t processor);
352 extern kern_return_t processor_exit_from_user(
353 processor_t processor);
354
355 kern_return_t
356 sched_processor_enable(processor_t processor, boolean_t enable);
357
358 extern void processor_queue_shutdown(
359 processor_t processor);
360
361 extern void processor_queue_shutdown(
362 processor_t processor);
363
364 extern processor_set_t processor_pset(
365 processor_t processor);
366
367 extern pset_node_t pset_node_root(void);
368
369 extern processor_set_t pset_create(
370 pset_node_t node);
371
372 extern void pset_init(
373 processor_set_t pset,
374 pset_node_t node);
375
376 extern processor_set_t pset_find(
377 uint32_t cluster_id,
378 processor_set_t default_pset);
379
380 extern kern_return_t processor_info_count(
381 processor_flavor_t flavor,
382 mach_msg_type_number_t *count);
383
384 #define pset_deallocate(x)
385 #define pset_reference(x)
386
387 extern void machine_run_count(
388 uint32_t count);
389
390 extern processor_t machine_choose_processor(
391 processor_set_t pset,
392 processor_t processor);
393
394 #define next_pset(p) (((p)->pset_list != PROCESSOR_SET_NULL)? (p)->pset_list: (p)->node->psets)
395
396 #define PSET_THING_TASK 0
397 #define PSET_THING_THREAD 1
398
399 extern kern_return_t processor_set_things(
400 processor_set_t pset,
401 void **thing_list,
402 mach_msg_type_number_t *count,
403 int type);
404
405 extern pset_cluster_type_t recommended_pset_type(thread_t thread);
406
407 inline static bool
408 pset_is_recommended(processor_set_t pset)
409 {
410 return (pset->recommended_bitmask & pset->cpu_bitmask) != 0;
411 }
412
413 extern void processor_state_update_idle(processor_t processor);
414 extern void processor_state_update_from_thread(processor_t processor, thread_t thread);
415 extern void processor_state_update_explicit(processor_t processor, int pri,
416 sfi_class_id_t sfi_class, pset_cluster_type_t pset_type,
417 perfcontrol_class_t perfctl_class, thread_urgency_t urgency);
418
419 #define PSET_LOAD_NUMERATOR_SHIFT 16
420 #define PSET_LOAD_FRACTIONAL_SHIFT 4
421
422 inline static int
423 sched_get_pset_load_average(processor_set_t pset)
424 {
425 return pset->load_average >> (PSET_LOAD_NUMERATOR_SHIFT - PSET_LOAD_FRACTIONAL_SHIFT);
426 }
427 extern void sched_update_pset_load_average(processor_set_t pset);
428
429 inline static void
430 pset_update_processor_state(processor_set_t pset, processor_t processor, uint new_state)
431 {
432 pset_assert_locked(pset);
433
434 uint old_state = processor->state;
435 uint cpuid = processor->cpu_id;
436
437 assert(processor->processor_set == pset);
438 assert(bit_test(pset->cpu_bitmask, cpuid));
439
440 assert(old_state < PROCESSOR_STATE_LEN);
441 assert(new_state < PROCESSOR_STATE_LEN);
442
443 processor->state = new_state;
444
445 bit_clear(pset->cpu_state_map[old_state], cpuid);
446 bit_set(pset->cpu_state_map[new_state], cpuid);
447
448 if ((old_state == PROCESSOR_RUNNING) || (new_state == PROCESSOR_RUNNING)) {
449 sched_update_pset_load_average(pset);
450 if (new_state == PROCESSOR_RUNNING) {
451 assert(processor == current_processor());
452 }
453 }
454 }
455
456 #else /* MACH_KERNEL_PRIVATE */
457
458 __BEGIN_DECLS
459
460 extern void pset_deallocate(
461 processor_set_t pset);
462
463 extern void pset_reference(
464 processor_set_t pset);
465
466 __END_DECLS
467
468 #endif /* MACH_KERNEL_PRIVATE */
469
470 #ifdef KERNEL_PRIVATE
471 __BEGIN_DECLS
472 extern unsigned int processor_count;
473 extern processor_t cpu_to_processor(int cpu);
474
475 extern kern_return_t enable_smt_processors(bool enable);
476
477 extern boolean_t processor_in_panic_context(processor_t processor);
478 __END_DECLS
479
480 #endif /* KERNEL_PRIVATE */
481
482 #endif /* _KERN_PROCESSOR_H_ */