Path: blob/master/tools/testing/selftests/kvm/x86/hyperv_cpuid.c
38245 views
// SPDX-License-Identifier: GPL-2.01/*2* Test for x86 KVM_CAP_HYPERV_CPUID3*4* Copyright (C) 2018, Red Hat, Inc.5*6* This work is licensed under the terms of the GNU GPL, version 2.7*8*/9#include <fcntl.h>10#include <stdio.h>11#include <stdlib.h>12#include <string.h>13#include <sys/ioctl.h>1415#include "test_util.h"16#include "kvm_util.h"17#include "processor.h"18#include "vmx.h"1920static void guest_code(void)21{22}2324static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)25{26const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip;27const struct kvm_cpuid2 *hv_cpuid_entries;28int i;29int nent_expected = 10;30u32 test_val;3132if (vcpu)33hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);34else35hv_cpuid_entries = kvm_get_supported_hv_cpuid();3637TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,38"KVM_GET_SUPPORTED_HV_CPUID should return %d entries"39" (returned %d)",40nent_expected, hv_cpuid_entries->nent);4142for (i = 0; i < hv_cpuid_entries->nent; i++) {43const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];4445TEST_ASSERT((entry->function >= 0x40000000) &&46(entry->function <= 0x40000082),47"function %x is out of supported range",48entry->function);4950TEST_ASSERT(entry->index == 0,51".index field should be zero");5253TEST_ASSERT(entry->flags == 0,54".flags field should be zero");5556TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&57!entry->padding[2], "padding should be zero");5859switch (entry->function) {60case 0x40000000:61test_val = 0x40000082;6263TEST_ASSERT(entry->eax == test_val,64"Wrong max leaf report in 0x40000000.EAX: %x"65" (evmcs=%d)",66entry->eax, evmcs_expected67);68break;69case 0x40000003:70TEST_ASSERT(has_irqchip || !(entry->edx & BIT(19)),71"\"Direct\" Synthetic Timers should require in-kernel APIC");72break;73case 0x40000004:74test_val = entry->eax & (1UL << 18);7576TEST_ASSERT(!!test_val == !is_smt_possible(),77"NoNonArchitecturalCoreSharing bit"78" doesn't reflect SMT setting");7980TEST_ASSERT(has_irqchip || !(entry->eax & BIT(10)),81"Cluster IPI (i.e. SEND_IPI) should require in-kernel APIC");82break;83case 0x4000000A:84TEST_ASSERT(entry->eax & (1UL << 19),85"Enlightened MSR-Bitmap should always be supported"86" 0x40000000.EAX: %x", entry->eax);87if (evmcs_expected)88TEST_ASSERT((entry->eax & 0xffff) == 0x101,89"Supported Enlightened VMCS version range is supposed to be 1:1"90" 0x40000000.EAX: %x", entry->eax);9192break;93default:94break;9596}97/*98* If needed for debug:99* fprintf(stdout,100* "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",101* entry->function, entry->eax, entry->ebx, entry->ecx,102* entry->edx);103*/104}105106/*107* Note, the CPUID array returned by the system-scoped helper is a one-108* time allocation, i.e. must not be freed.109*/110if (vcpu)111free((void *)hv_cpuid_entries);112}113114static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)115{116static struct kvm_cpuid2 cpuid = {.nent = 0};117int ret;118119if (vcpu)120ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);121else122ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);123124TEST_ASSERT(ret == -1 && errno == E2BIG,125"%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"126" it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);127}128129int main(int argc, char *argv[])130{131struct kvm_vm *vm;132struct kvm_vcpu *vcpu;133134TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));135136/* Test the vCPU ioctl without an in-kernel local APIC. */137vm = vm_create_barebones();138vcpu = __vm_vcpu_add(vm, 0);139test_hv_cpuid(vcpu, false);140kvm_vm_free(vm);141142/* Test vCPU ioctl version */143vm = vm_create_with_one_vcpu(&vcpu, guest_code);144test_hv_cpuid_e2big(vm, vcpu);145test_hv_cpuid(vcpu, false);146147if (!kvm_cpu_has(X86_FEATURE_VMX) ||148!kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {149print_skip("Enlightened VMCS is unsupported");150goto do_sys;151}152vcpu_enable_evmcs(vcpu);153test_hv_cpuid(vcpu, true);154155do_sys:156/* Test system ioctl version */157if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {158print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");159goto out;160}161162test_hv_cpuid_e2big(vm, NULL);163test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX));164165out:166kvm_vm_free(vm);167168return 0;169}170171172