1 //===-- NativeRegisterContextLinux_arm.cpp --------------------*- 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 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 10 11 #include "NativeRegisterContextLinux_arm.h" 12 13 #include "Plugins/Process/Linux/NativeProcessLinux.h" 14 #include "Plugins/Process/Linux/Procfs.h" 15 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 16 #include "Plugins/Process/Utility/RegisterInfoPOSIX_arm.h" 17 #include "lldb/Utility/DataBufferHeap.h" 18 #include "lldb/Utility/Log.h" 19 #include "lldb/Utility/RegisterValue.h" 20 #include "lldb/Utility/Status.h" 21 22 #include <elf.h> 23 #include <sys/socket.h> 24 25 #define REG_CONTEXT_SIZE (GetGPRSize() + sizeof(m_fpr)) 26 27 #ifndef PTRACE_GETVFPREGS 28 #define PTRACE_GETVFPREGS 27 29 #define PTRACE_SETVFPREGS 28 30 #endif 31 #ifndef PTRACE_GETHBPREGS 32 #define PTRACE_GETHBPREGS 29 33 #define PTRACE_SETHBPREGS 30 34 #endif 35 #if !defined(PTRACE_TYPE_ARG3) 36 #define PTRACE_TYPE_ARG3 void * 37 #endif 38 #if !defined(PTRACE_TYPE_ARG4) 39 #define PTRACE_TYPE_ARG4 void * 40 #endif 41 42 using namespace lldb; 43 using namespace lldb_private; 44 using namespace lldb_private::process_linux; 45 46 // arm general purpose registers. 47 static const uint32_t g_gpr_regnums_arm[] = { 48 gpr_r0_arm, gpr_r1_arm, gpr_r2_arm, gpr_r3_arm, gpr_r4_arm, 49 gpr_r5_arm, gpr_r6_arm, gpr_r7_arm, gpr_r8_arm, gpr_r9_arm, 50 gpr_r10_arm, gpr_r11_arm, gpr_r12_arm, gpr_sp_arm, gpr_lr_arm, 51 gpr_pc_arm, gpr_cpsr_arm, 52 LLDB_INVALID_REGNUM // register sets need to end with this flag 53 }; 54 static_assert(((sizeof g_gpr_regnums_arm / sizeof g_gpr_regnums_arm[0]) - 1) == 55 k_num_gpr_registers_arm, 56 "g_gpr_regnums_arm has wrong number of register infos"); 57 58 // arm floating point registers. 59 static const uint32_t g_fpu_regnums_arm[] = { 60 fpu_s0_arm, fpu_s1_arm, fpu_s2_arm, fpu_s3_arm, fpu_s4_arm, 61 fpu_s5_arm, fpu_s6_arm, fpu_s7_arm, fpu_s8_arm, fpu_s9_arm, 62 fpu_s10_arm, fpu_s11_arm, fpu_s12_arm, fpu_s13_arm, fpu_s14_arm, 63 fpu_s15_arm, fpu_s16_arm, fpu_s17_arm, fpu_s18_arm, fpu_s19_arm, 64 fpu_s20_arm, fpu_s21_arm, fpu_s22_arm, fpu_s23_arm, fpu_s24_arm, 65 fpu_s25_arm, fpu_s26_arm, fpu_s27_arm, fpu_s28_arm, fpu_s29_arm, 66 fpu_s30_arm, fpu_s31_arm, fpu_fpscr_arm, fpu_d0_arm, fpu_d1_arm, 67 fpu_d2_arm, fpu_d3_arm, fpu_d4_arm, fpu_d5_arm, fpu_d6_arm, 68 fpu_d7_arm, fpu_d8_arm, fpu_d9_arm, fpu_d10_arm, fpu_d11_arm, 69 fpu_d12_arm, fpu_d13_arm, fpu_d14_arm, fpu_d15_arm, fpu_d16_arm, 70 fpu_d17_arm, fpu_d18_arm, fpu_d19_arm, fpu_d20_arm, fpu_d21_arm, 71 fpu_d22_arm, fpu_d23_arm, fpu_d24_arm, fpu_d25_arm, fpu_d26_arm, 72 fpu_d27_arm, fpu_d28_arm, fpu_d29_arm, fpu_d30_arm, fpu_d31_arm, 73 fpu_q0_arm, fpu_q1_arm, fpu_q2_arm, fpu_q3_arm, fpu_q4_arm, 74 fpu_q5_arm, fpu_q6_arm, fpu_q7_arm, fpu_q8_arm, fpu_q9_arm, 75 fpu_q10_arm, fpu_q11_arm, fpu_q12_arm, fpu_q13_arm, fpu_q14_arm, 76 fpu_q15_arm, 77 LLDB_INVALID_REGNUM // register sets need to end with this flag 78 }; 79 static_assert(((sizeof g_fpu_regnums_arm / sizeof g_fpu_regnums_arm[0]) - 1) == 80 k_num_fpr_registers_arm, 81 "g_fpu_regnums_arm has wrong number of register infos"); 82 83 namespace { 84 // Number of register sets provided by this context. 85 enum { k_num_register_sets = 2 }; 86 } 87 88 // Register sets for arm. 89 static const RegisterSet g_reg_sets_arm[k_num_register_sets] = { 90 {"General Purpose Registers", "gpr", k_num_gpr_registers_arm, 91 g_gpr_regnums_arm}, 92 {"Floating Point Registers", "fpu", k_num_fpr_registers_arm, 93 g_fpu_regnums_arm}}; 94 95 #if defined(__arm__) 96 97 std::unique_ptr<NativeRegisterContextLinux> 98 NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux( 99 const ArchSpec &target_arch, NativeThreadProtocol &native_thread) { 100 return llvm::make_unique<NativeRegisterContextLinux_arm>(target_arch, 101 native_thread); 102 } 103 104 #endif // defined(__arm__) 105 106 NativeRegisterContextLinux_arm::NativeRegisterContextLinux_arm( 107 const ArchSpec &target_arch, NativeThreadProtocol &native_thread) 108 : NativeRegisterContextLinux(native_thread, 109 new RegisterInfoPOSIX_arm(target_arch)) { 110 switch (target_arch.GetMachine()) { 111 case llvm::Triple::arm: 112 m_reg_info.num_registers = k_num_registers_arm; 113 m_reg_info.num_gpr_registers = k_num_gpr_registers_arm; 114 m_reg_info.num_fpr_registers = k_num_fpr_registers_arm; 115 m_reg_info.last_gpr = k_last_gpr_arm; 116 m_reg_info.first_fpr = k_first_fpr_arm; 117 m_reg_info.last_fpr = k_last_fpr_arm; 118 m_reg_info.first_fpr_v = fpu_s0_arm; 119 m_reg_info.last_fpr_v = fpu_s31_arm; 120 m_reg_info.gpr_flags = gpr_cpsr_arm; 121 break; 122 default: 123 assert(false && "Unhandled target architecture."); 124 break; 125 } 126 127 ::memset(&m_fpr, 0, sizeof(m_fpr)); 128 ::memset(&m_gpr_arm, 0, sizeof(m_gpr_arm)); 129 ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs)); 130 ::memset(&m_hbr_regs, 0, sizeof(m_hbr_regs)); 131 132 // 16 is just a maximum value, query hardware for actual watchpoint count 133 m_max_hwp_supported = 16; 134 m_max_hbp_supported = 16; 135 m_refresh_hwdebug_info = true; 136 } 137 138 uint32_t NativeRegisterContextLinux_arm::GetRegisterSetCount() const { 139 return k_num_register_sets; 140 } 141 142 uint32_t NativeRegisterContextLinux_arm::GetUserRegisterCount() const { 143 uint32_t count = 0; 144 for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) 145 count += g_reg_sets_arm[set_index].num_registers; 146 return count; 147 } 148 149 const RegisterSet * 150 NativeRegisterContextLinux_arm::GetRegisterSet(uint32_t set_index) const { 151 if (set_index < k_num_register_sets) 152 return &g_reg_sets_arm[set_index]; 153 154 return nullptr; 155 } 156 157 Status 158 NativeRegisterContextLinux_arm::ReadRegister(const RegisterInfo *reg_info, 159 RegisterValue ®_value) { 160 Status error; 161 162 if (!reg_info) { 163 error.SetErrorString("reg_info NULL"); 164 return error; 165 } 166 167 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB]; 168 169 if (IsFPR(reg)) { 170 error = ReadFPR(); 171 if (error.Fail()) 172 return error; 173 } else { 174 uint32_t full_reg = reg; 175 bool is_subreg = reg_info->invalidate_regs && 176 (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM); 177 178 if (is_subreg) { 179 // Read the full aligned 64-bit register. 180 full_reg = reg_info->invalidate_regs[0]; 181 } 182 183 error = ReadRegisterRaw(full_reg, reg_value); 184 185 if (error.Success()) { 186 // If our read was not aligned (for ah,bh,ch,dh), shift our returned 187 // value one byte to the right. 188 if (is_subreg && (reg_info->byte_offset & 0x1)) 189 reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8); 190 191 // If our return byte size was greater than the return value reg size, 192 // then use the type specified by reg_info rather than the uint64_t 193 // default 194 if (reg_value.GetByteSize() > reg_info->byte_size) 195 reg_value.SetType(reg_info); 196 } 197 return error; 198 } 199 200 // Get pointer to m_fpr variable and set the data from it. 201 uint32_t fpr_offset = CalculateFprOffset(reg_info); 202 assert(fpr_offset < sizeof m_fpr); 203 uint8_t *src = (uint8_t *)&m_fpr + fpr_offset; 204 switch (reg_info->byte_size) { 205 case 2: 206 reg_value.SetUInt16(*(uint16_t *)src); 207 break; 208 case 4: 209 reg_value.SetUInt32(*(uint32_t *)src); 210 break; 211 case 8: 212 reg_value.SetUInt64(*(uint64_t *)src); 213 break; 214 case 16: 215 reg_value.SetBytes(src, 16, GetByteOrder()); 216 break; 217 default: 218 assert(false && "Unhandled data size."); 219 error.SetErrorStringWithFormat("unhandled byte size: %" PRIu32, 220 reg_info->byte_size); 221 break; 222 } 223 224 return error; 225 } 226 227 Status 228 NativeRegisterContextLinux_arm::WriteRegister(const RegisterInfo *reg_info, 229 const RegisterValue ®_value) { 230 if (!reg_info) 231 return Status("reg_info NULL"); 232 233 const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB]; 234 if (reg_index == LLDB_INVALID_REGNUM) 235 return Status("no lldb regnum for %s", reg_info && reg_info->name 236 ? reg_info->name 237 : "<unknown register>"); 238 239 if (IsGPR(reg_index)) 240 return WriteRegisterRaw(reg_index, reg_value); 241 242 if (IsFPR(reg_index)) { 243 // Get pointer to m_fpr variable and set the data to it. 244 uint32_t fpr_offset = CalculateFprOffset(reg_info); 245 assert(fpr_offset < sizeof m_fpr); 246 uint8_t *dst = (uint8_t *)&m_fpr + fpr_offset; 247 switch (reg_info->byte_size) { 248 case 2: 249 *(uint16_t *)dst = reg_value.GetAsUInt16(); 250 break; 251 case 4: 252 *(uint32_t *)dst = reg_value.GetAsUInt32(); 253 break; 254 case 8: 255 *(uint64_t *)dst = reg_value.GetAsUInt64(); 256 break; 257 default: 258 assert(false && "Unhandled data size."); 259 return Status("unhandled register data size %" PRIu32, 260 reg_info->byte_size); 261 } 262 263 Status error = WriteFPR(); 264 if (error.Fail()) 265 return error; 266 267 return Status(); 268 } 269 270 return Status("failed - register wasn't recognized to be a GPR or an FPR, " 271 "write strategy unknown"); 272 } 273 274 Status NativeRegisterContextLinux_arm::ReadAllRegisterValues( 275 lldb::DataBufferSP &data_sp) { 276 Status error; 277 278 data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0)); 279 if (!data_sp) 280 return Status("failed to allocate DataBufferHeap instance of size %" PRIu64, 281 (uint64_t)REG_CONTEXT_SIZE); 282 283 error = ReadGPR(); 284 if (error.Fail()) 285 return error; 286 287 error = ReadFPR(); 288 if (error.Fail()) 289 return error; 290 291 uint8_t *dst = data_sp->GetBytes(); 292 if (dst == nullptr) { 293 error.SetErrorStringWithFormat("DataBufferHeap instance of size %" PRIu64 294 " returned a null pointer", 295 (uint64_t)REG_CONTEXT_SIZE); 296 return error; 297 } 298 299 ::memcpy(dst, &m_gpr_arm, GetGPRSize()); 300 dst += GetGPRSize(); 301 ::memcpy(dst, &m_fpr, sizeof(m_fpr)); 302 303 return error; 304 } 305 306 Status NativeRegisterContextLinux_arm::WriteAllRegisterValues( 307 const lldb::DataBufferSP &data_sp) { 308 Status error; 309 310 if (!data_sp) { 311 error.SetErrorStringWithFormat( 312 "NativeRegisterContextLinux_x86_64::%s invalid data_sp provided", 313 __FUNCTION__); 314 return error; 315 } 316 317 if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) { 318 error.SetErrorStringWithFormat( 319 "NativeRegisterContextLinux_x86_64::%s data_sp contained mismatched " 320 "data size, expected %" PRIu64 ", actual %" PRIu64, 321 __FUNCTION__, (uint64_t)REG_CONTEXT_SIZE, data_sp->GetByteSize()); 322 return error; 323 } 324 325 uint8_t *src = data_sp->GetBytes(); 326 if (src == nullptr) { 327 error.SetErrorStringWithFormat("NativeRegisterContextLinux_x86_64::%s " 328 "DataBuffer::GetBytes() returned a null " 329 "pointer", 330 __FUNCTION__); 331 return error; 332 } 333 ::memcpy(&m_gpr_arm, src, GetRegisterInfoInterface().GetGPRSize()); 334 335 error = WriteGPR(); 336 if (error.Fail()) 337 return error; 338 339 src += GetRegisterInfoInterface().GetGPRSize(); 340 ::memcpy(&m_fpr, src, sizeof(m_fpr)); 341 342 error = WriteFPR(); 343 if (error.Fail()) 344 return error; 345 346 return error; 347 } 348 349 bool NativeRegisterContextLinux_arm::IsGPR(unsigned reg) const { 350 return reg <= m_reg_info.last_gpr; // GPR's come first. 351 } 352 353 bool NativeRegisterContextLinux_arm::IsFPR(unsigned reg) const { 354 return (m_reg_info.first_fpr <= reg && reg <= m_reg_info.last_fpr); 355 } 356 357 uint32_t NativeRegisterContextLinux_arm::NumSupportedHardwareBreakpoints() { 358 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 359 360 if (log) 361 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 362 363 Status error; 364 365 // Read hardware breakpoint and watchpoint information. 366 error = ReadHardwareDebugInfo(); 367 368 if (error.Fail()) 369 return 0; 370 371 LLDB_LOG(log, "{0}", m_max_hbp_supported); 372 return m_max_hbp_supported; 373 } 374 375 uint32_t 376 NativeRegisterContextLinux_arm::SetHardwareBreakpoint(lldb::addr_t addr, 377 size_t size) { 378 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 379 LLDB_LOG(log, "addr: {0:x}, size: {1:x}", addr, size); 380 381 // Read hardware breakpoint and watchpoint information. 382 Status error = ReadHardwareDebugInfo(); 383 384 if (error.Fail()) 385 return LLDB_INVALID_INDEX32; 386 387 uint32_t control_value = 0, bp_index = 0; 388 389 // Setup address and control values. 390 // Use size to get a hint of arm vs thumb modes. 391 switch (size) { 392 case 2: 393 control_value = (0x3 << 5) | 7; 394 addr &= ~1; 395 break; 396 case 4: 397 control_value = (0xfu << 5) | 7; 398 addr &= ~3; 399 break; 400 default: 401 return LLDB_INVALID_INDEX32; 402 } 403 404 // Iterate over stored breakpoints and find a free bp_index 405 bp_index = LLDB_INVALID_INDEX32; 406 for (uint32_t i = 0; i < m_max_hbp_supported; i++) { 407 if ((m_hbr_regs[i].control & 1) == 0) { 408 bp_index = i; // Mark last free slot 409 } else if (m_hbr_regs[i].address == addr) { 410 return LLDB_INVALID_INDEX32; // We do not support duplicate breakpoints. 411 } 412 } 413 414 if (bp_index == LLDB_INVALID_INDEX32) 415 return LLDB_INVALID_INDEX32; 416 417 // Update breakpoint in local cache 418 m_hbr_regs[bp_index].real_addr = addr; 419 m_hbr_regs[bp_index].address = addr; 420 m_hbr_regs[bp_index].control = control_value; 421 422 // PTRACE call to set corresponding hardware breakpoint register. 423 error = WriteHardwareDebugRegs(eDREGTypeBREAK, bp_index); 424 425 if (error.Fail()) { 426 m_hbr_regs[bp_index].address = 0; 427 m_hbr_regs[bp_index].control &= ~1; 428 429 return LLDB_INVALID_INDEX32; 430 } 431 432 return bp_index; 433 } 434 435 bool NativeRegisterContextLinux_arm::ClearHardwareBreakpoint(uint32_t hw_idx) { 436 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 437 LLDB_LOG(log, "hw_idx: {0}", hw_idx); 438 439 // Read hardware breakpoint and watchpoint information. 440 Status error = ReadHardwareDebugInfo(); 441 442 if (error.Fail()) 443 return false; 444 445 if (hw_idx >= m_max_hbp_supported) 446 return false; 447 448 // Create a backup we can revert to in case of failure. 449 lldb::addr_t tempAddr = m_hbr_regs[hw_idx].address; 450 uint32_t tempControl = m_hbr_regs[hw_idx].control; 451 452 m_hbr_regs[hw_idx].control &= ~1; 453 m_hbr_regs[hw_idx].address = 0; 454 455 // PTRACE call to clear corresponding hardware breakpoint register. 456 error = WriteHardwareDebugRegs(eDREGTypeBREAK, hw_idx); 457 458 if (error.Fail()) { 459 m_hbr_regs[hw_idx].control = tempControl; 460 m_hbr_regs[hw_idx].address = tempAddr; 461 462 return false; 463 } 464 465 return true; 466 } 467 468 Status NativeRegisterContextLinux_arm::GetHardwareBreakHitIndex( 469 uint32_t &bp_index, lldb::addr_t trap_addr) { 470 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 471 472 if (log) 473 log->Printf("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); 474 475 lldb::addr_t break_addr; 476 477 for (bp_index = 0; bp_index < m_max_hbp_supported; ++bp_index) { 478 break_addr = m_hbr_regs[bp_index].address; 479 480 if ((m_hbr_regs[bp_index].control & 0x1) && (trap_addr == break_addr)) { 481 m_hbr_regs[bp_index].hit_addr = trap_addr; 482 return Status(); 483 } 484 } 485 486 bp_index = LLDB_INVALID_INDEX32; 487 return Status(); 488 } 489 490 Status NativeRegisterContextLinux_arm::ClearAllHardwareBreakpoints() { 491 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 492 493 if (log) 494 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 495 496 Status error; 497 498 // Read hardware breakpoint and watchpoint information. 499 error = ReadHardwareDebugInfo(); 500 501 if (error.Fail()) 502 return error; 503 504 lldb::addr_t tempAddr = 0; 505 uint32_t tempControl = 0; 506 507 for (uint32_t i = 0; i < m_max_hbp_supported; i++) { 508 if (m_hbr_regs[i].control & 0x01) { 509 // Create a backup we can revert to in case of failure. 510 tempAddr = m_hbr_regs[i].address; 511 tempControl = m_hbr_regs[i].control; 512 513 // Clear breakpoints in local cache 514 m_hbr_regs[i].control &= ~1; 515 m_hbr_regs[i].address = 0; 516 517 // Ptrace call to update hardware debug registers 518 error = WriteHardwareDebugRegs(eDREGTypeBREAK, i); 519 520 if (error.Fail()) { 521 m_hbr_regs[i].control = tempControl; 522 m_hbr_regs[i].address = tempAddr; 523 524 return error; 525 } 526 } 527 } 528 529 return Status(); 530 } 531 532 uint32_t NativeRegisterContextLinux_arm::NumSupportedHardwareWatchpoints() { 533 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 534 535 // Read hardware breakpoint and watchpoint information. 536 Status error = ReadHardwareDebugInfo(); 537 538 if (error.Fail()) 539 return 0; 540 541 LLDB_LOG(log, "{0}", m_max_hwp_supported); 542 return m_max_hwp_supported; 543 } 544 545 uint32_t NativeRegisterContextLinux_arm::SetHardwareWatchpoint( 546 lldb::addr_t addr, size_t size, uint32_t watch_flags) { 547 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 548 LLDB_LOG(log, "addr: {0:x}, size: {1:x} watch_flags: {2:x}", addr, size, 549 watch_flags); 550 551 // Read hardware breakpoint and watchpoint information. 552 Status error = ReadHardwareDebugInfo(); 553 554 if (error.Fail()) 555 return LLDB_INVALID_INDEX32; 556 557 uint32_t control_value = 0, wp_index = 0, addr_word_offset = 0, byte_mask = 0; 558 lldb::addr_t real_addr = addr; 559 560 // Check if we are setting watchpoint other than read/write/access Also 561 // update watchpoint flag to match Arm write-read bit configuration. 562 switch (watch_flags) { 563 case 1: 564 watch_flags = 2; 565 break; 566 case 2: 567 watch_flags = 1; 568 break; 569 case 3: 570 break; 571 default: 572 return LLDB_INVALID_INDEX32; 573 } 574 575 // Can't watch zero bytes 576 // Can't watch more than 4 bytes per WVR/WCR pair 577 578 if (size == 0 || size > 4) 579 return LLDB_INVALID_INDEX32; 580 581 // Check 4-byte alignment for hardware watchpoint target address. Below is a 582 // hack to recalculate address and size in order to make sure we can watch 583 // non 4-byte alligned addresses as well. 584 if (addr & 0x03) { 585 uint8_t watch_mask = (addr & 0x03) + size; 586 587 if (watch_mask > 0x04) 588 return LLDB_INVALID_INDEX32; 589 else if (watch_mask <= 0x02) 590 size = 2; 591 else if (watch_mask <= 0x04) 592 size = 4; 593 594 addr = addr & (~0x03); 595 } 596 597 // We can only watch up to four bytes that follow a 4 byte aligned address 598 // per watchpoint register pair, so make sure we can properly encode this. 599 addr_word_offset = addr % 4; 600 byte_mask = ((1u << size) - 1u) << addr_word_offset; 601 602 // Check if we need multiple watchpoint register 603 if (byte_mask > 0xfu) 604 return LLDB_INVALID_INDEX32; 605 606 // Setup control value 607 // Make the byte_mask into a valid Byte Address Select mask 608 control_value = byte_mask << 5; 609 610 // Turn on appropriate watchpoint flags read or write 611 control_value |= (watch_flags << 3); 612 613 // Enable this watchpoint and make it stop in privileged or user mode; 614 control_value |= 7; 615 616 // Make sure bits 1:0 are clear in our address 617 addr &= ~((lldb::addr_t)3); 618 619 // Iterate over stored watchpoints and find a free wp_index 620 wp_index = LLDB_INVALID_INDEX32; 621 for (uint32_t i = 0; i < m_max_hwp_supported; i++) { 622 if ((m_hwp_regs[i].control & 1) == 0) { 623 wp_index = i; // Mark last free slot 624 } else if (m_hwp_regs[i].address == addr) { 625 return LLDB_INVALID_INDEX32; // We do not support duplicate watchpoints. 626 } 627 } 628 629 if (wp_index == LLDB_INVALID_INDEX32) 630 return LLDB_INVALID_INDEX32; 631 632 // Update watchpoint in local cache 633 m_hwp_regs[wp_index].real_addr = real_addr; 634 m_hwp_regs[wp_index].address = addr; 635 m_hwp_regs[wp_index].control = control_value; 636 637 // PTRACE call to set corresponding watchpoint register. 638 error = WriteHardwareDebugRegs(eDREGTypeWATCH, wp_index); 639 640 if (error.Fail()) { 641 m_hwp_regs[wp_index].address = 0; 642 m_hwp_regs[wp_index].control &= ~1; 643 644 return LLDB_INVALID_INDEX32; 645 } 646 647 return wp_index; 648 } 649 650 bool NativeRegisterContextLinux_arm::ClearHardwareWatchpoint( 651 uint32_t wp_index) { 652 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 653 LLDB_LOG(log, "wp_index: {0}", wp_index); 654 655 // Read hardware breakpoint and watchpoint information. 656 Status error = ReadHardwareDebugInfo(); 657 658 if (error.Fail()) 659 return false; 660 661 if (wp_index >= m_max_hwp_supported) 662 return false; 663 664 // Create a backup we can revert to in case of failure. 665 lldb::addr_t tempAddr = m_hwp_regs[wp_index].address; 666 uint32_t tempControl = m_hwp_regs[wp_index].control; 667 668 // Update watchpoint in local cache 669 m_hwp_regs[wp_index].control &= ~1; 670 m_hwp_regs[wp_index].address = 0; 671 672 // Ptrace call to update hardware debug registers 673 error = WriteHardwareDebugRegs(eDREGTypeWATCH, wp_index); 674 675 if (error.Fail()) { 676 m_hwp_regs[wp_index].control = tempControl; 677 m_hwp_regs[wp_index].address = tempAddr; 678 679 return false; 680 } 681 682 return true; 683 } 684 685 Status NativeRegisterContextLinux_arm::ClearAllHardwareWatchpoints() { 686 // Read hardware breakpoint and watchpoint information. 687 Status error = ReadHardwareDebugInfo(); 688 689 if (error.Fail()) 690 return error; 691 692 lldb::addr_t tempAddr = 0; 693 uint32_t tempControl = 0; 694 695 for (uint32_t i = 0; i < m_max_hwp_supported; i++) { 696 if (m_hwp_regs[i].control & 0x01) { 697 // Create a backup we can revert to in case of failure. 698 tempAddr = m_hwp_regs[i].address; 699 tempControl = m_hwp_regs[i].control; 700 701 // Clear watchpoints in local cache 702 m_hwp_regs[i].control &= ~1; 703 m_hwp_regs[i].address = 0; 704 705 // Ptrace call to update hardware debug registers 706 error = WriteHardwareDebugRegs(eDREGTypeWATCH, i); 707 708 if (error.Fail()) { 709 m_hwp_regs[i].control = tempControl; 710 m_hwp_regs[i].address = tempAddr; 711 712 return error; 713 } 714 } 715 } 716 717 return Status(); 718 } 719 720 uint32_t NativeRegisterContextLinux_arm::GetWatchpointSize(uint32_t wp_index) { 721 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 722 LLDB_LOG(log, "wp_index: {0}", wp_index); 723 724 switch ((m_hwp_regs[wp_index].control >> 5) & 0x0f) { 725 case 0x01: 726 return 1; 727 case 0x03: 728 return 2; 729 case 0x07: 730 return 3; 731 case 0x0f: 732 return 4; 733 default: 734 return 0; 735 } 736 } 737 bool NativeRegisterContextLinux_arm::WatchpointIsEnabled(uint32_t wp_index) { 738 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 739 LLDB_LOG(log, "wp_index: {0}", wp_index); 740 741 if ((m_hwp_regs[wp_index].control & 0x1) == 0x1) 742 return true; 743 else 744 return false; 745 } 746 747 Status 748 NativeRegisterContextLinux_arm::GetWatchpointHitIndex(uint32_t &wp_index, 749 lldb::addr_t trap_addr) { 750 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 751 LLDB_LOG(log, "wp_index: {0}, trap_addr: {1:x}", wp_index, trap_addr); 752 753 uint32_t watch_size; 754 lldb::addr_t watch_addr; 755 756 for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) { 757 watch_size = GetWatchpointSize(wp_index); 758 watch_addr = m_hwp_regs[wp_index].address; 759 760 if (WatchpointIsEnabled(wp_index) && trap_addr >= watch_addr && 761 trap_addr < watch_addr + watch_size) { 762 m_hwp_regs[wp_index].hit_addr = trap_addr; 763 return Status(); 764 } 765 } 766 767 wp_index = LLDB_INVALID_INDEX32; 768 return Status(); 769 } 770 771 lldb::addr_t 772 NativeRegisterContextLinux_arm::GetWatchpointAddress(uint32_t wp_index) { 773 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 774 LLDB_LOG(log, "wp_index: {0}", wp_index); 775 776 if (wp_index >= m_max_hwp_supported) 777 return LLDB_INVALID_ADDRESS; 778 779 if (WatchpointIsEnabled(wp_index)) 780 return m_hwp_regs[wp_index].real_addr; 781 else 782 return LLDB_INVALID_ADDRESS; 783 } 784 785 lldb::addr_t 786 NativeRegisterContextLinux_arm::GetWatchpointHitAddress(uint32_t wp_index) { 787 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 788 LLDB_LOG(log, "wp_index: {0}", wp_index); 789 790 if (wp_index >= m_max_hwp_supported) 791 return LLDB_INVALID_ADDRESS; 792 793 if (WatchpointIsEnabled(wp_index)) 794 return m_hwp_regs[wp_index].hit_addr; 795 else 796 return LLDB_INVALID_ADDRESS; 797 } 798 799 Status NativeRegisterContextLinux_arm::ReadHardwareDebugInfo() { 800 Status error; 801 802 if (!m_refresh_hwdebug_info) { 803 return Status(); 804 } 805 806 unsigned int cap_val; 807 808 error = NativeProcessLinux::PtraceWrapper(PTRACE_GETHBPREGS, m_thread.GetID(), 809 nullptr, &cap_val, 810 sizeof(unsigned int)); 811 812 if (error.Fail()) 813 return error; 814 815 m_max_hwp_supported = (cap_val >> 8) & 0xff; 816 m_max_hbp_supported = cap_val & 0xff; 817 m_refresh_hwdebug_info = false; 818 819 return error; 820 } 821 822 Status NativeRegisterContextLinux_arm::WriteHardwareDebugRegs(int hwbType, 823 int hwb_index) { 824 Status error; 825 826 lldb::addr_t *addr_buf; 827 uint32_t *ctrl_buf; 828 829 if (hwbType == eDREGTypeWATCH) { 830 addr_buf = &m_hwp_regs[hwb_index].address; 831 ctrl_buf = &m_hwp_regs[hwb_index].control; 832 833 error = NativeProcessLinux::PtraceWrapper( 834 PTRACE_SETHBPREGS, m_thread.GetID(), 835 (PTRACE_TYPE_ARG3)(intptr_t) - ((hwb_index << 1) + 1), addr_buf, 836 sizeof(unsigned int)); 837 838 if (error.Fail()) 839 return error; 840 841 error = NativeProcessLinux::PtraceWrapper( 842 PTRACE_SETHBPREGS, m_thread.GetID(), 843 (PTRACE_TYPE_ARG3)(intptr_t) - ((hwb_index << 1) + 2), ctrl_buf, 844 sizeof(unsigned int)); 845 } else { 846 addr_buf = &m_hbr_regs[hwb_index].address; 847 ctrl_buf = &m_hbr_regs[hwb_index].control; 848 849 error = NativeProcessLinux::PtraceWrapper( 850 PTRACE_SETHBPREGS, m_thread.GetID(), 851 (PTRACE_TYPE_ARG3)(intptr_t)((hwb_index << 1) + 1), addr_buf, 852 sizeof(unsigned int)); 853 854 if (error.Fail()) 855 return error; 856 857 error = NativeProcessLinux::PtraceWrapper( 858 PTRACE_SETHBPREGS, m_thread.GetID(), 859 (PTRACE_TYPE_ARG3)(intptr_t)((hwb_index << 1) + 2), ctrl_buf, 860 sizeof(unsigned int)); 861 } 862 863 return error; 864 } 865 866 uint32_t NativeRegisterContextLinux_arm::CalculateFprOffset( 867 const RegisterInfo *reg_info) const { 868 return reg_info->byte_offset - 869 GetRegisterInfoAtIndex(m_reg_info.first_fpr)->byte_offset; 870 } 871 872 Status NativeRegisterContextLinux_arm::DoReadRegisterValue( 873 uint32_t offset, const char *reg_name, uint32_t size, 874 RegisterValue &value) { 875 // PTRACE_PEEKUSER don't work in the aarch64 linux kernel used on android 876 // devices (always return "Bad address"). To avoid using PTRACE_PEEKUSER we 877 // read out the full GPR register set instead. This approach is about 4 times 878 // slower but the performance overhead is negligible in comparision to 879 // processing time in lldb-server. 880 assert(offset % 4 == 0 && "Try to write a register with unaligned offset"); 881 if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm)) 882 return Status("Register isn't fit into the size of the GPR area"); 883 884 Status error = DoReadGPR(m_gpr_arm, sizeof(m_gpr_arm)); 885 if (error.Fail()) 886 return error; 887 888 value.SetUInt32(m_gpr_arm[offset / sizeof(uint32_t)]); 889 return Status(); 890 } 891 892 Status NativeRegisterContextLinux_arm::DoWriteRegisterValue( 893 uint32_t offset, const char *reg_name, const RegisterValue &value) { 894 // PTRACE_POKEUSER don't work in the aarch64 linux kernel used on android 895 // devices (always return "Bad address"). To avoid using PTRACE_POKEUSER we 896 // read out the full GPR register set, modify the requested register and 897 // write it back. This approach is about 4 times slower but the performance 898 // overhead is negligible in comparision to processing time in lldb-server. 899 assert(offset % 4 == 0 && "Try to write a register with unaligned offset"); 900 if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm)) 901 return Status("Register isn't fit into the size of the GPR area"); 902 903 Status error = DoReadGPR(m_gpr_arm, sizeof(m_gpr_arm)); 904 if (error.Fail()) 905 return error; 906 907 uint32_t reg_value = value.GetAsUInt32(); 908 // As precaution for an undefined behavior encountered while setting PC we 909 // will clear thumb bit of new PC if we are already in thumb mode; that is 910 // CPSR thumb mode bit is set. 911 if (offset / sizeof(uint32_t) == gpr_pc_arm) { 912 // Check if we are already in thumb mode and thumb bit of current PC is 913 // read out to be zero and thumb bit of next PC is read out to be one. 914 if ((m_gpr_arm[gpr_cpsr_arm] & 0x20) && !(m_gpr_arm[gpr_pc_arm] & 0x01) && 915 (value.GetAsUInt32() & 0x01)) { 916 reg_value &= (~1ull); 917 } 918 } 919 920 m_gpr_arm[offset / sizeof(uint32_t)] = reg_value; 921 return DoWriteGPR(m_gpr_arm, sizeof(m_gpr_arm)); 922 } 923 924 Status NativeRegisterContextLinux_arm::DoReadGPR(void *buf, size_t buf_size) { 925 #ifdef __arm__ 926 return NativeRegisterContextLinux::DoReadGPR(buf, buf_size); 927 #else // __aarch64__ 928 struct iovec ioVec; 929 ioVec.iov_base = buf; 930 ioVec.iov_len = buf_size; 931 932 return ReadRegisterSet(&ioVec, buf_size, NT_PRSTATUS); 933 #endif // __arm__ 934 } 935 936 Status NativeRegisterContextLinux_arm::DoWriteGPR(void *buf, size_t buf_size) { 937 #ifdef __arm__ 938 return NativeRegisterContextLinux::DoWriteGPR(buf, buf_size); 939 #else // __aarch64__ 940 struct iovec ioVec; 941 ioVec.iov_base = buf; 942 ioVec.iov_len = buf_size; 943 944 return WriteRegisterSet(&ioVec, buf_size, NT_PRSTATUS); 945 #endif // __arm__ 946 } 947 948 Status NativeRegisterContextLinux_arm::DoReadFPR(void *buf, size_t buf_size) { 949 #ifdef __arm__ 950 return NativeProcessLinux::PtraceWrapper(PTRACE_GETVFPREGS, m_thread.GetID(), 951 nullptr, buf, buf_size); 952 #else // __aarch64__ 953 struct iovec ioVec; 954 ioVec.iov_base = buf; 955 ioVec.iov_len = buf_size; 956 957 return ReadRegisterSet(&ioVec, buf_size, NT_ARM_VFP); 958 #endif // __arm__ 959 } 960 961 Status NativeRegisterContextLinux_arm::DoWriteFPR(void *buf, size_t buf_size) { 962 #ifdef __arm__ 963 return NativeProcessLinux::PtraceWrapper(PTRACE_SETVFPREGS, m_thread.GetID(), 964 nullptr, buf, buf_size); 965 #else // __aarch64__ 966 struct iovec ioVec; 967 ioVec.iov_base = buf; 968 ioVec.iov_len = buf_size; 969 970 return WriteRegisterSet(&ioVec, buf_size, NT_ARM_VFP); 971 #endif // __arm__ 972 } 973 974 #endif // defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 975