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>
18 #include <asm/xsave.h>
24 void kvm_update_cpuid(struct kvm_vcpu
*vcpu
)
26 struct kvm_cpuid_entry2
*best
;
27 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
29 best
= kvm_find_cpuid_entry(vcpu
, 1, 0);
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
);
41 if (best
->ecx
& bit(X86_FEATURE_TSC_DEADLINE_TIMER
))
42 apic
->lapic_timer
.timer_mode_mask
= 3 << 17;
44 apic
->lapic_timer
.timer_mode_mask
= 1 << 17;
48 static int is_efer_nx(void)
50 unsigned long long efer
= 0;
52 rdmsrl_safe(MSR_EFER
, &efer
);
53 return efer
& EFER_NX
;
56 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
59 struct kvm_cpuid_entry2
*e
, *entry
;
62 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
63 e
= &vcpu
->arch
.cpuid_entries
[i
];
64 if (e
->function
== 0x80000001) {
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");
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
)
81 struct kvm_cpuid_entry
*cpuid_entries
;
84 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
87 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
91 if (copy_from_user(cpuid_entries
, entries
,
92 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
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;
106 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
107 cpuid_fix_nx_cap(vcpu
);
109 kvm_apic_set_version(vcpu
);
110 kvm_x86_ops
->cpuid_update(vcpu
);
111 kvm_update_cpuid(vcpu
);
114 vfree(cpuid_entries
);
119 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
120 struct kvm_cpuid2
*cpuid
,
121 struct kvm_cpuid_entry2 __user
*entries
)
126 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
129 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
130 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
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
);
142 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
143 struct kvm_cpuid2
*cpuid
,
144 struct kvm_cpuid_entry2 __user
*entries
)
149 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
152 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
153 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
158 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
162 static void cpuid_mask(u32
*word
, int wordnum
)
164 *word
&= boot_cpu_data
.x86_capability
[wordnum
];
167 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
170 entry
->function
= function
;
171 entry
->index
= index
;
172 cpuid_count(entry
->function
, entry
->index
,
173 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
177 static bool supported_xcr0_bit(unsigned bit
)
179 u64 mask
= ((u64
)1 << bit
);
181 return mask
& (XSTATE_FP
| XSTATE_SSE
| XSTATE_YMM
) & host_xcr0
;
184 #define F(x) bit(X86_FEATURE_##x)
186 static int do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
187 u32 index
, int *nent
, int maxnent
)
190 unsigned f_nx
= is_efer_nx() ? F(NX
) : 0;
192 unsigned f_gbpages
= (kvm_x86_ops
->get_lpage_level() == PT_PDPE_LEVEL
)
194 unsigned f_lm
= F(LM
);
196 unsigned f_gbpages
= 0;
199 unsigned f_rdtscp
= kvm_x86_ops
->rdtscp_supported() ? F(RDTSCP
) : 0;
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
);
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
) |
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
);
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
) |
245 const u32 kvm_supported_word9_x86_features
=
246 F(FSGSBASE
) | F(BMI1
) | F(AVX2
) | F(SMEP
) | F(BMI2
) | F(ERMS
);
248 /* all calls to cpuid_count() should be made on the same cpu */
253 if (*nent
>= maxnent
)
256 do_cpuid_1_ent(entry
, function
, index
);
261 entry
->eax
= min(entry
->eax
, (u32
)0xd);
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
);
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 */
277 int t
, times
= entry
->eax
& 0xff;
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
)
285 do_cpuid_1_ent(&entry
[t
], function
, 0);
286 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
291 /* function 4 has additional index. */
295 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
296 /* read more entries until cache_type is zero */
298 if (*nent
>= maxnent
)
301 cache_type
= entry
[i
- 1].eax
& 0x1f;
304 do_cpuid_1_ent(&entry
[i
], function
, i
);
306 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
312 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
313 /* Mask ebx against host capbability word 9 */
315 entry
->ebx
&= kvm_supported_word9_x86_features
;
316 cpuid_mask(&entry
->ebx
, 9);
326 /* function 0xb has additional index. */
330 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
331 /* read more entries until level_type is zero */
333 if (*nent
>= maxnent
)
336 level_type
= entry
[i
- 1].ecx
& 0xff00;
339 do_cpuid_1_ent(&entry
[i
], function
, i
);
341 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
349 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
350 for (idx
= 1, i
= 1; idx
< 64; ++idx
) {
351 if (*nent
>= maxnent
)
354 do_cpuid_1_ent(&entry
[i
], function
, idx
);
355 if (entry
[i
].eax
== 0 || !supported_xcr0_bit(idx
))
358 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
364 case KVM_CPUID_SIGNATURE
: {
365 char signature
[12] = "KVMKVMKVM\0\0";
366 u32
*sigptr
= (u32
*)signature
;
368 entry
->ebx
= sigptr
[0];
369 entry
->ecx
= sigptr
[1];
370 entry
->edx
= sigptr
[2];
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
);
381 entry
->eax
|= (1 << KVM_FEATURE_STEAL_TIME
);
388 entry
->eax
= min(entry
->eax
, 0x8000001a);
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);
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;
403 entry
->eax
= g_phys_as
| (virt_as
<< 8);
404 entry
->ebx
= entry
->edx
= 0;
408 entry
->ecx
= entry
->edx
= 0;
414 /*Add support for Centaur's CPUID instruction*/
416 /*Just support up to 0xC0000004 now*/
417 entry
->eax
= min(entry
->eax
, 0xC0000004);
420 entry
->edx
&= kvm_supported_word5_x86_features
;
421 cpuid_mask(&entry
->edx
, 5);
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 */
432 entry
->eax
= entry
->ebx
= entry
->ecx
= entry
->edx
= 0;
436 kvm_x86_ops
->set_supported_cpuid(function
, entry
);
448 struct kvm_cpuid_param
{
452 bool (*qualifier
)(struct kvm_cpuid_param
*param
);
455 static bool is_centaur_cpu(struct kvm_cpuid_param
*param
)
457 return boot_cpu_data
.x86_vendor
== X86_VENDOR_CENTAUR
;
460 int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
461 struct kvm_cpuid_entry2 __user
*entries
)
463 struct kvm_cpuid_entry2
*cpuid_entries
;
464 int limit
, nent
= 0, r
= -E2BIG
, i
;
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
},
476 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
477 cpuid
->nent
= KVM_MAX_CPUID_ENTRIES
;
479 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
484 for (i
= 0; i
< ARRAY_SIZE(param
); i
++) {
485 struct kvm_cpuid_param
*ent
= ¶m
[i
];
487 if (ent
->qualifier
&& !ent
->qualifier(ent
))
490 r
= do_cpuid_ent(&cpuid_entries
[nent
], ent
->func
, ent
->idx
,
496 if (!ent
->has_leaf_count
)
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
,
509 if (copy_to_user(entries
, cpuid_entries
,
510 nent
* sizeof(struct kvm_cpuid_entry2
)))
516 vfree(cpuid_entries
);
521 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
523 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
524 int j
, nent
= vcpu
->arch
.cpuid_nent
;
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
;
535 return 0; /* silence gcc, even though control never reaches here */
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
)
543 if (e
->function
!= function
)
545 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
547 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
548 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
553 struct kvm_cpuid_entry2
*kvm_find_cpuid_entry(struct kvm_vcpu
*vcpu
,
554 u32 function
, u32 index
)
557 struct kvm_cpuid_entry2
*best
= NULL
;
559 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
560 struct kvm_cpuid_entry2
*e
;
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
);
572 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry
);
574 int cpuid_maxphyaddr(struct kvm_vcpu
*vcpu
)
576 struct kvm_cpuid_entry2
*best
;
578 best
= kvm_find_cpuid_entry(vcpu
, 0x80000000, 0);
579 if (!best
|| best
->eax
< 0x80000008)
581 best
= kvm_find_cpuid_entry(vcpu
, 0x80000008, 0);
583 return best
->eax
& 0xff;
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.
593 static struct kvm_cpuid_entry2
* check_cpuid_limit(struct kvm_vcpu
*vcpu
,
594 u32 function
, u32 index
)
596 struct kvm_cpuid_entry2
*maxlevel
;
598 maxlevel
= kvm_find_cpuid_entry(vcpu
, function
& 0x80000000, 0);
599 if (!maxlevel
|| maxlevel
->eax
>= function
)
601 if (function
& 0x80000000) {
602 maxlevel
= kvm_find_cpuid_entry(vcpu
, 0, 0);
606 return kvm_find_cpuid_entry(vcpu
, maxlevel
->eax
, index
);
609 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
612 struct kvm_cpuid_entry2
*best
;
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
);
623 best
= check_cpuid_limit(vcpu
, function
, index
);
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
);
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
));
638 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);