Commit 258305e
committed
arch: riscv: Defer PMP lock and ensure stack guard in IRQ offload
This commit addresses two aspects of PMP stack guarding on RISC-V:
1. Defer Locking of Interrupt Stack Guard PMP Entry:
The PMP entry for the interrupt stack guard, initially configured in
`z_riscv_pmp_init()`, was previously locked immediately using the
`PMP_L` 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 changes between the
RO and RW stages, the already-locked PMP entry for the original
address cannot be reconfigured.
This change removes the premature `PMP_L` flag from the PMP entry setup
in `z_riscv_pmp_init()`. The essential locking is now deferred to
`z_riscv_pmp_stackguard_enable()`, which is executed only when the
system is ready and the stack guard is enabled
(`CONFIG_PMP_STACK_GUARD` && `CONFIG_MULTITHREADING`).
2. Enforce PMP Stack Guard during IRQ Offload:
Since the PMP entry for the stack guard is no longer locked early in
the boot process, we must ensure that the PMP settings are enforced
in all critical execution contexts. The `do_irq_offload` path needs
explicit `mstatus` configuration to activate the PMP protection for
the interrupt stack.
This change adds assembly instructions to `do_irq_offload` to
properly configure `mstatus` before executing the offloaded routine:
- Clear `mstatus.MPP` (Machine Previous Privilege).
- Set `mstatus.MPRV` to 1.
Setting `mstatus.MPRV` makes memory accesses within the offloaded IRQ
routine behave as if they are occurring from M-mode, thus subjecting
them to the PMP rules, including the stack guard entry. This is
essential to ensure the stack guard is active during IRQ offloads,
conditionally applied when `CONFIG_PMP_STACK_GUARD` and
`CONFIG_MULTITHREADING` are enabled.
Signed-off-by: Firas Sammoura <[email protected]>1 parent d3e645e commit 258305e
2 files changed
+13
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
486 | 486 | | |
487 | 487 | | |
488 | 488 | | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
489 | 501 | | |
490 | 502 | | |
491 | 503 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
381 | | - | |
| 381 | + | |
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
| |||
0 commit comments