diff options
Diffstat (limited to 'libc-top-half/musl/src/env')
-rw-r--r-- | libc-top-half/musl/src/env/__init_tls.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/env/__init_tls.c b/libc-top-half/musl/src/env/__init_tls.c index ee785bc..ece8d24 100644 --- a/libc-top-half/musl/src/env/__init_tls.c +++ b/libc-top-half/musl/src/env/__init_tls.c @@ -16,6 +16,43 @@ volatile int __thread_list_lock; #ifndef __wasilibc_unmodified_upstream + +/* These symbols are generated by wasm-ld. __stack_high/__stack_low + * symbols are only available in LLVM v16 and higher, therefore they're + * defined as weak symbols and if not available, __heap_base/__data_end + * is used instead. + * + * TODO: remove usage of __heap_base/__data_end for stack size calculation + * once we drop support for LLVM v15 and older. + */ +extern unsigned char __heap_base; +extern unsigned char __data_end; +extern unsigned char __global_base; +extern weak unsigned char __stack_high; +extern weak unsigned char __stack_low; + +static inline void setup_default_stack_size() +{ + ptrdiff_t stack_size; + + if (&__stack_high) + stack_size = &__stack_high - &__stack_low; + else { + unsigned char *sp; + __asm__( + ".globaltype __stack_pointer, i32\n" + "global.get __stack_pointer\n" + "local.set %0\n" + : "=r"(sp)); + stack_size = sp > &__global_base ? &__heap_base - &__data_end : (ptrdiff_t)&__global_base; + } + + if (stack_size > __default_stacksize) + __default_stacksize = + stack_size < DEFAULT_STACK_MAX ? + stack_size : DEFAULT_STACK_MAX; +} + void __wasi_init_tp() { __init_tp((void *)__get_tp()); } @@ -31,6 +68,20 @@ int __init_tp(void *p) if (!r) libc.can_do_threads = 1; td->detach_state = DT_JOINABLE; td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock); +#else + setup_default_stack_size(); + td->detach_state = DT_JOINABLE; + /* + * Initialize the TID to a value which doesn't conflict with + * host-allocated TIDs, so that TID-based locks can work. + * + * Note: + * - Host-allocated TIDs range from 1 to 0x1fffffff. (inclusive) + * - __tl_lock and __lockfile uses TID 0 as "unlocked". + * - __lockfile relies on the fact the most significant two bits + * of TIDs are 0. + */ + td->tid = 0x3fffffff; #endif td->locale = &libc.global_locale; td->robust_list.head = &td->robust_list.head; |