Skip to content

Commit

Permalink
Merge pull request #3340 from Sonicadvance1/exitfunctionlink_data
Browse files Browse the repository at this point in the history
FEXCore: Describe exit function linking object with a structure
  • Loading branch information
Sonicadvance1 committed Dec 20, 2023
2 parents 3d2cbc5 + cf86ae6 commit 93ec676
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
9 changes: 7 additions & 2 deletions FEXCore/Source/Interface/Context/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> &delinker);

struct ExitFunctionLinkData {
uint64_t HostBranch;
uint64_t GuestRIP;
};

template<auto Fn>
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<std::shared_lock>(static_cast<ContextImpl*>(Thread->CTX)->CodeInvalidationMutex, Thread);

return Fn(Frame, record);
return Fn(Frame, Record);
}

// Wrapper which takes CpuStateFrame instead of InternalThreadState and unique_locks CodeInvalidationMutex
Expand Down
14 changes: 7 additions & 7 deletions FEXCore/Source/Interface/Core/JIT/Arm64/JIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
});
}

Expand Down

0 comments on commit 93ec676

Please sign in to comment.