xref: /openbmc/linux/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
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 
117edcb734SVitaly Kuznetsov #define _GNU_SOURCE /* for program_invocation_short_name */
127edcb734SVitaly Kuznetsov #include <fcntl.h>
137edcb734SVitaly Kuznetsov #include <stdio.h>
147edcb734SVitaly Kuznetsov #include <stdlib.h>
157edcb734SVitaly Kuznetsov #include <string.h>
167edcb734SVitaly Kuznetsov #include <sys/ioctl.h>
177edcb734SVitaly Kuznetsov 
187edcb734SVitaly Kuznetsov #include "test_util.h"
197edcb734SVitaly Kuznetsov #include "kvm_util.h"
207edcb734SVitaly Kuznetsov #include "processor.h"
2165efa61dSPaolo Bonzini #include "vmx.h"
227edcb734SVitaly Kuznetsov 
guest_code(void)237edcb734SVitaly Kuznetsov static void guest_code(void)
247edcb734SVitaly Kuznetsov {
257edcb734SVitaly Kuznetsov }
267edcb734SVitaly Kuznetsov 
smt_possible(void)27fb0cb6a8SVitaly Kuznetsov static bool smt_possible(void)
28e738772eSVitaly Kuznetsov {
29e738772eSVitaly Kuznetsov 	char buf[16];
30e738772eSVitaly Kuznetsov 	FILE *f;
31fb0cb6a8SVitaly Kuznetsov 	bool res = true;
32e738772eSVitaly Kuznetsov 
33e738772eSVitaly Kuznetsov 	f = fopen("/sys/devices/system/cpu/smt/control", "r");
34e738772eSVitaly Kuznetsov 	if (f) {
35e738772eSVitaly Kuznetsov 		if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
36e738772eSVitaly Kuznetsov 			if (!strncmp(buf, "forceoff", 8) ||
37e738772eSVitaly Kuznetsov 			    !strncmp(buf, "notsupported", 12))
38fb0cb6a8SVitaly Kuznetsov 				res = false;
39e738772eSVitaly Kuznetsov 		}
40e738772eSVitaly Kuznetsov 		fclose(f);
41e738772eSVitaly Kuznetsov 	}
42e738772eSVitaly Kuznetsov 
43e738772eSVitaly Kuznetsov 	return res;
44e738772eSVitaly Kuznetsov }
45e738772eSVitaly Kuznetsov 
test_hv_cpuid(const struct kvm_cpuid2 * hv_cpuid_entries,bool evmcs_expected)46*813e38cdSSean Christopherson static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
478b460692SVitaly Kuznetsov 			  bool evmcs_expected)
487edcb734SVitaly Kuznetsov {
497edcb734SVitaly Kuznetsov 	int i;
506081f9c7SVitaly Kuznetsov 	int nent_expected = 10;
51fb0cb6a8SVitaly Kuznetsov 	u32 test_val;
527edcb734SVitaly Kuznetsov 
536081f9c7SVitaly Kuznetsov 	TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
54fb0cb6a8SVitaly Kuznetsov 		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
556081f9c7SVitaly Kuznetsov 		    " (returned %d)",
566081f9c7SVitaly Kuznetsov 		    nent_expected, hv_cpuid_entries->nent);
577edcb734SVitaly Kuznetsov 
587edcb734SVitaly Kuznetsov 	for (i = 0; i < hv_cpuid_entries->nent; i++) {
59*813e38cdSSean Christopherson 		const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
607edcb734SVitaly Kuznetsov 
617edcb734SVitaly Kuznetsov 		TEST_ASSERT((entry->function >= 0x40000000) &&
62fb0cb6a8SVitaly Kuznetsov 			    (entry->function <= 0x40000082),
63d9eaf19eSAndrew Jones 			    "function %x is our of supported range",
647edcb734SVitaly Kuznetsov 			    entry->function);
657edcb734SVitaly Kuznetsov 
667edcb734SVitaly Kuznetsov 		TEST_ASSERT(entry->index == 0,
677edcb734SVitaly Kuznetsov 			    ".index field should be zero");
687edcb734SVitaly Kuznetsov 
697edcb734SVitaly Kuznetsov 		TEST_ASSERT(entry->flags == 0,
707edcb734SVitaly Kuznetsov 			    ".flags field should be zero");
717edcb734SVitaly Kuznetsov 
72be7fcf1dSDan Carpenter 		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
73be7fcf1dSDan Carpenter 			    !entry->padding[2], "padding should be zero");
747edcb734SVitaly Kuznetsov 
75fb0cb6a8SVitaly Kuznetsov 		switch (entry->function) {
76fb0cb6a8SVitaly Kuznetsov 		case 0x40000000:
77fb0cb6a8SVitaly Kuznetsov 			test_val = 0x40000082;
78e738772eSVitaly Kuznetsov 
79fb0cb6a8SVitaly Kuznetsov 			TEST_ASSERT(entry->eax == test_val,
80fb0cb6a8SVitaly Kuznetsov 				    "Wrong max leaf report in 0x40000000.EAX: %x"
81fb0cb6a8SVitaly Kuznetsov 				    " (evmcs=%d)",
828b460692SVitaly Kuznetsov 				    entry->eax, evmcs_expected
83fb0cb6a8SVitaly Kuznetsov 				);
84fb0cb6a8SVitaly Kuznetsov 			break;
85fb0cb6a8SVitaly Kuznetsov 		case 0x40000004:
86fb0cb6a8SVitaly Kuznetsov 			test_val = entry->eax & (1UL << 18);
87fb0cb6a8SVitaly Kuznetsov 
88fb0cb6a8SVitaly Kuznetsov 			TEST_ASSERT(!!test_val == !smt_possible(),
89e738772eSVitaly Kuznetsov 				    "NoNonArchitecturalCoreSharing bit"
90e738772eSVitaly Kuznetsov 				    " doesn't reflect SMT setting");
91fb0cb6a8SVitaly Kuznetsov 			break;
926081f9c7SVitaly Kuznetsov 		case 0x4000000A:
936081f9c7SVitaly Kuznetsov 			TEST_ASSERT(entry->eax & (1UL << 19),
946081f9c7SVitaly Kuznetsov 				    "Enlightened MSR-Bitmap should always be supported"
956081f9c7SVitaly Kuznetsov 				    " 0x40000000.EAX: %x", entry->eax);
966081f9c7SVitaly Kuznetsov 			if (evmcs_expected)
976081f9c7SVitaly Kuznetsov 				TEST_ASSERT((entry->eax & 0xffff) == 0x101,
986081f9c7SVitaly Kuznetsov 				    "Supported Enlightened VMCS version range is supposed to be 1:1"
996081f9c7SVitaly Kuznetsov 				    " 0x40000000.EAX: %x", entry->eax);
100e738772eSVitaly Kuznetsov 
1016081f9c7SVitaly Kuznetsov 			break;
1026081f9c7SVitaly Kuznetsov 		default:
1036081f9c7SVitaly Kuznetsov 			break;
1046081f9c7SVitaly Kuznetsov 
1056081f9c7SVitaly Kuznetsov 		}
1067edcb734SVitaly Kuznetsov 		/*
1077edcb734SVitaly Kuznetsov 		 * If needed for debug:
1087edcb734SVitaly Kuznetsov 		 * fprintf(stdout,
1097edcb734SVitaly Kuznetsov 		 *	"CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
1107edcb734SVitaly Kuznetsov 		 *	entry->function, entry->eax, entry->ebx, entry->ecx,
1117edcb734SVitaly Kuznetsov 		 *	entry->edx);
1127edcb734SVitaly Kuznetsov 		 */
1137edcb734SVitaly Kuznetsov 	}
1147edcb734SVitaly Kuznetsov }
1157edcb734SVitaly Kuznetsov 
test_hv_cpuid_e2big(struct kvm_vm * vm,struct kvm_vcpu * vcpu)1165c6e31b3SSean Christopherson void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
1177edcb734SVitaly Kuznetsov {
1187edcb734SVitaly Kuznetsov 	static struct kvm_cpuid2 cpuid = {.nent = 0};
1197edcb734SVitaly Kuznetsov 	int ret;
1207edcb734SVitaly Kuznetsov 
1215c6e31b3SSean Christopherson 	if (vcpu)
122768e9a61SSean Christopherson 		ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
1238b460692SVitaly Kuznetsov 	else
12468c1b3e9SSean Christopherson 		ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
1257edcb734SVitaly Kuznetsov 
1267edcb734SVitaly Kuznetsov 	TEST_ASSERT(ret == -1 && errno == E2BIG,
1278b460692SVitaly Kuznetsov 		    "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
1285c6e31b3SSean Christopherson 		    " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
1297edcb734SVitaly Kuznetsov }
1307edcb734SVitaly Kuznetsov 
main(int argc,char * argv[])1317edcb734SVitaly Kuznetsov int main(int argc, char *argv[])
1327edcb734SVitaly Kuznetsov {
1337edcb734SVitaly Kuznetsov 	struct kvm_vm *vm;
134*813e38cdSSean Christopherson 	const struct kvm_cpuid2 *hv_cpuid_entries;
1355c6e31b3SSean Christopherson 	struct kvm_vcpu *vcpu;
1367edcb734SVitaly Kuznetsov 
1377ed397d1SSean Christopherson 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
1387edcb734SVitaly Kuznetsov 
1395c6e31b3SSean Christopherson 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
1408b460692SVitaly Kuznetsov 
1418b460692SVitaly Kuznetsov 	/* Test vCPU ioctl version */
1425c6e31b3SSean Christopherson 	test_hv_cpuid_e2big(vm, vcpu);
1438b460692SVitaly Kuznetsov 
144768e9a61SSean Christopherson 	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
1458b460692SVitaly Kuznetsov 	test_hv_cpuid(hv_cpuid_entries, false);
146*813e38cdSSean Christopherson 	free((void *)hv_cpuid_entries);
1478b460692SVitaly Kuznetsov 
1481ecbb337SSean Christopherson 	if (!kvm_cpu_has(X86_FEATURE_VMX) ||
1493ea9b809SSean Christopherson 	    !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
150d0aac332SAndrew Jones 		print_skip("Enlightened VMCS is unsupported");
1518b460692SVitaly Kuznetsov 		goto do_sys;
152fb0cb6a8SVitaly Kuznetsov 	}
153768e9a61SSean Christopherson 	vcpu_enable_evmcs(vcpu);
154768e9a61SSean Christopherson 	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
1558b460692SVitaly Kuznetsov 	test_hv_cpuid(hv_cpuid_entries, true);
156*813e38cdSSean Christopherson 	free((void *)hv_cpuid_entries);
1578b460692SVitaly Kuznetsov 
1588b460692SVitaly Kuznetsov do_sys:
1598b460692SVitaly Kuznetsov 	/* Test system ioctl version */
1603ea9b809SSean Christopherson 	if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
1618b460692SVitaly Kuznetsov 		print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
1628b460692SVitaly Kuznetsov 		goto out;
163eba3afdeSVitaly Kuznetsov 	}
1647edcb734SVitaly Kuznetsov 
1655c6e31b3SSean Christopherson 	test_hv_cpuid_e2big(vm, NULL);
1668b460692SVitaly Kuznetsov 
16732f00fd9SVitaly Kuznetsov 	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
1681ecbb337SSean Christopherson 	test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
1698b460692SVitaly Kuznetsov 
1708b460692SVitaly Kuznetsov out:
1717edcb734SVitaly Kuznetsov 	kvm_vm_free(vm);
1727edcb734SVitaly Kuznetsov 
1737edcb734SVitaly Kuznetsov 	return 0;
1747edcb734SVitaly Kuznetsov }
175