X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/39236c6e673c41db228275375ab7fdb0f837b292..d26ffc64f583ab2d29df48f13518685602bc8832:/osfmk/i386/mp.c diff --git a/osfmk/i386/mp.c b/osfmk/i386/mp.c index dc50b4848..3b7232687 100644 --- a/osfmk/i386/mp.c +++ b/osfmk/i386/mp.c @@ -29,8 +29,8 @@ * @OSF_COPYRIGHT@ */ -#include #include +#include #include #include @@ -50,12 +50,14 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -75,13 +77,14 @@ #endif #include -#include -#include - #include #include +#if MONOTONIC +#include +#endif /* MONOTONIC */ + #if MP_DEBUG #define PAUSE delay(1000000) #define DBG(x...) kprintf(x) @@ -108,8 +111,6 @@ void i386_cpu_IPI(int cpu); #if MACH_KDP static void mp_kdp_wait(boolean_t flush, boolean_t isNMI); #endif /* MACH_KDP */ -static void mp_rendezvous_action(void); -static void mp_broadcast_action(void); #if MACH_KDP static boolean_t cpu_signal_pending(int cpu, mp_event_t event); @@ -120,7 +121,13 @@ boolean_t smp_initialized = FALSE; uint32_t TSC_sync_margin = 0xFFF; volatile boolean_t force_immediate_debugger_NMI = FALSE; volatile boolean_t pmap_tlb_flush_timeout = FALSE; -decl_simple_lock_data(,mp_kdp_lock); +#if DEBUG || DEVELOPMENT +boolean_t mp_interrupt_watchdog_enabled = TRUE; +uint32_t mp_interrupt_watchdog_events = 0; +#endif + +decl_simple_lock_data(,debugger_callback_lock); +struct debugger_callback *debugger_callback = NULL; decl_lck_mtx_data(static, mp_cpu_boot_lock); lck_mtx_ext_t mp_cpu_boot_lock_ext; @@ -163,11 +170,12 @@ lck_mtx_ext_t mp_bc_lock_ext; static volatile int debugger_cpu = -1; volatile long NMIPI_acks = 0; volatile long NMI_count = 0; +static NMI_reason_t NMI_panic_reason = NONE; +static int vector_timed_out; extern void NMI_cpus(void); static void mp_cpus_call_init(void); -static void mp_cpus_call_cpu_init(void); static void mp_cpus_call_action(void); static void mp_call_PM(void); @@ -178,7 +186,7 @@ boolean_t i386_smp_init(int nmi_vector, i386_intr_func_t nmi_handler, int ipi_vector, i386_intr_func_t ipi_handler); void i386_start_cpu(int lapic_id, int cpu_num); void i386_send_NMI(int cpu); - +void NMIPI_enable(boolean_t); #if GPROF /* * Initialize dummy structs for profiling. These aren't used but @@ -221,8 +229,8 @@ static void free_warm_timer_call(timer_call_t call); void smp_init(void) { - simple_lock_init(&mp_kdp_lock, 0); simple_lock_init(&mp_rv_lock, 0); + simple_lock_init(&debugger_callback_lock, 0); lck_grp_attr_setdefault(&smp_lck_grp_attr); lck_grp_init(&smp_lck_grp, "i386_smp", &smp_lck_grp_attr); lck_mtx_init_ext(&mp_cpu_boot_lock, &mp_cpu_boot_lock_ext, &smp_lck_grp, LCK_ATTR_NULL); @@ -239,7 +247,16 @@ smp_init(void) DBGLOG_CPU_INIT(master_cpu); mp_cpus_call_init(); - mp_cpus_call_cpu_init(); + mp_cpus_call_cpu_init(master_cpu); + +#if DEBUG || DEVELOPMENT + if (PE_parse_boot_argn("interrupt_watchdog", + &mp_interrupt_watchdog_enabled, + sizeof(mp_interrupt_watchdog_enabled))) { + kprintf("Interrupt watchdog %sabled\n", + mp_interrupt_watchdog_enabled ? "en" : "dis"); + } +#endif if (PE_parse_boot_argn("TSC_sync_margin", &TSC_sync_margin, sizeof(TSC_sync_margin))) { @@ -437,7 +454,7 @@ intel_startCPU( * Initialize (or re-initialize) the descriptor tables for this cpu. * Propagate processor mode to slave. */ - cpu_desc_init64(cpu_datap(slot_num)); + cpu_desc_init(cpu_datap(slot_num)); /* Serialize use of the slave boot stack, etc. */ lck_mtx_lock(&mp_cpu_boot_lock); @@ -487,6 +504,9 @@ MP_EVENT_NAME_DECL(); #endif /* MP_DEBUG */ +/* + * Note: called with NULL state when polling for TLB flush and cross-calls. + */ int cpu_signal_handler(x86_saved_state_t *regs) { @@ -509,7 +529,7 @@ cpu_signal_handler(x86_saved_state_t *regs) do { #if MACH_KDP - if (i_bit(MP_KDP, my_word) && regs != NULL) { + if (i_bit(MP_KDP, my_word)) { DBGLOG(cpu_handle,my_cpu,MP_KDP); i_bit_clear(MP_KDP, my_word); /* Ensure that the i386_kernel_state at the base of the @@ -529,22 +549,6 @@ cpu_signal_handler(x86_saved_state_t *regs) DBGLOG(cpu_handle,my_cpu,MP_TLB_FLUSH); i_bit_clear(MP_TLB_FLUSH, my_word); pmap_update_interrupt(); - } else if (i_bit(MP_AST, my_word)) { - DBGLOG(cpu_handle,my_cpu,MP_AST); - i_bit_clear(MP_AST, my_word); - ast_check(cpu_to_processor(my_cpu)); - } else if (i_bit(MP_RENDEZVOUS, my_word)) { - DBGLOG(cpu_handle,my_cpu,MP_RENDEZVOUS); - i_bit_clear(MP_RENDEZVOUS, my_word); - mp_rendezvous_action(); - } else if (i_bit(MP_BROADCAST, my_word)) { - DBGLOG(cpu_handle,my_cpu,MP_BROADCAST); - i_bit_clear(MP_BROADCAST, my_word); - mp_broadcast_action(); - } else if (i_bit(MP_CHUD, my_word)) { - DBGLOG(cpu_handle,my_cpu,MP_CHUD); - i_bit_clear(MP_CHUD, my_word); - chudxnu_cpu_signal_handler(); } else if (i_bit(MP_CALL, my_word)) { DBGLOG(cpu_handle,my_cpu,MP_CALL); i_bit_clear(MP_CALL, my_word); @@ -554,15 +558,26 @@ cpu_signal_handler(x86_saved_state_t *regs) i_bit_clear(MP_CALL_PM, my_word); mp_call_PM(); } + if (regs == NULL) { + /* Called to poll only for cross-calls and TLB flush */ + break; + } else if (i_bit(MP_AST, my_word)) { + DBGLOG(cpu_handle,my_cpu,MP_AST); + i_bit_clear(MP_AST, my_word); + ast_check(cpu_to_processor(my_cpu)); + } } while (*my_word); return 0; } +extern void kprintf_break_lock(void); static int NMIInterruptHandler(x86_saved_state_t *regs) { - void *stackptr; + void *stackptr; + char pstr[192]; + uint64_t now = mach_absolute_time(); if (panic_active() && !panicDebugging) { if (pmsafe_debug) @@ -577,23 +592,58 @@ NMIInterruptHandler(x86_saved_state_t *regs) __asm__ volatile("movq %%rbp, %0" : "=m" (stackptr)); if (cpu_number() == debugger_cpu) - goto NMExit; + goto NMExit; - if (spinlock_timed_out) { - char pstr[192]; - snprintf(&pstr[0], sizeof(pstr), "Panic(CPU %d): NMIPI for spinlock acquisition timeout, spinlock: %p, spinlock owner: %p, current_thread: %p, spinlock_owner_cpu: 0x%x\n", cpu_number(), spinlock_timed_out, (void *) spinlock_timed_out->interlock.lock_data, current_thread(), spinlock_owner_cpu); + if (NMI_panic_reason == SPINLOCK_TIMEOUT) { + snprintf(&pstr[0], sizeof(pstr), + "Panic(CPU %d, time %llu): NMIPI for spinlock acquisition timeout, spinlock: %p, spinlock owner: %p, current_thread: %p, spinlock_owner_cpu: 0x%x\n", + cpu_number(), now, spinlock_timed_out, (void *) spinlock_timed_out->interlock.lock_data, current_thread(), spinlock_owner_cpu); panic_i386_backtrace(stackptr, 64, &pstr[0], TRUE, regs); - } else if (pmap_tlb_flush_timeout == TRUE) { - char pstr[128]; - snprintf(&pstr[0], sizeof(pstr), "Panic(CPU %d): Unresponsive processor (this CPU did not acknowledge interrupts) TLB state:0x%x\n", cpu_number(), current_cpu_datap()->cpu_tlb_invalid); + } else if (NMI_panic_reason == TLB_FLUSH_TIMEOUT) { + snprintf(&pstr[0], sizeof(pstr), + "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: TLB flush timeout, TLB state:0x%x\n", + cpu_number(), now, current_cpu_datap()->cpu_tlb_invalid); panic_i386_backtrace(stackptr, 48, &pstr[0], TRUE, regs); + } else if (NMI_panic_reason == CROSSCALL_TIMEOUT) { + snprintf(&pstr[0], sizeof(pstr), + "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: cross-call timeout\n", + cpu_number(), now); + panic_i386_backtrace(stackptr, 64, &pstr[0], TRUE, regs); + } else if (NMI_panic_reason == INTERRUPT_WATCHDOG) { + snprintf(&pstr[0], sizeof(pstr), + "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: interrupt watchdog for vector 0x%x\n", + cpu_number(), now, vector_timed_out); + panic_i386_backtrace(stackptr, 64, &pstr[0], TRUE, regs); } - + #if MACH_KDP if (pmsafe_debug && !kdp_snapshot) pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_SAFE); current_cpu_datap()->cpu_NMI_acknowledged = TRUE; - mp_kdp_wait(FALSE, pmap_tlb_flush_timeout || spinlock_timed_out || panic_active()); + i_bit_clear(MP_KDP, ¤t_cpu_datap()->cpu_signals); + if (panic_active() || NMI_panic_reason != NONE) { + mp_kdp_wait(FALSE, TRUE); + } else if (!mp_kdp_trap && + !mp_kdp_is_NMI && + virtualized && (debug_boot_arg & DB_NMI)) { + /* + * Under a VMM with the debug boot-arg set, drop into kdp. + * Since an NMI is involved, there's a risk of contending with + * a panic. And side-effects of NMIs may result in entry into, + * and continuing from, the debugger being unreliable. + */ + if (__sync_bool_compare_and_swap(&mp_kdp_is_NMI, FALSE, TRUE)) { + kprintf_break_lock(); + kprintf("Debugger entry requested by NMI\n"); + kdp_i386_trap(T_DEBUG, saved_state64(regs), 0, 0); + printf("Debugger entry requested by NMI\n"); + mp_kdp_is_NMI = FALSE; + } else { + mp_kdp_wait(FALSE, FALSE); + } + } else { + mp_kdp_wait(FALSE, FALSE); + } if (pmsafe_debug && !kdp_snapshot) pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_NORMAL); #endif @@ -642,7 +692,7 @@ NMI_cpus(void) intrs_enabled = ml_set_interrupts_enabled(FALSE); for (cpu = 0; cpu < real_ncpus; cpu++) { - if (!cpu_datap(cpu)->cpu_running) + if (!cpu_is_running(cpu)) continue; cpu_datap(cpu)->cpu_NMI_acknowledged = FALSE; cpu_NMI_interrupt(cpu); @@ -726,66 +776,57 @@ i386_signal_cpu(int cpu, mp_event_t event, mp_sync_t mode) KERNEL_DEBUG(TRACE_MP_TLB_FLUSH | DBG_FUNC_END, cpu, 0, 0, 0, 0); } -/* - * Send event to all running cpus. - * Called with the topology locked. - */ -void -i386_signal_cpus(mp_event_t event, mp_sync_t mode) -{ - unsigned int cpu; - unsigned int my_cpu = cpu_number(); - - assert(hw_lock_held((hw_lock_t)&x86_topo_lock)); - - for (cpu = 0; cpu < real_ncpus; cpu++) { - if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running) - continue; - i386_signal_cpu(cpu, event, mode); - } -} - -/* - * Return the number of running cpus. - * Called with the topology locked. - */ -int -i386_active_cpus(void) -{ - unsigned int cpu; - unsigned int ncpus = 0; - - assert(hw_lock_held((hw_lock_t)&x86_topo_lock)); - - for (cpu = 0; cpu < real_ncpus; cpu++) { - if (cpu_datap(cpu)->cpu_running) - ncpus++; - } - return(ncpus); -} - /* * Helper function called when busy-waiting: panic if too long * a TSC-based time has elapsed since the start of the spin. */ -static void -mp_spin_timeout_check(uint64_t tsc_start, const char *msg) +static boolean_t +mp_spin_timeout(uint64_t tsc_start) { uint64_t tsc_timeout; cpu_pause(); if (machine_timeout_suspended()) - return; + return FALSE; /* * The timeout is 4 * the spinlock timeout period * unless we have serial console printing (kprintf) enabled * in which case we allow an even greater margin. */ - tsc_timeout = disable_serial_output ? (uint64_t) LockTimeOutTSC << 2 - : (uint64_t) LockTimeOutTSC << 4; - if (rdtsc64() > tsc_start + tsc_timeout) - panic("%s: spin timeout", msg); + tsc_timeout = disable_serial_output ? LockTimeOutTSC << 2 + : LockTimeOutTSC << 4; + return (rdtsc64() > tsc_start + tsc_timeout); +} + +/* + * Helper function to take a spinlock while ensuring that incoming IPIs + * are still serviced if interrupts are masked while we spin. + * Returns current interrupt state. + */ +boolean_t +mp_safe_spin_lock(usimple_lock_t lock) +{ + if (ml_get_interrupts_enabled()) { + simple_lock(lock); + return TRUE; + } else { + uint64_t tsc_spin_start = rdtsc64(); + while (!simple_lock_try(lock)) { + cpu_signal_handler(NULL); + if (mp_spin_timeout(tsc_spin_start)) { + uint32_t lock_cpu; + uintptr_t lowner = (uintptr_t) + lock->interlock.lock_data; + spinlock_timed_out = lock; + lock_cpu = spinlock_timeout_NMI(lowner); + NMIPI_panic(cpu_to_cpumask(lock_cpu), SPINLOCK_TIMEOUT); + panic("mp_safe_spin_lock() timed out, lock: %p, owner thread: 0x%lx, current_thread: %p, owner on CPU 0x%x, time: %llu", + lock, lowner, current_thread(), lock_cpu, mach_absolute_time()); + } + } + return FALSE; + } } /* @@ -803,7 +844,7 @@ mp_spin_timeout_check(uint64_t tsc_start, const char *msg) */ static void -mp_rendezvous_action(void) +mp_rendezvous_action(__unused void *null) { boolean_t intrs_enabled; uint64_t tsc_spin_start; @@ -817,12 +858,14 @@ mp_rendezvous_action(void) /* spin on entry rendezvous */ atomic_incl(&mp_rv_entry, 1); tsc_spin_start = rdtsc64(); + while (mp_rv_entry < mp_rv_ncpus) { /* poll for pesky tlb flushes if interrupts disabled */ if (!intrs_enabled) handle_pending_TLB_flushes(); - mp_spin_timeout_check(tsc_spin_start, - "mp_rendezvous_action() entry"); + if (mp_spin_timeout(tsc_spin_start)) { + 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()); + } } /* action function */ @@ -835,8 +878,8 @@ mp_rendezvous_action(void) while (mp_rv_exit < mp_rv_ncpus) { if (!intrs_enabled) handle_pending_TLB_flushes(); - mp_spin_timeout_check(tsc_spin_start, - "mp_rendezvous_action() exit"); + if (mp_spin_timeout(tsc_spin_start)) + 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()); } /* teardown function */ @@ -866,7 +909,7 @@ mp_rendezvous(void (*setup_func)(void *), } /* obtain rendezvous lock */ - simple_lock(&mp_rv_lock); + (void) mp_safe_spin_lock(&mp_rv_lock); /* set static function pointers */ mp_rv_setup_func = setup_func; @@ -882,13 +925,10 @@ mp_rendezvous(void (*setup_func)(void *), * signal other processors, which will call mp_rendezvous_action() * with interrupts disabled */ - simple_lock(&x86_topo_lock); - mp_rv_ncpus = i386_active_cpus(); - i386_signal_cpus(MP_RENDEZVOUS, ASYNC); - simple_unlock(&x86_topo_lock); + mp_rv_ncpus = mp_cpus_call(CPUMASK_OTHERS, NOSYNC, &mp_rendezvous_action, NULL) + 1; /* call executor function on this cpu */ - mp_rendezvous_action(); + mp_rendezvous_action(NULL); /* * Spin for everyone to complete. @@ -897,7 +937,8 @@ mp_rendezvous(void (*setup_func)(void *), */ tsc_spin_start = rdtsc64(); while (mp_rv_complete < mp_rv_ncpus) { - mp_spin_timeout_check(tsc_spin_start, "mp_rendezvous()"); + if (mp_spin_timeout(tsc_spin_start)) + panic("mp_rendezvous() timeout: %ld of %d responses, start: 0x%llx, cur: 0x%llx", mp_rv_complete, mp_rv_ncpus, tsc_spin_start, rdtsc64()); } /* Tidy up */ @@ -955,7 +996,7 @@ typedef struct { void (*func)(void *,void *); /* routine to call */ void *arg0; /* routine's 1st arg */ void *arg1; /* routine's 2nd arg */ - volatile long *countp; /* completion counter */ + cpumask_t *maskp; /* completion response mask */ } mp_call_t; @@ -978,12 +1019,44 @@ mp_call_head_lock(mp_call_queue_t *cqp) return intrs_enabled; } +/* + * Deliver an NMIPI to a set of processors to cause them to panic . + */ +void +NMIPI_panic(cpumask_t cpu_mask, NMI_reason_t why) { + unsigned int cpu, cpu_bit; + uint64_t deadline; + + NMIPI_enable(TRUE); + NMI_panic_reason = why; + + for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) { + if ((cpu_mask & cpu_bit) == 0) + continue; + cpu_datap(cpu)->cpu_NMI_acknowledged = FALSE; + cpu_NMI_interrupt(cpu); + } + + /* Wait (only so long) for NMi'ed cpus to respond */ + deadline = mach_absolute_time() + LockTimeOut; + for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) { + if ((cpu_mask & cpu_bit) == 0) + continue; + while (!cpu_datap(cpu)->cpu_NMI_acknowledged && + mach_absolute_time() < deadline) { + cpu_pause(); + } + } +} + +#if MACH_ASSERT static inline boolean_t mp_call_head_is_locked(mp_call_queue_t *cqp) { return !ml_get_interrupts_enabled() && hw_lock_held((hw_lock_t)&cqp->lock); } +#endif static inline void mp_call_head_unlock(mp_call_queue_t *cqp, boolean_t intrs_enabled) @@ -1049,20 +1122,16 @@ mp_cpus_call_init(void) } /* - * Called by each processor to add call buffers to the free list + * Called at processor registration to add call buffers to the free list * and to initialize the per-cpu call queue. - * Also called but ignored on slave processors on re-start/wake. */ -static void -mp_cpus_call_cpu_init(void) +void +mp_cpus_call_cpu_init(int cpu) { int i; - mp_call_queue_t *cqp = &mp_cpus_call_head[cpu_number()]; + mp_call_queue_t *cqp = &mp_cpus_call_head[cpu]; mp_call_t *callp; - if (cqp->queue.next != NULL) - return; /* restart/wake case: called already */ - simple_lock_init(&cqp->lock, 0); queue_init(&cqp->queue); for (i = 0; i < MP_CPUS_CALL_BUFS_PER_CPU; i++) { @@ -1070,7 +1139,7 @@ mp_cpus_call_cpu_init(void) mp_call_free(callp); } - DBG("mp_cpus_call_init() done on cpu %d\n", cpu_number()); + DBG("mp_cpus_call_init(%d) done\n", cpu); } /* @@ -1096,12 +1165,13 @@ mp_cpus_call_action(void) mp_call_head_unlock(cqp, intrs_enabled); KERNEL_DEBUG_CONSTANT( TRACE_MP_CPUS_CALL_ACTION, - call.func, call.arg0, call.arg1, call.countp, 0); + VM_KERNEL_UNSLIDE(call.func), VM_KERNEL_UNSLIDE_OR_PERM(call.arg0), + VM_KERNEL_UNSLIDE_OR_PERM(call.arg1), VM_KERNEL_ADDRPERM(call.maskp), 0); call.func(call.arg0, call.arg1); (void) mp_call_head_lock(cqp); } - if (call.countp != NULL) - atomic_incl(call.countp, 1); + if (call.maskp != NULL) + i_bit_set(cpu_number(), call.maskp); } mp_call_head_unlock(cqp, intrs_enabled); } @@ -1132,29 +1202,36 @@ mp_cpus_call( (void (*)(void *,void *))action_func, arg, NULL, - NULL, NULL); } static void mp_cpus_call_wait(boolean_t intrs_enabled, - long mp_cpus_signals, - volatile long *mp_cpus_calls) + cpumask_t cpus_called, + cpumask_t *cpus_responded) { mp_call_queue_t *cqp; uint64_t tsc_spin_start; + assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0); cqp = &mp_cpus_call_head[cpu_number()]; tsc_spin_start = rdtsc64(); - while (*mp_cpus_calls < mp_cpus_signals) { + while (*cpus_responded != cpus_called) { if (!intrs_enabled) { /* Sniffing w/o locking */ if (!queue_empty(&cqp->queue)) mp_cpus_call_action(); - handle_pending_TLB_flushes(); + cpu_signal_handler(NULL); + } + if (mp_spin_timeout(tsc_spin_start)) { + cpumask_t cpus_unresponsive; + + cpus_unresponsive = cpus_called & ~(*cpus_responded); + NMIPI_panic(cpus_unresponsive, CROSSCALL_TIMEOUT); + panic("mp_cpus_call_wait() timeout, cpus: 0x%llx", + cpus_unresponsive); } - mp_spin_timeout_check(tsc_spin_start, "mp_cpus_call_wait()"); } } @@ -1165,21 +1242,20 @@ mp_cpus_call1( void (*action_func)(void *, void *), void *arg0, void *arg1, - cpumask_t *cpus_calledp, - cpumask_t *cpus_notcalledp) + cpumask_t *cpus_calledp) { - cpu_t cpu; + cpu_t cpu = 0; boolean_t intrs_enabled = FALSE; boolean_t call_self = FALSE; cpumask_t cpus_called = 0; - cpumask_t cpus_notcalled = 0; - long mp_cpus_signals = 0; - volatile long mp_cpus_calls = 0; + cpumask_t cpus_responded = 0; + long cpus_call_count = 0; uint64_t tsc_spin_start; + boolean_t topo_lock; KERNEL_DEBUG_CONSTANT( TRACE_MP_CPUS_CALL | DBG_FUNC_START, - cpus, mode, VM_KERNEL_UNSLIDE(action_func), arg0, arg1); + cpus, mode, VM_KERNEL_UNSLIDE(action_func), VM_KERNEL_UNSLIDE_OR_PERM(arg0), VM_KERNEL_UNSLIDE_OR_PERM(arg1)); if (!smp_initialized) { if ((cpus & CPUMASK_SELF) == 0) @@ -1195,94 +1271,89 @@ mp_cpus_call1( /* * Queue the call for each non-local requested cpu. - * The topo lock is not taken. Instead we sniff the cpu_running state - * and then re-check it after taking the call lock. A cpu being taken - * offline runs the action function after clearing the cpu_running. + * This is performed under the topo lock to prevent changes to + * cpus online state and to prevent concurrent rendezvouses -- + * although an exception is made if we're calling only the master + * processor since that always remains active. Note: this exception + * is expected for longterm timer nosync cross-calls to the master cpu. */ - mp_disable_preemption(); /* interrupts may be enabled */ - tsc_spin_start = rdtsc64(); + mp_disable_preemption(); + intrs_enabled = ml_get_interrupts_enabled(); + topo_lock = (cpus != cpu_to_cpumask(master_cpu)); + if (topo_lock) { + ml_set_interrupts_enabled(FALSE); + (void) mp_safe_spin_lock(&x86_topo_lock); + } for (cpu = 0; cpu < (cpu_t) real_ncpus; cpu++) { if (((cpu_to_cpumask(cpu) & cpus) == 0) || - !cpu_datap(cpu)->cpu_running) + !cpu_is_running(cpu)) continue; + tsc_spin_start = rdtsc64(); if (cpu == (cpu_t) cpu_number()) { /* * We don't IPI ourself and if calling asynchronously, * we defer our call until we have signalled all others. */ call_self = TRUE; - cpus_called |= cpu_to_cpumask(cpu); if (mode == SYNC && action_func != NULL) { KERNEL_DEBUG_CONSTANT( TRACE_MP_CPUS_CALL_LOCAL, VM_KERNEL_UNSLIDE(action_func), - arg0, arg1, 0, 0); + VM_KERNEL_UNSLIDE_OR_PERM(arg0), VM_KERNEL_UNSLIDE_OR_PERM(arg1), 0, 0); action_func(arg0, arg1); } } else { /* * Here to queue a call to cpu and IPI. - * Spinning for request buffer unless NOSYNC. */ mp_call_t *callp = NULL; mp_call_queue_t *cqp = &mp_cpus_call_head[cpu]; + boolean_t intrs_inner; queue_call: if (callp == NULL) callp = mp_call_alloc(); - intrs_enabled = mp_call_head_lock(cqp); - if (!cpu_datap(cpu)->cpu_running) { - mp_call_head_unlock(cqp, intrs_enabled); - continue; - } - if (mode == NOSYNC) { - if (callp == NULL) { - cpus_notcalled |= cpu_to_cpumask(cpu); - mp_call_head_unlock(cqp, intrs_enabled); - KERNEL_DEBUG_CONSTANT( - TRACE_MP_CPUS_CALL_NOBUF, - cpu, 0, 0, 0, 0); - continue; - } - callp->countp = NULL; - } else { - if (callp == NULL) { - mp_call_head_unlock(cqp, intrs_enabled); - KERNEL_DEBUG_CONSTANT( - TRACE_MP_CPUS_CALL_NOBUF, - cpu, 0, 0, 0, 0); - if (!intrs_enabled) { - /* Sniffing w/o locking */ - if (!queue_empty(&cqp->queue)) - mp_cpus_call_action(); - handle_pending_TLB_flushes(); - } - mp_spin_timeout_check( - tsc_spin_start, - "mp_cpus_call1()"); - goto queue_call; + intrs_inner = mp_call_head_lock(cqp); + if (callp == NULL) { + mp_call_head_unlock(cqp, intrs_inner); + KERNEL_DEBUG_CONSTANT( + TRACE_MP_CPUS_CALL_NOBUF, + cpu, 0, 0, 0, 0); + if (!intrs_inner) { + /* Sniffing w/o locking */ + if (!queue_empty(&cqp->queue)) + mp_cpus_call_action(); + handle_pending_TLB_flushes(); } - callp->countp = &mp_cpus_calls; + if (mp_spin_timeout(tsc_spin_start)) + panic("mp_cpus_call1() timeout start: 0x%llx, cur: 0x%llx", + tsc_spin_start, rdtsc64()); + goto queue_call; } + callp->maskp = (mode == NOSYNC) ? NULL : &cpus_responded; callp->func = action_func; callp->arg0 = arg0; callp->arg1 = arg1; mp_call_enqueue_locked(cqp, callp); - mp_cpus_signals++; + cpus_call_count++; cpus_called |= cpu_to_cpumask(cpu); i386_signal_cpu(cpu, MP_CALL, ASYNC); - mp_call_head_unlock(cqp, intrs_enabled); + mp_call_head_unlock(cqp, intrs_inner); if (mode == SYNC) { - mp_cpus_call_wait(intrs_enabled, mp_cpus_signals, &mp_cpus_calls); + mp_cpus_call_wait(intrs_inner, cpus_called, &cpus_responded); } } } + if (topo_lock) { + simple_unlock(&x86_topo_lock); + ml_set_interrupts_enabled(intrs_enabled); + } /* Call locally if mode not SYNC */ if (mode != SYNC && call_self ) { KERNEL_DEBUG_CONSTANT( TRACE_MP_CPUS_CALL_LOCAL, - VM_KERNEL_UNSLIDE(action_func), arg0, arg1, 0, 0); + VM_KERNEL_UNSLIDE(action_func), VM_KERNEL_UNSLIDE_OR_PERM(arg0), VM_KERNEL_UNSLIDE_OR_PERM(arg1), 0, 0); if (action_func != NULL) { ml_set_interrupts_enabled(FALSE); action_func(arg0, arg1); @@ -1290,32 +1361,32 @@ mp_cpus_call1( } } + /* For ASYNC, now wait for all signaled cpus to complete their calls */ + if (mode == ASYNC) + mp_cpus_call_wait(intrs_enabled, cpus_called, &cpus_responded); + /* Safe to allow pre-emption now */ mp_enable_preemption(); - /* For ASYNC, now wait for all signaled cpus to complete their calls */ - if (mode == ASYNC) { - mp_cpus_call_wait(intrs_enabled, mp_cpus_signals, &mp_cpus_calls); - } - out: - cpu = (cpu_t) mp_cpus_signals + (call_self ? 1 : 0); + if (call_self){ + cpus_called |= cpu_to_cpumask(cpu); + cpus_call_count++; + } if (cpus_calledp) *cpus_calledp = cpus_called; - if (cpus_notcalledp) - *cpus_notcalledp = cpus_notcalled; KERNEL_DEBUG_CONSTANT( TRACE_MP_CPUS_CALL | DBG_FUNC_END, - cpu, cpus_called, cpus_notcalled, 0, 0); + cpus_call_count, cpus_called, 0, 0, 0); - return cpu; + return (cpu_t) cpus_call_count; } static void -mp_broadcast_action(void) +mp_broadcast_action(__unused void *null) { /* call action function */ if (mp_bc_action_func != NULL) @@ -1354,16 +1425,14 @@ mp_broadcast( /* * signal other processors, which will call mp_broadcast_action() */ - simple_lock(&x86_topo_lock); - mp_bc_ncpus = i386_active_cpus(); /* total including this cpu */ - mp_bc_count = mp_bc_ncpus; - i386_signal_cpus(MP_BROADCAST, ASYNC); + mp_bc_count = real_ncpus; /* assume max possible active */ + mp_bc_ncpus = mp_cpus_call(CPUMASK_OTHERS, NOSYNC, *mp_broadcast_action, NULL) + 1; + atomic_decl(&mp_bc_count, real_ncpus - mp_bc_ncpus); /* subtract inactive */ /* call executor function on this cpu */ - mp_broadcast_action(); - simple_unlock(&x86_topo_lock); + mp_broadcast_action(NULL); - /* block for all cpus to have run action_func */ + /* block for other cpus to have run action_func */ if (mp_bc_ncpus > 1) thread_block(THREAD_CONTINUE_NULL); else @@ -1373,6 +1442,30 @@ mp_broadcast( lck_mtx_unlock(&mp_bc_lock); } +void +mp_cpus_kick(cpumask_t cpus) +{ + cpu_t cpu; + boolean_t intrs_enabled = FALSE; + + intrs_enabled = ml_set_interrupts_enabled(FALSE); + mp_safe_spin_lock(&x86_topo_lock); + + for (cpu = 0; cpu < (cpu_t) real_ncpus; cpu++) { + if ((cpu == (cpu_t) cpu_number()) + || ((cpu_to_cpumask(cpu) & cpus) == 0) + || !cpu_is_running(cpu)) + { + continue; + } + + lapic_send_ipi(cpu, LAPIC_VECTOR(KICK)); + } + + simple_unlock(&x86_topo_lock); + ml_set_interrupts_enabled(intrs_enabled); +} + void i386_activate_cpu(void) { @@ -1385,7 +1478,7 @@ i386_activate_cpu(void) return; } - simple_lock(&x86_topo_lock); + mp_safe_spin_lock(&x86_topo_lock); cdp->cpu_running = TRUE; started_cpu(); simple_unlock(&x86_topo_lock); @@ -1403,7 +1496,7 @@ i386_deactivate_cpu(void) TRACE_MP_CPU_DEACTIVATE | DBG_FUNC_START, 0, 0, 0, 0, 0); - simple_lock(&x86_topo_lock); + mp_safe_spin_lock(&x86_topo_lock); cdp->cpu_running = FALSE; simple_unlock(&x86_topo_lock); @@ -1414,6 +1507,10 @@ i386_deactivate_cpu(void) timer_queue_shutdown(&cdp->rtclock_timer.queue); mp_cpus_call(cpu_to_cpumask(master_cpu), ASYNC, timer_queue_expire_local, NULL); +#if MONOTONIC + mt_cpu_down(cdp); +#endif /* MONOTONIC */ + /* * Open an interrupt window * and ensure any pending IPI or timer is serviced @@ -1441,12 +1538,13 @@ int pmsafe_debug = 1; #if MACH_KDP volatile boolean_t mp_kdp_trap = FALSE; +volatile boolean_t mp_kdp_is_NMI = FALSE; volatile unsigned long mp_kdp_ncpus; boolean_t mp_kdp_state; void -mp_kdp_enter(void) +mp_kdp_enter(boolean_t proceed_on_failure) { unsigned int cpu; unsigned int ncpus = 0; @@ -1455,11 +1553,6 @@ mp_kdp_enter(void) DBG("mp_kdp_enter()\n"); -#if DEBUG - if (!smp_initialized) - simple_lock_init(&mp_kdp_lock, 0); -#endif - /* * Here to enter the debugger. * In case of races, only one cpu is allowed to enter kdp after @@ -1474,26 +1567,44 @@ mp_kdp_enter(void) return; } - cpu_datap(my_cpu)->debugger_entry_time = mach_absolute_time(); - simple_lock(&mp_kdp_lock); - - if (pmsafe_debug && !kdp_snapshot) - pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_SAFE); + uint64_t start_time = cpu_datap(my_cpu)->debugger_entry_time = mach_absolute_time(); + int locked = 0; + while (!locked || mp_kdp_trap) { + if (locked) { + simple_unlock(&x86_topo_lock); + } + if (proceed_on_failure) { + if (mach_absolute_time() - start_time > 500000000ll) { + kprintf("mp_kdp_enter() can't get x86_topo_lock! Debugging anyway! #YOLO\n"); + break; + } + locked = simple_lock_try(&x86_topo_lock); + if (!locked) { + cpu_pause(); + } + } else { + mp_safe_spin_lock(&x86_topo_lock); + locked = TRUE; + } - while (mp_kdp_trap) { - simple_unlock(&mp_kdp_lock); - DBG("mp_kdp_enter() race lost\n"); + if (locked && mp_kdp_trap) { + simple_unlock(&x86_topo_lock); + DBG("mp_kdp_enter() race lost\n"); #if MACH_KDP - mp_kdp_wait(TRUE, FALSE); + mp_kdp_wait(TRUE, FALSE); #endif - simple_lock(&mp_kdp_lock); + locked = FALSE; + } } + + if (pmsafe_debug && !kdp_snapshot) + pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_SAFE); + debugger_cpu = my_cpu; ncpus = 1; - mp_kdp_ncpus = 1; /* self */ + atomic_incl((volatile long *)&mp_kdp_ncpus, 1); mp_kdp_trap = TRUE; debugger_entry_time = cpu_datap(my_cpu)->debugger_entry_time; - simple_unlock(&mp_kdp_lock); /* * Deliver a nudge to other cpus, counting how many @@ -1501,7 +1612,7 @@ mp_kdp_enter(void) DBG("mp_kdp_enter() signaling other processors\n"); if (force_immediate_debugger_NMI == FALSE) { for (cpu = 0; cpu < real_ncpus; cpu++) { - if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running) + if (cpu == my_cpu || !cpu_is_running(cpu)) continue; ncpus++; i386_signal_cpu(cpu, MP_KDP, ASYNC); @@ -1517,10 +1628,7 @@ mp_kdp_enter(void) * "unsafe-to-interrupt" points such as the trampolines, * but neither do we want to lose state by waiting too long. */ - tsc_timeout = rdtsc64() + (ncpus * 1000 * 1000 * 10ULL); - - if (virtualized) - tsc_timeout = ~0ULL; + tsc_timeout = rdtsc64() + (LockTimeOutTSC); while (mp_kdp_ncpus != ncpus && rdtsc64() < tsc_timeout) { /* @@ -1533,24 +1641,45 @@ mp_kdp_enter(void) cpu_pause(); } /* If we've timed out, and some processor(s) are still unresponsive, - * interrupt them with an NMI via the local APIC. + * interrupt them with an NMI via the local APIC, iff a panic is + * in progress. */ + if (panic_active()) { + NMIPI_enable(TRUE); + } if (mp_kdp_ncpus != ncpus) { + cpumask_t cpus_NMI_pending = 0; + DBG("mp_kdp_enter() timed-out on cpu %d, NMI-ing\n", my_cpu); for (cpu = 0; cpu < real_ncpus; cpu++) { - if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running) + if (cpu == my_cpu || !cpu_is_running(cpu)) continue; - if (cpu_signal_pending(cpu, MP_KDP)) + if (cpu_signal_pending(cpu, MP_KDP)) { + cpus_NMI_pending |= cpu_to_cpumask(cpu); cpu_NMI_interrupt(cpu); + } + } + /* Wait again for the same timeout */ + tsc_timeout = rdtsc64() + (LockTimeOutTSC); + while (mp_kdp_ncpus != ncpus && rdtsc64() < tsc_timeout) { + handle_pending_TLB_flushes(); + cpu_pause(); + } + if (mp_kdp_ncpus != ncpus) { + kdb_printf("mp_kdp_enter(): %llu, %lu, %u TIMED-OUT WAITING FOR NMI-ACK, PROCEEDING\n", cpus_NMI_pending, mp_kdp_ncpus, ncpus); } } } else for (cpu = 0; cpu < real_ncpus; cpu++) { - if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running) + if (cpu == my_cpu || !cpu_is_running(cpu)) continue; cpu_NMI_interrupt(cpu); } + if (locked) { + simple_unlock(&x86_topo_lock); + } + DBG("mp_kdp_enter() %d processors done %s\n", (int)mp_kdp_ncpus, (mp_kdp_ncpus == ncpus) ? "OK" : "timed out"); @@ -1604,8 +1733,7 @@ static void mp_kdp_wait(boolean_t flush, boolean_t isNMI) { DBG("mp_kdp_wait()\n"); - /* If an I/O port has been specified as a debugging aid, issue a read */ - panic_io_port_read(); + current_cpu_datap()->debugger_ipi_time = mach_absolute_time(); #if CONFIG_MCA /* If we've trapped due to a machine-check, save MCA registers */ @@ -1663,12 +1791,13 @@ mp_kdp_exit(void) DBG("mp_kdp_exit() done\n"); (void) ml_set_interrupts_enabled(mp_kdp_state); - postcode(0); + postcode(MP_KDP_EXIT); } + #endif /* MACH_KDP */ boolean_t -mp_recent_debugger_activity() { +mp_recent_debugger_activity(void) { uint64_t abstime = mach_absolute_time(); return (((abstime - debugger_entry_time) < LastDebuggerEntryAllowance) || ((abstime - debugger_exit_time) < LastDebuggerEntryAllowance)); @@ -1706,11 +1835,8 @@ slave_machine_init(void *param) * Cold start */ clock_init(); - cpu_machine_init(); /* Interrupts enabled hereafter */ - mp_cpus_call_cpu_init(); - } else { - cpu_machine_init(); /* Interrupts enabled hereafter */ } + cpu_machine_init(); /* Interrupts enabled hereafter */ } #undef cpu_number @@ -1833,3 +1959,103 @@ ml_interrupt_prewarm( return cwd.cwd_result; } } + +#if DEBUG || DEVELOPMENT +void +kernel_spin(uint64_t spin_ns) +{ + boolean_t istate; + uint64_t spin_abs; + uint64_t deadline; + cpu_data_t *cdp; + + kprintf("kernel_spin(%llu) spinning uninterruptibly\n", spin_ns); + istate = ml_set_interrupts_enabled(FALSE); + cdp = current_cpu_datap(); + nanoseconds_to_absolutetime(spin_ns, &spin_abs); + + /* Fake interrupt handler entry for testing mp_interrupt_watchdog() */ + cdp->cpu_int_event_time = mach_absolute_time(); + cdp->cpu_int_state = (void *) USER_STATE(current_thread()); + + deadline = mach_absolute_time() + spin_ns; + while (mach_absolute_time() < deadline) + cpu_pause(); + + cdp->cpu_int_event_time = 0; + cdp->cpu_int_state = NULL; + + ml_set_interrupts_enabled(istate); + kprintf("kernel_spin() continuing\n"); +} + +/* + * Called from the scheduler's maintenance thread, + * scan running processors for long-running ISRs and: + * - panic if longer than LockTimeOut, or + * - log if more than a quantum. + */ +void +mp_interrupt_watchdog(void) +{ + cpu_t cpu; + boolean_t intrs_enabled = FALSE; + uint16_t cpu_int_num; + uint64_t cpu_int_event_time; + uint64_t cpu_rip; + uint64_t cpu_int_duration; + uint64_t now; + x86_saved_state_t *cpu_int_state; + + if (__improbable(!mp_interrupt_watchdog_enabled)) + return; + + intrs_enabled = ml_set_interrupts_enabled(FALSE); + now = mach_absolute_time(); + /* + * While timeouts are not suspended, + * check all other processors for long outstanding interrupt handling. + */ + for (cpu = 0; + cpu < (cpu_t) real_ncpus && !machine_timeout_suspended(); + cpu++) { + if ((cpu == (cpu_t) cpu_number()) || + (!cpu_is_running(cpu))) + continue; + cpu_int_event_time = cpu_datap(cpu)->cpu_int_event_time; + if (cpu_int_event_time == 0) + continue; + if (__improbable(now < cpu_int_event_time)) + continue; /* skip due to inter-processor skew */ + cpu_int_state = cpu_datap(cpu)->cpu_int_state; + if (__improbable(cpu_int_state == NULL)) + /* The interrupt may have been dismissed */ + continue; + + /* Here with a cpu handling an interrupt */ + + cpu_int_duration = now - cpu_int_event_time; + if (__improbable(cpu_int_duration > LockTimeOut)) { + cpu_int_num = saved_state64(cpu_int_state)->isf.trapno; + cpu_rip = saved_state64(cpu_int_state)->isf.rip; + vector_timed_out = cpu_int_num; + NMIPI_panic(cpu_to_cpumask(cpu), INTERRUPT_WATCHDOG); + panic("Interrupt watchdog, " + "cpu: %d interrupt: 0x%x time: %llu..%llu state: %p RIP: 0x%llx", + cpu, cpu_int_num, cpu_int_event_time, now, cpu_int_state, cpu_rip); + /* NOT REACHED */ + } else if (__improbable(cpu_int_duration > (uint64_t) std_quantum)) { + mp_interrupt_watchdog_events++; + cpu_int_num = saved_state64(cpu_int_state)->isf.trapno; + cpu_rip = saved_state64(cpu_int_state)->isf.rip; + ml_set_interrupts_enabled(intrs_enabled); + printf("Interrupt watchdog, " + "cpu: %d interrupt: 0x%x time: %llu..%llu RIP: 0x%llx\n", + cpu, cpu_int_num, cpu_int_event_time, now, cpu_rip); + return; + } + } + + ml_set_interrupts_enabled(intrs_enabled); +} +#endif