]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
x86/bhyve: Detect FreeBSD Bhyve hypervisor
authorDavid Woodhouse <dwmw@amazon.co.uk>
Mon, 11 Aug 2025 10:48:11 +0000 (11:48 +0100)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Mon, 18 Aug 2025 09:48:45 +0000 (10:48 +0100)
This detects the Bhyve hypervisor and enables 15-bit MSI support if
available.

Detecting Bhyve used to be a purely cosmetic issue, of the kernel printing
'Hypervisor detected: Bhyve' at boot time.

But FreeBSD 15.0 will support¹ the 15-bit MSI enlightenment to support
more than 255 vCPUs (http://david.woodhou.se/ExtDestId.pdf) which means
there's now actually some functional reason to do so.

¹ https://github.com/freebsd/freebsd-src/commit/313a68ea20b4

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Acked-by: Ahmed S. Darwish <darwi@linutronix.de>
arch/x86/Kconfig
arch/x86/include/asm/hypervisor.h
arch/x86/kernel/cpu/Makefile
arch/x86/kernel/cpu/bhyve.c [new file with mode: 0644]
arch/x86/kernel/cpu/hypervisor.c

index 58d890fe2100eb6990880bcf5ba600cdefb0a7d1..ac1c6df442122bcc9abe8ea66585029bc66325f5 100644 (file)
@@ -879,6 +879,15 @@ config ACRN_GUEST
          IOT with small footprint and real-time features. More details can be
          found in https://projectacrn.org/.
 
+config BHYVE_GUEST
+       bool "Bhyve (BSD Hypervisor) Guest support"
+       depends on X86_64
+       help
+         This option allows to run Linux to recognise when it is running as a
+         guest in the Bhyve hypervisor, and to support more than 255 vCPUs when
+         when doing so. More details about Bhyve can be found at https://bhyve.org
+         and https://wiki.freebsd.org/bhyve/.
+
 config INTEL_TDX_GUEST
        bool "Intel TDX (Trust Domain Extensions) - Guest Support"
        depends on X86_64 && CPU_SUP_INTEL
index e41cbf2ec41d2031ef9d1ee919a359f75daf20a1..9ad86a7d13f6d79274a5436b688266920869b087 100644 (file)
@@ -30,6 +30,7 @@ enum x86_hypervisor_type {
        X86_HYPER_KVM,
        X86_HYPER_JAILHOUSE,
        X86_HYPER_ACRN,
+       X86_HYPER_BHYVE,
 };
 
 #ifdef CONFIG_HYPERVISOR_GUEST
@@ -64,6 +65,7 @@ extern const struct hypervisor_x86 x86_hyper_xen_pv;
 extern const struct hypervisor_x86 x86_hyper_kvm;
 extern const struct hypervisor_x86 x86_hyper_jailhouse;
 extern const struct hypervisor_x86 x86_hyper_acrn;
+extern const struct hypervisor_x86 x86_hyper_bhyve;
 extern struct hypervisor_x86 x86_hyper_xen_hvm;
 
 extern bool nopv;
index 1e26179ff18c4aac9be136b306cccc7b1db97700..2f8a58ef690ec75678d523476136d50e930cee3f 100644 (file)
@@ -58,6 +58,7 @@ obj-$(CONFIG_X86_SGX)                 += sgx/
 obj-$(CONFIG_X86_LOCAL_APIC)           += perfctr-watchdog.o
 
 obj-$(CONFIG_HYPERVISOR_GUEST)         += vmware.o hypervisor.o mshyperv.o
+obj-$(CONFIG_BHYVE_GUEST)              += bhyve.o
 obj-$(CONFIG_ACRN_GUEST)               += acrn.o
 
 obj-$(CONFIG_DEBUG_FS)                 += debugfs.o
diff --git a/arch/x86/kernel/cpu/bhyve.c b/arch/x86/kernel/cpu/bhyve.c
new file mode 100644 (file)
index 0000000..1856af5
--- /dev/null
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * FreeBSD Bhyve guest enlightenments
+ *
+ * Copyright © 2025 Amazon.com, Inc. or its affiliates.
+ *
+ * Author: David Woodhouse <dwmw2@infradead.org>
+ */
+
+#include <linux/init.h>
+#include <linux/export.h>
+#include <asm/processor.h>
+#include <asm/hypervisor.h>
+
+static uint32_t bhyve_cpuid_base;
+static uint32_t bhyve_cpuid_max;
+
+#define BHYVE_SIGNATURE                        "bhyve bhyve "
+
+#define CPUID_BHYVE_FEATURES           0x40000001
+
+/* Features advertised in CPUID_BHYVE_FEATURES %eax */
+#define CPUID_BHYVE_FEAT_EXT_DEST_ID   (1UL << 0) /* MSI Extended Dest ID */
+
+static uint32_t __init bhyve_detect(void)
+{
+       if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
+                return 0;
+
+       bhyve_cpuid_base = cpuid_base_hypervisor(BHYVE_SIGNATURE, 0);
+       if (!bhyve_cpuid_base)
+               return 0;
+
+       bhyve_cpuid_max = cpuid_eax(bhyve_cpuid_base);
+       return bhyve_cpuid_max;
+}
+
+static uint32_t bhyve_features(void)
+{
+       if (bhyve_cpuid_max < (bhyve_cpuid_base | CPUID_BHYVE_FEATURES))
+               return 0;
+
+       return cpuid_eax(bhyve_cpuid_base | CPUID_BHYVE_FEATURES);
+}
+
+static bool __init bhyve_ext_dest_id(void)
+{
+       return !!(bhyve_features() & CPUID_BHYVE_FEAT_EXT_DEST_ID);
+}
+
+static bool __init bhyve_x2apic_available(void)
+{
+       return true;
+}
+
+const struct hypervisor_x86 x86_hyper_bhyve __refconst = {
+       .name                   = "Bhyve",
+       .detect                 = bhyve_detect,
+       .init.init_platform     = x86_init_noop,
+       .init.x2apic_available  = bhyve_x2apic_available,
+       .init.msi_ext_dest_id   = bhyve_ext_dest_id,
+};
index 553bfbfc3a1b5ae28ac688afb646296d9dcff7e7..f3e9219845e8582a696a0f91946ab02d0baf0774 100644 (file)
@@ -45,6 +45,9 @@ static const __initconst struct hypervisor_x86 * const hypervisors[] =
 #ifdef CONFIG_ACRN_GUEST
        &x86_hyper_acrn,
 #endif
+#ifdef CONFIG_BHYVE_GUEST
+       &x86_hyper_bhyve,
+#endif
 };
 
 enum x86_hypervisor_type x86_hyper_type;