2 * Copyright (c) 2000-2012 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@
33 #include <kdp/kdp_internal.h>
34 #include <mach_ldebug.h>
37 #include <mach/mach_types.h>
38 #include <mach/kern_return.h>
40 #include <kern/kern_types.h>
41 #include <kern/startup.h>
42 #include <kern/timer_queue.h>
43 #include <kern/processor.h>
44 #include <kern/cpu_number.h>
45 #include <kern/cpu_data.h>
46 #include <kern/assert.h>
47 #include <kern/machine.h>
49 #include <kern/misc_protos.h>
50 #include <kern/timer_call.h>
51 #include <kern/kalloc.h>
52 #include <kern/queue.h>
53 #include <prng/random.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_kern.h>
58 #include <profiling/profile-mk.h>
60 #include <i386/bit_routines.h>
61 #include <i386/proc_reg.h>
62 #include <i386/cpu_threads.h>
63 #include <i386/mp_desc.h>
64 #include <i386/misc_protos.h>
65 #include <i386/trap.h>
66 #include <i386/postcode.h>
67 #include <i386/machine_routines.h>
69 #include <i386/mp_events.h>
70 #include <i386/lapic.h>
71 #include <i386/cpuid.h>
73 #include <i386/machine_cpu.h>
74 #include <i386/pmCPU.h>
76 #include <i386/machine_check.h>
78 #include <i386/acpi.h>
80 #include <chud/chud_xnu.h>
81 #include <chud/chud_xnu_private.h>
83 #include <sys/kdebug.h>
85 #include <console/serial_protos.h>
88 #include <kern/monotonic.h>
89 #endif /* MONOTONIC */
92 #define PAUSE delay(1000000)
93 #define DBG(x...) kprintf(x)
99 /* Debugging/test trace events: */
100 #define TRACE_MP_TLB_FLUSH MACHDBG_CODE(DBG_MACH_MP, 0)
101 #define TRACE_MP_CPUS_CALL MACHDBG_CODE(DBG_MACH_MP, 1)
102 #define TRACE_MP_CPUS_CALL_LOCAL MACHDBG_CODE(DBG_MACH_MP, 2)
103 #define TRACE_MP_CPUS_CALL_ACTION MACHDBG_CODE(DBG_MACH_MP, 3)
104 #define TRACE_MP_CPUS_CALL_NOBUF MACHDBG_CODE(DBG_MACH_MP, 4)
105 #define TRACE_MP_CPU_FAST_START MACHDBG_CODE(DBG_MACH_MP, 5)
106 #define TRACE_MP_CPU_START MACHDBG_CODE(DBG_MACH_MP, 6)
107 #define TRACE_MP_CPU_DEACTIVATE MACHDBG_CODE(DBG_MACH_MP, 7)
109 #define ABS(v) (((v) > 0)?(v):-(v))
111 void slave_boot_init(void);
112 void i386_cpu_IPI(int cpu
);
115 static void mp_kdp_wait(boolean_t flush
, boolean_t isNMI
);
116 #endif /* MACH_KDP */
119 static boolean_t
cpu_signal_pending(int cpu
, mp_event_t event
);
120 #endif /* MACH_KDP */
121 static int NMIInterruptHandler(x86_saved_state_t
*regs
);
123 boolean_t smp_initialized
= FALSE
;
124 uint32_t TSC_sync_margin
= 0xFFF;
125 volatile boolean_t force_immediate_debugger_NMI
= FALSE
;
126 volatile boolean_t pmap_tlb_flush_timeout
= FALSE
;
127 #if DEBUG || DEVELOPMENT
128 boolean_t mp_interrupt_watchdog_enabled
= TRUE
;
129 uint32_t mp_interrupt_watchdog_events
= 0;
132 decl_simple_lock_data(,debugger_callback_lock
);
133 struct debugger_callback
*debugger_callback
= NULL
;
135 decl_lck_mtx_data(static, mp_cpu_boot_lock
);
136 lck_mtx_ext_t mp_cpu_boot_lock_ext
;
138 /* Variables needed for MP rendezvous. */
139 decl_simple_lock_data(,mp_rv_lock
);
140 static void (*mp_rv_setup_func
)(void *arg
);
141 static void (*mp_rv_action_func
)(void *arg
);
142 static void (*mp_rv_teardown_func
)(void *arg
);
143 static void *mp_rv_func_arg
;
144 static volatile int mp_rv_ncpus
;
145 /* Cache-aligned barriers: */
146 static volatile long mp_rv_entry
__attribute__((aligned(64)));
147 static volatile long mp_rv_exit
__attribute__((aligned(64)));
148 static volatile long mp_rv_complete
__attribute__((aligned(64)));
150 volatile uint64_t debugger_entry_time
;
151 volatile uint64_t debugger_exit_time
;
154 extern int kdp_snapshot
;
155 static struct _kdp_xcpu_call_func
{
156 kdp_x86_xcpu_func_t func
;
159 volatile uint16_t cpu
;
160 } kdp_xcpu_call_func
= {
166 /* Variables needed for MP broadcast. */
167 static void (*mp_bc_action_func
)(void *arg
);
168 static void *mp_bc_func_arg
;
169 static int mp_bc_ncpus
;
170 static volatile long mp_bc_count
;
171 decl_lck_mtx_data(static, mp_bc_lock
);
172 lck_mtx_ext_t mp_bc_lock_ext
;
173 static volatile int debugger_cpu
= -1;
174 volatile long NMIPI_acks
= 0;
175 volatile long NMI_count
= 0;
176 static NMI_reason_t NMI_panic_reason
= NONE
;
177 static int vector_timed_out
;
179 extern void NMI_cpus(void);
181 static void mp_cpus_call_init(void);
182 static void mp_cpus_call_action(void);
183 static void mp_call_PM(void);
185 char mp_slave_stack
[PAGE_SIZE
] __attribute__((aligned(PAGE_SIZE
))); // Temp stack for slave init
187 /* PAL-related routines */
188 boolean_t
i386_smp_init(int nmi_vector
, i386_intr_func_t nmi_handler
,
189 int ipi_vector
, i386_intr_func_t ipi_handler
);
190 void i386_start_cpu(int lapic_id
, int cpu_num
);
191 void i386_send_NMI(int cpu
);
195 * Initialize dummy structs for profiling. These aren't used but
196 * allows hertz_tick() to be built with GPROF defined.
198 struct profile_vars _profile_vars
;
199 struct profile_vars
*_profile_vars_cpus
[MAX_CPUS
] = { &_profile_vars
};
200 #define GPROF_INIT() \
204 /* Hack to initialize pointers to unused profiling structs */ \
205 for (i = 1; i < MAX_CPUS; i++) \
206 _profile_vars_cpus[i] = &_profile_vars; \
212 static lck_grp_t smp_lck_grp
;
213 static lck_grp_attr_t smp_lck_grp_attr
;
215 #define NUM_CPU_WARM_CALLS 20
216 struct timer_call cpu_warm_call_arr
[NUM_CPU_WARM_CALLS
];
217 queue_head_t cpu_warm_call_list
;
218 decl_simple_lock_data(static, cpu_warm_lock
);
220 typedef struct cpu_warm_data
{
221 timer_call_t cwd_call
;
222 uint64_t cwd_deadline
;
226 static void cpu_prewarm_init(void);
227 static void cpu_warm_timer_call_func(call_entry_param_t p0
, call_entry_param_t p1
);
228 static void _cpu_warm_setup(void *arg
);
229 static timer_call_t
grab_warm_timer_call(void);
230 static void free_warm_timer_call(timer_call_t call
);
235 simple_lock_init(&mp_rv_lock
, 0);
236 simple_lock_init(&debugger_callback_lock
, 0);
237 lck_grp_attr_setdefault(&smp_lck_grp_attr
);
238 lck_grp_init(&smp_lck_grp
, "i386_smp", &smp_lck_grp_attr
);
239 lck_mtx_init_ext(&mp_cpu_boot_lock
, &mp_cpu_boot_lock_ext
, &smp_lck_grp
, LCK_ATTR_NULL
);
240 lck_mtx_init_ext(&mp_bc_lock
, &mp_bc_lock_ext
, &smp_lck_grp
, LCK_ATTR_NULL
);
243 if(!i386_smp_init(LAPIC_NMI_INTERRUPT
, NMIInterruptHandler
,
244 LAPIC_VECTOR(INTERPROCESSOR
), cpu_signal_handler
))
250 DBGLOG_CPU_INIT(master_cpu
);
253 mp_cpus_call_cpu_init(master_cpu
);
255 #if DEBUG || DEVELOPMENT
256 if (PE_parse_boot_argn("interrupt_watchdog",
257 &mp_interrupt_watchdog_enabled
,
258 sizeof(mp_interrupt_watchdog_enabled
))) {
259 kprintf("Interrupt watchdog %sabled\n",
260 mp_interrupt_watchdog_enabled
? "en" : "dis");
264 if (PE_parse_boot_argn("TSC_sync_margin",
265 &TSC_sync_margin
, sizeof(TSC_sync_margin
))) {
266 kprintf("TSC sync Margin 0x%x\n", TSC_sync_margin
);
267 } else if (cpuid_vmm_present()) {
268 kprintf("TSC sync margin disabled\n");
271 smp_initialized
= TRUE
;
282 } processor_start_info_t
;
283 static processor_start_info_t start_info
__attribute__((aligned(64)));
286 * Cache-alignment is to avoid cross-cpu false-sharing interference.
288 static volatile long tsc_entry_barrier
__attribute__((aligned(64)));
289 static volatile long tsc_exit_barrier
__attribute__((aligned(64)));
290 static volatile uint64_t tsc_target
__attribute__((aligned(64)));
293 * Poll a CPU to see when it has marked itself as running.
296 mp_wait_for_cpu_up(int slot_num
, unsigned int iters
, unsigned int usecdelay
)
298 while (iters
-- > 0) {
299 if (cpu_datap(slot_num
)->cpu_running
)
306 * Quickly bring a CPU back online which has been halted.
309 intel_startCPU_fast(int slot_num
)
314 * Try to perform a fast restart
316 rc
= pmCPUExitHalt(slot_num
);
317 if (rc
!= KERN_SUCCESS
)
319 * The CPU was not eligible for a fast restart.
323 KERNEL_DEBUG_CONSTANT(
324 TRACE_MP_CPU_FAST_START
| DBG_FUNC_START
,
325 slot_num
, 0, 0, 0, 0);
328 * Wait until the CPU is back online.
330 mp_disable_preemption();
333 * We use short pauses (1us) for low latency. 30,000 iterations is
334 * longer than a full restart would require so it should be more
338 mp_wait_for_cpu_up(slot_num
, 30000, 1);
339 mp_enable_preemption();
341 KERNEL_DEBUG_CONSTANT(
342 TRACE_MP_CPU_FAST_START
| DBG_FUNC_END
,
343 slot_num
, cpu_datap(slot_num
)->cpu_running
, 0, 0, 0);
346 * Check to make sure that the CPU is really running. If not,
347 * go through the slow path.
349 if (cpu_datap(slot_num
)->cpu_running
)
350 return(KERN_SUCCESS
);
352 return(KERN_FAILURE
);
358 /* Here on the started cpu with cpu_running set TRUE */
360 if (TSC_sync_margin
&&
361 start_info
.target_cpu
== cpu_number()) {
363 * I've just started-up, synchronize again with the starter cpu
364 * and then snap my TSC.
367 atomic_decl(&tsc_entry_barrier
, 1);
368 while (tsc_entry_barrier
!= 0)
369 ; /* spin for starter and target at barrier */
370 tsc_target
= rdtsc64();
371 atomic_decl(&tsc_exit_barrier
, 1);
379 processor_start_info_t
*psip
= (processor_start_info_t
*) arg
;
381 /* Ignore this if the current processor is not the starter */
382 if (cpu_number() != psip
->starter_cpu
)
385 DBG("start_cpu(%p) about to start cpu %d, lapic %d\n",
386 arg
, psip
->target_cpu
, psip
->target_lapic
);
388 KERNEL_DEBUG_CONSTANT(
389 TRACE_MP_CPU_START
| DBG_FUNC_START
,
391 psip
->target_lapic
, 0, 0, 0);
393 i386_start_cpu(psip
->target_lapic
, psip
->target_cpu
);
395 #ifdef POSTCODE_DELAY
396 /* Wait much longer if postcodes are displayed for a delay period. */
399 DBG("start_cpu(%p) about to wait for cpu %d\n",
400 arg
, psip
->target_cpu
);
402 mp_wait_for_cpu_up(psip
->target_cpu
, i
*100, 100);
404 KERNEL_DEBUG_CONSTANT(
405 TRACE_MP_CPU_START
| DBG_FUNC_END
,
407 cpu_datap(psip
->target_cpu
)->cpu_running
, 0, 0, 0);
409 if (TSC_sync_margin
&&
410 cpu_datap(psip
->target_cpu
)->cpu_running
) {
412 * Compare the TSC from the started processor with ours.
413 * Report and log/panic if it diverges by more than
414 * TSC_sync_margin (TSC_SYNC_MARGIN) ticks. This margin
415 * can be overriden by boot-arg (with 0 meaning no checking).
417 uint64_t tsc_starter
;
419 atomic_decl(&tsc_entry_barrier
, 1);
420 while (tsc_entry_barrier
!= 0)
421 ; /* spin for both processors at barrier */
422 tsc_starter
= rdtsc64();
423 atomic_decl(&tsc_exit_barrier
, 1);
424 while (tsc_exit_barrier
!= 0)
425 ; /* spin for target to store its TSC */
426 tsc_delta
= tsc_target
- tsc_starter
;
427 kprintf("TSC sync for cpu %d: 0x%016llx delta 0x%llx (%lld)\n",
428 psip
->target_cpu
, tsc_target
, tsc_delta
, tsc_delta
);
429 if (ABS(tsc_delta
) > (int64_t) TSC_sync_margin
) {
435 "Unsynchronized TSC for cpu %d: "
436 "0x%016llx, delta 0x%llx\n",
437 psip
->target_cpu
, tsc_target
, tsc_delta
);
446 int lapic
= cpu_to_lapic
[slot_num
];
451 DBGLOG_CPU_INIT(slot_num
);
453 DBG("intel_startCPU(%d) lapic_id=%d\n", slot_num
, lapic
);
454 DBG("IdlePTD(%p): 0x%x\n", &IdlePTD
, (int) (uintptr_t)IdlePTD
);
457 * Initialize (or re-initialize) the descriptor tables for this cpu.
458 * Propagate processor mode to slave.
460 cpu_desc_init(cpu_datap(slot_num
));
462 /* Serialize use of the slave boot stack, etc. */
463 lck_mtx_lock(&mp_cpu_boot_lock
);
465 istate
= ml_set_interrupts_enabled(FALSE
);
466 if (slot_num
== get_cpu_number()) {
467 ml_set_interrupts_enabled(istate
);
468 lck_mtx_unlock(&mp_cpu_boot_lock
);
472 start_info
.starter_cpu
= cpu_number();
473 start_info
.target_cpu
= slot_num
;
474 start_info
.target_lapic
= lapic
;
475 tsc_entry_barrier
= 2;
476 tsc_exit_barrier
= 2;
479 * Perform the processor startup sequence with all running
480 * processors rendezvous'ed. This is required during periods when
481 * the cache-disable bit is set for MTRR/PAT initialization.
483 mp_rendezvous_no_intrs(start_cpu
, (void *) &start_info
);
485 start_info
.target_cpu
= 0;
487 ml_set_interrupts_enabled(istate
);
488 lck_mtx_unlock(&mp_cpu_boot_lock
);
490 if (!cpu_datap(slot_num
)->cpu_running
) {
491 kprintf("Failed to start CPU %02d\n", slot_num
);
492 printf("Failed to start CPU %02d, rebooting...\n", slot_num
);
497 kprintf("Started cpu %d (lapic id %08x)\n", slot_num
, lapic
);
503 cpu_signal_event_log_t
*cpu_signal
[MAX_CPUS
];
504 cpu_signal_event_log_t
*cpu_handle
[MAX_CPUS
];
506 MP_EVENT_NAME_DECL();
508 #endif /* MP_DEBUG */
511 * Note: called with NULL state when polling for TLB flush and cross-calls.
514 cpu_signal_handler(x86_saved_state_t
*regs
)
517 #pragma unused (regs)
518 #endif /* !MACH_KDP */
520 volatile int *my_word
;
522 SCHED_STATS_IPI(current_processor());
524 my_cpu
= cpu_number();
525 my_word
= &cpu_data_ptr
[my_cpu
]->cpu_signals
;
526 /* Store the initial set of signals for diagnostics. New
527 * signals could arrive while these are being processed
528 * so it's no more than a hint.
531 cpu_data_ptr
[my_cpu
]->cpu_prior_signals
= *my_word
;
535 if (i_bit(MP_KDP
, my_word
)) {
536 DBGLOG(cpu_handle
,my_cpu
,MP_KDP
);
537 i_bit_clear(MP_KDP
, my_word
);
538 /* Ensure that the i386_kernel_state at the base of the
539 * current thread's stack (if any) is synchronized with the
540 * context at the moment of the interrupt, to facilitate
541 * access through the debugger.
543 sync_iss_to_iks(regs
);
544 if (pmsafe_debug
&& !kdp_snapshot
)
545 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
546 mp_kdp_wait(TRUE
, FALSE
);
547 if (pmsafe_debug
&& !kdp_snapshot
)
548 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_NORMAL
);
550 #endif /* MACH_KDP */
551 if (i_bit(MP_TLB_FLUSH
, my_word
)) {
552 DBGLOG(cpu_handle
,my_cpu
,MP_TLB_FLUSH
);
553 i_bit_clear(MP_TLB_FLUSH
, my_word
);
554 pmap_update_interrupt();
555 } else if (i_bit(MP_CHUD
, my_word
)) {
556 DBGLOG(cpu_handle
,my_cpu
,MP_CHUD
);
557 i_bit_clear(MP_CHUD
, my_word
);
558 chudxnu_cpu_signal_handler();
559 } else if (i_bit(MP_CALL
, my_word
)) {
560 DBGLOG(cpu_handle
,my_cpu
,MP_CALL
);
561 i_bit_clear(MP_CALL
, my_word
);
562 mp_cpus_call_action();
563 } else if (i_bit(MP_CALL_PM
, my_word
)) {
564 DBGLOG(cpu_handle
,my_cpu
,MP_CALL_PM
);
565 i_bit_clear(MP_CALL_PM
, my_word
);
569 /* Called to poll only for cross-calls and TLB flush */
571 } else if (i_bit(MP_AST
, my_word
)) {
572 DBGLOG(cpu_handle
,my_cpu
,MP_AST
);
573 i_bit_clear(MP_AST
, my_word
);
574 ast_check(cpu_to_processor(my_cpu
));
581 extern void kprintf_break_lock(void);
583 NMIInterruptHandler(x86_saved_state_t
*regs
)
587 uint64_t now
= mach_absolute_time();
589 if (panic_active() && !panicDebugging
) {
591 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
596 atomic_incl(&NMIPI_acks
, 1);
597 atomic_incl(&NMI_count
, 1);
598 sync_iss_to_iks_unconditionally(regs
);
599 __asm__
volatile("movq %%rbp, %0" : "=m" (stackptr
));
601 if (cpu_number() == debugger_cpu
)
604 if (NMI_panic_reason
== SPINLOCK_TIMEOUT
) {
605 snprintf(&pstr
[0], sizeof(pstr
),
606 "Panic(CPU %d, time %llu): NMIPI for spinlock acquisition timeout, spinlock: %p, spinlock owner: %p, current_thread: %p, spinlock_owner_cpu: 0x%x\n",
607 cpu_number(), now
, spinlock_timed_out
, (void *) spinlock_timed_out
->interlock
.lock_data
, current_thread(), spinlock_owner_cpu
);
608 panic_i386_backtrace(stackptr
, 64, &pstr
[0], TRUE
, regs
);
609 } else if (NMI_panic_reason
== TLB_FLUSH_TIMEOUT
) {
610 snprintf(&pstr
[0], sizeof(pstr
),
611 "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: TLB flush timeout, TLB state:0x%x\n",
612 cpu_number(), now
, current_cpu_datap()->cpu_tlb_invalid
);
613 panic_i386_backtrace(stackptr
, 48, &pstr
[0], TRUE
, regs
);
614 } else if (NMI_panic_reason
== CROSSCALL_TIMEOUT
) {
615 snprintf(&pstr
[0], sizeof(pstr
),
616 "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: cross-call timeout\n",
618 panic_i386_backtrace(stackptr
, 64, &pstr
[0], TRUE
, regs
);
619 } else if (NMI_panic_reason
== INTERRUPT_WATCHDOG
) {
620 snprintf(&pstr
[0], sizeof(pstr
),
621 "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: interrupt watchdog for vector 0x%x\n",
622 cpu_number(), now
, vector_timed_out
);
623 panic_i386_backtrace(stackptr
, 64, &pstr
[0], TRUE
, regs
);
627 if (pmsafe_debug
&& !kdp_snapshot
)
628 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
629 current_cpu_datap()->cpu_NMI_acknowledged
= TRUE
;
630 i_bit_clear(MP_KDP
, ¤t_cpu_datap()->cpu_signals
);
631 if (panic_active() || NMI_panic_reason
!= NONE
) {
632 mp_kdp_wait(FALSE
, TRUE
);
633 } else if (!mp_kdp_trap
&&
635 virtualized
&& (debug_boot_arg
& DB_NMI
)) {
637 * Under a VMM with the debug boot-arg set, drop into kdp.
638 * Since an NMI is involved, there's a risk of contending with
639 * a panic. And side-effects of NMIs may result in entry into,
640 * and continuing from, the debugger being unreliable.
642 if (__sync_bool_compare_and_swap(&mp_kdp_is_NMI
, FALSE
, TRUE
)) {
643 kprintf_break_lock();
644 kprintf("Debugger entry requested by NMI\n");
645 kdp_i386_trap(T_DEBUG
, saved_state64(regs
), 0, 0);
646 printf("Debugger entry requested by NMI\n");
647 mp_kdp_is_NMI
= FALSE
;
649 mp_kdp_wait(FALSE
, FALSE
);
652 mp_kdp_wait(FALSE
, FALSE
);
654 if (pmsafe_debug
&& !kdp_snapshot
)
655 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_NORMAL
);
663 * cpu_interrupt is really just to be used by the scheduler to
664 * get a CPU's attention it may not always issue an IPI. If an
665 * IPI is always needed then use i386_cpu_IPI.
668 cpu_interrupt(int cpu
)
670 boolean_t did_IPI
= FALSE
;
673 && pmCPUExitIdle(cpu_datap(cpu
))) {
678 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_AST
), cpu
, did_IPI
, 0, 0, 0);
682 * Send a true NMI via the local APIC to the specified CPU.
685 cpu_NMI_interrupt(int cpu
)
687 if (smp_initialized
) {
696 boolean_t intrs_enabled
;
697 uint64_t tsc_timeout
;
699 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
701 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
702 if (!cpu_datap(cpu
)->cpu_running
)
704 cpu_datap(cpu
)->cpu_NMI_acknowledged
= FALSE
;
705 cpu_NMI_interrupt(cpu
);
706 tsc_timeout
= !machine_timeout_suspended() ?
707 rdtsc64() + (1000 * 1000 * 1000 * 10ULL) :
709 while (!cpu_datap(cpu
)->cpu_NMI_acknowledged
) {
710 handle_pending_TLB_flushes();
712 if (rdtsc64() > tsc_timeout
)
713 panic("NMI_cpus() timeout cpu %d", cpu
);
715 cpu_datap(cpu
)->cpu_NMI_acknowledged
= FALSE
;
718 ml_set_interrupts_enabled(intrs_enabled
);
721 static void (* volatile mp_PM_func
)(void) = NULL
;
726 assert(!ml_get_interrupts_enabled());
728 if (mp_PM_func
!= NULL
)
733 cpu_PM_interrupt(int cpu
)
735 assert(!ml_get_interrupts_enabled());
737 if (mp_PM_func
!= NULL
) {
738 if (cpu
== cpu_number())
741 i386_signal_cpu(cpu
, MP_CALL_PM
, ASYNC
);
746 PM_interrupt_register(void (*fn
)(void))
752 i386_signal_cpu(int cpu
, mp_event_t event
, mp_sync_t mode
)
754 volatile int *signals
= &cpu_datap(cpu
)->cpu_signals
;
755 uint64_t tsc_timeout
;
758 if (!cpu_datap(cpu
)->cpu_running
)
761 if (event
== MP_TLB_FLUSH
)
762 KERNEL_DEBUG(TRACE_MP_TLB_FLUSH
| DBG_FUNC_START
, cpu
, 0, 0, 0, 0);
764 DBGLOG(cpu_signal
, cpu
, event
);
766 i_bit_set(event
, signals
);
770 tsc_timeout
= !machine_timeout_suspended() ?
771 rdtsc64() + (1000*1000*1000) :
773 while (i_bit(event
, signals
) && rdtsc64() < tsc_timeout
) {
776 if (i_bit(event
, signals
)) {
777 DBG("i386_signal_cpu(%d, 0x%x, SYNC) timed out\n",
782 if (event
== MP_TLB_FLUSH
)
783 KERNEL_DEBUG(TRACE_MP_TLB_FLUSH
| DBG_FUNC_END
, cpu
, 0, 0, 0, 0);
787 * Helper function called when busy-waiting: panic if too long
788 * a TSC-based time has elapsed since the start of the spin.
791 mp_spin_timeout(uint64_t tsc_start
)
793 uint64_t tsc_timeout
;
796 if (machine_timeout_suspended())
800 * The timeout is 4 * the spinlock timeout period
801 * unless we have serial console printing (kprintf) enabled
802 * in which case we allow an even greater margin.
804 tsc_timeout
= disable_serial_output
? LockTimeOutTSC
<< 2
805 : LockTimeOutTSC
<< 4;
806 return (rdtsc64() > tsc_start
+ tsc_timeout
);
810 * Helper function to take a spinlock while ensuring that incoming IPIs
811 * are still serviced if interrupts are masked while we spin.
812 * Returns current interrupt state.
815 mp_safe_spin_lock(usimple_lock_t lock
)
817 if (ml_get_interrupts_enabled()) {
821 uint64_t tsc_spin_start
= rdtsc64();
822 while (!simple_lock_try(lock
)) {
823 cpu_signal_handler(NULL
);
824 if (mp_spin_timeout(tsc_spin_start
)) {
826 uintptr_t lowner
= (uintptr_t)
827 lock
->interlock
.lock_data
;
828 spinlock_timed_out
= lock
;
829 lock_cpu
= spinlock_timeout_NMI(lowner
);
830 NMIPI_panic(cpu_to_cpumask(lock_cpu
), SPINLOCK_TIMEOUT
);
831 panic("mp_safe_spin_lock() timed out, lock: %p, owner thread: 0x%lx, current_thread: %p, owner on CPU 0x%x, time: %llu",
832 lock
, lowner
, current_thread(), lock_cpu
, mach_absolute_time());
840 * All-CPU rendezvous:
841 * - CPUs are signalled,
842 * - all execute the setup function (if specified),
843 * - rendezvous (i.e. all cpus reach a barrier),
844 * - all execute the action function (if specified),
845 * - rendezvous again,
846 * - execute the teardown function (if specified), and then
849 * Note that the supplied external functions _must_ be reentrant and aware
850 * that they are running in parallel and in an unknown lock context.
854 mp_rendezvous_action(__unused
void *null
)
856 boolean_t intrs_enabled
;
857 uint64_t tsc_spin_start
;
860 if (mp_rv_setup_func
!= NULL
)
861 mp_rv_setup_func(mp_rv_func_arg
);
863 intrs_enabled
= ml_get_interrupts_enabled();
865 /* spin on entry rendezvous */
866 atomic_incl(&mp_rv_entry
, 1);
867 tsc_spin_start
= rdtsc64();
869 while (mp_rv_entry
< mp_rv_ncpus
) {
870 /* poll for pesky tlb flushes if interrupts disabled */
872 handle_pending_TLB_flushes();
873 if (mp_spin_timeout(tsc_spin_start
)) {
874 panic("mp_rv_action() entry: %ld of %d responses, start: 0x%llx, cur: 0x%llx", mp_rv_entry
, mp_rv_ncpus
, tsc_spin_start
, rdtsc64());
878 /* action function */
879 if (mp_rv_action_func
!= NULL
)
880 mp_rv_action_func(mp_rv_func_arg
);
882 /* spin on exit rendezvous */
883 atomic_incl(&mp_rv_exit
, 1);
884 tsc_spin_start
= rdtsc64();
885 while (mp_rv_exit
< mp_rv_ncpus
) {
887 handle_pending_TLB_flushes();
888 if (mp_spin_timeout(tsc_spin_start
))
889 panic("mp_rv_action() exit: %ld of %d responses, start: 0x%llx, cur: 0x%llx", mp_rv_exit
, mp_rv_ncpus
, tsc_spin_start
, rdtsc64());
892 /* teardown function */
893 if (mp_rv_teardown_func
!= NULL
)
894 mp_rv_teardown_func(mp_rv_func_arg
);
896 /* Bump completion count */
897 atomic_incl(&mp_rv_complete
, 1);
901 mp_rendezvous(void (*setup_func
)(void *),
902 void (*action_func
)(void *),
903 void (*teardown_func
)(void *),
906 uint64_t tsc_spin_start
;
908 if (!smp_initialized
) {
909 if (setup_func
!= NULL
)
911 if (action_func
!= NULL
)
913 if (teardown_func
!= NULL
)
918 /* obtain rendezvous lock */
919 (void) mp_safe_spin_lock(&mp_rv_lock
);
921 /* set static function pointers */
922 mp_rv_setup_func
= setup_func
;
923 mp_rv_action_func
= action_func
;
924 mp_rv_teardown_func
= teardown_func
;
925 mp_rv_func_arg
= arg
;
932 * signal other processors, which will call mp_rendezvous_action()
933 * with interrupts disabled
935 mp_rv_ncpus
= mp_cpus_call(CPUMASK_OTHERS
, NOSYNC
, &mp_rendezvous_action
, NULL
) + 1;
937 /* call executor function on this cpu */
938 mp_rendezvous_action(NULL
);
941 * Spin for everyone to complete.
942 * This is necessary to ensure that all processors have proceeded
943 * from the exit barrier before we release the rendezvous structure.
945 tsc_spin_start
= rdtsc64();
946 while (mp_rv_complete
< mp_rv_ncpus
) {
947 if (mp_spin_timeout(tsc_spin_start
))
948 panic("mp_rendezvous() timeout: %ld of %d responses, start: 0x%llx, cur: 0x%llx", mp_rv_complete
, mp_rv_ncpus
, tsc_spin_start
, rdtsc64());
952 mp_rv_setup_func
= NULL
;
953 mp_rv_action_func
= NULL
;
954 mp_rv_teardown_func
= NULL
;
955 mp_rv_func_arg
= NULL
;
958 simple_unlock(&mp_rv_lock
);
962 mp_rendezvous_break_lock(void)
964 simple_lock_init(&mp_rv_lock
, 0);
968 setup_disable_intrs(__unused
void * param_not_used
)
970 /* disable interrupts before the first barrier */
971 boolean_t intr
= ml_set_interrupts_enabled(FALSE
);
973 current_cpu_datap()->cpu_iflag
= intr
;
974 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__
);
978 teardown_restore_intrs(__unused
void * param_not_used
)
980 /* restore interrupt flag following MTRR changes */
981 ml_set_interrupts_enabled(current_cpu_datap()->cpu_iflag
);
982 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__
);
986 * A wrapper to mp_rendezvous() to call action_func() with interrupts disabled.
987 * This is exported for use by kexts.
990 mp_rendezvous_no_intrs(
991 void (*action_func
)(void *),
994 mp_rendezvous(setup_disable_intrs
,
996 teardown_restore_intrs
,
1002 queue_chain_t link
; /* queue linkage */
1003 void (*func
)(void *,void *); /* routine to call */
1004 void *arg0
; /* routine's 1st arg */
1005 void *arg1
; /* routine's 2nd arg */
1006 cpumask_t
*maskp
; /* completion response mask */
1012 decl_simple_lock_data(, lock
);
1014 #define MP_CPUS_CALL_BUFS_PER_CPU MAX_CPUS
1015 static mp_call_queue_t mp_cpus_call_freelist
;
1016 static mp_call_queue_t mp_cpus_call_head
[MAX_CPUS
];
1018 static inline boolean_t
1019 mp_call_head_lock(mp_call_queue_t
*cqp
)
1021 boolean_t intrs_enabled
;
1023 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
1024 simple_lock(&cqp
->lock
);
1026 return intrs_enabled
;
1030 * Deliver an NMIPI to a set of processors to cause them to panic .
1033 NMIPI_panic(cpumask_t cpu_mask
, NMI_reason_t why
) {
1034 unsigned int cpu
, cpu_bit
;
1037 NMI_panic_reason
= why
;
1039 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
1040 if ((cpu_mask
& cpu_bit
) == 0)
1042 cpu_datap(cpu
)->cpu_NMI_acknowledged
= FALSE
;
1043 cpu_NMI_interrupt(cpu
);
1046 /* Wait (only so long) for NMi'ed cpus to respond */
1047 deadline
= mach_absolute_time() + LockTimeOut
;
1048 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
1049 if ((cpu_mask
& cpu_bit
) == 0)
1051 while (!cpu_datap(cpu
)->cpu_NMI_acknowledged
&&
1052 mach_absolute_time() < deadline
) {
1059 static inline boolean_t
1060 mp_call_head_is_locked(mp_call_queue_t
*cqp
)
1062 return !ml_get_interrupts_enabled() &&
1063 hw_lock_held((hw_lock_t
)&cqp
->lock
);
1068 mp_call_head_unlock(mp_call_queue_t
*cqp
, boolean_t intrs_enabled
)
1070 simple_unlock(&cqp
->lock
);
1071 ml_set_interrupts_enabled(intrs_enabled
);
1074 static inline mp_call_t
*
1077 mp_call_t
*callp
= NULL
;
1078 boolean_t intrs_enabled
;
1079 mp_call_queue_t
*cqp
= &mp_cpus_call_freelist
;
1081 intrs_enabled
= mp_call_head_lock(cqp
);
1082 if (!queue_empty(&cqp
->queue
))
1083 queue_remove_first(&cqp
->queue
, callp
, typeof(callp
), link
);
1084 mp_call_head_unlock(cqp
, intrs_enabled
);
1090 mp_call_free(mp_call_t
*callp
)
1092 boolean_t intrs_enabled
;
1093 mp_call_queue_t
*cqp
= &mp_cpus_call_freelist
;
1095 intrs_enabled
= mp_call_head_lock(cqp
);
1096 queue_enter_first(&cqp
->queue
, callp
, typeof(callp
), link
);
1097 mp_call_head_unlock(cqp
, intrs_enabled
);
1100 static inline mp_call_t
*
1101 mp_call_dequeue_locked(mp_call_queue_t
*cqp
)
1103 mp_call_t
*callp
= NULL
;
1105 assert(mp_call_head_is_locked(cqp
));
1106 if (!queue_empty(&cqp
->queue
))
1107 queue_remove_first(&cqp
->queue
, callp
, typeof(callp
), link
);
1112 mp_call_enqueue_locked(
1113 mp_call_queue_t
*cqp
,
1116 queue_enter(&cqp
->queue
, callp
, typeof(callp
), link
);
1119 /* Called on the boot processor to initialize global structures */
1121 mp_cpus_call_init(void)
1123 mp_call_queue_t
*cqp
= &mp_cpus_call_freelist
;
1125 DBG("mp_cpus_call_init()\n");
1126 simple_lock_init(&cqp
->lock
, 0);
1127 queue_init(&cqp
->queue
);
1131 * Called at processor registration to add call buffers to the free list
1132 * and to initialize the per-cpu call queue.
1135 mp_cpus_call_cpu_init(int cpu
)
1138 mp_call_queue_t
*cqp
= &mp_cpus_call_head
[cpu
];
1141 simple_lock_init(&cqp
->lock
, 0);
1142 queue_init(&cqp
->queue
);
1143 for (i
= 0; i
< MP_CPUS_CALL_BUFS_PER_CPU
; i
++) {
1144 callp
= (mp_call_t
*) kalloc(sizeof(mp_call_t
));
1145 mp_call_free(callp
);
1148 DBG("mp_cpus_call_init(%d) done\n", cpu
);
1152 * This is called from cpu_signal_handler() to process an MP_CALL signal.
1153 * And also from i386_deactivate_cpu() when a cpu is being taken offline.
1156 mp_cpus_call_action(void)
1158 mp_call_queue_t
*cqp
;
1159 boolean_t intrs_enabled
;
1163 assert(!ml_get_interrupts_enabled());
1164 cqp
= &mp_cpus_call_head
[cpu_number()];
1165 intrs_enabled
= mp_call_head_lock(cqp
);
1166 while ((callp
= mp_call_dequeue_locked(cqp
)) != NULL
) {
1167 /* Copy call request to the stack to free buffer */
1169 mp_call_free(callp
);
1170 if (call
.func
!= NULL
) {
1171 mp_call_head_unlock(cqp
, intrs_enabled
);
1172 KERNEL_DEBUG_CONSTANT(
1173 TRACE_MP_CPUS_CALL_ACTION
,
1174 VM_KERNEL_UNSLIDE(call
.func
), VM_KERNEL_UNSLIDE_OR_PERM(call
.arg0
),
1175 VM_KERNEL_UNSLIDE_OR_PERM(call
.arg1
), VM_KERNEL_ADDRPERM(call
.maskp
), 0);
1176 call
.func(call
.arg0
, call
.arg1
);
1177 (void) mp_call_head_lock(cqp
);
1179 if (call
.maskp
!= NULL
)
1180 i_bit_set(cpu_number(), call
.maskp
);
1182 mp_call_head_unlock(cqp
, intrs_enabled
);
1186 * mp_cpus_call() runs a given function on cpus specified in a given cpu mask.
1187 * Possible modes are:
1188 * SYNC: function is called serially on target cpus in logical cpu order
1189 * waiting for each call to be acknowledged before proceeding
1190 * ASYNC: function call is queued to the specified cpus
1191 * waiting for all calls to complete in parallel before returning
1192 * NOSYNC: function calls are queued
1193 * but we return before confirmation of calls completing.
1194 * The action function may be NULL.
1195 * The cpu mask may include the local cpu. Offline cpus are ignored.
1196 * The return value is the number of cpus on which the call was made or queued.
1202 void (*action_func
)(void *),
1205 return mp_cpus_call1(
1208 (void (*)(void *,void *))action_func
,
1215 mp_cpus_call_wait(boolean_t intrs_enabled
,
1216 cpumask_t cpus_called
,
1217 cpumask_t
*cpus_responded
)
1219 mp_call_queue_t
*cqp
;
1220 uint64_t tsc_spin_start
;
1222 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
1223 cqp
= &mp_cpus_call_head
[cpu_number()];
1225 tsc_spin_start
= rdtsc64();
1226 while (*cpus_responded
!= cpus_called
) {
1227 if (!intrs_enabled
) {
1228 /* Sniffing w/o locking */
1229 if (!queue_empty(&cqp
->queue
))
1230 mp_cpus_call_action();
1231 cpu_signal_handler(NULL
);
1233 if (mp_spin_timeout(tsc_spin_start
)) {
1234 cpumask_t cpus_unresponsive
;
1236 cpus_unresponsive
= cpus_called
& ~(*cpus_responded
);
1237 NMIPI_panic(cpus_unresponsive
, CROSSCALL_TIMEOUT
);
1238 panic("mp_cpus_call_wait() timeout, cpus: 0x%llx",
1248 void (*action_func
)(void *, void *),
1251 cpumask_t
*cpus_calledp
)
1254 boolean_t intrs_enabled
= FALSE
;
1255 boolean_t call_self
= FALSE
;
1256 cpumask_t cpus_called
= 0;
1257 cpumask_t cpus_responded
= 0;
1258 long cpus_call_count
= 0;
1259 uint64_t tsc_spin_start
;
1260 boolean_t topo_lock
;
1262 KERNEL_DEBUG_CONSTANT(
1263 TRACE_MP_CPUS_CALL
| DBG_FUNC_START
,
1264 cpus
, mode
, VM_KERNEL_UNSLIDE(action_func
), VM_KERNEL_UNSLIDE_OR_PERM(arg0
), VM_KERNEL_UNSLIDE_OR_PERM(arg1
));
1266 if (!smp_initialized
) {
1267 if ((cpus
& CPUMASK_SELF
) == 0)
1269 if (action_func
!= NULL
) {
1270 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
1271 action_func(arg0
, arg1
);
1272 ml_set_interrupts_enabled(intrs_enabled
);
1279 * Queue the call for each non-local requested cpu.
1280 * This is performed under the topo lock to prevent changes to
1281 * cpus online state and to prevent concurrent rendezvouses --
1282 * although an exception is made if we're calling only the master
1283 * processor since that always remains active. Note: this exception
1284 * is expected for longterm timer nosync cross-calls to the master cpu.
1286 mp_disable_preemption();
1287 intrs_enabled
= ml_get_interrupts_enabled();
1288 topo_lock
= (cpus
!= cpu_to_cpumask(master_cpu
));
1290 ml_set_interrupts_enabled(FALSE
);
1291 (void) mp_safe_spin_lock(&x86_topo_lock
);
1293 for (cpu
= 0; cpu
< (cpu_t
) real_ncpus
; cpu
++) {
1294 if (((cpu_to_cpumask(cpu
) & cpus
) == 0) ||
1295 !cpu_datap(cpu
)->cpu_running
)
1297 tsc_spin_start
= rdtsc64();
1298 if (cpu
== (cpu_t
) cpu_number()) {
1300 * We don't IPI ourself and if calling asynchronously,
1301 * we defer our call until we have signalled all others.
1304 if (mode
== SYNC
&& action_func
!= NULL
) {
1305 KERNEL_DEBUG_CONSTANT(
1306 TRACE_MP_CPUS_CALL_LOCAL
,
1307 VM_KERNEL_UNSLIDE(action_func
),
1308 VM_KERNEL_UNSLIDE_OR_PERM(arg0
), VM_KERNEL_UNSLIDE_OR_PERM(arg1
), 0, 0);
1309 action_func(arg0
, arg1
);
1313 * Here to queue a call to cpu and IPI.
1315 mp_call_t
*callp
= NULL
;
1316 mp_call_queue_t
*cqp
= &mp_cpus_call_head
[cpu
];
1317 boolean_t intrs_inner
;
1321 callp
= mp_call_alloc();
1322 intrs_inner
= mp_call_head_lock(cqp
);
1323 if (callp
== NULL
) {
1324 mp_call_head_unlock(cqp
, intrs_inner
);
1325 KERNEL_DEBUG_CONSTANT(
1326 TRACE_MP_CPUS_CALL_NOBUF
,
1329 /* Sniffing w/o locking */
1330 if (!queue_empty(&cqp
->queue
))
1331 mp_cpus_call_action();
1332 handle_pending_TLB_flushes();
1334 if (mp_spin_timeout(tsc_spin_start
))
1335 panic("mp_cpus_call1() timeout start: 0x%llx, cur: 0x%llx",
1336 tsc_spin_start
, rdtsc64());
1339 callp
->maskp
= (mode
== NOSYNC
) ? NULL
: &cpus_responded
;
1340 callp
->func
= action_func
;
1343 mp_call_enqueue_locked(cqp
, callp
);
1345 cpus_called
|= cpu_to_cpumask(cpu
);
1346 i386_signal_cpu(cpu
, MP_CALL
, ASYNC
);
1347 mp_call_head_unlock(cqp
, intrs_inner
);
1349 mp_cpus_call_wait(intrs_inner
, cpus_called
, &cpus_responded
);
1354 simple_unlock(&x86_topo_lock
);
1355 ml_set_interrupts_enabled(intrs_enabled
);
1358 /* Call locally if mode not SYNC */
1359 if (mode
!= SYNC
&& call_self
) {
1360 KERNEL_DEBUG_CONSTANT(
1361 TRACE_MP_CPUS_CALL_LOCAL
,
1362 VM_KERNEL_UNSLIDE(action_func
), VM_KERNEL_UNSLIDE_OR_PERM(arg0
), VM_KERNEL_UNSLIDE_OR_PERM(arg1
), 0, 0);
1363 if (action_func
!= NULL
) {
1364 ml_set_interrupts_enabled(FALSE
);
1365 action_func(arg0
, arg1
);
1366 ml_set_interrupts_enabled(intrs_enabled
);
1370 /* For ASYNC, now wait for all signaled cpus to complete their calls */
1372 mp_cpus_call_wait(intrs_enabled
, cpus_called
, &cpus_responded
);
1374 /* Safe to allow pre-emption now */
1375 mp_enable_preemption();
1379 cpus_called
|= cpu_to_cpumask(cpu
);
1384 *cpus_calledp
= cpus_called
;
1386 KERNEL_DEBUG_CONSTANT(
1387 TRACE_MP_CPUS_CALL
| DBG_FUNC_END
,
1388 cpus_call_count
, cpus_called
, 0, 0, 0);
1390 return (cpu_t
) cpus_call_count
;
1395 mp_broadcast_action(__unused
void *null
)
1397 /* call action function */
1398 if (mp_bc_action_func
!= NULL
)
1399 mp_bc_action_func(mp_bc_func_arg
);
1401 /* if we're the last one through, wake up the instigator */
1402 if (atomic_decl_and_test(&mp_bc_count
, 1))
1403 thread_wakeup(((event_t
)(uintptr_t) &mp_bc_count
));
1407 * mp_broadcast() runs a given function on all active cpus.
1408 * The caller blocks until the functions has run on all cpus.
1409 * The caller will also block if there is another pending braodcast.
1413 void (*action_func
)(void *),
1416 if (!smp_initialized
) {
1417 if (action_func
!= NULL
)
1422 /* obtain broadcast lock */
1423 lck_mtx_lock(&mp_bc_lock
);
1425 /* set static function pointers */
1426 mp_bc_action_func
= action_func
;
1427 mp_bc_func_arg
= arg
;
1429 assert_wait((event_t
)(uintptr_t)&mp_bc_count
, THREAD_UNINT
);
1432 * signal other processors, which will call mp_broadcast_action()
1434 mp_bc_count
= real_ncpus
; /* assume max possible active */
1435 mp_bc_ncpus
= mp_cpus_call(CPUMASK_OTHERS
, NOSYNC
, *mp_broadcast_action
, NULL
) + 1;
1436 atomic_decl(&mp_bc_count
, real_ncpus
- mp_bc_ncpus
); /* subtract inactive */
1438 /* call executor function on this cpu */
1439 mp_broadcast_action(NULL
);
1441 /* block for other cpus to have run action_func */
1442 if (mp_bc_ncpus
> 1)
1443 thread_block(THREAD_CONTINUE_NULL
);
1445 clear_wait(current_thread(), THREAD_AWAKENED
);
1448 lck_mtx_unlock(&mp_bc_lock
);
1452 mp_cpus_kick(cpumask_t cpus
)
1455 boolean_t intrs_enabled
= FALSE
;
1457 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
1458 mp_safe_spin_lock(&x86_topo_lock
);
1460 for (cpu
= 0; cpu
< (cpu_t
) real_ncpus
; cpu
++) {
1461 if ((cpu
== (cpu_t
) cpu_number())
1462 || ((cpu_to_cpumask(cpu
) & cpus
) == 0)
1463 || (!cpu_datap(cpu
)->cpu_running
))
1468 lapic_send_ipi(cpu
, LAPIC_VECTOR(KICK
));
1471 simple_unlock(&x86_topo_lock
);
1472 ml_set_interrupts_enabled(intrs_enabled
);
1476 i386_activate_cpu(void)
1478 cpu_data_t
*cdp
= current_cpu_datap();
1480 assert(!ml_get_interrupts_enabled());
1482 if (!smp_initialized
) {
1483 cdp
->cpu_running
= TRUE
;
1487 mp_safe_spin_lock(&x86_topo_lock
);
1488 cdp
->cpu_running
= TRUE
;
1490 simple_unlock(&x86_topo_lock
);
1495 i386_deactivate_cpu(void)
1497 cpu_data_t
*cdp
= current_cpu_datap();
1499 assert(!ml_get_interrupts_enabled());
1501 KERNEL_DEBUG_CONSTANT(
1502 TRACE_MP_CPU_DEACTIVATE
| DBG_FUNC_START
,
1505 mp_safe_spin_lock(&x86_topo_lock
);
1506 cdp
->cpu_running
= FALSE
;
1507 simple_unlock(&x86_topo_lock
);
1510 * Move all of this cpu's timers to the master/boot cpu,
1511 * and poke it in case there's a sooner deadline for it to schedule.
1513 timer_queue_shutdown(&cdp
->rtclock_timer
.queue
);
1514 mp_cpus_call(cpu_to_cpumask(master_cpu
), ASYNC
, timer_queue_expire_local
, NULL
);
1518 #endif /* MONOTONIC */
1521 * Open an interrupt window
1522 * and ensure any pending IPI or timer is serviced
1524 mp_disable_preemption();
1525 ml_set_interrupts_enabled(TRUE
);
1527 while (cdp
->cpu_signals
&& x86_lcpu()->rtcDeadline
!= EndOfAllTime
)
1530 * Ensure there's no remaining timer deadline set
1531 * - AICPM may have left one active.
1535 ml_set_interrupts_enabled(FALSE
);
1536 mp_enable_preemption();
1538 KERNEL_DEBUG_CONSTANT(
1539 TRACE_MP_CPU_DEACTIVATE
| DBG_FUNC_END
,
1543 int pmsafe_debug
= 1;
1546 volatile boolean_t mp_kdp_trap
= FALSE
;
1547 volatile boolean_t mp_kdp_is_NMI
= FALSE
;
1548 volatile unsigned long mp_kdp_ncpus
;
1549 boolean_t mp_kdp_state
;
1553 mp_kdp_enter(boolean_t proceed_on_failure
)
1556 unsigned int ncpus
= 0;
1557 unsigned int my_cpu
;
1558 uint64_t tsc_timeout
;
1560 DBG("mp_kdp_enter()\n");
1563 * Here to enter the debugger.
1564 * In case of races, only one cpu is allowed to enter kdp after
1567 mp_kdp_state
= ml_set_interrupts_enabled(FALSE
);
1568 my_cpu
= cpu_number();
1570 if (my_cpu
== (unsigned) debugger_cpu
) {
1571 kprintf("\n\nRECURSIVE DEBUGGER ENTRY DETECTED\n\n");
1576 uint64_t start_time
= cpu_datap(my_cpu
)->debugger_entry_time
= mach_absolute_time();
1578 while (!locked
|| mp_kdp_trap
) {
1580 simple_unlock(&x86_topo_lock
);
1582 if (proceed_on_failure
) {
1583 if (mach_absolute_time() - start_time
> 500000000ll) {
1584 kprintf("mp_kdp_enter() can't get x86_topo_lock! Debugging anyway! #YOLO\n");
1587 locked
= simple_lock_try(&x86_topo_lock
);
1592 mp_safe_spin_lock(&x86_topo_lock
);
1596 if (locked
&& mp_kdp_trap
) {
1597 simple_unlock(&x86_topo_lock
);
1598 DBG("mp_kdp_enter() race lost\n");
1600 mp_kdp_wait(TRUE
, FALSE
);
1606 if (pmsafe_debug
&& !kdp_snapshot
)
1607 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
1609 debugger_cpu
= my_cpu
;
1611 atomic_incl((volatile long *)&mp_kdp_ncpus
, 1);
1613 debugger_entry_time
= cpu_datap(my_cpu
)->debugger_entry_time
;
1616 * Deliver a nudge to other cpus, counting how many
1618 DBG("mp_kdp_enter() signaling other processors\n");
1619 if (force_immediate_debugger_NMI
== FALSE
) {
1620 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1621 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1624 i386_signal_cpu(cpu
, MP_KDP
, ASYNC
);
1627 * Wait other processors to synchronize
1629 DBG("mp_kdp_enter() waiting for (%d) processors to suspend\n", ncpus
);
1632 * This timeout is rather arbitrary; we don't want to NMI
1633 * processors that are executing at potentially
1634 * "unsafe-to-interrupt" points such as the trampolines,
1635 * but neither do we want to lose state by waiting too long.
1637 tsc_timeout
= rdtsc64() + (LockTimeOutTSC
);
1639 while (mp_kdp_ncpus
!= ncpus
&& rdtsc64() < tsc_timeout
) {
1641 * A TLB shootdown request may be pending--this would
1642 * result in the requesting processor waiting in
1643 * PMAP_UPDATE_TLBS() until this processor deals with it.
1644 * Process it, so it can now enter mp_kdp_wait()
1646 handle_pending_TLB_flushes();
1649 /* If we've timed out, and some processor(s) are still unresponsive,
1650 * interrupt them with an NMI via the local APIC.
1652 if (mp_kdp_ncpus
!= ncpus
) {
1653 DBG("mp_kdp_enter() timed-out on cpu %d, NMI-ing\n", my_cpu
);
1654 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1655 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1657 if (cpu_signal_pending(cpu
, MP_KDP
))
1658 cpu_NMI_interrupt(cpu
);
1660 /* Wait again for the same timeout */
1661 tsc_timeout
= rdtsc64() + (LockTimeOutTSC
);
1662 while (mp_kdp_ncpus
!= ncpus
&& rdtsc64() < tsc_timeout
) {
1663 handle_pending_TLB_flushes();
1666 if (mp_kdp_ncpus
!= ncpus
) {
1667 panic("mp_kdp_enter() timed-out waiting after NMI");
1672 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1673 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1675 cpu_NMI_interrupt(cpu
);
1679 simple_unlock(&x86_topo_lock
);
1682 DBG("mp_kdp_enter() %d processors done %s\n",
1683 (int)mp_kdp_ncpus
, (mp_kdp_ncpus
== ncpus
) ? "OK" : "timed out");
1685 postcode(MP_KDP_ENTER
);
1689 cpu_signal_pending(int cpu
, mp_event_t event
)
1691 volatile int *signals
= &cpu_datap(cpu
)->cpu_signals
;
1692 boolean_t retval
= FALSE
;
1694 if (i_bit(event
, signals
))
1699 long kdp_x86_xcpu_invoke(const uint16_t lcpu
, kdp_x86_xcpu_func_t func
,
1700 void *arg0
, void *arg1
)
1702 if (lcpu
> (real_ncpus
- 1))
1708 kdp_xcpu_call_func
.func
= func
;
1709 kdp_xcpu_call_func
.ret
= -1;
1710 kdp_xcpu_call_func
.arg0
= arg0
;
1711 kdp_xcpu_call_func
.arg1
= arg1
;
1712 kdp_xcpu_call_func
.cpu
= lcpu
;
1713 DBG("Invoking function %p on CPU %d\n", func
, (int32_t)lcpu
);
1714 while (kdp_xcpu_call_func
.cpu
!= KDP_XCPU_NONE
)
1716 return kdp_xcpu_call_func
.ret
;
1720 kdp_x86_xcpu_poll(void)
1722 if ((uint16_t)cpu_number() == kdp_xcpu_call_func
.cpu
) {
1723 kdp_xcpu_call_func
.ret
=
1724 kdp_xcpu_call_func
.func(kdp_xcpu_call_func
.arg0
,
1725 kdp_xcpu_call_func
.arg1
,
1727 kdp_xcpu_call_func
.cpu
= KDP_XCPU_NONE
;
1732 mp_kdp_wait(boolean_t flush
, boolean_t isNMI
)
1734 DBG("mp_kdp_wait()\n");
1736 current_cpu_datap()->debugger_ipi_time
= mach_absolute_time();
1738 /* If we've trapped due to a machine-check, save MCA registers */
1742 atomic_incl((volatile long *)&mp_kdp_ncpus
, 1);
1743 while (mp_kdp_trap
|| (isNMI
== TRUE
)) {
1745 * A TLB shootdown request may be pending--this would result
1746 * in the requesting processor waiting in PMAP_UPDATE_TLBS()
1747 * until this processor handles it.
1748 * Process it, so it can now enter mp_kdp_wait()
1751 handle_pending_TLB_flushes();
1753 kdp_x86_xcpu_poll();
1757 atomic_decl((volatile long *)&mp_kdp_ncpus
, 1);
1758 DBG("mp_kdp_wait() done\n");
1764 DBG("mp_kdp_exit()\n");
1766 atomic_decl((volatile long *)&mp_kdp_ncpus
, 1);
1768 debugger_exit_time
= mach_absolute_time();
1770 mp_kdp_trap
= FALSE
;
1773 /* Wait other processors to stop spinning. XXX needs timeout */
1774 DBG("mp_kdp_exit() waiting for processors to resume\n");
1775 while (mp_kdp_ncpus
> 0) {
1777 * a TLB shootdown request may be pending... this would result in the requesting
1778 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1779 * Process it, so it can now enter mp_kdp_wait()
1781 handle_pending_TLB_flushes();
1786 if (pmsafe_debug
&& !kdp_snapshot
)
1787 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_NORMAL
);
1789 debugger_exit_time
= mach_absolute_time();
1791 DBG("mp_kdp_exit() done\n");
1792 (void) ml_set_interrupts_enabled(mp_kdp_state
);
1793 postcode(MP_KDP_EXIT
);
1796 #endif /* MACH_KDP */
1799 mp_recent_debugger_activity(void) {
1800 uint64_t abstime
= mach_absolute_time();
1801 return (((abstime
- debugger_entry_time
) < LastDebuggerEntryAllowance
) ||
1802 ((abstime
- debugger_exit_time
) < LastDebuggerEntryAllowance
));
1808 __unused processor_t processor
)
1814 processor_t processor
)
1816 int cpu
= processor
->cpu_id
;
1818 if (cpu
!= cpu_number()) {
1819 i386_signal_cpu(cpu
, MP_AST
, ASYNC
);
1820 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_AST
), cpu
, 1, 0, 0, 0);
1825 slave_machine_init(void *param
)
1828 * Here in process context, but with interrupts disabled.
1830 DBG("slave_machine_init() CPU%d\n", get_cpu_number());
1832 if (param
== FULL_SLAVE_INIT
) {
1838 cpu_machine_init(); /* Interrupts enabled hereafter */
1842 int cpu_number(void)
1844 return get_cpu_number();
1852 simple_lock_init(&cpu_warm_lock
, 0);
1853 queue_init(&cpu_warm_call_list
);
1854 for (i
= 0; i
< NUM_CPU_WARM_CALLS
; i
++) {
1855 enqueue_head(&cpu_warm_call_list
, (queue_entry_t
)&cpu_warm_call_arr
[i
]);
1860 grab_warm_timer_call()
1863 timer_call_t call
= NULL
;
1866 simple_lock(&cpu_warm_lock
);
1867 if (!queue_empty(&cpu_warm_call_list
)) {
1868 call
= (timer_call_t
) dequeue_head(&cpu_warm_call_list
);
1870 simple_unlock(&cpu_warm_lock
);
1877 free_warm_timer_call(timer_call_t call
)
1882 simple_lock(&cpu_warm_lock
);
1883 enqueue_head(&cpu_warm_call_list
, (queue_entry_t
)call
);
1884 simple_unlock(&cpu_warm_lock
);
1889 * Runs in timer call context (interrupts disabled).
1892 cpu_warm_timer_call_func(
1893 call_entry_param_t p0
,
1894 __unused call_entry_param_t p1
)
1896 free_warm_timer_call((timer_call_t
)p0
);
1901 * Runs with interrupts disabled on the CPU we wish to warm (i.e. CPU 0).
1907 cpu_warm_data_t cwdp
= (cpu_warm_data_t
)arg
;
1909 timer_call_enter(cwdp
->cwd_call
, cwdp
->cwd_deadline
, TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
1910 cwdp
->cwd_result
= 0;
1916 * Not safe to call with interrupts disabled.
1919 ml_interrupt_prewarm(
1922 struct cpu_warm_data cwd
;
1926 if (ml_get_interrupts_enabled() == FALSE
) {
1927 panic("%s: Interrupts disabled?\n", __FUNCTION__
);
1931 * If the platform doesn't need our help, say that we succeeded.
1933 if (!ml_get_interrupt_prewake_applicable()) {
1934 return KERN_SUCCESS
;
1938 * Grab a timer call to use.
1940 call
= grab_warm_timer_call();
1942 return KERN_RESOURCE_SHORTAGE
;
1945 timer_call_setup(call
, cpu_warm_timer_call_func
, call
);
1946 cwd
.cwd_call
= call
;
1947 cwd
.cwd_deadline
= deadline
;
1951 * For now, non-local interrupts happen on the master processor.
1953 ct
= mp_cpus_call(cpu_to_cpumask(master_cpu
), SYNC
, _cpu_warm_setup
, &cwd
);
1955 free_warm_timer_call(call
);
1956 return KERN_FAILURE
;
1958 return cwd
.cwd_result
;
1962 #if DEBUG || DEVELOPMENT
1964 kernel_spin(uint64_t spin_ns
)
1971 kprintf("kernel_spin(%llu) spinning uninterruptibly\n", spin_ns
);
1972 istate
= ml_set_interrupts_enabled(FALSE
);
1973 cdp
= current_cpu_datap();
1974 nanoseconds_to_absolutetime(spin_ns
, &spin_abs
);
1976 /* Fake interrupt handler entry for testing mp_interrupt_watchdog() */
1977 cdp
->cpu_int_event_time
= mach_absolute_time();
1978 cdp
->cpu_int_state
= (void *) USER_STATE(current_thread());
1980 deadline
= mach_absolute_time() + spin_ns
;
1981 while (mach_absolute_time() < deadline
)
1984 cdp
->cpu_int_event_time
= 0;
1985 cdp
->cpu_int_state
= NULL
;
1987 ml_set_interrupts_enabled(istate
);
1988 kprintf("kernel_spin() continuing\n");
1992 * Called from the scheduler's maintenance thread,
1993 * scan running processors for long-running ISRs and:
1994 * - panic if longer than LockTimeOut, or
1995 * - log if more than a quantum.
1998 mp_interrupt_watchdog(void)
2001 boolean_t intrs_enabled
= FALSE
;
2002 uint16_t cpu_int_num
;
2003 uint64_t cpu_int_event_time
;
2005 uint64_t cpu_int_duration
;
2007 x86_saved_state_t
*cpu_int_state
;
2009 if (__improbable(!mp_interrupt_watchdog_enabled
))
2012 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
2013 now
= mach_absolute_time();
2015 * While timeouts are not suspended,
2016 * check all other processors for long outstanding interrupt handling.
2019 cpu
< (cpu_t
) real_ncpus
&& !machine_timeout_suspended();
2021 if ((cpu
== (cpu_t
) cpu_number()) ||
2022 (!cpu_datap(cpu
)->cpu_running
))
2024 cpu_int_event_time
= cpu_datap(cpu
)->cpu_int_event_time
;
2025 if (cpu_int_event_time
== 0)
2027 if (__improbable(now
< cpu_int_event_time
))
2028 continue; /* skip due to inter-processor skew */
2029 cpu_int_state
= cpu_datap(cpu
)->cpu_int_state
;
2030 if (__improbable(cpu_int_state
== NULL
))
2031 /* The interrupt may have been dismissed */
2034 /* Here with a cpu handling an interrupt */
2036 cpu_int_duration
= now
- cpu_int_event_time
;
2037 if (__improbable(cpu_int_duration
> LockTimeOut
)) {
2038 cpu_int_num
= saved_state64(cpu_int_state
)->isf
.trapno
;
2039 cpu_rip
= saved_state64(cpu_int_state
)->isf
.rip
;
2040 vector_timed_out
= cpu_int_num
;
2041 NMIPI_panic(cpu_to_cpumask(cpu
), INTERRUPT_WATCHDOG
);
2042 panic("Interrupt watchdog, "
2043 "cpu: %d interrupt: 0x%x time: %llu..%llu state: %p RIP: 0x%llx",
2044 cpu
, cpu_int_num
, cpu_int_event_time
, now
, cpu_int_state
, cpu_rip
);
2046 } else if (__improbable(cpu_int_duration
> (uint64_t) std_quantum
)) {
2047 mp_interrupt_watchdog_events
++;
2048 cpu_int_num
= saved_state64(cpu_int_state
)->isf
.trapno
;
2049 cpu_rip
= saved_state64(cpu_int_state
)->isf
.rip
;
2050 ml_set_interrupts_enabled(intrs_enabled
);
2051 printf("Interrupt watchdog, "
2052 "cpu: %d interrupt: 0x%x time: %llu..%llu RIP: 0x%llx\n",
2053 cpu
, cpu_int_num
, cpu_int_event_time
, now
, cpu_rip
);
2058 ml_set_interrupts_enabled(intrs_enabled
);