Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1406359rdb; Fri, 1 Dec 2023 16:07:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IELj2MpVtVk/gQppCG5T0MDhYLeYqRCJ85DB9OpA36yqG78ZFx8rNaYvJpz/o/47YAgmOWK X-Received: by 2002:a05:6358:9214:b0:170:8da:1f50 with SMTP id d20-20020a056358921400b0017008da1f50mr747273rwb.20.1701475660932; Fri, 01 Dec 2023 16:07:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701475660; cv=none; d=google.com; s=arc-20160816; b=jgfXxjqAM2zeDE8yU92WdtQWF8j2SuXUAXdBS5JNvD+fbFAKm7NKbqIf/47A4jy/H/ T9KLZuOzBu1PmjVTnRiHJziXQaMHfc5w+GDvWGeVbySqOGE3JVL/IJbSEqUDrpVdq/Yx deAlSNtmiLyISisxnumi+cyoeukMAOaFwp6r9yNjNvbx5ePAiEtgqoZ89VRNwPXxQI/2 weUBljnBLTpl/IKoVT+inKaVsd3KlO131rNfMBhzj1UepAfzm5Di7rdbx5F+TdtGRqtL 8OgvrNCScKb9C7Y71sTrSlQK67j5i1cQN3vLEsOwlYRI6p7qU/IbxciKn1LWdOZsimt0 LGiA== 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=tAuhvXq6cBpnB6C4oo6DppaIL90HLN4H/La3MC06/hY=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=rDSvHWObo59fgdDkFDJjqODq4YroHxKubMeRchFMZQ2iEgrQXQNPNq/Z/c+mz76FPq hXkgVf+SGysB2IeNYZnJhxUEbZj5/r2Mnxe0M5/9m/xXakC2DkiLt7Qm8pTV2xKbkUPg E3CQj4zliVAISicRPE84BFnu0R4AURAM8/WIEW1mM5iiG34Y/alqStMDfzFtvdEVsp+F i1JQup3bpiW2JLXgwi1HUeX2jXLqs9o4WhCfKLBbwVplyMzy7UE7XP14BM5dIgDIIKmv 9+zUEbOwV0cD7NNXiBBfzs9AvojoFDHDZ8rfvEsOWtXKEC9fNfYWNyzGTeWWOWCweZg4 DVMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JHiIk0sf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id j2-20020a056a00174200b006cd88a2426fsi4072132pfc.157.2023.12.01.16.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 16:07:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JHiIk0sf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id C14F080E9941; Fri, 1 Dec 2023 16:07:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441958AbjLBAF6 (ORCPT + 99 others); Fri, 1 Dec 2023 19:05:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442009AbjLBAFP (ORCPT ); Fri, 1 Dec 2023 19:05:15 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50BB41980 for ; Fri, 1 Dec 2023 16:04:58 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d1b2153ba1so45202387b3.2 for ; Fri, 01 Dec 2023 16:04:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701475497; x=1702080297; 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=tAuhvXq6cBpnB6C4oo6DppaIL90HLN4H/La3MC06/hY=; b=JHiIk0sfjYlz6ZXEnfkfQMwzsocr3vRNLceC0LmM0J3hHuMOvHNLnQQvNMPDWAwwG7 3VBqTTCTpa0UKi37pGb7sK0VnvqQICVGw42+5jiKyKAhXqo+vM2G0o0rcDyRZ49v1K64 Nzjh4n2/3TOyYlWNHVnxnY3e5GWFUQQ1YcksP+T6Jhr8SsfisJ1IU95ljQVQdWmpj1D+ Q4DaznvIeQHLQzmQfo79PpyIOv0+I4WwoKLBy+fFpxea1dcV7KbvLQ+5MVeI0A65bK3/ XSbI0WE9k/mTkSSPSSoUSF4Pg4+BWO6Xxbkyzq06NKkLSRCZmRjX2n1Tbg9deC4aLOJQ ATRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701475497; x=1702080297; 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=tAuhvXq6cBpnB6C4oo6DppaIL90HLN4H/La3MC06/hY=; b=swj7vt6wlOtncWY6ynibTHuXYn3i1Bw+rzhSDt1I5OqZUGDElfxrcKTBSsbHwzPGXO DRvld52/b5jPF15Q4XFhTG3CXnV2B4j5+N8d3c+/YTALU0vHein9SwGu3YK0+L5Ehc09 X5NIw88+g6kksljRL0Xmrkp/Wg2MlgMHleOf2bcTic78O/OhN35XQNnqmJOdtmlXDcA8 uTVZsz1j3cnpI1b6k84JsKUXkkzBcxr6cs7PpmlM3bOBjVcpFgVXJphdFm9HyPPHCgdl D/+8bNvg02wfz6CMg0OFK5+7rGQJCmjkYuNiHg/tCjsgIoPGwPK25T4oWgYXtXrrnMGI 8hYA== X-Gm-Message-State: AOJu0Yxq15Asw8S/kzgon/9vJJNqPX79pylemCs6i81MqiwLh2UtJ/8+ YABYLkt7KvNsFld3mGpbOZkvysuKHCI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:2712:b0:5d4:1b2d:f436 with SMTP id dy18-20020a05690c271200b005d41b2df436mr116491ywb.5.1701475497031; Fri, 01 Dec 2023 16:04:57 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 1 Dec 2023 16:04:08 -0800 In-Reply-To: <20231202000417.922113-1-seanjc@google.com> Mime-Version: 1.0 References: <20231202000417.922113-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231202000417.922113-20-seanjc@google.com> Subject: [PATCH v9 19/28] KVM: selftests: Add functional test for Intel's fixed PMU counters 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" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Fri, 01 Dec 2023 16:07:26 -0800 (PST) From: Jinrong Liang Extend the fixed counters test to verify that supported counters can actually be enabled in the control MSRs, that unsupported counters cannot, and that enabled counters actually count. Co-developed-by: Like Xu Signed-off-by: Like Xu Signed-off-by: Jinrong Liang [sean: fold into the rd/wr access test, massage changelog] Reviewed-by: Dapeng Mi Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/pmu_counters_test.c | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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 b07294af71a3..f5dedd112471 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -332,7 +332,6 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters vector = wrmsr_safe(msr, 0); GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); } - GUEST_DONE(); } static void guest_test_gp_counters(void) @@ -350,6 +349,7 @@ static void guest_test_gp_counters(void) base_msr = MSR_IA32_PERFCTR0; guest_rd_wr_counters(base_msr, MAX_NR_GP_COUNTERS, nr_gp_counters, 0); + GUEST_DONE(); } static void test_gp_counters(uint8_t pmu_version, uint64_t perf_capabilities, @@ -373,6 +373,7 @@ static void guest_test_fixed_counters(void) { uint64_t supported_bitmask = 0; uint8_t nr_fixed_counters = 0; + uint8_t i; /* Fixed counters require Architectural vPMU Version 2+. */ if (guest_get_pmu_version() >= 2) @@ -387,6 +388,34 @@ static void guest_test_fixed_counters(void) guest_rd_wr_counters(MSR_CORE_PERF_FIXED_CTR0, MAX_NR_FIXED_COUNTERS, nr_fixed_counters, supported_bitmask); + + for (i = 0; i < MAX_NR_FIXED_COUNTERS; i++) { + uint8_t vector; + uint64_t val; + + if (i >= nr_fixed_counters && !(supported_bitmask & BIT_ULL(i))) { + vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL, + FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL)); + __GUEST_ASSERT(vector == GP_VECTOR, + "Expected #GP for counter %u in FIXED_CTR_CTRL", i); + + vector = wrmsr_safe(MSR_CORE_PERF_GLOBAL_CTRL, + FIXED_PMC_GLOBAL_CTRL_ENABLE(i)); + __GUEST_ASSERT(vector == GP_VECTOR, + "Expected #GP for counter %u in PERF_GLOBAL_CTRL", i); + continue; + } + + wrmsr(MSR_CORE_PERF_FIXED_CTR0 + i, 0); + wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL)); + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, FIXED_PMC_GLOBAL_CTRL_ENABLE(i)); + __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES})); + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0); + val = rdmsr(MSR_CORE_PERF_FIXED_CTR0 + i); + + GUEST_ASSERT_NE(val, 0); + } + GUEST_DONE(); } static void test_fixed_counters(uint8_t pmu_version, uint64_t perf_capabilities, -- 2.43.0.rc2.451.g8631bc7472-goog