2018-12-10 18:21:59 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Test for x86 KVM_CAP_HYPERV_CPUID
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018, Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include "test_util.h"
|
|
|
|
#include "kvm_util.h"
|
|
|
|
#include "processor.h"
|
2019-08-14 12:02:41 -04:00
|
|
|
#include "vmx.h"
|
2018-12-10 18:21:59 +01:00
|
|
|
|
|
|
|
static void guest_code(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-01-17 16:34:53 -08:00
|
|
|
static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
|
2018-12-10 18:21:59 +01:00
|
|
|
{
|
2025-01-17 16:34:54 -08:00
|
|
|
const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip;
|
2025-01-17 16:34:53 -08:00
|
|
|
const struct kvm_cpuid2 *hv_cpuid_entries;
|
2018-12-10 18:21:59 +01:00
|
|
|
int i;
|
2022-02-03 11:46:15 +01:00
|
|
|
int nent_expected = 10;
|
2020-05-29 16:45:43 +03:00
|
|
|
u32 test_val;
|
2018-12-10 18:21:59 +01:00
|
|
|
|
2025-01-17 16:34:53 -08:00
|
|
|
if (vcpu)
|
|
|
|
hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
|
|
|
|
else
|
|
|
|
hv_cpuid_entries = kvm_get_supported_hv_cpuid();
|
|
|
|
|
2022-02-03 11:46:15 +01:00
|
|
|
TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
|
2020-05-29 16:45:43 +03:00
|
|
|
"KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
|
2022-02-03 11:46:15 +01:00
|
|
|
" (returned %d)",
|
|
|
|
nent_expected, hv_cpuid_entries->nent);
|
2018-12-10 18:21:59 +01:00
|
|
|
|
|
|
|
for (i = 0; i < hv_cpuid_entries->nent; i++) {
|
2022-06-14 20:06:55 +00:00
|
|
|
const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
|
2018-12-10 18:21:59 +01:00
|
|
|
|
|
|
|
TEST_ASSERT((entry->function >= 0x40000000) &&
|
2020-05-29 16:45:43 +03:00
|
|
|
(entry->function <= 0x40000082),
|
2020-03-10 10:15:55 +01:00
|
|
|
"function %x is our of supported range",
|
2018-12-10 18:21:59 +01:00
|
|
|
entry->function);
|
|
|
|
|
|
|
|
TEST_ASSERT(entry->index == 0,
|
|
|
|
".index field should be zero");
|
|
|
|
|
|
|
|
TEST_ASSERT(entry->flags == 0,
|
|
|
|
".flags field should be zero");
|
|
|
|
|
2019-05-14 13:34:51 +03:00
|
|
|
TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
|
|
|
|
!entry->padding[2], "padding should be zero");
|
2018-12-10 18:21:59 +01:00
|
|
|
|
2020-05-29 16:45:43 +03:00
|
|
|
switch (entry->function) {
|
|
|
|
case 0x40000000:
|
|
|
|
test_val = 0x40000082;
|
2019-09-16 18:22:58 +02:00
|
|
|
|
2020-05-29 16:45:43 +03:00
|
|
|
TEST_ASSERT(entry->eax == test_val,
|
|
|
|
"Wrong max leaf report in 0x40000000.EAX: %x"
|
|
|
|
" (evmcs=%d)",
|
2020-09-29 17:09:44 +02:00
|
|
|
entry->eax, evmcs_expected
|
2020-05-29 16:45:43 +03:00
|
|
|
);
|
|
|
|
break;
|
2025-01-17 16:34:54 -08:00
|
|
|
case 0x40000003:
|
|
|
|
TEST_ASSERT(has_irqchip || !(entry->edx & BIT(19)),
|
|
|
|
"\"Direct\" Synthetic Timers should require in-kernel APIC");
|
|
|
|
break;
|
2020-05-29 16:45:43 +03:00
|
|
|
case 0x40000004:
|
|
|
|
test_val = entry->eax & (1UL << 18);
|
|
|
|
|
2025-03-05 16:59:54 -06:00
|
|
|
TEST_ASSERT(!!test_val == !is_smt_possible(),
|
2019-09-16 18:22:58 +02:00
|
|
|
"NoNonArchitecturalCoreSharing bit"
|
|
|
|
" doesn't reflect SMT setting");
|
2025-01-17 16:34:54 -08:00
|
|
|
|
|
|
|
TEST_ASSERT(has_irqchip || !(entry->eax & BIT(10)),
|
|
|
|
"Cluster IPI (i.e. SEND_IPI) should require in-kernel APIC");
|
2020-05-29 16:45:43 +03:00
|
|
|
break;
|
2022-02-03 11:46:15 +01:00
|
|
|
case 0x4000000A:
|
|
|
|
TEST_ASSERT(entry->eax & (1UL << 19),
|
|
|
|
"Enlightened MSR-Bitmap should always be supported"
|
|
|
|
" 0x40000000.EAX: %x", entry->eax);
|
|
|
|
if (evmcs_expected)
|
|
|
|
TEST_ASSERT((entry->eax & 0xffff) == 0x101,
|
|
|
|
"Supported Enlightened VMCS version range is supposed to be 1:1"
|
|
|
|
" 0x40000000.EAX: %x", entry->eax);
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-09-16 18:22:58 +02:00
|
|
|
|
2022-02-03 11:46:15 +01:00
|
|
|
}
|
2018-12-10 18:21:59 +01:00
|
|
|
/*
|
|
|
|
* If needed for debug:
|
|
|
|
* fprintf(stdout,
|
|
|
|
* "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
|
|
|
|
* entry->function, entry->eax, entry->ebx, entry->ecx,
|
|
|
|
* entry->edx);
|
|
|
|
*/
|
|
|
|
}
|
2025-01-17 16:34:53 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note, the CPUID array returned by the system-scoped helper is a one-
|
|
|
|
* time allocation, i.e. must not be freed.
|
|
|
|
*/
|
|
|
|
if (vcpu)
|
|
|
|
free((void *)hv_cpuid_entries);
|
2018-12-10 18:21:59 +01:00
|
|
|
}
|
|
|
|
|
2025-01-17 16:34:52 -08:00
|
|
|
static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
|
2018-12-10 18:21:59 +01:00
|
|
|
{
|
|
|
|
static struct kvm_cpuid2 cpuid = {.nent = 0};
|
|
|
|
int ret;
|
|
|
|
|
2022-02-15 16:08:48 -08:00
|
|
|
if (vcpu)
|
2022-06-02 13:41:33 -07:00
|
|
|
ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
|
2020-09-29 17:09:44 +02:00
|
|
|
else
|
2022-02-16 16:51:20 -08:00
|
|
|
ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
|
2018-12-10 18:21:59 +01:00
|
|
|
|
|
|
|
TEST_ASSERT(ret == -1 && errno == E2BIG,
|
2020-09-29 17:09:44 +02:00
|
|
|
"%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
|
2022-02-15 16:08:48 -08:00
|
|
|
" it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
|
2018-12-10 18:21:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct kvm_vm *vm;
|
2022-02-15 16:08:48 -08:00
|
|
|
struct kvm_vcpu *vcpu;
|
2018-12-10 18:21:59 +01:00
|
|
|
|
2022-05-27 16:24:02 -07:00
|
|
|
TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
|
2018-12-10 18:21:59 +01:00
|
|
|
|
2025-01-17 16:34:54 -08:00
|
|
|
/* Test the vCPU ioctl without an in-kernel local APIC. */
|
|
|
|
vm = vm_create_barebones();
|
|
|
|
vcpu = __vm_vcpu_add(vm, 0);
|
|
|
|
test_hv_cpuid(vcpu, false);
|
|
|
|
kvm_vm_free(vm);
|
2020-05-29 16:45:43 +03:00
|
|
|
|
2020-09-29 17:09:44 +02:00
|
|
|
/* Test vCPU ioctl version */
|
2025-01-17 16:34:54 -08:00
|
|
|
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
|
2022-02-15 16:08:48 -08:00
|
|
|
test_hv_cpuid_e2big(vm, vcpu);
|
2025-01-17 16:34:53 -08:00
|
|
|
test_hv_cpuid(vcpu, false);
|
2018-12-10 18:21:59 +01:00
|
|
|
|
2022-06-14 20:06:31 +00:00
|
|
|
if (!kvm_cpu_has(X86_FEATURE_VMX) ||
|
2022-05-27 15:13:03 -07:00
|
|
|
!kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
|
2020-09-29 17:09:44 +02:00
|
|
|
print_skip("Enlightened VMCS is unsupported");
|
|
|
|
goto do_sys;
|
2019-04-26 15:27:11 +02:00
|
|
|
}
|
2022-06-02 13:41:33 -07:00
|
|
|
vcpu_enable_evmcs(vcpu);
|
2025-01-17 16:34:53 -08:00
|
|
|
test_hv_cpuid(vcpu, true);
|
2020-09-29 17:09:44 +02:00
|
|
|
|
|
|
|
do_sys:
|
|
|
|
/* Test system ioctl version */
|
2022-05-27 15:13:03 -07:00
|
|
|
if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
|
2020-09-29 17:09:44 +02:00
|
|
|
print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-02-15 16:08:48 -08:00
|
|
|
test_hv_cpuid_e2big(vm, NULL);
|
2025-01-17 16:34:53 -08:00
|
|
|
test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX));
|
2020-09-29 17:09:44 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
kvm_vm_free(vm);
|
2018-12-10 18:21:59 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|