From 9dcd9f936510c3c3d3d8a7a3bcbb07730a6c53ff Mon Sep 17 00:00:00 2001 From: Pengpeng Hou Date: Fri, 28 Aug 2026 22:22:30 +0800 Subject: [PATCH] lib: sbi: use unsigned masks for HPM counter 31 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 Reviewed-by: Anup Patel Link: https://lore.kernel.org/r/20260828142354.13035-1-pengpeng@iscas.ac.cn Signed-off-by: Anup Patel --- 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 4558abff..8483d264 100644 --- a/lib/sbi/sbi_emulate_csr.c +++ b/lib/sbi/sbi_emulate_csr.c @@ -104,7 +104,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); \ } \ }