Skip to content

Commit 2c76131

Browse files
committed
KVM: selftests: Extend cpuid_test to verify KVM_GET_CPUID2 "nent" updates
Verify that KVM reports the actual number of CPUID entries on success, but doesn't touch the userspace struct on failure (which for better or worse, is KVM's ABI). Link: https://lore.kernel.org/r/20230526210340.2799158-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent ab322c4 commit 2c76131

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,25 @@ static void set_cpuid_after_run(struct kvm_vcpu *vcpu)
163163
ent->eax = eax;
164164
}
165165

166+
static void test_get_cpuid2(struct kvm_vcpu *vcpu)
167+
{
168+
struct kvm_cpuid2 *cpuid = allocate_kvm_cpuid2(vcpu->cpuid->nent + 1);
169+
int i, r;
170+
171+
vcpu_ioctl(vcpu, KVM_GET_CPUID2, cpuid);
172+
TEST_ASSERT(cpuid->nent == vcpu->cpuid->nent,
173+
"KVM didn't update nent on success, wanted %u, got %u\n",
174+
vcpu->cpuid->nent, cpuid->nent);
175+
176+
for (i = 0; i < vcpu->cpuid->nent; i++) {
177+
cpuid->nent = i;
178+
r = __vcpu_ioctl(vcpu, KVM_GET_CPUID2, cpuid);
179+
TEST_ASSERT(r && errno == E2BIG, KVM_IOCTL_ERROR(KVM_GET_CPUID2, r));
180+
TEST_ASSERT(cpuid->nent == i, "KVM modified nent on failure");
181+
}
182+
free(cpuid);
183+
}
184+
166185
int main(void)
167186
{
168187
struct kvm_vcpu *vcpu;
@@ -183,5 +202,7 @@ int main(void)
183202

184203
set_cpuid_after_run(vcpu);
185204

205+
test_get_cpuid2(vcpu);
206+
186207
kvm_vm_free(vm);
187208
}

0 commit comments

Comments
 (0)