Commit 22f232d1 authored by Andrew Jones's avatar Andrew Jones Committed by Paolo Bonzini

KVM: selftests: x86: Set supported CPUIDs on default VM

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: default avatarAndrew 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: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 08d3e277
...@@ -740,9 +740,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, ...@@ -740,9 +740,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
/* Cache the HVA pointer of the region */ /* Cache the HVA pointer of the region */
host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_phys_mem); 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); ucall_init(vm, NULL);
/* Export the shared variables to the guest */ /* Export the shared variables to the guest */
......
...@@ -179,10 +179,6 @@ static void add_vcpus(struct kvm_vm *vm, int vcpus, uint64_t vcpu_memory_bytes) ...@@ -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); 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->vcpu_id = vcpu_id;
vcpu_args->gva = guest_test_virt_mem + vcpu_args->gva = guest_test_virt_mem +
(vcpu_id * vcpu_memory_bytes); (vcpu_id * vcpu_memory_bytes);
......
...@@ -311,8 +311,15 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, ...@@ -311,8 +311,15 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
vm_create_irqchip(vm); vm_create_irqchip(vm);
#endif #endif
for (i = 0; i < nr_vcpus; ++i) for (i = 0; i < nr_vcpus; ++i) {
vm_vcpu_add_default(vm, vcpuids ? vcpuids[i] : i, guest_code); 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; return vm;
} }
......
...@@ -121,8 +121,6 @@ static struct kvm_vm *spawn_vm(pthread_t *vcpu_thread, void *guest_code) ...@@ -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); 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, vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_THP,
MEM_REGION_GPA, MEM_REGION_SLOT, MEM_REGION_GPA, MEM_REGION_SLOT,
MEM_REGION_SIZE / getpagesize(), 0); MEM_REGION_SIZE / getpagesize(), 0);
......
...@@ -81,7 +81,6 @@ int main(int argc, char *argv[]) ...@@ -81,7 +81,6 @@ int main(int argc, char *argv[])
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code); 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); run = vcpu_state(vm, VCPU_ID);
while (1) { while (1) {
......
...@@ -85,7 +85,6 @@ int main(void) ...@@ -85,7 +85,6 @@ int main(void)
} }
vm = vm_create_default(VCPU_ID, 0, guest_code); 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); run = vcpu_state(vm, VCPU_ID);
/* Test software BPs - int3 */ /* Test software BPs - int3 */
......
...@@ -92,8 +92,6 @@ int main(int argc, char *argv[]) ...@@ -92,8 +92,6 @@ int main(int argc, char *argv[])
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code); vm = vm_create_default(VCPU_ID, 0, guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
if (!nested_vmx_supported() || if (!nested_vmx_supported() ||
!kvm_check_cap(KVM_CAP_NESTED_STATE) || !kvm_check_cap(KVM_CAP_NESTED_STATE) ||
!kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) { !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
......
...@@ -102,8 +102,6 @@ int main(int argc, char *argv[]) ...@@ -102,8 +102,6 @@ int main(int argc, char *argv[])
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code); 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); run = vcpu_state(vm, VCPU_ID);
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA, vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
......
...@@ -165,7 +165,6 @@ int main(int argc, char *argv[]) ...@@ -165,7 +165,6 @@ int main(int argc, char *argv[])
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code); 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); run = vcpu_state(vm, VCPU_ID);
vcpu_regs_get(vm, VCPU_ID, &regs1); vcpu_regs_get(vm, VCPU_ID, &regs1);
......
...@@ -44,7 +44,6 @@ int main(int argc, char *argv[]) ...@@ -44,7 +44,6 @@ int main(int argc, char *argv[])
nested_svm_check_supported(); nested_svm_check_supported();
vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code); 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_alloc_svm(vm, &svm_gva);
vcpu_args_set(vm, VCPU_ID, 1, svm_gva); vcpu_args_set(vm, VCPU_ID, 1, svm_gva);
......
...@@ -107,7 +107,6 @@ int main(void) ...@@ -107,7 +107,6 @@ int main(void)
uint64_t val; uint64_t val;
vm = vm_create_default(VCPU_ID, 0, guest_code); vm = vm_create_default(VCPU_ID, 0, guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
val = 0; val = 0;
ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val); ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
......
...@@ -205,7 +205,6 @@ int main(int argc, char *argv[]) ...@@ -205,7 +205,6 @@ int main(int argc, char *argv[])
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code); 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); run = vcpu_state(vm, VCPU_ID);
rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR); rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
......
...@@ -87,7 +87,6 @@ int main(int argc, char *argv[]) ...@@ -87,7 +87,6 @@ int main(int argc, char *argv[])
nested_vmx_check_supported(); nested_vmx_check_supported();
vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code); 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); kvm_get_cpu_address_width(&paddr_width, &vaddr_width);
high_gpa = (1ul << paddr_width) - getpagesize(); high_gpa = (1ul << paddr_width) - getpagesize();
......
...@@ -57,7 +57,6 @@ int main(int argc, char *argv[]) ...@@ -57,7 +57,6 @@ int main(int argc, char *argv[])
nested_vmx_check_supported(); nested_vmx_check_supported();
vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code); 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). */ /* Allocate VMX pages and shared descriptors (vmx_pages). */
vcpu_alloc_vmx(vm, &vmx_pages_gva); vcpu_alloc_vmx(vm, &vmx_pages_gva);
......
...@@ -82,7 +82,6 @@ int main(int argc, char *argv[]) ...@@ -82,7 +82,6 @@ int main(int argc, char *argv[])
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, l1_guest_code); 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); vmx = vcpu_alloc_vmx(vm, &vmx_pages_gva);
vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva); vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
run = vcpu_state(vm, VCPU_ID); run = vcpu_state(vm, VCPU_ID);
......
...@@ -176,7 +176,6 @@ int main(int argc, char *argv[]) ...@@ -176,7 +176,6 @@ int main(int argc, char *argv[])
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code); 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); run = vcpu_state(vm, VCPU_ID);
vcpu_regs_get(vm, VCPU_ID, &regs1); vcpu_regs_get(vm, VCPU_ID, &regs1);
......
...@@ -244,6 +244,22 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -244,6 +244,22 @@ void test_vmx_nested_state(struct kvm_vm *vm)
free(state); 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[]) int main(int argc, char *argv[])
{ {
struct kvm_vm *vm; struct kvm_vm *vm;
...@@ -264,6 +280,11 @@ int main(int argc, char *argv[]) ...@@ -264,6 +280,11 @@ int main(int argc, char *argv[])
vm = vm_create_default(VCPU_ID, 0, 0); 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. */ /* Passing a NULL kvm_nested_state causes a EFAULT. */
test_nested_state_expect_efault(vm, NULL); test_nested_state_expect_efault(vm, NULL);
......
...@@ -132,7 +132,6 @@ int main(int argc, char *argv[]) ...@@ -132,7 +132,6 @@ int main(int argc, char *argv[])
nested_vmx_check_supported(); nested_vmx_check_supported();
vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code); 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). */ /* Allocate VMX pages and shared descriptors (vmx_pages). */
vcpu_alloc_vmx(vm, &vmx_pages_gva); vcpu_alloc_vmx(vm, &vmx_pages_gva);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment