Refactor Ryzen fix to fix compilation issues

This commit is contained in:
SChernykh 2019-12-31 11:55:07 +02:00
parent 29dd2c2138
commit c9f90e6770
5 changed files with 31 additions and 13 deletions

View file

@ -300,6 +300,10 @@ namespace randomx {
code = allocatedCode + (codeOffset.fetch_add(59 * 64) % CodeSize); code = allocatedCode + (codeOffset.fetch_add(59 * 64) % CodeSize);
memcpy(code, codePrologue, prologueSize); memcpy(code, codePrologue, prologueSize);
memcpy(code + epilogueOffset, codeEpilogue, epilogueSize); memcpy(code + epilogueOffset, codeEpilogue, epilogueSize);
# ifdef XMRIG_FIX_RYZEN
mainLoopBounds.first = code + prologueSize;
mainLoopBounds.second = code + epilogueOffset;
# endif
} }
JitCompilerX86::~JitCompilerX86() { JitCompilerX86::~JitCompilerX86() {
@ -386,7 +390,7 @@ namespace randomx {
} }
# ifdef XMRIG_FIX_RYZEN # ifdef XMRIG_FIX_RYZEN
xmrig::Rx::setMainLoopBounds(code + prologueSize, code + epilogueOffset); xmrig::Rx::setMainLoopBounds(mainLoopBounds);
# endif # endif
codePos = prologueSize; codePos = prologueSize;

View file

@ -69,6 +69,9 @@ namespace randomx {
int registerUsage[RegistersCount]; int registerUsage[RegistersCount];
uint8_t* allocatedCode; uint8_t* allocatedCode;
uint8_t* code; uint8_t* code;
# ifdef XMRIG_FIX_RYZEN
std::pair<const void*, const void*> mainLoopBounds;
# endif
int32_t codePos; int32_t codePos;
uint32_t vm_flags; uint32_t vm_flags;

View file

@ -58,18 +58,13 @@ public:
static void init(IRxListener *listener); static void init(IRxListener *listener);
# ifdef XMRIG_FIX_RYZEN # ifdef XMRIG_FIX_RYZEN
static inline const std::pair<const void*, const void*> &mainLoopBounds() { return m_mainLoopBounds; } static void setMainLoopBounds(const std::pair<const void*, const void*>& bounds);
static inline void setMainLoopBounds(const void* loopBegin, const void* loopEnd) { m_mainLoopBounds.first = loopBegin; m_mainLoopBounds.second = loopEnd; }
# endif # endif
private: private:
static void msrInit(const RxConfig &config); static void msrInit(const RxConfig &config);
static void msrDestroy(); static void msrDestroy();
static void setupMainLoopExceptionFrame(); static void setupMainLoopExceptionFrame();
# ifdef XMRIG_FIX_RYZEN
static thread_local std::pair<const void*, const void*> m_mainLoopBounds;
# endif
}; };

View file

@ -180,6 +180,9 @@ static bool wrmsr(const MsrItems &preset, bool save)
} }
#ifdef XMRIG_FIX_RYZEN
static thread_local std::pair<const void*, const void*> mainLoopBounds = { nullptr, nullptr };
static void MainLoopHandler(int sig, siginfo_t *info, void *ucontext) static void MainLoopHandler(int sig, siginfo_t *info, void *ucontext)
{ {
ucontext_t *ucp = (ucontext_t*) 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]); LOG_VERBOSE(YELLOW_BOLD("%s at %p"), (sig == SIGSEGV) ? "SIGSEGV" : "SIGILL", ucp->uc_mcontext.gregs[REG_RIP]);
void* p = reinterpret_cast<void*>(ucp->uc_mcontext.gregs[REG_RIP]); void* p = reinterpret_cast<void*>(ucp->uc_mcontext.gregs[REG_RIP]);
const std::pair<const void*, const void*>& loopBounds = Rx::mainLoopBounds(); const std::pair<const void*, const void*>& loopBounds = mainLoopBounds;
if ((loopBounds.first <= p) && (p < loopBounds.second)) { if ((loopBounds.first <= p) && (p < loopBounds.second)) {
ucp->uc_mcontext.gregs[REG_RIP] = reinterpret_cast<size_t>(loopBounds.second); ucp->uc_mcontext.gregs[REG_RIP] = reinterpret_cast<size_t>(loopBounds.second);
@ -197,8 +200,11 @@ static void MainLoopHandler(int sig, siginfo_t *info, void *ucontext)
} }
} }
void Rx::setMainLoopBounds(const std::pair<const void*, const void*>& bounds)
thread_local std::pair<const void*, const void*> Rx::m_mainLoopBounds = { nullptr, nullptr }; {
mainLoopBounds = bounds;
}
#endif
} // namespace xmrig } // namespace xmrig
@ -235,9 +241,11 @@ void xmrig::Rx::msrDestroy()
void xmrig::Rx::setupMainLoopExceptionFrame() void xmrig::Rx::setupMainLoopExceptionFrame()
{ {
# ifdef XMRIG_FIX_RYZEN
struct sigaction act = {}; struct sigaction act = {};
act.sa_sigaction = MainLoopHandler; act.sa_sigaction = MainLoopHandler;
act.sa_flags = SA_RESTART | SA_SIGINFO; act.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGSEGV, &act, nullptr); sigaction(SIGSEGV, &act, nullptr);
sigaction(SIGILL, &act, nullptr); sigaction(SIGILL, &act, nullptr);
# endif
} }

View file

@ -303,6 +303,9 @@ static bool wrmsr(const MsrItems &preset, bool save)
} }
#ifdef XMRIG_FIX_RYZEN
static thread_local std::pair<const void*, const void*> mainLoopBounds = { nullptr, nullptr };
static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo) static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo)
{ {
if (ExceptionInfo->ExceptionRecord->ExceptionCode == 0xC0000005) { if (ExceptionInfo->ExceptionRecord->ExceptionCode == 0xC0000005) {
@ -320,7 +323,7 @@ static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo)
} }
void* p = reinterpret_cast<void*>(ExceptionInfo->ContextRecord->Rip); void* p = reinterpret_cast<void*>(ExceptionInfo->ContextRecord->Rip);
const std::pair<const void*, const void*>& loopBounds = Rx::mainLoopBounds(); const std::pair<const void*, const void*>& loopBounds = mainLoopBounds;
if ((loopBounds.first <= p) && (p < loopBounds.second)) { if ((loopBounds.first <= p) && (p < loopBounds.second)) {
ExceptionInfo->ContextRecord->Rip = reinterpret_cast<DWORD64>(loopBounds.second); ExceptionInfo->ContextRecord->Rip = reinterpret_cast<DWORD64>(loopBounds.second);
@ -330,8 +333,11 @@ static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo)
return EXCEPTION_CONTINUE_SEARCH; return EXCEPTION_CONTINUE_SEARCH;
} }
void Rx::setMainLoopBounds(const std::pair<const void*, const void*>& bounds)
thread_local std::pair<const void*, const void*> Rx::m_mainLoopBounds = { nullptr, nullptr }; {
mainLoopBounds = bounds;
}
#endif
} // namespace xmrig } // namespace xmrig
@ -368,5 +374,7 @@ void xmrig::Rx::msrDestroy()
void xmrig::Rx::setupMainLoopExceptionFrame() void xmrig::Rx::setupMainLoopExceptionFrame()
{ {
# ifdef XMRIG_FIX_RYZEN
AddVectoredExceptionHandler(1, MainLoopHandler); AddVectoredExceptionHandler(1, MainLoopHandler);
# endif
} }