xref: /linux/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
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 #define VCPU_ID 0
24 
25 static void guest_code(void)
26 {
27 }
28 
29 static bool smt_possible(void)
30 {
31 	char buf[16];
32 	FILE *f;
33 	bool res = true;
34 
35 	f = fopen("/sys/devices/system/cpu/smt/control", "r");
36 	if (f) {
37 		if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
38 			if (!strncmp(buf, "forceoff", 8) ||
39 			    !strncmp(buf, "notsupported", 12))
40 				res = false;
41 		}
42 		fclose(f);
43 	}
44 
45 	return res;
46 }
47 
48 static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
49 			  bool evmcs_enabled)
50 {
51 	int i;
52 	int nent = 9;
53 	u32 test_val;
54 
55 	if (evmcs_enabled)
56 		nent += 1; /* 0x4000000A */
57 
58 	TEST_ASSERT(hv_cpuid_entries->nent == nent,
59 		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
60 		    " with evmcs=%d (returned %d)",
61 		    nent, evmcs_enabled, hv_cpuid_entries->nent);
62 
63 	for (i = 0; i < hv_cpuid_entries->nent; i++) {
64 		struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
65 
66 		TEST_ASSERT((entry->function >= 0x40000000) &&
67 			    (entry->function <= 0x40000082),
68 			    "function %x is our of supported range",
69 			    entry->function);
70 
71 		TEST_ASSERT(evmcs_enabled || (entry->function != 0x4000000A),
72 			    "0x4000000A leaf should not be reported");
73 
74 		TEST_ASSERT(entry->index == 0,
75 			    ".index field should be zero");
76 
77 		TEST_ASSERT(entry->flags == 0,
78 			    ".flags field should be zero");
79 
80 		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
81 			    !entry->padding[2], "padding should be zero");
82 
83 		switch (entry->function) {
84 		case 0x40000000:
85 			test_val = 0x40000082;
86 
87 			TEST_ASSERT(entry->eax == test_val,
88 				    "Wrong max leaf report in 0x40000000.EAX: %x"
89 				    " (evmcs=%d)",
90 				    entry->eax, evmcs_enabled
91 				);
92 			break;
93 		case 0x40000004:
94 			test_val = entry->eax & (1UL << 18);
95 
96 			TEST_ASSERT(!!test_val == !smt_possible(),
97 				    "NoNonArchitecturalCoreSharing bit"
98 				    " doesn't reflect SMT setting");
99 			break;
100 		}
101 
102 		/*
103 		 * If needed for debug:
104 		 * fprintf(stdout,
105 		 *	"CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
106 		 *	entry->function, entry->eax, entry->ebx, entry->ecx,
107 		 *	entry->edx);
108 		 */
109 	}
110 
111 }
112 
113 void test_hv_cpuid_e2big(struct kvm_vm *vm)
114 {
115 	static struct kvm_cpuid2 cpuid = {.nent = 0};
116 	int ret;
117 
118 	ret = _vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
119 
120 	TEST_ASSERT(ret == -1 && errno == E2BIG,
121 		    "KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
122 		    " it should have: %d %d", ret, errno);
123 }
124 
125 
126 struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)
127 {
128 	int nent = 20; /* should be enough */
129 	static struct kvm_cpuid2 *cpuid;
130 
131 	cpuid = malloc(sizeof(*cpuid) + nent * sizeof(struct kvm_cpuid_entry2));
132 
133 	if (!cpuid) {
134 		perror("malloc");
135 		abort();
136 	}
137 
138 	cpuid->nent = nent;
139 
140 	vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
141 
142 	return cpuid;
143 }
144 
145 
146 int main(int argc, char *argv[])
147 {
148 	struct kvm_vm *vm;
149 	int rv, stage;
150 	struct kvm_cpuid2 *hv_cpuid_entries;
151 	bool evmcs_enabled;
152 
153 	/* Tell stdout not to buffer its content */
154 	setbuf(stdout, NULL);
155 
156 	rv = kvm_check_cap(KVM_CAP_HYPERV_CPUID);
157 	if (!rv) {
158 		print_skip("KVM_CAP_HYPERV_CPUID not supported");
159 		exit(KSFT_SKIP);
160 	}
161 
162 	for (stage = 0; stage < 3; stage++) {
163 		evmcs_enabled = false;
164 
165 		vm = vm_create_default(VCPU_ID, 0, guest_code);
166 		switch (stage) {
167 		case 0:
168 			test_hv_cpuid_e2big(vm);
169 			continue;
170 		case 1:
171 			break;
172 		case 2:
173 			if (!nested_vmx_supported() ||
174 			    !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
175 				print_skip("Enlightened VMCS is unsupported");
176 				continue;
177 			}
178 			vcpu_enable_evmcs(vm, VCPU_ID);
179 			evmcs_enabled = true;
180 			break;
181 		}
182 
183 		hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
184 		test_hv_cpuid(hv_cpuid_entries, evmcs_enabled);
185 		free(hv_cpuid_entries);
186 		kvm_vm_free(vm);
187 	}
188 
189 	return 0;
190 }
191