KVM: selftests: x86: Set supported CPUIDs on default VM
authorAndrew Jones <drjones@redhat.com>
Wed, 11 Nov 2020 12:26:35 +0000 (13:26 +0100)
committerPaolo Bonzini <pbonzini@redhat.com>
Mon, 16 Nov 2020 18:14:20 +0000 (13:14 -0500)
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>
18 files changed:
tools/testing/selftests/kvm/dirty_log_test.c
tools/testing/selftests/kvm/include/perf_test_util.h
tools/testing/selftests/kvm/lib/kvm_util.c
tools/testing/selftests/kvm/set_memory_region_test.c
tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c
tools/testing/selftests/kvm/x86_64/debug_regs.c
tools/testing/selftests/kvm/x86_64/evmcs_test.c
tools/testing/selftests/kvm/x86_64/smm_test.c
tools/testing/selftests/kvm/x86_64/state_test.c
tools/testing/selftests/kvm/x86_64/svm_vmcall_test.c
tools/testing/selftests/kvm/x86_64/tsc_msrs_test.c
tools/testing/selftests/kvm/x86_64/user_msr_test.c
tools/testing/selftests/kvm/x86_64/vmx_apic_access_test.c
tools/testing/selftests/kvm/x86_64/vmx_close_while_nested_test.c
tools/testing/selftests/kvm/x86_64/vmx_dirty_log_test.c
tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c
tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c
tools/testing/selftests/kvm/x86_64/vmx_tsc_adjust_test.c

index b1f8731..471baec 100644 (file)
@@ -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 */
index 2618052..239421e 100644 (file)
@@ -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);
index df2035a..b2c426a 100644 (file)
@@ -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;
 }
index b3ece55..5fa5823 100644 (file)
@@ -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);
index 140e919..f40fd09 100644 (file)
@@ -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) {
index 2fc6b3a..6097a82 100644 (file)
@@ -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 */
index 7579281..37b8a78 100644 (file)
@@ -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)) {
index ae39a22..613c42c 100644 (file)
@@ -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,
index f6c8b90..32854c1 100644 (file)
@@ -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);
index 0e1adb4..be2ca15 100644 (file)
@@ -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);
index f8e7611..e357d8e 100644 (file)
@@ -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);
index f2a667c..fe88b98 100644 (file)
@@ -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);
index 1f65342..d14888b 100644 (file)
@@ -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();
index fe40ade..2835a17 100644 (file)
@@ -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);
index e894a63..537de10 100644 (file)
@@ -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);
index 25b7830..a07480a 100644 (file)
@@ -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);
index d59f3eb..5827b9b 100644 (file)
@@ -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;
@@ -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);
 
index fbe8417..7e33a35 100644 (file)
@@ -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);