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 if (!data_sp) 358 return Status("failed to allocate DataBufferHeap instance of size %" PRIu64, 359 REG_CONTEXT_SIZE); 360 361 error = ReadGPR(); 362 if (error.Fail()) 363 return error; 364 365 error = ReadFPR(); 366 if (error.Fail()) 367 return error; 368 369 error = ReadVMX(); 370 if (error.Fail()) 371 return error; 372 373 error = ReadVSX(); 374 if (error.Fail()) 375 return error; 376 377 uint8_t *dst = data_sp->GetBytes(); 378 if (dst == nullptr) { 379 error.SetErrorStringWithFormat("DataBufferHeap instance of size %" PRIu64 380 " returned a null pointer", 381 REG_CONTEXT_SIZE); 382 return error; 383 } 384 385 ::memcpy(dst, &m_gpr_ppc64le, GetGPRSize()); 386 dst += GetGPRSize(); 387 ::memcpy(dst, &m_fpr_ppc64le, GetFPRSize()); 388 dst += GetFPRSize(); 389 ::memcpy(dst, &m_vmx_ppc64le, sizeof(m_vmx_ppc64le)); 390 dst += sizeof(m_vmx_ppc64le); 391 ::memcpy(dst, &m_vsx_ppc64le, sizeof(m_vsx_ppc64le)); 392 393 return error; 394 } 395 396 Status NativeRegisterContextLinux_ppc64le::WriteAllRegisterValues( 397 const lldb::DataBufferSP &data_sp) { 398 Status error; 399 400 if (!data_sp) { 401 error.SetErrorStringWithFormat( 402 "NativeRegisterContextLinux_ppc64le::%s invalid data_sp provided", 403 __FUNCTION__); 404 return error; 405 } 406 407 if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) { 408 error.SetErrorStringWithFormat( 409 "NativeRegisterContextLinux_ppc64le::%s data_sp contained mismatched " 410 "data size, expected %" PRIu64 ", actual %" PRIu64, 411 __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize()); 412 return error; 413 } 414 415 uint8_t *src = data_sp->GetBytes(); 416 if (src == nullptr) { 417 error.SetErrorStringWithFormat("NativeRegisterContextLinux_ppc64le::%s " 418 "DataBuffer::GetBytes() returned a null " 419 "pointer", 420 __FUNCTION__); 421 return error; 422 } 423 424 ::memcpy(&m_gpr_ppc64le, src, GetGPRSize()); 425 error = WriteGPR(); 426 427 if (error.Fail()) 428 return error; 429 430 src += GetGPRSize(); 431 ::memcpy(&m_fpr_ppc64le, src, GetFPRSize()); 432 433 error = WriteFPR(); 434 if (error.Fail()) 435 return error; 436 437 src += GetFPRSize(); 438 ::memcpy(&m_vmx_ppc64le, src, sizeof(m_vmx_ppc64le)); 439 440 error = WriteVMX(); 441 if (error.Fail()) 442 return error; 443 444 src += sizeof(m_vmx_ppc64le); 445 ::memcpy(&m_vsx_ppc64le, src, sizeof(m_vsx_ppc64le)); 446 error = WriteVSX(); 447 448 return error; 449 } 450 451 bool NativeRegisterContextLinux_ppc64le::IsGPR(unsigned reg) const { 452 return reg <= k_last_gpr_ppc64le; // GPR's come first. 453 } 454 455 bool NativeRegisterContextLinux_ppc64le::IsFPR(unsigned reg) const { 456 return (k_first_fpr_ppc64le <= reg && reg <= k_last_fpr_ppc64le); 457 } 458 459 Status NativeRegisterContextLinux_ppc64le::DoReadGPR( 460 void *buf, size_t buf_size) { 461 int regset = NT_PRSTATUS; 462 return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGS, m_thread.GetID(), 463 ®set, buf, buf_size); 464 } 465 466 Status NativeRegisterContextLinux_ppc64le::DoWriteGPR( 467 void *buf, size_t buf_size) { 468 int regset = NT_PRSTATUS; 469 return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGS, m_thread.GetID(), 470 ®set, buf, buf_size); 471 } 472 473 Status NativeRegisterContextLinux_ppc64le::DoReadFPR(void *buf, 474 size_t buf_size) { 475 int regset = NT_FPREGSET; 476 return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(), 477 ®set, buf, buf_size); 478 } 479 480 Status NativeRegisterContextLinux_ppc64le::DoWriteFPR(void *buf, 481 size_t buf_size) { 482 int regset = NT_FPREGSET; 483 return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(), 484 ®set, buf, buf_size); 485 } 486 487 uint32_t NativeRegisterContextLinux_ppc64le::CalculateFprOffset( 488 const RegisterInfo *reg_info) const { 489 return reg_info->byte_offset - 490 GetRegisterInfoAtIndex(k_first_fpr_ppc64le)->byte_offset; 491 } 492 493 uint32_t NativeRegisterContextLinux_ppc64le::CalculateVmxOffset( 494 const RegisterInfo *reg_info) const { 495 return reg_info->byte_offset - 496 GetRegisterInfoAtIndex(k_first_vmx_ppc64le)->byte_offset; 497 } 498 499 uint32_t NativeRegisterContextLinux_ppc64le::CalculateVsxOffset( 500 const RegisterInfo *reg_info) const { 501 return reg_info->byte_offset - 502 GetRegisterInfoAtIndex(k_first_vsx_ppc64le)->byte_offset; 503 } 504 505 Status NativeRegisterContextLinux_ppc64le::ReadVMX() { 506 int regset = NT_PPC_VMX; 507 return NativeProcessLinux::PtraceWrapper(PTRACE_GETVRREGS, m_thread.GetID(), 508 ®set, &m_vmx_ppc64le, 509 sizeof(m_vmx_ppc64le)); 510 } 511 512 Status NativeRegisterContextLinux_ppc64le::WriteVMX() { 513 int regset = NT_PPC_VMX; 514 return NativeProcessLinux::PtraceWrapper(PTRACE_SETVRREGS, m_thread.GetID(), 515 ®set, &m_vmx_ppc64le, 516 sizeof(m_vmx_ppc64le)); 517 } 518 519 Status NativeRegisterContextLinux_ppc64le::ReadVSX() { 520 int regset = NT_PPC_VSX; 521 return NativeProcessLinux::PtraceWrapper(PTRACE_GETVSRREGS, m_thread.GetID(), 522 ®set, &m_vsx_ppc64le, 523 sizeof(m_vsx_ppc64le)); 524 } 525 526 Status NativeRegisterContextLinux_ppc64le::WriteVSX() { 527 int regset = NT_PPC_VSX; 528 return NativeProcessLinux::PtraceWrapper(PTRACE_SETVSRREGS, m_thread.GetID(), 529 ®set, &m_vsx_ppc64le, 530 sizeof(m_vsx_ppc64le)); 531 } 532 533 bool NativeRegisterContextLinux_ppc64le::IsVMX(unsigned reg) { 534 return (reg >= k_first_vmx_ppc64le) && (reg <= k_last_vmx_ppc64le); 535 } 536 537 bool NativeRegisterContextLinux_ppc64le::IsVSX(unsigned reg) { 538 return (reg >= k_first_vsx_ppc64le) && (reg <= k_last_vsx_ppc64le); 539 } 540 541 uint32_t NativeRegisterContextLinux_ppc64le::NumSupportedHardwareWatchpoints() { 542 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 543 544 // Read hardware breakpoint and watchpoint information. 545 Status error = ReadHardwareDebugInfo(); 546 547 if (error.Fail()) 548 return 0; 549 550 LLDB_LOG(log, "{0}", m_max_hwp_supported); 551 return m_max_hwp_supported; 552 } 553 554 uint32_t NativeRegisterContextLinux_ppc64le::SetHardwareWatchpoint( 555 lldb::addr_t addr, size_t size, uint32_t watch_flags) { 556 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 557 LLDB_LOG(log, "addr: {0:x}, size: {1:x} watch_flags: {2:x}", addr, size, 558 watch_flags); 559 560 // Read hardware breakpoint and watchpoint information. 561 Status error = ReadHardwareDebugInfo(); 562 563 if (error.Fail()) 564 return LLDB_INVALID_INDEX32; 565 566 uint32_t control_value = 0, wp_index = 0; 567 lldb::addr_t real_addr = addr; 568 uint32_t rw_mode = 0; 569 570 // Check if we are setting watchpoint other than read/write/access Update 571 // watchpoint flag to match ppc64le write-read bit configuration. 572 switch (watch_flags) { 573 case eWatchpointKindWrite: 574 rw_mode = PPC_BREAKPOINT_TRIGGER_WRITE; 575 watch_flags = 2; 576 break; 577 case eWatchpointKindRead: 578 rw_mode = PPC_BREAKPOINT_TRIGGER_READ; 579 watch_flags = 1; 580 break; 581 case (eWatchpointKindRead | eWatchpointKindWrite): 582 rw_mode = PPC_BREAKPOINT_TRIGGER_RW; 583 break; 584 default: 585 return LLDB_INVALID_INDEX32; 586 } 587 588 // Check if size has a valid hardware watchpoint length. 589 if (size != 1 && size != 2 && size != 4 && size != 8) 590 return LLDB_INVALID_INDEX32; 591 592 // Check 8-byte alignment for hardware watchpoint target address. Below is a 593 // hack to recalculate address and size in order to make sure we can watch 594 // non 8-byte alligned addresses as well. 595 if (addr & 0x07) { 596 597 addr_t begin = llvm::alignDown(addr, 8); 598 addr_t end = llvm::alignTo(addr + size, 8); 599 size = llvm::PowerOf2Ceil(end - begin); 600 601 addr = addr & (~0x07); 602 } 603 604 // Setup control value 605 control_value = watch_flags << 3; 606 control_value |= ((1 << size) - 1) << 5; 607 control_value |= (2 << 1) | 1; 608 609 // Iterate over stored watchpoints and find a free wp_index 610 wp_index = LLDB_INVALID_INDEX32; 611 for (uint32_t i = 0; i < m_max_hwp_supported; i++) { 612 if ((m_hwp_regs[i].control & 1) == 0) { 613 wp_index = i; // Mark last free slot 614 } else if (m_hwp_regs[i].address == addr) { 615 return LLDB_INVALID_INDEX32; // We do not support duplicate watchpoints. 616 } 617 } 618 619 if (wp_index == LLDB_INVALID_INDEX32) 620 return LLDB_INVALID_INDEX32; 621 622 // Update watchpoint in local cache 623 m_hwp_regs[wp_index].real_addr = real_addr; 624 m_hwp_regs[wp_index].address = addr; 625 m_hwp_regs[wp_index].control = control_value; 626 m_hwp_regs[wp_index].mode = rw_mode; 627 628 // PTRACE call to set corresponding watchpoint register. 629 error = WriteHardwareDebugRegs(); 630 631 if (error.Fail()) { 632 m_hwp_regs[wp_index].address = 0; 633 m_hwp_regs[wp_index].control &= llvm::maskTrailingZeros<uint32_t>(1); 634 635 return LLDB_INVALID_INDEX32; 636 } 637 638 return wp_index; 639 } 640 641 bool NativeRegisterContextLinux_ppc64le::ClearHardwareWatchpoint( 642 uint32_t wp_index) { 643 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 644 LLDB_LOG(log, "wp_index: {0}", wp_index); 645 646 // Read hardware breakpoint and watchpoint information. 647 Status error = ReadHardwareDebugInfo(); 648 649 if (error.Fail()) 650 return false; 651 652 if (wp_index >= m_max_hwp_supported) 653 return false; 654 655 // Create a backup we can revert to in case of failure. 656 lldb::addr_t tempAddr = m_hwp_regs[wp_index].address; 657 uint32_t tempControl = m_hwp_regs[wp_index].control; 658 long *tempSlot = reinterpret_cast<long *>(m_hwp_regs[wp_index].slot); 659 660 // Update watchpoint in local cache 661 m_hwp_regs[wp_index].control &= llvm::maskTrailingZeros<uint32_t>(1); 662 m_hwp_regs[wp_index].address = 0; 663 m_hwp_regs[wp_index].slot = 0; 664 m_hwp_regs[wp_index].mode = 0; 665 666 // Ptrace call to update hardware debug registers 667 error = NativeProcessLinux::PtraceWrapper(PPC_PTRACE_DELHWDEBUG, 668 m_thread.GetID(), 0, tempSlot); 669 670 if (error.Fail()) { 671 m_hwp_regs[wp_index].control = tempControl; 672 m_hwp_regs[wp_index].address = tempAddr; 673 m_hwp_regs[wp_index].slot = reinterpret_cast<long>(tempSlot); 674 675 return false; 676 } 677 678 return true; 679 } 680 681 uint32_t 682 NativeRegisterContextLinux_ppc64le::GetWatchpointSize(uint32_t wp_index) { 683 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 684 LLDB_LOG(log, "wp_index: {0}", wp_index); 685 686 unsigned control = (m_hwp_regs[wp_index].control >> 5) & 0xff; 687 if (llvm::isPowerOf2_32(control + 1)) { 688 return llvm::countPopulation(control); 689 } 690 691 return 0; 692 } 693 694 bool NativeRegisterContextLinux_ppc64le::WatchpointIsEnabled( 695 uint32_t wp_index) { 696 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 697 LLDB_LOG(log, "wp_index: {0}", wp_index); 698 699 return !!((m_hwp_regs[wp_index].control & 0x1) == 0x1); 700 } 701 702 Status NativeRegisterContextLinux_ppc64le::GetWatchpointHitIndex( 703 uint32_t &wp_index, lldb::addr_t trap_addr) { 704 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 705 LLDB_LOG(log, "wp_index: {0}, trap_addr: {1:x}", wp_index, trap_addr); 706 707 uint32_t watch_size; 708 lldb::addr_t watch_addr; 709 710 for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) { 711 watch_size = GetWatchpointSize(wp_index); 712 watch_addr = m_hwp_regs[wp_index].address; 713 714 if (WatchpointIsEnabled(wp_index) && trap_addr >= watch_addr && 715 trap_addr <= watch_addr + watch_size) { 716 m_hwp_regs[wp_index].hit_addr = trap_addr; 717 return Status(); 718 } 719 } 720 721 wp_index = LLDB_INVALID_INDEX32; 722 return Status(); 723 } 724 725 lldb::addr_t 726 NativeRegisterContextLinux_ppc64le::GetWatchpointAddress(uint32_t wp_index) { 727 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 728 LLDB_LOG(log, "wp_index: {0}", wp_index); 729 730 if (wp_index >= m_max_hwp_supported) 731 return LLDB_INVALID_ADDRESS; 732 733 if (WatchpointIsEnabled(wp_index)) 734 return m_hwp_regs[wp_index].real_addr; 735 else 736 return LLDB_INVALID_ADDRESS; 737 } 738 739 lldb::addr_t 740 NativeRegisterContextLinux_ppc64le::GetWatchpointHitAddress(uint32_t wp_index) { 741 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 742 LLDB_LOG(log, "wp_index: {0}", wp_index); 743 744 if (wp_index >= m_max_hwp_supported) 745 return LLDB_INVALID_ADDRESS; 746 747 if (WatchpointIsEnabled(wp_index)) 748 return m_hwp_regs[wp_index].hit_addr; 749 750 return LLDB_INVALID_ADDRESS; 751 } 752 753 Status NativeRegisterContextLinux_ppc64le::ReadHardwareDebugInfo() { 754 if (!m_refresh_hwdebug_info) { 755 return Status(); 756 } 757 758 ::pid_t tid = m_thread.GetID(); 759 760 struct ppc_debug_info hwdebug_info; 761 Status error; 762 763 error = NativeProcessLinux::PtraceWrapper( 764 PPC_PTRACE_GETHWDBGINFO, tid, 0, &hwdebug_info, sizeof(hwdebug_info)); 765 766 if (error.Fail()) 767 return error; 768 769 m_max_hwp_supported = hwdebug_info.num_data_bps; 770 m_max_hbp_supported = hwdebug_info.num_instruction_bps; 771 m_refresh_hwdebug_info = false; 772 773 return error; 774 } 775 776 Status NativeRegisterContextLinux_ppc64le::WriteHardwareDebugRegs() { 777 struct ppc_hw_breakpoint reg_state; 778 Status error; 779 long ret; 780 781 for (uint32_t i = 0; i < m_max_hwp_supported; i++) { 782 reg_state.addr = m_hwp_regs[i].address; 783 reg_state.trigger_type = m_hwp_regs[i].mode; 784 reg_state.version = 1; 785 reg_state.addr_mode = PPC_BREAKPOINT_MODE_EXACT; 786 reg_state.condition_mode = PPC_BREAKPOINT_CONDITION_NONE; 787 reg_state.addr2 = 0; 788 reg_state.condition_value = 0; 789 790 error = NativeProcessLinux::PtraceWrapper(PPC_PTRACE_SETHWDEBUG, 791 m_thread.GetID(), 0, ®_state, 792 sizeof(reg_state), &ret); 793 794 if (error.Fail()) 795 return error; 796 797 m_hwp_regs[i].slot = ret; 798 } 799 800 return error; 801 } 802 803 #endif // defined(__powerpc64__) 804