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 union perf_capabilities {
29 u64 full_width_write:1;
32 u64 pebs_output_pt_available:1;
33 u64 anythread_deprecated:1;
39 * The LBR format and most PEBS features are immutable, all other features are
40 * fungible (if supported by the host and KVM).
42 static const union perf_capabilities immutable_caps = {
50 static const union perf_capabilities format_caps = {
55 static void guest_code(uint64_t current_val)
60 vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, current_val);
61 GUEST_ASSERT_2(vector == GP_VECTOR, current_val, vector);
63 vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, 0);
64 GUEST_ASSERT_2(vector == GP_VECTOR, 0, vector);
66 for (i = 0; i < 64; i++) {
67 vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES,
68 current_val ^ BIT_ULL(i));
69 GUEST_ASSERT_2(vector == GP_VECTOR,
70 current_val ^ BIT_ULL(i), vector);
77 * Verify that guest WRMSRs to PERF_CAPABILITIES #GP regardless of the value
78 * written, that the guest always sees the userspace controlled value, and that
79 * PERF_CAPABILITIES is immutable after KVM_RUN.
81 static void test_guest_wrmsr_perf_capabilities(union perf_capabilities host_cap)
83 struct kvm_vcpu *vcpu;
84 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code);
88 vm_init_descriptor_tables(vm);
89 vcpu_init_descriptor_tables(vcpu);
91 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
93 vcpu_args_set(vcpu, 1, host_cap.capabilities);
96 switch (get_ucall(vcpu, &uc)) {
98 REPORT_GUEST_ASSERT_2(uc, "val = 0x%lx, vector = %lu");
103 TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
106 ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities);
108 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
110 r = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
111 TEST_ASSERT(!r, "Post-KVM_RUN write '0' didn't fail");
113 for (i = 0; i < 64; i++) {
114 r = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES,
115 host_cap.capabilities ^ BIT_ULL(i));
116 TEST_ASSERT(!r, "Post-KVM_RUN write '0x%llx'didn't fail",
117 host_cap.capabilities ^ BIT_ULL(i));
124 * Verify KVM allows writing PERF_CAPABILITIES with all KVM-supported features
125 * enabled, as well as '0' (to disable all features).
127 static void test_basic_perf_capabilities(union perf_capabilities host_cap)
129 struct kvm_vcpu *vcpu;
130 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
132 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
133 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
138 static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
140 const uint64_t fungible_caps = host_cap.capabilities & ~immutable_caps.capabilities;
142 struct kvm_vcpu *vcpu;
143 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
146 for_each_set_bit(bit, &fungible_caps, 64) {
147 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, BIT_ULL(bit));
148 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES,
149 host_cap.capabilities & ~BIT_ULL(bit));
151 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
157 * Verify KVM rejects attempts to set unsupported and/or immutable features in
158 * PERF_CAPABILITIES. Note, LBR format and PEBS format need to be validated
159 * separately as they are multi-bit values, e.g. toggling or setting a single
160 * bit can generate a false positive without dedicated safeguards.
162 static void test_immutable_perf_capabilities(union perf_capabilities host_cap)
164 const uint64_t reserved_caps = (~host_cap.capabilities |
165 immutable_caps.capabilities) &
166 ~format_caps.capabilities;
168 struct kvm_vcpu *vcpu;
169 struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
170 union perf_capabilities val = host_cap;
173 for_each_set_bit(bit, &reserved_caps, 64) {
174 r = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES,
175 host_cap.capabilities ^ BIT_ULL(bit));
176 TEST_ASSERT(!r, "%s immutable feature 0x%llx (bit %d) didn't fail",
177 host_cap.capabilities & BIT_ULL(bit) ? "Setting" : "Clearing",
182 * KVM only supports the host's native LBR format, as well as '0' (to
183 * disable LBR support). Verify KVM rejects all other LBR formats.
185 for (val.lbr_format = 1; val.lbr_format; val.lbr_format++) {
186 if (val.lbr_format == host_cap.lbr_format)
189 r = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val.capabilities);
190 TEST_ASSERT(!r, "Bad LBR FMT = 0x%x didn't fail, host = 0x%x",
191 val.lbr_format, host_cap.lbr_format);
194 /* Ditto for the PEBS format. */
195 for (val.pebs_format = 1; val.pebs_format; val.pebs_format++) {
196 if (val.pebs_format == host_cap.pebs_format)
199 r = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val.capabilities);
200 TEST_ASSERT(!r, "Bad PEBS FMT = 0x%x didn't fail, host = 0x%x",
201 val.pebs_format, host_cap.pebs_format);
208 * Test that LBR MSRs are writable when LBRs are enabled, and then verify that
209 * disabling the vPMU via CPUID also disables LBR support. Set bits 2:0 of
210 * LBR_TOS as those bits are writable across all uarch implementations (arch
211 * LBRs will need to poke a different MSR).
213 static void test_lbr_perf_capabilities(union perf_capabilities host_cap)
215 struct kvm_vcpu *vcpu;
219 if (!host_cap.lbr_format)
222 vm = vm_create_with_one_vcpu(&vcpu, NULL);
224 vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
225 vcpu_set_msr(vcpu, MSR_LBR_TOS, 7);
227 vcpu_clear_cpuid_entry(vcpu, X86_PROPERTY_PMU_VERSION.function);
229 r = _vcpu_set_msr(vcpu, MSR_LBR_TOS, 7);
230 TEST_ASSERT(!r, "Writing LBR_TOS should fail after disabling vPMU");
235 int main(int argc, char *argv[])
237 union perf_capabilities host_cap;
239 TEST_REQUIRE(get_kvm_param_bool("enable_pmu"));
240 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM));
242 TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
243 TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0);
245 host_cap.capabilities = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
247 TEST_ASSERT(host_cap.full_width_write,
248 "Full-width writes should always be supported");
250 test_basic_perf_capabilities(host_cap);
251 test_fungible_perf_capabilities(host_cap);
252 test_immutable_perf_capabilities(host_cap);
253 test_guest_wrmsr_perf_capabilities(host_cap);
254 test_lbr_perf_capabilities(host_cap);