2 * Copyright (c) 2003-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <mach/boolean.h>
31 #include <mach/mach_types.h>
33 #include <kern/kern_types.h>
34 #include <kern/processor.h>
35 #include <kern/timer_call.h>
36 #include <kern/thread_call.h>
37 #include <kern/kalloc.h>
38 #include <kern/thread.h>
40 #include <libkern/OSAtomic.h>
42 #include <machine/machine_routines.h>
43 #include <machine/cpu_data.h>
44 #include <machine/trap.h>
46 #include <chud/chud_xnu.h>
47 #include <chud/chud_xnu_private.h>
49 #include <i386/misc_protos.h>
50 #include <i386/lapic.h>
52 #include <i386/machine_cpu.h>
54 #include <sys/kdebug.h>
55 #define CHUD_TIMER_CALLBACK_CANCEL 0
56 #define CHUD_TIMER_CALLBACK_ENTER 1
57 #define CHUD_TIMER_CALLBACK 2
58 #define CHUD_AST_SEND 3
59 #define CHUD_AST_CALLBACK 4
60 #define CHUD_CPUSIG_SEND 5
61 #define CHUD_CPUSIG_CALLBACK 6
64 void chudxnu_cancel_all_callbacks(void)
66 chudxnu_cpusig_callback_cancel();
67 chudxnu_cpu_timer_callback_cancel_all();
68 chudxnu_interrupt_callback_cancel();
69 chudxnu_perfmon_ast_callback_cancel();
72 static lck_grp_t chud_request_lck_grp
;
73 static lck_grp_attr_t chud_request_lck_grp_attr
;
74 static lck_attr_t chud_request_lck_attr
;
77 static chudcpu_data_t chudcpu_boot_cpu
;
79 chudxnu_cpu_alloc(boolean_t boot_processor
)
81 chudcpu_data_t
*chud_proc_info
;
84 chud_proc_info
= &chudcpu_boot_cpu
;
86 lck_attr_setdefault(&chud_request_lck_attr
);
87 lck_grp_attr_setdefault(&chud_request_lck_grp_attr
);
88 lck_grp_init(&chud_request_lck_grp
, "chud_request", &chud_request_lck_grp_attr
);
91 chud_proc_info
= (chudcpu_data_t
*)
92 kalloc(sizeof(chudcpu_data_t
));
93 if (chud_proc_info
== (chudcpu_data_t
*)NULL
) {
97 bzero((char *)chud_proc_info
, sizeof(chudcpu_data_t
));
98 chud_proc_info
->t_deadline
= 0xFFFFFFFFFFFFFFFFULL
;
100 mpqueue_init(&chud_proc_info
->cpu_request_queue
, &chud_request_lck_grp
, &chud_request_lck_attr
);
102 /* timer_call_cancel() can be called before first usage, so init here: <rdar://problem/9320202> */
103 timer_call_setup(&(chud_proc_info
->cpu_timer_call
), NULL
, NULL
);
106 return (void *)chud_proc_info
;
110 chudxnu_cpu_free(void *cp
)
112 if (cp
== NULL
|| cp
== (void *)&chudcpu_boot_cpu
) {
115 kfree(cp
,sizeof(chudcpu_data_t
));
120 chudxnu_private_cpu_timer_callback(
121 timer_call_param_t param0
,
122 timer_call_param_t param1
)
124 #pragma unused (param0)
125 #pragma unused (param1)
126 chudcpu_data_t
*chud_proc_info
;
128 x86_thread_state_t state
;
129 mach_msg_type_number_t count
;
130 chudxnu_cpu_timer_callback_func_t fn
;
132 oldlevel
= ml_set_interrupts_enabled(FALSE
);
133 chud_proc_info
= (chudcpu_data_t
*)(current_cpu_datap()->cpu_chud
);
135 count
= x86_THREAD_STATE_COUNT
;
136 if (chudxnu_thread_get_state(current_thread(),
138 (thread_state_t
)&state
,
140 FALSE
) == KERN_SUCCESS
) {
141 fn
= chud_proc_info
->cpu_timer_callback_fn
;
145 (thread_state_t
)&state
,
150 ml_set_interrupts_enabled(oldlevel
);
153 __private_extern__ kern_return_t
154 chudxnu_cpu_timer_callback_enter(
155 chudxnu_cpu_timer_callback_func_t func
,
159 chudcpu_data_t
*chud_proc_info
;
162 oldlevel
= ml_set_interrupts_enabled(FALSE
);
163 chud_proc_info
= (chudcpu_data_t
*)(current_cpu_datap()->cpu_chud
);
165 // cancel any existing callback for this cpu
166 timer_call_cancel(&(chud_proc_info
->cpu_timer_call
));
168 chud_proc_info
->cpu_timer_callback_fn
= func
;
170 clock_interval_to_deadline(time
, units
, &(chud_proc_info
->t_deadline
));
171 timer_call_setup(&(chud_proc_info
->cpu_timer_call
),
172 chudxnu_private_cpu_timer_callback
, NULL
);
173 timer_call_enter(&(chud_proc_info
->cpu_timer_call
),
174 chud_proc_info
->t_deadline
,
175 TIMER_CALL_SYS_CRITICAL
|TIMER_CALL_LOCAL
);
177 ml_set_interrupts_enabled(oldlevel
);
181 __private_extern__ kern_return_t
182 chudxnu_cpu_timer_callback_cancel(void)
184 chudcpu_data_t
*chud_proc_info
;
187 oldlevel
= ml_set_interrupts_enabled(FALSE
);
188 chud_proc_info
= (chudcpu_data_t
*)(current_cpu_datap()->cpu_chud
);
190 timer_call_cancel(&(chud_proc_info
->cpu_timer_call
));
193 chud_proc_info
->t_deadline
|= ~(chud_proc_info
->t_deadline
);
194 chud_proc_info
->cpu_timer_callback_fn
= NULL
;
196 ml_set_interrupts_enabled(oldlevel
);
200 __private_extern__ kern_return_t
201 chudxnu_cpu_timer_callback_cancel_all(void)
204 chudcpu_data_t
*chud_proc_info
;
206 for(cpu
=0; cpu
< real_ncpus
; cpu
++) {
207 chud_proc_info
= (chudcpu_data_t
*) cpu_data_ptr
[cpu
]->cpu_chud
;
208 if (chud_proc_info
== NULL
)
210 timer_call_cancel(&(chud_proc_info
->cpu_timer_call
));
211 chud_proc_info
->t_deadline
|= ~(chud_proc_info
->t_deadline
);
212 chud_proc_info
->cpu_timer_callback_fn
= NULL
;
218 #pragma mark **** ast ****
220 static kern_return_t
chud_null_ast(thread_flavor_t flavor
, thread_state_t tstate
,
221 mach_msg_type_number_t count
);
222 static chudxnu_perfmon_ast_callback_func_t perfmon_ast_callback_fn
= chud_null_ast
;
224 static kern_return_t
chud_null_ast(thread_flavor_t flavor __unused
,
225 thread_state_t tstate __unused
, mach_msg_type_number_t count __unused
) {
230 chudxnu_private_chud_ast_callback(ast_t reasons
, ast_t
*myast
)
232 boolean_t oldlevel
= ml_set_interrupts_enabled(FALSE
);
233 kern_return_t retval
= KERN_FAILURE
;
234 chudxnu_perfmon_ast_callback_func_t fn
= perfmon_ast_callback_fn
;
237 if ((*myast
& AST_CHUD_URGENT
) && (reasons
& (AST_URGENT
| AST_CHUD_URGENT
))) { // Only execute urgent callbacks if reasons specifies an urgent context.
238 *myast
&= ~AST_CHUD_URGENT
;
240 if (AST_URGENT
== *myast
) { // If the only flag left is AST_URGENT, we can clear it; we know that we set it, but if there are also other bits set in reasons then someone else might still need AST_URGENT, so we'll leave it set. The normal machinery in ast_taken will ensure it gets cleared eventually, as necessary.
244 retval
= KERN_SUCCESS
;
247 if ((*myast
& AST_CHUD
) && (reasons
& AST_CHUD
)) { // Only execute non-urgent callbacks if reasons actually specifies AST_CHUD. This implies non-urgent callbacks since the only time this'll happen is if someone either calls ast_taken with AST_CHUD explicitly (not done at time of writing, but possible) or with AST_ALL, which of course includes AST_CHUD.
249 retval
= KERN_SUCCESS
;
252 if (KERN_SUCCESS
== retval
) {
253 x86_thread_state_t state
;
254 mach_msg_type_number_t count
= x86_THREAD_STATE_COUNT
;
255 thread_t thread
= current_thread();
257 if (KERN_SUCCESS
== chudxnu_thread_get_state(thread
,
259 (thread_state_t
)&state
,
261 (thread
->task
!= kernel_task
))) {
262 (fn
)(x86_THREAD_STATE
, (thread_state_t
)&state
, count
);
267 ml_set_interrupts_enabled(oldlevel
);
271 volatile perfASTCallback perfASTHook
;
273 __private_extern__ kern_return_t
274 chudxnu_perfmon_ast_callback_enter(chudxnu_perfmon_ast_callback_func_t func
)
276 if(OSCompareAndSwapPtr(NULL
, chudxnu_private_chud_ast_callback
,
277 (void * volatile *)&perfASTHook
)) {
278 chudxnu_perfmon_ast_callback_func_t old
= perfmon_ast_callback_fn
;
280 while(!OSCompareAndSwapPtr(old
, func
,
281 (void * volatile *)&perfmon_ast_callback_fn
)) {
282 old
= perfmon_ast_callback_fn
;
290 __private_extern__ kern_return_t
291 chudxnu_perfmon_ast_callback_cancel(void)
293 if(OSCompareAndSwapPtr(chudxnu_private_chud_ast_callback
, NULL
,
294 (void * volatile *)&perfASTHook
)) {
295 chudxnu_perfmon_ast_callback_func_t old
= perfmon_ast_callback_fn
;
297 while(!OSCompareAndSwapPtr(old
, chud_null_ast
,
298 (void * volatile *)&perfmon_ast_callback_fn
)) {
299 old
= perfmon_ast_callback_fn
;
307 __private_extern__ kern_return_t
308 chudxnu_perfmon_ast_send_urgent(boolean_t urgent
)
310 boolean_t oldlevel
= ml_set_interrupts_enabled(FALSE
);
311 ast_t
*myast
= ast_pending();
314 *myast
|= (AST_CHUD_URGENT
| AST_URGENT
);
316 *myast
|= (AST_CHUD
);
319 ml_set_interrupts_enabled(oldlevel
);
324 #pragma mark **** interrupt ****
326 static kern_return_t
chud_null_int(uint32_t trapentry
, thread_flavor_t flavor
,
327 thread_state_t tstate
, mach_msg_type_number_t count
);
328 static chudxnu_interrupt_callback_func_t interrupt_callback_fn
= chud_null_int
;
330 static kern_return_t
chud_null_int(uint32_t trapentry __unused
, thread_flavor_t flavor __unused
,
331 thread_state_t tstate __unused
, mach_msg_type_number_t count __unused
) {
336 chudxnu_private_interrupt_callback(void *foo
) __attribute__((used
));
339 chudxnu_private_interrupt_callback(void *foo
)
342 chudxnu_interrupt_callback_func_t fn
= interrupt_callback_fn
;
346 x86_thread_state_t state
;
347 mach_msg_type_number_t count
;
349 oldlevel
= ml_set_interrupts_enabled(FALSE
);
351 count
= x86_THREAD_STATE_COUNT
;
352 if(chudxnu_thread_get_state(current_thread(),
354 (thread_state_t
)&state
,
356 FALSE
) == KERN_SUCCESS
) {
358 X86_INTERRUPT_PERFMON
,
360 (thread_state_t
)&state
,
363 ml_set_interrupts_enabled(oldlevel
);
367 __private_extern__ kern_return_t
368 chudxnu_interrupt_callback_enter(chudxnu_interrupt_callback_func_t func
)
370 if(OSCompareAndSwapPtr(chud_null_int
, func
,
371 (void * volatile *)&interrupt_callback_fn
)) {
372 lapic_set_pmi_func((i386_intr_func_t
)chudxnu_private_interrupt_callback
);
378 __private_extern__ kern_return_t
379 chudxnu_interrupt_callback_cancel(void)
381 chudxnu_interrupt_callback_func_t old
= interrupt_callback_fn
;
383 while(!OSCompareAndSwapPtr(old
, chud_null_int
,
384 (void * volatile *)&interrupt_callback_fn
)) {
385 old
= interrupt_callback_fn
;
388 lapic_set_pmi_func(NULL
);
393 #pragma mark **** cpu signal ****
395 static chudxnu_cpusig_callback_func_t cpusig_callback_fn
= NULL
;
398 chudxnu_private_cpu_signal_handler(int request
)
400 chudxnu_cpusig_callback_func_t fn
= cpusig_callback_fn
;
403 x86_thread_state_t state
;
404 mach_msg_type_number_t count
= x86_THREAD_STATE_COUNT
;
406 if (chudxnu_thread_get_state(current_thread(),
408 (thread_state_t
) &state
, &count
,
409 FALSE
) == KERN_SUCCESS
) {
411 request
, x86_THREAD_STATE
,
412 (thread_state_t
) &state
, count
);
417 return KERN_SUCCESS
; //ignored
420 * chudxnu_cpu_signal_handler() is called from the IPI handler
421 * when a CHUD signal arrives from another processor.
423 __private_extern__
void
424 chudxnu_cpu_signal_handler(void)
426 chudcpu_signal_request_t
*reqp
;
427 chudcpu_data_t
*chudinfop
;
429 chudinfop
= (chudcpu_data_t
*) current_cpu_datap()->cpu_chud
;
431 mpdequeue_head(&(chudinfop
->cpu_request_queue
),
432 (queue_entry_t
*) &reqp
);
433 while (reqp
!= NULL
) {
434 chudxnu_private_cpu_signal_handler(reqp
->req_code
);
436 mpdequeue_head(&(chudinfop
->cpu_request_queue
),
437 (queue_entry_t
*) &reqp
);
441 __private_extern__ kern_return_t
442 chudxnu_cpusig_callback_enter(chudxnu_cpusig_callback_func_t func
)
444 if(OSCompareAndSwapPtr(NULL
, func
,
445 (void * volatile *)&cpusig_callback_fn
)) {
451 __private_extern__ kern_return_t
452 chudxnu_cpusig_callback_cancel(void)
454 chudxnu_cpusig_callback_func_t old
= cpusig_callback_fn
;
456 while(!OSCompareAndSwapPtr(old
, NULL
,
457 (void * volatile *)&cpusig_callback_fn
)) {
458 old
= cpusig_callback_fn
;
464 __private_extern__ kern_return_t
465 chudxnu_cpusig_send(int otherCPU
, uint32_t request_code
)
468 kern_return_t retval
= KERN_FAILURE
;
469 chudcpu_signal_request_t request
;
471 chudcpu_data_t
*target_chudp
;
474 disable_preemption();
475 // force interrupts on for a cross CPU signal.
476 old_level
= chudxnu_set_interrupts_enabled(TRUE
);
477 thisCPU
= cpu_number();
479 if ((unsigned) otherCPU
< real_ncpus
&&
480 thisCPU
!= otherCPU
&&
481 cpu_data_ptr
[otherCPU
]->cpu_running
) {
483 target_chudp
= (chudcpu_data_t
*)
484 cpu_data_ptr
[otherCPU
]->cpu_chud
;
486 /* Fill out request */
487 request
.req_sync
= 0xFFFFFFFF; /* set sync flag */
488 //request.req_type = CPRQchud; /* set request type */
489 request
.req_code
= request_code
; /* set request */
492 * Insert the new request in the target cpu's request queue
493 * and signal target cpu.
495 mpenqueue_tail(&target_chudp
->cpu_request_queue
,
497 i386_signal_cpu(otherCPU
, MP_CHUD
, ASYNC
);
499 /* Wait for response or timeout */
500 deadline
= mach_absolute_time() + LockTimeOut
;
501 while (request
.req_sync
!= 0) {
502 if (mach_absolute_time() > deadline
) {
503 panic("chudxnu_cpusig_send(%d,%d) timed out\n",
504 otherCPU
, request_code
);
508 retval
= KERN_SUCCESS
;
510 retval
= KERN_INVALID_ARGUMENT
;
513 chudxnu_set_interrupts_enabled(old_level
);