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

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
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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);
+26 −0
Original line number Diff line number Diff line
@@ -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
 *
+49 −38
Original line number Diff line number Diff line
@@ -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];
@@ -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,
@@ -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:
@@ -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;

	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;
@@ -137,7 +140,10 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)

	cpuid->nent = nent;

	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;
}
@@ -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);
		switch (stage) {
		case 0:
			test_hv_cpuid_e2big(vm);
			continue;
		case 1:
			break;
		case 2:

	/* 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);

	if (!nested_vmx_supported() ||
	    !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
		print_skip("Enlightened VMCS is unsupported");
				continue;
		goto do_sys;
	}
	vcpu_enable_evmcs(vm, VCPU_ID);
			evmcs_enabled = true;
			break;
	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;
	}

		hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
		test_hv_cpuid(hv_cpuid_entries, evmcs_enabled);
	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;
}