1 //===-- EmulateInstructionARM64.cpp -------------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #include "EmulateInstructionARM64.h" 12 13 #include <stdlib.h> 14 15 #include "lldb/Core/Address.h" 16 #include "lldb/Core/ArchSpec.h" 17 #include "lldb/Core/ConstString.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Core/Stream.h" 20 #include "lldb/Symbol/UnwindPlan.h" 21 22 #include "Plugins/Process/Utility/ARMDefines.h" 23 #include "Plugins/Process/Utility/ARMUtils.h" 24 #include "Utility/ARM64_DWARF_Registers.h" 25 26 #include "llvm/ADT/STLExtras.h" 27 #include "llvm/Support/MathExtras.h" // for SignExtend32 template function 28 // and CountTrailingZeros_32 function 29 30 #include "Plugins/Process/Utility/InstructionUtils.h" 31 32 using namespace lldb; 33 using namespace lldb_private; 34 35 #define No_VFP 0 36 #define VFPv1 (1u << 1) 37 #define VFPv2 (1u << 2) 38 #define VFPv3 (1u << 3) 39 #define AdvancedSIMD (1u << 4) 40 41 #define VFPv1_ABOVE (VFPv1 | VFPv2 | VFPv3 | AdvancedSIMD) 42 #define VFPv2_ABOVE (VFPv2 | VFPv3 | AdvancedSIMD) 43 #define VFPv2v3 (VFPv2 | VFPv3) 44 45 #define UInt(x) ((uint64_t)x) 46 #define SInt(x) ((int64_t)x) 47 #define bit bool 48 #define boolean bool 49 #define integer int64_t 50 51 static inline bool IsZero(uint64_t x) { return x == 0; } 52 53 static inline uint64_t NOT(uint64_t x) { return ~x; } 54 55 #if 0 56 // LSL_C() 57 // ======= 58 static inline uint64_t 59 LSL_C (uint64_t x, integer shift, bool &carry_out) 60 { 61 assert (shift >= 0); 62 uint64_t result = x << shift; 63 carry_out = ((1ull << (64-1)) >> (shift - 1)) != 0; 64 return result; 65 } 66 #endif 67 68 // LSL() 69 // ===== 70 71 static inline uint64_t LSL(uint64_t x, integer shift) { 72 if (shift == 0) 73 return x; 74 return x << shift; 75 } 76 77 // AddWithCarry() 78 // =============== 79 static inline uint64_t 80 AddWithCarry(uint32_t N, uint64_t x, uint64_t y, bit carry_in, 81 EmulateInstructionARM64::ProcState &proc_state) { 82 uint64_t unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in); 83 int64_t signed_sum = SInt(x) + SInt(y) + UInt(carry_in); 84 uint64_t result = unsigned_sum; 85 if (N < 64) 86 result = Bits64(result, N - 1, 0); 87 proc_state.N = Bit64(result, N - 1); 88 proc_state.Z = IsZero(result); 89 proc_state.C = UInt(result) == unsigned_sum; 90 proc_state.V = SInt(result) == signed_sum; 91 return result; 92 } 93 94 // ConstrainUnpredictable() 95 // ======================== 96 97 EmulateInstructionARM64::ConstraintType 98 ConstrainUnpredictable(EmulateInstructionARM64::Unpredictable which) { 99 EmulateInstructionARM64::ConstraintType result = 100 EmulateInstructionARM64::Constraint_UNKNOWN; 101 switch (which) { 102 case EmulateInstructionARM64::Unpredictable_WBOVERLAP: 103 case EmulateInstructionARM64::Unpredictable_LDPOVERLAP: 104 // TODO: don't know what to really do here? Pseudo code says: 105 // set result to one of above Constraint behaviours or UNDEFINED 106 break; 107 } 108 return result; 109 } 110 111 //---------------------------------------------------------------------- 112 // 113 // EmulateInstructionARM implementation 114 // 115 //---------------------------------------------------------------------- 116 117 void EmulateInstructionARM64::Initialize() { 118 PluginManager::RegisterPlugin(GetPluginNameStatic(), 119 GetPluginDescriptionStatic(), CreateInstance); 120 } 121 122 void EmulateInstructionARM64::Terminate() { 123 PluginManager::UnregisterPlugin(CreateInstance); 124 } 125 126 ConstString EmulateInstructionARM64::GetPluginNameStatic() { 127 ConstString g_plugin_name("lldb.emulate-instruction.arm64"); 128 return g_plugin_name; 129 } 130 131 lldb_private::ConstString EmulateInstructionARM64::GetPluginName() { 132 static ConstString g_plugin_name("EmulateInstructionARM64"); 133 return g_plugin_name; 134 } 135 136 const char *EmulateInstructionARM64::GetPluginDescriptionStatic() { 137 return "Emulate instructions for the ARM64 architecture."; 138 } 139 140 EmulateInstruction * 141 EmulateInstructionARM64::CreateInstance(const ArchSpec &arch, 142 InstructionType inst_type) { 143 if (EmulateInstructionARM64::SupportsEmulatingInstructionsOfTypeStatic( 144 inst_type)) { 145 if (arch.GetTriple().getArch() == llvm::Triple::aarch64) { 146 std::auto_ptr<EmulateInstructionARM64> emulate_insn_ap( 147 new EmulateInstructionARM64(arch)); 148 if (emulate_insn_ap.get()) 149 return emulate_insn_ap.release(); 150 } 151 } 152 153 return NULL; 154 } 155 156 bool EmulateInstructionARM64::SetTargetTriple(const ArchSpec &arch) { 157 if (arch.GetTriple().getArch() == llvm::Triple::arm) 158 return true; 159 else if (arch.GetTriple().getArch() == llvm::Triple::thumb) 160 return true; 161 162 return false; 163 } 164 165 bool EmulateInstructionARM64::GetRegisterInfo(RegisterKind reg_kind, 166 uint32_t reg_num, 167 RegisterInfo ®_info) { 168 if (reg_kind == eRegisterKindGeneric) { 169 switch (reg_num) { 170 case LLDB_REGNUM_GENERIC_PC: 171 reg_kind = eRegisterKindDWARF; 172 reg_num = arm64_dwarf::pc; 173 break; 174 case LLDB_REGNUM_GENERIC_SP: 175 reg_kind = eRegisterKindDWARF; 176 reg_num = arm64_dwarf::sp; 177 break; 178 case LLDB_REGNUM_GENERIC_FP: 179 reg_kind = eRegisterKindDWARF; 180 reg_num = arm64_dwarf::fp; 181 break; 182 case LLDB_REGNUM_GENERIC_RA: 183 reg_kind = eRegisterKindDWARF; 184 reg_num = arm64_dwarf::lr; 185 break; 186 case LLDB_REGNUM_GENERIC_FLAGS: 187 // There is no DWARF register number for the CPSR right now... 188 reg_info.name = "cpsr"; 189 reg_info.alt_name = NULL; 190 reg_info.byte_size = 4; 191 reg_info.byte_offset = 0; 192 reg_info.encoding = eEncodingUint; 193 reg_info.format = eFormatHex; 194 for (uint32_t i = 0; i < lldb::kNumRegisterKinds; ++i) 195 reg_info.kinds[reg_kind] = LLDB_INVALID_REGNUM; 196 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS; 197 return true; 198 199 default: 200 return false; 201 } 202 } 203 204 if (reg_kind == eRegisterKindDWARF) 205 return arm64_dwarf::GetRegisterInfo(reg_num, reg_info); 206 return false; 207 } 208 209 EmulateInstructionARM64::Opcode * 210 EmulateInstructionARM64::GetOpcodeForInstruction(const uint32_t opcode) { 211 static EmulateInstructionARM64::Opcode g_opcodes[] = { 212 //---------------------------------------------------------------------- 213 // Prologue instructions 214 //---------------------------------------------------------------------- 215 216 // push register(s) 217 {0xff000000, 0xd1000000, No_VFP, 218 &EmulateInstructionARM64::EmulateADDSUBImm, 219 "SUB <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}"}, 220 {0xff000000, 0xf1000000, No_VFP, 221 &EmulateInstructionARM64::EmulateADDSUBImm, 222 "SUBS <Xd>, <Xn|SP>, #<imm> {, <shift>}"}, 223 {0xff000000, 0x91000000, No_VFP, 224 &EmulateInstructionARM64::EmulateADDSUBImm, 225 "ADD <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}"}, 226 {0xff000000, 0xb1000000, No_VFP, 227 &EmulateInstructionARM64::EmulateADDSUBImm, 228 "ADDS <Xd>, <Xn|SP>, #<imm> {, <shift>}"}, 229 230 {0xff000000, 0x51000000, No_VFP, 231 &EmulateInstructionARM64::EmulateADDSUBImm, 232 "SUB <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}"}, 233 {0xff000000, 0x71000000, No_VFP, 234 &EmulateInstructionARM64::EmulateADDSUBImm, 235 "SUBS <Wd>, <Wn|WSP>, #<imm> {, <shift>}"}, 236 {0xff000000, 0x11000000, No_VFP, 237 &EmulateInstructionARM64::EmulateADDSUBImm, 238 "ADD <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}"}, 239 {0xff000000, 0x31000000, No_VFP, 240 &EmulateInstructionARM64::EmulateADDSUBImm, 241 "ADDS <Wd>, <Wn|WSP>, #<imm> {, <shift>}"}, 242 243 {0xffc00000, 0x29000000, No_VFP, 244 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 245 "STP <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]"}, 246 {0xffc00000, 0xa9000000, No_VFP, 247 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 248 "STP <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]"}, 249 {0xffc00000, 0x2d000000, No_VFP, 250 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 251 "STP <St>, <St2>, [<Xn|SP>{, #<imm>}]"}, 252 {0xffc00000, 0x6d000000, No_VFP, 253 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 254 "STP <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]"}, 255 {0xffc00000, 0xad000000, No_VFP, 256 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 257 "STP <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]"}, 258 259 {0xffc00000, 0x29800000, No_VFP, 260 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 261 "STP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"}, 262 {0xffc00000, 0xa9800000, No_VFP, 263 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 264 "STP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"}, 265 {0xffc00000, 0x2d800000, No_VFP, 266 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 267 "STP <St>, <St2>, [<Xn|SP>, #<imm>]!"}, 268 {0xffc00000, 0x6d800000, No_VFP, 269 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 270 "STP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"}, 271 {0xffc00000, 0xad800000, No_VFP, 272 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 273 "STP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"}, 274 275 {0xffc00000, 0x28800000, No_VFP, 276 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 277 "STP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"}, 278 {0xffc00000, 0xa8800000, No_VFP, 279 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 280 "STP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"}, 281 {0xffc00000, 0x2c800000, No_VFP, 282 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 283 "STP <St>, <St2>, [<Xn|SP>, #<imm>]!"}, 284 {0xffc00000, 0x6c800000, No_VFP, 285 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 286 "STP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"}, 287 {0xffc00000, 0xac800000, No_VFP, 288 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 289 "STP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"}, 290 291 {0xffc00000, 0x29400000, No_VFP, 292 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 293 "LDP <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]"}, 294 {0xffc00000, 0xa9400000, No_VFP, 295 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 296 "LDP <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]"}, 297 {0xffc00000, 0x2d400000, No_VFP, 298 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 299 "LDP <St>, <St2>, [<Xn|SP>{, #<imm>}]"}, 300 {0xffc00000, 0x6d400000, No_VFP, 301 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 302 "LDP <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]"}, 303 {0xffc00000, 0xad400000, No_VFP, 304 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>, 305 "LDP <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]"}, 306 307 {0xffc00000, 0x29c00000, No_VFP, 308 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 309 "LDP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"}, 310 {0xffc00000, 0xa9c00000, No_VFP, 311 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 312 "LDP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"}, 313 {0xffc00000, 0x2dc00000, No_VFP, 314 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 315 "LDP <St>, <St2>, [<Xn|SP>, #<imm>]!"}, 316 {0xffc00000, 0x6dc00000, No_VFP, 317 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 318 "LDP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"}, 319 {0xffc00000, 0xadc00000, No_VFP, 320 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>, 321 "LDP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"}, 322 323 {0xffc00000, 0x28c00000, No_VFP, 324 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 325 "LDP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"}, 326 {0xffc00000, 0xa8c00000, No_VFP, 327 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 328 "LDP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"}, 329 {0xffc00000, 0x2cc00000, No_VFP, 330 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 331 "LDP <St>, <St2>, [<Xn|SP>, #<imm>]!"}, 332 {0xffc00000, 0x6cc00000, No_VFP, 333 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 334 "LDP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"}, 335 {0xffc00000, 0xacc00000, No_VFP, 336 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>, 337 "LDP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"}, 338 339 {0xffe00c00, 0xb8000400, No_VFP, 340 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>, 341 "STR <Wt>, [<Xn|SP>], #<simm>"}, 342 {0xffe00c00, 0xf8000400, No_VFP, 343 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>, 344 "STR <Xt>, [<Xn|SP>], #<simm>"}, 345 {0xffe00c00, 0xb8000c00, No_VFP, 346 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>, 347 "STR <Wt>, [<Xn|SP>, #<simm>]!"}, 348 {0xffe00c00, 0xf8000c00, No_VFP, 349 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>, 350 "STR <Xt>, [<Xn|SP>, #<simm>]!"}, 351 {0xffc00000, 0xb9000000, No_VFP, 352 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>, 353 "STR <Wt>, [<Xn|SP>{, #<pimm>}]"}, 354 {0xffc00000, 0xf9000000, No_VFP, 355 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>, 356 "STR <Xt>, [<Xn|SP>{, #<pimm>}]"}, 357 358 {0xffe00c00, 0xb8400400, No_VFP, 359 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>, 360 "LDR <Wt>, [<Xn|SP>], #<simm>"}, 361 {0xffe00c00, 0xf8400400, No_VFP, 362 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>, 363 "LDR <Xt>, [<Xn|SP>], #<simm>"}, 364 {0xffe00c00, 0xb8400c00, No_VFP, 365 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>, 366 "LDR <Wt>, [<Xn|SP>, #<simm>]!"}, 367 {0xffe00c00, 0xf8400c00, No_VFP, 368 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>, 369 "LDR <Xt>, [<Xn|SP>, #<simm>]!"}, 370 {0xffc00000, 0xb9400000, No_VFP, 371 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>, 372 "LDR <Wt>, [<Xn|SP>{, #<pimm>}]"}, 373 {0xffc00000, 0xf9400000, No_VFP, 374 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>, 375 "LDR <Xt>, [<Xn|SP>{, #<pimm>}]"}, 376 377 {0xfc000000, 0x14000000, No_VFP, &EmulateInstructionARM64::EmulateB, 378 "B <label>"}, 379 {0xff000010, 0x54000000, No_VFP, &EmulateInstructionARM64::EmulateBcond, 380 "B.<cond> <label>"}, 381 {0x7f000000, 0x34000000, No_VFP, &EmulateInstructionARM64::EmulateCBZ, 382 "CBZ <Wt>, <label>"}, 383 {0x7f000000, 0x35000000, No_VFP, &EmulateInstructionARM64::EmulateCBZ, 384 "CBNZ <Wt>, <label>"}, 385 {0x7f000000, 0x36000000, No_VFP, &EmulateInstructionARM64::EmulateTBZ, 386 "TBZ <R><t>, #<imm>, <label>"}, 387 {0x7f000000, 0x37000000, No_VFP, &EmulateInstructionARM64::EmulateTBZ, 388 "TBNZ <R><t>, #<imm>, <label>"}, 389 390 }; 391 static const size_t k_num_arm_opcodes = llvm::array_lengthof(g_opcodes); 392 393 for (size_t i = 0; i < k_num_arm_opcodes; ++i) { 394 if ((g_opcodes[i].mask & opcode) == g_opcodes[i].value) 395 return &g_opcodes[i]; 396 } 397 return nullptr; 398 } 399 400 bool EmulateInstructionARM64::ReadInstruction() { 401 bool success = false; 402 m_addr = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 403 LLDB_INVALID_ADDRESS, &success); 404 if (success) { 405 Context read_inst_context; 406 read_inst_context.type = eContextReadOpcode; 407 read_inst_context.SetNoArgs(); 408 m_opcode.SetOpcode32( 409 ReadMemoryUnsigned(read_inst_context, m_addr, 4, 0, &success), 410 GetByteOrder()); 411 } 412 if (!success) 413 m_addr = LLDB_INVALID_ADDRESS; 414 return success; 415 } 416 417 bool EmulateInstructionARM64::EvaluateInstruction(uint32_t evaluate_options) { 418 const uint32_t opcode = m_opcode.GetOpcode32(); 419 Opcode *opcode_data = GetOpcodeForInstruction(opcode); 420 if (opcode_data == NULL) 421 return false; 422 423 // printf ("opcode template for 0x%8.8x: %s\n", opcode, opcode_data->name); 424 const bool auto_advance_pc = 425 evaluate_options & eEmulateInstructionOptionAutoAdvancePC; 426 m_ignore_conditions = 427 evaluate_options & eEmulateInstructionOptionIgnoreConditions; 428 429 bool success = false; 430 // if (m_opcode_cpsr == 0 || m_ignore_conditions == false) 431 // { 432 // m_opcode_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, 433 // // use eRegisterKindDWARF is we ever get a cpsr DWARF register 434 // number 435 // LLDB_REGNUM_GENERIC_FLAGS, 436 // // use arm64_dwarf::cpsr if we 437 // ever get one 438 // 0, 439 // &success); 440 // } 441 442 // Only return false if we are unable to read the CPSR if we care about 443 // conditions 444 if (success == false && m_ignore_conditions == false) 445 return false; 446 447 uint32_t orig_pc_value = 0; 448 if (auto_advance_pc) { 449 orig_pc_value = 450 ReadRegisterUnsigned(eRegisterKindDWARF, arm64_dwarf::pc, 0, &success); 451 if (!success) 452 return false; 453 } 454 455 // Call the Emulate... function. 456 success = (this->*opcode_data->callback)(opcode); 457 if (!success) 458 return false; 459 460 if (auto_advance_pc) { 461 uint32_t new_pc_value = 462 ReadRegisterUnsigned(eRegisterKindDWARF, arm64_dwarf::pc, 0, &success); 463 if (!success) 464 return false; 465 466 if (auto_advance_pc && (new_pc_value == orig_pc_value)) { 467 EmulateInstruction::Context context; 468 context.type = eContextAdvancePC; 469 context.SetNoArgs(); 470 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, arm64_dwarf::pc, 471 orig_pc_value + 4)) 472 return false; 473 } 474 } 475 return true; 476 } 477 478 bool EmulateInstructionARM64::CreateFunctionEntryUnwind( 479 UnwindPlan &unwind_plan) { 480 unwind_plan.Clear(); 481 unwind_plan.SetRegisterKind(eRegisterKindDWARF); 482 483 UnwindPlan::RowSP row(new UnwindPlan::Row); 484 485 // Our previous Call Frame Address is the stack pointer 486 row->GetCFAValue().SetIsRegisterPlusOffset(arm64_dwarf::sp, 0); 487 488 unwind_plan.AppendRow(row); 489 unwind_plan.SetSourceName("EmulateInstructionARM64"); 490 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo); 491 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolYes); 492 unwind_plan.SetReturnAddressRegister(arm64_dwarf::lr); 493 return true; 494 } 495 496 uint32_t EmulateInstructionARM64::GetFramePointerRegisterNumber() const { 497 if (m_arch.GetTriple().isAndroid()) 498 return LLDB_INVALID_REGNUM; // Don't use frame pointer on android 499 500 return arm64_dwarf::fp; 501 } 502 503 bool EmulateInstructionARM64::UsingAArch32() { 504 bool aarch32 = m_opcode_pstate.RW == 1; 505 // if !HaveAnyAArch32() then assert !aarch32; 506 // if HighestELUsingAArch32() then assert aarch32; 507 return aarch32; 508 } 509 510 bool EmulateInstructionARM64::BranchTo(const Context &context, uint32_t N, 511 addr_t target) { 512 #if 0 513 // Set program counter to a new address, with a branch reason hint 514 // for possible use by hardware fetching the next instruction. 515 BranchTo(bits(N) target, BranchType branch_type) 516 Hint_Branch(branch_type); 517 if N == 32 then 518 assert UsingAArch32(); 519 _PC = ZeroExtend(target); 520 else 521 assert N == 64 && !UsingAArch32(); 522 // Remove the tag bits from a tagged target 523 case PSTATE.EL of 524 when EL0, EL1 525 if target<55> == '1' && TCR_EL1.TBI1 == '1' then 526 target<63:56> = '11111111'; 527 if target<55> == '0' && TCR_EL1.TBI0 == '1' then 528 target<63:56> = '00000000'; 529 when EL2 530 if TCR_EL2.TBI == '1' then 531 target<63:56> = '00000000'; 532 when EL3 533 if TCR_EL3.TBI == '1' then 534 target<63:56> = '00000000'; 535 _PC = target<63:0>; 536 return; 537 #endif 538 539 addr_t addr; 540 541 // Hint_Branch(branch_type); 542 if (N == 32) { 543 if (!UsingAArch32()) 544 return false; 545 addr = target; 546 } else if (N == 64) { 547 if (UsingAArch32()) 548 return false; 549 // TODO: Remove the tag bits from a tagged target 550 addr = target; 551 } else 552 return false; 553 554 if (!WriteRegisterUnsigned(context, eRegisterKindGeneric, 555 LLDB_REGNUM_GENERIC_PC, addr)) 556 return false; 557 558 return true; 559 } 560 561 bool EmulateInstructionARM64::ConditionHolds(const uint32_t cond) { 562 // If we are ignoring conditions, then always return true. 563 // this allows us to iterate over disassembly code and still 564 // emulate an instruction even if we don't have all the right 565 // bits set in the CPSR register... 566 if (m_ignore_conditions) 567 return true; 568 569 bool result = false; 570 switch (UnsignedBits(cond, 3, 1)) { 571 case 0: 572 result = (m_opcode_pstate.Z == 1); 573 break; 574 case 1: 575 result = (m_opcode_pstate.C == 1); 576 break; 577 case 2: 578 result = (m_opcode_pstate.N == 1); 579 break; 580 case 3: 581 result = (m_opcode_pstate.V == 1); 582 break; 583 case 4: 584 result = (m_opcode_pstate.C == 1 && m_opcode_pstate.Z == 0); 585 break; 586 case 5: 587 result = (m_opcode_pstate.N == m_opcode_pstate.V); 588 break; 589 case 6: 590 result = (m_opcode_pstate.N == m_opcode_pstate.V && m_opcode_pstate.Z == 0); 591 break; 592 case 7: 593 // Always execute (cond == 0b1110, or the special 0b1111 which gives 594 // opcodes different meanings, but always means execution happens. 595 return true; 596 } 597 598 if (cond & 1) 599 result = !result; 600 return result; 601 } 602 603 bool EmulateInstructionARM64::EmulateADDSUBImm(const uint32_t opcode) { 604 // integer d = UInt(Rd); 605 // integer n = UInt(Rn); 606 // integer datasize = if sf == 1 then 64 else 32; 607 // boolean sub_op = (op == 1); 608 // boolean setflags = (S == 1); 609 // bits(datasize) imm; 610 // 611 // case shift of 612 // when '00' imm = ZeroExtend(imm12, datasize); 613 // when '01' imm = ZeroExtend(imm12 : Zeros(12), datasize); 614 // when '1x' UNDEFINED; 615 // 616 // 617 // bits(datasize) result; 618 // bits(datasize) operand1 = if n == 31 then SP[] else X[n]; 619 // bits(datasize) operand2 = imm; 620 // bits(4) nzcv; 621 // bit carry_in; 622 // 623 // if sub_op then 624 // operand2 = NOT(operand2); 625 // carry_in = 1; 626 // else 627 // carry_in = 0; 628 // 629 // (result, nzcv) = AddWithCarry(operand1, operand2, carry_in); 630 // 631 // if setflags then 632 // PSTATE.NZCV = nzcv; 633 // 634 // if d == 31 && !setflags then 635 // SP[] = result; 636 // else 637 // X[d] = result; 638 639 const uint32_t sf = Bit32(opcode, 31); 640 const uint32_t op = Bit32(opcode, 30); 641 const uint32_t S = Bit32(opcode, 29); 642 const uint32_t shift = Bits32(opcode, 23, 22); 643 const uint32_t imm12 = Bits32(opcode, 21, 10); 644 const uint32_t Rn = Bits32(opcode, 9, 5); 645 const uint32_t Rd = Bits32(opcode, 4, 0); 646 647 bool success = false; 648 649 const uint32_t d = UInt(Rd); 650 const uint32_t n = UInt(Rn); 651 const uint32_t datasize = (sf == 1) ? 64 : 32; 652 boolean sub_op = op == 1; 653 boolean setflags = S == 1; 654 uint64_t imm; 655 656 switch (shift) { 657 case 0: 658 imm = imm12; 659 break; 660 case 1: 661 imm = imm12 << 12; 662 break; 663 default: 664 return false; // UNDEFINED; 665 } 666 uint64_t result; 667 uint64_t operand1 = ReadRegisterUnsigned(eRegisterKindDWARF, 668 arm64_dwarf::x0 + n, 0, &success); 669 uint64_t operand2 = imm; 670 bit carry_in; 671 672 if (sub_op) { 673 operand2 = NOT(operand2); 674 carry_in = 1; 675 imm = -imm; // For the Register plug offset context below 676 } else { 677 carry_in = 0; 678 } 679 680 ProcState proc_state; 681 682 result = AddWithCarry(datasize, operand1, operand2, carry_in, proc_state); 683 684 if (setflags) { 685 m_emulated_pstate.N = proc_state.N; 686 m_emulated_pstate.Z = proc_state.Z; 687 m_emulated_pstate.C = proc_state.C; 688 m_emulated_pstate.V = proc_state.V; 689 } 690 691 Context context; 692 RegisterInfo reg_info_Rn; 693 if (arm64_dwarf::GetRegisterInfo(n, reg_info_Rn)) 694 context.SetRegisterPlusOffset(reg_info_Rn, imm); 695 696 if (n == GetFramePointerRegisterNumber() && d == arm64_dwarf::sp && 697 !setflags) { 698 // 'mov sp, fp' - common epilogue instruction, CFA is now in terms 699 // of the stack pointer, instead of frame pointer. 700 context.type = EmulateInstruction::eContextRestoreStackPointer; 701 } else if ((n == arm64_dwarf::sp || n == GetFramePointerRegisterNumber()) && 702 d == arm64_dwarf::sp && !setflags) { 703 context.type = EmulateInstruction::eContextAdjustStackPointer; 704 } else if (d == GetFramePointerRegisterNumber() && n == arm64_dwarf::sp && 705 !setflags) { 706 context.type = EmulateInstruction::eContextSetFramePointer; 707 } else { 708 context.type = EmulateInstruction::eContextImmediate; 709 } 710 711 // If setflags && d == arm64_dwarf::sp then d = WZR/XZR. See CMN, CMP 712 if (!setflags || d != arm64_dwarf::sp) 713 WriteRegisterUnsigned(context, eRegisterKindDWARF, arm64_dwarf::x0 + d, 714 result); 715 716 return false; 717 } 718 719 template <EmulateInstructionARM64::AddrMode a_mode> 720 bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode) { 721 uint32_t opc = Bits32(opcode, 31, 30); 722 uint32_t V = Bit32(opcode, 26); 723 uint32_t L = Bit32(opcode, 22); 724 uint32_t imm7 = Bits32(opcode, 21, 15); 725 uint32_t Rt2 = Bits32(opcode, 14, 10); 726 uint32_t Rn = Bits32(opcode, 9, 5); 727 uint32_t Rt = Bits32(opcode, 4, 0); 728 729 integer n = UInt(Rn); 730 integer t = UInt(Rt); 731 integer t2 = UInt(Rt2); 732 uint64_t idx; 733 734 MemOp memop = L == 1 ? MemOp_LOAD : MemOp_STORE; 735 boolean vector = (V == 1); 736 // AccType acctype = AccType_NORMAL; 737 boolean is_signed = false; 738 boolean wback = a_mode != AddrMode_OFF; 739 boolean wb_unknown = false; 740 boolean rt_unknown = false; 741 integer scale; 742 integer size; 743 744 if (opc == 3) 745 return false; // UNDEFINED 746 747 if (vector) { 748 scale = 2 + UInt(opc); 749 } else { 750 scale = (opc & 2) ? 3 : 2; 751 is_signed = (opc & 1) != 0; 752 if (is_signed && memop == MemOp_STORE) 753 return false; // UNDEFINED 754 } 755 756 if (!vector && wback && ((t == n) || (t2 == n))) { 757 switch (ConstrainUnpredictable(Unpredictable_WBOVERLAP)) { 758 case Constraint_UNKNOWN: 759 wb_unknown = true; // writeback is UNKNOWN 760 break; 761 762 case Constraint_SUPPRESSWB: 763 wback = false; // writeback is suppressed 764 break; 765 766 case Constraint_NOP: 767 memop = MemOp_NOP; // do nothing 768 wback = false; 769 break; 770 771 case Constraint_NONE: 772 break; 773 } 774 } 775 776 if (memop == MemOp_LOAD && t == t2) { 777 switch (ConstrainUnpredictable(Unpredictable_LDPOVERLAP)) { 778 case Constraint_UNKNOWN: 779 rt_unknown = true; // result is UNKNOWN 780 break; 781 782 case Constraint_NOP: 783 memop = MemOp_NOP; // do nothing 784 wback = false; 785 break; 786 787 default: 788 break; 789 } 790 } 791 792 idx = LSL(llvm::SignExtend64<7>(imm7), scale); 793 size = (integer)1 << scale; 794 uint64_t datasize = size * 8; 795 uint64_t address; 796 uint64_t wb_address; 797 798 RegisterValue data_Rt; 799 RegisterValue data_Rt2; 800 801 // if (vector) 802 // CheckFPEnabled(false); 803 804 RegisterInfo reg_info_base; 805 RegisterInfo reg_info_Rt; 806 RegisterInfo reg_info_Rt2; 807 if (!GetRegisterInfo(eRegisterKindDWARF, arm64_dwarf::x0 + n, reg_info_base)) 808 return false; 809 810 if (vector) { 811 if (!GetRegisterInfo(eRegisterKindDWARF, arm64_dwarf::v0 + n, reg_info_Rt)) 812 return false; 813 if (!GetRegisterInfo(eRegisterKindDWARF, arm64_dwarf::v0 + n, reg_info_Rt2)) 814 return false; 815 } else { 816 if (!GetRegisterInfo(eRegisterKindDWARF, arm64_dwarf::x0 + t, reg_info_Rt)) 817 return false; 818 if (!GetRegisterInfo(eRegisterKindDWARF, arm64_dwarf::x0 + t2, 819 reg_info_Rt2)) 820 return false; 821 } 822 823 bool success = false; 824 if (n == 31) { 825 // CheckSPAlignment(); 826 address = 827 ReadRegisterUnsigned(eRegisterKindDWARF, arm64_dwarf::sp, 0, &success); 828 } else 829 address = ReadRegisterUnsigned(eRegisterKindDWARF, arm64_dwarf::x0 + n, 0, 830 &success); 831 832 wb_address = address + idx; 833 if (a_mode != AddrMode_POST) 834 address = wb_address; 835 836 Context context_t; 837 Context context_t2; 838 839 uint8_t buffer[RegisterValue::kMaxRegisterByteSize]; 840 Error error; 841 842 switch (memop) { 843 case MemOp_STORE: { 844 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is 845 // based off of the sp 846 // or fp register 847 { 848 context_t.type = eContextPushRegisterOnStack; 849 context_t2.type = eContextPushRegisterOnStack; 850 } else { 851 context_t.type = eContextRegisterStore; 852 context_t2.type = eContextRegisterStore; 853 } 854 context_t.SetRegisterToRegisterPlusOffset(reg_info_Rt, reg_info_base, 0); 855 context_t2.SetRegisterToRegisterPlusOffset(reg_info_Rt2, reg_info_base, 856 size); 857 858 if (!ReadRegister(®_info_Rt, data_Rt)) 859 return false; 860 861 if (data_Rt.GetAsMemoryData(®_info_Rt, buffer, reg_info_Rt.byte_size, 862 eByteOrderLittle, error) == 0) 863 return false; 864 865 if (!WriteMemory(context_t, address + 0, buffer, reg_info_Rt.byte_size)) 866 return false; 867 868 if (!ReadRegister(®_info_Rt2, data_Rt2)) 869 return false; 870 871 if (data_Rt2.GetAsMemoryData(®_info_Rt2, buffer, reg_info_Rt2.byte_size, 872 eByteOrderLittle, error) == 0) 873 return false; 874 875 if (!WriteMemory(context_t2, address + size, buffer, 876 reg_info_Rt2.byte_size)) 877 return false; 878 } break; 879 880 case MemOp_LOAD: { 881 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this load is 882 // based off of the sp 883 // or fp register 884 { 885 context_t.type = eContextPopRegisterOffStack; 886 context_t2.type = eContextPopRegisterOffStack; 887 } else { 888 context_t.type = eContextRegisterLoad; 889 context_t2.type = eContextRegisterLoad; 890 } 891 context_t.SetAddress(address); 892 context_t2.SetAddress(address + size); 893 894 if (rt_unknown) 895 memset(buffer, 'U', reg_info_Rt.byte_size); 896 else { 897 if (!ReadMemory(context_t, address, buffer, reg_info_Rt.byte_size)) 898 return false; 899 } 900 901 if (data_Rt.SetFromMemoryData(®_info_Rt, buffer, reg_info_Rt.byte_size, 902 eByteOrderLittle, error) == 0) 903 return false; 904 905 if (!vector && is_signed && !data_Rt.SignExtend(datasize)) 906 return false; 907 908 if (!WriteRegister(context_t, ®_info_Rt, data_Rt)) 909 return false; 910 911 if (!rt_unknown) { 912 if (!ReadMemory(context_t2, address + size, buffer, 913 reg_info_Rt2.byte_size)) 914 return false; 915 } 916 917 if (data_Rt2.SetFromMemoryData(®_info_Rt2, buffer, 918 reg_info_Rt2.byte_size, eByteOrderLittle, 919 error) == 0) 920 return false; 921 922 if (!vector && is_signed && !data_Rt2.SignExtend(datasize)) 923 return false; 924 925 if (!WriteRegister(context_t2, ®_info_Rt2, data_Rt2)) 926 return false; 927 } break; 928 929 default: 930 break; 931 } 932 933 if (wback) { 934 if (wb_unknown) 935 wb_address = LLDB_INVALID_ADDRESS; 936 Context context; 937 context.SetImmediateSigned(idx); 938 if (n == 31) 939 context.type = eContextAdjustStackPointer; 940 else 941 context.type = eContextAdjustBaseRegister; 942 WriteRegisterUnsigned(context, ®_info_base, wb_address); 943 } 944 return true; 945 } 946 947 template <EmulateInstructionARM64::AddrMode a_mode> 948 bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode) { 949 uint32_t size = Bits32(opcode, 31, 30); 950 uint32_t opc = Bits32(opcode, 23, 22); 951 uint32_t n = Bits32(opcode, 9, 5); 952 uint32_t t = Bits32(opcode, 4, 0); 953 954 bool wback; 955 bool postindex; 956 uint64_t offset; 957 958 switch (a_mode) { 959 case AddrMode_POST: 960 wback = true; 961 postindex = true; 962 offset = llvm::SignExtend64<9>(Bits32(opcode, 20, 12)); 963 break; 964 case AddrMode_PRE: 965 wback = true; 966 postindex = false; 967 offset = llvm::SignExtend64<9>(Bits32(opcode, 20, 12)); 968 break; 969 case AddrMode_OFF: 970 wback = false; 971 postindex = false; 972 offset = LSL(Bits32(opcode, 21, 10), size); 973 break; 974 } 975 976 MemOp memop; 977 978 if (Bit32(opc, 1) == 0) { 979 memop = Bit32(opc, 0) == 1 ? MemOp_LOAD : MemOp_STORE; 980 } else { 981 memop = MemOp_LOAD; 982 if (size == 2 && Bit32(opc, 0) == 1) 983 return false; 984 } 985 986 Error error; 987 bool success = false; 988 uint64_t address; 989 uint8_t buffer[RegisterValue::kMaxRegisterByteSize]; 990 RegisterValue data_Rt; 991 992 if (n == 31) 993 address = 994 ReadRegisterUnsigned(eRegisterKindDWARF, arm64_dwarf::sp, 0, &success); 995 else 996 address = ReadRegisterUnsigned(eRegisterKindDWARF, arm64_dwarf::x0 + n, 0, 997 &success); 998 999 if (!success) 1000 return false; 1001 1002 if (!postindex) 1003 address += offset; 1004 1005 RegisterInfo reg_info_base; 1006 if (!GetRegisterInfo(eRegisterKindDWARF, arm64_dwarf::x0 + n, reg_info_base)) 1007 return false; 1008 1009 RegisterInfo reg_info_Rt; 1010 if (!GetRegisterInfo(eRegisterKindDWARF, arm64_dwarf::x0 + t, reg_info_Rt)) 1011 return false; 1012 1013 Context context; 1014 switch (memop) { 1015 case MemOp_STORE: 1016 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is 1017 // based off of the sp 1018 // or fp register 1019 context.type = eContextPushRegisterOnStack; 1020 else 1021 context.type = eContextRegisterStore; 1022 context.SetRegisterToRegisterPlusOffset(reg_info_Rt, reg_info_base, 1023 postindex ? 0 : offset); 1024 1025 if (!ReadRegister(®_info_Rt, data_Rt)) 1026 return false; 1027 1028 if (data_Rt.GetAsMemoryData(®_info_Rt, buffer, reg_info_Rt.byte_size, 1029 eByteOrderLittle, error) == 0) 1030 return false; 1031 1032 if (!WriteMemory(context, address, buffer, reg_info_Rt.byte_size)) 1033 return false; 1034 break; 1035 1036 case MemOp_LOAD: 1037 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is 1038 // based off of the sp 1039 // or fp register 1040 context.type = eContextPopRegisterOffStack; 1041 else 1042 context.type = eContextRegisterLoad; 1043 context.SetAddress(address); 1044 1045 if (!ReadMemory(context, address, buffer, reg_info_Rt.byte_size)) 1046 return false; 1047 1048 if (data_Rt.SetFromMemoryData(®_info_Rt, buffer, reg_info_Rt.byte_size, 1049 eByteOrderLittle, error) == 0) 1050 return false; 1051 1052 if (!WriteRegister(context, ®_info_Rt, data_Rt)) 1053 return false; 1054 break; 1055 default: 1056 return false; 1057 } 1058 1059 if (wback) { 1060 if (postindex) 1061 address += offset; 1062 1063 if (n == 31) 1064 context.type = eContextAdjustStackPointer; 1065 else 1066 context.type = eContextAdjustBaseRegister; 1067 context.SetImmediateSigned(offset); 1068 1069 if (!WriteRegisterUnsigned(context, ®_info_base, address)) 1070 return false; 1071 } 1072 return true; 1073 } 1074 1075 bool EmulateInstructionARM64::EmulateB(const uint32_t opcode) { 1076 #if 0 1077 // ARM64 pseudo code... 1078 if branch_type == BranchType_CALL then X[30] = PC[] + 4; 1079 BranchTo(PC[] + offset, branch_type); 1080 #endif 1081 1082 bool success = false; 1083 1084 EmulateInstruction::Context context; 1085 context.type = EmulateInstruction::eContextRelativeBranchImmediate; 1086 const uint64_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, 1087 LLDB_REGNUM_GENERIC_PC, 0, &success); 1088 if (!success) 1089 return false; 1090 1091 int64_t offset = llvm::SignExtend64<28>(Bits32(opcode, 25, 0) << 2); 1092 BranchType branch_type = Bit32(opcode, 31) ? BranchType_CALL : BranchType_JMP; 1093 addr_t target = pc + offset; 1094 context.SetImmediateSigned(offset); 1095 1096 switch (branch_type) { 1097 case BranchType_CALL: { 1098 addr_t x30 = pc + 4; 1099 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, arm64_dwarf::x30, 1100 x30)) 1101 return false; 1102 } break; 1103 case BranchType_JMP: 1104 break; 1105 default: 1106 return false; 1107 } 1108 1109 if (!BranchTo(context, 64, target)) 1110 return false; 1111 return true; 1112 } 1113 1114 bool EmulateInstructionARM64::EmulateBcond(const uint32_t opcode) { 1115 #if 0 1116 // ARM64 pseudo code... 1117 bits(64) offset = SignExtend(imm19:'00', 64); 1118 bits(4) condition = cond; 1119 if ConditionHolds(condition) then 1120 BranchTo(PC[] + offset, BranchType_JMP); 1121 #endif 1122 1123 if (ConditionHolds(Bits32(opcode, 3, 0))) { 1124 bool success = false; 1125 1126 const uint64_t pc = ReadRegisterUnsigned( 1127 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); 1128 if (!success) 1129 return false; 1130 1131 int64_t offset = llvm::SignExtend64<21>(Bits32(opcode, 23, 5) << 2); 1132 addr_t target = pc + offset; 1133 1134 EmulateInstruction::Context context; 1135 context.type = EmulateInstruction::eContextRelativeBranchImmediate; 1136 context.SetImmediateSigned(offset); 1137 if (!BranchTo(context, 64, target)) 1138 return false; 1139 } 1140 return true; 1141 } 1142 1143 bool EmulateInstructionARM64::EmulateCBZ(const uint32_t opcode) { 1144 #if 0 1145 integer t = UInt(Rt); 1146 integer datasize = if sf == '1' then 64 else 32; 1147 boolean iszero = (op == '0'); 1148 bits(64) offset = SignExtend(imm19:'00', 64); 1149 1150 bits(datasize) operand1 = X[t]; 1151 if IsZero(operand1) == iszero then 1152 BranchTo(PC[] + offset, BranchType_JMP); 1153 #endif 1154 1155 bool success = false; 1156 1157 uint32_t t = Bits32(opcode, 4, 0); 1158 bool is_zero = Bit32(opcode, 24) == 0; 1159 int32_t offset = llvm::SignExtend64<21>(Bits32(opcode, 23, 5) << 2); 1160 1161 const uint64_t operand = ReadRegisterUnsigned( 1162 eRegisterKindDWARF, arm64_dwarf::x0 + t, 0, &success); 1163 if (!success) 1164 return false; 1165 1166 if (m_ignore_conditions || ((operand == 0) == is_zero)) { 1167 const uint64_t pc = ReadRegisterUnsigned( 1168 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); 1169 if (!success) 1170 return false; 1171 1172 EmulateInstruction::Context context; 1173 context.type = EmulateInstruction::eContextRelativeBranchImmediate; 1174 context.SetImmediateSigned(offset); 1175 if (!BranchTo(context, 64, pc + offset)) 1176 return false; 1177 } 1178 return true; 1179 } 1180 1181 bool EmulateInstructionARM64::EmulateTBZ(const uint32_t opcode) { 1182 #if 0 1183 integer t = UInt(Rt); 1184 integer datasize = if b5 == '1' then 64 else 32; 1185 integer bit_pos = UInt(b5:b40); 1186 bit bit_val = op; 1187 bits(64) offset = SignExtend(imm14:'00', 64); 1188 #endif 1189 1190 bool success = false; 1191 1192 uint32_t t = Bits32(opcode, 4, 0); 1193 uint32_t bit_pos = (Bit32(opcode, 31) << 6) | (Bits32(opcode, 23, 19)); 1194 uint32_t bit_val = Bit32(opcode, 24); 1195 int64_t offset = llvm::SignExtend64<16>(Bits32(opcode, 18, 5) << 2); 1196 1197 const uint64_t operand = ReadRegisterUnsigned( 1198 eRegisterKindDWARF, arm64_dwarf::x0 + t, 0, &success); 1199 if (!success) 1200 return false; 1201 1202 if (m_ignore_conditions || Bit32(operand, bit_pos) == bit_val) { 1203 const uint64_t pc = ReadRegisterUnsigned( 1204 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); 1205 if (!success) 1206 return false; 1207 1208 EmulateInstruction::Context context; 1209 context.type = EmulateInstruction::eContextRelativeBranchImmediate; 1210 context.SetImmediateSigned(offset); 1211 if (!BranchTo(context, 64, pc + offset)) 1212 return false; 1213 } 1214 return true; 1215 } 1216