//===-- 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/ArchSpec.h" #include "lldb/Core/Address.h" #include "lldb/Core/ConstString.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/Stream.h" #include "lldb/Symbol/UnwindPlan.h" #include "Plugins/Process/Utility/ARMDefines.h" #include "Plugins/Process/Utility/ARMUtils.h" #include "Utility/ARM64_DWARF_Registers.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; #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 = eRegisterKindDWARF; reg_num = arm64_dwarf::pc; break; case LLDB_REGNUM_GENERIC_SP: reg_kind = eRegisterKindDWARF; reg_num = arm64_dwarf::sp; break; case LLDB_REGNUM_GENERIC_FP: reg_kind = eRegisterKindDWARF; reg_num = arm64_dwarf::fp; break; case LLDB_REGNUM_GENERIC_RA: reg_kind = eRegisterKindDWARF; reg_num = arm64_dwarf::lr; break; case LLDB_REGNUM_GENERIC_FLAGS: // There is no DWARF register number for the CPSR right now... reg_info.name = "cpsr"; reg_info.alt_name = NULL; reg_info.byte_size = 4; reg_info.byte_offset = 0; reg_info.encoding = eEncodingUint; reg_info.format = eFormatHex; for (uint32_t i=0; i, , # {, }" }, { 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