xref: /linux/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c (revision f4b0c4b508364fde023e4f7b9f23f7e38c663dfe)
17edcb734SVitaly Kuznetsov // SPDX-License-Identifier: GPL-2.0
27edcb734SVitaly Kuznetsov /*
37edcb734SVitaly Kuznetsov  * Test for x86 KVM_CAP_HYPERV_CPUID
47edcb734SVitaly Kuznetsov  *
57edcb734SVitaly Kuznetsov  * Copyright (C) 2018, Red Hat, Inc.
67edcb734SVitaly Kuznetsov  *
77edcb734SVitaly Kuznetsov  * This work is licensed under the terms of the GNU GPL, version 2.
87edcb734SVitaly Kuznetsov  *
97edcb734SVitaly Kuznetsov  */
107edcb734SVitaly Kuznetsov #include <fcntl.h>
117edcb734SVitaly Kuznetsov #include <stdio.h>
127edcb734SVitaly Kuznetsov #include <stdlib.h>
137edcb734SVitaly Kuznetsov #include <string.h>
147edcb734SVitaly Kuznetsov #include <sys/ioctl.h>
157edcb734SVitaly Kuznetsov 
167edcb734SVitaly Kuznetsov #include "test_util.h"
177edcb734SVitaly Kuznetsov #include "kvm_util.h"
187edcb734SVitaly Kuznetsov #include "processor.h"
1965efa61dSPaolo Bonzini #include "vmx.h"
207edcb734SVitaly Kuznetsov 
guest_code(void)217edcb734SVitaly Kuznetsov static void guest_code(void)
227edcb734SVitaly Kuznetsov {
237edcb734SVitaly Kuznetsov }
247edcb734SVitaly Kuznetsov 
smt_possible(void)25fb0cb6a8SVitaly Kuznetsov static bool smt_possible(void)
26e738772eSVitaly Kuznetsov {
27e738772eSVitaly Kuznetsov 	char buf[16];
28e738772eSVitaly Kuznetsov 	FILE *f;
29fb0cb6a8SVitaly Kuznetsov 	bool res = true;
30e738772eSVitaly Kuznetsov 
31e738772eSVitaly Kuznetsov 	f = fopen("/sys/devices/system/cpu/smt/control", "r");
32e738772eSVitaly Kuznetsov 	if (f) {
33e738772eSVitaly Kuznetsov 		if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
34e738772eSVitaly Kuznetsov 			if (!strncmp(buf, "forceoff", 8) ||
35e738772eSVitaly Kuznetsov 			    !strncmp(buf, "notsupported", 12))
36fb0cb6a8SVitaly Kuznetsov 				res = false;
37e738772eSVitaly Kuznetsov 		}
38e738772eSVitaly Kuznetsov 		fclose(f);
39e738772eSVitaly Kuznetsov 	}
40e738772eSVitaly Kuznetsov 
41e738772eSVitaly Kuznetsov 	return res;
42e738772eSVitaly Kuznetsov }
43e738772eSVitaly Kuznetsov 
test_hv_cpuid(const struct kvm_cpuid2 * hv_cpuid_entries,bool evmcs_expected)44*813e38cdSSean Christopherson static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
458b460692SVitaly Kuznetsov 			  bool evmcs_expected)
467edcb734SVitaly Kuznetsov {
477edcb734SVitaly Kuznetsov 	int i;
486081f9c7SVitaly Kuznetsov 	int nent_expected = 10;
49fb0cb6a8SVitaly Kuznetsov 	u32 test_val;
507edcb734SVitaly Kuznetsov 
516081f9c7SVitaly Kuznetsov 	TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
52fb0cb6a8SVitaly Kuznetsov 		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
536081f9c7SVitaly Kuznetsov 		    " (returned %d)",
546081f9c7SVitaly Kuznetsov 		    nent_expected, hv_cpuid_entries->nent);
557edcb734SVitaly Kuznetsov 
567edcb734SVitaly Kuznetsov 	for (i = 0; i < hv_cpuid_entries->nent; i++) {
57*813e38cdSSean Christopherson 		const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
587edcb734SVitaly Kuznetsov 
597edcb734SVitaly Kuznetsov 		TEST_ASSERT((entry->function >= 0x40000000) &&
60fb0cb6a8SVitaly Kuznetsov 			    (entry->function <= 0x40000082),
61d9eaf19eSAndrew Jones 			    "function %x is our of supported range",
627edcb734SVitaly Kuznetsov 			    entry->function);
637edcb734SVitaly Kuznetsov 
647edcb734SVitaly Kuznetsov 		TEST_ASSERT(entry->index == 0,
657edcb734SVitaly Kuznetsov 			    ".index field should be zero");
667edcb734SVitaly Kuznetsov 
677edcb734SVitaly Kuznetsov 		TEST_ASSERT(entry->flags == 0,
687edcb734SVitaly Kuznetsov 			    ".flags field should be zero");
697edcb734SVitaly Kuznetsov 
70be7fcf1dSDan Carpenter 		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
71be7fcf1dSDan Carpenter 			    !entry->padding[2], "padding should be zero");
727edcb734SVitaly Kuznetsov 
73fb0cb6a8SVitaly Kuznetsov 		switch (entry->function) {
74fb0cb6a8SVitaly Kuznetsov 		case 0x40000000:
75fb0cb6a8SVitaly Kuznetsov 			test_val = 0x40000082;
76e738772eSVitaly Kuznetsov 
77fb0cb6a8SVitaly Kuznetsov 			TEST_ASSERT(entry->eax == test_val,
78fb0cb6a8SVitaly Kuznetsov 				    "Wrong max leaf report in 0x40000000.EAX: %x"
79fb0cb6a8SVitaly Kuznetsov 				    " (evmcs=%d)",
808b460692SVitaly Kuznetsov 				    entry->eax, evmcs_expected
81fb0cb6a8SVitaly Kuznetsov 				);
82fb0cb6a8SVitaly Kuznetsov 			break;
83fb0cb6a8SVitaly Kuznetsov 		case 0x40000004:
84fb0cb6a8SVitaly Kuznetsov 			test_val = entry->eax & (1UL << 18);
85fb0cb6a8SVitaly Kuznetsov 
86fb0cb6a8SVitaly Kuznetsov 			TEST_ASSERT(!!test_val == !smt_possible(),
87e738772eSVitaly Kuznetsov 				    "NoNonArchitecturalCoreSharing bit"
88e738772eSVitaly Kuznetsov 				    " doesn't reflect SMT setting");
89fb0cb6a8SVitaly Kuznetsov 			break;
906081f9c7SVitaly Kuznetsov 		case 0x4000000A:
916081f9c7SVitaly Kuznetsov 			TEST_ASSERT(entry->eax & (1UL << 19),
926081f9c7SVitaly Kuznetsov 				    "Enlightened MSR-Bitmap should always be supported"
936081f9c7SVitaly Kuznetsov 				    " 0x40000000.EAX: %x", entry->eax);
946081f9c7SVitaly Kuznetsov 			if (evmcs_expected)
956081f9c7SVitaly Kuznetsov 				TEST_ASSERT((entry->eax & 0xffff) == 0x101,
966081f9c7SVitaly Kuznetsov 				    "Supported Enlightened VMCS version range is supposed to be 1:1"
976081f9c7SVitaly Kuznetsov 				    " 0x40000000.EAX: %x", entry->eax);
98e738772eSVitaly Kuznetsov 
996081f9c7SVitaly Kuznetsov 			break;
1006081f9c7SVitaly Kuznetsov 		default:
1016081f9c7SVitaly Kuznetsov 			break;
1026081f9c7SVitaly Kuznetsov 
1036081f9c7SVitaly Kuznetsov 		}
1047edcb734SVitaly Kuznetsov 		/*
1057edcb734SVitaly Kuznetsov 		 * If needed for debug:
1067edcb734SVitaly Kuznetsov 		 * fprintf(stdout,
1077edcb734SVitaly Kuznetsov 		 *	"CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
1087edcb734SVitaly Kuznetsov 		 *	entry->function, entry->eax, entry->ebx, entry->ecx,
1097edcb734SVitaly Kuznetsov 		 *	entry->edx);
1107edcb734SVitaly Kuznetsov 		 */
1117edcb734SVitaly Kuznetsov 	}
1127edcb734SVitaly Kuznetsov }
1137edcb734SVitaly Kuznetsov 
test_hv_cpuid_e2big(struct kvm_vm * vm,struct kvm_vcpu * vcpu)1145c6e31b3SSean Christopherson void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
1157edcb734SVitaly Kuznetsov {
1167edcb734SVitaly Kuznetsov 	static struct kvm_cpuid2 cpuid = {.nent = 0};
1177edcb734SVitaly Kuznetsov 	int ret;
1187edcb734SVitaly Kuznetsov 
1195c6e31b3SSean Christopherson 	if (vcpu)
120768e9a61SSean Christopherson 		ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
1218b460692SVitaly Kuznetsov 	else
12268c1b3e9SSean Christopherson 		ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
1237edcb734SVitaly Kuznetsov 
1247edcb734SVitaly Kuznetsov 	TEST_ASSERT(ret == -1 && errno == E2BIG,
1258b460692SVitaly Kuznetsov 		    "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
1265c6e31b3SSean Christopherson 		    " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
1277edcb734SVitaly Kuznetsov }
1287edcb734SVitaly Kuznetsov 
main(int argc,char * argv[])1297edcb734SVitaly Kuznetsov int main(int argc, char *argv[])
1307edcb734SVitaly Kuznetsov {
1317edcb734SVitaly Kuznetsov 	struct kvm_vm *vm;
132*813e38cdSSean Christopherson 	const struct kvm_cpuid2 *hv_cpuid_entries;
1335c6e31b3SSean Christopherson 	struct kvm_vcpu *vcpu;
1347edcb734SVitaly Kuznetsov 
1357ed397d1SSean Christopherson 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
1367edcb734SVitaly Kuznetsov 
1375c6e31b3SSean Christopherson 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
1388b460692SVitaly Kuznetsov 
1398b460692SVitaly Kuznetsov 	/* Test vCPU ioctl version */
1405c6e31b3SSean Christopherson 	test_hv_cpuid_e2big(vm, vcpu);
1418b460692SVitaly Kuznetsov 
142768e9a61SSean Christopherson 	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
1438b460692SVitaly Kuznetsov 	test_hv_cpuid(hv_cpuid_entries, false);
144*813e38cdSSean Christopherson 	free((void *)hv_cpuid_entries);
1458b460692SVitaly Kuznetsov 
1461ecbb337SSean Christopherson 	if (!kvm_cpu_has(X86_FEATURE_VMX) ||
1473ea9b809SSean Christopherson 	    !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
148d0aac332SAndrew Jones 		print_skip("Enlightened VMCS is unsupported");
1498b460692SVitaly Kuznetsov 		goto do_sys;
150fb0cb6a8SVitaly Kuznetsov 	}
151768e9a61SSean Christopherson 	vcpu_enable_evmcs(vcpu);
152768e9a61SSean Christopherson 	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
1538b460692SVitaly Kuznetsov 	test_hv_cpuid(hv_cpuid_entries, true);
154*813e38cdSSean Christopherson 	free((void *)hv_cpuid_entries);
1558b460692SVitaly Kuznetsov 
1568b460692SVitaly Kuznetsov do_sys:
1578b460692SVitaly Kuznetsov 	/* Test system ioctl version */
1583ea9b809SSean Christopherson 	if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
1598b460692SVitaly Kuznetsov 		print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
1608b460692SVitaly Kuznetsov 		goto out;
161eba3afdeSVitaly Kuznetsov 	}
1627edcb734SVitaly Kuznetsov 
1635c6e31b3SSean Christopherson 	test_hv_cpuid_e2big(vm, NULL);
1648b460692SVitaly Kuznetsov 
16532f00fd9SVitaly Kuznetsov 	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
1661ecbb337SSean Christopherson 	test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
1678b460692SVitaly Kuznetsov 
1688b460692SVitaly Kuznetsov out:
1697edcb734SVitaly Kuznetsov 	kvm_vm_free(vm);
1707edcb734SVitaly Kuznetsov 
1717edcb734SVitaly Kuznetsov 	return 0;
1727edcb734SVitaly Kuznetsov }
173