KVM: move and fix substitue search for missing CPUID entries
authorAndre Przywara <andre.przywara@amd.com>
Thu, 31 Mar 2011 14:58:49 +0000 (16:58 +0200)
committerAvi Kivity <avi@redhat.com>
Wed, 6 Apr 2011 10:15:56 +0000 (13:15 +0300)
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>
arch/x86/kvm/x86.c

index 4a1ba05f7af303969e0a89eb873b4bc1afbdf62c..934b4c6b0bf9abea16252e45ac459ed8b398681e 100644 (file)
@@ -4958,12 +4958,6 @@ struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
                        best = e;
                        break;
                }
-               /*
-                * Both basic or both extended?
-                */
-               if (((e->function ^ function) & 0x80000000) == 0)
-                       if (!best || e->function > best->function)
-                               best = e;
        }
        return best;
 }
@@ -4983,6 +4977,27 @@ not_found:
        return 36;
 }
 
+/*
+ * If no match is found, check whether we exceed the vCPU's limit
+ * and return the content of the highest valid _standard_ leaf instead.
+ * This is to satisfy the CPUID specification.
+ */
+static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
+                                                  u32 function, u32 index)
+{
+       struct kvm_cpuid_entry2 *maxlevel;
+
+       maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
+       if (!maxlevel || maxlevel->eax >= function)
+               return NULL;
+       if (function & 0x80000000) {
+               maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
+               if (!maxlevel)
+                       return NULL;
+       }
+       return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
+}
+
 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
 {
        u32 function, index;
@@ -4995,6 +5010,10 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
        kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
        kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
        best = kvm_find_cpuid_entry(vcpu, function, index);
+
+       if (!best)
+               best = check_cpuid_limit(vcpu, function, index);
+
        if (best) {
                kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
                kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);