//===-- EmulateInstructionARM64.cpp ---------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "EmulateInstructionARM64.h" #include "lldb/Core/Address.h" #include "lldb/Core/PluginManager.h" #include "lldb/Symbol/UnwindPlan.h" #include "lldb/Utility/ArchSpec.h" #include "lldb/Utility/ConstString.h" #include "lldb/Utility/RegisterValue.h" #include "lldb/Utility/Stream.h" #include "llvm/Support/CheckedArithmetic.h" #include "Plugins/Process/Utility/ARMDefines.h" #include "Plugins/Process/Utility/ARMUtils.h" #include "Plugins/Process/Utility/lldb-arm64-register-enums.h" #include #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" #include "Plugins/Process/Utility/InstructionUtils.h" using namespace lldb; using namespace lldb_private; LLDB_PLUGIN_DEFINE_ADV(EmulateInstructionARM64, InstructionARM64) 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; } // LSL() // ===== static inline uint64_t LSL(uint64_t x, integer shift) { if (shift == 0) return x; return x << shift; } // 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 || arch.GetTriple().getArch() == llvm::Triple::aarch64_32) { return new EmulateInstructionARM64(arch); } } return nullptr; } 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