]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ast.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
55 * This file contains routines to check whether an ast is needed.
57 * ast_check() - check whether ast is needed for interrupt or context
58 * switch. Usually called by clock interrupt handler.
64 #include <platforms.h>
65 #include <task_swapper.h>
68 #include <kern/counters.h>
69 #include <kern/cpu_number.h>
70 #include <kern/misc_protos.h>
71 #include <kern/queue.h>
72 #include <kern/sched.h>
73 #include <kern/sched_prim.h>
74 #include <kern/thread.h>
75 #include <kern/thread_act.h>
76 #include <kern/thread_swap.h>
77 #include <kern/processor.h>
79 #include <mach/policy.h>
81 #include <kern/task_swap.h>
82 #endif /* TASK_SWAPPER */
84 volatile ast_t need_ast
[NCPUS
];
92 for (i
=0; i
<NCPUS
; i
++) {
93 need_ast
[i
] = AST_NONE
;
95 #endif /* MACHINE_AST */
100 boolean_t preemption
,
105 register thread_t self
= current_thread();
106 register processor_t mypr
;
107 register ast_t reasons
;
109 thread_act_t act
= self
->top_act
;
111 extern void bsd_ast(thread_act_t
);
112 extern void bsdinit_task(void);
115 mp_disable_preemption();
116 mycpu
= cpu_number();
117 reasons
= need_ast
[mycpu
] & mask
;
118 need_ast
[mycpu
] &= ~reasons
;
119 mp_enable_preemption();
121 ml_set_interrupts_enabled(interrupt
);
124 * No ast for an idle thread
126 if (self
->state
& TH_IDLE
)
130 * Check for preemption
132 if ((reasons
& AST_URGENT
) && (wait_queue_assert_possible(self
))) {
133 reasons
&= ~AST_URGENT
;
134 if ((reasons
& (AST_BLOCK
|AST_QUANTUM
)) == 0) {
135 mp_disable_preemption();
136 mypr
= current_processor();
137 if (csw_needed(self
, mypr
)) {
138 reasons
|= (mypr
->first_quantum
? AST_BLOCK
: AST_QUANTUM
);
140 mp_enable_preemption();
142 if (reasons
& (AST_BLOCK
| AST_QUANTUM
)) {
143 counter(c_ast_taken_block
++);
144 thread_block_reason((void (*)(void))0,
145 (reasons
& (AST_BLOCK
| AST_QUANTUM
)));
153 * Check for BSD hardcoded hooks
155 if (reasons
& AST_BSD
) {
156 thread_ast_clear(act
,AST_BSD
);
159 if (reasons
& AST_BSD_INIT
) {
160 thread_ast_clear(act
,AST_BSD_INIT
);
166 /* must be before AST_APC */
167 if (reasons
& AST_SWAPOUT
) {
171 mp_disable_preemption();
172 mycpu
= cpu_number();
173 if (need_ast
[mycpu
] & AST_APC
) {
174 /* generated in swapout_ast() to get suspended */
175 reasons
|= AST_APC
; /* process now ... */
176 need_ast
[mycpu
] &= ~AST_APC
; /* ... and not later */
178 mp_enable_preemption();
181 #endif /* TASK_SWAPPER */
186 if (reasons
& AST_APC
) {
187 act_execute_returnhandlers();
191 * thread_block needs to know if the thread's quantum
192 * expired so the thread can be put on the tail of
193 * run queue. One of the previous actions might well
194 * have woken a high-priority thread, so we also use
197 reasons
&= (AST_BLOCK
| AST_QUANTUM
);
199 mp_disable_preemption();
200 mypr
= current_processor();
201 if (csw_needed(self
, mypr
)) {
202 reasons
= (mypr
->first_quantum
? AST_BLOCK
: AST_QUANTUM
);
204 mp_enable_preemption();
206 if ((reasons
& (AST_BLOCK
| AST_QUANTUM
)) &&
207 (wait_queue_assert_possible(self
))) {
208 counter(c_ast_taken_block
++);
210 * JMM - SMP machines don't like blocking at a continuation
211 * here - why not? Could be a combination of set_state and
212 * suspension on the thread_create_running API?
214 * thread_block_reason(thread_exception_return, reasons);
216 thread_block_reason((void (*)(void))0, reasons
);
224 register processor_t myprocessor
;
225 register thread_t thread
= current_thread();
226 spl_t s
= splsched();
228 mp_disable_preemption();
229 mycpu
= cpu_number();
232 * Check processor state for ast conditions.
234 myprocessor
= cpu_to_processor(mycpu
);
235 switch(myprocessor
->state
) {
236 case PROCESSOR_OFF_LINE
:
238 case PROCESSOR_DISPATCHING
:
245 case PROCESSOR_ASSIGN
:
247 * Need ast to force action thread onto processor.
249 * XXX Should check if action thread is already there.
253 #endif /* NCPUS > 1 */
255 case PROCESSOR_RUNNING
:
256 case PROCESSOR_SHUTDOWN
:
258 * Propagate thread ast to processor. If we already
259 * need an ast, don't look for more reasons.
261 ast_propagate(current_act()->ast
);
262 if (ast_needed(mycpu
))
266 * Context switch check.
268 if (csw_needed(thread
, myprocessor
)) {
269 ast_on((myprocessor
->first_quantum
?
270 AST_BLOCK
: AST_QUANTUM
));
275 panic("ast_check: Bad processor state");
277 mp_enable_preemption();
282 * JMM - Temporary exports to other components
292 intr
= ml_set_interrupts_enabled(FALSE
);
294 (void *)ml_set_interrupts_enabled(intr
);
298 ast_off(ast_t reason
)
300 ast_off_fast(reason
);