]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/ast.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / osfmk / kern / ast.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 Software.Distribution@CS.CMU.EDU
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/*
60 *
61 * This file contains routines to check whether an ast is needed.
62 *
63 * ast_check() - check whether ast is needed for interrupt or context
64 * switch. Usually called by clock interrupt handler.
65 *
66 */
67
1c79356b
A
68#include <kern/ast.h>
69#include <kern/counters.h>
70#include <kern/cpu_number.h>
71#include <kern/misc_protos.h>
72#include <kern/queue.h>
1c79356b
A
73#include <kern/sched_prim.h>
74#include <kern/thread.h>
1c79356b
A
75#include <kern/processor.h>
76#include <kern/spl.h>
fe8ab488 77#include <kern/sfi.h>
39236c6e
A
78#if CONFIG_TELEMETRY
79#include <kern/telemetry.h>
80#endif
3e170ce0 81#include <kern/waitq.h>
316670eb 82#include <kern/ledger.h>
1c79356b 83#include <mach/policy.h>
0c530ab8 84#include <machine/trap.h> // for CHUD AST hook
6d2010ae 85#include <machine/pal_routines.h>
316670eb 86#include <security/mac_mach_internal.h> // for MACF AST hook
6d2010ae
A
87
88volatile perfASTCallback perfASTHook;
1c79356b 89
b0d623f7 90
1c79356b
A
91void
92ast_init(void)
93{
1c79356b
A
94}
95
316670eb
A
96extern void chudxnu_thread_ast(thread_t); // XXX this should probably be in a header...
97
55e303ae
A
98/*
99 * Called at splsched.
100 */
1c79356b
A
101void
102ast_taken(
91447636
A
103 ast_t reasons,
104 boolean_t enable
1c79356b
A
105)
106{
91447636
A
107 boolean_t preempt_trap = (reasons == AST_PREEMPTION);
108 ast_t *myast = ast_pending();
109 thread_t thread = current_thread();
6d2010ae 110 perfASTCallback perf_hook = perfASTHook;
91447636 111
91447636
A
112 /*
113 * CHUD hook - all threads including idle processor threads
114 */
b0d623f7
A
115 if (perf_hook) {
116 if (*myast & AST_CHUD_ALL) {
6d2010ae 117 (*perf_hook)(reasons, myast);
91447636 118
b0d623f7
A
119 if (*myast == AST_NONE)
120 return;
91447636 121 }
91447636 122 }
b0d623f7
A
123 else
124 *myast &= ~AST_CHUD_ALL;
1c79356b 125
91447636
A
126 reasons &= *myast;
127 *myast &= ~reasons;
1c79356b
A
128
129 /*
55e303ae
A
130 * Handle ASTs for all threads
131 * except idle processor threads.
1c79356b 132 */
91447636 133 if (!(thread->state & TH_IDLE)) {
55e303ae
A
134 /*
135 * Check for urgent preemption.
136 */
137 if ( (reasons & AST_URGENT) &&
3e170ce0 138 waitq_wait_possible(thread) ) {
55e303ae
A
139 if (reasons & AST_PREEMPT) {
140 counter(c_ast_taken_block++);
91447636 141 thread_block_reason(THREAD_CONTINUE_NULL, NULL,
fe8ab488 142 reasons & AST_PREEMPTION);
55e303ae
A
143 }
144
145 reasons &= ~AST_PREEMPTION;
1c79356b 146 }
0b4e3aa0 147
55e303ae
A
148 /*
149 * The kernel preempt traps
150 * skip all other ASTs.
151 */
152 if (!preempt_trap) {
153 ml_set_interrupts_enabled(enable);
0b4e3aa0 154
1c79356b 155#ifdef MACH_BSD
55e303ae
A
156 /*
157 * Handle BSD hook.
158 */
159 if (reasons & AST_BSD) {
91447636
A
160 thread_ast_clear(thread, AST_BSD);
161 bsd_ast(thread);
55e303ae 162 }
1c79356b 163#endif
316670eb
A
164#if CONFIG_MACF
165 /*
166 * Handle MACF hook.
167 */
168 if (reasons & AST_MACF) {
169 thread_ast_clear(thread, AST_MACF);
170 mac_thread_userret(thread);
171 }
172#endif
55e303ae
A
173 /*
174 * Thread APC hook.
175 */
3e170ce0
A
176 if (reasons & AST_APC) {
177 thread_ast_clear(thread, AST_APC);
178 special_handler(thread);
179 }
39236c6e
A
180
181 if (reasons & AST_GUARD) {
182 thread_ast_clear(thread, AST_GUARD);
183 guard_ast(thread);
184 }
185
316670eb
A
186 if (reasons & AST_LEDGER) {
187 thread_ast_clear(thread, AST_LEDGER);
188 ledger_ast(thread);
189 }
190
191 /*
192 * Kernel Profiling Hook
193 */
39236c6e 194 if (reasons & AST_KPERF) {
316670eb
A
195 thread_ast_clear(thread, AST_KPERF);
196 chudxnu_thread_ast(thread);
197 }
198
39236c6e
A
199#if CONFIG_TELEMETRY
200 if (reasons & AST_TELEMETRY_ALL) {
fe8ab488
A
201 boolean_t interrupted_userspace = FALSE;
202 boolean_t is_windowed = FALSE;
39236c6e
A
203
204 assert((reasons & AST_TELEMETRY_ALL) != AST_TELEMETRY_ALL); /* only one is valid at a time */
205 interrupted_userspace = (reasons & AST_TELEMETRY_USER) ? TRUE : FALSE;
fe8ab488 206 is_windowed = ((reasons & AST_TELEMETRY_WINDOWED) ? TRUE : FALSE);
39236c6e 207 thread_ast_clear(thread, AST_TELEMETRY_ALL);
fe8ab488 208 telemetry_ast(thread, interrupted_userspace, is_windowed);
39236c6e
A
209 }
210#endif
211
55e303ae
A
212 ml_set_interrupts_enabled(FALSE);
213
3e170ce0 214#if CONFIG_SCHED_SFI
fe8ab488
A
215 if (reasons & AST_SFI) {
216 sfi_ast(thread);
217 }
3e170ce0 218#endif
fe8ab488 219
3e170ce0 220 /*
fe8ab488 221 * Check for preemption. Conditions may have changed from when the AST_PREEMPT was originally set.
55e303ae 222 */
fe8ab488 223 thread_lock(thread);
c910b4d9 224 if (reasons & AST_PREEMPT)
fe8ab488
A
225 reasons = csw_check(current_processor(), reasons & AST_QUANTUM);
226 thread_unlock(thread);
55e303ae 227
3e170ce0
A
228 assert(waitq_wait_possible(thread));
229
230 if (reasons & AST_PREEMPT) {
55e303ae 231 counter(c_ast_taken_block++);
fe8ab488 232 thread_block_reason((thread_continue_t)thread_exception_return, NULL, reasons & AST_PREEMPTION);
55e303ae
A
233 }
234 }
1c79356b 235 }
0b4e3aa0 236
55e303ae 237 ml_set_interrupts_enabled(enable);
1c79356b
A
238}
239
9bccf70c
A
240/*
241 * Called at splsched.
242 */
1c79356b 243void
9bccf70c 244ast_check(
3e170ce0 245 processor_t processor)
1c79356b 246{
3e170ce0 247 thread_t thread = processor->active_thread;
cf7d32b8 248
3e170ce0
A
249 if (processor->state == PROCESSOR_RUNNING ||
250 processor->state == PROCESSOR_SHUTDOWN) {
251 ast_t preempt;
1c79356b 252
1c79356b 253 /*
0b4e3aa0 254 * Propagate thread ast to processor.
1c79356b 255 */
6d2010ae
A
256 pal_ast_check(thread);
257
91447636 258 ast_propagate(thread->ast);
1c79356b
A
259
260 /*
261 * Context switch check.
262 */
fe8ab488
A
263 thread_lock(thread);
264
265 processor->current_pri = thread->sched_pri;
266 processor->current_thmode = thread->sched_mode;
267 processor->current_sfi_class = thread->sfi_class = sfi_thread_classify(thread);
268
269 if ((preempt = csw_check(processor, AST_NONE)) != AST_NONE)
9bccf70c 270 ast_on(preempt);
3e170ce0 271
fe8ab488 272 thread_unlock(thread);
1c79356b 273 }
1c79356b 274}
3e170ce0
A
275
276/*
277 * Set AST flags on current processor
278 * Called at splsched
279 */
280void
281ast_on(ast_t reasons)
282{
283 ast_t *pending_ast = ast_pending();
284
285 *pending_ast |= reasons;
286}
287
288/*
289 * Clear AST flags on current processor
290 * Called at splsched
291 */
292void
293ast_off(ast_t reasons)
294{
295 ast_t *pending_ast = ast_pending();
296
297 *pending_ast &= ~reasons;
298}
299
300/*
301 * Re-set current processor's per-thread AST flags to those set on thread
302 * Called at splsched
303 */
304void
305ast_context(thread_t thread)
306{
307 ast_t *pending_ast = ast_pending();
308
309 *pending_ast = ((*pending_ast & ~AST_PER_THREAD) | thread->ast);
310}
311
312