Skip to content

Commit

Permalink
KVM: selftests: test KVM_GET_SUPPORTED_HV_CPUID as a system ioctl
Browse files Browse the repository at this point in the history
KVM_GET_SUPPORTED_HV_CPUID is now supported as both vCPU and VM ioctl,
test that.

Signed-off-by: Vitaly Kuznetsov <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
vittyvk authored and bonzini committed Nov 15, 2020
1 parent c21d54f commit 8b46069
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 38 deletions.
2 changes: 2 additions & 0 deletions tools/testing/selftests/kvm/include/kvm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
Expand Down
26 changes: 26 additions & 0 deletions tools/testing/selftests/kvm/lib/kvm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,32 @@ void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
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
*
Expand Down
87 changes: 49 additions & 38 deletions tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ static bool smt_possible(void)
}

static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
bool evmcs_enabled)
bool evmcs_expected)
{
int i;
int nent = 9;
u32 test_val;

if (evmcs_enabled)
if (evmcs_expected)
nent += 1; /* 0x4000000A */

TEST_ASSERT(hv_cpuid_entries->nent == nent,
"KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
" 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++) {
struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
Expand All @@ -68,7 +68,7 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
"function %x is our of supported range",
entry->function);

TEST_ASSERT(evmcs_enabled || (entry->function != 0x4000000A),
TEST_ASSERT(evmcs_expected || (entry->function != 0x4000000A),
"0x4000000A leaf should not be reported");

TEST_ASSERT(entry->index == 0,
Expand All @@ -87,7 +87,7 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
TEST_ASSERT(entry->eax == test_val,
"Wrong max leaf report in 0x40000000.EAX: %x"
" (evmcs=%d)",
entry->eax, evmcs_enabled
entry->eax, evmcs_expected
);
break;
case 0x40000004:
Expand All @@ -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};
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,
"KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
" it should have: %d %d", ret, errno);
"%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
" 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 */
static struct kvm_cpuid2 *cpuid;
Expand All @@ -137,7 +140,10 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)

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;
}
Expand All @@ -146,45 +152,50 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)
int main(int argc, char *argv[])
{
struct kvm_vm *vm;
int rv, stage;
struct kvm_cpuid2 *hv_cpuid_entries;
bool evmcs_enabled;

/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);

rv = kvm_check_cap(KVM_CAP_HYPERV_CPUID);
if (!rv) {
if (!kvm_check_cap(KVM_CAP_HYPERV_CPUID)) {
print_skip("KVM_CAP_HYPERV_CPUID not supported");
exit(KSFT_SKIP);
}

for (stage = 0; stage < 3; stage++) {
evmcs_enabled = false;
vm = vm_create_default(VCPU_ID, 0, guest_code);

vm = vm_create_default(VCPU_ID, 0, guest_code);
switch (stage) {
case 0:
test_hv_cpuid_e2big(vm);
continue;
case 1:
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;
}
/* Test vCPU ioctl version */
test_hv_cpuid_e2big(vm, false);

hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm, false);
test_hv_cpuid(hv_cpuid_entries, false);
free(hv_cpuid_entries);

hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
test_hv_cpuid(hv_cpuid_entries, evmcs_enabled);
free(hv_cpuid_entries);
kvm_vm_free(vm);
if (!nested_vmx_supported() ||
!kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
print_skip("Enlightened VMCS is unsupported");
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;
}

0 comments on commit 8b46069

Please sign in to comment.