]>
Commit | Line | Data |
---|---|---|
55e303ae | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2000-2020 Apple Inc. All rights reserved. |
55e303ae | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
55e303ae A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | ||
55e303ae | 32 | #include <mach_kdp.h> |
39037602 | 33 | #include <kdp/kdp_internal.h> |
55e303ae | 34 | #include <mach_ldebug.h> |
91447636 A |
35 | |
36 | #include <mach/mach_types.h> | |
37 | #include <mach/kern_return.h> | |
38 | ||
39 | #include <kern/kern_types.h> | |
40 | #include <kern/startup.h> | |
c910b4d9 | 41 | #include <kern/timer_queue.h> |
91447636 A |
42 | #include <kern/processor.h> |
43 | #include <kern/cpu_number.h> | |
44 | #include <kern/cpu_data.h> | |
45 | #include <kern/assert.h> | |
0a7de745 | 46 | #include <kern/lock_group.h> |
91447636 | 47 | #include <kern/machine.h> |
0c530ab8 | 48 | #include <kern/pms.h> |
593a1d5f | 49 | #include <kern/misc_protos.h> |
39236c6e | 50 | #include <kern/timer_call.h> |
f427ee49 | 51 | #include <kern/zalloc.h> |
6d2010ae | 52 | #include <kern/queue.h> |
fe8ab488 | 53 | #include <prng/random.h> |
91447636 A |
54 | |
55 | #include <vm/vm_map.h> | |
56 | #include <vm/vm_kern.h> | |
57 | ||
fe8ab488 | 58 | #include <i386/bit_routines.h> |
b0d623f7 A |
59 | #include <i386/proc_reg.h> |
60 | #include <i386/cpu_threads.h> | |
61 | #include <i386/mp_desc.h> | |
62 | #include <i386/misc_protos.h> | |
63 | #include <i386/trap.h> | |
64 | #include <i386/postcode.h> | |
65 | #include <i386/machine_routines.h> | |
55e303ae A |
66 | #include <i386/mp.h> |
67 | #include <i386/mp_events.h> | |
593a1d5f | 68 | #include <i386/lapic.h> |
55e303ae | 69 | #include <i386/cpuid.h> |
b0d623f7 | 70 | #include <i386/fpu.h> |
55e303ae | 71 | #include <i386/machine_cpu.h> |
0c530ab8 | 72 | #include <i386/pmCPU.h> |
b0d623f7 | 73 | #if CONFIG_MCA |
2d21ac55 | 74 | #include <i386/machine_check.h> |
b0d623f7 A |
75 | #endif |
76 | #include <i386/acpi.h> | |
0c530ab8 | 77 | |
0c530ab8 | 78 | #include <sys/kdebug.h> |
55e303ae | 79 | |
39236c6e A |
80 | #include <console/serial_protos.h> |
81 | ||
5ba3f43e A |
82 | #if MONOTONIC |
83 | #include <kern/monotonic.h> | |
84 | #endif /* MONOTONIC */ | |
85 | ||
0a7de745 A |
86 | #if MP_DEBUG |
87 | #define PAUSE delay(1000000) | |
88 | #define DBG(x...) kprintf(x) | |
55e303ae A |
89 | #else |
90 | #define DBG(x...) | |
91 | #define PAUSE | |
0a7de745 | 92 | #endif /* MP_DEBUG */ |
55e303ae | 93 | |
6d2010ae | 94 | /* Debugging/test trace events: */ |
0a7de745 A |
95 | #define TRACE_MP_TLB_FLUSH MACHDBG_CODE(DBG_MACH_MP, 0) |
96 | #define TRACE_MP_CPUS_CALL MACHDBG_CODE(DBG_MACH_MP, 1) | |
97 | #define TRACE_MP_CPUS_CALL_LOCAL MACHDBG_CODE(DBG_MACH_MP, 2) | |
98 | #define TRACE_MP_CPUS_CALL_ACTION MACHDBG_CODE(DBG_MACH_MP, 3) | |
99 | #define TRACE_MP_CPUS_CALL_NOBUF MACHDBG_CODE(DBG_MACH_MP, 4) | |
100 | #define TRACE_MP_CPU_FAST_START MACHDBG_CODE(DBG_MACH_MP, 5) | |
101 | #define TRACE_MP_CPU_START MACHDBG_CODE(DBG_MACH_MP, 6) | |
102 | #define TRACE_MP_CPU_DEACTIVATE MACHDBG_CODE(DBG_MACH_MP, 7) | |
55e303ae | 103 | |
0a7de745 | 104 | #define ABS(v) (((v) > 0)?(v):-(v)) |
7e4a7d39 | 105 | |
0a7de745 A |
106 | void slave_boot_init(void); |
107 | void i386_cpu_IPI(int cpu); | |
55e303ae | 108 | |
39236c6e | 109 | #if MACH_KDP |
0a7de745 | 110 | static void mp_kdp_wait(boolean_t flush, boolean_t isNMI); |
39236c6e | 111 | #endif /* MACH_KDP */ |
55e303ae | 112 | |
39236c6e | 113 | #if MACH_KDP |
0a7de745 | 114 | static boolean_t cpu_signal_pending(int cpu, mp_event_t event); |
39236c6e | 115 | #endif /* MACH_KDP */ |
0a7de745 | 116 | static int NMIInterruptHandler(x86_saved_state_t *regs); |
0c530ab8 | 117 | |
0a7de745 A |
118 | boolean_t smp_initialized = FALSE; |
119 | uint32_t TSC_sync_margin = 0xFFF; | |
120 | volatile boolean_t force_immediate_debugger_NMI = FALSE; | |
121 | volatile boolean_t pmap_tlb_flush_timeout = FALSE; | |
5ba3f43e | 122 | #if DEBUG || DEVELOPMENT |
0a7de745 A |
123 | boolean_t mp_interrupt_watchdog_enabled = TRUE; |
124 | uint32_t mp_interrupt_watchdog_events = 0; | |
5ba3f43e | 125 | #endif |
91447636 | 126 | |
f427ee49 | 127 | SIMPLE_LOCK_DECLARE(debugger_callback_lock, 0); |
39037602 A |
128 | struct debugger_callback *debugger_callback = NULL; |
129 | ||
f427ee49 A |
130 | static LCK_GRP_DECLARE(smp_lck_grp, "i386_smp"); |
131 | static LCK_MTX_EARLY_DECLARE(mp_cpu_boot_lock, &smp_lck_grp); | |
55e303ae A |
132 | |
133 | /* Variables needed for MP rendezvous. */ | |
f427ee49 | 134 | SIMPLE_LOCK_DECLARE(mp_rv_lock, 0); |
0a7de745 A |
135 | static void (*mp_rv_setup_func)(void *arg); |
136 | static void (*mp_rv_action_func)(void *arg); | |
137 | static void (*mp_rv_teardown_func)(void *arg); | |
138 | static void *mp_rv_func_arg; | |
139 | static volatile int mp_rv_ncpus; | |
140 | /* Cache-aligned barriers: */ | |
141 | static volatile long mp_rv_entry __attribute__((aligned(64))); | |
142 | static volatile long mp_rv_exit __attribute__((aligned(64))); | |
143 | static volatile long mp_rv_complete __attribute__((aligned(64))); | |
144 | ||
145 | volatile uint64_t debugger_entry_time; | |
146 | volatile uint64_t debugger_exit_time; | |
b0d623f7 | 147 | #if MACH_KDP |
7ddcb079 | 148 | #include <kdp/kdp.h> |
d41d1dae | 149 | extern int kdp_snapshot; |
b0d623f7 A |
150 | static struct _kdp_xcpu_call_func { |
151 | kdp_x86_xcpu_func_t func; | |
152 | void *arg0, *arg1; | |
153 | volatile long ret; | |
154 | volatile uint16_t cpu; | |
155 | } kdp_xcpu_call_func = { | |
156 | .cpu = KDP_XCPU_NONE | |
157 | }; | |
158 | ||
159 | #endif | |
160 | ||
2d21ac55 A |
161 | /* Variables needed for MP broadcast. */ |
162 | static void (*mp_bc_action_func)(void *arg); | |
163 | static void *mp_bc_func_arg; | |
0a7de745 | 164 | static int mp_bc_ncpus; |
2d21ac55 | 165 | static volatile long mp_bc_count; |
f427ee49 | 166 | static LCK_MTX_EARLY_DECLARE(mp_bc_lock, &smp_lck_grp); |
0a7de745 A |
167 | static volatile int debugger_cpu = -1; |
168 | volatile long NMIPI_acks = 0; | |
169 | volatile long NMI_count = 0; | |
170 | static NMI_reason_t NMI_panic_reason = NONE; | |
171 | static int vector_timed_out; | |
39236c6e | 172 | |
0a7de745 | 173 | extern void NMI_cpus(void); |
2d21ac55 | 174 | |
0a7de745 A |
175 | static void mp_cpus_call_init(void); |
176 | static void mp_cpus_call_action(void); | |
177 | static void mp_call_PM(void); | |
2d21ac55 | 178 | |
0a7de745 | 179 | char mp_slave_stack[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE))); // Temp stack for slave init |
b0d623f7 | 180 | |
6d2010ae | 181 | /* PAL-related routines */ |
0a7de745 A |
182 | boolean_t i386_smp_init(int nmi_vector, i386_intr_func_t nmi_handler, |
183 | int ipi_vector, i386_intr_func_t ipi_handler); | |
6d2010ae A |
184 | void i386_start_cpu(int lapic_id, int cpu_num); |
185 | void i386_send_NMI(int cpu); | |
5c9f4661 | 186 | void NMIPI_enable(boolean_t); |
91447636 | 187 | |
0a7de745 A |
188 | #define NUM_CPU_WARM_CALLS 20 |
189 | struct timer_call cpu_warm_call_arr[NUM_CPU_WARM_CALLS]; | |
190 | queue_head_t cpu_warm_call_list; | |
6d2010ae A |
191 | decl_simple_lock_data(static, cpu_warm_lock); |
192 | ||
193 | typedef struct cpu_warm_data { | |
0a7de745 A |
194 | timer_call_t cwd_call; |
195 | uint64_t cwd_deadline; | |
196 | int cwd_result; | |
6d2010ae A |
197 | } *cpu_warm_data_t; |
198 | ||
0a7de745 | 199 | static void cpu_prewarm_init(void); |
f427ee49 | 200 | static void cpu_warm_timer_call_func(timer_call_param_t p0, timer_call_param_t p1); |
0a7de745 A |
201 | static void _cpu_warm_setup(void *arg); |
202 | static timer_call_t grab_warm_timer_call(void); | |
203 | static void free_warm_timer_call(timer_call_t call); | |
b0d623f7 | 204 | |
55e303ae A |
205 | void |
206 | smp_init(void) | |
55e303ae | 207 | { |
91447636 | 208 | console_init(); |
55e303ae | 209 | |
0a7de745 A |
210 | if (!i386_smp_init(LAPIC_NMI_INTERRUPT, NMIInterruptHandler, |
211 | LAPIC_VECTOR(INTERPROCESSOR), cpu_signal_handler)) { | |
55e303ae | 212 | return; |
0a7de745 | 213 | } |
55e303ae | 214 | |
91447636 A |
215 | cpu_thread_init(); |
216 | ||
91447636 A |
217 | DBGLOG_CPU_INIT(master_cpu); |
218 | ||
6d2010ae | 219 | mp_cpus_call_init(); |
fe8ab488 | 220 | mp_cpus_call_cpu_init(master_cpu); |
55e303ae | 221 | |
5ba3f43e A |
222 | #if DEBUG || DEVELOPMENT |
223 | if (PE_parse_boot_argn("interrupt_watchdog", | |
0a7de745 A |
224 | &mp_interrupt_watchdog_enabled, |
225 | sizeof(mp_interrupt_watchdog_enabled))) { | |
5ba3f43e | 226 | kprintf("Interrupt watchdog %sabled\n", |
0a7de745 | 227 | mp_interrupt_watchdog_enabled ? "en" : "dis"); |
5ba3f43e A |
228 | } |
229 | #endif | |
230 | ||
7e4a7d39 | 231 | if (PE_parse_boot_argn("TSC_sync_margin", |
0a7de745 | 232 | &TSC_sync_margin, sizeof(TSC_sync_margin))) { |
7e4a7d39 | 233 | kprintf("TSC sync Margin 0x%x\n", TSC_sync_margin); |
316670eb A |
234 | } else if (cpuid_vmm_present()) { |
235 | kprintf("TSC sync margin disabled\n"); | |
236 | TSC_sync_margin = 0; | |
237 | } | |
55e303ae A |
238 | smp_initialized = TRUE; |
239 | ||
6d2010ae A |
240 | cpu_prewarm_init(); |
241 | ||
55e303ae A |
242 | return; |
243 | } | |
244 | ||
7e4a7d39 | 245 | typedef struct { |
0a7de745 A |
246 | int target_cpu; |
247 | int target_lapic; | |
248 | int starter_cpu; | |
7e4a7d39 | 249 | } processor_start_info_t; |
0a7de745 | 250 | static processor_start_info_t start_info __attribute__((aligned(64))); |
7e4a7d39 | 251 | |
0a7de745 | 252 | /* |
7e4a7d39 A |
253 | * Cache-alignment is to avoid cross-cpu false-sharing interference. |
254 | */ | |
0a7de745 A |
255 | static volatile long tsc_entry_barrier __attribute__((aligned(64))); |
256 | static volatile long tsc_exit_barrier __attribute__((aligned(64))); | |
257 | static volatile uint64_t tsc_target __attribute__((aligned(64))); | |
7e4a7d39 | 258 | |
0c530ab8 | 259 | /* |
593a1d5f | 260 | * Poll a CPU to see when it has marked itself as running. |
0c530ab8 | 261 | */ |
593a1d5f A |
262 | static void |
263 | mp_wait_for_cpu_up(int slot_num, unsigned int iters, unsigned int usecdelay) | |
91447636 | 264 | { |
7e4a7d39 | 265 | while (iters-- > 0) { |
0a7de745 | 266 | if (cpu_datap(slot_num)->cpu_running) { |
7e4a7d39 | 267 | break; |
0a7de745 | 268 | } |
593a1d5f | 269 | delay(usecdelay); |
91447636 | 270 | } |
55e303ae A |
271 | } |
272 | ||
b0d623f7 A |
273 | /* |
274 | * Quickly bring a CPU back online which has been halted. | |
275 | */ | |
276 | kern_return_t | |
277 | intel_startCPU_fast(int slot_num) | |
278 | { | |
0a7de745 | 279 | kern_return_t rc; |
b0d623f7 A |
280 | |
281 | /* | |
282 | * Try to perform a fast restart | |
283 | */ | |
284 | rc = pmCPUExitHalt(slot_num); | |
0a7de745 | 285 | if (rc != KERN_SUCCESS) { |
b0d623f7 A |
286 | /* |
287 | * The CPU was not eligible for a fast restart. | |
288 | */ | |
0a7de745 A |
289 | return rc; |
290 | } | |
b0d623f7 | 291 | |
bd504ef0 A |
292 | KERNEL_DEBUG_CONSTANT( |
293 | TRACE_MP_CPU_FAST_START | DBG_FUNC_START, | |
294 | slot_num, 0, 0, 0, 0); | |
295 | ||
b0d623f7 A |
296 | /* |
297 | * Wait until the CPU is back online. | |
298 | */ | |
299 | mp_disable_preemption(); | |
0a7de745 | 300 | |
b0d623f7 A |
301 | /* |
302 | * We use short pauses (1us) for low latency. 30,000 iterations is | |
303 | * longer than a full restart would require so it should be more | |
304 | * than long enough. | |
305 | */ | |
6d2010ae | 306 | |
b0d623f7 A |
307 | mp_wait_for_cpu_up(slot_num, 30000, 1); |
308 | mp_enable_preemption(); | |
309 | ||
bd504ef0 A |
310 | KERNEL_DEBUG_CONSTANT( |
311 | TRACE_MP_CPU_FAST_START | DBG_FUNC_END, | |
312 | slot_num, cpu_datap(slot_num)->cpu_running, 0, 0, 0); | |
313 | ||
b0d623f7 A |
314 | /* |
315 | * Check to make sure that the CPU is really running. If not, | |
316 | * go through the slow path. | |
317 | */ | |
0a7de745 A |
318 | if (cpu_datap(slot_num)->cpu_running) { |
319 | return KERN_SUCCESS; | |
320 | } else { | |
321 | return KERN_FAILURE; | |
322 | } | |
b0d623f7 A |
323 | } |
324 | ||
7e4a7d39 A |
325 | static void |
326 | started_cpu(void) | |
327 | { | |
328 | /* Here on the started cpu with cpu_running set TRUE */ | |
c910b4d9 | 329 | |
7e4a7d39 A |
330 | if (TSC_sync_margin && |
331 | start_info.target_cpu == cpu_number()) { | |
332 | /* | |
333 | * I've just started-up, synchronize again with the starter cpu | |
334 | * and then snap my TSC. | |
335 | */ | |
336 | tsc_target = 0; | |
337 | atomic_decl(&tsc_entry_barrier, 1); | |
0a7de745 A |
338 | while (tsc_entry_barrier != 0) { |
339 | ; /* spin for starter and target at barrier */ | |
340 | } | |
7e4a7d39 A |
341 | tsc_target = rdtsc64(); |
342 | atomic_decl(&tsc_exit_barrier, 1); | |
343 | } | |
344 | } | |
c910b4d9 A |
345 | |
346 | static void | |
347 | start_cpu(void *arg) | |
348 | { | |
0a7de745 A |
349 | int i = 1000; |
350 | processor_start_info_t *psip = (processor_start_info_t *) arg; | |
c910b4d9 A |
351 | |
352 | /* Ignore this if the current processor is not the starter */ | |
0a7de745 | 353 | if (cpu_number() != psip->starter_cpu) { |
c910b4d9 | 354 | return; |
0a7de745 | 355 | } |
c910b4d9 | 356 | |
bd504ef0 | 357 | DBG("start_cpu(%p) about to start cpu %d, lapic %d\n", |
0a7de745 | 358 | arg, psip->target_cpu, psip->target_lapic); |
bd504ef0 A |
359 | |
360 | KERNEL_DEBUG_CONSTANT( | |
361 | TRACE_MP_CPU_START | DBG_FUNC_START, | |
362 | psip->target_cpu, | |
363 | psip->target_lapic, 0, 0, 0); | |
364 | ||
6d2010ae | 365 | i386_start_cpu(psip->target_lapic, psip->target_cpu); |
c910b4d9 | 366 | |
0a7de745 | 367 | #ifdef POSTCODE_DELAY |
c910b4d9 A |
368 | /* Wait much longer if postcodes are displayed for a delay period. */ |
369 | i *= 10000; | |
370 | #endif | |
bd504ef0 | 371 | DBG("start_cpu(%p) about to wait for cpu %d\n", |
0a7de745 | 372 | arg, psip->target_cpu); |
bd504ef0 | 373 | |
0a7de745 | 374 | mp_wait_for_cpu_up(psip->target_cpu, i * 100, 100); |
bd504ef0 A |
375 | |
376 | KERNEL_DEBUG_CONSTANT( | |
377 | TRACE_MP_CPU_START | DBG_FUNC_END, | |
378 | psip->target_cpu, | |
379 | cpu_datap(psip->target_cpu)->cpu_running, 0, 0, 0); | |
380 | ||
7e4a7d39 A |
381 | if (TSC_sync_margin && |
382 | cpu_datap(psip->target_cpu)->cpu_running) { | |
383 | /* | |
384 | * Compare the TSC from the started processor with ours. | |
385 | * Report and log/panic if it diverges by more than | |
386 | * TSC_sync_margin (TSC_SYNC_MARGIN) ticks. This margin | |
387 | * can be overriden by boot-arg (with 0 meaning no checking). | |
388 | */ | |
0a7de745 A |
389 | uint64_t tsc_starter; |
390 | int64_t tsc_delta; | |
7e4a7d39 | 391 | atomic_decl(&tsc_entry_barrier, 1); |
0a7de745 A |
392 | while (tsc_entry_barrier != 0) { |
393 | ; /* spin for both processors at barrier */ | |
394 | } | |
7e4a7d39 A |
395 | tsc_starter = rdtsc64(); |
396 | atomic_decl(&tsc_exit_barrier, 1); | |
0a7de745 A |
397 | while (tsc_exit_barrier != 0) { |
398 | ; /* spin for target to store its TSC */ | |
399 | } | |
7e4a7d39 A |
400 | tsc_delta = tsc_target - tsc_starter; |
401 | kprintf("TSC sync for cpu %d: 0x%016llx delta 0x%llx (%lld)\n", | |
0a7de745 | 402 | psip->target_cpu, tsc_target, tsc_delta, tsc_delta); |
f427ee49 A |
403 | #if DEBUG || DEVELOPMENT |
404 | /* | |
405 | * Stash the delta for inspection later, since we can no | |
406 | * longer print/log it with interrupts disabled. | |
407 | */ | |
408 | cpu_datap(psip->target_cpu)->tsc_sync_delta = tsc_delta; | |
409 | #endif | |
0a7de745 | 410 | if (ABS(tsc_delta) > (int64_t) TSC_sync_margin) { |
7e4a7d39 A |
411 | #if DEBUG |
412 | panic( | |
413 | #else | |
f427ee49 | 414 | kprintf( |
7e4a7d39 A |
415 | #endif |
416 | "Unsynchronized TSC for cpu %d: " | |
0a7de745 | 417 | "0x%016llx, delta 0x%llx\n", |
7e4a7d39 A |
418 | psip->target_cpu, tsc_target, tsc_delta); |
419 | } | |
420 | } | |
c910b4d9 A |
421 | } |
422 | ||
55e303ae A |
423 | kern_return_t |
424 | intel_startCPU( | |
0a7de745 | 425 | int slot_num) |
55e303ae | 426 | { |
0a7de745 A |
427 | int lapic = cpu_to_lapic[slot_num]; |
428 | boolean_t istate; | |
55e303ae | 429 | |
91447636 A |
430 | assert(lapic != -1); |
431 | ||
432 | DBGLOG_CPU_INIT(slot_num); | |
55e303ae | 433 | |
91447636 | 434 | DBG("intel_startCPU(%d) lapic_id=%d\n", slot_num, lapic); |
6d2010ae | 435 | DBG("IdlePTD(%p): 0x%x\n", &IdlePTD, (int) (uintptr_t)IdlePTD); |
55e303ae | 436 | |
0c530ab8 A |
437 | /* |
438 | * Initialize (or re-initialize) the descriptor tables for this cpu. | |
439 | * Propagate processor mode to slave. | |
440 | */ | |
5ba3f43e | 441 | cpu_desc_init(cpu_datap(slot_num)); |
91447636 | 442 | |
c910b4d9 | 443 | /* Serialize use of the slave boot stack, etc. */ |
b0d623f7 | 444 | lck_mtx_lock(&mp_cpu_boot_lock); |
55e303ae | 445 | |
c910b4d9 | 446 | istate = ml_set_interrupts_enabled(FALSE); |
91447636 | 447 | if (slot_num == get_cpu_number()) { |
c910b4d9 | 448 | ml_set_interrupts_enabled(istate); |
b0d623f7 | 449 | lck_mtx_unlock(&mp_cpu_boot_lock); |
91447636 A |
450 | return KERN_SUCCESS; |
451 | } | |
55e303ae | 452 | |
b0d623f7 A |
453 | start_info.starter_cpu = cpu_number(); |
454 | start_info.target_cpu = slot_num; | |
c910b4d9 | 455 | start_info.target_lapic = lapic; |
7e4a7d39 A |
456 | tsc_entry_barrier = 2; |
457 | tsc_exit_barrier = 2; | |
55e303ae | 458 | |
c910b4d9 | 459 | /* |
b0d623f7 | 460 | * Perform the processor startup sequence with all running |
c910b4d9 A |
461 | * processors rendezvous'ed. This is required during periods when |
462 | * the cache-disable bit is set for MTRR/PAT initialization. | |
463 | */ | |
b0d623f7 | 464 | mp_rendezvous_no_intrs(start_cpu, (void *) &start_info); |
55e303ae | 465 | |
7e4a7d39 A |
466 | start_info.target_cpu = 0; |
467 | ||
c910b4d9 | 468 | ml_set_interrupts_enabled(istate); |
b0d623f7 | 469 | lck_mtx_unlock(&mp_cpu_boot_lock); |
55e303ae | 470 | |
91447636 | 471 | if (!cpu_datap(slot_num)->cpu_running) { |
0c530ab8 | 472 | kprintf("Failed to start CPU %02d\n", slot_num); |
91447636 A |
473 | printf("Failed to start CPU %02d, rebooting...\n", slot_num); |
474 | delay(1000000); | |
b0d623f7 | 475 | halt_cpu(); |
55e303ae A |
476 | return KERN_SUCCESS; |
477 | } else { | |
2d21ac55 | 478 | kprintf("Started cpu %d (lapic id %08x)\n", slot_num, lapic); |
55e303ae A |
479 | return KERN_SUCCESS; |
480 | } | |
481 | } | |
482 | ||
0a7de745 A |
483 | #if MP_DEBUG |
484 | cpu_signal_event_log_t *cpu_signal[MAX_CPUS]; | |
485 | cpu_signal_event_log_t *cpu_handle[MAX_CPUS]; | |
55e303ae A |
486 | |
487 | MP_EVENT_NAME_DECL(); | |
488 | ||
0a7de745 | 489 | #endif /* MP_DEBUG */ |
55e303ae | 490 | |
fe8ab488 A |
491 | /* |
492 | * Note: called with NULL state when polling for TLB flush and cross-calls. | |
493 | */ | |
593a1d5f | 494 | int |
0c530ab8 | 495 | cpu_signal_handler(x86_saved_state_t *regs) |
55e303ae | 496 | { |
0a7de745 | 497 | #if !MACH_KDP |
39236c6e A |
498 | #pragma unused (regs) |
499 | #endif /* !MACH_KDP */ | |
0a7de745 A |
500 | int my_cpu; |
501 | volatile int *my_word; | |
55e303ae | 502 | |
f427ee49 | 503 | SCHED_STATS_INC(ipi_count); |
55e303ae A |
504 | |
505 | my_cpu = cpu_number(); | |
060df5ea A |
506 | my_word = &cpu_data_ptr[my_cpu]->cpu_signals; |
507 | /* Store the initial set of signals for diagnostics. New | |
508 | * signals could arrive while these are being processed | |
509 | * so it's no more than a hint. | |
510 | */ | |
6d2010ae | 511 | |
060df5ea | 512 | cpu_data_ptr[my_cpu]->cpu_prior_signals = *my_word; |
55e303ae A |
513 | |
514 | do { | |
0a7de745 | 515 | #if MACH_KDP |
fe8ab488 | 516 | if (i_bit(MP_KDP, my_word)) { |
0a7de745 | 517 | DBGLOG(cpu_handle, my_cpu, MP_KDP); |
55e303ae | 518 | i_bit_clear(MP_KDP, my_word); |
0c530ab8 A |
519 | /* Ensure that the i386_kernel_state at the base of the |
520 | * current thread's stack (if any) is synchronized with the | |
521 | * context at the moment of the interrupt, to facilitate | |
522 | * access through the debugger. | |
0c530ab8 | 523 | */ |
b0d623f7 | 524 | sync_iss_to_iks(regs); |
0a7de745 | 525 | if (pmsafe_debug && !kdp_snapshot) { |
d41d1dae | 526 | pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_SAFE); |
0a7de745 | 527 | } |
b0d623f7 | 528 | mp_kdp_wait(TRUE, FALSE); |
0a7de745 | 529 | if (pmsafe_debug && !kdp_snapshot) { |
d41d1dae | 530 | pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_NORMAL); |
0a7de745 | 531 | } |
55e303ae | 532 | } else |
0a7de745 | 533 | #endif /* MACH_KDP */ |
91447636 | 534 | if (i_bit(MP_TLB_FLUSH, my_word)) { |
0a7de745 | 535 | DBGLOG(cpu_handle, my_cpu, MP_TLB_FLUSH); |
55e303ae A |
536 | i_bit_clear(MP_TLB_FLUSH, my_word); |
537 | pmap_update_interrupt(); | |
2d21ac55 | 538 | } else if (i_bit(MP_CALL, my_word)) { |
0a7de745 | 539 | DBGLOG(cpu_handle, my_cpu, MP_CALL); |
2d21ac55 A |
540 | i_bit_clear(MP_CALL, my_word); |
541 | mp_cpus_call_action(); | |
c910b4d9 | 542 | } else if (i_bit(MP_CALL_PM, my_word)) { |
0a7de745 | 543 | DBGLOG(cpu_handle, my_cpu, MP_CALL_PM); |
c910b4d9 A |
544 | i_bit_clear(MP_CALL_PM, my_word); |
545 | mp_call_PM(); | |
55e303ae | 546 | } |
fe8ab488 A |
547 | if (regs == NULL) { |
548 | /* Called to poll only for cross-calls and TLB flush */ | |
549 | break; | |
550 | } else if (i_bit(MP_AST, my_word)) { | |
0a7de745 | 551 | DBGLOG(cpu_handle, my_cpu, MP_AST); |
fe8ab488 A |
552 | i_bit_clear(MP_AST, my_word); |
553 | ast_check(cpu_to_processor(my_cpu)); | |
554 | } | |
55e303ae A |
555 | } while (*my_word); |
556 | ||
593a1d5f | 557 | return 0; |
55e303ae A |
558 | } |
559 | ||
fe8ab488 | 560 | extern void kprintf_break_lock(void); |
d9a64523 | 561 | int |
2d21ac55 | 562 | NMIInterruptHandler(x86_saved_state_t *regs) |
0c530ab8 | 563 | { |
0a7de745 A |
564 | void *stackptr; |
565 | char pstr[256]; | |
566 | uint64_t now = mach_absolute_time(); | |
060df5ea | 567 | |
6d2010ae | 568 | if (panic_active() && !panicDebugging) { |
0a7de745 | 569 | if (pmsafe_debug) { |
6d2010ae | 570 | pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_SAFE); |
0a7de745 A |
571 | } |
572 | for (;;) { | |
6d2010ae | 573 | cpu_pause(); |
0a7de745 | 574 | } |
6d2010ae A |
575 | } |
576 | ||
060df5ea | 577 | atomic_incl(&NMIPI_acks, 1); |
39236c6e | 578 | atomic_incl(&NMI_count, 1); |
0c530ab8 | 579 | sync_iss_to_iks_unconditionally(regs); |
0a7de745 | 580 | __asm__ volatile ("movq %%rbp, %0" : "=m" (stackptr)); |
935ed37a | 581 | |
0a7de745 | 582 | if (cpu_number() == debugger_cpu) { |
fe8ab488 | 583 | goto NMExit; |
0a7de745 | 584 | } |
593a1d5f | 585 | |
5ba3f43e A |
586 | if (NMI_panic_reason == SPINLOCK_TIMEOUT) { |
587 | snprintf(&pstr[0], sizeof(pstr), | |
0a7de745 A |
588 | "Panic(CPU %d, time %llu): NMIPI for spinlock acquisition timeout, spinlock: %p, spinlock owner: %p, current_thread: %p, spinlock_owner_cpu: 0x%x\n", |
589 | cpu_number(), now, spinlock_timed_out, (void *) spinlock_timed_out->interlock.lock_data, current_thread(), spinlock_owner_cpu); | |
fe8ab488 | 590 | panic_i386_backtrace(stackptr, 64, &pstr[0], TRUE, regs); |
5ba3f43e A |
591 | } else if (NMI_panic_reason == TLB_FLUSH_TIMEOUT) { |
592 | snprintf(&pstr[0], sizeof(pstr), | |
0a7de745 A |
593 | "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: TLB flush timeout, TLB state:0x%x\n", |
594 | cpu_number(), now, current_cpu_datap()->cpu_tlb_invalid); | |
6d2010ae | 595 | panic_i386_backtrace(stackptr, 48, &pstr[0], TRUE, regs); |
5ba3f43e A |
596 | } else if (NMI_panic_reason == CROSSCALL_TIMEOUT) { |
597 | snprintf(&pstr[0], sizeof(pstr), | |
0a7de745 A |
598 | "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: cross-call timeout\n", |
599 | cpu_number(), now); | |
5ba3f43e A |
600 | panic_i386_backtrace(stackptr, 64, &pstr[0], TRUE, regs); |
601 | } else if (NMI_panic_reason == INTERRUPT_WATCHDOG) { | |
602 | snprintf(&pstr[0], sizeof(pstr), | |
0a7de745 A |
603 | "Panic(CPU %d, time %llu): NMIPI for unresponsive processor: interrupt watchdog for vector 0x%x\n", |
604 | cpu_number(), now, vector_timed_out); | |
5ba3f43e A |
605 | panic_i386_backtrace(stackptr, 64, &pstr[0], TRUE, regs); |
606 | } | |
0a7de745 | 607 | |
b0d623f7 | 608 | #if MACH_KDP |
0a7de745 | 609 | if (pmsafe_debug && !kdp_snapshot) { |
d41d1dae | 610 | pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_SAFE); |
0a7de745 | 611 | } |
060df5ea | 612 | current_cpu_datap()->cpu_NMI_acknowledged = TRUE; |
15129b1c | 613 | i_bit_clear(MP_KDP, ¤t_cpu_datap()->cpu_signals); |
5ba3f43e | 614 | if (panic_active() || NMI_panic_reason != NONE) { |
fe8ab488 | 615 | mp_kdp_wait(FALSE, TRUE); |
39037602 | 616 | } else if (!mp_kdp_trap && |
0a7de745 A |
617 | !mp_kdp_is_NMI && |
618 | virtualized && (debug_boot_arg & DB_NMI)) { | |
fe8ab488 A |
619 | /* |
620 | * Under a VMM with the debug boot-arg set, drop into kdp. | |
621 | * Since an NMI is involved, there's a risk of contending with | |
0a7de745 | 622 | * a panic. And side-effects of NMIs may result in entry into, |
fe8ab488 A |
623 | * and continuing from, the debugger being unreliable. |
624 | */ | |
39037602 A |
625 | if (__sync_bool_compare_and_swap(&mp_kdp_is_NMI, FALSE, TRUE)) { |
626 | kprintf_break_lock(); | |
627 | kprintf("Debugger entry requested by NMI\n"); | |
628 | kdp_i386_trap(T_DEBUG, saved_state64(regs), 0, 0); | |
629 | printf("Debugger entry requested by NMI\n"); | |
630 | mp_kdp_is_NMI = FALSE; | |
631 | } else { | |
632 | mp_kdp_wait(FALSE, FALSE); | |
633 | } | |
fe8ab488 A |
634 | } else { |
635 | mp_kdp_wait(FALSE, FALSE); | |
636 | } | |
0a7de745 | 637 | if (pmsafe_debug && !kdp_snapshot) { |
d41d1dae | 638 | pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_NORMAL); |
0a7de745 | 639 | } |
b0d623f7 | 640 | #endif |
0a7de745 | 641 | NMExit: |
0c530ab8 A |
642 | return 1; |
643 | } | |
644 | ||
2d21ac55 A |
645 | |
646 | /* | |
647 | * cpu_interrupt is really just to be used by the scheduler to | |
648 | * get a CPU's attention it may not always issue an IPI. If an | |
649 | * IPI is always needed then use i386_cpu_IPI. | |
650 | */ | |
651 | void | |
652 | cpu_interrupt(int cpu) | |
653 | { | |
6d2010ae A |
654 | boolean_t did_IPI = FALSE; |
655 | ||
2d21ac55 A |
656 | if (smp_initialized |
657 | && pmCPUExitIdle(cpu_datap(cpu))) { | |
658 | i386_cpu_IPI(cpu); | |
6d2010ae | 659 | did_IPI = TRUE; |
2d21ac55 | 660 | } |
6d2010ae A |
661 | |
662 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_REMOTE_AST), cpu, did_IPI, 0, 0, 0); | |
55e303ae A |
663 | } |
664 | ||
0c530ab8 A |
665 | /* |
666 | * Send a true NMI via the local APIC to the specified CPU. | |
667 | */ | |
935ed37a | 668 | void |
0c530ab8 A |
669 | cpu_NMI_interrupt(int cpu) |
670 | { | |
0c530ab8 | 671 | if (smp_initialized) { |
6d2010ae | 672 | i386_send_NMI(cpu); |
0c530ab8 | 673 | } |
0c530ab8 A |
674 | } |
675 | ||
39236c6e A |
676 | void |
677 | NMI_cpus(void) | |
678 | { | |
0a7de745 A |
679 | unsigned int cpu; |
680 | boolean_t intrs_enabled; | |
681 | uint64_t tsc_timeout; | |
39236c6e A |
682 | |
683 | intrs_enabled = ml_set_interrupts_enabled(FALSE); | |
d9a64523 | 684 | NMIPI_enable(TRUE); |
39236c6e | 685 | for (cpu = 0; cpu < real_ncpus; cpu++) { |
0a7de745 | 686 | if (!cpu_is_running(cpu)) { |
39236c6e | 687 | continue; |
0a7de745 | 688 | } |
39236c6e A |
689 | cpu_datap(cpu)->cpu_NMI_acknowledged = FALSE; |
690 | cpu_NMI_interrupt(cpu); | |
691 | tsc_timeout = !machine_timeout_suspended() ? | |
0a7de745 A |
692 | rdtsc64() + (1000 * 1000 * 1000 * 10ULL) : |
693 | ~0ULL; | |
39236c6e A |
694 | while (!cpu_datap(cpu)->cpu_NMI_acknowledged) { |
695 | handle_pending_TLB_flushes(); | |
696 | cpu_pause(); | |
0a7de745 | 697 | if (rdtsc64() > tsc_timeout) { |
39236c6e | 698 | panic("NMI_cpus() timeout cpu %d", cpu); |
0a7de745 | 699 | } |
39236c6e A |
700 | } |
701 | cpu_datap(cpu)->cpu_NMI_acknowledged = FALSE; | |
702 | } | |
d9a64523 | 703 | NMIPI_enable(FALSE); |
39236c6e A |
704 | |
705 | ml_set_interrupts_enabled(intrs_enabled); | |
706 | } | |
707 | ||
0a7de745 | 708 | static void(*volatile mp_PM_func)(void) = NULL; |
c910b4d9 A |
709 | |
710 | static void | |
711 | mp_call_PM(void) | |
712 | { | |
713 | assert(!ml_get_interrupts_enabled()); | |
714 | ||
0a7de745 | 715 | if (mp_PM_func != NULL) { |
c910b4d9 | 716 | mp_PM_func(); |
0a7de745 | 717 | } |
c910b4d9 A |
718 | } |
719 | ||
720 | void | |
721 | cpu_PM_interrupt(int cpu) | |
722 | { | |
723 | assert(!ml_get_interrupts_enabled()); | |
724 | ||
725 | if (mp_PM_func != NULL) { | |
0a7de745 | 726 | if (cpu == cpu_number()) { |
c910b4d9 | 727 | mp_PM_func(); |
0a7de745 | 728 | } else { |
c910b4d9 | 729 | i386_signal_cpu(cpu, MP_CALL_PM, ASYNC); |
0a7de745 | 730 | } |
c910b4d9 A |
731 | } |
732 | } | |
733 | ||
734 | void | |
735 | PM_interrupt_register(void (*fn)(void)) | |
736 | { | |
737 | mp_PM_func = fn; | |
738 | } | |
739 | ||
55e303ae A |
740 | void |
741 | i386_signal_cpu(int cpu, mp_event_t event, mp_sync_t mode) | |
742 | { | |
0a7de745 A |
743 | volatile int *signals = &cpu_datap(cpu)->cpu_signals; |
744 | uint64_t tsc_timeout; | |
745 | ||
6601e61a | 746 | |
0a7de745 | 747 | if (!cpu_datap(cpu)->cpu_running) { |
55e303ae | 748 | return; |
0a7de745 | 749 | } |
55e303ae | 750 | |
0a7de745 A |
751 | if (event == MP_TLB_FLUSH) { |
752 | KERNEL_DEBUG(TRACE_MP_TLB_FLUSH | DBG_FUNC_START, cpu, 0, 0, 0, 0); | |
753 | } | |
6601e61a | 754 | |
0c530ab8 | 755 | DBGLOG(cpu_signal, cpu, event); |
0a7de745 | 756 | |
55e303ae | 757 | i_bit_set(event, signals); |
2d21ac55 | 758 | i386_cpu_IPI(cpu); |
55e303ae | 759 | if (mode == SYNC) { |
0a7de745 | 760 | again: |
39236c6e | 761 | tsc_timeout = !machine_timeout_suspended() ? |
0a7de745 A |
762 | rdtsc64() + (1000 * 1000 * 1000) : |
763 | ~0ULL; | |
91447636 | 764 | while (i_bit(event, signals) && rdtsc64() < tsc_timeout) { |
55e303ae A |
765 | cpu_pause(); |
766 | } | |
767 | if (i_bit(event, signals)) { | |
768 | DBG("i386_signal_cpu(%d, 0x%x, SYNC) timed out\n", | |
0a7de745 | 769 | cpu, event); |
55e303ae A |
770 | goto again; |
771 | } | |
772 | } | |
0a7de745 A |
773 | if (event == MP_TLB_FLUSH) { |
774 | KERNEL_DEBUG(TRACE_MP_TLB_FLUSH | DBG_FUNC_END, cpu, 0, 0, 0, 0); | |
775 | } | |
55e303ae A |
776 | } |
777 | ||
39236c6e A |
778 | /* |
779 | * Helper function called when busy-waiting: panic if too long | |
780 | * a TSC-based time has elapsed since the start of the spin. | |
781 | */ | |
fe8ab488 A |
782 | static boolean_t |
783 | mp_spin_timeout(uint64_t tsc_start) | |
39236c6e | 784 | { |
0a7de745 | 785 | uint64_t tsc_timeout; |
39236c6e A |
786 | |
787 | cpu_pause(); | |
0a7de745 | 788 | if (machine_timeout_suspended()) { |
fe8ab488 | 789 | return FALSE; |
0a7de745 | 790 | } |
39236c6e A |
791 | |
792 | /* | |
793 | * The timeout is 4 * the spinlock timeout period | |
794 | * unless we have serial console printing (kprintf) enabled | |
795 | * in which case we allow an even greater margin. | |
796 | */ | |
39037602 | 797 | tsc_timeout = disable_serial_output ? LockTimeOutTSC << 2 |
0a7de745 A |
798 | : LockTimeOutTSC << 4; |
799 | return rdtsc64() > tsc_start + tsc_timeout; | |
fe8ab488 A |
800 | } |
801 | ||
802 | /* | |
803 | * Helper function to take a spinlock while ensuring that incoming IPIs | |
804 | * are still serviced if interrupts are masked while we spin. | |
39037602 | 805 | * Returns current interrupt state. |
fe8ab488 | 806 | */ |
5ba3f43e | 807 | boolean_t |
fe8ab488 A |
808 | mp_safe_spin_lock(usimple_lock_t lock) |
809 | { | |
810 | if (ml_get_interrupts_enabled()) { | |
0a7de745 | 811 | simple_lock(lock, LCK_GRP_NULL); |
fe8ab488 A |
812 | return TRUE; |
813 | } else { | |
814 | uint64_t tsc_spin_start = rdtsc64(); | |
0a7de745 | 815 | while (!simple_lock_try(lock, LCK_GRP_NULL)) { |
fe8ab488 A |
816 | cpu_signal_handler(NULL); |
817 | if (mp_spin_timeout(tsc_spin_start)) { | |
818 | uint32_t lock_cpu; | |
819 | uintptr_t lowner = (uintptr_t) | |
0a7de745 | 820 | lock->interlock.lock_data; |
fe8ab488 A |
821 | spinlock_timed_out = lock; |
822 | lock_cpu = spinlock_timeout_NMI(lowner); | |
5ba3f43e A |
823 | NMIPI_panic(cpu_to_cpumask(lock_cpu), SPINLOCK_TIMEOUT); |
824 | panic("mp_safe_spin_lock() timed out, lock: %p, owner thread: 0x%lx, current_thread: %p, owner on CPU 0x%x, time: %llu", | |
0a7de745 | 825 | lock, lowner, current_thread(), lock_cpu, mach_absolute_time()); |
fe8ab488 A |
826 | } |
827 | } | |
828 | return FALSE; | |
0a7de745 | 829 | } |
39236c6e A |
830 | } |
831 | ||
55e303ae A |
832 | /* |
833 | * All-CPU rendezvous: | |
0a7de745 | 834 | * - CPUs are signalled, |
55e303ae A |
835 | * - all execute the setup function (if specified), |
836 | * - rendezvous (i.e. all cpus reach a barrier), | |
837 | * - all execute the action function (if specified), | |
838 | * - rendezvous again, | |
839 | * - execute the teardown function (if specified), and then | |
840 | * - resume. | |
841 | * | |
842 | * Note that the supplied external functions _must_ be reentrant and aware | |
843 | * that they are running in parallel and in an unknown lock context. | |
844 | */ | |
845 | ||
846 | static void | |
39037602 | 847 | mp_rendezvous_action(__unused void *null) |
55e303ae | 848 | { |
0a7de745 A |
849 | boolean_t intrs_enabled; |
850 | uint64_t tsc_spin_start; | |
55e303ae | 851 | |
d9a64523 A |
852 | /* |
853 | * Note that mp_rv_lock was acquired by the thread that initiated the | |
854 | * rendezvous and must have been acquired before we enter | |
855 | * mp_rendezvous_action(). | |
856 | */ | |
857 | current_cpu_datap()->cpu_rendezvous_in_progress = TRUE; | |
858 | ||
55e303ae | 859 | /* setup function */ |
0a7de745 | 860 | if (mp_rv_setup_func != NULL) { |
55e303ae | 861 | mp_rv_setup_func(mp_rv_func_arg); |
0a7de745 | 862 | } |
2d21ac55 A |
863 | |
864 | intrs_enabled = ml_get_interrupts_enabled(); | |
865 | ||
55e303ae | 866 | /* spin on entry rendezvous */ |
0c530ab8 | 867 | atomic_incl(&mp_rv_entry, 1); |
39236c6e | 868 | tsc_spin_start = rdtsc64(); |
490019cf | 869 | |
0c530ab8 | 870 | while (mp_rv_entry < mp_rv_ncpus) { |
2d21ac55 | 871 | /* poll for pesky tlb flushes if interrupts disabled */ |
0a7de745 | 872 | if (!intrs_enabled) { |
2d21ac55 | 873 | handle_pending_TLB_flushes(); |
0a7de745 | 874 | } |
490019cf A |
875 | if (mp_spin_timeout(tsc_spin_start)) { |
876 | 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()); | |
877 | } | |
0c530ab8 | 878 | } |
6d2010ae | 879 | |
55e303ae | 880 | /* action function */ |
0a7de745 | 881 | if (mp_rv_action_func != NULL) { |
55e303ae | 882 | mp_rv_action_func(mp_rv_func_arg); |
0a7de745 | 883 | } |
6d2010ae | 884 | |
55e303ae | 885 | /* spin on exit rendezvous */ |
0c530ab8 | 886 | atomic_incl(&mp_rv_exit, 1); |
39236c6e | 887 | tsc_spin_start = rdtsc64(); |
2d21ac55 | 888 | while (mp_rv_exit < mp_rv_ncpus) { |
0a7de745 | 889 | if (!intrs_enabled) { |
2d21ac55 | 890 | handle_pending_TLB_flushes(); |
0a7de745 A |
891 | } |
892 | if (mp_spin_timeout(tsc_spin_start)) { | |
490019cf | 893 | 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()); |
0a7de745 | 894 | } |
2d21ac55 | 895 | } |
6d2010ae | 896 | |
55e303ae | 897 | /* teardown function */ |
0a7de745 | 898 | if (mp_rv_teardown_func != NULL) { |
55e303ae | 899 | mp_rv_teardown_func(mp_rv_func_arg); |
0a7de745 | 900 | } |
0c530ab8 | 901 | |
d9a64523 A |
902 | current_cpu_datap()->cpu_rendezvous_in_progress = FALSE; |
903 | ||
0c530ab8 A |
904 | /* Bump completion count */ |
905 | atomic_incl(&mp_rv_complete, 1); | |
55e303ae A |
906 | } |
907 | ||
908 | void | |
0a7de745 A |
909 | mp_rendezvous(void (*setup_func)(void *), |
910 | void (*action_func)(void *), | |
911 | void (*teardown_func)(void *), | |
912 | void *arg) | |
55e303ae | 913 | { |
0a7de745 | 914 | uint64_t tsc_spin_start; |
55e303ae A |
915 | |
916 | if (!smp_initialized) { | |
0a7de745 | 917 | if (setup_func != NULL) { |
55e303ae | 918 | setup_func(arg); |
0a7de745 A |
919 | } |
920 | if (action_func != NULL) { | |
55e303ae | 921 | action_func(arg); |
0a7de745 A |
922 | } |
923 | if (teardown_func != NULL) { | |
55e303ae | 924 | teardown_func(arg); |
0a7de745 | 925 | } |
55e303ae A |
926 | return; |
927 | } | |
0a7de745 | 928 | |
55e303ae | 929 | /* obtain rendezvous lock */ |
d9a64523 | 930 | mp_rendezvous_lock(); |
55e303ae A |
931 | |
932 | /* set static function pointers */ | |
933 | mp_rv_setup_func = setup_func; | |
934 | mp_rv_action_func = action_func; | |
935 | mp_rv_teardown_func = teardown_func; | |
936 | mp_rv_func_arg = arg; | |
937 | ||
0c530ab8 A |
938 | mp_rv_entry = 0; |
939 | mp_rv_exit = 0; | |
940 | mp_rv_complete = 0; | |
55e303ae A |
941 | |
942 | /* | |
943 | * signal other processors, which will call mp_rendezvous_action() | |
2d21ac55 | 944 | * with interrupts disabled |
55e303ae | 945 | */ |
39037602 | 946 | mp_rv_ncpus = mp_cpus_call(CPUMASK_OTHERS, NOSYNC, &mp_rendezvous_action, NULL) + 1; |
55e303ae A |
947 | |
948 | /* call executor function on this cpu */ | |
39037602 | 949 | mp_rendezvous_action(NULL); |
55e303ae | 950 | |
0c530ab8 A |
951 | /* |
952 | * Spin for everyone to complete. | |
953 | * This is necessary to ensure that all processors have proceeded | |
954 | * from the exit barrier before we release the rendezvous structure. | |
955 | */ | |
39236c6e | 956 | tsc_spin_start = rdtsc64(); |
0c530ab8 | 957 | while (mp_rv_complete < mp_rv_ncpus) { |
0a7de745 | 958 | if (mp_spin_timeout(tsc_spin_start)) { |
490019cf | 959 | panic("mp_rendezvous() timeout: %ld of %d responses, start: 0x%llx, cur: 0x%llx", mp_rv_complete, mp_rv_ncpus, tsc_spin_start, rdtsc64()); |
0a7de745 | 960 | } |
0c530ab8 | 961 | } |
0a7de745 | 962 | |
2d21ac55 A |
963 | /* Tidy up */ |
964 | mp_rv_setup_func = NULL; | |
965 | mp_rv_action_func = NULL; | |
966 | mp_rv_teardown_func = NULL; | |
967 | mp_rv_func_arg = NULL; | |
968 | ||
55e303ae | 969 | /* release lock */ |
d9a64523 A |
970 | mp_rendezvous_unlock(); |
971 | } | |
972 | ||
973 | void | |
974 | mp_rendezvous_lock(void) | |
975 | { | |
976 | (void) mp_safe_spin_lock(&mp_rv_lock); | |
977 | } | |
978 | ||
979 | void | |
980 | mp_rendezvous_unlock(void) | |
981 | { | |
55e303ae A |
982 | simple_unlock(&mp_rv_lock); |
983 | } | |
984 | ||
0c530ab8 A |
985 | void |
986 | mp_rendezvous_break_lock(void) | |
987 | { | |
988 | simple_lock_init(&mp_rv_lock, 0); | |
989 | } | |
990 | ||
991 | static void | |
992 | setup_disable_intrs(__unused void * param_not_used) | |
993 | { | |
994 | /* disable interrupts before the first barrier */ | |
995 | boolean_t intr = ml_set_interrupts_enabled(FALSE); | |
996 | ||
997 | current_cpu_datap()->cpu_iflag = intr; | |
998 | DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__); | |
999 | } | |
1000 | ||
1001 | static void | |
1002 | teardown_restore_intrs(__unused void * param_not_used) | |
1003 | { | |
1004 | /* restore interrupt flag following MTRR changes */ | |
1005 | ml_set_interrupts_enabled(current_cpu_datap()->cpu_iflag); | |
1006 | DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__); | |
1007 | } | |
1008 | ||
1009 | /* | |
1010 | * A wrapper to mp_rendezvous() to call action_func() with interrupts disabled. | |
1011 | * This is exported for use by kexts. | |
1012 | */ | |
1013 | void | |
1014 | mp_rendezvous_no_intrs( | |
0a7de745 A |
1015 | void (*action_func)(void *), |
1016 | void *arg) | |
0c530ab8 A |
1017 | { |
1018 | mp_rendezvous(setup_disable_intrs, | |
0a7de745 A |
1019 | action_func, |
1020 | teardown_restore_intrs, | |
1021 | arg); | |
0c530ab8 A |
1022 | } |
1023 | ||
6d2010ae A |
1024 | |
1025 | typedef struct { | |
0a7de745 A |
1026 | queue_chain_t link; /* queue linkage */ |
1027 | void (*func)(void *, void *); /* routine to call */ | |
1028 | void *arg0; /* routine's 1st arg */ | |
1029 | void *arg1; /* routine's 2nd arg */ | |
1030 | cpumask_t *maskp; /* completion response mask */ | |
6d2010ae | 1031 | } mp_call_t; |
316670eb A |
1032 | |
1033 | ||
1034 | typedef struct { | |
0a7de745 A |
1035 | queue_head_t queue; |
1036 | decl_simple_lock_data(, lock); | |
316670eb | 1037 | } mp_call_queue_t; |
0a7de745 A |
1038 | #define MP_CPUS_CALL_BUFS_PER_CPU MAX_CPUS |
1039 | static mp_call_queue_t mp_cpus_call_freelist; | |
1040 | static mp_call_queue_t mp_cpus_call_head[MAX_CPUS]; | |
6d2010ae A |
1041 | |
1042 | static inline boolean_t | |
316670eb | 1043 | mp_call_head_lock(mp_call_queue_t *cqp) |
6d2010ae | 1044 | { |
0a7de745 | 1045 | boolean_t intrs_enabled; |
6d2010ae A |
1046 | |
1047 | intrs_enabled = ml_set_interrupts_enabled(FALSE); | |
0a7de745 | 1048 | simple_lock(&cqp->lock, LCK_GRP_NULL); |
6d2010ae A |
1049 | |
1050 | return intrs_enabled; | |
1051 | } | |
1052 | ||
5ba3f43e A |
1053 | /* |
1054 | * Deliver an NMIPI to a set of processors to cause them to panic . | |
1055 | */ | |
fe8ab488 | 1056 | void |
0a7de745 A |
1057 | NMIPI_panic(cpumask_t cpu_mask, NMI_reason_t why) |
1058 | { | |
d9a64523 A |
1059 | unsigned int cpu; |
1060 | cpumask_t cpu_bit; | |
fe8ab488 A |
1061 | uint64_t deadline; |
1062 | ||
5c9f4661 | 1063 | NMIPI_enable(TRUE); |
5ba3f43e A |
1064 | NMI_panic_reason = why; |
1065 | ||
fe8ab488 | 1066 | for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) { |
0a7de745 | 1067 | if ((cpu_mask & cpu_bit) == 0) { |
5ba3f43e | 1068 | continue; |
0a7de745 | 1069 | } |
5ba3f43e A |
1070 | cpu_datap(cpu)->cpu_NMI_acknowledged = FALSE; |
1071 | cpu_NMI_interrupt(cpu); | |
1072 | } | |
1073 | ||
1074 | /* Wait (only so long) for NMi'ed cpus to respond */ | |
1075 | deadline = mach_absolute_time() + LockTimeOut; | |
1076 | for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) { | |
0a7de745 | 1077 | if ((cpu_mask & cpu_bit) == 0) { |
5ba3f43e | 1078 | continue; |
0a7de745 | 1079 | } |
5ba3f43e | 1080 | while (!cpu_datap(cpu)->cpu_NMI_acknowledged && |
0a7de745 | 1081 | mach_absolute_time() < deadline) { |
5ba3f43e A |
1082 | cpu_pause(); |
1083 | } | |
fe8ab488 | 1084 | } |
fe8ab488 A |
1085 | } |
1086 | ||
1087 | #if MACH_ASSERT | |
6d2010ae | 1088 | static inline boolean_t |
316670eb | 1089 | mp_call_head_is_locked(mp_call_queue_t *cqp) |
6d2010ae A |
1090 | { |
1091 | return !ml_get_interrupts_enabled() && | |
0a7de745 | 1092 | hw_lock_held((hw_lock_t)&cqp->lock); |
6d2010ae | 1093 | } |
fe8ab488 | 1094 | #endif |
6d2010ae A |
1095 | |
1096 | static inline void | |
316670eb | 1097 | mp_call_head_unlock(mp_call_queue_t *cqp, boolean_t intrs_enabled) |
6d2010ae | 1098 | { |
316670eb | 1099 | simple_unlock(&cqp->lock); |
6d2010ae A |
1100 | ml_set_interrupts_enabled(intrs_enabled); |
1101 | } | |
1102 | ||
1103 | static inline mp_call_t * | |
1104 | mp_call_alloc(void) | |
1105 | { | |
0a7de745 A |
1106 | mp_call_t *callp = NULL; |
1107 | boolean_t intrs_enabled; | |
1108 | mp_call_queue_t *cqp = &mp_cpus_call_freelist; | |
316670eb A |
1109 | |
1110 | intrs_enabled = mp_call_head_lock(cqp); | |
0a7de745 | 1111 | if (!queue_empty(&cqp->queue)) { |
316670eb | 1112 | queue_remove_first(&cqp->queue, callp, typeof(callp), link); |
0a7de745 | 1113 | } |
316670eb | 1114 | mp_call_head_unlock(cqp, intrs_enabled); |
6d2010ae | 1115 | |
6d2010ae A |
1116 | return callp; |
1117 | } | |
1118 | ||
1119 | static inline void | |
1120 | mp_call_free(mp_call_t *callp) | |
0c530ab8 | 1121 | { |
0a7de745 A |
1122 | boolean_t intrs_enabled; |
1123 | mp_call_queue_t *cqp = &mp_cpus_call_freelist; | |
316670eb A |
1124 | |
1125 | intrs_enabled = mp_call_head_lock(cqp); | |
1126 | queue_enter_first(&cqp->queue, callp, typeof(callp), link); | |
1127 | mp_call_head_unlock(cqp, intrs_enabled); | |
6d2010ae A |
1128 | } |
1129 | ||
1130 | static inline mp_call_t * | |
316670eb | 1131 | mp_call_dequeue_locked(mp_call_queue_t *cqp) |
6d2010ae | 1132 | { |
0a7de745 | 1133 | mp_call_t *callp = NULL; |
0c530ab8 | 1134 | |
316670eb | 1135 | assert(mp_call_head_is_locked(cqp)); |
0a7de745 | 1136 | if (!queue_empty(&cqp->queue)) { |
316670eb | 1137 | queue_remove_first(&cqp->queue, callp, typeof(callp), link); |
0a7de745 | 1138 | } |
6d2010ae A |
1139 | return callp; |
1140 | } | |
1141 | ||
316670eb A |
1142 | static inline void |
1143 | mp_call_enqueue_locked( | |
0a7de745 A |
1144 | mp_call_queue_t *cqp, |
1145 | mp_call_t *callp) | |
316670eb A |
1146 | { |
1147 | queue_enter(&cqp->queue, callp, typeof(callp), link); | |
1148 | } | |
1149 | ||
6d2010ae A |
1150 | /* Called on the boot processor to initialize global structures */ |
1151 | static void | |
1152 | mp_cpus_call_init(void) | |
1153 | { | |
0a7de745 | 1154 | mp_call_queue_t *cqp = &mp_cpus_call_freelist; |
316670eb | 1155 | |
6d2010ae | 1156 | DBG("mp_cpus_call_init()\n"); |
316670eb A |
1157 | simple_lock_init(&cqp->lock, 0); |
1158 | queue_init(&cqp->queue); | |
6d2010ae A |
1159 | } |
1160 | ||
1161 | /* | |
fe8ab488 | 1162 | * Called at processor registration to add call buffers to the free list |
6d2010ae | 1163 | * and to initialize the per-cpu call queue. |
6d2010ae | 1164 | */ |
fe8ab488 A |
1165 | void |
1166 | mp_cpus_call_cpu_init(int cpu) | |
6d2010ae | 1167 | { |
0a7de745 A |
1168 | int i; |
1169 | mp_call_queue_t *cqp = &mp_cpus_call_head[cpu]; | |
1170 | mp_call_t *callp; | |
6d2010ae | 1171 | |
316670eb A |
1172 | simple_lock_init(&cqp->lock, 0); |
1173 | queue_init(&cqp->queue); | |
6d2010ae | 1174 | for (i = 0; i < MP_CPUS_CALL_BUFS_PER_CPU; i++) { |
f427ee49 | 1175 | callp = zalloc_permanent_type(mp_call_t); |
6d2010ae | 1176 | mp_call_free(callp); |
0c530ab8 | 1177 | } |
6d2010ae | 1178 | |
fe8ab488 | 1179 | DBG("mp_cpus_call_init(%d) done\n", cpu); |
0c530ab8 A |
1180 | } |
1181 | ||
2d21ac55 A |
1182 | /* |
1183 | * This is called from cpu_signal_handler() to process an MP_CALL signal. | |
6d2010ae | 1184 | * And also from i386_deactivate_cpu() when a cpu is being taken offline. |
2d21ac55 A |
1185 | */ |
1186 | static void | |
1187 | mp_cpus_call_action(void) | |
1188 | { | |
0a7de745 A |
1189 | mp_call_queue_t *cqp; |
1190 | boolean_t intrs_enabled; | |
1191 | mp_call_t *callp; | |
1192 | mp_call_t call; | |
6d2010ae A |
1193 | |
1194 | assert(!ml_get_interrupts_enabled()); | |
316670eb A |
1195 | cqp = &mp_cpus_call_head[cpu_number()]; |
1196 | intrs_enabled = mp_call_head_lock(cqp); | |
1197 | while ((callp = mp_call_dequeue_locked(cqp)) != NULL) { | |
6d2010ae A |
1198 | /* Copy call request to the stack to free buffer */ |
1199 | call = *callp; | |
1200 | mp_call_free(callp); | |
1201 | if (call.func != NULL) { | |
316670eb | 1202 | mp_call_head_unlock(cqp, intrs_enabled); |
6d2010ae A |
1203 | KERNEL_DEBUG_CONSTANT( |
1204 | TRACE_MP_CPUS_CALL_ACTION, | |
4bd07ac2 A |
1205 | VM_KERNEL_UNSLIDE(call.func), VM_KERNEL_UNSLIDE_OR_PERM(call.arg0), |
1206 | VM_KERNEL_UNSLIDE_OR_PERM(call.arg1), VM_KERNEL_ADDRPERM(call.maskp), 0); | |
6d2010ae | 1207 | call.func(call.arg0, call.arg1); |
316670eb | 1208 | (void) mp_call_head_lock(cqp); |
6d2010ae | 1209 | } |
0a7de745 | 1210 | if (call.maskp != NULL) { |
fe8ab488 | 1211 | i_bit_set(cpu_number(), call.maskp); |
0a7de745 | 1212 | } |
6d2010ae | 1213 | } |
316670eb | 1214 | mp_call_head_unlock(cqp, intrs_enabled); |
2d21ac55 A |
1215 | } |
1216 | ||
1217 | /* | |
1218 | * mp_cpus_call() runs a given function on cpus specified in a given cpu mask. | |
6d2010ae A |
1219 | * Possible modes are: |
1220 | * SYNC: function is called serially on target cpus in logical cpu order | |
1221 | * waiting for each call to be acknowledged before proceeding | |
1222 | * ASYNC: function call is queued to the specified cpus | |
1223 | * waiting for all calls to complete in parallel before returning | |
1224 | * NOSYNC: function calls are queued | |
0a7de745 | 1225 | * but we return before confirmation of calls completing. |
2d21ac55 A |
1226 | * The action function may be NULL. |
1227 | * The cpu mask may include the local cpu. Offline cpus are ignored. | |
6d2010ae | 1228 | * The return value is the number of cpus on which the call was made or queued. |
2d21ac55 A |
1229 | */ |
1230 | cpu_t | |
1231 | mp_cpus_call( | |
0a7de745 A |
1232 | cpumask_t cpus, |
1233 | mp_sync_t mode, | |
1234 | void (*action_func)(void *), | |
1235 | void *arg) | |
6d2010ae A |
1236 | { |
1237 | return mp_cpus_call1( | |
0a7de745 A |
1238 | cpus, |
1239 | mode, | |
1240 | (void (*)(void *, void *))action_func, | |
1241 | arg, | |
1242 | NULL, | |
1243 | NULL); | |
6d2010ae A |
1244 | } |
1245 | ||
1246 | static void | |
0a7de745 A |
1247 | mp_cpus_call_wait(boolean_t intrs_enabled, |
1248 | cpumask_t cpus_called, | |
1249 | cpumask_t *cpus_responded) | |
6d2010ae | 1250 | { |
0a7de745 A |
1251 | mp_call_queue_t *cqp; |
1252 | uint64_t tsc_spin_start; | |
6d2010ae | 1253 | |
39037602 | 1254 | assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0); |
316670eb | 1255 | cqp = &mp_cpus_call_head[cpu_number()]; |
6d2010ae | 1256 | |
39236c6e | 1257 | tsc_spin_start = rdtsc64(); |
fe8ab488 | 1258 | while (*cpus_responded != cpus_called) { |
6d2010ae | 1259 | if (!intrs_enabled) { |
316670eb | 1260 | /* Sniffing w/o locking */ |
0a7de745 | 1261 | if (!queue_empty(&cqp->queue)) { |
6d2010ae | 1262 | mp_cpus_call_action(); |
0a7de745 | 1263 | } |
fe8ab488 A |
1264 | cpu_signal_handler(NULL); |
1265 | } | |
1266 | if (mp_spin_timeout(tsc_spin_start)) { | |
0a7de745 | 1267 | cpumask_t cpus_unresponsive; |
fe8ab488 | 1268 | |
fe8ab488 | 1269 | cpus_unresponsive = cpus_called & ~(*cpus_responded); |
5ba3f43e | 1270 | NMIPI_panic(cpus_unresponsive, CROSSCALL_TIMEOUT); |
3e170ce0 | 1271 | panic("mp_cpus_call_wait() timeout, cpus: 0x%llx", |
0a7de745 | 1272 | cpus_unresponsive); |
6d2010ae | 1273 | } |
6d2010ae A |
1274 | } |
1275 | } | |
1276 | ||
1277 | cpu_t | |
1278 | mp_cpus_call1( | |
0a7de745 A |
1279 | cpumask_t cpus, |
1280 | mp_sync_t mode, | |
1281 | void (*action_func)(void *, void *), | |
1282 | void *arg0, | |
1283 | void *arg1, | |
1284 | cpumask_t *cpus_calledp) | |
1285 | { | |
1286 | cpu_t cpu = 0; | |
1287 | boolean_t intrs_enabled = FALSE; | |
1288 | boolean_t call_self = FALSE; | |
1289 | cpumask_t cpus_called = 0; | |
1290 | cpumask_t cpus_responded = 0; | |
1291 | long cpus_call_count = 0; | |
1292 | uint64_t tsc_spin_start; | |
1293 | boolean_t topo_lock; | |
6d2010ae A |
1294 | |
1295 | KERNEL_DEBUG_CONSTANT( | |
1296 | TRACE_MP_CPUS_CALL | DBG_FUNC_START, | |
4bd07ac2 | 1297 | cpus, mode, VM_KERNEL_UNSLIDE(action_func), VM_KERNEL_UNSLIDE_OR_PERM(arg0), VM_KERNEL_UNSLIDE_OR_PERM(arg1)); |
2d21ac55 A |
1298 | |
1299 | if (!smp_initialized) { | |
0a7de745 | 1300 | if ((cpus & CPUMASK_SELF) == 0) { |
6d2010ae | 1301 | goto out; |
0a7de745 | 1302 | } |
2d21ac55 | 1303 | if (action_func != NULL) { |
6d2010ae A |
1304 | intrs_enabled = ml_set_interrupts_enabled(FALSE); |
1305 | action_func(arg0, arg1); | |
2d21ac55 A |
1306 | ml_set_interrupts_enabled(intrs_enabled); |
1307 | } | |
6d2010ae A |
1308 | call_self = TRUE; |
1309 | goto out; | |
2d21ac55 | 1310 | } |
2d21ac55 | 1311 | |
6d2010ae A |
1312 | /* |
1313 | * Queue the call for each non-local requested cpu. | |
fe8ab488 A |
1314 | * This is performed under the topo lock to prevent changes to |
1315 | * cpus online state and to prevent concurrent rendezvouses -- | |
1316 | * although an exception is made if we're calling only the master | |
1317 | * processor since that always remains active. Note: this exception | |
1318 | * is expected for longterm timer nosync cross-calls to the master cpu. | |
0a7de745 | 1319 | */ |
fe8ab488 A |
1320 | mp_disable_preemption(); |
1321 | intrs_enabled = ml_get_interrupts_enabled(); | |
1322 | topo_lock = (cpus != cpu_to_cpumask(master_cpu)); | |
1323 | if (topo_lock) { | |
1324 | ml_set_interrupts_enabled(FALSE); | |
1325 | (void) mp_safe_spin_lock(&x86_topo_lock); | |
1326 | } | |
2d21ac55 A |
1327 | for (cpu = 0; cpu < (cpu_t) real_ncpus; cpu++) { |
1328 | if (((cpu_to_cpumask(cpu) & cpus) == 0) || | |
0a7de745 | 1329 | !cpu_is_running(cpu)) { |
2d21ac55 | 1330 | continue; |
0a7de745 | 1331 | } |
fe8ab488 | 1332 | tsc_spin_start = rdtsc64(); |
2d21ac55 A |
1333 | if (cpu == (cpu_t) cpu_number()) { |
1334 | /* | |
1335 | * We don't IPI ourself and if calling asynchronously, | |
1336 | * we defer our call until we have signalled all others. | |
1337 | */ | |
1338 | call_self = TRUE; | |
1339 | if (mode == SYNC && action_func != NULL) { | |
6d2010ae A |
1340 | KERNEL_DEBUG_CONSTANT( |
1341 | TRACE_MP_CPUS_CALL_LOCAL, | |
316670eb | 1342 | VM_KERNEL_UNSLIDE(action_func), |
4bd07ac2 | 1343 | VM_KERNEL_UNSLIDE_OR_PERM(arg0), VM_KERNEL_UNSLIDE_OR_PERM(arg1), 0, 0); |
6d2010ae | 1344 | action_func(arg0, arg1); |
2d21ac55 A |
1345 | } |
1346 | } else { | |
1347 | /* | |
6d2010ae | 1348 | * Here to queue a call to cpu and IPI. |
2d21ac55 | 1349 | */ |
0a7de745 A |
1350 | mp_call_t *callp = NULL; |
1351 | mp_call_queue_t *cqp = &mp_cpus_call_head[cpu]; | |
1352 | boolean_t intrs_inner; | |
316670eb | 1353 | |
0a7de745 A |
1354 | queue_call: |
1355 | if (callp == NULL) { | |
316670eb | 1356 | callp = mp_call_alloc(); |
0a7de745 | 1357 | } |
fe8ab488 | 1358 | intrs_inner = mp_call_head_lock(cqp); |
39037602 A |
1359 | if (callp == NULL) { |
1360 | mp_call_head_unlock(cqp, intrs_inner); | |
1361 | KERNEL_DEBUG_CONSTANT( | |
1362 | TRACE_MP_CPUS_CALL_NOBUF, | |
1363 | cpu, 0, 0, 0, 0); | |
1364 | if (!intrs_inner) { | |
1365 | /* Sniffing w/o locking */ | |
0a7de745 | 1366 | if (!queue_empty(&cqp->queue)) { |
39037602 | 1367 | mp_cpus_call_action(); |
0a7de745 | 1368 | } |
39037602 | 1369 | handle_pending_TLB_flushes(); |
2d21ac55 | 1370 | } |
0a7de745 | 1371 | if (mp_spin_timeout(tsc_spin_start)) { |
39037602 | 1372 | panic("mp_cpus_call1() timeout start: 0x%llx, cur: 0x%llx", |
0a7de745 A |
1373 | tsc_spin_start, rdtsc64()); |
1374 | } | |
39037602 | 1375 | goto queue_call; |
6d2010ae | 1376 | } |
39037602 | 1377 | callp->maskp = (mode == NOSYNC) ? NULL : &cpus_responded; |
316670eb A |
1378 | callp->func = action_func; |
1379 | callp->arg0 = arg0; | |
1380 | callp->arg1 = arg1; | |
1381 | mp_call_enqueue_locked(cqp, callp); | |
fe8ab488 | 1382 | cpus_call_count++; |
6d2010ae A |
1383 | cpus_called |= cpu_to_cpumask(cpu); |
1384 | i386_signal_cpu(cpu, MP_CALL, ASYNC); | |
fe8ab488 | 1385 | mp_call_head_unlock(cqp, intrs_inner); |
6d2010ae | 1386 | if (mode == SYNC) { |
fe8ab488 | 1387 | mp_cpus_call_wait(intrs_inner, cpus_called, &cpus_responded); |
2d21ac55 A |
1388 | } |
1389 | } | |
1390 | } | |
fe8ab488 A |
1391 | if (topo_lock) { |
1392 | simple_unlock(&x86_topo_lock); | |
1393 | ml_set_interrupts_enabled(intrs_enabled); | |
1394 | } | |
2d21ac55 | 1395 | |
6d2010ae | 1396 | /* Call locally if mode not SYNC */ |
0a7de745 | 1397 | if (mode != SYNC && call_self) { |
6d2010ae A |
1398 | KERNEL_DEBUG_CONSTANT( |
1399 | TRACE_MP_CPUS_CALL_LOCAL, | |
4bd07ac2 | 1400 | VM_KERNEL_UNSLIDE(action_func), VM_KERNEL_UNSLIDE_OR_PERM(arg0), VM_KERNEL_UNSLIDE_OR_PERM(arg1), 0, 0); |
6d2010ae A |
1401 | if (action_func != NULL) { |
1402 | ml_set_interrupts_enabled(FALSE); | |
1403 | action_func(arg0, arg1); | |
2d21ac55 A |
1404 | ml_set_interrupts_enabled(intrs_enabled); |
1405 | } | |
2d21ac55 | 1406 | } |
2d21ac55 | 1407 | |
6d2010ae | 1408 | /* For ASYNC, now wait for all signaled cpus to complete their calls */ |
0a7de745 | 1409 | if (mode == ASYNC) { |
fe8ab488 | 1410 | mp_cpus_call_wait(intrs_enabled, cpus_called, &cpus_responded); |
0a7de745 | 1411 | } |
6d2010ae | 1412 | |
39037602 A |
1413 | /* Safe to allow pre-emption now */ |
1414 | mp_enable_preemption(); | |
1415 | ||
6d2010ae | 1416 | out: |
0a7de745 | 1417 | if (call_self) { |
fe8ab488 A |
1418 | cpus_called |= cpu_to_cpumask(cpu); |
1419 | cpus_call_count++; | |
1420 | } | |
6d2010ae | 1421 | |
0a7de745 | 1422 | if (cpus_calledp) { |
6d2010ae | 1423 | *cpus_calledp = cpus_called; |
0a7de745 | 1424 | } |
6d2010ae A |
1425 | |
1426 | KERNEL_DEBUG_CONSTANT( | |
1427 | TRACE_MP_CPUS_CALL | DBG_FUNC_END, | |
39037602 | 1428 | cpus_call_count, cpus_called, 0, 0, 0); |
2d21ac55 | 1429 | |
fe8ab488 | 1430 | return (cpu_t) cpus_call_count; |
2d21ac55 A |
1431 | } |
1432 | ||
6d2010ae | 1433 | |
2d21ac55 | 1434 | static void |
39037602 | 1435 | mp_broadcast_action(__unused void *null) |
2d21ac55 | 1436 | { |
0a7de745 A |
1437 | /* call action function */ |
1438 | if (mp_bc_action_func != NULL) { | |
1439 | mp_bc_action_func(mp_bc_func_arg); | |
1440 | } | |
2d21ac55 | 1441 | |
0a7de745 A |
1442 | /* if we're the last one through, wake up the instigator */ |
1443 | if (atomic_decl_and_test(&mp_bc_count, 1)) { | |
1444 | thread_wakeup(((event_t)(uintptr_t) &mp_bc_count)); | |
1445 | } | |
2d21ac55 A |
1446 | } |
1447 | ||
1448 | /* | |
1449 | * mp_broadcast() runs a given function on all active cpus. | |
1450 | * The caller blocks until the functions has run on all cpus. | |
0a7de745 | 1451 | * The caller will also block if there is another pending broadcast. |
2d21ac55 A |
1452 | */ |
1453 | void | |
1454 | mp_broadcast( | |
0a7de745 A |
1455 | void (*action_func)(void *), |
1456 | void *arg) | |
1457 | { | |
1458 | if (!smp_initialized) { | |
1459 | if (action_func != NULL) { | |
1460 | action_func(arg); | |
1461 | } | |
1462 | return; | |
1463 | } | |
1464 | ||
1465 | /* obtain broadcast lock */ | |
1466 | lck_mtx_lock(&mp_bc_lock); | |
1467 | ||
1468 | /* set static function pointers */ | |
1469 | mp_bc_action_func = action_func; | |
1470 | mp_bc_func_arg = arg; | |
1471 | ||
1472 | assert_wait((event_t)(uintptr_t)&mp_bc_count, THREAD_UNINT); | |
1473 | ||
1474 | /* | |
1475 | * signal other processors, which will call mp_broadcast_action() | |
1476 | */ | |
1477 | mp_bc_count = real_ncpus; /* assume max possible active */ | |
cb323159 | 1478 | mp_bc_ncpus = mp_cpus_call(CPUMASK_ALL, NOSYNC, *mp_broadcast_action, NULL); |
0a7de745 A |
1479 | atomic_decl(&mp_bc_count, real_ncpus - mp_bc_ncpus); /* subtract inactive */ |
1480 | ||
0a7de745 A |
1481 | /* block for other cpus to have run action_func */ |
1482 | if (mp_bc_ncpus > 1) { | |
1483 | thread_block(THREAD_CONTINUE_NULL); | |
1484 | } else { | |
1485 | clear_wait(current_thread(), THREAD_AWAKENED); | |
1486 | } | |
1487 | ||
1488 | /* release lock */ | |
1489 | lck_mtx_unlock(&mp_bc_lock); | |
2d21ac55 A |
1490 | } |
1491 | ||
fe8ab488 A |
1492 | void |
1493 | mp_cpus_kick(cpumask_t cpus) | |
1494 | { | |
0a7de745 A |
1495 | cpu_t cpu; |
1496 | boolean_t intrs_enabled = FALSE; | |
fe8ab488 A |
1497 | |
1498 | intrs_enabled = ml_set_interrupts_enabled(FALSE); | |
1499 | mp_safe_spin_lock(&x86_topo_lock); | |
1500 | ||
1501 | for (cpu = 0; cpu < (cpu_t) real_ncpus; cpu++) { | |
f427ee49 | 1502 | if (((cpu_to_cpumask(cpu) & cpus) == 0) |
0a7de745 A |
1503 | || !cpu_is_running(cpu)) { |
1504 | continue; | |
fe8ab488 A |
1505 | } |
1506 | ||
1507 | lapic_send_ipi(cpu, LAPIC_VECTOR(KICK)); | |
1508 | } | |
1509 | ||
1510 | simple_unlock(&x86_topo_lock); | |
1511 | ml_set_interrupts_enabled(intrs_enabled); | |
1512 | } | |
1513 | ||
2d21ac55 A |
1514 | void |
1515 | i386_activate_cpu(void) | |
1516 | { | |
0a7de745 | 1517 | cpu_data_t *cdp = current_cpu_datap(); |
2d21ac55 A |
1518 | |
1519 | assert(!ml_get_interrupts_enabled()); | |
1520 | ||
1521 | if (!smp_initialized) { | |
1522 | cdp->cpu_running = TRUE; | |
1523 | return; | |
1524 | } | |
1525 | ||
5ba3f43e | 1526 | mp_safe_spin_lock(&x86_topo_lock); |
2d21ac55 | 1527 | cdp->cpu_running = TRUE; |
7e4a7d39 | 1528 | started_cpu(); |
0a7de745 | 1529 | pmap_tlbi_range(0, ~0ULL, true, 0); |
2d21ac55 A |
1530 | simple_unlock(&x86_topo_lock); |
1531 | } | |
1532 | ||
1533 | void | |
1534 | i386_deactivate_cpu(void) | |
1535 | { | |
0a7de745 | 1536 | cpu_data_t *cdp = current_cpu_datap(); |
2d21ac55 A |
1537 | |
1538 | assert(!ml_get_interrupts_enabled()); | |
0a7de745 | 1539 | |
bd504ef0 A |
1540 | KERNEL_DEBUG_CONSTANT( |
1541 | TRACE_MP_CPU_DEACTIVATE | DBG_FUNC_START, | |
1542 | 0, 0, 0, 0, 0); | |
2d21ac55 | 1543 | |
5ba3f43e | 1544 | mp_safe_spin_lock(&x86_topo_lock); |
2d21ac55 A |
1545 | cdp->cpu_running = FALSE; |
1546 | simple_unlock(&x86_topo_lock); | |
1547 | ||
bd504ef0 A |
1548 | /* |
1549 | * Move all of this cpu's timers to the master/boot cpu, | |
1550 | * and poke it in case there's a sooner deadline for it to schedule. | |
1551 | */ | |
c910b4d9 | 1552 | timer_queue_shutdown(&cdp->rtclock_timer.queue); |
39236c6e | 1553 | mp_cpus_call(cpu_to_cpumask(master_cpu), ASYNC, timer_queue_expire_local, NULL); |
c910b4d9 | 1554 | |
5ba3f43e A |
1555 | #if MONOTONIC |
1556 | mt_cpu_down(cdp); | |
1557 | #endif /* MONOTONIC */ | |
1558 | ||
2d21ac55 | 1559 | /* |
bd504ef0 A |
1560 | * Open an interrupt window |
1561 | * and ensure any pending IPI or timer is serviced | |
2d21ac55 | 1562 | */ |
bd504ef0 A |
1563 | mp_disable_preemption(); |
1564 | ml_set_interrupts_enabled(TRUE); | |
1565 | ||
0a7de745 | 1566 | while (cdp->cpu_signals && x86_lcpu()->rtcDeadline != EndOfAllTime) { |
bd504ef0 | 1567 | cpu_pause(); |
0a7de745 | 1568 | } |
bd504ef0 A |
1569 | /* |
1570 | * Ensure there's no remaining timer deadline set | |
1571 | * - AICPM may have left one active. | |
1572 | */ | |
1573 | setPop(0); | |
1574 | ||
1575 | ml_set_interrupts_enabled(FALSE); | |
1576 | mp_enable_preemption(); | |
1577 | ||
1578 | KERNEL_DEBUG_CONSTANT( | |
1579 | TRACE_MP_CPU_DEACTIVATE | DBG_FUNC_END, | |
1580 | 0, 0, 0, 0, 0); | |
2d21ac55 A |
1581 | } |
1582 | ||
0a7de745 | 1583 | int pmsafe_debug = 1; |
2d21ac55 | 1584 | |
0a7de745 A |
1585 | #if MACH_KDP |
1586 | volatile boolean_t mp_kdp_trap = FALSE; | |
1587 | volatile boolean_t mp_kdp_is_NMI = FALSE; | |
1588 | volatile unsigned long mp_kdp_ncpus; | |
1589 | boolean_t mp_kdp_state; | |
91447636 | 1590 | |
55e303ae A |
1591 | |
1592 | void | |
5ba3f43e | 1593 | mp_kdp_enter(boolean_t proceed_on_failure) |
55e303ae | 1594 | { |
0a7de745 A |
1595 | unsigned int cpu; |
1596 | unsigned int ncpus = 0; | |
1597 | unsigned int my_cpu; | |
1598 | uint64_t tsc_timeout; | |
55e303ae A |
1599 | |
1600 | DBG("mp_kdp_enter()\n"); | |
1601 | ||
1602 | /* | |
1603 | * Here to enter the debugger. | |
1604 | * In case of races, only one cpu is allowed to enter kdp after | |
1605 | * stopping others. | |
1606 | */ | |
91447636 | 1607 | mp_kdp_state = ml_set_interrupts_enabled(FALSE); |
060df5ea | 1608 | my_cpu = cpu_number(); |
7ddcb079 A |
1609 | |
1610 | if (my_cpu == (unsigned) debugger_cpu) { | |
1611 | kprintf("\n\nRECURSIVE DEBUGGER ENTRY DETECTED\n\n"); | |
1612 | kdp_reset(); | |
1613 | return; | |
1614 | } | |
1615 | ||
5ba3f43e A |
1616 | uint64_t start_time = cpu_datap(my_cpu)->debugger_entry_time = mach_absolute_time(); |
1617 | int locked = 0; | |
1618 | while (!locked || mp_kdp_trap) { | |
1619 | if (locked) { | |
1620 | simple_unlock(&x86_topo_lock); | |
1621 | } | |
1622 | if (proceed_on_failure) { | |
1623 | if (mach_absolute_time() - start_time > 500000000ll) { | |
d9a64523 | 1624 | paniclog_append_noflush("mp_kdp_enter() can't get x86_topo_lock! Debugging anyway! #YOLO\n"); |
5ba3f43e A |
1625 | break; |
1626 | } | |
0a7de745 | 1627 | locked = simple_lock_try(&x86_topo_lock, LCK_GRP_NULL); |
5ba3f43e A |
1628 | if (!locked) { |
1629 | cpu_pause(); | |
1630 | } | |
1631 | } else { | |
1632 | mp_safe_spin_lock(&x86_topo_lock); | |
1633 | locked = TRUE; | |
1634 | } | |
2d21ac55 | 1635 | |
5ba3f43e A |
1636 | if (locked && mp_kdp_trap) { |
1637 | simple_unlock(&x86_topo_lock); | |
1638 | DBG("mp_kdp_enter() race lost\n"); | |
b0d623f7 | 1639 | #if MACH_KDP |
5ba3f43e | 1640 | mp_kdp_wait(TRUE, FALSE); |
b0d623f7 | 1641 | #endif |
5ba3f43e A |
1642 | locked = FALSE; |
1643 | } | |
55e303ae | 1644 | } |
5ba3f43e | 1645 | |
0a7de745 | 1646 | if (pmsafe_debug && !kdp_snapshot) { |
5ba3f43e | 1647 | pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_SAFE); |
0a7de745 | 1648 | } |
5ba3f43e | 1649 | |
593a1d5f | 1650 | debugger_cpu = my_cpu; |
060df5ea | 1651 | ncpus = 1; |
5ba3f43e | 1652 | atomic_incl((volatile long *)&mp_kdp_ncpus, 1); |
55e303ae | 1653 | mp_kdp_trap = TRUE; |
060df5ea | 1654 | debugger_entry_time = cpu_datap(my_cpu)->debugger_entry_time; |
55e303ae | 1655 | |
0c530ab8 A |
1656 | /* |
1657 | * Deliver a nudge to other cpus, counting how many | |
1658 | */ | |
55e303ae | 1659 | DBG("mp_kdp_enter() signaling other processors\n"); |
2d21ac55 | 1660 | if (force_immediate_debugger_NMI == FALSE) { |
060df5ea | 1661 | for (cpu = 0; cpu < real_ncpus; cpu++) { |
0a7de745 | 1662 | if (cpu == my_cpu || !cpu_is_running(cpu)) { |
2d21ac55 | 1663 | continue; |
0a7de745 | 1664 | } |
2d21ac55 A |
1665 | ncpus++; |
1666 | i386_signal_cpu(cpu, MP_KDP, ASYNC); | |
1667 | } | |
1668 | /* | |
1669 | * Wait other processors to synchronize | |
1670 | */ | |
1671 | DBG("mp_kdp_enter() waiting for (%d) processors to suspend\n", ncpus); | |
0c530ab8 | 1672 | |
2d21ac55 A |
1673 | /* |
1674 | * This timeout is rather arbitrary; we don't want to NMI | |
1675 | * processors that are executing at potentially | |
1676 | * "unsafe-to-interrupt" points such as the trampolines, | |
1677 | * but neither do we want to lose state by waiting too long. | |
1678 | */ | |
39037602 | 1679 | tsc_timeout = rdtsc64() + (LockTimeOutTSC); |
0c530ab8 | 1680 | |
2d21ac55 A |
1681 | while (mp_kdp_ncpus != ncpus && rdtsc64() < tsc_timeout) { |
1682 | /* | |
1683 | * A TLB shootdown request may be pending--this would | |
1684 | * result in the requesting processor waiting in | |
1685 | * PMAP_UPDATE_TLBS() until this processor deals with it. | |
1686 | * Process it, so it can now enter mp_kdp_wait() | |
1687 | */ | |
1688 | handle_pending_TLB_flushes(); | |
1689 | cpu_pause(); | |
1690 | } | |
1691 | /* If we've timed out, and some processor(s) are still unresponsive, | |
5c9f4661 A |
1692 | * interrupt them with an NMI via the local APIC, iff a panic is |
1693 | * in progress. | |
0c530ab8 | 1694 | */ |
5c9f4661 A |
1695 | if (panic_active()) { |
1696 | NMIPI_enable(TRUE); | |
1697 | } | |
2d21ac55 | 1698 | if (mp_kdp_ncpus != ncpus) { |
d9a64523 | 1699 | unsigned int wait_cycles = 0; |
0a7de745 | 1700 | if (proceed_on_failure) { |
d9a64523 | 1701 | paniclog_append_noflush("mp_kdp_enter() timed-out on cpu %d, NMI-ing\n", my_cpu); |
0a7de745 | 1702 | } else { |
d9a64523 | 1703 | DBG("mp_kdp_enter() timed-out on cpu %d, NMI-ing\n", my_cpu); |
0a7de745 | 1704 | } |
2d21ac55 | 1705 | for (cpu = 0; cpu < real_ncpus; cpu++) { |
0a7de745 | 1706 | if (cpu == my_cpu || !cpu_is_running(cpu)) { |
2d21ac55 | 1707 | continue; |
0a7de745 | 1708 | } |
a39ff7e2 | 1709 | if (cpu_signal_pending(cpu, MP_KDP)) { |
d9a64523 | 1710 | cpu_datap(cpu)->cpu_NMI_acknowledged = FALSE; |
2d21ac55 | 1711 | cpu_NMI_interrupt(cpu); |
a39ff7e2 | 1712 | } |
2d21ac55 | 1713 | } |
39037602 A |
1714 | /* Wait again for the same timeout */ |
1715 | tsc_timeout = rdtsc64() + (LockTimeOutTSC); | |
1716 | while (mp_kdp_ncpus != ncpus && rdtsc64() < tsc_timeout) { | |
1717 | handle_pending_TLB_flushes(); | |
1718 | cpu_pause(); | |
d9a64523 | 1719 | ++wait_cycles; |
39037602 A |
1720 | } |
1721 | if (mp_kdp_ncpus != ncpus) { | |
d9a64523 A |
1722 | paniclog_append_noflush("mp_kdp_enter() NMI pending on cpus:"); |
1723 | for (cpu = 0; cpu < real_ncpus; cpu++) { | |
0a7de745 | 1724 | if (cpu_is_running(cpu) && !cpu_datap(cpu)->cpu_NMI_acknowledged) { |
d9a64523 | 1725 | paniclog_append_noflush(" %d", cpu); |
0a7de745 | 1726 | } |
d9a64523 A |
1727 | } |
1728 | paniclog_append_noflush("\n"); | |
1729 | if (proceed_on_failure) { | |
1730 | paniclog_append_noflush("mp_kdp_enter() timed-out during %s wait after NMI;" | |
1731 | "expected %u acks but received %lu after %u loops in %llu ticks\n", | |
0a7de745 | 1732 | (locked ? "locked" : "unlocked"), ncpus, mp_kdp_ncpus, wait_cycles, LockTimeOutTSC); |
d9a64523 A |
1733 | } else { |
1734 | panic("mp_kdp_enter() timed-out during %s wait after NMI;" | |
1735 | "expected %u acks but received %lu after %u loops in %llu ticks", | |
0a7de745 | 1736 | (locked ? "locked" : "unlocked"), ncpus, mp_kdp_ncpus, wait_cycles, LockTimeOutTSC); |
d9a64523 | 1737 | } |
39037602 | 1738 | } |
2d21ac55 | 1739 | } |
0a7de745 | 1740 | } else { |
0c530ab8 | 1741 | for (cpu = 0; cpu < real_ncpus; cpu++) { |
0a7de745 | 1742 | if (cpu == my_cpu || !cpu_is_running(cpu)) { |
0c530ab8 | 1743 | continue; |
0a7de745 | 1744 | } |
2d21ac55 | 1745 | cpu_NMI_interrupt(cpu); |
0c530ab8 | 1746 | } |
0a7de745 | 1747 | } |
0c530ab8 | 1748 | |
5ba3f43e A |
1749 | if (locked) { |
1750 | simple_unlock(&x86_topo_lock); | |
1751 | } | |
1752 | ||
bd504ef0 | 1753 | DBG("mp_kdp_enter() %d processors done %s\n", |
6d2010ae | 1754 | (int)mp_kdp_ncpus, (mp_kdp_ncpus == ncpus) ? "OK" : "timed out"); |
0a7de745 | 1755 | |
91447636 | 1756 | postcode(MP_KDP_ENTER); |
55e303ae A |
1757 | } |
1758 | ||
d9a64523 A |
1759 | boolean_t |
1760 | mp_kdp_all_cpus_halted() | |
1761 | { | |
1762 | unsigned int ncpus = 0, cpu = 0, my_cpu = 0; | |
1763 | ||
1764 | my_cpu = cpu_number(); | |
1765 | ncpus = 1; /* current CPU */ | |
1766 | for (cpu = 0; cpu < real_ncpus; cpu++) { | |
0a7de745 | 1767 | if (cpu == my_cpu || !cpu_is_running(cpu)) { |
d9a64523 | 1768 | continue; |
0a7de745 | 1769 | } |
d9a64523 A |
1770 | ncpus++; |
1771 | } | |
1772 | ||
0a7de745 | 1773 | return mp_kdp_ncpus == ncpus; |
d9a64523 A |
1774 | } |
1775 | ||
0c530ab8 A |
1776 | static boolean_t |
1777 | cpu_signal_pending(int cpu, mp_event_t event) | |
1778 | { | |
0a7de745 | 1779 | volatile int *signals = &cpu_datap(cpu)->cpu_signals; |
0c530ab8 A |
1780 | boolean_t retval = FALSE; |
1781 | ||
0a7de745 | 1782 | if (i_bit(event, signals)) { |
0c530ab8 | 1783 | retval = TRUE; |
0a7de745 | 1784 | } |
0c530ab8 A |
1785 | return retval; |
1786 | } | |
b0d623f7 | 1787 | |
0a7de745 A |
1788 | long |
1789 | kdp_x86_xcpu_invoke(const uint16_t lcpu, kdp_x86_xcpu_func_t func, | |
1790 | void *arg0, void *arg1) | |
b0d623f7 | 1791 | { |
0a7de745 | 1792 | if (lcpu > (real_ncpus - 1)) { |
b0d623f7 | 1793 | return -1; |
0a7de745 | 1794 | } |
b0d623f7 | 1795 | |
0a7de745 | 1796 | if (func == NULL) { |
b0d623f7 | 1797 | return -1; |
0a7de745 | 1798 | } |
b0d623f7 A |
1799 | |
1800 | kdp_xcpu_call_func.func = func; | |
0a7de745 | 1801 | kdp_xcpu_call_func.ret = -1; |
b0d623f7 A |
1802 | kdp_xcpu_call_func.arg0 = arg0; |
1803 | kdp_xcpu_call_func.arg1 = arg1; | |
1804 | kdp_xcpu_call_func.cpu = lcpu; | |
1805 | DBG("Invoking function %p on CPU %d\n", func, (int32_t)lcpu); | |
0a7de745 | 1806 | while (kdp_xcpu_call_func.cpu != KDP_XCPU_NONE) { |
b0d623f7 | 1807 | cpu_pause(); |
0a7de745 A |
1808 | } |
1809 | return kdp_xcpu_call_func.ret; | |
b0d623f7 A |
1810 | } |
1811 | ||
1812 | static void | |
1813 | kdp_x86_xcpu_poll(void) | |
1814 | { | |
1815 | if ((uint16_t)cpu_number() == kdp_xcpu_call_func.cpu) { | |
0a7de745 | 1816 | kdp_xcpu_call_func.ret = |
b0d623f7 | 1817 | kdp_xcpu_call_func.func(kdp_xcpu_call_func.arg0, |
0a7de745 A |
1818 | kdp_xcpu_call_func.arg1, |
1819 | cpu_number()); | |
b0d623f7 A |
1820 | kdp_xcpu_call_func.cpu = KDP_XCPU_NONE; |
1821 | } | |
1822 | } | |
0c530ab8 | 1823 | |
55e303ae | 1824 | static void |
b0d623f7 | 1825 | mp_kdp_wait(boolean_t flush, boolean_t isNMI) |
55e303ae | 1826 | { |
6601e61a | 1827 | DBG("mp_kdp_wait()\n"); |
813fb2f6 | 1828 | |
bd504ef0 | 1829 | current_cpu_datap()->debugger_ipi_time = mach_absolute_time(); |
b0d623f7 | 1830 | #if CONFIG_MCA |
2d21ac55 A |
1831 | /* If we've trapped due to a machine-check, save MCA registers */ |
1832 | mca_check_save(); | |
b0d623f7 | 1833 | #endif |
2d21ac55 | 1834 | |
2d21ac55 | 1835 | atomic_incl((volatile long *)&mp_kdp_ncpus, 1); |
b0d623f7 | 1836 | while (mp_kdp_trap || (isNMI == TRUE)) { |
0a7de745 | 1837 | /* |
2d21ac55 A |
1838 | * A TLB shootdown request may be pending--this would result |
1839 | * in the requesting processor waiting in PMAP_UPDATE_TLBS() | |
1840 | * until this processor handles it. | |
0c530ab8 A |
1841 | * Process it, so it can now enter mp_kdp_wait() |
1842 | */ | |
0a7de745 | 1843 | if (flush) { |
2d21ac55 | 1844 | handle_pending_TLB_flushes(); |
0a7de745 | 1845 | } |
b0d623f7 A |
1846 | |
1847 | kdp_x86_xcpu_poll(); | |
55e303ae A |
1848 | cpu_pause(); |
1849 | } | |
2d21ac55 | 1850 | |
0c530ab8 | 1851 | atomic_decl((volatile long *)&mp_kdp_ncpus, 1); |
55e303ae A |
1852 | DBG("mp_kdp_wait() done\n"); |
1853 | } | |
1854 | ||
1855 | void | |
1856 | mp_kdp_exit(void) | |
1857 | { | |
1858 | DBG("mp_kdp_exit()\n"); | |
593a1d5f | 1859 | debugger_cpu = -1; |
0c530ab8 | 1860 | atomic_decl((volatile long *)&mp_kdp_ncpus, 1); |
b0d623f7 A |
1861 | |
1862 | debugger_exit_time = mach_absolute_time(); | |
1863 | ||
55e303ae | 1864 | mp_kdp_trap = FALSE; |
39236c6e | 1865 | mfence(); |
55e303ae A |
1866 | |
1867 | /* Wait other processors to stop spinning. XXX needs timeout */ | |
1868 | DBG("mp_kdp_exit() waiting for processors to resume\n"); | |
0c530ab8 | 1869 | while (mp_kdp_ncpus > 0) { |
0a7de745 | 1870 | /* |
0c530ab8 A |
1871 | * a TLB shootdown request may be pending... this would result in the requesting |
1872 | * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it. | |
1873 | * Process it, so it can now enter mp_kdp_wait() | |
1874 | */ | |
0a7de745 | 1875 | handle_pending_TLB_flushes(); |
0c530ab8 | 1876 | |
55e303ae A |
1877 | cpu_pause(); |
1878 | } | |
2d21ac55 | 1879 | |
0a7de745 A |
1880 | if (pmsafe_debug && !kdp_snapshot) { |
1881 | pmSafeMode(¤t_cpu_datap()->lcpu, PM_SAFE_FL_NORMAL); | |
1882 | } | |
2d21ac55 | 1883 | |
6d2010ae A |
1884 | debugger_exit_time = mach_absolute_time(); |
1885 | ||
55e303ae | 1886 | DBG("mp_kdp_exit() done\n"); |
91447636 | 1887 | (void) ml_set_interrupts_enabled(mp_kdp_state); |
5ba3f43e | 1888 | postcode(MP_KDP_EXIT); |
39037602 A |
1889 | } |
1890 | ||
0a7de745 | 1891 | #endif /* MACH_KDP */ |
55e303ae | 1892 | |
b0d623f7 | 1893 | boolean_t |
0a7de745 A |
1894 | mp_recent_debugger_activity(void) |
1895 | { | |
060df5ea | 1896 | uint64_t abstime = mach_absolute_time(); |
0a7de745 A |
1897 | return ((abstime - debugger_entry_time) < LastDebuggerEntryAllowance) || |
1898 | ((abstime - debugger_exit_time) < LastDebuggerEntryAllowance); | |
b0d623f7 A |
1899 | } |
1900 | ||
55e303ae A |
1901 | /*ARGSUSED*/ |
1902 | void | |
1903 | init_ast_check( | |
0a7de745 | 1904 | __unused processor_t processor) |
55e303ae A |
1905 | { |
1906 | } | |
1907 | ||
1908 | void | |
1909 | cause_ast_check( | |
0a7de745 | 1910 | processor_t processor) |
55e303ae | 1911 | { |
0a7de745 | 1912 | int cpu = processor->cpu_id; |
55e303ae A |
1913 | |
1914 | if (cpu != cpu_number()) { | |
1915 | i386_signal_cpu(cpu, MP_AST, ASYNC); | |
6d2010ae | 1916 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_REMOTE_AST), cpu, 1, 0, 0, 0); |
55e303ae A |
1917 | } |
1918 | } | |
1919 | ||
593a1d5f A |
1920 | void |
1921 | slave_machine_init(void *param) | |
91447636 A |
1922 | { |
1923 | /* | |
0a7de745 | 1924 | * Here in process context, but with interrupts disabled. |
91447636 A |
1925 | */ |
1926 | DBG("slave_machine_init() CPU%d\n", get_cpu_number()); | |
1927 | ||
593a1d5f A |
1928 | if (param == FULL_SLAVE_INIT) { |
1929 | /* | |
1930 | * Cold start | |
1931 | */ | |
1932 | clock_init(); | |
593a1d5f | 1933 | } |
0a7de745 | 1934 | cpu_machine_init(); /* Interrupts enabled hereafter */ |
55e303ae A |
1935 | } |
1936 | ||
b0d623f7 | 1937 | #undef cpu_number |
0a7de745 A |
1938 | int |
1939 | cpu_number(void) | |
55e303ae A |
1940 | { |
1941 | return get_cpu_number(); | |
1942 | } | |
1943 | ||
f427ee49 A |
1944 | vm_offset_t |
1945 | current_percpu_base(void) | |
1946 | { | |
1947 | return get_current_percpu_base(); | |
1948 | } | |
1949 | ||
6d2010ae A |
1950 | static void |
1951 | cpu_prewarm_init() | |
1952 | { | |
1953 | int i; | |
1954 | ||
1955 | simple_lock_init(&cpu_warm_lock, 0); | |
1956 | queue_init(&cpu_warm_call_list); | |
1957 | for (i = 0; i < NUM_CPU_WARM_CALLS; i++) { | |
1958 | enqueue_head(&cpu_warm_call_list, (queue_entry_t)&cpu_warm_call_arr[i]); | |
1959 | } | |
1960 | } | |
1961 | ||
1962 | static timer_call_t | |
1963 | grab_warm_timer_call() | |
1964 | { | |
1965 | spl_t x; | |
1966 | timer_call_t call = NULL; | |
1967 | ||
1968 | x = splsched(); | |
0a7de745 | 1969 | simple_lock(&cpu_warm_lock, LCK_GRP_NULL); |
6d2010ae A |
1970 | if (!queue_empty(&cpu_warm_call_list)) { |
1971 | call = (timer_call_t) dequeue_head(&cpu_warm_call_list); | |
1972 | } | |
1973 | simple_unlock(&cpu_warm_lock); | |
1974 | splx(x); | |
1975 | ||
1976 | return call; | |
1977 | } | |
1978 | ||
1979 | static void | |
1980 | free_warm_timer_call(timer_call_t call) | |
1981 | { | |
1982 | spl_t x; | |
1983 | ||
1984 | x = splsched(); | |
0a7de745 | 1985 | simple_lock(&cpu_warm_lock, LCK_GRP_NULL); |
6d2010ae A |
1986 | enqueue_head(&cpu_warm_call_list, (queue_entry_t)call); |
1987 | simple_unlock(&cpu_warm_lock); | |
1988 | splx(x); | |
1989 | } | |
1990 | ||
1991 | /* | |
1992 | * Runs in timer call context (interrupts disabled). | |
1993 | */ | |
1994 | static void | |
1995 | cpu_warm_timer_call_func( | |
f427ee49 A |
1996 | timer_call_param_t p0, |
1997 | __unused timer_call_param_t p1) | |
6d2010ae A |
1998 | { |
1999 | free_warm_timer_call((timer_call_t)p0); | |
2000 | return; | |
2001 | } | |
2002 | ||
2003 | /* | |
2004 | * Runs with interrupts disabled on the CPU we wish to warm (i.e. CPU 0). | |
2005 | */ | |
2006 | static void | |
2007 | _cpu_warm_setup( | |
0a7de745 | 2008 | void *arg) |
6d2010ae A |
2009 | { |
2010 | cpu_warm_data_t cwdp = (cpu_warm_data_t)arg; | |
2011 | ||
39236c6e | 2012 | timer_call_enter(cwdp->cwd_call, cwdp->cwd_deadline, TIMER_CALL_SYS_CRITICAL | TIMER_CALL_LOCAL); |
6d2010ae A |
2013 | cwdp->cwd_result = 0; |
2014 | ||
2015 | return; | |
2016 | } | |
2017 | ||
2018 | /* | |
2019 | * Not safe to call with interrupts disabled. | |
2020 | */ | |
2021 | kern_return_t | |
2022 | ml_interrupt_prewarm( | |
0a7de745 | 2023 | uint64_t deadline) |
6d2010ae A |
2024 | { |
2025 | struct cpu_warm_data cwd; | |
2026 | timer_call_t call; | |
2027 | cpu_t ct; | |
2028 | ||
2029 | if (ml_get_interrupts_enabled() == FALSE) { | |
2030 | panic("%s: Interrupts disabled?\n", __FUNCTION__); | |
2031 | } | |
2032 | ||
0a7de745 A |
2033 | /* |
2034 | * If the platform doesn't need our help, say that we succeeded. | |
6d2010ae A |
2035 | */ |
2036 | if (!ml_get_interrupt_prewake_applicable()) { | |
2037 | return KERN_SUCCESS; | |
2038 | } | |
2039 | ||
2040 | /* | |
2041 | * Grab a timer call to use. | |
2042 | */ | |
2043 | call = grab_warm_timer_call(); | |
2044 | if (call == NULL) { | |
2045 | return KERN_RESOURCE_SHORTAGE; | |
2046 | } | |
2047 | ||
2048 | timer_call_setup(call, cpu_warm_timer_call_func, call); | |
2049 | cwd.cwd_call = call; | |
2050 | cwd.cwd_deadline = deadline; | |
2051 | cwd.cwd_result = 0; | |
2052 | ||
2053 | /* | |
2054 | * For now, non-local interrupts happen on the master processor. | |
2055 | */ | |
2056 | ct = mp_cpus_call(cpu_to_cpumask(master_cpu), SYNC, _cpu_warm_setup, &cwd); | |
2057 | if (ct == 0) { | |
2058 | free_warm_timer_call(call); | |
2059 | return KERN_FAILURE; | |
2060 | } else { | |
2061 | return cwd.cwd_result; | |
2062 | } | |
2063 | } | |
39037602 A |
2064 | |
2065 | #if DEBUG || DEVELOPMENT | |
2066 | void | |
2067 | kernel_spin(uint64_t spin_ns) | |
2068 | { | |
0a7de745 A |
2069 | boolean_t istate; |
2070 | uint64_t spin_abs; | |
2071 | uint64_t deadline; | |
2072 | cpu_data_t *cdp; | |
39037602 A |
2073 | |
2074 | kprintf("kernel_spin(%llu) spinning uninterruptibly\n", spin_ns); | |
2075 | istate = ml_set_interrupts_enabled(FALSE); | |
5ba3f43e | 2076 | cdp = current_cpu_datap(); |
39037602 | 2077 | nanoseconds_to_absolutetime(spin_ns, &spin_abs); |
5ba3f43e A |
2078 | |
2079 | /* Fake interrupt handler entry for testing mp_interrupt_watchdog() */ | |
2080 | cdp->cpu_int_event_time = mach_absolute_time(); | |
2081 | cdp->cpu_int_state = (void *) USER_STATE(current_thread()); | |
2082 | ||
39037602 | 2083 | deadline = mach_absolute_time() + spin_ns; |
0a7de745 | 2084 | while (mach_absolute_time() < deadline) { |
39037602 | 2085 | cpu_pause(); |
0a7de745 | 2086 | } |
5ba3f43e A |
2087 | |
2088 | cdp->cpu_int_event_time = 0; | |
2089 | cdp->cpu_int_state = NULL; | |
2090 | ||
39037602 A |
2091 | ml_set_interrupts_enabled(istate); |
2092 | kprintf("kernel_spin() continuing\n"); | |
2093 | } | |
5ba3f43e A |
2094 | |
2095 | /* | |
2096 | * Called from the scheduler's maintenance thread, | |
2097 | * scan running processors for long-running ISRs and: | |
2098 | * - panic if longer than LockTimeOut, or | |
2099 | * - log if more than a quantum. | |
2100 | */ | |
2101 | void | |
2102 | mp_interrupt_watchdog(void) | |
2103 | { | |
0a7de745 A |
2104 | cpu_t cpu; |
2105 | boolean_t intrs_enabled = FALSE; | |
2106 | uint16_t cpu_int_num; | |
2107 | uint64_t cpu_int_event_time; | |
2108 | uint64_t cpu_rip; | |
2109 | uint64_t cpu_int_duration; | |
2110 | uint64_t now; | |
2111 | x86_saved_state_t *cpu_int_state; | |
2112 | ||
2113 | if (__improbable(!mp_interrupt_watchdog_enabled)) { | |
5ba3f43e | 2114 | return; |
0a7de745 | 2115 | } |
5ba3f43e A |
2116 | |
2117 | intrs_enabled = ml_set_interrupts_enabled(FALSE); | |
2118 | now = mach_absolute_time(); | |
2119 | /* | |
2120 | * While timeouts are not suspended, | |
2121 | * check all other processors for long outstanding interrupt handling. | |
2122 | */ | |
2123 | for (cpu = 0; | |
0a7de745 A |
2124 | cpu < (cpu_t) real_ncpus && !machine_timeout_suspended(); |
2125 | cpu++) { | |
5ba3f43e | 2126 | if ((cpu == (cpu_t) cpu_number()) || |
0a7de745 | 2127 | (!cpu_is_running(cpu))) { |
5ba3f43e | 2128 | continue; |
0a7de745 | 2129 | } |
5ba3f43e | 2130 | cpu_int_event_time = cpu_datap(cpu)->cpu_int_event_time; |
0a7de745 | 2131 | if (cpu_int_event_time == 0) { |
5ba3f43e | 2132 | continue; |
0a7de745 A |
2133 | } |
2134 | if (__improbable(now < cpu_int_event_time)) { | |
2135 | continue; /* skip due to inter-processor skew */ | |
2136 | } | |
5ba3f43e | 2137 | cpu_int_state = cpu_datap(cpu)->cpu_int_state; |
0a7de745 | 2138 | if (__improbable(cpu_int_state == NULL)) { |
5ba3f43e A |
2139 | /* The interrupt may have been dismissed */ |
2140 | continue; | |
0a7de745 | 2141 | } |
5ba3f43e A |
2142 | |
2143 | /* Here with a cpu handling an interrupt */ | |
2144 | ||
2145 | cpu_int_duration = now - cpu_int_event_time; | |
2146 | if (__improbable(cpu_int_duration > LockTimeOut)) { | |
2147 | cpu_int_num = saved_state64(cpu_int_state)->isf.trapno; | |
2148 | cpu_rip = saved_state64(cpu_int_state)->isf.rip; | |
2149 | vector_timed_out = cpu_int_num; | |
2150 | NMIPI_panic(cpu_to_cpumask(cpu), INTERRUPT_WATCHDOG); | |
2151 | panic("Interrupt watchdog, " | |
0a7de745 A |
2152 | "cpu: %d interrupt: 0x%x time: %llu..%llu state: %p RIP: 0x%llx", |
2153 | cpu, cpu_int_num, cpu_int_event_time, now, cpu_int_state, cpu_rip); | |
5ba3f43e A |
2154 | /* NOT REACHED */ |
2155 | } else if (__improbable(cpu_int_duration > (uint64_t) std_quantum)) { | |
2156 | mp_interrupt_watchdog_events++; | |
2157 | cpu_int_num = saved_state64(cpu_int_state)->isf.trapno; | |
2158 | cpu_rip = saved_state64(cpu_int_state)->isf.rip; | |
2159 | ml_set_interrupts_enabled(intrs_enabled); | |
2160 | printf("Interrupt watchdog, " | |
0a7de745 A |
2161 | "cpu: %d interrupt: 0x%x time: %llu..%llu RIP: 0x%llx\n", |
2162 | cpu, cpu_int_num, cpu_int_event_time, now, cpu_rip); | |
5ba3f43e A |
2163 | return; |
2164 | } | |
2165 | } | |
2166 | ||
2167 | ml_set_interrupts_enabled(intrs_enabled); | |
2168 | } | |
39037602 | 2169 | #endif |