blob: 494d4d3518597f28676ad2cf36f2737234e01572 [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 ||
Vitaly Kuznetsov033a3ea2022-01-26 14:18:04 +0100136 e2[i].flags != orig->flags ||
Vitaly Kuznetsovc6617c62022-01-17 16:05:40 +0100137 e2[i].eax != orig->eax || e2[i].ebx != orig->ebx ||
138 e2[i].ecx != orig->ecx || e2[i].edx != orig->edx)
139 return -EINVAL;
140 }
141
142 return 0;
143}
144
Paul Durrant760849b2021-11-05 09:51:01 +0000145static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
146{
147 u32 function;
148 struct kvm_cpuid_entry2 *entry;
149
150 vcpu->arch.kvm_cpuid_base = 0;
151
152 for_each_possible_hypervisor_cpuid_base(function) {
153 entry = kvm_find_cpuid_entry(vcpu, function, 0);
154
155 if (entry) {
156 u32 signature[3];
157
158 signature[0] = entry->ebx;
159 signature[1] = entry->ecx;
160 signature[2] = entry->edx;
161
162 BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE));
163 if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) {
164 vcpu->arch.kvm_cpuid_base = function;
165 break;
166 }
167 }
168 }
169}
170
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100171static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu,
172 struct kvm_cpuid_entry2 *entries, int nent)
Paul Durrant760849b2021-11-05 09:51:01 +0000173{
174 u32 base = vcpu->arch.kvm_cpuid_base;
175
176 if (!base)
177 return NULL;
178
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100179 return cpuid_entry2_find(entries, nent, base | KVM_CPUID_FEATURES, 0);
180}
181
182static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu)
183{
184 return __kvm_find_kvm_cpuid_features(vcpu, vcpu->arch.cpuid_entries,
185 vcpu->arch.cpuid_nent);
Paul Durrant760849b2021-11-05 09:51:01 +0000186}
187
Oliver Upton01b4f512020-10-27 16:10:42 -0700188void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
189{
Paul Durrant760849b2021-11-05 09:51:01 +0000190 struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu);
Oliver Upton01b4f512020-10-27 16:10:42 -0700191
192 /*
193 * save the feature bitmap to avoid cpuid lookup for every PV
194 * operation
195 */
196 if (best)
197 vcpu->arch.pv_cpuid.features = best->eax;
198}
199
Vitaly Kuznetsov5c89be12022-01-24 11:36:05 +0100200/*
201 * Calculate guest's supported XCR0 taking into account guest CPUID data and
202 * supported_xcr0 (comprised of host configuration and KVM_SUPPORTED_XCR0).
203 */
204static u64 cpuid_get_supported_xcr0(struct kvm_cpuid_entry2 *entries, int nent)
205{
206 struct kvm_cpuid_entry2 *best;
207
208 best = cpuid_entry2_find(entries, nent, 0xd, 0);
209 if (!best)
210 return 0;
211
212 return (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
213}
214
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100215static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries,
216 int nent)
Avi Kivity00b27a32011-11-23 16:30:32 +0200217{
218 struct kvm_cpuid_entry2 *best;
Vitaly Kuznetsov5c89be12022-01-24 11:36:05 +0100219 u64 guest_supported_xcr0 = cpuid_get_supported_xcr0(entries, nent);
Avi Kivity00b27a32011-11-23 16:30:32 +0200220
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100221 best = cpuid_entry2_find(entries, nent, 1, 0);
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800222 if (best) {
223 /* Update OSXSAVE bit */
224 if (boot_cpu_has(X86_FEATURE_XSAVE))
225 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
Sean Christophersonb32666b2020-03-02 15:56:31 -0800226 kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
Avi Kivity00b27a32011-11-23 16:30:32 +0200227
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800228 cpuid_entry_change(best, X86_FEATURE_APIC,
Sean Christophersonb32666b2020-03-02 15:56:31 -0800229 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800230 }
Jim Mattsonc7dd15b2016-11-09 09:50:11 -0800231
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100232 best = cpuid_entry2_find(entries, nent, 7, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800233 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
234 cpuid_entry_change(best, X86_FEATURE_OSPKE,
235 kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
Huaitong Hanb9baba82016-03-22 16:51:21 +0800236
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100237 best = cpuid_entry2_find(entries, nent, 0xD, 0);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800238 if (best)
Xiaoyao Lia71936ab2020-04-29 23:43:12 +0800239 best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
Paolo Bonzinid7876f12013-10-02 16:06:15 +0200240
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100241 best = cpuid_entry2_find(entries, nent, 0xD, 1);
Sean Christopherson4c615342020-03-02 15:56:30 -0800242 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
243 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
Paolo Bonzini412a3c42014-12-03 14:38:01 +0100244 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
245
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100246 best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent);
Wanpeng Licaa057a2018-03-12 04:53:03 -0700247 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
248 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
249 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
250
Wanpeng Li511a85562019-05-21 14:06:54 +0800251 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100252 best = cpuid_entry2_find(entries, nent, 0x1, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800253 if (best)
254 cpuid_entry_change(best, X86_FEATURE_MWAIT,
255 vcpu->arch.ia32_misc_enable_msr &
256 MSR_IA32_MISC_ENABLE_MWAIT);
Wanpeng Li511a85562019-05-21 14:06:54 +0800257 }
Vitaly Kuznetsov5c89be12022-01-24 11:36:05 +0100258
259 /*
260 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1) enumerate
261 * the supported XSAVE Feature Request Mask (XFRM), i.e. the enclave's
262 * requested XCR0 value. The enclave's XFRM must be a subset of XCRO
263 * at the time of EENTER, thus adjust the allowed XFRM by the guest's
264 * supported XCR0. Similar to XCR0 handling, FP and SSE are forced to
265 * '1' even on CPUs that don't support XSAVE.
266 */
267 best = cpuid_entry2_find(entries, nent, 0x12, 0x1);
268 if (best) {
269 best->ecx &= guest_supported_xcr0 & 0xffffffff;
270 best->edx &= guest_supported_xcr0 >> 32;
271 best->ecx |= XFEATURE_MASK_FPSSE;
272 }
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800273}
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100274
275void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
276{
277 __kvm_update_cpuid_runtime(vcpu, vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent);
278}
Jim Mattson2259c172020-10-29 10:06:48 -0700279EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800280
Xiaoyao Li346ce352020-07-09 12:34:24 +0800281static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800282{
283 struct kvm_lapic *apic = vcpu->arch.apic;
284 struct kvm_cpuid_entry2 *best;
285
286 best = kvm_find_cpuid_entry(vcpu, 1, 0);
287 if (best && apic) {
288 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
289 apic->lapic_timer.timer_mode_mask = 3 << 17;
290 else
291 apic->lapic_timer.timer_mode_mask = 1 << 17;
292
293 kvm_apic_set_version(vcpu);
294 }
295
Vitaly Kuznetsov5c89be12022-01-24 11:36:05 +0100296 vcpu->arch.guest_supported_xcr0 =
297 cpuid_get_supported_xcr0(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent);
Sean Christopherson72add912021-04-12 16:21:42 +1200298
Oliver Upton01b4f512020-10-27 16:10:42 -0700299 kvm_update_pv_runtime(vcpu);
300
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300301 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Sean Christophersona8ac8642021-02-03 16:01:15 -0800302 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300303
Wei Huangc6702c92015-06-19 13:44:45 +0200304 kvm_pmu_refresh(vcpu);
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000305 vcpu->arch.cr4_guest_rsvd_bits =
306 __cr4_reserved_bits(guest_cpuid_has, vcpu);
Sean Christophersonc44d9b32020-09-29 21:16:56 -0700307
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +0100308 kvm_hv_set_cpuid(vcpu);
309
Sean Christophersonc44d9b32020-09-29 21:16:56 -0700310 /* Invoke the vendor callback only after the above state is updated. */
Jason Baronb36464772021-01-14 22:27:56 -0500311 static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu);
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800312
313 /*
Sean Christopherson49c6f872021-06-22 10:56:51 -0700314 * Except for the MMU, which needs to do its thing any vendor specific
315 * adjustments to the reserved GPA bits.
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800316 */
Sean Christopherson49c6f872021-06-22 10:56:51 -0700317 kvm_mmu_after_set_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200318}
319
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300320int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
321{
322 struct kvm_cpuid_entry2 *best;
323
324 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
325 if (!best || best->eax < 0x80000008)
326 goto not_found;
327 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
328 if (best)
329 return best->eax & 0xff;
330not_found:
331 return 36;
332}
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300333
Sean Christophersona8ac8642021-02-03 16:01:15 -0800334/*
335 * This "raw" version returns the reserved GPA bits without any adjustments for
336 * encryption technologies that usurp bits. The raw mask should be used if and
337 * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs.
338 */
339u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
340{
341 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
342}
343
Sean Christopherson8b44b172021-11-05 09:51:00 +0000344static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
345 int nent)
346{
Jing Liu5ab2f452022-01-05 04:35:19 -0800347 int r;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000348
Vitaly Kuznetsovee3a5f92022-01-17 16:05:39 +0100349 __kvm_update_cpuid_runtime(vcpu, e2, nent);
350
Vitaly Kuznetsovc6617c62022-01-17 16:05:40 +0100351 /*
352 * KVM does not correctly handle changing guest CPUID after KVM_RUN, as
353 * MAXPHYADDR, GBPAGES support, AMD reserved bit behavior, etc.. aren't
354 * tracked in kvm_mmu_page_role. As a result, KVM may miss guest page
355 * faults due to reusing SPs/SPTEs. In practice no sane VMM mucks with
356 * the core vCPU model on the fly. It would've been better to forbid any
357 * KVM_SET_CPUID{,2} calls after KVM_RUN altogether but unfortunately
358 * some VMMs (e.g. QEMU) reuse vCPU fds for CPU hotplug/unplug and do
359 * KVM_SET_CPUID{,2} again. To support this legacy behavior, check
360 * whether the supplied CPUID data is equal to what's already set.
361 */
Sean Christopherson811f95f2022-01-25 21:04:45 +0000362 if (vcpu->arch.last_vmentry_cpu != -1) {
363 r = kvm_cpuid_check_equal(vcpu, e2, nent);
364 if (r)
365 return r;
366
367 kvfree(e2);
368 return 0;
369 }
Vitaly Kuznetsovc6617c62022-01-17 16:05:40 +0100370
Jing Liu5ab2f452022-01-05 04:35:19 -0800371 r = kvm_check_cpuid(vcpu, e2, nent);
372 if (r)
373 return r;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000374
Jing Liu5ab2f452022-01-05 04:35:19 -0800375 kvfree(vcpu->arch.cpuid_entries);
376 vcpu->arch.cpuid_entries = e2;
377 vcpu->arch.cpuid_nent = nent;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000378
Jing Liu5ab2f452022-01-05 04:35:19 -0800379 kvm_update_kvm_cpuid_base(vcpu);
Jing Liu5ab2f452022-01-05 04:35:19 -0800380 kvm_vcpu_after_set_cpuid(vcpu);
Sean Christopherson8b44b172021-11-05 09:51:00 +0000381
Jing Liu5ab2f452022-01-05 04:35:19 -0800382 return 0;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000383}
384
Avi Kivity00b27a32011-11-23 16:30:32 +0200385/* when an old userspace process fills a new kernel module */
386int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
387 struct kvm_cpuid *cpuid,
388 struct kvm_cpuid_entry __user *entries)
389{
390 int r, i;
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200391 struct kvm_cpuid_entry *e = NULL;
392 struct kvm_cpuid_entry2 *e2 = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200393
Avi Kivity00b27a32011-11-23 16:30:32 +0200394 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200395 return -E2BIG;
396
Paolo Bonzini83676e92016-06-01 14:09:19 +0200397 if (cpuid->nent) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200398 e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent));
399 if (IS_ERR(e))
400 return PTR_ERR(e);
401
402 e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
403 if (!e2) {
404 r = -ENOMEM;
405 goto out_free_cpuid;
Denis Efremov7ec28e22020-06-03 13:11:31 +0300406 }
Paolo Bonzini83676e92016-06-01 14:09:19 +0200407 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200408 for (i = 0; i < cpuid->nent; i++) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200409 e2[i].function = e[i].function;
410 e2[i].eax = e[i].eax;
411 e2[i].ebx = e[i].ebx;
412 e2[i].ecx = e[i].ecx;
413 e2[i].edx = e[i].edx;
414 e2[i].index = 0;
415 e2[i].flags = 0;
416 e2[i].padding[0] = 0;
417 e2[i].padding[1] = 0;
418 e2[i].padding[2] = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200419 }
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200420
Sean Christopherson8b44b172021-11-05 09:51:00 +0000421 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
422 if (r)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200423 kvfree(e2);
Avi Kivity00b27a32011-11-23 16:30:32 +0200424
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200425out_free_cpuid:
426 kvfree(e);
427
Avi Kivity00b27a32011-11-23 16:30:32 +0200428 return r;
429}
430
431int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
432 struct kvm_cpuid2 *cpuid,
433 struct kvm_cpuid_entry2 __user *entries)
434{
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200435 struct kvm_cpuid_entry2 *e2 = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200436 int r;
437
Avi Kivity00b27a32011-11-23 16:30:32 +0200438 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200439 return -E2BIG;
440
441 if (cpuid->nent) {
442 e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent));
443 if (IS_ERR(e2))
444 return PTR_ERR(e2);
Xiaoyao Lia76733a2020-07-09 12:34:22 +0800445 }
446
Sean Christopherson8b44b172021-11-05 09:51:00 +0000447 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
448 if (r)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200449 kvfree(e2);
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200450
Sean Christopherson8b44b172021-11-05 09:51:00 +0000451 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200452}
453
454int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
455 struct kvm_cpuid2 *cpuid,
456 struct kvm_cpuid_entry2 __user *entries)
457{
458 int r;
459
460 r = -E2BIG;
461 if (cpuid->nent < vcpu->arch.cpuid_nent)
462 goto out;
463 r = -EFAULT;
Michael Roth181f4942021-01-27 20:44:51 -0600464 if (copy_to_user(entries, vcpu->arch.cpuid_entries,
Avi Kivity00b27a32011-11-23 16:30:32 +0200465 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
466 goto out;
467 return 0;
468
469out:
470 cpuid->nent = vcpu->arch.cpuid_nent;
471 return r;
472}
473
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200474/* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */
Sean Christopherson462f8dd2021-04-20 18:08:50 -0700475static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf)
Sean Christopherson66a69502020-03-02 15:56:41 -0800476{
Sean Christophersond8577a42020-03-02 15:56:52 -0800477 const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
478 struct kvm_cpuid_entry2 entry;
479
Sean Christopherson66a69502020-03-02 15:56:41 -0800480 reverse_cpuid_check(leaf);
Sean Christophersond8577a42020-03-02 15:56:52 -0800481
482 cpuid_count(cpuid.function, cpuid.index,
483 &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
484
Sean Christopherson855c7e92020-03-25 12:12:59 -0700485 kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
Sean Christopherson66a69502020-03-02 15:56:41 -0800486}
487
Sean Christopherson462f8dd2021-04-20 18:08:50 -0700488static __always_inline
489void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask)
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200490{
491 /* Use kvm_cpu_cap_mask for non-scattered leafs. */
492 BUILD_BUG_ON(leaf < NCAPINTS);
493
494 kvm_cpu_caps[leaf] = mask;
495
496 __kvm_cpu_cap_mask(leaf);
497}
498
499static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
500{
501 /* Use kvm_cpu_cap_init_scattered for scattered leafs. */
502 BUILD_BUG_ON(leaf >= NCAPINTS);
503
504 kvm_cpu_caps[leaf] &= mask;
505
506 __kvm_cpu_cap_mask(leaf);
507}
508
Sean Christopherson66a69502020-03-02 15:56:41 -0800509void kvm_set_cpu_caps(void)
510{
Sean Christopherson66a69502020-03-02 15:56:41 -0800511#ifdef CONFIG_X86_64
512 unsigned int f_gbpages = F(GBPAGES);
513 unsigned int f_lm = F(LM);
Jing Liu690a7572022-01-05 04:35:27 -0800514 unsigned int f_xfd = F(XFD);
Sean Christopherson66a69502020-03-02 15:56:41 -0800515#else
516 unsigned int f_gbpages = 0;
517 unsigned int f_lm = 0;
Jing Liu690a7572022-01-05 04:35:27 -0800518 unsigned int f_xfd = 0;
Sean Christopherson66a69502020-03-02 15:56:41 -0800519#endif
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200520 memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
Sean Christopherson66a69502020-03-02 15:56:41 -0800521
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200522 BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
Sean Christopherson66a69502020-03-02 15:56:41 -0800523 sizeof(boot_cpu_data.x86_capability));
524
525 memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200526 sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)));
Sean Christopherson66a69502020-03-02 15:56:41 -0800527
528 kvm_cpu_cap_mask(CPUID_1_ECX,
529 /*
530 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
531 * advertised to guests via CPUID!
532 */
533 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
534 0 /* DS-CPL, VMX, SMX, EST */ |
535 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
Like Xu27461da32020-05-29 15:43:45 +0800536 F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800537 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
538 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
539 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
540 F(F16C) | F(RDRAND)
541 );
Sean Christopherson93c380e2020-03-02 15:56:54 -0800542 /* KVM emulates x2apic in software irrespective of host support. */
543 kvm_cpu_cap_set(X86_FEATURE_X2APIC);
Sean Christopherson66a69502020-03-02 15:56:41 -0800544
545 kvm_cpu_cap_mask(CPUID_1_EDX,
546 F(FPU) | F(VME) | F(DE) | F(PSE) |
547 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
548 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
549 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
550 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
551 0 /* Reserved, DS, ACPI */ | F(MMX) |
552 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
553 0 /* HTT, TM, Reserved, PBE */
554 );
555
556 kvm_cpu_cap_mask(CPUID_7_0_EBX,
Jim Mattsone3bcfda2022-02-03 16:13:48 -0800557 F(FSGSBASE) | F(SGX) | F(BMI1) | F(HLE) | F(AVX2) |
558 F(FDP_EXCPTN_ONLY) | F(SMEP) | F(BMI2) | F(ERMS) | F(INVPCID) |
559 F(RTM) | F(ZERO_FCS_FDS) | 0 /*MPX*/ | F(AVX512F) |
560 F(AVX512DQ) | F(RDSEED) | F(ADX) | F(SMAP) | F(AVX512IFMA) |
561 F(CLFLUSHOPT) | F(CLWB) | 0 /*INTEL_PT*/ | F(AVX512PF) |
562 F(AVX512ER) | F(AVX512CD) | F(SHA_NI) | F(AVX512BW) |
563 F(AVX512VL));
Sean Christopherson66a69502020-03-02 15:56:41 -0800564
565 kvm_cpu_cap_mask(CPUID_7_ECX,
Babu Mogerfa44b822020-05-12 18:59:16 -0500566 F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800567 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
568 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
Sean Christopherson72add912021-04-12 16:21:42 +1200569 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
Paolo Bonzini76ea4382021-05-06 06:30:04 -0400570 F(SGX_LC) | F(BUS_LOCK_DETECT)
Sean Christopherson66a69502020-03-02 15:56:41 -0800571 );
572 /* Set LA57 based on hardware capability. */
573 if (cpuid_ecx(7) & F(LA57))
574 kvm_cpu_cap_set(X86_FEATURE_LA57);
575
Babu Mogerfa44b822020-05-12 18:59:16 -0500576 /*
577 * PKU not yet implemented for shadow paging and requires OSPKE
578 * to be set on the host. Clear it if that is not the case
579 */
580 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
581 kvm_cpu_cap_clear(X86_FEATURE_PKU);
582
Sean Christopherson66a69502020-03-02 15:56:41 -0800583 kvm_cpu_cap_mask(CPUID_7_EDX,
584 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
585 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
Paolo Bonzini43bd9ef2020-08-09 13:04:56 -0400586 F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
Jing Liu690a7572022-01-05 04:35:27 -0800587 F(SERIALIZE) | F(TSXLDTRK) | F(AVX512_FP16) |
588 F(AMX_TILE) | F(AMX_INT8) | F(AMX_BF16)
Sean Christopherson66a69502020-03-02 15:56:41 -0800589 );
590
Sean Christopherson93c380e2020-03-02 15:56:54 -0800591 /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
592 kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
593 kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
594
595 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
596 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
597 if (boot_cpu_has(X86_FEATURE_STIBP))
598 kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
599 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
600 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
601
Sean Christopherson66a69502020-03-02 15:56:41 -0800602 kvm_cpu_cap_mask(CPUID_7_1_EAX,
Yang Zhong1085a6b2021-01-05 08:49:09 +0800603 F(AVX_VNNI) | F(AVX512_BF16)
Sean Christopherson66a69502020-03-02 15:56:41 -0800604 );
605
606 kvm_cpu_cap_mask(CPUID_D_1_EAX,
Jing Liu690a7572022-01-05 04:35:27 -0800607 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd
Sean Christopherson66a69502020-03-02 15:56:41 -0800608 );
609
Sean Christopherson72add912021-04-12 16:21:42 +1200610 kvm_cpu_cap_init_scattered(CPUID_12_EAX,
611 SF(SGX1) | SF(SGX2)
612 );
613
Sean Christopherson66a69502020-03-02 15:56:41 -0800614 kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
615 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
616 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
617 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
618 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
Like Xub1d66da2021-11-17 16:03:04 +0800619 F(TOPOEXT) | 0 /* PERFCTR_CORE */
Sean Christopherson66a69502020-03-02 15:56:41 -0800620 );
621
622 kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
623 F(FPU) | F(VME) | F(DE) | F(PSE) |
624 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
625 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
626 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
627 F(PAT) | F(PSE36) | 0 /* Reserved */ |
Sean Christopherson13832792021-08-05 11:38:04 -0700628 F(NX) | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800629 F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
630 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
631 );
632
633 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
634 kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
635
636 kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
637 F(CLZERO) | F(XSAVEERPTR) |
638 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
Babu Mogerb73a5432021-09-23 20:15:28 -0500639 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON) |
640 __feature_bit(KVM_X86_FEATURE_PSFD)
Sean Christopherson66a69502020-03-02 15:56:41 -0800641 );
642
Sean Christopherson9b58b982020-03-02 15:56:42 -0800643 /*
Sean Christopherson93c380e2020-03-02 15:56:54 -0800644 * AMD has separate bits for each SPEC_CTRL bit.
645 * arch/x86/kernel/cpu/bugs.c is kind enough to
646 * record that in cpufeatures so use them.
647 */
648 if (boot_cpu_has(X86_FEATURE_IBPB))
649 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
650 if (boot_cpu_has(X86_FEATURE_IBRS))
651 kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
652 if (boot_cpu_has(X86_FEATURE_STIBP))
653 kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
654 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
655 kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
656 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
657 kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
658 /*
659 * The preference is to use SPEC CTRL MSR instead of the
660 * VIRT_SPEC MSR.
661 */
662 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
663 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
664 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
665
666 /*
Sean Christopherson9b58b982020-03-02 15:56:42 -0800667 * Hide all SVM features by default, SVM will set the cap bits for
668 * features it emulates and/or exposes for L1.
669 */
670 kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
671
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700672 kvm_cpu_cap_mask(CPUID_8000_001F_EAX,
673 0 /* SME */ | F(SEV) | 0 /* VM_PAGE_FLUSH */ | F(SEV_ES) |
674 F(SME_COHERENT));
675
Sean Christopherson66a69502020-03-02 15:56:41 -0800676 kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
677 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
678 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
679 F(PMM) | F(PMM_EN)
680 );
Sean Christopherson78bba962021-05-04 10:17:34 -0700681
682 /*
683 * Hide RDTSCP and RDPID if either feature is reported as supported but
684 * probing MSR_TSC_AUX failed. This is purely a sanity check and
685 * should never happen, but the guest will likely crash if RDTSCP or
686 * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in
687 * the past. For example, the sanity check may fire if this instance of
688 * KVM is running as L1 on top of an older, broken KVM.
689 */
690 if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) ||
691 kvm_cpu_cap_has(X86_FEATURE_RDPID)) &&
692 !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) {
693 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
694 kvm_cpu_cap_clear(X86_FEATURE_RDPID);
695 }
Sean Christopherson66a69502020-03-02 15:56:41 -0800696}
697EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
698
Sean Christophersone53c95e2020-03-02 15:56:19 -0800699struct kvm_cpuid_array {
700 struct kvm_cpuid_entry2 *entries;
Xiaoyao Li65b18912020-06-04 12:16:36 +0800701 int maxnent;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800702 int nent;
703};
704
705static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800706 u32 function, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +0200707{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800708 struct kvm_cpuid_entry2 *entry;
709
710 if (array->nent >= array->maxnent)
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800711 return NULL;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800712
713 entry = &array->entries[array->nent++];
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800714
Avi Kivity00b27a32011-11-23 16:30:32 +0200715 entry->function = function;
716 entry->index = index;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200717 entry->flags = 0;
718
Avi Kivity00b27a32011-11-23 16:30:32 +0200719 cpuid_count(entry->function, entry->index,
720 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200721
722 switch (function) {
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200723 case 4:
724 case 7:
725 case 0xb:
726 case 0xd:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700727 case 0xf:
728 case 0x10:
729 case 0x12:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200730 case 0x14:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700731 case 0x17:
732 case 0x18:
Jing Liu690a7572022-01-05 04:35:27 -0800733 case 0x1d:
734 case 0x1e:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700735 case 0x1f:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200736 case 0x8000001d:
737 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
738 break;
739 }
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800740
741 return entry;
Avi Kivity00b27a32011-11-23 16:30:32 +0200742}
743
Sean Christophersone53c95e2020-03-02 15:56:19 -0800744static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200745{
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800746 struct kvm_cpuid_entry2 *entry;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800747
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800748 if (array->nent >= array->maxnent)
749 return -E2BIG;
750
751 entry = &array->entries[array->nent];
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200752 entry->function = func;
753 entry->index = 0;
754 entry->flags = 0;
755
Borislav Petkov84cffe42013-10-29 12:54:56 +0100756 switch (func) {
757 case 0:
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200758 entry->eax = 7;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800759 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100760 break;
761 case 1:
762 entry->ecx = F(MOVBE);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800763 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100764 break;
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200765 case 7:
766 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200767 entry->eax = 0;
Sean Christopherson85d00112021-05-04 10:17:21 -0700768 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
769 entry->ecx = F(RDPID);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800770 ++array->nent;
Gustavo A. R. Silva551912d2021-05-28 15:07:56 -0500771 break;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100772 default:
773 break;
774 }
775
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200776 return 0;
777}
778
Sean Christophersone53c95e2020-03-02 15:56:19 -0800779static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200780{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800781 struct kvm_cpuid_entry2 *entry;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800782 int r, i, max_idx;
Avi Kivity00b27a32011-11-23 16:30:32 +0200783
Avi Kivity00b27a32011-11-23 16:30:32 +0200784 /* all calls to cpuid_count() should be made on the same cpu */
785 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200786
787 r = -E2BIG;
788
Sean Christophersone53c95e2020-03-02 15:56:19 -0800789 entry = do_host_cpuid(array, function, 0);
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800790 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200791 goto out;
792
Avi Kivity00b27a32011-11-23 16:30:32 +0200793 switch (function) {
794 case 0:
Like Xua87f2d32019-06-06 09:18:45 +0800795 /* Limited to the highest leaf implemented in KVM. */
796 entry->eax = min(entry->eax, 0x1fU);
Avi Kivity00b27a32011-11-23 16:30:32 +0200797 break;
798 case 1:
Sean Christophersonbd791992020-03-02 15:56:53 -0800799 cpuid_entry_override(entry, CPUID_1_EDX);
800 cpuid_entry_override(entry, CPUID_1_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200801 break;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800802 case 2:
Sean Christophersonc571a142020-03-02 15:56:50 -0800803 /*
804 * On ancient CPUs, function 2 entries are STATEFUL. That is,
805 * CPUID(function=2, index=0) may return different results each
806 * time, with the least-significant byte in EAX enumerating the
807 * number of times software should do CPUID(2, 0).
808 *
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800809 * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
810 * idiotic. Intel's SDM states that EAX & 0xff "will always
811 * return 01H. Software should ignore this value and not
Sean Christophersonc571a142020-03-02 15:56:50 -0800812 * interpret it as an informational descriptor", while AMD's
813 * APM states that CPUID(2) is reserved.
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800814 *
815 * WARN if a frankenstein CPU that supports virtualization and
816 * a stateful CPUID.0x2 is encountered.
Sean Christophersonc571a142020-03-02 15:56:50 -0800817 */
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800818 WARN_ON_ONCE((entry->eax & 0xff) > 1);
Avi Kivity00b27a32011-11-23 16:30:32 +0200819 break;
Jim Mattson32a243d2019-03-27 13:15:36 -0700820 /* functions 4 and 0x8000001d have additional index. */
821 case 4:
Sean Christophersonc8629032020-03-02 15:56:18 -0800822 case 0x8000001d:
823 /*
824 * Read entries until the cache type in the previous entry is
825 * zero, i.e. indicates an invalid entry.
826 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800827 for (i = 1; entry->eax & 0x1f; ++i) {
828 entry = do_host_cpuid(array, function, i);
829 if (!entry)
Sean Christopherson0fc62672020-03-02 15:56:08 -0800830 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200831 }
832 break;
Jan Kiszkae453aa02015-05-24 17:22:38 +0200833 case 6: /* Thermal management */
834 entry->eax = 0x4; /* allow ARAT */
835 entry->ebx = 0;
836 entry->ecx = 0;
837 entry->edx = 0;
838 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200839 /* function 7 has additional index. */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800840 case 7:
Sean Christopherson09f628a2020-03-02 15:56:48 -0800841 entry->eax = min(entry->eax, 1u);
Sean Christophersonbd791992020-03-02 15:56:53 -0800842 cpuid_entry_override(entry, CPUID_7_0_EBX);
843 cpuid_entry_override(entry, CPUID_7_ECX);
844 cpuid_entry_override(entry, CPUID_7_EDX);
Sean Christopherson09f628a2020-03-02 15:56:48 -0800845
Sean Christophersonbcf600c2020-03-02 15:56:49 -0800846 /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
847 if (entry->eax == 1) {
848 entry = do_host_cpuid(array, function, 1);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800849 if (!entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200850 goto out;
851
Sean Christophersonbd791992020-03-02 15:56:53 -0800852 cpuid_entry_override(entry, CPUID_7_1_EAX);
Sean Christopherson09f628a2020-03-02 15:56:48 -0800853 entry->ebx = 0;
854 entry->ecx = 0;
855 entry->edx = 0;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200856 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200857 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200858 case 9:
859 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200860 case 0xa: { /* Architectural Performance Monitoring */
861 struct x86_pmu_capability cap;
862 union cpuid10_eax eax;
863 union cpuid10_edx edx;
864
865 perf_get_x86_pmu_capability(&cap);
866
867 /*
Like Xu4732f242022-01-11 15:38:23 +0800868 * The guest architecture pmu is only supported if the architecture
869 * pmu exists on the host and the module parameters allow it.
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200870 */
Like Xu4732f242022-01-11 15:38:23 +0800871 if (!cap.version || !enable_pmu)
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200872 memset(&cap, 0, sizeof(cap));
873
874 eax.split.version_id = min(cap.version, 2);
875 eax.split.num_counters = cap.num_counters_gp;
876 eax.split.bit_width = cap.bit_width_gp;
877 eax.split.mask_length = cap.events_mask_len;
878
Like Xu2e8cd7a2020-06-24 09:59:28 +0800879 edx.split.num_counters_fixed = min(cap.num_counters_fixed, MAX_FIXED_COUNTERS);
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200880 edx.split.bit_width_fixed = cap.bit_width_fixed;
Like Xu7234c362021-06-28 15:43:54 +0800881 if (cap.version)
882 edx.split.anythread_deprecated = 1;
Stephane Eraniancadbaa02020-10-28 12:42:47 -0700883 edx.split.reserved1 = 0;
884 edx.split.reserved2 = 0;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200885
886 entry->eax = eax.full;
887 entry->ebx = cap.events_mask;
888 entry->ecx = 0;
889 entry->edx = edx.full;
890 break;
891 }
Like Xua87f2d32019-06-06 09:18:45 +0800892 /*
893 * Per Intel's SDM, the 0x1f is a superset of 0xb,
894 * thus they can be handled by common code.
895 */
896 case 0x1f:
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800897 case 0xb:
Jim Mattsona1a640b2019-09-25 11:17:14 -0700898 /*
Sean Christophersone53c95e2020-03-02 15:56:19 -0800899 * Populate entries until the level type (ECX[15:8]) of the
900 * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
901 * the starting entry, filled by the primary do_host_cpuid().
Jim Mattsona1a640b2019-09-25 11:17:14 -0700902 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800903 for (i = 1; entry->ecx & 0xff00; ++i) {
904 entry = do_host_cpuid(array, function, i);
905 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200906 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200907 }
908 break;
Jing Liu445ecdf2022-01-05 04:35:15 -0800909 case 0xd: {
Like Xu1ffce092022-01-25 19:52:23 +0800910 u64 permitted_xcr0 = supported_xcr0 & xstate_get_guest_group_perm();
911 u64 permitted_xss = supported_xss;
Jing Liu445ecdf2022-01-05 04:35:15 -0800912
Like Xu1ffce092022-01-25 19:52:23 +0800913 entry->eax &= permitted_xcr0;
914 entry->ebx = xstate_required_size(permitted_xcr0, false);
Radim Krčmáře08e8332014-12-04 18:30:41 +0100915 entry->ecx = entry->ebx;
Like Xu1ffce092022-01-25 19:52:23 +0800916 entry->edx &= permitted_xcr0 >> 32;
917 if (!permitted_xcr0)
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100918 break;
919
Sean Christophersone53c95e2020-03-02 15:56:19 -0800920 entry = do_host_cpuid(array, function, 1);
921 if (!entry)
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800922 goto out;
923
Sean Christophersonbd791992020-03-02 15:56:53 -0800924 cpuid_entry_override(entry, CPUID_D_1_EAX);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800925 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
Like Xu1ffce092022-01-25 19:52:23 +0800926 entry->ebx = xstate_required_size(permitted_xcr0 | permitted_xss,
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100927 true);
928 else {
Like Xu1ffce092022-01-25 19:52:23 +0800929 WARN_ON_ONCE(permitted_xss != 0);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800930 entry->ebx = 0;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100931 }
Like Xu1ffce092022-01-25 19:52:23 +0800932 entry->ecx &= permitted_xss;
933 entry->edx &= permitted_xss >> 32;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800934
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800935 for (i = 2; i < 64; ++i) {
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100936 bool s_state;
Like Xu1ffce092022-01-25 19:52:23 +0800937 if (permitted_xcr0 & BIT_ULL(i))
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100938 s_state = false;
Like Xu1ffce092022-01-25 19:52:23 +0800939 else if (permitted_xss & BIT_ULL(i))
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100940 s_state = true;
941 else
Sean Christopherson1893c942020-03-02 15:56:10 -0800942 continue;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800943
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800944 entry = do_host_cpuid(array, function, i);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800945 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200946 goto out;
947
Sean Christopherson91001d42020-03-02 15:56:11 -0800948 /*
Sean Christophersoncfc48182020-03-02 15:56:23 -0800949 * The supported check above should have filtered out
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100950 * invalid sub-leafs. Only valid sub-leafs should
Sean Christopherson91001d42020-03-02 15:56:11 -0800951 * reach this point, and they should have a non-zero
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100952 * save state size. Furthermore, check whether the
Like Xu1ffce092022-01-25 19:52:23 +0800953 * processor agrees with permitted_xcr0/permitted_xss
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100954 * on whether this is an XCR0- or IA32_XSS-managed area.
Sean Christopherson91001d42020-03-02 15:56:11 -0800955 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100956 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800957 --array->nent;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800958 continue;
Sean Christopherson8b2fc442020-03-02 15:56:12 -0800959 }
Like Xue9737462022-01-17 15:45:31 +0800960
961 if (!kvm_cpu_cap_has(X86_FEATURE_XFD))
962 entry->ecx &= ~BIT_ULL(2);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800963 entry->edx = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200964 }
965 break;
Jing Liu445ecdf2022-01-05 04:35:15 -0800966 }
Sean Christopherson72add912021-04-12 16:21:42 +1200967 case 0x12:
968 /* Intel SGX */
969 if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) {
970 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
971 break;
972 }
973
974 /*
975 * Index 0: Sub-features, MISCSELECT (a.k.a extended features)
976 * and max enclave sizes. The SGX sub-features and MISCSELECT
977 * are restricted by kernel and KVM capabilities (like most
978 * feature flags), while enclave size is unrestricted.
979 */
980 cpuid_entry_override(entry, CPUID_12_EAX);
981 entry->ebx &= SGX_MISC_EXINFO;
982
983 entry = do_host_cpuid(array, function, 1);
984 if (!entry)
985 goto out;
986
987 /*
988 * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la
989 * feature flags. Advertise all supported flags, including
990 * privileged attributes that require explicit opt-in from
991 * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is
992 * expected to derive it from supported XCR0.
993 */
994 entry->eax &= SGX_ATTR_DEBUG | SGX_ATTR_MODE64BIT |
Sean Christophersonfe7e9482021-04-12 16:21:43 +1200995 SGX_ATTR_PROVISIONKEY | SGX_ATTR_EINITTOKENKEY |
Sean Christopherson72add912021-04-12 16:21:42 +1200996 SGX_ATTR_KSS;
997 entry->ebx &= 0;
998 break;
Chao Peng86f52012018-10-24 16:05:11 +0800999 /* Intel PT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -08001000 case 0x14:
Sean Christophersondd69cc22020-03-02 15:56:55 -08001001 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
Sean Christopherson73920792020-03-02 15:56:26 -08001002 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Chao Peng86f52012018-10-24 16:05:11 +08001003 break;
Sean Christopherson73920792020-03-02 15:56:26 -08001004 }
Chao Peng86f52012018-10-24 16:05:11 +08001005
Sean Christopherson74fa0bc2020-03-02 15:56:17 -08001006 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001007 if (!do_host_cpuid(array, function, i))
Chao Peng86f52012018-10-24 16:05:11 +08001008 goto out;
Chao Peng86f52012018-10-24 16:05:11 +08001009 }
1010 break;
Jing Liu690a7572022-01-05 04:35:27 -08001011 /* Intel AMX TILE */
1012 case 0x1d:
1013 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
1014 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1015 break;
1016 }
1017
1018 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
1019 if (!do_host_cpuid(array, function, i))
1020 goto out;
1021 }
1022 break;
1023 case 0x1e: /* TMUL information */
1024 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
1025 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1026 break;
1027 }
1028 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001029 case KVM_CPUID_SIGNATURE: {
Paul Durrant760849b2021-11-05 09:51:01 +00001030 const u32 *sigptr = (const u32 *)KVM_SIGNATURE;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +03001031 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +02001032 entry->ebx = sigptr[0];
1033 entry->ecx = sigptr[1];
1034 entry->edx = sigptr[2];
1035 break;
1036 }
1037 case KVM_CPUID_FEATURES:
1038 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
1039 (1 << KVM_FEATURE_NOP_IO_DELAY) |
1040 (1 << KVM_FEATURE_CLOCKSOURCE2) |
1041 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001042 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05301043 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
Wanpeng Lif38a7b72017-12-12 17:33:04 -08001044 (1 << KVM_FEATURE_PV_UNHALT) |
Radim Krčmářfe2a3022018-02-01 22:16:21 +01001045 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
Wanpeng Li4180bf12018-07-23 14:39:54 +08001046 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001047 (1 << KVM_FEATURE_PV_SEND_IPI) |
Wanpeng Li32b72ec2019-06-11 20:23:50 +08001048 (1 << KVM_FEATURE_POLL_CONTROL) |
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +02001049 (1 << KVM_FEATURE_PV_SCHED_YIELD) |
1050 (1 << KVM_FEATURE_ASYNC_PF_INT);
Avi Kivity00b27a32011-11-23 16:30:32 +02001051
1052 if (sched_info_on())
1053 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
1054
1055 entry->ebx = 0;
1056 entry->ecx = 0;
1057 entry->edx = 0;
1058 break;
1059 case 0x80000000:
Brijesh Singh8765d752017-12-04 10:57:25 -06001060 entry->eax = min(entry->eax, 0x8000001f);
Avi Kivity00b27a32011-11-23 16:30:32 +02001061 break;
1062 case 0x80000001:
Sean Christophersonbd791992020-03-02 15:56:53 -08001063 cpuid_entry_override(entry, CPUID_8000_0001_EDX);
1064 cpuid_entry_override(entry, CPUID_8000_0001_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +02001065 break;
Eric Northup43d05de2020-04-14 18:23:20 -07001066 case 0x80000006:
1067 /* L2 cache and TLB: pass through host info. */
1068 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -03001069 case 0x80000007: /* Advanced power management */
1070 /* invariant TSC is CPUID.80000007H:EDX[8] */
1071 entry->edx &= (1 << 8);
1072 /* mask against host */
1073 entry->edx &= boot_cpu_data.x86_power;
1074 entry->eax = entry->ebx = entry->ecx = 0;
1075 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001076 case 0x80000008: {
1077 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
1078 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
1079 unsigned phys_as = entry->eax & 0xff;
1080
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001081 /*
Sean Christophersone39f00f2021-06-23 16:05:47 -07001082 * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as
1083 * the guest operates in the same PA space as the host, i.e.
1084 * reductions in MAXPHYADDR for memory encryption affect shadow
1085 * paging, too.
1086 *
1087 * If TDP is enabled but an explicit guest MAXPHYADDR is not
1088 * provided, use the raw bare metal MAXPHYADDR as reductions to
1089 * the HPAs do not affect GPAs.
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001090 */
Sean Christophersone39f00f2021-06-23 16:05:47 -07001091 if (!tdp_enabled)
1092 g_phys_as = boot_cpu_data.x86_phys_bits;
1093 else if (!g_phys_as)
Avi Kivity00b27a32011-11-23 16:30:32 +02001094 g_phys_as = phys_as;
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001095
Avi Kivity00b27a32011-11-23 16:30:32 +02001096 entry->eax = g_phys_as | (virt_as << 8);
Ashok Raj15d45072018-02-01 22:59:43 +01001097 entry->edx = 0;
Sean Christophersonbd791992020-03-02 15:56:53 -08001098 cpuid_entry_override(entry, CPUID_8000_0008_EBX);
Avi Kivity00b27a32011-11-23 16:30:32 +02001099 break;
1100 }
Sean Christopherson25703872020-03-02 15:57:09 -08001101 case 0x8000000A:
1102 if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
1103 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1104 break;
1105 }
1106 entry->eax = 1; /* SVM revision 1 */
1107 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
1108 ASID emulation to nested SVM */
1109 entry->ecx = 0; /* Reserved */
1110 cpuid_entry_override(entry, CPUID_8000_000A_EDX);
1111 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001112 case 0x80000019:
1113 entry->ecx = entry->edx = 0;
1114 break;
1115 case 0x8000001a:
Jim Mattson382409b2019-03-27 13:15:37 -07001116 case 0x8000001e:
Avi Kivity00b27a32011-11-23 16:30:32 +02001117 break;
Peter Gondac1de0f22019-11-21 12:33:43 -08001118 case 0x8000001F:
Sean Christophersone39f00f2021-06-23 16:05:47 -07001119 if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) {
Peter Gondac1de0f22019-11-21 12:33:43 -08001120 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Sean Christophersone39f00f2021-06-23 16:05:47 -07001121 } else {
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07001122 cpuid_entry_override(entry, CPUID_8000_001F_EAX);
Sean Christophersone39f00f2021-06-23 16:05:47 -07001123
1124 /*
1125 * Enumerate '0' for "PA bits reduction", the adjusted
1126 * MAXPHYADDR is enumerated directly (see 0x80000008).
1127 */
1128 entry->ebx &= ~GENMASK(11, 6);
1129 }
Peter Gondac1de0f22019-11-21 12:33:43 -08001130 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001131 /*Add support for Centaur's CPUID instruction*/
1132 case 0xC0000000:
1133 /*Just support up to 0xC0000004 now*/
1134 entry->eax = min(entry->eax, 0xC0000004);
1135 break;
1136 case 0xC0000001:
Sean Christophersonbd791992020-03-02 15:56:53 -08001137 cpuid_entry_override(entry, CPUID_C000_0001_EDX);
Avi Kivity00b27a32011-11-23 16:30:32 +02001138 break;
1139 case 3: /* Processor serial number */
1140 case 5: /* MONITOR/MWAIT */
Avi Kivity00b27a32011-11-23 16:30:32 +02001141 case 0xC0000002:
1142 case 0xC0000003:
1143 case 0xC0000004:
1144 default:
1145 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1146 break;
1147 }
1148
Sasha Levin831bf662011-11-28 11:20:29 +02001149 r = 0;
1150
1151out:
Avi Kivity00b27a32011-11-23 16:30:32 +02001152 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +02001153
1154 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +02001155}
1156
Sean Christophersone53c95e2020-03-02 15:56:19 -08001157static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1158 unsigned int type)
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001159{
1160 if (type == KVM_GET_EMULATED_CPUID)
Sean Christophersone53c95e2020-03-02 15:56:19 -08001161 return __do_cpuid_func_emulated(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001162
Sean Christophersone53c95e2020-03-02 15:56:19 -08001163 return __do_cpuid_func(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001164}
1165
Sean Christopherson8b860792020-03-02 15:56:06 -08001166#define CENTAUR_CPUID_SIGNATURE 0xC0000000
Sasha Levin831bf662011-11-28 11:20:29 +02001167
Sean Christophersone53c95e2020-03-02 15:56:19 -08001168static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1169 unsigned int type)
Sean Christopherson619a17f2020-03-02 15:56:05 -08001170{
1171 u32 limit;
1172 int r;
1173
Sean Christopherson8b860792020-03-02 15:56:06 -08001174 if (func == CENTAUR_CPUID_SIGNATURE &&
1175 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
1176 return 0;
1177
Sean Christophersone53c95e2020-03-02 15:56:19 -08001178 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -08001179 if (r)
1180 return r;
1181
Sean Christophersone53c95e2020-03-02 15:56:19 -08001182 limit = array->entries[array->nent - 1].eax;
Sean Christopherson619a17f2020-03-02 15:56:05 -08001183 for (func = func + 1; func <= limit; ++func) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001184 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -08001185 if (r)
1186 break;
1187 }
1188
1189 return r;
1190}
1191
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001192static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
1193 __u32 num_entries, unsigned int ioctl_type)
1194{
1195 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +01001196 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001197
1198 if (ioctl_type != KVM_GET_EMULATED_CPUID)
1199 return false;
1200
1201 /*
1202 * We want to make sure that ->padding is being passed clean from
1203 * userspace in case we want to use it for something in the future.
1204 *
1205 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
1206 * have to give ourselves satisfied only with the emulated side. /me
1207 * sheds a tear.
1208 */
1209 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +01001210 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
1211 return true;
1212
1213 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001214 return true;
1215 }
1216 return false;
1217}
1218
1219int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
1220 struct kvm_cpuid_entry2 __user *entries,
1221 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +02001222{
Sean Christopherson8b860792020-03-02 15:56:06 -08001223 static const u32 funcs[] = {
1224 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
Sasha Levin831bf662011-11-28 11:20:29 +02001225 };
Avi Kivity00b27a32011-11-23 16:30:32 +02001226
Sean Christophersone53c95e2020-03-02 15:56:19 -08001227 struct kvm_cpuid_array array = {
1228 .nent = 0,
Sean Christophersone53c95e2020-03-02 15:56:19 -08001229 };
1230 int r, i;
Sean Christophersond5a661d2020-03-02 15:56:07 -08001231
Avi Kivity00b27a32011-11-23 16:30:32 +02001232 if (cpuid->nent < 1)
Sean Christophersond5a661d2020-03-02 15:56:07 -08001233 return -E2BIG;
Avi Kivity00b27a32011-11-23 16:30:32 +02001234 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1235 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001236
1237 if (sanity_check_entries(entries, cpuid->nent, type))
1238 return -EINVAL;
1239
Sean Christophersone53c95e2020-03-02 15:56:19 -08001240 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
Kees Cookfad953c2018-06-12 14:27:37 -07001241 cpuid->nent));
Sean Christophersone53c95e2020-03-02 15:56:19 -08001242 if (!array.entries)
Sean Christophersond5a661d2020-03-02 15:56:07 -08001243 return -ENOMEM;
Avi Kivity00b27a32011-11-23 16:30:32 +02001244
Xiaoyao Li65b18912020-06-04 12:16:36 +08001245 array.maxnent = cpuid->nent;
1246
Sean Christopherson8b860792020-03-02 15:56:06 -08001247 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001248 r = get_cpuid_func(&array, funcs[i], type);
Sasha Levin831bf662011-11-28 11:20:29 +02001249 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +02001250 goto out_free;
1251 }
Sean Christophersone53c95e2020-03-02 15:56:19 -08001252 cpuid->nent = array.nent;
Avi Kivity00b27a32011-11-23 16:30:32 +02001253
Sean Christophersone53c95e2020-03-02 15:56:19 -08001254 if (copy_to_user(entries, array.entries,
1255 array.nent * sizeof(struct kvm_cpuid_entry2)))
Sean Christophersond5a661d2020-03-02 15:56:07 -08001256 r = -EFAULT;
Avi Kivity00b27a32011-11-23 16:30:32 +02001257
1258out_free:
Sean Christophersone53c95e2020-03-02 15:56:19 -08001259 vfree(array.entries);
Avi Kivity00b27a32011-11-23 16:30:32 +02001260 return r;
1261}
1262
Avi Kivity00b27a32011-11-23 16:30:32 +02001263struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
1264 u32 function, u32 index)
1265{
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +02001266 return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
1267 function, index);
Avi Kivity00b27a32011-11-23 16:30:32 +02001268}
1269EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
1270
Avi Kivity00b27a32011-11-23 16:30:32 +02001271/*
Sean Christopherson8d892312020-03-04 17:34:34 -08001272 * Intel CPUID semantics treats any query for an out-of-range leaf as if the
1273 * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
1274 * returns all zeroes for any undefined leaf, whether or not the leaf is in
1275 * range. Centaur/VIA follows Intel semantics.
1276 *
1277 * A leaf is considered out-of-range if its function is higher than the maximum
1278 * supported leaf of its associated class or if its associated class does not
1279 * exist.
1280 *
1281 * There are three primary classes to be considered, with their respective
1282 * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
1283 * class exists if a guest CPUID entry for its <base> leaf exists. For a given
1284 * class, CPUID.<base>.EAX contains the max supported leaf for the class.
1285 *
1286 * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
1287 * - Hypervisor: 0x40000000 - 0x4fffffff
1288 * - Extended: 0x80000000 - 0xbfffffff
1289 * - Centaur: 0xc0000000 - 0xcfffffff
1290 *
1291 * The Hypervisor class is further subdivided into sub-classes that each act as
Ingo Molnard9f6e122021-03-18 15:28:01 +01001292 * their own independent class associated with a 0x100 byte range. E.g. if Qemu
Sean Christopherson8d892312020-03-04 17:34:34 -08001293 * is advertising support for both HyperV and KVM, the resulting Hypervisor
1294 * CPUID sub-classes are:
1295 *
1296 * - HyperV: 0x40000000 - 0x400000ff
1297 * - KVM: 0x40000100 - 0x400001ff
Avi Kivity00b27a32011-11-23 16:30:32 +02001298 */
Sean Christopherson09c74312020-03-04 17:34:36 -08001299static struct kvm_cpuid_entry2 *
1300get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +02001301{
Sean Christopherson8d892312020-03-04 17:34:34 -08001302 struct kvm_cpuid_entry2 *basic, *class;
Sean Christopherson09c74312020-03-04 17:34:36 -08001303 u32 function = *fn_ptr;
Avi Kivity00b27a32011-11-23 16:30:32 +02001304
Sean Christopherson8d892312020-03-04 17:34:34 -08001305 basic = kvm_find_cpuid_entry(vcpu, 0, 0);
1306 if (!basic)
Sean Christopherson09c74312020-03-04 17:34:36 -08001307 return NULL;
1308
1309 if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
1310 is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
1311 return NULL;
Sean Christopherson8d892312020-03-04 17:34:34 -08001312
1313 if (function >= 0x40000000 && function <= 0x4fffffff)
1314 class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
1315 else if (function >= 0xc0000000)
1316 class = kvm_find_cpuid_entry(vcpu, 0xc0000000, 0);
1317 else
1318 class = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
1319
Sean Christopherson09c74312020-03-04 17:34:36 -08001320 if (class && function <= class->eax)
1321 return NULL;
1322
1323 /*
1324 * Leaf specific adjustments are also applied when redirecting to the
1325 * max basic entry, e.g. if the max basic leaf is 0xb but there is no
1326 * entry for CPUID.0xb.index (see below), then the output value for EDX
1327 * needs to be pulled from CPUID.0xb.1.
1328 */
1329 *fn_ptr = basic->eax;
1330
1331 /*
1332 * The class does not exist or the requested function is out of range;
1333 * the effective CPUID entry is the max basic leaf. Note, the index of
1334 * the original requested leaf is observed!
1335 */
1336 return kvm_find_cpuid_entry(vcpu, basic->eax, index);
Avi Kivity00b27a32011-11-23 16:30:32 +02001337}
1338
Yu Zhange911eb32017-08-24 20:27:52 +08001339bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
Sean Christophersonf91af512020-03-04 17:34:37 -08001340 u32 *ecx, u32 *edx, bool exact_only)
Avi Kivity00b27a32011-11-23 16:30:32 +02001341{
Jan Kiszkab7fb8482020-03-04 17:34:31 -08001342 u32 orig_function = *eax, function = *eax, index = *ecx;
Jim Mattson43561122019-09-25 17:04:17 -07001343 struct kvm_cpuid_entry2 *entry;
Sean Christopherson2b110b62020-03-17 12:53:54 -07001344 bool exact, used_max_basic = false;
Avi Kivity00b27a32011-11-23 16:30:32 +02001345
Jim Mattson43561122019-09-25 17:04:17 -07001346 entry = kvm_find_cpuid_entry(vcpu, function, index);
Sean Christophersonf91af512020-03-04 17:34:37 -08001347 exact = !!entry;
Sean Christopherson09c74312020-03-04 17:34:36 -08001348
Sean Christopherson2b110b62020-03-17 12:53:54 -07001349 if (!entry && !exact_only) {
Sean Christopherson09c74312020-03-04 17:34:36 -08001350 entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
Sean Christopherson2b110b62020-03-17 12:53:54 -07001351 used_max_basic = !!entry;
1352 }
Sean Christopherson09c74312020-03-04 17:34:36 -08001353
Jim Mattson43561122019-09-25 17:04:17 -07001354 if (entry) {
1355 *eax = entry->eax;
1356 *ebx = entry->ebx;
1357 *ecx = entry->ecx;
1358 *edx = entry->edx;
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001359 if (function == 7 && index == 0) {
1360 u64 data;
1361 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1362 (data & TSX_CTRL_CPUID_CLEAR))
1363 *ebx &= ~(F(RTM) | F(HLE));
1364 }
Jim Mattson43561122019-09-25 17:04:17 -07001365 } else {
Avi Kivity62046e52012-06-07 14:07:48 +03001366 *eax = *ebx = *ecx = *edx = 0;
Jim Mattson43561122019-09-25 17:04:17 -07001367 /*
1368 * When leaf 0BH or 1FH is defined, CL is pass-through
1369 * and EDX is always the x2APIC ID, even for undefined
1370 * subleaves. Index 1 will exist iff the leaf is
1371 * implemented, so we pass through CL iff leaf 1
1372 * exists. EDX can be copied from any existing index.
1373 */
1374 if (function == 0xb || function == 0x1f) {
1375 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1376 if (entry) {
1377 *ecx = index & 0xff;
1378 *edx = entry->edx;
1379 }
1380 }
1381 }
Sean Christopherson2b110b62020-03-17 12:53:54 -07001382 trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
1383 used_max_basic);
Sean Christophersonf91af512020-03-04 17:34:37 -08001384 return exact;
Avi Kivity62046e52012-06-07 14:07:48 +03001385}
Julian Stecklina66f7b722012-12-05 15:26:19 +01001386EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +03001387
Kyle Huey6a908b62016-11-29 12:40:37 -08001388int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity62046e52012-06-07 14:07:48 +03001389{
Jiang Biao1e131752016-11-07 08:55:49 +08001390 u32 eax, ebx, ecx, edx;
Avi Kivity62046e52012-06-07 14:07:48 +03001391
Kyle Hueydb2336a2017-03-20 01:16:28 -07001392 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1393 return 1;
1394
Sean Christophersonde3cd112019-04-30 10:36:17 -07001395 eax = kvm_rax_read(vcpu);
1396 ecx = kvm_rcx_read(vcpu);
Sean Christophersonf91af512020-03-04 17:34:37 -08001397 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001398 kvm_rax_write(vcpu, eax);
1399 kvm_rbx_write(vcpu, ebx);
1400 kvm_rcx_write(vcpu, ecx);
1401 kvm_rdx_write(vcpu, edx);
Kyle Huey6affcbe2016-11-29 12:40:40 -08001402 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +02001403}
1404EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);