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/RegisterValue.h"
15068f8a7eSTamas Berghammer #include "lldb/Host/HostInfo.h"
16666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
176f9e6901SZachary Turner #include "lldb/Utility/Log.h"
1897206d57SZachary Turner #include "lldb/Utility/Status.h"
19068f8a7eSTamas Berghammer 
20068f8a7eSTamas Berghammer #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
21068f8a7eSTamas Berghammer #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
222850b1beSTodd Fiala 
236ec13991SPavel Labath #include <linux/elf.h>
246ec13991SPavel Labath 
252850b1beSTodd Fiala using namespace lldb_private;
26db264a6dSTamas Berghammer using namespace lldb_private::process_linux;
272850b1beSTodd Fiala 
282850b1beSTodd Fiala // ----------------------------------------------------------------------------
292850b1beSTodd Fiala // Private namespace.
302850b1beSTodd Fiala // ----------------------------------------------------------------------------
312850b1beSTodd Fiala 
32b9c1b51eSKate Stone namespace {
332850b1beSTodd Fiala // x86 32-bit general purpose registers.
34b9c1b51eSKate Stone const uint32_t g_gpr_regnums_i386[] = {
35b9c1b51eSKate Stone     lldb_eax_i386,      lldb_ebx_i386,    lldb_ecx_i386, lldb_edx_i386,
36b9c1b51eSKate Stone     lldb_edi_i386,      lldb_esi_i386,    lldb_ebp_i386, lldb_esp_i386,
37b9c1b51eSKate Stone     lldb_eip_i386,      lldb_eflags_i386, lldb_cs_i386,  lldb_fs_i386,
38b9c1b51eSKate Stone     lldb_gs_i386,       lldb_ss_i386,     lldb_ds_i386,  lldb_es_i386,
39b9c1b51eSKate Stone     lldb_ax_i386,       lldb_bx_i386,     lldb_cx_i386,  lldb_dx_i386,
40b9c1b51eSKate Stone     lldb_di_i386,       lldb_si_i386,     lldb_bp_i386,  lldb_sp_i386,
41b9c1b51eSKate Stone     lldb_ah_i386,       lldb_bh_i386,     lldb_ch_i386,  lldb_dh_i386,
42b9c1b51eSKate Stone     lldb_al_i386,       lldb_bl_i386,     lldb_cl_i386,  lldb_dl_i386,
432850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
442850b1beSTodd Fiala };
45b9c1b51eSKate Stone static_assert((sizeof(g_gpr_regnums_i386) / sizeof(g_gpr_regnums_i386[0])) -
46b9c1b51eSKate Stone                       1 ==
47b9c1b51eSKate Stone                   k_num_gpr_registers_i386,
482850b1beSTodd Fiala               "g_gpr_regnums_i386 has wrong number of register infos");
492850b1beSTodd Fiala 
502850b1beSTodd Fiala // x86 32-bit floating point registers.
51b9c1b51eSKate Stone const uint32_t g_fpu_regnums_i386[] = {
52b9c1b51eSKate Stone     lldb_fctrl_i386,    lldb_fstat_i386,     lldb_ftag_i386,  lldb_fop_i386,
53b9c1b51eSKate Stone     lldb_fiseg_i386,    lldb_fioff_i386,     lldb_foseg_i386, lldb_fooff_i386,
54b9c1b51eSKate Stone     lldb_mxcsr_i386,    lldb_mxcsrmask_i386, lldb_st0_i386,   lldb_st1_i386,
55b9c1b51eSKate Stone     lldb_st2_i386,      lldb_st3_i386,       lldb_st4_i386,   lldb_st5_i386,
56b9c1b51eSKate Stone     lldb_st6_i386,      lldb_st7_i386,       lldb_mm0_i386,   lldb_mm1_i386,
57b9c1b51eSKate Stone     lldb_mm2_i386,      lldb_mm3_i386,       lldb_mm4_i386,   lldb_mm5_i386,
58b9c1b51eSKate Stone     lldb_mm6_i386,      lldb_mm7_i386,       lldb_xmm0_i386,  lldb_xmm1_i386,
59b9c1b51eSKate Stone     lldb_xmm2_i386,     lldb_xmm3_i386,      lldb_xmm4_i386,  lldb_xmm5_i386,
60b9c1b51eSKate Stone     lldb_xmm6_i386,     lldb_xmm7_i386,
612850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
622850b1beSTodd Fiala };
63b9c1b51eSKate Stone static_assert((sizeof(g_fpu_regnums_i386) / sizeof(g_fpu_regnums_i386[0])) -
64b9c1b51eSKate Stone                       1 ==
65b9c1b51eSKate Stone                   k_num_fpr_registers_i386,
662850b1beSTodd Fiala               "g_fpu_regnums_i386 has wrong number of register infos");
672850b1beSTodd Fiala 
682850b1beSTodd Fiala // x86 32-bit AVX registers.
69b9c1b51eSKate Stone const uint32_t g_avx_regnums_i386[] = {
70b9c1b51eSKate Stone     lldb_ymm0_i386,     lldb_ymm1_i386, lldb_ymm2_i386, lldb_ymm3_i386,
71b9c1b51eSKate Stone     lldb_ymm4_i386,     lldb_ymm5_i386, lldb_ymm6_i386, lldb_ymm7_i386,
722850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
732850b1beSTodd Fiala };
74b9c1b51eSKate Stone static_assert((sizeof(g_avx_regnums_i386) / sizeof(g_avx_regnums_i386[0])) -
75b9c1b51eSKate Stone                       1 ==
76b9c1b51eSKate Stone                   k_num_avx_registers_i386,
772850b1beSTodd Fiala               " g_avx_regnums_i386 has wrong number of register infos");
782850b1beSTodd Fiala 
79cda0ae46SValentina Giusti // x64 32-bit MPX registers.
80cda0ae46SValentina Giusti static const uint32_t g_mpx_regnums_i386[] = {
81cda0ae46SValentina Giusti     lldb_bnd0_i386,     lldb_bnd1_i386, lldb_bnd2_i386, lldb_bnd3_i386,
82cda0ae46SValentina Giusti     lldb_bndcfgu_i386,  lldb_bndstatus_i386,
83cda0ae46SValentina Giusti     LLDB_INVALID_REGNUM // register sets need to end with this flag
84cda0ae46SValentina Giusti };
85cda0ae46SValentina Giusti static_assert((sizeof(g_mpx_regnums_i386) / sizeof(g_mpx_regnums_i386[0])) -
86cda0ae46SValentina Giusti                       1 ==
87cda0ae46SValentina Giusti                   k_num_mpx_registers_i386,
88cda0ae46SValentina Giusti               "g_mpx_regnums_x86_64 has wrong number of register infos");
89cda0ae46SValentina Giusti 
902850b1beSTodd Fiala // x86 64-bit general purpose registers.
91b9c1b51eSKate Stone static const uint32_t g_gpr_regnums_x86_64[] = {
92b9c1b51eSKate Stone     lldb_rax_x86_64,    lldb_rbx_x86_64,    lldb_rcx_x86_64, lldb_rdx_x86_64,
93b9c1b51eSKate Stone     lldb_rdi_x86_64,    lldb_rsi_x86_64,    lldb_rbp_x86_64, lldb_rsp_x86_64,
94b9c1b51eSKate Stone     lldb_r8_x86_64,     lldb_r9_x86_64,     lldb_r10_x86_64, lldb_r11_x86_64,
95b9c1b51eSKate Stone     lldb_r12_x86_64,    lldb_r13_x86_64,    lldb_r14_x86_64, lldb_r15_x86_64,
96b9c1b51eSKate Stone     lldb_rip_x86_64,    lldb_rflags_x86_64, lldb_cs_x86_64,  lldb_fs_x86_64,
97b9c1b51eSKate Stone     lldb_gs_x86_64,     lldb_ss_x86_64,     lldb_ds_x86_64,  lldb_es_x86_64,
98b9c1b51eSKate Stone     lldb_eax_x86_64,    lldb_ebx_x86_64,    lldb_ecx_x86_64, lldb_edx_x86_64,
99b9c1b51eSKate Stone     lldb_edi_x86_64,    lldb_esi_x86_64,    lldb_ebp_x86_64, lldb_esp_x86_64,
1007f013bcdSZachary Turner     lldb_r8d_x86_64,  // Low 32 bits or r8
1017f013bcdSZachary Turner     lldb_r9d_x86_64,  // Low 32 bits or r9
1027f013bcdSZachary Turner     lldb_r10d_x86_64, // Low 32 bits or r10
1037f013bcdSZachary Turner     lldb_r11d_x86_64, // Low 32 bits or r11
1047f013bcdSZachary Turner     lldb_r12d_x86_64, // Low 32 bits or r12
1057f013bcdSZachary Turner     lldb_r13d_x86_64, // Low 32 bits or r13
1067f013bcdSZachary Turner     lldb_r14d_x86_64, // Low 32 bits or r14
1077f013bcdSZachary Turner     lldb_r15d_x86_64, // Low 32 bits or r15
108b9c1b51eSKate Stone     lldb_ax_x86_64,     lldb_bx_x86_64,     lldb_cx_x86_64,  lldb_dx_x86_64,
109b9c1b51eSKate Stone     lldb_di_x86_64,     lldb_si_x86_64,     lldb_bp_x86_64,  lldb_sp_x86_64,
1107f013bcdSZachary Turner     lldb_r8w_x86_64,  // Low 16 bits or r8
1117f013bcdSZachary Turner     lldb_r9w_x86_64,  // Low 16 bits or r9
1127f013bcdSZachary Turner     lldb_r10w_x86_64, // Low 16 bits or r10
1137f013bcdSZachary Turner     lldb_r11w_x86_64, // Low 16 bits or r11
1147f013bcdSZachary Turner     lldb_r12w_x86_64, // Low 16 bits or r12
1157f013bcdSZachary Turner     lldb_r13w_x86_64, // Low 16 bits or r13
1167f013bcdSZachary Turner     lldb_r14w_x86_64, // Low 16 bits or r14
1177f013bcdSZachary Turner     lldb_r15w_x86_64, // Low 16 bits or r15
118b9c1b51eSKate Stone     lldb_ah_x86_64,     lldb_bh_x86_64,     lldb_ch_x86_64,  lldb_dh_x86_64,
119b9c1b51eSKate Stone     lldb_al_x86_64,     lldb_bl_x86_64,     lldb_cl_x86_64,  lldb_dl_x86_64,
120b9c1b51eSKate Stone     lldb_dil_x86_64,    lldb_sil_x86_64,    lldb_bpl_x86_64, lldb_spl_x86_64,
1217f013bcdSZachary Turner     lldb_r8l_x86_64,    // Low 8 bits or r8
1227f013bcdSZachary Turner     lldb_r9l_x86_64,    // Low 8 bits or r9
1237f013bcdSZachary Turner     lldb_r10l_x86_64,   // Low 8 bits or r10
1247f013bcdSZachary Turner     lldb_r11l_x86_64,   // Low 8 bits or r11
1257f013bcdSZachary Turner     lldb_r12l_x86_64,   // Low 8 bits or r12
1267f013bcdSZachary Turner     lldb_r13l_x86_64,   // Low 8 bits or r13
1277f013bcdSZachary Turner     lldb_r14l_x86_64,   // Low 8 bits or r14
1287f013bcdSZachary Turner     lldb_r15l_x86_64,   // Low 8 bits or r15
1292850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
1302850b1beSTodd Fiala };
131b9c1b51eSKate Stone static_assert((sizeof(g_gpr_regnums_x86_64) / sizeof(g_gpr_regnums_x86_64[0])) -
132b9c1b51eSKate Stone                       1 ==
133b9c1b51eSKate Stone                   k_num_gpr_registers_x86_64,
1342850b1beSTodd Fiala               "g_gpr_regnums_x86_64 has wrong number of register infos");
1352850b1beSTodd Fiala 
1362850b1beSTodd Fiala // x86 64-bit floating point registers.
137b9c1b51eSKate Stone static const uint32_t g_fpu_regnums_x86_64[] = {
138b9c1b51eSKate Stone     lldb_fctrl_x86_64,     lldb_fstat_x86_64, lldb_ftag_x86_64,
139b9c1b51eSKate Stone     lldb_fop_x86_64,       lldb_fiseg_x86_64, lldb_fioff_x86_64,
140b9c1b51eSKate Stone     lldb_foseg_x86_64,     lldb_fooff_x86_64, lldb_mxcsr_x86_64,
141b9c1b51eSKate Stone     lldb_mxcsrmask_x86_64, lldb_st0_x86_64,   lldb_st1_x86_64,
142b9c1b51eSKate Stone     lldb_st2_x86_64,       lldb_st3_x86_64,   lldb_st4_x86_64,
143b9c1b51eSKate Stone     lldb_st5_x86_64,       lldb_st6_x86_64,   lldb_st7_x86_64,
144b9c1b51eSKate Stone     lldb_mm0_x86_64,       lldb_mm1_x86_64,   lldb_mm2_x86_64,
145b9c1b51eSKate Stone     lldb_mm3_x86_64,       lldb_mm4_x86_64,   lldb_mm5_x86_64,
146b9c1b51eSKate Stone     lldb_mm6_x86_64,       lldb_mm7_x86_64,   lldb_xmm0_x86_64,
147b9c1b51eSKate Stone     lldb_xmm1_x86_64,      lldb_xmm2_x86_64,  lldb_xmm3_x86_64,
148b9c1b51eSKate Stone     lldb_xmm4_x86_64,      lldb_xmm5_x86_64,  lldb_xmm6_x86_64,
149b9c1b51eSKate Stone     lldb_xmm7_x86_64,      lldb_xmm8_x86_64,  lldb_xmm9_x86_64,
150b9c1b51eSKate Stone     lldb_xmm10_x86_64,     lldb_xmm11_x86_64, lldb_xmm12_x86_64,
151b9c1b51eSKate Stone     lldb_xmm13_x86_64,     lldb_xmm14_x86_64, lldb_xmm15_x86_64,
1522850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
1532850b1beSTodd Fiala };
154b9c1b51eSKate Stone static_assert((sizeof(g_fpu_regnums_x86_64) / sizeof(g_fpu_regnums_x86_64[0])) -
155b9c1b51eSKate Stone                       1 ==
156b9c1b51eSKate Stone                   k_num_fpr_registers_x86_64,
1572850b1beSTodd Fiala               "g_fpu_regnums_x86_64 has wrong number of register infos");
1582850b1beSTodd Fiala 
1592850b1beSTodd Fiala // x86 64-bit AVX registers.
160b9c1b51eSKate Stone static const uint32_t g_avx_regnums_x86_64[] = {
161b9c1b51eSKate Stone     lldb_ymm0_x86_64,   lldb_ymm1_x86_64,  lldb_ymm2_x86_64,  lldb_ymm3_x86_64,
162b9c1b51eSKate Stone     lldb_ymm4_x86_64,   lldb_ymm5_x86_64,  lldb_ymm6_x86_64,  lldb_ymm7_x86_64,
163b9c1b51eSKate Stone     lldb_ymm8_x86_64,   lldb_ymm9_x86_64,  lldb_ymm10_x86_64, lldb_ymm11_x86_64,
164b9c1b51eSKate Stone     lldb_ymm12_x86_64,  lldb_ymm13_x86_64, lldb_ymm14_x86_64, lldb_ymm15_x86_64,
1652850b1beSTodd Fiala     LLDB_INVALID_REGNUM // register sets need to end with this flag
1662850b1beSTodd Fiala };
167b9c1b51eSKate Stone static_assert((sizeof(g_avx_regnums_x86_64) / sizeof(g_avx_regnums_x86_64[0])) -
168b9c1b51eSKate Stone                       1 ==
169b9c1b51eSKate Stone                   k_num_avx_registers_x86_64,
1702850b1beSTodd Fiala               "g_avx_regnums_x86_64 has wrong number of register infos");
1712850b1beSTodd Fiala 
172cda0ae46SValentina Giusti // x86 64-bit MPX registers.
173cda0ae46SValentina Giusti static const uint32_t g_mpx_regnums_x86_64[] = {
174cda0ae46SValentina Giusti     lldb_bnd0_x86_64,    lldb_bnd1_x86_64,    lldb_bnd2_x86_64,
175cda0ae46SValentina Giusti     lldb_bnd3_x86_64,    lldb_bndcfgu_x86_64, lldb_bndstatus_x86_64,
176cda0ae46SValentina Giusti     LLDB_INVALID_REGNUM // register sets need to end with this flag
177cda0ae46SValentina Giusti };
178cda0ae46SValentina Giusti static_assert((sizeof(g_mpx_regnums_x86_64) / sizeof(g_mpx_regnums_x86_64[0])) -
179cda0ae46SValentina Giusti                       1 ==
180cda0ae46SValentina Giusti                   k_num_mpx_registers_x86_64,
181cda0ae46SValentina Giusti               "g_mpx_regnums_x86_64 has wrong number of register infos");
182cda0ae46SValentina Giusti 
1832850b1beSTodd Fiala // Number of register sets provided by this context.
184cda0ae46SValentina Giusti enum { k_num_extended_register_sets = 2, k_num_register_sets = 4 };
1852850b1beSTodd Fiala 
1862850b1beSTodd Fiala // Register sets for x86 32-bit.
187b9c1b51eSKate Stone static const RegisterSet g_reg_sets_i386[k_num_register_sets] = {
188b9c1b51eSKate Stone     {"General Purpose Registers", "gpr", k_num_gpr_registers_i386,
189b9c1b51eSKate Stone      g_gpr_regnums_i386},
190b9c1b51eSKate Stone     {"Floating Point Registers", "fpu", k_num_fpr_registers_i386,
191b9c1b51eSKate Stone      g_fpu_regnums_i386},
192b9c1b51eSKate Stone     {"Advanced Vector Extensions", "avx", k_num_avx_registers_i386,
193cda0ae46SValentina Giusti      g_avx_regnums_i386},
194cda0ae46SValentina Giusti     { "Memory Protection Extensions", "mpx", k_num_mpx_registers_i386,
195cda0ae46SValentina Giusti      g_mpx_regnums_i386}};
1962850b1beSTodd Fiala 
1972850b1beSTodd Fiala // Register sets for x86 64-bit.
198b9c1b51eSKate Stone static const RegisterSet g_reg_sets_x86_64[k_num_register_sets] = {
199b9c1b51eSKate Stone     {"General Purpose Registers", "gpr", k_num_gpr_registers_x86_64,
200b9c1b51eSKate Stone      g_gpr_regnums_x86_64},
201b9c1b51eSKate Stone     {"Floating Point Registers", "fpu", k_num_fpr_registers_x86_64,
202b9c1b51eSKate Stone      g_fpu_regnums_x86_64},
203b9c1b51eSKate Stone     {"Advanced Vector Extensions", "avx", k_num_avx_registers_x86_64,
204cda0ae46SValentina Giusti      g_avx_regnums_x86_64},
205cda0ae46SValentina Giusti     { "Memory Protection Extensions", "mpx", k_num_mpx_registers_x86_64,
206cda0ae46SValentina Giusti      g_mpx_regnums_x86_64}};
2072850b1beSTodd Fiala }
2082850b1beSTodd Fiala 
2092850b1beSTodd Fiala #define REG_CONTEXT_SIZE (GetRegisterInfoInterface().GetGPRSize() + sizeof(FPR))
2102850b1beSTodd Fiala 
2112850b1beSTodd Fiala // ----------------------------------------------------------------------------
2122850b1beSTodd Fiala // Required ptrace defines.
2132850b1beSTodd Fiala // ----------------------------------------------------------------------------
2142850b1beSTodd Fiala 
2152850b1beSTodd Fiala // Support ptrace extensions even when compiled without required kernel support
2162850b1beSTodd Fiala #ifndef NT_X86_XSTATE
2172850b1beSTodd Fiala #define NT_X86_XSTATE 0x202
2182850b1beSTodd Fiala #endif
219296e063dSAbhishek Aggarwal #ifndef NT_PRXFPREG
220296e063dSAbhishek Aggarwal #define NT_PRXFPREG 0x46e62b7f
221296e063dSAbhishek Aggarwal #endif
2222850b1beSTodd Fiala 
2236ec13991SPavel Labath // On x86_64 NT_PRFPREG is used to access the FXSAVE area. On i386, we need to
2246ec13991SPavel Labath // use NT_PRXFPREG.
2256ec13991SPavel Labath static inline unsigned int fxsr_regset(const ArchSpec &arch) {
2266ec13991SPavel Labath   return arch.GetAddressByteSize() == 8 ? NT_PRFPREG : NT_PRXFPREG;
2276ec13991SPavel Labath }
2286ec13991SPavel Labath 
2295f957b54SValentina Giusti // ----------------------------------------------------------------------------
2305f957b54SValentina Giusti // Required MPX define.
2315f957b54SValentina Giusti // ----------------------------------------------------------------------------
2325f957b54SValentina Giusti 
2335f957b54SValentina Giusti // Support MPX extensions also if compiled with compiler without MPX support.
2345f957b54SValentina Giusti #ifndef bit_MPX
2355f957b54SValentina Giusti #define bit_MPX 0x4000
2365f957b54SValentina Giusti #endif
2375f957b54SValentina Giusti 
2385f957b54SValentina Giusti // ----------------------------------------------------------------------------
2395f957b54SValentina Giusti // XCR0 extended register sets masks.
2405f957b54SValentina Giusti // ----------------------------------------------------------------------------
2415f957b54SValentina Giusti #define mask_XSTATE_AVX (1ULL << 2)
2425f957b54SValentina Giusti #define mask_XSTATE_BNDREGS (1ULL << 3)
2435f957b54SValentina Giusti #define mask_XSTATE_BNDCFG (1ULL << 4)
2445f957b54SValentina Giusti #define mask_XSTATE_MPX (mask_XSTATE_BNDREGS | mask_XSTATE_BNDCFG)
2455f957b54SValentina Giusti 
246068f8a7eSTamas Berghammer NativeRegisterContextLinux *
247b9c1b51eSKate Stone NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(
248b9c1b51eSKate Stone     const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
249b9c1b51eSKate Stone     uint32_t concrete_frame_idx) {
250b9c1b51eSKate Stone   return new NativeRegisterContextLinux_x86_64(target_arch, native_thread,
251b9c1b51eSKate Stone                                                concrete_frame_idx);
252068f8a7eSTamas Berghammer }
253068f8a7eSTamas Berghammer 
2542850b1beSTodd Fiala // ----------------------------------------------------------------------------
2552850b1beSTodd Fiala // NativeRegisterContextLinux_x86_64 members.
2562850b1beSTodd Fiala // ----------------------------------------------------------------------------
2572850b1beSTodd Fiala 
258068f8a7eSTamas Berghammer static RegisterInfoInterface *
259b9c1b51eSKate Stone CreateRegisterInfoInterface(const ArchSpec &target_arch) {
260b9c1b51eSKate Stone   if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) {
261068f8a7eSTamas Berghammer     // 32-bit hosts run with a RegisterContextLinux_i386 context.
262068f8a7eSTamas Berghammer     return new RegisterContextLinux_i386(target_arch);
263b9c1b51eSKate Stone   } else {
264068f8a7eSTamas Berghammer     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
265068f8a7eSTamas Berghammer            "Register setting path assumes this is a 64-bit host");
266b9c1b51eSKate Stone     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the
267b9c1b51eSKate Stone     // x86_64 register context.
268068f8a7eSTamas Berghammer     return new RegisterContextLinux_x86_64(target_arch);
269068f8a7eSTamas Berghammer   }
270068f8a7eSTamas Berghammer }
271068f8a7eSTamas Berghammer 
272b9c1b51eSKate Stone NativeRegisterContextLinux_x86_64::NativeRegisterContextLinux_x86_64(
273b9c1b51eSKate Stone     const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
274b9c1b51eSKate Stone     uint32_t concrete_frame_idx)
275b9c1b51eSKate Stone     : NativeRegisterContextLinux(native_thread, concrete_frame_idx,
276b9c1b51eSKate Stone                                  CreateRegisterInfoInterface(target_arch)),
27797e57e9bSValentina Giusti       m_xstate_type(XStateType::Invalid), m_fpr(), m_iovec(), m_ymm_set(),
278cda0ae46SValentina Giusti       m_mpx_set(), m_reg_info(), m_gpr_x86_64() {
2792850b1beSTodd Fiala   // Set up data about ranges of valid registers.
280b9c1b51eSKate Stone   switch (target_arch.GetMachine()) {
2812850b1beSTodd Fiala   case llvm::Triple::x86:
2822850b1beSTodd Fiala     m_reg_info.num_registers = k_num_registers_i386;
2832850b1beSTodd Fiala     m_reg_info.num_gpr_registers = k_num_gpr_registers_i386;
2842850b1beSTodd Fiala     m_reg_info.num_fpr_registers = k_num_fpr_registers_i386;
2852850b1beSTodd Fiala     m_reg_info.num_avx_registers = k_num_avx_registers_i386;
286cda0ae46SValentina Giusti     m_reg_info.num_mpx_registers = k_num_mpx_registers_i386;
2872850b1beSTodd Fiala     m_reg_info.last_gpr = k_last_gpr_i386;
2882850b1beSTodd Fiala     m_reg_info.first_fpr = k_first_fpr_i386;
2892850b1beSTodd Fiala     m_reg_info.last_fpr = k_last_fpr_i386;
2907f013bcdSZachary Turner     m_reg_info.first_st = lldb_st0_i386;
2917f013bcdSZachary Turner     m_reg_info.last_st = lldb_st7_i386;
2927f013bcdSZachary Turner     m_reg_info.first_mm = lldb_mm0_i386;
2937f013bcdSZachary Turner     m_reg_info.last_mm = lldb_mm7_i386;
2947f013bcdSZachary Turner     m_reg_info.first_xmm = lldb_xmm0_i386;
2957f013bcdSZachary Turner     m_reg_info.last_xmm = lldb_xmm7_i386;
2967f013bcdSZachary Turner     m_reg_info.first_ymm = lldb_ymm0_i386;
2977f013bcdSZachary Turner     m_reg_info.last_ymm = lldb_ymm7_i386;
298cda0ae46SValentina Giusti     m_reg_info.first_mpxr = lldb_bnd0_i386;
299cda0ae46SValentina Giusti     m_reg_info.last_mpxr = lldb_bnd3_i386;
300cda0ae46SValentina Giusti     m_reg_info.first_mpxc = lldb_bndcfgu_i386;
301cda0ae46SValentina Giusti     m_reg_info.last_mpxc = lldb_bndstatus_i386;
3027f013bcdSZachary Turner     m_reg_info.first_dr = lldb_dr0_i386;
3037f013bcdSZachary Turner     m_reg_info.gpr_flags = lldb_eflags_i386;
3042850b1beSTodd Fiala     break;
3052850b1beSTodd Fiala   case llvm::Triple::x86_64:
3062850b1beSTodd Fiala     m_reg_info.num_registers = k_num_registers_x86_64;
3072850b1beSTodd Fiala     m_reg_info.num_gpr_registers = k_num_gpr_registers_x86_64;
3082850b1beSTodd Fiala     m_reg_info.num_fpr_registers = k_num_fpr_registers_x86_64;
3092850b1beSTodd Fiala     m_reg_info.num_avx_registers = k_num_avx_registers_x86_64;
310cda0ae46SValentina Giusti     m_reg_info.num_mpx_registers = k_num_mpx_registers_x86_64;
3112850b1beSTodd Fiala     m_reg_info.last_gpr = k_last_gpr_x86_64;
3122850b1beSTodd Fiala     m_reg_info.first_fpr = k_first_fpr_x86_64;
3132850b1beSTodd Fiala     m_reg_info.last_fpr = k_last_fpr_x86_64;
3147f013bcdSZachary Turner     m_reg_info.first_st = lldb_st0_x86_64;
3157f013bcdSZachary Turner     m_reg_info.last_st = lldb_st7_x86_64;
3167f013bcdSZachary Turner     m_reg_info.first_mm = lldb_mm0_x86_64;
3177f013bcdSZachary Turner     m_reg_info.last_mm = lldb_mm7_x86_64;
3187f013bcdSZachary Turner     m_reg_info.first_xmm = lldb_xmm0_x86_64;
3197f013bcdSZachary Turner     m_reg_info.last_xmm = lldb_xmm15_x86_64;
3207f013bcdSZachary Turner     m_reg_info.first_ymm = lldb_ymm0_x86_64;
3217f013bcdSZachary Turner     m_reg_info.last_ymm = lldb_ymm15_x86_64;
322cda0ae46SValentina Giusti     m_reg_info.first_mpxr = lldb_bnd0_x86_64;
323cda0ae46SValentina Giusti     m_reg_info.last_mpxr = lldb_bnd3_x86_64;
324cda0ae46SValentina Giusti     m_reg_info.first_mpxc = lldb_bndcfgu_x86_64;
325cda0ae46SValentina Giusti     m_reg_info.last_mpxc = lldb_bndstatus_x86_64;
3267f013bcdSZachary Turner     m_reg_info.first_dr = lldb_dr0_x86_64;
3277f013bcdSZachary Turner     m_reg_info.gpr_flags = lldb_rflags_x86_64;
3282850b1beSTodd Fiala     break;
3292850b1beSTodd Fiala   default:
3302850b1beSTodd Fiala     assert(false && "Unhandled target architecture.");
3312850b1beSTodd Fiala     break;
3322850b1beSTodd Fiala   }
3332850b1beSTodd Fiala 
3342850b1beSTodd Fiala   // Initialize m_iovec to point to the buffer and buffer size
3352850b1beSTodd Fiala   // using the conventions of Berkeley style UIO structures, as required
3362850b1beSTodd Fiala   // by PTRACE extensions.
3372850b1beSTodd Fiala   m_iovec.iov_base = &m_fpr.xstate.xsave;
3382850b1beSTodd Fiala   m_iovec.iov_len = sizeof(m_fpr.xstate.xsave);
3392850b1beSTodd Fiala 
3402850b1beSTodd Fiala   // Clear out the FPR state.
3412850b1beSTodd Fiala   ::memset(&m_fpr, 0, sizeof(FPR));
3427f658eddSAbhishek Aggarwal 
3437f658eddSAbhishek Aggarwal   // Store byte offset of fctrl (i.e. first register of FPR)
3447f658eddSAbhishek Aggarwal   const RegisterInfo *reg_info_fctrl = GetRegisterInfoByName("fctrl");
3457f658eddSAbhishek Aggarwal   m_fctrl_offset_in_userarea = reg_info_fctrl->byte_offset;
3462850b1beSTodd Fiala }
3472850b1beSTodd Fiala 
3482850b1beSTodd Fiala // CONSIDER after local and llgs debugging are merged, register set support can
3492850b1beSTodd Fiala // be moved into a base x86-64 class with IsRegisterSetAvailable made virtual.
350b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::GetRegisterSetCount() const {
3512850b1beSTodd Fiala   uint32_t sets = 0;
352b9c1b51eSKate Stone   for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) {
3532850b1beSTodd Fiala     if (IsRegisterSetAvailable(set_index))
3542850b1beSTodd Fiala       ++sets;
3552850b1beSTodd Fiala   }
3562850b1beSTodd Fiala 
3572850b1beSTodd Fiala   return sets;
3582850b1beSTodd Fiala }
3592850b1beSTodd Fiala 
360b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::GetUserRegisterCount() const {
3618fa23b8eSTamas Berghammer   uint32_t count = 0;
362b9c1b51eSKate Stone   for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) {
363db264a6dSTamas Berghammer     const RegisterSet *set = GetRegisterSet(set_index);
3648fa23b8eSTamas Berghammer     if (set)
3658fa23b8eSTamas Berghammer       count += set->num_registers;
3668fa23b8eSTamas Berghammer   }
3678fa23b8eSTamas Berghammer   return count;
3688fa23b8eSTamas Berghammer }
3698fa23b8eSTamas Berghammer 
370db264a6dSTamas Berghammer const RegisterSet *
371b9c1b51eSKate Stone NativeRegisterContextLinux_x86_64::GetRegisterSet(uint32_t set_index) const {
3722850b1beSTodd Fiala   if (!IsRegisterSetAvailable(set_index))
3732850b1beSTodd Fiala     return nullptr;
3742850b1beSTodd Fiala 
375b9c1b51eSKate Stone   switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
3762850b1beSTodd Fiala   case llvm::Triple::x86:
3772850b1beSTodd Fiala     return &g_reg_sets_i386[set_index];
3782850b1beSTodd Fiala   case llvm::Triple::x86_64:
3792850b1beSTodd Fiala     return &g_reg_sets_x86_64[set_index];
3802850b1beSTodd Fiala   default:
3812850b1beSTodd Fiala     assert(false && "Unhandled target architecture.");
3822850b1beSTodd Fiala     return nullptr;
3832850b1beSTodd Fiala   }
3842850b1beSTodd Fiala 
3852850b1beSTodd Fiala   return nullptr;
3862850b1beSTodd Fiala }
3872850b1beSTodd Fiala 
38897206d57SZachary Turner Status
38997206d57SZachary Turner NativeRegisterContextLinux_x86_64::ReadRegister(const RegisterInfo *reg_info,
39097206d57SZachary Turner                                                 RegisterValue &reg_value) {
39197206d57SZachary Turner   Status error;
3922850b1beSTodd Fiala 
393b9c1b51eSKate Stone   if (!reg_info) {
3942850b1beSTodd Fiala     error.SetErrorString("reg_info NULL");
3952850b1beSTodd Fiala     return error;
3962850b1beSTodd Fiala   }
3972850b1beSTodd Fiala 
3982850b1beSTodd Fiala   const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
399b9c1b51eSKate Stone   if (reg == LLDB_INVALID_REGNUM) {
400b9c1b51eSKate Stone     // This is likely an internal register for lldb use only and should not be
401b9c1b51eSKate Stone     // directly queried.
402b9c1b51eSKate Stone     error.SetErrorStringWithFormat("register \"%s\" is an internal-only lldb "
403b9c1b51eSKate Stone                                    "register, cannot read directly",
404b9c1b51eSKate Stone                                    reg_info->name);
4052850b1beSTodd Fiala     return error;
4062850b1beSTodd Fiala   }
4072850b1beSTodd Fiala 
4085f957b54SValentina Giusti   if (IsFPR(reg) || IsAVX(reg) || IsMPX(reg)) {
409068f8a7eSTamas Berghammer     error = ReadFPR();
410068f8a7eSTamas Berghammer     if (error.Fail())
4112850b1beSTodd Fiala       return error;
412b9c1b51eSKate Stone   } else {
4132850b1beSTodd Fiala     uint32_t full_reg = reg;
414b9c1b51eSKate Stone     bool is_subreg = reg_info->invalidate_regs &&
415b9c1b51eSKate Stone                      (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM);
4162850b1beSTodd Fiala 
417b9c1b51eSKate Stone     if (is_subreg) {
4182850b1beSTodd Fiala       // Read the full aligned 64-bit register.
4192850b1beSTodd Fiala       full_reg = reg_info->invalidate_regs[0];
4202850b1beSTodd Fiala     }
4212850b1beSTodd Fiala 
4222850b1beSTodd Fiala     error = ReadRegisterRaw(full_reg, reg_value);
4232850b1beSTodd Fiala 
424b9c1b51eSKate Stone     if (error.Success()) {
425b9c1b51eSKate Stone       // If our read was not aligned (for ah,bh,ch,dh), shift our returned value
426b9c1b51eSKate Stone       // one byte to the right.
4272850b1beSTodd Fiala       if (is_subreg && (reg_info->byte_offset & 0x1))
4282850b1beSTodd Fiala         reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8);
4292850b1beSTodd Fiala 
430b9c1b51eSKate Stone       // If our return byte size was greater than the return value reg size,
431b9c1b51eSKate Stone       // then
4322850b1beSTodd Fiala       // use the type specified by reg_info rather than the uint64_t default
4332850b1beSTodd Fiala       if (reg_value.GetByteSize() > reg_info->byte_size)
4342850b1beSTodd Fiala         reg_value.SetType(reg_info);
4352850b1beSTodd Fiala     }
4362850b1beSTodd Fiala     return error;
4372850b1beSTodd Fiala   }
4382850b1beSTodd Fiala 
439b9c1b51eSKate Stone   if (reg_info->encoding == lldb::eEncodingVector) {
4402850b1beSTodd Fiala     lldb::ByteOrder byte_order = GetByteOrder();
4412850b1beSTodd Fiala 
442b9c1b51eSKate Stone     if (byte_order != lldb::eByteOrderInvalid) {
4432850b1beSTodd Fiala       if (reg >= m_reg_info.first_st && reg <= m_reg_info.last_st)
444b9c1b51eSKate Stone         reg_value.SetBytes(
445b9c1b51eSKate Stone             m_fpr.xstate.fxsave.stmm[reg - m_reg_info.first_st].bytes,
446b9c1b51eSKate Stone             reg_info->byte_size, byte_order);
4472850b1beSTodd Fiala       if (reg >= m_reg_info.first_mm && reg <= m_reg_info.last_mm)
448b9c1b51eSKate Stone         reg_value.SetBytes(
449b9c1b51eSKate Stone             m_fpr.xstate.fxsave.stmm[reg - m_reg_info.first_mm].bytes,
450b9c1b51eSKate Stone             reg_info->byte_size, byte_order);
4512850b1beSTodd Fiala       if (reg >= m_reg_info.first_xmm && reg <= m_reg_info.last_xmm)
452b9c1b51eSKate Stone         reg_value.SetBytes(
453b9c1b51eSKate Stone             m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_xmm].bytes,
454b9c1b51eSKate Stone             reg_info->byte_size, byte_order);
455b9c1b51eSKate Stone       if (reg >= m_reg_info.first_ymm && reg <= m_reg_info.last_ymm) {
4562850b1beSTodd Fiala         // Concatenate ymm using the register halves in xmm.bytes and ymmh.bytes
4575f957b54SValentina Giusti         if (CopyXSTATEtoYMM(reg, byte_order))
458b9c1b51eSKate Stone           reg_value.SetBytes(m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes,
459b9c1b51eSKate Stone                              reg_info->byte_size, byte_order);
460b9c1b51eSKate Stone         else {
4612850b1beSTodd Fiala           error.SetErrorString("failed to copy ymm register value");
4622850b1beSTodd Fiala           return error;
4632850b1beSTodd Fiala         }
4642850b1beSTodd Fiala       }
465cda0ae46SValentina Giusti       if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
4665f957b54SValentina Giusti         if (CopyXSTATEtoMPX(reg))
467cda0ae46SValentina Giusti           reg_value.SetBytes(m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].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       }
474cda0ae46SValentina Giusti       if (reg >= m_reg_info.first_mpxc && reg <= m_reg_info.last_mpxc) {
4755f957b54SValentina Giusti         if (CopyXSTATEtoMPX(reg))
476cda0ae46SValentina Giusti           reg_value.SetBytes(m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes,
477cda0ae46SValentina Giusti                              reg_info->byte_size, byte_order);
478cda0ae46SValentina Giusti         else {
479cda0ae46SValentina Giusti           error.SetErrorString("failed to copy mpx register value");
480cda0ae46SValentina Giusti           return error;
481cda0ae46SValentina Giusti         }
482cda0ae46SValentina Giusti       }
483ee44a92dSDimitar Vlahovski 
4842850b1beSTodd Fiala       if (reg_value.GetType() != RegisterValue::eTypeBytes)
485b9c1b51eSKate Stone         error.SetErrorString(
486b9c1b51eSKate Stone             "write failed - type was expected to be RegisterValue::eTypeBytes");
4872850b1beSTodd Fiala 
4882850b1beSTodd Fiala       return error;
4892850b1beSTodd Fiala     }
4902850b1beSTodd Fiala 
4912850b1beSTodd Fiala     error.SetErrorString("byte order is invalid");
4922850b1beSTodd Fiala     return error;
4932850b1beSTodd Fiala   }
4942850b1beSTodd Fiala 
4952850b1beSTodd Fiala   // Get pointer to m_fpr.xstate.fxsave variable and set the data from it.
4967f658eddSAbhishek Aggarwal 
4977f658eddSAbhishek Aggarwal   // Byte offsets of all registers are calculated wrt 'UserArea' structure.
4987f658eddSAbhishek Aggarwal   // However, ReadFPR() reads fpu registers {using ptrace(PTRACE_GETFPREGS,..)}
499b9c1b51eSKate Stone   // and stores them in 'm_fpr' (of type FPR structure). To extract values of
500b9c1b51eSKate Stone   // fpu
501b9c1b51eSKate Stone   // registers, m_fpr should be read at byte offsets calculated wrt to FPR
502b9c1b51eSKate Stone   // structure.
5037f658eddSAbhishek Aggarwal 
5047f658eddSAbhishek Aggarwal   // Since, FPR structure is also one of the member of UserArea structure.
505b9c1b51eSKate Stone   // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) -
506b9c1b51eSKate Stone   // byte_offset(fctrl wrt UserArea)
5077f658eddSAbhishek Aggarwal   assert((reg_info->byte_offset - m_fctrl_offset_in_userarea) < sizeof(m_fpr));
508b9c1b51eSKate Stone   uint8_t *src =
509b9c1b51eSKate Stone       (uint8_t *)&m_fpr + reg_info->byte_offset - m_fctrl_offset_in_userarea;
510b9c1b51eSKate Stone   switch (reg_info->byte_size) {
511b352a1c8SAbhishek Aggarwal   case 1:
512b352a1c8SAbhishek Aggarwal     reg_value.SetUInt8(*(uint8_t *)src);
513b352a1c8SAbhishek Aggarwal     break;
5142850b1beSTodd Fiala   case 2:
5152850b1beSTodd Fiala     reg_value.SetUInt16(*(uint16_t *)src);
5162850b1beSTodd Fiala     break;
5172850b1beSTodd Fiala   case 4:
5182850b1beSTodd Fiala     reg_value.SetUInt32(*(uint32_t *)src);
5192850b1beSTodd Fiala     break;
5202850b1beSTodd Fiala   case 8:
5212850b1beSTodd Fiala     reg_value.SetUInt64(*(uint64_t *)src);
5222850b1beSTodd Fiala     break;
5232850b1beSTodd Fiala   default:
5242850b1beSTodd Fiala     assert(false && "Unhandled data size.");
525b9c1b51eSKate Stone     error.SetErrorStringWithFormat("unhandled byte size: %" PRIu32,
526b9c1b51eSKate Stone                                    reg_info->byte_size);
5272850b1beSTodd Fiala     break;
5282850b1beSTodd Fiala   }
5292850b1beSTodd Fiala 
5302850b1beSTodd Fiala   return error;
5312850b1beSTodd Fiala }
5322850b1beSTodd Fiala 
53397206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::WriteRegister(
534b9c1b51eSKate Stone     const RegisterInfo *reg_info, const RegisterValue &reg_value) {
5352850b1beSTodd Fiala   assert(reg_info && "reg_info is null");
5362850b1beSTodd Fiala 
5372850b1beSTodd Fiala   const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB];
5382850b1beSTodd Fiala   if (reg_index == LLDB_INVALID_REGNUM)
53997206d57SZachary Turner     return Status("no lldb regnum for %s", reg_info && reg_info->name
540b9c1b51eSKate Stone                                                ? reg_info->name
541b9c1b51eSKate Stone                                                : "<unknown register>");
5422850b1beSTodd Fiala 
5432850b1beSTodd Fiala   if (IsGPR(reg_index))
544068f8a7eSTamas Berghammer     return WriteRegisterRaw(reg_index, reg_value);
5452850b1beSTodd Fiala 
5465f957b54SValentina Giusti   if (IsFPR(reg_index) || IsAVX(reg_index) || IsMPX(reg_index)) {
547b9c1b51eSKate Stone     if (reg_info->encoding == lldb::eEncodingVector) {
5482850b1beSTodd Fiala       if (reg_index >= m_reg_info.first_st && reg_index <= m_reg_info.last_st)
549b9c1b51eSKate Stone         ::memcpy(
550b9c1b51eSKate Stone             m_fpr.xstate.fxsave.stmm[reg_index - m_reg_info.first_st].bytes,
551b9c1b51eSKate Stone             reg_value.GetBytes(), reg_value.GetByteSize());
5522850b1beSTodd Fiala 
5532850b1beSTodd Fiala       if (reg_index >= m_reg_info.first_mm && reg_index <= m_reg_info.last_mm)
554b9c1b51eSKate Stone         ::memcpy(
555b9c1b51eSKate Stone             m_fpr.xstate.fxsave.stmm[reg_index - m_reg_info.first_mm].bytes,
556b9c1b51eSKate Stone             reg_value.GetBytes(), reg_value.GetByteSize());
5572850b1beSTodd Fiala 
5582850b1beSTodd Fiala       if (reg_index >= m_reg_info.first_xmm && reg_index <= m_reg_info.last_xmm)
559b9c1b51eSKate Stone         ::memcpy(
560b9c1b51eSKate Stone             m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_xmm].bytes,
561b9c1b51eSKate Stone             reg_value.GetBytes(), reg_value.GetByteSize());
5622850b1beSTodd Fiala 
563b9c1b51eSKate Stone       if (reg_index >= m_reg_info.first_ymm &&
564b9c1b51eSKate Stone           reg_index <= m_reg_info.last_ymm) {
565b9c1b51eSKate Stone         // Store ymm register content, and split into the register halves in
566b9c1b51eSKate Stone         // xmm.bytes and ymmh.bytes
567b9c1b51eSKate Stone         ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
568b9c1b51eSKate Stone                  reg_value.GetBytes(), reg_value.GetByteSize());
5692850b1beSTodd Fiala         if (!CopyYMMtoXSTATE(reg_index, GetByteOrder()))
57097206d57SZachary Turner           return Status("CopyYMMtoXSTATE() failed");
5712850b1beSTodd Fiala       }
572cda0ae46SValentina Giusti 
573cda0ae46SValentina Giusti       if (reg_index >= m_reg_info.first_mpxr &&
574cda0ae46SValentina Giusti           reg_index <= m_reg_info.last_mpxr) {
575cda0ae46SValentina Giusti         ::memcpy(m_mpx_set.mpxr[reg_index - m_reg_info.first_mpxr].bytes,
576cda0ae46SValentina Giusti                  reg_value.GetBytes(), reg_value.GetByteSize());
577cda0ae46SValentina Giusti         if (!CopyMPXtoXSTATE(reg_index))
57897206d57SZachary Turner           return Status("CopyMPXtoXSTATE() failed");
579cda0ae46SValentina Giusti       }
580cda0ae46SValentina Giusti 
581cda0ae46SValentina Giusti       if (reg_index >= m_reg_info.first_mpxc &&
582cda0ae46SValentina Giusti           reg_index <= m_reg_info.last_mpxc) {
583cda0ae46SValentina Giusti         ::memcpy(m_mpx_set.mpxc[reg_index - m_reg_info.first_mpxc].bytes,
584cda0ae46SValentina Giusti                  reg_value.GetBytes(), reg_value.GetByteSize());
585cda0ae46SValentina Giusti         if (!CopyMPXtoXSTATE(reg_index))
58697206d57SZachary Turner           return Status("CopyMPXtoXSTATE() failed");
587cda0ae46SValentina Giusti       }
588b9c1b51eSKate Stone     } else {
5892850b1beSTodd Fiala       // Get pointer to m_fpr.xstate.fxsave variable and set the data to it.
5907f658eddSAbhishek Aggarwal 
5917f658eddSAbhishek Aggarwal       // Byte offsets of all registers are calculated wrt 'UserArea' structure.
592b9c1b51eSKate Stone       // However, WriteFPR() takes m_fpr (of type FPR structure) and writes only
593b9c1b51eSKate Stone       // fpu
594b9c1b51eSKate Stone       // registers using ptrace(PTRACE_SETFPREGS,..) API. Hence fpu registers
595b9c1b51eSKate Stone       // should
5967f658eddSAbhishek Aggarwal       // be written in m_fpr at byte offsets calculated wrt FPR structure.
5977f658eddSAbhishek Aggarwal 
5987f658eddSAbhishek Aggarwal       // Since, FPR structure is also one of the member of UserArea structure.
599b9c1b51eSKate Stone       // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) -
600b9c1b51eSKate Stone       // byte_offset(fctrl wrt UserArea)
601b9c1b51eSKate Stone       assert((reg_info->byte_offset - m_fctrl_offset_in_userarea) <
602b9c1b51eSKate Stone              sizeof(m_fpr));
603b9c1b51eSKate Stone       uint8_t *dst = (uint8_t *)&m_fpr + reg_info->byte_offset -
604b9c1b51eSKate Stone                      m_fctrl_offset_in_userarea;
605b9c1b51eSKate Stone       switch (reg_info->byte_size) {
606b352a1c8SAbhishek Aggarwal       case 1:
607b352a1c8SAbhishek Aggarwal         *(uint8_t *)dst = reg_value.GetAsUInt8();
608b352a1c8SAbhishek Aggarwal         break;
6092850b1beSTodd Fiala       case 2:
6102850b1beSTodd Fiala         *(uint16_t *)dst = reg_value.GetAsUInt16();
6112850b1beSTodd Fiala         break;
6122850b1beSTodd Fiala       case 4:
6132850b1beSTodd Fiala         *(uint32_t *)dst = reg_value.GetAsUInt32();
6142850b1beSTodd Fiala         break;
6152850b1beSTodd Fiala       case 8:
6162850b1beSTodd Fiala         *(uint64_t *)dst = reg_value.GetAsUInt64();
6172850b1beSTodd Fiala         break;
6182850b1beSTodd Fiala       default:
6192850b1beSTodd Fiala         assert(false && "Unhandled data size.");
62097206d57SZachary Turner         return Status("unhandled register data size %" PRIu32,
621b9c1b51eSKate Stone                       reg_info->byte_size);
6222850b1beSTodd Fiala       }
6232850b1beSTodd Fiala     }
6242850b1beSTodd Fiala 
62597206d57SZachary Turner     Status error = WriteFPR();
626068f8a7eSTamas Berghammer     if (error.Fail())
627068f8a7eSTamas Berghammer       return error;
628068f8a7eSTamas Berghammer 
629b9c1b51eSKate Stone     if (IsAVX(reg_index)) {
6302850b1beSTodd Fiala       if (!CopyYMMtoXSTATE(reg_index, GetByteOrder()))
63197206d57SZachary Turner         return Status("CopyYMMtoXSTATE() failed");
6322850b1beSTodd Fiala     }
633cda0ae46SValentina Giusti 
634cda0ae46SValentina Giusti     if (IsMPX(reg_index)) {
635cda0ae46SValentina Giusti       if (!CopyMPXtoXSTATE(reg_index))
63697206d57SZachary Turner         return Status("CopyMPXtoXSTATE() failed");
637cda0ae46SValentina Giusti     }
63897206d57SZachary Turner     return Status();
6392850b1beSTodd Fiala   }
64097206d57SZachary Turner   return Status("failed - register wasn't recognized to be a GPR or an FPR, "
641b9c1b51eSKate Stone                 "write strategy unknown");
6422850b1beSTodd Fiala }
6432850b1beSTodd Fiala 
64497206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::ReadAllRegisterValues(
645b9c1b51eSKate Stone     lldb::DataBufferSP &data_sp) {
64697206d57SZachary Turner   Status error;
6472850b1beSTodd Fiala 
6482850b1beSTodd Fiala   data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0));
649068f8a7eSTamas Berghammer   error = ReadGPR();
650068f8a7eSTamas Berghammer   if (error.Fail())
6512850b1beSTodd Fiala     return error;
6522850b1beSTodd Fiala 
653068f8a7eSTamas Berghammer   error = ReadFPR();
654068f8a7eSTamas Berghammer   if (error.Fail())
6552850b1beSTodd Fiala     return error;
6562850b1beSTodd Fiala 
6572850b1beSTodd Fiala   uint8_t *dst = data_sp->GetBytes();
6582850b1beSTodd Fiala   ::memcpy(dst, &m_gpr_x86_64, GetRegisterInfoInterface().GetGPRSize());
6592850b1beSTodd Fiala   dst += GetRegisterInfoInterface().GetGPRSize();
66058db5bb2SValentina Giusti   if (m_xstate_type == XStateType::FXSAVE)
6612850b1beSTodd Fiala     ::memcpy(dst, &m_fpr.xstate.fxsave, sizeof(m_fpr.xstate.fxsave));
66258db5bb2SValentina Giusti   else if (m_xstate_type == XStateType::XSAVE) {
6632850b1beSTodd Fiala     lldb::ByteOrder byte_order = GetByteOrder();
6642850b1beSTodd Fiala 
66597e57e9bSValentina Giusti     if (IsCPUFeatureAvailable(RegSet::avx)) {
6662850b1beSTodd Fiala       // Assemble the YMM register content from the register halves.
667b9c1b51eSKate Stone       for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm;
668b9c1b51eSKate Stone            ++reg) {
669b9c1b51eSKate Stone         if (!CopyXSTATEtoYMM(reg, byte_order)) {
6705f957b54SValentina Giusti           error.SetErrorStringWithFormat(
6715f957b54SValentina Giusti               "NativeRegisterContextLinux_x86_64::%s "
672b9c1b51eSKate Stone               "CopyXSTATEtoYMM() failed for reg num "
673b9c1b51eSKate Stone               "%" PRIu32,
674b9c1b51eSKate Stone               __FUNCTION__, reg);
6752850b1beSTodd Fiala           return error;
6762850b1beSTodd Fiala         }
6772850b1beSTodd Fiala       }
6785f957b54SValentina Giusti     }
6792850b1beSTodd Fiala 
68097e57e9bSValentina Giusti     if (IsCPUFeatureAvailable(RegSet::mpx)) {
681cda0ae46SValentina Giusti       for (uint32_t reg = m_reg_info.first_mpxr; reg <= m_reg_info.last_mpxc;
682cda0ae46SValentina Giusti            ++reg) {
683cda0ae46SValentina Giusti         if (!CopyXSTATEtoMPX(reg)) {
6845f957b54SValentina Giusti           error.SetErrorStringWithFormat(
6855f957b54SValentina Giusti               "NativeRegisterContextLinux_x86_64::%s "
686cda0ae46SValentina Giusti               "CopyXSTATEtoMPX() failed for reg num "
687cda0ae46SValentina Giusti               "%" PRIu32,
688cda0ae46SValentina Giusti               __FUNCTION__, reg);
689cda0ae46SValentina Giusti           return error;
690cda0ae46SValentina Giusti         }
691cda0ae46SValentina Giusti       }
6925f957b54SValentina Giusti     }
6932850b1beSTodd Fiala     // Copy the extended register state including the assembled ymm registers.
6942850b1beSTodd Fiala     ::memcpy(dst, &m_fpr, sizeof(m_fpr));
695b9c1b51eSKate Stone   } else {
6962850b1beSTodd Fiala     assert(false && "how do we save the floating point registers?");
6972850b1beSTodd Fiala     error.SetErrorString("unsure how to save the floating point registers");
6982850b1beSTodd Fiala   }
6994778e410SRavitheja Addepally   /** The following code is specific to Linux x86 based architectures,
7004778e410SRavitheja Addepally    *  where the register orig_eax (32 bit)/orig_rax (64 bit) is set to
7014778e410SRavitheja Addepally    *  -1 to solve the bug 23659, such a setting prevents the automatic
7024778e410SRavitheja Addepally    *  decrement of the instruction pointer which was causing the SIGILL
7034778e410SRavitheja Addepally    *  exception.
7044778e410SRavitheja Addepally    * **/
7054778e410SRavitheja Addepally 
7064778e410SRavitheja Addepally   RegisterValue value((uint64_t)-1);
707b9c1b51eSKate Stone   const RegisterInfo *reg_info =
708b9c1b51eSKate Stone       GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_eax");
7094778e410SRavitheja Addepally   if (reg_info == nullptr)
7104778e410SRavitheja Addepally     reg_info = GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_rax");
7114778e410SRavitheja Addepally 
71264ad85ceSTamas Berghammer   if (reg_info != nullptr)
7134778e410SRavitheja Addepally     return DoWriteRegisterValue(reg_info->byte_offset, reg_info->name, value);
7142850b1beSTodd Fiala 
7152850b1beSTodd Fiala   return error;
7162850b1beSTodd Fiala }
7172850b1beSTodd Fiala 
71897206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::WriteAllRegisterValues(
719b9c1b51eSKate Stone     const lldb::DataBufferSP &data_sp) {
72097206d57SZachary Turner   Status error;
7212850b1beSTodd Fiala 
722b9c1b51eSKate Stone   if (!data_sp) {
723b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
724b9c1b51eSKate Stone         "NativeRegisterContextLinux_x86_64::%s invalid data_sp provided",
725b9c1b51eSKate Stone         __FUNCTION__);
7262850b1beSTodd Fiala     return error;
7272850b1beSTodd Fiala   }
7282850b1beSTodd Fiala 
729b9c1b51eSKate Stone   if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) {
730*993f2a42SPavel Labath     error.SetErrorStringWithFormatv(
731*993f2a42SPavel Labath         "data_sp contained mismatched data size, expected {0}, actual {1}",
732*993f2a42SPavel Labath         REG_CONTEXT_SIZE, data_sp->GetByteSize());
7332850b1beSTodd Fiala     return error;
7342850b1beSTodd Fiala   }
7352850b1beSTodd Fiala 
7362850b1beSTodd Fiala   uint8_t *src = data_sp->GetBytes();
737b9c1b51eSKate Stone   if (src == nullptr) {
738b9c1b51eSKate Stone     error.SetErrorStringWithFormat("NativeRegisterContextLinux_x86_64::%s "
739b9c1b51eSKate Stone                                    "DataBuffer::GetBytes() returned a null "
740b9c1b51eSKate Stone                                    "pointer",
741b9c1b51eSKate Stone                                    __FUNCTION__);
7422850b1beSTodd Fiala     return error;
7432850b1beSTodd Fiala   }
7442850b1beSTodd Fiala   ::memcpy(&m_gpr_x86_64, src, GetRegisterInfoInterface().GetGPRSize());
7452850b1beSTodd Fiala 
746068f8a7eSTamas Berghammer   error = WriteGPR();
747068f8a7eSTamas Berghammer   if (error.Fail())
7482850b1beSTodd Fiala     return error;
7492850b1beSTodd Fiala 
7502850b1beSTodd Fiala   src += GetRegisterInfoInterface().GetGPRSize();
75158db5bb2SValentina Giusti   if (m_xstate_type == XStateType::FXSAVE)
7522850b1beSTodd Fiala     ::memcpy(&m_fpr.xstate.fxsave, src, sizeof(m_fpr.xstate.fxsave));
75358db5bb2SValentina Giusti   else if (m_xstate_type == XStateType::XSAVE)
7542850b1beSTodd Fiala     ::memcpy(&m_fpr.xstate.xsave, src, sizeof(m_fpr.xstate.xsave));
7552850b1beSTodd Fiala 
756068f8a7eSTamas Berghammer   error = WriteFPR();
757068f8a7eSTamas Berghammer   if (error.Fail())
7582850b1beSTodd Fiala     return error;
7592850b1beSTodd Fiala 
76058db5bb2SValentina Giusti   if (m_xstate_type == XStateType::XSAVE) {
7612850b1beSTodd Fiala     lldb::ByteOrder byte_order = GetByteOrder();
7622850b1beSTodd Fiala 
76397e57e9bSValentina Giusti     if (IsCPUFeatureAvailable(RegSet::avx)) {
7642850b1beSTodd Fiala       // Parse the YMM register content from the register halves.
765b9c1b51eSKate Stone       for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm;
766b9c1b51eSKate Stone            ++reg) {
767b9c1b51eSKate Stone         if (!CopyYMMtoXSTATE(reg, byte_order)) {
7685f957b54SValentina Giusti           error.SetErrorStringWithFormat(
7695f957b54SValentina Giusti               "NativeRegisterContextLinux_x86_64::%s "
770b9c1b51eSKate Stone               "CopyYMMtoXSTATE() failed for reg num "
771b9c1b51eSKate Stone               "%" PRIu32,
772b9c1b51eSKate Stone               __FUNCTION__, reg);
7732850b1beSTodd Fiala           return error;
7742850b1beSTodd Fiala         }
7752850b1beSTodd Fiala       }
7765f957b54SValentina Giusti     }
777cda0ae46SValentina Giusti 
77897e57e9bSValentina Giusti     if (IsCPUFeatureAvailable(RegSet::mpx)) {
779cda0ae46SValentina Giusti       for (uint32_t reg = m_reg_info.first_mpxr; reg <= m_reg_info.last_mpxc;
780cda0ae46SValentina Giusti            ++reg) {
781cda0ae46SValentina Giusti         if (!CopyMPXtoXSTATE(reg)) {
7825f957b54SValentina Giusti           error.SetErrorStringWithFormat(
7835f957b54SValentina Giusti               "NativeRegisterContextLinux_x86_64::%s "
784cda0ae46SValentina Giusti               "CopyMPXtoXSTATE() failed for reg num "
785cda0ae46SValentina Giusti               "%" PRIu32,
786cda0ae46SValentina Giusti               __FUNCTION__, reg);
787cda0ae46SValentina Giusti           return error;
788cda0ae46SValentina Giusti         }
789cda0ae46SValentina Giusti       }
7902850b1beSTodd Fiala     }
7915f957b54SValentina Giusti   }
7922850b1beSTodd Fiala 
7932850b1beSTodd Fiala   return error;
7942850b1beSTodd Fiala }
7952850b1beSTodd Fiala 
7965f957b54SValentina Giusti bool NativeRegisterContextLinux_x86_64::IsCPUFeatureAvailable(
7975f957b54SValentina Giusti     RegSet feature_code) const {
79858db5bb2SValentina Giusti   if (m_xstate_type == XStateType::Invalid) {
79958db5bb2SValentina Giusti     if (const_cast<NativeRegisterContextLinux_x86_64 *>(this)->ReadFPR().Fail())
8005f957b54SValentina Giusti       return false;
80158db5bb2SValentina Giusti   }
8025f957b54SValentina Giusti   switch (feature_code) {
80358db5bb2SValentina Giusti   case RegSet::gpr:
80458db5bb2SValentina Giusti   case RegSet::fpu:
8055f957b54SValentina Giusti     return true;
80658db5bb2SValentina Giusti   case RegSet::avx: // Check if CPU has AVX and if there is kernel support, by
80758db5bb2SValentina Giusti                     // reading in the XCR0 area of XSAVE.
80858db5bb2SValentina Giusti     if ((m_fpr.xstate.xsave.i387.xcr0 & mask_XSTATE_AVX) == mask_XSTATE_AVX)
8095f957b54SValentina Giusti       return true;
81058db5bb2SValentina Giusti      break;
81158db5bb2SValentina Giusti   case RegSet::mpx: // Check if CPU has MPX and if there is kernel support, by
81258db5bb2SValentina Giusti                     // reading in the XCR0 area of XSAVE.
81358db5bb2SValentina Giusti     if ((m_fpr.xstate.xsave.i387.xcr0 & mask_XSTATE_MPX) == mask_XSTATE_MPX)
81458db5bb2SValentina Giusti       return true;
81558db5bb2SValentina Giusti     break;
8165f957b54SValentina Giusti   }
8175f957b54SValentina Giusti   return false;
8185f957b54SValentina Giusti }
8195f957b54SValentina Giusti 
820b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsRegisterSetAvailable(
821b9c1b51eSKate Stone     uint32_t set_index) const {
8222850b1beSTodd Fiala   uint32_t num_sets = k_num_register_sets - k_num_extended_register_sets;
8232850b1beSTodd Fiala 
82497e57e9bSValentina Giusti   switch (static_cast<RegSet>(set_index)) {
82597e57e9bSValentina Giusti   case RegSet::gpr:
82697e57e9bSValentina Giusti   case RegSet::fpu:
8272850b1beSTodd Fiala     return (set_index < num_sets);
82897e57e9bSValentina Giusti   case RegSet::avx:
82997e57e9bSValentina Giusti     return IsCPUFeatureAvailable(RegSet::avx);
83097e57e9bSValentina Giusti   case RegSet::mpx:
83197e57e9bSValentina Giusti     return IsCPUFeatureAvailable(RegSet::mpx);
8325f957b54SValentina Giusti   }
83358db5bb2SValentina Giusti   return false;
8342850b1beSTodd Fiala }
8352850b1beSTodd Fiala 
836b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsGPR(uint32_t reg_index) const {
8372850b1beSTodd Fiala   // GPRs come first.
8382850b1beSTodd Fiala   return reg_index <= m_reg_info.last_gpr;
8392850b1beSTodd Fiala }
8402850b1beSTodd Fiala 
841b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsFPR(uint32_t reg_index) const {
842b9c1b51eSKate Stone   return (m_reg_info.first_fpr <= reg_index &&
843b9c1b51eSKate Stone           reg_index <= m_reg_info.last_fpr);
8442850b1beSTodd Fiala }
8452850b1beSTodd Fiala 
84697206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::WriteFPR() {
84758db5bb2SValentina Giusti   switch (m_xstate_type) {
84897e57e9bSValentina Giusti   case XStateType::FXSAVE:
8496ec13991SPavel Labath     return WriteRegisterSet(
8506ec13991SPavel Labath         &m_iovec, sizeof(m_fpr.xstate.xsave),
8516ec13991SPavel Labath         fxsr_regset(GetRegisterInfoInterface().GetTargetArchitecture()));
85297e57e9bSValentina Giusti   case XStateType::XSAVE:
853b9c1b51eSKate Stone     return WriteRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave),
854b9c1b51eSKate Stone                             NT_X86_XSTATE);
855068f8a7eSTamas Berghammer   default:
85697206d57SZachary Turner     return Status("Unrecognized FPR type.");
857068f8a7eSTamas Berghammer   }
8582850b1beSTodd Fiala }
8592850b1beSTodd Fiala 
860b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsAVX(uint32_t reg_index) const {
86197e57e9bSValentina Giusti   if (!IsCPUFeatureAvailable(RegSet::avx))
8625f957b54SValentina Giusti     return false;
863b9c1b51eSKate Stone   return (m_reg_info.first_ymm <= reg_index &&
864b9c1b51eSKate Stone           reg_index <= m_reg_info.last_ymm);
8652850b1beSTodd Fiala }
8662850b1beSTodd Fiala 
867b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::CopyXSTATEtoYMM(
868b9c1b51eSKate Stone     uint32_t reg_index, lldb::ByteOrder byte_order) {
8692850b1beSTodd Fiala   if (!IsAVX(reg_index))
8702850b1beSTodd Fiala     return false;
8712850b1beSTodd Fiala 
872b9c1b51eSKate Stone   if (byte_order == lldb::eByteOrderLittle) {
8732850b1beSTodd Fiala     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
8742850b1beSTodd Fiala              m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes,
8752850b1beSTodd Fiala              sizeof(XMMReg));
876b9c1b51eSKate Stone     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes +
877b9c1b51eSKate Stone                  sizeof(XMMReg),
8782850b1beSTodd Fiala              m_fpr.xstate.xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes,
8792850b1beSTodd Fiala              sizeof(YMMHReg));
8802850b1beSTodd Fiala     return true;
8812850b1beSTodd Fiala   }
8822850b1beSTodd Fiala 
883b9c1b51eSKate Stone   if (byte_order == lldb::eByteOrderBig) {
884b9c1b51eSKate Stone     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes +
885b9c1b51eSKate Stone                  sizeof(XMMReg),
8862850b1beSTodd Fiala              m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes,
8872850b1beSTodd Fiala              sizeof(XMMReg));
8882850b1beSTodd Fiala     ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes,
8892850b1beSTodd Fiala              m_fpr.xstate.xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes,
8902850b1beSTodd Fiala              sizeof(YMMHReg));
8912850b1beSTodd Fiala     return true;
8922850b1beSTodd Fiala   }
8932850b1beSTodd Fiala   return false; // unsupported or invalid byte order
8942850b1beSTodd Fiala }
8952850b1beSTodd Fiala 
896b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::CopyYMMtoXSTATE(
897b9c1b51eSKate Stone     uint32_t reg, lldb::ByteOrder byte_order) {
8982850b1beSTodd Fiala   if (!IsAVX(reg))
8992850b1beSTodd Fiala     return false;
9002850b1beSTodd Fiala 
901b9c1b51eSKate Stone   if (byte_order == lldb::eByteOrderLittle) {
9022850b1beSTodd Fiala     ::memcpy(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_ymm].bytes,
903b9c1b51eSKate Stone              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, sizeof(XMMReg));
9042850b1beSTodd Fiala     ::memcpy(m_fpr.xstate.xsave.ymmh[reg - m_reg_info.first_ymm].bytes,
9052850b1beSTodd Fiala              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg),
9062850b1beSTodd Fiala              sizeof(YMMHReg));
9072850b1beSTodd Fiala     return true;
9082850b1beSTodd Fiala   }
9092850b1beSTodd Fiala 
910b9c1b51eSKate Stone   if (byte_order == lldb::eByteOrderBig) {
9112850b1beSTodd Fiala     ::memcpy(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_ymm].bytes,
9122850b1beSTodd Fiala              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg),
9132850b1beSTodd Fiala              sizeof(XMMReg));
9142850b1beSTodd Fiala     ::memcpy(m_fpr.xstate.xsave.ymmh[reg - m_reg_info.first_ymm].bytes,
915b9c1b51eSKate Stone              m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, sizeof(YMMHReg));
9162850b1beSTodd Fiala     return true;
9172850b1beSTodd Fiala   }
9182850b1beSTodd Fiala   return false; // unsupported or invalid byte order
9192850b1beSTodd Fiala }
9202850b1beSTodd Fiala 
921b9c1b51eSKate Stone void *NativeRegisterContextLinux_x86_64::GetFPRBuffer() {
92258db5bb2SValentina Giusti   switch (m_xstate_type) {
92397e57e9bSValentina Giusti   case XStateType::FXSAVE:
924068f8a7eSTamas Berghammer     return &m_fpr.xstate.fxsave;
92597e57e9bSValentina Giusti   case XStateType::XSAVE:
926068f8a7eSTamas Berghammer     return &m_iovec;
9272850b1beSTodd Fiala   default:
928068f8a7eSTamas Berghammer     return nullptr;
9292850b1beSTodd Fiala   }
9302850b1beSTodd Fiala }
9312850b1beSTodd Fiala 
932b9c1b51eSKate Stone size_t NativeRegisterContextLinux_x86_64::GetFPRSize() {
93358db5bb2SValentina Giusti   switch (m_xstate_type) {
93497e57e9bSValentina Giusti   case XStateType::FXSAVE:
935068f8a7eSTamas Berghammer     return sizeof(m_fpr.xstate.fxsave);
93697e57e9bSValentina Giusti   case XStateType::XSAVE:
937068f8a7eSTamas Berghammer     return sizeof(m_iovec);
938068f8a7eSTamas Berghammer   default:
939068f8a7eSTamas Berghammer     return 0;
940068f8a7eSTamas Berghammer   }
9412850b1beSTodd Fiala }
9422850b1beSTodd Fiala 
94397206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::ReadFPR() {
94497206d57SZachary Turner   Status error;
94558db5bb2SValentina Giusti 
94658db5bb2SValentina Giusti   // Probe XSAVE and if it is not supported fall back to FXSAVE.
94758db5bb2SValentina Giusti   if (m_xstate_type != XStateType::FXSAVE) {
94858db5bb2SValentina Giusti     error =
94958db5bb2SValentina Giusti         ReadRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), NT_X86_XSTATE);
95058db5bb2SValentina Giusti     if (!error.Fail()) {
95158db5bb2SValentina Giusti       m_xstate_type = XStateType::XSAVE;
95258db5bb2SValentina Giusti       return error;
953296e063dSAbhishek Aggarwal     }
954068f8a7eSTamas Berghammer   }
9556ec13991SPavel Labath   error = ReadRegisterSet(
9566ec13991SPavel Labath       &m_iovec, sizeof(m_fpr.xstate.xsave),
9576ec13991SPavel Labath       fxsr_regset(GetRegisterInfoInterface().GetTargetArchitecture()));
95858db5bb2SValentina Giusti   if (!error.Fail()) {
95958db5bb2SValentina Giusti     m_xstate_type = XStateType::FXSAVE;
96058db5bb2SValentina Giusti     return error;
96158db5bb2SValentina Giusti   }
96297206d57SZachary Turner   return Status("Unrecognized FPR type.");
9632850b1beSTodd Fiala }
9642850b1beSTodd Fiala 
965cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::IsMPX(uint32_t reg_index) const {
96697e57e9bSValentina Giusti   if (!IsCPUFeatureAvailable(RegSet::mpx))
9675f957b54SValentina Giusti     return false;
968cda0ae46SValentina Giusti   return (m_reg_info.first_mpxr <= reg_index &&
969cda0ae46SValentina Giusti           reg_index <= m_reg_info.last_mpxc);
970cda0ae46SValentina Giusti }
971cda0ae46SValentina Giusti 
972cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::CopyXSTATEtoMPX(uint32_t reg) {
973cda0ae46SValentina Giusti   if (!IsMPX(reg))
974cda0ae46SValentina Giusti     return false;
975cda0ae46SValentina Giusti 
976cda0ae46SValentina Giusti   if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
977cda0ae46SValentina Giusti     ::memcpy(m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes,
978cda0ae46SValentina Giusti              m_fpr.xstate.xsave.mpxr[reg - m_reg_info.first_mpxr].bytes,
979cda0ae46SValentina Giusti              sizeof(MPXReg));
980cda0ae46SValentina Giusti   } else {
981cda0ae46SValentina Giusti     ::memcpy(m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes,
982cda0ae46SValentina Giusti              m_fpr.xstate.xsave.mpxc[reg - m_reg_info.first_mpxc].bytes,
983cda0ae46SValentina Giusti              sizeof(MPXCsr));
984cda0ae46SValentina Giusti   }
985cda0ae46SValentina Giusti   return true;
986cda0ae46SValentina Giusti }
987cda0ae46SValentina Giusti 
988cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::CopyMPXtoXSTATE(uint32_t reg) {
989cda0ae46SValentina Giusti   if (!IsMPX(reg))
990cda0ae46SValentina Giusti     return false;
991cda0ae46SValentina Giusti 
992cda0ae46SValentina Giusti   if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) {
993cda0ae46SValentina Giusti     ::memcpy(m_fpr.xstate.xsave.mpxr[reg - m_reg_info.first_mpxr].bytes,
994cda0ae46SValentina Giusti              m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes, sizeof(MPXReg));
995cda0ae46SValentina Giusti   } else {
996cda0ae46SValentina Giusti     ::memcpy(m_fpr.xstate.xsave.mpxc[reg - m_reg_info.first_mpxc].bytes,
997cda0ae46SValentina Giusti              m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes, sizeof(MPXCsr));
998cda0ae46SValentina Giusti   }
999cda0ae46SValentina Giusti   return true;
1000cda0ae46SValentina Giusti }
1001cda0ae46SValentina Giusti 
100297206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::IsWatchpointHit(uint32_t wp_index,
1003b9c1b51eSKate Stone                                                           bool &is_hit) {
100418fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
100597206d57SZachary Turner     return Status("Watchpoint index out of range");
100618fe6404SChaoren Lin 
100718fe6404SChaoren Lin   RegisterValue reg_value;
100897206d57SZachary Turner   Status error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1009b9c1b51eSKate Stone   if (error.Fail()) {
1010c16f5dcaSChaoren Lin     is_hit = false;
1011c16f5dcaSChaoren Lin     return error;
1012c16f5dcaSChaoren Lin   }
101318fe6404SChaoren Lin 
101418fe6404SChaoren Lin   uint64_t status_bits = reg_value.GetAsUInt64();
101518fe6404SChaoren Lin 
1016c16f5dcaSChaoren Lin   is_hit = status_bits & (1 << wp_index);
101718fe6404SChaoren Lin 
101818fe6404SChaoren Lin   return error;
101918fe6404SChaoren Lin }
102018fe6404SChaoren Lin 
102197206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::GetWatchpointHitIndex(
1022b9c1b51eSKate Stone     uint32_t &wp_index, lldb::addr_t trap_addr) {
1023c16f5dcaSChaoren Lin   uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
1024b9c1b51eSKate Stone   for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) {
1025c16f5dcaSChaoren Lin     bool is_hit;
102697206d57SZachary Turner     Status error = IsWatchpointHit(wp_index, is_hit);
1027c16f5dcaSChaoren Lin     if (error.Fail()) {
1028c16f5dcaSChaoren Lin       wp_index = LLDB_INVALID_INDEX32;
1029c16f5dcaSChaoren Lin       return error;
1030c16f5dcaSChaoren Lin     } else if (is_hit) {
1031c16f5dcaSChaoren Lin       return error;
1032c16f5dcaSChaoren Lin     }
1033c16f5dcaSChaoren Lin   }
1034c16f5dcaSChaoren Lin   wp_index = LLDB_INVALID_INDEX32;
103597206d57SZachary Turner   return Status();
1036c16f5dcaSChaoren Lin }
1037c16f5dcaSChaoren Lin 
103897206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::IsWatchpointVacant(uint32_t wp_index,
1039b9c1b51eSKate Stone                                                              bool &is_vacant) {
104018fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
104197206d57SZachary Turner     return Status("Watchpoint index out of range");
104218fe6404SChaoren Lin 
104318fe6404SChaoren Lin   RegisterValue reg_value;
104497206d57SZachary Turner   Status error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1045b9c1b51eSKate Stone   if (error.Fail()) {
1046c16f5dcaSChaoren Lin     is_vacant = false;
1047c16f5dcaSChaoren Lin     return error;
1048c16f5dcaSChaoren Lin   }
104918fe6404SChaoren Lin 
105018fe6404SChaoren Lin   uint64_t control_bits = reg_value.GetAsUInt64();
105118fe6404SChaoren Lin 
1052c16f5dcaSChaoren Lin   is_vacant = !(control_bits & (1 << (2 * wp_index)));
105318fe6404SChaoren Lin 
105418fe6404SChaoren Lin   return error;
105518fe6404SChaoren Lin }
105618fe6404SChaoren Lin 
105797206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::SetHardwareWatchpointWithIndex(
105818fe6404SChaoren Lin     lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index) {
105918fe6404SChaoren Lin 
106018fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
106197206d57SZachary Turner     return Status("Watchpoint index out of range");
106218fe6404SChaoren Lin 
1063b9c1b51eSKate Stone   // Read only watchpoints aren't supported on x86_64. Fall back to read/write
1064b9c1b51eSKate Stone   // waitchpoints instead.
1065b9c1b51eSKate Stone   // TODO: Add logic to detect when a write happens and ignore that watchpoint
1066b9c1b51eSKate Stone   // hit.
1067cf8eb9daSOmair Javaid   if (watch_flags == 0x2)
106840caea63STamas Berghammer     watch_flags = 0x3;
1069cf8eb9daSOmair Javaid 
107018fe6404SChaoren Lin   if (watch_flags != 0x1 && watch_flags != 0x3)
107197206d57SZachary Turner     return Status("Invalid read/write bits for watchpoint");
107218fe6404SChaoren Lin 
107318fe6404SChaoren Lin   if (size != 1 && size != 2 && size != 4 && size != 8)
107497206d57SZachary Turner     return Status("Invalid size for watchpoint");
107518fe6404SChaoren Lin 
1076c16f5dcaSChaoren Lin   bool is_vacant;
107797206d57SZachary Turner   Status error = IsWatchpointVacant(wp_index, is_vacant);
1078b9c1b51eSKate Stone   if (error.Fail())
1079b9c1b51eSKate Stone     return error;
1080b9c1b51eSKate Stone   if (!is_vacant)
108197206d57SZachary Turner     return Status("Watchpoint index not vacant");
108218fe6404SChaoren Lin 
108318fe6404SChaoren Lin   RegisterValue reg_value;
10846a504f6eSChaoren Lin   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1085b9c1b51eSKate Stone   if (error.Fail())
1086b9c1b51eSKate Stone     return error;
108718fe6404SChaoren Lin 
108818fe6404SChaoren Lin   // for watchpoints 0, 1, 2, or 3, respectively,
108918fe6404SChaoren Lin   // set bits 1, 3, 5, or 7
109018fe6404SChaoren Lin   uint64_t enable_bit = 1 << (2 * wp_index);
109118fe6404SChaoren Lin 
109218fe6404SChaoren Lin   // set bits 16-17, 20-21, 24-25, or 28-29
109318fe6404SChaoren Lin   // with 0b01 for write, and 0b11 for read/write
109418fe6404SChaoren Lin   uint64_t rw_bits = watch_flags << (16 + 4 * wp_index);
109518fe6404SChaoren Lin 
109618fe6404SChaoren Lin   // set bits 18-19, 22-23, 26-27, or 30-31
109718fe6404SChaoren Lin   // with 0b00, 0b01, 0b10, or 0b11
109818fe6404SChaoren Lin   // for 1, 2, 8 (if supported), or 4 bytes, respectively
109918fe6404SChaoren Lin   uint64_t size_bits = (size == 8 ? 0x2 : size - 1) << (18 + 4 * wp_index);
110018fe6404SChaoren Lin 
110118fe6404SChaoren Lin   uint64_t bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index));
110218fe6404SChaoren Lin 
110318fe6404SChaoren Lin   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
110418fe6404SChaoren Lin 
110518fe6404SChaoren Lin   control_bits |= enable_bit | rw_bits | size_bits;
110618fe6404SChaoren Lin 
1107068f8a7eSTamas Berghammer   error = WriteRegisterRaw(m_reg_info.first_dr + wp_index, RegisterValue(addr));
1108b9c1b51eSKate Stone   if (error.Fail())
1109b9c1b51eSKate Stone     return error;
111018fe6404SChaoren Lin 
1111b9c1b51eSKate Stone   error =
1112b9c1b51eSKate Stone       WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits));
1113b9c1b51eSKate Stone   if (error.Fail())
1114b9c1b51eSKate Stone     return error;
111518fe6404SChaoren Lin 
111618fe6404SChaoren Lin   error.Clear();
111718fe6404SChaoren Lin   return error;
111818fe6404SChaoren Lin }
111918fe6404SChaoren Lin 
1120b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::ClearHardwareWatchpoint(
1121b9c1b51eSKate Stone     uint32_t wp_index) {
112218fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
112318fe6404SChaoren Lin     return false;
112418fe6404SChaoren Lin 
112518fe6404SChaoren Lin   RegisterValue reg_value;
112618fe6404SChaoren Lin 
112718fe6404SChaoren Lin   // for watchpoints 0, 1, 2, or 3, respectively,
112818fe6404SChaoren Lin   // clear bits 0, 1, 2, or 3 of the debug status register (DR6)
112997206d57SZachary Turner   Status error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1130b9c1b51eSKate Stone   if (error.Fail())
1131b9c1b51eSKate Stone     return false;
113218fe6404SChaoren Lin   uint64_t bit_mask = 1 << wp_index;
113318fe6404SChaoren Lin   uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask;
1134068f8a7eSTamas Berghammer   error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits));
1135b9c1b51eSKate Stone   if (error.Fail())
1136b9c1b51eSKate Stone     return false;
113718fe6404SChaoren Lin 
113818fe6404SChaoren Lin   // for watchpoints 0, 1, 2, or 3, respectively,
113918fe6404SChaoren Lin   // clear bits {0-1,16-19}, {2-3,20-23}, {4-5,24-27}, or {6-7,28-31}
114018fe6404SChaoren Lin   // of the debug control register (DR7)
11416a504f6eSChaoren Lin   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1142b9c1b51eSKate Stone   if (error.Fail())
1143b9c1b51eSKate Stone     return false;
114418fe6404SChaoren Lin   bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index));
114518fe6404SChaoren Lin   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
1146b9c1b51eSKate Stone   return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits))
1147b9c1b51eSKate Stone       .Success();
114818fe6404SChaoren Lin }
114918fe6404SChaoren Lin 
115097206d57SZachary Turner Status NativeRegisterContextLinux_x86_64::ClearAllHardwareWatchpoints() {
115118fe6404SChaoren Lin   RegisterValue reg_value;
115218fe6404SChaoren Lin 
115318fe6404SChaoren Lin   // clear bits {0-4} of the debug status register (DR6)
115497206d57SZachary Turner   Status error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value);
1155b9c1b51eSKate Stone   if (error.Fail())
1156b9c1b51eSKate Stone     return error;
115718fe6404SChaoren Lin   uint64_t bit_mask = 0xF;
115818fe6404SChaoren Lin   uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask;
1159068f8a7eSTamas Berghammer   error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits));
1160b9c1b51eSKate Stone   if (error.Fail())
1161b9c1b51eSKate Stone     return error;
116218fe6404SChaoren Lin 
116318fe6404SChaoren Lin   // clear bits {0-7,16-31} of the debug control register (DR7)
11646a504f6eSChaoren Lin   error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value);
1165b9c1b51eSKate Stone   if (error.Fail())
1166b9c1b51eSKate Stone     return error;
116718fe6404SChaoren Lin   bit_mask = 0xFF | (0xFFFF << 16);
116818fe6404SChaoren Lin   uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
1169068f8a7eSTamas Berghammer   return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits));
117018fe6404SChaoren Lin }
117118fe6404SChaoren Lin 
1172b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::SetHardwareWatchpoint(
1173b9c1b51eSKate Stone     lldb::addr_t addr, size_t size, uint32_t watch_flags) {
1174c16f5dcaSChaoren Lin   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
117518fe6404SChaoren Lin   const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints();
1176b9c1b51eSKate Stone   for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index) {
1177c16f5dcaSChaoren Lin     bool is_vacant;
117897206d57SZachary Turner     Status error = IsWatchpointVacant(wp_index, is_vacant);
1179b9c1b51eSKate Stone     if (is_vacant) {
1180c16f5dcaSChaoren Lin       error = SetHardwareWatchpointWithIndex(addr, size, watch_flags, wp_index);
1181c16f5dcaSChaoren Lin       if (error.Success())
118218fe6404SChaoren Lin         return wp_index;
118318fe6404SChaoren Lin     }
1184b9c1b51eSKate Stone     if (error.Fail() && log) {
1185c16f5dcaSChaoren Lin       log->Printf("NativeRegisterContextLinux_x86_64::%s Error: %s",
1186c16f5dcaSChaoren Lin                   __FUNCTION__, error.AsCString());
1187c16f5dcaSChaoren Lin     }
1188c16f5dcaSChaoren Lin   }
118918fe6404SChaoren Lin   return LLDB_INVALID_INDEX32;
119018fe6404SChaoren Lin }
119118fe6404SChaoren Lin 
119218fe6404SChaoren Lin lldb::addr_t
1193b9c1b51eSKate Stone NativeRegisterContextLinux_x86_64::GetWatchpointAddress(uint32_t wp_index) {
119418fe6404SChaoren Lin   if (wp_index >= NumSupportedHardwareWatchpoints())
119518fe6404SChaoren Lin     return LLDB_INVALID_ADDRESS;
119618fe6404SChaoren Lin   RegisterValue reg_value;
119718fe6404SChaoren Lin   if (ReadRegisterRaw(m_reg_info.first_dr + wp_index, reg_value).Fail())
119818fe6404SChaoren Lin     return LLDB_INVALID_ADDRESS;
119918fe6404SChaoren Lin   return reg_value.GetAsUInt64();
120018fe6404SChaoren Lin }
120118fe6404SChaoren Lin 
1202b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::NumSupportedHardwareWatchpoints() {
120318fe6404SChaoren Lin   // Available debug address registers: dr0, dr1, dr2, dr3
120418fe6404SChaoren Lin   return 4;
120518fe6404SChaoren Lin }
1206068f8a7eSTamas Berghammer 
1207068f8a7eSTamas Berghammer #endif // defined(__i386__) || defined(__x86_64__)
1208