blob: f42e9491a6c8cb76d91636d5973c40deddf88a78 [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>
Sean Christopherson72add912021-04-12 16:21:42 +120021#include <asm/sgx.h>
Avi Kivity00b27a32011-11-23 16:30:32 +020022#include "cpuid.h"
23#include "lapic.h"
24#include "mmu.h"
25#include "trace.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020026#include "pmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020027
Sean Christopherson66a69502020-03-02 15:56:41 -080028/*
29 * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
30 * aligned to sizeof(unsigned long) because it's not accessed via bitops.
31 */
Sean Christopherson4e66c0c2021-04-12 16:21:35 +120032u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
Sean Christopherson66a69502020-03-02 15:56:41 -080033EXPORT_SYMBOL_GPL(kvm_cpu_caps);
34
Paolo Bonzini412a3c42014-12-03 14:38:01 +010035static u32 xstate_required_size(u64 xstate_bv, bool compacted)
Paolo Bonzini4344ee92013-10-02 16:06:16 +020036{
37 int feature_bit = 0;
38 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
39
Dave Hansend91cab72015-09-02 16:31:26 -070040 xstate_bv &= XFEATURE_MASK_EXTEND;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020041 while (xstate_bv) {
42 if (xstate_bv & 0x1) {
Paolo Bonzini412a3c42014-12-03 14:38:01 +010043 u32 eax, ebx, ecx, edx, offset;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020044 cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
Paolo Bonzini412a3c42014-12-03 14:38:01 +010045 offset = compacted ? ret : ebx;
46 ret = max(ret, offset + eax);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020047 }
48
49 xstate_bv >>= 1;
50 feature_bit++;
51 }
52
53 return ret;
54}
55
Sean Christopherson87382002019-12-17 13:32:42 -080056#define F feature_bit
Sean Christopherson4e66c0c2021-04-12 16:21:35 +120057#define SF(name) (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0)
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010058
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +020059static inline struct kvm_cpuid_entry2 *cpuid_entry2_find(
60 struct kvm_cpuid_entry2 *entries, int nent, u32 function, u32 index)
61{
62 struct kvm_cpuid_entry2 *e;
63 int i;
64
65 for (i = 0; i < nent; i++) {
66 e = &entries[i];
67
68 if (e->function == function && (e->index == index ||
69 !(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX)))
70 return e;
71 }
72
73 return NULL;
74}
75
76static int kvm_check_cpuid(struct kvm_cpuid_entry2 *entries, int nent)
Xiaoyao Lia76733a2020-07-09 12:34:22 +080077{
78 struct kvm_cpuid_entry2 *best;
79
80 /*
81 * The existing code assumes virtual address is 48-bit or 57-bit in the
82 * canonical address checks; exit if it is ever changed.
83 */
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +020084 best = cpuid_entry2_find(entries, nent, 0x80000008, 0);
Xiaoyao Lia76733a2020-07-09 12:34:22 +080085 if (best) {
86 int vaddr_bits = (best->eax & 0xff00) >> 8;
87
88 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
89 return -EINVAL;
90 }
91
92 return 0;
93}
94
Oliver Upton01b4f512020-10-27 16:10:42 -070095void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
96{
97 struct kvm_cpuid_entry2 *best;
98
99 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
100
101 /*
102 * save the feature bitmap to avoid cpuid lookup for every PV
103 * operation
104 */
105 if (best)
106 vcpu->arch.pv_cpuid.features = best->eax;
107}
108
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800109void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
Avi Kivity00b27a32011-11-23 16:30:32 +0200110{
111 struct kvm_cpuid_entry2 *best;
Avi Kivity00b27a32011-11-23 16:30:32 +0200112
113 best = kvm_find_cpuid_entry(vcpu, 1, 0);
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800114 if (best) {
115 /* Update OSXSAVE bit */
116 if (boot_cpu_has(X86_FEATURE_XSAVE))
117 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
Sean Christophersonb32666b2020-03-02 15:56:31 -0800118 kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
Avi Kivity00b27a32011-11-23 16:30:32 +0200119
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800120 cpuid_entry_change(best, X86_FEATURE_APIC,
Sean Christophersonb32666b2020-03-02 15:56:31 -0800121 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800122 }
Jim Mattsonc7dd15b2016-11-09 09:50:11 -0800123
Huaitong Hanb9baba82016-03-22 16:51:21 +0800124 best = kvm_find_cpuid_entry(vcpu, 7, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800125 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
126 cpuid_entry_change(best, X86_FEATURE_OSPKE,
127 kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
Huaitong Hanb9baba82016-03-22 16:51:21 +0800128
Paolo Bonzinid7876f12013-10-02 16:06:15 +0200129 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800130 if (best)
Xiaoyao Lia71936ab2020-04-29 23:43:12 +0800131 best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
Paolo Bonzinid7876f12013-10-02 16:06:15 +0200132
Paolo Bonzini412a3c42014-12-03 14:38:01 +0100133 best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
Sean Christopherson4c615342020-03-02 15:56:30 -0800134 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
135 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
Paolo Bonzini412a3c42014-12-03 14:38:01 +0100136 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
137
Wanpeng Licaa057a2018-03-12 04:53:03 -0700138 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
139 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
140 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
141 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
142
Wanpeng Li511a85562019-05-21 14:06:54 +0800143 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
144 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800145 if (best)
146 cpuid_entry_change(best, X86_FEATURE_MWAIT,
147 vcpu->arch.ia32_misc_enable_msr &
148 MSR_IA32_MISC_ENABLE_MWAIT);
Wanpeng Li511a85562019-05-21 14:06:54 +0800149 }
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800150}
Jim Mattson2259c172020-10-29 10:06:48 -0700151EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800152
Xiaoyao Li346ce352020-07-09 12:34:24 +0800153static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800154{
155 struct kvm_lapic *apic = vcpu->arch.apic;
156 struct kvm_cpuid_entry2 *best;
157
158 best = kvm_find_cpuid_entry(vcpu, 1, 0);
159 if (best && apic) {
160 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
161 apic->lapic_timer.timer_mode_mask = 3 << 17;
162 else
163 apic->lapic_timer.timer_mode_mask = 1 << 17;
164
165 kvm_apic_set_version(vcpu);
166 }
167
168 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
169 if (!best)
170 vcpu->arch.guest_supported_xcr0 = 0;
171 else
172 vcpu->arch.guest_supported_xcr0 =
173 (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
Wanpeng Li511a85562019-05-21 14:06:54 +0800174
Sean Christopherson72add912021-04-12 16:21:42 +1200175 /*
176 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1) enumerate
177 * the supported XSAVE Feature Request Mask (XFRM), i.e. the enclave's
178 * requested XCR0 value. The enclave's XFRM must be a subset of XCRO
179 * at the time of EENTER, thus adjust the allowed XFRM by the guest's
180 * supported XCR0. Similar to XCR0 handling, FP and SSE are forced to
181 * '1' even on CPUs that don't support XSAVE.
182 */
183 best = kvm_find_cpuid_entry(vcpu, 0x12, 0x1);
184 if (best) {
185 best->ecx &= vcpu->arch.guest_supported_xcr0 & 0xffffffff;
186 best->edx &= vcpu->arch.guest_supported_xcr0 >> 32;
187 best->ecx |= XFEATURE_MASK_FPSSE;
188 }
189
Oliver Upton01b4f512020-10-27 16:10:42 -0700190 kvm_update_pv_runtime(vcpu);
191
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300192 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Sean Christophersona8ac8642021-02-03 16:01:15 -0800193 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300194
Wei Huangc6702c92015-06-19 13:44:45 +0200195 kvm_pmu_refresh(vcpu);
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000196 vcpu->arch.cr4_guest_rsvd_bits =
197 __cr4_reserved_bits(guest_cpuid_has, vcpu);
Sean Christophersonc44d9b32020-09-29 21:16:56 -0700198
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +0100199 kvm_hv_set_cpuid(vcpu);
200
Sean Christophersonc44d9b32020-09-29 21:16:56 -0700201 /* Invoke the vendor callback only after the above state is updated. */
Jason Baronb36464772021-01-14 22:27:56 -0500202 static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu);
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800203
204 /*
205 * Except for the MMU, which needs to be reset after any vendor
206 * specific adjustments to the reserved GPA bits.
207 */
208 kvm_mmu_reset_context(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200209}
210
211static int is_efer_nx(void)
212{
Sean Christopherson91661982020-03-02 15:57:06 -0800213 return host_efer & EFER_NX;
Avi Kivity00b27a32011-11-23 16:30:32 +0200214}
215
216static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
217{
218 int i;
219 struct kvm_cpuid_entry2 *e, *entry;
220
221 entry = NULL;
222 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
223 e = &vcpu->arch.cpuid_entries[i];
224 if (e->function == 0x80000001) {
225 entry = e;
226 break;
227 }
228 }
Sean Christopherson4c615342020-03-02 15:56:30 -0800229 if (entry && cpuid_entry_has(entry, X86_FEATURE_NX) && !is_efer_nx()) {
Sean Christophersonb32666b2020-03-02 15:56:31 -0800230 cpuid_entry_clear(entry, X86_FEATURE_NX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200231 printk(KERN_INFO "kvm: guest NX capability removed\n");
232 }
233}
234
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300235int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
236{
237 struct kvm_cpuid_entry2 *best;
238
239 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
240 if (!best || best->eax < 0x80000008)
241 goto not_found;
242 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
243 if (best)
244 return best->eax & 0xff;
245not_found:
246 return 36;
247}
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300248
Sean Christophersona8ac8642021-02-03 16:01:15 -0800249/*
250 * This "raw" version returns the reserved GPA bits without any adjustments for
251 * encryption technologies that usurp bits. The raw mask should be used if and
252 * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs.
253 */
254u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
255{
256 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
257}
258
Avi Kivity00b27a32011-11-23 16:30:32 +0200259/* when an old userspace process fills a new kernel module */
260int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
261 struct kvm_cpuid *cpuid,
262 struct kvm_cpuid_entry __user *entries)
263{
264 int r, i;
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200265 struct kvm_cpuid_entry *e = NULL;
266 struct kvm_cpuid_entry2 *e2 = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200267
Avi Kivity00b27a32011-11-23 16:30:32 +0200268 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200269 return -E2BIG;
270
Paolo Bonzini83676e92016-06-01 14:09:19 +0200271 if (cpuid->nent) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200272 e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent));
273 if (IS_ERR(e))
274 return PTR_ERR(e);
275
276 e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
277 if (!e2) {
278 r = -ENOMEM;
279 goto out_free_cpuid;
Denis Efremov7ec28e22020-06-03 13:11:31 +0300280 }
Paolo Bonzini83676e92016-06-01 14:09:19 +0200281 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200282 for (i = 0; i < cpuid->nent; i++) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200283 e2[i].function = e[i].function;
284 e2[i].eax = e[i].eax;
285 e2[i].ebx = e[i].ebx;
286 e2[i].ecx = e[i].ecx;
287 e2[i].edx = e[i].edx;
288 e2[i].index = 0;
289 e2[i].flags = 0;
290 e2[i].padding[0] = 0;
291 e2[i].padding[1] = 0;
292 e2[i].padding[2] = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200293 }
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200294
295 r = kvm_check_cpuid(e2, cpuid->nent);
Xiaoyao Lia76733a2020-07-09 12:34:22 +0800296 if (r) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200297 kvfree(e2);
298 goto out_free_cpuid;
Xiaoyao Lia76733a2020-07-09 12:34:22 +0800299 }
300
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200301 kvfree(vcpu->arch.cpuid_entries);
302 vcpu->arch.cpuid_entries = e2;
303 vcpu->arch.cpuid_nent = cpuid->nent;
304
Avi Kivity00b27a32011-11-23 16:30:32 +0200305 cpuid_fix_nx_cap(vcpu);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800306 kvm_update_cpuid_runtime(vcpu);
Xiaoyao Li346ce352020-07-09 12:34:24 +0800307 kvm_vcpu_after_set_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200308
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200309out_free_cpuid:
310 kvfree(e);
311
Avi Kivity00b27a32011-11-23 16:30:32 +0200312 return r;
313}
314
315int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
316 struct kvm_cpuid2 *cpuid,
317 struct kvm_cpuid_entry2 __user *entries)
318{
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200319 struct kvm_cpuid_entry2 *e2 = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200320 int r;
321
Avi Kivity00b27a32011-11-23 16:30:32 +0200322 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200323 return -E2BIG;
324
325 if (cpuid->nent) {
326 e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent));
327 if (IS_ERR(e2))
328 return PTR_ERR(e2);
Xiaoyao Lia76733a2020-07-09 12:34:22 +0800329 }
330
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200331 r = kvm_check_cpuid(e2, cpuid->nent);
332 if (r) {
333 kvfree(e2);
334 return r;
335 }
336
337 kvfree(vcpu->arch.cpuid_entries);
338 vcpu->arch.cpuid_entries = e2;
339 vcpu->arch.cpuid_nent = cpuid->nent;
340
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800341 kvm_update_cpuid_runtime(vcpu);
Xiaoyao Li346ce352020-07-09 12:34:24 +0800342 kvm_vcpu_after_set_cpuid(vcpu);
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200343
344 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200345}
346
347int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
348 struct kvm_cpuid2 *cpuid,
349 struct kvm_cpuid_entry2 __user *entries)
350{
351 int r;
352
353 r = -E2BIG;
354 if (cpuid->nent < vcpu->arch.cpuid_nent)
355 goto out;
356 r = -EFAULT;
Michael Roth181f4942021-01-27 20:44:51 -0600357 if (copy_to_user(entries, vcpu->arch.cpuid_entries,
Avi Kivity00b27a32011-11-23 16:30:32 +0200358 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
359 goto out;
360 return 0;
361
362out:
363 cpuid->nent = vcpu->arch.cpuid_nent;
364 return r;
365}
366
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200367/* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */
Sean Christopherson462f8dd2021-04-20 18:08:50 -0700368static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf)
Sean Christopherson66a69502020-03-02 15:56:41 -0800369{
Sean Christophersond8577a42020-03-02 15:56:52 -0800370 const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
371 struct kvm_cpuid_entry2 entry;
372
Sean Christopherson66a69502020-03-02 15:56:41 -0800373 reverse_cpuid_check(leaf);
Sean Christophersond8577a42020-03-02 15:56:52 -0800374
375 cpuid_count(cpuid.function, cpuid.index,
376 &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
377
Sean Christopherson855c7e92020-03-25 12:12:59 -0700378 kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
Sean Christopherson66a69502020-03-02 15:56:41 -0800379}
380
Sean Christopherson462f8dd2021-04-20 18:08:50 -0700381static __always_inline
382void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask)
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200383{
384 /* Use kvm_cpu_cap_mask for non-scattered leafs. */
385 BUILD_BUG_ON(leaf < NCAPINTS);
386
387 kvm_cpu_caps[leaf] = mask;
388
389 __kvm_cpu_cap_mask(leaf);
390}
391
392static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
393{
394 /* Use kvm_cpu_cap_init_scattered for scattered leafs. */
395 BUILD_BUG_ON(leaf >= NCAPINTS);
396
397 kvm_cpu_caps[leaf] &= mask;
398
399 __kvm_cpu_cap_mask(leaf);
400}
401
Sean Christopherson66a69502020-03-02 15:56:41 -0800402void kvm_set_cpu_caps(void)
403{
404 unsigned int f_nx = is_efer_nx() ? F(NX) : 0;
405#ifdef CONFIG_X86_64
406 unsigned int f_gbpages = F(GBPAGES);
407 unsigned int f_lm = F(LM);
408#else
409 unsigned int f_gbpages = 0;
410 unsigned int f_lm = 0;
411#endif
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200412 memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
Sean Christopherson66a69502020-03-02 15:56:41 -0800413
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200414 BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
Sean Christopherson66a69502020-03-02 15:56:41 -0800415 sizeof(boot_cpu_data.x86_capability));
416
417 memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200418 sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)));
Sean Christopherson66a69502020-03-02 15:56:41 -0800419
420 kvm_cpu_cap_mask(CPUID_1_ECX,
421 /*
422 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
423 * advertised to guests via CPUID!
424 */
425 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
426 0 /* DS-CPL, VMX, SMX, EST */ |
427 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
Like Xu27461da32020-05-29 15:43:45 +0800428 F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800429 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
430 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
431 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
432 F(F16C) | F(RDRAND)
433 );
Sean Christopherson93c380e2020-03-02 15:56:54 -0800434 /* KVM emulates x2apic in software irrespective of host support. */
435 kvm_cpu_cap_set(X86_FEATURE_X2APIC);
Sean Christopherson66a69502020-03-02 15:56:41 -0800436
437 kvm_cpu_cap_mask(CPUID_1_EDX,
438 F(FPU) | F(VME) | F(DE) | F(PSE) |
439 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
440 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
441 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
442 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
443 0 /* Reserved, DS, ACPI */ | F(MMX) |
444 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
445 0 /* HTT, TM, Reserved, PBE */
446 );
447
448 kvm_cpu_cap_mask(CPUID_7_0_EBX,
Sean Christopherson72add912021-04-12 16:21:42 +1200449 F(FSGSBASE) | F(SGX) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
Sean Christophersone4203332021-02-11 16:34:10 -0800450 F(BMI2) | F(ERMS) | F(INVPCID) | F(RTM) | 0 /*MPX*/ | F(RDSEED) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800451 F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
452 F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
453 F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | 0 /*INTEL_PT*/
454 );
455
456 kvm_cpu_cap_mask(CPUID_7_ECX,
Babu Mogerfa44b822020-05-12 18:59:16 -0500457 F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800458 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
459 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
Sean Christopherson72add912021-04-12 16:21:42 +1200460 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
461 F(SGX_LC)
Sean Christopherson66a69502020-03-02 15:56:41 -0800462 );
463 /* Set LA57 based on hardware capability. */
464 if (cpuid_ecx(7) & F(LA57))
465 kvm_cpu_cap_set(X86_FEATURE_LA57);
466
Babu Mogerfa44b822020-05-12 18:59:16 -0500467 /*
468 * PKU not yet implemented for shadow paging and requires OSPKE
469 * to be set on the host. Clear it if that is not the case
470 */
471 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
472 kvm_cpu_cap_clear(X86_FEATURE_PKU);
473
Sean Christopherson66a69502020-03-02 15:56:41 -0800474 kvm_cpu_cap_mask(CPUID_7_EDX,
475 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
476 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
Paolo Bonzini43bd9ef2020-08-09 13:04:56 -0400477 F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
Cathy Zhang2224fc92020-12-07 19:34:41 -0800478 F(SERIALIZE) | F(TSXLDTRK) | F(AVX512_FP16)
Sean Christopherson66a69502020-03-02 15:56:41 -0800479 );
480
Sean Christopherson93c380e2020-03-02 15:56:54 -0800481 /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
482 kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
483 kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
484
485 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
486 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
487 if (boot_cpu_has(X86_FEATURE_STIBP))
488 kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
489 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
490 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
491
Sean Christopherson66a69502020-03-02 15:56:41 -0800492 kvm_cpu_cap_mask(CPUID_7_1_EAX,
Yang Zhong1085a6b2021-01-05 08:49:09 +0800493 F(AVX_VNNI) | F(AVX512_BF16)
Sean Christopherson66a69502020-03-02 15:56:41 -0800494 );
495
496 kvm_cpu_cap_mask(CPUID_D_1_EAX,
497 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES)
498 );
499
Sean Christopherson72add912021-04-12 16:21:42 +1200500 kvm_cpu_cap_init_scattered(CPUID_12_EAX,
501 SF(SGX1) | SF(SGX2)
502 );
503
Sean Christopherson66a69502020-03-02 15:56:41 -0800504 kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
505 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
506 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
507 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
508 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
509 F(TOPOEXT) | F(PERFCTR_CORE)
510 );
511
512 kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
513 F(FPU) | F(VME) | F(DE) | F(PSE) |
514 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
515 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
516 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
517 F(PAT) | F(PSE36) | 0 /* Reserved */ |
518 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
519 F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
520 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
521 );
522
523 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
524 kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
525
526 kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
527 F(CLZERO) | F(XSAVEERPTR) |
528 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
529 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON)
530 );
531
Sean Christopherson9b58b982020-03-02 15:56:42 -0800532 /*
Sean Christopherson93c380e2020-03-02 15:56:54 -0800533 * AMD has separate bits for each SPEC_CTRL bit.
534 * arch/x86/kernel/cpu/bugs.c is kind enough to
535 * record that in cpufeatures so use them.
536 */
537 if (boot_cpu_has(X86_FEATURE_IBPB))
538 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
539 if (boot_cpu_has(X86_FEATURE_IBRS))
540 kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
541 if (boot_cpu_has(X86_FEATURE_STIBP))
542 kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
543 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
544 kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
545 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
546 kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
547 /*
548 * The preference is to use SPEC CTRL MSR instead of the
549 * VIRT_SPEC MSR.
550 */
551 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
552 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
553 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
554
555 /*
Sean Christopherson9b58b982020-03-02 15:56:42 -0800556 * Hide all SVM features by default, SVM will set the cap bits for
557 * features it emulates and/or exposes for L1.
558 */
559 kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
560
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700561 kvm_cpu_cap_mask(CPUID_8000_001F_EAX,
562 0 /* SME */ | F(SEV) | 0 /* VM_PAGE_FLUSH */ | F(SEV_ES) |
563 F(SME_COHERENT));
564
Sean Christopherson66a69502020-03-02 15:56:41 -0800565 kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
566 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
567 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
568 F(PMM) | F(PMM_EN)
569 );
Sean Christopherson78bba962021-05-04 10:17:34 -0700570
571 /*
572 * Hide RDTSCP and RDPID if either feature is reported as supported but
573 * probing MSR_TSC_AUX failed. This is purely a sanity check and
574 * should never happen, but the guest will likely crash if RDTSCP or
575 * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in
576 * the past. For example, the sanity check may fire if this instance of
577 * KVM is running as L1 on top of an older, broken KVM.
578 */
579 if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) ||
580 kvm_cpu_cap_has(X86_FEATURE_RDPID)) &&
581 !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) {
582 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
583 kvm_cpu_cap_clear(X86_FEATURE_RDPID);
584 }
Sean Christopherson66a69502020-03-02 15:56:41 -0800585}
586EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
587
Sean Christophersone53c95e2020-03-02 15:56:19 -0800588struct kvm_cpuid_array {
589 struct kvm_cpuid_entry2 *entries;
Xiaoyao Li65b18912020-06-04 12:16:36 +0800590 int maxnent;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800591 int nent;
592};
593
594static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800595 u32 function, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +0200596{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800597 struct kvm_cpuid_entry2 *entry;
598
599 if (array->nent >= array->maxnent)
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800600 return NULL;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800601
602 entry = &array->entries[array->nent++];
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800603
Avi Kivity00b27a32011-11-23 16:30:32 +0200604 entry->function = function;
605 entry->index = index;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200606 entry->flags = 0;
607
Avi Kivity00b27a32011-11-23 16:30:32 +0200608 cpuid_count(entry->function, entry->index,
609 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200610
611 switch (function) {
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200612 case 4:
613 case 7:
614 case 0xb:
615 case 0xd:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700616 case 0xf:
617 case 0x10:
618 case 0x12:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200619 case 0x14:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700620 case 0x17:
621 case 0x18:
622 case 0x1f:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200623 case 0x8000001d:
624 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
625 break;
626 }
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800627
628 return entry;
Avi Kivity00b27a32011-11-23 16:30:32 +0200629}
630
Sean Christophersone53c95e2020-03-02 15:56:19 -0800631static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200632{
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800633 struct kvm_cpuid_entry2 *entry;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800634
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800635 if (array->nent >= array->maxnent)
636 return -E2BIG;
637
638 entry = &array->entries[array->nent];
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200639 entry->function = func;
640 entry->index = 0;
641 entry->flags = 0;
642
Borislav Petkov84cffe42013-10-29 12:54:56 +0100643 switch (func) {
644 case 0:
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200645 entry->eax = 7;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800646 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100647 break;
648 case 1:
649 entry->ecx = F(MOVBE);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800650 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100651 break;
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200652 case 7:
653 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200654 entry->eax = 0;
Sean Christopherson85d00112021-05-04 10:17:21 -0700655 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
656 entry->ecx = F(RDPID);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800657 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100658 default:
659 break;
660 }
661
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200662 return 0;
663}
664
Sean Christophersone53c95e2020-03-02 15:56:19 -0800665static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200666{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800667 struct kvm_cpuid_entry2 *entry;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800668 int r, i, max_idx;
Avi Kivity00b27a32011-11-23 16:30:32 +0200669
Avi Kivity00b27a32011-11-23 16:30:32 +0200670 /* all calls to cpuid_count() should be made on the same cpu */
671 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200672
673 r = -E2BIG;
674
Sean Christophersone53c95e2020-03-02 15:56:19 -0800675 entry = do_host_cpuid(array, function, 0);
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800676 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200677 goto out;
678
Avi Kivity00b27a32011-11-23 16:30:32 +0200679 switch (function) {
680 case 0:
Like Xua87f2d32019-06-06 09:18:45 +0800681 /* Limited to the highest leaf implemented in KVM. */
682 entry->eax = min(entry->eax, 0x1fU);
Avi Kivity00b27a32011-11-23 16:30:32 +0200683 break;
684 case 1:
Sean Christophersonbd791992020-03-02 15:56:53 -0800685 cpuid_entry_override(entry, CPUID_1_EDX);
686 cpuid_entry_override(entry, CPUID_1_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200687 break;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800688 case 2:
Sean Christophersonc571a142020-03-02 15:56:50 -0800689 /*
690 * On ancient CPUs, function 2 entries are STATEFUL. That is,
691 * CPUID(function=2, index=0) may return different results each
692 * time, with the least-significant byte in EAX enumerating the
693 * number of times software should do CPUID(2, 0).
694 *
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800695 * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
696 * idiotic. Intel's SDM states that EAX & 0xff "will always
697 * return 01H. Software should ignore this value and not
Sean Christophersonc571a142020-03-02 15:56:50 -0800698 * interpret it as an informational descriptor", while AMD's
699 * APM states that CPUID(2) is reserved.
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800700 *
701 * WARN if a frankenstein CPU that supports virtualization and
702 * a stateful CPUID.0x2 is encountered.
Sean Christophersonc571a142020-03-02 15:56:50 -0800703 */
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800704 WARN_ON_ONCE((entry->eax & 0xff) > 1);
Avi Kivity00b27a32011-11-23 16:30:32 +0200705 break;
Jim Mattson32a243d2019-03-27 13:15:36 -0700706 /* functions 4 and 0x8000001d have additional index. */
707 case 4:
Sean Christophersonc8629032020-03-02 15:56:18 -0800708 case 0x8000001d:
709 /*
710 * Read entries until the cache type in the previous entry is
711 * zero, i.e. indicates an invalid entry.
712 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800713 for (i = 1; entry->eax & 0x1f; ++i) {
714 entry = do_host_cpuid(array, function, i);
715 if (!entry)
Sean Christopherson0fc62672020-03-02 15:56:08 -0800716 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200717 }
718 break;
Jan Kiszkae453aa02015-05-24 17:22:38 +0200719 case 6: /* Thermal management */
720 entry->eax = 0x4; /* allow ARAT */
721 entry->ebx = 0;
722 entry->ecx = 0;
723 entry->edx = 0;
724 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200725 /* function 7 has additional index. */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800726 case 7:
Sean Christopherson09f628a2020-03-02 15:56:48 -0800727 entry->eax = min(entry->eax, 1u);
Sean Christophersonbd791992020-03-02 15:56:53 -0800728 cpuid_entry_override(entry, CPUID_7_0_EBX);
729 cpuid_entry_override(entry, CPUID_7_ECX);
730 cpuid_entry_override(entry, CPUID_7_EDX);
Sean Christopherson09f628a2020-03-02 15:56:48 -0800731
Sean Christophersonbcf600c2020-03-02 15:56:49 -0800732 /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
733 if (entry->eax == 1) {
734 entry = do_host_cpuid(array, function, 1);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800735 if (!entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200736 goto out;
737
Sean Christophersonbd791992020-03-02 15:56:53 -0800738 cpuid_entry_override(entry, CPUID_7_1_EAX);
Sean Christopherson09f628a2020-03-02 15:56:48 -0800739 entry->ebx = 0;
740 entry->ecx = 0;
741 entry->edx = 0;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200742 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200743 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200744 case 9:
745 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200746 case 0xa: { /* Architectural Performance Monitoring */
747 struct x86_pmu_capability cap;
748 union cpuid10_eax eax;
749 union cpuid10_edx edx;
750
751 perf_get_x86_pmu_capability(&cap);
752
753 /*
754 * Only support guest architectural pmu on a host
755 * with architectural pmu.
756 */
757 if (!cap.version)
758 memset(&cap, 0, sizeof(cap));
759
760 eax.split.version_id = min(cap.version, 2);
761 eax.split.num_counters = cap.num_counters_gp;
762 eax.split.bit_width = cap.bit_width_gp;
763 eax.split.mask_length = cap.events_mask_len;
764
Like Xu2e8cd7a2020-06-24 09:59:28 +0800765 edx.split.num_counters_fixed = min(cap.num_counters_fixed, MAX_FIXED_COUNTERS);
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200766 edx.split.bit_width_fixed = cap.bit_width_fixed;
Stephane Eraniancadbaa02020-10-28 12:42:47 -0700767 edx.split.anythread_deprecated = 1;
768 edx.split.reserved1 = 0;
769 edx.split.reserved2 = 0;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200770
771 entry->eax = eax.full;
772 entry->ebx = cap.events_mask;
773 entry->ecx = 0;
774 entry->edx = edx.full;
775 break;
776 }
Like Xua87f2d32019-06-06 09:18:45 +0800777 /*
778 * Per Intel's SDM, the 0x1f is a superset of 0xb,
779 * thus they can be handled by common code.
780 */
781 case 0x1f:
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800782 case 0xb:
Jim Mattsona1a640b2019-09-25 11:17:14 -0700783 /*
Sean Christophersone53c95e2020-03-02 15:56:19 -0800784 * Populate entries until the level type (ECX[15:8]) of the
785 * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
786 * the starting entry, filled by the primary do_host_cpuid().
Jim Mattsona1a640b2019-09-25 11:17:14 -0700787 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800788 for (i = 1; entry->ecx & 0xff00; ++i) {
789 entry = do_host_cpuid(array, function, i);
790 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200791 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200792 }
793 break;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800794 case 0xd:
795 entry->eax &= supported_xcr0;
796 entry->ebx = xstate_required_size(supported_xcr0, false);
Radim Krčmáře08e8332014-12-04 18:30:41 +0100797 entry->ecx = entry->ebx;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800798 entry->edx &= supported_xcr0 >> 32;
799 if (!supported_xcr0)
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100800 break;
801
Sean Christophersone53c95e2020-03-02 15:56:19 -0800802 entry = do_host_cpuid(array, function, 1);
803 if (!entry)
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800804 goto out;
805
Sean Christophersonbd791992020-03-02 15:56:53 -0800806 cpuid_entry_override(entry, CPUID_D_1_EAX);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800807 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100808 entry->ebx = xstate_required_size(supported_xcr0 | supported_xss,
809 true);
810 else {
811 WARN_ON_ONCE(supported_xss != 0);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800812 entry->ebx = 0;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100813 }
814 entry->ecx &= supported_xss;
815 entry->edx &= supported_xss >> 32;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800816
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800817 for (i = 2; i < 64; ++i) {
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100818 bool s_state;
819 if (supported_xcr0 & BIT_ULL(i))
820 s_state = false;
821 else if (supported_xss & BIT_ULL(i))
822 s_state = true;
823 else
Sean Christopherson1893c942020-03-02 15:56:10 -0800824 continue;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800825
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800826 entry = do_host_cpuid(array, function, i);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800827 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200828 goto out;
829
Sean Christopherson91001d42020-03-02 15:56:11 -0800830 /*
Sean Christophersoncfc48182020-03-02 15:56:23 -0800831 * The supported check above should have filtered out
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100832 * invalid sub-leafs. Only valid sub-leafs should
Sean Christopherson91001d42020-03-02 15:56:11 -0800833 * reach this point, and they should have a non-zero
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100834 * save state size. Furthermore, check whether the
835 * processor agrees with supported_xcr0/supported_xss
836 * on whether this is an XCR0- or IA32_XSS-managed area.
Sean Christopherson91001d42020-03-02 15:56:11 -0800837 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100838 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800839 --array->nent;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800840 continue;
Sean Christopherson8b2fc442020-03-02 15:56:12 -0800841 }
Sean Christophersone53c95e2020-03-02 15:56:19 -0800842 entry->edx = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200843 }
844 break;
Sean Christopherson72add912021-04-12 16:21:42 +1200845 case 0x12:
846 /* Intel SGX */
847 if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) {
848 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
849 break;
850 }
851
852 /*
853 * Index 0: Sub-features, MISCSELECT (a.k.a extended features)
854 * and max enclave sizes. The SGX sub-features and MISCSELECT
855 * are restricted by kernel and KVM capabilities (like most
856 * feature flags), while enclave size is unrestricted.
857 */
858 cpuid_entry_override(entry, CPUID_12_EAX);
859 entry->ebx &= SGX_MISC_EXINFO;
860
861 entry = do_host_cpuid(array, function, 1);
862 if (!entry)
863 goto out;
864
865 /*
866 * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la
867 * feature flags. Advertise all supported flags, including
868 * privileged attributes that require explicit opt-in from
869 * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is
870 * expected to derive it from supported XCR0.
871 */
872 entry->eax &= SGX_ATTR_DEBUG | SGX_ATTR_MODE64BIT |
Sean Christophersonfe7e9482021-04-12 16:21:43 +1200873 SGX_ATTR_PROVISIONKEY | SGX_ATTR_EINITTOKENKEY |
Sean Christopherson72add912021-04-12 16:21:42 +1200874 SGX_ATTR_KSS;
875 entry->ebx &= 0;
876 break;
Chao Peng86f52012018-10-24 16:05:11 +0800877 /* Intel PT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800878 case 0x14:
Sean Christophersondd69cc22020-03-02 15:56:55 -0800879 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
Sean Christopherson73920792020-03-02 15:56:26 -0800880 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Chao Peng86f52012018-10-24 16:05:11 +0800881 break;
Sean Christopherson73920792020-03-02 15:56:26 -0800882 }
Chao Peng86f52012018-10-24 16:05:11 +0800883
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800884 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800885 if (!do_host_cpuid(array, function, i))
Chao Peng86f52012018-10-24 16:05:11 +0800886 goto out;
Chao Peng86f52012018-10-24 16:05:11 +0800887 }
888 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200889 case KVM_CPUID_SIGNATURE: {
Mathias Krause326d07c2012-08-30 01:30:13 +0200890 static const char signature[12] = "KVMKVMKVM\0\0";
891 const u32 *sigptr = (const u32 *)signature;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +0300892 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +0200893 entry->ebx = sigptr[0];
894 entry->ecx = sigptr[1];
895 entry->edx = sigptr[2];
896 break;
897 }
898 case KVM_CPUID_FEATURES:
899 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
900 (1 << KVM_FEATURE_NOP_IO_DELAY) |
901 (1 << KVM_FEATURE_CLOCKSOURCE2) |
902 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300903 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +0530904 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
Wanpeng Lif38a7b72017-12-12 17:33:04 -0800905 (1 << KVM_FEATURE_PV_UNHALT) |
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100906 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
Wanpeng Li4180bf12018-07-23 14:39:54 +0800907 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -0300908 (1 << KVM_FEATURE_PV_SEND_IPI) |
Wanpeng Li32b72ec2019-06-11 20:23:50 +0800909 (1 << KVM_FEATURE_POLL_CONTROL) |
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +0200910 (1 << KVM_FEATURE_PV_SCHED_YIELD) |
911 (1 << KVM_FEATURE_ASYNC_PF_INT);
Avi Kivity00b27a32011-11-23 16:30:32 +0200912
913 if (sched_info_on())
914 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
915
916 entry->ebx = 0;
917 entry->ecx = 0;
918 entry->edx = 0;
919 break;
920 case 0x80000000:
Brijesh Singh8765d752017-12-04 10:57:25 -0600921 entry->eax = min(entry->eax, 0x8000001f);
Avi Kivity00b27a32011-11-23 16:30:32 +0200922 break;
923 case 0x80000001:
Sean Christophersonbd791992020-03-02 15:56:53 -0800924 cpuid_entry_override(entry, CPUID_8000_0001_EDX);
925 cpuid_entry_override(entry, CPUID_8000_0001_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200926 break;
Eric Northup43d05de2020-04-14 18:23:20 -0700927 case 0x80000006:
928 /* L2 cache and TLB: pass through host info. */
929 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -0300930 case 0x80000007: /* Advanced power management */
931 /* invariant TSC is CPUID.80000007H:EDX[8] */
932 entry->edx &= (1 << 8);
933 /* mask against host */
934 entry->edx &= boot_cpu_data.x86_power;
935 entry->eax = entry->ebx = entry->ecx = 0;
936 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200937 case 0x80000008: {
938 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
939 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
940 unsigned phys_as = entry->eax & 0xff;
941
942 if (!g_phys_as)
943 g_phys_as = phys_as;
944 entry->eax = g_phys_as | (virt_as << 8);
Ashok Raj15d45072018-02-01 22:59:43 +0100945 entry->edx = 0;
Sean Christophersonbd791992020-03-02 15:56:53 -0800946 cpuid_entry_override(entry, CPUID_8000_0008_EBX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200947 break;
948 }
Sean Christopherson25703872020-03-02 15:57:09 -0800949 case 0x8000000A:
950 if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
951 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
952 break;
953 }
954 entry->eax = 1; /* SVM revision 1 */
955 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
956 ASID emulation to nested SVM */
957 entry->ecx = 0; /* Reserved */
958 cpuid_entry_override(entry, CPUID_8000_000A_EDX);
959 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200960 case 0x80000019:
961 entry->ecx = entry->edx = 0;
962 break;
963 case 0x8000001a:
Jim Mattson382409b2019-03-27 13:15:37 -0700964 case 0x8000001e:
Avi Kivity00b27a32011-11-23 16:30:32 +0200965 break;
Peter Gondac1de0f22019-11-21 12:33:43 -0800966 /* Support memory encryption cpuid if host supports it */
967 case 0x8000001F:
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700968 if (!kvm_cpu_cap_has(X86_FEATURE_SEV))
Peter Gondac1de0f22019-11-21 12:33:43 -0800969 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700970 else
971 cpuid_entry_override(entry, CPUID_8000_001F_EAX);
Peter Gondac1de0f22019-11-21 12:33:43 -0800972 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200973 /*Add support for Centaur's CPUID instruction*/
974 case 0xC0000000:
975 /*Just support up to 0xC0000004 now*/
976 entry->eax = min(entry->eax, 0xC0000004);
977 break;
978 case 0xC0000001:
Sean Christophersonbd791992020-03-02 15:56:53 -0800979 cpuid_entry_override(entry, CPUID_C000_0001_EDX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200980 break;
981 case 3: /* Processor serial number */
982 case 5: /* MONITOR/MWAIT */
Avi Kivity00b27a32011-11-23 16:30:32 +0200983 case 0xC0000002:
984 case 0xC0000003:
985 case 0xC0000004:
986 default:
987 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
988 break;
989 }
990
Sasha Levin831bf662011-11-28 11:20:29 +0200991 r = 0;
992
993out:
Avi Kivity00b27a32011-11-23 16:30:32 +0200994 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200995
996 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200997}
998
Sean Christophersone53c95e2020-03-02 15:56:19 -0800999static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1000 unsigned int type)
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001001{
1002 if (type == KVM_GET_EMULATED_CPUID)
Sean Christophersone53c95e2020-03-02 15:56:19 -08001003 return __do_cpuid_func_emulated(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001004
Sean Christophersone53c95e2020-03-02 15:56:19 -08001005 return __do_cpuid_func(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001006}
1007
Sean Christopherson8b860792020-03-02 15:56:06 -08001008#define CENTAUR_CPUID_SIGNATURE 0xC0000000
Sasha Levin831bf662011-11-28 11:20:29 +02001009
Sean Christophersone53c95e2020-03-02 15:56:19 -08001010static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1011 unsigned int type)
Sean Christopherson619a17f2020-03-02 15:56:05 -08001012{
1013 u32 limit;
1014 int r;
1015
Sean Christopherson8b860792020-03-02 15:56:06 -08001016 if (func == CENTAUR_CPUID_SIGNATURE &&
1017 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
1018 return 0;
1019
Sean Christophersone53c95e2020-03-02 15:56:19 -08001020 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -08001021 if (r)
1022 return r;
1023
Sean Christophersone53c95e2020-03-02 15:56:19 -08001024 limit = array->entries[array->nent - 1].eax;
Sean Christopherson619a17f2020-03-02 15:56:05 -08001025 for (func = func + 1; func <= limit; ++func) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001026 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -08001027 if (r)
1028 break;
1029 }
1030
1031 return r;
1032}
1033
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001034static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
1035 __u32 num_entries, unsigned int ioctl_type)
1036{
1037 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +01001038 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001039
1040 if (ioctl_type != KVM_GET_EMULATED_CPUID)
1041 return false;
1042
1043 /*
1044 * We want to make sure that ->padding is being passed clean from
1045 * userspace in case we want to use it for something in the future.
1046 *
1047 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
1048 * have to give ourselves satisfied only with the emulated side. /me
1049 * sheds a tear.
1050 */
1051 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +01001052 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
1053 return true;
1054
1055 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001056 return true;
1057 }
1058 return false;
1059}
1060
1061int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
1062 struct kvm_cpuid_entry2 __user *entries,
1063 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +02001064{
Sean Christopherson8b860792020-03-02 15:56:06 -08001065 static const u32 funcs[] = {
1066 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
Sasha Levin831bf662011-11-28 11:20:29 +02001067 };
Avi Kivity00b27a32011-11-23 16:30:32 +02001068
Sean Christophersone53c95e2020-03-02 15:56:19 -08001069 struct kvm_cpuid_array array = {
1070 .nent = 0,
Sean Christophersone53c95e2020-03-02 15:56:19 -08001071 };
1072 int r, i;
Sean Christophersond5a661d2020-03-02 15:56:07 -08001073
Avi Kivity00b27a32011-11-23 16:30:32 +02001074 if (cpuid->nent < 1)
Sean Christophersond5a661d2020-03-02 15:56:07 -08001075 return -E2BIG;
Avi Kivity00b27a32011-11-23 16:30:32 +02001076 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1077 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001078
1079 if (sanity_check_entries(entries, cpuid->nent, type))
1080 return -EINVAL;
1081
Sean Christophersone53c95e2020-03-02 15:56:19 -08001082 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
Kees Cookfad953c2018-06-12 14:27:37 -07001083 cpuid->nent));
Sean Christophersone53c95e2020-03-02 15:56:19 -08001084 if (!array.entries)
Sean Christophersond5a661d2020-03-02 15:56:07 -08001085 return -ENOMEM;
Avi Kivity00b27a32011-11-23 16:30:32 +02001086
Xiaoyao Li65b18912020-06-04 12:16:36 +08001087 array.maxnent = cpuid->nent;
1088
Sean Christopherson8b860792020-03-02 15:56:06 -08001089 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001090 r = get_cpuid_func(&array, funcs[i], type);
Sasha Levin831bf662011-11-28 11:20:29 +02001091 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +02001092 goto out_free;
1093 }
Sean Christophersone53c95e2020-03-02 15:56:19 -08001094 cpuid->nent = array.nent;
Avi Kivity00b27a32011-11-23 16:30:32 +02001095
Sean Christophersone53c95e2020-03-02 15:56:19 -08001096 if (copy_to_user(entries, array.entries,
1097 array.nent * sizeof(struct kvm_cpuid_entry2)))
Sean Christophersond5a661d2020-03-02 15:56:07 -08001098 r = -EFAULT;
Avi Kivity00b27a32011-11-23 16:30:32 +02001099
1100out_free:
Sean Christophersone53c95e2020-03-02 15:56:19 -08001101 vfree(array.entries);
Avi Kivity00b27a32011-11-23 16:30:32 +02001102 return r;
1103}
1104
Avi Kivity00b27a32011-11-23 16:30:32 +02001105struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
1106 u32 function, u32 index)
1107{
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +02001108 return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
1109 function, index);
Avi Kivity00b27a32011-11-23 16:30:32 +02001110}
1111EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
1112
Avi Kivity00b27a32011-11-23 16:30:32 +02001113/*
Sean Christopherson8d892312020-03-04 17:34:34 -08001114 * Intel CPUID semantics treats any query for an out-of-range leaf as if the
1115 * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
1116 * returns all zeroes for any undefined leaf, whether or not the leaf is in
1117 * range. Centaur/VIA follows Intel semantics.
1118 *
1119 * A leaf is considered out-of-range if its function is higher than the maximum
1120 * supported leaf of its associated class or if its associated class does not
1121 * exist.
1122 *
1123 * There are three primary classes to be considered, with their respective
1124 * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
1125 * class exists if a guest CPUID entry for its <base> leaf exists. For a given
1126 * class, CPUID.<base>.EAX contains the max supported leaf for the class.
1127 *
1128 * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
1129 * - Hypervisor: 0x40000000 - 0x4fffffff
1130 * - Extended: 0x80000000 - 0xbfffffff
1131 * - Centaur: 0xc0000000 - 0xcfffffff
1132 *
1133 * The Hypervisor class is further subdivided into sub-classes that each act as
Ingo Molnard9f6e122021-03-18 15:28:01 +01001134 * their own independent class associated with a 0x100 byte range. E.g. if Qemu
Sean Christopherson8d892312020-03-04 17:34:34 -08001135 * is advertising support for both HyperV and KVM, the resulting Hypervisor
1136 * CPUID sub-classes are:
1137 *
1138 * - HyperV: 0x40000000 - 0x400000ff
1139 * - KVM: 0x40000100 - 0x400001ff
Avi Kivity00b27a32011-11-23 16:30:32 +02001140 */
Sean Christopherson09c74312020-03-04 17:34:36 -08001141static struct kvm_cpuid_entry2 *
1142get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +02001143{
Sean Christopherson8d892312020-03-04 17:34:34 -08001144 struct kvm_cpuid_entry2 *basic, *class;
Sean Christopherson09c74312020-03-04 17:34:36 -08001145 u32 function = *fn_ptr;
Avi Kivity00b27a32011-11-23 16:30:32 +02001146
Sean Christopherson8d892312020-03-04 17:34:34 -08001147 basic = kvm_find_cpuid_entry(vcpu, 0, 0);
1148 if (!basic)
Sean Christopherson09c74312020-03-04 17:34:36 -08001149 return NULL;
1150
1151 if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
1152 is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
1153 return NULL;
Sean Christopherson8d892312020-03-04 17:34:34 -08001154
1155 if (function >= 0x40000000 && function <= 0x4fffffff)
1156 class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
1157 else if (function >= 0xc0000000)
1158 class = kvm_find_cpuid_entry(vcpu, 0xc0000000, 0);
1159 else
1160 class = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
1161
Sean Christopherson09c74312020-03-04 17:34:36 -08001162 if (class && function <= class->eax)
1163 return NULL;
1164
1165 /*
1166 * Leaf specific adjustments are also applied when redirecting to the
1167 * max basic entry, e.g. if the max basic leaf is 0xb but there is no
1168 * entry for CPUID.0xb.index (see below), then the output value for EDX
1169 * needs to be pulled from CPUID.0xb.1.
1170 */
1171 *fn_ptr = basic->eax;
1172
1173 /*
1174 * The class does not exist or the requested function is out of range;
1175 * the effective CPUID entry is the max basic leaf. Note, the index of
1176 * the original requested leaf is observed!
1177 */
1178 return kvm_find_cpuid_entry(vcpu, basic->eax, index);
Avi Kivity00b27a32011-11-23 16:30:32 +02001179}
1180
Yu Zhange911eb32017-08-24 20:27:52 +08001181bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
Sean Christophersonf91af512020-03-04 17:34:37 -08001182 u32 *ecx, u32 *edx, bool exact_only)
Avi Kivity00b27a32011-11-23 16:30:32 +02001183{
Jan Kiszkab7fb8482020-03-04 17:34:31 -08001184 u32 orig_function = *eax, function = *eax, index = *ecx;
Jim Mattson43561122019-09-25 17:04:17 -07001185 struct kvm_cpuid_entry2 *entry;
Sean Christopherson2b110b62020-03-17 12:53:54 -07001186 bool exact, used_max_basic = false;
Avi Kivity00b27a32011-11-23 16:30:32 +02001187
Jim Mattson43561122019-09-25 17:04:17 -07001188 entry = kvm_find_cpuid_entry(vcpu, function, index);
Sean Christophersonf91af512020-03-04 17:34:37 -08001189 exact = !!entry;
Sean Christopherson09c74312020-03-04 17:34:36 -08001190
Sean Christopherson2b110b62020-03-17 12:53:54 -07001191 if (!entry && !exact_only) {
Sean Christopherson09c74312020-03-04 17:34:36 -08001192 entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
Sean Christopherson2b110b62020-03-17 12:53:54 -07001193 used_max_basic = !!entry;
1194 }
Sean Christopherson09c74312020-03-04 17:34:36 -08001195
Jim Mattson43561122019-09-25 17:04:17 -07001196 if (entry) {
1197 *eax = entry->eax;
1198 *ebx = entry->ebx;
1199 *ecx = entry->ecx;
1200 *edx = entry->edx;
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001201 if (function == 7 && index == 0) {
1202 u64 data;
1203 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1204 (data & TSX_CTRL_CPUID_CLEAR))
1205 *ebx &= ~(F(RTM) | F(HLE));
1206 }
Jim Mattson43561122019-09-25 17:04:17 -07001207 } else {
Avi Kivity62046e52012-06-07 14:07:48 +03001208 *eax = *ebx = *ecx = *edx = 0;
Jim Mattson43561122019-09-25 17:04:17 -07001209 /*
1210 * When leaf 0BH or 1FH is defined, CL is pass-through
1211 * and EDX is always the x2APIC ID, even for undefined
1212 * subleaves. Index 1 will exist iff the leaf is
1213 * implemented, so we pass through CL iff leaf 1
1214 * exists. EDX can be copied from any existing index.
1215 */
1216 if (function == 0xb || function == 0x1f) {
1217 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1218 if (entry) {
1219 *ecx = index & 0xff;
1220 *edx = entry->edx;
1221 }
1222 }
1223 }
Sean Christopherson2b110b62020-03-17 12:53:54 -07001224 trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
1225 used_max_basic);
Sean Christophersonf91af512020-03-04 17:34:37 -08001226 return exact;
Avi Kivity62046e52012-06-07 14:07:48 +03001227}
Julian Stecklina66f7b722012-12-05 15:26:19 +01001228EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +03001229
Kyle Huey6a908b62016-11-29 12:40:37 -08001230int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity62046e52012-06-07 14:07:48 +03001231{
Jiang Biao1e131752016-11-07 08:55:49 +08001232 u32 eax, ebx, ecx, edx;
Avi Kivity62046e52012-06-07 14:07:48 +03001233
Kyle Hueydb2336a2017-03-20 01:16:28 -07001234 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1235 return 1;
1236
Sean Christophersonde3cd112019-04-30 10:36:17 -07001237 eax = kvm_rax_read(vcpu);
1238 ecx = kvm_rcx_read(vcpu);
Sean Christophersonf91af512020-03-04 17:34:37 -08001239 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001240 kvm_rax_write(vcpu, eax);
1241 kvm_rbx_write(vcpu, ebx);
1242 kvm_rcx_write(vcpu, ecx);
1243 kvm_rdx_write(vcpu, edx);
Kyle Huey6affcbe2016-11-29 12:40:40 -08001244 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +02001245}
1246EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);