diff --git a/FEXCore/Source/Interface/Context/Context.h b/FEXCore/Source/Interface/Context/Context.h index 137f8a2bea..a292fb14a1 100644 --- a/FEXCore/Source/Interface/Context/Context.h +++ b/FEXCore/Source/Interface/Context/Context.h @@ -223,9 +223,6 @@ class ContextImpl final : public FEXCore::Context::Context { FEX_CONFIG_OPT(SMCChecks, SMCCHECKS); FEX_CONFIG_OPT(MaxInstPerBlock, MAXINST); FEX_CONFIG_OPT(RootFSPath, ROOTFS); - FEX_CONFIG_OPT(ThunkHostLibsPath, THUNKHOSTLIBS); - FEX_CONFIG_OPT(ThunkHostLibsPath32, THUNKHOSTLIBS32); - FEX_CONFIG_OPT(ThunkConfigFile, THUNKCONFIG); FEX_CONFIG_OPT(GlobalJITNaming, GLOBALJITNAMING); FEX_CONFIG_OPT(LibraryJITNaming, LIBRARYJITNAMING); FEX_CONFIG_OPT(BlockJITNaming, BLOCKJITNAMING); diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp index 93b2e1f1fe..9075db493e 100644 --- a/FEXCore/Source/Interface/Core/Core.cpp +++ b/FEXCore/Source/Interface/Core/Core.cpp @@ -392,7 +392,7 @@ void ContextImpl::ExecuteThread(FEXCore::Core::InternalThreadState* Thread) { void ContextImpl::InitializeThreadTLSData(FEXCore::Core::InternalThreadState* Thread) { // Let's do some initial bookkeeping here if (ThunkHandler) { - ThunkHandler->RegisterTLSState(Thread); + ThunkHandler->RegisterTLSState(this, Thread); } #ifndef _WIN32 Alloc::OSAllocator::RegisterTLSData(Thread); diff --git a/FEXCore/Source/Interface/HLE/Thunks/Thunks.cpp b/FEXCore/Source/Interface/HLE/Thunks/Thunks.cpp index 9e0eaa24de..058202d7e0 100644 --- a/FEXCore/Source/Interface/HLE/Thunks/Thunks.cpp +++ b/FEXCore/Source/Interface/HLE/Thunks/Thunks.cpp @@ -8,6 +8,7 @@ tags: glue|thunks #include #include +#include #include #include #include @@ -22,10 +23,8 @@ tags: glue|thunks #endif #include -#include "FEXCore/Core/X86Enums.h" #include #include -#include #include #include #include @@ -76,8 +75,15 @@ struct LoadlibArgs { const char* Name; }; -static thread_local FEXCore::Core::InternalThreadState* Thread = nullptr; +struct ThunkHandler_impl; +struct TEMP_TLS_DATA { + FEXCore::Core::InternalThreadState* Thread {}; + ThunkHandler_impl* ThunkHandler {}; + FEXCore::Context::Context* CTX {}; +}; + +static thread_local TEMP_TLS_DATA ThreadData {}; struct ExportEntry { uint8_t* sha256; @@ -165,23 +171,22 @@ struct ThunkHandler_impl final : public ThunkHandler { Set arg0/1 to arg regs, use CTX::HandleCallback to handle the callback */ static void CallCallback(void* callback, void* arg0, void* arg1) { - if (!Thread) { + if (!ThreadData.Thread) { ERROR_AND_DIE_FMT("Thunked library attempted to invoke guest callback asynchronously"); } - auto CTX = static_cast(Thread->CTX); - if (CTX->Config.Is64BitMode) { - Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RDI] = (uintptr_t)arg0; - Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RSI] = (uintptr_t)arg1; + if (ThreadData.ThunkHandler->Is64BitMode()) { + ThreadData.Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RDI] = (uintptr_t)arg0; + ThreadData.Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RSI] = (uintptr_t)arg1; } else { if ((reinterpret_cast(arg1) >> 32) != 0) { ERROR_AND_DIE_FMT("Tried to call guest function with arguments packed to a 64-bit address"); } - Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RCX] = (uintptr_t)arg0; - Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RDX] = (uintptr_t)arg1; + ThreadData.Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RCX] = (uintptr_t)arg0; + ThreadData.Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RDX] = (uintptr_t)arg1; } - Thread->CTX->HandleCallback(Thread, (uintptr_t)callback); + ThreadData.CTX->HandleCallback(ThreadData.Thread, (uintptr_t)callback); } /** @@ -201,8 +206,7 @@ struct ThunkHandler_impl final : public ThunkHandler { }; auto args = reinterpret_cast(argsv); - auto CTX = static_cast(Thread->CTX); - CTX->AddThunkTrampolineIRHandler(args->original_callee, args->target_addr); + ThreadData.CTX->AddThunkTrampolineIRHandler(args->original_callee, args->target_addr); } /** @@ -247,13 +251,13 @@ struct ThunkHandler_impl final : public ThunkHandler { } static void LoadLib(void* ArgsV) { - auto CTX = static_cast(Thread->CTX); - auto Args = reinterpret_cast(ArgsV); std::string_view Name = Args->Name; - auto SOName = (CTX->Config.Is64BitMode() ? CTX->Config.ThunkHostLibsPath() : CTX->Config.ThunkHostLibsPath32()) + "/" + Name.data() + "-host.so"; + auto SOName = + (ThreadData.ThunkHandler->Is64BitMode() ? ThreadData.ThunkHandler->ThunkHostLibsPath() : ThreadData.ThunkHandler->ThunkHostLibsPath32()) + + "/" + Name.data() + "-host.so"; LogMan::Msg::DFmt("LoadLib: {} -> {}", Name, SOName); @@ -281,16 +285,14 @@ struct ThunkHandler_impl final : public ThunkHandler { Name); } - auto That = reinterpret_cast(CTX->ThunkHandler.get()); - { - std::lock_guard lk(That->ThunksMutex); + std::lock_guard lk(ThreadData.ThunkHandler->ThunksMutex); - That->Libs.insert(fextl::string {Name}); + ThreadData.ThunkHandler->Libs.insert(fextl::string {Name}); int i; for (i = 0; Exports[i].sha256; i++) { - That->Thunks[*reinterpret_cast(Exports[i].sha256)] = Exports[i].Fn; + ThreadData.ThunkHandler->Thunks[*reinterpret_cast(Exports[i].sha256)] = Exports[i].Fn; } LogMan::Msg::DFmt("Loaded {} syms", i); @@ -305,12 +307,9 @@ struct ThunkHandler_impl final : public ThunkHandler { auto& [Name, rv] = *reinterpret_cast(ArgsRV); - auto CTX = static_cast(Thread->CTX); - auto That = reinterpret_cast(CTX->ThunkHandler.get()); - { - std::shared_lock lk(That->ThunksMutex); - rv = That->Libs.contains(Name); + std::shared_lock lk(ThreadData.ThunkHandler->ThunksMutex); + rv = ThreadData.ThunkHandler->Libs.contains(Name); } } @@ -327,8 +326,10 @@ struct ThunkHandler_impl final : public ThunkHandler { } } - void RegisterTLSState(FEXCore::Core::InternalThreadState* _Thread) override { - Thread = _Thread; + void RegisterTLSState(FEXCore::Context::Context* CTX, FEXCore::Core::InternalThreadState* _Thread) override { + ThreadData.Thread = _Thread; + ThreadData.ThunkHandler = this; + ThreadData.CTX = CTX; } void AppendThunkDefinitions(std::span Definitions) override { @@ -336,6 +337,10 @@ struct ThunkHandler_impl final : public ThunkHandler { Thunks.emplace(Definition.Sum, Definition.ThunkFunction); } } + + FEX_CONFIG_OPT(Is64BitMode, IS64BIT_MODE); + FEX_CONFIG_OPT(ThunkHostLibsPath, THUNKHOSTLIBS); + FEX_CONFIG_OPT(ThunkHostLibsPath32, THUNKHOSTLIBS32); }; fextl::unique_ptr ThunkHandler::Create() { @@ -369,8 +374,7 @@ FEX_DEFAULT_VISIBILITY HostToGuestTrampolinePtr* MakeHostTrampolineForGuestFunction(void* HostPacker, uintptr_t GuestTarget, uintptr_t GuestUnpacker) { LOGMAN_THROW_AA_FMT(GuestTarget, "Tried to create host-trampoline to null pointer guest function"); - const auto CTX = static_cast(Thread->CTX); - const auto ThunkHandler = reinterpret_cast(CTX->ThunkHandler.get()); + const auto ThunkHandler = reinterpret_cast(ThreadData.ThunkHandler); const GuestcallInfo gci = {GuestUnpacker, GuestTarget}; @@ -436,15 +440,15 @@ FEX_DEFAULT_VISIBILITY void FinalizeHostTrampolineForGuestFunction(HostToGuestTr } FEX_DEFAULT_VISIBILITY void* GetGuestStack() { - if (!Thread) { + if (!ThreadData.Thread) { ERROR_AND_DIE_FMT("Thunked library attempted to query guest stack pointer asynchronously"); } - return (void*)(uintptr_t)((Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RSP])); + return (void*)(uintptr_t)((ThreadData.Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RSP])); } FEX_DEFAULT_VISIBILITY void MoveGuestStack(uintptr_t NewAddress) { - if (!Thread) { + if (!ThreadData.Thread) { ERROR_AND_DIE_FMT("Thunked library attempted to query guest stack pointer asynchronously"); } @@ -452,7 +456,7 @@ FEX_DEFAULT_VISIBILITY void MoveGuestStack(uintptr_t NewAddress) { ERROR_AND_DIE_FMT("Tried to set stack pointer for 32-bit guest to a 64-bit address"); } - Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RSP] = NewAddress; + ThreadData.Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RSP] = NewAddress; } #else diff --git a/FEXCore/Source/Interface/HLE/Thunks/Thunks.h b/FEXCore/Source/Interface/HLE/Thunks/Thunks.h index 6977924f53..de31da425f 100644 --- a/FEXCore/Source/Interface/HLE/Thunks/Thunks.h +++ b/FEXCore/Source/Interface/HLE/Thunks/Thunks.h @@ -7,13 +7,12 @@ tags: glue|thunks #pragma once -#include "Interface/IR/IR.h" - #include #include +#include namespace FEXCore::Context { -class ContextImpl; +class Context; } namespace FEXCore::Core { @@ -30,7 +29,7 @@ typedef void ThunkedFunction(void* ArgsRv); class ThunkHandler { public: virtual ThunkedFunction* LookupThunk(const IR::SHA256Sum& sha256) = 0; - virtual void RegisterTLSState(FEXCore::Core::InternalThreadState* Thread) = 0; + virtual void RegisterTLSState(FEXCore::Context::Context* CTX, FEXCore::Core::InternalThreadState* Thread) = 0; virtual ~ThunkHandler() {} static fextl::unique_ptr Create();