]> git.ipfire.org Git - people/arne_f/kernel.git/blob - tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c
KVM: selftests: Fix a condition in test_hv_cpuid()
[people/arne_f/kernel.git] / tools / testing / selftests / kvm / x86_64 / hyperv_cpuid.c
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
22 #define VCPU_ID 0
23
24 static void guest_code(void)
25 {
26 }
27
28 static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
29 int evmcs_enabled)
30 {
31 int i;
32
33 if (!evmcs_enabled)
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);
38 else
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);
43
44 for (i = 0; i < hv_cpuid_entries->nent; i++) {
45 struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
46
47 TEST_ASSERT((entry->function >= 0x40000000) &&
48 (entry->function <= 0x4000000A),
49 "function %lx is our of supported range",
50 entry->function);
51
52 TEST_ASSERT(entry->index == 0,
53 ".index field should be zero");
54
55 TEST_ASSERT(entry->index == 0,
56 ".index field should be zero");
57
58 TEST_ASSERT(entry->flags == 0,
59 ".flags field should be zero");
60
61 TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
62 !entry->padding[2], "padding should be zero");
63
64 /*
65 * If needed for debug:
66 * fprintf(stdout,
67 * "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
68 * entry->function, entry->eax, entry->ebx, entry->ecx,
69 * entry->edx);
70 */
71 }
72
73 }
74
75 void test_hv_cpuid_e2big(struct kvm_vm *vm)
76 {
77 static struct kvm_cpuid2 cpuid = {.nent = 0};
78 int ret;
79
80 ret = _vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
81
82 TEST_ASSERT(ret == -1 && errno == E2BIG,
83 "KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
84 " it should have: %d %d", ret, errno);
85 }
86
87
88 struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(struct kvm_vm *vm)
89 {
90 int nent = 20; /* should be enough */
91 static struct kvm_cpuid2 *cpuid;
92 int ret;
93
94 cpuid = malloc(sizeof(*cpuid) + nent * sizeof(struct kvm_cpuid_entry2));
95
96 if (!cpuid) {
97 perror("malloc");
98 abort();
99 }
100
101 cpuid->nent = nent;
102
103 vcpu_ioctl(vm, VCPU_ID, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
104
105 return cpuid;
106 }
107
108
109 int main(int argc, char *argv[])
110 {
111 struct kvm_vm *vm;
112 int rv;
113 uint16_t evmcs_ver;
114 struct kvm_cpuid2 *hv_cpuid_entries;
115 struct kvm_enable_cap enable_evmcs_cap = {
116 .cap = KVM_CAP_HYPERV_ENLIGHTENED_VMCS,
117 .args[0] = (unsigned long)&evmcs_ver
118 };
119
120 /* Tell stdout not to buffer its content */
121 setbuf(stdout, NULL);
122
123 rv = kvm_check_cap(KVM_CAP_HYPERV_CPUID);
124 if (!rv) {
125 fprintf(stderr,
126 "KVM_CAP_HYPERV_CPUID not supported, skip test\n");
127 exit(KSFT_SKIP);
128 }
129
130 /* Create VM */
131 vm = vm_create_default(VCPU_ID, 0, guest_code);
132
133 test_hv_cpuid_e2big(vm);
134
135 hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
136 if (!hv_cpuid_entries)
137 return 1;
138
139 test_hv_cpuid(hv_cpuid_entries, 0);
140
141 free(hv_cpuid_entries);
142
143 rv = _vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap);
144
145 if (rv) {
146 fprintf(stderr,
147 "Enlightened VMCS is unsupported, skip related test\n");
148 goto vm_free;
149 }
150
151 hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
152 if (!hv_cpuid_entries)
153 return 1;
154
155 test_hv_cpuid(hv_cpuid_entries, 1);
156
157 free(hv_cpuid_entries);
158
159 vm_free:
160 kvm_vm_free(vm);
161
162 return 0;
163 }