From 0ceb0804b02185aa2268d7548c34b3bf3f73a376 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 4 Mar 2023 01:18:36 -0800 Subject: [PATCH] core/thread: Fix up stack and TLS alignments Make sure both the stack and TLS blocks are correctly aligned by adjusting the TLS base address to the most strict alignment of the TLS block and the stack. Signed-off-by: Keith Packard --- core/thread.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/core/thread.c b/core/thread.c index 430130d761..4332cb9e91 100644 --- a/core/thread.c +++ b/core/thread.c @@ -226,9 +226,19 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, thread_t *thread = (thread_t *)(uintptr_t)(stack + stacksize); #ifdef PICOLIBC_TLS - stacksize -= _tls_size(); +#if __PICOLIBC_MAJOR__ > 1 || __PICOLIBC_MINOR__ >= 8 +#define TLS_ALIGN (alignof(thread_t) > _tls_align() ? alignof(thread_t) : _tls_align()) +#else +#define TLS_ALIGN alignof(thread_t) +#endif + char *tls = stack + stacksize - _tls_size(); + /* + * Make sure the TLS area is aligned as required and that the + * resulting stack will also be aligned as required + */ + thread->tls = (void *) ((uintptr_t) tls & ~ (TLS_ALIGN - 1)); + stacksize = (char *) thread->tls - stack; - thread->tls = stack + stacksize; _init_tls(thread->tls); #endif