Skip to content

Commit f2c5aa3

Browse files
committed
KVM: selftests: Precisely mask off dynamic fields in CPUID test
When comparing vCPU CPUID entries against KVM's supported CPUID, mask off only the dynamic fields/bits instead of skipping the entire entry. Precisely masking bits isn't meaningfully more difficult than skipping entire entries, and will be necessary to maintain test coverage when a future commit enables OSXSAVE by default, i.e. makes one bit in all of CPUID.0x1 dynamic. Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com> Link: https://lore.kernel.org/r/20241003234337.273364-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent f891221 commit f2c5aa3

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

tools/testing/selftests/kvm/x86_64/cpuid_test.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212
#include "kvm_util.h"
1313
#include "processor.h"
1414

15-
/* CPUIDs known to differ */
16-
struct {
17-
u32 function;
18-
u32 index;
19-
} mangled_cpuids[] = {
20-
/*
21-
* These entries depend on the vCPU's XCR0 register and IA32_XSS MSR,
22-
* which are not controlled for by this test.
23-
*/
24-
{.function = 0xd, .index = 0},
25-
{.function = 0xd, .index = 1},
15+
struct cpuid_mask {
16+
union {
17+
struct {
18+
u32 eax;
19+
u32 ebx;
20+
u32 ecx;
21+
u32 edx;
22+
};
23+
u32 regs[4];
24+
};
2625
};
2726

2827
static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)
@@ -56,17 +55,23 @@ static void guest_main(struct kvm_cpuid2 *guest_cpuid)
5655
GUEST_DONE();
5756
}
5857

59-
static bool is_cpuid_mangled(const struct kvm_cpuid_entry2 *entrie)
58+
static struct cpuid_mask get_const_cpuid_mask(const struct kvm_cpuid_entry2 *entry)
6059
{
61-
int i;
62-
63-
for (i = 0; i < ARRAY_SIZE(mangled_cpuids); i++) {
64-
if (mangled_cpuids[i].function == entrie->function &&
65-
mangled_cpuids[i].index == entrie->index)
66-
return true;
60+
struct cpuid_mask mask;
61+
62+
memset(&mask, 0xff, sizeof(mask));
63+
64+
switch (entry->function) {
65+
case 0xd:
66+
/*
67+
* CPUID.0xD.{0,1}.EBX enumerate XSAVE size based on the current
68+
* XCR0 and IA32_XSS MSR values.
69+
*/
70+
if (entry->index < 2)
71+
mask.ebx = 0;
72+
break;
6773
}
68-
69-
return false;
74+
return mask;
7075
}
7176

7277
static void compare_cpuids(const struct kvm_cpuid2 *cpuid1,
@@ -79,6 +84,8 @@ static void compare_cpuids(const struct kvm_cpuid2 *cpuid1,
7984
"CPUID nent mismatch: %d vs. %d", cpuid1->nent, cpuid2->nent);
8085

8186
for (i = 0; i < cpuid1->nent; i++) {
87+
struct cpuid_mask mask;
88+
8289
e1 = &cpuid1->entries[i];
8390
e2 = &cpuid2->entries[i];
8491

@@ -88,15 +95,19 @@ static void compare_cpuids(const struct kvm_cpuid2 *cpuid1,
8895
i, e1->function, e1->index, e1->flags,
8996
e2->function, e2->index, e2->flags);
9097

91-
if (is_cpuid_mangled(e1))
92-
continue;
98+
/* Mask off dynamic bits, e.g. OSXSAVE, when comparing entries. */
99+
mask = get_const_cpuid_mask(e1);
93100

94-
TEST_ASSERT(e1->eax == e2->eax && e1->ebx == e2->ebx &&
95-
e1->ecx == e2->ecx && e1->edx == e2->edx,
101+
TEST_ASSERT((e1->eax & mask.eax) == (e2->eax & mask.eax) &&
102+
(e1->ebx & mask.ebx) == (e2->ebx & mask.ebx) &&
103+
(e1->ecx & mask.ecx) == (e2->ecx & mask.ecx) &&
104+
(e1->edx & mask.edx) == (e2->edx & mask.edx),
96105
"CPUID 0x%x.%x differ: 0x%x:0x%x:0x%x:0x%x vs 0x%x:0x%x:0x%x:0x%x",
97106
e1->function, e1->index,
98-
e1->eax, e1->ebx, e1->ecx, e1->edx,
99-
e2->eax, e2->ebx, e2->ecx, e2->edx);
107+
e1->eax & mask.eax, e1->ebx & mask.ebx,
108+
e1->ecx & mask.ecx, e1->edx & mask.edx,
109+
e2->eax & mask.eax, e2->ebx & mask.ebx,
110+
e2->ecx & mask.ecx, e2->edx & mask.edx);
100111
}
101112
}
102113

0 commit comments

Comments
 (0)