2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/cpuset.h>
37 #include <machine/clock.h>
38 #include <machine/cpufunc.h>
39 #include <machine/md_var.h>
40 #include <machine/specialreg.h>
42 #include <machine/vmm.h>
46 #define CPUID_VM_HIGH 0x40000000
48 static const char bhyve_id[12] = "bhyve bhyve ";
50 static uint64_t bhyve_xcpuids;
53 x86_emulate_cpuid(struct vm *vm, int vcpu_id,
54 uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
56 int error, enable_invpcid;
57 unsigned int func, regs[4];
58 enum x2apic_state x2apic_state;
61 * Requests for invalid CPUID levels should map to the highest
62 * available level instead.
64 if (cpu_exthigh != 0 && *eax >= 0x80000000) {
65 if (*eax > cpu_exthigh)
67 } else if (*eax >= 0x40000000) {
68 if (*eax > CPUID_VM_HIGH)
70 } else if (*eax > cpu_high) {
77 * In general the approach used for CPU topology is to
78 * advertise a flat topology where all CPUs are packages with
79 * no multi-core or SMT.
83 * Pass these through to the guest
94 cpuid_count(*eax, *ecx, regs);
99 * Hide rdtscp/ia32_tsc_aux until we know how
102 cpuid_count(*eax, *ecx, regs);
103 regs[3] &= ~AMDID_RDTSCP;
106 case CPUID_8000_0007:
107 cpuid_count(*eax, *ecx, regs);
109 * If the host TSCs are not synchronized across
110 * physical cpus then we cannot advertise an
111 * invariant tsc to a vcpu.
113 * XXX This still falls short because the vcpu
114 * can observe the TSC moving backwards as it
115 * migrates across physical cpus. But at least
116 * it should discourage the guest from using the
117 * TSC to keep track of time.
120 regs[3] &= ~AMDPM_TSC_INVARIANT;
123 case CPUID_0000_0001:
126 error = vm_get_x2apic_state(vm, vcpu_id, &x2apic_state);
128 panic("x86_emulate_cpuid: error %d "
129 "fetching x2apic state", error);
133 * Override the APIC ID only in ebx
135 regs[1] &= ~(CPUID_LOCAL_APIC_ID);
136 regs[1] |= (vcpu_id << CPUID_0000_0001_APICID_SHIFT);
139 * Don't expose VMX, SpeedStep or TME capability.
140 * Advertise x2APIC capability and Hypervisor guest.
142 regs[2] &= ~(CPUID2_VMX | CPUID2_EST | CPUID2_TM2);
144 regs[2] |= CPUID2_HV;
146 if (x2apic_state != X2APIC_DISABLED)
147 regs[2] |= CPUID2_X2APIC;
150 * Hide xsave/osxsave/avx until the FPU save/restore
151 * issues are resolved
153 regs[2] &= ~(CPUID2_XSAVE | CPUID2_OSXSAVE |
157 * Hide monitor/mwait until we know how to deal with
158 * these instructions.
160 regs[2] &= ~CPUID2_MON;
163 * Hide the performance and debug features.
165 regs[2] &= ~CPUID2_PDCM;
168 * No TSC deadline support in the APIC yet
170 regs[2] &= ~CPUID2_TSCDLT;
173 * Hide thermal monitoring
175 regs[3] &= ~(CPUID_ACPI | CPUID_TM);
178 * Machine check handling is done in the host.
179 * Hide MTRR capability.
181 regs[3] &= ~(CPUID_MCA | CPUID_MCE | CPUID_MTRR);
184 * Hide the debug store capability.
186 regs[3] &= ~CPUID_DS;
189 * Disable multi-core.
191 regs[1] &= ~CPUID_HTT_CORES;
192 regs[3] &= ~CPUID_HTT;
195 case CPUID_0000_0004:
199 * Do not expose topology.
201 regs[0] &= 0xffff8000;
202 regs[0] |= 0x04008000;
205 case CPUID_0000_0007:
213 error = vm_get_capability(vm, vcpu_id,
214 VM_CAP_ENABLE_INVPCID, &enable_invpcid);
215 if (error == 0 && enable_invpcid)
216 regs[1] |= CPUID_STDEXT_INVPCID;
220 case CPUID_0000_0006:
221 case CPUID_0000_000A:
222 case CPUID_0000_000D:
224 * Handle the access, but report 0 for
233 case CPUID_0000_000B:
235 * Processor topology enumeration
239 regs[2] = *ecx & 0xff;
244 regs[0] = CPUID_VM_HIGH;
245 bcopy(bhyve_id, ®s[1], 4);
246 bcopy(bhyve_id + 4, ®s[2], 4);
247 bcopy(bhyve_id + 8, ®s[3], 4);
252 * The leaf value has already been clamped so
253 * simply pass this through, keeping count of
254 * how many unhandled leaf values have been seen.
256 atomic_add_long(&bhyve_xcpuids, 1);
257 cpuid_count(*eax, *ecx, regs);