Skip to content

Commit d39ca1a

Browse files
committed
arch: riscv: Use CONFIG_PMP_SLOTS for PMP array sizing
The arrays used for M-mode Physical Memory Protection (PMP), specifically for the stack guard feature (CONFIG_PMP_STACK_GUARD), were previously sized using the hardcoded PMP_M_MODE_SLOTS macro, defined as 8. This affected arrays in both the _thread_arch struct (m_mode_pmpaddr_regs, m_mode_pmpcfg_regs) and local variables within the z_riscv_pmp_stackguard_disable function. This commit changes the array sizing to use the Kconfig option CONFIG_PMP_SLOTS. This option reflects the total number of PMP slots available and configured for the target hardware. Using CONFIG_PMP_SLOTS ensures these arrays are dimensioned according to the system's actual capabilities, providing better flexibility and correctness over a fixed size. The PMP_M_MODE_SLOTS macro definition has been removed from thread.h as it is no longer used. Signed-off-by: Firas Sammoura <[email protected]>
1 parent 6bb4cb5 commit d39ca1a

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

arch/riscv/core/pmp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ void z_riscv_pmp_stackguard_enable(struct k_thread *thread)
539539
void z_riscv_pmp_stackguard_disable(void)
540540
{
541541

542-
unsigned long pmp_addr[PMP_M_MODE_SLOTS];
543-
unsigned long pmp_cfg[PMP_M_MODE_SLOTS / sizeof(unsigned long)];
542+
unsigned long pmp_addr[CONFIG_PMP_SLOTS];
543+
unsigned long pmp_cfg[CONFIG_PMP_SLOTS / sizeof(unsigned long)];
544544
unsigned int index = global_pmp_end_index;
545545

546546
/* Retrieve the pmpaddr value matching the last global PMP slot. */

include/zephyr/arch/riscv/thread.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ struct z_riscv_fp_context {
6363
};
6464
typedef struct z_riscv_fp_context z_riscv_fp_context_t;
6565

66-
#define PMP_M_MODE_SLOTS 8 /* 8 is plenty enough for m-mode */
67-
6866
struct _thread_arch {
6967
#ifdef CONFIG_FPU_SHARING
7068
struct z_riscv_fp_context saved_fp_context;
@@ -81,8 +79,8 @@ struct _thread_arch {
8179
#endif
8280
#ifdef CONFIG_PMP_STACK_GUARD
8381
unsigned int m_mode_pmp_end_index;
84-
unsigned long m_mode_pmpaddr_regs[PMP_M_MODE_SLOTS];
85-
unsigned long m_mode_pmpcfg_regs[PMP_M_MODE_SLOTS / sizeof(unsigned long)];
82+
unsigned long m_mode_pmpaddr_regs[CONFIG_PMP_SLOTS];
83+
unsigned long m_mode_pmpcfg_regs[CONFIG_PMP_SLOTS / sizeof(unsigned long)];
8684
#endif
8785
};
8886

0 commit comments

Comments
 (0)