2 * Copyright (c) 1996, by Steve Passe
3 * Copyright (c) 2003, by Peter Wemm
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. The name of the developer may NOT be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_kstack_pages.h"
32 #include "opt_mp_watchdog.h"
33 #include "opt_sched.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
41 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/memrange.h>
46 #include <sys/mutex.h>
49 #include <sys/sched.h>
51 #include <sys/sysctl.h>
54 #include <vm/vm_param.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_extern.h>
59 #include <machine/apicreg.h>
60 #include <machine/clock.h>
61 #include <machine/cputypes.h>
62 #include <machine/cpufunc.h>
63 #include <machine/mca.h>
64 #include <machine/md_var.h>
65 #include <machine/mp_watchdog.h>
66 #include <machine/pcb.h>
67 #include <machine/psl.h>
68 #include <machine/smp.h>
69 #include <machine/specialreg.h>
70 #include <machine/tss.h>
72 #define WARMBOOT_TARGET 0
73 #define WARMBOOT_OFF (KERNBASE + 0x0467)
74 #define WARMBOOT_SEG (KERNBASE + 0x0469)
76 #define CMOS_REG (0x70)
77 #define CMOS_DATA (0x71)
78 #define BIOS_RESET (0x0f)
79 #define BIOS_WARM (0x0a)
81 /* lock region used by kernel profiling */
84 int mp_naps; /* # of Applications processors */
85 int boot_cpu_id = -1; /* designated BSP */
87 extern struct pcpu __pcpu[];
89 /* AP uses this during bootstrap. Do not staticize. */
93 /* Free these after use */
94 void *bootstacks[MAXCPU];
96 /* Temporary variables for init_secondary() */
97 char *doublefault_stack;
101 /* Hotwire a 0->4MB V==P mapping */
102 extern pt_entry_t *KPTphys;
104 /* SMP page table page */
105 extern pt_entry_t *SMPpt;
107 struct pcb stoppcbs[MAXCPU];
108 struct pcb **susppcbs = NULL;
110 /* Variables needed for SMP tlb shootdown. */
111 vm_offset_t smp_tlb_addr1;
112 vm_offset_t smp_tlb_addr2;
113 volatile int smp_tlb_wait;
115 extern inthand_t IDTVEC(fast_syscall), IDTVEC(fast_syscall32);
118 * Local data and functions.
121 static volatile cpumask_t ipi_nmi_pending;
123 /* used to hold the AP's until we are ready to release them */
124 static struct mtx ap_boot_mtx;
126 /* Set to 1 once we're ready to let the APs out of the pen. */
127 static volatile int aps_ready = 0;
130 * Store data from cpu_add() until later in the boot when we actually setup
137 int cpu_hyperthread:1;
138 } static cpu_info[MAX_APIC_ID + 1];
139 int cpu_apic_ids[MAXCPU];
140 int apic_cpuids[MAX_APIC_ID + 1];
142 /* Holds pending bitmap based IPIs per CPU */
143 static volatile u_int cpu_ipi_pending[MAXCPU];
145 static u_int boot_address;
146 static int cpu_logical; /* logical cpus per core */
147 static int cpu_cores; /* cores per package */
149 static void assign_cpu_ids(void);
150 static void set_interrupt_apic_ids(void);
151 static int start_all_aps(void);
152 static int start_ap(int apic_id);
153 static void release_aps(void *dummy);
155 static int hlt_logical_cpus;
156 static u_int hyperthreading_cpus; /* logical cpus sharing L1 cache */
157 static cpumask_t hyperthreading_cpus_mask;
158 static int hyperthreading_allowed = 1;
159 static struct sysctl_ctx_list logical_cpu_clist;
160 static u_int bootMP_size;
163 mem_range_AP_init(void)
165 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
166 mem_range_softc.mr_op->initAP(&mem_range_softc);
173 /* AMD processors do not support HTT. */
174 cpu_cores = (amd_feature2 & AMDID2_CMP) != 0 ?
175 (cpu_procinfo2 & AMDID_CMP_CORES) + 1 : 1;
180 * Round up to the next power of two, if necessary, and then
182 * Returns -1 if argument is zero.
188 return (fls(x << (1 - powerof2(x))) - 1);
201 /* Both zero and one here mean one logical processor per package. */
202 max_logical = (cpu_feature & CPUID_HTT) != 0 ?
203 (cpu_procinfo & CPUID_HTT_CORES) >> 16 : 1;
204 if (max_logical <= 1)
208 * Because of uniformity assumption we examine only
209 * those logical processors that belong to the same
210 * package as BSP. Further, we count number of
211 * logical processors that belong to the same core
212 * as BSP thus deducing number of threads per core.
214 cpuid_count(0x04, 0, p);
215 max_cores = ((p[0] >> 26) & 0x3f) + 1;
216 core_id_bits = mask_width(max_logical/max_cores);
217 if (core_id_bits < 0)
219 pkg_id_bits = core_id_bits + mask_width(max_cores);
221 for (id = 0; id <= MAX_APIC_ID; id++) {
222 /* Check logical CPU availability. */
223 if (!cpu_info[id].cpu_present || cpu_info[id].cpu_disabled)
225 /* Check if logical CPU has the same package ID. */
226 if ((id >> pkg_id_bits) != (boot_cpu_id >> pkg_id_bits))
229 /* Check if logical CPU has the same package and core IDs. */
230 if ((id >> core_id_bits) == (boot_cpu_id >> core_id_bits))
234 KASSERT(cpu_cores >= 1 && cpu_logical >= 1,
235 ("topo_probe_0x4 couldn't find BSP"));
237 cpu_cores /= cpu_logical;
238 hyperthreading_cpus = cpu_logical;
252 /* We only support three levels for now. */
253 for (i = 0; i < 3; i++) {
254 cpuid_count(0x0b, i, p);
256 /* Fall back if CPU leaf 11 doesn't really exist. */
257 if (i == 0 && p[1] == 0) {
263 logical = p[1] &= 0xffff;
264 type = (p[2] >> 8) & 0xff;
265 if (type == 0 || logical == 0)
268 * Because of uniformity assumption we examine only
269 * those logical processors that belong to the same
272 for (cnt = 0, x = 0; x <= MAX_APIC_ID; x++) {
273 if (!cpu_info[x].cpu_present ||
274 cpu_info[x].cpu_disabled)
276 if (x >> bits == boot_cpu_id >> bits)
279 if (type == CPUID_TYPE_SMT)
281 else if (type == CPUID_TYPE_CORE)
284 if (cpu_logical == 0)
286 cpu_cores /= cpu_logical;
290 * Both topology discovery code and code that consumes topology
291 * information assume top-down uniformity of the topology.
292 * That is, all physical packages must be identical and each
293 * core in a package must have the same number of threads.
294 * Topology information is queried only on BSP, on which this
295 * code runs and for which it can query CPUID information.
296 * Then topology is extrapolated on all packages using the
297 * uniformity assumption.
302 static int cpu_topo_probed = 0;
307 logical_cpus_mask = 0;
309 cpu_cores = cpu_logical = 1;
310 else if (cpu_vendor_id == CPU_VENDOR_AMD)
312 else if (cpu_vendor_id == CPU_VENDOR_INTEL) {
314 * See Intel(R) 64 Architecture Processor
315 * Topology Enumeration article for details.
317 * Note that 0x1 <= cpu_high < 4 case should be
318 * compatible with topo_probe_0x4() logic when
319 * CPUID.1:EBX[23:16] > 0 (cpu_cores will be 1)
320 * or it should trigger the fallback otherwise.
324 else if (cpu_high >= 0x1)
329 * Fallback: assume each logical CPU is in separate
330 * physical package. That is, no multi-core, no SMT.
332 if (cpu_cores == 0 || cpu_logical == 0)
333 cpu_cores = cpu_logical = 1;
343 * Determine whether any threading flags are
347 if (cpu_logical > 1 && hyperthreading_cpus)
348 cg_flags = CG_FLAG_HTT;
349 else if (cpu_logical > 1)
350 cg_flags = CG_FLAG_SMT;
353 if (mp_ncpus % (cpu_cores * cpu_logical) != 0) {
354 printf("WARNING: Non-uniform processors.\n");
355 printf("WARNING: Using suboptimal topology.\n");
356 return (smp_topo_none());
359 * No multi-core or hyper-threaded.
361 if (cpu_logical * cpu_cores == 1)
362 return (smp_topo_none());
364 * Only HTT no multi-core.
366 if (cpu_logical > 1 && cpu_cores == 1)
367 return (smp_topo_1level(CG_SHARE_L1, cpu_logical, cg_flags));
369 * Only multi-core no HTT.
371 if (cpu_cores > 1 && cpu_logical == 1)
372 return (smp_topo_1level(CG_SHARE_L2, cpu_cores, cg_flags));
374 * Both HTT and multi-core.
376 return (smp_topo_2level(CG_SHARE_L2, cpu_cores,
377 CG_SHARE_L1, cpu_logical, cg_flags));
381 * Calculate usable address in base memory for AP trampoline code.
384 mp_bootaddress(u_int basemem)
387 bootMP_size = mptramp_end - mptramp_start;
388 boot_address = trunc_page(basemem * 1024); /* round down to 4k boundary */
389 if (((basemem * 1024) - boot_address) < bootMP_size)
390 boot_address -= PAGE_SIZE; /* not enough, lower by 4k */
391 /* 3 levels of page table pages */
392 mptramp_pagetables = boot_address - (PAGE_SIZE * 3);
394 return mptramp_pagetables;
398 cpu_add(u_int apic_id, char boot_cpu)
401 if (apic_id > MAX_APIC_ID) {
402 panic("SMP: APIC ID %d too high", apic_id);
405 KASSERT(cpu_info[apic_id].cpu_present == 0, ("CPU %d added twice",
407 cpu_info[apic_id].cpu_present = 1;
409 KASSERT(boot_cpu_id == -1,
410 ("CPU %d claims to be BSP, but CPU %d already is", apic_id,
412 boot_cpu_id = apic_id;
413 cpu_info[apic_id].cpu_bsp = 1;
415 if (mp_ncpus < MAXCPU) {
417 mp_maxid = mp_ncpus -1;
420 printf("SMP: Added CPU %d (%s)\n", apic_id, boot_cpu ? "BSP" :
425 cpu_mp_setmaxid(void)
429 * mp_maxid should be already set by calls to cpu_add().
430 * Just sanity check its value here.
433 KASSERT(mp_maxid == 0,
434 ("%s: mp_ncpus is zero, but mp_maxid is not", __func__));
435 else if (mp_ncpus == 1)
438 KASSERT(mp_maxid >= mp_ncpus - 1,
439 ("%s: counters out of sync: max %d, count %d", __func__,
440 mp_maxid, mp_ncpus));
448 * Always record BSP in CPU map so that the mbuf init code works
454 * No CPUs were found, so this must be a UP system. Setup
455 * the variables to represent a system with a single CPU
462 /* At least one CPU was found. */
465 * One CPU was found, so this must be a UP system with
472 /* At least two CPUs were found. */
477 * Initialize the IPI handlers and start up the AP's.
484 /* Initialize the logical ID to APIC ID table. */
485 for (i = 0; i < MAXCPU; i++) {
486 cpu_apic_ids[i] = -1;
487 cpu_ipi_pending[i] = 0;
490 /* Install an inter-CPU IPI for TLB invalidation */
491 setidt(IPI_INVLTLB, IDTVEC(invltlb), SDT_SYSIGT, SEL_KPL, 0);
492 setidt(IPI_INVLPG, IDTVEC(invlpg), SDT_SYSIGT, SEL_KPL, 0);
493 setidt(IPI_INVLRNG, IDTVEC(invlrng), SDT_SYSIGT, SEL_KPL, 0);
495 /* Install an inter-CPU IPI for cache invalidation. */
496 setidt(IPI_INVLCACHE, IDTVEC(invlcache), SDT_SYSIGT, SEL_KPL, 0);
498 /* Install an inter-CPU IPI for all-CPU rendezvous */
499 setidt(IPI_RENDEZVOUS, IDTVEC(rendezvous), SDT_SYSIGT, SEL_KPL, 0);
501 /* Install generic inter-CPU IPI handler */
502 setidt(IPI_BITMAP_VECTOR, IDTVEC(ipi_intr_bitmap_handler),
503 SDT_SYSIGT, SEL_KPL, 0);
505 /* Install an inter-CPU IPI for CPU stop/restart */
506 setidt(IPI_STOP, IDTVEC(cpustop), SDT_SYSIGT, SEL_KPL, 0);
508 /* Install an inter-CPU IPI for CPU suspend/resume */
509 setidt(IPI_SUSPEND, IDTVEC(cpususpend), SDT_SYSIGT, SEL_KPL, 0);
511 /* Set boot_cpu_id if needed. */
512 if (boot_cpu_id == -1) {
513 boot_cpu_id = PCPU_GET(apic_id);
514 cpu_info[boot_cpu_id].cpu_bsp = 1;
516 KASSERT(boot_cpu_id == PCPU_GET(apic_id),
517 ("BSP's APIC ID doesn't match boot_cpu_id"));
519 /* Probe logical/physical core configuration. */
524 /* Start each Application Processor */
527 set_interrupt_apic_ids();
532 * Print various information about the SMP system hardware and setup.
535 cpu_mp_announce(void)
537 const char *hyperthread;
540 printf("FreeBSD/SMP: %d package(s) x %d core(s)",
541 mp_ncpus / (cpu_cores * cpu_logical), cpu_cores);
542 if (hyperthreading_cpus > 1)
543 printf(" x %d HTT threads", cpu_logical);
544 else if (cpu_logical > 1)
545 printf(" x %d SMT threads", cpu_logical);
548 /* List active CPUs first. */
549 printf(" cpu0 (BSP): APIC ID: %2d\n", boot_cpu_id);
550 for (i = 1; i < mp_ncpus; i++) {
551 if (cpu_info[cpu_apic_ids[i]].cpu_hyperthread)
555 printf(" cpu%d (AP%s): APIC ID: %2d\n", i, hyperthread,
559 /* List disabled CPUs last. */
560 for (i = 0; i <= MAX_APIC_ID; i++) {
561 if (!cpu_info[i].cpu_present || !cpu_info[i].cpu_disabled)
563 if (cpu_info[i].cpu_hyperthread)
567 printf(" cpu (AP%s): APIC ID: %2d (disabled)\n", hyperthread,
573 * AP CPU's call this to initialize themselves.
581 int cpu, gsel_tss, x;
582 struct region_descriptor ap_gdt;
584 /* Set by the startup code for us to use */
588 common_tss[cpu] = common_tss[0];
589 common_tss[cpu].tss_rsp0 = 0; /* not used until after switch */
590 common_tss[cpu].tss_iobase = sizeof(struct amd64tss) +
592 common_tss[cpu].tss_ist1 = (long)&doublefault_stack[PAGE_SIZE];
594 /* The NMI stack runs on IST2. */
595 np = ((struct nmi_pcpu *) &nmi_stack[PAGE_SIZE]) - 1;
596 common_tss[cpu].tss_ist2 = (long) np;
598 /* Prepare private GDT */
599 gdt_segs[GPROC0_SEL].ssd_base = (long) &common_tss[cpu];
600 for (x = 0; x < NGDT; x++) {
601 if (x != GPROC0_SEL && x != (GPROC0_SEL + 1) &&
602 x != GUSERLDT_SEL && x != (GUSERLDT_SEL + 1))
603 ssdtosd(&gdt_segs[x], &gdt[NGDT * cpu + x]);
605 ssdtosyssd(&gdt_segs[GPROC0_SEL],
606 (struct system_segment_descriptor *)&gdt[NGDT * cpu + GPROC0_SEL]);
607 ap_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
608 ap_gdt.rd_base = (long) &gdt[NGDT * cpu];
609 lgdt(&ap_gdt); /* does magic intra-segment return */
611 /* Get per-cpu data */
614 /* prime data page for it to use */
615 pcpu_init(pc, cpu, sizeof(struct pcpu));
616 dpcpu_init(dpcpu, cpu);
617 pc->pc_apic_id = cpu_apic_ids[cpu];
618 pc->pc_prvspace = pc;
619 pc->pc_curthread = 0;
620 pc->pc_tssp = &common_tss[cpu];
621 pc->pc_commontssp = &common_tss[cpu];
623 pc->pc_tss = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
625 pc->pc_fs32p = &gdt[NGDT * cpu + GUFS32_SEL];
626 pc->pc_gs32p = &gdt[NGDT * cpu + GUGS32_SEL];
627 pc->pc_ldt = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
630 /* Save the per-cpu pointer for use by the NMI handler. */
631 np->np_pcpu = (register_t) pc;
633 wrmsr(MSR_FSBASE, 0); /* User value */
634 wrmsr(MSR_GSBASE, (u_int64_t)pc);
635 wrmsr(MSR_KGSBASE, (u_int64_t)pc); /* XXX User value while we're in the kernel */
639 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
643 * Set to a known state:
644 * Set by mpboot.s: CR0_PG, CR0_PE
645 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
648 cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
651 /* Set up the fast syscall stuff */
652 msr = rdmsr(MSR_EFER) | EFER_SCE;
653 wrmsr(MSR_EFER, msr);
654 wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall));
655 wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32));
656 msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) |
657 ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48);
658 wrmsr(MSR_STAR, msr);
659 wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D);
661 /* Disable local APIC just to be sure. */
664 /* signal our startup to the BSP. */
667 /* Spin until the BSP releases the AP's. */
671 /* Initialize the PAT MSR. */
674 /* set up CPU registers and state */
677 /* set up SSE/NX registers */
680 /* set up FPU state on the AP */
683 /* A quick check from sanity claus */
684 if (PCPU_GET(apic_id) != lapic_id()) {
685 printf("SMP: cpuid = %d\n", PCPU_GET(cpuid));
686 printf("SMP: actual apic_id = %d\n", lapic_id());
687 printf("SMP: correct apic_id = %d\n", PCPU_GET(apic_id));
688 panic("cpuid mismatch! boom!!");
691 /* Initialize curthread. */
692 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
693 PCPU_SET(curthread, PCPU_GET(idlethread));
697 mtx_lock_spin(&ap_boot_mtx);
699 /* Init local apic for irq's */
702 /* Set memory range attributes for this CPU to match the BSP */
707 CTR1(KTR_SMP, "SMP: AP CPU #%d Launched", PCPU_GET(cpuid));
708 printf("SMP: AP CPU #%d Launched!\n", PCPU_GET(cpuid));
710 /* Determine if we are a logical CPU. */
711 /* XXX Calculation depends on cpu_logical being a power of 2, e.g. 2 */
712 if (cpu_logical > 1 && PCPU_GET(apic_id) % cpu_logical != 0)
713 logical_cpus_mask |= PCPU_GET(cpumask);
715 /* Determine if we are a hyperthread. */
716 if (hyperthreading_cpus > 1 &&
717 PCPU_GET(apic_id) % hyperthreading_cpus != 0)
718 hyperthreading_cpus_mask |= PCPU_GET(cpumask);
720 /* Build our map of 'other' CPUs. */
721 PCPU_SET(other_cpus, all_cpus & ~PCPU_GET(cpumask));
726 if (smp_cpus == mp_ncpus) {
727 /* enable IPI's, tlb shootdown, freezes etc */
728 atomic_store_rel_int(&smp_started, 1);
729 smp_active = 1; /* historic */
733 * Enable global pages TLB extension
734 * This also implicitly flushes the TLB
737 load_cr4(rcr4() | CR4_PGE);
741 mtx_unlock_spin(&ap_boot_mtx);
743 /* wait until all the AP's are up */
744 while (smp_started == 0)
749 panic("scheduler returned us to %s", __func__);
753 /*******************************************************************
754 * local functions and data
758 * We tell the I/O APIC code about all the CPUs we want to receive
759 * interrupts. If we don't want certain CPUs to receive IRQs we
760 * can simply not tell the I/O APIC code about them in this function.
761 * We also do not tell it about the BSP since it tells itself about
762 * the BSP internally to work with UP kernels and on UP machines.
765 set_interrupt_apic_ids(void)
769 for (i = 0; i < MAXCPU; i++) {
770 apic_id = cpu_apic_ids[i];
773 if (cpu_info[apic_id].cpu_bsp)
775 if (cpu_info[apic_id].cpu_disabled)
778 /* Don't let hyperthreads service interrupts. */
779 if (hyperthreading_cpus > 1 &&
780 apic_id % hyperthreading_cpus != 0)
788 * Assign logical CPU IDs to local APICs.
795 TUNABLE_INT_FETCH("machdep.hyperthreading_allowed",
796 &hyperthreading_allowed);
798 /* Check for explicitly disabled CPUs. */
799 for (i = 0; i <= MAX_APIC_ID; i++) {
800 if (!cpu_info[i].cpu_present || cpu_info[i].cpu_bsp)
803 if (hyperthreading_cpus > 1 && i % hyperthreading_cpus != 0) {
804 cpu_info[i].cpu_hyperthread = 1;
805 #if defined(SCHED_ULE)
807 * Don't use HT CPU if it has been disabled by a
810 if (hyperthreading_allowed == 0) {
811 cpu_info[i].cpu_disabled = 1;
817 /* Don't use this CPU if it has been disabled by a tunable. */
818 if (resource_disabled("lapic", i)) {
819 cpu_info[i].cpu_disabled = 1;
825 * Assign CPU IDs to local APIC IDs and disable any CPUs
826 * beyond MAXCPU. CPU 0 is always assigned to the BSP.
828 * To minimize confusion for userland, we attempt to number
829 * CPUs such that all threads and cores in a package are
830 * grouped together. For now we assume that the BSP is always
831 * the first thread in a package and just start adding APs
832 * starting with the BSP's APIC ID.
835 cpu_apic_ids[0] = boot_cpu_id;
836 apic_cpuids[boot_cpu_id] = 0;
837 for (i = boot_cpu_id + 1; i != boot_cpu_id;
838 i == MAX_APIC_ID ? i = 0 : i++) {
839 if (!cpu_info[i].cpu_present || cpu_info[i].cpu_bsp ||
840 cpu_info[i].cpu_disabled)
843 if (mp_ncpus < MAXCPU) {
844 cpu_apic_ids[mp_ncpus] = i;
845 apic_cpuids[i] = mp_ncpus;
848 cpu_info[i].cpu_disabled = 1;
850 KASSERT(mp_maxid >= mp_ncpus - 1,
851 ("%s: counters out of sync: max %d, count %d", __func__, mp_maxid,
856 * start each AP in our list
861 vm_offset_t va = boot_address + KERNBASE;
862 u_int64_t *pt4, *pt3, *pt2;
863 u_int32_t mpbioswarmvec;
867 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
869 /* install the AP 1st level boot code */
870 pmap_kenter(va, boot_address);
871 pmap_invalidate_page(kernel_pmap, va);
872 bcopy(mptramp_start, (void *)va, bootMP_size);
874 /* Locate the page tables, they'll be below the trampoline */
875 pt4 = (u_int64_t *)(uintptr_t)(mptramp_pagetables + KERNBASE);
876 pt3 = pt4 + (PAGE_SIZE) / sizeof(u_int64_t);
877 pt2 = pt3 + (PAGE_SIZE) / sizeof(u_int64_t);
879 /* Create the initial 1GB replicated page tables */
880 for (i = 0; i < 512; i++) {
881 /* Each slot of the level 4 pages points to the same level 3 page */
882 pt4[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + PAGE_SIZE);
883 pt4[i] |= PG_V | PG_RW | PG_U;
885 /* Each slot of the level 3 pages points to the same level 2 page */
886 pt3[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + (2 * PAGE_SIZE));
887 pt3[i] |= PG_V | PG_RW | PG_U;
889 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
890 pt2[i] = i * (2 * 1024 * 1024);
891 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
894 /* save the current value of the warm-start vector */
895 mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF);
896 outb(CMOS_REG, BIOS_RESET);
897 mpbiosreason = inb(CMOS_DATA);
899 /* setup a vector to our boot code */
900 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
901 *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4);
902 outb(CMOS_REG, BIOS_RESET);
903 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
906 for (cpu = 1; cpu < mp_ncpus; cpu++) {
907 apic_id = cpu_apic_ids[cpu];
909 /* allocate and set up an idle stack data page */
910 bootstacks[cpu] = (void *)kmem_alloc(kernel_map, KSTACK_PAGES * PAGE_SIZE);
911 doublefault_stack = (char *)kmem_alloc(kernel_map, PAGE_SIZE);
912 nmi_stack = (char *)kmem_alloc(kernel_map, PAGE_SIZE);
913 dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE);
915 bootSTK = (char *)bootstacks[cpu] + KSTACK_PAGES * PAGE_SIZE - 8;
918 /* attempt to start the Application Processor */
919 if (!start_ap(apic_id)) {
920 /* restore the warmstart vector */
921 *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
922 panic("AP #%d (PHY# %d) failed!", cpu, apic_id);
925 all_cpus |= (1 << cpu); /* record AP in CPU map */
928 /* build our map of 'other' CPUs */
929 PCPU_SET(other_cpus, all_cpus & ~PCPU_GET(cpumask));
931 /* restore the warmstart vector */
932 *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
934 outb(CMOS_REG, BIOS_RESET);
935 outb(CMOS_DATA, mpbiosreason);
937 /* number of APs actually started */
943 * This function starts the AP (application processor) identified
944 * by the APIC ID 'physicalCpu'. It does quite a "song and dance"
945 * to accomplish this. This is necessary because of the nuances
946 * of the different hardware we might encounter. It isn't pretty,
947 * but it seems to work.
950 start_ap(int apic_id)
955 /* calculate the vector */
956 vector = (boot_address >> 12) & 0xff;
958 /* used as a watchpoint to signal AP startup */
962 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting
963 * and running the target CPU. OR this INIT IPI might be latched (P5
964 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
968 /* do an INIT IPI: assert RESET */
969 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
970 APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, apic_id);
972 /* wait for pending status end */
975 /* do an INIT IPI: deassert RESET */
976 lapic_ipi_raw(APIC_DEST_ALLESELF | APIC_TRIGMOD_LEVEL |
977 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, 0);
979 /* wait for pending status end */
980 DELAY(10000); /* wait ~10mS */
984 * next we do a STARTUP IPI: the previous INIT IPI might still be
985 * latched, (P5 bug) this 1st STARTUP would then terminate
986 * immediately, and the previously started INIT IPI would continue. OR
987 * the previous INIT IPI has already run. and this STARTUP IPI will
988 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
992 /* do a STARTUP IPI */
993 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
994 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
997 DELAY(200); /* wait ~200uS */
1000 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
1001 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
1002 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
1003 * recognized after hardware RESET or INIT IPI.
1006 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
1007 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
1010 DELAY(200); /* wait ~200uS */
1012 /* Wait up to 5 seconds for it to start. */
1013 for (ms = 0; ms < 5000; ms++) {
1015 return 1; /* return SUCCESS */
1018 return 0; /* return FAILURE */
1022 * Flush the TLB on all other CPU's
1025 smp_tlb_shootdown(u_int vector, vm_offset_t addr1, vm_offset_t addr2)
1029 ncpu = mp_ncpus - 1; /* does not shootdown self */
1031 return; /* no other cpus */
1032 if (!(read_rflags() & PSL_I))
1033 panic("%s: interrupts disabled", __func__);
1034 mtx_lock_spin(&smp_ipi_mtx);
1035 smp_tlb_addr1 = addr1;
1036 smp_tlb_addr2 = addr2;
1037 atomic_store_rel_int(&smp_tlb_wait, 0);
1038 ipi_all_but_self(vector);
1039 while (smp_tlb_wait < ncpu)
1041 mtx_unlock_spin(&smp_ipi_mtx);
1045 smp_targeted_tlb_shootdown(cpumask_t mask, u_int vector, vm_offset_t addr1, vm_offset_t addr2)
1047 int ncpu, othercpus;
1049 othercpus = mp_ncpus - 1;
1050 if (mask == (cpumask_t)-1) {
1055 mask &= ~PCPU_GET(cpumask);
1058 ncpu = bitcount32(mask);
1059 if (ncpu > othercpus) {
1060 /* XXX this should be a panic offence */
1061 printf("SMP: tlb shootdown to %d other cpus (only have %d)\n",
1065 /* XXX should be a panic, implied by mask == 0 above */
1069 if (!(read_rflags() & PSL_I))
1070 panic("%s: interrupts disabled", __func__);
1071 mtx_lock_spin(&smp_ipi_mtx);
1072 smp_tlb_addr1 = addr1;
1073 smp_tlb_addr2 = addr2;
1074 atomic_store_rel_int(&smp_tlb_wait, 0);
1075 if (mask == (cpumask_t)-1)
1076 ipi_all_but_self(vector);
1078 ipi_selected(mask, vector);
1079 while (smp_tlb_wait < ncpu)
1081 mtx_unlock_spin(&smp_ipi_mtx);
1085 * Send an IPI to specified CPU handling the bitmap logic.
1088 ipi_send_cpu(int cpu, u_int ipi)
1090 u_int bitmap, old_pending, new_pending;
1092 KASSERT(cpu_apic_ids[cpu] != -1, ("IPI to non-existent CPU %d", cpu));
1094 if (IPI_IS_BITMAPED(ipi)) {
1096 ipi = IPI_BITMAP_VECTOR;
1098 old_pending = cpu_ipi_pending[cpu];
1099 new_pending = old_pending | bitmap;
1100 } while (!atomic_cmpset_int(&cpu_ipi_pending[cpu],
1101 old_pending, new_pending));
1105 lapic_ipi_vectored(ipi, cpu_apic_ids[cpu]);
1109 smp_cache_flush(void)
1113 smp_tlb_shootdown(IPI_INVLCACHE, 0, 0);
1121 smp_tlb_shootdown(IPI_INVLTLB, 0, 0);
1126 smp_invlpg(vm_offset_t addr)
1130 smp_tlb_shootdown(IPI_INVLPG, addr, 0);
1134 smp_invlpg_range(vm_offset_t addr1, vm_offset_t addr2)
1138 smp_tlb_shootdown(IPI_INVLRNG, addr1, addr2);
1143 smp_masked_invltlb(cpumask_t mask)
1147 smp_targeted_tlb_shootdown(mask, IPI_INVLTLB, 0, 0);
1152 smp_masked_invlpg(cpumask_t mask, vm_offset_t addr)
1156 smp_targeted_tlb_shootdown(mask, IPI_INVLPG, addr, 0);
1161 smp_masked_invlpg_range(cpumask_t mask, vm_offset_t addr1, vm_offset_t addr2)
1165 smp_targeted_tlb_shootdown(mask, IPI_INVLRNG, addr1, addr2);
1170 ipi_bitmap_handler(struct trapframe frame)
1172 int cpu = PCPU_GET(cpuid);
1175 ipi_bitmap = atomic_readandclear_int(&cpu_ipi_pending[cpu]);
1177 if (ipi_bitmap & (1 << IPI_PREEMPT))
1178 sched_preempt(curthread);
1180 /* Nothing to do for AST */
1182 if (ipi_bitmap & (1 << IPI_HARDCLOCK))
1183 hardclockintr(&frame);
1185 if (ipi_bitmap & (1 << IPI_STATCLOCK))
1186 statclockintr(&frame);
1188 if (ipi_bitmap & (1 << IPI_PROFCLOCK))
1189 profclockintr(&frame);
1193 * send an IPI to a set of cpus.
1196 ipi_selected(cpumask_t cpus, u_int ipi)
1201 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1202 * of help in order to understand what is the source.
1203 * Set the mask of receiving CPUs for this purpose.
1205 if (ipi == IPI_STOP_HARD)
1206 atomic_set_int(&ipi_nmi_pending, cpus);
1208 CTR3(KTR_SMP, "%s: cpus: %x ipi: %x", __func__, cpus, ipi);
1209 while ((cpu = ffs(cpus)) != 0) {
1211 cpus &= ~(1 << cpu);
1212 ipi_send_cpu(cpu, ipi);
1217 * send an IPI to a specific CPU.
1220 ipi_cpu(int cpu, u_int ipi)
1224 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1225 * of help in order to understand what is the source.
1226 * Set the mask of receiving CPUs for this purpose.
1228 if (ipi == IPI_STOP_HARD)
1229 atomic_set_int(&ipi_nmi_pending, 1 << cpu);
1231 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu, ipi);
1232 ipi_send_cpu(cpu, ipi);
1236 * send an IPI to all CPUs EXCEPT myself
1239 ipi_all_but_self(u_int ipi)
1242 if (IPI_IS_BITMAPED(ipi)) {
1243 ipi_selected(PCPU_GET(other_cpus), ipi);
1248 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1249 * of help in order to understand what is the source.
1250 * Set the mask of receiving CPUs for this purpose.
1252 if (ipi == IPI_STOP_HARD)
1253 atomic_set_int(&ipi_nmi_pending, PCPU_GET(other_cpus));
1255 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
1256 lapic_ipi_vectored(ipi, APIC_IPI_DEST_OTHERS);
1265 * As long as there is not a simple way to know about a NMI's
1266 * source, if the bitmask for the current CPU is present in
1267 * the global pending bitword an IPI_STOP_HARD has been issued
1268 * and should be handled.
1270 cpumask = PCPU_GET(cpumask);
1271 if ((ipi_nmi_pending & cpumask) == 0)
1274 atomic_clear_int(&ipi_nmi_pending, cpumask);
1280 * Handle an IPI_STOP by saving our current context and spinning until we
1284 cpustop_handler(void)
1289 cpu = PCPU_GET(cpuid);
1290 cpumask = PCPU_GET(cpumask);
1292 savectx(&stoppcbs[cpu]);
1294 /* Indicate that we are stopped */
1295 atomic_set_int(&stopped_cpus, cpumask);
1297 /* Wait for restart */
1298 while (!(started_cpus & cpumask))
1301 atomic_clear_int(&started_cpus, cpumask);
1302 atomic_clear_int(&stopped_cpus, cpumask);
1304 if (cpu == 0 && cpustop_restartfunc != NULL) {
1305 cpustop_restartfunc();
1306 cpustop_restartfunc = NULL;
1311 * Handle an IPI_SUSPEND by saving our current context and spinning until we
1315 cpususpend_handler(void)
1321 cpu = PCPU_GET(cpuid);
1322 cpumask = PCPU_GET(cpumask);
1324 rf = intr_disable();
1327 if (savectx(susppcbs[cpu])) {
1329 atomic_set_int(&stopped_cpus, cpumask);
1332 PCPU_SET(switchtime, 0);
1333 PCPU_SET(switchticks, ticks);
1336 /* Wait for resume */
1337 while (!(started_cpus & cpumask))
1340 atomic_clear_int(&started_cpus, cpumask);
1341 atomic_clear_int(&stopped_cpus, cpumask);
1343 /* Restore CR3 and enable interrupts */
1351 * This is called once the rest of the system is up and running and we're
1352 * ready to let the AP's out of the pen.
1355 release_aps(void *dummy __unused)
1360 atomic_store_rel_int(&aps_ready, 1);
1361 while (smp_started == 0)
1364 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
1367 sysctl_hlt_cpus(SYSCTL_HANDLER_ARGS)
1372 mask = hlt_cpus_mask;
1373 error = sysctl_handle_int(oidp, &mask, 0, req);
1374 if (error || !req->newptr)
1377 if (logical_cpus_mask != 0 &&
1378 (mask & logical_cpus_mask) == logical_cpus_mask)
1379 hlt_logical_cpus = 1;
1381 hlt_logical_cpus = 0;
1383 if (! hyperthreading_allowed)
1384 mask |= hyperthreading_cpus_mask;
1386 if ((mask & all_cpus) == all_cpus)
1388 hlt_cpus_mask = mask;
1391 SYSCTL_PROC(_machdep, OID_AUTO, hlt_cpus, CTLTYPE_INT|CTLFLAG_RW,
1392 0, 0, sysctl_hlt_cpus, "IU",
1393 "Bitmap of CPUs to halt. 101 (binary) will halt CPUs 0 and 2.");
1396 sysctl_hlt_logical_cpus(SYSCTL_HANDLER_ARGS)
1400 disable = hlt_logical_cpus;
1401 error = sysctl_handle_int(oidp, &disable, 0, req);
1402 if (error || !req->newptr)
1406 hlt_cpus_mask |= logical_cpus_mask;
1408 hlt_cpus_mask &= ~logical_cpus_mask;
1410 if (! hyperthreading_allowed)
1411 hlt_cpus_mask |= hyperthreading_cpus_mask;
1413 if ((hlt_cpus_mask & all_cpus) == all_cpus)
1414 hlt_cpus_mask &= ~(1<<0);
1416 hlt_logical_cpus = disable;
1421 sysctl_hyperthreading_allowed(SYSCTL_HANDLER_ARGS)
1425 allowed = hyperthreading_allowed;
1426 error = sysctl_handle_int(oidp, &allowed, 0, req);
1427 if (error || !req->newptr)
1432 * SCHED_ULE doesn't allow enabling/disabling HT cores at
1435 if (allowed != hyperthreading_allowed)
1441 hlt_cpus_mask &= ~hyperthreading_cpus_mask;
1443 hlt_cpus_mask |= hyperthreading_cpus_mask;
1445 if (logical_cpus_mask != 0 &&
1446 (hlt_cpus_mask & logical_cpus_mask) == logical_cpus_mask)
1447 hlt_logical_cpus = 1;
1449 hlt_logical_cpus = 0;
1451 if ((hlt_cpus_mask & all_cpus) == all_cpus)
1452 hlt_cpus_mask &= ~(1<<0);
1454 hyperthreading_allowed = allowed;
1459 cpu_hlt_setup(void *dummy __unused)
1462 if (logical_cpus_mask != 0) {
1463 TUNABLE_INT_FETCH("machdep.hlt_logical_cpus",
1465 sysctl_ctx_init(&logical_cpu_clist);
1466 SYSCTL_ADD_PROC(&logical_cpu_clist,
1467 SYSCTL_STATIC_CHILDREN(_machdep), OID_AUTO,
1468 "hlt_logical_cpus", CTLTYPE_INT|CTLFLAG_RW, 0, 0,
1469 sysctl_hlt_logical_cpus, "IU", "");
1470 SYSCTL_ADD_UINT(&logical_cpu_clist,
1471 SYSCTL_STATIC_CHILDREN(_machdep), OID_AUTO,
1472 "logical_cpus_mask", CTLTYPE_INT|CTLFLAG_RD,
1473 &logical_cpus_mask, 0, "");
1475 if (hlt_logical_cpus)
1476 hlt_cpus_mask |= logical_cpus_mask;
1479 * If necessary for security purposes, force
1480 * hyperthreading off, regardless of the value
1481 * of hlt_logical_cpus.
1483 if (hyperthreading_cpus_mask) {
1484 SYSCTL_ADD_PROC(&logical_cpu_clist,
1485 SYSCTL_STATIC_CHILDREN(_machdep), OID_AUTO,
1486 "hyperthreading_allowed", CTLTYPE_INT|CTLFLAG_RW,
1487 0, 0, sysctl_hyperthreading_allowed, "IU", "");
1488 if (! hyperthreading_allowed)
1489 hlt_cpus_mask |= hyperthreading_cpus_mask;
1493 SYSINIT(cpu_hlt, SI_SUB_SMP, SI_ORDER_ANY, cpu_hlt_setup, NULL);
1496 mp_grab_cpu_hlt(void)
1504 mask = PCPU_GET(cpumask);
1506 cpuid = PCPU_GET(cpuid);
1511 while (mask & hlt_cpus_mask) {
1513 __asm __volatile("sti; hlt" : : : "memory");