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