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);
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;

View file

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

View file

@ -58,18 +58,13 @@ public:
static void init(IRxListener *listener);
# ifdef XMRIG_FIX_RYZEN
static inline const std::pair<const void*, const void*> &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<const void*, const void*>& bounds);
# endif
private:
static void msrInit(const RxConfig &config);
static void msrDestroy();
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)
{
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<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)) {
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)
}
}
thread_local std::pair<const void*, const void*> Rx::m_mainLoopBounds = { nullptr, nullptr };
void Rx::setMainLoopBounds(const std::pair<const void*, const void*>& 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
}

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)
{
if (ExceptionInfo->ExceptionRecord->ExceptionCode == 0xC0000005) {
@ -320,7 +323,7 @@ static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo)
}
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)) {
ExceptionInfo->ContextRecord->Rip = reinterpret_cast<DWORD64>(loopBounds.second);
@ -330,8 +333,11 @@ static LONG WINAPI MainLoopHandler(_EXCEPTION_POINTERS *ExceptionInfo)
return EXCEPTION_CONTINUE_SEARCH;
}
thread_local std::pair<const void*, const void*> Rx::m_mainLoopBounds = { nullptr, nullptr };
void Rx::setMainLoopBounds(const std::pair<const void*, const void*>& 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
}