From 9c11c01a2c0014a95f445e61a5d825f9123d4899 Mon Sep 17 00:00:00 2001 From: Alvin Chang Date: Wed, 11 Sep 2024 15:51:09 +0800 Subject: [PATCH] core: riscv: Fix misconfiguration of XSCRATCH when XRET to kernel mode When the program wants to XRET to kernel mode, the value of XSCRATCH must be cleared to zero. Signed-off-by: Alvin Chang --- core/arch/riscv/kernel/thread_rv.S | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/arch/riscv/kernel/thread_rv.S b/core/arch/riscv/kernel/thread_rv.S index 4efbe316ba3..8f687c1ec7d 100644 --- a/core/arch/riscv/kernel/thread_rv.S +++ b/core/arch/riscv/kernel/thread_rv.S @@ -154,8 +154,8 @@ native_interrupt_from_kernel: /* Restore XIE */ load_xregs sp, THREAD_CTX_REG_IE, REG_T0 csrw CSR_XIE, t0 - /* Set scratch as thread_core_local */ - csrw CSR_XSCRATCH, tp + /* We are going to XRET to kernel mode. Set XSCRATCH as 0 */ + csrw CSR_XSCRATCH, 0 /* Restore all GPRs */ load_xregs sp, THREAD_CTX_REG_RA, REG_RA load_xregs sp, THREAD_CTX_REG_GP, REG_GP @@ -252,8 +252,8 @@ set_sp: /* Restore XIE */ load_xregs sp, THREAD_ABT_REG_IE, REG_T0 csrw CSR_XIE, t0 - /* Set scratch as thread_core_local */ - csrw CSR_XSCRATCH, tp + /* We are going to XRET to kernel mode. Set XSCRATCH as 0 */ + csrw CSR_XSCRATCH, 0 /* Update core local flags */ lw a0, THREAD_CORE_LOCAL_FLAGS(tp)