mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / arch / x86 / kvm / cpuid.c
blob6620ac3072157aadd6ee9ef4b6e297eccaa9ec46
1 /*
2 * Kernel-based Virtual Machine driver for Linux
3 * cpuid support routines
5 * derived from arch/x86/kvm/x86.c
7 * Copyright 2011 Red Hat, Inc. and/or its affiliates.
8 * Copyright IBM Corporation, 2008
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
15 #include <linux/kvm_host.h>
16 #include <linux/module.h>
17 #include <linux/vmalloc.h>
18 #include <linux/uaccess.h>
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"
26 int kvm_update_cpuid(struct kvm_vcpu *vcpu)
28 struct kvm_cpuid_entry2 *best;
29 struct kvm_lapic *apic = vcpu->arch.apic;
31 best = kvm_find_cpuid_entry(vcpu, 1, 0);
32 if (!best)
33 return 0;
35 /* Update OSXSAVE bit */
36 if (cpu_has_xsave && best->function == 0x1) {
37 best->ecx &= ~(bit(X86_FEATURE_OSXSAVE));
38 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
39 best->ecx |= bit(X86_FEATURE_OSXSAVE);
42 if (apic) {
43 if (best->ecx & bit(X86_FEATURE_TSC_DEADLINE_TIMER))
44 apic->lapic_timer.timer_mode_mask = 3 << 17;
45 else
46 apic->lapic_timer.timer_mode_mask = 1 << 17;
49 /* The existing code assumes virtual address is 48-bit in the canonical
50 * address checks; exit if it is ever changed */
51 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
52 if (best && ((best->eax & 0xff00) >> 8) != 48 &&
53 ((best->eax & 0xff00) >> 8) != 0)
54 return -EINVAL;
56 kvm_pmu_cpuid_update(vcpu);
57 return 0;
60 static int is_efer_nx(void)
62 unsigned long long efer = 0;
64 rdmsrl_safe(MSR_EFER, &efer);
65 return efer & EFER_NX;
68 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
70 int i;
71 struct kvm_cpuid_entry2 *e, *entry;
73 entry = NULL;
74 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
75 e = &vcpu->arch.cpuid_entries[i];
76 if (e->function == 0x80000001) {
77 entry = e;
78 break;
81 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
82 entry->edx &= ~(1 << 20);
83 printk(KERN_INFO "kvm: guest NX capability removed\n");
87 /* when an old userspace process fills a new kernel module */
88 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
89 struct kvm_cpuid *cpuid,
90 struct kvm_cpuid_entry __user *entries)
92 int r, i;
93 struct kvm_cpuid_entry *cpuid_entries;
95 r = -E2BIG;
96 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
97 goto out;
98 r = -ENOMEM;
99 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
100 if (!cpuid_entries)
101 goto out;
102 r = -EFAULT;
103 if (copy_from_user(cpuid_entries, entries,
104 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
105 goto out_free;
106 for (i = 0; i < cpuid->nent; i++) {
107 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
108 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
109 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
110 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
111 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
112 vcpu->arch.cpuid_entries[i].index = 0;
113 vcpu->arch.cpuid_entries[i].flags = 0;
114 vcpu->arch.cpuid_entries[i].padding[0] = 0;
115 vcpu->arch.cpuid_entries[i].padding[1] = 0;
116 vcpu->arch.cpuid_entries[i].padding[2] = 0;
118 vcpu->arch.cpuid_nent = cpuid->nent;
119 cpuid_fix_nx_cap(vcpu);
120 kvm_apic_set_version(vcpu);
121 kvm_x86_ops->cpuid_update(vcpu);
122 r = kvm_update_cpuid(vcpu);
124 out_free:
125 vfree(cpuid_entries);
126 out:
127 return r;
130 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
131 struct kvm_cpuid2 *cpuid,
132 struct kvm_cpuid_entry2 __user *entries)
134 int r;
136 r = -E2BIG;
137 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
138 goto out;
139 r = -EFAULT;
140 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
141 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
142 goto out;
143 vcpu->arch.cpuid_nent = cpuid->nent;
144 kvm_apic_set_version(vcpu);
145 kvm_x86_ops->cpuid_update(vcpu);
146 r = kvm_update_cpuid(vcpu);
147 out:
148 return r;
151 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
152 struct kvm_cpuid2 *cpuid,
153 struct kvm_cpuid_entry2 __user *entries)
155 int r;
157 r = -E2BIG;
158 if (cpuid->nent < vcpu->arch.cpuid_nent)
159 goto out;
160 r = -EFAULT;
161 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
162 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
163 goto out;
164 return 0;
166 out:
167 cpuid->nent = vcpu->arch.cpuid_nent;
168 return r;
171 static void cpuid_mask(u32 *word, int wordnum)
173 *word &= boot_cpu_data.x86_capability[wordnum];
176 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
177 u32 index)
179 entry->function = function;
180 entry->index = index;
181 cpuid_count(entry->function, entry->index,
182 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
183 entry->flags = 0;
186 static bool supported_xcr0_bit(unsigned bit)
188 u64 mask = ((u64)1 << bit);
190 return mask & (XSTATE_FP | XSTATE_SSE | XSTATE_YMM) & host_xcr0;
193 #define F(x) bit(X86_FEATURE_##x)
195 static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
196 u32 index, int *nent, int maxnent)
198 int r;
199 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
200 #ifdef CONFIG_X86_64
201 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
202 ? F(GBPAGES) : 0;
203 unsigned f_lm = F(LM);
204 #else
205 unsigned f_gbpages = 0;
206 unsigned f_lm = 0;
207 #endif
208 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
209 unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0;
211 /* cpuid 1.edx */
212 const u32 kvm_supported_word0_x86_features =
213 F(FPU) | F(VME) | F(DE) | F(PSE) |
214 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
215 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
216 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
217 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
218 0 /* Reserved, DS, ACPI */ | F(MMX) |
219 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
220 0 /* HTT, TM, Reserved, PBE */;
221 /* cpuid 0x80000001.edx */
222 const u32 kvm_supported_word1_x86_features =
223 F(FPU) | F(VME) | F(DE) | F(PSE) |
224 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
225 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
226 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
227 F(PAT) | F(PSE36) | 0 /* Reserved */ |
228 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
229 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
230 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
231 /* cpuid 1.ecx */
232 const u32 kvm_supported_word4_x86_features =
233 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
234 0 /* DS-CPL, VMX, SMX, EST */ |
235 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
236 F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
237 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
238 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
239 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
240 F(F16C) | F(RDRAND);
241 /* cpuid 0x80000001.ecx */
242 const u32 kvm_supported_word6_x86_features =
243 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
244 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
245 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
246 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
248 /* cpuid 0xC0000001.edx */
249 const u32 kvm_supported_word5_x86_features =
250 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
251 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
252 F(PMM) | F(PMM_EN);
254 /* cpuid 7.0.ebx */
255 const u32 kvm_supported_word9_x86_features =
256 F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
257 F(BMI2) | F(ERMS) | f_invpcid | F(RTM);
259 /* all calls to cpuid_count() should be made on the same cpu */
260 get_cpu();
262 r = -E2BIG;
264 if (*nent >= maxnent)
265 goto out;
267 do_cpuid_1_ent(entry, function, index);
268 ++*nent;
270 switch (function) {
271 case 0:
272 entry->eax = min(entry->eax, (u32)0xd);
273 break;
274 case 1:
275 entry->edx &= kvm_supported_word0_x86_features;
276 cpuid_mask(&entry->edx, 0);
277 entry->ecx &= kvm_supported_word4_x86_features;
278 cpuid_mask(&entry->ecx, 4);
279 /* we support x2apic emulation even if host does not support
280 * it since we emulate x2apic in software */
281 entry->ecx |= F(X2APIC);
282 break;
283 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
284 * may return different values. This forces us to get_cpu() before
285 * issuing the first command, and also to emulate this annoying behavior
286 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
287 case 2: {
288 int t, times = entry->eax & 0xff;
290 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
291 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
292 for (t = 1; t < times; ++t) {
293 if (*nent >= maxnent)
294 goto out;
296 do_cpuid_1_ent(&entry[t], function, 0);
297 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
298 ++*nent;
300 break;
302 /* function 4 has additional index. */
303 case 4: {
304 int i, cache_type;
306 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
307 /* read more entries until cache_type is zero */
308 for (i = 1; ; ++i) {
309 if (*nent >= maxnent)
310 goto out;
312 cache_type = entry[i - 1].eax & 0x1f;
313 if (!cache_type)
314 break;
315 do_cpuid_1_ent(&entry[i], function, i);
316 entry[i].flags |=
317 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
318 ++*nent;
320 break;
322 case 7: {
323 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
324 /* Mask ebx against host capability word 9 */
325 if (index == 0) {
326 entry->ebx &= kvm_supported_word9_x86_features;
327 cpuid_mask(&entry->ebx, 9);
328 // TSC_ADJUST is emulated
329 entry->ebx |= F(TSC_ADJUST);
330 } else
331 entry->ebx = 0;
332 entry->eax = 0;
333 entry->ecx = 0;
334 entry->edx = 0;
335 break;
337 case 9:
338 break;
339 case 0xa: { /* Architectural Performance Monitoring */
340 struct x86_pmu_capability cap;
341 union cpuid10_eax eax;
342 union cpuid10_edx edx;
344 perf_get_x86_pmu_capability(&cap);
347 * Only support guest architectural pmu on a host
348 * with architectural pmu.
350 if (!cap.version)
351 memset(&cap, 0, sizeof(cap));
353 eax.split.version_id = min(cap.version, 2);
354 eax.split.num_counters = cap.num_counters_gp;
355 eax.split.bit_width = cap.bit_width_gp;
356 eax.split.mask_length = cap.events_mask_len;
358 edx.split.num_counters_fixed = cap.num_counters_fixed;
359 edx.split.bit_width_fixed = cap.bit_width_fixed;
360 edx.split.reserved = 0;
362 entry->eax = eax.full;
363 entry->ebx = cap.events_mask;
364 entry->ecx = 0;
365 entry->edx = edx.full;
366 break;
368 /* function 0xb has additional index. */
369 case 0xb: {
370 int i, level_type;
372 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
373 /* read more entries until level_type is zero */
374 for (i = 1; ; ++i) {
375 if (*nent >= maxnent)
376 goto out;
378 level_type = entry[i - 1].ecx & 0xff00;
379 if (!level_type)
380 break;
381 do_cpuid_1_ent(&entry[i], function, i);
382 entry[i].flags |=
383 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
384 ++*nent;
386 break;
388 case 0xd: {
389 int idx, i;
391 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
392 for (idx = 1, i = 1; idx < 64; ++idx) {
393 if (*nent >= maxnent)
394 goto out;
396 do_cpuid_1_ent(&entry[i], function, idx);
397 if (entry[i].eax == 0 || !supported_xcr0_bit(idx))
398 continue;
399 entry[i].flags |=
400 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
401 ++*nent;
402 ++i;
404 break;
406 case KVM_CPUID_SIGNATURE: {
407 static const char signature[12] = "KVMKVMKVM\0\0";
408 const u32 *sigptr = (const u32 *)signature;
409 entry->eax = KVM_CPUID_FEATURES;
410 entry->ebx = sigptr[0];
411 entry->ecx = sigptr[1];
412 entry->edx = sigptr[2];
413 break;
415 case KVM_CPUID_FEATURES:
416 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
417 (1 << KVM_FEATURE_NOP_IO_DELAY) |
418 (1 << KVM_FEATURE_CLOCKSOURCE2) |
419 (1 << KVM_FEATURE_ASYNC_PF) |
420 (1 << KVM_FEATURE_PV_EOI) |
421 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
422 (1 << KVM_FEATURE_PV_UNHALT);
424 if (sched_info_on())
425 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
427 entry->ebx = 0;
428 entry->ecx = 0;
429 entry->edx = 0;
430 break;
431 case 0x80000000:
432 entry->eax = min(entry->eax, 0x8000001a);
433 break;
434 case 0x80000001:
435 entry->edx &= kvm_supported_word1_x86_features;
436 cpuid_mask(&entry->edx, 1);
437 entry->ecx &= kvm_supported_word6_x86_features;
438 cpuid_mask(&entry->ecx, 6);
439 break;
440 case 0x80000007: /* Advanced power management */
441 /* invariant TSC is CPUID.80000007H:EDX[8] */
442 entry->edx &= (1 << 8);
443 /* mask against host */
444 entry->edx &= boot_cpu_data.x86_power;
445 entry->eax = entry->ebx = entry->ecx = 0;
446 break;
447 case 0x80000008: {
448 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
449 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
450 unsigned phys_as = entry->eax & 0xff;
452 if (!g_phys_as)
453 g_phys_as = phys_as;
454 entry->eax = g_phys_as | (virt_as << 8);
455 entry->ebx = entry->edx = 0;
456 break;
458 case 0x80000019:
459 entry->ecx = entry->edx = 0;
460 break;
461 case 0x8000001a:
462 break;
463 case 0x8000001d:
464 break;
465 /*Add support for Centaur's CPUID instruction*/
466 case 0xC0000000:
467 /*Just support up to 0xC0000004 now*/
468 entry->eax = min(entry->eax, 0xC0000004);
469 break;
470 case 0xC0000001:
471 entry->edx &= kvm_supported_word5_x86_features;
472 cpuid_mask(&entry->edx, 5);
473 break;
474 case 3: /* Processor serial number */
475 case 5: /* MONITOR/MWAIT */
476 case 6: /* Thermal management */
477 case 0xC0000002:
478 case 0xC0000003:
479 case 0xC0000004:
480 default:
481 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
482 break;
485 kvm_x86_ops->set_supported_cpuid(function, entry);
487 r = 0;
489 out:
490 put_cpu();
492 return r;
495 #undef F
497 struct kvm_cpuid_param {
498 u32 func;
499 u32 idx;
500 bool has_leaf_count;
501 bool (*qualifier)(const struct kvm_cpuid_param *param);
504 static bool is_centaur_cpu(const struct kvm_cpuid_param *param)
506 return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR;
509 int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
510 struct kvm_cpuid_entry2 __user *entries)
512 struct kvm_cpuid_entry2 *cpuid_entries;
513 int limit, nent = 0, r = -E2BIG, i;
514 u32 func;
515 static const struct kvm_cpuid_param param[] = {
516 { .func = 0, .has_leaf_count = true },
517 { .func = 0x80000000, .has_leaf_count = true },
518 { .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true },
519 { .func = KVM_CPUID_SIGNATURE },
520 { .func = KVM_CPUID_FEATURES },
523 if (cpuid->nent < 1)
524 goto out;
525 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
526 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
527 r = -ENOMEM;
528 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
529 if (!cpuid_entries)
530 goto out;
532 r = 0;
533 for (i = 0; i < ARRAY_SIZE(param); i++) {
534 const struct kvm_cpuid_param *ent = &param[i];
536 if (ent->qualifier && !ent->qualifier(ent))
537 continue;
539 r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx,
540 &nent, cpuid->nent);
542 if (r)
543 goto out_free;
545 if (!ent->has_leaf_count)
546 continue;
548 limit = cpuid_entries[nent - 1].eax;
549 for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func)
550 r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx,
551 &nent, cpuid->nent);
553 if (r)
554 goto out_free;
557 r = -EFAULT;
558 if (copy_to_user(entries, cpuid_entries,
559 nent * sizeof(struct kvm_cpuid_entry2)))
560 goto out_free;
561 cpuid->nent = nent;
562 r = 0;
564 out_free:
565 vfree(cpuid_entries);
566 out:
567 return r;
570 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
572 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
573 int j, nent = vcpu->arch.cpuid_nent;
575 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
576 /* when no next entry is found, the current entry[i] is reselected */
577 for (j = i + 1; ; j = (j + 1) % nent) {
578 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
579 if (ej->function == e->function) {
580 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
581 return j;
584 return 0; /* silence gcc, even though control never reaches here */
587 /* find an entry with matching function, matching index (if needed), and that
588 * should be read next (if it's stateful) */
589 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
590 u32 function, u32 index)
592 if (e->function != function)
593 return 0;
594 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
595 return 0;
596 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
597 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
598 return 0;
599 return 1;
602 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
603 u32 function, u32 index)
605 int i;
606 struct kvm_cpuid_entry2 *best = NULL;
608 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
609 struct kvm_cpuid_entry2 *e;
611 e = &vcpu->arch.cpuid_entries[i];
612 if (is_matching_cpuid_entry(e, function, index)) {
613 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
614 move_to_next_stateful_cpuid_entry(vcpu, i);
615 best = e;
616 break;
619 return best;
621 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
623 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
625 struct kvm_cpuid_entry2 *best;
627 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
628 if (!best || best->eax < 0x80000008)
629 goto not_found;
630 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
631 if (best)
632 return best->eax & 0xff;
633 not_found:
634 return 36;
638 * If no match is found, check whether we exceed the vCPU's limit
639 * and return the content of the highest valid _standard_ leaf instead.
640 * This is to satisfy the CPUID specification.
642 static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
643 u32 function, u32 index)
645 struct kvm_cpuid_entry2 *maxlevel;
647 maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
648 if (!maxlevel || maxlevel->eax >= function)
649 return NULL;
650 if (function & 0x80000000) {
651 maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
652 if (!maxlevel)
653 return NULL;
655 return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
658 void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
660 u32 function = *eax, index = *ecx;
661 struct kvm_cpuid_entry2 *best;
663 best = kvm_find_cpuid_entry(vcpu, function, index);
665 if (!best)
666 best = check_cpuid_limit(vcpu, function, index);
668 if (best) {
669 *eax = best->eax;
670 *ebx = best->ebx;
671 *ecx = best->ecx;
672 *edx = best->edx;
673 } else
674 *eax = *ebx = *ecx = *edx = 0;
676 EXPORT_SYMBOL_GPL(kvm_cpuid);
678 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
680 u32 function, eax, ebx, ecx, edx;
682 function = eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
683 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
684 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx);
685 kvm_register_write(vcpu, VCPU_REGS_RAX, eax);
686 kvm_register_write(vcpu, VCPU_REGS_RBX, ebx);
687 kvm_register_write(vcpu, VCPU_REGS_RCX, ecx);
688 kvm_register_write(vcpu, VCPU_REGS_RDX, edx);
689 kvm_x86_ops->skip_emulated_instruction(vcpu);
690 trace_kvm_cpuid(function, eax, ebx, ecx, edx);
692 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);