blob: 5b86df6c5273261667fcbe6c9c19951751f35850 [file] [log] [blame]
Pavel Labath13e37d42017-10-25 21:05:31 +00001//===-- ArchitectureArm.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Pavel Labath13e37d42017-10-25 21:05:31 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Plugins/Architecture/Arm/ArchitectureArm.h"
10#include "Plugins/Process/Utility/ARMDefines.h"
11#include "Plugins/Process/Utility/InstructionUtils.h"
Pavel Labath13e37d42017-10-25 21:05:31 +000012#include "lldb/Core/PluginManager.h"
13#include "lldb/Target/RegisterContext.h"
14#include "lldb/Target/Thread.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000015#include "lldb/Utility/ArchSpec.h"
Pavel Labath13e37d42017-10-25 21:05:31 +000016
17using namespace lldb_private;
18using namespace lldb;
19
20ConstString ArchitectureArm::GetPluginNameStatic() {
21 return ConstString("arm");
22}
23
24void ArchitectureArm::Initialize() {
25 PluginManager::RegisterPlugin(GetPluginNameStatic(),
26 "Arm-specific algorithms",
27 &ArchitectureArm::Create);
28}
29
30void ArchitectureArm::Terminate() {
31 PluginManager::UnregisterPlugin(&ArchitectureArm::Create);
32}
33
34std::unique_ptr<Architecture> ArchitectureArm::Create(const ArchSpec &arch) {
35 if (arch.GetMachine() != llvm::Triple::arm)
36 return nullptr;
37 return std::unique_ptr<Architecture>(new ArchitectureArm());
38}
39
40ConstString ArchitectureArm::GetPluginName() { return GetPluginNameStatic(); }
41uint32_t ArchitectureArm::GetPluginVersion() { return 1; }
42
Tatyana Krasnukha3da0f212018-06-27 07:01:07 +000043void ArchitectureArm::OverrideStopInfo(Thread &thread) const {
Adrian Prantl05097242018-04-30 16:49:04 +000044 // We need to check if we are stopped in Thumb mode in a IT instruction and
45 // detect if the condition doesn't pass. If this is the case it means we
46 // won't actually execute this instruction. If this happens we need to clear
47 // the stop reason to no thread plans think we are stopped for a reason and
48 // the plans should keep going.
Pavel Labath13e37d42017-10-25 21:05:31 +000049 //
50 // We do this because when single stepping many ARM processes, debuggers
Adrian Prantl05097242018-04-30 16:49:04 +000051 // often use the BVR/BCR registers that says "stop when the PC is not equal
52 // to its current value". This method of stepping means we can end up
53 // stopping on instructions inside an if/then block that wouldn't get
54 // executed. By fixing this we can stop the debugger from seeming like you
55 // stepped through both the "if" _and_ the "else" clause when source level
56 // stepping because the debugger stops regardless due to the BVR/BCR
Pavel Labath13e37d42017-10-25 21:05:31 +000057 // triggering a stop.
58 //
Adrian Prantl05097242018-04-30 16:49:04 +000059 // It also means we can set breakpoints on instructions inside an an if/then
60 // block and correctly skip them if we use the BKPT instruction. The ARM and
61 // Thumb BKPT instructions are unconditional even when executed in a Thumb IT
62 // block.
Pavel Labath13e37d42017-10-25 21:05:31 +000063 //
Adrian Prantl05097242018-04-30 16:49:04 +000064 // If your debugger inserts software traps in ARM/Thumb code, it will need to
65 // use 16 and 32 bit instruction for 16 and 32 bit thumb instructions
66 // respectively. If your debugger inserts a 16 bit thumb trap on top of a 32
67 // bit thumb instruction for an opcode that is inside an if/then, it will
68 // change the it/then to conditionally execute your
Pavel Labath13e37d42017-10-25 21:05:31 +000069 // 16 bit trap and then cause your program to crash if it executes the
70 // trailing 16 bits (the second half of the 32 bit thumb instruction you
71 // partially overwrote).
72
73 RegisterContextSP reg_ctx_sp(thread.GetRegisterContext());
74 if (!reg_ctx_sp)
75 return;
76
77 const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
78 if (cpsr == 0)
79 return;
80
81 // Read the J and T bits to get the ISETSTATE
82 const uint32_t J = Bit32(cpsr, 24);
83 const uint32_t T = Bit32(cpsr, 5);
84 const uint32_t ISETSTATE = J << 1 | T;
85 if (ISETSTATE == 0) {
86// NOTE: I am pretty sure we want to enable the code below
Adrian Prantl05097242018-04-30 16:49:04 +000087// that detects when we stop on an instruction in ARM mode that is conditional
88// and the condition doesn't pass. This can happen if you set a breakpoint on
89// an instruction that is conditional. We currently will _always_ stop on the
90// instruction which is bad. You can also run into this while single stepping
91// and you could appear to run code in the "if" and in the "else" clause
92// because it would stop at all of the conditional instructions in both. In
93// such cases, we really don't want to stop at this location.
Pavel Labath13e37d42017-10-25 21:05:31 +000094// I will check with the lldb-dev list first before I enable this.
95#if 0
Bruce Mitchener4ebdee02018-05-29 09:10:46 +000096 // ARM mode: check for condition on instruction
Pavel Labath13e37d42017-10-25 21:05:31 +000097 const addr_t pc = reg_ctx_sp->GetPC();
98 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +000099 // If we fail to read the opcode we will get UINT64_MAX as the result in
100 // "opcode" which we can use to detect if we read a valid opcode.
Pavel Labath13e37d42017-10-25 21:05:31 +0000101 const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
102 if (opcode <= UINT32_MAX)
103 {
104 const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
105 if (!ARMConditionPassed(condition, cpsr))
106 {
107 // We ARE stopped on an ARM instruction whose condition doesn't
Adrian Prantl05097242018-04-30 16:49:04 +0000108 // pass so this instruction won't get executed. Regardless of why
109 // it stopped, we need to clear the stop info
Pavel Labath13e37d42017-10-25 21:05:31 +0000110 thread.SetStopInfo (StopInfoSP());
111 }
112 }
113#endif
114 } else if (ISETSTATE == 1) {
115 // Thumb mode
116 const uint32_t ITSTATE = Bits32(cpsr, 15, 10) << 2 | Bits32(cpsr, 26, 25);
117 if (ITSTATE != 0) {
118 const uint32_t condition = Bits32(ITSTATE, 7, 4);
119 if (!ARMConditionPassed(condition, cpsr)) {
120 // We ARE stopped in a Thumb IT instruction on an instruction whose
121 // condition doesn't pass so this instruction won't get executed.
122 // Regardless of why it stopped, we need to clear the stop info
123 thread.SetStopInfo(StopInfoSP());
124 }
125 }
126 }
127}
Tatyana Krasnukha7aa9e7b2018-09-21 18:56:44 +0000128
129addr_t ArchitectureArm::GetCallableLoadAddress(addr_t code_addr,
130 AddressClass addr_class) const {
131 bool is_alternate_isa = false;
132
133 switch (addr_class) {
134 case AddressClass::eData:
135 case AddressClass::eDebug:
136 return LLDB_INVALID_ADDRESS;
137 case AddressClass::eCodeAlternateISA:
138 is_alternate_isa = true;
139 break;
140 default: break;
141 }
142
143 if ((code_addr & 2u) || is_alternate_isa)
144 return code_addr | 1u;
145 return code_addr;
146}
147
148addr_t ArchitectureArm::GetOpcodeLoadAddress(addr_t opcode_addr,
149 AddressClass addr_class) const {
150 switch (addr_class) {
151 case AddressClass::eData:
152 case AddressClass::eDebug:
153 return LLDB_INVALID_ADDRESS;
154 default: break;
155 }
156 return opcode_addr & ~(1ull);
Jonas Devlieghereceff6642018-11-11 23:17:06 +0000157}