Skip to content

Commit

Permalink
core: riscv: Simplify SP setup in setup_unwind_user_mode()
Browse files Browse the repository at this point in the history
The parameter "regs" is the stack pointer which is allocated to store
system call registers when calling thread_scall_handler(). Thus, we can
simply get the original stack pointer by "regs + 1" equation, and use it
to exit user mode.

The code is referenced from ARM's setup_unwind_user_mode().

Signed-off-by: Alvin Chang <[email protected]>
Reviewed-by: Yu Chien Peter Lin <[email protected]>
  • Loading branch information
gagachang authored and jforissier committed Sep 4, 2024
1 parent 980d32c commit cd7384a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/arch/riscv/kernel/thread_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ static void setup_unwind_user_mode(struct thread_scall_regs *regs)
{
regs->ra = (uintptr_t)thread_unwind_user_mode;
regs->status = xstatus_for_xret(true, PRV_S);
regs->sp = thread_get_saved_thread_sp();
/*
* We are going to exit user mode. The stack pointer must be set as the
* original value it had before allocating space of scall "regs" and
* calling thread_scall_handler(). Thus, we can simply set stack pointer
* as (regs + 1) value.
*/
regs->sp = (uintptr_t)(regs + 1);
}

static void thread_unhandled_trap(unsigned long cause __unused,
Expand Down

0 comments on commit cd7384a

Please sign in to comment.