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/NativeRegisterContextWatchpoint_x86.h"
16 #include "Plugins/Process/Utility/RegisterContext_x86.h"
17 #include "Plugins/Process/Utility/lldb-x86-register-enums.h"
18 #include <sys/uio.h>
19 
20 namespace lldb_private {
21 namespace process_linux {
22 
23 class NativeProcessLinux;
24 
25 class NativeRegisterContextLinux_x86_64
26     : public NativeRegisterContextLinux,
27       public NativeRegisterContextWatchpoint_x86 {
28 public:
29   NativeRegisterContextLinux_x86_64(const ArchSpec &target_arch,
30                                     NativeThreadProtocol &native_thread);
31 
32   uint32_t GetRegisterSetCount() const override;
33 
34   const RegisterSet *GetRegisterSet(uint32_t set_index) const override;
35 
36   uint32_t GetUserRegisterCount() const override;
37 
38   Status ReadRegister(const RegisterInfo *reg_info,
39                       RegisterValue &reg_value) override;
40 
41   Status WriteRegister(const RegisterInfo *reg_info,
42                        const RegisterValue &reg_value) override;
43 
44   Status ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override;
45 
46   Status WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
47 
48   llvm::Optional<SyscallData> GetSyscallData() override;
49 
50   llvm::Optional<MmapData> GetMmapData() override;
51 
52 protected:
GetGPRBuffer()53   void *GetGPRBuffer() override { return &m_gpr_x86_64; }
54 
55   void *GetFPRBuffer() override;
56 
57   size_t GetFPRSize() override;
58 
59   Status ReadFPR() override;
60 
61   Status WriteFPR() override;
62 
63   uint32_t GetPtraceOffset(uint32_t reg_index) override;
64 
65 private:
66   // Private member types.
67   enum class XStateType { Invalid, FXSAVE, XSAVE };
68   enum class RegSet { gpr, fpu, avx, mpx };
69 
70   // Info about register ranges.
71   struct RegInfo {
72     uint32_t num_registers;
73     uint32_t num_gpr_registers;
74     uint32_t num_fpr_registers;
75     uint32_t num_avx_registers;
76     uint32_t num_mpx_registers;
77     uint32_t last_gpr;
78     uint32_t first_fpr;
79     uint32_t last_fpr;
80     uint32_t first_st;
81     uint32_t last_st;
82     uint32_t first_mm;
83     uint32_t last_mm;
84     uint32_t first_xmm;
85     uint32_t last_xmm;
86     uint32_t first_ymm;
87     uint32_t last_ymm;
88     uint32_t first_mpxr;
89     uint32_t last_mpxr;
90     uint32_t first_mpxc;
91     uint32_t last_mpxc;
92     uint32_t first_dr;
93     uint32_t last_dr;
94     uint32_t gpr_flags;
95   };
96 
97   // Private member variables.
98   mutable XStateType m_xstate_type;
99   std::unique_ptr<FPR, llvm::FreeDeleter>
100       m_xstate; // Extended States Area, named FPR for historical reasons.
101   struct iovec m_iovec;
102   YMM m_ymm_set;
103   MPX m_mpx_set;
104   RegInfo m_reg_info;
105   uint64_t m_gpr_x86_64[k_num_gpr_registers_x86_64];
106   uint32_t m_fctrl_offset_in_userarea;
107 
108   // Private member methods.
109   bool IsCPUFeatureAvailable(RegSet feature_code) const;
110 
111   bool IsRegisterSetAvailable(uint32_t set_index) const;
112 
113   bool IsGPR(uint32_t reg_index) const;
114 
115   bool IsFPR(uint32_t reg_index) const;
116 
117   bool IsDR(uint32_t reg_index) const;
118 
119   bool CopyXSTATEtoYMM(uint32_t reg_index, lldb::ByteOrder byte_order);
120 
121   bool CopyYMMtoXSTATE(uint32_t reg, lldb::ByteOrder byte_order);
122 
123   bool IsAVX(uint32_t reg_index) const;
124 
125   bool CopyXSTATEtoMPX(uint32_t reg);
126 
127   bool CopyMPXtoXSTATE(uint32_t reg);
128 
129   bool IsMPX(uint32_t reg_index) const;
130 
131   void UpdateXSTATEforWrite(uint32_t reg_index);
132 };
133 
134 } // namespace process_linux
135 } // namespace lldb_private
136 
137 #endif // #ifndef lldb_NativeRegisterContextLinux_x86_64_h
138 
139 #endif // defined(__i386__) || defined(__x86_64__)
140