Skip to content

Commit

Permalink
KVM: selftests: x86: Set supported CPUIDs on default VM
Browse files Browse the repository at this point in the history
Almost all tests do this anyway and the ones that don't don't
appear to care. Only vmx_set_nested_state_test assumes that
a feature (VMX) is disabled until later setting the supported
CPUIDs. It's better to disable that explicitly anyway.

Signed-off-by: Andrew Jones <drjones@redhat.com>
Message-Id: <20201111122636.73346-11-drjones@redhat.com>
[Restore CPUID_VMX, or vmx_set_nested_state breaks. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
Andrew Jones authored and Paolo Bonzini committed Nov 16, 2020
1 parent 08d3e27 commit 22f232d
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 26 deletions.
3 changes: 0 additions & 3 deletions tools/testing/selftests/kvm/dirty_log_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
/* Cache the HVA pointer of the region */
host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_phys_mem);

#ifdef __x86_64__
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
#endif
ucall_init(vm, NULL);

/* Export the shared variables to the guest */
Expand Down
4 changes: 0 additions & 4 deletions tools/testing/selftests/kvm/include/perf_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ static void add_vcpus(struct kvm_vm *vm, int vcpus, uint64_t vcpu_memory_bytes)

vm_vcpu_add_default(vm, vcpu_id, guest_code);

#ifdef __x86_64__
vcpu_set_cpuid(vm, vcpu_id, kvm_get_supported_cpuid());
#endif

vcpu_args->vcpu_id = vcpu_id;
vcpu_args->gva = guest_test_virt_mem +
(vcpu_id * vcpu_memory_bytes);
Expand Down
11 changes: 9 additions & 2 deletions tools/testing/selftests/kvm/lib/kvm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,15 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
vm_create_irqchip(vm);
#endif

for (i = 0; i < nr_vcpus; ++i)
vm_vcpu_add_default(vm, vcpuids ? vcpuids[i] : i, guest_code);
for (i = 0; i < nr_vcpus; ++i) {
uint32_t vcpuid = vcpuids ? vcpuids[i] : i;

vm_vcpu_add_default(vm, vcpuid, guest_code);

#ifdef __x86_64__
vcpu_set_cpuid(vm, vcpuid, kvm_get_supported_cpuid());
#endif
}

return vm;
}
Expand Down
2 changes: 0 additions & 2 deletions tools/testing/selftests/kvm/set_memory_region_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ static struct kvm_vm *spawn_vm(pthread_t *vcpu_thread, void *guest_code)

vm = vm_create_default(VCPU_ID, 0, guest_code);

vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());

vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_THP,
MEM_REGION_GPA, MEM_REGION_SLOT,
MEM_REGION_SIZE / getpagesize(), 0);
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ int main(int argc, char *argv[])

/* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
run = vcpu_state(vm, VCPU_ID);

while (1) {
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/debug_regs.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ int main(void)
}

vm = vm_create_default(VCPU_ID, 0, guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
run = vcpu_state(vm, VCPU_ID);

/* Test software BPs - int3 */
Expand Down
2 changes: 0 additions & 2 deletions tools/testing/selftests/kvm/x86_64/evmcs_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ int main(int argc, char *argv[])
/* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code);

vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());

if (!nested_vmx_supported() ||
!kvm_check_cap(KVM_CAP_NESTED_STATE) ||
!kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
Expand Down
2 changes: 0 additions & 2 deletions tools/testing/selftests/kvm/x86_64/smm_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ int main(int argc, char *argv[])
/* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code);

vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());

run = vcpu_state(vm, VCPU_ID);

vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/state_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ int main(int argc, char *argv[])

/* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
run = vcpu_state(vm, VCPU_ID);

vcpu_regs_get(vm, VCPU_ID, &regs1);
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/svm_vmcall_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ int main(int argc, char *argv[])
nested_svm_check_supported();

vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());

vcpu_alloc_svm(vm, &svm_gva);
vcpu_args_set(vm, VCPU_ID, 1, svm_gva);
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/tsc_msrs_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ int main(void)
uint64_t val;

vm = vm_create_default(VCPU_ID, 0, guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());

val = 0;
ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/user_msr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ int main(int argc, char *argv[])

/* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
run = vcpu_state(vm, VCPU_ID);

rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/vmx_apic_access_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ int main(int argc, char *argv[])
nested_vmx_check_supported();

vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());

kvm_get_cpu_address_width(&paddr_width, &vaddr_width);
high_gpa = (1ul << paddr_width) - getpagesize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ int main(int argc, char *argv[])
nested_vmx_check_supported();

vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());

/* Allocate VMX pages and shared descriptors (vmx_pages). */
vcpu_alloc_vmx(vm, &vmx_pages_gva);
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/vmx_dirty_log_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ int main(int argc, char *argv[])

/* Create VM */
vm = vm_create_default(VCPU_ID, 0, l1_guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
vmx = vcpu_alloc_vmx(vm, &vmx_pages_gva);
vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
run = vcpu_state(vm, VCPU_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ int main(int argc, char *argv[])

/* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
run = vcpu_state(vm, VCPU_ID);

vcpu_regs_get(vm, VCPU_ID, &regs1);
Expand Down
21 changes: 21 additions & 0 deletions tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ void test_vmx_nested_state(struct kvm_vm *vm)
free(state);
}

void disable_vmx(struct kvm_vm *vm)
{
struct kvm_cpuid2 *cpuid = kvm_get_supported_cpuid();
int i;

for (i = 0; i < cpuid->nent; ++i)
if (cpuid->entries[i].function == 1 &&
cpuid->entries[i].index == 0)
break;
TEST_ASSERT(i != cpuid->nent, "CPUID function 1 not found");

cpuid->entries[i].ecx &= ~CPUID_VMX;
vcpu_set_cpuid(vm, VCPU_ID, cpuid);
cpuid->entries[i].ecx |= CPUID_VMX;
}

int main(int argc, char *argv[])
{
struct kvm_vm *vm;
Expand All @@ -264,6 +280,11 @@ int main(int argc, char *argv[])

vm = vm_create_default(VCPU_ID, 0, 0);

/*
* First run tests with VMX disabled to check error handling.
*/
disable_vmx(vm);

/* Passing a NULL kvm_nested_state causes a EFAULT. */
test_nested_state_expect_efault(vm, NULL);

Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/kvm/x86_64/vmx_tsc_adjust_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ int main(int argc, char *argv[])
nested_vmx_check_supported();

vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());

/* Allocate VMX pages and shared descriptors (vmx_pages). */
vcpu_alloc_vmx(vm, &vmx_pages_gva);
Expand Down

0 comments on commit 22f232d

Please sign in to comment.