47be763e1b60ca282369b90fbb717cf884032e70
[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 void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
187                          u32 index, int *nent, int maxnent)
188 {
189         unsigned f_nx = is_efer_nx() ? F(NX) : 0;
190 #ifdef CONFIG_X86_64
191         unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
192                                 ? F(GBPAGES) : 0;
193         unsigned f_lm = F(LM);
194 #else
195         unsigned f_gbpages = 0;
196         unsigned f_lm = 0;
197 #endif
198         unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
199
200         /* cpuid 1.edx */
201         const u32 kvm_supported_word0_x86_features =
202                 F(FPU) | F(VME) | F(DE) | F(PSE) |
203                 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
204                 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
205                 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
206                 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
207                 0 /* Reserved, DS, ACPI */ | F(MMX) |
208                 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
209                 0 /* HTT, TM, Reserved, PBE */;
210         /* cpuid 0x80000001.edx */
211         const u32 kvm_supported_word1_x86_features =
212                 F(FPU) | F(VME) | F(DE) | F(PSE) |
213                 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
214                 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
215                 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
216                 F(PAT) | F(PSE36) | 0 /* Reserved */ |
217                 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
218                 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
219                 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
220         /* cpuid 1.ecx */
221         const u32 kvm_supported_word4_x86_features =
222                 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
223                 0 /* DS-CPL, VMX, SMX, EST */ |
224                 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
225                 F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ |
226                 0 /* Reserved, DCA */ | F(XMM4_1) |
227                 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
228                 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
229                 F(F16C) | F(RDRAND);
230         /* cpuid 0x80000001.ecx */
231         const u32 kvm_supported_word6_x86_features =
232                 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
233                 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
234                 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(XOP) |
235                 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
236
237         /* cpuid 0xC0000001.edx */
238         const u32 kvm_supported_word5_x86_features =
239                 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
240                 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
241                 F(PMM) | F(PMM_EN);
242
243         /* cpuid 7.0.ebx */
244         const u32 kvm_supported_word9_x86_features =
245                 F(FSGSBASE) | F(BMI1) | F(AVX2) | F(SMEP) | F(BMI2) | F(ERMS);
246
247         /* all calls to cpuid_count() should be made on the same cpu */
248         get_cpu();
249         do_cpuid_1_ent(entry, function, index);
250         ++*nent;
251
252         switch (function) {
253         case 0:
254                 entry->eax = min(entry->eax, (u32)0xd);
255                 break;
256         case 1:
257                 entry->edx &= kvm_supported_word0_x86_features;
258                 cpuid_mask(&entry->edx, 0);
259                 entry->ecx &= kvm_supported_word4_x86_features;
260                 cpuid_mask(&entry->ecx, 4);
261                 /* we support x2apic emulation even if host does not support
262                  * it since we emulate x2apic in software */
263                 entry->ecx |= F(X2APIC);
264                 break;
265         /* function 2 entries are STATEFUL. That is, repeated cpuid commands
266          * may return different values. This forces us to get_cpu() before
267          * issuing the first command, and also to emulate this annoying behavior
268          * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
269         case 2: {
270                 int t, times = entry->eax & 0xff;
271
272                 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
273                 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
274                 for (t = 1; t < times && *nent < maxnent; ++t) {
275                         do_cpuid_1_ent(&entry[t], function, 0);
276                         entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
277                         ++*nent;
278                 }
279                 break;
280         }
281         /* function 4 has additional index. */
282         case 4: {
283                 int i, cache_type;
284
285                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
286                 /* read more entries until cache_type is zero */
287                 for (i = 1; *nent < maxnent; ++i) {
288                         cache_type = entry[i - 1].eax & 0x1f;
289                         if (!cache_type)
290                                 break;
291                         do_cpuid_1_ent(&entry[i], function, i);
292                         entry[i].flags |=
293                                KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
294                         ++*nent;
295                 }
296                 break;
297         }
298         case 7: {
299                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
300                 /* Mask ebx against host capbability word 9 */
301                 if (index == 0) {
302                         entry->ebx &= kvm_supported_word9_x86_features;
303                         cpuid_mask(&entry->ebx, 9);
304                 } else
305                         entry->ebx = 0;
306                 entry->eax = 0;
307                 entry->ecx = 0;
308                 entry->edx = 0;
309                 break;
310         }
311         case 9:
312                 break;
313         /* function 0xb has additional index. */
314         case 0xb: {
315                 int i, level_type;
316
317                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
318                 /* read more entries until level_type is zero */
319                 for (i = 1; *nent < maxnent; ++i) {
320                         level_type = entry[i - 1].ecx & 0xff00;
321                         if (!level_type)
322                                 break;
323                         do_cpuid_1_ent(&entry[i], function, i);
324                         entry[i].flags |=
325                                KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
326                         ++*nent;
327                 }
328                 break;
329         }
330         case 0xd: {
331                 int idx, i;
332
333                 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
334                 for (idx = 1, i = 1; *nent < maxnent && idx < 64; ++idx) {
335                         do_cpuid_1_ent(&entry[i], function, idx);
336                         if (entry[i].eax == 0 || !supported_xcr0_bit(idx))
337                                 continue;
338                         entry[i].flags |=
339                                KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
340                         ++*nent;
341                         ++i;
342                 }
343                 break;
344         }
345         case KVM_CPUID_SIGNATURE: {
346                 char signature[12] = "KVMKVMKVM\0\0";
347                 u32 *sigptr = (u32 *)signature;
348                 entry->eax = 0;
349                 entry->ebx = sigptr[0];
350                 entry->ecx = sigptr[1];
351                 entry->edx = sigptr[2];
352                 break;
353         }
354         case KVM_CPUID_FEATURES:
355                 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
356                              (1 << KVM_FEATURE_NOP_IO_DELAY) |
357                              (1 << KVM_FEATURE_CLOCKSOURCE2) |
358                              (1 << KVM_FEATURE_ASYNC_PF) |
359                              (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
360
361                 if (sched_info_on())
362                         entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
363
364                 entry->ebx = 0;
365                 entry->ecx = 0;
366                 entry->edx = 0;
367                 break;
368         case 0x80000000:
369                 entry->eax = min(entry->eax, 0x8000001a);
370                 break;
371         case 0x80000001:
372                 entry->edx &= kvm_supported_word1_x86_features;
373                 cpuid_mask(&entry->edx, 1);
374                 entry->ecx &= kvm_supported_word6_x86_features;
375                 cpuid_mask(&entry->ecx, 6);
376                 break;
377         case 0x80000008: {
378                 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
379                 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
380                 unsigned phys_as = entry->eax & 0xff;
381
382                 if (!g_phys_as)
383                         g_phys_as = phys_as;
384                 entry->eax = g_phys_as | (virt_as << 8);
385                 entry->ebx = entry->edx = 0;
386                 break;
387         }
388         case 0x80000019:
389                 entry->ecx = entry->edx = 0;
390                 break;
391         case 0x8000001a:
392                 break;
393         case 0x8000001d:
394                 break;
395         /*Add support for Centaur's CPUID instruction*/
396         case 0xC0000000:
397                 /*Just support up to 0xC0000004 now*/
398                 entry->eax = min(entry->eax, 0xC0000004);
399                 break;
400         case 0xC0000001:
401                 entry->edx &= kvm_supported_word5_x86_features;
402                 cpuid_mask(&entry->edx, 5);
403                 break;
404         case 3: /* Processor serial number */
405         case 5: /* MONITOR/MWAIT */
406         case 6: /* Thermal management */
407         case 0xA: /* Architectural Performance Monitoring */
408         case 0x80000007: /* Advanced power management */
409         case 0xC0000002:
410         case 0xC0000003:
411         case 0xC0000004:
412         default:
413                 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
414                 break;
415         }
416
417         kvm_x86_ops->set_supported_cpuid(function, entry);
418
419         put_cpu();
420 }
421
422 #undef F
423
424 int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
425                                       struct kvm_cpuid_entry2 __user *entries)
426 {
427         struct kvm_cpuid_entry2 *cpuid_entries;
428         int limit, nent = 0, r = -E2BIG;
429         u32 func;
430
431         if (cpuid->nent < 1)
432                 goto out;
433         if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
434                 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
435         r = -ENOMEM;
436         cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
437         if (!cpuid_entries)
438                 goto out;
439
440         do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
441         limit = cpuid_entries[0].eax;
442         for (func = 1; func <= limit && nent < cpuid->nent; ++func)
443                 do_cpuid_ent(&cpuid_entries[nent], func, 0,
444                              &nent, cpuid->nent);
445         r = -E2BIG;
446         if (nent >= cpuid->nent)
447                 goto out_free;
448
449         do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
450         limit = cpuid_entries[nent - 1].eax;
451         for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
452                 do_cpuid_ent(&cpuid_entries[nent], func, 0,
453                              &nent, cpuid->nent);
454
455
456
457         r = -E2BIG;
458         if (nent >= cpuid->nent)
459                 goto out_free;
460
461         /* Add support for Centaur's CPUID instruction. */
462         if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR) {
463                 do_cpuid_ent(&cpuid_entries[nent], 0xC0000000, 0,
464                                 &nent, cpuid->nent);
465
466                 r = -E2BIG;
467                 if (nent >= cpuid->nent)
468                         goto out_free;
469
470                 limit = cpuid_entries[nent - 1].eax;
471                 for (func = 0xC0000001;
472                         func <= limit && nent < cpuid->nent; ++func)
473                         do_cpuid_ent(&cpuid_entries[nent], func, 0,
474                                         &nent, cpuid->nent);
475
476                 r = -E2BIG;
477                 if (nent >= cpuid->nent)
478                         goto out_free;
479         }
480
481         do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_SIGNATURE, 0, &nent,
482                      cpuid->nent);
483
484         r = -E2BIG;
485         if (nent >= cpuid->nent)
486                 goto out_free;
487
488         do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_FEATURES, 0, &nent,
489                      cpuid->nent);
490
491         r = -E2BIG;
492         if (nent >= cpuid->nent)
493                 goto out_free;
494
495         r = -EFAULT;
496         if (copy_to_user(entries, cpuid_entries,
497                          nent * sizeof(struct kvm_cpuid_entry2)))
498                 goto out_free;
499         cpuid->nent = nent;
500         r = 0;
501
502 out_free:
503         vfree(cpuid_entries);
504 out:
505         return r;
506 }
507
508 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
509 {
510         struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
511         int j, nent = vcpu->arch.cpuid_nent;
512
513         e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
514         /* when no next entry is found, the current entry[i] is reselected */
515         for (j = i + 1; ; j = (j + 1) % nent) {
516                 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
517                 if (ej->function == e->function) {
518                         ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
519                         return j;
520                 }
521         }
522         return 0; /* silence gcc, even though control never reaches here */
523 }
524
525 /* find an entry with matching function, matching index (if needed), and that
526  * should be read next (if it's stateful) */
527 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
528         u32 function, u32 index)
529 {
530         if (e->function != function)
531                 return 0;
532         if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
533                 return 0;
534         if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
535             !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
536                 return 0;
537         return 1;
538 }
539
540 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
541                                               u32 function, u32 index)
542 {
543         int i;
544         struct kvm_cpuid_entry2 *best = NULL;
545
546         for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
547                 struct kvm_cpuid_entry2 *e;
548
549                 e = &vcpu->arch.cpuid_entries[i];
550                 if (is_matching_cpuid_entry(e, function, index)) {
551                         if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
552                                 move_to_next_stateful_cpuid_entry(vcpu, i);
553                         best = e;
554                         break;
555                 }
556         }
557         return best;
558 }
559 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
560
561 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
562 {
563         struct kvm_cpuid_entry2 *best;
564
565         best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
566         if (!best || best->eax < 0x80000008)
567                 goto not_found;
568         best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
569         if (best)
570                 return best->eax & 0xff;
571 not_found:
572         return 36;
573 }
574
575 /*
576  * If no match is found, check whether we exceed the vCPU's limit
577  * and return the content of the highest valid _standard_ leaf instead.
578  * This is to satisfy the CPUID specification.
579  */
580 static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
581                                                   u32 function, u32 index)
582 {
583         struct kvm_cpuid_entry2 *maxlevel;
584
585         maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
586         if (!maxlevel || maxlevel->eax >= function)
587                 return NULL;
588         if (function & 0x80000000) {
589                 maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
590                 if (!maxlevel)
591                         return NULL;
592         }
593         return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
594 }
595
596 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
597 {
598         u32 function, index;
599         struct kvm_cpuid_entry2 *best;
600
601         function = kvm_register_read(vcpu, VCPU_REGS_RAX);
602         index = kvm_register_read(vcpu, VCPU_REGS_RCX);
603         kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
604         kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
605         kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
606         kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
607         best = kvm_find_cpuid_entry(vcpu, function, index);
608
609         if (!best)
610                 best = check_cpuid_limit(vcpu, function, index);
611
612         if (best) {
613                 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
614                 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
615                 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
616                 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
617         }
618         kvm_x86_ops->skip_emulated_instruction(vcpu);
619         trace_kvm_cpuid(function,
620                         kvm_register_read(vcpu, VCPU_REGS_RAX),
621                         kvm_register_read(vcpu, VCPU_REGS_RBX),
622                         kvm_register_read(vcpu, VCPU_REGS_RCX),
623                         kvm_register_read(vcpu, VCPU_REGS_RDX));
624 }
625 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);