KVM: Refactor and simplify kvm_dev_ioctl_get_supported_cpuid
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / x86 / kvm / cpuid.c
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>
17 #include <asm/user.h>
18 #include <asm/xsave.h>
19 #include "cpuid.h"
20 #include "lapic.h"
21 #include "mmu.h"
22 #include "trace.h"
23
24 void kvm_update_cpuid(struct kvm_vcpu *vcpu)
25 {
26         struct kvm_cpuid_entry2 *best;
27         struct kvm_lapic *apic = vcpu->arch.apic;
28
29         best = kvm_find_cpuid_entry(vcpu, 1, 0);
30         if (!best)
31                 return;
32
33         /* Update OSXSAVE bit */
34         if (cpu_has_xsave && best->function == 0x1) {
35                 best->ecx &= ~(bit(X86_FEATURE_OSXSAVE));
36                 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
37                         best->ecx |= bit(X86_FEATURE_OSXSAVE);
38         }
39
40         if (apic) {
41                 if (best->ecx & bit(X86_FEATURE_TSC_DEADLINE_TIMER))
42                         apic->lapic_timer.timer_mode_mask = 3 << 17;
43                 else
44                         apic->lapic_timer.timer_mode_mask = 1 << 17;
45         }
46 }
47
48 static int is_efer_nx(void)
49 {
50         unsigned long long efer = 0;
51
52         rdmsrl_safe(MSR_EFER, &efer);
53         return efer & EFER_NX;
54 }
55
56 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
57 {
58         int i;
59         struct kvm_cpuid_entry2 *e, *entry;
60
61         entry = NULL;
62         for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
63                 e = &vcpu->arch.cpuid_entries[i];
64                 if (e->function == 0x80000001) {
65                         entry = e;
66                         break;
67                 }
68         }
69         if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
70                 entry->edx &= ~(1 << 20);
71                 printk(KERN_INFO "kvm: guest NX capability removed\n");
72         }
73 }
74
75 /* when an old userspace process fills a new kernel module */
76 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
77                              struct kvm_cpuid *cpuid,
78                              struct kvm_cpuid_entry __user *entries)
79 {
80         int r, i;
81         struct kvm_cpuid_entry *cpuid_entries;
82
83         r = -E2BIG;
84         if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
85                 goto out;
86         r = -ENOMEM;
87         cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
88         if (!cpuid_entries)
89                 goto out;
90         r = -EFAULT;
91         if (copy_from_user(cpuid_entries, entries,
92                            cpuid->nent * sizeof(struct kvm_cpuid_entry)))
93                 goto out_free;
94         for (i = 0; i < cpuid->nent; i++) {
95                 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
96                 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
97                 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
98                 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
99                 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
100                 vcpu->arch.cpuid_entries[i].index = 0;
101                 vcpu->arch.cpuid_entries[i].flags = 0;
102                 vcpu->arch.cpuid_entries[i].padding[0] = 0;
103                 vcpu->arch.cpuid_entries[i].padding[1] = 0;
104                 vcpu->arch.cpuid_entries[i].padding[2] = 0;
105         }
106         vcpu->arch.cpuid_nent = cpuid->nent;
107         cpuid_fix_nx_cap(vcpu);
108         r = 0;
109         kvm_apic_set_version(vcpu);
110         kvm_x86_ops->cpuid_update(vcpu);
111         kvm_update_cpuid(vcpu);
112
113 out_free:
114         vfree(cpuid_entries);
115 out:
116         return r;
117 }
118
119 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
120                               struct kvm_cpuid2 *cpuid,
121                               struct kvm_cpuid_entry2 __user *entries)
122 {
123         int r;
124
125         r = -E2BIG;
126         if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
127                 goto out;
128         r = -EFAULT;
129         if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
130                            cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
131                 goto out;
132         vcpu->arch.cpuid_nent = cpuid->nent;
133         kvm_apic_set_version(vcpu);
134         kvm_x86_ops->cpuid_update(vcpu);
135         kvm_update_cpuid(vcpu);
136         return 0;
137
138 out:
139         return r;
140 }
141
142 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
143                               struct kvm_cpuid2 *cpuid,
144                               struct kvm_cpuid_entry2 __user *entries)
145 {
146         int r;
147
148         r = -E2BIG;
149         if (cpuid->nent < vcpu->arch.cpuid_nent)
150                 goto out;
151         r = -EFAULT;
152         if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
153                          vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
154                 goto out;
155         return 0;
156
157 out:
158         cpuid->nent = vcpu->arch.cpuid_nent;
159         return r;
160 }
161
162 static void cpuid_mask(u32 *word, int wordnum)
163 {
164         *word &= boot_cpu_data.x86_capability[wordnum];
165 }
166
167 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
168                            u32 index)
169 {
170         entry->function = function;
171         entry->index = index;
172         cpuid_count(entry->function, entry->index,
173                     &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
174         entry->flags = 0;
175 }
176
177 static bool supported_xcr0_bit(unsigned bit)
178 {
179         u64 mask = ((u64)1 << bit);
180
181         return mask & (XSTATE_FP | XSTATE_SSE | XSTATE_YMM) & host_xcr0;
182 }
183
184 #define F(x) bit(X86_FEATURE_##x)
185
186 static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
187                          u32 index, int *nent, int maxnent)
188 {
189         int r;
190         unsigned f_nx = is_efer_nx() ? F(NX) : 0;
191 #ifdef CONFIG_X86_64
192         unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
193                                 ? F(GBPAGES) : 0;
194         unsigned f_lm = F(LM);
195 #else
196         unsigned f_gbpages = 0;
197         unsigned f_lm = 0;
198 #endif
199         unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
200
201         /* cpuid 1.edx */
202         const u32 kvm_supported_word0_x86_features =
203                 F(FPU) | F(VME) | F(DE) | F(PSE) |
204                 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
205                 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
206                 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
207                 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
208                 0 /* Reserved, DS, ACPI */ | F(MMX) |
209                 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
210                 0 /* HTT, TM, Reserved, PBE */;
211         /* cpuid 0x80000001.edx */
212         const u32 kvm_supported_word1_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(SYSCALL) |
216                 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
217                 F(PAT) | F(PSE36) | 0 /* Reserved */ |
218                 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
219                 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
220                 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
221         /* cpuid 1.ecx */
222         const u32 kvm_supported_word4_x86_features =
223                 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
224                 0 /* DS-CPL, VMX, SMX, EST */ |
225                 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
226                 F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
227                 0 /* Reserved, DCA */ | F(XMM4_1) |
228                 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
229                 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
230                 F(F16C) | F(RDRAND);
231         /* cpuid 0x80000001.ecx */
232         const u32 kvm_supported_word6_x86_features =
233                 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
234                 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
235                 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(XOP) |
236                 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
237
238         /* cpuid 0xC0000001.edx */
239         const u32 kvm_supported_word5_x86_features =
240                 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
241                 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
242                 F(PMM) | F(PMM_EN);
243
244         /* cpuid 7.0.ebx */
245         const u32 kvm_supported_word9_x86_features =
246                 F(FSGSBASE) | F(BMI1) | F(AVX2) | F(SMEP) | F(BMI2) | F(ERMS);
247
248         /* all calls to cpuid_count() should be made on the same cpu */
249         get_cpu();
250
251         r = -E2BIG;
252
253         if (*nent >= maxnent)
254                 goto out;
255
256         do_cpuid_1_ent(entry, function, index);
257         ++*nent;
258
259         switch (function) {
260         case 0:
261                 entry->eax = min(entry->eax, (u32)0xd);
262                 break;
263         case 1:
264                 entry->edx &= kvm_supported_word0_x86_features;
265                 cpuid_mask(&entry->edx, 0);
266                 entry->ecx &= kvm_supported_word4_x86_features;
267                 cpuid_mask(&entry->ecx, 4);
268                 /* we support x2apic emulation even if host does not support
269                  * it since we emulate x2apic in software */
270                 entry->ecx |= F(X2APIC);
271                 break;
272         /* function 2 entries are STATEFUL. That is, repeated cpuid commands
273          * may return different values. This forces us to get_cpu() before
274          * issuing the first command, and also to emulate this annoying behavior
275          * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
276         case 2: {
277                 int t, times = entry->eax & 0xff;
278
279                 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
280                 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
281                 for (t = 1; t < times; ++t) {
282                         if (*nent >= maxnent)
283                                 goto out;
284
285                         do_cpuid_1_ent(&entry[t], function, 0);
286                         entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
287                         ++*nent;
288                 }
289                 break;
290         }
291         /* function 4 has additional index. */
292         case 4: {
293                 int i, cache_type;
294
295                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
296                 /* read more entries until cache_type is zero */
297                 for (i = 1; ; ++i) {
298                         if (*nent >= maxnent)
299                                 goto out;
300
301                         cache_type = entry[i - 1].eax & 0x1f;
302                         if (!cache_type)
303                                 break;
304                         do_cpuid_1_ent(&entry[i], function, i);
305                         entry[i].flags |=
306                                KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
307                         ++*nent;
308                 }
309                 break;
310         }
311         case 7: {
312                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
313                 /* Mask ebx against host capbability word 9 */
314                 if (index == 0) {
315                         entry->ebx &= kvm_supported_word9_x86_features;
316                         cpuid_mask(&entry->ebx, 9);
317                 } else
318                         entry->ebx = 0;
319                 entry->eax = 0;
320                 entry->ecx = 0;
321                 entry->edx = 0;
322                 break;
323         }
324         case 9:
325                 break;
326         /* function 0xb has additional index. */
327         case 0xb: {
328                 int i, level_type;
329
330                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
331                 /* read more entries until level_type is zero */
332                 for (i = 1; ; ++i) {
333                         if (*nent >= maxnent)
334                                 goto out;
335
336                         level_type = entry[i - 1].ecx & 0xff00;
337                         if (!level_type)
338                                 break;
339                         do_cpuid_1_ent(&entry[i], function, i);
340                         entry[i].flags |=
341                                KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
342                         ++*nent;
343                 }
344                 break;
345         }
346         case 0xd: {
347                 int idx, i;
348
349                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
350                 for (idx = 1, i = 1; idx < 64; ++idx) {
351                         if (*nent >= maxnent)
352                                 goto out;
353
354                         do_cpuid_1_ent(&entry[i], function, idx);
355                         if (entry[i].eax == 0 || !supported_xcr0_bit(idx))
356                                 continue;
357                         entry[i].flags |=
358                                KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
359                         ++*nent;
360                         ++i;
361                 }
362                 break;
363         }
364         case KVM_CPUID_SIGNATURE: {
365                 char signature[12] = "KVMKVMKVM\0\0";
366                 u32 *sigptr = (u32 *)signature;
367                 entry->eax = 0;
368                 entry->ebx = sigptr[0];
369                 entry->ecx = sigptr[1];
370                 entry->edx = sigptr[2];
371                 break;
372         }
373         case KVM_CPUID_FEATURES:
374                 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
375                              (1 << KVM_FEATURE_NOP_IO_DELAY) |
376                              (1 << KVM_FEATURE_CLOCKSOURCE2) |
377                              (1 << KVM_FEATURE_ASYNC_PF) |
378                              (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
379
380                 if (sched_info_on())
381                         entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
382
383                 entry->ebx = 0;
384                 entry->ecx = 0;
385                 entry->edx = 0;
386                 break;
387         case 0x80000000:
388                 entry->eax = min(entry->eax, 0x8000001a);
389                 break;
390         case 0x80000001:
391                 entry->edx &= kvm_supported_word1_x86_features;
392                 cpuid_mask(&entry->edx, 1);
393                 entry->ecx &= kvm_supported_word6_x86_features;
394                 cpuid_mask(&entry->ecx, 6);
395                 break;
396         case 0x80000008: {
397                 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
398                 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
399                 unsigned phys_as = entry->eax & 0xff;
400
401                 if (!g_phys_as)
402                         g_phys_as = phys_as;
403                 entry->eax = g_phys_as | (virt_as << 8);
404                 entry->ebx = entry->edx = 0;
405                 break;
406         }
407         case 0x80000019:
408                 entry->ecx = entry->edx = 0;
409                 break;
410         case 0x8000001a:
411                 break;
412         case 0x8000001d:
413                 break;
414         /*Add support for Centaur's CPUID instruction*/
415         case 0xC0000000:
416                 /*Just support up to 0xC0000004 now*/
417                 entry->eax = min(entry->eax, 0xC0000004);
418                 break;
419         case 0xC0000001:
420                 entry->edx &= kvm_supported_word5_x86_features;
421                 cpuid_mask(&entry->edx, 5);
422                 break;
423         case 3: /* Processor serial number */
424         case 5: /* MONITOR/MWAIT */
425         case 6: /* Thermal management */
426         case 0xA: /* Architectural Performance Monitoring */
427         case 0x80000007: /* Advanced power management */
428         case 0xC0000002:
429         case 0xC0000003:
430         case 0xC0000004:
431         default:
432                 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
433                 break;
434         }
435
436         kvm_x86_ops->set_supported_cpuid(function, entry);
437
438         r = 0;
439
440 out:
441         put_cpu();
442
443         return r;
444 }
445
446 #undef F
447
448 struct kvm_cpuid_param {
449         u32 func;
450         u32 idx;
451         bool has_leaf_count;
452         bool (*qualifier)(struct kvm_cpuid_param *param);
453 };
454
455 static bool is_centaur_cpu(struct kvm_cpuid_param *param)
456 {
457         return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR;
458 }
459
460 int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
461                                       struct kvm_cpuid_entry2 __user *entries)
462 {
463         struct kvm_cpuid_entry2 *cpuid_entries;
464         int limit, nent = 0, r = -E2BIG, i;
465         u32 func;
466         static struct kvm_cpuid_param param[] = {
467                 { .func = 0, .has_leaf_count = true },
468                 { .func = 0x80000000, .has_leaf_count = true },
469                 { .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true },
470                 { .func = KVM_CPUID_SIGNATURE },
471                 { .func = KVM_CPUID_FEATURES },
472         };
473
474         if (cpuid->nent < 1)
475                 goto out;
476         if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
477                 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
478         r = -ENOMEM;
479         cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
480         if (!cpuid_entries)
481                 goto out;
482
483         r = 0;
484         for (i = 0; i < ARRAY_SIZE(param); i++) {
485                 struct kvm_cpuid_param *ent = &param[i];
486
487                 if (ent->qualifier && !ent->qualifier(ent))
488                         continue;
489
490                 r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx,
491                                 &nent, cpuid->nent);
492
493                 if (r)
494                         goto out_free;
495
496                 if (!ent->has_leaf_count)
497                         continue;
498
499                 limit = cpuid_entries[nent - 1].eax;
500                 for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func)
501                         r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx,
502                                      &nent, cpuid->nent);
503
504                 if (r)
505                         goto out_free;
506         }
507
508         r = -EFAULT;
509         if (copy_to_user(entries, cpuid_entries,
510                          nent * sizeof(struct kvm_cpuid_entry2)))
511                 goto out_free;
512         cpuid->nent = nent;
513         r = 0;
514
515 out_free:
516         vfree(cpuid_entries);
517 out:
518         return r;
519 }
520
521 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
522 {
523         struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
524         int j, nent = vcpu->arch.cpuid_nent;
525
526         e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
527         /* when no next entry is found, the current entry[i] is reselected */
528         for (j = i + 1; ; j = (j + 1) % nent) {
529                 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
530                 if (ej->function == e->function) {
531                         ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
532                         return j;
533                 }
534         }
535         return 0; /* silence gcc, even though control never reaches here */
536 }
537
538 /* find an entry with matching function, matching index (if needed), and that
539  * should be read next (if it's stateful) */
540 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
541         u32 function, u32 index)
542 {
543         if (e->function != function)
544                 return 0;
545         if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
546                 return 0;
547         if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
548             !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
549                 return 0;
550         return 1;
551 }
552
553 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
554                                               u32 function, u32 index)
555 {
556         int i;
557         struct kvm_cpuid_entry2 *best = NULL;
558
559         for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
560                 struct kvm_cpuid_entry2 *e;
561
562                 e = &vcpu->arch.cpuid_entries[i];
563                 if (is_matching_cpuid_entry(e, function, index)) {
564                         if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
565                                 move_to_next_stateful_cpuid_entry(vcpu, i);
566                         best = e;
567                         break;
568                 }
569         }
570         return best;
571 }
572 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
573
574 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
575 {
576         struct kvm_cpuid_entry2 *best;
577
578         best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
579         if (!best || best->eax < 0x80000008)
580                 goto not_found;
581         best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
582         if (best)
583                 return best->eax & 0xff;
584 not_found:
585         return 36;
586 }
587
588 /*
589  * If no match is found, check whether we exceed the vCPU's limit
590  * and return the content of the highest valid _standard_ leaf instead.
591  * This is to satisfy the CPUID specification.
592  */
593 static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
594                                                   u32 function, u32 index)
595 {
596         struct kvm_cpuid_entry2 *maxlevel;
597
598         maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
599         if (!maxlevel || maxlevel->eax >= function)
600                 return NULL;
601         if (function & 0x80000000) {
602                 maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
603                 if (!maxlevel)
604                         return NULL;
605         }
606         return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
607 }
608
609 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
610 {
611         u32 function, index;
612         struct kvm_cpuid_entry2 *best;
613
614         function = kvm_register_read(vcpu, VCPU_REGS_RAX);
615         index = kvm_register_read(vcpu, VCPU_REGS_RCX);
616         kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
617         kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
618         kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
619         kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
620         best = kvm_find_cpuid_entry(vcpu, function, index);
621
622         if (!best)
623                 best = check_cpuid_limit(vcpu, function, index);
624
625         if (best) {
626                 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
627                 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
628                 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
629                 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
630         }
631         kvm_x86_ops->skip_emulated_instruction(vcpu);
632         trace_kvm_cpuid(function,
633                         kvm_register_read(vcpu, VCPU_REGS_RAX),
634                         kvm_register_read(vcpu, VCPU_REGS_RBX),
635                         kvm_register_read(vcpu, VCPU_REGS_RCX),
636                         kvm_register_read(vcpu, VCPU_REGS_RDX));
637 }
638 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);