diff --git a/Makefile b/Makefile index 8a1dbc8..2cabb1d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include $(TOPDIR)/share/wums_rules export WUMS_MAJOR := 0 export WUMS_MINOR := 3 -export WUMS_PATCH := 5 +export WUMS_PATCH := 6 VERSION := $(WUMS_MAJOR).$(WUMS_MINOR).$(WUMS_PATCH) @@ -102,10 +102,10 @@ lib: @[ -d $@ ] || mkdir -p $@ release: - @[ -d $@ ] || mkdir -p $@ + @$(shell [ ! -d 'release' ] && mkdir -p 'release') debug: - @[ -d $@ ] || mkdir -p $@ + @$(shell [ ! -d 'debug' ] && mkdir -p 'debug') lib/libwums.a : lib release $(SOURCES) $(INCLUDES) @$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \ diff --git a/include/wums/defines/module_defines.h b/include/wums/defines/module_defines.h index 2ebd6d3..cafaf4e 100644 --- a/include/wums/defines/module_defines.h +++ b/include/wums/defines/module_defines.h @@ -47,10 +47,10 @@ typedef struct module_information_single_t { uint32_t number_export_entries; hook_data_t * hook_entries; uint32_t number_hook_entries; - uint32_t bssAddr; - uint32_t bssSize; - uint32_t sbssAddr; - uint32_t sbssSize; + uint32_t bssAddr; // deprecated + uint32_t bssSize; // deprecated + uint32_t sbssAddr; // deprecated + uint32_t sbssSize; // deprecated uint32_t startAddress; uint32_t endAddress; uint32_t entrypoint; diff --git a/include/wums/hooks.h b/include/wums/hooks.h index b3c84ac..484381f 100644 --- a/include/wums/hooks.h +++ b/include/wums/hooks.h @@ -27,6 +27,7 @@ #include "common.h" #include "defines/module_defines.h" +#include "reent_internal.h" #ifdef __cplusplus extern "C" { @@ -68,7 +69,9 @@ typedef enum wums_hook_type_t { WUMS_HOOK_GET_CUSTOM_RPL_ALLOCATOR, // for internal usage only WUMS_HOOK_CLEAR_ALLOCATED_RPL_MEMORY, // for internal usage only // Introduced in 0.3.5 - WUMS_HOOK_INIT_WUT_THREAD // for internal usage only + WUMS_HOOK_INIT_WUT_THREAD, // for internal usage only + // Introduced in 0.3.6 + WUMS_HOOK_INIT_REENT_FUNCTIONS // for internal usage only } wums_hook_type_t; typedef uint32_t (*WUMSRPLAllocatorAllocFn)(int32_t size, int32_t align, void **outAddr); @@ -196,6 +199,14 @@ typedef struct wums_relocs_done_args_t { } \ WUMS_HOOK_EX(WUMS_HOOK_INIT_WUT_THREAD, on_init_wut_thread); +#define WUMS_INIT_REENT_FUNCTIONS() \ + __EXTERN_C_MACRO void WUMSReentAPI_InitInternal(wums_loader_init_reent_args_t_); \ + void wums_init_reent_functions(wums_loader_init_reent_args_t_ args); \ + WUMS_HOOK_EX(WUMS_HOOK_INIT_REENT_FUNCTIONS, wums_init_reent_functions); \ + void wums_init_reent_functions(wums_loader_init_reent_args_t_ args) { \ + return WUMSReentAPI_InitInternal(args); \ + } + #define WUMS_USE_WUT_SOCKETS() \ __EXTERN_C_MACRO void __init_wut_socket(); \ WUMS_HOOK_EX(WUMS_HOOK_INIT_WUT_SOCKETS, __init_wut_socket); \ diff --git a/include/wums/meta.h b/include/wums/meta.h index ebe5da7..40a420e 100644 --- a/include/wums/meta.h +++ b/include/wums/meta.h @@ -39,7 +39,7 @@ extern "C" { #endif -#define WUMS_VERSION "0.3.5" +#define WUMS_VERSION "0.3.6" #define WUMS_MODULE_EXPORT_NAME(__module_name) \ WUMS_META(export_name, __module_name); \ @@ -50,6 +50,7 @@ extern "C" { WUMS_USE_WUT_THREAD(); \ WUMS___INIT_WRAPPER(); \ WUMS___FINI_WRAPPER(); \ + WUMS_INIT_REENT_FUNCTIONS(); \ WUMS_META(buildtimestamp, __DATE__ " " __TIME__); \ extern const char wums_meta_module_name[] WUMS_SECTION("meta"); \ const char wums_meta_module_name[] = __module_name; \ diff --git a/include/wums/reent_internal.h b/include/wums/reent_internal.h new file mode 100644 index 0000000..0b85217 --- /dev/null +++ b/include/wums/reent_internal.h @@ -0,0 +1,25 @@ +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef bool (*WUMSReent_GetReentContext)(const void *moduleId, void **outPtr); +typedef bool (*WUMSReent_AddReentContext)(const void *moduleId, void *reentPtr, void (*cleanupFn)(void *)); + +typedef uint32_t WUMS_REENT_API_VERSION; + +#define WUMS_REENT_CUR_API_VERSION 0x02 + +typedef struct wums_loader_init_reent_args_t_ { + WUMS_REENT_API_VERSION version; + WUMSReent_GetReentContext get_context_ptr; + WUMSReent_AddReentContext add_reent_context_ptr; +} wums_loader_init_reent_args_t_; + +void WUMSReentAPI_InitInternal(wums_loader_init_reent_args_t_ args); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/include/wums/wums_debug.h b/include/wums/wums_debug.h new file mode 100644 index 0000000..87ded96 --- /dev/null +++ b/include/wums/wums_debug.h @@ -0,0 +1,19 @@ +#pragma once + +extern "C" void OSReport(const char *fmt, ...); + +#ifdef __cplusplus +extern "C" { +#endif +extern const char wums_meta_info_dump[]; +#ifdef __cplusplus +} +#endif + +#ifdef DEBUG +#define WUMS_DEBUG_REPORT(fmt, ...) OSReport("[%s] " fmt, wums_meta_info_dump, ##__VA_ARGS__) +#else +#define WUMS_DEBUG_REPORT(fmt, ...) +#endif + +#define WUMS_DEBUG_WARN(fmt, ...) OSReport("\033[33m[%s] " fmt "\033[0m", wums_meta_info_dump, ##__VA_ARGS__) \ No newline at end of file diff --git a/libraries/libwums/crt.cpp b/libraries/libwums/crt.cpp index 2d44ec9..0547304 100644 --- a/libraries/libwums/crt.cpp +++ b/libraries/libwums/crt.cpp @@ -1,5 +1,6 @@ +#include "wums/wums_debug.h" #include "wums_reent.h" -#include "wums_thread_specific.h" + #include #include @@ -43,16 +44,22 @@ struct _reent *__getreent(void) { return __wums_getreent(); } -extern "C" void __attribute__((weak)) wut_set_thread_specific(__wut_thread_specific_id id, void *value); - -void wut_set_thread_specific(__wut_thread_specific_id id, void *value) { - return wums_set_thread_specific(id, value); -} +typedef enum __wut_thread_specific_id { + WUT_THREAD_SPECIFIC_0 = 0, + WUT_THREAD_SPECIFIC_1 = 1, +} __wut_thread_specific_id; extern "C" void *__attribute__((weak)) wut_get_thread_specific(__wut_thread_specific_id id); void *wut_get_thread_specific(__wut_thread_specific_id id) { - return wums_get_thread_specific(id); + if ((uint32_t) id == 0x13371337) { // Mechanism to detect if the function was overridden properly + return (void *) 0x42424242; + } + + WUMS_DEBUG_WARN("wums_get_thread_specific: NOT SUPPORTED\n"); + OSFatal("wums_get_thread_specific: NOT SUPPORTED\n"); + + return nullptr; } extern "C" const char wums_meta_module_name[]; @@ -100,6 +107,8 @@ __assert_func(const char *file, } OSFatal(buffer); + while (true) + ; /* NOTREACHED */ } diff --git a/libraries/libwums/wums_reent.cpp b/libraries/libwums/wums_reent.cpp index 8b3d5ed..2c4e06d 100644 --- a/libraries/libwums/wums_reent.cpp +++ b/libraries/libwums/wums_reent.cpp @@ -1,78 +1,99 @@ -#include "wums_thread_specific.h" -#include -#include -#include - -#define __WUMS_CONTEXT_THREAD_SPECIFIC_ID WUT_THREAD_SPECIFIC_1 - -typedef uint32_t OSThread; +#include "wums_reent.h" -extern const char wums_meta_info_dump[]; +#include "wums/hooks.h" +#include "wums/reent_internal.h" +#include "wums/wums_debug.h" -extern "C" void OSFatal(const char *); -extern "C" void OSReport(const char *, ...); +#include -extern "C" OSThread *OSGetCurrentThread(); +extern "C" void OSFatal(const char *format, ...); +extern "C" void *OSGetCurrentThread(); -typedef void (*OSThreadCleanupCallbackFn)(OSThread *thread, void *stack); +typedef void *(*MEMAllocFromDefaultHeapFn)(uint32_t size); +typedef void *(*MEMAllocFromDefaultHeapExFn)(uint32_t size, int32_t alignment); +typedef void (*MEMFreeToDefaultHeapFn)(void *ptr); -extern "C" OSThreadCleanupCallbackFn -OSSetThreadCleanupCallback(OSThread *thread, - OSThreadCleanupCallbackFn callback); +extern MEMAllocFromDefaultHeapFn MEMAllocFromDefaultHeap; +extern MEMAllocFromDefaultHeapExFn MEMAllocFromDefaultHeapEx; +extern MEMFreeToDefaultHeapFn MEMFreeToDefaultHeap; -struct __wums_thread_context { - struct _reent reent; - OSThreadCleanupCallbackFn savedCleanup; +struct wums_loader_init_reent_args_t { + WUMSReent_GetReentContext get_context_ptr = nullptr; + WUMSReent_AddReentContext add_reent_context_ptr = nullptr; }; -static void -__wums_thread_cleanup(OSThread *thread, - void *stack) { - struct __wums_thread_context *context; +static wums_loader_init_reent_args_t __internal_functions = {}; - context = (struct __wums_thread_context *) wums_get_thread_specific(__WUMS_CONTEXT_THREAD_SPECIFIC_ID); - if (!context || &context->reent == _GLOBAL_REENT) { - OSReport("[%s] __wums_thread_cleanup: Context was NULL or reent was global\n", wums_meta_info_dump); - OSFatal("__wums_thread_cleanup: Context was NULL or reent was global"); +void WUMSReentAPI_InitInternal(wums_loader_init_reent_args_t_ args) { + if (args.version > WUMS_REENT_CUR_API_VERSION) { + OSFatal("Incompatible reent api version"); + return; } + WUMS_DEBUG_REPORT("WUMSReentAPI_InitInternal: Initializing reent module\n"); - if (context->savedCleanup) { - context->savedCleanup(thread, stack); - } + __internal_functions.get_context_ptr = args.get_context_ptr; + __internal_functions.add_reent_context_ptr = args.add_reent_context_ptr; +} + +// use variable in the .data section as unique module id +static const int sReentModuleId = 0; - _reclaim_reent(&context->reent); +bool wums_backend_get_context(const void *id, void **outPtr) { + return __internal_functions.get_context_ptr(id, outPtr); +} - // Use global reent during free since the current reent is getting freed - wums_set_thread_specific(__WUMS_CONTEXT_THREAD_SPECIFIC_ID, _GLOBAL_REENT); +bool wums_backend_register_context(const void *moduleId, void *reentPtr, void (*cleanupFn)(void *)) { + return __internal_functions.add_reent_context_ptr(moduleId, reentPtr, cleanupFn); +} - free(context); +static void reclaim_reent_trampoline(void *payload) { + WUMS_DEBUG_REPORT("reclaim_reent_trampoline: Destroying reent payload: %p\n", payload); - wums_set_thread_specific(__WUMS_CONTEXT_THREAD_SPECIFIC_ID, NULL); + if (payload) { + auto *reentPtr = static_cast<_reent *>(payload); + _reclaim_reent(reentPtr); + + // Make sure to use MEMFreeToDefaultHeap + MEMFreeToDefaultHeap(reentPtr); + } } -struct _reent * -__wums_getreent(void) { - struct __wums_thread_context *context; +struct _reent *__wums_getreent() { + if (!OSGetCurrentThread()) { + return _GLOBAL_REENT; + } + void *existingCtx = nullptr; + if (!wums_backend_get_context(&sReentModuleId, &existingCtx)) { + return _GLOBAL_REENT; + } + // if non-null we can use it + if (existingCtx) { + return static_cast<_reent *>(existingCtx); + } + + // Use `MEMAllocFromDefaultHeap` to avoid creating a new reent for allocating the reent + auto *newReent = static_cast(MEMAllocFromDefaultHeap(sizeof(struct _reent))); + if (!newReent) { + return _GLOBAL_REENT; + } - context = (struct __wums_thread_context *) wums_get_thread_specific(__WUMS_CONTEXT_THREAD_SPECIFIC_ID); - if (!context) { - // Temporarily use global reent during context allocation - wums_set_thread_specific(__WUMS_CONTEXT_THREAD_SPECIFIC_ID, _GLOBAL_REENT); + WUMS_DEBUG_REPORT("Allocated context! for thread %p: %p\n", OSGetCurrentThread(), newReent); - context = (struct __wums_thread_context *) malloc(sizeof(*context)); - if (!context) { - OSReport("[%s] __wums_getreent: Failed to allocate reent context\n", wums_meta_info_dump); - OSFatal("__wums_getreent: Failed to allocate reent context"); - wums_set_thread_specific(__WUMS_CONTEXT_THREAD_SPECIFIC_ID, NULL); - return NULL; - } + _REENT_INIT_PTR(newReent); - _REENT_INIT_PTR(&context->reent); - context->savedCleanup = OSSetThreadCleanupCallback(OSGetCurrentThread(), &__wums_thread_cleanup); + const bool result = wums_backend_register_context( + &sReentModuleId, + newReent, + reclaim_reent_trampoline); - wums_set_thread_specific(__WUMS_CONTEXT_THREAD_SPECIFIC_ID, context); + if (!result) { + WUMS_DEBUG_WARN("Failed to register context for thread %p\n", OSGetCurrentThread()); + _reclaim_reent(newReent); + // Make sure to call the free function + MEMFreeToDefaultHeap(newReent); + return _GLOBAL_REENT; } - return &context->reent; -} + return newReent; +} \ No newline at end of file diff --git a/libraries/libwums/wums_thread_specific.cpp b/libraries/libwums/wums_thread_specific.cpp deleted file mode 100644 index 21fe6a1..0000000 --- a/libraries/libwums/wums_thread_specific.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "wums_thread_specific.h" -#include -#include - -extern "C" void OSFatal(const char *); -extern "C" void OSReport(const char *, ...); - -typedef struct OSThread { - uint8_t other[0x68c]; - void *reserved[5]; -} OSThread; - -static_assert(offsetof(OSThread, reserved) == 0x68c, "OSThread: \"reserved\" at wrong offset"); -static_assert(sizeof(OSThread) == 0x6a0, "OSThread: wrong size"); - -extern "C" const char wums_meta_info_dump[]; - -void wums_set_thread_specific(__wut_thread_specific_id id, void *value) { - OSThread *thread; - asm volatile("lwz %0, -0x20(0)" - : "=r"(thread)); // OSGetCurrentThread() - if (thread != nullptr) { - if (id == WUT_THREAD_SPECIFIC_0) { - thread->reserved[3] = value; - } else if (id == WUT_THREAD_SPECIFIC_1) { - thread->reserved[4] = value; - } else { - OSReport("[%s] wums_set_thread_specific: invalid id\n", wums_meta_info_dump); - OSFatal("wut_set_thread_specific: invalid id"); - } - } else { - OSReport("[%s] wums_set_thread_specific: invalid thread\n", wums_meta_info_dump); - OSFatal("wums_set_thread_specific: invalid thread"); - } -} - -void *wums_get_thread_specific(__wut_thread_specific_id id) { - OSThread *thread; - asm volatile("lwz %0, -0x20(0)" - : "=r"(thread)); // OSGetCurrentThread() - if (thread != nullptr) { - if (id == WUT_THREAD_SPECIFIC_0) { - return thread->reserved[3]; - } else if (id == WUT_THREAD_SPECIFIC_1) { - return thread->reserved[4]; - } else if ((uint32_t) id == 0x13371337) { // Mechanism to detect if the function was overridden properly - return (void *) 0x42424242; - } else { - OSReport("[%s] wums_get_thread_specific: invalid id\n", wums_meta_info_dump); - OSFatal("wums_get_thread_specific: invalid id"); - } - } else { - OSReport("[%s] wums_get_thread_specific: invalid thread\n", wums_meta_info_dump); - OSFatal("wums_get_thread_specific: invalid thread\n"); - } - return nullptr; -} \ No newline at end of file diff --git a/libraries/libwums/wums_thread_specific.h b/libraries/libwums/wums_thread_specific.h deleted file mode 100644 index 6a7d922..0000000 --- a/libraries/libwums/wums_thread_specific.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -typedef enum __wut_thread_specific_id { - WUT_THREAD_SPECIFIC_0 = 0, - WUT_THREAD_SPECIFIC_1 = 1, -} __wut_thread_specific_id; - -#ifdef __cplusplus -extern "C" { -#endif - -void wums_set_thread_specific(__wut_thread_specific_id id, void *value); - -void *wums_get_thread_specific(__wut_thread_specific_id id); - -#ifdef __cplusplus -} -#endif \ No newline at end of file