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