git: 0efaebd5ab83 - main - hwpmc: Decline the AMD PerfMonV2 path when GLOBAL_CTL is absent

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Fri, 06 Nov 2026 15:57:29 UTC
The branch main has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=0efaebd5ab83f5977f7f5a8f76321e77dee1fdb3

commit 0efaebd5ab83f5977f7f5a8f76321e77dee1fdb3
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-06 08:49:16 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-06 15:57:12 +0000

    hwpmc: Decline the AMD PerfMonV2 path when GLOBAL_CTL is absent
    
    The v2 path writes AMD_PMC_GLOBAL_CTL whenever the PerfMonV2 CPUID bit
    is set. bhyve passes that bit through from the host but does not emulate
    the register, so loading hwpmc in a guest on a Family 19h or later host
    panics the kernel.
    
    amd_v2_hwcheck_cpu() already probes EVSEL MSRs, so probe GLOBAL_CTL
    there too, in order to reject the v2 path and fall back to the generic
    CPU class.
    
    Fixes:          9d3064010590 ("hwpmc_amd: add PerfMonV2 global-control path")
    Tested by:      Osvaldo Janeri Filho <ojanerif@amd.com>
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    mhorne, afscoelho@gmail.com
    Differential Revision:  https://reviews.freebsd.org/D61133
---
 sys/dev/hwpmc/hwpmc_amd.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/sys/dev/hwpmc/hwpmc_amd.c b/sys/dev/hwpmc/hwpmc_amd.c
index f466fff4cfb3..e85a3af7cba8 100644
--- a/sys/dev/hwpmc/hwpmc_amd.c
+++ b/sys/dev/hwpmc/hwpmc_amd.c
@@ -1377,6 +1377,16 @@ amd_v2_hwcheck_cpu(void *arg)
 	int error, i;
 
 	state = arg;
+
+	/*
+	 * A buggy hypervisor can advertise the PerfMonV2 CPUID bit without
+	 * implementing GLOBAL_CTL.
+	 */
+	if (rdmsr_safe(AMD_PMC_GLOBAL_CTL, &reg) != 0) {
+		atomic_set_int(&state->avh_read_error, 1);
+		return;
+	}
+
 	for (i = 0; i < amd_core_npmcs; i++) {
 		error = rdmsr_safe(amd_pmcdesc[i].pm_evsel, &reg);
 		if (error != 0) {
@@ -1407,7 +1417,7 @@ amd_hwcheck(void)
 		smp_rendezvous_cpus(all_cpus, smp_no_rendezvous_barrier,
 		    amd_v2_hwcheck_cpu, smp_no_rendezvous_barrier, &state);
 		if (state.avh_read_error != 0) {
-			printf("hwpmc: AMD PerfMonV2 EVSEL read failed on one "
+			printf("hwpmc: AMD PerfMonV2 MSR read failed on one "
 			    "or more CPUs!\n");
 			return (-1);
 		}
@@ -1585,8 +1595,12 @@ pmc_amd_initialize(void)
 			pmcs_per_umc = amd_umc_npmcs / popcntq(regs[2]);
 		}
 
-		/* EAX bit 0 holds the PerfMonV2 flag. */
-		if (EXTPERFMON_PERFMONV2(regs[0]) && family >= 0x19)
+		/*
+		 * EAX bit 0 holds the PerfMonV2 flag.  PerfMonV2 requires
+		 * PerfCtrExtCore; reject inconsistent CPUID (e.g. bhyve).
+		 */
+		if (EXTPERFMON_PERFMONV2(regs[0]) && family >= 0x19 &&
+		    (amd_feature2 & AMDID2_PCXC) != 0)
 			amd_perfmon_v2 = true;
 	}