]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/mp.c
4dd1e625dcc199bd9d0a90dac7d139592903ea49
[apple/xnu.git] / osfmk / i386 / mp.c
1 /*
2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31
32 #include <mach_rt.h>
33 #include <mach_kdb.h>
34 #include <mach_kdp.h>
35 #include <mach_ldebug.h>
36 #include <gprof.h>
37
38 #include <mach/mach_types.h>
39 #include <mach/kern_return.h>
40
41 #include <kern/kern_types.h>
42 #include <kern/startup.h>
43 #include <kern/timer_queue.h>
44 #include <kern/processor.h>
45 #include <kern/cpu_number.h>
46 #include <kern/cpu_data.h>
47 #include <kern/assert.h>
48 #include <kern/machine.h>
49 #include <kern/pms.h>
50 #include <kern/misc_protos.h>
51 #include <kern/etimer.h>
52
53 #include <vm/vm_map.h>
54 #include <vm/vm_kern.h>
55
56 #include <profiling/profile-mk.h>
57
58 #include <i386/proc_reg.h>
59 #include <i386/cpu_threads.h>
60 #include <i386/mp_desc.h>
61 #include <i386/misc_protos.h>
62 #include <i386/trap.h>
63 #include <i386/postcode.h>
64 #include <i386/machine_routines.h>
65 #include <i386/mp.h>
66 #include <i386/mp_events.h>
67 #include <i386/lapic.h>
68 #include <i386/ipl.h>
69 #include <i386/cpuid.h>
70 #include <i386/fpu.h>
71 #include <i386/machine_cpu.h>
72 #include <i386/mtrr.h>
73 #include <i386/pmCPU.h>
74 #if CONFIG_MCA
75 #include <i386/machine_check.h>
76 #endif
77 #include <i386/acpi.h>
78
79 #include <chud/chud_xnu.h>
80 #include <chud/chud_xnu_private.h>
81
82 #include <sys/kdebug.h>
83 #if MACH_KDB
84 #include <machine/db_machdep.h>
85 #include <ddb/db_aout.h>
86 #include <ddb/db_access.h>
87 #include <ddb/db_sym.h>
88 #include <ddb/db_variables.h>
89 #include <ddb/db_command.h>
90 #include <ddb/db_output.h>
91 #include <ddb/db_expr.h>
92 #endif
93
94 #if MP_DEBUG
95 #define PAUSE delay(1000000)
96 #define DBG(x...) kprintf(x)
97 #else
98 #define DBG(x...)
99 #define PAUSE
100 #endif /* MP_DEBUG */
101
102
103 #define ABS(v) (((v) > 0)?(v):-(v))
104
105 void slave_boot_init(void);
106
107 #if MACH_KDB
108 static void mp_kdb_wait(void);
109 volatile boolean_t mp_kdb_trap = FALSE;
110 volatile long mp_kdb_ncpus = 0;
111 #endif
112
113 static void mp_kdp_wait(boolean_t flush, boolean_t isNMI);
114 static void mp_rendezvous_action(void);
115 static void mp_broadcast_action(void);
116
117 static boolean_t cpu_signal_pending(int cpu, mp_event_t event);
118 static int cpu_signal_handler(x86_saved_state_t *regs);
119 static int NMIInterruptHandler(x86_saved_state_t *regs);
120
121 boolean_t smp_initialized = FALSE;
122 uint32_t TSC_sync_margin = 0xFFF;
123 volatile boolean_t force_immediate_debugger_NMI = FALSE;
124 volatile boolean_t pmap_tlb_flush_timeout = FALSE;
125 decl_simple_lock_data(,mp_kdp_lock);
126
127 decl_lck_mtx_data(static, mp_cpu_boot_lock);
128 lck_mtx_ext_t mp_cpu_boot_lock_ext;
129
130 /* Variables needed for MP rendezvous. */
131 decl_simple_lock_data(,mp_rv_lock);
132 static void (*mp_rv_setup_func)(void *arg);
133 static void (*mp_rv_action_func)(void *arg);
134 static void (*mp_rv_teardown_func)(void *arg);
135 static void *mp_rv_func_arg;
136 static volatile int mp_rv_ncpus;
137 /* Cache-aligned barriers: */
138 static volatile long mp_rv_entry __attribute__((aligned(64)));
139 static volatile long mp_rv_exit __attribute__((aligned(64)));
140 static volatile long mp_rv_complete __attribute__((aligned(64)));
141
142 volatile uint64_t debugger_entry_time;
143 volatile uint64_t debugger_exit_time;
144 #if MACH_KDP
145
146 extern int kdp_snapshot;
147 static struct _kdp_xcpu_call_func {
148 kdp_x86_xcpu_func_t func;
149 void *arg0, *arg1;
150 volatile long ret;
151 volatile uint16_t cpu;
152 } kdp_xcpu_call_func = {
153 .cpu = KDP_XCPU_NONE
154 };
155
156 #endif
157
158 /* Variables needed for MP broadcast. */
159 static void (*mp_bc_action_func)(void *arg);
160 static void *mp_bc_func_arg;
161 static int mp_bc_ncpus;
162 static volatile long mp_bc_count;
163 decl_lck_mtx_data(static, mp_bc_lock);
164 lck_mtx_ext_t mp_bc_lock_ext;
165 static volatile int debugger_cpu = -1;
166
167 static void mp_cpus_call_action(void);
168 static void mp_call_PM(void);
169
170 char mp_slave_stack[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE))); // Temp stack for slave init
171
172
173 #if GPROF
174 /*
175 * Initialize dummy structs for profiling. These aren't used but
176 * allows hertz_tick() to be built with GPROF defined.
177 */
178 struct profile_vars _profile_vars;
179 struct profile_vars *_profile_vars_cpus[MAX_CPUS] = { &_profile_vars };
180 #define GPROF_INIT() \
181 { \
182 int i; \
183 \
184 /* Hack to initialize pointers to unused profiling structs */ \
185 for (i = 1; i < MAX_CPUS; i++) \
186 _profile_vars_cpus[i] = &_profile_vars; \
187 }
188 #else
189 #define GPROF_INIT()
190 #endif /* GPROF */
191
192 static lck_grp_t smp_lck_grp;
193 static lck_grp_attr_t smp_lck_grp_attr;
194
195 extern void slave_pstart(void);
196
197 void
198 smp_init(void)
199 {
200 simple_lock_init(&mp_kdp_lock, 0);
201 simple_lock_init(&mp_rv_lock, 0);
202 lck_grp_attr_setdefault(&smp_lck_grp_attr);
203 lck_grp_init(&smp_lck_grp, "i386_smp", &smp_lck_grp_attr);
204 lck_mtx_init_ext(&mp_cpu_boot_lock, &mp_cpu_boot_lock_ext, &smp_lck_grp, LCK_ATTR_NULL);
205 lck_mtx_init_ext(&mp_bc_lock, &mp_bc_lock_ext, &smp_lck_grp, LCK_ATTR_NULL);
206 console_init();
207
208 /* Local APIC? */
209 if (!lapic_probe())
210 return;
211
212 lapic_init();
213 lapic_configure();
214 lapic_set_intr_func(LAPIC_NMI_INTERRUPT, NMIInterruptHandler);
215 lapic_set_intr_func(LAPIC_VECTOR(INTERPROCESSOR), cpu_signal_handler);
216
217 cpu_thread_init();
218
219 GPROF_INIT();
220 DBGLOG_CPU_INIT(master_cpu);
221
222 install_real_mode_bootstrap(slave_pstart);
223
224 if (PE_parse_boot_argn("TSC_sync_margin",
225 &TSC_sync_margin, sizeof(TSC_sync_margin)))
226 kprintf("TSC sync Margin 0x%x\n", TSC_sync_margin);
227 smp_initialized = TRUE;
228
229 return;
230 }
231
232 typedef struct {
233 int target_cpu;
234 int target_lapic;
235 int starter_cpu;
236 } processor_start_info_t;
237 static processor_start_info_t start_info __attribute__((aligned(64)));
238
239 /*
240 * Cache-alignment is to avoid cross-cpu false-sharing interference.
241 */
242 static volatile long tsc_entry_barrier __attribute__((aligned(64)));
243 static volatile long tsc_exit_barrier __attribute__((aligned(64)));
244 static volatile uint64_t tsc_target __attribute__((aligned(64)));
245
246 /*
247 * Poll a CPU to see when it has marked itself as running.
248 */
249 static void
250 mp_wait_for_cpu_up(int slot_num, unsigned int iters, unsigned int usecdelay)
251 {
252 while (iters-- > 0) {
253 if (cpu_datap(slot_num)->cpu_running)
254 break;
255 delay(usecdelay);
256 }
257 }
258
259 /*
260 * Quickly bring a CPU back online which has been halted.
261 */
262 kern_return_t
263 intel_startCPU_fast(int slot_num)
264 {
265 kern_return_t rc;
266
267 /*
268 * Try to perform a fast restart
269 */
270 rc = pmCPUExitHalt(slot_num);
271 if (rc != KERN_SUCCESS)
272 /*
273 * The CPU was not eligible for a fast restart.
274 */
275 return(rc);
276
277 /*
278 * Wait until the CPU is back online.
279 */
280 mp_disable_preemption();
281
282 /*
283 * We use short pauses (1us) for low latency. 30,000 iterations is
284 * longer than a full restart would require so it should be more
285 * than long enough.
286 */
287 mp_wait_for_cpu_up(slot_num, 30000, 1);
288 mp_enable_preemption();
289
290 /*
291 * Check to make sure that the CPU is really running. If not,
292 * go through the slow path.
293 */
294 if (cpu_datap(slot_num)->cpu_running)
295 return(KERN_SUCCESS);
296 else
297 return(KERN_FAILURE);
298 }
299
300 static void
301 started_cpu(void)
302 {
303 /* Here on the started cpu with cpu_running set TRUE */
304
305 if (TSC_sync_margin &&
306 start_info.target_cpu == cpu_number()) {
307 /*
308 * I've just started-up, synchronize again with the starter cpu
309 * and then snap my TSC.
310 */
311 tsc_target = 0;
312 atomic_decl(&tsc_entry_barrier, 1);
313 while (tsc_entry_barrier != 0)
314 ; /* spin for starter and target at barrier */
315 tsc_target = rdtsc64();
316 atomic_decl(&tsc_exit_barrier, 1);
317 }
318 }
319
320 static void
321 start_cpu(void *arg)
322 {
323 int i = 1000;
324 processor_start_info_t *psip = (processor_start_info_t *) arg;
325
326 /* Ignore this if the current processor is not the starter */
327 if (cpu_number() != psip->starter_cpu)
328 return;
329
330 LAPIC_WRITE(ICRD, psip->target_lapic << LAPIC_ICRD_DEST_SHIFT);
331 LAPIC_WRITE(ICR, LAPIC_ICR_DM_INIT);
332 delay(100);
333
334 LAPIC_WRITE(ICRD, psip->target_lapic << LAPIC_ICRD_DEST_SHIFT);
335 LAPIC_WRITE(ICR, LAPIC_ICR_DM_STARTUP|(REAL_MODE_BOOTSTRAP_OFFSET>>12));
336
337 #ifdef POSTCODE_DELAY
338 /* Wait much longer if postcodes are displayed for a delay period. */
339 i *= 10000;
340 #endif
341 mp_wait_for_cpu_up(psip->target_cpu, i*100, 100);
342 if (TSC_sync_margin &&
343 cpu_datap(psip->target_cpu)->cpu_running) {
344 /*
345 * Compare the TSC from the started processor with ours.
346 * Report and log/panic if it diverges by more than
347 * TSC_sync_margin (TSC_SYNC_MARGIN) ticks. This margin
348 * can be overriden by boot-arg (with 0 meaning no checking).
349 */
350 uint64_t tsc_starter;
351 int64_t tsc_delta;
352 atomic_decl(&tsc_entry_barrier, 1);
353 while (tsc_entry_barrier != 0)
354 ; /* spin for both processors at barrier */
355 tsc_starter = rdtsc64();
356 atomic_decl(&tsc_exit_barrier, 1);
357 while (tsc_exit_barrier != 0)
358 ; /* spin for target to store its TSC */
359 tsc_delta = tsc_target - tsc_starter;
360 kprintf("TSC sync for cpu %d: 0x%016llx delta 0x%llx (%lld)\n",
361 psip->target_cpu, tsc_target, tsc_delta, tsc_delta);
362 if (ABS(tsc_delta) > (int64_t) TSC_sync_margin) {
363 #if DEBUG
364 panic(
365 #else
366 printf(
367 #endif
368 "Unsynchronized TSC for cpu %d: "
369 "0x%016llx, delta 0x%llx\n",
370 psip->target_cpu, tsc_target, tsc_delta);
371 }
372 }
373 }
374
375 extern char prot_mode_gdt[];
376 extern char slave_boot_base[];
377 extern char real_mode_bootstrap_base[];
378 extern char real_mode_bootstrap_end[];
379 extern char slave_boot_end[];
380
381 kern_return_t
382 intel_startCPU(
383 int slot_num)
384 {
385 int lapic = cpu_to_lapic[slot_num];
386 boolean_t istate;
387
388 assert(lapic != -1);
389
390 DBGLOG_CPU_INIT(slot_num);
391
392 DBG("intel_startCPU(%d) lapic_id=%d\n", slot_num, lapic);
393 DBG("IdlePTD(%p): 0x%x\n", &IdlePTD, (int) IdlePTD);
394
395 /*
396 * Initialize (or re-initialize) the descriptor tables for this cpu.
397 * Propagate processor mode to slave.
398 */
399 if (cpu_mode_is64bit())
400 cpu_desc_init64(cpu_datap(slot_num));
401 else
402 cpu_desc_init(cpu_datap(slot_num));
403
404 /* Serialize use of the slave boot stack, etc. */
405 lck_mtx_lock(&mp_cpu_boot_lock);
406
407 istate = ml_set_interrupts_enabled(FALSE);
408 if (slot_num == get_cpu_number()) {
409 ml_set_interrupts_enabled(istate);
410 lck_mtx_unlock(&mp_cpu_boot_lock);
411 return KERN_SUCCESS;
412 }
413
414 start_info.starter_cpu = cpu_number();
415 start_info.target_cpu = slot_num;
416 start_info.target_lapic = lapic;
417 tsc_entry_barrier = 2;
418 tsc_exit_barrier = 2;
419
420 /*
421 * Perform the processor startup sequence with all running
422 * processors rendezvous'ed. This is required during periods when
423 * the cache-disable bit is set for MTRR/PAT initialization.
424 */
425 mp_rendezvous_no_intrs(start_cpu, (void *) &start_info);
426
427 start_info.target_cpu = 0;
428
429 ml_set_interrupts_enabled(istate);
430 lck_mtx_unlock(&mp_cpu_boot_lock);
431
432 if (!cpu_datap(slot_num)->cpu_running) {
433 kprintf("Failed to start CPU %02d\n", slot_num);
434 printf("Failed to start CPU %02d, rebooting...\n", slot_num);
435 delay(1000000);
436 halt_cpu();
437 return KERN_SUCCESS;
438 } else {
439 kprintf("Started cpu %d (lapic id %08x)\n", slot_num, lapic);
440 return KERN_SUCCESS;
441 }
442 }
443
444 #if MP_DEBUG
445 cpu_signal_event_log_t *cpu_signal[MAX_CPUS];
446 cpu_signal_event_log_t *cpu_handle[MAX_CPUS];
447
448 MP_EVENT_NAME_DECL();
449
450 #endif /* MP_DEBUG */
451
452 int
453 cpu_signal_handler(x86_saved_state_t *regs)
454 {
455 int my_cpu;
456 volatile int *my_word;
457 #if MACH_KDB && MACH_ASSERT
458 int i=100;
459 #endif /* MACH_KDB && MACH_ASSERT */
460
461 mp_disable_preemption();
462
463 my_cpu = cpu_number();
464 my_word = &current_cpu_datap()->cpu_signals;
465
466 do {
467 #if MACH_KDB && MACH_ASSERT
468 if (i-- <= 0)
469 Debugger("cpu_signal_handler: signals did not clear");
470 #endif /* MACH_KDB && MACH_ASSERT */
471 #if MACH_KDP
472 if (i_bit(MP_KDP, my_word)) {
473 DBGLOG(cpu_handle,my_cpu,MP_KDP);
474 i_bit_clear(MP_KDP, my_word);
475 /* Ensure that the i386_kernel_state at the base of the
476 * current thread's stack (if any) is synchronized with the
477 * context at the moment of the interrupt, to facilitate
478 * access through the debugger.
479 */
480 sync_iss_to_iks(regs);
481 if (pmsafe_debug && !kdp_snapshot)
482 pmSafeMode(&current_cpu_datap()->lcpu, PM_SAFE_FL_SAFE);
483 mp_kdp_wait(TRUE, FALSE);
484 if (pmsafe_debug && !kdp_snapshot)
485 pmSafeMode(&current_cpu_datap()->lcpu, PM_SAFE_FL_NORMAL);
486 } else
487 #endif /* MACH_KDP */
488 if (i_bit(MP_TLB_FLUSH, my_word)) {
489 DBGLOG(cpu_handle,my_cpu,MP_TLB_FLUSH);
490 i_bit_clear(MP_TLB_FLUSH, my_word);
491 pmap_update_interrupt();
492 } else if (i_bit(MP_AST, my_word)) {
493 DBGLOG(cpu_handle,my_cpu,MP_AST);
494 i_bit_clear(MP_AST, my_word);
495 ast_check(cpu_to_processor(my_cpu));
496 #if MACH_KDB
497 } else if (i_bit(MP_KDB, my_word)) {
498
499 i_bit_clear(MP_KDB, my_word);
500 current_cpu_datap()->cpu_kdb_is_slave++;
501 mp_kdb_wait();
502 current_cpu_datap()->cpu_kdb_is_slave--;
503 #endif /* MACH_KDB */
504 } else if (i_bit(MP_RENDEZVOUS, my_word)) {
505 DBGLOG(cpu_handle,my_cpu,MP_RENDEZVOUS);
506 i_bit_clear(MP_RENDEZVOUS, my_word);
507 mp_rendezvous_action();
508 } else if (i_bit(MP_BROADCAST, my_word)) {
509 DBGLOG(cpu_handle,my_cpu,MP_BROADCAST);
510 i_bit_clear(MP_BROADCAST, my_word);
511 mp_broadcast_action();
512 } else if (i_bit(MP_CHUD, my_word)) {
513 DBGLOG(cpu_handle,my_cpu,MP_CHUD);
514 i_bit_clear(MP_CHUD, my_word);
515 chudxnu_cpu_signal_handler();
516 } else if (i_bit(MP_CALL, my_word)) {
517 DBGLOG(cpu_handle,my_cpu,MP_CALL);
518 i_bit_clear(MP_CALL, my_word);
519 mp_cpus_call_action();
520 } else if (i_bit(MP_CALL_PM, my_word)) {
521 DBGLOG(cpu_handle,my_cpu,MP_CALL_PM);
522 i_bit_clear(MP_CALL_PM, my_word);
523 mp_call_PM();
524 }
525 } while (*my_word);
526
527 mp_enable_preemption();
528
529 return 0;
530 }
531
532 static int
533 NMIInterruptHandler(x86_saved_state_t *regs)
534 {
535 void *stackptr;
536
537 sync_iss_to_iks_unconditionally(regs);
538 #if defined (__i386__)
539 __asm__ volatile("movl %%ebp, %0" : "=m" (stackptr));
540 #elif defined (__x86_64__)
541 __asm__ volatile("movq %%rbp, %0" : "=m" (stackptr));
542 #endif
543
544 if (cpu_number() == debugger_cpu)
545 goto NMExit;
546
547 if (pmap_tlb_flush_timeout == TRUE && current_cpu_datap()->cpu_tlb_invalid) {
548 char pstr[128];
549 snprintf(&pstr[0], sizeof(pstr), "Panic(CPU %d): Unresponsive processor\n", cpu_number());
550 panic_i386_backtrace(stackptr, 16, &pstr[0], TRUE, regs);
551 }
552
553 #if MACH_KDP
554 if (pmsafe_debug && !kdp_snapshot)
555 pmSafeMode(&current_cpu_datap()->lcpu, PM_SAFE_FL_SAFE);
556 mp_kdp_wait(FALSE, pmap_tlb_flush_timeout);
557 if (pmsafe_debug && !kdp_snapshot)
558 pmSafeMode(&current_cpu_datap()->lcpu, PM_SAFE_FL_NORMAL);
559 #endif
560 NMExit:
561 return 1;
562 }
563
564 #ifdef MP_DEBUG
565 int max_lock_loops = 100000000;
566 int trappedalready = 0; /* (BRINGUP) */
567 #endif /* MP_DEBUG */
568
569 static void
570 i386_cpu_IPI(int cpu)
571 {
572 boolean_t state;
573
574 #ifdef MP_DEBUG
575 if(cpu_datap(cpu)->cpu_signals & 6) { /* (BRINGUP) */
576 kprintf("i386_cpu_IPI: sending enter debugger signal (%08X) to cpu %d\n", cpu_datap(cpu)->cpu_signals, cpu);
577 }
578 #endif /* MP_DEBUG */
579
580 #if MACH_KDB
581 #ifdef MP_DEBUG
582 if(!trappedalready && (cpu_datap(cpu)->cpu_signals & 6)) { /* (BRINGUP) */
583 if(kdb_cpu != cpu_number()) {
584 trappedalready = 1;
585 panic("i386_cpu_IPI: sending enter debugger signal (%08X) to cpu %d and I do not own debugger, owner = %08X\n",
586 cpu_datap(cpu)->cpu_signals, cpu, kdb_cpu);
587 }
588 }
589 #endif /* MP_DEBUG */
590 #endif
591
592 /* Wait for previous interrupt to be delivered... */
593 #ifdef MP_DEBUG
594 int pending_busy_count = 0;
595 while (LAPIC_READ(ICR) & LAPIC_ICR_DS_PENDING) {
596 if (++pending_busy_count > max_lock_loops)
597 panic("i386_cpu_IPI() deadlock\n");
598 #else
599 while (LAPIC_READ(ICR) & LAPIC_ICR_DS_PENDING) {
600 #endif /* MP_DEBUG */
601 cpu_pause();
602 }
603
604 state = ml_set_interrupts_enabled(FALSE);
605 LAPIC_WRITE(ICRD, cpu_to_lapic[cpu] << LAPIC_ICRD_DEST_SHIFT);
606 LAPIC_WRITE(ICR, LAPIC_VECTOR(INTERPROCESSOR) | LAPIC_ICR_DM_FIXED);
607 (void) ml_set_interrupts_enabled(state);
608 }
609
610 /*
611 * cpu_interrupt is really just to be used by the scheduler to
612 * get a CPU's attention it may not always issue an IPI. If an
613 * IPI is always needed then use i386_cpu_IPI.
614 */
615 void
616 cpu_interrupt(int cpu)
617 {
618 if (smp_initialized
619 && pmCPUExitIdle(cpu_datap(cpu))) {
620 i386_cpu_IPI(cpu);
621 }
622 }
623
624 /*
625 * Send a true NMI via the local APIC to the specified CPU.
626 */
627 void
628 cpu_NMI_interrupt(int cpu)
629 {
630 boolean_t state;
631
632 if (smp_initialized) {
633 state = ml_set_interrupts_enabled(FALSE);
634 /* Program the interrupt command register */
635 LAPIC_WRITE(ICRD, cpu_to_lapic[cpu] << LAPIC_ICRD_DEST_SHIFT);
636 /* The vector is ignored in this case--the target CPU will enter on the
637 * NMI vector.
638 */
639 LAPIC_WRITE(ICR, LAPIC_VECTOR(INTERPROCESSOR)|LAPIC_ICR_DM_NMI);
640 (void) ml_set_interrupts_enabled(state);
641 }
642 }
643
644 static void (* volatile mp_PM_func)(void) = NULL;
645
646 static void
647 mp_call_PM(void)
648 {
649 assert(!ml_get_interrupts_enabled());
650
651 if (mp_PM_func != NULL)
652 mp_PM_func();
653 }
654
655 void
656 cpu_PM_interrupt(int cpu)
657 {
658 assert(!ml_get_interrupts_enabled());
659
660 if (mp_PM_func != NULL) {
661 if (cpu == cpu_number())
662 mp_PM_func();
663 else
664 i386_signal_cpu(cpu, MP_CALL_PM, ASYNC);
665 }
666 }
667
668 void
669 PM_interrupt_register(void (*fn)(void))
670 {
671 mp_PM_func = fn;
672 }
673
674 void
675 i386_signal_cpu(int cpu, mp_event_t event, mp_sync_t mode)
676 {
677 volatile int *signals = &cpu_datap(cpu)->cpu_signals;
678 uint64_t tsc_timeout;
679
680
681 if (!cpu_datap(cpu)->cpu_running)
682 return;
683
684 if (event == MP_TLB_FLUSH)
685 KERNEL_DEBUG(0xef800020 | DBG_FUNC_START, cpu, 0, 0, 0, 0);
686
687 DBGLOG(cpu_signal, cpu, event);
688
689 i_bit_set(event, signals);
690 i386_cpu_IPI(cpu);
691 if (mode == SYNC) {
692 again:
693 tsc_timeout = rdtsc64() + (1000*1000*1000);
694 while (i_bit(event, signals) && rdtsc64() < tsc_timeout) {
695 cpu_pause();
696 }
697 if (i_bit(event, signals)) {
698 DBG("i386_signal_cpu(%d, 0x%x, SYNC) timed out\n",
699 cpu, event);
700 goto again;
701 }
702 }
703 if (event == MP_TLB_FLUSH)
704 KERNEL_DEBUG(0xef800020 | DBG_FUNC_END, cpu, 0, 0, 0, 0);
705 }
706
707 /*
708 * Send event to all running cpus.
709 * Called with the topology locked.
710 */
711 void
712 i386_signal_cpus(mp_event_t event, mp_sync_t mode)
713 {
714 unsigned int cpu;
715 unsigned int my_cpu = cpu_number();
716
717 assert(hw_lock_held((hw_lock_t)&x86_topo_lock));
718
719 for (cpu = 0; cpu < real_ncpus; cpu++) {
720 if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running)
721 continue;
722 i386_signal_cpu(cpu, event, mode);
723 }
724 }
725
726 /*
727 * Return the number of running cpus.
728 * Called with the topology locked.
729 */
730 int
731 i386_active_cpus(void)
732 {
733 unsigned int cpu;
734 unsigned int ncpus = 0;
735
736 assert(hw_lock_held((hw_lock_t)&x86_topo_lock));
737
738 for (cpu = 0; cpu < real_ncpus; cpu++) {
739 if (cpu_datap(cpu)->cpu_running)
740 ncpus++;
741 }
742 return(ncpus);
743 }
744
745 /*
746 * All-CPU rendezvous:
747 * - CPUs are signalled,
748 * - all execute the setup function (if specified),
749 * - rendezvous (i.e. all cpus reach a barrier),
750 * - all execute the action function (if specified),
751 * - rendezvous again,
752 * - execute the teardown function (if specified), and then
753 * - resume.
754 *
755 * Note that the supplied external functions _must_ be reentrant and aware
756 * that they are running in parallel and in an unknown lock context.
757 */
758
759 static void
760 mp_rendezvous_action(void)
761 {
762 boolean_t intrs_enabled;
763
764 /* setup function */
765 if (mp_rv_setup_func != NULL)
766 mp_rv_setup_func(mp_rv_func_arg);
767
768 intrs_enabled = ml_get_interrupts_enabled();
769
770
771 /* spin on entry rendezvous */
772 atomic_incl(&mp_rv_entry, 1);
773 while (mp_rv_entry < mp_rv_ncpus) {
774 /* poll for pesky tlb flushes if interrupts disabled */
775 if (!intrs_enabled)
776 handle_pending_TLB_flushes();
777 cpu_pause();
778 }
779 /* action function */
780 if (mp_rv_action_func != NULL)
781 mp_rv_action_func(mp_rv_func_arg);
782 /* spin on exit rendezvous */
783 atomic_incl(&mp_rv_exit, 1);
784 while (mp_rv_exit < mp_rv_ncpus) {
785 if (!intrs_enabled)
786 handle_pending_TLB_flushes();
787 cpu_pause();
788 }
789 /* teardown function */
790 if (mp_rv_teardown_func != NULL)
791 mp_rv_teardown_func(mp_rv_func_arg);
792
793 /* Bump completion count */
794 atomic_incl(&mp_rv_complete, 1);
795 }
796
797 void
798 mp_rendezvous(void (*setup_func)(void *),
799 void (*action_func)(void *),
800 void (*teardown_func)(void *),
801 void *arg)
802 {
803
804 if (!smp_initialized) {
805 if (setup_func != NULL)
806 setup_func(arg);
807 if (action_func != NULL)
808 action_func(arg);
809 if (teardown_func != NULL)
810 teardown_func(arg);
811 return;
812 }
813
814 /* obtain rendezvous lock */
815 simple_lock(&mp_rv_lock);
816
817 /* set static function pointers */
818 mp_rv_setup_func = setup_func;
819 mp_rv_action_func = action_func;
820 mp_rv_teardown_func = teardown_func;
821 mp_rv_func_arg = arg;
822
823 mp_rv_entry = 0;
824 mp_rv_exit = 0;
825 mp_rv_complete = 0;
826
827 /*
828 * signal other processors, which will call mp_rendezvous_action()
829 * with interrupts disabled
830 */
831 simple_lock(&x86_topo_lock);
832 mp_rv_ncpus = i386_active_cpus();
833 i386_signal_cpus(MP_RENDEZVOUS, ASYNC);
834 simple_unlock(&x86_topo_lock);
835
836 /* call executor function on this cpu */
837 mp_rendezvous_action();
838
839 /*
840 * Spin for everyone to complete.
841 * This is necessary to ensure that all processors have proceeded
842 * from the exit barrier before we release the rendezvous structure.
843 */
844 while (mp_rv_complete < mp_rv_ncpus) {
845 cpu_pause();
846 }
847
848 /* Tidy up */
849 mp_rv_setup_func = NULL;
850 mp_rv_action_func = NULL;
851 mp_rv_teardown_func = NULL;
852 mp_rv_func_arg = NULL;
853
854 /* release lock */
855 simple_unlock(&mp_rv_lock);
856 }
857
858 void
859 mp_rendezvous_break_lock(void)
860 {
861 simple_lock_init(&mp_rv_lock, 0);
862 }
863
864 static void
865 setup_disable_intrs(__unused void * param_not_used)
866 {
867 /* disable interrupts before the first barrier */
868 boolean_t intr = ml_set_interrupts_enabled(FALSE);
869
870 current_cpu_datap()->cpu_iflag = intr;
871 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__);
872 }
873
874 static void
875 teardown_restore_intrs(__unused void * param_not_used)
876 {
877 /* restore interrupt flag following MTRR changes */
878 ml_set_interrupts_enabled(current_cpu_datap()->cpu_iflag);
879 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__);
880 }
881
882 /*
883 * A wrapper to mp_rendezvous() to call action_func() with interrupts disabled.
884 * This is exported for use by kexts.
885 */
886 void
887 mp_rendezvous_no_intrs(
888 void (*action_func)(void *),
889 void *arg)
890 {
891 mp_rendezvous(setup_disable_intrs,
892 action_func,
893 teardown_restore_intrs,
894 arg);
895 }
896
897 void
898 handle_pending_TLB_flushes(void)
899 {
900 volatile int *my_word = &current_cpu_datap()->cpu_signals;
901
902 if (i_bit(MP_TLB_FLUSH, my_word)) {
903 DBGLOG(cpu_handle, cpu_number(), MP_TLB_FLUSH);
904 i_bit_clear(MP_TLB_FLUSH, my_word);
905 pmap_update_interrupt();
906 }
907 }
908
909 /*
910 * This is called from cpu_signal_handler() to process an MP_CALL signal.
911 */
912 static void
913 mp_cpus_call_action(void)
914 {
915 if (mp_rv_action_func != NULL)
916 mp_rv_action_func(mp_rv_func_arg);
917 atomic_incl(&mp_rv_complete, 1);
918 }
919
920 /*
921 * mp_cpus_call() runs a given function on cpus specified in a given cpu mask.
922 * If the mode is SYNC, the function is called serially on the target cpus
923 * in logical cpu order. If the mode is ASYNC, the function is called in
924 * parallel over the specified cpus.
925 * The action function may be NULL.
926 * The cpu mask may include the local cpu. Offline cpus are ignored.
927 * Return does not occur until the function has completed on all cpus.
928 * The return value is the number of cpus on which the function was called.
929 */
930 cpu_t
931 mp_cpus_call(
932 cpumask_t cpus,
933 mp_sync_t mode,
934 void (*action_func)(void *),
935 void *arg)
936 {
937 cpu_t cpu;
938 boolean_t intrs_enabled = ml_get_interrupts_enabled();
939 boolean_t call_self = FALSE;
940
941 if (!smp_initialized) {
942 if ((cpus & CPUMASK_SELF) == 0)
943 return 0;
944 if (action_func != NULL) {
945 (void) ml_set_interrupts_enabled(FALSE);
946 action_func(arg);
947 ml_set_interrupts_enabled(intrs_enabled);
948 }
949 return 1;
950 }
951
952 /* obtain rendezvous lock */
953 simple_lock(&mp_rv_lock);
954
955 /* Use the rendezvous data structures for this call */
956 mp_rv_action_func = action_func;
957 mp_rv_func_arg = arg;
958 mp_rv_ncpus = 0;
959 mp_rv_complete = 0;
960
961 simple_lock(&x86_topo_lock);
962 for (cpu = 0; cpu < (cpu_t) real_ncpus; cpu++) {
963 if (((cpu_to_cpumask(cpu) & cpus) == 0) ||
964 !cpu_datap(cpu)->cpu_running)
965 continue;
966 if (cpu == (cpu_t) cpu_number()) {
967 /*
968 * We don't IPI ourself and if calling asynchronously,
969 * we defer our call until we have signalled all others.
970 */
971 call_self = TRUE;
972 if (mode == SYNC && action_func != NULL) {
973 (void) ml_set_interrupts_enabled(FALSE);
974 action_func(arg);
975 ml_set_interrupts_enabled(intrs_enabled);
976 }
977 } else {
978 /*
979 * Bump count of other cpus called and signal this cpu.
980 * Note: we signal asynchronously regardless of mode
981 * because we wait on mp_rv_complete either here
982 * (if mode == SYNC) or later (if mode == ASYNC).
983 * While spinning, poll for TLB flushes if interrupts
984 * are disabled.
985 */
986 mp_rv_ncpus++;
987 i386_signal_cpu(cpu, MP_CALL, ASYNC);
988 if (mode == SYNC) {
989 simple_unlock(&x86_topo_lock);
990 while (mp_rv_complete < mp_rv_ncpus) {
991 if (!intrs_enabled)
992 handle_pending_TLB_flushes();
993 cpu_pause();
994 }
995 simple_lock(&x86_topo_lock);
996 }
997 }
998 }
999 simple_unlock(&x86_topo_lock);
1000
1001 /*
1002 * If calls are being made asynchronously,
1003 * make the local call now if needed, and then
1004 * wait for all other cpus to finish their calls.
1005 */
1006 if (mode == ASYNC) {
1007 if (call_self && action_func != NULL) {
1008 (void) ml_set_interrupts_enabled(FALSE);
1009 action_func(arg);
1010 ml_set_interrupts_enabled(intrs_enabled);
1011 }
1012 while (mp_rv_complete < mp_rv_ncpus) {
1013 if (!intrs_enabled)
1014 handle_pending_TLB_flushes();
1015 cpu_pause();
1016 }
1017 }
1018
1019 /* Determine the number of cpus called */
1020 cpu = mp_rv_ncpus + (call_self ? 1 : 0);
1021
1022 simple_unlock(&mp_rv_lock);
1023
1024 return cpu;
1025 }
1026
1027 static void
1028 mp_broadcast_action(void)
1029 {
1030 /* call action function */
1031 if (mp_bc_action_func != NULL)
1032 mp_bc_action_func(mp_bc_func_arg);
1033
1034 /* if we're the last one through, wake up the instigator */
1035 if (atomic_decl_and_test(&mp_bc_count, 1))
1036 thread_wakeup(((event_t)(uintptr_t) &mp_bc_count));
1037 }
1038
1039 /*
1040 * mp_broadcast() runs a given function on all active cpus.
1041 * The caller blocks until the functions has run on all cpus.
1042 * The caller will also block if there is another pending braodcast.
1043 */
1044 void
1045 mp_broadcast(
1046 void (*action_func)(void *),
1047 void *arg)
1048 {
1049 if (!smp_initialized) {
1050 if (action_func != NULL)
1051 action_func(arg);
1052 return;
1053 }
1054
1055 /* obtain broadcast lock */
1056 lck_mtx_lock(&mp_bc_lock);
1057
1058 /* set static function pointers */
1059 mp_bc_action_func = action_func;
1060 mp_bc_func_arg = arg;
1061
1062 assert_wait((event_t)(uintptr_t)&mp_bc_count, THREAD_UNINT);
1063
1064 /*
1065 * signal other processors, which will call mp_broadcast_action()
1066 */
1067 simple_lock(&x86_topo_lock);
1068 mp_bc_ncpus = i386_active_cpus(); /* total including this cpu */
1069 mp_bc_count = mp_bc_ncpus;
1070 i386_signal_cpus(MP_BROADCAST, ASYNC);
1071
1072 /* call executor function on this cpu */
1073 mp_broadcast_action();
1074 simple_unlock(&x86_topo_lock);
1075
1076 /* block for all cpus to have run action_func */
1077 if (mp_bc_ncpus > 1)
1078 thread_block(THREAD_CONTINUE_NULL);
1079 else
1080 clear_wait(current_thread(), THREAD_AWAKENED);
1081
1082 /* release lock */
1083 lck_mtx_unlock(&mp_bc_lock);
1084 }
1085
1086 void
1087 i386_activate_cpu(void)
1088 {
1089 cpu_data_t *cdp = current_cpu_datap();
1090
1091 assert(!ml_get_interrupts_enabled());
1092
1093 if (!smp_initialized) {
1094 cdp->cpu_running = TRUE;
1095 return;
1096 }
1097
1098 simple_lock(&x86_topo_lock);
1099 cdp->cpu_running = TRUE;
1100 started_cpu();
1101 simple_unlock(&x86_topo_lock);
1102 }
1103
1104 extern void etimer_timer_expire(void *arg);
1105
1106 void
1107 i386_deactivate_cpu(void)
1108 {
1109 cpu_data_t *cdp = current_cpu_datap();
1110
1111 assert(!ml_get_interrupts_enabled());
1112
1113 simple_lock(&x86_topo_lock);
1114 cdp->cpu_running = FALSE;
1115 simple_unlock(&x86_topo_lock);
1116
1117 timer_queue_shutdown(&cdp->rtclock_timer.queue);
1118 cdp->rtclock_timer.deadline = EndOfAllTime;
1119 mp_cpus_call(cpu_to_cpumask(master_cpu), ASYNC, etimer_timer_expire, NULL);
1120
1121 /*
1122 * In case a rendezvous/braodcast/call was initiated to this cpu
1123 * before we cleared cpu_running, we must perform any actions due.
1124 */
1125 if (i_bit(MP_RENDEZVOUS, &cdp->cpu_signals))
1126 mp_rendezvous_action();
1127 if (i_bit(MP_BROADCAST, &cdp->cpu_signals))
1128 mp_broadcast_action();
1129 if (i_bit(MP_CALL, &cdp->cpu_signals))
1130 mp_cpus_call_action();
1131 cdp->cpu_signals = 0; /* all clear */
1132 }
1133
1134 int pmsafe_debug = 1;
1135
1136 #if MACH_KDP
1137 volatile boolean_t mp_kdp_trap = FALSE;
1138 volatile unsigned long mp_kdp_ncpus;
1139 boolean_t mp_kdp_state;
1140
1141
1142 void
1143 mp_kdp_enter(void)
1144 {
1145 unsigned int cpu;
1146 unsigned int ncpus;
1147 unsigned int my_cpu;
1148 uint64_t tsc_timeout;
1149
1150 DBG("mp_kdp_enter()\n");
1151
1152 /*
1153 * Here to enter the debugger.
1154 * In case of races, only one cpu is allowed to enter kdp after
1155 * stopping others.
1156 */
1157 mp_kdp_state = ml_set_interrupts_enabled(FALSE);
1158 simple_lock(&mp_kdp_lock);
1159 debugger_entry_time = mach_absolute_time();
1160 if (pmsafe_debug && !kdp_snapshot)
1161 pmSafeMode(&current_cpu_datap()->lcpu, PM_SAFE_FL_SAFE);
1162
1163 while (mp_kdp_trap) {
1164 simple_unlock(&mp_kdp_lock);
1165 DBG("mp_kdp_enter() race lost\n");
1166 #if MACH_KDP
1167 mp_kdp_wait(TRUE, FALSE);
1168 #endif
1169 simple_lock(&mp_kdp_lock);
1170 }
1171 my_cpu = cpu_number();
1172 debugger_cpu = my_cpu;
1173 mp_kdp_ncpus = 1; /* self */
1174 mp_kdp_trap = TRUE;
1175 simple_unlock(&mp_kdp_lock);
1176
1177 /*
1178 * Deliver a nudge to other cpus, counting how many
1179 */
1180 DBG("mp_kdp_enter() signaling other processors\n");
1181 if (force_immediate_debugger_NMI == FALSE) {
1182 for (ncpus = 1, cpu = 0; cpu < real_ncpus; cpu++) {
1183 if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running)
1184 continue;
1185 ncpus++;
1186 i386_signal_cpu(cpu, MP_KDP, ASYNC);
1187 }
1188 /*
1189 * Wait other processors to synchronize
1190 */
1191 DBG("mp_kdp_enter() waiting for (%d) processors to suspend\n", ncpus);
1192
1193 /*
1194 * This timeout is rather arbitrary; we don't want to NMI
1195 * processors that are executing at potentially
1196 * "unsafe-to-interrupt" points such as the trampolines,
1197 * but neither do we want to lose state by waiting too long.
1198 */
1199 tsc_timeout = rdtsc64() + (ncpus * 1000 * 1000);
1200
1201 while (mp_kdp_ncpus != ncpus && rdtsc64() < tsc_timeout) {
1202 /*
1203 * A TLB shootdown request may be pending--this would
1204 * result in the requesting processor waiting in
1205 * PMAP_UPDATE_TLBS() until this processor deals with it.
1206 * Process it, so it can now enter mp_kdp_wait()
1207 */
1208 handle_pending_TLB_flushes();
1209 cpu_pause();
1210 }
1211 /* If we've timed out, and some processor(s) are still unresponsive,
1212 * interrupt them with an NMI via the local APIC.
1213 */
1214 if (mp_kdp_ncpus != ncpus) {
1215 for (cpu = 0; cpu < real_ncpus; cpu++) {
1216 if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running)
1217 continue;
1218 if (cpu_signal_pending(cpu, MP_KDP))
1219 cpu_NMI_interrupt(cpu);
1220 }
1221 }
1222 }
1223 else
1224 for (cpu = 0; cpu < real_ncpus; cpu++) {
1225 if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running)
1226 continue;
1227 cpu_NMI_interrupt(cpu);
1228 }
1229
1230 DBG("mp_kdp_enter() %u processors done %s\n",
1231 mp_kdp_ncpus, (mp_kdp_ncpus == ncpus) ? "OK" : "timed out");
1232
1233 postcode(MP_KDP_ENTER);
1234 }
1235
1236 static boolean_t
1237 cpu_signal_pending(int cpu, mp_event_t event)
1238 {
1239 volatile int *signals = &cpu_datap(cpu)->cpu_signals;
1240 boolean_t retval = FALSE;
1241
1242 if (i_bit(event, signals))
1243 retval = TRUE;
1244 return retval;
1245 }
1246
1247 long kdp_x86_xcpu_invoke(const uint16_t lcpu, kdp_x86_xcpu_func_t func,
1248 void *arg0, void *arg1)
1249 {
1250 if (lcpu > (real_ncpus - 1))
1251 return -1;
1252
1253 if (func == NULL)
1254 return -1;
1255
1256 kdp_xcpu_call_func.func = func;
1257 kdp_xcpu_call_func.ret = -1;
1258 kdp_xcpu_call_func.arg0 = arg0;
1259 kdp_xcpu_call_func.arg1 = arg1;
1260 kdp_xcpu_call_func.cpu = lcpu;
1261 DBG("Invoking function %p on CPU %d\n", func, (int32_t)lcpu);
1262 while (kdp_xcpu_call_func.cpu != KDP_XCPU_NONE)
1263 cpu_pause();
1264 return kdp_xcpu_call_func.ret;
1265 }
1266
1267 static void
1268 kdp_x86_xcpu_poll(void)
1269 {
1270 if ((uint16_t)cpu_number() == kdp_xcpu_call_func.cpu) {
1271 kdp_xcpu_call_func.ret =
1272 kdp_xcpu_call_func.func(kdp_xcpu_call_func.arg0,
1273 kdp_xcpu_call_func.arg1,
1274 cpu_number());
1275 kdp_xcpu_call_func.cpu = KDP_XCPU_NONE;
1276 }
1277 }
1278
1279 static void
1280 mp_kdp_wait(boolean_t flush, boolean_t isNMI)
1281 {
1282 DBG("mp_kdp_wait()\n");
1283 /* If an I/O port has been specified as a debugging aid, issue a read */
1284 panic_io_port_read();
1285
1286 #if CONFIG_MCA
1287 /* If we've trapped due to a machine-check, save MCA registers */
1288 mca_check_save();
1289 #endif
1290
1291 atomic_incl((volatile long *)&mp_kdp_ncpus, 1);
1292 while (mp_kdp_trap || (isNMI == TRUE)) {
1293 /*
1294 * A TLB shootdown request may be pending--this would result
1295 * in the requesting processor waiting in PMAP_UPDATE_TLBS()
1296 * until this processor handles it.
1297 * Process it, so it can now enter mp_kdp_wait()
1298 */
1299 if (flush)
1300 handle_pending_TLB_flushes();
1301
1302 kdp_x86_xcpu_poll();
1303 cpu_pause();
1304 }
1305
1306 atomic_decl((volatile long *)&mp_kdp_ncpus, 1);
1307 DBG("mp_kdp_wait() done\n");
1308 }
1309
1310 void
1311 mp_kdp_exit(void)
1312 {
1313 DBG("mp_kdp_exit()\n");
1314 debugger_cpu = -1;
1315 atomic_decl((volatile long *)&mp_kdp_ncpus, 1);
1316
1317 debugger_exit_time = mach_absolute_time();
1318
1319 mp_kdp_trap = FALSE;
1320 __asm__ volatile("mfence");
1321
1322 /* Wait other processors to stop spinning. XXX needs timeout */
1323 DBG("mp_kdp_exit() waiting for processors to resume\n");
1324 while (mp_kdp_ncpus > 0) {
1325 /*
1326 * a TLB shootdown request may be pending... this would result in the requesting
1327 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1328 * Process it, so it can now enter mp_kdp_wait()
1329 */
1330 handle_pending_TLB_flushes();
1331
1332 cpu_pause();
1333 }
1334
1335 if (pmsafe_debug && !kdp_snapshot)
1336 pmSafeMode(&current_cpu_datap()->lcpu, PM_SAFE_FL_NORMAL);
1337
1338 DBG("mp_kdp_exit() done\n");
1339 (void) ml_set_interrupts_enabled(mp_kdp_state);
1340 postcode(0);
1341 }
1342 #endif /* MACH_KDP */
1343
1344 boolean_t
1345 mp_recent_debugger_activity() {
1346 return (((mach_absolute_time() - debugger_entry_time) < LastDebuggerEntryAllowance) ||
1347 ((mach_absolute_time() - debugger_exit_time) < LastDebuggerEntryAllowance));
1348 }
1349
1350 /*ARGSUSED*/
1351 void
1352 init_ast_check(
1353 __unused processor_t processor)
1354 {
1355 }
1356
1357 void
1358 cause_ast_check(
1359 processor_t processor)
1360 {
1361 int cpu = processor->cpu_id;
1362
1363 if (cpu != cpu_number()) {
1364 i386_signal_cpu(cpu, MP_AST, ASYNC);
1365 }
1366 }
1367
1368 #if MACH_KDB
1369 /*
1370 * invoke kdb on slave processors
1371 */
1372
1373 void
1374 remote_kdb(void)
1375 {
1376 unsigned int my_cpu = cpu_number();
1377 unsigned int cpu;
1378 int kdb_ncpus;
1379 uint64_t tsc_timeout = 0;
1380
1381 mp_kdb_trap = TRUE;
1382 mp_kdb_ncpus = 1;
1383 for (kdb_ncpus = 1, cpu = 0; cpu < real_ncpus; cpu++) {
1384 if (cpu == my_cpu || !cpu_datap(cpu)->cpu_running)
1385 continue;
1386 kdb_ncpus++;
1387 i386_signal_cpu(cpu, MP_KDB, ASYNC);
1388 }
1389 DBG("remote_kdb() waiting for (%d) processors to suspend\n",kdb_ncpus);
1390
1391 tsc_timeout = rdtsc64() + (kdb_ncpus * 100 * 1000 * 1000);
1392
1393 while (mp_kdb_ncpus != kdb_ncpus && rdtsc64() < tsc_timeout) {
1394 /*
1395 * a TLB shootdown request may be pending... this would result in the requesting
1396 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1397 * Process it, so it can now enter mp_kdp_wait()
1398 */
1399 handle_pending_TLB_flushes();
1400
1401 cpu_pause();
1402 }
1403 DBG("mp_kdp_enter() %d processors done %s\n",
1404 mp_kdb_ncpus, (mp_kdb_ncpus == kdb_ncpus) ? "OK" : "timed out");
1405 }
1406
1407 static void
1408 mp_kdb_wait(void)
1409 {
1410 DBG("mp_kdb_wait()\n");
1411
1412 /* If an I/O port has been specified as a debugging aid, issue a read */
1413 panic_io_port_read();
1414
1415 atomic_incl(&mp_kdb_ncpus, 1);
1416 while (mp_kdb_trap) {
1417 /*
1418 * a TLB shootdown request may be pending... this would result in the requesting
1419 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1420 * Process it, so it can now enter mp_kdp_wait()
1421 */
1422 handle_pending_TLB_flushes();
1423
1424 cpu_pause();
1425 }
1426 atomic_decl((volatile long *)&mp_kdb_ncpus, 1);
1427 DBG("mp_kdb_wait() done\n");
1428 }
1429
1430 /*
1431 * Clear kdb interrupt
1432 */
1433
1434 void
1435 clear_kdb_intr(void)
1436 {
1437 mp_disable_preemption();
1438 i_bit_clear(MP_KDB, &current_cpu_datap()->cpu_signals);
1439 mp_enable_preemption();
1440 }
1441
1442 void
1443 mp_kdb_exit(void)
1444 {
1445 DBG("mp_kdb_exit()\n");
1446 atomic_decl((volatile long *)&mp_kdb_ncpus, 1);
1447 mp_kdb_trap = FALSE;
1448 __asm__ volatile("mfence");
1449
1450 while (mp_kdb_ncpus > 0) {
1451 /*
1452 * a TLB shootdown request may be pending... this would result in the requesting
1453 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1454 * Process it, so it can now enter mp_kdp_wait()
1455 */
1456 handle_pending_TLB_flushes();
1457
1458 cpu_pause();
1459 }
1460
1461 DBG("mp_kdb_exit() done\n");
1462 }
1463
1464 #endif /* MACH_KDB */
1465
1466 void
1467 slave_machine_init(void *param)
1468 {
1469 /*
1470 * Here in process context, but with interrupts disabled.
1471 */
1472 DBG("slave_machine_init() CPU%d\n", get_cpu_number());
1473
1474 if (param == FULL_SLAVE_INIT) {
1475 /*
1476 * Cold start
1477 */
1478 clock_init();
1479
1480 cpu_machine_init(); /* Interrupts enabled hereafter */
1481 }
1482 }
1483
1484 #undef cpu_number
1485 int cpu_number(void)
1486 {
1487 return get_cpu_number();
1488 }
1489
1490 #if MACH_KDB
1491 #include <ddb/db_output.h>
1492
1493 #define TRAP_DEBUG 0 /* Must match interrupt.s and spl.s */
1494
1495
1496 #if TRAP_DEBUG
1497 #define MTRAPS 100
1498 struct mp_trap_hist_struct {
1499 unsigned char type;
1500 unsigned char data[5];
1501 } trap_hist[MTRAPS], *cur_trap_hist = trap_hist,
1502 *max_trap_hist = &trap_hist[MTRAPS];
1503
1504 void db_trap_hist(void);
1505
1506 /*
1507 * SPL:
1508 * 1: new spl
1509 * 2: old spl
1510 * 3: new tpr
1511 * 4: old tpr
1512 * INT:
1513 * 1: int vec
1514 * 2: old spl
1515 * 3: new spl
1516 * 4: post eoi tpr
1517 * 5: exit tpr
1518 */
1519
1520 void
1521 db_trap_hist(void)
1522 {
1523 int i,j;
1524 for(i=0;i<MTRAPS;i++)
1525 if (trap_hist[i].type == 1 || trap_hist[i].type == 2) {
1526 db_printf("%s%s",
1527 (&trap_hist[i]>=cur_trap_hist)?"*":" ",
1528 (trap_hist[i].type == 1)?"SPL":"INT");
1529 for(j=0;j<5;j++)
1530 db_printf(" %02x", trap_hist[i].data[j]);
1531 db_printf("\n");
1532 }
1533
1534 }
1535 #endif /* TRAP_DEBUG */
1536 #endif /* MACH_KDB */
1537