From: Like Xu <like.xu.linux@gmail.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Jinrong Liang <cloudliang@tencent.com>,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Subject: [PATCH 6/7] KVM: selftests: Test consistency of PMU MSRs with Intel PMU version
Date: Thu, 23 Mar 2023 15:27:13 +0800 [thread overview]
Message-ID: <20230323072714.82289-7-likexu@tencent.com> (raw)
In-Reply-To: <20230323072714.82289-1-likexu@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>
---
.../selftests/kvm/x86_64/pmu_cpuid_test.c | 57 +++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/tools/testing/selftests/kvm/x86_64/pmu_cpuid_test.c b/tools/testing/selftests/kvm/x86_64/pmu_cpuid_test.c
index 79f2e144c6c6..caf0d98079c7 100644
--- a/tools/testing/selftests/kvm/x86_64/pmu_cpuid_test.c
+++ b/tools/testing/selftests/kvm/x86_64/pmu_cpuid_test.c
@@ -17,6 +17,7 @@
#define NUM_BRANCHES 10
#define EVENTSEL_OS BIT_ULL(17)
+#define EVENTSEL_ANY BIT_ULL(21)
#define EVENTSEL_EN BIT_ULL(22)
#define PMU_CAP_FW_WRITES BIT_ULL(13)
#define EVENTS_MASK GENMASK_ULL(7, 0)
@@ -90,6 +91,14 @@ static uint32_t kvm_fixed_ctrs_bitmask(void)
return kvm_entry->ecx;
}
+static uint32_t kvm_max_pmu_version(void)
+{
+ const struct kvm_cpuid_entry2 *kvm_entry;
+
+ kvm_entry = get_cpuid_entry(kvm_get_supported_cpuid(), 0xa, 0);
+ return kvm_entry->eax & PMU_VERSION_MASK;
+}
+
static struct kvm_vcpu *new_vcpu(void *guest_code)
{
struct kvm_vm *vm;
@@ -220,6 +229,25 @@ static void intel_guest_run_fixed_counters(uint64_t supported_bitmask,
GUEST_DONE();
}
+static void intel_guest_check_pmu_version(uint8_t version)
+{
+ switch (version) {
+ case 0:
+ wrmsr(MSR_INTEL_ARCH_PMU_GPCTR, 0xffffull);
+ case 1:
+ wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0x1ull);
+ case 2:
+ /* AnyThread Bit is only supported in version 3 */
+ wrmsr(MSR_P6_EVNTSEL0, EVENTSEL_ANY);
+ break;
+ default:
+ /* KVM currently supports up to pmu version 2 */
+ GUEST_SYNC(GP_VECTOR);
+ }
+
+ GUEST_DONE();
+}
+
static void test_arch_events_setup(struct kvm_vcpu *vcpu, uint8_t evt_vector,
uint8_t unavl_mask, uint8_t idx)
{
@@ -341,6 +369,18 @@ static void intel_test_fixed_counters(void)
}
}
+static void test_pmu_version_setup(struct kvm_vcpu *vcpu, uint8_t version)
+{
+ struct kvm_cpuid_entry2 *entry;
+
+ 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);
+ vm_install_exception_handler(vcpu->vm, GP_VECTOR, guest_gp_handler);
+}
+
static void intel_check_arch_event_is_unavl(uint8_t idx)
{
const char *msg = "Unavailable arch event is counting.";
@@ -441,11 +481,28 @@ static void intel_test_arch_events(void)
}
}
+static void intel_test_pmu_version(void)
+{
+ const char *msg = "Something beyond this PMU version is leaked.";
+ uint8_t version, unsupported_version = kvm_max_pmu_version() + 1;
+ struct kvm_vcpu *vcpu;
+
+ TEST_REQUIRE(kvm_gp_ctrs_num() > 2);
+
+ for (version = 0; version <= unsupported_version; version++) {
+ vcpu = new_vcpu(intel_guest_check_pmu_version);
+ test_pmu_version_setup(vcpu, version);
+ run_vcpu(vcpu, msg, first_uc_arg_equals, (void *)GP_VECTOR);
+ free_vcpu(vcpu);
+ }
+}
+
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.40.0
next prev parent reply other threads:[~2023-03-23 7:28 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-23 7:27 [PATCH 0/7] KVM: selftests: Test the consistency of the PMU's CPUID and its features Like Xu
2023-03-23 7:27 ` [PATCH 1/7] KVM: selftests: Test Intel PMU architectural events on gp counters Like Xu
2023-05-24 22:32 ` Sean Christopherson
2023-05-24 22:59 ` Jim Mattson
2023-03-23 7:27 ` [PATCH 2/7] KVM: selftests: Test Intel PMU architectural events on fixed counters Like Xu
2023-05-24 22:36 ` Sean Christopherson
2023-03-23 7:27 ` [PATCH 3/7] KVM: selftests: Test consistency of CPUID with num of GP counters Like Xu
2023-05-24 22:44 ` Sean Christopherson
2023-03-23 7:27 ` [PATCH 4/7] KVM: selftests: Test consistency of CPUID with num of Fixed counters Like Xu
2023-05-24 22:47 ` Sean Christopherson
2023-05-24 23:08 ` Jim Mattson
2023-03-23 7:27 ` [PATCH 5/7] KVM: selftests: Test Intel supported fixed counters bit mask Like Xu
2023-03-23 7:27 ` Like Xu [this message]
2023-03-23 7:27 ` [PATCH 7/7] KVM: selftests: Test Intel counters' bit width emulation Like Xu
2023-05-24 22:52 ` Sean Christopherson
2023-05-24 22:53 ` [PATCH 0/7] KVM: selftests: Test the consistency of the PMU's CPUID and its features Sean Christopherson
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=20230323072714.82289-7-likexu@tencent.com \
--to=like.xu.linux@gmail.com \
--cc=cloudliang@tencent.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.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
all inboxes | Powered by JetHome®