Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp818908rdf; Fri, 3 Nov 2023 17:03:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEbT7B0UqlhQ4R5RmPvXZfiglEybAjeYQdNvbvcXYn46qtpDqLVAoVlgIqZJ3oqsCT5yTou X-Received: by 2002:a05:6870:1309:b0:1ea:7bd1:c495 with SMTP id 9-20020a056870130900b001ea7bd1c495mr22023961oab.27.1699056205933; Fri, 03 Nov 2023 17:03:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699056205; cv=none; d=google.com; s=arc-20160816; b=rVBNps17Xd65woWJTCd5+bnmzPie/Q+y2v+867hvdOHwpuExTbZ12u0gMeWsJgCmr4 BWapKKPZNwsTjM9vxyHLpqU6gzTDvEqFy069NQK2pCLdV+2OBBMHCiAAXCIK4KQegDjQ vyxqDFfwd4IKJOO846MkDezW3UIY/uTTrCEox0VasNz8WXpZJFSU4EGpyKSPqeStEV6U dN+ri7UUxHiiKpSgGFp480A9YGvHX+2rNQRbOvUR6gKuDQj1qR9e2TbgqVHhXq66szzP yt8mz9szHroE5jsVqjECls7DBzjCmdIdVDRXTsLAE0aS1gqVRD6bmB8l4eYboq7pZBO6 A8uw== 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=L3Ph1WGBEgHW7QvmfHelm4EacJM/EhO7PSNkxK0HmCo=; fh=z/UQYx9FUD9Bz9JyYrmi2ijAScp29gktKr8lejSUuXU=; b=AJZW88M/gV+ONRQ0Pai5YwCsuoWW+GgjsAbC+uQKlKCHzZpOxIfaNUFIv497eXXMCr SXicFtGXgz0GerbJ8GqDDBERtu36aub5tHf16UDSlzV2l83zoRpW8NiBzuWG+YUZEG05 w+KbtRrqYJqIUb1knKtXRdcij4BvuK6br8X/2d1KW4v1Ti5R4itnwNbsBWBDpDr+F4lw I1l+2WzXEorDxFHFIGxuU66E6dqB5nbYxkpzmEYt7KzgJ/sNwQ0X7G1VAonQkEAXZjMZ tKeK9wBlX8zOW+4Fx6lbaqQPCQhLYjLP9CCtczxl6vVvzckraI/m0B8BS7LOzyX3gJXC QCtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DccDeXfi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id g5-20020a656cc5000000b00565ecee8793si2611407pgw.875.2023.11.03.17.03.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 17:03:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DccDeXfi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (Postfix) with ESMTP id 8BEC98207319; Fri, 3 Nov 2023 17:03:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231740AbjKDADK (ORCPT + 99 others); Fri, 3 Nov 2023 20:03:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231226AbjKDAC7 (ORCPT ); Fri, 3 Nov 2023 20:02:59 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96F4619D for ; Fri, 3 Nov 2023 17:02:56 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1cc29f3afe0so20466135ad.2 for ; Fri, 03 Nov 2023 17:02:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699056176; x=1699660976; 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=L3Ph1WGBEgHW7QvmfHelm4EacJM/EhO7PSNkxK0HmCo=; b=DccDeXfiDvfv1fBWEzY45WjEbiOWdrzZaTNQV4f0F7uJwEyYRbB+VVZf9wIn1IeCTE iUVhrrFl/42ZXS4ylZj3+rYQbFEFxo1SVJWWPyw6CfR3I1AzEgsH+SlCQqrCiYl6GZT3 MYnHtkO1Fn1LmgGtqObBctRZ7WRfIotrQqHt7wIFyJ6lvVudHnoKz/0402GO07m1C12a sHT3RmqN+J7A0OQqLbzMuwW8AMKjv8b2iU4mDN4lZYLYNMqS72px5WI4hPFrDV50O/mF YQLqOd7nEQvgbLuV+RXSkqHYfMgwkEowuSi7XSedAqeNJUx291KThC4BoNpi5+JXYlXc jLvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699056176; x=1699660976; 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=L3Ph1WGBEgHW7QvmfHelm4EacJM/EhO7PSNkxK0HmCo=; b=s4nVTl8k1N+503Y/ykA5GXwm3hMrunDeloZYWHUEwb0pSyEg6AUNLO/S/jEOtLeGJv ESZFuJDX5r46Ce1RSVaN9Eijzok+gjHQiMGWNS2TLQmcgUJ39rJVRDP+YKTkB6S2Fq9Y TlHPe+GeC9kY/RL/TwovOQRvoijGkmNJW1yDDTJbQmGHkvB0UGh2GubamdKbhk6TCo3i SDeZAjkkdQj6VS8R/87/4pfPbwvvIo+w6cP25b5l1laJnRwqpzLUd0tMrzIgVqNo3LEi NvR+mC7wgosr9tASfSITQVojthb+GOY+4AQdjG57G/6UUaYHC0n8hUHNvgn8sFaRTIQC IVFA== X-Gm-Message-State: AOJu0Yz/0iFiD/iTBuw7ROlUUnxeZ5/2L5k8k0tcWvrClhaZcNRtfFc3 aDcNuQlAyEgsgzqDwuu3wpYb+1fyupA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d301:b0:1cc:32c6:e5d4 with SMTP id b1-20020a170902d30100b001cc32c6e5d4mr312291plc.6.1699056176128; Fri, 03 Nov 2023 17:02:56 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 3 Nov 2023 17:02:26 -0700 In-Reply-To: <20231104000239.367005-1-seanjc@google.com> Mime-Version: 1.0 References: <20231104000239.367005-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231104000239.367005-9-seanjc@google.com> Subject: [PATCH v6 08/20] 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 , Jinrong Liang , Like Xu , Jim Mattson , Aaron Lewis 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 fry.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 (fry.vger.email [0.0.0.0]); Fri, 03 Nov 2023 17:03:20 -0700 (PDT) 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 architectural events and fixed counters that KVM currently supports. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 63 +++++++++++++------ 1 file changed, 45 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 2d9771151dd9..b103c462701b 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -281,24 +281,39 @@ 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_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_CYCLES_FIXED KVM_X86_PMU_FEATURE(ECX, 2) static inline unsigned int x86_family(unsigned int eax) { @@ -697,10 +712,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) @@ -916,10 +937,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.42.0.869.gea05f2083d-goog