xref: /llvm-project/lldb/unittests/Process/Utility/RegisterContextNetBSDTest_x86_64.cpp (revision 56440359d093ea6f8e9c91064fdd47928cf07092)
1 //===-- RegisterContextNetBSDTest_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 #if defined(__x86_64__)
10 
11 // clang-format off
12 #include <sys/types.h>
13 #include <amd64/reg.h>
14 // clang-format on
15 
16 #include "gmock/gmock.h"
17 #include "gtest/gtest.h"
18 
19 #include "Plugins/Process/Utility/lldb-x86-register-enums.h"
20 #include "Plugins/Process/Utility/RegisterContextNetBSD_i386.h"
21 #include "Plugins/Process/Utility/RegisterContextNetBSD_x86_64.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
GetRegParams(RegisterInfoInterface & ctx,uint32_t reg)26 static std::pair<size_t, size_t> GetRegParams(RegisterInfoInterface &ctx,
27                                               uint32_t reg) {
28   const RegisterInfo &info = ctx.GetRegisterInfo()[reg];
29   return {info.byte_offset, info.byte_size};
30 }
31 
32 #define EXPECT_OFF(regname, offset, size)                                      \
33   EXPECT_THAT(GetRegParams(reg_ctx, lldb_##regname),                           \
34               ::testing::Pair(offset + base_offset, size))
35 
36 #define EXPECT_GPR_X86_64(regname, regconst)                                   \
37   EXPECT_THAT(                                                                 \
38       GetRegParams(reg_ctx, lldb_##regname##_x86_64),                          \
39       ::testing::Pair(offsetof(reg, regs[regconst]),                           \
40                       sizeof(reg::regs[regconst])))
41 #define EXPECT_DBR_X86_64(num)                                                 \
42   EXPECT_OFF(dr##num##_x86_64, offsetof(dbreg, dr[num]), sizeof(dbreg::dr[num]))
43 
TEST(RegisterContextNetBSDTest,x86_64)44 TEST(RegisterContextNetBSDTest, x86_64) {
45   ArchSpec arch{"x86_64-unknown-netbsd"};
46   RegisterContextNetBSD_x86_64 reg_ctx{arch};
47 
48   EXPECT_GPR_X86_64(rdi, _REG_RDI);
49   EXPECT_GPR_X86_64(rsi, _REG_RSI);
50   EXPECT_GPR_X86_64(rdx, _REG_RDX);
51   EXPECT_GPR_X86_64(rcx, _REG_RCX);
52   EXPECT_GPR_X86_64(r8, _REG_R8);
53   EXPECT_GPR_X86_64(r9, _REG_R9);
54   EXPECT_GPR_X86_64(r10, _REG_R10);
55   EXPECT_GPR_X86_64(r11, _REG_R11);
56   EXPECT_GPR_X86_64(r12, _REG_R12);
57   EXPECT_GPR_X86_64(r13, _REG_R13);
58   EXPECT_GPR_X86_64(r14, _REG_R14);
59   EXPECT_GPR_X86_64(r15, _REG_R15);
60   EXPECT_GPR_X86_64(rbp, _REG_RBP);
61   EXPECT_GPR_X86_64(rbx, _REG_RBX);
62   EXPECT_GPR_X86_64(rax, _REG_RAX);
63   EXPECT_GPR_X86_64(gs, _REG_GS);
64   EXPECT_GPR_X86_64(fs, _REG_FS);
65   EXPECT_GPR_X86_64(es, _REG_ES);
66   EXPECT_GPR_X86_64(ds, _REG_DS);
67   EXPECT_GPR_X86_64(rip, _REG_RIP);
68   EXPECT_GPR_X86_64(cs, _REG_CS);
69   EXPECT_GPR_X86_64(rflags, _REG_RFLAGS);
70   EXPECT_GPR_X86_64(rsp, _REG_RSP);
71   EXPECT_GPR_X86_64(ss, _REG_SS);
72 
73   // fctrl is the first FPR field, it is used to determine offset of the whole
74   // FPR struct
75   size_t base_offset = reg_ctx.GetRegisterInfo()[lldb_fctrl_x86_64].byte_offset;
76 
77   // assert against FXSAVE struct
78   EXPECT_OFF(fctrl_x86_64, 0x00, 2);
79   EXPECT_OFF(fstat_x86_64, 0x02, 2);
80   // TODO: This is a known bug, abridged ftag should is 8 bits in length.
81   EXPECT_OFF(ftag_x86_64, 0x04, 2);
82   EXPECT_OFF(fop_x86_64, 0x06, 2);
83   // NB: Technically fiseg/foseg are 16-bit long and the higher 16 bits
84   // are reserved.  However, LLDB defines them to be 32-bit long for backwards
85   // compatibility, as they were used to reconstruct FIP/FDP before explicit
86   // register entries for them were added.  Also, this is still how GDB does it.
87   EXPECT_OFF(fioff_x86_64, 0x08, 4);
88   EXPECT_OFF(fiseg_x86_64, 0x0C, 4);
89   EXPECT_OFF(fip_x86_64, 0x08, 8);
90   EXPECT_OFF(fooff_x86_64, 0x10, 4);
91   EXPECT_OFF(foseg_x86_64, 0x14, 4);
92   EXPECT_OFF(fdp_x86_64, 0x10, 8);
93   EXPECT_OFF(mxcsr_x86_64, 0x18, 4);
94   EXPECT_OFF(mxcsrmask_x86_64, 0x1C, 4);
95   EXPECT_OFF(st0_x86_64, 0x20, 10);
96   EXPECT_OFF(st1_x86_64, 0x30, 10);
97   EXPECT_OFF(st2_x86_64, 0x40, 10);
98   EXPECT_OFF(st3_x86_64, 0x50, 10);
99   EXPECT_OFF(st4_x86_64, 0x60, 10);
100   EXPECT_OFF(st5_x86_64, 0x70, 10);
101   EXPECT_OFF(st6_x86_64, 0x80, 10);
102   EXPECT_OFF(st7_x86_64, 0x90, 10);
103   EXPECT_OFF(mm0_x86_64, 0x20, 8);
104   EXPECT_OFF(mm1_x86_64, 0x30, 8);
105   EXPECT_OFF(mm2_x86_64, 0x40, 8);
106   EXPECT_OFF(mm3_x86_64, 0x50, 8);
107   EXPECT_OFF(mm4_x86_64, 0x60, 8);
108   EXPECT_OFF(mm5_x86_64, 0x70, 8);
109   EXPECT_OFF(mm6_x86_64, 0x80, 8);
110   EXPECT_OFF(mm7_x86_64, 0x90, 8);
111   EXPECT_OFF(xmm0_x86_64, 0xA0, 16);
112   EXPECT_OFF(xmm1_x86_64, 0xB0, 16);
113   EXPECT_OFF(xmm2_x86_64, 0xC0, 16);
114   EXPECT_OFF(xmm3_x86_64, 0xD0, 16);
115   EXPECT_OFF(xmm4_x86_64, 0xE0, 16);
116   EXPECT_OFF(xmm5_x86_64, 0xF0, 16);
117   EXPECT_OFF(xmm6_x86_64, 0x100, 16);
118   EXPECT_OFF(xmm7_x86_64, 0x110, 16);
119   EXPECT_OFF(xmm8_x86_64, 0x120, 16);
120   EXPECT_OFF(xmm9_x86_64, 0x130, 16);
121   EXPECT_OFF(xmm10_x86_64, 0x140, 16);
122   EXPECT_OFF(xmm11_x86_64, 0x150, 16);
123   EXPECT_OFF(xmm12_x86_64, 0x160, 16);
124   EXPECT_OFF(xmm13_x86_64, 0x170, 16);
125   EXPECT_OFF(xmm14_x86_64, 0x180, 16);
126   EXPECT_OFF(xmm15_x86_64, 0x190, 16);
127 
128   base_offset = reg_ctx.GetRegisterInfo()[lldb_dr0_x86_64].byte_offset;
129   EXPECT_DBR_X86_64(0);
130   EXPECT_DBR_X86_64(1);
131   EXPECT_DBR_X86_64(2);
132   EXPECT_DBR_X86_64(3);
133   EXPECT_DBR_X86_64(4);
134   EXPECT_DBR_X86_64(5);
135   EXPECT_DBR_X86_64(6);
136   EXPECT_DBR_X86_64(7);
137 }
138 
139 #endif // defined(__x86_64__)
140