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" 15b9c1b51eSKate Stone #include "lldb/Core/Log.h" 162850b1beSTodd Fiala #include "lldb/Core/RegisterValue.h" 17068f8a7eSTamas Berghammer #include "lldb/Host/HostInfo.h" 18*bf9a7730SZachary Turner #include "lldb/Utility/Error.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 388b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::ReadRegister( 389b9c1b51eSKate Stone const RegisterInfo *reg_info, RegisterValue ®_value) { 3902850b1beSTodd Fiala Error error; 3912850b1beSTodd Fiala 392b9c1b51eSKate Stone if (!reg_info) { 3932850b1beSTodd Fiala error.SetErrorString("reg_info NULL"); 3942850b1beSTodd Fiala return error; 3952850b1beSTodd Fiala } 3962850b1beSTodd Fiala 3972850b1beSTodd Fiala const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB]; 398b9c1b51eSKate Stone if (reg == LLDB_INVALID_REGNUM) { 399b9c1b51eSKate Stone // This is likely an internal register for lldb use only and should not be 400b9c1b51eSKate Stone // directly queried. 401b9c1b51eSKate Stone error.SetErrorStringWithFormat("register \"%s\" is an internal-only lldb " 402b9c1b51eSKate Stone "register, cannot read directly", 403b9c1b51eSKate Stone reg_info->name); 4042850b1beSTodd Fiala return error; 4052850b1beSTodd Fiala } 4062850b1beSTodd Fiala 4075f957b54SValentina Giusti if (IsFPR(reg) || IsAVX(reg) || IsMPX(reg)) { 408068f8a7eSTamas Berghammer error = ReadFPR(); 409068f8a7eSTamas Berghammer if (error.Fail()) 4102850b1beSTodd Fiala return error; 411b9c1b51eSKate Stone } else { 4122850b1beSTodd Fiala uint32_t full_reg = reg; 413b9c1b51eSKate Stone bool is_subreg = reg_info->invalidate_regs && 414b9c1b51eSKate Stone (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM); 4152850b1beSTodd Fiala 416b9c1b51eSKate Stone if (is_subreg) { 4172850b1beSTodd Fiala // Read the full aligned 64-bit register. 4182850b1beSTodd Fiala full_reg = reg_info->invalidate_regs[0]; 4192850b1beSTodd Fiala } 4202850b1beSTodd Fiala 4212850b1beSTodd Fiala error = ReadRegisterRaw(full_reg, reg_value); 4222850b1beSTodd Fiala 423b9c1b51eSKate Stone if (error.Success()) { 424b9c1b51eSKate Stone // If our read was not aligned (for ah,bh,ch,dh), shift our returned value 425b9c1b51eSKate Stone // one byte to the right. 4262850b1beSTodd Fiala if (is_subreg && (reg_info->byte_offset & 0x1)) 4272850b1beSTodd Fiala reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8); 4282850b1beSTodd Fiala 429b9c1b51eSKate Stone // If our return byte size was greater than the return value reg size, 430b9c1b51eSKate Stone // then 4312850b1beSTodd Fiala // use the type specified by reg_info rather than the uint64_t default 4322850b1beSTodd Fiala if (reg_value.GetByteSize() > reg_info->byte_size) 4332850b1beSTodd Fiala reg_value.SetType(reg_info); 4342850b1beSTodd Fiala } 4352850b1beSTodd Fiala return error; 4362850b1beSTodd Fiala } 4372850b1beSTodd Fiala 438b9c1b51eSKate Stone if (reg_info->encoding == lldb::eEncodingVector) { 4392850b1beSTodd Fiala lldb::ByteOrder byte_order = GetByteOrder(); 4402850b1beSTodd Fiala 441b9c1b51eSKate Stone if (byte_order != lldb::eByteOrderInvalid) { 4422850b1beSTodd Fiala if (reg >= m_reg_info.first_st && reg <= m_reg_info.last_st) 443b9c1b51eSKate Stone reg_value.SetBytes( 444b9c1b51eSKate Stone m_fpr.xstate.fxsave.stmm[reg - m_reg_info.first_st].bytes, 445b9c1b51eSKate Stone reg_info->byte_size, byte_order); 4462850b1beSTodd Fiala if (reg >= m_reg_info.first_mm && reg <= m_reg_info.last_mm) 447b9c1b51eSKate Stone reg_value.SetBytes( 448b9c1b51eSKate Stone m_fpr.xstate.fxsave.stmm[reg - m_reg_info.first_mm].bytes, 449b9c1b51eSKate Stone reg_info->byte_size, byte_order); 4502850b1beSTodd Fiala if (reg >= m_reg_info.first_xmm && reg <= m_reg_info.last_xmm) 451b9c1b51eSKate Stone reg_value.SetBytes( 452b9c1b51eSKate Stone m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_xmm].bytes, 453b9c1b51eSKate Stone reg_info->byte_size, byte_order); 454b9c1b51eSKate Stone if (reg >= m_reg_info.first_ymm && reg <= m_reg_info.last_ymm) { 4552850b1beSTodd Fiala // Concatenate ymm using the register halves in xmm.bytes and ymmh.bytes 4565f957b54SValentina Giusti if (CopyXSTATEtoYMM(reg, byte_order)) 457b9c1b51eSKate Stone reg_value.SetBytes(m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, 458b9c1b51eSKate Stone reg_info->byte_size, byte_order); 459b9c1b51eSKate Stone else { 4602850b1beSTodd Fiala error.SetErrorString("failed to copy ymm register value"); 4612850b1beSTodd Fiala return error; 4622850b1beSTodd Fiala } 4632850b1beSTodd Fiala } 464cda0ae46SValentina Giusti if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) { 4655f957b54SValentina Giusti if (CopyXSTATEtoMPX(reg)) 466cda0ae46SValentina Giusti reg_value.SetBytes(m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes, 467cda0ae46SValentina Giusti reg_info->byte_size, byte_order); 468cda0ae46SValentina Giusti else { 469cda0ae46SValentina Giusti error.SetErrorString("failed to copy mpx register value"); 470cda0ae46SValentina Giusti return error; 471cda0ae46SValentina Giusti } 472cda0ae46SValentina Giusti } 473cda0ae46SValentina Giusti if (reg >= m_reg_info.first_mpxc && reg <= m_reg_info.last_mpxc) { 4745f957b54SValentina Giusti if (CopyXSTATEtoMPX(reg)) 475cda0ae46SValentina Giusti reg_value.SetBytes(m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes, 476cda0ae46SValentina Giusti reg_info->byte_size, byte_order); 477cda0ae46SValentina Giusti else { 478cda0ae46SValentina Giusti error.SetErrorString("failed to copy mpx register value"); 479cda0ae46SValentina Giusti return error; 480cda0ae46SValentina Giusti } 481cda0ae46SValentina Giusti } 482ee44a92dSDimitar Vlahovski 4832850b1beSTodd Fiala if (reg_value.GetType() != RegisterValue::eTypeBytes) 484b9c1b51eSKate Stone error.SetErrorString( 485b9c1b51eSKate Stone "write failed - type was expected to be RegisterValue::eTypeBytes"); 4862850b1beSTodd Fiala 4872850b1beSTodd Fiala return error; 4882850b1beSTodd Fiala } 4892850b1beSTodd Fiala 4902850b1beSTodd Fiala error.SetErrorString("byte order is invalid"); 4912850b1beSTodd Fiala return error; 4922850b1beSTodd Fiala } 4932850b1beSTodd Fiala 4942850b1beSTodd Fiala // Get pointer to m_fpr.xstate.fxsave variable and set the data from it. 4957f658eddSAbhishek Aggarwal 4967f658eddSAbhishek Aggarwal // Byte offsets of all registers are calculated wrt 'UserArea' structure. 4977f658eddSAbhishek Aggarwal // However, ReadFPR() reads fpu registers {using ptrace(PTRACE_GETFPREGS,..)} 498b9c1b51eSKate Stone // and stores them in 'm_fpr' (of type FPR structure). To extract values of 499b9c1b51eSKate Stone // fpu 500b9c1b51eSKate Stone // registers, m_fpr should be read at byte offsets calculated wrt to FPR 501b9c1b51eSKate Stone // structure. 5027f658eddSAbhishek Aggarwal 5037f658eddSAbhishek Aggarwal // Since, FPR structure is also one of the member of UserArea structure. 504b9c1b51eSKate Stone // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) - 505b9c1b51eSKate Stone // byte_offset(fctrl wrt UserArea) 5067f658eddSAbhishek Aggarwal assert((reg_info->byte_offset - m_fctrl_offset_in_userarea) < sizeof(m_fpr)); 507b9c1b51eSKate Stone uint8_t *src = 508b9c1b51eSKate Stone (uint8_t *)&m_fpr + reg_info->byte_offset - m_fctrl_offset_in_userarea; 509b9c1b51eSKate Stone switch (reg_info->byte_size) { 510b352a1c8SAbhishek Aggarwal case 1: 511b352a1c8SAbhishek Aggarwal reg_value.SetUInt8(*(uint8_t *)src); 512b352a1c8SAbhishek Aggarwal break; 5132850b1beSTodd Fiala case 2: 5142850b1beSTodd Fiala reg_value.SetUInt16(*(uint16_t *)src); 5152850b1beSTodd Fiala break; 5162850b1beSTodd Fiala case 4: 5172850b1beSTodd Fiala reg_value.SetUInt32(*(uint32_t *)src); 5182850b1beSTodd Fiala break; 5192850b1beSTodd Fiala case 8: 5202850b1beSTodd Fiala reg_value.SetUInt64(*(uint64_t *)src); 5212850b1beSTodd Fiala break; 5222850b1beSTodd Fiala default: 5232850b1beSTodd Fiala assert(false && "Unhandled data size."); 524b9c1b51eSKate Stone error.SetErrorStringWithFormat("unhandled byte size: %" PRIu32, 525b9c1b51eSKate Stone reg_info->byte_size); 5262850b1beSTodd Fiala break; 5272850b1beSTodd Fiala } 5282850b1beSTodd Fiala 5292850b1beSTodd Fiala return error; 5302850b1beSTodd Fiala } 5312850b1beSTodd Fiala 532b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::WriteRegister( 533b9c1b51eSKate Stone const RegisterInfo *reg_info, const RegisterValue ®_value) { 5342850b1beSTodd Fiala assert(reg_info && "reg_info is null"); 5352850b1beSTodd Fiala 5362850b1beSTodd Fiala const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB]; 5372850b1beSTodd Fiala if (reg_index == LLDB_INVALID_REGNUM) 538b9c1b51eSKate Stone return Error("no lldb regnum for %s", reg_info && reg_info->name 539b9c1b51eSKate Stone ? reg_info->name 540b9c1b51eSKate Stone : "<unknown register>"); 5412850b1beSTodd Fiala 5422850b1beSTodd Fiala if (IsGPR(reg_index)) 543068f8a7eSTamas Berghammer return WriteRegisterRaw(reg_index, reg_value); 5442850b1beSTodd Fiala 5455f957b54SValentina Giusti if (IsFPR(reg_index) || IsAVX(reg_index) || IsMPX(reg_index)) { 546b9c1b51eSKate Stone if (reg_info->encoding == lldb::eEncodingVector) { 5472850b1beSTodd Fiala if (reg_index >= m_reg_info.first_st && reg_index <= m_reg_info.last_st) 548b9c1b51eSKate Stone ::memcpy( 549b9c1b51eSKate Stone m_fpr.xstate.fxsave.stmm[reg_index - m_reg_info.first_st].bytes, 550b9c1b51eSKate Stone reg_value.GetBytes(), reg_value.GetByteSize()); 5512850b1beSTodd Fiala 5522850b1beSTodd Fiala if (reg_index >= m_reg_info.first_mm && reg_index <= m_reg_info.last_mm) 553b9c1b51eSKate Stone ::memcpy( 554b9c1b51eSKate Stone m_fpr.xstate.fxsave.stmm[reg_index - m_reg_info.first_mm].bytes, 555b9c1b51eSKate Stone reg_value.GetBytes(), reg_value.GetByteSize()); 5562850b1beSTodd Fiala 5572850b1beSTodd Fiala if (reg_index >= m_reg_info.first_xmm && reg_index <= m_reg_info.last_xmm) 558b9c1b51eSKate Stone ::memcpy( 559b9c1b51eSKate Stone m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_xmm].bytes, 560b9c1b51eSKate Stone reg_value.GetBytes(), reg_value.GetByteSize()); 5612850b1beSTodd Fiala 562b9c1b51eSKate Stone if (reg_index >= m_reg_info.first_ymm && 563b9c1b51eSKate Stone reg_index <= m_reg_info.last_ymm) { 564b9c1b51eSKate Stone // Store ymm register content, and split into the register halves in 565b9c1b51eSKate Stone // xmm.bytes and ymmh.bytes 566b9c1b51eSKate Stone ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes, 567b9c1b51eSKate Stone reg_value.GetBytes(), reg_value.GetByteSize()); 5682850b1beSTodd Fiala if (!CopyYMMtoXSTATE(reg_index, GetByteOrder())) 5692850b1beSTodd Fiala return Error("CopyYMMtoXSTATE() failed"); 5702850b1beSTodd Fiala } 571cda0ae46SValentina Giusti 572cda0ae46SValentina Giusti if (reg_index >= m_reg_info.first_mpxr && 573cda0ae46SValentina Giusti reg_index <= m_reg_info.last_mpxr) { 574cda0ae46SValentina Giusti ::memcpy(m_mpx_set.mpxr[reg_index - m_reg_info.first_mpxr].bytes, 575cda0ae46SValentina Giusti reg_value.GetBytes(), reg_value.GetByteSize()); 576cda0ae46SValentina Giusti if (!CopyMPXtoXSTATE(reg_index)) 577cda0ae46SValentina Giusti return Error("CopyMPXtoXSTATE() failed"); 578cda0ae46SValentina Giusti } 579cda0ae46SValentina Giusti 580cda0ae46SValentina Giusti if (reg_index >= m_reg_info.first_mpxc && 581cda0ae46SValentina Giusti reg_index <= m_reg_info.last_mpxc) { 582cda0ae46SValentina Giusti ::memcpy(m_mpx_set.mpxc[reg_index - m_reg_info.first_mpxc].bytes, 583cda0ae46SValentina Giusti reg_value.GetBytes(), reg_value.GetByteSize()); 584cda0ae46SValentina Giusti if (!CopyMPXtoXSTATE(reg_index)) 585cda0ae46SValentina Giusti return Error("CopyMPXtoXSTATE() failed"); 586cda0ae46SValentina Giusti } 587b9c1b51eSKate Stone } else { 5882850b1beSTodd Fiala // Get pointer to m_fpr.xstate.fxsave variable and set the data to it. 5897f658eddSAbhishek Aggarwal 5907f658eddSAbhishek Aggarwal // Byte offsets of all registers are calculated wrt 'UserArea' structure. 591b9c1b51eSKate Stone // However, WriteFPR() takes m_fpr (of type FPR structure) and writes only 592b9c1b51eSKate Stone // fpu 593b9c1b51eSKate Stone // registers using ptrace(PTRACE_SETFPREGS,..) API. Hence fpu registers 594b9c1b51eSKate Stone // should 5957f658eddSAbhishek Aggarwal // be written in m_fpr at byte offsets calculated wrt FPR structure. 5967f658eddSAbhishek Aggarwal 5977f658eddSAbhishek Aggarwal // Since, FPR structure is also one of the member of UserArea structure. 598b9c1b51eSKate Stone // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) - 599b9c1b51eSKate Stone // byte_offset(fctrl wrt UserArea) 600b9c1b51eSKate Stone assert((reg_info->byte_offset - m_fctrl_offset_in_userarea) < 601b9c1b51eSKate Stone sizeof(m_fpr)); 602b9c1b51eSKate Stone uint8_t *dst = (uint8_t *)&m_fpr + reg_info->byte_offset - 603b9c1b51eSKate Stone m_fctrl_offset_in_userarea; 604b9c1b51eSKate Stone switch (reg_info->byte_size) { 605b352a1c8SAbhishek Aggarwal case 1: 606b352a1c8SAbhishek Aggarwal *(uint8_t *)dst = reg_value.GetAsUInt8(); 607b352a1c8SAbhishek Aggarwal break; 6082850b1beSTodd Fiala case 2: 6092850b1beSTodd Fiala *(uint16_t *)dst = reg_value.GetAsUInt16(); 6102850b1beSTodd Fiala break; 6112850b1beSTodd Fiala case 4: 6122850b1beSTodd Fiala *(uint32_t *)dst = reg_value.GetAsUInt32(); 6132850b1beSTodd Fiala break; 6142850b1beSTodd Fiala case 8: 6152850b1beSTodd Fiala *(uint64_t *)dst = reg_value.GetAsUInt64(); 6162850b1beSTodd Fiala break; 6172850b1beSTodd Fiala default: 6182850b1beSTodd Fiala assert(false && "Unhandled data size."); 619b9c1b51eSKate Stone return Error("unhandled register data size %" PRIu32, 620b9c1b51eSKate Stone reg_info->byte_size); 6212850b1beSTodd Fiala } 6222850b1beSTodd Fiala } 6232850b1beSTodd Fiala 624068f8a7eSTamas Berghammer Error error = WriteFPR(); 625068f8a7eSTamas Berghammer if (error.Fail()) 626068f8a7eSTamas Berghammer return error; 627068f8a7eSTamas Berghammer 628b9c1b51eSKate Stone if (IsAVX(reg_index)) { 6292850b1beSTodd Fiala if (!CopyYMMtoXSTATE(reg_index, GetByteOrder())) 6302850b1beSTodd Fiala return Error("CopyYMMtoXSTATE() failed"); 6312850b1beSTodd Fiala } 632cda0ae46SValentina Giusti 633cda0ae46SValentina Giusti if (IsMPX(reg_index)) { 634cda0ae46SValentina Giusti if (!CopyMPXtoXSTATE(reg_index)) 635cda0ae46SValentina Giusti return Error("CopyMPXtoXSTATE() failed"); 636cda0ae46SValentina Giusti } 637665be50eSMehdi Amini return Error(); 6382850b1beSTodd Fiala } 639b9c1b51eSKate Stone return Error("failed - register wasn't recognized to be a GPR or an FPR, " 640b9c1b51eSKate Stone "write strategy unknown"); 6412850b1beSTodd Fiala } 6422850b1beSTodd Fiala 643b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::ReadAllRegisterValues( 644b9c1b51eSKate Stone lldb::DataBufferSP &data_sp) { 6452850b1beSTodd Fiala Error error; 6462850b1beSTodd Fiala 6472850b1beSTodd Fiala data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0)); 648b9c1b51eSKate Stone if (!data_sp) { 649b9c1b51eSKate Stone error.SetErrorStringWithFormat( 650b9c1b51eSKate Stone "failed to allocate DataBufferHeap instance of size %" PRIu64, 651b9c1b51eSKate Stone REG_CONTEXT_SIZE); 6522850b1beSTodd Fiala return error; 6532850b1beSTodd Fiala } 6542850b1beSTodd Fiala 655068f8a7eSTamas Berghammer error = ReadGPR(); 656068f8a7eSTamas Berghammer if (error.Fail()) 6572850b1beSTodd Fiala return error; 6582850b1beSTodd Fiala 659068f8a7eSTamas Berghammer error = ReadFPR(); 660068f8a7eSTamas Berghammer if (error.Fail()) 6612850b1beSTodd Fiala return error; 6622850b1beSTodd Fiala 6632850b1beSTodd Fiala uint8_t *dst = data_sp->GetBytes(); 664b9c1b51eSKate Stone if (dst == nullptr) { 665b9c1b51eSKate Stone error.SetErrorStringWithFormat("DataBufferHeap instance of size %" PRIu64 666b9c1b51eSKate Stone " returned a null pointer", 667b9c1b51eSKate Stone REG_CONTEXT_SIZE); 6682850b1beSTodd Fiala return error; 6692850b1beSTodd Fiala } 6702850b1beSTodd Fiala 6712850b1beSTodd Fiala ::memcpy(dst, &m_gpr_x86_64, GetRegisterInfoInterface().GetGPRSize()); 6722850b1beSTodd Fiala dst += GetRegisterInfoInterface().GetGPRSize(); 67358db5bb2SValentina Giusti if (m_xstate_type == XStateType::FXSAVE) 6742850b1beSTodd Fiala ::memcpy(dst, &m_fpr.xstate.fxsave, sizeof(m_fpr.xstate.fxsave)); 67558db5bb2SValentina Giusti else if (m_xstate_type == XStateType::XSAVE) { 6762850b1beSTodd Fiala lldb::ByteOrder byte_order = GetByteOrder(); 6772850b1beSTodd Fiala 67897e57e9bSValentina Giusti if (IsCPUFeatureAvailable(RegSet::avx)) { 6792850b1beSTodd Fiala // Assemble the YMM register content from the register halves. 680b9c1b51eSKate Stone for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm; 681b9c1b51eSKate Stone ++reg) { 682b9c1b51eSKate Stone if (!CopyXSTATEtoYMM(reg, byte_order)) { 6835f957b54SValentina Giusti error.SetErrorStringWithFormat( 6845f957b54SValentina Giusti "NativeRegisterContextLinux_x86_64::%s " 685b9c1b51eSKate Stone "CopyXSTATEtoYMM() failed for reg num " 686b9c1b51eSKate Stone "%" PRIu32, 687b9c1b51eSKate Stone __FUNCTION__, reg); 6882850b1beSTodd Fiala return error; 6892850b1beSTodd Fiala } 6902850b1beSTodd Fiala } 6915f957b54SValentina Giusti } 6922850b1beSTodd Fiala 69397e57e9bSValentina Giusti if (IsCPUFeatureAvailable(RegSet::mpx)) { 694cda0ae46SValentina Giusti for (uint32_t reg = m_reg_info.first_mpxr; reg <= m_reg_info.last_mpxc; 695cda0ae46SValentina Giusti ++reg) { 696cda0ae46SValentina Giusti if (!CopyXSTATEtoMPX(reg)) { 6975f957b54SValentina Giusti error.SetErrorStringWithFormat( 6985f957b54SValentina Giusti "NativeRegisterContextLinux_x86_64::%s " 699cda0ae46SValentina Giusti "CopyXSTATEtoMPX() failed for reg num " 700cda0ae46SValentina Giusti "%" PRIu32, 701cda0ae46SValentina Giusti __FUNCTION__, reg); 702cda0ae46SValentina Giusti return error; 703cda0ae46SValentina Giusti } 704cda0ae46SValentina Giusti } 7055f957b54SValentina Giusti } 7062850b1beSTodd Fiala // Copy the extended register state including the assembled ymm registers. 7072850b1beSTodd Fiala ::memcpy(dst, &m_fpr, sizeof(m_fpr)); 708b9c1b51eSKate Stone } else { 7092850b1beSTodd Fiala assert(false && "how do we save the floating point registers?"); 7102850b1beSTodd Fiala error.SetErrorString("unsure how to save the floating point registers"); 7112850b1beSTodd Fiala } 7124778e410SRavitheja Addepally /** The following code is specific to Linux x86 based architectures, 7134778e410SRavitheja Addepally * where the register orig_eax (32 bit)/orig_rax (64 bit) is set to 7144778e410SRavitheja Addepally * -1 to solve the bug 23659, such a setting prevents the automatic 7154778e410SRavitheja Addepally * decrement of the instruction pointer which was causing the SIGILL 7164778e410SRavitheja Addepally * exception. 7174778e410SRavitheja Addepally * **/ 7184778e410SRavitheja Addepally 7194778e410SRavitheja Addepally RegisterValue value((uint64_t)-1); 720b9c1b51eSKate Stone const RegisterInfo *reg_info = 721b9c1b51eSKate Stone GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_eax"); 7224778e410SRavitheja Addepally if (reg_info == nullptr) 7234778e410SRavitheja Addepally reg_info = GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_rax"); 7244778e410SRavitheja Addepally 72564ad85ceSTamas Berghammer if (reg_info != nullptr) 7264778e410SRavitheja Addepally return DoWriteRegisterValue(reg_info->byte_offset, reg_info->name, value); 7272850b1beSTodd Fiala 7282850b1beSTodd Fiala return error; 7292850b1beSTodd Fiala } 7302850b1beSTodd Fiala 731b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::WriteAllRegisterValues( 732b9c1b51eSKate Stone const lldb::DataBufferSP &data_sp) { 7332850b1beSTodd Fiala Error error; 7342850b1beSTodd Fiala 735b9c1b51eSKate Stone if (!data_sp) { 736b9c1b51eSKate Stone error.SetErrorStringWithFormat( 737b9c1b51eSKate Stone "NativeRegisterContextLinux_x86_64::%s invalid data_sp provided", 738b9c1b51eSKate Stone __FUNCTION__); 7392850b1beSTodd Fiala return error; 7402850b1beSTodd Fiala } 7412850b1beSTodd Fiala 742b9c1b51eSKate Stone if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) { 743b9c1b51eSKate Stone error.SetErrorStringWithFormat( 744b9c1b51eSKate Stone "NativeRegisterContextLinux_x86_64::%s data_sp contained mismatched " 745b9c1b51eSKate Stone "data size, expected %" PRIu64 ", actual %" PRIu64, 746b9c1b51eSKate Stone __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize()); 7472850b1beSTodd Fiala return error; 7482850b1beSTodd Fiala } 7492850b1beSTodd Fiala 7502850b1beSTodd Fiala uint8_t *src = data_sp->GetBytes(); 751b9c1b51eSKate Stone if (src == nullptr) { 752b9c1b51eSKate Stone error.SetErrorStringWithFormat("NativeRegisterContextLinux_x86_64::%s " 753b9c1b51eSKate Stone "DataBuffer::GetBytes() returned a null " 754b9c1b51eSKate Stone "pointer", 755b9c1b51eSKate Stone __FUNCTION__); 7562850b1beSTodd Fiala return error; 7572850b1beSTodd Fiala } 7582850b1beSTodd Fiala ::memcpy(&m_gpr_x86_64, src, GetRegisterInfoInterface().GetGPRSize()); 7592850b1beSTodd Fiala 760068f8a7eSTamas Berghammer error = WriteGPR(); 761068f8a7eSTamas Berghammer if (error.Fail()) 7622850b1beSTodd Fiala return error; 7632850b1beSTodd Fiala 7642850b1beSTodd Fiala src += GetRegisterInfoInterface().GetGPRSize(); 76558db5bb2SValentina Giusti if (m_xstate_type == XStateType::FXSAVE) 7662850b1beSTodd Fiala ::memcpy(&m_fpr.xstate.fxsave, src, sizeof(m_fpr.xstate.fxsave)); 76758db5bb2SValentina Giusti else if (m_xstate_type == XStateType::XSAVE) 7682850b1beSTodd Fiala ::memcpy(&m_fpr.xstate.xsave, src, sizeof(m_fpr.xstate.xsave)); 7692850b1beSTodd Fiala 770068f8a7eSTamas Berghammer error = WriteFPR(); 771068f8a7eSTamas Berghammer if (error.Fail()) 7722850b1beSTodd Fiala return error; 7732850b1beSTodd Fiala 77458db5bb2SValentina Giusti if (m_xstate_type == XStateType::XSAVE) { 7752850b1beSTodd Fiala lldb::ByteOrder byte_order = GetByteOrder(); 7762850b1beSTodd Fiala 77797e57e9bSValentina Giusti if (IsCPUFeatureAvailable(RegSet::avx)) { 7782850b1beSTodd Fiala // Parse the YMM register content from the register halves. 779b9c1b51eSKate Stone for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm; 780b9c1b51eSKate Stone ++reg) { 781b9c1b51eSKate Stone if (!CopyYMMtoXSTATE(reg, byte_order)) { 7825f957b54SValentina Giusti error.SetErrorStringWithFormat( 7835f957b54SValentina Giusti "NativeRegisterContextLinux_x86_64::%s " 784b9c1b51eSKate Stone "CopyYMMtoXSTATE() failed for reg num " 785b9c1b51eSKate Stone "%" PRIu32, 786b9c1b51eSKate Stone __FUNCTION__, reg); 7872850b1beSTodd Fiala return error; 7882850b1beSTodd Fiala } 7892850b1beSTodd Fiala } 7905f957b54SValentina Giusti } 791cda0ae46SValentina Giusti 79297e57e9bSValentina Giusti if (IsCPUFeatureAvailable(RegSet::mpx)) { 793cda0ae46SValentina Giusti for (uint32_t reg = m_reg_info.first_mpxr; reg <= m_reg_info.last_mpxc; 794cda0ae46SValentina Giusti ++reg) { 795cda0ae46SValentina Giusti if (!CopyMPXtoXSTATE(reg)) { 7965f957b54SValentina Giusti error.SetErrorStringWithFormat( 7975f957b54SValentina Giusti "NativeRegisterContextLinux_x86_64::%s " 798cda0ae46SValentina Giusti "CopyMPXtoXSTATE() failed for reg num " 799cda0ae46SValentina Giusti "%" PRIu32, 800cda0ae46SValentina Giusti __FUNCTION__, reg); 801cda0ae46SValentina Giusti return error; 802cda0ae46SValentina Giusti } 803cda0ae46SValentina Giusti } 8042850b1beSTodd Fiala } 8055f957b54SValentina Giusti } 8062850b1beSTodd Fiala 8072850b1beSTodd Fiala return error; 8082850b1beSTodd Fiala } 8092850b1beSTodd Fiala 8105f957b54SValentina Giusti bool NativeRegisterContextLinux_x86_64::IsCPUFeatureAvailable( 8115f957b54SValentina Giusti RegSet feature_code) const { 81258db5bb2SValentina Giusti if (m_xstate_type == XStateType::Invalid) { 81358db5bb2SValentina Giusti if (const_cast<NativeRegisterContextLinux_x86_64 *>(this)->ReadFPR().Fail()) 8145f957b54SValentina Giusti return false; 81558db5bb2SValentina Giusti } 8165f957b54SValentina Giusti switch (feature_code) { 81758db5bb2SValentina Giusti case RegSet::gpr: 81858db5bb2SValentina Giusti case RegSet::fpu: 8195f957b54SValentina Giusti return true; 82058db5bb2SValentina Giusti case RegSet::avx: // Check if CPU has AVX and if there is kernel support, by 82158db5bb2SValentina Giusti // reading in the XCR0 area of XSAVE. 82258db5bb2SValentina Giusti if ((m_fpr.xstate.xsave.i387.xcr0 & mask_XSTATE_AVX) == mask_XSTATE_AVX) 8235f957b54SValentina Giusti return true; 82458db5bb2SValentina Giusti break; 82558db5bb2SValentina Giusti case RegSet::mpx: // Check if CPU has MPX and if there is kernel support, by 82658db5bb2SValentina Giusti // reading in the XCR0 area of XSAVE. 82758db5bb2SValentina Giusti if ((m_fpr.xstate.xsave.i387.xcr0 & mask_XSTATE_MPX) == mask_XSTATE_MPX) 82858db5bb2SValentina Giusti return true; 82958db5bb2SValentina Giusti break; 8305f957b54SValentina Giusti } 8315f957b54SValentina Giusti return false; 8325f957b54SValentina Giusti } 8335f957b54SValentina Giusti 834b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsRegisterSetAvailable( 835b9c1b51eSKate Stone uint32_t set_index) const { 8362850b1beSTodd Fiala uint32_t num_sets = k_num_register_sets - k_num_extended_register_sets; 8372850b1beSTodd Fiala 83897e57e9bSValentina Giusti switch (static_cast<RegSet>(set_index)) { 83997e57e9bSValentina Giusti case RegSet::gpr: 84097e57e9bSValentina Giusti case RegSet::fpu: 8412850b1beSTodd Fiala return (set_index < num_sets); 84297e57e9bSValentina Giusti case RegSet::avx: 84397e57e9bSValentina Giusti return IsCPUFeatureAvailable(RegSet::avx); 84497e57e9bSValentina Giusti case RegSet::mpx: 84597e57e9bSValentina Giusti return IsCPUFeatureAvailable(RegSet::mpx); 8465f957b54SValentina Giusti } 84758db5bb2SValentina Giusti return false; 8482850b1beSTodd Fiala } 8492850b1beSTodd Fiala 850b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsGPR(uint32_t reg_index) const { 8512850b1beSTodd Fiala // GPRs come first. 8522850b1beSTodd Fiala return reg_index <= m_reg_info.last_gpr; 8532850b1beSTodd Fiala } 8542850b1beSTodd Fiala 855b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsFPR(uint32_t reg_index) const { 856b9c1b51eSKate Stone return (m_reg_info.first_fpr <= reg_index && 857b9c1b51eSKate Stone reg_index <= m_reg_info.last_fpr); 8582850b1beSTodd Fiala } 8592850b1beSTodd Fiala 860b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::WriteFPR() { 86158db5bb2SValentina Giusti switch (m_xstate_type) { 86297e57e9bSValentina Giusti case XStateType::FXSAVE: 8636ec13991SPavel Labath return WriteRegisterSet( 8646ec13991SPavel Labath &m_iovec, sizeof(m_fpr.xstate.xsave), 8656ec13991SPavel Labath fxsr_regset(GetRegisterInfoInterface().GetTargetArchitecture())); 86697e57e9bSValentina Giusti case XStateType::XSAVE: 867b9c1b51eSKate Stone return WriteRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), 868b9c1b51eSKate Stone NT_X86_XSTATE); 869068f8a7eSTamas Berghammer default: 87058db5bb2SValentina Giusti return Error("Unrecognized FPR type."); 871068f8a7eSTamas Berghammer } 8722850b1beSTodd Fiala } 8732850b1beSTodd Fiala 874b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::IsAVX(uint32_t reg_index) const { 87597e57e9bSValentina Giusti if (!IsCPUFeatureAvailable(RegSet::avx)) 8765f957b54SValentina Giusti return false; 877b9c1b51eSKate Stone return (m_reg_info.first_ymm <= reg_index && 878b9c1b51eSKate Stone reg_index <= m_reg_info.last_ymm); 8792850b1beSTodd Fiala } 8802850b1beSTodd Fiala 881b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::CopyXSTATEtoYMM( 882b9c1b51eSKate Stone uint32_t reg_index, lldb::ByteOrder byte_order) { 8832850b1beSTodd Fiala if (!IsAVX(reg_index)) 8842850b1beSTodd Fiala return false; 8852850b1beSTodd Fiala 886b9c1b51eSKate Stone if (byte_order == lldb::eByteOrderLittle) { 8872850b1beSTodd Fiala ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes, 8882850b1beSTodd Fiala m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes, 8892850b1beSTodd Fiala sizeof(XMMReg)); 890b9c1b51eSKate Stone ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes + 891b9c1b51eSKate Stone sizeof(XMMReg), 8922850b1beSTodd Fiala m_fpr.xstate.xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes, 8932850b1beSTodd Fiala sizeof(YMMHReg)); 8942850b1beSTodd Fiala return true; 8952850b1beSTodd Fiala } 8962850b1beSTodd Fiala 897b9c1b51eSKate Stone if (byte_order == lldb::eByteOrderBig) { 898b9c1b51eSKate Stone ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes + 899b9c1b51eSKate Stone sizeof(XMMReg), 9002850b1beSTodd Fiala m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes, 9012850b1beSTodd Fiala sizeof(XMMReg)); 9022850b1beSTodd Fiala ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes, 9032850b1beSTodd Fiala m_fpr.xstate.xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes, 9042850b1beSTodd Fiala sizeof(YMMHReg)); 9052850b1beSTodd Fiala return true; 9062850b1beSTodd Fiala } 9072850b1beSTodd Fiala return false; // unsupported or invalid byte order 9082850b1beSTodd Fiala } 9092850b1beSTodd Fiala 910b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::CopyYMMtoXSTATE( 911b9c1b51eSKate Stone uint32_t reg, lldb::ByteOrder byte_order) { 9122850b1beSTodd Fiala if (!IsAVX(reg)) 9132850b1beSTodd Fiala return false; 9142850b1beSTodd Fiala 915b9c1b51eSKate Stone if (byte_order == lldb::eByteOrderLittle) { 9162850b1beSTodd Fiala ::memcpy(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_ymm].bytes, 917b9c1b51eSKate Stone m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, sizeof(XMMReg)); 9182850b1beSTodd Fiala ::memcpy(m_fpr.xstate.xsave.ymmh[reg - m_reg_info.first_ymm].bytes, 9192850b1beSTodd Fiala m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg), 9202850b1beSTodd Fiala sizeof(YMMHReg)); 9212850b1beSTodd Fiala return true; 9222850b1beSTodd Fiala } 9232850b1beSTodd Fiala 924b9c1b51eSKate Stone if (byte_order == lldb::eByteOrderBig) { 9252850b1beSTodd Fiala ::memcpy(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_ymm].bytes, 9262850b1beSTodd Fiala m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg), 9272850b1beSTodd Fiala sizeof(XMMReg)); 9282850b1beSTodd Fiala ::memcpy(m_fpr.xstate.xsave.ymmh[reg - m_reg_info.first_ymm].bytes, 929b9c1b51eSKate Stone m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, sizeof(YMMHReg)); 9302850b1beSTodd Fiala return true; 9312850b1beSTodd Fiala } 9322850b1beSTodd Fiala return false; // unsupported or invalid byte order 9332850b1beSTodd Fiala } 9342850b1beSTodd Fiala 935b9c1b51eSKate Stone void *NativeRegisterContextLinux_x86_64::GetFPRBuffer() { 93658db5bb2SValentina Giusti switch (m_xstate_type) { 93797e57e9bSValentina Giusti case XStateType::FXSAVE: 938068f8a7eSTamas Berghammer return &m_fpr.xstate.fxsave; 93997e57e9bSValentina Giusti case XStateType::XSAVE: 940068f8a7eSTamas Berghammer return &m_iovec; 9412850b1beSTodd Fiala default: 942068f8a7eSTamas Berghammer return nullptr; 9432850b1beSTodd Fiala } 9442850b1beSTodd Fiala } 9452850b1beSTodd Fiala 946b9c1b51eSKate Stone size_t NativeRegisterContextLinux_x86_64::GetFPRSize() { 94758db5bb2SValentina Giusti switch (m_xstate_type) { 94897e57e9bSValentina Giusti case XStateType::FXSAVE: 949068f8a7eSTamas Berghammer return sizeof(m_fpr.xstate.fxsave); 95097e57e9bSValentina Giusti case XStateType::XSAVE: 951068f8a7eSTamas Berghammer return sizeof(m_iovec); 952068f8a7eSTamas Berghammer default: 953068f8a7eSTamas Berghammer return 0; 954068f8a7eSTamas Berghammer } 9552850b1beSTodd Fiala } 9562850b1beSTodd Fiala 957b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::ReadFPR() { 95858db5bb2SValentina Giusti Error error; 95958db5bb2SValentina Giusti 96058db5bb2SValentina Giusti // Probe XSAVE and if it is not supported fall back to FXSAVE. 96158db5bb2SValentina Giusti if (m_xstate_type != XStateType::FXSAVE) { 96258db5bb2SValentina Giusti error = 96358db5bb2SValentina Giusti ReadRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), NT_X86_XSTATE); 96458db5bb2SValentina Giusti if (!error.Fail()) { 96558db5bb2SValentina Giusti m_xstate_type = XStateType::XSAVE; 96658db5bb2SValentina Giusti return error; 967296e063dSAbhishek Aggarwal } 968068f8a7eSTamas Berghammer } 9696ec13991SPavel Labath error = ReadRegisterSet( 9706ec13991SPavel Labath &m_iovec, sizeof(m_fpr.xstate.xsave), 9716ec13991SPavel Labath fxsr_regset(GetRegisterInfoInterface().GetTargetArchitecture())); 97258db5bb2SValentina Giusti if (!error.Fail()) { 97358db5bb2SValentina Giusti m_xstate_type = XStateType::FXSAVE; 97458db5bb2SValentina Giusti return error; 97558db5bb2SValentina Giusti } 97658db5bb2SValentina Giusti return Error("Unrecognized FPR type."); 9772850b1beSTodd Fiala } 9782850b1beSTodd Fiala 979cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::IsMPX(uint32_t reg_index) const { 98097e57e9bSValentina Giusti if (!IsCPUFeatureAvailable(RegSet::mpx)) 9815f957b54SValentina Giusti return false; 982cda0ae46SValentina Giusti return (m_reg_info.first_mpxr <= reg_index && 983cda0ae46SValentina Giusti reg_index <= m_reg_info.last_mpxc); 984cda0ae46SValentina Giusti } 985cda0ae46SValentina Giusti 986cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::CopyXSTATEtoMPX(uint32_t reg) { 987cda0ae46SValentina Giusti if (!IsMPX(reg)) 988cda0ae46SValentina Giusti return false; 989cda0ae46SValentina Giusti 990cda0ae46SValentina Giusti if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) { 991cda0ae46SValentina Giusti ::memcpy(m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes, 992cda0ae46SValentina Giusti m_fpr.xstate.xsave.mpxr[reg - m_reg_info.first_mpxr].bytes, 993cda0ae46SValentina Giusti sizeof(MPXReg)); 994cda0ae46SValentina Giusti } else { 995cda0ae46SValentina Giusti ::memcpy(m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes, 996cda0ae46SValentina Giusti m_fpr.xstate.xsave.mpxc[reg - m_reg_info.first_mpxc].bytes, 997cda0ae46SValentina Giusti sizeof(MPXCsr)); 998cda0ae46SValentina Giusti } 999cda0ae46SValentina Giusti return true; 1000cda0ae46SValentina Giusti } 1001cda0ae46SValentina Giusti 1002cda0ae46SValentina Giusti bool NativeRegisterContextLinux_x86_64::CopyMPXtoXSTATE(uint32_t reg) { 1003cda0ae46SValentina Giusti if (!IsMPX(reg)) 1004cda0ae46SValentina Giusti return false; 1005cda0ae46SValentina Giusti 1006cda0ae46SValentina Giusti if (reg >= m_reg_info.first_mpxr && reg <= m_reg_info.last_mpxr) { 1007cda0ae46SValentina Giusti ::memcpy(m_fpr.xstate.xsave.mpxr[reg - m_reg_info.first_mpxr].bytes, 1008cda0ae46SValentina Giusti m_mpx_set.mpxr[reg - m_reg_info.first_mpxr].bytes, sizeof(MPXReg)); 1009cda0ae46SValentina Giusti } else { 1010cda0ae46SValentina Giusti ::memcpy(m_fpr.xstate.xsave.mpxc[reg - m_reg_info.first_mpxc].bytes, 1011cda0ae46SValentina Giusti m_mpx_set.mpxc[reg - m_reg_info.first_mpxc].bytes, sizeof(MPXCsr)); 1012cda0ae46SValentina Giusti } 1013cda0ae46SValentina Giusti return true; 1014cda0ae46SValentina Giusti } 1015cda0ae46SValentina Giusti 1016b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::IsWatchpointHit(uint32_t wp_index, 1017b9c1b51eSKate Stone bool &is_hit) { 101818fe6404SChaoren Lin if (wp_index >= NumSupportedHardwareWatchpoints()) 101918fe6404SChaoren Lin return Error("Watchpoint index out of range"); 102018fe6404SChaoren Lin 102118fe6404SChaoren Lin RegisterValue reg_value; 10226a504f6eSChaoren Lin Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value); 1023b9c1b51eSKate Stone if (error.Fail()) { 1024c16f5dcaSChaoren Lin is_hit = false; 1025c16f5dcaSChaoren Lin return error; 1026c16f5dcaSChaoren Lin } 102718fe6404SChaoren Lin 102818fe6404SChaoren Lin uint64_t status_bits = reg_value.GetAsUInt64(); 102918fe6404SChaoren Lin 1030c16f5dcaSChaoren Lin is_hit = status_bits & (1 << wp_index); 103118fe6404SChaoren Lin 103218fe6404SChaoren Lin return error; 103318fe6404SChaoren Lin } 103418fe6404SChaoren Lin 1035b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::GetWatchpointHitIndex( 1036b9c1b51eSKate Stone uint32_t &wp_index, lldb::addr_t trap_addr) { 1037c16f5dcaSChaoren Lin uint32_t num_hw_wps = NumSupportedHardwareWatchpoints(); 1038b9c1b51eSKate Stone for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) { 1039c16f5dcaSChaoren Lin bool is_hit; 1040c16f5dcaSChaoren Lin Error error = IsWatchpointHit(wp_index, is_hit); 1041c16f5dcaSChaoren Lin if (error.Fail()) { 1042c16f5dcaSChaoren Lin wp_index = LLDB_INVALID_INDEX32; 1043c16f5dcaSChaoren Lin return error; 1044c16f5dcaSChaoren Lin } else if (is_hit) { 1045c16f5dcaSChaoren Lin return error; 1046c16f5dcaSChaoren Lin } 1047c16f5dcaSChaoren Lin } 1048c16f5dcaSChaoren Lin wp_index = LLDB_INVALID_INDEX32; 1049665be50eSMehdi Amini return Error(); 1050c16f5dcaSChaoren Lin } 1051c16f5dcaSChaoren Lin 1052b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::IsWatchpointVacant(uint32_t wp_index, 1053b9c1b51eSKate Stone bool &is_vacant) { 105418fe6404SChaoren Lin if (wp_index >= NumSupportedHardwareWatchpoints()) 105518fe6404SChaoren Lin return Error("Watchpoint index out of range"); 105618fe6404SChaoren Lin 105718fe6404SChaoren Lin RegisterValue reg_value; 10586a504f6eSChaoren Lin Error error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value); 1059b9c1b51eSKate Stone if (error.Fail()) { 1060c16f5dcaSChaoren Lin is_vacant = false; 1061c16f5dcaSChaoren Lin return error; 1062c16f5dcaSChaoren Lin } 106318fe6404SChaoren Lin 106418fe6404SChaoren Lin uint64_t control_bits = reg_value.GetAsUInt64(); 106518fe6404SChaoren Lin 1066c16f5dcaSChaoren Lin is_vacant = !(control_bits & (1 << (2 * wp_index))); 106718fe6404SChaoren Lin 106818fe6404SChaoren Lin return error; 106918fe6404SChaoren Lin } 107018fe6404SChaoren Lin 1071b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::SetHardwareWatchpointWithIndex( 107218fe6404SChaoren Lin lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index) { 107318fe6404SChaoren Lin 107418fe6404SChaoren Lin if (wp_index >= NumSupportedHardwareWatchpoints()) 107518fe6404SChaoren Lin return Error("Watchpoint index out of range"); 107618fe6404SChaoren Lin 1077b9c1b51eSKate Stone // Read only watchpoints aren't supported on x86_64. Fall back to read/write 1078b9c1b51eSKate Stone // waitchpoints instead. 1079b9c1b51eSKate Stone // TODO: Add logic to detect when a write happens and ignore that watchpoint 1080b9c1b51eSKate Stone // hit. 1081cf8eb9daSOmair Javaid if (watch_flags == 0x2) 108240caea63STamas Berghammer watch_flags = 0x3; 1083cf8eb9daSOmair Javaid 108418fe6404SChaoren Lin if (watch_flags != 0x1 && watch_flags != 0x3) 108518fe6404SChaoren Lin return Error("Invalid read/write bits for watchpoint"); 108618fe6404SChaoren Lin 108718fe6404SChaoren Lin if (size != 1 && size != 2 && size != 4 && size != 8) 108818fe6404SChaoren Lin return Error("Invalid size for watchpoint"); 108918fe6404SChaoren Lin 1090c16f5dcaSChaoren Lin bool is_vacant; 1091c16f5dcaSChaoren Lin Error error = IsWatchpointVacant(wp_index, is_vacant); 1092b9c1b51eSKate Stone if (error.Fail()) 1093b9c1b51eSKate Stone return error; 1094b9c1b51eSKate Stone if (!is_vacant) 1095b9c1b51eSKate Stone return Error("Watchpoint index not vacant"); 109618fe6404SChaoren Lin 109718fe6404SChaoren Lin RegisterValue reg_value; 10986a504f6eSChaoren Lin error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value); 1099b9c1b51eSKate Stone if (error.Fail()) 1100b9c1b51eSKate Stone return error; 110118fe6404SChaoren Lin 110218fe6404SChaoren Lin // for watchpoints 0, 1, 2, or 3, respectively, 110318fe6404SChaoren Lin // set bits 1, 3, 5, or 7 110418fe6404SChaoren Lin uint64_t enable_bit = 1 << (2 * wp_index); 110518fe6404SChaoren Lin 110618fe6404SChaoren Lin // set bits 16-17, 20-21, 24-25, or 28-29 110718fe6404SChaoren Lin // with 0b01 for write, and 0b11 for read/write 110818fe6404SChaoren Lin uint64_t rw_bits = watch_flags << (16 + 4 * wp_index); 110918fe6404SChaoren Lin 111018fe6404SChaoren Lin // set bits 18-19, 22-23, 26-27, or 30-31 111118fe6404SChaoren Lin // with 0b00, 0b01, 0b10, or 0b11 111218fe6404SChaoren Lin // for 1, 2, 8 (if supported), or 4 bytes, respectively 111318fe6404SChaoren Lin uint64_t size_bits = (size == 8 ? 0x2 : size - 1) << (18 + 4 * wp_index); 111418fe6404SChaoren Lin 111518fe6404SChaoren Lin uint64_t bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index)); 111618fe6404SChaoren Lin 111718fe6404SChaoren Lin uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; 111818fe6404SChaoren Lin 111918fe6404SChaoren Lin control_bits |= enable_bit | rw_bits | size_bits; 112018fe6404SChaoren Lin 1121068f8a7eSTamas Berghammer error = WriteRegisterRaw(m_reg_info.first_dr + wp_index, RegisterValue(addr)); 1122b9c1b51eSKate Stone if (error.Fail()) 1123b9c1b51eSKate Stone return error; 112418fe6404SChaoren Lin 1125b9c1b51eSKate Stone error = 1126b9c1b51eSKate Stone WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits)); 1127b9c1b51eSKate Stone if (error.Fail()) 1128b9c1b51eSKate Stone return error; 112918fe6404SChaoren Lin 113018fe6404SChaoren Lin error.Clear(); 113118fe6404SChaoren Lin return error; 113218fe6404SChaoren Lin } 113318fe6404SChaoren Lin 1134b9c1b51eSKate Stone bool NativeRegisterContextLinux_x86_64::ClearHardwareWatchpoint( 1135b9c1b51eSKate Stone uint32_t wp_index) { 113618fe6404SChaoren Lin if (wp_index >= NumSupportedHardwareWatchpoints()) 113718fe6404SChaoren Lin return false; 113818fe6404SChaoren Lin 113918fe6404SChaoren Lin RegisterValue reg_value; 114018fe6404SChaoren Lin 114118fe6404SChaoren Lin // for watchpoints 0, 1, 2, or 3, respectively, 114218fe6404SChaoren Lin // clear bits 0, 1, 2, or 3 of the debug status register (DR6) 11436a504f6eSChaoren Lin Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value); 1144b9c1b51eSKate Stone if (error.Fail()) 1145b9c1b51eSKate Stone return false; 114618fe6404SChaoren Lin uint64_t bit_mask = 1 << wp_index; 114718fe6404SChaoren Lin uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask; 1148068f8a7eSTamas Berghammer error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits)); 1149b9c1b51eSKate Stone if (error.Fail()) 1150b9c1b51eSKate Stone return false; 115118fe6404SChaoren Lin 115218fe6404SChaoren Lin // for watchpoints 0, 1, 2, or 3, respectively, 115318fe6404SChaoren Lin // clear bits {0-1,16-19}, {2-3,20-23}, {4-5,24-27}, or {6-7,28-31} 115418fe6404SChaoren Lin // of the debug control register (DR7) 11556a504f6eSChaoren Lin error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value); 1156b9c1b51eSKate Stone if (error.Fail()) 1157b9c1b51eSKate Stone return false; 115818fe6404SChaoren Lin bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index)); 115918fe6404SChaoren Lin uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; 1160b9c1b51eSKate Stone return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits)) 1161b9c1b51eSKate Stone .Success(); 116218fe6404SChaoren Lin } 116318fe6404SChaoren Lin 1164b9c1b51eSKate Stone Error NativeRegisterContextLinux_x86_64::ClearAllHardwareWatchpoints() { 116518fe6404SChaoren Lin RegisterValue reg_value; 116618fe6404SChaoren Lin 116718fe6404SChaoren Lin // clear bits {0-4} of the debug status register (DR6) 11686a504f6eSChaoren Lin Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value); 1169b9c1b51eSKate Stone if (error.Fail()) 1170b9c1b51eSKate Stone return error; 117118fe6404SChaoren Lin uint64_t bit_mask = 0xF; 117218fe6404SChaoren Lin uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask; 1173068f8a7eSTamas Berghammer error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits)); 1174b9c1b51eSKate Stone if (error.Fail()) 1175b9c1b51eSKate Stone return error; 117618fe6404SChaoren Lin 117718fe6404SChaoren Lin // clear bits {0-7,16-31} of the debug control register (DR7) 11786a504f6eSChaoren Lin error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value); 1179b9c1b51eSKate Stone if (error.Fail()) 1180b9c1b51eSKate Stone return error; 118118fe6404SChaoren Lin bit_mask = 0xFF | (0xFFFF << 16); 118218fe6404SChaoren Lin uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; 1183068f8a7eSTamas Berghammer return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits)); 118418fe6404SChaoren Lin } 118518fe6404SChaoren Lin 1186b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::SetHardwareWatchpoint( 1187b9c1b51eSKate Stone lldb::addr_t addr, size_t size, uint32_t watch_flags) { 1188c16f5dcaSChaoren Lin Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 118918fe6404SChaoren Lin const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints(); 1190b9c1b51eSKate Stone for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index) { 1191c16f5dcaSChaoren Lin bool is_vacant; 1192c16f5dcaSChaoren Lin Error error = IsWatchpointVacant(wp_index, is_vacant); 1193b9c1b51eSKate Stone if (is_vacant) { 1194c16f5dcaSChaoren Lin error = SetHardwareWatchpointWithIndex(addr, size, watch_flags, wp_index); 1195c16f5dcaSChaoren Lin if (error.Success()) 119618fe6404SChaoren Lin return wp_index; 119718fe6404SChaoren Lin } 1198b9c1b51eSKate Stone if (error.Fail() && log) { 1199c16f5dcaSChaoren Lin log->Printf("NativeRegisterContextLinux_x86_64::%s Error: %s", 1200c16f5dcaSChaoren Lin __FUNCTION__, error.AsCString()); 1201c16f5dcaSChaoren Lin } 1202c16f5dcaSChaoren Lin } 120318fe6404SChaoren Lin return LLDB_INVALID_INDEX32; 120418fe6404SChaoren Lin } 120518fe6404SChaoren Lin 120618fe6404SChaoren Lin lldb::addr_t 1207b9c1b51eSKate Stone NativeRegisterContextLinux_x86_64::GetWatchpointAddress(uint32_t wp_index) { 120818fe6404SChaoren Lin if (wp_index >= NumSupportedHardwareWatchpoints()) 120918fe6404SChaoren Lin return LLDB_INVALID_ADDRESS; 121018fe6404SChaoren Lin RegisterValue reg_value; 121118fe6404SChaoren Lin if (ReadRegisterRaw(m_reg_info.first_dr + wp_index, reg_value).Fail()) 121218fe6404SChaoren Lin return LLDB_INVALID_ADDRESS; 121318fe6404SChaoren Lin return reg_value.GetAsUInt64(); 121418fe6404SChaoren Lin } 121518fe6404SChaoren Lin 1216b9c1b51eSKate Stone uint32_t NativeRegisterContextLinux_x86_64::NumSupportedHardwareWatchpoints() { 121718fe6404SChaoren Lin // Available debug address registers: dr0, dr1, dr2, dr3 121818fe6404SChaoren Lin return 4; 121918fe6404SChaoren Lin } 1220068f8a7eSTamas Berghammer 1221068f8a7eSTamas Berghammer #endif // defined(__i386__) || defined(__x86_64__) 1222