2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysctl.h>
39 #include <machine/clock.h>
40 #include <machine/cpufunc.h>
41 #include <machine/md_var.h>
42 #include <machine/segments.h>
43 #include <machine/specialreg.h>
45 #include <machine/vmm.h>
53 static SYSCTL_NODE(_hw_vmm, OID_AUTO, topology, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
56 #define CPUID_VM_HIGH 0x40000000
58 static const char bhyve_id[12] = "bhyve bhyve ";
60 static uint64_t bhyve_xcpuids;
61 SYSCTL_ULONG(_hw_vmm, OID_AUTO, bhyve_xcpuids, CTLFLAG_RW, &bhyve_xcpuids, 0,
62 "Number of times an unknown cpuid leaf was accessed");
64 #if __FreeBSD_version < 1200060 /* Remove after 11 EOL helps MFCing */
65 extern u_int threads_per_core;
66 SYSCTL_UINT(_hw_vmm_topology, OID_AUTO, threads_per_core, CTLFLAG_RDTUN,
67 &threads_per_core, 0, NULL);
69 extern u_int cores_per_package;
70 SYSCTL_UINT(_hw_vmm_topology, OID_AUTO, cores_per_package, CTLFLAG_RDTUN,
71 &cores_per_package, 0, NULL);
74 static int cpuid_leaf_b = 1;
75 SYSCTL_INT(_hw_vmm_topology, OID_AUTO, cpuid_leaf_b, CTLFLAG_RDTUN,
76 &cpuid_leaf_b, 0, NULL);
79 * Round up to the next power of two, if necessary, and then take log2.
80 * Returns -1 if argument is zero.
86 return (fls(x << (1 - powerof2(x))) - 1);
90 x86_emulate_cpuid(struct vm *vm, int vcpu_id,
91 uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
93 const struct xsave_limits *limits;
95 int error, enable_invpcid, level, width, x2apic_id;
96 unsigned int func, regs[4], logical_cpus;
97 enum x2apic_state x2apic_state;
98 uint16_t cores, maxcpus, sockets, threads;
100 VCPU_CTR2(vm, vcpu_id, "cpuid %#x,%#x", *eax, *ecx);
103 * Requests for invalid CPUID levels should map to the highest
104 * available level instead.
106 if (cpu_exthigh != 0 && *eax >= 0x80000000) {
107 if (*eax > cpu_exthigh)
109 } else if (*eax >= 0x40000000) {
110 if (*eax > CPUID_VM_HIGH)
111 *eax = CPUID_VM_HIGH;
112 } else if (*eax > cpu_high) {
119 * In general the approach used for CPU topology is to
120 * advertise a flat topology where all CPUs are packages with
121 * no multi-core or SMT.
125 * Pass these through to the guest
127 case CPUID_0000_0000:
128 case CPUID_0000_0002:
129 case CPUID_0000_0003:
130 case CPUID_8000_0000:
131 case CPUID_8000_0002:
132 case CPUID_8000_0003:
133 case CPUID_8000_0004:
134 case CPUID_8000_0006:
135 cpuid_count(*eax, *ecx, regs);
137 case CPUID_8000_0008:
138 cpuid_count(*eax, *ecx, regs);
141 * As on Intel (0000_0007:0, EDX), mask out
142 * unsupported or unsafe AMD extended features
145 regs[1] &= (AMDFEID_CLZERO | AMDFEID_IRPERF |
148 vm_get_topology(vm, &sockets, &cores, &threads,
151 * Here, width is ApicIdCoreIdSize, present on
152 * at least Family 15h and newer. It
153 * represents the "number of bits in the
154 * initial apicid that indicate thread id
157 * Our topo_probe_amd() uses it for
158 * pkg_id_shift and other OSes may rely on it.
160 width = MIN(0xF, log2(threads * cores));
163 logical_cpus = MIN(0xFF, threads * cores - 1);
164 regs[2] = (width << AMDID_COREID_SIZE_SHIFT) | logical_cpus;
168 case CPUID_8000_0001:
169 cpuid_count(*eax, *ecx, regs);
172 * Hide SVM from guest.
174 regs[2] &= ~AMDID2_SVM;
177 * Don't advertise extended performance counter MSRs
180 regs[2] &= ~AMDID2_PCXC;
181 regs[2] &= ~AMDID2_PNXC;
182 regs[2] &= ~AMDID2_PTSCEL2I;
185 * Don't advertise Instruction Based Sampling feature.
187 regs[2] &= ~AMDID2_IBS;
189 /* NodeID MSR not available */
190 regs[2] &= ~AMDID2_NODE_ID;
192 /* Don't advertise the OS visible workaround feature */
193 regs[2] &= ~AMDID2_OSVW;
195 /* Hide mwaitx/monitorx capability from the guest */
196 regs[2] &= ~AMDID2_MWAITX;
199 * Hide rdtscp/ia32_tsc_aux until we know how
202 regs[3] &= ~AMDID_RDTSCP;
205 case CPUID_8000_0007:
207 * AMD uses this leaf to advertise the processor's
208 * power monitoring and RAS capabilities. These
209 * features are hardware-specific and exposing
210 * them to a guest doesn't make a lot of sense.
212 * Intel uses this leaf only to advertise the
213 * "Invariant TSC" feature with all other bits
214 * being reserved (set to zero).
222 * "Invariant TSC" can be advertised to the guest if:
223 * - host TSC frequency is invariant
224 * - host TSCs are synchronized across physical cpus
226 * XXX This still falls short because the vcpu
227 * can observe the TSC moving backwards as it
228 * migrates across physical cpus. But at least
229 * it should discourage the guest from using the
230 * TSC to keep track of time.
232 if (tsc_is_invariant && smp_tsc)
233 regs[3] |= AMDPM_TSC_INVARIANT;
236 case CPUID_8000_001D:
237 /* AMD Cache topology, like 0000_0004 for Intel. */
242 * Similar to Intel, generate a ficticious cache
243 * topology for the guest with L3 shared by the
244 * package, and L1 and L2 local to a core.
246 vm_get_topology(vm, &sockets, &cores, &threads,
250 logical_cpus = threads;
252 func = 1; /* data cache */
255 logical_cpus = threads;
257 func = 3; /* unified cache */
260 logical_cpus = threads * cores;
262 func = 3; /* unified cache */
271 logical_cpus = MIN(0xfff, logical_cpus - 1);
272 regs[0] = (logical_cpus << 14) | (1 << 8) |
274 regs[1] = (func > 0) ? (CACHE_LINE_SIZE - 1) : 0;
279 case CPUID_8000_001E:
281 * AMD Family 16h+ and Hygon Family 18h additional
284 if (!vmm_is_svm() || CPUID_TO_FAMILY(cpu_id) < 0x16)
287 vm_get_topology(vm, &sockets, &cores, &threads,
290 threads = MIN(0xFF, threads - 1);
291 regs[1] = (threads << 8) |
292 (vcpu_id >> log2(threads + 1));
294 * XXX Bhyve topology cannot yet represent >1 node per
301 case CPUID_0000_0001:
304 error = vm_get_x2apic_state(vm, vcpu_id, &x2apic_state);
306 panic("x86_emulate_cpuid: error %d "
307 "fetching x2apic state", error);
311 * Override the APIC ID only in ebx
313 regs[1] &= ~(CPUID_LOCAL_APIC_ID);
314 regs[1] |= (vcpu_id << CPUID_0000_0001_APICID_SHIFT);
317 * Don't expose VMX, SpeedStep, TME or SMX capability.
318 * Advertise x2APIC capability and Hypervisor guest.
320 regs[2] &= ~(CPUID2_VMX | CPUID2_EST | CPUID2_TM2);
321 regs[2] &= ~(CPUID2_SMX);
323 regs[2] |= CPUID2_HV;
325 if (x2apic_state != X2APIC_DISABLED)
326 regs[2] |= CPUID2_X2APIC;
328 regs[2] &= ~CPUID2_X2APIC;
331 * Only advertise CPUID2_XSAVE in the guest if
332 * the host is using XSAVE.
334 if (!(regs[2] & CPUID2_OSXSAVE))
335 regs[2] &= ~CPUID2_XSAVE;
338 * If CPUID2_XSAVE is being advertised and the
339 * guest has set CR4_XSAVE, set
342 regs[2] &= ~CPUID2_OSXSAVE;
343 if (regs[2] & CPUID2_XSAVE) {
344 error = vm_get_register(vm, vcpu_id,
345 VM_REG_GUEST_CR4, &cr4);
347 panic("x86_emulate_cpuid: error %d "
348 "fetching %%cr4", error);
350 regs[2] |= CPUID2_OSXSAVE;
354 * Hide monitor/mwait until we know how to deal with
355 * these instructions.
357 regs[2] &= ~CPUID2_MON;
360 * Hide the performance and debug features.
362 regs[2] &= ~CPUID2_PDCM;
365 * No TSC deadline support in the APIC yet
367 regs[2] &= ~CPUID2_TSCDLT;
370 * Hide thermal monitoring
372 regs[3] &= ~(CPUID_ACPI | CPUID_TM);
375 * Hide the debug store capability.
377 regs[3] &= ~CPUID_DS;
380 * Advertise the Machine Check and MTRR capability.
382 * Some guest OSes (e.g. Windows) will not boot if
383 * these features are absent.
385 regs[3] |= (CPUID_MCA | CPUID_MCE | CPUID_MTRR);
387 vm_get_topology(vm, &sockets, &cores, &threads,
389 logical_cpus = threads * cores;
390 regs[1] &= ~CPUID_HTT_CORES;
391 regs[1] |= (logical_cpus & 0xff) << 16;
392 regs[3] |= CPUID_HTT;
395 case CPUID_0000_0004:
396 cpuid_count(*eax, *ecx, regs);
398 if (regs[0] || regs[1] || regs[2] || regs[3]) {
399 vm_get_topology(vm, &sockets, &cores, &threads,
402 regs[0] |= (cores - 1) << 26;
405 * - L1 and L2 are shared only by the logical
406 * processors in a single core.
407 * - L3 and above are shared by all logical
408 * processors in the package.
410 logical_cpus = threads;
411 level = (regs[0] >> 5) & 0x7;
413 logical_cpus *= cores;
414 regs[0] |= (logical_cpus - 1) << 14;
418 case CPUID_0000_0007:
426 cpuid_count(*eax, *ecx, regs);
428 /* Only leaf 0 is supported */
432 * Expose known-safe features.
434 regs[1] &= (CPUID_STDEXT_FSGSBASE |
435 CPUID_STDEXT_BMI1 | CPUID_STDEXT_HLE |
436 CPUID_STDEXT_AVX2 | CPUID_STDEXT_BMI2 |
437 CPUID_STDEXT_ERMS | CPUID_STDEXT_RTM |
438 CPUID_STDEXT_AVX512F |
439 CPUID_STDEXT_RDSEED |
440 CPUID_STDEXT_AVX512PF |
441 CPUID_STDEXT_AVX512ER |
442 CPUID_STDEXT_AVX512CD | CPUID_STDEXT_SHA);
444 regs[3] &= CPUID_STDEXT3_MD_CLEAR;
446 /* Advertise INVPCID if it is enabled. */
447 error = vm_get_capability(vm, vcpu_id,
448 VM_CAP_ENABLE_INVPCID, &enable_invpcid);
449 if (error == 0 && enable_invpcid)
450 regs[1] |= CPUID_STDEXT_INVPCID;
454 case CPUID_0000_0006:
455 regs[0] = CPUTPM1_ARAT;
461 case CPUID_0000_000A:
463 * Handle the access, but report 0 for
472 case CPUID_0000_000B:
474 * Intel processor topology enumeration
476 if (vmm_is_intel()) {
477 vm_get_topology(vm, &sockets, &cores, &threads,
480 logical_cpus = threads;
481 width = log2(logical_cpus);
482 level = CPUID_TYPE_SMT;
487 logical_cpus = threads * cores;
488 width = log2(logical_cpus);
489 level = CPUID_TYPE_CORE;
493 if (!cpuid_leaf_b || *ecx >= 2) {
500 regs[0] = width & 0x1f;
501 regs[1] = logical_cpus & 0xffff;
502 regs[2] = (level << 8) | (*ecx & 0xff);
512 case CPUID_0000_000D:
513 limits = vmm_get_xsave_limits();
514 if (!limits->xsave_enabled) {
522 cpuid_count(*eax, *ecx, regs);
526 * Only permit the guest to use bits
527 * that are active in the host in
528 * %xcr0. Also, claim that the
529 * maximum save area size is
530 * equivalent to the host's current
531 * save area size. Since this runs
532 * "inside" of vmrun(), it runs with
533 * the guest's xcr0, so the current
534 * save area size is correct as-is.
536 regs[0] &= limits->xcr0_allowed;
537 regs[2] = limits->xsave_max_size;
538 regs[3] &= (limits->xcr0_allowed >> 32);
541 /* Only permit XSAVEOPT. */
542 regs[0] &= CPUID_EXTSTATE_XSAVEOPT;
549 * If the leaf is for a permitted feature,
550 * pass through as-is, otherwise return
553 if (!(limits->xcr0_allowed & (1ul << *ecx))) {
563 case CPUID_0000_0015:
565 * Don't report CPU TSC/Crystal ratio and clock
566 * values since guests may use these to derive the
567 * local APIC frequency..
576 regs[0] = CPUID_VM_HIGH;
577 bcopy(bhyve_id, ®s[1], 4);
578 bcopy(bhyve_id + 4, ®s[2], 4);
579 bcopy(bhyve_id + 8, ®s[3], 4);
585 * The leaf value has already been clamped so
586 * simply pass this through, keeping count of
587 * how many unhandled leaf values have been seen.
589 atomic_add_long(&bhyve_xcpuids, 1);
590 cpuid_count(*eax, *ecx, regs);
603 vm_cpuid_capability(struct vm *vm, int vcpuid, enum vm_cpuid_capability cap)
607 KASSERT(cap > 0 && cap < VCC_LAST, ("%s: invalid vm_cpu_capability %d",
611 * Simply passthrough the capabilities of the host cpu for now.
616 if (amd_feature & AMDID_NX)
620 if (amd_feature & AMDID_FFXSR)
624 if (amd_feature2 & AMDID2_TCE)
628 panic("%s: unknown vm_cpu_capability %d", __func__, cap);