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/counter.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 thread_block_reason(THREAD_CONTINUE_NULL
, NULL
, urgent_reason
);
137 assert(ml_get_interrupts_enabled() == FALSE
);
141 * An AST flag was set while returning to user mode
142 * Called with interrupts disabled, returns with interrupts enabled
143 * May call continuation instead of returning
148 assert(ml_get_interrupts_enabled() == FALSE
);
150 thread_t thread
= current_thread();
152 /* We are about to return to userspace, there must not be a pending wait */
153 assert(waitq_wait_possible(thread
));
154 assert((thread
->state
& TH_IDLE
) == 0);
156 /* TODO: Add more 'return to userspace' assertions here */
159 * If this thread was urgently preempted in userspace,
160 * take the preemption before processing the ASTs.
161 * The trap handler will call us again if we have more ASTs, so it's
162 * safe to block in a continuation here.
164 if (ast_peek(AST_URGENT
) == AST_URGENT
) {
165 ast_t urgent_reason
= ast_consume(AST_PREEMPTION
);
167 assert(urgent_reason
& AST_PREEMPT
);
169 /* TODO: Should we csw_check again to notice if conditions have changed? */
171 thread_block_reason(thread_preempted
, NULL
, urgent_reason
);
176 * AST_KEVENT does not send an IPI when setting the ast for a thread running in parallel
177 * on a different processor. Only the ast bit on the thread will be set.
179 * Force a propagate for concurrent updates without an IPI.
181 ast_propagate(thread
);
184 * Consume all non-preemption processor ASTs matching reasons
185 * because we're handling them here.
187 * If one of the AST handlers blocks in a continuation,
188 * we'll reinstate the unserviced thread-level AST flags
189 * from the thread to the processor on context switch.
190 * If one of the AST handlers sets another AST,
191 * the trap handler will call ast_taken_user again.
193 * We expect the AST handlers not to thread_exception_return
194 * without an ast_propagate or context switch to reinstate
195 * the per-processor ASTs.
197 * TODO: Why are AST_DTRACE and AST_KPERF not per-thread ASTs?
199 ast_t reasons
= ast_consume(AST_PER_THREAD
| AST_KPERF
| AST_DTRACE
);
201 ml_set_interrupts_enabled(TRUE
);
204 if (reasons
& AST_DTRACE
) {
210 if (reasons
& AST_BSD
) {
211 thread_ast_clear(thread
, AST_BSD
);
217 if (reasons
& AST_MACF
) {
218 thread_ast_clear(thread
, AST_MACF
);
219 mac_thread_userret(thread
);
224 if (reasons
& AST_ARCADE
) {
225 thread_ast_clear(thread
, AST_ARCADE
);
230 if (reasons
& AST_APC
) {
231 thread_ast_clear(thread
, AST_APC
);
232 thread_apc_ast(thread
);
235 if (reasons
& AST_GUARD
) {
236 thread_ast_clear(thread
, AST_GUARD
);
240 if (reasons
& AST_LEDGER
) {
241 thread_ast_clear(thread
, AST_LEDGER
);
245 if (reasons
& AST_KPERF
) {
246 thread_ast_clear(thread
, AST_KPERF
);
247 kperf_kpc_thread_ast(thread
);
250 if (reasons
& AST_RESET_PCS
) {
251 thread_ast_clear(thread
, AST_RESET_PCS
);
252 thread_reset_pcs_ast(thread
);
255 if (reasons
& AST_KEVENT
) {
256 thread_ast_clear(thread
, AST_KEVENT
);
257 uint16_t bits
= atomic_exchange(&thread
->kevent_ast_bits
, 0);
259 kevent_ast(thread
, bits
);
264 if (reasons
& AST_TELEMETRY_ALL
) {
265 ast_t telemetry_reasons
= reasons
& AST_TELEMETRY_ALL
;
266 thread_ast_clear(thread
, AST_TELEMETRY_ALL
);
267 telemetry_ast(thread
, telemetry_reasons
);
271 spl_t s
= splsched();
275 * SFI is currently a per-processor AST, not a per-thread AST
276 * TODO: SFI should be a per-thread AST
278 if (ast_consume(AST_SFI
) == AST_SFI
) {
283 /* We are about to return to userspace, there must not be a pending wait */
284 assert(waitq_wait_possible(thread
));
287 * We've handled all per-thread ASTs, time to handle non-urgent preemption.
289 * We delay reading the preemption bits until now in case the thread
290 * blocks while handling per-thread ASTs.
292 * If one of the AST handlers had managed to set a new AST bit,
293 * thread_exception_return will call ast_taken again.
295 ast_t preemption_reasons
= ast_consume(AST_PREEMPTION
);
297 if (preemption_reasons
& AST_PREEMPT
) {
298 /* Conditions may have changed from when the AST_PREEMPT was originally set, so re-check. */
301 preemption_reasons
= csw_check(thread
, current_processor(), (preemption_reasons
& AST_QUANTUM
));
302 thread_unlock(thread
);
305 /* csw_check might tell us that SFI is needed */
306 if (preemption_reasons
& AST_SFI
) {
311 if (preemption_reasons
& AST_PREEMPT
) {
312 /* switching to a continuation implicitly re-enables interrupts */
313 thread_block_reason(thread_preempted
, NULL
, preemption_reasons
);
318 if (ast_consume(AST_UNQUIESCE
) == AST_UNQUIESCE
) {
319 cpu_quiescent_counter_ast();
322 cpu_quiescent_counter_assert_ast();
327 * Here's a good place to put assertions of things which must be true
328 * upon return to userspace.
330 assert((thread
->sched_flags
& TH_SFLAG_WAITQ_PROMOTED
) == 0);
331 assert((thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
) == 0);
332 assert((thread
->sched_flags
& TH_SFLAG_EXEC_PROMOTED
) == 0);
333 assert((thread
->sched_flags
& TH_SFLAG_PROMOTED
) == 0);
334 assert((thread
->sched_flags
& TH_SFLAG_DEPRESS
) == 0);
336 assert(thread
->kern_promotion_schedpri
== 0);
337 assert(thread
->waiting_for_mutex
== NULL
);
338 assert(thread
->rwlock_count
== 0);
342 * Set AST flags on current processor
346 ast_on(ast_t reasons
)
348 ast_t
*pending_ast
= ast_pending();
350 *pending_ast
|= reasons
;
354 * Clear AST flags on current processor
358 ast_off(ast_t reasons
)
360 ast_t
*pending_ast
= ast_pending();
362 *pending_ast
&= ~reasons
;
366 * Consume the requested subset of the AST flags set on the processor
367 * Return the bits that were set
371 ast_consume(ast_t reasons
)
373 ast_t
*pending_ast
= ast_pending();
375 reasons
&= *pending_ast
;
376 *pending_ast
&= ~reasons
;
382 * Read the requested subset of the AST flags set on the processor
383 * Return the bits that were set, don't modify the processor
387 ast_peek(ast_t reasons
)
389 ast_t
*pending_ast
= ast_pending();
391 reasons
&= *pending_ast
;
397 * Re-set current processor's per-thread AST flags to those set on thread
401 ast_context(thread_t thread
)
403 ast_t
*pending_ast
= ast_pending();
405 *pending_ast
= ((*pending_ast
& ~AST_PER_THREAD
) | thread
->ast
);
409 * Propagate ASTs set on a thread to the current processor
413 ast_propagate(thread_t thread
)