LibC: Cleanup uthread initialization
This is now done in a constructor function in pthread.cpp
This commit is contained in:
@@ -3,9 +3,6 @@
|
|||||||
#include <BAN/Debug.h>
|
#include <BAN/Debug.h>
|
||||||
#include <BAN/PlacementNew.h>
|
#include <BAN/PlacementNew.h>
|
||||||
|
|
||||||
#include <kernel/Arch.h>
|
|
||||||
#include <kernel/Thread.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -33,21 +30,32 @@ static void _pthread_cancel_handler(int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((constructor))
|
__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);
|
signal(SIGCANCEL, &_pthread_cancel_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
// stack is 16 byte aligned on entry, this `call` is used to align it
|
// stack is 16 byte aligned on entry, this `call` is used to align it
|
||||||
extern "C" void _pthread_trampoline(void*);
|
extern "C" void _pthread_trampoline(void*);
|
||||||
asm(
|
asm(
|
||||||
#if ARCH(x86_64)
|
#if defined(__x86_64__)
|
||||||
"_pthread_trampoline:"
|
"_pthread_trampoline:"
|
||||||
"popq %rdi;"
|
"popq %rdi;"
|
||||||
"andq $-16, %rsp;"
|
"andq $-16, %rsp;"
|
||||||
"xorq %rbp, %rbp;"
|
"xorq %rbp, %rbp;"
|
||||||
"call _pthread_trampoline_cpp"
|
"call _pthread_trampoline_cpp"
|
||||||
#elif ARCH(i686)
|
#elif defined(__i686__)
|
||||||
"_pthread_trampoline:"
|
"_pthread_trampoline:"
|
||||||
"popl %edi;"
|
"popl %edi;"
|
||||||
"andl $-16, %esp;"
|
"andl $-16, %esp;"
|
||||||
@@ -1322,7 +1330,7 @@ extern "C" void* __tls_get_addr(tls_index* ti)
|
|||||||
return reinterpret_cast<void*>(uthread->dtv[ti->ti_module] + ti->ti_offset);
|
return reinterpret_cast<void*>(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)
|
extern "C" void* __attribute__((__regparm__(1))) ___tls_get_addr(tls_index* ti)
|
||||||
{
|
{
|
||||||
auto* uthread = _get_uthread();
|
auto* uthread = _get_uthread();
|
||||||
|
|||||||
@@ -58,42 +58,20 @@ extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t
|
|||||||
::environ = environ;
|
::environ = environ;
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
if (uthread* self = reinterpret_cast<uthread*>(syscall(SYS_GET_FSBASE)))
|
if (syscall(SYS_GET_FSBASE) == 0)
|
||||||
#elif defined(__i686__)
|
#elif defined(__i686__)
|
||||||
if (uthread* self = reinterpret_cast<uthread*>(syscall(SYS_GET_GSBASE)))
|
if (syscall(SYS_GET_GSBASE) == 0)
|
||||||
#else
|
#else
|
||||||
#error
|
#error
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
self->id = syscall(SYS_THREAD_GETID);
|
static uthread uthread {};
|
||||||
self->errno_ = 0;
|
uthread.self = &uthread;
|
||||||
self->cancel_type = PTHREAD_CANCEL_DEFERRED;
|
uthread.master_tls_addr = nullptr;
|
||||||
self->cancel_state = PTHREAD_CANCEL_ENABLE;
|
uthread.master_tls_size = 0;
|
||||||
self->canceled = false;
|
uthread.master_tls_module_count = 0;
|
||||||
self->cleanup_funs = nullptr;
|
uthread.dynamic_tls = nullptr;
|
||||||
}
|
uthread.dtv[0] = 0;
|
||||||
else
|
|
||||||
{
|
|
||||||
alignas(uthread) static uint8_t storage[sizeof(uthread)];
|
|
||||||
|
|
||||||
uthread& uthread = *reinterpret_cast<struct uthread*>(storage);
|
|
||||||
uthread = {
|
|
||||||
.self = &uthread,
|
|
||||||
.master_tls_addr = nullptr,
|
|
||||||
.master_tls_size = 0,
|
|
||||||
.master_tls_module_count = 1,
|
|
||||||
.dynamic_tls = nullptr,
|
|
||||||
.id = static_cast<pthread_t>(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 },
|
|
||||||
};
|
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
syscall(SYS_SET_FSBASE, &uthread);
|
syscall(SYS_SET_FSBASE, &uthread);
|
||||||
#elif defined(__i686__)
|
#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;
|
sigset_t ss;
|
||||||
sigemptyset(&ss);
|
sigemptyset(&ss);
|
||||||
|
|
||||||
struct sigaction sa {
|
const struct sigaction sa {
|
||||||
.sa_sigaction = __dump_backtrace,
|
.sa_sigaction = __dump_backtrace,
|
||||||
.sa_mask = ss,
|
.sa_mask = ss,
|
||||||
.sa_flags = SA_RESETHAND | SA_SIGINFO,
|
.sa_flags = SA_RESETHAND | SA_SIGINFO,
|
||||||
|
|||||||
Reference in New Issue
Block a user