1 // SPDX-License-Identifier: GPL-2.0
3 * Test for VMX-pmu perf capability msr
5 * Copyright (C) 2021 Intel Corporation
7 * Test to check the effect of various CPUID settings on
8 * MSR_IA32_PERF_CAPABILITIES MSR, and check that what
9 * we write with KVM_SET_MSR is _not_ modified by the guest
10 * and check it can be retrieved with KVM_GET_MSR, also test
11 * the invalid LBR formats are rejected.
14 #define _GNU_SOURCE /* for program_invocation_short_name */
15 #include <sys/ioctl.h>
20 #define PMU_CAP_FW_WRITES (1ULL << 13)
21 #define PMU_CAP_LBR_FMT 0x3f
23 union perf_capabilities {
30 u64 full_width_write:1;
33 u64 pebs_output_pt_available:1;
34 u64 anythread_deprecated:1;
39 static void guest_code(void)
41 wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT);
45 * Verify KVM allows writing PERF_CAPABILITIES with all KVM-supported features
46 * enabled, as well as '0' (to disable all features).
48 static void test_basic_perf_capabilities(union perf_capabilities host_cap)
50 struct kvm_vcpu *vcpu;
51 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
53 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
54 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
59 static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
61 struct kvm_vcpu *vcpu;
62 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code);
64 /* testcase 1, set capabilities when we have PDCM bit */
65 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, PMU_CAP_FW_WRITES);
67 /* check whatever we write with KVM_SET_MSR is _not_ modified */
69 ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), PMU_CAP_FW_WRITES);
74 static void test_immutable_perf_capabilities(union perf_capabilities host_cap)
76 struct kvm_vcpu *vcpu;
77 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
82 * KVM only supports the host's native LBR format, as well as '0' (to
83 * disable LBR support). Verify KVM rejects all other LBR formats.
85 for (val = 1; val <= PMU_CAP_LBR_FMT; val++) {
86 if (val == (host_cap.capabilities & PMU_CAP_LBR_FMT))
89 ret = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val);
90 TEST_ASSERT(!ret, "Bad LBR FMT = 0x%lx didn't fail", val);
95 int main(int argc, char *argv[])
97 union perf_capabilities host_cap;
99 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM));
101 TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
102 TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0);
104 host_cap.capabilities = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
105 host_cap.capabilities &= (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT);
107 TEST_ASSERT(host_cap.full_width_write,
108 "Full-width writes should always be supported");
110 test_basic_perf_capabilities(host_cap);
111 test_fungible_perf_capabilities(host_cap);
112 test_immutable_perf_capabilities(host_cap);
114 printf("Completed perf capability tests.\n");