[PATCH] lib: sbi: use unsigned masks for HPM counter 31

Pengpeng Hou pengpeng at iscas.ac.cn
Fri Aug 28 07:22:30 PDT 2026


The HPM feature detector and CSR emulator build mhpm_mask bits with a
plain signed 1. Both macro families expand through counter 31, so the
last expansion evaluates 1 << 31 and shifts a signed int into its sign
bit.

Use BIT() at both sites. The mask is unsigned and bit 31 is part of the
represented HPM counter set.

Fixes: c744ed77b18c ("lib: sbi_pmu: Enable noncontigous hpm event and counters")
Fixes: ee1f83ca848d ("lib: sbi_pmu: remove mhpm_count field in hart feature")
Signed-off-by: Pengpeng Hou <pengpeng at iscas.ac.cn>
---
 lib/sbi/sbi_emulate_csr.c | 2 +-
 lib/sbi/sbi_hart.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/sbi/sbi_emulate_csr.c b/lib/sbi/sbi_emulate_csr.c
index c2253c82..5d3a6e9d 100644
--- a/lib/sbi/sbi_emulate_csr.c
+++ b/lib/sbi/sbi_emulate_csr.c
@@ -103,7 +103,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,
 
 #define switchcase_hpm(__uref, __mref, __csr)				\
 	case __csr:							\
-		if (sbi_hart_mhpm_mask(scratch) & (1 << (__csr - __uref)))\
+		if (sbi_hart_mhpm_mask(scratch) & BIT(__csr - __uref))	\
 			return SBI_ENOTSUPP;				\
 		if (!hpm_allowed(__csr - __uref, prev_mode, virt))	\
 			return SBI_ENOTSUPP;				\
diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c
index bee88557..8e4c36d0 100644
--- a/lib/sbi/sbi_hart.c
+++ b/lib/sbi/sbi_hart.c
@@ -512,7 +512,7 @@ static int hart_detect_features(struct sbi_scratch *scratch, bool cold_boot)
 	if (!trap.cause) {						  \
 		csr_write_allowed(__csr, &trap, 1UL);			  \
 		if (!trap.cause && csr_swap(__csr, oldval) == 1UL) {	  \
-			(hfeatures->__mask) |= 1 << (__csr - CSR_MCYCLE); \
+			(hfeatures->__mask) |= BIT(__csr - CSR_MCYCLE);	  \
 		}							  \
 	}
 

base-commit: 4e79fd7de59f1b2899092c1a84ce68c8ebc68f93
-- 
2.43.0




More information about the opensbi mailing list