2 * Copyright (c) 2000-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@
35 #include <mach_ldebug.h>
38 #include <mach/mach_types.h>
39 #include <mach/kern_return.h>
41 #include <kern/kern_types.h>
42 #include <kern/startup.h>
43 #include <kern/timer_queue.h>
44 #include <kern/processor.h>
45 #include <kern/cpu_number.h>
46 #include <kern/cpu_data.h>
47 #include <kern/assert.h>
48 #include <kern/machine.h>
50 #include <kern/misc_protos.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_kern.h>
55 #include <profiling/profile-mk.h>
57 #include <i386/proc_reg.h>
58 #include <i386/cpu_threads.h>
59 #include <i386/mp_desc.h>
60 #include <i386/misc_protos.h>
61 #include <i386/trap.h>
62 #include <i386/postcode.h>
63 #include <i386/machine_routines.h>
65 #include <i386/mp_events.h>
66 #include <i386/lapic.h>
68 #include <i386/cpuid.h>
70 #include <i386/machine_cpu.h>
71 #include <i386/mtrr.h>
72 #include <i386/pmCPU.h>
74 #include <i386/machine_check.h>
76 #include <i386/acpi.h>
78 #include <chud/chud_xnu.h>
79 #include <chud/chud_xnu_private.h>
81 #include <sys/kdebug.h>
83 #include <machine/db_machdep.h>
84 #include <ddb/db_aout.h>
85 #include <ddb/db_access.h>
86 #include <ddb/db_sym.h>
87 #include <ddb/db_variables.h>
88 #include <ddb/db_command.h>
89 #include <ddb/db_output.h>
90 #include <ddb/db_expr.h>
94 #define PAUSE delay(1000000)
95 #define DBG(x...) kprintf(x)
102 void slave_boot_init(void);
105 static void mp_kdb_wait(void);
106 volatile boolean_t mp_kdb_trap
= FALSE
;
107 volatile long mp_kdb_ncpus
= 0;
110 static void mp_kdp_wait(boolean_t flush
, boolean_t isNMI
);
111 static void mp_rendezvous_action(void);
112 static void mp_broadcast_action(void);
114 static boolean_t
cpu_signal_pending(int cpu
, mp_event_t event
);
115 static int cpu_signal_handler(x86_saved_state_t
*regs
);
116 static int NMIInterruptHandler(x86_saved_state_t
*regs
);
118 boolean_t smp_initialized
= FALSE
;
119 volatile boolean_t force_immediate_debugger_NMI
= FALSE
;
120 volatile boolean_t pmap_tlb_flush_timeout
= FALSE
;
121 decl_simple_lock_data(,mp_kdp_lock
);
123 decl_lck_mtx_data(static, mp_cpu_boot_lock
);
124 lck_mtx_ext_t mp_cpu_boot_lock_ext
;
126 /* Variables needed for MP rendezvous. */
127 decl_simple_lock_data(,mp_rv_lock
);
128 static void (*mp_rv_setup_func
)(void *arg
);
129 static void (*mp_rv_action_func
)(void *arg
);
130 static void (*mp_rv_teardown_func
)(void *arg
);
131 static void *mp_rv_func_arg
;
132 static volatile int mp_rv_ncpus
;
133 /* Cache-aligned barriers: */
134 static volatile long mp_rv_entry
__attribute__((aligned(64)));
135 static volatile long mp_rv_exit
__attribute__((aligned(64)));
136 static volatile long mp_rv_complete
__attribute__((aligned(64)));
138 volatile uint64_t debugger_entry_time
;
139 volatile uint64_t debugger_exit_time
;
142 static struct _kdp_xcpu_call_func
{
143 kdp_x86_xcpu_func_t func
;
146 volatile uint16_t cpu
;
147 } kdp_xcpu_call_func
= {
153 /* Variables needed for MP broadcast. */
154 static void (*mp_bc_action_func
)(void *arg
);
155 static void *mp_bc_func_arg
;
156 static int mp_bc_ncpus
;
157 static volatile long mp_bc_count
;
158 decl_lck_mtx_data(static, mp_bc_lock
);
159 lck_mtx_ext_t mp_bc_lock_ext
;
160 static volatile int debugger_cpu
= -1;
162 static void mp_cpus_call_action(void);
163 static void mp_call_PM(void);
165 char mp_slave_stack
[PAGE_SIZE
] __attribute__((aligned(PAGE_SIZE
))); // Temp stack for slave init
170 * Initialize dummy structs for profiling. These aren't used but
171 * allows hertz_tick() to be built with GPROF defined.
173 struct profile_vars _profile_vars
;
174 struct profile_vars
*_profile_vars_cpus
[MAX_CPUS
] = { &_profile_vars
};
175 #define GPROF_INIT() \
179 /* Hack to initialize pointers to unused profiling structs */ \
180 for (i = 1; i < MAX_CPUS; i++) \
181 _profile_vars_cpus[i] = &_profile_vars; \
187 static lck_grp_t smp_lck_grp
;
188 static lck_grp_attr_t smp_lck_grp_attr
;
190 extern void slave_pstart(void);
195 simple_lock_init(&mp_kdp_lock
, 0);
196 simple_lock_init(&mp_rv_lock
, 0);
197 lck_grp_attr_setdefault(&smp_lck_grp_attr
);
198 lck_grp_init(&smp_lck_grp
, "i386_smp", &smp_lck_grp_attr
);
199 lck_mtx_init_ext(&mp_cpu_boot_lock
, &mp_cpu_boot_lock_ext
, &smp_lck_grp
, LCK_ATTR_NULL
);
200 lck_mtx_init_ext(&mp_bc_lock
, &mp_bc_lock_ext
, &smp_lck_grp
, LCK_ATTR_NULL
);
209 lapic_set_intr_func(LAPIC_NMI_INTERRUPT
, NMIInterruptHandler
);
210 lapic_set_intr_func(LAPIC_VECTOR(INTERPROCESSOR
), cpu_signal_handler
);
215 DBGLOG_CPU_INIT(master_cpu
);
217 install_real_mode_bootstrap(slave_pstart
);
219 smp_initialized
= TRUE
;
225 * Poll a CPU to see when it has marked itself as running.
228 mp_wait_for_cpu_up(int slot_num
, unsigned int iters
, unsigned int usecdelay
)
230 while (iters
-- > 0) {
231 if (cpu_datap(slot_num
)->cpu_running
)
238 * Quickly bring a CPU back online which has been halted.
241 intel_startCPU_fast(int slot_num
)
246 * Try to perform a fast restart
248 rc
= pmCPUExitHalt(slot_num
);
249 if (rc
!= KERN_SUCCESS
)
251 * The CPU was not eligible for a fast restart.
256 * Wait until the CPU is back online.
258 mp_disable_preemption();
261 * We use short pauses (1us) for low latency. 30,000 iterations is
262 * longer than a full restart would require so it should be more
265 mp_wait_for_cpu_up(slot_num
, 30000, 1);
266 mp_enable_preemption();
269 * Check to make sure that the CPU is really running. If not,
270 * go through the slow path.
272 if (cpu_datap(slot_num
)->cpu_running
)
273 return(KERN_SUCCESS
);
275 return(KERN_FAILURE
);
282 } processor_start_info_t
;
284 static processor_start_info_t start_info
;
290 processor_start_info_t
*psip
= (processor_start_info_t
*) arg
;
292 /* Ignore this if the current processor is not the starter */
293 if (cpu_number() != psip
->starter_cpu
)
296 LAPIC_WRITE(ICRD
, psip
->target_lapic
<< LAPIC_ICRD_DEST_SHIFT
);
297 LAPIC_WRITE(ICR
, LAPIC_ICR_DM_INIT
);
300 LAPIC_WRITE(ICRD
, psip
->target_lapic
<< LAPIC_ICRD_DEST_SHIFT
);
301 LAPIC_WRITE(ICR
, LAPIC_ICR_DM_STARTUP
|(REAL_MODE_BOOTSTRAP_OFFSET
>>12));
303 #ifdef POSTCODE_DELAY
304 /* Wait much longer if postcodes are displayed for a delay period. */
307 mp_wait_for_cpu_up(psip
->target_cpu
, i
*100, 100);
310 extern char prot_mode_gdt
[];
311 extern char slave_boot_base
[];
312 extern char real_mode_bootstrap_base
[];
313 extern char real_mode_bootstrap_end
[];
314 extern char slave_boot_end
[];
320 int lapic
= cpu_to_lapic
[slot_num
];
325 DBGLOG_CPU_INIT(slot_num
);
327 DBG("intel_startCPU(%d) lapic_id=%d\n", slot_num
, lapic
);
328 DBG("IdlePTD(%p): 0x%x\n", &IdlePTD
, (int) IdlePTD
);
331 * Initialize (or re-initialize) the descriptor tables for this cpu.
332 * Propagate processor mode to slave.
334 if (cpu_mode_is64bit())
335 cpu_desc_init64(cpu_datap(slot_num
));
337 cpu_desc_init(cpu_datap(slot_num
));
339 /* Serialize use of the slave boot stack, etc. */
340 lck_mtx_lock(&mp_cpu_boot_lock
);
342 istate
= ml_set_interrupts_enabled(FALSE
);
343 if (slot_num
== get_cpu_number()) {
344 ml_set_interrupts_enabled(istate
);
345 lck_mtx_unlock(&mp_cpu_boot_lock
);
349 start_info
.starter_cpu
= cpu_number();
350 start_info
.target_cpu
= slot_num
;
351 start_info
.target_lapic
= lapic
;
354 * Perform the processor startup sequence with all running
355 * processors rendezvous'ed. This is required during periods when
356 * the cache-disable bit is set for MTRR/PAT initialization.
358 mp_rendezvous_no_intrs(start_cpu
, (void *) &start_info
);
360 ml_set_interrupts_enabled(istate
);
361 lck_mtx_unlock(&mp_cpu_boot_lock
);
363 if (!cpu_datap(slot_num
)->cpu_running
) {
364 kprintf("Failed to start CPU %02d\n", slot_num
);
365 printf("Failed to start CPU %02d, rebooting...\n", slot_num
);
370 kprintf("Started cpu %d (lapic id %08x)\n", slot_num
, lapic
);
376 cpu_signal_event_log_t
*cpu_signal
[MAX_CPUS
];
377 cpu_signal_event_log_t
*cpu_handle
[MAX_CPUS
];
379 MP_EVENT_NAME_DECL();
381 #endif /* MP_DEBUG */
384 cpu_signal_handler(x86_saved_state_t
*regs
)
387 volatile int *my_word
;
388 #if MACH_KDB && MACH_ASSERT
390 #endif /* MACH_KDB && MACH_ASSERT */
392 mp_disable_preemption();
394 my_cpu
= cpu_number();
395 my_word
= ¤t_cpu_datap()->cpu_signals
;
398 #if MACH_KDB && MACH_ASSERT
400 Debugger("cpu_signal_handler: signals did not clear");
401 #endif /* MACH_KDB && MACH_ASSERT */
403 if (i_bit(MP_KDP
, my_word
)) {
404 DBGLOG(cpu_handle
,my_cpu
,MP_KDP
);
405 i_bit_clear(MP_KDP
, my_word
);
406 /* Ensure that the i386_kernel_state at the base of the
407 * current thread's stack (if any) is synchronized with the
408 * context at the moment of the interrupt, to facilitate
409 * access through the debugger.
411 sync_iss_to_iks(regs
);
412 mp_kdp_wait(TRUE
, FALSE
);
414 #endif /* MACH_KDP */
415 if (i_bit(MP_TLB_FLUSH
, my_word
)) {
416 DBGLOG(cpu_handle
,my_cpu
,MP_TLB_FLUSH
);
417 i_bit_clear(MP_TLB_FLUSH
, my_word
);
418 pmap_update_interrupt();
419 } else if (i_bit(MP_AST
, my_word
)) {
420 DBGLOG(cpu_handle
,my_cpu
,MP_AST
);
421 i_bit_clear(MP_AST
, my_word
);
422 ast_check(cpu_to_processor(my_cpu
));
424 } else if (i_bit(MP_KDB
, my_word
)) {
426 i_bit_clear(MP_KDB
, my_word
);
427 current_cpu_datap()->cpu_kdb_is_slave
++;
429 current_cpu_datap()->cpu_kdb_is_slave
--;
430 #endif /* MACH_KDB */
431 } else if (i_bit(MP_RENDEZVOUS
, my_word
)) {
432 DBGLOG(cpu_handle
,my_cpu
,MP_RENDEZVOUS
);
433 i_bit_clear(MP_RENDEZVOUS
, my_word
);
434 mp_rendezvous_action();
435 } else if (i_bit(MP_BROADCAST
, my_word
)) {
436 DBGLOG(cpu_handle
,my_cpu
,MP_BROADCAST
);
437 i_bit_clear(MP_BROADCAST
, my_word
);
438 mp_broadcast_action();
439 } else if (i_bit(MP_CHUD
, my_word
)) {
440 DBGLOG(cpu_handle
,my_cpu
,MP_CHUD
);
441 i_bit_clear(MP_CHUD
, my_word
);
442 chudxnu_cpu_signal_handler();
443 } else if (i_bit(MP_CALL
, my_word
)) {
444 DBGLOG(cpu_handle
,my_cpu
,MP_CALL
);
445 i_bit_clear(MP_CALL
, my_word
);
446 mp_cpus_call_action();
447 } else if (i_bit(MP_CALL_PM
, my_word
)) {
448 DBGLOG(cpu_handle
,my_cpu
,MP_CALL_PM
);
449 i_bit_clear(MP_CALL_PM
, my_word
);
454 mp_enable_preemption();
460 NMIInterruptHandler(x86_saved_state_t
*regs
)
464 sync_iss_to_iks_unconditionally(regs
);
465 #if defined (__i386__)
466 __asm__
volatile("movl %%ebp, %0" : "=m" (stackptr
));
467 #elif defined (__x86_64__)
468 __asm__
volatile("movq %%rbp, %0" : "=m" (stackptr
));
471 if (cpu_number() == debugger_cpu
)
474 if (pmap_tlb_flush_timeout
== TRUE
&& current_cpu_datap()->cpu_tlb_invalid
) {
476 snprintf(&pstr
[0], sizeof(pstr
), "Panic(CPU %d): Unresponsive processor\n", cpu_number());
477 panic_i386_backtrace(stackptr
, 16, &pstr
[0], TRUE
, regs
);
481 mp_kdp_wait(FALSE
, pmap_tlb_flush_timeout
);
488 int max_lock_loops
= 100000000;
489 int trappedalready
= 0; /* (BRINGUP) */
490 #endif /* MP_DEBUG */
493 i386_cpu_IPI(int cpu
)
498 if(cpu_datap(cpu
)->cpu_signals
& 6) { /* (BRINGUP) */
499 kprintf("i386_cpu_IPI: sending enter debugger signal (%08X) to cpu %d\n", cpu_datap(cpu
)->cpu_signals
, cpu
);
501 #endif /* MP_DEBUG */
505 if(!trappedalready
&& (cpu_datap(cpu
)->cpu_signals
& 6)) { /* (BRINGUP) */
506 if(kdb_cpu
!= cpu_number()) {
508 panic("i386_cpu_IPI: sending enter debugger signal (%08X) to cpu %d and I do not own debugger, owner = %08X\n",
509 cpu_datap(cpu
)->cpu_signals
, cpu
, kdb_cpu
);
512 #endif /* MP_DEBUG */
515 /* Wait for previous interrupt to be delivered... */
517 int pending_busy_count
= 0;
518 while (LAPIC_READ(ICR
) & LAPIC_ICR_DS_PENDING
) {
519 if (++pending_busy_count
> max_lock_loops
)
520 panic("i386_cpu_IPI() deadlock\n");
522 while (LAPIC_READ(ICR
) & LAPIC_ICR_DS_PENDING
) {
523 #endif /* MP_DEBUG */
527 state
= ml_set_interrupts_enabled(FALSE
);
528 LAPIC_WRITE(ICRD
, cpu_to_lapic
[cpu
] << LAPIC_ICRD_DEST_SHIFT
);
529 LAPIC_WRITE(ICR
, LAPIC_VECTOR(INTERPROCESSOR
) | LAPIC_ICR_DM_FIXED
);
530 (void) ml_set_interrupts_enabled(state
);
534 * cpu_interrupt is really just to be used by the scheduler to
535 * get a CPU's attention it may not always issue an IPI. If an
536 * IPI is always needed then use i386_cpu_IPI.
539 cpu_interrupt(int cpu
)
542 && pmCPUExitIdle(cpu_datap(cpu
))) {
548 * Send a true NMI via the local APIC to the specified CPU.
551 cpu_NMI_interrupt(int cpu
)
555 if (smp_initialized
) {
556 state
= ml_set_interrupts_enabled(FALSE
);
557 /* Program the interrupt command register */
558 LAPIC_WRITE(ICRD
, cpu_to_lapic
[cpu
] << LAPIC_ICRD_DEST_SHIFT
);
559 /* The vector is ignored in this case--the target CPU will enter on the
562 LAPIC_WRITE(ICR
, LAPIC_VECTOR(INTERPROCESSOR
)|LAPIC_ICR_DM_NMI
);
563 (void) ml_set_interrupts_enabled(state
);
567 static void (* volatile mp_PM_func
)(void) = NULL
;
572 assert(!ml_get_interrupts_enabled());
574 if (mp_PM_func
!= NULL
)
579 cpu_PM_interrupt(int cpu
)
581 assert(!ml_get_interrupts_enabled());
583 if (mp_PM_func
!= NULL
) {
584 if (cpu
== cpu_number())
587 i386_signal_cpu(cpu
, MP_CALL_PM
, ASYNC
);
592 PM_interrupt_register(void (*fn
)(void))
598 i386_signal_cpu(int cpu
, mp_event_t event
, mp_sync_t mode
)
600 volatile int *signals
= &cpu_datap(cpu
)->cpu_signals
;
601 uint64_t tsc_timeout
;
604 if (!cpu_datap(cpu
)->cpu_running
)
607 if (event
== MP_TLB_FLUSH
)
608 KERNEL_DEBUG(0xef800020 | DBG_FUNC_START
, cpu
, 0, 0, 0, 0);
610 DBGLOG(cpu_signal
, cpu
, event
);
612 i_bit_set(event
, signals
);
616 tsc_timeout
= rdtsc64() + (1000*1000*1000);
617 while (i_bit(event
, signals
) && rdtsc64() < tsc_timeout
) {
620 if (i_bit(event
, signals
)) {
621 DBG("i386_signal_cpu(%d, 0x%x, SYNC) timed out\n",
626 if (event
== MP_TLB_FLUSH
)
627 KERNEL_DEBUG(0xef800020 | DBG_FUNC_END
, cpu
, 0, 0, 0, 0);
631 * Send event to all running cpus.
632 * Called with the topology locked.
635 i386_signal_cpus(mp_event_t event
, mp_sync_t mode
)
638 unsigned int my_cpu
= cpu_number();
640 assert(hw_lock_held((hw_lock_t
)&x86_topo_lock
));
642 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
643 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
645 i386_signal_cpu(cpu
, event
, mode
);
650 * Return the number of running cpus.
651 * Called with the topology locked.
654 i386_active_cpus(void)
657 unsigned int ncpus
= 0;
659 assert(hw_lock_held((hw_lock_t
)&x86_topo_lock
));
661 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
662 if (cpu_datap(cpu
)->cpu_running
)
669 * All-CPU rendezvous:
670 * - CPUs are signalled,
671 * - all execute the setup function (if specified),
672 * - rendezvous (i.e. all cpus reach a barrier),
673 * - all execute the action function (if specified),
674 * - rendezvous again,
675 * - execute the teardown function (if specified), and then
678 * Note that the supplied external functions _must_ be reentrant and aware
679 * that they are running in parallel and in an unknown lock context.
683 mp_rendezvous_action(void)
685 boolean_t intrs_enabled
;
688 if (mp_rv_setup_func
!= NULL
)
689 mp_rv_setup_func(mp_rv_func_arg
);
691 intrs_enabled
= ml_get_interrupts_enabled();
694 /* spin on entry rendezvous */
695 atomic_incl(&mp_rv_entry
, 1);
696 while (mp_rv_entry
< mp_rv_ncpus
) {
697 /* poll for pesky tlb flushes if interrupts disabled */
699 handle_pending_TLB_flushes();
702 /* action function */
703 if (mp_rv_action_func
!= NULL
)
704 mp_rv_action_func(mp_rv_func_arg
);
705 /* spin on exit rendezvous */
706 atomic_incl(&mp_rv_exit
, 1);
707 while (mp_rv_exit
< mp_rv_ncpus
) {
709 handle_pending_TLB_flushes();
712 /* teardown function */
713 if (mp_rv_teardown_func
!= NULL
)
714 mp_rv_teardown_func(mp_rv_func_arg
);
716 /* Bump completion count */
717 atomic_incl(&mp_rv_complete
, 1);
721 mp_rendezvous(void (*setup_func
)(void *),
722 void (*action_func
)(void *),
723 void (*teardown_func
)(void *),
727 if (!smp_initialized
) {
728 if (setup_func
!= NULL
)
730 if (action_func
!= NULL
)
732 if (teardown_func
!= NULL
)
737 /* obtain rendezvous lock */
738 simple_lock(&mp_rv_lock
);
740 /* set static function pointers */
741 mp_rv_setup_func
= setup_func
;
742 mp_rv_action_func
= action_func
;
743 mp_rv_teardown_func
= teardown_func
;
744 mp_rv_func_arg
= arg
;
751 * signal other processors, which will call mp_rendezvous_action()
752 * with interrupts disabled
754 simple_lock(&x86_topo_lock
);
755 mp_rv_ncpus
= i386_active_cpus();
756 i386_signal_cpus(MP_RENDEZVOUS
, ASYNC
);
757 simple_unlock(&x86_topo_lock
);
759 /* call executor function on this cpu */
760 mp_rendezvous_action();
763 * Spin for everyone to complete.
764 * This is necessary to ensure that all processors have proceeded
765 * from the exit barrier before we release the rendezvous structure.
767 while (mp_rv_complete
< mp_rv_ncpus
) {
772 mp_rv_setup_func
= NULL
;
773 mp_rv_action_func
= NULL
;
774 mp_rv_teardown_func
= NULL
;
775 mp_rv_func_arg
= NULL
;
778 simple_unlock(&mp_rv_lock
);
782 mp_rendezvous_break_lock(void)
784 simple_lock_init(&mp_rv_lock
, 0);
788 setup_disable_intrs(__unused
void * param_not_used
)
790 /* disable interrupts before the first barrier */
791 boolean_t intr
= ml_set_interrupts_enabled(FALSE
);
793 current_cpu_datap()->cpu_iflag
= intr
;
794 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__
);
798 teardown_restore_intrs(__unused
void * param_not_used
)
800 /* restore interrupt flag following MTRR changes */
801 ml_set_interrupts_enabled(current_cpu_datap()->cpu_iflag
);
802 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__
);
806 * A wrapper to mp_rendezvous() to call action_func() with interrupts disabled.
807 * This is exported for use by kexts.
810 mp_rendezvous_no_intrs(
811 void (*action_func
)(void *),
814 mp_rendezvous(setup_disable_intrs
,
816 teardown_restore_intrs
,
821 handle_pending_TLB_flushes(void)
823 volatile int *my_word
= ¤t_cpu_datap()->cpu_signals
;
825 if (i_bit(MP_TLB_FLUSH
, my_word
)) {
826 DBGLOG(cpu_handle
, cpu_number(), MP_TLB_FLUSH
);
827 i_bit_clear(MP_TLB_FLUSH
, my_word
);
828 pmap_update_interrupt();
833 * This is called from cpu_signal_handler() to process an MP_CALL signal.
836 mp_cpus_call_action(void)
838 if (mp_rv_action_func
!= NULL
)
839 mp_rv_action_func(mp_rv_func_arg
);
840 atomic_incl(&mp_rv_complete
, 1);
844 * mp_cpus_call() runs a given function on cpus specified in a given cpu mask.
845 * If the mode is SYNC, the function is called serially on the target cpus
846 * in logical cpu order. If the mode is ASYNC, the function is called in
847 * parallel over the specified cpus.
848 * The action function may be NULL.
849 * The cpu mask may include the local cpu. Offline cpus are ignored.
850 * Return does not occur until the function has completed on all cpus.
851 * The return value is the number of cpus on which the function was called.
857 void (*action_func
)(void *),
861 boolean_t intrs_enabled
= ml_get_interrupts_enabled();
862 boolean_t call_self
= FALSE
;
864 if (!smp_initialized
) {
865 if ((cpus
& CPUMASK_SELF
) == 0)
867 if (action_func
!= NULL
) {
868 (void) ml_set_interrupts_enabled(FALSE
);
870 ml_set_interrupts_enabled(intrs_enabled
);
875 /* obtain rendezvous lock */
876 simple_lock(&mp_rv_lock
);
878 /* Use the rendezvous data structures for this call */
879 mp_rv_action_func
= action_func
;
880 mp_rv_func_arg
= arg
;
884 simple_lock(&x86_topo_lock
);
885 for (cpu
= 0; cpu
< (cpu_t
) real_ncpus
; cpu
++) {
886 if (((cpu_to_cpumask(cpu
) & cpus
) == 0) ||
887 !cpu_datap(cpu
)->cpu_running
)
889 if (cpu
== (cpu_t
) cpu_number()) {
891 * We don't IPI ourself and if calling asynchronously,
892 * we defer our call until we have signalled all others.
895 if (mode
== SYNC
&& action_func
!= NULL
) {
896 (void) ml_set_interrupts_enabled(FALSE
);
898 ml_set_interrupts_enabled(intrs_enabled
);
902 * Bump count of other cpus called and signal this cpu.
903 * Note: we signal asynchronously regardless of mode
904 * because we wait on mp_rv_complete either here
905 * (if mode == SYNC) or later (if mode == ASYNC).
906 * While spinning, poll for TLB flushes if interrupts
910 i386_signal_cpu(cpu
, MP_CALL
, ASYNC
);
912 simple_unlock(&x86_topo_lock
);
913 while (mp_rv_complete
< mp_rv_ncpus
) {
915 handle_pending_TLB_flushes();
918 simple_lock(&x86_topo_lock
);
922 simple_unlock(&x86_topo_lock
);
925 * If calls are being made asynchronously,
926 * make the local call now if needed, and then
927 * wait for all other cpus to finish their calls.
930 if (call_self
&& action_func
!= NULL
) {
931 (void) ml_set_interrupts_enabled(FALSE
);
933 ml_set_interrupts_enabled(intrs_enabled
);
935 while (mp_rv_complete
< mp_rv_ncpus
) {
937 handle_pending_TLB_flushes();
942 /* Determine the number of cpus called */
943 cpu
= mp_rv_ncpus
+ (call_self
? 1 : 0);
945 simple_unlock(&mp_rv_lock
);
951 mp_broadcast_action(void)
953 /* call action function */
954 if (mp_bc_action_func
!= NULL
)
955 mp_bc_action_func(mp_bc_func_arg
);
957 /* if we're the last one through, wake up the instigator */
958 if (atomic_decl_and_test(&mp_bc_count
, 1))
959 thread_wakeup(((event_t
)(uintptr_t) &mp_bc_count
));
963 * mp_broadcast() runs a given function on all active cpus.
964 * The caller blocks until the functions has run on all cpus.
965 * The caller will also block if there is another pending braodcast.
969 void (*action_func
)(void *),
972 if (!smp_initialized
) {
973 if (action_func
!= NULL
)
978 /* obtain broadcast lock */
979 lck_mtx_lock(&mp_bc_lock
);
981 /* set static function pointers */
982 mp_bc_action_func
= action_func
;
983 mp_bc_func_arg
= arg
;
985 assert_wait((event_t
)(uintptr_t)&mp_bc_count
, THREAD_UNINT
);
988 * signal other processors, which will call mp_broadcast_action()
990 simple_lock(&x86_topo_lock
);
991 mp_bc_ncpus
= i386_active_cpus(); /* total including this cpu */
992 mp_bc_count
= mp_bc_ncpus
;
993 i386_signal_cpus(MP_BROADCAST
, ASYNC
);
995 /* call executor function on this cpu */
996 mp_broadcast_action();
997 simple_unlock(&x86_topo_lock
);
999 /* block for all cpus to have run action_func */
1000 if (mp_bc_ncpus
> 1)
1001 thread_block(THREAD_CONTINUE_NULL
);
1003 clear_wait(current_thread(), THREAD_AWAKENED
);
1006 lck_mtx_unlock(&mp_bc_lock
);
1010 i386_activate_cpu(void)
1012 cpu_data_t
*cdp
= current_cpu_datap();
1014 assert(!ml_get_interrupts_enabled());
1016 if (!smp_initialized
) {
1017 cdp
->cpu_running
= TRUE
;
1021 simple_lock(&x86_topo_lock
);
1022 cdp
->cpu_running
= TRUE
;
1023 simple_unlock(&x86_topo_lock
);
1026 extern void etimer_timer_expire(void *arg
);
1029 i386_deactivate_cpu(void)
1031 cpu_data_t
*cdp
= current_cpu_datap();
1033 assert(!ml_get_interrupts_enabled());
1035 simple_lock(&x86_topo_lock
);
1036 cdp
->cpu_running
= FALSE
;
1037 simple_unlock(&x86_topo_lock
);
1039 timer_queue_shutdown(&cdp
->rtclock_timer
.queue
);
1040 cdp
->rtclock_timer
.deadline
= EndOfAllTime
;
1041 mp_cpus_call(cpu_to_cpumask(master_cpu
), ASYNC
, etimer_timer_expire
, NULL
);
1044 * In case a rendezvous/braodcast/call was initiated to this cpu
1045 * before we cleared cpu_running, we must perform any actions due.
1047 if (i_bit(MP_RENDEZVOUS
, &cdp
->cpu_signals
))
1048 mp_rendezvous_action();
1049 if (i_bit(MP_BROADCAST
, &cdp
->cpu_signals
))
1050 mp_broadcast_action();
1051 if (i_bit(MP_CALL
, &cdp
->cpu_signals
))
1052 mp_cpus_call_action();
1053 cdp
->cpu_signals
= 0; /* all clear */
1056 int pmsafe_debug
= 1;
1059 volatile boolean_t mp_kdp_trap
= FALSE
;
1060 volatile unsigned long mp_kdp_ncpus
;
1061 boolean_t mp_kdp_state
;
1069 unsigned int my_cpu
;
1070 uint64_t tsc_timeout
;
1072 DBG("mp_kdp_enter()\n");
1075 * Here to enter the debugger.
1076 * In case of races, only one cpu is allowed to enter kdp after
1079 mp_kdp_state
= ml_set_interrupts_enabled(FALSE
);
1080 simple_lock(&mp_kdp_lock
);
1081 debugger_entry_time
= mach_absolute_time();
1083 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
1085 while (mp_kdp_trap
) {
1086 simple_unlock(&mp_kdp_lock
);
1087 DBG("mp_kdp_enter() race lost\n");
1089 mp_kdp_wait(TRUE
, FALSE
);
1091 simple_lock(&mp_kdp_lock
);
1093 my_cpu
= cpu_number();
1094 debugger_cpu
= my_cpu
;
1095 mp_kdp_ncpus
= 1; /* self */
1097 simple_unlock(&mp_kdp_lock
);
1100 * Deliver a nudge to other cpus, counting how many
1102 DBG("mp_kdp_enter() signaling other processors\n");
1103 if (force_immediate_debugger_NMI
== FALSE
) {
1104 for (ncpus
= 1, cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1105 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1108 i386_signal_cpu(cpu
, MP_KDP
, ASYNC
);
1111 * Wait other processors to synchronize
1113 DBG("mp_kdp_enter() waiting for (%d) processors to suspend\n", ncpus
);
1116 * This timeout is rather arbitrary; we don't want to NMI
1117 * processors that are executing at potentially
1118 * "unsafe-to-interrupt" points such as the trampolines,
1119 * but neither do we want to lose state by waiting too long.
1121 tsc_timeout
= rdtsc64() + (ncpus
* 1000 * 1000);
1123 while (mp_kdp_ncpus
!= ncpus
&& rdtsc64() < tsc_timeout
) {
1125 * A TLB shootdown request may be pending--this would
1126 * result in the requesting processor waiting in
1127 * PMAP_UPDATE_TLBS() until this processor deals with it.
1128 * Process it, so it can now enter mp_kdp_wait()
1130 handle_pending_TLB_flushes();
1133 /* If we've timed out, and some processor(s) are still unresponsive,
1134 * interrupt them with an NMI via the local APIC.
1136 if (mp_kdp_ncpus
!= ncpus
) {
1137 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1138 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1140 if (cpu_signal_pending(cpu
, MP_KDP
))
1141 cpu_NMI_interrupt(cpu
);
1146 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1147 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1149 cpu_NMI_interrupt(cpu
);
1152 DBG("mp_kdp_enter() %u processors done %s\n",
1153 mp_kdp_ncpus
, (mp_kdp_ncpus
== ncpus
) ? "OK" : "timed out");
1155 postcode(MP_KDP_ENTER
);
1159 cpu_signal_pending(int cpu
, mp_event_t event
)
1161 volatile int *signals
= &cpu_datap(cpu
)->cpu_signals
;
1162 boolean_t retval
= FALSE
;
1164 if (i_bit(event
, signals
))
1169 long kdp_x86_xcpu_invoke(const uint16_t lcpu
, kdp_x86_xcpu_func_t func
,
1170 void *arg0
, void *arg1
)
1172 if (lcpu
> (real_ncpus
- 1))
1178 kdp_xcpu_call_func
.func
= func
;
1179 kdp_xcpu_call_func
.ret
= -1;
1180 kdp_xcpu_call_func
.arg0
= arg0
;
1181 kdp_xcpu_call_func
.arg1
= arg1
;
1182 kdp_xcpu_call_func
.cpu
= lcpu
;
1183 DBG("Invoking function %p on CPU %d\n", func
, (int32_t)lcpu
);
1184 while (kdp_xcpu_call_func
.cpu
!= KDP_XCPU_NONE
)
1186 return kdp_xcpu_call_func
.ret
;
1190 kdp_x86_xcpu_poll(void)
1192 if ((uint16_t)cpu_number() == kdp_xcpu_call_func
.cpu
) {
1193 kdp_xcpu_call_func
.ret
=
1194 kdp_xcpu_call_func
.func(kdp_xcpu_call_func
.arg0
,
1195 kdp_xcpu_call_func
.arg1
,
1197 kdp_xcpu_call_func
.cpu
= KDP_XCPU_NONE
;
1202 mp_kdp_wait(boolean_t flush
, boolean_t isNMI
)
1204 DBG("mp_kdp_wait()\n");
1205 /* If an I/O port has been specified as a debugging aid, issue a read */
1206 panic_io_port_read();
1209 /* If we've trapped due to a machine-check, save MCA registers */
1214 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
1216 atomic_incl((volatile long *)&mp_kdp_ncpus
, 1);
1217 while (mp_kdp_trap
|| (isNMI
== TRUE
)) {
1219 * A TLB shootdown request may be pending--this would result
1220 * in the requesting processor waiting in PMAP_UPDATE_TLBS()
1221 * until this processor handles it.
1222 * Process it, so it can now enter mp_kdp_wait()
1225 handle_pending_TLB_flushes();
1227 kdp_x86_xcpu_poll();
1232 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_NORMAL
);
1234 atomic_decl((volatile long *)&mp_kdp_ncpus
, 1);
1235 DBG("mp_kdp_wait() done\n");
1241 DBG("mp_kdp_exit()\n");
1243 atomic_decl((volatile long *)&mp_kdp_ncpus
, 1);
1245 debugger_exit_time
= mach_absolute_time();
1247 mp_kdp_trap
= FALSE
;
1248 __asm__
volatile("mfence");
1250 /* Wait other processors to stop spinning. XXX needs timeout */
1251 DBG("mp_kdp_exit() waiting for processors to resume\n");
1252 while (mp_kdp_ncpus
> 0) {
1254 * a TLB shootdown request may be pending... this would result in the requesting
1255 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1256 * Process it, so it can now enter mp_kdp_wait()
1258 handle_pending_TLB_flushes();
1264 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_NORMAL
);
1266 DBG("mp_kdp_exit() done\n");
1267 (void) ml_set_interrupts_enabled(mp_kdp_state
);
1270 #endif /* MACH_KDP */
1273 mp_recent_debugger_activity() {
1274 return (((mach_absolute_time() - debugger_entry_time
) < LastDebuggerEntryAllowance
) ||
1275 ((mach_absolute_time() - debugger_exit_time
) < LastDebuggerEntryAllowance
));
1281 __unused processor_t processor
)
1287 processor_t processor
)
1289 int cpu
= processor
->cpu_id
;
1291 if (cpu
!= cpu_number()) {
1292 i386_signal_cpu(cpu
, MP_AST
, ASYNC
);
1298 * invoke kdb on slave processors
1304 unsigned int my_cpu
= cpu_number();
1307 uint64_t tsc_timeout
= 0;
1311 for (kdb_ncpus
= 1, cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1312 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1315 i386_signal_cpu(cpu
, MP_KDB
, ASYNC
);
1317 DBG("remote_kdb() waiting for (%d) processors to suspend\n",kdb_ncpus
);
1319 tsc_timeout
= rdtsc64() + (kdb_ncpus
* 100 * 1000 * 1000);
1321 while (mp_kdb_ncpus
!= kdb_ncpus
&& rdtsc64() < tsc_timeout
) {
1323 * a TLB shootdown request may be pending... this would result in the requesting
1324 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1325 * Process it, so it can now enter mp_kdp_wait()
1327 handle_pending_TLB_flushes();
1331 DBG("mp_kdp_enter() %d processors done %s\n",
1332 mp_kdb_ncpus
, (mp_kdb_ncpus
== kdb_ncpus
) ? "OK" : "timed out");
1338 DBG("mp_kdb_wait()\n");
1340 /* If an I/O port has been specified as a debugging aid, issue a read */
1341 panic_io_port_read();
1343 atomic_incl(&mp_kdb_ncpus
, 1);
1344 while (mp_kdb_trap
) {
1346 * a TLB shootdown request may be pending... this would result in the requesting
1347 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1348 * Process it, so it can now enter mp_kdp_wait()
1350 handle_pending_TLB_flushes();
1354 atomic_decl((volatile long *)&mp_kdb_ncpus
, 1);
1355 DBG("mp_kdb_wait() done\n");
1359 * Clear kdb interrupt
1363 clear_kdb_intr(void)
1365 mp_disable_preemption();
1366 i_bit_clear(MP_KDB
, ¤t_cpu_datap()->cpu_signals
);
1367 mp_enable_preemption();
1373 DBG("mp_kdb_exit()\n");
1374 atomic_decl((volatile long *)&mp_kdb_ncpus
, 1);
1375 mp_kdb_trap
= FALSE
;
1376 __asm__
volatile("mfence");
1378 while (mp_kdb_ncpus
> 0) {
1380 * a TLB shootdown request may be pending... this would result in the requesting
1381 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1382 * Process it, so it can now enter mp_kdp_wait()
1384 handle_pending_TLB_flushes();
1389 DBG("mp_kdb_exit() done\n");
1392 #endif /* MACH_KDB */
1395 slave_machine_init(void *param
)
1398 * Here in process context, but with interrupts disabled.
1400 DBG("slave_machine_init() CPU%d\n", get_cpu_number());
1402 if (param
== FULL_SLAVE_INIT
) {
1408 cpu_machine_init(); /* Interrupts enabled hereafter */
1413 int cpu_number(void)
1415 return get_cpu_number();
1419 #include <ddb/db_output.h>
1421 #define TRAP_DEBUG 0 /* Must match interrupt.s and spl.s */
1426 struct mp_trap_hist_struct
{
1428 unsigned char data
[5];
1429 } trap_hist
[MTRAPS
], *cur_trap_hist
= trap_hist
,
1430 *max_trap_hist
= &trap_hist
[MTRAPS
];
1432 void db_trap_hist(void);
1452 for(i
=0;i
<MTRAPS
;i
++)
1453 if (trap_hist
[i
].type
== 1 || trap_hist
[i
].type
== 2) {
1455 (&trap_hist
[i
]>=cur_trap_hist
)?"*":" ",
1456 (trap_hist
[i
].type
== 1)?"SPL":"INT");
1458 db_printf(" %02x", trap_hist
[i
].data
[j
]);
1463 #endif /* TRAP_DEBUG */
1464 #endif /* MACH_KDB */