Skip to content

Commit

Permalink
fork: return value in child
Browse files Browse the repository at this point in the history
Due to exiting on different stack than current's thread in fork in child
the return value was not set by threads_setupUserReturn.

JIRA: RTOS-979
  • Loading branch information
badochov committed Nov 29, 2024
1 parent 07519a2 commit 08999da
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 47 deletions.
22 changes: 10 additions & 12 deletions hal/armv7a/_interrupts.S
Original file line number Diff line number Diff line change
Expand Up @@ -259,31 +259,29 @@ _interrupts_dispatch:
.type _syscalls_dispatch, %function
_syscalls_dispatch:
stmfd sp, {r0-r4}^
sub r1, sp, #0x14
sub r2, sp, #0x14
mrs r3, spsr
mov r2, lr
mov r1, lr
tst r3, #THUMB_STATE
ldreq r0, [r2, #-4]
ldreq r0, [r1, #-4]
biceq r0, r0, #0xff000000
ldrneh r0, [r2, #-2]
ldrneh r0, [r1, #-2]
bicne r0, r0, #0xff00
mrc p15, 0, r4, c13, c0, 4 /* TPIDRPRW */

cpsie af, #SYS_MODE

/* Store CPU state onto kernel stack */
stmfd r4!, {r2}
stmfd r4!, {r1}
stmfd r4!, {r5-r14}
mov r2, sp
mov r1, sp
mov sp, r4
ldmfd r1, {r4-r8}
ldmfd r2, {r4-r8}
push {r3-r8}
push_fpu_state r4
sub r1, sp, #8
push {r1}
push {r1}

mov r1, r2
sub r2, sp, #8
push {r2}
push {r2}

cpsie if

Expand Down
4 changes: 2 additions & 2 deletions hal/armv7a/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static void exceptions_defaultHandler(unsigned int n, exc_context_t *ctx)
}


extern void threads_setupUserReturn(void *retval);
extern void threads_setupUserReturn(void *retval, cpu_context_t *ctx);


void exceptions_dispatch(unsigned int n, exc_context_t *ctx)
Expand All @@ -130,7 +130,7 @@ void exceptions_dispatch(unsigned int n, exc_context_t *ctx)

/* Handle signals if necessary */
if (hal_cpuSupervisorMode(&ctx->cpuCtx) == 0) {
threads_setupUserReturn((void *)ctx->cpuCtx.r0);
threads_setupUserReturn((void *)ctx->cpuCtx.r0, &ctx->cpuCtx);
}
}

Expand Down
2 changes: 1 addition & 1 deletion hal/armv7m/imxrt/_init.S
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ _syscall_dispatch:
str r12, [sp, #8] /* ustack */

/* At this point full context is saved on kernel stack */
add r2, sp, #(38 * 4)
str sp, [r6]
isb
dmb
Expand All @@ -139,6 +138,7 @@ _syscall_dispatch:

stmdb r0!, {r1-r4}

mov r2, sp
sub sp, sp, #(18 * 4)

mov r1, r0
Expand Down
1 change: 1 addition & 0 deletions hal/armv7m/stm32/_init.S
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ _syscall_dispatch:
/* Copy arguments back to the user stack */
stmdb r0!, {r1-r4}
mov r1, r0
mov r2, sp
ldrb r0, [r7, #-3]

/* Prepare pseudo context */
Expand Down
1 change: 1 addition & 0 deletions hal/armv8m/mcx/_init.S
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ _syscall_dispatch:
/* Copy arguments back to the user stack */
stmdb r0!, {r1-r4}
mov r1, r0
mov r2, sp
ldrb r0, [r7, #-3]

/* Prepare pseudo context */
Expand Down
1 change: 1 addition & 0 deletions hal/armv8m/nrf/_init.S
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ _syscall_dispatch:
/* Copy arguments back to the user stack */
stmdb r0!, {r1-r4}
mov r1, r0
mov r2, sp
ldrb r0, [r7, #-3]

/* Prepare pseudo context */
Expand Down
22 changes: 10 additions & 12 deletions hal/armv8r/_interrupts.S
Original file line number Diff line number Diff line change
Expand Up @@ -259,31 +259,29 @@ _interrupts_dispatch:
.type _syscalls_dispatch, %function
_syscalls_dispatch:
stmfd sp, {r0-r4}^
sub r1, sp, #0x14
sub r2, sp, #0x14
mrs r3, spsr
mov r2, lr
mov r1, lr
tst r3, #THUMB_STATE
ldreq r0, [r2, #-4]
ldreq r0, [r1, #-4]
biceq r0, r0, #0xff000000
ldrneh r0, [r2, #-2]
ldrneh r0, [r1, #-2]
bicne r0, r0, #0xff00
mrc p15, 0, r4, c13, c0, 4 /* TPIDRPRW */

cpsie af, #MODE_SYS

/* Store CPU state onto kernel stack */
stmfd r4!, {r2}
stmfd r4!, {r1}
stmfd r4!, {r5-r14}
mov r2, sp
mov r1, sp
mov sp, r4
ldmfd r1, {r4-r8}
ldmfd r2, {r4-r8}
push {r3-r8}
push_fpu_state r4
sub r1, sp, #8
push {r1}
push {r1}

mov r1, r2
sub r2, sp, #8
push {r2}
push {r2}

cpsie if

Expand Down
4 changes: 2 additions & 2 deletions hal/armv8r/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static void exceptions_defaultHandler(unsigned int n, exc_context_t *ctx)
}


extern void threads_setupUserReturn(void *retval);
extern void threads_setupUserReturn(void *retval, cpu_context_t *ctx);


void exceptions_dispatch(unsigned int n, exc_context_t *ctx)
Expand All @@ -135,7 +135,7 @@ void exceptions_dispatch(unsigned int n, exc_context_t *ctx)

/* Handle signals if necessary */
if (hal_cpuSupervisorMode(&ctx->cpuCtx) == 0) {
threads_setupUserReturn((void *)ctx->cpuCtx.r0);
threads_setupUserReturn((void *)ctx->cpuCtx.r0, &ctx->cpuCtx);
}
}

Expand Down
7 changes: 5 additions & 2 deletions hal/ia32/_exceptions.S
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,14 @@ sym:
addl $4, %esp;\
testl %eax, %eax;\
jnz exception_popContext;\
/* cpu_context_t */;\
leal 28(%esp), %eax;\
/* return value */ ;\
movl 56(%esp), %eax;\
movl 56(%esp), %ebx;\
pushl %eax;\
pushl %ebx;\
call threads_setupUserReturn;\
addl $4, %esp;\
addl $8, %esp;\
;\
jmp exception_popContext

Expand Down
4 changes: 2 additions & 2 deletions hal/ia32/_interrupts.S
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ _interrupts_syscall:
movw %dx, %ds
movw %dx, %es
movl (4 * CTXPUSHL + 12)(%esp), %edx
pushl %esp
pushl %edx
pushl %eax
sti
call syscalls_dispatch
cli
addl $8, %esp
movl %eax, (4 * CTXPUSHL - EAX_OFFSET)(%esp) /* Save return value to eax in context */
addl $12, %esp
jmp interrupts_popContextUnlocked
.size _interrupts_syscall, .-_interrupts_syscall

Expand Down
3 changes: 1 addition & 2 deletions hal/riscv64/_interrupts.S
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ _interrupts_notIrq:
sd s2, 248(sp)
mv a0, a7 /* syscall number */
ld a1, 288(sp) /* ustack */

mv a2, sp
csrs sstatus, SSTATUS_SIE
call syscalls_dispatch
sd a0, 56(sp)
csrc sstatus, SSTATUS_SIE

tail _interrupts_returnUnlocked
Expand Down
4 changes: 2 additions & 2 deletions hal/riscv64/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ inline ptr_t hal_exceptionsPC(exc_context_t *ctx)
}


extern void threads_setupUserReturn(void *retval);
extern void threads_setupUserReturn(void *retval, cpu_context_t *ctx);


void exceptions_dispatch(unsigned int n, cpu_context_t *ctx)
Expand All @@ -195,7 +195,7 @@ void exceptions_dispatch(unsigned int n, cpu_context_t *ctx)

/* Handle signals if necessary */
if (hal_cpuSupervisorMode(ctx) == 0) {
threads_setupUserReturn((void *)ctx->a0);
threads_setupUserReturn((void *)ctx->a0, ctx);
}
}

Expand Down
4 changes: 2 additions & 2 deletions hal/sparcv8leon/_traps.S
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,9 @@ s_fpu_done:
nop
nop

/* void *syscalls_dispatch(int n, char *ustack) */
/* void *syscalls_dispatch(int n, char *ustack, cpu_context_t *ctx) */
call syscalls_dispatch
nop
add %sp, 0x60, %sp

/* disable traps */
#ifdef LEON3_USE_PWR
Expand Down
4 changes: 2 additions & 2 deletions hal/sparcv8leon/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ __attribute__((noreturn)) static void exceptions_defaultHandler(unsigned int n,
}


extern void threads_setupUserReturn(void *retval);
extern void threads_setupUserReturn(void *retval, cpu_context_t *ctx);


void exceptions_dispatch(unsigned int n, exc_context_t *ctx)
Expand All @@ -167,7 +167,7 @@ void exceptions_dispatch(unsigned int n, exc_context_t *ctx)

/* Handle signals if necessary */
if (hal_cpuSupervisorMode(&ctx->cpuCtx) == 0) {
threads_setupUserReturn((void *)ctx->cpuCtx.o0);
threads_setupUserReturn((void *)ctx->cpuCtx.o0, &ctx->cpuCtx);
}
}

Expand Down
5 changes: 2 additions & 3 deletions proc/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -1482,10 +1482,10 @@ static int _threads_checkSignal(thread_t *selected, process_t *proc, cpu_context
}


void threads_setupUserReturn(void *retval)
void threads_setupUserReturn(void *retval, cpu_context_t *ctx)
{
spinlock_ctx_t sc;
cpu_context_t *ctx, *signalCtx;
cpu_context_t *signalCtx;
void *f;
void *kstackTop;
thread_t *thread;
Expand All @@ -1494,7 +1494,6 @@ void threads_setupUserReturn(void *retval)
thread = _proc_current();

kstackTop = thread->kstack + thread->kstacksz;
ctx = kstackTop - sizeof(*ctx);
signalCtx = (void *)((char *)hal_cpuGetUserSP(ctx) - sizeof(*signalCtx));
hal_cpuSetReturnValue(ctx, retval);

Expand Down
2 changes: 1 addition & 1 deletion proc/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ extern int _threads_init(vm_map_t *kmap, vm_object_t *kernel);
extern int threads_sigpost(process_t *process, thread_t *thread, int sig);


extern void threads_setupUserReturn(void *retval);
extern void threads_setupUserReturn(void *retval, cpu_context_t *ctx);


#endif
4 changes: 2 additions & 2 deletions syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -1854,7 +1854,7 @@ const void *const syscalls[] = { SYSCALLS(SYSCALLS_NAME) };
const char *const syscall_strings[] = { SYSCALLS(SYSCALLS_STRING) };


void *syscalls_dispatch(int n, char *ustack)
void *syscalls_dispatch(int n, char *ustack, cpu_context_t *ctx)
{
void *retval;

Expand All @@ -1868,7 +1868,7 @@ void *syscalls_dispatch(int n, char *ustack)
proc_threadEnd();
}

threads_setupUserReturn(retval);
threads_setupUserReturn(retval, ctx);

return retval;
}
Expand Down

0 comments on commit 08999da

Please sign in to comment.