180814287SRaphael Isemann //===-- ArchitectureArm.cpp -----------------------------------------------===//
213e37d4dSPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
613e37d4dSPavel Labath //
713e37d4dSPavel Labath //===----------------------------------------------------------------------===//
813e37d4dSPavel Labath
913e37d4dSPavel Labath #include "Plugins/Architecture/Arm/ArchitectureArm.h"
1013e37d4dSPavel Labath #include "Plugins/Process/Utility/ARMDefines.h"
1113e37d4dSPavel Labath #include "Plugins/Process/Utility/InstructionUtils.h"
1213e37d4dSPavel Labath #include "lldb/Core/PluginManager.h"
1313e37d4dSPavel Labath #include "lldb/Target/RegisterContext.h"
1413e37d4dSPavel Labath #include "lldb/Target/Thread.h"
155f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
1613e37d4dSPavel Labath
1713e37d4dSPavel Labath using namespace lldb_private;
1813e37d4dSPavel Labath using namespace lldb;
1913e37d4dSPavel Labath
LLDB_PLUGIN_DEFINE(ArchitectureArm)20bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ArchitectureArm)
21fbb4d1e4SJonas Devlieghere
2213e37d4dSPavel Labath void ArchitectureArm::Initialize() {
2313e37d4dSPavel Labath PluginManager::RegisterPlugin(GetPluginNameStatic(),
2413e37d4dSPavel Labath "Arm-specific algorithms",
2513e37d4dSPavel Labath &ArchitectureArm::Create);
2613e37d4dSPavel Labath }
2713e37d4dSPavel Labath
Terminate()2813e37d4dSPavel Labath void ArchitectureArm::Terminate() {
2913e37d4dSPavel Labath PluginManager::UnregisterPlugin(&ArchitectureArm::Create);
3013e37d4dSPavel Labath }
3113e37d4dSPavel Labath
Create(const ArchSpec & arch)3213e37d4dSPavel Labath std::unique_ptr<Architecture> ArchitectureArm::Create(const ArchSpec &arch) {
3313e37d4dSPavel Labath if (arch.GetMachine() != llvm::Triple::arm)
3413e37d4dSPavel Labath return nullptr;
3513e37d4dSPavel Labath return std::unique_ptr<Architecture>(new ArchitectureArm());
3613e37d4dSPavel Labath }
3713e37d4dSPavel Labath
OverrideStopInfo(Thread & thread) const383da0f218STatyana Krasnukha void ArchitectureArm::OverrideStopInfo(Thread &thread) const {
3905097246SAdrian Prantl // We need to check if we are stopped in Thumb mode in a IT instruction and
4005097246SAdrian Prantl // detect if the condition doesn't pass. If this is the case it means we
4105097246SAdrian Prantl // won't actually execute this instruction. If this happens we need to clear
4205097246SAdrian Prantl // the stop reason to no thread plans think we are stopped for a reason and
4305097246SAdrian Prantl // the plans should keep going.
4413e37d4dSPavel Labath //
4513e37d4dSPavel Labath // We do this because when single stepping many ARM processes, debuggers
4605097246SAdrian Prantl // often use the BVR/BCR registers that says "stop when the PC is not equal
4705097246SAdrian Prantl // to its current value". This method of stepping means we can end up
4805097246SAdrian Prantl // stopping on instructions inside an if/then block that wouldn't get
4905097246SAdrian Prantl // executed. By fixing this we can stop the debugger from seeming like you
5005097246SAdrian Prantl // stepped through both the "if" _and_ the "else" clause when source level
5105097246SAdrian Prantl // stepping because the debugger stops regardless due to the BVR/BCR
5213e37d4dSPavel Labath // triggering a stop.
5313e37d4dSPavel Labath //
54*678e3ee1SFangrui Song // It also means we can set breakpoints on instructions inside an if/then
5505097246SAdrian Prantl // block and correctly skip them if we use the BKPT instruction. The ARM and
5605097246SAdrian Prantl // Thumb BKPT instructions are unconditional even when executed in a Thumb IT
5705097246SAdrian Prantl // block.
5813e37d4dSPavel Labath //
5905097246SAdrian Prantl // If your debugger inserts software traps in ARM/Thumb code, it will need to
6005097246SAdrian Prantl // use 16 and 32 bit instruction for 16 and 32 bit thumb instructions
6105097246SAdrian Prantl // respectively. If your debugger inserts a 16 bit thumb trap on top of a 32
6205097246SAdrian Prantl // bit thumb instruction for an opcode that is inside an if/then, it will
6305097246SAdrian Prantl // change the it/then to conditionally execute your
6413e37d4dSPavel Labath // 16 bit trap and then cause your program to crash if it executes the
6513e37d4dSPavel Labath // trailing 16 bits (the second half of the 32 bit thumb instruction you
6613e37d4dSPavel Labath // partially overwrote).
6713e37d4dSPavel Labath
6813e37d4dSPavel Labath RegisterContextSP reg_ctx_sp(thread.GetRegisterContext());
6913e37d4dSPavel Labath if (!reg_ctx_sp)
7013e37d4dSPavel Labath return;
7113e37d4dSPavel Labath
7213e37d4dSPavel Labath const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
7313e37d4dSPavel Labath if (cpsr == 0)
7413e37d4dSPavel Labath return;
7513e37d4dSPavel Labath
7613e37d4dSPavel Labath // Read the J and T bits to get the ISETSTATE
7713e37d4dSPavel Labath const uint32_t J = Bit32(cpsr, 24);
7813e37d4dSPavel Labath const uint32_t T = Bit32(cpsr, 5);
7913e37d4dSPavel Labath const uint32_t ISETSTATE = J << 1 | T;
8013e37d4dSPavel Labath if (ISETSTATE == 0) {
8113e37d4dSPavel Labath // NOTE: I am pretty sure we want to enable the code below
8205097246SAdrian Prantl // that detects when we stop on an instruction in ARM mode that is conditional
8305097246SAdrian Prantl // and the condition doesn't pass. This can happen if you set a breakpoint on
8405097246SAdrian Prantl // an instruction that is conditional. We currently will _always_ stop on the
8505097246SAdrian Prantl // instruction which is bad. You can also run into this while single stepping
8605097246SAdrian Prantl // and you could appear to run code in the "if" and in the "else" clause
8705097246SAdrian Prantl // because it would stop at all of the conditional instructions in both. In
8805097246SAdrian Prantl // such cases, we really don't want to stop at this location.
8913e37d4dSPavel Labath // I will check with the lldb-dev list first before I enable this.
9013e37d4dSPavel Labath #if 0
914ebdee0aSBruce Mitchener // ARM mode: check for condition on instruction
9213e37d4dSPavel Labath const addr_t pc = reg_ctx_sp->GetPC();
9313e37d4dSPavel Labath Status error;
9405097246SAdrian Prantl // If we fail to read the opcode we will get UINT64_MAX as the result in
9505097246SAdrian Prantl // "opcode" which we can use to detect if we read a valid opcode.
9613e37d4dSPavel Labath const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
9713e37d4dSPavel Labath if (opcode <= UINT32_MAX)
9813e37d4dSPavel Labath {
9913e37d4dSPavel Labath const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
10013e37d4dSPavel Labath if (!ARMConditionPassed(condition, cpsr))
10113e37d4dSPavel Labath {
10213e37d4dSPavel Labath // We ARE stopped on an ARM instruction whose condition doesn't
10305097246SAdrian Prantl // pass so this instruction won't get executed. Regardless of why
10405097246SAdrian Prantl // it stopped, we need to clear the stop info
10513e37d4dSPavel Labath thread.SetStopInfo (StopInfoSP());
10613e37d4dSPavel Labath }
10713e37d4dSPavel Labath }
10813e37d4dSPavel Labath #endif
10913e37d4dSPavel Labath } else if (ISETSTATE == 1) {
11013e37d4dSPavel Labath // Thumb mode
11113e37d4dSPavel Labath const uint32_t ITSTATE = Bits32(cpsr, 15, 10) << 2 | Bits32(cpsr, 26, 25);
11213e37d4dSPavel Labath if (ITSTATE != 0) {
11313e37d4dSPavel Labath const uint32_t condition = Bits32(ITSTATE, 7, 4);
11413e37d4dSPavel Labath if (!ARMConditionPassed(condition, cpsr)) {
11513e37d4dSPavel Labath // We ARE stopped in a Thumb IT instruction on an instruction whose
11613e37d4dSPavel Labath // condition doesn't pass so this instruction won't get executed.
11713e37d4dSPavel Labath // Regardless of why it stopped, we need to clear the stop info
11813e37d4dSPavel Labath thread.SetStopInfo(StopInfoSP());
11913e37d4dSPavel Labath }
12013e37d4dSPavel Labath }
12113e37d4dSPavel Labath }
12213e37d4dSPavel Labath }
1237aa9e7bcSTatyana Krasnukha
GetCallableLoadAddress(addr_t code_addr,AddressClass addr_class) const1247aa9e7bcSTatyana Krasnukha addr_t ArchitectureArm::GetCallableLoadAddress(addr_t code_addr,
1257aa9e7bcSTatyana Krasnukha AddressClass addr_class) const {
1267aa9e7bcSTatyana Krasnukha bool is_alternate_isa = false;
1277aa9e7bcSTatyana Krasnukha
1287aa9e7bcSTatyana Krasnukha switch (addr_class) {
1297aa9e7bcSTatyana Krasnukha case AddressClass::eData:
1307aa9e7bcSTatyana Krasnukha case AddressClass::eDebug:
1317aa9e7bcSTatyana Krasnukha return LLDB_INVALID_ADDRESS;
1327aa9e7bcSTatyana Krasnukha case AddressClass::eCodeAlternateISA:
1337aa9e7bcSTatyana Krasnukha is_alternate_isa = true;
1347aa9e7bcSTatyana Krasnukha break;
1357aa9e7bcSTatyana Krasnukha default: break;
1367aa9e7bcSTatyana Krasnukha }
1377aa9e7bcSTatyana Krasnukha
1387aa9e7bcSTatyana Krasnukha if ((code_addr & 2u) || is_alternate_isa)
1397aa9e7bcSTatyana Krasnukha return code_addr | 1u;
1407aa9e7bcSTatyana Krasnukha return code_addr;
1417aa9e7bcSTatyana Krasnukha }
1427aa9e7bcSTatyana Krasnukha
GetOpcodeLoadAddress(addr_t opcode_addr,AddressClass addr_class) const1437aa9e7bcSTatyana Krasnukha addr_t ArchitectureArm::GetOpcodeLoadAddress(addr_t opcode_addr,
1447aa9e7bcSTatyana Krasnukha AddressClass addr_class) const {
1457aa9e7bcSTatyana Krasnukha switch (addr_class) {
1467aa9e7bcSTatyana Krasnukha case AddressClass::eData:
1477aa9e7bcSTatyana Krasnukha case AddressClass::eDebug:
1487aa9e7bcSTatyana Krasnukha return LLDB_INVALID_ADDRESS;
1497aa9e7bcSTatyana Krasnukha default: break;
1507aa9e7bcSTatyana Krasnukha }
1517aa9e7bcSTatyana Krasnukha return opcode_addr & ~(1ull);
1527aa9e7bcSTatyana Krasnukha }
153