KVM: selftests: Expand negative testing of guest writes to PERF_CAPABILITIES
authorSean Christopherson <seanjc@google.com>
Sat, 11 Mar 2023 00:46:13 +0000 (16:46 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 6 Apr 2023 21:58:44 +0000 (14:58 -0700)
Test that the guest can't write 0 to PERF_CAPABILITIES, can't write the
current value, and can't toggle _any_ bits.  There is no reason to special
case the LBR format.

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

index d91bf44a2e39305460eee4d4e4aa90e8e61bfde9..44fc6101a547206d4b037fb29609a2e8fb5d2002 100644 (file)
@@ -54,9 +54,59 @@ static const union perf_capabilities format_caps = {
        .pebs_format = -1,
 };
 
-static void guest_code(void)
+static void guest_code(uint64_t current_val)
 {
-       wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT);
+       uint8_t vector;
+       int i;
+
+       vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, current_val);
+       GUEST_ASSERT_2(vector == GP_VECTOR, current_val, vector);
+
+       vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, 0);
+       GUEST_ASSERT_2(vector == GP_VECTOR, 0, vector);
+
+       for (i = 0; i < 64; i++) {
+               vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES,
+                                   current_val ^ BIT_ULL(i));
+               GUEST_ASSERT_2(vector == GP_VECTOR,
+                              current_val ^ BIT_ULL(i), vector);
+       }
+
+       GUEST_DONE();
+}
+
+/*
+ * Verify that guest WRMSRs to PERF_CAPABILITIES #GP regardless of the value
+ * written, that the guest always sees the userspace controlled value, and that
+ * PERF_CAPABILITIES is immutable after KVM_RUN.
+ */
+static void test_guest_wrmsr_perf_capabilities(union perf_capabilities host_cap)
+{
+       struct kvm_vcpu *vcpu;
+       struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+       struct ucall uc;
+
+       vm_init_descriptor_tables(vm);
+       vcpu_init_descriptor_tables(vcpu);
+
+       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
+
+       vcpu_args_set(vcpu, 1, host_cap.capabilities);
+       vcpu_run(vcpu);
+
+       switch (get_ucall(vcpu, &uc)) {
+       case UCALL_ABORT:
+               REPORT_GUEST_ASSERT_2(uc, "val = 0x%lx, vector = %lu");
+               break;
+       case UCALL_DONE:
+               break;
+       default:
+               TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
+       }
+
+       ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities);
+
+       kvm_vm_free(vm);
 }
 
 /*
@@ -79,7 +129,7 @@ static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
        const uint64_t fungible_caps = host_cap.capabilities & ~immutable_caps.capabilities;
 
        struct kvm_vcpu *vcpu;
-       struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+       struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
        int bit;
 
        for_each_set_bit(bit, &fungible_caps, 64) {
@@ -89,10 +139,6 @@ static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
        }
        vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
 
-       /* check whatever we write with KVM_SET_MSR is _not_ modified */
-       vcpu_run(vcpu);
-       ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities);
-
        kvm_vm_free(vm);
 }
 
@@ -153,6 +199,7 @@ int main(int argc, char *argv[])
        test_basic_perf_capabilities(host_cap);
        test_fungible_perf_capabilities(host_cap);
        test_immutable_perf_capabilities(host_cap);
+       test_guest_wrmsr_perf_capabilities(host_cap);
 
        printf("Completed perf capability tests.\n");
 }