From nobody Tue Nov 10 15:59:56 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jPBDs1FjGz4wC9j for ; Tue, 10 Nov 2026 15:59:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jPBDr5nyCz4YBP for ; Tue, 10 Nov 2026 15:59:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794326396; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=EB2v2HYhtUGimerd7/NO0rov45fDpkOkBVlr1qfxp8o=; b=j5R7nDX9Fl8Mmamrl/KNtOuWeL3jj6yVMMhAAgi4ve6hRpDNdeCFihlFcC6gMVSdeAl+CS /q+paeYOgyV+0qjiu006Ot809rjXdLGg9DsZjw6vpS5QST26N1jIWG0XUaMSD12hhxVl4g Sa6CYrEWgGuA3XwFkpLvQ21SX/Q9HqP37hwDtQZCAlNpZ4YrqnmlCw35gl+gHA/+5GU1hD 2r7rWDUe3awko/RHc6uQEW+4PuDUjZMakdbnO0W0C2X8n5+TiwVNwtm+w/FmAjda9WlOQK XjbVfhU+YjS6smDZI6uDT4nHddakmg65fLGl8w8X0ZW1UzZU1fWF1YuCB3dFTg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794326396; b=Vg4l++5rAhJfrFuUv1Wdz/xkBYWdEHVhBP+00p/C6iTuMgeIyggCoZMmIjcAi21dikspWI 4vlvwMixHPqDzj8QiMWHj3BiGeyQC6kyjfUo21OVgW+5m8TKXHYDxSNoIFBejYUUJpMCLV Rnvrgz16lNLm3mwewxuI/PhiMCptW4FnQja841bxS/ljvtq0v18oE9k87Whsm+pot0ruwB 76dxtQnC0R5Cn2Vhfci0NsnY7qqF/z8Pz9uA2oqLU7csTjuTXmLqgXDIK0ArAzMan1XOtv 0KudyNKotpInZtB+lUsy1chKRsRTtjSwBbwEjkFLSEDp15cCf8YKHEOb6F9Tvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794326396; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=EB2v2HYhtUGimerd7/NO0rov45fDpkOkBVlr1qfxp8o=; b=aDjwiiB/0r7UGWh5vclIRc1yGdzaEDzJ0cqqoA9Y9bEsMLcuzV/ZrDH/Exqzx9JY6/mZE9 K5+DE/SXK4Nq6tk6WS3KF9FRVqWEWw1udGLcwLgtshXp+RnSjQSIUHRiaplVZ3lSxXJvoh dEYnipoAYb/jVxGu58eCjwu6ApH2W/4itKJAguuQ1yz6qR1KF9Sj7C+CwspZyMwTNE0rE6 MyEMyBKyk3faSfWsGsxyTd5ilA8ht70hSIz45yioaVYENia+a/djfzL4VnSVJmSJgL8zCf 953mLRw2LZSQT5yC50znNZ6GxlC5ATY7jRq3hXfOHpMtoViESj3Zq/OVHnt3iw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jPBDr3b4BzbkL for ; Tue, 10 Nov 2026 15:59:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 87bb by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 10 Nov 2026 15:59:56 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Osvaldo Janeri Filho From: Mitchell Horne Subject: git: c5400dc5421c - main - libpmc: Strip leading zeros from the cpuid model List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c5400dc5421cce1be7152f401dc09725036d0963 Auto-Submitted: auto-generated Date: Tue, 10 Nov 2026 15:59:56 +0000 Message-Id: <6af33f7c.87bb.173878e8@gitrepo.freebsd.org> The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=c5400dc5421cce1be7152f401dc09725036d0963 commit c5400dc5421cce1be7152f401dc09725036d0963 Author: Osvaldo Janeri Filho AuthorDate: 2026-11-10 15:50:57 +0000 Commit: Mitchell Horne CommitDate: 2026-11-10 15:59:54 +0000 libpmc: Strip leading zeros from the cpuid model Upstream's mapfile.csv lists models without leading zeros, so entries for models below 10h had to be fixed up by hand on every import. Strip them before matching and use the entries as imported. Add tests to validate the functionality. Reviewed by: mhorne, Ali Mashtizadeh Sponsored by: Advanced Micro Devices, Inc. MFC after: 1 week Signed-off-by: Osvaldo Janeri Filho Differential Revision: https://reviews.freebsd.org/D60790 --- lib/libpmc/libpmc_pmu_util.c | 30 ++++++++++++ lib/libpmc/libpmcinternal.h | 1 + lib/libpmc/pmu-events/arch/x86/mapfile.csv | 8 +-- tests/sys/pmc/pmc_mapfile_test.c | 79 ++++++++++++++++++++++++++++-- 4 files changed, 111 insertions(+), 7 deletions(-) diff --git a/lib/libpmc/libpmc_pmu_util.c b/lib/libpmc/libpmc_pmu_util.c index 6472dd8311de..53d9d8766add 100644 --- a/lib/libpmc/libpmc_pmu_util.c +++ b/lib/libpmc/libpmc_pmu_util.c @@ -49,6 +49,8 @@ #include #include #include + +#include "libpmcinternal.h" #include "pmu-events/pmu-events.h" struct pmu_alias { @@ -251,6 +253,33 @@ struct pmu_event_desc { uint8_t ped_pebs; }; +/* + * hwpmc zero-pads the model field of kern.hwpmc.cpuid for AMD/Hygon x86 CPUs + * -- see the "%02X" in the snprintf() calls in hwpmc_amd.c -- so e.g. Zen 5 + * model 2h is reported as "AuthenticAMD-26-02-". + * pmu-events/arch/x86/mapfile.csv lists those low models without the leading + * zero, so it has to be stripped before matching. + * + * This is scoped tightly to the "--[-]" + * shape that only AuthenticAMD/HygonGenuine strings use; any other cpuid + * string can never reach the strip below and comes back unchanged. Notably, + * arm64's bare "0x" (hwpmc_arm64.c) and powerpc's bare "%08x" PVR + * (hwpmc_powerpc.c). + */ +void +_pmc_cpuid_strip_model_zero(char *buf) +{ + char *p; + + if (strncmp(buf, "AuthenticAMD-", sizeof("AuthenticAMD-") - 1) != 0 && + strncmp(buf, "HygonGenuine-", sizeof("HygonGenuine-") - 1) != 0) + return; + if ((p = strchr(buf, '-')) == NULL || (p = strchr(p + 1, '-')) == NULL) + return; + while (p[1] == '0' && p[2] != '-' && p[2] != '\0') + memmove(p + 1, p + 2, strlen(p + 1)); +} + static const struct pmu_events_map * pmu_events_map_get(const char *cpuid) { @@ -268,6 +297,7 @@ pmu_events_map_get(const char *cpuid) (void *)NULL, 0) == -1) return (NULL); } + _pmc_cpuid_strip_model_zero(buf); for (pme = pmu_events_map; pme->cpuid != NULL; pme++) { if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) { printf("regex '%s' failed to compile, ignoring\n", diff --git a/lib/libpmc/libpmcinternal.h b/lib/libpmc/libpmcinternal.h index 960843b3aea8..7de9d540027c 100644 --- a/lib/libpmc/libpmcinternal.h +++ b/lib/libpmc/libpmcinternal.h @@ -33,5 +33,6 @@ * Prototypes. */ const char *_pmc_name_of_event(enum pmc_event _ev, enum pmc_cputype _cpu); +void _pmc_cpuid_strip_model_zero(char *_buf); #endif /* LIBPMC_INTERNAL_H */ diff --git a/lib/libpmc/pmu-events/arch/x86/mapfile.csv b/lib/libpmc/pmu-events/arch/x86/mapfile.csv index 05480113bd1c..0b1a8b38dd28 100644 --- a/lib/libpmc/pmu-events/arch/x86/mapfile.csv +++ b/lib/libpmc/pmu-events/arch/x86/mapfile.csv @@ -55,10 +55,10 @@ GenuineIntel-6-BE,v1,alderlaken,core GenuineIntel-6-AA,v1,meteorlake,core GenuineIntel-6-AC,v1,meteorlake,core GenuineIntel-6-B5,v1,meteorlake,core -AuthenticAMD-23-[012][0-9A-F],v2,amdzen1,core +AuthenticAMD-23-([12][0-9A-F]|[0-9A-F]),v2,amdzen1,core AuthenticAMD-23-[[:xdigit:]]+,v1,amdzen2,core -AuthenticAMD-25-[0245][[:xdigit:]],v1,amdzen3,core +AuthenticAMD-25-([245][[:xdigit:]]|[[:xdigit:]]),v1,amdzen3,core AuthenticAMD-25-[[:xdigit:]]+,v1,amdzen4,core -AuthenticAMD-26-[012467][[:xdigit:]],v1,amdzen5,core +AuthenticAMD-26-([12467][[:xdigit:]]|[[:xdigit:]]),v1,amdzen5,core AuthenticAMD-26-[[:xdigit:]]+,v1,amdzen6,core -HygonGenuine-24-00,v2,amdzen1,core +HygonGenuine-24-0,v2,amdzen1,core diff --git a/tests/sys/pmc/pmc_mapfile_test.c b/tests/sys/pmc/pmc_mapfile_test.c index 91a9d6e84bf7..0474919903a1 100644 --- a/tests/sys/pmc/pmc_mapfile_test.c +++ b/tests/sys/pmc/pmc_mapfile_test.c @@ -9,16 +9,33 @@ /* * libpmc picks its pmu-events table by matching kern.hwpmc.cpuid against * mapfile.csv. hwpmc prints the model with two digits, so Turin (Family - * 1Ah, model 02h) is "AuthenticAMD-26-02-". For Zen 5 and Zen 6 - * cpuid strings, look up an event that only one of amdzen5 and amdzen6 has. - * No PMC hardware is needed. + * 1Ah, model 02h) is "AuthenticAMD-26-02-". For each cpuid + * string, look up an event that only the expected table has. No PMC + * hardware is needed. */ #include +#include + #include #include +/* + * _pmc_cpuid_strip_model_zero() is an internal libpmc interface that + * pmu_events_map_get() uses to undo hwpmc's zero-padding of the AMD/Hygon + * model field before matching it against mapfile.csv. + * + * It is not part of the installed pmc.h, so declare it here to exercise it + * directly: this is the only way to check, byte for byte, that cpuid strings + * outside its AMD/Hygon vendor-prefixed shape come back completely unmodified. + */ +void _pmc_cpuid_strip_model_zero(char *); + +#define ZEN1_ONLY_EVENT "bp_l1_tlb_miss_l2_miss" +#define ZEN2_ONLY_EVENT "bp_l1_tlb_miss_l2_tlb_miss" +#define ZEN3_ONLY_EVENT "bp_l1_btb_correct" +#define ZEN4_ONLY_EVENT "bp_l1_tlb_fetch_hit.all" #define ZEN5_ONLY_EVENT "bp_l2_btb_correct" #define ZEN6_ONLY_EVENT "bp_fe_redir.all" @@ -78,10 +95,66 @@ ATF_TC_BODY(zen6_models, tc) "amdzen6"); } +ATF_TC(low_models); +ATF_TC_HEAD(low_models, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Models below 10h, with or without the leading zero"); +} +ATF_TC_BODY(low_models, tc) +{ + check_table("AuthenticAMD-23-01-0", ZEN1_ONLY_EVENT, ZEN2_ONLY_EVENT, + "amdzen1"); + check_table("HygonGenuine-24-00-0", ZEN1_ONLY_EVENT, ZEN2_ONLY_EVENT, + "amdzen1"); + check_table("AuthenticAMD-25-01-0", ZEN3_ONLY_EVENT, ZEN4_ONLY_EVENT, + "amdzen3"); + check_table("AuthenticAMD-26-2-0", ZEN5_ONLY_EVENT, ZEN6_ONLY_EVENT, + "amdzen5"); +} + +/* + * Only AuthenticAMD/HygonGenuine cpuid strings use the + * "--[-]" shape that needs the model's + * leading zero stripped. Every other cpuid shape must come back unchanged, in + * particular arm64's bare "0x" and powerpc's bare "%08x" PVR. Also, + * check a string that merely happens to contain two dashes and a zero, to + * confirm the vendor-prefix check (not "has a dash") is what actually gates + * the strip. + */ +static const char *const non_amd_cpuids[] = { + "0x00000000410fd020", /* arm64 MIDR, cortex-a34 */ + "0x00000000410fd030", /* arm64 MIDR, cortex-a53 */ + "00390202", /* powerpc PVR */ + "NotAVendor-1-00-1", /* two dashes, but not AuthenticAMD/HygonGenuine */ +}; + +ATF_TC(non_amd_cpuid_unchanged); +ATF_TC_HEAD(non_amd_cpuid_unchanged, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Non-AMD/Hygon cpuid strings are never touched by the " + "model zero-strip, e.g. arm64's bare 0x"); +} +ATF_TC_BODY(non_amd_cpuid_unchanged, tc) +{ + char buf[64]; + size_t i; + + for (i = 0; i < nitems(non_amd_cpuids); i++) { + strlcpy(buf, non_amd_cpuids[i], sizeof(buf)); + _pmc_cpuid_strip_model_zero(buf); + ATF_CHECK_MSG(strcmp(buf, non_amd_cpuids[i]) == 0, + "%s was modified to %s", non_amd_cpuids[i], buf); + } +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, zen5_models); ATF_TP_ADD_TC(tp, zen6_models); + ATF_TP_ADD_TC(tp, low_models); + ATF_TP_ADD_TC(tp, non_amd_cpuid_unchanged); return (atf_no_error()); }