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.
13 #include <sys/ioctl.h>
15 #include <linux/bitmap.h>
17 #include "kvm_test_harness.h"
21 static union perf_capabilities
{
28 u64 full_width_write
:1;
31 u64 pebs_output_pt_available
:1;
32 u64 anythread_deprecated
:1;
38 * The LBR format and most PEBS features are immutable, all other features are
39 * fungible (if supported by the host and KVM).
41 static const union perf_capabilities immutable_caps
= {
49 static const union perf_capabilities format_caps
= {
54 static void guest_test_perf_capabilities_gp(uint64_t val
)
56 uint8_t vector
= wrmsr_safe(MSR_IA32_PERF_CAPABILITIES
, val
);
58 __GUEST_ASSERT(vector
== GP_VECTOR
,
59 "Expected #GP for value '0x%lx', got vector '0x%x'",
63 static void guest_code(uint64_t current_val
)
67 guest_test_perf_capabilities_gp(current_val
);
68 guest_test_perf_capabilities_gp(0);
70 for (i
= 0; i
< 64; i
++)
71 guest_test_perf_capabilities_gp(current_val
^ BIT_ULL(i
));
76 KVM_ONE_VCPU_TEST_SUITE(vmx_pmu_caps
);
79 * Verify that guest WRMSRs to PERF_CAPABILITIES #GP regardless of the value
80 * written, that the guest always sees the userspace controlled value, and that
81 * PERF_CAPABILITIES is immutable after KVM_RUN.
83 KVM_ONE_VCPU_TEST(vmx_pmu_caps
, guest_wrmsr_perf_capabilities
, guest_code
)
88 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, host_cap
.capabilities
);
90 vcpu_args_set(vcpu
, 1, host_cap
.capabilities
);
93 switch (get_ucall(vcpu
, &uc
)) {
95 REPORT_GUEST_ASSERT(uc
);
100 TEST_FAIL("Unexpected ucall: %lu", uc
.cmd
);
103 TEST_ASSERT_EQ(vcpu_get_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
),
104 host_cap
.capabilities
);
106 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, host_cap
.capabilities
);
108 r
= _vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, 0);
109 TEST_ASSERT(!r
, "Post-KVM_RUN write '0' didn't fail");
111 for (i
= 0; i
< 64; i
++) {
112 r
= _vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
,
113 host_cap
.capabilities
^ BIT_ULL(i
));
114 TEST_ASSERT(!r
, "Post-KVM_RUN write '0x%llx'didn't fail",
115 host_cap
.capabilities
^ BIT_ULL(i
));
120 * Verify KVM allows writing PERF_CAPABILITIES with all KVM-supported features
121 * enabled, as well as '0' (to disable all features).
123 KVM_ONE_VCPU_TEST(vmx_pmu_caps
, basic_perf_capabilities
, guest_code
)
125 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, 0);
126 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, host_cap
.capabilities
);
129 KVM_ONE_VCPU_TEST(vmx_pmu_caps
, fungible_perf_capabilities
, guest_code
)
131 const uint64_t fungible_caps
= host_cap
.capabilities
& ~immutable_caps
.capabilities
;
134 for_each_set_bit(bit
, &fungible_caps
, 64) {
135 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, BIT_ULL(bit
));
136 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
,
137 host_cap
.capabilities
& ~BIT_ULL(bit
));
139 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, host_cap
.capabilities
);
143 * Verify KVM rejects attempts to set unsupported and/or immutable features in
144 * PERF_CAPABILITIES. Note, LBR format and PEBS format need to be validated
145 * separately as they are multi-bit values, e.g. toggling or setting a single
146 * bit can generate a false positive without dedicated safeguards.
148 KVM_ONE_VCPU_TEST(vmx_pmu_caps
, immutable_perf_capabilities
, guest_code
)
150 const uint64_t reserved_caps
= (~host_cap
.capabilities
|
151 immutable_caps
.capabilities
) &
152 ~format_caps
.capabilities
;
153 union perf_capabilities val
= host_cap
;
156 for_each_set_bit(bit
, &reserved_caps
, 64) {
157 r
= _vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
,
158 host_cap
.capabilities
^ BIT_ULL(bit
));
159 TEST_ASSERT(!r
, "%s immutable feature 0x%llx (bit %d) didn't fail",
160 host_cap
.capabilities
& BIT_ULL(bit
) ? "Setting" : "Clearing",
165 * KVM only supports the host's native LBR format, as well as '0' (to
166 * disable LBR support). Verify KVM rejects all other LBR formats.
168 for (val
.lbr_format
= 1; val
.lbr_format
; val
.lbr_format
++) {
169 if (val
.lbr_format
== host_cap
.lbr_format
)
172 r
= _vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, val
.capabilities
);
173 TEST_ASSERT(!r
, "Bad LBR FMT = 0x%x didn't fail, host = 0x%x",
174 val
.lbr_format
, host_cap
.lbr_format
);
177 /* Ditto for the PEBS format. */
178 for (val
.pebs_format
= 1; val
.pebs_format
; val
.pebs_format
++) {
179 if (val
.pebs_format
== host_cap
.pebs_format
)
182 r
= _vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, val
.capabilities
);
183 TEST_ASSERT(!r
, "Bad PEBS FMT = 0x%x didn't fail, host = 0x%x",
184 val
.pebs_format
, host_cap
.pebs_format
);
189 * Test that LBR MSRs are writable when LBRs are enabled, and then verify that
190 * disabling the vPMU via CPUID also disables LBR support. Set bits 2:0 of
191 * LBR_TOS as those bits are writable across all uarch implementations (arch
192 * LBRs will need to poke a different MSR).
194 KVM_ONE_VCPU_TEST(vmx_pmu_caps
, lbr_perf_capabilities
, guest_code
)
198 if (!host_cap
.lbr_format
)
201 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, host_cap
.capabilities
);
202 vcpu_set_msr(vcpu
, MSR_LBR_TOS
, 7);
204 vcpu_clear_cpuid_entry(vcpu
, X86_PROPERTY_PMU_VERSION
.function
);
206 r
= _vcpu_set_msr(vcpu
, MSR_LBR_TOS
, 7);
207 TEST_ASSERT(!r
, "Writing LBR_TOS should fail after disabling vPMU");
210 KVM_ONE_VCPU_TEST(vmx_pmu_caps
, perf_capabilities_unsupported
, guest_code
)
215 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, host_cap
.capabilities
);
216 val
= vcpu_get_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
);
217 TEST_ASSERT_EQ(val
, host_cap
.capabilities
);
219 vcpu_clear_cpuid_feature(vcpu
, X86_FEATURE_PDCM
);
221 val
= vcpu_get_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
);
222 TEST_ASSERT_EQ(val
, 0);
224 vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, 0);
226 for (i
= 0; i
< 64; i
++) {
227 r
= _vcpu_set_msr(vcpu
, MSR_IA32_PERF_CAPABILITIES
, BIT_ULL(i
));
228 TEST_ASSERT(!r
, "Setting PERF_CAPABILITIES bit %d (= 0x%llx) should fail without PDCM",
233 int main(int argc
, char *argv
[])
235 TEST_REQUIRE(kvm_is_pmu_enabled());
236 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM
));
238 TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION
));
239 TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION
) > 0);
241 host_cap
.capabilities
= kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES
);
243 TEST_ASSERT(host_cap
.full_width_write
,
244 "Full-width writes should always be supported");
246 return test_harness_run(argc
, argv
);