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/DataBufferHeap.h" 15 #include "lldb/Core/Error.h" 16 #include "lldb/Core/Log.h" 17 #include "lldb/Core/RegisterValue.h" 18 19 #include "Plugins/Process/Linux/Procfs.h" 20 #include "Plugins/Process/Utility/RegisterContextLinux_arm.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 NativeRegisterContextLinux * 98 NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux( 99 const ArchSpec &target_arch, NativeThreadProtocol &native_thread, 100 uint32_t concrete_frame_idx) { 101 return new NativeRegisterContextLinux_arm(target_arch, native_thread, 102 concrete_frame_idx); 103 } 104 105 #endif // defined(__arm__) 106 107 NativeRegisterContextLinux_arm::NativeRegisterContextLinux_arm( 108 const ArchSpec &target_arch, NativeThreadProtocol &native_thread, 109 uint32_t concrete_frame_idx) 110 : NativeRegisterContextLinux(native_thread, concrete_frame_idx, 111 new RegisterContextLinux_arm(target_arch)) { 112 switch (target_arch.GetMachine()) { 113 case llvm::Triple::arm: 114 m_reg_info.num_registers = k_num_registers_arm; 115 m_reg_info.num_gpr_registers = k_num_gpr_registers_arm; 116 m_reg_info.num_fpr_registers = k_num_fpr_registers_arm; 117 m_reg_info.last_gpr = k_last_gpr_arm; 118 m_reg_info.first_fpr = k_first_fpr_arm; 119 m_reg_info.last_fpr = k_last_fpr_arm; 120 m_reg_info.first_fpr_v = fpu_s0_arm; 121 m_reg_info.last_fpr_v = fpu_s31_arm; 122 m_reg_info.gpr_flags = gpr_cpsr_arm; 123 break; 124 default: 125 assert(false && "Unhandled target architecture."); 126 break; 127 } 128 129 ::memset(&m_fpr, 0, sizeof(m_fpr)); 130 ::memset(&m_gpr_arm, 0, sizeof(m_gpr_arm)); 131 ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs)); 132 133 // 16 is just a maximum value, query hardware for actual watchpoint count 134 m_max_hwp_supported = 16; 135 m_max_hbp_supported = 16; 136 m_refresh_hwdebug_info = true; 137 } 138 139 uint32_t NativeRegisterContextLinux_arm::GetRegisterSetCount() const { 140 return k_num_register_sets; 141 } 142 143 uint32_t NativeRegisterContextLinux_arm::GetUserRegisterCount() const { 144 uint32_t count = 0; 145 for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) 146 count += g_reg_sets_arm[set_index].num_registers; 147 return count; 148 } 149 150 const RegisterSet * 151 NativeRegisterContextLinux_arm::GetRegisterSet(uint32_t set_index) const { 152 if (set_index < k_num_register_sets) 153 return &g_reg_sets_arm[set_index]; 154 155 return nullptr; 156 } 157 158 Error NativeRegisterContextLinux_arm::ReadRegister(const RegisterInfo *reg_info, 159 RegisterValue ®_value) { 160 Error 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 value 187 // 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 193 // use the type specified by reg_info rather than the uint64_t 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 Error NativeRegisterContextLinux_arm::WriteRegister( 228 const RegisterInfo *reg_info, const RegisterValue ®_value) { 229 if (!reg_info) 230 return Error("reg_info NULL"); 231 232 const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB]; 233 if (reg_index == LLDB_INVALID_REGNUM) 234 return Error("no lldb regnum for %s", reg_info && reg_info->name 235 ? reg_info->name 236 : "<unknown register>"); 237 238 if (IsGPR(reg_index)) 239 return WriteRegisterRaw(reg_index, reg_value); 240 241 if (IsFPR(reg_index)) { 242 // Get pointer to m_fpr variable and set the data to it. 243 uint32_t fpr_offset = CalculateFprOffset(reg_info); 244 assert(fpr_offset < sizeof m_fpr); 245 uint8_t *dst = (uint8_t *)&m_fpr + fpr_offset; 246 switch (reg_info->byte_size) { 247 case 2: 248 *(uint16_t *)dst = reg_value.GetAsUInt16(); 249 break; 250 case 4: 251 *(uint32_t *)dst = reg_value.GetAsUInt32(); 252 break; 253 case 8: 254 *(uint64_t *)dst = reg_value.GetAsUInt64(); 255 break; 256 default: 257 assert(false && "Unhandled data size."); 258 return Error("unhandled register data size %" PRIu32, 259 reg_info->byte_size); 260 } 261 262 Error error = WriteFPR(); 263 if (error.Fail()) 264 return error; 265 266 return Error(); 267 } 268 269 return Error("failed - register wasn't recognized to be a GPR or an FPR, " 270 "write strategy unknown"); 271 } 272 273 Error NativeRegisterContextLinux_arm::ReadAllRegisterValues( 274 lldb::DataBufferSP &data_sp) { 275 Error error; 276 277 data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0)); 278 if (!data_sp) 279 return Error("failed to allocate DataBufferHeap instance of size %" PRIu64, 280 (uint64_t)REG_CONTEXT_SIZE); 281 282 error = ReadGPR(); 283 if (error.Fail()) 284 return error; 285 286 error = ReadFPR(); 287 if (error.Fail()) 288 return error; 289 290 uint8_t *dst = data_sp->GetBytes(); 291 if (dst == nullptr) { 292 error.SetErrorStringWithFormat("DataBufferHeap instance of size %" PRIu64 293 " returned a null pointer", 294 (uint64_t)REG_CONTEXT_SIZE); 295 return error; 296 } 297 298 ::memcpy(dst, &m_gpr_arm, GetGPRSize()); 299 dst += GetGPRSize(); 300 ::memcpy(dst, &m_fpr, sizeof(m_fpr)); 301 302 return error; 303 } 304 305 Error NativeRegisterContextLinux_arm::WriteAllRegisterValues( 306 const lldb::DataBufferSP &data_sp) { 307 Error error; 308 309 if (!data_sp) { 310 error.SetErrorStringWithFormat( 311 "NativeRegisterContextLinux_x86_64::%s invalid data_sp provided", 312 __FUNCTION__); 313 return error; 314 } 315 316 if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) { 317 error.SetErrorStringWithFormat( 318 "NativeRegisterContextLinux_x86_64::%s data_sp contained mismatched " 319 "data size, expected %" PRIu64 ", actual %" PRIu64, 320 __FUNCTION__, (uint64_t)REG_CONTEXT_SIZE, data_sp->GetByteSize()); 321 return error; 322 } 323 324 uint8_t *src = data_sp->GetBytes(); 325 if (src == nullptr) { 326 error.SetErrorStringWithFormat("NativeRegisterContextLinux_x86_64::%s " 327 "DataBuffer::GetBytes() returned a null " 328 "pointer", 329 __FUNCTION__); 330 return error; 331 } 332 ::memcpy(&m_gpr_arm, src, GetRegisterInfoInterface().GetGPRSize()); 333 334 error = WriteGPR(); 335 if (error.Fail()) 336 return error; 337 338 src += GetRegisterInfoInterface().GetGPRSize(); 339 ::memcpy(&m_fpr, src, sizeof(m_fpr)); 340 341 error = WriteFPR(); 342 if (error.Fail()) 343 return error; 344 345 return error; 346 } 347 348 bool NativeRegisterContextLinux_arm::IsGPR(unsigned reg) const { 349 return reg <= m_reg_info.last_gpr; // GPR's come first. 350 } 351 352 bool NativeRegisterContextLinux_arm::IsFPR(unsigned reg) const { 353 return (m_reg_info.first_fpr <= reg && reg <= m_reg_info.last_fpr); 354 } 355 356 uint32_t 357 NativeRegisterContextLinux_arm::SetHardwareBreakpoint(lldb::addr_t addr, 358 size_t size) { 359 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 360 361 if (log) 362 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 363 364 Error error; 365 366 // Read hardware breakpoint and watchpoint information. 367 error = ReadHardwareDebugInfo(); 368 369 if (error.Fail()) 370 return LLDB_INVALID_INDEX32; 371 372 uint32_t control_value = 0, bp_index = 0; 373 374 // Check if size has a valid hardware breakpoint length. 375 // Thumb instructions are 2-bytes but we have no way here to determine 376 // if target address is a thumb or arm instruction. 377 // TODO: Add support for setting thumb mode hardware breakpoints 378 if (size != 4 && size != 2) 379 return LLDB_INVALID_INDEX32; 380 381 // Setup control value 382 // Make the byte_mask into a valid Byte Address Select mask 383 control_value = 0xfu << 5; 384 385 // Enable this breakpoint and make it stop in privileged or user mode; 386 control_value |= 7; 387 388 // Make sure bits 1:0 are clear in our address 389 // This should be different once we support thumb here. 390 addr &= ~((lldb::addr_t)3); 391 392 // Iterate over stored hardware breakpoints 393 // Find a free bp_index or update reference count if duplicate. 394 bp_index = LLDB_INVALID_INDEX32; 395 396 for (uint32_t i = 0; i < m_max_hbp_supported; i++) { 397 if ((m_hbr_regs[i].control & 1) == 0) { 398 bp_index = i; // Mark last free slot 399 } else if (m_hbr_regs[i].address == addr && 400 m_hbr_regs[i].control == control_value) { 401 bp_index = i; // Mark duplicate index 402 break; // Stop searching here 403 } 404 } 405 406 if (bp_index == LLDB_INVALID_INDEX32) 407 return LLDB_INVALID_INDEX32; 408 409 // Add new or update existing breakpoint 410 if ((m_hbr_regs[bp_index].control & 1) == 0) { 411 m_hbr_regs[bp_index].address = addr; 412 m_hbr_regs[bp_index].control = control_value; 413 m_hbr_regs[bp_index].refcount = 1; 414 415 // PTRACE call to set corresponding hardware breakpoint register. 416 error = WriteHardwareDebugRegs(eDREGTypeBREAK, bp_index); 417 418 if (error.Fail()) { 419 m_hbr_regs[bp_index].address = 0; 420 m_hbr_regs[bp_index].control &= ~1; 421 m_hbr_regs[bp_index].refcount = 0; 422 423 return LLDB_INVALID_INDEX32; 424 } 425 } else 426 m_hbr_regs[bp_index].refcount++; 427 428 return bp_index; 429 } 430 431 bool NativeRegisterContextLinux_arm::ClearHardwareBreakpoint(uint32_t hw_idx) { 432 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 433 434 if (log) 435 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 436 437 Error error; 438 439 // Read hardware breakpoint and watchpoint information. 440 error = ReadHardwareDebugInfo(); 441 442 if (error.Fail()) 443 return false; 444 445 if (hw_idx >= m_max_hbp_supported) 446 return false; 447 448 // Update reference count if multiple references. 449 if (m_hbr_regs[hw_idx].refcount > 1) { 450 m_hbr_regs[hw_idx].refcount--; 451 return true; 452 } else if (m_hbr_regs[hw_idx].refcount == 1) { 453 // Create a backup we can revert to in case of failure. 454 lldb::addr_t tempAddr = m_hbr_regs[hw_idx].address; 455 uint32_t tempControl = m_hbr_regs[hw_idx].control; 456 uint32_t tempRefCount = m_hbr_regs[hw_idx].refcount; 457 458 m_hbr_regs[hw_idx].control &= ~1; 459 m_hbr_regs[hw_idx].address = 0; 460 m_hbr_regs[hw_idx].refcount = 0; 461 462 // PTRACE call to clear corresponding hardware breakpoint register. 463 WriteHardwareDebugRegs(eDREGTypeBREAK, hw_idx); 464 465 if (error.Fail()) { 466 m_hbr_regs[hw_idx].control = tempControl; 467 m_hbr_regs[hw_idx].address = tempAddr; 468 m_hbr_regs[hw_idx].refcount = tempRefCount; 469 470 return false; 471 } 472 473 return true; 474 } 475 476 return false; 477 } 478 479 uint32_t NativeRegisterContextLinux_arm::NumSupportedHardwareWatchpoints() { 480 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 481 482 if (log) 483 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 484 485 Error error; 486 487 // Read hardware breakpoint and watchpoint information. 488 error = ReadHardwareDebugInfo(); 489 490 if (error.Fail()) 491 return 0; 492 493 return m_max_hwp_supported; 494 } 495 496 uint32_t NativeRegisterContextLinux_arm::SetHardwareWatchpoint( 497 lldb::addr_t addr, size_t size, uint32_t watch_flags) { 498 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 499 500 if (log) 501 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 502 503 Error error; 504 505 // Read hardware breakpoint and watchpoint information. 506 error = ReadHardwareDebugInfo(); 507 508 if (error.Fail()) 509 return LLDB_INVALID_INDEX32; 510 511 uint32_t control_value = 0, wp_index = 0, addr_word_offset = 0, byte_mask = 0; 512 lldb::addr_t real_addr = addr; 513 514 // Check if we are setting watchpoint other than read/write/access 515 // Also update watchpoint flag to match Arm write-read bit configuration. 516 switch (watch_flags) { 517 case 1: 518 watch_flags = 2; 519 break; 520 case 2: 521 watch_flags = 1; 522 break; 523 case 3: 524 break; 525 default: 526 return LLDB_INVALID_INDEX32; 527 } 528 529 // Can't watch zero bytes 530 // Can't watch more than 4 bytes per WVR/WCR pair 531 532 if (size == 0 || size > 4) 533 return LLDB_INVALID_INDEX32; 534 535 // Check 4-byte alignment for hardware watchpoint target address. 536 // Below is a hack to recalculate address and size in order to 537 // make sure we can watch non 4-byte alligned addresses as well. 538 if (addr & 0x03) { 539 uint8_t watch_mask = (addr & 0x03) + size; 540 541 if (watch_mask > 0x04) 542 return LLDB_INVALID_INDEX32; 543 else if (watch_mask <= 0x02) 544 size = 2; 545 else if (watch_mask <= 0x04) 546 size = 4; 547 548 addr = addr & (~0x03); 549 } 550 551 // We can only watch up to four bytes that follow a 4 byte aligned address 552 // per watchpoint register pair, so make sure we can properly encode this. 553 addr_word_offset = addr % 4; 554 byte_mask = ((1u << size) - 1u) << addr_word_offset; 555 556 // Check if we need multiple watchpoint register 557 if (byte_mask > 0xfu) 558 return LLDB_INVALID_INDEX32; 559 560 // Setup control value 561 // Make the byte_mask into a valid Byte Address Select mask 562 control_value = byte_mask << 5; 563 564 // Turn on appropriate watchpoint flags read or write 565 control_value |= (watch_flags << 3); 566 567 // Enable this watchpoint and make it stop in privileged or user mode; 568 control_value |= 7; 569 570 // Make sure bits 1:0 are clear in our address 571 addr &= ~((lldb::addr_t)3); 572 573 // Iterate over stored watchpoints 574 // Find a free wp_index or update reference count if duplicate. 575 wp_index = LLDB_INVALID_INDEX32; 576 for (uint32_t i = 0; i < m_max_hwp_supported; i++) { 577 if ((m_hwp_regs[i].control & 1) == 0) { 578 wp_index = i; // Mark last free slot 579 } else if (m_hwp_regs[i].address == addr && 580 m_hwp_regs[i].control == control_value) { 581 wp_index = i; // Mark duplicate index 582 break; // Stop searching here 583 } 584 } 585 586 if (wp_index == LLDB_INVALID_INDEX32) 587 return LLDB_INVALID_INDEX32; 588 589 // Add new or update existing watchpoint 590 if ((m_hwp_regs[wp_index].control & 1) == 0) { 591 // Update watchpoint in local cache 592 m_hwp_regs[wp_index].real_addr = real_addr; 593 m_hwp_regs[wp_index].address = addr; 594 m_hwp_regs[wp_index].control = control_value; 595 m_hwp_regs[wp_index].refcount = 1; 596 597 // PTRACE call to set corresponding watchpoint register. 598 error = WriteHardwareDebugRegs(eDREGTypeWATCH, wp_index); 599 600 if (error.Fail()) { 601 m_hwp_regs[wp_index].address = 0; 602 m_hwp_regs[wp_index].control &= ~1; 603 m_hwp_regs[wp_index].refcount = 0; 604 605 return LLDB_INVALID_INDEX32; 606 } 607 } else 608 m_hwp_regs[wp_index].refcount++; 609 610 return wp_index; 611 } 612 613 bool NativeRegisterContextLinux_arm::ClearHardwareWatchpoint( 614 uint32_t wp_index) { 615 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 616 617 if (log) 618 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 619 620 Error error; 621 622 // Read hardware breakpoint and watchpoint information. 623 error = ReadHardwareDebugInfo(); 624 625 if (error.Fail()) 626 return false; 627 628 if (wp_index >= m_max_hwp_supported) 629 return false; 630 631 // Update reference count if multiple references. 632 if (m_hwp_regs[wp_index].refcount > 1) { 633 m_hwp_regs[wp_index].refcount--; 634 return true; 635 } else if (m_hwp_regs[wp_index].refcount == 1) { 636 // Create a backup we can revert to in case of failure. 637 lldb::addr_t tempAddr = m_hwp_regs[wp_index].address; 638 uint32_t tempControl = m_hwp_regs[wp_index].control; 639 uint32_t tempRefCount = m_hwp_regs[wp_index].refcount; 640 641 // Update watchpoint in local cache 642 m_hwp_regs[wp_index].control &= ~1; 643 m_hwp_regs[wp_index].address = 0; 644 m_hwp_regs[wp_index].refcount = 0; 645 646 // Ptrace call to update hardware debug registers 647 error = WriteHardwareDebugRegs(eDREGTypeWATCH, wp_index); 648 649 if (error.Fail()) { 650 m_hwp_regs[wp_index].control = tempControl; 651 m_hwp_regs[wp_index].address = tempAddr; 652 m_hwp_regs[wp_index].refcount = tempRefCount; 653 654 return false; 655 } 656 657 return true; 658 } 659 660 return false; 661 } 662 663 Error NativeRegisterContextLinux_arm::ClearAllHardwareWatchpoints() { 664 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 665 666 if (log) 667 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 668 669 Error error; 670 671 // Read hardware breakpoint and watchpoint information. 672 error = ReadHardwareDebugInfo(); 673 674 if (error.Fail()) 675 return error; 676 677 lldb::addr_t tempAddr = 0; 678 uint32_t tempControl = 0, tempRefCount = 0; 679 680 for (uint32_t i = 0; i < m_max_hwp_supported; i++) { 681 if (m_hwp_regs[i].control & 0x01) { 682 // Create a backup we can revert to in case of failure. 683 tempAddr = m_hwp_regs[i].address; 684 tempControl = m_hwp_regs[i].control; 685 tempRefCount = m_hwp_regs[i].refcount; 686 687 // Clear watchpoints in local cache 688 m_hwp_regs[i].control &= ~1; 689 m_hwp_regs[i].address = 0; 690 m_hwp_regs[i].refcount = 0; 691 692 // Ptrace call to update hardware debug registers 693 error = WriteHardwareDebugRegs(eDREGTypeWATCH, i); 694 695 if (error.Fail()) { 696 m_hwp_regs[i].control = tempControl; 697 m_hwp_regs[i].address = tempAddr; 698 m_hwp_regs[i].refcount = tempRefCount; 699 700 return error; 701 } 702 } 703 } 704 705 return Error(); 706 } 707 708 uint32_t NativeRegisterContextLinux_arm::GetWatchpointSize(uint32_t wp_index) { 709 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 710 711 if (log) 712 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 713 714 switch ((m_hwp_regs[wp_index].control >> 5) & 0x0f) { 715 case 0x01: 716 return 1; 717 case 0x03: 718 return 2; 719 case 0x07: 720 return 3; 721 case 0x0f: 722 return 4; 723 default: 724 return 0; 725 } 726 } 727 bool NativeRegisterContextLinux_arm::WatchpointIsEnabled(uint32_t wp_index) { 728 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 729 730 if (log) 731 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 732 733 if ((m_hwp_regs[wp_index].control & 0x1) == 0x1) 734 return true; 735 else 736 return false; 737 } 738 739 Error NativeRegisterContextLinux_arm::GetWatchpointHitIndex( 740 uint32_t &wp_index, lldb::addr_t trap_addr) { 741 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 742 743 if (log) 744 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 745 746 uint32_t watch_size; 747 lldb::addr_t watch_addr; 748 749 for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) { 750 watch_size = GetWatchpointSize(wp_index); 751 watch_addr = m_hwp_regs[wp_index].address; 752 753 if (m_hwp_regs[wp_index].refcount >= 1 && WatchpointIsEnabled(wp_index) && 754 trap_addr >= watch_addr && trap_addr < watch_addr + watch_size) { 755 m_hwp_regs[wp_index].hit_addr = trap_addr; 756 return Error(); 757 } 758 } 759 760 wp_index = LLDB_INVALID_INDEX32; 761 return Error(); 762 } 763 764 lldb::addr_t 765 NativeRegisterContextLinux_arm::GetWatchpointAddress(uint32_t wp_index) { 766 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 767 768 if (log) 769 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 770 771 if (wp_index >= m_max_hwp_supported) 772 return LLDB_INVALID_ADDRESS; 773 774 if (WatchpointIsEnabled(wp_index)) 775 return m_hwp_regs[wp_index].real_addr; 776 else 777 return LLDB_INVALID_ADDRESS; 778 } 779 780 lldb::addr_t 781 NativeRegisterContextLinux_arm::GetWatchpointHitAddress(uint32_t wp_index) { 782 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 783 784 if (log) 785 log->Printf("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); 786 787 if (wp_index >= m_max_hwp_supported) 788 return LLDB_INVALID_ADDRESS; 789 790 if (WatchpointIsEnabled(wp_index)) 791 return m_hwp_regs[wp_index].hit_addr; 792 else 793 return LLDB_INVALID_ADDRESS; 794 } 795 796 Error NativeRegisterContextLinux_arm::ReadHardwareDebugInfo() { 797 Error error; 798 799 if (!m_refresh_hwdebug_info) { 800 return Error(); 801 } 802 803 unsigned int cap_val; 804 805 error = NativeProcessLinux::PtraceWrapper(PTRACE_GETHBPREGS, m_thread.GetID(), 806 nullptr, &cap_val, 807 sizeof(unsigned int)); 808 809 if (error.Fail()) 810 return error; 811 812 m_max_hwp_supported = (cap_val >> 8) & 0xff; 813 m_max_hbp_supported = cap_val & 0xff; 814 m_refresh_hwdebug_info = false; 815 816 return error; 817 } 818 819 Error NativeRegisterContextLinux_arm::WriteHardwareDebugRegs(int hwbType, 820 int hwb_index) { 821 Error error; 822 823 lldb::addr_t *addr_buf; 824 uint32_t *ctrl_buf; 825 826 if (hwbType == eDREGTypeWATCH) { 827 addr_buf = &m_hwp_regs[hwb_index].address; 828 ctrl_buf = &m_hwp_regs[hwb_index].control; 829 830 error = NativeProcessLinux::PtraceWrapper( 831 PTRACE_SETHBPREGS, m_thread.GetID(), 832 (PTRACE_TYPE_ARG3)(intptr_t) - ((hwb_index << 1) + 1), addr_buf, 833 sizeof(unsigned int)); 834 835 if (error.Fail()) 836 return error; 837 838 error = NativeProcessLinux::PtraceWrapper( 839 PTRACE_SETHBPREGS, m_thread.GetID(), 840 (PTRACE_TYPE_ARG3)(intptr_t) - ((hwb_index << 1) + 2), ctrl_buf, 841 sizeof(unsigned int)); 842 } else { 843 addr_buf = &m_hwp_regs[hwb_index].address; 844 ctrl_buf = &m_hwp_regs[hwb_index].control; 845 846 error = NativeProcessLinux::PtraceWrapper( 847 PTRACE_SETHBPREGS, m_thread.GetID(), 848 (PTRACE_TYPE_ARG3)(intptr_t)((hwb_index << 1) + 1), addr_buf, 849 sizeof(unsigned int)); 850 851 if (error.Fail()) 852 return error; 853 854 error = NativeProcessLinux::PtraceWrapper( 855 PTRACE_SETHBPREGS, m_thread.GetID(), 856 (PTRACE_TYPE_ARG3)(intptr_t)((hwb_index << 1) + 2), ctrl_buf, 857 sizeof(unsigned int)); 858 } 859 860 return error; 861 } 862 863 uint32_t NativeRegisterContextLinux_arm::CalculateFprOffset( 864 const RegisterInfo *reg_info) const { 865 return reg_info->byte_offset - 866 GetRegisterInfoAtIndex(m_reg_info.first_fpr)->byte_offset; 867 } 868 869 Error NativeRegisterContextLinux_arm::DoReadRegisterValue( 870 uint32_t offset, const char *reg_name, uint32_t size, 871 RegisterValue &value) { 872 // PTRACE_PEEKUSER don't work in the aarch64 linux kernel used on android 873 // devices (always return 874 // "Bad address"). To avoid using PTRACE_PEEKUSER we read out the full GPR 875 // register set instead. 876 // This approach is about 4 times slower but the performance overhead is 877 // negligible in 878 // comparision to processing time in lldb-server. 879 assert(offset % 4 == 0 && "Try to write a register with unaligned offset"); 880 if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm)) 881 return Error("Register isn't fit into the size of the GPR area"); 882 883 Error error = DoReadGPR(m_gpr_arm, sizeof(m_gpr_arm)); 884 if (error.Fail()) 885 return error; 886 887 value.SetUInt32(m_gpr_arm[offset / sizeof(uint32_t)]); 888 return Error(); 889 } 890 891 Error NativeRegisterContextLinux_arm::DoWriteRegisterValue( 892 uint32_t offset, const char *reg_name, const RegisterValue &value) { 893 // PTRACE_POKEUSER don't work in the aarch64 linux kernel used on android 894 // devices (always return 895 // "Bad address"). To avoid using PTRACE_POKEUSER we read out the full GPR 896 // register set, modify 897 // the requested register and write it back. This approach is about 4 times 898 // slower but the 899 // performance overhead is negligible in comparision to processing time in 900 // lldb-server. 901 assert(offset % 4 == 0 && "Try to write a register with unaligned offset"); 902 if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm)) 903 return Error("Register isn't fit into the size of the GPR area"); 904 905 Error error = DoReadGPR(m_gpr_arm, sizeof(m_gpr_arm)); 906 if (error.Fail()) 907 return error; 908 909 uint32_t reg_value = value.GetAsUInt32(); 910 // As precaution for an undefined behavior encountered while setting PC we 911 // will clear thumb bit of new PC if we are already in thumb mode; that is 912 // CPSR thumb mode bit is set. 913 if (offset / sizeof(uint32_t) == gpr_pc_arm) { 914 // Check if we are already in thumb mode and 915 // thumb bit of current PC is read out to be zero and 916 // thumb bit of next PC is read out to be one. 917 if ((m_gpr_arm[gpr_cpsr_arm] & 0x20) && !(m_gpr_arm[gpr_pc_arm] & 0x01) && 918 (value.GetAsUInt32() & 0x01)) { 919 reg_value &= (~1ull); 920 } 921 } 922 923 m_gpr_arm[offset / sizeof(uint32_t)] = reg_value; 924 return DoWriteGPR(m_gpr_arm, sizeof(m_gpr_arm)); 925 } 926 927 Error NativeRegisterContextLinux_arm::DoReadGPR(void *buf, size_t buf_size) { 928 #ifdef __arm__ 929 return NativeRegisterContextLinux::DoReadGPR(buf, buf_size); 930 #else // __aarch64__ 931 struct iovec ioVec; 932 ioVec.iov_base = buf; 933 ioVec.iov_len = buf_size; 934 935 return ReadRegisterSet(&ioVec, buf_size, NT_PRSTATUS); 936 #endif // __arm__ 937 } 938 939 Error NativeRegisterContextLinux_arm::DoWriteGPR(void *buf, size_t buf_size) { 940 #ifdef __arm__ 941 return NativeRegisterContextLinux::DoWriteGPR(buf, buf_size); 942 #else // __aarch64__ 943 struct iovec ioVec; 944 ioVec.iov_base = buf; 945 ioVec.iov_len = buf_size; 946 947 return WriteRegisterSet(&ioVec, buf_size, NT_PRSTATUS); 948 #endif // __arm__ 949 } 950 951 Error NativeRegisterContextLinux_arm::DoReadFPR(void *buf, size_t buf_size) { 952 #ifdef __arm__ 953 return NativeProcessLinux::PtraceWrapper(PTRACE_GETVFPREGS, m_thread.GetID(), 954 nullptr, buf, buf_size); 955 #else // __aarch64__ 956 struct iovec ioVec; 957 ioVec.iov_base = buf; 958 ioVec.iov_len = buf_size; 959 960 return ReadRegisterSet(&ioVec, buf_size, NT_ARM_VFP); 961 #endif // __arm__ 962 } 963 964 Error NativeRegisterContextLinux_arm::DoWriteFPR(void *buf, size_t buf_size) { 965 #ifdef __arm__ 966 return NativeProcessLinux::PtraceWrapper(PTRACE_SETVFPREGS, m_thread.GetID(), 967 nullptr, buf, buf_size); 968 #else // __aarch64__ 969 struct iovec ioVec; 970 ioVec.iov_base = buf; 971 ioVec.iov_len = buf_size; 972 973 return WriteRegisterSet(&ioVec, buf_size, NT_ARM_VFP); 974 #endif // __arm__ 975 } 976 977 #endif // defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 978