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