Skip to content

Commit ecebb96

Browse files
vittyvkbonzini
authored andcommitted
KVM: selftests: Test KVM_SET_CPUID2 after KVM_RUN
KVM forbids KVM_SET_CPUID2 after KVM_RUN was performed on a vCPU unless the supplied CPUID data is equal to what was previously set. Test this. Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Message-Id: <20220117150542.2176196-5-vkuznets@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 9e6d484 commit ecebb96

3 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎tools/testing/selftests/kvm/include/x86_64/processor.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ uint64_t kvm_get_feature_msr(uint64_t msr_index);
375375
struct kvm_cpuid2 *kvm_get_supported_cpuid(void);
376376

377377
struct kvm_cpuid2 *vcpu_get_cpuid(struct kvm_vm *vm, uint32_t vcpuid);
378+
int __vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid,
379+
struct kvm_cpuid2 *cpuid);
378380
void vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid,
379381
struct kvm_cpuid2 *cpuid);
380382

@@ -418,6 +420,11 @@ uint64_t vm_get_page_table_entry(struct kvm_vm *vm, int vcpuid, uint64_t vaddr);
418420
void vm_set_page_table_entry(struct kvm_vm *vm, int vcpuid, uint64_t vaddr,
419421
uint64_t pte);
420422

423+
/*
424+
* get_cpuid() - find matching CPUID entry and return pointer to it.
425+
*/
426+
struct kvm_cpuid_entry2 *get_cpuid(struct kvm_cpuid2 *cpuid, uint32_t function,
427+
uint32_t index);
421428
/*
422429
* set_cpuid() - overwrites a matching cpuid entry with the provided value.
423430
* matches based on ent->function && ent->index. returns true

‎tools/testing/selftests/kvm/lib/x86_64/processor.c‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,17 @@ kvm_get_supported_cpuid_index(uint32_t function, uint32_t index)
886886
return entry;
887887
}
888888

889+
890+
int __vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid,
891+
struct kvm_cpuid2 *cpuid)
892+
{
893+
struct vcpu *vcpu = vcpu_find(vm, vcpuid);
894+
895+
TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
896+
897+
return ioctl(vcpu->fd, KVM_SET_CPUID2, cpuid);
898+
}
899+
889900
/*
890901
* VM VCPU CPUID Set
891902
*
@@ -903,12 +914,9 @@ kvm_get_supported_cpuid_index(uint32_t function, uint32_t index)
903914
void vcpu_set_cpuid(struct kvm_vm *vm,
904915
uint32_t vcpuid, struct kvm_cpuid2 *cpuid)
905916
{
906-
struct vcpu *vcpu = vcpu_find(vm, vcpuid);
907917
int rc;
908918

909-
TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
910-
911-
rc = ioctl(vcpu->fd, KVM_SET_CPUID2, cpuid);
919+
rc = __vcpu_set_cpuid(vm, vcpuid, cpuid);
912920
TEST_ASSERT(rc == 0, "KVM_SET_CPUID2 failed, rc: %i errno: %i",
913921
rc, errno);
914922

@@ -1384,6 +1392,23 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid)
13841392
}
13851393
}
13861394

1395+
struct kvm_cpuid_entry2 *get_cpuid(struct kvm_cpuid2 *cpuid, uint32_t function,
1396+
uint32_t index)
1397+
{
1398+
int i;
1399+
1400+
for (i = 0; i < cpuid->nent; i++) {
1401+
struct kvm_cpuid_entry2 *cur = &cpuid->entries[i];
1402+
1403+
if (cur->function == function && cur->index == index)
1404+
return cur;
1405+
}
1406+
1407+
TEST_FAIL("CPUID function 0x%x index 0x%x not found ", function, index);
1408+
1409+
return NULL;
1410+
}
1411+
13871412
bool set_cpuid(struct kvm_cpuid2 *cpuid,
13881413
struct kvm_cpuid_entry2 *ent)
13891414
{

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, struct
154154
return guest_cpuids;
155155
}
156156

157+
static void set_cpuid_after_run(struct kvm_vm *vm, struct kvm_cpuid2 *cpuid)
158+
{
159+
struct kvm_cpuid_entry2 *ent;
160+
int rc;
161+
u32 eax, ebx, x;
162+
163+
/* Setting unmodified CPUID is allowed */
164+
rc = __vcpu_set_cpuid(vm, VCPU_ID, cpuid);
165+
TEST_ASSERT(!rc, "Setting unmodified CPUID after KVM_RUN failed: %d", rc);
166+
167+
/* Changing CPU features is forbidden */
168+
ent = get_cpuid(cpuid, 0x7, 0);
169+
ebx = ent->ebx;
170+
ent->ebx--;
171+
rc = __vcpu_set_cpuid(vm, VCPU_ID, cpuid);
172+
TEST_ASSERT(rc, "Changing CPU features should fail");
173+
ent->ebx = ebx;
174+
175+
/* Changing MAXPHYADDR is forbidden */
176+
ent = get_cpuid(cpuid, 0x80000008, 0);
177+
eax = ent->eax;
178+
x = eax & 0xff;
179+
ent->eax = (eax & ~0xffu) | (x - 1);
180+
rc = __vcpu_set_cpuid(vm, VCPU_ID, cpuid);
181+
TEST_ASSERT(rc, "Changing MAXPHYADDR should fail");
182+
ent->eax = eax;
183+
}
184+
157185
int main(void)
158186
{
159187
struct kvm_cpuid2 *supp_cpuid, *cpuid2;
@@ -175,5 +203,7 @@ int main(void)
175203
for (stage = 0; stage < 3; stage++)
176204
run_vcpu(vm, VCPU_ID, stage);
177205

206+
set_cpuid_after_run(vm, cpuid2);
207+
178208
kvm_vm_free(vm);
179209
}

0 commit comments

Comments
 (0)