diff --git a/src/crypto/randomx/dataset.cpp b/src/crypto/randomx/dataset.cpp index 4acf366a..f03cd3c0 100644 --- a/src/crypto/randomx/dataset.cpp +++ b/src/crypto/randomx/dataset.cpp @@ -110,6 +110,7 @@ namespace randomx { cache->jit->generateSuperscalarHash(cache->programs); cache->jit->generateDatasetInitCode(); + cache->datasetInit = cache->jit->getDatasetInitFunc(); # ifdef XMRIG_SECURE_JIT cache->jit->enableExecution(); diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index b38491db..50e39c50 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -98,14 +98,10 @@ static size_t CalcDatasetItemSize() constexpr uint32_t IntRegMap[8] = { 4, 5, 6, 7, 12, 13, 14, 15 }; -JitCompilerA64::JitCompilerA64(bool hugePagesEnable) - : literalPos(ImulRcpLiteralsEnd) +JitCompilerA64::JitCompilerA64(bool hugePagesEnable) : + hugePages(hugePagesJIT && hugePagesEnable), + literalPos(ImulRcpLiteralsEnd) { - allocatedSize = CodeSize + CalcDatasetItemSize(); - code = static_cast(allocExecutableMemory(allocatedSize, hugePagesJIT && hugePagesEnable)); - - memset(reg_changed_offset, 0, sizeof(reg_changed_offset)); - memcpy(code, (void*) randomx_program_aarch64, CodeSize); } JitCompilerA64::~JitCompilerA64() @@ -115,9 +111,14 @@ JitCompilerA64::~JitCompilerA64() void JitCompilerA64::generateProgram(Program& program, ProgramConfiguration& config, uint32_t) { -# ifdef XMRIG_SECURE_JIT - enableWriting(); -# endif + if (!allocatedSize) { + allocate(CodeSize); + } +#ifdef XMRIG_SECURE_JIT + else { + enableWriting(); + } +#endif uint32_t codePos = MainLoopBegin + 4; @@ -170,6 +171,15 @@ void JitCompilerA64::generateProgram(Program& program, ProgramConfiguration& con void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration& config, uint32_t datasetOffset) { + if (!allocatedSize) { + allocate(CodeSize); + } +#ifdef XMRIG_SECURE_JIT + else { + enableWriting(); + } +#endif + uint32_t codePos = MainLoopBegin + 4; // and w16, w10, ScratchpadL3Mask64 @@ -228,6 +238,15 @@ void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration template void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N]) { + if (!allocatedSize) { + allocate(CodeSize + CalcDatasetItemSize()); + } +#ifdef XMRIG_SECURE_JIT + else { + enableWriting(); + } +#endif + uint32_t codePos = CodeSize; uint8_t* p1 = (uint8_t*)randomx_calc_dataset_item_aarch64; @@ -369,6 +388,16 @@ void JitCompilerA64::enableExecution() const xmrig::VirtualMemory::protectRX(code, allocatedSize); } + +void JitCompilerA64::allocate(size_t size) +{ + allocatedSize = size; + code = static_cast(allocExecutableMemory(allocatedSize, hugePages)); + + memcpy(code, reinterpret_cast(randomx_program_aarch64), CodeSize); +} + + void JitCompilerA64::emitMovImmediate(uint32_t dst, uint32_t imm, uint8_t* code, uint32_t& codePos) { uint32_t k = codePos; diff --git a/src/crypto/randomx/jit_compiler_a64.hpp b/src/crypto/randomx/jit_compiler_a64.hpp index 1e8063de..faa2ac2d 100644 --- a/src/crypto/randomx/jit_compiler_a64.hpp +++ b/src/crypto/randomx/jit_compiler_a64.hpp @@ -77,12 +77,15 @@ namespace randomx { static InstructionGeneratorA64 engine[256]; private: - uint32_t reg_changed_offset[8]; - uint8_t* code; + const bool hugePages; + uint32_t reg_changed_offset[8]{}; + uint8_t* code = nullptr; uint32_t literalPos; uint32_t num32bitLiterals = 0; size_t allocatedSize = 0; + void allocate(size_t size); + static void emit32(uint32_t val, uint8_t* code, uint32_t& codePos) { *(uint32_t*)(code + codePos) = val; diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp index 28b9f39a..14aa7067 100644 --- a/src/crypto/randomx/randomx.cpp +++ b/src/crypto/randomx/randomx.cpp @@ -383,7 +383,7 @@ extern "C" { case RANDOMX_FLAG_JIT: cache->jit = new randomx::JitCompiler(false); cache->initialize = &randomx::initCacheCompile; - cache->datasetInit = cache->jit->getDatasetInitFunc(); + cache->datasetInit = nullptr; cache->memory = memory; break;