blob: 31ea934d9b4998f6eea95397405c0603fa54500a [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity00b27a32011-11-23 16:30:32 +02002/*
3 * Kernel-based Virtual Machine driver for Linux
4 * cpuid support routines
5 *
6 * derived from arch/x86/kvm/x86.c
7 *
8 * Copyright 2011 Red Hat, Inc. and/or its affiliates.
9 * Copyright IBM Corporation, 2008
Avi Kivity00b27a32011-11-23 16:30:32 +020010 */
11
12#include <linux/kvm_host.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040013#include <linux/export.h>
Jan Kiszkabb5a7982011-12-14 17:58:18 +010014#include <linux/vmalloc.h>
15#include <linux/uaccess.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010016#include <linux/sched/stat.h>
17
Luwei Kang4504b5c2016-11-07 14:03:20 +080018#include <asm/processor.h>
Avi Kivity00b27a32011-11-23 16:30:32 +020019#include <asm/user.h>
Ingo Molnar669ebab2015-04-28 08:41:33 +020020#include <asm/fpu/xstate.h>
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
22#include "lapic.h"
23#include "mmu.h"
24#include "trace.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020025#include "pmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020026
Sean Christopherson66a69502020-03-02 15:56:41 -080027/*
28 * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
29 * aligned to sizeof(unsigned long) because it's not accessed via bitops.
30 */
31u32 kvm_cpu_caps[NCAPINTS] __read_mostly;
32EXPORT_SYMBOL_GPL(kvm_cpu_caps);
33
Paolo Bonzini412a3c42014-12-03 14:38:01 +010034static u32 xstate_required_size(u64 xstate_bv, bool compacted)
Paolo Bonzini4344ee92013-10-02 16:06:16 +020035{
36 int feature_bit = 0;
37 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
38
Dave Hansend91cab72015-09-02 16:31:26 -070039 xstate_bv &= XFEATURE_MASK_EXTEND;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020040 while (xstate_bv) {
41 if (xstate_bv & 0x1) {
Paolo Bonzini412a3c42014-12-03 14:38:01 +010042 u32 eax, ebx, ecx, edx, offset;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020043 cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
Paolo Bonzini412a3c42014-12-03 14:38:01 +010044 offset = compacted ? ret : ebx;
45 ret = max(ret, offset + eax);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020046 }
47
48 xstate_bv >>= 1;
49 feature_bit++;
50 }
51
52 return ret;
53}
54
Sean Christopherson87382002019-12-17 13:32:42 -080055#define F feature_bit
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010056
Nadav Amitdd598092014-09-16 15:10:03 +030057int kvm_update_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity00b27a32011-11-23 16:30:32 +020058{
59 struct kvm_cpuid_entry2 *best;
60 struct kvm_lapic *apic = vcpu->arch.apic;
61
62 best = kvm_find_cpuid_entry(vcpu, 1, 0);
63 if (!best)
Nadav Amitdd598092014-09-16 15:10:03 +030064 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +020065
66 /* Update OSXSAVE bit */
Sean Christophersonb32666b2020-03-02 15:56:31 -080067 if (boot_cpu_has(X86_FEATURE_XSAVE) && best->function == 0x1)
68 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
69 kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
Avi Kivity00b27a32011-11-23 16:30:32 +020070
Sean Christophersonb32666b2020-03-02 15:56:31 -080071 cpuid_entry_change(best, X86_FEATURE_APIC,
72 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
Jim Mattsonc7dd15b2016-11-09 09:50:11 -080073
Avi Kivity00b27a32011-11-23 16:30:32 +020074 if (apic) {
Sean Christopherson4c615342020-03-02 15:56:30 -080075 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
Avi Kivity00b27a32011-11-23 16:30:32 +020076 apic->lapic_timer.timer_mode_mask = 3 << 17;
77 else
78 apic->lapic_timer.timer_mode_mask = 1 << 17;
79 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020080
Huaitong Hanb9baba82016-03-22 16:51:21 +080081 best = kvm_find_cpuid_entry(vcpu, 7, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -080082 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
83 cpuid_entry_change(best, X86_FEATURE_OSPKE,
84 kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
Huaitong Hanb9baba82016-03-22 16:51:21 +080085
Paolo Bonzinid7876f12013-10-02 16:06:15 +020086 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020087 if (!best) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020088 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020089 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
90 } else {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020091 vcpu->arch.guest_supported_xcr0 =
Sean Christophersoncfc48182020-03-02 15:56:23 -080092 (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +000093 vcpu->arch.guest_xstate_size = best->ebx =
Paolo Bonzini412a3c42014-12-03 14:38:01 +010094 xstate_required_size(vcpu->arch.xcr0, false);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020095 }
Paolo Bonzinid7876f12013-10-02 16:06:15 +020096
Paolo Bonzini412a3c42014-12-03 14:38:01 +010097 best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
Sean Christopherson4c615342020-03-02 15:56:30 -080098 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
99 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
Paolo Bonzini412a3c42014-12-03 14:38:01 +0100100 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
101
Nadav Amitdd598092014-09-16 15:10:03 +0300102 /*
Yu Zhangfd8cb432017-08-24 20:27:56 +0800103 * The existing code assumes virtual address is 48-bit or 57-bit in the
104 * canonical address checks; exit if it is ever changed.
Nadav Amitdd598092014-09-16 15:10:03 +0300105 */
106 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
Yu Zhangfd8cb432017-08-24 20:27:56 +0800107 if (best) {
108 int vaddr_bits = (best->eax & 0xff00) >> 8;
109
110 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
111 return -EINVAL;
112 }
Nadav Amitdd598092014-09-16 15:10:03 +0300113
Wanpeng Licaa057a2018-03-12 04:53:03 -0700114 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
115 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
116 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
117 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
118
Wanpeng Li511a85562019-05-21 14:06:54 +0800119 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
120 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800121 if (best)
122 cpuid_entry_change(best, X86_FEATURE_MWAIT,
123 vcpu->arch.ia32_misc_enable_msr &
124 MSR_IA32_MISC_ENABLE_MWAIT);
Wanpeng Li511a85562019-05-21 14:06:54 +0800125 }
126
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300127 /* Update physical-address width */
128 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +0800129 kvm_mmu_reset_context(vcpu);
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300130
Wei Huangc6702c92015-06-19 13:44:45 +0200131 kvm_pmu_refresh(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300132 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200133}
134
135static int is_efer_nx(void)
136{
137 unsigned long long efer = 0;
138
139 rdmsrl_safe(MSR_EFER, &efer);
140 return efer & EFER_NX;
141}
142
143static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
144{
145 int i;
146 struct kvm_cpuid_entry2 *e, *entry;
147
148 entry = NULL;
149 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
150 e = &vcpu->arch.cpuid_entries[i];
151 if (e->function == 0x80000001) {
152 entry = e;
153 break;
154 }
155 }
Sean Christopherson4c615342020-03-02 15:56:30 -0800156 if (entry && cpuid_entry_has(entry, X86_FEATURE_NX) && !is_efer_nx()) {
Sean Christophersonb32666b2020-03-02 15:56:31 -0800157 cpuid_entry_clear(entry, X86_FEATURE_NX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200158 printk(KERN_INFO "kvm: guest NX capability removed\n");
159 }
160}
161
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300162int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
163{
164 struct kvm_cpuid_entry2 *best;
165
166 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
167 if (!best || best->eax < 0x80000008)
168 goto not_found;
169 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
170 if (best)
171 return best->eax & 0xff;
172not_found:
173 return 36;
174}
175EXPORT_SYMBOL_GPL(cpuid_query_maxphyaddr);
176
Avi Kivity00b27a32011-11-23 16:30:32 +0200177/* when an old userspace process fills a new kernel module */
178int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
179 struct kvm_cpuid *cpuid,
180 struct kvm_cpuid_entry __user *entries)
181{
182 int r, i;
Paolo Bonzini83676e92016-06-01 14:09:19 +0200183 struct kvm_cpuid_entry *cpuid_entries = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200184
185 r = -E2BIG;
186 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
187 goto out;
188 r = -ENOMEM;
Paolo Bonzini83676e92016-06-01 14:09:19 +0200189 if (cpuid->nent) {
Kees Cook42bc47b2018-06-12 14:27:11 -0700190 cpuid_entries =
191 vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
192 cpuid->nent));
Paolo Bonzini83676e92016-06-01 14:09:19 +0200193 if (!cpuid_entries)
194 goto out;
195 r = -EFAULT;
196 if (copy_from_user(cpuid_entries, entries,
197 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
198 goto out;
199 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200200 for (i = 0; i < cpuid->nent; i++) {
201 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
202 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
203 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
204 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
205 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
206 vcpu->arch.cpuid_entries[i].index = 0;
207 vcpu->arch.cpuid_entries[i].flags = 0;
208 vcpu->arch.cpuid_entries[i].padding[0] = 0;
209 vcpu->arch.cpuid_entries[i].padding[1] = 0;
210 vcpu->arch.cpuid_entries[i].padding[2] = 0;
211 }
212 vcpu->arch.cpuid_nent = cpuid->nent;
213 cpuid_fix_nx_cap(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200214 kvm_apic_set_version(vcpu);
215 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300216 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200217
Avi Kivity00b27a32011-11-23 16:30:32 +0200218out:
Paolo Bonzini83676e92016-06-01 14:09:19 +0200219 vfree(cpuid_entries);
Avi Kivity00b27a32011-11-23 16:30:32 +0200220 return r;
221}
222
223int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
224 struct kvm_cpuid2 *cpuid,
225 struct kvm_cpuid_entry2 __user *entries)
226{
227 int r;
228
229 r = -E2BIG;
230 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
231 goto out;
232 r = -EFAULT;
233 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
234 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
235 goto out;
236 vcpu->arch.cpuid_nent = cpuid->nent;
237 kvm_apic_set_version(vcpu);
238 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300239 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200240out:
241 return r;
242}
243
244int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
245 struct kvm_cpuid2 *cpuid,
246 struct kvm_cpuid_entry2 __user *entries)
247{
248 int r;
249
250 r = -E2BIG;
251 if (cpuid->nent < vcpu->arch.cpuid_nent)
252 goto out;
253 r = -EFAULT;
254 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
255 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
256 goto out;
257 return 0;
258
259out:
260 cpuid->nent = vcpu->arch.cpuid_nent;
261 return r;
262}
263
Sean Christopherson66a69502020-03-02 15:56:41 -0800264static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
265{
266 reverse_cpuid_check(leaf);
267 kvm_cpu_caps[leaf] &= mask;
268}
269
270void kvm_set_cpu_caps(void)
271{
272 unsigned int f_nx = is_efer_nx() ? F(NX) : 0;
273#ifdef CONFIG_X86_64
274 unsigned int f_gbpages = F(GBPAGES);
275 unsigned int f_lm = F(LM);
276#else
277 unsigned int f_gbpages = 0;
278 unsigned int f_lm = 0;
279#endif
280
281 BUILD_BUG_ON(sizeof(kvm_cpu_caps) >
282 sizeof(boot_cpu_data.x86_capability));
283
284 memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
285 sizeof(kvm_cpu_caps));
286
287 kvm_cpu_cap_mask(CPUID_1_ECX,
288 /*
289 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
290 * advertised to guests via CPUID!
291 */
292 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
293 0 /* DS-CPL, VMX, SMX, EST */ |
294 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
295 F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
296 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
297 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
298 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
299 F(F16C) | F(RDRAND)
300 );
301
302 kvm_cpu_cap_mask(CPUID_1_EDX,
303 F(FPU) | F(VME) | F(DE) | F(PSE) |
304 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
305 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
306 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
307 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
308 0 /* Reserved, DS, ACPI */ | F(MMX) |
309 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
310 0 /* HTT, TM, Reserved, PBE */
311 );
312
313 kvm_cpu_cap_mask(CPUID_7_0_EBX,
314 F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
315 F(BMI2) | F(ERMS) | 0 /*INVPCID*/ | F(RTM) | 0 /*MPX*/ | F(RDSEED) |
316 F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
317 F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
318 F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | 0 /*INTEL_PT*/
319 );
320
321 kvm_cpu_cap_mask(CPUID_7_ECX,
322 F(AVX512VBMI) | F(LA57) | 0 /*PKU*/ | 0 /*OSPKE*/ | F(RDPID) |
323 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
324 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
325 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/
326 );
327 /* Set LA57 based on hardware capability. */
328 if (cpuid_ecx(7) & F(LA57))
329 kvm_cpu_cap_set(X86_FEATURE_LA57);
330
331 kvm_cpu_cap_mask(CPUID_7_EDX,
332 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
333 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
334 F(MD_CLEAR)
335 );
336
337 kvm_cpu_cap_mask(CPUID_7_1_EAX,
338 F(AVX512_BF16)
339 );
340
341 kvm_cpu_cap_mask(CPUID_D_1_EAX,
342 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES)
343 );
344
345 kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
346 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
347 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
348 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
349 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
350 F(TOPOEXT) | F(PERFCTR_CORE)
351 );
352
353 kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
354 F(FPU) | F(VME) | F(DE) | F(PSE) |
355 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
356 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
357 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
358 F(PAT) | F(PSE36) | 0 /* Reserved */ |
359 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
360 F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
361 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
362 );
363
364 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
365 kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
366
367 kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
368 F(CLZERO) | F(XSAVEERPTR) |
369 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
370 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON)
371 );
372
373 kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
374 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
375 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
376 F(PMM) | F(PMM_EN)
377 );
378}
379EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
380
Sean Christophersone53c95e2020-03-02 15:56:19 -0800381struct kvm_cpuid_array {
382 struct kvm_cpuid_entry2 *entries;
383 const int maxnent;
384 int nent;
385};
386
387static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800388 u32 function, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +0200389{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800390 struct kvm_cpuid_entry2 *entry;
391
392 if (array->nent >= array->maxnent)
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800393 return NULL;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800394
395 entry = &array->entries[array->nent++];
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800396
Avi Kivity00b27a32011-11-23 16:30:32 +0200397 entry->function = function;
398 entry->index = index;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200399 entry->flags = 0;
400
Avi Kivity00b27a32011-11-23 16:30:32 +0200401 cpuid_count(entry->function, entry->index,
402 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200403
404 switch (function) {
405 case 2:
406 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
407 break;
408 case 4:
409 case 7:
410 case 0xb:
411 case 0xd:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700412 case 0xf:
413 case 0x10:
414 case 0x12:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200415 case 0x14:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700416 case 0x17:
417 case 0x18:
418 case 0x1f:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200419 case 0x8000001d:
420 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
421 break;
422 }
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800423
424 return entry;
Avi Kivity00b27a32011-11-23 16:30:32 +0200425}
426
Sean Christophersone53c95e2020-03-02 15:56:19 -0800427static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200428{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800429 struct kvm_cpuid_entry2 *entry = &array->entries[array->nent];
430
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200431 entry->function = func;
432 entry->index = 0;
433 entry->flags = 0;
434
Borislav Petkov84cffe42013-10-29 12:54:56 +0100435 switch (func) {
436 case 0:
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200437 entry->eax = 7;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800438 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100439 break;
440 case 1:
441 entry->ecx = F(MOVBE);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800442 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100443 break;
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200444 case 7:
445 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200446 entry->eax = 0;
447 entry->ecx = F(RDPID);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800448 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100449 default:
450 break;
451 }
452
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200453 return 0;
454}
455
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800456static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200457{
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800458 switch (entry->index) {
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200459 case 0:
Jing Liu0b774622019-07-11 13:49:57 +0800460 entry->eax = min(entry->eax, 1u);
Sean Christophersone745e372020-03-02 15:56:32 -0800461 cpuid_entry_mask(entry, CPUID_7_0_EBX);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200462 /* TSC_ADJUST is emulated */
Sean Christophersonb32666b2020-03-02 15:56:31 -0800463 cpuid_entry_set(entry, X86_FEATURE_TSC_ADJUST);
Sean Christophersone745e372020-03-02 15:56:32 -0800464 cpuid_entry_mask(entry, CPUID_7_ECX);
Sean Christophersone745e372020-03-02 15:56:32 -0800465 cpuid_entry_mask(entry, CPUID_7_EDX);
Paolo Bonzini0c549142019-08-19 17:24:07 +0200466 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800467 cpuid_entry_set(entry, X86_FEATURE_SPEC_CTRL);
Paolo Bonzini0c549142019-08-19 17:24:07 +0200468 if (boot_cpu_has(X86_FEATURE_STIBP))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800469 cpuid_entry_set(entry, X86_FEATURE_INTEL_STIBP);
Sean Christophersonacfad332020-03-02 15:56:15 -0800470 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800471 cpuid_entry_set(entry, X86_FEATURE_SPEC_CTRL_SSBD);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200472 /*
473 * We emulate ARCH_CAPABILITIES in software even
474 * if the host doesn't support it.
475 */
Sean Christophersonb32666b2020-03-02 15:56:31 -0800476 cpuid_entry_set(entry, X86_FEATURE_ARCH_CAPABILITIES);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200477 break;
Jing Liu0b774622019-07-11 13:49:57 +0800478 case 1:
Sean Christopherson66a69502020-03-02 15:56:41 -0800479 cpuid_entry_mask(entry, CPUID_7_1_EAX);
Jing Liu0b774622019-07-11 13:49:57 +0800480 entry->ebx = 0;
481 entry->ecx = 0;
482 entry->edx = 0;
483 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200484 default:
485 WARN_ON_ONCE(1);
486 entry->eax = 0;
487 entry->ebx = 0;
488 entry->ecx = 0;
489 entry->edx = 0;
490 break;
491 }
492}
493
Sean Christophersone53c95e2020-03-02 15:56:19 -0800494static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200495{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800496 struct kvm_cpuid_entry2 *entry;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800497 int r, i, max_idx;
Chao Peng86f52012018-10-24 16:05:11 +0800498 unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200499
Avi Kivity00b27a32011-11-23 16:30:32 +0200500 /* all calls to cpuid_count() should be made on the same cpu */
501 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200502
503 r = -E2BIG;
504
Sean Christophersone53c95e2020-03-02 15:56:19 -0800505 entry = do_host_cpuid(array, function, 0);
506 if (WARN_ON(!entry))
Sasha Levin831bf662011-11-28 11:20:29 +0200507 goto out;
508
Avi Kivity00b27a32011-11-23 16:30:32 +0200509 switch (function) {
510 case 0:
Like Xua87f2d32019-06-06 09:18:45 +0800511 /* Limited to the highest leaf implemented in KVM. */
512 entry->eax = min(entry->eax, 0x1fU);
Avi Kivity00b27a32011-11-23 16:30:32 +0200513 break;
514 case 1:
Sean Christophersone745e372020-03-02 15:56:32 -0800515 cpuid_entry_mask(entry, CPUID_1_EDX);
Sean Christophersone745e372020-03-02 15:56:32 -0800516 cpuid_entry_mask(entry, CPUID_1_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200517 /* we support x2apic emulation even if host does not support
518 * it since we emulate x2apic in software */
Sean Christophersonb32666b2020-03-02 15:56:31 -0800519 cpuid_entry_set(entry, X86_FEATURE_X2APIC);
Avi Kivity00b27a32011-11-23 16:30:32 +0200520 break;
521 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
522 * may return different values. This forces us to get_cpu() before
523 * issuing the first command, and also to emulate this annoying behavior
524 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800525 case 2:
Avi Kivity00b27a32011-11-23 16:30:32 +0200526 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800527
528 for (i = 1, max_idx = entry->eax & 0xff; i < max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800529 entry = do_host_cpuid(array, function, 0);
530 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200531 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200532 }
533 break;
Jim Mattson32a243d2019-03-27 13:15:36 -0700534 /* functions 4 and 0x8000001d have additional index. */
535 case 4:
Sean Christophersonc8629032020-03-02 15:56:18 -0800536 case 0x8000001d:
537 /*
538 * Read entries until the cache type in the previous entry is
539 * zero, i.e. indicates an invalid entry.
540 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800541 for (i = 1; entry->eax & 0x1f; ++i) {
542 entry = do_host_cpuid(array, function, i);
543 if (!entry)
Sean Christopherson0fc62672020-03-02 15:56:08 -0800544 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200545 }
546 break;
Jan Kiszkae453aa02015-05-24 17:22:38 +0200547 case 6: /* Thermal management */
548 entry->eax = 0x4; /* allow ARAT */
549 entry->ebx = 0;
550 entry->ecx = 0;
551 entry->edx = 0;
552 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200553 /* function 7 has additional index. */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800554 case 7:
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800555 do_cpuid_7_mask(entry);
Sean Christopherson87849b12020-03-02 15:56:13 -0800556
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800557 for (i = 1, max_idx = entry->eax; i <= max_idx; i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800558 entry = do_host_cpuid(array, function, i);
559 if (!entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200560 goto out;
561
Sean Christophersone53c95e2020-03-02 15:56:19 -0800562 do_cpuid_7_mask(entry);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200563 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200564 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200565 case 9:
566 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200567 case 0xa: { /* Architectural Performance Monitoring */
568 struct x86_pmu_capability cap;
569 union cpuid10_eax eax;
570 union cpuid10_edx edx;
571
572 perf_get_x86_pmu_capability(&cap);
573
574 /*
575 * Only support guest architectural pmu on a host
576 * with architectural pmu.
577 */
578 if (!cap.version)
579 memset(&cap, 0, sizeof(cap));
580
581 eax.split.version_id = min(cap.version, 2);
582 eax.split.num_counters = cap.num_counters_gp;
583 eax.split.bit_width = cap.bit_width_gp;
584 eax.split.mask_length = cap.events_mask_len;
585
586 edx.split.num_counters_fixed = cap.num_counters_fixed;
587 edx.split.bit_width_fixed = cap.bit_width_fixed;
588 edx.split.reserved = 0;
589
590 entry->eax = eax.full;
591 entry->ebx = cap.events_mask;
592 entry->ecx = 0;
593 entry->edx = edx.full;
594 break;
595 }
Like Xua87f2d32019-06-06 09:18:45 +0800596 /*
597 * Per Intel's SDM, the 0x1f is a superset of 0xb,
598 * thus they can be handled by common code.
599 */
600 case 0x1f:
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800601 case 0xb:
Jim Mattsona1a640b2019-09-25 11:17:14 -0700602 /*
Sean Christophersone53c95e2020-03-02 15:56:19 -0800603 * Populate entries until the level type (ECX[15:8]) of the
604 * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
605 * the starting entry, filled by the primary do_host_cpuid().
Jim Mattsona1a640b2019-09-25 11:17:14 -0700606 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800607 for (i = 1; entry->ecx & 0xff00; ++i) {
608 entry = do_host_cpuid(array, function, i);
609 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200610 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200611 }
612 break;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800613 case 0xd:
614 entry->eax &= supported_xcr0;
615 entry->ebx = xstate_required_size(supported_xcr0, false);
Radim Krčmáře08e8332014-12-04 18:30:41 +0100616 entry->ecx = entry->ebx;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800617 entry->edx &= supported_xcr0 >> 32;
618 if (!supported_xcr0)
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100619 break;
620
Sean Christophersone53c95e2020-03-02 15:56:19 -0800621 entry = do_host_cpuid(array, function, 1);
622 if (!entry)
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800623 goto out;
624
Sean Christophersone745e372020-03-02 15:56:32 -0800625 cpuid_entry_mask(entry, CPUID_D_1_EAX);
Sean Christopherson9e6d01c2020-03-02 15:56:40 -0800626
627 if (!kvm_x86_ops->xsaves_supported())
628 cpuid_entry_clear(entry, X86_FEATURE_XSAVES);
629
Sean Christophersone53c95e2020-03-02 15:56:19 -0800630 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
Sean Christophersoncfc48182020-03-02 15:56:23 -0800631 entry->ebx = xstate_required_size(supported_xcr0, true);
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800632 else
Sean Christophersone53c95e2020-03-02 15:56:19 -0800633 entry->ebx = 0;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800634 /* Saving XSS controlled state via XSAVES isn't supported. */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800635 entry->ecx = 0;
636 entry->edx = 0;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800637
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800638 for (i = 2; i < 64; ++i) {
Sean Christophersoncfc48182020-03-02 15:56:23 -0800639 if (!(supported_xcr0 & BIT_ULL(i)))
Sean Christopherson1893c942020-03-02 15:56:10 -0800640 continue;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800641
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800642 entry = do_host_cpuid(array, function, i);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800643 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200644 goto out;
645
Sean Christopherson91001d42020-03-02 15:56:11 -0800646 /*
Sean Christophersoncfc48182020-03-02 15:56:23 -0800647 * The supported check above should have filtered out
Sean Christopherson91001d42020-03-02 15:56:11 -0800648 * invalid sub-leafs as well as sub-leafs managed by
649 * IA32_XSS MSR. Only XCR0-managed sub-leafs should
650 * reach this point, and they should have a non-zero
651 * save state size.
652 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800653 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 1))) {
654 --array->nent;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800655 continue;
Sean Christopherson8b2fc442020-03-02 15:56:12 -0800656 }
Sean Christopherson91001d42020-03-02 15:56:11 -0800657
Sean Christophersone53c95e2020-03-02 15:56:19 -0800658 entry->ecx = 0;
659 entry->edx = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200660 }
661 break;
Chao Peng86f52012018-10-24 16:05:11 +0800662 /* Intel PT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800663 case 0x14:
Sean Christopherson73920792020-03-02 15:56:26 -0800664 if (!f_intel_pt) {
665 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Chao Peng86f52012018-10-24 16:05:11 +0800666 break;
Sean Christopherson73920792020-03-02 15:56:26 -0800667 }
Chao Peng86f52012018-10-24 16:05:11 +0800668
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800669 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800670 if (!do_host_cpuid(array, function, i))
Chao Peng86f52012018-10-24 16:05:11 +0800671 goto out;
Chao Peng86f52012018-10-24 16:05:11 +0800672 }
673 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200674 case KVM_CPUID_SIGNATURE: {
Mathias Krause326d07c2012-08-30 01:30:13 +0200675 static const char signature[12] = "KVMKVMKVM\0\0";
676 const u32 *sigptr = (const u32 *)signature;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +0300677 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +0200678 entry->ebx = sigptr[0];
679 entry->ecx = sigptr[1];
680 entry->edx = sigptr[2];
681 break;
682 }
683 case KVM_CPUID_FEATURES:
684 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
685 (1 << KVM_FEATURE_NOP_IO_DELAY) |
686 (1 << KVM_FEATURE_CLOCKSOURCE2) |
687 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300688 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +0530689 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
Wanpeng Lif38a7b72017-12-12 17:33:04 -0800690 (1 << KVM_FEATURE_PV_UNHALT) |
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100691 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
Wanpeng Li4180bf12018-07-23 14:39:54 +0800692 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -0300693 (1 << KVM_FEATURE_PV_SEND_IPI) |
Wanpeng Li32b72ec2019-06-11 20:23:50 +0800694 (1 << KVM_FEATURE_POLL_CONTROL) |
695 (1 << KVM_FEATURE_PV_SCHED_YIELD);
Avi Kivity00b27a32011-11-23 16:30:32 +0200696
697 if (sched_info_on())
698 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
699
700 entry->ebx = 0;
701 entry->ecx = 0;
702 entry->edx = 0;
703 break;
704 case 0x80000000:
Brijesh Singh8765d752017-12-04 10:57:25 -0600705 entry->eax = min(entry->eax, 0x8000001f);
Avi Kivity00b27a32011-11-23 16:30:32 +0200706 break;
707 case 0x80000001:
Sean Christophersone745e372020-03-02 15:56:32 -0800708 cpuid_entry_mask(entry, CPUID_8000_0001_EDX);
Sean Christopherson66a69502020-03-02 15:56:41 -0800709 /* Add it manually because it may not be in host CPUID. */
Paolo Bonzinifb7d4372020-03-03 15:54:39 +0100710 if (!tdp_enabled)
711 cpuid_entry_set(entry, X86_FEATURE_GBPAGES);
Sean Christophersone745e372020-03-02 15:56:32 -0800712 cpuid_entry_mask(entry, CPUID_8000_0001_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200713 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -0300714 case 0x80000007: /* Advanced power management */
715 /* invariant TSC is CPUID.80000007H:EDX[8] */
716 entry->edx &= (1 << 8);
717 /* mask against host */
718 entry->edx &= boot_cpu_data.x86_power;
719 entry->eax = entry->ebx = entry->ecx = 0;
720 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200721 case 0x80000008: {
722 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
723 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
724 unsigned phys_as = entry->eax & 0xff;
725
726 if (!g_phys_as)
727 g_phys_as = phys_as;
728 entry->eax = g_phys_as | (virt_as << 8);
Ashok Raj15d45072018-02-01 22:59:43 +0100729 entry->edx = 0;
Sean Christophersone745e372020-03-02 15:56:32 -0800730 cpuid_entry_mask(entry, CPUID_8000_0008_EBX);
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -0400731 /*
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400732 * AMD has separate bits for each SPEC_CTRL bit.
733 * arch/x86/kernel/cpu/bugs.c is kind enough to
734 * record that in cpufeatures so use them.
735 */
736 if (boot_cpu_has(X86_FEATURE_IBPB))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800737 cpuid_entry_set(entry, X86_FEATURE_AMD_IBPB);
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400738 if (boot_cpu_has(X86_FEATURE_IBRS))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800739 cpuid_entry_set(entry, X86_FEATURE_AMD_IBRS);
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400740 if (boot_cpu_has(X86_FEATURE_STIBP))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800741 cpuid_entry_set(entry, X86_FEATURE_AMD_STIBP);
Sean Christophersonacfad332020-03-02 15:56:15 -0800742 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800743 cpuid_entry_set(entry, X86_FEATURE_AMD_SSBD);
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400744 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800745 cpuid_entry_set(entry, X86_FEATURE_AMD_SSB_NO);
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400746 /*
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -0400747 * The preference is to use SPEC CTRL MSR instead of the
748 * VIRT_SPEC MSR.
749 */
750 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
751 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800752 cpuid_entry_set(entry, X86_FEATURE_VIRT_SSBD);
Avi Kivity00b27a32011-11-23 16:30:32 +0200753 break;
754 }
755 case 0x80000019:
756 entry->ecx = entry->edx = 0;
757 break;
758 case 0x8000001a:
Jim Mattson382409b2019-03-27 13:15:37 -0700759 case 0x8000001e:
Avi Kivity00b27a32011-11-23 16:30:32 +0200760 break;
Peter Gondac1de0f22019-11-21 12:33:43 -0800761 /* Support memory encryption cpuid if host supports it */
762 case 0x8000001F:
763 if (!boot_cpu_has(X86_FEATURE_SEV))
764 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
765 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200766 /*Add support for Centaur's CPUID instruction*/
767 case 0xC0000000:
768 /*Just support up to 0xC0000004 now*/
769 entry->eax = min(entry->eax, 0xC0000004);
770 break;
771 case 0xC0000001:
Sean Christophersone745e372020-03-02 15:56:32 -0800772 cpuid_entry_mask(entry, CPUID_C000_0001_EDX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200773 break;
774 case 3: /* Processor serial number */
775 case 5: /* MONITOR/MWAIT */
Avi Kivity00b27a32011-11-23 16:30:32 +0200776 case 0xC0000002:
777 case 0xC0000003:
778 case 0xC0000004:
779 default:
780 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
781 break;
782 }
783
Sean Christopherson160b486f2020-03-02 15:56:27 -0800784 kvm_x86_ops->set_supported_cpuid(entry);
Avi Kivity00b27a32011-11-23 16:30:32 +0200785
Sasha Levin831bf662011-11-28 11:20:29 +0200786 r = 0;
787
788out:
Avi Kivity00b27a32011-11-23 16:30:32 +0200789 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200790
791 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200792}
793
Sean Christophersone53c95e2020-03-02 15:56:19 -0800794static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
795 unsigned int type)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200796{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800797 if (array->nent >= array->maxnent)
Paolo Bonzini433f4ba2019-12-04 10:28:54 +0100798 return -E2BIG;
799
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200800 if (type == KVM_GET_EMULATED_CPUID)
Sean Christophersone53c95e2020-03-02 15:56:19 -0800801 return __do_cpuid_func_emulated(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200802
Sean Christophersone53c95e2020-03-02 15:56:19 -0800803 return __do_cpuid_func(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200804}
805
Sean Christopherson8b860792020-03-02 15:56:06 -0800806#define CENTAUR_CPUID_SIGNATURE 0xC0000000
Sasha Levin831bf662011-11-28 11:20:29 +0200807
Sean Christophersone53c95e2020-03-02 15:56:19 -0800808static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
809 unsigned int type)
Sean Christopherson619a17f2020-03-02 15:56:05 -0800810{
811 u32 limit;
812 int r;
813
Sean Christopherson8b860792020-03-02 15:56:06 -0800814 if (func == CENTAUR_CPUID_SIGNATURE &&
815 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
816 return 0;
817
Sean Christophersone53c95e2020-03-02 15:56:19 -0800818 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -0800819 if (r)
820 return r;
821
Sean Christophersone53c95e2020-03-02 15:56:19 -0800822 limit = array->entries[array->nent - 1].eax;
Sean Christopherson619a17f2020-03-02 15:56:05 -0800823 for (func = func + 1; func <= limit; ++func) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800824 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -0800825 if (r)
826 break;
827 }
828
829 return r;
830}
831
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200832static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
833 __u32 num_entries, unsigned int ioctl_type)
834{
835 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100836 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200837
838 if (ioctl_type != KVM_GET_EMULATED_CPUID)
839 return false;
840
841 /*
842 * We want to make sure that ->padding is being passed clean from
843 * userspace in case we want to use it for something in the future.
844 *
845 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
846 * have to give ourselves satisfied only with the emulated side. /me
847 * sheds a tear.
848 */
849 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100850 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
851 return true;
852
853 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200854 return true;
855 }
856 return false;
857}
858
859int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
860 struct kvm_cpuid_entry2 __user *entries,
861 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +0200862{
Sean Christopherson8b860792020-03-02 15:56:06 -0800863 static const u32 funcs[] = {
864 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
Sasha Levin831bf662011-11-28 11:20:29 +0200865 };
Avi Kivity00b27a32011-11-23 16:30:32 +0200866
Sean Christophersone53c95e2020-03-02 15:56:19 -0800867 struct kvm_cpuid_array array = {
868 .nent = 0,
869 .maxnent = cpuid->nent,
870 };
871 int r, i;
Sean Christophersond5a661d2020-03-02 15:56:07 -0800872
Avi Kivity00b27a32011-11-23 16:30:32 +0200873 if (cpuid->nent < 1)
Sean Christophersond5a661d2020-03-02 15:56:07 -0800874 return -E2BIG;
Avi Kivity00b27a32011-11-23 16:30:32 +0200875 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
876 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200877
878 if (sanity_check_entries(entries, cpuid->nent, type))
879 return -EINVAL;
880
Sean Christophersone53c95e2020-03-02 15:56:19 -0800881 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
Kees Cookfad953c2018-06-12 14:27:37 -0700882 cpuid->nent));
Sean Christophersone53c95e2020-03-02 15:56:19 -0800883 if (!array.entries)
Sean Christophersond5a661d2020-03-02 15:56:07 -0800884 return -ENOMEM;
Avi Kivity00b27a32011-11-23 16:30:32 +0200885
Sean Christopherson8b860792020-03-02 15:56:06 -0800886 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800887 r = get_cpuid_func(&array, funcs[i], type);
Sasha Levin831bf662011-11-28 11:20:29 +0200888 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +0200889 goto out_free;
890 }
Sean Christophersone53c95e2020-03-02 15:56:19 -0800891 cpuid->nent = array.nent;
Avi Kivity00b27a32011-11-23 16:30:32 +0200892
Sean Christophersone53c95e2020-03-02 15:56:19 -0800893 if (copy_to_user(entries, array.entries,
894 array.nent * sizeof(struct kvm_cpuid_entry2)))
Sean Christophersond5a661d2020-03-02 15:56:07 -0800895 r = -EFAULT;
Avi Kivity00b27a32011-11-23 16:30:32 +0200896
897out_free:
Sean Christophersone53c95e2020-03-02 15:56:19 -0800898 vfree(array.entries);
Avi Kivity00b27a32011-11-23 16:30:32 +0200899 return r;
900}
901
902static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
903{
904 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
Wanpeng Lia3641632017-06-08 01:22:07 -0700905 struct kvm_cpuid_entry2 *ej;
906 int j = i;
907 int nent = vcpu->arch.cpuid_nent;
Avi Kivity00b27a32011-11-23 16:30:32 +0200908
909 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
910 /* when no next entry is found, the current entry[i] is reselected */
Wanpeng Lia3641632017-06-08 01:22:07 -0700911 do {
912 j = (j + 1) % nent;
913 ej = &vcpu->arch.cpuid_entries[j];
914 } while (ej->function != e->function);
915
916 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
917
918 return j;
Avi Kivity00b27a32011-11-23 16:30:32 +0200919}
920
921/* find an entry with matching function, matching index (if needed), and that
922 * should be read next (if it's stateful) */
923static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
924 u32 function, u32 index)
925{
926 if (e->function != function)
927 return 0;
928 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
929 return 0;
930 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
931 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
932 return 0;
933 return 1;
934}
935
936struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
937 u32 function, u32 index)
938{
939 int i;
940 struct kvm_cpuid_entry2 *best = NULL;
941
942 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
943 struct kvm_cpuid_entry2 *e;
944
945 e = &vcpu->arch.cpuid_entries[i];
946 if (is_matching_cpuid_entry(e, function, index)) {
947 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
948 move_to_next_stateful_cpuid_entry(vcpu, i);
949 best = e;
950 break;
951 }
952 }
953 return best;
954}
955EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
956
Avi Kivity00b27a32011-11-23 16:30:32 +0200957/*
Jim Mattson43561122019-09-25 17:04:17 -0700958 * If the basic or extended CPUID leaf requested is higher than the
959 * maximum supported basic or extended leaf, respectively, then it is
960 * out of range.
Avi Kivity00b27a32011-11-23 16:30:32 +0200961 */
Jim Mattson43561122019-09-25 17:04:17 -0700962static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200963{
Jim Mattson43561122019-09-25 17:04:17 -0700964 struct kvm_cpuid_entry2 *max;
Avi Kivity00b27a32011-11-23 16:30:32 +0200965
Jim Mattson43561122019-09-25 17:04:17 -0700966 max = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
967 return max && function <= max->eax;
Avi Kivity00b27a32011-11-23 16:30:32 +0200968}
969
Yu Zhange911eb32017-08-24 20:27:52 +0800970bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
971 u32 *ecx, u32 *edx, bool check_limit)
Avi Kivity00b27a32011-11-23 16:30:32 +0200972{
Avi Kivity62046e52012-06-07 14:07:48 +0300973 u32 function = *eax, index = *ecx;
Jim Mattson43561122019-09-25 17:04:17 -0700974 struct kvm_cpuid_entry2 *entry;
975 struct kvm_cpuid_entry2 *max;
976 bool found;
Avi Kivity00b27a32011-11-23 16:30:32 +0200977
Jim Mattson43561122019-09-25 17:04:17 -0700978 entry = kvm_find_cpuid_entry(vcpu, function, index);
979 found = entry;
980 /*
981 * Intel CPUID semantics treats any query for an out-of-range
982 * leaf as if the highest basic leaf (i.e. CPUID.0H:EAX) were
Jim Mattson5f41a372019-09-25 17:04:18 -0700983 * requested. AMD CPUID semantics returns all zeroes for any
984 * undefined leaf, whether or not the leaf is in range.
Jim Mattson43561122019-09-25 17:04:17 -0700985 */
Jim Mattson5f41a372019-09-25 17:04:18 -0700986 if (!entry && check_limit && !guest_cpuid_is_amd(vcpu) &&
987 !cpuid_function_in_range(vcpu, function)) {
Jim Mattson43561122019-09-25 17:04:17 -0700988 max = kvm_find_cpuid_entry(vcpu, 0, 0);
989 if (max) {
990 function = max->eax;
991 entry = kvm_find_cpuid_entry(vcpu, function, index);
992 }
Yu Zhange911eb32017-08-24 20:27:52 +0800993 }
Jim Mattson43561122019-09-25 17:04:17 -0700994 if (entry) {
995 *eax = entry->eax;
996 *ebx = entry->ebx;
997 *ecx = entry->ecx;
998 *edx = entry->edx;
Paolo Bonziniedef5c32019-11-18 12:23:00 -0500999 if (function == 7 && index == 0) {
1000 u64 data;
1001 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1002 (data & TSX_CTRL_CPUID_CLEAR))
1003 *ebx &= ~(F(RTM) | F(HLE));
1004 }
Jim Mattson43561122019-09-25 17:04:17 -07001005 } else {
Avi Kivity62046e52012-06-07 14:07:48 +03001006 *eax = *ebx = *ecx = *edx = 0;
Jim Mattson43561122019-09-25 17:04:17 -07001007 /*
1008 * When leaf 0BH or 1FH is defined, CL is pass-through
1009 * and EDX is always the x2APIC ID, even for undefined
1010 * subleaves. Index 1 will exist iff the leaf is
1011 * implemented, so we pass through CL iff leaf 1
1012 * exists. EDX can be copied from any existing index.
1013 */
1014 if (function == 0xb || function == 0x1f) {
1015 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1016 if (entry) {
1017 *ecx = index & 0xff;
1018 *edx = entry->edx;
1019 }
1020 }
1021 }
1022 trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx, found);
1023 return found;
Avi Kivity62046e52012-06-07 14:07:48 +03001024}
Julian Stecklina66f7b722012-12-05 15:26:19 +01001025EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +03001026
Kyle Huey6a908b62016-11-29 12:40:37 -08001027int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity62046e52012-06-07 14:07:48 +03001028{
Jiang Biao1e131752016-11-07 08:55:49 +08001029 u32 eax, ebx, ecx, edx;
Avi Kivity62046e52012-06-07 14:07:48 +03001030
Kyle Hueydb2336a2017-03-20 01:16:28 -07001031 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1032 return 1;
1033
Sean Christophersonde3cd112019-04-30 10:36:17 -07001034 eax = kvm_rax_read(vcpu);
1035 ecx = kvm_rcx_read(vcpu);
Yu Zhange911eb32017-08-24 20:27:52 +08001036 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, true);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001037 kvm_rax_write(vcpu, eax);
1038 kvm_rbx_write(vcpu, ebx);
1039 kvm_rcx_write(vcpu, ecx);
1040 kvm_rdx_write(vcpu, edx);
Kyle Huey6affcbe2016-11-29 12:40:40 -08001041 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +02001042}
1043EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);