From 21218ec05c46ad4272a2ce6c45d7de44927f4105 Mon Sep 17 00:00:00 2001 From: Jani Paalijarvi Date: Thu, 23 May 2024 10:43:33 +0300 Subject: [PATCH 1/2] riscv_pmp.c: Check that size is power of two for NAPOT The size must be power-of-two according to the the PMP spec. Signed-off-by: Jani Paalijarvi --- arch/risc-v/src/common/riscv_pmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/risc-v/src/common/riscv_pmp.c b/arch/risc-v/src/common/riscv_pmp.c index e6074b5154b02..1114390d5eb18 100644 --- a/arch/risc-v/src/common/riscv_pmp.c +++ b/arch/risc-v/src/common/riscv_pmp.c @@ -134,9 +134,9 @@ static bool pmp_check_region_attrs(uintptr_t base, uintptr_t size, case PMPCFG_A_NAPOT: { - /* For NAPOT, both base and size must be properly aligned */ + /* For NAPOT, Naturally aligned power-of-two region, >= 8 bytes */ - if ((base & 0x07) != 0 || size < 8) + if ((base & 0x07) != 0 || size < 8 || (size & (size - 1)) != 0) { return false; } From ecd9e3ed608c346f4dbd920a20d551808427b98e Mon Sep 17 00:00:00 2001 From: Jani Paalijarvi Date: Fri, 24 May 2024 08:57:41 +0300 Subject: [PATCH 2/2] mpfs_mpu: Check that size is valid for MPUCFG The size must be power-of-two for NAPOT according to the the PMP spec. Signed-off-by: Jani Paalijarvi --- arch/risc-v/src/mpfs/mpfs_mpu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/risc-v/src/mpfs/mpfs_mpu.c b/arch/risc-v/src/mpfs/mpfs_mpu.c index 2643c79c0ac7a..2adaf3d911c58 100644 --- a/arch/risc-v/src/mpfs/mpfs_mpu.c +++ b/arch/risc-v/src/mpfs/mpfs_mpu.c @@ -159,9 +159,11 @@ int mpfs_mpu_set(uintptr_t reg, uintptr_t perm, uintptr_t base, return -EACCES; } - /* Base must be word aligned, minimum size is 4K */ + /* Base must be word aligned, + * minimum size is 4K and it has to be power-of-two + */ - if ((base & 0x07) != 0 || size < 0x1000) + if ((base & 0x07) != 0 || size < 0x1000 || (size & (size - 1)) != 0) { return -EINVAL; }