Skip to content

Commit 0ceb080

Browse files
committed
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 <[email protected]>
1 parent 698abba commit 0ceb080

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

core/thread.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,19 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority,
226226
thread_t *thread = (thread_t *)(uintptr_t)(stack + stacksize);
227227

228228
#ifdef PICOLIBC_TLS
229-
stacksize -= _tls_size();
229+
#if __PICOLIBC_MAJOR__ > 1 || __PICOLIBC_MINOR__ >= 8
230+
#define TLS_ALIGN (alignof(thread_t) > _tls_align() ? alignof(thread_t) : _tls_align())
231+
#else
232+
#define TLS_ALIGN alignof(thread_t)
233+
#endif
234+
char *tls = stack + stacksize - _tls_size();
235+
/*
236+
* Make sure the TLS area is aligned as required and that the
237+
* resulting stack will also be aligned as required
238+
*/
239+
thread->tls = (void *) ((uintptr_t) tls & ~ (TLS_ALIGN - 1));
240+
stacksize = (char *) thread->tls - stack;
230241

231-
thread->tls = stack + stacksize;
232242
_init_tls(thread->tls);
233243
#endif
234244

0 commit comments

Comments
 (0)