Skip to content

Commit 824d227

Browse files
committed
KVM: selftests: Add a CPUID testcase for KVM_SET_CPUID2 with runtime updates
Add a CPUID testcase to verify that KVM allows KVM_SET_CPUID2 after (or in conjunction with) runtime updates. This is a regression test for the bug introduced by commit 93da6af ("KVM: x86: Defer runtime updates of dynamic CPUID bits until CPUID emulation"), where KVM would incorrectly reject KVM_SET_CPUID due to a not handling a pending runtime update on the current CPUID, resulting in a false mismatch between the "old" and "new" CPUID entries. Link: https://lore.kernel.org/all/20251128123202.68424a95@imammedo Link: https://patch.msgid.link/20251202015049.1167490-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent e2b43fb commit 824d227

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,28 @@ struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, struct
155155
static void set_cpuid_after_run(struct kvm_vcpu *vcpu)
156156
{
157157
struct kvm_cpuid_entry2 *ent;
158+
struct kvm_sregs sregs;
158159
int rc;
159160
u32 eax, ebx, x;
160161

161162
/* Setting unmodified CPUID is allowed */
162163
rc = __vcpu_set_cpuid(vcpu);
163164
TEST_ASSERT(!rc, "Setting unmodified CPUID after KVM_RUN failed: %d", rc);
164165

166+
/*
167+
* Toggle CR4 bits that affect dynamic CPUID feature flags to verify
168+
* setting unmodified CPUID succeeds with runtime CPUID updates.
169+
*/
170+
vcpu_sregs_get(vcpu, &sregs);
171+
if (kvm_cpu_has(X86_FEATURE_XSAVE))
172+
sregs.cr4 ^= X86_CR4_OSXSAVE;
173+
if (kvm_cpu_has(X86_FEATURE_PKU))
174+
sregs.cr4 ^= X86_CR4_PKE;
175+
vcpu_sregs_set(vcpu, &sregs);
176+
177+
rc = __vcpu_set_cpuid(vcpu);
178+
TEST_ASSERT(!rc, "Setting unmodified CPUID after KVM_RUN failed: %d", rc);
179+
165180
/* Changing CPU features is forbidden */
166181
ent = vcpu_get_cpuid_entry(vcpu, 0x7);
167182
ebx = ent->ebx;

0 commit comments

Comments
 (0)