Skip to content

Commit 760849b

Browse files
Paul Durrantbonzini
authored andcommitted
KVM: x86: Make sure KVM_CPUID_FEATURES really are KVM_CPUID_FEATURES
Currently when kvm_update_cpuid_runtime() runs, it assumes that the KVM_CPUID_FEATURES leaf is located at 0x40000001. This is not true, however, if Hyper-V support is enabled. In this case the KVM leaves will be offset. This patch introdues as new 'kvm_cpuid_base' field into struct kvm_vcpu_arch to track the location of the KVM leaves and function kvm_update_kvm_cpuid_base() (called from kvm_set_cpuid()) to locate the leaves using the 'KVMKVMKVM\0\0\0' signature (which is now given a definition in kvm_para.h). Adjustment of KVM_CPUID_FEATURES will hence now target the correct leaf. NOTE: A new for_each_possible_hypervisor_cpuid_base() macro is intoduced into processor.h to avoid having duplicate code for the iteration over possible hypervisor base leaves. Signed-off-by: Paul Durrant <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 8b44b17 commit 760849b

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ struct kvm_vcpu_arch {
725725

726726
int cpuid_nent;
727727
struct kvm_cpuid_entry2 *cpuid_entries;
728+
u32 kvm_cpuid_base;
728729

729730
u64 reserved_gpa_bits;
730731
int maxphyaddr;

arch/x86/include/asm/processor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,11 +807,14 @@ static inline u32 amd_get_nodes_per_socket(void) { return 0; }
807807
static inline u32 amd_get_highest_perf(void) { return 0; }
808808
#endif
809809

810+
#define for_each_possible_hypervisor_cpuid_base(function) \
811+
for (function = 0x40000000; function < 0x40010000; function += 0x100)
812+
810813
static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
811814
{
812815
uint32_t base, eax, signature[3];
813816

814-
for (base = 0x40000000; base < 0x40010000; base += 0x100) {
817+
for_each_possible_hypervisor_cpuid_base(base) {
815818
cpuid(base, &eax, &signature[0], &signature[1], &signature[2]);
816819

817820
if (!memcmp(sig, signature, 12) &&

arch/x86/include/uapi/asm/kvm_para.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* should be used to determine that a VM is running under KVM.
99
*/
1010
#define KVM_CPUID_SIGNATURE 0x40000000
11+
#define KVM_SIGNATURE "KVMKVMKVM\0\0\0"
1112

1213
/* This CPUID returns two feature bitmaps in eax, edx. Before enabling
1314
* a particular paravirtualization, the appropriate feature bit should

arch/x86/kernel/kvm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ static noinline uint32_t __kvm_cpuid_base(void)
755755
return 0; /* So we don't blow up on old processors */
756756

757757
if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
758-
return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
758+
return hypervisor_cpuid_base(KVM_SIGNATURE, 0);
759759

760760
return 0;
761761
}

arch/x86/kvm/cpuid.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,45 @@ static int kvm_check_cpuid(struct kvm_cpuid_entry2 *entries, int nent)
9999
return 0;
100100
}
101101

102-
void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
102+
static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
103103
{
104-
struct kvm_cpuid_entry2 *best;
104+
u32 function;
105+
struct kvm_cpuid_entry2 *entry;
106+
107+
vcpu->arch.kvm_cpuid_base = 0;
108+
109+
for_each_possible_hypervisor_cpuid_base(function) {
110+
entry = kvm_find_cpuid_entry(vcpu, function, 0);
111+
112+
if (entry) {
113+
u32 signature[3];
114+
115+
signature[0] = entry->ebx;
116+
signature[1] = entry->ecx;
117+
signature[2] = entry->edx;
118+
119+
BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE));
120+
if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) {
121+
vcpu->arch.kvm_cpuid_base = function;
122+
break;
123+
}
124+
}
125+
}
126+
}
127+
128+
struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu)
129+
{
130+
u32 base = vcpu->arch.kvm_cpuid_base;
131+
132+
if (!base)
133+
return NULL;
134+
135+
return kvm_find_cpuid_entry(vcpu, base | KVM_CPUID_FEATURES, 0);
136+
}
105137

106-
best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
138+
void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
139+
{
140+
struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu);
107141

108142
/*
109143
* save the feature bitmap to avoid cpuid lookup for every PV
@@ -142,7 +176,7 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
142176
cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
143177
best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
144178

145-
best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
179+
best = kvm_find_kvm_cpuid_features(vcpu);
146180
if (kvm_hlt_in_guest(vcpu->kvm) && best &&
147181
(best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
148182
best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
@@ -252,6 +286,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
252286
vcpu->arch.cpuid_entries = e2;
253287
vcpu->arch.cpuid_nent = nent;
254288

289+
kvm_update_kvm_cpuid_base(vcpu);
255290
kvm_update_cpuid_runtime(vcpu);
256291
kvm_vcpu_after_set_cpuid(vcpu);
257292

@@ -872,8 +907,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
872907
}
873908
break;
874909
case KVM_CPUID_SIGNATURE: {
875-
static const char signature[12] = "KVMKVMKVM\0\0";
876-
const u32 *sigptr = (const u32 *)signature;
910+
const u32 *sigptr = (const u32 *)KVM_SIGNATURE;
877911
entry->eax = KVM_CPUID_FEATURES;
878912
entry->ebx = sigptr[0];
879913
entry->ecx = sigptr[1];

0 commit comments

Comments
 (0)