• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- RegisterContextPOSIX_x86.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 <cstring>
10 #include <errno.h>
11 #include <stdint.h>
12 
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Target/Thread.h"
16 #include "lldb/Utility/DataBufferHeap.h"
17 #include "lldb/Utility/DataExtractor.h"
18 #include "lldb/Utility/Endian.h"
19 #include "lldb/Utility/RegisterValue.h"
20 #include "lldb/Utility/Scalar.h"
21 #include "llvm/Support/Compiler.h"
22 
23 #include "RegisterContextPOSIX_x86.h"
24 #include "RegisterContext_x86.h"
25 
26 using namespace lldb_private;
27 using namespace lldb;
28 
29 const uint32_t g_gpr_regnums_i386[] = {
30     lldb_eax_i386,       lldb_ebx_i386,    lldb_ecx_i386, lldb_edx_i386,
31     lldb_edi_i386,       lldb_esi_i386,    lldb_ebp_i386, lldb_esp_i386,
32     lldb_eip_i386,       lldb_eflags_i386, lldb_cs_i386,  lldb_fs_i386,
33     lldb_gs_i386,        lldb_ss_i386,     lldb_ds_i386,  lldb_es_i386,
34     lldb_ax_i386,        lldb_bx_i386,     lldb_cx_i386,  lldb_dx_i386,
35     lldb_di_i386,        lldb_si_i386,     lldb_bp_i386,  lldb_sp_i386,
36     lldb_ah_i386,        lldb_bh_i386,     lldb_ch_i386,  lldb_dh_i386,
37     lldb_al_i386,        lldb_bl_i386,     lldb_cl_i386,  lldb_dl_i386,
38     LLDB_INVALID_REGNUM, // Register sets must be terminated with
39                          // LLDB_INVALID_REGNUM.
40 };
41 static_assert((sizeof(g_gpr_regnums_i386) / sizeof(g_gpr_regnums_i386[0])) -
42                       1 ==
43                   k_num_gpr_registers_i386,
44               "g_gpr_regnums_i386 has wrong number of register infos");
45 
46 const uint32_t g_lldb_regnums_i386[] = {
47     lldb_fctrl_i386,    lldb_fstat_i386,     lldb_ftag_i386,  lldb_fop_i386,
48     lldb_fiseg_i386,    lldb_fioff_i386,     lldb_foseg_i386, lldb_fooff_i386,
49     lldb_mxcsr_i386,    lldb_mxcsrmask_i386, lldb_st0_i386,   lldb_st1_i386,
50     lldb_st2_i386,      lldb_st3_i386,       lldb_st4_i386,   lldb_st5_i386,
51     lldb_st6_i386,      lldb_st7_i386,       lldb_mm0_i386,   lldb_mm1_i386,
52     lldb_mm2_i386,      lldb_mm3_i386,       lldb_mm4_i386,   lldb_mm5_i386,
53     lldb_mm6_i386,      lldb_mm7_i386,       lldb_xmm0_i386,  lldb_xmm1_i386,
54     lldb_xmm2_i386,     lldb_xmm3_i386,      lldb_xmm4_i386,  lldb_xmm5_i386,
55     lldb_xmm6_i386,     lldb_xmm7_i386,
56     LLDB_INVALID_REGNUM // Register sets must be terminated with
57                         // LLDB_INVALID_REGNUM.
58 };
59 static_assert((sizeof(g_lldb_regnums_i386) / sizeof(g_lldb_regnums_i386[0])) -
60                       1 ==
61                   k_num_fpr_registers_i386,
62               "g_lldb_regnums_i386 has wrong number of register infos");
63 
64 const uint32_t g_avx_regnums_i386[] = {
65     lldb_ymm0_i386,     lldb_ymm1_i386, lldb_ymm2_i386, lldb_ymm3_i386,
66     lldb_ymm4_i386,     lldb_ymm5_i386, lldb_ymm6_i386, lldb_ymm7_i386,
67     LLDB_INVALID_REGNUM // Register sets must be terminated with
68                         // LLDB_INVALID_REGNUM.
69 };
70 static_assert((sizeof(g_avx_regnums_i386) / sizeof(g_avx_regnums_i386[0])) -
71                       1 ==
72                   k_num_avx_registers_i386,
73               " g_avx_regnums_i386 has wrong number of register infos");
74 
75 static const uint32_t g_gpr_regnums_x86_64[] = {
76     lldb_rax_x86_64,    lldb_rbx_x86_64,    lldb_rcx_x86_64, lldb_rdx_x86_64,
77     lldb_rdi_x86_64,    lldb_rsi_x86_64,    lldb_rbp_x86_64, lldb_rsp_x86_64,
78     lldb_r8_x86_64,     lldb_r9_x86_64,     lldb_r10_x86_64, lldb_r11_x86_64,
79     lldb_r12_x86_64,    lldb_r13_x86_64,    lldb_r14_x86_64, lldb_r15_x86_64,
80     lldb_rip_x86_64,    lldb_rflags_x86_64, lldb_cs_x86_64,  lldb_fs_x86_64,
81     lldb_gs_x86_64,     lldb_ss_x86_64,     lldb_ds_x86_64,  lldb_es_x86_64,
82     lldb_eax_x86_64,    lldb_ebx_x86_64,    lldb_ecx_x86_64, lldb_edx_x86_64,
83     lldb_edi_x86_64,    lldb_esi_x86_64,    lldb_ebp_x86_64, lldb_esp_x86_64,
84     lldb_r8d_x86_64,  // Low 32 bits or r8
85     lldb_r9d_x86_64,  // Low 32 bits or r9
86     lldb_r10d_x86_64, // Low 32 bits or r10
87     lldb_r11d_x86_64, // Low 32 bits or r11
88     lldb_r12d_x86_64, // Low 32 bits or r12
89     lldb_r13d_x86_64, // Low 32 bits or r13
90     lldb_r14d_x86_64, // Low 32 bits or r14
91     lldb_r15d_x86_64, // Low 32 bits or r15
92     lldb_ax_x86_64,     lldb_bx_x86_64,     lldb_cx_x86_64,  lldb_dx_x86_64,
93     lldb_di_x86_64,     lldb_si_x86_64,     lldb_bp_x86_64,  lldb_sp_x86_64,
94     lldb_r8w_x86_64,  // Low 16 bits or r8
95     lldb_r9w_x86_64,  // Low 16 bits or r9
96     lldb_r10w_x86_64, // Low 16 bits or r10
97     lldb_r11w_x86_64, // Low 16 bits or r11
98     lldb_r12w_x86_64, // Low 16 bits or r12
99     lldb_r13w_x86_64, // Low 16 bits or r13
100     lldb_r14w_x86_64, // Low 16 bits or r14
101     lldb_r15w_x86_64, // Low 16 bits or r15
102     lldb_ah_x86_64,     lldb_bh_x86_64,     lldb_ch_x86_64,  lldb_dh_x86_64,
103     lldb_al_x86_64,     lldb_bl_x86_64,     lldb_cl_x86_64,  lldb_dl_x86_64,
104     lldb_dil_x86_64,    lldb_sil_x86_64,    lldb_bpl_x86_64, lldb_spl_x86_64,
105     lldb_r8l_x86_64,    // Low 8 bits or r8
106     lldb_r9l_x86_64,    // Low 8 bits or r9
107     lldb_r10l_x86_64,   // Low 8 bits or r10
108     lldb_r11l_x86_64,   // Low 8 bits or r11
109     lldb_r12l_x86_64,   // Low 8 bits or r12
110     lldb_r13l_x86_64,   // Low 8 bits or r13
111     lldb_r14l_x86_64,   // Low 8 bits or r14
112     lldb_r15l_x86_64,   // Low 8 bits or r15
113     LLDB_INVALID_REGNUM // Register sets must be terminated with
114                         // LLDB_INVALID_REGNUM.
115 };
116 static_assert((sizeof(g_gpr_regnums_x86_64) / sizeof(g_gpr_regnums_x86_64[0])) -
117                       1 ==
118                   k_num_gpr_registers_x86_64,
119               "g_gpr_regnums_x86_64 has wrong number of register infos");
120 
121 static const uint32_t g_lldb_regnums_x86_64[] = {
122     lldb_fctrl_x86_64,  lldb_fstat_x86_64, lldb_ftag_x86_64,
123     lldb_fop_x86_64,    lldb_fiseg_x86_64, lldb_fioff_x86_64,
124     lldb_fip_x86_64,    lldb_foseg_x86_64, lldb_fooff_x86_64,
125     lldb_fdp_x86_64,    lldb_mxcsr_x86_64, lldb_mxcsrmask_x86_64,
126     lldb_st0_x86_64,    lldb_st1_x86_64,   lldb_st2_x86_64,
127     lldb_st3_x86_64,    lldb_st4_x86_64,   lldb_st5_x86_64,
128     lldb_st6_x86_64,    lldb_st7_x86_64,   lldb_mm0_x86_64,
129     lldb_mm1_x86_64,    lldb_mm2_x86_64,   lldb_mm3_x86_64,
130     lldb_mm4_x86_64,    lldb_mm5_x86_64,   lldb_mm6_x86_64,
131     lldb_mm7_x86_64,    lldb_xmm0_x86_64,  lldb_xmm1_x86_64,
132     lldb_xmm2_x86_64,   lldb_xmm3_x86_64,  lldb_xmm4_x86_64,
133     lldb_xmm5_x86_64,   lldb_xmm6_x86_64,  lldb_xmm7_x86_64,
134     lldb_xmm8_x86_64,   lldb_xmm9_x86_64,  lldb_xmm10_x86_64,
135     lldb_xmm11_x86_64,  lldb_xmm12_x86_64, lldb_xmm13_x86_64,
136     lldb_xmm14_x86_64,  lldb_xmm15_x86_64,
137     LLDB_INVALID_REGNUM // Register sets must be terminated with
138                         // LLDB_INVALID_REGNUM.
139 };
140 static_assert((sizeof(g_lldb_regnums_x86_64) /
141                sizeof(g_lldb_regnums_x86_64[0])) -
142                       1 ==
143                   k_num_fpr_registers_x86_64,
144               "g_lldb_regnums_x86_64 has wrong number of register infos");
145 
146 static const uint32_t g_avx_regnums_x86_64[] = {
147     lldb_ymm0_x86_64,   lldb_ymm1_x86_64,  lldb_ymm2_x86_64,  lldb_ymm3_x86_64,
148     lldb_ymm4_x86_64,   lldb_ymm5_x86_64,  lldb_ymm6_x86_64,  lldb_ymm7_x86_64,
149     lldb_ymm8_x86_64,   lldb_ymm9_x86_64,  lldb_ymm10_x86_64, lldb_ymm11_x86_64,
150     lldb_ymm12_x86_64,  lldb_ymm13_x86_64, lldb_ymm14_x86_64, lldb_ymm15_x86_64,
151     LLDB_INVALID_REGNUM // Register sets must be terminated with
152                         // LLDB_INVALID_REGNUM.
153 };
154 static_assert((sizeof(g_avx_regnums_x86_64) / sizeof(g_avx_regnums_x86_64[0])) -
155                       1 ==
156                   k_num_avx_registers_x86_64,
157               "g_avx_regnums_x86_64 has wrong number of register infos");
158 
159 uint32_t RegisterContextPOSIX_x86::g_contained_eax[] = {lldb_eax_i386,
160                                                         LLDB_INVALID_REGNUM};
161 uint32_t RegisterContextPOSIX_x86::g_contained_ebx[] = {lldb_ebx_i386,
162                                                         LLDB_INVALID_REGNUM};
163 uint32_t RegisterContextPOSIX_x86::g_contained_ecx[] = {lldb_ecx_i386,
164                                                         LLDB_INVALID_REGNUM};
165 uint32_t RegisterContextPOSIX_x86::g_contained_edx[] = {lldb_edx_i386,
166                                                         LLDB_INVALID_REGNUM};
167 uint32_t RegisterContextPOSIX_x86::g_contained_edi[] = {lldb_edi_i386,
168                                                         LLDB_INVALID_REGNUM};
169 uint32_t RegisterContextPOSIX_x86::g_contained_esi[] = {lldb_esi_i386,
170                                                         LLDB_INVALID_REGNUM};
171 uint32_t RegisterContextPOSIX_x86::g_contained_ebp[] = {lldb_ebp_i386,
172                                                         LLDB_INVALID_REGNUM};
173 uint32_t RegisterContextPOSIX_x86::g_contained_esp[] = {lldb_esp_i386,
174                                                         LLDB_INVALID_REGNUM};
175 
176 uint32_t RegisterContextPOSIX_x86::g_invalidate_eax[] = {
177     lldb_eax_i386, lldb_ax_i386, lldb_ah_i386, lldb_al_i386,
178     LLDB_INVALID_REGNUM};
179 uint32_t RegisterContextPOSIX_x86::g_invalidate_ebx[] = {
180     lldb_ebx_i386, lldb_bx_i386, lldb_bh_i386, lldb_bl_i386,
181     LLDB_INVALID_REGNUM};
182 uint32_t RegisterContextPOSIX_x86::g_invalidate_ecx[] = {
183     lldb_ecx_i386, lldb_cx_i386, lldb_ch_i386, lldb_cl_i386,
184     LLDB_INVALID_REGNUM};
185 uint32_t RegisterContextPOSIX_x86::g_invalidate_edx[] = {
186     lldb_edx_i386, lldb_dx_i386, lldb_dh_i386, lldb_dl_i386,
187     LLDB_INVALID_REGNUM};
188 uint32_t RegisterContextPOSIX_x86::g_invalidate_edi[] = {
189     lldb_edi_i386, lldb_di_i386, LLDB_INVALID_REGNUM};
190 uint32_t RegisterContextPOSIX_x86::g_invalidate_esi[] = {
191     lldb_esi_i386, lldb_si_i386, LLDB_INVALID_REGNUM};
192 uint32_t RegisterContextPOSIX_x86::g_invalidate_ebp[] = {
193     lldb_ebp_i386, lldb_bp_i386, LLDB_INVALID_REGNUM};
194 uint32_t RegisterContextPOSIX_x86::g_invalidate_esp[] = {
195     lldb_esp_i386, lldb_sp_i386, LLDB_INVALID_REGNUM};
196 
197 uint32_t RegisterContextPOSIX_x86::g_contained_rax[] = {lldb_rax_x86_64,
198                                                         LLDB_INVALID_REGNUM};
199 uint32_t RegisterContextPOSIX_x86::g_contained_rbx[] = {lldb_rbx_x86_64,
200                                                         LLDB_INVALID_REGNUM};
201 uint32_t RegisterContextPOSIX_x86::g_contained_rcx[] = {lldb_rcx_x86_64,
202                                                         LLDB_INVALID_REGNUM};
203 uint32_t RegisterContextPOSIX_x86::g_contained_rdx[] = {lldb_rdx_x86_64,
204                                                         LLDB_INVALID_REGNUM};
205 uint32_t RegisterContextPOSIX_x86::g_contained_rdi[] = {lldb_rdi_x86_64,
206                                                         LLDB_INVALID_REGNUM};
207 uint32_t RegisterContextPOSIX_x86::g_contained_rsi[] = {lldb_rsi_x86_64,
208                                                         LLDB_INVALID_REGNUM};
209 uint32_t RegisterContextPOSIX_x86::g_contained_rbp[] = {lldb_rbp_x86_64,
210                                                         LLDB_INVALID_REGNUM};
211 uint32_t RegisterContextPOSIX_x86::g_contained_rsp[] = {lldb_rsp_x86_64,
212                                                         LLDB_INVALID_REGNUM};
213 uint32_t RegisterContextPOSIX_x86::g_contained_r8[] = {lldb_r8_x86_64,
214                                                        LLDB_INVALID_REGNUM};
215 uint32_t RegisterContextPOSIX_x86::g_contained_r9[] = {lldb_r9_x86_64,
216                                                        LLDB_INVALID_REGNUM};
217 uint32_t RegisterContextPOSIX_x86::g_contained_r10[] = {lldb_r10_x86_64,
218                                                         LLDB_INVALID_REGNUM};
219 uint32_t RegisterContextPOSIX_x86::g_contained_r11[] = {lldb_r11_x86_64,
220                                                         LLDB_INVALID_REGNUM};
221 uint32_t RegisterContextPOSIX_x86::g_contained_r12[] = {lldb_r12_x86_64,
222                                                         LLDB_INVALID_REGNUM};
223 uint32_t RegisterContextPOSIX_x86::g_contained_r13[] = {lldb_r13_x86_64,
224                                                         LLDB_INVALID_REGNUM};
225 uint32_t RegisterContextPOSIX_x86::g_contained_r14[] = {lldb_r14_x86_64,
226                                                         LLDB_INVALID_REGNUM};
227 uint32_t RegisterContextPOSIX_x86::g_contained_r15[] = {lldb_r15_x86_64,
228                                                         LLDB_INVALID_REGNUM};
229 
230 uint32_t RegisterContextPOSIX_x86::g_invalidate_rax[] = {
231     lldb_rax_x86_64, lldb_eax_x86_64, lldb_ax_x86_64,
232     lldb_ah_x86_64,  lldb_al_x86_64,  LLDB_INVALID_REGNUM};
233 uint32_t RegisterContextPOSIX_x86::g_invalidate_rbx[] = {
234     lldb_rbx_x86_64, lldb_ebx_x86_64, lldb_bx_x86_64,
235     lldb_bh_x86_64,  lldb_bl_x86_64,  LLDB_INVALID_REGNUM};
236 uint32_t RegisterContextPOSIX_x86::g_invalidate_rcx[] = {
237     lldb_rcx_x86_64, lldb_ecx_x86_64, lldb_cx_x86_64,
238     lldb_ch_x86_64,  lldb_cl_x86_64,  LLDB_INVALID_REGNUM};
239 uint32_t RegisterContextPOSIX_x86::g_invalidate_rdx[] = {
240     lldb_rdx_x86_64, lldb_edx_x86_64, lldb_dx_x86_64,
241     lldb_dh_x86_64,  lldb_dl_x86_64,  LLDB_INVALID_REGNUM};
242 uint32_t RegisterContextPOSIX_x86::g_invalidate_rdi[] = {
243     lldb_rdi_x86_64, lldb_edi_x86_64, lldb_di_x86_64, lldb_dil_x86_64,
244     LLDB_INVALID_REGNUM};
245 uint32_t RegisterContextPOSIX_x86::g_invalidate_rsi[] = {
246     lldb_rsi_x86_64, lldb_esi_x86_64, lldb_si_x86_64, lldb_sil_x86_64,
247     LLDB_INVALID_REGNUM};
248 uint32_t RegisterContextPOSIX_x86::g_invalidate_rbp[] = {
249     lldb_rbp_x86_64, lldb_ebp_x86_64, lldb_bp_x86_64, lldb_bpl_x86_64,
250     LLDB_INVALID_REGNUM};
251 uint32_t RegisterContextPOSIX_x86::g_invalidate_rsp[] = {
252     lldb_rsp_x86_64, lldb_esp_x86_64, lldb_sp_x86_64, lldb_spl_x86_64,
253     LLDB_INVALID_REGNUM};
254 uint32_t RegisterContextPOSIX_x86::g_invalidate_r8[] = {
255     lldb_r8_x86_64, lldb_r8d_x86_64, lldb_r8w_x86_64, lldb_r8l_x86_64,
256     LLDB_INVALID_REGNUM};
257 uint32_t RegisterContextPOSIX_x86::g_invalidate_r9[] = {
258     lldb_r9_x86_64, lldb_r9d_x86_64, lldb_r9w_x86_64, lldb_r9l_x86_64,
259     LLDB_INVALID_REGNUM};
260 uint32_t RegisterContextPOSIX_x86::g_invalidate_r10[] = {
261     lldb_r10_x86_64, lldb_r10d_x86_64, lldb_r10w_x86_64, lldb_r10l_x86_64,
262     LLDB_INVALID_REGNUM};
263 uint32_t RegisterContextPOSIX_x86::g_invalidate_r11[] = {
264     lldb_r11_x86_64, lldb_r11d_x86_64, lldb_r11w_x86_64, lldb_r11l_x86_64,
265     LLDB_INVALID_REGNUM};
266 uint32_t RegisterContextPOSIX_x86::g_invalidate_r12[] = {
267     lldb_r12_x86_64, lldb_r12d_x86_64, lldb_r12w_x86_64, lldb_r12l_x86_64,
268     LLDB_INVALID_REGNUM};
269 uint32_t RegisterContextPOSIX_x86::g_invalidate_r13[] = {
270     lldb_r13_x86_64, lldb_r13d_x86_64, lldb_r13w_x86_64, lldb_r13l_x86_64,
271     LLDB_INVALID_REGNUM};
272 uint32_t RegisterContextPOSIX_x86::g_invalidate_r14[] = {
273     lldb_r14_x86_64, lldb_r14d_x86_64, lldb_r14w_x86_64, lldb_r14l_x86_64,
274     LLDB_INVALID_REGNUM};
275 uint32_t RegisterContextPOSIX_x86::g_invalidate_r15[] = {
276     lldb_r15_x86_64, lldb_r15d_x86_64, lldb_r15w_x86_64, lldb_r15l_x86_64,
277     LLDB_INVALID_REGNUM};
278 
279 uint32_t RegisterContextPOSIX_x86::g_contained_fip[] = {lldb_fip_x86_64,
280                                                         LLDB_INVALID_REGNUM};
281 uint32_t RegisterContextPOSIX_x86::g_contained_fdp[] = {lldb_fdp_x86_64,
282                                                         LLDB_INVALID_REGNUM};
283 
284 uint32_t RegisterContextPOSIX_x86::g_invalidate_fip[] = {
285     lldb_fip_x86_64, lldb_fioff_x86_64, lldb_fiseg_x86_64, LLDB_INVALID_REGNUM};
286 uint32_t RegisterContextPOSIX_x86::g_invalidate_fdp[] = {
287     lldb_fdp_x86_64, lldb_fooff_x86_64, lldb_foseg_x86_64, LLDB_INVALID_REGNUM};
288 
289 uint32_t RegisterContextPOSIX_x86::g_contained_st0_32[] = {lldb_st0_i386,
290                                                            LLDB_INVALID_REGNUM};
291 uint32_t RegisterContextPOSIX_x86::g_contained_st1_32[] = {lldb_st1_i386,
292                                                            LLDB_INVALID_REGNUM};
293 uint32_t RegisterContextPOSIX_x86::g_contained_st2_32[] = {lldb_st2_i386,
294                                                            LLDB_INVALID_REGNUM};
295 uint32_t RegisterContextPOSIX_x86::g_contained_st3_32[] = {lldb_st3_i386,
296                                                            LLDB_INVALID_REGNUM};
297 uint32_t RegisterContextPOSIX_x86::g_contained_st4_32[] = {lldb_st4_i386,
298                                                            LLDB_INVALID_REGNUM};
299 uint32_t RegisterContextPOSIX_x86::g_contained_st5_32[] = {lldb_st5_i386,
300                                                            LLDB_INVALID_REGNUM};
301 uint32_t RegisterContextPOSIX_x86::g_contained_st6_32[] = {lldb_st6_i386,
302                                                            LLDB_INVALID_REGNUM};
303 uint32_t RegisterContextPOSIX_x86::g_contained_st7_32[] = {lldb_st7_i386,
304                                                            LLDB_INVALID_REGNUM};
305 
306 uint32_t RegisterContextPOSIX_x86::g_invalidate_st0_32[] = {
307     lldb_st0_i386, lldb_mm0_i386, LLDB_INVALID_REGNUM};
308 uint32_t RegisterContextPOSIX_x86::g_invalidate_st1_32[] = {
309     lldb_st1_i386, lldb_mm1_i386, LLDB_INVALID_REGNUM};
310 uint32_t RegisterContextPOSIX_x86::g_invalidate_st2_32[] = {
311     lldb_st2_i386, lldb_mm2_i386, LLDB_INVALID_REGNUM};
312 uint32_t RegisterContextPOSIX_x86::g_invalidate_st3_32[] = {
313     lldb_st3_i386, lldb_mm3_i386, LLDB_INVALID_REGNUM};
314 uint32_t RegisterContextPOSIX_x86::g_invalidate_st4_32[] = {
315     lldb_st4_i386, lldb_mm4_i386, LLDB_INVALID_REGNUM};
316 uint32_t RegisterContextPOSIX_x86::g_invalidate_st5_32[] = {
317     lldb_st5_i386, lldb_mm5_i386, LLDB_INVALID_REGNUM};
318 uint32_t RegisterContextPOSIX_x86::g_invalidate_st6_32[] = {
319     lldb_st6_i386, lldb_mm6_i386, LLDB_INVALID_REGNUM};
320 uint32_t RegisterContextPOSIX_x86::g_invalidate_st7_32[] = {
321     lldb_st7_i386, lldb_mm7_i386, LLDB_INVALID_REGNUM};
322 
323 uint32_t RegisterContextPOSIX_x86::g_contained_st0_64[] = {lldb_st0_x86_64,
324                                                            LLDB_INVALID_REGNUM};
325 uint32_t RegisterContextPOSIX_x86::g_contained_st1_64[] = {lldb_st1_x86_64,
326                                                            LLDB_INVALID_REGNUM};
327 uint32_t RegisterContextPOSIX_x86::g_contained_st2_64[] = {lldb_st2_x86_64,
328                                                            LLDB_INVALID_REGNUM};
329 uint32_t RegisterContextPOSIX_x86::g_contained_st3_64[] = {lldb_st3_x86_64,
330                                                            LLDB_INVALID_REGNUM};
331 uint32_t RegisterContextPOSIX_x86::g_contained_st4_64[] = {lldb_st4_x86_64,
332                                                            LLDB_INVALID_REGNUM};
333 uint32_t RegisterContextPOSIX_x86::g_contained_st5_64[] = {lldb_st5_x86_64,
334                                                            LLDB_INVALID_REGNUM};
335 uint32_t RegisterContextPOSIX_x86::g_contained_st6_64[] = {lldb_st6_x86_64,
336                                                            LLDB_INVALID_REGNUM};
337 uint32_t RegisterContextPOSIX_x86::g_contained_st7_64[] = {lldb_st7_x86_64,
338                                                            LLDB_INVALID_REGNUM};
339 
340 uint32_t RegisterContextPOSIX_x86::g_invalidate_st0_64[] = {
341     lldb_st0_x86_64, lldb_mm0_x86_64, LLDB_INVALID_REGNUM};
342 uint32_t RegisterContextPOSIX_x86::g_invalidate_st1_64[] = {
343     lldb_st1_x86_64, lldb_mm1_x86_64, LLDB_INVALID_REGNUM};
344 uint32_t RegisterContextPOSIX_x86::g_invalidate_st2_64[] = {
345     lldb_st2_x86_64, lldb_mm2_x86_64, LLDB_INVALID_REGNUM};
346 uint32_t RegisterContextPOSIX_x86::g_invalidate_st3_64[] = {
347     lldb_st3_x86_64, lldb_mm3_x86_64, LLDB_INVALID_REGNUM};
348 uint32_t RegisterContextPOSIX_x86::g_invalidate_st4_64[] = {
349     lldb_st4_x86_64, lldb_mm4_x86_64, LLDB_INVALID_REGNUM};
350 uint32_t RegisterContextPOSIX_x86::g_invalidate_st5_64[] = {
351     lldb_st5_x86_64, lldb_mm5_x86_64, LLDB_INVALID_REGNUM};
352 uint32_t RegisterContextPOSIX_x86::g_invalidate_st6_64[] = {
353     lldb_st6_x86_64, lldb_mm6_x86_64, LLDB_INVALID_REGNUM};
354 uint32_t RegisterContextPOSIX_x86::g_invalidate_st7_64[] = {
355     lldb_st7_x86_64, lldb_mm7_x86_64, LLDB_INVALID_REGNUM};
356 
357 // Number of register sets provided by this context.
358 enum { k_num_extended_register_sets = 1, k_num_register_sets = 3 };
359 
360 static const RegisterSet g_reg_sets_i386[k_num_register_sets] = {
361     {"General Purpose Registers", "gpr", k_num_gpr_registers_i386,
362      g_gpr_regnums_i386},
363     {"Floating Point Registers", "fpu", k_num_fpr_registers_i386,
364      g_lldb_regnums_i386},
365     {"Advanced Vector Extensions", "avx", k_num_avx_registers_i386,
366      g_avx_regnums_i386}};
367 
368 static const RegisterSet g_reg_sets_x86_64[k_num_register_sets] = {
369     {"General Purpose Registers", "gpr", k_num_gpr_registers_x86_64,
370      g_gpr_regnums_x86_64},
371     {"Floating Point Registers", "fpu", k_num_fpr_registers_x86_64,
372      g_lldb_regnums_x86_64},
373     {"Advanced Vector Extensions", "avx", k_num_avx_registers_x86_64,
374      g_avx_regnums_x86_64}};
375 
IsGPR(unsigned reg)376 bool RegisterContextPOSIX_x86::IsGPR(unsigned reg) {
377   return reg <= m_reg_info.last_gpr; // GPR's come first.
378 }
379 
IsFPR(unsigned reg)380 bool RegisterContextPOSIX_x86::IsFPR(unsigned reg) {
381   return (m_reg_info.first_fpr <= reg && reg <= m_reg_info.last_fpr);
382 }
383 
IsAVX(unsigned reg)384 bool RegisterContextPOSIX_x86::IsAVX(unsigned reg) {
385   return (m_reg_info.first_ymm <= reg && reg <= m_reg_info.last_ymm);
386 }
387 
IsFPR(unsigned reg,FPRType fpr_type)388 bool RegisterContextPOSIX_x86::IsFPR(unsigned reg, FPRType fpr_type) {
389   bool generic_fpr = IsFPR(reg);
390 
391   if (fpr_type == eXSAVE)
392     return generic_fpr || IsAVX(reg);
393   return generic_fpr;
394 }
395 
RegisterContextPOSIX_x86(Thread & thread,uint32_t concrete_frame_idx,RegisterInfoInterface * register_info)396 RegisterContextPOSIX_x86::RegisterContextPOSIX_x86(
397     Thread &thread, uint32_t concrete_frame_idx,
398     RegisterInfoInterface *register_info)
399     : RegisterContext(thread, concrete_frame_idx) {
400   m_register_info_up.reset(register_info);
401 
402   switch (register_info->m_target_arch.GetMachine()) {
403   case llvm::Triple::x86:
404     m_reg_info.num_registers = k_num_registers_i386;
405     m_reg_info.num_gpr_registers = k_num_gpr_registers_i386;
406     m_reg_info.num_fpr_registers = k_num_fpr_registers_i386;
407     m_reg_info.num_avx_registers = k_num_avx_registers_i386;
408     m_reg_info.last_gpr = k_last_gpr_i386;
409     m_reg_info.first_fpr = k_first_fpr_i386;
410     m_reg_info.last_fpr = k_last_fpr_i386;
411     m_reg_info.first_st = lldb_st0_i386;
412     m_reg_info.last_st = lldb_st7_i386;
413     m_reg_info.first_mm = lldb_mm0_i386;
414     m_reg_info.last_mm = lldb_mm7_i386;
415     m_reg_info.first_xmm = lldb_xmm0_i386;
416     m_reg_info.last_xmm = lldb_xmm7_i386;
417     m_reg_info.first_ymm = lldb_ymm0_i386;
418     m_reg_info.last_ymm = lldb_ymm7_i386;
419     m_reg_info.first_dr = lldb_dr0_i386;
420     m_reg_info.gpr_flags = lldb_eflags_i386;
421     break;
422   case llvm::Triple::x86_64:
423     m_reg_info.num_registers = k_num_registers_x86_64;
424     m_reg_info.num_gpr_registers = k_num_gpr_registers_x86_64;
425     m_reg_info.num_fpr_registers = k_num_fpr_registers_x86_64;
426     m_reg_info.num_avx_registers = k_num_avx_registers_x86_64;
427     m_reg_info.last_gpr = k_last_gpr_x86_64;
428     m_reg_info.first_fpr = k_first_fpr_x86_64;
429     m_reg_info.last_fpr = k_last_fpr_x86_64;
430     m_reg_info.first_st = lldb_st0_x86_64;
431     m_reg_info.last_st = lldb_st7_x86_64;
432     m_reg_info.first_mm = lldb_mm0_x86_64;
433     m_reg_info.last_mm = lldb_mm7_x86_64;
434     m_reg_info.first_xmm = lldb_xmm0_x86_64;
435     m_reg_info.last_xmm = lldb_xmm15_x86_64;
436     m_reg_info.first_ymm = lldb_ymm0_x86_64;
437     m_reg_info.last_ymm = lldb_ymm15_x86_64;
438     m_reg_info.first_dr = lldb_dr0_x86_64;
439     m_reg_info.gpr_flags = lldb_rflags_x86_64;
440     break;
441   default:
442     assert(false && "Unhandled target architecture.");
443     break;
444   }
445 
446   ::memset(&m_fpr, 0, sizeof(FPR));
447 
448   m_fpr_type = eNotValid;
449 }
450 
~RegisterContextPOSIX_x86()451 RegisterContextPOSIX_x86::~RegisterContextPOSIX_x86() {}
452 
GetFPRType()453 RegisterContextPOSIX_x86::FPRType RegisterContextPOSIX_x86::GetFPRType() {
454   if (m_fpr_type == eNotValid) {
455     // TODO: Use assembly to call cpuid on the inferior and query ebx or ecx
456     m_fpr_type = eXSAVE; // extended floating-point registers, if available
457     if (!ReadFPR())
458       m_fpr_type = eFXSAVE; // assume generic floating-point registers
459   }
460   return m_fpr_type;
461 }
462 
Invalidate()463 void RegisterContextPOSIX_x86::Invalidate() {}
464 
InvalidateAllRegisters()465 void RegisterContextPOSIX_x86::InvalidateAllRegisters() {}
466 
GetRegisterOffset(unsigned reg)467 unsigned RegisterContextPOSIX_x86::GetRegisterOffset(unsigned reg) {
468   assert(reg < m_reg_info.num_registers && "Invalid register number.");
469   return GetRegisterInfo()[reg].byte_offset;
470 }
471 
GetRegisterSize(unsigned reg)472 unsigned RegisterContextPOSIX_x86::GetRegisterSize(unsigned reg) {
473   assert(reg < m_reg_info.num_registers && "Invalid register number.");
474   return GetRegisterInfo()[reg].byte_size;
475 }
476 
GetRegisterCount()477 size_t RegisterContextPOSIX_x86::GetRegisterCount() {
478   size_t num_registers =
479       m_reg_info.num_gpr_registers + m_reg_info.num_fpr_registers;
480   if (GetFPRType() == eXSAVE)
481     return num_registers + m_reg_info.num_avx_registers;
482   return num_registers;
483 }
484 
GetGPRSize()485 size_t RegisterContextPOSIX_x86::GetGPRSize() {
486   return m_register_info_up->GetGPRSize();
487 }
488 
GetFXSAVEOffset()489 size_t RegisterContextPOSIX_x86::GetFXSAVEOffset() {
490   return GetRegisterInfo()[m_reg_info.first_fpr].byte_offset;
491 }
492 
GetRegisterInfo()493 const RegisterInfo *RegisterContextPOSIX_x86::GetRegisterInfo() {
494   // Commonly, this method is overridden and g_register_infos is copied and
495   // specialized. So, use GetRegisterInfo() rather than g_register_infos in
496   // this scope.
497   return m_register_info_up->GetRegisterInfo();
498 }
499 
500 const RegisterInfo *
GetRegisterInfoAtIndex(size_t reg)501 RegisterContextPOSIX_x86::GetRegisterInfoAtIndex(size_t reg) {
502   if (reg < m_reg_info.num_registers)
503     return &GetRegisterInfo()[reg];
504   else
505     return nullptr;
506 }
507 
GetRegisterSetCount()508 size_t RegisterContextPOSIX_x86::GetRegisterSetCount() {
509   size_t sets = 0;
510   for (size_t set = 0; set < k_num_register_sets; ++set) {
511     if (IsRegisterSetAvailable(set))
512       ++sets;
513   }
514 
515   return sets;
516 }
517 
GetRegisterSet(size_t set)518 const RegisterSet *RegisterContextPOSIX_x86::GetRegisterSet(size_t set) {
519   if (IsRegisterSetAvailable(set)) {
520     switch (m_register_info_up->m_target_arch.GetMachine()) {
521     case llvm::Triple::x86:
522       return &g_reg_sets_i386[set];
523     case llvm::Triple::x86_64:
524       return &g_reg_sets_x86_64[set];
525     default:
526       assert(false && "Unhandled target architecture.");
527       return nullptr;
528     }
529   }
530   return nullptr;
531 }
532 
GetRegisterName(unsigned reg)533 const char *RegisterContextPOSIX_x86::GetRegisterName(unsigned reg) {
534   assert(reg < m_reg_info.num_registers && "Invalid register offset.");
535   return GetRegisterInfo()[reg].name;
536 }
537 
538 // Parse ymm registers and into xmm.bytes and ymmh.bytes.
CopyYMMtoXSTATE(uint32_t reg,lldb::ByteOrder byte_order)539 bool RegisterContextPOSIX_x86::CopyYMMtoXSTATE(uint32_t reg,
540                                                lldb::ByteOrder byte_order) {
541   if (!IsAVX(reg))
542     return false;
543 
544   if (byte_order == eByteOrderLittle) {
545     uint32_t reg_no = reg - m_reg_info.first_ymm;
546     YMMToXState(m_ymm_set.ymm[reg_no],
547         m_fpr.fxsave.xmm[reg_no].bytes,
548         m_fpr.xsave.ymmh[reg_no].bytes);
549     return true;
550   }
551 
552   return false; // unsupported or invalid byte order
553 }
554 
555 // Concatenate xmm.bytes with ymmh.bytes
CopyXSTATEtoYMM(uint32_t reg,lldb::ByteOrder byte_order)556 bool RegisterContextPOSIX_x86::CopyXSTATEtoYMM(uint32_t reg,
557                                                lldb::ByteOrder byte_order) {
558   if (!IsAVX(reg))
559     return false;
560 
561   if (byte_order == eByteOrderLittle) {
562     uint32_t reg_no = reg - m_reg_info.first_ymm;
563     m_ymm_set.ymm[reg_no] = XStateToYMM(
564         m_fpr.fxsave.xmm[reg_no].bytes,
565         m_fpr.xsave.ymmh[reg_no].bytes);
566     return true;
567   }
568 
569   return false; // unsupported or invalid byte order
570 }
571 
IsRegisterSetAvailable(size_t set_index)572 bool RegisterContextPOSIX_x86::IsRegisterSetAvailable(size_t set_index) {
573   // Note: Extended register sets are assumed to be at the end of g_reg_sets...
574   size_t num_sets = k_num_register_sets - k_num_extended_register_sets;
575 
576   if (GetFPRType() == eXSAVE) // ...and to start with AVX registers.
577     ++num_sets;
578   return (set_index < num_sets);
579 }
580