diff --git a/src/backend/x86/bal_x86_tier1_compiler.c b/src/backend/x86/bal_x86_tier1_compiler.c index d19d9e74..a71c3d81 100644 --- a/src/backend/x86/bal_x86_tier1_compiler.c +++ b/src/backend/x86/bal_x86_tier1_compiler.c @@ -90,6 +90,10 @@ static void translate_and_reg(bal_tier1_compiler_t *compiler const bal_decoder_instruction_metadata_t *metadata, uint32_t instruction); +static void translate_or_reg(bal_tier1_compiler_t *compiler, + const bal_decoder_instruction_metadata_t *metadata, + uint32_t instruction); + static void translate_ldr(bal_tier1_compiler_t *compiler, const bal_decoder_instruction_metadata_t *metadata, uint32_t instruction); @@ -358,6 +362,27 @@ bal_tier1_compiler_translate(bal_tier1_compiler_t *compiler, break; } + if (0 == strcmp(metadata->name, "ORR") + && 5U == metadata->operands[1].bit_position + && (BAL_OPERAND_TYPE_REGISTER_64 == metadata->operands[1].type + || BAL_OPERAND_TYPE_REGISTER_32 == metadata->operands[1].type)) + { + if (BAL_OPERAND_TYPE_REGISTER_64 == metadata->operands[3].type + || BAL_OPERAND_TYPE_REGISTER_32 == metadata->operands[3].type) + { + translate_or_reg(compiler, metadata, instruction); + break; + } + + BAL_LOG_ERROR(&bal_thread_logger, + "Aborting function: Tier 1 does not support ORR " + "(immediate) at GVA 0x%016llX.", + (unsigned long long)guest_address); + compiler->status = BAL_ERROR_UNKNOWN_INSTRUCTION; + is_block_terminated = true; + break; + } + if (BAL_LIKELY(BAL_OPERAND_TYPE_REGISTER_64 == metadata->operands[1].type) || BAL_LIKELY(BAL_OPERAND_TYPE_REGISTER_32 == metadata->operands[1].type)) { @@ -1480,6 +1505,212 @@ translate_and_reg(bal_tier1_compiler_t *BAL_RESTRICT compile } } +static void +push_shift_macro(bal_tier1_compiler_t *compiler, + bal_x86_register_t x86_register, + uint32_t shift_amount, + uint32_t shift_type) +{ + bal_x86_macro_opcode_t shift_opcode = BAL_X86_MACRO_SHL_REGISTER_IMMEDIATE; + + if (0b01 == shift_type) + { + shift_opcode = BAL_X86_MACRO_SHR_REGISTER_IMMEDIATE; + } + else if (0b10 == shift_type) + { + shift_opcode = BAL_X86_MACRO_SAR_REGISTER_IMMEDIATE; + } + else if (0b11 == shift_type) + { + shift_opcode = BAL_X86_MACRO_ROR_REGISTER_IMMEDIATE; + } + + const bal_x86_macro_t shift_macro = { + .opcode = shift_opcode, + .destination = x86_register, + .immediate_or_offset = shift_amount, + }; + bal_sliding_window_push(&compiler->window, shift_macro); +} + +void +translate_or_reg(bal_tier1_compiler_t *BAL_RESTRICT compiler, + const bal_decoder_instruction_metadata_t *BAL_RESTRICT metadata, + const uint32_t instruction) +{ + const bal_decoder_operand_t *BAL_RESTRICT operands = metadata->operands; + const uint8_t rd = (uint8_t)extract_operand_value(instruction, &operands[0]); + const uint8_t rn = (uint8_t)extract_operand_value(instruction, &operands[1]); + const uint32_t shift_amount = extract_operand_value(instruction, &operands[2]); + const uint8_t rm = (uint8_t)extract_operand_value(instruction, &operands[3]); + const uint32_t shift_type = extract_operand_value(instruction, &operands[4]); + + if (31U == rd) + { + return; + } + + if (BAL_UNLIKELY(shift_type > 3U)) + { + BAL_LOG_ERROR( + &bal_thread_logger, "Aborting function: unsupported shift type %u", shift_type); + compiler->status = BAL_ERROR_UNKNOWN_INSTRUCTION; + return; + } + + const bool rn_is_zero = 31U == rn; + const bool rm_is_zero = 31U == rm; + + bal_x86_register_t x86_rn = BAL_X86_INVALID; + + if (!rn_is_zero) + { + const bool skip_load_rn = false; + x86_rn = allocate_x86_register(compiler, rn, skip_load_rn); + + if (BAL_UNLIKELY(BAL_X86_INVALID == x86_rn)) + { + compiler->status = BAL_ERROR_INCORRECT_REGISTER_TYPE; + return; + } + } + + bal_x86_register_t x86_rm = BAL_X86_INVALID; + + if (!rm_is_zero) + { + const bool skip_load_rm = false; + x86_rm = allocate_x86_register(compiler, rm, skip_load_rm); + + if (BAL_UNLIKELY(BAL_X86_INVALID == x86_rm)) + { + compiler->status = BAL_ERROR_INCORRECT_REGISTER_TYPE; + return; + } + } + + const bool skip_load_rd = true; + const bal_x86_register_t x86_rd = allocate_x86_register(compiler, rd, skip_load_rd); + + if (BAL_UNLIKELY(BAL_X86_INVALID == x86_rd)) + { + compiler->status = BAL_ERROR_INCORRECT_REGISTER_TYPE; + return; + } + + if (rn_is_zero && rm_is_zero) + { + const bal_x86_macro_t mov_macro = { + .opcode = BAL_X86_MACRO_MOV_REGISTER_IMMEDIATE, + .destination = x86_rd, + .immediate_or_offset = 0ULL, + }; + bal_sliding_window_push(&compiler->window, mov_macro); + compiler->is_dirty |= 1U << rd; + return; + } + + if (rm_is_zero) + { + if (x86_rd != x86_rn) + { + const bal_x86_macro_t mov_macro = { + .opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER, + .destination = x86_rd, + .source = x86_rn, + }; + bal_sliding_window_push(&compiler->window, mov_macro); + } + + compiler->is_dirty |= 1U << rd; + return; + } + + if (rn_is_zero) + { + if (shift_amount > 0) + { + if (x86_rd != x86_rm) + { + const bal_x86_macro_t copy_macro = { + .opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER, + .destination = BAL_X86_REGISTER_DISCARD_RESULT, + .source = x86_rm, + }; + bal_sliding_window_push(&compiler->window, copy_macro); + push_shift_macro(compiler, BAL_X86_REGISTER_DISCARD_RESULT, shift_amount, + shift_type); + + const bal_x86_macro_t result_macro = { + .opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER, + .destination = x86_rd, + .source = BAL_X86_REGISTER_DISCARD_RESULT, + }; + bal_sliding_window_push(&compiler->window, result_macro); + } + else + { + push_shift_macro(compiler, x86_rm, shift_amount, shift_type); + } + } + else if (x86_rd != x86_rm) + { + const bal_x86_macro_t mov_macro = { + .opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER, + .destination = x86_rd, + .source = x86_rm, + }; + bal_sliding_window_push(&compiler->window, mov_macro); + } + + compiler->is_dirty |= 1U << rd; + return; + } + + bal_x86_register_t x86_operand2 = x86_rm; + + if (shift_amount > 0) + { + if (x86_rd != x86_rm) + { + const bal_x86_macro_t copy_macro = { + .opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER, + .destination = BAL_X86_REGISTER_DISCARD_RESULT, + .source = x86_rm, + }; + bal_sliding_window_push(&compiler->window, copy_macro); + x86_operand2 = BAL_X86_REGISTER_DISCARD_RESULT; + } + + push_shift_macro(compiler, x86_operand2, shift_amount, shift_type); + } + + bal_x86_register_t x86_or_source = x86_operand2; + + if (x86_rd == x86_operand2) + { + x86_or_source = x86_rn; + } + else if (x86_rd != x86_rn) + { + const bal_x86_macro_t mov_macro = { + .opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER, + .destination = x86_rd, + .source = x86_rn, + }; + bal_sliding_window_push(&compiler->window, mov_macro); + } + + const bal_x86_macro_t or_macro = { + .opcode = BAL_X86_MACRO_OR_REGISTER_REGISTER, + .destination = x86_rd, + .source = x86_or_source, + }; + bal_sliding_window_push(&compiler->window, or_macro); + compiler->is_dirty |= 1U << rd; +} + void translate_ldr(bal_tier1_compiler_t *compiler, const bal_decoder_instruction_metadata_t *metadata, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index da4bab93..c45c5e0c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -46,6 +46,8 @@ target_link_libraries(test_bal_assembler_sub PUBLIC BallisticTestAssemblerFixtur add_ballistic_test(test_bal_decoder_integrity unit/bal_decoder/test_bal_decoder_integrity.c) +add_ballistic_test(test_bal_tier1_orr unit/bal_tier1/test_bal_tier1_orr.c) + 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) @@ -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_tier1/test_bal_tier1_orr.c b/tests/unit/bal_tier1/test_bal_tier1_orr.c new file mode 100644 index 00000000..72b063b3 --- /dev/null +++ b/tests/unit/bal_tier1/test_bal_tier1_orr.c @@ -0,0 +1,193 @@ +#include "bal_engine.h" +#include "bal_engine_flags.h" +#include "bal_memory.h" +#include "unity.h" +#include + +#define ARM64_RET_ENCODING 0xD65F03C0U +#define TEST_GUEST_MEMORY_SIZE 4096U + +#define ORR_X8_X9_X10 0xAA0A0128U +#define ORR_X8_X9_X10_LSL4 0xAA0A1128U +#define ORR_X8_X9_X8_LSL4 0xAA081128U +#define ORR_X8_X8_X10 0xAA0A0108U +#define ORR_XZR_X9_X10 0xAA0A01E8U +#define ORR_X8_XZR_X10 0xAA0A03E8U +#define ORR_X8_X9_XZR 0xAA1F0128U +#define ORR_X8_XZR_XZR 0xAA1F03E8U +#define ORR_X8_XZR_X8_LSL4 0xAA0813E8U + +static uint32_t bal_test_guest_memory[TEST_GUEST_MEMORY_SIZE / sizeof(uint32_t)]; + +void +setUp(void) +{ + (void)memset(bal_test_guest_memory, 0, sizeof(bal_test_guest_memory)); +} + +void +tearDown(void) +{ +} + +static void +run_single_guest_instruction(const uint32_t instruction, bal_cpu_t *BAL_RESTRICT cpu) +{ + bal_test_guest_memory[0] = instruction; + bal_test_guest_memory[1] = ARM64_RET_ENCODING; + + bal_allocator_t allocator = {}; + bal_allocator_default_init(&allocator); + + bal_memory_interface_t memory_interface = {}; + TEST_ASSERT_EQUAL(BAL_SUCCESS, + bal_flat_translation_interface_init(&allocator, + &memory_interface, + bal_test_guest_memory, + sizeof(bal_test_guest_memory))); + + cpu->pc = 0ULL; + + bal_engine_t engine = {}; + TEST_ASSERT_EQUAL(BAL_SUCCESS, bal_engine_init(&engine, cpu, &allocator, &memory_interface)); + + engine.flags |= BAL_ENGINE_FLAG_SINGLE_STEP; + + const bal_error_t run_status = bal_engine_run_thread(&engine); + + bal_engine_destroy(&engine); + (void)bal_flat_translation_interface_destroy(&allocator, &memory_interface); + + TEST_ASSERT_EQUAL(BAL_SUCCESS, run_status); +} + +static void +test_BalTier1Orr_CombinesBothSourceRegisters(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0xAAAAAAAAAAAAAAAAULL; + cpu.x[9] = 0x0F0F0F0F0F0F0F0FULL; + cpu.x[10] = 0xF0F0F0F0F0F0F0F0ULL; + + run_single_guest_instruction(ORR_X8_X9_X10, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0xFFFFFFFFFFFFFFFFULL, cpu.x[8]); +} + +static void +test_BalTier1Orr_AppliesLeftShiftToSecondSource(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0xDEADBEEFDEADBEEFULL; + cpu.x[9] = 0x0000000000000001ULL; + cpu.x[10] = 0x0000000000000001ULL; + + run_single_guest_instruction(ORR_X8_X9_X10_LSL4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000011ULL, cpu.x[8]); +} + +static void +test_BalTier1Orr_DestinationMatchingSecondSourceUsesOldValue(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0x00000000000000FFULL; + cpu.x[9] = 0x000000000000FF00ULL; + + run_single_guest_instruction(ORR_X8_X9_X8_LSL4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x000000000000FFF0ULL, cpu.x[8]); +} + +static void +test_BalTier1Orr_DestinationMatchingFirstSource(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0x0000000000000001ULL; + cpu.x[10] = 0x0000000000000002ULL; + + run_single_guest_instruction(ORR_X8_X8_X10, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000003ULL, cpu.x[8]); +} + +static void +test_BalTier1Orr_WriteToZeroRegisterIsDiscarded(void) +{ + bal_cpu_t cpu = {}; + cpu.x[9] = 0x0F0F0F0F0F0F0F0FULL; + cpu.x[10] = 0xF0F0F0F0F0F0F0F0ULL; + + run_single_guest_instruction(ORR_XZR_X9_X10, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000000ULL, cpu.x[31]); +} + +static void +test_BalTier1Orr_ZeroRegisterSourceReadsAsZeroNotSP(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0xDEADBEEFDEADBEEFULL; + cpu.x[10] = 0x0123456789ABCDEFULL; + cpu.x[31] = 0xCAFEBABE00000000ULL; + + run_single_guest_instruction(ORR_X8_XZR_X10, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0123456789ABCDEFULL, cpu.x[8]); + TEST_ASSERT_EQUAL_HEX64(0xCAFEBABE00000000ULL, cpu.x[31]); +} + +static void +test_BalTier1Orr_SecondSourceZeroRegisterKeepsFirstOperand(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0xDEADBEEFDEADBEEFULL; + cpu.x[9] = 0x0123456789ABCDEFULL; + cpu.x[31] = 0xCAFEBABE00000000ULL; + + run_single_guest_instruction(ORR_X8_X9_XZR, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0123456789ABCDEFULL, cpu.x[8]); +} + +static void +test_BalTier1Orr_BothSourcesZeroRegisterClearDestination(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0xDEADBEEFDEADBEEFULL; + cpu.x[31] = 0xCAFEBABE00000000ULL; + + run_single_guest_instruction(ORR_X8_XZR_XZR, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000000ULL, cpu.x[8]); +} + +static void +test_BalTier1Orr_ZeroRegisterSourceShiftOfDestinationItself(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0x00000000000000FFULL; + cpu.x[31] = 0xCAFEBABE00000000ULL; + + run_single_guest_instruction(ORR_X8_XZR_X8_LSL4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000FF0ULL, cpu.x[8]); +} + +int +main(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_BalTier1Orr_CombinesBothSourceRegisters); + RUN_TEST(test_BalTier1Orr_AppliesLeftShiftToSecondSource); + RUN_TEST(test_BalTier1Orr_DestinationMatchingSecondSourceUsesOldValue); + RUN_TEST(test_BalTier1Orr_DestinationMatchingFirstSource); + RUN_TEST(test_BalTier1Orr_WriteToZeroRegisterIsDiscarded); + RUN_TEST(test_BalTier1Orr_ZeroRegisterSourceReadsAsZeroNotSP); + RUN_TEST(test_BalTier1Orr_SecondSourceZeroRegisterKeepsFirstOperand); + RUN_TEST(test_BalTier1Orr_BothSourcesZeroRegisterClearDestination); + RUN_TEST(test_BalTier1Orr_ZeroRegisterSourceShiftOfDestinationItself); + return UNITY_END(); +} + +/*** end of file ***/