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.
11 #define _GNU_SOURCE /* for program_invocation_short_name */
16 #include <sys/ioctl.h>
18 #include "test_util.h"
20 #include "processor.h"
25 static void guest_code(void)
29 static bool smt_possible(void)
35 f
= fopen("/sys/devices/system/cpu/smt/control", "r");
37 if (fread(buf
, sizeof(*buf
), sizeof(buf
), f
) > 0) {
38 if (!strncmp(buf
, "forceoff", 8) ||
39 !strncmp(buf
, "notsupported", 12))
48 static void test_hv_cpuid(struct kvm_cpuid2
*hv_cpuid_entries
,
56 nent
+= 1; /* 0x4000000A */
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_expected
, hv_cpuid_entries
->nent
);
63 for (i
= 0; i
< hv_cpuid_entries
->nent
; i
++) {
64 struct kvm_cpuid_entry2
*entry
= &hv_cpuid_entries
->entries
[i
];
66 TEST_ASSERT((entry
->function
>= 0x40000000) &&
67 (entry
->function
<= 0x40000082),
68 "function %x is our of supported range",
71 TEST_ASSERT(evmcs_expected
|| (entry
->function
!= 0x4000000A),
72 "0x4000000A leaf should not be reported");
74 TEST_ASSERT(entry
->index
== 0,
75 ".index field should be zero");
77 TEST_ASSERT(entry
->flags
== 0,
78 ".flags field should be zero");
80 TEST_ASSERT(!entry
->padding
[0] && !entry
->padding
[1] &&
81 !entry
->padding
[2], "padding should be zero");
83 switch (entry
->function
) {
85 test_val
= 0x40000082;
87 TEST_ASSERT(entry
->eax
== test_val
,
88 "Wrong max leaf report in 0x40000000.EAX: %x"
90 entry
->eax
, evmcs_expected
94 test_val
= entry
->eax
& (1UL << 18);
96 TEST_ASSERT(!!test_val
== !smt_possible(),
97 "NoNonArchitecturalCoreSharing bit"
98 " doesn't reflect SMT setting");
103 * If needed for debug:
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,
113 void test_hv_cpuid_e2big(struct kvm_vm
*vm
, bool system
)
115 static struct kvm_cpuid2 cpuid
= {.nent
= 0};
119 ret
= _vcpu_ioctl(vm
, VCPU_ID
, KVM_GET_SUPPORTED_HV_CPUID
, &cpuid
);
121 ret
= _kvm_ioctl(vm
, KVM_GET_SUPPORTED_HV_CPUID
, &cpuid
);
123 TEST_ASSERT(ret
== -1 && errno
== E2BIG
,
124 "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
125 " it should have: %d %d", system
? "KVM" : "vCPU", ret
, errno
);
129 struct kvm_cpuid2
*kvm_get_supported_hv_cpuid(struct kvm_vm
*vm
, bool system
)
131 int nent
= 20; /* should be enough */
132 static struct kvm_cpuid2
*cpuid
;
134 cpuid
= malloc(sizeof(*cpuid
) + nent
* sizeof(struct kvm_cpuid_entry2
));
144 vcpu_ioctl(vm
, VCPU_ID
, KVM_GET_SUPPORTED_HV_CPUID
, cpuid
);
146 kvm_ioctl(vm
, KVM_GET_SUPPORTED_HV_CPUID
, cpuid
);
152 int main(int argc
, char *argv
[])
155 struct kvm_cpuid2
*hv_cpuid_entries
;
157 /* Tell stdout not to buffer its content */
158 setbuf(stdout
, NULL
);
160 if (!kvm_check_cap(KVM_CAP_HYPERV_CPUID
)) {
161 print_skip("KVM_CAP_HYPERV_CPUID not supported");
165 vm
= vm_create_default(VCPU_ID
, 0, guest_code
);
167 /* Test vCPU ioctl version */
168 test_hv_cpuid_e2big(vm
, false);
170 hv_cpuid_entries
= kvm_get_supported_hv_cpuid(vm
, false);
171 test_hv_cpuid(hv_cpuid_entries
, false);
172 free(hv_cpuid_entries
);
174 if (!nested_vmx_supported() ||
175 !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS
)) {
176 print_skip("Enlightened VMCS is unsupported");
179 vcpu_enable_evmcs(vm
, VCPU_ID
);
180 hv_cpuid_entries
= kvm_get_supported_hv_cpuid(vm
, false);
181 test_hv_cpuid(hv_cpuid_entries
, true);
182 free(hv_cpuid_entries
);
185 /* Test system ioctl version */
186 if (!kvm_check_cap(KVM_CAP_SYS_HYPERV_CPUID
)) {
187 print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
191 test_hv_cpuid_e2big(vm
, true);
193 hv_cpuid_entries
= kvm_get_supported_hv_cpuid(vm
, true);
194 test_hv_cpuid(hv_cpuid_entries
, nested_vmx_supported());
195 free(hv_cpuid_entries
);