//===-- EmulateInstructionARM64.cpp ------------------------------*- C++-*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "EmulateInstructionARM64.h" #include #include "lldb/Core/Address.h" #include "lldb/Core/ArchSpec.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/RegisterValue.h" #include "lldb/Symbol/UnwindPlan.h" #include "lldb/Utility/ConstString.h" #include "lldb/Utility/Stream.h" #include "Plugins/Process/Utility/ARMDefines.h" #include "Plugins/Process/Utility/ARMUtils.h" #include "Plugins/Process/Utility/lldb-arm64-register-enums.h" #define GPR_OFFSET(idx) ((idx)*8) #define GPR_OFFSET_NAME(reg) 0 #define FPU_OFFSET(idx) ((idx)*16) #define FPU_OFFSET_NAME(reg) 0 #define EXC_OFFSET_NAME(reg) 0 #define DBG_OFFSET_NAME(reg) 0 #define DBG_OFFSET_NAME(reg) 0 #define DEFINE_DBG(re, y) \ "na", nullptr, 8, 0, lldb::eEncodingUint, lldb::eFormatHex, \ {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, \ LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM}, \ nullptr, nullptr, nullptr, 0 #define DECLARE_REGISTER_INFOS_ARM64_STRUCT #include "Plugins/Process/Utility/RegisterInfos_arm64.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/MathExtras.h" // for SignExtend32 template function // and CountTrailingZeros_32 function #include "Plugins/Process/Utility/InstructionUtils.h" using namespace lldb; using namespace lldb_private; static bool LLDBTableGetRegisterInfo(uint32_t reg_num, RegisterInfo ®_info) { if (reg_num >= llvm::array_lengthof(g_register_infos_arm64_le)) return false; reg_info = g_register_infos_arm64_le[reg_num]; return true; } #define No_VFP 0 #define VFPv1 (1u << 1) #define VFPv2 (1u << 2) #define VFPv3 (1u << 3) #define AdvancedSIMD (1u << 4) #define VFPv1_ABOVE (VFPv1 | VFPv2 | VFPv3 | AdvancedSIMD) #define VFPv2_ABOVE (VFPv2 | VFPv3 | AdvancedSIMD) #define VFPv2v3 (VFPv2 | VFPv3) #define UInt(x) ((uint64_t)x) #define SInt(x) ((int64_t)x) #define bit bool #define boolean bool #define integer int64_t static inline bool IsZero(uint64_t x) { return x == 0; } static inline uint64_t NOT(uint64_t x) { return ~x; } #if 0 // LSL_C() // ======= static inline uint64_t LSL_C (uint64_t x, integer shift, bool &carry_out) { assert (shift >= 0); uint64_t result = x << shift; carry_out = ((1ull << (64-1)) >> (shift - 1)) != 0; return result; } #endif // LSL() // ===== static inline uint64_t LSL(uint64_t x, integer shift) { if (shift == 0) return x; return x << shift; } // AddWithCarry() // =============== static inline uint64_t AddWithCarry(uint32_t N, uint64_t x, uint64_t y, bit carry_in, EmulateInstructionARM64::ProcState &proc_state) { uint64_t unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in); int64_t signed_sum = SInt(x) + SInt(y) + UInt(carry_in); uint64_t result = unsigned_sum; if (N < 64) result = Bits64(result, N - 1, 0); proc_state.N = Bit64(result, N - 1); proc_state.Z = IsZero(result); proc_state.C = UInt(result) == unsigned_sum; proc_state.V = SInt(result) == signed_sum; return result; } // ConstrainUnpredictable() // ======================== EmulateInstructionARM64::ConstraintType ConstrainUnpredictable(EmulateInstructionARM64::Unpredictable which) { EmulateInstructionARM64::ConstraintType result = EmulateInstructionARM64::Constraint_UNKNOWN; switch (which) { case EmulateInstructionARM64::Unpredictable_WBOVERLAP: case EmulateInstructionARM64::Unpredictable_LDPOVERLAP: // TODO: don't know what to really do here? Pseudo code says: // set result to one of above Constraint behaviours or UNDEFINED break; } return result; } //---------------------------------------------------------------------- // // EmulateInstructionARM implementation // //---------------------------------------------------------------------- void EmulateInstructionARM64::Initialize() { PluginManager::RegisterPlugin(GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance); } void EmulateInstructionARM64::Terminate() { PluginManager::UnregisterPlugin(CreateInstance); } ConstString EmulateInstructionARM64::GetPluginNameStatic() { ConstString g_plugin_name("lldb.emulate-instruction.arm64"); return g_plugin_name; } lldb_private::ConstString EmulateInstructionARM64::GetPluginName() { static ConstString g_plugin_name("EmulateInstructionARM64"); return g_plugin_name; } const char *EmulateInstructionARM64::GetPluginDescriptionStatic() { return "Emulate instructions for the ARM64 architecture."; } EmulateInstruction * EmulateInstructionARM64::CreateInstance(const ArchSpec &arch, InstructionType inst_type) { if (EmulateInstructionARM64::SupportsEmulatingInstructionsOfTypeStatic( inst_type)) { if (arch.GetTriple().getArch() == llvm::Triple::aarch64) { std::auto_ptr emulate_insn_ap( new EmulateInstructionARM64(arch)); if (emulate_insn_ap.get()) return emulate_insn_ap.release(); } } return NULL; } bool EmulateInstructionARM64::SetTargetTriple(const ArchSpec &arch) { if (arch.GetTriple().getArch() == llvm::Triple::arm) return true; else if (arch.GetTriple().getArch() == llvm::Triple::thumb) return true; return false; } bool EmulateInstructionARM64::GetRegisterInfo(RegisterKind reg_kind, uint32_t reg_num, RegisterInfo ®_info) { if (reg_kind == eRegisterKindGeneric) { switch (reg_num) { case LLDB_REGNUM_GENERIC_PC: reg_kind = eRegisterKindLLDB; reg_num = gpr_pc_arm64; break; case LLDB_REGNUM_GENERIC_SP: reg_kind = eRegisterKindLLDB; reg_num = gpr_sp_arm64; break; case LLDB_REGNUM_GENERIC_FP: reg_kind = eRegisterKindLLDB; reg_num = gpr_fp_arm64; break; case LLDB_REGNUM_GENERIC_RA: reg_kind = eRegisterKindLLDB; reg_num = gpr_lr_arm64; break; case LLDB_REGNUM_GENERIC_FLAGS: reg_kind = eRegisterKindLLDB; reg_num = gpr_cpsr_arm64; break; default: return false; } } if (reg_kind == eRegisterKindLLDB) return LLDBTableGetRegisterInfo(reg_num, reg_info); return false; } EmulateInstructionARM64::Opcode * EmulateInstructionARM64::GetOpcodeForInstruction(const uint32_t opcode) { static EmulateInstructionARM64::Opcode g_opcodes[] = { //---------------------------------------------------------------------- // Prologue instructions //---------------------------------------------------------------------- // push register(s) {0xff000000, 0xd1000000, No_VFP, &EmulateInstructionARM64::EmulateADDSUBImm, "SUB , , # {, }"}, {0xff000000, 0xf1000000, No_VFP, &EmulateInstructionARM64::EmulateADDSUBImm, "SUBS , , # {, }"}, {0xff000000, 0x91000000, No_VFP, &EmulateInstructionARM64::EmulateADDSUBImm, "ADD , , # {, }"}, {0xff000000, 0xb1000000, No_VFP, &EmulateInstructionARM64::EmulateADDSUBImm, "ADDS , , # {, }"}, {0xff000000, 0x51000000, No_VFP, &EmulateInstructionARM64::EmulateADDSUBImm, "SUB , , # {, }"}, {0xff000000, 0x71000000, No_VFP, &EmulateInstructionARM64::EmulateADDSUBImm, "SUBS , , # {, }"}, {0xff000000, 0x11000000, No_VFP, &EmulateInstructionARM64::EmulateADDSUBImm, "ADD , , # {, }"}, {0xff000000, 0x31000000, No_VFP, &EmulateInstructionARM64::EmulateADDSUBImm, "ADDS , , # {, }"}, {0xffc00000, 0x29000000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [{, #}]"}, {0xffc00000, 0xa9000000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [{, #}]"}, {0xffc00000, 0x2d000000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [{, #}]"}, {0xffc00000, 0x6d000000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP
, , [{, #}]"}, {0xffc00000, 0xad000000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [{, #}]"}, {0xffc00000, 0x29800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [, #]!"}, {0xffc00000, 0xa9800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [, #]!"}, {0xffc00000, 0x2d800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [, #]!"}, {0xffc00000, 0x6d800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP
, , [, #]!"}, {0xffc00000, 0xad800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [, #]!"}, {0xffc00000, 0x28800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [, #]!"}, {0xffc00000, 0xa8800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [, #]!"}, {0xffc00000, 0x2c800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [, #]!"}, {0xffc00000, 0x6c800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP
, , [, #]!"}, {0xffc00000, 0xac800000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "STP , , [, #]!"}, {0xffc00000, 0x29400000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [{, #}]"}, {0xffc00000, 0xa9400000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [{, #}]"}, {0xffc00000, 0x2d400000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [{, #}]"}, {0xffc00000, 0x6d400000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP
, , [{, #}]"}, {0xffc00000, 0xad400000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [{, #}]"}, {0xffc00000, 0x29c00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [, #]!"}, {0xffc00000, 0xa9c00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [, #]!"}, {0xffc00000, 0x2dc00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [, #]!"}, {0xffc00000, 0x6dc00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP
, , [, #]!"}, {0xffc00000, 0xadc00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [, #]!"}, {0xffc00000, 0x28c00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [, #]!"}, {0xffc00000, 0xa8c00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [, #]!"}, {0xffc00000, 0x2cc00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [, #]!"}, {0xffc00000, 0x6cc00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP
, , [, #]!"}, {0xffc00000, 0xacc00000, No_VFP, &EmulateInstructionARM64::EmulateLDPSTP, "LDP , , [, #]!"}, {0xffe00c00, 0xb8000400, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "STR , [], #"}, {0xffe00c00, 0xf8000400, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "STR , [], #"}, {0xffe00c00, 0xb8000c00, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "STR , [, #]!"}, {0xffe00c00, 0xf8000c00, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "STR , [, #]!"}, {0xffc00000, 0xb9000000, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "STR , [{, #}]"}, {0xffc00000, 0xf9000000, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "STR , [{, #}]"}, {0xffe00c00, 0xb8400400, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "LDR , [], #"}, {0xffe00c00, 0xf8400400, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "LDR , [], #"}, {0xffe00c00, 0xb8400c00, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "LDR , [, #]!"}, {0xffe00c00, 0xf8400c00, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "LDR , [, #]!"}, {0xffc00000, 0xb9400000, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "LDR , [{, #}]"}, {0xffc00000, 0xf9400000, No_VFP, &EmulateInstructionARM64::EmulateLDRSTRImm, "LDR , [{, #}]"}, {0xfc000000, 0x14000000, No_VFP, &EmulateInstructionARM64::EmulateB, "B