Skip to content

Commit bd22f5c

Browse files
aprzywaravikivity
authored andcommitted
KVM: move and fix substitue search for missing CPUID entries
If KVM cannot find an exact match for a requested CPUID leaf, the code will try to find the closest match instead of simply confessing it's failure. The implementation was meant to satisfy the CPUID specification, but did not properly check for extended and standard leaves and also didn't account for the index subleaf. Beside that this rule only applies to CPUID intercepts, which is not the only user of the kvm_find_cpuid_entry() function. So fix this algorithm and call it from kvm_emulate_cpuid(). This fixes a crash of newer Linux kernels as KVM guests on AMD Bulldozer CPUs, where bogus values were returned in response to a CPUID intercept. Signed-off-by: Andre Przywara <andre.przywara@amd.com> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 20800bc commit bd22f5c

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

‎arch/x86/kvm/x86.c‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,12 +4958,6 @@ struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
49584958
best = e;
49594959
break;
49604960
}
4961-
/*
4962-
* Both basic or both extended?
4963-
*/
4964-
if (((e->function ^ function) & 0x80000000) == 0)
4965-
if (!best || e->function > best->function)
4966-
best = e;
49674961
}
49684962
return best;
49694963
}
@@ -4983,6 +4977,27 @@ int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
49834977
return 36;
49844978
}
49854979

4980+
/*
4981+
* If no match is found, check whether we exceed the vCPU's limit
4982+
* and return the content of the highest valid _standard_ leaf instead.
4983+
* This is to satisfy the CPUID specification.
4984+
*/
4985+
static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
4986+
u32 function, u32 index)
4987+
{
4988+
struct kvm_cpuid_entry2 *maxlevel;
4989+
4990+
maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
4991+
if (!maxlevel || maxlevel->eax >= function)
4992+
return NULL;
4993+
if (function & 0x80000000) {
4994+
maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
4995+
if (!maxlevel)
4996+
return NULL;
4997+
}
4998+
return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
4999+
}
5000+
49865001
void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
49875002
{
49885003
u32 function, index;
@@ -4995,6 +5010,10 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
49955010
kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
49965011
kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
49975012
best = kvm_find_cpuid_entry(vcpu, function, index);
5013+
5014+
if (!best)
5015+
best = check_cpuid_limit(vcpu, function, index);
5016+
49985017
if (best) {
49995018
kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
50005019
kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);

0 commit comments

Comments
 (0)