Commit 8b460692 authored by Vitaly Kuznetsov's avatar Vitaly Kuznetsov Committed by Paolo Bonzini

KVM: selftests: test KVM_GET_SUPPORTED_HV_CPUID as a system ioctl

KVM_GET_SUPPORTED_HV_CPUID is now supported as both vCPU and VM ioctl,
test that.
Signed-off-by: default avatarVitaly Kuznetsov <vkuznets@redhat.com>
Message-Id: <20200929150944.1235688-3-vkuznets@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent c21d54f0
...@@ -114,6 +114,8 @@ void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl, ...@@ -114,6 +114,8 @@ void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl,
int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl, int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl,
void *arg); void *arg);
void vm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg); void vm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
void kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
int _kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags);
void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot); void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
......
...@@ -1582,6 +1582,32 @@ void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg) ...@@ -1582,6 +1582,32 @@ void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
cmd, ret, errno, strerror(errno)); cmd, ret, errno, strerror(errno));
} }
/*
* KVM system ioctl
*
* Input Args:
* vm - Virtual Machine
* cmd - Ioctl number
* arg - Argument to pass to the ioctl
*
* Return: None
*
* Issues an arbitrary ioctl on a KVM fd.
*/
void kvm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
{
int ret;
ret = ioctl(vm->kvm_fd, cmd, arg);
TEST_ASSERT(ret == 0, "KVM ioctl %lu failed, rc: %i errno: %i (%s)",
cmd, ret, errno, strerror(errno));
}
int _kvm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
{
return ioctl(vm->kvm_fd, cmd, arg);
}
/* /*
* VM Dump * VM Dump
* *
......
...@@ -46,19 +46,19 @@ static bool smt_possible(void) ...@@ -46,19 +46,19 @@ static bool smt_possible(void)
} }
static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
bool evmcs_enabled) bool evmcs_expected)
{ {
int i; int i;
int nent = 9; int nent = 9;
u32 test_val; u32 test_val;
if (evmcs_enabled) if (evmcs_expected)
nent += 1; /* 0x4000000A */ nent += 1; /* 0x4000000A */
TEST_ASSERT(hv_cpuid_entries->nent == nent, TEST_ASSERT(hv_cpuid_entries->nent == nent,
"KVM_GET_SUPPORTED_HV_CPUID should return %d entries" "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
" with evmcs=%d (returned %d)", " with evmcs=%d (returned %d)",
nent, evmcs_enabled, hv_cpuid_entries->nent); nent, evmcs_expected, hv_cpuid_entries->nent);
for (i = 0; i < hv_cpuid_entries->nent; i++) { for (i = 0; i < hv_cpuid_entries->nent; i++) {
struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i]; struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
...@@ -68,7 +68,7 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, ...@@ -68,7 +68,7 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
"function %x is our of supported range", "function %x is our of supported range",
entry->function); entry->function);
TEST_ASSERT(evmcs_enabled || (entry->function != 0x4000000A), TEST_ASSERT(evmcs_expected || (entry->function != 0x4000000A),
"0x4000000A leaf should not be reported"); "0x4000000A leaf should not be reported");
TEST_ASSERT(entry->index == 0, TEST_ASSERT(entry->index == 0,
...@@ -87,7 +87,7 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, ...@@ -87,7 +87,7 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
TEST_ASSERT(entry->eax == test_val, TEST_ASSERT(entry->eax == test_val,
"Wrong max leaf report in 0x40000000.EAX: %x" "Wrong max leaf report in 0x40000000.EAX: %x"
" (evmcs=%d)", " (evmcs=%d)",
entry->eax, evmcs_enabled entry->eax, evmcs_expected
); );
break; break;
case 0x40000004: case 0x40000004:
...@@ -110,20 +110,23 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, ...@@ -110,20 +110,23 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
} }
void test_hv_cpuid_e2big(struct kvm_vm *vm) void test_hv_cpuid_e2big(struct kvm_vm *vm, bool system)
{ {
static struct kvm_cpuid2 cpuid = {.nent = 0}; static struct kvm_cpuid2 cpuid = {.nent = 0};
int ret; int ret;
ret = _vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, &cpuid); if (!system)
ret = _vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
else
ret = _kvm_ioctl(vm, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
TEST_ASSERT(ret == -1 && errno == E2BIG, TEST_ASSERT(ret == -1 && errno == E2BIG,
"KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when" "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
" it should have: %d %d", ret, errno); " it should have: %d %d", system ? "KVM" : "vCPU", ret, errno);
} }
struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm) struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm, bool system)
{ {
int nent = 20; /* should be enough */ int nent = 20; /* should be enough */
static struct kvm_cpuid2 *cpuid; static struct kvm_cpuid2 *cpuid;
...@@ -137,7 +140,10 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm) ...@@ -137,7 +140,10 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)
cpuid->nent = nent; cpuid->nent = nent;
vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, cpuid); if (!system)
vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
else
kvm_ioctl(vm, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
return cpuid; return cpuid;
} }
...@@ -146,45 +152,50 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm) ...@@ -146,45 +152,50 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct kvm_vm *vm; struct kvm_vm *vm;
int rv, stage;
struct kvm_cpuid2 *hv_cpuid_entries; struct kvm_cpuid2 *hv_cpuid_entries;
bool evmcs_enabled;
/* Tell stdout not to buffer its content */ /* Tell stdout not to buffer its content */
setbuf(stdout, NULL); setbuf(stdout, NULL);
rv = kvm_check_cap(KVM_CAP_HYPERV_CPUID); if (!kvm_check_cap(KVM_CAP_HYPERV_CPUID)) {
if (!rv) {
print_skip("KVM_CAP_HYPERV_CPUID not supported"); print_skip("KVM_CAP_HYPERV_CPUID not supported");
exit(KSFT_SKIP); exit(KSFT_SKIP);
} }
for (stage = 0; stage < 3; stage++) { vm = vm_create_default(VCPU_ID, 0, guest_code);
evmcs_enabled = false;
vm = vm_create_default(VCPU_ID, 0, guest_code); /* Test vCPU ioctl version */
switch (stage) { test_hv_cpuid_e2big(vm, false);
case 0:
test_hv_cpuid_e2big(vm); hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm, false);
continue; test_hv_cpuid(hv_cpuid_entries, false);
case 1: free(hv_cpuid_entries);
break;
case 2:
if (!nested_vmx_supported() ||
!kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
print_skip("Enlightened VMCS is unsupported");
continue;
}
vcpu_enable_evmcs(vm, VCPU_ID);
evmcs_enabled = true;
break;
}
hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm); if (!nested_vmx_supported() ||
test_hv_cpuid(hv_cpuid_entries, evmcs_enabled); !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
free(hv_cpuid_entries); print_skip("Enlightened VMCS is unsupported");
kvm_vm_free(vm); goto do_sys;
} }
vcpu_enable_evmcs(vm, VCPU_ID);
hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm, false);
test_hv_cpuid(hv_cpuid_entries, true);
free(hv_cpuid_entries);
do_sys:
/* Test system ioctl version */
if (!kvm_check_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
goto out;
}
test_hv_cpuid_e2big(vm, true);
hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm, true);
test_hv_cpuid(hv_cpuid_entries, nested_vmx_supported());
free(hv_cpuid_entries);
out:
kvm_vm_free(vm);
return 0; return 0;
} }
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