Skip to content

Commit

Permalink
Merge pull request #3559 from bylaws/ec-pt5
Browse files Browse the repository at this point in the history
LookupCache: Track ARM64EC page state in the code cache
  • Loading branch information
Sonicadvance1 committed Apr 9, 2024
2 parents eedb120 + 526e3e6 commit f0dad86
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions FEXCore/Source/Interface/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,14 @@ namespace FEXCore::Context {
FEXCORE_PROFILE_SCOPED("CompileBlock");
auto Thread = Frame->Thread;

#ifdef _M_ARM_64EC
// If the target PC is EC code, mark it in the L2 and return straight to the dispatcher
// so it can handle the call/return.
if (Thread->LookupCache->CheckPageEC(GuestRIP)) {
return GuestRIP;
}
#endif

// Invalidate might take a unique lock on this, to guarantee that during invalidation no code gets compiled
auto lk = GuardSignalDeferringSection<std::shared_lock>(CodeInvalidationMutex, Thread);

Expand Down
21 changes: 21 additions & 0 deletions FEXCore/Source/Interface/Core/LookupCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <stddef.h>
#include <utility>
#include <mutex>
#ifdef _M_ARM_64EC
#include <winnt.h>
#endif

namespace FEXCore {

Expand Down Expand Up @@ -68,6 +71,24 @@ class LookupCache {
return 0;
}

#ifdef _M_ARM_64EC
bool CheckPageEC(uint64_t Address) {
if (!RtlIsEcCode(Address)) {
return false;
}

std::lock_guard<std::recursive_mutex> lk(WriteLock);

// Mark L2 entry for this page as EC by setting the LSB, this can then be
// checked by the dispatcher to see if it needs to perform a call/return to
// EC code.
const auto PageIndex = (Address & (VirtualMemSize -1)) >> 12;
const auto Pointers = reinterpret_cast<uintptr_t*>(PagePointer);
Pointers[PageIndex] |= 1;
return true;
}
#endif

fextl::map<uint64_t, fextl::vector<uint64_t>> CodePages;

// Appends Block {Address} to CodePages [Start, Start + Length)
Expand Down

0 comments on commit f0dad86

Please sign in to comment.