KVM: x86: Manually retrieve CPUID.0x1 when getting FMS for RESET/INIT

Manually look for a CPUID.0x1 entry instead of bouncing through
kvm_cpuid() when retrieving the Family-Model-Stepping information for
vCPU RESET/INIT.  This fixes a potential undefined behavior bug due to
kvm_cpuid() using the uninitialized "dummy" param as the ECX _input_,
a.k.a. the index.

A more minimal fix would be to simply zero "dummy", but the extra work in
kvm_cpuid() is wasteful, and KVM should be treating the FMS retrieval as
an out-of-band access, e.g. same as how KVM computes guest.MAXPHYADDR.
Both Intel's SDM and AMD's APM describe the RDX value at RESET/INIT as
holding the CPU's FMS information, not as holding CPUID.0x1.EAX.  KVM's
usage of CPUID entries to get FMS is simply a pragmatic approach to avoid
having yet another way for userspace to provide inconsistent data.

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Message-Id: <20210929222426.1855730-3-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Sean Christopherson
2021-09-29 15:24:26 -07:00
committed by Paolo Bonzini
parent 62dd57dd67
commit 25b9784586

View File

@@ -10779,9 +10779,9 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
{ {
struct kvm_cpuid_entry2 *cpuid_0x1;
unsigned long old_cr0 = kvm_read_cr0(vcpu); unsigned long old_cr0 = kvm_read_cr0(vcpu);
unsigned long new_cr0; unsigned long new_cr0;
u32 eax, dummy;
/* /*
* Several of the "set" flows, e.g. ->set_cr0(), read other registers * Several of the "set" flows, e.g. ->set_cr0(), read other registers
@@ -10862,13 +10862,11 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
* Fall back to KVM's default Family/Model/Stepping of 0x600 (P6/Athlon) * Fall back to KVM's default Family/Model/Stepping of 0x600 (P6/Athlon)
* if no CPUID match is found. Note, it's impossible to get a match at * if no CPUID match is found. Note, it's impossible to get a match at
* RESET since KVM emulates RESET before exposing the vCPU to userspace, * RESET since KVM emulates RESET before exposing the vCPU to userspace,
* i.e. it'simpossible for kvm_cpuid() to find a valid entry on RESET. * i.e. it's impossible for kvm_find_cpuid_entry() to find a valid entry
* But, go through the motions in case that's ever remedied. * on RESET. But, go through the motions in case that's ever remedied.
*/ */
eax = 1; cpuid_0x1 = kvm_find_cpuid_entry(vcpu, 1, 0);
if (!kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true)) kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600);
eax = 0x600;
kvm_rdx_write(vcpu, eax);
vcpu->arch.ia32_xss = 0; vcpu->arch.ia32_xss = 0;