Skip to content

Commit a00bf32

Browse files
12101111haraldh
andauthored
threads: implement init of TLS and stack pointer (#342)
* threads: implement init of TLS and stack pointer * fix: rename wasi_snapshot_preview2_thread_spawn to wasi_thread_spawn Signed-off-by: Harald Hoyer <harald@profian.com> * fix: change signature of wasi_thread_start Signed-off-by: Harald Hoyer <harald@profian.com> * fix: pthread_exit for WASI Can't use `exit()` because it is too high level. Have to unlock the thread list. Signed-off-by: Harald Hoyer <harald@profian.com> * fix: initialize struct pthread for the main thread Signed-off-by: Harald Hoyer <harald@profian.com> * fix: store the aligned stack minus `struct start_args` Signed-off-by: Harald Hoyer <harald@profian.com> Signed-off-by: Harald Hoyer <harald@profian.com> Co-authored-by: Harald Hoyer <harald@profian.com>
1 parent b99173e commit a00bf32

11 files changed

Lines changed: 163 additions & 40 deletions

File tree

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,16 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
192192
ifeq ($(THREAD_MODEL), posix)
193193
LIBC_TOP_HALF_MUSL_SOURCES += \
194194
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
195+
env/__init_tls.c \
196+
stdio/__lockfile.c \
195197
thread/__lock.c \
196198
thread/__wait.c \
197199
thread/__timedwait.c \
200+
thread/default_attr.c \
201+
thread/pthread_attr_destroy.c \
202+
thread/pthread_attr_init.c \
203+
thread/pthread_attr_setstack.c \
204+
thread/pthread_attr_setstacksize.c \
198205
thread/pthread_cleanup_push.c \
199206
thread/pthread_cond_broadcast.c \
200207
thread/pthread_cond_destroy.c \
@@ -235,6 +242,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
235242
thread/pthread_rwlockattr_init.c \
236243
thread/pthread_rwlockattr_setpshared.c \
237244
thread/pthread_setcancelstate.c \
245+
thread/pthread_self.c \
238246
thread/pthread_testcancel.c \
239247
thread/sem_destroy.c \
240248
thread/sem_getvalue.c \

expected/wasm32-wasi/posix/defined-symbols.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ __c_locale
2323
__clock
2424
__clock_gettime
2525
__clock_nanosleep
26+
__copy_tls
2627
__cos
2728
__cosdf
2829
__cosl
@@ -38,6 +39,8 @@ __ctype_tolower_loc
3839
__ctype_toupper_loc
3940
__cxa_atexit
4041
__cxa_finalize
42+
__default_guardsize
43+
__default_stacksize
4144
__des_setkey
4245
__do_cleanup_pop
4346
__do_cleanup_push
@@ -87,6 +90,7 @@ __getopt_msg
8790
__gmtime_r
8891
__hwcap
8992
__inet_aton
93+
__init_tp
9094
__intscan
9195
__invtrigl_R
9296
__isalnum_l
@@ -144,6 +148,7 @@ __locale_lock
144148
__locale_lockptr
145149
__localtime_r
146150
__lock
151+
__lockfile
147152
__log2_data
148153
__log2f_data
149154
__log_data
@@ -265,6 +270,7 @@ __tan
265270
__tandf
266271
__tanl
267272
__testcancel
273+
__thread_list_lock
268274
__timedwait
269275
__timedwait_cp
270276
__tl_lock
@@ -288,6 +294,7 @@ __tsearch_balance
288294
__uflow
289295
__unlist_locked_file
290296
__unlock
297+
__unlockfile
291298
__uselocale
292299
__utc
293300
__wait
@@ -318,6 +325,7 @@ __wasi_fd_seek
318325
__wasi_fd_sync
319326
__wasi_fd_tell
320327
__wasi_fd_write
328+
__wasi_init_tp
321329
__wasi_path_create_directory
322330
__wasi_path_filestat_get
323331
__wasi_path_filestat_set_times
@@ -371,6 +379,7 @@ __wasilibc_nocwd_scandirat
371379
__wasilibc_nocwd_symlinkat
372380
__wasilibc_nocwd_utimensat
373381
__wasilibc_open_nomode
382+
__wasilibc_pthread_self
374383
__wasilibc_register_preopened_fd
375384
__wasilibc_rename_newat
376385
__wasilibc_rename_oldat
@@ -953,6 +962,10 @@ program_invocation_name
953962
program_invocation_short_name
954963
pselect
955964
psignal
965+
pthread_attr_destroy
966+
pthread_attr_init
967+
pthread_attr_setstack
968+
pthread_attr_setstacksize
956969
pthread_cond_broadcast
957970
pthread_cond_destroy
958971
pthread_cond_init
@@ -992,6 +1005,7 @@ pthread_rwlock_wrlock
9921005
pthread_rwlockattr_destroy
9931006
pthread_rwlockattr_init
9941007
pthread_rwlockattr_setpshared
1008+
pthread_self
9951009
pthread_setcancelstate
9961010
pthread_testcancel
9971011
pthread_timedjoin_np
@@ -1182,6 +1196,7 @@ tfind
11821196
tgamma
11831197
tgammaf
11841198
tgammal
1199+
thrd_current
11851200
thrd_sleep
11861201
time
11871202
timegm

expected/wasm32-wasi/posix/undefined-symbols.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
__addtf3
2-
__copy_tls
3-
__default_guardsize
4-
__default_stacksize
52
__divtf3
63
__eqtf2
74
__extenddftf2
@@ -59,19 +56,18 @@ __imported_wasi_snapshot_preview1_sock_accept
5956
__imported_wasi_snapshot_preview1_sock_recv
6057
__imported_wasi_snapshot_preview1_sock_send
6158
__imported_wasi_snapshot_preview1_sock_shutdown
62-
__imported_wasi_snapshot_preview2_thread_spawn
59+
__imported_wasi_thread_spawn
6360
__letf2
64-
__lockfile
6561
__lttf2
6662
__main_argc_argv
6763
__netf2
6864
__stack_pointer
6965
__subtf3
70-
__thread_list_lock
66+
__tls_align
7167
__tls_base
68+
__tls_size
7269
__trunctfdf2
7370
__trunctfsf2
74-
__unlockfile
7571
__unordtf2
76-
__wasilibc_pthread_self
7772
__wasm_call_ctors
73+
__wasm_init_tls

libc-bottom-half/crt/crt1-command.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifdef _REENTRANT
22
#include <stdatomic.h>
3+
extern void __wasi_init_tp(void);
34
#endif
45
#include <wasi/api.h>
56
extern void __wasm_call_ctors(void);
@@ -29,6 +30,10 @@ void _start(void) {
2930
started = 1;
3031
#endif
3132

33+
#ifdef _REENTRANT
34+
__wasi_init_tp();
35+
#endif
36+
3237
// The linker synthesizes this to call constructors.
3338
__wasm_call_ctors();
3439

libc-bottom-half/sources/__wasilibc_real.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,13 @@ __wasi_errno_t __wasi_sock_shutdown(
660660
}
661661

662662
#ifdef _REENTRANT
663-
int32_t __imported_wasi_snapshot_preview2_thread_spawn(int32_t arg0) __attribute__((
664-
__import_module__("wasi_snapshot_preview2"),
663+
int32_t __imported_wasi_thread_spawn(int32_t arg0) __attribute__((
664+
__import_module__("wasi"),
665665
__import_name__("thread_spawn")
666666
));
667667

668668
__wasi_errno_t __wasi_thread_spawn(void* start_arg) {
669-
int32_t ret = __imported_wasi_snapshot_preview2_thread_spawn((int32_t) start_arg);
669+
int32_t ret = __imported_wasi_thread_spawn((int32_t) start_arg);
670670
return (uint16_t) ret;
671671
}
672672
#endif
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
static inline uintptr_t __get_tp(void) {
2-
#if _REENTRANT
3-
int val;
4-
__asm__("global.get __wasilibc_pthread_self\n"
5-
"local.set %0"
6-
: "=r"(val));
7-
return val;
8-
#else
9-
return 0;
10-
#endif
1+
extern _Thread_local struct __pthread __wasilibc_pthread_self;
2+
3+
static inline uintptr_t __get_tp() {
4+
return (uintptr_t)&__wasilibc_pthread_self;
115
}

libc-top-half/musl/src/env/__init_tls.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
#ifdef __wasilibc_unmodified_upstream
12
#define SYSCALL_NO_TLS 1
23
#include <elf.h>
4+
#endif
35
#include <limits.h>
6+
#ifdef __wasilibc_unmodified_upstream
47
#include <sys/mman.h>
8+
#endif
59
#include <string.h>
610
#include <stddef.h>
711
#include "pthread_impl.h"
@@ -11,22 +15,32 @@
1115

1216
volatile int __thread_list_lock;
1317

18+
#ifndef __wasilibc_unmodified_upstream
19+
void __wasi_init_tp() {
20+
__init_tp((void *)__get_tp());
21+
}
22+
#endif
23+
1424
int __init_tp(void *p)
1525
{
1626
pthread_t td = p;
1727
td->self = td;
28+
#ifdef __wasilibc_unmodified_upstream
1829
int r = __set_thread_area(TP_ADJ(p));
1930
if (r < 0) return -1;
2031
if (!r) libc.can_do_threads = 1;
2132
td->detach_state = DT_JOINABLE;
2233
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
34+
#endif
2335
td->locale = &libc.global_locale;
2436
td->robust_list.head = &td->robust_list.head;
2537
td->sysinfo = __sysinfo;
2638
td->next = td->prev = td;
2739
return 0;
2840
}
2941

42+
#ifdef __wasilibc_unmodified_upstream
43+
3044
static struct builtin_tls {
3145
char c;
3246
struct pthread pt;
@@ -35,9 +49,15 @@ static struct builtin_tls {
3549
#define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
3650

3751
static struct tls_module main_tls;
52+
#endif
53+
54+
#ifndef __wasilibc_unmodified_upstream
55+
extern void __wasm_init_tls(void*);
56+
#endif
3857

3958
void *__copy_tls(unsigned char *mem)
4059
{
60+
#ifdef __wasilibc_unmodified_upstream
4161
pthread_t td;
4262
struct tls_module *p;
4363
size_t i;
@@ -69,8 +89,20 @@ void *__copy_tls(unsigned char *mem)
6989
dtv[0] = libc.tls_cnt;
7090
td->dtv = dtv;
7191
return td;
92+
#else
93+
size_t tls_align = __builtin_wasm_tls_align();
94+
volatile void* tls_base = __builtin_wasm_tls_base();
95+
mem += tls_align;
96+
mem -= (uintptr_t)mem & (tls_align-1);
97+
__wasm_init_tls(mem);
98+
__asm__("local.get %0\n"
99+
"global.set __tls_base\n"
100+
:: "r"(tls_base));
101+
return mem;
102+
#endif
72103
}
73104

105+
#ifdef __wasilibc_unmodified_upstream
74106
#if ULONG_MAX == 0xffffffff
75107
typedef Elf32_Phdr Phdr;
76108
#else
@@ -151,3 +183,4 @@ static void static_init_tls(size_t *aux)
151183
}
152184

153185
weak_alias(static_init_tls, __init_tls);
186+
#endif

libc-top-half/musl/src/internal/libc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ struct tls_module {
1818
};
1919

2020
struct __libc {
21-
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
21+
#ifdef __wasilibc_unmodified_upstream
2222
char can_do_threads;
23+
#endif
24+
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
2325
char threaded;
2426
#endif
2527
#ifdef __wasilibc_unmodified_upstream // WASI doesn't currently use any code that needs "secure" mode
@@ -32,7 +34,7 @@ struct __libc {
3234
#ifdef __wasilibc_unmodified_upstream // WASI has no auxv
3335
size_t *auxv;
3436
#endif
35-
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
37+
#ifdef __wasilibc_unmodified_upstream // WASI use different TLS implement
3638
struct tls_module *tls_head;
3739
size_t tls_size, tls_align, tls_cnt;
3840
#endif

libc-top-half/musl/src/internal/pthread_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ struct pthread {
2525
/* Part 1 -- these fields may be external or
2626
* internal (accessed via asm) ABI. Do not change. */
2727
struct pthread *self;
28+
#ifdef __wasilibc_unmodified_upstream
2829
#ifndef TLS_ABOVE_TP
2930
uintptr_t *dtv;
31+
#endif
3032
#endif
3133
struct pthread *prev, *next; /* non-ABI */
3234
uintptr_t sysinfo;

0 commit comments

Comments
 (0)