diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index f32465bb1..e521cf907 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 eabd6e070..02b1a80fc 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 1a26698d0..75ba85e17 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 c482ace56..c98510bda 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 d685592de..acfb7f01a 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 }