1 //===-- NativeRegisterContextLinux_ppc64le.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 // This implementation is related to the OpenPOWER ABI for Power Architecture 10 // 64-bit ELF V2 ABI 11 12 #if defined(__powerpc64__) 13 14 #include "NativeRegisterContextLinux_ppc64le.h" 15 16 #include "lldb/Host/common/NativeProcessProtocol.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 "Plugins/Process/Linux/NativeProcessLinux.h" 23 #include "Plugins/Process/Linux/Procfs.h" 24 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 25 #include "Plugins/Process/Utility/RegisterInfoPOSIX_ppc64le.h" 26 27 // System includes - They have to be included after framework includes because 28 // they define some macros which collide with variable names in other modules 29 #include <sys/socket.h> 30 #include <elf.h> 31 #include <asm/ptrace.h> 32 33 #define REG_CONTEXT_SIZE \ 34 (GetGPRSize() + GetFPRSize() + sizeof(m_vmx_ppc64le) + sizeof(m_vsx_ppc64le)) 35 using namespace lldb; 36 using namespace lldb_private; 37 using namespace lldb_private::process_linux; 38 39 static const uint32_t g_gpr_regnums_ppc64le[] = { 40 gpr_r0_ppc64le, gpr_r1_ppc64le, gpr_r2_ppc64le, gpr_r3_ppc64le, 41 gpr_r4_ppc64le, gpr_r5_ppc64le, gpr_r6_ppc64le, gpr_r7_ppc64le, 42 gpr_r8_ppc64le, gpr_r9_ppc64le, gpr_r10_ppc64le, gpr_r11_ppc64le, 43 gpr_r12_ppc64le, gpr_r13_ppc64le, gpr_r14_ppc64le, gpr_r15_ppc64le, 44 gpr_r16_ppc64le, gpr_r17_ppc64le, gpr_r18_ppc64le, gpr_r19_ppc64le, 45 gpr_r20_ppc64le, gpr_r21_ppc64le, gpr_r22_ppc64le, gpr_r23_ppc64le, 46 gpr_r24_ppc64le, gpr_r25_ppc64le, gpr_r26_ppc64le, gpr_r27_ppc64le, 47 gpr_r28_ppc64le, gpr_r29_ppc64le, gpr_r30_ppc64le, gpr_r31_ppc64le, 48 gpr_pc_ppc64le, gpr_msr_ppc64le, gpr_origr3_ppc64le, gpr_ctr_ppc64le, 49 gpr_lr_ppc64le, gpr_xer_ppc64le, gpr_cr_ppc64le, gpr_softe_ppc64le, 50 gpr_trap_ppc64le, 51 LLDB_INVALID_REGNUM // register sets need to end with this flag 52 }; 53 54 static const uint32_t g_fpr_regnums_ppc64le[] = { 55 fpr_f0_ppc64le, fpr_f1_ppc64le, fpr_f2_ppc64le, fpr_f3_ppc64le, 56 fpr_f4_ppc64le, fpr_f5_ppc64le, fpr_f6_ppc64le, fpr_f7_ppc64le, 57 fpr_f8_ppc64le, fpr_f9_ppc64le, fpr_f10_ppc64le, fpr_f11_ppc64le, 58 fpr_f12_ppc64le, fpr_f13_ppc64le, fpr_f14_ppc64le, fpr_f15_ppc64le, 59 fpr_f16_ppc64le, fpr_f17_ppc64le, fpr_f18_ppc64le, fpr_f19_ppc64le, 60 fpr_f20_ppc64le, fpr_f21_ppc64le, fpr_f22_ppc64le, fpr_f23_ppc64le, 61 fpr_f24_ppc64le, fpr_f25_ppc64le, fpr_f26_ppc64le, fpr_f27_ppc64le, 62 fpr_f28_ppc64le, fpr_f29_ppc64le, fpr_f30_ppc64le, fpr_f31_ppc64le, 63 fpr_fpscr_ppc64le, 64 LLDB_INVALID_REGNUM // register sets need to end with this flag 65 }; 66 67 static const uint32_t g_vmx_regnums_ppc64le[] = { 68 vmx_vr0_ppc64le, vmx_vr1_ppc64le, vmx_vr2_ppc64le, vmx_vr3_ppc64le, 69 vmx_vr4_ppc64le, vmx_vr5_ppc64le, vmx_vr6_ppc64le, vmx_vr7_ppc64le, 70 vmx_vr8_ppc64le, vmx_vr9_ppc64le, vmx_vr10_ppc64le, vmx_vr11_ppc64le, 71 vmx_vr12_ppc64le, vmx_vr13_ppc64le, vmx_vr14_ppc64le, vmx_vr15_ppc64le, 72 vmx_vr16_ppc64le, vmx_vr17_ppc64le, vmx_vr18_ppc64le, vmx_vr19_ppc64le, 73 vmx_vr20_ppc64le, vmx_vr21_ppc64le, vmx_vr22_ppc64le, vmx_vr23_ppc64le, 74 vmx_vr24_ppc64le, vmx_vr25_ppc64le, vmx_vr26_ppc64le, vmx_vr27_ppc64le, 75 vmx_vr28_ppc64le, vmx_vr29_ppc64le, vmx_vr30_ppc64le, vmx_vr31_ppc64le, 76 vmx_vscr_ppc64le, vmx_vrsave_ppc64le, 77 LLDB_INVALID_REGNUM // register sets need to end with this flag 78 }; 79 80 static const uint32_t g_vsx_regnums_ppc64le[] = { 81 vsx_vs0_ppc64le, vsx_vs1_ppc64le, vsx_vs2_ppc64le, vsx_vs3_ppc64le, 82 vsx_vs4_ppc64le, vsx_vs5_ppc64le, vsx_vs6_ppc64le, vsx_vs7_ppc64le, 83 vsx_vs8_ppc64le, vsx_vs9_ppc64le, vsx_vs10_ppc64le, vsx_vs11_ppc64le, 84 vsx_vs12_ppc64le, vsx_vs13_ppc64le, vsx_vs14_ppc64le, vsx_vs15_ppc64le, 85 vsx_vs16_ppc64le, vsx_vs17_ppc64le, vsx_vs18_ppc64le, vsx_vs19_ppc64le, 86 vsx_vs20_ppc64le, vsx_vs21_ppc64le, vsx_vs22_ppc64le, vsx_vs23_ppc64le, 87 vsx_vs24_ppc64le, vsx_vs25_ppc64le, vsx_vs26_ppc64le, vsx_vs27_ppc64le, 88 vsx_vs28_ppc64le, vsx_vs29_ppc64le, vsx_vs30_ppc64le, vsx_vs31_ppc64le, 89 vsx_vs32_ppc64le, vsx_vs33_ppc64le, vsx_vs34_ppc64le, vsx_vs35_ppc64le, 90 vsx_vs36_ppc64le, vsx_vs37_ppc64le, vsx_vs38_ppc64le, vsx_vs39_ppc64le, 91 vsx_vs40_ppc64le, vsx_vs41_ppc64le, vsx_vs42_ppc64le, vsx_vs43_ppc64le, 92 vsx_vs44_ppc64le, vsx_vs45_ppc64le, vsx_vs46_ppc64le, vsx_vs47_ppc64le, 93 vsx_vs48_ppc64le, vsx_vs49_ppc64le, vsx_vs50_ppc64le, vsx_vs51_ppc64le, 94 vsx_vs52_ppc64le, vsx_vs53_ppc64le, vsx_vs54_ppc64le, vsx_vs55_ppc64le, 95 vsx_vs56_ppc64le, vsx_vs57_ppc64le, vsx_vs58_ppc64le, vsx_vs59_ppc64le, 96 vsx_vs60_ppc64le, vsx_vs61_ppc64le, vsx_vs62_ppc64le, vsx_vs63_ppc64le, 97 LLDB_INVALID_REGNUM // register sets need to end with this flag 98 }; 99 100 namespace { 101 // Number of register sets provided by this context. 102 enum { k_num_register_sets = 4 }; 103 } 104 105 static const RegisterSet g_reg_sets_ppc64le[k_num_register_sets] = { 106 {"General Purpose Registers", "gpr", k_num_gpr_registers_ppc64le, 107 g_gpr_regnums_ppc64le}, 108 {"Floating Point Registers", "fpr", k_num_fpr_registers_ppc64le, 109 g_fpr_regnums_ppc64le}, 110 {"AltiVec/VMX Registers", "vmx", k_num_vmx_registers_ppc64le, 111 g_vmx_regnums_ppc64le}, 112 {"VSX Registers", "vsx", k_num_vsx_registers_ppc64le, 113 g_vsx_regnums_ppc64le}, 114 }; 115 116 std::unique_ptr<NativeRegisterContextLinux> 117 NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux( 118 const ArchSpec &target_arch, NativeThreadProtocol &native_thread) { 119 switch (target_arch.GetMachine()) { 120 case llvm::Triple::ppc64le: 121 return llvm::make_unique<NativeRegisterContextLinux_ppc64le>(target_arch, 122 native_thread); 123 default: 124 llvm_unreachable("have no register context for architecture"); 125 } 126 } 127 128 NativeRegisterContextLinux_ppc64le::NativeRegisterContextLinux_ppc64le( 129 const ArchSpec &target_arch, NativeThreadProtocol &native_thread) 130 : NativeRegisterContextLinux(native_thread, 131 new RegisterInfoPOSIX_ppc64le(target_arch)) { 132 if (target_arch.GetMachine() != llvm::Triple::ppc64le) { 133 llvm_unreachable("Unhandled target architecture."); 134 } 135 136 ::memset(&m_gpr_ppc64le, 0, sizeof(m_gpr_ppc64le)); 137 ::memset(&m_fpr_ppc64le, 0, sizeof(m_fpr_ppc64le)); 138 ::memset(&m_vmx_ppc64le, 0, sizeof(m_vmx_ppc64le)); 139 ::memset(&m_vsx_ppc64le, 0, sizeof(m_vsx_ppc64le)); 140 ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs)); 141 } 142 143 uint32_t NativeRegisterContextLinux_ppc64le::GetRegisterSetCount() const { 144 return k_num_register_sets; 145 } 146 147 const RegisterSet * 148 NativeRegisterContextLinux_ppc64le::GetRegisterSet(uint32_t set_index) const { 149 if (set_index < k_num_register_sets) 150 return &g_reg_sets_ppc64le[set_index]; 151 152 return nullptr; 153 } 154 155 uint32_t NativeRegisterContextLinux_ppc64le::GetUserRegisterCount() const { 156 uint32_t count = 0; 157 for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) 158 count += g_reg_sets_ppc64le[set_index].num_registers; 159 return count; 160 } 161 162 Status NativeRegisterContextLinux_ppc64le::ReadRegister( 163 const RegisterInfo *reg_info, RegisterValue ®_value) { 164 Status error; 165 166 if (!reg_info) { 167 error.SetErrorString("reg_info NULL"); 168 return error; 169 } 170 171 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB]; 172 173 if (IsFPR(reg)) { 174 error = ReadFPR(); 175 if (error.Fail()) 176 return error; 177 178 // Get pointer to m_fpr_ppc64le variable and set the data from it. 179 uint32_t fpr_offset = CalculateFprOffset(reg_info); 180 assert(fpr_offset < sizeof m_fpr_ppc64le); 181 uint8_t *src = (uint8_t *)&m_fpr_ppc64le + fpr_offset; 182 reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size, 183 eByteOrderLittle, error); 184 } else if (IsVSX(reg)) { 185 uint32_t vsx_offset = CalculateVsxOffset(reg_info); 186 assert(vsx_offset < sizeof(m_vsx_ppc64le)); 187 188 if (vsx_offset < sizeof(m_vsx_ppc64le) / 2) { 189 error = ReadVSX(); 190 if (error.Fail()) 191 return error; 192 193 error = ReadFPR(); 194 if (error.Fail()) 195 return error; 196 197 uint64_t value[2]; 198 uint8_t *dst, *src; 199 dst = (uint8_t *)&value; 200 src = (uint8_t *)&m_vsx_ppc64le + vsx_offset / 2; 201 ::memcpy(dst, src, 8); 202 dst += 8; 203 src = (uint8_t *)&m_fpr_ppc64le + vsx_offset / 2; 204 ::memcpy(dst, src, 8); 205 reg_value.SetFromMemoryData(reg_info, &value, reg_info->byte_size, 206 eByteOrderLittle, error); 207 } else { 208 error = ReadVMX(); 209 if (error.Fail()) 210 return error; 211 212 // Get pointer to m_vmx_ppc64le variable and set the data from it. 213 uint32_t vmx_offset = vsx_offset - sizeof(m_vsx_ppc64le) / 2; 214 uint8_t *src = (uint8_t *)&m_vmx_ppc64le + vmx_offset; 215 reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size, 216 eByteOrderLittle, error); 217 } 218 } else if (IsVMX(reg)) { 219 error = ReadVMX(); 220 if (error.Fail()) 221 return error; 222 223 // Get pointer to m_vmx_ppc64le variable and set the data from it. 224 uint32_t vmx_offset = CalculateVmxOffset(reg_info); 225 assert(vmx_offset < sizeof m_vmx_ppc64le); 226 uint8_t *src = (uint8_t *)&m_vmx_ppc64le + vmx_offset; 227 reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size, 228 eByteOrderLittle, error); 229 } else if (IsGPR(reg)) { 230 error = ReadGPR(); 231 if (error.Fail()) 232 return error; 233 234 uint8_t *src = (uint8_t *) &m_gpr_ppc64le + reg_info->byte_offset; 235 reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size, 236 eByteOrderLittle, error); 237 } else { 238 return Status("failed - register wasn't recognized to be a GPR, FPR, VSX " 239 "or VMX, read strategy unknown"); 240 } 241 242 return error; 243 } 244 245 Status NativeRegisterContextLinux_ppc64le::WriteRegister( 246 const RegisterInfo *reg_info, const RegisterValue ®_value) { 247 Status error; 248 if (!reg_info) 249 return Status("reg_info NULL"); 250 251 const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB]; 252 if (reg_index == LLDB_INVALID_REGNUM) 253 return Status("no lldb regnum for %s", reg_info && reg_info->name 254 ? reg_info->name 255 : "<unknown register>"); 256 257 if (IsGPR(reg_index)) { 258 error = ReadGPR(); 259 if (error.Fail()) 260 return error; 261 262 uint8_t *dst = (uint8_t *)&m_gpr_ppc64le + reg_info->byte_offset; 263 ::memcpy(dst, reg_value.GetBytes(), reg_value.GetByteSize()); 264 265 error = WriteGPR(); 266 if (error.Fail()) 267 return error; 268 269 return Status(); 270 } 271 272 if (IsFPR(reg_index)) { 273 error = ReadFPR(); 274 if (error.Fail()) 275 return error; 276 277 // Get pointer to m_fpr_ppc64le variable and set the data to it. 278 uint32_t fpr_offset = CalculateFprOffset(reg_info); 279 assert(fpr_offset < GetFPRSize()); 280 uint8_t *dst = (uint8_t *)&m_fpr_ppc64le + fpr_offset; 281 ::memcpy(dst, reg_value.GetBytes(), reg_value.GetByteSize()); 282 283 error = WriteFPR(); 284 if (error.Fail()) 285 return error; 286 287 return Status(); 288 } 289 290 if (IsVMX(reg_index)) { 291 error = ReadVMX(); 292 if (error.Fail()) 293 return error; 294 295 // Get pointer to m_vmx_ppc64le variable and set the data to it. 296 uint32_t vmx_offset = CalculateVmxOffset(reg_info); 297 assert(vmx_offset < sizeof(m_vmx_ppc64le)); 298 uint8_t *dst = (uint8_t *)&m_vmx_ppc64le + vmx_offset; 299 ::memcpy(dst, reg_value.GetBytes(), reg_value.GetByteSize()); 300 301 error = WriteVMX(); 302 if (error.Fail()) 303 return error; 304 305 return Status(); 306 } 307 308 if (IsVSX(reg_index)) { 309 uint32_t vsx_offset = CalculateVsxOffset(reg_info); 310 assert(vsx_offset < sizeof(m_vsx_ppc64le)); 311 312 if (vsx_offset < sizeof(m_vsx_ppc64le) / 2) { 313 error = ReadVSX(); 314 if (error.Fail()) 315 return error; 316 317 error = ReadFPR(); 318 if (error.Fail()) 319 return error; 320 321 uint64_t value[2]; 322 ::memcpy(value, reg_value.GetBytes(), 16); 323 uint8_t *dst, *src; 324 src = (uint8_t *)value; 325 dst = (uint8_t *)&m_vsx_ppc64le + vsx_offset / 2; 326 ::memcpy(dst, src, 8); 327 src += 8; 328 dst = (uint8_t *)&m_fpr_ppc64le + vsx_offset / 2; 329 ::memcpy(dst, src, 8); 330 331 WriteVSX(); 332 WriteFPR(); 333 } else { 334 error = ReadVMX(); 335 if (error.Fail()) 336 return error; 337 338 // Get pointer to m_vmx_ppc64le variable and set the data from it. 339 uint32_t vmx_offset = vsx_offset - sizeof(m_vsx_ppc64le) / 2; 340 uint8_t *dst = (uint8_t *)&m_vmx_ppc64le + vmx_offset; 341 ::memcpy(dst, reg_value.GetBytes(), reg_value.GetByteSize()); 342 WriteVMX(); 343 } 344 345 return Status(); 346 } 347 348 return Status("failed - register wasn't recognized to be a GPR, FPR, VSX " 349 "or VMX, write strategy unknown"); 350 } 351 352 Status NativeRegisterContextLinux_ppc64le::ReadAllRegisterValues( 353 lldb::DataBufferSP &data_sp) { 354 Status error; 355 356 data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0)); 357 error = ReadGPR(); 358 if (error.Fail()) 359 return error; 360 361 error = ReadFPR(); 362 if (error.Fail()) 363 return error; 364 365 error = ReadVMX(); 366 if (error.Fail()) 367 return error; 368 369 error = ReadVSX(); 370 if (error.Fail()) 371 return error; 372 373 uint8_t *dst = data_sp->GetBytes(); 374 if (dst == nullptr) { 375 error.SetErrorStringWithFormat("DataBufferHeap instance of size %" PRIu64 376 " returned a null pointer", 377 REG_CONTEXT_SIZE); 378 return error; 379 } 380 381 ::memcpy(dst, &m_gpr_ppc64le, GetGPRSize()); 382 dst += GetGPRSize(); 383 ::memcpy(dst, &m_fpr_ppc64le, GetFPRSize()); 384 dst += GetFPRSize(); 385 ::memcpy(dst, &m_vmx_ppc64le, sizeof(m_vmx_ppc64le)); 386 dst += sizeof(m_vmx_ppc64le); 387 ::memcpy(dst, &m_vsx_ppc64le, sizeof(m_vsx_ppc64le)); 388 389 return error; 390 } 391 392 Status NativeRegisterContextLinux_ppc64le::WriteAllRegisterValues( 393 const lldb::DataBufferSP &data_sp) { 394 Status error; 395 396 if (!data_sp) { 397 error.SetErrorStringWithFormat( 398 "NativeRegisterContextLinux_ppc64le::%s invalid data_sp provided", 399 __FUNCTION__); 400 return error; 401 } 402 403 if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) { 404 error.SetErrorStringWithFormat( 405 "NativeRegisterContextLinux_ppc64le::%s data_sp contained mismatched " 406 "data size, expected %" PRIu64 ", actual %" PRIu64, 407 __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize()); 408 return error; 409 } 410 411 uint8_t *src = data_sp->GetBytes(); 412 if (src == nullptr) { 413 error.SetErrorStringWithFormat("NativeRegisterContextLinux_ppc64le::%s " 414 "DataBuffer::GetBytes() returned a null " 415 "pointer", 416 __FUNCTION__); 417 return error; 418 } 419 420 ::memcpy(&m_gpr_ppc64le, src, GetGPRSize()); 421 error = WriteGPR(); 422 423 if (error.Fail()) 424 return error; 425 426 src += GetGPRSize(); 427 ::memcpy(&m_fpr_ppc64le, src, GetFPRSize()); 428 429 error = WriteFPR(); 430 if (error.Fail()) 431 return error; 432 433 src += GetFPRSize(); 434 ::memcpy(&m_vmx_ppc64le, src, sizeof(m_vmx_ppc64le)); 435 436 error = WriteVMX(); 437 if (error.Fail()) 438 return error; 439 440 src += sizeof(m_vmx_ppc64le); 441 ::memcpy(&m_vsx_ppc64le, src, sizeof(m_vsx_ppc64le)); 442 error = WriteVSX(); 443 444 return error; 445 } 446 447 bool NativeRegisterContextLinux_ppc64le::IsGPR(unsigned reg) const { 448 return reg <= k_last_gpr_ppc64le; // GPR's come first. 449 } 450 451 bool NativeRegisterContextLinux_ppc64le::IsFPR(unsigned reg) const { 452 return (k_first_fpr_ppc64le <= reg && reg <= k_last_fpr_ppc64le); 453 } 454 455 Status NativeRegisterContextLinux_ppc64le::DoReadGPR( 456 void *buf, size_t buf_size) { 457 int regset = NT_PRSTATUS; 458 return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGS, m_thread.GetID(), 459 ®set, buf, buf_size); 460 } 461 462 Status NativeRegisterContextLinux_ppc64le::DoWriteGPR( 463 void *buf, size_t buf_size) { 464 int regset = NT_PRSTATUS; 465 return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGS, m_thread.GetID(), 466 ®set, buf, buf_size); 467 } 468 469 Status NativeRegisterContextLinux_ppc64le::DoReadFPR(void *buf, 470 size_t buf_size) { 471 int regset = NT_FPREGSET; 472 return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(), 473 ®set, buf, buf_size); 474 } 475 476 Status NativeRegisterContextLinux_ppc64le::DoWriteFPR(void *buf, 477 size_t buf_size) { 478 int regset = NT_FPREGSET; 479 return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(), 480 ®set, buf, buf_size); 481 } 482 483 uint32_t NativeRegisterContextLinux_ppc64le::CalculateFprOffset( 484 const RegisterInfo *reg_info) const { 485 return reg_info->byte_offset - 486 GetRegisterInfoAtIndex(k_first_fpr_ppc64le)->byte_offset; 487 } 488 489 uint32_t NativeRegisterContextLinux_ppc64le::CalculateVmxOffset( 490 const RegisterInfo *reg_info) const { 491 return reg_info->byte_offset - 492 GetRegisterInfoAtIndex(k_first_vmx_ppc64le)->byte_offset; 493 } 494 495 uint32_t NativeRegisterContextLinux_ppc64le::CalculateVsxOffset( 496 const RegisterInfo *reg_info) const { 497 return reg_info->byte_offset - 498 GetRegisterInfoAtIndex(k_first_vsx_ppc64le)->byte_offset; 499 } 500 501 Status NativeRegisterContextLinux_ppc64le::ReadVMX() { 502 int regset = NT_PPC_VMX; 503 return NativeProcessLinux::PtraceWrapper(PTRACE_GETVRREGS, m_thread.GetID(), 504 ®set, &m_vmx_ppc64le, 505 sizeof(m_vmx_ppc64le)); 506 } 507 508 Status NativeRegisterContextLinux_ppc64le::WriteVMX() { 509 int regset = NT_PPC_VMX; 510 return NativeProcessLinux::PtraceWrapper(PTRACE_SETVRREGS, m_thread.GetID(), 511 ®set, &m_vmx_ppc64le, 512 sizeof(m_vmx_ppc64le)); 513 } 514 515 Status NativeRegisterContextLinux_ppc64le::ReadVSX() { 516 int regset = NT_PPC_VSX; 517 return NativeProcessLinux::PtraceWrapper(PTRACE_GETVSRREGS, m_thread.GetID(), 518 ®set, &m_vsx_ppc64le, 519 sizeof(m_vsx_ppc64le)); 520 } 521 522 Status NativeRegisterContextLinux_ppc64le::WriteVSX() { 523 int regset = NT_PPC_VSX; 524 return NativeProcessLinux::PtraceWrapper(PTRACE_SETVSRREGS, m_thread.GetID(), 525 ®set, &m_vsx_ppc64le, 526 sizeof(m_vsx_ppc64le)); 527 } 528 529 bool NativeRegisterContextLinux_ppc64le::IsVMX(unsigned reg) { 530 return (reg >= k_first_vmx_ppc64le) && (reg <= k_last_vmx_ppc64le); 531 } 532 533 bool NativeRegisterContextLinux_ppc64le::IsVSX(unsigned reg) { 534 return (reg >= k_first_vsx_ppc64le) && (reg <= k_last_vsx_ppc64le); 535 } 536 537 uint32_t NativeRegisterContextLinux_ppc64le::NumSupportedHardwareWatchpoints() { 538 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 539 540 // Read hardware breakpoint and watchpoint information. 541 Status error = ReadHardwareDebugInfo(); 542 543 if (error.Fail()) 544 return 0; 545 546 LLDB_LOG(log, "{0}", m_max_hwp_supported); 547 return m_max_hwp_supported; 548 } 549 550 uint32_t NativeRegisterContextLinux_ppc64le::SetHardwareWatchpoint( 551 lldb::addr_t addr, size_t size, uint32_t watch_flags) { 552 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 553 LLDB_LOG(log, "addr: {0:x}, size: {1:x} watch_flags: {2:x}", addr, size, 554 watch_flags); 555 556 // Read hardware breakpoint and watchpoint information. 557 Status error = ReadHardwareDebugInfo(); 558 559 if (error.Fail()) 560 return LLDB_INVALID_INDEX32; 561 562 uint32_t control_value = 0, wp_index = 0; 563 lldb::addr_t real_addr = addr; 564 uint32_t rw_mode = 0; 565 566 // Check if we are setting watchpoint other than read/write/access Update 567 // watchpoint flag to match ppc64le write-read bit configuration. 568 switch (watch_flags) { 569 case eWatchpointKindWrite: 570 rw_mode = PPC_BREAKPOINT_TRIGGER_WRITE; 571 watch_flags = 2; 572 break; 573 case eWatchpointKindRead: 574 rw_mode = PPC_BREAKPOINT_TRIGGER_READ; 575 watch_flags = 1; 576 break; 577 case (eWatchpointKindRead | eWatchpointKindWrite): 578 rw_mode = PPC_BREAKPOINT_TRIGGER_RW; 579 break; 580 default: 581 return LLDB_INVALID_INDEX32; 582 } 583 584 // Check if size has a valid hardware watchpoint length. 585 if (size != 1 && size != 2 && size != 4 && size != 8) 586 return LLDB_INVALID_INDEX32; 587 588 // Check 8-byte alignment for hardware watchpoint target address. Below is a 589 // hack to recalculate address and size in order to make sure we can watch 590 // non 8-byte alligned addresses as well. 591 if (addr & 0x07) { 592 593 addr_t begin = llvm::alignDown(addr, 8); 594 addr_t end = llvm::alignTo(addr + size, 8); 595 size = llvm::PowerOf2Ceil(end - begin); 596 597 addr = addr & (~0x07); 598 } 599 600 // Setup control value 601 control_value = watch_flags << 3; 602 control_value |= ((1 << size) - 1) << 5; 603 control_value |= (2 << 1) | 1; 604 605 // Iterate over stored watchpoints and find a free wp_index 606 wp_index = LLDB_INVALID_INDEX32; 607 for (uint32_t i = 0; i < m_max_hwp_supported; i++) { 608 if ((m_hwp_regs[i].control & 1) == 0) { 609 wp_index = i; // Mark last free slot 610 } else if (m_hwp_regs[i].address == addr) { 611 return LLDB_INVALID_INDEX32; // We do not support duplicate watchpoints. 612 } 613 } 614 615 if (wp_index == LLDB_INVALID_INDEX32) 616 return LLDB_INVALID_INDEX32; 617 618 // Update watchpoint in local cache 619 m_hwp_regs[wp_index].real_addr = real_addr; 620 m_hwp_regs[wp_index].address = addr; 621 m_hwp_regs[wp_index].control = control_value; 622 m_hwp_regs[wp_index].mode = rw_mode; 623 624 // PTRACE call to set corresponding watchpoint register. 625 error = WriteHardwareDebugRegs(); 626 627 if (error.Fail()) { 628 m_hwp_regs[wp_index].address = 0; 629 m_hwp_regs[wp_index].control &= llvm::maskTrailingZeros<uint32_t>(1); 630 631 return LLDB_INVALID_INDEX32; 632 } 633 634 return wp_index; 635 } 636 637 bool NativeRegisterContextLinux_ppc64le::ClearHardwareWatchpoint( 638 uint32_t wp_index) { 639 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 640 LLDB_LOG(log, "wp_index: {0}", wp_index); 641 642 // Read hardware breakpoint and watchpoint information. 643 Status error = ReadHardwareDebugInfo(); 644 645 if (error.Fail()) 646 return false; 647 648 if (wp_index >= m_max_hwp_supported) 649 return false; 650 651 // Create a backup we can revert to in case of failure. 652 lldb::addr_t tempAddr = m_hwp_regs[wp_index].address; 653 uint32_t tempControl = m_hwp_regs[wp_index].control; 654 long *tempSlot = reinterpret_cast<long *>(m_hwp_regs[wp_index].slot); 655 656 // Update watchpoint in local cache 657 m_hwp_regs[wp_index].control &= llvm::maskTrailingZeros<uint32_t>(1); 658 m_hwp_regs[wp_index].address = 0; 659 m_hwp_regs[wp_index].slot = 0; 660 m_hwp_regs[wp_index].mode = 0; 661 662 // Ptrace call to update hardware debug registers 663 error = NativeProcessLinux::PtraceWrapper(PPC_PTRACE_DELHWDEBUG, 664 m_thread.GetID(), 0, tempSlot); 665 666 if (error.Fail()) { 667 m_hwp_regs[wp_index].control = tempControl; 668 m_hwp_regs[wp_index].address = tempAddr; 669 m_hwp_regs[wp_index].slot = reinterpret_cast<long>(tempSlot); 670 671 return false; 672 } 673 674 return true; 675 } 676 677 uint32_t 678 NativeRegisterContextLinux_ppc64le::GetWatchpointSize(uint32_t wp_index) { 679 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 680 LLDB_LOG(log, "wp_index: {0}", wp_index); 681 682 unsigned control = (m_hwp_regs[wp_index].control >> 5) & 0xff; 683 if (llvm::isPowerOf2_32(control + 1)) { 684 return llvm::countPopulation(control); 685 } 686 687 return 0; 688 } 689 690 bool NativeRegisterContextLinux_ppc64le::WatchpointIsEnabled( 691 uint32_t wp_index) { 692 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 693 LLDB_LOG(log, "wp_index: {0}", wp_index); 694 695 return !!((m_hwp_regs[wp_index].control & 0x1) == 0x1); 696 } 697 698 Status NativeRegisterContextLinux_ppc64le::GetWatchpointHitIndex( 699 uint32_t &wp_index, lldb::addr_t trap_addr) { 700 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 701 LLDB_LOG(log, "wp_index: {0}, trap_addr: {1:x}", wp_index, trap_addr); 702 703 uint32_t watch_size; 704 lldb::addr_t watch_addr; 705 706 for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) { 707 watch_size = GetWatchpointSize(wp_index); 708 watch_addr = m_hwp_regs[wp_index].address; 709 710 if (WatchpointIsEnabled(wp_index) && trap_addr >= watch_addr && 711 trap_addr <= watch_addr + watch_size) { 712 m_hwp_regs[wp_index].hit_addr = trap_addr; 713 return Status(); 714 } 715 } 716 717 wp_index = LLDB_INVALID_INDEX32; 718 return Status(); 719 } 720 721 lldb::addr_t 722 NativeRegisterContextLinux_ppc64le::GetWatchpointAddress(uint32_t wp_index) { 723 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 724 LLDB_LOG(log, "wp_index: {0}", wp_index); 725 726 if (wp_index >= m_max_hwp_supported) 727 return LLDB_INVALID_ADDRESS; 728 729 if (WatchpointIsEnabled(wp_index)) 730 return m_hwp_regs[wp_index].real_addr; 731 else 732 return LLDB_INVALID_ADDRESS; 733 } 734 735 lldb::addr_t 736 NativeRegisterContextLinux_ppc64le::GetWatchpointHitAddress(uint32_t wp_index) { 737 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 738 LLDB_LOG(log, "wp_index: {0}", wp_index); 739 740 if (wp_index >= m_max_hwp_supported) 741 return LLDB_INVALID_ADDRESS; 742 743 if (WatchpointIsEnabled(wp_index)) 744 return m_hwp_regs[wp_index].hit_addr; 745 746 return LLDB_INVALID_ADDRESS; 747 } 748 749 Status NativeRegisterContextLinux_ppc64le::ReadHardwareDebugInfo() { 750 if (!m_refresh_hwdebug_info) { 751 return Status(); 752 } 753 754 ::pid_t tid = m_thread.GetID(); 755 756 struct ppc_debug_info hwdebug_info; 757 Status error; 758 759 error = NativeProcessLinux::PtraceWrapper( 760 PPC_PTRACE_GETHWDBGINFO, tid, 0, &hwdebug_info, sizeof(hwdebug_info)); 761 762 if (error.Fail()) 763 return error; 764 765 m_max_hwp_supported = hwdebug_info.num_data_bps; 766 m_max_hbp_supported = hwdebug_info.num_instruction_bps; 767 m_refresh_hwdebug_info = false; 768 769 return error; 770 } 771 772 Status NativeRegisterContextLinux_ppc64le::WriteHardwareDebugRegs() { 773 struct ppc_hw_breakpoint reg_state; 774 Status error; 775 long ret; 776 777 for (uint32_t i = 0; i < m_max_hwp_supported; i++) { 778 reg_state.addr = m_hwp_regs[i].address; 779 reg_state.trigger_type = m_hwp_regs[i].mode; 780 reg_state.version = 1; 781 reg_state.addr_mode = PPC_BREAKPOINT_MODE_EXACT; 782 reg_state.condition_mode = PPC_BREAKPOINT_CONDITION_NONE; 783 reg_state.addr2 = 0; 784 reg_state.condition_value = 0; 785 786 error = NativeProcessLinux::PtraceWrapper(PPC_PTRACE_SETHWDEBUG, 787 m_thread.GetID(), 0, ®_state, 788 sizeof(reg_state), &ret); 789 790 if (error.Fail()) 791 return error; 792 793 m_hwp_regs[i].slot = ret; 794 } 795 796 return error; 797 } 798 799 #endif // defined(__powerpc64__) 800