]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ast.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / osfmk / kern / ast.c
1 /*
2 * Copyright (c) 2000-2009 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 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>
69 #include <platforms.h>
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>
76 #include <kern/sched_prim.h>
77 #include <kern/thread.h>
78 #include <kern/processor.h>
79 #include <kern/spl.h>
80 #include <kern/wait_queue.h>
81 #include <mach/policy.h>
82 #include <machine/trap.h> // for CHUD AST hook
83
84
85 void
86 ast_init(void)
87 {
88 }
89
90 /*
91 * Called at splsched.
92 */
93 void
94 ast_taken(
95 ast_t reasons,
96 boolean_t enable
97 )
98 {
99 boolean_t preempt_trap = (reasons == AST_PREEMPTION);
100 ast_t *myast = ast_pending();
101 thread_t thread = current_thread();
102 perfCallback perf_hook = perfASTHook;
103
104 /*
105 * CHUD hook - all threads including idle processor threads
106 */
107 if (perf_hook) {
108 if (*myast & AST_CHUD_ALL) {
109 (*perf_hook)(0, NULL, 0, 0);
110
111 if (*myast == AST_NONE)
112 return;
113 }
114 }
115 else
116 *myast &= ~AST_CHUD_ALL;
117
118 reasons &= *myast;
119 *myast &= ~reasons;
120
121 /*
122 * Handle ASTs for all threads
123 * except idle processor threads.
124 */
125 if (!(thread->state & TH_IDLE)) {
126 /*
127 * Check for urgent preemption.
128 */
129 if ( (reasons & AST_URGENT) &&
130 wait_queue_assert_possible(thread) ) {
131 if (reasons & AST_PREEMPT) {
132 counter(c_ast_taken_block++);
133 thread_block_reason(THREAD_CONTINUE_NULL, NULL,
134 AST_PREEMPT | AST_URGENT);
135 }
136
137 reasons &= ~AST_PREEMPTION;
138 }
139
140 /*
141 * The kernel preempt traps
142 * skip all other ASTs.
143 */
144 if (!preempt_trap) {
145 ml_set_interrupts_enabled(enable);
146
147 #ifdef MACH_BSD
148 /*
149 * Handle BSD hook.
150 */
151 if (reasons & AST_BSD) {
152 thread_ast_clear(thread, AST_BSD);
153 bsd_ast(thread);
154 }
155 #endif
156
157 /*
158 * Thread APC hook.
159 */
160 if (reasons & AST_APC)
161 act_execute_returnhandlers();
162
163 ml_set_interrupts_enabled(FALSE);
164
165 /*
166 * Check for preemption.
167 */
168 if (reasons & AST_PREEMPT)
169 reasons = csw_check(current_processor());
170
171 if ( (reasons & AST_PREEMPT) &&
172 wait_queue_assert_possible(thread) ) {
173 counter(c_ast_taken_block++);
174 thread_block_reason((thread_continue_t)thread_exception_return, NULL, AST_PREEMPT);
175 }
176 }
177 }
178
179 ml_set_interrupts_enabled(enable);
180 }
181
182 /*
183 * Called at splsched.
184 */
185 void
186 ast_check(
187 processor_t processor)
188 {
189 thread_t thread = processor->active_thread;
190
191 processor->current_pri = thread->sched_pri;
192 if ( processor->state == PROCESSOR_RUNNING ||
193 processor->state == PROCESSOR_SHUTDOWN ) {
194 ast_t preempt;
195
196 /*
197 * Propagate thread ast to processor.
198 */
199 ast_propagate(thread->ast);
200
201 /*
202 * Context switch check.
203 */
204 if ((preempt = csw_check(processor)) != AST_NONE)
205 ast_on(preempt);
206 }
207 }