]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ast.c
1f7d743f19f7e951afcac1585bdc89518f815e75
[apple/xnu.git] / osfmk / kern / ast.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 /*
59 */
60
61 /*
62 *
63 * This file contains routines to check whether an ast is needed.
64 *
65 * ast_check() - check whether ast is needed for interrupt or context
66 * switch. Usually called by clock interrupt handler.
67 *
68 */
69
70 #include <cputypes.h>
71 #include <platforms.h>
72
73 #include <kern/ast.h>
74 #include <kern/counters.h>
75 #include <kern/cpu_number.h>
76 #include <kern/misc_protos.h>
77 #include <kern/queue.h>
78 #include <kern/sched_prim.h>
79 #include <kern/thread.h>
80 #include <kern/processor.h>
81 #include <kern/spl.h>
82 #include <kern/wait_queue.h>
83 #include <mach/policy.h>
84
85 #ifdef __ppc__
86 #include <ppc/trap.h> // for CHUD AST hook
87 #endif
88
89 void
90 ast_init(void)
91 {
92 }
93
94 /*
95 * Called at splsched.
96 */
97 void
98 ast_taken(
99 ast_t reasons,
100 boolean_t enable
101 )
102 {
103 boolean_t preempt_trap = (reasons == AST_PREEMPTION);
104 ast_t *myast = ast_pending();
105 thread_t thread = current_thread();
106
107 #ifdef __ppc__
108 /*
109 * CHUD hook - all threads including idle processor threads
110 */
111 if(perfASTHook) {
112 if(*myast & AST_PPC_CHUD_ALL) {
113 perfASTHook(0, NULL, 0, 0);
114
115 if(*myast == AST_NONE) {
116 return; // nothing left to do
117 }
118 }
119 } else {
120 *myast &= ~AST_PPC_CHUD_ALL;
121 }
122 #endif
123
124 reasons &= *myast;
125 *myast &= ~reasons;
126
127 /*
128 * Handle ASTs for all threads
129 * except idle processor threads.
130 */
131 if (!(thread->state & TH_IDLE)) {
132 /*
133 * Check for urgent preemption.
134 */
135 if ( (reasons & AST_URGENT) &&
136 wait_queue_assert_possible(thread) ) {
137 if (reasons & AST_PREEMPT) {
138 counter(c_ast_taken_block++);
139 thread_block_reason(THREAD_CONTINUE_NULL, NULL,
140 AST_PREEMPT | AST_URGENT);
141 }
142
143 reasons &= ~AST_PREEMPTION;
144 }
145
146 /*
147 * The kernel preempt traps
148 * skip all other ASTs.
149 */
150 if (!preempt_trap) {
151 ml_set_interrupts_enabled(enable);
152
153 #ifdef MACH_BSD
154 /*
155 * Handle BSD hook.
156 */
157 if (reasons & AST_BSD) {
158 thread_ast_clear(thread, AST_BSD);
159 bsd_ast(thread);
160 }
161 #endif
162
163 /*
164 * Thread APC hook.
165 */
166 if (reasons & AST_APC)
167 act_execute_returnhandlers();
168
169 ml_set_interrupts_enabled(FALSE);
170
171 /*
172 * Check for preemption.
173 */
174 if (reasons & AST_PREEMPT) {
175 processor_t myprocessor = current_processor();
176
177 if (csw_needed(thread, myprocessor))
178 reasons = AST_PREEMPT;
179 else
180 reasons = AST_NONE;
181 }
182 if ( (reasons & AST_PREEMPT) &&
183 wait_queue_assert_possible(thread) ) {
184 counter(c_ast_taken_block++);
185 thread_block_reason((thread_continue_t)thread_exception_return, NULL, AST_PREEMPT);
186 }
187 }
188 }
189
190 ml_set_interrupts_enabled(enable);
191 }
192
193 /*
194 * Called at splsched.
195 */
196 void
197 ast_check(
198 processor_t processor)
199 {
200 register thread_t thread = processor->active_thread;
201
202 processor->current_pri = thread->sched_pri;
203 if ( processor->state == PROCESSOR_RUNNING ||
204 processor->state == PROCESSOR_SHUTDOWN ) {
205 register ast_t preempt;
206
207 /*
208 * Propagate thread ast to processor.
209 */
210 ast_propagate(thread->ast);
211
212 /*
213 * Context switch check.
214 */
215 if ((preempt = csw_check(thread, processor)) != AST_NONE)
216 ast_on(preempt);
217 }
218 }