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