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