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 void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
187 u32 index
, int *nent
, int maxnent
)
189 unsigned f_nx
= is_efer_nx() ? F(NX
) : 0;
191 unsigned f_gbpages
= (kvm_x86_ops
->get_lpage_level() == PT_PDPE_LEVEL
)
193 unsigned f_lm
= F(LM
);
195 unsigned f_gbpages
= 0;
198 unsigned f_rdtscp
= kvm_x86_ops
->rdtscp_supported() ? F(RDTSCP
) : 0;
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
);
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 0 /* Reserved */ | 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
) |
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
);
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
) |
244 const u32 kvm_supported_word9_x86_features
=
245 F(SMEP
) | F(FSGSBASE
) | F(ERMS
);
247 /* all calls to cpuid_count() should be made on the same cpu */
249 do_cpuid_1_ent(entry
, function
, index
);
254 entry
->eax
= min(entry
->eax
, (u32
)0xd);
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
);
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 */
270 int t
, times
= entry
->eax
& 0xff;
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
;
281 /* function 4 has additional index. */
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;
291 do_cpuid_1_ent(&entry
[i
], function
, i
);
293 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
299 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
300 /* Mask ebx against host capbability word 9 */
302 entry
->ebx
&= kvm_supported_word9_x86_features
;
303 cpuid_mask(&entry
->ebx
, 9);
313 /* function 0xb has additional index. */
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;
323 do_cpuid_1_ent(&entry
[i
], function
, i
);
325 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
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
))
339 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
345 case KVM_CPUID_SIGNATURE
: {
346 char signature
[12] = "KVMKVMKVM\0\0";
347 u32
*sigptr
= (u32
*)signature
;
349 entry
->ebx
= sigptr
[0];
350 entry
->ecx
= sigptr
[1];
351 entry
->edx
= sigptr
[2];
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
);
362 entry
->eax
|= (1 << KVM_FEATURE_STEAL_TIME
);
369 entry
->eax
= min(entry
->eax
, 0x8000001a);
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);
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;
384 entry
->eax
= g_phys_as
| (virt_as
<< 8);
385 entry
->ebx
= entry
->edx
= 0;
389 entry
->ecx
= entry
->edx
= 0;
395 /*Add support for Centaur's CPUID instruction*/
397 /*Just support up to 0xC0000004 now*/
398 entry
->eax
= min(entry
->eax
, 0xC0000004);
401 entry
->edx
&= kvm_supported_word5_x86_features
;
402 cpuid_mask(&entry
->edx
, 5);
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 */
413 entry
->eax
= entry
->ebx
= entry
->ecx
= entry
->edx
= 0;
417 kvm_x86_ops
->set_supported_cpuid(function
, entry
);
424 int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
425 struct kvm_cpuid_entry2 __user
*entries
)
427 struct kvm_cpuid_entry2
*cpuid_entries
;
428 int limit
, nent
= 0, r
= -E2BIG
;
433 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
434 cpuid
->nent
= KVM_MAX_CPUID_ENTRIES
;
436 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
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,
446 if (nent
>= cpuid
->nent
)
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,
458 if (nent
>= cpuid
->nent
)
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,
467 if (nent
>= cpuid
->nent
)
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,
477 if (nent
>= cpuid
->nent
)
481 do_cpuid_ent(&cpuid_entries
[nent
], KVM_CPUID_SIGNATURE
, 0, &nent
,
485 if (nent
>= cpuid
->nent
)
488 do_cpuid_ent(&cpuid_entries
[nent
], KVM_CPUID_FEATURES
, 0, &nent
,
492 if (nent
>= cpuid
->nent
)
496 if (copy_to_user(entries
, cpuid_entries
,
497 nent
* sizeof(struct kvm_cpuid_entry2
)))
503 vfree(cpuid_entries
);
508 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
510 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
511 int j
, nent
= vcpu
->arch
.cpuid_nent
;
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
;
522 return 0; /* silence gcc, even though control never reaches here */
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
)
530 if (e
->function
!= function
)
532 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
534 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
535 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
540 struct kvm_cpuid_entry2
*kvm_find_cpuid_entry(struct kvm_vcpu
*vcpu
,
541 u32 function
, u32 index
)
544 struct kvm_cpuid_entry2
*best
= NULL
;
546 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
547 struct kvm_cpuid_entry2
*e
;
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
);
559 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry
);
561 int cpuid_maxphyaddr(struct kvm_vcpu
*vcpu
)
563 struct kvm_cpuid_entry2
*best
;
565 best
= kvm_find_cpuid_entry(vcpu
, 0x80000000, 0);
566 if (!best
|| best
->eax
< 0x80000008)
568 best
= kvm_find_cpuid_entry(vcpu
, 0x80000008, 0);
570 return best
->eax
& 0xff;
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.
580 static struct kvm_cpuid_entry2
* check_cpuid_limit(struct kvm_vcpu
*vcpu
,
581 u32 function
, u32 index
)
583 struct kvm_cpuid_entry2
*maxlevel
;
585 maxlevel
= kvm_find_cpuid_entry(vcpu
, function
& 0x80000000, 0);
586 if (!maxlevel
|| maxlevel
->eax
>= function
)
588 if (function
& 0x80000000) {
589 maxlevel
= kvm_find_cpuid_entry(vcpu
, 0, 0);
593 return kvm_find_cpuid_entry(vcpu
, maxlevel
->eax
, index
);
596 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
599 struct kvm_cpuid_entry2
*best
;
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
);
610 best
= check_cpuid_limit(vcpu
, function
, index
);
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
);
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
));
625 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);