diff --git a/src/backend/x86/bal_x86_assembler.c b/src/backend/x86/bal_x86_assembler.c index 4ec644b7..ab3f5627 100644 --- a/src/backend/x86/bal_x86_assembler.c +++ b/src/backend/x86/bal_x86_assembler.c @@ -106,7 +106,6 @@ bal_x86_assembler_reset(bal_x86_assembler_t *assembler) assembler->offset = 0; assembler->status = BAL_SUCCESS; (void)memset(assembler->buffer, 0, assembler->capacity); - (void)memset(assembler->rx_buffer, 0, assembler->capacity); } void diff --git a/src/bal_engine.c b/src/bal_engine.c index cd9efa86..cd701072 100644 --- a/src/bal_engine.c +++ b/src/bal_engine.c @@ -25,7 +25,7 @@ #define MEMORY_ALIGNMENT 64U #define TIER1_BUFFER_SIZE_BYTES (1024 * 1024 * 16) // 16 MiB -#define BLOCK_CACHE_SETS 4096 // 256 KB total footprint. +#define BLOCK_CACHE_SETS 128 // 8 KB total footprint. #define BLOCK_CACHE_WAYS 4 #define BLOCK_CACHE_MASK (BLOCK_CACHE_SETS - 1) @@ -49,6 +49,8 @@ typedef struct } block_cache_set_t; static_assert(64 == sizeof(block_cache_set_t), "Block cache set size mismatch"); +static_assert(8192 == BLOCK_CACHE_SETS * sizeof(block_cache_set_t), + "Block cache footprint must be exactly 8 KB"); typedef struct { @@ -67,8 +69,14 @@ typedef struct } thread_state; BAL_ALIGNED(64) block_cache_set_t block_cache[BLOCK_CACHE_SETS]; + + /// Two-bit FIFO way-eviction counter per set, packed four sets per byte. + uint8_t block_cache_fifo[BLOCK_CACHE_SETS / 4U]; } internal_engine_state_t; +static_assert(2U * BLOCK_CACHE_SETS == 8U * sizeof(((internal_engine_state_t *)0)->block_cache_fifo), + "Block cache FIFO counters must be exactly 2 bits per set"); + // Disable address sanitizers when executing jit code because it introduces very subtle but // dangerous bugs. __attribute__((no_sanitize("address", "undefined"))) static void execute_jit_block( @@ -81,9 +89,14 @@ static void *block_cache_lookup(const block_cache_set_t *BAL_RESTRICT cache, /// Inserts a newly compiled JIT block into cache. static void block_cache_insert(block_cache_set_t *BAL_RESTRICT cache, + uint8_t *BAL_RESTRICT fifo, bal_guest_address_t pc, void *host_code); +/// Zeroes every block cache entry and FIFO counter. Must be called whenever the Tier 1 +/// layout is reset: cached host code pointers go stale. +static void block_cache_reset(internal_engine_state_t *BAL_RESTRICT internal_engine_state); + /// Helper function which gets called by LDR instructions. static uint64_t guest_read(void *context, uint64_t guest_address); @@ -292,8 +305,7 @@ bal_engine_run_thread(bal_engine_t *engine) BAL_LOG_INFO(&bal_thread_logger, "Clear cache requested. Resetting JIT layout and block cache."); bal_tier1_compiler_reset(&internal_engine_state->tier1_compiler); - (void)memset( - internal_engine_state->block_cache, 0, sizeof(internal_engine_state->block_cache)); + block_cache_reset(internal_engine_state); atomic_store_explicit(&internal_engine_state->thread_state.clear_cache_requested, false, memory_order_release); @@ -355,7 +367,7 @@ bal_engine_run_thread(bal_engine_t *engine) "Tier 1 executable buffer full. Resetting JIT layout cache"); bal_tier1_compiler_reset(&internal_engine_state->tier1_compiler); - // TODO: When the block cache is implemented, it must be cleared here. + block_cache_reset(internal_engine_state); entry_point = bal_tier1_compiler_translate(&internal_engine_state->tier1_compiler, engine->memory_interface, @@ -392,7 +404,10 @@ bal_engine_run_thread(bal_engine_t *engine) if (BAL_LIKELY(!(engine->flags & BAL_ENGINE_FLAG_DISABLE_BLOCK_CACHE))) { - block_cache_insert(internal_engine_state->block_cache, pc, entry_point); + block_cache_insert(internal_engine_state->block_cache, + internal_engine_state->block_cache_fifo, + pc, + entry_point); } const bal_jit_block_t compiled_block = (bal_jit_block_t)entry_point; @@ -459,6 +474,7 @@ bal_engine_reset(bal_engine_t *BAL_RESTRICT engine) (void)memset(engine_state->tier1_buffer.rw_pointer, 0, engine_state->tier1_buffer_size); } bal_tier1_compiler_reset(&engine_state->tier1_compiler); + block_cache_reset(engine_state); return engine->status; } @@ -583,6 +599,7 @@ block_cache_lookup(const block_cache_set_t *BAL_RESTRICT cache, const bal_guest_ BAL_HOT void block_cache_insert(block_cache_set_t *BAL_RESTRICT cache, + uint8_t *BAL_RESTRICT fifo, const bal_guest_address_t pc, void *BAL_RESTRICT host_code) { @@ -608,12 +625,24 @@ block_cache_insert(block_cache_set_t *BAL_RESTRICT cache, } } - // All 4 ways are full. Use the lower bits of the PC as a cheap starting index. - const uint32_t evict_way = (pc >> 4) & (BLOCK_CACHE_WAYS - 1); + uint8_t *const BAL_RESTRICT fifo_byte = &fifo[set_index >> 2]; + const uint32_t fifo_shift = (set_index & 3) * 2; + const uint32_t evict_way = (*fifo_byte >> fifo_shift) & (BLOCK_CACHE_WAYS - 1); + const uint32_t next_way = (evict_way + 1) & (BLOCK_CACHE_WAYS - 1); + ways[evict_way].guest_address = pc; ways[evict_way].host_code = host_code; } +static void +block_cache_reset(internal_engine_state_t *BAL_RESTRICT internal_engine_state) +{ + (void)memset(internal_engine_state->block_cache, 0, sizeof(internal_engine_state->block_cache)); + (void)memset(internal_engine_state->block_cache_fifo, + 0, + sizeof(internal_engine_state->block_cache_fifo)); +} + static uint64_t guest_read(void *context, const uint64_t guest_address) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index da4bab93..48d23505 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -49,6 +49,8 @@ add_ballistic_test(test_bal_decoder_integrity unit/bal_decoder/test_bal_decoder_ add_ballistic_test(test_bal_jit_debug_lifecycle unit/bal_jit_debug/test_bal_jit_debug_lifecycle.c) add_ballistic_test(test_bal_jit_debug_add_block unit/bal_jit_debug/test_bal_jit_debug_add_block.c) +add_ballistic_test(test_bal_engine_block_cache unit/bal_engine/test_bal_engine_block_cache.c) + add_ballistic_test(test_bal_log_init_default unit/bal_log/test_bal_log_init_default.c) add_ballistic_test(test_bal_log_message unit/bal_log/test_bal_log_message.c) @@ -93,7 +95,6 @@ set_tests_properties(doctest PROPERTIES ENVIRONMENT "LD_LIBRARY_PATH=${LUA_LD_PATH};DYLD_LIBRARY_PATH=${LUA_DYLD_LIBRARY_PATH};DOCTEST_CC=${DOCTEST_CC};DOCTEST_CFLAGS=${DOCTEST_CFLAGS};DOCTEST_LTO=${DOCTEST_LTO};DOCTEST_LIB_DIR=${BALLISTIC_LIBRARIES_DIRECTORY}" ) - # TODO (GloriousTaco): Support IPC on Windows. if (UNIX) if (LINUX) diff --git a/tests/unit/bal_engine/test_bal_engine_block_cache.c b/tests/unit/bal_engine/test_bal_engine_block_cache.c new file mode 100644 index 00000000..0dbfce9e --- /dev/null +++ b/tests/unit/bal_engine/test_bal_engine_block_cache.c @@ -0,0 +1,141 @@ +#include "bal_engine.h" +#include "bal_engine_flags.h" +#include "bal_memory.h" +#include "unity.h" +#include +#include + +#define GUEST_MEMORY_SIZE 4096U + +#define BLOCK_COUNT 5U +#define FIRST_BLOCK_GVA 0x100U +#define BLOCK_STRIDE 0x200U + +static uint32_t +movz_x8(const uint32_t imm16) +{ + return 0xD2800000U | (imm16 << 5) | 8U; +} + +#define RET_ENCODING 0xD65F03C0U + +static bal_allocator_t allocator = {}; +static bal_memory_interface_t memory_interface = {}; +static uint32_t guest_memory[GUEST_MEMORY_SIZE / sizeof(uint32_t)]; + +void +setUp(void) +{ + (void)memset(guest_memory, 0, sizeof(guest_memory)); + + for (uint32_t i = 0; i < BLOCK_COUNT; ++i) + { + const uint32_t word_index = (FIRST_BLOCK_GVA + i * BLOCK_STRIDE) / sizeof(uint32_t); + guest_memory[word_index] = movz_x8(i + 1U); + guest_memory[word_index + 1] = RET_ENCODING; + } + + bal_allocator_default_init(&allocator); + TEST_ASSERT_EQUAL_INT(BAL_SUCCESS, + bal_flat_translation_interface_init( + &allocator, &memory_interface, guest_memory, sizeof(guest_memory))); + + bal_thread_logger.min_level = BAL_LOG_LEVEL_NONE; +} + +void +tearDown(void) +{ + (void)bal_flat_translation_interface_destroy(&allocator, &memory_interface); +} + +static void +execute_block(bal_engine_t *engine, bal_cpu_t *cpu, const uint32_t index) +{ + cpu->pc = FIRST_BLOCK_GVA + index * BLOCK_STRIDE; + cpu->x[8] = 0ULL; + + TEST_ASSERT_EQUAL_INT(BAL_SUCCESS, bal_engine_run_thread(engine)); + TEST_ASSERT_EQUAL_UINT64(index + 1ULL, cpu->x[8]); +} + +static void +test_BlockCacheFiveBlocksInOneSet_EvictedBlockReExecutesCorrectly(void) +{ + bal_cpu_t cpu = {}; + cpu.x[30] = BAL_ENGINE_SENTINEL; + + bal_engine_t engine = {}; + TEST_ASSERT_EQUAL_INT( + BAL_SUCCESS, bal_engine_init(&engine, &cpu, &allocator, &memory_interface)); + + for (uint32_t i = 0; i < BLOCK_COUNT; ++i) + { + execute_block(&engine, &cpu, i); + } + + for (uint32_t i = 0; i < BLOCK_COUNT; ++i) + { + execute_block(&engine, &cpu, i); + } + + bal_engine_destroy(&engine); +} + +static void +test_ClearCacheRequest_ResetsCacheAndLayout(void) +{ + bal_cpu_t cpu = {}; + cpu.x[30] = BAL_ENGINE_SENTINEL; + + bal_engine_t engine = {}; + TEST_ASSERT_EQUAL_INT( + BAL_SUCCESS, bal_engine_init(&engine, &cpu, &allocator, &memory_interface)); + + for (uint32_t i = 0; i < BLOCK_COUNT; ++i) + { + execute_block(&engine, &cpu, i); + } + + bal_engine_clear_cache(&engine); + + for (uint32_t i = 0; i < BLOCK_COUNT; ++i) + { + execute_block(&engine, &cpu, i); + } + + bal_engine_destroy(&engine); +} + +static void +test_DisableBlockCacheFlag_EveryBlockRecompiles(void) +{ + bal_cpu_t cpu = {}; + cpu.x[30] = BAL_ENGINE_SENTINEL; + + bal_engine_t engine = {}; + TEST_ASSERT_EQUAL_INT( + BAL_SUCCESS, bal_engine_init(&engine, &cpu, &allocator, &memory_interface)); + + engine.flags |= BAL_ENGINE_FLAG_DISABLE_BLOCK_CACHE; + + for (uint32_t pass = 0; pass < 2U; ++pass) + { + for (uint32_t i = 0; i < BLOCK_COUNT; ++i) + { + execute_block(&engine, &cpu, i); + } + } + + bal_engine_destroy(&engine); +} + +int +main(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_BlockCacheFiveBlocksInOneSet_EvictedBlockReExecutesCorrectly); + RUN_TEST(test_ClearCacheRequest_ResetsCacheAndLayout); + RUN_TEST(test_DisableBlockCacheFlag_EveryBlockRecompiles); + return UNITY_END(); +}