Message ID | 20231110021306.1269082-27-seanjc@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp837943vqs; Thu, 9 Nov 2023 18:16:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IF0XKocAOq7roPjPPZ53/+nIG8jKpMRfh9hwgU0SPmsOCbf9TGfwawEE+YjKunCnTvZSvl1 X-Received: by 2002:a05:6a20:8419:b0:16b:d3d5:a5c5 with SMTP id c25-20020a056a20841900b0016bd3d5a5c5mr8471122pzd.52.1699582602729; Thu, 09 Nov 2023 18:16:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699582602; cv=none; d=google.com; s=arc-20160816; b=yU32f/5ID7zUtRrTMgR0KbOGq8E5QoNxQawsUR9Ea+wQT9M2x8UNImvo2IjSteHutL Udir50fDTylqTsTiDuOCVLYo7qL7bQmvBca1wq+N+G65NgKp2iurxy9kmZ+wIXTxBPR3 sMGWYDLv9F9GzPM+7EKn7nrjjOsp/cH7zNJZsJVeNw+Xn7sY/QcLDYXs1R/T7rjenmmf UtEBbxmziHCYpQ5OTlDYIfuZI8HCA4HjNjf97baMfmTXPgmlp2Mho9UHrO+HppTlvAhD ExAVUy5+D/Mv7VJPqvEz3oSI8iw0hfA5/Niv0fWv2JU2Xc2KfIbOLFLGg2GFWXlf6bsf wF/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=HhKpd1SrztABGm/U/aQBO8MgOdSjtPm1/FH+XbVpKsc=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=ztbrkYTL88bbV/BkCYFScfNcTEBBPI2smm8cOk6yNaBUXtzROIlaKmP4oU9/vo3bKB HCXH5/xfEnOFioWw+zlnrzl7ou9AlLten4kK8G8JztGOk/XULmLie8FYv/wqU5Q6Ee7I +49Zh6L4N9xdCGKCGc2epp9g5/Cml+uSFlMenVLiJKKyJK3fk/jgVnskfGTv/QISLLi1 brcYM4GtRXr0UhadIphqzVSbjUc1D3NlTwkoQOe3z9He8fI5nD6ZENlt9jkCqwz/rJSc BbEO9BS35XmuK4gECNjTfhlcMZnPhmrYLQrm2ypUe4r9zRTV6KWUyf6toYpEQg13okIe Amjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=m4jnRaPO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id lx5-20020a17090b4b0500b002810ae70420si3469829pjb.121.2023.11.09.18.16.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 18:16:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=m4jnRaPO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id B411783B0274; Thu, 9 Nov 2023 18:16:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345927AbjKJCPl (ORCPT <rfc822;lhua1029@gmail.com> + 30 others); Thu, 9 Nov 2023 21:15:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345817AbjKJCO4 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 9 Nov 2023 21:14:56 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF99E46A3 for <linux-kernel@vger.kernel.org>; Thu, 9 Nov 2023 18:14:03 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da03390793fso1867984276.3 for <linux-kernel@vger.kernel.org>; Thu, 09 Nov 2023 18:14:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699582443; x=1700187243; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=HhKpd1SrztABGm/U/aQBO8MgOdSjtPm1/FH+XbVpKsc=; b=m4jnRaPOMa1ghC1NR+EN38TtemHSytDQX+0OzCHCThb+07voZlaL/nZ7za+Bd1r/o4 HTDQAIwyrWA119M8RRfkacyCgiChaAPlQvm4D/NCU7ETDv3zjsO8Cqyhb2Tb7D+fLQ8m v3l7ASGhlRagf7Xr3CFNsgs3T0A/bkqf+/oOTu9GHvdUvBga3ldZMzH63ZBHXsJ7NGRs UXtuBPs8QdbteWHMR61ri8fsL6+eQ9r+0v+ndesv5+1nfavXQkVYcCt9GMKloYz/mgOq UdTWpqyDSEcH6a67BqGOxVctB9vBUc46IIu3FAmmapy7nvWrfeGV2oNtfgCFieSPVKVd LeyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699582443; x=1700187243; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HhKpd1SrztABGm/U/aQBO8MgOdSjtPm1/FH+XbVpKsc=; b=MGWrJu9HnbjZiFLdcA4k/aaLPdps1ByRjCOGIAQawMGNVU2hzRgdXk7l6Aq68v3jFJ 991MYMXG8BFu6vyQ0vifq1JUCH99e+0theDPKceUmYDJQ6daPfQrLnOIalCX6m/pKd6G ypZpWSGAFvewu5ixAiiMPVONk1POTCmlo1yVATcjJ6jdeCLSYv0tvHVtmq/vMS4VRDco +6E5b5bEkE9ePsUHRledCoxOo2xCAzKCYAVcACx0xVHAS2AhlaHcWNYqaSwd+Z9wVyGG M56hWPB836fXHuoU37vYpFPWbwvU6vF8i6iCVDD3zDpMgUDvaGhGP4sKcAO0blwISfVD gf7g== X-Gm-Message-State: AOJu0YweMf/M0OvM6Xvq/LHQi3QLfleMPLr0b2woDy2LBEwpP+CWNWRb 1jXT+iEMMP80/6hCSI2IdFZ8TlUXhaI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1366:b0:dae:49a3:ae23 with SMTP id bt6-20020a056902136600b00dae49a3ae23mr179118ybb.7.1699582443106; Thu, 09 Nov 2023 18:14:03 -0800 (PST) Reply-To: Sean Christopherson <seanjc@google.com> Date: Thu, 9 Nov 2023 18:13:06 -0800 In-Reply-To: <20231110021306.1269082-1-seanjc@google.com> Mime-Version: 1.0 References: <20231110021306.1269082-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231110021306.1269082-27-seanjc@google.com> Subject: [PATCH v8 26/26] KVM: selftests: Extend PMU counters test to validate RDPMC after WRMSR From: Sean Christopherson <seanjc@google.com> To: Sean Christopherson <seanjc@google.com>, Paolo Bonzini <pbonzini@redhat.com> Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang <kan.liang@linux.intel.com>, Dapeng Mi <dapeng1.mi@linux.intel.com>, Jim Mattson <jmattson@google.com>, Jinrong Liang <cloudliang@tencent.com>, Aaron Lewis <aaronlewis@google.com>, Like Xu <likexu@tencent.com> Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 09 Nov 2023 18:16:06 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782141526976838395 X-GMAIL-MSGID: 1782141526976838395 |
Series |
KVM: x86/pmu: selftests: Fixes and new tests
|
|
Commit Message
Sean Christopherson
Nov. 10, 2023, 2:13 a.m. UTC
Extend the read/write PMU counters subtest to verify that RDPMC also reads
back the written value. Opportunsitically verify that attempting to use
the "fast" mode of RDPMC fails, as the "fast" flag is only supported by
non-architectural PMUs, which KVM doesn't virtualize.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
.../selftests/kvm/x86_64/pmu_counters_test.c | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
Comments
在 2023/11/10 10:13, Sean Christopherson 写道: > Extend the read/write PMU counters subtest to verify that RDPMC also reads > back the written value. Opportunsitically verify that attempting to use > the "fast" mode of RDPMC fails, as the "fast" flag is only supported by > non-architectural PMUs, which KVM doesn't virtualize. > > Signed-off-by: Sean Christopherson <seanjc@google.com> > --- > .../selftests/kvm/x86_64/pmu_counters_test.c | 31 +++++++++++++++++++ > 1 file changed, 31 insertions(+) > > 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 cb808ac827ba..248ebe8c0577 100644 > --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c > +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c > @@ -328,6 +328,7 @@ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ > static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters, > uint8_t nr_counters, uint32_t or_mask) > { > + const bool pmu_has_fast_mode = !guest_get_pmu_version(); > uint8_t i; > > for (i = 0; i < nr_possible_counters; i++) { > @@ -352,6 +353,7 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters > const uint64_t expected_val = expect_success ? test_val : 0; > const bool expect_gp = !expect_success && msr != MSR_P6_PERFCTR0 && > msr != MSR_P6_PERFCTR1; > + uint32_t rdpmc_idx; > uint8_t vector; > uint64_t val; > > @@ -365,6 +367,35 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters > if (!expect_gp) > GUEST_ASSERT_PMC_VALUE(RDMSR, msr, val, expected_val); > > + rdpmc_idx = i; > + if (base_msr == MSR_CORE_PERF_FIXED_CTR0) > + rdpmc_idx |= INTEL_RDPMC_FIXED; > + > + /* Redo the read tests with RDPMC, and with forced emulation. */ > + vector = rdpmc_safe(rdpmc_idx, &val); > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > + if (expect_success) > + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > + > + vector = rdpmc_safe_fep(rdpmc_idx, &val); > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > + if (expect_success) > + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > + > + /* > + * KVM doesn't support non-architectural PMUs, i.e. it should > + * impossible to have fast mode RDPMC. Verify that attempting > + * to use fast RDPMC always #GPs. > + */ > + GUEST_ASSERT(!expect_success || !pmu_has_fast_mode); > + rdpmc_idx |= INTEL_RDPMC_FAST; > + > + vector = rdpmc_safe(rdpmc_idx, &val); > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); > + > + vector = rdpmc_safe_fep(rdpmc_idx, &val); > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); > + > vector = wrmsr_safe(msr, 0); > GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); > } > This test case failed on my Intel machine. Error message: Testing arch events, PMU version 0, perf_caps = 0 Testing GP counters, PMU version 0, perf_caps = 0 ==== Test Assertion Failure ==== lib/x86_64/processor.c:1100: Unhandled exception in guest pid=464480 tid=464480 errno=4 - Interrupted system call 1 0x00000000004120e1: assert_on_unhandled_exception 于 processor.c:1146 2 0x00000000004062d9: _vcpu_run 于 kvm_util.c:1634 3 0x00000000004062fa: vcpu_run 于 kvm_util.c:1645 4 0x0000000000403697: run_vcpu 于 pmu_counters_test.c:56 5 0x00000000004026fc: test_gp_counters 于 pmu_counters_test.c:434 6 (已内连入)test_intel_counters 于 pmu_counters_test.c:580 7 (已内连入)main 于 pmu_counters_test.c:604 8 0x00007f7a2f03ad84: ?? ??:0 9 0x00000000004028bd: _start 于 ??:? Unhandled exception '0x6' at guest RIP '0x402bab'
On Mon, Nov 13, 2023, Jinrong Liang wrote: > 在 2023/11/10 10:13, Sean Christopherson 写道: > > Extend the read/write PMU counters subtest to verify that RDPMC also reads > > back the written value. Opportunsitically verify that attempting to use > > the "fast" mode of RDPMC fails, as the "fast" flag is only supported by > > non-architectural PMUs, which KVM doesn't virtualize. > > > > Signed-off-by: Sean Christopherson <seanjc@google.com> > > --- > > + /* Redo the read tests with RDPMC, and with forced emulation. */ > > + vector = rdpmc_safe(rdpmc_idx, &val); > > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > > + if (expect_success) > > + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > > + > > + vector = rdpmc_safe_fep(rdpmc_idx, &val); > > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > > + if (expect_success) > > + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > This test case failed on my Intel machine. > > Error message: > Testing arch events, PMU version 0, perf_caps = 0 > Testing GP counters, PMU version 0, perf_caps = 0 > ==== Test Assertion Failure ==== > lib/x86_64/processor.c:1100: Unhandled exception in guest > pid=464480 tid=464480 errno=4 - Interrupted system call > 1 0x00000000004120e1: assert_on_unhandled_exception 于 processor.c:1146 > 2 0x00000000004062d9: _vcpu_run 于 kvm_util.c:1634 > 3 0x00000000004062fa: vcpu_run 于 kvm_util.c:1645 > 4 0x0000000000403697: run_vcpu 于 pmu_counters_test.c:56 > 5 0x00000000004026fc: test_gp_counters 于 pmu_counters_test.c:434 > 6 (已内连入)test_intel_counters 于 pmu_counters_test.c:580 > 7 (已内连入)main 于 pmu_counters_test.c:604 > 8 0x00007f7a2f03ad84: ?? ??:0 > 9 0x00000000004028bd: _start 于 ??:? > Unhandled exception '0x6' at guest RIP '0x402bab' Argh, I didn't add a check to see if forced emulation is actually enabled (forced emulation uses a magic "prefix" to trigger a #UD, which KVM intercepts; if forced emulation isn't enabled, KVM ignores the magic prefix and reflects the #UD back into the guest). This fixes the test for me: --- .../selftests/kvm/x86_64/pmu_counters_test.c | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) 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 248ebe8c0577..ae5f6042f1e8 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -325,6 +325,26 @@ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ "Expected " #insn "(0x%x) to yield 0x%lx, got 0x%lx", \ msr, expected_val, val); +static void guest_test_rdpmc(uint32_t rdpmc_idx, bool expect_success, + uint64_t expected_val) +{ + uint8_t vector; + uint64_t val; + + vector = rdpmc_safe(rdpmc_idx, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); + if (expect_success) + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); + + if (!is_forced_emulation_enabled) + return; + + vector = rdpmc_safe_fep(rdpmc_idx, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); + if (expect_success) + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); +} + static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters, uint8_t nr_counters, uint32_t or_mask) { @@ -367,20 +387,15 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters if (!expect_gp) GUEST_ASSERT_PMC_VALUE(RDMSR, msr, val, expected_val); + /* + * Redo the read tests with RDPMC, which has different indexing + * semantics and additional capabilities. + */ rdpmc_idx = i; if (base_msr == MSR_CORE_PERF_FIXED_CTR0) rdpmc_idx |= INTEL_RDPMC_FIXED; - /* Redo the read tests with RDPMC, and with forced emulation. */ - vector = rdpmc_safe(rdpmc_idx, &val); - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); - if (expect_success) - GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); - - vector = rdpmc_safe_fep(rdpmc_idx, &val); - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); - if (expect_success) - GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); + guest_test_rdpmc(rdpmc_idx, expect_success, expected_val); /* * KVM doesn't support non-architectural PMUs, i.e. it should @@ -389,12 +404,7 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters */ GUEST_ASSERT(!expect_success || !pmu_has_fast_mode); rdpmc_idx |= INTEL_RDPMC_FAST; - - vector = rdpmc_safe(rdpmc_idx, &val); - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); - - vector = rdpmc_safe_fep(rdpmc_idx, &val); - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); + guest_test_rdpmc(rdpmc_idx, false, -1ull); vector = wrmsr_safe(msr, 0); GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); base-commit: 743a1a6d106931691be32e081e929d9b3de5777f --
Sean Christopherson <seanjc@google.com> 于2023年11月13日周一 21:40写道: > > On Mon, Nov 13, 2023, Jinrong Liang wrote: > > 在 2023/11/10 10:13, Sean Christopherson 写道: > > > Extend the read/write PMU counters subtest to verify that RDPMC also reads > > > back the written value. Opportunsitically verify that attempting to use > > > the "fast" mode of RDPMC fails, as the "fast" flag is only supported by > > > non-architectural PMUs, which KVM doesn't virtualize. > > > > > > Signed-off-by: Sean Christopherson <seanjc@google.com> > > > --- > > > + /* Redo the read tests with RDPMC, and with forced emulation. */ > > > + vector = rdpmc_safe(rdpmc_idx, &val); > > > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > > > + if (expect_success) > > > + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > > > + > > > + vector = rdpmc_safe_fep(rdpmc_idx, &val); > > > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > > > + if (expect_success) > > > + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > > > This test case failed on my Intel machine. > > > > Error message: > > Testing arch events, PMU version 0, perf_caps = 0 > > Testing GP counters, PMU version 0, perf_caps = 0 > > ==== Test Assertion Failure ==== > > lib/x86_64/processor.c:1100: Unhandled exception in guest > > pid=464480 tid=464480 errno=4 - Interrupted system call > > 1 0x00000000004120e1: assert_on_unhandled_exception 于 processor.c:1146 > > 2 0x00000000004062d9: _vcpu_run 于 kvm_util.c:1634 > > 3 0x00000000004062fa: vcpu_run 于 kvm_util.c:1645 > > 4 0x0000000000403697: run_vcpu 于 pmu_counters_test.c:56 > > 5 0x00000000004026fc: test_gp_counters 于 pmu_counters_test.c:434 > > 6 (已内连入)test_intel_counters 于 pmu_counters_test.c:580 > > 7 (已内连入)main 于 pmu_counters_test.c:604 > > 8 0x00007f7a2f03ad84: ?? ??:0 > > 9 0x00000000004028bd: _start 于 ??:? > > Unhandled exception '0x6' at guest RIP '0x402bab' > > Argh, I didn't add a check to see if forced emulation is actually enabled (forced > emulation uses a magic "prefix" to trigger a #UD, which KVM intercepts; if forced > emulation isn't enabled, KVM ignores the magic prefix and reflects the #UD back > into the guest). > > This fixes the test for me: > > --- > .../selftests/kvm/x86_64/pmu_counters_test.c | 42 ++++++++++++------- > 1 file changed, 26 insertions(+), 16 deletions(-) > > 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 248ebe8c0577..ae5f6042f1e8 100644 > --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c > +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c > @@ -325,6 +325,26 @@ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ > "Expected " #insn "(0x%x) to yield 0x%lx, got 0x%lx", \ > msr, expected_val, val); > > +static void guest_test_rdpmc(uint32_t rdpmc_idx, bool expect_success, > + uint64_t expected_val) > +{ > + uint8_t vector; > + uint64_t val; > + > + vector = rdpmc_safe(rdpmc_idx, &val); > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > + if (expect_success) > + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > + > + if (!is_forced_emulation_enabled) > + return; > + > + vector = rdpmc_safe_fep(rdpmc_idx, &val); > + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > + if (expect_success) > + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > +} > + > static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters, > uint8_t nr_counters, uint32_t or_mask) > { > @@ -367,20 +387,15 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters > if (!expect_gp) > GUEST_ASSERT_PMC_VALUE(RDMSR, msr, val, expected_val); > > + /* > + * Redo the read tests with RDPMC, which has different indexing > + * semantics and additional capabilities. > + */ > rdpmc_idx = i; > if (base_msr == MSR_CORE_PERF_FIXED_CTR0) > rdpmc_idx |= INTEL_RDPMC_FIXED; > > - /* Redo the read tests with RDPMC, and with forced emulation. */ > - vector = rdpmc_safe(rdpmc_idx, &val); > - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > - if (expect_success) > - GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > - > - vector = rdpmc_safe_fep(rdpmc_idx, &val); > - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); > - if (expect_success) > - GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); > + guest_test_rdpmc(rdpmc_idx, expect_success, expected_val); > > /* > * KVM doesn't support non-architectural PMUs, i.e. it should > @@ -389,12 +404,7 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters > */ > GUEST_ASSERT(!expect_success || !pmu_has_fast_mode); > rdpmc_idx |= INTEL_RDPMC_FAST; > - > - vector = rdpmc_safe(rdpmc_idx, &val); > - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); > - > - vector = rdpmc_safe_fep(rdpmc_idx, &val); > - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); > + guest_test_rdpmc(rdpmc_idx, false, -1ull); > > vector = wrmsr_safe(msr, 0); > GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); > > base-commit: 743a1a6d106931691be32e081e929d9b3de5777f > -- This fix worked perfectly, thanks. >
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 cb808ac827ba..248ebe8c0577 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -328,6 +328,7 @@ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters, uint8_t nr_counters, uint32_t or_mask) { + const bool pmu_has_fast_mode = !guest_get_pmu_version(); uint8_t i; for (i = 0; i < nr_possible_counters; i++) { @@ -352,6 +353,7 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters const uint64_t expected_val = expect_success ? test_val : 0; const bool expect_gp = !expect_success && msr != MSR_P6_PERFCTR0 && msr != MSR_P6_PERFCTR1; + uint32_t rdpmc_idx; uint8_t vector; uint64_t val; @@ -365,6 +367,35 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters if (!expect_gp) GUEST_ASSERT_PMC_VALUE(RDMSR, msr, val, expected_val); + rdpmc_idx = i; + if (base_msr == MSR_CORE_PERF_FIXED_CTR0) + rdpmc_idx |= INTEL_RDPMC_FIXED; + + /* Redo the read tests with RDPMC, and with forced emulation. */ + vector = rdpmc_safe(rdpmc_idx, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); + if (expect_success) + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); + + vector = rdpmc_safe_fep(rdpmc_idx, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); + if (expect_success) + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); + + /* + * KVM doesn't support non-architectural PMUs, i.e. it should + * impossible to have fast mode RDPMC. Verify that attempting + * to use fast RDPMC always #GPs. + */ + GUEST_ASSERT(!expect_success || !pmu_has_fast_mode); + rdpmc_idx |= INTEL_RDPMC_FAST; + + vector = rdpmc_safe(rdpmc_idx, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); + + vector = rdpmc_safe_fep(rdpmc_idx, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); + vector = wrmsr_safe(msr, 0); GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); }