1 //===-- NativeRegisterContextLinux_arm64.h ---------------------*- 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 #if defined(__arm64__) || defined(__aarch64__)
10 
11 #ifndef lldb_NativeRegisterContextLinux_arm64_h
12 #define lldb_NativeRegisterContextLinux_arm64_h
13 
14 #include "Plugins/Process/Linux/NativeRegisterContextLinux.h"
15 #include "Plugins/Process/Utility/RegisterInfoPOSIX_arm64.h"
16 
17 #include <asm/ptrace.h>
18 
19 namespace lldb_private {
20 namespace process_linux {
21 
22 class NativeProcessLinux;
23 
24 class NativeRegisterContextLinux_arm64 : public NativeRegisterContextLinux {
25 public:
26   NativeRegisterContextLinux_arm64(const ArchSpec &target_arch,
27                                    NativeThreadProtocol &native_thread);
28 
29   uint32_t GetRegisterSetCount() const override;
30 
31   uint32_t GetUserRegisterCount() const override;
32 
33   const RegisterSet *GetRegisterSet(uint32_t set_index) const override;
34 
35   Status ReadRegister(const RegisterInfo *reg_info,
36                       RegisterValue &reg_value) override;
37 
38   Status WriteRegister(const RegisterInfo *reg_info,
39                        const RegisterValue &reg_value) override;
40 
41   Status ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override;
42 
43   Status WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
44 
45   void InvalidateAllRegisters() override;
46 
47   std::vector<uint32_t>
48   GetExpeditedRegisters(ExpeditedRegs expType) const override;
49 
50   // Hardware breakpoints/watchpoint management functions
51 
52   uint32_t NumSupportedHardwareBreakpoints() override;
53 
54   uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override;
55 
56   bool ClearHardwareBreakpoint(uint32_t hw_idx) override;
57 
58   Status ClearAllHardwareBreakpoints() override;
59 
60   Status GetHardwareBreakHitIndex(uint32_t &bp_index,
61                                   lldb::addr_t trap_addr) override;
62 
63   uint32_t NumSupportedHardwareWatchpoints() override;
64 
65   uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size,
66                                  uint32_t watch_flags) override;
67 
68   bool ClearHardwareWatchpoint(uint32_t hw_index) override;
69 
70   Status ClearAllHardwareWatchpoints() override;
71 
72   Status GetWatchpointHitIndex(uint32_t &wp_index,
73                                lldb::addr_t trap_addr) override;
74 
75   lldb::addr_t GetWatchpointHitAddress(uint32_t wp_index) override;
76 
77   lldb::addr_t GetWatchpointAddress(uint32_t wp_index) override;
78 
79   uint32_t GetWatchpointSize(uint32_t wp_index);
80 
81   bool WatchpointIsEnabled(uint32_t wp_index);
82 
83   // Debug register type select
84   enum DREGType { eDREGTypeWATCH = 0, eDREGTypeBREAK };
85 
86 protected:
87 
88   Status ReadGPR() override;
89 
90   Status WriteGPR() override;
91 
92   Status ReadFPR() override;
93 
94   Status WriteFPR() override;
95 
96   void *GetGPRBuffer() override { return &m_gpr_arm64; }
97 
98   // GetGPRBufferSize returns sizeof arm64 GPR ptrace buffer, it is different
99   // from GetGPRSize which returns sizeof RegisterInfoPOSIX_arm64::GPR.
100   size_t GetGPRBufferSize() { return sizeof(m_gpr_arm64); }
101 
102   void *GetFPRBuffer() override { return &m_fpr; }
103 
104   size_t GetFPRSize() override { return sizeof(m_fpr); }
105 
106 private:
107   bool m_gpr_is_valid;
108   bool m_fpu_is_valid;
109   bool m_sve_buffer_is_valid;
110 
111   bool m_sve_header_is_valid;
112 
113   struct user_pt_regs m_gpr_arm64; // 64-bit general purpose registers.
114 
115   RegisterInfoPOSIX_arm64::FPU
116       m_fpr; // floating-point registers including extended register sets.
117 
118   SVEState m_sve_state;
119   struct user_sve_header m_sve_header;
120   std::vector<uint8_t> m_sve_ptrace_payload;
121 
122   // Debug register info for hardware breakpoints and watchpoints management.
123   struct DREG {
124     lldb::addr_t address;  // Breakpoint/watchpoint address value.
125     lldb::addr_t hit_addr; // Address at which last watchpoint trigger exception
126                            // occurred.
127     lldb::addr_t real_addr; // Address value that should cause target to stop.
128     uint32_t control;       // Breakpoint/watchpoint control value.
129     uint32_t refcount;      // Serves as enable/disable and reference counter.
130   };
131 
132   struct DREG m_hbr_regs[16]; // Arm native linux hardware breakpoints
133   struct DREG m_hwp_regs[16]; // Arm native linux hardware watchpoints
134 
135   uint32_t m_max_hwp_supported;
136   uint32_t m_max_hbp_supported;
137   bool m_refresh_hwdebug_info;
138 
139   bool IsGPR(unsigned reg) const;
140 
141   bool IsFPR(unsigned reg) const;
142 
143   Status ReadAllSVE();
144 
145   Status WriteAllSVE();
146 
147   Status ReadSVEHeader();
148 
149   Status WriteSVEHeader();
150 
151   bool IsSVE(unsigned reg) const;
152 
153   uint64_t GetSVERegVG() { return m_sve_header.vl / 8; }
154 
155   void SetSVERegVG(uint64_t vg) { m_sve_header.vl = vg * 8; }
156 
157   void *GetSVEHeader() { return &m_sve_header; }
158 
159   void *GetSVEBuffer();
160 
161   size_t GetSVEHeaderSize() { return sizeof(m_sve_header); }
162 
163   size_t GetSVEBufferSize() { return m_sve_ptrace_payload.size(); }
164 
165   Status ReadHardwareDebugInfo();
166 
167   Status WriteHardwareDebugRegs(int hwbType);
168 
169   uint32_t CalculateFprOffset(const RegisterInfo *reg_info) const;
170 
171   RegisterInfoPOSIX_arm64 &GetRegisterInfo() const;
172 
173   void ConfigureRegisterContext();
174 
175   uint32_t CalculateSVEOffset(const RegisterInfo *reg_info) const;
176 };
177 
178 } // namespace process_linux
179 } // namespace lldb_private
180 
181 #endif // #ifndef lldb_NativeRegisterContextLinux_arm64_h
182 
183 #endif // defined (__arm64__) || defined (__aarch64__)
184