]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/ast.c
xnu-2422.115.4.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
68#include <cputypes.h>
1c79356b 69#include <platforms.h>
1c79356b
A
70
71#include <kern/ast.h>
72#include <kern/counters.h>
73#include <kern/cpu_number.h>
74#include <kern/misc_protos.h>
75#include <kern/queue.h>
1c79356b
A
76#include <kern/sched_prim.h>
77#include <kern/thread.h>
1c79356b
A
78#include <kern/processor.h>
79#include <kern/spl.h>
39236c6e
A
80#if CONFIG_TELEMETRY
81#include <kern/telemetry.h>
82#endif
91447636 83#include <kern/wait_queue.h>
316670eb 84#include <kern/ledger.h>
1c79356b 85#include <mach/policy.h>
0c530ab8 86#include <machine/trap.h> // for CHUD AST hook
6d2010ae 87#include <machine/pal_routines.h>
316670eb 88#include <security/mac_mach_internal.h> // for MACF AST hook
6d2010ae
A
89
90volatile perfASTCallback perfASTHook;
1c79356b 91
b0d623f7 92
1c79356b
A
93void
94ast_init(void)
95{
1c79356b
A
96}
97
316670eb
A
98extern void chudxnu_thread_ast(thread_t); // XXX this should probably be in a header...
99
55e303ae
A
100/*
101 * Called at splsched.
102 */
1c79356b
A
103void
104ast_taken(
91447636
A
105 ast_t reasons,
106 boolean_t enable
1c79356b
A
107)
108{
91447636
A
109 boolean_t preempt_trap = (reasons == AST_PREEMPTION);
110 ast_t *myast = ast_pending();
111 thread_t thread = current_thread();
6d2010ae 112 perfASTCallback perf_hook = perfASTHook;
91447636 113
91447636
A
114 /*
115 * CHUD hook - all threads including idle processor threads
116 */
b0d623f7
A
117 if (perf_hook) {
118 if (*myast & AST_CHUD_ALL) {
6d2010ae 119 (*perf_hook)(reasons, myast);
91447636 120
b0d623f7
A
121 if (*myast == AST_NONE)
122 return;
91447636 123 }
91447636 124 }
b0d623f7
A
125 else
126 *myast &= ~AST_CHUD_ALL;
1c79356b 127
91447636
A
128 reasons &= *myast;
129 *myast &= ~reasons;
1c79356b
A
130
131 /*
55e303ae
A
132 * Handle ASTs for all threads
133 * except idle processor threads.
1c79356b 134 */
91447636 135 if (!(thread->state & TH_IDLE)) {
55e303ae
A
136 /*
137 * Check for urgent preemption.
138 */
139 if ( (reasons & AST_URGENT) &&
91447636 140 wait_queue_assert_possible(thread) ) {
55e303ae
A
141 if (reasons & AST_PREEMPT) {
142 counter(c_ast_taken_block++);
91447636 143 thread_block_reason(THREAD_CONTINUE_NULL, NULL,
55e303ae
A
144 AST_PREEMPT | AST_URGENT);
145 }
146
147 reasons &= ~AST_PREEMPTION;
1c79356b 148 }
0b4e3aa0 149
55e303ae
A
150 /*
151 * The kernel preempt traps
152 * skip all other ASTs.
153 */
154 if (!preempt_trap) {
155 ml_set_interrupts_enabled(enable);
0b4e3aa0 156
1c79356b 157#ifdef MACH_BSD
55e303ae
A
158 /*
159 * Handle BSD hook.
160 */
161 if (reasons & AST_BSD) {
91447636
A
162 thread_ast_clear(thread, AST_BSD);
163 bsd_ast(thread);
55e303ae 164 }
1c79356b 165#endif
316670eb
A
166#if CONFIG_MACF
167 /*
168 * Handle MACF hook.
169 */
170 if (reasons & AST_MACF) {
171 thread_ast_clear(thread, AST_MACF);
172 mac_thread_userret(thread);
173 }
174#endif
55e303ae
A
175 /*
176 * Thread APC hook.
177 */
178 if (reasons & AST_APC)
179 act_execute_returnhandlers();
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) {
201 boolean_t interrupted_userspace;
202
203 assert((reasons & AST_TELEMETRY_ALL) != AST_TELEMETRY_ALL); /* only one is valid at a time */
204 interrupted_userspace = (reasons & AST_TELEMETRY_USER) ? TRUE : FALSE;
205 thread_ast_clear(thread, AST_TELEMETRY_ALL);
206 telemetry_ast(thread, interrupted_userspace);
207 }
208#endif
209
55e303ae
A
210 ml_set_interrupts_enabled(FALSE);
211
212 /*
213 * Check for preemption.
214 */
c910b4d9
A
215 if (reasons & AST_PREEMPT)
216 reasons = csw_check(current_processor());
55e303ae 217
55e303ae 218 if ( (reasons & AST_PREEMPT) &&
91447636 219 wait_queue_assert_possible(thread) ) {
55e303ae 220 counter(c_ast_taken_block++);
91447636 221 thread_block_reason((thread_continue_t)thread_exception_return, NULL, AST_PREEMPT);
55e303ae
A
222 }
223 }
1c79356b 224 }
0b4e3aa0 225
55e303ae 226 ml_set_interrupts_enabled(enable);
1c79356b
A
227}
228
9bccf70c
A
229/*
230 * Called at splsched.
231 */
1c79356b 232void
9bccf70c
A
233ast_check(
234 processor_t processor)
1c79356b 235{
cf7d32b8
A
236 thread_t thread = processor->active_thread;
237
238 processor->current_pri = thread->sched_pri;
6d2010ae 239 processor->current_thmode = thread->sched_mode;
91447636
A
240 if ( processor->state == PROCESSOR_RUNNING ||
241 processor->state == PROCESSOR_SHUTDOWN ) {
cf7d32b8 242 ast_t preempt;
1c79356b 243
1c79356b 244 /*
0b4e3aa0 245 * Propagate thread ast to processor.
1c79356b 246 */
6d2010ae
A
247 pal_ast_check(thread);
248
91447636 249 ast_propagate(thread->ast);
1c79356b
A
250
251 /*
252 * Context switch check.
253 */
c910b4d9 254 if ((preempt = csw_check(processor)) != AST_NONE)
9bccf70c 255 ast_on(preempt);
1c79356b 256 }
1c79356b 257}