blob: c194e49622b94efc5d2bf87b9c8a305691572b2b [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>
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
22#include "lapic.h"
23#include "mmu.h"
24#include "trace.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020025#include "pmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020026
Paolo Bonzini412a3c42014-12-03 14:38:01 +010027static u32 xstate_required_size(u64 xstate_bv, bool compacted)
Paolo Bonzini4344ee92013-10-02 16:06:16 +020028{
29 int feature_bit = 0;
30 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
31
Dave Hansend91cab72015-09-02 16:31:26 -070032 xstate_bv &= XFEATURE_MASK_EXTEND;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020033 while (xstate_bv) {
34 if (xstate_bv & 0x1) {
Paolo Bonzini412a3c42014-12-03 14:38:01 +010035 u32 eax, ebx, ecx, edx, offset;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020036 cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
Paolo Bonzini412a3c42014-12-03 14:38:01 +010037 offset = compacted ? ret : ebx;
38 ret = max(ret, offset + eax);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020039 }
40
41 xstate_bv >>= 1;
42 feature_bit++;
43 }
44
45 return ret;
46}
47
Sean Christopherson87382002019-12-17 13:32:42 -080048#define F feature_bit
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010049
Nadav Amitdd598092014-09-16 15:10:03 +030050int kvm_update_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity00b27a32011-11-23 16:30:32 +020051{
52 struct kvm_cpuid_entry2 *best;
53 struct kvm_lapic *apic = vcpu->arch.apic;
54
55 best = kvm_find_cpuid_entry(vcpu, 1, 0);
56 if (!best)
Nadav Amitdd598092014-09-16 15:10:03 +030057 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +020058
59 /* Update OSXSAVE bit */
Borislav Petkovd366bf72016-04-04 22:25:02 +020060 if (boot_cpu_has(X86_FEATURE_XSAVE) && best->function == 0x1) {
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010061 best->ecx &= ~F(OSXSAVE);
Avi Kivity00b27a32011-11-23 16:30:32 +020062 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010063 best->ecx |= F(OSXSAVE);
Avi Kivity00b27a32011-11-23 16:30:32 +020064 }
65
Jim Mattsonc7dd15b2016-11-09 09:50:11 -080066 best->edx &= ~F(APIC);
67 if (vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE)
68 best->edx |= F(APIC);
69
Avi Kivity00b27a32011-11-23 16:30:32 +020070 if (apic) {
Paolo Bonzini5c404ca2014-12-03 14:34:47 +010071 if (best->ecx & F(TSC_DEADLINE_TIMER))
Avi Kivity00b27a32011-11-23 16:30:32 +020072 apic->lapic_timer.timer_mode_mask = 3 << 17;
73 else
74 apic->lapic_timer.timer_mode_mask = 1 << 17;
75 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020076
Huaitong Hanb9baba82016-03-22 16:51:21 +080077 best = kvm_find_cpuid_entry(vcpu, 7, 0);
78 if (best) {
79 /* Update OSPKE bit */
80 if (boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7) {
81 best->ecx &= ~F(OSPKE);
82 if (kvm_read_cr4_bits(vcpu, X86_CR4_PKE))
83 best->ecx |= F(OSPKE);
84 }
85 }
86
Paolo Bonzinid7876f12013-10-02 16:06:15 +020087 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020088 if (!best) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020089 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020090 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
91 } else {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020092 vcpu->arch.guest_supported_xcr0 =
Sean Christophersoncfc48182020-03-02 15:56:23 -080093 (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +000094 vcpu->arch.guest_xstate_size = best->ebx =
Paolo Bonzini412a3c42014-12-03 14:38:01 +010095 xstate_required_size(vcpu->arch.xcr0, false);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020096 }
Paolo Bonzinid7876f12013-10-02 16:06:15 +020097
Paolo Bonzini412a3c42014-12-03 14:38:01 +010098 best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
99 if (best && (best->eax & (F(XSAVES) | F(XSAVEC))))
100 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
101
Nadav Amitdd598092014-09-16 15:10:03 +0300102 /*
Yu Zhangfd8cb432017-08-24 20:27:56 +0800103 * The existing code assumes virtual address is 48-bit or 57-bit in the
104 * canonical address checks; exit if it is ever changed.
Nadav Amitdd598092014-09-16 15:10:03 +0300105 */
106 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
Yu Zhangfd8cb432017-08-24 20:27:56 +0800107 if (best) {
108 int vaddr_bits = (best->eax & 0xff00) >> 8;
109
110 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
111 return -EINVAL;
112 }
Nadav Amitdd598092014-09-16 15:10:03 +0300113
Wanpeng Licaa057a2018-03-12 04:53:03 -0700114 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
115 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
116 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
117 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
118
Wanpeng Li511a85562019-05-21 14:06:54 +0800119 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
120 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
121 if (best) {
122 if (vcpu->arch.ia32_misc_enable_msr & MSR_IA32_MISC_ENABLE_MWAIT)
123 best->ecx |= F(MWAIT);
124 else
125 best->ecx &= ~F(MWAIT);
126 }
127 }
128
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300129 /* Update physical-address width */
130 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +0800131 kvm_mmu_reset_context(vcpu);
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300132
Wei Huangc6702c92015-06-19 13:44:45 +0200133 kvm_pmu_refresh(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300134 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200135}
136
137static int is_efer_nx(void)
138{
139 unsigned long long efer = 0;
140
141 rdmsrl_safe(MSR_EFER, &efer);
142 return efer & EFER_NX;
143}
144
145static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
146{
147 int i;
148 struct kvm_cpuid_entry2 *e, *entry;
149
150 entry = NULL;
151 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
152 e = &vcpu->arch.cpuid_entries[i];
153 if (e->function == 0x80000001) {
154 entry = e;
155 break;
156 }
157 }
Paolo Bonzini5c404ca2014-12-03 14:34:47 +0100158 if (entry && (entry->edx & F(NX)) && !is_efer_nx()) {
159 entry->edx &= ~F(NX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200160 printk(KERN_INFO "kvm: guest NX capability removed\n");
161 }
162}
163
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300164int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
165{
166 struct kvm_cpuid_entry2 *best;
167
168 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
169 if (!best || best->eax < 0x80000008)
170 goto not_found;
171 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
172 if (best)
173 return best->eax & 0xff;
174not_found:
175 return 36;
176}
177EXPORT_SYMBOL_GPL(cpuid_query_maxphyaddr);
178
Avi Kivity00b27a32011-11-23 16:30:32 +0200179/* when an old userspace process fills a new kernel module */
180int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
181 struct kvm_cpuid *cpuid,
182 struct kvm_cpuid_entry __user *entries)
183{
184 int r, i;
Paolo Bonzini83676e92016-06-01 14:09:19 +0200185 struct kvm_cpuid_entry *cpuid_entries = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200186
187 r = -E2BIG;
188 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
189 goto out;
190 r = -ENOMEM;
Paolo Bonzini83676e92016-06-01 14:09:19 +0200191 if (cpuid->nent) {
Kees Cook42bc47b2018-06-12 14:27:11 -0700192 cpuid_entries =
193 vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
194 cpuid->nent));
Paolo Bonzini83676e92016-06-01 14:09:19 +0200195 if (!cpuid_entries)
196 goto out;
197 r = -EFAULT;
198 if (copy_from_user(cpuid_entries, entries,
199 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
200 goto out;
201 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200202 for (i = 0; i < cpuid->nent; i++) {
203 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
204 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
205 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
206 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
207 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
208 vcpu->arch.cpuid_entries[i].index = 0;
209 vcpu->arch.cpuid_entries[i].flags = 0;
210 vcpu->arch.cpuid_entries[i].padding[0] = 0;
211 vcpu->arch.cpuid_entries[i].padding[1] = 0;
212 vcpu->arch.cpuid_entries[i].padding[2] = 0;
213 }
214 vcpu->arch.cpuid_nent = cpuid->nent;
215 cpuid_fix_nx_cap(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200216 kvm_apic_set_version(vcpu);
217 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300218 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200219
Avi Kivity00b27a32011-11-23 16:30:32 +0200220out:
Paolo Bonzini83676e92016-06-01 14:09:19 +0200221 vfree(cpuid_entries);
Avi Kivity00b27a32011-11-23 16:30:32 +0200222 return r;
223}
224
225int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
226 struct kvm_cpuid2 *cpuid,
227 struct kvm_cpuid_entry2 __user *entries)
228{
229 int r;
230
231 r = -E2BIG;
232 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
233 goto out;
234 r = -EFAULT;
235 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
236 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
237 goto out;
238 vcpu->arch.cpuid_nent = cpuid->nent;
239 kvm_apic_set_version(vcpu);
240 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300241 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200242out:
243 return r;
244}
245
246int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
247 struct kvm_cpuid2 *cpuid,
248 struct kvm_cpuid_entry2 __user *entries)
249{
250 int r;
251
252 r = -E2BIG;
253 if (cpuid->nent < vcpu->arch.cpuid_nent)
254 goto out;
255 r = -EFAULT;
256 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
257 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
258 goto out;
259 return 0;
260
261out:
262 cpuid->nent = vcpu->arch.cpuid_nent;
263 return r;
264}
265
Sean Christophersona7c48c32019-12-17 13:32:41 -0800266static __always_inline void cpuid_mask(u32 *word, int wordnum)
Avi Kivity00b27a32011-11-23 16:30:32 +0200267{
Sean Christophersona7c48c32019-12-17 13:32:41 -0800268 reverse_cpuid_check(wordnum);
Avi Kivity00b27a32011-11-23 16:30:32 +0200269 *word &= boot_cpu_data.x86_capability[wordnum];
270}
271
Sean Christophersone53c95e2020-03-02 15:56:19 -0800272struct kvm_cpuid_array {
273 struct kvm_cpuid_entry2 *entries;
274 const int maxnent;
275 int nent;
276};
277
278static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800279 u32 function, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +0200280{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800281 struct kvm_cpuid_entry2 *entry;
282
283 if (array->nent >= array->maxnent)
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800284 return NULL;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800285
286 entry = &array->entries[array->nent++];
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800287
Avi Kivity00b27a32011-11-23 16:30:32 +0200288 entry->function = function;
289 entry->index = index;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200290 entry->flags = 0;
291
Avi Kivity00b27a32011-11-23 16:30:32 +0200292 cpuid_count(entry->function, entry->index,
293 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200294
295 switch (function) {
296 case 2:
297 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
298 break;
299 case 4:
300 case 7:
301 case 0xb:
302 case 0xd:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700303 case 0xf:
304 case 0x10:
305 case 0x12:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200306 case 0x14:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700307 case 0x17:
308 case 0x18:
309 case 0x1f:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200310 case 0x8000001d:
311 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
312 break;
313 }
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800314
315 return entry;
Avi Kivity00b27a32011-11-23 16:30:32 +0200316}
317
Sean Christophersone53c95e2020-03-02 15:56:19 -0800318static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200319{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800320 struct kvm_cpuid_entry2 *entry = &array->entries[array->nent];
321
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200322 entry->function = func;
323 entry->index = 0;
324 entry->flags = 0;
325
Borislav Petkov84cffe42013-10-29 12:54:56 +0100326 switch (func) {
327 case 0:
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200328 entry->eax = 7;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800329 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100330 break;
331 case 1:
332 entry->ecx = F(MOVBE);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800333 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100334 break;
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200335 case 7:
336 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200337 entry->eax = 0;
338 entry->ecx = F(RDPID);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800339 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100340 default:
341 break;
342 }
343
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200344 return 0;
345}
346
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800347static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200348{
349 unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0;
350 unsigned f_mpx = kvm_mpx_supported() ? F(MPX) : 0;
351 unsigned f_umip = kvm_x86_ops->umip_emulated() ? F(UMIP) : 0;
352 unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
353 unsigned f_la57;
John Allena47970e2019-12-19 14:17:59 -0600354 unsigned f_pku = kvm_x86_ops->pku_supported() ? F(PKU) : 0;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200355
356 /* cpuid 7.0.ebx */
357 const u32 kvm_cpuid_7_0_ebx_x86_features =
358 F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
359 F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) |
360 F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
361 F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
362 F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | f_intel_pt;
363
364 /* cpuid 7.0.ecx*/
365 const u32 kvm_cpuid_7_0_ecx_x86_features =
John Allena47970e2019-12-19 14:17:59 -0600366 F(AVX512VBMI) | F(LA57) | 0 /*PKU*/ | 0 /*OSPKE*/ | F(RDPID) |
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200367 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
368 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
Tao Xue69e72fa2019-07-16 14:55:49 +0800369 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200370
371 /* cpuid 7.0.edx*/
372 const u32 kvm_cpuid_7_0_edx_x86_features =
373 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
374 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
375 F(MD_CLEAR);
376
Jing Liu0b774622019-07-11 13:49:57 +0800377 /* cpuid 7.1.eax */
378 const u32 kvm_cpuid_7_1_eax_x86_features =
379 F(AVX512_BF16);
380
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800381 switch (entry->index) {
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200382 case 0:
Jing Liu0b774622019-07-11 13:49:57 +0800383 entry->eax = min(entry->eax, 1u);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200384 entry->ebx &= kvm_cpuid_7_0_ebx_x86_features;
385 cpuid_mask(&entry->ebx, CPUID_7_0_EBX);
386 /* TSC_ADJUST is emulated */
387 entry->ebx |= F(TSC_ADJUST);
388
389 entry->ecx &= kvm_cpuid_7_0_ecx_x86_features;
390 f_la57 = entry->ecx & F(LA57);
391 cpuid_mask(&entry->ecx, CPUID_7_ECX);
392 /* Set LA57 based on hardware capability. */
393 entry->ecx |= f_la57;
394 entry->ecx |= f_umip;
John Allena47970e2019-12-19 14:17:59 -0600395 entry->ecx |= f_pku;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200396 /* PKU is not yet implemented for shadow paging. */
397 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
398 entry->ecx &= ~F(PKU);
399
400 entry->edx &= kvm_cpuid_7_0_edx_x86_features;
401 cpuid_mask(&entry->edx, CPUID_7_EDX);
Paolo Bonzini0c549142019-08-19 17:24:07 +0200402 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
403 entry->edx |= F(SPEC_CTRL);
404 if (boot_cpu_has(X86_FEATURE_STIBP))
405 entry->edx |= F(INTEL_STIBP);
Sean Christophersonacfad332020-03-02 15:56:15 -0800406 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
Paolo Bonzini0c549142019-08-19 17:24:07 +0200407 entry->edx |= F(SPEC_CTRL_SSBD);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200408 /*
409 * We emulate ARCH_CAPABILITIES in software even
410 * if the host doesn't support it.
411 */
412 entry->edx |= F(ARCH_CAPABILITIES);
413 break;
Jing Liu0b774622019-07-11 13:49:57 +0800414 case 1:
415 entry->eax &= kvm_cpuid_7_1_eax_x86_features;
416 entry->ebx = 0;
417 entry->ecx = 0;
418 entry->edx = 0;
419 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200420 default:
421 WARN_ON_ONCE(1);
422 entry->eax = 0;
423 entry->ebx = 0;
424 entry->ecx = 0;
425 entry->edx = 0;
426 break;
427 }
428}
429
Sean Christophersone53c95e2020-03-02 15:56:19 -0800430static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200431{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800432 struct kvm_cpuid_entry2 *entry;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800433 int r, i, max_idx;
Avi Kivity00b27a32011-11-23 16:30:32 +0200434 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
435#ifdef CONFIG_X86_64
436 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
437 ? F(GBPAGES) : 0;
438 unsigned f_lm = F(LM);
439#else
440 unsigned f_gbpages = 0;
441 unsigned f_lm = 0;
442#endif
443 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Wanpeng Li55412b22014-12-02 19:21:30 +0800444 unsigned f_xsaves = kvm_x86_ops->xsaves_supported() ? F(XSAVES) : 0;
Chao Peng86f52012018-10-24 16:05:11 +0800445 unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200446
447 /* cpuid 1.edx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800448 const u32 kvm_cpuid_1_edx_x86_features =
Avi Kivity00b27a32011-11-23 16:30:32 +0200449 F(FPU) | F(VME) | F(DE) | F(PSE) |
450 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
451 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
452 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
H. Peter Anvin840d2832014-02-27 08:31:30 -0800453 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
Avi Kivity00b27a32011-11-23 16:30:32 +0200454 0 /* Reserved, DS, ACPI */ | F(MMX) |
455 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
456 0 /* HTT, TM, Reserved, PBE */;
457 /* cpuid 0x80000001.edx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800458 const u32 kvm_cpuid_8000_0001_edx_x86_features =
Avi Kivity00b27a32011-11-23 16:30:32 +0200459 F(FPU) | F(VME) | F(DE) | F(PSE) |
460 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
461 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
462 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
463 F(PAT) | F(PSE36) | 0 /* Reserved */ |
464 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
465 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
466 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
467 /* cpuid 1.ecx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800468 const u32 kvm_cpuid_1_ecx_x86_features =
Gabriel L. Somlo87c00572014-05-07 16:52:13 -0400469 /* NOTE: MONITOR (and MWAIT) are emulated as NOP,
470 * but *not* advertised to guests via CPUID ! */
Avi Kivity00b27a32011-11-23 16:30:32 +0200471 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
472 0 /* DS-CPL, VMX, SMX, EST */ |
473 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
Liu, Jinsongfb215362011-11-28 03:55:19 -0800474 F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
Mao, Junjiead756a12012-07-02 01:18:48 +0000475 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
Avi Kivity00b27a32011-11-23 16:30:32 +0200476 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
477 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
478 F(F16C) | F(RDRAND);
479 /* cpuid 0x80000001.ecx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800480 const u32 kvm_cpuid_8000_0001_ecx_x86_features =
Avi Kivity00b27a32011-11-23 16:30:32 +0200481 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
482 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500483 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
Stanislav Lanci806793f2018-01-29 11:39:44 -0500484 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -0600485 F(TOPOEXT) | F(PERFCTR_CORE);
Avi Kivity00b27a32011-11-23 16:30:32 +0200486
Ashok Raj15d45072018-02-01 22:59:43 +0100487 /* cpuid 0x80000008.ebx */
488 const u32 kvm_cpuid_8000_0008_ebx_x86_features =
Sebastian Andrzej Siewior504ce192019-09-25 23:37:21 +0200489 F(CLZERO) | F(XSAVEERPTR) |
490 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
491 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON);
Ashok Raj15d45072018-02-01 22:59:43 +0100492
Avi Kivity00b27a32011-11-23 16:30:32 +0200493 /* cpuid 0xC0000001.edx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800494 const u32 kvm_cpuid_C000_0001_edx_x86_features =
Avi Kivity00b27a32011-11-23 16:30:32 +0200495 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
496 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
497 F(PMM) | F(PMM_EN);
498
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100499 /* cpuid 0xD.1.eax */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800500 const u32 kvm_cpuid_D_1_eax_x86_features =
Wanpeng Li55412b22014-12-02 19:21:30 +0800501 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | f_xsaves;
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100502
Avi Kivity00b27a32011-11-23 16:30:32 +0200503 /* all calls to cpuid_count() should be made on the same cpu */
504 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200505
506 r = -E2BIG;
507
Sean Christophersone53c95e2020-03-02 15:56:19 -0800508 entry = do_host_cpuid(array, function, 0);
509 if (WARN_ON(!entry))
Sasha Levin831bf662011-11-28 11:20:29 +0200510 goto out;
511
Avi Kivity00b27a32011-11-23 16:30:32 +0200512 switch (function) {
513 case 0:
Like Xua87f2d32019-06-06 09:18:45 +0800514 /* Limited to the highest leaf implemented in KVM. */
515 entry->eax = min(entry->eax, 0x1fU);
Avi Kivity00b27a32011-11-23 16:30:32 +0200516 break;
517 case 1:
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800518 entry->edx &= kvm_cpuid_1_edx_x86_features;
519 cpuid_mask(&entry->edx, CPUID_1_EDX);
520 entry->ecx &= kvm_cpuid_1_ecx_x86_features;
521 cpuid_mask(&entry->ecx, CPUID_1_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200522 /* we support x2apic emulation even if host does not support
523 * it since we emulate x2apic in software */
524 entry->ecx |= F(X2APIC);
525 break;
526 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
527 * may return different values. This forces us to get_cpu() before
528 * issuing the first command, and also to emulate this annoying behavior
529 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800530 case 2:
Avi Kivity00b27a32011-11-23 16:30:32 +0200531 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800532
533 for (i = 1, max_idx = entry->eax & 0xff; i < max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800534 entry = do_host_cpuid(array, function, 0);
535 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200536 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200537 }
538 break;
Jim Mattson32a243d2019-03-27 13:15:36 -0700539 /* functions 4 and 0x8000001d have additional index. */
540 case 4:
Sean Christophersonc8629032020-03-02 15:56:18 -0800541 case 0x8000001d:
542 /*
543 * Read entries until the cache type in the previous entry is
544 * zero, i.e. indicates an invalid entry.
545 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800546 for (i = 1; entry->eax & 0x1f; ++i) {
547 entry = do_host_cpuid(array, function, i);
548 if (!entry)
Sean Christopherson0fc62672020-03-02 15:56:08 -0800549 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200550 }
551 break;
Jan Kiszkae453aa02015-05-24 17:22:38 +0200552 case 6: /* Thermal management */
553 entry->eax = 0x4; /* allow ARAT */
554 entry->ebx = 0;
555 entry->ecx = 0;
556 entry->edx = 0;
557 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200558 /* function 7 has additional index. */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800559 case 7:
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800560 do_cpuid_7_mask(entry);
Sean Christopherson87849b12020-03-02 15:56:13 -0800561
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800562 for (i = 1, max_idx = entry->eax; i <= max_idx; i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800563 entry = do_host_cpuid(array, function, i);
564 if (!entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200565 goto out;
566
Sean Christophersone53c95e2020-03-02 15:56:19 -0800567 do_cpuid_7_mask(entry);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200568 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200569 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200570 case 9:
571 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200572 case 0xa: { /* Architectural Performance Monitoring */
573 struct x86_pmu_capability cap;
574 union cpuid10_eax eax;
575 union cpuid10_edx edx;
576
577 perf_get_x86_pmu_capability(&cap);
578
579 /*
580 * Only support guest architectural pmu on a host
581 * with architectural pmu.
582 */
583 if (!cap.version)
584 memset(&cap, 0, sizeof(cap));
585
586 eax.split.version_id = min(cap.version, 2);
587 eax.split.num_counters = cap.num_counters_gp;
588 eax.split.bit_width = cap.bit_width_gp;
589 eax.split.mask_length = cap.events_mask_len;
590
591 edx.split.num_counters_fixed = cap.num_counters_fixed;
592 edx.split.bit_width_fixed = cap.bit_width_fixed;
593 edx.split.reserved = 0;
594
595 entry->eax = eax.full;
596 entry->ebx = cap.events_mask;
597 entry->ecx = 0;
598 entry->edx = edx.full;
599 break;
600 }
Like Xua87f2d32019-06-06 09:18:45 +0800601 /*
602 * Per Intel's SDM, the 0x1f is a superset of 0xb,
603 * thus they can be handled by common code.
604 */
605 case 0x1f:
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800606 case 0xb:
Jim Mattsona1a640b2019-09-25 11:17:14 -0700607 /*
Sean Christophersone53c95e2020-03-02 15:56:19 -0800608 * Populate entries until the level type (ECX[15:8]) of the
609 * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
610 * the starting entry, filled by the primary do_host_cpuid().
Jim Mattsona1a640b2019-09-25 11:17:14 -0700611 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800612 for (i = 1; entry->ecx & 0xff00; ++i) {
613 entry = do_host_cpuid(array, function, i);
614 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200615 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200616 }
617 break;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800618 case 0xd:
619 entry->eax &= supported_xcr0;
620 entry->ebx = xstate_required_size(supported_xcr0, false);
Radim Krčmáře08e8332014-12-04 18:30:41 +0100621 entry->ecx = entry->ebx;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800622 entry->edx &= supported_xcr0 >> 32;
623 if (!supported_xcr0)
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100624 break;
625
Sean Christophersone53c95e2020-03-02 15:56:19 -0800626 entry = do_host_cpuid(array, function, 1);
627 if (!entry)
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800628 goto out;
629
Sean Christophersone53c95e2020-03-02 15:56:19 -0800630 entry->eax &= kvm_cpuid_D_1_eax_x86_features;
631 cpuid_mask(&entry->eax, CPUID_D_1_EAX);
632 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
Sean Christophersoncfc48182020-03-02 15:56:23 -0800633 entry->ebx = xstate_required_size(supported_xcr0, true);
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800634 else
Sean Christophersone53c95e2020-03-02 15:56:19 -0800635 entry->ebx = 0;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800636 /* Saving XSS controlled state via XSAVES isn't supported. */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800637 entry->ecx = 0;
638 entry->edx = 0;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800639
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800640 for (i = 2; i < 64; ++i) {
Sean Christophersoncfc48182020-03-02 15:56:23 -0800641 if (!(supported_xcr0 & BIT_ULL(i)))
Sean Christopherson1893c942020-03-02 15:56:10 -0800642 continue;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800643
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800644 entry = do_host_cpuid(array, function, i);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800645 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200646 goto out;
647
Sean Christopherson91001d42020-03-02 15:56:11 -0800648 /*
Sean Christophersoncfc48182020-03-02 15:56:23 -0800649 * The supported check above should have filtered out
Sean Christopherson91001d42020-03-02 15:56:11 -0800650 * invalid sub-leafs as well as sub-leafs managed by
651 * IA32_XSS MSR. Only XCR0-managed sub-leafs should
652 * reach this point, and they should have a non-zero
653 * save state size.
654 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800655 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 1))) {
656 --array->nent;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800657 continue;
Sean Christopherson8b2fc442020-03-02 15:56:12 -0800658 }
Sean Christopherson91001d42020-03-02 15:56:11 -0800659
Sean Christophersone53c95e2020-03-02 15:56:19 -0800660 entry->ecx = 0;
661 entry->edx = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200662 }
663 break;
Chao Peng86f52012018-10-24 16:05:11 +0800664 /* Intel PT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800665 case 0x14:
Sean Christopherson73920792020-03-02 15:56:26 -0800666 if (!f_intel_pt) {
667 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Chao Peng86f52012018-10-24 16:05:11 +0800668 break;
Sean Christopherson73920792020-03-02 15:56:26 -0800669 }
Chao Peng86f52012018-10-24 16:05:11 +0800670
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800671 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800672 if (!do_host_cpuid(array, function, i))
Chao Peng86f52012018-10-24 16:05:11 +0800673 goto out;
Chao Peng86f52012018-10-24 16:05:11 +0800674 }
675 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200676 case KVM_CPUID_SIGNATURE: {
Mathias Krause326d07c2012-08-30 01:30:13 +0200677 static const char signature[12] = "KVMKVMKVM\0\0";
678 const u32 *sigptr = (const u32 *)signature;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +0300679 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +0200680 entry->ebx = sigptr[0];
681 entry->ecx = sigptr[1];
682 entry->edx = sigptr[2];
683 break;
684 }
685 case KVM_CPUID_FEATURES:
686 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
687 (1 << KVM_FEATURE_NOP_IO_DELAY) |
688 (1 << KVM_FEATURE_CLOCKSOURCE2) |
689 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300690 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +0530691 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
Wanpeng Lif38a7b72017-12-12 17:33:04 -0800692 (1 << KVM_FEATURE_PV_UNHALT) |
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100693 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
Wanpeng Li4180bf12018-07-23 14:39:54 +0800694 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -0300695 (1 << KVM_FEATURE_PV_SEND_IPI) |
Wanpeng Li32b72ec2019-06-11 20:23:50 +0800696 (1 << KVM_FEATURE_POLL_CONTROL) |
697 (1 << KVM_FEATURE_PV_SCHED_YIELD);
Avi Kivity00b27a32011-11-23 16:30:32 +0200698
699 if (sched_info_on())
700 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
701
702 entry->ebx = 0;
703 entry->ecx = 0;
704 entry->edx = 0;
705 break;
706 case 0x80000000:
Brijesh Singh8765d752017-12-04 10:57:25 -0600707 entry->eax = min(entry->eax, 0x8000001f);
Avi Kivity00b27a32011-11-23 16:30:32 +0200708 break;
709 case 0x80000001:
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800710 entry->edx &= kvm_cpuid_8000_0001_edx_x86_features;
711 cpuid_mask(&entry->edx, CPUID_8000_0001_EDX);
712 entry->ecx &= kvm_cpuid_8000_0001_ecx_x86_features;
713 cpuid_mask(&entry->ecx, CPUID_8000_0001_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200714 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -0300715 case 0x80000007: /* Advanced power management */
716 /* invariant TSC is CPUID.80000007H:EDX[8] */
717 entry->edx &= (1 << 8);
718 /* mask against host */
719 entry->edx &= boot_cpu_data.x86_power;
720 entry->eax = entry->ebx = entry->ecx = 0;
721 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200722 case 0x80000008: {
723 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
724 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
725 unsigned phys_as = entry->eax & 0xff;
726
727 if (!g_phys_as)
728 g_phys_as = phys_as;
729 entry->eax = g_phys_as | (virt_as << 8);
Ashok Raj15d45072018-02-01 22:59:43 +0100730 entry->edx = 0;
Ashok Raj15d45072018-02-01 22:59:43 +0100731 entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
732 cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -0400733 /*
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400734 * AMD has separate bits for each SPEC_CTRL bit.
735 * arch/x86/kernel/cpu/bugs.c is kind enough to
736 * record that in cpufeatures so use them.
737 */
738 if (boot_cpu_has(X86_FEATURE_IBPB))
739 entry->ebx |= F(AMD_IBPB);
740 if (boot_cpu_has(X86_FEATURE_IBRS))
741 entry->ebx |= F(AMD_IBRS);
742 if (boot_cpu_has(X86_FEATURE_STIBP))
743 entry->ebx |= F(AMD_STIBP);
Sean Christophersonacfad332020-03-02 15:56:15 -0800744 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400745 entry->ebx |= F(AMD_SSBD);
746 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
747 entry->ebx |= F(AMD_SSB_NO);
748 /*
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -0400749 * The preference is to use SPEC CTRL MSR instead of the
750 * VIRT_SPEC MSR.
751 */
752 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
753 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
Tom Lendackybc226f02018-05-10 22:06:39 +0200754 entry->ebx |= F(VIRT_SSBD);
Avi Kivity00b27a32011-11-23 16:30:32 +0200755 break;
756 }
757 case 0x80000019:
758 entry->ecx = entry->edx = 0;
759 break;
760 case 0x8000001a:
Jim Mattson382409b2019-03-27 13:15:37 -0700761 case 0x8000001e:
Avi Kivity00b27a32011-11-23 16:30:32 +0200762 break;
Peter Gondac1de0f22019-11-21 12:33:43 -0800763 /* Support memory encryption cpuid if host supports it */
764 case 0x8000001F:
765 if (!boot_cpu_has(X86_FEATURE_SEV))
766 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
767 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200768 /*Add support for Centaur's CPUID instruction*/
769 case 0xC0000000:
770 /*Just support up to 0xC0000004 now*/
771 entry->eax = min(entry->eax, 0xC0000004);
772 break;
773 case 0xC0000001:
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800774 entry->edx &= kvm_cpuid_C000_0001_edx_x86_features;
775 cpuid_mask(&entry->edx, CPUID_C000_0001_EDX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200776 break;
777 case 3: /* Processor serial number */
778 case 5: /* MONITOR/MWAIT */
Avi Kivity00b27a32011-11-23 16:30:32 +0200779 case 0xC0000002:
780 case 0xC0000003:
781 case 0xC0000004:
782 default:
783 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
784 break;
785 }
786
787 kvm_x86_ops->set_supported_cpuid(function, entry);
788
Sasha Levin831bf662011-11-28 11:20:29 +0200789 r = 0;
790
791out:
Avi Kivity00b27a32011-11-23 16:30:32 +0200792 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200793
794 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200795}
796
Sean Christophersone53c95e2020-03-02 15:56:19 -0800797static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
798 unsigned int type)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200799{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800800 if (array->nent >= array->maxnent)
Paolo Bonzini433f4ba2019-12-04 10:28:54 +0100801 return -E2BIG;
802
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200803 if (type == KVM_GET_EMULATED_CPUID)
Sean Christophersone53c95e2020-03-02 15:56:19 -0800804 return __do_cpuid_func_emulated(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200805
Sean Christophersone53c95e2020-03-02 15:56:19 -0800806 return __do_cpuid_func(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200807}
808
Sean Christopherson8b860792020-03-02 15:56:06 -0800809#define CENTAUR_CPUID_SIGNATURE 0xC0000000
Sasha Levin831bf662011-11-28 11:20:29 +0200810
Sean Christophersone53c95e2020-03-02 15:56:19 -0800811static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
812 unsigned int type)
Sean Christopherson619a17f2020-03-02 15:56:05 -0800813{
814 u32 limit;
815 int r;
816
Sean Christopherson8b860792020-03-02 15:56:06 -0800817 if (func == CENTAUR_CPUID_SIGNATURE &&
818 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
819 return 0;
820
Sean Christophersone53c95e2020-03-02 15:56:19 -0800821 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -0800822 if (r)
823 return r;
824
Sean Christophersone53c95e2020-03-02 15:56:19 -0800825 limit = array->entries[array->nent - 1].eax;
Sean Christopherson619a17f2020-03-02 15:56:05 -0800826 for (func = func + 1; func <= limit; ++func) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800827 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -0800828 if (r)
829 break;
830 }
831
832 return r;
833}
834
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200835static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
836 __u32 num_entries, unsigned int ioctl_type)
837{
838 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100839 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200840
841 if (ioctl_type != KVM_GET_EMULATED_CPUID)
842 return false;
843
844 /*
845 * We want to make sure that ->padding is being passed clean from
846 * userspace in case we want to use it for something in the future.
847 *
848 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
849 * have to give ourselves satisfied only with the emulated side. /me
850 * sheds a tear.
851 */
852 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100853 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
854 return true;
855
856 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200857 return true;
858 }
859 return false;
860}
861
862int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
863 struct kvm_cpuid_entry2 __user *entries,
864 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +0200865{
Sean Christopherson8b860792020-03-02 15:56:06 -0800866 static const u32 funcs[] = {
867 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
Sasha Levin831bf662011-11-28 11:20:29 +0200868 };
Avi Kivity00b27a32011-11-23 16:30:32 +0200869
Sean Christophersone53c95e2020-03-02 15:56:19 -0800870 struct kvm_cpuid_array array = {
871 .nent = 0,
872 .maxnent = cpuid->nent,
873 };
874 int r, i;
Sean Christophersond5a661d2020-03-02 15:56:07 -0800875
Avi Kivity00b27a32011-11-23 16:30:32 +0200876 if (cpuid->nent < 1)
Sean Christophersond5a661d2020-03-02 15:56:07 -0800877 return -E2BIG;
Avi Kivity00b27a32011-11-23 16:30:32 +0200878 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
879 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200880
881 if (sanity_check_entries(entries, cpuid->nent, type))
882 return -EINVAL;
883
Sean Christophersone53c95e2020-03-02 15:56:19 -0800884 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
Kees Cookfad953c2018-06-12 14:27:37 -0700885 cpuid->nent));
Sean Christophersone53c95e2020-03-02 15:56:19 -0800886 if (!array.entries)
Sean Christophersond5a661d2020-03-02 15:56:07 -0800887 return -ENOMEM;
Avi Kivity00b27a32011-11-23 16:30:32 +0200888
Sean Christopherson8b860792020-03-02 15:56:06 -0800889 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800890 r = get_cpuid_func(&array, funcs[i], type);
Sasha Levin831bf662011-11-28 11:20:29 +0200891 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +0200892 goto out_free;
893 }
Sean Christophersone53c95e2020-03-02 15:56:19 -0800894 cpuid->nent = array.nent;
Avi Kivity00b27a32011-11-23 16:30:32 +0200895
Sean Christophersone53c95e2020-03-02 15:56:19 -0800896 if (copy_to_user(entries, array.entries,
897 array.nent * sizeof(struct kvm_cpuid_entry2)))
Sean Christophersond5a661d2020-03-02 15:56:07 -0800898 r = -EFAULT;
Avi Kivity00b27a32011-11-23 16:30:32 +0200899
900out_free:
Sean Christophersone53c95e2020-03-02 15:56:19 -0800901 vfree(array.entries);
Avi Kivity00b27a32011-11-23 16:30:32 +0200902 return r;
903}
904
905static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
906{
907 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
Wanpeng Lia3641632017-06-08 01:22:07 -0700908 struct kvm_cpuid_entry2 *ej;
909 int j = i;
910 int nent = vcpu->arch.cpuid_nent;
Avi Kivity00b27a32011-11-23 16:30:32 +0200911
912 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
913 /* when no next entry is found, the current entry[i] is reselected */
Wanpeng Lia3641632017-06-08 01:22:07 -0700914 do {
915 j = (j + 1) % nent;
916 ej = &vcpu->arch.cpuid_entries[j];
917 } while (ej->function != e->function);
918
919 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
920
921 return j;
Avi Kivity00b27a32011-11-23 16:30:32 +0200922}
923
924/* find an entry with matching function, matching index (if needed), and that
925 * should be read next (if it's stateful) */
926static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
927 u32 function, u32 index)
928{
929 if (e->function != function)
930 return 0;
931 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
932 return 0;
933 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
934 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
935 return 0;
936 return 1;
937}
938
939struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
940 u32 function, u32 index)
941{
942 int i;
943 struct kvm_cpuid_entry2 *best = NULL;
944
945 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
946 struct kvm_cpuid_entry2 *e;
947
948 e = &vcpu->arch.cpuid_entries[i];
949 if (is_matching_cpuid_entry(e, function, index)) {
950 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
951 move_to_next_stateful_cpuid_entry(vcpu, i);
952 best = e;
953 break;
954 }
955 }
956 return best;
957}
958EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
959
Avi Kivity00b27a32011-11-23 16:30:32 +0200960/*
Jim Mattson43561122019-09-25 17:04:17 -0700961 * If the basic or extended CPUID leaf requested is higher than the
962 * maximum supported basic or extended leaf, respectively, then it is
963 * out of range.
Avi Kivity00b27a32011-11-23 16:30:32 +0200964 */
Jim Mattson43561122019-09-25 17:04:17 -0700965static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200966{
Jim Mattson43561122019-09-25 17:04:17 -0700967 struct kvm_cpuid_entry2 *max;
Avi Kivity00b27a32011-11-23 16:30:32 +0200968
Jim Mattson43561122019-09-25 17:04:17 -0700969 max = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
970 return max && function <= max->eax;
Avi Kivity00b27a32011-11-23 16:30:32 +0200971}
972
Yu Zhange911eb32017-08-24 20:27:52 +0800973bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
974 u32 *ecx, u32 *edx, bool check_limit)
Avi Kivity00b27a32011-11-23 16:30:32 +0200975{
Avi Kivity62046e52012-06-07 14:07:48 +0300976 u32 function = *eax, index = *ecx;
Jim Mattson43561122019-09-25 17:04:17 -0700977 struct kvm_cpuid_entry2 *entry;
978 struct kvm_cpuid_entry2 *max;
979 bool found;
Avi Kivity00b27a32011-11-23 16:30:32 +0200980
Jim Mattson43561122019-09-25 17:04:17 -0700981 entry = kvm_find_cpuid_entry(vcpu, function, index);
982 found = entry;
983 /*
984 * Intel CPUID semantics treats any query for an out-of-range
985 * leaf as if the highest basic leaf (i.e. CPUID.0H:EAX) were
Jim Mattson5f41a372019-09-25 17:04:18 -0700986 * requested. AMD CPUID semantics returns all zeroes for any
987 * undefined leaf, whether or not the leaf is in range.
Jim Mattson43561122019-09-25 17:04:17 -0700988 */
Jim Mattson5f41a372019-09-25 17:04:18 -0700989 if (!entry && check_limit && !guest_cpuid_is_amd(vcpu) &&
990 !cpuid_function_in_range(vcpu, function)) {
Jim Mattson43561122019-09-25 17:04:17 -0700991 max = kvm_find_cpuid_entry(vcpu, 0, 0);
992 if (max) {
993 function = max->eax;
994 entry = kvm_find_cpuid_entry(vcpu, function, index);
995 }
Yu Zhange911eb32017-08-24 20:27:52 +0800996 }
Jim Mattson43561122019-09-25 17:04:17 -0700997 if (entry) {
998 *eax = entry->eax;
999 *ebx = entry->ebx;
1000 *ecx = entry->ecx;
1001 *edx = entry->edx;
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001002 if (function == 7 && index == 0) {
1003 u64 data;
1004 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1005 (data & TSX_CTRL_CPUID_CLEAR))
1006 *ebx &= ~(F(RTM) | F(HLE));
1007 }
Jim Mattson43561122019-09-25 17:04:17 -07001008 } else {
Avi Kivity62046e52012-06-07 14:07:48 +03001009 *eax = *ebx = *ecx = *edx = 0;
Jim Mattson43561122019-09-25 17:04:17 -07001010 /*
1011 * When leaf 0BH or 1FH is defined, CL is pass-through
1012 * and EDX is always the x2APIC ID, even for undefined
1013 * subleaves. Index 1 will exist iff the leaf is
1014 * implemented, so we pass through CL iff leaf 1
1015 * exists. EDX can be copied from any existing index.
1016 */
1017 if (function == 0xb || function == 0x1f) {
1018 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1019 if (entry) {
1020 *ecx = index & 0xff;
1021 *edx = entry->edx;
1022 }
1023 }
1024 }
1025 trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx, found);
1026 return found;
Avi Kivity62046e52012-06-07 14:07:48 +03001027}
Julian Stecklina66f7b722012-12-05 15:26:19 +01001028EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +03001029
Kyle Huey6a908b62016-11-29 12:40:37 -08001030int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity62046e52012-06-07 14:07:48 +03001031{
Jiang Biao1e131752016-11-07 08:55:49 +08001032 u32 eax, ebx, ecx, edx;
Avi Kivity62046e52012-06-07 14:07:48 +03001033
Kyle Hueydb2336a2017-03-20 01:16:28 -07001034 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1035 return 1;
1036
Sean Christophersonde3cd112019-04-30 10:36:17 -07001037 eax = kvm_rax_read(vcpu);
1038 ecx = kvm_rcx_read(vcpu);
Yu Zhange911eb32017-08-24 20:27:52 +08001039 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, true);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001040 kvm_rax_write(vcpu, eax);
1041 kvm_rbx_write(vcpu, ebx);
1042 kvm_rcx_write(vcpu, ecx);
1043 kvm_rdx_write(vcpu, edx);
Kyle Huey6affcbe2016-11-29 12:40:40 -08001044 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +02001045}
1046EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);