Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1405760rdb; Fri, 1 Dec 2023 16:06:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IG0GUgbKiIt0ZOzWs/vnrmPmPRrtZfnEjAOsBtoCXANL1E9nf6/wYqvFcfVjtxosb/15I2w X-Received: by 2002:a05:6a20:a10d:b0:18c:1af0:df70 with SMTP id q13-20020a056a20a10d00b0018c1af0df70mr447492pzk.6.1701475596134; Fri, 01 Dec 2023 16:06:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701475596; cv=none; d=google.com; s=arc-20160816; b=WoYSKtTsZTiE2ObeAipf1YetiYzlBdiLIIE0oWCkYU/PlU20YgJkKmVmRVXE5BD+a5 c4tCh1THT58sb5mLTOMLRGQZlAuspStgZ5seDDBm5wQ16gnnXJqZaEu68UNFG4SJpZdu 8JUozGk6QSnSJLFRIkG6zpecf5uCWclA7vMFOpEcyljU5xrliPZdUC5tmj/uYhQivLUz nlvBn7ZRdTYrF+IfD7JX9SFdbXJo0PScu/oaw6HPbK6nB+qW1rK+rTdpCefAKCu1InPW uOLmaiw4AY/SJLF1ik6YgmbDr9jAXfqo5c05Ud9rkW28zvHPZmqQ1Grs3AEIwanFP3Gx F8CQ== 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=zkC08Su0KAc7JEm525UecQDKz9wPksIT3+qHHEmH+bM=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=PQT7DeXO7Vq3g6CtLXYJjwYWjnulSTClAwacs45vUvtMbVuh/CEmHCOkXru6EzL9eM NOwMcoblZk154vtPyPijHjTC2/NuZjJizh5dA7dvwBLZYelbIF3/QukjUXJvGTfTHqPM wCIYIGlIXOvShPoMoVgLA3MUqa2M248wvqgRlg/nb5d1t0seUbEoxjqlc9u/bJpmzpJ+ s9BDlzNIsW5tZfimJRTUclPKzQ8woooKnxHfWl1Kh6vLpPYVwbG4IwYins8ErO/svjMH 5oHM848fiL4hE2tSZNvD2nhtFAKRvR4zDnX77/8idOgyzod2jU4453NC33dMnXyoiUMj Lnbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rZv7dd4Y; 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 b11-20020a655ccb000000b005c1ce3c9608si3965705pgt.756.2023.12.01.16.06.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 16:06:36 -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=rZv7dd4Y; 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 51AC5807E451; Fri, 1 Dec 2023 16:05:50 -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 S1442098AbjLBAF1 (ORCPT + 99 others); Fri, 1 Dec 2023 19:05:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235282AbjLBAFC (ORCPT ); Fri, 1 Dec 2023 19:05:02 -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 9351D171A for ; Fri, 1 Dec 2023 16:04:46 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ce16bc121aso48772037b3.1 for ; Fri, 01 Dec 2023 16:04:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701475485; x=1702080285; 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=zkC08Su0KAc7JEm525UecQDKz9wPksIT3+qHHEmH+bM=; b=rZv7dd4YzEsKWiIQFqU1PRebyeaCplqTJnfxI5MjlU0xrBe+0p6c/2uQ7GrWyv+XgM ye2067JcyRZSDPmQloLL70wb6sY+faknpZcH/Bv0Yu2K9x3YqbHmqFlpBHL77ncnqHfq HpPvWLLgU5uSCbsMWMF2vxwuc/6ZWzCw9E5Rx/Z5gFCfbYP08W1Gf7oSXMy2RmEjCQHx Fv0mS4CTqAIMTx5SnJK80xOBHuP6ekoqRvlitUq6DzbsmlQR4znzsx1nAAuvSvWCharz 1Py6Y6MpAa8WT38zhF8/lI0cdrba6XOis3wtXrbbX5Mh+S1sPrFRkQbkmD1DgFe9Dktv nZqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701475485; x=1702080285; 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=zkC08Su0KAc7JEm525UecQDKz9wPksIT3+qHHEmH+bM=; b=MDfjcaAWYVX7n/4IJPGzSG4gosbcDyxPnTHPk+YDcqjk7W6WP2CL/Y5Xgqn35IS/FS 4F05YV/NDzbOoxcEuL0p25RtJMduXvd/avkAO6Ja4vQubz1euXUoh3XOkIkCUZQywVrR bbPh2k4vmbD6pTPdCnPqbvQ51vlUWHjUMMawXPCHH2FT9cQZrpX6W9G9U3eaTKkJzE+X bqPMGeHC9GBVh2atNG3iZ0fgjPJT1Bu8P06hpMmp4XpOx9Gd4srvWDTb4XTE9qRTtUUV Vx2wCXzkveV+EuJZ6rr5nrAPRVpMLyZd/qt9kWefsyaJuIxJTT2shVCLUDFCPhLCAD8V k0gA== X-Gm-Message-State: AOJu0YzSTNkRl+sgzIQwXcpSR04nmgIG1AVouZWtlarFVdDgWEw675lN ogpM9GHYqrWDAwQrkLtihO47f7Xl38k= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:2710:b0:5d5:5183:ebd7 with SMTP id dy16-20020a05690c271000b005d55183ebd7mr88939ywb.7.1701475485636; Fri, 01 Dec 2023 16:04:45 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 1 Dec 2023 16:04:02 -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-14-seanjc@google.com> Subject: [PATCH v9 13/28] KVM: selftests: Extend {kvm,this}_pmu_has() to support fixed 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:05:50 -0800 (PST) Extend the kvm_x86_pmu_feature framework to allow querying for fixed counters via {kvm,this}_pmu_has(). Like architectural events, checking for a fixed counter annoyingly requires checking multiple CPUID fields, as a fixed counter exists if: FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i); Note, KVM currently doesn't actually support exposing fixed counters via the bitmask, but that will hopefully change sooner than later, and Intel's SDM explicitly "recommends" checking both the number of counters and the mask. Rename the intermedate "anti_feature" field to simply 'f' since the fixed counter bitmask (thankfully) doesn't have reversed polarity like the architectural events bitmask. Note, ideally the helpers would use BUILD_BUG_ON() to assert on the incoming register, but the expected usage in PMU tests can't guarantee the inputs are compile-time constants. Opportunistically define macros for all of the known architectural events and fixed counters. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 65 ++++++++++++++----- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 4f737d3b893c..92d4f8ecc730 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -282,24 +282,41 @@ struct kvm_x86_cpu_property { * that indicates the feature is _not_ supported, and a property that states * the length of the bit mask of unsupported features. A feature is supported * if the size of the bit mask is larger than the "unavailable" bit, and said - * bit is not set. + * bit is not set. Fixed counters also bizarre enumeration, but inverted from + * arch events for general purpose counters. Fixed counters are supported if a + * feature flag is set **OR** the total number of fixed counters is greater + * than index of the counter. * - * Wrap the "unavailable" feature to simplify checking whether or not a given - * architectural event is supported. + * Wrap the events for general purpose and fixed counters to simplify checking + * whether or not a given architectural event is supported. */ struct kvm_x86_pmu_feature { - struct kvm_x86_cpu_feature anti_feature; + struct kvm_x86_cpu_feature f; }; -#define KVM_X86_PMU_FEATURE(__bit) \ -({ \ - struct kvm_x86_pmu_feature feature = { \ - .anti_feature = KVM_X86_CPU_FEATURE(0xa, 0, EBX, __bit), \ - }; \ - \ - feature; \ +#define KVM_X86_PMU_FEATURE(__reg, __bit) \ +({ \ + struct kvm_x86_pmu_feature feature = { \ + .f = KVM_X86_CPU_FEATURE(0xa, 0, __reg, __bit), \ + }; \ + \ + kvm_static_assert(KVM_CPUID_##__reg == KVM_CPUID_EBX || \ + KVM_CPUID_##__reg == KVM_CPUID_ECX); \ + feature; \ }) -#define X86_PMU_FEATURE_BRANCH_INSNS_RETIRED KVM_X86_PMU_FEATURE(5) +#define X86_PMU_FEATURE_CPU_CYCLES KVM_X86_PMU_FEATURE(EBX, 0) +#define X86_PMU_FEATURE_INSNS_RETIRED KVM_X86_PMU_FEATURE(EBX, 1) +#define X86_PMU_FEATURE_REFERENCE_CYCLES KVM_X86_PMU_FEATURE(EBX, 2) +#define X86_PMU_FEATURE_LLC_REFERENCES KVM_X86_PMU_FEATURE(EBX, 3) +#define X86_PMU_FEATURE_LLC_MISSES KVM_X86_PMU_FEATURE(EBX, 4) +#define X86_PMU_FEATURE_BRANCH_INSNS_RETIRED KVM_X86_PMU_FEATURE(EBX, 5) +#define X86_PMU_FEATURE_BRANCHES_MISPREDICTED KVM_X86_PMU_FEATURE(EBX, 6) +#define X86_PMU_FEATURE_TOPDOWN_SLOTS KVM_X86_PMU_FEATURE(EBX, 7) + +#define X86_PMU_FEATURE_INSNS_RETIRED_FIXED KVM_X86_PMU_FEATURE(ECX, 0) +#define X86_PMU_FEATURE_CPU_CYCLES_FIXED KVM_X86_PMU_FEATURE(ECX, 1) +#define X86_PMU_FEATURE_REFERENCE_TSC_CYCLES_FIXED KVM_X86_PMU_FEATURE(ECX, 2) +#define X86_PMU_FEATURE_TOPDOWN_SLOTS_FIXED KVM_X86_PMU_FEATURE(ECX, 3) static inline unsigned int x86_family(unsigned int eax) { @@ -698,10 +715,16 @@ static __always_inline bool this_cpu_has_p(struct kvm_x86_cpu_property property) static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature) { - uint32_t nr_bits = this_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); + uint32_t nr_bits; - return nr_bits > feature.anti_feature.bit && - !this_cpu_has(feature.anti_feature); + if (feature.f.reg == KVM_CPUID_EBX) { + nr_bits = this_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); + return nr_bits > feature.f.bit && !this_cpu_has(feature.f); + } + + GUEST_ASSERT(feature.f.reg == KVM_CPUID_ECX); + nr_bits = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); + return nr_bits > feature.f.bit || this_cpu_has(feature.f); } static __always_inline uint64_t this_cpu_supported_xcr0(void) @@ -917,10 +940,16 @@ static __always_inline bool kvm_cpu_has_p(struct kvm_x86_cpu_property property) static inline bool kvm_pmu_has(struct kvm_x86_pmu_feature feature) { - uint32_t nr_bits = kvm_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); + uint32_t nr_bits; - return nr_bits > feature.anti_feature.bit && - !kvm_cpu_has(feature.anti_feature); + if (feature.f.reg == KVM_CPUID_EBX) { + nr_bits = kvm_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); + return nr_bits > feature.f.bit && !kvm_cpu_has(feature.f); + } + + TEST_ASSERT_EQ(feature.f.reg, KVM_CPUID_ECX); + nr_bits = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); + return nr_bits > feature.f.bit || kvm_cpu_has(feature.f); } static __always_inline uint64_t kvm_cpu_supported_xcr0(void) -- 2.43.0.rc2.451.g8631bc7472-goog