xref: /xnu-10002.1.13/osfmk/kern/sched_prim.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1 /*
2  * Copyright (c) 2000-2016 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_FREE_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989,1988,1987 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  [email protected]
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  *	File:	sched_prim.c
60  *	Author:	Avadis Tevanian, Jr.
61  *	Date:	1986
62  *
63  *	Scheduling primitives
64  *
65  */
66 
67 #include <debug.h>
68 
69 #include <mach/mach_types.h>
70 #include <mach/machine.h>
71 #include <mach/policy.h>
72 #include <mach/sync_policy.h>
73 #include <mach/thread_act.h>
74 
75 #include <machine/machine_routines.h>
76 #include <machine/sched_param.h>
77 #include <machine/machine_cpu.h>
78 #include <machine/limits.h>
79 #include <machine/atomic.h>
80 
81 #include <machine/commpage.h>
82 
83 #include <kern/kern_types.h>
84 #include <kern/backtrace.h>
85 #include <kern/clock.h>
86 #include <kern/cpu_number.h>
87 #include <kern/cpu_data.h>
88 #include <kern/smp.h>
89 #include <kern/debug.h>
90 #include <kern/macro_help.h>
91 #include <kern/machine.h>
92 #include <kern/misc_protos.h>
93 #if MONOTONIC
94 #include <kern/monotonic.h>
95 #endif /* MONOTONIC */
96 #include <kern/processor.h>
97 #include <kern/queue.h>
98 #include <kern/recount.h>
99 #include <kern/restartable.h>
100 #include <kern/sched.h>
101 #include <kern/sched_prim.h>
102 #include <kern/sfi.h>
103 #include <kern/syscall_subr.h>
104 #include <kern/task.h>
105 #include <kern/thread.h>
106 #include <kern/thread_group.h>
107 #include <kern/ledger.h>
108 #include <kern/timer_queue.h>
109 #include <kern/waitq.h>
110 #include <kern/policy_internal.h>
111 
112 #include <vm/pmap.h>
113 #include <vm/vm_kern.h>
114 #include <vm/vm_map.h>
115 #include <vm/vm_pageout.h>
116 
117 #include <mach/sdt.h>
118 #include <mach/mach_host.h>
119 #include <mach/host_info.h>
120 
121 #include <sys/kdebug.h>
122 #include <kperf/kperf.h>
123 #include <kern/kpc.h>
124 #include <san/kasan.h>
125 #include <kern/pms.h>
126 #include <kern/host.h>
127 #include <stdatomic.h>
128 #include <os/atomic_private.h>
129 
130 #ifdef KDBG_MACOS_RELEASE
131 #define KTRC KDBG_MACOS_RELEASE
132 #else
133 #define KTRC KDBG_RELEASE
134 #endif
135 
136 struct sched_statistics PERCPU_DATA(sched_stats);
137 bool sched_stats_active;
138 
139 static uint64_t
deadline_add(uint64_t d,uint64_t e)140 deadline_add(uint64_t d, uint64_t e)
141 {
142 	uint64_t sum;
143 	return os_add_overflow(d, e, &sum) ? UINT64_MAX : sum;
144 }
145 
146 int
rt_runq_count(processor_set_t pset)147 rt_runq_count(processor_set_t pset)
148 {
149 	return os_atomic_load(&SCHED(rt_runq)(pset)->count, relaxed);
150 }
151 
152 uint64_t
rt_runq_earliest_deadline(processor_set_t pset)153 rt_runq_earliest_deadline(processor_set_t pset)
154 {
155 	return os_atomic_load_wide(&SCHED(rt_runq)(pset)->earliest_deadline, relaxed);
156 }
157 
158 static int
rt_runq_priority(processor_set_t pset)159 rt_runq_priority(processor_set_t pset)
160 {
161 	pset_assert_locked(pset);
162 	rt_queue_t rt_run_queue = SCHED(rt_runq)(pset);
163 
164 	bitmap_t *map = rt_run_queue->bitmap;
165 	int i = bitmap_first(map, NRTQS);
166 	assert(i < NRTQS);
167 
168 	if (i >= 0) {
169 		return i + BASEPRI_RTQUEUES;
170 	}
171 
172 	return i;
173 }
174 
175 static thread_t rt_runq_first(rt_queue_t rt_runq);
176 
177 #if DEBUG
178 static void
check_rt_runq_consistency(rt_queue_t rt_run_queue,thread_t thread)179 check_rt_runq_consistency(rt_queue_t rt_run_queue, thread_t thread)
180 {
181 	bitmap_t *map = rt_run_queue->bitmap;
182 
183 	uint64_t earliest_deadline = RT_DEADLINE_NONE;
184 	uint32_t constraint = RT_CONSTRAINT_NONE;
185 	int ed_index = NOPRI;
186 	int count = 0;
187 	bool found_thread = false;
188 
189 	for (int pri = BASEPRI_RTQUEUES; pri <= MAXPRI; pri++) {
190 		int i = pri - BASEPRI_RTQUEUES;
191 		rt_queue_pri_t *rt_runq = &rt_run_queue->rt_queue_pri[i];
192 		queue_t queue = &rt_runq->pri_queue;
193 		queue_entry_t iter;
194 		int n = 0;
195 		uint64_t previous_deadline = 0;
196 		qe_foreach(iter, queue) {
197 			thread_t iter_thread = qe_element(iter, struct thread, runq_links);
198 			assert_thread_magic(iter_thread);
199 			if (iter_thread == thread) {
200 				found_thread = true;
201 			}
202 			assert(iter_thread->sched_pri == (i + BASEPRI_RTQUEUES));
203 			assert(iter_thread->realtime.deadline < RT_DEADLINE_NONE);
204 			assert(iter_thread->realtime.constraint < RT_CONSTRAINT_NONE);
205 			assert(previous_deadline <= iter_thread->realtime.deadline);
206 			n++;
207 			if (iter == queue_first(queue)) {
208 				assert(rt_runq->pri_earliest_deadline == iter_thread->realtime.deadline);
209 				assert(rt_runq->pri_constraint == iter_thread->realtime.constraint);
210 			}
211 			previous_deadline = iter_thread->realtime.deadline;
212 		}
213 		assert(n == rt_runq->pri_count);
214 		if (n == 0) {
215 			assert(bitmap_test(map, i) == false);
216 			assert(rt_runq->pri_earliest_deadline == RT_DEADLINE_NONE);
217 			assert(rt_runq->pri_constraint == RT_CONSTRAINT_NONE);
218 		} else {
219 			assert(bitmap_test(map, i) == true);
220 		}
221 		if (rt_runq->pri_earliest_deadline < earliest_deadline) {
222 			earliest_deadline = rt_runq->pri_earliest_deadline;
223 			constraint = rt_runq->pri_constraint;
224 			ed_index = i;
225 		}
226 		count += n;
227 	}
228 	assert(os_atomic_load_wide(&rt_run_queue->earliest_deadline, relaxed) == earliest_deadline);
229 	assert(os_atomic_load(&rt_run_queue->count, relaxed) == count);
230 	assert(os_atomic_load(&rt_run_queue->constraint, relaxed) == constraint);
231 	assert(os_atomic_load(&rt_run_queue->ed_index, relaxed) == ed_index);
232 	if (thread) {
233 		assert(found_thread);
234 	}
235 }
236 #define CHECK_RT_RUNQ_CONSISTENCY(q, th)    check_rt_runq_consistency(q, th)
237 #else
238 #define CHECK_RT_RUNQ_CONSISTENCY(q, th)    do {} while (0)
239 #endif
240 
241 uint32_t rt_constraint_threshold;
242 
243 static bool
rt_runq_is_low_latency(processor_set_t pset)244 rt_runq_is_low_latency(processor_set_t pset)
245 {
246 	return os_atomic_load(&SCHED(rt_runq)(pset)->constraint, relaxed) <= rt_constraint_threshold;
247 }
248 
249 TUNABLE(bool, cpulimit_affects_quantum, "cpulimit_affects_quantum", true);
250 
251 /* TODO: enable this, to 50us (less than the deferred IPI latency, to beat a spill) */
252 TUNABLE(uint32_t, nonurgent_preemption_timer_us, "nonurgent_preemption_timer", 0); /* microseconds */
253 static uint64_t nonurgent_preemption_timer_abs = 0;
254 
255 #define         DEFAULT_PREEMPTION_RATE         100             /* (1/s) */
256 TUNABLE(int, default_preemption_rate, "preempt", DEFAULT_PREEMPTION_RATE);
257 
258 #define         DEFAULT_BG_PREEMPTION_RATE      400             /* (1/s) */
259 TUNABLE(int, default_bg_preemption_rate, "bg_preempt", DEFAULT_BG_PREEMPTION_RATE);
260 
261 #define         MAX_UNSAFE_RT_QUANTA               100
262 #define         SAFE_RT_MULTIPLIER                 2
263 
264 #define         MAX_UNSAFE_FIXED_QUANTA               100
265 #define         SAFE_FIXED_MULTIPLIER                 2
266 
267 TUNABLE_DEV_WRITEABLE(int, max_unsafe_rt_quanta, "max_unsafe_rt_quanta", MAX_UNSAFE_RT_QUANTA);
268 TUNABLE_DEV_WRITEABLE(int, max_unsafe_fixed_quanta, "max_unsafe_fixed_quanta", MAX_UNSAFE_FIXED_QUANTA);
269 
270 TUNABLE_DEV_WRITEABLE(int, safe_rt_multiplier, "safe_rt_multiplier", SAFE_RT_MULTIPLIER);
271 TUNABLE_DEV_WRITEABLE(int, safe_fixed_multiplier, "safe_fixed_multiplier", SAFE_RT_MULTIPLIER);
272 
273 #define         MAX_POLL_QUANTA                 2
274 TUNABLE(int, max_poll_quanta, "poll", MAX_POLL_QUANTA);
275 
276 #define         SCHED_POLL_YIELD_SHIFT          4               /* 1/16 */
277 int             sched_poll_yield_shift = SCHED_POLL_YIELD_SHIFT;
278 
279 uint64_t        max_poll_computation;
280 
281 uint64_t        max_unsafe_rt_computation;
282 uint64_t        max_unsafe_fixed_computation;
283 uint64_t        sched_safe_rt_duration;
284 uint64_t        sched_safe_fixed_duration;
285 
286 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
287 
288 uint32_t        std_quantum;
289 uint32_t        min_std_quantum;
290 uint32_t        bg_quantum;
291 
292 uint32_t        std_quantum_us;
293 uint32_t        bg_quantum_us;
294 
295 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
296 
297 uint32_t        thread_depress_time;
298 uint32_t        default_timeshare_computation;
299 uint32_t        default_timeshare_constraint;
300 
301 uint32_t        max_rt_quantum;
302 uint32_t        min_rt_quantum;
303 
304 uint32_t        rt_deadline_epsilon;
305 
306 uint32_t        rt_constraint_threshold;
307 
308 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
309 
310 unsigned                sched_tick;
311 uint32_t                sched_tick_interval;
312 
313 /* Timeshare load calculation interval (15ms) */
314 uint32_t                sched_load_compute_interval_us = 15000;
315 uint64_t                sched_load_compute_interval_abs;
316 static _Atomic uint64_t sched_load_compute_deadline;
317 
318 uint32_t        sched_pri_shifts[TH_BUCKET_MAX];
319 uint32_t        sched_fixed_shift;
320 
321 uint32_t        sched_decay_usage_age_factor = 1; /* accelerate 5/8^n usage aging */
322 
323 /* Allow foreground to decay past default to resolve inversions */
324 #define DEFAULT_DECAY_BAND_LIMIT ((BASEPRI_FOREGROUND - BASEPRI_DEFAULT) + 2)
325 int             sched_pri_decay_band_limit = DEFAULT_DECAY_BAND_LIMIT;
326 
327 /* Defaults for timer deadline profiling */
328 #define TIMER_DEADLINE_TRACKING_BIN_1_DEFAULT 2000000 /* Timers with deadlines <=
329 	                                               * 2ms */
330 #define TIMER_DEADLINE_TRACKING_BIN_2_DEFAULT 5000000 /* Timers with deadlines
331 	                                               *   <= 5ms */
332 
333 uint64_t timer_deadline_tracking_bin_1;
334 uint64_t timer_deadline_tracking_bin_2;
335 
336 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
337 
338 thread_t sched_maintenance_thread;
339 
340 /* interrupts disabled lock to guard recommended cores state */
341 decl_simple_lock_data(, sched_available_cores_lock);
342 uint64_t        perfcontrol_requested_recommended_cores = ALL_CORES_RECOMMENDED;
343 uint64_t        perfcontrol_system_requested_recommended_cores = ALL_CORES_RECOMMENDED;
344 uint64_t        perfcontrol_user_requested_recommended_cores = ALL_CORES_RECOMMENDED;
345 static uint64_t usercontrol_requested_recommended_cores = ALL_CORES_RECOMMENDED;
346 static uint64_t sched_online_processors = 0;
347 static void sched_update_recommended_cores(uint64_t recommended_cores, processor_reason_t reason, uint32_t flags);
348 static void sched_update_powered_cores(uint64_t reqested_powered_cores, processor_reason_t reason, uint32_t flags);
349 
350 #if __arm64__
351 static void sched_recommended_cores_maintenance(void);
352 uint64_t    perfcontrol_failsafe_starvation_threshold;
353 extern char *proc_name_address(struct proc *p);
354 #endif /* __arm64__ */
355 
356 uint64_t        sched_one_second_interval;
357 boolean_t       allow_direct_handoff = TRUE;
358 
359 /* Forwards */
360 
361 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
362 
363 static void load_shift_init(void);
364 static void preempt_pri_init(void);
365 
366 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
367 
368 thread_t        processor_idle(
369 	thread_t                        thread,
370 	processor_t                     processor);
371 
372 static ast_t
373 csw_check_locked(
374 	thread_t        thread,
375 	processor_t     processor,
376 	processor_set_t pset,
377 	ast_t           check_reason);
378 
379 static void processor_setrun(
380 	processor_t                    processor,
381 	thread_t                       thread,
382 	integer_t                      options);
383 
384 static void
385 sched_realtime_timebase_init(void);
386 
387 static void
388 sched_timer_deadline_tracking_init(void);
389 
390 #if     DEBUG
391 extern int debug_task;
392 #define TLOG(a, fmt, args...) if(debug_task & a) kprintf(fmt, ## args)
393 #else
394 #define TLOG(a, fmt, args...) do {} while (0)
395 #endif
396 
397 static processor_t
398 thread_bind_internal(
399 	thread_t                thread,
400 	processor_t             processor);
401 
402 static void
403 sched_vm_group_maintenance(void);
404 
405 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
406 int8_t          sched_load_shifts[NRQS];
407 bitmap_t        sched_preempt_pri[BITMAP_LEN(NRQS_MAX)];
408 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
409 
410 /*
411  * Statically allocate a buffer to hold the longest possible
412  * scheduler description string, as currently implemented.
413  * bsd/kern/kern_sysctl.c has a corresponding definition in bsd/
414  * to export to userspace via sysctl(3). If either version
415  * changes, update the other.
416  *
417  * Note that in addition to being an upper bound on the strings
418  * in the kernel, it's also an exact parameter to PE_get_default(),
419  * which interrogates the device tree on some platforms. That
420  * API requires the caller know the exact size of the device tree
421  * property, so we need both a legacy size (32) and the current size
422  * (48) to deal with old and new device trees. The device tree property
423  * is similarly padded to a fixed size so that the same kernel image
424  * can run on multiple devices with different schedulers configured
425  * in the device tree.
426  */
427 char sched_string[SCHED_STRING_MAX_LENGTH];
428 
429 uint32_t sched_debug_flags = SCHED_DEBUG_FLAG_CHOOSE_PROCESSOR_TRACEPOINTS;
430 
431 /* Global flag which indicates whether Background Stepper Context is enabled */
432 static int cpu_throttle_enabled = 1;
433 
434 #if DEVELOPMENT || DEBUG
435 int enable_task_set_cluster_type = 0;
436 bool system_ecore_only = false;
437 #endif /* DEVELOPMENT || DEBUG */
438 
439 void
sched_init(void)440 sched_init(void)
441 {
442 	boolean_t direct_handoff = FALSE;
443 	kprintf("Scheduler: Default of %s\n", SCHED(sched_name));
444 
445 	if (!PE_parse_boot_argn("sched_pri_decay_limit", &sched_pri_decay_band_limit, sizeof(sched_pri_decay_band_limit))) {
446 		/* No boot-args, check in device tree */
447 		if (!PE_get_default("kern.sched_pri_decay_limit",
448 		    &sched_pri_decay_band_limit,
449 		    sizeof(sched_pri_decay_band_limit))) {
450 			/* Allow decay all the way to normal limits */
451 			sched_pri_decay_band_limit = DEFAULT_DECAY_BAND_LIMIT;
452 		}
453 	}
454 
455 	kprintf("Setting scheduler priority decay band limit %d\n", sched_pri_decay_band_limit);
456 
457 	if (PE_parse_boot_argn("sched_debug", &sched_debug_flags, sizeof(sched_debug_flags))) {
458 		kprintf("Scheduler: Debug flags 0x%08x\n", sched_debug_flags);
459 	}
460 	strlcpy(sched_string, SCHED(sched_name), sizeof(sched_string));
461 
462 #if __arm64__
463 	clock_interval_to_absolutetime_interval(expecting_ipi_wfe_timeout_usec, NSEC_PER_USEC, &expecting_ipi_wfe_timeout_mt);
464 #endif /* __arm64__ */
465 
466 	SCHED(init)();
467 	SCHED(rt_init)(&pset0);
468 	sched_timer_deadline_tracking_init();
469 
470 	SCHED(pset_init)(&pset0);
471 	SCHED(processor_init)(master_processor);
472 
473 	if (PE_parse_boot_argn("direct_handoff", &direct_handoff, sizeof(direct_handoff))) {
474 		allow_direct_handoff = direct_handoff;
475 	}
476 
477 #if DEVELOPMENT || DEBUG
478 	if (PE_parse_boot_argn("enable_skstsct", &enable_task_set_cluster_type, sizeof(enable_task_set_cluster_type))) {
479 		system_ecore_only = (enable_task_set_cluster_type == 2);
480 	}
481 #endif /* DEVELOPMENT || DEBUG */
482 
483 	simple_lock_init(&sched_available_cores_lock, 0);
484 }
485 
486 void
sched_timebase_init(void)487 sched_timebase_init(void)
488 {
489 	uint64_t        abstime;
490 
491 	clock_interval_to_absolutetime_interval(1, NSEC_PER_SEC, &abstime);
492 	sched_one_second_interval = abstime;
493 
494 	SCHED(timebase_init)();
495 	sched_realtime_timebase_init();
496 }
497 
498 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
499 
500 void
sched_timeshare_init(void)501 sched_timeshare_init(void)
502 {
503 	/*
504 	 * Calculate the timeslicing quantum
505 	 * in us.
506 	 */
507 	if (default_preemption_rate < 1) {
508 		default_preemption_rate = DEFAULT_PREEMPTION_RATE;
509 	}
510 	std_quantum_us = (1000 * 1000) / default_preemption_rate;
511 
512 	printf("standard timeslicing quantum is %d us\n", std_quantum_us);
513 
514 	if (default_bg_preemption_rate < 1) {
515 		default_bg_preemption_rate = DEFAULT_BG_PREEMPTION_RATE;
516 	}
517 	bg_quantum_us = (1000 * 1000) / default_bg_preemption_rate;
518 
519 	printf("standard background quantum is %d us\n", bg_quantum_us);
520 
521 	load_shift_init();
522 	preempt_pri_init();
523 	sched_tick = 0;
524 }
525 
526 void
sched_set_max_unsafe_rt_quanta(int max)527 sched_set_max_unsafe_rt_quanta(int max)
528 {
529 	const uint32_t quantum_size = SCHED(initial_quantum_size)(THREAD_NULL);
530 
531 	max_unsafe_rt_computation = ((uint64_t)max) * quantum_size;
532 
533 	const int mult = safe_rt_multiplier <= 0 ? 2 : safe_rt_multiplier;
534 	sched_safe_rt_duration = mult * ((uint64_t)max) * quantum_size;
535 
536 
537 #if DEVELOPMENT || DEBUG
538 	max_unsafe_rt_quanta = max;
539 #else
540 	/*
541 	 * On RELEASE kernels, this is only called on boot where
542 	 * max is already equal to max_unsafe_rt_quanta.
543 	 */
544 	assert3s(max, ==, max_unsafe_rt_quanta);
545 #endif
546 }
547 
548 void
sched_set_max_unsafe_fixed_quanta(int max)549 sched_set_max_unsafe_fixed_quanta(int max)
550 {
551 	const uint32_t quantum_size = SCHED(initial_quantum_size)(THREAD_NULL);
552 
553 	max_unsafe_fixed_computation = ((uint64_t)max) * quantum_size;
554 
555 	const int mult = safe_fixed_multiplier <= 0 ? 2 : safe_fixed_multiplier;
556 	sched_safe_fixed_duration = mult * ((uint64_t)max) * quantum_size;
557 
558 #if DEVELOPMENT || DEBUG
559 	max_unsafe_fixed_quanta = max;
560 #else
561 	/*
562 	 * On RELEASE kernels, this is only called on boot where
563 	 * max is already equal to max_unsafe_fixed_quanta.
564 	 */
565 	assert3s(max, ==, max_unsafe_fixed_quanta);
566 #endif
567 }
568 
569 void
sched_timeshare_timebase_init(void)570 sched_timeshare_timebase_init(void)
571 {
572 	uint64_t        abstime;
573 	uint32_t        shift;
574 
575 	/* standard timeslicing quantum */
576 	clock_interval_to_absolutetime_interval(
577 		std_quantum_us, NSEC_PER_USEC, &abstime);
578 	assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
579 	std_quantum = (uint32_t)abstime;
580 
581 	/* smallest remaining quantum (250 us) */
582 	clock_interval_to_absolutetime_interval(250, NSEC_PER_USEC, &abstime);
583 	assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
584 	min_std_quantum = (uint32_t)abstime;
585 
586 	/* quantum for background tasks */
587 	clock_interval_to_absolutetime_interval(
588 		bg_quantum_us, NSEC_PER_USEC, &abstime);
589 	assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
590 	bg_quantum = (uint32_t)abstime;
591 
592 	/* scheduler tick interval */
593 	clock_interval_to_absolutetime_interval(USEC_PER_SEC >> SCHED_TICK_SHIFT,
594 	    NSEC_PER_USEC, &abstime);
595 	assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
596 	sched_tick_interval = (uint32_t)abstime;
597 
598 	/* timeshare load calculation interval & deadline initialization */
599 	clock_interval_to_absolutetime_interval(sched_load_compute_interval_us, NSEC_PER_USEC, &sched_load_compute_interval_abs);
600 	os_atomic_init(&sched_load_compute_deadline, sched_load_compute_interval_abs);
601 
602 	/*
603 	 * Compute conversion factor from usage to
604 	 * timesharing priorities with 5/8 ** n aging.
605 	 */
606 	abstime = (abstime * 5) / 3;
607 	for (shift = 0; abstime > BASEPRI_DEFAULT; ++shift) {
608 		abstime >>= 1;
609 	}
610 	sched_fixed_shift = shift;
611 
612 	for (uint32_t i = 0; i < TH_BUCKET_MAX; i++) {
613 		sched_pri_shifts[i] = INT8_MAX;
614 	}
615 
616 	sched_set_max_unsafe_rt_quanta(max_unsafe_rt_quanta);
617 	sched_set_max_unsafe_fixed_quanta(max_unsafe_fixed_quanta);
618 
619 	max_poll_computation = ((uint64_t)max_poll_quanta) * std_quantum;
620 	thread_depress_time = 1 * std_quantum;
621 	default_timeshare_computation = std_quantum / 2;
622 	default_timeshare_constraint = std_quantum;
623 
624 #if __arm64__
625 	perfcontrol_failsafe_starvation_threshold = (2 * sched_tick_interval);
626 #endif /* __arm64__ */
627 
628 	if (nonurgent_preemption_timer_us) {
629 		clock_interval_to_absolutetime_interval(nonurgent_preemption_timer_us, NSEC_PER_USEC, &abstime);
630 		nonurgent_preemption_timer_abs = abstime;
631 	}
632 }
633 
634 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
635 
636 void
pset_rt_init(processor_set_t pset)637 pset_rt_init(processor_set_t pset)
638 {
639 	for (int pri = BASEPRI_RTQUEUES; pri <= MAXPRI; pri++) {
640 		int i = pri - BASEPRI_RTQUEUES;
641 		rt_queue_pri_t *rqi = &pset->rt_runq.rt_queue_pri[i];
642 		queue_init(&rqi->pri_queue);
643 		rqi->pri_count = 0;
644 		rqi->pri_earliest_deadline = RT_DEADLINE_NONE;
645 		rqi->pri_constraint = RT_CONSTRAINT_NONE;
646 	}
647 	os_atomic_init(&pset->rt_runq.count, 0);
648 	os_atomic_init(&pset->rt_runq.earliest_deadline, RT_DEADLINE_NONE);
649 	os_atomic_init(&pset->rt_runq.constraint, RT_CONSTRAINT_NONE);
650 	os_atomic_init(&pset->rt_runq.ed_index, NOPRI);
651 	memset(&pset->rt_runq.runq_stats, 0, sizeof pset->rt_runq.runq_stats);
652 }
653 
654 /* epsilon for comparing RT deadlines */
655 int rt_deadline_epsilon_us = 100;
656 
657 int
sched_get_rt_deadline_epsilon(void)658 sched_get_rt_deadline_epsilon(void)
659 {
660 	return rt_deadline_epsilon_us;
661 }
662 
663 void
sched_set_rt_deadline_epsilon(int new_epsilon_us)664 sched_set_rt_deadline_epsilon(int new_epsilon_us)
665 {
666 	rt_deadline_epsilon_us = new_epsilon_us;
667 
668 	uint64_t abstime;
669 	clock_interval_to_absolutetime_interval(rt_deadline_epsilon_us, NSEC_PER_USEC, &abstime);
670 	assert((abstime >> 32) == 0 && ((rt_deadline_epsilon_us == 0) || (uint32_t)abstime != 0));
671 	rt_deadline_epsilon = (uint32_t)abstime;
672 }
673 
674 static void
sched_realtime_timebase_init(void)675 sched_realtime_timebase_init(void)
676 {
677 	uint64_t abstime;
678 
679 	/* smallest rt computation (50 us) */
680 	clock_interval_to_absolutetime_interval(50, NSEC_PER_USEC, &abstime);
681 	assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
682 	min_rt_quantum = (uint32_t)abstime;
683 
684 	/* maximum rt computation (50 ms) */
685 	clock_interval_to_absolutetime_interval(
686 		50, 1000 * NSEC_PER_USEC, &abstime);
687 	assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
688 	max_rt_quantum = (uint32_t)abstime;
689 
690 	/* constraint threshold for sending backup IPIs (4 ms) */
691 	clock_interval_to_absolutetime_interval(4, NSEC_PER_MSEC, &abstime);
692 	assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
693 	rt_constraint_threshold = (uint32_t)abstime;
694 
695 	/* epsilon for comparing deadlines */
696 	sched_set_rt_deadline_epsilon(rt_deadline_epsilon_us);
697 }
698 
699 void
sched_check_spill(processor_set_t pset,thread_t thread)700 sched_check_spill(processor_set_t pset, thread_t thread)
701 {
702 	(void)pset;
703 	(void)thread;
704 
705 	return;
706 }
707 
708 bool
sched_thread_should_yield(processor_t processor,thread_t thread)709 sched_thread_should_yield(processor_t processor, thread_t thread)
710 {
711 	(void)thread;
712 
713 	return !SCHED(processor_queue_empty)(processor) || rt_runq_count(processor->processor_set) > 0;
714 }
715 
716 /* Default implementations of .steal_thread_enabled */
717 bool
sched_steal_thread_DISABLED(processor_set_t pset)718 sched_steal_thread_DISABLED(processor_set_t pset)
719 {
720 	(void)pset;
721 	return false;
722 }
723 
724 bool
sched_steal_thread_enabled(processor_set_t pset)725 sched_steal_thread_enabled(processor_set_t pset)
726 {
727 	return bit_count(pset->node->pset_map) > 1;
728 }
729 
730 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
731 
732 /*
733  * Set up values for timeshare
734  * loading factors.
735  */
736 static void
load_shift_init(void)737 load_shift_init(void)
738 {
739 	int8_t          k, *p = sched_load_shifts;
740 	uint32_t        i, j;
741 
742 	uint32_t        sched_decay_penalty = 1;
743 
744 	if (PE_parse_boot_argn("sched_decay_penalty", &sched_decay_penalty, sizeof(sched_decay_penalty))) {
745 		kprintf("Overriding scheduler decay penalty %u\n", sched_decay_penalty);
746 	}
747 
748 	if (PE_parse_boot_argn("sched_decay_usage_age_factor", &sched_decay_usage_age_factor, sizeof(sched_decay_usage_age_factor))) {
749 		kprintf("Overriding scheduler decay usage age factor %u\n", sched_decay_usage_age_factor);
750 	}
751 
752 	if (sched_decay_penalty == 0) {
753 		/*
754 		 * There is no penalty for timeshare threads for using too much
755 		 * CPU, so set all load shifts to INT8_MIN. Even under high load,
756 		 * sched_pri_shift will be >INT8_MAX, and there will be no
757 		 * penalty applied to threads (nor will sched_usage be updated per
758 		 * thread).
759 		 */
760 		for (i = 0; i < NRQS; i++) {
761 			sched_load_shifts[i] = INT8_MIN;
762 		}
763 
764 		return;
765 	}
766 
767 	*p++ = INT8_MIN; *p++ = 0;
768 
769 	/*
770 	 * For a given system load "i", the per-thread priority
771 	 * penalty per quantum of CPU usage is ~2^k priority
772 	 * levels. "sched_decay_penalty" can cause more
773 	 * array entries to be filled with smaller "k" values
774 	 */
775 	for (i = 2, j = 1 << sched_decay_penalty, k = 1; i < NRQS; ++k) {
776 		for (j <<= 1; (i < j) && (i < NRQS); ++i) {
777 			*p++ = k;
778 		}
779 	}
780 }
781 
782 static void
preempt_pri_init(void)783 preempt_pri_init(void)
784 {
785 	bitmap_t *p = sched_preempt_pri;
786 
787 	for (int i = BASEPRI_FOREGROUND; i < MINPRI_KERNEL; ++i) {
788 		bitmap_set(p, i);
789 	}
790 
791 	for (int i = BASEPRI_PREEMPT; i <= MAXPRI; ++i) {
792 		bitmap_set(p, i);
793 	}
794 }
795 
796 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
797 
798 void
check_monotonic_time(uint64_t ctime)799 check_monotonic_time(uint64_t ctime)
800 {
801 	processor_t processor = current_processor();
802 	uint64_t last_dispatch = processor->last_dispatch;
803 
804 	if (last_dispatch > ctime) {
805 		panic("Non-monotonic time: last_dispatch at 0x%llx, ctime 0x%llx",
806 		    last_dispatch, ctime);
807 	}
808 }
809 
810 
811 /*
812  *	Thread wait timer expiration.
813  *	Runs in timer interrupt context with interrupts disabled.
814  */
815 void
thread_timer_expire(void * p0,__unused void * p1)816 thread_timer_expire(void *p0, __unused void *p1)
817 {
818 	thread_t thread = (thread_t)p0;
819 
820 	assert_thread_magic(thread);
821 
822 	assert(ml_get_interrupts_enabled() == FALSE);
823 
824 	thread_lock(thread);
825 
826 	if (thread->wait_timer_armed) {
827 		thread->wait_timer_armed = false;
828 		clear_wait_internal(thread, THREAD_TIMED_OUT);
829 		/* clear_wait_internal may have dropped and retaken the thread lock */
830 	}
831 
832 	thread->wait_timer_active--;
833 
834 	thread_unlock(thread);
835 }
836 
837 /*
838  *	thread_unblock:
839  *
840  *	Unblock thread on wake up.
841  *
842  *	Returns TRUE if the thread should now be placed on the runqueue.
843  *
844  *	Thread must be locked.
845  *
846  *	Called at splsched().
847  */
848 boolean_t
thread_unblock(thread_t thread,wait_result_t wresult)849 thread_unblock(
850 	thread_t                thread,
851 	wait_result_t   wresult)
852 {
853 	boolean_t               ready_for_runq = FALSE;
854 	thread_t                cthread = current_thread();
855 	uint32_t                new_run_count;
856 	int                             old_thread_state;
857 
858 	/*
859 	 *	Set wait_result.
860 	 */
861 	thread->wait_result = wresult;
862 
863 	/*
864 	 *	Cancel pending wait timer.
865 	 */
866 	if (thread->wait_timer_armed) {
867 		if (timer_call_cancel(thread->wait_timer)) {
868 			thread->wait_timer_active--;
869 		}
870 		thread->wait_timer_armed = false;
871 	}
872 
873 	boolean_t aticontext, pidle;
874 	ml_get_power_state(&aticontext, &pidle);
875 
876 	/*
877 	 *	Update scheduling state: not waiting,
878 	 *	set running.
879 	 */
880 	old_thread_state = thread->state;
881 	thread->state = (old_thread_state | TH_RUN) &
882 	    ~(TH_WAIT | TH_UNINT | TH_WAIT_REPORT | TH_WAKING);
883 
884 	if ((old_thread_state & TH_RUN) == 0) {
885 		uint64_t ctime = mach_approximate_time();
886 
887 		check_monotonic_time(ctime);
888 
889 		thread->last_made_runnable_time = thread->last_basepri_change_time = ctime;
890 		timer_start(&thread->runnable_timer, ctime);
891 
892 		ready_for_runq = TRUE;
893 
894 		if (old_thread_state & TH_WAIT_REPORT) {
895 			(*thread->sched_call)(SCHED_CALL_UNBLOCK, thread);
896 		}
897 
898 		/* Update the runnable thread count */
899 		new_run_count = SCHED(run_count_incr)(thread);
900 
901 #if CONFIG_SCHED_AUTO_JOIN
902 		if (aticontext == FALSE && work_interval_should_propagate(cthread, thread)) {
903 			work_interval_auto_join_propagate(cthread, thread);
904 		}
905 #endif /*CONFIG_SCHED_AUTO_JOIN */
906 
907 	} else {
908 		/*
909 		 * Either the thread is idling in place on another processor,
910 		 * or it hasn't finished context switching yet.
911 		 */
912 		assert((thread->state & TH_IDLE) == 0);
913 		/*
914 		 * The run count is only dropped after the context switch completes
915 		 * and the thread is still waiting, so we should not run_incr here
916 		 */
917 		new_run_count = os_atomic_load(&sched_run_buckets[TH_BUCKET_RUN], relaxed);
918 	}
919 
920 	/*
921 	 * Calculate deadline for real-time threads.
922 	 */
923 	if (thread->sched_mode == TH_MODE_REALTIME) {
924 		uint64_t ctime = mach_absolute_time();
925 		thread->realtime.deadline = thread->realtime.constraint + ctime;
926 		KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SET_RT_DEADLINE) | DBG_FUNC_NONE,
927 		    (uintptr_t)thread_tid(thread), thread->realtime.deadline, thread->realtime.computation, 0);
928 	}
929 
930 	/*
931 	 * Clear old quantum, fail-safe computation, etc.
932 	 */
933 	thread->quantum_remaining = 0;
934 	thread->computation_metered = 0;
935 	thread->reason = AST_NONE;
936 	thread->block_hint = kThreadWaitNone;
937 
938 	/* Obtain power-relevant interrupt and "platform-idle exit" statistics.
939 	 * We also account for "double hop" thread signaling via
940 	 * the thread callout infrastructure.
941 	 * DRK: consider removing the callout wakeup counters in the future
942 	 * they're present for verification at the moment.
943 	 */
944 
945 	if (__improbable(aticontext && !(thread_get_tag_internal(thread) & THREAD_TAG_CALLOUT))) {
946 		DTRACE_SCHED2(iwakeup, struct thread *, thread, struct proc *, current_proc());
947 
948 		uint64_t ttd = current_processor()->timer_call_ttd;
949 
950 		if (ttd) {
951 			if (ttd <= timer_deadline_tracking_bin_1) {
952 				thread->thread_timer_wakeups_bin_1++;
953 			} else if (ttd <= timer_deadline_tracking_bin_2) {
954 				thread->thread_timer_wakeups_bin_2++;
955 			}
956 		}
957 
958 		ledger_credit_thread(thread, thread->t_ledger,
959 		    task_ledgers.interrupt_wakeups, 1);
960 		if (pidle) {
961 			ledger_credit_thread(thread, thread->t_ledger,
962 			    task_ledgers.platform_idle_wakeups, 1);
963 		}
964 	} else if (thread_get_tag_internal(cthread) & THREAD_TAG_CALLOUT) {
965 		/* TODO: what about an interrupt that does a wake taken on a callout thread? */
966 		if (cthread->callout_woken_from_icontext) {
967 			ledger_credit_thread(thread, thread->t_ledger,
968 			    task_ledgers.interrupt_wakeups, 1);
969 			thread->thread_callout_interrupt_wakeups++;
970 
971 			if (cthread->callout_woken_from_platform_idle) {
972 				ledger_credit_thread(thread, thread->t_ledger,
973 				    task_ledgers.platform_idle_wakeups, 1);
974 				thread->thread_callout_platform_idle_wakeups++;
975 			}
976 
977 			cthread->callout_woke_thread = TRUE;
978 		}
979 	}
980 
981 	if (thread_get_tag_internal(thread) & THREAD_TAG_CALLOUT) {
982 		thread->callout_woken_from_icontext = !!aticontext;
983 		thread->callout_woken_from_platform_idle = !!pidle;
984 		thread->callout_woke_thread = FALSE;
985 	}
986 
987 #if KPERF
988 	if (ready_for_runq) {
989 		kperf_make_runnable(thread, aticontext);
990 	}
991 #endif /* KPERF */
992 
993 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
994 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_MAKE_RUNNABLE) | DBG_FUNC_NONE,
995 	    (uintptr_t)thread_tid(thread), thread->sched_pri, thread->wait_result,
996 	    sched_run_buckets[TH_BUCKET_RUN], 0);
997 
998 	DTRACE_SCHED2(wakeup, struct thread *, thread, struct proc *, current_proc());
999 
1000 	return ready_for_runq;
1001 }
1002 
1003 /*
1004  *	Routine:	thread_allowed_for_handoff
1005  *	Purpose:
1006  *		Check if the thread is allowed for handoff operation
1007  *	Conditions:
1008  *		thread lock held, IPC locks may be held.
1009  *	TODO: In future, do not allow handoff if threads have different cluster
1010  *	recommendations.
1011  */
1012 boolean_t
thread_allowed_for_handoff(thread_t thread)1013 thread_allowed_for_handoff(
1014 	thread_t         thread)
1015 {
1016 	thread_t self = current_thread();
1017 
1018 	if (allow_direct_handoff &&
1019 	    thread->sched_mode == TH_MODE_REALTIME &&
1020 	    self->sched_mode == TH_MODE_REALTIME) {
1021 		return TRUE;
1022 	}
1023 
1024 	return FALSE;
1025 }
1026 
1027 /*
1028  *	Routine:	thread_go
1029  *	Purpose:
1030  *		Unblock and dispatch thread.
1031  *	Conditions:
1032  *		thread lock held, IPC locks may be held.
1033  *		thread must have been waiting
1034  */
1035 void
thread_go(thread_t thread,wait_result_t wresult,bool try_handoff)1036 thread_go(
1037 	thread_t                thread,
1038 	wait_result_t           wresult,
1039 	bool                    try_handoff)
1040 {
1041 	thread_t self = current_thread();
1042 
1043 	assert_thread_magic(thread);
1044 
1045 	assert(thread->at_safe_point == FALSE);
1046 	assert(thread->wait_event == NO_EVENT64);
1047 	assert(waitq_is_null(thread->waitq));
1048 
1049 	assert(!(thread->state & (TH_TERMINATE | TH_TERMINATE2)));
1050 	assert(thread->state & TH_WAIT);
1051 
1052 	if (thread->started) {
1053 		assert(thread->state & TH_WAKING);
1054 	}
1055 
1056 	thread_lock_assert(thread, LCK_ASSERT_OWNED);
1057 
1058 	assert(ml_get_interrupts_enabled() == false);
1059 
1060 	if (thread_unblock(thread, wresult)) {
1061 #if SCHED_TRACE_THREAD_WAKEUPS
1062 		backtrace(&thread->thread_wakeup_bt[0],
1063 		    (sizeof(thread->thread_wakeup_bt) / sizeof(uintptr_t)), NULL,
1064 		    NULL);
1065 #endif /* SCHED_TRACE_THREAD_WAKEUPS */
1066 		if (try_handoff && thread_allowed_for_handoff(thread)) {
1067 			thread_reference(thread);
1068 			assert(self->handoff_thread == NULL);
1069 			self->handoff_thread = thread;
1070 		} else {
1071 			thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
1072 		}
1073 	}
1074 }
1075 
1076 /*
1077  *	Routine:	thread_mark_wait_locked
1078  *	Purpose:
1079  *		Mark a thread as waiting.  If, given the circumstances,
1080  *		it doesn't want to wait (i.e. already aborted), then
1081  *		indicate that in the return value.
1082  *	Conditions:
1083  *		at splsched() and thread is locked.
1084  */
1085 __private_extern__
1086 wait_result_t
thread_mark_wait_locked(thread_t thread,wait_interrupt_t interruptible_orig)1087 thread_mark_wait_locked(
1088 	thread_t                        thread,
1089 	wait_interrupt_t        interruptible_orig)
1090 {
1091 	boolean_t                       at_safe_point;
1092 	wait_interrupt_t        interruptible = interruptible_orig;
1093 
1094 	if (thread->state & TH_IDLE) {
1095 		panic("Invalid attempt to wait while running the idle thread");
1096 	}
1097 
1098 	assert(!(thread->state & (TH_WAIT | TH_WAKING | TH_IDLE | TH_UNINT | TH_TERMINATE2 | TH_WAIT_REPORT)));
1099 
1100 	/*
1101 	 *	The thread may have certain types of interrupts/aborts masked
1102 	 *	off.  Even if the wait location says these types of interrupts
1103 	 *	are OK, we have to honor mask settings (outer-scoped code may
1104 	 *	not be able to handle aborts at the moment).
1105 	 */
1106 	interruptible &= TH_OPT_INTMASK;
1107 	if (interruptible > (thread->options & TH_OPT_INTMASK)) {
1108 		interruptible = thread->options & TH_OPT_INTMASK;
1109 	}
1110 
1111 	at_safe_point = (interruptible == THREAD_ABORTSAFE);
1112 
1113 	if (interruptible == THREAD_UNINT ||
1114 	    !(thread->sched_flags & TH_SFLAG_ABORT) ||
1115 	    (!at_safe_point &&
1116 	    (thread->sched_flags & TH_SFLAG_ABORTSAFELY))) {
1117 		if (!(thread->state & TH_TERMINATE)) {
1118 			DTRACE_SCHED(sleep);
1119 		}
1120 
1121 		int state_bits = TH_WAIT;
1122 		if (!interruptible) {
1123 			state_bits |= TH_UNINT;
1124 		}
1125 		if (thread->sched_call) {
1126 			wait_interrupt_t mask = THREAD_WAIT_NOREPORT_USER;
1127 			if (is_kerneltask(get_threadtask(thread))) {
1128 				mask = THREAD_WAIT_NOREPORT_KERNEL;
1129 			}
1130 			if ((interruptible_orig & mask) == 0) {
1131 				state_bits |= TH_WAIT_REPORT;
1132 			}
1133 		}
1134 		thread->state |= state_bits;
1135 		thread->at_safe_point = at_safe_point;
1136 
1137 		/* TODO: pass this through assert_wait instead, have
1138 		 * assert_wait just take a struct as an argument */
1139 		assert(!thread->block_hint);
1140 		thread->block_hint = thread->pending_block_hint;
1141 		thread->pending_block_hint = kThreadWaitNone;
1142 
1143 		return thread->wait_result = THREAD_WAITING;
1144 	} else {
1145 		if (thread->sched_flags & TH_SFLAG_ABORTSAFELY) {
1146 			thread->sched_flags &= ~TH_SFLAG_ABORTED_MASK;
1147 		}
1148 	}
1149 	thread->pending_block_hint = kThreadWaitNone;
1150 
1151 	return thread->wait_result = THREAD_INTERRUPTED;
1152 }
1153 
1154 /*
1155  *	Routine:	thread_interrupt_level
1156  *	Purpose:
1157  *	        Set the maximum interruptible state for the
1158  *		current thread.  The effective value of any
1159  *		interruptible flag passed into assert_wait
1160  *		will never exceed this.
1161  *
1162  *		Useful for code that must not be interrupted,
1163  *		but which calls code that doesn't know that.
1164  *	Returns:
1165  *		The old interrupt level for the thread.
1166  */
1167 __private_extern__
1168 wait_interrupt_t
thread_interrupt_level(wait_interrupt_t new_level)1169 thread_interrupt_level(
1170 	wait_interrupt_t new_level)
1171 {
1172 	thread_t thread = current_thread();
1173 	wait_interrupt_t result = thread->options & TH_OPT_INTMASK;
1174 
1175 	thread->options = (thread->options & ~TH_OPT_INTMASK) | (new_level & TH_OPT_INTMASK);
1176 
1177 	return result;
1178 }
1179 
1180 /*
1181  *	assert_wait:
1182  *
1183  *	Assert that the current thread is about to go to
1184  *	sleep until the specified event occurs.
1185  */
1186 wait_result_t
assert_wait(event_t event,wait_interrupt_t interruptible)1187 assert_wait(
1188 	event_t                         event,
1189 	wait_interrupt_t        interruptible)
1190 {
1191 	if (__improbable(event == NO_EVENT)) {
1192 		panic("%s() called with NO_EVENT", __func__);
1193 	}
1194 
1195 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1196 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1197 	    VM_KERNEL_UNSLIDE_OR_PERM(event), 0, 0, 0, 0);
1198 
1199 	struct waitq *waitq;
1200 	waitq = global_eventq(event);
1201 	return waitq_assert_wait64(waitq, CAST_EVENT64_T(event), interruptible, TIMEOUT_WAIT_FOREVER);
1202 }
1203 
1204 /*
1205  *	assert_wait_queue:
1206  *
1207  *	Return the global waitq for the specified event
1208  */
1209 struct waitq *
assert_wait_queue(event_t event)1210 assert_wait_queue(
1211 	event_t                         event)
1212 {
1213 	return global_eventq(event);
1214 }
1215 
1216 wait_result_t
assert_wait_timeout(event_t event,wait_interrupt_t interruptible,uint32_t interval,uint32_t scale_factor)1217 assert_wait_timeout(
1218 	event_t                         event,
1219 	wait_interrupt_t        interruptible,
1220 	uint32_t                        interval,
1221 	uint32_t                        scale_factor)
1222 {
1223 	thread_t                        thread = current_thread();
1224 	wait_result_t           wresult;
1225 	uint64_t                        deadline;
1226 	spl_t                           s;
1227 
1228 	if (__improbable(event == NO_EVENT)) {
1229 		panic("%s() called with NO_EVENT", __func__);
1230 	}
1231 
1232 	struct waitq *waitq;
1233 	waitq = global_eventq(event);
1234 
1235 	s = splsched();
1236 	waitq_lock(waitq);
1237 
1238 	clock_interval_to_deadline(interval, scale_factor, &deadline);
1239 
1240 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1241 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1242 	    VM_KERNEL_UNSLIDE_OR_PERM(event), interruptible, deadline, 0, 0);
1243 
1244 	wresult = waitq_assert_wait64_locked(waitq, CAST_EVENT64_T(event),
1245 	    interruptible,
1246 	    TIMEOUT_URGENCY_SYS_NORMAL,
1247 	    deadline, TIMEOUT_NO_LEEWAY,
1248 	    thread);
1249 
1250 	waitq_unlock(waitq);
1251 	splx(s);
1252 	return wresult;
1253 }
1254 
1255 wait_result_t
assert_wait_timeout_with_leeway(event_t event,wait_interrupt_t interruptible,wait_timeout_urgency_t urgency,uint32_t interval,uint32_t leeway,uint32_t scale_factor)1256 assert_wait_timeout_with_leeway(
1257 	event_t                         event,
1258 	wait_interrupt_t        interruptible,
1259 	wait_timeout_urgency_t  urgency,
1260 	uint32_t                        interval,
1261 	uint32_t                        leeway,
1262 	uint32_t                        scale_factor)
1263 {
1264 	thread_t                        thread = current_thread();
1265 	wait_result_t           wresult;
1266 	uint64_t                        deadline;
1267 	uint64_t                        abstime;
1268 	uint64_t                        slop;
1269 	uint64_t                        now;
1270 	spl_t                           s;
1271 
1272 	if (__improbable(event == NO_EVENT)) {
1273 		panic("%s() called with NO_EVENT", __func__);
1274 	}
1275 
1276 	now = mach_absolute_time();
1277 	clock_interval_to_absolutetime_interval(interval, scale_factor, &abstime);
1278 	deadline = now + abstime;
1279 
1280 	clock_interval_to_absolutetime_interval(leeway, scale_factor, &slop);
1281 
1282 	struct waitq *waitq;
1283 	waitq = global_eventq(event);
1284 
1285 	s = splsched();
1286 	waitq_lock(waitq);
1287 
1288 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1289 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1290 	    VM_KERNEL_UNSLIDE_OR_PERM(event), interruptible, deadline, 0, 0);
1291 
1292 	wresult = waitq_assert_wait64_locked(waitq, CAST_EVENT64_T(event),
1293 	    interruptible,
1294 	    urgency, deadline, slop,
1295 	    thread);
1296 
1297 	waitq_unlock(waitq);
1298 	splx(s);
1299 	return wresult;
1300 }
1301 
1302 wait_result_t
assert_wait_deadline(event_t event,wait_interrupt_t interruptible,uint64_t deadline)1303 assert_wait_deadline(
1304 	event_t                         event,
1305 	wait_interrupt_t        interruptible,
1306 	uint64_t                        deadline)
1307 {
1308 	thread_t                        thread = current_thread();
1309 	wait_result_t           wresult;
1310 	spl_t                           s;
1311 
1312 	if (__improbable(event == NO_EVENT)) {
1313 		panic("%s() called with NO_EVENT", __func__);
1314 	}
1315 
1316 	struct waitq *waitq;
1317 	waitq = global_eventq(event);
1318 
1319 	s = splsched();
1320 	waitq_lock(waitq);
1321 
1322 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1323 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1324 	    VM_KERNEL_UNSLIDE_OR_PERM(event), interruptible, deadline, 0, 0);
1325 
1326 	wresult = waitq_assert_wait64_locked(waitq, CAST_EVENT64_T(event),
1327 	    interruptible,
1328 	    TIMEOUT_URGENCY_SYS_NORMAL, deadline,
1329 	    TIMEOUT_NO_LEEWAY, thread);
1330 	waitq_unlock(waitq);
1331 	splx(s);
1332 	return wresult;
1333 }
1334 
1335 wait_result_t
assert_wait_deadline_with_leeway(event_t event,wait_interrupt_t interruptible,wait_timeout_urgency_t urgency,uint64_t deadline,uint64_t leeway)1336 assert_wait_deadline_with_leeway(
1337 	event_t                         event,
1338 	wait_interrupt_t        interruptible,
1339 	wait_timeout_urgency_t  urgency,
1340 	uint64_t                        deadline,
1341 	uint64_t                        leeway)
1342 {
1343 	thread_t                        thread = current_thread();
1344 	wait_result_t           wresult;
1345 	spl_t                           s;
1346 
1347 	if (__improbable(event == NO_EVENT)) {
1348 		panic("%s() called with NO_EVENT", __func__);
1349 	}
1350 
1351 	struct waitq *waitq;
1352 	waitq = global_eventq(event);
1353 
1354 	s = splsched();
1355 	waitq_lock(waitq);
1356 
1357 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1358 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1359 	    VM_KERNEL_UNSLIDE_OR_PERM(event), interruptible, deadline, 0, 0);
1360 
1361 	wresult = waitq_assert_wait64_locked(waitq, CAST_EVENT64_T(event),
1362 	    interruptible,
1363 	    urgency, deadline, leeway,
1364 	    thread);
1365 	waitq_unlock(waitq);
1366 	splx(s);
1367 	return wresult;
1368 }
1369 
1370 void
sched_cond_init(sched_cond_atomic_t * cond)1371 sched_cond_init(
1372 	sched_cond_atomic_t *cond)
1373 {
1374 	os_atomic_init(cond, SCHED_COND_INIT);
1375 }
1376 
1377 wait_result_t
sched_cond_wait_parameter(sched_cond_atomic_t * cond,wait_interrupt_t interruptible,thread_continue_t continuation,void * parameter)1378 sched_cond_wait_parameter(
1379 	sched_cond_atomic_t *cond,
1380 	wait_interrupt_t interruptible,
1381 	thread_continue_t continuation,
1382 	void *parameter)
1383 {
1384 	assert_wait((event_t) cond, interruptible);
1385 	/* clear active bit to indicate future wakeups will have to unblock this thread */
1386 	sched_cond_t new_state = (sched_cond_t) os_atomic_andnot(cond, SCHED_COND_ACTIVE, relaxed);
1387 	if (__improbable(new_state & SCHED_COND_WAKEUP)) {
1388 		/* a wakeup has been issued; undo wait assertion, ack the wakeup, and return */
1389 		thread_t thread = current_thread();
1390 		clear_wait(thread, THREAD_AWAKENED);
1391 		sched_cond_ack(cond);
1392 		return THREAD_AWAKENED;
1393 	}
1394 	return thread_block_parameter(continuation, parameter);
1395 }
1396 
1397 wait_result_t
sched_cond_wait(sched_cond_atomic_t * cond,wait_interrupt_t interruptible,thread_continue_t continuation)1398 sched_cond_wait(
1399 	sched_cond_atomic_t *cond,
1400 	wait_interrupt_t interruptible,
1401 	thread_continue_t continuation)
1402 {
1403 	return sched_cond_wait_parameter(cond, interruptible, continuation, NULL);
1404 }
1405 
1406 sched_cond_t
sched_cond_ack(sched_cond_atomic_t * cond)1407 sched_cond_ack(
1408 	sched_cond_atomic_t *cond)
1409 {
1410 	sched_cond_t new_cond = (sched_cond_t) os_atomic_xor(cond, SCHED_COND_ACTIVE | SCHED_COND_WAKEUP, acquire);
1411 	assert(new_cond & SCHED_COND_ACTIVE);
1412 	return new_cond;
1413 }
1414 
1415 kern_return_t
sched_cond_signal(sched_cond_atomic_t * cond,thread_t thread)1416 sched_cond_signal(
1417 	sched_cond_atomic_t  *cond,
1418 	thread_t thread)
1419 {
1420 	disable_preemption();
1421 	sched_cond_t old_cond = (sched_cond_t) os_atomic_or_orig(cond, SCHED_COND_WAKEUP, release);
1422 	if (!(old_cond & (SCHED_COND_WAKEUP | SCHED_COND_ACTIVE))) {
1423 		/* this was the first wakeup to be issued AND the thread was inactive */
1424 		thread_wakeup_thread((event_t) cond, thread);
1425 	}
1426 	enable_preemption();
1427 	return KERN_SUCCESS;
1428 }
1429 
1430 /*
1431  * thread_isoncpu:
1432  *
1433  * Return TRUE if a thread is running on a processor such that an AST
1434  * is needed to pull it out of userspace execution, or if executing in
1435  * the kernel, bring to a context switch boundary that would cause
1436  * thread state to be serialized in the thread PCB.
1437  *
1438  * Thread locked, returns the same way. While locked, fields
1439  * like "state" cannot change. "runq" can change only from set to unset.
1440  */
1441 static inline boolean_t
thread_isoncpu(thread_t thread)1442 thread_isoncpu(thread_t thread)
1443 {
1444 	/* Not running or runnable */
1445 	if (!(thread->state & TH_RUN)) {
1446 		return FALSE;
1447 	}
1448 
1449 	/* Waiting on a runqueue, not currently running */
1450 	/* TODO: This is invalid - it can get dequeued without thread lock, but not context switched. */
1451 	if (thread->runq != PROCESSOR_NULL) {
1452 		return FALSE;
1453 	}
1454 
1455 	/*
1456 	 * Thread does not have a stack yet
1457 	 * It could be on the stack alloc queue or preparing to be invoked
1458 	 */
1459 	if (!thread->kernel_stack) {
1460 		return FALSE;
1461 	}
1462 
1463 	/*
1464 	 * Thread must be running on a processor, or
1465 	 * about to run, or just did run. In all these
1466 	 * cases, an AST to the processor is needed
1467 	 * to guarantee that the thread is kicked out
1468 	 * of userspace and the processor has
1469 	 * context switched (and saved register state).
1470 	 */
1471 	return TRUE;
1472 }
1473 
1474 /*
1475  * thread_stop:
1476  *
1477  * Force a preemption point for a thread and wait
1478  * for it to stop running on a CPU. If a stronger
1479  * guarantee is requested, wait until no longer
1480  * runnable. Arbitrates access among
1481  * multiple stop requests. (released by unstop)
1482  *
1483  * The thread must enter a wait state and stop via a
1484  * separate means.
1485  *
1486  * Returns FALSE if interrupted.
1487  */
1488 boolean_t
thread_stop(thread_t thread,boolean_t until_not_runnable)1489 thread_stop(
1490 	thread_t                thread,
1491 	boolean_t       until_not_runnable)
1492 {
1493 	wait_result_t   wresult;
1494 	spl_t                   s = splsched();
1495 	boolean_t               oncpu;
1496 
1497 	wake_lock(thread);
1498 	thread_lock(thread);
1499 
1500 	while (thread->state & TH_SUSP) {
1501 		thread->wake_active = TRUE;
1502 		thread_unlock(thread);
1503 
1504 		wresult = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
1505 		wake_unlock(thread);
1506 		splx(s);
1507 
1508 		if (wresult == THREAD_WAITING) {
1509 			wresult = thread_block(THREAD_CONTINUE_NULL);
1510 		}
1511 
1512 		if (wresult != THREAD_AWAKENED) {
1513 			return FALSE;
1514 		}
1515 
1516 		s = splsched();
1517 		wake_lock(thread);
1518 		thread_lock(thread);
1519 	}
1520 
1521 	thread->state |= TH_SUSP;
1522 
1523 	while ((oncpu = thread_isoncpu(thread)) ||
1524 	    (until_not_runnable && (thread->state & TH_RUN))) {
1525 		processor_t             processor;
1526 
1527 		if (oncpu) {
1528 			assert(thread->state & TH_RUN);
1529 			processor = thread->chosen_processor;
1530 			cause_ast_check(processor);
1531 		}
1532 
1533 		thread->wake_active = TRUE;
1534 		thread_unlock(thread);
1535 
1536 		wresult = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
1537 		wake_unlock(thread);
1538 		splx(s);
1539 
1540 		if (wresult == THREAD_WAITING) {
1541 			wresult = thread_block(THREAD_CONTINUE_NULL);
1542 		}
1543 
1544 		if (wresult != THREAD_AWAKENED) {
1545 			thread_unstop(thread);
1546 			return FALSE;
1547 		}
1548 
1549 		s = splsched();
1550 		wake_lock(thread);
1551 		thread_lock(thread);
1552 	}
1553 
1554 	thread_unlock(thread);
1555 	wake_unlock(thread);
1556 	splx(s);
1557 
1558 	/*
1559 	 * We return with the thread unlocked. To prevent it from
1560 	 * transitioning to a runnable state (or from TH_RUN to
1561 	 * being on the CPU), the caller must ensure the thread
1562 	 * is stopped via an external means (such as an AST)
1563 	 */
1564 
1565 	return TRUE;
1566 }
1567 
1568 /*
1569  * thread_unstop:
1570  *
1571  * Release a previous stop request and set
1572  * the thread running if appropriate.
1573  *
1574  * Use only after a successful stop operation.
1575  */
1576 void
thread_unstop(thread_t thread)1577 thread_unstop(
1578 	thread_t        thread)
1579 {
1580 	spl_t           s = splsched();
1581 
1582 	wake_lock(thread);
1583 	thread_lock(thread);
1584 
1585 	assert((thread->state & (TH_RUN | TH_WAIT | TH_SUSP)) != TH_SUSP);
1586 
1587 	if (thread->state & TH_SUSP) {
1588 		thread->state &= ~TH_SUSP;
1589 
1590 		if (thread->wake_active) {
1591 			thread->wake_active = FALSE;
1592 			thread_unlock(thread);
1593 
1594 			thread_wakeup(&thread->wake_active);
1595 			wake_unlock(thread);
1596 			splx(s);
1597 
1598 			return;
1599 		}
1600 	}
1601 
1602 	thread_unlock(thread);
1603 	wake_unlock(thread);
1604 	splx(s);
1605 }
1606 
1607 /*
1608  * thread_wait:
1609  *
1610  * Wait for a thread to stop running. (non-interruptible)
1611  *
1612  */
1613 void
thread_wait(thread_t thread,boolean_t until_not_runnable)1614 thread_wait(
1615 	thread_t        thread,
1616 	boolean_t       until_not_runnable)
1617 {
1618 	wait_result_t   wresult;
1619 	boolean_t       oncpu;
1620 	processor_t     processor;
1621 	spl_t           s = splsched();
1622 
1623 	wake_lock(thread);
1624 	thread_lock(thread);
1625 
1626 	/*
1627 	 * Wait until not running on a CPU.  If stronger requirement
1628 	 * desired, wait until not runnable.  Assumption: if thread is
1629 	 * on CPU, then TH_RUN is set, so we're not waiting in any case
1630 	 * where the original, pure "TH_RUN" check would have let us
1631 	 * finish.
1632 	 */
1633 	while ((oncpu = thread_isoncpu(thread)) ||
1634 	    (until_not_runnable && (thread->state & TH_RUN))) {
1635 		if (oncpu) {
1636 			assert(thread->state & TH_RUN);
1637 			processor = thread->chosen_processor;
1638 			cause_ast_check(processor);
1639 		}
1640 
1641 		thread->wake_active = TRUE;
1642 		thread_unlock(thread);
1643 
1644 		wresult = assert_wait(&thread->wake_active, THREAD_UNINT);
1645 		wake_unlock(thread);
1646 		splx(s);
1647 
1648 		if (wresult == THREAD_WAITING) {
1649 			thread_block(THREAD_CONTINUE_NULL);
1650 		}
1651 
1652 		s = splsched();
1653 		wake_lock(thread);
1654 		thread_lock(thread);
1655 	}
1656 
1657 	thread_unlock(thread);
1658 	wake_unlock(thread);
1659 	splx(s);
1660 }
1661 
1662 /*
1663  *	Routine: clear_wait_internal
1664  *
1665  *		Clear the wait condition for the specified thread.
1666  *		Start the thread executing if that is appropriate.
1667  *	Arguments:
1668  *		thread		thread to awaken
1669  *		result		Wakeup result the thread should see
1670  *	Conditions:
1671  *		At splsched
1672  *		the thread is locked.
1673  *	Returns:
1674  *		KERN_SUCCESS		thread was rousted out a wait
1675  *		KERN_FAILURE		thread was waiting but could not be rousted
1676  *		KERN_NOT_WAITING	thread was not waiting
1677  */
1678 __private_extern__ kern_return_t
clear_wait_internal(thread_t thread,wait_result_t wresult)1679 clear_wait_internal(
1680 	thread_t        thread,
1681 	wait_result_t   wresult)
1682 {
1683 	waitq_t waitq = thread->waitq;
1684 
1685 	if (wresult == THREAD_INTERRUPTED && (thread->state & TH_UNINT)) {
1686 		return KERN_FAILURE;
1687 	}
1688 
1689 	/*
1690 	 * Check that the thread is waiting and not waking, as a waking thread
1691 	 * has already cleared its waitq, and is destined to be go'ed, don't
1692 	 * need to do it again.
1693 	 */
1694 	if ((thread->state & (TH_WAIT | TH_TERMINATE | TH_WAKING)) != TH_WAIT) {
1695 		assert(waitq_is_null(thread->waitq));
1696 		return KERN_NOT_WAITING;
1697 	}
1698 
1699 	/* may drop and retake the thread lock */
1700 	if (!waitq_is_null(waitq) && !waitq_pull_thread_locked(waitq, thread)) {
1701 		return KERN_NOT_WAITING;
1702 	}
1703 
1704 	thread_go(thread, wresult, /* handoff */ false);
1705 
1706 	return KERN_SUCCESS;
1707 }
1708 
1709 
1710 /*
1711  *	clear_wait:
1712  *
1713  *	Clear the wait condition for the specified thread.  Start the thread
1714  *	executing if that is appropriate.
1715  *
1716  *	parameters:
1717  *	  thread		thread to awaken
1718  *	  result		Wakeup result the thread should see
1719  */
1720 kern_return_t
clear_wait(thread_t thread,wait_result_t result)1721 clear_wait(
1722 	thread_t                thread,
1723 	wait_result_t   result)
1724 {
1725 	kern_return_t ret;
1726 	spl_t           s;
1727 
1728 	s = splsched();
1729 	thread_lock(thread);
1730 
1731 	ret = clear_wait_internal(thread, result);
1732 
1733 	if (thread == current_thread()) {
1734 		/*
1735 		 * The thread must be ready to wait again immediately
1736 		 * after clearing its own wait.
1737 		 */
1738 		assert((thread->state & TH_WAKING) == 0);
1739 	}
1740 
1741 	thread_unlock(thread);
1742 	splx(s);
1743 	return ret;
1744 }
1745 
1746 
1747 /*
1748  *	thread_wakeup_prim:
1749  *
1750  *	Common routine for thread_wakeup, thread_wakeup_with_result,
1751  *	and thread_wakeup_one.
1752  *
1753  */
1754 kern_return_t
thread_wakeup_prim(event_t event,boolean_t one_thread,wait_result_t result)1755 thread_wakeup_prim(
1756 	event_t          event,
1757 	boolean_t        one_thread,
1758 	wait_result_t    result)
1759 {
1760 	if (__improbable(event == NO_EVENT)) {
1761 		panic("%s() called with NO_EVENT", __func__);
1762 	}
1763 
1764 	struct waitq *wq = global_eventq(event);
1765 
1766 	if (one_thread) {
1767 		return waitq_wakeup64_one(wq, CAST_EVENT64_T(event), result, WAITQ_WAKEUP_DEFAULT);
1768 	} else {
1769 		return waitq_wakeup64_all(wq, CAST_EVENT64_T(event), result, WAITQ_WAKEUP_DEFAULT);
1770 	}
1771 }
1772 
1773 /*
1774  * Wakeup a specified thread if and only if it's waiting for this event
1775  */
1776 kern_return_t
thread_wakeup_thread(event_t event,thread_t thread)1777 thread_wakeup_thread(
1778 	event_t         event,
1779 	thread_t        thread)
1780 {
1781 	if (__improbable(event == NO_EVENT)) {
1782 		panic("%s() called with NO_EVENT", __func__);
1783 	}
1784 
1785 	if (__improbable(thread == THREAD_NULL)) {
1786 		panic("%s() called with THREAD_NULL", __func__);
1787 	}
1788 
1789 	struct waitq *wq = global_eventq(event);
1790 
1791 	return waitq_wakeup64_thread(wq, CAST_EVENT64_T(event), thread, THREAD_AWAKENED);
1792 }
1793 
1794 /*
1795  * Wakeup a thread waiting on an event and promote it to a priority.
1796  *
1797  * Requires woken thread to un-promote itself when done.
1798  */
1799 kern_return_t
thread_wakeup_one_with_pri(event_t event,int priority)1800 thread_wakeup_one_with_pri(
1801 	event_t      event,
1802 	int          priority)
1803 {
1804 	if (__improbable(event == NO_EVENT)) {
1805 		panic("%s() called with NO_EVENT", __func__);
1806 	}
1807 
1808 	struct waitq *wq = global_eventq(event);
1809 
1810 	return waitq_wakeup64_one(wq, CAST_EVENT64_T(event), THREAD_AWAKENED, priority);
1811 }
1812 
1813 /*
1814  * Wakeup a thread waiting on an event,
1815  * promote it to a priority,
1816  * and return a reference to the woken thread.
1817  *
1818  * Requires woken thread to un-promote itself when done.
1819  */
1820 thread_t
thread_wakeup_identify(event_t event,int priority)1821 thread_wakeup_identify(event_t  event,
1822     int      priority)
1823 {
1824 	if (__improbable(event == NO_EVENT)) {
1825 		panic("%s() called with NO_EVENT", __func__);
1826 	}
1827 
1828 	struct waitq *wq = global_eventq(event);
1829 
1830 	return waitq_wakeup64_identify(wq, CAST_EVENT64_T(event), THREAD_AWAKENED, priority);
1831 }
1832 
1833 /*
1834  *	thread_bind:
1835  *
1836  *	Force the current thread to execute on the specified processor.
1837  *	Takes effect after the next thread_block().
1838  *
1839  *	Returns the previous binding.  PROCESSOR_NULL means
1840  *	not bound.
1841  *
1842  *	XXX - DO NOT export this to users - XXX
1843  */
1844 processor_t
thread_bind(processor_t processor)1845 thread_bind(
1846 	processor_t             processor)
1847 {
1848 	thread_t                self = current_thread();
1849 	processor_t             prev;
1850 	spl_t                   s;
1851 
1852 	s = splsched();
1853 	thread_lock(self);
1854 
1855 	prev = thread_bind_internal(self, processor);
1856 
1857 	thread_unlock(self);
1858 	splx(s);
1859 
1860 	return prev;
1861 }
1862 
1863 void
thread_bind_during_wakeup(thread_t thread,processor_t processor)1864 thread_bind_during_wakeup(thread_t thread, processor_t processor)
1865 {
1866 	assert(!ml_get_interrupts_enabled());
1867 	assert((thread->state & (TH_WAIT | TH_WAKING)) == (TH_WAIT | TH_WAKING));
1868 #if MACH_ASSERT
1869 	thread_lock_assert(thread, LCK_ASSERT_OWNED);
1870 #endif
1871 
1872 	if (thread->bound_processor != processor) {
1873 		thread_bind_internal(thread, processor);
1874 	}
1875 }
1876 
1877 void
thread_unbind_after_queue_shutdown(thread_t thread,processor_t processor __assert_only)1878 thread_unbind_after_queue_shutdown(
1879 	thread_t                thread,
1880 	processor_t             processor __assert_only)
1881 {
1882 	assert(!ml_get_interrupts_enabled());
1883 
1884 	thread_lock(thread);
1885 
1886 	if (thread->bound_processor) {
1887 		bool removed;
1888 
1889 		assert(thread->bound_processor == processor);
1890 
1891 		removed = thread_run_queue_remove(thread);
1892 		/*
1893 		 * we can always unbind even if we didn't really remove the
1894 		 * thread from the runqueue
1895 		 */
1896 		thread_bind_internal(thread, PROCESSOR_NULL);
1897 		if (removed) {
1898 			thread_run_queue_reinsert(thread, SCHED_TAILQ);
1899 		}
1900 	}
1901 
1902 	thread_unlock(thread);
1903 }
1904 
1905 /*
1906  * thread_bind_internal:
1907  *
1908  * If the specified thread is not the current thread, and it is currently
1909  * running on another CPU, a remote AST must be sent to that CPU to cause
1910  * the thread to migrate to its bound processor. Otherwise, the migration
1911  * will occur at the next quantum expiration or blocking point.
1912  *
1913  * When the thread is the current thread, and explicit thread_block() should
1914  * be used to force the current processor to context switch away and
1915  * let the thread migrate to the bound processor.
1916  *
1917  * Thread must be locked, and at splsched.
1918  */
1919 
1920 static processor_t
thread_bind_internal(thread_t thread,processor_t processor)1921 thread_bind_internal(
1922 	thread_t                thread,
1923 	processor_t             processor)
1924 {
1925 	processor_t             prev;
1926 
1927 	/* <rdar://problem/15102234> */
1928 	assert(thread->sched_pri < BASEPRI_RTQUEUES);
1929 	/* A thread can't be bound if it's sitting on a (potentially incorrect) runqueue */
1930 	assert(thread->runq == PROCESSOR_NULL);
1931 
1932 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_THREAD_BIND),
1933 	    thread_tid(thread), processor ? processor->cpu_id : ~0ul, 0, 0, 0);
1934 
1935 	prev = thread->bound_processor;
1936 	thread->bound_processor = processor;
1937 
1938 	return prev;
1939 }
1940 
1941 /*
1942  * thread_vm_bind_group_add:
1943  *
1944  * The "VM bind group" is a special mechanism to mark a collection
1945  * of threads from the VM subsystem that, in general, should be scheduled
1946  * with only one CPU of parallelism. To accomplish this, we initially
1947  * bind all the threads to the master processor, which has the effect
1948  * that only one of the threads in the group can execute at once, including
1949  * preempting threads in the group that are a lower priority. Future
1950  * mechanisms may use more dynamic mechanisms to prevent the collection
1951  * of VM threads from using more CPU time than desired.
1952  *
1953  * The current implementation can result in priority inversions where
1954  * compute-bound priority 95 or realtime threads that happen to have
1955  * landed on the master processor prevent the VM threads from running.
1956  * When this situation is detected, we unbind the threads for one
1957  * scheduler tick to allow the scheduler to run the threads an
1958  * additional CPUs, before restoring the binding (assuming high latency
1959  * is no longer a problem).
1960  */
1961 
1962 /*
1963  * The current max is provisioned for:
1964  * vm_compressor_swap_trigger_thread (92)
1965  * 2 x vm_pageout_iothread_internal (92) when vm_restricted_to_single_processor==TRUE
1966  * vm_pageout_continue (92)
1967  * memorystatus_thread (95)
1968  */
1969 #define MAX_VM_BIND_GROUP_COUNT (5)
1970 decl_simple_lock_data(static, sched_vm_group_list_lock);
1971 static thread_t sched_vm_group_thread_list[MAX_VM_BIND_GROUP_COUNT];
1972 static int sched_vm_group_thread_count;
1973 static boolean_t sched_vm_group_temporarily_unbound = FALSE;
1974 
1975 void
thread_vm_bind_group_add(void)1976 thread_vm_bind_group_add(void)
1977 {
1978 	thread_t self = current_thread();
1979 
1980 	thread_reference(self);
1981 	self->options |= TH_OPT_SCHED_VM_GROUP;
1982 
1983 	simple_lock(&sched_vm_group_list_lock, LCK_GRP_NULL);
1984 	assert(sched_vm_group_thread_count < MAX_VM_BIND_GROUP_COUNT);
1985 	sched_vm_group_thread_list[sched_vm_group_thread_count++] = self;
1986 	simple_unlock(&sched_vm_group_list_lock);
1987 
1988 	thread_bind(master_processor);
1989 
1990 	/* Switch to bound processor if not already there */
1991 	thread_block(THREAD_CONTINUE_NULL);
1992 }
1993 
1994 static void
sched_vm_group_maintenance(void)1995 sched_vm_group_maintenance(void)
1996 {
1997 	uint64_t ctime = mach_absolute_time();
1998 	uint64_t longtime = ctime - sched_tick_interval;
1999 	int i;
2000 	spl_t s;
2001 	boolean_t high_latency_observed = FALSE;
2002 	boolean_t runnable_and_not_on_runq_observed = FALSE;
2003 	boolean_t bind_target_changed = FALSE;
2004 	processor_t bind_target = PROCESSOR_NULL;
2005 
2006 	/* Make sure nobody attempts to add new threads while we are enumerating them */
2007 	simple_lock(&sched_vm_group_list_lock, LCK_GRP_NULL);
2008 
2009 	s = splsched();
2010 
2011 	for (i = 0; i < sched_vm_group_thread_count; i++) {
2012 		thread_t thread = sched_vm_group_thread_list[i];
2013 		assert(thread != THREAD_NULL);
2014 		thread_lock(thread);
2015 		if ((thread->state & (TH_RUN | TH_WAIT)) == TH_RUN) {
2016 			if (thread->runq != PROCESSOR_NULL && thread->last_made_runnable_time < longtime) {
2017 				high_latency_observed = TRUE;
2018 			} else if (thread->runq == PROCESSOR_NULL) {
2019 				/* There are some cases where a thread be transitiong that also fall into this case */
2020 				runnable_and_not_on_runq_observed = TRUE;
2021 			}
2022 		}
2023 		thread_unlock(thread);
2024 
2025 		if (high_latency_observed && runnable_and_not_on_runq_observed) {
2026 			/* All the things we are looking for are true, stop looking */
2027 			break;
2028 		}
2029 	}
2030 
2031 	splx(s);
2032 
2033 	if (sched_vm_group_temporarily_unbound) {
2034 		/* If we turned off binding, make sure everything is OK before rebinding */
2035 		if (!high_latency_observed) {
2036 			/* rebind */
2037 			bind_target_changed = TRUE;
2038 			bind_target = master_processor;
2039 			sched_vm_group_temporarily_unbound = FALSE; /* might be reset to TRUE if change cannot be completed */
2040 		}
2041 	} else {
2042 		/*
2043 		 * Check if we're in a bad state, which is defined by high
2044 		 * latency with no core currently executing a thread. If a
2045 		 * single thread is making progress on a CPU, that means the
2046 		 * binding concept to reduce parallelism is working as
2047 		 * designed.
2048 		 */
2049 		if (high_latency_observed && !runnable_and_not_on_runq_observed) {
2050 			/* unbind */
2051 			bind_target_changed = TRUE;
2052 			bind_target = PROCESSOR_NULL;
2053 			sched_vm_group_temporarily_unbound = TRUE;
2054 		}
2055 	}
2056 
2057 	if (bind_target_changed) {
2058 		s = splsched();
2059 		for (i = 0; i < sched_vm_group_thread_count; i++) {
2060 			thread_t thread = sched_vm_group_thread_list[i];
2061 			boolean_t removed;
2062 			assert(thread != THREAD_NULL);
2063 
2064 			thread_lock(thread);
2065 			removed = thread_run_queue_remove(thread);
2066 			if (removed || ((thread->state & (TH_RUN | TH_WAIT)) == TH_WAIT)) {
2067 				thread_bind_internal(thread, bind_target);
2068 			} else {
2069 				/*
2070 				 * Thread was in the middle of being context-switched-to,
2071 				 * or was in the process of blocking. To avoid switching the bind
2072 				 * state out mid-flight, defer the change if possible.
2073 				 */
2074 				if (bind_target == PROCESSOR_NULL) {
2075 					thread_bind_internal(thread, bind_target);
2076 				} else {
2077 					sched_vm_group_temporarily_unbound = TRUE; /* next pass will try again */
2078 				}
2079 			}
2080 
2081 			if (removed) {
2082 				thread_run_queue_reinsert(thread, SCHED_PREEMPT | SCHED_TAILQ);
2083 			}
2084 			thread_unlock(thread);
2085 		}
2086 		splx(s);
2087 	}
2088 
2089 	simple_unlock(&sched_vm_group_list_lock);
2090 }
2091 
2092 #if defined(__x86_64__)
2093 #define SCHED_AVOID_CPU0 1
2094 #else
2095 #define SCHED_AVOID_CPU0 0
2096 #endif
2097 
2098 int sched_allow_rt_smt = 1;
2099 int sched_avoid_cpu0 = SCHED_AVOID_CPU0;
2100 int sched_allow_rt_steal = 1;
2101 int sched_backup_cpu_timeout_count = 5; /* The maximum number of 10us delays to wait before using a backup cpu */
2102 
2103 int sched_rt_n_backup_processors = SCHED_DEFAULT_BACKUP_PROCESSORS;
2104 
2105 int
sched_get_rt_n_backup_processors(void)2106 sched_get_rt_n_backup_processors(void)
2107 {
2108 	return sched_rt_n_backup_processors;
2109 }
2110 
2111 void
sched_set_rt_n_backup_processors(int n)2112 sched_set_rt_n_backup_processors(int n)
2113 {
2114 	if (n < 0) {
2115 		n = 0;
2116 	} else if (n > SCHED_MAX_BACKUP_PROCESSORS) {
2117 		n = SCHED_MAX_BACKUP_PROCESSORS;
2118 	}
2119 
2120 	sched_rt_n_backup_processors = n;
2121 }
2122 
2123 int sched_rt_runq_strict_priority = false;
2124 
2125 inline static processor_set_t
change_locked_pset(processor_set_t current_pset,processor_set_t new_pset)2126 change_locked_pset(processor_set_t current_pset, processor_set_t new_pset)
2127 {
2128 	if (current_pset != new_pset) {
2129 		pset_unlock(current_pset);
2130 		pset_lock(new_pset);
2131 	}
2132 
2133 	return new_pset;
2134 }
2135 
2136 /*
2137  * Invoked prior to idle entry to determine if, on SMT capable processors, an SMT
2138  * rebalancing opportunity exists when a core is (instantaneously) idle, but
2139  * other SMT-capable cores may be over-committed. TODO: some possible negatives:
2140  * IPI thrash if this core does not remain idle following the load balancing ASTs
2141  * Idle "thrash", when IPI issue is followed by idle entry/core power down
2142  * followed by a wakeup shortly thereafter.
2143  */
2144 
2145 #if (DEVELOPMENT || DEBUG)
2146 int sched_smt_balance = 1;
2147 #endif
2148 
2149 /* Invoked with pset locked, returns with pset unlocked */
2150 bool
sched_SMT_balance(processor_t cprocessor,processor_set_t cpset)2151 sched_SMT_balance(processor_t cprocessor, processor_set_t cpset)
2152 {
2153 	processor_t ast_processor = NULL;
2154 
2155 #if (DEVELOPMENT || DEBUG)
2156 	if (__improbable(sched_smt_balance == 0)) {
2157 		goto smt_balance_exit;
2158 	}
2159 #endif
2160 
2161 	assert(cprocessor == current_processor());
2162 	if (cprocessor->is_SMT == FALSE) {
2163 		goto smt_balance_exit;
2164 	}
2165 
2166 	processor_t sib_processor = cprocessor->processor_secondary ? cprocessor->processor_secondary : cprocessor->processor_primary;
2167 
2168 	/* Determine if both this processor and its sibling are idle,
2169 	 * indicating an SMT rebalancing opportunity.
2170 	 */
2171 	if (sib_processor->state != PROCESSOR_IDLE) {
2172 		goto smt_balance_exit;
2173 	}
2174 
2175 	processor_t sprocessor;
2176 
2177 	sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
2178 	uint64_t running_secondary_map = (cpset->cpu_state_map[PROCESSOR_RUNNING] &
2179 	    ~cpset->primary_map);
2180 	for (int cpuid = lsb_first(running_secondary_map); cpuid >= 0; cpuid = lsb_next(running_secondary_map, cpuid)) {
2181 		sprocessor = processor_array[cpuid];
2182 		if ((sprocessor->processor_primary->state == PROCESSOR_RUNNING) &&
2183 		    (sprocessor->current_pri < BASEPRI_RTQUEUES)) {
2184 			ipi_type = sched_ipi_action(sprocessor, NULL, SCHED_IPI_EVENT_SMT_REBAL);
2185 			if (ipi_type != SCHED_IPI_NONE) {
2186 				assert(sprocessor != cprocessor);
2187 				ast_processor = sprocessor;
2188 				break;
2189 			}
2190 		}
2191 	}
2192 
2193 smt_balance_exit:
2194 	pset_unlock(cpset);
2195 
2196 	if (ast_processor) {
2197 		KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_SMT_BALANCE), ast_processor->cpu_id, ast_processor->state, ast_processor->processor_primary->state, 0, 0);
2198 		sched_ipi_perform(ast_processor, ipi_type);
2199 	}
2200 	return false;
2201 }
2202 
2203 static cpumap_t
pset_available_cpumap(processor_set_t pset)2204 pset_available_cpumap(processor_set_t pset)
2205 {
2206 	return pset->cpu_available_map & pset->recommended_bitmask;
2207 }
2208 
2209 int
pset_available_cpu_count(processor_set_t pset)2210 pset_available_cpu_count(processor_set_t pset)
2211 {
2212 	return bit_count(pset_available_cpumap(pset));
2213 }
2214 
2215 bool
pset_is_recommended(processor_set_t pset)2216 pset_is_recommended(processor_set_t pset)
2217 {
2218 	if (!pset) {
2219 		return false;
2220 	}
2221 	return pset_available_cpu_count(pset) > 0;
2222 }
2223 
2224 static cpumap_t
pset_available_but_not_running_cpumap(processor_set_t pset)2225 pset_available_but_not_running_cpumap(processor_set_t pset)
2226 {
2227 	return (pset->cpu_state_map[PROCESSOR_IDLE] | pset->cpu_state_map[PROCESSOR_DISPATCHING]) &
2228 	       pset->recommended_bitmask;
2229 }
2230 
2231 bool
pset_has_stealable_threads(processor_set_t pset)2232 pset_has_stealable_threads(processor_set_t pset)
2233 {
2234 	pset_assert_locked(pset);
2235 
2236 	cpumap_t avail_map = pset_available_but_not_running_cpumap(pset);
2237 	/*
2238 	 * Secondary CPUs never steal, so allow stealing of threads if there are more threads than
2239 	 * available primary CPUs
2240 	 */
2241 	avail_map &= pset->primary_map;
2242 
2243 	return (pset->pset_runq.count > 0) && ((pset->pset_runq.count + rt_runq_count(pset)) > bit_count(avail_map));
2244 }
2245 
2246 static cpumap_t
pset_available_but_not_running_rt_threads_cpumap(processor_set_t pset)2247 pset_available_but_not_running_rt_threads_cpumap(processor_set_t pset)
2248 {
2249 	cpumap_t avail_map = pset_available_cpumap(pset);
2250 	if (!sched_allow_rt_smt) {
2251 		/*
2252 		 * Secondary CPUs are not allowed to run RT threads, so
2253 		 * only primary CPUs should be included
2254 		 */
2255 		avail_map &= pset->primary_map;
2256 	}
2257 
2258 	return avail_map & ~pset->realtime_map;
2259 }
2260 
2261 static bool
pset_needs_a_followup_IPI(processor_set_t pset)2262 pset_needs_a_followup_IPI(processor_set_t pset)
2263 {
2264 	int nbackup_cpus = 0;
2265 
2266 	if (rt_runq_is_low_latency(pset)) {
2267 		nbackup_cpus = sched_rt_n_backup_processors;
2268 	}
2269 
2270 	int rt_rq_count = rt_runq_count(pset);
2271 
2272 	return (rt_rq_count > 0) && ((rt_rq_count + nbackup_cpus - bit_count(pset->pending_AST_URGENT_cpu_mask)) > 0);
2273 }
2274 
2275 bool
pset_has_stealable_rt_threads(processor_set_t pset)2276 pset_has_stealable_rt_threads(processor_set_t pset)
2277 {
2278 	pset_node_t node = pset->node;
2279 	if (bit_count(node->pset_map) == 1) {
2280 		return false;
2281 	}
2282 
2283 	cpumap_t avail_map = pset_available_but_not_running_rt_threads_cpumap(pset);
2284 
2285 	return rt_runq_count(pset) > bit_count(avail_map);
2286 }
2287 
2288 static void
pset_update_rt_stealable_state(processor_set_t pset)2289 pset_update_rt_stealable_state(processor_set_t pset)
2290 {
2291 	if (pset_has_stealable_rt_threads(pset)) {
2292 		pset->stealable_rt_threads_earliest_deadline = rt_runq_earliest_deadline(pset);
2293 	} else {
2294 		pset->stealable_rt_threads_earliest_deadline = RT_DEADLINE_NONE;
2295 	}
2296 }
2297 
2298 static void
clear_pending_AST_bits(processor_set_t pset,processor_t processor,__kdebug_only const int trace_point_number)2299 clear_pending_AST_bits(processor_set_t pset, processor_t processor, __kdebug_only const int trace_point_number)
2300 {
2301 	/* Acknowledge any pending IPIs here with pset lock held */
2302 	pset_assert_locked(pset);
2303 	if (bit_clear_if_set(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
2304 		KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_END,
2305 		    processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, 0, trace_point_number);
2306 	}
2307 	bit_clear(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
2308 
2309 #if defined(CONFIG_SCHED_DEFERRED_AST)
2310 	bit_clear(pset->pending_deferred_AST_cpu_mask, processor->cpu_id);
2311 #endif
2312 }
2313 
2314 /*
2315  * Called with pset locked, on a processor that is committing to run a new thread
2316  * Will transition an idle or dispatching processor to running as it picks up
2317  * the first new thread from the idle thread.
2318  */
2319 static void
pset_commit_processor_to_new_thread(processor_set_t pset,processor_t processor,thread_t new_thread)2320 pset_commit_processor_to_new_thread(processor_set_t pset, processor_t processor, thread_t new_thread)
2321 {
2322 	pset_assert_locked(pset);
2323 
2324 	if (processor->state == PROCESSOR_DISPATCHING || processor->state == PROCESSOR_IDLE) {
2325 		assert(current_thread() == processor->idle_thread);
2326 
2327 		/*
2328 		 * Dispatching processor is now committed to running new_thread,
2329 		 * so change its state to PROCESSOR_RUNNING.
2330 		 */
2331 		pset_update_processor_state(pset, processor, PROCESSOR_RUNNING);
2332 	} else {
2333 		assert((processor->state == PROCESSOR_RUNNING) || (processor->state == PROCESSOR_SHUTDOWN));
2334 	}
2335 
2336 	processor_state_update_from_thread(processor, new_thread, true);
2337 
2338 	if (new_thread->sched_pri >= BASEPRI_RTQUEUES) {
2339 		bit_set(pset->realtime_map, processor->cpu_id);
2340 	} else {
2341 		bit_clear(pset->realtime_map, processor->cpu_id);
2342 	}
2343 	pset_update_rt_stealable_state(pset);
2344 
2345 	pset_node_t node = pset->node;
2346 
2347 	if (bit_count(node->pset_map) == 1) {
2348 		/* Node has only a single pset, so skip node pset map updates */
2349 		return;
2350 	}
2351 
2352 	cpumap_t avail_map = pset_available_cpumap(pset);
2353 
2354 	if (new_thread->sched_pri >= BASEPRI_RTQUEUES) {
2355 		if ((avail_map & pset->realtime_map) == avail_map) {
2356 			/* No more non-RT CPUs in this pset */
2357 			atomic_bit_clear(&node->pset_non_rt_map, pset->pset_id, memory_order_relaxed);
2358 		}
2359 		avail_map &= pset->primary_map;
2360 		if ((avail_map & pset->realtime_map) == avail_map) {
2361 			/* No more non-RT primary CPUs in this pset */
2362 			atomic_bit_clear(&node->pset_non_rt_primary_map, pset->pset_id, memory_order_relaxed);
2363 		}
2364 	} else {
2365 		if ((avail_map & pset->realtime_map) != avail_map) {
2366 			if (!bit_test(atomic_load(&node->pset_non_rt_map), pset->pset_id)) {
2367 				atomic_bit_set(&node->pset_non_rt_map, pset->pset_id, memory_order_relaxed);
2368 			}
2369 		}
2370 		avail_map &= pset->primary_map;
2371 		if ((avail_map & pset->realtime_map) != avail_map) {
2372 			if (!bit_test(atomic_load(&node->pset_non_rt_primary_map), pset->pset_id)) {
2373 				atomic_bit_set(&node->pset_non_rt_primary_map, pset->pset_id, memory_order_relaxed);
2374 			}
2375 		}
2376 	}
2377 }
2378 
2379 static processor_t choose_processor_for_realtime_thread(processor_set_t pset, processor_t skip_processor, bool consider_secondaries, bool skip_spills);
2380 static processor_t choose_furthest_deadline_processor_for_realtime_thread(processor_set_t pset, int max_pri, uint64_t minimum_deadline,
2381     processor_t skip_processor, bool skip_spills, bool include_ast_urgent_pending_cpus);
2382 static processor_t choose_next_processor_for_realtime_thread(processor_set_t pset, int max_pri, uint64_t minimum_deadline, processor_t skip_processor, bool consider_secondaries);
2383 #if defined(__x86_64__)
2384 static bool all_available_primaries_are_running_realtime_threads(processor_set_t pset, bool include_backups);
2385 static bool these_processors_are_running_realtime_threads(processor_set_t pset, uint64_t these_map, bool include_backups);
2386 #endif
2387 static bool sched_ok_to_run_realtime_thread(processor_set_t pset, processor_t processor, bool as_backup);
2388 static bool processor_is_fast_track_candidate_for_realtime_thread(processor_set_t pset, processor_t processor);
2389 
2390 static bool
other_psets_have_earlier_rt_threads_pending(processor_set_t stealing_pset,uint64_t earliest_deadline)2391 other_psets_have_earlier_rt_threads_pending(processor_set_t stealing_pset, uint64_t earliest_deadline)
2392 {
2393 	pset_map_t pset_map = stealing_pset->node->pset_map;
2394 
2395 	bit_clear(pset_map, stealing_pset->pset_id);
2396 
2397 	for (int pset_id = lsb_first(pset_map); pset_id >= 0; pset_id = lsb_next(pset_map, pset_id)) {
2398 		processor_set_t nset = pset_array[pset_id];
2399 
2400 		if (deadline_add(nset->stealable_rt_threads_earliest_deadline, rt_deadline_epsilon) < earliest_deadline) {
2401 			return true;
2402 		}
2403 	}
2404 
2405 	return false;
2406 }
2407 
2408 /*
2409  * starting_pset must be locked, but returns true if it is unlocked before return
2410  */
2411 static bool
choose_next_rt_processor_for_IPI(processor_set_t starting_pset,processor_t chosen_processor,bool spill_ipi,processor_t * result_processor,sched_ipi_type_t * result_ipi_type)2412 choose_next_rt_processor_for_IPI(processor_set_t starting_pset, processor_t chosen_processor, bool spill_ipi,
2413     processor_t *result_processor, sched_ipi_type_t *result_ipi_type)
2414 {
2415 	bool starting_pset_is_unlocked = false;
2416 	uint64_t earliest_deadline = rt_runq_earliest_deadline(starting_pset);
2417 	int max_pri = rt_runq_priority(starting_pset);
2418 	__kdebug_only uint64_t spill_tid = thread_tid(rt_runq_first(&starting_pset->rt_runq));
2419 	processor_set_t pset = starting_pset;
2420 	processor_t next_rt_processor = PROCESSOR_NULL;
2421 	if (spill_ipi) {
2422 		processor_set_t nset = next_pset(pset);
2423 		assert(nset != starting_pset);
2424 		pset = change_locked_pset(pset, nset);
2425 		starting_pset_is_unlocked = true;
2426 	}
2427 	do {
2428 		const bool consider_secondaries = true;
2429 		next_rt_processor = choose_next_processor_for_realtime_thread(pset, max_pri, earliest_deadline, chosen_processor, consider_secondaries);
2430 		if (next_rt_processor == PROCESSOR_NULL) {
2431 			if (!spill_ipi) {
2432 				break;
2433 			}
2434 			processor_set_t nset = next_pset(pset);
2435 			if (nset == starting_pset) {
2436 				break;
2437 			}
2438 			pset = change_locked_pset(pset, nset);
2439 			starting_pset_is_unlocked = true;
2440 		}
2441 	} while (next_rt_processor == PROCESSOR_NULL);
2442 	if (next_rt_processor) {
2443 		if (pset != starting_pset) {
2444 			if (bit_set_if_clear(pset->rt_pending_spill_cpu_mask, next_rt_processor->cpu_id)) {
2445 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_RT_SIGNAL_SPILL) | DBG_FUNC_START,
2446 				    next_rt_processor->cpu_id, pset->rt_pending_spill_cpu_mask, starting_pset->cpu_set_low, (uintptr_t)spill_tid);
2447 			}
2448 		}
2449 		*result_ipi_type = sched_ipi_action(next_rt_processor, NULL, SCHED_IPI_EVENT_RT_PREEMPT);
2450 		*result_processor = next_rt_processor;
2451 	}
2452 	if (pset != starting_pset) {
2453 		pset_unlock(pset);
2454 	}
2455 
2456 	return starting_pset_is_unlocked;
2457 }
2458 
2459 /*
2460  * backup processor - used by choose_processor to send a backup IPI to in case the preferred processor can't immediately respond
2461  * followup processor - used in thread_select when there are still threads on the run queue and available processors
2462  * spill processor - a processor in a different processor set that is signalled to steal a thread from this run queue
2463  */
2464 typedef enum {
2465 	none,
2466 	backup,
2467 	followup,
2468 	spill
2469 } next_processor_type_t;
2470 
2471 #undef LOOP_COUNT
2472 #ifdef LOOP_COUNT
2473 int max_loop_count[MAX_SCHED_CPUS] = { 0 };
2474 #endif
2475 
2476 /*
2477  *	thread_select:
2478  *
2479  *	Select a new thread for the current processor to execute.
2480  *
2481  *	May select the current thread, which must be locked.
2482  */
2483 static thread_t
thread_select(thread_t thread,processor_t processor,ast_t * reason)2484 thread_select(thread_t          thread,
2485     processor_t       processor,
2486     ast_t            *reason)
2487 {
2488 	processor_set_t         pset = processor->processor_set;
2489 	thread_t                        new_thread = THREAD_NULL;
2490 
2491 	assert(processor == current_processor());
2492 	assert((thread->state & (TH_RUN | TH_TERMINATE2)) == TH_RUN);
2493 
2494 	KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SELECT) | DBG_FUNC_START,
2495 	    0, pset->pending_AST_URGENT_cpu_mask, 0, 0);
2496 
2497 	__kdebug_only int idle_reason = 0;
2498 	__kdebug_only int delay_count = 0;
2499 
2500 #if defined(__x86_64__)
2501 	int timeout_count = sched_backup_cpu_timeout_count;
2502 	if ((sched_avoid_cpu0 == 1) && (processor->cpu_id == 0)) {
2503 		/* Prefer cpu0 as backup */
2504 		timeout_count--;
2505 	} else if ((sched_avoid_cpu0 == 2) && (processor->processor_primary != processor)) {
2506 		/* Prefer secondary cpu as backup */
2507 		timeout_count--;
2508 	}
2509 #endif
2510 	bool pending_AST_URGENT = false;
2511 	bool pending_AST_PREEMPT = false;
2512 
2513 #ifdef LOOP_COUNT
2514 	int loop_count = -1;
2515 #endif
2516 
2517 	do {
2518 		/*
2519 		 *	Update the priority.
2520 		 */
2521 		if (SCHED(can_update_priority)(thread)) {
2522 			SCHED(update_priority)(thread);
2523 		}
2524 
2525 		pset_lock(pset);
2526 
2527 restart:
2528 #ifdef LOOP_COUNT
2529 		loop_count++;
2530 		if (loop_count > max_loop_count[processor->cpu_id]) {
2531 			max_loop_count[processor->cpu_id] = loop_count;
2532 			if (bit_count(loop_count) == 1) {
2533 				kprintf("[%d]%s>max_loop_count = %d\n", processor->cpu_id, __FUNCTION__, loop_count);
2534 			}
2535 		}
2536 #endif
2537 		pending_AST_URGENT = bit_test(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id);
2538 		pending_AST_PREEMPT = bit_test(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
2539 
2540 		processor_state_update_from_thread(processor, thread, true);
2541 
2542 		idle_reason = 0;
2543 
2544 		processor_t ast_processor = PROCESSOR_NULL;
2545 		processor_t next_rt_processor = PROCESSOR_NULL;
2546 		sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
2547 		sched_ipi_type_t next_rt_ipi_type = SCHED_IPI_NONE;
2548 
2549 		assert(processor->state != PROCESSOR_OFF_LINE);
2550 
2551 		/*
2552 		 * Bound threads are dispatched to a processor without going through
2553 		 * choose_processor(), so in those cases we must continue trying to dequeue work
2554 		 * as we are the only option.
2555 		 */
2556 		if (!SCHED(processor_bound_count)(processor)) {
2557 			if (!processor->is_recommended) {
2558 				/*
2559 				 * The performance controller has provided a hint to not dispatch more threads,
2560 				 */
2561 				idle_reason = 1;
2562 				goto send_followup_ipi_before_idle;
2563 			} else if (rt_runq_count(pset)) {
2564 				bool ok_to_run_realtime_thread = sched_ok_to_run_realtime_thread(pset, processor, false);
2565 				/* Give the current RT thread a chance to complete */
2566 				ok_to_run_realtime_thread |= (thread->sched_pri >= BASEPRI_RTQUEUES && processor->first_timeslice);
2567 #if defined(__x86_64__)
2568 				/*
2569 				 * On Intel we want to avoid SMT secondary processors and processor 0
2570 				 * but allow them to be used as backup processors in case the preferred chosen
2571 				 * processor is delayed by interrupts or processor stalls.  So if it is
2572 				 * not ok_to_run_realtime_thread as preferred (sched_ok_to_run_realtime_thread(pset, processor, as_backup=false))
2573 				 * but ok_to_run_realtime_thread as backup (sched_ok_to_run_realtime_thread(pset, processor, as_backup=true))
2574 				 * we delay up to (timeout_count * 10us) to give the preferred processor chance
2575 				 * to grab the thread before the (current) backup processor does.
2576 				 *
2577 				 * timeout_count defaults to 5 but can be tuned using sysctl kern.sched_backup_cpu_timeout_count
2578 				 * on DEVELOPMENT || DEBUG kernels.  It is also adjusted (see above) depending on whether we want to use
2579 				 * cpu0 before secondary cpus or not.
2580 				 */
2581 				if (!ok_to_run_realtime_thread) {
2582 					if (sched_ok_to_run_realtime_thread(pset, processor, true)) {
2583 						if (timeout_count-- > 0) {
2584 							pset_unlock(pset);
2585 							thread_unlock(thread);
2586 							delay(10);
2587 							delay_count++;
2588 							thread_lock(thread);
2589 							pset_lock(pset);
2590 							goto restart;
2591 						}
2592 						ok_to_run_realtime_thread = true;
2593 					}
2594 				}
2595 #endif
2596 				if (!ok_to_run_realtime_thread) {
2597 					idle_reason = 2;
2598 					goto send_followup_ipi_before_idle;
2599 				}
2600 			} else if (processor->processor_primary != processor) {
2601 				/*
2602 				 * Should this secondary SMT processor attempt to find work? For pset runqueue systems,
2603 				 * we should look for work only under the same conditions that choose_processor()
2604 				 * would have assigned work, which is when all primary processors have been assigned work.
2605 				 */
2606 				if ((pset->recommended_bitmask & pset->primary_map & pset->cpu_state_map[PROCESSOR_IDLE]) != 0) {
2607 					/* There are idle primaries */
2608 					idle_reason = 3;
2609 					goto idle;
2610 				}
2611 			}
2612 		}
2613 
2614 		/*
2615 		 *	Test to see if the current thread should continue
2616 		 *	to run on this processor.  Must not be attempting to wait, and not
2617 		 *	bound to a different processor, nor be in the wrong
2618 		 *	processor set, nor be forced to context switch by TH_SUSP.
2619 		 *
2620 		 *	Note that there are never any RT threads in the regular runqueue.
2621 		 *
2622 		 *	This code is very insanely tricky.
2623 		 */
2624 
2625 		/* i.e. not waiting, not TH_SUSP'ed */
2626 		bool still_running = ((thread->state & (TH_TERMINATE | TH_IDLE | TH_WAIT | TH_RUN | TH_SUSP)) == TH_RUN);
2627 
2628 		/*
2629 		 * Threads running on SMT processors are forced to context switch. Don't rebalance realtime threads.
2630 		 * TODO: This should check if it's worth it to rebalance, i.e. 'are there any idle primary processors'
2631 		 *       <rdar://problem/47907700>
2632 		 *
2633 		 * A yielding thread shouldn't be forced to context switch.
2634 		 */
2635 
2636 		bool is_yielding         = (*reason & AST_YIELD) == AST_YIELD;
2637 
2638 		bool needs_smt_rebalance = !is_yielding && thread->sched_pri < BASEPRI_RTQUEUES && processor->processor_primary != processor;
2639 
2640 		bool affinity_mismatch   = thread->affinity_set != AFFINITY_SET_NULL && thread->affinity_set->aset_pset != pset;
2641 
2642 		bool bound_elsewhere     = thread->bound_processor != PROCESSOR_NULL && thread->bound_processor != processor;
2643 
2644 		bool avoid_processor     = !is_yielding && SCHED(avoid_processor_enabled) && SCHED(thread_avoid_processor)(processor, thread, *reason);
2645 
2646 		bool ok_to_run_realtime_thread = sched_ok_to_run_realtime_thread(pset, processor, true);
2647 
2648 		bool current_thread_can_keep_running = (still_running && !needs_smt_rebalance && !affinity_mismatch && !bound_elsewhere && !avoid_processor);
2649 		if (current_thread_can_keep_running) {
2650 			/*
2651 			 * This thread is eligible to keep running on this processor.
2652 			 *
2653 			 * RT threads with un-expired quantum stay on processor,
2654 			 * unless there's a valid RT thread with an earlier deadline
2655 			 * and it is still ok_to_run_realtime_thread.
2656 			 */
2657 			if (thread->sched_pri >= BASEPRI_RTQUEUES && processor->first_timeslice) {
2658 				/*
2659 				 * Pick a new RT thread only if ok_to_run_realtime_thread
2660 				 * (but the current thread is allowed to complete).
2661 				 */
2662 				if (ok_to_run_realtime_thread) {
2663 					if (bit_test(pset->rt_pending_spill_cpu_mask, processor->cpu_id)) {
2664 						goto pick_new_rt_thread;
2665 					}
2666 					if (rt_runq_priority(pset) > thread->sched_pri) {
2667 						if (sched_rt_runq_strict_priority) {
2668 							/* The next RT thread is better, so pick it off the runqueue. */
2669 							goto pick_new_rt_thread;
2670 						}
2671 
2672 						/*
2673 						 * See if the current lower priority thread can continue to run without causing
2674 						 * the higher priority thread on the runq queue to miss its deadline.
2675 						 */
2676 						thread_t hi_thread = rt_runq_first(SCHED(rt_runq)(pset));
2677 						if (thread->realtime.computation + hi_thread->realtime.computation + rt_deadline_epsilon >= hi_thread->realtime.constraint) {
2678 							/* The next RT thread is better, so pick it off the runqueue. */
2679 							goto pick_new_rt_thread;
2680 						}
2681 					} else if ((rt_runq_count(pset) > 0) && (deadline_add(rt_runq_earliest_deadline(pset), rt_deadline_epsilon) < thread->realtime.deadline)) {
2682 						/* The next RT thread is better, so pick it off the runqueue. */
2683 						goto pick_new_rt_thread;
2684 					}
2685 					if (other_psets_have_earlier_rt_threads_pending(pset, thread->realtime.deadline)) {
2686 						goto pick_new_rt_thread;
2687 					}
2688 				}
2689 
2690 				/* This is still the best RT thread to run. */
2691 				processor->deadline = thread->realtime.deadline;
2692 
2693 				sched_update_pset_load_average(pset, 0);
2694 
2695 				clear_pending_AST_bits(pset, processor, 1);
2696 
2697 				next_rt_processor = PROCESSOR_NULL;
2698 				next_rt_ipi_type = SCHED_IPI_NONE;
2699 
2700 				bool pset_unlocked = false;
2701 				__kdebug_only next_processor_type_t nptype = none;
2702 				if (sched_allow_rt_steal && pset_has_stealable_rt_threads(pset)) {
2703 					nptype = spill;
2704 					pset_unlocked = choose_next_rt_processor_for_IPI(pset, processor, true, &next_rt_processor, &next_rt_ipi_type);
2705 				} else if (pset_needs_a_followup_IPI(pset)) {
2706 					nptype = followup;
2707 					pset_unlocked = choose_next_rt_processor_for_IPI(pset, processor, false, &next_rt_processor, &next_rt_ipi_type);
2708 				}
2709 				if (!pset_unlocked) {
2710 					pset_unlock(pset);
2711 				}
2712 
2713 				if (next_rt_processor) {
2714 					KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_NEXT_PROCESSOR) | DBG_FUNC_NONE,
2715 					    next_rt_processor->cpu_id, next_rt_processor->state, nptype, 2);
2716 					sched_ipi_perform(next_rt_processor, next_rt_ipi_type);
2717 				}
2718 
2719 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SELECT) | DBG_FUNC_END,
2720 				    (uintptr_t)thread_tid(thread), pset->pending_AST_URGENT_cpu_mask, delay_count, 1);
2721 				return thread;
2722 			}
2723 
2724 			if ((rt_runq_count(pset) == 0) &&
2725 			    SCHED(processor_queue_has_priority)(processor, thread->sched_pri, TRUE) == FALSE) {
2726 				/* This thread is still the highest priority runnable (non-idle) thread */
2727 				processor->deadline = RT_DEADLINE_NONE;
2728 
2729 				sched_update_pset_load_average(pset, 0);
2730 
2731 				clear_pending_AST_bits(pset, processor, 2);
2732 
2733 				pset_unlock(pset);
2734 
2735 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SELECT) | DBG_FUNC_END,
2736 				    (uintptr_t)thread_tid(thread), pset->pending_AST_URGENT_cpu_mask, delay_count, 2);
2737 				return thread;
2738 			}
2739 		} else {
2740 			/*
2741 			 * This processor must context switch.
2742 			 * If it's due to a rebalance, we should aggressively find this thread a new home.
2743 			 */
2744 			if (needs_smt_rebalance || affinity_mismatch || bound_elsewhere || avoid_processor) {
2745 				*reason |= AST_REBALANCE;
2746 			}
2747 		}
2748 
2749 		bool secondary_forced_idle = ((processor->processor_secondary != PROCESSOR_NULL) &&
2750 		    (thread_no_smt(thread) || (thread->sched_pri >= BASEPRI_RTQUEUES)) &&
2751 		    (processor->processor_secondary->state == PROCESSOR_IDLE));
2752 
2753 		/* OK, so we're not going to run the current thread. Look at the RT queue. */
2754 		if (ok_to_run_realtime_thread) {
2755 pick_new_rt_thread:
2756 			new_thread = sched_rt_choose_thread(pset);
2757 			if (new_thread != THREAD_NULL) {
2758 				processor->deadline = new_thread->realtime.deadline;
2759 				pset_commit_processor_to_new_thread(pset, processor, new_thread);
2760 
2761 				clear_pending_AST_bits(pset, processor, 3);
2762 
2763 				if (processor->processor_secondary != NULL) {
2764 					processor_t sprocessor = processor->processor_secondary;
2765 					if ((sprocessor->state == PROCESSOR_RUNNING) || (sprocessor->state == PROCESSOR_DISPATCHING)) {
2766 						ipi_type = sched_ipi_action(sprocessor, NULL, SCHED_IPI_EVENT_SMT_REBAL);
2767 						ast_processor = sprocessor;
2768 					}
2769 				}
2770 			}
2771 		}
2772 
2773 send_followup_ipi_before_idle:
2774 		/* This might not have been cleared if we didn't call sched_rt_choose_thread() */
2775 		if (bit_clear_if_set(pset->rt_pending_spill_cpu_mask, processor->cpu_id)) {
2776 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_RT_SIGNAL_SPILL) | DBG_FUNC_END, processor->cpu_id, pset->rt_pending_spill_cpu_mask, 0, 5);
2777 		}
2778 		__kdebug_only next_processor_type_t nptype = none;
2779 		bool pset_unlocked = false;
2780 		if (sched_allow_rt_steal && pset_has_stealable_rt_threads(pset)) {
2781 			nptype = spill;
2782 			pset_unlocked = choose_next_rt_processor_for_IPI(pset, processor, true, &next_rt_processor, &next_rt_ipi_type);
2783 		} else if (pset_needs_a_followup_IPI(pset)) {
2784 			nptype = followup;
2785 			pset_unlocked = choose_next_rt_processor_for_IPI(pset, processor, false, &next_rt_processor, &next_rt_ipi_type);
2786 		}
2787 
2788 		assert(new_thread || !ast_processor);
2789 		if (new_thread || next_rt_processor) {
2790 			if (!pset_unlocked) {
2791 				pset_unlock(pset);
2792 				pset_unlocked = true;
2793 			}
2794 			if (ast_processor == next_rt_processor) {
2795 				ast_processor = PROCESSOR_NULL;
2796 				ipi_type = SCHED_IPI_NONE;
2797 			}
2798 
2799 			if (ast_processor) {
2800 				sched_ipi_perform(ast_processor, ipi_type);
2801 			}
2802 
2803 			if (next_rt_processor) {
2804 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_NEXT_PROCESSOR) | DBG_FUNC_NONE,
2805 				    next_rt_processor->cpu_id, next_rt_processor->state, nptype, 3);
2806 				sched_ipi_perform(next_rt_processor, next_rt_ipi_type);
2807 			}
2808 
2809 			if (new_thread) {
2810 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SELECT) | DBG_FUNC_END,
2811 				    (uintptr_t)thread_tid(new_thread), pset->pending_AST_URGENT_cpu_mask, delay_count, 3);
2812 				return new_thread;
2813 			}
2814 		}
2815 
2816 		if (pset_unlocked) {
2817 			pset_lock(pset);
2818 		}
2819 
2820 		if (!pending_AST_URGENT && bit_test(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
2821 			/* Things changed while we dropped the lock */
2822 			goto restart;
2823 		}
2824 
2825 		if (processor->is_recommended) {
2826 			bool spill_pending = bit_test(pset->rt_pending_spill_cpu_mask, processor->cpu_id);
2827 			if (sched_ok_to_run_realtime_thread(pset, processor, true) && (spill_pending || rt_runq_count(pset))) {
2828 				/* Things changed while we dropped the lock */
2829 				goto restart;
2830 			}
2831 
2832 			if ((processor->processor_primary != processor) && (processor->processor_primary->current_pri >= BASEPRI_RTQUEUES)) {
2833 				/* secondary can only run realtime thread */
2834 				if (idle_reason == 0) {
2835 					idle_reason = 4;
2836 				}
2837 				goto idle;
2838 			}
2839 		} else if (!SCHED(processor_bound_count)(processor)) {
2840 			/* processor not recommended and no bound threads */
2841 			if (idle_reason == 0) {
2842 				idle_reason = 5;
2843 			}
2844 			goto idle;
2845 		}
2846 
2847 		processor->deadline = RT_DEADLINE_NONE;
2848 
2849 		/* No RT threads, so let's look at the regular threads. */
2850 		if ((new_thread = SCHED(choose_thread)(processor, MINPRI, *reason)) != THREAD_NULL) {
2851 			pset_commit_processor_to_new_thread(pset, processor, new_thread);
2852 
2853 			clear_pending_AST_bits(pset, processor, 4);
2854 
2855 			ast_processor = PROCESSOR_NULL;
2856 			ipi_type = SCHED_IPI_NONE;
2857 
2858 			processor_t sprocessor = processor->processor_secondary;
2859 			if (sprocessor != NULL) {
2860 				if (sprocessor->state == PROCESSOR_RUNNING) {
2861 					if (thread_no_smt(new_thread)) {
2862 						ipi_type = sched_ipi_action(sprocessor, NULL, SCHED_IPI_EVENT_SMT_REBAL);
2863 						ast_processor = sprocessor;
2864 					}
2865 				} else if (secondary_forced_idle && !thread_no_smt(new_thread) && pset_has_stealable_threads(pset)) {
2866 					ipi_type = sched_ipi_action(sprocessor, NULL, SCHED_IPI_EVENT_PREEMPT);
2867 					ast_processor = sprocessor;
2868 				}
2869 			}
2870 			pset_unlock(pset);
2871 
2872 			if (ast_processor) {
2873 				sched_ipi_perform(ast_processor, ipi_type);
2874 			}
2875 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SELECT) | DBG_FUNC_END,
2876 			    (uintptr_t)thread_tid(new_thread), pset->pending_AST_URGENT_cpu_mask, delay_count, 4);
2877 			return new_thread;
2878 		}
2879 
2880 		if (processor->must_idle) {
2881 			processor->must_idle = false;
2882 			*reason |= AST_REBALANCE;
2883 			idle_reason = 6;
2884 			goto idle;
2885 		}
2886 
2887 		if (SCHED(steal_thread_enabled)(pset) && (processor->processor_primary == processor)) {
2888 			/*
2889 			 * No runnable threads, attempt to steal
2890 			 * from other processors. Returns with pset lock dropped.
2891 			 */
2892 
2893 			if ((new_thread = SCHED(steal_thread)(pset)) != THREAD_NULL) {
2894 				pset_lock(pset);
2895 				pset_commit_processor_to_new_thread(pset, processor, new_thread);
2896 				if (!pending_AST_URGENT && bit_test(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
2897 					/*
2898 					 * A realtime thread choose this processor while it was DISPATCHING
2899 					 * and the pset lock was dropped
2900 					 */
2901 					ast_on(AST_URGENT | AST_PREEMPT);
2902 				}
2903 
2904 				clear_pending_AST_bits(pset, processor, 5);
2905 
2906 				pset_unlock(pset);
2907 
2908 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SELECT) | DBG_FUNC_END,
2909 				    (uintptr_t)thread_tid(new_thread), pset->pending_AST_URGENT_cpu_mask, delay_count, 5);
2910 				return new_thread;
2911 			}
2912 
2913 			/*
2914 			 * If other threads have appeared, shortcut
2915 			 * around again.
2916 			 */
2917 			if (SCHED(processor_bound_count)(processor)) {
2918 				continue;
2919 			}
2920 			if (processor->is_recommended) {
2921 				if (!SCHED(processor_queue_empty)(processor) || (sched_ok_to_run_realtime_thread(pset, processor, true) && (rt_runq_count(pset) > 0))) {
2922 					continue;
2923 				}
2924 			}
2925 
2926 			pset_lock(pset);
2927 		}
2928 
2929 idle:
2930 		/* Someone selected this processor while we had dropped the lock */
2931 		if ((!pending_AST_URGENT && bit_test(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) ||
2932 		    (!pending_AST_PREEMPT && bit_test(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id))) {
2933 			goto restart;
2934 		}
2935 
2936 		if ((idle_reason == 0) && current_thread_can_keep_running) {
2937 			/* This thread is the only runnable (non-idle) thread */
2938 			if (thread->sched_pri >= BASEPRI_RTQUEUES) {
2939 				processor->deadline = thread->realtime.deadline;
2940 			} else {
2941 				processor->deadline = RT_DEADLINE_NONE;
2942 			}
2943 
2944 			sched_update_pset_load_average(pset, 0);
2945 
2946 			clear_pending_AST_bits(pset, processor, 6);
2947 
2948 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SELECT) | DBG_FUNC_END,
2949 			    (uintptr_t)thread_tid(thread), pset->pending_AST_URGENT_cpu_mask, delay_count, 6);
2950 			pset_unlock(pset);
2951 			return thread;
2952 		}
2953 
2954 		/*
2955 		 *	Nothing is runnable, or this processor must be forced idle,
2956 		 *	so set this processor idle if it was running.
2957 		 */
2958 		if ((processor->state == PROCESSOR_RUNNING) || (processor->state == PROCESSOR_DISPATCHING)) {
2959 			pset_update_processor_state(pset, processor, PROCESSOR_IDLE);
2960 			processor_state_update_idle(processor);
2961 		}
2962 		pset_update_rt_stealable_state(pset);
2963 
2964 		clear_pending_AST_bits(pset, processor, 7);
2965 
2966 		/* Invoked with pset locked, returns with pset unlocked */
2967 		processor->next_idle_short = SCHED(processor_balance)(processor, pset);
2968 
2969 		new_thread = processor->idle_thread;
2970 	} while (new_thread == THREAD_NULL);
2971 
2972 	KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SELECT) | DBG_FUNC_END,
2973 	    (uintptr_t)thread_tid(new_thread), pset->pending_AST_URGENT_cpu_mask, delay_count, 10 + idle_reason);
2974 	return new_thread;
2975 }
2976 
2977 /*
2978  * thread_invoke
2979  *
2980  * Called at splsched with neither thread locked.
2981  *
2982  * Perform a context switch and start executing the new thread.
2983  *
2984  * Returns FALSE when the context switch didn't happen.
2985  * The reference to the new thread is still consumed.
2986  *
2987  * "self" is what is currently running on the processor,
2988  * "thread" is the new thread to context switch to
2989  * (which may be the same thread in some cases)
2990  */
2991 static boolean_t
thread_invoke(thread_t self,thread_t thread,ast_t reason)2992 thread_invoke(
2993 	thread_t                        self,
2994 	thread_t                        thread,
2995 	ast_t                           reason)
2996 {
2997 	if (__improbable(get_preemption_level() != 0)) {
2998 		int pl = get_preemption_level();
2999 		panic("thread_invoke: preemption_level %d, possible cause: %s",
3000 		    pl, (pl < 0 ? "unlocking an unlocked mutex or spinlock" :
3001 		    "blocking while holding a spinlock, or within interrupt context"));
3002 	}
3003 
3004 	thread_continue_t       continuation = self->continuation;
3005 	void                    *parameter   = self->parameter;
3006 
3007 	struct recount_snap snap = { 0 };
3008 	recount_snapshot(&snap);
3009 	uint64_t ctime = snap.rsn_time_mach;
3010 
3011 	check_monotonic_time(ctime);
3012 
3013 #ifdef CONFIG_MACH_APPROXIMATE_TIME
3014 	commpage_update_mach_approximate_time(ctime);
3015 #endif
3016 
3017 	if (ctime < thread->last_made_runnable_time) {
3018 		panic("Non-monotonic time: invoke at 0x%llx, runnable at 0x%llx",
3019 		    ctime, thread->last_made_runnable_time);
3020 	}
3021 
3022 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
3023 	if (!((thread->state & TH_IDLE) != 0 ||
3024 	    ((reason & AST_HANDOFF) && self->sched_mode == TH_MODE_REALTIME))) {
3025 		sched_timeshare_consider_maintenance(ctime, true);
3026 	}
3027 #endif
3028 
3029 	recount_log_switch_thread(&snap);
3030 
3031 	assert_thread_magic(self);
3032 	assert(self == current_thread());
3033 	assert(self->runq == PROCESSOR_NULL);
3034 	assert((self->state & (TH_RUN | TH_TERMINATE2)) == TH_RUN);
3035 
3036 	thread_lock(thread);
3037 
3038 	assert_thread_magic(thread);
3039 	assert((thread->state & (TH_RUN | TH_WAIT | TH_UNINT | TH_TERMINATE | TH_TERMINATE2)) == TH_RUN);
3040 	assert(thread->bound_processor == PROCESSOR_NULL || thread->bound_processor == current_processor());
3041 	assert(thread->runq == PROCESSOR_NULL);
3042 
3043 	/* Update SFI class based on other factors */
3044 	thread->sfi_class = sfi_thread_classify(thread);
3045 
3046 	/* Update the same_pri_latency for the thread (used by perfcontrol callouts) */
3047 	thread->same_pri_latency = ctime - thread->last_basepri_change_time;
3048 	/*
3049 	 * In case a base_pri update happened between the timestamp and
3050 	 * taking the thread lock
3051 	 */
3052 	if (ctime <= thread->last_basepri_change_time) {
3053 		thread->same_pri_latency = ctime - thread->last_made_runnable_time;
3054 	}
3055 
3056 	/* Allow realtime threads to hang onto a stack. */
3057 	if ((self->sched_mode == TH_MODE_REALTIME) && !self->reserved_stack) {
3058 		self->reserved_stack = self->kernel_stack;
3059 	}
3060 
3061 	/* Prepare for spin debugging */
3062 #if SCHED_HYGIENE_DEBUG
3063 	ml_spin_debug_clear(thread);
3064 #endif
3065 
3066 	if (continuation != NULL) {
3067 		if (!thread->kernel_stack) {
3068 			/*
3069 			 * If we are using a privileged stack,
3070 			 * check to see whether we can exchange it with
3071 			 * that of the other thread.
3072 			 */
3073 			if (self->kernel_stack == self->reserved_stack && !thread->reserved_stack) {
3074 				goto need_stack;
3075 			}
3076 
3077 			/*
3078 			 * Context switch by performing a stack handoff.
3079 			 * Requires both threads to be parked in a continuation.
3080 			 */
3081 			continuation = thread->continuation;
3082 			parameter = thread->parameter;
3083 
3084 			processor_t processor = current_processor();
3085 			processor->active_thread = thread;
3086 			processor_state_update_from_thread(processor, thread, false);
3087 
3088 			if (thread->last_processor != processor && thread->last_processor != NULL) {
3089 				if (thread->last_processor->processor_set != processor->processor_set) {
3090 					thread->ps_switch++;
3091 				}
3092 				thread->p_switch++;
3093 			}
3094 			thread->last_processor = processor;
3095 			thread->c_switch++;
3096 			ast_context(thread);
3097 
3098 			thread_unlock(thread);
3099 
3100 			self->reason = reason;
3101 
3102 			processor->last_dispatch = ctime;
3103 			self->last_run_time = ctime;
3104 			timer_update(&thread->runnable_timer, ctime);
3105 			recount_switch_thread(&snap, self, get_threadtask(self));
3106 
3107 			KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3108 			    MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_HANDOFF) | DBG_FUNC_NONE,
3109 			    self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
3110 
3111 			if ((thread->chosen_processor != processor) && (thread->chosen_processor != PROCESSOR_NULL)) {
3112 				SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT_IST(MACHDBG_CODE(DBG_MACH_SCHED, MACH_MOVED) | DBG_FUNC_NONE,
3113 				    (uintptr_t)thread_tid(thread), (uintptr_t)thread->chosen_processor->cpu_id, 0, 0, 0);
3114 			}
3115 
3116 			DTRACE_SCHED2(off__cpu, struct thread *, thread, struct proc *, current_proc());
3117 
3118 			SCHED_STATS_CSW(processor, self->reason, self->sched_pri, thread->sched_pri);
3119 
3120 #if KPERF
3121 			kperf_off_cpu(self);
3122 #endif /* KPERF */
3123 
3124 			/*
3125 			 * This is where we actually switch thread identity,
3126 			 * and address space if required.  However, register
3127 			 * state is not switched - this routine leaves the
3128 			 * stack and register state active on the current CPU.
3129 			 */
3130 			TLOG(1, "thread_invoke: calling stack_handoff\n");
3131 			stack_handoff(self, thread);
3132 
3133 			/* 'self' is now off core */
3134 			assert(thread == current_thread_volatile());
3135 
3136 			DTRACE_SCHED(on__cpu);
3137 
3138 #if KPERF
3139 			kperf_on_cpu(thread, continuation, NULL);
3140 #endif /* KPERF */
3141 
3142 			recount_log_switch_thread_on(&snap);
3143 
3144 			thread_dispatch(self, thread);
3145 
3146 #if KASAN
3147 			/* Old thread's stack has been moved to the new thread, so explicitly
3148 			 * unpoison it. */
3149 			kasan_unpoison_stack(thread->kernel_stack, kernel_stack_size);
3150 #endif
3151 
3152 			thread->continuation = thread->parameter = NULL;
3153 
3154 			boolean_t enable_interrupts = TRUE;
3155 
3156 			/* idle thread needs to stay interrupts-disabled */
3157 			if ((thread->state & TH_IDLE)) {
3158 				enable_interrupts = FALSE;
3159 			}
3160 
3161 			assert(continuation);
3162 			call_continuation(continuation, parameter,
3163 			    thread->wait_result, enable_interrupts);
3164 			/*NOTREACHED*/
3165 		} else if (thread == self) {
3166 			/* same thread but with continuation */
3167 			ast_context(self);
3168 
3169 			thread_unlock(self);
3170 
3171 #if KPERF
3172 			kperf_on_cpu(thread, continuation, NULL);
3173 #endif /* KPERF */
3174 
3175 			recount_log_switch_thread_on(&snap);
3176 
3177 			KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3178 			    MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED) | DBG_FUNC_NONE,
3179 			    self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
3180 
3181 #if KASAN
3182 			/* stack handoff to self - no thread_dispatch(), so clear the stack
3183 			 * and free the fakestack directly */
3184 #if KASAN_CLASSIC
3185 			kasan_fakestack_drop(self);
3186 			kasan_fakestack_gc(self);
3187 #endif /* KASAN_CLASSIC */
3188 			kasan_unpoison_stack(self->kernel_stack, kernel_stack_size);
3189 #endif /* KASAN */
3190 
3191 			self->continuation = self->parameter = NULL;
3192 
3193 			boolean_t enable_interrupts = TRUE;
3194 
3195 			/* idle thread needs to stay interrupts-disabled */
3196 			if ((self->state & TH_IDLE)) {
3197 				enable_interrupts = FALSE;
3198 			}
3199 
3200 			call_continuation(continuation, parameter,
3201 			    self->wait_result, enable_interrupts);
3202 			/*NOTREACHED*/
3203 		}
3204 	} else {
3205 		/*
3206 		 * Check that the other thread has a stack
3207 		 */
3208 		if (!thread->kernel_stack) {
3209 need_stack:
3210 			if (!stack_alloc_try(thread)) {
3211 				thread_unlock(thread);
3212 				thread_stack_enqueue(thread);
3213 				return FALSE;
3214 			}
3215 		} else if (thread == self) {
3216 			ast_context(self);
3217 			thread_unlock(self);
3218 
3219 			KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3220 			    MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED) | DBG_FUNC_NONE,
3221 			    self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
3222 
3223 			return TRUE;
3224 		}
3225 	}
3226 
3227 	/*
3228 	 * Context switch by full context save.
3229 	 */
3230 	processor_t processor = current_processor();
3231 	processor->active_thread = thread;
3232 	processor_state_update_from_thread(processor, thread, false);
3233 
3234 	if (thread->last_processor != processor && thread->last_processor != NULL) {
3235 		if (thread->last_processor->processor_set != processor->processor_set) {
3236 			thread->ps_switch++;
3237 		}
3238 		thread->p_switch++;
3239 	}
3240 	thread->last_processor = processor;
3241 	thread->c_switch++;
3242 	ast_context(thread);
3243 
3244 	thread_unlock(thread);
3245 
3246 	self->reason = reason;
3247 
3248 	processor->last_dispatch = ctime;
3249 	self->last_run_time = ctime;
3250 	timer_update(&thread->runnable_timer, ctime);
3251 	recount_switch_thread(&snap, self, get_threadtask(self));
3252 
3253 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3254 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED) | DBG_FUNC_NONE,
3255 	    self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
3256 
3257 	if ((thread->chosen_processor != processor) && (thread->chosen_processor != NULL)) {
3258 		SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT_IST(MACHDBG_CODE(DBG_MACH_SCHED, MACH_MOVED) | DBG_FUNC_NONE,
3259 		    (uintptr_t)thread_tid(thread), (uintptr_t)thread->chosen_processor->cpu_id, 0, 0, 0);
3260 	}
3261 
3262 	DTRACE_SCHED2(off__cpu, struct thread *, thread, struct proc *, current_proc());
3263 
3264 	SCHED_STATS_CSW(processor, self->reason, self->sched_pri, thread->sched_pri);
3265 
3266 #if KPERF
3267 	kperf_off_cpu(self);
3268 #endif /* KPERF */
3269 
3270 	/*
3271 	 * This is where we actually switch register context,
3272 	 * and address space if required.  We will next run
3273 	 * as a result of a subsequent context switch.
3274 	 *
3275 	 * Once registers are switched and the processor is running "thread",
3276 	 * the stack variables and non-volatile registers will contain whatever
3277 	 * was there the last time that thread blocked. No local variables should
3278 	 * be used after this point, except for the special case of "thread", which
3279 	 * the platform layer returns as the previous thread running on the processor
3280 	 * via the function call ABI as a return register, and "self", which may have
3281 	 * been stored on the stack or a non-volatile register, but a stale idea of
3282 	 * what was on the CPU is newly-accurate because that thread is again
3283 	 * running on the CPU.
3284 	 *
3285 	 * If one of the threads is using a continuation, thread_continue
3286 	 * is used to stitch up its context.
3287 	 *
3288 	 * If we are invoking a thread which is resuming from a continuation,
3289 	 * the CPU will invoke thread_continue next.
3290 	 *
3291 	 * If the current thread is parking in a continuation, then its state
3292 	 * won't be saved and the stack will be discarded. When the stack is
3293 	 * re-allocated, it will be configured to resume from thread_continue.
3294 	 */
3295 
3296 	assert(continuation == self->continuation);
3297 	thread = machine_switch_context(self, continuation, thread);
3298 	assert(self == current_thread_volatile());
3299 	TLOG(1, "thread_invoke: returning machine_switch_context: self %p continuation %p thread %p\n", self, continuation, thread);
3300 
3301 	assert(continuation == NULL && self->continuation == NULL);
3302 
3303 	DTRACE_SCHED(on__cpu);
3304 
3305 #if KPERF
3306 	kperf_on_cpu(self, NULL, __builtin_frame_address(0));
3307 #endif /* KPERF */
3308 
3309 	/* Previous snap on the old stack is gone. */
3310 	recount_log_switch_thread_on(NULL);
3311 
3312 	/* We have been resumed and are set to run. */
3313 	thread_dispatch(thread, self);
3314 
3315 	return TRUE;
3316 }
3317 
3318 #if defined(CONFIG_SCHED_DEFERRED_AST)
3319 /*
3320  *	pset_cancel_deferred_dispatch:
3321  *
3322  *	Cancels all ASTs that we can cancel for the given processor set
3323  *	if the current processor is running the last runnable thread in the
3324  *	system.
3325  *
3326  *	This function assumes the current thread is runnable.  This must
3327  *	be called with the pset unlocked.
3328  */
3329 static void
pset_cancel_deferred_dispatch(processor_set_t pset,processor_t processor)3330 pset_cancel_deferred_dispatch(
3331 	processor_set_t         pset,
3332 	processor_t             processor)
3333 {
3334 	processor_t             active_processor = NULL;
3335 	uint32_t                sampled_sched_run_count;
3336 
3337 	pset_lock(pset);
3338 	sampled_sched_run_count = os_atomic_load(&sched_run_buckets[TH_BUCKET_RUN], relaxed);
3339 
3340 	/*
3341 	 * If we have emptied the run queue, and our current thread is runnable, we
3342 	 * should tell any processors that are still DISPATCHING that they will
3343 	 * probably not have any work to do.  In the event that there are no
3344 	 * pending signals that we can cancel, this is also uninteresting.
3345 	 *
3346 	 * In the unlikely event that another thread becomes runnable while we are
3347 	 * doing this (sched_run_count is atomically updated, not guarded), the
3348 	 * codepath making it runnable SHOULD (a dangerous word) need the pset lock
3349 	 * in order to dispatch it to a processor in our pset.  So, the other
3350 	 * codepath will wait while we squash all cancelable ASTs, get the pset
3351 	 * lock, and then dispatch the freshly runnable thread.  So this should be
3352 	 * correct (we won't accidentally have a runnable thread that hasn't been
3353 	 * dispatched to an idle processor), if not ideal (we may be restarting the
3354 	 * dispatch process, which could have some overhead).
3355 	 */
3356 
3357 	if ((sampled_sched_run_count == 1) && (pset->pending_deferred_AST_cpu_mask)) {
3358 		uint64_t dispatching_map = (pset->cpu_state_map[PROCESSOR_DISPATCHING] &
3359 		    pset->pending_deferred_AST_cpu_mask &
3360 		    ~pset->pending_AST_URGENT_cpu_mask);
3361 		for (int cpuid = lsb_first(dispatching_map); cpuid >= 0; cpuid = lsb_next(dispatching_map, cpuid)) {
3362 			active_processor = processor_array[cpuid];
3363 			/*
3364 			 * If a processor is DISPATCHING, it could be because of
3365 			 * a cancelable signal.
3366 			 *
3367 			 * IF the processor is not our
3368 			 * current processor (the current processor should not
3369 			 * be DISPATCHING, so this is a bit paranoid), AND there
3370 			 * is a cancelable signal pending on the processor, AND
3371 			 * there is no non-cancelable signal pending (as there is
3372 			 * no point trying to backtrack on bringing the processor
3373 			 * up if a signal we cannot cancel is outstanding), THEN
3374 			 * it should make sense to roll back the processor state
3375 			 * to the IDLE state.
3376 			 *
3377 			 * If the racey nature of this approach (as the signal
3378 			 * will be arbitrated by hardware, and can fire as we
3379 			 * roll back state) results in the core responding
3380 			 * despite being pushed back to the IDLE state, it
3381 			 * should be no different than if the core took some
3382 			 * interrupt while IDLE.
3383 			 */
3384 			if (active_processor != processor) {
3385 				/*
3386 				 * Squash all of the processor state back to some
3387 				 * reasonable facsimile of PROCESSOR_IDLE.
3388 				 */
3389 
3390 				processor_state_update_idle(active_processor);
3391 				active_processor->deadline = RT_DEADLINE_NONE;
3392 				pset_update_processor_state(pset, active_processor, PROCESSOR_IDLE);
3393 				bit_clear(pset->pending_deferred_AST_cpu_mask, active_processor->cpu_id);
3394 				machine_signal_idle_cancel(active_processor);
3395 			}
3396 		}
3397 	}
3398 
3399 	pset_unlock(pset);
3400 }
3401 #else
3402 /* We don't support deferred ASTs; everything is candycanes and sunshine. */
3403 #endif
3404 
3405 static void
thread_csw_callout(thread_t old,thread_t new,uint64_t timestamp)3406 thread_csw_callout(
3407 	thread_t            old,
3408 	thread_t            new,
3409 	uint64_t            timestamp)
3410 {
3411 	perfcontrol_event event = (new->state & TH_IDLE) ? IDLE : CONTEXT_SWITCH;
3412 	uint64_t same_pri_latency = (new->state & TH_IDLE) ? 0 : new->same_pri_latency;
3413 	machine_switch_perfcontrol_context(event, timestamp, 0,
3414 	    same_pri_latency, old, new);
3415 }
3416 
3417 
3418 /*
3419  *	thread_dispatch:
3420  *
3421  *	Handle threads at context switch.  Re-dispatch other thread
3422  *	if still running, otherwise update run state and perform
3423  *	special actions.  Update quantum for other thread and begin
3424  *	the quantum for ourselves.
3425  *
3426  *      "thread" is the old thread that we have switched away from.
3427  *      "self" is the new current thread that we have context switched to
3428  *
3429  *	Called at splsched.
3430  *
3431  */
3432 void
thread_dispatch(thread_t thread,thread_t self)3433 thread_dispatch(
3434 	thread_t                thread,
3435 	thread_t                self)
3436 {
3437 	processor_t             processor = self->last_processor;
3438 	bool was_idle = false;
3439 
3440 	assert(processor == current_processor());
3441 	assert(self == current_thread_volatile());
3442 	assert(thread != self);
3443 
3444 	if (thread != THREAD_NULL) {
3445 		/*
3446 		 * Do the perfcontrol callout for context switch.
3447 		 * The reason we do this here is:
3448 		 * - thread_dispatch() is called from various places that are not
3449 		 *   the direct context switch path for eg. processor shutdown etc.
3450 		 *   So adding the callout here covers all those cases.
3451 		 * - We want this callout as early as possible to be close
3452 		 *   to the timestamp taken in thread_invoke()
3453 		 * - We want to avoid holding the thread lock while doing the
3454 		 *   callout
3455 		 * - We do not want to callout if "thread" is NULL.
3456 		 */
3457 		thread_csw_callout(thread, self, processor->last_dispatch);
3458 
3459 #if KASAN
3460 		if (thread->continuation != NULL) {
3461 			/*
3462 			 * Thread has a continuation and the normal stack is going away.
3463 			 * Unpoison the stack and mark all fakestack objects as unused.
3464 			 */
3465 #if KASAN_CLASSIC
3466 			kasan_fakestack_drop(thread);
3467 #endif /* KASAN_CLASSIC */
3468 			if (thread->kernel_stack) {
3469 				kasan_unpoison_stack(thread->kernel_stack, kernel_stack_size);
3470 			}
3471 		}
3472 
3473 
3474 #if KASAN_CLASSIC
3475 		/*
3476 		 * Free all unused fakestack objects.
3477 		 */
3478 		kasan_fakestack_gc(thread);
3479 #endif /* KASAN_CLASSIC */
3480 #endif /* KASAN */
3481 
3482 		/*
3483 		 *	If blocked at a continuation, discard
3484 		 *	the stack.
3485 		 */
3486 		if (thread->continuation != NULL && thread->kernel_stack != 0) {
3487 			stack_free(thread);
3488 		}
3489 
3490 		if (thread->state & TH_IDLE) {
3491 			was_idle = true;
3492 			KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3493 			    MACHDBG_CODE(DBG_MACH_SCHED, MACH_DISPATCH) | DBG_FUNC_NONE,
3494 			    (uintptr_t)thread_tid(thread), 0, thread->state,
3495 			    sched_run_buckets[TH_BUCKET_RUN], 0);
3496 		} else {
3497 			int64_t consumed;
3498 			int64_t remainder = 0;
3499 
3500 			if (processor->quantum_end > processor->last_dispatch) {
3501 				remainder = processor->quantum_end -
3502 				    processor->last_dispatch;
3503 			}
3504 
3505 			consumed = thread->quantum_remaining - remainder;
3506 
3507 			if ((thread->reason & AST_LEDGER) == 0) {
3508 				/*
3509 				 * Bill CPU time to both the task and
3510 				 * the individual thread.
3511 				 */
3512 				ledger_credit_thread(thread, thread->t_ledger,
3513 				    task_ledgers.cpu_time, consumed);
3514 				ledger_credit_thread(thread, thread->t_threadledger,
3515 				    thread_ledgers.cpu_time, consumed);
3516 				if (thread->t_bankledger) {
3517 					ledger_credit_thread(thread, thread->t_bankledger,
3518 					    bank_ledgers.cpu_time,
3519 					    (consumed - thread->t_deduct_bank_ledger_time));
3520 				}
3521 				thread->t_deduct_bank_ledger_time = 0;
3522 				if (consumed > 0) {
3523 					/*
3524 					 * This should never be negative, but in traces we are seeing some instances
3525 					 * of consumed being negative.
3526 					 * <rdar://problem/57782596> thread_dispatch() thread CPU consumed calculation sometimes results in negative value
3527 					 */
3528 					sched_update_pset_avg_execution_time(current_processor()->processor_set, consumed, processor->last_dispatch, thread->th_sched_bucket);
3529 				}
3530 			}
3531 
3532 			/* For the thread that we just context switched away from, figure
3533 			 * out if we have expired the wq quantum and set the AST if we have
3534 			 */
3535 			if (thread_get_tag(thread) & THREAD_TAG_WORKQUEUE) {
3536 				thread_evaluate_workqueue_quantum_expiry(thread);
3537 			}
3538 
3539 			if (__improbable(thread->rwlock_count != 0)) {
3540 				smr_mark_active_trackers_stalled(thread);
3541 			}
3542 
3543 			/*
3544 			 * Pairs with task_restartable_ranges_synchronize
3545 			 */
3546 			wake_lock(thread);
3547 			thread_lock(thread);
3548 
3549 			/*
3550 			 * Same as ast_check(), in case we missed the IPI
3551 			 */
3552 			thread_reset_pcs_ack_IPI(thread);
3553 
3554 			/*
3555 			 * Apply a priority floor if the thread holds a kernel resource
3556 			 * or explicitly requested it.
3557 			 * Do this before checking starting_pri to avoid overpenalizing
3558 			 * repeated rwlock blockers.
3559 			 */
3560 			if (__improbable(thread->rwlock_count != 0)) {
3561 				lck_rw_set_promotion_locked(thread);
3562 			}
3563 			if (__improbable(thread->priority_floor_count != 0)) {
3564 				thread_floor_boost_set_promotion_locked(thread);
3565 			}
3566 
3567 			boolean_t keep_quantum = processor->first_timeslice;
3568 
3569 			/*
3570 			 * Treat a thread which has dropped priority since it got on core
3571 			 * as having expired its quantum.
3572 			 */
3573 			if (processor->starting_pri > thread->sched_pri) {
3574 				keep_quantum = FALSE;
3575 			}
3576 
3577 			/* Compute remainder of current quantum. */
3578 			if (keep_quantum &&
3579 			    processor->quantum_end > processor->last_dispatch) {
3580 				thread->quantum_remaining = (uint32_t)remainder;
3581 			} else {
3582 				thread->quantum_remaining = 0;
3583 			}
3584 
3585 			if (thread->sched_mode == TH_MODE_REALTIME) {
3586 				/*
3587 				 *	Cancel the deadline if the thread has
3588 				 *	consumed the entire quantum.
3589 				 */
3590 				if (thread->quantum_remaining == 0) {
3591 					KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_CANCEL_RT_DEADLINE) | DBG_FUNC_NONE,
3592 					    (uintptr_t)thread_tid(thread), thread->realtime.deadline, thread->realtime.computation, 0);
3593 					thread->realtime.deadline = RT_DEADLINE_QUANTUM_EXPIRED;
3594 				}
3595 			} else {
3596 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
3597 				/*
3598 				 *	For non-realtime threads treat a tiny
3599 				 *	remaining quantum as an expired quantum
3600 				 *	but include what's left next time.
3601 				 */
3602 				if (thread->quantum_remaining < min_std_quantum) {
3603 					thread->reason |= AST_QUANTUM;
3604 					thread->quantum_remaining += SCHED(initial_quantum_size)(thread);
3605 				}
3606 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
3607 			}
3608 
3609 			/*
3610 			 *	If we are doing a direct handoff then
3611 			 *	take the remainder of the quantum.
3612 			 */
3613 			if ((thread->reason & (AST_HANDOFF | AST_QUANTUM)) == AST_HANDOFF) {
3614 				self->quantum_remaining = thread->quantum_remaining;
3615 				thread->reason |= AST_QUANTUM;
3616 				thread->quantum_remaining = 0;
3617 			} else {
3618 #if defined(CONFIG_SCHED_MULTIQ)
3619 				if (SCHED(sched_groups_enabled) &&
3620 				    thread->sched_group == self->sched_group) {
3621 					KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3622 					    MACHDBG_CODE(DBG_MACH_SCHED, MACH_QUANTUM_HANDOFF),
3623 					    self->reason, (uintptr_t)thread_tid(thread),
3624 					    self->quantum_remaining, thread->quantum_remaining, 0);
3625 
3626 					self->quantum_remaining = thread->quantum_remaining;
3627 					thread->quantum_remaining = 0;
3628 					/* Don't set AST_QUANTUM here - old thread might still want to preempt someone else */
3629 				}
3630 #endif /* defined(CONFIG_SCHED_MULTIQ) */
3631 			}
3632 
3633 			thread->computation_metered += (processor->last_dispatch - thread->computation_epoch);
3634 
3635 			if (!(thread->state & TH_WAIT)) {
3636 				/*
3637 				 *	Still runnable.
3638 				 */
3639 				thread->last_made_runnable_time = thread->last_basepri_change_time = processor->last_dispatch;
3640 
3641 				machine_thread_going_off_core(thread, FALSE, processor->last_dispatch, TRUE);
3642 
3643 				ast_t reason = thread->reason;
3644 				sched_options_t options = SCHED_NONE;
3645 
3646 				if (reason & AST_REBALANCE) {
3647 					options |= SCHED_REBALANCE;
3648 					if (reason & AST_QUANTUM) {
3649 						/*
3650 						 * Having gone to the trouble of forcing this thread off a less preferred core,
3651 						 * we should force the preferable core to reschedule immediately to give this
3652 						 * thread a chance to run instead of just sitting on the run queue where
3653 						 * it may just be stolen back by the idle core we just forced it off.
3654 						 * But only do this at the end of a quantum to prevent cascading effects.
3655 						 */
3656 						options |= SCHED_PREEMPT;
3657 					}
3658 				}
3659 
3660 				if (reason & AST_QUANTUM) {
3661 					options |= SCHED_TAILQ;
3662 				} else if (reason & AST_PREEMPT) {
3663 					options |= SCHED_HEADQ;
3664 				} else {
3665 					options |= (SCHED_PREEMPT | SCHED_TAILQ);
3666 				}
3667 
3668 				thread_setrun(thread, options);
3669 
3670 				KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3671 				    MACHDBG_CODE(DBG_MACH_SCHED, MACH_DISPATCH) | DBG_FUNC_NONE,
3672 				    (uintptr_t)thread_tid(thread), thread->reason, thread->state,
3673 				    sched_run_buckets[TH_BUCKET_RUN], 0);
3674 
3675 				if (thread->wake_active) {
3676 					thread->wake_active = FALSE;
3677 					thread_unlock(thread);
3678 
3679 					thread_wakeup(&thread->wake_active);
3680 				} else {
3681 					thread_unlock(thread);
3682 				}
3683 
3684 				wake_unlock(thread);
3685 			} else {
3686 				/*
3687 				 *	Waiting.
3688 				 */
3689 				boolean_t should_terminate = FALSE;
3690 				uint32_t new_run_count;
3691 				int thread_state = thread->state;
3692 
3693 				/* Only the first call to thread_dispatch
3694 				 * after explicit termination should add
3695 				 * the thread to the termination queue
3696 				 */
3697 				if ((thread_state & (TH_TERMINATE | TH_TERMINATE2)) == TH_TERMINATE) {
3698 					should_terminate = TRUE;
3699 					thread_state |= TH_TERMINATE2;
3700 				}
3701 
3702 				timer_stop(&thread->runnable_timer, processor->last_dispatch);
3703 
3704 				thread_state &= ~TH_RUN;
3705 				thread->state = thread_state;
3706 
3707 				thread->last_made_runnable_time = thread->last_basepri_change_time = THREAD_NOT_RUNNABLE;
3708 				thread->chosen_processor = PROCESSOR_NULL;
3709 
3710 				new_run_count = SCHED(run_count_decr)(thread);
3711 
3712 #if CONFIG_SCHED_AUTO_JOIN
3713 				if ((thread->sched_flags & TH_SFLAG_THREAD_GROUP_AUTO_JOIN) != 0) {
3714 					work_interval_auto_join_unwind(thread);
3715 				}
3716 #endif /* CONFIG_SCHED_AUTO_JOIN */
3717 
3718 #if CONFIG_SCHED_SFI
3719 				if (thread->reason & AST_SFI) {
3720 					thread->wait_sfi_begin_time = processor->last_dispatch;
3721 				}
3722 #endif
3723 				machine_thread_going_off_core(thread, should_terminate, processor->last_dispatch, FALSE);
3724 
3725 				KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3726 				    MACHDBG_CODE(DBG_MACH_SCHED, MACH_DISPATCH) | DBG_FUNC_NONE,
3727 				    (uintptr_t)thread_tid(thread), thread->reason, thread_state,
3728 				    new_run_count, 0);
3729 
3730 				if (thread_state & TH_WAIT_REPORT) {
3731 					(*thread->sched_call)(SCHED_CALL_BLOCK, thread);
3732 				}
3733 
3734 				if (thread->wake_active) {
3735 					thread->wake_active = FALSE;
3736 					thread_unlock(thread);
3737 
3738 					thread_wakeup(&thread->wake_active);
3739 				} else {
3740 					thread_unlock(thread);
3741 				}
3742 
3743 				wake_unlock(thread);
3744 
3745 				if (should_terminate) {
3746 					thread_terminate_enqueue(thread);
3747 				}
3748 			}
3749 		}
3750 		/*
3751 		 * The thread could have been added to the termination queue, so it's
3752 		 * unsafe to use after this point.
3753 		 */
3754 		thread = THREAD_NULL;
3755 	}
3756 
3757 	int urgency = THREAD_URGENCY_NONE;
3758 	uint64_t latency = 0;
3759 
3760 	/* Update (new) current thread and reprogram running timers */
3761 	thread_lock(self);
3762 
3763 	if (!(self->state & TH_IDLE)) {
3764 		uint64_t        arg1, arg2;
3765 
3766 #if CONFIG_SCHED_SFI
3767 		ast_t                   new_ast;
3768 
3769 		new_ast = sfi_thread_needs_ast(self, NULL);
3770 
3771 		if (new_ast != AST_NONE) {
3772 			ast_on(new_ast);
3773 		}
3774 #endif
3775 
3776 		if (processor->last_dispatch < self->last_made_runnable_time) {
3777 			panic("Non-monotonic time: dispatch at 0x%llx, runnable at 0x%llx",
3778 			    processor->last_dispatch, self->last_made_runnable_time);
3779 		}
3780 
3781 		assert(self->last_made_runnable_time <= self->last_basepri_change_time);
3782 
3783 		latency = processor->last_dispatch - self->last_made_runnable_time;
3784 		assert(latency >= self->same_pri_latency);
3785 
3786 		urgency = thread_get_urgency(self, &arg1, &arg2);
3787 
3788 		thread_tell_urgency(urgency, arg1, arg2, latency, self);
3789 
3790 		/*
3791 		 *	Start a new CPU limit interval if the previous one has
3792 		 *	expired. This should happen before initializing a new
3793 		 *	quantum.
3794 		 */
3795 		if (cpulimit_affects_quantum &&
3796 		    thread_cpulimit_interval_has_expired(processor->last_dispatch)) {
3797 			thread_cpulimit_restart(processor->last_dispatch);
3798 		}
3799 
3800 		/*
3801 		 *	Get a new quantum if none remaining.
3802 		 */
3803 		if (self->quantum_remaining == 0) {
3804 			thread_quantum_init(self, processor->last_dispatch);
3805 		}
3806 
3807 		/*
3808 		 *	Set up quantum timer and timeslice.
3809 		 */
3810 		processor->quantum_end = processor->last_dispatch +
3811 		    self->quantum_remaining;
3812 
3813 		running_timer_setup(processor, RUNNING_TIMER_QUANTUM, self,
3814 		    processor->quantum_end, processor->last_dispatch);
3815 		if (was_idle) {
3816 			/*
3817 			 * kperf's running timer is active whenever the idle thread for a
3818 			 * CPU is not running.
3819 			 */
3820 			kperf_running_setup(processor, processor->last_dispatch);
3821 		}
3822 		running_timers_activate(processor);
3823 		processor->first_timeslice = TRUE;
3824 	} else {
3825 		running_timers_deactivate(processor);
3826 		processor->first_timeslice = FALSE;
3827 		thread_tell_urgency(THREAD_URGENCY_NONE, 0, 0, 0, self);
3828 	}
3829 
3830 	assert(self->block_hint == kThreadWaitNone);
3831 	self->computation_epoch = processor->last_dispatch;
3832 	self->reason = AST_NONE;
3833 	processor->starting_pri = self->sched_pri;
3834 
3835 	thread_unlock(self);
3836 
3837 	machine_thread_going_on_core(self, urgency, latency, self->same_pri_latency,
3838 	    processor->last_dispatch);
3839 
3840 #if defined(CONFIG_SCHED_DEFERRED_AST)
3841 	/*
3842 	 * TODO: Can we state that redispatching our old thread is also
3843 	 * uninteresting?
3844 	 */
3845 	if ((os_atomic_load(&sched_run_buckets[TH_BUCKET_RUN], relaxed) == 1) && !(self->state & TH_IDLE)) {
3846 		pset_cancel_deferred_dispatch(processor->processor_set, processor);
3847 	}
3848 #endif
3849 }
3850 
3851 /*
3852  *	thread_block_reason:
3853  *
3854  *	Forces a reschedule, blocking the caller if a wait
3855  *	has been asserted.
3856  *
3857  *	If a continuation is specified, then thread_invoke will
3858  *	attempt to discard the thread's kernel stack.  When the
3859  *	thread resumes, it will execute the continuation function
3860  *	on a new kernel stack.
3861  */
3862 wait_result_t
thread_block_reason(thread_continue_t continuation,void * parameter,ast_t reason)3863 thread_block_reason(
3864 	thread_continue_t       continuation,
3865 	void                            *parameter,
3866 	ast_t                           reason)
3867 {
3868 	thread_t        self = current_thread();
3869 	processor_t     processor;
3870 	thread_t        new_thread;
3871 	spl_t           s;
3872 
3873 	s = splsched();
3874 
3875 	processor = current_processor();
3876 
3877 	/* If we're explicitly yielding, force a subsequent quantum */
3878 	if (reason & AST_YIELD) {
3879 		processor->first_timeslice = FALSE;
3880 	}
3881 
3882 	/* We're handling all scheduling AST's */
3883 	ast_off(AST_SCHEDULING);
3884 
3885 	clear_pending_nonurgent_preemption(processor);
3886 
3887 #if PROC_REF_DEBUG
3888 	if ((continuation != NULL) && (get_threadtask(self) != kernel_task)) {
3889 		uthread_assert_zero_proc_refcount(get_bsdthread_info(self));
3890 	}
3891 #endif
3892 
3893 	self->continuation = continuation;
3894 	self->parameter = parameter;
3895 
3896 	if (self->state & ~(TH_RUN | TH_IDLE)) {
3897 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3898 		    MACHDBG_CODE(DBG_MACH_SCHED, MACH_BLOCK),
3899 		    reason, VM_KERNEL_UNSLIDE(continuation), 0, 0, 0);
3900 	}
3901 
3902 	do {
3903 		thread_lock(self);
3904 		new_thread = thread_select(self, processor, &reason);
3905 		thread_unlock(self);
3906 	} while (!thread_invoke(self, new_thread, reason));
3907 
3908 	splx(s);
3909 
3910 	return self->wait_result;
3911 }
3912 
3913 /*
3914  *	thread_block:
3915  *
3916  *	Block the current thread if a wait has been asserted.
3917  */
3918 wait_result_t
thread_block(thread_continue_t continuation)3919 thread_block(
3920 	thread_continue_t       continuation)
3921 {
3922 	return thread_block_reason(continuation, NULL, AST_NONE);
3923 }
3924 
3925 wait_result_t
thread_block_parameter(thread_continue_t continuation,void * parameter)3926 thread_block_parameter(
3927 	thread_continue_t       continuation,
3928 	void                            *parameter)
3929 {
3930 	return thread_block_reason(continuation, parameter, AST_NONE);
3931 }
3932 
3933 /*
3934  *	thread_run:
3935  *
3936  *	Switch directly from the current thread to the
3937  *	new thread, handing off our quantum if appropriate.
3938  *
3939  *	New thread must be runnable, and not on a run queue.
3940  *
3941  *	Called at splsched.
3942  */
3943 int
thread_run(thread_t self,thread_continue_t continuation,void * parameter,thread_t new_thread)3944 thread_run(
3945 	thread_t                        self,
3946 	thread_continue_t       continuation,
3947 	void                            *parameter,
3948 	thread_t                        new_thread)
3949 {
3950 	ast_t reason = AST_NONE;
3951 
3952 	if ((self->state & TH_IDLE) == 0) {
3953 		reason = AST_HANDOFF;
3954 	}
3955 
3956 	/*
3957 	 * If this thread hadn't been setrun'ed, it
3958 	 * might not have a chosen processor, so give it one
3959 	 */
3960 	if (new_thread->chosen_processor == NULL) {
3961 		new_thread->chosen_processor = current_processor();
3962 	}
3963 
3964 	self->continuation = continuation;
3965 	self->parameter = parameter;
3966 
3967 	while (!thread_invoke(self, new_thread, reason)) {
3968 		/* the handoff failed, so we have to fall back to the normal block path */
3969 		processor_t processor = current_processor();
3970 
3971 		reason = AST_NONE;
3972 
3973 		thread_lock(self);
3974 		new_thread = thread_select(self, processor, &reason);
3975 		thread_unlock(self);
3976 	}
3977 
3978 	return self->wait_result;
3979 }
3980 
3981 /*
3982  *	thread_continue:
3983  *
3984  *	Called at splsched when a thread first receives
3985  *	a new stack after a continuation.
3986  *
3987  *	Called with THREAD_NULL as the old thread when
3988  *	invoked by machine_load_context.
3989  */
3990 void
thread_continue(thread_t thread)3991 thread_continue(
3992 	thread_t        thread)
3993 {
3994 	thread_t                self = current_thread();
3995 	thread_continue_t       continuation;
3996 	void                    *parameter;
3997 
3998 	DTRACE_SCHED(on__cpu);
3999 
4000 	continuation = self->continuation;
4001 	parameter = self->parameter;
4002 
4003 	assert(continuation != NULL);
4004 
4005 #if KPERF
4006 	kperf_on_cpu(self, continuation, NULL);
4007 #endif
4008 
4009 	thread_dispatch(thread, self);
4010 
4011 	self->continuation = self->parameter = NULL;
4012 
4013 #if SCHED_HYGIENE_DEBUG
4014 	/* Reset interrupt-masked spin debugging timeout */
4015 	ml_spin_debug_clear(self);
4016 #endif
4017 
4018 	TLOG(1, "thread_continue: calling call_continuation\n");
4019 
4020 	boolean_t enable_interrupts = TRUE;
4021 
4022 	/* bootstrap thread, idle thread need to stay interrupts-disabled */
4023 	if (thread == THREAD_NULL || (self->state & TH_IDLE)) {
4024 		enable_interrupts = FALSE;
4025 	}
4026 
4027 #if KASAN_TBI
4028 	kasan_unpoison_stack(self->kernel_stack, kernel_stack_size);
4029 #endif /* KASAN_TBI */
4030 
4031 
4032 	call_continuation(continuation, parameter, self->wait_result, enable_interrupts);
4033 	/*NOTREACHED*/
4034 }
4035 
4036 void
thread_quantum_init(thread_t thread,uint64_t now)4037 thread_quantum_init(thread_t thread, uint64_t now)
4038 {
4039 	uint64_t new_quantum = 0;
4040 
4041 	switch (thread->sched_mode) {
4042 	case TH_MODE_REALTIME:
4043 		new_quantum = thread->realtime.computation;
4044 		new_quantum = MIN(new_quantum, max_unsafe_rt_computation);
4045 		break;
4046 
4047 	case TH_MODE_FIXED:
4048 		new_quantum = SCHED(initial_quantum_size)(thread);
4049 		new_quantum = MIN(new_quantum, max_unsafe_fixed_computation);
4050 		break;
4051 
4052 	default:
4053 		new_quantum = SCHED(initial_quantum_size)(thread);
4054 		break;
4055 	}
4056 
4057 	if (cpulimit_affects_quantum) {
4058 		const uint64_t cpulimit_remaining = thread_cpulimit_remaining(now);
4059 
4060 		/*
4061 		 * If there's no remaining CPU time, the ledger system will
4062 		 * notice and put the thread to sleep.
4063 		 */
4064 		if (cpulimit_remaining > 0) {
4065 			new_quantum = MIN(new_quantum, cpulimit_remaining);
4066 		}
4067 	}
4068 
4069 	assert3u(new_quantum, <, UINT32_MAX);
4070 	assert3u(new_quantum, >, 0);
4071 
4072 	thread->quantum_remaining = (uint32_t)new_quantum;
4073 }
4074 
4075 uint32_t
sched_timeshare_initial_quantum_size(thread_t thread)4076 sched_timeshare_initial_quantum_size(thread_t thread)
4077 {
4078 	if ((thread != THREAD_NULL) && thread->th_sched_bucket == TH_BUCKET_SHARE_BG) {
4079 		return bg_quantum;
4080 	} else {
4081 		return std_quantum;
4082 	}
4083 }
4084 
4085 /*
4086  *	run_queue_init:
4087  *
4088  *	Initialize a run queue before first use.
4089  */
4090 void
run_queue_init(run_queue_t rq)4091 run_queue_init(
4092 	run_queue_t             rq)
4093 {
4094 	rq->highq = NOPRI;
4095 	for (u_int i = 0; i < BITMAP_LEN(NRQS); i++) {
4096 		rq->bitmap[i] = 0;
4097 	}
4098 	rq->urgency = rq->count = 0;
4099 	for (int i = 0; i < NRQS; i++) {
4100 		circle_queue_init(&rq->queues[i]);
4101 	}
4102 }
4103 
4104 /*
4105  *	run_queue_dequeue:
4106  *
4107  *	Perform a dequeue operation on a run queue,
4108  *	and return the resulting thread.
4109  *
4110  *	The run queue must be locked (see thread_run_queue_remove()
4111  *	for more info), and not empty.
4112  */
4113 thread_t
run_queue_dequeue(run_queue_t rq,sched_options_t options)4114 run_queue_dequeue(
4115 	run_queue_t     rq,
4116 	sched_options_t options)
4117 {
4118 	thread_t        thread;
4119 	circle_queue_t  queue = &rq->queues[rq->highq];
4120 
4121 	if (options & SCHED_HEADQ) {
4122 		thread = cqe_dequeue_head(queue, struct thread, runq_links);
4123 	} else {
4124 		thread = cqe_dequeue_tail(queue, struct thread, runq_links);
4125 	}
4126 
4127 	assert(thread != THREAD_NULL);
4128 	assert_thread_magic(thread);
4129 
4130 	thread->runq = PROCESSOR_NULL;
4131 	SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count);
4132 	rq->count--;
4133 	if (SCHED(priority_is_urgent)(rq->highq)) {
4134 		rq->urgency--; assert(rq->urgency >= 0);
4135 	}
4136 	if (circle_queue_empty(queue)) {
4137 		bitmap_clear(rq->bitmap, rq->highq);
4138 		rq->highq = bitmap_first(rq->bitmap, NRQS);
4139 	}
4140 
4141 	return thread;
4142 }
4143 
4144 /*
4145  *	run_queue_enqueue:
4146  *
4147  *	Perform a enqueue operation on a run queue.
4148  *
4149  *	The run queue must be locked (see thread_run_queue_remove()
4150  *	for more info).
4151  */
4152 boolean_t
run_queue_enqueue(run_queue_t rq,thread_t thread,sched_options_t options)4153 run_queue_enqueue(
4154 	run_queue_t      rq,
4155 	thread_t         thread,
4156 	sched_options_t  options)
4157 {
4158 	circle_queue_t  queue = &rq->queues[thread->sched_pri];
4159 	boolean_t       result = FALSE;
4160 
4161 	assert_thread_magic(thread);
4162 
4163 	if (circle_queue_empty(queue)) {
4164 		circle_enqueue_tail(queue, &thread->runq_links);
4165 
4166 		rq_bitmap_set(rq->bitmap, thread->sched_pri);
4167 		if (thread->sched_pri > rq->highq) {
4168 			rq->highq = thread->sched_pri;
4169 			result = TRUE;
4170 		}
4171 	} else {
4172 		if (options & SCHED_TAILQ) {
4173 			circle_enqueue_tail(queue, &thread->runq_links);
4174 		} else {
4175 			circle_enqueue_head(queue, &thread->runq_links);
4176 		}
4177 	}
4178 	if (SCHED(priority_is_urgent)(thread->sched_pri)) {
4179 		rq->urgency++;
4180 	}
4181 	SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count);
4182 	rq->count++;
4183 
4184 	return result;
4185 }
4186 
4187 /*
4188  *	run_queue_remove:
4189  *
4190  *	Remove a specific thread from a runqueue.
4191  *
4192  *	The run queue must be locked.
4193  */
4194 void
run_queue_remove(run_queue_t rq,thread_t thread)4195 run_queue_remove(
4196 	run_queue_t    rq,
4197 	thread_t       thread)
4198 {
4199 	circle_queue_t  queue = &rq->queues[thread->sched_pri];
4200 
4201 	assert(thread->runq != PROCESSOR_NULL);
4202 	assert_thread_magic(thread);
4203 
4204 	circle_dequeue(queue, &thread->runq_links);
4205 	SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count);
4206 	rq->count--;
4207 	if (SCHED(priority_is_urgent)(thread->sched_pri)) {
4208 		rq->urgency--; assert(rq->urgency >= 0);
4209 	}
4210 
4211 	if (circle_queue_empty(queue)) {
4212 		/* update run queue status */
4213 		bitmap_clear(rq->bitmap, thread->sched_pri);
4214 		rq->highq = bitmap_first(rq->bitmap, NRQS);
4215 	}
4216 
4217 	thread->runq = PROCESSOR_NULL;
4218 }
4219 
4220 /*
4221  *      run_queue_peek
4222  *
4223  *      Peek at the runq and return the highest
4224  *      priority thread from the runq.
4225  *
4226  *	The run queue must be locked.
4227  */
4228 thread_t
run_queue_peek(run_queue_t rq)4229 run_queue_peek(
4230 	run_queue_t    rq)
4231 {
4232 	if (rq->count > 0) {
4233 		circle_queue_t queue = &rq->queues[rq->highq];
4234 		thread_t thread = cqe_queue_first(queue, struct thread, runq_links);
4235 		assert_thread_magic(thread);
4236 		return thread;
4237 	} else {
4238 		return THREAD_NULL;
4239 	}
4240 }
4241 
4242 static bool
rt_runq_enqueue(rt_queue_t rt_run_queue,thread_t thread,processor_t processor)4243 rt_runq_enqueue(rt_queue_t rt_run_queue, thread_t thread, processor_t processor)
4244 {
4245 	int pri = thread->sched_pri;
4246 	assert((pri >= BASEPRI_RTQUEUES) && (pri <= MAXPRI));
4247 	int i = pri - BASEPRI_RTQUEUES;
4248 	rt_queue_pri_t *rt_runq = &rt_run_queue->rt_queue_pri[i];
4249 	bitmap_t *map = rt_run_queue->bitmap;
4250 
4251 	bitmap_set(map, i);
4252 
4253 	queue_t     queue       = &rt_runq->pri_queue;
4254 	uint64_t    deadline    = thread->realtime.deadline;
4255 	bool        preempt     = false;
4256 	bool        earliest    = false;
4257 
4258 	if (queue_empty(queue)) {
4259 		enqueue_tail(queue, &thread->runq_links);
4260 		preempt = true;
4261 		earliest = true;
4262 		rt_runq->pri_earliest_deadline = deadline;
4263 		rt_runq->pri_constraint = thread->realtime.constraint;
4264 	} else {
4265 		/* Insert into rt_runq in thread deadline order */
4266 		queue_entry_t iter;
4267 		qe_foreach(iter, queue) {
4268 			thread_t iter_thread = qe_element(iter, struct thread, runq_links);
4269 			assert_thread_magic(iter_thread);
4270 
4271 			if (deadline < iter_thread->realtime.deadline) {
4272 				if (iter == queue_first(queue)) {
4273 					preempt = true;
4274 					earliest = true;
4275 					rt_runq->pri_earliest_deadline = deadline;
4276 					rt_runq->pri_constraint = thread->realtime.constraint;
4277 				}
4278 				insque(&thread->runq_links, queue_prev(iter));
4279 				break;
4280 			} else if (iter == queue_last(queue)) {
4281 				enqueue_tail(queue, &thread->runq_links);
4282 				break;
4283 			}
4284 		}
4285 	}
4286 	if (earliest && (deadline < os_atomic_load_wide(&rt_run_queue->earliest_deadline, relaxed))) {
4287 		os_atomic_store_wide(&rt_run_queue->earliest_deadline, deadline, relaxed);
4288 		os_atomic_store(&rt_run_queue->constraint, thread->realtime.constraint, relaxed);
4289 		os_atomic_store(&rt_run_queue->ed_index, pri - BASEPRI_RTQUEUES, relaxed);
4290 	}
4291 
4292 	SCHED_STATS_RUNQ_CHANGE(&rt_run_queue->runq_stats, os_atomic_load(&rt_run_queue->count, relaxed));
4293 	rt_runq->pri_count++;
4294 	os_atomic_inc(&rt_run_queue->count, relaxed);
4295 
4296 	thread->runq = processor;
4297 
4298 	CHECK_RT_RUNQ_CONSISTENCY(rt_run_queue, thread);
4299 
4300 	return preempt;
4301 }
4302 
4303 static thread_t
rt_runq_dequeue(rt_queue_t rt_run_queue)4304 rt_runq_dequeue(rt_queue_t rt_run_queue)
4305 {
4306 	bitmap_t *map = rt_run_queue->bitmap;
4307 	int i = bitmap_first(map, NRTQS);
4308 	assert((i >= 0) && (i < NRTQS));
4309 
4310 	rt_queue_pri_t *rt_runq = &rt_run_queue->rt_queue_pri[i];
4311 
4312 	if (!sched_rt_runq_strict_priority) {
4313 		int ed_index = os_atomic_load(&rt_run_queue->ed_index, relaxed);
4314 		if (ed_index != i) {
4315 			assert((ed_index >= 0) && (ed_index < NRTQS));
4316 			rt_queue_pri_t *ed_runq = &rt_run_queue->rt_queue_pri[ed_index];
4317 
4318 			thread_t ed_thread = qe_queue_first(&ed_runq->pri_queue, struct thread, runq_links);
4319 			thread_t hi_thread = qe_queue_first(&rt_runq->pri_queue, struct thread, runq_links);
4320 
4321 			if (ed_thread->realtime.computation + hi_thread->realtime.computation + rt_deadline_epsilon < hi_thread->realtime.constraint) {
4322 				/* choose the earliest deadline thread */
4323 				rt_runq = ed_runq;
4324 				i = ed_index;
4325 			}
4326 		}
4327 	}
4328 
4329 	assert(rt_runq->pri_count > 0);
4330 	uint64_t earliest_deadline = RT_DEADLINE_NONE;
4331 	uint32_t constraint = RT_CONSTRAINT_NONE;
4332 	int ed_index = NOPRI;
4333 	thread_t new_thread = qe_dequeue_head(&rt_runq->pri_queue, struct thread, runq_links);
4334 	SCHED_STATS_RUNQ_CHANGE(&rt_run_queue->runq_stats, os_atomic_load(&rt_run_queue->count, relaxed));
4335 	if (--rt_runq->pri_count > 0) {
4336 		thread_t next_rt = qe_queue_first(&rt_runq->pri_queue, struct thread, runq_links);
4337 		assert(next_rt != THREAD_NULL);
4338 		earliest_deadline = next_rt->realtime.deadline;
4339 		constraint = next_rt->realtime.constraint;
4340 		ed_index = i;
4341 	} else {
4342 		bitmap_clear(map, i);
4343 	}
4344 	rt_runq->pri_earliest_deadline = earliest_deadline;
4345 	rt_runq->pri_constraint = constraint;
4346 
4347 	for (i = bitmap_first(map, NRTQS); i >= 0; i = bitmap_next(map, i)) {
4348 		rt_runq = &rt_run_queue->rt_queue_pri[i];
4349 		if (rt_runq->pri_earliest_deadline < earliest_deadline) {
4350 			earliest_deadline = rt_runq->pri_earliest_deadline;
4351 			constraint = rt_runq->pri_constraint;
4352 			ed_index = i;
4353 		}
4354 	}
4355 	os_atomic_store_wide(&rt_run_queue->earliest_deadline, earliest_deadline, relaxed);
4356 	os_atomic_store(&rt_run_queue->constraint, constraint, relaxed);
4357 	os_atomic_store(&rt_run_queue->ed_index, ed_index, relaxed);
4358 	os_atomic_dec(&rt_run_queue->count, relaxed);
4359 
4360 	new_thread->runq = PROCESSOR_NULL;
4361 
4362 	CHECK_RT_RUNQ_CONSISTENCY(rt_run_queue, THREAD_NULL);
4363 
4364 	return new_thread;
4365 }
4366 
4367 static thread_t
rt_runq_first(rt_queue_t rt_run_queue)4368 rt_runq_first(rt_queue_t rt_run_queue)
4369 {
4370 	bitmap_t *map = rt_run_queue->bitmap;
4371 	int i = bitmap_first(map, NRTQS);
4372 	if (i < 0) {
4373 		return THREAD_NULL;
4374 	}
4375 	rt_queue_pri_t *rt_runq = &rt_run_queue->rt_queue_pri[i];
4376 	thread_t next_rt = qe_queue_first(&rt_runq->pri_queue, struct thread, runq_links);
4377 
4378 	return next_rt;
4379 }
4380 
4381 static void
rt_runq_remove(rt_queue_t rt_run_queue,thread_t thread)4382 rt_runq_remove(rt_queue_t rt_run_queue, thread_t thread)
4383 {
4384 	CHECK_RT_RUNQ_CONSISTENCY(rt_run_queue, thread);
4385 
4386 	int pri = thread->sched_pri;
4387 	assert((pri >= BASEPRI_RTQUEUES) && (pri <= MAXPRI));
4388 	int i = pri - BASEPRI_RTQUEUES;
4389 	rt_queue_pri_t *rt_runq = &rt_run_queue->rt_queue_pri[i];
4390 	bitmap_t *map = rt_run_queue->bitmap;
4391 
4392 	assert(rt_runq->pri_count > 0);
4393 	uint64_t earliest_deadline = RT_DEADLINE_NONE;
4394 	uint32_t constraint = RT_CONSTRAINT_NONE;
4395 	int ed_index = NOPRI;
4396 	remqueue(&thread->runq_links);
4397 	SCHED_STATS_RUNQ_CHANGE(&rt_run_queue->runq_stats, os_atomic_load(&rt_run_queue->count, relaxed));
4398 	if (--rt_runq->pri_count > 0) {
4399 		thread_t next_rt = qe_queue_first(&rt_runq->pri_queue, struct thread, runq_links);
4400 		earliest_deadline = next_rt->realtime.deadline;
4401 		constraint = next_rt->realtime.constraint;
4402 		ed_index = i;
4403 	} else {
4404 		bitmap_clear(map, i);
4405 	}
4406 	rt_runq->pri_earliest_deadline = earliest_deadline;
4407 	rt_runq->pri_constraint = constraint;
4408 
4409 	for (i = bitmap_first(map, NRTQS); i >= 0; i = bitmap_next(map, i)) {
4410 		rt_runq = &rt_run_queue->rt_queue_pri[i];
4411 		if (rt_runq->pri_earliest_deadline < earliest_deadline) {
4412 			earliest_deadline = rt_runq->pri_earliest_deadline;
4413 			constraint = rt_runq->pri_constraint;
4414 			ed_index = i;
4415 		}
4416 	}
4417 	os_atomic_store_wide(&rt_run_queue->earliest_deadline, earliest_deadline, relaxed);
4418 	os_atomic_store(&rt_run_queue->constraint, constraint, relaxed);
4419 	os_atomic_store(&rt_run_queue->ed_index, ed_index, relaxed);
4420 	os_atomic_dec(&rt_run_queue->count, relaxed);
4421 
4422 	thread->runq = PROCESSOR_NULL;
4423 
4424 	CHECK_RT_RUNQ_CONSISTENCY(rt_run_queue, THREAD_NULL);
4425 }
4426 
4427 rt_queue_t
sched_rtlocal_runq(processor_set_t pset)4428 sched_rtlocal_runq(processor_set_t pset)
4429 {
4430 	return &pset->rt_runq;
4431 }
4432 
4433 void
sched_rtlocal_init(processor_set_t pset)4434 sched_rtlocal_init(processor_set_t pset)
4435 {
4436 	pset_rt_init(pset);
4437 }
4438 
4439 void
sched_rtlocal_queue_shutdown(processor_t processor)4440 sched_rtlocal_queue_shutdown(processor_t processor)
4441 {
4442 	processor_set_t pset = processor->processor_set;
4443 	thread_t        thread;
4444 	queue_head_t    tqueue;
4445 
4446 	pset_lock(pset);
4447 
4448 	/* We only need to migrate threads if this is the last active or last recommended processor in the pset */
4449 	if (bit_count(pset_available_cpumap(pset)) > 0) {
4450 		pset_unlock(pset);
4451 		return;
4452 	}
4453 
4454 	queue_init(&tqueue);
4455 
4456 	while (rt_runq_count(pset) > 0) {
4457 		thread = rt_runq_dequeue(&pset->rt_runq);
4458 		enqueue_tail(&tqueue, &thread->runq_links);
4459 	}
4460 	sched_update_pset_load_average(pset, 0);
4461 	pset_update_rt_stealable_state(pset);
4462 	pset_unlock(pset);
4463 
4464 	qe_foreach_element_safe(thread, &tqueue, runq_links) {
4465 		remqueue(&thread->runq_links);
4466 
4467 		thread_lock(thread);
4468 
4469 		thread_setrun(thread, SCHED_TAILQ);
4470 
4471 		thread_unlock(thread);
4472 	}
4473 }
4474 
4475 /* Assumes RT lock is not held, and acquires splsched/rt_lock itself */
4476 void
sched_rtlocal_runq_scan(sched_update_scan_context_t scan_context)4477 sched_rtlocal_runq_scan(sched_update_scan_context_t scan_context)
4478 {
4479 	thread_t        thread;
4480 
4481 	pset_node_t node = &pset_node0;
4482 	processor_set_t pset = node->psets;
4483 
4484 	spl_t s = splsched();
4485 	do {
4486 		while (pset != NULL) {
4487 			pset_lock(pset);
4488 
4489 			bitmap_t *map = pset->rt_runq.bitmap;
4490 			for (int i = bitmap_first(map, NRTQS); i >= 0; i = bitmap_next(map, i)) {
4491 				rt_queue_pri_t *rt_runq = &pset->rt_runq.rt_queue_pri[i];
4492 
4493 				qe_foreach_element_safe(thread, &rt_runq->pri_queue, runq_links) {
4494 					if (thread->last_made_runnable_time < scan_context->earliest_rt_make_runnable_time) {
4495 						scan_context->earliest_rt_make_runnable_time = thread->last_made_runnable_time;
4496 					}
4497 				}
4498 			}
4499 
4500 			pset_unlock(pset);
4501 
4502 			pset = pset->pset_list;
4503 		}
4504 	} while (((node = node->node_list) != NULL) && ((pset = node->psets) != NULL));
4505 	splx(s);
4506 }
4507 
4508 int64_t
sched_rtlocal_runq_count_sum(void)4509 sched_rtlocal_runq_count_sum(void)
4510 {
4511 	pset_node_t node = &pset_node0;
4512 	processor_set_t pset = node->psets;
4513 	int64_t count = 0;
4514 
4515 	do {
4516 		while (pset != NULL) {
4517 			count += pset->rt_runq.runq_stats.count_sum;
4518 
4519 			pset = pset->pset_list;
4520 		}
4521 	} while (((node = node->node_list) != NULL) && ((pset = node->psets) != NULL));
4522 
4523 	return count;
4524 }
4525 
4526 /*
4527  * Called with stealing_pset locked and
4528  * returns with stealing_pset locked
4529  * but the lock will have been dropped
4530  * if a thread is returned.
4531  */
4532 thread_t
sched_rtlocal_steal_thread(processor_set_t stealing_pset,uint64_t earliest_deadline)4533 sched_rtlocal_steal_thread(processor_set_t stealing_pset, uint64_t earliest_deadline)
4534 {
4535 	if (!sched_allow_rt_steal) {
4536 		return THREAD_NULL;
4537 	}
4538 	pset_map_t pset_map = stealing_pset->node->pset_map;
4539 
4540 	bit_clear(pset_map, stealing_pset->pset_id);
4541 
4542 	processor_set_t pset = stealing_pset;
4543 
4544 	processor_set_t target_pset;
4545 	uint64_t target_deadline;
4546 
4547 retry:
4548 	target_pset = NULL;
4549 	target_deadline = earliest_deadline - rt_deadline_epsilon;
4550 
4551 	for (int pset_id = lsb_first(pset_map); pset_id >= 0; pset_id = lsb_next(pset_map, pset_id)) {
4552 		processor_set_t nset = pset_array[pset_id];
4553 
4554 		/*
4555 		 * During startup, while pset_array[] and node->pset_map are still being initialized,
4556 		 * the update to pset_map may become visible to this cpu before the update to pset_array[].
4557 		 * It would be good to avoid inserting a memory barrier here that is only needed during startup,
4558 		 * so just check nset is not NULL instead.
4559 		 */
4560 		if (nset && (nset->stealable_rt_threads_earliest_deadline < target_deadline)) {
4561 			target_deadline = nset->stealable_rt_threads_earliest_deadline;
4562 			target_pset = nset;
4563 		}
4564 	}
4565 
4566 	if (target_pset != NULL) {
4567 		pset = change_locked_pset(pset, target_pset);
4568 		if (pset->stealable_rt_threads_earliest_deadline <= target_deadline) {
4569 			thread_t new_thread = rt_runq_dequeue(&pset->rt_runq);
4570 			pset_update_rt_stealable_state(pset);
4571 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_RT_STEAL) | DBG_FUNC_NONE, (uintptr_t)thread_tid(new_thread), pset->pset_id, pset->cpu_set_low, 0);
4572 
4573 			pset = change_locked_pset(pset, stealing_pset);
4574 			return new_thread;
4575 		}
4576 		pset = change_locked_pset(pset, stealing_pset);
4577 		earliest_deadline = rt_runq_earliest_deadline(pset);
4578 		goto retry;
4579 	}
4580 
4581 	pset = change_locked_pset(pset, stealing_pset);
4582 	return THREAD_NULL;
4583 }
4584 
4585 /*
4586  * pset is locked
4587  */
4588 thread_t
sched_rt_choose_thread(processor_set_t pset)4589 sched_rt_choose_thread(processor_set_t pset)
4590 {
4591 	processor_t processor = current_processor();
4592 
4593 	if (SCHED(steal_thread_enabled)(pset)) {
4594 		do {
4595 			bool spill_pending = bit_clear_if_set(pset->rt_pending_spill_cpu_mask, processor->cpu_id);
4596 			if (spill_pending) {
4597 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_RT_SIGNAL_SPILL) | DBG_FUNC_END, processor->cpu_id, pset->rt_pending_spill_cpu_mask, 0, 2);
4598 			}
4599 			thread_t new_thread = SCHED(rt_steal_thread)(pset, rt_runq_earliest_deadline(pset));
4600 			if (new_thread != THREAD_NULL) {
4601 				if (bit_clear_if_set(pset->rt_pending_spill_cpu_mask, processor->cpu_id)) {
4602 					KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_RT_SIGNAL_SPILL) | DBG_FUNC_END, processor->cpu_id, pset->rt_pending_spill_cpu_mask, 0, 3);
4603 				}
4604 				return new_thread;
4605 			}
4606 		} while (bit_test(pset->rt_pending_spill_cpu_mask, processor->cpu_id));
4607 	}
4608 
4609 	if (bit_clear_if_set(pset->rt_pending_spill_cpu_mask, processor->cpu_id)) {
4610 		KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_RT_SIGNAL_SPILL) | DBG_FUNC_END, processor->cpu_id, pset->rt_pending_spill_cpu_mask, 0, 4);
4611 	}
4612 
4613 	if (rt_runq_count(pset) > 0) {
4614 		thread_t new_thread = rt_runq_dequeue(SCHED(rt_runq)(pset));
4615 		assert(new_thread != THREAD_NULL);
4616 		pset_update_rt_stealable_state(pset);
4617 		return new_thread;
4618 	}
4619 
4620 	return THREAD_NULL;
4621 }
4622 
4623 /*
4624  *	realtime_queue_insert:
4625  *
4626  *	Enqueue a thread for realtime execution.
4627  */
4628 static bool
realtime_queue_insert(processor_t processor,processor_set_t pset,thread_t thread)4629 realtime_queue_insert(processor_t processor, processor_set_t pset, thread_t thread)
4630 {
4631 	pset_assert_locked(pset);
4632 
4633 	bool preempt = rt_runq_enqueue(SCHED(rt_runq)(pset), thread, processor);
4634 	pset_update_rt_stealable_state(pset);
4635 
4636 	return preempt;
4637 }
4638 
4639 /*
4640  *	realtime_setrun:
4641  *
4642  *	Dispatch a thread for realtime execution.
4643  *
4644  *	Thread must be locked.  Associated pset must
4645  *	be locked, and is returned unlocked.
4646  */
4647 static void
realtime_setrun(processor_t chosen_processor,thread_t thread)4648 realtime_setrun(
4649 	processor_t                     chosen_processor,
4650 	thread_t                        thread)
4651 {
4652 	processor_set_t pset = chosen_processor->processor_set;
4653 	pset_assert_locked(pset);
4654 	bool pset_is_locked = true;
4655 
4656 	int n_backup = 0;
4657 
4658 	if (thread->realtime.constraint <= rt_constraint_threshold) {
4659 		n_backup = sched_rt_n_backup_processors;
4660 	}
4661 	assert((n_backup >= 0) && (n_backup <= SCHED_MAX_BACKUP_PROCESSORS));
4662 
4663 	int existing_backups = bit_count(pset->pending_AST_URGENT_cpu_mask) - rt_runq_count(pset);
4664 	if (existing_backups > 0) {
4665 		n_backup = n_backup - existing_backups;
4666 		if (n_backup < 0) {
4667 			n_backup = 0;
4668 		}
4669 	}
4670 
4671 	sched_ipi_type_t ipi_type[SCHED_MAX_BACKUP_PROCESSORS + 1] = {};
4672 	processor_t ipi_processor[SCHED_MAX_BACKUP_PROCESSORS + 1] = {};
4673 
4674 	thread->chosen_processor = chosen_processor;
4675 
4676 	/* <rdar://problem/15102234> */
4677 	assert(thread->bound_processor == PROCESSOR_NULL);
4678 
4679 	realtime_queue_insert(chosen_processor, pset, thread);
4680 
4681 	processor_t processor = chosen_processor;
4682 
4683 	int count = 0;
4684 	for (int i = 0; i <= n_backup; i++) {
4685 		if (i == 0) {
4686 			ipi_type[i] = SCHED_IPI_NONE;
4687 			ipi_processor[i] = processor;
4688 			count++;
4689 
4690 			ast_t preempt = AST_NONE;
4691 			if (thread->sched_pri > processor->current_pri) {
4692 				preempt = (AST_PREEMPT | AST_URGENT);
4693 			} else if (thread->sched_pri == processor->current_pri) {
4694 				if (deadline_add(thread->realtime.deadline, rt_deadline_epsilon) < processor->deadline) {
4695 					preempt = (AST_PREEMPT | AST_URGENT);
4696 				}
4697 			}
4698 
4699 			if (preempt != AST_NONE) {
4700 				if (processor->state == PROCESSOR_IDLE) {
4701 					if (processor == current_processor()) {
4702 						pset_update_processor_state(pset, processor, PROCESSOR_DISPATCHING);
4703 						ast_on(preempt);
4704 
4705 						if ((preempt & AST_URGENT) == AST_URGENT) {
4706 							if (bit_set_if_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
4707 								KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_START,
4708 								    processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, (uintptr_t)thread_tid(thread), 1);
4709 							}
4710 						}
4711 
4712 						if ((preempt & AST_PREEMPT) == AST_PREEMPT) {
4713 							bit_set(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
4714 						}
4715 					} else {
4716 						ipi_type[i] = sched_ipi_action(processor, thread, SCHED_IPI_EVENT_RT_PREEMPT);
4717 					}
4718 				} else if (processor->state == PROCESSOR_DISPATCHING) {
4719 					if (bit_set_if_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
4720 						KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_START,
4721 						    processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, (uintptr_t)thread_tid(thread), 2);
4722 					}
4723 				} else {
4724 					if (processor == current_processor()) {
4725 						ast_on(preempt);
4726 
4727 						if ((preempt & AST_URGENT) == AST_URGENT) {
4728 							if (bit_set_if_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
4729 								KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_START,
4730 								    processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, (uintptr_t)thread_tid(thread), 3);
4731 							}
4732 						}
4733 
4734 						if ((preempt & AST_PREEMPT) == AST_PREEMPT) {
4735 							bit_set(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
4736 						}
4737 					} else {
4738 						ipi_type[i] = sched_ipi_action(processor, thread, SCHED_IPI_EVENT_RT_PREEMPT);
4739 					}
4740 				}
4741 			} else {
4742 				/* Selected processor was too busy, just keep thread enqueued and let other processors drain it naturally. */
4743 			}
4744 		} else {
4745 			if (!pset_is_locked) {
4746 				pset_lock(pset);
4747 			}
4748 			ipi_type[i] = SCHED_IPI_NONE;
4749 			ipi_processor[i] = PROCESSOR_NULL;
4750 			pset_is_locked = !choose_next_rt_processor_for_IPI(pset, chosen_processor, false, &ipi_processor[i], &ipi_type[i]);
4751 			if (ipi_processor[i] == PROCESSOR_NULL) {
4752 				break;
4753 			}
4754 			count++;
4755 
4756 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_NEXT_PROCESSOR) | DBG_FUNC_NONE,
4757 			    ipi_processor[i]->cpu_id, ipi_processor[i]->state, backup, 1);
4758 #if defined(__x86_64__)
4759 #define p_is_good(p) (((p)->processor_primary == (p)) && ((sched_avoid_cpu0 != 1) || ((p)->cpu_id != 0)))
4760 			if (n_backup == SCHED_DEFAULT_BACKUP_PROCESSORS_SMT) {
4761 				processor_t p0 = ipi_processor[0];
4762 				processor_t p1 = ipi_processor[1];
4763 				assert(p0 && p1);
4764 				if (p_is_good(p0) && p_is_good(p1)) {
4765 					/*
4766 					 * Both the chosen processor and the first backup are non-cpu0 primaries,
4767 					 * so there is no need for a 2nd backup processor.
4768 					 */
4769 					break;
4770 				}
4771 			}
4772 #endif
4773 		}
4774 	}
4775 
4776 	if (pset_is_locked) {
4777 		pset_unlock(pset);
4778 	}
4779 
4780 	assert((count > 0) && (count <= (n_backup + 1)));
4781 	for (int i = 0; i < count; i++) {
4782 		assert(ipi_processor[i] != PROCESSOR_NULL);
4783 		sched_ipi_perform(ipi_processor[i], ipi_type[i]);
4784 	}
4785 }
4786 
4787 
4788 sched_ipi_type_t
sched_ipi_deferred_policy(processor_set_t pset,processor_t dst,thread_t thread,__unused sched_ipi_event_t event)4789 sched_ipi_deferred_policy(processor_set_t pset, processor_t dst,
4790     thread_t thread, __unused sched_ipi_event_t event)
4791 {
4792 #if defined(CONFIG_SCHED_DEFERRED_AST)
4793 #if CONFIG_THREAD_GROUPS
4794 	if (thread) {
4795 		struct thread_group *tg = thread_group_get(thread);
4796 		if (thread_group_uses_immediate_ipi(tg)) {
4797 			return SCHED_IPI_IMMEDIATE;
4798 		}
4799 	}
4800 #endif /* CONFIG_THREAD_GROUPS */
4801 	if (!bit_test(pset->pending_deferred_AST_cpu_mask, dst->cpu_id)) {
4802 		return SCHED_IPI_DEFERRED;
4803 	}
4804 #else /* CONFIG_SCHED_DEFERRED_AST */
4805 	(void) thread;
4806 	panic("Request for deferred IPI on an unsupported platform; pset: %p CPU: %d", pset, dst->cpu_id);
4807 #endif /* CONFIG_SCHED_DEFERRED_AST */
4808 	return SCHED_IPI_NONE;
4809 }
4810 
4811 sched_ipi_type_t
sched_ipi_action(processor_t dst,thread_t thread,sched_ipi_event_t event)4812 sched_ipi_action(processor_t dst, thread_t thread, sched_ipi_event_t event)
4813 {
4814 	sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
4815 	assert(dst != NULL);
4816 
4817 	processor_set_t pset = dst->processor_set;
4818 	if (current_processor() == dst) {
4819 		return SCHED_IPI_NONE;
4820 	}
4821 
4822 	bool dst_idle = (dst->state == PROCESSOR_IDLE);
4823 	if (dst_idle) {
4824 		pset_update_processor_state(pset, dst, PROCESSOR_DISPATCHING);
4825 	}
4826 
4827 	ipi_type = SCHED(ipi_policy)(dst, thread, dst_idle, event);
4828 	switch (ipi_type) {
4829 	case SCHED_IPI_NONE:
4830 		return SCHED_IPI_NONE;
4831 #if defined(CONFIG_SCHED_DEFERRED_AST)
4832 	case SCHED_IPI_DEFERRED:
4833 		bit_set(pset->pending_deferred_AST_cpu_mask, dst->cpu_id);
4834 		break;
4835 #endif /* CONFIG_SCHED_DEFERRED_AST */
4836 	default:
4837 		if (bit_set_if_clear(pset->pending_AST_URGENT_cpu_mask, dst->cpu_id)) {
4838 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_START,
4839 			    dst->cpu_id, pset->pending_AST_URGENT_cpu_mask, (uintptr_t)thread_tid(thread), 4);
4840 		}
4841 		bit_set(pset->pending_AST_PREEMPT_cpu_mask, dst->cpu_id);
4842 		break;
4843 	}
4844 	return ipi_type;
4845 }
4846 
4847 sched_ipi_type_t
sched_ipi_policy(processor_t dst,thread_t thread,boolean_t dst_idle,sched_ipi_event_t event)4848 sched_ipi_policy(processor_t dst, thread_t thread, boolean_t dst_idle, sched_ipi_event_t event)
4849 {
4850 	sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
4851 	boolean_t deferred_ipi_supported = false;
4852 	processor_set_t pset = dst->processor_set;
4853 
4854 #if defined(CONFIG_SCHED_DEFERRED_AST)
4855 	deferred_ipi_supported = true;
4856 #endif /* CONFIG_SCHED_DEFERRED_AST */
4857 
4858 	switch (event) {
4859 	case SCHED_IPI_EVENT_SPILL:
4860 	case SCHED_IPI_EVENT_SMT_REBAL:
4861 	case SCHED_IPI_EVENT_REBALANCE:
4862 	case SCHED_IPI_EVENT_BOUND_THR:
4863 	case SCHED_IPI_EVENT_RT_PREEMPT:
4864 		/*
4865 		 * The RT preempt, spill, SMT rebalance, rebalance and the bound thread
4866 		 * scenarios use immediate IPIs always.
4867 		 */
4868 		ipi_type = dst_idle ? SCHED_IPI_IDLE : SCHED_IPI_IMMEDIATE;
4869 		break;
4870 	case SCHED_IPI_EVENT_PREEMPT:
4871 		/* In the preemption case, use immediate IPIs for RT threads */
4872 		if (thread && (thread->sched_pri >= BASEPRI_RTQUEUES)) {
4873 			ipi_type = dst_idle ? SCHED_IPI_IDLE : SCHED_IPI_IMMEDIATE;
4874 			break;
4875 		}
4876 
4877 		/*
4878 		 * For Non-RT threads preemption,
4879 		 * If the core is active, use immediate IPIs.
4880 		 * If the core is idle, use deferred IPIs if supported; otherwise immediate IPI.
4881 		 */
4882 		if (deferred_ipi_supported && dst_idle) {
4883 			return sched_ipi_deferred_policy(pset, dst, thread, event);
4884 		}
4885 		ipi_type = dst_idle ? SCHED_IPI_IDLE : SCHED_IPI_IMMEDIATE;
4886 		break;
4887 	default:
4888 		panic("Unrecognized scheduler IPI event type %d", event);
4889 	}
4890 	assert(ipi_type != SCHED_IPI_NONE);
4891 	return ipi_type;
4892 }
4893 
4894 void
sched_ipi_perform(processor_t dst,sched_ipi_type_t ipi)4895 sched_ipi_perform(processor_t dst, sched_ipi_type_t ipi)
4896 {
4897 	switch (ipi) {
4898 	case SCHED_IPI_NONE:
4899 		break;
4900 	case SCHED_IPI_IDLE:
4901 		machine_signal_idle(dst);
4902 		break;
4903 	case SCHED_IPI_IMMEDIATE:
4904 		cause_ast_check(dst);
4905 		break;
4906 	case SCHED_IPI_DEFERRED:
4907 		machine_signal_idle_deferred(dst);
4908 		break;
4909 	default:
4910 		panic("Unrecognized scheduler IPI type: %d", ipi);
4911 	}
4912 }
4913 
4914 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
4915 
4916 boolean_t
priority_is_urgent(int priority)4917 priority_is_urgent(int priority)
4918 {
4919 	return bitmap_test(sched_preempt_pri, priority) ? TRUE : FALSE;
4920 }
4921 
4922 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
4923 
4924 /*
4925  *	processor_setrun:
4926  *
4927  *	Dispatch a thread for execution on a
4928  *	processor.
4929  *
4930  *	Thread must be locked.  Associated pset must
4931  *	be locked, and is returned unlocked.
4932  */
4933 static void
processor_setrun(processor_t processor,thread_t thread,integer_t options)4934 processor_setrun(
4935 	processor_t                     processor,
4936 	thread_t                        thread,
4937 	integer_t                       options)
4938 {
4939 	processor_set_t pset = processor->processor_set;
4940 	pset_assert_locked(pset);
4941 	ast_t preempt = AST_NONE;
4942 	enum { eExitIdle, eInterruptRunning, eDoNothing } ipi_action = eDoNothing;
4943 
4944 	sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
4945 
4946 	thread->chosen_processor = processor;
4947 
4948 	/*
4949 	 *	Set preemption mode.
4950 	 */
4951 #if defined(CONFIG_SCHED_DEFERRED_AST)
4952 	/* TODO: Do we need to care about urgency (see rdar://problem/20136239)? */
4953 #endif
4954 	if (SCHED(priority_is_urgent)(thread->sched_pri) && thread->sched_pri > processor->current_pri) {
4955 		preempt = (AST_PREEMPT | AST_URGENT);
4956 	} else if (processor->current_is_eagerpreempt) {
4957 		preempt = (AST_PREEMPT | AST_URGENT);
4958 	} else if ((thread->sched_mode == TH_MODE_TIMESHARE) && (thread->sched_pri < thread->base_pri)) {
4959 		if (SCHED(priority_is_urgent)(thread->base_pri) && thread->sched_pri > processor->current_pri) {
4960 			preempt = (options & SCHED_PREEMPT)? AST_PREEMPT: AST_NONE;
4961 		} else {
4962 			preempt = AST_NONE;
4963 		}
4964 	} else {
4965 		preempt = (options & SCHED_PREEMPT)? AST_PREEMPT: AST_NONE;
4966 	}
4967 
4968 	if ((options & (SCHED_PREEMPT | SCHED_REBALANCE)) == (SCHED_PREEMPT | SCHED_REBALANCE)) {
4969 		/*
4970 		 * Having gone to the trouble of forcing this thread off a less preferred core,
4971 		 * we should force the preferable core to reschedule immediately to give this
4972 		 * thread a chance to run instead of just sitting on the run queue where
4973 		 * it may just be stolen back by the idle core we just forced it off.
4974 		 */
4975 		preempt |= AST_PREEMPT;
4976 	}
4977 
4978 	SCHED(processor_enqueue)(processor, thread, options);
4979 	sched_update_pset_load_average(pset, 0);
4980 
4981 	if (preempt != AST_NONE) {
4982 		if (processor->state == PROCESSOR_IDLE) {
4983 			ipi_action = eExitIdle;
4984 		} else if (processor->state == PROCESSOR_DISPATCHING) {
4985 			if (bit_set_if_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
4986 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_START,
4987 				    processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, (uintptr_t)thread_tid(thread), 5);
4988 			}
4989 		} else if ((processor->state == PROCESSOR_RUNNING ||
4990 		    processor->state == PROCESSOR_SHUTDOWN) &&
4991 		    (thread->sched_pri >= processor->current_pri)) {
4992 			ipi_action = eInterruptRunning;
4993 		}
4994 	} else {
4995 		/*
4996 		 * New thread is not important enough to preempt what is running, but
4997 		 * special processor states may need special handling
4998 		 */
4999 		if (processor->state == PROCESSOR_SHUTDOWN &&
5000 		    thread->sched_pri >= processor->current_pri) {
5001 			ipi_action = eInterruptRunning;
5002 		} else if (processor->state == PROCESSOR_IDLE) {
5003 			ipi_action = eExitIdle;
5004 		} else if (processor->state == PROCESSOR_DISPATCHING) {
5005 			if (bit_set_if_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
5006 				KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_START,
5007 				    processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, (uintptr_t)thread_tid(thread), 6);
5008 			}
5009 		}
5010 	}
5011 
5012 	if (ipi_action != eDoNothing) {
5013 		if (processor == current_processor()) {
5014 			if (ipi_action == eExitIdle) {
5015 				pset_update_processor_state(pset, processor, PROCESSOR_DISPATCHING);
5016 			}
5017 			if ((preempt = csw_check_locked(processor->active_thread, processor, pset, AST_NONE)) != AST_NONE) {
5018 				ast_on(preempt);
5019 			}
5020 
5021 			if ((preempt & AST_URGENT) == AST_URGENT) {
5022 				if (bit_set_if_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
5023 					KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_START,
5024 					    processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, (uintptr_t)thread_tid(thread), 7);
5025 				}
5026 			} else {
5027 				if (bit_clear_if_set(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
5028 					KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_END, processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, 0, 7);
5029 				}
5030 			}
5031 
5032 			if ((preempt & AST_PREEMPT) == AST_PREEMPT) {
5033 				bit_set(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
5034 			} else {
5035 				bit_clear(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
5036 			}
5037 		} else {
5038 			sched_ipi_event_t event = (options & SCHED_REBALANCE) ? SCHED_IPI_EVENT_REBALANCE : SCHED_IPI_EVENT_PREEMPT;
5039 			ipi_type = sched_ipi_action(processor, thread, event);
5040 		}
5041 	}
5042 
5043 	pset_unlock(pset);
5044 	sched_ipi_perform(processor, ipi_type);
5045 
5046 	if (ipi_action != eDoNothing && processor == current_processor()) {
5047 		ast_t new_preempt = update_pending_nonurgent_preemption(processor, preempt);
5048 		ast_on(new_preempt);
5049 	}
5050 }
5051 
5052 /*
5053  *	choose_next_pset:
5054  *
5055  *	Return the next sibling pset containing
5056  *	available processors.
5057  *
5058  *	Returns the original pset if none other is
5059  *	suitable.
5060  */
5061 static processor_set_t
choose_next_pset(processor_set_t pset)5062 choose_next_pset(
5063 	processor_set_t         pset)
5064 {
5065 	processor_set_t         nset = pset;
5066 
5067 	do {
5068 		nset = next_pset(nset);
5069 
5070 		/*
5071 		 * Sometimes during startup the pset_map can contain a bit
5072 		 * for a pset that isn't fully published in pset_array because
5073 		 * the pset_map read isn't an acquire load.
5074 		 *
5075 		 * In order to avoid needing an acquire barrier here, just bail
5076 		 * out.
5077 		 */
5078 		if (nset == PROCESSOR_SET_NULL) {
5079 			return pset;
5080 		}
5081 	} while (nset->online_processor_count < 1 && nset != pset);
5082 
5083 	return nset;
5084 }
5085 
5086 /*
5087  *	choose_processor:
5088  *
5089  *	Choose a processor for the thread, beginning at
5090  *	the pset.  Accepts an optional processor hint in
5091  *	the pset.
5092  *
5093  *	Returns a processor, possibly from a different pset.
5094  *
5095  *	The thread must be locked.  The pset must be locked,
5096  *	and the resulting pset is locked on return.
5097  */
5098 processor_t
choose_processor(processor_set_t starting_pset,processor_t processor,thread_t thread)5099 choose_processor(
5100 	processor_set_t         starting_pset,
5101 	processor_t             processor,
5102 	thread_t                thread)
5103 {
5104 	processor_set_t pset = starting_pset;
5105 	processor_set_t nset;
5106 
5107 	assert(thread->sched_pri <= MAXPRI);
5108 
5109 	/*
5110 	 * Prefer the hinted processor, when appropriate.
5111 	 */
5112 
5113 	/* Fold last processor hint from secondary processor to its primary */
5114 	if (processor != PROCESSOR_NULL) {
5115 		processor = processor->processor_primary;
5116 	}
5117 
5118 	/*
5119 	 * Only consult platform layer if pset is active, which
5120 	 * it may not be in some cases when a multi-set system
5121 	 * is going to sleep.
5122 	 */
5123 	if (pset->online_processor_count) {
5124 		if ((processor == PROCESSOR_NULL) || (processor->processor_set == pset && processor->state == PROCESSOR_IDLE)) {
5125 			processor_t mc_processor = machine_choose_processor(pset, processor);
5126 			if (mc_processor != PROCESSOR_NULL) {
5127 				processor = mc_processor->processor_primary;
5128 			}
5129 		}
5130 	}
5131 
5132 	/*
5133 	 * At this point, we may have a processor hint, and we may have
5134 	 * an initial starting pset. If the hint is not in the pset, or
5135 	 * if the hint is for a processor in an invalid state, discard
5136 	 * the hint.
5137 	 */
5138 	if (processor != PROCESSOR_NULL) {
5139 		if (processor->processor_set != pset) {
5140 			processor = PROCESSOR_NULL;
5141 		} else if (!processor->is_recommended) {
5142 			processor = PROCESSOR_NULL;
5143 		} else {
5144 			switch (processor->state) {
5145 			case PROCESSOR_START:
5146 			case PROCESSOR_SHUTDOWN:
5147 			case PROCESSOR_PENDING_OFFLINE:
5148 			case PROCESSOR_OFF_LINE:
5149 				/*
5150 				 * Hint is for a processor that cannot support running new threads.
5151 				 */
5152 				processor = PROCESSOR_NULL;
5153 				break;
5154 			case PROCESSOR_IDLE:
5155 				/*
5156 				 * Hint is for an idle processor. Assume it is no worse than any other
5157 				 * idle processor. The platform layer had an opportunity to provide
5158 				 * the "least cost idle" processor above.
5159 				 */
5160 				if ((thread->sched_pri < BASEPRI_RTQUEUES) || processor_is_fast_track_candidate_for_realtime_thread(pset, processor)) {
5161 					uint64_t idle_primary_map = (pset->cpu_state_map[PROCESSOR_IDLE] & pset->primary_map & pset->recommended_bitmask);
5162 					uint64_t preferred_idle_primary_map = idle_primary_map & pset->perfcontrol_cpu_preferred_bitmask;
5163 					/*
5164 					 * Only 1 idle core, choose it.
5165 					 */
5166 					if (bit_count(idle_primary_map) == 1) {
5167 						return processor;
5168 					}
5169 
5170 					/*
5171 					 * If the rotation bitmask to force a migration is set for this core and one of the preferred cores
5172 					 * is idle, don't continue running on the same core.
5173 					 */
5174 					if (!(bit_test(processor->processor_set->perfcontrol_cpu_migration_bitmask, processor->cpu_id) && preferred_idle_primary_map != 0)) {
5175 						return processor;
5176 					}
5177 				}
5178 				processor = PROCESSOR_NULL;
5179 				break;
5180 			case PROCESSOR_RUNNING:
5181 			case PROCESSOR_DISPATCHING:
5182 				/*
5183 				 * Hint is for an active CPU. This fast-path allows
5184 				 * realtime threads to preempt non-realtime threads
5185 				 * to regain their previous executing processor.
5186 				 */
5187 				if (thread->sched_pri >= BASEPRI_RTQUEUES) {
5188 					if (processor_is_fast_track_candidate_for_realtime_thread(pset, processor)) {
5189 						return processor;
5190 					}
5191 					processor = PROCESSOR_NULL;
5192 				}
5193 
5194 				/* Otherwise, use hint as part of search below */
5195 				break;
5196 			default:
5197 				processor = PROCESSOR_NULL;
5198 				break;
5199 			}
5200 		}
5201 	}
5202 
5203 	/*
5204 	 * Iterate through the processor sets to locate
5205 	 * an appropriate processor. Seed results with
5206 	 * a last-processor hint, if available, so that
5207 	 * a search must find something strictly better
5208 	 * to replace it.
5209 	 *
5210 	 * A primary/secondary pair of SMT processors are
5211 	 * "unpaired" if the primary is busy but its
5212 	 * corresponding secondary is idle (so the physical
5213 	 * core has full use of its resources).
5214 	 */
5215 
5216 	integer_t lowest_priority = MAXPRI + 1;
5217 	integer_t lowest_secondary_priority = MAXPRI + 1;
5218 	integer_t lowest_unpaired_primary_priority = MAXPRI + 1;
5219 	integer_t lowest_idle_secondary_priority = MAXPRI + 1;
5220 	integer_t lowest_count = INT_MAX;
5221 	processor_t lp_processor = PROCESSOR_NULL;
5222 	processor_t lp_unpaired_primary_processor = PROCESSOR_NULL;
5223 	processor_t lp_idle_secondary_processor = PROCESSOR_NULL;
5224 	processor_t lp_paired_secondary_processor = PROCESSOR_NULL;
5225 	processor_t lc_processor = PROCESSOR_NULL;
5226 
5227 	if (processor != PROCESSOR_NULL) {
5228 		/* All other states should be enumerated above. */
5229 		assert(processor->state == PROCESSOR_RUNNING || processor->state == PROCESSOR_DISPATCHING);
5230 		assert(thread->sched_pri < BASEPRI_RTQUEUES);
5231 
5232 		lowest_priority = processor->current_pri;
5233 		lp_processor = processor;
5234 
5235 		lowest_count = SCHED(processor_runq_count)(processor);
5236 		lc_processor = processor;
5237 	}
5238 
5239 	if (thread->sched_pri >= BASEPRI_RTQUEUES) {
5240 		pset_node_t node = pset->node;
5241 		bool include_ast_urgent_pending_cpus = false;
5242 		cpumap_t ast_urgent_pending;
5243 try_again:
5244 		ast_urgent_pending = 0;
5245 		int consider_secondaries = (!pset->is_SMT) || (bit_count(node->pset_map) == 1) || (node->pset_non_rt_primary_map == 0) || include_ast_urgent_pending_cpus;
5246 		for (; consider_secondaries < 2; consider_secondaries++) {
5247 			pset = change_locked_pset(pset, starting_pset);
5248 			do {
5249 				cpumap_t available_map = pset_available_cpumap(pset);
5250 				if (available_map == 0) {
5251 					goto no_available_cpus;
5252 				}
5253 
5254 				processor = choose_processor_for_realtime_thread(pset, PROCESSOR_NULL, consider_secondaries, false);
5255 				if (processor) {
5256 					return processor;
5257 				}
5258 
5259 				if (consider_secondaries) {
5260 					processor = choose_furthest_deadline_processor_for_realtime_thread(pset, thread->sched_pri, thread->realtime.deadline, PROCESSOR_NULL, false, include_ast_urgent_pending_cpus);
5261 					if (processor) {
5262 						/*
5263 						 * Instead of looping through all the psets to find the global
5264 						 * furthest deadline processor, preempt the first candidate found.
5265 						 * The preempted thread will then find any other available far deadline
5266 						 * processors to preempt.
5267 						 */
5268 						return processor;
5269 					}
5270 
5271 					ast_urgent_pending |= pset->pending_AST_URGENT_cpu_mask;
5272 
5273 					if (rt_runq_count(pset) < lowest_count) {
5274 						int cpuid = bit_first(available_map);
5275 						assert(cpuid >= 0);
5276 						lc_processor = processor_array[cpuid];
5277 						lowest_count = rt_runq_count(pset);
5278 					}
5279 				}
5280 
5281 no_available_cpus:
5282 				nset = next_pset(pset);
5283 
5284 				if (nset != starting_pset) {
5285 					pset = change_locked_pset(pset, nset);
5286 				}
5287 			} while (nset != starting_pset);
5288 		}
5289 
5290 		/* Short cut for single pset nodes */
5291 		if (bit_count(node->pset_map) == 1) {
5292 			if (lc_processor) {
5293 				pset_assert_locked(lc_processor->processor_set);
5294 				return lc_processor;
5295 			}
5296 		} else {
5297 			if (ast_urgent_pending && !include_ast_urgent_pending_cpus) {
5298 				/* See the comment in choose_furthest_deadline_processor_for_realtime_thread() */
5299 				include_ast_urgent_pending_cpus = true;
5300 				goto try_again;
5301 			}
5302 		}
5303 
5304 		processor = lc_processor;
5305 
5306 		if (processor) {
5307 			pset = change_locked_pset(pset, processor->processor_set);
5308 			/* Check that chosen processor is still usable */
5309 			cpumap_t available_map = pset_available_cpumap(pset);
5310 			if (bit_test(available_map, processor->cpu_id)) {
5311 				return processor;
5312 			}
5313 
5314 			/* processor is no longer usable */
5315 			processor = PROCESSOR_NULL;
5316 		}
5317 
5318 		pset_assert_locked(pset);
5319 		pset_unlock(pset);
5320 		return PROCESSOR_NULL;
5321 	}
5322 
5323 	/* No realtime threads from this point on */
5324 	assert(thread->sched_pri < BASEPRI_RTQUEUES);
5325 
5326 	do {
5327 		/*
5328 		 * Choose an idle processor, in pset traversal order
5329 		 */
5330 		uint64_t idle_primary_map = (pset->cpu_state_map[PROCESSOR_IDLE] & pset->primary_map & pset->recommended_bitmask);
5331 		uint64_t preferred_idle_primary_map = idle_primary_map & pset->perfcontrol_cpu_preferred_bitmask;
5332 
5333 		/* there shouldn't be a pending AST if the processor is idle */
5334 		assert((idle_primary_map & pset->pending_AST_URGENT_cpu_mask) == 0);
5335 
5336 		/*
5337 		 * Look at the preferred cores first.
5338 		 */
5339 		int cpuid = lsb_next(preferred_idle_primary_map, pset->cpu_preferred_last_chosen);
5340 		if (cpuid < 0) {
5341 			cpuid = lsb_first(preferred_idle_primary_map);
5342 		}
5343 		if (cpuid >= 0) {
5344 			processor = processor_array[cpuid];
5345 			pset->cpu_preferred_last_chosen = cpuid;
5346 			return processor;
5347 		}
5348 
5349 		/*
5350 		 * Fall back to all idle cores if none of the preferred ones are available.
5351 		 */
5352 		cpuid = lsb_first(idle_primary_map);
5353 		if (cpuid >= 0) {
5354 			processor = processor_array[cpuid];
5355 			return processor;
5356 		}
5357 
5358 		/*
5359 		 * Otherwise, enumerate active and idle processors to find primary candidates
5360 		 * with lower priority/etc.
5361 		 */
5362 
5363 		uint64_t active_map = ((pset->cpu_state_map[PROCESSOR_RUNNING] | pset->cpu_state_map[PROCESSOR_DISPATCHING]) &
5364 		    pset->recommended_bitmask &
5365 		    ~pset->pending_AST_URGENT_cpu_mask);
5366 
5367 		if (SCHED(priority_is_urgent)(thread->sched_pri) == FALSE) {
5368 			active_map &= ~pset->pending_AST_PREEMPT_cpu_mask;
5369 		}
5370 
5371 		active_map = bit_ror64(active_map, (pset->last_chosen + 1));
5372 		for (int rotid = lsb_first(active_map); rotid >= 0; rotid = lsb_next(active_map, rotid)) {
5373 			cpuid = ((rotid + pset->last_chosen + 1) & 63);
5374 			processor = processor_array[cpuid];
5375 
5376 			integer_t cpri = processor->current_pri;
5377 			processor_t primary = processor->processor_primary;
5378 			if (primary != processor) {
5379 				/* If primary is running a NO_SMT thread, don't choose its secondary */
5380 				if (!((primary->state == PROCESSOR_RUNNING) && processor_active_thread_no_smt(primary))) {
5381 					if (cpri < lowest_secondary_priority) {
5382 						lowest_secondary_priority = cpri;
5383 						lp_paired_secondary_processor = processor;
5384 					}
5385 				}
5386 			} else {
5387 				if (cpri < lowest_priority) {
5388 					lowest_priority = cpri;
5389 					lp_processor = processor;
5390 				}
5391 			}
5392 
5393 			integer_t ccount = SCHED(processor_runq_count)(processor);
5394 			if (ccount < lowest_count) {
5395 				lowest_count = ccount;
5396 				lc_processor = processor;
5397 			}
5398 		}
5399 
5400 		/*
5401 		 * For SMT configs, these idle secondary processors must have active primary. Otherwise
5402 		 * the idle primary would have short-circuited the loop above
5403 		 */
5404 		uint64_t idle_secondary_map = (pset->cpu_state_map[PROCESSOR_IDLE] &
5405 		    ~pset->primary_map &
5406 		    pset->recommended_bitmask);
5407 
5408 		/* there shouldn't be a pending AST if the processor is idle */
5409 		assert((idle_secondary_map & pset->pending_AST_URGENT_cpu_mask) == 0);
5410 		assert((idle_secondary_map & pset->pending_AST_PREEMPT_cpu_mask) == 0);
5411 
5412 		for (cpuid = lsb_first(idle_secondary_map); cpuid >= 0; cpuid = lsb_next(idle_secondary_map, cpuid)) {
5413 			processor = processor_array[cpuid];
5414 
5415 			processor_t cprimary = processor->processor_primary;
5416 
5417 			integer_t primary_pri = cprimary->current_pri;
5418 
5419 			/*
5420 			 * TODO: This should also make the same decisions
5421 			 * as secondary_can_run_realtime_thread
5422 			 *
5423 			 * TODO: Keep track of the pending preemption priority
5424 			 * of the primary to make this more accurate.
5425 			 */
5426 
5427 			/* If the primary is running a no-smt thread, then don't choose its secondary */
5428 			if (cprimary->state == PROCESSOR_RUNNING &&
5429 			    processor_active_thread_no_smt(cprimary)) {
5430 				continue;
5431 			}
5432 
5433 			/*
5434 			 * Find the idle secondary processor with the lowest priority primary
5435 			 *
5436 			 * We will choose this processor as a fallback if we find no better
5437 			 * primary to preempt.
5438 			 */
5439 			if (primary_pri < lowest_idle_secondary_priority) {
5440 				lp_idle_secondary_processor = processor;
5441 				lowest_idle_secondary_priority = primary_pri;
5442 			}
5443 
5444 			/* Find the the lowest priority active primary with idle secondary */
5445 			if (primary_pri < lowest_unpaired_primary_priority) {
5446 				/* If the primary processor is offline or starting up, it's not a candidate for this path */
5447 				if (cprimary->state != PROCESSOR_RUNNING &&
5448 				    cprimary->state != PROCESSOR_DISPATCHING) {
5449 					continue;
5450 				}
5451 
5452 				if (!cprimary->is_recommended) {
5453 					continue;
5454 				}
5455 
5456 				/* if the primary is pending preemption, don't try to re-preempt it */
5457 				if (bit_test(pset->pending_AST_URGENT_cpu_mask, cprimary->cpu_id)) {
5458 					continue;
5459 				}
5460 
5461 				if (SCHED(priority_is_urgent)(thread->sched_pri) == FALSE &&
5462 				    bit_test(pset->pending_AST_PREEMPT_cpu_mask, cprimary->cpu_id)) {
5463 					continue;
5464 				}
5465 
5466 				lowest_unpaired_primary_priority = primary_pri;
5467 				lp_unpaired_primary_processor = cprimary;
5468 			}
5469 		}
5470 
5471 		/*
5472 		 * We prefer preempting a primary processor over waking up its secondary.
5473 		 * The secondary will then be woken up by the preempted thread.
5474 		 */
5475 		if (thread->sched_pri > lowest_unpaired_primary_priority) {
5476 			pset->last_chosen = lp_unpaired_primary_processor->cpu_id;
5477 			return lp_unpaired_primary_processor;
5478 		}
5479 
5480 		/*
5481 		 * We prefer preempting a lower priority active processor over directly
5482 		 * waking up an idle secondary.
5483 		 * The preempted thread will then find the idle secondary.
5484 		 */
5485 		if (thread->sched_pri > lowest_priority) {
5486 			pset->last_chosen = lp_processor->cpu_id;
5487 			return lp_processor;
5488 		}
5489 
5490 		/*
5491 		 * lc_processor is used to indicate the best processor set run queue
5492 		 * on which to enqueue a thread when all available CPUs are busy with
5493 		 * higher priority threads, so try to make sure it is initialized.
5494 		 */
5495 		if (lc_processor == PROCESSOR_NULL) {
5496 			cpumap_t available_map = pset_available_cpumap(pset);
5497 			cpuid = lsb_first(available_map);
5498 			if (cpuid >= 0) {
5499 				lc_processor = processor_array[cpuid];
5500 				lowest_count = SCHED(processor_runq_count)(lc_processor);
5501 			}
5502 		}
5503 
5504 		/*
5505 		 * Move onto the next processor set.
5506 		 *
5507 		 * If all primary processors in this pset are running a higher
5508 		 * priority thread, move on to next pset. Only when we have
5509 		 * exhausted the search for primary processors do we
5510 		 * fall back to secondaries.
5511 		 */
5512 #if CONFIG_SCHED_EDGE
5513 		/*
5514 		 * The edge scheduler expects a CPU to be selected from the pset it passed in
5515 		 * as the starting pset for non-RT workloads. The edge migration algorithm
5516 		 * should already have considered idle CPUs and loads to decide the starting_pset;
5517 		 * which means that this loop can be short-circuted.
5518 		 */
5519 		nset = starting_pset;
5520 #else /* CONFIG_SCHED_EDGE */
5521 		nset = next_pset(pset);
5522 #endif /* CONFIG_SCHED_EDGE */
5523 
5524 		if (nset != starting_pset) {
5525 			pset = change_locked_pset(pset, nset);
5526 		}
5527 	} while (nset != starting_pset);
5528 
5529 	/*
5530 	 * Make sure that we pick a running processor,
5531 	 * and that the correct processor set is locked.
5532 	 * Since we may have unlocked the candidate processor's
5533 	 * pset, it may have changed state.
5534 	 *
5535 	 * All primary processors are running a higher priority
5536 	 * thread, so the only options left are enqueuing on
5537 	 * the secondary processor that would perturb the least priority
5538 	 * primary, or the least busy primary.
5539 	 */
5540 
5541 	/* lowest_priority is evaluated in the main loops above */
5542 	if (lp_idle_secondary_processor != PROCESSOR_NULL) {
5543 		processor = lp_idle_secondary_processor;
5544 	} else if (lp_paired_secondary_processor != PROCESSOR_NULL) {
5545 		processor = lp_paired_secondary_processor;
5546 	} else if (lc_processor != PROCESSOR_NULL) {
5547 		processor = lc_processor;
5548 	} else {
5549 		processor = PROCESSOR_NULL;
5550 	}
5551 
5552 	if (processor) {
5553 		pset = change_locked_pset(pset, processor->processor_set);
5554 		/* Check that chosen processor is still usable */
5555 		cpumap_t available_map = pset_available_cpumap(pset);
5556 		if (bit_test(available_map, processor->cpu_id)) {
5557 			pset->last_chosen = processor->cpu_id;
5558 			return processor;
5559 		}
5560 
5561 		/* processor is no longer usable */
5562 		processor = PROCESSOR_NULL;
5563 	}
5564 
5565 	pset_assert_locked(pset);
5566 	pset_unlock(pset);
5567 	return PROCESSOR_NULL;
5568 }
5569 
5570 /*
5571  * Default implementation of SCHED(choose_node)()
5572  * for single node systems
5573  */
5574 pset_node_t
sched_choose_node(__unused thread_t thread)5575 sched_choose_node(__unused thread_t thread)
5576 {
5577 	return &pset_node0;
5578 }
5579 
5580 /*
5581  *	choose_starting_pset:
5582  *
5583  *	Choose a starting processor set for the thread.
5584  *	May return a processor hint within the pset.
5585  *
5586  *	Returns a starting processor set, to be used by
5587  *      choose_processor.
5588  *
5589  *	The thread must be locked.  The resulting pset is unlocked on return,
5590  *      and is chosen without taking any pset locks.
5591  */
5592 processor_set_t
choose_starting_pset(pset_node_t node,thread_t thread,processor_t * processor_hint)5593 choose_starting_pset(pset_node_t node, thread_t thread, processor_t *processor_hint)
5594 {
5595 	processor_set_t pset;
5596 	processor_t processor = PROCESSOR_NULL;
5597 
5598 	if (thread->affinity_set != AFFINITY_SET_NULL) {
5599 		/*
5600 		 * Use affinity set policy hint.
5601 		 */
5602 		pset = thread->affinity_set->aset_pset;
5603 	} else if (thread->last_processor != PROCESSOR_NULL) {
5604 		/*
5605 		 *	Simple (last processor) affinity case.
5606 		 */
5607 		processor = thread->last_processor;
5608 		pset = processor->processor_set;
5609 	} else {
5610 		/*
5611 		 *	No Affinity case:
5612 		 *
5613 		 *	Utilitize a per task hint to spread threads
5614 		 *	among the available processor sets.
5615 		 * NRG this seems like the wrong thing to do.
5616 		 * See also task->pset_hint = pset in thread_setrun()
5617 		 */
5618 		pset = get_threadtask(thread)->pset_hint;
5619 		if (pset == PROCESSOR_SET_NULL) {
5620 			pset = current_processor()->processor_set;
5621 		}
5622 
5623 		pset = choose_next_pset(pset);
5624 	}
5625 
5626 	if (!bit_test(node->pset_map, pset->pset_id)) {
5627 		/* pset is not from this node so choose one that is */
5628 		int id = lsb_first(node->pset_map);
5629 		if (id < 0) {
5630 			/* startup race, so check again under the node lock */
5631 			lck_spin_lock(&pset_node_lock);
5632 			if (bit_test(node->pset_map, pset->pset_id)) {
5633 				id = pset->pset_id;
5634 			} else {
5635 				id = lsb_first(node->pset_map);
5636 			}
5637 			lck_spin_unlock(&pset_node_lock);
5638 		}
5639 		assert(id >= 0);
5640 		pset = pset_array[id];
5641 	}
5642 
5643 	if (bit_count(node->pset_map) == 1) {
5644 		/* Only a single pset in this node */
5645 		goto out;
5646 	}
5647 
5648 	bool avoid_cpu0 = false;
5649 
5650 #if defined(__x86_64__)
5651 	if ((thread->sched_pri >= BASEPRI_RTQUEUES) && sched_avoid_cpu0) {
5652 		/* Avoid the pset containing cpu0 */
5653 		avoid_cpu0 = true;
5654 		/* Assert that cpu0 is in pset0.  I expect this to be true on __x86_64__ */
5655 		assert(bit_test(pset_array[0]->cpu_bitmask, 0));
5656 	}
5657 #endif
5658 
5659 	if (thread->sched_pri >= BASEPRI_RTQUEUES) {
5660 		pset_map_t rt_target_map = atomic_load(&node->pset_non_rt_primary_map);
5661 		if ((avoid_cpu0 && pset->pset_id == 0) || !bit_test(rt_target_map, pset->pset_id)) {
5662 			if (avoid_cpu0) {
5663 				rt_target_map = bit_ror64(rt_target_map, 1);
5664 			}
5665 			int rotid = lsb_first(rt_target_map);
5666 			if (rotid >= 0) {
5667 				int id = avoid_cpu0 ? ((rotid + 1) & 63) : rotid;
5668 				pset = pset_array[id];
5669 				goto out;
5670 			}
5671 		}
5672 		if (!pset->is_SMT || !sched_allow_rt_smt) {
5673 			/* All psets are full of RT threads - fall back to choose processor to find the furthest deadline RT thread */
5674 			goto out;
5675 		}
5676 		rt_target_map = atomic_load(&node->pset_non_rt_map);
5677 		if ((avoid_cpu0 && pset->pset_id == 0) || !bit_test(rt_target_map, pset->pset_id)) {
5678 			if (avoid_cpu0) {
5679 				rt_target_map = bit_ror64(rt_target_map, 1);
5680 			}
5681 			int rotid = lsb_first(rt_target_map);
5682 			if (rotid >= 0) {
5683 				int id = avoid_cpu0 ? ((rotid + 1) & 63) : rotid;
5684 				pset = pset_array[id];
5685 				goto out;
5686 			}
5687 		}
5688 		/* All psets are full of RT threads - fall back to choose processor to find the furthest deadline RT thread */
5689 	} else {
5690 		pset_map_t idle_map = atomic_load(&node->pset_idle_map);
5691 		if (!bit_test(idle_map, pset->pset_id)) {
5692 			int next_idle_pset_id = lsb_first(idle_map);
5693 			if (next_idle_pset_id >= 0) {
5694 				pset = pset_array[next_idle_pset_id];
5695 			}
5696 		}
5697 	}
5698 
5699 out:
5700 	if ((processor != PROCESSOR_NULL) && (processor->processor_set != pset)) {
5701 		processor = PROCESSOR_NULL;
5702 	}
5703 	if (processor != PROCESSOR_NULL) {
5704 		*processor_hint = processor;
5705 	}
5706 
5707 	assert(pset != NULL);
5708 	return pset;
5709 }
5710 
5711 /*
5712  *	thread_setrun:
5713  *
5714  *	Dispatch thread for execution, onto an idle
5715  *	processor or run queue, and signal a preemption
5716  *	as appropriate.
5717  *
5718  *	Thread must be locked.
5719  */
5720 void
thread_setrun(thread_t thread,sched_options_t options)5721 thread_setrun(
5722 	thread_t                        thread,
5723 	sched_options_t                 options)
5724 {
5725 	processor_t                     processor = PROCESSOR_NULL;
5726 	processor_set_t         pset;
5727 
5728 	assert((thread->state & (TH_RUN | TH_WAIT | TH_UNINT | TH_TERMINATE | TH_TERMINATE2)) == TH_RUN);
5729 	assert(thread->runq == PROCESSOR_NULL);
5730 
5731 #if CONFIG_PREADOPT_TG
5732 	/* We know that the thread is not in the runq by virtue of being in this
5733 	 * function and the thread is not self since we are running. We can safely
5734 	 * resolve the thread group hierarchy and modify the thread's thread group
5735 	 * here. */
5736 	thread_resolve_and_enforce_thread_group_hierarchy_if_needed(thread);
5737 #endif
5738 
5739 	/*
5740 	 *	Update priority if needed.
5741 	 */
5742 	if (SCHED(can_update_priority)(thread)) {
5743 		SCHED(update_priority)(thread);
5744 	}
5745 	thread->sfi_class = sfi_thread_classify(thread);
5746 
5747 	if (thread->bound_processor == PROCESSOR_NULL) {
5748 		/*
5749 		 * Unbound case.
5750 		 *
5751 		 * Usually, this loop will only be executed once,
5752 		 * but if CLPC derecommends a processor after it has been chosen,
5753 		 * or if a processor is shut down after it is chosen,
5754 		 * choose_processor() may return NULL, so a retry
5755 		 * may be necessary.  A single retry will usually
5756 		 * be enough, and we can't afford to retry too many times
5757 		 * because interrupts are disabled.
5758 		 */
5759 #define CHOOSE_PROCESSOR_MAX_RETRIES 3
5760 		for (int retry = 0; retry <= CHOOSE_PROCESSOR_MAX_RETRIES; retry++) {
5761 			processor_t processor_hint = PROCESSOR_NULL;
5762 			pset_node_t node = SCHED(choose_node)(thread);
5763 			processor_set_t starting_pset = choose_starting_pset(node, thread, &processor_hint);
5764 
5765 			pset_lock(starting_pset);
5766 
5767 			processor = SCHED(choose_processor)(starting_pset, processor_hint, thread);
5768 			if (processor != PROCESSOR_NULL) {
5769 				pset = processor->processor_set;
5770 				pset_assert_locked(pset);
5771 				break;
5772 			}
5773 		}
5774 		/*
5775 		 * If choose_processor() still returns NULL,
5776 		 * which is very unlikely,
5777 		 * choose the master_processor, which is always
5778 		 * safe to choose.
5779 		 */
5780 		if (processor == PROCESSOR_NULL) {
5781 			/* Choose fallback processor */
5782 			processor = master_processor;
5783 			pset = processor->processor_set;
5784 			pset_lock(pset);
5785 			assert((pset_available_cpu_count(pset) > 0) || (processor->state != PROCESSOR_OFF_LINE && processor->is_recommended));
5786 		}
5787 		task_t task = get_threadtask(thread);
5788 		if (!(task->t_flags & TF_USE_PSET_HINT_CLUSTER_TYPE)) {
5789 			task->pset_hint = pset; /* NRG this is done without holding the task lock */
5790 		}
5791 		SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT_IST(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHOOSE_PROCESSOR) | DBG_FUNC_NONE,
5792 		    (uintptr_t)thread_tid(thread), (uintptr_t)-1, processor->cpu_id, processor->state, 0);
5793 		assert((pset_available_cpu_count(pset) > 0) || (processor->state != PROCESSOR_OFF_LINE && processor->is_recommended));
5794 	} else {
5795 		/*
5796 		 *	Bound case:
5797 		 *
5798 		 *	Unconditionally dispatch on the processor.
5799 		 */
5800 		processor = thread->bound_processor;
5801 		pset = processor->processor_set;
5802 		pset_lock(pset);
5803 
5804 		SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT_IST(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHOOSE_PROCESSOR) | DBG_FUNC_NONE,
5805 		    (uintptr_t)thread_tid(thread), (uintptr_t)-2, processor->cpu_id, processor->state, 0);
5806 	}
5807 
5808 	/*
5809 	 *	Dispatch the thread on the chosen processor.
5810 	 *	TODO: This should be based on sched_mode, not sched_pri
5811 	 */
5812 	if (thread->sched_pri >= BASEPRI_RTQUEUES) {
5813 		realtime_setrun(processor, thread);
5814 	} else {
5815 		processor_setrun(processor, thread, options);
5816 	}
5817 	/* pset is now unlocked */
5818 	if (thread->bound_processor == PROCESSOR_NULL) {
5819 		SCHED(check_spill)(pset, thread);
5820 	}
5821 }
5822 
5823 processor_set_t
task_choose_pset(task_t task)5824 task_choose_pset(
5825 	task_t          task)
5826 {
5827 	processor_set_t         pset = task->pset_hint;
5828 
5829 	if (pset != PROCESSOR_SET_NULL) {
5830 		pset = choose_next_pset(pset);
5831 	}
5832 
5833 	return pset;
5834 }
5835 
5836 /*
5837  *	Check for a preemption point in
5838  *	the current context.
5839  *
5840  *	Called at splsched with thread locked.
5841  */
5842 ast_t
csw_check(thread_t thread,processor_t processor,ast_t check_reason)5843 csw_check(
5844 	thread_t                thread,
5845 	processor_t             processor,
5846 	ast_t                   check_reason)
5847 {
5848 	processor_set_t pset = processor->processor_set;
5849 
5850 	assert(thread == processor->active_thread);
5851 
5852 	pset_lock(pset);
5853 
5854 	processor_state_update_from_thread(processor, thread, true);
5855 
5856 	ast_t preempt = csw_check_locked(thread, processor, pset, check_reason);
5857 
5858 	/* Acknowledge the IPI if we decided not to preempt */
5859 
5860 	if ((preempt & AST_URGENT) == 0) {
5861 		if (bit_clear_if_set(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
5862 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PENDING_AST_URGENT) | DBG_FUNC_END, processor->cpu_id, pset->pending_AST_URGENT_cpu_mask, 0, 8);
5863 		}
5864 	}
5865 
5866 	if ((preempt & AST_PREEMPT) == 0) {
5867 		bit_clear(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
5868 	}
5869 
5870 	pset_unlock(pset);
5871 
5872 	return update_pending_nonurgent_preemption(processor, preempt);
5873 }
5874 
5875 void
clear_pending_nonurgent_preemption(processor_t processor)5876 clear_pending_nonurgent_preemption(processor_t processor)
5877 {
5878 	if (!processor->pending_nonurgent_preemption) {
5879 		return;
5880 	}
5881 
5882 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_PREEMPT_TIMER_ACTIVE) | DBG_FUNC_END);
5883 
5884 	processor->pending_nonurgent_preemption = false;
5885 	running_timer_clear(processor, RUNNING_TIMER_PREEMPT);
5886 }
5887 
5888 ast_t
update_pending_nonurgent_preemption(processor_t processor,ast_t reason)5889 update_pending_nonurgent_preemption(processor_t processor, ast_t reason)
5890 {
5891 	if ((reason & (AST_URGENT | AST_PREEMPT)) != (AST_PREEMPT)) {
5892 		clear_pending_nonurgent_preemption(processor);
5893 		return reason;
5894 	}
5895 
5896 	if (nonurgent_preemption_timer_abs == 0) {
5897 		/* Preemption timer not enabled */
5898 		return reason;
5899 	}
5900 
5901 	if (current_thread()->state & TH_IDLE) {
5902 		/* idle threads don't need nonurgent preemption */
5903 		return reason;
5904 	}
5905 
5906 	if (processor->pending_nonurgent_preemption) {
5907 		/* Timer is already armed, no need to do it again */
5908 		return reason;
5909 	}
5910 
5911 	if (ml_did_interrupt_userspace()) {
5912 		/*
5913 		 * We're preempting userspace here, so we don't need
5914 		 * to defer the preemption.  Force AST_URGENT
5915 		 * so that we can avoid arming this timer without risking
5916 		 * ast_taken_user deciding to spend too long in kernel
5917 		 * space to handle other ASTs.
5918 		 */
5919 
5920 		return reason | AST_URGENT;
5921 	}
5922 
5923 	/*
5924 	 * We've decided to do a nonurgent preemption when running in
5925 	 * kernelspace. We defer the preemption until reaching userspace boundary
5926 	 * to give a grace period for locks etc to be dropped and to reach
5927 	 * a clean preemption point, so that the preempting thread doesn't
5928 	 * always immediately hit the lock that the waking thread still holds.
5929 	 *
5930 	 * Arm a timer to enforce that the preemption executes within a bounded
5931 	 * time if the thread doesn't block or return to userspace quickly.
5932 	 */
5933 
5934 	processor->pending_nonurgent_preemption = true;
5935 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_PREEMPT_TIMER_ACTIVE) | DBG_FUNC_START,
5936 	    reason);
5937 
5938 	uint64_t now = mach_absolute_time();
5939 
5940 	uint64_t deadline = now + nonurgent_preemption_timer_abs;
5941 
5942 	running_timer_enter(processor, RUNNING_TIMER_PREEMPT, NULL,
5943 	    now, deadline);
5944 
5945 	return reason;
5946 }
5947 
5948 /*
5949  * Check for preemption at splsched with
5950  * pset and thread locked
5951  */
5952 ast_t
csw_check_locked(thread_t thread,processor_t processor,processor_set_t pset,ast_t check_reason)5953 csw_check_locked(
5954 	thread_t                thread,
5955 	processor_t             processor,
5956 	processor_set_t         pset,
5957 	ast_t                   check_reason)
5958 {
5959 	/*
5960 	 * If the current thread is running on a processor that is no longer recommended,
5961 	 * urgently preempt it, at which point thread_select() should
5962 	 * try to idle the processor and re-dispatch the thread to a recommended processor.
5963 	 */
5964 	if (!processor->is_recommended) {
5965 		return check_reason | AST_PREEMPT | AST_URGENT;
5966 	}
5967 
5968 	if (bit_test(pset->rt_pending_spill_cpu_mask, processor->cpu_id)) {
5969 		return check_reason | AST_PREEMPT | AST_URGENT;
5970 	}
5971 
5972 	if (rt_runq_count(pset) > 0) {
5973 		if ((rt_runq_priority(pset) > processor->current_pri) || !processor->first_timeslice) {
5974 			return check_reason | AST_PREEMPT | AST_URGENT;
5975 		} else if (deadline_add(rt_runq_earliest_deadline(pset), rt_deadline_epsilon) < processor->deadline) {
5976 			return check_reason | AST_PREEMPT | AST_URGENT;
5977 		} else {
5978 			return check_reason | AST_PREEMPT;
5979 		}
5980 	}
5981 
5982 	ast_t result = SCHED(processor_csw_check)(processor);
5983 	if (result != AST_NONE) {
5984 		return check_reason | result | (thread_is_eager_preempt(thread) ? AST_URGENT : AST_NONE);
5985 	}
5986 
5987 	/*
5988 	 * Same for avoid-processor
5989 	 *
5990 	 * TODO: Should these set AST_REBALANCE?
5991 	 */
5992 	if (SCHED(avoid_processor_enabled) && SCHED(thread_avoid_processor)(processor, thread, check_reason)) {
5993 		return check_reason | AST_PREEMPT;
5994 	}
5995 
5996 	/*
5997 	 * Even though we could continue executing on this processor, a
5998 	 * secondary SMT core should try to shed load to another primary core.
5999 	 *
6000 	 * TODO: Should this do the same check that thread_select does? i.e.
6001 	 * if no bound threads target this processor, and idle primaries exist, preempt
6002 	 * The case of RT threads existing is already taken care of above
6003 	 */
6004 
6005 	if (processor->current_pri < BASEPRI_RTQUEUES &&
6006 	    processor->processor_primary != processor) {
6007 		return check_reason | AST_PREEMPT;
6008 	}
6009 
6010 	if (thread->state & TH_SUSP) {
6011 		return check_reason | AST_PREEMPT;
6012 	}
6013 
6014 #if CONFIG_SCHED_SFI
6015 	/*
6016 	 * Current thread may not need to be preempted, but maybe needs
6017 	 * an SFI wait?
6018 	 */
6019 	result = sfi_thread_needs_ast(thread, NULL);
6020 	if (result != AST_NONE) {
6021 		return result;
6022 	}
6023 #endif
6024 
6025 	return AST_NONE;
6026 }
6027 
6028 /*
6029  * Handle preemption IPI or IPI in response to setting an AST flag
6030  * Triggered by cause_ast_check
6031  * Called at splsched
6032  */
6033 void
ast_check(processor_t processor)6034 ast_check(processor_t processor)
6035 {
6036 	smr_ack_ipi();
6037 
6038 	if (processor->state != PROCESSOR_RUNNING &&
6039 	    processor->state != PROCESSOR_SHUTDOWN) {
6040 		return;
6041 	}
6042 
6043 	SCHED_DEBUG_AST_CHECK_KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_SCHED,
6044 	    MACH_SCHED_AST_CHECK) | DBG_FUNC_START);
6045 
6046 	thread_t thread = processor->active_thread;
6047 
6048 	assert(thread == current_thread());
6049 
6050 	/*
6051 	 * Pairs with task_restartable_ranges_synchronize
6052 	 */
6053 	thread_lock(thread);
6054 
6055 	thread_reset_pcs_ack_IPI(thread);
6056 
6057 	/*
6058 	 * Propagate thread ast to processor.
6059 	 * (handles IPI in response to setting AST flag)
6060 	 */
6061 	ast_propagate(thread);
6062 
6063 	/*
6064 	 * Stash the old urgency and perfctl values to find out if
6065 	 * csw_check updates them.
6066 	 */
6067 	thread_urgency_t old_urgency = processor->current_urgency;
6068 	perfcontrol_class_t old_perfctl_class = processor->current_perfctl_class;
6069 
6070 	ast_t preempt;
6071 
6072 	if ((preempt = csw_check(thread, processor, AST_NONE)) != AST_NONE) {
6073 		ast_on(preempt);
6074 	}
6075 
6076 	if (old_urgency != processor->current_urgency) {
6077 		/*
6078 		 * Urgency updates happen with the thread lock held (ugh).
6079 		 * TODO: This doesn't notice QoS changes...
6080 		 */
6081 		uint64_t urgency_param1, urgency_param2;
6082 
6083 		thread_urgency_t urgency = thread_get_urgency(thread, &urgency_param1, &urgency_param2);
6084 		thread_tell_urgency(urgency, urgency_param1, urgency_param2, 0, thread);
6085 	}
6086 
6087 	thread_unlock(thread);
6088 
6089 	if (old_perfctl_class != processor->current_perfctl_class) {
6090 		/*
6091 		 * We updated the perfctl class of this thread from another core.
6092 		 * Let CLPC know that the currently running thread has a new
6093 		 * class.
6094 		 */
6095 
6096 		machine_switch_perfcontrol_state_update(PERFCONTROL_ATTR_UPDATE,
6097 		    mach_approximate_time(), 0, thread);
6098 	}
6099 
6100 	SCHED_DEBUG_AST_CHECK_KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_SCHED,
6101 	    MACH_SCHED_AST_CHECK) | DBG_FUNC_END, preempt);
6102 }
6103 
6104 
6105 void
thread_preempt_expire(timer_call_param_t p0,__unused timer_call_param_t p1)6106 thread_preempt_expire(
6107 	timer_call_param_t      p0,
6108 	__unused timer_call_param_t      p1)
6109 {
6110 	processor_t processor = p0;
6111 
6112 	assert(processor == current_processor());
6113 	assert(p1 == NULL);
6114 
6115 	thread_t thread = current_thread();
6116 
6117 	/*
6118 	 * This is set and cleared by the current core, so we will
6119 	 * never see a race with running timer expiration
6120 	 */
6121 	assert(processor->pending_nonurgent_preemption);
6122 
6123 	clear_pending_nonurgent_preemption(processor);
6124 
6125 	thread_lock(thread);
6126 
6127 	/*
6128 	 * Check again to see if it's still worth a
6129 	 * context switch, but this time force enable kernel preemption
6130 	 */
6131 
6132 	ast_t preempt = csw_check(thread, processor, AST_URGENT);
6133 
6134 	if (preempt) {
6135 		ast_on(preempt);
6136 	}
6137 
6138 	thread_unlock(thread);
6139 
6140 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_PREEMPT_TIMER_ACTIVE), preempt);
6141 }
6142 
6143 
6144 /*
6145  *	set_sched_pri:
6146  *
6147  *	Set the scheduled priority of the specified thread.
6148  *
6149  *	This may cause the thread to change queues.
6150  *
6151  *	Thread must be locked.
6152  */
6153 void
set_sched_pri(thread_t thread,int16_t new_priority,set_sched_pri_options_t options)6154 set_sched_pri(
6155 	thread_t        thread,
6156 	int16_t         new_priority,
6157 	set_sched_pri_options_t options)
6158 {
6159 	bool is_current_thread = (thread == current_thread());
6160 	bool removed_from_runq = false;
6161 	bool lazy_update = ((options & SETPRI_LAZY) == SETPRI_LAZY);
6162 
6163 	int16_t old_priority = thread->sched_pri;
6164 
6165 	/* If we're already at this priority, no need to mess with the runqueue */
6166 	if (new_priority == old_priority) {
6167 #if CONFIG_SCHED_CLUTCH
6168 		/* For the first thread in the system, the priority is correct but
6169 		 * th_sched_bucket is still TH_BUCKET_RUN. Since the clutch
6170 		 * scheduler relies on the bucket being set for all threads, update
6171 		 * its bucket here.
6172 		 */
6173 		if (thread->th_sched_bucket == TH_BUCKET_RUN) {
6174 			assert(thread == vm_pageout_scan_thread);
6175 			SCHED(update_thread_bucket)(thread);
6176 		}
6177 #endif /* CONFIG_SCHED_CLUTCH */
6178 
6179 		return;
6180 	}
6181 
6182 	if (is_current_thread) {
6183 		assert(thread->state & TH_RUN);
6184 		assert(thread->runq == PROCESSOR_NULL);
6185 	} else {
6186 		removed_from_runq = thread_run_queue_remove(thread);
6187 	}
6188 
6189 	thread->sched_pri = new_priority;
6190 
6191 #if CONFIG_SCHED_CLUTCH
6192 	/*
6193 	 * Since for the clutch scheduler, the thread's bucket determines its runq
6194 	 * in the hierarchy it is important to update the bucket when the thread
6195 	 * lock is held and the thread has been removed from the runq hierarchy.
6196 	 */
6197 	SCHED(update_thread_bucket)(thread);
6198 
6199 #endif /* CONFIG_SCHED_CLUTCH */
6200 
6201 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHANGE_PRIORITY),
6202 	    (uintptr_t)thread_tid(thread),
6203 	    thread->base_pri,
6204 	    thread->sched_pri,
6205 	    thread->sched_usage,
6206 	    0);
6207 
6208 	if (removed_from_runq) {
6209 		thread_run_queue_reinsert(thread, SCHED_PREEMPT | SCHED_TAILQ);
6210 	} else if (is_current_thread) {
6211 		processor_t processor = thread->last_processor;
6212 		assert(processor == current_processor());
6213 
6214 		thread_urgency_t old_urgency = processor->current_urgency;
6215 
6216 		/*
6217 		 * When dropping in priority, check if the thread no longer belongs on core.
6218 		 * If a thread raises its own priority, don't aggressively rebalance it.
6219 		 * <rdar://problem/31699165>
6220 		 *
6221 		 * csw_check does a processor_state_update_from_thread, but
6222 		 * we should do our own if we're being lazy.
6223 		 */
6224 		if (!lazy_update && new_priority < old_priority) {
6225 			ast_t preempt;
6226 
6227 			if ((preempt = csw_check(thread, processor, AST_NONE)) != AST_NONE) {
6228 				ast_on(preempt);
6229 			}
6230 		} else {
6231 			processor_state_update_from_thread(processor, thread, false);
6232 		}
6233 
6234 		/*
6235 		 * set_sched_pri doesn't alter RT params. We expect direct base priority/QoS
6236 		 * class alterations from user space to occur relatively infrequently, hence
6237 		 * those are lazily handled. QoS classes have distinct priority bands, and QoS
6238 		 * inheritance is expected to involve priority changes.
6239 		 */
6240 		if (processor->current_urgency != old_urgency) {
6241 			uint64_t urgency_param1, urgency_param2;
6242 
6243 			thread_urgency_t new_urgency = thread_get_urgency(thread,
6244 			    &urgency_param1, &urgency_param2);
6245 
6246 			thread_tell_urgency(new_urgency, urgency_param1,
6247 			    urgency_param2, 0, thread);
6248 		}
6249 
6250 		/* TODO: only call this if current_perfctl_class changed */
6251 		uint64_t ctime = mach_approximate_time();
6252 		machine_thread_going_on_core(thread, processor->current_urgency, 0, 0, ctime);
6253 	} else if (thread->state & TH_RUN) {
6254 		processor_t processor = thread->last_processor;
6255 
6256 		if (!lazy_update &&
6257 		    processor != PROCESSOR_NULL &&
6258 		    processor != current_processor() &&
6259 		    processor->active_thread == thread) {
6260 			cause_ast_check(processor);
6261 		}
6262 	}
6263 }
6264 
6265 /*
6266  * thread_run_queue_remove_for_handoff
6267  *
6268  * Pull a thread or its (recursive) push target out of the runqueue
6269  * so that it is ready for thread_run()
6270  *
6271  * Called at splsched
6272  *
6273  * Returns the thread that was pulled or THREAD_NULL if no thread could be pulled.
6274  * This may be different than the thread that was passed in.
6275  */
6276 thread_t
thread_run_queue_remove_for_handoff(thread_t thread)6277 thread_run_queue_remove_for_handoff(thread_t thread)
6278 {
6279 	thread_t pulled_thread = THREAD_NULL;
6280 
6281 	thread_lock(thread);
6282 
6283 	/*
6284 	 * Check that the thread is not bound to a different processor,
6285 	 * NO_SMT flag is not set on the thread, cluster type of
6286 	 * processor matches with thread if the thread is pinned to a
6287 	 * particular cluster and that realtime is not involved.
6288 	 *
6289 	 * Next, pull it off its run queue.  If it doesn't come, it's not eligible.
6290 	 */
6291 	processor_t processor = current_processor();
6292 	if ((thread->bound_processor == PROCESSOR_NULL || thread->bound_processor == processor)
6293 	    && (!thread_no_smt(thread))
6294 	    && (processor->current_pri < BASEPRI_RTQUEUES)
6295 	    && (thread->sched_pri < BASEPRI_RTQUEUES)
6296 #if __AMP__
6297 	    && ((thread->th_bound_cluster_id == THREAD_BOUND_CLUSTER_NONE) ||
6298 	    processor->processor_set->pset_id == thread->th_bound_cluster_id)
6299 #endif /* __AMP__ */
6300 	    ) {
6301 		if (thread_run_queue_remove(thread)) {
6302 			pulled_thread = thread;
6303 		}
6304 	}
6305 
6306 	thread_unlock(thread);
6307 
6308 	return pulled_thread;
6309 }
6310 
6311 /*
6312  * thread_prepare_for_handoff
6313  *
6314  * Make the thread ready for handoff.
6315  * If the thread was runnable then pull it off the runq, if the thread could
6316  * not be pulled, return NULL.
6317  *
6318  * If the thread was woken up from wait for handoff, make sure it is not bound to
6319  * different processor.
6320  *
6321  * Called at splsched
6322  *
6323  * Returns the thread that was pulled or THREAD_NULL if no thread could be pulled.
6324  * This may be different than the thread that was passed in.
6325  */
6326 thread_t
thread_prepare_for_handoff(thread_t thread,thread_handoff_option_t option)6327 thread_prepare_for_handoff(thread_t thread, thread_handoff_option_t option)
6328 {
6329 	thread_t pulled_thread = THREAD_NULL;
6330 
6331 	if (option & THREAD_HANDOFF_SETRUN_NEEDED) {
6332 		processor_t processor = current_processor();
6333 		thread_lock(thread);
6334 
6335 		/*
6336 		 * Check that the thread is not bound to a different processor,
6337 		 * NO_SMT flag is not set on the thread and cluster type of
6338 		 * processor matches with thread if the thread is pinned to a
6339 		 * particular cluster. Call setrun instead if above conditions
6340 		 * are not satisfied.
6341 		 */
6342 		if ((thread->bound_processor == PROCESSOR_NULL || thread->bound_processor == processor)
6343 		    && (!thread_no_smt(thread))
6344 #if __AMP__
6345 		    && ((thread->th_bound_cluster_id == THREAD_BOUND_CLUSTER_NONE) ||
6346 		    processor->processor_set->pset_id == thread->th_bound_cluster_id)
6347 #endif /* __AMP__ */
6348 		    ) {
6349 			pulled_thread = thread;
6350 		} else {
6351 			thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
6352 		}
6353 		thread_unlock(thread);
6354 	} else {
6355 		pulled_thread = thread_run_queue_remove_for_handoff(thread);
6356 	}
6357 
6358 	return pulled_thread;
6359 }
6360 
6361 /*
6362  *	thread_run_queue_remove:
6363  *
6364  *	Remove a thread from its current run queue and
6365  *	return TRUE if successful.
6366  *
6367  *	Thread must be locked.
6368  *
6369  *	If thread->runq is PROCESSOR_NULL, the thread will not re-enter the
6370  *	run queues because the caller locked the thread.  Otherwise
6371  *	the thread is on a run queue, but could be chosen for dispatch
6372  *	and removed by another processor under a different lock, which
6373  *	will set thread->runq to PROCESSOR_NULL.
6374  *
6375  *	Hence the thread select path must not rely on anything that could
6376  *	be changed under the thread lock after calling this function,
6377  *	most importantly thread->sched_pri.
6378  */
6379 boolean_t
thread_run_queue_remove(thread_t thread)6380 thread_run_queue_remove(
6381 	thread_t        thread)
6382 {
6383 	boolean_t removed = FALSE;
6384 	processor_t processor = thread->runq;
6385 
6386 	if ((thread->state & (TH_RUN | TH_WAIT)) == TH_WAIT) {
6387 		/* Thread isn't runnable */
6388 		assert(thread->runq == PROCESSOR_NULL);
6389 		return FALSE;
6390 	}
6391 
6392 	if (processor == PROCESSOR_NULL) {
6393 		/*
6394 		 * The thread is either not on the runq,
6395 		 * or is in the midst of being removed from the runq.
6396 		 *
6397 		 * runq is set to NULL under the pset lock, not the thread
6398 		 * lock, so the thread may still be in the process of being dequeued
6399 		 * from the runq. It will wait in invoke for the thread lock to be
6400 		 * dropped.
6401 		 */
6402 
6403 		return FALSE;
6404 	}
6405 
6406 	if (thread->sched_pri < BASEPRI_RTQUEUES) {
6407 		return SCHED(processor_queue_remove)(processor, thread);
6408 	}
6409 
6410 	processor_set_t pset = processor->processor_set;
6411 
6412 	pset_lock(pset);
6413 
6414 	if (thread->runq != PROCESSOR_NULL) {
6415 		/*
6416 		 *	Thread is on the RT run queue and we have a lock on
6417 		 *	that run queue.
6418 		 */
6419 		rt_runq_remove(SCHED(rt_runq)(pset), thread);
6420 		pset_update_rt_stealable_state(pset);
6421 
6422 		removed = TRUE;
6423 	}
6424 
6425 	pset_unlock(pset);
6426 
6427 	return removed;
6428 }
6429 
6430 /*
6431  * Put the thread back where it goes after a thread_run_queue_remove
6432  *
6433  * Thread must have been removed under the same thread lock hold
6434  *
6435  * thread locked, at splsched
6436  */
6437 void
thread_run_queue_reinsert(thread_t thread,sched_options_t options)6438 thread_run_queue_reinsert(thread_t thread, sched_options_t options)
6439 {
6440 	assert(thread->runq == PROCESSOR_NULL);
6441 	assert(thread->state & (TH_RUN));
6442 
6443 	thread_setrun(thread, options);
6444 }
6445 
6446 void
sys_override_cpu_throttle(boolean_t enable_override)6447 sys_override_cpu_throttle(boolean_t enable_override)
6448 {
6449 	if (enable_override) {
6450 		cpu_throttle_enabled = 0;
6451 	} else {
6452 		cpu_throttle_enabled = 1;
6453 	}
6454 }
6455 
6456 thread_urgency_t
thread_get_urgency(thread_t thread,uint64_t * arg1,uint64_t * arg2)6457 thread_get_urgency(thread_t thread, uint64_t *arg1, uint64_t *arg2)
6458 {
6459 	uint64_t urgency_param1 = 0, urgency_param2 = 0;
6460 	task_t task = get_threadtask_early(thread);
6461 
6462 	thread_urgency_t urgency;
6463 
6464 	if (thread == NULL || task == TASK_NULL || (thread->state & TH_IDLE)) {
6465 		urgency_param1 = 0;
6466 		urgency_param2 = 0;
6467 
6468 		urgency = THREAD_URGENCY_NONE;
6469 	} else if (thread->sched_mode == TH_MODE_REALTIME) {
6470 		urgency_param1 = thread->realtime.period;
6471 		urgency_param2 = thread->realtime.deadline;
6472 
6473 		urgency = THREAD_URGENCY_REAL_TIME;
6474 	} else if (cpu_throttle_enabled &&
6475 	    (thread->sched_pri <= MAXPRI_THROTTLE) &&
6476 	    (thread->base_pri <= MAXPRI_THROTTLE)) {
6477 		/*
6478 		 * Threads that are running at low priority but are not
6479 		 * tagged with a specific QoS are separated out from
6480 		 * the "background" urgency. Performance management
6481 		 * subsystem can decide to either treat these threads
6482 		 * as normal threads or look at other signals like thermal
6483 		 * levels for optimal power/perf tradeoffs for a platform.
6484 		 */
6485 		boolean_t thread_lacks_qos = (proc_get_effective_thread_policy(thread, TASK_POLICY_QOS) == THREAD_QOS_UNSPECIFIED); //thread_has_qos_policy(thread);
6486 		boolean_t task_is_suppressed = (proc_get_effective_task_policy(task, TASK_POLICY_SUP_ACTIVE) == 0x1);
6487 
6488 		/*
6489 		 * Background urgency applied when thread priority is
6490 		 * MAXPRI_THROTTLE or lower and thread is not promoted
6491 		 * and thread has a QoS specified
6492 		 */
6493 		urgency_param1 = thread->sched_pri;
6494 		urgency_param2 = thread->base_pri;
6495 
6496 		if (thread_lacks_qos && !task_is_suppressed) {
6497 			urgency = THREAD_URGENCY_LOWPRI;
6498 		} else {
6499 			urgency = THREAD_URGENCY_BACKGROUND;
6500 		}
6501 	} else {
6502 		/* For otherwise unclassified threads, report throughput QoS parameters */
6503 		urgency_param1 = proc_get_effective_thread_policy(thread, TASK_POLICY_THROUGH_QOS);
6504 		urgency_param2 = proc_get_effective_task_policy(task, TASK_POLICY_THROUGH_QOS);
6505 		urgency = THREAD_URGENCY_NORMAL;
6506 	}
6507 
6508 	if (arg1 != NULL) {
6509 		*arg1 = urgency_param1;
6510 	}
6511 	if (arg2 != NULL) {
6512 		*arg2 = urgency_param2;
6513 	}
6514 
6515 	return urgency;
6516 }
6517 
6518 perfcontrol_class_t
thread_get_perfcontrol_class(thread_t thread)6519 thread_get_perfcontrol_class(thread_t thread)
6520 {
6521 	/* Special case handling */
6522 	if (thread->state & TH_IDLE) {
6523 		return PERFCONTROL_CLASS_IDLE;
6524 	}
6525 
6526 	if (thread->sched_mode == TH_MODE_REALTIME) {
6527 		return PERFCONTROL_CLASS_REALTIME;
6528 	}
6529 
6530 	/* perfcontrol_class based on base_pri */
6531 	if (thread->base_pri <= MAXPRI_THROTTLE) {
6532 		return PERFCONTROL_CLASS_BACKGROUND;
6533 	} else if (thread->base_pri <= BASEPRI_UTILITY) {
6534 		return PERFCONTROL_CLASS_UTILITY;
6535 	} else if (thread->base_pri <= BASEPRI_DEFAULT) {
6536 		return PERFCONTROL_CLASS_NONUI;
6537 	} else if (thread->base_pri <= BASEPRI_USER_INITIATED) {
6538 		return PERFCONTROL_CLASS_USER_INITIATED;
6539 	} else if (thread->base_pri <= BASEPRI_FOREGROUND) {
6540 		return PERFCONTROL_CLASS_UI;
6541 	} else {
6542 		if (get_threadtask(thread) == kernel_task) {
6543 			/*
6544 			 * Classify Above UI kernel threads as PERFCONTROL_CLASS_KERNEL.
6545 			 * All other lower priority kernel threads should be treated
6546 			 * as regular threads for performance control purposes.
6547 			 */
6548 			return PERFCONTROL_CLASS_KERNEL;
6549 		}
6550 		return PERFCONTROL_CLASS_ABOVEUI;
6551 	}
6552 }
6553 
6554 /*
6555  *	This is the processor idle loop, which just looks for other threads
6556  *	to execute.  Processor idle threads invoke this without supplying a
6557  *	current thread to idle without an asserted wait state.
6558  *
6559  *	Returns a the next thread to execute if dispatched directly.
6560  */
6561 
6562 #if 0
6563 #define IDLE_KERNEL_DEBUG_CONSTANT(...) KERNEL_DEBUG_CONSTANT(__VA_ARGS__)
6564 #else
6565 #define IDLE_KERNEL_DEBUG_CONSTANT(...) do { } while(0)
6566 #endif
6567 
6568 #if (DEVELOPMENT || DEBUG)
6569 int sched_idle_delay_cpuid = -1;
6570 #endif
6571 
6572 thread_t
processor_idle(thread_t thread,processor_t processor)6573 processor_idle(
6574 	thread_t                        thread,
6575 	processor_t                     processor)
6576 {
6577 	processor_set_t         pset = processor->processor_set;
6578 	struct recount_snap snap = { 0 };
6579 
6580 	(void)splsched();
6581 
6582 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
6583 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_IDLE) | DBG_FUNC_START,
6584 	    (uintptr_t)thread_tid(thread), 0, 0, 0, 0);
6585 
6586 	SCHED_STATS_INC(idle_transitions);
6587 	assert(processor->running_timers_active == false);
6588 
6589 	recount_snapshot(&snap);
6590 	recount_processor_idle(&processor->pr_recount, &snap);
6591 
6592 	while (1) {
6593 		/*
6594 		 * Ensure that updates to my processor and pset state,
6595 		 * made by the IPI source processor before sending the IPI,
6596 		 * are visible on this processor now (even though we don't
6597 		 * take the pset lock yet).
6598 		 */
6599 		atomic_thread_fence(memory_order_acquire);
6600 
6601 		if (processor->state != PROCESSOR_IDLE) {
6602 			break;
6603 		}
6604 		if (bit_test(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
6605 			break;
6606 		}
6607 #if defined(CONFIG_SCHED_DEFERRED_AST)
6608 		if (bit_test(pset->pending_deferred_AST_cpu_mask, processor->cpu_id)) {
6609 			break;
6610 		}
6611 #endif
6612 		if (bit_test(pset->rt_pending_spill_cpu_mask, processor->cpu_id)) {
6613 			break;
6614 		}
6615 
6616 		if (processor->is_recommended && (processor->processor_primary == processor)) {
6617 			if (rt_runq_count(pset)) {
6618 				break;
6619 			}
6620 		} else {
6621 			if (SCHED(processor_bound_count)(processor)) {
6622 				break;
6623 			}
6624 		}
6625 
6626 		IDLE_KERNEL_DEBUG_CONSTANT(
6627 			MACHDBG_CODE(DBG_MACH_SCHED, MACH_IDLE) | DBG_FUNC_NONE, (uintptr_t)thread_tid(thread), rt_runq_count(pset), SCHED(processor_runq_count)(processor), -1, 0);
6628 
6629 		machine_track_platform_idle(TRUE);
6630 
6631 		machine_idle();
6632 		/* returns with interrupts enabled */
6633 
6634 		machine_track_platform_idle(FALSE);
6635 
6636 #if (DEVELOPMENT || DEBUG)
6637 		if (processor->cpu_id == sched_idle_delay_cpuid) {
6638 			delay(500);
6639 		}
6640 #endif
6641 
6642 		(void)splsched();
6643 
6644 		atomic_thread_fence(memory_order_acquire);
6645 
6646 		IDLE_KERNEL_DEBUG_CONSTANT(
6647 			MACHDBG_CODE(DBG_MACH_SCHED, MACH_IDLE) | DBG_FUNC_NONE, (uintptr_t)thread_tid(thread), rt_runq_count(pset), SCHED(processor_runq_count)(processor), -2, 0);
6648 
6649 		/*
6650 		 * Check if we should call sched_timeshare_consider_maintenance() here.
6651 		 * The CPU was woken out of idle due to an interrupt and we should do the
6652 		 * call only if the processor is still idle. If the processor is non-idle,
6653 		 * the threads running on the processor would do the call as part of
6654 		 * context swithing.
6655 		 */
6656 		if (processor->state == PROCESSOR_IDLE) {
6657 			sched_timeshare_consider_maintenance(mach_absolute_time(), true);
6658 		}
6659 
6660 		if (!SCHED(processor_queue_empty)(processor)) {
6661 			/* Secondary SMT processors respond to directed wakeups
6662 			 * exclusively. Some platforms induce 'spurious' SMT wakeups.
6663 			 */
6664 			if (processor->processor_primary == processor) {
6665 				break;
6666 			}
6667 		}
6668 	}
6669 
6670 	recount_snapshot(&snap);
6671 	recount_processor_run(&processor->pr_recount, &snap);
6672 	smr_cpu_join(processor, snap.rsn_time_mach);
6673 
6674 	ast_t reason = AST_NONE;
6675 
6676 	/* We're handling all scheduling AST's */
6677 	ast_off(AST_SCHEDULING);
6678 
6679 	/*
6680 	 * thread_select will move the processor from dispatching to running,
6681 	 * or put it in idle if there's nothing to do.
6682 	 */
6683 	thread_t cur_thread = current_thread();
6684 
6685 	thread_lock(cur_thread);
6686 	thread_t new_thread = thread_select(cur_thread, processor, &reason);
6687 	thread_unlock(cur_thread);
6688 
6689 	assert(processor->running_timers_active == false);
6690 
6691 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
6692 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_IDLE) | DBG_FUNC_END,
6693 	    (uintptr_t)thread_tid(thread), processor->state, (uintptr_t)thread_tid(new_thread), reason, 0);
6694 
6695 	return new_thread;
6696 }
6697 
6698 /*
6699  *	Each processor has a dedicated thread which
6700  *	executes the idle loop when there is no suitable
6701  *	previous context.
6702  *
6703  *	This continuation is entered with interrupts disabled.
6704  */
6705 void
idle_thread(__assert_only void * parameter,__unused wait_result_t result)6706 idle_thread(__assert_only void* parameter,
6707     __unused wait_result_t result)
6708 {
6709 	assert(ml_get_interrupts_enabled() == FALSE);
6710 	assert(parameter == NULL);
6711 
6712 	processor_t processor = current_processor();
6713 
6714 	smr_cpu_leave(processor, processor->last_dispatch);
6715 
6716 	/*
6717 	 * Ensure that anything running in idle context triggers
6718 	 * preemption-disabled checks.
6719 	 */
6720 	disable_preemption_without_measurements();
6721 
6722 	/*
6723 	 * Enable interrupts temporarily to handle any pending interrupts
6724 	 * or IPIs before deciding to sleep
6725 	 */
6726 	spllo();
6727 
6728 	thread_t new_thread = processor_idle(THREAD_NULL, processor);
6729 	/* returns with interrupts disabled */
6730 
6731 	enable_preemption();
6732 
6733 	if (new_thread != THREAD_NULL) {
6734 		thread_run(processor->idle_thread,
6735 		    idle_thread, NULL, new_thread);
6736 		/*NOTREACHED*/
6737 	}
6738 
6739 	thread_block(idle_thread);
6740 	/*NOTREACHED*/
6741 }
6742 
6743 kern_return_t
idle_thread_create(processor_t processor)6744 idle_thread_create(
6745 	processor_t             processor)
6746 {
6747 	kern_return_t   result;
6748 	thread_t                thread;
6749 	spl_t                   s;
6750 	char                    name[MAXTHREADNAMESIZE];
6751 
6752 	result = kernel_thread_create(idle_thread, NULL, MAXPRI_KERNEL, &thread);
6753 	if (result != KERN_SUCCESS) {
6754 		return result;
6755 	}
6756 
6757 	snprintf(name, sizeof(name), "idle #%d", processor->cpu_id);
6758 	thread_set_thread_name(thread, name);
6759 
6760 	s = splsched();
6761 	thread_lock(thread);
6762 	thread->bound_processor = processor;
6763 	processor->idle_thread = thread;
6764 	thread->sched_pri = thread->base_pri = IDLEPRI;
6765 	thread->state = (TH_RUN | TH_IDLE);
6766 	thread->options |= TH_OPT_IDLE_THREAD;
6767 	thread->last_made_runnable_time = thread->last_basepri_change_time = mach_absolute_time();
6768 	thread_unlock(thread);
6769 	splx(s);
6770 
6771 	thread_deallocate(thread);
6772 
6773 	return KERN_SUCCESS;
6774 }
6775 
6776 static void sched_update_powered_cores_continue(void);
6777 
6778 /*
6779  * sched_startup:
6780  *
6781  * Kicks off scheduler services.
6782  *
6783  * Called at splsched.
6784  */
6785 void
sched_startup(void)6786 sched_startup(void)
6787 {
6788 	kern_return_t   result;
6789 	thread_t                thread;
6790 
6791 	simple_lock_init(&sched_vm_group_list_lock, 0);
6792 
6793 	result = kernel_thread_start_priority((thread_continue_t)sched_init_thread,
6794 	    NULL, MAXPRI_KERNEL, &thread);
6795 	if (result != KERN_SUCCESS) {
6796 		panic("sched_startup");
6797 	}
6798 
6799 	thread_deallocate(thread);
6800 
6801 	assert_thread_magic(thread);
6802 
6803 	/*
6804 	 * Yield to the sched_init_thread once, to
6805 	 * initialize our own thread after being switched
6806 	 * back to.
6807 	 *
6808 	 * The current thread is the only other thread
6809 	 * active at this point.
6810 	 */
6811 	thread_block(THREAD_CONTINUE_NULL);
6812 
6813 	result = kernel_thread_start_priority((thread_continue_t)sched_update_powered_cores_continue,
6814 	    NULL, MAXPRI_KERNEL, &thread);
6815 	if (result != KERN_SUCCESS) {
6816 		panic("sched_startup");
6817 	}
6818 
6819 	thread_deallocate(thread);
6820 
6821 	assert_thread_magic(thread);
6822 }
6823 
6824 #if __arm64__
6825 static _Atomic uint64_t sched_perfcontrol_callback_deadline;
6826 #endif /* __arm64__ */
6827 
6828 
6829 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
6830 
6831 static volatile uint64_t                sched_maintenance_deadline;
6832 static uint64_t                         sched_tick_last_abstime;
6833 static uint64_t                         sched_tick_delta;
6834 uint64_t                                sched_tick_max_delta;
6835 
6836 
6837 /*
6838  *	sched_init_thread:
6839  *
6840  *	Perform periodic bookkeeping functions about ten
6841  *	times per second.
6842  */
6843 void
sched_timeshare_maintenance_continue(void)6844 sched_timeshare_maintenance_continue(void)
6845 {
6846 	uint64_t        sched_tick_ctime, late_time;
6847 
6848 	struct sched_update_scan_context scan_context = {
6849 		.earliest_bg_make_runnable_time = UINT64_MAX,
6850 		.earliest_normal_make_runnable_time = UINT64_MAX,
6851 		.earliest_rt_make_runnable_time = UINT64_MAX
6852 	};
6853 
6854 	sched_tick_ctime = mach_absolute_time();
6855 
6856 	if (__improbable(sched_tick_last_abstime == 0)) {
6857 		sched_tick_last_abstime = sched_tick_ctime;
6858 		late_time = 0;
6859 		sched_tick_delta = 1;
6860 	} else {
6861 		late_time = sched_tick_ctime - sched_tick_last_abstime;
6862 		sched_tick_delta = late_time / sched_tick_interval;
6863 		/* Ensure a delta of 1, since the interval could be slightly
6864 		 * smaller than the sched_tick_interval due to dispatch
6865 		 * latencies.
6866 		 */
6867 		sched_tick_delta = MAX(sched_tick_delta, 1);
6868 
6869 		/* In the event interrupt latencies or platform
6870 		 * idle events that advanced the timebase resulted
6871 		 * in periods where no threads were dispatched,
6872 		 * cap the maximum "tick delta" at SCHED_TICK_MAX_DELTA
6873 		 * iterations.
6874 		 */
6875 		sched_tick_delta = MIN(sched_tick_delta, SCHED_TICK_MAX_DELTA);
6876 
6877 		sched_tick_last_abstime = sched_tick_ctime;
6878 		sched_tick_max_delta = MAX(sched_tick_delta, sched_tick_max_delta);
6879 	}
6880 
6881 	scan_context.sched_tick_last_abstime = sched_tick_last_abstime;
6882 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_MAINTENANCE) | DBG_FUNC_START,
6883 	    sched_tick_delta, late_time, 0, 0, 0);
6884 
6885 	/* Add a number of pseudo-ticks corresponding to the elapsed interval
6886 	 * This could be greater than 1 if substantial intervals where
6887 	 * all processors are idle occur, which rarely occurs in practice.
6888 	 */
6889 
6890 	sched_tick += sched_tick_delta;
6891 
6892 	update_vm_info();
6893 
6894 	/*
6895 	 *  Compute various averages.
6896 	 */
6897 	compute_averages(sched_tick_delta);
6898 
6899 	/*
6900 	 *  Scan the run queues for threads which
6901 	 *  may need to be updated, and find the earliest runnable thread on the runqueue
6902 	 *  to report its latency.
6903 	 */
6904 	SCHED(thread_update_scan)(&scan_context);
6905 
6906 	SCHED(rt_runq_scan)(&scan_context);
6907 
6908 	uint64_t ctime = mach_absolute_time();
6909 
6910 	uint64_t bg_max_latency       = (ctime > scan_context.earliest_bg_make_runnable_time) ?
6911 	    ctime - scan_context.earliest_bg_make_runnable_time : 0;
6912 
6913 	uint64_t default_max_latency  = (ctime > scan_context.earliest_normal_make_runnable_time) ?
6914 	    ctime - scan_context.earliest_normal_make_runnable_time : 0;
6915 
6916 	uint64_t realtime_max_latency = (ctime > scan_context.earliest_rt_make_runnable_time) ?
6917 	    ctime - scan_context.earliest_rt_make_runnable_time : 0;
6918 
6919 	machine_max_runnable_latency(bg_max_latency, default_max_latency, realtime_max_latency);
6920 
6921 	/*
6922 	 * Check to see if the special sched VM group needs attention.
6923 	 */
6924 	sched_vm_group_maintenance();
6925 
6926 #if __arm64__
6927 	/* Check to see if the recommended cores failsafe is active */
6928 	sched_recommended_cores_maintenance();
6929 #endif /* __arm64__ */
6930 
6931 
6932 #if DEBUG || DEVELOPMENT
6933 #if __x86_64__
6934 #include <i386/misc_protos.h>
6935 	/* Check for long-duration interrupts */
6936 	mp_interrupt_watchdog();
6937 #endif /* __x86_64__ */
6938 #endif /* DEBUG || DEVELOPMENT */
6939 
6940 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_MAINTENANCE) | DBG_FUNC_END,
6941 	    sched_pri_shifts[TH_BUCKET_SHARE_FG], sched_pri_shifts[TH_BUCKET_SHARE_BG],
6942 	    sched_pri_shifts[TH_BUCKET_SHARE_UT], sched_pri_shifts[TH_BUCKET_SHARE_DF], 0);
6943 
6944 	assert_wait((event_t)sched_timeshare_maintenance_continue, THREAD_UNINT);
6945 	thread_block((thread_continue_t)sched_timeshare_maintenance_continue);
6946 	/*NOTREACHED*/
6947 }
6948 
6949 static uint64_t sched_maintenance_wakeups;
6950 
6951 /*
6952  * Determine if the set of routines formerly driven by a maintenance timer
6953  * must be invoked, based on a deadline comparison. Signals the scheduler
6954  * maintenance thread on deadline expiration. Must be invoked at an interval
6955  * lower than the "sched_tick_interval", currently accomplished by
6956  * invocation via the quantum expiration timer and at context switch time.
6957  * Performance matters: this routine reuses a timestamp approximating the
6958  * current absolute time received from the caller, and should perform
6959  * no more than a comparison against the deadline in the common case.
6960  */
6961 void
sched_timeshare_consider_maintenance(uint64_t ctime,bool safe_point)6962 sched_timeshare_consider_maintenance(uint64_t ctime, bool safe_point)
6963 {
6964 	uint64_t deadline = sched_maintenance_deadline;
6965 
6966 	if (__improbable(ctime >= deadline)) {
6967 		if (__improbable(current_thread() == sched_maintenance_thread)) {
6968 			return;
6969 		}
6970 		OSMemoryBarrier();
6971 
6972 		uint64_t ndeadline = ctime + sched_tick_interval;
6973 
6974 		if (__probable(os_atomic_cmpxchg(&sched_maintenance_deadline, deadline, ndeadline, seq_cst))) {
6975 			thread_wakeup((event_t)sched_timeshare_maintenance_continue);
6976 			sched_maintenance_wakeups++;
6977 			smr_maintenance(ctime);
6978 		}
6979 	}
6980 
6981 	smr_cpu_tick(ctime, safe_point);
6982 
6983 #if !CONFIG_SCHED_CLUTCH
6984 	/*
6985 	 * Only non-clutch schedulers use the global load calculation EWMA algorithm. For clutch
6986 	 * scheduler, the load is maintained at the thread group and bucket level.
6987 	 */
6988 	uint64_t load_compute_deadline = os_atomic_load_wide(&sched_load_compute_deadline, relaxed);
6989 
6990 	if (__improbable(load_compute_deadline && ctime >= load_compute_deadline)) {
6991 		uint64_t new_deadline = 0;
6992 		if (os_atomic_cmpxchg(&sched_load_compute_deadline, load_compute_deadline, new_deadline, relaxed)) {
6993 			compute_sched_load();
6994 			new_deadline = ctime + sched_load_compute_interval_abs;
6995 			os_atomic_store_wide(&sched_load_compute_deadline, new_deadline, relaxed);
6996 		}
6997 	}
6998 #endif /* CONFIG_SCHED_CLUTCH */
6999 
7000 #if __arm64__
7001 	uint64_t perf_deadline = os_atomic_load(&sched_perfcontrol_callback_deadline, relaxed);
7002 
7003 	if (__improbable(perf_deadline && ctime >= perf_deadline)) {
7004 		/* CAS in 0, if success, make callback. Otherwise let the next context switch check again. */
7005 		if (os_atomic_cmpxchg(&sched_perfcontrol_callback_deadline, perf_deadline, 0, relaxed)) {
7006 			machine_perfcontrol_deadline_passed(perf_deadline);
7007 		}
7008 	}
7009 #endif /* __arm64__ */
7010 }
7011 
7012 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
7013 
7014 void
sched_init_thread(void)7015 sched_init_thread(void)
7016 {
7017 	thread_block(THREAD_CONTINUE_NULL);
7018 
7019 	thread_t thread = current_thread();
7020 
7021 	thread_set_thread_name(thread, "sched_maintenance_thread");
7022 
7023 	sched_maintenance_thread = thread;
7024 
7025 	SCHED(maintenance_continuation)();
7026 
7027 	/*NOTREACHED*/
7028 }
7029 
7030 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
7031 
7032 /*
7033  *	thread_update_scan / runq_scan:
7034  *
7035  *	Scan the run queues to account for timesharing threads
7036  *	which need to be updated.
7037  *
7038  *	Scanner runs in two passes.  Pass one squirrels likely
7039  *	threads away in an array, pass two does the update.
7040  *
7041  *	This is necessary because the run queue is locked for
7042  *	the candidate scan, but	the thread is locked for the update.
7043  *
7044  *	Array should be sized to make forward progress, without
7045  *	disabling preemption for long periods.
7046  */
7047 
7048 #define THREAD_UPDATE_SIZE              128
7049 
7050 static thread_t thread_update_array[THREAD_UPDATE_SIZE];
7051 static uint32_t thread_update_count = 0;
7052 
7053 /* Returns TRUE if thread was added, FALSE if thread_update_array is full */
7054 boolean_t
thread_update_add_thread(thread_t thread)7055 thread_update_add_thread(thread_t thread)
7056 {
7057 	if (thread_update_count == THREAD_UPDATE_SIZE) {
7058 		return FALSE;
7059 	}
7060 
7061 	thread_update_array[thread_update_count++] = thread;
7062 	thread_reference(thread);
7063 	return TRUE;
7064 }
7065 
7066 void
thread_update_process_threads(void)7067 thread_update_process_threads(void)
7068 {
7069 	assert(thread_update_count <= THREAD_UPDATE_SIZE);
7070 
7071 	for (uint32_t i = 0; i < thread_update_count; i++) {
7072 		thread_t thread = thread_update_array[i];
7073 		assert_thread_magic(thread);
7074 		thread_update_array[i] = THREAD_NULL;
7075 
7076 		spl_t s = splsched();
7077 		thread_lock(thread);
7078 		if (!(thread->state & (TH_WAIT)) && thread->sched_stamp != sched_tick) {
7079 			SCHED(update_priority)(thread);
7080 		}
7081 		thread_unlock(thread);
7082 		splx(s);
7083 
7084 		thread_deallocate(thread);
7085 	}
7086 
7087 	thread_update_count = 0;
7088 }
7089 
7090 static boolean_t
runq_scan_thread(thread_t thread,sched_update_scan_context_t scan_context)7091 runq_scan_thread(
7092 	thread_t thread,
7093 	sched_update_scan_context_t scan_context)
7094 {
7095 	assert_thread_magic(thread);
7096 
7097 	if (thread->sched_stamp != sched_tick &&
7098 	    thread->sched_mode == TH_MODE_TIMESHARE) {
7099 		if (thread_update_add_thread(thread) == FALSE) {
7100 			return TRUE;
7101 		}
7102 	}
7103 
7104 	if (cpu_throttle_enabled && ((thread->sched_pri <= MAXPRI_THROTTLE) && (thread->base_pri <= MAXPRI_THROTTLE))) {
7105 		if (thread->last_made_runnable_time < scan_context->earliest_bg_make_runnable_time) {
7106 			scan_context->earliest_bg_make_runnable_time = thread->last_made_runnable_time;
7107 		}
7108 	} else {
7109 		if (thread->last_made_runnable_time < scan_context->earliest_normal_make_runnable_time) {
7110 			scan_context->earliest_normal_make_runnable_time = thread->last_made_runnable_time;
7111 		}
7112 	}
7113 
7114 	return FALSE;
7115 }
7116 
7117 /*
7118  *	Scan a runq for candidate threads.
7119  *
7120  *	Returns TRUE if retry is needed.
7121  */
7122 boolean_t
runq_scan(run_queue_t runq,sched_update_scan_context_t scan_context)7123 runq_scan(
7124 	run_queue_t                   runq,
7125 	sched_update_scan_context_t   scan_context)
7126 {
7127 	int count       = runq->count;
7128 	int queue_index;
7129 
7130 	assert(count >= 0);
7131 
7132 	if (count == 0) {
7133 		return FALSE;
7134 	}
7135 
7136 	for (queue_index = bitmap_first(runq->bitmap, NRQS);
7137 	    queue_index >= 0;
7138 	    queue_index = bitmap_next(runq->bitmap, queue_index)) {
7139 		thread_t thread;
7140 		circle_queue_t queue = &runq->queues[queue_index];
7141 
7142 		cqe_foreach_element(thread, queue, runq_links) {
7143 			assert(count > 0);
7144 			if (runq_scan_thread(thread, scan_context) == TRUE) {
7145 				return TRUE;
7146 			}
7147 			count--;
7148 		}
7149 	}
7150 
7151 	return FALSE;
7152 }
7153 
7154 #if CONFIG_SCHED_CLUTCH
7155 
7156 boolean_t
sched_clutch_timeshare_scan(queue_t thread_queue,uint16_t thread_count,sched_update_scan_context_t scan_context)7157 sched_clutch_timeshare_scan(
7158 	queue_t thread_queue,
7159 	uint16_t thread_count,
7160 	sched_update_scan_context_t scan_context)
7161 {
7162 	if (thread_count == 0) {
7163 		return FALSE;
7164 	}
7165 
7166 	thread_t thread;
7167 	qe_foreach_element_safe(thread, thread_queue, th_clutch_timeshare_link) {
7168 		if (runq_scan_thread(thread, scan_context) == TRUE) {
7169 			return TRUE;
7170 		}
7171 		thread_count--;
7172 	}
7173 
7174 	assert(thread_count == 0);
7175 	return FALSE;
7176 }
7177 
7178 
7179 #endif /* CONFIG_SCHED_CLUTCH */
7180 
7181 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
7182 
7183 bool
thread_is_eager_preempt(thread_t thread)7184 thread_is_eager_preempt(thread_t thread)
7185 {
7186 	return thread->sched_flags & TH_SFLAG_EAGERPREEMPT;
7187 }
7188 
7189 void
thread_set_eager_preempt(thread_t thread)7190 thread_set_eager_preempt(thread_t thread)
7191 {
7192 	spl_t s = splsched();
7193 	thread_lock(thread);
7194 
7195 	assert(!thread_is_eager_preempt(thread));
7196 
7197 	thread->sched_flags |= TH_SFLAG_EAGERPREEMPT;
7198 
7199 	if (thread == current_thread()) {
7200 		/* csw_check updates current_is_eagerpreempt on the processor */
7201 		ast_t ast = csw_check(thread, current_processor(), AST_NONE);
7202 
7203 		thread_unlock(thread);
7204 
7205 		if (ast != AST_NONE) {
7206 			thread_block_reason(THREAD_CONTINUE_NULL, NULL, ast);
7207 		}
7208 	} else {
7209 		processor_t last_processor = thread->last_processor;
7210 
7211 		if (last_processor != PROCESSOR_NULL &&
7212 		    last_processor->state == PROCESSOR_RUNNING &&
7213 		    last_processor->active_thread == thread) {
7214 			cause_ast_check(last_processor);
7215 		}
7216 
7217 		thread_unlock(thread);
7218 	}
7219 
7220 	splx(s);
7221 }
7222 
7223 void
thread_clear_eager_preempt(thread_t thread)7224 thread_clear_eager_preempt(thread_t thread)
7225 {
7226 	spl_t s = splsched();
7227 	thread_lock(thread);
7228 
7229 	assert(thread_is_eager_preempt(thread));
7230 
7231 	thread->sched_flags &= ~TH_SFLAG_EAGERPREEMPT;
7232 
7233 	if (thread == current_thread()) {
7234 		current_processor()->current_is_eagerpreempt = false;
7235 	}
7236 
7237 	thread_unlock(thread);
7238 	splx(s);
7239 }
7240 
7241 /*
7242  * Scheduling statistics
7243  */
7244 void
sched_stats_handle_csw(processor_t processor,int reasons,int selfpri,int otherpri)7245 sched_stats_handle_csw(processor_t processor, int reasons, int selfpri, int otherpri)
7246 {
7247 	struct sched_statistics *stats;
7248 	boolean_t to_realtime = FALSE;
7249 
7250 	stats = PERCPU_GET_RELATIVE(sched_stats, processor, processor);
7251 	stats->csw_count++;
7252 
7253 	if (otherpri >= BASEPRI_REALTIME) {
7254 		stats->rt_sched_count++;
7255 		to_realtime = TRUE;
7256 	}
7257 
7258 	if ((reasons & AST_PREEMPT) != 0) {
7259 		stats->preempt_count++;
7260 
7261 		if (selfpri >= BASEPRI_REALTIME) {
7262 			stats->preempted_rt_count++;
7263 		}
7264 
7265 		if (to_realtime) {
7266 			stats->preempted_by_rt_count++;
7267 		}
7268 	}
7269 }
7270 
7271 void
sched_stats_handle_runq_change(struct runq_stats * stats,int old_count)7272 sched_stats_handle_runq_change(struct runq_stats *stats, int old_count)
7273 {
7274 	uint64_t timestamp = mach_absolute_time();
7275 
7276 	stats->count_sum += (timestamp - stats->last_change_timestamp) * old_count;
7277 	stats->last_change_timestamp = timestamp;
7278 }
7279 
7280 /*
7281  *     For calls from assembly code
7282  */
7283 #undef thread_wakeup
7284 void
7285 thread_wakeup(
7286 	event_t         x);
7287 
7288 void
thread_wakeup(event_t x)7289 thread_wakeup(
7290 	event_t         x)
7291 {
7292 	thread_wakeup_with_result(x, THREAD_AWAKENED);
7293 }
7294 
7295 boolean_t
preemption_enabled(void)7296 preemption_enabled(void)
7297 {
7298 	return get_preemption_level() == 0 && ml_get_interrupts_enabled();
7299 }
7300 
7301 static void
sched_timer_deadline_tracking_init(void)7302 sched_timer_deadline_tracking_init(void)
7303 {
7304 	nanoseconds_to_absolutetime(TIMER_DEADLINE_TRACKING_BIN_1_DEFAULT, &timer_deadline_tracking_bin_1);
7305 	nanoseconds_to_absolutetime(TIMER_DEADLINE_TRACKING_BIN_2_DEFAULT, &timer_deadline_tracking_bin_2);
7306 }
7307 
7308 static uint64_t latest_requested_powered_cores = ALL_CORES_POWERED;
7309 processor_reason_t latest_requested_reason = REASON_NONE;
7310 static uint64_t current_requested_powered_cores = ALL_CORES_POWERED;
7311 bool perfcontrol_sleep_override = false;
7312 
7313 LCK_GRP_DECLARE(cluster_powerdown_grp, "cluster_powerdown");
7314 LCK_MTX_DECLARE(cluster_powerdown_lock, &cluster_powerdown_grp);
7315 int32_t cluster_powerdown_suspend_count = 0;
7316 
7317 bool
sched_is_in_sleep(void)7318 sched_is_in_sleep(void)
7319 {
7320 	os_atomic_thread_fence(acquire);
7321 	return perfcontrol_sleep_override;
7322 }
7323 
7324 static void
sched_update_powered_cores_continue(void)7325 sched_update_powered_cores_continue(void)
7326 {
7327 	lck_mtx_lock(&cluster_powerdown_lock);
7328 
7329 	if (!cluster_powerdown_suspend_count) {
7330 		spl_t s = splsched();
7331 		simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7332 
7333 		uint64_t latest = latest_requested_powered_cores;
7334 		processor_reason_t reason = latest_requested_reason;
7335 		uint64_t current = current_requested_powered_cores;
7336 		current_requested_powered_cores = latest;
7337 		bool in_sleep = perfcontrol_sleep_override;
7338 
7339 		simple_unlock(&sched_available_cores_lock);
7340 		splx(s);
7341 
7342 		while (latest != current) {
7343 			if (!in_sleep) {
7344 				assert((reason == REASON_CLPC_SYSTEM) || (reason == REASON_CLPC_USER));
7345 				sched_update_powered_cores(latest, reason, SHUTDOWN_TEMPORARY | WAIT_FOR_LAST_START);
7346 			}
7347 
7348 			s = splsched();
7349 			simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7350 
7351 			latest = latest_requested_powered_cores;
7352 			reason = latest_requested_reason;
7353 			current = current_requested_powered_cores;
7354 			current_requested_powered_cores = latest;
7355 			in_sleep = perfcontrol_sleep_override;
7356 
7357 			simple_unlock(&sched_available_cores_lock);
7358 			splx(s);
7359 		}
7360 
7361 		assert_wait((event_t)sched_update_powered_cores_continue, THREAD_UNINT);
7362 
7363 		s = splsched();
7364 		simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7365 		if (latest_requested_powered_cores != current_requested_powered_cores) {
7366 			clear_wait(current_thread(), THREAD_AWAKENED);
7367 		}
7368 		simple_unlock(&sched_available_cores_lock);
7369 		splx(s);
7370 	}
7371 
7372 	lck_mtx_unlock(&cluster_powerdown_lock);
7373 
7374 	thread_block((thread_continue_t)sched_update_powered_cores_continue);
7375 	/*NOTREACHED*/
7376 }
7377 
7378 void
sched_perfcontrol_update_powered_cores(uint64_t requested_powered_cores,processor_reason_t reason,__unused uint32_t flags)7379 sched_perfcontrol_update_powered_cores(uint64_t requested_powered_cores, processor_reason_t reason, __unused uint32_t flags)
7380 {
7381 	assert((reason == REASON_CLPC_SYSTEM) || (reason == REASON_CLPC_USER));
7382 
7383 #if DEVELOPMENT || DEBUG
7384 	if (flags & (ASSERT_IN_SLEEP | ASSERT_POWERDOWN_SUSPENDED)) {
7385 		if (flags & ASSERT_POWERDOWN_SUSPENDED) {
7386 			assert(cluster_powerdown_suspend_count > 0);
7387 		}
7388 		if (flags & ASSERT_IN_SLEEP) {
7389 			assert(perfcontrol_sleep_override == true);
7390 		}
7391 		return;
7392 	}
7393 #endif
7394 
7395 	spl_t s = splsched();
7396 	simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7397 
7398 	bool should_wakeup = !cluster_powerdown_suspend_count;
7399 	if (should_wakeup) {
7400 		latest_requested_powered_cores = requested_powered_cores;
7401 		latest_requested_reason = reason;
7402 	}
7403 
7404 	simple_unlock(&sched_available_cores_lock);
7405 	splx(s);
7406 
7407 	if (should_wakeup) {
7408 		thread_wakeup((event_t)sched_update_powered_cores_continue);
7409 	}
7410 }
7411 
7412 void
suspend_cluster_powerdown(void)7413 suspend_cluster_powerdown(void)
7414 {
7415 	lck_mtx_lock(&cluster_powerdown_lock);
7416 
7417 	assert(cluster_powerdown_suspend_count >= 0);
7418 
7419 	bool first_suspend = (cluster_powerdown_suspend_count == 0);
7420 	if (first_suspend) {
7421 		spl_t s = splsched();
7422 		simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7423 		latest_requested_powered_cores = ALL_CORES_POWERED;
7424 		current_requested_powered_cores = ALL_CORES_POWERED;
7425 		latest_requested_reason = REASON_SYSTEM;
7426 		simple_unlock(&sched_available_cores_lock);
7427 		splx(s);
7428 	}
7429 
7430 	cluster_powerdown_suspend_count++;
7431 
7432 	if (first_suspend) {
7433 		kprintf("%s>calling sched_update_powered_cores(ALL_CORES_POWERED, REASON_SYSTEM, LOCK_STATE | WAIT_FOR_START)\n", __FUNCTION__);
7434 		sched_update_powered_cores(ALL_CORES_POWERED, REASON_SYSTEM, LOCK_STATE | WAIT_FOR_START);
7435 	}
7436 
7437 	lck_mtx_unlock(&cluster_powerdown_lock);
7438 }
7439 
7440 void
resume_cluster_powerdown(void)7441 resume_cluster_powerdown(void)
7442 {
7443 	lck_mtx_lock(&cluster_powerdown_lock);
7444 
7445 	if (cluster_powerdown_suspend_count <= 0) {
7446 		panic("resume_cluster_powerdown() called with cluster_powerdown_suspend_count=%d\n", cluster_powerdown_suspend_count);
7447 	}
7448 
7449 	cluster_powerdown_suspend_count--;
7450 
7451 	bool last_resume = (cluster_powerdown_suspend_count == 0);
7452 
7453 	if (last_resume) {
7454 		spl_t s = splsched();
7455 		simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7456 		latest_requested_powered_cores = ALL_CORES_POWERED;
7457 		current_requested_powered_cores = ALL_CORES_POWERED;
7458 		latest_requested_reason = REASON_SYSTEM;
7459 		simple_unlock(&sched_available_cores_lock);
7460 		splx(s);
7461 
7462 		kprintf("%s>calling sched_update_powered_cores(ALL_CORES_POWERED, REASON_SYSTEM, UNLOCK_STATE)\n", __FUNCTION__);
7463 		sched_update_powered_cores(ALL_CORES_POWERED, REASON_SYSTEM, UNLOCK_STATE);
7464 	}
7465 
7466 	lck_mtx_unlock(&cluster_powerdown_lock);
7467 }
7468 
7469 LCK_MTX_DECLARE(user_cluster_powerdown_lock, &cluster_powerdown_grp);
7470 static bool user_suspended_cluster_powerdown = false;
7471 
7472 kern_return_t
suspend_cluster_powerdown_from_user(void)7473 suspend_cluster_powerdown_from_user(void)
7474 {
7475 	kern_return_t ret = KERN_FAILURE;
7476 
7477 	lck_mtx_lock(&user_cluster_powerdown_lock);
7478 
7479 	if (!user_suspended_cluster_powerdown) {
7480 		suspend_cluster_powerdown();
7481 		user_suspended_cluster_powerdown = true;
7482 		ret = KERN_SUCCESS;
7483 	}
7484 
7485 	lck_mtx_unlock(&user_cluster_powerdown_lock);
7486 
7487 	return ret;
7488 }
7489 
7490 kern_return_t
resume_cluster_powerdown_from_user(void)7491 resume_cluster_powerdown_from_user(void)
7492 {
7493 	kern_return_t ret = KERN_FAILURE;
7494 
7495 	lck_mtx_lock(&user_cluster_powerdown_lock);
7496 
7497 	if (user_suspended_cluster_powerdown) {
7498 		resume_cluster_powerdown();
7499 		user_suspended_cluster_powerdown = false;
7500 		ret = KERN_SUCCESS;
7501 	}
7502 
7503 	lck_mtx_unlock(&user_cluster_powerdown_lock);
7504 
7505 	return ret;
7506 }
7507 
7508 int
get_cluster_powerdown_user_suspended(void)7509 get_cluster_powerdown_user_suspended(void)
7510 {
7511 	lck_mtx_lock(&user_cluster_powerdown_lock);
7512 
7513 	int ret = (int)user_suspended_cluster_powerdown;
7514 
7515 	lck_mtx_unlock(&user_cluster_powerdown_lock);
7516 
7517 	return ret;
7518 }
7519 
7520 #if DEVELOPMENT || DEBUG
7521 /* Functions to support the temporary sysctl */
7522 static uint64_t saved_requested_powered_cores = ALL_CORES_POWERED;
7523 void
sched_set_powered_cores(int requested_powered_cores)7524 sched_set_powered_cores(int requested_powered_cores)
7525 {
7526 	processor_reason_t reason = bit_test(requested_powered_cores, 31) ? REASON_CLPC_USER : REASON_CLPC_SYSTEM;
7527 	uint32_t flags = requested_powered_cores & 0x30000000;
7528 
7529 	saved_requested_powered_cores = requested_powered_cores;
7530 
7531 	requested_powered_cores = bits(requested_powered_cores, 28, 0);
7532 
7533 	sched_perfcontrol_update_powered_cores(requested_powered_cores, reason, flags);
7534 }
7535 int
sched_get_powered_cores(void)7536 sched_get_powered_cores(void)
7537 {
7538 	return (int)saved_requested_powered_cores;
7539 }
7540 #endif
7541 
7542 /*
7543  * Ensure that all cores are powered and recommended before sleep
7544  */
7545 void
sched_override_available_cores_for_sleep(void)7546 sched_override_available_cores_for_sleep(void)
7547 {
7548 	spl_t s = splsched();
7549 	simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7550 
7551 	if (perfcontrol_sleep_override == false) {
7552 		perfcontrol_sleep_override = true;
7553 #if __arm__ || __arm64__
7554 		sched_update_recommended_cores(ALL_CORES_RECOMMENDED, REASON_SYSTEM, 0);
7555 #endif
7556 	}
7557 
7558 	simple_unlock(&sched_available_cores_lock);
7559 	splx(s);
7560 
7561 	suspend_cluster_powerdown();
7562 }
7563 
7564 /*
7565  * Restore the previously recommended cores, but leave all cores powered
7566  * after sleep
7567  */
7568 void
sched_restore_available_cores_after_sleep(void)7569 sched_restore_available_cores_after_sleep(void)
7570 {
7571 	spl_t s = splsched();
7572 	simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7573 
7574 	if (perfcontrol_sleep_override == true) {
7575 		perfcontrol_sleep_override = false;
7576 #if __arm__ || __arm64__
7577 		sched_update_recommended_cores(perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores,
7578 		    REASON_NONE, 0);
7579 #endif
7580 	}
7581 
7582 	simple_unlock(&sched_available_cores_lock);
7583 	splx(s);
7584 
7585 	resume_cluster_powerdown();
7586 }
7587 
7588 #if __arm__ || __arm64__
7589 
7590 uint32_t    perfcontrol_requested_recommended_core_count = MAX_CPUS;
7591 bool        perfcontrol_failsafe_active = false;
7592 
7593 uint64_t    perfcontrol_failsafe_maintenance_runnable_time;
7594 uint64_t    perfcontrol_failsafe_activation_time;
7595 uint64_t    perfcontrol_failsafe_deactivation_time;
7596 
7597 /* data covering who likely caused it and how long they ran */
7598 #define FAILSAFE_NAME_LEN       33 /* (2*MAXCOMLEN)+1 from size of p_name */
7599 char        perfcontrol_failsafe_name[FAILSAFE_NAME_LEN];
7600 int         perfcontrol_failsafe_pid;
7601 uint64_t    perfcontrol_failsafe_tid;
7602 uint64_t    perfcontrol_failsafe_thread_timer_at_start;
7603 uint64_t    perfcontrol_failsafe_thread_timer_last_seen;
7604 uint64_t    perfcontrol_failsafe_recommended_at_trigger;
7605 
7606 /*
7607  * Perf controller calls here to update the recommended core bitmask.
7608  * If the failsafe is active, we don't immediately apply the new value.
7609  * Instead, we store the new request and use it after the failsafe deactivates.
7610  *
7611  * If the failsafe is not active, immediately apply the update.
7612  *
7613  * No scheduler locks are held, no other locks are held that scheduler might depend on,
7614  * interrupts are enabled
7615  *
7616  * currently prototype is in osfmk/arm/machine_routines.h
7617  */
7618 void
sched_perfcontrol_update_recommended_cores_reason(uint64_t recommended_cores,processor_reason_t reason,uint32_t flags)7619 sched_perfcontrol_update_recommended_cores_reason(uint64_t recommended_cores, processor_reason_t reason, uint32_t flags)
7620 {
7621 	assert(preemption_enabled());
7622 
7623 	spl_t s = splsched();
7624 	simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7625 
7626 	if (reason == REASON_CLPC_SYSTEM) {
7627 		perfcontrol_system_requested_recommended_cores = recommended_cores;
7628 	} else {
7629 		assert(reason == REASON_CLPC_USER);
7630 		perfcontrol_user_requested_recommended_cores = recommended_cores;
7631 	}
7632 
7633 	perfcontrol_requested_recommended_cores = perfcontrol_system_requested_recommended_cores & perfcontrol_user_requested_recommended_cores;
7634 	perfcontrol_requested_recommended_core_count = __builtin_popcountll(perfcontrol_requested_recommended_cores);
7635 
7636 	if ((perfcontrol_failsafe_active == false) && (perfcontrol_sleep_override == false)) {
7637 		sched_update_recommended_cores(perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores, reason, flags);
7638 	} else {
7639 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
7640 		    MACHDBG_CODE(DBG_MACH_SCHED, MACH_REC_CORES_FAILSAFE) | DBG_FUNC_NONE,
7641 		    perfcontrol_requested_recommended_cores,
7642 		    sched_maintenance_thread->last_made_runnable_time, 0, 0, 0);
7643 	}
7644 
7645 	simple_unlock(&sched_available_cores_lock);
7646 	splx(s);
7647 }
7648 
7649 void
sched_perfcontrol_update_recommended_cores(uint32_t recommended_cores)7650 sched_perfcontrol_update_recommended_cores(uint32_t recommended_cores)
7651 {
7652 	sched_perfcontrol_update_recommended_cores_reason(recommended_cores, REASON_CLPC_USER, 0);
7653 }
7654 
7655 /*
7656  * Consider whether we need to activate the recommended cores failsafe
7657  *
7658  * Called from quantum timer interrupt context of a realtime thread
7659  * No scheduler locks are held, interrupts are disabled
7660  */
7661 void
sched_consider_recommended_cores(uint64_t ctime,thread_t cur_thread)7662 sched_consider_recommended_cores(uint64_t ctime, thread_t cur_thread)
7663 {
7664 	/*
7665 	 * Check if a realtime thread is starving the system
7666 	 * and bringing up non-recommended cores would help
7667 	 *
7668 	 * TODO: Is this the correct check for recommended == possible cores?
7669 	 * TODO: Validate the checks without the relevant lock are OK.
7670 	 */
7671 
7672 	if (__improbable(perfcontrol_failsafe_active == TRUE)) {
7673 		/* keep track of how long the responsible thread runs */
7674 		uint64_t cur_th_time = recount_current_thread_time_mach();
7675 
7676 		simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7677 
7678 		if (perfcontrol_failsafe_active == TRUE &&
7679 		    cur_thread->thread_id == perfcontrol_failsafe_tid) {
7680 			perfcontrol_failsafe_thread_timer_last_seen = cur_th_time;
7681 		}
7682 
7683 		simple_unlock(&sched_available_cores_lock);
7684 
7685 		/* we're already trying to solve the problem, so bail */
7686 		return;
7687 	}
7688 
7689 	/* The failsafe won't help if there are no more processors to enable */
7690 	if (__probable(perfcontrol_requested_recommended_core_count >= processor_count)) {
7691 		return;
7692 	}
7693 
7694 	uint64_t too_long_ago = ctime - perfcontrol_failsafe_starvation_threshold;
7695 
7696 	/* Use the maintenance thread as our canary in the coal mine */
7697 	thread_t m_thread = sched_maintenance_thread;
7698 
7699 	/* If it doesn't look bad, nothing to see here */
7700 	if (__probable(m_thread->last_made_runnable_time >= too_long_ago)) {
7701 		return;
7702 	}
7703 
7704 	/* It looks bad, take the lock to be sure */
7705 	thread_lock(m_thread);
7706 
7707 	if (m_thread->runq == PROCESSOR_NULL ||
7708 	    (m_thread->state & (TH_RUN | TH_WAIT)) != TH_RUN ||
7709 	    m_thread->last_made_runnable_time >= too_long_ago) {
7710 		/*
7711 		 * Maintenance thread is either on cpu or blocked, and
7712 		 * therefore wouldn't benefit from more cores
7713 		 */
7714 		thread_unlock(m_thread);
7715 		return;
7716 	}
7717 
7718 	uint64_t maintenance_runnable_time = m_thread->last_made_runnable_time;
7719 
7720 	thread_unlock(m_thread);
7721 
7722 	/*
7723 	 * There are cores disabled at perfcontrol's recommendation, but the
7724 	 * system is so overloaded that the maintenance thread can't run.
7725 	 * That likely means that perfcontrol can't run either, so it can't fix
7726 	 * the recommendation.  We have to kick in a failsafe to keep from starving.
7727 	 *
7728 	 * When the maintenance thread has been starved for too long,
7729 	 * ignore the recommendation from perfcontrol and light up all the cores.
7730 	 *
7731 	 * TODO: Consider weird states like boot, sleep, or debugger
7732 	 */
7733 
7734 	simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7735 
7736 	if (perfcontrol_failsafe_active == TRUE) {
7737 		simple_unlock(&sched_available_cores_lock);
7738 		return;
7739 	}
7740 
7741 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
7742 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_REC_CORES_FAILSAFE) | DBG_FUNC_START,
7743 	    perfcontrol_requested_recommended_cores, maintenance_runnable_time, 0, 0, 0);
7744 
7745 	perfcontrol_failsafe_active = TRUE;
7746 	perfcontrol_failsafe_activation_time = mach_absolute_time();
7747 	perfcontrol_failsafe_maintenance_runnable_time = maintenance_runnable_time;
7748 	perfcontrol_failsafe_recommended_at_trigger = perfcontrol_requested_recommended_cores;
7749 
7750 	/* Capture some data about who screwed up (assuming that the thread on core is at fault) */
7751 	task_t task = get_threadtask(cur_thread);
7752 	perfcontrol_failsafe_pid = task_pid(task);
7753 	strlcpy(perfcontrol_failsafe_name, proc_name_address(get_bsdtask_info(task)), sizeof(perfcontrol_failsafe_name));
7754 
7755 	perfcontrol_failsafe_tid = cur_thread->thread_id;
7756 
7757 	/* Blame the thread for time it has run recently */
7758 	uint64_t recent_computation = (ctime - cur_thread->computation_epoch) + cur_thread->computation_metered;
7759 
7760 	uint64_t last_seen = recount_current_thread_time_mach();
7761 
7762 	/* Compute the start time of the bad behavior in terms of the thread's on core time */
7763 	perfcontrol_failsafe_thread_timer_at_start  = last_seen - recent_computation;
7764 	perfcontrol_failsafe_thread_timer_last_seen = last_seen;
7765 
7766 	/* Ignore the previously recommended core configuration */
7767 	sched_update_recommended_cores(ALL_CORES_RECOMMENDED, REASON_SYSTEM, 0);
7768 
7769 	simple_unlock(&sched_available_cores_lock);
7770 }
7771 
7772 /*
7773  * Now that our bacon has been saved by the failsafe, consider whether to turn it off
7774  *
7775  * Runs in the context of the maintenance thread, no locks held
7776  */
7777 static void
sched_recommended_cores_maintenance(void)7778 sched_recommended_cores_maintenance(void)
7779 {
7780 	/* Common case - no failsafe, nothing to be done here */
7781 	if (__probable(perfcontrol_failsafe_active == FALSE)) {
7782 		return;
7783 	}
7784 
7785 	uint64_t ctime = mach_absolute_time();
7786 
7787 	boolean_t print_diagnostic = FALSE;
7788 	char p_name[FAILSAFE_NAME_LEN] = "";
7789 
7790 	spl_t s = splsched();
7791 	simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7792 
7793 	/* Check again, under the lock, to avoid races */
7794 	if (perfcontrol_failsafe_active == FALSE) {
7795 		goto out;
7796 	}
7797 
7798 	/*
7799 	 * Ensure that the other cores get another few ticks to run some threads
7800 	 * If we don't have this hysteresis, the maintenance thread is the first
7801 	 * to run, and then it immediately kills the other cores
7802 	 */
7803 	if ((ctime - perfcontrol_failsafe_activation_time) < perfcontrol_failsafe_starvation_threshold) {
7804 		goto out;
7805 	}
7806 
7807 	/* Capture some diagnostic state under the lock so we can print it out later */
7808 
7809 	int      pid = perfcontrol_failsafe_pid;
7810 	uint64_t tid = perfcontrol_failsafe_tid;
7811 
7812 	uint64_t thread_usage       = perfcontrol_failsafe_thread_timer_last_seen -
7813 	    perfcontrol_failsafe_thread_timer_at_start;
7814 	uint64_t rec_cores_before   = perfcontrol_failsafe_recommended_at_trigger;
7815 	uint64_t rec_cores_after    = perfcontrol_requested_recommended_cores;
7816 	uint64_t failsafe_duration  = ctime - perfcontrol_failsafe_activation_time;
7817 	strlcpy(p_name, perfcontrol_failsafe_name, sizeof(p_name));
7818 
7819 	print_diagnostic = TRUE;
7820 
7821 	/* Deactivate the failsafe and reinstate the requested recommendation settings */
7822 
7823 	perfcontrol_failsafe_deactivation_time = ctime;
7824 	perfcontrol_failsafe_active = FALSE;
7825 
7826 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
7827 	    MACHDBG_CODE(DBG_MACH_SCHED, MACH_REC_CORES_FAILSAFE) | DBG_FUNC_END,
7828 	    perfcontrol_requested_recommended_cores, failsafe_duration, 0, 0, 0);
7829 
7830 	sched_update_recommended_cores(perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores,
7831 	    REASON_NONE, 0);
7832 
7833 out:
7834 	simple_unlock(&sched_available_cores_lock);
7835 	splx(s);
7836 
7837 	if (print_diagnostic) {
7838 		uint64_t failsafe_duration_ms = 0, thread_usage_ms = 0;
7839 
7840 		absolutetime_to_nanoseconds(failsafe_duration, &failsafe_duration_ms);
7841 		failsafe_duration_ms = failsafe_duration_ms / NSEC_PER_MSEC;
7842 
7843 		absolutetime_to_nanoseconds(thread_usage, &thread_usage_ms);
7844 		thread_usage_ms = thread_usage_ms / NSEC_PER_MSEC;
7845 
7846 		printf("recommended core failsafe kicked in for %lld ms "
7847 		    "likely due to %s[%d] thread 0x%llx spending "
7848 		    "%lld ms on cpu at realtime priority - "
7849 		    "new recommendation: 0x%llx -> 0x%llx\n",
7850 		    failsafe_duration_ms, p_name, pid, tid, thread_usage_ms,
7851 		    rec_cores_before, rec_cores_after);
7852 	}
7853 }
7854 
7855 #endif /* __arm64__ */
7856 
7857 kern_return_t
sched_processor_enable(processor_t processor,boolean_t enable)7858 sched_processor_enable(processor_t processor, boolean_t enable)
7859 {
7860 	assert(preemption_enabled());
7861 
7862 	if (processor == master_processor) {
7863 		/* The system can hang if this is allowed */
7864 		return KERN_NOT_SUPPORTED;
7865 	}
7866 
7867 	spl_t s = splsched();
7868 	simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7869 
7870 	if (enable) {
7871 		bit_set(usercontrol_requested_recommended_cores, processor->cpu_id);
7872 	} else {
7873 		bit_clear(usercontrol_requested_recommended_cores, processor->cpu_id);
7874 	}
7875 
7876 #if __arm64__
7877 	if ((perfcontrol_failsafe_active == false) && (perfcontrol_sleep_override == false)) {
7878 		sched_update_recommended_cores(perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores,
7879 		    REASON_USER, 0);
7880 	} else {
7881 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
7882 		    MACHDBG_CODE(DBG_MACH_SCHED, MACH_REC_CORES_FAILSAFE) | DBG_FUNC_NONE,
7883 		    perfcontrol_requested_recommended_cores,
7884 		    sched_maintenance_thread->last_made_runnable_time, 0, 0, 0);
7885 	}
7886 #else /* __arm64__ */
7887 	sched_update_recommended_cores(usercontrol_requested_recommended_cores, REASON_USER, 0);
7888 #endif /* ! __arm64__ */
7889 
7890 	simple_unlock(&sched_available_cores_lock);
7891 	splx(s);
7892 
7893 	return KERN_SUCCESS;
7894 }
7895 
7896 void
sched_mark_processor_online_locked(processor_t processor,__assert_only processor_reason_t reason)7897 sched_mark_processor_online_locked(processor_t processor, __assert_only processor_reason_t reason)
7898 {
7899 	assert((processor != master_processor) || (reason == REASON_SYSTEM));
7900 
7901 	bit_set(sched_online_processors, processor->cpu_id);
7902 }
7903 
7904 kern_return_t
sched_mark_processor_offline(processor_t processor,processor_reason_t reason)7905 sched_mark_processor_offline(processor_t processor, processor_reason_t reason)
7906 {
7907 	assert((processor != master_processor) || (reason == REASON_SYSTEM));
7908 	kern_return_t ret = KERN_SUCCESS;
7909 
7910 	spl_t s = splsched();
7911 	simple_lock(&sched_available_cores_lock, LCK_GRP_NULL);
7912 
7913 	if (reason == REASON_SYSTEM) {
7914 		bit_clear(sched_online_processors, processor->cpu_id);
7915 		simple_unlock(&sched_available_cores_lock);
7916 		splx(s);
7917 		return ret;
7918 	}
7919 
7920 	uint64_t available_cores = sched_online_processors & perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores;
7921 
7922 	if (!bit_test(sched_online_processors, processor->cpu_id)) {
7923 		/* Processor is already offline */
7924 		ret = KERN_NOT_IN_SET;
7925 	} else if (available_cores == BIT(processor->cpu_id)) {
7926 		ret = KERN_RESOURCE_SHORTAGE;
7927 	} else {
7928 		bit_clear(sched_online_processors, processor->cpu_id);
7929 		ret = KERN_SUCCESS;
7930 	}
7931 
7932 	simple_unlock(&sched_available_cores_lock);
7933 	splx(s);
7934 
7935 	return ret;
7936 }
7937 
7938 /*
7939  * Apply a new recommended cores mask to the processors it affects
7940  * Runs after considering failsafes and such
7941  *
7942  * Iterate over processors and update their ->is_recommended field.
7943  * If a processor is running, we let it drain out at its next
7944  * quantum expiration or blocking point. If a processor is idle, there
7945  * may be more work for it to do, so IPI it.
7946  *
7947  * interrupts disabled, sched_available_cores_lock is held
7948  */
7949 static void
sched_update_recommended_cores(uint64_t recommended_cores,processor_reason_t reason,__unused uint32_t flags)7950 sched_update_recommended_cores(uint64_t recommended_cores, processor_reason_t reason, __unused uint32_t flags)
7951 {
7952 	uint64_t        needs_exit_idle_mask = 0x0;
7953 
7954 	KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_UPDATE_REC_CORES) | DBG_FUNC_START,
7955 	    recommended_cores,
7956 #if __arm64__
7957 	    perfcontrol_failsafe_active, 0, 0);
7958 #else /* __arm64__ */
7959 	    0, 0, 0);
7960 #endif /* ! __arm64__ */
7961 
7962 	if (__builtin_popcountll(recommended_cores & sched_online_processors) == 0) {
7963 		bit_set(recommended_cores, master_processor->cpu_id); /* add boot processor or we hang */
7964 	}
7965 
7966 	/* First set recommended cores */
7967 	for (pset_node_t node = &pset_node0; node != NULL; node = node->node_list) {
7968 		for (int pset_id = lsb_first(node->pset_map); pset_id >= 0; pset_id = lsb_next(node->pset_map, pset_id)) {
7969 			processor_set_t pset = pset_array[pset_id];
7970 
7971 			cpumap_t changed_recommendations = (recommended_cores & pset->cpu_bitmask) ^ pset->recommended_bitmask;
7972 			cpumap_t newly_recommended = changed_recommendations & recommended_cores;
7973 
7974 			if (newly_recommended == 0) {
7975 				/* Nothing to do */
7976 				continue;
7977 			}
7978 
7979 			pset_lock(pset);
7980 
7981 			for (int cpu_id = lsb_first(newly_recommended); cpu_id >= 0; cpu_id = lsb_next(newly_recommended, cpu_id)) {
7982 				processor_t processor = processor_array[cpu_id];
7983 				processor->is_recommended = TRUE;
7984 				processor->last_recommend_reason = reason;
7985 				bit_set(pset->recommended_bitmask, processor->cpu_id);
7986 
7987 				if (processor->state == PROCESSOR_IDLE) {
7988 					if (processor != current_processor()) {
7989 						bit_set(needs_exit_idle_mask, processor->cpu_id);
7990 					}
7991 				}
7992 				if ((processor->state != PROCESSOR_OFF_LINE) && (processor->state != PROCESSOR_PENDING_OFFLINE)) {
7993 					os_atomic_inc(&processor_avail_count_user, relaxed);
7994 					if (processor->processor_primary == processor) {
7995 						os_atomic_inc(&primary_processor_avail_count_user, relaxed);
7996 					}
7997 					SCHED(pset_made_schedulable)(processor, pset, false);
7998 				}
7999 			}
8000 			pset_update_rt_stealable_state(pset);
8001 
8002 			pset_unlock(pset);
8003 
8004 			for (int cpu_id = lsb_first(newly_recommended); cpu_id >= 0;
8005 			    cpu_id = lsb_next(newly_recommended, cpu_id)) {
8006 				smr_cpu_up(processor_array[cpu_id],
8007 				    SMR_CPU_REASON_IGNORED);
8008 			}
8009 		}
8010 	}
8011 
8012 	/* Now shutdown not recommended cores */
8013 	for (pset_node_t node = &pset_node0; node != NULL; node = node->node_list) {
8014 		for (int pset_id = lsb_first(node->pset_map); pset_id >= 0; pset_id = lsb_next(node->pset_map, pset_id)) {
8015 			processor_set_t pset = pset_array[pset_id];
8016 
8017 			cpumap_t changed_recommendations = (recommended_cores & pset->cpu_bitmask) ^ pset->recommended_bitmask;
8018 			cpumap_t newly_unrecommended = changed_recommendations & ~recommended_cores;
8019 
8020 			if (newly_unrecommended == 0) {
8021 				/* Nothing to do */
8022 				continue;
8023 			}
8024 
8025 			pset_lock(pset);
8026 
8027 			for (int cpu_id = lsb_first(newly_unrecommended); cpu_id >= 0; cpu_id = lsb_next(newly_unrecommended, cpu_id)) {
8028 				processor_t processor = processor_array[cpu_id];
8029 				sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
8030 
8031 				processor->is_recommended = FALSE;
8032 				if (reason != REASON_NONE) {
8033 					processor->last_derecommend_reason = reason;
8034 				}
8035 				bit_clear(pset->recommended_bitmask, processor->cpu_id);
8036 				if ((processor->state != PROCESSOR_OFF_LINE) && (processor->state != PROCESSOR_PENDING_OFFLINE)) {
8037 					os_atomic_dec(&processor_avail_count_user, relaxed);
8038 					if (processor->processor_primary == processor) {
8039 						os_atomic_dec(&primary_processor_avail_count_user, relaxed);
8040 					}
8041 				}
8042 				pset_update_rt_stealable_state(pset);
8043 
8044 				if ((processor->state == PROCESSOR_RUNNING) || (processor->state == PROCESSOR_DISPATCHING)) {
8045 					ipi_type = SCHED_IPI_IMMEDIATE;
8046 				}
8047 				SCHED(processor_queue_shutdown)(processor);
8048 				/* pset unlocked */
8049 
8050 				SCHED(rt_queue_shutdown)(processor);
8051 
8052 				if (ipi_type == SCHED_IPI_NONE) {
8053 					/*
8054 					 * If the core is idle,
8055 					 * we can directly mark the processor
8056 					 * as "Ignored"
8057 					 *
8058 					 * Otherwise, smr will detect this
8059 					 * during smr_cpu_leave() when the
8060 					 * processor actually idles.
8061 					 */
8062 					smr_cpu_down(processor, SMR_CPU_REASON_IGNORED);
8063 				} else if (processor == current_processor()) {
8064 					ast_on(AST_PREEMPT);
8065 				} else {
8066 					sched_ipi_perform(processor, ipi_type);
8067 				}
8068 
8069 				pset_lock(pset);
8070 			}
8071 			pset_unlock(pset);
8072 		}
8073 	}
8074 
8075 #if defined(__x86_64__)
8076 	commpage_update_active_cpus();
8077 #endif
8078 	/* Issue all pending IPIs now that the pset lock has been dropped */
8079 	for (int cpuid = lsb_first(needs_exit_idle_mask); cpuid >= 0; cpuid = lsb_next(needs_exit_idle_mask, cpuid)) {
8080 		processor_t processor = processor_array[cpuid];
8081 		machine_signal_idle(processor);
8082 	}
8083 
8084 	KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_UPDATE_REC_CORES) | DBG_FUNC_END,
8085 	    needs_exit_idle_mask, 0, 0, 0);
8086 }
8087 
8088 static void
sched_update_powered_cores(uint64_t requested_powered_cores,processor_reason_t reason,uint32_t flags)8089 sched_update_powered_cores(uint64_t requested_powered_cores, processor_reason_t reason, uint32_t flags)
8090 {
8091 	KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_UPDATE_POWERED_CORES) | DBG_FUNC_START,
8092 	    requested_powered_cores, reason, flags, 0);
8093 
8094 	assert((flags & (LOCK_STATE | UNLOCK_STATE)) ? (reason == REASON_SYSTEM) && (requested_powered_cores == ALL_CORES_POWERED) : 1);
8095 
8096 	/*
8097 	 * Loop through newly set requested_powered_cores and start them.
8098 	 * Loop through newly cleared requested_powered_cores and shut them down.
8099 	 */
8100 
8101 	if ((reason == REASON_CLPC_SYSTEM) || (reason == REASON_CLPC_USER)) {
8102 		flags |= SHUTDOWN_TEMPORARY;
8103 	}
8104 
8105 	/* First set powered cores */
8106 	for (pset_node_t node = &pset_node0; node != NULL; node = node->node_list) {
8107 		for (int pset_id = lsb_first(node->pset_map); pset_id >= 0; pset_id = lsb_next(node->pset_map, pset_id)) {
8108 			processor_set_t pset = pset_array[pset_id];
8109 
8110 			spl_t s = splsched();
8111 			pset_lock(pset);
8112 			cpumap_t pset_requested_powered_cores = requested_powered_cores & pset->cpu_bitmask;
8113 			cpumap_t powered_cores = (pset->cpu_state_map[PROCESSOR_IDLE] | pset->cpu_state_map[PROCESSOR_DISPATCHING] | pset->cpu_state_map[PROCESSOR_RUNNING]);
8114 			cpumap_t requested_changes = pset_requested_powered_cores ^ powered_cores;
8115 			pset_unlock(pset);
8116 			splx(s);
8117 
8118 			cpumap_t newly_powered = requested_changes & requested_powered_cores;
8119 
8120 			cpumap_t cpu_map = newly_powered;
8121 
8122 			if (flags & (LOCK_STATE | UNLOCK_STATE)) {
8123 				/*
8124 				 * We need to change the lock state even if
8125 				 * we don't need to change the actual state.
8126 				 */
8127 				cpu_map = pset_requested_powered_cores;
8128 				/* But not the master_processor, which is always implicitly locked */
8129 				bit_clear(cpu_map, master_processor->cpu_id);
8130 			}
8131 
8132 			if (cpu_map == 0) {
8133 				/* Nothing to do */
8134 				continue;
8135 			}
8136 
8137 			int last_start_cpu_id = bit_first(cpu_map);
8138 
8139 			for (int cpu_id = lsb_first(cpu_map); cpu_id >= 0; cpu_id = lsb_next(cpu_map, cpu_id)) {
8140 				processor_t processor = processor_array[cpu_id];
8141 
8142 				if ((flags & WAIT_FOR_LAST_START) && (cpu_id == last_start_cpu_id)) {
8143 					processor_start_reason(processor, reason, flags | WAIT_FOR_START);
8144 				} else {
8145 					processor_start_reason(processor, reason, flags);
8146 				}
8147 			}
8148 		}
8149 	}
8150 
8151 	/* Now shutdown not powered cores */
8152 	for (pset_node_t node = &pset_node0; node != NULL; node = node->node_list) {
8153 		for (int pset_id = lsb_first(node->pset_map); pset_id >= 0; pset_id = lsb_next(node->pset_map, pset_id)) {
8154 			processor_set_t pset = pset_array[pset_id];
8155 
8156 			spl_t s = splsched();
8157 			pset_lock(pset);
8158 			cpumap_t powered_cores = (pset->cpu_state_map[PROCESSOR_IDLE] | pset->cpu_state_map[PROCESSOR_DISPATCHING] | pset->cpu_state_map[PROCESSOR_RUNNING]);
8159 			cpumap_t requested_changes = (requested_powered_cores & pset->cpu_bitmask) ^ powered_cores;
8160 			pset_unlock(pset);
8161 			splx(s);
8162 
8163 			cpumap_t newly_unpowered = requested_changes & ~requested_powered_cores;
8164 
8165 			if (newly_unpowered == 0) {
8166 				/* Nothing to do */
8167 				continue;
8168 			}
8169 
8170 			for (int cpu_id = lsb_first(newly_unpowered); cpu_id >= 0; cpu_id = lsb_next(newly_unpowered, cpu_id)) {
8171 				processor_t processor = processor_array[cpu_id];
8172 
8173 				processor_exit_reason(processor, reason, flags);
8174 			}
8175 		}
8176 	}
8177 
8178 	KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_UPDATE_POWERED_CORES) | DBG_FUNC_END, 0, 0, 0, 0);
8179 }
8180 
8181 void
thread_set_options(uint32_t thopt)8182 thread_set_options(uint32_t thopt)
8183 {
8184 	spl_t x;
8185 	thread_t t = current_thread();
8186 
8187 	x = splsched();
8188 	thread_lock(t);
8189 
8190 	t->options |= thopt;
8191 
8192 	thread_unlock(t);
8193 	splx(x);
8194 }
8195 
8196 void
thread_set_pending_block_hint(thread_t thread,block_hint_t block_hint)8197 thread_set_pending_block_hint(thread_t thread, block_hint_t block_hint)
8198 {
8199 	thread->pending_block_hint = block_hint;
8200 }
8201 
8202 uint32_t
qos_max_parallelism(int qos,uint64_t options)8203 qos_max_parallelism(int qos, uint64_t options)
8204 {
8205 	return SCHED(qos_max_parallelism)(qos, options);
8206 }
8207 
8208 uint32_t
sched_qos_max_parallelism(__unused int qos,uint64_t options)8209 sched_qos_max_parallelism(__unused int qos, uint64_t options)
8210 {
8211 	host_basic_info_data_t hinfo;
8212 	mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
8213 
8214 
8215 	/*
8216 	 * The QOS_PARALLELISM_CLUSTER_SHARED_RESOURCE should be used on AMP platforms only which
8217 	 * implement their own qos_max_parallelism() interfaces.
8218 	 */
8219 	assert((options & QOS_PARALLELISM_CLUSTER_SHARED_RESOURCE) == 0);
8220 
8221 	/* Query the machine layer for core information */
8222 	__assert_only kern_return_t kret = host_info(host_self(), HOST_BASIC_INFO,
8223 	    (host_info_t)&hinfo, &count);
8224 	assert(kret == KERN_SUCCESS);
8225 
8226 	if (options & QOS_PARALLELISM_COUNT_LOGICAL) {
8227 		return hinfo.logical_cpu;
8228 	} else {
8229 		return hinfo.physical_cpu;
8230 	}
8231 }
8232 
8233 int sched_allow_NO_SMT_threads = 1;
8234 bool
thread_no_smt(thread_t thread)8235 thread_no_smt(thread_t thread)
8236 {
8237 	return sched_allow_NO_SMT_threads &&
8238 	       (thread->bound_processor == PROCESSOR_NULL) &&
8239 	       ((thread->sched_flags & TH_SFLAG_NO_SMT) || (get_threadtask(thread)->t_flags & TF_NO_SMT));
8240 }
8241 
8242 bool
processor_active_thread_no_smt(processor_t processor)8243 processor_active_thread_no_smt(processor_t processor)
8244 {
8245 	return sched_allow_NO_SMT_threads && !processor->current_is_bound && processor->current_is_NO_SMT;
8246 }
8247 
8248 #if __arm64__
8249 
8250 /*
8251  * Set up or replace old timer with new timer
8252  *
8253  * Returns true if canceled old timer, false if it did not
8254  */
8255 boolean_t
sched_perfcontrol_update_callback_deadline(uint64_t new_deadline)8256 sched_perfcontrol_update_callback_deadline(uint64_t new_deadline)
8257 {
8258 	/*
8259 	 * Exchange deadline for new deadline, if old deadline was nonzero,
8260 	 * then I cancelled the callback, otherwise I didn't
8261 	 */
8262 
8263 	return os_atomic_xchg(&sched_perfcontrol_callback_deadline, new_deadline,
8264 	           relaxed) != 0;
8265 }
8266 
8267 /*
8268  * Set global SFI window (in usec)
8269  */
8270 kern_return_t
sched_perfcontrol_sfi_set_window(uint64_t window_usecs)8271 sched_perfcontrol_sfi_set_window(uint64_t window_usecs)
8272 {
8273 	kern_return_t ret = KERN_NOT_SUPPORTED;
8274 #if CONFIG_THREAD_GROUPS
8275 	if (window_usecs == 0ULL) {
8276 		ret = sfi_window_cancel();
8277 	} else {
8278 		ret = sfi_set_window(window_usecs);
8279 	}
8280 #endif // CONFIG_THREAD_GROUPS
8281 	return ret;
8282 }
8283 
8284 /*
8285  * Set background and maintenance SFI class offtimes
8286  */
8287 kern_return_t
sched_perfcontrol_sfi_set_bg_offtime(uint64_t offtime_usecs)8288 sched_perfcontrol_sfi_set_bg_offtime(uint64_t offtime_usecs)
8289 {
8290 	kern_return_t ret = KERN_NOT_SUPPORTED;
8291 #if CONFIG_THREAD_GROUPS
8292 	if (offtime_usecs == 0ULL) {
8293 		ret = sfi_class_offtime_cancel(SFI_CLASS_MAINTENANCE);
8294 		ret |= sfi_class_offtime_cancel(SFI_CLASS_DARWIN_BG);
8295 	} else {
8296 		ret = sfi_set_class_offtime(SFI_CLASS_MAINTENANCE, offtime_usecs);
8297 		ret |= sfi_set_class_offtime(SFI_CLASS_DARWIN_BG, offtime_usecs);
8298 	}
8299 #endif // CONFIG_THREAD_GROUPS
8300 	return ret;
8301 }
8302 
8303 /*
8304  * Set utility SFI class offtime
8305  */
8306 kern_return_t
sched_perfcontrol_sfi_set_utility_offtime(uint64_t offtime_usecs)8307 sched_perfcontrol_sfi_set_utility_offtime(uint64_t offtime_usecs)
8308 {
8309 	kern_return_t ret = KERN_NOT_SUPPORTED;
8310 #if CONFIG_THREAD_GROUPS
8311 	if (offtime_usecs == 0ULL) {
8312 		ret = sfi_class_offtime_cancel(SFI_CLASS_UTILITY);
8313 	} else {
8314 		ret = sfi_set_class_offtime(SFI_CLASS_UTILITY, offtime_usecs);
8315 	}
8316 #endif // CONFIG_THREAD_GROUPS
8317 	return ret;
8318 }
8319 
8320 #endif /* __arm64__ */
8321 
8322 #if CONFIG_SCHED_EDGE
8323 
8324 #define SCHED_PSET_LOAD_EWMA_TC_NSECS 10000000u
8325 
8326 /*
8327  * sched_edge_pset_running_higher_bucket()
8328  *
8329  * Routine to calculate cumulative running counts for each scheduling
8330  * bucket. This effectively lets the load calculation calculate if a
8331  * cluster is running any threads at a QoS lower than the thread being
8332  * migrated etc.
8333  */
8334 
8335 static void
sched_edge_pset_running_higher_bucket(processor_set_t pset,uint32_t * running_higher)8336 sched_edge_pset_running_higher_bucket(processor_set_t pset, uint32_t *running_higher)
8337 {
8338 	bitmap_t *active_map = &pset->cpu_state_map[PROCESSOR_RUNNING];
8339 
8340 	/* Edge Scheduler Optimization */
8341 	for (int cpu = bitmap_first(active_map, MAX_CPUS); cpu >= 0; cpu = bitmap_next(active_map, cpu)) {
8342 		sched_bucket_t cpu_bucket = os_atomic_load(&pset->cpu_running_buckets[cpu], relaxed);
8343 		for (sched_bucket_t bucket = cpu_bucket; bucket < TH_BUCKET_SCHED_MAX; bucket++) {
8344 			running_higher[bucket]++;
8345 		}
8346 	}
8347 }
8348 
8349 /*
8350  * sched_update_pset_load_average()
8351  *
8352  * Updates the load average for each sched bucket for a cluster.
8353  * This routine must be called with the pset lock held.
8354  */
8355 void
sched_update_pset_load_average(processor_set_t pset,uint64_t curtime)8356 sched_update_pset_load_average(processor_set_t pset, uint64_t curtime)
8357 {
8358 	int avail_cpu_count = pset_available_cpu_count(pset);
8359 	if (avail_cpu_count == 0) {
8360 		/* Looks like the pset is not runnable any more; nothing to do here */
8361 		return;
8362 	}
8363 
8364 	/*
8365 	 * Edge Scheduler Optimization
8366 	 *
8367 	 * See if more callers of this routine can pass in timestamps to avoid the
8368 	 * mach_absolute_time() call here.
8369 	 */
8370 
8371 	if (!curtime) {
8372 		curtime = mach_absolute_time();
8373 	}
8374 	uint64_t last_update = os_atomic_load(&pset->pset_load_last_update, relaxed);
8375 	int64_t delta_ticks = curtime - last_update;
8376 	if (delta_ticks < 0) {
8377 		return;
8378 	}
8379 
8380 	uint64_t delta_nsecs = 0;
8381 	absolutetime_to_nanoseconds(delta_ticks, &delta_nsecs);
8382 
8383 	if (__improbable(delta_nsecs > UINT32_MAX)) {
8384 		delta_nsecs = UINT32_MAX;
8385 	}
8386 
8387 #if CONFIG_SCHED_EDGE
8388 	/* Update the shared resource load on the pset */
8389 	for (cluster_shared_rsrc_type_t shared_rsrc_type = CLUSTER_SHARED_RSRC_TYPE_MIN; shared_rsrc_type < CLUSTER_SHARED_RSRC_TYPE_COUNT; shared_rsrc_type++) {
8390 		uint64_t shared_rsrc_runnable_load = sched_edge_shared_rsrc_runnable_load(&pset->pset_clutch_root, shared_rsrc_type);
8391 		uint64_t shared_rsrc_running_load = bit_count(pset->cpu_running_cluster_shared_rsrc_thread[shared_rsrc_type]);
8392 		uint64_t new_shared_load = shared_rsrc_runnable_load + shared_rsrc_running_load;
8393 		uint64_t old_shared_load = os_atomic_xchg(&pset->pset_cluster_shared_rsrc_load[shared_rsrc_type], new_shared_load, relaxed);
8394 		if (old_shared_load != new_shared_load) {
8395 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED_CLUTCH, MACH_SCHED_EDGE_CLUSTER_SHARED_LOAD) | DBG_FUNC_NONE, pset->pset_cluster_id, shared_rsrc_type, new_shared_load, shared_rsrc_running_load);
8396 		}
8397 	}
8398 #endif /* CONFIG_SCHED_EDGE */
8399 
8400 	uint32_t running_higher[TH_BUCKET_SCHED_MAX] = {0};
8401 	sched_edge_pset_running_higher_bucket(pset, running_higher);
8402 
8403 	for (sched_bucket_t sched_bucket = TH_BUCKET_FIXPRI; sched_bucket < TH_BUCKET_SCHED_MAX; sched_bucket++) {
8404 		uint64_t old_load_average = os_atomic_load(&pset->pset_load_average[sched_bucket], relaxed);
8405 		uint64_t old_load_average_factor = old_load_average * SCHED_PSET_LOAD_EWMA_TC_NSECS;
8406 		uint32_t current_runq_depth = (sched_edge_cluster_cumulative_count(&pset->pset_clutch_root, sched_bucket) +  rt_runq_count(pset) + running_higher[sched_bucket]) / avail_cpu_count;
8407 
8408 		/*
8409 		 * For the new load average multiply current_runq_depth by delta_nsecs (which resuts in a 32.0 value).
8410 		 * Since we want to maintain the load average as a 24.8 fixed arithmetic value for precision, the
8411 		 * new load averga needs to be shifted before it can be added to the old load average.
8412 		 */
8413 		uint64_t new_load_average_factor = (current_runq_depth * delta_nsecs) << SCHED_PSET_LOAD_EWMA_FRACTION_BITS;
8414 
8415 		/*
8416 		 * For extremely parallel workloads, it is important that the load average on a cluster moves zero to non-zero
8417 		 * instantly to allow threads to be migrated to other (potentially idle) clusters quickly. Hence use the EWMA
8418 		 * when the system is already loaded; otherwise for an idle system use the latest load average immediately.
8419 		 */
8420 		int old_load_shifted = (int)((old_load_average + SCHED_PSET_LOAD_EWMA_ROUND_BIT) >> SCHED_PSET_LOAD_EWMA_FRACTION_BITS);
8421 		boolean_t load_uptick = (old_load_shifted == 0) && (current_runq_depth != 0);
8422 		boolean_t load_downtick = (old_load_shifted != 0) && (current_runq_depth == 0);
8423 		uint64_t load_average;
8424 		if (load_uptick || load_downtick) {
8425 			load_average = (current_runq_depth << SCHED_PSET_LOAD_EWMA_FRACTION_BITS);
8426 		} else {
8427 			/* Indicates a loaded system; use EWMA for load average calculation */
8428 			load_average = (old_load_average_factor + new_load_average_factor) / (delta_nsecs + SCHED_PSET_LOAD_EWMA_TC_NSECS);
8429 		}
8430 		os_atomic_store(&pset->pset_load_average[sched_bucket], load_average, relaxed);
8431 		if (load_average != old_load_average) {
8432 			KTRC(MACHDBG_CODE(DBG_MACH_SCHED_CLUTCH, MACH_SCHED_EDGE_LOAD_AVG) | DBG_FUNC_NONE, pset->pset_cluster_id, (load_average >> SCHED_PSET_LOAD_EWMA_FRACTION_BITS), load_average & SCHED_PSET_LOAD_EWMA_FRACTION_MASK, sched_bucket);
8433 		}
8434 	}
8435 	os_atomic_store(&pset->pset_load_last_update, curtime, relaxed);
8436 }
8437 
8438 void
sched_update_pset_avg_execution_time(processor_set_t pset,uint64_t execution_time,uint64_t curtime,sched_bucket_t sched_bucket)8439 sched_update_pset_avg_execution_time(processor_set_t pset, uint64_t execution_time, uint64_t curtime, sched_bucket_t sched_bucket)
8440 {
8441 	pset_execution_time_t old_execution_time_packed, new_execution_time_packed;
8442 	uint64_t avg_thread_execution_time = 0;
8443 
8444 	os_atomic_rmw_loop(&pset->pset_execution_time[sched_bucket].pset_execution_time_packed,
8445 	    old_execution_time_packed.pset_execution_time_packed,
8446 	    new_execution_time_packed.pset_execution_time_packed, relaxed, {
8447 		uint64_t last_update = old_execution_time_packed.pset_execution_time_last_update;
8448 		int64_t delta_ticks = curtime - last_update;
8449 		if (delta_ticks < 0) {
8450 		        /*
8451 		         * Its possible that another CPU came in and updated the pset_execution_time
8452 		         * before this CPU could do it. Since the average execution time is meant to
8453 		         * be an approximate measure per cluster, ignore the older update.
8454 		         */
8455 		        os_atomic_rmw_loop_give_up(return );
8456 		}
8457 		uint64_t delta_nsecs = 0;
8458 		absolutetime_to_nanoseconds(delta_ticks, &delta_nsecs);
8459 
8460 		uint64_t nanotime = 0;
8461 		absolutetime_to_nanoseconds(execution_time, &nanotime);
8462 		uint64_t execution_time_us = nanotime / NSEC_PER_USEC;
8463 
8464 		uint64_t old_execution_time = (old_execution_time_packed.pset_avg_thread_execution_time * SCHED_PSET_LOAD_EWMA_TC_NSECS);
8465 		uint64_t new_execution_time = (execution_time_us * delta_nsecs);
8466 
8467 		avg_thread_execution_time = (old_execution_time + new_execution_time) / (delta_nsecs + SCHED_PSET_LOAD_EWMA_TC_NSECS);
8468 		new_execution_time_packed.pset_avg_thread_execution_time = avg_thread_execution_time;
8469 		new_execution_time_packed.pset_execution_time_last_update = curtime;
8470 	});
8471 	if (new_execution_time_packed.pset_avg_thread_execution_time != old_execution_time_packed.pset_execution_time_packed) {
8472 		KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PSET_AVG_EXEC_TIME) | DBG_FUNC_NONE, pset->pset_cluster_id, avg_thread_execution_time, sched_bucket);
8473 	}
8474 }
8475 
8476 uint64_t
sched_pset_cluster_shared_rsrc_load(processor_set_t pset,cluster_shared_rsrc_type_t shared_rsrc_type)8477 sched_pset_cluster_shared_rsrc_load(processor_set_t pset, cluster_shared_rsrc_type_t shared_rsrc_type)
8478 {
8479 	return os_atomic_load(&pset->pset_cluster_shared_rsrc_load[shared_rsrc_type], relaxed);
8480 }
8481 
8482 #else /* CONFIG_SCHED_EDGE */
8483 
8484 void
sched_update_pset_load_average(processor_set_t pset,__unused uint64_t curtime)8485 sched_update_pset_load_average(processor_set_t pset, __unused uint64_t curtime)
8486 {
8487 	int non_rt_load = pset->pset_runq.count;
8488 	int load = ((bit_count(pset->cpu_state_map[PROCESSOR_RUNNING]) + non_rt_load + rt_runq_count(pset)) << PSET_LOAD_NUMERATOR_SHIFT);
8489 	int new_load_average = ((int)pset->load_average + load) >> 1;
8490 
8491 	pset->load_average = new_load_average;
8492 #if (DEVELOPMENT || DEBUG)
8493 #if __AMP__
8494 	if (pset->pset_cluster_type == PSET_AMP_P) {
8495 		KTRC(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PSET_LOAD_AVERAGE) | DBG_FUNC_NONE, sched_get_pset_load_average(pset, 0), (bit_count(pset->cpu_state_map[PROCESSOR_RUNNING]) + pset->pset_runq.count + rt_runq_count(pset)));
8496 	}
8497 #endif
8498 #endif
8499 }
8500 
8501 void
sched_update_pset_avg_execution_time(__unused processor_set_t pset,__unused uint64_t execution_time,__unused uint64_t curtime,__unused sched_bucket_t sched_bucket)8502 sched_update_pset_avg_execution_time(__unused processor_set_t pset, __unused uint64_t execution_time, __unused uint64_t curtime, __unused sched_bucket_t sched_bucket)
8503 {
8504 }
8505 
8506 #endif /* CONFIG_SCHED_EDGE */
8507 
8508 /* pset is locked */
8509 static bool
processor_is_fast_track_candidate_for_realtime_thread(processor_set_t pset,processor_t processor)8510 processor_is_fast_track_candidate_for_realtime_thread(processor_set_t pset, processor_t processor)
8511 {
8512 	int cpuid = processor->cpu_id;
8513 #if defined(__x86_64__)
8514 	if (sched_avoid_cpu0 && (cpuid == 0)) {
8515 		return false;
8516 	}
8517 #endif
8518 
8519 	cpumap_t fasttrack_map = pset_available_cpumap(pset) & ~pset->pending_AST_URGENT_cpu_mask & ~pset->realtime_map;
8520 
8521 	return bit_test(fasttrack_map, cpuid);
8522 }
8523 
8524 /* pset is locked */
8525 static processor_t
choose_processor_for_realtime_thread(processor_set_t pset,processor_t skip_processor,bool consider_secondaries,bool skip_spills)8526 choose_processor_for_realtime_thread(processor_set_t pset, processor_t skip_processor, bool consider_secondaries, bool skip_spills)
8527 {
8528 #if defined(__x86_64__)
8529 	bool avoid_cpu0 = sched_avoid_cpu0 && bit_test(pset->cpu_bitmask, 0);
8530 #else
8531 	const bool avoid_cpu0 = false;
8532 #endif
8533 	cpumap_t cpu_map;
8534 
8535 try_again:
8536 	cpu_map = pset_available_cpumap(pset) & ~pset->pending_AST_URGENT_cpu_mask & ~pset->realtime_map;
8537 	if (skip_processor) {
8538 		bit_clear(cpu_map, skip_processor->cpu_id);
8539 	}
8540 	if (skip_spills) {
8541 		cpu_map &= ~pset->rt_pending_spill_cpu_mask;
8542 	}
8543 
8544 	if (avoid_cpu0 && (sched_avoid_cpu0 == 2)) {
8545 		bit_clear(cpu_map, 0);
8546 	}
8547 
8548 	cpumap_t primary_map = cpu_map & pset->primary_map;
8549 	if (avoid_cpu0) {
8550 		primary_map = bit_ror64(primary_map, 1);
8551 	}
8552 
8553 	int rotid = lsb_first(primary_map);
8554 	if (rotid >= 0) {
8555 		int cpuid = avoid_cpu0 ? ((rotid + 1) & 63) : rotid;
8556 
8557 		processor_t processor = processor_array[cpuid];
8558 
8559 		return processor;
8560 	}
8561 
8562 	if (!pset->is_SMT || !sched_allow_rt_smt || !consider_secondaries) {
8563 		goto out;
8564 	}
8565 
8566 	if (avoid_cpu0 && (sched_avoid_cpu0 == 2)) {
8567 		/* Also avoid cpu1 */
8568 		bit_clear(cpu_map, 1);
8569 	}
8570 
8571 	/* Consider secondary processors whose primary is actually running a realtime thread */
8572 	cpumap_t secondary_map = cpu_map & ~pset->primary_map & (pset->realtime_map << 1);
8573 	if (avoid_cpu0) {
8574 		/* Also avoid cpu1 */
8575 		secondary_map = bit_ror64(secondary_map, 2);
8576 	}
8577 	rotid = lsb_first(secondary_map);
8578 	if (rotid >= 0) {
8579 		int cpuid = avoid_cpu0 ?  ((rotid + 2) & 63) : rotid;
8580 
8581 		processor_t processor = processor_array[cpuid];
8582 
8583 		return processor;
8584 	}
8585 
8586 	/* Consider secondary processors */
8587 	secondary_map = cpu_map & ~pset->primary_map;
8588 	if (avoid_cpu0) {
8589 		/* Also avoid cpu1 */
8590 		secondary_map = bit_ror64(secondary_map, 2);
8591 	}
8592 	rotid = lsb_first(secondary_map);
8593 	if (rotid >= 0) {
8594 		int cpuid = avoid_cpu0 ?  ((rotid + 2) & 63) : rotid;
8595 
8596 		processor_t processor = processor_array[cpuid];
8597 
8598 		return processor;
8599 	}
8600 
8601 	/*
8602 	 * I was hoping the compiler would optimize
8603 	 * this away when avoid_cpu0 is const bool false
8604 	 * but it still complains about the assignmnent
8605 	 * in that case.
8606 	 */
8607 	if (avoid_cpu0 && (sched_avoid_cpu0 == 2)) {
8608 #if defined(__x86_64__)
8609 		avoid_cpu0 = false;
8610 #else
8611 		assert(0);
8612 #endif
8613 		goto try_again;
8614 	}
8615 
8616 out:
8617 	if (skip_processor) {
8618 		return PROCESSOR_NULL;
8619 	}
8620 
8621 	/*
8622 	 * If we didn't find an obvious processor to choose, but there are still more CPUs
8623 	 * not already running realtime threads than realtime threads in the realtime run queue,
8624 	 * this thread belongs in this pset, so choose some other processor in this pset
8625 	 * to ensure the thread is enqueued here.
8626 	 */
8627 	cpumap_t non_realtime_map = pset_available_cpumap(pset) & pset->primary_map & ~pset->realtime_map;
8628 	if (bit_count(non_realtime_map) > rt_runq_count(pset)) {
8629 		cpu_map = non_realtime_map;
8630 		assert(cpu_map != 0);
8631 		int cpuid = bit_first(cpu_map);
8632 		assert(cpuid >= 0);
8633 		return processor_array[cpuid];
8634 	}
8635 
8636 	if (!pset->is_SMT || !sched_allow_rt_smt || !consider_secondaries) {
8637 		goto skip_secondaries;
8638 	}
8639 
8640 	non_realtime_map = pset_available_cpumap(pset) & ~pset->realtime_map;
8641 	if (bit_count(non_realtime_map) > rt_runq_count(pset)) {
8642 		cpu_map = non_realtime_map;
8643 		assert(cpu_map != 0);
8644 		int cpuid = bit_first(cpu_map);
8645 		assert(cpuid >= 0);
8646 		return processor_array[cpuid];
8647 	}
8648 
8649 skip_secondaries:
8650 	return PROCESSOR_NULL;
8651 }
8652 
8653 /*
8654  * Choose the processor with (1) the lowest priority less than max_pri and (2) the furthest deadline for that priority.
8655  * If all available processors are at max_pri, choose the furthest deadline that is greater than minimum_deadline.
8656  *
8657  * pset is locked.
8658  */
8659 static processor_t
choose_furthest_deadline_processor_for_realtime_thread(processor_set_t pset,int max_pri,uint64_t minimum_deadline,processor_t skip_processor,bool skip_spills,bool include_ast_urgent_pending_cpus)8660 choose_furthest_deadline_processor_for_realtime_thread(processor_set_t pset, int max_pri, uint64_t minimum_deadline, processor_t skip_processor, bool skip_spills, bool include_ast_urgent_pending_cpus)
8661 {
8662 	uint64_t  furthest_deadline = deadline_add(minimum_deadline, rt_deadline_epsilon);
8663 	processor_t fd_processor = PROCESSOR_NULL;
8664 	int lowest_priority = max_pri;
8665 
8666 	cpumap_t cpu_map = pset_available_cpumap(pset) & ~pset->pending_AST_URGENT_cpu_mask;
8667 	if (skip_processor) {
8668 		bit_clear(cpu_map, skip_processor->cpu_id);
8669 	}
8670 	if (skip_spills) {
8671 		cpu_map &= ~pset->rt_pending_spill_cpu_mask;
8672 	}
8673 
8674 	for (int cpuid = bit_first(cpu_map); cpuid >= 0; cpuid = bit_next(cpu_map, cpuid)) {
8675 		processor_t processor = processor_array[cpuid];
8676 
8677 		if (processor->current_pri > lowest_priority) {
8678 			continue;
8679 		}
8680 
8681 		if (processor->current_pri < lowest_priority) {
8682 			lowest_priority = processor->current_pri;
8683 			furthest_deadline = processor->deadline;
8684 			fd_processor = processor;
8685 			continue;
8686 		}
8687 
8688 		if (processor->deadline > furthest_deadline) {
8689 			furthest_deadline = processor->deadline;
8690 			fd_processor = processor;
8691 		}
8692 	}
8693 
8694 	if (fd_processor) {
8695 		return fd_processor;
8696 	}
8697 
8698 	/*
8699 	 * There is a race condition possible when there are multiple processor sets.
8700 	 * choose_processor() takes pset lock A, sees the pending_AST_URGENT_cpu_mask set for a processor in that set and finds no suitable candiate CPU,
8701 	 * so it drops pset lock A and tries to take pset lock B.  Meanwhile the pending_AST_URGENT_cpu_mask CPU is looking for a thread to run and holds
8702 	 * pset lock B. It doesn't find any threads (because the candidate thread isn't yet on any run queue), so drops lock B, takes lock A again to clear
8703 	 * the pending_AST_URGENT_cpu_mask bit, and keeps running the current (far deadline) thread. choose_processor() now has lock B and can only find
8704 	 * the lowest count processor in set B so enqueues it on set B's run queue but doesn't IPI anyone. (The lowest count includes all threads,
8705 	 * near and far deadlines, so will prefer a low count of earlier deadlines to a high count of far deadlines, which is suboptimal for EDF scheduling.
8706 	 * To make a better choice we would need to know how many threads with earlier deadlines than the candidate thread exist on each pset's run queue.
8707 	 * But even if we chose the better run queue, we still wouldn't send an IPI in this case.)
8708 	 *
8709 	 * The migitation is to also look for suitable CPUs that have their pending_AST_URGENT_cpu_mask bit set where there are no earlier deadline threads
8710 	 * on the run queue of that pset.
8711 	 */
8712 	if (include_ast_urgent_pending_cpus && (rt_runq_earliest_deadline(pset) > furthest_deadline)) {
8713 		cpu_map = pset_available_cpumap(pset) & pset->pending_AST_URGENT_cpu_mask;
8714 		assert(skip_processor == PROCESSOR_NULL);
8715 		assert(skip_spills == false);
8716 
8717 		for (int cpuid = bit_first(cpu_map); cpuid >= 0; cpuid = bit_next(cpu_map, cpuid)) {
8718 			processor_t processor = processor_array[cpuid];
8719 
8720 			if (processor->current_pri > lowest_priority) {
8721 				continue;
8722 			}
8723 
8724 			if (processor->current_pri < lowest_priority) {
8725 				lowest_priority = processor->current_pri;
8726 				furthest_deadline = processor->deadline;
8727 				fd_processor = processor;
8728 				continue;
8729 			}
8730 
8731 			if (processor->deadline > furthest_deadline) {
8732 				furthest_deadline = processor->deadline;
8733 				fd_processor = processor;
8734 			}
8735 		}
8736 	}
8737 
8738 	return fd_processor;
8739 }
8740 
8741 /* pset is locked */
8742 static processor_t
choose_next_processor_for_realtime_thread(processor_set_t pset,int max_pri,uint64_t minimum_deadline,processor_t skip_processor,bool consider_secondaries)8743 choose_next_processor_for_realtime_thread(processor_set_t pset, int max_pri, uint64_t minimum_deadline, processor_t skip_processor, bool consider_secondaries)
8744 {
8745 	bool skip_spills = true;
8746 	bool include_ast_urgent_pending_cpus = false;
8747 
8748 	processor_t next_processor = choose_processor_for_realtime_thread(pset, skip_processor, consider_secondaries, skip_spills);
8749 	if (next_processor != PROCESSOR_NULL) {
8750 		return next_processor;
8751 	}
8752 
8753 	next_processor = choose_furthest_deadline_processor_for_realtime_thread(pset, max_pri, minimum_deadline, skip_processor, skip_spills, include_ast_urgent_pending_cpus);
8754 	return next_processor;
8755 }
8756 
8757 #if defined(__x86_64__)
8758 /* pset is locked */
8759 static bool
all_available_primaries_are_running_realtime_threads(processor_set_t pset,bool include_backups)8760 all_available_primaries_are_running_realtime_threads(processor_set_t pset, bool include_backups)
8761 {
8762 	bool avoid_cpu0 = sched_avoid_cpu0 && bit_test(pset->cpu_bitmask, 0);
8763 	int nbackup_cpus = 0;
8764 
8765 	if (include_backups && rt_runq_is_low_latency(pset)) {
8766 		nbackup_cpus = sched_rt_n_backup_processors;
8767 	}
8768 
8769 	cpumap_t cpu_map = pset_available_cpumap(pset) & pset->primary_map & ~pset->realtime_map;
8770 	if (avoid_cpu0 && (sched_avoid_cpu0 == 2)) {
8771 		bit_clear(cpu_map, 0);
8772 	}
8773 	return (rt_runq_count(pset) + nbackup_cpus) > bit_count(cpu_map);
8774 }
8775 
8776 /* pset is locked */
8777 static bool
these_processors_are_running_realtime_threads(processor_set_t pset,uint64_t these_map,bool include_backups)8778 these_processors_are_running_realtime_threads(processor_set_t pset, uint64_t these_map, bool include_backups)
8779 {
8780 	int nbackup_cpus = 0;
8781 
8782 	if (include_backups && rt_runq_is_low_latency(pset)) {
8783 		nbackup_cpus = sched_rt_n_backup_processors;
8784 	}
8785 
8786 	cpumap_t cpu_map = pset_available_cpumap(pset) & these_map & ~pset->realtime_map;
8787 	return (rt_runq_count(pset) + nbackup_cpus) > bit_count(cpu_map);
8788 }
8789 #endif
8790 
8791 static bool
sched_ok_to_run_realtime_thread(processor_set_t pset,processor_t processor,bool as_backup)8792 sched_ok_to_run_realtime_thread(processor_set_t pset, processor_t processor, bool as_backup)
8793 {
8794 	if (!processor->is_recommended) {
8795 		return false;
8796 	}
8797 	bool ok_to_run_realtime_thread = true;
8798 #if defined(__x86_64__)
8799 	bool spill_pending = bit_test(pset->rt_pending_spill_cpu_mask, processor->cpu_id);
8800 	if (spill_pending) {
8801 		return true;
8802 	}
8803 	if (processor->cpu_id == 0) {
8804 		if (sched_avoid_cpu0 == 1) {
8805 			ok_to_run_realtime_thread = these_processors_are_running_realtime_threads(pset, pset->primary_map & ~0x1, as_backup);
8806 		} else if (sched_avoid_cpu0 == 2) {
8807 			ok_to_run_realtime_thread = these_processors_are_running_realtime_threads(pset, ~0x3, as_backup);
8808 		}
8809 	} else if (sched_avoid_cpu0 && (processor->cpu_id == 1) && processor->is_SMT) {
8810 		ok_to_run_realtime_thread = sched_allow_rt_smt && these_processors_are_running_realtime_threads(pset, ~0x2, as_backup);
8811 	} else if (processor->processor_primary != processor) {
8812 		ok_to_run_realtime_thread = (sched_allow_rt_smt && all_available_primaries_are_running_realtime_threads(pset, as_backup));
8813 	}
8814 #else
8815 	(void)pset;
8816 	(void)processor;
8817 	(void)as_backup;
8818 #endif
8819 	return ok_to_run_realtime_thread;
8820 }
8821 
8822 void
sched_pset_made_schedulable(__unused processor_t processor,processor_set_t pset,boolean_t drop_lock)8823 sched_pset_made_schedulable(__unused processor_t processor, processor_set_t pset, boolean_t drop_lock)
8824 {
8825 	if (drop_lock) {
8826 		pset_unlock(pset);
8827 	}
8828 }
8829 
8830 void
thread_set_no_smt(bool set)8831 thread_set_no_smt(bool set)
8832 {
8833 	if (!system_is_SMT) {
8834 		/* Not a machine that supports SMT */
8835 		return;
8836 	}
8837 
8838 	thread_t thread = current_thread();
8839 
8840 	spl_t s = splsched();
8841 	thread_lock(thread);
8842 	if (set) {
8843 		thread->sched_flags |= TH_SFLAG_NO_SMT;
8844 	}
8845 	thread_unlock(thread);
8846 	splx(s);
8847 }
8848 
8849 bool
thread_get_no_smt(void)8850 thread_get_no_smt(void)
8851 {
8852 	return current_thread()->sched_flags & TH_SFLAG_NO_SMT;
8853 }
8854 
8855 extern void task_set_no_smt(task_t);
8856 void
task_set_no_smt(task_t task)8857 task_set_no_smt(task_t task)
8858 {
8859 	if (!system_is_SMT) {
8860 		/* Not a machine that supports SMT */
8861 		return;
8862 	}
8863 
8864 	if (task == TASK_NULL) {
8865 		task = current_task();
8866 	}
8867 
8868 	task_lock(task);
8869 	task->t_flags |= TF_NO_SMT;
8870 	task_unlock(task);
8871 }
8872 
8873 #if DEBUG || DEVELOPMENT
8874 extern void sysctl_task_set_no_smt(char no_smt);
8875 void
sysctl_task_set_no_smt(char no_smt)8876 sysctl_task_set_no_smt(char no_smt)
8877 {
8878 	if (!system_is_SMT) {
8879 		/* Not a machine that supports SMT */
8880 		return;
8881 	}
8882 
8883 	task_t task = current_task();
8884 
8885 	task_lock(task);
8886 	if (no_smt == '1') {
8887 		task->t_flags |= TF_NO_SMT;
8888 	}
8889 	task_unlock(task);
8890 }
8891 
8892 extern char sysctl_task_get_no_smt(void);
8893 char
sysctl_task_get_no_smt(void)8894 sysctl_task_get_no_smt(void)
8895 {
8896 	task_t task = current_task();
8897 
8898 	if (task->t_flags & TF_NO_SMT) {
8899 		return '1';
8900 	}
8901 	return '0';
8902 }
8903 #endif /* DEVELOPMENT || DEBUG */
8904 
8905 
8906 __private_extern__ void
thread_bind_cluster_type(thread_t thread,char cluster_type,bool soft_bound)8907 thread_bind_cluster_type(thread_t thread, char cluster_type, bool soft_bound)
8908 {
8909 #if __AMP__
8910 	spl_t s = splsched();
8911 	thread_lock(thread);
8912 	thread->sched_flags &= ~(TH_SFLAG_BOUND_SOFT);
8913 	thread->th_bound_cluster_id = THREAD_BOUND_CLUSTER_NONE;
8914 	if (soft_bound) {
8915 		thread->sched_flags |= TH_SFLAG_BOUND_SOFT;
8916 	}
8917 	switch (cluster_type) {
8918 	case 'e':
8919 	case 'E':
8920 		if (pset0.pset_cluster_type == PSET_AMP_E) {
8921 			thread->th_bound_cluster_id = pset0.pset_id;
8922 		} else if (pset_node1.psets != PROCESSOR_SET_NULL) {
8923 			thread->th_bound_cluster_id = pset_node1.psets->pset_id;
8924 		}
8925 		break;
8926 	case 'p':
8927 	case 'P':
8928 		if (pset0.pset_cluster_type == PSET_AMP_P) {
8929 			thread->th_bound_cluster_id = pset0.pset_id;
8930 		} else if (pset_node1.psets != PROCESSOR_SET_NULL) {
8931 			thread->th_bound_cluster_id = pset_node1.psets->pset_id;
8932 		}
8933 		break;
8934 	default:
8935 		break;
8936 	}
8937 	thread_unlock(thread);
8938 	splx(s);
8939 
8940 	if (thread == current_thread()) {
8941 		thread_block(THREAD_CONTINUE_NULL);
8942 	}
8943 #else /* __AMP__ */
8944 	(void)thread;
8945 	(void)cluster_type;
8946 	(void)soft_bound;
8947 #endif /* __AMP__ */
8948 }
8949 
8950 extern uint32_t thread_bound_cluster_id(thread_t thread);
8951 uint32_t
thread_bound_cluster_id(thread_t thread)8952 thread_bound_cluster_id(thread_t thread)
8953 {
8954 	return thread->th_bound_cluster_id;
8955 }
8956 
8957 __private_extern__ kern_return_t
thread_bind_cluster_id(thread_t thread,uint32_t cluster_id,thread_bind_option_t options)8958 thread_bind_cluster_id(thread_t thread, uint32_t cluster_id, thread_bind_option_t options)
8959 {
8960 #if __AMP__
8961 
8962 	processor_set_t pset = NULL;
8963 	if (options & (THREAD_BIND_SOFT | THREAD_BIND_ELIGIBLE_ONLY)) {
8964 		/* Validate the inputs for the bind case */
8965 		int max_clusters = ml_get_cluster_count();
8966 		if (cluster_id >= max_clusters) {
8967 			/* Invalid cluster id */
8968 			return KERN_INVALID_ARGUMENT;
8969 		}
8970 		pset = pset_array[cluster_id];
8971 		if (pset == NULL) {
8972 			/* Cluster has not been initialized yet */
8973 			return KERN_INVALID_ARGUMENT;
8974 		}
8975 		if (options & THREAD_BIND_ELIGIBLE_ONLY) {
8976 			if (SCHED(thread_eligible_for_pset(thread, pset)) == false) {
8977 				/* Thread is not recommended for the cluster type */
8978 				return KERN_INVALID_POLICY;
8979 			}
8980 		}
8981 	}
8982 
8983 	if (options & THREAD_UNBIND) {
8984 		/* If the thread was actually not bound to some cluster, nothing to do here */
8985 		if (thread_bound_cluster_id(thread) == THREAD_BOUND_CLUSTER_NONE) {
8986 			return KERN_SUCCESS;
8987 		}
8988 	}
8989 
8990 	spl_t s = splsched();
8991 	thread_lock(thread);
8992 
8993 	/* Unbind the thread from its previous bound state */
8994 	thread->sched_flags &= ~(TH_SFLAG_BOUND_SOFT);
8995 	thread->th_bound_cluster_id = THREAD_BOUND_CLUSTER_NONE;
8996 
8997 	if (options & THREAD_UNBIND) {
8998 		/* Nothing more to do here */
8999 		goto thread_bind_cluster_complete;
9000 	}
9001 
9002 	if (options & THREAD_BIND_SOFT) {
9003 		thread->sched_flags |= TH_SFLAG_BOUND_SOFT;
9004 	}
9005 	thread->th_bound_cluster_id = cluster_id;
9006 
9007 thread_bind_cluster_complete:
9008 	thread_unlock(thread);
9009 	splx(s);
9010 
9011 	if (thread == current_thread()) {
9012 		thread_block(THREAD_CONTINUE_NULL);
9013 	}
9014 #else /* __AMP__ */
9015 	(void)thread;
9016 	(void)cluster_id;
9017 	(void)options;
9018 #endif /* __AMP__ */
9019 	return KERN_SUCCESS;
9020 }
9021 
9022 #if DEVELOPMENT || DEBUG
9023 extern int32_t sysctl_get_bound_cpuid(void);
9024 int32_t
sysctl_get_bound_cpuid(void)9025 sysctl_get_bound_cpuid(void)
9026 {
9027 	int32_t cpuid = -1;
9028 	thread_t self = current_thread();
9029 
9030 	processor_t processor = self->bound_processor;
9031 	if (processor == NULL) {
9032 		cpuid = -1;
9033 	} else {
9034 		cpuid = processor->cpu_id;
9035 	}
9036 
9037 	return cpuid;
9038 }
9039 
9040 extern kern_return_t sysctl_thread_bind_cpuid(int32_t cpuid);
9041 kern_return_t
sysctl_thread_bind_cpuid(int32_t cpuid)9042 sysctl_thread_bind_cpuid(int32_t cpuid)
9043 {
9044 	processor_t processor = PROCESSOR_NULL;
9045 
9046 	if (cpuid == -1) {
9047 		goto unbind;
9048 	}
9049 
9050 	if (cpuid < 0 || cpuid >= MAX_SCHED_CPUS) {
9051 		return KERN_INVALID_VALUE;
9052 	}
9053 
9054 	processor = processor_array[cpuid];
9055 	if (processor == PROCESSOR_NULL) {
9056 		return KERN_INVALID_VALUE;
9057 	}
9058 
9059 #if __AMP__
9060 
9061 	thread_t thread = current_thread();
9062 
9063 	if (thread->th_bound_cluster_id != THREAD_BOUND_CLUSTER_NONE) {
9064 		if ((thread->sched_flags & TH_SFLAG_BOUND_SOFT) == 0) {
9065 			/* Cannot hard-bind an already hard-cluster-bound thread */
9066 			return KERN_NOT_SUPPORTED;
9067 		}
9068 	}
9069 
9070 #endif /* __AMP__ */
9071 
9072 unbind:
9073 	thread_bind(processor);
9074 
9075 	thread_block(THREAD_CONTINUE_NULL);
9076 	return KERN_SUCCESS;
9077 }
9078 
9079 extern char sysctl_get_task_cluster_type(void);
9080 char
sysctl_get_task_cluster_type(void)9081 sysctl_get_task_cluster_type(void)
9082 {
9083 	task_t task = current_task();
9084 	processor_set_t pset_hint = task->pset_hint;
9085 
9086 	if (!pset_hint) {
9087 		return '0';
9088 	}
9089 
9090 #if __AMP__
9091 	if (pset_hint->pset_cluster_type == PSET_AMP_E) {
9092 		return 'E';
9093 	} else if (pset_hint->pset_cluster_type == PSET_AMP_P) {
9094 		return 'P';
9095 	}
9096 #endif
9097 
9098 	return '0';
9099 }
9100 
9101 #if __AMP__
9102 static processor_set_t
find_pset_of_type(pset_cluster_type_t t)9103 find_pset_of_type(pset_cluster_type_t t)
9104 {
9105 	for (pset_node_t node = &pset_node0; node != NULL; node = node->node_list) {
9106 		if (node->pset_cluster_type != t) {
9107 			continue;
9108 		}
9109 
9110 		processor_set_t pset = PROCESSOR_SET_NULL;
9111 		for (int pset_id = lsb_first(node->pset_map); pset_id >= 0; pset_id = lsb_next(node->pset_map, pset_id)) {
9112 			pset = pset_array[pset_id];
9113 			/* Prefer one with recommended processsors */
9114 			if (pset->recommended_bitmask != 0) {
9115 				assert(pset->pset_cluster_type == t);
9116 				return pset;
9117 			}
9118 		}
9119 		/* Otherwise return whatever was found last */
9120 		return pset;
9121 	}
9122 
9123 	return PROCESSOR_SET_NULL;
9124 }
9125 #endif
9126 
9127 extern void sysctl_task_set_cluster_type(char cluster_type);
9128 void
sysctl_task_set_cluster_type(char cluster_type)9129 sysctl_task_set_cluster_type(char cluster_type)
9130 {
9131 	task_t task = current_task();
9132 	processor_set_t pset_hint = PROCESSOR_SET_NULL;
9133 
9134 #if __AMP__
9135 	switch (cluster_type) {
9136 	case 'e':
9137 	case 'E':
9138 		pset_hint = find_pset_of_type(PSET_AMP_E);
9139 		break;
9140 	case 'p':
9141 	case 'P':
9142 		pset_hint = find_pset_of_type(PSET_AMP_P);
9143 		break;
9144 	default:
9145 		break;
9146 	}
9147 
9148 	if (pset_hint) {
9149 		task_lock(task);
9150 		task->t_flags |= TF_USE_PSET_HINT_CLUSTER_TYPE;
9151 		task->pset_hint = pset_hint;
9152 		task_unlock(task);
9153 
9154 		thread_block(THREAD_CONTINUE_NULL);
9155 	}
9156 #else
9157 	(void)cluster_type;
9158 	(void)task;
9159 	(void)pset_hint;
9160 #endif
9161 }
9162 
9163 /*
9164  * The quantum length used for Fixed and RT sched modes. In general the quantum
9165  * can vary - for example for background or QOS.
9166  */
9167 extern uint64_t sysctl_get_quantum_us(void);
9168 uint64_t
sysctl_get_quantum_us(void)9169 sysctl_get_quantum_us(void)
9170 {
9171 	uint32_t quantum;
9172 	uint64_t quantum_ns;
9173 
9174 	quantum = SCHED(initial_quantum_size)(THREAD_NULL);
9175 	absolutetime_to_nanoseconds(quantum, &quantum_ns);
9176 
9177 	return quantum_ns / 1000;
9178 }
9179 
9180 #endif /* DEVELOPMENT || DEBUG */
9181