1 #include "kvm/kvm-cpu.h"
2
3 #include "kvm/kvm.h"
4 #include "kvm/util.h"
5
6 #include <sys/ioctl.h>
7 #include <stdlib.h>
8
9 #define MAX_KVM_CPUID_ENTRIES 100
10
filter_cpuid(struct kvm_cpuid2 * kvm_cpuid,int cpu_id)11 static void filter_cpuid(struct kvm_cpuid2 *kvm_cpuid, int cpu_id)
12 {
13 unsigned int i;
14
15 /*
16 * Filter CPUID functions that are not supported by the hypervisor.
17 */
18 for (i = 0; i < kvm_cpuid->nent; i++) {
19 struct kvm_cpuid_entry2 *entry = &kvm_cpuid->entries[i];
20
21 switch (entry->function) {
22 case 1:
23 entry->ebx &= ~(0xff << 24);
24 entry->ebx |= cpu_id << 24;
25 /* Set X86_FEATURE_HYPERVISOR */
26 if (entry->index == 0)
27 entry->ecx |= (1 << 31);
28 break;
29 case 6:
30 /* Clear X86_FEATURE_EPB */
31 entry->ecx = entry->ecx & ~(1 << 3);
32 break;
33 case 10: { /* Architectural Performance Monitoring */
34 union cpuid10_eax {
35 struct {
36 unsigned int version_id :8;
37 unsigned int num_counters :8;
38 unsigned int bit_width :8;
39 unsigned int mask_length :8;
40 } split;
41 unsigned int full;
42 } eax;
43
44 /*
45 * If the host has perf system running,
46 * but no architectural events available
47 * through kvm pmu -- disable perf support,
48 * thus guest won't even try to access msr
49 * registers.
50 */
51 if (entry->eax) {
52 eax.full = entry->eax;
53 if (eax.split.version_id != 2 ||
54 !eax.split.num_counters)
55 entry->eax = 0;
56 }
57 break;
58 }
59 default:
60 /* Keep the CPUID function as -is */
61 break;
62 };
63 }
64 }
65
kvm_cpu__setup_cpuid(struct kvm_cpu * vcpu)66 void kvm_cpu__setup_cpuid(struct kvm_cpu *vcpu)
67 {
68 struct kvm_cpuid2 *kvm_cpuid;
69
70 kvm_cpuid = calloc(1, sizeof(*kvm_cpuid) +
71 MAX_KVM_CPUID_ENTRIES * sizeof(*kvm_cpuid->entries));
72
73 kvm_cpuid->nent = MAX_KVM_CPUID_ENTRIES;
74 if (ioctl(vcpu->kvm->sys_fd, KVM_GET_SUPPORTED_CPUID, kvm_cpuid) < 0)
75 die_perror("KVM_GET_SUPPORTED_CPUID failed");
76
77 filter_cpuid(kvm_cpuid, vcpu->cpu_id);
78
79 if (ioctl(vcpu->vcpu_fd, KVM_SET_CPUID2, kvm_cpuid) < 0)
80 die_perror("KVM_SET_CPUID2 failed");
81
82 free(kvm_cpuid);
83 }
84