From 58bda887ced2a80daee1397ad9d7b2bcf229e96d Mon Sep 17 00:00:00 2001 From: Jimmy Zheng Date: Mon, 15 Jul 2024 17:09:50 +0800 Subject: [PATCH] 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 --- arch/riscv/core/isr.S | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/riscv/core/isr.S b/arch/riscv/core/isr.S index da2068ed43d..bd21fed6b55 100644 --- a/arch/riscv/core/isr.S +++ b/arch/riscv/core/isr.S @@ -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 */ /*