From 998c55030af226946b6408eabb965dd5f22c76c8 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 18 Oct 2019 16:26:15 +0200 Subject: [PATCH 1/6] Fixed code cache cleanup on iOS/Darwin --- src/crypto/randomx/jit_compiler_a64.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index b31d1fa82..1a6a6a06c 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -103,6 +103,19 @@ JitCompilerA64::~JitCompilerA64() freePagedMemory(code, CodeSize + CalcDatasetItemSize()); } +#if defined(ios_HOST_OS) || defined (darwin_HOST_OS) +void sys_icache_invalidate(void *start, size_t len); +#endif + +static void clear_code_cache(void* p, size_t size) +{ +# ifdef HAVE_BUILTIN_CLEAR_CACHE_ + __builtin___clear_cache(p, size); +# elif defined(ios_HOST_OS) || defined (darwin_HOST_OS) + sys_icache_invalidate(p, size); +# endif +} + void JitCompilerA64::generateProgram(Program& program, ProgramConfiguration& config) { uint32_t codePos = MainLoopBegin + 4; @@ -149,9 +162,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 HAVE_BUILTIN_CLEAR_CACHE_ - __builtin___clear_cache(reinterpret_cast(code + MainLoopBegin), reinterpret_cast(code + codePos)); -# endif + clear_code_cache(reinterpret_cast(code + MainLoopBegin), reinterpret_cast(code + codePos)); } void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration& config, uint32_t datasetOffset) @@ -206,9 +217,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 HAVE_BUILTIN_CLEAR_CACHE - __builtin___clear_cache(reinterpret_cast(code + MainLoopBegin), reinterpret_cast(code + codePos)); -# endif + clear_code_cache(reinterpret_cast(code + MainLoopBegin), reinterpret_cast(code + codePos)); } template @@ -324,9 +333,7 @@ void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N], s memcpy(code + codePos, p1, p2 - p1); codePos += p2 - p1; -# ifdef HAVE_BUILTIN_CLEAR_CACHE - __builtin___clear_cache(reinterpret_cast(code + CodeSize), reinterpret_cast(code + codePos)); -# endif + clear_code_cache(reinterpret_cast(code + CodeSize), reinterpret_cast(code + codePos)); } template void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_MAX_ACCESSES], std::vector &reciprocalCache); From 1a66c3f1a1063e3b12d3e54f965d0cccf7da8ff6 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 18 Oct 2019 16:32:01 +0200 Subject: [PATCH 2/6] Update jit_compiler_a64.cpp --- src/crypto/randomx/jit_compiler_a64.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index f62550d04..79e099a0d 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -109,7 +109,7 @@ void sys_icache_invalidate(void *start, size_t len); static void clear_code_cache(void* p, size_t size) { -# ifdef HAVE_BUILTIN_CLEAR_CACHE_ +# ifdef HAVE_BUILTIN_CLEAR_CACHE __builtin___clear_cache(p, size); # elif defined(ios_HOST_OS) || defined (darwin_HOST_OS) sys_icache_invalidate(p, size); From 0ad992985c005d5f03685767988c3657ef6b4485 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 18 Oct 2019 16:36:50 +0200 Subject: [PATCH 3/6] Update jit_compiler_a64.cpp --- src/crypto/randomx/jit_compiler_a64.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index 79e099a0d..586b407f7 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -107,12 +107,12 @@ JitCompilerA64::~JitCompilerA64() void sys_icache_invalidate(void *start, size_t len); #endif -static void clear_code_cache(void* p, size_t size) +static void clear_code_cache(char* p1, char* p2) { # ifdef HAVE_BUILTIN_CLEAR_CACHE - __builtin___clear_cache(p, size); + __builtin___clear_cache(p1, p2); # elif defined(ios_HOST_OS) || defined (darwin_HOST_OS) - sys_icache_invalidate(p, size); + sys_icache_invalidate(p1, static_cast(p2 - p1)); # endif } From a56febcd13a4da8a62a55940b6edc7e8b9a79935 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 18 Oct 2019 17:39:57 +0200 Subject: [PATCH 4/6] Force HAVE_BUILTIN_CLEAR_CACHE for GNU compilers They always have __builtin___clear_cache --- cmake/flags.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 97f22f3c1..e9533eed5 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -54,6 +54,8 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU) #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf-2") + add_definitions(/DHAVE_BUILTIN_CLEAR_CACHE) + elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ox /Ot /Oi /MT /GL") From 5611249af7b4650d02bbdb7cce4502422edc3656 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 18 Oct 2019 18:04:13 +0200 Subject: [PATCH 5/6] Fixed __builtin___clear_cache detection --- src/crypto/randomx/jit_compiler_a64.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index 586b407f7..f88f945ac 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -109,7 +109,7 @@ void sys_icache_invalidate(void *start, size_t len); static void clear_code_cache(char* p1, char* p2) { -# ifdef HAVE_BUILTIN_CLEAR_CACHE +# if defined (HAVE_BUILTIN_CLEAR_CACHE) || defined (__GNUC__) __builtin___clear_cache(p1, p2); # elif defined(ios_HOST_OS) || defined (darwin_HOST_OS) sys_icache_invalidate(p1, static_cast(p2 - p1)); From 578bebb04dbc9631f285860b48cee0bcee05b05d Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 18 Oct 2019 18:17:57 +0200 Subject: [PATCH 6/6] Prefer sys_icache_invalidate on iOS Also break compilation with error if clear cache is not available --- src/crypto/randomx/jit_compiler_a64.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index f88f945ac..bf790c2b4 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -109,10 +109,12 @@ void sys_icache_invalidate(void *start, size_t len); static void clear_code_cache(char* p1, char* p2) { -# if defined (HAVE_BUILTIN_CLEAR_CACHE) || defined (__GNUC__) - __builtin___clear_cache(p1, p2); -# elif defined(ios_HOST_OS) || defined (darwin_HOST_OS) +# if defined(ios_HOST_OS) || defined (darwin_HOST_OS) sys_icache_invalidate(p1, static_cast(p2 - p1)); +# elif defined (HAVE_BUILTIN_CLEAR_CACHE) || defined (__GNUC__) + __builtin___clear_cache(p1, p2); +# else +# error "No clear code cache function found" # endif }