1 //===-- RegisterContextFreeBSD_x86_64.cpp ---------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===---------------------------------------------------------------------===// 8 9 #include "RegisterContextFreeBSD_x86_64.h" 10 #include "RegisterContextFreeBSD_i386.h" 11 #include "RegisterContextPOSIX_x86.h" 12 #include "llvm/Support/Threading.h" 13 #include <vector> 14 15 using namespace lldb_private; 16 using namespace lldb; 17 18 // http://svnweb.freebsd.org/base/head/sys/x86/include/reg.h 19 typedef struct _GPR { 20 uint64_t r15; 21 uint64_t r14; 22 uint64_t r13; 23 uint64_t r12; 24 uint64_t r11; 25 uint64_t r10; 26 uint64_t r9; 27 uint64_t r8; 28 uint64_t rdi; 29 uint64_t rsi; 30 uint64_t rbp; 31 uint64_t rbx; 32 uint64_t rdx; 33 uint64_t rcx; 34 uint64_t rax; 35 uint32_t trapno; 36 uint16_t fs; 37 uint16_t gs; 38 uint32_t err; 39 uint16_t es; 40 uint16_t ds; 41 uint64_t rip; 42 uint64_t cs; 43 uint64_t rflags; 44 uint64_t rsp; 45 uint64_t ss; 46 } GPR; 47 48 struct DBG { 49 uint64_t dr[16]; /* debug registers */ 50 /* Index 0-3: debug address registers */ 51 /* Index 4-5: reserved */ 52 /* Index 6: debug status */ 53 /* Index 7: debug control */ 54 /* Index 8-15: reserved */ 55 }; 56 57 struct UserArea { 58 GPR gpr; 59 FPR fpr; 60 DBG dbg; 61 }; 62 63 #define DR_OFFSET(reg_index) \ 64 (LLVM_EXTENSION offsetof(UserArea, dbg) + \ 65 LLVM_EXTENSION offsetof(DBG, dr[reg_index])) 66 67 // Include RegisterInfos_x86_64 to declare our g_register_infos_x86_64 68 // structure. 69 #define DECLARE_REGISTER_INFOS_X86_64_STRUCT 70 #include "RegisterInfos_x86_64.h" 71 #undef DECLARE_REGISTER_INFOS_X86_64_STRUCT 72 73 static std::vector<lldb_private::RegisterInfo> & GetSharedRegisterInfoVector_i386(const lldb_private::ArchSpec & arch)74GetSharedRegisterInfoVector_i386(const lldb_private::ArchSpec &arch) { 75 static std::vector<lldb_private::RegisterInfo> g_register_infos; 76 static llvm::once_flag g_initialized; 77 llvm::call_once(g_initialized, [&]() { 78 if (g_register_infos.empty()) { 79 // Copy the register information from base class 80 std::unique_ptr<RegisterContextFreeBSD_i386> reg_interface( 81 new RegisterContextFreeBSD_i386(arch)); 82 const RegisterInfo *base_info = reg_interface->GetRegisterInfo(); 83 g_register_infos.insert(g_register_infos.end(), &base_info[0], 84 &base_info[k_num_registers_i386]); 85 86 // Include RegisterInfos_x86_64 to update the g_register_infos structure 87 // with x86_64 offsets. 88 #define UPDATE_REGISTER_INFOS_I386_STRUCT_WITH_X86_64_OFFSETS 89 #include "RegisterInfos_x86_64.h" 90 #undef UPDATE_REGISTER_INFOS_I386_STRUCT_WITH_X86_64_OFFSETS 91 } 92 }); 93 return g_register_infos; 94 } 95 96 static const RegisterInfo * PrivateGetRegisterInfoPtr(const lldb_private::ArchSpec & target_arch)97PrivateGetRegisterInfoPtr(const lldb_private::ArchSpec &target_arch) { 98 switch (target_arch.GetMachine()) { 99 case llvm::Triple::x86: 100 return &GetSharedRegisterInfoVector_i386(target_arch)[0]; 101 case llvm::Triple::x86_64: 102 return g_register_infos_x86_64; 103 default: 104 assert(false && "Unhandled target architecture."); 105 return nullptr; 106 } 107 } 108 109 static uint32_t PrivateGetRegisterCount(const lldb_private::ArchSpec & target_arch)110PrivateGetRegisterCount(const lldb_private::ArchSpec &target_arch) { 111 switch (target_arch.GetMachine()) { 112 case llvm::Triple::x86: 113 // This vector should have already been filled. 114 assert(!GetSharedRegisterInfoVector_i386(target_arch).empty() && 115 "i386 register info vector not filled."); 116 return static_cast<uint32_t>( 117 GetSharedRegisterInfoVector_i386(target_arch).size()); 118 case llvm::Triple::x86_64: 119 return static_cast<uint32_t>(sizeof(g_register_infos_x86_64) / 120 sizeof(g_register_infos_x86_64[0])); 121 default: 122 assert(false && "Unhandled target architecture."); 123 return 0; 124 } 125 } 126 RegisterContextFreeBSD_x86_64(const ArchSpec & target_arch)127RegisterContextFreeBSD_x86_64::RegisterContextFreeBSD_x86_64( 128 const ArchSpec &target_arch) 129 : lldb_private::RegisterInfoInterface(target_arch), 130 m_register_info_p(PrivateGetRegisterInfoPtr(target_arch)), 131 m_register_count(PrivateGetRegisterCount(target_arch)) {} 132 GetGPRSize() const133size_t RegisterContextFreeBSD_x86_64::GetGPRSize() const { return sizeof(GPR); } 134 GetRegisterInfo() const135const RegisterInfo *RegisterContextFreeBSD_x86_64::GetRegisterInfo() const { 136 return m_register_info_p; 137 } 138 GetRegisterCount() const139uint32_t RegisterContextFreeBSD_x86_64::GetRegisterCount() const { 140 return m_register_count; 141 } 142