git: 546a76463123 - main - tests/sys/pmc: add pidtrack, callchain, pmc(8), RAPL and AMD tests

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 16:14:25 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=546a7646312352237e49c3a7144e967d2e89b044

commit 546a7646312352237e49c3a7144e967d2e89b044
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-10 16:00:39 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-10 16:13:35 +0000

    tests/sys/pmc: add pidtrack, callchain, pmc(8), RAPL and AMD tests
    
    Add to the suite of ATF tests for hwpmc(4).  Vendor-neutral tests go in
    tests/sys/pmc; tests that depend on AMD hardware interfaces go in
    tests/sys/pmc/amd, which is only built on amd64.
    
    tests/sys/pmc:
      pmc_pidtrack_test      process-mode PMCs follow their PID: a live
                             self-read counts the process' own work, an idle
                             process sharing the CPU with a busy one does not
                             pick up its work, and the count survives a
                             context switch
      pmc_callchain_ur_test  pmcstat -U collects user callchains through the
                             PMC_UR ring; regression test for the KASSERTs
                             fixed in rGaf3929c5152b and rG66118c3f1011
      pmc_cli_test           pmc(8): missing log, list-events, record/info
                             round trip with the "instructions" alias
      pmcstat_tsc_test       (amd64) tsc_freq in the initlog record, TSC
                             column of pmcstat -R, tsc in pmc filter -j,
                             pmcstat -G callgraph
      pmc_rapl_test          (amd64) RAPL class: 64-bit counters, system
                             mode only, PKG and CORES energy increase under
                             load
    
    tests/sys/pmc/amd:
      pmu_detect_test        K8 class with 48-bit counters, at least six core
                             counters, TSC class alongside it
      pmu_event_test         pmu-events lookup and EventSelect/UnitMask/OS/USR
                             encoding checked against the amdzen1..amdzen6
                             tables; ex_ret_instr counts a known loop
      pmu_event_neg_test     raw PMCALLOCATE rejects an otherwise valid config
                             word with HOST, GUEST, HOST|GUEST, reserved bit
                             44 or PRECISERETIRE without PMC_CAP_PRECISE
                             (pmc_exterr_test covers an all-ones config word)
      pmu_mux_test           every core counter that CPUID reports can be
                             held at once, one more is refused, a released
                             one is reusable
      rapl_domains_test      AMD RAPL has at most PKG and CORES rows and no
                             DRAM domain
      pmc_frontend_test      pmc(8) frontend study on IBS fetch samples
      amd_pmc_test_common.h  CPUID-based Zen generation detection and helpers
    
    Reviewed by:    mhorne
    Discussed with: netchild
    MFC after:      1 month
    Sponsored by:   Advanced Micro Devices, Inc.
    Signed-off-by:  Osvaldo Janeri Filho <ojanerif@amd.com>
    Differential Revision:  https://reviews.freebsd.org/D59549
---
 etc/mtree/BSD.tests.dist                |   2 +
 tests/sys/pmc/Makefile                  |  21 +-
 tests/sys/pmc/amd/Makefile              |  18 ++
 tests/sys/pmc/amd/amd_pmc_test_common.h | 316 +++++++++++++++++++++
 tests/sys/pmc/amd/pmc_frontend_test.sh  |  54 ++++
 tests/sys/pmc/amd/pmu_detect_test.c     | 133 +++++++++
 tests/sys/pmc/amd/pmu_event_neg_test.c  | 254 +++++++++++++++++
 tests/sys/pmc/amd/pmu_event_test.c      | 481 ++++++++++++++++++++++++++++++++
 tests/sys/pmc/amd/pmu_mux_test.c        | 280 +++++++++++++++++++
 tests/sys/pmc/amd/rapl_domains_test.c   | 128 +++++++++
 tests/sys/pmc/pmc_callchain_ur_test.sh  | 100 +++++++
 tests/sys/pmc/pmc_cli_test.sh           | 102 +++++++
 tests/sys/pmc/pmc_pidtrack_test.c       | 399 ++++++++++++++++++++++++++
 tests/sys/pmc/pmc_rapl_test.c           | 260 +++++++++++++++++
 tests/sys/pmc/pmcstat_tsc_test.sh       | 122 ++++++++
 15 files changed, 2667 insertions(+), 3 deletions(-)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index 8c6d2dd458ad..3ec8bd0f6942 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -988,6 +988,8 @@
             ..
         ..
         pmc
+            amd
+            ..
         ..
         posixshm
         ..
diff --git a/tests/sys/pmc/Makefile b/tests/sys/pmc/Makefile
index c44932483c9a..32ba88d1f611 100644
--- a/tests/sys/pmc/Makefile
+++ b/tests/sys/pmc/Makefile
@@ -4,15 +4,30 @@ ATF_TESTS_C+=	pmc_api_test
 ATF_TESTS_C+=	pmc_credexec_test
 ATF_TESTS_C+=	pmc_detach_test
 ATF_TESTS_C+=	pmc_exec_test
