2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
29 #include <mach_ldebug.h>
32 #include <mach/mach_types.h>
33 #include <mach/kern_return.h>
35 #include <kern/kern_types.h>
36 #include <kern/startup.h>
37 #include <kern/processor.h>
38 #include <kern/cpu_number.h>
39 #include <kern/cpu_data.h>
40 #include <kern/assert.h>
41 #include <kern/machine.h>
44 #include <vm/vm_map.h>
45 #include <vm/vm_kern.h>
47 #include <profiling/profile-mk.h>
50 #include <i386/mp_events.h>
51 #include <i386/mp_slave_boot.h>
52 #include <i386/apic.h>
55 #include <i386/cpuid.h>
56 #include <i386/proc_reg.h>
57 #include <i386/machine_cpu.h>
58 #include <i386/misc_protos.h>
59 #include <i386/mtrr.h>
60 #include <i386/postcode.h>
61 #include <i386/perfmon.h>
62 #include <i386/cpu_threads.h>
63 #include <i386/mp_desc.h>
64 #include <i386/trap.h>
65 #include <i386/machine_routines.h>
66 #include <i386/pmCPU.h>
67 #include <i386/hpet.h>
69 #include <chud/chud_xnu.h>
70 #include <chud/chud_xnu_private.h>
72 #include <sys/kdebug.h>
74 #include <i386/db_machdep.h>
75 #include <ddb/db_aout.h>
76 #include <ddb/db_access.h>
77 #include <ddb/db_sym.h>
78 #include <ddb/db_variables.h>
79 #include <ddb/db_command.h>
80 #include <ddb/db_output.h>
81 #include <ddb/db_expr.h>
85 #define PAUSE delay(1000000)
86 #define DBG(x...) kprintf(x)
92 /* Initialize lapic_id so cpu_number() works on non SMP systems */
93 unsigned long lapic_id_initdata
= 0;
94 unsigned long lapic_id
= (unsigned long)&lapic_id_initdata
;
95 vm_offset_t lapic_start
;
97 static i386_intr_func_t lapic_timer_func
;
98 static i386_intr_func_t lapic_pmi_func
;
99 static i386_intr_func_t lapic_thermal_func
;
101 /* TRUE if local APIC was enabled by the OS not by the BIOS */
102 static boolean_t lapic_os_enabled
= FALSE
;
104 /* Base vector for local APIC interrupt sources */
105 int lapic_interrupt_base
= LAPIC_DEFAULT_INTERRUPT_BASE
;
107 void slave_boot_init(void);
110 static void mp_kdb_wait(void);
111 volatile boolean_t mp_kdb_trap
= FALSE
;
112 volatile long mp_kdb_ncpus
= 0;
115 static void mp_kdp_wait(void);
116 static void mp_rendezvous_action(void);
118 static int NMIInterruptHandler(void *regs
);
119 static boolean_t
cpu_signal_pending(int cpu
, mp_event_t event
);
120 static void cpu_NMI_interrupt(int cpu
);
122 boolean_t smp_initialized
= FALSE
;
124 decl_simple_lock_data(,mp_kdp_lock
);
126 decl_mutex_data(static, mp_cpu_boot_lock
);
128 /* Variables needed for MP rendezvous. */
129 decl_simple_lock_data(,mp_rv_lock
);
130 static void (*mp_rv_setup_func
)(void *arg
);
131 static void (*mp_rv_action_func
)(void *arg
);
132 static void (*mp_rv_teardown_func
)(void *arg
);
133 static void *mp_rv_func_arg
;
134 static int mp_rv_ncpus
;
135 /* Cache-aligned barriers: */
136 static volatile long mp_rv_entry
__attribute__((aligned(64)));
137 static volatile long mp_rv_exit
__attribute__((aligned(64)));
138 static volatile long mp_rv_complete
__attribute__((aligned(64)));
140 int lapic_to_cpu
[MAX_CPUS
];
141 int cpu_to_lapic
[MAX_CPUS
];
144 lapic_cpu_map_init(void)
148 for (i
= 0; i
< MAX_CPUS
; i
++) {
149 lapic_to_cpu
[i
] = -1;
150 cpu_to_lapic
[i
] = -1;
155 lapic_cpu_map(int apic_id
, int cpu
)
157 cpu_to_lapic
[cpu
] = apic_id
;
158 lapic_to_cpu
[apic_id
] = cpu
;
162 * Retrieve the local apic ID a cpu.
164 * Returns the local apic ID for the given processor.
165 * If the processor does not exist or apic not configured, returns -1.
169 ml_get_apicid(uint32_t cpu
)
171 if(cpu
>= (uint32_t)MAX_CPUS
)
172 return 0xFFFFFFFF; /* Return -1 if cpu too big */
174 /* Return the apic ID (or -1 if not configured) */
175 return (uint32_t)cpu_to_lapic
[cpu
];
181 lapic_cpu_map_dump(void)
185 for (i
= 0; i
< MAX_CPUS
; i
++) {
186 if (cpu_to_lapic
[i
] == -1)
188 kprintf("cpu_to_lapic[%d]: %d\n",
191 for (i
= 0; i
< MAX_CPUS
; i
++) {
192 if (lapic_to_cpu
[i
] == -1)
194 kprintf("lapic_to_cpu[%d]: %d\n",
198 #define LAPIC_CPU_MAP_DUMP() lapic_cpu_map_dump()
199 #define LAPIC_DUMP() lapic_dump()
201 #define LAPIC_CPU_MAP_DUMP()
203 #endif /* MP_DEBUG */
207 * Initialize dummy structs for profiling. These aren't used but
208 * allows hertz_tick() to be built with GPROF defined.
210 struct profile_vars _profile_vars
;
211 struct profile_vars
*_profile_vars_cpus
[MAX_CPUS
] = { &_profile_vars
};
212 #define GPROF_INIT() \
216 /* Hack to initialize pointers to unused profiling structs */ \
217 for (i = 1; i < MAX_CPUS; i++) \
218 _profile_vars_cpus[i] = &_profile_vars; \
228 vm_map_entry_t entry
;
231 boolean_t is_boot_processor
;
232 boolean_t is_lapic_enabled
;
233 vm_offset_t lapic_base
;
235 simple_lock_init(&mp_kdp_lock
, 0);
236 simple_lock_init(&mp_rv_lock
, 0);
237 mutex_init(&mp_cpu_boot_lock
, 0);
244 /* Examine the local APIC state */
245 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
246 is_boot_processor
= (lo
& MSR_IA32_APIC_BASE_BSP
) != 0;
247 is_lapic_enabled
= (lo
& MSR_IA32_APIC_BASE_ENABLE
) != 0;
248 lapic_base
= (lo
& MSR_IA32_APIC_BASE_BASE
);
249 kprintf("MSR_IA32_APIC_BASE 0x%x %s %s\n", lapic_base
,
250 is_lapic_enabled
? "enabled" : "disabled",
251 is_boot_processor
? "BSP" : "AP");
252 if (!is_boot_processor
|| !is_lapic_enabled
)
253 panic("Unexpected local APIC state\n");
255 /* Establish a map to the local apic */
256 lapic_start
= vm_map_min(kernel_map
);
257 result
= vm_map_find_space(kernel_map
,
258 (vm_map_address_t
*) &lapic_start
,
259 round_page(LAPIC_SIZE
), 0,
260 VM_MAKE_TAG(VM_MEMORY_IOKIT
), &entry
);
261 if (result
!= KERN_SUCCESS
) {
262 panic("smp_init: vm_map_find_entry FAILED (err=%d)", result
);
264 vm_map_unlock(kernel_map
);
265 /* Map in the local APIC non-cacheable, as recommended by Intel
266 * in section 8.4.1 of the "System Programming Guide".
268 pmap_enter(pmap_kernel(),
270 (ppnum_t
) i386_btop(lapic_base
),
271 VM_PROT_READ
|VM_PROT_WRITE
,
274 lapic_id
= (unsigned long)(lapic_start
+ LAPIC_ID
);
276 if ((LAPIC_REG(VERSION
)&LAPIC_VERSION_MASK
) != 0x14) {
277 printf("Local APIC version not 0x14 as expected\n");
280 /* Set up the lapic_id <-> cpu_number map and add this boot processor */
281 lapic_cpu_map_init();
282 lapic_cpu_map((LAPIC_REG(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
, 0);
283 kprintf("Boot cpu local APIC id 0x%x\n", cpu_to_lapic
[0]);
290 DBGLOG_CPU_INIT(master_cpu
);
294 smp_initialized
= TRUE
;
303 /* write-read register */
304 LAPIC_REG(ERROR_STATUS
) = 0;
305 return LAPIC_REG(ERROR_STATUS
);
309 lapic_esr_clear(void)
311 LAPIC_REG(ERROR_STATUS
) = 0;
312 LAPIC_REG(ERROR_STATUS
) = 0;
315 static const char *DM
[8] = {
330 #define BOOL(a) ((a)?' ':'!')
332 kprintf("LAPIC %d at 0x%x version 0x%x\n",
333 (LAPIC_REG(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
,
335 LAPIC_REG(VERSION
)&LAPIC_VERSION_MASK
);
336 kprintf("Priorities: Task 0x%x Arbitration 0x%x Processor 0x%x\n",
337 LAPIC_REG(TPR
)&LAPIC_TPR_MASK
,
338 LAPIC_REG(APR
)&LAPIC_APR_MASK
,
339 LAPIC_REG(PPR
)&LAPIC_PPR_MASK
);
340 kprintf("Destination Format 0x%x Logical Destination 0x%x\n",
341 LAPIC_REG(DFR
)>>LAPIC_DFR_SHIFT
,
342 LAPIC_REG(LDR
)>>LAPIC_LDR_SHIFT
);
343 kprintf("%cEnabled %cFocusChecking SV 0x%x\n",
344 BOOL(LAPIC_REG(SVR
)&LAPIC_SVR_ENABLE
),
345 BOOL(!(LAPIC_REG(SVR
)&LAPIC_SVR_FOCUS_OFF
)),
346 LAPIC_REG(SVR
) & LAPIC_SVR_MASK
);
347 kprintf("LVT_TIMER: Vector 0x%02x %s %cmasked %s\n",
348 LAPIC_REG(LVT_TIMER
)&LAPIC_LVT_VECTOR_MASK
,
349 (LAPIC_REG(LVT_TIMER
)&LAPIC_LVT_DS_PENDING
)?"SendPending":"Idle",
350 BOOL(LAPIC_REG(LVT_TIMER
)&LAPIC_LVT_MASKED
),
351 (LAPIC_REG(LVT_TIMER
)&LAPIC_LVT_PERIODIC
)?"Periodic":"OneShot");
352 kprintf(" Initial Count: 0x%08x \n", LAPIC_REG(TIMER_INITIAL_COUNT
));
353 kprintf(" Current Count: 0x%08x \n", LAPIC_REG(TIMER_CURRENT_COUNT
));
354 kprintf(" Divide Config: 0x%08x \n", LAPIC_REG(TIMER_DIVIDE_CONFIG
));
355 kprintf("LVT_PERFCNT: Vector 0x%02x [%s] %s %cmasked\n",
356 LAPIC_REG(LVT_PERFCNT
)&LAPIC_LVT_VECTOR_MASK
,
357 DM
[(LAPIC_REG(LVT_PERFCNT
)>>LAPIC_LVT_DM_SHIFT
)&LAPIC_LVT_DM_MASK
],
358 (LAPIC_REG(LVT_PERFCNT
)&LAPIC_LVT_DS_PENDING
)?"SendPending":"Idle",
359 BOOL(LAPIC_REG(LVT_PERFCNT
)&LAPIC_LVT_MASKED
));
360 kprintf("LVT_THERMAL: Vector 0x%02x [%s] %s %cmasked\n",
361 LAPIC_REG(LVT_THERMAL
)&LAPIC_LVT_VECTOR_MASK
,
362 DM
[(LAPIC_REG(LVT_THERMAL
)>>LAPIC_LVT_DM_SHIFT
)&LAPIC_LVT_DM_MASK
],
363 (LAPIC_REG(LVT_THERMAL
)&LAPIC_LVT_DS_PENDING
)?"SendPending":"Idle",
364 BOOL(LAPIC_REG(LVT_THERMAL
)&LAPIC_LVT_MASKED
));
365 kprintf("LVT_LINT0: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
366 LAPIC_REG(LVT_LINT0
)&LAPIC_LVT_VECTOR_MASK
,
367 DM
[(LAPIC_REG(LVT_LINT0
)>>LAPIC_LVT_DM_SHIFT
)&LAPIC_LVT_DM_MASK
],
368 (LAPIC_REG(LVT_LINT0
)&LAPIC_LVT_TM_LEVEL
)?"Level":"Edge ",
369 (LAPIC_REG(LVT_LINT0
)&LAPIC_LVT_IP_PLRITY_LOW
)?"Low ":"High",
370 (LAPIC_REG(LVT_LINT0
)&LAPIC_LVT_DS_PENDING
)?"SendPending":"Idle",
371 BOOL(LAPIC_REG(LVT_LINT0
)&LAPIC_LVT_MASKED
));
372 kprintf("LVT_LINT1: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
373 LAPIC_REG(LVT_LINT1
)&LAPIC_LVT_VECTOR_MASK
,
374 DM
[(LAPIC_REG(LVT_LINT1
)>>LAPIC_LVT_DM_SHIFT
)&LAPIC_LVT_DM_MASK
],
375 (LAPIC_REG(LVT_LINT1
)&LAPIC_LVT_TM_LEVEL
)?"Level":"Edge ",
376 (LAPIC_REG(LVT_LINT1
)&LAPIC_LVT_IP_PLRITY_LOW
)?"Low ":"High",
377 (LAPIC_REG(LVT_LINT1
)&LAPIC_LVT_DS_PENDING
)?"SendPending":"Idle",
378 BOOL(LAPIC_REG(LVT_LINT1
)&LAPIC_LVT_MASKED
));
379 kprintf("LVT_ERROR: Vector 0x%02x %s %cmasked\n",
380 LAPIC_REG(LVT_ERROR
)&LAPIC_LVT_VECTOR_MASK
,
381 (LAPIC_REG(LVT_ERROR
)&LAPIC_LVT_DS_PENDING
)?"SendPending":"Idle",
382 BOOL(LAPIC_REG(LVT_ERROR
)&LAPIC_LVT_MASKED
));
383 kprintf("ESR: %08x \n", lapic_esr_read());
385 for(i
=0xf; i
>=0; i
--)
386 kprintf("%x%x%x%x",i
,i
,i
,i
);
390 kprintf("%08x",LAPIC_REG_OFFSET(TMR_BASE
, i
*0x10));
394 kprintf("%08x",LAPIC_REG_OFFSET(IRR_BASE
, i
*0x10));
397 for(i
=7; i
>= 0; i
--)
398 kprintf("%08x",LAPIC_REG_OFFSET(ISR_BASE
, i
*0x10));
409 db_apic(__unused db_expr_t addr
,
410 __unused
int have_addr
,
411 __unused db_expr_t count
,
412 __unused
char *modif
)
428 if (cpuid_features() & CPUID_FEATURE_APIC
)
431 if (cpuid_family() == 6 || cpuid_family() == 15) {
434 * There may be a local APIC which wasn't enabled by BIOS.
435 * So we try to enable it explicitly.
437 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
438 lo
&= ~MSR_IA32_APIC_BASE_BASE
;
439 lo
|= MSR_IA32_APIC_BASE_ENABLE
| LAPIC_START
;
440 lo
|= MSR_IA32_APIC_BASE_ENABLE
;
441 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
444 * Re-initialize cpu features info and re-check.
447 if (cpuid_features() & CPUID_FEATURE_APIC
) {
448 printf("Local APIC discovered and enabled\n");
449 lapic_os_enabled
= TRUE
;
450 lapic_interrupt_base
= LAPIC_REDUCED_INTERRUPT_BASE
;
465 /* Shutdown if local APIC was enabled by OS */
466 if (lapic_os_enabled
== FALSE
)
469 mp_disable_preemption();
472 if (get_cpu_number() == master_cpu
) {
473 value
= LAPIC_REG(LVT_LINT0
);
474 value
|= LAPIC_LVT_MASKED
;
475 LAPIC_REG(LVT_LINT0
) = value
;
479 LAPIC_REG(LVT_TIMER
) |= LAPIC_LVT_MASKED
;
481 /* Perfmon: masked */
482 LAPIC_REG(LVT_PERFCNT
) |= LAPIC_LVT_MASKED
;
485 LAPIC_REG(LVT_ERROR
) |= LAPIC_LVT_MASKED
;
487 /* APIC software disabled */
488 LAPIC_REG(SVR
) &= ~LAPIC_SVR_ENABLE
;
490 /* Bypass the APIC completely and update cpu features */
491 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
492 lo
&= ~MSR_IA32_APIC_BASE_ENABLE
;
493 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
496 mp_enable_preemption();
504 /* Set flat delivery model, logical processor id */
505 LAPIC_REG(DFR
) = LAPIC_DFR_FLAT
;
506 LAPIC_REG(LDR
) = (get_cpu_number()) << LAPIC_LDR_SHIFT
;
511 LAPIC_REG(SVR
) = LAPIC_VECTOR(SPURIOUS
) | LAPIC_SVR_ENABLE
;
514 if (get_cpu_number() == master_cpu
) {
515 value
= LAPIC_REG(LVT_LINT0
);
516 value
&= ~LAPIC_LVT_MASKED
;
517 value
|= LAPIC_LVT_DM_EXTINT
;
518 LAPIC_REG(LVT_LINT0
) = value
;
521 /* Timer: unmasked, one-shot */
522 LAPIC_REG(LVT_TIMER
) = LAPIC_VECTOR(TIMER
);
524 /* Perfmon: unmasked */
525 LAPIC_REG(LVT_PERFCNT
) = LAPIC_VECTOR(PERFCNT
);
527 /* Thermal: unmasked */
528 LAPIC_REG(LVT_THERMAL
) = LAPIC_VECTOR(THERMAL
);
532 LAPIC_REG(LVT_ERROR
) = LAPIC_VECTOR(ERROR
);
536 lapic_set_timer_func(i386_intr_func_t func
)
538 lapic_timer_func
= func
;
544 lapic_timer_mode_t mode
,
545 lapic_timer_divide_t divisor
,
546 lapic_timer_count_t initial_count
)
549 uint32_t timer_vector
;
551 state
= ml_set_interrupts_enabled(FALSE
);
552 timer_vector
= LAPIC_REG(LVT_TIMER
);
553 timer_vector
&= ~(LAPIC_LVT_MASKED
|LAPIC_LVT_PERIODIC
);;
554 timer_vector
|= interrupt
? 0 : LAPIC_LVT_MASKED
;
555 timer_vector
|= (mode
== periodic
) ? LAPIC_LVT_PERIODIC
: 0;
556 LAPIC_REG(LVT_TIMER
) = timer_vector
;
557 LAPIC_REG(TIMER_DIVIDE_CONFIG
) = divisor
;
558 LAPIC_REG(TIMER_INITIAL_COUNT
) = initial_count
;
559 ml_set_interrupts_enabled(state
);
564 lapic_timer_mode_t
*mode
,
565 lapic_timer_divide_t
*divisor
,
566 lapic_timer_count_t
*initial_count
,
567 lapic_timer_count_t
*current_count
)
571 state
= ml_set_interrupts_enabled(FALSE
);
573 *mode
= (LAPIC_REG(LVT_TIMER
) & LAPIC_LVT_PERIODIC
) ?
576 *divisor
= LAPIC_REG(TIMER_DIVIDE_CONFIG
) & LAPIC_TIMER_DIVIDE_MASK
;
578 *initial_count
= LAPIC_REG(TIMER_INITIAL_COUNT
);
580 *current_count
= LAPIC_REG(TIMER_CURRENT_COUNT
);
581 ml_set_interrupts_enabled(state
);
585 lapic_set_pmi_func(i386_intr_func_t func
)
587 lapic_pmi_func
= func
;
591 lapic_set_thermal_func(i386_intr_func_t func
)
593 lapic_thermal_func
= func
;
597 _lapic_end_of_interrupt(void)
603 lapic_end_of_interrupt(void)
605 _lapic_end_of_interrupt();
609 lapic_interrupt(int interrupt
, x86_saved_state_t
*state
)
613 /* Did we just field an interruption for the HPET comparator? */
614 if(current_cpu_datap()->cpu_pmHpetVec
== ((uint32_t)interrupt
- 0x40)) {
615 /* Yes, go handle it... */
616 retval
= HPETInterrupt();
617 /* Was it really handled? */
619 /* If so, EOI the 'rupt */
620 _lapic_end_of_interrupt();
623 * indicating that this has been handled
629 interrupt
-= lapic_interrupt_base
;
631 if (interrupt
== (LAPIC_NMI_INTERRUPT
- lapic_interrupt_base
)) {
632 retval
= NMIInterruptHandler(state
);
633 _lapic_end_of_interrupt();
641 case LAPIC_PERFCNT_INTERRUPT
:
642 if (lapic_pmi_func
!= NULL
)
643 (*lapic_pmi_func
)(NULL
);
644 /* Clear interrupt masked */
645 LAPIC_REG(LVT_PERFCNT
) = LAPIC_VECTOR(PERFCNT
);
646 _lapic_end_of_interrupt();
649 case LAPIC_TIMER_INTERRUPT
:
650 _lapic_end_of_interrupt();
651 if (lapic_timer_func
!= NULL
)
652 (*lapic_timer_func
)(state
);
655 case LAPIC_THERMAL_INTERRUPT
:
656 if (lapic_thermal_func
!= NULL
)
657 (*lapic_thermal_func
)(NULL
);
658 _lapic_end_of_interrupt();
661 case LAPIC_ERROR_INTERRUPT
:
663 panic("Local APIC error\n");
664 _lapic_end_of_interrupt();
667 case LAPIC_SPURIOUS_INTERRUPT
:
669 /* No EOI required here */
672 case LAPIC_INTERPROCESSOR_INTERRUPT
:
673 _lapic_end_of_interrupt();
674 cpu_signal_handler(state
);
683 lapic_smm_restore(void)
687 if (lapic_os_enabled
== FALSE
)
690 state
= ml_set_interrupts_enabled(FALSE
);
692 if (LAPIC_ISR_IS_SET(LAPIC_REDUCED_INTERRUPT_BASE
, TIMER
)) {
694 * Bogus SMI handler enables interrupts but does not know about
695 * local APIC interrupt sources. When APIC timer counts down to
696 * zero while in SMM, local APIC will end up waiting for an EOI
697 * but no interrupt was delivered to the OS.
699 _lapic_end_of_interrupt();
702 * timer is one-shot, trigger another quick countdown to trigger
703 * another timer interrupt.
705 if (LAPIC_REG(TIMER_CURRENT_COUNT
) == 0) {
706 LAPIC_REG(TIMER_INITIAL_COUNT
) = 1;
709 kprintf("lapic_smm_restore\n");
712 ml_set_interrupts_enabled(state
);
721 int lapic
= cpu_to_lapic
[slot_num
];
725 DBGLOG_CPU_INIT(slot_num
);
727 DBG("intel_startCPU(%d) lapic_id=%d\n", slot_num
, lapic
);
728 DBG("IdlePTD(%p): 0x%x\n", &IdlePTD
, (int) IdlePTD
);
731 * Initialize (or re-initialize) the descriptor tables for this cpu.
732 * Propagate processor mode to slave.
734 if (cpu_mode_is64bit())
735 cpu_desc_init64(cpu_datap(slot_num
), FALSE
);
737 cpu_desc_init(cpu_datap(slot_num
), FALSE
);
739 /* Serialize use of the slave boot stack. */
740 mutex_lock(&mp_cpu_boot_lock
);
742 mp_disable_preemption();
743 if (slot_num
== get_cpu_number()) {
744 mp_enable_preemption();
745 mutex_unlock(&mp_cpu_boot_lock
);
749 LAPIC_REG(ICRD
) = lapic
<< LAPIC_ICRD_DEST_SHIFT
;
750 LAPIC_REG(ICR
) = LAPIC_ICR_DM_INIT
;
753 LAPIC_REG(ICRD
) = lapic
<< LAPIC_ICRD_DEST_SHIFT
;
754 LAPIC_REG(ICR
) = LAPIC_ICR_DM_STARTUP
|(MP_BOOT
>>12);
757 LAPIC_REG(ICRD
) = lapic
<< LAPIC_ICRD_DEST_SHIFT
;
758 LAPIC_REG(ICR
) = LAPIC_ICR_DM_STARTUP
|(MP_BOOT
>>12);
761 #ifdef POSTCODE_DELAY
762 /* Wait much longer if postcodes are displayed for a delay period. */
766 if (cpu_datap(slot_num
)->cpu_running
)
771 mp_enable_preemption();
772 mutex_unlock(&mp_cpu_boot_lock
);
774 if (!cpu_datap(slot_num
)->cpu_running
) {
775 kprintf("Failed to start CPU %02d\n", slot_num
);
776 printf("Failed to start CPU %02d, rebooting...\n", slot_num
);
781 kprintf("Started cpu %d (lapic id %p)\n", slot_num
, lapic
);
782 printf("Started CPU %02d\n", slot_num
);
787 extern char slave_boot_base
[];
788 extern char slave_boot_end
[];
789 extern void slave_pstart(void);
792 slave_boot_init(void)
794 DBG("V(slave_boot_base)=%p P(slave_boot_base)=%p MP_BOOT=%p sz=0x%x\n",
796 kvtophys((vm_offset_t
) slave_boot_base
),
798 slave_boot_end
-slave_boot_base
);
801 * Copy the boot entry code to the real-mode vector area MP_BOOT.
802 * This is in page 1 which has been reserved for this purpose by
803 * machine_startup() from the boot processor.
804 * The slave boot code is responsible for switching to protected
805 * mode and then jumping to the common startup, _start().
807 bcopy_phys(kvtophys((vm_offset_t
) slave_boot_base
),
809 slave_boot_end
-slave_boot_base
);
812 * Zero a stack area above the boot code.
814 DBG("bzero_phys 0x%x sz 0x%x\n",MP_BOOTSTACK
+MP_BOOT
-0x400, 0x400);
815 bzero_phys((addr64_t
)MP_BOOTSTACK
+MP_BOOT
-0x400, 0x400);
818 * Set the location at the base of the stack to point to the
819 * common startup entry.
821 DBG("writing 0x%x at phys 0x%x\n",
822 kvtophys((vm_offset_t
) &slave_pstart
), MP_MACH_START
+MP_BOOT
);
823 ml_phys_write_word(MP_MACH_START
+MP_BOOT
,
824 (unsigned int)kvtophys((vm_offset_t
) &slave_pstart
));
831 cpu_signal_event_log_t
*cpu_signal
[MAX_CPUS
];
832 cpu_signal_event_log_t
*cpu_handle
[MAX_CPUS
];
834 MP_EVENT_NAME_DECL();
836 #endif /* MP_DEBUG */
839 cpu_signal_handler(x86_saved_state_t
*regs
)
842 volatile int *my_word
;
843 #if MACH_KDB && MACH_ASSERT
845 #endif /* MACH_KDB && MACH_ASSERT */
847 mp_disable_preemption();
849 my_cpu
= cpu_number();
850 my_word
= ¤t_cpu_datap()->cpu_signals
;
853 #if MACH_KDB && MACH_ASSERT
855 Debugger("cpu_signal_handler: signals did not clear");
856 #endif /* MACH_KDB && MACH_ASSERT */
858 if (i_bit(MP_KDP
, my_word
)) {
859 DBGLOG(cpu_handle
,my_cpu
,MP_KDP
);
860 i_bit_clear(MP_KDP
, my_word
);
861 /* Ensure that the i386_kernel_state at the base of the
862 * current thread's stack (if any) is synchronized with the
863 * context at the moment of the interrupt, to facilitate
864 * access through the debugger.
867 sync_iss_to_iks(saved_state32(regs
));
870 #endif /* MACH_KDP */
871 if (i_bit(MP_TLB_FLUSH
, my_word
)) {
872 DBGLOG(cpu_handle
,my_cpu
,MP_TLB_FLUSH
);
873 i_bit_clear(MP_TLB_FLUSH
, my_word
);
874 pmap_update_interrupt();
875 } else if (i_bit(MP_AST
, my_word
)) {
876 DBGLOG(cpu_handle
,my_cpu
,MP_AST
);
877 i_bit_clear(MP_AST
, my_word
);
878 ast_check(cpu_to_processor(my_cpu
));
880 } else if (i_bit(MP_KDB
, my_word
)) {
882 i_bit_clear(MP_KDB
, my_word
);
883 current_cpu_datap()->cpu_kdb_is_slave
++;
885 current_cpu_datap()->cpu_kdb_is_slave
--;
886 #endif /* MACH_KDB */
887 } else if (i_bit(MP_RENDEZVOUS
, my_word
)) {
888 DBGLOG(cpu_handle
,my_cpu
,MP_RENDEZVOUS
);
889 i_bit_clear(MP_RENDEZVOUS
, my_word
);
890 mp_rendezvous_action();
891 } else if (i_bit(MP_CHUD
, my_word
)) {
892 DBGLOG(cpu_handle
,my_cpu
,MP_CHUD
);
893 i_bit_clear(MP_CHUD
, my_word
);
894 chudxnu_cpu_signal_handler();
898 mp_enable_preemption();
903 /* We want this to show up in backtraces, so mark it noinline
905 static int __attribute__((noinline
))
906 NMIInterruptHandler(void *regs
)
908 boolean_t state
= ml_set_interrupts_enabled(FALSE
);
909 sync_iss_to_iks_unconditionally(regs
);
911 (void) ml_set_interrupts_enabled(state
);
916 extern int max_lock_loops
;
917 #endif /* MP_DEBUG */
919 int trappedalready
= 0; /* (BRINGUP */
922 cpu_interrupt(int cpu
)
926 if(cpu_datap(cpu
)->cpu_signals
& 6) { /* (BRINGUP) */
927 kprintf("cpu_interrupt: sending enter debugger signal (%08X) to cpu %d\n", cpu_datap(cpu
)->cpu_signals
, cpu
);
930 if (smp_initialized
) {
933 // if(!trappedalready && (cpu_datap(cpu)->cpu_signals & 6)) { /* (BRINGUP) */
934 // if(kdb_cpu != cpu_number()) {
935 // trappedalready = 1;
936 // panic("cpu_interrupt: sending enter debugger signal (%08X) to cpu %d and I do not own debugger, owner = %08X\n",
937 // cpu_datap(cpu)->cpu_signals, cpu, kdb_cpu);
942 /* Wait for previous interrupt to be delivered... */
944 int pending_busy_count
= 0;
945 while (LAPIC_REG(ICR
) & LAPIC_ICR_DS_PENDING
) {
946 if (++pending_busy_count
> max_lock_loops
)
947 panic("cpus_interrupt() deadlock\n");
949 while (LAPIC_REG(ICR
) & LAPIC_ICR_DS_PENDING
) {
950 #endif /* MP_DEBUG */
954 state
= ml_set_interrupts_enabled(FALSE
);
956 cpu_to_lapic
[cpu
] << LAPIC_ICRD_DEST_SHIFT
;
958 LAPIC_VECTOR(INTERPROCESSOR
) | LAPIC_ICR_DM_FIXED
;
959 (void) ml_set_interrupts_enabled(state
);
965 * Send a true NMI via the local APIC to the specified CPU.
968 cpu_NMI_interrupt(int cpu
)
972 if (smp_initialized
) {
973 state
= ml_set_interrupts_enabled(FALSE
);
975 cpu_to_lapic
[cpu
] << LAPIC_ICRD_DEST_SHIFT
;
976 /* The vector is ignored in this case, the other CPU will come in on the
980 LAPIC_VECTOR(INTERPROCESSOR
) | LAPIC_ICR_DM_NMI
;
981 (void) ml_set_interrupts_enabled(state
);
987 i386_signal_cpu(int cpu
, mp_event_t event
, mp_sync_t mode
)
989 volatile int *signals
= &cpu_datap(cpu
)->cpu_signals
;
990 uint64_t tsc_timeout
;
993 if (!cpu_datap(cpu
)->cpu_running
)
996 if (event
== MP_TLB_FLUSH
)
997 KERNEL_DEBUG(0xef800020 | DBG_FUNC_START
, cpu
, 0, 0, 0, 0);
999 DBGLOG(cpu_signal
, cpu
, event
);
1001 i_bit_set(event
, signals
);
1005 tsc_timeout
= rdtsc64() + (1000*1000*1000);
1006 while (i_bit(event
, signals
) && rdtsc64() < tsc_timeout
) {
1009 if (i_bit(event
, signals
)) {
1010 DBG("i386_signal_cpu(%d, 0x%x, SYNC) timed out\n",
1015 if (event
== MP_TLB_FLUSH
)
1016 KERNEL_DEBUG(0xef800020 | DBG_FUNC_END
, cpu
, 0, 0, 0, 0);
1020 i386_signal_cpus(mp_event_t event
, mp_sync_t mode
)
1023 unsigned int my_cpu
= cpu_number();
1025 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1026 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1028 i386_signal_cpu(cpu
, event
, mode
);
1033 i386_active_cpus(void)
1036 unsigned int ncpus
= 0;
1038 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1039 if (cpu_datap(cpu
)->cpu_running
)
1046 * All-CPU rendezvous:
1047 * - CPUs are signalled,
1048 * - all execute the setup function (if specified),
1049 * - rendezvous (i.e. all cpus reach a barrier),
1050 * - all execute the action function (if specified),
1051 * - rendezvous again,
1052 * - execute the teardown function (if specified), and then
1055 * Note that the supplied external functions _must_ be reentrant and aware
1056 * that they are running in parallel and in an unknown lock context.
1060 mp_rendezvous_action(void)
1063 /* setup function */
1064 if (mp_rv_setup_func
!= NULL
)
1065 mp_rv_setup_func(mp_rv_func_arg
);
1066 /* spin on entry rendezvous */
1067 atomic_incl(&mp_rv_entry
, 1);
1068 while (mp_rv_entry
< mp_rv_ncpus
) {
1069 boolean_t intr
= ml_set_interrupts_enabled(FALSE
);
1070 /* poll for pesky tlb flushes */
1071 handle_pending_TLB_flushes();
1072 ml_set_interrupts_enabled(intr
);
1075 /* action function */
1076 if (mp_rv_action_func
!= NULL
)
1077 mp_rv_action_func(mp_rv_func_arg
);
1078 /* spin on exit rendezvous */
1079 atomic_incl(&mp_rv_exit
, 1);
1080 while (mp_rv_exit
< mp_rv_ncpus
)
1083 /* teardown function */
1084 if (mp_rv_teardown_func
!= NULL
)
1085 mp_rv_teardown_func(mp_rv_func_arg
);
1087 /* Bump completion count */
1088 atomic_incl(&mp_rv_complete
, 1);
1092 mp_rendezvous(void (*setup_func
)(void *),
1093 void (*action_func
)(void *),
1094 void (*teardown_func
)(void *),
1098 if (!smp_initialized
) {
1099 if (setup_func
!= NULL
)
1101 if (action_func
!= NULL
)
1103 if (teardown_func
!= NULL
)
1108 /* obtain rendezvous lock */
1109 simple_lock(&mp_rv_lock
);
1111 /* set static function pointers */
1112 mp_rv_setup_func
= setup_func
;
1113 mp_rv_action_func
= action_func
;
1114 mp_rv_teardown_func
= teardown_func
;
1115 mp_rv_func_arg
= arg
;
1122 * signal other processors, which will call mp_rendezvous_action()
1124 mp_rv_ncpus
= i386_active_cpus();
1125 i386_signal_cpus(MP_RENDEZVOUS
, ASYNC
);
1127 /* call executor function on this cpu */
1128 mp_rendezvous_action();
1131 * Spin for everyone to complete.
1132 * This is necessary to ensure that all processors have proceeded
1133 * from the exit barrier before we release the rendezvous structure.
1135 while (mp_rv_complete
< mp_rv_ncpus
) {
1140 simple_unlock(&mp_rv_lock
);
1144 mp_rendezvous_break_lock(void)
1146 simple_lock_init(&mp_rv_lock
, 0);
1150 setup_disable_intrs(__unused
void * param_not_used
)
1152 /* disable interrupts before the first barrier */
1153 boolean_t intr
= ml_set_interrupts_enabled(FALSE
);
1155 current_cpu_datap()->cpu_iflag
= intr
;
1156 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__
);
1160 teardown_restore_intrs(__unused
void * param_not_used
)
1162 /* restore interrupt flag following MTRR changes */
1163 ml_set_interrupts_enabled(current_cpu_datap()->cpu_iflag
);
1164 DBG("CPU%d: %s\n", get_cpu_number(), __FUNCTION__
);
1168 * A wrapper to mp_rendezvous() to call action_func() with interrupts disabled.
1169 * This is exported for use by kexts.
1172 mp_rendezvous_no_intrs(
1173 void (*action_func
)(void *),
1176 mp_rendezvous(setup_disable_intrs
,
1178 teardown_restore_intrs
,
1183 handle_pending_TLB_flushes(void)
1185 volatile int *my_word
= ¤t_cpu_datap()->cpu_signals
;
1187 if (i_bit(MP_TLB_FLUSH
, my_word
)) {
1188 DBGLOG(cpu_handle
, cpu_number(), MP_TLB_FLUSH
);
1189 i_bit_clear(MP_TLB_FLUSH
, my_word
);
1190 pmap_update_interrupt();
1195 volatile boolean_t mp_kdp_trap
= FALSE
;
1196 volatile long mp_kdp_ncpus
;
1197 boolean_t mp_kdp_state
;
1205 unsigned int my_cpu
= cpu_number();
1206 uint64_t tsc_timeout
;
1208 DBG("mp_kdp_enter()\n");
1211 * Here to enter the debugger.
1212 * In case of races, only one cpu is allowed to enter kdp after
1215 mp_kdp_state
= ml_set_interrupts_enabled(FALSE
);
1216 simple_lock(&mp_kdp_lock
);
1218 while (mp_kdp_trap
) {
1219 simple_unlock(&mp_kdp_lock
);
1220 DBG("mp_kdp_enter() race lost\n");
1222 simple_lock(&mp_kdp_lock
);
1224 mp_kdp_ncpus
= 1; /* self */
1226 simple_unlock(&mp_kdp_lock
);
1229 * Deliver a nudge to other cpus, counting how many
1231 DBG("mp_kdp_enter() signaling other processors\n");
1232 for (ncpus
= 1, cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1233 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1236 i386_signal_cpu(cpu
, MP_KDP
, ASYNC
);
1239 * Wait other processors to synchronize
1241 DBG("mp_kdp_enter() waiting for (%d) processors to suspend\n", ncpus
);
1243 tsc_timeout
= rdtsc64() + (ncpus
* 100 * 1000 * 1000);
1245 while (mp_kdp_ncpus
!= ncpus
&& rdtsc64() < tsc_timeout
) {
1247 * A TLB shootdown request may be pending... this would
1248 * result in the requesting processor waiting in
1249 * PMAP_UPDATE_TLBS() until this processor deals with it.
1250 * Process it, so it can now enter mp_kdp_wait()
1252 handle_pending_TLB_flushes();
1255 /* If we've timed out, and some processor(s) are still unresponsive,
1256 * interrupt them with an NMI via the local APIC.
1258 if (mp_kdp_ncpus
!= ncpus
) {
1259 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1260 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1262 if (cpu_signal_pending(cpu
, MP_KDP
))
1263 cpu_NMI_interrupt(cpu
);
1267 DBG("mp_kdp_enter() %d processors done %s\n",
1268 mp_kdp_ncpus
, (mp_kdp_ncpus
== ncpus
) ? "OK" : "timed out");
1270 postcode(MP_KDP_ENTER
);
1274 cpu_signal_pending(int cpu
, mp_event_t event
)
1276 volatile int *signals
= &cpu_datap(cpu
)->cpu_signals
;
1277 boolean_t retval
= FALSE
;
1279 if (i_bit(event
, signals
))
1287 DBG("mp_kdp_wait()\n");
1289 panic_io_port_read();
1291 atomic_incl(&mp_kdp_ncpus
, 1);
1292 while (mp_kdp_trap
) {
1294 * a TLB shootdown request may be pending... this would result in the requesting
1295 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1296 * Process it, so it can now enter mp_kdp_wait()
1298 handle_pending_TLB_flushes();
1302 atomic_decl((volatile long *)&mp_kdp_ncpus
, 1);
1303 DBG("mp_kdp_wait() done\n");
1309 DBG("mp_kdp_exit()\n");
1310 atomic_decl((volatile long *)&mp_kdp_ncpus
, 1);
1311 mp_kdp_trap
= FALSE
;
1312 __asm__
volatile("mfence");
1314 /* Wait other processors to stop spinning. XXX needs timeout */
1315 DBG("mp_kdp_exit() waiting for processors to resume\n");
1316 while (mp_kdp_ncpus
> 0) {
1318 * a TLB shootdown request may be pending... this would result in the requesting
1319 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1320 * Process it, so it can now enter mp_kdp_wait()
1322 handle_pending_TLB_flushes();
1326 DBG("mp_kdp_exit() done\n");
1327 (void) ml_set_interrupts_enabled(mp_kdp_state
);
1330 #endif /* MACH_KDP */
1335 __unused processor_t processor
)
1341 processor_t processor
)
1343 int cpu
= PROCESSOR_DATA(processor
, slot_num
);
1345 if (cpu
!= cpu_number()) {
1346 i386_signal_cpu(cpu
, MP_AST
, ASYNC
);
1352 * invoke kdb on slave processors
1358 unsigned int my_cpu
= cpu_number();
1361 uint64_t tsc_timeout
= 0;
1365 for (kdb_ncpus
= 1, cpu
= 0; cpu
< real_ncpus
; cpu
++) {
1366 if (cpu
== my_cpu
|| !cpu_datap(cpu
)->cpu_running
)
1369 i386_signal_cpu(cpu
, MP_KDB
, ASYNC
);
1371 DBG("remote_kdb() waiting for (%d) processors to suspend\n",kdb_ncpus
);
1373 tsc_timeout
= rdtsc64() + (kdb_ncpus
* 100 * 1000 * 1000);
1375 while (mp_kdb_ncpus
!= kdb_ncpus
&& rdtsc64() < tsc_timeout
) {
1377 * a TLB shootdown request may be pending... this would result in the requesting
1378 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1379 * Process it, so it can now enter mp_kdp_wait()
1381 handle_pending_TLB_flushes();
1385 DBG("mp_kdp_enter() %d processors done %s\n",
1386 mp_kdb_ncpus
, (mp_kdb_ncpus
== kdb_ncpus
) ? "OK" : "timed out");
1392 DBG("mp_kdb_wait()\n");
1394 panic_io_port_read();
1396 atomic_incl(&mp_kdb_ncpus
, 1);
1397 while (mp_kdb_trap
) {
1399 * a TLB shootdown request may be pending... this would result in the requesting
1400 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1401 * Process it, so it can now enter mp_kdp_wait()
1403 handle_pending_TLB_flushes();
1407 atomic_decl((volatile long *)&mp_kdb_ncpus
, 1);
1408 DBG("mp_kdb_wait() done\n");
1412 * Clear kdb interrupt
1416 clear_kdb_intr(void)
1418 mp_disable_preemption();
1419 i_bit_clear(MP_KDB
, ¤t_cpu_datap()->cpu_signals
);
1420 mp_enable_preemption();
1426 DBG("mp_kdb_exit()\n");
1427 atomic_decl((volatile long *)&mp_kdb_ncpus
, 1);
1428 mp_kdb_trap
= FALSE
;
1429 __asm__
volatile("mfence");
1431 while (mp_kdb_ncpus
> 0) {
1433 * a TLB shootdown request may be pending... this would result in the requesting
1434 * processor waiting in PMAP_UPDATE_TLBS() until this processor deals with it.
1435 * Process it, so it can now enter mp_kdp_wait()
1437 handle_pending_TLB_flushes();
1441 DBG("mp_kdb_exit() done\n");
1444 #endif /* MACH_KDB */
1447 * i386_init_slave() is called from pstart.
1448 * We're in the cpu's interrupt stack with interrupts disabled.
1449 * At this point we are in legacy mode. We need to switch on IA32e
1450 * if the mode is set to 64-bits.
1453 i386_init_slave(void)
1455 postcode(I386_INIT_SLAVE
);
1457 /* Ensure that caching and write-through are enabled */
1458 set_cr0(get_cr0() & ~(CR0_NW
|CR0_CD
));
1460 DBG("i386_init_slave() CPU%d: phys (%d) active.\n",
1461 get_cpu_number(), get_cpu_phys_number());
1463 assert(!ml_get_interrupts_enabled());
1464 if (cpu_mode_is64bit()) {
1465 cpu_IA32e_enable(current_cpu_datap());
1466 cpu_desc_load64(current_cpu_datap());
1467 fast_syscall_init64();
1469 fast_syscall_init();
1476 LAPIC_CPU_MAP_DUMP();
1486 cpu_init(); /* Sets cpu_running which starter cpu waits for */
1490 panic("i386_init_slave() returned from slave_main()");
1494 slave_machine_init(void)
1497 * Here in process context, but with interrupts disabled.
1499 DBG("slave_machine_init() CPU%d\n", get_cpu_number());
1503 cpu_machine_init(); /* Interrupts enabled hereafter */
1507 int cpu_number(void)
1509 return get_cpu_number();
1513 #include <ddb/db_output.h>
1515 #define TRAP_DEBUG 0 /* Must match interrupt.s and spl.s */
1520 struct mp_trap_hist_struct
{
1522 unsigned char data
[5];
1523 } trap_hist
[MTRAPS
], *cur_trap_hist
= trap_hist
,
1524 *max_trap_hist
= &trap_hist
[MTRAPS
];
1526 void db_trap_hist(void);
1546 for(i
=0;i
<MTRAPS
;i
++)
1547 if (trap_hist
[i
].type
== 1 || trap_hist
[i
].type
== 2) {
1549 (&trap_hist
[i
]>=cur_trap_hist
)?"*":" ",
1550 (trap_hist
[i
].type
== 1)?"SPL":"INT");
1552 db_printf(" %02x", trap_hist
[i
].data
[j
]);
1557 #endif /* TRAP_DEBUG */
1558 #endif /* MACH_KDB */