1366f6083SPeter Grehan /*- 2366f6083SPeter Grehan * Copyright (c) 2011 NetApp, Inc. 3366f6083SPeter Grehan * All rights reserved. 4366f6083SPeter Grehan * 5366f6083SPeter Grehan * Redistribution and use in source and binary forms, with or without 6366f6083SPeter Grehan * modification, are permitted provided that the following conditions 7366f6083SPeter Grehan * are met: 8366f6083SPeter Grehan * 1. Redistributions of source code must retain the above copyright 9366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer. 10366f6083SPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright 11366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer in the 12366f6083SPeter Grehan * documentation and/or other materials provided with the distribution. 13366f6083SPeter Grehan * 14366f6083SPeter Grehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15366f6083SPeter Grehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16366f6083SPeter Grehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17366f6083SPeter Grehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18366f6083SPeter Grehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19366f6083SPeter Grehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20366f6083SPeter Grehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21366f6083SPeter Grehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22366f6083SPeter Grehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23366f6083SPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24366f6083SPeter Grehan * SUCH DAMAGE. 25366f6083SPeter Grehan * 26366f6083SPeter Grehan * $FreeBSD$ 27366f6083SPeter Grehan */ 28366f6083SPeter Grehan 29366f6083SPeter Grehan #include <sys/cdefs.h> 30366f6083SPeter Grehan __FBSDID("$FreeBSD$"); 31366f6083SPeter Grehan 32a2da7af6SNeel Natu #include <sys/param.h> 33abb023fbSJohn Baldwin #include <sys/pcpu.h> 348b287612SJohn Baldwin #include <sys/systm.h> 35a2da7af6SNeel Natu #include <sys/cpuset.h> 368bd3845dSNeel Natu #include <sys/sysctl.h> 37366f6083SPeter Grehan 381472b87fSNeel Natu #include <machine/clock.h> 39366f6083SPeter Grehan #include <machine/cpufunc.h> 408b287612SJohn Baldwin #include <machine/md_var.h> 41abb023fbSJohn Baldwin #include <machine/segments.h> 42366f6083SPeter Grehan #include <machine/specialreg.h> 43366f6083SPeter Grehan 44a2da7af6SNeel Natu #include <machine/vmm.h> 45a2da7af6SNeel Natu 46abb023fbSJohn Baldwin #include "vmm_host.h" 475a1f0b36SNeel Natu #include "vmm_ktr.h" 485a1f0b36SNeel Natu #include "vmm_util.h" 49366f6083SPeter Grehan #include "x86.h" 50366f6083SPeter Grehan 518bd3845dSNeel Natu SYSCTL_DECL(_hw_vmm); 528bd3845dSNeel Natu static SYSCTL_NODE(_hw_vmm, OID_AUTO, topology, CTLFLAG_RD, 0, NULL); 538bd3845dSNeel Natu 548b287612SJohn Baldwin #define CPUID_VM_HIGH 0x40000000 558b287612SJohn Baldwin 56560d5edaSPeter Grehan static const char bhyve_id[12] = "bhyve bhyve "; 57560d5edaSPeter Grehan 58560d5edaSPeter Grehan static uint64_t bhyve_xcpuids; 595a1f0b36SNeel Natu SYSCTL_ULONG(_hw_vmm, OID_AUTO, bhyve_xcpuids, CTLFLAG_RW, &bhyve_xcpuids, 0, 605a1f0b36SNeel Natu "Number of times an unknown cpuid leaf was accessed"); 618b287612SJohn Baldwin 628bd3845dSNeel Natu /* 638bd3845dSNeel Natu * The default CPU topology is a single thread per package. 648bd3845dSNeel Natu */ 658bd3845dSNeel Natu static u_int threads_per_core = 1; 668bd3845dSNeel Natu SYSCTL_UINT(_hw_vmm_topology, OID_AUTO, threads_per_core, CTLFLAG_RDTUN, 678bd3845dSNeel Natu &threads_per_core, 0, NULL); 688bd3845dSNeel Natu 698bd3845dSNeel Natu static u_int cores_per_package = 1; 708bd3845dSNeel Natu SYSCTL_UINT(_hw_vmm_topology, OID_AUTO, cores_per_package, CTLFLAG_RDTUN, 718bd3845dSNeel Natu &cores_per_package, 0, NULL); 728bd3845dSNeel Natu 738bd3845dSNeel Natu static int cpuid_leaf_b = 1; 748bd3845dSNeel Natu SYSCTL_INT(_hw_vmm_topology, OID_AUTO, cpuid_leaf_b, CTLFLAG_RDTUN, 758bd3845dSNeel Natu &cpuid_leaf_b, 0, NULL); 768bd3845dSNeel Natu 778bd3845dSNeel Natu /* 788bd3845dSNeel Natu * Round up to the next power of two, if necessary, and then take log2. 798bd3845dSNeel Natu * Returns -1 if argument is zero. 808bd3845dSNeel Natu */ 818bd3845dSNeel Natu static __inline int 828bd3845dSNeel Natu log2(u_int x) 838bd3845dSNeel Natu { 848bd3845dSNeel Natu 858bd3845dSNeel Natu return (fls(x << (1 - powerof2(x))) - 1); 868bd3845dSNeel Natu } 878bd3845dSNeel Natu 88366f6083SPeter Grehan int 89a2da7af6SNeel Natu x86_emulate_cpuid(struct vm *vm, int vcpu_id, 90a2da7af6SNeel Natu uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) 91366f6083SPeter Grehan { 92abb023fbSJohn Baldwin const struct xsave_limits *limits; 93abb023fbSJohn Baldwin uint64_t cr4; 948bd3845dSNeel Natu int error, enable_invpcid, level, width, x2apic_id; 958bd3845dSNeel Natu unsigned int func, regs[4], logical_cpus; 96a2da7af6SNeel Natu enum x2apic_state x2apic_state; 97366f6083SPeter Grehan 985a1f0b36SNeel Natu VCPU_CTR2(vm, vcpu_id, "cpuid %#x,%#x", *eax, *ecx); 995a1f0b36SNeel Natu 1008b287612SJohn Baldwin /* 1018b287612SJohn Baldwin * Requests for invalid CPUID levels should map to the highest 1028b287612SJohn Baldwin * available level instead. 1038b287612SJohn Baldwin */ 1048b287612SJohn Baldwin if (cpu_exthigh != 0 && *eax >= 0x80000000) { 1058b287612SJohn Baldwin if (*eax > cpu_exthigh) 1068b287612SJohn Baldwin *eax = cpu_exthigh; 1078b287612SJohn Baldwin } else if (*eax >= 0x40000000) { 1088b287612SJohn Baldwin if (*eax > CPUID_VM_HIGH) 1098b287612SJohn Baldwin *eax = CPUID_VM_HIGH; 1108b287612SJohn Baldwin } else if (*eax > cpu_high) { 1118b287612SJohn Baldwin *eax = cpu_high; 1128b287612SJohn Baldwin } 113366f6083SPeter Grehan 11425448de2SNeel Natu func = *eax; 11525448de2SNeel Natu 1168b287612SJohn Baldwin /* 1178b287612SJohn Baldwin * In general the approach used for CPU topology is to 1188b287612SJohn Baldwin * advertise a flat topology where all CPUs are packages with 1198b287612SJohn Baldwin * no multi-core or SMT. 1208b287612SJohn Baldwin */ 121366f6083SPeter Grehan switch (func) { 122560d5edaSPeter Grehan /* 123560d5edaSPeter Grehan * Pass these through to the guest 124560d5edaSPeter Grehan */ 125366f6083SPeter Grehan case CPUID_0000_0000: 126366f6083SPeter Grehan case CPUID_0000_0002: 127366f6083SPeter Grehan case CPUID_0000_0003: 128366f6083SPeter Grehan case CPUID_8000_0000: 129366f6083SPeter Grehan case CPUID_8000_0002: 130366f6083SPeter Grehan case CPUID_8000_0003: 131366f6083SPeter Grehan case CPUID_8000_0004: 132366f6083SPeter Grehan case CPUID_8000_0006: 1335a1f0b36SNeel Natu cpuid_count(*eax, *ecx, regs); 1345a1f0b36SNeel Natu break; 135366f6083SPeter Grehan case CPUID_8000_0008: 1368b287612SJohn Baldwin cpuid_count(*eax, *ecx, regs); 1375a1f0b36SNeel Natu if (vmm_is_amd()) { 1385a1f0b36SNeel Natu /* 1395a1f0b36SNeel Natu * XXX this might appear silly because AMD 1405a1f0b36SNeel Natu * cpus don't have threads. 1415a1f0b36SNeel Natu * 1425a1f0b36SNeel Natu * However this matches the logical cpus as 1435a1f0b36SNeel Natu * advertised by leaf 0x1 and will work even 1445a1f0b36SNeel Natu * if the 'threads_per_core' tunable is set 1455a1f0b36SNeel Natu * incorrectly on an AMD host. 1465a1f0b36SNeel Natu */ 1475a1f0b36SNeel Natu logical_cpus = threads_per_core * 1485a1f0b36SNeel Natu cores_per_package; 1495a1f0b36SNeel Natu regs[2] = logical_cpus - 1; 1505a1f0b36SNeel Natu } 151366f6083SPeter Grehan break; 152366f6083SPeter Grehan 153560d5edaSPeter Grehan case CPUID_8000_0001: 15406053618SNeel Natu cpuid_count(*eax, *ecx, regs); 15506053618SNeel Natu 15606053618SNeel Natu /* 1575a1f0b36SNeel Natu * Hide SVM and Topology Extension features from guest. 15806053618SNeel Natu */ 1595a1f0b36SNeel Natu regs[2] &= ~(AMDID2_SVM | AMDID2_TOPOLOGY); 16006053618SNeel Natu 161560d5edaSPeter Grehan /* 16202904c45SNeel Natu * Don't advertise extended performance counter MSRs 16302904c45SNeel Natu * to the guest. 16402904c45SNeel Natu */ 16502904c45SNeel Natu regs[2] &= ~AMDID2_PCXC; 16602904c45SNeel Natu regs[2] &= ~AMDID2_PNXC; 16702904c45SNeel Natu regs[2] &= ~AMDID2_PTSCEL2I; 16802904c45SNeel Natu 16902904c45SNeel Natu /* 1702688a818SNeel Natu * Don't advertise Instruction Based Sampling feature. 1712688a818SNeel Natu */ 1722688a818SNeel Natu regs[2] &= ~AMDID2_IBS; 1732688a818SNeel Natu 17465d5111aSNeel Natu /* NodeID MSR not available */ 17565d5111aSNeel Natu regs[2] &= ~AMDID2_NODE_ID; 17665d5111aSNeel Natu 177*592cd7d3SNeel Natu /* Don't advertise the OS visible workaround feature */ 178*592cd7d3SNeel Natu regs[2] &= ~AMDID2_OSVW; 179*592cd7d3SNeel Natu 1802688a818SNeel Natu /* 181560d5edaSPeter Grehan * Hide rdtscp/ia32_tsc_aux until we know how 182560d5edaSPeter Grehan * to deal with them. 183560d5edaSPeter Grehan */ 184560d5edaSPeter Grehan regs[3] &= ~AMDID_RDTSCP; 185560d5edaSPeter Grehan break; 186560d5edaSPeter Grehan 1871472b87fSNeel Natu case CPUID_8000_0007: 1881472b87fSNeel Natu /* 189*592cd7d3SNeel Natu * AMD uses this leaf to advertise the processor's 190*592cd7d3SNeel Natu * power monitoring and RAS capabilities. These 191*592cd7d3SNeel Natu * features are hardware-specific and exposing 192*592cd7d3SNeel Natu * them to a guest doesn't make a lot of sense. 193*592cd7d3SNeel Natu * 194*592cd7d3SNeel Natu * Intel uses this leaf only to advertise the 195*592cd7d3SNeel Natu * "Invariant TSC" feature with all other bits 196*592cd7d3SNeel Natu * being reserved (set to zero). 197*592cd7d3SNeel Natu */ 198*592cd7d3SNeel Natu regs[0] = 0; 199*592cd7d3SNeel Natu regs[1] = 0; 200*592cd7d3SNeel Natu regs[2] = 0; 201*592cd7d3SNeel Natu regs[3] = 0; 202*592cd7d3SNeel Natu 203*592cd7d3SNeel Natu /* 204*592cd7d3SNeel Natu * "Invariant TSC" can be advertised to the guest if: 205*592cd7d3SNeel Natu * - host TSC frequency is invariant 206*592cd7d3SNeel Natu * - host TSCs are synchronized across physical cpus 2071472b87fSNeel Natu * 2081472b87fSNeel Natu * XXX This still falls short because the vcpu 2091472b87fSNeel Natu * can observe the TSC moving backwards as it 2101472b87fSNeel Natu * migrates across physical cpus. But at least 2111472b87fSNeel Natu * it should discourage the guest from using the 2121472b87fSNeel Natu * TSC to keep track of time. 2131472b87fSNeel Natu */ 214*592cd7d3SNeel Natu if (tsc_is_invariant && smp_tsc) 215*592cd7d3SNeel Natu regs[3] |= AMDPM_TSC_INVARIANT; 2161472b87fSNeel Natu break; 2171472b87fSNeel Natu 218366f6083SPeter Grehan case CPUID_0000_0001: 2198b287612SJohn Baldwin do_cpuid(1, regs); 2208b287612SJohn Baldwin 221a2da7af6SNeel Natu error = vm_get_x2apic_state(vm, vcpu_id, &x2apic_state); 222a2da7af6SNeel Natu if (error) { 223a2da7af6SNeel Natu panic("x86_emulate_cpuid: error %d " 224a2da7af6SNeel Natu "fetching x2apic state", error); 225a2da7af6SNeel Natu } 226a2da7af6SNeel Natu 227366f6083SPeter Grehan /* 228366f6083SPeter Grehan * Override the APIC ID only in ebx 229366f6083SPeter Grehan */ 2308b287612SJohn Baldwin regs[1] &= ~(CPUID_LOCAL_APIC_ID); 2318b287612SJohn Baldwin regs[1] |= (vcpu_id << CPUID_0000_0001_APICID_SHIFT); 232366f6083SPeter Grehan 233366f6083SPeter Grehan /* 2341f3025e1SPeter Grehan * Don't expose VMX, SpeedStep or TME capability. 2358b287612SJohn Baldwin * Advertise x2APIC capability and Hypervisor guest. 236366f6083SPeter Grehan */ 2378b287612SJohn Baldwin regs[2] &= ~(CPUID2_VMX | CPUID2_EST | CPUID2_TM2); 238a2da7af6SNeel Natu 239a2da7af6SNeel Natu regs[2] |= CPUID2_HV; 240a2da7af6SNeel Natu 241a2da7af6SNeel Natu if (x2apic_state != X2APIC_DISABLED) 242a2da7af6SNeel Natu regs[2] |= CPUID2_X2APIC; 24352e5c8a2SNeel Natu else 24452e5c8a2SNeel Natu regs[2] &= ~CPUID2_X2APIC; 245366f6083SPeter Grehan 246366f6083SPeter Grehan /* 247abb023fbSJohn Baldwin * Only advertise CPUID2_XSAVE in the guest if 248abb023fbSJohn Baldwin * the host is using XSAVE. 249298379f7SPeter Grehan */ 250abb023fbSJohn Baldwin if (!(regs[2] & CPUID2_OSXSAVE)) 251abb023fbSJohn Baldwin regs[2] &= ~CPUID2_XSAVE; 252abb023fbSJohn Baldwin 253abb023fbSJohn Baldwin /* 254abb023fbSJohn Baldwin * If CPUID2_XSAVE is being advertised and the 255abb023fbSJohn Baldwin * guest has set CR4_XSAVE, set 256abb023fbSJohn Baldwin * CPUID2_OSXSAVE. 257abb023fbSJohn Baldwin */ 258abb023fbSJohn Baldwin regs[2] &= ~CPUID2_OSXSAVE; 259abb023fbSJohn Baldwin if (regs[2] & CPUID2_XSAVE) { 260abb023fbSJohn Baldwin error = vm_get_register(vm, vcpu_id, 261abb023fbSJohn Baldwin VM_REG_GUEST_CR4, &cr4); 262abb023fbSJohn Baldwin if (error) 263abb023fbSJohn Baldwin panic("x86_emulate_cpuid: error %d " 264abb023fbSJohn Baldwin "fetching %%cr4", error); 265abb023fbSJohn Baldwin if (cr4 & CR4_XSAVE) 266abb023fbSJohn Baldwin regs[2] |= CPUID2_OSXSAVE; 267abb023fbSJohn Baldwin } 268298379f7SPeter Grehan 269298379f7SPeter Grehan /* 270ff6ec151SNeel Natu * Hide monitor/mwait until we know how to deal with 271ff6ec151SNeel Natu * these instructions. 272ff6ec151SNeel Natu */ 273ff6ec151SNeel Natu regs[2] &= ~CPUID2_MON; 274ff6ec151SNeel Natu 275ff6ec151SNeel Natu /* 276560d5edaSPeter Grehan * Hide the performance and debug features. 277560d5edaSPeter Grehan */ 278560d5edaSPeter Grehan regs[2] &= ~CPUID2_PDCM; 279560d5edaSPeter Grehan 280517e21d3SPeter Grehan /* 281517e21d3SPeter Grehan * No TSC deadline support in the APIC yet 282517e21d3SPeter Grehan */ 283517e21d3SPeter Grehan regs[2] &= ~CPUID2_TSCDLT; 284517e21d3SPeter Grehan 285560d5edaSPeter Grehan /* 2861f3025e1SPeter Grehan * Hide thermal monitoring 2871f3025e1SPeter Grehan */ 2881f3025e1SPeter Grehan regs[3] &= ~(CPUID_ACPI | CPUID_TM); 2891f3025e1SPeter Grehan 2901f3025e1SPeter Grehan /* 291366f6083SPeter Grehan * Machine check handling is done in the host. 292366f6083SPeter Grehan * Hide MTRR capability. 293366f6083SPeter Grehan */ 294366f6083SPeter Grehan regs[3] &= ~(CPUID_MCA | CPUID_MCE | CPUID_MTRR); 295366f6083SPeter Grehan 2968b287612SJohn Baldwin /* 297560d5edaSPeter Grehan * Hide the debug store capability. 298560d5edaSPeter Grehan */ 299560d5edaSPeter Grehan regs[3] &= ~CPUID_DS; 300560d5edaSPeter Grehan 3018bd3845dSNeel Natu logical_cpus = threads_per_core * cores_per_package; 3028b287612SJohn Baldwin regs[1] &= ~CPUID_HTT_CORES; 3038bd3845dSNeel Natu regs[1] |= (logical_cpus & 0xff) << 16; 3048bd3845dSNeel Natu regs[3] |= CPUID_HTT; 3058b287612SJohn Baldwin break; 3068b287612SJohn Baldwin 3078b287612SJohn Baldwin case CPUID_0000_0004: 308534dc967SNeel Natu cpuid_count(*eax, *ecx, regs); 3098b287612SJohn Baldwin 3108bd3845dSNeel Natu if (regs[0] || regs[1] || regs[2] || regs[3]) { 311534dc967SNeel Natu regs[0] &= 0x3ff; 3128bd3845dSNeel Natu regs[0] |= (cores_per_package - 1) << 26; 3138b287612SJohn Baldwin /* 3148bd3845dSNeel Natu * Cache topology: 3158bd3845dSNeel Natu * - L1 and L2 are shared only by the logical 3168bd3845dSNeel Natu * processors in a single core. 3178bd3845dSNeel Natu * - L3 and above are shared by all logical 3188bd3845dSNeel Natu * processors in the package. 3198b287612SJohn Baldwin */ 3208bd3845dSNeel Natu logical_cpus = threads_per_core; 3218bd3845dSNeel Natu level = (regs[0] >> 5) & 0x7; 3228bd3845dSNeel Natu if (level >= 3) 3238bd3845dSNeel Natu logical_cpus *= cores_per_package; 3248bd3845dSNeel Natu regs[0] |= (logical_cpus - 1) << 14; 3258bd3845dSNeel Natu } 326366f6083SPeter Grehan break; 327366f6083SPeter Grehan 328a0cad470SPeter Grehan case CPUID_0000_0007: 32949cc03daSNeel Natu regs[0] = 0; 33049cc03daSNeel Natu regs[1] = 0; 33149cc03daSNeel Natu regs[2] = 0; 33249cc03daSNeel Natu regs[3] = 0; 33349cc03daSNeel Natu 33449cc03daSNeel Natu /* leaf 0 */ 33549cc03daSNeel Natu if (*ecx == 0) { 33644a68c4eSJohn Baldwin cpuid_count(*eax, *ecx, regs); 33744a68c4eSJohn Baldwin 33844a68c4eSJohn Baldwin /* Only leaf 0 is supported */ 33944a68c4eSJohn Baldwin regs[0] = 0; 34044a68c4eSJohn Baldwin 34144a68c4eSJohn Baldwin /* 34244a68c4eSJohn Baldwin * Expose known-safe features. 34344a68c4eSJohn Baldwin */ 34444a68c4eSJohn Baldwin regs[1] &= (CPUID_STDEXT_FSGSBASE | 34544a68c4eSJohn Baldwin CPUID_STDEXT_BMI1 | CPUID_STDEXT_HLE | 34644a68c4eSJohn Baldwin CPUID_STDEXT_AVX2 | CPUID_STDEXT_BMI2 | 34744a68c4eSJohn Baldwin CPUID_STDEXT_ERMS | CPUID_STDEXT_RTM | 34844a68c4eSJohn Baldwin CPUID_STDEXT_AVX512F | 34944a68c4eSJohn Baldwin CPUID_STDEXT_AVX512PF | 35044a68c4eSJohn Baldwin CPUID_STDEXT_AVX512ER | 35144a68c4eSJohn Baldwin CPUID_STDEXT_AVX512CD); 35244a68c4eSJohn Baldwin regs[2] = 0; 35344a68c4eSJohn Baldwin regs[3] = 0; 35444a68c4eSJohn Baldwin 35544a68c4eSJohn Baldwin /* Advertise INVPCID if it is enabled. */ 35649cc03daSNeel Natu error = vm_get_capability(vm, vcpu_id, 35749cc03daSNeel Natu VM_CAP_ENABLE_INVPCID, &enable_invpcid); 35849cc03daSNeel Natu if (error == 0 && enable_invpcid) 35949cc03daSNeel Natu regs[1] |= CPUID_STDEXT_INVPCID; 36049cc03daSNeel Natu } 36149cc03daSNeel Natu break; 36249cc03daSNeel Natu 36349cc03daSNeel Natu case CPUID_0000_0006: 364560d5edaSPeter Grehan case CPUID_0000_000A: 3651f3025e1SPeter Grehan /* 3661f3025e1SPeter Grehan * Handle the access, but report 0 for 3671f3025e1SPeter Grehan * all options 3681f3025e1SPeter Grehan */ 3691f3025e1SPeter Grehan regs[0] = 0; 3701f3025e1SPeter Grehan regs[1] = 0; 3711f3025e1SPeter Grehan regs[2] = 0; 3721f3025e1SPeter Grehan regs[3] = 0; 3731f3025e1SPeter Grehan break; 3741f3025e1SPeter Grehan 375366f6083SPeter Grehan case CPUID_0000_000B: 376366f6083SPeter Grehan /* 377366f6083SPeter Grehan * Processor topology enumeration 378366f6083SPeter Grehan */ 3798bd3845dSNeel Natu if (*ecx == 0) { 3808bd3845dSNeel Natu logical_cpus = threads_per_core; 3818bd3845dSNeel Natu width = log2(logical_cpus); 3828bd3845dSNeel Natu level = CPUID_TYPE_SMT; 3838bd3845dSNeel Natu x2apic_id = vcpu_id; 3848bd3845dSNeel Natu } 3858bd3845dSNeel Natu 3868bd3845dSNeel Natu if (*ecx == 1) { 3878bd3845dSNeel Natu logical_cpus = threads_per_core * 3888bd3845dSNeel Natu cores_per_package; 3898bd3845dSNeel Natu width = log2(logical_cpus); 3908bd3845dSNeel Natu level = CPUID_TYPE_CORE; 3918bd3845dSNeel Natu x2apic_id = vcpu_id; 3928bd3845dSNeel Natu } 3938bd3845dSNeel Natu 3948bd3845dSNeel Natu if (!cpuid_leaf_b || *ecx >= 2) { 3958bd3845dSNeel Natu width = 0; 3968bd3845dSNeel Natu logical_cpus = 0; 3978bd3845dSNeel Natu level = 0; 3988bd3845dSNeel Natu x2apic_id = 0; 3998bd3845dSNeel Natu } 4008bd3845dSNeel Natu 4018bd3845dSNeel Natu regs[0] = width & 0x1f; 4028bd3845dSNeel Natu regs[1] = logical_cpus & 0xffff; 4038bd3845dSNeel Natu regs[2] = (level << 8) | (*ecx & 0xff); 4048bd3845dSNeel Natu regs[3] = x2apic_id; 405366f6083SPeter Grehan break; 406366f6083SPeter Grehan 407abb023fbSJohn Baldwin case CPUID_0000_000D: 408abb023fbSJohn Baldwin limits = vmm_get_xsave_limits(); 409abb023fbSJohn Baldwin if (!limits->xsave_enabled) { 410abb023fbSJohn Baldwin regs[0] = 0; 411abb023fbSJohn Baldwin regs[1] = 0; 412abb023fbSJohn Baldwin regs[2] = 0; 413abb023fbSJohn Baldwin regs[3] = 0; 414abb023fbSJohn Baldwin break; 415abb023fbSJohn Baldwin } 416abb023fbSJohn Baldwin 417abb023fbSJohn Baldwin cpuid_count(*eax, *ecx, regs); 418abb023fbSJohn Baldwin switch (*ecx) { 419abb023fbSJohn Baldwin case 0: 420abb023fbSJohn Baldwin /* 421abb023fbSJohn Baldwin * Only permit the guest to use bits 422abb023fbSJohn Baldwin * that are active in the host in 423abb023fbSJohn Baldwin * %xcr0. Also, claim that the 424abb023fbSJohn Baldwin * maximum save area size is 425abb023fbSJohn Baldwin * equivalent to the host's current 426abb023fbSJohn Baldwin * save area size. Since this runs 427abb023fbSJohn Baldwin * "inside" of vmrun(), it runs with 428abb023fbSJohn Baldwin * the guest's xcr0, so the current 429abb023fbSJohn Baldwin * save area size is correct as-is. 430abb023fbSJohn Baldwin */ 431abb023fbSJohn Baldwin regs[0] &= limits->xcr0_allowed; 432abb023fbSJohn Baldwin regs[2] = limits->xsave_max_size; 433abb023fbSJohn Baldwin regs[3] &= (limits->xcr0_allowed >> 32); 434abb023fbSJohn Baldwin break; 435abb023fbSJohn Baldwin case 1: 436abb023fbSJohn Baldwin /* Only permit XSAVEOPT. */ 437abb023fbSJohn Baldwin regs[0] &= CPUID_EXTSTATE_XSAVEOPT; 438abb023fbSJohn Baldwin regs[1] = 0; 439abb023fbSJohn Baldwin regs[2] = 0; 440abb023fbSJohn Baldwin regs[3] = 0; 441abb023fbSJohn Baldwin break; 442abb023fbSJohn Baldwin default: 443abb023fbSJohn Baldwin /* 444abb023fbSJohn Baldwin * If the leaf is for a permitted feature, 445abb023fbSJohn Baldwin * pass through as-is, otherwise return 446abb023fbSJohn Baldwin * all zeroes. 447abb023fbSJohn Baldwin */ 448abb023fbSJohn Baldwin if (!(limits->xcr0_allowed & (1ul << *ecx))) { 449abb023fbSJohn Baldwin regs[0] = 0; 450abb023fbSJohn Baldwin regs[1] = 0; 451abb023fbSJohn Baldwin regs[2] = 0; 452abb023fbSJohn Baldwin regs[3] = 0; 453abb023fbSJohn Baldwin } 454abb023fbSJohn Baldwin break; 455abb023fbSJohn Baldwin } 456abb023fbSJohn Baldwin break; 457abb023fbSJohn Baldwin 4588b287612SJohn Baldwin case 0x40000000: 4598b287612SJohn Baldwin regs[0] = CPUID_VM_HIGH; 4608b287612SJohn Baldwin bcopy(bhyve_id, ®s[1], 4); 461560d5edaSPeter Grehan bcopy(bhyve_id + 4, ®s[2], 4); 462560d5edaSPeter Grehan bcopy(bhyve_id + 8, ®s[3], 4); 4638b287612SJohn Baldwin break; 464560d5edaSPeter Grehan 465366f6083SPeter Grehan default: 466560d5edaSPeter Grehan /* 467560d5edaSPeter Grehan * The leaf value has already been clamped so 468560d5edaSPeter Grehan * simply pass this through, keeping count of 469560d5edaSPeter Grehan * how many unhandled leaf values have been seen. 470560d5edaSPeter Grehan */ 471560d5edaSPeter Grehan atomic_add_long(&bhyve_xcpuids, 1); 472560d5edaSPeter Grehan cpuid_count(*eax, *ecx, regs); 473560d5edaSPeter Grehan break; 474366f6083SPeter Grehan } 475366f6083SPeter Grehan 476366f6083SPeter Grehan *eax = regs[0]; 477366f6083SPeter Grehan *ebx = regs[1]; 478366f6083SPeter Grehan *ecx = regs[2]; 479366f6083SPeter Grehan *edx = regs[3]; 480560d5edaSPeter Grehan 481366f6083SPeter Grehan return (1); 482366f6083SPeter Grehan } 483