+ATF_TESTS_C+=	pmc_lifecycle_test
+ATF_TESTS_C+=	pmc_log_test
+ATF_TESTS_C+=	pmc_pidtrack_test
+ATF_TESTS_C+=	pmc_wrap_test
+
+ATF_TESTS_SH+=	pmc_callchain_ur_test
+ATF_TESTS_SH+=	pmc_cli_test
+
 .if ${MACHINE_ARCH} == "amd64"
 ATF_TESTS_C+=	pmc_exterr_test
 ATF_TESTS_C+=	pmc_mapfile_test
+ATF_TESTS_C+=	pmc_rapl_test
+
+ATF_TESTS_SH+=	pmcstat_tsc_test
+
+TESTS_SUBDIRS+=	amd
 .endif
-ATF_TESTS_C+=	pmc_lifecycle_test
-ATF_TESTS_C+=	pmc_log_test
-ATF_TESTS_C+=	pmc_wrap_test
 
 TEST_METADATA.pmc_exterr_test+=	required_user="root"
+TEST_METADATA.pmc_callchain_ur_test+=	is_exclusive="true"
+TEST_METADATA.pmc_cli_test+=	is_exclusive="true"
+TEST_METADATA.pmc_pidtrack_test+=	is_exclusive="true"
+TEST_METADATA.pmc_rapl_test+=	is_exclusive="true"
+TEST_METADATA.pmcstat_tsc_test+=	required_kmods="hwpmc"
 
 LIBADD+=	pmc
 LIBADD.pmc_detach_test+=	pthread
