diff --git a/userspace/libraries/LibC/pthread.cpp b/userspace/libraries/LibC/pthread.cpp index c62c5439..8f3c1137 100644 --- a/userspace/libraries/LibC/pthread.cpp +++ b/userspace/libraries/LibC/pthread.cpp @@ -3,9 +3,6 @@ #include #include -#include -#include - #include #include #include @@ -33,21 +30,32 @@ static void _pthread_cancel_handler(int) } __attribute__((constructor)) -static void _install_main_thread_cancel_handler() +static void _init_pthread() { + uthread* uthread = _get_uthread(); + uthread->id = syscall(SYS_THREAD_GETID); + uthread->attr = s_default_pthread_attr; + uthread->errno_ = 0; + uthread->cancel_type = PTHREAD_CANCEL_DEFERRED; + uthread->cancel_state = PTHREAD_CANCEL_ENABLE; + uthread->canceled = false; + uthread->cleanup_funcs = nullptr; + memset(uthread->specific_keys, 0, sizeof(uthread->specific_keys)); + memset(uthread->specific_vals, 0, sizeof(uthread->specific_vals)); + signal(SIGCANCEL, &_pthread_cancel_handler); } // stack is 16 byte aligned on entry, this `call` is used to align it extern "C" void _pthread_trampoline(void*); asm( -#if ARCH(x86_64) +#if defined(__x86_64__) "_pthread_trampoline:" "popq %rdi;" "andq $-16, %rsp;" "xorq %rbp, %rbp;" "call _pthread_trampoline_cpp" -#elif ARCH(i686) +#elif defined(__i686__) "_pthread_trampoline:" "popl %edi;" "andl $-16, %esp;" @@ -1322,7 +1330,7 @@ extern "C" void* __tls_get_addr(tls_index* ti) return reinterpret_cast(uthread->dtv[ti->ti_module] + ti->ti_offset); } -#if ARCH(i686) +#if defined(__i686__) extern "C" void* __attribute__((__regparm__(1))) ___tls_get_addr(tls_index* ti) { auto* uthread = _get_uthread(); diff --git a/userspace/libraries/LibC/unistd.cpp b/userspace/libraries/LibC/unistd.cpp index 0d6fa71b..1c5b0389 100644 --- a/userspace/libraries/LibC/unistd.cpp +++ b/userspace/libraries/LibC/unistd.cpp @@ -58,42 +58,20 @@ extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t ::environ = environ; #if defined(__x86_64__) - if (uthread* self = reinterpret_cast(syscall(SYS_GET_FSBASE))) + if (syscall(SYS_GET_FSBASE) == 0) #elif defined(__i686__) - if (uthread* self = reinterpret_cast(syscall(SYS_GET_GSBASE))) + if (syscall(SYS_GET_GSBASE) == 0) #else #error #endif { - self->id = syscall(SYS_THREAD_GETID); - self->errno_ = 0; - self->cancel_type = PTHREAD_CANCEL_DEFERRED; - self->cancel_state = PTHREAD_CANCEL_ENABLE; - self->canceled = false; - self->cleanup_funs = nullptr; - } - else - { - alignas(uthread) static uint8_t storage[sizeof(uthread)]; - - uthread& uthread = *reinterpret_cast(storage); - uthread = { - .self = &uthread, - .master_tls_addr = nullptr, - .master_tls_size = 0, - .master_tls_module_count = 1, - .dynamic_tls = nullptr, - .id = static_cast(syscall(SYS_THREAD_GETID)), - .errno_ = 0, - .cancel_type = PTHREAD_CANCEL_DEFERRED, - .cancel_state = PTHREAD_CANCEL_ENABLE, - .canceled = false, - .cleanup_funs = nullptr, - .specific_keys = {}, - .specific_vals = {}, - .dtv = { 0 }, - }; - + static uthread uthread {}; + uthread.self = &uthread; + uthread.master_tls_addr = nullptr; + uthread.master_tls_size = 0; + uthread.master_tls_module_count = 0; + uthread.dynamic_tls = nullptr; + uthread.dtv[0] = 0; #if defined(__x86_64__) syscall(SYS_SET_FSBASE, &uthread); #elif defined(__i686__) @@ -108,7 +86,7 @@ extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t sigset_t ss; sigemptyset(&ss); - struct sigaction sa { + const struct sigaction sa { .sa_sigaction = __dump_backtrace, .sa_mask = ss, .sa_flags = SA_RESETHAND | SA_SIGINFO,