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