2 * Copyright (c) 1996, by Steve Passe
3 * Copyright (c) 2008, by Kip Macy
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$");
32 #include "opt_kstack_pages.h"
33 #include "opt_mp_watchdog.h"
35 #include "opt_sched.h"
40 #error How did you get here?
44 #error The apic device is required for SMP, add "device apic" to your config file.
46 #if defined(CPU_DISABLE_CMPXCHG) && !defined(COMPILING_LINT)
47 #error SMP not supported with CPU_DISABLE_CMPXCHG
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/cons.h> /* cngetc() */
55 #include <sys/cpuset.h>
59 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/memrange.h>
64 #include <sys/mutex.h>
67 #include <sys/rwlock.h>
68 #include <sys/sched.h>
70 #include <sys/sysctl.h>
73 #include <vm/vm_param.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_page.h>
79 #include <x86/apicreg.h>
80 #include <machine/md_var.h>
81 #include <machine/mp_watchdog.h>
82 #include <machine/pcb.h>
83 #include <machine/psl.h>
84 #include <machine/smp.h>
85 #include <machine/specialreg.h>
86 #include <machine/pcpu.h>
88 #include <xen/xen-os.h>
89 #include <xen/evtchn.h>
90 #include <xen/xen_intr.h>
91 #include <xen/hypervisor.h>
92 #include <xen/interface/vcpu.h>
94 /*---------------------------- Extern Declarations ---------------------------*/
95 extern struct pcpu __pcpu[];
97 extern void Xhypervisor_callback(void);
98 extern void failsafe_callback(void);
99 extern void pmap_lazyfix_action(void);
101 /*--------------------------- Forward Declarations ---------------------------*/
102 static driver_filter_t smp_reschedule_interrupt;
103 static driver_filter_t smp_call_function_interrupt;
104 static void assign_cpu_ids(void);
105 static void set_interrupt_apic_ids(void);
106 static int start_all_aps(void);
107 static int start_ap(int apic_id);
108 static void release_aps(void *dummy);
110 /*---------------------------------- Macros ----------------------------------*/
111 #define IPI_TO_IDX(ipi) ((ipi) - APIC_IPI_INTS)
113 /*-------------------------------- Local Types -------------------------------*/
114 typedef void call_data_func_t(uintptr_t , uintptr_t);
122 struct xen_ipi_handler
124 driver_filter_t *filter;
125 const char *description;
130 CALL_FUNCTION_VECTOR,
133 /*-------------------------------- Global Data -------------------------------*/
134 static u_int hyperthreading_cpus;
135 static cpuset_t hyperthreading_cpus_mask;
137 int mp_naps; /* # of Applications processors */
138 int boot_cpu_id = -1; /* designated BSP */
141 static union descriptor *bootAPgdt;
143 /* Free these after use */
144 void *bootstacks[MAXCPU];
146 struct pcb stoppcbs[MAXCPU];
148 /* Variables needed for SMP tlb shootdown. */
149 vm_offset_t smp_tlb_addr1;
150 vm_offset_t smp_tlb_addr2;
151 volatile int smp_tlb_wait;
153 static u_int logical_cpus;
154 static volatile cpuset_t ipi_nmi_pending;
156 /* used to hold the AP's until we are ready to release them */
157 static struct mtx ap_boot_mtx;
159 /* Set to 1 once we're ready to let the APs out of the pen. */
160 static volatile int aps_ready = 0;
163 * Store data from cpu_add() until later in the boot when we actually setup
166 static struct cpu_info cpu_info[MAX_APIC_ID + 1];
167 int cpu_apic_ids[MAXCPU];
168 int apic_cpuids[MAX_APIC_ID + 1];
170 /* Holds pending bitmap based IPIs per CPU */
171 static volatile u_int cpu_ipi_pending[MAXCPU];
173 static int cpu_logical;
174 static int cpu_cores;
176 static const struct xen_ipi_handler xen_ipis[] =
178 [RESCHEDULE_VECTOR] = { smp_reschedule_interrupt, "resched" },
179 [CALL_FUNCTION_VECTOR] = { smp_call_function_interrupt,"callfunc" }
182 /*------------------------------- Per-CPU Data -------------------------------*/
183 DPCPU_DEFINE(xen_intr_handle_t, ipi_handle[nitems(xen_ipis)]);
184 DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
186 /*------------------------------ Implementation ------------------------------*/
192 if (cpu_logical == 0)
194 if (mp_ncpus % (cpu_cores * cpu_logical) != 0) {
195 printf("WARNING: Non-uniform processors.\n");
196 printf("WARNING: Using suboptimal topology.\n");
197 return (smp_topo_none());
200 * No multi-core or hyper-threaded.
202 if (cpu_logical * cpu_cores == 1)
203 return (smp_topo_none());
205 * Only HTT no multi-core.
207 if (cpu_logical > 1 && cpu_cores == 1)
208 return (smp_topo_1level(CG_SHARE_L1, cpu_logical, CG_FLAG_HTT));
210 * Only multi-core no HTT.
212 if (cpu_cores > 1 && cpu_logical == 1)
213 return (smp_topo_1level(CG_SHARE_NONE, cpu_cores, 0));
215 * Both HTT and multi-core.
217 return (smp_topo_2level(CG_SHARE_NONE, cpu_cores,
218 CG_SHARE_L1, cpu_logical, CG_FLAG_HTT));
222 * Calculate usable address in base memory for AP trampoline code.
225 mp_bootaddress(u_int basemem)
232 cpu_add(u_int apic_id, char boot_cpu)
235 if (apic_id > MAX_APIC_ID) {
236 panic("SMP: APIC ID %d too high", apic_id);
239 KASSERT(cpu_info[apic_id].cpu_present == 0, ("CPU %d added twice",
241 cpu_info[apic_id].cpu_present = 1;
243 KASSERT(boot_cpu_id == -1,
244 ("CPU %d claims to be BSP, but CPU %d already is", apic_id,
246 boot_cpu_id = apic_id;
247 cpu_info[apic_id].cpu_bsp = 1;
249 if (mp_ncpus < MAXCPU)
252 printf("SMP: Added CPU %d (%s)\n", apic_id, boot_cpu ? "BSP" :
257 cpu_mp_setmaxid(void)
260 mp_maxid = MAXCPU - 1;
268 * Always record BSP in CPU map so that the mbuf init code works
271 CPU_SETOF(0, &all_cpus);
274 * No CPUs were found, so this must be a UP system. Setup
275 * the variables to represent a system with a single CPU
282 /* At least one CPU was found. */
285 * One CPU was found, so this must be a UP system with
291 /* At least two CPUs were found. */
296 * Initialize the IPI handlers and start up the AP's.
303 /* Initialize the logical ID to APIC ID table. */
304 for (i = 0; i < MAXCPU; i++) {
305 cpu_apic_ids[i] = -1;
306 cpu_ipi_pending[i] = 0;
309 /* Set boot_cpu_id if needed. */
310 if (boot_cpu_id == -1) {
311 boot_cpu_id = PCPU_GET(apic_id);
312 cpu_info[boot_cpu_id].cpu_bsp = 1;
314 KASSERT(boot_cpu_id == PCPU_GET(apic_id),
315 ("BSP's APIC ID doesn't match boot_cpu_id"));
316 cpu_apic_ids[0] = boot_cpu_id;
317 apic_cpuids[boot_cpu_id] = 0;
321 /* Start each Application Processor */
324 /* Setup the initial logical CPUs info. */
326 CPU_ZERO(&logical_cpus_mask);
327 if (cpu_feature & CPUID_HTT)
328 logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
330 set_interrupt_apic_ids();
335 iv_rendezvous(uintptr_t a, uintptr_t b)
337 smp_rendezvous_action();
341 iv_invltlb(uintptr_t a, uintptr_t b)
347 iv_invlpg(uintptr_t a, uintptr_t b)
353 iv_invlrng(uintptr_t a, uintptr_t b)
355 vm_offset_t start = (vm_offset_t)a;
356 vm_offset_t end = (vm_offset_t)b;
358 while (start < end) {
366 iv_invlcache(uintptr_t a, uintptr_t b)
370 atomic_add_int(&smp_tlb_wait, 1);
374 iv_lazypmap(uintptr_t a, uintptr_t b)
376 pmap_lazyfix_action();
377 atomic_add_int(&smp_tlb_wait, 1);
381 * These start from "IPI offset" APIC_IPI_INTS
383 static call_data_func_t *ipi_vectors[6] =
394 * Reschedule call back. Nothing to do,
395 * all the work is done automatically when
396 * we return from the interrupt.
399 smp_reschedule_interrupt(void *unused)
401 int cpu = PCPU_GET(cpuid);
404 ipi_bitmap = atomic_readandclear_int(&cpu_ipi_pending[cpu]);
406 if (ipi_bitmap & (1 << IPI_PREEMPT)) {
408 (*ipi_preempt_counts[cpu])++;
410 sched_preempt(curthread);
413 if (ipi_bitmap & (1 << IPI_AST)) {
415 (*ipi_ast_counts[cpu])++;
417 /* Nothing to do for AST */
419 return (FILTER_HANDLED);
431 static struct _call_data *call_data;
434 smp_call_function_interrupt(void *unused)
436 call_data_func_t *func;
437 uintptr_t arg1 = call_data->arg1;
438 uintptr_t arg2 = call_data->arg2;
439 int wait = call_data->wait;
440 atomic_t *started = &call_data->started;
441 atomic_t *finished = &call_data->finished;
443 /* We only handle function IPIs, not bitmap IPIs */
444 if (call_data->func_id < APIC_IPI_INTS ||
445 call_data->func_id > IPI_BITMAP_VECTOR)
446 panic("invalid function id %u", call_data->func_id);
448 func = ipi_vectors[IPI_TO_IDX(call_data->func_id)];
450 * Notify initiating CPU that I've grabbed the data and am
451 * about to execute the function
456 * At this point the info structure may be out of scope unless wait==1
462 atomic_inc(finished);
464 atomic_add_int(&smp_tlb_wait, 1);
465 return (FILTER_HANDLED);
469 * Print various information about the SMP system hardware and setup.
472 cpu_mp_announce(void)
477 printf(" cpu0 (BSP): APIC ID: %2d\n", boot_cpu_id);
478 for (i = 1, x = 0; x <= MAX_APIC_ID; x++) {
479 if (!cpu_info[x].cpu_present || cpu_info[x].cpu_bsp)
481 if (cpu_info[x].cpu_disabled)
482 printf(" cpu (AP): APIC ID: %2d (disabled)\n", x);
484 KASSERT(i < mp_ncpus,
485 ("mp_ncpus and actual cpus are out of whack"));
486 printf(" cpu%d (AP): APIC ID: %2d\n", i++, x);
492 xen_smp_cpu_init(unsigned int cpu)
494 xen_intr_handle_t *ipi_handle;
495 const struct xen_ipi_handler *ipi;
498 ipi_handle = DPCPU_ID_GET(cpu, ipi_handle);
499 for (ipi = xen_ipis, idx = 0; idx < nitems(xen_ipis); ipi++, idx++) {
502 * The PCPU variable pc_device is not initialized on i386 PV,
503 * so we have to use the root_bus device in order to setup
506 rc = xen_intr_alloc_and_bind_ipi(root_bus, cpu,
507 ipi->filter, INTR_TYPE_TTY, &ipi_handle[idx]);
509 printf("Unable to allocate a XEN IPI port. "
513 xen_intr_describe(ipi_handle[idx], "%s", ipi->description);
516 for (;idx < nitems(xen_ipis); idx++)
517 ipi_handle[idx] = NULL;
522 /* Either all are successfully mapped, or none at all. */
523 for (idx = 0; idx < nitems(xen_ipis); idx++) {
524 if (ipi_handle[idx] == NULL)
527 xen_intr_unbind(ipi_handle[idx]);
528 ipi_handle[idx] = NULL;
535 xen_smp_intr_init_cpus(void *unused)
539 for (i = 0; i < mp_ncpus; i++)
544 xen_smp_intr_setup_cpus(void *unused)
548 for (i = 0; i < mp_ncpus; i++)
549 DPCPU_ID_SET(i, vcpu_info,
550 &HYPERVISOR_shared_info->vcpu_info[i]);
553 #define MTOPSIZE (1<<(14 + PAGE_SHIFT))
556 * AP CPU's call this to initialize themselves.
566 /* bootAP is set in start_ap() to our ID. */
567 PCPU_SET(currentldt, _default_ldt);
568 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
570 gdt[bootAP * NGDT + GPROC0_SEL].sd.sd_type = SDT_SYS386TSS;
572 PCPU_SET(common_tss.tss_esp0, 0); /* not used until after switch */
573 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
574 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
576 PCPU_SET(tss_gdt, &gdt[bootAP * NGDT + GPROC0_SEL].sd);
578 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
580 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
583 * Set to a known state:
584 * Set by mpboot.s: CR0_PG, CR0_PE
585 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
588 * signal our startup to the BSP.
592 /* Spin until the BSP releases the AP's. */
596 /* BSP may have changed PTD while we were waiting */
598 for (addr = 0; addr < NKPT * NBPDR - 1; addr += PAGE_SIZE)
606 /* set up FPU state on the AP */
609 /* A quick check from sanity claus */
610 if (PCPU_GET(apic_id) != lapic_id()) {
611 printf("SMP: cpuid = %d\n", PCPU_GET(cpuid));
612 printf("SMP: actual apic_id = %d\n", lapic_id());
613 printf("SMP: correct apic_id = %d\n", PCPU_GET(apic_id));
614 panic("cpuid mismatch! boom!!");
618 /* Initialize curthread. */
619 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
620 PCPU_SET(curthread, PCPU_GET(idlethread));
622 mtx_lock_spin(&ap_boot_mtx);
625 /* Init local apic for irq's */
630 cpuid = PCPU_GET(cpuid);
631 CTR1(KTR_SMP, "SMP: AP CPU #%d Launched", cpuid);
632 printf("SMP: AP CPU #%d Launched!\n", cpuid);
634 /* Determine if we are a logical CPU. */
635 if (logical_cpus > 1 && PCPU_GET(apic_id) % logical_cpus != 0)
636 CPU_SET(cpuid, &logical_cpus_mask);
638 /* Determine if we are a hyperthread. */
639 if (hyperthreading_cpus > 1 &&
640 PCPU_GET(apic_id) % hyperthreading_cpus != 0)
641 CPU_SET(cpuid, &hyperthreading_cpus_mask);
646 if (smp_cpus == mp_ncpus) {
647 /* enable IPI's, tlb shootdown, freezes etc */
648 atomic_store_rel_int(&smp_started, 1);
651 mtx_unlock_spin(&ap_boot_mtx);
653 /* wait until all the AP's are up */
654 while (smp_started == 0)
657 PCPU_SET(curthread, PCPU_GET(idlethread));
659 /* Start per-CPU event timers. */
662 /* enter the scheduler */
665 panic("scheduler returned us to %s", __func__);
669 /*******************************************************************
670 * local functions and data
674 * We tell the I/O APIC code about all the CPUs we want to receive
675 * interrupts. If we don't want certain CPUs to receive IRQs we
676 * can simply not tell the I/O APIC code about them in this function.
677 * We also do not tell it about the BSP since it tells itself about
678 * the BSP internally to work with UP kernels and on UP machines.
681 set_interrupt_apic_ids(void)
685 for (i = 0; i < MAXCPU; i++) {
686 apic_id = cpu_apic_ids[i];
689 if (cpu_info[apic_id].cpu_bsp)
691 if (cpu_info[apic_id].cpu_disabled)
694 /* Don't let hyperthreads service interrupts. */
695 if (hyperthreading_cpus > 1 &&
696 apic_id % hyperthreading_cpus != 0)
704 * Assign logical CPU IDs to local APICs.
711 /* Check for explicitly disabled CPUs. */
712 for (i = 0; i <= MAX_APIC_ID; i++) {
713 if (!cpu_info[i].cpu_present || cpu_info[i].cpu_bsp)
716 /* Don't use this CPU if it has been disabled by a tunable. */
717 if (resource_disabled("lapic", i)) {
718 cpu_info[i].cpu_disabled = 1;
724 * Assign CPU IDs to local APIC IDs and disable any CPUs
725 * beyond MAXCPU. CPU 0 has already been assigned to the BSP,
726 * so we only have to assign IDs for APs.
729 for (i = 0; i <= MAX_APIC_ID; i++) {
730 if (!cpu_info[i].cpu_present || cpu_info[i].cpu_bsp ||
731 cpu_info[i].cpu_disabled)
734 if (mp_ncpus < MAXCPU) {
735 cpu_apic_ids[mp_ncpus] = i;
736 apic_cpuids[i] = mp_ncpus;
739 cpu_info[i].cpu_disabled = 1;
741 KASSERT(mp_maxid >= mp_ncpus - 1,
742 ("%s: counters out of sync: max %d, count %d", __func__, mp_maxid,
747 * start each AP in our list
749 /* Lowest 1MB is already mapped: don't touch*/
750 #define TMPMAP_START 1
757 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
759 /* set up temporary P==V mapping for AP boot */
760 /* XXX this is a hack, we should boot the AP on its own stack/PTD */
763 for (cpu = 1; cpu < mp_ncpus; cpu++) {
764 apic_id = cpu_apic_ids[cpu];
768 bootAPgdt = gdt + (512*cpu);
770 /* Get per-cpu data */
771 pc = &__pcpu[bootAP];
772 pcpu_init(pc, bootAP, sizeof(struct pcpu));
773 dpcpu_init((void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
774 M_WAITOK | M_ZERO), bootAP);
775 pc->pc_apic_id = cpu_apic_ids[bootAP];
776 pc->pc_vcpu_id = cpu_apic_ids[bootAP];
777 pc->pc_prvspace = pc;
778 pc->pc_curthread = 0;
780 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
781 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
783 PT_SET_MA(bootAPgdt, VTOM(bootAPgdt) | PG_V | PG_RW);
784 bzero(bootAPgdt, PAGE_SIZE);
785 for (x = 0; x < NGDT; x++)
786 ssdtosd(&gdt_segs[x], &bootAPgdt[x].sd);
787 PT_SET_MA(bootAPgdt, vtomach(bootAPgdt) | PG_V);
790 if (HYPERVISOR_vcpu_op(VCPUOP_get_physid, cpu, &cpu_id) == 0) {
791 apicid = xen_vcpu_physid_to_x86_apicid(cpu_id.phys_id);
792 acpiid = xen_vcpu_physid_to_x86_acpiid(cpu_id.phys_id);
795 x86_acpiid_to_apicid[acpiid] = apicid;
800 /* attempt to start the Application Processor */
801 if (!start_ap(cpu)) {
802 printf("AP #%d (PHY# %d) failed!\n", cpu, apic_id);
803 /* better panic as the AP may be running loose */
804 printf("panic y/n? [y] ");
809 CPU_SET(cpu, &all_cpus); /* record AP in CPU map */
813 pmap_invalidate_range(kernel_pmap, 0, NKPT * NBPDR - 1);
815 /* number of APs actually started */
819 extern uint8_t *pcpu_boot_stack;
820 extern trap_info_t trap_table[];
823 smp_trap_init(trap_info_t *trap_ctxt)
825 const trap_info_t *t = trap_table;
827 for (t = trap_table; t->address; t++) {
828 trap_ctxt[t->vector].flags = t->flags;
829 trap_ctxt[t->vector].cs = t->cs;
830 trap_ctxt[t->vector].address = t->address;
834 extern struct rwlock pvh_global_lock;
837 cpu_initialize_context(unsigned int cpu)
839 /* vcpu_guest_context_t is too large to allocate on the stack.
840 * Hence we allocate statically and protect it with a lock */
841 vm_page_t m[NPGPTD + 2];
842 static vcpu_guest_context_t ctxt;
843 vm_offset_t boot_stack;
845 vm_paddr_t ma[NPGPTD];
850 * Page 1, [4] boot stack
854 for (i = 0; i < NPGPTD + 2; i++) {
855 m[i] = vm_page_alloc(NULL, 0,
856 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
859 pmap_zero_page(m[i]);
862 boot_stack = kva_alloc(PAGE_SIZE);
863 newPTD = kva_alloc(NPGPTD * PAGE_SIZE);
864 ma[0] = VM_PAGE_TO_MACH(m[0])|PG_V;
867 pmap_kenter(boot_stack, VM_PAGE_TO_PHYS(m[NPGPTD + 1]));
868 for (i = 0; i < NPGPTD; i++) {
869 ((vm_paddr_t *)boot_stack)[i] =
870 ma[i] = VM_PAGE_TO_MACH(m[i])|PG_V;
875 * Copy cpu0 IdlePTD to new IdlePTD - copying only
878 pmap_qenter(newPTD, m, 4);
880 memcpy((uint8_t *)newPTD + KPTDI*sizeof(vm_paddr_t),
881 (uint8_t *)PTOV(IdlePTD) + KPTDI*sizeof(vm_paddr_t),
882 nkpt*sizeof(vm_paddr_t));
884 pmap_qremove(newPTD, 4);
885 kva_free(newPTD, 4 * PAGE_SIZE);
887 * map actual idle stack to boot_stack
889 pmap_kenter(boot_stack, VM_PAGE_TO_PHYS(m[NPGPTD]));
892 xen_pgdpt_pin(VM_PAGE_TO_MACH(m[NPGPTD + 1]));
893 rw_wlock(&pvh_global_lock);
894 for (i = 0; i < 4; i++) {
895 int pdir = (PTDPTDI + i) / NPDEPG;
896 int curoffset = (PTDPTDI + i) % NPDEPG;
898 xen_queue_pt_update((vm_paddr_t)
899 ((ma[pdir] & ~PG_V) + (curoffset*sizeof(vm_paddr_t))),
903 rw_wunlock(&pvh_global_lock);
905 memset(&ctxt, 0, sizeof(ctxt));
906 ctxt.flags = VGCF_IN_KERNEL;
907 ctxt.user_regs.ds = GSEL(GDATA_SEL, SEL_KPL);
908 ctxt.user_regs.es = GSEL(GDATA_SEL, SEL_KPL);
909 ctxt.user_regs.fs = GSEL(GPRIV_SEL, SEL_KPL);
910 ctxt.user_regs.gs = GSEL(GDATA_SEL, SEL_KPL);
911 ctxt.user_regs.cs = GSEL(GCODE_SEL, SEL_KPL);
912 ctxt.user_regs.ss = GSEL(GDATA_SEL, SEL_KPL);
913 ctxt.user_regs.eip = (unsigned long)init_secondary;
914 ctxt.user_regs.eflags = PSL_KERNEL | 0x1000; /* IOPL_RING1 */
916 memset(&ctxt.fpu_ctxt, 0, sizeof(ctxt.fpu_ctxt));
918 smp_trap_init(ctxt.trap_ctxt);
922 (uint32_t)((uint64_t)vtomach(bootAPgdt) >> PAGE_SHIFT);
926 ctxt.user_regs.esp = boot_stack + PAGE_SIZE;
928 ctxt.kernel_ss = GSEL(GDATA_SEL, SEL_KPL);
929 ctxt.kernel_sp = boot_stack + PAGE_SIZE;
931 ctxt.event_callback_cs = GSEL(GCODE_SEL, SEL_KPL);
932 ctxt.event_callback_eip = (unsigned long)Xhypervisor_callback;
933 ctxt.failsafe_callback_cs = GSEL(GCODE_SEL, SEL_KPL);
934 ctxt.failsafe_callback_eip = (unsigned long)failsafe_callback;
936 ctxt.ctrlreg[3] = VM_PAGE_TO_MACH(m[NPGPTD + 1]);
937 #else /* __x86_64__ */
938 ctxt.user_regs.esp = idle->thread.rsp0 - sizeof(struct pt_regs);
939 ctxt.kernel_ss = GSEL(GDATA_SEL, SEL_KPL);
940 ctxt.kernel_sp = idle->thread.rsp0;
942 ctxt.event_callback_eip = (unsigned long)hypervisor_callback;
943 ctxt.failsafe_callback_eip = (unsigned long)failsafe_callback;
944 ctxt.syscall_callback_eip = (unsigned long)system_call;
946 ctxt.ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(init_level4_pgt));
948 ctxt.gs_base_kernel = (unsigned long)(cpu_pda(cpu));
951 printf("gdtpfn=%lx pdptpfn=%lx\n",
953 ctxt.ctrlreg[3] >> PAGE_SHIFT);
955 PANIC_IF(HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, &ctxt));
957 PANIC_IF(HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL));
961 * This function starts the AP (application processor) identified
962 * by the APIC ID 'physicalCpu'. It does quite a "song and dance"
963 * to accomplish this. This is necessary because of the nuances
964 * of the different hardware we might encounter. It isn't pretty,
965 * but it seems to work.
970 start_ap(int apic_id)
974 /* used as a watchpoint to signal AP startup */
977 cpu_initialize_context(apic_id);
979 /* Wait up to 5 seconds for it to start. */
980 for (ms = 0; ms < 5000; ms++) {
982 return (1); /* return SUCCESS */
985 return (0); /* return FAILURE */
989 ipi_pcpu(int cpu, u_int ipi)
991 KASSERT((ipi <= nitems(xen_ipis)), ("invalid IPI"));
992 xen_intr_signal(DPCPU_ID_GET(cpu, ipi_handle[ipi]));
996 * send an IPI to a specific CPU.
999 ipi_send_cpu(int cpu, u_int ipi)
1001 u_int bitmap, old_pending, new_pending;
1003 if (IPI_IS_BITMAPED(ipi)) {
1005 ipi = IPI_BITMAP_VECTOR;
1007 old_pending = cpu_ipi_pending[cpu];
1008 new_pending = old_pending | bitmap;
1009 } while (!atomic_cmpset_int(&cpu_ipi_pending[cpu],
1010 old_pending, new_pending));
1012 ipi_pcpu(cpu, RESCHEDULE_VECTOR);
1014 KASSERT(call_data != NULL, ("call_data not set"));
1015 ipi_pcpu(cpu, CALL_FUNCTION_VECTOR);
1020 * Flush the TLB on all other CPU's
1023 smp_tlb_shootdown(u_int vector, vm_offset_t addr1, vm_offset_t addr2)
1026 struct _call_data data;
1028 ncpu = mp_ncpus - 1; /* does not shootdown self */
1030 return; /* no other cpus */
1031 if (!(read_eflags() & PSL_I))
1032 panic("%s: interrupts disabled", __func__);
1033 mtx_lock_spin(&smp_ipi_mtx);
1034 KASSERT(call_data == NULL, ("call_data isn't null?!"));
1036 call_data->func_id = vector;
1037 call_data->arg1 = addr1;
1038 call_data->arg2 = addr2;
1039 atomic_store_rel_int(&smp_tlb_wait, 0);
1040 ipi_all_but_self(vector);
1041 while (smp_tlb_wait < ncpu)
1044 mtx_unlock_spin(&smp_ipi_mtx);
1048 smp_targeted_tlb_shootdown(cpuset_t mask, u_int vector, vm_offset_t addr1,
1051 int cpu, ncpu, othercpus;
1052 struct _call_data data;
1054 othercpus = mp_ncpus - 1;
1055 if (CPU_ISFULLSET(&mask)) {
1059 CPU_CLR(PCPU_GET(cpuid), &mask);
1060 if (CPU_EMPTY(&mask))
1063 if (!(read_eflags() & PSL_I))
1064 panic("%s: interrupts disabled", __func__);
1065 mtx_lock_spin(&smp_ipi_mtx);
1066 KASSERT(call_data == NULL, ("call_data isn't null?!"));
1068 call_data->func_id = vector;
1069 call_data->arg1 = addr1;
1070 call_data->arg2 = addr2;
1071 atomic_store_rel_int(&smp_tlb_wait, 0);
1072 if (CPU_ISFULLSET(&mask)) {
1074 ipi_all_but_self(vector);
1077 while ((cpu = CPU_FFS(&mask)) != 0) {
1079 CPU_CLR(cpu, &mask);
1080 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu,
1082 ipi_send_cpu(cpu, vector);
1086 while (smp_tlb_wait < ncpu)
1089 mtx_unlock_spin(&smp_ipi_mtx);
1093 smp_cache_flush(void)
1097 smp_tlb_shootdown(IPI_INVLCACHE, 0, 0);
1105 smp_tlb_shootdown(IPI_INVLTLB, 0, 0);
1110 smp_invlpg(vm_offset_t addr)
1114 smp_tlb_shootdown(IPI_INVLPG, addr, 0);
1119 smp_invlpg_range(vm_offset_t addr1, vm_offset_t addr2)
1123 smp_tlb_shootdown(IPI_INVLRNG, addr1, addr2);
1128 smp_masked_invltlb(cpuset_t mask)
1132 smp_targeted_tlb_shootdown(mask, IPI_INVLTLB, 0, 0);
1137 smp_masked_invlpg(cpuset_t mask, vm_offset_t addr)
1141 smp_targeted_tlb_shootdown(mask, IPI_INVLPG, addr, 0);
1146 smp_masked_invlpg_range(cpuset_t mask, vm_offset_t addr1, vm_offset_t addr2)
1150 smp_targeted_tlb_shootdown(mask, IPI_INVLRNG, addr1, addr2);
1155 * send an IPI to a set of cpus.
1158 ipi_selected(cpuset_t cpus, u_int ipi)
1163 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1164 * of help in order to understand what is the source.
1165 * Set the mask of receiving CPUs for this purpose.
1167 if (ipi == IPI_STOP_HARD)
1168 CPU_OR_ATOMIC(&ipi_nmi_pending, &cpus);
1170 while ((cpu = CPU_FFS(&cpus)) != 0) {
1172 CPU_CLR(cpu, &cpus);
1173 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu, ipi);
1174 ipi_send_cpu(cpu, ipi);
1179 * send an IPI to a specific CPU.
1182 ipi_cpu(int cpu, u_int ipi)
1186 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1187 * of help in order to understand what is the source.
1188 * Set the mask of receiving CPUs for this purpose.
1190 if (ipi == IPI_STOP_HARD)
1191 CPU_SET_ATOMIC(cpu, &ipi_nmi_pending);
1193 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu, ipi);
1194 ipi_send_cpu(cpu, ipi);
1198 * send an IPI to all CPUs EXCEPT myself
1201 ipi_all_but_self(u_int ipi)
1203 cpuset_t other_cpus;
1206 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1207 * of help in order to understand what is the source.
1208 * Set the mask of receiving CPUs for this purpose.
1210 other_cpus = all_cpus;
1211 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
1212 if (ipi == IPI_STOP_HARD)
1213 CPU_OR_ATOMIC(&ipi_nmi_pending, &other_cpus);
1215 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
1216 ipi_selected(other_cpus, ipi);
1225 * As long as there is not a simple way to know about a NMI's
1226 * source, if the bitmask for the current CPU is present in
1227 * the global pending bitword an IPI_STOP_HARD has been issued
1228 * and should be handled.
1230 cpuid = PCPU_GET(cpuid);
1231 if (!CPU_ISSET(cpuid, &ipi_nmi_pending))
1234 CPU_CLR_ATOMIC(cpuid, &ipi_nmi_pending);
1240 * Handle an IPI_STOP by saving our current context and spinning until we
1244 cpustop_handler(void)
1248 cpu = PCPU_GET(cpuid);
1250 savectx(&stoppcbs[cpu]);
1252 /* Indicate that we are stopped */
1253 CPU_SET_ATOMIC(cpu, &stopped_cpus);
1255 /* Wait for restart */
1256 while (!CPU_ISSET(cpu, &started_cpus))
1259 CPU_CLR_ATOMIC(cpu, &started_cpus);
1260 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
1262 if (cpu == 0 && cpustop_restartfunc != NULL) {
1263 cpustop_restartfunc();
1264 cpustop_restartfunc = NULL;
1269 * Handlers for TLB related IPIs
1271 * On i386 Xen PV this are no-ops since this port doesn't support SMP.
1274 invltlb_handler(void)
1279 invlpg_handler(void)
1284 invlrng_handler(void)
1289 invlcache_handler(void)
1294 * This is called once the rest of the system is up and running and we're
1295 * ready to let the AP's out of the pen.
1298 release_aps(void *dummy __unused)
1303 atomic_store_rel_int(&aps_ready, 1);
1304 while (smp_started == 0)
1307 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
1308 SYSINIT(start_ipis, SI_SUB_SMP, SI_ORDER_ANY, xen_smp_intr_init_cpus, NULL);
1309 SYSINIT(start_cpu, SI_SUB_INTR, SI_ORDER_ANY, xen_smp_intr_setup_cpus, NULL);