gdbserver/Windows: crash during connection establishment phase
[binutils-gdb.git] / gdb / gdbserver / lynx-i386-low.c
blob37c0dc97470c9dd305771e91e5c3c68287285cc4
1 /* Copyright (C) 2010-2018 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "server.h"
19 #include "lynx-low.h"
20 #include <limits.h>
21 #include <sys/ptrace.h>
22 #include "x86-xstate.h"
23 #include "arch/i386.h"
24 #include "x86-tdesc.h"
26 /* The following two typedefs are defined in a .h file which is not
27 in the standard include path (/sys/include/family/x86/ucontext.h),
28 so we just duplicate them here. */
30 /* General register context */
31 typedef struct usr_econtext {
33 uint32_t uec_fault;
34 uint32_t uec_es;
35 uint32_t uec_ds;
36 uint32_t uec_edi;
37 uint32_t uec_esi;
38 uint32_t uec_ebp;
39 uint32_t uec_temp;
40 uint32_t uec_ebx;
41 uint32_t uec_edx;
42 uint32_t uec_ecx;
43 uint32_t uec_eax;
44 uint32_t uec_inum;
45 uint32_t uec_ecode;
46 uint32_t uec_eip;
47 uint32_t uec_cs;
48 uint32_t uec_eflags;
49 uint32_t uec_esp;
50 uint32_t uec_ss;
51 uint32_t uec_fs;
52 uint32_t uec_gs;
53 } usr_econtext_t;
55 /* Floating point and SIMD register context */
56 typedef struct usr_fcontext {
57 uint16_t ufc_control;
58 uint16_t ufc_status;
59 uint16_t ufc_tag;
60 uint16_t ufc_opcode;
61 uint8_t *ufc_inst_off;
62 uint32_t ufc_inst_sel;
63 uint8_t *ufc_data_off;
64 uint32_t ufc_data_sel;
65 uint32_t usse_mxcsr;
66 uint32_t usse_mxcsr_mask;
67 struct ufp387_real {
68 uint16_t umant4;
69 uint16_t umant3;
70 uint16_t umant2;
71 uint16_t umant1;
72 uint16_t us_and_e;
73 uint16_t ureserved_1;
74 uint16_t ureserved_2;
75 uint16_t ureserved_3;
76 } ufc_reg[8];
77 struct uxmm_register {
78 uint16_t uchunk_1;
79 uint16_t uchunk_2;
80 uint16_t uchunk_3;
81 uint16_t uchunk_4;
82 uint16_t uchunk_5;
83 uint16_t uchunk_6;
84 uint16_t uchunk_7;
85 uint16_t uchunk_8;
86 } uxmm_reg[8];
87 char ureserved[16][14];
88 } usr_fcontext_t;
90 /* The index of various registers inside the regcache. */
92 enum lynx_i386_gdb_regnum
94 I386_EAX_REGNUM,
95 I386_ECX_REGNUM,
96 I386_EDX_REGNUM,
97 I386_EBX_REGNUM,
98 I386_ESP_REGNUM,
99 I386_EBP_REGNUM,
100 I386_ESI_REGNUM,
101 I386_EDI_REGNUM,
102 I386_EIP_REGNUM,
103 I386_EFLAGS_REGNUM,
104 I386_CS_REGNUM,
105 I386_SS_REGNUM,
106 I386_DS_REGNUM,
107 I386_ES_REGNUM,
108 I386_FS_REGNUM,
109 I386_GS_REGNUM,
110 I386_ST0_REGNUM,
111 I386_FCTRL_REGNUM = I386_ST0_REGNUM + 8,
112 I386_FSTAT_REGNUM,
113 I386_FTAG_REGNUM,
114 I386_FISEG_REGNUM,
115 I386_FIOFF_REGNUM,
116 I386_FOSEG_REGNUM,
117 I386_FOOFF_REGNUM,
118 I386_FOP_REGNUM,
119 I386_XMM0_REGNUM = 32,
120 I386_MXCSR_REGNUM = I386_XMM0_REGNUM + 8,
121 I386_SENTINEL_REGUM
124 /* The fill_function for the general-purpose register set. */
126 static void
127 lynx_i386_fill_gregset (struct regcache *regcache, char *buf)
129 #define lynx_i386_collect_gp(regnum, fld) \
130 collect_register (regcache, regnum, \
131 buf + offsetof (usr_econtext_t, uec_##fld))
133 lynx_i386_collect_gp (I386_EAX_REGNUM, eax);
134 lynx_i386_collect_gp (I386_ECX_REGNUM, ecx);
135 lynx_i386_collect_gp (I386_EDX_REGNUM, edx);
136 lynx_i386_collect_gp (I386_EBX_REGNUM, ebx);
137 lynx_i386_collect_gp (I386_ESP_REGNUM, esp);
138 lynx_i386_collect_gp (I386_EBP_REGNUM, ebp);
139 lynx_i386_collect_gp (I386_ESI_REGNUM, esi);
140 lynx_i386_collect_gp (I386_EDI_REGNUM, edi);
141 lynx_i386_collect_gp (I386_EIP_REGNUM, eip);
142 lynx_i386_collect_gp (I386_EFLAGS_REGNUM, eflags);
143 lynx_i386_collect_gp (I386_CS_REGNUM, cs);
144 lynx_i386_collect_gp (I386_SS_REGNUM, ss);
145 lynx_i386_collect_gp (I386_DS_REGNUM, ds);
146 lynx_i386_collect_gp (I386_ES_REGNUM, es);
147 lynx_i386_collect_gp (I386_FS_REGNUM, fs);
148 lynx_i386_collect_gp (I386_GS_REGNUM, gs);
151 /* The store_function for the general-purpose register set. */
153 static void
154 lynx_i386_store_gregset (struct regcache *regcache, const char *buf)
156 #define lynx_i386_supply_gp(regnum, fld) \
157 supply_register (regcache, regnum, \
158 buf + offsetof (usr_econtext_t, uec_##fld))
160 lynx_i386_supply_gp (I386_EAX_REGNUM, eax);
161 lynx_i386_supply_gp (I386_ECX_REGNUM, ecx);
162 lynx_i386_supply_gp (I386_EDX_REGNUM, edx);
163 lynx_i386_supply_gp (I386_EBX_REGNUM, ebx);
164 lynx_i386_supply_gp (I386_ESP_REGNUM, esp);
165 lynx_i386_supply_gp (I386_EBP_REGNUM, ebp);
166 lynx_i386_supply_gp (I386_ESI_REGNUM, esi);
167 lynx_i386_supply_gp (I386_EDI_REGNUM, edi);
168 lynx_i386_supply_gp (I386_EIP_REGNUM, eip);
169 lynx_i386_supply_gp (I386_EFLAGS_REGNUM, eflags);
170 lynx_i386_supply_gp (I386_CS_REGNUM, cs);
171 lynx_i386_supply_gp (I386_SS_REGNUM, ss);
172 lynx_i386_supply_gp (I386_DS_REGNUM, ds);
173 lynx_i386_supply_gp (I386_ES_REGNUM, es);
174 lynx_i386_supply_gp (I386_FS_REGNUM, fs);
175 lynx_i386_supply_gp (I386_GS_REGNUM, gs);
178 /* Extract the first 16 bits of register REGNUM in the REGCACHE,
179 and store these 2 bytes at DEST.
181 This is useful to collect certain 16bit registers which are known
182 by GDBserver as 32bit registers (such as the Control Register
183 for instance). */
185 static void
186 collect_16bit_register (struct regcache *regcache, int regnum, char *dest)
188 gdb_byte word[4];
190 collect_register (regcache, regnum, word);
191 memcpy (dest, word, 2);
194 /* The fill_function for the floating-point register set. */
196 static void
197 lynx_i386_fill_fpregset (struct regcache *regcache, char *buf)
199 int i;
201 /* Collect %st0 .. %st7. */
202 for (i = 0; i < 8; i++)
203 collect_register (regcache, I386_ST0_REGNUM + i,
204 buf + offsetof (usr_fcontext_t, ufc_reg)
205 + i * sizeof (struct ufp387_real));
207 /* Collect the other FPU registers. */
208 collect_16bit_register (regcache, I386_FCTRL_REGNUM,
209 buf + offsetof (usr_fcontext_t, ufc_control));
210 collect_16bit_register (regcache, I386_FSTAT_REGNUM,
211 buf + offsetof (usr_fcontext_t, ufc_status));
212 collect_16bit_register (regcache, I386_FTAG_REGNUM,
213 buf + offsetof (usr_fcontext_t, ufc_tag));
214 collect_register (regcache, I386_FISEG_REGNUM,
215 buf + offsetof (usr_fcontext_t, ufc_inst_sel));
216 collect_register (regcache, I386_FIOFF_REGNUM,
217 buf + offsetof (usr_fcontext_t, ufc_inst_off));
218 collect_register (regcache, I386_FOSEG_REGNUM,
219 buf + offsetof (usr_fcontext_t, ufc_data_sel));
220 collect_register (regcache, I386_FOOFF_REGNUM,
221 buf + offsetof (usr_fcontext_t, ufc_data_off));
222 collect_16bit_register (regcache, I386_FOP_REGNUM,
223 buf + offsetof (usr_fcontext_t, ufc_opcode));
225 /* Collect the XMM registers. */
226 for (i = 0; i < 8; i++)
227 collect_register (regcache, I386_XMM0_REGNUM + i,
228 buf + offsetof (usr_fcontext_t, uxmm_reg)
229 + i * sizeof (struct uxmm_register));
230 collect_register (regcache, I386_MXCSR_REGNUM,
231 buf + offsetof (usr_fcontext_t, usse_mxcsr));
234 /* This is the supply counterpart for collect_16bit_register:
235 It extracts a 2byte value from BUF, and uses that value to
236 set REGNUM's value in the regcache.
238 This is useful to supply the value of certain 16bit registers
239 which are known by GDBserver as 32bit registers (such as the Control
240 Register for instance). */
242 static void
243 supply_16bit_register (struct regcache *regcache, int regnum, const char *buf)
245 gdb_byte word[4];
247 memcpy (word, buf, 2);
248 memset (word + 2, 0, 2);
249 supply_register (regcache, regnum, word);
252 /* The store_function for the floating-point register set. */
254 static void
255 lynx_i386_store_fpregset (struct regcache *regcache, const char *buf)
257 int i;
259 /* Store the %st0 .. %st7 registers. */
260 for (i = 0; i < 8; i++)
261 supply_register (regcache, I386_ST0_REGNUM + i,
262 buf + offsetof (usr_fcontext_t, ufc_reg)
263 + i * sizeof (struct ufp387_real));
265 /* Store the other FPU registers. */
266 supply_16bit_register (regcache, I386_FCTRL_REGNUM,
267 buf + offsetof (usr_fcontext_t, ufc_control));
268 supply_16bit_register (regcache, I386_FSTAT_REGNUM,
269 buf + offsetof (usr_fcontext_t, ufc_status));
270 supply_16bit_register (regcache, I386_FTAG_REGNUM,
271 buf + offsetof (usr_fcontext_t, ufc_tag));
272 supply_register (regcache, I386_FISEG_REGNUM,
273 buf + offsetof (usr_fcontext_t, ufc_inst_sel));
274 supply_register (regcache, I386_FIOFF_REGNUM,
275 buf + offsetof (usr_fcontext_t, ufc_inst_off));
276 supply_register (regcache, I386_FOSEG_REGNUM,
277 buf + offsetof (usr_fcontext_t, ufc_data_sel));
278 supply_register (regcache, I386_FOOFF_REGNUM,
279 buf + offsetof (usr_fcontext_t, ufc_data_off));
280 supply_16bit_register (regcache, I386_FOP_REGNUM,
281 buf + offsetof (usr_fcontext_t, ufc_opcode));
283 /* Store the XMM registers. */
284 for (i = 0; i < 8; i++)
285 supply_register (regcache, I386_XMM0_REGNUM + i,
286 buf + offsetof (usr_fcontext_t, uxmm_reg)
287 + i * sizeof (struct uxmm_register));
288 supply_register (regcache, I386_MXCSR_REGNUM,
289 buf + offsetof (usr_fcontext_t, usse_mxcsr));
292 /* Implements the lynx_target_ops.arch_setup routine. */
294 static void
295 lynx_i386_arch_setup (void)
297 struct target_desc *tdesc
298 = i386_create_target_description (X86_XSTATE_SSE_MASK, false);
300 init_target_desc (tdesc, i386_expedite_regs);
302 lynx_tdesc = tdesc;
305 /* Description of all the x86-lynx register sets. */
307 struct lynx_regset_info lynx_target_regsets[] = {
308 /* General Purpose Registers. */
309 {PTRACE_GETREGS, PTRACE_SETREGS, sizeof(usr_econtext_t),
310 lynx_i386_fill_gregset, lynx_i386_store_gregset},
311 /* Floating Point Registers. */
312 { PTRACE_GETFPREGS, PTRACE_SETFPREGS, sizeof(usr_fcontext_t),
313 lynx_i386_fill_fpregset, lynx_i386_store_fpregset },
314 /* End of list marker. */
315 {0, 0, -1, NULL, NULL }
318 /* The lynx_target_ops vector for x86-lynx. */
320 struct lynx_target_ops the_low_target = {
321 lynx_i386_arch_setup,