Skip to content

Commit

Permalink
Use own clear cache function for aarch64
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed Oct 18, 2023
1 parent 2777910 commit 0498eaf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
1 change: 0 additions & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: C/C++ CI

on:
push:
branches: [ master ]
pull_request:

jobs:
Expand Down
16 changes: 4 additions & 12 deletions src/jit_compiler_a64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,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
randomx_clear_cache(code, code + CodeSize);
}

JitCompilerA64::~JitCompilerA64()
Expand Down Expand Up @@ -169,9 +167,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
randomx_clear_cache(code + MainLoopBegin, code + codePos);
}

void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration& config, uint32_t datasetOffset)
Expand Down Expand Up @@ -226,9 +222,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
randomx_clear_cache(code + MainLoopBegin, code + codePos);
}

template<size_t N>
Expand Down Expand Up @@ -344,9 +338,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
randomx_clear_cache(code + CodeSize, code + codePos);
}

template void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_ACCESSES], std::vector<uint64_t> &reciprocalCache);
Expand Down
53 changes: 52 additions & 1 deletion src/jit_compiler_a64_static.S
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

.arch armv8-a
.text
.global DECL(randomx_clear_cache)
.global DECL(randomx_program_aarch64)
.global DECL(randomx_program_aarch64_main_loop)
.global DECL(randomx_program_aarch64_vm_instructions)
Expand All @@ -52,6 +53,57 @@
.global DECL(randomx_calc_dataset_item_aarch64_store_result)
.global DECL(randomx_calc_dataset_item_aarch64_end)

.balign 4
DECL(randomx_clear_cache):
# x0 = begin
# x1 = end

# Range check
cmp x0, x1
bhs randomx_clear_cache_exit

# Read "Cache Type Register, EL0"
# https://developer.arm.com/documentation/ddi0488/h/system-control/aarch64-register-descriptions/cache-type-register--el0
mrs x4, ctr_el0

# [19:16] DminLine
# Log2 of the number of words in the smallest cache line of all the data and unified caches that the processor controls
lsr x2, x4, 16
and x2, x2, 15
mov x3, 4
lsl x3, x3, x2

# Invalidate all data cache lines between x0 and x1
mov x2, x0
randomx_dcache_invalidate_loop:
#dc cvau, x2
add x2, x2, x3
cmp x2, x1
blo randomx_clear_cache_loop

# Data Synchronization Barrier
#dsb ish

# [3:0] IminLine
# Log2 of the number of words in the smallest cache line of all the Instruction Caches that the processor controls
and x2, x4, 15
mov x3, 4
lsl x3, x3, x2

# Invalidate all instruction cache lines between x0 and x1
mov x2, x0
randomx_clear_cache_loop:
#ic ivau, x2
add x2, x2, x3
cmp x2, x1
blo randomx_clear_cache_loop

# Instruction Synchronization Barrier
#isb sy

randomx_clear_cache_exit:
ret

#include "configuration.h"

# Register allocation
Expand Down Expand Up @@ -106,7 +158,6 @@
# v30 -> E 'or' mask = 0x3*00000000******3*00000000******
# v31 -> scale mask = 0x81f000000000000081f0000000000000

.balign 4
DECL(randomx_program_aarch64):
# Save callee-saved registers
sub sp, sp, 192
Expand Down
1 change: 1 addition & 0 deletions src/jit_compiler_a64_static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

extern "C" {
void randomx_clear_cache(void* begin, void* end);
void randomx_program_aarch64(void* reg, void* mem, void* scratchpad, uint64_t iterations);
void randomx_program_aarch64_main_loop();
void randomx_program_aarch64_vm_instructions();
Expand Down

0 comments on commit 0498eaf

Please sign in to comment.