2 * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
58 #include <kern/counters.h>
59 #include <kern/cpu_quiesce.h>
60 #include <kern/misc_protos.h>
61 #include <kern/queue.h>
62 #include <kern/sched_prim.h>
63 #include <kern/thread.h>
64 #include <kern/processor.h>
65 #include <kern/restartable.h>
69 #include <kern/telemetry.h>
71 #include <kern/waitq.h>
72 #include <kern/ledger.h>
73 #include <kern/machine.h>
74 #include <kperf/kperf_kpc.h>
75 #include <mach/policy.h>
76 #include <security/mac_mach_internal.h> // for MACF AST hook
77 #include <stdatomic.h>
80 #include <kern/arcade.h>
83 static void __attribute__((noinline
, noreturn
, disable_tail_calls
))
84 thread_preempted(__unused
void* parameter
, __unused wait_result_t result
)
87 * We've been scheduled again after a userspace preemption,
88 * try again to return to userspace.
90 thread_exception_return();
94 * AST_URGENT was detected while in kernel mode
95 * Called with interrupts disabled, returns the same way
96 * Must return to caller
99 ast_taken_kernel(void)
101 assert(ml_get_interrupts_enabled() == FALSE
);
103 thread_t thread
= current_thread();
105 /* Idle threads handle preemption themselves */
106 if ((thread
->state
& TH_IDLE
)) {
107 ast_off(AST_PREEMPTION
);
112 * It's possible for this to be called after AST_URGENT
113 * has already been handled, due to races in enable_preemption
115 if (ast_peek(AST_URGENT
) != AST_URGENT
) {
120 * Don't preempt if the thread is already preparing to block.
121 * TODO: the thread can cheese this with clear_wait()
123 if (waitq_wait_possible(thread
) == FALSE
) {
124 /* Consume AST_URGENT or the interrupt will call us again */
125 ast_consume(AST_URGENT
);
129 /* TODO: Should we csw_check again to notice if conditions have changed? */
131 ast_t urgent_reason
= ast_consume(AST_PREEMPTION
);
133 assert(urgent_reason
& AST_PREEMPT
);
135 counter(c_ast_taken_block
++);
137 thread_block_reason(THREAD_CONTINUE_NULL
, NULL
, urgent_reason
);
139 assert(ml_get_interrupts_enabled() == FALSE
);
143 * An AST flag was set while returning to user mode
144 * Called with interrupts disabled, returns with interrupts enabled
145 * May call continuation instead of returning
150 assert(ml_get_interrupts_enabled() == FALSE
);
152 thread_t thread
= current_thread();
154 /* We are about to return to userspace, there must not be a pending wait */
155 assert(waitq_wait_possible(thread
));
156 assert((thread
->state
& TH_IDLE
) == 0);
158 /* TODO: Add more 'return to userspace' assertions here */
161 * If this thread was urgently preempted in userspace,
162 * take the preemption before processing the ASTs.
163 * The trap handler will call us again if we have more ASTs, so it's
164 * safe to block in a continuation here.
166 if (ast_peek(AST_URGENT
) == AST_URGENT
) {
167 ast_t urgent_reason
= ast_consume(AST_PREEMPTION
);
169 assert(urgent_reason
& AST_PREEMPT
);
171 /* TODO: Should we csw_check again to notice if conditions have changed? */
173 thread_block_reason(thread_preempted
, NULL
, urgent_reason
);
178 * AST_KEVENT does not send an IPI when setting the ast for a thread running in parallel
179 * on a different processor. Only the ast bit on the thread will be set.
181 * Force a propagate for concurrent updates without an IPI.
183 ast_propagate(thread
);
186 * Consume all non-preemption processor ASTs matching reasons
187 * because we're handling them here.
189 * If one of the AST handlers blocks in a continuation,
190 * we'll reinstate the unserviced thread-level AST flags
191 * from the thread to the processor on context switch.
192 * If one of the AST handlers sets another AST,
193 * the trap handler will call ast_taken_user again.
195 * We expect the AST handlers not to thread_exception_return
196 * without an ast_propagate or context switch to reinstate
197 * the per-processor ASTs.
199 * TODO: Why are AST_DTRACE and AST_KPERF not per-thread ASTs?
201 ast_t reasons
= ast_consume(AST_PER_THREAD
| AST_KPERF
| AST_DTRACE
);
203 ml_set_interrupts_enabled(TRUE
);
206 if (reasons
& AST_DTRACE
) {
212 if (reasons
& AST_BSD
) {
213 thread_ast_clear(thread
, AST_BSD
);
219 if (reasons
& AST_MACF
) {
220 thread_ast_clear(thread
, AST_MACF
);
221 mac_thread_userret(thread
);
226 if (reasons
& AST_ARCADE
) {
227 thread_ast_clear(thread
, AST_ARCADE
);
232 if (reasons
& AST_APC
) {
233 thread_ast_clear(thread
, AST_APC
);
234 thread_apc_ast(thread
);
237 if (reasons
& AST_GUARD
) {
238 thread_ast_clear(thread
, AST_GUARD
);
242 if (reasons
& AST_LEDGER
) {
243 thread_ast_clear(thread
, AST_LEDGER
);
247 if (reasons
& AST_KPERF
) {
248 thread_ast_clear(thread
, AST_KPERF
);
249 kperf_kpc_thread_ast(thread
);
252 if (reasons
& AST_RESET_PCS
) {
253 thread_ast_clear(thread
, AST_RESET_PCS
);
254 thread_reset_pcs_ast(thread
);
257 if (reasons
& AST_KEVENT
) {
258 thread_ast_clear(thread
, AST_KEVENT
);
259 uint16_t bits
= atomic_exchange(&thread
->kevent_ast_bits
, 0);
261 kevent_ast(thread
, bits
);
266 if (reasons
& AST_TELEMETRY_ALL
) {
267 ast_t telemetry_reasons
= reasons
& AST_TELEMETRY_ALL
;
268 thread_ast_clear(thread
, AST_TELEMETRY_ALL
);
269 telemetry_ast(thread
, telemetry_reasons
);
273 spl_t s
= splsched();
277 * SFI is currently a per-processor AST, not a per-thread AST
278 * TODO: SFI should be a per-thread AST
280 if (ast_consume(AST_SFI
) == AST_SFI
) {
285 /* We are about to return to userspace, there must not be a pending wait */
286 assert(waitq_wait_possible(thread
));
289 * We've handled all per-thread ASTs, time to handle non-urgent preemption.
291 * We delay reading the preemption bits until now in case the thread
292 * blocks while handling per-thread ASTs.
294 * If one of the AST handlers had managed to set a new AST bit,
295 * thread_exception_return will call ast_taken again.
297 ast_t preemption_reasons
= ast_consume(AST_PREEMPTION
);
299 if (preemption_reasons
& AST_PREEMPT
) {
300 /* Conditions may have changed from when the AST_PREEMPT was originally set, so re-check. */
303 preemption_reasons
= csw_check(thread
, current_processor(), (preemption_reasons
& AST_QUANTUM
));
304 thread_unlock(thread
);
307 /* csw_check might tell us that SFI is needed */
308 if (preemption_reasons
& AST_SFI
) {
313 if (preemption_reasons
& AST_PREEMPT
) {
314 counter(c_ast_taken_block
++);
315 /* switching to a continuation implicitly re-enables interrupts */
316 thread_block_reason(thread_preempted
, NULL
, preemption_reasons
);
321 if (ast_consume(AST_UNQUIESCE
) == AST_UNQUIESCE
) {
322 cpu_quiescent_counter_ast();
325 cpu_quiescent_counter_assert_ast();
330 * Here's a good place to put assertions of things which must be true
331 * upon return to userspace.
333 assert((thread
->sched_flags
& TH_SFLAG_WAITQ_PROMOTED
) == 0);
334 assert((thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
) == 0);
335 assert((thread
->sched_flags
& TH_SFLAG_EXEC_PROMOTED
) == 0);
336 assert((thread
->sched_flags
& TH_SFLAG_PROMOTED
) == 0);
337 assert((thread
->sched_flags
& TH_SFLAG_DEPRESS
) == 0);
339 assert(thread
->kern_promotion_schedpri
== 0);
340 assert(thread
->waiting_for_mutex
== NULL
);
341 assert(thread
->rwlock_count
== 0);
345 * Set AST flags on current processor
349 ast_on(ast_t reasons
)
351 ast_t
*pending_ast
= ast_pending();
353 *pending_ast
|= reasons
;
357 * Clear AST flags on current processor
361 ast_off(ast_t reasons
)
363 ast_t
*pending_ast
= ast_pending();
365 *pending_ast
&= ~reasons
;
369 * Consume the requested subset of the AST flags set on the processor
370 * Return the bits that were set
374 ast_consume(ast_t reasons
)
376 ast_t
*pending_ast
= ast_pending();
378 reasons
&= *pending_ast
;
379 *pending_ast
&= ~reasons
;
385 * Read the requested subset of the AST flags set on the processor
386 * Return the bits that were set, don't modify the processor
390 ast_peek(ast_t reasons
)
392 ast_t
*pending_ast
= ast_pending();
394 reasons
&= *pending_ast
;
400 * Re-set current processor's per-thread AST flags to those set on thread
404 ast_context(thread_t thread
)
406 ast_t
*pending_ast
= ast_pending();
408 *pending_ast
= ((*pending_ast
& ~AST_PER_THREAD
) | thread
->ast
);
412 * Propagate ASTs set on a thread to the current processor
416 ast_propagate(thread_t thread
)