diff mbox series

[8/9] KVM: selftests: Test AMD Guest PerfCtrExtCore

Message ID 20231121115457.76269-9-cloudliang@tencent.com (mailing list archive)
State New, archived
Headers show
Series Test the consistency of AMD PMU counters and their features | expand

Commit Message

Jinrong Liang Nov. 21, 2023, 11:54 a.m. UTC
From: Jinrong Liang <cloudliang@tencent.com>

Add test case for AMD Guest PerfCtrExtCore.

Signed-off-by: Jinrong Liang <cloudliang@tencent.com>
---
 .../selftests/kvm/x86_64/pmu_counters_test.c  | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
index 07cb9694e225..ca5b352ea6ae 100644
--- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
+++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
@@ -863,6 +863,36 @@  static void test_amd_pmu_version(void)
 	}
 }
 
+static void guest_test_perfctr_core(void)
+{
+	bool expect_gp = !this_cpu_has(X86_FEATURE_PERFCTR_CORE);
+	unsigned int i;
+
+	for (i = 0; i < AMD64_NR_COUNTERS_CORE; i++) {
+		guest_wr_rd_amd_counters(MSR_F15H_PERF_CTR0 + i * 2, expect_gp, 0xffff);
+		guest_wr_rd_amd_counters(MSR_F15H_PERF_CTL0 + i * 2, expect_gp, 0);
+	}
+
+	GUEST_DONE();
+}
+
+static void test_amd_perfctr_core(void)
+{
+	bool kvm_pmu_has_perfctr_core = kvm_cpu_has(X86_FEATURE_PERFCTR_CORE);
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	unsigned int i;
+
+	for (i = 0; i <= kvm_pmu_has_perfctr_core; i++) {
+		vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_perfctr_core);
+
+		vcpu_set_or_clear_cpuid_feature(vcpu, X86_FEATURE_PERFCTR_CORE, i);
+
+		run_vcpu(vcpu);
+		kvm_vm_free(vm);
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	TEST_REQUIRE(kvm_is_pmu_enabled());
@@ -881,6 +911,7 @@  int main(int argc, char *argv[])
 		test_amd_zen_events();
 		test_amd_counters_num();
 		test_amd_pmu_version();
+		test_amd_perfctr_core();
 	} else {
 		TEST_FAIL("Unknown CPU vendor");
 	}