xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm.cpp (revision be691f3bb6417f04a68938fadbcaee2d5795e764)
1 //===-- NativeRegisterContextLinux_arm.cpp --------------------------------===//
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 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
10 
11 #include "NativeRegisterContextLinux_arm.h"
12 
13 #include "Plugins/Process/Linux/NativeProcessLinux.h"
14 #include "Plugins/Process/Linux/Procfs.h"
15 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
16 #include "Plugins/Process/Utility/RegisterInfoPOSIX_arm.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 <elf.h>
23 #include <sys/socket.h>
24 
25 #define REG_CONTEXT_SIZE (GetGPRSize() + sizeof(m_fpr))
26 
27 #ifndef PTRACE_GETVFPREGS
28 #define PTRACE_GETVFPREGS 27
29 #define PTRACE_SETVFPREGS 28
30 #endif
31 #ifndef PTRACE_GETHBPREGS
32 #define PTRACE_GETHBPREGS 29
33 #define PTRACE_SETHBPREGS 30
34 #endif
35 #if !defined(PTRACE_TYPE_ARG3)
36 #define PTRACE_TYPE_ARG3 void *
37 #endif
38 #if !defined(PTRACE_TYPE_ARG4)
39 #define PTRACE_TYPE_ARG4 void *
40 #endif
41 
42 using namespace lldb;
43 using namespace lldb_private;
44 using namespace lldb_private::process_linux;
45 
46 #if defined(__arm__)
47 
48 std::unique_ptr<NativeRegisterContextLinux>
49 NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(
50     const ArchSpec &target_arch, NativeThreadLinux &native_thread) {
51   return std::make_unique<NativeRegisterContextLinux_arm>(target_arch,
52                                                            native_thread);
53 }
54 
55 #endif // defined(__arm__)
56 
57 NativeRegisterContextLinux_arm::NativeRegisterContextLinux_arm(
58     const ArchSpec &target_arch, NativeThreadProtocol &native_thread)
59     : NativeRegisterContextRegisterInfo(native_thread,
60                                         new RegisterInfoPOSIX_arm(target_arch)),
61       NativeRegisterContextLinux(native_thread) {
62   assert(target_arch.GetMachine() == llvm::Triple::arm);
63 
64   ::memset(&m_fpr, 0, sizeof(m_fpr));
65   ::memset(&m_gpr_arm, 0, sizeof(m_gpr_arm));
66   ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs));
67   ::memset(&m_hbr_regs, 0, sizeof(m_hbr_regs));
68 
69   // 16 is just a maximum value, query hardware for actual watchpoint count
70   m_max_hwp_supported = 16;
71   m_max_hbp_supported = 16;
72   m_refresh_hwdebug_info = true;
73 }
74 
75 RegisterInfoPOSIX_arm &NativeRegisterContextLinux_arm::GetRegisterInfo() const {
76   return static_cast<RegisterInfoPOSIX_arm &>(*m_register_info_interface_up);
77 }
78 
79 uint32_t NativeRegisterContextLinux_arm::GetRegisterSetCount() const {
80   return GetRegisterInfo().GetRegisterSetCount();
81 }
82 
83 uint32_t NativeRegisterContextLinux_arm::GetUserRegisterCount() const {
84   uint32_t count = 0;
85   for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index)
86     count += GetRegisterSet(set_index)->num_registers;
87   return count;
88 }
89 
90 const RegisterSet *
91 NativeRegisterContextLinux_arm::GetRegisterSet(uint32_t set_index) const {
92   return GetRegisterInfo().GetRegisterSet(set_index);
93 }
94 
95 Status
96 NativeRegisterContextLinux_arm::ReadRegister(const RegisterInfo *reg_info,
97                                              RegisterValue &reg_value) {
98   Status error;
99 
100   if (!reg_info) {
101     error.SetErrorString("reg_info NULL");
102     return error;
103   }
104 
105   const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
106 
107   if (IsFPR(reg)) {
108     error = ReadFPR();
109     if (error.Fail())
110       return error;
111   } else {
112     uint32_t full_reg = reg;
113     bool is_subreg = reg_info->invalidate_regs &&
114                      (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM);
115 
116     if (is_subreg) {
117       // Read the full aligned 64-bit register.
118       full_reg = reg_info->invalidate_regs[0];
119     }
120 
121     error = ReadRegisterRaw(full_reg, reg_value);
122 
123     if (error.Success()) {
124       // If our read was not aligned (for ah,bh,ch,dh), shift our returned
125       // value one byte to the right.
126       if (is_subreg && (reg_info->byte_offset & 0x1))
127         reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8);
128 
129       // If our return byte size was greater than the return value reg size,
130       // then use the type specified by reg_info rather than the uint64_t
131       // default
132       if (reg_value.GetByteSize() > reg_info->byte_size)
133         reg_value.SetType(reg_info);
134     }
135     return error;
136   }
137 
138   // Get pointer to m_fpr variable and set the data from it.
139   uint32_t fpr_offset = CalculateFprOffset(reg_info);
140   assert(fpr_offset < sizeof m_fpr);
141   uint8_t *src = (uint8_t *)&m_fpr + fpr_offset;
142   switch (reg_info->byte_size) {
143   case 2:
144     reg_value.SetUInt16(*(uint16_t *)src);
145     break;
146   case 4:
147     reg_value.SetUInt32(*(uint32_t *)src);
148     break;
149   case 8:
150     reg_value.SetUInt64(*(uint64_t *)src);
151     break;
152   case 16:
153     reg_value.SetBytes(src, 16, GetByteOrder());
154     break;
155   default:
156     assert(false && "Unhandled data size.");
157     error.SetErrorStringWithFormat("unhandled byte size: %" PRIu32,
158                                    reg_info->byte_size);
159     break;
160   }
161 
162   return error;
163 }
164 
165 Status
166 NativeRegisterContextLinux_arm::WriteRegister(const RegisterInfo *reg_info,
167                                               const RegisterValue &reg_value) {
168   if (!reg_info)
169     return Status("reg_info NULL");
170 
171   const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB];
172   if (reg_index == LLDB_INVALID_REGNUM)
173     return Status("no lldb regnum for %s", reg_info && reg_info->name
174                                                ? reg_info->name
175                                                : "<unknown register>");
176 
177   if (IsGPR(reg_index))
178     return WriteRegisterRaw(reg_index, reg_value);
179 
180   if (IsFPR(reg_index)) {
181     // Get pointer to m_fpr variable and set the data to it.
182     uint32_t fpr_offset = CalculateFprOffset(reg_info);
183     assert(fpr_offset < sizeof m_fpr);
184     uint8_t *dst = (uint8_t *)&m_fpr + fpr_offset;
185     switch (reg_info->byte_size) {
186     case 2:
187       *(uint16_t *)dst = reg_value.GetAsUInt16();
188       break;
189     case 4:
190       *(uint32_t *)dst = reg_value.GetAsUInt32();
191       break;
192     case 8:
193       *(uint64_t *)dst = reg_value.GetAsUInt64();
194       break;
195     default:
196       assert(false && "Unhandled data size.");
197       return Status("unhandled register data size %" PRIu32,
198                     reg_info->byte_size);
199     }
200 
201     Status error = WriteFPR();
202     if (error.Fail())
203       return error;
204 
205     return Status();
206   }
207 
208   return Status("failed - register wasn't recognized to be a GPR or an FPR, "
209                 "write strategy unknown");
210 }
211 
212 Status NativeRegisterContextLinux_arm::ReadAllRegisterValues(
213     lldb::DataBufferSP &data_sp) {
214   Status error;
215 
216   data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0));
217   error = ReadGPR();
218   if (error.Fail())
219     return error;
220 
221   error = ReadFPR();
222   if (error.Fail())
223     return error;
224 
225   uint8_t *dst = data_sp->GetBytes();
226   ::memcpy(dst, &m_gpr_arm, GetGPRSize());
227   dst += GetGPRSize();
228   ::memcpy(dst, &m_fpr, sizeof(m_fpr));
229 
230   return error;
231 }
232 
233 Status NativeRegisterContextLinux_arm::WriteAllRegisterValues(
234     const lldb::DataBufferSP &data_sp) {
235   Status error;
236 
237   if (!data_sp) {
238     error.SetErrorStringWithFormat(
239         "NativeRegisterContextLinux_x86_64::%s invalid data_sp provided",
240         __FUNCTION__);
241     return error;
242   }
243 
244   if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) {
245     error.SetErrorStringWithFormat(
246         "NativeRegisterContextLinux_x86_64::%s data_sp contained mismatched "
247         "data size, expected %" PRIu64 ", actual %" PRIu64,
248         __FUNCTION__, (uint64_t)REG_CONTEXT_SIZE, data_sp->GetByteSize());
249     return error;
250   }
251 
252   uint8_t *src = data_sp->GetBytes();
253   if (src == nullptr) {
254     error.SetErrorStringWithFormat("NativeRegisterContextLinux_x86_64::%s "
255                                    "DataBuffer::GetBytes() returned a null "
256                                    "pointer",
257                                    __FUNCTION__);
258     return error;
259   }
260   ::memcpy(&m_gpr_arm, src, GetRegisterInfoInterface().GetGPRSize());
261 
262   error = WriteGPR();
263   if (error.Fail())
264     return error;
265 
266   src += GetRegisterInfoInterface().GetGPRSize();
267   ::memcpy(&m_fpr, src, sizeof(m_fpr));
268 
269   error = WriteFPR();
270   if (error.Fail())
271     return error;
272 
273   return error;
274 }
275 
276 bool NativeRegisterContextLinux_arm::IsGPR(unsigned reg) const {
277   if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
278       RegisterInfoPOSIX_arm::GPRegSet)
279     return true;
280   return false;
281 }
282 
283 bool NativeRegisterContextLinux_arm::IsFPR(unsigned reg) const {
284   if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
285       RegisterInfoPOSIX_arm::FPRegSet)
286     return true;
287   return false;
288 }
289 
290 uint32_t NativeRegisterContextLinux_arm::NumSupportedHardwareBreakpoints() {
291   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS));
292 
293   LLDB_LOGF(log, "NativeRegisterContextLinux_arm::%s()", __FUNCTION__);
294 
295   Status error;
296 
297   // Read hardware breakpoint and watchpoint information.
298   error = ReadHardwareDebugInfo();
299 
300   if (error.Fail())
301     return 0;
302 
303   LLDB_LOG(log, "{0}", m_max_hbp_supported);
304   return m_max_hbp_supported;
305 }
306 
307 uint32_t
308 NativeRegisterContextLinux_arm::SetHardwareBreakpoint(lldb::addr_t addr,
309                                                       size_t size) {
310   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS));
311   LLDB_LOG(log, "addr: {0:x}, size: {1:x}", addr, size);
312 
313   // Read hardware breakpoint and watchpoint information.
314   Status error = ReadHardwareDebugInfo();
315 
316   if (error.Fail())
317     return LLDB_INVALID_INDEX32;
318 
319   uint32_t control_value = 0, bp_index = 0;
320 
321   // Setup address and control values.
322   // Use size to get a hint of arm vs thumb modes.
323   switch (size) {
324   case 2:
325     control_value = (0x3 << 5) | 7;
326     addr &= ~1;
327     break;
328   case 4:
329     control_value = (0xfu << 5) | 7;
330     addr &= ~3;
331     break;
332   default:
333     return LLDB_INVALID_INDEX32;
334   }
335 
336   // Iterate over stored breakpoints and find a free bp_index
337   bp_index = LLDB_INVALID_INDEX32;
338   for (uint32_t i = 0; i < m_max_hbp_supported; i++) {
339     if ((m_hbr_regs[i].control & 1) == 0) {
340       bp_index = i; // Mark last free slot
341     } else if (m_hbr_regs[i].address == addr) {
342       return LLDB_INVALID_INDEX32; // We do not support duplicate breakpoints.
343     }
344   }
345 
346   if (bp_index == LLDB_INVALID_INDEX32)
347     return LLDB_INVALID_INDEX32;
348 
349   // Update breakpoint in local cache
350   m_hbr_regs[bp_index].real_addr = addr;
351   m_hbr_regs[bp_index].address = addr;
352   m_hbr_regs[bp_index].control = control_value;
353 
354   // PTRACE call to set corresponding hardware breakpoint register.
355   error = WriteHardwareDebugRegs(eDREGTypeBREAK, bp_index);
356 
357   if (error.Fail()) {
358     m_hbr_regs[bp_index].address = 0;
359     m_hbr_regs[bp_index].control &= ~1;
360 
361     return LLDB_INVALID_INDEX32;
362   }
363 
364   return bp_index;
365 }
366 
367 bool NativeRegisterContextLinux_arm::ClearHardwareBreakpoint(uint32_t hw_idx) {
368   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS));
369   LLDB_LOG(log, "hw_idx: {0}", hw_idx);
370 
371   // Read hardware breakpoint and watchpoint information.
372   Status error = ReadHardwareDebugInfo();
373 
374   if (error.Fail())
375     return false;
376 
377   if (hw_idx >= m_max_hbp_supported)
378     return false;
379 
380   // Create a backup we can revert to in case of failure.
381   lldb::addr_t tempAddr = m_hbr_regs[hw_idx].address;
382   uint32_t tempControl = m_hbr_regs[hw_idx].control;
383 
384   m_hbr_regs[hw_idx].control &= ~1;
385   m_hbr_regs[hw_idx].address = 0;
386 
387   // PTRACE call to clear corresponding hardware breakpoint register.
388   error = WriteHardwareDebugRegs(eDREGTypeBREAK, hw_idx);
389 
390   if (error.Fail()) {
391     m_hbr_regs[hw_idx].control = tempControl;
392     m_hbr_regs[hw_idx].address = tempAddr;
393 
394     return false;
395   }
396 
397   return true;
398 }
399 
400 Status NativeRegisterContextLinux_arm::GetHardwareBreakHitIndex(
401     uint32_t &bp_index, lldb::addr_t trap_addr) {
402   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS));
403 
404   LLDB_LOGF(log, "NativeRegisterContextLinux_arm64::%s()", __FUNCTION__);
405 
406   lldb::addr_t break_addr;
407 
408   for (bp_index = 0; bp_index < m_max_hbp_supported; ++bp_index) {
409     break_addr = m_hbr_regs[bp_index].address;
410 
411     if ((m_hbr_regs[bp_index].control & 0x1) && (trap_addr == break_addr)) {
412       m_hbr_regs[bp_index].hit_addr = trap_addr;
413       return Status();
414     }
415   }
416 
417   bp_index = LLDB_INVALID_INDEX32;
418   return Status();
419 }
420 
421 Status NativeRegisterContextLinux_arm::ClearAllHardwareBreakpoints() {
422   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS));
423 
424   LLDB_LOGF(log, "NativeRegisterContextLinux_arm::%s()", __FUNCTION__);
425 
426   Status error;
427 
428   // Read hardware breakpoint and watchpoint information.
429   error = ReadHardwareDebugInfo();
430 
431   if (error.Fail())
432     return error;
433 
434   lldb::addr_t tempAddr = 0;
435   uint32_t tempControl = 0;
436 
437   for (uint32_t i = 0; i < m_max_hbp_supported; i++) {
438     if (m_hbr_regs[i].control & 0x01) {
439       // Create a backup we can revert to in case of failure.
440       tempAddr = m_hbr_regs[i].address;
441       tempControl = m_hbr_regs[i].control;
442 
443       // Clear breakpoints in local cache
444       m_hbr_regs[i].control &= ~1;
445       m_hbr_regs[i].address = 0;
446 
447       // Ptrace call to update hardware debug registers
448       error = WriteHardwareDebugRegs(eDREGTypeBREAK, i);
449 
450       if (error.Fail()) {
451         m_hbr_regs[i].control = tempControl;
452         m_hbr_regs[i].address = tempAddr;
453 
454         return error;
455       }
456     }
457   }
458 
459   return Status();
460 }
461 
462 uint32_t NativeRegisterContextLinux_arm::NumSupportedHardwareWatchpoints() {
463   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
464 
465   // Read hardware breakpoint and watchpoint information.
466   Status error = ReadHardwareDebugInfo();
467 
468   if (error.Fail())
469     return 0;
470 
471   LLDB_LOG(log, "{0}", m_max_hwp_supported);
472   return m_max_hwp_supported;
473 }
474 
475 uint32_t NativeRegisterContextLinux_arm::SetHardwareWatchpoint(
476     lldb::addr_t addr, size_t size, uint32_t watch_flags) {
477   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
478   LLDB_LOG(log, "addr: {0:x}, size: {1:x} watch_flags: {2:x}", addr, size,
479            watch_flags);
480 
481   // Read hardware breakpoint and watchpoint information.
482   Status error = ReadHardwareDebugInfo();
483 
484   if (error.Fail())
485     return LLDB_INVALID_INDEX32;
486 
487   uint32_t control_value = 0, wp_index = 0, addr_word_offset = 0, byte_mask = 0;
488   lldb::addr_t real_addr = addr;
489 
490   // Check if we are setting watchpoint other than read/write/access Also
491   // update watchpoint flag to match Arm write-read bit configuration.
492   switch (watch_flags) {
493   case 1:
494     watch_flags = 2;
495     break;
496   case 2:
497     watch_flags = 1;
498     break;
499   case 3:
500     break;
501   default:
502     return LLDB_INVALID_INDEX32;
503   }
504 
505   // Can't watch zero bytes
506   // Can't watch more than 4 bytes per WVR/WCR pair
507 
508   if (size == 0 || size > 4)
509     return LLDB_INVALID_INDEX32;
510 
511   // Check 4-byte alignment for hardware watchpoint target address. Below is a
512   // hack to recalculate address and size in order to make sure we can watch
513   // non 4-byte aligned addresses as well.
514   if (addr & 0x03) {
515     uint8_t watch_mask = (addr & 0x03) + size;
516 
517     if (watch_mask > 0x04)
518       return LLDB_INVALID_INDEX32;
519     else if (watch_mask <= 0x02)
520       size = 2;
521     else if (watch_mask <= 0x04)
522       size = 4;
523 
524     addr = addr & (~0x03);
525   }
526 
527   // We can only watch up to four bytes that follow a 4 byte aligned address
528   // per watchpoint register pair, so make sure we can properly encode this.
529   addr_word_offset = addr % 4;
530   byte_mask = ((1u << size) - 1u) << addr_word_offset;
531 
532   // Check if we need multiple watchpoint register
533   if (byte_mask > 0xfu)
534     return LLDB_INVALID_INDEX32;
535 
536   // Setup control value
537   // Make the byte_mask into a valid Byte Address Select mask
538   control_value = byte_mask << 5;
539 
540   // Turn on appropriate watchpoint flags read or write
541   control_value |= (watch_flags << 3);
542 
543   // Enable this watchpoint and make it stop in privileged or user mode;
544   control_value |= 7;
545 
546   // Make sure bits 1:0 are clear in our address
547   addr &= ~((lldb::addr_t)3);
548 
549   // Iterate over stored watchpoints and find a free wp_index
550   wp_index = LLDB_INVALID_INDEX32;
551   for (uint32_t i = 0; i < m_max_hwp_supported; i++) {
552     if ((m_hwp_regs[i].control & 1) == 0) {
553       wp_index = i; // Mark last free slot
554     } else if (m_hwp_regs[i].address == addr) {
555       return LLDB_INVALID_INDEX32; // We do not support duplicate watchpoints.
556     }
557   }
558 
559   if (wp_index == LLDB_INVALID_INDEX32)
560     return LLDB_INVALID_INDEX32;
561 
562   // Update watchpoint in local cache
563   m_hwp_regs[wp_index].real_addr = real_addr;
564   m_hwp_regs[wp_index].address = addr;
565   m_hwp_regs[wp_index].control = control_value;
566 
567   // PTRACE call to set corresponding watchpoint register.
568   error = WriteHardwareDebugRegs(eDREGTypeWATCH, wp_index);
569 
570   if (error.Fail()) {
571     m_hwp_regs[wp_index].address = 0;
572     m_hwp_regs[wp_index].control &= ~1;
573 
574     return LLDB_INVALID_INDEX32;
575   }
576 
577   return wp_index;
578 }
579 
580 bool NativeRegisterContextLinux_arm::ClearHardwareWatchpoint(
581     uint32_t wp_index) {
582   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
583   LLDB_LOG(log, "wp_index: {0}", wp_index);
584 
585   // Read hardware breakpoint and watchpoint information.
586   Status error = ReadHardwareDebugInfo();
587 
588   if (error.Fail())
589     return false;
590 
591   if (wp_index >= m_max_hwp_supported)
592     return false;
593 
594   // Create a backup we can revert to in case of failure.
595   lldb::addr_t tempAddr = m_hwp_regs[wp_index].address;
596   uint32_t tempControl = m_hwp_regs[wp_index].control;
597 
598   // Update watchpoint in local cache
599   m_hwp_regs[wp_index].control &= ~1;
600   m_hwp_regs[wp_index].address = 0;
601 
602   // Ptrace call to update hardware debug registers
603   error = WriteHardwareDebugRegs(eDREGTypeWATCH, wp_index);
604 
605   if (error.Fail()) {
606     m_hwp_regs[wp_index].control = tempControl;
607     m_hwp_regs[wp_index].address = tempAddr;
608 
609     return false;
610   }
611 
612   return true;
613 }
614 
615 Status NativeRegisterContextLinux_arm::ClearAllHardwareWatchpoints() {
616   // Read hardware breakpoint and watchpoint information.
617   Status error = ReadHardwareDebugInfo();
618 
619   if (error.Fail())
620     return error;
621 
622   lldb::addr_t tempAddr = 0;
623   uint32_t tempControl = 0;
624 
625   for (uint32_t i = 0; i < m_max_hwp_supported; i++) {
626     if (m_hwp_regs[i].control & 0x01) {
627       // Create a backup we can revert to in case of failure.
628       tempAddr = m_hwp_regs[i].address;
629       tempControl = m_hwp_regs[i].control;
630 
631       // Clear watchpoints in local cache
632       m_hwp_regs[i].control &= ~1;
633       m_hwp_regs[i].address = 0;
634 
635       // Ptrace call to update hardware debug registers
636       error = WriteHardwareDebugRegs(eDREGTypeWATCH, i);
637 
638       if (error.Fail()) {
639         m_hwp_regs[i].control = tempControl;
640         m_hwp_regs[i].address = tempAddr;
641 
642         return error;
643       }
644     }
645   }
646 
647   return Status();
648 }
649 
650 uint32_t NativeRegisterContextLinux_arm::GetWatchpointSize(uint32_t wp_index) {
651   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
652   LLDB_LOG(log, "wp_index: {0}", wp_index);
653 
654   switch ((m_hwp_regs[wp_index].control >> 5) & 0x0f) {
655   case 0x01:
656     return 1;
657   case 0x03:
658     return 2;
659   case 0x07:
660     return 3;
661   case 0x0f:
662     return 4;
663   default:
664     return 0;
665   }
666 }
667 bool NativeRegisterContextLinux_arm::WatchpointIsEnabled(uint32_t wp_index) {
668   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
669   LLDB_LOG(log, "wp_index: {0}", wp_index);
670 
671   if ((m_hwp_regs[wp_index].control & 0x1) == 0x1)
672     return true;
673   else
674     return false;
675 }
676 
677 Status
678 NativeRegisterContextLinux_arm::GetWatchpointHitIndex(uint32_t &wp_index,
679                                                       lldb::addr_t trap_addr) {
680   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
681   LLDB_LOG(log, "wp_index: {0}, trap_addr: {1:x}", wp_index, trap_addr);
682 
683   uint32_t watch_size;
684   lldb::addr_t watch_addr;
685 
686   for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) {
687     watch_size = GetWatchpointSize(wp_index);
688     watch_addr = m_hwp_regs[wp_index].address;
689 
690     if (WatchpointIsEnabled(wp_index) && trap_addr >= watch_addr &&
691         trap_addr < watch_addr + watch_size) {
692       m_hwp_regs[wp_index].hit_addr = trap_addr;
693       return Status();
694     }
695   }
696 
697   wp_index = LLDB_INVALID_INDEX32;
698   return Status();
699 }
700 
701 lldb::addr_t
702 NativeRegisterContextLinux_arm::GetWatchpointAddress(uint32_t wp_index) {
703   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
704   LLDB_LOG(log, "wp_index: {0}", wp_index);
705 
706   if (wp_index >= m_max_hwp_supported)
707     return LLDB_INVALID_ADDRESS;
708 
709   if (WatchpointIsEnabled(wp_index))
710     return m_hwp_regs[wp_index].real_addr;
711   else
712     return LLDB_INVALID_ADDRESS;
713 }
714 
715 lldb::addr_t
716 NativeRegisterContextLinux_arm::GetWatchpointHitAddress(uint32_t wp_index) {
717   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
718   LLDB_LOG(log, "wp_index: {0}", wp_index);
719 
720   if (wp_index >= m_max_hwp_supported)
721     return LLDB_INVALID_ADDRESS;
722 
723   if (WatchpointIsEnabled(wp_index))
724     return m_hwp_regs[wp_index].hit_addr;
725   else
726     return LLDB_INVALID_ADDRESS;
727 }
728 
729 Status NativeRegisterContextLinux_arm::ReadHardwareDebugInfo() {
730   Status error;
731 
732   if (!m_refresh_hwdebug_info) {
733     return Status();
734   }
735 
736   unsigned int cap_val;
737 
738   error = NativeProcessLinux::PtraceWrapper(PTRACE_GETHBPREGS, m_thread.GetID(),
739                                             nullptr, &cap_val,
740                                             sizeof(unsigned int));
741 
742   if (error.Fail())
743     return error;
744 
745   m_max_hwp_supported = (cap_val >> 8) & 0xff;
746   m_max_hbp_supported = cap_val & 0xff;
747   m_refresh_hwdebug_info = false;
748 
749   return error;
750 }
751 
752 Status NativeRegisterContextLinux_arm::WriteHardwareDebugRegs(int hwbType,
753                                                               int hwb_index) {
754   Status error;
755 
756   lldb::addr_t *addr_buf;
757   uint32_t *ctrl_buf;
758 
759   if (hwbType == eDREGTypeWATCH) {
760     addr_buf = &m_hwp_regs[hwb_index].address;
761     ctrl_buf = &m_hwp_regs[hwb_index].control;
762 
763     error = NativeProcessLinux::PtraceWrapper(
764         PTRACE_SETHBPREGS, m_thread.GetID(),
765         (PTRACE_TYPE_ARG3)(intptr_t) - ((hwb_index << 1) + 1), addr_buf,
766         sizeof(unsigned int));
767 
768     if (error.Fail())
769       return error;
770 
771     error = NativeProcessLinux::PtraceWrapper(
772         PTRACE_SETHBPREGS, m_thread.GetID(),
773         (PTRACE_TYPE_ARG3)(intptr_t) - ((hwb_index << 1) + 2), ctrl_buf,
774         sizeof(unsigned int));
775   } else {
776     addr_buf = &m_hbr_regs[hwb_index].address;
777     ctrl_buf = &m_hbr_regs[hwb_index].control;
778 
779     error = NativeProcessLinux::PtraceWrapper(
780         PTRACE_SETHBPREGS, m_thread.GetID(),
781         (PTRACE_TYPE_ARG3)(intptr_t)((hwb_index << 1) + 1), addr_buf,
782         sizeof(unsigned int));
783 
784     if (error.Fail())
785       return error;
786 
787     error = NativeProcessLinux::PtraceWrapper(
788         PTRACE_SETHBPREGS, m_thread.GetID(),
789         (PTRACE_TYPE_ARG3)(intptr_t)((hwb_index << 1) + 2), ctrl_buf,
790         sizeof(unsigned int));
791   }
792 
793   return error;
794 }
795 
796 uint32_t NativeRegisterContextLinux_arm::CalculateFprOffset(
797     const RegisterInfo *reg_info) const {
798   return reg_info->byte_offset - GetGPRSize();
799 }
800 
801 Status NativeRegisterContextLinux_arm::DoReadRegisterValue(
802     uint32_t offset, const char *reg_name, uint32_t size,
803     RegisterValue &value) {
804   // PTRACE_PEEKUSER don't work in the aarch64 linux kernel used on android
805   // devices (always return "Bad address"). To avoid using PTRACE_PEEKUSER we
806   // read out the full GPR register set instead. This approach is about 4 times
807   // slower but the performance overhead is negligible in comparison to
808   // processing time in lldb-server.
809   assert(offset % 4 == 0 && "Try to write a register with unaligned offset");
810   if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm))
811     return Status("Register isn't fit into the size of the GPR area");
812 
813   Status error = ReadGPR();
814   if (error.Fail())
815     return error;
816 
817   value.SetUInt32(m_gpr_arm[offset / sizeof(uint32_t)]);
818   return Status();
819 }
820 
821 Status NativeRegisterContextLinux_arm::DoWriteRegisterValue(
822     uint32_t offset, const char *reg_name, const RegisterValue &value) {
823   // PTRACE_POKEUSER don't work in the aarch64 linux kernel used on android
824   // devices (always return "Bad address"). To avoid using PTRACE_POKEUSER we
825   // read out the full GPR register set, modify the requested register and
826   // write it back. This approach is about 4 times slower but the performance
827   // overhead is negligible in comparison to processing time in lldb-server.
828   assert(offset % 4 == 0 && "Try to write a register with unaligned offset");
829   if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm))
830     return Status("Register isn't fit into the size of the GPR area");
831 
832   Status error = ReadGPR();
833   if (error.Fail())
834     return error;
835 
836   uint32_t reg_value = value.GetAsUInt32();
837   // As precaution for an undefined behavior encountered while setting PC we
838   // will clear thumb bit of new PC if we are already in thumb mode; that is
839   // CPSR thumb mode bit is set.
840   if (offset / sizeof(uint32_t) == gpr_pc_arm) {
841     // Check if we are already in thumb mode and thumb bit of current PC is
842     // read out to be zero and thumb bit of next PC is read out to be one.
843     if ((m_gpr_arm[gpr_cpsr_arm] & 0x20) && !(m_gpr_arm[gpr_pc_arm] & 0x01) &&
844         (value.GetAsUInt32() & 0x01)) {
845       reg_value &= (~1ull);
846     }
847   }
848 
849   m_gpr_arm[offset / sizeof(uint32_t)] = reg_value;
850   return WriteGPR();
851 }
852 
853 Status NativeRegisterContextLinux_arm::ReadGPR() {
854 #ifdef __arm__
855   return NativeRegisterContextLinux::ReadGPR();
856 #else  // __aarch64__
857   struct iovec ioVec;
858   ioVec.iov_base = GetGPRBuffer();
859   ioVec.iov_len = GetGPRSize();
860 
861   return ReadRegisterSet(&ioVec, GetGPRSize(), NT_PRSTATUS);
862 #endif // __arm__
863 }
864 
865 Status NativeRegisterContextLinux_arm::WriteGPR() {
866 #ifdef __arm__
867   return NativeRegisterContextLinux::WriteGPR();
868 #else  // __aarch64__
869   struct iovec ioVec;
870   ioVec.iov_base = GetGPRBuffer();
871   ioVec.iov_len = GetGPRSize();
872 
873   return WriteRegisterSet(&ioVec, GetGPRSize(), NT_PRSTATUS);
874 #endif // __arm__
875 }
876 
877 Status NativeRegisterContextLinux_arm::ReadFPR() {
878 #ifdef __arm__
879   return NativeProcessLinux::PtraceWrapper(PTRACE_GETVFPREGS, m_thread.GetID(),
880                                            nullptr, GetFPRBuffer(),
881                                            GetFPRSize());
882 #else  // __aarch64__
883   struct iovec ioVec;
884   ioVec.iov_base = GetFPRBuffer();
885   ioVec.iov_len = GetFPRSize();
886 
887   return ReadRegisterSet(&ioVec, GetFPRSize(), NT_ARM_VFP);
888 #endif // __arm__
889 }
890 
891 Status NativeRegisterContextLinux_arm::WriteFPR() {
892 #ifdef __arm__
893   return NativeProcessLinux::PtraceWrapper(PTRACE_SETVFPREGS, m_thread.GetID(),
894                                            nullptr, GetFPRBuffer(),
895                                            GetFPRSize());
896 #else  // __aarch64__
897   struct iovec ioVec;
898   ioVec.iov_base = GetFPRBuffer();
899   ioVec.iov_len = GetFPRSize();
900 
901   return WriteRegisterSet(&ioVec, GetFPRSize(), NT_ARM_VFP);
902 #endif // __arm__
903 }
904 
905 #endif // defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
906