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"
24 static void guest_code(void)
28 static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
34 TEST_ASSERT(hv_cpuid_entries->nent == 6,
35 "KVM_GET_SUPPORTED_HV_CPUID should return 6 entries"
36 " when Enlightened VMCS is disabled (returned %d)",
37 hv_cpuid_entries->nent);
39 TEST_ASSERT(hv_cpuid_entries->nent == 7,
40 "KVM_GET_SUPPORTED_HV_CPUID should return 7 entries"
41 " when Enlightened VMCS is enabled (returned %d)",
42 hv_cpuid_entries->nent);
44 for (i = 0; i < hv_cpuid_entries->nent; i++) {
45 struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
47 TEST_ASSERT((entry->function >= 0x40000000) &&
48 (entry->function <= 0x4000000A),
49 "function %lx is our of supported range",
52 TEST_ASSERT(entry->index == 0,
53 ".index field should be zero");
55 TEST_ASSERT(entry->index == 0,
56 ".index field should be zero");
58 TEST_ASSERT(entry->flags == 0,
59 ".flags field should be zero");
61 TEST_ASSERT(entry->padding[0] == entry->padding[1]
62 == entry->padding[2] == 0,
63 ".index field should be zero");
66 * If needed for debug:
68 * "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
69 * entry->function, entry->eax, entry->ebx, entry->ecx,
76 void test_hv_cpuid_e2big(struct kvm_vm *vm)
78 static struct kvm_cpuid2 cpuid = {.nent = 0};
81 ret = _vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
83 TEST_ASSERT(ret == -1 && errno == E2BIG,
84 "KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
85 " it should have: %d %d", ret, errno);
89 struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)
91 int nent = 20; /* should be enough */
92 static struct kvm_cpuid2 *cpuid;
95 cpuid = malloc(sizeof(*cpuid) + nent * sizeof(struct kvm_cpuid_entry2));
104 vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
110 int main(int argc, char *argv[])
115 struct kvm_cpuid2 *hv_cpuid_entries;
116 struct kvm_enable_cap enable_evmcs_cap = {
117 .cap = KVM_CAP_HYPERV_ENLIGHTENED_VMCS,
118 .args[0] = (unsigned long)&evmcs_ver
121 /* Tell stdout not to buffer its content */
122 setbuf(stdout, NULL);
124 rv = kvm_check_cap(KVM_CAP_HYPERV_CPUID);
127 "KVM_CAP_HYPERV_CPUID not supported, skip test\n");
132 vm = vm_create_default(VCPU_ID, 0, guest_code);
134 test_hv_cpuid_e2big(vm);
136 hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
137 if (!hv_cpuid_entries)
140 test_hv_cpuid(hv_cpuid_entries, 0);
142 free(hv_cpuid_entries);
144 vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap);
146 hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
147 if (!hv_cpuid_entries)
150 test_hv_cpuid(hv_cpuid_entries, 1);
152 free(hv_cpuid_entries);