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(
118d1486e65SPavel Labath     const ArchSpec &target_arch, NativeThreadLinux &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)
130f5ca2756SMichał Górny     : NativeRegisterContextRegisterInfo(
131*e1d4fb1eSPavel Labath           native_thread, new RegisterInfoPOSIX_ppc64le(target_arch)),
132*e1d4fb1eSPavel Labath       NativeRegisterContextLinux(native_thread) {
133aae0a752SEugene Zemtsov   if (target_arch.GetMachine() != llvm::Triple::ppc64le) {
134aae0a752SEugene Zemtsov     llvm_unreachable("Unhandled target architecture.");
135aae0a752SEugene Zemtsov   }
136aae0a752SEugene Zemtsov 
137aae0a752SEugene Zemtsov   ::memset(&m_gpr_ppc64le, 0, sizeof(m_gpr_ppc64le));
138e6a66105SPavel Labath   ::memset(&m_fpr_ppc64le, 0, sizeof(m_fpr_ppc64le));
139e6a66105SPavel Labath   ::memset(&m_vmx_ppc64le, 0, sizeof(m_vmx_ppc64le));
140e6a66105SPavel Labath   ::memset(&m_vsx_ppc64le, 0, sizeof(m_vsx_ppc64le));
141c51ad483SPavel Labath   ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs));
142aae0a752SEugene Zemtsov }
143aae0a752SEugene Zemtsov 
144aae0a752SEugene Zemtsov uint32_t NativeRegisterContextLinux_ppc64le::GetRegisterSetCount() const {
145aae0a752SEugene Zemtsov   return k_num_register_sets;
146aae0a752SEugene Zemtsov }
147aae0a752SEugene Zemtsov 
148aae0a752SEugene Zemtsov const RegisterSet *
149aae0a752SEugene Zemtsov NativeRegisterContextLinux_ppc64le::GetRegisterSet(uint32_t set_index) const {
150aae0a752SEugene Zemtsov   if (set_index < k_num_register_sets)
151aae0a752SEugene Zemtsov     return &g_reg_sets_ppc64le[set_index];
152aae0a752SEugene Zemtsov 
153aae0a752SEugene Zemtsov   return nullptr;
154aae0a752SEugene Zemtsov }
155aae0a752SEugene Zemtsov 
156aae0a752SEugene Zemtsov uint32_t NativeRegisterContextLinux_ppc64le::GetUserRegisterCount() const {
157aae0a752SEugene Zemtsov   uint32_t count = 0;
158aae0a752SEugene Zemtsov   for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index)
159aae0a752SEugene Zemtsov     count += g_reg_sets_ppc64le[set_index].num_registers;
160aae0a752SEugene Zemtsov   return count;
161aae0a752SEugene Zemtsov }
162aae0a752SEugene Zemtsov 
163aae0a752SEugene Zemtsov Status NativeRegisterContextLinux_ppc64le::ReadRegister(
164aae0a752SEugene Zemtsov     const RegisterInfo *reg_info, RegisterValue &reg_value) {
165aae0a752SEugene Zemtsov   Status error;
166aae0a752SEugene Zemtsov 
167aae0a752SEugene Zemtsov   if (!reg_info) {
168aae0a752SEugene Zemtsov     error.SetErrorString("reg_info NULL");
169aae0a752SEugene Zemtsov     return error;
170aae0a752SEugene Zemtsov   }
171aae0a752SEugene Zemtsov 
172aae0a752SEugene Zemtsov   const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
173aae0a752SEugene Zemtsov 
174e6a66105SPavel Labath   if (IsFPR(reg)) {
175e6a66105SPavel Labath     error = ReadFPR();
176e6a66105SPavel Labath     if (error.Fail())
177e6a66105SPavel Labath       return error;
178e6a66105SPavel Labath 
179e6a66105SPavel Labath     // Get pointer to m_fpr_ppc64le variable and set the data from it.
180e6a66105SPavel Labath     uint32_t fpr_offset = CalculateFprOffset(reg_info);
181e6a66105SPavel Labath     assert(fpr_offset < sizeof m_fpr_ppc64le);
182e6a66105SPavel Labath     uint8_t *src = (uint8_t *)&m_fpr_ppc64le + fpr_offset;
183e6a66105SPavel Labath     reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size,
184e6a66105SPavel Labath                                 eByteOrderLittle, error);
185e6a66105SPavel Labath   } else if (IsVSX(reg)) {
186e6a66105SPavel Labath     uint32_t vsx_offset = CalculateVsxOffset(reg_info);
187e6a66105SPavel Labath     assert(vsx_offset < sizeof(m_vsx_ppc64le));
188e6a66105SPavel Labath 
189e6a66105SPavel Labath     if (vsx_offset < sizeof(m_vsx_ppc64le) / 2) {
190e6a66105SPavel Labath       error = ReadVSX();
191e6a66105SPavel Labath       if (error.Fail())
192e6a66105SPavel Labath         return error;
193e6a66105SPavel Labath 
194e6a66105SPavel Labath       error = ReadFPR();
195e6a66105SPavel Labath       if (error.Fail())
196e6a66105SPavel Labath         return error;
197e6a66105SPavel Labath 
198e6a66105SPavel Labath       uint64_t value[2];
199e6a66105SPavel Labath       uint8_t *dst, *src;
200e6a66105SPavel Labath       dst = (uint8_t *)&value;
201e6a66105SPavel Labath       src = (uint8_t *)&m_vsx_ppc64le + vsx_offset / 2;
202e6a66105SPavel Labath       ::memcpy(dst, src, 8);
203e6a66105SPavel Labath       dst += 8;
204e6a66105SPavel Labath       src = (uint8_t *)&m_fpr_ppc64le + vsx_offset / 2;
205e6a66105SPavel Labath       ::memcpy(dst, src, 8);
206e6a66105SPavel Labath       reg_value.SetFromMemoryData(reg_info, &value, reg_info->byte_size,
207e6a66105SPavel Labath                                   eByteOrderLittle, error);
208e6a66105SPavel Labath     } else {
209e6a66105SPavel Labath       error = ReadVMX();
210e6a66105SPavel Labath       if (error.Fail())
211e6a66105SPavel Labath         return error;
212e6a66105SPavel Labath 
213e6a66105SPavel Labath       // Get pointer to m_vmx_ppc64le variable and set the data from it.
214e6a66105SPavel Labath       uint32_t vmx_offset = vsx_offset - sizeof(m_vsx_ppc64le) / 2;
215e6a66105SPavel Labath       uint8_t *src = (uint8_t *)&m_vmx_ppc64le + vmx_offset;
216e6a66105SPavel Labath       reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size,
217e6a66105SPavel Labath                                   eByteOrderLittle, error);
218e6a66105SPavel Labath     }
219e6a66105SPavel Labath   } else if (IsVMX(reg)) {
220e6a66105SPavel Labath     error = ReadVMX();
221e6a66105SPavel Labath     if (error.Fail())
222e6a66105SPavel Labath       return error;
223e6a66105SPavel Labath 
224e6a66105SPavel Labath     // Get pointer to m_vmx_ppc64le variable and set the data from it.
225e6a66105SPavel Labath     uint32_t vmx_offset = CalculateVmxOffset(reg_info);
226e6a66105SPavel Labath     assert(vmx_offset < sizeof m_vmx_ppc64le);
227e6a66105SPavel Labath     uint8_t *src = (uint8_t *)&m_vmx_ppc64le + vmx_offset;
228e6a66105SPavel Labath     reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size,
229e6a66105SPavel Labath                                 eByteOrderLittle, error);
230e6a66105SPavel Labath   } else if (IsGPR(reg)) {
231aae0a752SEugene Zemtsov     error = ReadGPR();
232aae0a752SEugene Zemtsov     if (error.Fail())
233aae0a752SEugene Zemtsov       return error;
234aae0a752SEugene Zemtsov 
235aae0a752SEugene Zemtsov     uint8_t *src = (uint8_t *) &m_gpr_ppc64le + reg_info->byte_offset;
236aae0a752SEugene Zemtsov     reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size,
237aae0a752SEugene Zemtsov                                 eByteOrderLittle, error);
238aae0a752SEugene Zemtsov   } else {
239e6a66105SPavel Labath     return Status("failed - register wasn't recognized to be a GPR, FPR, VSX "
240e6a66105SPavel Labath                   "or VMX, read strategy unknown");
241aae0a752SEugene Zemtsov   }
242aae0a752SEugene Zemtsov 
243aae0a752SEugene Zemtsov   return error;
244aae0a752SEugene Zemtsov }
245aae0a752SEugene Zemtsov 
246aae0a752SEugene Zemtsov Status NativeRegisterContextLinux_ppc64le::WriteRegister(
247aae0a752SEugene Zemtsov     const RegisterInfo *reg_info, const RegisterValue &reg_value) {
248aae0a752SEugene Zemtsov   Status error;
249aae0a752SEugene Zemtsov   if (!reg_info)
250aae0a752SEugene Zemtsov     return Status("reg_info NULL");
251aae0a752SEugene Zemtsov 
252aae0a752SEugene Zemtsov   const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB];
253aae0a752SEugene Zemtsov   if (reg_index == LLDB_INVALID_REGNUM)
254aae0a752SEugene Zemtsov     return Status("no lldb regnum for %s", reg_info && reg_info->name
255aae0a752SEugene Zemtsov                                                ? reg_info->name
256aae0a752SEugene Zemtsov                                                : "<unknown register>");
257aae0a752SEugene Zemtsov 
258aae0a752SEugene Zemtsov   if (IsGPR(reg_index)) {
259aae0a752SEugene Zemtsov     error = ReadGPR();
260aae0a752SEugene Zemtsov     if (error.Fail())
261aae0a752SEugene Zemtsov       return error;
262aae0a752SEugene Zemtsov 
263aae0a752SEugene Zemtsov     uint8_t *dst = (uint8_t *)&m_gpr_ppc64le + reg_info->byte_offset;
264aae0a752SEugene Zemtsov     ::memcpy(dst, reg_value.GetBytes(), reg_value.GetByteSize());
265aae0a752SEugene Zemtsov 
266aae0a752SEugene Zemtsov     error = WriteGPR();
267aae0a752SEugene Zemtsov     if (error.Fail())
268aae0a752SEugene Zemtsov       return error;
269aae0a752SEugene Zemtsov 
270aae0a752SEugene Zemtsov     return Status();
271aae0a752SEugene Zemtsov   }
272aae0a752SEugene Zemtsov 
273e6a66105SPavel Labath   if (IsFPR(reg_index)) {
274e6a66105SPavel Labath     error = ReadFPR();
275e6a66105SPavel Labath     if (error.Fail())
276e6a66105SPavel Labath       return error;
277e6a66105SPavel Labath 
278e6a66105SPavel Labath     // Get pointer to m_fpr_ppc64le variable and set the data to it.
279e6a66105SPavel Labath     uint32_t fpr_offset = CalculateFprOffset(reg_info);
280e6a66105SPavel Labath     assert(fpr_offset < GetFPRSize());
281e6a66105SPavel Labath     uint8_t *dst = (uint8_t *)&m_fpr_ppc64le + fpr_offset;
282e6a66105SPavel Labath     ::memcpy(dst, reg_value.GetBytes(), reg_value.GetByteSize());
283e6a66105SPavel Labath 
284e6a66105SPavel Labath     error = WriteFPR();
285e6a66105SPavel Labath     if (error.Fail())
286e6a66105SPavel Labath       return error;
287e6a66105SPavel Labath 
288e6a66105SPavel Labath     return Status();
289e6a66105SPavel Labath   }
290e6a66105SPavel Labath 
291e6a66105SPavel Labath   if (IsVMX(reg_index)) {
292e6a66105SPavel Labath     error = ReadVMX();
293e6a66105SPavel Labath     if (error.Fail())
294e6a66105SPavel Labath       return error;
295e6a66105SPavel Labath 
296e6a66105SPavel Labath     // Get pointer to m_vmx_ppc64le variable and set the data to it.
297e6a66105SPavel Labath     uint32_t vmx_offset = CalculateVmxOffset(reg_info);
298e6a66105SPavel Labath     assert(vmx_offset < sizeof(m_vmx_ppc64le));
299e6a66105SPavel Labath     uint8_t *dst = (uint8_t *)&m_vmx_ppc64le + vmx_offset;
300e6a66105SPavel Labath     ::memcpy(dst, reg_value.GetBytes(), reg_value.GetByteSize());
301e6a66105SPavel Labath 
302e6a66105SPavel Labath     error = WriteVMX();
303e6a66105SPavel Labath     if (error.Fail())
304e6a66105SPavel Labath       return error;
305e6a66105SPavel Labath 
306e6a66105SPavel Labath     return Status();
307e6a66105SPavel Labath   }
308e6a66105SPavel Labath 
309e6a66105SPavel Labath   if (IsVSX(reg_index)) {
310e6a66105SPavel Labath     uint32_t vsx_offset = CalculateVsxOffset(reg_info);
311e6a66105SPavel Labath     assert(vsx_offset < sizeof(m_vsx_ppc64le));
312e6a66105SPavel Labath 
313e6a66105SPavel Labath     if (vsx_offset < sizeof(m_vsx_ppc64le) / 2) {
314e6a66105SPavel Labath       error = ReadVSX();
315e6a66105SPavel Labath       if (error.Fail())
316e6a66105SPavel Labath         return error;
317e6a66105SPavel Labath 
318e6a66105SPavel Labath       error = ReadFPR();
319e6a66105SPavel Labath       if (error.Fail())
320e6a66105SPavel Labath         return error;
321e6a66105SPavel Labath 
322e6a66105SPavel Labath       uint64_t value[2];
323e6a66105SPavel Labath       ::memcpy(value, reg_value.GetBytes(), 16);
324e6a66105SPavel Labath       uint8_t *dst, *src;
325e6a66105SPavel Labath       src = (uint8_t *)value;
326e6a66105SPavel Labath       dst = (uint8_t *)&m_vsx_ppc64le + vsx_offset / 2;
327e6a66105SPavel Labath       ::memcpy(dst, src, 8);
328e6a66105SPavel Labath       src += 8;
329e6a66105SPavel Labath       dst = (uint8_t *)&m_fpr_ppc64le + vsx_offset / 2;
330e6a66105SPavel Labath       ::memcpy(dst, src, 8);
331e6a66105SPavel Labath 
332e6a66105SPavel Labath       WriteVSX();
333e6a66105SPavel Labath       WriteFPR();
334e6a66105SPavel Labath     } else {
335e6a66105SPavel Labath       error = ReadVMX();
336e6a66105SPavel Labath       if (error.Fail())
337e6a66105SPavel Labath         return error;
338e6a66105SPavel Labath 
339e6a66105SPavel Labath       // Get pointer to m_vmx_ppc64le variable and set the data from it.
340e6a66105SPavel Labath       uint32_t vmx_offset = vsx_offset - sizeof(m_vsx_ppc64le) / 2;
341e6a66105SPavel Labath       uint8_t *dst = (uint8_t *)&m_vmx_ppc64le + vmx_offset;
342e6a66105SPavel Labath       ::memcpy(dst, reg_value.GetBytes(), reg_value.GetByteSize());
343e6a66105SPavel Labath       WriteVMX();
344e6a66105SPavel Labath     }
345e6a66105SPavel Labath 
346e6a66105SPavel Labath     return Status();
347e6a66105SPavel Labath   }
348e6a66105SPavel Labath 
349e6a66105SPavel Labath   return Status("failed - register wasn't recognized to be a GPR, FPR, VSX "
350e6a66105SPavel Labath                 "or VMX, write strategy unknown");
351aae0a752SEugene Zemtsov }
352aae0a752SEugene Zemtsov 
353aae0a752SEugene Zemtsov Status NativeRegisterContextLinux_ppc64le::ReadAllRegisterValues(
354aae0a752SEugene Zemtsov     lldb::DataBufferSP &data_sp) {
355aae0a752SEugene Zemtsov   Status error;
356aae0a752SEugene Zemtsov 
357aae0a752SEugene Zemtsov   data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0));
358aae0a752SEugene Zemtsov   error = ReadGPR();
359aae0a752SEugene Zemtsov   if (error.Fail())
360aae0a752SEugene Zemtsov     return error;
361aae0a752SEugene Zemtsov 
362e6a66105SPavel Labath   error = ReadFPR();
363e6a66105SPavel Labath   if (error.Fail())
364e6a66105SPavel Labath     return error;
365e6a66105SPavel Labath 
366e6a66105SPavel Labath   error = ReadVMX();
367e6a66105SPavel Labath   if (error.Fail())
368e6a66105SPavel Labath     return error;
369e6a66105SPavel Labath 
370e6a66105SPavel Labath   error = ReadVSX();
371e6a66105SPavel Labath   if (error.Fail())
372e6a66105SPavel Labath     return error;
373e6a66105SPavel Labath 
374aae0a752SEugene Zemtsov   uint8_t *dst = data_sp->GetBytes();
375aae0a752SEugene Zemtsov   ::memcpy(dst, &m_gpr_ppc64le, GetGPRSize());
376e6a66105SPavel Labath   dst += GetGPRSize();
377e6a66105SPavel Labath   ::memcpy(dst, &m_fpr_ppc64le, GetFPRSize());
378e6a66105SPavel Labath   dst += GetFPRSize();
379e6a66105SPavel Labath   ::memcpy(dst, &m_vmx_ppc64le, sizeof(m_vmx_ppc64le));
380e6a66105SPavel Labath   dst += sizeof(m_vmx_ppc64le);
381e6a66105SPavel Labath   ::memcpy(dst, &m_vsx_ppc64le, sizeof(m_vsx_ppc64le));
382aae0a752SEugene Zemtsov 
383aae0a752SEugene Zemtsov   return error;
384aae0a752SEugene Zemtsov }
385aae0a752SEugene Zemtsov 
386aae0a752SEugene Zemtsov Status NativeRegisterContextLinux_ppc64le::WriteAllRegisterValues(
387aae0a752SEugene Zemtsov     const lldb::DataBufferSP &data_sp) {
388aae0a752SEugene Zemtsov   Status error;
389aae0a752SEugene Zemtsov 
390aae0a752SEugene Zemtsov   if (!data_sp) {
391aae0a752SEugene Zemtsov     error.SetErrorStringWithFormat(
392aae0a752SEugene Zemtsov         "NativeRegisterContextLinux_ppc64le::%s invalid data_sp provided",
393aae0a752SEugene Zemtsov         __FUNCTION__);
394aae0a752SEugene Zemtsov     return error;
395aae0a752SEugene Zemtsov   }
396aae0a752SEugene Zemtsov 
397aae0a752SEugene Zemtsov   if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) {
398aae0a752SEugene Zemtsov     error.SetErrorStringWithFormat(
399aae0a752SEugene Zemtsov         "NativeRegisterContextLinux_ppc64le::%s data_sp contained mismatched "
400aae0a752SEugene Zemtsov         "data size, expected %" PRIu64 ", actual %" PRIu64,
401aae0a752SEugene Zemtsov         __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize());
402aae0a752SEugene Zemtsov     return error;
403aae0a752SEugene Zemtsov   }
404aae0a752SEugene Zemtsov 
405aae0a752SEugene Zemtsov   uint8_t *src = data_sp->GetBytes();
406aae0a752SEugene Zemtsov   if (src == nullptr) {
407aae0a752SEugene Zemtsov     error.SetErrorStringWithFormat("NativeRegisterContextLinux_ppc64le::%s "
408aae0a752SEugene Zemtsov                                    "DataBuffer::GetBytes() returned a null "
409aae0a752SEugene Zemtsov                                    "pointer",
410aae0a752SEugene Zemtsov                                    __FUNCTION__);
411aae0a752SEugene Zemtsov     return error;
412aae0a752SEugene Zemtsov   }
413aae0a752SEugene Zemtsov 
414aae0a752SEugene Zemtsov   ::memcpy(&m_gpr_ppc64le, src, GetGPRSize());
415aae0a752SEugene Zemtsov   error = WriteGPR();
416aae0a752SEugene Zemtsov 
417e6a66105SPavel Labath   if (error.Fail())
418e6a66105SPavel Labath     return error;
419e6a66105SPavel Labath 
420e6a66105SPavel Labath   src += GetGPRSize();
421e6a66105SPavel Labath   ::memcpy(&m_fpr_ppc64le, src, GetFPRSize());
422e6a66105SPavel Labath 
423e6a66105SPavel Labath   error = WriteFPR();
424e6a66105SPavel Labath   if (error.Fail())
425e6a66105SPavel Labath     return error;
426e6a66105SPavel Labath 
427e6a66105SPavel Labath   src += GetFPRSize();
428e6a66105SPavel Labath   ::memcpy(&m_vmx_ppc64le, src, sizeof(m_vmx_ppc64le));
429e6a66105SPavel Labath 
430e6a66105SPavel Labath   error = WriteVMX();
431e6a66105SPavel Labath   if (error.Fail())
432e6a66105SPavel Labath     return error;
433e6a66105SPavel Labath 
434e6a66105SPavel Labath   src += sizeof(m_vmx_ppc64le);
435e6a66105SPavel Labath   ::memcpy(&m_vsx_ppc64le, src, sizeof(m_vsx_ppc64le));
436e6a66105SPavel Labath   error = WriteVSX();
437e6a66105SPavel Labath 
438aae0a752SEugene Zemtsov   return error;
439aae0a752SEugene Zemtsov }
440aae0a752SEugene Zemtsov 
441aae0a752SEugene Zemtsov bool NativeRegisterContextLinux_ppc64le::IsGPR(unsigned reg) const {
442aae0a752SEugene Zemtsov   return reg <= k_last_gpr_ppc64le; // GPR's come first.
443aae0a752SEugene Zemtsov }
444aae0a752SEugene Zemtsov 
445e6a66105SPavel Labath bool NativeRegisterContextLinux_ppc64le::IsFPR(unsigned reg) const {
446e6a66105SPavel Labath   return (k_first_fpr_ppc64le <= reg && reg <= k_last_fpr_ppc64le);
447e6a66105SPavel Labath }
448e6a66105SPavel Labath 
449e6a66105SPavel Labath uint32_t NativeRegisterContextLinux_ppc64le::CalculateFprOffset(
450e6a66105SPavel Labath     const RegisterInfo *reg_info) const {
451e6a66105SPavel Labath   return reg_info->byte_offset -
452e6a66105SPavel Labath          GetRegisterInfoAtIndex(k_first_fpr_ppc64le)->byte_offset;
453e6a66105SPavel Labath }
454e6a66105SPavel Labath 
455e6a66105SPavel Labath uint32_t NativeRegisterContextLinux_ppc64le::CalculateVmxOffset(
456e6a66105SPavel Labath     const RegisterInfo *reg_info) const {
457e6a66105SPavel Labath   return reg_info->byte_offset -
458e6a66105SPavel Labath          GetRegisterInfoAtIndex(k_first_vmx_ppc64le)->byte_offset;
459e6a66105SPavel Labath }
460e6a66105SPavel Labath 
461e6a66105SPavel Labath uint32_t NativeRegisterContextLinux_ppc64le::CalculateVsxOffset(
462e6a66105SPavel Labath     const RegisterInfo *reg_info) const {
463e6a66105SPavel Labath   return reg_info->byte_offset -
464e6a66105SPavel Labath          GetRegisterInfoAtIndex(k_first_vsx_ppc64le)->byte_offset;
465e6a66105SPavel Labath }
466e6a66105SPavel Labath 
467e6a66105SPavel Labath Status NativeRegisterContextLinux_ppc64le::ReadVMX() {
468e6a66105SPavel Labath   int regset = NT_PPC_VMX;
469e6a66105SPavel Labath   return NativeProcessLinux::PtraceWrapper(PTRACE_GETVRREGS, m_thread.GetID(),
470e6a66105SPavel Labath                                            &regset, &m_vmx_ppc64le,
471e6a66105SPavel Labath                                            sizeof(m_vmx_ppc64le));
472e6a66105SPavel Labath }
473e6a66105SPavel Labath 
474e6a66105SPavel Labath Status NativeRegisterContextLinux_ppc64le::WriteVMX() {
475e6a66105SPavel Labath   int regset = NT_PPC_VMX;
476e6a66105SPavel Labath   return NativeProcessLinux::PtraceWrapper(PTRACE_SETVRREGS, m_thread.GetID(),
477e6a66105SPavel Labath                                            &regset, &m_vmx_ppc64le,
478e6a66105SPavel Labath                                            sizeof(m_vmx_ppc64le));
479e6a66105SPavel Labath }
480e6a66105SPavel Labath 
481e6a66105SPavel Labath Status NativeRegisterContextLinux_ppc64le::ReadVSX() {
482e6a66105SPavel Labath   int regset = NT_PPC_VSX;
483e6a66105SPavel Labath   return NativeProcessLinux::PtraceWrapper(PTRACE_GETVSRREGS, m_thread.GetID(),
484e6a66105SPavel Labath                                            &regset, &m_vsx_ppc64le,
485e6a66105SPavel Labath                                            sizeof(m_vsx_ppc64le));
486e6a66105SPavel Labath }
487e6a66105SPavel Labath 
488e6a66105SPavel Labath Status NativeRegisterContextLinux_ppc64le::WriteVSX() {
489e6a66105SPavel Labath   int regset = NT_PPC_VSX;
490e6a66105SPavel Labath   return NativeProcessLinux::PtraceWrapper(PTRACE_SETVSRREGS, m_thread.GetID(),
491e6a66105SPavel Labath                                            &regset, &m_vsx_ppc64le,
492e6a66105SPavel Labath                                            sizeof(m_vsx_ppc64le));
493e6a66105SPavel Labath }
494e6a66105SPavel Labath 
495e6a66105SPavel Labath bool NativeRegisterContextLinux_ppc64le::IsVMX(unsigned reg) {
496e6a66105SPavel Labath   return (reg >= k_first_vmx_ppc64le) && (reg <= k_last_vmx_ppc64le);
497e6a66105SPavel Labath }
498e6a66105SPavel Labath 
499e6a66105SPavel Labath bool NativeRegisterContextLinux_ppc64le::IsVSX(unsigned reg) {
500e6a66105SPavel Labath   return (reg >= k_first_vsx_ppc64le) && (reg <= k_last_vsx_ppc64le);
501e6a66105SPavel Labath }
502e6a66105SPavel Labath 
503c51ad483SPavel Labath uint32_t NativeRegisterContextLinux_ppc64le::NumSupportedHardwareWatchpoints() {
504c51ad483SPavel Labath   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
505c51ad483SPavel Labath 
506c51ad483SPavel Labath   // Read hardware breakpoint and watchpoint information.
507c51ad483SPavel Labath   Status error = ReadHardwareDebugInfo();
508c51ad483SPavel Labath 
509c51ad483SPavel Labath   if (error.Fail())
510c51ad483SPavel Labath     return 0;
511c51ad483SPavel Labath 
512c51ad483SPavel Labath   LLDB_LOG(log, "{0}", m_max_hwp_supported);
513c51ad483SPavel Labath   return m_max_hwp_supported;
514c51ad483SPavel Labath }
515c51ad483SPavel Labath 
516c51ad483SPavel Labath uint32_t NativeRegisterContextLinux_ppc64le::SetHardwareWatchpoint(
517c51ad483SPavel Labath     lldb::addr_t addr, size_t size, uint32_t watch_flags) {
518c51ad483SPavel Labath   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
519c51ad483SPavel Labath   LLDB_LOG(log, "addr: {0:x}, size: {1:x} watch_flags: {2:x}", addr, size,
520c51ad483SPavel Labath            watch_flags);
521c51ad483SPavel Labath 
522c51ad483SPavel Labath   // Read hardware breakpoint and watchpoint information.
523c51ad483SPavel Labath   Status error = ReadHardwareDebugInfo();
524c51ad483SPavel Labath 
525c51ad483SPavel Labath   if (error.Fail())
526c51ad483SPavel Labath     return LLDB_INVALID_INDEX32;
527c51ad483SPavel Labath 
528c51ad483SPavel Labath   uint32_t control_value = 0, wp_index = 0;
529c51ad483SPavel Labath   lldb::addr_t real_addr = addr;
530c51ad483SPavel Labath   uint32_t rw_mode = 0;
531c51ad483SPavel Labath 
53205097246SAdrian Prantl   // Check if we are setting watchpoint other than read/write/access Update
53305097246SAdrian Prantl   // watchpoint flag to match ppc64le write-read bit configuration.
534c51ad483SPavel Labath   switch (watch_flags) {
535c51ad483SPavel Labath   case eWatchpointKindWrite:
536c51ad483SPavel Labath     rw_mode = PPC_BREAKPOINT_TRIGGER_WRITE;
537c51ad483SPavel Labath     watch_flags = 2;
538c51ad483SPavel Labath     break;
539c51ad483SPavel Labath   case eWatchpointKindRead:
540c51ad483SPavel Labath     rw_mode = PPC_BREAKPOINT_TRIGGER_READ;
541c51ad483SPavel Labath     watch_flags = 1;
542c51ad483SPavel Labath     break;
543c51ad483SPavel Labath   case (eWatchpointKindRead | eWatchpointKindWrite):
544c51ad483SPavel Labath     rw_mode = PPC_BREAKPOINT_TRIGGER_RW;
545c51ad483SPavel Labath     break;
546c51ad483SPavel Labath   default:
547c51ad483SPavel Labath     return LLDB_INVALID_INDEX32;
548c51ad483SPavel Labath   }
549c51ad483SPavel Labath 
550c51ad483SPavel Labath   // Check if size has a valid hardware watchpoint length.
551c51ad483SPavel Labath   if (size != 1 && size != 2 && size != 4 && size != 8)
552c51ad483SPavel Labath     return LLDB_INVALID_INDEX32;
553c51ad483SPavel Labath 
55405097246SAdrian Prantl   // Check 8-byte alignment for hardware watchpoint target address. Below is a
55505097246SAdrian Prantl   // hack to recalculate address and size in order to make sure we can watch
556e9264b74SKazuaki Ishizaki   // non 8-byte aligned addresses as well.
557c51ad483SPavel Labath   if (addr & 0x07) {
558c51ad483SPavel Labath 
559c51ad483SPavel Labath     addr_t begin = llvm::alignDown(addr, 8);
560c51ad483SPavel Labath     addr_t end = llvm::alignTo(addr + size, 8);
561c51ad483SPavel Labath     size = llvm::PowerOf2Ceil(end - begin);
562c51ad483SPavel Labath 
563c51ad483SPavel Labath     addr = addr & (~0x07);
564c51ad483SPavel Labath   }
565c51ad483SPavel Labath 
566c51ad483SPavel Labath   // Setup control value
567c51ad483SPavel Labath   control_value = watch_flags << 3;
568c51ad483SPavel Labath   control_value |= ((1 << size) - 1) << 5;
569c51ad483SPavel Labath   control_value |= (2 << 1) | 1;
570c51ad483SPavel Labath 
571c51ad483SPavel Labath   // Iterate over stored watchpoints and find a free wp_index
572c51ad483SPavel Labath   wp_index = LLDB_INVALID_INDEX32;
573c51ad483SPavel Labath   for (uint32_t i = 0; i < m_max_hwp_supported; i++) {
574c51ad483SPavel Labath     if ((m_hwp_regs[i].control & 1) == 0) {
575c51ad483SPavel Labath       wp_index = i; // Mark last free slot
576c51ad483SPavel Labath     } else if (m_hwp_regs[i].address == addr) {
577c51ad483SPavel Labath       return LLDB_INVALID_INDEX32; // We do not support duplicate watchpoints.
578c51ad483SPavel Labath     }
579c51ad483SPavel Labath   }
580c51ad483SPavel Labath 
581c51ad483SPavel Labath   if (wp_index == LLDB_INVALID_INDEX32)
582c51ad483SPavel Labath     return LLDB_INVALID_INDEX32;
583c51ad483SPavel Labath 
584c51ad483SPavel Labath   // Update watchpoint in local cache
585c51ad483SPavel Labath   m_hwp_regs[wp_index].real_addr = real_addr;
586c51ad483SPavel Labath   m_hwp_regs[wp_index].address = addr;
587c51ad483SPavel Labath   m_hwp_regs[wp_index].control = control_value;
588c51ad483SPavel Labath   m_hwp_regs[wp_index].mode = rw_mode;
589c51ad483SPavel Labath 
590c51ad483SPavel Labath   // PTRACE call to set corresponding watchpoint register.
591c51ad483SPavel Labath   error = WriteHardwareDebugRegs();
592c51ad483SPavel Labath 
593c51ad483SPavel Labath   if (error.Fail()) {
594c51ad483SPavel Labath     m_hwp_regs[wp_index].address = 0;
595c51ad483SPavel Labath     m_hwp_regs[wp_index].control &= llvm::maskTrailingZeros<uint32_t>(1);
596c51ad483SPavel Labath 
597c51ad483SPavel Labath     return LLDB_INVALID_INDEX32;
598c51ad483SPavel Labath   }
599c51ad483SPavel Labath 
600c51ad483SPavel Labath   return wp_index;
601c51ad483SPavel Labath }
602c51ad483SPavel Labath 
603c51ad483SPavel Labath bool NativeRegisterContextLinux_ppc64le::ClearHardwareWatchpoint(
604c51ad483SPavel Labath     uint32_t wp_index) {
605c51ad483SPavel Labath   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
606c51ad483SPavel Labath   LLDB_LOG(log, "wp_index: {0}", wp_index);
607c51ad483SPavel Labath 
608c51ad483SPavel Labath   // Read hardware breakpoint and watchpoint information.
609c51ad483SPavel Labath   Status error = ReadHardwareDebugInfo();
610c51ad483SPavel Labath 
611c51ad483SPavel Labath   if (error.Fail())
612c51ad483SPavel Labath     return false;
613c51ad483SPavel Labath 
614c51ad483SPavel Labath   if (wp_index >= m_max_hwp_supported)
615c51ad483SPavel Labath     return false;
616c51ad483SPavel Labath 
617c51ad483SPavel Labath   // Create a backup we can revert to in case of failure.
618c51ad483SPavel Labath   lldb::addr_t tempAddr = m_hwp_regs[wp_index].address;
619c51ad483SPavel Labath   uint32_t tempControl = m_hwp_regs[wp_index].control;
620c51ad483SPavel Labath   long *tempSlot = reinterpret_cast<long *>(m_hwp_regs[wp_index].slot);
621c51ad483SPavel Labath 
622c51ad483SPavel Labath   // Update watchpoint in local cache
623c51ad483SPavel Labath   m_hwp_regs[wp_index].control &= llvm::maskTrailingZeros<uint32_t>(1);
624c51ad483SPavel Labath   m_hwp_regs[wp_index].address = 0;
625c51ad483SPavel Labath   m_hwp_regs[wp_index].slot = 0;
626c51ad483SPavel Labath   m_hwp_regs[wp_index].mode = 0;
627c51ad483SPavel Labath 
628c51ad483SPavel Labath   // Ptrace call to update hardware debug registers
629c51ad483SPavel Labath   error = NativeProcessLinux::PtraceWrapper(PPC_PTRACE_DELHWDEBUG,
630c51ad483SPavel Labath                                             m_thread.GetID(), 0, tempSlot);
631c51ad483SPavel Labath 
632c51ad483SPavel Labath   if (error.Fail()) {
633c51ad483SPavel Labath     m_hwp_regs[wp_index].control = tempControl;
634c51ad483SPavel Labath     m_hwp_regs[wp_index].address = tempAddr;
635c51ad483SPavel Labath     m_hwp_regs[wp_index].slot = reinterpret_cast<long>(tempSlot);
636c51ad483SPavel Labath 
637c51ad483SPavel Labath     return false;
638c51ad483SPavel Labath   }
639c51ad483SPavel Labath 
640c51ad483SPavel Labath   return true;
641c51ad483SPavel Labath }
642c51ad483SPavel Labath 
643c51ad483SPavel Labath uint32_t
644c51ad483SPavel Labath NativeRegisterContextLinux_ppc64le::GetWatchpointSize(uint32_t wp_index) {
645c51ad483SPavel Labath   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
646c51ad483SPavel Labath   LLDB_LOG(log, "wp_index: {0}", wp_index);
647c51ad483SPavel Labath 
648c51ad483SPavel Labath   unsigned control = (m_hwp_regs[wp_index].control >> 5) & 0xff;
649e6a66105SPavel Labath   if (llvm::isPowerOf2_32(control + 1)) {
650c51ad483SPavel Labath     return llvm::countPopulation(control);
651c51ad483SPavel Labath   }
652c51ad483SPavel Labath 
653e6a66105SPavel Labath   return 0;
654e6a66105SPavel Labath }
655e6a66105SPavel Labath 
656c51ad483SPavel Labath bool NativeRegisterContextLinux_ppc64le::WatchpointIsEnabled(
657c51ad483SPavel Labath     uint32_t wp_index) {
658c51ad483SPavel Labath   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
659c51ad483SPavel Labath   LLDB_LOG(log, "wp_index: {0}", wp_index);
660c51ad483SPavel Labath 
661c51ad483SPavel Labath   return !!((m_hwp_regs[wp_index].control & 0x1) == 0x1);
662c51ad483SPavel Labath }
663c51ad483SPavel Labath 
664c51ad483SPavel Labath Status NativeRegisterContextLinux_ppc64le::GetWatchpointHitIndex(
665c51ad483SPavel Labath     uint32_t &wp_index, lldb::addr_t trap_addr) {
666c51ad483SPavel Labath   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
667c51ad483SPavel Labath   LLDB_LOG(log, "wp_index: {0}, trap_addr: {1:x}", wp_index, trap_addr);
668c51ad483SPavel Labath 
669c51ad483SPavel Labath   uint32_t watch_size;
670c51ad483SPavel Labath   lldb::addr_t watch_addr;
671c51ad483SPavel Labath 
672c51ad483SPavel Labath   for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) {
673c51ad483SPavel Labath     watch_size = GetWatchpointSize(wp_index);
674c51ad483SPavel Labath     watch_addr = m_hwp_regs[wp_index].address;
675c51ad483SPavel Labath 
676c51ad483SPavel Labath     if (WatchpointIsEnabled(wp_index) && trap_addr >= watch_addr &&
677c51ad483SPavel Labath         trap_addr <= watch_addr + watch_size) {
678c51ad483SPavel Labath       m_hwp_regs[wp_index].hit_addr = trap_addr;
679c51ad483SPavel Labath       return Status();
680c51ad483SPavel Labath     }
681c51ad483SPavel Labath   }
682c51ad483SPavel Labath 
683c51ad483SPavel Labath   wp_index = LLDB_INVALID_INDEX32;
684c51ad483SPavel Labath   return Status();
685c51ad483SPavel Labath }
686c51ad483SPavel Labath 
687c51ad483SPavel Labath lldb::addr_t
688c51ad483SPavel Labath NativeRegisterContextLinux_ppc64le::GetWatchpointAddress(uint32_t wp_index) {
689c51ad483SPavel Labath   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
690c51ad483SPavel Labath   LLDB_LOG(log, "wp_index: {0}", wp_index);
691c51ad483SPavel Labath 
692c51ad483SPavel Labath   if (wp_index >= m_max_hwp_supported)
693c51ad483SPavel Labath     return LLDB_INVALID_ADDRESS;
694c51ad483SPavel Labath 
695c51ad483SPavel Labath   if (WatchpointIsEnabled(wp_index))
696c51ad483SPavel Labath     return m_hwp_regs[wp_index].real_addr;
697c51ad483SPavel Labath   else
698c51ad483SPavel Labath     return LLDB_INVALID_ADDRESS;
699c51ad483SPavel Labath }
700c51ad483SPavel Labath 
701c51ad483SPavel Labath lldb::addr_t
702c51ad483SPavel Labath NativeRegisterContextLinux_ppc64le::GetWatchpointHitAddress(uint32_t wp_index) {
703c51ad483SPavel Labath   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
704c51ad483SPavel Labath   LLDB_LOG(log, "wp_index: {0}", wp_index);
705c51ad483SPavel Labath 
706c51ad483SPavel Labath   if (wp_index >= m_max_hwp_supported)
707c51ad483SPavel Labath     return LLDB_INVALID_ADDRESS;
708c51ad483SPavel Labath 
709c51ad483SPavel Labath   if (WatchpointIsEnabled(wp_index))
710c51ad483SPavel Labath     return m_hwp_regs[wp_index].hit_addr;
711c51ad483SPavel Labath 
712c51ad483SPavel Labath   return LLDB_INVALID_ADDRESS;
713c51ad483SPavel Labath }
714c51ad483SPavel Labath 
715c51ad483SPavel Labath Status NativeRegisterContextLinux_ppc64le::ReadHardwareDebugInfo() {
716c51ad483SPavel Labath   if (!m_refresh_hwdebug_info) {
717c51ad483SPavel Labath     return Status();
718c51ad483SPavel Labath   }
719c51ad483SPavel Labath 
720c51ad483SPavel Labath   ::pid_t tid = m_thread.GetID();
721c51ad483SPavel Labath 
722c51ad483SPavel Labath   struct ppc_debug_info hwdebug_info;
723c51ad483SPavel Labath   Status error;
724c51ad483SPavel Labath 
725c51ad483SPavel Labath   error = NativeProcessLinux::PtraceWrapper(
726c51ad483SPavel Labath       PPC_PTRACE_GETHWDBGINFO, tid, 0, &hwdebug_info, sizeof(hwdebug_info));
727c51ad483SPavel Labath 
728c51ad483SPavel Labath   if (error.Fail())
729c51ad483SPavel Labath     return error;
730c51ad483SPavel Labath 
731c51ad483SPavel Labath   m_max_hwp_supported = hwdebug_info.num_data_bps;
732c51ad483SPavel Labath   m_max_hbp_supported = hwdebug_info.num_instruction_bps;
733c51ad483SPavel Labath   m_refresh_hwdebug_info = false;
734c51ad483SPavel Labath 
735c51ad483SPavel Labath   return error;
736c51ad483SPavel Labath }
737c51ad483SPavel Labath 
738c51ad483SPavel Labath Status NativeRegisterContextLinux_ppc64le::WriteHardwareDebugRegs() {
739c51ad483SPavel Labath   struct ppc_hw_breakpoint reg_state;
740c51ad483SPavel Labath   Status error;
741c51ad483SPavel Labath   long ret;
742c51ad483SPavel Labath 
743c51ad483SPavel Labath   for (uint32_t i = 0; i < m_max_hwp_supported; i++) {
744c51ad483SPavel Labath     reg_state.addr = m_hwp_regs[i].address;
745c51ad483SPavel Labath     reg_state.trigger_type = m_hwp_regs[i].mode;
746c51ad483SPavel Labath     reg_state.version = 1;
747c51ad483SPavel Labath     reg_state.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
748c51ad483SPavel Labath     reg_state.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
749c51ad483SPavel Labath     reg_state.addr2 = 0;
750c51ad483SPavel Labath     reg_state.condition_value = 0;
751c51ad483SPavel Labath 
752c51ad483SPavel Labath     error = NativeProcessLinux::PtraceWrapper(PPC_PTRACE_SETHWDEBUG,
753c51ad483SPavel Labath                                               m_thread.GetID(), 0, &reg_state,
754c51ad483SPavel Labath                                               sizeof(reg_state), &ret);
755c51ad483SPavel Labath 
756c51ad483SPavel Labath     if (error.Fail())
757c51ad483SPavel Labath       return error;
758c51ad483SPavel Labath 
759c51ad483SPavel Labath     m_hwp_regs[i].slot = ret;
760c51ad483SPavel Labath   }
761c51ad483SPavel Labath 
762c51ad483SPavel Labath   return error;
763c51ad483SPavel Labath }
764c51ad483SPavel Labath 
765aae0a752SEugene Zemtsov #endif // defined(__powerpc64__)
766