]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/ast.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / osfmk / kern / ast.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
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.
1c79356b 11 *
e5568f75
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
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.
1c79356b
A
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 <cpus.h>
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>
73#include <kern/thread_act.h>
74#include <kern/thread_swap.h>
75#include <kern/processor.h>
76#include <kern/spl.h>
77#include <mach/policy.h>
1c79356b
A
78
79volatile ast_t need_ast[NCPUS];
80
81void
82ast_init(void)
83{
84#ifndef MACHINE_AST
85 register int i;
86
87 for (i=0; i<NCPUS; i++) {
88 need_ast[i] = AST_NONE;
89 }
90#endif /* MACHINE_AST */
91}
92
55e303ae
A
93/*
94 * Called at splsched.
95 */
1c79356b
A
96void
97ast_taken(
0b4e3aa0
A
98 ast_t reasons,
99 boolean_t enable
1c79356b
A
100)
101{
0b4e3aa0 102 register thread_t self = current_thread();
55e303ae
A
103 register int mycpu = cpu_number();
104 boolean_t preempt_trap = (reasons == AST_PREEMPTION);
1c79356b 105
0b4e3aa0 106 reasons &= need_ast[mycpu];
1c79356b 107 need_ast[mycpu] &= ~reasons;
1c79356b
A
108
109 /*
55e303ae
A
110 * Handle ASTs for all threads
111 * except idle processor threads.
1c79356b 112 */
55e303ae
A
113 if (!(self->state & TH_IDLE)) {
114 /*
115 * Check for urgent preemption.
116 */
117 if ( (reasons & AST_URGENT) &&
118 wait_queue_assert_possible(self) ) {
119 if (reasons & AST_PREEMPT) {
120 counter(c_ast_taken_block++);
121 thread_block_reason(THREAD_CONTINUE_NULL,
122 AST_PREEMPT | AST_URGENT);
123 }
124
125 reasons &= ~AST_PREEMPTION;
1c79356b 126 }
0b4e3aa0 127
55e303ae
A
128 /*
129 * The kernel preempt traps
130 * skip all other ASTs.
131 */
132 if (!preempt_trap) {
133 ml_set_interrupts_enabled(enable);
0b4e3aa0 134
1c79356b 135#ifdef MACH_BSD
55e303ae
A
136 /*
137 * Handle BSD hook.
138 */
139 if (reasons & AST_BSD) {
140 extern void bsd_ast(thread_act_t act);
141 thread_act_t act = self->top_act;
142
143 thread_ast_clear(act, AST_BSD);
144 bsd_ast(act);
145 }
1c79356b
A
146#endif
147
55e303ae
A
148 /*
149 * Thread APC hook.
150 */
151 if (reasons & AST_APC)
152 act_execute_returnhandlers();
153
154 ml_set_interrupts_enabled(FALSE);
155
156 /*
157 * Check for preemption.
158 */
159 if (reasons & AST_PREEMPT) {
160 processor_t myprocessor = current_processor();
161
162 if (csw_needed(self, myprocessor))
163 reasons = AST_PREEMPT;
164 else
165 reasons = AST_NONE;
166 }
167 if ( (reasons & AST_PREEMPT) &&
168 wait_queue_assert_possible(self) ) {
169 counter(c_ast_taken_block++);
170 thread_block_reason(thread_exception_return, AST_PREEMPT);
171 }
172 }
1c79356b 173 }
0b4e3aa0 174
55e303ae 175 ml_set_interrupts_enabled(enable);
1c79356b
A
176}
177
9bccf70c
A
178/*
179 * Called at splsched.
180 */
1c79356b 181void
9bccf70c
A
182ast_check(
183 processor_t processor)
1c79356b 184{
55e303ae 185 register thread_t self = processor->active_thread;
1c79356b 186
9bccf70c
A
187 processor->current_pri = self->sched_pri;
188 if (processor->state == PROCESSOR_RUNNING) {
189 register ast_t preempt;
190processor_running:
1c79356b 191
1c79356b 192 /*
0b4e3aa0 193 * Propagate thread ast to processor.
1c79356b 194 */
0b4e3aa0 195 ast_propagate(self->top_act->ast);
1c79356b
A
196
197 /*
198 * Context switch check.
199 */
9bccf70c
A
200 if ((preempt = csw_check(self, processor)) != AST_NONE)
201 ast_on(preempt);
1c79356b 202 }
9bccf70c
A
203 else
204 if ( processor->state == PROCESSOR_DISPATCHING ||
205 processor->state == PROCESSOR_IDLE ) {
206 return;
207 }
208 else
209 if (processor->state == PROCESSOR_SHUTDOWN)
210 goto processor_running;
1c79356b 211}