1 //===-- RegisterContextPOSIXProcessMonitor_arm64.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 #include "lldb/Target/Thread.h"
10 #include "lldb/Utility/DataBufferHeap.h"
11 #include "lldb/Utility/RegisterValue.h"
12 
13 #include "Plugins/Process/Utility/RegisterContextPOSIX_arm64.h"
14 #include "ProcessFreeBSD.h"
15 #include "ProcessMonitor.h"
16 #include "RegisterContextPOSIXProcessMonitor_arm64.h"
17 
18 #define REG_CONTEXT_SIZE (GetGPRSize())
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 RegisterContextPOSIXProcessMonitor_arm64::
RegisterContextPOSIXProcessMonitor_arm64(lldb_private::Thread & thread,uint32_t concrete_frame_idx,lldb_private::RegisterInfoInterface * register_info)24     RegisterContextPOSIXProcessMonitor_arm64(
25         lldb_private::Thread &thread, uint32_t concrete_frame_idx,
26         lldb_private::RegisterInfoInterface *register_info)
27     : RegisterContextPOSIX_arm64(thread, concrete_frame_idx, register_info) {}
28 
GetMonitor()29 ProcessMonitor &RegisterContextPOSIXProcessMonitor_arm64::GetMonitor() {
30   lldb::ProcessSP base = CalculateProcess();
31   ProcessFreeBSD *process = static_cast<ProcessFreeBSD *>(base.get());
32   return process->GetMonitor();
33 }
34 
ReadGPR()35 bool RegisterContextPOSIXProcessMonitor_arm64::ReadGPR() {
36   ProcessMonitor &monitor = GetMonitor();
37   return monitor.ReadGPR(m_thread.GetID(), &m_gpr_arm64, GetGPRSize());
38 }
39 
ReadFPR()40 bool RegisterContextPOSIXProcessMonitor_arm64::ReadFPR() {
41   ProcessMonitor &monitor = GetMonitor();
42   return monitor.ReadFPR(m_thread.GetID(), &m_fpr, sizeof m_fpr);
43 }
44 
WriteGPR()45 bool RegisterContextPOSIXProcessMonitor_arm64::WriteGPR() {
46   ProcessMonitor &monitor = GetMonitor();
47   return monitor.WriteGPR(m_thread.GetID(), &m_gpr_arm64, GetGPRSize());
48 }
49 
WriteFPR()50 bool RegisterContextPOSIXProcessMonitor_arm64::WriteFPR() {
51   ProcessMonitor &monitor = GetMonitor();
52   return monitor.WriteFPR(m_thread.GetID(), &m_fpr, sizeof m_fpr);
53 }
54 
ReadRegister(const unsigned reg,lldb_private::RegisterValue & value)55 bool RegisterContextPOSIXProcessMonitor_arm64::ReadRegister(
56     const unsigned reg, lldb_private::RegisterValue &value) {
57   ProcessMonitor &monitor = GetMonitor();
58   return monitor.ReadRegisterValue(m_thread.GetID(), GetRegisterOffset(reg),
59                                    GetRegisterName(reg), GetRegisterSize(reg),
60                                    value);
61 }
62 
WriteRegister(const unsigned reg,const lldb_private::RegisterValue & value)63 bool RegisterContextPOSIXProcessMonitor_arm64::WriteRegister(
64     const unsigned reg, const lldb_private::RegisterValue &value) {
65   unsigned reg_to_write = reg;
66   lldb_private::RegisterValue value_to_write = value;
67 
68   // Check if this is a subregister of a full register.
69   const lldb_private::RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
70   if (reg_info->invalidate_regs &&
71       (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM)) {
72     lldb_private::RegisterValue full_value;
73     uint32_t full_reg = reg_info->invalidate_regs[0];
74     const lldb_private::RegisterInfo *full_reg_info =
75         GetRegisterInfoAtIndex(full_reg);
76 
77     // Read the full register.
78     if (ReadRegister(full_reg_info, full_value)) {
79       lldb_private::Status error;
80       lldb::ByteOrder byte_order = GetByteOrder();
81       uint8_t dst[lldb_private::RegisterValue::kMaxRegisterByteSize];
82 
83       // Get the bytes for the full register.
84       const uint32_t dest_size = full_value.GetAsMemoryData(
85           full_reg_info, dst, sizeof(dst), byte_order, error);
86       if (error.Success() && dest_size) {
87         uint8_t src[lldb_private::RegisterValue::kMaxRegisterByteSize];
88 
89         // Get the bytes for the source data.
90         const uint32_t src_size = value.GetAsMemoryData(
91             reg_info, src, sizeof(src), byte_order, error);
92         if (error.Success() && src_size && (src_size < dest_size)) {
93           // Copy the src bytes to the destination.
94           ::memcpy(dst + (reg_info->byte_offset & 0x1), src, src_size);
95           // Set this full register as the value to write.
96           value_to_write.SetBytes(dst, full_value.GetByteSize(), byte_order);
97           value_to_write.SetType(full_reg_info);
98           reg_to_write = full_reg;
99         }
100       }
101     }
102   }
103 
104   ProcessMonitor &monitor = GetMonitor();
105   return monitor.WriteRegisterValue(
106       m_thread.GetID(), GetRegisterOffset(reg_to_write),
107       GetRegisterName(reg_to_write), value_to_write);
108 }
109 
ReadRegister(const lldb_private::RegisterInfo * reg_info,lldb_private::RegisterValue & value)110 bool RegisterContextPOSIXProcessMonitor_arm64::ReadRegister(
111     const lldb_private::RegisterInfo *reg_info,
112     lldb_private::RegisterValue &value) {
113   if (!reg_info)
114     return false;
115 
116   const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
117 
118   if (IsFPR(reg)) {
119     if (!ReadFPR())
120       return false;
121   } else {
122     uint32_t full_reg = reg;
123     bool is_subreg = reg_info->invalidate_regs &&
124                      (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM);
125 
126     if (is_subreg) {
127       // Read the full aligned 64-bit register.
128       full_reg = reg_info->invalidate_regs[0];
129     }
130     return ReadRegister(full_reg, value);
131   }
132 
133   // Get pointer to m_fpr variable and set the data from it.
134   assert(reg_info->byte_offset < sizeof m_fpr);
135   uint8_t *src = (uint8_t *)&m_fpr + reg_info->byte_offset;
136   switch (reg_info->byte_size) {
137   case 2:
138     value.SetUInt16(*(uint16_t *)src);
139     return true;
140   case 4:
141     value.SetUInt32(*(uint32_t *)src);
142     return true;
143   case 8:
144     value.SetUInt64(*(uint64_t *)src);
145     return true;
146   default:
147     assert(false && "Unhandled data size.");
148     return false;
149   }
150 }
151 
WriteRegister(const lldb_private::RegisterInfo * reg_info,const lldb_private::RegisterValue & value)152 bool RegisterContextPOSIXProcessMonitor_arm64::WriteRegister(
153     const lldb_private::RegisterInfo *reg_info,
154     const lldb_private::RegisterValue &value) {
155   const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
156 
157   if (IsGPR(reg))
158     return WriteRegister(reg, value);
159 
160   return false;
161 }
162 
ReadAllRegisterValues(lldb::DataBufferSP & data_sp)163 bool RegisterContextPOSIXProcessMonitor_arm64::ReadAllRegisterValues(
164     lldb::DataBufferSP &data_sp) {
165   bool success = false;
166   data_sp.reset(new lldb_private::DataBufferHeap(REG_CONTEXT_SIZE, 0));
167   if (ReadGPR() && ReadFPR()) {
168     uint8_t *dst = data_sp->GetBytes();
169     success = dst != 0;
170 
171     if (success) {
172       ::memcpy(dst, &m_gpr_arm64, GetGPRSize());
173       dst += GetGPRSize();
174       ::memcpy(dst, &m_fpr, sizeof m_fpr);
175     }
176   }
177   return success;
178 }
179 
WriteAllRegisterValues(const lldb::DataBufferSP & data_sp)180 bool RegisterContextPOSIXProcessMonitor_arm64::WriteAllRegisterValues(
181     const lldb::DataBufferSP &data_sp) {
182   bool success = false;
183   if (data_sp && data_sp->GetByteSize() == REG_CONTEXT_SIZE) {
184     uint8_t *src = data_sp->GetBytes();
185     if (src) {
186       ::memcpy(&m_gpr_arm64, src, GetGPRSize());
187       if (WriteGPR()) {
188         src += GetGPRSize();
189         ::memcpy(&m_fpr, src, sizeof m_fpr);
190         success = WriteFPR();
191       }
192     }
193   }
194   return success;
195 }
196 
SetHardwareWatchpoint(lldb::addr_t addr,size_t size,bool read,bool write)197 uint32_t RegisterContextPOSIXProcessMonitor_arm64::SetHardwareWatchpoint(
198     lldb::addr_t addr, size_t size, bool read, bool write) {
199   const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints();
200   uint32_t hw_index;
201 
202   for (hw_index = 0; hw_index < num_hw_watchpoints; ++hw_index) {
203     if (IsWatchpointVacant(hw_index))
204       return SetHardwareWatchpointWithIndex(addr, size, read, write, hw_index);
205   }
206 
207   return LLDB_INVALID_INDEX32;
208 }
209 
ClearHardwareWatchpoint(uint32_t hw_index)210 bool RegisterContextPOSIXProcessMonitor_arm64::ClearHardwareWatchpoint(
211     uint32_t hw_index) {
212   return false;
213 }
214 
HardwareSingleStep(bool enable)215 bool RegisterContextPOSIXProcessMonitor_arm64::HardwareSingleStep(bool enable) {
216   return false;
217 }
218 
UpdateAfterBreakpoint()219 bool RegisterContextPOSIXProcessMonitor_arm64::UpdateAfterBreakpoint() {
220   if (GetPC() == LLDB_INVALID_ADDRESS)
221     return false;
222 
223   return true;
224 }
225 
GetRegisterIndexFromOffset(unsigned offset)226 unsigned RegisterContextPOSIXProcessMonitor_arm64::GetRegisterIndexFromOffset(
227     unsigned offset) {
228   unsigned reg;
229   for (reg = 0; reg < k_num_registers_arm64; reg++) {
230     if (GetRegisterInfo()[reg].byte_offset == offset)
231       break;
232   }
233   assert(reg < k_num_registers_arm64 && "Invalid register offset.");
234   return reg;
235 }
236 
IsWatchpointHit(uint32_t hw_index)237 bool RegisterContextPOSIXProcessMonitor_arm64::IsWatchpointHit(
238     uint32_t hw_index) {
239   return false;
240 }
241 
ClearWatchpointHits()242 bool RegisterContextPOSIXProcessMonitor_arm64::ClearWatchpointHits() {
243   return false;
244 }
245 
GetWatchpointAddress(uint32_t hw_index)246 lldb::addr_t RegisterContextPOSIXProcessMonitor_arm64::GetWatchpointAddress(
247     uint32_t hw_index) {
248   return LLDB_INVALID_ADDRESS;
249 }
250 
IsWatchpointVacant(uint32_t hw_index)251 bool RegisterContextPOSIXProcessMonitor_arm64::IsWatchpointVacant(
252     uint32_t hw_index) {
253   return false;
254 }
255 
SetHardwareWatchpointWithIndex(lldb::addr_t addr,size_t size,bool read,bool write,uint32_t hw_index)256 bool RegisterContextPOSIXProcessMonitor_arm64::SetHardwareWatchpointWithIndex(
257     lldb::addr_t addr, size_t size, bool read, bool write, uint32_t hw_index) {
258   return false;
259 }
260 
261 uint32_t
NumSupportedHardwareWatchpoints()262 RegisterContextPOSIXProcessMonitor_arm64::NumSupportedHardwareWatchpoints() {
263   return 0;
264 }
265