]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/win32-i386-low.c
[GDBserver] Make Zx/zx packet handling idempotent.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / win32-i386-low.c
1 /* Copyright (C) 2007-2014 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
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.
9
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.
14
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/>. */
17
18 #include "server.h"
19 #include "win32-low.h"
20 #include "i386-low.h"
21
22 #ifndef CONTEXT_EXTENDED_REGISTERS
23 #define CONTEXT_EXTENDED_REGISTERS 0
24 #endif
25
26 #define FCS_REGNUM 27
27 #define FOP_REGNUM 31
28
29 #define FLAG_TRACE_BIT 0x100
30
31 #ifdef __x86_64__
32 /* Defined in auto-generated file reg-amd64.c. */
33 void init_registers_amd64 (void);
34 extern const struct target_desc *tdesc_amd64;
35 #else
36 /* Defined in auto-generated file reg-i386.c. */
37 void init_registers_i386 (void);
38 extern const struct target_desc *tdesc_i386;
39 #endif
40
41 static struct i386_debug_reg_state debug_reg_state;
42
43 static int debug_registers_changed = 0;
44 static int debug_registers_used = 0;
45
46 /* Update the inferior's debug register REGNUM from STATE. */
47
48 void
49 i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
50 {
51 if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
52 fatal ("Invalid debug register %d", regnum);
53
54 /* debug_reg_state.dr_mirror is already set.
55 Just notify i386_set_thread_context, i386_thread_added
56 that the registers need to be updated. */
57 debug_registers_changed = 1;
58 debug_registers_used = 1;
59 }
60
61 CORE_ADDR
62 i386_dr_low_get_addr (int regnum)
63 {
64 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
65
66 return debug_reg_state.dr_mirror[regnum];
67 }
68
69 /* Update the inferior's DR7 debug control register from STATE. */
70
71 void
72 i386_dr_low_set_control (const struct i386_debug_reg_state *state)
73 {
74 /* debug_reg_state.dr_control_mirror is already set.
75 Just notify i386_set_thread_context, i386_thread_added
76 that the registers need to be updated. */
77 debug_registers_changed = 1;
78 debug_registers_used = 1;
79 }
80
81 unsigned
82 i386_dr_low_get_control (void)
83 {
84 return debug_reg_state.dr_control_mirror;
85 }
86
87 /* Get the value of the DR6 debug status register from the inferior
88 and record it in STATE. */
89
90 unsigned
91 i386_dr_low_get_status (void)
92 {
93 /* We don't need to do anything here, the last call to thread_rec for
94 current_event.dwThreadId id has already set it. */
95 return debug_reg_state.dr_status_mirror;
96 }
97
98 /* Breakpoint/watchpoint support. */
99
100 static int
101 i386_supports_z_point_type (char z_type)
102 {
103 switch (z_type)
104 {
105 case Z_PACKET_WRITE_WP:
106 case Z_PACKET_ACCESS_WP:
107 return 1;
108 default:
109 return 0;
110 }
111 }
112
113 static int
114 i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
115 int size, struct raw_breakpoint *bp)
116 {
117 switch (type)
118 {
119 case raw_bkpt_type_write_wp:
120 case raw_bkpt_type_access_wp:
121 {
122 enum target_hw_bp_type hw_type
123 = raw_bkpt_type_to_target_hw_bp_type (type);
124
125 return i386_low_insert_watchpoint (&debug_reg_state,
126 hw_type, addr, size);
127 }
128 default:
129 /* Unsupported. */
130 return 1;
131 }
132 }
133
134 static int
135 i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
136 int size, struct raw_breakpoint *bp)
137 {
138 switch (type)
139 {
140 case raw_bkpt_type_write_wp:
141 case raw_bkpt_type_access_wp:
142 {
143 enum target_hw_bp_type hw_type
144 = raw_bkpt_type_to_target_hw_bp_type (type);
145
146 return i386_low_remove_watchpoint (&debug_reg_state,
147 hw_type, addr, size);
148 }
149 default:
150 /* Unsupported. */
151 return 1;
152 }
153 }
154
155 static int
156 i386_stopped_by_watchpoint (void)
157 {
158 return i386_low_stopped_by_watchpoint (&debug_reg_state);
159 }
160
161 static CORE_ADDR
162 i386_stopped_data_address (void)
163 {
164 CORE_ADDR addr;
165 if (i386_low_stopped_data_address (&debug_reg_state, &addr))
166 return addr;
167 return 0;
168 }
169
170 static void
171 i386_initial_stuff (void)
172 {
173 i386_low_init_dregs (&debug_reg_state);
174 debug_registers_changed = 0;
175 debug_registers_used = 0;
176 }
177
178 static void
179 i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
180 {
181 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
182 the system doesn't support extended registers. */
183 static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS;
184
185 again:
186 th->context.ContextFlags = (CONTEXT_FULL
187 | CONTEXT_FLOATING_POINT
188 | CONTEXT_DEBUG_REGISTERS
189 | extended_registers);
190
191 if (!GetThreadContext (th->h, &th->context))
192 {
193 DWORD e = GetLastError ();
194
195 if (extended_registers && e == ERROR_INVALID_PARAMETER)
196 {
197 extended_registers = 0;
198 goto again;
199 }
200
201 error ("GetThreadContext failure %ld\n", (long) e);
202 }
203
204 debug_registers_changed = 0;
205
206 if (th->tid == current_event->dwThreadId)
207 {
208 /* Copy dr values from the current thread. */
209 struct i386_debug_reg_state *dr = &debug_reg_state;
210 dr->dr_mirror[0] = th->context.Dr0;
211 dr->dr_mirror[1] = th->context.Dr1;
212 dr->dr_mirror[2] = th->context.Dr2;
213 dr->dr_mirror[3] = th->context.Dr3;
214 dr->dr_status_mirror = th->context.Dr6;
215 dr->dr_control_mirror = th->context.Dr7;
216 }
217 }
218
219 static void
220 i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
221 {
222 if (debug_registers_changed)
223 {
224 struct i386_debug_reg_state *dr = &debug_reg_state;
225 th->context.Dr0 = dr->dr_mirror[0];
226 th->context.Dr1 = dr->dr_mirror[1];
227 th->context.Dr2 = dr->dr_mirror[2];
228 th->context.Dr3 = dr->dr_mirror[3];
229 /* th->context.Dr6 = dr->dr_status_mirror;
230 FIXME: should we set dr6 also ?? */
231 th->context.Dr7 = dr->dr_control_mirror;
232 }
233
234 SetThreadContext (th->h, &th->context);
235 }
236
237 static void
238 i386_thread_added (win32_thread_info *th)
239 {
240 /* Set the debug registers for the new thread if they are used. */
241 if (debug_registers_used)
242 {
243 struct i386_debug_reg_state *dr = &debug_reg_state;
244 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
245 GetThreadContext (th->h, &th->context);
246
247 th->context.Dr0 = dr->dr_mirror[0];
248 th->context.Dr1 = dr->dr_mirror[1];
249 th->context.Dr2 = dr->dr_mirror[2];
250 th->context.Dr3 = dr->dr_mirror[3];
251 /* th->context.Dr6 = dr->dr_status_mirror;
252 FIXME: should we set dr6 also ?? */
253 th->context.Dr7 = dr->dr_control_mirror;
254
255 SetThreadContext (th->h, &th->context);
256 th->context.ContextFlags = 0;
257 }
258 }
259
260 static void
261 i386_single_step (win32_thread_info *th)
262 {
263 th->context.EFlags |= FLAG_TRACE_BIT;
264 }
265
266 #ifndef __x86_64__
267
268 /* An array of offset mappings into a Win32 Context structure.
269 This is a one-to-one mapping which is indexed by gdb's register
270 numbers. It retrieves an offset into the context structure where
271 the 4 byte register is located.
272 An offset value of -1 indicates that Win32 does not provide this
273 register in it's CONTEXT structure. In this case regptr will return
274 a pointer into a dummy register. */
275 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
276 static const int mappings[] = {
277 context_offset (Eax),
278 context_offset (Ecx),
279 context_offset (Edx),
280 context_offset (Ebx),
281 context_offset (Esp),
282 context_offset (Ebp),
283 context_offset (Esi),
284 context_offset (Edi),
285 context_offset (Eip),
286 context_offset (EFlags),
287 context_offset (SegCs),
288 context_offset (SegSs),
289 context_offset (SegDs),
290 context_offset (SegEs),
291 context_offset (SegFs),
292 context_offset (SegGs),
293 context_offset (FloatSave.RegisterArea[0 * 10]),
294 context_offset (FloatSave.RegisterArea[1 * 10]),
295 context_offset (FloatSave.RegisterArea[2 * 10]),
296 context_offset (FloatSave.RegisterArea[3 * 10]),
297 context_offset (FloatSave.RegisterArea[4 * 10]),
298 context_offset (FloatSave.RegisterArea[5 * 10]),
299 context_offset (FloatSave.RegisterArea[6 * 10]),
300 context_offset (FloatSave.RegisterArea[7 * 10]),
301 context_offset (FloatSave.ControlWord),
302 context_offset (FloatSave.StatusWord),
303 context_offset (FloatSave.TagWord),
304 context_offset (FloatSave.ErrorSelector),
305 context_offset (FloatSave.ErrorOffset),
306 context_offset (FloatSave.DataSelector),
307 context_offset (FloatSave.DataOffset),
308 context_offset (FloatSave.ErrorSelector),
309 /* XMM0-7 */
310 context_offset (ExtendedRegisters[10 * 16]),
311 context_offset (ExtendedRegisters[11 * 16]),
312 context_offset (ExtendedRegisters[12 * 16]),
313 context_offset (ExtendedRegisters[13 * 16]),
314 context_offset (ExtendedRegisters[14 * 16]),
315 context_offset (ExtendedRegisters[15 * 16]),
316 context_offset (ExtendedRegisters[16 * 16]),
317 context_offset (ExtendedRegisters[17 * 16]),
318 /* MXCSR */
319 context_offset (ExtendedRegisters[24])
320 };
321 #undef context_offset
322
323 #else /* __x86_64__ */
324
325 #define context_offset(x) (offsetof (CONTEXT, x))
326 static const int mappings[] =
327 {
328 context_offset (Rax),
329 context_offset (Rbx),
330 context_offset (Rcx),
331 context_offset (Rdx),
332 context_offset (Rsi),
333 context_offset (Rdi),
334 context_offset (Rbp),
335 context_offset (Rsp),
336 context_offset (R8),
337 context_offset (R9),
338 context_offset (R10),
339 context_offset (R11),
340 context_offset (R12),
341 context_offset (R13),
342 context_offset (R14),
343 context_offset (R15),
344 context_offset (Rip),
345 context_offset (EFlags),
346 context_offset (SegCs),
347 context_offset (SegSs),
348 context_offset (SegDs),
349 context_offset (SegEs),
350 context_offset (SegFs),
351 context_offset (SegGs),
352 context_offset (FloatSave.FloatRegisters[0]),
353 context_offset (FloatSave.FloatRegisters[1]),
354 context_offset (FloatSave.FloatRegisters[2]),
355 context_offset (FloatSave.FloatRegisters[3]),
356 context_offset (FloatSave.FloatRegisters[4]),
357 context_offset (FloatSave.FloatRegisters[5]),
358 context_offset (FloatSave.FloatRegisters[6]),
359 context_offset (FloatSave.FloatRegisters[7]),
360 context_offset (FloatSave.ControlWord),
361 context_offset (FloatSave.StatusWord),
362 context_offset (FloatSave.TagWord),
363 context_offset (FloatSave.ErrorSelector),
364 context_offset (FloatSave.ErrorOffset),
365 context_offset (FloatSave.DataSelector),
366 context_offset (FloatSave.DataOffset),
367 context_offset (FloatSave.ErrorSelector)
368 /* XMM0-7 */ ,
369 context_offset (Xmm0),
370 context_offset (Xmm1),
371 context_offset (Xmm2),
372 context_offset (Xmm3),
373 context_offset (Xmm4),
374 context_offset (Xmm5),
375 context_offset (Xmm6),
376 context_offset (Xmm7),
377 context_offset (Xmm8),
378 context_offset (Xmm9),
379 context_offset (Xmm10),
380 context_offset (Xmm11),
381 context_offset (Xmm12),
382 context_offset (Xmm13),
383 context_offset (Xmm14),
384 context_offset (Xmm15),
385 /* MXCSR */
386 context_offset (FloatSave.MxCsr)
387 };
388 #undef context_offset
389
390 #endif /* __x86_64__ */
391
392 /* Fetch register from gdbserver regcache data. */
393 static void
394 i386_fetch_inferior_register (struct regcache *regcache,
395 win32_thread_info *th, int r)
396 {
397 char *context_offset = (char *) &th->context + mappings[r];
398
399 long l;
400 if (r == FCS_REGNUM)
401 {
402 l = *((long *) context_offset) & 0xffff;
403 supply_register (regcache, r, (char *) &l);
404 }
405 else if (r == FOP_REGNUM)
406 {
407 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
408 supply_register (regcache, r, (char *) &l);
409 }
410 else
411 supply_register (regcache, r, context_offset);
412 }
413
414 /* Store a new register value into the thread context of TH. */
415 static void
416 i386_store_inferior_register (struct regcache *regcache,
417 win32_thread_info *th, int r)
418 {
419 char *context_offset = (char *) &th->context + mappings[r];
420 collect_register (regcache, r, context_offset);
421 }
422
423 static const unsigned char i386_win32_breakpoint = 0xcc;
424 #define i386_win32_breakpoint_len 1
425
426 static void
427 i386_arch_setup (void)
428 {
429 #ifdef __x86_64__
430 init_registers_amd64 ();
431 win32_tdesc = tdesc_amd64;
432 #else
433 init_registers_i386 ();
434 win32_tdesc = tdesc_i386;
435 #endif
436 }
437
438 struct win32_target_ops the_low_target = {
439 i386_arch_setup,
440 sizeof (mappings) / sizeof (mappings[0]),
441 i386_initial_stuff,
442 i386_get_thread_context,
443 i386_set_thread_context,
444 i386_thread_added,
445 i386_fetch_inferior_register,
446 i386_store_inferior_register,
447 i386_single_step,
448 &i386_win32_breakpoint,
449 i386_win32_breakpoint_len,
450 i386_supports_z_point_type,
451 i386_insert_point,
452 i386_remove_point,
453 i386_stopped_by_watchpoint,
454 i386_stopped_data_address
455 };