1 //===-- NativeRegisterContextLinux_x86_64.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(__i386__) || defined(__x86_64__)
10 
11 #ifndef lldb_NativeRegisterContextLinux_x86_64_h
12 #define lldb_NativeRegisterContextLinux_x86_64_h
13 
14 #include "Plugins/Process/Linux/NativeRegisterContextLinux.h"
15 #include "Plugins/Process/Utility/RegisterContext_x86.h"
16 #include "Plugins/Process/Utility/lldb-x86-register-enums.h"
17 #include <sys/uio.h>
18 
19 namespace lldb_private {
20 namespace process_linux {
21 
22 class NativeProcessLinux;
23 
24 class NativeRegisterContextLinux_x86_64 : public NativeRegisterContextLinux {
25 public:
26   NativeRegisterContextLinux_x86_64(const ArchSpec &target_arch,
27                                     NativeThreadProtocol &native_thread);
28 
29   uint32_t GetRegisterSetCount() const override;
30 
31   const RegisterSet *GetRegisterSet(uint32_t set_index) const override;
32 
33   uint32_t GetUserRegisterCount() 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   Status IsWatchpointHit(uint32_t wp_index, bool &is_hit) override;
46 
47   Status GetWatchpointHitIndex(uint32_t &wp_index,
48                                lldb::addr_t trap_addr) override;
49 
50   Status IsWatchpointVacant(uint32_t wp_index, bool &is_vacant) override;
51 
52   bool ClearHardwareWatchpoint(uint32_t wp_index) override;
53 
54   Status ClearAllHardwareWatchpoints() override;
55 
56   Status SetHardwareWatchpointWithIndex(lldb::addr_t addr, size_t size,
57                                         uint32_t watch_flags,
58                                         uint32_t wp_index);
59 
60   uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size,
61                                  uint32_t watch_flags) override;
62 
63   lldb::addr_t GetWatchpointAddress(uint32_t wp_index) override;
64 
65   uint32_t NumSupportedHardwareWatchpoints() override;
66 
67 protected:
68   void *GetGPRBuffer() override { return &m_gpr_x86_64; }
69 
70   void *GetFPRBuffer() override;
71 
72   size_t GetFPRSize() override;
73 
74   Status ReadFPR() override;
75 
76   Status WriteFPR() override;
77 
78 private:
79   // Private member types.
80   enum class XStateType { Invalid, FXSAVE, XSAVE };
81   enum class RegSet { gpr, fpu, avx, mpx };
82 
83   // Info about register ranges.
84   struct RegInfo {
85     uint32_t num_registers;
86     uint32_t num_gpr_registers;
87     uint32_t num_fpr_registers;
88     uint32_t num_avx_registers;
89     uint32_t num_mpx_registers;
90     uint32_t last_gpr;
91     uint32_t first_fpr;
92     uint32_t last_fpr;
93     uint32_t first_st;
94     uint32_t last_st;
95     uint32_t first_mm;
96     uint32_t last_mm;
97     uint32_t first_xmm;
98     uint32_t last_xmm;
99     uint32_t first_ymm;
100     uint32_t last_ymm;
101     uint32_t first_mpxr;
102     uint32_t last_mpxr;
103     uint32_t first_mpxc;
104     uint32_t last_mpxc;
105     uint32_t first_dr;
106     uint32_t gpr_flags;
107   };
108 
109   // Private member variables.
110   mutable XStateType m_xstate_type;
111   std::unique_ptr<FPR, llvm::FreeDeleter>
112       m_xstate; // Extended States Area, named FPR for historical reasons.
113   struct iovec m_iovec;
114   YMM m_ymm_set;
115   MPX m_mpx_set;
116   RegInfo m_reg_info;
117   uint64_t m_gpr_x86_64[k_num_gpr_registers_x86_64];
118   uint32_t m_fctrl_offset_in_userarea;
119 
120   // Private member methods.
121   bool IsCPUFeatureAvailable(RegSet feature_code) const;
122 
123   bool IsRegisterSetAvailable(uint32_t set_index) const;
124 
125   bool IsGPR(uint32_t reg_index) const;
126 
127   bool IsFPR(uint32_t reg_index) const;
128 
129   bool CopyXSTATEtoYMM(uint32_t reg_index, lldb::ByteOrder byte_order);
130 
131   bool CopyYMMtoXSTATE(uint32_t reg, lldb::ByteOrder byte_order);
132 
133   bool IsAVX(uint32_t reg_index) const;
134 
135   bool CopyXSTATEtoMPX(uint32_t reg);
136 
137   bool CopyMPXtoXSTATE(uint32_t reg);
138 
139   bool IsMPX(uint32_t reg_index) const;
140 
141   void UpdateXSTATEforWrite(uint32_t reg_index);
142 };
143 
144 } // namespace process_linux
145 } // namespace lldb_private
146 
147 #endif // #ifndef lldb_NativeRegisterContextLinux_x86_64_h
148 
149 #endif // defined(__i386__) || defined(__x86_64__)
150