1 //===-- NativeRegisterContextLinux_x86_64.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 #if defined(__i386__) || defined(__x86_64__)
10 
11 #include "NativeRegisterContextLinux_x86_64.h"
12 
13 #include "lldb/Host/HostInfo.h"
14 #include "lldb/Utility/DataBufferHeap.h"
15 #include "lldb/Utility/Log.h"
16 #include "lldb/Utility/RegisterValue.h"
17 #include "lldb/Utility/Status.h"
18 
19 #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
20 #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
21 #include <cpuid.h>
22 #include <linux/elf.h>
23 
24 using namespace lldb_private;
25 using namespace lldb_private::process_linux;
26 
27 // ----------------------------------------------------------------------------
28 // Private namespace.
29 // ----------------------------------------------------------------------------
30 
31 namespace {
32 // x86 32-bit general purpose registers.
33 const uint32_t g_gpr_regnums_i386[] = {
34     lldb_eax_i386,      lldb_ebx_i386,    lldb_ecx_i386, lldb_edx_i386,
35     lldb_edi_i386,      lldb_esi_i386,    lldb_ebp_i386, lldb_esp_i386,
36     lldb_eip_i386,      lldb_eflags_i386, lldb_cs_i386,  lldb_fs_i386,
37     lldb_gs_i386,       lldb_ss_i386,     lldb_ds_i386,  lldb_es_i386,
38     lldb_ax_i386,       lldb_bx_i386,     lldb_cx_i386,  lldb_dx_i386,
39     lldb_di_i386,       lldb_si_i386,     lldb_bp_i386,  lldb_sp_i386,
40     lldb_ah_i386,       lldb_bh_i386,     lldb_ch_i386,  lldb_dh_i386,
41     lldb_al_i386,       lldb_bl_i386,     lldb_cl_i386,  lldb_dl_i386,
42     LLDB_INVALID_REGNUM // register sets need to end with this flag
43 };
44 static_assert((sizeof(g_gpr_regnums_i386) / sizeof(g_gpr_regnums_i386[0])) -
45                       1 ==
46                   k_num_gpr_registers_i386,
47               "g_gpr_regnums_i386 has wrong number of register infos");
48 
49 // x86 32-bit floating point registers.
50 const uint32_t g_fpu_regnums_i386[] = {
51     lldb_fctrl_i386,    lldb_fstat_i386,     lldb_ftag_i386,  lldb_fop_i386,
52     lldb_fiseg_i386,    lldb_fioff_i386,     lldb_foseg_i386, lldb_fooff_i386,
53     lldb_mxcsr_i386,    lldb_mxcsrmask_i386, lldb_st0_i386,   lldb_st1_i386,
54     lldb_st2_i386,      lldb_st3_i386,       lldb_st4_i386,   lldb_st5_i386,
55     lldb_st6_i386,      lldb_st7_i386,       lldb_mm0_i386,   lldb_mm1_i386,
56     lldb_mm2_i386,      lldb_mm3_i386,       lldb_mm4_i386,   lldb_mm5_i386,
57     lldb_mm6_i386,      lldb_mm7_i386,       lldb_xmm0_i386,  lldb_xmm1_i386,
58     lldb_xmm2_i386,     lldb_xmm3_i386,      lldb_xmm4_i386,  lldb_xmm5_i386,
59     lldb_xmm6_i386,     lldb_xmm7_i386,
60     LLDB_INVALID_REGNUM // register sets need to end with this flag
61 };
62 static_assert((sizeof(g_fpu_regnums_i386) / sizeof(g_fpu_regnums_i386[0])) -
63                       1 ==
64                   k_num_fpr_registers_i386,
65               "g_fpu_regnums_i386 has wrong number of register infos");
66 
67 // x86 32-bit AVX registers.
68 const uint32_t g_avx_regnums_i386[] = {
69     lldb_ymm0_i386,     lldb_ymm1_i386, lldb_ymm2_i386, lldb_ymm3_i386,
70     lldb_ymm4_i386,     lldb_ymm5_i386, lldb_ymm6_i386, lldb_ymm7_i386,
71     LLDB_INVALID_REGNUM // register sets need to end with this flag
72 };
73 static_assert((sizeof(g_avx_regnums_i386) / sizeof(g_avx_regnums_i386[0])) -
74                       1 ==
75                   k_num_avx_registers_i386,
76               " g_avx_regnums_i386 has wrong number of register infos");
77 
78 // x64 32-bit MPX registers.
79 static const uint32_t g_mpx_regnums_i386[] = {
80     lldb_bnd0_i386,     lldb_bnd1_i386, lldb_bnd2_i386, lldb_bnd3_i386,
81     lldb_bndcfgu_i386,  lldb_bndstatus_i386,
82     LLDB_INVALID_REGNUM // register sets need to end with this flag
83 };
84 static_assert((sizeof(g_mpx_regnums_i386) / sizeof(g_mpx_regnums_i386[0])) -
85                       1 ==
86                   k_num_mpx_registers_i386,
87               "g_mpx_regnums_x86_64 has wrong number of register infos");
88 
89 // x86 64-bit general purpose registers.
90 static const uint32_t g_gpr_regnums_x86_64[] = {
91     lldb_rax_x86_64,    lldb_rbx_x86_64,    lldb_rcx_x86_64, lldb_rdx_x86_64,
92     lldb_rdi_x86_64,    lldb_rsi_x86_64,    lldb_rbp_x86_64, lldb_rsp_x86_64,
93     lldb_r8_x86_64,     lldb_r9_x86_64,     lldb_r10_x86_64, lldb_r11_x86_64,
94     lldb_r12_x86_64,    lldb_r13_x86_64,    lldb_r14_x86_64, lldb_r15_x86_64,
95     lldb_rip_x86_64,    lldb_rflags_x86_64, lldb_cs_x86_64,  lldb_fs_x86_64,
96     lldb_gs_x86_64,     lldb_ss_x86_64,     lldb_ds_x86_64,  lldb_es_x86_64,
97     lldb_eax_x86_64,    lldb_ebx_x86_64,    lldb_ecx_x86_64, lldb_edx_x86_64,
98     lldb_edi_x86_64,    lldb_esi_x86_64,    lldb_ebp_x86_64, lldb_esp_x86_64,
99     lldb_r8d_x86_64,  // Low 32 bits or r8
100     lldb_r9d_x86_64,  // Low 32 bits or r9
101     lldb_r10d_x86_64, // Low 32 bits or r10
102     lldb_r11d_x86_64, // Low 32 bits or r11
103     lldb_r12d_x86_64, // Low 32 bits or r12
104     lldb_r13d_x86_64, // Low 32 bits or r13
105     lldb_r14d_x86_64, // Low 32 bits or r14
106     lldb_r15d_x86_64, // Low 32 bits or r15
107     lldb_ax_x86_64,     lldb_bx_x86_64,     lldb_cx_x86_64,  lldb_dx_x86_64,
108     lldb_di_x86_64,     lldb_si_x86_64,     lldb_bp_x86_64,  lldb_sp_x86_64,
109     lldb_r8w_x86_64,  // Low 16 bits or r8
110     lldb_r9w_x86_64,  // Low 16 bits or r9
111     lldb_r10w_x86_64, // Low 16 bits or r10
112     lldb_r11w_x86_64, // Low 16 bits or r11
113     lldb_r12w_x86_64, // Low 16 bits or r12
114     lldb_r13w_x86_64, // Low 16 bits or r13
115     lldb_r14w_x86_64, // Low 16 bits or r14
116     lldb_r15w_x86_64, // Low 16 bits or r15
117     lldb_ah_x86_64,     lldb_bh_x86_64,     lldb_ch_x86_64,  lldb_dh_x86_64,
118     lldb_al_x86_64,     lldb_bl_x86_64,     lldb_cl_x86_64,  lldb_dl_x86_64,
119     lldb_dil_x86_64,    lldb_sil_x86_64,    lldb_bpl_x86_64, lldb_spl_x86_64,
120     lldb_r8l_x86_64,    // Low 8 bits or r8
121     lldb_r9l_x86_64,    // Low 8 bits or r9
122     lldb_r10l_x86_64,   // Low 8 bits or r10
123     lldb_r11l_x86_64,   // Low 8 bits or r11
124     lldb_r12l_x86_64,   // Low 8 bits or r12
125     lldb_r13l_x86_64,   // Low 8 bits or r13
126     lldb_r14l_x86_64,   // Low 8 bits or r14
127     lldb_r15l_x86_64,   // Low 8 bits or r15
128     LLDB_INVALID_REGNUM // register sets need to end with this flag
129 };
130 static_assert((sizeof(g_gpr_regnums_x86_64) / sizeof(g_gpr_regnums_x86_64[0])) -
131                       1 ==
132                   k_num_gpr_registers_x86_64,
133               "g_gpr_regnums_x86_64 has wrong number of register infos");
134 
135 // x86 64-bit floating point registers.
136 static const uint32_t g_fpu_regnums_x86_64[] = {
137     lldb_fctrl_x86_64,     lldb_fstat_x86_64, lldb_ftag_x86_64,
138     lldb_fop_x86_64,       lldb_fiseg_x86_64, lldb_fioff_x86_64,
139     lldb_foseg_x86_64,     lldb_fooff_x86_64, lldb_mxcsr_x86_64,
140     lldb_mxcsrmask_x86_64, lldb_st0_x86_64,   lldb_st1_x86_64,
141     lldb_st2_x86_64,       lldb_st3_x86_64,   lldb_st4_x86_64,
142     lldb_st5_x86_64,       lldb_st6_x86_64,   lldb_st7_x86_64,
143     lldb_mm0_x86_64,       lldb_mm1_x86_64,   lldb_mm2_x86_64,
144     lldb_mm3_x86_64,       lldb_mm4_x86_64,   lldb_mm5_x86_64,
145     lldb_mm6_x86_64,       lldb_mm7_x86_64,   lldb_xmm0_x86_64,
146     lldb_xmm1_x86_64,      lldb_xmm2_x86_64,  lldb_xmm3_x86_64,
147     lldb_xmm4_x86_64,      lldb_xmm5_x86_64,  lldb_xmm6_x86_64,
148     lldb_xmm7_x86_64,      lldb_xmm8_x86_64,  lldb_xmm9_x86_64,
149     lldb_xmm10_x86_64,     lldb_xmm11_x86_64, lldb_xmm12_x86_64,
150     lldb_xmm13_x86_64,     lldb_xmm14_x86_64, lldb_xmm15_x86_64,
151     LLDB_INVALID_REGNUM // register sets need to end with this flag
152 };
153 static_assert((sizeof(g_fpu_regnums_x86_64) / sizeof(g_fpu_regnums_x86_64[0])) -
154                       1 ==
155                   k_num_fpr_registers_x86_64,
156               "g_fpu_regnums_x86_64 has wrong number of register infos");
157 
158 // x86 64-bit AVX registers.
159 static const uint32_t g_avx_regnums_x86_64[] = {
160     lldb_ymm0_x86_64,   lldb_ymm1_x86_64,  lldb_ymm2_x86_64,  lldb_ymm3_x86_64,
161     lldb_ymm4_x86_64,   lldb_ymm5_x86_64,  lldb_ymm6_x86_64,  lldb_ymm7_x86_64,
162     lldb_ymm8_x86_64,   lldb_ymm9_x86_64,  lldb_ymm10_x86_64, lldb_ymm11_x86_64,
163     lldb_ymm12_x86_64,  lldb_ymm13_x86_64, lldb_ymm14_x86_64, lldb_ymm15_x86_64,
164     LLDB_INVALID_REGNUM // register sets need to end with this flag
165 };
166 static_assert((sizeof(g_avx_regnums_x86_64) / sizeof(g_avx_regnums_x86_64[0])) -
167                       1 ==
168                   k_num_avx_registers_x86_64,
169               "g_avx_regnums_x86_64 has wrong number of register infos");
170 
171 // x86 64-bit MPX registers.
172 static const uint32_t g_mpx_regnums_x86_64[] = {
173     lldb_bnd0_x86_64,    lldb_bnd1_x86_64,    lldb_bnd2_x86_64,
174     lldb_bnd3_x86_64,    lldb_bndcfgu_x86_64, lldb_bndstatus_x86_64,
175     LLDB_INVALID_REGNUM // register sets need to end with this flag
176 };
177 static_assert((sizeof(g_mpx_regnums_x86_64) / sizeof(g_mpx_regnums_x86_64[0])) -
178                       1 ==
179                   k_num_mpx_registers_x86_64,
180               "g_mpx_regnums_x86_64 has wrong number of register infos");
181 
182 // Number of register sets provided by this context.
183 enum { k_num_extended_register_sets = 2, k_num_register_sets = 4 };
184 
185 // Register sets for x86 32-bit.
186 static const RegisterSet g_reg_sets_i386[k_num_register_sets] = {
187     {"General Purpose Registers", "gpr", k_num_gpr_registers_i386,
188      g_gpr_regnums_i386},
189     {"Floating Point Registers", "fpu", k_num_fpr_registers_i386,
190      g_fpu_regnums_i386},
191     {"Advanced Vector Extensions", "avx", k_num_avx_registers_i386,
192      g_avx_regnums_i386},
193     { "Memory Protection Extensions", "mpx", k_num_mpx_registers_i386,
194      g_mpx_regnums_i386}};
195 
196 // Register sets for x86 64-bit.
197 static const RegisterSet g_reg_sets_x86_64[k_num_register_sets] = {
198     {"General Purpose Registers", "gpr", k_num_gpr_registers_x86_64,
199      g_gpr_regnums_x86_64},
200     {"Floating Point Registers", "fpu", k_num_fpr_registers_x86_64,
201      g_fpu_regnums_x86_64},
202     {"Advanced Vector Extensions", "avx", k_num_avx_registers_x86_64,
203      g_avx_regnums_x86_64},
204     { "Memory Protection Extensions", "mpx", k_num_mpx_registers_x86_64,
205      g_mpx_regnums_x86_64}};
206 }
207 
208 #define REG_CONTEXT_SIZE (GetRegisterInfoInterface().GetGPRSize() + sizeof(FPR))
209 
210 // ----------------------------------------------------------------------------
211 // Required ptrace defines.
212 // ----------------------------------------------------------------------------
213 
214 // Support ptrace extensions even when compiled without required kernel support
215 #ifndef NT_X86_XSTATE
216 #define NT_X86_XSTATE 0x202
217 #endif
218 #ifndef NT_PRXFPREG
219 #define NT_PRXFPREG 0x46e62b7f
220 #endif
221 
222 // On x86_64 NT_PRFPREG is used to access the FXSAVE area. On i386, we need to
223 // use NT_PRXFPREG.
224 static inline unsigned int fxsr_regset(const ArchSpec &arch) {
225   return arch.GetAddressByteSize() == 8 ? NT_PRFPREG : NT_PRXFPREG;
226 }
227 
228 // ----------------------------------------------------------------------------
229 // Required MPX define.
230 // ----------------------------------------------------------------------------
231 
232 // Support MPX extensions also if compiled with compiler without MPX support.
233 #ifndef bit_MPX
234 #define bit_MPX 0x4000
235 #endif
236 
237 // ----------------------------------------------------------------------------
238 // XCR0 extended register sets masks.
239 // ----------------------------------------------------------------------------
240 #define mask_XSTATE_AVX (1ULL << 2)
241 #define mask_XSTATE_BNDREGS (1ULL << 3)
242 #define mask_XSTATE_BNDCFG (1ULL << 4)
243 #define mask_XSTATE_MPX (mask_XSTATE_BNDREGS | mask_XSTATE_BNDCFG)
244 
245 std::unique_ptr<NativeRegisterContextLinux>
246 NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(
247     const ArchSpec &target_arch, NativeThreadProtocol &native_thread) {
248   return std::unique_ptr<NativeRegisterContextLinux>(
249       new NativeRegisterContextLinux_x86_64(target_arch, native_thread));
250 }
251 
252 // ----------------------------------------------------------------------------
253 // NativeRegisterContextLinux_x86_64 members.
254 // ----------------------------------------------------------------------------
255 
256 static RegisterInfoInterface *
257 CreateRegisterInfoInterface(const ArchSpec &target_arch) {
258   if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) {
259     // 32-bit hosts run with a RegisterContextLinux_i386 context.
260     return new RegisterContextLinux_i386(target_arch);
261   } else {
262     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
263            "Register setting path assumes this is a 64-bit host");
264     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the
265     // x86_64 register context.
266     return new RegisterContextLinux_x86_64(target_arch);
267   }
268 }
269 
270 // Return the size of the XSTATE area supported on this cpu. It is necessary to
271 // allocate the full size of the area even if we do not use/recognise all of it
272 // because ptrace(PTRACE_SETREGSET, NT_X86_XSTATE) will refuse to write to it if
273 // we do not pass it a buffer of sufficient size. The size is always at least
274 // sizeof(FPR) so that the allocated buffer can be safely cast to FPR*.
275 static std::size_t GetXSTATESize() {
276   unsigned int eax, ebx, ecx, edx;
277   // First check whether the XSTATE are is supported at all.
278   if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx) || !(ecx & bit_XSAVE))
279     return sizeof(FPR);
280 
281   // Then fetch the maximum size of the area.
282   if (!__get_cpuid_count(0x0d, 0, &eax, &ebx, &ecx, &edx))
283     return sizeof(FPR);
284   return std::max<std::size_t>(ecx, sizeof(FPR));
285 }
286 
287 NativeRegisterContextLinux_x86_64::NativeRegisterContextLinux_x86_64(
288     const ArchSpec &target_arch, NativeThreadProtocol &native_thread)
289     : NativeRegisterContextLinux(native_thread,
290                                  CreateRegisterInfoInterface(target_arch)),
291       m_xstate_type(XStateType::Invalid), m_ymm_set(), m_mpx_set(),
292       m_reg_info(), m_gpr_x86_64() {
293   // Set up data about ranges of valid registers.
294   switch (target_arch.GetMachine()) {
295   case llvm::Triple::x86:
296     m_reg_info.num_registers = k_num_registers_i386;
297     m_reg_info.num_gpr_registers = k_num_gpr_registers_i386;
298     m_reg_info.num_fpr_registers = k_num_fpr_registers_i386;
299     m_reg_info.num_avx_registers = k_num_avx_registers_i386;
300     m_reg_info.num_mpx_registers = k_num_mpx_registers_i386;
301     m_reg_info.last_gpr = k_last_gpr_i386;
302     m_reg_info.first_fpr = k_first_fpr_i386;
303     m_reg_info.last_fpr = k_last_fpr_i386;
304     m_reg_info.first_st = lldb_st0_i386;
305     m_reg_info.last_st = lldb_st7_i386;
306     m_reg_info.first_mm = lldb_mm0_i386;
307     m_reg_info.last_mm = lldb_mm7_i386;
308     m_reg_info.first_xmm = lldb_xmm0_i386;
309     m_reg_info.last_xmm = lldb_xmm7_i386;
310     m_reg_info.first_ymm = lldb_ymm0_i386;
311     m_reg_info.last_ymm = lldb_ymm7_i386;
312     m_reg_info.first_mpxr = lldb_bnd0_i386;
313     m_reg_info.last_mpxr = lldb_bnd3_i386;
314     m_reg_info.first_mpxc = lldb_bndcfgu_i386;
315     m_reg_info.last_mpxc = lldb_bndstatus_i386;
316     m_reg_info.first_dr = lldb_dr0_i386;
317     m_reg_info.gpr_flags = lldb_eflags_i386;
318     break;
319   case llvm::Triple::x86_64:
320     m_reg_info.num_registers = k_num_registers_x86_64;
321     m_reg_info.num_gpr_registers = k_num_gpr_registers_x86_64;
322     m_reg_info.num_fpr_registers = k_num_fpr_registers_x86_64;
323     m_reg_info.num_avx_registers = k_num_avx_registers_x86_64;
324     m_reg_info.num_mpx_registers = k_num_mpx_registers_x86_64;
325     m_reg_info.last_gpr = k_last_gpr_x86_64;
326     m_reg_info.first_fpr = k_first_fpr_x86_64;
327     m_reg_info.last_fpr = k_last_fpr_x86_64;
328     m_reg_info.first_st = lldb_st0_x86_64;
329     m_reg_info.last_st = lldb_st7_x86_64;
330     m_reg_info.first_mm = lldb_mm0_x86_64;
331     m_reg_info.last_mm = lldb_mm7_x86_64;
332     m_reg_info.first_xmm = lldb_xmm0_x86_64;
333     m_reg_info.last_xmm = lldb_xmm15_x86_64;
334     m_reg_info.first_ymm = lldb_ymm0_x86_64;
335     m_reg_info.last_ymm = lldb_ymm15_x86_64;
336     m_reg_info.first_mpxr = lldb_bnd0_x86_64;
337     m_reg_info.last_mpxr = lldb_bnd3_x86_64;
338     m_reg_info.first_mpxc = lldb_bndcfgu_x86_64;
339     m_reg_info.last_mpxc = lldb_bndstatus_x86_64;
340     m_reg_info.first_dr = lldb_dr0_x86_64;
341     m_reg_info.gpr_flags = lldb_rflags_x86_64;
342     break;
343   default:
344     assert(false && "Unhandled target architecture.");
345     break;
346   }
347 
348   std::size_t xstate_size = GetXSTATESize();
349   m_xstate.reset(static_cast<FPR *>(std::malloc(xstate_size)));
350   m_iovec.iov_base = m_xstate.get();
351   m_iovec.iov_len = xstate_size;
352 
353   // Clear out the FPR state.
354   ::memset(m_xstate.get(), 0, xstate_size);
355 
356   // Store byte offset of fctrl (i.e. first register of FPR)
357   const RegisterInfo *reg_info_fctrl = GetRegisterInfoByName("fctrl");
358   m_fctrl_offset_in_userarea = reg_info_fctrl->byte_offset;
359 }
360 
361 // CONSIDER after local and llgs debugging are merged, register set support can
362 // be moved into a base x86-64 class with IsRegisterSetAvailable made virtual.
363 uint32_t NativeRegisterContextLinux_x86_64::GetRegisterSetCount() const {
364   uint32_t sets = 0;
365   for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) {
366     if (IsRegisterSetAvailable(set_index))
367       ++sets;
368   }
369 
370   return sets;
371 }
372 
373 uint32_t NativeRegisterContextLinux_x86_64::GetUserRegisterCount() const {
374   uint32_t count = 0;
375   for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) {
376     const RegisterSet *set = GetRegisterSet(set_index);
377     if (set)
378       count += set->num_registers;
379   }
380   return count;
381 }
382 
383 const RegisterSet *
384 NativeRegisterContextLinux_x86_64::GetRegisterSet(uint32_t set_index) const {
385   if (!IsRegisterSetAvailable(set_index))
386     return nullptr;
387 
388   switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
389   case llvm::Triple::x86:
390     return &g_reg_sets_i386[set_index];
391   case llvm::Triple::x86_64:
392     return &g_reg_sets_x86_64[set_index];
393   default:
394     assert(false && "Unhandled target architecture.");
395     return nullptr;
396   }
397 
398   return nullptr;
399 }
400 
401 Status
402 NativeRegisterContextLinux_x86_64::ReadRegister(const RegisterInfo *reg_info,
403                                                 RegisterValue &reg_value) {
404   Status error;
405 
406   if (!reg_info) {
407     error.SetErrorString("reg_info NULL");
408     return error;
409   }
410 
411   const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
412   if (reg == LLDB_INVALID_REGNUM) {
413     // This is likely an internal register for lldb use only and should not be
414     // directly queried.
415     error.SetErrorStringWithFormat("register \"%s\" is an internal-only lldb "
416                                    "register, cannot read directly",
417                                    reg_info->name);
418     return error;
419   }
420 
421   if (IsFPR(reg) || IsAVX(reg) || IsMPX(reg)) {
422     error = ReadFPR();
423     if (error.Fail())
424       return error;
425   } else {
426     uint32_t full_reg = reg;
427     bool is_subreg = reg_info->invalidate_regs &&
428                      (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM);
429 
430     if (is_subreg) {
431       // Read the full aligned 64-bit register.
432       full_reg = reg_info->invalidate_regs[0];
433     }
434 
435     error = ReadRegisterRaw(full_reg, reg_value);
436 
437     if (error.Success()) {
438       // If our read was not aligned (for ah,bh,ch,dh), shift our returned
439       // value one byte to the right.
440       if (is_subreg && (reg_info->byte_offset & 0x1))
441         reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8);
442 
443       // If our return byte size was greater than the return value reg size,
444       // then use the type specified by reg_info rather than the uint64_t
445       // default
446       if (reg_value.GetByteSize() > reg_info->byte_size)
447         reg_value.SetType(reg_info);
448     }
449     return error;
450   }
451 
452   if (reg_info->encoding == lldb::eEncodingVector) {
453     lldb::ByteOrder byte_order = GetByteOrder();
454 
455     if (byte_order != lldb::eByteOrderInvalid) {
456       if (reg >= m_reg_info.first_st && reg <= m_reg_info.last_st)
457         reg_value.SetBytes(
458             m_xstate->fxsave.stmm[reg - m_reg_info.first_st].bytes,
459             reg_info->byte_size, byte_order);
460       if (reg >= m_reg_info.first_mm && reg <= m_reg_info.last_mm)
461         reg_value.SetBytes(
462             m_xstate->fxsave.stmm[reg - m_reg_info.first_mm].bytes,
463             reg_info->byte_size, byte_order);
464       if (reg >= m_reg_info.first_xmm && reg <= m_reg_info.last_xmm)
465         reg_value.SetBytes(
466             m_xstate->fxsave.xmm[reg - m_reg_info.first_xmm].bytes,
467             reg_info->byte_size, byte_order);
468       if (reg >= m_reg_info.first_ymm && reg <= m_reg_info.last_ymm) {
469         // Concatenate ymm using the register halves in xmm.bytes and
470         // ymmh.bytes
471         if (CopyXSTATEtoYMM(reg, byte_order))
472           reg_value.SetBytes(m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes,
473                              reg_info->byte_size, byte_order);
474         else {
475           error.SetErrorString("failed to copy ymm register value");
476           return error;
477         }
478       }
479       if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
480         if (CopyXSTATEtoMPX(reg))
481           reg_value.SetBytes(m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes,
482                              reg_info->byte_size, byte_order);
483         else {
484           error.SetErrorString("failed to copy mpx register value");
485           return error;
486         }
487       }
488       if (reg >= m_reg_info.first_mpxc && reg <= m_reg_info.last_mpxc) {
489         if (CopyXSTATEtoMPX(reg))
490           reg_value.SetBytes(m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes,
491                              reg_info->byte_size, byte_order);
492         else {
493           error.SetErrorString("failed to copy mpx register value");
494           return error;
495         }
496       }
497 
498       if (reg_value.GetType() != RegisterValue::eTypeBytes)
499         error.SetErrorString(
500             "write failed - type was expected to be RegisterValue::eTypeBytes");
501 
502       return error;
503     }
504 
505     error.SetErrorString("byte order is invalid");
506     return error;
507   }
508 
509   // Get pointer to m_xstate->fxsave variable and set the data from it.
510 
511   // Byte offsets of all registers are calculated wrt 'UserArea' structure.
512   // However, ReadFPR() reads fpu registers {using ptrace(PTRACE_GETFPREGS,..)}
513   // and stores them in 'm_fpr' (of type FPR structure). To extract values of
514   // fpu registers, m_fpr should be read at byte offsets calculated wrt to FPR
515   // structure.
516 
517   // Since, FPR structure is also one of the member of UserArea structure.
518   // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) -
519   // byte_offset(fctrl wrt UserArea)
520   assert((reg_info->byte_offset - m_fctrl_offset_in_userarea) < sizeof(FPR));
521   uint8_t *src = (uint8_t *)m_xstate.get() + reg_info->byte_offset -
522                  m_fctrl_offset_in_userarea;
523   switch (reg_info->byte_size) {
524   case 1:
525     reg_value.SetUInt8(*(uint8_t *)src);
526     break;
527   case 2:
528     reg_value.SetUInt16(*(uint16_t *)src);
529     break;
530   case 4:
531     reg_value.SetUInt32(*(uint32_t *)src);
532     break;
533   case 8:
534     reg_value.SetUInt64(*(uint64_t *)src);
535     break;
536   default:
537     assert(false && "Unhandled data size.");
538     error.SetErrorStringWithFormat("unhandled byte size: %" PRIu32,
539                                    reg_info->byte_size);
540     break;
541   }
542 
543   return error;
544 }
545 
546 void NativeRegisterContextLinux_x86_64::UpdateXSTATEforWrite(
547     uint32_t reg_index) {
548   XSAVE_HDR::XFeature &xstate_bv = m_xstate->xsave.header.xstate_bv;
549   if (IsFPR(reg_index)) {
550     // IsFPR considers both %st and %xmm registers as floating point, but these
551     // map to two features. Set both flags, just in case.
552     xstate_bv |= XSAVE_HDR::XFeature::FP | XSAVE_HDR::XFeature::SSE;
553   } else if (IsAVX(reg_index)) {
554     // Lower bytes of some %ymm registers are shared with %xmm registers.
555     xstate_bv |= XSAVE_HDR::XFeature::YMM | XSAVE_HDR::XFeature::SSE;
556   } else if (IsMPX(reg_index)) {
557     // MPX registers map to two XSAVE features.
558     xstate_bv |= XSAVE_HDR::XFeature::BNDREGS | XSAVE_HDR::XFeature::BNDCSR;
559   }
560 }
561 
562 Status NativeRegisterContextLinux_x86_64::WriteRegister(
563     const RegisterInfo *reg_info, const RegisterValue &reg_value) {
564   assert(reg_info && "reg_info is null");
565 
566   const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB];
567   if (reg_index == LLDB_INVALID_REGNUM)
568     return Status("no lldb regnum for %s", reg_info && reg_info->name
569                                                ? reg_info->name
570                                                : "<unknown register>");
571 
572   UpdateXSTATEforWrite(reg_index);
573 
574   if (IsGPR(reg_index))
575     return WriteRegisterRaw(reg_index, reg_value);
576 
577   if (IsFPR(reg_index) || IsAVX(reg_index) || IsMPX(reg_index)) {
578     if (reg_info->encoding == lldb::eEncodingVector) {
579       if (reg_index >= m_reg_info.first_st && reg_index <= m_reg_info.last_st)
580         ::memcpy(m_xstate->fxsave.stmm[reg_index - m_reg_info.first_st].bytes,
581                  reg_value.GetBytes(), reg_value.GetByteSize());
582 
583       if (reg_index >= m_reg_info.first_mm && reg_index <= m_reg_info.last_mm)
584         ::memcpy(m_xstate->fxsave.stmm[reg_index - m_reg_info.first_mm].bytes,
585                  reg_value.GetBytes(), reg_value.GetByteSize());
586 
587       if (reg_index >= m_reg_info.first_xmm && reg_index <= m_reg_info.last_xmm)
588         ::memcpy(m_xstate->fxsave.xmm[reg_index - m_reg_info.first_xmm].bytes,
589                  reg_value.GetBytes(), reg_value.GetByteSize());
590 
591       if (reg_index >= m_reg_info.first_ymm &&
592           reg_index <= m_reg_info.last_ymm) {
593         // Store ymm register content, and split into the register halves in
594         // xmm.bytes and ymmh.bytes
595         ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
596                  reg_value.GetBytes(), reg_value.GetByteSize());
597         if (!CopyYMMtoXSTATE(reg_index, GetByteOrder()))
598           return Status("CopyYMMtoXSTATE() failed");
599       }
600 
601       if (reg_index >= m_reg_info.first_mpxr &&
602           reg_index <= m_reg_info.last_mpxr) {
603         ::memcpy(m_mpx_set.mpxr[reg_index - m_reg_info.first_mpxr].bytes,
604                  reg_value.GetBytes(), reg_value.GetByteSize());
605         if (!CopyMPXtoXSTATE(reg_index))
606           return Status("CopyMPXtoXSTATE() failed");
607       }
608 
609       if (reg_index >= m_reg_info.first_mpxc &&
610           reg_index <= m_reg_info.last_mpxc) {
611         ::memcpy(m_mpx_set.mpxc[reg_index - m_reg_info.first_mpxc].bytes,
612                  reg_value.GetBytes(), reg_value.GetByteSize());
613         if (!CopyMPXtoXSTATE(reg_index))
614           return Status("CopyMPXtoXSTATE() failed");
615       }
616     } else {
617       // Get pointer to m_xstate->fxsave variable and set the data to it.
618 
619       // Byte offsets of all registers are calculated wrt 'UserArea' structure.
620       // However, WriteFPR() takes m_fpr (of type FPR structure) and writes
621       // only fpu registers using ptrace(PTRACE_SETFPREGS,..) API. Hence fpu
622       // registers should be written in m_fpr at byte offsets calculated wrt
623       // FPR structure.
624 
625       // Since, FPR structure is also one of the member of UserArea structure.
626       // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) -
627       // byte_offset(fctrl wrt UserArea)
628       assert((reg_info->byte_offset - m_fctrl_offset_in_userarea) <
629              sizeof(FPR));
630       uint8_t *dst = (uint8_t *)m_xstate.get() + reg_info->byte_offset -
631                      m_fctrl_offset_in_userarea;
632       switch (reg_info->byte_size) {
633       case 1:
634         *(uint8_t *)dst = reg_value.GetAsUInt8();
635         break;
636       case 2:
637         *(uint16_t *)dst = reg_value.GetAsUInt16();
638         break;
639       case 4:
640         *(uint32_t *)dst = reg_value.GetAsUInt32();
641         break;
642       case 8:
643         *(uint64_t *)dst = reg_value.GetAsUInt64();
644         break;
645       default:
646         assert(false && "Unhandled data size.");
647         return Status("unhandled register data size %" PRIu32,
648                       reg_info->byte_size);
649       }
650     }
651 
652     Status error = WriteFPR();
653     if (error.Fail())
654       return error;
655 
656     if (IsAVX(reg_index)) {
657       if (!CopyYMMtoXSTATE(reg_index, GetByteOrder()))
658         return Status("CopyYMMtoXSTATE() failed");
659     }
660 
661     if (IsMPX(reg_index)) {
662       if (!CopyMPXtoXSTATE(reg_index))
663         return Status("CopyMPXtoXSTATE() failed");
664     }
665     return Status();
666   }
667   return Status("failed - register wasn't recognized to be a GPR or an FPR, "
668                 "write strategy unknown");
669 }
670 
671 Status NativeRegisterContextLinux_x86_64::ReadAllRegisterValues(
672     lldb::DataBufferSP &data_sp) {
673   Status error;
674 
675   data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0));
676   error = ReadGPR();
677   if (error.Fail())
678     return error;
679 
680   error = ReadFPR();
681   if (error.Fail())
682     return error;
683 
684   uint8_t *dst = data_sp->GetBytes();
685   ::memcpy(dst, &m_gpr_x86_64, GetRegisterInfoInterface().GetGPRSize());
686   dst += GetRegisterInfoInterface().GetGPRSize();
687   if (m_xstate_type == XStateType::FXSAVE)
688     ::memcpy(dst, &m_xstate->fxsave, sizeof(m_xstate->fxsave));
689   else if (m_xstate_type == XStateType::XSAVE) {
690     lldb::ByteOrder byte_order = GetByteOrder();
691 
692     if (IsCPUFeatureAvailable(RegSet::avx)) {
693       // Assemble the YMM register content from the register halves.
694       for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm;
695            ++reg) {
696         if (!CopyXSTATEtoYMM(reg, byte_order)) {
697           error.SetErrorStringWithFormat(
698               "NativeRegisterContextLinux_x86_64::%s "
699               "CopyXSTATEtoYMM() failed for reg num "
700               "%" PRIu32,
701               __FUNCTION__, reg);
702           return error;
703         }
704       }
705     }
706 
707     if (IsCPUFeatureAvailable(RegSet::mpx)) {
708       for (uint32_t reg = m_reg_info.first_mpxr; reg <= m_reg_info.last_mpxc;
709            ++reg) {
710         if (!CopyXSTATEtoMPX(reg)) {
711           error.SetErrorStringWithFormat(
712               "NativeRegisterContextLinux_x86_64::%s "
713               "CopyXSTATEtoMPX() failed for reg num "
714               "%" PRIu32,
715               __FUNCTION__, reg);
716           return error;
717         }
718       }
719     }
720     // Copy the extended register state including the assembled ymm registers.
721     ::memcpy(dst, m_xstate.get(), sizeof(FPR));
722   } else {
723     assert(false && "how do we save the floating point registers?");
724     error.SetErrorString("unsure how to save the floating point registers");
725   }
726   /** The following code is specific to Linux x86 based architectures,
727    *  where the register orig_eax (32 bit)/orig_rax (64 bit) is set to
728    *  -1 to solve the bug 23659, such a setting prevents the automatic
729    *  decrement of the instruction pointer which was causing the SIGILL
730    *  exception.
731    * **/
732 
733   RegisterValue value((uint64_t)-1);
734   const RegisterInfo *reg_info =
735       GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_eax");
736   if (reg_info == nullptr)
737     reg_info = GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_rax");
738 
739   if (reg_info != nullptr)
740     return DoWriteRegisterValue(reg_info->byte_offset, reg_info->name, value);
741 
742   return error;
743 }
744 
745 Status NativeRegisterContextLinux_x86_64::WriteAllRegisterValues(
746     const lldb::DataBufferSP &data_sp) {
747   Status error;
748 
749   if (!data_sp) {
750     error.SetErrorStringWithFormat(
751         "NativeRegisterContextLinux_x86_64::%s invalid data_sp provided",
752         __FUNCTION__);
753     return error;
754   }
755 
756   if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) {
757     error.SetErrorStringWithFormatv(
758         "data_sp contained mismatched data size, expected {0}, actual {1}",
759         REG_CONTEXT_SIZE, data_sp->GetByteSize());
760     return error;
761   }
762 
763   uint8_t *src = data_sp->GetBytes();
764   if (src == nullptr) {
765     error.SetErrorStringWithFormat("NativeRegisterContextLinux_x86_64::%s "
766                                    "DataBuffer::GetBytes() returned a null "
767                                    "pointer",
768                                    __FUNCTION__);
769     return error;
770   }
771   ::memcpy(&m_gpr_x86_64, src, GetRegisterInfoInterface().GetGPRSize());
772 
773   error = WriteGPR();
774   if (error.Fail())
775     return error;
776 
777   src += GetRegisterInfoInterface().GetGPRSize();
778   if (m_xstate_type == XStateType::FXSAVE)
779     ::memcpy(&m_xstate->fxsave, src, sizeof(m_xstate->fxsave));
780   else if (m_xstate_type == XStateType::XSAVE)
781     ::memcpy(&m_xstate->xsave, src, sizeof(m_xstate->xsave));
782 
783   error = WriteFPR();
784   if (error.Fail())
785     return error;
786 
787   if (m_xstate_type == XStateType::XSAVE) {
788     lldb::ByteOrder byte_order = GetByteOrder();
789 
790     if (IsCPUFeatureAvailable(RegSet::avx)) {
791       // Parse the YMM register content from the register halves.
792       for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm;
793            ++reg) {
794         if (!CopyYMMtoXSTATE(reg, byte_order)) {
795           error.SetErrorStringWithFormat(
796               "NativeRegisterContextLinux_x86_64::%s "
797               "CopyYMMtoXSTATE() failed for reg num "
798               "%" PRIu32,
799               __FUNCTION__, reg);
800           return error;
801         }
802       }
803     }
804 
805     if (IsCPUFeatureAvailable(RegSet::mpx)) {
806       for (uint32_t reg = m_reg_info.first_mpxr; reg <= m_reg_info.last_mpxc;
807            ++reg) {
808         if (!CopyMPXtoXSTATE(reg)) {
809           error.SetErrorStringWithFormat(
810               "NativeRegisterContextLinux_x86_64::%s "
811               "CopyMPXtoXSTATE() failed for reg num "
812               "%" PRIu32,
813               __FUNCTION__, reg);
814           return error;
815         }
816       }
817     }
818   }
819 
820   return error;
821 }
822 
823 bool NativeRegisterContextLinux_x86_64::IsCPUFeatureAvailable(
824     RegSet feature_code) const {
825   if (m_xstate_type == XStateType::Invalid) {
826     if (const_cast<NativeRegisterContextLinux_x86_64 *>(this)->ReadFPR().Fail())
827       return false;
828   }
829   switch (feature_code) {
830   case RegSet::gpr:
831   case RegSet::fpu:
832     return true;
833   case RegSet::avx: // Check if CPU has AVX and if there is kernel support, by
834                     // reading in the XCR0 area of XSAVE.
835     if ((m_xstate->xsave.i387.xcr0 & mask_XSTATE_AVX) == mask_XSTATE_AVX)
836       return true;
837      break;
838   case RegSet::mpx: // Check if CPU has MPX and if there is kernel support, by
839                     // reading in the XCR0 area of XSAVE.
840     if ((m_xstate->xsave.i387.xcr0 & mask_XSTATE_MPX) == mask_XSTATE_MPX)
841       return true;
842     break;
843   }
844   return false;
845 }
846 
847 bool NativeRegisterContextLinux_x86_64::IsRegisterSetAvailable(
848     uint32_t set_index) const {
849   uint32_t num_sets = k_num_register_sets - k_num_extended_register_sets;
850 
851   switch (static_cast<RegSet>(set_index)) {
852   case RegSet::gpr:
853   case RegSet::fpu:
854     return (set_index < num_sets);
855   case RegSet::avx:
856     return IsCPUFeatureAvailable(RegSet::avx);
857   case RegSet::mpx:
858     return IsCPUFeatureAvailable(RegSet::mpx);
859   }
860   return false;
861 }
862 
863 bool NativeRegisterContextLinux_x86_64::IsGPR(uint32_t reg_index) const {
864   // GPRs come first.
865   return reg_index <= m_reg_info.last_gpr;
866 }
867 
868 bool NativeRegisterContextLinux_x86_64::IsFPR(uint32_t reg_index) const {
869   return (m_reg_info.first_fpr <= reg_index &&
870           reg_index <= m_reg_info.last_fpr);
871 }
872 
873 Status NativeRegisterContextLinux_x86_64::WriteFPR() {
874   switch (m_xstate_type) {
875   case XStateType::FXSAVE:
876     return WriteRegisterSet(
877         &m_iovec, sizeof(m_xstate->fxsave),
878         fxsr_regset(GetRegisterInfoInterface().GetTargetArchitecture()));
879   case XStateType::XSAVE:
880     return WriteRegisterSet(&m_iovec, sizeof(m_xstate->xsave), NT_X86_XSTATE);
881   default:
882     return Status("Unrecognized FPR type.");
883   }
884 }
885 
886 bool NativeRegisterContextLinux_x86_64::IsAVX(uint32_t reg_index) const {
887   if (!IsCPUFeatureAvailable(RegSet::avx))
888     return false;
889   return (m_reg_info.first_ymm <= reg_index &&
890           reg_index <= m_reg_info.last_ymm);
891 }
892 
893 bool NativeRegisterContextLinux_x86_64::CopyXSTATEtoYMM(
894     uint32_t reg_index, lldb::ByteOrder byte_order) {
895   if (!IsAVX(reg_index))
896     return false;
897 
898   if (byte_order == lldb::eByteOrderLittle) {
899     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
900              m_xstate->fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes,
901              sizeof(XMMReg));
902     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes +
903                  sizeof(XMMReg),
904              m_xstate->xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes,
905              sizeof(YMMHReg));
906     return true;
907   }
908 
909   if (byte_order == lldb::eByteOrderBig) {
910     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes +
911                  sizeof(XMMReg),
912              m_xstate->fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes,
913              sizeof(XMMReg));
914     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
915              m_xstate->xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes,
916              sizeof(YMMHReg));
917     return true;
918   }
919   return false; // unsupported or invalid byte order
920 }
921 
922 bool NativeRegisterContextLinux_x86_64::CopyYMMtoXSTATE(
923     uint32_t reg, lldb::ByteOrder byte_order) {
924   if (!IsAVX(reg))
925     return false;
926 
927   if (byte_order == lldb::eByteOrderLittle) {
928     ::memcpy(m_xstate->fxsave.xmm[reg - m_reg_info.first_ymm].bytes,
929              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, sizeof(XMMReg));
930     ::memcpy(m_xstate->xsave.ymmh[reg - m_reg_info.first_ymm].bytes,
931              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg),
932              sizeof(YMMHReg));
933     return true;
934   }
935 
936   if (byte_order == lldb::eByteOrderBig) {
937     ::memcpy(m_xstate->fxsave.xmm[reg - m_reg_info.first_ymm].bytes,
938              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg),
939              sizeof(XMMReg));
940     ::memcpy(m_xstate->xsave.ymmh[reg - m_reg_info.first_ymm].bytes,
941              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, sizeof(YMMHReg));
942     return true;
943   }
944   return false; // unsupported or invalid byte order
945 }
946 
947 void *NativeRegisterContextLinux_x86_64::GetFPRBuffer() {
948   switch (m_xstate_type) {
949   case XStateType::FXSAVE:
950     return &m_xstate->fxsave;
951   case XStateType::XSAVE:
952     return &m_iovec;
953   default:
954     return nullptr;
955   }
956 }
957 
958 size_t NativeRegisterContextLinux_x86_64::GetFPRSize() {
959   switch (m_xstate_type) {
960   case XStateType::FXSAVE:
961     return sizeof(m_xstate->fxsave);
962   case XStateType::XSAVE:
963     return sizeof(m_iovec);
964   default:
965     return 0;
966   }
967 }
968 
969 Status NativeRegisterContextLinux_x86_64::ReadFPR() {
970   Status error;
971 
972   // Probe XSAVE and if it is not supported fall back to FXSAVE.
973   if (m_xstate_type != XStateType::FXSAVE) {
974     error = ReadRegisterSet(&m_iovec, sizeof(m_xstate->xsave), NT_X86_XSTATE);
975     if (!error.Fail()) {
976       m_xstate_type = XStateType::XSAVE;
977       return error;
978     }
979   }
980   error = ReadRegisterSet(
981       &m_iovec, sizeof(m_xstate->xsave),
982       fxsr_regset(GetRegisterInfoInterface().GetTargetArchitecture()));
983   if (!error.Fail()) {
984     m_xstate_type = XStateType::FXSAVE;
985     return error;
986   }
987   return Status("Unrecognized FPR type.");
988 }
989 
990 bool NativeRegisterContextLinux_x86_64::IsMPX(uint32_t reg_index) const {
991   if (!IsCPUFeatureAvailable(RegSet::mpx))
992     return false;
993   return (m_reg_info.first_mpxr <= reg_index &&
994           reg_index <= m_reg_info.last_mpxc);
995 }
996 
997 bool NativeRegisterContextLinux_x86_64::CopyXSTATEtoMPX(uint32_t reg) {
998   if (!IsMPX(reg))
999     return false;
1000 
1001   if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
1002     ::memcpy(m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes,
1003              m_xstate->xsave.mpxr[reg - m_reg_info.first_mpxr].bytes,
1004              sizeof(MPXReg));
1005   } else {
1006     ::memcpy(m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes,
1007              m_xstate->xsave.mpxc[reg - m_reg_info.first_mpxc].bytes,
1008              sizeof(MPXCsr));
1009   }
1010   return true;
1011 }
1012 
1013 bool NativeRegisterContextLinux_x86_64::CopyMPXtoXSTATE(uint32_t reg) {
1014   if (!IsMPX(reg))
1015     return false;
1016 
1017   if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
1018     ::memcpy(m_xstate->xsave.mpxr[reg - m_reg_info.first_mpxr].bytes,
1019              m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes, sizeof(MPXReg));
1020   } else {
1021     ::memcpy(m_xstate->xsave.mpxc[reg - m_reg_info.first_mpxc].bytes,
1022              m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes, sizeof(MPXCsr));
1023   }
1024   return true;
1025 }
1026 
1027 Status NativeRegisterContextLinux_x86_64::IsWatchpointHit(uint32_t wp_index,
1028                                                           bool &is_hit) {
1029   if (wp_index >= NumSupportedHardwareWatchpoints())
1030     return Status("Watchpoint index out of range");
1031 
1032   RegisterValue reg_value;
1033   Status error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1034   if (error.Fail()) {
1035     is_hit = false;
1036     return error;
1037   }
1038 
1039   uint64_t status_bits = reg_value.GetAsUInt64();
1040 
1041   is_hit = status_bits & (1 << wp_index);
1042 
1043   return error;
1044 }
1045 
1046 Status NativeRegisterContextLinux_x86_64::GetWatchpointHitIndex(
1047     uint32_t &wp_index, lldb::addr_t trap_addr) {
1048   uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
1049   for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) {
1050     bool is_hit;
1051     Status error = IsWatchpointHit(wp_index, is_hit);
1052     if (error.Fail()) {
1053       wp_index = LLDB_INVALID_INDEX32;
1054       return error;
1055     } else if (is_hit) {
1056       return error;
1057     }
1058   }
1059   wp_index = LLDB_INVALID_INDEX32;
1060   return Status();
1061 }
1062 
1063 Status NativeRegisterContextLinux_x86_64::IsWatchpointVacant(uint32_t wp_index,
1064                                                              bool &is_vacant) {
1065   if (wp_index >= NumSupportedHardwareWatchpoints())
1066     return Status("Watchpoint index out of range");
1067 
1068   RegisterValue reg_value;
1069   Status error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1070   if (error.Fail()) {
1071     is_vacant = false;
1072     return error;
1073   }
1074 
1075   uint64_t control_bits = reg_value.GetAsUInt64();
1076 
1077   is_vacant = !(control_bits & (1 << (2 * wp_index)));
1078 
1079   return error;
1080 }
1081 
1082 Status NativeRegisterContextLinux_x86_64::SetHardwareWatchpointWithIndex(
1083     lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index) {
1084 
1085   if (wp_index >= NumSupportedHardwareWatchpoints())
1086     return Status("Watchpoint index out of range");
1087 
1088   // Read only watchpoints aren't supported on x86_64. Fall back to read/write
1089   // waitchpoints instead.
1090   // TODO: Add logic to detect when a write happens and ignore that watchpoint
1091   // hit.
1092   if (watch_flags == 0x2)
1093     watch_flags = 0x3;
1094 
1095   if (watch_flags != 0x1 && watch_flags != 0x3)
1096     return Status("Invalid read/write bits for watchpoint");
1097 
1098   if (size != 1 && size != 2 && size != 4 && size != 8)
1099     return Status("Invalid size for watchpoint");
1100 
1101   bool is_vacant;
1102   Status error = IsWatchpointVacant(wp_index, is_vacant);
1103   if (error.Fail())
1104     return error;
1105   if (!is_vacant)
1106     return Status("Watchpoint index not vacant");
1107 
1108   RegisterValue reg_value;
1109   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1110   if (error.Fail())
1111     return error;
1112 
1113   // for watchpoints 0, 1, 2, or 3, respectively, set bits 1, 3, 5, or 7
1114   uint64_t enable_bit = 1 << (2 * wp_index);
1115 
1116   // set bits 16-17, 20-21, 24-25, or 28-29
1117   // with 0b01 for write, and 0b11 for read/write
1118   uint64_t rw_bits = watch_flags << (16 + 4 * wp_index);
1119 
1120   // set bits 18-19, 22-23, 26-27, or 30-31
1121   // with 0b00, 0b01, 0b10, or 0b11
1122   // for 1, 2, 8 (if supported), or 4 bytes, respectively
1123   uint64_t size_bits = (size == 8 ? 0x2 : size - 1) << (18 + 4 * wp_index);
1124 
1125   uint64_t bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index));
1126 
1127   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
1128 
1129   control_bits |= enable_bit | rw_bits | size_bits;
1130 
1131   error = WriteRegisterRaw(m_reg_info.first_dr + wp_index, RegisterValue(addr));
1132   if (error.Fail())
1133     return error;
1134 
1135   error =
1136       WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits));
1137   if (error.Fail())
1138     return error;
1139 
1140   error.Clear();
1141   return error;
1142 }
1143 
1144 bool NativeRegisterContextLinux_x86_64::ClearHardwareWatchpoint(
1145     uint32_t wp_index) {
1146   if (wp_index >= NumSupportedHardwareWatchpoints())
1147     return false;
1148 
1149   RegisterValue reg_value;
1150 
1151   // for watchpoints 0, 1, 2, or 3, respectively, clear bits 0, 1, 2, or 3 of
1152   // the debug status register (DR6)
1153   Status error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1154   if (error.Fail())
1155     return false;
1156   uint64_t bit_mask = 1 << wp_index;
1157   uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask;
1158   error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits));
1159   if (error.Fail())
1160     return false;
1161 
1162   // for watchpoints 0, 1, 2, or 3, respectively, clear bits {0-1,16-19},
1163   // {2-3,20-23}, {4-5,24-27}, or {6-7,28-31} of the debug control register
1164   // (DR7)
1165   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1166   if (error.Fail())
1167     return false;
1168   bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index));
1169   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
1170   return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits))
1171       .Success();
1172 }
1173 
1174 Status NativeRegisterContextLinux_x86_64::ClearAllHardwareWatchpoints() {
1175   RegisterValue reg_value;
1176 
1177   // clear bits {0-4} of the debug status register (DR6)
1178   Status error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1179   if (error.Fail())
1180     return error;
1181   uint64_t bit_mask = 0xF;
1182   uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask;
1183   error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits));
1184   if (error.Fail())
1185     return error;
1186 
1187   // clear bits {0-7,16-31} of the debug control register (DR7)
1188   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1189   if (error.Fail())
1190     return error;
1191   bit_mask = 0xFF | (0xFFFF << 16);
1192   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
1193   return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits));
1194 }
1195 
1196 uint32_t NativeRegisterContextLinux_x86_64::SetHardwareWatchpoint(
1197     lldb::addr_t addr, size_t size, uint32_t watch_flags) {
1198   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1199   const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints();
1200   for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index) {
1201     bool is_vacant;
1202     Status error = IsWatchpointVacant(wp_index, is_vacant);
1203     if (is_vacant) {
1204       error = SetHardwareWatchpointWithIndex(addr, size, watch_flags, wp_index);
1205       if (error.Success())
1206         return wp_index;
1207     }
1208     if (error.Fail() && log) {
1209       log->Printf("NativeRegisterContextLinux_x86_64::%s Error: %s",
1210                   __FUNCTION__, error.AsCString());
1211     }
1212   }
1213   return LLDB_INVALID_INDEX32;
1214 }
1215 
1216 lldb::addr_t
1217 NativeRegisterContextLinux_x86_64::GetWatchpointAddress(uint32_t wp_index) {
1218   if (wp_index >= NumSupportedHardwareWatchpoints())
1219     return LLDB_INVALID_ADDRESS;
1220   RegisterValue reg_value;
1221   if (ReadRegisterRaw(m_reg_info.first_dr + wp_index, reg_value).Fail())
1222     return LLDB_INVALID_ADDRESS;
1223   return reg_value.GetAsUInt64();
1224 }
1225 
1226 uint32_t NativeRegisterContextLinux_x86_64::NumSupportedHardwareWatchpoints() {
1227   // Available debug address registers: dr0, dr1, dr2, dr3
1228   return 4;
1229 }
1230 
1231 #endif // defined(__i386__) || defined(__x86_64__)
1232