]>
Commit | Line | Data |
---|---|---|
00b27a3e AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * cpuid support routines | |
4 | * | |
5 | * derived from arch/x86/kvm/x86.c | |
6 | * | |
7 | * Copyright 2011 Red Hat, Inc. and/or its affiliates. | |
8 | * Copyright IBM Corporation, 2008 | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
11 | * the COPYING file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <linux/kvm_host.h> | |
1767e931 | 16 | #include <linux/export.h> |
bb5a798a JK |
17 | #include <linux/vmalloc.h> |
18 | #include <linux/uaccess.h> | |
4504b5c9 | 19 | #include <asm/processor.h> |
4e241557 | 20 | #include <asm/fpu/internal.h> /* For use_eager_fpu. Ugh! */ |
00b27a3e | 21 | #include <asm/user.h> |
669ebabb | 22 | #include <asm/fpu/xstate.h> |
00b27a3e AK |
23 | #include "cpuid.h" |
24 | #include "lapic.h" | |
25 | #include "mmu.h" | |
26 | #include "trace.h" | |
474a5bb9 | 27 | #include "pmu.h" |
00b27a3e | 28 | |
412a3c41 | 29 | static u32 xstate_required_size(u64 xstate_bv, bool compacted) |
4344ee98 PB |
30 | { |
31 | int feature_bit = 0; | |
32 | u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; | |
33 | ||
d91cab78 | 34 | xstate_bv &= XFEATURE_MASK_EXTEND; |
4344ee98 PB |
35 | while (xstate_bv) { |
36 | if (xstate_bv & 0x1) { | |
412a3c41 | 37 | u32 eax, ebx, ecx, edx, offset; |
4344ee98 | 38 | cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx); |
412a3c41 PB |
39 | offset = compacted ? ret : ebx; |
40 | ret = max(ret, offset + eax); | |
4344ee98 PB |
41 | } |
42 | ||
43 | xstate_bv >>= 1; | |
44 | feature_bit++; | |
45 | } | |
46 | ||
47 | return ret; | |
48 | } | |
49 | ||
a87036ad PB |
50 | bool kvm_mpx_supported(void) |
51 | { | |
52 | return ((host_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR)) | |
53 | && kvm_x86_ops->mpx_supported()); | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(kvm_mpx_supported); | |
56 | ||
4ff41732 PB |
57 | u64 kvm_supported_xcr0(void) |
58 | { | |
59 | u64 xcr0 = KVM_SUPPORTED_XCR0 & host_xcr0; | |
60 | ||
a87036ad | 61 | if (!kvm_mpx_supported()) |
d91cab78 | 62 | xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR); |
4ff41732 PB |
63 | |
64 | return xcr0; | |
65 | } | |
66 | ||
5c404cab PB |
67 | #define F(x) bit(X86_FEATURE_##x) |
68 | ||
4504b5c9 LK |
69 | /* These are scattered features in cpufeatures.h. */ |
70 | #define KVM_CPUID_BIT_AVX512_4VNNIW 2 | |
71 | #define KVM_CPUID_BIT_AVX512_4FMAPS 3 | |
72 | #define KF(x) bit(KVM_CPUID_BIT_##x) | |
73 | ||
dd598091 | 74 | int kvm_update_cpuid(struct kvm_vcpu *vcpu) |
00b27a3e AK |
75 | { |
76 | struct kvm_cpuid_entry2 *best; | |
77 | struct kvm_lapic *apic = vcpu->arch.apic; | |
78 | ||
79 | best = kvm_find_cpuid_entry(vcpu, 1, 0); | |
80 | if (!best) | |
dd598091 | 81 | return 0; |
00b27a3e AK |
82 | |
83 | /* Update OSXSAVE bit */ | |
d366bf7e | 84 | if (boot_cpu_has(X86_FEATURE_XSAVE) && best->function == 0x1) { |
5c404cab | 85 | best->ecx &= ~F(OSXSAVE); |
00b27a3e | 86 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) |
5c404cab | 87 | best->ecx |= F(OSXSAVE); |
00b27a3e AK |
88 | } |
89 | ||
90 | if (apic) { | |
5c404cab | 91 | if (best->ecx & F(TSC_DEADLINE_TIMER)) |
00b27a3e AK |
92 | apic->lapic_timer.timer_mode_mask = 3 << 17; |
93 | else | |
94 | apic->lapic_timer.timer_mode_mask = 1 << 17; | |
95 | } | |
f5132b01 | 96 | |
b9baba86 HH |
97 | best = kvm_find_cpuid_entry(vcpu, 7, 0); |
98 | if (best) { | |
99 | /* Update OSPKE bit */ | |
100 | if (boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7) { | |
101 | best->ecx &= ~F(OSPKE); | |
102 | if (kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) | |
103 | best->ecx |= F(OSPKE); | |
104 | } | |
105 | } | |
106 | ||
d7876f1b | 107 | best = kvm_find_cpuid_entry(vcpu, 0xD, 0); |
4344ee98 | 108 | if (!best) { |
d7876f1b | 109 | vcpu->arch.guest_supported_xcr0 = 0; |
4344ee98 PB |
110 | vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; |
111 | } else { | |
d7876f1b PB |
112 | vcpu->arch.guest_supported_xcr0 = |
113 | (best->eax | ((u64)best->edx << 32)) & | |
4ff41732 | 114 | kvm_supported_xcr0(); |
56c103ec | 115 | vcpu->arch.guest_xstate_size = best->ebx = |
412a3c41 | 116 | xstate_required_size(vcpu->arch.xcr0, false); |
4344ee98 | 117 | } |
d7876f1b | 118 | |
412a3c41 PB |
119 | best = kvm_find_cpuid_entry(vcpu, 0xD, 1); |
120 | if (best && (best->eax & (F(XSAVES) | F(XSAVEC)))) | |
121 | best->ebx = xstate_required_size(vcpu->arch.xcr0, true); | |
122 | ||
5a5fbdc0 | 123 | if (use_eager_fpu()) |
370777da | 124 | kvm_x86_ops->fpu_activate(vcpu); |
c447e76b | 125 | |
dd598091 NA |
126 | /* |
127 | * The existing code assumes virtual address is 48-bit in the canonical | |
128 | * address checks; exit if it is ever changed. | |
129 | */ | |
130 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); | |
131 | if (best && ((best->eax & 0xff00) >> 8) != 48 && | |
132 | ((best->eax & 0xff00) >> 8) != 0) | |
133 | return -EINVAL; | |
134 | ||
5a4f55cd EK |
135 | /* Update physical-address width */ |
136 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); | |
137 | ||
c6702c9d | 138 | kvm_pmu_refresh(vcpu); |
dd598091 | 139 | return 0; |
00b27a3e AK |
140 | } |
141 | ||
142 | static int is_efer_nx(void) | |
143 | { | |
144 | unsigned long long efer = 0; | |
145 | ||
146 | rdmsrl_safe(MSR_EFER, &efer); | |
147 | return efer & EFER_NX; | |
148 | } | |
149 | ||
150 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) | |
151 | { | |
152 | int i; | |
153 | struct kvm_cpuid_entry2 *e, *entry; | |
154 | ||
155 | entry = NULL; | |
156 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { | |
157 | e = &vcpu->arch.cpuid_entries[i]; | |
158 | if (e->function == 0x80000001) { | |
159 | entry = e; | |
160 | break; | |
161 | } | |
162 | } | |
5c404cab PB |
163 | if (entry && (entry->edx & F(NX)) && !is_efer_nx()) { |
164 | entry->edx &= ~F(NX); | |
00b27a3e AK |
165 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
166 | } | |
167 | } | |
168 | ||
5a4f55cd EK |
169 | int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu) |
170 | { | |
171 | struct kvm_cpuid_entry2 *best; | |
172 | ||
173 | best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0); | |
174 | if (!best || best->eax < 0x80000008) | |
175 | goto not_found; | |
176 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); | |
177 | if (best) | |
178 | return best->eax & 0xff; | |
179 | not_found: | |
180 | return 36; | |
181 | } | |
182 | EXPORT_SYMBOL_GPL(cpuid_query_maxphyaddr); | |
183 | ||
00b27a3e AK |
184 | /* when an old userspace process fills a new kernel module */ |
185 | int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, | |
186 | struct kvm_cpuid *cpuid, | |
187 | struct kvm_cpuid_entry __user *entries) | |
188 | { | |
189 | int r, i; | |
83676e92 | 190 | struct kvm_cpuid_entry *cpuid_entries = NULL; |
00b27a3e AK |
191 | |
192 | r = -E2BIG; | |
193 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
194 | goto out; | |
195 | r = -ENOMEM; | |
83676e92 PB |
196 | if (cpuid->nent) { |
197 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * | |
198 | cpuid->nent); | |
199 | if (!cpuid_entries) | |
200 | goto out; | |
201 | r = -EFAULT; | |
202 | if (copy_from_user(cpuid_entries, entries, | |
203 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) | |
204 | goto out; | |
205 | } | |
00b27a3e AK |
206 | for (i = 0; i < cpuid->nent; i++) { |
207 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; | |
208 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; | |
209 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; | |
210 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; | |
211 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; | |
212 | vcpu->arch.cpuid_entries[i].index = 0; | |
213 | vcpu->arch.cpuid_entries[i].flags = 0; | |
214 | vcpu->arch.cpuid_entries[i].padding[0] = 0; | |
215 | vcpu->arch.cpuid_entries[i].padding[1] = 0; | |
216 | vcpu->arch.cpuid_entries[i].padding[2] = 0; | |
217 | } | |
218 | vcpu->arch.cpuid_nent = cpuid->nent; | |
219 | cpuid_fix_nx_cap(vcpu); | |
00b27a3e AK |
220 | kvm_apic_set_version(vcpu); |
221 | kvm_x86_ops->cpuid_update(vcpu); | |
dd598091 | 222 | r = kvm_update_cpuid(vcpu); |
00b27a3e | 223 | |
00b27a3e | 224 | out: |
83676e92 | 225 | vfree(cpuid_entries); |
00b27a3e AK |
226 | return r; |
227 | } | |
228 | ||
229 | int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, | |
230 | struct kvm_cpuid2 *cpuid, | |
231 | struct kvm_cpuid_entry2 __user *entries) | |
232 | { | |
233 | int r; | |
234 | ||
235 | r = -E2BIG; | |
236 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
237 | goto out; | |
238 | r = -EFAULT; | |
239 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, | |
240 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) | |
241 | goto out; | |
242 | vcpu->arch.cpuid_nent = cpuid->nent; | |
243 | kvm_apic_set_version(vcpu); | |
244 | kvm_x86_ops->cpuid_update(vcpu); | |
dd598091 | 245 | r = kvm_update_cpuid(vcpu); |
00b27a3e AK |
246 | out: |
247 | return r; | |
248 | } | |
249 | ||
250 | int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, | |
251 | struct kvm_cpuid2 *cpuid, | |
252 | struct kvm_cpuid_entry2 __user *entries) | |
253 | { | |
254 | int r; | |
255 | ||
256 | r = -E2BIG; | |
257 | if (cpuid->nent < vcpu->arch.cpuid_nent) | |
258 | goto out; | |
259 | r = -EFAULT; | |
260 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, | |
261 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) | |
262 | goto out; | |
263 | return 0; | |
264 | ||
265 | out: | |
266 | cpuid->nent = vcpu->arch.cpuid_nent; | |
267 | return r; | |
268 | } | |
269 | ||
270 | static void cpuid_mask(u32 *word, int wordnum) | |
271 | { | |
272 | *word &= boot_cpu_data.x86_capability[wordnum]; | |
273 | } | |
274 | ||
275 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |
276 | u32 index) | |
277 | { | |
278 | entry->function = function; | |
279 | entry->index = index; | |
280 | cpuid_count(entry->function, entry->index, | |
281 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); | |
282 | entry->flags = 0; | |
283 | } | |
284 | ||
9c15bb1d BP |
285 | static int __do_cpuid_ent_emulated(struct kvm_cpuid_entry2 *entry, |
286 | u32 func, u32 index, int *nent, int maxnent) | |
287 | { | |
84cffe49 BP |
288 | switch (func) { |
289 | case 0: | |
290 | entry->eax = 1; /* only one leaf currently */ | |
291 | ++*nent; | |
292 | break; | |
293 | case 1: | |
294 | entry->ecx = F(MOVBE); | |
295 | ++*nent; | |
296 | break; | |
297 | default: | |
298 | break; | |
299 | } | |
300 | ||
301 | entry->function = func; | |
302 | entry->index = index; | |
303 | ||
9c15bb1d BP |
304 | return 0; |
305 | } | |
306 | ||
307 | static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |
308 | u32 index, int *nent, int maxnent) | |
00b27a3e | 309 | { |
831bf664 | 310 | int r; |
00b27a3e AK |
311 | unsigned f_nx = is_efer_nx() ? F(NX) : 0; |
312 | #ifdef CONFIG_X86_64 | |
313 | unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL) | |
314 | ? F(GBPAGES) : 0; | |
315 | unsigned f_lm = F(LM); | |
316 | #else | |
317 | unsigned f_gbpages = 0; | |
318 | unsigned f_lm = 0; | |
319 | #endif | |
320 | unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0; | |
ad756a16 | 321 | unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0; |
a87036ad | 322 | unsigned f_mpx = kvm_mpx_supported() ? F(MPX) : 0; |
55412b2e | 323 | unsigned f_xsaves = kvm_x86_ops->xsaves_supported() ? F(XSAVES) : 0; |
00b27a3e AK |
324 | |
325 | /* cpuid 1.edx */ | |
e0b18ef7 | 326 | const u32 kvm_cpuid_1_edx_x86_features = |
00b27a3e AK |
327 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
328 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | | |
329 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | | |
330 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | | |
840d2830 | 331 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) | |
00b27a3e AK |
332 | 0 /* Reserved, DS, ACPI */ | F(MMX) | |
333 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | | |
334 | 0 /* HTT, TM, Reserved, PBE */; | |
335 | /* cpuid 0x80000001.edx */ | |
e0b18ef7 | 336 | const u32 kvm_cpuid_8000_0001_edx_x86_features = |
00b27a3e AK |
337 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
338 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | | |
339 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | | |
340 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | | |
341 | F(PAT) | F(PSE36) | 0 /* Reserved */ | | |
342 | f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | | |
343 | F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp | | |
344 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW); | |
345 | /* cpuid 1.ecx */ | |
e0b18ef7 | 346 | const u32 kvm_cpuid_1_ecx_x86_features = |
87c00572 GS |
347 | /* NOTE: MONITOR (and MWAIT) are emulated as NOP, |
348 | * but *not* advertised to guests via CPUID ! */ | |
00b27a3e AK |
349 | F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ | |
350 | 0 /* DS-CPL, VMX, SMX, EST */ | | |
351 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | | |
fb215366 | 352 | F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ | |
ad756a16 | 353 | F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) | |
00b27a3e AK |
354 | F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) | |
355 | 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) | | |
356 | F(F16C) | F(RDRAND); | |
357 | /* cpuid 0x80000001.ecx */ | |
e0b18ef7 | 358 | const u32 kvm_cpuid_8000_0001_ecx_x86_features = |
00b27a3e AK |
359 | F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ | |
360 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | | |
2b036c6b | 361 | F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) | |
00b27a3e AK |
362 | 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM); |
363 | ||
364 | /* cpuid 0xC0000001.edx */ | |
e0b18ef7 | 365 | const u32 kvm_cpuid_C000_0001_edx_x86_features = |
00b27a3e AK |
366 | F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) | |
367 | F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) | | |
368 | F(PMM) | F(PMM_EN); | |
369 | ||
370 | /* cpuid 7.0.ebx */ | |
e0b18ef7 | 371 | const u32 kvm_cpuid_7_0_ebx_x86_features = |
83c52915 | 372 | F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) | |
390bd528 | 373 | F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) | |
612263b3 | 374 | F(ADX) | F(SMAP) | F(AVX512F) | F(AVX512PF) | F(AVX512ER) | |
8e3562f6 LK |
375 | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) | |
376 | F(AVX512BW) | F(AVX512VL); | |
00b27a3e | 377 | |
b65d6e17 | 378 | /* cpuid 0xD.1.eax */ |
e0b18ef7 | 379 | const u32 kvm_cpuid_D_1_eax_x86_features = |
55412b2e | 380 | F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | f_xsaves; |
b65d6e17 | 381 | |
b9baba86 HH |
382 | /* cpuid 7.0.ecx*/ |
383 | const u32 kvm_cpuid_7_0_ecx_x86_features = F(PKU) | 0 /*OSPKE*/; | |
384 | ||
4504b5c9 LK |
385 | /* cpuid 7.0.edx*/ |
386 | const u32 kvm_cpuid_7_0_edx_x86_features = | |
387 | KF(AVX512_4VNNIW) | KF(AVX512_4FMAPS); | |
388 | ||
00b27a3e AK |
389 | /* all calls to cpuid_count() should be made on the same cpu */ |
390 | get_cpu(); | |
831bf664 SL |
391 | |
392 | r = -E2BIG; | |
393 | ||
394 | if (*nent >= maxnent) | |
395 | goto out; | |
396 | ||
00b27a3e AK |
397 | do_cpuid_1_ent(entry, function, index); |
398 | ++*nent; | |
399 | ||
400 | switch (function) { | |
401 | case 0: | |
402 | entry->eax = min(entry->eax, (u32)0xd); | |
403 | break; | |
404 | case 1: | |
e0b18ef7 HH |
405 | entry->edx &= kvm_cpuid_1_edx_x86_features; |
406 | cpuid_mask(&entry->edx, CPUID_1_EDX); | |
407 | entry->ecx &= kvm_cpuid_1_ecx_x86_features; | |
408 | cpuid_mask(&entry->ecx, CPUID_1_ECX); | |
00b27a3e AK |
409 | /* we support x2apic emulation even if host does not support |
410 | * it since we emulate x2apic in software */ | |
411 | entry->ecx |= F(X2APIC); | |
412 | break; | |
413 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands | |
414 | * may return different values. This forces us to get_cpu() before | |
415 | * issuing the first command, and also to emulate this annoying behavior | |
416 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ | |
417 | case 2: { | |
418 | int t, times = entry->eax & 0xff; | |
419 | ||
420 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
421 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; | |
831bf664 SL |
422 | for (t = 1; t < times; ++t) { |
423 | if (*nent >= maxnent) | |
424 | goto out; | |
425 | ||
00b27a3e AK |
426 | do_cpuid_1_ent(&entry[t], function, 0); |
427 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
428 | ++*nent; | |
429 | } | |
430 | break; | |
431 | } | |
432 | /* function 4 has additional index. */ | |
433 | case 4: { | |
434 | int i, cache_type; | |
435 | ||
436 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
437 | /* read more entries until cache_type is zero */ | |
831bf664 SL |
438 | for (i = 1; ; ++i) { |
439 | if (*nent >= maxnent) | |
440 | goto out; | |
441 | ||
00b27a3e AK |
442 | cache_type = entry[i - 1].eax & 0x1f; |
443 | if (!cache_type) | |
444 | break; | |
445 | do_cpuid_1_ent(&entry[i], function, i); | |
446 | entry[i].flags |= | |
447 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
448 | ++*nent; | |
449 | } | |
450 | break; | |
451 | } | |
e453aa0f JK |
452 | case 6: /* Thermal management */ |
453 | entry->eax = 0x4; /* allow ARAT */ | |
454 | entry->ebx = 0; | |
455 | entry->ecx = 0; | |
456 | entry->edx = 0; | |
457 | break; | |
00b27a3e AK |
458 | case 7: { |
459 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
bbbda795 | 460 | /* Mask ebx against host capability word 9 */ |
00b27a3e | 461 | if (index == 0) { |
e0b18ef7 HH |
462 | entry->ebx &= kvm_cpuid_7_0_ebx_x86_features; |
463 | cpuid_mask(&entry->ebx, CPUID_7_0_EBX); | |
ba904635 WA |
464 | // TSC_ADJUST is emulated |
465 | entry->ebx |= F(TSC_ADJUST); | |
b9baba86 HH |
466 | entry->ecx &= kvm_cpuid_7_0_ecx_x86_features; |
467 | cpuid_mask(&entry->ecx, CPUID_7_ECX); | |
468 | /* PKU is not yet implemented for shadow paging. */ | |
469 | if (!tdp_enabled) | |
470 | entry->ecx &= ~F(PKU); | |
4504b5c9 LK |
471 | entry->edx &= kvm_cpuid_7_0_edx_x86_features; |
472 | entry->edx &= get_scattered_cpuid_leaf(7, 0, CPUID_EDX); | |
b9baba86 | 473 | } else { |
00b27a3e | 474 | entry->ebx = 0; |
b9baba86 | 475 | entry->ecx = 0; |
4504b5c9 | 476 | entry->edx = 0; |
b9baba86 | 477 | } |
00b27a3e | 478 | entry->eax = 0; |
00b27a3e AK |
479 | break; |
480 | } | |
481 | case 9: | |
482 | break; | |
a6c06ed1 GN |
483 | case 0xa: { /* Architectural Performance Monitoring */ |
484 | struct x86_pmu_capability cap; | |
485 | union cpuid10_eax eax; | |
486 | union cpuid10_edx edx; | |
487 | ||
488 | perf_get_x86_pmu_capability(&cap); | |
489 | ||
490 | /* | |
491 | * Only support guest architectural pmu on a host | |
492 | * with architectural pmu. | |
493 | */ | |
494 | if (!cap.version) | |
495 | memset(&cap, 0, sizeof(cap)); | |
496 | ||
497 | eax.split.version_id = min(cap.version, 2); | |
498 | eax.split.num_counters = cap.num_counters_gp; | |
499 | eax.split.bit_width = cap.bit_width_gp; | |
500 | eax.split.mask_length = cap.events_mask_len; | |
501 | ||
502 | edx.split.num_counters_fixed = cap.num_counters_fixed; | |
503 | edx.split.bit_width_fixed = cap.bit_width_fixed; | |
504 | edx.split.reserved = 0; | |
505 | ||
506 | entry->eax = eax.full; | |
507 | entry->ebx = cap.events_mask; | |
508 | entry->ecx = 0; | |
509 | entry->edx = edx.full; | |
510 | break; | |
511 | } | |
00b27a3e AK |
512 | /* function 0xb has additional index. */ |
513 | case 0xb: { | |
514 | int i, level_type; | |
515 | ||
516 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
517 | /* read more entries until level_type is zero */ | |
831bf664 SL |
518 | for (i = 1; ; ++i) { |
519 | if (*nent >= maxnent) | |
520 | goto out; | |
521 | ||
00b27a3e AK |
522 | level_type = entry[i - 1].ecx & 0xff00; |
523 | if (!level_type) | |
524 | break; | |
525 | do_cpuid_1_ent(&entry[i], function, i); | |
526 | entry[i].flags |= | |
527 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
528 | ++*nent; | |
529 | } | |
530 | break; | |
531 | } | |
532 | case 0xd: { | |
533 | int idx, i; | |
4ff41732 | 534 | u64 supported = kvm_supported_xcr0(); |
00b27a3e | 535 | |
4ff41732 | 536 | entry->eax &= supported; |
e08e8336 RK |
537 | entry->ebx = xstate_required_size(supported, false); |
538 | entry->ecx = entry->ebx; | |
4ff41732 | 539 | entry->edx &= supported >> 32; |
00b27a3e | 540 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
b65d6e17 PB |
541 | if (!supported) |
542 | break; | |
543 | ||
831bf664 | 544 | for (idx = 1, i = 1; idx < 64; ++idx) { |
4ff41732 | 545 | u64 mask = ((u64)1 << idx); |
831bf664 SL |
546 | if (*nent >= maxnent) |
547 | goto out; | |
548 | ||
00b27a3e | 549 | do_cpuid_1_ent(&entry[i], function, idx); |
412a3c41 | 550 | if (idx == 1) { |
e0b18ef7 | 551 | entry[i].eax &= kvm_cpuid_D_1_eax_x86_features; |
316314ca | 552 | cpuid_mask(&entry[i].eax, CPUID_D_1_EAX); |
412a3c41 PB |
553 | entry[i].ebx = 0; |
554 | if (entry[i].eax & (F(XSAVES)|F(XSAVEC))) | |
555 | entry[i].ebx = | |
556 | xstate_required_size(supported, | |
557 | true); | |
404e0a19 PB |
558 | } else { |
559 | if (entry[i].eax == 0 || !(supported & mask)) | |
560 | continue; | |
561 | if (WARN_ON_ONCE(entry[i].ecx & 1)) | |
562 | continue; | |
563 | } | |
564 | entry[i].ecx = 0; | |
565 | entry[i].edx = 0; | |
00b27a3e AK |
566 | entry[i].flags |= |
567 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
568 | ++*nent; | |
569 | ++i; | |
570 | } | |
571 | break; | |
572 | } | |
573 | case KVM_CPUID_SIGNATURE: { | |
326d07cb MK |
574 | static const char signature[12] = "KVMKVMKVM\0\0"; |
575 | const u32 *sigptr = (const u32 *)signature; | |
57c22e5f | 576 | entry->eax = KVM_CPUID_FEATURES; |
00b27a3e AK |
577 | entry->ebx = sigptr[0]; |
578 | entry->ecx = sigptr[1]; | |
579 | entry->edx = sigptr[2]; | |
580 | break; | |
581 | } | |
582 | case KVM_CPUID_FEATURES: | |
583 | entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) | | |
584 | (1 << KVM_FEATURE_NOP_IO_DELAY) | | |
585 | (1 << KVM_FEATURE_CLOCKSOURCE2) | | |
586 | (1 << KVM_FEATURE_ASYNC_PF) | | |
ae7a2a3f | 587 | (1 << KVM_FEATURE_PV_EOI) | |
6aef266c SV |
588 | (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) | |
589 | (1 << KVM_FEATURE_PV_UNHALT); | |
00b27a3e AK |
590 | |
591 | if (sched_info_on()) | |
592 | entry->eax |= (1 << KVM_FEATURE_STEAL_TIME); | |
593 | ||
594 | entry->ebx = 0; | |
595 | entry->ecx = 0; | |
596 | entry->edx = 0; | |
597 | break; | |
598 | case 0x80000000: | |
599 | entry->eax = min(entry->eax, 0x8000001a); | |
600 | break; | |
601 | case 0x80000001: | |
e0b18ef7 HH |
602 | entry->edx &= kvm_cpuid_8000_0001_edx_x86_features; |
603 | cpuid_mask(&entry->edx, CPUID_8000_0001_EDX); | |
604 | entry->ecx &= kvm_cpuid_8000_0001_ecx_x86_features; | |
605 | cpuid_mask(&entry->ecx, CPUID_8000_0001_ECX); | |
00b27a3e | 606 | break; |
e4c9a5a1 MT |
607 | case 0x80000007: /* Advanced power management */ |
608 | /* invariant TSC is CPUID.80000007H:EDX[8] */ | |
609 | entry->edx &= (1 << 8); | |
610 | /* mask against host */ | |
611 | entry->edx &= boot_cpu_data.x86_power; | |
612 | entry->eax = entry->ebx = entry->ecx = 0; | |
613 | break; | |
00b27a3e AK |
614 | case 0x80000008: { |
615 | unsigned g_phys_as = (entry->eax >> 16) & 0xff; | |
616 | unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U); | |
617 | unsigned phys_as = entry->eax & 0xff; | |
618 | ||
619 | if (!g_phys_as) | |
620 | g_phys_as = phys_as; | |
621 | entry->eax = g_phys_as | (virt_as << 8); | |
622 | entry->ebx = entry->edx = 0; | |
623 | break; | |
624 | } | |
625 | case 0x80000019: | |
626 | entry->ecx = entry->edx = 0; | |
627 | break; | |
628 | case 0x8000001a: | |
629 | break; | |
630 | case 0x8000001d: | |
631 | break; | |
632 | /*Add support for Centaur's CPUID instruction*/ | |
633 | case 0xC0000000: | |
634 | /*Just support up to 0xC0000004 now*/ | |
635 | entry->eax = min(entry->eax, 0xC0000004); | |
636 | break; | |
637 | case 0xC0000001: | |
e0b18ef7 HH |
638 | entry->edx &= kvm_cpuid_C000_0001_edx_x86_features; |
639 | cpuid_mask(&entry->edx, CPUID_C000_0001_EDX); | |
00b27a3e AK |
640 | break; |
641 | case 3: /* Processor serial number */ | |
642 | case 5: /* MONITOR/MWAIT */ | |
00b27a3e AK |
643 | case 0xC0000002: |
644 | case 0xC0000003: | |
645 | case 0xC0000004: | |
646 | default: | |
647 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; | |
648 | break; | |
649 | } | |
650 | ||
651 | kvm_x86_ops->set_supported_cpuid(function, entry); | |
652 | ||
831bf664 SL |
653 | r = 0; |
654 | ||
655 | out: | |
00b27a3e | 656 | put_cpu(); |
831bf664 SL |
657 | |
658 | return r; | |
00b27a3e AK |
659 | } |
660 | ||
9c15bb1d BP |
661 | static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 func, |
662 | u32 idx, int *nent, int maxnent, unsigned int type) | |
663 | { | |
664 | if (type == KVM_GET_EMULATED_CPUID) | |
665 | return __do_cpuid_ent_emulated(entry, func, idx, nent, maxnent); | |
666 | ||
667 | return __do_cpuid_ent(entry, func, idx, nent, maxnent); | |
668 | } | |
669 | ||
00b27a3e AK |
670 | #undef F |
671 | ||
831bf664 SL |
672 | struct kvm_cpuid_param { |
673 | u32 func; | |
674 | u32 idx; | |
675 | bool has_leaf_count; | |
326d07cb | 676 | bool (*qualifier)(const struct kvm_cpuid_param *param); |
831bf664 SL |
677 | }; |
678 | ||
326d07cb | 679 | static bool is_centaur_cpu(const struct kvm_cpuid_param *param) |
831bf664 SL |
680 | { |
681 | return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR; | |
682 | } | |
683 | ||
9c15bb1d BP |
684 | static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries, |
685 | __u32 num_entries, unsigned int ioctl_type) | |
686 | { | |
687 | int i; | |
1b2ca422 | 688 | __u32 pad[3]; |
9c15bb1d BP |
689 | |
690 | if (ioctl_type != KVM_GET_EMULATED_CPUID) | |
691 | return false; | |
692 | ||
693 | /* | |
694 | * We want to make sure that ->padding is being passed clean from | |
695 | * userspace in case we want to use it for something in the future. | |
696 | * | |
697 | * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we | |
698 | * have to give ourselves satisfied only with the emulated side. /me | |
699 | * sheds a tear. | |
700 | */ | |
701 | for (i = 0; i < num_entries; i++) { | |
1b2ca422 BP |
702 | if (copy_from_user(pad, entries[i].padding, sizeof(pad))) |
703 | return true; | |
704 | ||
705 | if (pad[0] || pad[1] || pad[2]) | |
9c15bb1d BP |
706 | return true; |
707 | } | |
708 | return false; | |
709 | } | |
710 | ||
711 | int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, | |
712 | struct kvm_cpuid_entry2 __user *entries, | |
713 | unsigned int type) | |
00b27a3e AK |
714 | { |
715 | struct kvm_cpuid_entry2 *cpuid_entries; | |
831bf664 | 716 | int limit, nent = 0, r = -E2BIG, i; |
00b27a3e | 717 | u32 func; |
326d07cb | 718 | static const struct kvm_cpuid_param param[] = { |
831bf664 SL |
719 | { .func = 0, .has_leaf_count = true }, |
720 | { .func = 0x80000000, .has_leaf_count = true }, | |
721 | { .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true }, | |
722 | { .func = KVM_CPUID_SIGNATURE }, | |
723 | { .func = KVM_CPUID_FEATURES }, | |
724 | }; | |
00b27a3e AK |
725 | |
726 | if (cpuid->nent < 1) | |
727 | goto out; | |
728 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
729 | cpuid->nent = KVM_MAX_CPUID_ENTRIES; | |
9c15bb1d BP |
730 | |
731 | if (sanity_check_entries(entries, cpuid->nent, type)) | |
732 | return -EINVAL; | |
733 | ||
00b27a3e | 734 | r = -ENOMEM; |
84cffe49 | 735 | cpuid_entries = vzalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); |
00b27a3e AK |
736 | if (!cpuid_entries) |
737 | goto out; | |
738 | ||
831bf664 SL |
739 | r = 0; |
740 | for (i = 0; i < ARRAY_SIZE(param); i++) { | |
326d07cb | 741 | const struct kvm_cpuid_param *ent = ¶m[i]; |
00b27a3e | 742 | |
831bf664 SL |
743 | if (ent->qualifier && !ent->qualifier(ent)) |
744 | continue; | |
00b27a3e | 745 | |
831bf664 | 746 | r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx, |
9c15bb1d | 747 | &nent, cpuid->nent, type); |
00b27a3e | 748 | |
831bf664 | 749 | if (r) |
00b27a3e AK |
750 | goto out_free; |
751 | ||
831bf664 SL |
752 | if (!ent->has_leaf_count) |
753 | continue; | |
754 | ||
00b27a3e | 755 | limit = cpuid_entries[nent - 1].eax; |
831bf664 SL |
756 | for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func) |
757 | r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx, | |
9c15bb1d | 758 | &nent, cpuid->nent, type); |
00b27a3e | 759 | |
831bf664 | 760 | if (r) |
00b27a3e AK |
761 | goto out_free; |
762 | } | |
763 | ||
00b27a3e AK |
764 | r = -EFAULT; |
765 | if (copy_to_user(entries, cpuid_entries, | |
766 | nent * sizeof(struct kvm_cpuid_entry2))) | |
767 | goto out_free; | |
768 | cpuid->nent = nent; | |
769 | r = 0; | |
770 | ||
771 | out_free: | |
772 | vfree(cpuid_entries); | |
773 | out: | |
774 | return r; | |
775 | } | |
776 | ||
777 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) | |
778 | { | |
779 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; | |
780 | int j, nent = vcpu->arch.cpuid_nent; | |
781 | ||
782 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; | |
783 | /* when no next entry is found, the current entry[i] is reselected */ | |
784 | for (j = i + 1; ; j = (j + 1) % nent) { | |
785 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; | |
786 | if (ej->function == e->function) { | |
787 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; | |
788 | return j; | |
789 | } | |
790 | } | |
791 | return 0; /* silence gcc, even though control never reaches here */ | |
792 | } | |
793 | ||
794 | /* find an entry with matching function, matching index (if needed), and that | |
795 | * should be read next (if it's stateful) */ | |
796 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, | |
797 | u32 function, u32 index) | |
798 | { | |
799 | if (e->function != function) | |
800 | return 0; | |
801 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) | |
802 | return 0; | |
803 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && | |
804 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) | |
805 | return 0; | |
806 | return 1; | |
807 | } | |
808 | ||
809 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, | |
810 | u32 function, u32 index) | |
811 | { | |
812 | int i; | |
813 | struct kvm_cpuid_entry2 *best = NULL; | |
814 | ||
815 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { | |
816 | struct kvm_cpuid_entry2 *e; | |
817 | ||
818 | e = &vcpu->arch.cpuid_entries[i]; | |
819 | if (is_matching_cpuid_entry(e, function, index)) { | |
820 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) | |
821 | move_to_next_stateful_cpuid_entry(vcpu, i); | |
822 | best = e; | |
823 | break; | |
824 | } | |
825 | } | |
826 | return best; | |
827 | } | |
828 | EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry); | |
829 | ||
00b27a3e AK |
830 | /* |
831 | * If no match is found, check whether we exceed the vCPU's limit | |
832 | * and return the content of the highest valid _standard_ leaf instead. | |
833 | * This is to satisfy the CPUID specification. | |
834 | */ | |
835 | static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu, | |
836 | u32 function, u32 index) | |
837 | { | |
838 | struct kvm_cpuid_entry2 *maxlevel; | |
839 | ||
840 | maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0); | |
841 | if (!maxlevel || maxlevel->eax >= function) | |
842 | return NULL; | |
843 | if (function & 0x80000000) { | |
844 | maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0); | |
845 | if (!maxlevel) | |
846 | return NULL; | |
847 | } | |
848 | return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index); | |
849 | } | |
850 | ||
62046e5a | 851 | void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx) |
00b27a3e | 852 | { |
62046e5a | 853 | u32 function = *eax, index = *ecx; |
00b27a3e AK |
854 | struct kvm_cpuid_entry2 *best; |
855 | ||
00b27a3e AK |
856 | best = kvm_find_cpuid_entry(vcpu, function, index); |
857 | ||
858 | if (!best) | |
859 | best = check_cpuid_limit(vcpu, function, index); | |
860 | ||
bc613494 MT |
861 | /* |
862 | * Perfmon not yet supported for L2 guest. | |
863 | */ | |
864 | if (is_guest_mode(vcpu) && function == 0xa) | |
865 | best = NULL; | |
866 | ||
00b27a3e | 867 | if (best) { |
62046e5a AK |
868 | *eax = best->eax; |
869 | *ebx = best->ebx; | |
870 | *ecx = best->ecx; | |
871 | *edx = best->edx; | |
872 | } else | |
873 | *eax = *ebx = *ecx = *edx = 0; | |
a9d4e439 | 874 | trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx); |
62046e5a | 875 | } |
66f7b72e | 876 | EXPORT_SYMBOL_GPL(kvm_cpuid); |
62046e5a AK |
877 | |
878 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) | |
879 | { | |
1e13175b | 880 | u32 eax, ebx, ecx, edx; |
62046e5a | 881 | |
1e13175b | 882 | eax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
62046e5a AK |
883 | ecx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
884 | kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx); | |
885 | kvm_register_write(vcpu, VCPU_REGS_RAX, eax); | |
886 | kvm_register_write(vcpu, VCPU_REGS_RBX, ebx); | |
887 | kvm_register_write(vcpu, VCPU_REGS_RCX, ecx); | |
888 | kvm_register_write(vcpu, VCPU_REGS_RDX, edx); | |
00b27a3e | 889 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
00b27a3e AK |
890 | } |
891 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |