12850b1beSTodd Fiala //===-- NativeRegisterContextLinux_x86_64.cpp ---------------*- C++ -*-===//
22850b1beSTodd Fiala //
32850b1beSTodd Fiala //                     The LLVM Compiler Infrastructure
42850b1beSTodd Fiala //
52850b1beSTodd Fiala // This file is distributed under the University of Illinois Open Source
62850b1beSTodd Fiala // License. See LICENSE.TXT for details.
72850b1beSTodd Fiala //
82850b1beSTodd Fiala //===----------------------------------------------------------------------===//
92850b1beSTodd Fiala 
10068f8a7eSTamas Berghammer #if defined(__i386__) || defined(__x86_64__)
11068f8a7eSTamas Berghammer 
122850b1beSTodd Fiala #include "NativeRegisterContextLinux_x86_64.h"
132850b1beSTodd Fiala 
142850b1beSTodd Fiala #include "lldb/Core/DataBufferHeap.h"
152850b1beSTodd Fiala #include "lldb/Core/Error.h"
16b9c1b51eSKate Stone #include "lldb/Core/Log.h"
172850b1beSTodd Fiala #include "lldb/Core/RegisterValue.h"
18068f8a7eSTamas Berghammer #include "lldb/Host/HostInfo.h"
19068f8a7eSTamas Berghammer 
20068f8a7eSTamas Berghammer #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
21068f8a7eSTamas Berghammer #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
222850b1beSTodd Fiala 
232850b1beSTodd Fiala using namespace lldb_private;
24db264a6dSTamas Berghammer using namespace lldb_private::process_linux;
252850b1beSTodd Fiala 
262850b1beSTodd Fiala // ----------------------------------------------------------------------------
272850b1beSTodd Fiala // Private namespace.
282850b1beSTodd Fiala // ----------------------------------------------------------------------------
292850b1beSTodd Fiala 
30b9c1b51eSKate Stone namespace {
312850b1beSTodd Fiala // x86 32-bit general purpose registers.
32b9c1b51eSKate Stone const uint32_t g_gpr_regnums_i386[] = {
33b9c1b51eSKate Stone     lldb_eax_i386,      lldb_ebx_i386,    lldb_ecx_i386, lldb_edx_i386,
34b9c1b51eSKate Stone     lldb_edi_i386,      lldb_esi_i386,    lldb_ebp_i386, lldb_esp_i386,
35b9c1b51eSKate Stone     lldb_eip_i386,      lldb_eflags_i386, lldb_cs_i386,  lldb_fs_i386,
36b9c1b51eSKate Stone     lldb_gs_i386,       lldb_ss_i386,     lldb_ds_i386,  lldb_es_i386,
37b9c1b51eSKate Stone     lldb_ax_i386,       lldb_bx_i386,     lldb_cx_i386,  lldb_dx_i386,
38b9c1b51eSKate Stone     lldb_di_i386,       lldb_si_i386,     lldb_bp_i386,  lldb_sp_i386,
39b9c1b51eSKate Stone     lldb_ah_i386,       lldb_bh_i386,     lldb_ch_i386,  lldb_dh_i386,
40b9c1b51eSKate Stone     lldb_al_i386,       lldb_bl_i386,     lldb_cl_i386,  lldb_dl_i386,
412850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
422850b1beSTodd Fiala };
43b9c1b51eSKate Stone static_assert((sizeof(g_gpr_regnums_i386) / sizeof(g_gpr_regnums_i386[0])) -
44b9c1b51eSKate Stone                       1 ==
45b9c1b51eSKate Stone                   k_num_gpr_registers_i386,
462850b1beSTodd Fiala               "g_gpr_regnums_i386 has wrong number of register infos");
472850b1beSTodd Fiala 
482850b1beSTodd Fiala // x86 32-bit floating point registers.
49b9c1b51eSKate Stone const uint32_t g_fpu_regnums_i386[] = {
50b9c1b51eSKate Stone     lldb_fctrl_i386,    lldb_fstat_i386,     lldb_ftag_i386,  lldb_fop_i386,
51b9c1b51eSKate Stone     lldb_fiseg_i386,    lldb_fioff_i386,     lldb_foseg_i386, lldb_fooff_i386,
52b9c1b51eSKate Stone     lldb_mxcsr_i386,    lldb_mxcsrmask_i386, lldb_st0_i386,   lldb_st1_i386,
53b9c1b51eSKate Stone     lldb_st2_i386,      lldb_st3_i386,       lldb_st4_i386,   lldb_st5_i386,
54b9c1b51eSKate Stone     lldb_st6_i386,      lldb_st7_i386,       lldb_mm0_i386,   lldb_mm1_i386,
55b9c1b51eSKate Stone     lldb_mm2_i386,      lldb_mm3_i386,       lldb_mm4_i386,   lldb_mm5_i386,
56b9c1b51eSKate Stone     lldb_mm6_i386,      lldb_mm7_i386,       lldb_xmm0_i386,  lldb_xmm1_i386,
57b9c1b51eSKate Stone     lldb_xmm2_i386,     lldb_xmm3_i386,      lldb_xmm4_i386,  lldb_xmm5_i386,
58b9c1b51eSKate Stone     lldb_xmm6_i386,     lldb_xmm7_i386,
592850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
602850b1beSTodd Fiala };
61b9c1b51eSKate Stone static_assert((sizeof(g_fpu_regnums_i386) / sizeof(g_fpu_regnums_i386[0])) -
62b9c1b51eSKate Stone                       1 ==
63b9c1b51eSKate Stone                   k_num_fpr_registers_i386,
642850b1beSTodd Fiala               "g_fpu_regnums_i386 has wrong number of register infos");
652850b1beSTodd Fiala 
662850b1beSTodd Fiala // x86 32-bit AVX registers.
67b9c1b51eSKate Stone const uint32_t g_avx_regnums_i386[] = {
68b9c1b51eSKate Stone     lldb_ymm0_i386,     lldb_ymm1_i386, lldb_ymm2_i386, lldb_ymm3_i386,
69b9c1b51eSKate Stone     lldb_ymm4_i386,     lldb_ymm5_i386, lldb_ymm6_i386, lldb_ymm7_i386,
702850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
712850b1beSTodd Fiala };
72b9c1b51eSKate Stone static_assert((sizeof(g_avx_regnums_i386) / sizeof(g_avx_regnums_i386[0])) -
73b9c1b51eSKate Stone                       1 ==
74b9c1b51eSKate Stone                   k_num_avx_registers_i386,
752850b1beSTodd Fiala               " g_avx_regnums_i386 has wrong number of register infos");
762850b1beSTodd Fiala 
77cda0ae46SValentina Giusti // x64 32-bit MPX registers.
78cda0ae46SValentina Giusti static const uint32_t g_mpx_regnums_i386[] = {
79cda0ae46SValentina Giusti     lldb_bnd0_i386,     lldb_bnd1_i386, lldb_bnd2_i386, lldb_bnd3_i386,
80cda0ae46SValentina Giusti     lldb_bndcfgu_i386,  lldb_bndstatus_i386,
81cda0ae46SValentina Giusti     LLDB_INVALID_REGNUM // register sets need to end with this flag
82cda0ae46SValentina Giusti };
83cda0ae46SValentina Giusti static_assert((sizeof(g_mpx_regnums_i386) / sizeof(g_mpx_regnums_i386[0])) -
84cda0ae46SValentina Giusti                       1 ==
85cda0ae46SValentina Giusti                   k_num_mpx_registers_i386,
86cda0ae46SValentina Giusti               "g_mpx_regnums_x86_64 has wrong number of register infos");
87cda0ae46SValentina Giusti 
882850b1beSTodd Fiala // x86 64-bit general purpose registers.
89b9c1b51eSKate Stone static const uint32_t g_gpr_regnums_x86_64[] = {
90b9c1b51eSKate Stone     lldb_rax_x86_64,    lldb_rbx_x86_64,    lldb_rcx_x86_64, lldb_rdx_x86_64,
91b9c1b51eSKate Stone     lldb_rdi_x86_64,    lldb_rsi_x86_64,    lldb_rbp_x86_64, lldb_rsp_x86_64,
92b9c1b51eSKate Stone     lldb_r8_x86_64,     lldb_r9_x86_64,     lldb_r10_x86_64, lldb_r11_x86_64,
93b9c1b51eSKate Stone     lldb_r12_x86_64,    lldb_r13_x86_64,    lldb_r14_x86_64, lldb_r15_x86_64,
94b9c1b51eSKate Stone     lldb_rip_x86_64,    lldb_rflags_x86_64, lldb_cs_x86_64,  lldb_fs_x86_64,
95b9c1b51eSKate Stone     lldb_gs_x86_64,     lldb_ss_x86_64,     lldb_ds_x86_64,  lldb_es_x86_64,
96b9c1b51eSKate Stone     lldb_eax_x86_64,    lldb_ebx_x86_64,    lldb_ecx_x86_64, lldb_edx_x86_64,
97b9c1b51eSKate Stone     lldb_edi_x86_64,    lldb_esi_x86_64,    lldb_ebp_x86_64, lldb_esp_x86_64,
987f013bcdSZachary Turner     lldb_r8d_x86_64,  // Low 32 bits or r8
997f013bcdSZachary Turner     lldb_r9d_x86_64,  // Low 32 bits or r9
1007f013bcdSZachary Turner     lldb_r10d_x86_64, // Low 32 bits or r10
1017f013bcdSZachary Turner     lldb_r11d_x86_64, // Low 32 bits or r11
1027f013bcdSZachary Turner     lldb_r12d_x86_64, // Low 32 bits or r12
1037f013bcdSZachary Turner     lldb_r13d_x86_64, // Low 32 bits or r13
1047f013bcdSZachary Turner     lldb_r14d_x86_64, // Low 32 bits or r14
1057f013bcdSZachary Turner     lldb_r15d_x86_64, // Low 32 bits or r15
106b9c1b51eSKate Stone     lldb_ax_x86_64,     lldb_bx_x86_64,     lldb_cx_x86_64,  lldb_dx_x86_64,
107b9c1b51eSKate Stone     lldb_di_x86_64,     lldb_si_x86_64,     lldb_bp_x86_64,  lldb_sp_x86_64,
1087f013bcdSZachary Turner     lldb_r8w_x86_64,  // Low 16 bits or r8
1097f013bcdSZachary Turner     lldb_r9w_x86_64,  // Low 16 bits or r9
1107f013bcdSZachary Turner     lldb_r10w_x86_64, // Low 16 bits or r10
1117f013bcdSZachary Turner     lldb_r11w_x86_64, // Low 16 bits or r11
1127f013bcdSZachary Turner     lldb_r12w_x86_64, // Low 16 bits or r12
1137f013bcdSZachary Turner     lldb_r13w_x86_64, // Low 16 bits or r13
1147f013bcdSZachary Turner     lldb_r14w_x86_64, // Low 16 bits or r14
1157f013bcdSZachary Turner     lldb_r15w_x86_64, // Low 16 bits or r15
116b9c1b51eSKate Stone     lldb_ah_x86_64,     lldb_bh_x86_64,     lldb_ch_x86_64,  lldb_dh_x86_64,
117b9c1b51eSKate Stone     lldb_al_x86_64,     lldb_bl_x86_64,     lldb_cl_x86_64,  lldb_dl_x86_64,
118b9c1b51eSKate Stone     lldb_dil_x86_64,    lldb_sil_x86_64,    lldb_bpl_x86_64, lldb_spl_x86_64,
1197f013bcdSZachary Turner     lldb_r8l_x86_64,    // Low 8 bits or r8
1207f013bcdSZachary Turner     lldb_r9l_x86_64,    // Low 8 bits or r9
1217f013bcdSZachary Turner     lldb_r10l_x86_64,   // Low 8 bits or r10
1227f013bcdSZachary Turner     lldb_r11l_x86_64,   // Low 8 bits or r11
1237f013bcdSZachary Turner     lldb_r12l_x86_64,   // Low 8 bits or r12
1247f013bcdSZachary Turner     lldb_r13l_x86_64,   // Low 8 bits or r13
1257f013bcdSZachary Turner     lldb_r14l_x86_64,   // Low 8 bits or r14
1267f013bcdSZachary Turner     lldb_r15l_x86_64,   // Low 8 bits or r15
1272850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
1282850b1beSTodd Fiala };
129b9c1b51eSKate Stone static_assert((sizeof(g_gpr_regnums_x86_64) / sizeof(g_gpr_regnums_x86_64[0])) -
130b9c1b51eSKate Stone                       1 ==
131b9c1b51eSKate Stone                   k_num_gpr_registers_x86_64,
1322850b1beSTodd Fiala               "g_gpr_regnums_x86_64 has wrong number of register infos");
1332850b1beSTodd Fiala 
1342850b1beSTodd Fiala // x86 64-bit floating point registers.
135b9c1b51eSKate Stone static const uint32_t g_fpu_regnums_x86_64[] = {
136b9c1b51eSKate Stone     lldb_fctrl_x86_64,     lldb_fstat_x86_64, lldb_ftag_x86_64,
137b9c1b51eSKate Stone     lldb_fop_x86_64,       lldb_fiseg_x86_64, lldb_fioff_x86_64,
138b9c1b51eSKate Stone     lldb_foseg_x86_64,     lldb_fooff_x86_64, lldb_mxcsr_x86_64,
139b9c1b51eSKate Stone     lldb_mxcsrmask_x86_64, lldb_st0_x86_64,   lldb_st1_x86_64,
140b9c1b51eSKate Stone     lldb_st2_x86_64,       lldb_st3_x86_64,   lldb_st4_x86_64,
141b9c1b51eSKate Stone     lldb_st5_x86_64,       lldb_st6_x86_64,   lldb_st7_x86_64,
142b9c1b51eSKate Stone     lldb_mm0_x86_64,       lldb_mm1_x86_64,   lldb_mm2_x86_64,
143b9c1b51eSKate Stone     lldb_mm3_x86_64,       lldb_mm4_x86_64,   lldb_mm5_x86_64,
144b9c1b51eSKate Stone     lldb_mm6_x86_64,       lldb_mm7_x86_64,   lldb_xmm0_x86_64,
145b9c1b51eSKate Stone     lldb_xmm1_x86_64,      lldb_xmm2_x86_64,  lldb_xmm3_x86_64,
146b9c1b51eSKate Stone     lldb_xmm4_x86_64,      lldb_xmm5_x86_64,  lldb_xmm6_x86_64,
147b9c1b51eSKate Stone     lldb_xmm7_x86_64,      lldb_xmm8_x86_64,  lldb_xmm9_x86_64,
148b9c1b51eSKate Stone     lldb_xmm10_x86_64,     lldb_xmm11_x86_64, lldb_xmm12_x86_64,
149b9c1b51eSKate Stone     lldb_xmm13_x86_64,     lldb_xmm14_x86_64, lldb_xmm15_x86_64,
1502850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
1512850b1beSTodd Fiala };
152b9c1b51eSKate Stone static_assert((sizeof(g_fpu_regnums_x86_64) / sizeof(g_fpu_regnums_x86_64[0])) -
153b9c1b51eSKate Stone                       1 ==
154b9c1b51eSKate Stone                   k_num_fpr_registers_x86_64,
1552850b1beSTodd Fiala               "g_fpu_regnums_x86_64 has wrong number of register infos");
1562850b1beSTodd Fiala 
1572850b1beSTodd Fiala // x86 64-bit AVX registers.
158b9c1b51eSKate Stone static const uint32_t g_avx_regnums_x86_64[] = {
159b9c1b51eSKate Stone     lldb_ymm0_x86_64,   lldb_ymm1_x86_64,  lldb_ymm2_x86_64,  lldb_ymm3_x86_64,
160b9c1b51eSKate Stone     lldb_ymm4_x86_64,   lldb_ymm5_x86_64,  lldb_ymm6_x86_64,  lldb_ymm7_x86_64,
161b9c1b51eSKate Stone     lldb_ymm8_x86_64,   lldb_ymm9_x86_64,  lldb_ymm10_x86_64, lldb_ymm11_x86_64,
162b9c1b51eSKate Stone     lldb_ymm12_x86_64,  lldb_ymm13_x86_64, lldb_ymm14_x86_64, lldb_ymm15_x86_64,
1632850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
1642850b1beSTodd Fiala };
165b9c1b51eSKate Stone static_assert((sizeof(g_avx_regnums_x86_64) / sizeof(g_avx_regnums_x86_64[0])) -
166b9c1b51eSKate Stone                       1 ==
167b9c1b51eSKate Stone                   k_num_avx_registers_x86_64,
1682850b1beSTodd Fiala               "g_avx_regnums_x86_64 has wrong number of register infos");
1692850b1beSTodd Fiala 
170cda0ae46SValentina Giusti // x86 64-bit MPX registers.
171cda0ae46SValentina Giusti static const uint32_t g_mpx_regnums_x86_64[] = {
172cda0ae46SValentina Giusti     lldb_bnd0_x86_64,    lldb_bnd1_x86_64,    lldb_bnd2_x86_64,
173cda0ae46SValentina Giusti     lldb_bnd3_x86_64,    lldb_bndcfgu_x86_64, lldb_bndstatus_x86_64,
174cda0ae46SValentina Giusti     LLDB_INVALID_REGNUM // register sets need to end with this flag
175cda0ae46SValentina Giusti };
176cda0ae46SValentina Giusti static_assert((sizeof(g_mpx_regnums_x86_64) / sizeof(g_mpx_regnums_x86_64[0])) -
177cda0ae46SValentina Giusti                       1 ==
178cda0ae46SValentina Giusti                   k_num_mpx_registers_x86_64,
179cda0ae46SValentina Giusti               "g_mpx_regnums_x86_64 has wrong number of register infos");
180cda0ae46SValentina Giusti 
1812850b1beSTodd Fiala // Number of register sets provided by this context.
182cda0ae46SValentina Giusti enum { k_num_extended_register_sets = 2, k_num_register_sets = 4 };
1832850b1beSTodd Fiala 
1842850b1beSTodd Fiala // Register sets for x86 32-bit.
185b9c1b51eSKate Stone static const RegisterSet g_reg_sets_i386[k_num_register_sets] = {
186b9c1b51eSKate Stone     {"General Purpose Registers", "gpr", k_num_gpr_registers_i386,
187b9c1b51eSKate Stone      g_gpr_regnums_i386},
188b9c1b51eSKate Stone     {"Floating Point Registers", "fpu", k_num_fpr_registers_i386,
189b9c1b51eSKate Stone      g_fpu_regnums_i386},
190b9c1b51eSKate Stone     {"Advanced Vector Extensions", "avx", k_num_avx_registers_i386,
191cda0ae46SValentina Giusti      g_avx_regnums_i386},
192cda0ae46SValentina Giusti     { "Memory Protection Extensions", "mpx", k_num_mpx_registers_i386,
193cda0ae46SValentina Giusti      g_mpx_regnums_i386}};
1942850b1beSTodd Fiala 
1952850b1beSTodd Fiala // Register sets for x86 64-bit.
196b9c1b51eSKate Stone static const RegisterSet g_reg_sets_x86_64[k_num_register_sets] = {
197b9c1b51eSKate Stone     {"General Purpose Registers", "gpr", k_num_gpr_registers_x86_64,
198b9c1b51eSKate Stone      g_gpr_regnums_x86_64},
199b9c1b51eSKate Stone     {"Floating Point Registers", "fpu", k_num_fpr_registers_x86_64,
200b9c1b51eSKate Stone      g_fpu_regnums_x86_64},
201b9c1b51eSKate Stone     {"Advanced Vector Extensions", "avx", k_num_avx_registers_x86_64,
202cda0ae46SValentina Giusti      g_avx_regnums_x86_64},
203cda0ae46SValentina Giusti     { "Memory Protection Extensions", "mpx", k_num_mpx_registers_x86_64,
204cda0ae46SValentina Giusti      g_mpx_regnums_x86_64}};
2052850b1beSTodd Fiala }
2062850b1beSTodd Fiala 
2072850b1beSTodd Fiala #define REG_CONTEXT_SIZE (GetRegisterInfoInterface().GetGPRSize() + sizeof(FPR))
2082850b1beSTodd Fiala 
2092850b1beSTodd Fiala // ----------------------------------------------------------------------------
2102850b1beSTodd Fiala // Required ptrace defines.
2112850b1beSTodd Fiala // ----------------------------------------------------------------------------
2122850b1beSTodd Fiala 
2132850b1beSTodd Fiala // Support ptrace extensions even when compiled without required kernel support
2142850b1beSTodd Fiala #ifndef NT_X86_XSTATE
2152850b1beSTodd Fiala #define NT_X86_XSTATE 0x202
2162850b1beSTodd Fiala #endif
217296e063dSAbhishek Aggarwal #ifndef NT_PRXFPREG
218296e063dSAbhishek Aggarwal #define NT_PRXFPREG 0x46e62b7f
219296e063dSAbhishek Aggarwal #endif
2202850b1beSTodd Fiala 
2215f957b54SValentina Giusti // ----------------------------------------------------------------------------
2225f957b54SValentina Giusti // Required MPX define.
2235f957b54SValentina Giusti // ----------------------------------------------------------------------------
2245f957b54SValentina Giusti 
2255f957b54SValentina Giusti // Support MPX extensions also if compiled with compiler without MPX support.
2265f957b54SValentina Giusti #ifndef bit_MPX
2275f957b54SValentina Giusti #define bit_MPX 0x4000
2285f957b54SValentina Giusti #endif
2295f957b54SValentina Giusti 
2305f957b54SValentina Giusti // ----------------------------------------------------------------------------
2315f957b54SValentina Giusti // XCR0 extended register sets masks.
2325f957b54SValentina Giusti // ----------------------------------------------------------------------------
2335f957b54SValentina Giusti #define mask_XSTATE_AVX (1ULL << 2)
2345f957b54SValentina Giusti #define mask_XSTATE_BNDREGS (1ULL << 3)
2355f957b54SValentina Giusti #define mask_XSTATE_BNDCFG (1ULL << 4)
2365f957b54SValentina Giusti #define mask_XSTATE_MPX (mask_XSTATE_BNDREGS | mask_XSTATE_BNDCFG)
2375f957b54SValentina Giusti 
238068f8a7eSTamas Berghammer NativeRegisterContextLinux *
239b9c1b51eSKate Stone NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(
240b9c1b51eSKate Stone     const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
241b9c1b51eSKate Stone     uint32_t concrete_frame_idx) {
242b9c1b51eSKate Stone   return new NativeRegisterContextLinux_x86_64(target_arch, native_thread,
243b9c1b51eSKate Stone                                                concrete_frame_idx);
244068f8a7eSTamas Berghammer }
245068f8a7eSTamas Berghammer 
2462850b1beSTodd Fiala // ----------------------------------------------------------------------------
2472850b1beSTodd Fiala // NativeRegisterContextLinux_x86_64 members.
2482850b1beSTodd Fiala // ----------------------------------------------------------------------------
2492850b1beSTodd Fiala 
250068f8a7eSTamas Berghammer static RegisterInfoInterface *
251b9c1b51eSKate Stone CreateRegisterInfoInterface(const ArchSpec &target_arch) {
252b9c1b51eSKate Stone   if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) {
253068f8a7eSTamas Berghammer     // 32-bit hosts run with a RegisterContextLinux_i386 context.
254068f8a7eSTamas Berghammer     return new RegisterContextLinux_i386(target_arch);
255b9c1b51eSKate Stone   } else {
256068f8a7eSTamas Berghammer     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
257068f8a7eSTamas Berghammer            "Register setting path assumes this is a 64-bit host");
258b9c1b51eSKate Stone     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the
259b9c1b51eSKate Stone     // x86_64 register context.
260068f8a7eSTamas Berghammer     return new RegisterContextLinux_x86_64(target_arch);
261068f8a7eSTamas Berghammer   }
262068f8a7eSTamas Berghammer }
263068f8a7eSTamas Berghammer 
264b9c1b51eSKate Stone NativeRegisterContextLinux_x86_64::NativeRegisterContextLinux_x86_64(
265b9c1b51eSKate Stone     const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
266b9c1b51eSKate Stone     uint32_t concrete_frame_idx)
267b9c1b51eSKate Stone     : NativeRegisterContextLinux(native_thread, concrete_frame_idx,
268b9c1b51eSKate Stone                                  CreateRegisterInfoInterface(target_arch)),
26997e57e9bSValentina Giusti       m_xstate_type(XStateType::Invalid), m_fpr(), m_iovec(), m_ymm_set(),
270cda0ae46SValentina Giusti       m_mpx_set(), m_reg_info(), m_gpr_x86_64() {
2712850b1beSTodd Fiala   // Set up data about ranges of valid registers.
272b9c1b51eSKate Stone   switch (target_arch.GetMachine()) {
2732850b1beSTodd Fiala   case llvm::Triple::x86:
2742850b1beSTodd Fiala     m_reg_info.num_registers = k_num_registers_i386;
2752850b1beSTodd Fiala     m_reg_info.num_gpr_registers = k_num_gpr_registers_i386;
2762850b1beSTodd Fiala     m_reg_info.num_fpr_registers = k_num_fpr_registers_i386;
2772850b1beSTodd Fiala     m_reg_info.num_avx_registers = k_num_avx_registers_i386;
278cda0ae46SValentina Giusti     m_reg_info.num_mpx_registers = k_num_mpx_registers_i386;
2792850b1beSTodd Fiala     m_reg_info.last_gpr = k_last_gpr_i386;
2802850b1beSTodd Fiala     m_reg_info.first_fpr = k_first_fpr_i386;
2812850b1beSTodd Fiala     m_reg_info.last_fpr = k_last_fpr_i386;
2827f013bcdSZachary Turner     m_reg_info.first_st = lldb_st0_i386;
2837f013bcdSZachary Turner     m_reg_info.last_st = lldb_st7_i386;
2847f013bcdSZachary Turner     m_reg_info.first_mm = lldb_mm0_i386;
2857f013bcdSZachary Turner     m_reg_info.last_mm = lldb_mm7_i386;
2867f013bcdSZachary Turner     m_reg_info.first_xmm = lldb_xmm0_i386;
2877f013bcdSZachary Turner     m_reg_info.last_xmm = lldb_xmm7_i386;
2887f013bcdSZachary Turner     m_reg_info.first_ymm = lldb_ymm0_i386;
2897f013bcdSZachary Turner     m_reg_info.last_ymm = lldb_ymm7_i386;
290cda0ae46SValentina Giusti     m_reg_info.first_mpxr = lldb_bnd0_i386;
291cda0ae46SValentina Giusti     m_reg_info.last_mpxr = lldb_bnd3_i386;
292cda0ae46SValentina Giusti     m_reg_info.first_mpxc = lldb_bndcfgu_i386;
293cda0ae46SValentina Giusti     m_reg_info.last_mpxc = lldb_bndstatus_i386;
2947f013bcdSZachary Turner     m_reg_info.first_dr = lldb_dr0_i386;
2957f013bcdSZachary Turner     m_reg_info.gpr_flags = lldb_eflags_i386;
2962850b1beSTodd Fiala     break;
2972850b1beSTodd Fiala   case llvm::Triple::x86_64:
2982850b1beSTodd Fiala     m_reg_info.num_registers = k_num_registers_x86_64;
2992850b1beSTodd Fiala     m_reg_info.num_gpr_registers = k_num_gpr_registers_x86_64;
3002850b1beSTodd Fiala     m_reg_info.num_fpr_registers = k_num_fpr_registers_x86_64;
3012850b1beSTodd Fiala     m_reg_info.num_avx_registers = k_num_avx_registers_x86_64;
302cda0ae46SValentina Giusti     m_reg_info.num_mpx_registers = k_num_mpx_registers_x86_64;
3032850b1beSTodd Fiala     m_reg_info.last_gpr = k_last_gpr_x86_64;
3042850b1beSTodd Fiala     m_reg_info.first_fpr = k_first_fpr_x86_64;
3052850b1beSTodd Fiala     m_reg_info.last_fpr = k_last_fpr_x86_64;
3067f013bcdSZachary Turner     m_reg_info.first_st = lldb_st0_x86_64;
3077f013bcdSZachary Turner     m_reg_info.last_st = lldb_st7_x86_64;
3087f013bcdSZachary Turner     m_reg_info.first_mm = lldb_mm0_x86_64;
3097f013bcdSZachary Turner     m_reg_info.last_mm = lldb_mm7_x86_64;
3107f013bcdSZachary Turner     m_reg_info.first_xmm = lldb_xmm0_x86_64;
3117f013bcdSZachary Turner     m_reg_info.last_xmm = lldb_xmm15_x86_64;
3127f013bcdSZachary Turner     m_reg_info.first_ymm = lldb_ymm0_x86_64;
3137f013bcdSZachary Turner     m_reg_info.last_ymm = lldb_ymm15_x86_64;
314cda0ae46SValentina Giusti     m_reg_info.first_mpxr = lldb_bnd0_x86_64;
315cda0ae46SValentina Giusti     m_reg_info.last_mpxr = lldb_bnd3_x86_64;
316cda0ae46SValentina Giusti     m_reg_info.first_mpxc = lldb_bndcfgu_x86_64;
317cda0ae46SValentina Giusti     m_reg_info.last_mpxc = lldb_bndstatus_x86_64;
3187f013bcdSZachary Turner     m_reg_info.first_dr = lldb_dr0_x86_64;
3197f013bcdSZachary Turner     m_reg_info.gpr_flags = lldb_rflags_x86_64;
3202850b1beSTodd Fiala     break;
3212850b1beSTodd Fiala   default:
3222850b1beSTodd Fiala     assert(false && "Unhandled target architecture.");
3232850b1beSTodd Fiala     break;
3242850b1beSTodd Fiala   }
3252850b1beSTodd Fiala 
3262850b1beSTodd Fiala   // Initialize m_iovec to point to the buffer and buffer size
3272850b1beSTodd Fiala   // using the conventions of Berkeley style UIO structures, as required
3282850b1beSTodd Fiala   // by PTRACE extensions.
3292850b1beSTodd Fiala   m_iovec.iov_base = &m_fpr.xstate.xsave;
3302850b1beSTodd Fiala   m_iovec.iov_len = sizeof(m_fpr.xstate.xsave);
3312850b1beSTodd Fiala 
3322850b1beSTodd Fiala   // Clear out the FPR state.
3332850b1beSTodd Fiala   ::memset(&m_fpr, 0, sizeof(FPR));
3347f658eddSAbhishek Aggarwal 
3357f658eddSAbhishek Aggarwal   // Store byte offset of fctrl (i.e. first register of FPR)
3367f658eddSAbhishek Aggarwal   const RegisterInfo *reg_info_fctrl = GetRegisterInfoByName("fctrl");
3377f658eddSAbhishek Aggarwal   m_fctrl_offset_in_userarea = reg_info_fctrl->byte_offset;
3382850b1beSTodd Fiala }
3392850b1beSTodd Fiala 
3402850b1beSTodd Fiala // CONSIDER after local and llgs debugging are merged, register set support can
3412850b1beSTodd Fiala // be moved into a base x86-64 class with IsRegisterSetAvailable made virtual.
342b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::GetRegisterSetCount() const {
3432850b1beSTodd Fiala   uint32_t sets = 0;
344b9c1b51eSKate Stone   for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) {
3452850b1beSTodd Fiala     if (IsRegisterSetAvailable(set_index))
3462850b1beSTodd Fiala       ++sets;
3472850b1beSTodd Fiala   }
3482850b1beSTodd Fiala 
3492850b1beSTodd Fiala   return sets;
3502850b1beSTodd Fiala }
3512850b1beSTodd Fiala 
352b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::GetUserRegisterCount() const {
3538fa23b8eSTamas Berghammer   uint32_t count = 0;
354b9c1b51eSKate Stone   for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) {
355db264a6dSTamas Berghammer     const RegisterSet *set = GetRegisterSet(set_index);
3568fa23b8eSTamas Berghammer     if (set)
3578fa23b8eSTamas Berghammer       count += set->num_registers;
3588fa23b8eSTamas Berghammer   }
3598fa23b8eSTamas Berghammer   return count;
3608fa23b8eSTamas Berghammer }
3618fa23b8eSTamas Berghammer 
362db264a6dSTamas Berghammer const RegisterSet *
363b9c1b51eSKate Stone NativeRegisterContextLinux_x86_64::GetRegisterSet(uint32_t set_index) const {
3642850b1beSTodd Fiala   if (!IsRegisterSetAvailable(set_index))
3652850b1beSTodd Fiala     return nullptr;
3662850b1beSTodd Fiala 
367b9c1b51eSKate Stone   switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
3682850b1beSTodd Fiala   case llvm::Triple::x86:
3692850b1beSTodd Fiala     return &g_reg_sets_i386[set_index];
3702850b1beSTodd Fiala   case llvm::Triple::x86_64:
3712850b1beSTodd Fiala     return &g_reg_sets_x86_64[set_index];
3722850b1beSTodd Fiala   default:
3732850b1beSTodd Fiala     assert(false && "Unhandled target architecture.");
3742850b1beSTodd Fiala     return nullptr;
3752850b1beSTodd Fiala   }
3762850b1beSTodd Fiala 
3772850b1beSTodd Fiala   return nullptr;
3782850b1beSTodd Fiala }
3792850b1beSTodd Fiala 
380b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::ReadRegister(
381b9c1b51eSKate Stone     const RegisterInfo *reg_info, RegisterValue &reg_value) {
3822850b1beSTodd Fiala   Error error;
3832850b1beSTodd Fiala 
384b9c1b51eSKate Stone   if (!reg_info) {
3852850b1beSTodd Fiala     error.SetErrorString("reg_info NULL");
3862850b1beSTodd Fiala     return error;
3872850b1beSTodd Fiala   }
3882850b1beSTodd Fiala 
3892850b1beSTodd Fiala   const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
390b9c1b51eSKate Stone   if (reg == LLDB_INVALID_REGNUM) {
391b9c1b51eSKate Stone     // This is likely an internal register for lldb use only and should not be
392b9c1b51eSKate Stone     // directly queried.
393b9c1b51eSKate Stone     error.SetErrorStringWithFormat("register \"%s\" is an internal-only lldb "
394b9c1b51eSKate Stone                                    "register, cannot read directly",
395b9c1b51eSKate Stone                                    reg_info->name);
3962850b1beSTodd Fiala     return error;
3972850b1beSTodd Fiala   }
3982850b1beSTodd Fiala 
3995f957b54SValentina Giusti   if (IsFPR(reg) || IsAVX(reg) || IsMPX(reg)) {
400068f8a7eSTamas Berghammer     error = ReadFPR();
401068f8a7eSTamas Berghammer     if (error.Fail())
4022850b1beSTodd Fiala       return error;
403b9c1b51eSKate Stone   } else {
4042850b1beSTodd Fiala     uint32_t full_reg = reg;
405b9c1b51eSKate Stone     bool is_subreg = reg_info->invalidate_regs &&
406b9c1b51eSKate Stone                      (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM);
4072850b1beSTodd Fiala 
408b9c1b51eSKate Stone     if (is_subreg) {
4092850b1beSTodd Fiala       // Read the full aligned 64-bit register.
4102850b1beSTodd Fiala       full_reg = reg_info->invalidate_regs[0];
4112850b1beSTodd Fiala     }
4122850b1beSTodd Fiala 
4132850b1beSTodd Fiala     error = ReadRegisterRaw(full_reg, reg_value);
4142850b1beSTodd Fiala 
415b9c1b51eSKate Stone     if (error.Success()) {
416b9c1b51eSKate Stone       // If our read was not aligned (for ah,bh,ch,dh), shift our returned value
417b9c1b51eSKate Stone       // one byte to the right.
4182850b1beSTodd Fiala       if (is_subreg && (reg_info->byte_offset & 0x1))
4192850b1beSTodd Fiala         reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8);
4202850b1beSTodd Fiala 
421b9c1b51eSKate Stone       // If our return byte size was greater than the return value reg size,
422b9c1b51eSKate Stone       // then
4232850b1beSTodd Fiala       // use the type specified by reg_info rather than the uint64_t default
4242850b1beSTodd Fiala       if (reg_value.GetByteSize() > reg_info->byte_size)
4252850b1beSTodd Fiala         reg_value.SetType(reg_info);
4262850b1beSTodd Fiala     }
4272850b1beSTodd Fiala     return error;
4282850b1beSTodd Fiala   }
4292850b1beSTodd Fiala 
430b9c1b51eSKate Stone   if (reg_info->encoding == lldb::eEncodingVector) {
4312850b1beSTodd Fiala     lldb::ByteOrder byte_order = GetByteOrder();
4322850b1beSTodd Fiala 
433b9c1b51eSKate Stone     if (byte_order != lldb::eByteOrderInvalid) {
4342850b1beSTodd Fiala       if (reg >= m_reg_info.first_st && reg <= m_reg_info.last_st)
435b9c1b51eSKate Stone         reg_value.SetBytes(
436b9c1b51eSKate Stone             m_fpr.xstate.fxsave.stmm[reg - m_reg_info.first_st].bytes,
437b9c1b51eSKate Stone             reg_info->byte_size, byte_order);
4382850b1beSTodd Fiala       if (reg >= m_reg_info.first_mm && reg <= m_reg_info.last_mm)
439b9c1b51eSKate Stone         reg_value.SetBytes(
440b9c1b51eSKate Stone             m_fpr.xstate.fxsave.stmm[reg - m_reg_info.first_mm].bytes,
441b9c1b51eSKate Stone             reg_info->byte_size, byte_order);
4422850b1beSTodd Fiala       if (reg >= m_reg_info.first_xmm && reg <= m_reg_info.last_xmm)
443b9c1b51eSKate Stone         reg_value.SetBytes(
444b9c1b51eSKate Stone             m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_xmm].bytes,
445b9c1b51eSKate Stone             reg_info->byte_size, byte_order);
446b9c1b51eSKate Stone       if (reg >= m_reg_info.first_ymm && reg <= m_reg_info.last_ymm) {
4472850b1beSTodd Fiala         // Concatenate ymm using the register halves in xmm.bytes and ymmh.bytes
4485f957b54SValentina Giusti         if (CopyXSTATEtoYMM(reg, byte_order))
449b9c1b51eSKate Stone           reg_value.SetBytes(m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes,
450b9c1b51eSKate Stone                              reg_info->byte_size, byte_order);
451b9c1b51eSKate Stone         else {
4522850b1beSTodd Fiala           error.SetErrorString("failed to copy ymm register value");
4532850b1beSTodd Fiala           return error;
4542850b1beSTodd Fiala         }
4552850b1beSTodd Fiala       }
456cda0ae46SValentina Giusti       if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
4575f957b54SValentina Giusti         if (CopyXSTATEtoMPX(reg))
458cda0ae46SValentina Giusti           reg_value.SetBytes(m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes,
459cda0ae46SValentina Giusti                              reg_info->byte_size, byte_order);
460cda0ae46SValentina Giusti         else {
461cda0ae46SValentina Giusti           error.SetErrorString("failed to copy mpx register value");
462cda0ae46SValentina Giusti           return error;
463cda0ae46SValentina Giusti         }
464cda0ae46SValentina Giusti       }
465cda0ae46SValentina Giusti       if (reg >= m_reg_info.first_mpxc && reg <= m_reg_info.last_mpxc) {
4665f957b54SValentina Giusti         if (CopyXSTATEtoMPX(reg))
467cda0ae46SValentina Giusti           reg_value.SetBytes(m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes,
468cda0ae46SValentina Giusti                              reg_info->byte_size, byte_order);
469cda0ae46SValentina Giusti         else {
470cda0ae46SValentina Giusti           error.SetErrorString("failed to copy mpx register value");
471cda0ae46SValentina Giusti           return error;
472cda0ae46SValentina Giusti         }
473cda0ae46SValentina Giusti       }
474ee44a92dSDimitar Vlahovski 
4752850b1beSTodd Fiala       if (reg_value.GetType() != RegisterValue::eTypeBytes)
476b9c1b51eSKate Stone         error.SetErrorString(
477b9c1b51eSKate Stone             "write failed - type was expected to be RegisterValue::eTypeBytes");
4782850b1beSTodd Fiala 
4792850b1beSTodd Fiala       return error;
4802850b1beSTodd Fiala     }
4812850b1beSTodd Fiala 
4822850b1beSTodd Fiala     error.SetErrorString("byte order is invalid");
4832850b1beSTodd Fiala     return error;
4842850b1beSTodd Fiala   }
4852850b1beSTodd Fiala 
4862850b1beSTodd Fiala   // Get pointer to m_fpr.xstate.fxsave variable and set the data from it.
4877f658eddSAbhishek Aggarwal 
4887f658eddSAbhishek Aggarwal   // Byte offsets of all registers are calculated wrt 'UserArea' structure.
4897f658eddSAbhishek Aggarwal   // However, ReadFPR() reads fpu registers {using ptrace(PTRACE_GETFPREGS,..)}
490b9c1b51eSKate Stone   // and stores them in 'm_fpr' (of type FPR structure). To extract values of
491b9c1b51eSKate Stone   // fpu
492b9c1b51eSKate Stone   // registers, m_fpr should be read at byte offsets calculated wrt to FPR
493b9c1b51eSKate Stone   // structure.
4947f658eddSAbhishek Aggarwal 
4957f658eddSAbhishek Aggarwal   // Since, FPR structure is also one of the member of UserArea structure.
496b9c1b51eSKate Stone   // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) -
497b9c1b51eSKate Stone   // byte_offset(fctrl wrt UserArea)
4987f658eddSAbhishek Aggarwal   assert((reg_info->byte_offset - m_fctrl_offset_in_userarea) < sizeof(m_fpr));
499b9c1b51eSKate Stone   uint8_t *src =
500b9c1b51eSKate Stone       (uint8_t *)&m_fpr + reg_info->byte_offset - m_fctrl_offset_in_userarea;
501b9c1b51eSKate Stone   switch (reg_info->byte_size) {
502b352a1c8SAbhishek Aggarwal   case 1:
503b352a1c8SAbhishek Aggarwal     reg_value.SetUInt8(*(uint8_t *)src);
504b352a1c8SAbhishek Aggarwal     break;
5052850b1beSTodd Fiala   case 2:
5062850b1beSTodd Fiala     reg_value.SetUInt16(*(uint16_t *)src);
5072850b1beSTodd Fiala     break;
5082850b1beSTodd Fiala   case 4:
5092850b1beSTodd Fiala     reg_value.SetUInt32(*(uint32_t *)src);
5102850b1beSTodd Fiala     break;
5112850b1beSTodd Fiala   case 8:
5122850b1beSTodd Fiala     reg_value.SetUInt64(*(uint64_t *)src);
5132850b1beSTodd Fiala     break;
5142850b1beSTodd Fiala   default:
5152850b1beSTodd Fiala     assert(false && "Unhandled data size.");
516b9c1b51eSKate Stone     error.SetErrorStringWithFormat("unhandled byte size: %" PRIu32,
517b9c1b51eSKate Stone                                    reg_info->byte_size);
5182850b1beSTodd Fiala     break;
5192850b1beSTodd Fiala   }
5202850b1beSTodd Fiala 
5212850b1beSTodd Fiala   return error;
5222850b1beSTodd Fiala }
5232850b1beSTodd Fiala 
524b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::WriteRegister(
525b9c1b51eSKate Stone     const RegisterInfo *reg_info, const RegisterValue &reg_value) {
5262850b1beSTodd Fiala   assert(reg_info && "reg_info is null");
5272850b1beSTodd Fiala 
5282850b1beSTodd Fiala   const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB];
5292850b1beSTodd Fiala   if (reg_index == LLDB_INVALID_REGNUM)
530b9c1b51eSKate Stone     return Error("no lldb regnum for %s", reg_info && reg_info->name
531b9c1b51eSKate Stone                                               ? reg_info->name
532b9c1b51eSKate Stone                                               : "<unknown register>");
5332850b1beSTodd Fiala 
5342850b1beSTodd Fiala   if (IsGPR(reg_index))
535068f8a7eSTamas Berghammer     return WriteRegisterRaw(reg_index, reg_value);
5362850b1beSTodd Fiala 
5375f957b54SValentina Giusti   if (IsFPR(reg_index) || IsAVX(reg_index) || IsMPX(reg_index)) {
538b9c1b51eSKate Stone     if (reg_info->encoding == lldb::eEncodingVector) {
5392850b1beSTodd Fiala       if (reg_index >= m_reg_info.first_st && reg_index <= m_reg_info.last_st)
540b9c1b51eSKate Stone         ::memcpy(
541b9c1b51eSKate Stone             m_fpr.xstate.fxsave.stmm[reg_index - m_reg_info.first_st].bytes,
542b9c1b51eSKate Stone             reg_value.GetBytes(), reg_value.GetByteSize());
5432850b1beSTodd Fiala 
5442850b1beSTodd Fiala       if (reg_index >= m_reg_info.first_mm && reg_index <= m_reg_info.last_mm)
545b9c1b51eSKate Stone         ::memcpy(
546b9c1b51eSKate Stone             m_fpr.xstate.fxsave.stmm[reg_index - m_reg_info.first_mm].bytes,
547b9c1b51eSKate Stone             reg_value.GetBytes(), reg_value.GetByteSize());
5482850b1beSTodd Fiala 
5492850b1beSTodd Fiala       if (reg_index >= m_reg_info.first_xmm && reg_index <= m_reg_info.last_xmm)
550b9c1b51eSKate Stone         ::memcpy(
551b9c1b51eSKate Stone             m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_xmm].bytes,
552b9c1b51eSKate Stone             reg_value.GetBytes(), reg_value.GetByteSize());
5532850b1beSTodd Fiala 
554b9c1b51eSKate Stone       if (reg_index >= m_reg_info.first_ymm &&
555b9c1b51eSKate Stone           reg_index <= m_reg_info.last_ymm) {
556b9c1b51eSKate Stone         // Store ymm register content, and split into the register halves in
557b9c1b51eSKate Stone         // xmm.bytes and ymmh.bytes
558b9c1b51eSKate Stone         ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
559b9c1b51eSKate Stone                  reg_value.GetBytes(), reg_value.GetByteSize());
5602850b1beSTodd Fiala         if (!CopyYMMtoXSTATE(reg_index, GetByteOrder()))
5612850b1beSTodd Fiala           return Error("CopyYMMtoXSTATE() failed");
5622850b1beSTodd Fiala       }
563cda0ae46SValentina Giusti 
564cda0ae46SValentina Giusti       if (reg_index >= m_reg_info.first_mpxr &&
565cda0ae46SValentina Giusti           reg_index <= m_reg_info.last_mpxr) {
566cda0ae46SValentina Giusti         ::memcpy(m_mpx_set.mpxr[reg_index - m_reg_info.first_mpxr].bytes,
567cda0ae46SValentina Giusti                  reg_value.GetBytes(), reg_value.GetByteSize());
568cda0ae46SValentina Giusti         if (!CopyMPXtoXSTATE(reg_index))
569cda0ae46SValentina Giusti           return Error("CopyMPXtoXSTATE() failed");
570cda0ae46SValentina Giusti       }
571cda0ae46SValentina Giusti 
572cda0ae46SValentina Giusti       if (reg_index >= m_reg_info.first_mpxc &&
573cda0ae46SValentina Giusti           reg_index <= m_reg_info.last_mpxc) {
574cda0ae46SValentina Giusti         ::memcpy(m_mpx_set.mpxc[reg_index - m_reg_info.first_mpxc].bytes,
575cda0ae46SValentina Giusti                  reg_value.GetBytes(), reg_value.GetByteSize());
576cda0ae46SValentina Giusti         if (!CopyMPXtoXSTATE(reg_index))
577cda0ae46SValentina Giusti           return Error("CopyMPXtoXSTATE() failed");
578cda0ae46SValentina Giusti       }
579b9c1b51eSKate Stone     } else {
5802850b1beSTodd Fiala       // Get pointer to m_fpr.xstate.fxsave variable and set the data to it.
5817f658eddSAbhishek Aggarwal 
5827f658eddSAbhishek Aggarwal       // Byte offsets of all registers are calculated wrt 'UserArea' structure.
583b9c1b51eSKate Stone       // However, WriteFPR() takes m_fpr (of type FPR structure) and writes only
584b9c1b51eSKate Stone       // fpu
585b9c1b51eSKate Stone       // registers using ptrace(PTRACE_SETFPREGS,..) API. Hence fpu registers
586b9c1b51eSKate Stone       // should
5877f658eddSAbhishek Aggarwal       // be written in m_fpr at byte offsets calculated wrt FPR structure.
5887f658eddSAbhishek Aggarwal 
5897f658eddSAbhishek Aggarwal       // Since, FPR structure is also one of the member of UserArea structure.
590b9c1b51eSKate Stone       // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) -
591b9c1b51eSKate Stone       // byte_offset(fctrl wrt UserArea)
592b9c1b51eSKate Stone       assert((reg_info->byte_offset - m_fctrl_offset_in_userarea) <
593b9c1b51eSKate Stone              sizeof(m_fpr));
594b9c1b51eSKate Stone       uint8_t *dst = (uint8_t *)&m_fpr + reg_info->byte_offset -
595b9c1b51eSKate Stone                      m_fctrl_offset_in_userarea;
596b9c1b51eSKate Stone       switch (reg_info->byte_size) {
597b352a1c8SAbhishek Aggarwal       case 1:
598b352a1c8SAbhishek Aggarwal         *(uint8_t *)dst = reg_value.GetAsUInt8();
599b352a1c8SAbhishek Aggarwal         break;
6002850b1beSTodd Fiala       case 2:
6012850b1beSTodd Fiala         *(uint16_t *)dst = reg_value.GetAsUInt16();
6022850b1beSTodd Fiala         break;
6032850b1beSTodd Fiala       case 4:
6042850b1beSTodd Fiala         *(uint32_t *)dst = reg_value.GetAsUInt32();
6052850b1beSTodd Fiala         break;
6062850b1beSTodd Fiala       case 8:
6072850b1beSTodd Fiala         *(uint64_t *)dst = reg_value.GetAsUInt64();
6082850b1beSTodd Fiala         break;
6092850b1beSTodd Fiala       default:
6102850b1beSTodd Fiala         assert(false && "Unhandled data size.");
611b9c1b51eSKate Stone         return Error("unhandled register data size %" PRIu32,
612b9c1b51eSKate Stone                      reg_info->byte_size);
6132850b1beSTodd Fiala       }
6142850b1beSTodd Fiala     }
6152850b1beSTodd Fiala 
616068f8a7eSTamas Berghammer     Error error = WriteFPR();
617068f8a7eSTamas Berghammer     if (error.Fail())
618068f8a7eSTamas Berghammer       return error;
619068f8a7eSTamas Berghammer 
620b9c1b51eSKate Stone     if (IsAVX(reg_index)) {
6212850b1beSTodd Fiala       if (!CopyYMMtoXSTATE(reg_index, GetByteOrder()))
6222850b1beSTodd Fiala         return Error("CopyYMMtoXSTATE() failed");
6232850b1beSTodd Fiala     }
624cda0ae46SValentina Giusti 
625cda0ae46SValentina Giusti     if (IsMPX(reg_index)) {
626cda0ae46SValentina Giusti       if (!CopyMPXtoXSTATE(reg_index))
627cda0ae46SValentina Giusti         return Error("CopyMPXtoXSTATE() failed");
628cda0ae46SValentina Giusti     }
629*665be50eSMehdi Amini     return Error();
6302850b1beSTodd Fiala   }
631b9c1b51eSKate Stone   return Error("failed - register wasn't recognized to be a GPR or an FPR, "
632b9c1b51eSKate Stone                "write strategy unknown");
6332850b1beSTodd Fiala }
6342850b1beSTodd Fiala 
635b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::ReadAllRegisterValues(
636b9c1b51eSKate Stone     lldb::DataBufferSP &data_sp) {
6372850b1beSTodd Fiala   Error error;
6382850b1beSTodd Fiala 
6392850b1beSTodd Fiala   data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0));
640b9c1b51eSKate Stone   if (!data_sp) {
641b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
642b9c1b51eSKate Stone         "failed to allocate DataBufferHeap instance of size %" PRIu64,
643b9c1b51eSKate Stone         REG_CONTEXT_SIZE);
6442850b1beSTodd Fiala     return error;
6452850b1beSTodd Fiala   }
6462850b1beSTodd Fiala 
647068f8a7eSTamas Berghammer   error = ReadGPR();
648068f8a7eSTamas Berghammer   if (error.Fail())
6492850b1beSTodd Fiala     return error;
6502850b1beSTodd Fiala 
651068f8a7eSTamas Berghammer   error = ReadFPR();
652068f8a7eSTamas Berghammer   if (error.Fail())
6532850b1beSTodd Fiala     return error;
6542850b1beSTodd Fiala 
6552850b1beSTodd Fiala   uint8_t *dst = data_sp->GetBytes();
656b9c1b51eSKate Stone   if (dst == nullptr) {
657b9c1b51eSKate Stone     error.SetErrorStringWithFormat("DataBufferHeap instance of size %" PRIu64
658b9c1b51eSKate Stone                                    " returned a null pointer",
659b9c1b51eSKate Stone                                    REG_CONTEXT_SIZE);
6602850b1beSTodd Fiala     return error;
6612850b1beSTodd Fiala   }
6622850b1beSTodd Fiala 
6632850b1beSTodd Fiala   ::memcpy(dst, &m_gpr_x86_64, GetRegisterInfoInterface().GetGPRSize());
6642850b1beSTodd Fiala   dst += GetRegisterInfoInterface().GetGPRSize();
66558db5bb2SValentina Giusti   if (m_xstate_type == XStateType::FXSAVE)
6662850b1beSTodd Fiala     ::memcpy(dst, &m_fpr.xstate.fxsave, sizeof(m_fpr.xstate.fxsave));
66758db5bb2SValentina Giusti   else if (m_xstate_type == XStateType::XSAVE) {
6682850b1beSTodd Fiala     lldb::ByteOrder byte_order = GetByteOrder();
6692850b1beSTodd Fiala 
67097e57e9bSValentina Giusti     if (IsCPUFeatureAvailable(RegSet::avx)) {
6712850b1beSTodd Fiala       // Assemble the YMM register content from the register halves.
672b9c1b51eSKate Stone       for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm;
673b9c1b51eSKate Stone            ++reg) {
674b9c1b51eSKate Stone         if (!CopyXSTATEtoYMM(reg, byte_order)) {
6755f957b54SValentina Giusti           error.SetErrorStringWithFormat(
6765f957b54SValentina Giusti               "NativeRegisterContextLinux_x86_64::%s "
677b9c1b51eSKate Stone               "CopyXSTATEtoYMM() failed for reg num "
678b9c1b51eSKate Stone               "%" PRIu32,
679b9c1b51eSKate Stone               __FUNCTION__, reg);
6802850b1beSTodd Fiala           return error;
6812850b1beSTodd Fiala         }
6822850b1beSTodd Fiala       }
6835f957b54SValentina Giusti     }
6842850b1beSTodd Fiala 
68597e57e9bSValentina Giusti     if (IsCPUFeatureAvailable(RegSet::mpx)) {
686cda0ae46SValentina Giusti       for (uint32_t reg = m_reg_info.first_mpxr; reg <= m_reg_info.last_mpxc;
687cda0ae46SValentina Giusti            ++reg) {
688cda0ae46SValentina Giusti         if (!CopyXSTATEtoMPX(reg)) {
6895f957b54SValentina Giusti           error.SetErrorStringWithFormat(
6905f957b54SValentina Giusti               "NativeRegisterContextLinux_x86_64::%s "
691cda0ae46SValentina Giusti               "CopyXSTATEtoMPX() failed for reg num "
692cda0ae46SValentina Giusti               "%" PRIu32,
693cda0ae46SValentina Giusti               __FUNCTION__, reg);
694cda0ae46SValentina Giusti           return error;
695cda0ae46SValentina Giusti         }
696cda0ae46SValentina Giusti       }
6975f957b54SValentina Giusti     }
6982850b1beSTodd Fiala     // Copy the extended register state including the assembled ymm registers.
6992850b1beSTodd Fiala     ::memcpy(dst, &m_fpr, sizeof(m_fpr));
700b9c1b51eSKate Stone   } else {
7012850b1beSTodd Fiala     assert(false && "how do we save the floating point registers?");
7022850b1beSTodd Fiala     error.SetErrorString("unsure how to save the floating point registers");
7032850b1beSTodd Fiala   }
7044778e410SRavitheja Addepally   /** The following code is specific to Linux x86 based architectures,
7054778e410SRavitheja Addepally    *  where the register orig_eax (32 bit)/orig_rax (64 bit) is set to
7064778e410SRavitheja Addepally    *  -1 to solve the bug 23659, such a setting prevents the automatic
7074778e410SRavitheja Addepally    *  decrement of the instruction pointer which was causing the SIGILL
7084778e410SRavitheja Addepally    *  exception.
7094778e410SRavitheja Addepally    * **/
7104778e410SRavitheja Addepally 
7114778e410SRavitheja Addepally   RegisterValue value((uint64_t)-1);
712b9c1b51eSKate Stone   const RegisterInfo *reg_info =
713b9c1b51eSKate Stone       GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_eax");
7144778e410SRavitheja Addepally   if (reg_info == nullptr)
7154778e410SRavitheja Addepally     reg_info = GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_rax");
7164778e410SRavitheja Addepally 
71764ad85ceSTamas Berghammer   if (reg_info != nullptr)
7184778e410SRavitheja Addepally     return DoWriteRegisterValue(reg_info->byte_offset, reg_info->name, value);
7192850b1beSTodd Fiala 
7202850b1beSTodd Fiala   return error;
7212850b1beSTodd Fiala }
7222850b1beSTodd Fiala 
723b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::WriteAllRegisterValues(
724b9c1b51eSKate Stone     const lldb::DataBufferSP &data_sp) {
7252850b1beSTodd Fiala   Error error;
7262850b1beSTodd Fiala 
727b9c1b51eSKate Stone   if (!data_sp) {
728b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
729b9c1b51eSKate Stone         "NativeRegisterContextLinux_x86_64::%s invalid data_sp provided",
730b9c1b51eSKate Stone         __FUNCTION__);
7312850b1beSTodd Fiala     return error;
7322850b1beSTodd Fiala   }
7332850b1beSTodd Fiala 
734b9c1b51eSKate Stone   if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) {
735b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
736b9c1b51eSKate Stone         "NativeRegisterContextLinux_x86_64::%s data_sp contained mismatched "
737b9c1b51eSKate Stone         "data size, expected %" PRIu64 ", actual %" PRIu64,
738b9c1b51eSKate Stone         __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize());
7392850b1beSTodd Fiala     return error;
7402850b1beSTodd Fiala   }
7412850b1beSTodd Fiala 
7422850b1beSTodd Fiala   uint8_t *src = data_sp->GetBytes();
743b9c1b51eSKate Stone   if (src == nullptr) {
744b9c1b51eSKate Stone     error.SetErrorStringWithFormat("NativeRegisterContextLinux_x86_64::%s "
745b9c1b51eSKate Stone                                    "DataBuffer::GetBytes() returned a null "
746b9c1b51eSKate Stone                                    "pointer",
747b9c1b51eSKate Stone                                    __FUNCTION__);
7482850b1beSTodd Fiala     return error;
7492850b1beSTodd Fiala   }
7502850b1beSTodd Fiala   ::memcpy(&m_gpr_x86_64, src, GetRegisterInfoInterface().GetGPRSize());
7512850b1beSTodd Fiala 
752068f8a7eSTamas Berghammer   error = WriteGPR();
753068f8a7eSTamas Berghammer   if (error.Fail())
7542850b1beSTodd Fiala     return error;
7552850b1beSTodd Fiala 
7562850b1beSTodd Fiala   src += GetRegisterInfoInterface().GetGPRSize();
75758db5bb2SValentina Giusti   if (m_xstate_type == XStateType::FXSAVE)
7582850b1beSTodd Fiala     ::memcpy(&m_fpr.xstate.fxsave, src, sizeof(m_fpr.xstate.fxsave));
75958db5bb2SValentina Giusti   else if (m_xstate_type == XStateType::XSAVE)
7602850b1beSTodd Fiala     ::memcpy(&m_fpr.xstate.xsave, src, sizeof(m_fpr.xstate.xsave));
7612850b1beSTodd Fiala 
762068f8a7eSTamas Berghammer   error = WriteFPR();
763068f8a7eSTamas Berghammer   if (error.Fail())
7642850b1beSTodd Fiala     return error;
7652850b1beSTodd Fiala 
76658db5bb2SValentina Giusti   if (m_xstate_type == XStateType::XSAVE) {
7672850b1beSTodd Fiala     lldb::ByteOrder byte_order = GetByteOrder();
7682850b1beSTodd Fiala 
76997e57e9bSValentina Giusti     if (IsCPUFeatureAvailable(RegSet::avx)) {
7702850b1beSTodd Fiala       // Parse the YMM register content from the register halves.
771b9c1b51eSKate Stone       for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm;
772b9c1b51eSKate Stone            ++reg) {
773b9c1b51eSKate Stone         if (!CopyYMMtoXSTATE(reg, byte_order)) {
7745f957b54SValentina Giusti           error.SetErrorStringWithFormat(
7755f957b54SValentina Giusti               "NativeRegisterContextLinux_x86_64::%s "
776b9c1b51eSKate Stone               "CopyYMMtoXSTATE() failed for reg num "
777b9c1b51eSKate Stone               "%" PRIu32,
778b9c1b51eSKate Stone               __FUNCTION__, reg);
7792850b1beSTodd Fiala           return error;
7802850b1beSTodd Fiala         }
7812850b1beSTodd Fiala       }
7825f957b54SValentina Giusti     }
783cda0ae46SValentina Giusti 
78497e57e9bSValentina Giusti     if (IsCPUFeatureAvailable(RegSet::mpx)) {
785cda0ae46SValentina Giusti       for (uint32_t reg = m_reg_info.first_mpxr; reg <= m_reg_info.last_mpxc;
786cda0ae46SValentina Giusti            ++reg) {
787cda0ae46SValentina Giusti         if (!CopyMPXtoXSTATE(reg)) {
7885f957b54SValentina Giusti           error.SetErrorStringWithFormat(
7895f957b54SValentina Giusti               "NativeRegisterContextLinux_x86_64::%s "
790cda0ae46SValentina Giusti               "CopyMPXtoXSTATE() failed for reg num "
791cda0ae46SValentina Giusti               "%" PRIu32,
792cda0ae46SValentina Giusti               __FUNCTION__, reg);
793cda0ae46SValentina Giusti           return error;
794cda0ae46SValentina Giusti         }
795cda0ae46SValentina Giusti       }
7962850b1beSTodd Fiala     }
7975f957b54SValentina Giusti   }
7982850b1beSTodd Fiala 
7992850b1beSTodd Fiala   return error;
8002850b1beSTodd Fiala }
8012850b1beSTodd Fiala 
8025f957b54SValentina Giusti bool NativeRegisterContextLinux_x86_64::IsCPUFeatureAvailable(
8035f957b54SValentina Giusti     RegSet feature_code) const {
80458db5bb2SValentina Giusti   if (m_xstate_type == XStateType::Invalid) {
80558db5bb2SValentina Giusti     if (const_cast<NativeRegisterContextLinux_x86_64 *>(this)->ReadFPR().Fail())
8065f957b54SValentina Giusti       return false;
80758db5bb2SValentina Giusti   }
8085f957b54SValentina Giusti   switch (feature_code) {
80958db5bb2SValentina Giusti   case RegSet::gpr:
81058db5bb2SValentina Giusti   case RegSet::fpu:
8115f957b54SValentina Giusti     return true;
81258db5bb2SValentina Giusti   case RegSet::avx: // Check if CPU has AVX and if there is kernel support, by
81358db5bb2SValentina Giusti                     // reading in the XCR0 area of XSAVE.
81458db5bb2SValentina Giusti     if ((m_fpr.xstate.xsave.i387.xcr0 & mask_XSTATE_AVX) == mask_XSTATE_AVX)
8155f957b54SValentina Giusti       return true;
81658db5bb2SValentina Giusti      break;
81758db5bb2SValentina Giusti   case RegSet::mpx: // Check if CPU has MPX and if there is kernel support, by
81858db5bb2SValentina Giusti                     // reading in the XCR0 area of XSAVE.
81958db5bb2SValentina Giusti     if ((m_fpr.xstate.xsave.i387.xcr0 & mask_XSTATE_MPX) == mask_XSTATE_MPX)
82058db5bb2SValentina Giusti       return true;
82158db5bb2SValentina Giusti     break;
8225f957b54SValentina Giusti   }
8235f957b54SValentina Giusti   return false;
8245f957b54SValentina Giusti }
8255f957b54SValentina Giusti 
826b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsRegisterSetAvailable(
827b9c1b51eSKate Stone     uint32_t set_index) const {
8282850b1beSTodd Fiala   uint32_t num_sets = k_num_register_sets - k_num_extended_register_sets;
8292850b1beSTodd Fiala 
83097e57e9bSValentina Giusti   switch (static_cast<RegSet>(set_index)) {
83197e57e9bSValentina Giusti   case RegSet::gpr:
83297e57e9bSValentina Giusti   case RegSet::fpu:
8332850b1beSTodd Fiala     return (set_index < num_sets);
83497e57e9bSValentina Giusti   case RegSet::avx:
83597e57e9bSValentina Giusti     return IsCPUFeatureAvailable(RegSet::avx);
83697e57e9bSValentina Giusti   case RegSet::mpx:
83797e57e9bSValentina Giusti     return IsCPUFeatureAvailable(RegSet::mpx);
8385f957b54SValentina Giusti   }
83958db5bb2SValentina Giusti   return false;
8402850b1beSTodd Fiala }
8412850b1beSTodd Fiala 
842b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsGPR(uint32_t reg_index) const {
8432850b1beSTodd Fiala   // GPRs come first.
8442850b1beSTodd Fiala   return reg_index <= m_reg_info.last_gpr;
8452850b1beSTodd Fiala }
8462850b1beSTodd Fiala 
847b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsFPR(uint32_t reg_index) const {
848b9c1b51eSKate Stone   return (m_reg_info.first_fpr <= reg_index &&
849b9c1b51eSKate Stone           reg_index <= m_reg_info.last_fpr);
8502850b1beSTodd Fiala }
8512850b1beSTodd Fiala 
852b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::WriteFPR() {
85358db5bb2SValentina Giusti   switch (m_xstate_type) {
85497e57e9bSValentina Giusti   case XStateType::FXSAVE:
855b9c1b51eSKate Stone       return WriteRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave),
856b9c1b51eSKate Stone                               NT_PRXFPREG);
85797e57e9bSValentina Giusti   case XStateType::XSAVE:
858b9c1b51eSKate Stone     return WriteRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave),
859b9c1b51eSKate Stone                             NT_X86_XSTATE);
860068f8a7eSTamas Berghammer   default:
86158db5bb2SValentina Giusti     return Error("Unrecognized FPR type.");
862068f8a7eSTamas Berghammer   }
8632850b1beSTodd Fiala }
8642850b1beSTodd Fiala 
865b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsAVX(uint32_t reg_index) const {
86697e57e9bSValentina Giusti   if (!IsCPUFeatureAvailable(RegSet::avx))
8675f957b54SValentina Giusti     return false;
868b9c1b51eSKate Stone   return (m_reg_info.first_ymm <= reg_index &&
869b9c1b51eSKate Stone           reg_index <= m_reg_info.last_ymm);
8702850b1beSTodd Fiala }
8712850b1beSTodd Fiala 
872b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::CopyXSTATEtoYMM(
873b9c1b51eSKate Stone     uint32_t reg_index, lldb::ByteOrder byte_order) {
8742850b1beSTodd Fiala   if (!IsAVX(reg_index))
8752850b1beSTodd Fiala     return false;
8762850b1beSTodd Fiala 
877b9c1b51eSKate Stone   if (byte_order == lldb::eByteOrderLittle) {
8782850b1beSTodd Fiala     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
8792850b1beSTodd Fiala              m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes,
8802850b1beSTodd Fiala              sizeof(XMMReg));
881b9c1b51eSKate Stone     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes +
882b9c1b51eSKate Stone                  sizeof(XMMReg),
8832850b1beSTodd Fiala              m_fpr.xstate.xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes,
8842850b1beSTodd Fiala              sizeof(YMMHReg));
8852850b1beSTodd Fiala     return true;
8862850b1beSTodd Fiala   }
8872850b1beSTodd Fiala 
888b9c1b51eSKate Stone   if (byte_order == lldb::eByteOrderBig) {
889b9c1b51eSKate Stone     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes +
890b9c1b51eSKate Stone                  sizeof(XMMReg),
8912850b1beSTodd Fiala              m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes,
8922850b1beSTodd Fiala              sizeof(XMMReg));
8932850b1beSTodd Fiala     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
8942850b1beSTodd Fiala              m_fpr.xstate.xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes,
8952850b1beSTodd Fiala              sizeof(YMMHReg));
8962850b1beSTodd Fiala     return true;
8972850b1beSTodd Fiala   }
8982850b1beSTodd Fiala   return false; // unsupported or invalid byte order
8992850b1beSTodd Fiala }
9002850b1beSTodd Fiala 
901b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::CopyYMMtoXSTATE(
902b9c1b51eSKate Stone     uint32_t reg, lldb::ByteOrder byte_order) {
9032850b1beSTodd Fiala   if (!IsAVX(reg))
9042850b1beSTodd Fiala     return false;
9052850b1beSTodd Fiala 
906b9c1b51eSKate Stone   if (byte_order == lldb::eByteOrderLittle) {
9072850b1beSTodd Fiala     ::memcpy(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_ymm].bytes,
908b9c1b51eSKate Stone              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, sizeof(XMMReg));
9092850b1beSTodd Fiala     ::memcpy(m_fpr.xstate.xsave.ymmh[reg - m_reg_info.first_ymm].bytes,
9102850b1beSTodd Fiala              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg),
9112850b1beSTodd Fiala              sizeof(YMMHReg));
9122850b1beSTodd Fiala     return true;
9132850b1beSTodd Fiala   }
9142850b1beSTodd Fiala 
915b9c1b51eSKate Stone   if (byte_order == lldb::eByteOrderBig) {
9162850b1beSTodd Fiala     ::memcpy(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_ymm].bytes,
9172850b1beSTodd Fiala              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg),
9182850b1beSTodd Fiala              sizeof(XMMReg));
9192850b1beSTodd Fiala     ::memcpy(m_fpr.xstate.xsave.ymmh[reg - m_reg_info.first_ymm].bytes,
920b9c1b51eSKate Stone              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, sizeof(YMMHReg));
9212850b1beSTodd Fiala     return true;
9222850b1beSTodd Fiala   }
9232850b1beSTodd Fiala   return false; // unsupported or invalid byte order
9242850b1beSTodd Fiala }
9252850b1beSTodd Fiala 
926b9c1b51eSKate Stone void *NativeRegisterContextLinux_x86_64::GetFPRBuffer() {
92758db5bb2SValentina Giusti   switch (m_xstate_type) {
92897e57e9bSValentina Giusti   case XStateType::FXSAVE:
929068f8a7eSTamas Berghammer     return &m_fpr.xstate.fxsave;
93097e57e9bSValentina Giusti   case XStateType::XSAVE:
931068f8a7eSTamas Berghammer     return &m_iovec;
9322850b1beSTodd Fiala   default:
933068f8a7eSTamas Berghammer     return nullptr;
9342850b1beSTodd Fiala   }
9352850b1beSTodd Fiala }
9362850b1beSTodd Fiala 
937b9c1b51eSKate Stone size_t NativeRegisterContextLinux_x86_64::GetFPRSize() {
93858db5bb2SValentina Giusti   switch (m_xstate_type) {
93997e57e9bSValentina Giusti   case XStateType::FXSAVE:
940068f8a7eSTamas Berghammer     return sizeof(m_fpr.xstate.fxsave);
94197e57e9bSValentina Giusti   case XStateType::XSAVE:
942068f8a7eSTamas Berghammer     return sizeof(m_iovec);
943068f8a7eSTamas Berghammer   default:
944068f8a7eSTamas Berghammer     return 0;
945068f8a7eSTamas Berghammer   }
9462850b1beSTodd Fiala }
9472850b1beSTodd Fiala 
948b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::ReadFPR() {
94958db5bb2SValentina Giusti   Error error;
95058db5bb2SValentina Giusti 
95158db5bb2SValentina Giusti   // Probe XSAVE and if it is not supported fall back to FXSAVE.
95258db5bb2SValentina Giusti   if (m_xstate_type != XStateType::FXSAVE) {
95358db5bb2SValentina Giusti     error =
95458db5bb2SValentina Giusti         ReadRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), NT_X86_XSTATE);
95558db5bb2SValentina Giusti     if (!error.Fail()) {
95658db5bb2SValentina Giusti       m_xstate_type = XStateType::XSAVE;
95758db5bb2SValentina Giusti       return error;
958296e063dSAbhishek Aggarwal     }
959068f8a7eSTamas Berghammer   }
96058db5bb2SValentina Giusti   error = ReadRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), NT_PRXFPREG);
96158db5bb2SValentina Giusti   if (!error.Fail()) {
96258db5bb2SValentina Giusti     m_xstate_type = XStateType::FXSAVE;
96358db5bb2SValentina Giusti     return error;
96458db5bb2SValentina Giusti   }
96558db5bb2SValentina Giusti   return Error("Unrecognized FPR type.");
9662850b1beSTodd Fiala }
9672850b1beSTodd Fiala 
968cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::IsMPX(uint32_t reg_index) const {
96997e57e9bSValentina Giusti   if (!IsCPUFeatureAvailable(RegSet::mpx))
9705f957b54SValentina Giusti     return false;
971cda0ae46SValentina Giusti   return (m_reg_info.first_mpxr <= reg_index &&
972cda0ae46SValentina Giusti           reg_index <= m_reg_info.last_mpxc);
973cda0ae46SValentina Giusti }
974cda0ae46SValentina Giusti 
975cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::CopyXSTATEtoMPX(uint32_t reg) {
976cda0ae46SValentina Giusti   if (!IsMPX(reg))
977cda0ae46SValentina Giusti     return false;
978cda0ae46SValentina Giusti 
979cda0ae46SValentina Giusti   if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
980cda0ae46SValentina Giusti     ::memcpy(m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes,
981cda0ae46SValentina Giusti              m_fpr.xstate.xsave.mpxr[reg - m_reg_info.first_mpxr].bytes,
982cda0ae46SValentina Giusti              sizeof(MPXReg));
983cda0ae46SValentina Giusti   } else {
984cda0ae46SValentina Giusti     ::memcpy(m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes,
985cda0ae46SValentina Giusti              m_fpr.xstate.xsave.mpxc[reg - m_reg_info.first_mpxc].bytes,
986cda0ae46SValentina Giusti              sizeof(MPXCsr));
987cda0ae46SValentina Giusti   }
988cda0ae46SValentina Giusti   return true;
989cda0ae46SValentina Giusti }
990cda0ae46SValentina Giusti 
991cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::CopyMPXtoXSTATE(uint32_t reg) {
992cda0ae46SValentina Giusti   if (!IsMPX(reg))
993cda0ae46SValentina Giusti     return false;
994cda0ae46SValentina Giusti 
995cda0ae46SValentina Giusti   if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
996cda0ae46SValentina Giusti     ::memcpy(m_fpr.xstate.xsave.mpxr[reg - m_reg_info.first_mpxr].bytes,
997cda0ae46SValentina Giusti              m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes, sizeof(MPXReg));
998cda0ae46SValentina Giusti   } else {
999cda0ae46SValentina Giusti     ::memcpy(m_fpr.xstate.xsave.mpxc[reg - m_reg_info.first_mpxc].bytes,
1000cda0ae46SValentina Giusti              m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes, sizeof(MPXCsr));
1001cda0ae46SValentina Giusti   }
1002cda0ae46SValentina Giusti   return true;
1003cda0ae46SValentina Giusti }
1004cda0ae46SValentina Giusti 
1005b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::IsWatchpointHit(uint32_t wp_index,
1006b9c1b51eSKate Stone                                                          bool &is_hit) {
100718fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
100818fe6404SChaoren Lin     return Error("Watchpoint index out of range");
100918fe6404SChaoren Lin 
101018fe6404SChaoren Lin   RegisterValue reg_value;
10116a504f6eSChaoren Lin   Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1012b9c1b51eSKate Stone   if (error.Fail()) {
1013c16f5dcaSChaoren Lin     is_hit = false;
1014c16f5dcaSChaoren Lin     return error;
1015c16f5dcaSChaoren Lin   }
101618fe6404SChaoren Lin 
101718fe6404SChaoren Lin   uint64_t status_bits = reg_value.GetAsUInt64();
101818fe6404SChaoren Lin 
1019c16f5dcaSChaoren Lin   is_hit = status_bits & (1 << wp_index);
102018fe6404SChaoren Lin 
102118fe6404SChaoren Lin   return error;
102218fe6404SChaoren Lin }
102318fe6404SChaoren Lin 
1024b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::GetWatchpointHitIndex(
1025b9c1b51eSKate Stone     uint32_t &wp_index, lldb::addr_t trap_addr) {
1026c16f5dcaSChaoren Lin   uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
1027b9c1b51eSKate Stone   for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) {
1028c16f5dcaSChaoren Lin     bool is_hit;
1029c16f5dcaSChaoren Lin     Error error = IsWatchpointHit(wp_index, is_hit);
1030c16f5dcaSChaoren Lin     if (error.Fail()) {
1031c16f5dcaSChaoren Lin       wp_index = LLDB_INVALID_INDEX32;
1032c16f5dcaSChaoren Lin       return error;
1033c16f5dcaSChaoren Lin     } else if (is_hit) {
1034c16f5dcaSChaoren Lin       return error;
1035c16f5dcaSChaoren Lin     }
1036c16f5dcaSChaoren Lin   }
1037c16f5dcaSChaoren Lin   wp_index = LLDB_INVALID_INDEX32;
1038*665be50eSMehdi Amini   return Error();
1039c16f5dcaSChaoren Lin }
1040c16f5dcaSChaoren Lin 
1041b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::IsWatchpointVacant(uint32_t wp_index,
1042b9c1b51eSKate Stone                                                             bool &is_vacant) {
104318fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
104418fe6404SChaoren Lin     return Error("Watchpoint index out of range");
104518fe6404SChaoren Lin 
104618fe6404SChaoren Lin   RegisterValue reg_value;
10476a504f6eSChaoren Lin   Error error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1048b9c1b51eSKate Stone   if (error.Fail()) {
1049c16f5dcaSChaoren Lin     is_vacant = false;
1050c16f5dcaSChaoren Lin     return error;
1051c16f5dcaSChaoren Lin   }
105218fe6404SChaoren Lin 
105318fe6404SChaoren Lin   uint64_t control_bits = reg_value.GetAsUInt64();
105418fe6404SChaoren Lin 
1055c16f5dcaSChaoren Lin   is_vacant = !(control_bits & (1 << (2 * wp_index)));
105618fe6404SChaoren Lin 
105718fe6404SChaoren Lin   return error;
105818fe6404SChaoren Lin }
105918fe6404SChaoren Lin 
1060b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::SetHardwareWatchpointWithIndex(
106118fe6404SChaoren Lin     lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index) {
106218fe6404SChaoren Lin 
106318fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
106418fe6404SChaoren Lin     return Error("Watchpoint index out of range");
106518fe6404SChaoren Lin 
1066b9c1b51eSKate Stone   // Read only watchpoints aren't supported on x86_64. Fall back to read/write
1067b9c1b51eSKate Stone   // waitchpoints instead.
1068b9c1b51eSKate Stone   // TODO: Add logic to detect when a write happens and ignore that watchpoint
1069b9c1b51eSKate Stone   // hit.
1070cf8eb9daSOmair Javaid   if (watch_flags == 0x2)
107140caea63STamas Berghammer     watch_flags = 0x3;
1072cf8eb9daSOmair Javaid 
107318fe6404SChaoren Lin   if (watch_flags != 0x1 && watch_flags != 0x3)
107418fe6404SChaoren Lin     return Error("Invalid read/write bits for watchpoint");
107518fe6404SChaoren Lin 
107618fe6404SChaoren Lin   if (size != 1 && size != 2 && size != 4 && size != 8)
107718fe6404SChaoren Lin     return Error("Invalid size for watchpoint");
107818fe6404SChaoren Lin 
1079c16f5dcaSChaoren Lin   bool is_vacant;
1080c16f5dcaSChaoren Lin   Error error = IsWatchpointVacant(wp_index, is_vacant);
1081b9c1b51eSKate Stone   if (error.Fail())
1082b9c1b51eSKate Stone     return error;
1083b9c1b51eSKate Stone   if (!is_vacant)
1084b9c1b51eSKate Stone     return Error("Watchpoint index not vacant");
108518fe6404SChaoren Lin 
108618fe6404SChaoren Lin   RegisterValue reg_value;
10876a504f6eSChaoren Lin   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1088b9c1b51eSKate Stone   if (error.Fail())
1089b9c1b51eSKate Stone     return error;
109018fe6404SChaoren Lin 
109118fe6404SChaoren Lin   // for watchpoints 0, 1, 2, or 3, respectively,
109218fe6404SChaoren Lin   // set bits 1, 3, 5, or 7
109318fe6404SChaoren Lin   uint64_t enable_bit = 1 << (2 * wp_index);
109418fe6404SChaoren Lin 
109518fe6404SChaoren Lin   // set bits 16-17, 20-21, 24-25, or 28-29
109618fe6404SChaoren Lin   // with 0b01 for write, and 0b11 for read/write
109718fe6404SChaoren Lin   uint64_t rw_bits = watch_flags << (16 + 4 * wp_index);
109818fe6404SChaoren Lin 
109918fe6404SChaoren Lin   // set bits 18-19, 22-23, 26-27, or 30-31
110018fe6404SChaoren Lin   // with 0b00, 0b01, 0b10, or 0b11
110118fe6404SChaoren Lin   // for 1, 2, 8 (if supported), or 4 bytes, respectively
110218fe6404SChaoren Lin   uint64_t size_bits = (size == 8 ? 0x2 : size - 1) << (18 + 4 * wp_index);
110318fe6404SChaoren Lin 
110418fe6404SChaoren Lin   uint64_t bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index));
110518fe6404SChaoren Lin 
110618fe6404SChaoren Lin   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
110718fe6404SChaoren Lin 
110818fe6404SChaoren Lin   control_bits |= enable_bit | rw_bits | size_bits;
110918fe6404SChaoren Lin 
1110068f8a7eSTamas Berghammer   error = WriteRegisterRaw(m_reg_info.first_dr + wp_index, RegisterValue(addr));
1111b9c1b51eSKate Stone   if (error.Fail())
1112b9c1b51eSKate Stone     return error;
111318fe6404SChaoren Lin 
1114b9c1b51eSKate Stone   error =
1115b9c1b51eSKate Stone       WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits));
1116b9c1b51eSKate Stone   if (error.Fail())
1117b9c1b51eSKate Stone     return error;
111818fe6404SChaoren Lin 
111918fe6404SChaoren Lin   error.Clear();
112018fe6404SChaoren Lin   return error;
112118fe6404SChaoren Lin }
112218fe6404SChaoren Lin 
1123b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::ClearHardwareWatchpoint(
1124b9c1b51eSKate Stone     uint32_t wp_index) {
112518fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
112618fe6404SChaoren Lin     return false;
112718fe6404SChaoren Lin 
112818fe6404SChaoren Lin   RegisterValue reg_value;
112918fe6404SChaoren Lin 
113018fe6404SChaoren Lin   // for watchpoints 0, 1, 2, or 3, respectively,
113118fe6404SChaoren Lin   // clear bits 0, 1, 2, or 3 of the debug status register (DR6)
11326a504f6eSChaoren Lin   Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1133b9c1b51eSKate Stone   if (error.Fail())
1134b9c1b51eSKate Stone     return false;
113518fe6404SChaoren Lin   uint64_t bit_mask = 1 << wp_index;
113618fe6404SChaoren Lin   uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask;
1137068f8a7eSTamas Berghammer   error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits));
1138b9c1b51eSKate Stone   if (error.Fail())
1139b9c1b51eSKate Stone     return false;
114018fe6404SChaoren Lin 
114118fe6404SChaoren Lin   // for watchpoints 0, 1, 2, or 3, respectively,
114218fe6404SChaoren Lin   // clear bits {0-1,16-19}, {2-3,20-23}, {4-5,24-27}, or {6-7,28-31}
114318fe6404SChaoren Lin   // of the debug control register (DR7)
11446a504f6eSChaoren Lin   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1145b9c1b51eSKate Stone   if (error.Fail())
1146b9c1b51eSKate Stone     return false;
114718fe6404SChaoren Lin   bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index));
114818fe6404SChaoren Lin   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
1149b9c1b51eSKate Stone   return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits))
1150b9c1b51eSKate Stone       .Success();
115118fe6404SChaoren Lin }
115218fe6404SChaoren Lin 
1153b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::ClearAllHardwareWatchpoints() {
115418fe6404SChaoren Lin   RegisterValue reg_value;
115518fe6404SChaoren Lin 
115618fe6404SChaoren Lin   // clear bits {0-4} of the debug status register (DR6)
11576a504f6eSChaoren Lin   Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1158b9c1b51eSKate Stone   if (error.Fail())
1159b9c1b51eSKate Stone     return error;
116018fe6404SChaoren Lin   uint64_t bit_mask = 0xF;
116118fe6404SChaoren Lin   uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask;
1162068f8a7eSTamas Berghammer   error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits));
1163b9c1b51eSKate Stone   if (error.Fail())
1164b9c1b51eSKate Stone     return error;
116518fe6404SChaoren Lin 
116618fe6404SChaoren Lin   // clear bits {0-7,16-31} of the debug control register (DR7)
11676a504f6eSChaoren Lin   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1168b9c1b51eSKate Stone   if (error.Fail())
1169b9c1b51eSKate Stone     return error;
117018fe6404SChaoren Lin   bit_mask = 0xFF | (0xFFFF << 16);
117118fe6404SChaoren Lin   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
1172068f8a7eSTamas Berghammer   return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits));
117318fe6404SChaoren Lin }
117418fe6404SChaoren Lin 
1175b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::SetHardwareWatchpoint(
1176b9c1b51eSKate Stone     lldb::addr_t addr, size_t size, uint32_t watch_flags) {
1177c16f5dcaSChaoren Lin   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
117818fe6404SChaoren Lin   const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints();
1179b9c1b51eSKate Stone   for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index) {
1180c16f5dcaSChaoren Lin     bool is_vacant;
1181c16f5dcaSChaoren Lin     Error error = IsWatchpointVacant(wp_index, is_vacant);
1182b9c1b51eSKate Stone     if (is_vacant) {
1183c16f5dcaSChaoren Lin       error = SetHardwareWatchpointWithIndex(addr, size, watch_flags, wp_index);
1184c16f5dcaSChaoren Lin       if (error.Success())
118518fe6404SChaoren Lin         return wp_index;
118618fe6404SChaoren Lin     }
1187b9c1b51eSKate Stone     if (error.Fail() && log) {
1188c16f5dcaSChaoren Lin       log->Printf("NativeRegisterContextLinux_x86_64::%s Error: %s",
1189c16f5dcaSChaoren Lin                   __FUNCTION__, error.AsCString());
1190c16f5dcaSChaoren Lin     }
1191c16f5dcaSChaoren Lin   }
119218fe6404SChaoren Lin   return LLDB_INVALID_INDEX32;
119318fe6404SChaoren Lin }
119418fe6404SChaoren Lin 
119518fe6404SChaoren Lin lldb::addr_t
1196b9c1b51eSKate Stone NativeRegisterContextLinux_x86_64::GetWatchpointAddress(uint32_t wp_index) {
119718fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
119818fe6404SChaoren Lin     return LLDB_INVALID_ADDRESS;
119918fe6404SChaoren Lin   RegisterValue reg_value;
120018fe6404SChaoren Lin   if (ReadRegisterRaw(m_reg_info.first_dr + wp_index, reg_value).Fail())
120118fe6404SChaoren Lin     return LLDB_INVALID_ADDRESS;
120218fe6404SChaoren Lin   return reg_value.GetAsUInt64();
120318fe6404SChaoren Lin }
120418fe6404SChaoren Lin 
1205b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::NumSupportedHardwareWatchpoints() {
120618fe6404SChaoren Lin   // Available debug address registers: dr0, dr1, dr2, dr3
120718fe6404SChaoren Lin   return 4;
120818fe6404SChaoren Lin }
1209068f8a7eSTamas Berghammer 
1210068f8a7eSTamas Berghammer #endif // defined(__i386__) || defined(__x86_64__)
1211