diff --git a/tests/sys/pmc/amd/Makefile b/tests/sys/pmc/amd/Makefile
new file mode 100644
index 000000000000..486cf61589ea
--- /dev/null
+++ b/tests/sys/pmc/amd/Makefile
@@ -0,0 +1,18 @@
+TESTSDIR=	${TESTSBASE}/sys/pmc/amd
+
+ATF_TESTS_C+=	pmu_detect_test
+ATF_TESTS_C+=	pmu_event_neg_test
+ATF_TESTS_C+=	pmu_event_test
+ATF_TESTS_C+=	pmu_mux_test
+ATF_TESTS_C+=	rapl_domains_test
+ATF_TESTS_SH+=	pmc_frontend_test
+
+# These need the counters of cpu0 to themselves.
+TEST_METADATA.pmu_event_neg_test+=	is_exclusive="true"
+TEST_METADATA.pmu_event_test+=		is_exclusive="true"
+TEST_METADATA.pmu_mux_test+=		is_exclusive="true"
+TEST_METADATA.rapl_domains_test+=	is_exclusive="true"
+
+LIBADD+=	pmc
+
+.include <bsd.test.mk>
diff --git a/tests/sys/pmc/amd/amd_pmc_test_common.h b/tests/sys/pmc/amd/amd_pmc_test_common.h
new file mode 100644
index 000000000000..543d5afe3a75
--- /dev/null
+++ b/tests/sys/pmc/amd/amd_pmc_test_common.h
@@ -0,0 +1,316 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Author: Davi C.A <davi.chavesazevedo@amd.com>
+ */
+
+/*
+ * Common helpers for the AMD hwpmc tests.
+ */
+
+#ifndef _AMD_PMC_TEST_COMMON_H_
+#define	_AMD_PMC_TEST_COMMON_H_
+
+#include <sys/param.h>
+#include <sys/cpuset.h>
+#include <machine/cpufunc.h>
+#include <machine/specialreg.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <pmc.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define	AMD_CPUID_VENDOR_EBX	0x68747541U	/* "Auth" */
+#define	AMD_CPUID_VENDOR_EDX	0x69746e65U	/* "enti" */
+#define	AMD_CPUID_VENDOR_ECX	0x444d4163U	/* "cAMD" */
+
+/* Ordered oldest to newest so that generations can be compared. */
+enum amd_zen {
+	AMD_ZEN_PRE_ZEN = 0,
+	AMD_ZEN_1,
+	AMD_ZEN_PLUS,
+	AMD_ZEN_2,
+	AMD_ZEN_3,
+	AMD_ZEN_3_PLUS,
+	AMD_ZEN_4,
+	AMD_ZEN_5,
+	AMD_ZEN_6,
+	AMD_ZEN_FUTURE,
+	AMD_ZEN_UNKNOWN
+};
+
+struct amd_test_cpu {
+	uint32_t	family;
+	uint32_t	model;
+	enum amd_zen	zen;
+};
+
+static inline const char *
+amd_test_zen_name(enum amd_zen zen)
+{
+	switch (zen) {
+	case AMD_ZEN_PRE_ZEN:
+		return ("pre-Zen");
+	case AMD_ZEN_1:
+		return ("Zen 1");
+	case AMD_ZEN_PLUS:
+		return ("Zen+");
+	case AMD_ZEN_2:
+		return ("Zen 2");
+	case AMD_ZEN_3:
+		return ("Zen 3");
+	case AMD_ZEN_3_PLUS:
+		return ("Zen 3+");
+	case AMD_ZEN_4:
+		return ("Zen 4");
+	case AMD_ZEN_5:
+		return ("Zen 5");
+	case AMD_ZEN_6:
+		return ("Zen 6");
+	case AMD_ZEN_FUTURE:
+		return ("post-Zen 6");
+	default:
+		return ("unknown");
+	}
+}
+
+static inline enum amd_zen
+amd_test_map_zen(uint32_t family, uint32_t model)
+{
+	if (family < 0x17)
+		return (AMD_ZEN_PRE_ZEN);
+	if (family > 0x1a)
+		return (AMD_ZEN_FUTURE);
+
+	switch (family) {
+	case 0x17:
+		if (model >= 0x08 && model <= 0x0f)	/* Pinnacle Ridge */
+			return (AMD_ZEN_PLUS);
+		if ((model >= 0x01 && model <= 0x07) ||	/* Summit Ridge */
+		    (model >= 0x10 && model <= 0x17) ||	/* Raven Ridge */
+		    (model >= 0x20 && model <= 0x2f))	/* Dali */
+			return (AMD_ZEN_1);
+		if (model >= 0x18 && model <= 0x1f)	/* Picasso */
+			return (AMD_ZEN_PLUS);
+		if ((model >= 0x31 && model <= 0x3f) ||	/* Rome */
+		    (model >= 0x60 && model <= 0x6f) ||	/* Renoir */
+		    (model >= 0x71 && model <= 0x7f) ||	/* Matisse */
+		    (model >= 0x90 && model <= 0x9f) ||	/* Van Gogh */
+		    (model >= 0xa0 && model <= 0xaf))	/* Mendocino */
+			return (AMD_ZEN_2);
+		break;
+	case 0x19:
+		if ((model >= 0x10 && model <= 0x1f) ||	/* Genoa, Storm Peak */
+		    (model >= 0x60 && model <= 0x7f) ||	/* Raphael, Phoenix */
+		    (model >= 0xa0 && model <= 0xaf))	/* Bergamo, Siena */
+			return (AMD_ZEN_4);
+		if (model >= 0x40 && model <= 0x4f)	/* Rembrandt */
+			return (AMD_ZEN_3_PLUS);
+		if (model <= 0x0f ||				/* Milan */
+		    (model >= 0x20 && model <= 0x2f) ||	/* Vermeer */
+		    (model >= 0x50 && model <= 0x5f))	/* Cezanne */
+			return (AMD_ZEN_3);
+		break;
+	case 0x1a:
+		if ((model >= 0x50 && model <= 0x5f) ||	/* Medusa */
+		    (model >= 0x80 && model <= 0xaf) ||
+		    (model >= 0xc0 && model <= 0xcf) ||
+		    (model >= 0xd8 && model <= 0xef))
+			return (AMD_ZEN_6);
+		/* Turin, Strix, Krackan, Fire Range */
+		if (model <= 0x2f || (model >= 0x40 && model <= 0x4f) ||
+		    (model >= 0x60 && model <= 0x7f) ||
+		    (model >= 0xd0 && model <= 0xd7))
+			return (AMD_ZEN_5);
+		break;
+	}
+
+	return (AMD_ZEN_UNKNOWN);
+}
+
+static inline bool
+amd_test_is_amd(void)
+{
+	u_int regs[4];
+
+	do_cpuid(0, regs);
+	return (regs[1] == AMD_CPUID_VENDOR_EBX &&
+	    regs[3] == AMD_CPUID_VENDOR_EDX &&
+	    regs[2] == AMD_CPUID_VENDOR_ECX);
+}
+
+static inline void
+amd_test_skip_unless_amd(void)
+{
+	if (!amd_test_is_amd())
+		atf_tc_skip("CPU vendor is not AuthenticAMD");
+}
+
+/* Skip unless this is an AMD CPU of a known Zen generation. */
+static inline void
+amd_test_skip_unless_zen(struct amd_test_cpu *cpu)
+{
+	u_int regs[4];
+
+	amd_test_skip_unless_amd();
+
+	do_cpuid(1, regs);
+	cpu->family = (regs[0] >> 8) & 0x0f;
+	cpu->model = (regs[0] >> 4) & 0x0f;
+	if (cpu->family == 0x0f) {
+		cpu->family += (regs[0] >> 20) & 0xff;
+		cpu->model |= ((regs[0] >> 16) & 0x0f) << 4;
+	}
+	cpu->zen = amd_test_map_zen(cpu->family, cpu->model);
+
+	if (cpu->zen == AMD_ZEN_PRE_ZEN)
+		atf_tc_skip("AMD family %02xh predates Zen", cpu->family);
+	if (cpu->zen == AMD_ZEN_FUTURE || cpu->zen == AMD_ZEN_UNKNOWN)
+		atf_tc_skip("AMD family %02xh model %02xh is not in the "
+		    "Zen map", cpu->family, cpu->model);
+}
+
+/* Without PerfCtrExtCore (e.g. some guests) hwpmc exposes 4 core counters. */
+static inline void
+amd_test_skip_unless_perfctr_extcore(void)
+{
+	u_int regs[4];
+
+	do_cpuid(0x80000001, regs);
+	if ((regs[2] & AMDID2_PCXC) == 0)
+		atf_tc_skip("CPU lacks PerfCtrExtCore; only 4 core counters");
+}
+
+/*
+ * The number of core counters, computed as hwpmc_amd.c does: Fn8000_0022
+ * EBX[3:0] when that leaf reports counters, otherwise 6 with PerfCtrExtCore
+ * and 4 without.
+ */
+static inline int
+amd_test_core_counters(void)
+{
+	u_int regs[4];
+	int n;
+
+	do_cpuid(0x80000001, regs);
+	n = (regs[2] & AMDID2_PCXC) != 0 ? 6 : 4;
+	do_cpuid(0x80000000, regs);
+	if (regs[0] >= 0x80000022) {
+		do_cpuid(0x80000022, regs);
+		if (regs[1] != 0)
+			n = regs[1] & 0x0f;
+	}
+	return (n);
+}
+
+static inline void
+amd_test_skip_unless_hwpmc(void)
+{
+	if (pmc_init() < 0)
+		atf_tc_skip("pmc_init() failed: %s", strerror(errno));
+}
+
+/* Bind the calling thread to cpu0, where the system-mode PMCs count. */
+static inline void
+amd_test_bind_cpu0(void)
+{
+	cpuset_t mask;
+
+	CPU_ZERO(&mask);
+	CPU_SET(0, &mask);
+	if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+	    sizeof(mask), &mask) != 0)
+		atf_tc_skip("cannot bind to cpu0: %s", strerror(errno));
+}
+
+static inline const struct pmc_classinfo *
+amd_test_find_classinfo(const struct pmc_cpuinfo *cpuinfo, enum pmc_class cl)
+{
+	uint32_t i;
+
+	for (i = 0; i < cpuinfo->pm_nclass; i++)
+		if (cpuinfo->pm_classes[i].pm_class == cl)
+			return (&cpuinfo->pm_classes[i]);
+
+	return (NULL);
+}
+
+static inline size_t
+amd_test_count_pmc_rows_with_prefix(int cpu, const char *prefix)
+{
+	struct pmc_pmcinfo *pmcinfo;
+	size_t count;
+	int i, npmc;
+
+	count = 0;
+	ATF_REQUIRE_MSG(pmc_pmcinfo(cpu, &pmcinfo) == 0,
+	    "pmc_pmcinfo(%d) failed: %s", cpu, strerror(errno));
+
+	npmc = pmc_npmc(cpu);
+	if (npmc < 0) {
+		free(pmcinfo);
+		atf_tc_fail("pmc_npmc(%d) failed: %s", cpu, strerror(errno));
+	}
+
+	for (i = 0; i < npmc; i++) {
+		if (strncmp(pmcinfo->pm_pmcs[i].pm_name, prefix,
+		    strlen(prefix)) == 0)
+			count++;
+	}
+
+	free(pmcinfo);
+	return (count);
+}
+
+/*
+ * Count the AMD core counter rows ("K8-<n>") on a CPU.  The L3, DF and UMC
+ * rows share the K8 class and prefix ("K8-L3-", "K8-DF-", "K8-UMC") and are
+ * not core counters.  With free_only, count only rows that are free.
+ */
+static inline int
+amd_test_count_core_rows(int cpu, bool free_only)
+{
+	struct pmc_pmcinfo *pmcinfo;
+	const struct pmc_info *p;
+	int count, i, npmc;
+
+	ATF_REQUIRE_MSG(pmc_pmcinfo(cpu, &pmcinfo) == 0,
+	    "pmc_pmcinfo(%d) failed: %s", cpu, strerror(errno));
+
+	npmc = pmc_npmc(cpu);
+	if (npmc < 0) {
+		free(pmcinfo);
+		atf_tc_fail("pmc_npmc(%d) failed: %s", cpu, strerror(errno));
+	}
+
+	count = 0;
+	for (i = 0; i < npmc; i++) {
+		p = &pmcinfo->pm_pmcs[i];
+		if (strncmp(p->pm_name, "K8-", 3) != 0 ||
+		    p->pm_name[3] < '0' || p->pm_name[3] > '9')
+			continue;
+		if (free_only && p->pm_rowdisp != PMC_DISP_FREE)
+			continue;
+		count++;
+	}
+
+	free(pmcinfo);
+	return (count);
+}
+
+static inline void
+amd_test_release_pmc(pmc_id_t pmcid)
+{
+	if (pmcid != PMC_ID_INVALID)
+		ATF_REQUIRE_MSG(pmc_release(pmcid) == 0,
+		    "pmc_release(%u) failed: %s", pmcid, strerror(errno));
+}
+
+#endif /* _AMD_PMC_TEST_COMMON_H_ */
diff --git a/tests/sys/pmc/amd/pmc_frontend_test.sh b/tests/sys/pmc/amd/pmc_frontend_test.sh
new file mode 100644
index 000000000000..c3fe8215ec21
--- /dev/null
+++ b/tests/sys/pmc/amd/pmc_frontend_test.sh
@@ -0,0 +1,54 @@
+#
+# Copyright (c) 2026 Advanced Micro Devices, Inc.
+#
+# Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+
+#
+# pmc(8) frontend study, which records AMD IBS fetch samples.
+#
+
+atf_test_case pmc_frontend_ibs
+pmc_frontend_ibs_head()
+{
+	atf_set "descr" \
+	    "pmc record -s frontend captures IBS fetch samples and " \
+	    "pmc frontend analyzes the log"
+	atf_set "require.user" "root"
+	atf_set "require.progs" "pmc pmccontrol"
+	atf_set "timeout" "60"
+}
+pmc_frontend_ibs_body()
+{
+	local load rc
+
+	sysctl -n kern.hwpmc.cpuid > /dev/null 2>&1 || \
+	    atf_skip "hwpmc(4) is not loaded"
+	pmccontrol -l | grep -q 'IBS-FETCH' || \
+	    atf_skip "hwpmc registered no IBS fetch row on this host"
+
+	# Keep a CPU busy so that samples are taken while recording.
+	sh -c 'i=0; while [ $i -lt 3000000 ]; do i=$((i + 1)); done' &
+	load=$!
+	# pmc record returns kevent()'s result (1) when the -t timer ends
+	# the capture and 0 when it is interrupted.
+	pmc record -s frontend -t 3 frontend.pmc > record.out 2>&1
+	rc=$?
+	kill "$load" 2>/dev/null
+	wait
+	[ "$rc" -le 1 ] || \
+	    atf_fail "pmc record -s frontend failed: $(cat record.out)"
+
+	# Each decoded sample row of the analysis carries miss percentages.
+	atf_check -s exit:0 -o save:frontend.txt -e ignore \
+	    pmc frontend frontend.pmc
+	grep -q '[0-9]%' frontend.txt || \
+	    atf_fail "pmc frontend decoded no IBS fetch sample"
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case pmc_frontend_ibs
+}
diff --git a/tests/sys/pmc/amd/pmu_detect_test.c b/tests/sys/pmc/amd/pmu_detect_test.c
new file mode 100644
index 000000000000..277f7432e7ed
--- /dev/null
+++ b/tests/sys/pmc/amd/pmu_detect_test.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * Checks that hwpmc registers the AMD core PMU on Zen CPUs: the K8 class is
+ * present with 48-bit counters and at least six core rows, alongside the
+ * TSC class.
+ */
+
+#include <sys/param.h>
+#include <sys/pmc.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <pmc.h>
+#include <string.h>
+
+#include "amd_pmc_test_common.h"
+
+/* Every Zen generation has at least six core counters. */
+#define	AMD_CORE_PMC_MIN	6
+
+ATF_TC(pmu_detect_k8_class_present);
+ATF_TC_HEAD(pmu_detect_k8_class_present, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Verify that PMC_CLASS_K8 is listed in pmc_cpuinfo() on Zen.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_detect_k8_class_present, tc)
+{
+	const struct pmc_cpuinfo *cpuinfo;
+	struct amd_test_cpu cpu;
+
+	amd_test_skip_unless_zen(&cpu);
+	amd_test_skip_unless_hwpmc();
+
+	ATF_REQUIRE_MSG(pmc_cpuinfo(&cpuinfo) == 0,
+	    "pmc_cpuinfo() failed: %s", strerror(errno));
+	ATF_CHECK_MSG(amd_test_find_classinfo(cpuinfo, PMC_CLASS_K8) != NULL,
+	    "PMC_CLASS_K8 not registered on %s", amd_test_zen_name(cpu.zen));
+}
+
+ATF_TC(pmu_detect_counter_width);
+ATF_TC_HEAD(pmu_detect_counter_width, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Verify that the AMD core counters are reported as 48 bits wide.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_detect_counter_width, tc)
+{
+	const struct pmc_cpuinfo *cpuinfo;
+	const struct pmc_classinfo *ci;
+	struct amd_test_cpu cpu;
+
+	amd_test_skip_unless_zen(&cpu);
+	amd_test_skip_unless_hwpmc();
+
+	ATF_REQUIRE_MSG(pmc_cpuinfo(&cpuinfo) == 0,
+	    "pmc_cpuinfo() failed: %s", strerror(errno));
+	ci = amd_test_find_classinfo(cpuinfo, PMC_CLASS_K8);
+	ATF_REQUIRE_MSG(ci != NULL, "PMC_CLASS_K8 not registered");
+
+	ATF_CHECK_MSG(ci->pm_width == 48,
+	    "AMD core counter width is %u bits, expected 48", ci->pm_width);
+}
+
+ATF_TC(pmu_detect_core_pmc_count);
+ATF_TC_HEAD(pmu_detect_core_pmc_count, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Verify that cpu0 exposes at least "
+	    __XSTRING(AMD_CORE_PMC_MIN) " AMD core counter rows.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_detect_core_pmc_count, tc)
+{
+	struct amd_test_cpu cpu;
+	int n;
+
+	amd_test_skip_unless_zen(&cpu);
+	amd_test_skip_unless_perfctr_extcore();
+	amd_test_skip_unless_hwpmc();
+
+	n = amd_test_count_core_rows(0, false);
+	ATF_CHECK_MSG(n >= AMD_CORE_PMC_MIN,
+	    "expected at least %d core counter rows on cpu0 (%s), got %d",
+	    AMD_CORE_PMC_MIN, amd_test_zen_name(cpu.zen), n);
+}
+
+ATF_TC(pmu_detect_tsc_class_coexists);
+ATF_TC_HEAD(pmu_detect_tsc_class_coexists, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Verify that PMC_CLASS_TSC and PMC_CLASS_K8 are both registered.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_detect_tsc_class_coexists, tc)
+{
+	const struct pmc_cpuinfo *cpuinfo;
+	struct amd_test_cpu cpu;
+
+	amd_test_skip_unless_zen(&cpu);
+	amd_test_skip_unless_hwpmc();
+
+	ATF_REQUIRE_MSG(pmc_cpuinfo(&cpuinfo) == 0,
+	    "pmc_cpuinfo() failed: %s", strerror(errno));
+	ATF_CHECK_MSG(amd_test_find_classinfo(cpuinfo, PMC_CLASS_K8) != NULL,
+	    "PMC_CLASS_K8 not registered");
+	ATF_CHECK_MSG(amd_test_find_classinfo(cpuinfo, PMC_CLASS_TSC) != NULL,
+	    "PMC_CLASS_TSC not registered");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, pmu_detect_k8_class_present);
+	ATF_TP_ADD_TC(tp, pmu_detect_counter_width);
+	ATF_TP_ADD_TC(tp, pmu_detect_core_pmc_count);
+	ATF_TP_ADD_TC(tp, pmu_detect_tsc_class_coexists);
+
+	return (atf_no_error());
+}
diff --git a/tests/sys/pmc/amd/pmu_event_neg_test.c b/tests/sys/pmc/amd/pmu_event_neg_test.c
new file mode 100644
index 000000000000..a38ffa95357b
--- /dev/null
+++ b/tests/sys/pmc/amd/pmu_event_neg_test.c
@@ -0,0 +1,254 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * These tests issue PMC_OP_PMCALLOCATE directly, as libpmc does, with
+ * config words that libpmc would never build, to exercise the config
+ * validation in hwpmc_amd.c.
+ *
+ * Kernel validation path, amd_can_assign_pmc():
+ *	config = a->pm_md.pm_amd.pm_amd_config;
+ *	if ((config & ~amd_config_mask(subclass, caps)) != 0)
+ *		return (EINVAL);
+ *
+ * For the CORE sub-class amd_config_mask() is AMD_VALID_BITS (CounterMask,
+ * Invert, Enable, Int, PC, Edge, OS, USR, UnitMask, EventMask) on every
+ * family, plus PRECISERETIRE (bit 43) only with PMC_CAP_PRECISE, plus the
+ * kern.hwpmc.amd_core_extra_mask tunable.  HOST (bit 41), GUEST (bit 40)
+ * and bits 44 and up must therefore be rejected.
+ */
+
+#include <sys/param.h>
+#include <sys/module.h>
+#include <sys/pmc.h>
+#include <sys/sysctl.h>
+
+#include <dev/hwpmc/hwpmc_amd.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <pmc.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "amd_pmc_test_common.h"
+
+/* Find the hwpmc syscall number the same way pmc_init() does. */
+static int
+neg_get_pmc_syscall(void)
+{
+	struct module_stat ms;
+	int mod_id;
+
+	mod_id = modfind(PMC_MODULE_NAME);
+	if (mod_id < 0)
+		return (-1);
+
+	ms.version = sizeof(ms);
+	if (modstat(mod_id, &ms) < 0)
+		return (-1);
+
+	return (ms.data.intval);
+}
+
+static int
+neg_raw_pmcallocate(int pmc_sc, struct pmc_op_pmcallocate *op)
+{
+	return (syscall(pmc_sc, PMC_OP_PMCALLOCATE, op));
+}
+
+static void
+neg_raw_pmcrelease(int pmc_sc, pmc_id_t pmcid)
+{
+	struct pmc_op_simple op;
+
+	memset(&op, 0, sizeof(op));
+	op.pm_pmcid = pmcid;
+	(void)syscall(pmc_sc, PMC_OP_PMCRELEASE, &op);
+}
+
+/*
+ * Skip unless the raw allocation can be tested, and return the syscall
+ * number together with a valid system-mode request for ex_ret_instr on
+ * cpu0 (the event is in every amdzen* table).
+ */
+static void
+neg_setup(int *sc, struct pmc_op_pmcallocate *op)
+{
+	struct amd_test_cpu cpu;
+	uint64_t extra;
+	size_t len;
+	int error;
+
+	amd_test_skip_unless_zen(&cpu);
+	amd_test_skip_unless_hwpmc();
+
+	len = sizeof(extra);
+	if (sysctlbyname("kern.hwpmc.amd_core_extra_mask", &extra, &len,
+	    NULL, 0) == 0 && extra != 0)
+		atf_tc_skip("kern.hwpmc.amd_core_extra_mask=%#jx widens the "
+		    "accepted CORE bits", (uintmax_t)extra);
+	if (!pmc_pmu_enabled())
+		atf_tc_skip("pmu-events not enabled");
+
+	*sc = neg_get_pmc_syscall();
+	if (*sc < 0)
+		atf_tc_skip("cannot find the hwpmc syscall: %s",
+		    strerror(errno));
+
+	memset(op, 0, sizeof(*op));
+	error = pmc_pmu_pmcallocate("ex_ret_instr", op);
+	if (error != 0)
+		atf_tc_skip("ex_ret_instr not resolvable: %s",
+		    strerror(error));
+	op->pm_mode = PMC_MODE_SC;
+	op->pm_flags = PMC_F_EV_PMU;
+	op->pm_cpu = 0;
+	op->pm_pmcid = PMC_ID_INVALID;
+}
+
+/*
+ * Check that the request with bits added to its config word is refused
+ * with EINVAL.  hwpmc also answers EINVAL when no row is free, so the
+ * unmodified request must be accepted first.
+ */
+static void
+neg_expect_rejected(uint64_t bits, const char *what)
+{
+	struct pmc_op_pmcallocate op, valid;
+	int sc;
+
+	neg_setup(&sc, &valid);
+
+	op = valid;
+	if (neg_raw_pmcallocate(sc, &op) != 0)
+		atf_tc_skip("the unmodified request is refused: %s",
+		    strerror(errno));
+	neg_raw_pmcrelease(sc, op.pm_pmcid);
+
+	op = valid;
+	op.pm_md.pm_amd.pm_amd_config |= bits;
+	if (neg_raw_pmcallocate(sc, &op) == 0) {
+		neg_raw_pmcrelease(sc, op.pm_pmcid);
+		atf_tc_fail("config word with %s (0x%016jx) was accepted",
+		    what, (uintmax_t)op.pm_md.pm_amd.pm_amd_config);
+	}
+	ATF_CHECK_MSG(errno == EINVAL,
+	    "config word with %s: expected EINVAL, got %d (%s)",
+	    what, errno, strerror(errno));
+}
+
+ATF_TC(pmu_neg_valid_config_accepted);
+ATF_TC_HEAD(pmu_neg_valid_config_accepted, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "[NEG/SANITY] The unmodified ex_ret_instr request is accepted by "
+	    "the raw PMC_OP_PMCALLOCATE path used by the rejection tests.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_neg_valid_config_accepted, tc)
+{
+	struct pmc_op_pmcallocate op;
+	int sc;
+
+	neg_setup(&sc, &op);
+	if (amd_test_count_core_rows(0, true) == 0)
+		atf_tc_skip("no free core counter on cpu0");
+
+	ATF_REQUIRE_MSG(neg_raw_pmcallocate(sc, &op) == 0,
+	    "valid config word 0x%016jx was refused: %s",
+	    (uintmax_t)op.pm_md.pm_amd.pm_amd_config, strerror(errno));
+	neg_raw_pmcrelease(sc, op.pm_pmcid);
+}
+
+ATF_TC(pmu_neg_host_bit_rejected);
+ATF_TC_HEAD(pmu_neg_host_bit_rejected, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "[NEG] A CORE config word with HOST (bit 41) set is refused "
+	    "with EINVAL.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_neg_host_bit_rejected, tc)
+{
+	neg_expect_rejected(AMD_PMC_HOST, "HOST (bit 41)");
+}
+
+ATF_TC(pmu_neg_guest_bit_rejected);
+ATF_TC_HEAD(pmu_neg_guest_bit_rejected, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "[NEG] A CORE config word with GUEST (bit 40) set is refused "
+	    "with EINVAL.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_neg_guest_bit_rejected, tc)
+{
+	neg_expect_rejected(AMD_PMC_GUEST, "GUEST (bit 40)");
+}
+
+ATF_TC(pmu_neg_host_and_guest_bits_rejected);
+ATF_TC_HEAD(pmu_neg_host_and_guest_bits_rejected, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "[NEG] A CORE config word with both HOST and GUEST set is "
+	    "refused with EINVAL.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_neg_host_and_guest_bits_rejected, tc)
+{
+	neg_expect_rejected(AMD_PMC_HOST | AMD_PMC_GUEST, "HOST|GUEST");
+}
+
+ATF_TC(pmu_neg_reserved_bits_rejected);
+ATF_TC_HEAD(pmu_neg_reserved_bits_rejected, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "[NEG] A CORE config word with reserved bit 44 set is refused "
+	    "with EINVAL.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_neg_reserved_bits_rejected, tc)
+{
+	neg_expect_rejected(1ULL << 44, "reserved bit 44");
+}
+
+ATF_TC(pmu_neg_preciseretire_without_cap_rejected);
+ATF_TC_HEAD(pmu_neg_preciseretire_without_cap_rejected, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "[NEG] A CORE config word with PRECISERETIRE (bit 43) set is "
+	    "refused with EINVAL when PMC_CAP_PRECISE is not requested.");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(pmu_neg_preciseretire_without_cap_rejected, tc)
+{
+	/* libpmc requests only PMC_CAP_READ | PMC_CAP_WRITE. */
+	neg_expect_rejected(AMD_PMC_PRECISERETIRE,
+	    "PRECISERETIRE (bit 43) without PMC_CAP_PRECISE");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, pmu_neg_valid_config_accepted);
+	ATF_TP_ADD_TC(tp, pmu_neg_host_bit_rejected);
+	ATF_TP_ADD_TC(tp, pmu_neg_guest_bit_rejected);
+	ATF_TP_ADD_TC(tp, pmu_neg_host_and_guest_bits_rejected);
+	ATF_TP_ADD_TC(tp, pmu_neg_reserved_bits_rejected);
+	ATF_TP_ADD_TC(tp, pmu_neg_preciseretire_without_cap_rejected);
+
+	return (atf_no_error());
+}
diff --git a/tests/sys/pmc/amd/pmu_event_test.c b/tests/sys/pmc/amd/pmu_event_test.c
new file mode 100644
index 000000000000..3b9d6224570f
--- /dev/null
+++ b/tests/sys/pmc/amd/pmu_event_test.c
@@ -0,0 +1,481 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+ * Author: Davi C.A <davi.chavesazevedo@amd.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ *   [UNIT]  No hardware: AMD_PMC_TO_EVENTMASK splits a 12-bit EventSelect
+ *           into config word bits 7:0 and 35:32.
+ *
+ *   [SMOKE] pmc_pmu_pmcallocate() lookup by name for well-known core
+ *           events of the running Zen generation.  Verifies EventSelect
+ *           bits 7:0 + 35:32, UnitMask bits 15:8, and OsUser bits 17:16
+ *           in the returned config word.
+ *
+ *   [E2E]   Count ex_ret_instr on cpu0 across a known loop.
+ *
+ * Reference:
+ *   AMD PPR for Family 19h Model 11h: PERF_CTL MSR bit layout:
+ *     bits  7:0   EventSelect[7:0]
+ *     bits 15:8   UnitMask[7:0]
+ *     bit  16     USR (count in user mode)
+ *     bit  17     OS  (count in OS/kernel mode)
+ *     bits 35:32  EventSelect[11:8]
+ *     bit  40     GUEST (count in guest mode)
+ *     bit  41     HOST  (count in host mode)
+ *
+ *   hwpmc macros (sys/dev/hwpmc/hwpmc_amd.h):
+ *     AMD_PMC_TO_EVENTMASK(x) = ((x) & 0xFF) | (((uint64_t)(x) & 0xF00) << 24)
+ *     AMD_PMC_TO_UNITMASK(x)  = ((x) << 8) & 0xFF00
+ *     AMD_PMC_USR              = (1 << 16)
+ *     AMD_PMC_OS               = (1 << 17)
+ */
+
+#include <sys/param.h>
+#include <sys/pmc.h>
+
+#include <dev/hwpmc/hwpmc_amd.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <pmc.h>
+#include <stdbool.h>
+#include <stdint.h>
*** 1866 LINES SKIPPED ***