Skip to content

Commit 29555af

Browse files
yamtjohn-sharratt
authored andcommitted
Implement the critical part of wasi_thread_start in asm (WebAssembly#376)
* Implement the critical part of wasi_thread_start in asm It's fragile to set up the critical part of C environment in C. * Specify --target for asm files as well * wasi_thread_start: Move __tls_base initialization to asm as well
1 parent dfc70fa commit 29555af

File tree

6 files changed

+495
-457
lines changed

6 files changed

+495
-457
lines changed

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
293293
thread/sem_timedwait.c \
294294
thread/sem_trywait.c \
295295
thread/sem_wait.c \
296+
thread/wasm32/wasi_thread_start.s \
296297
)
297298
endif
298299

@@ -311,12 +312,13 @@ LIBC_TOP_HALF_HEADERS_PRIVATE = $(LIBC_TOP_HALF_DIR)/headers/private
311312
LIBC_TOP_HALF_SOURCES = $(LIBC_TOP_HALF_DIR)/sources
312313
LIBC_TOP_HALF_ALL_SOURCES = \
313314
$(LIBC_TOP_HALF_MUSL_SOURCES) \
314-
$(sort $(shell find $(LIBC_TOP_HALF_SOURCES) -name \*.c))
315+
$(sort $(shell find $(LIBC_TOP_HALF_SOURCES) -name \*.[cs]))
315316

316317
# Add any extra flags
317318
CFLAGS = $(EXTRA_CFLAGS)
318319
# Set the target.
319320
CFLAGS += --target=$(TARGET_TRIPLE)
321+
ASMFLAGS += --target=$(TARGET_TRIPLE)
320322
# WebAssembly floating-point match doesn't trap.
321323
# TODO: Add -fno-signaling-nans when the compiler supports it.
322324
CFLAGS += -fno-trapping-math
@@ -363,10 +365,11 @@ CFLAGS += -isystem "$(SYSROOT_INC)"
363365
# These variables describe the locations of various files and directories in
364366
# the build tree.
365367
objs = $(patsubst $(CURDIR)/%.c,$(OBJDIR)/%.o,$(1))
368+
asmobjs = $(patsubst $(CURDIR)/%.s,$(OBJDIR)/%.o,$(1))
366369
DLMALLOC_OBJS = $(call objs,$(DLMALLOC_SOURCES))
367370
EMMALLOC_OBJS = $(call objs,$(EMMALLOC_SOURCES))
368371
LIBC_BOTTOM_HALF_ALL_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_ALL_SOURCES))
369-
LIBC_TOP_HALF_ALL_OBJS = $(call objs,$(LIBC_TOP_HALF_ALL_SOURCES))
372+
LIBC_TOP_HALF_ALL_OBJS = $(call asmobjs,$(call objs,$(LIBC_TOP_HALF_ALL_SOURCES)))
370373
ifeq ($(MALLOC_IMPL),dlmalloc)
371374
LIBC_OBJS += $(DLMALLOC_OBJS)
372375
else ifeq ($(MALLOC_IMPL),emmalloc)
@@ -524,6 +527,10 @@ $(OBJDIR)/%.o: $(CURDIR)/%.c include_dirs
524527
@mkdir -p "$(@D)"
525528
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<
526529

530+
$(OBJDIR)/%.o: $(CURDIR)/%.s include_dirs
531+
@mkdir -p "$(@D)"
532+
$(CC) $(ASMFLAGS) -o $@ -c $<
533+
527534
-include $(shell find $(OBJDIR) -name \*.d)
528535

529536
$(DLMALLOC_OBJS): CFLAGS += \

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ __do_cleanup_pop
5959
__do_cleanup_push
6060
__do_des
6161
__do_orphaned_stdio_locks
62+
__dummy_reference
6263
__duplocale
6364
__eintr_handler_lock
6465
__eintr_valid_flag
@@ -485,9 +486,7 @@ __wasi_thread_parallelism
485486
__wasi_thread_signal
486487
__wasi_thread_sleep
487488
__wasi_thread_spawn
488-
__wasi_tty_get
489-
__wasi_tty_set
490-
__wasi_ws_connect
489+
__wasi_thread_start_C
491490
__wasilibc_access
492491
__wasilibc_cwd
493492
__wasilibc_cwd_lock

