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, uint64_t *rax, uint64_t *rbx,
91 uint64_t *rcx, uint64_t *rdx)
93 const struct xsave_limits *limits;
95 int error, enable_invpcid, enable_rdpid, enable_rdtscp, level,
97 unsigned int func, regs[4], logical_cpus, param;
98 enum x2apic_state x2apic_state;
99 uint16_t cores, maxcpus, sockets, threads;
102 * The function of CPUID is controlled through the provided value of
103 * %eax (and secondarily %ecx, for certain leaf data).
105 func = (uint32_t)*rax;
106 param = (uint32_t)*rcx;
108 VCPU_CTR2(vm, vcpu_id, "cpuid %#x,%#x", func, param);
111 * Requests for invalid CPUID levels should map to the highest
112 * available level instead.
114 if (cpu_exthigh != 0 && func >= 0x80000000) {
115 if (func > cpu_exthigh)
117 } else if (func >= 0x40000000) {
118 if (func > CPUID_VM_HIGH)
119 func = CPUID_VM_HIGH;
120 } else if (func > cpu_high) {
125 * In general the approach used for CPU topology is to
126 * advertise a flat topology where all CPUs are packages with
127 * no multi-core or SMT.
131 * Pass these through to the guest
133 case CPUID_0000_0000:
134 case CPUID_0000_0002:
135 case CPUID_0000_0003:
136 case CPUID_8000_0000:
137 case CPUID_8000_0002:
138 case CPUID_8000_0003:
139 case CPUID_8000_0004:
140 case CPUID_8000_0006:
141 cpuid_count(func, param, regs);
143 case CPUID_8000_0008:
144 cpuid_count(func, param, regs);
147 * As on Intel (0000_0007:0, EDX), mask out
148 * unsupported or unsafe AMD extended features
151 regs[1] &= (AMDFEID_CLZERO | AMDFEID_IRPERF |
154 vm_get_topology(vm, &sockets, &cores, &threads,
157 * Here, width is ApicIdCoreIdSize, present on
158 * at least Family 15h and newer. It
159 * represents the "number of bits in the
160 * initial apicid that indicate thread id
163 * Our topo_probe_amd() uses it for
164 * pkg_id_shift and other OSes may rely on it.
166 width = MIN(0xF, log2(threads * cores));
169 logical_cpus = MIN(0xFF, threads * cores - 1);
170 regs[2] = (width << AMDID_COREID_SIZE_SHIFT) | logical_cpus;
174 case CPUID_8000_0001:
175 cpuid_count(func, param, regs);
178 * Hide SVM from guest.
180 regs[2] &= ~AMDID2_SVM;
183 * Don't advertise extended performance counter MSRs
186 regs[2] &= ~AMDID2_PCXC;
187 regs[2] &= ~AMDID2_PNXC;
188 regs[2] &= ~AMDID2_PTSCEL2I;
191 * Don't advertise Instruction Based Sampling feature.
193 regs[2] &= ~AMDID2_IBS;
195 /* NodeID MSR not available */
196 regs[2] &= ~AMDID2_NODE_ID;
198 /* Don't advertise the OS visible workaround feature */
199 regs[2] &= ~AMDID2_OSVW;
201 /* Hide mwaitx/monitorx capability from the guest */
202 regs[2] &= ~AMDID2_MWAITX;
204 /* Advertise RDTSCP if it is enabled. */
205 error = vm_get_capability(vm, vcpu_id,
206 VM_CAP_RDTSCP, &enable_rdtscp);
207 if (error == 0 && enable_rdtscp)
208 regs[3] |= AMDID_RDTSCP;
210 regs[3] &= ~AMDID_RDTSCP;
213 case CPUID_8000_0007:
215 * AMD uses this leaf to advertise the processor's
216 * power monitoring and RAS capabilities. These
217 * features are hardware-specific and exposing
218 * them to a guest doesn't make a lot of sense.
220 * Intel uses this leaf only to advertise the
221 * "Invariant TSC" feature with all other bits
222 * being reserved (set to zero).
230 * "Invariant TSC" can be advertised to the guest if:
231 * - host TSC frequency is invariant
232 * - host TSCs are synchronized across physical cpus
234 * XXX This still falls short because the vcpu
235 * can observe the TSC moving backwards as it
236 * migrates across physical cpus. But at least
237 * it should discourage the guest from using the
238 * TSC to keep track of time.
240 if (tsc_is_invariant && smp_tsc)
241 regs[3] |= AMDPM_TSC_INVARIANT;
244 case CPUID_8000_001D:
245 /* AMD Cache topology, like 0000_0004 for Intel. */
250 * Similar to Intel, generate a ficticious cache
251 * topology for the guest with L3 shared by the
252 * package, and L1 and L2 local to a core.
254 vm_get_topology(vm, &sockets, &cores, &threads,
258 logical_cpus = threads;
260 func = 1; /* data cache */
263 logical_cpus = threads;
265 func = 3; /* unified cache */
268 logical_cpus = threads * cores;
270 func = 3; /* unified cache */
279 logical_cpus = MIN(0xfff, logical_cpus - 1);
280 regs[0] = (logical_cpus << 14) | (1 << 8) |
282 regs[1] = (func > 0) ? (CACHE_LINE_SIZE - 1) : 0;
287 case CPUID_8000_001E:
289 * AMD Family 16h+ and Hygon Family 18h additional
292 if (!vmm_is_svm() || CPUID_TO_FAMILY(cpu_id) < 0x16)
295 vm_get_topology(vm, &sockets, &cores, &threads,
298 threads = MIN(0xFF, threads - 1);
299 regs[1] = (threads << 8) |
300 (vcpu_id >> log2(threads + 1));
302 * XXX Bhyve topology cannot yet represent >1 node per
309 case CPUID_0000_0001:
312 error = vm_get_x2apic_state(vm, vcpu_id, &x2apic_state);
314 panic("x86_emulate_cpuid: error %d "
315 "fetching x2apic state", error);
319 * Override the APIC ID only in ebx
321 regs[1] &= ~(CPUID_LOCAL_APIC_ID);
322 regs[1] |= (vcpu_id << CPUID_0000_0001_APICID_SHIFT);
325 * Don't expose VMX, SpeedStep, TME or SMX capability.
326 * Advertise x2APIC capability and Hypervisor guest.
328 regs[2] &= ~(CPUID2_VMX | CPUID2_EST | CPUID2_TM2);
329 regs[2] &= ~(CPUID2_SMX);
331 regs[2] |= CPUID2_HV;
333 if (x2apic_state != X2APIC_DISABLED)
334 regs[2] |= CPUID2_X2APIC;
336 regs[2] &= ~CPUID2_X2APIC;
339 * Only advertise CPUID2_XSAVE in the guest if
340 * the host is using XSAVE.
342 if (!(regs[2] & CPUID2_OSXSAVE))
343 regs[2] &= ~CPUID2_XSAVE;
346 * If CPUID2_XSAVE is being advertised and the
347 * guest has set CR4_XSAVE, set
350 regs[2] &= ~CPUID2_OSXSAVE;
351 if (regs[2] & CPUID2_XSAVE) {
352 error = vm_get_register(vm, vcpu_id,
353 VM_REG_GUEST_CR4, &cr4);
355 panic("x86_emulate_cpuid: error %d "
356 "fetching %%cr4", error);
358 regs[2] |= CPUID2_OSXSAVE;
362 * Hide monitor/mwait until we know how to deal with
363 * these instructions.
365 regs[2] &= ~CPUID2_MON;
368 * Hide the performance and debug features.
370 regs[2] &= ~CPUID2_PDCM;
373 * No TSC deadline support in the APIC yet
375 regs[2] &= ~CPUID2_TSCDLT;
378 * Hide thermal monitoring
380 regs[3] &= ~(CPUID_ACPI | CPUID_TM);
383 * Hide the debug store capability.
385 regs[3] &= ~CPUID_DS;
388 * Advertise the Machine Check and MTRR capability.
390 * Some guest OSes (e.g. Windows) will not boot if
391 * these features are absent.
393 regs[3] |= (CPUID_MCA | CPUID_MCE | CPUID_MTRR);
395 vm_get_topology(vm, &sockets, &cores, &threads,
397 logical_cpus = threads * cores;
398 regs[1] &= ~CPUID_HTT_CORES;
399 regs[1] |= (logical_cpus & 0xff) << 16;
400 regs[3] |= CPUID_HTT;
403 case CPUID_0000_0004:
404 cpuid_count(func, param, regs);
406 if (regs[0] || regs[1] || regs[2] || regs[3]) {
407 vm_get_topology(vm, &sockets, &cores, &threads,
410 regs[0] |= (cores - 1) << 26;
413 * - L1 and L2 are shared only by the logical
414 * processors in a single core.
415 * - L3 and above are shared by all logical
416 * processors in the package.
418 logical_cpus = threads;
419 level = (regs[0] >> 5) & 0x7;
421 logical_cpus *= cores;
422 regs[0] |= (logical_cpus - 1) << 14;
426 case CPUID_0000_0007:
434 cpuid_count(func, param, regs);
436 /* Only leaf 0 is supported */
440 * Expose known-safe features.
442 regs[1] &= (CPUID_STDEXT_FSGSBASE |
443 CPUID_STDEXT_BMI1 | CPUID_STDEXT_HLE |
444 CPUID_STDEXT_AVX2 | CPUID_STDEXT_SMEP |
446 CPUID_STDEXT_ERMS | CPUID_STDEXT_RTM |
447 CPUID_STDEXT_AVX512F |
448 CPUID_STDEXT_RDSEED |
450 CPUID_STDEXT_AVX512PF |
451 CPUID_STDEXT_AVX512ER |
452 CPUID_STDEXT_AVX512CD | CPUID_STDEXT_SHA);
454 regs[3] &= CPUID_STDEXT3_MD_CLEAR;
456 /* Advertise RDPID if it is enabled. */
457 error = vm_get_capability(vm, vcpu_id,
458 VM_CAP_RDPID, &enable_rdpid);
459 if (error == 0 && enable_rdpid)
460 regs[2] |= CPUID_STDEXT2_RDPID;
462 /* Advertise INVPCID if it is enabled. */
463 error = vm_get_capability(vm, vcpu_id,
464 VM_CAP_ENABLE_INVPCID, &enable_invpcid);
465 if (error == 0 && enable_invpcid)
466 regs[1] |= CPUID_STDEXT_INVPCID;
470 case CPUID_0000_0006:
471 regs[0] = CPUTPM1_ARAT;
477 case CPUID_0000_000A:
479 * Handle the access, but report 0 for
488 case CPUID_0000_000B:
490 * Intel processor topology enumeration
492 if (vmm_is_intel()) {
493 vm_get_topology(vm, &sockets, &cores, &threads,
496 logical_cpus = threads;
497 width = log2(logical_cpus);
498 level = CPUID_TYPE_SMT;
503 logical_cpus = threads * cores;
504 width = log2(logical_cpus);
505 level = CPUID_TYPE_CORE;
509 if (!cpuid_leaf_b || param >= 2) {
516 regs[0] = width & 0x1f;
517 regs[1] = logical_cpus & 0xffff;
518 regs[2] = (level << 8) | (param & 0xff);
528 case CPUID_0000_000D:
529 limits = vmm_get_xsave_limits();
530 if (!limits->xsave_enabled) {
538 cpuid_count(func, param, regs);
542 * Only permit the guest to use bits
543 * that are active in the host in
544 * %xcr0. Also, claim that the
545 * maximum save area size is
546 * equivalent to the host's current
547 * save area size. Since this runs
548 * "inside" of vmrun(), it runs with
549 * the guest's xcr0, so the current
550 * save area size is correct as-is.
552 regs[0] &= limits->xcr0_allowed;
553 regs[2] = limits->xsave_max_size;
554 regs[3] &= (limits->xcr0_allowed >> 32);
557 /* Only permit XSAVEOPT. */
558 regs[0] &= CPUID_EXTSTATE_XSAVEOPT;
565 * If the leaf is for a permitted feature,
566 * pass through as-is, otherwise return
569 if (!(limits->xcr0_allowed & (1ul << param))) {
579 case CPUID_0000_0015:
581 * Don't report CPU TSC/Crystal ratio and clock
582 * values since guests may use these to derive the
583 * local APIC frequency..
592 regs[0] = CPUID_VM_HIGH;
593 bcopy(bhyve_id, ®s[1], 4);
594 bcopy(bhyve_id + 4, ®s[2], 4);
595 bcopy(bhyve_id + 8, ®s[3], 4);
601 * The leaf value has already been clamped so
602 * simply pass this through, keeping count of
603 * how many unhandled leaf values have been seen.
605 atomic_add_long(&bhyve_xcpuids, 1);
606 cpuid_count(func, param, regs);
611 * CPUID clears the upper 32-bits of the long-mode registers.
622 vm_cpuid_capability(struct vm *vm, int vcpuid, enum vm_cpuid_capability cap)
626 KASSERT(cap > 0 && cap < VCC_LAST, ("%s: invalid vm_cpu_capability %d",
630 * Simply passthrough the capabilities of the host cpu for now.
635 if (amd_feature & AMDID_NX)
639 if (amd_feature & AMDID_FFXSR)
643 if (amd_feature2 & AMDID2_TCE)
647 panic("%s: unknown vm_cpu_capability %d", __func__, cap);
653 vm_rdmtrr(struct vm_mtrr *mtrr, u_int num, uint64_t *val)
657 *val = MTRR_CAP_WC | MTRR_CAP_FIXED | VMM_MTRR_VAR_MAX;
659 case MSR_MTRRdefType:
660 *val = mtrr->def_type;
662 case MSR_MTRR4kBase ... MSR_MTRR4kBase + 7:
663 *val = mtrr->fixed4k[num - MSR_MTRR4kBase];
665 case MSR_MTRR16kBase ... MSR_MTRR16kBase + 1:
666 *val = mtrr->fixed16k[num - MSR_MTRR16kBase];
668 case MSR_MTRR64kBase:
669 *val = mtrr->fixed64k;
671 case MSR_MTRRVarBase ... MSR_MTRRVarBase + (VMM_MTRR_VAR_MAX * 2) - 1: {
672 u_int offset = num - MSR_MTRRVarBase;
673 if (offset % 2 == 0) {
674 *val = mtrr->var[offset / 2].base;
676 *val = mtrr->var[offset / 2].mask;
688 vm_wrmtrr(struct vm_mtrr *mtrr, u_int num, uint64_t val)
692 /* MTRRCAP is read only */
694 case MSR_MTRRdefType:
695 if (val & ~VMM_MTRR_DEF_MASK) {
696 /* generate #GP on writes to reserved fields */
699 mtrr->def_type = val;
701 case MSR_MTRR4kBase ... MSR_MTRR4kBase + 7:
702 mtrr->fixed4k[num - MSR_MTRR4kBase] = val;
704 case MSR_MTRR16kBase ... MSR_MTRR16kBase + 1:
705 mtrr->fixed16k[num - MSR_MTRR16kBase] = val;
707 case MSR_MTRR64kBase:
708 mtrr->fixed64k = val;
710 case MSR_MTRRVarBase ... MSR_MTRRVarBase + (VMM_MTRR_VAR_MAX * 2) - 1: {
711 u_int offset = num - MSR_MTRRVarBase;
712 if (offset % 2 == 0) {
713 if (val & ~VMM_MTRR_PHYSBASE_MASK) {
714 /* generate #GP on writes to reserved fields */
717 mtrr->var[offset / 2].base = val;
719 if (val & ~VMM_MTRR_PHYSMASK_MASK) {
720 /* generate #GP on writes to reserved fields */
723 mtrr->var[offset / 2].mask = val;