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::EmulateB,
360 "B <label>"},
361 {0xff000010, 0x54000000, No_VFP, &EmulateInstructionARM64::EmulateBcond,
362 "B.<cond> <label>"},
363 {0x7f000000, 0x34000000, No_VFP, &EmulateInstructionARM64::EmulateCBZ,
364 "CBZ <Wt>, <label>"},
365 {0x7f000000, 0x35000000, No_VFP, &EmulateInstructionARM64::EmulateCBZ,
366 "CBNZ <Wt>, <label>"},
367 {0x7f000000, 0x36000000, No_VFP, &EmulateInstructionARM64::EmulateTBZ,
368 "TBZ <R><t>, #<imm>, <label>"},
369 {0x7f000000, 0x37000000, No_VFP, &EmulateInstructionARM64::EmulateTBZ,
370 "TBNZ <R><t>, #<imm>, <label>"},
371
372 };
373 static const size_t k_num_arm_opcodes = std::size(g_opcodes);
374
375 for (size_t i = 0; i < k_num_arm_opcodes; ++i) {
376 if ((g_opcodes[i].mask & opcode) == g_opcodes[i].value)
377 return &g_opcodes[i];
378 }
379 return nullptr;
380}
381
383 bool success = false;
385 LLDB_INVALID_ADDRESS, &success);
386 if (success) {
387 Context read_inst_context;
388 read_inst_context.type = eContextReadOpcode;
389 read_inst_context.SetNoArgs();
390 m_opcode.SetOpcode32(
391 ReadMemoryUnsigned(read_inst_context, m_addr, 4, 0, &success),
392 GetByteOrder());
393 }
394 if (!success)
396 return success;
397}
398
399bool EmulateInstructionARM64::EvaluateInstruction(uint32_t evaluate_options) {
400 const uint32_t opcode = m_opcode.GetOpcode32();
401 Opcode *opcode_data = GetOpcodeForInstruction(opcode);
402 if (opcode_data == nullptr)
403 return false;
404
405 const bool auto_advance_pc =
406 evaluate_options & eEmulateInstructionOptionAutoAdvancePC;
408 evaluate_options & eEmulateInstructionOptionIgnoreConditions;
409
410 bool success = false;
411
412 // Only return false if we are unable to read the CPSR if we care about
413 // conditions
414 if (!success && !m_ignore_conditions)
415 return false;
416
417 uint64_t orig_pc_value = 0;
418 if (auto_advance_pc) {
419 orig_pc_value =
421 if (!success)
422 return false;
423 }
424
425 // Call the Emulate... function.
426 success = (this->*opcode_data->callback)(opcode);
427 if (!success)
428 return false;
429
430 if (auto_advance_pc) {
431 uint64_t new_pc_value =
433 if (!success)
434 return false;
435
436 if (new_pc_value == orig_pc_value) {
438 context.type = eContextAdvancePC;
439 context.SetNoArgs();
441 orig_pc_value + 4))
442 return false;
443 }
444 }
445 return true;
446}
447
449 UnwindPlan &unwind_plan) {
450 unwind_plan.Clear();
452
453 UnwindPlan::Row row;
454
455 // Our previous Call Frame Address is the stack pointer
457 row.SetRegisterLocationToSame(gpr_lr_arm64, /*must_replace=*/false);
458 row.SetRegisterLocationToSame(gpr_fp_arm64, /*must_replace=*/false);
459
460 unwind_plan.AppendRow(std::move(row));
461 unwind_plan.SetSourceName("EmulateInstructionARM64");
466 return true;
467}
468
470 if (m_arch.GetTriple().isAndroid())
471 return LLDB_INVALID_REGNUM; // Don't use frame pointer on android
472
473 return gpr_fp_arm64;
474}
475
477 bool aarch32 = m_opcode_pstate.RW == 1;
478 // if !HaveAnyAArch32() then assert !aarch32;
479 // if HighestELUsingAArch32() then assert aarch32;
480 return aarch32;
481}
482
483bool EmulateInstructionARM64::BranchTo(const Context &context, uint32_t N,
484 addr_t target) {
485#if 0
486 // Set program counter to a new address, with a branch reason hint for
487 // possible use by hardware fetching the next instruction.
488 BranchTo(bits(N) target, BranchType branch_type)
489 Hint_Branch(branch_type);
490 if N == 32 then
491 assert UsingAArch32();
492 _PC = ZeroExtend(target);
493 else
494 assert N == 64 && !UsingAArch32();
495 // Remove the tag bits from a tagged target
496 case PSTATE.EL of
497 when EL0, EL1
498 if target<55> == '1' && TCR_EL1.TBI1 == '1' then
499 target<63:56> = '11111111';
500 if target<55> == '0' && TCR_EL1.TBI0 == '1' then
501 target<63:56> = '00000000';
502 when EL2
503 if TCR_EL2.TBI == '1' then
504 target<63:56> = '00000000';
505 when EL3
506 if TCR_EL3.TBI == '1' then
507 target<63:56> = '00000000';
508 _PC = target<63:0>;
509 return;
510#endif
511
512 addr_t addr;
513
514 // Hint_Branch(branch_type);
515 if (N == 32) {
516 if (!UsingAArch32())
517 return false;
518 addr = target;
519 } else if (N == 64) {
520 if (UsingAArch32())
521 return false;
522 // TODO: Remove the tag bits from a tagged target
523 addr = target;
524 } else
525 return false;
526
529}
530
532 // If we are ignoring conditions, then always return true. this allows us to
533 // iterate over disassembly code and still emulate an instruction even if we
534 // don't have all the right bits set in the CPSR register...
536 return true;
537
538 bool result = false;
539 switch (UnsignedBits(cond, 3, 1)) {
540 case 0:
541 result = (m_opcode_pstate.Z == 1);
542 break;
543 case 1:
544 result = (m_opcode_pstate.C == 1);
545 break;
546 case 2:
547 result = (m_opcode_pstate.N == 1);
548 break;
549 case 3:
550 result = (m_opcode_pstate.V == 1);
551 break;
552 case 4:
553 result = (m_opcode_pstate.C == 1 && m_opcode_pstate.Z == 0);
554 break;
555 case 5:
556 result = (m_opcode_pstate.N == m_opcode_pstate.V);
557 break;
558 case 6:
559 result = (m_opcode_pstate.N == m_opcode_pstate.V && m_opcode_pstate.Z == 0);
560 break;
561 case 7:
562 // Always execute (cond == 0b1110, or the special 0b1111 which gives
563 // opcodes different meanings, but always means execution happens.
564 return true;
565 }
566
567 if (cond & 1)
568 result = !result;
569 return result;
570}
571
573AddWithCarry(uint32_t N, uint64_t x, uint64_t y, bit carry_in,
575 uint64_t unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
576 std::optional<int64_t> signed_sum = llvm::checkedAdd(SInt(x), SInt(y));
577 bool overflow = !signed_sum;
578 if (!overflow)
579 overflow |= !llvm::checkedAdd(*signed_sum, SInt(carry_in));
580 uint64_t result = unsigned_sum;
581 if (N < 64)
582 result = Bits64(result, N - 1, 0);
583 proc_state.N = Bit64(result, N - 1);
584 proc_state.Z = IsZero(result);
585 proc_state.C = UInt(result) != unsigned_sum;
586 proc_state.V = overflow;
587 return result;
588}
589
590bool EmulateInstructionARM64::EmulateADDSUBImm(const uint32_t opcode) {
591 // integer d = UInt(Rd);
592 // integer n = UInt(Rn);
593 // integer datasize = if sf == 1 then 64 else 32;
594 // boolean sub_op = (op == 1);
595 // boolean setflags = (S == 1);
596 // bits(datasize) imm;
597 //
598 // case shift of
599 // when '00' imm = ZeroExtend(imm12, datasize);
600 // when '01' imm = ZeroExtend(imm12 : Zeros(12), datasize);
601 // when '1x' UNDEFINED;
602 //
603 //
604 // bits(datasize) result;
605 // bits(datasize) operand1 = if n == 31 then SP[] else X[n];
606 // bits(datasize) operand2 = imm;
607 // bits(4) nzcv;
608 // bit carry_in;
609 //
610 // if sub_op then
611 // operand2 = NOT(operand2);
612 // carry_in = 1;
613 // else
614 // carry_in = 0;
615 //
616 // (result, nzcv) = AddWithCarry(operand1, operand2, carry_in);
617 //
618 // if setflags then
619 // PSTATE.NZCV = nzcv;
620 //
621 // if d == 31 && !setflags then
622 // SP[] = result;
623 // else
624 // X[d] = result;
625
626 const uint32_t sf = Bit32(opcode, 31);
627 const uint32_t op = Bit32(opcode, 30);
628 const uint32_t S = Bit32(opcode, 29);
629 const uint32_t shift = Bits32(opcode, 23, 22);
630 const uint32_t imm12 = Bits32(opcode, 21, 10);
631 const uint32_t Rn = Bits32(opcode, 9, 5);
632 const uint32_t Rd = Bits32(opcode, 4, 0);
633
634 bool success = false;
635
636 const uint32_t d = UInt(Rd);
637 const uint32_t n = UInt(Rn);
638 const uint32_t datasize = (sf == 1) ? 64 : 32;
639 boolean sub_op = op == 1;
640 boolean setflags = S == 1;
641 uint64_t imm;
642
643 switch (shift) {
644 case 0:
645 imm = imm12;
646 break;
647 case 1:
648 imm = static_cast<uint64_t>(imm12) << 12;
649 break;
650 default:
651 return false; // UNDEFINED;
652 }
653 uint64_t result;
654 uint64_t operand1 =
656 uint64_t operand2 = imm;
657 bit carry_in;
658
659 if (sub_op) {
660 operand2 = NOT(operand2);
661 carry_in = true;
662 imm = -imm; // For the Register plug offset context below
663 } else {
664 carry_in = false;
665 }
666
667 ProcState proc_state;
668
669 result = AddWithCarry(datasize, operand1, operand2, carry_in, proc_state);
670
671 if (setflags) {
672 m_emulated_pstate.N = proc_state.N;
673 m_emulated_pstate.Z = proc_state.Z;
674 m_emulated_pstate.C = proc_state.C;
675 m_emulated_pstate.V = proc_state.V;
676 }
677
678 Context context;
679 std::optional<RegisterInfo> reg_info_Rn =
681 if (reg_info_Rn)
682 context.SetRegisterPlusOffset(*reg_info_Rn, imm);
683
684 if (n == GetFramePointerRegisterNumber() && d == gpr_sp_arm64 && !setflags) {
685 // 'mov sp, fp' - common epilogue instruction, CFA is now in terms of the
686 // stack pointer, instead of frame pointer.
688 } else if ((n == gpr_sp_arm64 || n == GetFramePointerRegisterNumber()) &&
689 d == gpr_sp_arm64 && !setflags) {
691 } else if (d == GetFramePointerRegisterNumber() && n == gpr_sp_arm64 &&
692 !setflags) {
694 } else {
696 }
697
698 // If setflags && d == gpr_sp_arm64 then d = WZR/XZR. See CMN, CMP
699 if (!setflags || d != gpr_sp_arm64)
701
702 return false;
703}
704
705template <EmulateInstructionARM64::AddrMode a_mode>
706bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode) {
707 uint32_t opc = Bits32(opcode, 31, 30);
708 uint32_t V = Bit32(opcode, 26);
709 uint32_t L = Bit32(opcode, 22);
710 uint32_t imm7 = Bits32(opcode, 21, 15);
711 uint32_t Rt2 = Bits32(opcode, 14, 10);
712 uint32_t Rn = Bits32(opcode, 9, 5);
713 uint32_t Rt = Bits32(opcode, 4, 0);
714
715 integer n = UInt(Rn);
716 integer t = UInt(Rt);
717 integer t2 = UInt(Rt2);
718 uint64_t idx;
719
720 MemOp memop = L == 1 ? MemOp_LOAD : MemOp_STORE;
721 boolean vector = (V == 1);
722 // AccType acctype = AccType_NORMAL;
723 boolean is_signed = false;
724 boolean wback = a_mode != AddrMode_OFF;
725 boolean wb_unknown = false;
726 boolean rt_unknown = false;
727 integer scale;
728 integer size;
729
730 if (opc == 3)
731 return false; // UNDEFINED
732
733 if (vector) {
734 scale = 2 + UInt(opc);
735 } else {
736 scale = (opc & 2) ? 3 : 2;
737 is_signed = (opc & 1) != 0;
738 if (is_signed && memop == MemOp_STORE)
739 return false; // UNDEFINED
740 }
741
742 if (!vector && wback && ((t == n) || (t2 == n))) {
745 wb_unknown = true; // writeback is UNKNOWN
746 break;
747
749 wback = false; // writeback is suppressed
750 break;
751
752 case Constraint_NOP:
753 memop = MemOp_NOP; // do nothing
754 wback = false;
755 break;
756
757 case Constraint_NONE:
758 break;
759 }
760 }
761
762 if (memop == MemOp_LOAD && t == t2) {
765 rt_unknown = true; // result is UNKNOWN
766 break;
767
768 case Constraint_NOP:
769 memop = MemOp_NOP; // do nothing
770 wback = false;
771 break;
772
773 default:
774 break;
775 }
776 }
777
778 idx = LSL(llvm::SignExtend64<7>(imm7), scale);
779 size = (integer)1 << scale;
780 uint64_t datasize = size * 8;
781 uint64_t address;
782 uint64_t wb_address;
783
784 std::optional<RegisterInfo> reg_info_base =
786 if (!reg_info_base)
787 return false;
788
789 std::optional<RegisterInfo> reg_info_Rt;
790 std::optional<RegisterInfo> reg_info_Rt2;
791
792 if (vector) {
794 reg_info_Rt2 = GetRegisterInfo(eRegisterKindLLDB, fpu_d0_arm64 + t2);
795 } else {
797 reg_info_Rt2 = GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + t2);
798 }
799
800 if (!reg_info_Rt || !reg_info_Rt2)
801 return false;
802
803 bool success = false;
804 if (n == 31) {
805 // CheckSPAlignment();
806 address =
808 } else
809 address =
811
812 wb_address = address + idx;
813 if (a_mode != AddrMode_POST)
814 address = wb_address;
815
816 Context context_t;
817 Context context_t2;
818
821
822 switch (memop) {
823 case MemOp_STORE: {
824 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
825 // based off of the sp
826 // or fp register
827 {
830 } else {
831 context_t.type = eContextRegisterStore;
832 context_t2.type = eContextRegisterStore;
833 }
834 context_t.SetRegisterToRegisterPlusOffset(*reg_info_Rt, *reg_info_base, 0);
835 context_t2.SetRegisterToRegisterPlusOffset(*reg_info_Rt2, *reg_info_base,
836 size);
837
838 std::optional<RegisterValue> data_Rt = ReadRegister(*reg_info_Rt);
839 if (!data_Rt)
840 return false;
841
842 buffer.resize(reg_info_Rt->byte_size);
843 if (data_Rt->GetAsMemoryData(*reg_info_Rt, buffer.data(),
844 reg_info_Rt->byte_size, eByteOrderLittle,
845 error) == 0)
846 return false;
847
848 if (!WriteMemory(context_t, address + 0, buffer.data(),
849 reg_info_Rt->byte_size))
850 return false;
851
852 std::optional<RegisterValue> data_Rt2 = ReadRegister(*reg_info_Rt2);
853 if (!data_Rt2)
854 return false;
855
856 buffer.resize(reg_info_Rt2->byte_size);
857 if (data_Rt2->GetAsMemoryData(*reg_info_Rt2, buffer.data(),
858 reg_info_Rt2->byte_size, eByteOrderLittle,
859 error) == 0)
860 return false;
861
862 if (!WriteMemory(context_t2, address + size, buffer.data(),
863 reg_info_Rt2->byte_size))
864 return false;
865 } break;
866
867 case MemOp_LOAD: {
868 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this load is
869 // based off of the sp
870 // or fp register
871 {
874 } else {
875 context_t.type = eContextRegisterLoad;
876 context_t2.type = eContextRegisterLoad;
877 }
878 context_t.SetAddress(address);
879 context_t2.SetAddress(address + size);
880
881 buffer.resize(reg_info_Rt->byte_size);
882 if (rt_unknown)
883 std::fill(buffer.begin(), buffer.end(), 'U');
884 else {
885 if (!ReadMemory(context_t, address, buffer.data(),
886 reg_info_Rt->byte_size))
887 return false;
888 }
889
890 RegisterValue data_Rt;
891 if (data_Rt.SetFromMemoryData(*reg_info_Rt, buffer.data(),
892 reg_info_Rt->byte_size, eByteOrderLittle,
893 error) == 0)
894 return false;
895
896 if (!vector && is_signed && !data_Rt.SignExtend(datasize))
897 return false;
898
899 if (!WriteRegister(context_t, *reg_info_Rt, data_Rt))
900 return false;
901
902 buffer.resize(reg_info_Rt2->byte_size);
903 if (!rt_unknown)
904 if (!ReadMemory(context_t2, address + size, buffer.data(),
905 reg_info_Rt2->byte_size))
906 return false;
907
908 RegisterValue data_Rt2;
909 if (data_Rt2.SetFromMemoryData(*reg_info_Rt2, buffer.data(),
910 reg_info_Rt2->byte_size, eByteOrderLittle,
911 error) == 0)
912 return false;
913
914 if (!vector && is_signed && !data_Rt2.SignExtend(datasize))
915 return false;
916
917 if (!WriteRegister(context_t2, *reg_info_Rt2, data_Rt2))
918 return false;
919 } break;
920
921 default:
922 break;
923 }
924
925 if (wback) {
926 if (wb_unknown)
927 wb_address = LLDB_INVALID_ADDRESS;
928 Context context;
929 context.SetImmediateSigned(idx);
930 if (n == 31)
932 else
934 WriteRegisterUnsigned(context, *reg_info_base, wb_address);
935 }
936 return true;
937}
938
939template <EmulateInstructionARM64::AddrMode a_mode>
940bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode) {
941 uint32_t size = Bits32(opcode, 31, 30);
942 uint32_t opc = Bits32(opcode, 23, 22);
943 uint32_t vr = Bit32(opcode, 26);
944 uint32_t n = Bits32(opcode, 9, 5);
945 uint32_t t = Bits32(opcode, 4, 0);
946
947 MemOp memop;
948 if (vr) {
949 // opc<1> == 1 && size != 0 is an undefined encoding.
950 if (Bit32(opc, 1) == 1 && size != 0)
951 return false;
952 // opc<1> == 1 && size == 0 encode the 128-bit variant.
953 if (Bit32(opc, 1) == 1)
954 size = 4;
955 memop = Bit32(opc, 0) == 1 ? MemOp_LOAD : MemOp_STORE;
956 } else {
957 if (Bit32(opc, 1) == 0) {
958 memop = Bit32(opc, 0) == 1 ? MemOp_LOAD : MemOp_STORE;
959 } else {
960 memop = MemOp_LOAD;
961 if (size == 2 && Bit32(opc, 0) == 1)
962 return false;
963 }
964 }
965
966 bool wback;
967 bool postindex;
968 uint64_t offset;
969
970 switch (a_mode) {
971 case AddrMode_POST:
972 wback = true;
973 postindex = true;
974 offset = llvm::SignExtend64<9>(Bits32(opcode, 20, 12));
975 break;
976 case AddrMode_PRE:
977 wback = true;
978 postindex = false;
979 offset = llvm::SignExtend64<9>(Bits32(opcode, 20, 12));
980 break;
981 case AddrMode_OFF:
982 wback = false;
983 postindex = false;
984 offset = LSL(Bits32(opcode, 21, 10), size);
985 break;
986 }
987
989 bool success = false;
990 uint64_t address;
992
993 if (n == 31)
994 address =
996 else
997 address =
999
1000 if (!success)
1001 return false;
1002
1003 if (!postindex)
1004 address += offset;
1005
1006 std::optional<RegisterInfo> reg_info_base =
1008 if (!reg_info_base)
1009 return false;
1010
1011 std::optional<RegisterInfo> reg_info_Rt =
1014 if (!reg_info_Rt)
1015 return false;
1016
1017 Context context;
1018 switch (memop) {
1019 case MemOp_STORE: {
1020 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
1021 // based off of the sp
1022 // or fp register
1024 else
1025 context.type = eContextRegisterStore;
1026 context.SetRegisterToRegisterPlusOffset(*reg_info_Rt, *reg_info_base,
1027 postindex ? 0 : offset);
1028
1029 std::optional<RegisterValue> data_Rt = ReadRegister(*reg_info_Rt);
1030 if (!data_Rt)
1031 return false;
1032
1033 buffer.resize(reg_info_Rt->byte_size);
1034 if (data_Rt->GetAsMemoryData(*reg_info_Rt, buffer.data(),
1035 reg_info_Rt->byte_size, eByteOrderLittle,
1036 error) == 0)
1037 return false;
1038
1039 if (!WriteMemory(context, address, buffer.data(), reg_info_Rt->byte_size))
1040 return false;
1041 } break;
1042
1043 case MemOp_LOAD: {
1044 if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
1045 // based off of the sp
1046 // or fp register
1048 else
1049 context.type = eContextRegisterLoad;
1050 context.SetAddress(address);
1051
1052 buffer.resize(reg_info_Rt->byte_size);
1053 if (!ReadMemory(context, address, buffer.data(), reg_info_Rt->byte_size))
1054 return false;
1055
1056 RegisterValue data_Rt;
1057 if (data_Rt.SetFromMemoryData(*reg_info_Rt, buffer.data(),
1058 reg_info_Rt->byte_size, eByteOrderLittle,
1059 error) == 0)
1060 return false;
1061
1062 if (!WriteRegister(context, *reg_info_Rt, data_Rt))
1063 return false;
1064 } break;
1065 default:
1066 return false;
1067 }
1068
1069 if (wback) {
1070 if (postindex)
1071 address += offset;
1072
1073 if (n == 31)
1075 else
1077 context.SetImmediateSigned(offset);
1078
1079 if (!WriteRegisterUnsigned(context, *reg_info_base, address))
1080 return false;
1081 }
1082 return true;
1083}
1084
1085bool EmulateInstructionARM64::EmulateB(const uint32_t opcode) {
1086#if 0
1087 // ARM64 pseudo code...
1088 if branch_type == BranchType_CALL then X[30] = PC[] + 4;
1089 BranchTo(PC[] + offset, branch_type);
1090#endif
1091
1092 bool success = false;
1093
1097 LLDB_REGNUM_GENERIC_PC, 0, &success);
1098 if (!success)
1099 return false;
1100
1101 int64_t offset = llvm::SignExtend64<28>(Bits32(opcode, 25, 0) << 2);
1102 BranchType branch_type = Bit32(opcode, 31) ? BranchType_CALL : BranchType_JMP;
1103 addr_t target = pc + offset;
1104 context.SetImmediateSigned(offset);
1105
1106 switch (branch_type) {
1107 case BranchType_CALL: {
1108 addr_t x30 = pc + 4;
1110 return false;
1111 } break;
1112 case BranchType_JMP:
1113 break;
1114 default:
1115 return false;
1116 }
1117
1118 return BranchTo(context, 64, target);
1119}
1120
1121bool EmulateInstructionARM64::EmulateBcond(const uint32_t opcode) {
1122#if 0
1123 // ARM64 pseudo code...
1124 bits(64) offset = SignExtend(imm19:'00', 64);
1125 bits(4) condition = cond;
1126 if ConditionHolds(condition) then
1127 BranchTo(PC[] + offset, BranchType_JMP);
1128#endif
1129
1130 if (ConditionHolds(Bits32(opcode, 3, 0))) {
1131 bool success = false;
1132
1133 const uint64_t pc = ReadRegisterUnsigned(
1135 if (!success)
1136 return false;
1137
1138 int64_t offset = llvm::SignExtend64<21>(Bits32(opcode, 23, 5) << 2);
1139 addr_t target = pc + offset;
1140
1143 context.SetImmediateSigned(offset);
1144 if (!BranchTo(context, 64, target))
1145 return false;
1146 }
1147 return true;
1148}
1149
1150bool EmulateInstructionARM64::EmulateCBZ(const uint32_t opcode) {
1151#if 0
1152 integer t = UInt(Rt);
1153 integer datasize = if sf == '1' then 64 else 32;
1154 boolean iszero = (op == '0');
1155 bits(64) offset = SignExtend(imm19:'00', 64);
1156
1157 bits(datasize) operand1 = X[t];
1158 if IsZero(operand1) == iszero then
1159 BranchTo(PC[] + offset, BranchType_JMP);
1160#endif
1161
1162 bool success = false;
1163
1164 uint32_t t = Bits32(opcode, 4, 0);
1165 bool is_zero = Bit32(opcode, 24) == 0;
1166 int32_t offset = llvm::SignExtend64<21>(Bits32(opcode, 23, 5) << 2);
1167
1168 const uint64_t operand =
1170 if (!success)
1171 return false;
1172
1173 if (m_ignore_conditions || ((operand == 0) == is_zero)) {
1174 const uint64_t pc = ReadRegisterUnsigned(
1176 if (!success)
1177 return false;
1178
1181 context.SetImmediateSigned(offset);
1182 if (!BranchTo(context, 64, pc + offset))
1183 return false;
1184 }
1185 return true;
1186}
1187
1188bool EmulateInstructionARM64::EmulateTBZ(const uint32_t opcode) {
1189#if 0
1190 integer t = UInt(Rt);
1191 integer datasize = if b5 == '1' then 64 else 32;
1192 integer bit_pos = UInt(b5:b40);
1193 bit bit_val = op;
1194 bits(64) offset = SignExtend(imm14:'00', 64);
1195#endif
1196
1197 bool success = false;
1198
1199 uint32_t t = Bits32(opcode, 4, 0);
1200 uint32_t bit_pos = (Bit32(opcode, 31) << 6) | (Bits32(opcode, 23, 19));
1201 uint32_t bit_val = Bit32(opcode, 24);
1202 int64_t offset = llvm::SignExtend64<16>(Bits32(opcode, 18, 5) << 2);
1203
1204 const uint64_t operand =
1206 if (!success)
1207 return false;
1208
1209 if (m_ignore_conditions || Bit32(operand, bit_pos) == bit_val) {
1210 const uint64_t pc = ReadRegisterUnsigned(
1212 if (!success)
1213 return false;
1214
1217 context.SetImmediateSigned(offset);
1218 if (!BranchTo(context, 64, pc + offset))
1219 return false;
1220 }
1221 return true;
1222}
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 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)
bool EmulateB(const uint32_t opcode)
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:31
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:468
"lldb/Core/EmulateInstruction.h" A class that allows emulation of CPU opcodes.
lldb::ByteOrder GetByteOrder() const
size_t 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:240
bool SetRegisterLocationToSame(uint32_t reg_num, bool must_replace)
const FAValue & GetCFAValue() const
Definition UnwindPlan.h:365
void SetUnwindPlanForSignalTrap(lldb_private::LazyBool is_for_signal_trap)
Definition UnwindPlan.h:525
void SetRegisterKind(lldb::RegisterKind kind)
Definition UnwindPlan.h:462
void SetReturnAddressRegister(uint32_t regnum)
Definition UnwindPlan.h:464
void SetSourcedFromCompiler(lldb_private::LazyBool from_compiler)
Definition UnwindPlan.h:501
void SetSourceName(const char *)
void SetUnwindPlanValidAtAllInstructions(lldb_private::LazyBool valid_at_all_insn)
Definition UnwindPlan.h:513
#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),...