1 //===-- EmulateInstructionARM64.cpp ------------------------------*- C++-*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "EmulateInstructionARM64.h"
13 #include "lldb/Core/Address.h"
14 #include "lldb/Core/PluginManager.h"
15 #include "lldb/Symbol/UnwindPlan.h"
16 #include "lldb/Utility/ArchSpec.h"
17 #include "lldb/Utility/ConstString.h"
18 #include "lldb/Utility/RegisterValue.h"
19 #include "lldb/Utility/Stream.h"
21 #include "Plugins/Process/Utility/ARMDefines.h"
22 #include "Plugins/Process/Utility/ARMUtils.h"
23 #include "Plugins/Process/Utility/lldb-arm64-register-enums.h"
25 #define GPR_OFFSET(idx) ((idx)*8)
26 #define GPR_OFFSET_NAME(reg) 0
27 #define FPU_OFFSET(idx) ((idx)*16)
28 #define FPU_OFFSET_NAME(reg) 0
29 #define EXC_OFFSET_NAME(reg) 0
30 #define DBG_OFFSET_NAME(reg) 0
31 #define DBG_OFFSET_NAME(reg) 0
32 #define DEFINE_DBG(re, y) \
33 "na", nullptr, 8, 0, lldb::eEncodingUint, lldb::eFormatHex, \
34 {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, \
35 LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM}, \
36 nullptr, nullptr, nullptr, 0
38 #define DECLARE_REGISTER_INFOS_ARM64_STRUCT
40 #include "Plugins/Process/Utility/RegisterInfos_arm64.h"
42 #include "llvm/ADT/STLExtras.h"
43 #include "llvm/Support/MathExtras.h"
45 #include "Plugins/Process/Utility/InstructionUtils.h"
48 using namespace lldb_private;
50 static bool LLDBTableGetRegisterInfo(uint32_t reg_num, RegisterInfo ®_info) {
51 if (reg_num >= llvm::array_lengthof(g_register_infos_arm64_le))
53 reg_info = g_register_infos_arm64_le[reg_num];
58 #define VFPv1 (1u << 1)
59 #define VFPv2 (1u << 2)
60 #define VFPv3 (1u << 3)
61 #define AdvancedSIMD (1u << 4)
63 #define VFPv1_ABOVE (VFPv1 | VFPv2 | VFPv3 | AdvancedSIMD)
64 #define VFPv2_ABOVE (VFPv2 | VFPv3 | AdvancedSIMD)
65 #define VFPv2v3 (VFPv2 | VFPv3)
67 #define UInt(x) ((uint64_t)x)
68 #define SInt(x) ((int64_t)x)
71 #define integer int64_t
73 static inline bool IsZero(uint64_t x) { return x == 0; }
75 static inline uint64_t NOT(uint64_t x) { return ~x; }
80 static inline uint64_t LSL(uint64_t x, integer shift) {
88 static inline uint64_t
89 AddWithCarry(uint32_t N, uint64_t x, uint64_t y, bit carry_in,
90 EmulateInstructionARM64::ProcState &proc_state) {
91 uint64_t unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
92 int64_t signed_sum = SInt(x) + SInt(y) + UInt(carry_in);
93 uint64_t result = unsigned_sum;
95 result = Bits64(result, N - 1, 0);
96 proc_state.N = Bit64(result, N - 1);
97 proc_state.Z = IsZero(result);
98 proc_state.C = UInt(result) == unsigned_sum;
99 proc_state.V = SInt(result) == signed_sum;
103 // ConstrainUnpredictable()
104 // ========================
106 EmulateInstructionARM64::ConstraintType
107 ConstrainUnpredictable(EmulateInstructionARM64::Unpredictable which) {
108 EmulateInstructionARM64::ConstraintType result =
109 EmulateInstructionARM64::Constraint_UNKNOWN;
111 case EmulateInstructionARM64::Unpredictable_WBOVERLAP:
112 case EmulateInstructionARM64::Unpredictable_LDPOVERLAP:
113 // TODO: don't know what to really do here? Pseudo code says:
114 // set result to one of above Constraint behaviours or UNDEFINED
121 // EmulateInstructionARM implementation
124 void EmulateInstructionARM64::Initialize() {
125 PluginManager::RegisterPlugin(GetPluginNameStatic(),
126 GetPluginDescriptionStatic(), CreateInstance);
129 void EmulateInstructionARM64::Terminate() {
130 PluginManager::UnregisterPlugin(CreateInstance);
133 ConstString EmulateInstructionARM64::GetPluginNameStatic() {
134 ConstString g_plugin_name("lldb.emulate-instruction.arm64");
135 return g_plugin_name;
138 lldb_private::ConstString EmulateInstructionARM64::GetPluginName() {
139 static ConstString g_plugin_name("EmulateInstructionARM64");
140 return g_plugin_name;
143 const char *EmulateInstructionARM64::GetPluginDescriptionStatic() {
144 return "Emulate instructions for the ARM64 architecture.";
148 EmulateInstructionARM64::CreateInstance(const ArchSpec &arch,
149 InstructionType inst_type) {
150 if (EmulateInstructionARM64::SupportsEmulatingInstructionsOfTypeStatic(
152 if (arch.GetTriple().getArch() == llvm::Triple::aarch64) {
153 return new EmulateInstructionARM64(arch);
160 bool EmulateInstructionARM64::SetTargetTriple(const ArchSpec &arch) {
161 if (arch.GetTriple().getArch() == llvm::Triple::arm)
163 else if (arch.GetTriple().getArch() == llvm::Triple::thumb)
169 bool EmulateInstructionARM64::GetRegisterInfo(RegisterKind reg_kind,
171 RegisterInfo ®_info) {
172 if (reg_kind == eRegisterKindGeneric) {
174 case LLDB_REGNUM_GENERIC_PC:
175 reg_kind = eRegisterKindLLDB;
176 reg_num = gpr_pc_arm64;
178 case LLDB_REGNUM_GENERIC_SP:
179 reg_kind = eRegisterKindLLDB;
180 reg_num = gpr_sp_arm64;
182 case LLDB_REGNUM_GENERIC_FP:
183 reg_kind = eRegisterKindLLDB;
184 reg_num = gpr_fp_arm64;
186 case LLDB_REGNUM_GENERIC_RA:
187 reg_kind = eRegisterKindLLDB;
188 reg_num = gpr_lr_arm64;
190 case LLDB_REGNUM_GENERIC_FLAGS:
191 reg_kind = eRegisterKindLLDB;
192 reg_num = gpr_cpsr_arm64;
200 if (reg_kind == eRegisterKindLLDB)
201 return LLDBTableGetRegisterInfo(reg_num, reg_info);
205 EmulateInstructionARM64::Opcode *
206 EmulateInstructionARM64::GetOpcodeForInstruction(const uint32_t opcode) {
207 static EmulateInstructionARM64::Opcode g_opcodes[] = {
208 // Prologue instructions
211 {0xff000000, 0xd1000000, No_VFP,
212 &EmulateInstructionARM64::EmulateADDSUBImm,
213 "SUB <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}"},
214 {0xff000000, 0xf1000000, No_VFP,
215 &EmulateInstructionARM64::EmulateADDSUBImm,
216 "SUBS <Xd>, <Xn|SP>, #<imm> {, <shift>}"},
217 {0xff000000, 0x91000000, No_VFP,
218 &EmulateInstructionARM64::EmulateADDSUBImm,
219 "ADD <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}"},
220 {0xff000000, 0xb1000000, No_VFP,
221 &EmulateInstructionARM64::EmulateADDSUBImm,
222 "ADDS <Xd>, <Xn|SP>, #<imm> {, <shift>}"},
224 {0xff000000, 0x51000000, No_VFP,
225 &EmulateInstructionARM64::EmulateADDSUBImm,
226 "SUB <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}"},
227 {0xff000000, 0x71000000, No_VFP,
228 &EmulateInstructionARM64::EmulateADDSUBImm,
229 "SUBS <Wd>, <Wn|WSP>, #<imm> {, <shift>}"},
230 {0xff000000, 0x11000000, No_VFP,
231 &EmulateInstructionARM64::EmulateADDSUBImm,
232 "ADD <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}"},
233 {0xff000000, 0x31000000, No_VFP,
234 &EmulateInstructionARM64::EmulateADDSUBImm,
235 "ADDS <Wd>, <Wn|WSP>, #<imm> {, <shift>}"},
237 {0xffc00000, 0x29000000, No_VFP,
238 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
239 "STP <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]"},
240 {0xffc00000, 0xa9000000, No_VFP,
241 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
242 "STP <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]"},
243 {0xffc00000, 0x2d000000, No_VFP,
244 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
245 "STP <St>, <St2>, [<Xn|SP>{, #<imm>}]"},
246 {0xffc00000, 0x6d000000, No_VFP,
247 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
248 "STP <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]"},
249 {0xffc00000, 0xad000000, No_VFP,
250 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
251 "STP <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]"},
253 {0xffc00000, 0x29800000, No_VFP,
254 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
255 "STP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
256 {0xffc00000, 0xa9800000, No_VFP,
257 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
258 "STP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
259 {0xffc00000, 0x2d800000, No_VFP,
260 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
261 "STP <St>, <St2>, [<Xn|SP>, #<imm>]!"},
262 {0xffc00000, 0x6d800000, No_VFP,
263 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
264 "STP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
265 {0xffc00000, 0xad800000, No_VFP,
266 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
267 "STP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
269 {0xffc00000, 0x28800000, No_VFP,
270 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
271 "STP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
272 {0xffc00000, 0xa8800000, No_VFP,
273 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
274 "STP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
275 {0xffc00000, 0x2c800000, No_VFP,
276 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
277 "STP <St>, <St2>, [<Xn|SP>, #<imm>]!"},
278 {0xffc00000, 0x6c800000, No_VFP,
279 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
280 "STP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
281 {0xffc00000, 0xac800000, No_VFP,
282 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
283 "STP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
285 {0xffc00000, 0x29400000, No_VFP,
286 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
287 "LDP <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]"},
288 {0xffc00000, 0xa9400000, No_VFP,
289 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
290 "LDP <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]"},
291 {0xffc00000, 0x2d400000, No_VFP,
292 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
293 "LDP <St>, <St2>, [<Xn|SP>{, #<imm>}]"},
294 {0xffc00000, 0x6d400000, No_VFP,
295 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
296 "LDP <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]"},
297 {0xffc00000, 0xad400000, No_VFP,
298 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
299 "LDP <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]"},
301 {0xffc00000, 0x29c00000, No_VFP,
302 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
303 "LDP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
304 {0xffc00000, 0xa9c00000, No_VFP,
305 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
306 "LDP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
307 {0xffc00000, 0x2dc00000, No_VFP,
308 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
309 "LDP <St>, <St2>, [<Xn|SP>, #<imm>]!"},
310 {0xffc00000, 0x6dc00000, No_VFP,
311 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
312 "LDP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
313 {0xffc00000, 0xadc00000, No_VFP,
314 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
315 "LDP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
317 {0xffc00000, 0x28c00000, No_VFP,
318 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
319 "LDP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
320 {0xffc00000, 0xa8c00000, No_VFP,
321 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
322 "LDP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
323 {0xffc00000, 0x2cc00000, No_VFP,
324 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
325 "LDP <St>, <St2>, [<Xn|SP>, #<imm>]!"},
326 {0xffc00000, 0x6cc00000, No_VFP,
327 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
328 "LDP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
329 {0xffc00000, 0xacc00000, No_VFP,
330 &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
331 "LDP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
333 {0xffe00c00, 0xb8000400, No_VFP,
334 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>,
335 "STR <Wt>, [<Xn|SP>], #<simm>"},
336 {0xffe00c00, 0xf8000400, No_VFP,
337 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>,
338 "STR <Xt>, [<Xn|SP>], #<simm>"},
339 {0xffe00c00, 0xb8000c00, No_VFP,
340 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>,
341 "STR <Wt>, [<Xn|SP>, #<simm>]!"},
342 {0xffe00c00, 0xf8000c00, No_VFP,
343 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>,
344 "STR <Xt>, [<Xn|SP>, #<simm>]!"},
345 {0xffc00000, 0xb9000000, No_VFP,
346 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>,
347 "STR <Wt>, [<Xn|SP>{, #<pimm>}]"},
348 {0xffc00000, 0xf9000000, No_VFP,
349 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>,
350 "STR <Xt>, [<Xn|SP>{, #<pimm>}]"},
352 {0xffe00c00, 0xb8400400, No_VFP,
353 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>,
354 "LDR <Wt>, [<Xn|SP>], #<simm>"},
355 {0xffe00c00, 0xf8400400, No_VFP,
356 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>,
357 "LDR <Xt>, [<Xn|SP>], #<simm>"},
358 {0xffe00c00, 0xb8400c00, No_VFP,
359 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>,
360 "LDR <Wt>, [<Xn|SP>, #<simm>]!"},
361 {0xffe00c00, 0xf8400c00, No_VFP,
362 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>,
363 "LDR <Xt>, [<Xn|SP>, #<simm>]!"},
364 {0xffc00000, 0xb9400000, No_VFP,
365 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>,
366 "LDR <Wt>, [<Xn|SP>{, #<pimm>}]"},
367 {0xffc00000, 0xf9400000, No_VFP,
368 &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>,
369 "LDR <Xt>, [<Xn|SP>{, #<pimm>}]"},
371 {0xfc000000, 0x14000000, No_VFP, &EmulateInstructionARM64::EmulateB,
373 {0xff000010, 0x54000000, No_VFP, &EmulateInstructionARM64::EmulateBcond,
375 {0x7f000000, 0x34000000, No_VFP, &EmulateInstructionARM64::EmulateCBZ,
376 "CBZ <Wt>, <label>"},
377 {0x7f000000, 0x35000000, No_VFP, &EmulateInstructionARM64::EmulateCBZ,
378 "CBNZ <Wt>, <label>"},
379 {0x7f000000, 0x36000000, No_VFP, &EmulateInstructionARM64::EmulateTBZ,
380 "TBZ <R><t>, #<imm>, <label>"},
381 {0x7f000000, 0x37000000, No_VFP, &EmulateInstructionARM64::EmulateTBZ,
382 "TBNZ <R><t>, #<imm>, <label>"},
385 static const size_t k_num_arm_opcodes = llvm::array_lengthof(g_opcodes);
387 for (size_t i = 0; i < k_num_arm_opcodes; ++i) {
388 if ((g_opcodes[i].mask & opcode) == g_opcodes[i].value)
389 return &g_opcodes[i];
394 bool EmulateInstructionARM64::ReadInstruction() {
395 bool success = false;
396 m_addr = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC,
397 LLDB_INVALID_ADDRESS, &success);
399 Context read_inst_context;
400 read_inst_context.type = eContextReadOpcode;
401 read_inst_context.SetNoArgs();
402 m_opcode.SetOpcode32(
403 ReadMemoryUnsigned(read_inst_context, m_addr, 4, 0, &success),
407 m_addr = LLDB_INVALID_ADDRESS;
411 bool EmulateInstructionARM64::EvaluateInstruction(uint32_t evaluate_options) {
412 const uint32_t opcode = m_opcode.GetOpcode32();
413 Opcode *opcode_data = GetOpcodeForInstruction(opcode);
414 if (opcode_data == nullptr)
417 // printf ("opcode template for 0x%8.8x: %s\n", opcode, opcode_data->name);
418 const bool auto_advance_pc =
419 evaluate_options & eEmulateInstructionOptionAutoAdvancePC;
420 m_ignore_conditions =
421 evaluate_options & eEmulateInstructionOptionIgnoreConditions;
423 bool success = false;
424 // if (m_opcode_cpsr == 0 || m_ignore_conditions == false)
426 // m_opcode_cpsr = ReadRegisterUnsigned (eRegisterKindLLDB,
432 // Only return false if we are unable to read the CPSR if we care about
434 if (!success && !m_ignore_conditions)
437 uint32_t orig_pc_value = 0;
438 if (auto_advance_pc) {
440 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_pc_arm64, 0, &success);
445 // Call the Emulate... function.
446 success = (this->*opcode_data->callback)(opcode);
450 if (auto_advance_pc) {
451 uint32_t new_pc_value =
452 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_pc_arm64, 0, &success);
456 if (auto_advance_pc && (new_pc_value == orig_pc_value)) {
457 EmulateInstruction::Context context;
458 context.type = eContextAdvancePC;
460 if (!WriteRegisterUnsigned(context, eRegisterKindLLDB, gpr_pc_arm64,
468 bool EmulateInstructionARM64::CreateFunctionEntryUnwind(
469 UnwindPlan &unwind_plan) {
471 unwind_plan.SetRegisterKind(eRegisterKindLLDB);
473 UnwindPlan::RowSP row(new UnwindPlan::Row);
475 // Our previous Call Frame Address is the stack pointer
476 row->GetCFAValue().SetIsRegisterPlusOffset(gpr_sp_arm64, 0);
478 unwind_plan.AppendRow(row);
479 unwind_plan.SetSourceName("EmulateInstructionARM64");
480 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
481 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
482 unwind_plan.SetReturnAddressRegister(gpr_lr_arm64);
486 uint32_t EmulateInstructionARM64::GetFramePointerRegisterNumber() const {
487 if (m_arch.GetTriple().isAndroid())
488 return LLDB_INVALID_REGNUM; // Don't use frame pointer on android
493 bool EmulateInstructionARM64::UsingAArch32() {
494 bool aarch32 = m_opcode_pstate.RW == 1;
495 // if !HaveAnyAArch32() then assert !aarch32;
496 // if HighestELUsingAArch32() then assert aarch32;
500 bool EmulateInstructionARM64::BranchTo(const Context &context, uint32_t N,
503 // Set program counter to a new address, with a branch reason hint for
504 // possible use by hardware fetching the next instruction.
505 BranchTo(bits(N) target, BranchType branch_type)
506 Hint_Branch(branch_type);
508 assert UsingAArch32();
509 _PC = ZeroExtend(target);
511 assert N == 64 && !UsingAArch32();
512 // Remove the tag bits from a tagged target
515 if target<55> == '1' && TCR_EL1.TBI1 == '1' then
516 target<63:56> = '11111111';
517 if target<55> == '0' && TCR_EL1.TBI0 == '1' then
518 target<63:56> = '00000000';
520 if TCR_EL2.TBI == '1' then
521 target<63:56> = '00000000';
523 if TCR_EL3.TBI == '1' then
524 target<63:56> = '00000000';
531 // Hint_Branch(branch_type);
536 } else if (N == 64) {
539 // TODO: Remove the tag bits from a tagged target
544 return WriteRegisterUnsigned(context, eRegisterKindGeneric,
545 LLDB_REGNUM_GENERIC_PC, addr);
548 bool EmulateInstructionARM64::ConditionHolds(const uint32_t cond) {
549 // If we are ignoring conditions, then always return true. this allows us to
550 // iterate over disassembly code and still emulate an instruction even if we
551 // don't have all the right bits set in the CPSR register...
552 if (m_ignore_conditions)
556 switch (UnsignedBits(cond, 3, 1)) {
558 result = (m_opcode_pstate.Z == 1);
561 result = (m_opcode_pstate.C == 1);
564 result = (m_opcode_pstate.N == 1);
567 result = (m_opcode_pstate.V == 1);
570 result = (m_opcode_pstate.C == 1 && m_opcode_pstate.Z == 0);
573 result = (m_opcode_pstate.N == m_opcode_pstate.V);
576 result = (m_opcode_pstate.N == m_opcode_pstate.V && m_opcode_pstate.Z == 0);
579 // Always execute (cond == 0b1110, or the special 0b1111 which gives
580 // opcodes different meanings, but always means execution happens.
589 bool EmulateInstructionARM64::EmulateADDSUBImm(const uint32_t opcode) {
590 // integer d = UInt(Rd);
591 // integer n = UInt(Rn);
592 // integer datasize = if sf == 1 then 64 else 32;
593 // boolean sub_op = (op == 1);
594 // boolean setflags = (S == 1);
595 // bits(datasize) imm;
598 // when '00' imm = ZeroExtend(imm12, datasize);
599 // when '01' imm = ZeroExtend(imm12 : Zeros(12), datasize);
600 // when '1x' UNDEFINED;
603 // bits(datasize) result;
604 // bits(datasize) operand1 = if n == 31 then SP[] else X[n];
605 // bits(datasize) operand2 = imm;
610 // operand2 = NOT(operand2);
615 // (result, nzcv) = AddWithCarry(operand1, operand2, carry_in);
618 // PSTATE.NZCV = nzcv;
620 // if d == 31 && !setflags then
625 const uint32_t sf = Bit32(opcode, 31);
626 const uint32_t op = Bit32(opcode, 30);
627 const uint32_t S = Bit32(opcode, 29);
628 const uint32_t shift = Bits32(opcode, 23, 22);
629 const uint32_t imm12 = Bits32(opcode, 21, 10);
630 const uint32_t Rn = Bits32(opcode, 9, 5);
631 const uint32_t Rd = Bits32(opcode, 4, 0);
633 bool success = false;
635 const uint32_t d = UInt(Rd);
636 const uint32_t n = UInt(Rn);
637 const uint32_t datasize = (sf == 1) ? 64 : 32;
638 boolean sub_op = op == 1;
639 boolean setflags = S == 1;
650 return false; // UNDEFINED;
654 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + n, 0, &success);
655 uint64_t operand2 = imm;
659 operand2 = NOT(operand2);
661 imm = -imm; // For the Register plug offset context below
666 ProcState proc_state;
668 result = AddWithCarry(datasize, operand1, operand2, carry_in, proc_state);
671 m_emulated_pstate.N = proc_state.N;
672 m_emulated_pstate.Z = proc_state.Z;
673 m_emulated_pstate.C = proc_state.C;
674 m_emulated_pstate.V = proc_state.V;
678 RegisterInfo reg_info_Rn;
679 if (GetRegisterInfo(eRegisterKindLLDB, n, reg_info_Rn))
680 context.SetRegisterPlusOffset(reg_info_Rn, imm);
682 if (n == GetFramePointerRegisterNumber() && d == gpr_sp_arm64 && !setflags) {
683 // 'mov sp, fp' - common epilogue instruction, CFA is now in terms of the
684 // stack pointer, instead of frame pointer.
685 context.type = EmulateInstruction::eContextRestoreStackPointer;
686 } else if ((n == gpr_sp_arm64 || n == GetFramePointerRegisterNumber()) &&
687 d == gpr_sp_arm64 && !setflags) {
688 context.type = EmulateInstruction::eContextAdjustStackPointer;
689 } else if (d == GetFramePointerRegisterNumber() && n == gpr_sp_arm64 &&
691 context.type = EmulateInstruction::eContextSetFramePointer;
693 context.type = EmulateInstruction::eContextImmediate;
696 // If setflags && d == gpr_sp_arm64 then d = WZR/XZR. See CMN, CMP
697 if (!setflags || d != gpr_sp_arm64)
698 WriteRegisterUnsigned(context, eRegisterKindLLDB, gpr_x0_arm64 + d, result);
703 template <EmulateInstructionARM64::AddrMode a_mode>
704 bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode) {
705 uint32_t opc = Bits32(opcode, 31, 30);
706 uint32_t V = Bit32(opcode, 26);
707 uint32_t L = Bit32(opcode, 22);
708 uint32_t imm7 = Bits32(opcode, 21, 15);
709 uint32_t Rt2 = Bits32(opcode, 14, 10);
710 uint32_t Rn = Bits32(opcode, 9, 5);
711 uint32_t Rt = Bits32(opcode, 4, 0);
713 integer n = UInt(Rn);
714 integer t = UInt(Rt);
715 integer t2 = UInt(Rt2);
718 MemOp memop = L == 1 ? MemOp_LOAD : MemOp_STORE;
719 boolean vector = (V == 1);
720 // AccType acctype = AccType_NORMAL;
721 boolean is_signed = false;
722 boolean wback = a_mode != AddrMode_OFF;
723 boolean wb_unknown = false;
724 boolean rt_unknown = false;
729 return false; // UNDEFINED
732 scale = 2 + UInt(opc);
734 scale = (opc & 2) ? 3 : 2;
735 is_signed = (opc & 1) != 0;
736 if (is_signed && memop == MemOp_STORE)
737 return false; // UNDEFINED
740 if (!vector && wback && ((t == n) || (t2 == n))) {
741 switch (ConstrainUnpredictable(Unpredictable_WBOVERLAP)) {
742 case Constraint_UNKNOWN:
743 wb_unknown = true; // writeback is UNKNOWN
746 case Constraint_SUPPRESSWB:
747 wback = false; // writeback is suppressed
751 memop = MemOp_NOP; // do nothing
755 case Constraint_NONE:
760 if (memop == MemOp_LOAD && t == t2) {
761 switch (ConstrainUnpredictable(Unpredictable_LDPOVERLAP)) {
762 case Constraint_UNKNOWN:
763 rt_unknown = true; // result is UNKNOWN
767 memop = MemOp_NOP; // do nothing
776 idx = LSL(llvm::SignExtend64<7>(imm7), scale);
777 size = (integer)1 << scale;
778 uint64_t datasize = size * 8;
782 RegisterValue data_Rt;
783 RegisterValue data_Rt2;
786 // CheckFPEnabled(false);
788 RegisterInfo reg_info_base;
789 RegisterInfo reg_info_Rt;
790 RegisterInfo reg_info_Rt2;
791 if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + n, reg_info_base))
795 if (!GetRegisterInfo(eRegisterKindLLDB, fpu_d0_arm64 + t, reg_info_Rt))
797 if (!GetRegisterInfo(eRegisterKindLLDB, fpu_d0_arm64 + t2, reg_info_Rt2))
800 if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + t, reg_info_Rt))
802 if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + t2, reg_info_Rt2))
806 bool success = false;
808 // CheckSPAlignment();
810 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_sp_arm64, 0, &success);
813 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + n, 0, &success);
815 wb_address = address + idx;
816 if (a_mode != AddrMode_POST)
817 address = wb_address;
822 uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
827 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
828 // based off of the sp
831 context_t.type = eContextPushRegisterOnStack;
832 context_t2.type = eContextPushRegisterOnStack;
834 context_t.type = eContextRegisterStore;
835 context_t2.type = eContextRegisterStore;
837 context_t.SetRegisterToRegisterPlusOffset(reg_info_Rt, reg_info_base, 0);
838 context_t2.SetRegisterToRegisterPlusOffset(reg_info_Rt2, reg_info_base,
841 if (!ReadRegister(®_info_Rt, data_Rt))
844 if (data_Rt.GetAsMemoryData(®_info_Rt, buffer, reg_info_Rt.byte_size,
845 eByteOrderLittle, error) == 0)
848 if (!WriteMemory(context_t, address + 0, buffer, reg_info_Rt.byte_size))
851 if (!ReadRegister(®_info_Rt2, data_Rt2))
854 if (data_Rt2.GetAsMemoryData(®_info_Rt2, buffer, reg_info_Rt2.byte_size,
855 eByteOrderLittle, error) == 0)
858 if (!WriteMemory(context_t2, address + size, buffer,
859 reg_info_Rt2.byte_size))
864 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this load is
865 // based off of the sp
868 context_t.type = eContextPopRegisterOffStack;
869 context_t2.type = eContextPopRegisterOffStack;
871 context_t.type = eContextRegisterLoad;
872 context_t2.type = eContextRegisterLoad;
874 context_t.SetAddress(address);
875 context_t2.SetAddress(address + size);
878 memset(buffer, 'U', reg_info_Rt.byte_size);
880 if (!ReadMemory(context_t, address, buffer, reg_info_Rt.byte_size))
884 if (data_Rt.SetFromMemoryData(®_info_Rt, buffer, reg_info_Rt.byte_size,
885 eByteOrderLittle, error) == 0)
888 if (!vector && is_signed && !data_Rt.SignExtend(datasize))
891 if (!WriteRegister(context_t, ®_info_Rt, data_Rt))
895 if (!ReadMemory(context_t2, address + size, buffer,
896 reg_info_Rt2.byte_size))
900 if (data_Rt2.SetFromMemoryData(®_info_Rt2, buffer,
901 reg_info_Rt2.byte_size, eByteOrderLittle,
905 if (!vector && is_signed && !data_Rt2.SignExtend(datasize))
908 if (!WriteRegister(context_t2, ®_info_Rt2, data_Rt2))
918 wb_address = LLDB_INVALID_ADDRESS;
920 context.SetImmediateSigned(idx);
922 context.type = eContextAdjustStackPointer;
924 context.type = eContextAdjustBaseRegister;
925 WriteRegisterUnsigned(context, ®_info_base, wb_address);
930 template <EmulateInstructionARM64::AddrMode a_mode>
931 bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode) {
932 uint32_t size = Bits32(opcode, 31, 30);
933 uint32_t opc = Bits32(opcode, 23, 22);
934 uint32_t n = Bits32(opcode, 9, 5);
935 uint32_t t = Bits32(opcode, 4, 0);
945 offset = llvm::SignExtend64<9>(Bits32(opcode, 20, 12));
950 offset = llvm::SignExtend64<9>(Bits32(opcode, 20, 12));
955 offset = LSL(Bits32(opcode, 21, 10), size);
961 if (Bit32(opc, 1) == 0) {
962 memop = Bit32(opc, 0) == 1 ? MemOp_LOAD : MemOp_STORE;
965 if (size == 2 && Bit32(opc, 0) == 1)
970 bool success = false;
972 uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
973 RegisterValue data_Rt;
977 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_sp_arm64, 0, &success);
980 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + n, 0, &success);
988 RegisterInfo reg_info_base;
989 if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + n, reg_info_base))
992 RegisterInfo reg_info_Rt;
993 if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + t, reg_info_Rt))
999 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
1000 // based off of the sp
1002 context.type = eContextPushRegisterOnStack;
1004 context.type = eContextRegisterStore;
1005 context.SetRegisterToRegisterPlusOffset(reg_info_Rt, reg_info_base,
1006 postindex ? 0 : offset);
1008 if (!ReadRegister(®_info_Rt, data_Rt))
1011 if (data_Rt.GetAsMemoryData(®_info_Rt, buffer, reg_info_Rt.byte_size,
1012 eByteOrderLittle, error) == 0)
1015 if (!WriteMemory(context, address, buffer, reg_info_Rt.byte_size))
1020 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
1021 // based off of the sp
1023 context.type = eContextPopRegisterOffStack;
1025 context.type = eContextRegisterLoad;
1026 context.SetAddress(address);
1028 if (!ReadMemory(context, address, buffer, reg_info_Rt.byte_size))
1031 if (data_Rt.SetFromMemoryData(®_info_Rt, buffer, reg_info_Rt.byte_size,
1032 eByteOrderLittle, error) == 0)
1035 if (!WriteRegister(context, ®_info_Rt, data_Rt))
1047 context.type = eContextAdjustStackPointer;
1049 context.type = eContextAdjustBaseRegister;
1050 context.SetImmediateSigned(offset);
1052 if (!WriteRegisterUnsigned(context, ®_info_base, address))
1058 bool EmulateInstructionARM64::EmulateB(const uint32_t opcode) {
1060 // ARM64 pseudo code...
1061 if branch_type == BranchType_CALL then X[30] = PC[] + 4;
1062 BranchTo(PC[] + offset, branch_type);
1065 bool success = false;
1067 EmulateInstruction::Context context;
1068 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
1069 const uint64_t pc = ReadRegisterUnsigned(eRegisterKindGeneric,
1070 LLDB_REGNUM_GENERIC_PC, 0, &success);
1074 int64_t offset = llvm::SignExtend64<28>(Bits32(opcode, 25, 0) << 2);
1075 BranchType branch_type = Bit32(opcode, 31) ? BranchType_CALL : BranchType_JMP;
1076 addr_t target = pc + offset;
1077 context.SetImmediateSigned(offset);
1079 switch (branch_type) {
1080 case BranchType_CALL: {
1081 addr_t x30 = pc + 4;
1082 if (!WriteRegisterUnsigned(context, eRegisterKindLLDB, gpr_lr_arm64, x30))
1085 case BranchType_JMP:
1091 return BranchTo(context, 64, target);
1094 bool EmulateInstructionARM64::EmulateBcond(const uint32_t opcode) {
1096 // ARM64 pseudo code...
1097 bits(64) offset = SignExtend(imm19:'00', 64);
1098 bits(4) condition = cond;
1099 if ConditionHolds(condition) then
1100 BranchTo(PC[] + offset, BranchType_JMP);
1103 if (ConditionHolds(Bits32(opcode, 3, 0))) {
1104 bool success = false;
1106 const uint64_t pc = ReadRegisterUnsigned(
1107 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1111 int64_t offset = llvm::SignExtend64<21>(Bits32(opcode, 23, 5) << 2);
1112 addr_t target = pc + offset;
1114 EmulateInstruction::Context context;
1115 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
1116 context.SetImmediateSigned(offset);
1117 if (!BranchTo(context, 64, target))
1123 bool EmulateInstructionARM64::EmulateCBZ(const uint32_t opcode) {
1125 integer t = UInt(Rt);
1126 integer datasize = if sf == '1' then 64 else 32;
1127 boolean iszero = (op == '0');
1128 bits(64) offset = SignExtend(imm19:'00', 64);
1130 bits(datasize) operand1 = X[t];
1131 if IsZero(operand1) == iszero then
1132 BranchTo(PC[] + offset, BranchType_JMP);
1135 bool success = false;
1137 uint32_t t = Bits32(opcode, 4, 0);
1138 bool is_zero = Bit32(opcode, 24) == 0;
1139 int32_t offset = llvm::SignExtend64<21>(Bits32(opcode, 23, 5) << 2);
1141 const uint64_t operand =
1142 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + t, 0, &success);
1146 if (m_ignore_conditions || ((operand == 0) == is_zero)) {
1147 const uint64_t pc = ReadRegisterUnsigned(
1148 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1152 EmulateInstruction::Context context;
1153 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
1154 context.SetImmediateSigned(offset);
1155 if (!BranchTo(context, 64, pc + offset))
1161 bool EmulateInstructionARM64::EmulateTBZ(const uint32_t opcode) {
1163 integer t = UInt(Rt);
1164 integer datasize = if b5 == '1' then 64 else 32;
1165 integer bit_pos = UInt(b5:b40);
1167 bits(64) offset = SignExtend(imm14:'00', 64);
1170 bool success = false;
1172 uint32_t t = Bits32(opcode, 4, 0);
1173 uint32_t bit_pos = (Bit32(opcode, 31) << 6) | (Bits32(opcode, 23, 19));
1174 uint32_t bit_val = Bit32(opcode, 24);
1175 int64_t offset = llvm::SignExtend64<16>(Bits32(opcode, 18, 5) << 2);
1177 const uint64_t operand =
1178 ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + t, 0, &success);
1182 if (m_ignore_conditions || Bit32(operand, bit_pos) == bit_val) {
1183 const uint64_t pc = ReadRegisterUnsigned(
1184 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1188 EmulateInstruction::Context context;
1189 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
1190 context.SetImmediateSigned(offset);
1191 if (!BranchTo(context, 64, pc + offset))