From 0ac6b3e8f3c9c6a44909052ed539ebc359c9b3d8 Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Mon, 1 Jun 2026 20:20:58 +0200 Subject: [PATCH] CodeCache: Mark executable memory as EC code on ARM64EC See bd5b817c3a01b6881b7f2ba5be70cd391806bbaf. --- FEXCore/Source/Interface/Core/CodeCache.cpp | 18 +++++++++++++++++- Source/Windows/Common/ImageTracker.cpp | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp index 4e3a15aa1..9ca86c3ac 100644 --- a/FEXCore/Source/Interface/Core/CodeCache.cpp +++ b/FEXCore/Source/Interface/Core/CodeCache.cpp @@ -50,6 +50,10 @@ MappedCodeCacheFile::~MappedCodeCacheFile() { if (!CodeBuffer.empty()) { FEXCore::Allocator::munmap(CodeBuffer.data(), CodeBuffer.size_bytes()); } +#elif defined(_M_ARM64EC) + if (!CodeBuffer.empty()) { + FEXCore::Allocator::VirtualFree(CodeBuffer.data(), CodeBuffer.size_bytes()); + } #endif } @@ -600,7 +604,16 @@ CodeCache::LoadCache(std::span CacheFile, const ExecutableFileInfo& F return nullptr; } auto CodeBuffer = std::span {static_cast(CodeBufferAllocation), header.CodeBufferSize}; -#else +#elif defined(_M_ARM64EC) + // TODO: Implement lazy mapping on Windows + // NOTE: The executed code must have MEM_EXTENDED_PARAMETER_EC_CODE set, so we can't operate on the mapped cache file directly + void* CodeBufferAllocation = Allocator::VirtualAlloc(header.CodeBufferSize, true); + if (!CodeBufferAllocation) { + LogMan::Msg::EFmt("Failed to allocate code cache memory"); + return nullptr; + } + auto CodeBuffer = std::span {reinterpret_cast(CodeBufferAllocation), header.CodeBufferSize}; +#else // WoW64 // TODO: Implement lazy mapping on Windows auto CodeBuffer = CodeDataInFile; #endif @@ -870,6 +883,9 @@ void CodeCache::FinalizeCodePages(MappedCodeCacheFile& Code, std::span