lib: sbi: Fix counter index sanity check
authorAtish Patra <atishp@rivosinc.com>
Wed, 20 Jul 2022 21:50:32 +0000 (14:50 -0700)
committerAnup Patel <anup@brainfault.org>
Sat, 30 Jul 2022 05:39:18 +0000 (11:09 +0530)
commit1545afd342f2e22aaf91c4760dc5706965ec7fdf
treeaffabfba9fc05a9814ddeedba94abe8eaec31795
parent88b790f1291618274f94b6d5110ed09e20148bb2
lib: sbi: Fix counter index sanity check

The current implementation computes the possible counter range
by doing a left shift of counter base. However, this may overflow depending
on the counter base value. In case of overflow, the highest counter id
may be computed incorrectly. As per the SBI specification, the respective
function should return an error if any of the counter is not valid.

Fix the counter index check by avoiding left shifting while doing the
sanity checks. Without the shift, the implementation just iterates
over the counter mask and computes the correct counter index by adding
the base to it.

Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
lib/sbi/sbi_pmu.c