blob: c55e57b30e81585fd78d4180f51bdb3f2ce0b67c [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
Paul Durrant760849b2021-11-05 09:51:01 +0000122static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
123{
124 u32 function;
125 struct kvm_cpuid_entry2 *entry;
126
127 vcpu->arch.kvm_cpuid_base = 0;
128
129 for_each_possible_hypervisor_cpuid_base(function) {
130 entry = kvm_find_cpuid_entry(vcpu, function, 0);
131
132 if (entry) {
133 u32 signature[3];
134
135 signature[0] = entry->ebx;
136 signature[1] = entry->ecx;
137 signature[2] = entry->edx;
138
139 BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE));
140 if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) {
141 vcpu->arch.kvm_cpuid_base = function;
142 break;
143 }
144 }
145 }
146}
147
Paul Durrantdc23a512021-11-15 14:41:31 +0000148static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu)
Paul Durrant760849b2021-11-05 09:51:01 +0000149{
150 u32 base = vcpu->arch.kvm_cpuid_base;
151
152 if (!base)
153 return NULL;
154
155 return kvm_find_cpuid_entry(vcpu, base | KVM_CPUID_FEATURES, 0);
156}
157
Oliver Upton01b4f512020-10-27 16:10:42 -0700158void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
159{
Paul Durrant760849b2021-11-05 09:51:01 +0000160 struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu);
Oliver Upton01b4f512020-10-27 16:10:42 -0700161
162 /*
163 * save the feature bitmap to avoid cpuid lookup for every PV
164 * operation
165 */
166 if (best)
167 vcpu->arch.pv_cpuid.features = best->eax;
168}
169
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800170void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
Avi Kivity00b27a32011-11-23 16:30:32 +0200171{
172 struct kvm_cpuid_entry2 *best;
Avi Kivity00b27a32011-11-23 16:30:32 +0200173
174 best = kvm_find_cpuid_entry(vcpu, 1, 0);
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800175 if (best) {
176 /* Update OSXSAVE bit */
177 if (boot_cpu_has(X86_FEATURE_XSAVE))
178 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
Sean Christophersonb32666b2020-03-02 15:56:31 -0800179 kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
Avi Kivity00b27a32011-11-23 16:30:32 +0200180
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800181 cpuid_entry_change(best, X86_FEATURE_APIC,
Sean Christophersonb32666b2020-03-02 15:56:31 -0800182 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
Xiaoyao Li0d3b2ba2020-07-08 14:50:48 +0800183 }
Jim Mattsonc7dd15b2016-11-09 09:50:11 -0800184
Huaitong Hanb9baba82016-03-22 16:51:21 +0800185 best = kvm_find_cpuid_entry(vcpu, 7, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800186 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
187 cpuid_entry_change(best, X86_FEATURE_OSPKE,
188 kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
Huaitong Hanb9baba82016-03-22 16:51:21 +0800189
Paolo Bonzinid7876f12013-10-02 16:06:15 +0200190 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800191 if (best)
Xiaoyao Lia71936ab2020-04-29 23:43:12 +0800192 best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
Paolo Bonzinid7876f12013-10-02 16:06:15 +0200193
Paolo Bonzini412a3c42014-12-03 14:38:01 +0100194 best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
Sean Christopherson4c615342020-03-02 15:56:30 -0800195 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
196 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
Paolo Bonzini412a3c42014-12-03 14:38:01 +0100197 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
198
Paul Durrant760849b2021-11-05 09:51:01 +0000199 best = kvm_find_kvm_cpuid_features(vcpu);
Wanpeng Licaa057a2018-03-12 04:53:03 -0700200 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
201 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
202 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
203
Wanpeng Li511a85562019-05-21 14:06:54 +0800204 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
205 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800206 if (best)
207 cpuid_entry_change(best, X86_FEATURE_MWAIT,
208 vcpu->arch.ia32_misc_enable_msr &
209 MSR_IA32_MISC_ENABLE_MWAIT);
Wanpeng Li511a85562019-05-21 14:06:54 +0800210 }
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800211}
Jim Mattson2259c172020-10-29 10:06:48 -0700212EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime);
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800213
Xiaoyao Li346ce352020-07-09 12:34:24 +0800214static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800215{
216 struct kvm_lapic *apic = vcpu->arch.apic;
217 struct kvm_cpuid_entry2 *best;
218
219 best = kvm_find_cpuid_entry(vcpu, 1, 0);
220 if (best && apic) {
221 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
222 apic->lapic_timer.timer_mode_mask = 3 << 17;
223 else
224 apic->lapic_timer.timer_mode_mask = 1 << 17;
225
226 kvm_apic_set_version(vcpu);
227 }
228
229 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
230 if (!best)
231 vcpu->arch.guest_supported_xcr0 = 0;
232 else
233 vcpu->arch.guest_supported_xcr0 =
234 (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
Wanpeng Li511a85562019-05-21 14:06:54 +0800235
Sean Christopherson72add912021-04-12 16:21:42 +1200236 /*
237 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1) enumerate
238 * the supported XSAVE Feature Request Mask (XFRM), i.e. the enclave's
239 * requested XCR0 value. The enclave's XFRM must be a subset of XCRO
240 * at the time of EENTER, thus adjust the allowed XFRM by the guest's
241 * supported XCR0. Similar to XCR0 handling, FP and SSE are forced to
242 * '1' even on CPUs that don't support XSAVE.
243 */
244 best = kvm_find_cpuid_entry(vcpu, 0x12, 0x1);
245 if (best) {
246 best->ecx &= vcpu->arch.guest_supported_xcr0 & 0xffffffff;
247 best->edx &= vcpu->arch.guest_supported_xcr0 >> 32;
248 best->ecx |= XFEATURE_MASK_FPSSE;
249 }
250
Oliver Upton01b4f512020-10-27 16:10:42 -0700251 kvm_update_pv_runtime(vcpu);
252
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300253 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Sean Christophersona8ac8642021-02-03 16:01:15 -0800254 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300255
Wei Huangc6702c92015-06-19 13:44:45 +0200256 kvm_pmu_refresh(vcpu);
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000257 vcpu->arch.cr4_guest_rsvd_bits =
258 __cr4_reserved_bits(guest_cpuid_has, vcpu);
Sean Christophersonc44d9b32020-09-29 21:16:56 -0700259
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +0100260 kvm_hv_set_cpuid(vcpu);
261
Sean Christophersonc44d9b32020-09-29 21:16:56 -0700262 /* Invoke the vendor callback only after the above state is updated. */
Jason Baronb36464772021-01-14 22:27:56 -0500263 static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu);
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800264
265 /*
Sean Christopherson49c6f872021-06-22 10:56:51 -0700266 * Except for the MMU, which needs to do its thing any vendor specific
267 * adjustments to the reserved GPA bits.
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800268 */
Sean Christopherson49c6f872021-06-22 10:56:51 -0700269 kvm_mmu_after_set_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200270}
271
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300272int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
273{
274 struct kvm_cpuid_entry2 *best;
275
276 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
277 if (!best || best->eax < 0x80000008)
278 goto not_found;
279 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
280 if (best)
281 return best->eax & 0xff;
282not_found:
283 return 36;
284}
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300285
Sean Christophersona8ac8642021-02-03 16:01:15 -0800286/*
287 * This "raw" version returns the reserved GPA bits without any adjustments for
288 * encryption technologies that usurp bits. The raw mask should be used if and
289 * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs.
290 */
291u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
292{
293 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
294}
295
Sean Christopherson8b44b172021-11-05 09:51:00 +0000296static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
297 int nent)
298{
Jing Liu5ab2f452022-01-05 04:35:19 -0800299 int r;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000300
Jing Liu5ab2f452022-01-05 04:35:19 -0800301 r = kvm_check_cpuid(vcpu, e2, nent);
302 if (r)
303 return r;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000304
Jing Liu5ab2f452022-01-05 04:35:19 -0800305 kvfree(vcpu->arch.cpuid_entries);
306 vcpu->arch.cpuid_entries = e2;
307 vcpu->arch.cpuid_nent = nent;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000308
Jing Liu5ab2f452022-01-05 04:35:19 -0800309 kvm_update_kvm_cpuid_base(vcpu);
310 kvm_update_cpuid_runtime(vcpu);
311 kvm_vcpu_after_set_cpuid(vcpu);
Sean Christopherson8b44b172021-11-05 09:51:00 +0000312
Jing Liu5ab2f452022-01-05 04:35:19 -0800313 return 0;
Sean Christopherson8b44b172021-11-05 09:51:00 +0000314}
315
Avi Kivity00b27a32011-11-23 16:30:32 +0200316/* when an old userspace process fills a new kernel module */
317int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
318 struct kvm_cpuid *cpuid,
319 struct kvm_cpuid_entry __user *entries)
320{
321 int r, i;
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200322 struct kvm_cpuid_entry *e = NULL;
323 struct kvm_cpuid_entry2 *e2 = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200324
Avi Kivity00b27a32011-11-23 16:30:32 +0200325 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200326 return -E2BIG;
327
Paolo Bonzini83676e92016-06-01 14:09:19 +0200328 if (cpuid->nent) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200329 e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent));
330 if (IS_ERR(e))
331 return PTR_ERR(e);
332
333 e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
334 if (!e2) {
335 r = -ENOMEM;
336 goto out_free_cpuid;
Denis Efremov7ec28e22020-06-03 13:11:31 +0300337 }
Paolo Bonzini83676e92016-06-01 14:09:19 +0200338 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200339 for (i = 0; i < cpuid->nent; i++) {
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200340 e2[i].function = e[i].function;
341 e2[i].eax = e[i].eax;
342 e2[i].ebx = e[i].ebx;
343 e2[i].ecx = e[i].ecx;
344 e2[i].edx = e[i].edx;
345 e2[i].index = 0;
346 e2[i].flags = 0;
347 e2[i].padding[0] = 0;
348 e2[i].padding[1] = 0;
349 e2[i].padding[2] = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200350 }
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200351
Sean Christopherson8b44b172021-11-05 09:51:00 +0000352 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
353 if (r)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200354 kvfree(e2);
Avi Kivity00b27a32011-11-23 16:30:32 +0200355
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200356out_free_cpuid:
357 kvfree(e);
358
Avi Kivity00b27a32011-11-23 16:30:32 +0200359 return r;
360}
361
362int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
363 struct kvm_cpuid2 *cpuid,
364 struct kvm_cpuid_entry2 __user *entries)
365{
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200366 struct kvm_cpuid_entry2 *e2 = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200367 int r;
368
Avi Kivity00b27a32011-11-23 16:30:32 +0200369 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200370 return -E2BIG;
371
372 if (cpuid->nent) {
373 e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent));
374 if (IS_ERR(e2))
375 return PTR_ERR(e2);
Xiaoyao Lia76733a2020-07-09 12:34:22 +0800376 }
377
Sean Christopherson8b44b172021-11-05 09:51:00 +0000378 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
379 if (r)
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200380 kvfree(e2);
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +0200381
Sean Christopherson8b44b172021-11-05 09:51:00 +0000382 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200383}
384
385int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
386 struct kvm_cpuid2 *cpuid,
387 struct kvm_cpuid_entry2 __user *entries)
388{
389 int r;
390
391 r = -E2BIG;
392 if (cpuid->nent < vcpu->arch.cpuid_nent)
393 goto out;
394 r = -EFAULT;
Michael Roth181f4942021-01-27 20:44:51 -0600395 if (copy_to_user(entries, vcpu->arch.cpuid_entries,
Avi Kivity00b27a32011-11-23 16:30:32 +0200396 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
397 goto out;
398 return 0;
399
400out:
401 cpuid->nent = vcpu->arch.cpuid_nent;
402 return r;
403}
404
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200405/* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */
Sean Christopherson462f8dd2021-04-20 18:08:50 -0700406static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf)
Sean Christopherson66a69502020-03-02 15:56:41 -0800407{
Sean Christophersond8577a42020-03-02 15:56:52 -0800408 const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
409 struct kvm_cpuid_entry2 entry;
410
Sean Christopherson66a69502020-03-02 15:56:41 -0800411 reverse_cpuid_check(leaf);
Sean Christophersond8577a42020-03-02 15:56:52 -0800412
413 cpuid_count(cpuid.function, cpuid.index,
414 &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
415
Sean Christopherson855c7e92020-03-25 12:12:59 -0700416 kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
Sean Christopherson66a69502020-03-02 15:56:41 -0800417}
418
Sean Christopherson462f8dd2021-04-20 18:08:50 -0700419static __always_inline
420void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask)
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200421{
422 /* Use kvm_cpu_cap_mask for non-scattered leafs. */
423 BUILD_BUG_ON(leaf < NCAPINTS);
424
425 kvm_cpu_caps[leaf] = mask;
426
427 __kvm_cpu_cap_mask(leaf);
428}
429
430static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
431{
432 /* Use kvm_cpu_cap_init_scattered for scattered leafs. */
433 BUILD_BUG_ON(leaf >= NCAPINTS);
434
435 kvm_cpu_caps[leaf] &= mask;
436
437 __kvm_cpu_cap_mask(leaf);
438}
439
Sean Christopherson66a69502020-03-02 15:56:41 -0800440void kvm_set_cpu_caps(void)
441{
Sean Christopherson66a69502020-03-02 15:56:41 -0800442#ifdef CONFIG_X86_64
443 unsigned int f_gbpages = F(GBPAGES);
444 unsigned int f_lm = F(LM);
Jing Liu690a7572022-01-05 04:35:27 -0800445 unsigned int f_xfd = F(XFD);
Sean Christopherson66a69502020-03-02 15:56:41 -0800446#else
447 unsigned int f_gbpages = 0;
448 unsigned int f_lm = 0;
Jing Liu690a7572022-01-05 04:35:27 -0800449 unsigned int f_xfd = 0;
Sean Christopherson66a69502020-03-02 15:56:41 -0800450#endif
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200451 memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
Sean Christopherson66a69502020-03-02 15:56:41 -0800452
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200453 BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
Sean Christopherson66a69502020-03-02 15:56:41 -0800454 sizeof(boot_cpu_data.x86_capability));
455
456 memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
Sean Christopherson4e66c0c2021-04-12 16:21:35 +1200457 sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)));
Sean Christopherson66a69502020-03-02 15:56:41 -0800458
459 kvm_cpu_cap_mask(CPUID_1_ECX,
460 /*
461 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
462 * advertised to guests via CPUID!
463 */
464 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
465 0 /* DS-CPL, VMX, SMX, EST */ |
466 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
Like Xu27461da32020-05-29 15:43:45 +0800467 F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800468 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
469 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
470 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
471 F(F16C) | F(RDRAND)
472 );
Sean Christopherson93c380e2020-03-02 15:56:54 -0800473 /* KVM emulates x2apic in software irrespective of host support. */
474 kvm_cpu_cap_set(X86_FEATURE_X2APIC);
Sean Christopherson66a69502020-03-02 15:56:41 -0800475
476 kvm_cpu_cap_mask(CPUID_1_EDX,
477 F(FPU) | F(VME) | F(DE) | F(PSE) |
478 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
479 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
480 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
481 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
482 0 /* Reserved, DS, ACPI */ | F(MMX) |
483 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
484 0 /* HTT, TM, Reserved, PBE */
485 );
486
487 kvm_cpu_cap_mask(CPUID_7_0_EBX,
Sean Christopherson72add912021-04-12 16:21:42 +1200488 F(FSGSBASE) | F(SGX) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
Sean Christophersone4203332021-02-11 16:34:10 -0800489 F(BMI2) | F(ERMS) | F(INVPCID) | F(RTM) | 0 /*MPX*/ | F(RDSEED) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800490 F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
491 F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
492 F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | 0 /*INTEL_PT*/
493 );
494
495 kvm_cpu_cap_mask(CPUID_7_ECX,
Babu Mogerfa44b822020-05-12 18:59:16 -0500496 F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800497 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
498 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
Sean Christopherson72add912021-04-12 16:21:42 +1200499 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
Paolo Bonzini76ea4382021-05-06 06:30:04 -0400500 F(SGX_LC) | F(BUS_LOCK_DETECT)
Sean Christopherson66a69502020-03-02 15:56:41 -0800501 );
502 /* Set LA57 based on hardware capability. */
503 if (cpuid_ecx(7) & F(LA57))
504 kvm_cpu_cap_set(X86_FEATURE_LA57);
505
Babu Mogerfa44b822020-05-12 18:59:16 -0500506 /*
507 * PKU not yet implemented for shadow paging and requires OSPKE
508 * to be set on the host. Clear it if that is not the case
509 */
510 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
511 kvm_cpu_cap_clear(X86_FEATURE_PKU);
512
Sean Christopherson66a69502020-03-02 15:56:41 -0800513 kvm_cpu_cap_mask(CPUID_7_EDX,
514 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
515 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
Paolo Bonzini43bd9ef2020-08-09 13:04:56 -0400516 F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
Jing Liu690a7572022-01-05 04:35:27 -0800517 F(SERIALIZE) | F(TSXLDTRK) | F(AVX512_FP16) |
518 F(AMX_TILE) | F(AMX_INT8) | F(AMX_BF16)
Sean Christopherson66a69502020-03-02 15:56:41 -0800519 );
520
Sean Christopherson93c380e2020-03-02 15:56:54 -0800521 /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
522 kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
523 kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
524
525 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
526 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
527 if (boot_cpu_has(X86_FEATURE_STIBP))
528 kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
529 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
530 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
531
Sean Christopherson66a69502020-03-02 15:56:41 -0800532 kvm_cpu_cap_mask(CPUID_7_1_EAX,
Yang Zhong1085a6b2021-01-05 08:49:09 +0800533 F(AVX_VNNI) | F(AVX512_BF16)
Sean Christopherson66a69502020-03-02 15:56:41 -0800534 );
535
536 kvm_cpu_cap_mask(CPUID_D_1_EAX,
Jing Liu690a7572022-01-05 04:35:27 -0800537 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd
Sean Christopherson66a69502020-03-02 15:56:41 -0800538 );
539
Sean Christopherson72add912021-04-12 16:21:42 +1200540 kvm_cpu_cap_init_scattered(CPUID_12_EAX,
541 SF(SGX1) | SF(SGX2)
542 );
543
Sean Christopherson66a69502020-03-02 15:56:41 -0800544 kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
545 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
546 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
547 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
548 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
Like Xub1d66da2021-11-17 16:03:04 +0800549 F(TOPOEXT) | 0 /* PERFCTR_CORE */
Sean Christopherson66a69502020-03-02 15:56:41 -0800550 );
551
552 kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
553 F(FPU) | F(VME) | F(DE) | F(PSE) |
554 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
555 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
556 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
557 F(PAT) | F(PSE36) | 0 /* Reserved */ |
Sean Christopherson13832792021-08-05 11:38:04 -0700558 F(NX) | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sean Christopherson66a69502020-03-02 15:56:41 -0800559 F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
560 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
561 );
562
563 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
564 kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
565
566 kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
567 F(CLZERO) | F(XSAVEERPTR) |
568 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
Babu Mogerb73a5432021-09-23 20:15:28 -0500569 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON) |
570 __feature_bit(KVM_X86_FEATURE_PSFD)
Sean Christopherson66a69502020-03-02 15:56:41 -0800571 );
572
Sean Christopherson9b58b982020-03-02 15:56:42 -0800573 /*
Sean Christopherson93c380e2020-03-02 15:56:54 -0800574 * AMD has separate bits for each SPEC_CTRL bit.
575 * arch/x86/kernel/cpu/bugs.c is kind enough to
576 * record that in cpufeatures so use them.
577 */
578 if (boot_cpu_has(X86_FEATURE_IBPB))
579 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
580 if (boot_cpu_has(X86_FEATURE_IBRS))
581 kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
582 if (boot_cpu_has(X86_FEATURE_STIBP))
583 kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
584 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
585 kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
586 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
587 kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
588 /*
589 * The preference is to use SPEC CTRL MSR instead of the
590 * VIRT_SPEC MSR.
591 */
592 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
593 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
594 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
595
596 /*
Sean Christopherson9b58b982020-03-02 15:56:42 -0800597 * Hide all SVM features by default, SVM will set the cap bits for
598 * features it emulates and/or exposes for L1.
599 */
600 kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
601
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700602 kvm_cpu_cap_mask(CPUID_8000_001F_EAX,
603 0 /* SME */ | F(SEV) | 0 /* VM_PAGE_FLUSH */ | F(SEV_ES) |
604 F(SME_COHERENT));
605
Sean Christopherson66a69502020-03-02 15:56:41 -0800606 kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
607 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
608 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
609 F(PMM) | F(PMM_EN)
610 );
Sean Christopherson78bba962021-05-04 10:17:34 -0700611
612 /*
613 * Hide RDTSCP and RDPID if either feature is reported as supported but
614 * probing MSR_TSC_AUX failed. This is purely a sanity check and
615 * should never happen, but the guest will likely crash if RDTSCP or
616 * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in
617 * the past. For example, the sanity check may fire if this instance of
618 * KVM is running as L1 on top of an older, broken KVM.
619 */
620 if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) ||
621 kvm_cpu_cap_has(X86_FEATURE_RDPID)) &&
622 !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) {
623 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
624 kvm_cpu_cap_clear(X86_FEATURE_RDPID);
625 }
Sean Christopherson66a69502020-03-02 15:56:41 -0800626}
627EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
628
Sean Christophersone53c95e2020-03-02 15:56:19 -0800629struct kvm_cpuid_array {
630 struct kvm_cpuid_entry2 *entries;
Xiaoyao Li65b18912020-06-04 12:16:36 +0800631 int maxnent;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800632 int nent;
633};
634
635static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800636 u32 function, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +0200637{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800638 struct kvm_cpuid_entry2 *entry;
639
640 if (array->nent >= array->maxnent)
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800641 return NULL;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800642
643 entry = &array->entries[array->nent++];
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800644
Avi Kivity00b27a32011-11-23 16:30:32 +0200645 entry->function = function;
646 entry->index = index;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200647 entry->flags = 0;
648
Avi Kivity00b27a32011-11-23 16:30:32 +0200649 cpuid_count(entry->function, entry->index,
650 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200651
652 switch (function) {
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200653 case 4:
654 case 7:
655 case 0xb:
656 case 0xd:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700657 case 0xf:
658 case 0x10:
659 case 0x12:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200660 case 0x14:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700661 case 0x17:
662 case 0x18:
Jing Liu690a7572022-01-05 04:35:27 -0800663 case 0x1d:
664 case 0x1e:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700665 case 0x1f:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200666 case 0x8000001d:
667 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
668 break;
669 }
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800670
671 return entry;
Avi Kivity00b27a32011-11-23 16:30:32 +0200672}
673
Sean Christophersone53c95e2020-03-02 15:56:19 -0800674static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200675{
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800676 struct kvm_cpuid_entry2 *entry;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800677
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800678 if (array->nent >= array->maxnent)
679 return -E2BIG;
680
681 entry = &array->entries[array->nent];
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200682 entry->function = func;
683 entry->index = 0;
684 entry->flags = 0;
685
Borislav Petkov84cffe42013-10-29 12:54:56 +0100686 switch (func) {
687 case 0:
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200688 entry->eax = 7;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800689 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100690 break;
691 case 1:
692 entry->ecx = F(MOVBE);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800693 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100694 break;
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200695 case 7:
696 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200697 entry->eax = 0;
Sean Christopherson85d00112021-05-04 10:17:21 -0700698 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
699 entry->ecx = F(RDPID);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800700 ++array->nent;
Gustavo A. R. Silva551912d2021-05-28 15:07:56 -0500701 break;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100702 default:
703 break;
704 }
705
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200706 return 0;
707}
708
Sean Christophersone53c95e2020-03-02 15:56:19 -0800709static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200710{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800711 struct kvm_cpuid_entry2 *entry;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800712 int r, i, max_idx;
Avi Kivity00b27a32011-11-23 16:30:32 +0200713
Avi Kivity00b27a32011-11-23 16:30:32 +0200714 /* all calls to cpuid_count() should be made on the same cpu */
715 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200716
717 r = -E2BIG;
718
Sean Christophersone53c95e2020-03-02 15:56:19 -0800719 entry = do_host_cpuid(array, function, 0);
Sean Christopherson7c7f9542020-03-02 15:56:56 -0800720 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200721 goto out;
722
Avi Kivity00b27a32011-11-23 16:30:32 +0200723 switch (function) {
724 case 0:
Like Xua87f2d32019-06-06 09:18:45 +0800725 /* Limited to the highest leaf implemented in KVM. */
726 entry->eax = min(entry->eax, 0x1fU);
Avi Kivity00b27a32011-11-23 16:30:32 +0200727 break;
728 case 1:
Sean Christophersonbd791992020-03-02 15:56:53 -0800729 cpuid_entry_override(entry, CPUID_1_EDX);
730 cpuid_entry_override(entry, CPUID_1_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200731 break;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800732 case 2:
Sean Christophersonc571a142020-03-02 15:56:50 -0800733 /*
734 * On ancient CPUs, function 2 entries are STATEFUL. That is,
735 * CPUID(function=2, index=0) may return different results each
736 * time, with the least-significant byte in EAX enumerating the
737 * number of times software should do CPUID(2, 0).
738 *
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800739 * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
740 * idiotic. Intel's SDM states that EAX & 0xff "will always
741 * return 01H. Software should ignore this value and not
Sean Christophersonc571a142020-03-02 15:56:50 -0800742 * interpret it as an informational descriptor", while AMD's
743 * APM states that CPUID(2) is reserved.
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800744 *
745 * WARN if a frankenstein CPU that supports virtualization and
746 * a stateful CPUID.0x2 is encountered.
Sean Christophersonc571a142020-03-02 15:56:50 -0800747 */
Sean Christopherson7ff6c032020-03-02 15:56:51 -0800748 WARN_ON_ONCE((entry->eax & 0xff) > 1);
Avi Kivity00b27a32011-11-23 16:30:32 +0200749 break;
Jim Mattson32a243d2019-03-27 13:15:36 -0700750 /* functions 4 and 0x8000001d have additional index. */
751 case 4:
Sean Christophersonc8629032020-03-02 15:56:18 -0800752 case 0x8000001d:
753 /*
754 * Read entries until the cache type in the previous entry is
755 * zero, i.e. indicates an invalid entry.
756 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800757 for (i = 1; entry->eax & 0x1f; ++i) {
758 entry = do_host_cpuid(array, function, i);
759 if (!entry)
Sean Christopherson0fc62672020-03-02 15:56:08 -0800760 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200761 }
762 break;
Jan Kiszkae453aa02015-05-24 17:22:38 +0200763 case 6: /* Thermal management */
764 entry->eax = 0x4; /* allow ARAT */
765 entry->ebx = 0;
766 entry->ecx = 0;
767 entry->edx = 0;
768 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200769 /* function 7 has additional index. */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800770 case 7:
Sean Christopherson09f628a2020-03-02 15:56:48 -0800771 entry->eax = min(entry->eax, 1u);
Sean Christophersonbd791992020-03-02 15:56:53 -0800772 cpuid_entry_override(entry, CPUID_7_0_EBX);
773 cpuid_entry_override(entry, CPUID_7_ECX);
774 cpuid_entry_override(entry, CPUID_7_EDX);
Sean Christopherson09f628a2020-03-02 15:56:48 -0800775
Sean Christophersonbcf600c2020-03-02 15:56:49 -0800776 /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
777 if (entry->eax == 1) {
778 entry = do_host_cpuid(array, function, 1);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800779 if (!entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200780 goto out;
781
Sean Christophersonbd791992020-03-02 15:56:53 -0800782 cpuid_entry_override(entry, CPUID_7_1_EAX);
Sean Christopherson09f628a2020-03-02 15:56:48 -0800783 entry->ebx = 0;
784 entry->ecx = 0;
785 entry->edx = 0;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200786 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200787 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200788 case 9:
789 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200790 case 0xa: { /* Architectural Performance Monitoring */
791 struct x86_pmu_capability cap;
792 union cpuid10_eax eax;
793 union cpuid10_edx edx;
794
795 perf_get_x86_pmu_capability(&cap);
796
797 /*
798 * Only support guest architectural pmu on a host
799 * with architectural pmu.
800 */
801 if (!cap.version)
802 memset(&cap, 0, sizeof(cap));
803
804 eax.split.version_id = min(cap.version, 2);
805 eax.split.num_counters = cap.num_counters_gp;
806 eax.split.bit_width = cap.bit_width_gp;
807 eax.split.mask_length = cap.events_mask_len;
808
Like Xu2e8cd7a2020-06-24 09:59:28 +0800809 edx.split.num_counters_fixed = min(cap.num_counters_fixed, MAX_FIXED_COUNTERS);
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200810 edx.split.bit_width_fixed = cap.bit_width_fixed;
Like Xu7234c362021-06-28 15:43:54 +0800811 if (cap.version)
812 edx.split.anythread_deprecated = 1;
Stephane Eraniancadbaa02020-10-28 12:42:47 -0700813 edx.split.reserved1 = 0;
814 edx.split.reserved2 = 0;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200815
816 entry->eax = eax.full;
817 entry->ebx = cap.events_mask;
818 entry->ecx = 0;
819 entry->edx = edx.full;
820 break;
821 }
Like Xua87f2d32019-06-06 09:18:45 +0800822 /*
823 * Per Intel's SDM, the 0x1f is a superset of 0xb,
824 * thus they can be handled by common code.
825 */
826 case 0x1f:
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800827 case 0xb:
Jim Mattsona1a640b2019-09-25 11:17:14 -0700828 /*
Sean Christophersone53c95e2020-03-02 15:56:19 -0800829 * Populate entries until the level type (ECX[15:8]) of the
830 * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
831 * the starting entry, filled by the primary do_host_cpuid().
Jim Mattsona1a640b2019-09-25 11:17:14 -0700832 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800833 for (i = 1; entry->ecx & 0xff00; ++i) {
834 entry = do_host_cpuid(array, function, i);
835 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200836 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200837 }
838 break;
Jing Liu445ecdf2022-01-05 04:35:15 -0800839 case 0xd: {
840 u64 guest_perm = xstate_get_guest_group_perm();
841
842 entry->eax &= supported_xcr0 & guest_perm;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800843 entry->ebx = xstate_required_size(supported_xcr0, false);
Radim Krčmáře08e8332014-12-04 18:30:41 +0100844 entry->ecx = entry->ebx;
Jing Liu445ecdf2022-01-05 04:35:15 -0800845 entry->edx &= (supported_xcr0 & guest_perm) >> 32;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800846 if (!supported_xcr0)
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100847 break;
848
Sean Christophersone53c95e2020-03-02 15:56:19 -0800849 entry = do_host_cpuid(array, function, 1);
850 if (!entry)
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800851 goto out;
852
Sean Christophersonbd791992020-03-02 15:56:53 -0800853 cpuid_entry_override(entry, CPUID_D_1_EAX);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800854 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100855 entry->ebx = xstate_required_size(supported_xcr0 | supported_xss,
856 true);
857 else {
858 WARN_ON_ONCE(supported_xss != 0);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800859 entry->ebx = 0;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100860 }
861 entry->ecx &= supported_xss;
862 entry->edx &= supported_xss >> 32;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800863
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800864 for (i = 2; i < 64; ++i) {
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100865 bool s_state;
866 if (supported_xcr0 & BIT_ULL(i))
867 s_state = false;
868 else if (supported_xss & BIT_ULL(i))
869 s_state = true;
870 else
Sean Christopherson1893c942020-03-02 15:56:10 -0800871 continue;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800872
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800873 entry = do_host_cpuid(array, function, i);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800874 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200875 goto out;
876
Sean Christopherson91001d42020-03-02 15:56:11 -0800877 /*
Sean Christophersoncfc48182020-03-02 15:56:23 -0800878 * The supported check above should have filtered out
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100879 * invalid sub-leafs. Only valid sub-leafs should
Sean Christopherson91001d42020-03-02 15:56:11 -0800880 * reach this point, and they should have a non-zero
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100881 * save state size. Furthermore, check whether the
882 * processor agrees with supported_xcr0/supported_xss
883 * on whether this is an XCR0- or IA32_XSS-managed area.
Sean Christopherson91001d42020-03-02 15:56:11 -0800884 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100885 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800886 --array->nent;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800887 continue;
Sean Christopherson8b2fc442020-03-02 15:56:12 -0800888 }
Sean Christophersone53c95e2020-03-02 15:56:19 -0800889 entry->edx = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200890 }
891 break;
Jing Liu445ecdf2022-01-05 04:35:15 -0800892 }
Sean Christopherson72add912021-04-12 16:21:42 +1200893 case 0x12:
894 /* Intel SGX */
895 if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) {
896 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
897 break;
898 }
899
900 /*
901 * Index 0: Sub-features, MISCSELECT (a.k.a extended features)
902 * and max enclave sizes. The SGX sub-features and MISCSELECT
903 * are restricted by kernel and KVM capabilities (like most
904 * feature flags), while enclave size is unrestricted.
905 */
906 cpuid_entry_override(entry, CPUID_12_EAX);
907 entry->ebx &= SGX_MISC_EXINFO;
908
909 entry = do_host_cpuid(array, function, 1);
910 if (!entry)
911 goto out;
912
913 /*
914 * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la
915 * feature flags. Advertise all supported flags, including
916 * privileged attributes that require explicit opt-in from
917 * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is
918 * expected to derive it from supported XCR0.
919 */
920 entry->eax &= SGX_ATTR_DEBUG | SGX_ATTR_MODE64BIT |
Sean Christophersonfe7e9482021-04-12 16:21:43 +1200921 SGX_ATTR_PROVISIONKEY | SGX_ATTR_EINITTOKENKEY |
Sean Christopherson72add912021-04-12 16:21:42 +1200922 SGX_ATTR_KSS;
923 entry->ebx &= 0;
924 break;
Chao Peng86f52012018-10-24 16:05:11 +0800925 /* Intel PT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800926 case 0x14:
Sean Christophersondd69cc22020-03-02 15:56:55 -0800927 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
Sean Christopherson73920792020-03-02 15:56:26 -0800928 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Chao Peng86f52012018-10-24 16:05:11 +0800929 break;
Sean Christopherson73920792020-03-02 15:56:26 -0800930 }
Chao Peng86f52012018-10-24 16:05:11 +0800931
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800932 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800933 if (!do_host_cpuid(array, function, i))
Chao Peng86f52012018-10-24 16:05:11 +0800934 goto out;
Chao Peng86f52012018-10-24 16:05:11 +0800935 }
936 break;
Jing Liu690a7572022-01-05 04:35:27 -0800937 /* Intel AMX TILE */
938 case 0x1d:
939 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
940 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
941 break;
942 }
943
944 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
945 if (!do_host_cpuid(array, function, i))
946 goto out;
947 }
948 break;
949 case 0x1e: /* TMUL information */
950 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
951 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
952 break;
953 }
954 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200955 case KVM_CPUID_SIGNATURE: {
Paul Durrant760849b2021-11-05 09:51:01 +0000956 const u32 *sigptr = (const u32 *)KVM_SIGNATURE;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +0300957 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +0200958 entry->ebx = sigptr[0];
959 entry->ecx = sigptr[1];
960 entry->edx = sigptr[2];
961 break;
962 }
963 case KVM_CPUID_FEATURES:
964 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
965 (1 << KVM_FEATURE_NOP_IO_DELAY) |
966 (1 << KVM_FEATURE_CLOCKSOURCE2) |
967 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300968 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +0530969 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
Wanpeng Lif38a7b72017-12-12 17:33:04 -0800970 (1 << KVM_FEATURE_PV_UNHALT) |
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100971 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
Wanpeng Li4180bf12018-07-23 14:39:54 +0800972 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -0300973 (1 << KVM_FEATURE_PV_SEND_IPI) |
Wanpeng Li32b72ec2019-06-11 20:23:50 +0800974 (1 << KVM_FEATURE_POLL_CONTROL) |
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +0200975 (1 << KVM_FEATURE_PV_SCHED_YIELD) |
976 (1 << KVM_FEATURE_ASYNC_PF_INT);
Avi Kivity00b27a32011-11-23 16:30:32 +0200977
978 if (sched_info_on())
979 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
980
981 entry->ebx = 0;
982 entry->ecx = 0;
983 entry->edx = 0;
984 break;
985 case 0x80000000:
Brijesh Singh8765d752017-12-04 10:57:25 -0600986 entry->eax = min(entry->eax, 0x8000001f);
Avi Kivity00b27a32011-11-23 16:30:32 +0200987 break;
988 case 0x80000001:
Sean Christophersonbd791992020-03-02 15:56:53 -0800989 cpuid_entry_override(entry, CPUID_8000_0001_EDX);
990 cpuid_entry_override(entry, CPUID_8000_0001_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200991 break;
Eric Northup43d05de2020-04-14 18:23:20 -0700992 case 0x80000006:
993 /* L2 cache and TLB: pass through host info. */
994 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -0300995 case 0x80000007: /* Advanced power management */
996 /* invariant TSC is CPUID.80000007H:EDX[8] */
997 entry->edx &= (1 << 8);
998 /* mask against host */
999 entry->edx &= boot_cpu_data.x86_power;
1000 entry->eax = entry->ebx = entry->ecx = 0;
1001 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001002 case 0x80000008: {
1003 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
1004 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
1005 unsigned phys_as = entry->eax & 0xff;
1006
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001007 /*
Sean Christophersone39f00f2021-06-23 16:05:47 -07001008 * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as
1009 * the guest operates in the same PA space as the host, i.e.
1010 * reductions in MAXPHYADDR for memory encryption affect shadow
1011 * paging, too.
1012 *
1013 * If TDP is enabled but an explicit guest MAXPHYADDR is not
1014 * provided, use the raw bare metal MAXPHYADDR as reductions to
1015 * the HPAs do not affect GPAs.
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001016 */
Sean Christophersone39f00f2021-06-23 16:05:47 -07001017 if (!tdp_enabled)
1018 g_phys_as = boot_cpu_data.x86_phys_bits;
1019 else if (!g_phys_as)
Avi Kivity00b27a32011-11-23 16:30:32 +02001020 g_phys_as = phys_as;
Sean Christopherson4bf48e32021-06-23 16:05:46 -07001021
Avi Kivity00b27a32011-11-23 16:30:32 +02001022 entry->eax = g_phys_as | (virt_as << 8);
Ashok Raj15d45072018-02-01 22:59:43 +01001023 entry->edx = 0;
Sean Christophersonbd791992020-03-02 15:56:53 -08001024 cpuid_entry_override(entry, CPUID_8000_0008_EBX);
Avi Kivity00b27a32011-11-23 16:30:32 +02001025 break;
1026 }
Sean Christopherson25703872020-03-02 15:57:09 -08001027 case 0x8000000A:
1028 if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
1029 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1030 break;
1031 }
1032 entry->eax = 1; /* SVM revision 1 */
1033 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
1034 ASID emulation to nested SVM */
1035 entry->ecx = 0; /* Reserved */
1036 cpuid_entry_override(entry, CPUID_8000_000A_EDX);
1037 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001038 case 0x80000019:
1039 entry->ecx = entry->edx = 0;
1040 break;
1041 case 0x8000001a:
Jim Mattson382409b2019-03-27 13:15:37 -07001042 case 0x8000001e:
Avi Kivity00b27a32011-11-23 16:30:32 +02001043 break;
Peter Gondac1de0f22019-11-21 12:33:43 -08001044 case 0x8000001F:
Sean Christophersone39f00f2021-06-23 16:05:47 -07001045 if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) {
Peter Gondac1de0f22019-11-21 12:33:43 -08001046 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Sean Christophersone39f00f2021-06-23 16:05:47 -07001047 } else {
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07001048 cpuid_entry_override(entry, CPUID_8000_001F_EAX);
Sean Christophersone39f00f2021-06-23 16:05:47 -07001049
1050 /*
1051 * Enumerate '0' for "PA bits reduction", the adjusted
1052 * MAXPHYADDR is enumerated directly (see 0x80000008).
1053 */
1054 entry->ebx &= ~GENMASK(11, 6);
1055 }
Peter Gondac1de0f22019-11-21 12:33:43 -08001056 break;
Avi Kivity00b27a32011-11-23 16:30:32 +02001057 /*Add support for Centaur's CPUID instruction*/
1058 case 0xC0000000:
1059 /*Just support up to 0xC0000004 now*/
1060 entry->eax = min(entry->eax, 0xC0000004);
1061 break;
1062 case 0xC0000001:
Sean Christophersonbd791992020-03-02 15:56:53 -08001063 cpuid_entry_override(entry, CPUID_C000_0001_EDX);
Avi Kivity00b27a32011-11-23 16:30:32 +02001064 break;
1065 case 3: /* Processor serial number */
1066 case 5: /* MONITOR/MWAIT */
Avi Kivity00b27a32011-11-23 16:30:32 +02001067 case 0xC0000002:
1068 case 0xC0000003:
1069 case 0xC0000004:
1070 default:
1071 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1072 break;
1073 }
1074
Sasha Levin831bf662011-11-28 11:20:29 +02001075 r = 0;
1076
1077out:
Avi Kivity00b27a32011-11-23 16:30:32 +02001078 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +02001079
1080 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +02001081}
1082
Sean Christophersone53c95e2020-03-02 15:56:19 -08001083static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1084 unsigned int type)
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001085{
1086 if (type == KVM_GET_EMULATED_CPUID)
Sean Christophersone53c95e2020-03-02 15:56:19 -08001087 return __do_cpuid_func_emulated(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001088
Sean Christophersone53c95e2020-03-02 15:56:19 -08001089 return __do_cpuid_func(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001090}
1091
Sean Christopherson8b860792020-03-02 15:56:06 -08001092#define CENTAUR_CPUID_SIGNATURE 0xC0000000
Sasha Levin831bf662011-11-28 11:20:29 +02001093
Sean Christophersone53c95e2020-03-02 15:56:19 -08001094static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1095 unsigned int type)
Sean Christopherson619a17f2020-03-02 15:56:05 -08001096{
1097 u32 limit;
1098 int r;
1099
Sean Christopherson8b860792020-03-02 15:56:06 -08001100 if (func == CENTAUR_CPUID_SIGNATURE &&
1101 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
1102 return 0;
1103
Sean Christophersone53c95e2020-03-02 15:56:19 -08001104 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -08001105 if (r)
1106 return r;
1107
Sean Christophersone53c95e2020-03-02 15:56:19 -08001108 limit = array->entries[array->nent - 1].eax;
Sean Christopherson619a17f2020-03-02 15:56:05 -08001109 for (func = func + 1; func <= limit; ++func) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001110 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -08001111 if (r)
1112 break;
1113 }
1114
1115 return r;
1116}
1117
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001118static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
1119 __u32 num_entries, unsigned int ioctl_type)
1120{
1121 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +01001122 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001123
1124 if (ioctl_type != KVM_GET_EMULATED_CPUID)
1125 return false;
1126
1127 /*
1128 * We want to make sure that ->padding is being passed clean from
1129 * userspace in case we want to use it for something in the future.
1130 *
1131 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
1132 * have to give ourselves satisfied only with the emulated side. /me
1133 * sheds a tear.
1134 */
1135 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +01001136 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
1137 return true;
1138
1139 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001140 return true;
1141 }
1142 return false;
1143}
1144
1145int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
1146 struct kvm_cpuid_entry2 __user *entries,
1147 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +02001148{
Sean Christopherson8b860792020-03-02 15:56:06 -08001149 static const u32 funcs[] = {
1150 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
Sasha Levin831bf662011-11-28 11:20:29 +02001151 };
Avi Kivity00b27a32011-11-23 16:30:32 +02001152
Sean Christophersone53c95e2020-03-02 15:56:19 -08001153 struct kvm_cpuid_array array = {
1154 .nent = 0,
Sean Christophersone53c95e2020-03-02 15:56:19 -08001155 };
1156 int r, i;
Sean Christophersond5a661d2020-03-02 15:56:07 -08001157
Avi Kivity00b27a32011-11-23 16:30:32 +02001158 if (cpuid->nent < 1)
Sean Christophersond5a661d2020-03-02 15:56:07 -08001159 return -E2BIG;
Avi Kivity00b27a32011-11-23 16:30:32 +02001160 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1161 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02001162
1163 if (sanity_check_entries(entries, cpuid->nent, type))
1164 return -EINVAL;
1165
Sean Christophersone53c95e2020-03-02 15:56:19 -08001166 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
Kees Cookfad953c2018-06-12 14:27:37 -07001167 cpuid->nent));
Sean Christophersone53c95e2020-03-02 15:56:19 -08001168 if (!array.entries)
Sean Christophersond5a661d2020-03-02 15:56:07 -08001169 return -ENOMEM;
Avi Kivity00b27a32011-11-23 16:30:32 +02001170
Xiaoyao Li65b18912020-06-04 12:16:36 +08001171 array.maxnent = cpuid->nent;
1172
Sean Christopherson8b860792020-03-02 15:56:06 -08001173 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -08001174 r = get_cpuid_func(&array, funcs[i], type);
Sasha Levin831bf662011-11-28 11:20:29 +02001175 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +02001176 goto out_free;
1177 }
Sean Christophersone53c95e2020-03-02 15:56:19 -08001178 cpuid->nent = array.nent;
Avi Kivity00b27a32011-11-23 16:30:32 +02001179
Sean Christophersone53c95e2020-03-02 15:56:19 -08001180 if (copy_to_user(entries, array.entries,
1181 array.nent * sizeof(struct kvm_cpuid_entry2)))
Sean Christophersond5a661d2020-03-02 15:56:07 -08001182 r = -EFAULT;
Avi Kivity00b27a32011-11-23 16:30:32 +02001183
1184out_free:
Sean Christophersone53c95e2020-03-02 15:56:19 -08001185 vfree(array.entries);
Avi Kivity00b27a32011-11-23 16:30:32 +02001186 return r;
1187}
1188
Avi Kivity00b27a32011-11-23 16:30:32 +02001189struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
1190 u32 function, u32 index)
1191{
Vitaly Kuznetsovf69858f2020-10-01 15:05:39 +02001192 return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
1193 function, index);
Avi Kivity00b27a32011-11-23 16:30:32 +02001194}
1195EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
1196
Avi Kivity00b27a32011-11-23 16:30:32 +02001197/*
Sean Christopherson8d892312020-03-04 17:34:34 -08001198 * Intel CPUID semantics treats any query for an out-of-range leaf as if the
1199 * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
1200 * returns all zeroes for any undefined leaf, whether or not the leaf is in
1201 * range. Centaur/VIA follows Intel semantics.
1202 *
1203 * A leaf is considered out-of-range if its function is higher than the maximum
1204 * supported leaf of its associated class or if its associated class does not
1205 * exist.
1206 *
1207 * There are three primary classes to be considered, with their respective
1208 * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
1209 * class exists if a guest CPUID entry for its <base> leaf exists. For a given
1210 * class, CPUID.<base>.EAX contains the max supported leaf for the class.
1211 *
1212 * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
1213 * - Hypervisor: 0x40000000 - 0x4fffffff
1214 * - Extended: 0x80000000 - 0xbfffffff
1215 * - Centaur: 0xc0000000 - 0xcfffffff
1216 *
1217 * The Hypervisor class is further subdivided into sub-classes that each act as
Ingo Molnard9f6e122021-03-18 15:28:01 +01001218 * their own independent class associated with a 0x100 byte range. E.g. if Qemu
Sean Christopherson8d892312020-03-04 17:34:34 -08001219 * is advertising support for both HyperV and KVM, the resulting Hypervisor
1220 * CPUID sub-classes are:
1221 *
1222 * - HyperV: 0x40000000 - 0x400000ff
1223 * - KVM: 0x40000100 - 0x400001ff
Avi Kivity00b27a32011-11-23 16:30:32 +02001224 */
Sean Christopherson09c74312020-03-04 17:34:36 -08001225static struct kvm_cpuid_entry2 *
1226get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +02001227{
Sean Christopherson8d892312020-03-04 17:34:34 -08001228 struct kvm_cpuid_entry2 *basic, *class;
Sean Christopherson09c74312020-03-04 17:34:36 -08001229 u32 function = *fn_ptr;
Avi Kivity00b27a32011-11-23 16:30:32 +02001230
Sean Christopherson8d892312020-03-04 17:34:34 -08001231 basic = kvm_find_cpuid_entry(vcpu, 0, 0);
1232 if (!basic)
Sean Christopherson09c74312020-03-04 17:34:36 -08001233 return NULL;
1234
1235 if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
1236 is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
1237 return NULL;
Sean Christopherson8d892312020-03-04 17:34:34 -08001238
1239 if (function >= 0x40000000 && function <= 0x4fffffff)
1240 class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
1241 else if (function >= 0xc0000000)
1242 class = kvm_find_cpuid_entry(vcpu, 0xc0000000, 0);
1243 else
1244 class = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
1245
Sean Christopherson09c74312020-03-04 17:34:36 -08001246 if (class && function <= class->eax)
1247 return NULL;
1248
1249 /*
1250 * Leaf specific adjustments are also applied when redirecting to the
1251 * max basic entry, e.g. if the max basic leaf is 0xb but there is no
1252 * entry for CPUID.0xb.index (see below), then the output value for EDX
1253 * needs to be pulled from CPUID.0xb.1.
1254 */
1255 *fn_ptr = basic->eax;
1256
1257 /*
1258 * The class does not exist or the requested function is out of range;
1259 * the effective CPUID entry is the max basic leaf. Note, the index of
1260 * the original requested leaf is observed!
1261 */
1262 return kvm_find_cpuid_entry(vcpu, basic->eax, index);
Avi Kivity00b27a32011-11-23 16:30:32 +02001263}
1264
Yu Zhange911eb32017-08-24 20:27:52 +08001265bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
Sean Christophersonf91af512020-03-04 17:34:37 -08001266 u32 *ecx, u32 *edx, bool exact_only)
Avi Kivity00b27a32011-11-23 16:30:32 +02001267{
Jan Kiszkab7fb8482020-03-04 17:34:31 -08001268 u32 orig_function = *eax, function = *eax, index = *ecx;
Jim Mattson43561122019-09-25 17:04:17 -07001269 struct kvm_cpuid_entry2 *entry;
Sean Christopherson2b110b62020-03-17 12:53:54 -07001270 bool exact, used_max_basic = false;
Avi Kivity00b27a32011-11-23 16:30:32 +02001271
Jim Mattson43561122019-09-25 17:04:17 -07001272 entry = kvm_find_cpuid_entry(vcpu, function, index);
Sean Christophersonf91af512020-03-04 17:34:37 -08001273 exact = !!entry;
Sean Christopherson09c74312020-03-04 17:34:36 -08001274
Sean Christopherson2b110b62020-03-17 12:53:54 -07001275 if (!entry && !exact_only) {
Sean Christopherson09c74312020-03-04 17:34:36 -08001276 entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
Sean Christopherson2b110b62020-03-17 12:53:54 -07001277 used_max_basic = !!entry;
1278 }
Sean Christopherson09c74312020-03-04 17:34:36 -08001279
Jim Mattson43561122019-09-25 17:04:17 -07001280 if (entry) {
1281 *eax = entry->eax;
1282 *ebx = entry->ebx;
1283 *ecx = entry->ecx;
1284 *edx = entry->edx;
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001285 if (function == 7 && index == 0) {
1286 u64 data;
1287 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1288 (data & TSX_CTRL_CPUID_CLEAR))
1289 *ebx &= ~(F(RTM) | F(HLE));
1290 }
Jim Mattson43561122019-09-25 17:04:17 -07001291 } else {
Avi Kivity62046e52012-06-07 14:07:48 +03001292 *eax = *ebx = *ecx = *edx = 0;
Jim Mattson43561122019-09-25 17:04:17 -07001293 /*
1294 * When leaf 0BH or 1FH is defined, CL is pass-through
1295 * and EDX is always the x2APIC ID, even for undefined
1296 * subleaves. Index 1 will exist iff the leaf is
1297 * implemented, so we pass through CL iff leaf 1
1298 * exists. EDX can be copied from any existing index.
1299 */
1300 if (function == 0xb || function == 0x1f) {
1301 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1302 if (entry) {
1303 *ecx = index & 0xff;
1304 *edx = entry->edx;
1305 }
1306 }
1307 }
Sean Christopherson2b110b62020-03-17 12:53:54 -07001308 trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
1309 used_max_basic);
Sean Christophersonf91af512020-03-04 17:34:37 -08001310 return exact;
Avi Kivity62046e52012-06-07 14:07:48 +03001311}
Julian Stecklina66f7b722012-12-05 15:26:19 +01001312EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +03001313
Kyle Huey6a908b62016-11-29 12:40:37 -08001314int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity62046e52012-06-07 14:07:48 +03001315{
Jiang Biao1e131752016-11-07 08:55:49 +08001316 u32 eax, ebx, ecx, edx;
Avi Kivity62046e52012-06-07 14:07:48 +03001317
Kyle Hueydb2336a2017-03-20 01:16:28 -07001318 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1319 return 1;
1320
Sean Christophersonde3cd112019-04-30 10:36:17 -07001321 eax = kvm_rax_read(vcpu);
1322 ecx = kvm_rcx_read(vcpu);
Sean Christophersonf91af512020-03-04 17:34:37 -08001323 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001324 kvm_rax_write(vcpu, eax);
1325 kvm_rbx_write(vcpu, ebx);
1326 kvm_rcx_write(vcpu, ecx);
1327 kvm_rdx_write(vcpu, edx);
Kyle Huey6affcbe2016-11-29 12:40:40 -08001328 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +02001329}
1330EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);