1 /*
2 * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,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 #include <kern/ast.h>
58 #include <kern/counter.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>
64 #include <kern/restartable.h>
65 #include <kern/spl.h>
66 #include <kern/sfi.h>
67 #if CONFIG_TELEMETRY
68 #include <kern/telemetry.h>
69 #endif
70 #include <kern/waitq.h>
71 #include <kern/ledger.h>
72 #include <kern/machine.h>
73 #include <kern/kpc.h>
74 #include <kperf/kperf.h>
75 #include <mach/policy.h>
76 #include <security/mac_mach_internal.h> // for MACF AST hook
77 #include <stdatomic.h>
78
79 #if CONFIG_ARCADE
80 #include <kern/arcade.h>
81 #endif
82
83 static inline __attribute__((always_inline)) void handle_user_asts_interrupts_enabled(ast_t reasons, thread_t thread, task_t task);
84 static inline __attribute__((always_inline)) void assert_thread_return_to_user(thread_t thread);
85
86 static void __attribute__((noinline, noreturn, disable_tail_calls))
thread_preempted(__unused void * parameter,__unused wait_result_t result)87 thread_preempted(__unused void* parameter, __unused wait_result_t result)
88 {
89 /*
90 * We've been scheduled again after a userspace preemption,
91 * try again to return to userspace.
92 */
93 thread_exception_return();
94 }
95
96 /*
97 * Create a dedicated frame to clarify that this thread has been preempted
98 * while running in kernel space.
99 */
100 static void __attribute__((noinline, disable_tail_calls))
thread_preempted_in_kernel(ast_t urgent_reason)101 thread_preempted_in_kernel(ast_t urgent_reason)
102 {
103 thread_block_reason(THREAD_CONTINUE_NULL, NULL, urgent_reason);
104
105 assert(ml_get_interrupts_enabled() == FALSE);
106 }
107
108 /*
109 * AST_URGENT was detected while in kernel mode
110 * Called with interrupts disabled, returns the same way
111 * Must return to caller
112 */
113 void
ast_taken_kernel(void)114 ast_taken_kernel(void)
115 {
116 assert(ml_get_interrupts_enabled() == FALSE);
117
118 thread_t thread = current_thread();
119
120 /* Idle threads handle preemption themselves */
121 if ((thread->state & TH_IDLE)) {
122 ast_off(AST_PREEMPTION);
123 return;
124 }
125
126 /*
127 * It's possible for this to be called after AST_URGENT
128 * has already been handled, due to races in enable_preemption
129 */
130 if (ast_peek(AST_URGENT) != AST_URGENT) {
131 return;
132 }
133
134 /*
135 * Don't preempt if the thread is already preparing to block.
136 * TODO: the thread can cheese this with clear_wait()
137 */
138 if (waitq_wait_possible(thread) == FALSE) {
139 /* Consume AST_URGENT or the interrupt will call us again */
140 ast_consume(AST_URGENT);
141 return;
142 }
143
144 /* TODO: Should we csw_check again to notice if conditions have changed? */
145
146 ast_t urgent_reason = ast_consume(AST_PREEMPTION);
147
148 assert(urgent_reason & AST_PREEMPT);
149
150 /* We've decided to try context switching */
151 thread_preempted_in_kernel(urgent_reason);
152 }
153
154 /*
155 * An AST flag was set while returning to user mode
156 * Called with interrupts disabled, returns with interrupts enabled
157 * May call continuation instead of returning
158 */
159 void
ast_taken_user(void)160 ast_taken_user(void)
161 {
162 assert(ml_get_interrupts_enabled() == FALSE);
163
164 thread_t thread = current_thread();
165 task_t task = get_threadtask(thread);
166
167 /* We are about to return to userspace, there must not be a pending wait */
168 assert(waitq_wait_possible(thread));
169 assert((thread->state & TH_IDLE) == 0);
170
171 /* TODO: Add more 'return to userspace' assertions here */
172
173 /*
174 * If this thread was urgently preempted in userspace,
175 * take the preemption before processing the ASTs.
176 * The trap handler will call us again if we have more ASTs, so it's
177 * safe to block in a continuation here.
178 */
179 if (ast_peek(AST_URGENT) == AST_URGENT) {
180 ast_t urgent_reason = ast_consume(AST_PREEMPTION);
181
182 assert(urgent_reason & AST_PREEMPT);
183
184 /* TODO: Should we csw_check again to notice if conditions have changed? */
185
186 thread_block_reason(thread_preempted, NULL, urgent_reason);
187 /* NOTREACHED */
188 }
189
190 /*
191 * AST_KEVENT does not send an IPI when setting the ast for a thread running in parallel
192 * on a different processor. Only the ast bit on the thread will be set.
193 *
194 * Force a propagate for concurrent updates without an IPI.
195 */
196 ast_propagate(thread);
197
198 /*
199 * Consume all non-preemption processor ASTs matching reasons
200 * because we're handling them here.
201 *
202 * If one of the AST handlers blocks in a continuation,
203 * we'll reinstate the unserviced thread-level AST flags
204 * from the thread to the processor on context switch.
205 * If one of the AST handlers sets another AST,
206 * the trap handler will call ast_taken_user again.
207 *
208 * We expect the AST handlers not to thread_exception_return
209 * without an ast_propagate or context switch to reinstate
210 * the per-processor ASTs.
211 *
212 * TODO: Why are AST_DTRACE and AST_KPERF not per-thread ASTs?
213 */
214 ast_t reasons = ast_consume(AST_PER_THREAD | AST_KPERF | AST_DTRACE);
215
216 ml_set_interrupts_enabled(TRUE);
217
218 handle_user_asts_interrupts_enabled(reasons, thread, task);
219
220 spl_t s = splsched();
221
222 #if CONFIG_SCHED_SFI
223 /*
224 * SFI is currently a per-processor AST, not a per-thread AST
225 * TODO: SFI should be a per-thread AST
226 */
227 if (ast_consume(AST_SFI) == AST_SFI) {
228 sfi_ast(thread);
229 }
230 #endif
231
232 /* We are about to return to userspace, there must not be a pending wait */
233 assert(waitq_wait_possible(thread));
234
235 /*
236 * We've handled all per-thread ASTs, time to handle non-urgent preemption.
237 *
238 * We delay reading the preemption bits until now in case the thread
239 * blocks while handling per-thread ASTs.
240 *
241 * If one of the AST handlers had managed to set a new AST bit,
242 * thread_exception_return will call ast_taken again.
243 */
244 ast_t preemption_reasons = ast_consume(AST_PREEMPTION);
245
246 if (preemption_reasons & AST_PREEMPT) {
247 /* Conditions may have changed from when the AST_PREEMPT was originally set, so re-check. */
248
249 thread_lock(thread);
250 preemption_reasons = csw_check(thread, current_processor(), (preemption_reasons & AST_QUANTUM));
251 thread_unlock(thread);
252
253 #if CONFIG_SCHED_SFI
254 /* csw_check might tell us that SFI is needed */
255 if (preemption_reasons & AST_SFI) {
256 sfi_ast(thread);
257 }
258 #endif
259
260 if (preemption_reasons & AST_PREEMPT) {
261 /* switching to a continuation implicitly re-enables interrupts */
262 thread_block_reason(thread_preempted, NULL, preemption_reasons);
263 /* NOTREACHED */
264 }
265
266 /*
267 * We previously had a pending AST_PREEMPT, but csw_check
268 * decided that it should no longer be set, and to keep
269 * executing the current thread instead.
270 * Clear the pending preemption timer as we no longer
271 * have a pending AST_PREEMPT to time out.
272 *
273 * TODO: just do the thread block if we see AST_PREEMPT
274 * to avoid taking the pset lock twice.
275 * To do that thread block needs to be smarter
276 * about not context switching when it's not necessary
277 * e.g. the first-timeslice check for queue has priority
278 */
279 clear_pending_nonurgent_preemption(current_processor());
280 }
281
282 splx(s);
283
284 /*
285 * Here's a good place to put assertions of things which must be true
286 * upon return to userspace.
287 */
288 assert_thread_return_to_user(thread);
289 }
290
291 static inline void
handle_user_asts_interrupts_enabled(ast_t reasons,thread_t thread,task_t task)292 handle_user_asts_interrupts_enabled(ast_t reasons, thread_t thread, task_t task)
293 {
294 #if CONFIG_DTRACE
295 if (reasons & AST_DTRACE) {
296 dtrace_ast();
297 }
298 #endif
299
300 #ifdef MACH_BSD
301 if (reasons & AST_BSD) {
302 thread_ast_clear(thread, AST_BSD);
303 bsd_ast(thread);
304 }
305 #endif
306
307 #if CONFIG_MACF
308 if (reasons & AST_MACF) {
309 thread_ast_clear(thread, AST_MACF);
310 mac_thread_userret(thread);
311 }
312 #endif
313
314 #if CONFIG_ARCADE
315 if (reasons & AST_ARCADE) {
316 thread_ast_clear(thread, AST_ARCADE);
317 arcade_ast(thread);
318 }
319 #endif
320
321 if (reasons & AST_APC) {
322 thread_ast_clear(thread, AST_APC);
323 thread_apc_ast(thread);
324 }
325
326 #if HAS_MTE
327 if (reasons & AST_SYNTHESIZE_MACH) {
328 extern void mte_synthesize_async_tag_check_fault(thread_t thread, vm_map_t map);
329 thread_ast_clear(thread, AST_SYNTHESIZE_MACH);
330 mte_synthesize_async_tag_check_fault(thread, get_threadtask(thread)->map);
331 }
332 #endif /* HAS_MTE */
333
334 if (reasons & AST_MACH_EXCEPTION) {
335 thread_ast_clear(thread, AST_MACH_EXCEPTION);
336 mach_exception_ast(thread);
337 }
338
339 if (reasons & AST_LEDGER) {
340 thread_ast_clear(thread, AST_LEDGER);
341 ledger_ast(thread);
342 }
343
344 if (reasons & AST_KPERF) {
345 thread_ast_clear(thread, AST_KPERF);
346 #if CONFIG_CPU_COUNTERS
347 kpc_thread_ast_handler(thread);
348 #endif /* CONFIG_CPU_COUNTERS */
349 kperf_thread_ast_handler(thread);
350 thread->kperf_ast = 0;
351 }
352
353 if (reasons & AST_RESET_PCS) {
354 thread_ast_clear(thread, AST_RESET_PCS);
355 thread_reset_pcs_ast(task, thread);
356 }
357
358 if (reasons & AST_KEVENT) {
359 thread_ast_clear(thread, AST_KEVENT);
360 uint16_t bits = atomic_exchange(&thread->kevent_ast_bits, 0);
361 if (bits) {
362 kevent_ast(thread, bits);
363 }
364 }
365
366 if (reasons & AST_PROC_RESOURCE) {
367 thread_ast_clear(thread, AST_PROC_RESOURCE);
368 task_port_space_ast(task);
369 #if MACH_BSD
370 proc_filedesc_ast(task);
371 #endif /* MACH_BSD */
372 }
373
374 #if CONFIG_TELEMETRY
375 if (reasons & AST_TELEMETRY_ALL) {
376 ast_t telemetry_reasons = reasons & AST_TELEMETRY_ALL;
377 thread_ast_clear(thread, AST_TELEMETRY_ALL);
378 telemetry_ast(thread, telemetry_reasons);
379 }
380 #endif
381
382 #if MACH_ASSERT
383 if (reasons & AST_DEBUG_ASSERT) {
384 thread_ast_clear(thread, AST_DEBUG_ASSERT);
385 thread_debug_return_to_user_ast(thread);
386 }
387 #endif
388 }
389
390 static inline void
assert_thread_return_to_user(thread_t thread)391 assert_thread_return_to_user(thread_t thread)
392 {
393 assert(thread->kern_promotion_schedpri == 0);
394 if (thread->rwlock_count > 0) {
395 panic("rwlock_count is %d for thread %p, possibly it still holds a rwlock", thread->rwlock_count, thread);
396 }
397 assert(thread->priority_floor_count == 0);
398
399 assert3u(0, ==, thread->sched_flags &
400 (TH_SFLAG_WAITQ_PROMOTED |
401 TH_SFLAG_RW_PROMOTED |
402 TH_SFLAG_EXEC_PROMOTED |
403 TH_SFLAG_FLOOR_PROMOTED |
404 TH_SFLAG_DEPRESS));
405
406 #if CONFIG_EXCLAVES
407 assert3u(thread->options & TH_OPT_AOE, ==, 0);
408 #endif /* CONFIG_EXCLAVES */
409 }
410
411 #define ASYNC_THREAD_ASTS_HANDLED (AST_MACH_EXCEPTION | AST_DTRACE | AST_TELEMETRY_ALL | AST_KPERF | AST_DEBUG_ASSERT)
412
413 /*
414 * Check if ASTs need to be handled for threads that do work on other threads (currently
415 * aio threads).
416 * Called and returns with interrupts enabled
417 */
418 void
ast_check_async_thread(void)419 ast_check_async_thread(void)
420 {
421 thread_t thread = current_thread();
422 task_t task = get_threadtask(thread);
423
424 assert(ml_get_interrupts_enabled() == TRUE);
425
426 for (;;) {
427 spl_t s = splsched();
428 ast_t reasons = ast_consume(ASYNC_THREAD_ASTS_HANDLED);
429 splx(s);
430
431 if (!(reasons & ASYNC_THREAD_ASTS_HANDLED)) {
432 break;
433 }
434
435 handle_user_asts_interrupts_enabled(reasons & ASYNC_THREAD_ASTS_HANDLED, thread,
436 task);
437
438 assert_thread_return_to_user(thread);
439 }
440 }
441
442 /*
443 * Set AST flags on current processor
444 * Called at splsched
445 */
446 void
ast_on(ast_t reasons)447 ast_on(ast_t reasons)
448 {
449 ast_t *pending_ast = ast_pending();
450
451 *pending_ast |= reasons;
452 }
453
454 /*
455 * Clear AST flags on current processor
456 * Called at splsched
457 */
458 void
ast_off(ast_t reasons)459 ast_off(ast_t reasons)
460 {
461 ast_t *pending_ast = ast_pending();
462
463 *pending_ast &= ~reasons;
464 }
465
466 /*
467 * Consume the requested subset of the AST flags set on the processor
468 * Return the bits that were set
469 * Called at splsched
470 */
471 ast_t
ast_consume(ast_t reasons)472 ast_consume(ast_t reasons)
473 {
474 ast_t *pending_ast = ast_pending();
475
476 reasons &= *pending_ast;
477 *pending_ast &= ~reasons;
478
479 return reasons;
480 }
481
482 /*
483 * Read the requested subset of the AST flags set on the processor
484 * Return the bits that were set, don't modify the processor
485 * Called at splsched
486 */
487 ast_t
ast_peek(ast_t reasons)488 ast_peek(ast_t reasons)
489 {
490 ast_t *pending_ast = ast_pending();
491
492 reasons &= *pending_ast;
493
494 return reasons;
495 }
496
497 /*
498 * Re-set current processor's per-thread AST flags to those set on thread
499 * Called at splsched
500 */
501 void
ast_context(thread_t thread)502 ast_context(thread_t thread)
503 {
504 ast_t *pending_ast = ast_pending();
505
506 *pending_ast = (*pending_ast & ~AST_PER_THREAD) | thread_ast_get(thread);
507 }
508
509 /*
510 * Propagate ASTs set on a thread to the current processor
511 * Called at splsched
512 */
513 void
ast_propagate(thread_t thread)514 ast_propagate(thread_t thread)
515 {
516 ast_on(thread_ast_get(thread));
517 }
518
519 void
ast_dtrace_on(void)520 ast_dtrace_on(void)
521 {
522 ast_on(AST_DTRACE);
523 }
524