arch: riscv: update PMP setting to privileged mode for fault handler

When RISCV_ALWAYS_SWITCH_THROUGH_ECALL is enabled, do_swap() enables PMP
checking in is_kernel_syscall.
If a user thread violates memory protection and do_swap() is called from
the fault handler, a PMP error occurs because the thread is in privileged
mode but still using the old user mode PMP setting.

Update the PMP setting to privileged mode for fault handler.
This also enables the stack guard for user thread's privileged stack in
fault handler.

Signed-off-by: Jimmy Zheng <jimmyzhe@andestech.com>
This commit is contained in:
Jimmy Zheng 2024-07-15 17:09:50 +08:00 committed by Anas Nashif
parent 9f56ff5d64
commit 58bda887ce

View File

@ -347,6 +347,21 @@ no_fp: /* increment _current->arch.exception_depth */
*/
li t1, RISCV_EXC_ECALLU
beq t0, t1, is_user_syscall
#ifdef CONFIG_PMP_STACK_GUARD
/*
* Determine if we come from user space. If so, reconfigure the PMP for
* kernel mode stack guard.
*/
csrr t0, mstatus
li t1, MSTATUS_MPP
and t0, t0, t1
bnez t0, 1f
lr a0, ___cpu_t_current_OFFSET(s0)
call z_riscv_pmp_stackguard_enable
1:
#endif /* CONFIG_PMP_STACK_GUARD */
#endif /* CONFIG_USERSPACE */
/*