Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp636775rdd; Tue, 9 Jan 2024 15:14:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHRAWAq5Jm1j2IwlGmJPCJ1EHmK4tWdO2PGY4MT0YAzvRY9tJjQRCp8TLkijKOCKJP2RIJI X-Received: by 2002:a05:6a00:812:b0:6da:e7c2:42ca with SMTP id m18-20020a056a00081200b006dae7c242camr284096pfk.21.1704842044846; Tue, 09 Jan 2024 15:14:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704842044; cv=none; d=google.com; s=arc-20160816; b=DTuVy96VMwSBxDYn6ou1O10cx1dGsQE3y0dgxAYKo/cyoRLV9q4lUM0CvrwrFeZqyB AV4f0ftbmYRs3ceXRUGAGel2G15++AFHjL8QYKck9fXz70Gl8DSYWKcuvboIr6bJuxSA EM0RTNvbzeK/QdaHceKdFSCdERW1ZU1X2LIsN9ovYVrsKRgS+vrn4wi8Jt22IxKALEqK N4pkvqhbcr0WtYbpt3qkvbywb/Ph4NNjMqh0thhmAxFkafdJgxsKcrOwW9kc0I7xDNfy wXYHEeZ2Pt5nzucBFTnQCtfh5sO7oHJKyxQLPlTMVp2VynOIYcz8t2i1T38GDcbkoBqE UmRw== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21518-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id s23-20020a639257000000b005c1ce3c9617si2178902pgn.901.2024.01.09.15.14.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:14:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21518-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21518-linux.lists.archive=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 46A0D287F7D for ; Tue, 9 Jan 2024 23:14:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A08244D133; Tue, 9 Jan 2024 23:03:53 +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 Content-Type: text/plain; charset="UTF-8" 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); } -- 2.43.0.472.g3155946c3a-goog