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>
17 #include <linux/bitmap.h>
22 #define PMU_CAP_LBR_FMT 0x3f
24 union perf_capabilities {
31 u64 full_width_write:1;
34 u64 pebs_output_pt_available:1;
35 u64 anythread_deprecated:1;
41 * The LBR format and most PEBS features are immutable, all other features are
42 * fungible (if supported by the host and KVM).
44 static const union perf_capabilities immutable_caps = {
52 static const union perf_capabilities format_caps = {
57 static void guest_code(void)
59 wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT);
63 * Verify KVM allows writing PERF_CAPABILITIES with all KVM-supported features
64 * enabled, as well as '0' (to disable all features).
66 static void test_basic_perf_capabilities(union perf_capabilities host_cap)
68 struct kvm_vcpu *vcpu;
69 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
71 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
72 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
77 static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
79 const uint64_t fungible_caps = host_cap.capabilities & ~immutable_caps.capabilities;
81 struct kvm_vcpu *vcpu;
82 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code);
85 for_each_set_bit(bit, &fungible_caps, 64) {
86 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, BIT_ULL(bit));
87 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES,
88 host_cap.capabilities & ~BIT_ULL(bit));
90 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
92 /* check whatever we write with KVM_SET_MSR is _not_ modified */
94 ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities);
100 * Verify KVM rejects attempts to set unsupported and/or immutable features in
101 * PERF_CAPABILITIES. Note, LBR format and PEBS format need to be validated
102 * separately as they are multi-bit values, e.g. toggling or setting a single
103 * bit can generate a false positive without dedicated safeguards.
105 static void test_immutable_perf_capabilities(union perf_capabilities host_cap)
107 const uint64_t reserved_caps = (~host_cap.capabilities |
108 immutable_caps.capabilities) &
109 ~format_caps.capabilities;
111 struct kvm_vcpu *vcpu;
112 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
116 for_each_set_bit(bit, &reserved_caps, 64) {
117 r = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES,
118 host_cap.capabilities ^ BIT_ULL(bit));
119 TEST_ASSERT(!r, "%s immutable feature 0x%llx (bit %d) didn't fail",
120 host_cap.capabilities & BIT_ULL(bit) ? "Setting" : "Clearing",
125 * KVM only supports the host's native LBR format, as well as '0' (to
126 * disable LBR support). Verify KVM rejects all other LBR formats.
128 for (val = 1; val <= PMU_CAP_LBR_FMT; val++) {
129 if (val == (host_cap.capabilities & PMU_CAP_LBR_FMT))
132 r = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val);
133 TEST_ASSERT(!r, "Bad LBR FMT = 0x%lx didn't fail", val);
139 int main(int argc, char *argv[])
141 union perf_capabilities host_cap;
143 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM));
145 TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
146 TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0);
148 host_cap.capabilities = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
150 TEST_ASSERT(host_cap.full_width_write,
151 "Full-width writes should always be supported");
153 test_basic_perf_capabilities(host_cap);
154 test_fungible_perf_capabilities(host_cap);
155 test_immutable_perf_capabilities(host_cap);
157 printf("Completed perf capability tests.\n");