From cf86ae6b657c9eadbe2c8bbe2d695e6830879f31 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 17 Dec 2023 18:24:00 -0800 Subject: [PATCH] FEXCore: Describe exit function linking object with a structure Instead of just poking raw uint64_t data values, describe it with a struct. This will be a read-only in the future. --- FEXCore/Source/Interface/Context/Context.h | 9 +++++++-- FEXCore/Source/Interface/Core/JIT/Arm64/JIT.cpp | 14 +++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/FEXCore/Source/Interface/Context/Context.h b/FEXCore/Source/Interface/Context/Context.h index f4a9d9512..0d4b9bafa 100644 --- a/FEXCore/Source/Interface/Context/Context.h +++ b/FEXCore/Source/Interface/Context/Context.h @@ -277,12 +277,17 @@ namespace FEXCore::Context { static void ThreadRemoveCodeEntry(FEXCore::Core::InternalThreadState *Thread, uint64_t GuestRIP); static void ThreadAddBlockLink(FEXCore::Core::InternalThreadState *Thread, uint64_t GuestDestination, uintptr_t HostLink, const std::function &delinker); + struct ExitFunctionLinkData { + uint64_t HostBranch; + uint64_t GuestRIP; + }; + template - static uint64_t ThreadExitFunctionLink(FEXCore::Core::CpuStateFrame *Frame, uint64_t *record) { + static uint64_t ThreadExitFunctionLink(FEXCore::Core::CpuStateFrame *Frame, ExitFunctionLinkData *Record) { auto Thread = Frame->Thread; auto lk = GuardSignalDeferringSection(static_cast(Thread->CTX)->CodeInvalidationMutex, Thread); - return Fn(Frame, record); + return Fn(Frame, Record); } // Wrapper which takes CpuStateFrame instead of InternalThreadState and unique_locks CodeInvalidationMutex diff --git a/FEXCore/Source/Interface/Core/JIT/Arm64/JIT.cpp b/FEXCore/Source/Interface/Core/JIT/Arm64/JIT.cpp index 104cc6ae8..980a931e8 100644 --- a/FEXCore/Source/Interface/Core/JIT/Arm64/JIT.cpp +++ b/FEXCore/Source/Interface/Core/JIT/Arm64/JIT.cpp @@ -480,9 +480,9 @@ void Arm64JITCore::Op_Unhandled(IR::IROp_Header const *IROp, IR::NodeID Node) { } -static uint64_t Arm64JITCore_ExitFunctionLink(FEXCore::Core::CpuStateFrame *Frame, uint64_t *record) { +static uint64_t Arm64JITCore_ExitFunctionLink(FEXCore::Core::CpuStateFrame *Frame, FEXCore::Context::ContextImpl::ExitFunctionLinkData *Record) { auto Thread = Frame->Thread; - auto GuestRip = record[1]; + auto GuestRip = Record->GuestRIP; auto HostCode = Thread->LookupCache->FindBlock(GuestRip); @@ -491,7 +491,7 @@ static uint64_t Arm64JITCore_ExitFunctionLink(FEXCore::Core::CpuStateFrame *Fram return Frame->Pointers.Common.DispatcherLoopTop; } - uintptr_t branch = (uintptr_t)(record) - 8; + uintptr_t branch = (uintptr_t)(Record) - 8; auto LinkerAddress = Frame->Pointers.Common.ExitFunctionLinker; auto offset = HostCode/4 - branch/4; @@ -503,7 +503,7 @@ static uint64_t Arm64JITCore_ExitFunctionLink(FEXCore::Core::CpuStateFrame *Fram FEXCore::ARMEmitter::Emitter::ClearICache((void*)branch, 24); // Add de-linking handler - Thread->LookupCache->AddBlockLink(GuestRip, (uintptr_t)record, [branch, LinkerAddress]{ + Thread->LookupCache->AddBlockLink(GuestRip, (uintptr_t)Record, [branch, LinkerAddress]{ FEXCore::ARMEmitter::Emitter emit((uint8_t*)(branch), 24); FEXCore::ARMEmitter::ForwardLabel l_BranchHost; emit.ldr(FEXCore::ARMEmitter::XReg::x0, &l_BranchHost); @@ -514,11 +514,11 @@ static uint64_t Arm64JITCore_ExitFunctionLink(FEXCore::Core::CpuStateFrame *Fram }); } else { // fallback case - do a soft-er link by patching the pointer - record[0] = HostCode; + Record->HostBranch = HostCode; // Add de-linking handler - Thread->LookupCache->AddBlockLink(GuestRip, (uintptr_t)record, [record, LinkerAddress]{ - record[0] = LinkerAddress; + Thread->LookupCache->AddBlockLink(GuestRip, (uintptr_t)Record, [Record, LinkerAddress]{ + Record->HostBranch = LinkerAddress; }); }