blob: 14b5fb24c6be4f3779b1ad0a96eb6dc27b867a92 [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 */
Sean Christophersonb32666b2020-03-02 15:56:31 -080060 if (boot_cpu_has(X86_FEATURE_XSAVE) && best->function == 0x1)
61 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
62 kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
Avi Kivity00b27a32011-11-23 16:30:32 +020063
Sean Christophersonb32666b2020-03-02 15:56:31 -080064 cpuid_entry_change(best, X86_FEATURE_APIC,
65 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
Jim Mattsonc7dd15b2016-11-09 09:50:11 -080066
Avi Kivity00b27a32011-11-23 16:30:32 +020067 if (apic) {
Sean Christopherson4c615342020-03-02 15:56:30 -080068 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
Avi Kivity00b27a32011-11-23 16:30:32 +020069 apic->lapic_timer.timer_mode_mask = 3 << 17;
70 else
71 apic->lapic_timer.timer_mode_mask = 1 << 17;
72 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020073
Huaitong Hanb9baba82016-03-22 16:51:21 +080074 best = kvm_find_cpuid_entry(vcpu, 7, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -080075 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
76 cpuid_entry_change(best, X86_FEATURE_OSPKE,
77 kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
Huaitong Hanb9baba82016-03-22 16:51:21 +080078
Paolo Bonzinid7876f12013-10-02 16:06:15 +020079 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020080 if (!best) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020081 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +020082 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
83 } else {
Paolo Bonzinid7876f12013-10-02 16:06:15 +020084 vcpu->arch.guest_supported_xcr0 =
Sean Christophersoncfc48182020-03-02 15:56:23 -080085 (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +000086 vcpu->arch.guest_xstate_size = best->ebx =
Paolo Bonzini412a3c42014-12-03 14:38:01 +010087 xstate_required_size(vcpu->arch.xcr0, false);
Paolo Bonzini4344ee92013-10-02 16:06:16 +020088 }
Paolo Bonzinid7876f12013-10-02 16:06:15 +020089
Paolo Bonzini412a3c42014-12-03 14:38:01 +010090 best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
Sean Christopherson4c615342020-03-02 15:56:30 -080091 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
92 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
Paolo Bonzini412a3c42014-12-03 14:38:01 +010093 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
94
Nadav Amitdd598092014-09-16 15:10:03 +030095 /*
Yu Zhangfd8cb432017-08-24 20:27:56 +080096 * The existing code assumes virtual address is 48-bit or 57-bit in the
97 * canonical address checks; exit if it is ever changed.
Nadav Amitdd598092014-09-16 15:10:03 +030098 */
99 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
Yu Zhangfd8cb432017-08-24 20:27:56 +0800100 if (best) {
101 int vaddr_bits = (best->eax & 0xff00) >> 8;
102
103 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
104 return -EINVAL;
105 }
Nadav Amitdd598092014-09-16 15:10:03 +0300106
Wanpeng Licaa057a2018-03-12 04:53:03 -0700107 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
108 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
109 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
110 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
111
Wanpeng Li511a85562019-05-21 14:06:54 +0800112 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
113 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christophersonb32666b2020-03-02 15:56:31 -0800114 if (best)
115 cpuid_entry_change(best, X86_FEATURE_MWAIT,
116 vcpu->arch.ia32_misc_enable_msr &
117 MSR_IA32_MISC_ENABLE_MWAIT);
Wanpeng Li511a85562019-05-21 14:06:54 +0800118 }
119
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300120 /* Update physical-address width */
121 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +0800122 kvm_mmu_reset_context(vcpu);
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300123
Wei Huangc6702c92015-06-19 13:44:45 +0200124 kvm_pmu_refresh(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300125 return 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200126}
127
128static int is_efer_nx(void)
129{
130 unsigned long long efer = 0;
131
132 rdmsrl_safe(MSR_EFER, &efer);
133 return efer & EFER_NX;
134}
135
136static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
137{
138 int i;
139 struct kvm_cpuid_entry2 *e, *entry;
140
141 entry = NULL;
142 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
143 e = &vcpu->arch.cpuid_entries[i];
144 if (e->function == 0x80000001) {
145 entry = e;
146 break;
147 }
148 }
Sean Christopherson4c615342020-03-02 15:56:30 -0800149 if (entry && cpuid_entry_has(entry, X86_FEATURE_NX) && !is_efer_nx()) {
Sean Christophersonb32666b2020-03-02 15:56:31 -0800150 cpuid_entry_clear(entry, X86_FEATURE_NX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200151 printk(KERN_INFO "kvm: guest NX capability removed\n");
152 }
153}
154
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +0300155int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
156{
157 struct kvm_cpuid_entry2 *best;
158
159 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
160 if (!best || best->eax < 0x80000008)
161 goto not_found;
162 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
163 if (best)
164 return best->eax & 0xff;
165not_found:
166 return 36;
167}
168EXPORT_SYMBOL_GPL(cpuid_query_maxphyaddr);
169
Avi Kivity00b27a32011-11-23 16:30:32 +0200170/* when an old userspace process fills a new kernel module */
171int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
172 struct kvm_cpuid *cpuid,
173 struct kvm_cpuid_entry __user *entries)
174{
175 int r, i;
Paolo Bonzini83676e92016-06-01 14:09:19 +0200176 struct kvm_cpuid_entry *cpuid_entries = NULL;
Avi Kivity00b27a32011-11-23 16:30:32 +0200177
178 r = -E2BIG;
179 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
180 goto out;
181 r = -ENOMEM;
Paolo Bonzini83676e92016-06-01 14:09:19 +0200182 if (cpuid->nent) {
Kees Cook42bc47b2018-06-12 14:27:11 -0700183 cpuid_entries =
184 vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
185 cpuid->nent));
Paolo Bonzini83676e92016-06-01 14:09:19 +0200186 if (!cpuid_entries)
187 goto out;
188 r = -EFAULT;
189 if (copy_from_user(cpuid_entries, entries,
190 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
191 goto out;
192 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200193 for (i = 0; i < cpuid->nent; i++) {
194 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
195 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
196 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
197 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
198 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
199 vcpu->arch.cpuid_entries[i].index = 0;
200 vcpu->arch.cpuid_entries[i].flags = 0;
201 vcpu->arch.cpuid_entries[i].padding[0] = 0;
202 vcpu->arch.cpuid_entries[i].padding[1] = 0;
203 vcpu->arch.cpuid_entries[i].padding[2] = 0;
204 }
205 vcpu->arch.cpuid_nent = cpuid->nent;
206 cpuid_fix_nx_cap(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200207 kvm_apic_set_version(vcpu);
208 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300209 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200210
Avi Kivity00b27a32011-11-23 16:30:32 +0200211out:
Paolo Bonzini83676e92016-06-01 14:09:19 +0200212 vfree(cpuid_entries);
Avi Kivity00b27a32011-11-23 16:30:32 +0200213 return r;
214}
215
216int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
217 struct kvm_cpuid2 *cpuid,
218 struct kvm_cpuid_entry2 __user *entries)
219{
220 int r;
221
222 r = -E2BIG;
223 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
224 goto out;
225 r = -EFAULT;
226 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
227 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
228 goto out;
229 vcpu->arch.cpuid_nent = cpuid->nent;
230 kvm_apic_set_version(vcpu);
231 kvm_x86_ops->cpuid_update(vcpu);
Nadav Amitdd598092014-09-16 15:10:03 +0300232 r = kvm_update_cpuid(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +0200233out:
234 return r;
235}
236
237int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
238 struct kvm_cpuid2 *cpuid,
239 struct kvm_cpuid_entry2 __user *entries)
240{
241 int r;
242
243 r = -E2BIG;
244 if (cpuid->nent < vcpu->arch.cpuid_nent)
245 goto out;
246 r = -EFAULT;
247 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
248 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
249 goto out;
250 return 0;
251
252out:
253 cpuid->nent = vcpu->arch.cpuid_nent;
254 return r;
255}
256
Sean Christophersona7c48c32019-12-17 13:32:41 -0800257static __always_inline void cpuid_mask(u32 *word, int wordnum)
Avi Kivity00b27a32011-11-23 16:30:32 +0200258{
Sean Christophersona7c48c32019-12-17 13:32:41 -0800259 reverse_cpuid_check(wordnum);
Avi Kivity00b27a32011-11-23 16:30:32 +0200260 *word &= boot_cpu_data.x86_capability[wordnum];
261}
262
Sean Christophersone53c95e2020-03-02 15:56:19 -0800263struct kvm_cpuid_array {
264 struct kvm_cpuid_entry2 *entries;
265 const int maxnent;
266 int nent;
267};
268
269static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800270 u32 function, u32 index)
Avi Kivity00b27a32011-11-23 16:30:32 +0200271{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800272 struct kvm_cpuid_entry2 *entry;
273
274 if (array->nent >= array->maxnent)
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800275 return NULL;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800276
277 entry = &array->entries[array->nent++];
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800278
Avi Kivity00b27a32011-11-23 16:30:32 +0200279 entry->function = function;
280 entry->index = index;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200281 entry->flags = 0;
282
Avi Kivity00b27a32011-11-23 16:30:32 +0200283 cpuid_count(entry->function, entry->index,
284 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200285
286 switch (function) {
287 case 2:
288 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
289 break;
290 case 4:
291 case 7:
292 case 0xb:
293 case 0xd:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700294 case 0xf:
295 case 0x10:
296 case 0x12:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200297 case 0x14:
Jim Mattsona06dcd62019-09-12 09:55:03 -0700298 case 0x17:
299 case 0x18:
300 case 0x1f:
Paolo Bonzinid9aadaf2019-07-04 12:20:48 +0200301 case 0x8000001d:
302 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
303 break;
304 }
Sean Christophersonaa10a7d2020-03-02 15:56:16 -0800305
306 return entry;
Avi Kivity00b27a32011-11-23 16:30:32 +0200307}
308
Sean Christophersone53c95e2020-03-02 15:56:19 -0800309static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200310{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800311 struct kvm_cpuid_entry2 *entry = &array->entries[array->nent];
312
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200313 entry->function = func;
314 entry->index = 0;
315 entry->flags = 0;
316
Borislav Petkov84cffe42013-10-29 12:54:56 +0100317 switch (func) {
318 case 0:
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200319 entry->eax = 7;
Sean Christophersone53c95e2020-03-02 15:56:19 -0800320 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100321 break;
322 case 1:
323 entry->ecx = F(MOVBE);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800324 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100325 break;
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +0200326 case 7:
327 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
Paolo Bonziniab8bcf62019-06-24 10:23:33 +0200328 entry->eax = 0;
329 entry->ecx = F(RDPID);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800330 ++array->nent;
Borislav Petkov84cffe42013-10-29 12:54:56 +0100331 default:
332 break;
333 }
334
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200335 return 0;
336}
337
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800338static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200339{
340 unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0;
341 unsigned f_mpx = kvm_mpx_supported() ? F(MPX) : 0;
342 unsigned f_umip = kvm_x86_ops->umip_emulated() ? F(UMIP) : 0;
343 unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
344 unsigned f_la57;
John Allena47970e2019-12-19 14:17:59 -0600345 unsigned f_pku = kvm_x86_ops->pku_supported() ? F(PKU) : 0;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200346
347 /* cpuid 7.0.ebx */
348 const u32 kvm_cpuid_7_0_ebx_x86_features =
349 F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
350 F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) |
351 F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
352 F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
353 F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | f_intel_pt;
354
355 /* cpuid 7.0.ecx*/
356 const u32 kvm_cpuid_7_0_ecx_x86_features =
John Allena47970e2019-12-19 14:17:59 -0600357 F(AVX512VBMI) | F(LA57) | 0 /*PKU*/ | 0 /*OSPKE*/ | F(RDPID) |
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200358 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
359 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
Tao Xue69e72fa2019-07-16 14:55:49 +0800360 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200361
362 /* cpuid 7.0.edx*/
363 const u32 kvm_cpuid_7_0_edx_x86_features =
364 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
365 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
366 F(MD_CLEAR);
367
Jing Liu0b774622019-07-11 13:49:57 +0800368 /* cpuid 7.1.eax */
369 const u32 kvm_cpuid_7_1_eax_x86_features =
370 F(AVX512_BF16);
371
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800372 switch (entry->index) {
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200373 case 0:
Jing Liu0b774622019-07-11 13:49:57 +0800374 entry->eax = min(entry->eax, 1u);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200375 entry->ebx &= kvm_cpuid_7_0_ebx_x86_features;
376 cpuid_mask(&entry->ebx, CPUID_7_0_EBX);
377 /* TSC_ADJUST is emulated */
Sean Christophersonb32666b2020-03-02 15:56:31 -0800378 cpuid_entry_set(entry, X86_FEATURE_TSC_ADJUST);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200379
380 entry->ecx &= kvm_cpuid_7_0_ecx_x86_features;
Sean Christopherson4c615342020-03-02 15:56:30 -0800381 f_la57 = cpuid_entry_get(entry, X86_FEATURE_LA57);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200382 cpuid_mask(&entry->ecx, CPUID_7_ECX);
383 /* Set LA57 based on hardware capability. */
384 entry->ecx |= f_la57;
385 entry->ecx |= f_umip;
John Allena47970e2019-12-19 14:17:59 -0600386 entry->ecx |= f_pku;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200387 /* PKU is not yet implemented for shadow paging. */
388 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800389 cpuid_entry_clear(entry, X86_FEATURE_PKU);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200390
391 entry->edx &= kvm_cpuid_7_0_edx_x86_features;
392 cpuid_mask(&entry->edx, CPUID_7_EDX);
Paolo Bonzini0c549142019-08-19 17:24:07 +0200393 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800394 cpuid_entry_set(entry, X86_FEATURE_SPEC_CTRL);
Paolo Bonzini0c549142019-08-19 17:24:07 +0200395 if (boot_cpu_has(X86_FEATURE_STIBP))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800396 cpuid_entry_set(entry, X86_FEATURE_INTEL_STIBP);
Sean Christophersonacfad332020-03-02 15:56:15 -0800397 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800398 cpuid_entry_set(entry, X86_FEATURE_SPEC_CTRL_SSBD);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200399 /*
400 * We emulate ARCH_CAPABILITIES in software even
401 * if the host doesn't support it.
402 */
Sean Christophersonb32666b2020-03-02 15:56:31 -0800403 cpuid_entry_set(entry, X86_FEATURE_ARCH_CAPABILITIES);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200404 break;
Jing Liu0b774622019-07-11 13:49:57 +0800405 case 1:
406 entry->eax &= kvm_cpuid_7_1_eax_x86_features;
407 entry->ebx = 0;
408 entry->ecx = 0;
409 entry->edx = 0;
410 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200411 default:
412 WARN_ON_ONCE(1);
413 entry->eax = 0;
414 entry->ebx = 0;
415 entry->ecx = 0;
416 entry->edx = 0;
417 break;
418 }
419}
420
Sean Christophersone53c95e2020-03-02 15:56:19 -0800421static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200422{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800423 struct kvm_cpuid_entry2 *entry;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800424 int r, i, max_idx;
Avi Kivity00b27a32011-11-23 16:30:32 +0200425 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
426#ifdef CONFIG_X86_64
427 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
428 ? F(GBPAGES) : 0;
429 unsigned f_lm = F(LM);
430#else
431 unsigned f_gbpages = 0;
432 unsigned f_lm = 0;
433#endif
434 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Wanpeng Li55412b22014-12-02 19:21:30 +0800435 unsigned f_xsaves = kvm_x86_ops->xsaves_supported() ? F(XSAVES) : 0;
Chao Peng86f52012018-10-24 16:05:11 +0800436 unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200437
438 /* cpuid 1.edx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800439 const u32 kvm_cpuid_1_edx_x86_features =
Avi Kivity00b27a32011-11-23 16:30:32 +0200440 F(FPU) | F(VME) | F(DE) | F(PSE) |
441 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
442 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
443 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
H. Peter Anvin840d2832014-02-27 08:31:30 -0800444 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
Avi Kivity00b27a32011-11-23 16:30:32 +0200445 0 /* Reserved, DS, ACPI */ | F(MMX) |
446 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
447 0 /* HTT, TM, Reserved, PBE */;
448 /* cpuid 0x80000001.edx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800449 const u32 kvm_cpuid_8000_0001_edx_x86_features =
Avi Kivity00b27a32011-11-23 16:30:32 +0200450 F(FPU) | F(VME) | F(DE) | F(PSE) |
451 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
452 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
453 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
454 F(PAT) | F(PSE36) | 0 /* Reserved */ |
455 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
456 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
457 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
458 /* cpuid 1.ecx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800459 const u32 kvm_cpuid_1_ecx_x86_features =
Gabriel L. Somlo87c00572014-05-07 16:52:13 -0400460 /* NOTE: MONITOR (and MWAIT) are emulated as NOP,
461 * but *not* advertised to guests via CPUID ! */
Avi Kivity00b27a32011-11-23 16:30:32 +0200462 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
463 0 /* DS-CPL, VMX, SMX, EST */ |
464 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
Liu, Jinsongfb215362011-11-28 03:55:19 -0800465 F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
Mao, Junjiead756a12012-07-02 01:18:48 +0000466 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
Avi Kivity00b27a32011-11-23 16:30:32 +0200467 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
468 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
469 F(F16C) | F(RDRAND);
470 /* cpuid 0x80000001.ecx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800471 const u32 kvm_cpuid_8000_0001_ecx_x86_features =
Avi Kivity00b27a32011-11-23 16:30:32 +0200472 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
473 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500474 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
Stanislav Lanci806793f2018-01-29 11:39:44 -0500475 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -0600476 F(TOPOEXT) | F(PERFCTR_CORE);
Avi Kivity00b27a32011-11-23 16:30:32 +0200477
Ashok Raj15d45072018-02-01 22:59:43 +0100478 /* cpuid 0x80000008.ebx */
479 const u32 kvm_cpuid_8000_0008_ebx_x86_features =
Sebastian Andrzej Siewior504ce192019-09-25 23:37:21 +0200480 F(CLZERO) | F(XSAVEERPTR) |
481 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
482 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON);
Ashok Raj15d45072018-02-01 22:59:43 +0100483
Avi Kivity00b27a32011-11-23 16:30:32 +0200484 /* cpuid 0xC0000001.edx */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800485 const u32 kvm_cpuid_C000_0001_edx_x86_features =
Avi Kivity00b27a32011-11-23 16:30:32 +0200486 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
487 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
488 F(PMM) | F(PMM_EN);
489
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100490 /* cpuid 0xD.1.eax */
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800491 const u32 kvm_cpuid_D_1_eax_x86_features =
Wanpeng Li55412b22014-12-02 19:21:30 +0800492 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | f_xsaves;
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100493
Avi Kivity00b27a32011-11-23 16:30:32 +0200494 /* all calls to cpuid_count() should be made on the same cpu */
495 get_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200496
497 r = -E2BIG;
498
Sean Christophersone53c95e2020-03-02 15:56:19 -0800499 entry = do_host_cpuid(array, function, 0);
500 if (WARN_ON(!entry))
Sasha Levin831bf662011-11-28 11:20:29 +0200501 goto out;
502
Avi Kivity00b27a32011-11-23 16:30:32 +0200503 switch (function) {
504 case 0:
Like Xua87f2d32019-06-06 09:18:45 +0800505 /* Limited to the highest leaf implemented in KVM. */
506 entry->eax = min(entry->eax, 0x1fU);
Avi Kivity00b27a32011-11-23 16:30:32 +0200507 break;
508 case 1:
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800509 entry->edx &= kvm_cpuid_1_edx_x86_features;
510 cpuid_mask(&entry->edx, CPUID_1_EDX);
511 entry->ecx &= kvm_cpuid_1_ecx_x86_features;
512 cpuid_mask(&entry->ecx, CPUID_1_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200513 /* we support x2apic emulation even if host does not support
514 * it since we emulate x2apic in software */
Sean Christophersonb32666b2020-03-02 15:56:31 -0800515 cpuid_entry_set(entry, X86_FEATURE_X2APIC);
Avi Kivity00b27a32011-11-23 16:30:32 +0200516 break;
517 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
518 * may return different values. This forces us to get_cpu() before
519 * issuing the first command, and also to emulate this annoying behavior
520 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800521 case 2:
Avi Kivity00b27a32011-11-23 16:30:32 +0200522 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800523
524 for (i = 1, max_idx = entry->eax & 0xff; i < max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800525 entry = do_host_cpuid(array, function, 0);
526 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200527 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200528 }
529 break;
Jim Mattson32a243d2019-03-27 13:15:36 -0700530 /* functions 4 and 0x8000001d have additional index. */
531 case 4:
Sean Christophersonc8629032020-03-02 15:56:18 -0800532 case 0x8000001d:
533 /*
534 * Read entries until the cache type in the previous entry is
535 * zero, i.e. indicates an invalid entry.
536 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800537 for (i = 1; entry->eax & 0x1f; ++i) {
538 entry = do_host_cpuid(array, function, i);
539 if (!entry)
Sean Christopherson0fc62672020-03-02 15:56:08 -0800540 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200541 }
542 break;
Jan Kiszkae453aa02015-05-24 17:22:38 +0200543 case 6: /* Thermal management */
544 entry->eax = 0x4; /* allow ARAT */
545 entry->ebx = 0;
546 entry->ecx = 0;
547 entry->edx = 0;
548 break;
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200549 /* function 7 has additional index. */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800550 case 7:
Sean Christophersonaceac6e2020-03-02 15:56:14 -0800551 do_cpuid_7_mask(entry);
Sean Christopherson87849b12020-03-02 15:56:13 -0800552
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800553 for (i = 1, max_idx = entry->eax; i <= max_idx; i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800554 entry = do_host_cpuid(array, function, i);
555 if (!entry)
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200556 goto out;
557
Sean Christophersone53c95e2020-03-02 15:56:19 -0800558 do_cpuid_7_mask(entry);
Paolo Bonzini54d360d2019-07-04 12:18:13 +0200559 }
Avi Kivity00b27a32011-11-23 16:30:32 +0200560 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200561 case 9:
562 break;
Gleb Natapova6c06ed2011-11-10 14:57:28 +0200563 case 0xa: { /* Architectural Performance Monitoring */
564 struct x86_pmu_capability cap;
565 union cpuid10_eax eax;
566 union cpuid10_edx edx;
567
568 perf_get_x86_pmu_capability(&cap);
569
570 /*
571 * Only support guest architectural pmu on a host
572 * with architectural pmu.
573 */
574 if (!cap.version)
575 memset(&cap, 0, sizeof(cap));
576
577 eax.split.version_id = min(cap.version, 2);
578 eax.split.num_counters = cap.num_counters_gp;
579 eax.split.bit_width = cap.bit_width_gp;
580 eax.split.mask_length = cap.events_mask_len;
581
582 edx.split.num_counters_fixed = cap.num_counters_fixed;
583 edx.split.bit_width_fixed = cap.bit_width_fixed;
584 edx.split.reserved = 0;
585
586 entry->eax = eax.full;
587 entry->ebx = cap.events_mask;
588 entry->ecx = 0;
589 entry->edx = edx.full;
590 break;
591 }
Like Xua87f2d32019-06-06 09:18:45 +0800592 /*
593 * Per Intel's SDM, the 0x1f is a superset of 0xb,
594 * thus they can be handled by common code.
595 */
596 case 0x1f:
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800597 case 0xb:
Jim Mattsona1a640b2019-09-25 11:17:14 -0700598 /*
Sean Christophersone53c95e2020-03-02 15:56:19 -0800599 * Populate entries until the level type (ECX[15:8]) of the
600 * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
601 * the starting entry, filled by the primary do_host_cpuid().
Jim Mattsona1a640b2019-09-25 11:17:14 -0700602 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800603 for (i = 1; entry->ecx & 0xff00; ++i) {
604 entry = do_host_cpuid(array, function, i);
605 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200606 goto out;
Avi Kivity00b27a32011-11-23 16:30:32 +0200607 }
608 break;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800609 case 0xd:
610 entry->eax &= supported_xcr0;
611 entry->ebx = xstate_required_size(supported_xcr0, false);
Radim Krčmáře08e8332014-12-04 18:30:41 +0100612 entry->ecx = entry->ebx;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800613 entry->edx &= supported_xcr0 >> 32;
614 if (!supported_xcr0)
Paolo Bonzinib65d6e12014-11-21 18:13:26 +0100615 break;
616
Sean Christophersone53c95e2020-03-02 15:56:19 -0800617 entry = do_host_cpuid(array, function, 1);
618 if (!entry)
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800619 goto out;
620
Sean Christophersone53c95e2020-03-02 15:56:19 -0800621 entry->eax &= kvm_cpuid_D_1_eax_x86_features;
622 cpuid_mask(&entry->eax, CPUID_D_1_EAX);
623 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
Sean Christophersoncfc48182020-03-02 15:56:23 -0800624 entry->ebx = xstate_required_size(supported_xcr0, true);
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800625 else
Sean Christophersone53c95e2020-03-02 15:56:19 -0800626 entry->ebx = 0;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800627 /* Saving XSS controlled state via XSAVES isn't supported. */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800628 entry->ecx = 0;
629 entry->edx = 0;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800630
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800631 for (i = 2; i < 64; ++i) {
Sean Christophersoncfc48182020-03-02 15:56:23 -0800632 if (!(supported_xcr0 & BIT_ULL(i)))
Sean Christopherson1893c942020-03-02 15:56:10 -0800633 continue;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800634
Sean Christopherson0eee8f92020-03-02 15:56:21 -0800635 entry = do_host_cpuid(array, function, i);
Sean Christophersone53c95e2020-03-02 15:56:19 -0800636 if (!entry)
Sasha Levin831bf662011-11-28 11:20:29 +0200637 goto out;
638
Sean Christopherson91001d42020-03-02 15:56:11 -0800639 /*
Sean Christophersoncfc48182020-03-02 15:56:23 -0800640 * The supported check above should have filtered out
Sean Christopherson91001d42020-03-02 15:56:11 -0800641 * invalid sub-leafs as well as sub-leafs managed by
642 * IA32_XSS MSR. Only XCR0-managed sub-leafs should
643 * reach this point, and they should have a non-zero
644 * save state size.
645 */
Sean Christophersone53c95e2020-03-02 15:56:19 -0800646 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 1))) {
647 --array->nent;
Sean Christopherson3dc4a9c2020-03-02 15:56:09 -0800648 continue;
Sean Christopherson8b2fc442020-03-02 15:56:12 -0800649 }
Sean Christopherson91001d42020-03-02 15:56:11 -0800650
Sean Christophersone53c95e2020-03-02 15:56:19 -0800651 entry->ecx = 0;
652 entry->edx = 0;
Avi Kivity00b27a32011-11-23 16:30:32 +0200653 }
654 break;
Chao Peng86f52012018-10-24 16:05:11 +0800655 /* Intel PT */
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800656 case 0x14:
Sean Christopherson73920792020-03-02 15:56:26 -0800657 if (!f_intel_pt) {
658 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
Chao Peng86f52012018-10-24 16:05:11 +0800659 break;
Sean Christopherson73920792020-03-02 15:56:26 -0800660 }
Chao Peng86f52012018-10-24 16:05:11 +0800661
Sean Christopherson74fa0bc2020-03-02 15:56:17 -0800662 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800663 if (!do_host_cpuid(array, function, i))
Chao Peng86f52012018-10-24 16:05:11 +0800664 goto out;
Chao Peng86f52012018-10-24 16:05:11 +0800665 }
666 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200667 case KVM_CPUID_SIGNATURE: {
Mathias Krause326d07c2012-08-30 01:30:13 +0200668 static const char signature[12] = "KVMKVMKVM\0\0";
669 const u32 *sigptr = (const u32 *)signature;
Michael S. Tsirkin57c22e52012-05-02 17:55:56 +0300670 entry->eax = KVM_CPUID_FEATURES;
Avi Kivity00b27a32011-11-23 16:30:32 +0200671 entry->ebx = sigptr[0];
672 entry->ecx = sigptr[1];
673 entry->edx = sigptr[2];
674 break;
675 }
676 case KVM_CPUID_FEATURES:
677 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
678 (1 << KVM_FEATURE_NOP_IO_DELAY) |
679 (1 << KVM_FEATURE_CLOCKSOURCE2) |
680 (1 << KVM_FEATURE_ASYNC_PF) |
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300681 (1 << KVM_FEATURE_PV_EOI) |
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +0530682 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
Wanpeng Lif38a7b72017-12-12 17:33:04 -0800683 (1 << KVM_FEATURE_PV_UNHALT) |
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100684 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
Wanpeng Li4180bf12018-07-23 14:39:54 +0800685 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -0300686 (1 << KVM_FEATURE_PV_SEND_IPI) |
Wanpeng Li32b72ec2019-06-11 20:23:50 +0800687 (1 << KVM_FEATURE_POLL_CONTROL) |
688 (1 << KVM_FEATURE_PV_SCHED_YIELD);
Avi Kivity00b27a32011-11-23 16:30:32 +0200689
690 if (sched_info_on())
691 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
692
693 entry->ebx = 0;
694 entry->ecx = 0;
695 entry->edx = 0;
696 break;
697 case 0x80000000:
Brijesh Singh8765d752017-12-04 10:57:25 -0600698 entry->eax = min(entry->eax, 0x8000001f);
Avi Kivity00b27a32011-11-23 16:30:32 +0200699 break;
700 case 0x80000001:
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800701 entry->edx &= kvm_cpuid_8000_0001_edx_x86_features;
702 cpuid_mask(&entry->edx, CPUID_8000_0001_EDX);
703 entry->ecx &= kvm_cpuid_8000_0001_ecx_x86_features;
704 cpuid_mask(&entry->ecx, CPUID_8000_0001_ECX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200705 break;
Marcelo Tosattie4c9a5a12014-04-26 22:30:23 -0300706 case 0x80000007: /* Advanced power management */
707 /* invariant TSC is CPUID.80000007H:EDX[8] */
708 entry->edx &= (1 << 8);
709 /* mask against host */
710 entry->edx &= boot_cpu_data.x86_power;
711 entry->eax = entry->ebx = entry->ecx = 0;
712 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200713 case 0x80000008: {
714 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
715 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
716 unsigned phys_as = entry->eax & 0xff;
717
718 if (!g_phys_as)
719 g_phys_as = phys_as;
720 entry->eax = g_phys_as | (virt_as << 8);
Ashok Raj15d45072018-02-01 22:59:43 +0100721 entry->edx = 0;
Ashok Raj15d45072018-02-01 22:59:43 +0100722 entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
723 cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -0400724 /*
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400725 * AMD has separate bits for each SPEC_CTRL bit.
726 * arch/x86/kernel/cpu/bugs.c is kind enough to
727 * record that in cpufeatures so use them.
728 */
729 if (boot_cpu_has(X86_FEATURE_IBPB))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800730 cpuid_entry_set(entry, X86_FEATURE_AMD_IBPB);
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400731 if (boot_cpu_has(X86_FEATURE_IBRS))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800732 cpuid_entry_set(entry, X86_FEATURE_AMD_IBRS);
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400733 if (boot_cpu_has(X86_FEATURE_STIBP))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800734 cpuid_entry_set(entry, X86_FEATURE_AMD_STIBP);
Sean Christophersonacfad332020-03-02 15:56:15 -0800735 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800736 cpuid_entry_set(entry, X86_FEATURE_AMD_SSBD);
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400737 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800738 cpuid_entry_set(entry, X86_FEATURE_AMD_SSB_NO);
Paolo Bonzini4c6903a2019-08-14 12:07:34 -0400739 /*
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -0400740 * The preference is to use SPEC CTRL MSR instead of the
741 * VIRT_SPEC MSR.
742 */
743 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
744 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
Sean Christophersonb32666b2020-03-02 15:56:31 -0800745 cpuid_entry_set(entry, X86_FEATURE_VIRT_SSBD);
Avi Kivity00b27a32011-11-23 16:30:32 +0200746 break;
747 }
748 case 0x80000019:
749 entry->ecx = entry->edx = 0;
750 break;
751 case 0x8000001a:
Jim Mattson382409b2019-03-27 13:15:37 -0700752 case 0x8000001e:
Avi Kivity00b27a32011-11-23 16:30:32 +0200753 break;
Peter Gondac1de0f22019-11-21 12:33:43 -0800754 /* Support memory encryption cpuid if host supports it */
755 case 0x8000001F:
756 if (!boot_cpu_has(X86_FEATURE_SEV))
757 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
758 break;
Avi Kivity00b27a32011-11-23 16:30:32 +0200759 /*Add support for Centaur's CPUID instruction*/
760 case 0xC0000000:
761 /*Just support up to 0xC0000004 now*/
762 entry->eax = min(entry->eax, 0xC0000004);
763 break;
764 case 0xC0000001:
Huaitong Hane0b18ef2016-03-22 16:51:14 +0800765 entry->edx &= kvm_cpuid_C000_0001_edx_x86_features;
766 cpuid_mask(&entry->edx, CPUID_C000_0001_EDX);
Avi Kivity00b27a32011-11-23 16:30:32 +0200767 break;
768 case 3: /* Processor serial number */
769 case 5: /* MONITOR/MWAIT */
Avi Kivity00b27a32011-11-23 16:30:32 +0200770 case 0xC0000002:
771 case 0xC0000003:
772 case 0xC0000004:
773 default:
774 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
775 break;
776 }
777
Sean Christopherson160b486f2020-03-02 15:56:27 -0800778 kvm_x86_ops->set_supported_cpuid(entry);
Avi Kivity00b27a32011-11-23 16:30:32 +0200779
Sasha Levin831bf662011-11-28 11:20:29 +0200780 r = 0;
781
782out:
Avi Kivity00b27a32011-11-23 16:30:32 +0200783 put_cpu();
Sasha Levin831bf662011-11-28 11:20:29 +0200784
785 return r;
Avi Kivity00b27a32011-11-23 16:30:32 +0200786}
787
Sean Christophersone53c95e2020-03-02 15:56:19 -0800788static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
789 unsigned int type)
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200790{
Sean Christophersone53c95e2020-03-02 15:56:19 -0800791 if (array->nent >= array->maxnent)
Paolo Bonzini433f4ba2019-12-04 10:28:54 +0100792 return -E2BIG;
793
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200794 if (type == KVM_GET_EMULATED_CPUID)
Sean Christophersone53c95e2020-03-02 15:56:19 -0800795 return __do_cpuid_func_emulated(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200796
Sean Christophersone53c95e2020-03-02 15:56:19 -0800797 return __do_cpuid_func(array, func);
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200798}
799
Sean Christopherson8b860792020-03-02 15:56:06 -0800800#define CENTAUR_CPUID_SIGNATURE 0xC0000000
Sasha Levin831bf662011-11-28 11:20:29 +0200801
Sean Christophersone53c95e2020-03-02 15:56:19 -0800802static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
803 unsigned int type)
Sean Christopherson619a17f2020-03-02 15:56:05 -0800804{
805 u32 limit;
806 int r;
807
Sean Christopherson8b860792020-03-02 15:56:06 -0800808 if (func == CENTAUR_CPUID_SIGNATURE &&
809 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
810 return 0;
811
Sean Christophersone53c95e2020-03-02 15:56:19 -0800812 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -0800813 if (r)
814 return r;
815
Sean Christophersone53c95e2020-03-02 15:56:19 -0800816 limit = array->entries[array->nent - 1].eax;
Sean Christopherson619a17f2020-03-02 15:56:05 -0800817 for (func = func + 1; func <= limit; ++func) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800818 r = do_cpuid_func(array, func, type);
Sean Christopherson619a17f2020-03-02 15:56:05 -0800819 if (r)
820 break;
821 }
822
823 return r;
824}
825
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200826static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
827 __u32 num_entries, unsigned int ioctl_type)
828{
829 int i;
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100830 __u32 pad[3];
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200831
832 if (ioctl_type != KVM_GET_EMULATED_CPUID)
833 return false;
834
835 /*
836 * We want to make sure that ->padding is being passed clean from
837 * userspace in case we want to use it for something in the future.
838 *
839 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
840 * have to give ourselves satisfied only with the emulated side. /me
841 * sheds a tear.
842 */
843 for (i = 0; i < num_entries; i++) {
Borislav Petkov1b2ca422013-11-06 15:46:02 +0100844 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
845 return true;
846
847 if (pad[0] || pad[1] || pad[2])
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200848 return true;
849 }
850 return false;
851}
852
853int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
854 struct kvm_cpuid_entry2 __user *entries,
855 unsigned int type)
Avi Kivity00b27a32011-11-23 16:30:32 +0200856{
Sean Christopherson8b860792020-03-02 15:56:06 -0800857 static const u32 funcs[] = {
858 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
Sasha Levin831bf662011-11-28 11:20:29 +0200859 };
Avi Kivity00b27a32011-11-23 16:30:32 +0200860
Sean Christophersone53c95e2020-03-02 15:56:19 -0800861 struct kvm_cpuid_array array = {
862 .nent = 0,
863 .maxnent = cpuid->nent,
864 };
865 int r, i;
Sean Christophersond5a661d2020-03-02 15:56:07 -0800866
Avi Kivity00b27a32011-11-23 16:30:32 +0200867 if (cpuid->nent < 1)
Sean Christophersond5a661d2020-03-02 15:56:07 -0800868 return -E2BIG;
Avi Kivity00b27a32011-11-23 16:30:32 +0200869 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
870 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Borislav Petkov9c15bb12013-09-22 16:44:50 +0200871
872 if (sanity_check_entries(entries, cpuid->nent, type))
873 return -EINVAL;
874
Sean Christophersone53c95e2020-03-02 15:56:19 -0800875 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
Kees Cookfad953c2018-06-12 14:27:37 -0700876 cpuid->nent));
Sean Christophersone53c95e2020-03-02 15:56:19 -0800877 if (!array.entries)
Sean Christophersond5a661d2020-03-02 15:56:07 -0800878 return -ENOMEM;
Avi Kivity00b27a32011-11-23 16:30:32 +0200879
Sean Christopherson8b860792020-03-02 15:56:06 -0800880 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
Sean Christophersone53c95e2020-03-02 15:56:19 -0800881 r = get_cpuid_func(&array, funcs[i], type);
Sasha Levin831bf662011-11-28 11:20:29 +0200882 if (r)
Avi Kivity00b27a32011-11-23 16:30:32 +0200883 goto out_free;
884 }
Sean Christophersone53c95e2020-03-02 15:56:19 -0800885 cpuid->nent = array.nent;
Avi Kivity00b27a32011-11-23 16:30:32 +0200886
Sean Christophersone53c95e2020-03-02 15:56:19 -0800887 if (copy_to_user(entries, array.entries,
888 array.nent * sizeof(struct kvm_cpuid_entry2)))
Sean Christophersond5a661d2020-03-02 15:56:07 -0800889 r = -EFAULT;
Avi Kivity00b27a32011-11-23 16:30:32 +0200890
891out_free:
Sean Christophersone53c95e2020-03-02 15:56:19 -0800892 vfree(array.entries);
Avi Kivity00b27a32011-11-23 16:30:32 +0200893 return r;
894}
895
896static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
897{
898 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
Wanpeng Lia3641632017-06-08 01:22:07 -0700899 struct kvm_cpuid_entry2 *ej;
900 int j = i;
901 int nent = vcpu->arch.cpuid_nent;
Avi Kivity00b27a32011-11-23 16:30:32 +0200902
903 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
904 /* when no next entry is found, the current entry[i] is reselected */
Wanpeng Lia3641632017-06-08 01:22:07 -0700905 do {
906 j = (j + 1) % nent;
907 ej = &vcpu->arch.cpuid_entries[j];
908 } while (ej->function != e->function);
909
910 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
911
912 return j;
Avi Kivity00b27a32011-11-23 16:30:32 +0200913}
914
915/* find an entry with matching function, matching index (if needed), and that
916 * should be read next (if it's stateful) */
917static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
918 u32 function, u32 index)
919{
920 if (e->function != function)
921 return 0;
922 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
923 return 0;
924 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
925 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
926 return 0;
927 return 1;
928}
929
930struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
931 u32 function, u32 index)
932{
933 int i;
934 struct kvm_cpuid_entry2 *best = NULL;
935
936 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
937 struct kvm_cpuid_entry2 *e;
938
939 e = &vcpu->arch.cpuid_entries[i];
940 if (is_matching_cpuid_entry(e, function, index)) {
941 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
942 move_to_next_stateful_cpuid_entry(vcpu, i);
943 best = e;
944 break;
945 }
946 }
947 return best;
948}
949EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
950
Avi Kivity00b27a32011-11-23 16:30:32 +0200951/*
Jim Mattson43561122019-09-25 17:04:17 -0700952 * If the basic or extended CPUID leaf requested is higher than the
953 * maximum supported basic or extended leaf, respectively, then it is
954 * out of range.
Avi Kivity00b27a32011-11-23 16:30:32 +0200955 */
Jim Mattson43561122019-09-25 17:04:17 -0700956static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)
Avi Kivity00b27a32011-11-23 16:30:32 +0200957{
Jim Mattson43561122019-09-25 17:04:17 -0700958 struct kvm_cpuid_entry2 *max;
Avi Kivity00b27a32011-11-23 16:30:32 +0200959
Jim Mattson43561122019-09-25 17:04:17 -0700960 max = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
961 return max && function <= max->eax;
Avi Kivity00b27a32011-11-23 16:30:32 +0200962}
963
Yu Zhange911eb32017-08-24 20:27:52 +0800964bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
965 u32 *ecx, u32 *edx, bool check_limit)
Avi Kivity00b27a32011-11-23 16:30:32 +0200966{
Avi Kivity62046e52012-06-07 14:07:48 +0300967 u32 function = *eax, index = *ecx;
Jim Mattson43561122019-09-25 17:04:17 -0700968 struct kvm_cpuid_entry2 *entry;
969 struct kvm_cpuid_entry2 *max;
970 bool found;
Avi Kivity00b27a32011-11-23 16:30:32 +0200971
Jim Mattson43561122019-09-25 17:04:17 -0700972 entry = kvm_find_cpuid_entry(vcpu, function, index);
973 found = entry;
974 /*
975 * Intel CPUID semantics treats any query for an out-of-range
976 * leaf as if the highest basic leaf (i.e. CPUID.0H:EAX) were
Jim Mattson5f41a372019-09-25 17:04:18 -0700977 * requested. AMD CPUID semantics returns all zeroes for any
978 * undefined leaf, whether or not the leaf is in range.
Jim Mattson43561122019-09-25 17:04:17 -0700979 */
Jim Mattson5f41a372019-09-25 17:04:18 -0700980 if (!entry && check_limit && !guest_cpuid_is_amd(vcpu) &&
981 !cpuid_function_in_range(vcpu, function)) {
Jim Mattson43561122019-09-25 17:04:17 -0700982 max = kvm_find_cpuid_entry(vcpu, 0, 0);
983 if (max) {
984 function = max->eax;
985 entry = kvm_find_cpuid_entry(vcpu, function, index);
986 }
Yu Zhange911eb32017-08-24 20:27:52 +0800987 }
Jim Mattson43561122019-09-25 17:04:17 -0700988 if (entry) {
989 *eax = entry->eax;
990 *ebx = entry->ebx;
991 *ecx = entry->ecx;
992 *edx = entry->edx;
Paolo Bonziniedef5c32019-11-18 12:23:00 -0500993 if (function == 7 && index == 0) {
994 u64 data;
995 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
996 (data & TSX_CTRL_CPUID_CLEAR))
997 *ebx &= ~(F(RTM) | F(HLE));
998 }
Jim Mattson43561122019-09-25 17:04:17 -0700999 } else {
Avi Kivity62046e52012-06-07 14:07:48 +03001000 *eax = *ebx = *ecx = *edx = 0;
Jim Mattson43561122019-09-25 17:04:17 -07001001 /*
1002 * When leaf 0BH or 1FH is defined, CL is pass-through
1003 * and EDX is always the x2APIC ID, even for undefined
1004 * subleaves. Index 1 will exist iff the leaf is
1005 * implemented, so we pass through CL iff leaf 1
1006 * exists. EDX can be copied from any existing index.
1007 */
1008 if (function == 0xb || function == 0x1f) {
1009 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1010 if (entry) {
1011 *ecx = index & 0xff;
1012 *edx = entry->edx;
1013 }
1014 }
1015 }
1016 trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx, found);
1017 return found;
Avi Kivity62046e52012-06-07 14:07:48 +03001018}
Julian Stecklina66f7b722012-12-05 15:26:19 +01001019EXPORT_SYMBOL_GPL(kvm_cpuid);
Avi Kivity62046e52012-06-07 14:07:48 +03001020
Kyle Huey6a908b62016-11-29 12:40:37 -08001021int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity62046e52012-06-07 14:07:48 +03001022{
Jiang Biao1e131752016-11-07 08:55:49 +08001023 u32 eax, ebx, ecx, edx;
Avi Kivity62046e52012-06-07 14:07:48 +03001024
Kyle Hueydb2336a2017-03-20 01:16:28 -07001025 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1026 return 1;
1027
Sean Christophersonde3cd112019-04-30 10:36:17 -07001028 eax = kvm_rax_read(vcpu);
1029 ecx = kvm_rcx_read(vcpu);
Yu Zhange911eb32017-08-24 20:27:52 +08001030 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, true);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001031 kvm_rax_write(vcpu, eax);
1032 kvm_rbx_write(vcpu, ebx);
1033 kvm_rcx_write(vcpu, ecx);
1034 kvm_rdx_write(vcpu, edx);
Kyle Huey6affcbe2016-11-29 12:40:40 -08001035 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity00b27a32011-11-23 16:30:32 +02001036}
1037EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);