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