File Index
Files and directories
Sign in
FILE

hyperv_cpuid.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/kvm/x86/hyperv_cpuid.c 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#include <fcntl.h>
 11#include <stdio.h>
 12#include <stdlib.h>
 13#include <string.h>
 14#include <sys/ioctl.h>
 15
 16#include "test_util.h"
 17#include "kvm_util.h"
 18#include "processor.h"
 19#include "vmx.h"
 20
 21static void guest_code(void)
 22{
 23}
 24
 25static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
 26{
 27	const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip;
 28	const struct kvm_cpuid2 *hv_cpuid_entries;
 29	int i;
 30	int nent_expected = 10;
 31	u32 test_val;
 32
 33	if (vcpu)
 34		hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
 35	else
 36		hv_cpuid_entries = kvm_get_supported_hv_cpuid();
 37
 38	TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
 39		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
 40		    " (returned %d)",
 41		    nent_expected, hv_cpuid_entries->nent);
 42
 43	for (i = 0; i < hv_cpuid_entries->nent; i++) {
 44		const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
 45
 46		TEST_ASSERT((entry->function >= 0x40000000) &&
 47			    (entry->function <= 0x40000082),
 48			    "function %x is out of supported range",
 49			    entry->function);
 50
 51		TEST_ASSERT(entry->index == 0,
 52			    ".index field should be zero");
 53
 54		TEST_ASSERT(entry->flags == 0,
 55			    ".flags field should be zero");
 56
 57		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
 58			    !entry->padding[2], "padding should be zero");
 59
 60		switch (entry->function) {
 61		case 0x40000000:
 62			test_val = 0x40000082;
 63
 64			TEST_ASSERT(entry->eax == test_val,
 65				    "Wrong max leaf report in 0x40000000.EAX: %x"
 66				    " (evmcs=%d)",
 67				    entry->eax, evmcs_expected
 68				);
 69			break;
 70		case 0x40000003:
 71			TEST_ASSERT(has_irqchip || !(entry->edx & BIT(19)),
 72				    "\"Direct\" Synthetic Timers should require in-kernel APIC");
 73			break;
 74		case 0x40000004:
 75			test_val = entry->eax & (1UL << 18);
 76
 77			TEST_ASSERT(!!test_val == !is_smt_possible(),
 78				    "NoNonArchitecturalCoreSharing bit"
 79				    " doesn't reflect SMT setting");
 80
 81			TEST_ASSERT(has_irqchip || !(entry->eax & BIT(10)),
 82				    "Cluster IPI (i.e. SEND_IPI) should require in-kernel APIC");
 83			break;
 84		case 0x4000000A:
 85			TEST_ASSERT(entry->eax & (1UL << 19),
 86				    "Enlightened MSR-Bitmap should always be supported"
 87				    " 0x40000000.EAX: %x", entry->eax);
 88			if (evmcs_expected)
 89				TEST_ASSERT((entry->eax & 0xffff) == 0x101,
 90				    "Supported Enlightened VMCS version range is supposed to be 1:1"
 91				    " 0x40000000.EAX: %x", entry->eax);
 92
 93			break;
 94		default:
 95			break;
 96
 97		}
 98		/*
 99		 * If needed for debug:
100		 * fprintf(stdout,
101		 *	"CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
102		 *	entry->function, entry->eax, entry->ebx, entry->ecx,
103		 *	entry->edx);
104		 */
105	}
106
107	/*
108	 * Note, the CPUID array returned by the system-scoped helper is a one-
109	 * time allocation, i.e. must not be freed.
110	 */
111	if (vcpu)
112		free((void *)hv_cpuid_entries);
113}
114
115static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
116{
117	static struct kvm_cpuid2 cpuid = {.nent = 0};
118	int ret;
119
120	if (vcpu)
121		ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
122	else
123		ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
124
125	TEST_ASSERT(ret == -1 && errno == E2BIG,
126		    "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
127		    " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
128}
129
130int main(int argc, char *argv[])
131{
132	struct kvm_vm *vm;
133	struct kvm_vcpu *vcpu;
134
135	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
136
137	/* Test the vCPU ioctl without an in-kernel local APIC. */
138	vm = vm_create_barebones();
139	vcpu = __vm_vcpu_add(vm, 0);
140	test_hv_cpuid(vcpu, false);
141	kvm_vm_free(vm);
142
143	/* Test vCPU ioctl version */
144	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
145	test_hv_cpuid_e2big(vm, vcpu);
146	test_hv_cpuid(vcpu, false);
147
148	if (!kvm_cpu_has(X86_FEATURE_VMX) ||
149	    !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
150		print_skip("Enlightened VMCS is unsupported");
151		goto do_sys;
152	}
153	vcpu_enable_evmcs(vcpu);
154	test_hv_cpuid(vcpu, true);
155
156do_sys:
157	/* Test system ioctl version */
158	if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
159		print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
160		goto out;
161	}
162
163	test_hv_cpuid_e2big(vm, NULL);
164	test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX));
165
166out:
167	kvm_vm_free(vm);
168
169	return 0;
170}
~/ Read-only file preview.