[PATCH 1/3] target/riscv: Assert that the CSR numbers will be correct

Alistair Francis posted 3 patches 10 months, 3 weeks ago
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Bin Meng <bin.meng@windriver.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <dbarboza@ventanamicro.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>
[PATCH 1/3] target/riscv: Assert that the CSR numbers will be correct
Posted by Alistair Francis 10 months, 3 weeks ago
The CSRs will always be between either CSR_MHPMCOUNTER3 and
CSR_MHPMCOUNTER31 or CSR_MHPMCOUNTER3H and CSR_MHPMCOUNTER31H.

So although ctr_index can't be negative, Coverity doesn't know this and
it isn't obvious to human readers either. Let's add an assert to ensure
that Coverity knows the values will be within range.

To simplify the code let's also change the RV32 adjustment.

Fixes: Coverity CID 1523910
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
---
 target/riscv/csr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index fde7ce1a53..336ec7eda7 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -195,8 +195,11 @@ static RISCVException mctr(CPURISCVState *env, int csrno)
 
     if ((riscv_cpu_mxl(env) == MXL_RV32) && csrno >= CSR_MCYCLEH) {
         /* Offset for RV32 mhpmcounternh counters */
-        base_csrno += 0x80;
+        csrno -= 0x80;
     }
+
+    g_assert(csrno >= CSR_MHPMCOUNTER3 && csrno <= CSR_MHPMCOUNTER31);
+
     ctr_index = csrno - base_csrno;
     if ((BIT(ctr_index) & pmu_avail_ctrs >> 3) == 0) {
         /* The PMU is not enabled or counter is out of range */
-- 
2.43.0
Re: [PATCH 1/3] target/riscv: Assert that the CSR numbers will be correct
Posted by Daniel Henrique Barboza 10 months, 3 weeks ago

On 1/7/24 21:13, Alistair Francis wrote:
> The CSRs will always be between either CSR_MHPMCOUNTER3 and
> CSR_MHPMCOUNTER31 or CSR_MHPMCOUNTER3H and CSR_MHPMCOUNTER31H.
> 
> So although ctr_index can't be negative, Coverity doesn't know this and
> it isn't obvious to human readers either. Let's add an assert to ensure
> that Coverity knows the values will be within range.
> 
> To simplify the code let's also change the RV32 adjustment.
> 
> Fixes: Coverity CID 1523910
> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> ---
>   target/riscv/csr.c | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/target/riscv/csr.c b/target/riscv/csr.c
> index fde7ce1a53..336ec7eda7 100644
> --- a/target/riscv/csr.c
> +++ b/target/riscv/csr.c
> @@ -195,8 +195,11 @@ static RISCVException mctr(CPURISCVState *env, int csrno)
>   
>       if ((riscv_cpu_mxl(env) == MXL_RV32) && csrno >= CSR_MCYCLEH) {
>           /* Offset for RV32 mhpmcounternh counters */
> -        base_csrno += 0x80;
> +        csrno -= 0x80;
>       }
> +
> +    g_assert(csrno >= CSR_MHPMCOUNTER3 && csrno <= CSR_MHPMCOUNTER31);
> +
>       ctr_index = csrno - base_csrno;


After this change 'base_csrno' is now used to store CSR_MHPMCOUNTER3 and nothing
else. You can get rid of it, although having the var is arguably more readable.

Regardless, this LGTM:

Reviewed-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>


>       if ((BIT(ctr_index) & pmu_avail_ctrs >> 3) == 0) {
>           /* The PMU is not enabled or counter is out of range */