xref: /linux/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c (revision 58f6259b7a08f8d47d4629609703d358b042f0fd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Test for x86 KVM_CAP_HYPERV_CPUID
4  *
5  * Copyright (C) 2018, Red Hat, Inc.
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2.
8  *
9  */
10 
11 #define _GNU_SOURCE /* for program_invocation_short_name */
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/ioctl.h>
17 
18 #include "test_util.h"
19 #include "kvm_util.h"
20 #include "processor.h"
21 #include "vmx.h"
22 
23 static void guest_code(void)
24 {
25 }
26 
27 static bool smt_possible(void)
28 {
29 	char buf[16];
30 	FILE *f;
31 	bool res = true;
32 
33 	f = fopen("/sys/devices/system/cpu/smt/control", "r");
34 	if (f) {
35 		if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
36 			if (!strncmp(buf, "forceoff", 8) ||
37 			    !strncmp(buf, "notsupported", 12))
38 				res = false;
39 		}
40 		fclose(f);
41 	}
42 
43 	return res;
44 }
45 
46 static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
47 			  bool evmcs_expected)
48 {
49 	int i;
50 	int nent_expected = 10;
51 	u32 test_val;
52 
53 	TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
54 		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
55 		    " (returned %d)",
56 		    nent_expected, hv_cpuid_entries->nent);
57 
58 	for (i = 0; i < hv_cpuid_entries->nent; i++) {
59 		const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
60 
61 		TEST_ASSERT((entry->function >= 0x40000000) &&
62 			    (entry->function <= 0x40000082),
63 			    "function %x is our of supported range",
64 			    entry->function);
65 
66 		TEST_ASSERT(entry->index == 0,
67 			    ".index field should be zero");
68 
69 		TEST_ASSERT(entry->flags == 0,
70 			    ".flags field should be zero");
71 
72 		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
73 			    !entry->padding[2], "padding should be zero");
74 
75 		switch (entry->function) {
76 		case 0x40000000:
77 			test_val = 0x40000082;
78 
79 			TEST_ASSERT(entry->eax == test_val,
80 				    "Wrong max leaf report in 0x40000000.EAX: %x"
81 				    " (evmcs=%d)",
82 				    entry->eax, evmcs_expected
83 				);
84 			break;
85 		case 0x40000004:
86 			test_val = entry->eax & (1UL << 18);
87 
88 			TEST_ASSERT(!!test_val == !smt_possible(),
89 				    "NoNonArchitecturalCoreSharing bit"
90 				    " doesn't reflect SMT setting");
91 			break;
92 		case 0x4000000A:
93 			TEST_ASSERT(entry->eax & (1UL << 19),
94 				    "Enlightened MSR-Bitmap should always be supported"
95 				    " 0x40000000.EAX: %x", entry->eax);
96 			if (evmcs_expected)
97 				TEST_ASSERT((entry->eax & 0xffff) == 0x101,
98 				    "Supported Enlightened VMCS version range is supposed to be 1:1"
99 				    " 0x40000000.EAX: %x", entry->eax);
100 
101 			break;
102 		default:
103 			break;
104 
105 		}
106 		/*
107 		 * If needed for debug:
108 		 * fprintf(stdout,
109 		 *	"CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
110 		 *	entry->function, entry->eax, entry->ebx, entry->ecx,
111 		 *	entry->edx);
112 		 */
113 	}
114 }
115 
116 void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
117 {
118 	static struct kvm_cpuid2 cpuid = {.nent = 0};
119 	int ret;
120 
121 	if (vcpu)
122 		ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
123 	else
124 		ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
125 
126 	TEST_ASSERT(ret == -1 && errno == E2BIG,
127 		    "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
128 		    " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
129 }
130 
131 int main(int argc, char *argv[])
132 {
133 	struct kvm_vm *vm;
134 	const struct kvm_cpuid2 *hv_cpuid_entries;
135 	struct kvm_vcpu *vcpu;
136 
137 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
138 
139 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
140 
141 	/* Test vCPU ioctl version */
142 	test_hv_cpuid_e2big(vm, vcpu);
143 
144 	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
145 	test_hv_cpuid(hv_cpuid_entries, false);
146 	free((void *)hv_cpuid_entries);
147 
148 	if (!kvm_cpu_has(X86_FEATURE_VMX) ||
149 	    !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
150 		print_skip("Enlightened VMCS is unsupported");
151 		goto do_sys;
152 	}
153 	vcpu_enable_evmcs(vcpu);
154 	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
155 	test_hv_cpuid(hv_cpuid_entries, true);
156 	free((void *)hv_cpuid_entries);
157 
158 do_sys:
159 	/* Test system ioctl version */
160 	if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
161 		print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
162 		goto out;
163 	}
164 
165 	test_hv_cpuid_e2big(vm, NULL);
166 
167 	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
168 	test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
169 
170 out:
171 	kvm_vm_free(vm);
172 
173 	return 0;
174 }
175