]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/ast.c
xnu-792.25.20.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 3 *
6601e61a 4 * @APPLE_LICENSE_HEADER_START@
1c79356b 5 *
6601e61a
A
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.
8f6c56a5 11 *
6601e61a
A
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
8f6c56a5
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6601e61a
A
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.
8f6c56a5 19 *
6601e61a 20 * @APPLE_LICENSE_HEADER_END@
1c79356b
A
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>
1c79356b 63#include <platforms.h>
1c79356b
A
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>
1c79356b
A
70#include <kern/sched_prim.h>
71#include <kern/thread.h>
1c79356b
A
72#include <kern/processor.h>
73#include <kern/spl.h>
91447636 74#include <kern/wait_queue.h>
1c79356b 75#include <mach/policy.h>
0c530ab8 76#include <machine/trap.h> // for CHUD AST hook
1c79356b
A
77
78void
79ast_init(void)
80{
1c79356b
A
81}
82
55e303ae
A
83/*
84 * Called at splsched.
85 */
1c79356b
A
86void
87ast_taken(
91447636
A
88 ast_t reasons,
89 boolean_t enable
1c79356b
A
90)
91{
91447636
A
92 boolean_t preempt_trap = (reasons == AST_PREEMPTION);
93 ast_t *myast = ast_pending();
94 thread_t thread = current_thread();
95
91447636
A
96 /*
97 * CHUD hook - all threads including idle processor threads
98 */
99 if(perfASTHook) {
0c530ab8 100 if(*myast & AST_CHUD_ALL) {
91447636
A
101 perfASTHook(0, NULL, 0, 0);
102
103 if(*myast == AST_NONE) {
104 return; // nothing left to do
105 }
106 }
107 } else {
0c530ab8 108 *myast &= ~AST_CHUD_ALL;
91447636 109 }
1c79356b 110
91447636
A
111 reasons &= *myast;
112 *myast &= ~reasons;
1c79356b
A
113
114 /*
55e303ae
A
115 * Handle ASTs for all threads
116 * except idle processor threads.
1c79356b 117 */
91447636 118 if (!(thread->state & TH_IDLE)) {
55e303ae
A
119 /*
120 * Check for urgent preemption.
121 */
122 if ( (reasons & AST_URGENT) &&
91447636 123 wait_queue_assert_possible(thread) ) {
55e303ae
A
124 if (reasons & AST_PREEMPT) {
125 counter(c_ast_taken_block++);
91447636 126 thread_block_reason(THREAD_CONTINUE_NULL, NULL,
55e303ae
A
127 AST_PREEMPT | AST_URGENT);
128 }
129
130 reasons &= ~AST_PREEMPTION;
1c79356b 131 }
0b4e3aa0 132
55e303ae
A
133 /*
134 * The kernel preempt traps
135 * skip all other ASTs.
136 */
137 if (!preempt_trap) {
138 ml_set_interrupts_enabled(enable);
0b4e3aa0 139
1c79356b 140#ifdef MACH_BSD
55e303ae
A
141 /*
142 * Handle BSD hook.
143 */
144 if (reasons & AST_BSD) {
91447636
A
145 thread_ast_clear(thread, AST_BSD);
146 bsd_ast(thread);
55e303ae 147 }
1c79356b
A
148#endif
149
55e303ae
A
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
91447636 164 if (csw_needed(thread, myprocessor))
55e303ae
A
165 reasons = AST_PREEMPT;
166 else
167 reasons = AST_NONE;
168 }
169 if ( (reasons & AST_PREEMPT) &&
91447636 170 wait_queue_assert_possible(thread) ) {
55e303ae 171 counter(c_ast_taken_block++);
91447636 172 thread_block_reason((thread_continue_t)thread_exception_return, NULL, AST_PREEMPT);
55e303ae
A
173 }
174 }
1c79356b 175 }
0b4e3aa0 176
55e303ae 177 ml_set_interrupts_enabled(enable);
1c79356b
A
178}
179
9bccf70c
A
180/*
181 * Called at splsched.
182 */
1c79356b 183void
9bccf70c
A
184ast_check(
185 processor_t processor)
1c79356b 186{
91447636 187 register thread_t thread = processor->active_thread;
1c79356b 188
91447636
A
189 processor->current_pri = thread->sched_pri;
190 if ( processor->state == PROCESSOR_RUNNING ||
191 processor->state == PROCESSOR_SHUTDOWN ) {
9bccf70c 192 register ast_t preempt;
1c79356b 193
1c79356b 194 /*
0b4e3aa0 195 * Propagate thread ast to processor.
1c79356b 196 */
91447636 197 ast_propagate(thread->ast);
1c79356b
A
198
199 /*
200 * Context switch check.
201 */
91447636 202 if ((preempt = csw_check(thread, processor)) != AST_NONE)
9bccf70c 203 ast_on(preempt);
1c79356b 204 }
1c79356b 205}