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 void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
35 TEST_ASSERT(hv_cpuid_entries->nent == 6,
36 "KVM_GET_SUPPORTED_HV_CPUID should return 6 entries"
37 " when Enlightened VMCS is disabled (returned %d)",
38 hv_cpuid_entries->nent);
40 TEST_ASSERT(hv_cpuid_entries->nent == 7,
41 "KVM_GET_SUPPORTED_HV_CPUID should return 7 entries"
42 " when Enlightened VMCS is enabled (returned %d)",
43 hv_cpuid_entries->nent);
45 for (i = 0; i < hv_cpuid_entries->nent; i++) {
46 struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
48 TEST_ASSERT((entry->function >= 0x40000000) &&
49 (entry->function <= 0x4000000A),
50 "function %lx is our of supported range",
53 TEST_ASSERT(entry->index == 0,
54 ".index field should be zero");
56 TEST_ASSERT(entry->flags == 0,
57 ".flags field should be zero");
59 TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
60 !entry->padding[2], "padding should be zero");
63 * If needed for debug:
65 * "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
66 * entry->function, entry->eax, entry->ebx, entry->ecx,
73 void test_hv_cpuid_e2big(struct kvm_vm *vm)
75 static struct kvm_cpuid2 cpuid = {.nent = 0};
78 ret = _vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
80 TEST_ASSERT(ret == -1 && errno == E2BIG,
81 "KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
82 " it should have: %d %d", ret, errno);
86 struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)
88 int nent = 20; /* should be enough */
89 static struct kvm_cpuid2 *cpuid;
91 cpuid = malloc(sizeof(*cpuid) + nent * sizeof(struct kvm_cpuid_entry2));
100 vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
106 int main(int argc, char *argv[])
110 struct kvm_cpuid2 *hv_cpuid_entries;
112 /* Tell stdout not to buffer its content */
113 setbuf(stdout, NULL);
115 rv = kvm_check_cap(KVM_CAP_HYPERV_CPUID);
118 "KVM_CAP_HYPERV_CPUID not supported, skip test\n");
123 vm = vm_create_default(VCPU_ID, 0, guest_code);
125 test_hv_cpuid_e2big(vm);
127 hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
128 if (!hv_cpuid_entries)
131 test_hv_cpuid(hv_cpuid_entries, 0);
133 free(hv_cpuid_entries);
135 if (!kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
137 "Enlightened VMCS is unsupported, skip related test\n");
141 vcpu_enable_evmcs(vm, VCPU_ID);
143 hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
144 if (!hv_cpuid_entries)
147 test_hv_cpuid(hv_cpuid_entries, 1);
149 free(hv_cpuid_entries);