KVM: selftests: Verify that kvm_cpuid2.entries layout is unchanged by KVM
authorSean Christopherson <seanjc@google.com>
Tue, 14 Jun 2022 20:06:41 +0000 (20:06 +0000)
committerSean Christopherson <seanjc@google.com>
Thu, 14 Jul 2022 01:14:14 +0000 (18:14 -0700)
In the CPUID test, verify that KVM doesn't modify the kvm_cpuid2.entries
layout, i.e. that the order of entries and their flags is identical
between what the test provides via KVM_SET_CPUID2 and what KVM returns
via KVM_GET_CPUID2.

Asserting that the layouts match simplifies the test as there's no need
to iterate over both arrays.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20220614200707.3315957-17-seanjc@google.com
tools/testing/selftests/kvm/x86_64/cpuid_test.c

index 3767a0c..59dcdf5 100644 (file)
@@ -79,41 +79,34 @@ static bool is_cpuid_mangled(struct kvm_cpuid_entry2 *entrie)
        return false;
 }
 
-static void check_cpuid(struct kvm_cpuid2 *cpuid, struct kvm_cpuid_entry2 *entrie)
+static void compare_cpuids(struct kvm_cpuid2 *cpuid1, struct kvm_cpuid2 *cpuid2)
 {
+       struct kvm_cpuid_entry2 *e1, *e2;
        int i;
 
-       for (i = 0; i < cpuid->nent; i++) {
-               if (cpuid->entries[i].function == entrie->function &&
-                   cpuid->entries[i].index == entrie->index) {
-                       if (is_cpuid_mangled(entrie))
-                               return;
-
-                       TEST_ASSERT(cpuid->entries[i].eax == entrie->eax &&
-                                   cpuid->entries[i].ebx == entrie->ebx &&
-                                   cpuid->entries[i].ecx == entrie->ecx &&
-                                   cpuid->entries[i].edx == entrie->edx,
-                                   "CPUID 0x%x.%x differ: 0x%x:0x%x:0x%x:0x%x vs 0x%x:0x%x:0x%x:0x%x",
-                                   entrie->function, entrie->index,
-                                   cpuid->entries[i].eax, cpuid->entries[i].ebx,
-                                   cpuid->entries[i].ecx, cpuid->entries[i].edx,
-                                   entrie->eax, entrie->ebx, entrie->ecx, entrie->edx);
-                       return;
-               }
-       }
+       TEST_ASSERT(cpuid1->nent == cpuid2->nent,
+                   "CPUID nent mismatch: %d vs. %d", cpuid1->nent, cpuid2->nent);
 
-       TEST_ASSERT(false, "CPUID 0x%x.%x not found", entrie->function, entrie->index);
-}
+       for (i = 0; i < cpuid1->nent; i++) {
+               e1 = &cpuid1->entries[i];
+               e2 = &cpuid2->entries[i];
 
-static void compare_cpuids(struct kvm_cpuid2 *cpuid1, struct kvm_cpuid2 *cpuid2)
-{
-       int i;
+               TEST_ASSERT(e1->function == e2->function &&
+                           e1->index == e2->index && e1->flags == e2->flags,
+                           "CPUID entries[%d] mismtach: 0x%x.%d.%x vs. 0x%x.%d.%x\n",
+                           i, e1->function, e1->index, e1->flags,
+                           e2->function, e2->index, e2->flags);
 
-       for (i = 0; i < cpuid1->nent; i++)
-               check_cpuid(cpuid2, &cpuid1->entries[i]);
+               if (is_cpuid_mangled(e1))
+                       continue;
 
-       for (i = 0; i < cpuid2->nent; i++)
-               check_cpuid(cpuid1, &cpuid2->entries[i]);
+               TEST_ASSERT(e1->eax == e2->eax && e1->ebx == e2->ebx &&
+                           e1->ecx == e2->ecx && e1->edx == e2->edx,
+                           "CPUID 0x%x.%x differ: 0x%x:0x%x:0x%x:0x%x vs 0x%x:0x%x:0x%x:0x%x",
+                           e1->function, e1->index,
+                           e1->eax, e1->ebx, e1->ecx, e1->edx,
+                           e2->eax, e2->ebx, e2->ecx, e2->edx);
+       }
 }
 
 static void run_vcpu(struct kvm_vcpu *vcpu, int stage)