1 //===-- RegisterContextNetBSD_x86_64.cpp ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "RegisterContextNetBSD_x86_64.h"
10 #include "RegisterContextPOSIX_x86.h"
11 #include "llvm/ADT/Triple.h"
12 #include "llvm/Support/Compiler.h"
13 #include <cassert>
14 #include <cstddef>
15 
16 using namespace lldb_private;
17 using namespace lldb;
18 
19 // src/sys/arch/amd64/include/frame_regs.h
20 typedef struct _GPR {
21   uint64_t rdi;    /*  0 */
22   uint64_t rsi;    /*  1 */
23   uint64_t rdx;    /*  2 */
24   uint64_t rcx;    /*  3 */
25   uint64_t r8;     /*  4 */
26   uint64_t r9;     /*  5 */
27   uint64_t r10;    /*  6 */
28   uint64_t r11;    /*  7 */
29   uint64_t r12;    /*  8 */
30   uint64_t r13;    /*  9 */
31   uint64_t r14;    /* 10 */
32   uint64_t r15;    /* 11 */
33   uint64_t rbp;    /* 12 */
34   uint64_t rbx;    /* 13 */
35   uint64_t rax;    /* 14 */
36   uint64_t gs;     /* 15 */
37   uint64_t fs;     /* 16 */
38   uint64_t es;     /* 17 */
39   uint64_t ds;     /* 18 */
40   uint64_t trapno; /* 19 */
41   uint64_t err;    /* 20 */
42   uint64_t rip;    /* 21 */
43   uint64_t cs;     /* 22 */
44   uint64_t rflags; /* 23 */
45   uint64_t rsp;    /* 24 */
46   uint64_t ss;     /* 25 */
47 } GPR;
48 
49 struct DBG {
50   uint64_t dr[16]; /* debug registers */
51                    /* Index 0-3: debug address registers */
52                    /* Index 4-5: reserved */
53                    /* Index 6: debug status */
54                    /* Index 7: debug control */
55                    /* Index 8-15: reserved */
56 };
57 
58 /*
59  * src/sys/arch/amd64/include/mcontext.h
60  *
61  * typedef struct {
62  *       __gregset_t     __gregs;
63  *       __greg_t        _mc_tlsbase;
64  *       __fpregset_t    __fpregs;
65  * } mcontext_t;
66  */
67 
68 struct UserArea {
69   GPR gpr;
70   uint64_t mc_tlsbase;
71   FPR fpr;
72   DBG dbg;
73 };
74 
75 #define DR_OFFSET(reg_index)                                                   \
76   (LLVM_EXTENSION offsetof(UserArea, dbg) +                                    \
77    LLVM_EXTENSION offsetof(DBG, dr[reg_index]))
78 
79 
80 // Include RegisterInfos_x86_64 to declare our g_register_infos_x86_64
81 // structure.
82 #define DECLARE_REGISTER_INFOS_X86_64_STRUCT
83 #include "RegisterInfos_x86_64.h"
84 #undef DECLARE_REGISTER_INFOS_X86_64_STRUCT
85 
86 static const RegisterInfo *
87 PrivateGetRegisterInfoPtr(const lldb_private::ArchSpec &target_arch) {
88   switch (target_arch.GetMachine()) {
89   case llvm::Triple::x86_64:
90     return g_register_infos_x86_64;
91   default:
92     assert(false && "Unhandled target architecture.");
93     return nullptr;
94   }
95 }
96 
97 static uint32_t
98 PrivateGetRegisterCount(const lldb_private::ArchSpec &target_arch) {
99   switch (target_arch.GetMachine()) {
100   case llvm::Triple::x86_64:
101     return static_cast<uint32_t>(sizeof(g_register_infos_x86_64) /
102                                  sizeof(g_register_infos_x86_64[0]));
103   default:
104     assert(false && "Unhandled target architecture.");
105     return 0;
106   }
107 }
108 
109 RegisterContextNetBSD_x86_64::RegisterContextNetBSD_x86_64(
110     const ArchSpec &target_arch)
111     : lldb_private::RegisterInfoInterface(target_arch),
112       m_register_info_p(PrivateGetRegisterInfoPtr(target_arch)),
113       m_register_count(PrivateGetRegisterCount(target_arch)) {}
114 
115 size_t RegisterContextNetBSD_x86_64::GetGPRSize() const { return sizeof(GPR); }
116 
117 const RegisterInfo *RegisterContextNetBSD_x86_64::GetRegisterInfo() const {
118   return m_register_info_p;
119 }
120 
121 uint32_t RegisterContextNetBSD_x86_64::GetRegisterCount() const {
122   return m_register_count;
123 }
124