Skip to content

Commit

Permalink
core: riscv: Improve thread user mode record
Browse files Browse the repository at this point in the history
Make the asm definitions be more human-readable.

Besides, it's unnecessary to save and restore kernel SP and GP into
thread_user_mode_rec, since they will be setup by system call trap
handler before executing thread_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 Oct 7, 2024
1 parent 9f71579 commit 9df67cd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
8 changes: 4 additions & 4 deletions core/arch/riscv/include/kernel/thread_private_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ struct thread_user_mode_rec {
unsigned long pad;
/*
* x[] is used to save registers for user/kernel context-switching
* 0-3: ra-tp
* 4-6: s0-s1
* 6-15: s2-s11
* 0: ra
* 1-2: s0-s1
* 3-12: s2-s11
*/
unsigned long x[16];
unsigned long x[13];
};

extern long thread_user_kcode_offset;
Expand Down
10 changes: 4 additions & 6 deletions core/arch/riscv/kernel/asm-defines.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ DEFINES
/* struct thread_user_mode_rec */
DEFINE(THREAD_USER_MODE_REC_CTX_REGS_PTR,
offsetof(struct thread_user_mode_rec, ctx_regs_ptr));
DEFINE(THREAD_USER_MODE_REC_X1,
DEFINE(THREAD_USER_MODE_REC_RA,
offsetof(struct thread_user_mode_rec, x[0]));
DEFINE(THREAD_USER_MODE_REC_X4,
DEFINE(THREAD_USER_MODE_REC_S0,
offsetof(struct thread_user_mode_rec, x[1]));
DEFINE(THREAD_USER_MODE_REC_S2,
offsetof(struct thread_user_mode_rec, x[3]));
DEFINE(THREAD_USER_MODE_REC_X8,
offsetof(struct thread_user_mode_rec, x[4]));
DEFINE(THREAD_USER_MODE_REC_X18,
offsetof(struct thread_user_mode_rec, x[6]));
DEFINE(THREAD_USER_MODE_REC_SIZE, sizeof(struct thread_user_mode_rec));

/* struct thread_abort_regs */
Expand Down
26 changes: 9 additions & 17 deletions core/arch/riscv/kernel/thread_rv.S
Original file line number Diff line number Diff line change
Expand Up @@ -633,30 +633,22 @@ END_FUNC trap_from_user

/*
* void thread_unwind_user_mode(uint32_t ret, uint32_t exit_status0,
* uint32_t exit_status1);
* uint32_t exit_status1);
* See description in thread.h
*/
FUNC thread_unwind_user_mode , :

/* Store the exit status */
load_xregs sp, THREAD_USER_MODE_REC_CTX_REGS_PTR, REG_A3, REG_A5
sw a1, (a4)
sw a2, (a5)

/* Save user callee regs */
/* Save user callee-saved regs */
store_xregs a3, THREAD_CTX_REG_S0, REG_S0, REG_S1
store_xregs a3, THREAD_CTX_REG_S2, REG_S2, REG_S11
store_xregs a3, THREAD_CTX_REG_SP, REG_SP, REG_TP

/* Restore kernel callee regs */
mv a1, sp

load_xregs a1, THREAD_USER_MODE_REC_X1, REG_RA, REG_GP
load_xregs a1, THREAD_USER_MODE_REC_X8, REG_S0, REG_S1
load_xregs a1, THREAD_USER_MODE_REC_X18, REG_S2, REG_S11

/* Restore kernel ra(thread_enter_user_mode()) & callee-saved regs */
load_xregs sp, THREAD_USER_MODE_REC_RA, REG_RA
load_xregs sp, THREAD_USER_MODE_REC_S0, REG_S0, REG_S1
load_xregs sp, THREAD_USER_MODE_REC_S2, REG_S2, REG_S11
add sp, sp, THREAD_USER_MODE_REC_SIZE

/* Return from the call of thread_enter_user_mode() */
ret
END_FUNC thread_unwind_user_mode
Expand All @@ -672,9 +664,9 @@ FUNC __thread_enter_user_mode , :
*/
addi sp, sp, -THREAD_USER_MODE_REC_SIZE
store_xregs sp, THREAD_USER_MODE_REC_CTX_REGS_PTR, REG_A0, REG_A2
store_xregs sp, THREAD_USER_MODE_REC_X1, REG_RA, REG_GP
store_xregs sp, THREAD_USER_MODE_REC_X8, REG_S0, REG_S1
store_xregs sp, THREAD_USER_MODE_REC_X18, REG_S2, REG_S11
store_xregs sp, THREAD_USER_MODE_REC_RA, REG_RA
store_xregs sp, THREAD_USER_MODE_REC_S0, REG_S0, REG_S1
store_xregs sp, THREAD_USER_MODE_REC_S2, REG_S2, REG_S11

/*
* Save the kernel stack pointer in the thread context
Expand Down

0 comments on commit 9df67cd

Please sign in to comment.