1 // SPDX-License-Identifier: GPL-2.0
3 * Test for x86 KVM_CAP_HYPERV_CPUID
5 * Copyright (C) 2018, Red Hat, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2.
14 #include <sys/ioctl.h>
16 #include "test_util.h"
18 #include "processor.h"
21 static void guest_code(void)
25 static bool smt_possible(void)
31 f = fopen("/sys/devices/system/cpu/smt/control", "r");
33 if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
34 if (!strncmp(buf, "forceoff", 8) ||
35 !strncmp(buf, "notsupported", 12))
44 static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
48 int nent_expected = 10;
51 TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
52 "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
54 nent_expected, hv_cpuid_entries->nent);
56 for (i = 0; i < hv_cpuid_entries->nent; i++) {
57 const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
59 TEST_ASSERT((entry->function >= 0x40000000) &&
60 (entry->function <= 0x40000082),
61 "function %x is our of supported range",
64 TEST_ASSERT(entry->index == 0,
65 ".index field should be zero");
67 TEST_ASSERT(entry->flags == 0,
68 ".flags field should be zero");
70 TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
71 !entry->padding[2], "padding should be zero");
73 switch (entry->function) {
75 test_val = 0x40000082;
77 TEST_ASSERT(entry->eax == test_val,
78 "Wrong max leaf report in 0x40000000.EAX: %x"
80 entry->eax, evmcs_expected
84 test_val = entry->eax & (1UL << 18);
86 TEST_ASSERT(!!test_val == !smt_possible(),
87 "NoNonArchitecturalCoreSharing bit"
88 " doesn't reflect SMT setting");
91 TEST_ASSERT(entry->eax & (1UL << 19),
92 "Enlightened MSR-Bitmap should always be supported"
93 " 0x40000000.EAX: %x", entry->eax);
95 TEST_ASSERT((entry->eax & 0xffff) == 0x101,
96 "Supported Enlightened VMCS version range is supposed to be 1:1"
97 " 0x40000000.EAX: %x", entry->eax);
105 * If needed for debug:
107 * "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
108 * entry->function, entry->eax, entry->ebx, entry->ecx,
114 void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
116 static struct kvm_cpuid2 cpuid = {.nent = 0};
120 ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
122 ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
124 TEST_ASSERT(ret == -1 && errno == E2BIG,
125 "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
126 " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
129 int main(int argc, char *argv[])
132 const struct kvm_cpuid2 *hv_cpuid_entries;
133 struct kvm_vcpu *vcpu;
135 TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
137 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
139 /* Test vCPU ioctl version */
140 test_hv_cpuid_e2big(vm, vcpu);
142 hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
143 test_hv_cpuid(hv_cpuid_entries, false);
144 free((void *)hv_cpuid_entries);
146 if (!kvm_cpu_has(X86_FEATURE_VMX) ||
147 !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
148 print_skip("Enlightened VMCS is unsupported");
151 vcpu_enable_evmcs(vcpu);
152 hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
153 test_hv_cpuid(hv_cpuid_entries, true);
154 free((void *)hv_cpuid_entries);
157 /* Test system ioctl version */
158 if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
159 print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
163 test_hv_cpuid_e2big(vm, NULL);
165 hv_cpuid_entries = kvm_get_supported_hv_cpuid();
166 test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));