From patchwork Tue Jan 9 23:02:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 186616 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp463109dyi; Tue, 9 Jan 2024 15:14:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IG6tKtOTLpBY83p8Q70/EHW7k6iA4YkFr2QXhfiu1dr4b/ryE46QWargMm/9znqyomHi1LZ X-Received: by 2002:a17:906:d8d7:b0:a27:b8e0:ed89 with SMTP id re23-20020a170906d8d700b00a27b8e0ed89mr90348ejb.138.1704842058115; Tue, 09 Jan 2024 15:14:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704842058; cv=none; d=google.com; s=arc-20160816; b=e2AUqQF9Q524Rdx4Qlv279rojPOleyB4Af4KY95xtEl7NoDRpw7YbFxNSabsrxzdyX P7kllXj0VyZebxPDQ2//5hmxvXJIHB/14Ig+J3VByuYO7icA5m7k3r+O+Ena/p1e+R/S TDj5u2a8R76E6c326YSmd6siFZR5ofDCDCTVDF3zoJZFLidXeptfhbPc0dnh/qn/6Yv8 yz8XtoDs5L32voW0ilNGdDDO7ilS8Nfr0i8Zjkz9Rea4bT0vUOj0Gxqxs2d2Q66Ikh+R 5XaxwHw+Ge1w8DkKaTRFbR01LoBzDlFO7Q9qqV9yyCWvZhgEzRdmMLA2Id6q5VbFOgwn TzQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=KPmV5XTeXIjrJabLJdrKSKnfueERqfbYH9Vd6C8BrEE=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=OBmkMcKlTUjWld8scU2KpU3E8gRNeNvGh1MdfzkqbMJHpSxxN4wxnWZZM/tFgy/HWM jIyWvaaZ78A1OwukTQDiiKH4gSeRYjTcpO0lc1Yi2I+PhGaYAGjADQztkYd3DF2VvNL2 f6+F/i+owhRajEx5rtGJPpaHfcxzotX7ympQBVht5S8ujvcjC3hbxU6prcK6Aa4SNjfa sGOYTDEq1VO7Bo/1hN9Ah6SeH52UloBnjs2Pgn+BZLdoMI5oqntkmVZq2K+vprIGG0mk C924VqpvSsedMmXo02iPNutifAkPxxfVS+MeiDJm5jsrYXTD8UKhq37f79MExBtR2lfq hYKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="ad/fdzU8"; spf=pass (google.com: domain of linux-kernel+bounces-21518-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21518-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id h9-20020a1709063c0900b00a27c639e5f3si1173563ejg.1000.2024.01.09.15.14.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:14:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21518-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="ad/fdzU8"; spf=pass (google.com: domain of linux-kernel+bounces-21518-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21518-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 88D471F261B3 for ; Tue, 9 Jan 2024 23:14:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7E4324D58A; Tue, 9 Jan 2024 23:03:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ad/fdzU8" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67B244D103 for ; Tue, 9 Jan 2024 23:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5f38d676cecso50401487b3.0 for ; Tue, 09 Jan 2024 15:03:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704841430; x=1705446230; 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=KPmV5XTeXIjrJabLJdrKSKnfueERqfbYH9Vd6C8BrEE=; b=ad/fdzU8zoAarCh2kPmtvWjzsYFcdCgNeFlKbGbXUN47nLaO7FkHokEgAvnkMpVLOB hgoZJADaV4hN+X7inq9HWftIthyE7bP34CLUbHL0Mex8dguNB/XkvQn/VEtH9D/ajGbC UDtYMBoyVs7hbUBY83Wde1jPjGW77mdDLcD+c5iKZjJf/59txOtoNP7UrJjnoaB0y8of ewfau7T5KsbPq04QEHhU23xKiaygNHQb1eOSzKwNFlTAIIWyBIywNFGjQ3pEfgJBpfes iuKafT0xHBESUOZUulO6FJoLN/g04RuMayW4qAzaPuMJ9RNhLg5Dy0ZhMV9Cxmg48V0H 922A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704841430; x=1705446230; 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=KPmV5XTeXIjrJabLJdrKSKnfueERqfbYH9Vd6C8BrEE=; b=sVJQNMAD+D2+oWCFVSTivOehR1iNMirFflJ6/+zTo4CGmdMEWj9NamIYzsVnywt0nK TwYVAqfi+aPvjhfxHSHSWMg3tA4TU4PzRJWIAhhVr0yhEsepUY2ybOonA1A1+PChJUcy VmuLtBgRe+dtUlXHCG4u9mLkHKRMC4RE/J0SOrrrirpMo9P+0TPVsryl46ZDb3b7B8wF hlLkGg+p93/AJKpbIvaCf2ozJ25yQk7Tu4DDn00U9DonBGIuS3q+1Cc/MKTZdTjCyh1/ LGOsaVuDv7Bi3PnQ0MVQdoNj03n0x5HuQGbfgKLuFaFXbZxT2yCTTrtif58jVzFEGrSZ 55tw== X-Gm-Message-State: AOJu0YxbPC3D9sHjjan+s0Pe9Ud6XVvVEfhfwbXphztYzlx47FhQE+/6 QU2JUrvj/zoLQsqn3s3zlgZbiNLLNw8961L0uA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:8750:0:b0:dbd:30b0:828e with SMTP id e16-20020a258750000000b00dbd30b0828emr98708ybn.1.1704841430539; Tue, 09 Jan 2024 15:03:50 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 15:02:49 -0800 In-Reply-To: <20240109230250.424295-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240109230250.424295-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240109230250.424295-30-seanjc@google.com> Subject: [PATCH v10 29/29] KVM: selftests: Extend PMU counters test to validate RDPMC after WRMSR From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jim Mattson , Jinrong Liang , Aaron Lewis , Like Xu X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787656465982795929 X-GMAIL-MSGID: 1787656465982795929 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 --- .../selftests/kvm/x86_64/pmu_counters_test.c | 41 +++++++++++++++++++ 1 file changed, 41 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..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,9 +325,30 @@ __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) { + const bool pmu_has_fast_mode = !guest_get_pmu_version(); uint8_t i; for (i = 0; i < nr_possible_counters; i++) { @@ -352,6 +373,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 +387,25 @@ 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; + + guest_test_rdpmc(rdpmc_idx, expect_success, 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; + guest_test_rdpmc(rdpmc_idx, false, -1ull); + vector = wrmsr_safe(msr, 0); GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); }