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 <sys/kdebug.h>
82 #include <console/serial_protos.h>
85 #include <kern/monotonic.h>
86 #endif /* MONOTONIC */
89 #define PAUSE delay(1000000)
90 #define DBG(x...) kprintf(x)
96 /* Debugging/test trace events: */
97 #define TRACE_MP_TLB_FLUSH MACHDBG_CODE(DBG_MACH_MP, 0)
98 #define TRACE_MP_CPUS_CALL MACHDBG_CODE(DBG_MACH_MP, 1)
99 #define TRACE_MP_CPUS_CALL_LOCAL MACHDBG_CODE(DBG_MACH_MP, 2)
100 #define TRACE_MP_CPUS_CALL_ACTION MACHDBG_CODE(DBG_MACH_MP, 3)
101 #define TRACE_MP_CPUS_CALL_NOBUF MACHDBG_CODE(DBG_MACH_MP, 4)
102 #define TRACE_MP_CPU_FAST_START MACHDBG_CODE(DBG_MACH_MP, 5)
103 #define TRACE_MP_CPU_START MACHDBG_CODE(DBG_MACH_MP, 6)
104 #define TRACE_MP_CPU_DEACTIVATE MACHDBG_CODE(DBG_MACH_MP, 7)
106 #define ABS(v) (((v) > 0)?(v):-(v))
108 void slave_boot_init(void);
109 void i386_cpu_IPI(int cpu
);
112 static void mp_kdp_wait(boolean_t flush
, boolean_t isNMI
);
113 #endif /* MACH_KDP */
116 static boolean_t
cpu_signal_pending(int cpu
, mp_event_t event
);
117 #endif /* MACH_KDP */
118 static int NMIInterruptHandler(x86_saved_state_t
*regs
);
120 boolean_t smp_initialized
= FALSE
;
121 uint32_t TSC_sync_margin
= 0xFFF;
122 volatile boolean_t force_immediate_debugger_NMI
= FALSE
;
123 volatile boolean_t pmap_tlb_flush_timeout
= FALSE
;
124 #if DEBUG || DEVELOPMENT
125 boolean_t mp_interrupt_watchdog_enabled
= TRUE
;
126 uint32_t mp_interrupt_watchdog_events
= 0;
129 decl_simple_lock_data(,debugger_callback_lock
);
130 struct debugger_callback
*debugger_callback
= NULL
;
132 decl_lck_mtx_data(static, mp_cpu_boot_lock
);
133 lck_mtx_ext_t mp_cpu_boot_lock_ext
;
135 /* Variables needed for MP rendezvous. */
136 decl_simple_lock_data(,mp_rv_lock
);
137 static void (*mp_rv_setup_func
)(void *arg
);
138 static void (*mp_rv_action_func
)(void *arg
);
139 static void (*mp_rv_teardown_func
)(void *arg
);
140 static void *mp_rv_func_arg
;
141 static volatile int mp_rv_ncpus
;
142 /* Cache-aligned barriers: */
143 static volatile long mp_rv_entry
__attribute__((aligned(64)));
144 static volatile long mp_rv_exit
__attribute__((aligned(64)));
145 static volatile long mp_rv_complete
__attribute__((aligned(64)));
147 volatile uint64_t debugger_entry_time
;
148 volatile uint64_t debugger_exit_time
;
151 extern int kdp_snapshot
;
152 static struct _kdp_xcpu_call_func
{
153 kdp_x86_xcpu_func_t func
;
156 volatile uint16_t cpu
;
157 } kdp_xcpu_call_func
= {
163 /* Variables needed for MP broadcast. */
164 static void (*mp_bc_action_func
)(void *arg
);
165 static void *mp_bc_func_arg
;
166 static int mp_bc_ncpus
;
167 static volatile long mp_bc_count
;
168 decl_lck_mtx_data(static, mp_bc_lock
);
169 lck_mtx_ext_t mp_bc_lock_ext
;
170 static volatile int debugger_cpu
= -1;
171 volatile long NMIPI_acks
= 0;
172 volatile long NMI_count
= 0;
173 static NMI_reason_t NMI_panic_reason
= NONE
;
174 static int vector_timed_out
;
176 extern void NMI_cpus(void);
178 static void mp_cpus_call_init(void);
179 static void mp_cpus_call_action(void);
180 static void mp_call_PM(void);
182 char mp_slave_stack
[PAGE_SIZE
] __attribute__((aligned(PAGE_SIZE
))); // Temp stack for slave init
184 /* PAL-related routines */
185 boolean_t
i386_smp_init(int nmi_vector
, i386_intr_func_t nmi_handler
,
186 int ipi_vector
, i386_intr_func_t ipi_handler
);
187 void i386_start_cpu(int lapic_id
, int cpu_num
);
188 void i386_send_NMI(int cpu
);
189 void NMIPI_enable(boolean_t
);
192 * Initialize dummy structs for profiling. These aren't used but
193 * allows hertz_tick() to be built with GPROF defined.
195 struct profile_vars _profile_vars
;
196 struct profile_vars
*_profile_vars_cpus
[MAX_CPUS
] = { &_profile_vars
};
197 #define GPROF_INIT() \
201 /* Hack to initialize pointers to unused profiling structs */ \
202 for (i = 1; i < MAX_CPUS; i++) \
203 _profile_vars_cpus[i] = &_profile_vars; \
209 static lck_grp_t smp_lck_grp
;
210 static lck_grp_attr_t smp_lck_grp_attr
;
212 #define NUM_CPU_WARM_CALLS 20
213 struct timer_call cpu_warm_call_arr
[NUM_CPU_WARM_CALLS
];
214 queue_head_t cpu_warm_call_list
;
215 decl_simple_lock_data(static, cpu_warm_lock
);
217 typedef struct cpu_warm_data
{
218 timer_call_t cwd_call
;
219 uint64_t cwd_deadline
;
223 static void cpu_prewarm_init(void);
224 static void cpu_warm_timer_call_func(call_entry_param_t p0
, call_entry_param_t p1
);
225 static void _cpu_warm_setup(void *arg
);
226 static timer_call_t
grab_warm_timer_call(void);
227 static void free_warm_timer_call(timer_call_t call
);
232 simple_lock_init(&mp_rv_lock
, 0);
233 simple_lock_init(&debugger_callback_lock
, 0);
234 lck_grp_attr_setdefault(&smp_lck_grp_attr
);
235 lck_grp_init(&smp_lck_grp
, "i386_smp", &smp_lck_grp_attr
);
236 lck_mtx_init_ext(&mp_cpu_boot_lock
, &mp_cpu_boot_lock_ext
, &smp_lck_grp
, LCK_ATTR_NULL
);
237 lck_mtx_init_ext(&mp_bc_lock
, &mp_bc_lock_ext
, &smp_lck_grp
, LCK_ATTR_NULL
);
240 if(!i386_smp_init(LAPIC_NMI_INTERRUPT
, NMIInterruptHandler
,
241 LAPIC_VECTOR(INTERPROCESSOR
), cpu_signal_handler
))
247 DBGLOG_CPU_INIT(master_cpu
);
250 mp_cpus_call_cpu_init(master_cpu
);
252 #if DEBUG || DEVELOPMENT
253 if (PE_parse_boot_argn("interrupt_watchdog",
254 &mp_interrupt_watchdog_enabled
,
255 sizeof(mp_interrupt_watchdog_enabled
))) {
256 kprintf("Interrupt watchdog %sabled\n",
257 mp_interrupt_watchdog_enabled
? "en" : "dis");
261 if (PE_parse_boot_argn("TSC_sync_margin",
262 &TSC_sync_margin
, sizeof(TSC_sync_margin
))) {
263 kprintf("TSC sync Margin 0x%x\n", TSC_sync_margin
);
264 } else if (cpuid_vmm_present()) {
265 kprintf("TSC sync margin disabled\n");
268 smp_initialized
= TRUE
;
279 } processor_start_info_t
;
280 static processor_start_info_t start_info
__attribute__((aligned(64)));
283 * Cache-alignment is to avoid cross-cpu false-sharing interference.
285 static volatile long tsc_entry_barrier
__attribute__((aligned(64)));
286 static volatile long tsc_exit_barrier
__attribute__((aligned(64)));
287 static volatile uint64_t tsc_target
__attribute__((aligned(64)));
290 * Poll a CPU to see when it has marked itself as running.
293 mp_wait_for_cpu_up(int slot_num
, unsigned int iters
, unsigned int usecdelay
)
295 while (iters
-- > 0) {
296 if (cpu_datap(slot_num
)->cpu_running
)
303 * Quickly bring a CPU back online which has been halted.
306 intel_startCPU_fast(int slot_num
)
311 * Try to perform a fast restart
313 rc
= pmCPUExitHalt(slot_num
);
314 if (rc
!= KERN_SUCCESS
)
316 * The CPU was not eligible for a fast restart.
320 KERNEL_DEBUG_CONSTANT(
321 TRACE_MP_CPU_FAST_START
| DBG_FUNC_START
,
322 slot_num
, 0, 0, 0, 0);
325 * Wait until the CPU is back online.
327 mp_disable_preemption();
330 * We use short pauses (1us) for low latency. 30,000 iterations is
331 * longer than a full restart would require so it should be more
335 mp_wait_for_cpu_up(slot_num
, 30000, 1);
336 mp_enable_preemption();
338 KERNEL_DEBUG_CONSTANT(
339 TRACE_MP_CPU_FAST_START
| DBG_FUNC_END
,
340 slot_num
, cpu_datap(slot_num
)->cpu_running
, 0, 0, 0);
343 * Check to make sure that the CPU is really running. If not,
344 * go through the slow path.
346 if (cpu_datap(slot_num
)->cpu_running
)
347 return(KERN_SUCCESS
);
349 return(KERN_FAILURE
);
355 /* Here on the started cpu with cpu_running set TRUE */
357 if (TSC_sync_margin
&&
358 start_info
.target_cpu
== cpu_number()) {
360 * I've just started-up, synchronize again with the starter cpu
361 * and then snap my TSC.
364 atomic_decl(&tsc_entry_barrier
, 1);
365 while (tsc_entry_barrier
!= 0)
366 ; /* spin for starter and target at barrier */
367 tsc_target
= rdtsc64();
368 atomic_decl(&tsc_exit_barrier
, 1);
376 processor_start_info_t
*psip
= (processor_start_info_t
*) arg
;
378 /* Ignore this if the current processor is not the starter */
379 if (cpu_number() != psip
->starter_cpu
)
382 DBG("start_cpu(%p) about to start cpu %d, lapic %d\n",
383 arg
, psip
->target_cpu
, psip
->target_lapic
);
385 KERNEL_DEBUG_CONSTANT(
386 TRACE_MP_CPU_START
| DBG_FUNC_START
,
388 psip
->target_lapic
, 0, 0, 0);
390 i386_start_cpu(psip
->target_lapic
, psip
->target_cpu
);
392 #ifdef POSTCODE_DELAY
393 /* Wait much longer if postcodes are displayed for a delay period. */
396 DBG("start_cpu(%p) about to wait for cpu %d\n",
397 arg
, psip
->target_cpu
);
399 mp_wait_for_cpu_up(psip
->target_cpu
, i
*100, 100);
401 KERNEL_DEBUG_CONSTANT(
402 TRACE_MP_CPU_START
| DBG_FUNC_END
,
404 cpu_datap(psip
->target_cpu
)->cpu_running
, 0, 0, 0);
406 if (TSC_sync_margin
&&
407 cpu_datap(psip
->target_cpu
)->cpu_running
) {
409 * Compare the TSC from the started processor with ours.
410 * Report and log/panic if it diverges by more than
411 * TSC_sync_margin (TSC_SYNC_MARGIN) ticks. This margin
412 * can be overriden by boot-arg (with 0 meaning no checking).
414 uint64_t tsc_starter
;
416 atomic_decl(&tsc_entry_barrier
, 1);
417 while (tsc_entry_barrier
!= 0)
418 ; /* spin for both processors at barrier */
419 tsc_starter
= rdtsc64();
420 atomic_decl(&tsc_exit_barrier
, 1);
421 while (tsc_exit_barrier
!= 0)
422 ; /* spin for target to store its TSC */
423 tsc_delta
= tsc_target
- tsc_starter
;
424 kprintf("TSC sync for cpu %d: 0x%016llx delta 0x%llx (%lld)\n",
425 psip
->target_cpu
, tsc_target
, tsc_delta
, tsc_delta
);
426 if (ABS(tsc_delta
) > (int64_t) TSC_sync_margin
) {
432 "Unsynchronized TSC for cpu %d: "
433 "0x%016llx, delta 0x%llx\n",
434 psip
->target_cpu
, tsc_target
, tsc_delta
);
443 int lapic
= cpu_to_lapic
[slot_num
];
448 DBGLOG_CPU_INIT(slot_num
);
450 DBG("intel_startCPU(%d) lapic_id=%d\n", slot_num
, lapic
);
451 DBG("IdlePTD(%p): 0x%x\n", &IdlePTD
, (int) (uintptr_t)IdlePTD
);
454 * Initialize (or re-initialize) the descriptor tables for this cpu.
455 * Propagate processor mode to slave.
457 cpu_desc_init(cpu_datap(slot_num
));
459 /* Serialize use of the slave boot stack, etc. */
460 lck_mtx_lock(&mp_cpu_boot_lock
);
462 istate
= ml_set_interrupts_enabled(FALSE
);
463 if (slot_num
== get_cpu_number()) {
464 ml_set_interrupts_enabled(istate
);
465 lck_mtx_unlock(&mp_cpu_boot_lock
);
469 start_info
.starter_cpu
= cpu_number();
470 start_info
.target_cpu
= slot_num
;
471 start_info
.target_lapic
= lapic
;
472 tsc_entry_barrier
= 2;
473 tsc_exit_barrier
= 2;
476 * Perform the processor startup sequence with all running
477 * processors rendezvous'ed. This is required during periods when
478 * the cache-disable bit is set for MTRR/PAT initialization.
480 mp_rendezvous_no_intrs(start_cpu
, (void *) &start_info
);
482 start_info
.target_cpu
= 0;
484 ml_set_interrupts_enabled(istate
);
485 lck_mtx_unlock(&mp_cpu_boot_lock
);
487 if (!cpu_datap(slot_num
)->cpu_running
) {
488 kprintf("Failed to start CPU %02d\n", slot_num
);
489 printf("Failed to start CPU %02d, rebooting...\n", slot_num
);
494 kprintf("Started cpu %d (lapic id %08x)\n", slot_num
, lapic
);
500 cpu_signal_event_log_t
*cpu_signal
[MAX_CPUS
];
501 cpu_signal_event_log_t
*cpu_handle
[MAX_CPUS
];
503 MP_EVENT_NAME_DECL();
505 #endif /* MP_DEBUG */
508 * Note: called with NULL state when polling for TLB flush and cross-calls.
511 cpu_signal_handler(x86_saved_state_t
*regs
)
514 #pragma unused (regs)
515 #endif /* !MACH_KDP */
517 volatile int *my_word
;
519 SCHED_STATS_IPI(current_processor());
521 my_cpu
= cpu_number();
522 my_word
= &cpu_data_ptr
[my_cpu
]->cpu_signals
;
523 /* Store the initial set of signals for diagnostics. New
524 * signals could arrive while these are being processed
525 * so it's no more than a hint.
528 cpu_data_ptr
[my_cpu
]->cpu_prior_signals
= *my_word
;
532 if (i_bit(MP_KDP
, my_word
)) {
533 DBGLOG(cpu_handle
,my_cpu
,MP_KDP
);
534 i_bit_clear(MP_KDP
, my_word
);
535 /* Ensure that the i386_kernel_state at the base of the
536 * current thread's stack (if any) is synchronized with the
537 * context at the moment of the interrupt, to facilitate
538 * access through the debugger.
540 sync_iss_to_iks(regs
);
541 if (pmsafe_debug
&& !kdp_snapshot
)
542 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
543 mp_kdp_wait(TRUE
, FALSE
);
544 if (pmsafe_debug
&& !kdp_snapshot
)
545 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_NORMAL
);
547 #endif /* MACH_KDP */
548 if (i_bit(MP_TLB_FLUSH
, my_word
)) {
549 DBGLOG(cpu_handle
,my_cpu
,MP_TLB_FLUSH
);
550 i_bit_clear(MP_TLB_FLUSH
, my_word
);
551 pmap_update_interrupt();
552 } else if (i_bit(MP_CALL
, my_word
)) {
553 DBGLOG(cpu_handle
,my_cpu
,MP_CALL
);
554 i_bit_clear(MP_CALL
, my_word
);
555 mp_cpus_call_action();
556 } else if (i_bit(MP_CALL_PM
, my_word
)) {
557 DBGLOG(cpu_handle
,my_cpu
,MP_CALL_PM
);
558 i_bit_clear(MP_CALL_PM
, my_word
);
562 /* Called to poll only for cross-calls and TLB flush */
564 } else if (i_bit(MP_AST
, my_word
)) {
565 DBGLOG(cpu_handle
,my_cpu
,MP_AST
);
566 i_bit_clear(MP_AST
, my_word
);
567 ast_check(cpu_to_processor(my_cpu
));
574 extern void kprintf_break_lock(void);
576 NMIInterruptHandler(x86_saved_state_t
*regs
)
580 uint64_t now
= mach_absolute_time();
582 if (panic_active() && !panicDebugging
) {
584 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
589 atomic_incl(&NMIPI_acks
, 1);
590 atomic_incl(&NMI_count
, 1);
591 sync_iss_to_iks_unconditionally(regs
);
592 __asm__
volatile("movq %%rbp, %0" : "=m" (stackptr
));
594 if (cpu_number() == debugger_cpu
)
597 if (NMI_panic_reason
== SPINLOCK_TIMEOUT
) {
598 snprintf(&pstr
[0], sizeof(pstr
),
599 "Panic(CPU %d, time %llu): NMIPI for spinlock acquisition timeout, spinlock: %p, spinlock owner: %p, current_thread: %p, spinlock_owner_cpu: 0x%x\n",
600 cpu_number(), now
, spinlock_timed_out
, (void *) spinlock_timed_out
->interlock
.lock_data
, current_thread(), spinlock_owner_cpu
);
601 panic_i386_backtrace(stackptr
, 64, &pstr
[0], TRUE
, regs
);
602 } else if (NMI_panic_reason
== TLB_FLUSH_TIMEOUT
) {
603 snprintf(&pstr
[0], sizeof(pstr
),
604 "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: TLB flush timeout, TLB state:0x%x\n",
605 cpu_number(), now
, current_cpu_datap()->cpu_tlb_invalid
);
606 panic_i386_backtrace(stackptr
, 48, &pstr
[0], TRUE
, regs
);
607 } else if (NMI_panic_reason
== CROSSCALL_TIMEOUT
) {
608 snprintf(&pstr
[0], sizeof(pstr
),
609 "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: cross-call timeout\n",
611 panic_i386_backtrace(stackptr
, 64, &pstr
[0], TRUE
, regs
);
612 } else if (NMI_panic_reason
== INTERRUPT_WATCHDOG
) {
613 snprintf(&pstr
[0], sizeof(pstr
),
614 "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: interrupt watchdog for vector 0x%x\n",
615 cpu_number(), now
, vector_timed_out
);
616 panic_i386_backtrace(stackptr
, 64, &pstr
[0], TRUE
, regs
);
620 if (pmsafe_debug
&& !kdp_snapshot
)
621 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
622 current_cpu_datap()->cpu_NMI_acknowledged
= TRUE
;
623 i_bit_clear(MP_KDP
, ¤t_cpu_datap()->cpu_signals
);
624 if (panic_active() || NMI_panic_reason
!= NONE
) {
625 mp_kdp_wait(FALSE
, TRUE
);
626 } else if (!mp_kdp_trap
&&
628 virtualized
&& (debug_boot_arg
& DB_NMI
)) {
630 * Under a VMM with the debug boot-arg set, drop into kdp.
631 * Since an NMI is involved, there's a risk of contending with
632 * a panic. And side-effects of NMIs may result in entry into,
633 * and continuing from, the debugger being unreliable.
635 if (__sync_bool_compare_and_swap(&mp_kdp_is_NMI
, FALSE
, TRUE
)) {
636 kprintf_break_lock();
637 kprintf("Debugger entry requested by NMI\n");
638 kdp_i386_trap(T_DEBUG
, saved_state64(regs
), 0, 0);
639 printf("Debugger entry requested by NMI\n");
640 mp_kdp_is_NMI
= FALSE
;
642 mp_kdp_wait(FALSE
, FALSE
);
645 mp_kdp_wait(FALSE
, FALSE
);
647 if (pmsafe_debug
&& !kdp_snapshot
)
648 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_NORMAL
);
656 * cpu_interrupt is really just to be used by the scheduler to
657 * get a CPU's attention it may not always issue an IPI. If an
658 * IPI is always needed then use i386_cpu_IPI.
661 cpu_interrupt(int cpu
)
663 boolean_t did_IPI
= FALSE
;
666 && pmCPUExitIdle(cpu_datap(cpu
))) {
671 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_AST
), cpu
, did_IPI
, 0, 0, 0);
675 * Send a true NMI via the local APIC to the specified CPU.
678 cpu_NMI_interrupt(int cpu
)
680 if (smp_initialized
) {
689 boolean_t intrs_enabled
;
690 uint64_t tsc_timeout
;
692 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
694 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
695 if (!cpu_is_running(cpu
))
697 cpu_datap(cpu
)->cpu_NMI_acknowledged
= FALSE
;
698 cpu_NMI_interrupt(cpu
);
699 tsc_timeout
= !machine_timeout_suspended() ?
700 rdtsc64() + (1000 * 1000 * 1000 * 10ULL) :
702 while (!cpu_datap(cpu
)->cpu_NMI_acknowledged
) {
703 handle_pending_TLB_flushes();
705 if (rdtsc64() > tsc_timeout
)
706 panic("NMI_cpus() timeout cpu %d", cpu
);
708 cpu_datap(cpu
)->cpu_NMI_acknowledged
= FALSE
;
712 ml_set_interrupts_enabled(intrs_enabled
);
715 static void (* volatile mp_PM_func
)(void) = NULL
;
720 assert(!ml_get_interrupts_enabled());
722 if (mp_PM_func
!= NULL
)
727 cpu_PM_interrupt(int cpu
)
729 assert(!ml_get_interrupts_enabled());
731 if (mp_PM_func
!= NULL
) {
732 if (cpu
== cpu_number())
735 i386_signal_cpu(cpu
, MP_CALL_PM
, ASYNC
);
740 PM_interrupt_register(void (*fn
)(void))
746 i386_signal_cpu(int cpu
, mp_event_t event
, mp_sync_t mode
)
748 volatile int *signals
= &cpu_datap(cpu
)->cpu_signals
;
749 uint64_t tsc_timeout
;
752 if (!cpu_datap(cpu
)->cpu_running
)
755 if (event
== MP_TLB_FLUSH
)
756 KERNEL_DEBUG(TRACE_MP_TLB_FLUSH
| DBG_FUNC_START
, cpu
, 0, 0, 0, 0);
758 DBGLOG(cpu_signal
, cpu
, event
);
760 i_bit_set(event
, signals
);
764 tsc_timeout
= !machine_timeout_suspended() ?
765 rdtsc64() + (1000*1000*1000) :
767 while (i_bit(event
, signals
) && rdtsc64() < tsc_timeout
) {
770 if (i_bit(event
, signals
)) {
771 DBG("i386_signal_cpu(%d, 0x%x, SYNC) timed out\n",
776 if (event
== MP_TLB_FLUSH
)
777 KERNEL_DEBUG(TRACE_MP_TLB_FLUSH
| DBG_FUNC_END
, cpu
, 0, 0, 0, 0);
781 * Helper function called when busy-waiting: panic if too long
782 * a TSC-based time has elapsed since the start of the spin.
785 mp_spin_timeout(uint64_t tsc_start
)
787 uint64_t tsc_timeout
;
790 if (machine_timeout_suspended())
794 * The timeout is 4 * the spinlock timeout period
795 * unless we have serial console printing (kprintf) enabled
796 * in which case we allow an even greater margin.
798 tsc_timeout
= disable_serial_output
? LockTimeOutTSC
<< 2
799 : LockTimeOutTSC
<< 4;
800 return (rdtsc64() > tsc_start
+ tsc_timeout
);
804 * Helper function to take a spinlock while ensuring that incoming IPIs
805 * are still serviced if interrupts are masked while we spin.
806 * Returns current interrupt state.
809 mp_safe_spin_lock(usimple_lock_t lock
)
811 if (ml_get_interrupts_enabled()) {
815 uint64_t tsc_spin_start
= rdtsc64();
816 while (!simple_lock_try(lock
)) {
817 cpu_signal_handler(NULL
);
818 if (mp_spin_timeout(tsc_spin_start
)) {
820 uintptr_t lowner
= (uintptr_t)
821 lock
->interlock
.lock_data
;
822 spinlock_timed_out
= lock
;
823 lock_cpu
= spinlock_timeout_NMI(lowner
);
824 NMIPI_panic(cpu_to_cpumask(lock_cpu
), SPINLOCK_TIMEOUT
);
825 panic("mp_safe_spin_lock() timed out, lock: %p, owner thread: 0x%lx, current_thread: %p, owner on CPU 0x%x, time: %llu",
826 lock
, lowner
, current_thread(), lock_cpu
, mach_absolute_time());
834 * All-CPU rendezvous:
835 * - CPUs are signalled,
836 * - all execute the setup function (if specified),
837 * - rendezvous (i.e. all cpus reach a barrier),
838 * - all execute the action function (if specified),
839 * - rendezvous again,
840 * - execute the teardown function (if specified), and then
843 * Note that the supplied external functions _must_ be reentrant and aware
844 * that they are running in parallel and in an unknown lock context.
848 mp_rendezvous_action(__unused
void *null
)
850 boolean_t intrs_enabled
;
851 uint64_t tsc_spin_start
;
854 * Note that mp_rv_lock was acquired by the thread that initiated the
855 * rendezvous and must have been acquired before we enter
856 * mp_rendezvous_action().
858 current_cpu_datap()->cpu_rendezvous_in_progress
= TRUE
;
861 if (mp_rv_setup_func
!= NULL
)
862 mp_rv_setup_func(mp_rv_func_arg
);
864 intrs_enabled
= ml_get_interrupts_enabled();
866 /* spin on entry rendezvous */
867 atomic_incl(&mp_rv_entry
, 1);
868 tsc_spin_start
= rdtsc64();
870 while (mp_rv_entry
< mp_rv_ncpus
) {
871 /* poll for pesky tlb flushes if interrupts disabled */
873 handle_pending_TLB_flushes();
874 if (mp_spin_timeout(tsc_spin_start
)) {
875 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());
879 /* action function */
880 if (mp_rv_action_func
!= NULL
)
881 mp_rv_action_func(mp_rv_func_arg
);
883 /* spin on exit rendezvous */
884 atomic_incl(&mp_rv_exit
, 1);
885 tsc_spin_start
= rdtsc64();
886 while (mp_rv_exit
< mp_rv_ncpus
) {
888 handle_pending_TLB_flushes();
889 if (mp_spin_timeout(tsc_spin_start
))
890 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());
893 /* teardown function */
894 if (mp_rv_teardown_func
!= NULL
)
895 mp_rv_teardown_func(mp_rv_func_arg
);
897 current_cpu_datap()->cpu_rendezvous_in_progress
= FALSE
;
899 /* Bump completion count */
900 atomic_incl(&mp_rv_complete
, 1);
904 mp_rendezvous(void (*setup_func
)(void *),
905 void (*action_func
)(void *),
906 void (*teardown_func
)(void *),
909 uint64_t tsc_spin_start
;
911 if (!smp_initialized
) {
912 if (setup_func
!= NULL
)
914 if (action_func
!= NULL
)
916 if (teardown_func
!= NULL
)
921 /* obtain rendezvous lock */
922 mp_rendezvous_lock();
924 /* set static function pointers */
925 mp_rv_setup_func
= setup_func
;
926 mp_rv_action_func
= action_func
;
927 mp_rv_teardown_func
= teardown_func
;
928 mp_rv_func_arg
= arg
;
935 * signal other processors, which will call mp_rendezvous_action()
936 * with interrupts disabled
938 mp_rv_ncpus
= mp_cpus_call(CPUMASK_OTHERS
, NOSYNC
, &mp_rendezvous_action
, NULL
) + 1;
940 /* call executor function on this cpu */
941 mp_rendezvous_action(NULL
);
944 * Spin for everyone to complete.
945 * This is necessary to ensure that all processors have proceeded
946 * from the exit barrier before we release the rendezvous structure.
948 tsc_spin_start
= rdtsc64();
949 while (mp_rv_complete
< mp_rv_ncpus
) {
950 if (mp_spin_timeout(tsc_spin_start
))
951 panic("mp_rendezvous() timeout: %ld of %d responses, start: 0x%llx, cur: 0x%llx", mp_rv_complete
, mp_rv_ncpus
, tsc_spin_start
, rdtsc64());
955 mp_rv_setup_func
= NULL
;
956 mp_rv_action_func
= NULL
;
957 mp_rv_teardown_func
= NULL
;
958 mp_rv_func_arg
= NULL
;
961 mp_rendezvous_unlock();
965 mp_rendezvous_lock(void)
967 (void) mp_safe_spin_lock(&mp_rv_lock
);
971 mp_rendezvous_unlock(void)
973 simple_unlock(&mp_rv_lock
);
977 mp_rendezvous_break_lock(void)
979 simple_lock_init(&mp_rv_lock
, 0);
983 setup_disable_intrs(__unused
void * param_not_used
)
985 /* disable interrupts before the first barrier */
986 boolean_t intr
= ml_set_interrupts_enabled(FALSE
);
988 current_cpu_datap()->cpu_iflag
= intr
;
989 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__
);
993 teardown_restore_intrs(__unused
void * param_not_used
)
995 /* restore interrupt flag following MTRR changes */
996 ml_set_interrupts_enabled(current_cpu_datap()->cpu_iflag
);
997 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__
);
1001 * A wrapper to mp_rendezvous() to call action_func() with interrupts disabled.
1002 * This is exported for use by kexts.
1005 mp_rendezvous_no_intrs(
1006 void (*action_func
)(void *),
1009 mp_rendezvous(setup_disable_intrs
,
1011 teardown_restore_intrs
,
1017 queue_chain_t link
; /* queue linkage */
1018 void (*func
)(void *,void *); /* routine to call */
1019 void *arg0
; /* routine's 1st arg */
1020 void *arg1
; /* routine's 2nd arg */
1021 cpumask_t
*maskp
; /* completion response mask */
1027 decl_simple_lock_data(, lock
);
1029 #define MP_CPUS_CALL_BUFS_PER_CPU MAX_CPUS
1030 static mp_call_queue_t mp_cpus_call_freelist
;
1031 static mp_call_queue_t mp_cpus_call_head
[MAX_CPUS
];
1033 static inline boolean_t
1034 mp_call_head_lock(mp_call_queue_t
*cqp
)
1036 boolean_t intrs_enabled
;
1038 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
1039 simple_lock(&cqp
->lock
);
1041 return intrs_enabled
;
1045 * Deliver an NMIPI to a set of processors to cause them to panic .
1048 NMIPI_panic(cpumask_t cpu_mask
, NMI_reason_t why
) {
1054 NMI_panic_reason
= why
;
1056 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
1057 if ((cpu_mask
& cpu_bit
) == 0)
1059 cpu_datap(cpu
)->cpu_NMI_acknowledged
= FALSE
;
1060 cpu_NMI_interrupt(cpu
);
1063 /* Wait (only so long) for NMi'ed cpus to respond */
1064 deadline
= mach_absolute_time() + LockTimeOut
;
1065 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
1066 if ((cpu_mask
& cpu_bit
) == 0)
1068 while (!cpu_datap(cpu
)->cpu_NMI_acknowledged
&&
1069 mach_absolute_time() < deadline
) {
1076 static inline boolean_t
1077 mp_call_head_is_locked(mp_call_queue_t
*cqp
)
1079 return !ml_get_interrupts_enabled() &&
1080 hw_lock_held((hw_lock_t
)&cqp
->lock
);
1085 mp_call_head_unlock(mp_call_queue_t
*cqp
, boolean_t intrs_enabled
)
1087 simple_unlock(&cqp
->lock
);
1088 ml_set_interrupts_enabled(intrs_enabled
);
1091 static inline mp_call_t
*
1094 mp_call_t
*callp
= NULL
;
1095 boolean_t intrs_enabled
;
1096 mp_call_queue_t
*cqp
= &mp_cpus_call_freelist
;
1098 intrs_enabled
= mp_call_head_lock(cqp
);
1099 if (!queue_empty(&cqp
->queue
))
1100 queue_remove_first(&cqp
->queue
, callp
, typeof(callp
), link
);
1101 mp_call_head_unlock(cqp
, intrs_enabled
);
1107 mp_call_free(mp_call_t
*callp
)
1109 boolean_t intrs_enabled
;
1110 mp_call_queue_t
*cqp
= &mp_cpus_call_freelist
;
1112 intrs_enabled
= mp_call_head_lock(cqp
);
1113 queue_enter_first(&cqp
->queue
, callp
, typeof(callp
), link
);
1114 mp_call_head_unlock(cqp
, intrs_enabled
);
1117 static inline mp_call_t
*
1118 mp_call_dequeue_locked(mp_call_queue_t
*cqp
)
1120 mp_call_t
*callp
= NULL
;
1122 assert(mp_call_head_is_locked(cqp
));
1123 if (!queue_empty(&cqp
->queue
))
1124 queue_remove_first(&cqp
->queue
, callp
, typeof(callp
), link
);
1129 mp_call_enqueue_locked(
1130 mp_call_queue_t
*cqp
,
1133 queue_enter(&cqp
->queue
, callp
, typeof(callp
), link
);
1136 /* Called on the boot processor to initialize global structures */
1138 mp_cpus_call_init(void)
1140 mp_call_queue_t
*cqp
= &mp_cpus_call_freelist
;
1142 DBG("mp_cpus_call_init()\n");
1143 simple_lock_init(&cqp
->lock
, 0);
1144 queue_init(&cqp
->queue
);
1148 * Called at processor registration to add call buffers to the free list
1149 * and to initialize the per-cpu call queue.
1152 mp_cpus_call_cpu_init(int cpu
)
1155 mp_call_queue_t
*cqp
= &mp_cpus_call_head
[cpu
];
1158 simple_lock_init(&cqp
->lock
, 0);
1159 queue_init(&cqp
->queue
);
1160 for (i
= 0; i
< MP_CPUS_CALL_BUFS_PER_CPU
; i
++) {
1161 callp
= (mp_call_t
*) kalloc(sizeof(mp_call_t
));
1162 mp_call_free(callp
);
1165 DBG("mp_cpus_call_init(%d) done\n", cpu
);
1169 * This is called from cpu_signal_handler() to process an MP_CALL signal.
1170 * And also from i386_deactivate_cpu() when a cpu is being taken offline.
1173 mp_cpus_call_action(void)
1175 mp_call_queue_t
*cqp
;
1176 boolean_t intrs_enabled
;
1180 assert(!ml_get_interrupts_enabled());
1181 cqp
= &mp_cpus_call_head
[cpu_number()];
1182 intrs_enabled
= mp_call_head_lock(cqp
);
1183 while ((callp
= mp_call_dequeue_locked(cqp
)) != NULL
) {
1184 /* Copy call request to the stack to free buffer */
1186 mp_call_free(callp
);
1187 if (call
.func
!= NULL
) {
1188 mp_call_head_unlock(cqp
, intrs_enabled
);
1189 KERNEL_DEBUG_CONSTANT(
1190 TRACE_MP_CPUS_CALL_ACTION
,
1191 VM_KERNEL_UNSLIDE(call
.func
), VM_KERNEL_UNSLIDE_OR_PERM(call
.arg0
),
1192 VM_KERNEL_UNSLIDE_OR_PERM(call
.arg1
), VM_KERNEL_ADDRPERM(call
.maskp
), 0);
1193 call
.func(call
.arg0
, call
.arg1
);
1194 (void) mp_call_head_lock(cqp
);
1196 if (call
.maskp
!= NULL
)
1197 i_bit_set(cpu_number(), call
.maskp
);
1199 mp_call_head_unlock(cqp
, intrs_enabled
);
1203 * mp_cpus_call() runs a given function on cpus specified in a given cpu mask.
1204 * Possible modes are:
1205 * SYNC: function is called serially on target cpus in logical cpu order
1206 * waiting for each call to be acknowledged before proceeding
1207 * ASYNC: function call is queued to the specified cpus
1208 * waiting for all calls to complete in parallel before returning
1209 * NOSYNC: function calls are queued
1210 * but we return before confirmation of calls completing.
1211 * The action function may be NULL.
1212 * The cpu mask may include the local cpu. Offline cpus are ignored.
1213 * The return value is the number of cpus on which the call was made or queued.
1219 void (*action_func
)(void *),
1222 return mp_cpus_call1(
1225 (void (*)(void *,void *))action_func
,
1232 mp_cpus_call_wait(boolean_t intrs_enabled
,
1233 cpumask_t cpus_called
,
1234 cpumask_t
*cpus_responded
)
1236 mp_call_queue_t
*cqp
;
1237 uint64_t tsc_spin_start
;
1239 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
1240 cqp
= &mp_cpus_call_head
[cpu_number()];
1242 tsc_spin_start
= rdtsc64();
1243 while (*cpus_responded
!= cpus_called
) {
1244 if (!intrs_enabled
) {
1245 /* Sniffing w/o locking */
1246 if (!queue_empty(&cqp
->queue
))
1247 mp_cpus_call_action();
1248 cpu_signal_handler(NULL
);
1250 if (mp_spin_timeout(tsc_spin_start
)) {
1251 cpumask_t cpus_unresponsive
;
1253 cpus_unresponsive
= cpus_called
& ~(*cpus_responded
);
1254 NMIPI_panic(cpus_unresponsive
, CROSSCALL_TIMEOUT
);
1255 panic("mp_cpus_call_wait() timeout, cpus: 0x%llx",
1265 void (*action_func
)(void *, void *),
1268 cpumask_t
*cpus_calledp
)
1271 boolean_t intrs_enabled
= FALSE
;
1272 boolean_t call_self
= FALSE
;
1273 cpumask_t cpus_called
= 0;
1274 cpumask_t cpus_responded
= 0;
1275 long cpus_call_count
= 0;
1276 uint64_t tsc_spin_start
;
1277 boolean_t topo_lock
;
1279 KERNEL_DEBUG_CONSTANT(
1280 TRACE_MP_CPUS_CALL
| DBG_FUNC_START
,
1281 cpus
, mode
, VM_KERNEL_UNSLIDE(action_func
), VM_KERNEL_UNSLIDE_OR_PERM(arg0
), VM_KERNEL_UNSLIDE_OR_PERM(arg1
));
1283 if (!smp_initialized
) {
1284 if ((cpus
& CPUMASK_SELF
) == 0)
1286 if (action_func
!= NULL
) {
1287 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
1288 action_func(arg0
, arg1
);
1289 ml_set_interrupts_enabled(intrs_enabled
);
1296 * Queue the call for each non-local requested cpu.
1297 * This is performed under the topo lock to prevent changes to
1298 * cpus online state and to prevent concurrent rendezvouses --
1299 * although an exception is made if we're calling only the master
1300 * processor since that always remains active. Note: this exception
1301 * is expected for longterm timer nosync cross-calls to the master cpu.
1303 mp_disable_preemption();
1304 intrs_enabled
= ml_get_interrupts_enabled();
1305 topo_lock
= (cpus
!= cpu_to_cpumask(master_cpu
));
1307 ml_set_interrupts_enabled(FALSE
);
1308 (void) mp_safe_spin_lock(&x86_topo_lock
);
1310 for (cpu
= 0; cpu
< (cpu_t
) real_ncpus
; cpu
++) {
1311 if (((cpu_to_cpumask(cpu
) & cpus
) == 0) ||
1312 !cpu_is_running(cpu
))
1314 tsc_spin_start
= rdtsc64();
1315 if (cpu
== (cpu_t
) cpu_number()) {
1317 * We don't IPI ourself and if calling asynchronously,
1318 * we defer our call until we have signalled all others.
1321 if (mode
== SYNC
&& action_func
!= NULL
) {
1322 KERNEL_DEBUG_CONSTANT(
1323 TRACE_MP_CPUS_CALL_LOCAL
,
1324 VM_KERNEL_UNSLIDE(action_func
),
1325 VM_KERNEL_UNSLIDE_OR_PERM(arg0
), VM_KERNEL_UNSLIDE_OR_PERM(arg1
), 0, 0);
1326 action_func(arg0
, arg1
);
1330 * Here to queue a call to cpu and IPI.
1332 mp_call_t
*callp
= NULL
;
1333 mp_call_queue_t
*cqp
= &mp_cpus_call_head
[cpu
];
1334 boolean_t intrs_inner
;
1338 callp
= mp_call_alloc();
1339 intrs_inner
= mp_call_head_lock(cqp
);
1340 if (callp
== NULL
) {
1341 mp_call_head_unlock(cqp
, intrs_inner
);
1342 KERNEL_DEBUG_CONSTANT(
1343 TRACE_MP_CPUS_CALL_NOBUF
,
1346 /* Sniffing w/o locking */
1347 if (!queue_empty(&cqp
->queue
))
1348 mp_cpus_call_action();
1349 handle_pending_TLB_flushes();
1351 if (mp_spin_timeout(tsc_spin_start
))
1352 panic("mp_cpus_call1() timeout start: 0x%llx, cur: 0x%llx",
1353 tsc_spin_start
, rdtsc64());
1356 callp
->maskp
= (mode
== NOSYNC
) ? NULL
: &cpus_responded
;
1357 callp
->func
= action_func
;
1360 mp_call_enqueue_locked(cqp
, callp
);
1362 cpus_called
|= cpu_to_cpumask(cpu
);
1363 i386_signal_cpu(cpu
, MP_CALL
, ASYNC
);
1364 mp_call_head_unlock(cqp
, intrs_inner
);
1366 mp_cpus_call_wait(intrs_inner
, cpus_called
, &cpus_responded
);
1371 simple_unlock(&x86_topo_lock
);
1372 ml_set_interrupts_enabled(intrs_enabled
);
1375 /* Call locally if mode not SYNC */
1376 if (mode
!= SYNC
&& call_self
) {
1377 KERNEL_DEBUG_CONSTANT(
1378 TRACE_MP_CPUS_CALL_LOCAL
,
1379 VM_KERNEL_UNSLIDE(action_func
), VM_KERNEL_UNSLIDE_OR_PERM(arg0
), VM_KERNEL_UNSLIDE_OR_PERM(arg1
), 0, 0);
1380 if (action_func
!= NULL
) {
1381 ml_set_interrupts_enabled(FALSE
);
1382 action_func(arg0
, arg1
);
1383 ml_set_interrupts_enabled(intrs_enabled
);
1387 /* For ASYNC, now wait for all signaled cpus to complete their calls */
1389 mp_cpus_call_wait(intrs_enabled
, cpus_called
, &cpus_responded
);
1391 /* Safe to allow pre-emption now */
1392 mp_enable_preemption();
1396 cpus_called
|= cpu_to_cpumask(cpu
);
1401 *cpus_calledp
= cpus_called
;
1403 KERNEL_DEBUG_CONSTANT(
1404 TRACE_MP_CPUS_CALL
| DBG_FUNC_END
,
1405 cpus_call_count
, cpus_called
, 0, 0, 0);
1407 return (cpu_t
) cpus_call_count
;
1412 mp_broadcast_action(__unused
void *null
)
1414 /* call action function */
1415 if (mp_bc_action_func
!= NULL
)
1416 mp_bc_action_func(mp_bc_func_arg
);
1418 /* if we're the last one through, wake up the instigator */
1419 if (atomic_decl_and_test(&mp_bc_count
, 1))
1420 thread_wakeup(((event_t
)(uintptr_t) &mp_bc_count
));
1424 * mp_broadcast() runs a given function on all active cpus.
1425 * The caller blocks until the functions has run on all cpus.
1426 * The caller will also block if there is another pending braodcast.
1430 void (*action_func
)(void *),
1433 if (!smp_initialized
) {
1434 if (action_func
!= NULL
)
1439 /* obtain broadcast lock */
1440 lck_mtx_lock(&mp_bc_lock
);
1442 /* set static function pointers */
1443 mp_bc_action_func
= action_func
;
1444 mp_bc_func_arg
= arg
;
1446 assert_wait((event_t
)(uintptr_t)&mp_bc_count
, THREAD_UNINT
);
1449 * signal other processors, which will call mp_broadcast_action()
1451 mp_bc_count
= real_ncpus
; /* assume max possible active */
1452 mp_bc_ncpus
= mp_cpus_call(CPUMASK_OTHERS
, NOSYNC
, *mp_broadcast_action
, NULL
) + 1;
1453 atomic_decl(&mp_bc_count
, real_ncpus
- mp_bc_ncpus
); /* subtract inactive */
1455 /* call executor function on this cpu */
1456 mp_broadcast_action(NULL
);
1458 /* block for other cpus to have run action_func */
1459 if (mp_bc_ncpus
> 1)
1460 thread_block(THREAD_CONTINUE_NULL
);
1462 clear_wait(current_thread(), THREAD_AWAKENED
);
1465 lck_mtx_unlock(&mp_bc_lock
);
1469 mp_cpus_kick(cpumask_t cpus
)
1472 boolean_t intrs_enabled
= FALSE
;
1474 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
1475 mp_safe_spin_lock(&x86_topo_lock
);
1477 for (cpu
= 0; cpu
< (cpu_t
) real_ncpus
; cpu
++) {
1478 if ((cpu
== (cpu_t
) cpu_number())
1479 || ((cpu_to_cpumask(cpu
) & cpus
) == 0)
1480 || !cpu_is_running(cpu
))
1485 lapic_send_ipi(cpu
, LAPIC_VECTOR(KICK
));
1488 simple_unlock(&x86_topo_lock
);
1489 ml_set_interrupts_enabled(intrs_enabled
);
1493 i386_activate_cpu(void)
1495 cpu_data_t
*cdp
= current_cpu_datap();
1497 assert(!ml_get_interrupts_enabled());
1499 if (!smp_initialized
) {
1500 cdp
->cpu_running
= TRUE
;
1504 mp_safe_spin_lock(&x86_topo_lock
);
1505 cdp
->cpu_running
= TRUE
;
1507 simple_unlock(&x86_topo_lock
);
1512 i386_deactivate_cpu(void)
1514 cpu_data_t
*cdp
= current_cpu_datap();
1516 assert(!ml_get_interrupts_enabled());
1518 KERNEL_DEBUG_CONSTANT(
1519 TRACE_MP_CPU_DEACTIVATE
| DBG_FUNC_START
,
1522 mp_safe_spin_lock(&x86_topo_lock
);
1523 cdp
->cpu_running
= FALSE
;
1524 simple_unlock(&x86_topo_lock
);
1527 * Move all of this cpu's timers to the master/boot cpu,
1528 * and poke it in case there's a sooner deadline for it to schedule.
1530 timer_queue_shutdown(&cdp
->rtclock_timer
.queue
);
1531 mp_cpus_call(cpu_to_cpumask(master_cpu
), ASYNC
, timer_queue_expire_local
, NULL
);
1535 #endif /* MONOTONIC */
1538 * Open an interrupt window
1539 * and ensure any pending IPI or timer is serviced
1541 mp_disable_preemption();
1542 ml_set_interrupts_enabled(TRUE
);
1544 while (cdp
->cpu_signals
&& x86_lcpu()->rtcDeadline
!= EndOfAllTime
)
1547 * Ensure there's no remaining timer deadline set
1548 * - AICPM may have left one active.
1552 ml_set_interrupts_enabled(FALSE
);
1553 mp_enable_preemption();
1555 KERNEL_DEBUG_CONSTANT(
1556 TRACE_MP_CPU_DEACTIVATE
| DBG_FUNC_END
,
1560 int pmsafe_debug
= 1;
1563 volatile boolean_t mp_kdp_trap
= FALSE
;
1564 volatile boolean_t mp_kdp_is_NMI
= FALSE
;
1565 volatile unsigned long mp_kdp_ncpus
;
1566 boolean_t mp_kdp_state
;
1570 mp_kdp_enter(boolean_t proceed_on_failure
)
1573 unsigned int ncpus
= 0;
1574 unsigned int my_cpu
;
1575 uint64_t tsc_timeout
;
1577 DBG("mp_kdp_enter()\n");
1580 * Here to enter the debugger.
1581 * In case of races, only one cpu is allowed to enter kdp after
1584 mp_kdp_state
= ml_set_interrupts_enabled(FALSE
);
1585 my_cpu
= cpu_number();
1587 if (my_cpu
== (unsigned) debugger_cpu
) {
1588 kprintf("\n\nRECURSIVE DEBUGGER ENTRY DETECTED\n\n");
1593 uint64_t start_time
= cpu_datap(my_cpu
)->debugger_entry_time
= mach_absolute_time();
1595 while (!locked
|| mp_kdp_trap
) {
1597 simple_unlock(&x86_topo_lock
);
1599 if (proceed_on_failure
) {
1600 if (mach_absolute_time() - start_time
> 500000000ll) {
1601 paniclog_append_noflush("mp_kdp_enter() can't get x86_topo_lock! Debugging anyway! #YOLO\n");
1604 locked
= simple_lock_try(&x86_topo_lock
);
1609 mp_safe_spin_lock(&x86_topo_lock
);
1613 if (locked
&& mp_kdp_trap
) {
1614 simple_unlock(&x86_topo_lock
);
1615 DBG("mp_kdp_enter() race lost\n");
1617 mp_kdp_wait(TRUE
, FALSE
);
1623 if (pmsafe_debug
&& !kdp_snapshot
)
1624 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_SAFE
);
1626 debugger_cpu
= my_cpu
;
1628 atomic_incl((volatile long *)&mp_kdp_ncpus
, 1);
1630 debugger_entry_time
= cpu_datap(my_cpu
)->debugger_entry_time
;
1633 * Deliver a nudge to other cpus, counting how many
1635 DBG("mp_kdp_enter() signaling other processors\n");
1636 if (force_immediate_debugger_NMI
== FALSE
) {
1637 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1638 if (cpu
== my_cpu
|| !cpu_is_running(cpu
))
1641 i386_signal_cpu(cpu
, MP_KDP
, ASYNC
);
1644 * Wait other processors to synchronize
1646 DBG("mp_kdp_enter() waiting for (%d) processors to suspend\n", ncpus
);
1649 * This timeout is rather arbitrary; we don't want to NMI
1650 * processors that are executing at potentially
1651 * "unsafe-to-interrupt" points such as the trampolines,
1652 * but neither do we want to lose state by waiting too long.
1654 tsc_timeout
= rdtsc64() + (LockTimeOutTSC
);
1656 while (mp_kdp_ncpus
!= ncpus
&& rdtsc64() < tsc_timeout
) {
1658 * A TLB shootdown request may be pending--this would
1659 * result in the requesting processor waiting in
1660 * PMAP_UPDATE_TLBS() until this processor deals with it.
1661 * Process it, so it can now enter mp_kdp_wait()
1663 handle_pending_TLB_flushes();
1666 /* If we've timed out, and some processor(s) are still unresponsive,
1667 * interrupt them with an NMI via the local APIC, iff a panic is
1670 if (panic_active()) {
1673 if (mp_kdp_ncpus
!= ncpus
) {
1674 unsigned int wait_cycles
= 0;
1675 if (proceed_on_failure
)
1676 paniclog_append_noflush("mp_kdp_enter() timed-out on cpu %d, NMI-ing\n", my_cpu
);
1678 DBG("mp_kdp_enter() timed-out on cpu %d, NMI-ing\n", my_cpu
);
1679 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1680 if (cpu
== my_cpu
|| !cpu_is_running(cpu
))
1682 if (cpu_signal_pending(cpu
, MP_KDP
)) {
1683 cpu_datap(cpu
)->cpu_NMI_acknowledged
= FALSE
;
1684 cpu_NMI_interrupt(cpu
);
1687 /* Wait again for the same timeout */
1688 tsc_timeout
= rdtsc64() + (LockTimeOutTSC
);
1689 while (mp_kdp_ncpus
!= ncpus
&& rdtsc64() < tsc_timeout
) {
1690 handle_pending_TLB_flushes();
1694 if (mp_kdp_ncpus
!= ncpus
) {
1695 paniclog_append_noflush("mp_kdp_enter() NMI pending on cpus:");
1696 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1697 if (cpu_is_running(cpu
) && !cpu_datap(cpu
)->cpu_NMI_acknowledged
)
1698 paniclog_append_noflush(" %d", cpu
);
1700 paniclog_append_noflush("\n");
1701 if (proceed_on_failure
) {
1702 paniclog_append_noflush("mp_kdp_enter() timed-out during %s wait after NMI;"
1703 "expected %u acks but received %lu after %u loops in %llu ticks\n",
1704 (locked
? "locked" : "unlocked"), ncpus
, mp_kdp_ncpus
, wait_cycles
, LockTimeOutTSC
);
1706 panic("mp_kdp_enter() timed-out during %s wait after NMI;"
1707 "expected %u acks but received %lu after %u loops in %llu ticks",
1708 (locked
? "locked" : "unlocked"), ncpus
, mp_kdp_ncpus
, wait_cycles
, LockTimeOutTSC
);
1714 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1715 if (cpu
== my_cpu
|| !cpu_is_running(cpu
))
1717 cpu_NMI_interrupt(cpu
);
1721 simple_unlock(&x86_topo_lock
);
1724 DBG("mp_kdp_enter() %d processors done %s\n",
1725 (int)mp_kdp_ncpus
, (mp_kdp_ncpus
== ncpus
) ? "OK" : "timed out");
1727 postcode(MP_KDP_ENTER
);
1731 mp_kdp_all_cpus_halted()
1733 unsigned int ncpus
= 0, cpu
= 0, my_cpu
= 0;
1735 my_cpu
= cpu_number();
1736 ncpus
= 1; /* current CPU */
1737 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1738 if (cpu
== my_cpu
|| !cpu_is_running(cpu
))
1743 return (mp_kdp_ncpus
== ncpus
);
1747 cpu_signal_pending(int cpu
, mp_event_t event
)
1749 volatile int *signals
= &cpu_datap(cpu
)->cpu_signals
;
1750 boolean_t retval
= FALSE
;
1752 if (i_bit(event
, signals
))
1757 long kdp_x86_xcpu_invoke(const uint16_t lcpu
, kdp_x86_xcpu_func_t func
,
1758 void *arg0
, void *arg1
)
1760 if (lcpu
> (real_ncpus
- 1))
1766 kdp_xcpu_call_func
.func
= func
;
1767 kdp_xcpu_call_func
.ret
= -1;
1768 kdp_xcpu_call_func
.arg0
= arg0
;
1769 kdp_xcpu_call_func
.arg1
= arg1
;
1770 kdp_xcpu_call_func
.cpu
= lcpu
;
1771 DBG("Invoking function %p on CPU %d\n", func
, (int32_t)lcpu
);
1772 while (kdp_xcpu_call_func
.cpu
!= KDP_XCPU_NONE
)
1774 return kdp_xcpu_call_func
.ret
;
1778 kdp_x86_xcpu_poll(void)
1780 if ((uint16_t)cpu_number() == kdp_xcpu_call_func
.cpu
) {
1781 kdp_xcpu_call_func
.ret
=
1782 kdp_xcpu_call_func
.func(kdp_xcpu_call_func
.arg0
,
1783 kdp_xcpu_call_func
.arg1
,
1785 kdp_xcpu_call_func
.cpu
= KDP_XCPU_NONE
;
1790 mp_kdp_wait(boolean_t flush
, boolean_t isNMI
)
1792 DBG("mp_kdp_wait()\n");
1794 current_cpu_datap()->debugger_ipi_time
= mach_absolute_time();
1796 /* If we've trapped due to a machine-check, save MCA registers */
1800 atomic_incl((volatile long *)&mp_kdp_ncpus
, 1);
1801 while (mp_kdp_trap
|| (isNMI
== TRUE
)) {
1803 * A TLB shootdown request may be pending--this would result
1804 * in the requesting processor waiting in PMAP_UPDATE_TLBS()
1805 * until this processor handles it.
1806 * Process it, so it can now enter mp_kdp_wait()
1809 handle_pending_TLB_flushes();
1811 kdp_x86_xcpu_poll();
1815 atomic_decl((volatile long *)&mp_kdp_ncpus
, 1);
1816 DBG("mp_kdp_wait() done\n");
1822 DBG("mp_kdp_exit()\n");
1824 atomic_decl((volatile long *)&mp_kdp_ncpus
, 1);
1826 debugger_exit_time
= mach_absolute_time();
1828 mp_kdp_trap
= FALSE
;
1831 /* Wait other processors to stop spinning. XXX needs timeout */
1832 DBG("mp_kdp_exit() waiting for processors to resume\n");
1833 while (mp_kdp_ncpus
> 0) {
1835 * a TLB shootdown request may be pending... this would result in the requesting
1836 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1837 * Process it, so it can now enter mp_kdp_wait()
1839 handle_pending_TLB_flushes();
1844 if (pmsafe_debug
&& !kdp_snapshot
)
1845 pmSafeMode(¤t_cpu_datap()->lcpu
, PM_SAFE_FL_NORMAL
);
1847 debugger_exit_time
= mach_absolute_time();
1849 DBG("mp_kdp_exit() done\n");
1850 (void) ml_set_interrupts_enabled(mp_kdp_state
);
1851 postcode(MP_KDP_EXIT
);
1854 #endif /* MACH_KDP */
1857 mp_recent_debugger_activity(void) {
1858 uint64_t abstime
= mach_absolute_time();
1859 return (((abstime
- debugger_entry_time
) < LastDebuggerEntryAllowance
) ||
1860 ((abstime
- debugger_exit_time
) < LastDebuggerEntryAllowance
));
1866 __unused processor_t processor
)
1872 processor_t processor
)
1874 int cpu
= processor
->cpu_id
;
1876 if (cpu
!= cpu_number()) {
1877 i386_signal_cpu(cpu
, MP_AST
, ASYNC
);
1878 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_AST
), cpu
, 1, 0, 0, 0);
1883 slave_machine_init(void *param
)
1886 * Here in process context, but with interrupts disabled.
1888 DBG("slave_machine_init() CPU%d\n", get_cpu_number());
1890 if (param
== FULL_SLAVE_INIT
) {
1896 cpu_machine_init(); /* Interrupts enabled hereafter */
1900 int cpu_number(void)
1902 return get_cpu_number();
1910 simple_lock_init(&cpu_warm_lock
, 0);
1911 queue_init(&cpu_warm_call_list
);
1912 for (i
= 0; i
< NUM_CPU_WARM_CALLS
; i
++) {
1913 enqueue_head(&cpu_warm_call_list
, (queue_entry_t
)&cpu_warm_call_arr
[i
]);
1918 grab_warm_timer_call()
1921 timer_call_t call
= NULL
;
1924 simple_lock(&cpu_warm_lock
);
1925 if (!queue_empty(&cpu_warm_call_list
)) {
1926 call
= (timer_call_t
) dequeue_head(&cpu_warm_call_list
);
1928 simple_unlock(&cpu_warm_lock
);
1935 free_warm_timer_call(timer_call_t call
)
1940 simple_lock(&cpu_warm_lock
);
1941 enqueue_head(&cpu_warm_call_list
, (queue_entry_t
)call
);
1942 simple_unlock(&cpu_warm_lock
);
1947 * Runs in timer call context (interrupts disabled).
1950 cpu_warm_timer_call_func(
1951 call_entry_param_t p0
,
1952 __unused call_entry_param_t p1
)
1954 free_warm_timer_call((timer_call_t
)p0
);
1959 * Runs with interrupts disabled on the CPU we wish to warm (i.e. CPU 0).
1965 cpu_warm_data_t cwdp
= (cpu_warm_data_t
)arg
;
1967 timer_call_enter(cwdp
->cwd_call
, cwdp
->cwd_deadline
, TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
1968 cwdp
->cwd_result
= 0;
1974 * Not safe to call with interrupts disabled.
1977 ml_interrupt_prewarm(
1980 struct cpu_warm_data cwd
;
1984 if (ml_get_interrupts_enabled() == FALSE
) {
1985 panic("%s: Interrupts disabled?\n", __FUNCTION__
);
1989 * If the platform doesn't need our help, say that we succeeded.
1991 if (!ml_get_interrupt_prewake_applicable()) {
1992 return KERN_SUCCESS
;
1996 * Grab a timer call to use.
1998 call
= grab_warm_timer_call();
2000 return KERN_RESOURCE_SHORTAGE
;
2003 timer_call_setup(call
, cpu_warm_timer_call_func
, call
);
2004 cwd
.cwd_call
= call
;
2005 cwd
.cwd_deadline
= deadline
;
2009 * For now, non-local interrupts happen on the master processor.
2011 ct
= mp_cpus_call(cpu_to_cpumask(master_cpu
), SYNC
, _cpu_warm_setup
, &cwd
);
2013 free_warm_timer_call(call
);
2014 return KERN_FAILURE
;
2016 return cwd
.cwd_result
;
2020 #if DEBUG || DEVELOPMENT
2022 kernel_spin(uint64_t spin_ns
)
2029 kprintf("kernel_spin(%llu) spinning uninterruptibly\n", spin_ns
);
2030 istate
= ml_set_interrupts_enabled(FALSE
);
2031 cdp
= current_cpu_datap();
2032 nanoseconds_to_absolutetime(spin_ns
, &spin_abs
);
2034 /* Fake interrupt handler entry for testing mp_interrupt_watchdog() */
2035 cdp
->cpu_int_event_time
= mach_absolute_time();
2036 cdp
->cpu_int_state
= (void *) USER_STATE(current_thread());
2038 deadline
= mach_absolute_time() + spin_ns
;
2039 while (mach_absolute_time() < deadline
)
2042 cdp
->cpu_int_event_time
= 0;
2043 cdp
->cpu_int_state
= NULL
;
2045 ml_set_interrupts_enabled(istate
);
2046 kprintf("kernel_spin() continuing\n");
2050 * Called from the scheduler's maintenance thread,
2051 * scan running processors for long-running ISRs and:
2052 * - panic if longer than LockTimeOut, or
2053 * - log if more than a quantum.
2056 mp_interrupt_watchdog(void)
2059 boolean_t intrs_enabled
= FALSE
;
2060 uint16_t cpu_int_num
;
2061 uint64_t cpu_int_event_time
;
2063 uint64_t cpu_int_duration
;
2065 x86_saved_state_t
*cpu_int_state
;
2067 if (__improbable(!mp_interrupt_watchdog_enabled
))
2070 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
2071 now
= mach_absolute_time();
2073 * While timeouts are not suspended,
2074 * check all other processors for long outstanding interrupt handling.
2077 cpu
< (cpu_t
) real_ncpus
&& !machine_timeout_suspended();
2079 if ((cpu
== (cpu_t
) cpu_number()) ||
2080 (!cpu_is_running(cpu
)))
2082 cpu_int_event_time
= cpu_datap(cpu
)->cpu_int_event_time
;
2083 if (cpu_int_event_time
== 0)
2085 if (__improbable(now
< cpu_int_event_time
))
2086 continue; /* skip due to inter-processor skew */
2087 cpu_int_state
= cpu_datap(cpu
)->cpu_int_state
;
2088 if (__improbable(cpu_int_state
== NULL
))
2089 /* The interrupt may have been dismissed */
2092 /* Here with a cpu handling an interrupt */
2094 cpu_int_duration
= now
- cpu_int_event_time
;
2095 if (__improbable(cpu_int_duration
> LockTimeOut
)) {
2096 cpu_int_num
= saved_state64(cpu_int_state
)->isf
.trapno
;
2097 cpu_rip
= saved_state64(cpu_int_state
)->isf
.rip
;
2098 vector_timed_out
= cpu_int_num
;
2099 NMIPI_panic(cpu_to_cpumask(cpu
), INTERRUPT_WATCHDOG
);
2100 panic("Interrupt watchdog, "
2101 "cpu: %d interrupt: 0x%x time: %llu..%llu state: %p RIP: 0x%llx",
2102 cpu
, cpu_int_num
, cpu_int_event_time
, now
, cpu_int_state
, cpu_rip
);
2104 } else if (__improbable(cpu_int_duration
> (uint64_t) std_quantum
)) {
2105 mp_interrupt_watchdog_events
++;
2106 cpu_int_num
= saved_state64(cpu_int_state
)->isf
.trapno
;
2107 cpu_rip
= saved_state64(cpu_int_state
)->isf
.rip
;
2108 ml_set_interrupts_enabled(intrs_enabled
);
2109 printf("Interrupt watchdog, "
2110 "cpu: %d interrupt: 0x%x time: %llu..%llu RIP: 0x%llx\n",
2111 cpu
, cpu_int_num
, cpu_int_event_time
, now
, cpu_rip
);
2116 ml_set_interrupts_enabled(intrs_enabled
);