From 8f5a6930e80579ad852786b8464bbb7246d39dca Mon Sep 17 00:00:00 2001 From: Eero Nurkkala Date: Wed, 20 Sep 2023 15:18:20 +0300 Subject: [PATCH] riscv/riscv_pmp.c: fix broken TOR checks PMPCFG_A_TOR region may have zero size. The pmp configuration currently fails for zero-sized TOR. This patch bypasses such a restriction. Also replace log2ceil with LOG2_CEIL from lib/math32.h. Signed-off-by: Eero Nurkkala --- arch/risc-v/src/common/riscv_pmp.c | 31 +++--------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/arch/risc-v/src/common/riscv_pmp.c b/arch/risc-v/src/common/riscv_pmp.c index bf0b19a210..652a6d5614 100644 --- a/arch/risc-v/src/common/riscv_pmp.c +++ b/arch/risc-v/src/common/riscv_pmp.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "riscv_internal.h" @@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: log2ceil - * - * Description: - * Calculate the up-rounded power-of-two for input. - * - * Input Parameters: - * size - The size of the PMP region. - * - * Returned Value: - * Power-of-two for argument, rounded up. - * - ****************************************************************************/ - -static uintptr_t log2ceil(uintptr_t size) -{ - uintptr_t pot = 0; - - for (size = size - 1; size > 1; size >>= 1) - { - pot++; - } - - return pot; -} - /**************************************************************************** * Name: pmp_check_addrmatch_type * @@ -191,7 +166,7 @@ static bool pmp_check_region_attrs(uintptr_t base, uintptr_t size, { /* Check that the size is not too small */ - if (size < MIN_BLOCK_SIZE) + if ((type != PMPCFG_A_TOR) && (size < MIN_BLOCK_SIZE)) { return false; } @@ -216,7 +191,7 @@ static bool pmp_check_region_attrs(uintptr_t base, uintptr_t size, { /* Get the power-of-two for size, rounded up */ - uintptr_t pot = log2ceil(size); + uintptr_t pot = LOG2_CEIL(size); if ((base & ((UINT64_C(1) << pot) - 1)) != 0) {