From: Jinrong Liang <ljr.kernel@gmail.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>, Like Xu <likexu@tencent.com>,
David Matlack <dmatlack@google.com>,
Aaron Lewis <aaronlewis@google.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jinrong Liang <cloudliang@tencent.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 8/8] KVM: selftests: Test consistency of PMU MSRs with Intel PMU version
Date: Tue, 30 May 2023 21:42:48 +0800 [thread overview]
Message-ID: <20230530134248.23998-9-cloudliang@tencent.com> (raw)
In-Reply-To: <20230530134248.23998-1-cloudliang@tencent.com>
From: Jinrong Liang <cloudliang@tencent.com>
KVM user sapce may control the Intel guest PMU version number via
CPUID.0AH:EAX[07:00]. A test is added to check if a typical PMU register
that is not available at the current version number is leaking.
Co-developed-by: Like Xu <likexu@tencent.com>
Signed-off-by: Like Xu <likexu@tencent.com>
Signed-off-by: Jinrong Liang <cloudliang@tencent.com>
---
.../kvm/x86_64/pmu_basic_functionality_test.c | 64 +++++++++++++++++++
1 file changed, 64 insertions(+)
diff --git a/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c b/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c
index 108cfe254095..7da3eaf9ab5a 100644
--- a/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c
+++ b/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c
@@ -368,11 +368,75 @@ static void intel_test_fixed_counters(void)
}
}
+static void intel_guest_check_pmu_version(uint8_t version)
+{
+ switch (version) {
+ case 0:
+ GUEST_SYNC(wrmsr_safe(MSR_INTEL_ARCH_PMU_GPCTR, 0xffffull));
+ case 1:
+ GUEST_SYNC(wrmsr_safe(MSR_CORE_PERF_GLOBAL_CTRL, 0x1ull));
+ case 2:
+ /*
+ * AnyThread Bit is only supported in version 3
+ *
+ * The strange thing is that when version=0, writing ANY-Any
+ * Thread bit (bit 21) in MSR_P6_EVNTSEL0 and MSR_P6_EVNTSEL1
+ * will not generate #GP. While writing ANY-Any Thread bit
+ * (bit 21) in MSR_P6_EVNTSEL0+x (MAX_GP_CTR_NUM > x > 2) to
+ * ANY-Any Thread bit (bit 21) will generate #GP.
+ */
+ if (version == 0)
+ break;
+
+ GUEST_SYNC(wrmsr_safe(MSR_P6_EVNTSEL0, EVENTSEL_ANY));
+ break;
+ default:
+ /* KVM currently supports up to pmu version 2 */
+ GUEST_SYNC(GP_VECTOR);
+ }
+
+ GUEST_DONE();
+}
+
+static void test_pmu_version_setup(struct kvm_vcpu *vcpu, uint8_t version,
+ uint64_t expected)
+{
+ struct kvm_cpuid_entry2 *entry;
+ uint64_t msr_val;
+
+ entry = vcpu_get_cpuid_entry(vcpu, 0xa);
+ entry->eax = (entry->eax & ~PMU_VERSION_MASK) | version;
+ vcpu_set_cpuid(vcpu);
+
+ vcpu_args_set(vcpu, 1, version);
+ while (run_vcpu(vcpu, &msr_val) != UCALL_DONE) {
+ TEST_ASSERT(msr_val == expected,
+ "Something beyond this PMU version is leaked.");
+ }
+}
+
+static void intel_test_pmu_version(void)
+{
+ struct kvm_vm *vm;
+ struct kvm_vcpu *vcpu;
+ uint8_t version, unsupported_version = X86_INTEL_PMU_VERSION + 1;
+
+ TEST_REQUIRE(X86_INTEL_MAX_FIXED_CTR_NUM > 2);
+
+ for (version = 0; version <= unsupported_version; version++) {
+ vm = pmu_vm_create_with_one_vcpu(&vcpu,
+ intel_guest_check_pmu_version);
+ test_pmu_version_setup(vcpu, version, GP_VECTOR);
+ kvm_vm_free(vm);
+ }
+}
+
static void intel_test_pmu_cpuid(void)
{
intel_test_arch_events();
intel_test_counters_num();
intel_test_fixed_counters();
+ intel_test_pmu_version();
}
int main(int argc, char *argv[])
--
2.31.1
prev parent reply other threads:[~2023-05-30 13:44 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-30 13:42 [PATCH v2 0/8] KVM: selftests: Test the consistency of the PMU's CPUID and its features Jinrong Liang
2023-05-30 13:42 ` [PATCH v2 1/8] KVM: selftests: KVM: selftests: Add macros for fixed counters in processor.h Jinrong Liang
2023-06-28 19:46 ` Sean Christopherson
2023-05-30 13:42 ` [PATCH v2 2/8] KVM: selftests: Add pmu.h for PMU events and common masks Jinrong Liang
2023-06-28 20:02 ` Sean Christopherson
2023-05-30 13:42 ` [PATCH v2 3/8] KVM: selftests: Test Intel PMU architectural events on gp counters Jinrong Liang
2023-06-28 20:43 ` Sean Christopherson
2023-06-28 21:03 ` Jim Mattson
2023-05-30 13:42 ` [PATCH v2 4/8] KVM: selftests: Test Intel PMU architectural events on fixed counters Jinrong Liang
2023-05-30 13:42 ` [PATCH v2 5/8] KVM: selftests: Test consistency of CPUID with num of gp counters Jinrong Liang
2023-05-30 13:42 ` [PATCH v2 6/8] KVM: selftests: Test consistency of CPUID with num of fixed counters Jinrong Liang
2023-06-28 21:01 ` Sean Christopherson
2023-05-30 13:42 ` [PATCH v2 7/8] KVM: selftests: Test Intel supported fixed counters bit mask Jinrong Liang
2023-06-28 21:05 ` Sean Christopherson
2023-05-30 13:42 ` Jinrong Liang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230530134248.23998-9-cloudliang@tencent.com \
--to=ljr.kernel@gmail.com \
--cc=aaronlewis@google.com \
--cc=cloudliang@tencent.com \
--cc=dmatlack@google.com \
--cc=kvm@vger.kernel.org \
--cc=likexu@tencent.com \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome