[arch][riscv] make sure the stack is 16 byte aligned for new threads
This is according to the RISC-V ABI, even in 32bit mode.
@@ -41,13 +41,16 @@ static void initial_thread_func(void) {
}
void arch_thread_initialize(thread_t *t) {
- LTRACEF("t %p (%s)\n", t, t->name);
-
/* zero out the thread context */
memset(&t->arch.cs_frame, 0, sizeof(t->arch.cs_frame));
- t->arch.cs_frame.sp = (vaddr_t)t->stack + t->stack_size;
+ /* make sure the top of the stack is 16 byte aligned */
+ vaddr_t stack_top = ROUNDDOWN((vaddr_t)t->stack + t->stack_size, 16);
+
+ t->arch.cs_frame.sp = stack_top;
t->arch.cs_frame.ra = (vaddr_t)&initial_thread_func;
+
+ LTRACEF("t %p (%s) stack top %#lx entry %p arg %p\n", t, t->name, stack_top, t->entry, t->arg);
}
void arch_context_switch(thread_t *oldthread, thread_t *newthread) {
