1 //===-- EmulateInstructionARM64.cpp ---------------------------------------===//
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"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/PluginManager.h"
13 #include "lldb/Symbol/UnwindPlan.h"
14 #include "lldb/Utility/ArchSpec.h"
15 #include "lldb/Utility/RegisterValue.h"
16 #include "lldb/Utility/Stream.h"
18 #include "llvm/Support/CheckedArithmetic.h"
20 #include "Plugins/Process/Utility/ARMDefines.h"
21 #include "Plugins/Process/Utility/ARMUtils.h"
22 #include "Plugins/Process/Utility/lldb-arm64-register-enums.h"
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
41 #define DECLARE_REGISTER_INFOS_ARM64_STRUCT
43 #include "Plugins/Process/Utility/RegisterInfos_arm64.h"
45 #include "llvm/ADT/STLExtras.h"
46 #include "llvm/Support/MathExtras.h"
48 #include "Plugins/Process/Utility/InstructionUtils.h"
51 using namespace lldb_private
;
53 LLDB_PLUGIN_DEFINE_ADV(EmulateInstructionARM64
, InstructionARM64
)
55 static std::optional
<RegisterInfo
> LLDBTableGetRegisterInfo(uint32_t reg_num
) {
56 if (reg_num
>= std::size(g_register_infos_arm64_le
))
58 return g_register_infos_arm64_le
[reg_num
];
62 #define VFPv1 (1u << 1)
63 #define VFPv2 (1u << 2)
64 #define VFPv3 (1u << 3)
65 #define AdvancedSIMD (1u << 4)
67 #define VFPv1_ABOVE (VFPv1 | VFPv2 | VFPv3 | AdvancedSIMD)
68 #define VFPv2_ABOVE (VFPv2 | VFPv3 | AdvancedSIMD)
69 #define VFPv2v3 (VFPv2 | VFPv3)
71 #define UInt(x) ((uint64_t)x)
72 #define SInt(x) ((int64_t)x)
75 #define integer int64_t
77 static inline bool IsZero(uint64_t x
) { return x
== 0; }
79 static inline uint64_t NOT(uint64_t x
) { return ~x
; }
84 static inline uint64_t LSL(uint64_t x
, integer shift
) {
90 // ConstrainUnpredictable()
91 // ========================
93 EmulateInstructionARM64::ConstraintType
94 ConstrainUnpredictable(EmulateInstructionARM64::Unpredictable which
) {
95 EmulateInstructionARM64::ConstraintType result
=
96 EmulateInstructionARM64::Constraint_UNKNOWN
;
98 case EmulateInstructionARM64::Unpredictable_WBOVERLAP
:
99 case EmulateInstructionARM64::Unpredictable_LDPOVERLAP
:
100 // TODO: don't know what to really do here? Pseudo code says:
101 // set result to one of above Constraint behaviours or UNDEFINED
108 // EmulateInstructionARM implementation
111 void EmulateInstructionARM64::Initialize() {
112 PluginManager::RegisterPlugin(GetPluginNameStatic(),
113 GetPluginDescriptionStatic(), CreateInstance
);
116 void EmulateInstructionARM64::Terminate() {
117 PluginManager::UnregisterPlugin(CreateInstance
);
120 llvm::StringRef
EmulateInstructionARM64::GetPluginDescriptionStatic() {
121 return "Emulate instructions for the ARM64 architecture.";
125 EmulateInstructionARM64::CreateInstance(const ArchSpec
&arch
,
126 InstructionType inst_type
) {
127 if (EmulateInstructionARM64::SupportsEmulatingInstructionsOfTypeStatic(
129 if (arch
.GetTriple().getArch() == llvm::Triple::aarch64
||
130 arch
.GetTriple().getArch() == llvm::Triple::aarch64_32
) {
131 return new EmulateInstructionARM64(arch
);
138 bool EmulateInstructionARM64::SetTargetTriple(const ArchSpec
&arch
) {
139 if (arch
.GetTriple().getArch() == llvm::Triple::arm
)
141 else if (arch
.GetTriple().getArch() == llvm::Triple::thumb
)
147 std::optional
<RegisterInfo
>
148 EmulateInstructionARM64::GetRegisterInfo(RegisterKind reg_kind
,
150 if (reg_kind
== eRegisterKindGeneric
) {
152 case LLDB_REGNUM_GENERIC_PC
:
153 reg_kind
= eRegisterKindLLDB
;
154 reg_num
= gpr_pc_arm64
;
156 case LLDB_REGNUM_GENERIC_SP
:
157 reg_kind
= eRegisterKindLLDB
;
158 reg_num
= gpr_sp_arm64
;
160 case LLDB_REGNUM_GENERIC_FP
:
161 reg_kind
= eRegisterKindLLDB
;
162 reg_num
= gpr_fp_arm64
;
164 case LLDB_REGNUM_GENERIC_RA
:
165 reg_kind
= eRegisterKindLLDB
;
166 reg_num
= gpr_lr_arm64
;
168 case LLDB_REGNUM_GENERIC_FLAGS
:
169 reg_kind
= eRegisterKindLLDB
;
170 reg_num
= gpr_cpsr_arm64
;
178 if (reg_kind
== eRegisterKindLLDB
)
179 return LLDBTableGetRegisterInfo(reg_num
);
183 EmulateInstructionARM64::Opcode
*
184 EmulateInstructionARM64::GetOpcodeForInstruction(const uint32_t opcode
) {
185 static EmulateInstructionARM64::Opcode g_opcodes
[] = {
186 // Prologue instructions
189 {0xff000000, 0xd1000000, No_VFP
,
190 &EmulateInstructionARM64::EmulateADDSUBImm
,
191 "SUB <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}"},
192 {0xff000000, 0xf1000000, No_VFP
,
193 &EmulateInstructionARM64::EmulateADDSUBImm
,
194 "SUBS <Xd>, <Xn|SP>, #<imm> {, <shift>}"},
195 {0xff000000, 0x91000000, No_VFP
,
196 &EmulateInstructionARM64::EmulateADDSUBImm
,
197 "ADD <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}"},
198 {0xff000000, 0xb1000000, No_VFP
,
199 &EmulateInstructionARM64::EmulateADDSUBImm
,
200 "ADDS <Xd>, <Xn|SP>, #<imm> {, <shift>}"},
202 {0xff000000, 0x51000000, No_VFP
,
203 &EmulateInstructionARM64::EmulateADDSUBImm
,
204 "SUB <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}"},
205 {0xff000000, 0x71000000, No_VFP
,
206 &EmulateInstructionARM64::EmulateADDSUBImm
,
207 "SUBS <Wd>, <Wn|WSP>, #<imm> {, <shift>}"},
208 {0xff000000, 0x11000000, No_VFP
,
209 &EmulateInstructionARM64::EmulateADDSUBImm
,
210 "ADD <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}"},
211 {0xff000000, 0x31000000, No_VFP
,
212 &EmulateInstructionARM64::EmulateADDSUBImm
,
213 "ADDS <Wd>, <Wn|WSP>, #<imm> {, <shift>}"},
215 {0xffc00000, 0x29000000, No_VFP
,
216 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
217 "STP <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]"},
218 {0xffc00000, 0xa9000000, No_VFP
,
219 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
220 "STP <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]"},
221 {0xffc00000, 0x2d000000, No_VFP
,
222 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
223 "STP <St>, <St2>, [<Xn|SP>{, #<imm>}]"},
224 {0xffc00000, 0x6d000000, No_VFP
,
225 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
226 "STP <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]"},
227 {0xffc00000, 0xad000000, No_VFP
,
228 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
229 "STP <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]"},
231 {0xffc00000, 0x29800000, No_VFP
,
232 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
233 "STP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
234 {0xffc00000, 0xa9800000, No_VFP
,
235 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
236 "STP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
237 {0xffc00000, 0x2d800000, No_VFP
,
238 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
239 "STP <St>, <St2>, [<Xn|SP>, #<imm>]!"},
240 {0xffc00000, 0x6d800000, No_VFP
,
241 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
242 "STP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
243 {0xffc00000, 0xad800000, No_VFP
,
244 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
245 "STP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
247 {0xffc00000, 0x28800000, No_VFP
,
248 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
249 "STP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
250 {0xffc00000, 0xa8800000, No_VFP
,
251 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
252 "STP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
253 {0xffc00000, 0x2c800000, No_VFP
,
254 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
255 "STP <St>, <St2>, [<Xn|SP>, #<imm>]!"},
256 {0xffc00000, 0x6c800000, No_VFP
,
257 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
258 "STP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
259 {0xffc00000, 0xac800000, No_VFP
,
260 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
261 "STP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
263 {0xffc00000, 0x29400000, No_VFP
,
264 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
265 "LDP <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]"},
266 {0xffc00000, 0xa9400000, No_VFP
,
267 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
268 "LDP <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]"},
269 {0xffc00000, 0x2d400000, No_VFP
,
270 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
271 "LDP <St>, <St2>, [<Xn|SP>{, #<imm>}]"},
272 {0xffc00000, 0x6d400000, No_VFP
,
273 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
274 "LDP <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]"},
275 {0xffc00000, 0xad400000, No_VFP
,
276 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_OFF
>,
277 "LDP <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]"},
279 {0xffc00000, 0x29c00000, No_VFP
,
280 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
281 "LDP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
282 {0xffc00000, 0xa9c00000, No_VFP
,
283 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
284 "LDP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
285 {0xffc00000, 0x2dc00000, No_VFP
,
286 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
287 "LDP <St>, <St2>, [<Xn|SP>, #<imm>]!"},
288 {0xffc00000, 0x6dc00000, No_VFP
,
289 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
290 "LDP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
291 {0xffc00000, 0xadc00000, No_VFP
,
292 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_PRE
>,
293 "LDP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
295 {0xffc00000, 0x28c00000, No_VFP
,
296 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
297 "LDP <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
298 {0xffc00000, 0xa8c00000, No_VFP
,
299 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
300 "LDP <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
301 {0xffc00000, 0x2cc00000, No_VFP
,
302 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
303 "LDP <St>, <St2>, [<Xn|SP>, #<imm>]!"},
304 {0xffc00000, 0x6cc00000, No_VFP
,
305 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
306 "LDP <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
307 {0xffc00000, 0xacc00000, No_VFP
,
308 &EmulateInstructionARM64::EmulateLDPSTP
<AddrMode_POST
>,
309 "LDP <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
311 {0xffe00c00, 0xb8000400, No_VFP
,
312 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_POST
>,
313 "STR <Wt>, [<Xn|SP>], #<simm>"},
314 {0xffe00c00, 0xf8000400, No_VFP
,
315 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_POST
>,
316 "STR <Xt>, [<Xn|SP>], #<simm>"},
317 {0xffe00c00, 0xb8000c00, No_VFP
,
318 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_PRE
>,
319 "STR <Wt>, [<Xn|SP>, #<simm>]!"},
320 {0xffe00c00, 0xf8000c00, No_VFP
,
321 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_PRE
>,
322 "STR <Xt>, [<Xn|SP>, #<simm>]!"},
323 {0xffc00000, 0xb9000000, No_VFP
,
324 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_OFF
>,
325 "STR <Wt>, [<Xn|SP>{, #<pimm>}]"},
326 {0xffc00000, 0xf9000000, No_VFP
,
327 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_OFF
>,
328 "STR <Xt>, [<Xn|SP>{, #<pimm>}]"},
330 {0xffe00c00, 0xb8400400, No_VFP
,
331 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_POST
>,
332 "LDR <Wt>, [<Xn|SP>], #<simm>"},
333 {0xffe00c00, 0xf8400400, No_VFP
,
334 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_POST
>,
335 "LDR <Xt>, [<Xn|SP>], #<simm>"},
336 {0xffe00c00, 0xb8400c00, No_VFP
,
337 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_PRE
>,
338 "LDR <Wt>, [<Xn|SP>, #<simm>]!"},
339 {0xffe00c00, 0xf8400c00, No_VFP
,
340 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_PRE
>,
341 "LDR <Xt>, [<Xn|SP>, #<simm>]!"},
342 {0xffc00000, 0xb9400000, No_VFP
,
343 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_OFF
>,
344 "LDR <Wt>, [<Xn|SP>{, #<pimm>}]"},
345 {0xffc00000, 0xf9400000, No_VFP
,
346 &EmulateInstructionARM64::EmulateLDRSTRImm
<AddrMode_OFF
>,
347 "LDR <Xt>, [<Xn|SP>{, #<pimm>}]"},
349 {0xfc000000, 0x14000000, No_VFP
, &EmulateInstructionARM64::EmulateB
,
351 {0xff000010, 0x54000000, No_VFP
, &EmulateInstructionARM64::EmulateBcond
,
353 {0x7f000000, 0x34000000, No_VFP
, &EmulateInstructionARM64::EmulateCBZ
,
354 "CBZ <Wt>, <label>"},
355 {0x7f000000, 0x35000000, No_VFP
, &EmulateInstructionARM64::EmulateCBZ
,
356 "CBNZ <Wt>, <label>"},
357 {0x7f000000, 0x36000000, No_VFP
, &EmulateInstructionARM64::EmulateTBZ
,
358 "TBZ <R><t>, #<imm>, <label>"},
359 {0x7f000000, 0x37000000, No_VFP
, &EmulateInstructionARM64::EmulateTBZ
,
360 "TBNZ <R><t>, #<imm>, <label>"},
363 static const size_t k_num_arm_opcodes
= std::size(g_opcodes
);
365 for (size_t i
= 0; i
< k_num_arm_opcodes
; ++i
) {
366 if ((g_opcodes
[i
].mask
& opcode
) == g_opcodes
[i
].value
)
367 return &g_opcodes
[i
];
372 bool EmulateInstructionARM64::ReadInstruction() {
373 bool success
= false;
374 m_addr
= ReadRegisterUnsigned(eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_PC
,
375 LLDB_INVALID_ADDRESS
, &success
);
377 Context read_inst_context
;
378 read_inst_context
.type
= eContextReadOpcode
;
379 read_inst_context
.SetNoArgs();
380 m_opcode
.SetOpcode32(
381 ReadMemoryUnsigned(read_inst_context
, m_addr
, 4, 0, &success
),
385 m_addr
= LLDB_INVALID_ADDRESS
;
389 bool EmulateInstructionARM64::EvaluateInstruction(uint32_t evaluate_options
) {
390 const uint32_t opcode
= m_opcode
.GetOpcode32();
391 Opcode
*opcode_data
= GetOpcodeForInstruction(opcode
);
392 if (opcode_data
== nullptr)
395 const bool auto_advance_pc
=
396 evaluate_options
& eEmulateInstructionOptionAutoAdvancePC
;
397 m_ignore_conditions
=
398 evaluate_options
& eEmulateInstructionOptionIgnoreConditions
;
400 bool success
= false;
402 // Only return false if we are unable to read the CPSR if we care about
404 if (!success
&& !m_ignore_conditions
)
407 uint32_t orig_pc_value
= 0;
408 if (auto_advance_pc
) {
410 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_pc_arm64
, 0, &success
);
415 // Call the Emulate... function.
416 success
= (this->*opcode_data
->callback
)(opcode
);
420 if (auto_advance_pc
) {
421 uint32_t new_pc_value
=
422 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_pc_arm64
, 0, &success
);
426 if (new_pc_value
== orig_pc_value
) {
427 EmulateInstruction::Context context
;
428 context
.type
= eContextAdvancePC
;
430 if (!WriteRegisterUnsigned(context
, eRegisterKindLLDB
, gpr_pc_arm64
,
438 bool EmulateInstructionARM64::CreateFunctionEntryUnwind(
439 UnwindPlan
&unwind_plan
) {
441 unwind_plan
.SetRegisterKind(eRegisterKindLLDB
);
443 UnwindPlan::RowSP
row(new UnwindPlan::Row
);
445 // Our previous Call Frame Address is the stack pointer
446 row
->GetCFAValue().SetIsRegisterPlusOffset(gpr_sp_arm64
, 0);
447 row
->SetRegisterLocationToSame(gpr_lr_arm64
, /*must_replace=*/false);
448 row
->SetRegisterLocationToSame(gpr_fp_arm64
, /*must_replace=*/false);
450 unwind_plan
.AppendRow(row
);
451 unwind_plan
.SetSourceName("EmulateInstructionARM64");
452 unwind_plan
.SetSourcedFromCompiler(eLazyBoolNo
);
453 unwind_plan
.SetUnwindPlanValidAtAllInstructions(eLazyBoolYes
);
454 unwind_plan
.SetUnwindPlanForSignalTrap(eLazyBoolNo
);
455 unwind_plan
.SetReturnAddressRegister(gpr_lr_arm64
);
459 uint32_t EmulateInstructionARM64::GetFramePointerRegisterNumber() const {
460 if (m_arch
.GetTriple().isAndroid())
461 return LLDB_INVALID_REGNUM
; // Don't use frame pointer on android
466 bool EmulateInstructionARM64::UsingAArch32() {
467 bool aarch32
= m_opcode_pstate
.RW
== 1;
468 // if !HaveAnyAArch32() then assert !aarch32;
469 // if HighestELUsingAArch32() then assert aarch32;
473 bool EmulateInstructionARM64::BranchTo(const Context
&context
, uint32_t N
,
476 // Set program counter to a new address, with a branch reason hint for
477 // possible use by hardware fetching the next instruction.
478 BranchTo(bits(N
) target
, BranchType branch_type
)
479 Hint_Branch(branch_type
);
481 assert UsingAArch32();
482 _PC
= ZeroExtend(target
);
484 assert N
== 64 && !UsingAArch32();
485 // Remove the tag bits from a tagged target
488 if target
<55> == '1' && TCR_EL1
.TBI1
== '1' then
489 target
<63:56> = '11111111';
490 if target
<55> == '0' && TCR_EL1
.TBI0
== '1' then
491 target
<63:56> = '00000000';
493 if TCR_EL2
.TBI
== '1' then
494 target
<63:56> = '00000000';
496 if TCR_EL3
.TBI
== '1' then
497 target
<63:56> = '00000000';
504 // Hint_Branch(branch_type);
509 } else if (N
== 64) {
512 // TODO: Remove the tag bits from a tagged target
517 return WriteRegisterUnsigned(context
, eRegisterKindGeneric
,
518 LLDB_REGNUM_GENERIC_PC
, addr
);
521 bool EmulateInstructionARM64::ConditionHolds(const uint32_t cond
) {
522 // If we are ignoring conditions, then always return true. this allows us to
523 // iterate over disassembly code and still emulate an instruction even if we
524 // don't have all the right bits set in the CPSR register...
525 if (m_ignore_conditions
)
529 switch (UnsignedBits(cond
, 3, 1)) {
531 result
= (m_opcode_pstate
.Z
== 1);
534 result
= (m_opcode_pstate
.C
== 1);
537 result
= (m_opcode_pstate
.N
== 1);
540 result
= (m_opcode_pstate
.V
== 1);
543 result
= (m_opcode_pstate
.C
== 1 && m_opcode_pstate
.Z
== 0);
546 result
= (m_opcode_pstate
.N
== m_opcode_pstate
.V
);
549 result
= (m_opcode_pstate
.N
== m_opcode_pstate
.V
&& m_opcode_pstate
.Z
== 0);
552 // Always execute (cond == 0b1110, or the special 0b1111 which gives
553 // opcodes different meanings, but always means execution happens.
562 uint64_t EmulateInstructionARM64::
563 AddWithCarry(uint32_t N
, uint64_t x
, uint64_t y
, bit carry_in
,
564 EmulateInstructionARM64::ProcState
&proc_state
) {
565 uint64_t unsigned_sum
= UInt(x
) + UInt(y
) + UInt(carry_in
);
566 std::optional
<int64_t> signed_sum
= llvm::checkedAdd(SInt(x
), SInt(y
));
567 bool overflow
= !signed_sum
;
569 overflow
|= !llvm::checkedAdd(*signed_sum
, SInt(carry_in
));
570 uint64_t result
= unsigned_sum
;
572 result
= Bits64(result
, N
- 1, 0);
573 proc_state
.N
= Bit64(result
, N
- 1);
574 proc_state
.Z
= IsZero(result
);
575 proc_state
.C
= UInt(result
) != unsigned_sum
;
576 proc_state
.V
= overflow
;
580 bool EmulateInstructionARM64::EmulateADDSUBImm(const uint32_t opcode
) {
581 // integer d = UInt(Rd);
582 // integer n = UInt(Rn);
583 // integer datasize = if sf == 1 then 64 else 32;
584 // boolean sub_op = (op == 1);
585 // boolean setflags = (S == 1);
586 // bits(datasize) imm;
589 // when '00' imm = ZeroExtend(imm12, datasize);
590 // when '01' imm = ZeroExtend(imm12 : Zeros(12), datasize);
591 // when '1x' UNDEFINED;
594 // bits(datasize) result;
595 // bits(datasize) operand1 = if n == 31 then SP[] else X[n];
596 // bits(datasize) operand2 = imm;
601 // operand2 = NOT(operand2);
606 // (result, nzcv) = AddWithCarry(operand1, operand2, carry_in);
609 // PSTATE.NZCV = nzcv;
611 // if d == 31 && !setflags then
616 const uint32_t sf
= Bit32(opcode
, 31);
617 const uint32_t op
= Bit32(opcode
, 30);
618 const uint32_t S
= Bit32(opcode
, 29);
619 const uint32_t shift
= Bits32(opcode
, 23, 22);
620 const uint32_t imm12
= Bits32(opcode
, 21, 10);
621 const uint32_t Rn
= Bits32(opcode
, 9, 5);
622 const uint32_t Rd
= Bits32(opcode
, 4, 0);
624 bool success
= false;
626 const uint32_t d
= UInt(Rd
);
627 const uint32_t n
= UInt(Rn
);
628 const uint32_t datasize
= (sf
== 1) ? 64 : 32;
629 boolean sub_op
= op
== 1;
630 boolean setflags
= S
== 1;
638 imm
= static_cast<uint64_t>(imm12
) << 12;
641 return false; // UNDEFINED;
645 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_x0_arm64
+ n
, 0, &success
);
646 uint64_t operand2
= imm
;
650 operand2
= NOT(operand2
);
652 imm
= -imm
; // For the Register plug offset context below
657 ProcState proc_state
;
659 result
= AddWithCarry(datasize
, operand1
, operand2
, carry_in
, proc_state
);
662 m_emulated_pstate
.N
= proc_state
.N
;
663 m_emulated_pstate
.Z
= proc_state
.Z
;
664 m_emulated_pstate
.C
= proc_state
.C
;
665 m_emulated_pstate
.V
= proc_state
.V
;
669 std::optional
<RegisterInfo
> reg_info_Rn
=
670 GetRegisterInfo(eRegisterKindLLDB
, n
);
672 context
.SetRegisterPlusOffset(*reg_info_Rn
, imm
);
674 if (n
== GetFramePointerRegisterNumber() && d
== gpr_sp_arm64
&& !setflags
) {
675 // 'mov sp, fp' - common epilogue instruction, CFA is now in terms of the
676 // stack pointer, instead of frame pointer.
677 context
.type
= EmulateInstruction::eContextRestoreStackPointer
;
678 } else if ((n
== gpr_sp_arm64
|| n
== GetFramePointerRegisterNumber()) &&
679 d
== gpr_sp_arm64
&& !setflags
) {
680 context
.type
= EmulateInstruction::eContextAdjustStackPointer
;
681 } else if (d
== GetFramePointerRegisterNumber() && n
== gpr_sp_arm64
&&
683 context
.type
= EmulateInstruction::eContextSetFramePointer
;
685 context
.type
= EmulateInstruction::eContextImmediate
;
688 // If setflags && d == gpr_sp_arm64 then d = WZR/XZR. See CMN, CMP
689 if (!setflags
|| d
!= gpr_sp_arm64
)
690 WriteRegisterUnsigned(context
, eRegisterKindLLDB
, gpr_x0_arm64
+ d
, result
);
695 template <EmulateInstructionARM64::AddrMode a_mode
>
696 bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode
) {
697 uint32_t opc
= Bits32(opcode
, 31, 30);
698 uint32_t V
= Bit32(opcode
, 26);
699 uint32_t L
= Bit32(opcode
, 22);
700 uint32_t imm7
= Bits32(opcode
, 21, 15);
701 uint32_t Rt2
= Bits32(opcode
, 14, 10);
702 uint32_t Rn
= Bits32(opcode
, 9, 5);
703 uint32_t Rt
= Bits32(opcode
, 4, 0);
705 integer n
= UInt(Rn
);
706 integer t
= UInt(Rt
);
707 integer t2
= UInt(Rt2
);
710 MemOp memop
= L
== 1 ? MemOp_LOAD
: MemOp_STORE
;
711 boolean vector
= (V
== 1);
712 // AccType acctype = AccType_NORMAL;
713 boolean is_signed
= false;
714 boolean wback
= a_mode
!= AddrMode_OFF
;
715 boolean wb_unknown
= false;
716 boolean rt_unknown
= false;
721 return false; // UNDEFINED
724 scale
= 2 + UInt(opc
);
726 scale
= (opc
& 2) ? 3 : 2;
727 is_signed
= (opc
& 1) != 0;
728 if (is_signed
&& memop
== MemOp_STORE
)
729 return false; // UNDEFINED
732 if (!vector
&& wback
&& ((t
== n
) || (t2
== n
))) {
733 switch (ConstrainUnpredictable(Unpredictable_WBOVERLAP
)) {
734 case Constraint_UNKNOWN
:
735 wb_unknown
= true; // writeback is UNKNOWN
738 case Constraint_SUPPRESSWB
:
739 wback
= false; // writeback is suppressed
743 memop
= MemOp_NOP
; // do nothing
747 case Constraint_NONE
:
752 if (memop
== MemOp_LOAD
&& t
== t2
) {
753 switch (ConstrainUnpredictable(Unpredictable_LDPOVERLAP
)) {
754 case Constraint_UNKNOWN
:
755 rt_unknown
= true; // result is UNKNOWN
759 memop
= MemOp_NOP
; // do nothing
768 idx
= LSL(llvm::SignExtend64
<7>(imm7
), scale
);
769 size
= (integer
)1 << scale
;
770 uint64_t datasize
= size
* 8;
774 std::optional
<RegisterInfo
> reg_info_base
=
775 GetRegisterInfo(eRegisterKindLLDB
, gpr_x0_arm64
+ n
);
779 std::optional
<RegisterInfo
> reg_info_Rt
;
780 std::optional
<RegisterInfo
> reg_info_Rt2
;
783 reg_info_Rt
= GetRegisterInfo(eRegisterKindLLDB
, fpu_d0_arm64
+ t
);
784 reg_info_Rt2
= GetRegisterInfo(eRegisterKindLLDB
, fpu_d0_arm64
+ t2
);
786 reg_info_Rt
= GetRegisterInfo(eRegisterKindLLDB
, gpr_x0_arm64
+ t
);
787 reg_info_Rt2
= GetRegisterInfo(eRegisterKindLLDB
, gpr_x0_arm64
+ t2
);
790 if (!reg_info_Rt
|| !reg_info_Rt2
)
793 bool success
= false;
795 // CheckSPAlignment();
797 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_sp_arm64
, 0, &success
);
800 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_x0_arm64
+ n
, 0, &success
);
802 wb_address
= address
+ idx
;
803 if (a_mode
!= AddrMode_POST
)
804 address
= wb_address
;
809 RegisterValue::BytesContainer buffer
;
814 if (n
== 31 || n
== GetFramePointerRegisterNumber()) // if this store is
815 // based off of the sp
818 context_t
.type
= eContextPushRegisterOnStack
;
819 context_t2
.type
= eContextPushRegisterOnStack
;
821 context_t
.type
= eContextRegisterStore
;
822 context_t2
.type
= eContextRegisterStore
;
824 context_t
.SetRegisterToRegisterPlusOffset(*reg_info_Rt
, *reg_info_base
, 0);
825 context_t2
.SetRegisterToRegisterPlusOffset(*reg_info_Rt2
, *reg_info_base
,
828 std::optional
<RegisterValue
> data_Rt
= ReadRegister(*reg_info_Rt
);
832 buffer
.resize(reg_info_Rt
->byte_size
);
833 if (data_Rt
->GetAsMemoryData(*reg_info_Rt
, buffer
.data(),
834 reg_info_Rt
->byte_size
, eByteOrderLittle
,
838 if (!WriteMemory(context_t
, address
+ 0, buffer
.data(),
839 reg_info_Rt
->byte_size
))
842 std::optional
<RegisterValue
> data_Rt2
= ReadRegister(*reg_info_Rt2
);
846 buffer
.resize(reg_info_Rt2
->byte_size
);
847 if (data_Rt2
->GetAsMemoryData(*reg_info_Rt2
, buffer
.data(),
848 reg_info_Rt2
->byte_size
, eByteOrderLittle
,
852 if (!WriteMemory(context_t2
, address
+ size
, buffer
.data(),
853 reg_info_Rt2
->byte_size
))
858 if (n
== 31 || n
== GetFramePointerRegisterNumber()) // if this load is
859 // based off of the sp
862 context_t
.type
= eContextPopRegisterOffStack
;
863 context_t2
.type
= eContextPopRegisterOffStack
;
865 context_t
.type
= eContextRegisterLoad
;
866 context_t2
.type
= eContextRegisterLoad
;
868 context_t
.SetAddress(address
);
869 context_t2
.SetAddress(address
+ size
);
871 buffer
.resize(reg_info_Rt
->byte_size
);
873 std::fill(buffer
.begin(), buffer
.end(), 'U');
875 if (!ReadMemory(context_t
, address
, buffer
.data(),
876 reg_info_Rt
->byte_size
))
880 RegisterValue data_Rt
;
881 if (data_Rt
.SetFromMemoryData(*reg_info_Rt
, buffer
.data(),
882 reg_info_Rt
->byte_size
, eByteOrderLittle
,
886 if (!vector
&& is_signed
&& !data_Rt
.SignExtend(datasize
))
889 if (!WriteRegister(context_t
, *reg_info_Rt
, data_Rt
))
892 buffer
.resize(reg_info_Rt2
->byte_size
);
894 if (!ReadMemory(context_t2
, address
+ size
, buffer
.data(),
895 reg_info_Rt2
->byte_size
))
898 RegisterValue data_Rt2
;
899 if (data_Rt2
.SetFromMemoryData(*reg_info_Rt2
, buffer
.data(),
900 reg_info_Rt2
->byte_size
, eByteOrderLittle
,
904 if (!vector
&& is_signed
&& !data_Rt2
.SignExtend(datasize
))
907 if (!WriteRegister(context_t2
, *reg_info_Rt2
, data_Rt2
))
917 wb_address
= LLDB_INVALID_ADDRESS
;
919 context
.SetImmediateSigned(idx
);
921 context
.type
= eContextAdjustStackPointer
;
923 context
.type
= eContextAdjustBaseRegister
;
924 WriteRegisterUnsigned(context
, *reg_info_base
, wb_address
);
929 template <EmulateInstructionARM64::AddrMode a_mode
>
930 bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode
) {
931 uint32_t size
= Bits32(opcode
, 31, 30);
932 uint32_t opc
= Bits32(opcode
, 23, 22);
933 uint32_t n
= Bits32(opcode
, 9, 5);
934 uint32_t t
= Bits32(opcode
, 4, 0);
944 offset
= llvm::SignExtend64
<9>(Bits32(opcode
, 20, 12));
949 offset
= llvm::SignExtend64
<9>(Bits32(opcode
, 20, 12));
954 offset
= LSL(Bits32(opcode
, 21, 10), size
);
960 if (Bit32(opc
, 1) == 0) {
961 memop
= Bit32(opc
, 0) == 1 ? MemOp_LOAD
: MemOp_STORE
;
964 if (size
== 2 && Bit32(opc
, 0) == 1)
969 bool success
= false;
971 RegisterValue::BytesContainer buffer
;
975 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_sp_arm64
, 0, &success
);
978 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_x0_arm64
+ n
, 0, &success
);
986 std::optional
<RegisterInfo
> reg_info_base
=
987 GetRegisterInfo(eRegisterKindLLDB
, gpr_x0_arm64
+ n
);
991 std::optional
<RegisterInfo
> reg_info_Rt
=
992 GetRegisterInfo(eRegisterKindLLDB
, gpr_x0_arm64
+ t
);
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 std::optional
<RegisterValue
> data_Rt
= ReadRegister(*reg_info_Rt
);
1012 buffer
.resize(reg_info_Rt
->byte_size
);
1013 if (data_Rt
->GetAsMemoryData(*reg_info_Rt
, buffer
.data(),
1014 reg_info_Rt
->byte_size
, eByteOrderLittle
,
1018 if (!WriteMemory(context
, address
, buffer
.data(), reg_info_Rt
->byte_size
))
1023 if (n
== 31 || n
== GetFramePointerRegisterNumber()) // if this store is
1024 // based off of the sp
1026 context
.type
= eContextPopRegisterOffStack
;
1028 context
.type
= eContextRegisterLoad
;
1029 context
.SetAddress(address
);
1031 buffer
.resize(reg_info_Rt
->byte_size
);
1032 if (!ReadMemory(context
, address
, buffer
.data(), reg_info_Rt
->byte_size
))
1035 RegisterValue data_Rt
;
1036 if (data_Rt
.SetFromMemoryData(*reg_info_Rt
, buffer
.data(),
1037 reg_info_Rt
->byte_size
, eByteOrderLittle
,
1041 if (!WriteRegister(context
, *reg_info_Rt
, data_Rt
))
1053 context
.type
= eContextAdjustStackPointer
;
1055 context
.type
= eContextAdjustBaseRegister
;
1056 context
.SetImmediateSigned(offset
);
1058 if (!WriteRegisterUnsigned(context
, *reg_info_base
, address
))
1064 bool EmulateInstructionARM64::EmulateB(const uint32_t opcode
) {
1066 // ARM64 pseudo code...
1067 if branch_type
== BranchType_CALL then X
[30] = PC
[] + 4;
1068 BranchTo(PC
[] + offset
, branch_type
);
1071 bool success
= false;
1073 EmulateInstruction::Context context
;
1074 context
.type
= EmulateInstruction::eContextRelativeBranchImmediate
;
1075 const uint64_t pc
= ReadRegisterUnsigned(eRegisterKindGeneric
,
1076 LLDB_REGNUM_GENERIC_PC
, 0, &success
);
1080 int64_t offset
= llvm::SignExtend64
<28>(Bits32(opcode
, 25, 0) << 2);
1081 BranchType branch_type
= Bit32(opcode
, 31) ? BranchType_CALL
: BranchType_JMP
;
1082 addr_t target
= pc
+ offset
;
1083 context
.SetImmediateSigned(offset
);
1085 switch (branch_type
) {
1086 case BranchType_CALL
: {
1087 addr_t x30
= pc
+ 4;
1088 if (!WriteRegisterUnsigned(context
, eRegisterKindLLDB
, gpr_lr_arm64
, x30
))
1091 case BranchType_JMP
:
1097 return BranchTo(context
, 64, target
);
1100 bool EmulateInstructionARM64::EmulateBcond(const uint32_t opcode
) {
1102 // ARM64 pseudo code...
1103 bits(64) offset
= SignExtend(imm19
:'00', 64);
1104 bits(4) condition
= cond
;
1105 if ConditionHolds(condition
) then
1106 BranchTo(PC
[] + offset
, BranchType_JMP
);
1109 if (ConditionHolds(Bits32(opcode
, 3, 0))) {
1110 bool success
= false;
1112 const uint64_t pc
= ReadRegisterUnsigned(
1113 eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_PC
, 0, &success
);
1117 int64_t offset
= llvm::SignExtend64
<21>(Bits32(opcode
, 23, 5) << 2);
1118 addr_t target
= pc
+ offset
;
1120 EmulateInstruction::Context context
;
1121 context
.type
= EmulateInstruction::eContextRelativeBranchImmediate
;
1122 context
.SetImmediateSigned(offset
);
1123 if (!BranchTo(context
, 64, target
))
1129 bool EmulateInstructionARM64::EmulateCBZ(const uint32_t opcode
) {
1131 integer t
= UInt(Rt
);
1132 integer datasize
= if sf
== '1' then
64 else 32;
1133 boolean iszero
= (op
== '0');
1134 bits(64) offset
= SignExtend(imm19
:'00', 64);
1136 bits(datasize
) operand1
= X
[t
];
1137 if IsZero(operand1
) == iszero then
1138 BranchTo(PC
[] + offset
, BranchType_JMP
);
1141 bool success
= false;
1143 uint32_t t
= Bits32(opcode
, 4, 0);
1144 bool is_zero
= Bit32(opcode
, 24) == 0;
1145 int32_t offset
= llvm::SignExtend64
<21>(Bits32(opcode
, 23, 5) << 2);
1147 const uint64_t operand
=
1148 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_x0_arm64
+ t
, 0, &success
);
1152 if (m_ignore_conditions
|| ((operand
== 0) == is_zero
)) {
1153 const uint64_t pc
= ReadRegisterUnsigned(
1154 eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_PC
, 0, &success
);
1158 EmulateInstruction::Context context
;
1159 context
.type
= EmulateInstruction::eContextRelativeBranchImmediate
;
1160 context
.SetImmediateSigned(offset
);
1161 if (!BranchTo(context
, 64, pc
+ offset
))
1167 bool EmulateInstructionARM64::EmulateTBZ(const uint32_t opcode
) {
1169 integer t
= UInt(Rt
);
1170 integer datasize
= if b5
== '1' then
64 else 32;
1171 integer bit_pos
= UInt(b5
:b40
);
1173 bits(64) offset
= SignExtend(imm14
:'00', 64);
1176 bool success
= false;
1178 uint32_t t
= Bits32(opcode
, 4, 0);
1179 uint32_t bit_pos
= (Bit32(opcode
, 31) << 6) | (Bits32(opcode
, 23, 19));
1180 uint32_t bit_val
= Bit32(opcode
, 24);
1181 int64_t offset
= llvm::SignExtend64
<16>(Bits32(opcode
, 18, 5) << 2);
1183 const uint64_t operand
=
1184 ReadRegisterUnsigned(eRegisterKindLLDB
, gpr_x0_arm64
+ t
, 0, &success
);
1188 if (m_ignore_conditions
|| Bit32(operand
, bit_pos
) == bit_val
) {
1189 const uint64_t pc
= ReadRegisterUnsigned(
1190 eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_PC
, 0, &success
);
1194 EmulateInstruction::Context context
;
1195 context
.type
= EmulateInstruction::eContextRelativeBranchImmediate
;
1196 context
.SetImmediateSigned(offset
);
1197 if (!BranchTo(context
, 64, pc
+ offset
))