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>
20 #include <asm/xsave.h>
26 static u32
xstate_required_size(u64 xstate_bv
)
29 u32 ret
= XSAVE_HDR_SIZE
+ XSAVE_HDR_OFFSET
;
31 xstate_bv
&= XSTATE_EXTEND_MASK
;
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
);
46 u64
kvm_supported_xcr0(void)
48 u64 xcr0
= KVM_SUPPORTED_XCR0
& host_xcr0
;
50 if (!kvm_x86_ops
->mpx_supported())
51 xcr0
&= ~(XSTATE_BNDREGS
| XSTATE_BNDCSR
);
56 int kvm_update_cpuid(struct kvm_vcpu
*vcpu
)
58 struct kvm_cpuid_entry2
*best
;
59 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
61 best
= kvm_find_cpuid_entry(vcpu
, 1, 0);
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
);
73 if (best
->ecx
& bit(X86_FEATURE_TSC_DEADLINE_TIMER
))
74 apic
->lapic_timer
.timer_mode_mask
= 3 << 17;
76 apic
->lapic_timer
.timer_mode_mask
= 1 << 17;
79 best
= kvm_find_cpuid_entry(vcpu
, 0xD, 0);
81 vcpu
->arch
.guest_supported_xcr0
= 0;
82 vcpu
->arch
.guest_xstate_size
= XSAVE_HDR_SIZE
+ XSAVE_HDR_OFFSET
;
84 vcpu
->arch
.guest_supported_xcr0
=
85 (best
->eax
| ((u64
)best
->edx
<< 32)) &
87 vcpu
->arch
.guest_xstate_size
= best
->ebx
=
88 xstate_required_size(vcpu
->arch
.xcr0
);
92 * The existing code assumes virtual address is 48-bit in the canonical
93 * address checks; exit if it is ever changed.
95 best
= kvm_find_cpuid_entry(vcpu
, 0x80000008, 0);
96 if (best
&& ((best
->eax
& 0xff00) >> 8) != 48 &&
97 ((best
->eax
& 0xff00) >> 8) != 0)
100 kvm_pmu_cpuid_update(vcpu
);
104 static int is_efer_nx(void)
106 unsigned long long efer
= 0;
108 rdmsrl_safe(MSR_EFER
, &efer
);
109 return efer
& EFER_NX
;
112 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
115 struct kvm_cpuid_entry2
*e
, *entry
;
118 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
119 e
= &vcpu
->arch
.cpuid_entries
[i
];
120 if (e
->function
== 0x80000001) {
125 if (entry
&& (entry
->edx
& bit(X86_FEATURE_NX
)) && !is_efer_nx()) {
126 entry
->edx
&= ~bit(X86_FEATURE_NX
);
127 printk(KERN_INFO
"kvm: guest NX capability removed\n");
131 /* when an old userspace process fills a new kernel module */
132 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
133 struct kvm_cpuid
*cpuid
,
134 struct kvm_cpuid_entry __user
*entries
)
137 struct kvm_cpuid_entry
*cpuid_entries
;
140 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
143 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
147 if (copy_from_user(cpuid_entries
, entries
,
148 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
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;
162 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
163 cpuid_fix_nx_cap(vcpu
);
164 kvm_apic_set_version(vcpu
);
165 kvm_x86_ops
->cpuid_update(vcpu
);
166 r
= kvm_update_cpuid(vcpu
);
169 vfree(cpuid_entries
);
174 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
175 struct kvm_cpuid2
*cpuid
,
176 struct kvm_cpuid_entry2 __user
*entries
)
181 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
184 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
185 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
187 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
188 kvm_apic_set_version(vcpu
);
189 kvm_x86_ops
->cpuid_update(vcpu
);
190 r
= kvm_update_cpuid(vcpu
);
195 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
196 struct kvm_cpuid2
*cpuid
,
197 struct kvm_cpuid_entry2 __user
*entries
)
202 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
205 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
206 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
211 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
215 static void cpuid_mask(u32
*word
, int wordnum
)
217 *word
&= boot_cpu_data
.x86_capability
[wordnum
];
220 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
223 entry
->function
= function
;
224 entry
->index
= index
;
225 cpuid_count(entry
->function
, entry
->index
,
226 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
230 #define F(x) bit(X86_FEATURE_##x)
232 static int __do_cpuid_ent_emulated(struct kvm_cpuid_entry2
*entry
,
233 u32 func
, u32 index
, int *nent
, int maxnent
)
237 entry
->eax
= 1; /* only one leaf currently */
241 entry
->ecx
= F(MOVBE
);
248 entry
->function
= func
;
249 entry
->index
= index
;
254 static inline int __do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
255 u32 index
, int *nent
, int maxnent
)
258 unsigned f_nx
= is_efer_nx() ? F(NX
) : 0;
260 unsigned f_gbpages
= (kvm_x86_ops
->get_lpage_level() == PT_PDPE_LEVEL
)
262 unsigned f_lm
= F(LM
);
264 unsigned f_gbpages
= 0;
267 unsigned f_rdtscp
= kvm_x86_ops
->rdtscp_supported() ? F(RDTSCP
) : 0;
268 unsigned f_invpcid
= kvm_x86_ops
->invpcid_supported() ? F(INVPCID
) : 0;
269 unsigned f_mpx
= kvm_x86_ops
->mpx_supported() ? F(MPX
) : 0;
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
) |
277 F(PAT
) | F(PSE36
) | 0 /* PSN */ | F(CLFLUSH
) |
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
);
292 const u32 kvm_supported_word4_x86_features
=
293 /* NOTE: MONITOR (and MWAIT) are emulated as NOP,
294 * but *not* advertised to guests via CPUID ! */
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 */ |
298 F(FMA
) | F(CX16
) | 0 /* xTPR Update, PDCM */ |
299 F(PCID
) | 0 /* Reserved, DCA */ | F(XMM4_1
) |
300 F(XMM4_2
) | F(X2APIC
) | F(MOVBE
) | F(POPCNT
) |
301 0 /* Reserved*/ | F(AES
) | F(XSAVE
) | 0 /* OSXSAVE */ | F(AVX
) |
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
) |
307 F(3DNOWPREFETCH
) | F(OSVW
) | 0 /* IBS */ | F(XOP
) |
308 0 /* SKINIT, WDT, LWP */ | F(FMA4
) | F(TBM
);
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
) |
317 const u32 kvm_supported_word9_x86_features
=
318 F(FSGSBASE
) | F(BMI1
) | F(HLE
) | F(AVX2
) | F(SMEP
) |
319 F(BMI2
) | F(ERMS
) | f_invpcid
| F(RTM
) | f_mpx
| F(RDSEED
) |
320 F(ADX
) | F(SMAP
) | F(AVX512F
) | F(AVX512PF
) | F(AVX512ER
) |
323 /* cpuid 0xD.1.eax */
324 const u32 kvm_supported_word10_x86_features
=
325 F(XSAVEOPT
) | F(XSAVEC
) | F(XGETBV1
);
327 /* all calls to cpuid_count() should be made on the same cpu */
332 if (*nent
>= maxnent
)
335 do_cpuid_1_ent(entry
, function
, index
);
340 entry
->eax
= min(entry
->eax
, (u32
)0xd);
343 entry
->edx
&= kvm_supported_word0_x86_features
;
344 cpuid_mask(&entry
->edx
, 0);
345 entry
->ecx
&= kvm_supported_word4_x86_features
;
346 cpuid_mask(&entry
->ecx
, 4);
347 /* we support x2apic emulation even if host does not support
348 * it since we emulate x2apic in software */
349 entry
->ecx
|= F(X2APIC
);
351 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
352 * may return different values. This forces us to get_cpu() before
353 * issuing the first command, and also to emulate this annoying behavior
354 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
356 int t
, times
= entry
->eax
& 0xff;
358 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
359 entry
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
360 for (t
= 1; t
< times
; ++t
) {
361 if (*nent
>= maxnent
)
364 do_cpuid_1_ent(&entry
[t
], function
, 0);
365 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
370 /* function 4 has additional index. */
374 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
375 /* read more entries until cache_type is zero */
377 if (*nent
>= maxnent
)
380 cache_type
= entry
[i
- 1].eax
& 0x1f;
383 do_cpuid_1_ent(&entry
[i
], function
, i
);
385 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
391 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
392 /* Mask ebx against host capability word 9 */
394 entry
->ebx
&= kvm_supported_word9_x86_features
;
395 cpuid_mask(&entry
->ebx
, 9);
396 // TSC_ADJUST is emulated
397 entry
->ebx
|= F(TSC_ADJUST
);
407 case 0xa: { /* Architectural Performance Monitoring */
408 struct x86_pmu_capability cap
;
409 union cpuid10_eax eax
;
410 union cpuid10_edx edx
;
412 perf_get_x86_pmu_capability(&cap
);
415 * Only support guest architectural pmu on a host
416 * with architectural pmu.
419 memset(&cap
, 0, sizeof(cap
));
421 eax
.split
.version_id
= min(cap
.version
, 2);
422 eax
.split
.num_counters
= cap
.num_counters_gp
;
423 eax
.split
.bit_width
= cap
.bit_width_gp
;
424 eax
.split
.mask_length
= cap
.events_mask_len
;
426 edx
.split
.num_counters_fixed
= cap
.num_counters_fixed
;
427 edx
.split
.bit_width_fixed
= cap
.bit_width_fixed
;
428 edx
.split
.reserved
= 0;
430 entry
->eax
= eax
.full
;
431 entry
->ebx
= cap
.events_mask
;
433 entry
->edx
= edx
.full
;
436 /* function 0xb has additional index. */
440 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
441 /* read more entries until level_type is zero */
443 if (*nent
>= maxnent
)
446 level_type
= entry
[i
- 1].ecx
& 0xff00;
449 do_cpuid_1_ent(&entry
[i
], function
, i
);
451 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
458 u64 supported
= kvm_supported_xcr0();
460 entry
->eax
&= supported
;
461 entry
->edx
&= supported
>> 32;
462 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
466 for (idx
= 1, i
= 1; idx
< 64; ++idx
) {
467 u64 mask
= ((u64
)1 << idx
);
468 if (*nent
>= maxnent
)
471 do_cpuid_1_ent(&entry
[i
], function
, idx
);
473 entry
[i
].eax
&= kvm_supported_word10_x86_features
;
474 else if (entry
[i
].eax
== 0 || !(supported
& mask
))
477 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
483 case KVM_CPUID_SIGNATURE
: {
484 static const char signature
[12] = "KVMKVMKVM\0\0";
485 const u32
*sigptr
= (const u32
*)signature
;
486 entry
->eax
= KVM_CPUID_FEATURES
;
487 entry
->ebx
= sigptr
[0];
488 entry
->ecx
= sigptr
[1];
489 entry
->edx
= sigptr
[2];
492 case KVM_CPUID_FEATURES
:
493 entry
->eax
= (1 << KVM_FEATURE_CLOCKSOURCE
) |
494 (1 << KVM_FEATURE_NOP_IO_DELAY
) |
495 (1 << KVM_FEATURE_CLOCKSOURCE2
) |
496 (1 << KVM_FEATURE_ASYNC_PF
) |
497 (1 << KVM_FEATURE_PV_EOI
) |
498 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT
) |
499 (1 << KVM_FEATURE_PV_UNHALT
);
502 entry
->eax
|= (1 << KVM_FEATURE_STEAL_TIME
);
509 entry
->eax
= min(entry
->eax
, 0x8000001a);
512 entry
->edx
&= kvm_supported_word1_x86_features
;
513 cpuid_mask(&entry
->edx
, 1);
514 entry
->ecx
&= kvm_supported_word6_x86_features
;
515 cpuid_mask(&entry
->ecx
, 6);
517 case 0x80000007: /* Advanced power management */
518 /* invariant TSC is CPUID.80000007H:EDX[8] */
519 entry
->edx
&= (1 << 8);
520 /* mask against host */
521 entry
->edx
&= boot_cpu_data
.x86_power
;
522 entry
->eax
= entry
->ebx
= entry
->ecx
= 0;
525 unsigned g_phys_as
= (entry
->eax
>> 16) & 0xff;
526 unsigned virt_as
= max((entry
->eax
>> 8) & 0xff, 48U);
527 unsigned phys_as
= entry
->eax
& 0xff;
531 entry
->eax
= g_phys_as
| (virt_as
<< 8);
532 entry
->ebx
= entry
->edx
= 0;
536 entry
->ecx
= entry
->edx
= 0;
542 /*Add support for Centaur's CPUID instruction*/
544 /*Just support up to 0xC0000004 now*/
545 entry
->eax
= min(entry
->eax
, 0xC0000004);
548 entry
->edx
&= kvm_supported_word5_x86_features
;
549 cpuid_mask(&entry
->edx
, 5);
551 case 3: /* Processor serial number */
552 case 5: /* MONITOR/MWAIT */
553 case 6: /* Thermal management */
558 entry
->eax
= entry
->ebx
= entry
->ecx
= entry
->edx
= 0;
562 kvm_x86_ops
->set_supported_cpuid(function
, entry
);
572 static int do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 func
,
573 u32 idx
, int *nent
, int maxnent
, unsigned int type
)
575 if (type
== KVM_GET_EMULATED_CPUID
)
576 return __do_cpuid_ent_emulated(entry
, func
, idx
, nent
, maxnent
);
578 return __do_cpuid_ent(entry
, func
, idx
, nent
, maxnent
);
583 struct kvm_cpuid_param
{
587 bool (*qualifier
)(const struct kvm_cpuid_param
*param
);
590 static bool is_centaur_cpu(const struct kvm_cpuid_param
*param
)
592 return boot_cpu_data
.x86_vendor
== X86_VENDOR_CENTAUR
;
595 static bool sanity_check_entries(struct kvm_cpuid_entry2 __user
*entries
,
596 __u32 num_entries
, unsigned int ioctl_type
)
601 if (ioctl_type
!= KVM_GET_EMULATED_CPUID
)
605 * We want to make sure that ->padding is being passed clean from
606 * userspace in case we want to use it for something in the future.
608 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
609 * have to give ourselves satisfied only with the emulated side. /me
612 for (i
= 0; i
< num_entries
; i
++) {
613 if (copy_from_user(pad
, entries
[i
].padding
, sizeof(pad
)))
616 if (pad
[0] || pad
[1] || pad
[2])
622 int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2
*cpuid
,
623 struct kvm_cpuid_entry2 __user
*entries
,
626 struct kvm_cpuid_entry2
*cpuid_entries
;
627 int limit
, nent
= 0, r
= -E2BIG
, i
;
629 static const struct kvm_cpuid_param param
[] = {
630 { .func
= 0, .has_leaf_count
= true },
631 { .func
= 0x80000000, .has_leaf_count
= true },
632 { .func
= 0xC0000000, .qualifier
= is_centaur_cpu
, .has_leaf_count
= true },
633 { .func
= KVM_CPUID_SIGNATURE
},
634 { .func
= KVM_CPUID_FEATURES
},
639 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
640 cpuid
->nent
= KVM_MAX_CPUID_ENTRIES
;
642 if (sanity_check_entries(entries
, cpuid
->nent
, type
))
646 cpuid_entries
= vzalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
651 for (i
= 0; i
< ARRAY_SIZE(param
); i
++) {
652 const struct kvm_cpuid_param
*ent
= ¶m
[i
];
654 if (ent
->qualifier
&& !ent
->qualifier(ent
))
657 r
= do_cpuid_ent(&cpuid_entries
[nent
], ent
->func
, ent
->idx
,
658 &nent
, cpuid
->nent
, type
);
663 if (!ent
->has_leaf_count
)
666 limit
= cpuid_entries
[nent
- 1].eax
;
667 for (func
= ent
->func
+ 1; func
<= limit
&& nent
< cpuid
->nent
&& r
== 0; ++func
)
668 r
= do_cpuid_ent(&cpuid_entries
[nent
], func
, ent
->idx
,
669 &nent
, cpuid
->nent
, type
);
676 if (copy_to_user(entries
, cpuid_entries
,
677 nent
* sizeof(struct kvm_cpuid_entry2
)))
683 vfree(cpuid_entries
);
688 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
690 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
691 int j
, nent
= vcpu
->arch
.cpuid_nent
;
693 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
694 /* when no next entry is found, the current entry[i] is reselected */
695 for (j
= i
+ 1; ; j
= (j
+ 1) % nent
) {
696 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
697 if (ej
->function
== e
->function
) {
698 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
702 return 0; /* silence gcc, even though control never reaches here */
705 /* find an entry with matching function, matching index (if needed), and that
706 * should be read next (if it's stateful) */
707 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
708 u32 function
, u32 index
)
710 if (e
->function
!= function
)
712 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
714 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
715 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
720 struct kvm_cpuid_entry2
*kvm_find_cpuid_entry(struct kvm_vcpu
*vcpu
,
721 u32 function
, u32 index
)
724 struct kvm_cpuid_entry2
*best
= NULL
;
726 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
727 struct kvm_cpuid_entry2
*e
;
729 e
= &vcpu
->arch
.cpuid_entries
[i
];
730 if (is_matching_cpuid_entry(e
, function
, index
)) {
731 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
732 move_to_next_stateful_cpuid_entry(vcpu
, i
);
739 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry
);
741 int cpuid_maxphyaddr(struct kvm_vcpu
*vcpu
)
743 struct kvm_cpuid_entry2
*best
;
745 best
= kvm_find_cpuid_entry(vcpu
, 0x80000000, 0);
746 if (!best
|| best
->eax
< 0x80000008)
748 best
= kvm_find_cpuid_entry(vcpu
, 0x80000008, 0);
750 return best
->eax
& 0xff;
754 EXPORT_SYMBOL_GPL(cpuid_maxphyaddr
);
757 * If no match is found, check whether we exceed the vCPU's limit
758 * and return the content of the highest valid _standard_ leaf instead.
759 * This is to satisfy the CPUID specification.
761 static struct kvm_cpuid_entry2
* check_cpuid_limit(struct kvm_vcpu
*vcpu
,
762 u32 function
, u32 index
)
764 struct kvm_cpuid_entry2
*maxlevel
;
766 maxlevel
= kvm_find_cpuid_entry(vcpu
, function
& 0x80000000, 0);
767 if (!maxlevel
|| maxlevel
->eax
>= function
)
769 if (function
& 0x80000000) {
770 maxlevel
= kvm_find_cpuid_entry(vcpu
, 0, 0);
774 return kvm_find_cpuid_entry(vcpu
, maxlevel
->eax
, index
);
777 void kvm_cpuid(struct kvm_vcpu
*vcpu
, u32
*eax
, u32
*ebx
, u32
*ecx
, u32
*edx
)
779 u32 function
= *eax
, index
= *ecx
;
780 struct kvm_cpuid_entry2
*best
;
782 best
= kvm_find_cpuid_entry(vcpu
, function
, index
);
785 best
= check_cpuid_limit(vcpu
, function
, index
);
788 * Perfmon not yet supported for L2 guest.
790 if (is_guest_mode(vcpu
) && function
== 0xa)
799 *eax
= *ebx
= *ecx
= *edx
= 0;
800 trace_kvm_cpuid(function
, *eax
, *ebx
, *ecx
, *edx
);
802 EXPORT_SYMBOL_GPL(kvm_cpuid
);
804 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
806 u32 function
, eax
, ebx
, ecx
, edx
;
808 function
= eax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
809 ecx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
810 kvm_cpuid(vcpu
, &eax
, &ebx
, &ecx
, &edx
);
811 kvm_register_write(vcpu
, VCPU_REGS_RAX
, eax
);
812 kvm_register_write(vcpu
, VCPU_REGS_RBX
, ebx
);
813 kvm_register_write(vcpu
, VCPU_REGS_RCX
, ecx
);
814 kvm_register_write(vcpu
, VCPU_REGS_RDX
, edx
);
815 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
817 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);