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