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 int 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
,
54 TEST_ASSERT(hv_cpuid_entries
->nent
== 6,
55 "KVM_GET_SUPPORTED_HV_CPUID should return 6 entries"
56 " when Enlightened VMCS is disabled (returned %d)",
57 hv_cpuid_entries
->nent
);
59 TEST_ASSERT(hv_cpuid_entries
->nent
== 7,
60 "KVM_GET_SUPPORTED_HV_CPUID should return 7 entries"
61 " when Enlightened VMCS is enabled (returned %d)",
62 hv_cpuid_entries
->nent
);
64 for (i
= 0; i
< hv_cpuid_entries
->nent
; i
++) {
65 struct kvm_cpuid_entry2
*entry
= &hv_cpuid_entries
->entries
[i
];
67 TEST_ASSERT((entry
->function
>= 0x40000000) &&
68 (entry
->function
<= 0x4000000A),
69 "function %lx is our of supported range",
72 TEST_ASSERT(entry
->index
== 0,
73 ".index field should be zero");
75 TEST_ASSERT(entry
->flags
== 0,
76 ".flags field should be zero");
78 TEST_ASSERT(!entry
->padding
[0] && !entry
->padding
[1] &&
79 !entry
->padding
[2], "padding should be zero");
81 if (entry
->function
== 0x40000004) {
82 int nononarchcs
= !!(entry
->eax
& (1UL << 18));
84 TEST_ASSERT(nononarchcs
== !smt_possible(),
85 "NoNonArchitecturalCoreSharing bit"
86 " doesn't reflect SMT setting");
90 * If needed for debug:
92 * "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
93 * entry->function, entry->eax, entry->ebx, entry->ecx,
100 void test_hv_cpuid_e2big(struct kvm_vm
*vm
)
102 static struct kvm_cpuid2 cpuid
= {.nent
= 0};
105 ret
= _vcpu_ioctl(vm
, VCPU_ID
, KVM_GET_SUPPORTED_HV_CPUID
, &cpuid
);
107 TEST_ASSERT(ret
== -1 && errno
== E2BIG
,
108 "KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
109 " it should have: %d %d", ret
, errno
);
113 struct kvm_cpuid2
*kvm_get_supported_hv_cpuid(struct kvm_vm
*vm
)
115 int nent
= 20; /* should be enough */
116 static struct kvm_cpuid2
*cpuid
;
118 cpuid
= malloc(sizeof(*cpuid
) + nent
* sizeof(struct kvm_cpuid_entry2
));
127 vcpu_ioctl(vm
, VCPU_ID
, KVM_GET_SUPPORTED_HV_CPUID
, cpuid
);
133 int main(int argc
, char *argv
[])
137 struct kvm_cpuid2
*hv_cpuid_entries
;
139 /* Tell stdout not to buffer its content */
140 setbuf(stdout
, NULL
);
142 rv
= kvm_check_cap(KVM_CAP_HYPERV_CPUID
);
145 "KVM_CAP_HYPERV_CPUID not supported, skip test\n");
150 vm
= vm_create_default(VCPU_ID
, 0, guest_code
);
152 test_hv_cpuid_e2big(vm
);
154 hv_cpuid_entries
= kvm_get_supported_hv_cpuid(vm
);
155 if (!hv_cpuid_entries
)
158 test_hv_cpuid(hv_cpuid_entries
, 0);
160 free(hv_cpuid_entries
);
162 if (!kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS
)) {
164 "Enlightened VMCS is unsupported, skip related test\n");
168 vcpu_enable_evmcs(vm
, VCPU_ID
);
170 hv_cpuid_entries
= kvm_get_supported_hv_cpuid(vm
);
171 if (!hv_cpuid_entries
)
174 test_hv_cpuid(hv_cpuid_entries
, 1);
176 free(hv_cpuid_entries
);