[PATCH] lib: sbi: Fix pmp_set/pmp_get to use sbi_hart_pmp_count to get the number of pmp

Anup Patel anup at brainfault.org
Fri Nov 12 01:28:30 PST 2021


On Mon, Nov 8, 2021 at 3:33 PM Xiang W <wxjstz at 126.com> wrote:
>
> Signed-off-by: Xiang W <wxjstz at 126.com>

> ---
>  lib/sbi/riscv_asm.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
> index f6b8c8e..2d64592 100644
> --- a/lib/sbi/riscv_asm.c
> +++ b/lib/sbi/riscv_asm.c
> @@ -12,6 +12,8 @@
>  #include <sbi/sbi_error.h>
>  #include <sbi/sbi_platform.h>
>  #include <sbi/sbi_console.h>
> +#include <sbi/sbi_scratch.h>
> +#include <sbi/sbi_hart.h>
>
>  /* determine CPU extension, return non-zero support */
>  int misa_extension_imp(char ext)
> @@ -234,9 +236,11 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
>         int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
>         unsigned long cfgmask, pmpcfg;
>         unsigned long addrmask, pmpaddr;
> +       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
> +       unsigned int pmp_count = sbi_hart_pmp_count(scratch);

This seems redundant because the caller of these functions already
ensures "n" to be within limits (i.e. less than sbi_hart_pmp_count(scratch)).

That's why we are only checking PMP_COUNT so that we don't access
invalid CSR number.

Regards,
Anup

>
>         /* check parameters */
> -       if (n >= PMP_COUNT || log2len > __riscv_xlen || log2len < PMP_SHIFT)
> +       if (n >= pmp_count || log2len > __riscv_xlen || log2len < PMP_SHIFT)
>                 return SBI_EINVAL;
>
>         /* calculate PMP register and offset */
> @@ -286,9 +290,11 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
>         int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
>         unsigned long cfgmask, pmpcfg, prot;
>         unsigned long t1, addr, len;
> +       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
> +       unsigned int pmp_count = sbi_hart_pmp_count(scratch);
>
>         /* check parameters */
> -       if (n >= PMP_COUNT || !prot_out || !addr_out || !log2len)
> +       if (n >= pmp_count || !prot_out || !addr_out || !log2len)
>                 return SBI_EINVAL;
>         *prot_out = *addr_out = *log2len = 0;
>
> --
> 2.30.2
>
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi



More information about the opensbi mailing list