Fix gdbserver cross build.
[deliverable/binutils-gdb.git] / gdb / gdbserver / win32-i386-low.c
CommitLineData
ecd75fc8 1/* Copyright (C) 2007-2014 Free Software Foundation, Inc.
68070c10
PA
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
a9762ec7 7 the Free Software Foundation; either version 3 of the License, or
68070c10
PA
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
a9762ec7 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
68070c10
PA
17
18#include "server.h"
19#include "win32-low.h"
aa5ca48f 20#include "i386-low.h"
68070c10 21
54709339
PM
22#ifndef CONTEXT_EXTENDED_REGISTERS
23#define CONTEXT_EXTENDED_REGISTERS 0
24#endif
25
68070c10
PA
26#define FCS_REGNUM 27
27#define FOP_REGNUM 31
28
29#define FLAG_TRACE_BIT 0x100
30
1c07cc19
PM
31#ifdef __x86_64__
32/* Defined in auto-generated file reg-amd64.c. */
54709339 33void init_registers_amd64 (void);
3aee8918 34extern const struct target_desc *tdesc_amd64;
54709339 35#else
d05b4ac3
UW
36/* Defined in auto-generated file reg-i386.c. */
37void init_registers_i386 (void);
3aee8918 38extern const struct target_desc *tdesc_i386;
54709339 39#endif
d05b4ac3 40
aa5ca48f 41static struct i386_debug_reg_state debug_reg_state;
68070c10 42
34b34921
PA
43static int debug_registers_changed = 0;
44static int debug_registers_used = 0;
45
aa5ca48f
DE
46/* Update the inferior's debug register REGNUM from STATE. */
47
48void
49i386_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
964e4306
PA
61CORE_ADDR
62i386_dr_low_get_addr (int regnum)
63{
0a5b1e09 64 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
964e4306
PA
65
66 return debug_reg_state.dr_mirror[regnum];
67}
68
aa5ca48f
DE
69/* Update the inferior's DR7 debug control register from STATE. */
70
71void
72i386_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
964e4306
PA
81unsigned
82i386_dr_low_get_control (void)
83{
8d26e50c 84 return debug_reg_state.dr_control_mirror;
964e4306
PA
85}
86
aa5ca48f
DE
87/* Get the value of the DR6 debug status register from the inferior
88 and record it in STATE. */
89
964e4306
PA
90unsigned
91i386_dr_low_get_status (void)
aa5ca48f
DE
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. */
8d26e50c 95 return debug_reg_state.dr_status_mirror;
aa5ca48f
DE
96}
97
802e8e6d 98/* Breakpoint/watchpoint support. */
aa5ca48f
DE
99
100static int
802e8e6d
PA
101i386_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
113static int
114i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
115 int size, struct raw_breakpoint *bp)
aa5ca48f
DE
116{
117 switch (type)
118 {
802e8e6d
PA
119 case raw_bkpt_type_write_wp:
120 case raw_bkpt_type_access_wp:
a4165e94 121 {
802e8e6d
PA
122 enum target_hw_bp_type hw_type
123 = raw_bkpt_type_to_target_hw_bp_type (type);
a4165e94 124
4be83cc2
GB
125 return i386_dr_insert_watchpoint (&debug_reg_state,
126 hw_type, addr, size);
a4165e94 127 }
aa5ca48f
DE
128 default:
129 /* Unsupported. */
130 return 1;
131 }
132}
133
134static int
802e8e6d
PA
135i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
136 int size, struct raw_breakpoint *bp)
aa5ca48f
DE
137{
138 switch (type)
139 {
802e8e6d
PA
140 case raw_bkpt_type_write_wp:
141 case raw_bkpt_type_access_wp:
a4165e94 142 {
802e8e6d
PA
143 enum target_hw_bp_type hw_type
144 = raw_bkpt_type_to_target_hw_bp_type (type);
a4165e94 145
4be83cc2
GB
146 return i386_dr_remove_watchpoint (&debug_reg_state,
147 hw_type, addr, size);
a4165e94 148 }
aa5ca48f
DE
149 default:
150 /* Unsupported. */
151 return 1;
152 }
153}
154
155static int
156i386_stopped_by_watchpoint (void)
157{
4be83cc2 158 return i386_dr_stopped_by_watchpoint (&debug_reg_state);
aa5ca48f
DE
159}
160
161static CORE_ADDR
162i386_stopped_data_address (void)
163{
164 CORE_ADDR addr;
4be83cc2 165 if (i386_dr_stopped_data_address (&debug_reg_state, &addr))
aa5ca48f
DE
166 return addr;
167 return 0;
168}
169
68070c10 170static void
34b34921 171i386_initial_stuff (void)
68070c10 172{
aa5ca48f 173 i386_low_init_dregs (&debug_reg_state);
34b34921
PA
174 debug_registers_changed = 0;
175 debug_registers_used = 0;
68070c10
PA
176}
177
178static void
34b34921 179i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
68070c10 180{
912cf4ba
PA
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;
34b34921 184
912cf4ba
PA
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 }
34b34921
PA
203
204 debug_registers_changed = 0;
205
206 if (th->tid == current_event->dwThreadId)
207 {
208 /* Copy dr values from the current thread. */
aa5ca48f
DE
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;
8d26e50c
JB
214 dr->dr_status_mirror = th->context.Dr6;
215 dr->dr_control_mirror = th->context.Dr7;
34b34921 216 }
68070c10
PA
217}
218
219static void
34b34921 220i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
68070c10 221{
34b34921
PA
222 if (debug_registers_changed)
223 {
aa5ca48f
DE
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];
8d26e50c 229 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 230 FIXME: should we set dr6 also ?? */
8d26e50c 231 th->context.Dr7 = dr->dr_control_mirror;
34b34921
PA
232 }
233
234 SetThreadContext (th->h, &th->context);
68070c10
PA
235}
236
68070c10 237static void
34b34921 238i386_thread_added (win32_thread_info *th)
68070c10 239{
34b34921
PA
240 /* Set the debug registers for the new thread if they are used. */
241 if (debug_registers_used)
68070c10 242 {
aa5ca48f 243 struct i386_debug_reg_state *dr = &debug_reg_state;
34b34921
PA
244 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
245 GetThreadContext (th->h, &th->context);
246
aa5ca48f
DE
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];
8d26e50c 251 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 252 FIXME: should we set dr6 also ?? */
8d26e50c 253 th->context.Dr7 = dr->dr_control_mirror;
34b34921
PA
254
255 SetThreadContext (th->h, &th->context);
256 th->context.ContextFlags = 0;
68070c10 257 }
68070c10
PA
258}
259
260static void
34b34921 261i386_single_step (win32_thread_info *th)
68070c10
PA
262{
263 th->context.EFlags |= FLAG_TRACE_BIT;
264}
265
1c07cc19 266#ifndef __x86_64__
54709339 267
68070c10
PA
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))
276static 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
1c07cc19 323#else /* __x86_64__ */
54709339
PM
324
325#define context_offset(x) (offsetof (CONTEXT, x))
326static 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
1c07cc19 390#endif /* __x86_64__ */
54709339 391
34b34921
PA
392/* Fetch register from gdbserver regcache data. */
393static void
442ea881
PA
394i386_fetch_inferior_register (struct regcache *regcache,
395 win32_thread_info *th, int r)
34b34921
PA
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;
442ea881 403 supply_register (regcache, r, (char *) &l);
34b34921
PA
404 }
405 else if (r == FOP_REGNUM)
406 {
407 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
442ea881 408 supply_register (regcache, r, (char *) &l);
34b34921
PA
409 }
410 else
442ea881 411 supply_register (regcache, r, context_offset);
34b34921
PA
412}
413
414/* Store a new register value into the thread context of TH. */
415static void
442ea881
PA
416i386_store_inferior_register (struct regcache *regcache,
417 win32_thread_info *th, int r)
34b34921
PA
418{
419 char *context_offset = (char *) &th->context + mappings[r];
442ea881 420 collect_register (regcache, r, context_offset);
34b34921
PA
421}
422
912cf4ba
PA
423static const unsigned char i386_win32_breakpoint = 0xcc;
424#define i386_win32_breakpoint_len 1
425
54709339 426static void
3aee8918 427i386_arch_setup (void)
54709339 428{
1c07cc19 429#ifdef __x86_64__
54709339 430 init_registers_amd64 ();
3aee8918 431 win32_tdesc = tdesc_amd64;
54709339
PM
432#else
433 init_registers_i386 ();
3aee8918 434 win32_tdesc = tdesc_i386;
54709339
PM
435#endif
436}
437
68070c10 438struct win32_target_ops the_low_target = {
3aee8918 439 i386_arch_setup,
68070c10 440 sizeof (mappings) / sizeof (mappings[0]),
34b34921
PA
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,
912cf4ba
PA
448 &i386_win32_breakpoint,
449 i386_win32_breakpoint_len,
802e8e6d 450 i386_supports_z_point_type,
aa5ca48f
DE
451 i386_insert_point,
452 i386_remove_point,
453 i386_stopped_by_watchpoint,
454 i386_stopped_data_address
68070c10 455};
This page took 0.570542 seconds and 4 git commands to generate.