blob: 3902c28fb6cb7c1f260d0e4651913d4735671178 [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
Guang Zengbe50b202022-01-05 04:35:29 -080035u32 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);
Jing Liucc04b6a2022-01-05 04:35:14 -080045 /* ECX[1]: 64B alignment in compacted form */
46 if (compacted)
47 offset = (ecx & 0x2) ? ALIGN(ret, 64) : ret;
48 else
49 offset = ebx;
Paolo Bonzini412a3c42014-12-03 14:38:01 +010050 ret = max(ret, offset + eax);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020051 }
52
53 xstate_bv >>= 1;
54 feature_bit++;
55 }
56
57 return ret;
58}
59
Babu Mogerb73a5432021-09-23 20:15:28 -050060/*
61 * This one is tied to SSB in the user API, and not
62 * visible in /proc/cpuinfo.
63 */
64#define KVM_X86_FEATURE_PSFD (13*32+28) /* Predictive Store Forwarding Disable */
65
Sean Christopherson87382002019-12-17 13:32:42 -080066#define F feature_bit
Sean Christopherson4e66c0c2021-04-12 16:21:35 +120067#define SF(name) (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0)
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010068
Babu Mogerb73a5432021-09-23 20:15:28 -050069
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +020070static inline struct kvm_cpuid_entry2 *cpuid_entry2_find(
71 struct kvm_cpuid_entry2 *entries, int nent, u32 function, u32 index)
72{
73 struct kvm_cpuid_entry2 *e;
74 int i;
75
76 for (i = 0; i < nent; i++) {
77 e = &entries[i];
78
Sean Christophersone8a747d2021-09-29 15:24:25 -070079 if (e->function == function &&
80 (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) || e->index == index))
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +020081 return e;
82 }
83
84 return NULL;
85}
86
Jing Liu5ab2f452022-01-05 04:35:19 -080087static int kvm_check_cpuid(struct kvm_vcpu *vcpu,
88 struct kvm_cpuid_entry2 *entries,
89 int nent)
Xiaoyao Lia76733a2020-07-09 12:34:22 +080090{
91 struct kvm_cpuid_entry2 *best;
Jing Liu5ab2f452022-01-05 04:35:19 -080092 u64 xfeatures;
Xiaoyao Lia76733a2020-07-09 12:34:22 +080093
94 /*
95 * The existing code assumes virtual address is 48-bit or 57-bit in the
96 * canonical address checks; exit if it is ever changed.
97 */
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +020098 best = cpuid_entry2_find(entries, nent, 0x80000008, 0);
Xiaoyao Lia76733a2020-07-09 12:34:22 +080099 if (best) {
100 int vaddr_bits = (best->eax & 0xff00) >> 8;
101
102 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
103 return -EINVAL;
104 }
105
Jing Liu5ab2f452022-01-05 04:35:19 -0800106 /*
107 * Exposing dynamic xfeatures to the guest requires additional
108 * enabling in the FPU, e.g. to expand the guest XSAVE state size.
109 */
110 best = cpuid_entry2_find(entries, nent, 0xd, 0);
111 if (!best)
112 return 0;
113
114 xfeatures = best->eax | ((u64)best->edx << 32);
115 xfeatures &= XFEATURE_MASK_USER_DYNAMIC;
116 if (!xfeatures)
117 return 0;
118
119 return fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, xfeatures);
Xiaoyao Lia76733a2020-07-09 12:34:22 +0800120}
121
Vitaly Kuznetsovc6617c62022-01-17 16:05:40 +0100122/* Check whether the supplied CPUID data is equal to what is already set for the vCPU. */
123static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
124 int nent)
125{
126 struct kvm_cpuid_entry2 *orig;
127 int i;
128
129 if (nent != vcpu->arch.cpuid_nent)
130 return -EINVAL;
131
132 for (i = 0; i < nent; i++) {
133 orig = &vcpu->arch.cpuid_entries[i];
134 if (e2[i].function != orig->function ||
135 e2[i].index != orig->index ||
136 e2[i].eax != orig->eax || e2[i].ebx != orig->ebx ||
137 e2[i].ecx != orig->ecx || e2[i].edx != orig->edx)
138 return -EINVAL;
139 }
140
141 return 0;
142}
143
Paul Durrant760849b2021-11-05 09:51:01 +0000144static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
145{
146 u32 function;
147 struct kvm_cpuid_entry2 *entry;
148
149 vcpu->arch.kvm_cpuid_base = 0;
150
151 for_each_possible_hypervisor_cpuid_base(function) {
152 entry = kvm_find_cpuid_entry(vcpu, function, 0);
153
154 if (entry) {
155 u32 signature[3];
156
157 signature[0] = entry->ebx;
158 signature[1] = entry->ecx;
159 signature[2] = entry->edx;
160
161 BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE));
162 if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) {
163 vcpu->arch.kvm_cpuid_base = function;
164 break;
165 }
166 }
167 }
168}
169
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100170static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu,
171 struct kvm_cpuid_entry2 *entries, int nent)
Paul Durrant760849b2021-11-05 09:51:01 +0000172{
173 u32 base = vcpu->arch.kvm_cpuid_base;
174
175 if (!base)
176 return NULL;
177
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100178 return cpuid_entry2_find(entries, nent, base | KVM_CPUID_FEATURES, 0);
179}
180
181static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu)
182{
183 return __kvm_find_kvm_cpuid_features(vcpu, vcpu->arch.cpuid_entries,
184 vcpu->arch.cpuid_nent);
Paul Durrant760849b2021-11-05 09:51:01 +0000185}
186
Oliver Upton01b4f512020-10-27 16:10:42 -0700187void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
188{
Paul Durrant760849b2021-11-05 09:51:01 +0000189 struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu);
Oliver Upton01b4f512020-10-27 16:10:42 -0700190
191 /*
192 * save the feature bitmap to avoid cpuid lookup for every PV
193 * operation
194 */
195 if (best)
196 vcpu->arch.pv_cpuid.features = best->eax;
197}
198
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100199static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries,
200 int nent)
Avi Kivity00b27a32011-11-23 16:30:32 +0200201{
202 struct kvm_cpuid_entry2 *best;
Avi Kivity00b27a32011-11-23 16:30:32 +0200203
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100204 best = cpuid_entry2_find(entries, nent, 1, 0);
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800205 if (best) {
206 /* Update OSXSAVE bit */
207 if (boot_cpu_has(X86_FEATURE_XSAVE))
208 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
Sean Christophersonb32666b2020-03-02 15:56:31 -0800209 kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
Avi Kivity00b27a32011-11-23 16:30:32 +0200210
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800211 cpuid_entry_change(best, X86_FEATURE_APIC,
Sean Christophersonb32666b2020-03-02 15:56:31 -0800212 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800213 }
Jim Mattsonc7dd15b2016-11-09 09:50:11 -0800214
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100215 best = cpuid_entry2_find(entries, nent, 7, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800216 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
217 cpuid_entry_change(best, X86_FEATURE_OSPKE,
218 kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
Huaitong Hanb9baba82016-03-22 16:51:21 +0800219
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100220 best = cpuid_entry2_find(entries, nent, 0xD, 0);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800221 if (best)
Xiaoyao Lia71936ab2020-04-29 23:43:12 +0800222 best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
Paolo Bonzinid7876f12013-10-02 16:06:15 +0200223
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100224 best = cpuid_entry2_find(entries, nent, 0xD, 1);
Sean Christopherson4c615342020-03-02 15:56:30 -0800225 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
226 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
Paolo Bonzini412a3c42014-12-03 14:38:01 +0100227 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
228
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100229 best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent);
Wanpeng Licaa057a2018-03-12 04:53:03 -0700230 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
231 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
232 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
233
Wanpeng Li511a85562019-05-21 14:06:54 +0800234 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100235 best = cpuid_entry2_find(entries, nent, 0x1, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800236 if (best)
237 cpuid_entry_change(best, X86_FEATURE_MWAIT,
238 vcpu->arch.ia32_misc_enable_msr &
239 MSR_IA32_MISC_ENABLE_MWAIT);
Wanpeng Li511a85562019-05-21 14:06:54 +0800240 }
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800241}
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100242
243void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
244{
245 __kvm_update_cpuid_runtime(vcpu, vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent);
246}
Jim Mattson2259c172020-10-29 10:06:48 -0700247EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800248
Xiaoyao Li346ce352020-07-09 12:34:24 +0800249static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800250{
251 struct kvm_lapic *apic = vcpu->arch.apic;
252 struct kvm_cpuid_entry2 *best;
253
254 best = kvm_find_cpuid_entry(vcpu, 1, 0);
255 if (best && apic) {
256 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
257 apic->lapic_timer.timer_mode_mask = 3 << 17;
258 else
259 apic->lapic_timer.timer_mode_mask = 1 << 17;
260
261 kvm_apic_set_version(vcpu);
262 }
263
264 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
265 if (!best)
266 vcpu->arch.guest_supported_xcr0 = 0;
267 else
268 vcpu->arch.guest_supported_xcr0 =
269 (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
Wanpeng Li511a85562019-05-21 14:06:54 +0800270
Sean Christopherson72add912021-04-12 16:21:42 +1200271 /*
272 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1) enumerate
273 * the supported XSAVE Feature Request Mask (XFRM), i.e. the enclave's
274 * requested XCR0 value. The enclave's XFRM must be a subset of XCRO
275 * at the time of EENTER, thus adjust the allowed XFRM by the guest's
276 * supported XCR0. Similar to XCR0 handling, FP and SSE are forced to
277 * '1' even on CPUs that don't support XSAVE.
278 */
279 best = kvm_find_cpuid_entry(vcpu, 0x12, 0x1);
280 if (best) {
281 best->ecx &= vcpu->arch.guest_supported_xcr0 & 0xffffffff;
282 best->edx &= vcpu->arch.guest_supported_xcr0 >> 32;
283 best->ecx |= XFEATURE_MASK_FPSSE;
284 }
285
Oliver Upton01b4f512020-10-27 16:10:42 -0700286 kvm_update_pv_runtime(vcpu);
287
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300288 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Sean Christophersona8ac8642021-02-03 16:01:15 -0800289 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300290
Wei Huangc6702c92015-06-19 13:44:45 +0200291 kvm_pmu_refresh(vcpu);
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000292 vcpu->arch.cr4_guest_rsvd_bits =
293 __cr4_reserved_bits(guest_cpuid_has, vcpu);
Sean Christophersonc44d9b32020-09-29 21:16:56 -0700294
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +0100295 kvm_hv_set_cpuid(vcpu);
296
Sean Christophersonc44d9b32020-09-29 21:16:56 -0700297 /* Invoke the vendor callback only after the above state is updated. */
Jason Baronb36464772021-01-14 22:27:56 -0500298 static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu);
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800299
300 /*
Sean Christopherson49c6f872021-06-22 10:56:51 -0700301 * Except for the MMU, which needs to do its thing any vendor specific
302 * adjustments to the reserved GPA bits.
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800303 */
Sean Christopherson49c6f872021-06-22 10:56:51 -0700304 kvm_mmu_after_set_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200305}
306
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300307int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
308{
309 struct kvm_cpuid_entry2 *best;
310
311 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
312 if (!best || best->eax < 0x80000008)
313 goto not_found;
314 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
315 if (best)
316 return best->eax & 0xff;
317not_found:
318 return 36;
319}
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300320
Sean Christophersona8ac8642021-02-03 16:01:15 -0800321/*
322 * This "raw" version returns the reserved GPA bits without any adjustments for
323 * encryption technologies that usurp bits. The raw mask should be used if and
324 * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs.
325 */
326u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
327{
328 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
329}
330
Sean Christopherson8b44b172021-11-05 09:51:00 +0000331static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
332 int nent)
333{
Jing Liu5ab2f452022-01-05 04:35:19 -0800334 int r;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000335
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100336 __kvm_update_cpuid_runtime(vcpu, e2, nent);
337
Vitaly Kuznetsovc6617c62022-01-17 16:05:40 +0100338 /*
339 * KVM does not correctly handle changing guest CPUID after KVM_RUN, as
340 * MAXPHYADDR, GBPAGES support, AMD reserved bit behavior, etc.. aren't
341 * tracked in kvm_mmu_page_role. As a result, KVM may miss guest page
342 * faults due to reusing SPs/SPTEs. In practice no sane VMM mucks with
343 * the core vCPU model on the fly. It would've been better to forbid any
344 * KVM_SET_CPUID{,2} calls after KVM_RUN altogether but unfortunately
345 * some VMMs (e.g. QEMU) reuse vCPU fds for CPU hotplug/unplug and do
346 * KVM_SET_CPUID{,2} again. To support this legacy behavior, check
347 * whether the supplied CPUID data is equal to what's already set.
348 */
349 if (vcpu->arch.last_vmentry_cpu != -1)
350 return kvm_cpuid_check_equal(vcpu, e2, nent);
351
Jing Liu5ab2f452022-01-05 04:35:19 -0800352 r = kvm_check_cpuid(vcpu, e2, nent);
353 if (r)
354 return r;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000355
Jing Liu5ab2f452022-01-05 04:35:19 -0800356 kvfree(vcpu->arch.cpuid_entries);
357 vcpu->arch.cpuid_entries = e2;
358 vcpu->arch.cpuid_nent = nent;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000359
Jing Liu5ab2f452022-01-05 04:35:19 -0800360 kvm_update_kvm_cpuid_base(vcpu);
Jing Liu5ab2f452022-01-05 04:35:19 -0800361 kvm_vcpu_after_set_cpuid(vcpu);
Sean Christopherson8b44b172021-11-05 09:51:00 +0000362
Jing Liu5ab2f452022-01-05 04:35:19 -0800363 return 0;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000364}
365
Avi Kivity00b27a32011-11-23 16:30:32 +0200366/* when an old userspace process fills a new kernel module */
367int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
368 struct kvm_cpuid *cpuid,
369 struct kvm_cpuid_entry __user *entries)
370{
371 int r, i;
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200372 struct kvm_cpuid_entry *e = NULL;
373 struct kvm_cpuid_entry2 *e2 = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200374
Avi Kivity00b27a32011-11-23 16:30:32 +0200375 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200376 return -E2BIG;
377
Paolo Bonzini83676e92016-06-01 14:09:19 +0200378 if (cpuid->nent) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200379 e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent));
380 if (IS_ERR(e))
381 return PTR_ERR(e);
382
383 e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
384 if (!e2) {
385 r = -ENOMEM;
386 goto out_free_cpuid;
Denis Efremov7ec28e22020-06-03 13:11:31 +0300387 }
Paolo Bonzini83676e92016-06-01 14:09:19 +0200388 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200389 for (i = 0; i < cpuid->nent; i++) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200390 e2[i].function = e[i].function;
391 e2[i].eax = e[i].eax;
392 e2[i].ebx = e[i].ebx;
393 e2[i].ecx = e[i].ecx;
394 e2[i].edx = e[i].edx;
395 e2[i].index = 0;
396 e2[i].flags = 0;
397 e2[i].padding[0] = 0;
398 e2[i].padding[1] = 0;
399 e2[i].padding[2] = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200400 }
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200401
Sean Christopherson8b44b172021-11-05 09:51:00 +0000402 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
403 if (r)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200404 kvfree(e2);
Avi Kivity00b27a32011-11-23 16:30:32 +0200405
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200406out_free_cpuid:
407 kvfree(e);
408
Avi Kivity00b27a32011-11-23 16:30:32 +0200409 return r;
410}
411
412int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
413 struct kvm_cpuid2 *cpuid,
414 struct kvm_cpuid_entry2 __user *entries)
415{
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200416 struct kvm_cpuid_entry2 *e2 = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200417 int r;
418
Avi Kivity00b27a32011-11-23 16:30:32 +0200419 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200420 return -E2BIG;
421
422 if (cpuid->nent) {
423 e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent));
424 if (IS_ERR(e2))
425 return PTR_ERR(e2);
Xiaoyao Lia76733a2020-07-09 12:34:22 +0800426 }
427
Sean Christopherson8b44b172021-11-05 09:51:00 +0000428 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
429 if (r)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200430 kvfree(e2);
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200431
Sean Christopherson8b44b172021-11-05 09:51:00 +0000432 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200433}
434
435int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
436 struct kvm_cpuid2 *cpuid,
437 struct kvm_cpuid_entry2 __user *entries)
438{
439 int r;
440
441 r = -E2BIG;
442 if (cpuid->nent < vcpu->arch.cpuid_nent)
443 goto out;
444 r = -EFAULT;
Michael Roth181f4942021-01-27 20:44:51 -0600445 if (copy_to_user(entries, vcpu->arch.cpuid_entries,
Avi Kivity00b27a32011-11-23 16:30:32 +0200446 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
447 goto out;
448 return 0;
449
450out:
451 cpuid->nent = vcpu->arch.cpuid_nent;
452 return r;
453}
454
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200455/* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */
Sean Christopherson462f8dd2021-04-20 18:08:50 -0700456static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf)
Sean Christopherson66a69502020-03-02 15:56:41 -0800457{
Sean Christophersond8577a42020-03-02 15:56:52 -0800458 const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
459 struct kvm_cpuid_entry2 entry;
460
Sean Christopherson66a69502020-03-02 15:56:41 -0800461 reverse_cpuid_check(leaf);
Sean Christophersond8577a42020-03-02 15:56:52 -0800462
463 cpuid_count(cpuid.function, cpuid.index,
464 &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
465
Sean Christopherson855c7e92020-03-25 12:12:59 -0700466 kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
Sean Christopherson66a69502020-03-02 15:56:41 -0800467}
468
Sean Christopherson462f8dd2021-04-20 18:08:50 -0700469static __always_inline
470void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask)
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200471{
472 /* Use kvm_cpu_cap_mask for non-scattered leafs. */
473 BUILD_BUG_ON(leaf < NCAPINTS);
474
475 kvm_cpu_caps[leaf] = mask;
476
477 __kvm_cpu_cap_mask(leaf);
478}
479
480static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
481{
482 /* Use kvm_cpu_cap_init_scattered for scattered leafs. */
483 BUILD_BUG_ON(leaf >= NCAPINTS);
484
485 kvm_cpu_caps[leaf] &= mask;
486
487 __kvm_cpu_cap_mask(leaf);
488}
489
Sean Christopherson66a69502020-03-02 15:56:41 -0800490void kvm_set_cpu_caps(void)
491{
Sean Christopherson66a69502020-03-02 15:56:41 -0800492#ifdef CONFIG_X86_64
493 unsigned int f_gbpages = F(GBPAGES);
494 unsigned int f_lm = F(LM);
Jing Liu690a7572022-01-05 04:35:27 -0800495 unsigned int f_xfd = F(XFD);
Sean Christopherson66a69502020-03-02 15:56:41 -0800496#else
497 unsigned int f_gbpages = 0;
498 unsigned int f_lm = 0;
Jing Liu690a7572022-01-05 04:35:27 -0800499 unsigned int f_xfd = 0;
Sean Christopherson66a69502020-03-02 15:56:41 -0800500#endif
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200501 memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
Sean Christopherson66a69502020-03-02 15:56:41 -0800502
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200503 BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
Sean Christopherson66a69502020-03-02 15:56:41 -0800504 sizeof(boot_cpu_data.x86_capability));
505
506 memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200507 sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)));
Sean Christopherson66a69502020-03-02 15:56:41 -0800508
509 kvm_cpu_cap_mask(CPUID_1_ECX,
510 /*
511 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
512 * advertised to guests via CPUID!
513 */
514 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
515 0 /* DS-CPL, VMX, SMX, EST */ |
516 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
Like Xu27461da32020-05-29 15:43:45 +0800517 F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800518 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
519 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
520 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
521 F(F16C) | F(RDRAND)
522 );
Sean Christopherson93c380e2020-03-02 15:56:54 -0800523 /* KVM emulates x2apic in software irrespective of host support. */
524 kvm_cpu_cap_set(X86_FEATURE_X2APIC);
Sean Christopherson66a69502020-03-02 15:56:41 -0800525
526 kvm_cpu_cap_mask(CPUID_1_EDX,
527 F(FPU) | F(VME) | F(DE) | F(PSE) |
528 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
529 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
530 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
531 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
532 0 /* Reserved, DS, ACPI */ | F(MMX) |
533 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
534 0 /* HTT, TM, Reserved, PBE */
535 );
536
537 kvm_cpu_cap_mask(CPUID_7_0_EBX,
Sean Christopherson72add912021-04-12 16:21:42 +1200538 F(FSGSBASE) | F(SGX) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
Sean Christophersone4203332021-02-11 16:34:10 -0800539 F(BMI2) | F(ERMS) | F(INVPCID) | F(RTM) | 0 /*MPX*/ | F(RDSEED) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800540 F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
541 F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
542 F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | 0 /*INTEL_PT*/
543 );
544
545 kvm_cpu_cap_mask(CPUID_7_ECX,
Babu Mogerfa44b822020-05-12 18:59:16 -0500546 F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800547 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
548 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
Sean Christopherson72add912021-04-12 16:21:42 +1200549 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
Paolo Bonzini76ea4382021-05-06 06:30:04 -0400550 F(SGX_LC) | F(BUS_LOCK_DETECT)
Sean Christopherson66a69502020-03-02 15:56:41 -0800551 );
552 /* Set LA57 based on hardware capability. */
553 if (cpuid_ecx(7) & F(LA57))
554 kvm_cpu_cap_set(X86_FEATURE_LA57);
555
Babu Mogerfa44b822020-05-12 18:59:16 -0500556 /*
557 * PKU not yet implemented for shadow paging and requires OSPKE
558 * to be set on the host. Clear it if that is not the case
559 */
560 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
561 kvm_cpu_cap_clear(X86_FEATURE_PKU);
562
Sean Christopherson66a69502020-03-02 15:56:41 -0800563 kvm_cpu_cap_mask(CPUID_7_EDX,
564 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
565 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
Paolo Bonzini43bd9ef2020-08-09 13:04:56 -0400566 F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
Jing Liu690a7572022-01-05 04:35:27 -0800567 F(SERIALIZE) | F(TSXLDTRK) | F(AVX512_FP16) |
568 F(AMX_TILE) | F(AMX_INT8) | F(AMX_BF16)
Sean Christopherson66a69502020-03-02 15:56:41 -0800569 );
570
Sean Christopherson93c380e2020-03-02 15:56:54 -0800571 /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
572 kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
573 kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
574
575 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
576 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
577 if (boot_cpu_has(X86_FEATURE_STIBP))
578 kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
579 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
580 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
581
Sean Christopherson66a69502020-03-02 15:56:41 -0800582 kvm_cpu_cap_mask(CPUID_7_1_EAX,
Yang Zhong1085a6b2021-01-05 08:49:09 +0800583 F(AVX_VNNI) | F(AVX512_BF16)
Sean Christopherson66a69502020-03-02 15:56:41 -0800584 );
585
586 kvm_cpu_cap_mask(CPUID_D_1_EAX,
Jing Liu690a7572022-01-05 04:35:27 -0800587 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd
Sean Christopherson66a69502020-03-02 15:56:41 -0800588 );
589
Sean Christopherson72add912021-04-12 16:21:42 +1200590 kvm_cpu_cap_init_scattered(CPUID_12_EAX,
591 SF(SGX1) | SF(SGX2)
592 );
593
Sean Christopherson66a69502020-03-02 15:56:41 -0800594 kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
595 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
596 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
597 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
598 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
Like Xub1d66da2021-11-17 16:03:04 +0800599 F(TOPOEXT) | 0 /* PERFCTR_CORE */
Sean Christopherson66a69502020-03-02 15:56:41 -0800600 );
601
602 kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
603 F(FPU) | F(VME) | F(DE) | F(PSE) |
604 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
605 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
606 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
607 F(PAT) | F(PSE36) | 0 /* Reserved */ |
Sean Christopherson13832792021-08-05 11:38:04 -0700608 F(NX) | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800609 F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
610 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
611 );
612
613 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
614 kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
615
616 kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
617 F(CLZERO) | F(XSAVEERPTR) |
618 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
Babu Mogerb73a5432021-09-23 20:15:28 -0500619 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON) |
620 __feature_bit(KVM_X86_FEATURE_PSFD)
Sean Christopherson66a69502020-03-02 15:56:41 -0800621 );
622
Sean Christopherson9b58b982020-03-02 15:56:42 -0800623 /*
Sean Christopherson93c380e2020-03-02 15:56:54 -0800624 * AMD has separate bits for each SPEC_CTRL bit.
625 * arch/x86/kernel/cpu/bugs.c is kind enough to
626 * record that in cpufeatures so use them.
627 */
628 if (boot_cpu_has(X86_FEATURE_IBPB))
629 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
630 if (boot_cpu_has(X86_FEATURE_IBRS))
631 kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
632 if (boot_cpu_has(X86_FEATURE_STIBP))
633 kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
634 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
635 kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
636 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
637 kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
638 /*
639 * The preference is to use SPEC CTRL MSR instead of the
640 * VIRT_SPEC MSR.
641 */
642 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
643 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
644 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
645
646 /*
Sean Christopherson9b58b982020-03-02 15:56:42 -0800647 * Hide all SVM features by default, SVM will set the cap bits for
648 * features it emulates and/or exposes for L1.
649 */
650 kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
651
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700652 kvm_cpu_cap_mask(CPUID_8000_001F_EAX,
653 0 /* SME */ | F(SEV) | 0 /* VM_PAGE_FLUSH */ | F(SEV_ES) |
654 F(SME_COHERENT));
655
Sean Christopherson66a69502020-03-02 15:56:41 -0800656 kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
657 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
658 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
659 F(PMM) | F(PMM_EN)
660 );
Sean Christopherson78bba962021-05-04 10:17:34 -0700661
662 /*
663 * Hide RDTSCP and RDPID if either feature is reported as supported but
664 * probing MSR_TSC_AUX failed. This is purely a sanity check and
665 * should never happen, but the guest will likely crash if RDTSCP or
666 * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in
667 * the past. For example, the sanity check may fire if this instance of
668 * KVM is running as L1 on top of an older, broken KVM.
669 */
670 if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) ||
671 kvm_cpu_cap_has(X86_FEATURE_RDPID)) &&
672 !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) {
673 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
674 kvm_cpu_cap_clear(X86_FEATURE_RDPID);
675 }
Sean Christopherson66a69502020-03-02 15:56:41 -0800676}
677EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
678
Sean Christophersone53c95e2020-03-02 15:56:19 -0800679struct kvm_cpuid_array {
680 struct kvm_cpuid_entry2 *entries;
Xiaoyao Li65b18912020-06-04 12:16:36 +0800681 int maxnent;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800682 int nent;
683};
684
685static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800686 u32 function, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +0200687{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800688 struct kvm_cpuid_entry2 *entry;
689
690 if (array->nent >= array->maxnent)
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800691 return NULL;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800692
693 entry = &array->entries[array->nent++];
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800694
Avi Kivity00b27a32011-11-23 16:30:32 +0200695 entry->function = function;
696 entry->index = index;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200697 entry->flags = 0;
698
Avi Kivity00b27a32011-11-23 16:30:32 +0200699 cpuid_count(entry->function, entry->index,
700 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200701
702 switch (function) {
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200703 case 4:
704 case 7:
705 case 0xb:
706 case 0xd:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700707 case 0xf:
708 case 0x10:
709 case 0x12:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200710 case 0x14:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700711 case 0x17:
712 case 0x18:
Jing Liu690a7572022-01-05 04:35:27 -0800713 case 0x1d:
714 case 0x1e:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700715 case 0x1f:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200716 case 0x8000001d:
717 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
718 break;
719 }
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800720
721 return entry;
Avi Kivity00b27a32011-11-23 16:30:32 +0200722}
723
Sean Christophersone53c95e2020-03-02 15:56:19 -0800724static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200725{
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800726 struct kvm_cpuid_entry2 *entry;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800727
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800728 if (array->nent >= array->maxnent)
729 return -E2BIG;
730
731 entry = &array->entries[array->nent];
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200732 entry->function = func;
733 entry->index = 0;
734 entry->flags = 0;
735
Borislav Petkov84cffe42013-10-29 12:54:56 +0100736 switch (func) {
737 case 0:
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200738 entry->eax = 7;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800739 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100740 break;
741 case 1:
742 entry->ecx = F(MOVBE);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800743 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100744 break;
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200745 case 7:
746 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200747 entry->eax = 0;
Sean Christopherson85d00112021-05-04 10:17:21 -0700748 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
749 entry->ecx = F(RDPID);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800750 ++array->nent;
Gustavo A. R. Silva551912d2021-05-28 15:07:56 -0500751 break;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100752 default:
753 break;
754 }
755
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200756 return 0;
757}
758
Sean Christophersone53c95e2020-03-02 15:56:19 -0800759static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200760{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800761 struct kvm_cpuid_entry2 *entry;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800762 int r, i, max_idx;
Avi Kivity00b27a32011-11-23 16:30:32 +0200763
Avi Kivity00b27a32011-11-23 16:30:32 +0200764 /* all calls to cpuid_count() should be made on the same cpu */
765 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200766
767 r = -E2BIG;
768
Sean Christophersone53c95e2020-03-02 15:56:19 -0800769 entry = do_host_cpuid(array, function, 0);
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800770 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200771 goto out;
772
Avi Kivity00b27a32011-11-23 16:30:32 +0200773 switch (function) {
774 case 0:
Like Xua87f2d32019-06-06 09:18:45 +0800775 /* Limited to the highest leaf implemented in KVM. */
776 entry->eax = min(entry->eax, 0x1fU);
Avi Kivity00b27a32011-11-23 16:30:32 +0200777 break;
778 case 1:
Sean Christophersonbd791992020-03-02 15:56:53 -0800779 cpuid_entry_override(entry, CPUID_1_EDX);
780 cpuid_entry_override(entry, CPUID_1_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200781 break;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800782 case 2:
Sean Christophersonc571a142020-03-02 15:56:50 -0800783 /*
784 * On ancient CPUs, function 2 entries are STATEFUL. That is,
785 * CPUID(function=2, index=0) may return different results each
786 * time, with the least-significant byte in EAX enumerating the
787 * number of times software should do CPUID(2, 0).
788 *
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800789 * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
790 * idiotic. Intel's SDM states that EAX & 0xff "will always
791 * return 01H. Software should ignore this value and not
Sean Christophersonc571a142020-03-02 15:56:50 -0800792 * interpret it as an informational descriptor", while AMD's
793 * APM states that CPUID(2) is reserved.
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800794 *
795 * WARN if a frankenstein CPU that supports virtualization and
796 * a stateful CPUID.0x2 is encountered.
Sean Christophersonc571a142020-03-02 15:56:50 -0800797 */
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800798 WARN_ON_ONCE((entry->eax & 0xff) > 1);
Avi Kivity00b27a32011-11-23 16:30:32 +0200799 break;
Jim Mattson32a243d2019-03-27 13:15:36 -0700800 /* functions 4 and 0x8000001d have additional index. */
801 case 4:
Sean Christophersonc8629032020-03-02 15:56:18 -0800802 case 0x8000001d:
803 /*
804 * Read entries until the cache type in the previous entry is
805 * zero, i.e. indicates an invalid entry.
806 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800807 for (i = 1; entry->eax & 0x1f; ++i) {
808 entry = do_host_cpuid(array, function, i);
809 if (!entry)
Sean Christopherson0fc62672020-03-02 15:56:08 -0800810 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200811 }
812 break;
Jan Kiszkae453aa02015-05-24 17:22:38 +0200813 case 6: /* Thermal management */
814 entry->eax = 0x4; /* allow ARAT */
815 entry->ebx = 0;
816 entry->ecx = 0;
817 entry->edx = 0;
818 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200819 /* function 7 has additional index. */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800820 case 7:
Sean Christopherson09f628a2020-03-02 15:56:48 -0800821 entry->eax = min(entry->eax, 1u);
Sean Christophersonbd791992020-03-02 15:56:53 -0800822 cpuid_entry_override(entry, CPUID_7_0_EBX);
823 cpuid_entry_override(entry, CPUID_7_ECX);
824 cpuid_entry_override(entry, CPUID_7_EDX);
Sean Christopherson09f628a2020-03-02 15:56:48 -0800825
Sean Christophersonbcf600c2020-03-02 15:56:49 -0800826 /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
827 if (entry->eax == 1) {
828 entry = do_host_cpuid(array, function, 1);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800829 if (!entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200830 goto out;
831
Sean Christophersonbd791992020-03-02 15:56:53 -0800832 cpuid_entry_override(entry, CPUID_7_1_EAX);
Sean Christopherson09f628a2020-03-02 15:56:48 -0800833 entry->ebx = 0;
834 entry->ecx = 0;
835 entry->edx = 0;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200836 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200837 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200838 case 9:
839 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200840 case 0xa: { /* Architectural Performance Monitoring */
841 struct x86_pmu_capability cap;
842 union cpuid10_eax eax;
843 union cpuid10_edx edx;
844
845 perf_get_x86_pmu_capability(&cap);
846
847 /*
Like Xu4732f242022-01-11 15:38:23 +0800848 * The guest architecture pmu is only supported if the architecture
849 * pmu exists on the host and the module parameters allow it.
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200850 */
Like Xu4732f242022-01-11 15:38:23 +0800851 if (!cap.version || !enable_pmu)
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200852 memset(&cap, 0, sizeof(cap));
853
854 eax.split.version_id = min(cap.version, 2);
855 eax.split.num_counters = cap.num_counters_gp;
856 eax.split.bit_width = cap.bit_width_gp;
857 eax.split.mask_length = cap.events_mask_len;
858
Like Xu2e8cd7a2020-06-24 09:59:28 +0800859 edx.split.num_counters_fixed = min(cap.num_counters_fixed, MAX_FIXED_COUNTERS);
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200860 edx.split.bit_width_fixed = cap.bit_width_fixed;
Like Xu7234c362021-06-28 15:43:54 +0800861 if (cap.version)
862 edx.split.anythread_deprecated = 1;
Stephane Eraniancadbaa02020-10-28 12:42:47 -0700863 edx.split.reserved1 = 0;
864 edx.split.reserved2 = 0;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200865
866 entry->eax = eax.full;
867 entry->ebx = cap.events_mask;
868 entry->ecx = 0;
869 entry->edx = edx.full;
870 break;
871 }
Like Xua87f2d32019-06-06 09:18:45 +0800872 /*
873 * Per Intel's SDM, the 0x1f is a superset of 0xb,
874 * thus they can be handled by common code.
875 */
876 case 0x1f:
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800877 case 0xb:
Jim Mattsona1a640b2019-09-25 11:17:14 -0700878 /*
Sean Christophersone53c95e2020-03-02 15:56:19 -0800879 * Populate entries until the level type (ECX[15:8]) of the
880 * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
881 * the starting entry, filled by the primary do_host_cpuid().
Jim Mattsona1a640b2019-09-25 11:17:14 -0700882 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800883 for (i = 1; entry->ecx & 0xff00; ++i) {
884 entry = do_host_cpuid(array, function, i);
885 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200886 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200887 }
888 break;
Jing Liu445ecdf2022-01-05 04:35:15 -0800889 case 0xd: {
890 u64 guest_perm = xstate_get_guest_group_perm();
891
892 entry->eax &= supported_xcr0 & guest_perm;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800893 entry->ebx = xstate_required_size(supported_xcr0, false);
Radim Krčmáře08e8332014-12-04 18:30:41 +0100894 entry->ecx = entry->ebx;
Jing Liu445ecdf2022-01-05 04:35:15 -0800895 entry->edx &= (supported_xcr0 & guest_perm) >> 32;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800896 if (!supported_xcr0)
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100897 break;
898
Sean Christophersone53c95e2020-03-02 15:56:19 -0800899 entry = do_host_cpuid(array, function, 1);
900 if (!entry)
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800901 goto out;
902
Sean Christophersonbd791992020-03-02 15:56:53 -0800903 cpuid_entry_override(entry, CPUID_D_1_EAX);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800904 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100905 entry->ebx = xstate_required_size(supported_xcr0 | supported_xss,
906 true);
907 else {
908 WARN_ON_ONCE(supported_xss != 0);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800909 entry->ebx = 0;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100910 }
911 entry->ecx &= supported_xss;
912 entry->edx &= supported_xss >> 32;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800913
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800914 for (i = 2; i < 64; ++i) {
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100915 bool s_state;
916 if (supported_xcr0 & BIT_ULL(i))
917 s_state = false;
918 else if (supported_xss & BIT_ULL(i))
919 s_state = true;
920 else
Sean Christopherson1893c942020-03-02 15:56:10 -0800921 continue;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800922
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800923 entry = do_host_cpuid(array, function, i);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800924 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200925 goto out;
926
Sean Christopherson91001d42020-03-02 15:56:11 -0800927 /*
Sean Christophersoncfc48182020-03-02 15:56:23 -0800928 * The supported check above should have filtered out
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100929 * invalid sub-leafs. Only valid sub-leafs should
Sean Christopherson91001d42020-03-02 15:56:11 -0800930 * reach this point, and they should have a non-zero
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100931 * save state size. Furthermore, check whether the
932 * processor agrees with supported_xcr0/supported_xss
933 * on whether this is an XCR0- or IA32_XSS-managed area.
Sean Christopherson91001d42020-03-02 15:56:11 -0800934 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100935 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800936 --array->nent;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800937 continue;
Sean Christopherson8b2fc442020-03-02 15:56:12 -0800938 }
Like Xue9737462022-01-17 15:45:31 +0800939
940 if (!kvm_cpu_cap_has(X86_FEATURE_XFD))
941 entry->ecx &= ~BIT_ULL(2);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800942 entry->edx = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200943 }
944 break;
Jing Liu445ecdf2022-01-05 04:35:15 -0800945 }
Sean Christopherson72add912021-04-12 16:21:42 +1200946 case 0x12:
947 /* Intel SGX */
948 if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) {
949 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
950 break;
951 }
952
953 /*
954 * Index 0: Sub-features, MISCSELECT (a.k.a extended features)
955 * and max enclave sizes. The SGX sub-features and MISCSELECT
956 * are restricted by kernel and KVM capabilities (like most
957 * feature flags), while enclave size is unrestricted.
958 */
959 cpuid_entry_override(entry, CPUID_12_EAX);
960 entry->ebx &= SGX_MISC_EXINFO;
961
962 entry = do_host_cpuid(array, function, 1);
963 if (!entry)
964 goto out;
965
966 /*
967 * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la
968 * feature flags. Advertise all supported flags, including
969 * privileged attributes that require explicit opt-in from
970 * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is
971 * expected to derive it from supported XCR0.
972 */
973 entry->eax &= SGX_ATTR_DEBUG | SGX_ATTR_MODE64BIT |
Sean Christophersonfe7e9482021-04-12 16:21:43 +1200974 SGX_ATTR_PROVISIONKEY | SGX_ATTR_EINITTOKENKEY |
Sean Christopherson72add912021-04-12 16:21:42 +1200975 SGX_ATTR_KSS;
976 entry->ebx &= 0;
977 break;
Chao Peng86f52012018-10-24 16:05:11 +0800978 /* Intel PT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800979 case 0x14:
Sean Christophersondd69cc22020-03-02 15:56:55 -0800980 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
Sean Christopherson73920792020-03-02 15:56:26 -0800981 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Chao Peng86f52012018-10-24 16:05:11 +0800982 break;
Sean Christopherson73920792020-03-02 15:56:26 -0800983 }
Chao Peng86f52012018-10-24 16:05:11 +0800984
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800985 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800986 if (!do_host_cpuid(array, function, i))
Chao Peng86f52012018-10-24 16:05:11 +0800987 goto out;
Chao Peng86f52012018-10-24 16:05:11 +0800988 }
989 break;
Jing Liu690a7572022-01-05 04:35:27 -0800990 /* Intel AMX TILE */
991 case 0x1d:
992 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
993 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
994 break;
995 }
996
997 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
998 if (!do_host_cpuid(array, function, i))
999 goto out;
1000 }
1001 break;
1002 case 0x1e: /* TMUL information */
1003 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
1004 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1005 break;
1006 }
1007 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001008 case KVM_CPUID_SIGNATURE: {
Paul Durrant760849b2021-11-05 09:51:01 +00001009 const u32 *sigptr = (const u32 *)KVM_SIGNATURE;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +03001010 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +02001011 entry->ebx = sigptr[0];
1012 entry->ecx = sigptr[1];
1013 entry->edx = sigptr[2];
1014 break;
1015 }
1016 case KVM_CPUID_FEATURES:
1017 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
1018 (1 << KVM_FEATURE_NOP_IO_DELAY) |
1019 (1 << KVM_FEATURE_CLOCKSOURCE2) |
1020 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001021 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05301022 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
Wanpeng Lif38a7b72017-12-12 17:33:04 -08001023 (1 << KVM_FEATURE_PV_UNHALT) |
Radim Krčmářfe2a3022018-02-01 22:16:21 +01001024 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
Wanpeng Li4180bf12018-07-23 14:39:54 +08001025 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001026 (1 << KVM_FEATURE_PV_SEND_IPI) |
Wanpeng Li32b72ec2019-06-11 20:23:50 +08001027 (1 << KVM_FEATURE_POLL_CONTROL) |
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +02001028 (1 << KVM_FEATURE_PV_SCHED_YIELD) |
1029 (1 << KVM_FEATURE_ASYNC_PF_INT);
Avi Kivity00b27a32011-11-23 16:30:32 +02001030
1031 if (sched_info_on())
1032 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
1033
1034 entry->ebx = 0;
1035 entry->ecx = 0;
1036 entry->edx = 0;
1037 break;
1038 case 0x80000000:
Brijesh Singh8765d752017-12-04 10:57:25 -06001039 entry->eax = min(entry->eax, 0x8000001f);
Avi Kivity00b27a32011-11-23 16:30:32 +02001040 break;
1041 case 0x80000001:
Sean Christophersonbd791992020-03-02 15:56:53 -08001042 cpuid_entry_override(entry, CPUID_8000_0001_EDX);
1043 cpuid_entry_override(entry, CPUID_8000_0001_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +02001044 break;
Eric Northup43d05de2020-04-14 18:23:20 -07001045 case 0x80000006:
1046 /* L2 cache and TLB: pass through host info. */
1047 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -03001048 case 0x80000007: /* Advanced power management */
1049 /* invariant TSC is CPUID.80000007H:EDX[8] */
1050 entry->edx &= (1 << 8);
1051 /* mask against host */
1052 entry->edx &= boot_cpu_data.x86_power;
1053 entry->eax = entry->ebx = entry->ecx = 0;
1054 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001055 case 0x80000008: {
1056 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
1057 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
1058 unsigned phys_as = entry->eax & 0xff;
1059
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001060 /*
Sean Christophersone39f00f2021-06-23 16:05:47 -07001061 * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as
1062 * the guest operates in the same PA space as the host, i.e.
1063 * reductions in MAXPHYADDR for memory encryption affect shadow
1064 * paging, too.
1065 *
1066 * If TDP is enabled but an explicit guest MAXPHYADDR is not
1067 * provided, use the raw bare metal MAXPHYADDR as reductions to
1068 * the HPAs do not affect GPAs.
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001069 */
Sean Christophersone39f00f2021-06-23 16:05:47 -07001070 if (!tdp_enabled)
1071 g_phys_as = boot_cpu_data.x86_phys_bits;
1072 else if (!g_phys_as)
Avi Kivity00b27a32011-11-23 16:30:32 +02001073 g_phys_as = phys_as;
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001074
Avi Kivity00b27a32011-11-23 16:30:32 +02001075 entry->eax = g_phys_as | (virt_as << 8);
Ashok Raj15d45072018-02-01 22:59:43 +01001076 entry->edx = 0;
Sean Christophersonbd791992020-03-02 15:56:53 -08001077 cpuid_entry_override(entry, CPUID_8000_0008_EBX);
Avi Kivity00b27a32011-11-23 16:30:32 +02001078 break;
1079 }
Sean Christopherson25703872020-03-02 15:57:09 -08001080 case 0x8000000A:
1081 if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
1082 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1083 break;
1084 }
1085 entry->eax = 1; /* SVM revision 1 */
1086 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
1087 ASID emulation to nested SVM */
1088 entry->ecx = 0; /* Reserved */
1089 cpuid_entry_override(entry, CPUID_8000_000A_EDX);
1090 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001091 case 0x80000019:
1092 entry->ecx = entry->edx = 0;
1093 break;
1094 case 0x8000001a:
Jim Mattson382409b2019-03-27 13:15:37 -07001095 case 0x8000001e:
Avi Kivity00b27a32011-11-23 16:30:32 +02001096 break;
Peter Gondac1de0f22019-11-21 12:33:43 -08001097 case 0x8000001F:
Sean Christophersone39f00f2021-06-23 16:05:47 -07001098 if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) {
Peter Gondac1de0f22019-11-21 12:33:43 -08001099 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Sean Christophersone39f00f2021-06-23 16:05:47 -07001100 } else {
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07001101 cpuid_entry_override(entry, CPUID_8000_001F_EAX);
Sean Christophersone39f00f2021-06-23 16:05:47 -07001102
1103 /*
1104 * Enumerate '0' for "PA bits reduction", the adjusted
1105 * MAXPHYADDR is enumerated directly (see 0x80000008).
1106 */
1107 entry->ebx &= ~GENMASK(11, 6);
1108 }
Peter Gondac1de0f22019-11-21 12:33:43 -08001109 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001110 /*Add support for Centaur's CPUID instruction*/
1111 case 0xC0000000:
1112 /*Just support up to 0xC0000004 now*/
1113 entry->eax = min(entry->eax, 0xC0000004);
1114 break;
1115 case 0xC0000001:
Sean Christophersonbd791992020-03-02 15:56:53 -08001116 cpuid_entry_override(entry, CPUID_C000_0001_EDX);
Avi Kivity00b27a32011-11-23 16:30:32 +02001117 break;
1118 case 3: /* Processor serial number */
1119 case 5: /* MONITOR/MWAIT */
Avi Kivity00b27a32011-11-23 16:30:32 +02001120 case 0xC0000002:
1121 case 0xC0000003:
1122 case 0xC0000004:
1123 default:
1124 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1125 break;
1126 }
1127
Sasha Levin831bf662011-11-28 11:20:29 +02001128 r = 0;
1129
1130out:
Avi Kivity00b27a32011-11-23 16:30:32 +02001131 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +02001132
1133 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +02001134}
1135
Sean Christophersone53c95e2020-03-02 15:56:19 -08001136static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1137 unsigned int type)
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001138{
1139 if (type == KVM_GET_EMULATED_CPUID)
Sean Christophersone53c95e2020-03-02 15:56:19 -08001140 return __do_cpuid_func_emulated(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001141
Sean Christophersone53c95e2020-03-02 15:56:19 -08001142 return __do_cpuid_func(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001143}
1144
Sean Christopherson8b860792020-03-02 15:56:06 -08001145#define CENTAUR_CPUID_SIGNATURE 0xC0000000
Sasha Levin831bf662011-11-28 11:20:29 +02001146
Sean Christophersone53c95e2020-03-02 15:56:19 -08001147static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1148 unsigned int type)
Sean Christopherson619a17f2020-03-02 15:56:05 -08001149{
1150 u32 limit;
1151 int r;
1152
Sean Christopherson8b860792020-03-02 15:56:06 -08001153 if (func == CENTAUR_CPUID_SIGNATURE &&
1154 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
1155 return 0;
1156
Sean Christophersone53c95e2020-03-02 15:56:19 -08001157 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -08001158 if (r)
1159 return r;
1160
Sean Christophersone53c95e2020-03-02 15:56:19 -08001161 limit = array->entries[array->nent - 1].eax;
Sean Christopherson619a17f2020-03-02 15:56:05 -08001162 for (func = func + 1; func <= limit; ++func) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001163 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -08001164 if (r)
1165 break;
1166 }
1167
1168 return r;
1169}
1170
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001171static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
1172 __u32 num_entries, unsigned int ioctl_type)
1173{
1174 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +01001175 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001176
1177 if (ioctl_type != KVM_GET_EMULATED_CPUID)
1178 return false;
1179
1180 /*
1181 * We want to make sure that ->padding is being passed clean from
1182 * userspace in case we want to use it for something in the future.
1183 *
1184 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
1185 * have to give ourselves satisfied only with the emulated side. /me
1186 * sheds a tear.
1187 */
1188 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +01001189 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
1190 return true;
1191
1192 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001193 return true;
1194 }
1195 return false;
1196}
1197
1198int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
1199 struct kvm_cpuid_entry2 __user *entries,
1200 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +02001201{
Sean Christopherson8b860792020-03-02 15:56:06 -08001202 static const u32 funcs[] = {
1203 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
Sasha Levin831bf662011-11-28 11:20:29 +02001204 };
Avi Kivity00b27a32011-11-23 16:30:32 +02001205
Sean Christophersone53c95e2020-03-02 15:56:19 -08001206 struct kvm_cpuid_array array = {
1207 .nent = 0,
Sean Christophersone53c95e2020-03-02 15:56:19 -08001208 };
1209 int r, i;
Sean Christophersond5a661d2020-03-02 15:56:07 -08001210
Avi Kivity00b27a32011-11-23 16:30:32 +02001211 if (cpuid->nent < 1)
Sean Christophersond5a661d2020-03-02 15:56:07 -08001212 return -E2BIG;
Avi Kivity00b27a32011-11-23 16:30:32 +02001213 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1214 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001215
1216 if (sanity_check_entries(entries, cpuid->nent, type))
1217 return -EINVAL;
1218
Sean Christophersone53c95e2020-03-02 15:56:19 -08001219 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
Kees Cookfad953c2018-06-12 14:27:37 -07001220 cpuid->nent));
Sean Christophersone53c95e2020-03-02 15:56:19 -08001221 if (!array.entries)
Sean Christophersond5a661d2020-03-02 15:56:07 -08001222 return -ENOMEM;
Avi Kivity00b27a32011-11-23 16:30:32 +02001223
Xiaoyao Li65b18912020-06-04 12:16:36 +08001224 array.maxnent = cpuid->nent;
1225
Sean Christopherson8b860792020-03-02 15:56:06 -08001226 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001227 r = get_cpuid_func(&array, funcs[i], type);
Sasha Levin831bf662011-11-28 11:20:29 +02001228 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +02001229 goto out_free;
1230 }
Sean Christophersone53c95e2020-03-02 15:56:19 -08001231 cpuid->nent = array.nent;
Avi Kivity00b27a32011-11-23 16:30:32 +02001232
Sean Christophersone53c95e2020-03-02 15:56:19 -08001233 if (copy_to_user(entries, array.entries,
1234 array.nent * sizeof(struct kvm_cpuid_entry2)))
Sean Christophersond5a661d2020-03-02 15:56:07 -08001235 r = -EFAULT;
Avi Kivity00b27a32011-11-23 16:30:32 +02001236
1237out_free:
Sean Christophersone53c95e2020-03-02 15:56:19 -08001238 vfree(array.entries);
Avi Kivity00b27a32011-11-23 16:30:32 +02001239 return r;
1240}
1241
Avi Kivity00b27a32011-11-23 16:30:32 +02001242struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
1243 u32 function, u32 index)
1244{
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +02001245 return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
1246 function, index);
Avi Kivity00b27a32011-11-23 16:30:32 +02001247}
1248EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
1249
Avi Kivity00b27a32011-11-23 16:30:32 +02001250/*
Sean Christopherson8d892312020-03-04 17:34:34 -08001251 * Intel CPUID semantics treats any query for an out-of-range leaf as if the
1252 * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
1253 * returns all zeroes for any undefined leaf, whether or not the leaf is in
1254 * range. Centaur/VIA follows Intel semantics.
1255 *
1256 * A leaf is considered out-of-range if its function is higher than the maximum
1257 * supported leaf of its associated class or if its associated class does not
1258 * exist.
1259 *
1260 * There are three primary classes to be considered, with their respective
1261 * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
1262 * class exists if a guest CPUID entry for its <base> leaf exists. For a given
1263 * class, CPUID.<base>.EAX contains the max supported leaf for the class.
1264 *
1265 * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
1266 * - Hypervisor: 0x40000000 - 0x4fffffff
1267 * - Extended: 0x80000000 - 0xbfffffff
1268 * - Centaur: 0xc0000000 - 0xcfffffff
1269 *
1270 * The Hypervisor class is further subdivided into sub-classes that each act as
Ingo Molnard9f6e122021-03-18 15:28:01 +01001271 * their own independent class associated with a 0x100 byte range. E.g. if Qemu
Sean Christopherson8d892312020-03-04 17:34:34 -08001272 * is advertising support for both HyperV and KVM, the resulting Hypervisor
1273 * CPUID sub-classes are:
1274 *
1275 * - HyperV: 0x40000000 - 0x400000ff
1276 * - KVM: 0x40000100 - 0x400001ff
Avi Kivity00b27a32011-11-23 16:30:32 +02001277 */
Sean Christopherson09c74312020-03-04 17:34:36 -08001278static struct kvm_cpuid_entry2 *
1279get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +02001280{
Sean Christopherson8d892312020-03-04 17:34:34 -08001281 struct kvm_cpuid_entry2 *basic, *class;
Sean Christopherson09c74312020-03-04 17:34:36 -08001282 u32 function = *fn_ptr;
Avi Kivity00b27a32011-11-23 16:30:32 +02001283
Sean Christopherson8d892312020-03-04 17:34:34 -08001284 basic = kvm_find_cpuid_entry(vcpu, 0, 0);
1285 if (!basic)
Sean Christopherson09c74312020-03-04 17:34:36 -08001286 return NULL;
1287
1288 if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
1289 is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
1290 return NULL;
Sean Christopherson8d892312020-03-04 17:34:34 -08001291
1292 if (function >= 0x40000000 && function <= 0x4fffffff)
1293 class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
1294 else if (function >= 0xc0000000)
1295 class = kvm_find_cpuid_entry(vcpu, 0xc0000000, 0);
1296 else
1297 class = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
1298
Sean Christopherson09c74312020-03-04 17:34:36 -08001299 if (class && function <= class->eax)
1300 return NULL;
1301
1302 /*
1303 * Leaf specific adjustments are also applied when redirecting to the
1304 * max basic entry, e.g. if the max basic leaf is 0xb but there is no
1305 * entry for CPUID.0xb.index (see below), then the output value for EDX
1306 * needs to be pulled from CPUID.0xb.1.
1307 */
1308 *fn_ptr = basic->eax;
1309
1310 /*
1311 * The class does not exist or the requested function is out of range;
1312 * the effective CPUID entry is the max basic leaf. Note, the index of
1313 * the original requested leaf is observed!
1314 */
1315 return kvm_find_cpuid_entry(vcpu, basic->eax, index);
Avi Kivity00b27a32011-11-23 16:30:32 +02001316}
1317
Yu Zhange911eb32017-08-24 20:27:52 +08001318bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
Sean Christophersonf91af512020-03-04 17:34:37 -08001319 u32 *ecx, u32 *edx, bool exact_only)
Avi Kivity00b27a32011-11-23 16:30:32 +02001320{
Jan Kiszkab7fb8482020-03-04 17:34:31 -08001321 u32 orig_function = *eax, function = *eax, index = *ecx;
Jim Mattson43561122019-09-25 17:04:17 -07001322 struct kvm_cpuid_entry2 *entry;
Sean Christopherson2b110b62020-03-17 12:53:54 -07001323 bool exact, used_max_basic = false;
Avi Kivity00b27a32011-11-23 16:30:32 +02001324
Jim Mattson43561122019-09-25 17:04:17 -07001325 entry = kvm_find_cpuid_entry(vcpu, function, index);
Sean Christophersonf91af512020-03-04 17:34:37 -08001326 exact = !!entry;
Sean Christopherson09c74312020-03-04 17:34:36 -08001327
Sean Christopherson2b110b62020-03-17 12:53:54 -07001328 if (!entry && !exact_only) {
Sean Christopherson09c74312020-03-04 17:34:36 -08001329 entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
Sean Christopherson2b110b62020-03-17 12:53:54 -07001330 used_max_basic = !!entry;
1331 }
Sean Christopherson09c74312020-03-04 17:34:36 -08001332
Jim Mattson43561122019-09-25 17:04:17 -07001333 if (entry) {
1334 *eax = entry->eax;
1335 *ebx = entry->ebx;
1336 *ecx = entry->ecx;
1337 *edx = entry->edx;
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001338 if (function == 7 && index == 0) {
1339 u64 data;
1340 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1341 (data & TSX_CTRL_CPUID_CLEAR))
1342 *ebx &= ~(F(RTM) | F(HLE));
1343 }
Jim Mattson43561122019-09-25 17:04:17 -07001344 } else {
Avi Kivity62046e52012-06-07 14:07:48 +03001345 *eax = *ebx = *ecx = *edx = 0;
Jim Mattson43561122019-09-25 17:04:17 -07001346 /*
1347 * When leaf 0BH or 1FH is defined, CL is pass-through
1348 * and EDX is always the x2APIC ID, even for undefined
1349 * subleaves. Index 1 will exist iff the leaf is
1350 * implemented, so we pass through CL iff leaf 1
1351 * exists. EDX can be copied from any existing index.
1352 */
1353 if (function == 0xb || function == 0x1f) {
1354 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1355 if (entry) {
1356 *ecx = index & 0xff;
1357 *edx = entry->edx;
1358 }
1359 }
1360 }
Sean Christopherson2b110b62020-03-17 12:53:54 -07001361 trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
1362 used_max_basic);
Sean Christophersonf91af512020-03-04 17:34:37 -08001363 return exact;
Avi Kivity62046e52012-06-07 14:07:48 +03001364}
Julian Stecklina66f7b722012-12-05 15:26:19 +01001365EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +03001366
Kyle Huey6a908b62016-11-29 12:40:37 -08001367int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity62046e52012-06-07 14:07:48 +03001368{
Jiang Biao1e131752016-11-07 08:55:49 +08001369 u32 eax, ebx, ecx, edx;
Avi Kivity62046e52012-06-07 14:07:48 +03001370
Kyle Hueydb2336a2017-03-20 01:16:28 -07001371 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1372 return 1;
1373
Sean Christophersonde3cd112019-04-30 10:36:17 -07001374 eax = kvm_rax_read(vcpu);
1375 ecx = kvm_rcx_read(vcpu);
Sean Christophersonf91af512020-03-04 17:34:37 -08001376 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001377 kvm_rax_write(vcpu, eax);
1378 kvm_rbx_write(vcpu, ebx);
1379 kvm_rcx_write(vcpu, ecx);
1380 kvm_rdx_write(vcpu, edx);
Kyle Huey6affcbe2016-11-29 12:40:40 -08001381 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +02001382}
1383EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);