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_expected)
50 {
51 	int i;
52 	int nent_expected = 10;
53 	u32 test_val;
54 
55 	TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
56 		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
57 		    " (returned %d)",
58 		    nent_expected, hv_cpuid_entries->nent);
59 
60 	for (i = 0; i < hv_cpuid_entries->nent; i++) {
61 		struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
62 
63 		TEST_ASSERT((entry->function >= 0x40000000) &&
64 			    (entry->function <= 0x40000082),
65 			    "function %x is our of supported range",
66 			    entry->function);
67 
68 		TEST_ASSERT(entry->index == 0,
69 			    ".index field should be zero");
70 
71 		TEST_ASSERT(entry->flags == 0,
72 			    ".flags field should be zero");
73 
74 		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
75 			    !entry->padding[2], "padding should be zero");
76 
77 		switch (entry->function) {
78 		case 0x40000000:
79 			test_val = 0x40000082;
80 
81 			TEST_ASSERT(entry->eax == test_val,
82 				    "Wrong max leaf report in 0x40000000.EAX: %x"
83 				    " (evmcs=%d)",
84 				    entry->eax, evmcs_expected
85 				);
86 			break;
87 		case 0x40000004:
88 			test_val = entry->eax & (1UL << 18);
89 
90 			TEST_ASSERT(!!test_val == !smt_possible(),
91 				    "NoNonArchitecturalCoreSharing bit"
92 				    " doesn't reflect SMT setting");
93 			break;
94 		case 0x4000000A:
95 			TEST_ASSERT(entry->eax & (1UL << 19),
96 				    "Enlightened MSR-Bitmap should always be supported"
97 				    " 0x40000000.EAX: %x", entry->eax);
98 			if (evmcs_expected)
99 				TEST_ASSERT((entry->eax & 0xffff) == 0x101,
100 				    "Supported Enlightened VMCS version range is supposed to be 1:1"
101 				    " 0x40000000.EAX: %x", entry->eax);
102 
103 			break;
104 		default:
105 			break;
106 
107 		}
108 		/*
109 		 * If needed for debug:
110 		 * fprintf(stdout,
111 		 *	"CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
112 		 *	entry->function, entry->eax, entry->ebx, entry->ecx,
113 		 *	entry->edx);
114 		 */
115 	}
116 }
117 
118 void test_hv_cpuid_e2big(struct kvm_vm *vm, bool system)
119 {
120 	static struct kvm_cpuid2 cpuid = {.nent = 0};
121 	int ret;
122 
123 	if (!system)
124 		ret = _vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
125 	else
126 		ret = _kvm_ioctl(vm, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
127 
128 	TEST_ASSERT(ret == -1 && errno == E2BIG,
129 		    "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
130 		    " it should have: %d %d", system ? "KVM" : "vCPU", ret, errno);
131 }
132 
133 int main(int argc, char *argv[])
134 {
135 	struct kvm_vm *vm;
136 	struct kvm_cpuid2 *hv_cpuid_entries;
137 
138 	/* Tell stdout not to buffer its content */
139 	setbuf(stdout, NULL);
140 
141 	if (!kvm_check_cap(KVM_CAP_HYPERV_CPUID)) {
142 		print_skip("KVM_CAP_HYPERV_CPUID not supported");
143 		exit(KSFT_SKIP);
144 	}
145 
146 	vm = vm_create_default(VCPU_ID, 0, guest_code);
147 
148 	/* Test vCPU ioctl version */
149 	test_hv_cpuid_e2big(vm, false);
150 
151 	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vm, VCPU_ID);
152 	test_hv_cpuid(hv_cpuid_entries, false);
153 	free(hv_cpuid_entries);
154 
155 	if (!nested_vmx_supported() ||
156 	    !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
157 		print_skip("Enlightened VMCS is unsupported");
158 		goto do_sys;
159 	}
160 	vcpu_enable_evmcs(vm, VCPU_ID);
161 	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vm, VCPU_ID);
162 	test_hv_cpuid(hv_cpuid_entries, true);
163 	free(hv_cpuid_entries);
164 
165 do_sys:
166 	/* Test system ioctl version */
167 	if (!kvm_check_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
168 		print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
169 		goto out;
170 	}
171 
172 	test_hv_cpuid_e2big(vm, true);
173 
174 	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
175 	test_hv_cpuid(hv_cpuid_entries, nested_vmx_supported());
176 
177 out:
178 	kvm_vm_free(vm);
179 
180 	return 0;
181 }
182