Merge Xen PVHVM support into generic for both amd64 and i386. sys/amd64/amd64/mp_machdep.c: sys/amd64/include/cpu.h: sys/i386/i386/mp_machdep.c: sys/i386/include/cpu.h: - Introduce two new CPU hooks, for initialization and resume purposes. This allows us to get rid of the XENHVM ifdefs in mp_machdep, and also sets some hooks into common code that can be used by others. sys/amd64/conf/XENHVM: sys/i386/conf/XENHVM: - Remove these configs now that GENERIC has builtin support for Xen. sys/kern/subr_smp.c: - Make sure there are no pending IPIs when suspending with the GENERIC kernel. sys/x86/xen/hvm.c: - Add cpu resume operation, that will be called from mp_machdep using the new hooks. - Remove the CPU_FOREACH call in xen_hvm_init, it is not needed, and at the point were xen_hvm_init gets called we still don't know how many CPUs there are on the system. - Gate xen_hvm_init_cpu only to systems running under Xen. sys/x86/xen/xen_intr.c: - Gate the setup of event channels only to systems running under Xen. Index: amd64/amd64/mp_machdep.c =================================================================== --- amd64/amd64/mp_machdep.c (revision 255729) +++ amd64/amd64/mp_machdep.c (working copy) @@ -71,10 +71,6 @@ __FBSDID("$FreeBSD$"); #include #include -#ifdef XENHVM -#include -#endif - #define WARMBOOT_TARGET 0 #define WARMBOOT_OFF (KERNBASE + 0x0467) #define WARMBOOT_SEG (KERNBASE + 0x0469) @@ -161,7 +157,7 @@ int cpu_apic_ids[MAXCPU]; int apic_cpuids[MAX_APIC_ID + 1]; /* Holds pending bitmap based IPIs per CPU */ -static volatile u_int cpu_ipi_pending[MAXCPU]; +volatile u_int cpu_ipi_pending[MAXCPU]; static u_int boot_address; static int cpu_logical; /* logical cpus per core */ @@ -732,10 +728,8 @@ init_secondary(void) /* set up FPU state on the AP */ fpuinit(); -#ifdef XENHVM - /* register vcpu_info area */ - xen_hvm_init_cpu(); -#endif + if (cpu_ops.cpu_init) + cpu_ops.cpu_init(); /* A quick check from sanity claus */ cpuid = PCPU_GET(cpuid); @@ -1466,12 +1460,9 @@ cpususpend_handler(void) { u_int cpu; - cpu = PCPU_GET(cpuid); - -#ifdef XENHVM mtx_assert(&smp_ipi_mtx, MA_NOTOWNED); -#endif + cpu = PCPU_GET(cpuid); if (savectx(susppcbs[cpu])) { ctx_fpusave(susppcbs[cpu]->pcb_fpususpend); wbinvd(); @@ -1490,15 +1481,8 @@ cpususpend_handler(void) while (!CPU_ISSET(cpu, &started_cpus)) ia32_pause(); -#ifdef XENHVM - /* - * Reset pending bitmap IPIs, because Xen doesn't preserve pending - * event channels on migration. - */ - cpu_ipi_pending[cpu] = 0; - /* register vcpu_info area */ - xen_hvm_init_cpu(); -#endif + if (cpu_ops.cpu_resume) + cpu_ops.cpu_resume(); /* Resume MCA and local APIC */ mca_resume(); Index: amd64/conf/GENERIC =================================================================== --- amd64/conf/GENERIC (revision 255729) +++ amd64/conf/GENERIC (working copy) @@ -72,6 +72,7 @@ options KDTRACE_FRAME # Ensure frames are compil options KDTRACE_HOOKS # Kernel DTrace hooks options DDB_CTF # Kernel ELF linker loads CTF data options INCLUDE_CONFIG_FILE # Include this file in kernel +options XENHVM # Include Xen support # Debugging support. Always need this: options KDB # Enable kernel debugger support. @@ -341,6 +342,9 @@ device virtio_blk # VirtIO Block device device virtio_scsi # VirtIO SCSI device device virtio_balloon # VirtIO Memory Balloon device +# Xen support +device xenpci # Generic Xen bus + # HyperV drivers device hyperv # HyperV drivers Index: amd64/conf/XENHVM =================================================================== --- amd64/conf/XENHVM (revision 255729) +++ amd64/conf/XENHVM (working copy) @@ -1,22 +0,0 @@ -# -# XENHVM -- Xen HVM kernel configuration file for FreeBSD/amd64 -# -# $FreeBSD$ -# -include GENERIC -ident XENHVM - -# -# Adaptive locks rely on a lock-free pointer read to determine the run state -# of the thread holding a lock when under contention; under a virtualisation -# system, the thread run state may not accurately reflect whether the thread -# (or rather its host VCPU) is actually executing. As such, disable this -# optimisation. -# -options NO_ADAPTIVE_MUTEXES -options NO_ADAPTIVE_RWLOCKS -options NO_ADAPTIVE_SX - -# Xen HVM support -options XENHVM -device xenpci Index: amd64/include/cpu.h =================================================================== --- amd64/include/cpu.h (revision 255729) +++ amd64/include/cpu.h (working copy) @@ -61,6 +61,8 @@ * hypervisor environment. */ struct cpu_ops { + void (*cpu_init)(void); + void (*cpu_resume)(void); void (*ipi_vectored)(u_int, int); }; Index: i386/conf/GENERIC =================================================================== --- i386/conf/GENERIC (revision 255729) +++ i386/conf/GENERIC (working copy) @@ -72,6 +72,7 @@ options MAC # TrustedBSD MAC Framework options KDTRACE_HOOKS # Kernel DTrace hooks options DDB_CTF # Kernel ELF linker loads CTF data options INCLUDE_CONFIG_FILE # Include this file in kernel +options XENHVM # Include Xen support # Debugging support. Always need this: options KDB # Enable kernel debugger support. @@ -355,5 +356,8 @@ device virtio_blk # VirtIO Block device device virtio_scsi # VirtIO SCSI device device virtio_balloon # VirtIO Memory Balloon device +# Xen support +device xenpci # Generic Xen bus + # VMware support device vmx # VMware VMXNET3 Ethernet Index: i386/conf/XENHVM =================================================================== --- i386/conf/XENHVM (revision 255729) +++ i386/conf/XENHVM (working copy) @@ -1,22 +0,0 @@ -# -# XENHVM -- Xen HVM kernel configuration file for FreeBSD/i386 -# -# $FreeBSD$ -# -include GENERIC -ident XENHVM - -# -# Adaptive locks rely on a lock-free pointer read to determine the run state -# of the thread holding a lock when under contention; under a virtualisation -# system, the thread run state may not accurately reflect whether the thread -# (or rather its host VCPU) is actually executing. As such, disable this -# optimisation. -# -options NO_ADAPTIVE_MUTEXES -options NO_ADAPTIVE_RWLOCKS -options NO_ADAPTIVE_SX - -# Xen HVM support -options XENHVM -device xenpci Index: i386/i386/mp_machdep.c =================================================================== --- i386/i386/mp_machdep.c (revision 255729) +++ i386/i386/mp_machdep.c (working copy) @@ -83,10 +83,6 @@ __FBSDID("$FreeBSD$"); #include #include -#ifdef XENHVM -#include -#endif - #define WARMBOOT_TARGET 0 #define WARMBOOT_OFF (KERNBASE + 0x0467) #define WARMBOOT_SEG (KERNBASE + 0x0469) @@ -202,7 +198,7 @@ int cpu_apic_ids[MAXCPU]; int apic_cpuids[MAX_APIC_ID + 1]; /* Holds pending bitmap based IPIs per CPU */ -static volatile u_int cpu_ipi_pending[MAXCPU]; +volatile u_int cpu_ipi_pending[MAXCPU]; static u_int boot_address; static int cpu_logical; /* logical cpus per core */ @@ -757,10 +753,8 @@ init_secondary(void) /* set up SSE registers */ enable_sse(); -#ifdef XENHVM - /* register vcpu_info area */ - xen_hvm_init_cpu(); -#endif + if (cpu_ops.cpu_init) + cpu_ops.cpu_init(); #ifdef PAE /* Enable the PTE no-execute bit. */ @@ -1527,12 +1521,9 @@ cpususpend_handler(void) { u_int cpu; - cpu = PCPU_GET(cpuid); - -#ifdef XENHVM mtx_assert(&smp_ipi_mtx, MA_NOTOWNED); -#endif + cpu = PCPU_GET(cpuid); if (savectx(susppcbs[cpu])) { wbinvd(); CPU_SET_ATOMIC(cpu, &suspended_cpus); @@ -1549,15 +1540,8 @@ cpususpend_handler(void) while (!CPU_ISSET(cpu, &started_cpus)) ia32_pause(); -#ifdef XENHVM - /* - * Reset pending bitmap IPIs, because Xen doesn't preserve pending - * event channels on migration. - */ - cpu_ipi_pending[cpu] = 0; - /* register vcpu_info area */ - xen_hvm_init_cpu(); -#endif + if (cpu_ops.cpu_resume) + cpu_ops.cpu_resume(); /* Resume MCA and local APIC */ mca_resume(); Index: i386/include/cpu.h =================================================================== --- i386/include/cpu.h (revision 255729) +++ i386/include/cpu.h (working copy) @@ -61,6 +61,8 @@ * hypervisor environment. */ struct cpu_ops { + void (*cpu_init)(void); + void (*cpu_resume)(void); void (*ipi_vectored)(u_int, int); }; Index: kern/subr_smp.c =================================================================== --- kern/subr_smp.c (revision 255729) +++ kern/subr_smp.c (working copy) @@ -225,17 +225,15 @@ generic_stop_cpus(cpuset_t map, u_int type) CTR2(KTR_SMP, "stop_cpus(%s) with %u type", cpusetobj_strprint(cpusetbuf, &map), type); -#ifdef XENHVM /* - * When migrating a PVHVM domain we need to make sure there are - * no IPIs in progress. IPIs that have been issued, but not + * When migrating a Xen PVHVM domain we need to make sure there + * are no IPIs in progress. IPIs that have been issued, but not * yet delivered (not pending on a vCPU) will be lost in the * IPI rebinding process, violating FreeBSD's assumption of * reliable IPI delivery. */ if (type == IPI_SUSPEND) mtx_lock_spin(&smp_ipi_mtx); -#endif if (stopping_cpu != PCPU_GET(cpuid)) while (atomic_cmpset_int(&stopping_cpu, NOCPU, @@ -264,10 +262,8 @@ generic_stop_cpus(cpuset_t map, u_int type) } } -#ifdef XENHVM if (type == IPI_SUSPEND) mtx_unlock_spin(&smp_ipi_mtx); -#endif stopping_cpu = NOCPU; return (1); Index: x86/xen/hvm.c =================================================================== --- x86/xen/hvm.c (revision 255729) +++ x86/xen/hvm.c (working copy) @@ -72,6 +72,9 @@ static driver_filter_t xen_cpustop_handler; static driver_filter_t xen_cpususpend_handler; static driver_filter_t xen_cpustophard_handler; #endif +static void xen_ipi_vectored(u_int vector, int dest); +static void xen_hvm_cpu_resume(void); +static void xen_hvm_cpu_init(void); /*---------------------------- Extern Declarations ---------------------------*/ /* Variables used by mp_machdep to perform the MMU related IPIs */ @@ -91,6 +94,9 @@ extern pmap_t smp_tlb_pmap; extern void pmap_lazyfix_action(void); #endif +/* Variables used by mp_machdep to perform the bitmap IPI */ +extern volatile u_int cpu_ipi_pending[MAXCPU]; + /*---------------------------------- Macros ----------------------------------*/ #define IPI_TO_IDX(ipi) ((ipi) - APIC_IPI_INTS) @@ -110,6 +116,12 @@ struct xen_ipi_handler /*-------------------------------- Global Data -------------------------------*/ enum xen_domain_type xen_domain_type = XEN_NATIVE; +struct cpu_ops xen_hvm_cpu_ops = { + .ipi_vectored = xen_ipi_vectored, + .cpu_init = xen_hvm_cpu_init, + .cpu_resume = xen_hvm_cpu_resume +}; + static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support"); #ifdef SMP @@ -462,7 +474,23 @@ xen_ipi_vectored(u_int vector, int dest) } } +/* XEN diverged cpu operations */ static void +xen_hvm_cpu_resume(void) +{ + u_int cpuid = PCPU_GET(cpuid); + + /* + * Reset pending bitmap IPIs, because Xen doesn't preserve pending + * event channels on migration. + */ + cpu_ipi_pending[cpuid] = 0; + + /* register vcpu_info area */ + xen_hvm_cpu_init(); +} + +static void xen_cpu_ipi_init(int cpu) { xen_intr_handle_t *ipi_handle; @@ -490,7 +518,7 @@ xen_cpu_ipi_init(int cpu) } static void -xen_init_ipis(void) +xen_setup_cpus(void) { int i; @@ -507,7 +535,7 @@ static void xen_cpu_ipi_init(i); /* Set the xen pv ipi ops to replace the native ones */ - cpu_ops.ipi_vectored = xen_ipi_vectored; + cpu_ops = xen_hvm_cpu_ops; } #endif @@ -704,7 +732,7 @@ xen_hvm_resume(bool suspend_cancelled) XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME); /* Register vcpu_info area for CPU#0. */ - xen_hvm_init_cpu(); + xen_hvm_cpu_init(); } static void @@ -713,13 +741,16 @@ xen_hvm_sysinit(void *arg __unused) xen_hvm_init(XEN_HVM_INIT_COLD); } -void -xen_hvm_init_cpu(void) +static void +xen_hvm_cpu_init(void) { struct vcpu_register_vcpu_info info; struct vcpu_info *vcpu_info; int cpu, rc; + if (!xen_domain()) + return; + if (DPCPU_GET(vcpu_info) != NULL) { /* * vcpu_info is already set. We're resuming @@ -743,6 +774,6 @@ xen_hvm_sysinit(void *arg __unused) SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL); #ifdef SMP -SYSINIT(xen_init_ipis, SI_SUB_SMP, SI_ORDER_FIRST, xen_init_ipis, NULL); +SYSINIT(xen_setup_cpus, SI_SUB_SMP, SI_ORDER_FIRST, xen_setup_cpus, NULL); #endif -SYSINIT(xen_hvm_init_cpu, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_init_cpu, NULL); +SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL); Index: xen/hvm.h =================================================================== --- xen/hvm.h (revision 255729) +++ xen/hvm.h (working copy) @@ -94,5 +94,4 @@ enum { void xen_hvm_set_callback(device_t); void xen_hvm_suspend(void); void xen_hvm_resume(bool suspend_cancelled); -void xen_hvm_init_cpu(void); #endif /* __XEN_HVM_H__ */