From e096dad4c54988dcd89e6507cf201e4fa1cad0ca Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Sat, 16 May 2020 01:46:07 +0100 Subject: [PATCH] reworked syscalls interface --- libraries/wutcrt/wut_crt.c | 12 +- libraries/wutnewlib/wut_newlib.c | 51 +++++--- libraries/wutstdc++/wut_gthread.cpp | 195 ++++++++++++++++++---------- libraries/wutstdc++/wut_stdcpp.cpp | 23 ---- share/wut.specs | 2 +- 5 files changed, 167 insertions(+), 116 deletions(-) delete mode 100644 libraries/wutstdc++/wut_stdcpp.cpp diff --git a/libraries/wutcrt/wut_crt.c b/libraries/wutcrt/wut_crt.c index fa15cdbf..82931af3 100644 --- a/libraries/wutcrt/wut_crt.c +++ b/libraries/wutcrt/wut_crt.c @@ -1,11 +1,11 @@ void __init_wut_malloc(); void __init_wut_newlib(); -extern void __init_wut_stdcpp() __attribute__((weak)); +extern void __init_wut_stdcpp(); void __init_wut_devoptab(); void __fini_wut_malloc(); void __fini_wut_newlib(); -extern void __fini_wut_stdcpp() __attribute__((weak)); +extern void __fini_wut_stdcpp(); void __fini_wut_devoptab(); void __attribute__((weak)) @@ -13,9 +13,9 @@ __init_wut() { __init_wut_malloc(); __init_wut_newlib(); - if (__init_wut_stdcpp) { + //if (__init_wut_stdcpp) { __init_wut_stdcpp(); - } + //} __init_wut_devoptab(); } @@ -23,9 +23,9 @@ void __attribute__((weak)) __fini_wut() { __fini_wut_devoptab(); - if (__fini_wut_stdcpp) { + //if (__fini_wut_stdcpp) { __fini_wut_stdcpp(); - } + //} __fini_wut_newlib(); __fini_wut_malloc(); } diff --git a/libraries/wutnewlib/wut_newlib.c b/libraries/wutnewlib/wut_newlib.c index 36232292..d4b61aca 100644 --- a/libraries/wutnewlib/wut_newlib.c +++ b/libraries/wutnewlib/wut_newlib.c @@ -1,35 +1,44 @@ #include "wut_newlib.h" #include -static void -__init_wut_syscall_array() -{ - //__syscalls.sbrk_r = __wut_sbrk_r; - __syscalls.lock_init = __wut_lock_init; - __syscalls.lock_close = __wut_lock_close; - __syscalls.lock_acquire = __wut_lock_acquire; - __syscalls.lock_release = __wut_lock_release; - //__syscalls.malloc_lock = __wut_malloc_lock; - //__syscalls.malloc_unlock = __wut_malloc_unlock; - //use coreinit's exit function - __syscalls.exit = RPLWRAP(exit); - __syscalls.gettod_r = __wut_gettod_r; - __syscalls.clock_gettime = __wut_clock_gettime; - __syscalls.clock_settime = __wut_clock_settime; - __syscalls.clock_getres = __wut_clock_getres; - __syscalls.nanosleep = __wut_nanosleep; +void(*__wut_exit)(int rc); + +int __syscall_lock_init(int *lock, int recursive) { + return __wut_lock_init(lock,recursive); } +int __syscall_lock_close(int *lock) { + return __wut_lock_close(lock); +} + +int __syscall_lock_release(int *lock) { + return __wut_lock_release(lock); +} + +int __syscall_lock_acquire(int *lock) { + return __wut_lock_acquire(lock); +} + +void __syscall_exit(int rc) { + __wut_exit(rc); +} + +int __syscall_gettod_r(struct _reent *ptr, struct timeval *tp, struct timezone *tz){ + return __wut_gettod_r(ptr,tp,tz); +} + +int __syscall_nanosleep(const struct timespec *req, struct timespec *rem){ + return __wut_nanosleep(req, rem); +} + + void __init_wut_newlib() { - //__init_wut_sbrk_heap(); - //__init_wut_malloc_lock(); - __init_wut_syscall_array(); + __wut_exit = RPLWRAP(exit); } void __fini_wut_newlib() { - //__fini_wut_sbrk_heap(); } diff --git a/libraries/wutstdc++/wut_gthread.cpp b/libraries/wutstdc++/wut_gthread.cpp index 9d08d647..9d628f1b 100644 --- a/libraries/wutstdc++/wut_gthread.cpp +++ b/libraries/wutstdc++/wut_gthread.cpp @@ -1,73 +1,138 @@ #include "wut_gthread.h" #include +#include -typedef int (* __gthread_fn_active) (void); -typedef int (* __gthread_fn_create) (__gthread_t *__threadid, void *(*__func) (void*), void *__args); -typedef int (* __gthread_fn_join) (__gthread_t __threadid, void **__value_ptr); -typedef int (* __gthread_fn_detach) (__gthread_t __threadid); -typedef int (* __gthread_fn_equal) (__gthread_t __t1, __gthread_t __t2); -typedef __gthread_t (* __gthread_fn_self) (void); -typedef int (* __gthread_fn_yield) (void); -typedef int (* __gthread_fn_once) (__gthread_once_t *__once, void (*__func) (void)); -typedef int (* __gthread_fn_key_create) (__gthread_key_t *__key, void (*__dtor) (void *)); -typedef int (* __gthread_fn_key_delete) (__gthread_key_t __key); -typedef void *(* __gthread_fn_getspecific) (__gthread_key_t __key); -typedef int (* __gthread_fn_setspecific) (__gthread_key_t __key, const void *__ptr); -typedef void (* __gthread_fn_mutex_init_function) (__gthread_mutex_t *__mutex); -typedef int (* __gthread_fn_mutex_destroy) (__gthread_mutex_t *__mutex); -typedef int (* __gthread_fn_mutex_lock) (__gthread_mutex_t *__mutex); -typedef int (* __gthread_fn_mutex_trylock) (__gthread_mutex_t *__mutex); -typedef int (* __gthread_fn_mutex_unlock) (__gthread_mutex_t *__mutex); -typedef int (* __gthread_fn_recursive_mutex_init_function) (__gthread_recursive_mutex_t *__mutex); -typedef int (* __gthread_fn_recursive_mutex_lock) (__gthread_recursive_mutex_t *__mutex); -typedef int (* __gthread_fn_recursive_mutex_trylock) (__gthread_recursive_mutex_t *__mutex); -typedef int (* __gthread_fn_recursive_mutex_unlock) (__gthread_recursive_mutex_t *__mutex); -typedef int (* __gthread_fn_recursive_mutex_destroy) (__gthread_recursive_mutex_t *__mutex); -typedef void (* __gthread_fn_cond_init_function) (__gthread_cond_t *__cond); -typedef int (* __gthread_fn_cond_broadcast) (__gthread_cond_t *__cond); -typedef int (* __gthread_fn_cond_signal) (__gthread_cond_t *__cond); -typedef int (* __gthread_fn_cond_wait) (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex); -typedef int (* __gthread_fn_cond_timedwait) (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex, const __gthread_time_t *__abs_timeout); -typedef int (* __gthread_fn_cond_wait_recursive) (__gthread_cond_t *__cond, __gthread_recursive_mutex_t *__mutex); -typedef int (* __gthread_fn_cond_destroy) (__gthread_cond_t* __cond); - -int -__wut_active_p() +static constexpr __wut_key_t gthread_key_to_wut(__gthread_key_t key) { + __wut_key_t dst={}; + static_assert(sizeof(__wut_key_t) == sizeof(__gthread_key_t), "Incompatible definition of thread local key type"); + __builtin_memcpy(&dst, &key, sizeof(dst)); + return dst; +} + +extern "C" { + +int __gthr_impl_active (void) { return 1; } -void -__init_wut_gthread() -{ - __gthread_impl.active = &__wut_active_p; - __gthread_impl.active = (__gthread_fn_active)__wut_active_p; - __gthread_impl.create = (__gthread_fn_create)__wut_thread_create; - __gthread_impl.join = (__gthread_fn_join)__wut_thread_join; - __gthread_impl.detach = (__gthread_fn_detach)__wut_thread_detach; - __gthread_impl.equal = (__gthread_fn_equal)__wut_thread_equal; - __gthread_impl.self = (__gthread_fn_self)__wut_thread_self; - __gthread_impl.yield = (__gthread_fn_yield)__wut_thread_yield; - __gthread_impl.once = (__gthread_fn_once)__wut_once; - __gthread_impl.key_create = (__gthread_fn_key_create)__wut_key_create; - __gthread_impl.key_delete = (__gthread_fn_key_delete)__wut_key_delete; - __gthread_impl.getspecific = (__gthread_fn_getspecific)__wut_getspecific; - __gthread_impl.setspecific = (__gthread_fn_setspecific)__wut_setspecific; - __gthread_impl.mutex_init_function = (__gthread_fn_mutex_init_function)__wut_mutex_init_function; - __gthread_impl.mutex_destroy = (__gthread_fn_mutex_destroy)__wut_mutex_destroy; - __gthread_impl.mutex_lock = (__gthread_fn_mutex_lock)__wut_mutex_lock; - __gthread_impl.mutex_trylock = (__gthread_fn_mutex_trylock)__wut_mutex_trylock; - __gthread_impl.mutex_unlock = (__gthread_fn_mutex_unlock)__wut_mutex_unlock; - __gthread_impl.recursive_mutex_init_function = (__gthread_fn_recursive_mutex_init_function)__wut_recursive_mutex_init_function; - __gthread_impl.recursive_mutex_lock = (__gthread_fn_recursive_mutex_lock)__wut_recursive_mutex_lock; - __gthread_impl.recursive_mutex_trylock = (__gthread_fn_recursive_mutex_trylock)__wut_recursive_mutex_trylock; - __gthread_impl.recursive_mutex_unlock = (__gthread_fn_recursive_mutex_unlock)__wut_recursive_mutex_unlock; - __gthread_impl.recursive_mutex_destroy = (__gthread_fn_recursive_mutex_destroy)__wut_recursive_mutex_destroy; - __gthread_impl.cond_init_function = (__gthread_fn_cond_init_function)__wut_cond_init_function; - __gthread_impl.cond_broadcast = (__gthread_fn_cond_broadcast)__wut_cond_broadcast; - __gthread_impl.cond_signal = (__gthread_fn_cond_signal)__wut_cond_signal; - __gthread_impl.cond_wait = (__gthread_fn_cond_wait)__wut_cond_wait; - __gthread_impl.cond_timedwait = (__gthread_fn_cond_timedwait)__wut_cond_timedwait; - __gthread_impl.cond_wait_recursive = (__gthread_fn_cond_wait_recursive)__wut_cond_wait_recursive; - __gthread_impl.cond_destroy = (__gthread_fn_cond_destroy)__wut_cond_destroy; +int __gthr_impl_create (__gthread_t *__threadid, void *(*__func) (void*), void *__args) { + return __wut_thread_create((OSThread**)__threadid, __func, __args); } + +int __gthr_impl_join (__gthread_t __threadid, void **__value_ptr) { + return __wut_thread_join((OSThread*)__threadid, __value_ptr); +} + +int __gthr_impl_detach (__gthread_t __threadid) { + return __wut_thread_detach((OSThread*)__threadid); +} + +int __gthr_impl_equal (__gthread_t __t1, __gthread_t __t2) { + return __wut_thread_equal( (OSThread*)__t1, (OSThread*)__t2); +} + +__gthread_t __gthr_impl_self (void) { + return __wut_thread_self(); +} + +int __gthr_impl_yield (void) { + return __wut_thread_yield(); +} + +int __gthr_impl_once (__gthread_once_t *__once, void (*__func) (void)) { + return __wut_once((__wut_once_t*)__once, __func); +} + +int __gth_impl_key_create (__gthread_key_t *__key, void (*__dtor) (void *)) { + return __wut_key_create((__wut_key_t*)__key, __dtor); +} + +int __gthr_impl_key_delete (__gthread_key_t __key) { + return __wut_key_delete( gthread_key_to_wut(__key) ); +} +void *__gthr_impl_getspecific (__gthread_key_t __key) { + return __wut_getspecific( gthread_key_to_wut(__key) ); +} +int __gthr_impl_setspecific (__gthread_key_t __key, const void *__ptr) { + return __wut_setspecific(gthread_key_to_wut(__key), __ptr); +} + +void __gthr_impl_mutex_init_function (__gthread_mutex_t *__mutex) { + return __wut_mutex_init_function((OSMutex*)__mutex); +} + +int __gthr_impl_mutex_destroy (__gthread_mutex_t *__mutex) { + return __wut_mutex_destroy((OSMutex*)__mutex); +} +int __gthr_impl_mutex_lock (__gthread_mutex_t *__mutex) { + return __wut_mutex_lock((OSMutex*)__mutex); +} + +int __gthr_impl_mutex_trylock (__gthread_mutex_t *__mutex) { + return __wut_mutex_trylock((OSMutex*)__mutex); +} + +int __gthr_impl_mutex_unlock (__gthread_mutex_t *__mutex) { + return __wut_mutex_unlock((OSMutex*)__mutex); +} + +int __gthr_impl_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex) { + return __wut_recursive_mutex_init_function((OSMutex*)__mutex); +} + +int __gthr_impl_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex) { + return __wut_recursive_mutex_lock((OSMutex*)__mutex); +} + +int __gthr_impl_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex) { + return __wut_recursive_mutex_trylock((OSMutex*)__mutex); +} + +int __gthr_impl_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex) { + return __wut_recursive_mutex_unlock((OSMutex*)__mutex); +} + +int __gthr_impl_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex) { + return __wut_recursive_mutex_destroy((OSMutex*)__mutex); +} + +void __gthr_impl_cond_init_function (__gthread_cond_t *__cond) { + return __wut_cond_init_function((OSCondition*)__cond); +} + +int __gthr_impl_cond_broadcast (__gthread_cond_t *__cond) { + return __wut_cond_broadcast((OSCondition*)__cond); +} + +int __gthr_impl_cond_signal (__gthread_cond_t *__cond) { + return __wut_cond_signal((OSCondition*)__cond); +} + +int __gthr_impl_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex) { + return __wut_cond_wait((OSCondition*)__cond, (OSMutex*)__mutex); +} + +int __gthr_impl_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex, const __gthread_time_t *__abs_timeout) { + return __wut_cond_timedwait((OSCondition*)__cond, (OSMutex*)__mutex, __abs_timeout); +} + +int __gthr_impl_cond_wait_recursive (__gthread_cond_t *__cond, __gthread_recursive_mutex_t *__mutex) { + return __wut_cond_wait_recursive((OSCondition*)__cond, (OSMutex*)__mutex); +} + +int __gthr_impl_cond_destroy (__gthread_cond_t* __cond) { + return __wut_cond_destroy((OSCondition*)__cond); +} + + +void __init_wut_stdcpp() +{ +} + +void __fini_wut_stdcpp() +{ +} + +} + diff --git a/libraries/wutstdc++/wut_stdcpp.cpp b/libraries/wutstdc++/wut_stdcpp.cpp deleted file mode 100644 index c6dc7370..00000000 --- a/libraries/wutstdc++/wut_stdcpp.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "wut_gthread.h" - -__asm__ ( - "\t.section\t.text.__wrap___gxx_personality_v0,\"ax\",@progbits\n" - "\t.align\t2\n" - "\t.globl\t__wrap___gxx_personality_v0\n" - "\t.type\t__wrap___gxx_personality_v0, @function\n" - "__wrap___gxx_personality_v0:\n" - "\t.cfi_startproc\n" - "\tb\t__real___gxx_personality_v0\n" - "\t.cfi_endproc" -); - -extern "C" void -__init_wut_stdcpp() -{ - __init_wut_gthread(); -} - -extern "C" void -__fini_wut_stdcpp() -{ -} diff --git a/share/wut.specs b/share/wut.specs index 6b44cfdb..75905bcd 100644 --- a/share/wut.specs +++ b/share/wut.specs @@ -1,4 +1,4 @@ %rename link old_link *link: -%(old_link) -T %:getenv(DEVKITPRO /wut/share/wut.ld) --gc-sections --emit-relocs -z nocopyreloc -wrap __gxx_personality_v0 %(wut_entry) +%(old_link) -T %:getenv(DEVKITPRO /wut/share/wut.ld) --gc-sections --emit-relocs -z nocopyreloc %(wut_entry)