From c9f90e6770da3a67d1f2392cced6463bcd3bc50d Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 31 Dec 2019 11:55:07 +0200 Subject: [PATCH] Refactor Ryzen fix to fix compilation issues --- src/crypto/randomx/jit_compiler_x86.cpp | 6 +++++- src/crypto/randomx/jit_compiler_x86.hpp | 3 +++ src/crypto/rx/Rx.h | 7 +------ src/crypto/rx/Rx_linux.cpp | 14 +++++++++++--- src/crypto/rx/Rx_win.cpp | 14 +++++++++++--- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index f32465bb..e521cf90 100644 --- a/src/crypto/randomx/jit_compiler_x86.cpp +++ b/src/crypto/randomx/jit_compiler_x86.cpp @@ -300,6 +300,10 @@ namespace randomx { code = allocatedCode + (codeOffset.fetch_add(59 * 64) % CodeSize); memcpy(code, codePrologue, prologueSize); memcpy(code + epilogueOffset, codeEpilogue, epilogueSize); +# ifdef XMRIG_FIX_RYZEN + mainLoopBounds.first = code + prologueSize; + mainLoopBounds.second = code + epilogueOffset; +# endif } JitCompilerX86::~JitCompilerX86() { @@ -386,7 +390,7 @@ namespace randomx { } # ifdef XMRIG_FIX_RYZEN - xmrig::Rx::setMainLoopBounds(code + prologueSize, code + epilogueOffset); + xmrig::Rx::setMainLoopBounds(mainLoopBounds); # endif codePos = prologueSize; diff --git a/src/crypto/randomx/jit_compiler_x86.hpp b/src/crypto/randomx/jit_compiler_x86.hpp index eabd6e07..02b1a80f 100644 --- a/src/crypto/randomx/jit_compiler_x86.hpp +++ b/src/crypto/randomx/jit_compiler_x86.hpp @@ -69,6 +69,9 @@ namespace randomx { int registerUsage[RegistersCount]; uint8_t* allocatedCode; uint8_t* code; +# ifdef XMRIG_FIX_RYZEN + std::pair mainLoopBounds; +# endif int32_t codePos; uint32_t vm_flags; diff --git a/src/crypto/rx/Rx.h b/src/crypto/rx/Rx.h index 1a26698d..75ba85e1 100644 --- a/src/crypto/rx/Rx.h +++ b/src/crypto/rx/Rx.h @@ -58,18 +58,13 @@ public: static void init(IRxListener *listener); # ifdef XMRIG_FIX_RYZEN - static inline const std::pair &mainLoopBounds() { return m_mainLoopBounds; } - static inline void setMainLoopBounds(const void* loopBegin, const void* loopEnd) { m_mainLoopBounds.first = loopBegin; m_mainLoopBounds.second = loopEnd; } + static void setMainLoopBounds(const std::pair& bounds); # endif private: static void msrInit(const RxConfig &config); static void msrDestroy(); static void setupMainLoopExceptionFrame(); - -# ifdef XMRIG_FIX_RYZEN - static thread_local std::pair m_mainLoopBounds; -# endif }; diff --git a/src/crypto/rx/Rx_linux.cpp b/src/crypto/rx/Rx_linux.cpp index c482ace5..c98510bd 100644 --- a/src/crypto/rx/Rx_linux.cpp +++ b/src/crypto/rx/Rx_linux.cpp @@ -180,6 +180,9 @@ static bool wrmsr(const MsrItems &preset, bool save) } +#ifdef XMRIG_FIX_RYZEN +static thread_local std::pair mainLoopBounds = { nullptr, nullptr }; + static void MainLoopHandler(int sig, siginfo_t *info, void *ucontext) { ucontext_t *ucp = (ucontext_t*) ucontext; @@ -187,7 +190,7 @@ static void MainLoopHandler(int sig, siginfo_t *info, void *ucontext) LOG_VERBOSE(YELLOW_BOLD("%s at %p"), (sig == SIGSEGV) ? "SIGSEGV" : "SIGILL", ucp->uc_mcontext.gregs[REG_RIP]); void* p = reinterpret_cast(ucp->uc_mcontext.gregs[REG_RIP]); - const std::pair& loopBounds = Rx::mainLoopBounds(); + const std::pair& loopBounds = mainLoopBounds; if ((loopBounds.first <= p) && (p < loopBounds.second)) { ucp->uc_mcontext.gregs[REG_RIP] = reinterpret_cast(loopBounds.second); @@ -197,8 +200,11 @@ static void MainLoopHandler(int sig, siginfo_t *info, void *ucontext) } } - -thread_local std::pair Rx::m_mainLoopBounds = { nullptr, nullptr }; +void Rx::setMainLoopBounds(const std::pair& bounds) +{ + mainLoopBounds = bounds; +} +#endif } // namespace xmrig @@ -235,9 +241,11 @@ void xmrig::Rx::msrDestroy() void xmrig::Rx::setupMainLoopExceptionFrame() { +# ifdef XMRIG_FIX_RYZEN struct sigaction act = {}; act.sa_sigaction = MainLoopHandler; act.sa_flags = SA_RESTART | SA_SIGINFO; sigaction(SIGSEGV, &act, nullptr); sigaction(SIGILL, &act, nullptr); +# endif } diff --git a/src/crypto/rx/Rx_win.cpp b/src/crypto/rx/Rx_win.cpp index d685592d..acfb7f01 100644 --- a/src/crypto/rx/Rx_win.cpp +++ b/src/crypto/rx/Rx_win.cpp @@ -303,6 +303,9 @@ static bool wrmsr(const MsrItems &preset, bool save) } +#ifdef XMRIG_FIX_RYZEN +static thread_local std::pair mainLoopBounds = { nullptr, nullptr }; + static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo) { if (ExceptionInfo->ExceptionRecord->ExceptionCode == 0xC0000005) { @@ -320,7 +323,7 @@ static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo) } void* p = reinterpret_cast(ExceptionInfo->ContextRecord->Rip); - const std::pair& loopBounds = Rx::mainLoopBounds(); + const std::pair& loopBounds = mainLoopBounds; if ((loopBounds.first <= p) && (p < loopBounds.second)) { ExceptionInfo->ContextRecord->Rip = reinterpret_cast(loopBounds.second); @@ -330,8 +333,11 @@ static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo) return EXCEPTION_CONTINUE_SEARCH; } - -thread_local std::pair Rx::m_mainLoopBounds = { nullptr, nullptr }; +void Rx::setMainLoopBounds(const std::pair& bounds) +{ + mainLoopBounds = bounds; +} +#endif } // namespace xmrig @@ -368,5 +374,7 @@ void xmrig::Rx::msrDestroy() void xmrig::Rx::setupMainLoopExceptionFrame() { +# ifdef XMRIG_FIX_RYZEN AddVectoredExceptionHandler(1, MainLoopHandler); +# endif }