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/NativeRegisterContextDBReg_arm64.h"
16 #include "Plugins/Process/Utility/RegisterInfoPOSIX_arm64.h"
17 
18 #include <asm/ptrace.h>
19 
20 namespace lldb_private {
21 namespace process_linux {
22 
23 class NativeProcessLinux;
24 
25 class NativeRegisterContextLinux_arm64
26     : public NativeRegisterContextLinux,
27       public NativeRegisterContextDBReg_arm64 {
28 public:
29   NativeRegisterContextLinux_arm64(
30       const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
31       std::unique_ptr<RegisterInfoPOSIX_arm64> register_info_up);
32 
33   uint32_t GetRegisterSetCount() const override;
34 
35   uint32_t GetUserRegisterCount() const override;
36 
37   const RegisterSet *GetRegisterSet(uint32_t set_index) const override;
38 
39   Status ReadRegister(const RegisterInfo *reg_info,
40                       RegisterValue &reg_value) override;
41 
42   Status WriteRegister(const RegisterInfo *reg_info,
43                        const RegisterValue &reg_value) override;
44 
45   Status ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override;
46 
47   Status WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
48 
49   void InvalidateAllRegisters() override;
50 
51   std::vector<uint32_t>
52   GetExpeditedRegisters(ExpeditedRegs expType) const override;
53 
54   bool RegisterOffsetIsDynamic() const override { return true; }
55 
56 protected:
57   Status ReadGPR() override;
58 
59   Status WriteGPR() override;
60 
61   Status ReadFPR() override;
62 
63   Status WriteFPR() override;
64 
65   void *GetGPRBuffer() override { return &m_gpr_arm64; }
66 
67   // GetGPRBufferSize returns sizeof arm64 GPR ptrace buffer, it is different
68   // from GetGPRSize which returns sizeof RegisterInfoPOSIX_arm64::GPR.
69   size_t GetGPRBufferSize() { return sizeof(m_gpr_arm64); }
70 
71   void *GetFPRBuffer() override { return &m_fpr; }
72 
73   size_t GetFPRSize() override { return sizeof(m_fpr); }
74 
75 private:
76   bool m_gpr_is_valid;
77   bool m_fpu_is_valid;
78   bool m_sve_buffer_is_valid;
79   bool m_mte_ctrl_is_valid;
80 
81   bool m_sve_header_is_valid;
82   bool m_pac_mask_is_valid;
83 
84   struct user_pt_regs m_gpr_arm64; // 64-bit general purpose registers.
85 
86   RegisterInfoPOSIX_arm64::FPU
87       m_fpr; // floating-point registers including extended register sets.
88 
89   SVEState m_sve_state;
90   struct user_sve_header m_sve_header;
91   std::vector<uint8_t> m_sve_ptrace_payload;
92 
93   bool m_refresh_hwdebug_info;
94 
95   struct user_pac_mask {
96     uint64_t data_mask;
97     uint64_t insn_mask;
98   };
99 
100   struct user_pac_mask m_pac_mask;
101 
102   uint64_t m_mte_ctrl_reg;
103 
104   bool IsGPR(unsigned reg) const;
105 
106   bool IsFPR(unsigned reg) const;
107 
108   Status ReadAllSVE();
109 
110   Status WriteAllSVE();
111 
112   Status ReadSVEHeader();
113 
114   Status WriteSVEHeader();
115 
116   Status ReadPAuthMask();
117 
118   Status ReadMTEControl();
119 
120   Status WriteMTEControl();
121 
122   bool IsSVE(unsigned reg) const;
123   bool IsPAuth(unsigned reg) const;
124   bool IsMTE(unsigned reg) const;
125 
126   uint64_t GetSVERegVG() { return m_sve_header.vl / 8; }
127 
128   void SetSVERegVG(uint64_t vg) { m_sve_header.vl = vg * 8; }
129 
130   void *GetSVEHeader() { return &m_sve_header; }
131 
132   void *GetPACMask() { return &m_pac_mask; }
133 
134   void *GetMTEControl() { return &m_mte_ctrl_reg; }
135 
136   void *GetSVEBuffer();
137 
138   size_t GetSVEHeaderSize() { return sizeof(m_sve_header); }
139 
140   size_t GetPACMaskSize() { return sizeof(m_pac_mask); }
141 
142   size_t GetSVEBufferSize() { return m_sve_ptrace_payload.size(); }
143 
144   size_t GetMTEControlSize() { return sizeof(m_mte_ctrl_reg); }
145 
146   llvm::Error ReadHardwareDebugInfo() override;
147 
148   llvm::Error WriteHardwareDebugRegs(DREGType hwbType) override;
149 
150   uint32_t CalculateFprOffset(const RegisterInfo *reg_info) const;
151 
152   RegisterInfoPOSIX_arm64 &GetRegisterInfo() const;
153 
154   void ConfigureRegisterContext();
155 
156   uint32_t CalculateSVEOffset(const RegisterInfo *reg_info) const;
157 };
158 
159 } // namespace process_linux
160 } // namespace lldb_private
161 
162 #endif // #ifndef lldb_NativeRegisterContextLinux_arm64_h
163 
164 #endif // defined (__arm64__) || defined (__aarch64__)
165