1 //===-- DNBArchImpl.h -------------------------------------------*- C++ -*-===// 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 // 9 // Created by Greg Clayton on 6/25/07. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_ARM_DNBARCHIMPL_H 14 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_ARM_DNBARCHIMPL_H 15 16 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 17 18 #include "DNBArch.h" 19 20 #include <map> 21 22 class MachThread; 23 24 class DNBArchMachARM : public DNBArchProtocol { 25 public: 26 enum { kMaxNumThumbITBreakpoints = 4 }; 27 DNBArchMachARM(MachThread * thread)28 DNBArchMachARM(MachThread *thread) 29 : m_thread(thread), m_state(), m_disabled_watchpoints(), 30 m_hw_single_chained_step_addr(INVALID_NUB_ADDRESS), 31 m_last_decode_pc(INVALID_NUB_ADDRESS), m_watchpoint_hw_index(-1), 32 m_watchpoint_did_occur(false), 33 m_watchpoint_resume_single_step_enabled(false), 34 m_saved_register_states() { 35 m_disabled_watchpoints.resize(16); 36 memset(&m_dbg_save, 0, sizeof(m_dbg_save)); 37 #if defined(USE_ARM_DISASSEMBLER_FRAMEWORK) 38 ThumbStaticsInit(&m_last_decode_thumb); 39 #endif 40 } 41 ~DNBArchMachARM()42 virtual ~DNBArchMachARM() {} 43 44 static void Initialize(); 45 static const DNBRegisterSetInfo *GetRegisterSetInfo(nub_size_t *num_reg_sets); 46 47 virtual bool GetRegisterValue(uint32_t set, uint32_t reg, 48 DNBRegisterValue *value); 49 virtual bool SetRegisterValue(uint32_t set, uint32_t reg, 50 const DNBRegisterValue *value); 51 virtual nub_size_t GetRegisterContext(void *buf, nub_size_t buf_len); 52 virtual nub_size_t SetRegisterContext(const void *buf, nub_size_t buf_len); 53 virtual uint32_t SaveRegisterState(); 54 virtual bool RestoreRegisterState(uint32_t save_id); 55 56 virtual kern_return_t GetRegisterState(int set, bool force); 57 virtual kern_return_t SetRegisterState(int set); 58 virtual bool RegisterSetStateIsValid(int set) const; 59 60 virtual uint64_t GetPC(uint64_t failValue); // Get program counter 61 virtual kern_return_t SetPC(uint64_t value); 62 virtual uint64_t GetSP(uint64_t failValue); // Get stack pointer 63 virtual void ThreadWillResume(); 64 virtual bool ThreadDidStop(); 65 virtual bool NotifyException(MachException::Data &exc); 66 67 static DNBArchProtocol *Create(MachThread *thread); 68 static const uint8_t *SoftwareBreakpointOpcode(nub_size_t byte_size); 69 static uint32_t GetCPUType(); 70 71 virtual uint32_t NumSupportedHardwareBreakpoints(); 72 virtual uint32_t NumSupportedHardwareWatchpoints(); 73 virtual uint32_t EnableHardwareBreakpoint(nub_addr_t addr, nub_size_t size, 74 bool also_set_on_task); 75 virtual bool DisableHardwareBreakpoint(uint32_t hw_break_index, 76 bool also_set_on_task); 77 78 virtual uint32_t EnableHardwareWatchpoint(nub_addr_t addr, nub_size_t size, 79 bool read, bool write, 80 bool also_set_on_task); 81 virtual bool DisableHardwareWatchpoint(uint32_t hw_break_index, 82 bool also_set_on_task); 83 virtual bool DisableHardwareWatchpoint_helper(uint32_t hw_break_index, 84 bool also_set_on_task); 85 virtual bool ReenableHardwareWatchpoint(uint32_t hw_break_index); 86 virtual bool ReenableHardwareWatchpoint_helper(uint32_t hw_break_index); 87 88 virtual bool StepNotComplete(); 89 virtual uint32_t GetHardwareWatchpointHit(nub_addr_t &addr); 90 91 #if defined(ARM_DEBUG_STATE32) && (defined(__arm64__) || defined(__aarch64__)) 92 typedef arm_debug_state32_t DBG; 93 #else 94 typedef arm_debug_state_t DBG; 95 #endif 96 97 protected: 98 kern_return_t EnableHardwareSingleStep(bool enable); 99 kern_return_t SetSingleStepSoftwareBreakpoints(); 100 101 bool ConditionPassed(uint8_t condition, uint32_t cpsr); 102 #if defined(USE_ARM_DISASSEMBLER_FRAMEWORK) 103 bool ComputeNextPC(nub_addr_t currentPC, 104 arm_decoded_instruction_t decodedInstruction, 105 bool currentPCIsThumb, nub_addr_t *targetPC); 106 arm_error_t DecodeInstructionUsingDisassembler( 107 nub_addr_t curr_pc, uint32_t curr_cpsr, 108 arm_decoded_instruction_t *decodedInstruction, 109 thumb_static_data_t *thumbStaticData, nub_addr_t *next_pc); 110 void DecodeITBlockInstructions(nub_addr_t curr_pc); 111 #endif 112 void EvaluateNextInstructionForSoftwareBreakpointSetup(nub_addr_t currentPC, 113 uint32_t cpsr, 114 bool currentPCIsThumb, 115 nub_addr_t *nextPC, 116 bool *nextPCIsThumb); 117 118 enum RegisterSet { 119 e_regSetALL = REGISTER_SET_ALL, 120 e_regSetGPR, // ARM_THREAD_STATE 121 e_regSetVFP, // ARM_VFP_STATE (ARM_NEON_STATE if defined __arm64__) 122 e_regSetEXC, // ARM_EXCEPTION_STATE 123 e_regSetDBG, // ARM_DEBUG_STATE (ARM_DEBUG_STATE32 if defined __arm64__) 124 kNumRegisterSets 125 }; 126 127 enum { Read = 0, Write = 1, kNumErrors = 2 }; 128 129 typedef arm_thread_state_t GPR; 130 #if defined(__arm64__) || defined(__aarch64__) 131 typedef arm_neon_state_t FPU; 132 #else 133 typedef arm_vfp_state_t FPU; 134 #endif 135 typedef arm_exception_state_t EXC; 136 137 static const DNBRegisterInfo g_gpr_registers[]; 138 static const DNBRegisterInfo g_vfp_registers[]; 139 static const DNBRegisterInfo g_exc_registers[]; 140 static const DNBRegisterSetInfo g_reg_sets[]; 141 142 static const size_t k_num_gpr_registers; 143 static const size_t k_num_vfp_registers; 144 static const size_t k_num_exc_registers; 145 static const size_t k_num_all_registers; 146 static const size_t k_num_register_sets; 147 148 struct Context { 149 GPR gpr; 150 FPU vfp; 151 EXC exc; 152 }; 153 154 struct State { 155 Context context; 156 DBG dbg; 157 kern_return_t gpr_errs[2]; // Read/Write errors 158 kern_return_t vfp_errs[2]; // Read/Write errors 159 kern_return_t exc_errs[2]; // Read/Write errors 160 kern_return_t dbg_errs[2]; // Read/Write errors StateState161 State() { 162 uint32_t i; 163 for (i = 0; i < kNumErrors; i++) { 164 gpr_errs[i] = -1; 165 vfp_errs[i] = -1; 166 exc_errs[i] = -1; 167 dbg_errs[i] = -1; 168 } 169 } InvalidateRegisterSetStateState170 void InvalidateRegisterSetState(int set) { SetError(set, Read, -1); } GetErrorState171 kern_return_t GetError(int set, uint32_t err_idx) const { 172 if (err_idx < kNumErrors) { 173 switch (set) { 174 // When getting all errors, just OR all values together to see if 175 // we got any kind of error. 176 case e_regSetALL: 177 return gpr_errs[err_idx] | vfp_errs[err_idx] | exc_errs[err_idx] | 178 dbg_errs[err_idx]; 179 case e_regSetGPR: 180 return gpr_errs[err_idx]; 181 case e_regSetVFP: 182 return vfp_errs[err_idx]; 183 case e_regSetEXC: 184 return exc_errs[err_idx]; 185 case e_regSetDBG: 186 return dbg_errs[err_idx]; 187 default: 188 break; 189 } 190 } 191 return -1; 192 } SetErrorState193 bool SetError(int set, uint32_t err_idx, kern_return_t err) { 194 if (err_idx < kNumErrors) { 195 switch (set) { 196 case e_regSetALL: 197 gpr_errs[err_idx] = err; 198 vfp_errs[err_idx] = err; 199 dbg_errs[err_idx] = err; 200 exc_errs[err_idx] = err; 201 return true; 202 203 case e_regSetGPR: 204 gpr_errs[err_idx] = err; 205 return true; 206 207 case e_regSetVFP: 208 vfp_errs[err_idx] = err; 209 return true; 210 211 case e_regSetEXC: 212 exc_errs[err_idx] = err; 213 return true; 214 215 case e_regSetDBG: 216 dbg_errs[err_idx] = err; 217 return true; 218 default: 219 break; 220 } 221 } 222 return false; 223 } RegsAreValidState224 bool RegsAreValid(int set) const { 225 return GetError(set, Read) == KERN_SUCCESS; 226 } 227 }; 228 229 kern_return_t GetGPRState(bool force); 230 kern_return_t GetVFPState(bool force); 231 kern_return_t GetEXCState(bool force); 232 kern_return_t GetDBGState(bool force); 233 234 kern_return_t SetGPRState(); 235 kern_return_t SetVFPState(); 236 kern_return_t SetEXCState(); 237 kern_return_t SetDBGState(bool also_set_on_task); 238 239 bool IsWatchpointEnabled(const DBG &debug_state, uint32_t hw_index); 240 nub_addr_t GetWatchpointAddressByIndex(uint32_t hw_index); 241 nub_addr_t GetWatchAddress(const DBG &debug_state, uint32_t hw_index); 242 243 class disabled_watchpoint { 244 public: disabled_watchpoint()245 disabled_watchpoint() { 246 addr = 0; 247 control = 0; 248 } 249 nub_addr_t addr; 250 uint32_t control; 251 }; 252 253 protected: 254 MachThread *m_thread; 255 State m_state; 256 DBG m_dbg_save; 257 258 // armv8 doesn't keep the disabled watchpoint values in the debug register 259 // context like armv7; 260 // we need to save them aside when we disable them temporarily. 261 std::vector<disabled_watchpoint> m_disabled_watchpoints; 262 263 nub_addr_t m_hw_single_chained_step_addr; 264 nub_addr_t m_last_decode_pc; 265 266 // The following member variables should be updated atomically. 267 int32_t m_watchpoint_hw_index; 268 bool m_watchpoint_did_occur; 269 bool m_watchpoint_resume_single_step_enabled; 270 271 typedef std::map<uint32_t, Context> SaveRegisterStates; 272 SaveRegisterStates m_saved_register_states; 273 }; 274 275 #endif // #if defined (__arm__) 276 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_ARM_DNBARCHIMPL_H 277