]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: selftests: Verify get/set PERF_CAPABILITIES w/o guest PDMC behavior
authorSean Christopherson <seanjc@google.com>
Fri, 2 Aug 2024 18:55:10 +0000 (11:55 -0700)
committerSean Christopherson <seanjc@google.com>
Fri, 1 Nov 2024 16:22:35 +0000 (09:22 -0700)
Add another testcase to x86's PMU capabilities test to verify that KVM's
handling of userspace accesses to PERF_CAPABILITIES when the vCPU doesn't
support the MSR (per the vCPU's CPUID).  KVM's (newly established) ABI is
that userspace MSR accesses are subject to architectural existence checks,
but that if the MSR is advertised as supported _by KVM_, "bad" reads get
'0' and writes of '0' are always allowed.

Link: https://lore.kernel.org/r/20240802185511.305849-9-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c

index 7c92536551cca17a33fd40e0149b0e487a50023c..a1f5ff45d518d375701077327071d74005fb079a 100644 (file)
@@ -207,6 +207,29 @@ KVM_ONE_VCPU_TEST(vmx_pmu_caps, lbr_perf_capabilities, guest_code)
        TEST_ASSERT(!r, "Writing LBR_TOS should fail after disabling vPMU");
 }
 
+KVM_ONE_VCPU_TEST(vmx_pmu_caps, perf_capabilities_unsupported, guest_code)
+{
+       uint64_t val;
+       int i, r;
+
+       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
+       val = vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES);
+       TEST_ASSERT_EQ(val, host_cap.capabilities);
+
+       vcpu_clear_cpuid_feature(vcpu, X86_FEATURE_PDCM);
+
+       val = vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES);
+       TEST_ASSERT_EQ(val, 0);
+
+       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
+
+       for (i = 0; i < 64; i++) {
+               r = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, BIT_ULL(i));
+               TEST_ASSERT(!r, "Setting PERF_CAPABILITIES bit %d (= 0x%llx) should fail without PDCM",
+                           i, BIT_ULL(i));
+       }
+}
+
 int main(int argc, char *argv[])
 {
        TEST_REQUIRE(kvm_is_pmu_enabled());