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