blob: 41b0dc37720e0f0b572827c6e50d343aa6b43552 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010 */
11
12#include <linux/kvm_host.h>
13#include <linux/export.h>
14#include <linux/vmalloc.h>
15#include <linux/uaccess.h>
16#include <linux/sched/stat.h>
17
18#include <asm/processor.h>
19#include <asm/user.h>
20#include <asm/fpu/xstate.h>
21#include "cpuid.h"
22#include "lapic.h"
23#include "mmu.h"
24#include "trace.h"
25#include "pmu.h"
26
Olivier Deprez157378f2022-04-04 15:47:50 +020027/*
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034static u32 xstate_required_size(u64 xstate_bv, bool compacted)
35{
36 int feature_bit = 0;
37 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
38
39 xstate_bv &= XFEATURE_MASK_EXTEND;
40 while (xstate_bv) {
41 if (xstate_bv & 0x1) {
42 u32 eax, ebx, ecx, edx, offset;
43 cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
44 offset = compacted ? ret : ebx;
45 ret = max(ret, offset + eax);
46 }
47
48 xstate_bv >>= 1;
49 feature_bit++;
50 }
51
52 return ret;
53}
54
Olivier Deprez157378f2022-04-04 15:47:50 +020055#define F feature_bit
56
57static inline struct kvm_cpuid_entry2 *cpuid_entry2_find(
58 struct kvm_cpuid_entry2 *entries, int nent, u32 function, u32 index)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059{
Olivier Deprez157378f2022-04-04 15:47:50 +020060 struct kvm_cpuid_entry2 *e;
61 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062
Olivier Deprez157378f2022-04-04 15:47:50 +020063 for (i = 0; i < nent; i++) {
64 e = &entries[i];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065
Olivier Deprez157378f2022-04-04 15:47:50 +020066 if (e->function == function && (e->index == index ||
67 !(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX)))
68 return e;
69 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070
Olivier Deprez157378f2022-04-04 15:47:50 +020071 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072}
73
Olivier Deprez157378f2022-04-04 15:47:50 +020074static int kvm_check_cpuid(struct kvm_cpuid_entry2 *entries, int nent)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075{
76 struct kvm_cpuid_entry2 *best;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077
78 /*
79 * The existing code assumes virtual address is 48-bit or 57-bit in the
80 * canonical address checks; exit if it is ever changed.
81 */
Olivier Deprez157378f2022-04-04 15:47:50 +020082 best = cpuid_entry2_find(entries, nent, 0x80000008, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 if (best) {
84 int vaddr_bits = (best->eax & 0xff00) >> 8;
85
86 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
87 return -EINVAL;
88 }
89
Olivier Deprez157378f2022-04-04 15:47:50 +020090 return 0;
91}
92
93void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
94{
95 struct kvm_cpuid_entry2 *best;
96
97 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
98
99 /*
100 * save the feature bitmap to avoid cpuid lookup for every PV
101 * operation
102 */
103 if (best)
104 vcpu->arch.pv_cpuid.features = best->eax;
105}
106
107void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
108{
109 struct kvm_cpuid_entry2 *best;
110
111 best = kvm_find_cpuid_entry(vcpu, 1, 0);
112 if (best) {
113 /* Update OSXSAVE bit */
114 if (boot_cpu_has(X86_FEATURE_XSAVE))
115 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
116 kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
117
118 cpuid_entry_change(best, X86_FEATURE_APIC,
119 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
120 }
121
122 best = kvm_find_cpuid_entry(vcpu, 7, 0);
123 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
124 cpuid_entry_change(best, X86_FEATURE_OSPKE,
125 kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
126
127 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
128 if (best)
129 best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
130
131 best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
132 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
133 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
134 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
135
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
137 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
138 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
139 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
140
David Brazdil0f672f62019-12-10 10:32:29 +0000141 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
142 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +0200143 if (best)
144 cpuid_entry_change(best, X86_FEATURE_MWAIT,
145 vcpu->arch.ia32_misc_enable_msr &
146 MSR_IA32_MISC_ENABLE_MWAIT);
147 }
148}
149
150static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
151{
152 struct kvm_lapic *apic = vcpu->arch.apic;
153 struct kvm_cpuid_entry2 *best;
154
155 best = kvm_find_cpuid_entry(vcpu, 1, 0);
156 if (best && apic) {
157 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
158 apic->lapic_timer.timer_mode_mask = 3 << 17;
159 else
160 apic->lapic_timer.timer_mode_mask = 1 << 17;
161
162 kvm_apic_set_version(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +0000163 }
164
Olivier Deprez157378f2022-04-04 15:47:50 +0200165 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
166 if (!best)
167 vcpu->arch.guest_supported_xcr0 = 0;
168 else
169 vcpu->arch.guest_supported_xcr0 =
170 (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
171
172 kvm_update_pv_runtime(vcpu);
173
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
175 kvm_mmu_reset_context(vcpu);
176
177 kvm_pmu_refresh(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +0200178 vcpu->arch.cr4_guest_rsvd_bits =
179 __cr4_reserved_bits(guest_cpuid_has, vcpu);
180
181 vcpu->arch.cr3_lm_rsvd_bits = rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
182
183 /* Invoke the vendor callback only after the above state is updated. */
184 kvm_x86_ops.vcpu_after_set_cpuid(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000185}
186
187static int is_efer_nx(void)
188{
Olivier Deprez157378f2022-04-04 15:47:50 +0200189 return host_efer & EFER_NX;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190}
191
192static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
193{
194 int i;
195 struct kvm_cpuid_entry2 *e, *entry;
196
197 entry = NULL;
198 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
199 e = &vcpu->arch.cpuid_entries[i];
200 if (e->function == 0x80000001) {
201 entry = e;
202 break;
203 }
204 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200205 if (entry && cpuid_entry_has(entry, X86_FEATURE_NX) && !is_efer_nx()) {
206 cpuid_entry_clear(entry, X86_FEATURE_NX);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207 printk(KERN_INFO "kvm: guest NX capability removed\n");
208 }
209}
210
211int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
212{
213 struct kvm_cpuid_entry2 *best;
214
215 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
216 if (!best || best->eax < 0x80000008)
217 goto not_found;
218 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
219 if (best)
220 return best->eax & 0xff;
221not_found:
222 return 36;
223}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224
225/* when an old userspace process fills a new kernel module */
226int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
227 struct kvm_cpuid *cpuid,
228 struct kvm_cpuid_entry __user *entries)
229{
230 int r, i;
Olivier Deprez157378f2022-04-04 15:47:50 +0200231 struct kvm_cpuid_entry *e = NULL;
232 struct kvm_cpuid_entry2 *e2 = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000233
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Olivier Deprez157378f2022-04-04 15:47:50 +0200235 return -E2BIG;
236
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000237 if (cpuid->nent) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200238 e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent));
239 if (IS_ERR(e))
240 return PTR_ERR(e);
241
242 e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
243 if (!e2) {
244 r = -ENOMEM;
245 goto out_free_cpuid;
246 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247 }
248 for (i = 0; i < cpuid->nent; i++) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200249 e2[i].function = e[i].function;
250 e2[i].eax = e[i].eax;
251 e2[i].ebx = e[i].ebx;
252 e2[i].ecx = e[i].ecx;
253 e2[i].edx = e[i].edx;
254 e2[i].index = 0;
255 e2[i].flags = 0;
256 e2[i].padding[0] = 0;
257 e2[i].padding[1] = 0;
258 e2[i].padding[2] = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260
Olivier Deprez157378f2022-04-04 15:47:50 +0200261 r = kvm_check_cpuid(e2, cpuid->nent);
262 if (r) {
263 kvfree(e2);
264 goto out_free_cpuid;
265 }
266
267 kvfree(vcpu->arch.cpuid_entries);
268 vcpu->arch.cpuid_entries = e2;
269 vcpu->arch.cpuid_nent = cpuid->nent;
270
271 cpuid_fix_nx_cap(vcpu);
272 kvm_update_cpuid_runtime(vcpu);
273 kvm_vcpu_after_set_cpuid(vcpu);
274
275out_free_cpuid:
276 kvfree(e);
277
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000278 return r;
279}
280
281int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
282 struct kvm_cpuid2 *cpuid,
283 struct kvm_cpuid_entry2 __user *entries)
284{
Olivier Deprez157378f2022-04-04 15:47:50 +0200285 struct kvm_cpuid_entry2 *e2 = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000286 int r;
287
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Olivier Deprez157378f2022-04-04 15:47:50 +0200289 return -E2BIG;
290
291 if (cpuid->nent) {
292 e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent));
293 if (IS_ERR(e2))
294 return PTR_ERR(e2);
295 }
296
297 r = kvm_check_cpuid(e2, cpuid->nent);
298 if (r) {
299 kvfree(e2);
300 return r;
301 }
302
303 kvfree(vcpu->arch.cpuid_entries);
304 vcpu->arch.cpuid_entries = e2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305 vcpu->arch.cpuid_nent = cpuid->nent;
Olivier Deprez157378f2022-04-04 15:47:50 +0200306
307 kvm_update_cpuid_runtime(vcpu);
308 kvm_vcpu_after_set_cpuid(vcpu);
309
310 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311}
312
313int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
314 struct kvm_cpuid2 *cpuid,
315 struct kvm_cpuid_entry2 __user *entries)
316{
317 int r;
318
319 r = -E2BIG;
320 if (cpuid->nent < vcpu->arch.cpuid_nent)
321 goto out;
322 r = -EFAULT;
Olivier Deprez157378f2022-04-04 15:47:50 +0200323 if (copy_to_user(entries, vcpu->arch.cpuid_entries,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000324 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
325 goto out;
326 return 0;
327
328out:
329 cpuid->nent = vcpu->arch.cpuid_nent;
330 return r;
331}
332
Olivier Deprez157378f2022-04-04 15:47:50 +0200333static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000334{
Olivier Deprez157378f2022-04-04 15:47:50 +0200335 const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
336 struct kvm_cpuid_entry2 entry;
337
338 reverse_cpuid_check(leaf);
339 kvm_cpu_caps[leaf] &= mask;
340
341 cpuid_count(cpuid.function, cpuid.index,
342 &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
343
344 kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000345}
346
Olivier Deprez157378f2022-04-04 15:47:50 +0200347void kvm_set_cpu_caps(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348{
Olivier Deprez157378f2022-04-04 15:47:50 +0200349 unsigned int f_nx = is_efer_nx() ? F(NX) : 0;
350#ifdef CONFIG_X86_64
351 unsigned int f_gbpages = F(GBPAGES);
352 unsigned int f_lm = F(LM);
353#else
354 unsigned int f_gbpages = 0;
355 unsigned int f_lm = 0;
356#endif
357
358 BUILD_BUG_ON(sizeof(kvm_cpu_caps) >
359 sizeof(boot_cpu_data.x86_capability));
360
361 memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
362 sizeof(kvm_cpu_caps));
363
364 kvm_cpu_cap_mask(CPUID_1_ECX,
365 /*
366 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
367 * advertised to guests via CPUID!
368 */
369 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
370 0 /* DS-CPL, VMX, SMX, EST */ |
371 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
372 F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
373 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
374 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
375 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
376 F(F16C) | F(RDRAND)
377 );
378 /* KVM emulates x2apic in software irrespective of host support. */
379 kvm_cpu_cap_set(X86_FEATURE_X2APIC);
380
381 kvm_cpu_cap_mask(CPUID_1_EDX,
382 F(FPU) | F(VME) | F(DE) | F(PSE) |
383 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
384 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
385 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
386 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
387 0 /* Reserved, DS, ACPI */ | F(MMX) |
388 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
389 0 /* HTT, TM, Reserved, PBE */
390 );
391
392 kvm_cpu_cap_mask(CPUID_7_0_EBX,
393 F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
394 F(BMI2) | F(ERMS) | 0 /*INVPCID*/ | F(RTM) | 0 /*MPX*/ | F(RDSEED) |
395 F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
396 F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
397 F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | 0 /*INTEL_PT*/
398 );
399
400 kvm_cpu_cap_mask(CPUID_7_ECX,
401 F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
402 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
403 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
404 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/
405 );
406 /* Set LA57 based on hardware capability. */
407 if (cpuid_ecx(7) & F(LA57))
408 kvm_cpu_cap_set(X86_FEATURE_LA57);
409
410 /*
411 * PKU not yet implemented for shadow paging and requires OSPKE
412 * to be set on the host. Clear it if that is not the case
413 */
414 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
415 kvm_cpu_cap_clear(X86_FEATURE_PKU);
416
417 kvm_cpu_cap_mask(CPUID_7_EDX,
418 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
419 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
420 F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
421 F(SERIALIZE) | F(TSXLDTRK)
422 );
423
424 /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
425 kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
426 kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
427
428 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
429 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
430 if (boot_cpu_has(X86_FEATURE_STIBP))
431 kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
432 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
433 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
434
435 kvm_cpu_cap_mask(CPUID_7_1_EAX,
436 F(AVX512_BF16)
437 );
438
439 kvm_cpu_cap_mask(CPUID_D_1_EAX,
440 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES)
441 );
442
443 kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
444 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
445 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
446 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
447 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
448 F(TOPOEXT) | F(PERFCTR_CORE)
449 );
450
451 kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
452 F(FPU) | F(VME) | F(DE) | F(PSE) |
453 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
454 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
455 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
456 F(PAT) | F(PSE36) | 0 /* Reserved */ |
457 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
458 F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
459 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
460 );
461
462 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
463 kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
464
465 kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
466 F(CLZERO) | F(XSAVEERPTR) |
467 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
468 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON)
469 );
470
471 /*
472 * AMD has separate bits for each SPEC_CTRL bit.
473 * arch/x86/kernel/cpu/bugs.c is kind enough to
474 * record that in cpufeatures so use them.
475 */
476 if (boot_cpu_has(X86_FEATURE_IBPB))
477 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
478 if (boot_cpu_has(X86_FEATURE_IBRS))
479 kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
480 if (boot_cpu_has(X86_FEATURE_STIBP))
481 kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
482 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
483 kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
484 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
485 kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
486 /*
487 * The preference is to use SPEC CTRL MSR instead of the
488 * VIRT_SPEC MSR.
489 */
490 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
491 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
492 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
493
494 /*
495 * Hide all SVM features by default, SVM will set the cap bits for
496 * features it emulates and/or exposes for L1.
497 */
498 kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
499
500 kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
501 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
502 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
503 F(PMM) | F(PMM_EN)
504 );
505}
506EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
507
508struct kvm_cpuid_array {
509 struct kvm_cpuid_entry2 *entries;
510 int maxnent;
511 int nent;
512};
513
514static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
515 u32 function, u32 index)
516{
517 struct kvm_cpuid_entry2 *entry;
518
519 if (array->nent >= array->maxnent)
520 return NULL;
521
522 entry = &array->entries[array->nent++];
523
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524 entry->function = function;
525 entry->index = index;
David Brazdil0f672f62019-12-10 10:32:29 +0000526 entry->flags = 0;
527
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528 cpuid_count(entry->function, entry->index,
529 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
David Brazdil0f672f62019-12-10 10:32:29 +0000530
531 switch (function) {
David Brazdil0f672f62019-12-10 10:32:29 +0000532 case 4:
533 case 7:
534 case 0xb:
535 case 0xd:
536 case 0xf:
537 case 0x10:
538 case 0x12:
539 case 0x14:
540 case 0x17:
541 case 0x18:
542 case 0x1f:
543 case 0x8000001d:
544 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
545 break;
546 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200547
548 return entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549}
550
Olivier Deprez157378f2022-04-04 15:47:50 +0200551static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000552{
Olivier Deprez157378f2022-04-04 15:47:50 +0200553 struct kvm_cpuid_entry2 *entry;
554
555 if (array->nent >= array->maxnent)
556 return -E2BIG;
557
558 entry = &array->entries[array->nent];
David Brazdil0f672f62019-12-10 10:32:29 +0000559 entry->function = func;
560 entry->index = 0;
561 entry->flags = 0;
562
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563 switch (func) {
564 case 0:
565 entry->eax = 7;
Olivier Deprez157378f2022-04-04 15:47:50 +0200566 ++array->nent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000567 break;
568 case 1:
569 entry->ecx = F(MOVBE);
Olivier Deprez157378f2022-04-04 15:47:50 +0200570 ++array->nent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000571 break;
572 case 7:
573 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
David Brazdil0f672f62019-12-10 10:32:29 +0000574 entry->eax = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200575 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
576 entry->ecx = F(RDPID);
577 ++array->nent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000578 default:
579 break;
580 }
581
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000582 return 0;
583}
584
Olivier Deprez157378f2022-04-04 15:47:50 +0200585static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
David Brazdil0f672f62019-12-10 10:32:29 +0000586{
Olivier Deprez157378f2022-04-04 15:47:50 +0200587 struct kvm_cpuid_entry2 *entry;
588 int r, i, max_idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590 /* all calls to cpuid_count() should be made on the same cpu */
591 get_cpu();
592
593 r = -E2BIG;
594
Olivier Deprez157378f2022-04-04 15:47:50 +0200595 entry = do_host_cpuid(array, function, 0);
596 if (!entry)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000597 goto out;
598
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000599 switch (function) {
600 case 0:
David Brazdil0f672f62019-12-10 10:32:29 +0000601 /* Limited to the highest leaf implemented in KVM. */
602 entry->eax = min(entry->eax, 0x1fU);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000603 break;
604 case 1:
Olivier Deprez157378f2022-04-04 15:47:50 +0200605 cpuid_entry_override(entry, CPUID_1_EDX);
606 cpuid_entry_override(entry, CPUID_1_ECX);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000607 break;
Olivier Deprez157378f2022-04-04 15:47:50 +0200608 case 2:
609 /*
610 * On ancient CPUs, function 2 entries are STATEFUL. That is,
611 * CPUID(function=2, index=0) may return different results each
612 * time, with the least-significant byte in EAX enumerating the
613 * number of times software should do CPUID(2, 0).
614 *
615 * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
616 * idiotic. Intel's SDM states that EAX & 0xff "will always
617 * return 01H. Software should ignore this value and not
618 * interpret it as an informational descriptor", while AMD's
619 * APM states that CPUID(2) is reserved.
620 *
621 * WARN if a frankenstein CPU that supports virtualization and
622 * a stateful CPUID.0x2 is encountered.
623 */
624 WARN_ON_ONCE((entry->eax & 0xff) > 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000625 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000626 /* functions 4 and 0x8000001d have additional index. */
627 case 4:
Olivier Deprez157378f2022-04-04 15:47:50 +0200628 case 0x8000001d:
629 /*
630 * Read entries until the cache type in the previous entry is
631 * zero, i.e. indicates an invalid entry.
632 */
633 for (i = 1; entry->eax & 0x1f; ++i) {
634 entry = do_host_cpuid(array, function, i);
635 if (!entry)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000636 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000637 }
638 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000639 case 6: /* Thermal management */
640 entry->eax = 0x4; /* allow ARAT */
641 entry->ebx = 0;
642 entry->ecx = 0;
643 entry->edx = 0;
644 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000645 /* function 7 has additional index. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200646 case 7:
647 entry->eax = min(entry->eax, 1u);
648 cpuid_entry_override(entry, CPUID_7_0_EBX);
649 cpuid_entry_override(entry, CPUID_7_ECX);
650 cpuid_entry_override(entry, CPUID_7_EDX);
David Brazdil0f672f62019-12-10 10:32:29 +0000651
Olivier Deprez157378f2022-04-04 15:47:50 +0200652 /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
653 if (entry->eax == 1) {
654 entry = do_host_cpuid(array, function, 1);
655 if (!entry)
David Brazdil0f672f62019-12-10 10:32:29 +0000656 goto out;
657
Olivier Deprez157378f2022-04-04 15:47:50 +0200658 cpuid_entry_override(entry, CPUID_7_1_EAX);
659 entry->ebx = 0;
660 entry->ecx = 0;
661 entry->edx = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000662 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000663 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000664 case 9:
665 break;
666 case 0xa: { /* Architectural Performance Monitoring */
667 struct x86_pmu_capability cap;
668 union cpuid10_eax eax;
669 union cpuid10_edx edx;
670
671 perf_get_x86_pmu_capability(&cap);
672
673 /*
674 * Only support guest architectural pmu on a host
675 * with architectural pmu.
676 */
677 if (!cap.version)
678 memset(&cap, 0, sizeof(cap));
679
680 eax.split.version_id = min(cap.version, 2);
681 eax.split.num_counters = cap.num_counters_gp;
682 eax.split.bit_width = cap.bit_width_gp;
683 eax.split.mask_length = cap.events_mask_len;
684
Olivier Deprez157378f2022-04-04 15:47:50 +0200685 edx.split.num_counters_fixed = min(cap.num_counters_fixed, MAX_FIXED_COUNTERS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000686 edx.split.bit_width_fixed = cap.bit_width_fixed;
Olivier Deprez157378f2022-04-04 15:47:50 +0200687 if (cap.version)
688 edx.split.anythread_deprecated = 1;
689 edx.split.reserved1 = 0;
690 edx.split.reserved2 = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000691
692 entry->eax = eax.full;
693 entry->ebx = cap.events_mask;
694 entry->ecx = 0;
695 entry->edx = edx.full;
696 break;
697 }
David Brazdil0f672f62019-12-10 10:32:29 +0000698 /*
699 * Per Intel's SDM, the 0x1f is a superset of 0xb,
700 * thus they can be handled by common code.
701 */
702 case 0x1f:
Olivier Deprez157378f2022-04-04 15:47:50 +0200703 case 0xb:
David Brazdil0f672f62019-12-10 10:32:29 +0000704 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200705 * Populate entries until the level type (ECX[15:8]) of the
706 * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
707 * the starting entry, filled by the primary do_host_cpuid().
David Brazdil0f672f62019-12-10 10:32:29 +0000708 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200709 for (i = 1; entry->ecx & 0xff00; ++i) {
710 entry = do_host_cpuid(array, function, i);
711 if (!entry)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000712 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000713 }
714 break;
Olivier Deprez157378f2022-04-04 15:47:50 +0200715 case 0xd:
716 entry->eax &= supported_xcr0;
717 entry->ebx = xstate_required_size(supported_xcr0, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000718 entry->ecx = entry->ebx;
Olivier Deprez157378f2022-04-04 15:47:50 +0200719 entry->edx &= supported_xcr0 >> 32;
720 if (!supported_xcr0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721 break;
722
Olivier Deprez157378f2022-04-04 15:47:50 +0200723 entry = do_host_cpuid(array, function, 1);
724 if (!entry)
725 goto out;
726
727 cpuid_entry_override(entry, CPUID_D_1_EAX);
728 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
729 entry->ebx = xstate_required_size(supported_xcr0 | supported_xss,
730 true);
731 else {
732 WARN_ON_ONCE(supported_xss != 0);
733 entry->ebx = 0;
734 }
735 entry->ecx &= supported_xss;
736 entry->edx &= supported_xss >> 32;
737
738 for (i = 2; i < 64; ++i) {
739 bool s_state;
740 if (supported_xcr0 & BIT_ULL(i))
741 s_state = false;
742 else if (supported_xss & BIT_ULL(i))
743 s_state = true;
744 else
745 continue;
746
747 entry = do_host_cpuid(array, function, i);
748 if (!entry)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000749 goto out;
750
Olivier Deprez157378f2022-04-04 15:47:50 +0200751 /*
752 * The supported check above should have filtered out
753 * invalid sub-leafs. Only valid sub-leafs should
754 * reach this point, and they should have a non-zero
755 * save state size. Furthermore, check whether the
756 * processor agrees with supported_xcr0/supported_xss
757 * on whether this is an XCR0- or IA32_XSS-managed area.
758 */
759 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
760 --array->nent;
761 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000762 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200763 entry->edx = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000764 }
765 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000766 /* Intel PT */
Olivier Deprez157378f2022-04-04 15:47:50 +0200767 case 0x14:
768 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
769 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000770 break;
Olivier Deprez157378f2022-04-04 15:47:50 +0200771 }
David Brazdil0f672f62019-12-10 10:32:29 +0000772
Olivier Deprez157378f2022-04-04 15:47:50 +0200773 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
774 if (!do_host_cpuid(array, function, i))
David Brazdil0f672f62019-12-10 10:32:29 +0000775 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +0000776 }
777 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000778 case KVM_CPUID_SIGNATURE: {
779 static const char signature[12] = "KVMKVMKVM\0\0";
780 const u32 *sigptr = (const u32 *)signature;
781 entry->eax = KVM_CPUID_FEATURES;
782 entry->ebx = sigptr[0];
783 entry->ecx = sigptr[1];
784 entry->edx = sigptr[2];
785 break;
786 }
787 case KVM_CPUID_FEATURES:
788 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
789 (1 << KVM_FEATURE_NOP_IO_DELAY) |
790 (1 << KVM_FEATURE_CLOCKSOURCE2) |
791 (1 << KVM_FEATURE_ASYNC_PF) |
792 (1 << KVM_FEATURE_PV_EOI) |
793 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
794 (1 << KVM_FEATURE_PV_UNHALT) |
795 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
796 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
David Brazdil0f672f62019-12-10 10:32:29 +0000797 (1 << KVM_FEATURE_PV_SEND_IPI) |
798 (1 << KVM_FEATURE_POLL_CONTROL) |
Olivier Deprez157378f2022-04-04 15:47:50 +0200799 (1 << KVM_FEATURE_PV_SCHED_YIELD) |
800 (1 << KVM_FEATURE_ASYNC_PF_INT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000801
802 if (sched_info_on())
803 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
804
805 entry->ebx = 0;
806 entry->ecx = 0;
807 entry->edx = 0;
808 break;
809 case 0x80000000:
810 entry->eax = min(entry->eax, 0x8000001f);
811 break;
812 case 0x80000001:
Olivier Deprez157378f2022-04-04 15:47:50 +0200813 cpuid_entry_override(entry, CPUID_8000_0001_EDX);
814 cpuid_entry_override(entry, CPUID_8000_0001_ECX);
815 break;
816 case 0x80000006:
817 /* L2 cache and TLB: pass through host info. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000818 break;
819 case 0x80000007: /* Advanced power management */
820 /* invariant TSC is CPUID.80000007H:EDX[8] */
821 entry->edx &= (1 << 8);
822 /* mask against host */
823 entry->edx &= boot_cpu_data.x86_power;
824 entry->eax = entry->ebx = entry->ecx = 0;
825 break;
826 case 0x80000008: {
827 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
828 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
829 unsigned phys_as = entry->eax & 0xff;
830
Olivier Deprez0e641232021-09-23 10:07:05 +0200831 /*
832 * Use bare metal's MAXPHADDR if the CPU doesn't report guest
833 * MAXPHYADDR separately, or if TDP (NPT) is disabled, as the
834 * guest version "applies only to guests using nested paging".
835 */
836 if (!g_phys_as || !tdp_enabled)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000837 g_phys_as = phys_as;
Olivier Deprez0e641232021-09-23 10:07:05 +0200838
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000839 entry->eax = g_phys_as | (virt_as << 8);
840 entry->edx = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200841 cpuid_entry_override(entry, CPUID_8000_0008_EBX);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000842 break;
843 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200844 case 0x8000000A:
845 if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
846 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
847 break;
848 }
849 entry->eax = 1; /* SVM revision 1 */
850 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
851 ASID emulation to nested SVM */
852 entry->ecx = 0; /* Reserved */
853 cpuid_entry_override(entry, CPUID_8000_000A_EDX);
854 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000855 case 0x80000019:
856 entry->ecx = entry->edx = 0;
857 break;
858 case 0x8000001a:
David Brazdil0f672f62019-12-10 10:32:29 +0000859 case 0x8000001e:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000860 break;
Olivier Deprez157378f2022-04-04 15:47:50 +0200861 /* Support memory encryption cpuid if host supports it */
862 case 0x8000001F:
863 if (!boot_cpu_has(X86_FEATURE_SEV))
864 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
865 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000866 /*Add support for Centaur's CPUID instruction*/
867 case 0xC0000000:
868 /*Just support up to 0xC0000004 now*/
869 entry->eax = min(entry->eax, 0xC0000004);
870 break;
871 case 0xC0000001:
Olivier Deprez157378f2022-04-04 15:47:50 +0200872 cpuid_entry_override(entry, CPUID_C000_0001_EDX);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000873 break;
874 case 3: /* Processor serial number */
875 case 5: /* MONITOR/MWAIT */
876 case 0xC0000002:
877 case 0xC0000003:
878 case 0xC0000004:
879 default:
880 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
881 break;
882 }
883
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000884 r = 0;
885
886out:
887 put_cpu();
888
889 return r;
890}
891
Olivier Deprez157378f2022-04-04 15:47:50 +0200892static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
893 unsigned int type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000894{
895 if (type == KVM_GET_EMULATED_CPUID)
Olivier Deprez157378f2022-04-04 15:47:50 +0200896 return __do_cpuid_func_emulated(array, func);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000897
Olivier Deprez157378f2022-04-04 15:47:50 +0200898 return __do_cpuid_func(array, func);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899}
900
Olivier Deprez157378f2022-04-04 15:47:50 +0200901#define CENTAUR_CPUID_SIGNATURE 0xC0000000
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000902
Olivier Deprez157378f2022-04-04 15:47:50 +0200903static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
904 unsigned int type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000905{
Olivier Deprez157378f2022-04-04 15:47:50 +0200906 u32 limit;
907 int r;
908
909 if (func == CENTAUR_CPUID_SIGNATURE &&
910 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
911 return 0;
912
913 r = do_cpuid_func(array, func, type);
914 if (r)
915 return r;
916
917 limit = array->entries[array->nent - 1].eax;
918 for (func = func + 1; func <= limit; ++func) {
919 r = do_cpuid_func(array, func, type);
920 if (r)
921 break;
922 }
923
924 return r;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000925}
926
927static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
928 __u32 num_entries, unsigned int ioctl_type)
929{
930 int i;
931 __u32 pad[3];
932
933 if (ioctl_type != KVM_GET_EMULATED_CPUID)
934 return false;
935
936 /*
937 * We want to make sure that ->padding is being passed clean from
938 * userspace in case we want to use it for something in the future.
939 *
940 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
941 * have to give ourselves satisfied only with the emulated side. /me
942 * sheds a tear.
943 */
944 for (i = 0; i < num_entries; i++) {
945 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
946 return true;
947
948 if (pad[0] || pad[1] || pad[2])
949 return true;
950 }
951 return false;
952}
953
954int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
955 struct kvm_cpuid_entry2 __user *entries,
956 unsigned int type)
957{
Olivier Deprez157378f2022-04-04 15:47:50 +0200958 static const u32 funcs[] = {
959 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960 };
961
Olivier Deprez157378f2022-04-04 15:47:50 +0200962 struct kvm_cpuid_array array = {
963 .nent = 0,
964 };
965 int r, i;
966
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000967 if (cpuid->nent < 1)
Olivier Deprez157378f2022-04-04 15:47:50 +0200968 return -E2BIG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000969 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
970 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
971
972 if (sanity_check_entries(entries, cpuid->nent, type))
973 return -EINVAL;
974
Olivier Deprez157378f2022-04-04 15:47:50 +0200975 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000976 cpuid->nent));
Olivier Deprez157378f2022-04-04 15:47:50 +0200977 if (!array.entries)
978 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000979
Olivier Deprez157378f2022-04-04 15:47:50 +0200980 array.maxnent = cpuid->nent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000981
Olivier Deprez157378f2022-04-04 15:47:50 +0200982 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
983 r = get_cpuid_func(&array, funcs[i], type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984 if (r)
985 goto out_free;
986 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200987 cpuid->nent = array.nent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000988
Olivier Deprez157378f2022-04-04 15:47:50 +0200989 if (copy_to_user(entries, array.entries,
990 array.nent * sizeof(struct kvm_cpuid_entry2)))
991 r = -EFAULT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000992
993out_free:
Olivier Deprez157378f2022-04-04 15:47:50 +0200994 vfree(array.entries);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000995 return r;
996}
997
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000998struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
999 u32 function, u32 index)
1000{
Olivier Deprez157378f2022-04-04 15:47:50 +02001001 return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
1002 function, index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001003}
1004EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
1005
1006/*
Olivier Deprez157378f2022-04-04 15:47:50 +02001007 * Intel CPUID semantics treats any query for an out-of-range leaf as if the
1008 * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
1009 * returns all zeroes for any undefined leaf, whether or not the leaf is in
1010 * range. Centaur/VIA follows Intel semantics.
1011 *
1012 * A leaf is considered out-of-range if its function is higher than the maximum
1013 * supported leaf of its associated class or if its associated class does not
1014 * exist.
1015 *
1016 * There are three primary classes to be considered, with their respective
1017 * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
1018 * class exists if a guest CPUID entry for its <base> leaf exists. For a given
1019 * class, CPUID.<base>.EAX contains the max supported leaf for the class.
1020 *
1021 * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
1022 * - Hypervisor: 0x40000000 - 0x4fffffff
1023 * - Extended: 0x80000000 - 0xbfffffff
1024 * - Centaur: 0xc0000000 - 0xcfffffff
1025 *
1026 * The Hypervisor class is further subdivided into sub-classes that each act as
1027 * their own indepdent class associated with a 0x100 byte range. E.g. if Qemu
1028 * is advertising support for both HyperV and KVM, the resulting Hypervisor
1029 * CPUID sub-classes are:
1030 *
1031 * - HyperV: 0x40000000 - 0x400000ff
1032 * - KVM: 0x40000100 - 0x400001ff
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001033 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001034static struct kvm_cpuid_entry2 *
1035get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001036{
Olivier Deprez157378f2022-04-04 15:47:50 +02001037 struct kvm_cpuid_entry2 *basic, *class;
1038 u32 function = *fn_ptr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001039
Olivier Deprez157378f2022-04-04 15:47:50 +02001040 basic = kvm_find_cpuid_entry(vcpu, 0, 0);
1041 if (!basic)
1042 return NULL;
1043
1044 if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
1045 is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
1046 return NULL;
1047
1048 if (function >= 0x40000000 && function <= 0x4fffffff)
1049 class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
1050 else if (function >= 0xc0000000)
1051 class = kvm_find_cpuid_entry(vcpu, 0xc0000000, 0);
1052 else
1053 class = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
1054
1055 if (class && function <= class->eax)
1056 return NULL;
1057
1058 /*
1059 * Leaf specific adjustments are also applied when redirecting to the
1060 * max basic entry, e.g. if the max basic leaf is 0xb but there is no
1061 * entry for CPUID.0xb.index (see below), then the output value for EDX
1062 * needs to be pulled from CPUID.0xb.1.
1063 */
1064 *fn_ptr = basic->eax;
1065
1066 /*
1067 * The class does not exist or the requested function is out of range;
1068 * the effective CPUID entry is the max basic leaf. Note, the index of
1069 * the original requested leaf is observed!
1070 */
1071 return kvm_find_cpuid_entry(vcpu, basic->eax, index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001072}
1073
1074bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
Olivier Deprez157378f2022-04-04 15:47:50 +02001075 u32 *ecx, u32 *edx, bool exact_only)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001076{
Olivier Deprez157378f2022-04-04 15:47:50 +02001077 u32 orig_function = *eax, function = *eax, index = *ecx;
David Brazdil0f672f62019-12-10 10:32:29 +00001078 struct kvm_cpuid_entry2 *entry;
Olivier Deprez157378f2022-04-04 15:47:50 +02001079 bool exact, used_max_basic = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001080
David Brazdil0f672f62019-12-10 10:32:29 +00001081 entry = kvm_find_cpuid_entry(vcpu, function, index);
Olivier Deprez157378f2022-04-04 15:47:50 +02001082 exact = !!entry;
1083
1084 if (!entry && !exact_only) {
1085 entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
1086 used_max_basic = !!entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001087 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001088
David Brazdil0f672f62019-12-10 10:32:29 +00001089 if (entry) {
1090 *eax = entry->eax;
1091 *ebx = entry->ebx;
1092 *ecx = entry->ecx;
1093 *edx = entry->edx;
Olivier Deprez157378f2022-04-04 15:47:50 +02001094 if (function == 7 && index == 0) {
1095 u64 data;
1096 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1097 (data & TSX_CTRL_CPUID_CLEAR))
1098 *ebx &= ~(F(RTM) | F(HLE));
1099 }
David Brazdil0f672f62019-12-10 10:32:29 +00001100 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001101 *eax = *ebx = *ecx = *edx = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001102 /*
1103 * When leaf 0BH or 1FH is defined, CL is pass-through
1104 * and EDX is always the x2APIC ID, even for undefined
1105 * subleaves. Index 1 will exist iff the leaf is
1106 * implemented, so we pass through CL iff leaf 1
1107 * exists. EDX can be copied from any existing index.
1108 */
1109 if (function == 0xb || function == 0x1f) {
1110 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1111 if (entry) {
1112 *ecx = index & 0xff;
1113 *edx = entry->edx;
1114 }
1115 }
1116 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001117 trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
1118 used_max_basic);
1119 return exact;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001120}
1121EXPORT_SYMBOL_GPL(kvm_cpuid);
1122
1123int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1124{
1125 u32 eax, ebx, ecx, edx;
1126
1127 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1128 return 1;
1129
David Brazdil0f672f62019-12-10 10:32:29 +00001130 eax = kvm_rax_read(vcpu);
1131 ecx = kvm_rcx_read(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02001132 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
David Brazdil0f672f62019-12-10 10:32:29 +00001133 kvm_rax_write(vcpu, eax);
1134 kvm_rbx_write(vcpu, ebx);
1135 kvm_rcx_write(vcpu, ecx);
1136 kvm_rdx_write(vcpu, edx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001137 return kvm_skip_emulated_instruction(vcpu);
1138}
1139EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);