From 5e28b5342ba44dc7a4ced5bc1731ce01cd6ecc4a Mon Sep 17 00:00:00 2001 From: Stefanos Kornilios Mitsis Poiitidis Date: Thu, 12 Sep 2024 03:47:04 +0300 Subject: [PATCH] generate IR trampolines for HLE functions --- externals/hex-emu | 2 +- src/core/linker.cpp | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/externals/hex-emu b/externals/hex-emu index 0e5a75a7..710744fe 160000 --- a/externals/hex-emu +++ b/externals/hex-emu @@ -1 +1 @@ -Subproject commit 0e5a75a7f428d4cb58c44c3ec4c007b60de04dc7 +Subproject commit 710744fe5a9ede5452ecf7ca9a3e468da33703fd diff --git a/src/core/linker.cpp b/src/core/linker.cpp index bcc193d2..b42687c1 100644 --- a/src/core/linker.cpp +++ b/src/core/linker.cpp @@ -19,6 +19,7 @@ #include "core/tls.h" #include "core/virtual_memory.h" +#include #include #include #include "Config/Config.h" @@ -90,6 +91,13 @@ void AssertHandler(char const *Message) { fsync(OutputFD); } +std::unordered_set trampoline_entries; + +void GenerateTrampoline(u64 hle_handler) { + LOG_INFO(Core_Linker, "Adding trampoline {:#010x}", hle_handler); + trampoline_entries.insert(hle_handler); +} + static void RunMainEntry(VAddr addr, EntryParams* params, ExitFunc exit_func) { #ifdef ARCH_X86_64 // reinterpret_cast(addr)(params, exit_func); // can't be used, stack has to have @@ -118,9 +126,10 @@ static void RunMainEntry(VAddr addr, EntryParams* params, ExitFunc exit_func) { FEXCore::Config::Initialize(); FEXCore::Config::ReloadMetaLayer(); FEXCore::Config::Set(FEXCore::Config::CONFIG_IS64BIT_MODE, "1"); + FEXCore::Config::Set(FEXCore::Config::CONFIG_ENABLEAVX, "1"); InitializeStaticTables(FEXCore::Context::MODE_64BIT); - auto CTX = new FEXCore::Context::Context(); + FEXCore::Context::Context* CTX = new FEXCore::Context::Context(); CTX->CPUID.Init(CTX); // SyscallHandler->SetCodeLoader(&Loader); @@ -156,6 +165,25 @@ FEXCore::Config::ReloadMetaLayer(); rdi = (u64)exit_func; } + for (auto& entry : trampoline_entries) { + CTX->AddCustomIREntrypoint(entry, [entry](uintptr_t Entrypoint, FEXCore::IR::IREmitter* emit) { + using namespace FEXCore; + auto IRHeader = emit->_IRHeader(emit->Invalid(), 0); + auto Block = emit->CreateCodeNode(); + IRHeader.first->Blocks = emit->WrapNode(Block); + emit->SetCurrentCodeBlock(Block); + + const uint8_t GPRSize = 8; + + emit-> _Break(emit->_Constant(entry), FEXCore::IR::BreakDefinition { + .ErrorRegister = 0, + .Signal = 254, + .TrapNumber = 0, + .si_code = 0, + }); + }, nullptr, nullptr); + } + CTX->RunUntilExit(); UNIMPLEMENTED_MSG("Missing RunMainEntry() implementation for target CPU architecture."); @@ -392,6 +420,7 @@ bool Linker::Resolve(const std::string& name, Loader::SymbolType sym_type, Modul } if (record) { *return_info = *record; + GenerateTrampoline(return_info->virtual_address); return true; } @@ -405,6 +434,7 @@ bool Linker::Resolve(const std::string& name, Loader::SymbolType sym_type, Modul } LOG_ERROR(Core_Linker, "Linker: Stub resolved {} as {} (lib: {}, mod: {})", sr.name, return_info->name, library->name, module->name); + GenerateTrampoline(return_info->virtual_address); return false; }