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