Skip to content

Commit 6164968

Browse files
committed
arch/riscv: Reset MSTATUS before configuring PMP
The PMP configuration must be performed with M-mode privileges to ensure PMP CSRs (Control and Status Registers) can be correctly accessed and written. Firmwares that utilize multi-stage or memory-relocating boot sequences (e.g., jumping between Read-Only and Read-Write images) might enter the kernel with a non-default `mstatus` configuration left over from the previous execution environment. This commit ensures that the necessary M-mode privilege state is asserted before PMP registers are initialized, guaranteeing consistency and correctness in the hardware protection setup by clearing the `MSTATUS_MPRV` and setting the `MSTATUS_MPP` bits. Signed-off-by: Firas Sammoura <[email protected]>
1 parent b8576db commit 6164968

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

arch/riscv/core/pmp.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,14 @@ static unsigned int global_pmp_end_index;
350350
*/
351351
void z_riscv_pmp_init(void)
352352
{
353+
/*
354+
* Ensure we are in M-mode and that memory accesses use M-mode privileges
355+
* (MPRV=0) before configuring PMP registers. The PMP registers are
356+
* accessible only in M-mode.
357+
*/
358+
csr_clear(mstatus, MSTATUS_MPRV);
359+
csr_set(mstatus, MSTATUS_MPP);
360+
353361
unsigned long pmp_addr[CONFIG_PMP_SLOTS];
354362
unsigned long pmp_cfg[CONFIG_PMP_SLOTS / PMPCFG_STRIDE];
355363
unsigned int index = 0;

0 commit comments

Comments
 (0)