Skip to content

Commit

Permalink
arm64_task/pthread_start: Set sp_el0 upon starting user process
Browse files Browse the repository at this point in the history
As the handling of sp_el0 was moved from the context switch routine
to exception entry/exit, we must set sp_el0 explicitly when the user
process is first started.
  • Loading branch information
pussuw authored and xiaoxiang781216 committed Oct 2, 2024
1 parent 8e200e6 commit 24c931c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion arch/arm64/src/common/arm64_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ EXTERN uint8_t g_idle_topstack[]; /* End+1 of heap */

void arm64_new_task(struct tcb_s *tak_new);
void arm64_jump_to_user(uint64_t entry, uint64_t x0, uint64_t x1,
uint64_t *regs) noreturn_function;
uint64_t sp_el0, uint64_t *regs) noreturn_function;

/* Low level initialization provided by chip logic */

Expand Down
4 changes: 3 additions & 1 deletion arch/arm64/src/common/arm64_pthread_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
void up_pthread_start(pthread_trampoline_t startup,
pthread_startroutine_t entrypt, pthread_addr_t arg)
{
struct tcb_s *rtcb = this_task();

/* Set up to enter the user-space pthread start-up function in
* unprivileged mode. We need:
*
Expand All @@ -78,7 +80,7 @@ void up_pthread_start(pthread_trampoline_t startup,
*/

arm64_jump_to_user((uint64_t)startup, (uint64_t)entrypt, (uint64_t)arg,
this_task()->xcp.initregs);
(uint64_t)rtcb->xcp.ustkptr, rtcb->xcp.initregs);
}

#endif /* !CONFIG_BUILD_FLAT && __KERNEL__ && !CONFIG_DISABLE_PTHREAD */
4 changes: 3 additions & 1 deletion arch/arm64/src/common/arm64_task_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@

void up_task_start(main_t taskentry, int argc, char *argv[])
{
struct tcb_s *rtcb = this_task();

/* Set up to return to the user-space _start function in
* unprivileged mode. We need:
*
Expand All @@ -75,7 +77,7 @@ void up_task_start(main_t taskentry, int argc, char *argv[])
*/

arm64_jump_to_user((uint64_t)taskentry, (uint64_t)argc, (uint64_t)argv,
this_task()->xcp.initregs);
(uint64_t)rtcb->xcp.ustkptr, rtcb->xcp.initregs);
}

#endif /* !CONFIG_BUILD_FLAT */
12 changes: 7 additions & 5 deletions arch/arm64/src/common/arm64_vectors.S
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,23 @@ SECTION_FUNC(text, up_saveusercontext)
* the kernel is ready to give control to the user task in user space.
*
* arm64_jump_to_user(entry, x0, x1, regs)
* entry: process entry point
* x0: parameter 0 for process
* x1: parameter 1 for process
* regs: integer register save area to use
* entry: process entry point
* x0: parameter 0 for process
* x1: parameter 1 for process
* sp_el0: user stack pointer
* regs: integer register save area to use
*
****************************************************************************/

#ifndef CONFIG_BUILD_FLAT
GTEXT(arm64_jump_to_user)
SECTION_FUNC(text, arm64_jump_to_user)
msr daifset, #IRQ_DAIF_MASK
mov sp, x3
mov sp, x4
str x0, [sp, #8 * REG_ELR]
str x1, [sp, #8 * REG_X0]
str x2, [sp, #8 * REG_X1]
str x3, [sp, #8 * REG_SP_EL0]
mrs x0, spsr_el1
and x0, x0, #~SPSR_MODE_MASK
#orr x0, x0, #SPSR_MODE_EL0T # EL0T=0x00, out of range for orr
Expand Down

0 comments on commit 24c931c

Please sign in to comment.