libc-bottom-half/headers/public/wasi/api.h

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ _Static_assert(_Alignof(int32_t) == 4, "non-wasi data layout");
2525
_Static_assert(_Alignof(uint32_t) == 4, "non-wasi data layout");
2626
_Static_assert(_Alignof(int64_t) == 8, "non-wasi data layout");
2727
_Static_assert(_Alignof(uint64_t) == 8, "non-wasi data layout");
28-
_Static_assert(_Alignof(intptr_t) == 8, "non-wasi data layout");
29-
_Static_assert(_Alignof(uintptr_t) == 8, "non-wasi data layout");
30-
_Static_assert(_Alignof(void*) == 8, "non-wasi data layout");
31-
typedef int64_t __wasi_int_t;
32-
typedef uint64_t __wasi_uint_t;
33-
_Static_assert(_Alignof(__wasi_int_t) == 8, "non-wasi data layout");
34-
_Static_assert(_Alignof(__wasi_uint_t) == 8, "non-wasi data layout");
28+
_Static_assert(_Alignof(intptr_t) == 4, "non-wasi data layout");
29+
_Static_assert(_Alignof(uintptr_t) == 4, "non-wasi data layout");
30+
_Static_assert(_Alignof(void*) == 4, "non-wasi data layout");
31+
typedef int32_t __wasi_int_t;
32+
typedef uint32_t __wasi_uint_t;
33+
_Static_assert(_Alignof(__wasi_int_t) == 4, "non-wasi data layout");
34+
_Static_assert(_Alignof(__wasi_uint_t) == 4, "non-wasi data layout");
3535
#ifdef __cplusplus
3636
extern "C" {
3737
#endif
@@ -43,8 +43,8 @@ extern "C" {
4343
*/
4444
typedef __wasi_uint_t __wasi_pointersize_t;
4545

46-
_Static_assert(sizeof(__wasi_pointersize_t) == 8, "witx calculated size");
47-
_Static_assert(_Alignof(__wasi_pointersize_t) == 8, "witx calculated align");
46+
_Static_assert(sizeof(__wasi_pointersize_t) == 4, "witx calculated size");
47+
_Static_assert(_Alignof(__wasi_pointersize_t) == 4, "witx calculated align");
4848

4949
/**
5050
* Represents a number of items
@@ -990,10 +990,10 @@ typedef struct __wasi_iovec_t {
990990

991991
} __wasi_iovec_t;
992992

993-
_Static_assert(sizeof(__wasi_iovec_t) == 16, "witx calculated size");
994-
_Static_assert(_Alignof(__wasi_iovec_t) == 8, "witx calculated align");
993+
_Static_assert(sizeof(__wasi_iovec_t) == 8, "witx calculated size");
994+
_Static_assert(_Alignof(__wasi_iovec_t) == 4, "witx calculated align");
995995
_Static_assert(offsetof(__wasi_iovec_t, buf) == 0, "witx calculated offset");
996-
_Static_assert(offsetof(__wasi_iovec_t, buf_len) == 8, "witx calculated offset");
996+
_Static_assert(offsetof(__wasi_iovec_t, buf_len) == 4, "witx calculated offset");
997997

998998
/**
999999
* A region of memory for scatter/gather writes.
@@ -1011,10 +1011,10 @@ typedef struct __wasi_ciovec_t {
10111011

10121012
} __wasi_ciovec_t;
10131013

1014-
_Static_assert(sizeof(__wasi_ciovec_t) == 16, "witx calculated size");
1015-
_Static_assert(_Alignof(__wasi_ciovec_t) == 8, "witx calculated align");
1014+
_Static_assert(sizeof(__wasi_ciovec_t) == 8, "witx calculated size");
1015+
_Static_assert(_Alignof(__wasi_ciovec_t) == 4, "witx calculated align");
10161016
_Static_assert(offsetof(__wasi_ciovec_t, buf) == 0, "witx calculated offset");
1017-
_Static_assert(offsetof(__wasi_ciovec_t, buf_len) == 8, "witx calculated offset");
1017+
_Static_assert(offsetof(__wasi_ciovec_t, buf_len) == 4, "witx calculated offset");
10181018

10191019
/**
10201020
* Relative offset within a file.
@@ -3725,38 +3725,38 @@ typedef struct __wasi_thread_state_t {
37253725

37263726
} __wasi_thread_state_t;
37273727

3728-
_Static_assert(sizeof(__wasi_thread_state_t) == 200, "witx calculated size");
3729-
_Static_assert(_Alignof(__wasi_thread_state_t) == 8, "witx calculated align");
3728+
_Static_assert(sizeof(__wasi_thread_state_t) == 112, "witx calculated size");
3729+
_Static_assert(_Alignof(__wasi_thread_state_t) == 4, "witx calculated align");
37303730
_Static_assert(offsetof(__wasi_thread_state_t, thread_self) == 0, "witx calculated offset");
3731-
_Static_assert(offsetof(__wasi_thread_state_t, dtv) == 8, "witx calculated offset");
3732-
_Static_assert(offsetof(__wasi_thread_state_t, thread_prev) == 16, "witx calculated offset");
3733-
_Static_assert(offsetof(__wasi_thread_state_t, thread_next) == 24, "witx calculated offset");
3734-
_Static_assert(offsetof(__wasi_thread_state_t, sysinfo) == 32, "witx calculated offset");
3735-
_Static_assert(offsetof(__wasi_thread_state_t, canary) == 40, "witx calculated offset");
3736-
_Static_assert(offsetof(__wasi_thread_state_t, tid) == 48, "witx calculated offset");
3737-
_Static_assert(offsetof(__wasi_thread_state_t, errno_val) == 52, "witx calculated offset");
3738-
_Static_assert(offsetof(__wasi_thread_state_t, detach_state) == 56, "witx calculated offset");
3739-
_Static_assert(offsetof(__wasi_thread_state_t, cancel) == 60, "witx calculated offset");
3740-
_Static_assert(offsetof(__wasi_thread_state_t, canceldisable) == 64, "witx calculated offset");
3741-
_Static_assert(offsetof(__wasi_thread_state_t, cancelasync) == 65, "witx calculated offset");
3742-
_Static_assert(offsetof(__wasi_thread_state_t, flags) == 66, "witx calculated offset");
3743-
_Static_assert(offsetof(__wasi_thread_state_t, map_base) == 72, "witx calculated offset");
3744-
_Static_assert(offsetof(__wasi_thread_state_t, map_size) == 80, "witx calculated offset");
3745-
_Static_assert(offsetof(__wasi_thread_state_t, stack) == 88, "witx calculated offset");
3746-
_Static_assert(offsetof(__wasi_thread_state_t, stack_size) == 96, "witx calculated offset");
3747-
_Static_assert(offsetof(__wasi_thread_state_t, guard_size) == 104, "witx calculated offset");
3748-
_Static_assert(offsetof(__wasi_thread_state_t, result) == 112, "witx calculated offset");
3749-
_Static_assert(offsetof(__wasi_thread_state_t, cancelbuf) == 120, "witx calculated offset");
3750-
_Static_assert(offsetof(__wasi_thread_state_t, tsd) == 128, "witx calculated offset");
3751-
_Static_assert(offsetof(__wasi_thread_state_t, robust_list_head) == 136, "witx calculated offset");
3752-
_Static_assert(offsetof(__wasi_thread_state_t, robust_list_off) == 144, "witx calculated offset");
3753-
_Static_assert(offsetof(__wasi_thread_state_t, robust_list_pending) == 152, "witx calculated offset");
3754-
_Static_assert(offsetof(__wasi_thread_state_t, h_errno_val) == 160, "witx calculated offset");
3755-
_Static_assert(offsetof(__wasi_thread_state_t, timer_id) == 164, "witx calculated offset");
3756-
_Static_assert(offsetof(__wasi_thread_state_t, locale) == 168, "witx calculated offset");
3757-
_Static_assert(offsetof(__wasi_thread_state_t, killlock) == 176, "witx calculated offset");
3758-
_Static_assert(offsetof(__wasi_thread_state_t, dlerror_buf) == 184, "witx calculated offset");
3759-
_Static_assert(offsetof(__wasi_thread_state_t, stdio_locks) == 192, "witx calculated offset");
3731+
_Static_assert(offsetof(__wasi_thread_state_t, dtv) == 4, "witx calculated offset");
3732+
_Static_assert(offsetof(__wasi_thread_state_t, thread_prev) == 8, "witx calculated offset");
3733+
_Static_assert(offsetof(__wasi_thread_state_t, thread_next) == 12, "witx calculated offset");
3734+
_Static_assert(offsetof(__wasi_thread_state_t, sysinfo) == 16, "witx calculated offset");
3735+
_Static_assert(offsetof(__wasi_thread_state_t, canary) == 20, "witx calculated offset");
3736+
_Static_assert(offsetof(__wasi_thread_state_t, tid) == 24, "witx calculated offset");
3737+
_Static_assert(offsetof(__wasi_thread_state_t, errno_val) == 28, "witx calculated offset");
3738+
_Static_assert(offsetof(__wasi_thread_state_t, detach_state) == 32, "witx calculated offset");
3739+
_Static_assert(offsetof(__wasi_thread_state_t, cancel) == 36, "witx calculated offset");
3740+
_Static_assert(offsetof(__wasi_thread_state_t, canceldisable) == 40, "witx calculated offset");
3741+
_Static_assert(offsetof(__wasi_thread_state_t, cancelasync) == 41, "witx calculated offset");
3742+
_Static_assert(offsetof(__wasi_thread_state_t, flags) == 42, "witx calculated offset");
3743+
_Static_assert(offsetof(__wasi_thread_state_t, map_base) == 44, "witx calculated offset");
3744+
_Static_assert(offsetof(__wasi_thread_state_t, map_size) == 48, "witx calculated offset");
3745+
_Static_assert(offsetof(__wasi_thread_state_t, stack) == 52, "witx calculated offset");
3746+
_Static_assert(offsetof(__wasi_thread_state_t, stack_size) == 56, "witx calculated offset");
3747+
_Static_assert(offsetof(__wasi_thread_state_t, guard_size) == 60, "witx calculated offset");
3748+
_Static_assert(offsetof(__wasi_thread_state_t, result) == 64, "witx calculated offset");
3749+
_Static_assert(offsetof(__wasi_thread_state_t, cancelbuf) == 68, "witx calculated offset");
3750+
_Static_assert(offsetof(__wasi_thread_state_t, tsd) == 72, "witx calculated offset");
3751+
_Static_assert(offsetof(__wasi_thread_state_t, robust_list_head) == 76, "witx calculated offset");
3752+
_Static_assert(offsetof(__wasi_thread_state_t, robust_list_off) == 80, "witx calculated offset");
3753+
_Static_assert(offsetof(__wasi_thread_state_t, robust_list_pending) == 84, "witx calculated offset");
3754+
_Static_assert(offsetof(__wasi_thread_state_t, h_errno_val) == 88, "witx calculated offset");
3755+
_Static_assert(offsetof(__wasi_thread_state_t, timer_id) == 92, "witx calculated offset");
3756+
_Static_assert(offsetof(__wasi_thread_state_t, locale) == 96, "witx calculated offset");
3757+
_Static_assert(offsetof(__wasi_thread_state_t, killlock) == 100, "witx calculated offset");
3758+
_Static_assert(offsetof(__wasi_thread_state_t, dlerror_buf) == 104, "witx calculated offset");
3759+
_Static_assert(offsetof(__wasi_thread_state_t, stdio_locks) == 108, "witx calculated offset");
37603760

37613761
/**
37623762
* thread start type
@@ -3779,14 +3779,14 @@ typedef struct __wasi_thread_start_t {
37793779

37803780
} __wasi_thread_start_t;
37813781

3782-
_Static_assert(sizeof(__wasi_thread_start_t) == 24, "witx calculated size");
3783-
_Static_assert(_Alignof(__wasi_thread_start_t) == 8, "witx calculated align");
3782+
_Static_assert(sizeof(__wasi_thread_start_t) == 12, "witx calculated size");
3783+
_Static_assert(_Alignof(__wasi_thread_start_t) == 4, "witx calculated align");
37843784
_Static_assert(offsetof(__wasi_thread_start_t, start_funct) == 0, "witx calculated offset");
3785-
_Static_assert(offsetof(__wasi_thread_start_t, start_args) == 8, "witx calculated offset");
3786-
_Static_assert(offsetof(__wasi_thread_start_t, thread) == 16, "witx calculated offset");
3785+
_Static_assert(offsetof(__wasi_thread_start_t, start_args) == 4, "witx calculated offset");
3786+
_Static_assert(offsetof(__wasi_thread_start_t, thread) == 8, "witx calculated offset");
37873787

37883788
/**
3789-
* @defgroup wasix_64v1
3789+
* @defgroup wasix_32v1
37903790
* @{
37913791
*/
37923792

0 commit comments

Comments
 (0)