Fixed code cache flushing on macOS

p2pool
SChernykh 3 years ago
parent fe236c782e
commit 69f2baec41

@ -112,6 +112,7 @@ endif()
include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)
include(CheckSymbolExists)
function(add_flag flag)
string(REPLACE "-" "_" supported_cxx ${flag}_cxx)
@ -216,6 +217,11 @@ if(ARM_ID STREQUAL "aarch64" OR ARM_ID STREQUAL "arm64" OR ARM_ID STREQUAL "armv
endif()
endif()
check_symbol_exists("__builtin___clear_cache" "stdlib.h" HAVE_BUILTIN_CLEAR_CACHE)
if (HAVE_BUILTIN_CLEAR_CACHE)
add_definitions(-DHAVE_BUILTIN_CLEAR_CACHE)
endif()
set(RANDOMX_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/src" CACHE STRING "RandomX Include path")
add_library(randomx ${randomx_sources})

@ -96,9 +96,7 @@ JitCompilerA64::JitCompilerA64()
memset(reg_changed_offset, 0, sizeof(reg_changed_offset));
memcpy(code, (void*) randomx_program_aarch64, CodeSize);
#ifdef __GNUC__
__builtin___clear_cache(reinterpret_cast<char*>(code), reinterpret_cast<char*>(code + CodeSize));
#endif
flushInstructionCache(code, code + CodeSize);
}
JitCompilerA64::~JitCompilerA64()
@ -167,9 +165,7 @@ void JitCompilerA64::generateProgram(Program& program, ProgramConfiguration& con
codePos = ((uint8_t*)randomx_program_aarch64_update_spMix1) - ((uint8_t*)randomx_program_aarch64);
emit32(ARMV8A::EOR | 10 | (IntRegMap[config.readReg0] << 5) | (IntRegMap[config.readReg1] << 16), code, codePos);
#ifdef __GNUC__
__builtin___clear_cache(reinterpret_cast<char*>(code + MainLoopBegin), reinterpret_cast<char*>(code + codePos));
#endif
flushInstructionCache(code + MainLoopBegin, code + codePos);
}
void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration& config, uint32_t datasetOffset)
@ -224,9 +220,7 @@ void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration
emit32(ARMV8A::ADD_IMM_LO | 2 | (2 << 5) | (imm_lo << 10), code, codePos);
emit32(ARMV8A::ADD_IMM_HI | 2 | (2 << 5) | (imm_hi << 10), code, codePos);
#ifdef __GNUC__
__builtin___clear_cache(reinterpret_cast<char*>(code + MainLoopBegin), reinterpret_cast<char*>(code + codePos));
#endif
flushInstructionCache(code + MainLoopBegin, code + codePos);
}
template<size_t N>
@ -342,9 +336,7 @@ void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N], s
memcpy(code + codePos, p1, p2 - p1);
codePos += p2 - p1;
#ifdef __GNUC__
__builtin___clear_cache(reinterpret_cast<char*>(code + CodeSize), reinterpret_cast<char*>(code + codePos));
#endif
flushInstructionCache(code + CodeSize, code + codePos);
}
template void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_ACCESSES], std::vector<uint64_t> &reciprocalCache);

@ -34,6 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <windows.h>
#else
#ifdef __APPLE__
#include <libkern/OSCacheControl.h>
#include <mach/vm_statistics.h>
#include <TargetConditionals.h>
# if defined(__aarch64__) && TARGET_OS_OSX
@ -195,3 +196,16 @@ void freePagedMemory(void* ptr, std::size_t bytes) {
munmap(ptr, bytes);
#endif
}
void flushInstructionCache(void* begin, void* end)
{
const size_t size = static_cast<size_t>(reinterpret_cast<char*>(end) - reinterpret_cast<char*>(begin));
#if defined(_WIN32) || defined(__CYGWIN__)
::FlushInstructionCache(GetCurrentProcess(), begin, size);
#elif defined(__APPLE__)
sys_icache_invalidate(begin, size);
#elif defined(__GNUC__) || defined(HAVE_BUILTIN_CLEAR_CACHE)
__builtin___clear_cache(reinterpret_cast<char*>(begin), reinterpret_cast<char*>(end));
#endif
}

@ -40,3 +40,4 @@ void setPagesRX(void*, std::size_t);
void setPagesRWX(void*, std::size_t);
void* allocLargePagesMemory(std::size_t);
void freePagedMemory(void*, std::size_t);
void flushInstructionCache(void* begin, void* end);

Loading…
Cancel
Save