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/misc_protos.h>
60 #include <kern/queue.h>
61 #include <kern/sched_prim.h>
62 #include <kern/thread.h>
63 #include <kern/processor.h>
67 #include <kern/telemetry.h>
69 #include <kern/waitq.h>
70 #include <kern/ledger.h>
71 #include <kern/machine.h>
72 #include <kperf/kperf_kpc.h>
73 #include <mach/policy.h>
74 #include <security/mac_mach_internal.h> // for MACF AST hook
75 #include <stdatomic.h>
77 static void __attribute__((noinline
, noreturn
, disable_tail_calls
))
78 thread_preempted(__unused
void* parameter
, __unused wait_result_t result
)
81 * We've been scheduled again after a userspace preemption,
82 * try again to return to userspace.
84 thread_exception_return();
88 * AST_URGENT was detected while in kernel mode
89 * Called with interrupts disabled, returns the same way
90 * Must return to caller
93 ast_taken_kernel(void)
95 assert(ml_get_interrupts_enabled() == FALSE
);
97 thread_t thread
= current_thread();
99 /* Idle threads handle preemption themselves */
100 if ((thread
->state
& TH_IDLE
)) {
101 ast_off(AST_PREEMPTION
);
106 * It's possible for this to be called after AST_URGENT
107 * has already been handled, due to races in enable_preemption
109 if (ast_peek(AST_URGENT
) != AST_URGENT
)
113 * Don't preempt if the thread is already preparing to block.
114 * TODO: the thread can cheese this with clear_wait()
116 if (waitq_wait_possible(thread
) == FALSE
) {
117 /* Consume AST_URGENT or the interrupt will call us again */
118 ast_consume(AST_URGENT
);
122 /* TODO: Should we csw_check again to notice if conditions have changed? */
124 ast_t urgent_reason
= ast_consume(AST_PREEMPTION
);
126 assert(urgent_reason
& AST_PREEMPT
);
128 counter(c_ast_taken_block
++);
130 thread_block_reason(THREAD_CONTINUE_NULL
, NULL
, urgent_reason
);
132 assert(ml_get_interrupts_enabled() == FALSE
);
136 * An AST flag was set while returning to user mode
137 * Called with interrupts disabled, returns with interrupts enabled
138 * May call continuation instead of returning
143 assert(ml_get_interrupts_enabled() == FALSE
);
145 thread_t thread
= current_thread();
147 /* We are about to return to userspace, there must not be a pending wait */
148 assert(waitq_wait_possible(thread
));
149 assert((thread
->state
& TH_IDLE
) == 0);
151 /* TODO: Add more 'return to userspace' assertions here */
154 * If this thread was urgently preempted in userspace,
155 * take the preemption before processing the ASTs.
156 * The trap handler will call us again if we have more ASTs, so it's
157 * safe to block in a continuation here.
159 if (ast_peek(AST_URGENT
) == AST_URGENT
) {
160 ast_t urgent_reason
= ast_consume(AST_PREEMPTION
);
162 assert(urgent_reason
& AST_PREEMPT
);
164 /* TODO: Should we csw_check again to notice if conditions have changed? */
166 thread_block_reason(thread_preempted
, NULL
, urgent_reason
);
171 * AST_KEVENT does not send an IPI when setting the ast for a thread running in parallel
172 * on a different processor. Only the ast bit on the thread will be set.
174 * Force a propagate for concurrent updates without an IPI.
176 ast_propagate(thread
);
179 * Consume all non-preemption processor ASTs matching reasons
180 * because we're handling them here.
182 * If one of the AST handlers blocks in a continuation,
183 * we'll reinstate the unserviced thread-level AST flags
184 * from the thread to the processor on context switch.
185 * If one of the AST handlers sets another AST,
186 * the trap handler will call ast_taken_user again.
188 * We expect the AST handlers not to thread_exception_return
189 * without an ast_propagate or context switch to reinstate
190 * the per-processor ASTs.
192 * TODO: Why are AST_DTRACE and AST_KPERF not per-thread ASTs?
194 ast_t reasons
= ast_consume(AST_PER_THREAD
| AST_KPERF
| AST_DTRACE
);
196 ml_set_interrupts_enabled(TRUE
);
199 if (reasons
& AST_DTRACE
) {
205 if (reasons
& AST_BSD
) {
206 thread_ast_clear(thread
, AST_BSD
);
212 if (reasons
& AST_MACF
) {
213 thread_ast_clear(thread
, AST_MACF
);
214 mac_thread_userret(thread
);
218 if (reasons
& AST_APC
) {
219 thread_ast_clear(thread
, AST_APC
);
220 thread_apc_ast(thread
);
223 if (reasons
& AST_GUARD
) {
224 thread_ast_clear(thread
, AST_GUARD
);
228 if (reasons
& AST_LEDGER
) {
229 thread_ast_clear(thread
, AST_LEDGER
);
233 if (reasons
& AST_KPERF
) {
234 thread_ast_clear(thread
, AST_KPERF
);
235 kperf_kpc_thread_ast(thread
);
238 if (reasons
& AST_KEVENT
) {
239 thread_ast_clear(thread
, AST_KEVENT
);
240 uint16_t bits
= atomic_exchange(&thread
->kevent_ast_bits
, 0);
241 if (bits
) kevent_ast(thread
, bits
);
245 if (reasons
& AST_TELEMETRY_ALL
) {
246 ast_t telemetry_reasons
= reasons
& AST_TELEMETRY_ALL
;
247 thread_ast_clear(thread
, AST_TELEMETRY_ALL
);
248 telemetry_ast(thread
, telemetry_reasons
);
252 spl_t s
= splsched();
256 * SFI is currently a per-processor AST, not a per-thread AST
257 * TODO: SFI should be a per-thread AST
259 if (ast_consume(AST_SFI
) == AST_SFI
) {
264 /* We are about to return to userspace, there must not be a pending wait */
265 assert(waitq_wait_possible(thread
));
268 * We've handled all per-thread ASTs, time to handle non-urgent preemption.
270 * We delay reading the preemption bits until now in case the thread
271 * blocks while handling per-thread ASTs.
273 * If one of the AST handlers had managed to set a new AST bit,
274 * thread_exception_return will call ast_taken again.
276 ast_t preemption_reasons
= ast_consume(AST_PREEMPTION
);
278 if (preemption_reasons
& AST_PREEMPT
) {
279 /* Conditions may have changed from when the AST_PREEMPT was originally set, so re-check. */
282 preemption_reasons
= csw_check(current_processor(), (preemption_reasons
& AST_QUANTUM
));
283 thread_unlock(thread
);
286 /* csw_check might tell us that SFI is needed */
287 if (preemption_reasons
& AST_SFI
) {
292 if (preemption_reasons
& AST_PREEMPT
) {
293 counter(c_ast_taken_block
++);
294 /* switching to a continuation implicitly re-enables interrupts */
295 thread_block_reason(thread_preempted
, NULL
, preemption_reasons
);
304 * Handle preemption IPI or IPI in response to setting an AST flag
305 * Triggered by cause_ast_check
309 ast_check(processor_t processor
)
311 if (processor
->state
!= PROCESSOR_RUNNING
&&
312 processor
->state
!= PROCESSOR_SHUTDOWN
)
315 thread_t thread
= processor
->active_thread
;
317 assert(thread
== current_thread());
322 * Propagate thread ast to processor.
323 * (handles IPI in response to setting AST flag)
325 ast_propagate(thread
);
327 boolean_t needs_callout
= false;
328 processor
->current_pri
= thread
->sched_pri
;
329 processor
->current_sfi_class
= thread
->sfi_class
= sfi_thread_classify(thread
);
330 processor
->current_recommended_pset_type
= recommended_pset_type(thread
);
331 perfcontrol_class_t thread_class
= thread_get_perfcontrol_class(thread
);
332 if (thread_class
!= processor
->current_perfctl_class
) {
333 /* We updated the perfctl class of this thread from another core.
334 * Since we dont do CLPC callouts from another core, do a callout
335 * here to let CLPC know that the currently running thread has a new
338 needs_callout
= true;
340 processor
->current_perfctl_class
= thread_class
;
344 if ((preempt
= csw_check(processor
, AST_NONE
)) != AST_NONE
)
347 thread_unlock(thread
);
350 machine_switch_perfcontrol_state_update(PERFCONTROL_ATTR_UPDATE
,
351 mach_approximate_time(), 0, thread
);
356 * Set AST flags on current processor
360 ast_on(ast_t reasons
)
362 ast_t
*pending_ast
= ast_pending();
364 *pending_ast
|= reasons
;
368 * Clear AST flags on current processor
372 ast_off(ast_t reasons
)
374 ast_t
*pending_ast
= ast_pending();
376 *pending_ast
&= ~reasons
;
380 * Consume the requested subset of the AST flags set on the processor
381 * Return the bits that were set
385 ast_consume(ast_t reasons
)
387 ast_t
*pending_ast
= ast_pending();
389 reasons
&= *pending_ast
;
390 *pending_ast
&= ~reasons
;
396 * Read the requested subset of the AST flags set on the processor
397 * Return the bits that were set, don't modify the processor
401 ast_peek(ast_t reasons
)
403 ast_t
*pending_ast
= ast_pending();
405 reasons
&= *pending_ast
;
411 * Re-set current processor's per-thread AST flags to those set on thread
415 ast_context(thread_t thread
)
417 ast_t
*pending_ast
= ast_pending();
419 *pending_ast
= ((*pending_ast
& ~AST_PER_THREAD
) | thread
->ast
);
423 * Propagate ASTs set on a thread to the current processor
427 ast_propagate(thread_t thread
)