Skip to content

Commit a38482a

Browse files
committed
arch/riscv: Defer locking of interrupt stack guard PMP entry
The PMP entry for the interrupt stack guard is initially configured in `z_riscv_pmp_init()`. This entry was previously locked immediately using the `PMP_L` (Lock) flag. This immediate locking causes issues in complex boot scenarios, such as systems that jump from a Read-Only (RO) image to a Read-Write (RW) image. When switching images, the kernel must re-initialize PMP. If the interrupt stack address, calculated as: `(uintptr_t)z_interrupt_stacks[_current_cpu->id]`, changes between the RO and RW stages, the already-locked PMP entry for the original address cannot be reconfigured, leading to an incorrect or inoperable memory protection setup. This commit removes the premature `PMP_L` flag from the PMP entry setup in `z_riscv_pmp_init()`. The essential locking of the interrupt stack guard will now be deferred and correctly applied within `z_riscv_pmp_stackguard_enable()`, which is executed only when the system is ready and the conditions: `defined(CONFIG_PMP_STACK_GUARD) && defined(CONFIG_MULTITHREADING)` are met. Signed-off-by: Firas Sammoura <[email protected]>
1 parent b8576db commit a38482a

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

arch/riscv/core/pmp.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,8 @@ void z_riscv_pmp_init(void)
378378
* addresses inaccessible. This will never change so we do it here
379379
* and lock it too.
380380
*/
381-
set_pmp_entry(&index, PMP_NONE | PMP_L,
382-
(uintptr_t)z_interrupt_stacks[_current_cpu->id],
383-
Z_RISCV_STACK_GUARD_SIZE,
384-
pmp_addr, pmp_cfg, ARRAY_SIZE(pmp_addr));
385-
381+
set_pmp_entry(&index, PMP_NONE, (uintptr_t)z_interrupt_stacks[_current_cpu->id],
382+
Z_RISCV_STACK_GUARD_SIZE, pmp_addr, pmp_cfg, ARRAY_SIZE(pmp_addr));
386383
/*
387384
* This early, the kernel init code uses the IRQ stack and we want to
388385
* safeguard it as soon as possible. But we need a temporary default

0 commit comments

Comments
 (0)