git: 7336d1a5c23e - main - vmm/amd64: Port bhyve CPUID control from illumos
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 25 Nov 2026 21:35:52 UTC
The branch main has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=7336d1a5c23ef90e9b234f8b9f9baad76534dbe6
commit 7336d1a5c23ef90e9b234f8b9f9baad76534dbe6
Author: Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
AuthorDate: 2025-06-27 08:56:12 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-25 21:29:06 +0000
vmm/amd64: Port bhyve CPUID control from illumos
This is a partial port of the following bhyve improvement in illumos:
14485 bhyve needs better cpuid control
https://www.illumos.org/issues/14485
This only adds vmm kernel support for overriding the CPUID information
for a guest. This does also not contain any libvmmapi/bhyve userspace
code to make this feature actually useful.
Reviewed by: markj
MFC after: 1 month
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51552
---
sys/amd64/include/vmm.h | 69 ++-
sys/amd64/include/vmm_dev.h | 28 ++
sys/amd64/vmm/amd/svm.c | 6 +-
sys/amd64/vmm/intel/vmx.c | 18 +-
sys/amd64/vmm/vmm.c | 12 +
sys/amd64/vmm/vmm_cpuid.c | 961 ++++++++++++++++++++++++++++++++++++++++
sys/amd64/vmm/vmm_dev_machdep.c | 109 +++++
sys/amd64/vmm/x86.c | 618 --------------------------
sys/amd64/vmm/x86.h | 3 -
sys/modules/vmm/Makefile | 3 +-
10 files changed, 1189 insertions(+), 638 deletions(-)
diff --git a/sys/amd64/include/vmm.h b/sys/amd64/include/vmm.h
index 4ff7e83db90c..c6f40733f767 100644
--- a/sys/amd64/include/vmm.h
+++ b/sys/amd64/include/vmm.h
@@ -138,7 +138,8 @@ enum x2apic_state {
struct vm_exit exitinfo; /* (x) exit reason and collateral */ \
cpuset_t exitinfo_cpuset; /* (x) storage for vmexit handlers */ \
uint64_t nextrip; /* (x) next instruction to execute */ \
- uint64_t tsc_offset /* (o) TSC offsetting */
+ uint64_t tsc_offset; /* (o) TSC offsetting */ \
+ vcpu_cpuid_config_t cpuid_cfg /* (x) cpuid configuration */
#define VMM_VM_MD_FIELDS \
cpuset_t startup_cpus; /* (i) [r] waiting for startup */ \
@@ -691,4 +692,70 @@ vm_inject_ss(struct vcpu *vcpu, int errcode)
void vm_inject_pf(struct vcpu *vcpu, int error_code, uint64_t cr2);
+/*
+ * Describes an entry for `cpuid` emulation.
+ * Used internally by bhyve (kernel) in addition to exposed ioctl(2) interface.
+ */
+struct vcpu_cpuid_entry {
+ uint32_t vce_function;
+ uint32_t vce_index;
+ uint32_t vce_flags;
+ uint32_t vce_eax;
+ uint32_t vce_ebx;
+ uint32_t vce_ecx;
+ uint32_t vce_edx;
+ uint32_t _pad;
+};
+
+/*
+ * Defined flags for vcpu_cpuid_entry`vce_flags are below.
+ */
+
+/* Use index (ecx) input value when matching entry */
+#define VCE_FLAG_MATCH_INDEX (1 << 0)
+
+/* All valid flags for vcpu_cpuid_entry`vce_flags */
+#define VCE_FLAGS_VALID VCE_FLAG_MATCH_INDEX
+
+/*
+ * Defined flags for vcpu_cpuid configuration are below.
+ * These are used by both the ioctl(2) interface via vm_vcpu_cpuid_config and
+ * internally in the kernel vmm.
+ */
+
+/* Use legacy hard-coded cpuid masking tables applied to the host CPU */
+#define VCC_FLAG_LEGACY_HANDLING (1 << 0)
+/*
+ * Emulate Intel-style fallback behavior (emit highest "standard" entry) if the
+ * queried function/index do not match. If not set, emulate AMD-style, where
+ * all zeroes are returned in such cases.
+ */
+#define VCC_FLAG_INTEL_FALLBACK (1 << 1)
+
+/* All valid flags for vm_vcpu_cpuid_config`vvcc_flags */
+#define VCC_FLAGS_VALID \
+ (VCC_FLAG_LEGACY_HANDLING | VCC_FLAG_INTEL_FALLBACK)
+
+/* Maximum vcpu_cpuid_entry records per vCPU */
+#define VMM_MAX_CPUID_ENTRIES 256
+
+#ifdef _KERNEL
+typedef struct vcpu_cpuid_config {
+ uint32_t vcc_flags;
+ uint32_t vcc_nent;
+ struct vcpu_cpuid_entry *vcc_entries;
+} vcpu_cpuid_config_t;
+
+vcpu_cpuid_config_t *vm_cpuid_config(struct vcpu *);
+int vm_get_cpuid(struct vcpu *, vcpu_cpuid_config_t *);
+int vm_set_cpuid(struct vcpu *, const vcpu_cpuid_config_t *);
+void vcpu_emulate_cpuid(struct vcpu *, uint64_t *, uint64_t *, uint64_t *,
+ uint64_t *);
+void legacy_emulate_cpuid(struct vcpu *, uint32_t *, uint32_t *, uint32_t *,
+ uint32_t *);
+void vcpu_cpuid_init(vcpu_cpuid_config_t *);
+void vcpu_cpuid_cleanup(vcpu_cpuid_config_t *);
+#endif
+
+
#endif /* _VMM_H_ */
diff --git a/sys/amd64/include/vmm_dev.h b/sys/amd64/include/vmm_dev.h
index 4ed110b526ec..a38eb39d435d 100644
--- a/sys/amd64/include/vmm_dev.h
+++ b/sys/amd64/include/vmm_dev.h
@@ -253,6 +253,23 @@ struct vm_readwrite_kernemu_device {
};
_Static_assert(sizeof(struct vm_readwrite_kernemu_device) == 24, "ABI");
+struct vm_vcpu_cpuid_config {
+ int vvcc_vcpuid;
+ uint32_t vvcc_flags;
+ uint32_t vvcc_nent;
+ uint32_t _pad;
+ void *vvcc_entries;
+};
+
+/* Query the computed legacy cpuid value for a vcpuid with VM_LEGACY_CPUID */
+struct vm_legacy_cpuid {
+ int vlc_vcpuid;
+ uint32_t vlc_eax;
+ uint32_t vlc_ebx;
+ uint32_t vlc_ecx;
+ uint32_t vlc_edx;
+};
+
enum {
/* general routines */
IOCNUM_ABIVERS = 0,
@@ -321,6 +338,11 @@ enum {
IOCNUM_SET_TOPOLOGY = 63,
IOCNUM_GET_TOPOLOGY = 64,
+ /* CPUID override */
+ IOCNUM_GET_CPUID = 65,
+ IOCNUM_SET_CPUID = 66,
+ IOCNUM_LEGACY_CPUID = 67,
+
/* legacy interrupt injection */
IOCNUM_ISA_ASSERT_IRQ = 80,
IOCNUM_ISA_DEASSERT_IRQ = 81,
@@ -439,6 +461,12 @@ enum {
_IOW('v', IOCNUM_SET_TOPOLOGY, struct vm_cpu_topology)
#define VM_GET_TOPOLOGY \
_IOR('v', IOCNUM_GET_TOPOLOGY, struct vm_cpu_topology)
+#define VM_GET_CPUID \
+ _IOWR('v', IOCNUM_GET_CPUID, struct vm_vcpu_cpuid_config)
+#define VM_SET_CPUID \
+ _IOW('v', IOCNUM_SET_CPUID, struct vm_vcpu_cpuid_config)
+#define VM_LEGACY_CPUID \
+ _IOWR('v', IOCNUM_LEGACY_CPUID, struct vm_legacy_cpuid)
#define VM_GET_GPA_PMAP \
_IOWR('v', IOCNUM_GET_GPA_PMAP, struct vm_gpa_pte)
#define VM_GLA2GPA \
diff --git a/sys/amd64/vmm/amd/svm.c b/sys/amd64/vmm/amd/svm.c
index 2ac84e36aa5a..1fd266e658bc 100644
--- a/sys/amd64/vmm/amd/svm.c
+++ b/sys/amd64/vmm/amd/svm.c
@@ -1592,9 +1592,9 @@ svm_vmexit(struct svm_softc *svm_sc, struct svm_vcpu *vcpu,
break;
case VMCB_EXIT_CPUID:
vmm_stat_incr(vcpu->vcpu, VMEXIT_CPUID, 1);
- handled = x86_emulate_cpuid(vcpu->vcpu,
- &state->rax, &ctx->sctx_rbx, &ctx->sctx_rcx,
- &ctx->sctx_rdx);
+ vcpu_emulate_cpuid(vcpu->vcpu, &state->rax,
+ &ctx->sctx_rbx, &ctx->sctx_rcx, &ctx->sctx_rdx);
+ handled = 1;
break;
case VMCB_EXIT_HLT:
vmm_stat_incr(vcpu->vcpu, VMEXIT_HLT, 1);
diff --git a/sys/amd64/vmm/intel/vmx.c b/sys/amd64/vmm/intel/vmx.c
index 92f37d8f0009..e606ef108731 100644
--- a/sys/amd64/vmm/intel/vmx.c
+++ b/sys/amd64/vmm/intel/vmx.c
@@ -1247,17 +1247,6 @@ vmx_vcpu_init(void *vmi, struct vcpu *vcpu1, int vcpuid)
return (vcpu);
}
-static int
-vmx_handle_cpuid(struct vmx_vcpu *vcpu, struct vmxctx *vmxctx)
-{
- int handled;
-
- handled = x86_emulate_cpuid(vcpu->vcpu, (uint64_t *)&vmxctx->guest_rax,
- (uint64_t *)&vmxctx->guest_rbx, (uint64_t *)&vmxctx->guest_rcx,
- (uint64_t *)&vmxctx->guest_rdx);
- return (handled);
-}
-
static __inline void
vmx_run_trace(struct vmx_vcpu *vcpu)
{
@@ -2670,7 +2659,12 @@ vmx_exit_process(struct vmx *vmx, struct vmx_vcpu *vcpu, struct vm_exit *vmexit)
case EXIT_REASON_CPUID:
vmm_stat_incr(vcpu->vcpu, VMEXIT_CPUID, 1);
SDT_PROBE3(vmm, vmx, exit, cpuid, vmx, vcpuid, vmexit);
- handled = vmx_handle_cpuid(vcpu, vmxctx);
+ vcpu_emulate_cpuid(vcpu->vcpu,
+ (uint64_t *)&vmxctx->guest_rax,
+ (uint64_t *)&vmxctx->guest_rbx,
+ (uint64_t *)&vmxctx->guest_rcx,
+ (uint64_t *)&vmxctx->guest_rdx);
+ handled = HANDLED;
break;
case EXIT_REASON_EXCEPTION:
vmm_stat_incr(vcpu->vcpu, VMEXIT_EXCEPTION, 1);
diff --git a/sys/amd64/vmm/vmm.c b/sys/amd64/vmm/vmm.c
index dbfbdc9ff9e7..7525e2ad7b8f 100644
--- a/sys/amd64/vmm/vmm.c
+++ b/sys/amd64/vmm/vmm.c
@@ -86,6 +86,7 @@
#include "vrtc.h"
#include "vmm_stat.h"
#include "vmm_lapic.h"
+#include "x86.h"
#include "io/ppt.h"
#include "io/iommu.h"
@@ -212,6 +213,9 @@ vcpu_cleanup(struct vcpu *vcpu, bool destroy)
vcpu->cookie = NULL;
if (destroy) {
vmm_stat_free(vcpu->stats);
+
+ vcpu_cpuid_cleanup(&vcpu->cpuid_cfg);
+
fpu_save_area_free(vcpu->guestfpu);
vcpu_lock_destroy(vcpu);
free(vcpu, M_VM);
@@ -353,6 +357,8 @@ vm_alloc_vcpu(struct vm *vm, int vcpuid)
vcpu = vm->vcpu[vcpuid];
if (vcpu == NULL && !vm->dying) {
vcpu = vcpu_alloc(vm, vcpuid);
+
+ vcpu_cpuid_init(&vcpu->cpuid_cfg);
vcpu_init(vcpu);
/*
@@ -1650,6 +1656,12 @@ vm_set_capability(struct vcpu *vcpu, int type, int val)
return (vmmops_setcap(vcpu->cookie, type, val));
}
+vcpu_cpuid_config_t *
+vm_cpuid_config(struct vcpu *vcpu)
+{
+ return (&vcpu->cpuid_cfg);
+}
+
struct vlapic *
vm_lapic(struct vcpu *vcpu)
{
diff --git a/sys/amd64/vmm/vmm_cpuid.c b/sys/amd64/vmm/vmm_cpuid.c
new file mode 100644
index 000000000000..568042241243
--- /dev/null
+++ b/sys/amd64/vmm/vmm_cpuid.c
@@ -0,0 +1,961 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Copyright 2014 Pluribus Networks Inc.
+ * Copyright 2018 Joyent, Inc.
+ * Copyright 2022 Oxide Computer Company
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/sysctl.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <machine/clock.h>
+#include <machine/md_var.h>
+#include <machine/specialreg.h>
+#include <x86/bhyve.h>
+
+#include <dev/vmm/vmm_ktr.h>
+#include <dev/vmm/vmm_vm.h>
+
+#include "vmm_host.h"
+#include "vmm_util.h"
+#include "x86.h"
+
+static MALLOC_DEFINE(M_CPUID, "cpuid", "cpuid");
+
+/*
+ * CPUID Emulation
+ *
+ * All CPUID instruction exits are handled by the in-kernel emulation.
+ *
+ * ----------------
+ * Legacy Emulation
+ * ----------------
+ *
+ * Originally, the kernel vmm portion of bhyve relied on fixed logic to filter
+ * and/or generate CPUID results based on what was reported by the host CPU, as
+ * well as attributes of the VM (such as CPU topology, and enabled features).
+ * This is largely adequate to expose CPU capabilities to the guest in manner
+ * which allows it to operate properly.
+ *
+ * ------------------------------
+ * Userspace-Controlled Emulation
+ * ------------------------------
+ *
+ * In certain situations, more control over the CPUID emulation results present
+ * to the guest is desired. Live migration between physical hosts is one such
+ * example, where the underlying CPUs, or at least their microcode, may differ
+ * between the source and destination. In such cases, where changes to the
+ * CPUID results cannot be tolerated, the userspace portion of the VMM can be in
+ * complete control over the leaves which are presented to the guest. It may
+ * still consult the "legacy" CPUID data for guidance about which CPU features
+ * are safe to expose (due to hypervisor limitations, etc). This leaf
+ * information is configured on a per-vCPU basis.
+ *
+ * The emulation entries provided by userspace are expected to be in sorted
+ * order, running from lowest function and index to highest.
+ *
+ * For example:
+ * (func: 00h idx: 00h) ->
+ * (flags: 0, eax: highest std leaf, ebx-edx: vendor id)
+ * (func: 0Dh idx: 00h) ->
+ * (flags: VCE_FLAG_MATCH_INDEX, eax - edx: XCR0/XSAVE info)
+ * (func: 0Dh idx: 01h) ->
+ * (flags: VCE_FLAG_MATCH_INDEX, eax - edx: XSAVE/XSAVEOPT details)
+ * ...
+ * (func: 0Dh idx: 07H) ->
+ * (flags: VCE_FLAG_MATCH_INDEX, eax - edx: AVX-512 details)
+ * (func: 8000000h idx: 0h) ->
+ * (flags: 0, eax: highest extd leaf ...)
+ * ...
+ */
+
+
+#define CPUID_TYPE_MASK 0xf0000000
+#define CPUID_TYPE_STD 0x00000000
+#define CPUID_TYPE_EXTD 0x80000000
+
+static const struct vcpu_cpuid_entry cpuid_empty_entry = { 0 };
+
+/*
+ * Given the CPUID configuration for a vCPU, locate the entry which matches the
+ * provided function/index tuple. The entries list is walked in order, and the
+ * first valid match based on the function/index and flags will be emitted.
+ *
+ * If no match is found, but Intel-style fallback is configured, then the
+ * highest standard leaf encountered will be emitted.
+ */
+static const struct vcpu_cpuid_entry *
+cpuid_find_entry(const vcpu_cpuid_config_t *cfg, uint32_t func, uint32_t idx)
+{
+ const struct vcpu_cpuid_entry *last_std = &cpuid_empty_entry;
+ const bool intel_fallback =
+ (cfg->vcc_flags & VCC_FLAG_INTEL_FALLBACK) != 0;
+ bool matched_leaf = false;
+
+ KASSERT((cfg->vcc_flags & VCC_FLAG_LEGACY_HANDLING) == 0,
+ ("legacy CPUID handling enabled"));
+
+ for (u_int i = 0; i < cfg->vcc_nent; i++) {
+ const struct vcpu_cpuid_entry *ent = &cfg->vcc_entries[i];
+ const bool ent_is_std =
+ (ent->vce_function & CPUID_TYPE_MASK) == CPUID_TYPE_STD;
+ const bool ent_must_match_idx =
+ (ent->vce_flags & VCE_FLAG_MATCH_INDEX) != 0;
+
+ if (ent_is_std) {
+ /*
+ * Keep track of the last "standard" leaf for
+ * Intel-style fallback behavior.
+ *
+ * This does currently not account for the sub-leaf
+ * index matching behavior for fallback described in the
+ * SDM. It is not clear if any consumers rely on such
+ * matching when encountering fallback.
+ */
+ last_std = ent;
+ }
+ if (ent->vce_function == func) {
+ if (ent->vce_index == idx || !ent_must_match_idx) {
+ return (ent);
+ }
+ /*
+ * Make note of when the top-level leaf matches, even
+ * when the index does not.
+ */
+ matched_leaf = true;
+ } else if (ent->vce_function > func) {
+ if ((ent->vce_function & CPUID_TYPE_MASK) ==
+ (func & CPUID_TYPE_MASK)) {
+ /*
+ * We are beyond a valid leaf to match, but have
+ * not exceeded the maximum leaf for this "type"
+ * (standard, extended, hvm, etc), so return an
+ * empty entry.
+ */
+ return (&cpuid_empty_entry);
+ } else {
+ /*
+ * Otherwise, we can stop now, having gone
+ * beyond the last entry which could match the
+ * target function in a sorted list.
+ */
+ break;
+ }
+ }
+ }
+
+ if (matched_leaf || !intel_fallback) {
+ return (&cpuid_empty_entry);
+ } else {
+ return (last_std);
+ }
+}
+
+void
+vcpu_emulate_cpuid(struct vcpu *vcpu, uint64_t *rax, uint64_t *rbx,
+ uint64_t *rcx, uint64_t *rdx)
+{
+ const vcpu_cpuid_config_t *cfg = vm_cpuid_config(vcpu);
+
+ KASSERT(rax != NULL, ("rax == NULL"));
+ KASSERT(rbx != NULL, ("rbx == NULL"));
+ KASSERT(rcx != NULL, ("rcx == NULL"));
+ KASSERT(rdx != NULL, ("rdx == NULL"));
+
+ /* Fall back to legacy handling if specified */
+ if ((cfg->vcc_flags & VCC_FLAG_LEGACY_HANDLING) != 0) {
+ uint32_t regs[4] = { *rax, 0, *rcx, 0 };
+
+ legacy_emulate_cpuid(vcpu, ®s[0], ®s[1], ®s[2],
+ ®s[3]);
+ /* CPUID clears the upper 32-bits of the long-mode registers. */
+ *rax = regs[0];
+ *rbx = regs[1];
+ *rcx = regs[2];
+ *rdx = regs[3];
+ return;
+ }
+
+ const struct vcpu_cpuid_entry *ent = cpuid_find_entry(cfg, *rax, *rcx);
+ KASSERT(ent != NULL, ("ent == NULL"));
+ /* CPUID clears the upper 32-bits of the long-mode registers. */
+ *rax = ent->vce_eax;
+ *rbx = ent->vce_ebx;
+ *rcx = ent->vce_ecx;
+ *rdx = ent->vce_edx;
+}
+
+/*
+ * Get the current CPUID emulation configuration for this vCPU.
+ *
+ * Only the existing flags will be emitted if the vCPU is configured for legacy
+ * operation via the VCC_FLAG_LEGACY_HANDLING flag. If in userspace-controlled
+ * mode, then we will attempt to copy the existing entries into vcc_entries,
+ * its side specified by vcc_nent.
+ *
+ * Regardless of whether vcc_entries is adequately sized (or even present),
+ * vcc_nent will be set to the number of existing entries.
+ */
+int
+vm_get_cpuid(struct vcpu *vcpu, vcpu_cpuid_config_t *res)
+{
+ const vcpu_cpuid_config_t *src = vm_cpuid_config(vcpu);
+ if (src->vcc_nent > res->vcc_nent) {
+ res->vcc_nent = src->vcc_nent;
+ return (E2BIG);
+ } else if (src->vcc_nent != 0) {
+ bcopy(src->vcc_entries, res->vcc_entries,
+ src->vcc_nent * sizeof(struct vcpu_cpuid_entry));
+ }
+ res->vcc_flags = src->vcc_flags;
+ res->vcc_nent = src->vcc_nent;
+ return (0);
+}
+
+/*
+ * Set the CPUID emulation configuration for this vCPU.
+ *
+ * If VCC_FLAG_LEGACY_HANDLING is set in vcc_flags, then vcc_nent is expected to
+ * be set to 0, as configuring a list of entries would be useless when using the
+ * legacy handling.
+ *
+ * Any existing entries which are configured are freed, and the newly provided
+ * ones will be copied into their place.
+ */
+int
+vm_set_cpuid(struct vcpu *vcpu, const vcpu_cpuid_config_t *src)
+{
+ if (src->vcc_nent > VMM_MAX_CPUID_ENTRIES) {
+ return (EINVAL);
+ }
+ if ((src->vcc_flags & ~VCC_FLAGS_VALID) != 0) {
+ return (EINVAL);
+ }
+ if ((src->vcc_flags & VCC_FLAG_LEGACY_HANDLING) != 0 &&
+ src->vcc_nent != 0) {
+ /* No entries should be provided if using legacy handling */
+ return (EINVAL);
+ }
+ for (u_int i = 0; i < src->vcc_nent; i++) {
+ /* Ensure all entries carry valid flags */
+ if ((src->vcc_entries[i].vce_flags & ~VCE_FLAGS_VALID) != 0) {
+ return (EINVAL);
+ }
+ }
+
+ vcpu_cpuid_config_t *cfg = vm_cpuid_config(vcpu);
+
+ /* Free any existing entries first */
+ vcpu_cpuid_cleanup(cfg);
+
+ /* Copy supplied entries into freshly allocated space */
+ if (src->vcc_nent != 0) {
+ const size_t entries_sz =
+ src->vcc_nent * sizeof(struct vcpu_cpuid_entry);
+
+ cfg->vcc_nent = src->vcc_nent;
+ cfg->vcc_entries = malloc(entries_sz, M_CPUID, M_WAITOK);
+ bcopy(src->vcc_entries, cfg->vcc_entries, entries_sz);
+ }
+ cfg->vcc_flags = src->vcc_flags;
+
+ return (0);
+}
+
+void
+vcpu_cpuid_init(vcpu_cpuid_config_t *cfg)
+{
+ /* Default to legacy-style handling */
+ cfg->vcc_flags = VCC_FLAG_LEGACY_HANDLING;
+ cfg->vcc_nent = 0;
+ cfg->vcc_entries = NULL;
+}
+
+void
+vcpu_cpuid_cleanup(vcpu_cpuid_config_t *cfg)
+{
+ if (cfg->vcc_nent != 0) {
+ KASSERT(cfg->vcc_entries != NULL, ("cfg->vcc_entries == NULL"));
+
+ free(cfg->vcc_entries, M_CPUID);
+
+ cfg->vcc_nent = 0;
+ cfg->vcc_entries = NULL;
+ }
+}
+
+SYSCTL_DECL(_hw_vmm);
+static SYSCTL_NODE(_hw_vmm, OID_AUTO, topology, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
+ NULL);
+
+static uint64_t bhyve_xcpuids;
+SYSCTL_ULONG(_hw_vmm, OID_AUTO, bhyve_xcpuids, CTLFLAG_RW, &bhyve_xcpuids, 0,
+ "Number of times an unknown cpuid leaf was accessed");
+
+static int cpuid_leaf_b = 1;
+SYSCTL_INT(_hw_vmm_topology, OID_AUTO, cpuid_leaf_b, CTLFLAG_RDTUN,
+ &cpuid_leaf_b, 0, NULL);
+
+static const char bhyve_id[12] = "bhyve bhyve ";
+
+#define CPUID_0000_0000 (0x0)
+#define CPUID_0000_0001 (0x1)
+#define CPUID_0000_0002 (0x2)
+#define CPUID_0000_0003 (0x3)
+#define CPUID_0000_0004 (0x4)
+#define CPUID_0000_0006 (0x6)
+#define CPUID_0000_0007 (0x7)
+#define CPUID_0000_000A (0xA)
+#define CPUID_0000_000B (0xB)
+#define CPUID_0000_000D (0xD)
+#define CPUID_0000_000F (0xF)
+#define CPUID_0000_0010 (0x10)
+#define CPUID_0000_0015 (0x15)
+#define CPUID_8000_0000 (0x80000000)
+#define CPUID_8000_0001 (0x80000001)
+#define CPUID_8000_0002 (0x80000002)
+#define CPUID_8000_0003 (0x80000003)
+#define CPUID_8000_0004 (0x80000004)
+#define CPUID_8000_0006 (0x80000006)
+#define CPUID_8000_0007 (0x80000007)
+#define CPUID_8000_0008 (0x80000008)
+#define CPUID_8000_001D (0x8000001D)
+#define CPUID_8000_001E (0x8000001E)
+
+#define CPUID_VM_SIGNATURE 0x40000000
+#define CPUID_VM_HIGH CPUID_BHYVE_FEATURES
+
+/*
+ * CPUID instruction Fn0000_0001:
+ */
+#define CPUID_0000_0001_APICID_SHIFT 24
+
+/*
+ * Compute ceil(log2(x)). Returns -1 if x is zero.
+ */
+static __inline int
+log2(u_int x)
+{
+
+ return (x == 0 ? -1 : order_base_2(x));
+}
+
+/*
+ * The "legacy" bhyve cpuid emulation, which largly applies statically defined
+ * masks to the data provided by the host CPU.
+ */
+void
+legacy_emulate_cpuid(struct vcpu *vcpu, uint32_t *eax, uint32_t *ebx,
+ uint32_t *ecx, uint32_t *edx)
+{
+ struct vm *vm = vcpu_vm(vcpu);
+ int vcpu_id = vcpu_vcpuid(vcpu);
+ const struct xsave_limits *limits;
+ uint64_t cr4;
+ int error, enable_invpcid, enable_rdpid, enable_rdtscp, level,
+ width, x2apic_id;
+ unsigned int func, regs[4], logical_cpus, param;
+ enum x2apic_state x2apic_state;
+ uint16_t cores, maxcpus, sockets, threads;
+
+ /*
+ * The function of CPUID is controlled through the provided value of
+ * %eax (and secondarily %ecx, for certain leaf data).
+ */
+ func = (uint32_t)*eax;
+ param = (uint32_t)*ecx;
+
+ VCPU_CTR2(vm, vcpu_id, "cpuid %#x,%#x", func, param);
+
+ /*
+ * Requests for invalid CPUID levels should map to the highest
+ * available level instead.
+ */
+ if (cpu_exthigh != 0 && func >= 0x80000000) {
+ if (func > cpu_exthigh)
+ func = cpu_exthigh;
+ } else if (func >= CPUID_VM_SIGNATURE) {
+ if (func > CPUID_VM_HIGH)
+ func = CPUID_VM_HIGH;
+ } else if (func > cpu_high) {
+ func = cpu_high;
+ }
+
+ /*
+ * In general the approach used for CPU topology is to
+ * advertise a flat topology where all CPUs are packages with
+ * no multi-core or SMT.
+ */
+ switch (func) {
+ /*
+ * Pass these through to the guest
+ */
+ case CPUID_0000_0000:
+ case CPUID_0000_0002:
+ case CPUID_0000_0003:
+ case CPUID_8000_0000:
+ case CPUID_8000_0002:
+ case CPUID_8000_0003:
+ case CPUID_8000_0004:
+ case CPUID_8000_0006:
+ cpuid_count(func, param, regs);
+ break;
+ case CPUID_8000_0008:
+ cpuid_count(func, param, regs);
+ if (vmm_is_svm()) {
+ /*
+ * As on Intel (0000_0007:0, EDX), mask out
+ * unsupported or unsafe AMD extended features
+ * (8000_0008 EBX).
+ */
+ regs[1] &= AMDFEID_CLZERO | AMDFEID_XSAVEERPTR;
+
+ vm_get_topology(vm, &sockets, &cores, &threads,
+ &maxcpus);
+ /*
+ * Here, width is ApicIdCoreIdSize, present on
+ * at least Family 15h and newer. It
+ * represents the "number of bits in the
+ * initial apicid that indicate thread id
+ * within a package."
+ *
+ * Our topo_probe_amd() uses it for
+ * pkg_id_shift and other OSes may rely on it.
+ */
+ width = MIN(0xF, log2(threads * cores));
+ logical_cpus = MIN(0xFF, threads * cores - 1);
+ regs[2] = (width << AMDID_COREID_SIZE_SHIFT) |
+ logical_cpus;
+ }
+ break;
+
+ case CPUID_8000_0001:
+ cpuid_count(func, param, regs);
+
+ /*
+ * Hide SVM from guest.
+ */
+ regs[2] &= ~AMDID2_SVM;
+
+ /*
+ * Don't advertise extended performance counter MSRs
+ * to the guest.
+ */
+ regs[2] &= ~AMDID2_PCXC;
+ regs[2] &= ~AMDID2_PNXC;
+ regs[2] &= ~AMDID2_PTSCEL2I;
+
+ /*
+ * Don't advertise Instruction Based Sampling feature.
+ */
+ regs[2] &= ~AMDID2_IBS;
+
+ /* NodeID MSR not available */
+ regs[2] &= ~AMDID2_NODE_ID;
+
+ /* Don't advertise the OS visible workaround feature */
+ regs[2] &= ~AMDID2_OSVW;
+
+ /* Hide mwaitx/monitorx capability from the guest */
+ regs[2] &= ~AMDID2_MWAITX;
+
+ /* Advertise RDTSCP if it is enabled. */
+ error = vm_get_capability(vcpu,
+ VM_CAP_RDTSCP, &enable_rdtscp);
+ if (error == 0 && enable_rdtscp)
+ regs[3] |= AMDID_RDTSCP;
+ else
+ regs[3] &= ~AMDID_RDTSCP;
+ break;
+
+ case CPUID_8000_0007:
+ cpuid_count(func, param, regs);
+ /*
+ * AMD uses this leaf to advertise the processor's
+ * power monitoring and RAS capabilities. These
+ * features are hardware-specific and exposing
+ * them to a guest doesn't make a lot of sense.
+ *
+ * Intel uses this leaf only to advertise the
+ * "Invariant TSC" feature with all other bits
+ * being reserved (set to zero).
+ */
+ regs[0] = 0;
+ regs[1] = 0;
+ regs[2] = 0;
+ regs[3] = 0;
+
+ /*
+ * "Invariant TSC" can be advertised to the guest if:
+ * - host TSC frequency is invariant
+ * - host TSCs are synchronized across physical cpus
+ *
+ * XXX This still falls short because the vcpu
+ * can observe the TSC moving backwards as it
+ * migrates across physical cpus. But at least
+ * it should discourage the guest from using the
+ * TSC to keep track of time.
+ */
+ if (tsc_is_invariant && smp_tsc)
+ regs[3] |= AMDPM_TSC_INVARIANT;
+ break;
+
+ case CPUID_8000_001D:
+ /* AMD Cache topology, like 0000_0004 for Intel. */
+ if (!vmm_is_svm())
+ goto default_leaf;
+
+ /*
+ * Similar to Intel, generate a fictitious cache
+ * topology for the guest with L3 shared by the
+ * package, and L1 and L2 local to a core.
+ */
+ vm_get_topology(vm, &sockets, &cores, &threads,
+ &maxcpus);
+ switch (param) {
+ case 0:
+ logical_cpus = threads;
+ level = 1;
+ func = 1; /* data cache */
+ break;
+ case 1:
+ logical_cpus = threads;
+ level = 2;
+ func = 3; /* unified cache */
+ break;
+ case 2:
+ logical_cpus = threads * cores;
+ level = 3;
+ func = 3; /* unified cache */
+ break;
+ default:
+ logical_cpus = sockets * threads * cores;
+ level = 0;
+ func = 0;
+ break;
+ }
+
+ logical_cpus = MIN(0xfff, logical_cpus - 1);
+ regs[0] = (logical_cpus << 14) | (1 << 8) |
+ (level << 5) | func;
+ regs[1] = func > 0 ? CACHE_LINE_SIZE - 1 : 0;
+
+ /*
+ * ecx: Number of cache ways for non-fully
+ * associative cache, minus 1. Reported value
+ * of zero means there is one way.
+ */
+ regs[2] = 0;
+
+ regs[3] = 0;
+ break;
+
+ case CPUID_8000_001E:
+ /*
+ * AMD Family 16h+ and Hygon Family 18h additional
+ * identifiers.
+ */
+ if (!vmm_is_svm() || CPUID_TO_FAMILY(cpu_id) < 0x16)
+ goto default_leaf;
+
+ vm_get_topology(vm, &sockets, &cores, &threads,
+ &maxcpus);
+ regs[0] = vcpu_id;
+ threads = MIN(0xFF, threads - 1);
+ regs[1] = (threads << 8) |
+ (vcpu_id >> log2(threads + 1));
+ /*
+ * XXX Bhyve topology cannot yet represent >1 node per
+ * processor.
+ */
+ regs[2] = 0;
+ regs[3] = 0;
+ break;
+
+ case CPUID_8000_0022:
+ /*
+ * AMD uses this leaf to advertise performance
+ * counters and PerfMonV2. vmm implements neither.
+ */
+ regs[0] = 0;
+ regs[1] = 0;
+ regs[2] = 0;
+ regs[3] = 0;
+ break;
+
+ case CPUID_0000_0001:
+ do_cpuid(1, regs);
+
+ error = vm_get_x2apic_state(vcpu, &x2apic_state);
+ if (error) {
+ panic("x86_emulate_cpuid: error %d "
+ "fetching x2apic state", error);
+ }
+
+ /*
+ * Override the APIC ID only in ebx
+ */
+ regs[1] &= ~(CPUID_LOCAL_APIC_ID);
+ regs[1] |= (vcpu_id << CPUID_0000_0001_APICID_SHIFT);
+
+ /*
+ * Don't expose VMX, SpeedStep, TME or SMX capability.
+ * Advertise x2APIC capability and Hypervisor guest.
+ */
+ regs[2] &= ~(CPUID2_VMX | CPUID2_EST | CPUID2_TM2);
+ regs[2] &= ~(CPUID2_SMX);
+
+ regs[2] |= CPUID2_HV;
+
+ if (x2apic_state != X2APIC_DISABLED)
+ regs[2] |= CPUID2_X2APIC;
+ else
+ regs[2] &= ~CPUID2_X2APIC;
+
+ /*
+ * Only advertise CPUID2_XSAVE in the guest if
+ * the host is using XSAVE.
+ */
+ if (!(regs[2] & CPUID2_OSXSAVE))
+ regs[2] &= ~CPUID2_XSAVE;
+
+ /*
+ * If CPUID2_XSAVE is being advertised and the
+ * guest has set CR4_XSAVE, set
+ * CPUID2_OSXSAVE.
+ */
+ regs[2] &= ~CPUID2_OSXSAVE;
+ if (regs[2] & CPUID2_XSAVE) {
+ error = vm_get_register(vcpu,
+ VM_REG_GUEST_CR4, &cr4);
+ if (error)
+ panic("x86_emulate_cpuid: error %d "
+ "fetching %%cr4", error);
+ if (cr4 & CR4_XSAVE)
+ regs[2] |= CPUID2_OSXSAVE;
+ }
+
+ /*
+ * Hide monitor/mwait until we know how to deal with
+ * these instructions.
+ */
+ regs[2] &= ~CPUID2_MON;
+
+ /*
+ * Hide the performance and debug features.
+ */
+ regs[2] &= ~CPUID2_PDCM;
+
+ /*
+ * No TSC deadline support in the APIC yet
+ */
+ regs[2] &= ~CPUID2_TSCDLT;
+
+ /*
+ * Hide thermal monitoring
+ */
+ regs[3] &= ~(CPUID_ACPI | CPUID_TM);
+
+ /*
+ * Hide the debug store capability.
+ */
+ regs[3] &= ~CPUID_DS;
+
+ /*
+ * Advertise the Machine Check and MTRR capability.
+ *
+ * Some guest OSes (e.g. Windows) will not boot if
+ * these features are absent.
+ */
+ regs[3] |= (CPUID_MCA | CPUID_MCE | CPUID_MTRR);
+
+ vm_get_topology(vm, &sockets, &cores, &threads,
+ &maxcpus);
+ logical_cpus = threads * cores;
+ regs[1] &= ~CPUID_HTT_CORES;
+ regs[1] |= (logical_cpus & 0xff) << 16;
+ regs[3] |= CPUID_HTT;
+ break;
*** 1059 LINES SKIPPED ***