[GDBserver] Move Z packet defines and type convertion routines to shared code.
[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
98/* Watchpoint support. */
99
100static int
101i386_insert_point (char type, CORE_ADDR addr, int len)
102{
103 switch (type)
104 {
105 case '2':
106 case '3':
107 case '4':
a4165e94 108 {
932539e3 109 enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
a4165e94
PA
110
111 return i386_low_insert_watchpoint (&debug_reg_state,
112 hw_type, addr, len);
113 }
aa5ca48f
DE
114 default:
115 /* Unsupported. */
116 return 1;
117 }
118}
119
120static int
121i386_remove_point (char type, CORE_ADDR addr, int len)
122{
123 switch (type)
124 {
125 case '2':
126 case '3':
127 case '4':
a4165e94 128 {
932539e3 129 enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
a4165e94
PA
130
131 return i386_low_remove_watchpoint (&debug_reg_state,
132 hw_type, addr, len);
133 }
aa5ca48f
DE
134 default:
135 /* Unsupported. */
136 return 1;
137 }
138}
139
140static int
141i386_stopped_by_watchpoint (void)
142{
143 return i386_low_stopped_by_watchpoint (&debug_reg_state);
144}
145
146static CORE_ADDR
147i386_stopped_data_address (void)
148{
149 CORE_ADDR addr;
150 if (i386_low_stopped_data_address (&debug_reg_state, &addr))
151 return addr;
152 return 0;
153}
154
68070c10 155static void
34b34921 156i386_initial_stuff (void)
68070c10 157{
aa5ca48f 158 i386_low_init_dregs (&debug_reg_state);
34b34921
PA
159 debug_registers_changed = 0;
160 debug_registers_used = 0;
68070c10
PA
161}
162
163static void
34b34921 164i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
68070c10 165{
912cf4ba
PA
166 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
167 the system doesn't support extended registers. */
168 static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS;
34b34921 169
912cf4ba
PA
170 again:
171 th->context.ContextFlags = (CONTEXT_FULL
172 | CONTEXT_FLOATING_POINT
173 | CONTEXT_DEBUG_REGISTERS
174 | extended_registers);
175
176 if (!GetThreadContext (th->h, &th->context))
177 {
178 DWORD e = GetLastError ();
179
180 if (extended_registers && e == ERROR_INVALID_PARAMETER)
181 {
182 extended_registers = 0;
183 goto again;
184 }
185
186 error ("GetThreadContext failure %ld\n", (long) e);
187 }
34b34921
PA
188
189 debug_registers_changed = 0;
190
191 if (th->tid == current_event->dwThreadId)
192 {
193 /* Copy dr values from the current thread. */
aa5ca48f
DE
194 struct i386_debug_reg_state *dr = &debug_reg_state;
195 dr->dr_mirror[0] = th->context.Dr0;
196 dr->dr_mirror[1] = th->context.Dr1;
197 dr->dr_mirror[2] = th->context.Dr2;
198 dr->dr_mirror[3] = th->context.Dr3;
8d26e50c
JB
199 dr->dr_status_mirror = th->context.Dr6;
200 dr->dr_control_mirror = th->context.Dr7;
34b34921 201 }
68070c10
PA
202}
203
204static void
34b34921 205i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
68070c10 206{
34b34921
PA
207 if (debug_registers_changed)
208 {
aa5ca48f
DE
209 struct i386_debug_reg_state *dr = &debug_reg_state;
210 th->context.Dr0 = dr->dr_mirror[0];
211 th->context.Dr1 = dr->dr_mirror[1];
212 th->context.Dr2 = dr->dr_mirror[2];
213 th->context.Dr3 = dr->dr_mirror[3];
8d26e50c 214 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 215 FIXME: should we set dr6 also ?? */
8d26e50c 216 th->context.Dr7 = dr->dr_control_mirror;
34b34921
PA
217 }
218
219 SetThreadContext (th->h, &th->context);
68070c10
PA
220}
221
68070c10 222static void
34b34921 223i386_thread_added (win32_thread_info *th)
68070c10 224{
34b34921
PA
225 /* Set the debug registers for the new thread if they are used. */
226 if (debug_registers_used)
68070c10 227 {
aa5ca48f 228 struct i386_debug_reg_state *dr = &debug_reg_state;
34b34921
PA
229 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
230 GetThreadContext (th->h, &th->context);
231
aa5ca48f
DE
232 th->context.Dr0 = dr->dr_mirror[0];
233 th->context.Dr1 = dr->dr_mirror[1];
234 th->context.Dr2 = dr->dr_mirror[2];
235 th->context.Dr3 = dr->dr_mirror[3];
8d26e50c 236 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 237 FIXME: should we set dr6 also ?? */
8d26e50c 238 th->context.Dr7 = dr->dr_control_mirror;
34b34921
PA
239
240 SetThreadContext (th->h, &th->context);
241 th->context.ContextFlags = 0;
68070c10 242 }
68070c10
PA
243}
244
245static void
34b34921 246i386_single_step (win32_thread_info *th)
68070c10
PA
247{
248 th->context.EFlags |= FLAG_TRACE_BIT;
249}
250
1c07cc19 251#ifndef __x86_64__
54709339 252
68070c10
PA
253/* An array of offset mappings into a Win32 Context structure.
254 This is a one-to-one mapping which is indexed by gdb's register
255 numbers. It retrieves an offset into the context structure where
256 the 4 byte register is located.
257 An offset value of -1 indicates that Win32 does not provide this
258 register in it's CONTEXT structure. In this case regptr will return
259 a pointer into a dummy register. */
260#define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
261static const int mappings[] = {
262 context_offset (Eax),
263 context_offset (Ecx),
264 context_offset (Edx),
265 context_offset (Ebx),
266 context_offset (Esp),
267 context_offset (Ebp),
268 context_offset (Esi),
269 context_offset (Edi),
270 context_offset (Eip),
271 context_offset (EFlags),
272 context_offset (SegCs),
273 context_offset (SegSs),
274 context_offset (SegDs),
275 context_offset (SegEs),
276 context_offset (SegFs),
277 context_offset (SegGs),
278 context_offset (FloatSave.RegisterArea[0 * 10]),
279 context_offset (FloatSave.RegisterArea[1 * 10]),
280 context_offset (FloatSave.RegisterArea[2 * 10]),
281 context_offset (FloatSave.RegisterArea[3 * 10]),
282 context_offset (FloatSave.RegisterArea[4 * 10]),
283 context_offset (FloatSave.RegisterArea[5 * 10]),
284 context_offset (FloatSave.RegisterArea[6 * 10]),
285 context_offset (FloatSave.RegisterArea[7 * 10]),
286 context_offset (FloatSave.ControlWord),
287 context_offset (FloatSave.StatusWord),
288 context_offset (FloatSave.TagWord),
289 context_offset (FloatSave.ErrorSelector),
290 context_offset (FloatSave.ErrorOffset),
291 context_offset (FloatSave.DataSelector),
292 context_offset (FloatSave.DataOffset),
293 context_offset (FloatSave.ErrorSelector),
294 /* XMM0-7 */
295 context_offset (ExtendedRegisters[10 * 16]),
296 context_offset (ExtendedRegisters[11 * 16]),
297 context_offset (ExtendedRegisters[12 * 16]),
298 context_offset (ExtendedRegisters[13 * 16]),
299 context_offset (ExtendedRegisters[14 * 16]),
300 context_offset (ExtendedRegisters[15 * 16]),
301 context_offset (ExtendedRegisters[16 * 16]),
302 context_offset (ExtendedRegisters[17 * 16]),
303 /* MXCSR */
304 context_offset (ExtendedRegisters[24])
305};
306#undef context_offset
307
1c07cc19 308#else /* __x86_64__ */
54709339
PM
309
310#define context_offset(x) (offsetof (CONTEXT, x))
311static const int mappings[] =
312{
313 context_offset (Rax),
314 context_offset (Rbx),
315 context_offset (Rcx),
316 context_offset (Rdx),
317 context_offset (Rsi),
318 context_offset (Rdi),
319 context_offset (Rbp),
320 context_offset (Rsp),
321 context_offset (R8),
322 context_offset (R9),
323 context_offset (R10),
324 context_offset (R11),
325 context_offset (R12),
326 context_offset (R13),
327 context_offset (R14),
328 context_offset (R15),
329 context_offset (Rip),
330 context_offset (EFlags),
331 context_offset (SegCs),
332 context_offset (SegSs),
333 context_offset (SegDs),
334 context_offset (SegEs),
335 context_offset (SegFs),
336 context_offset (SegGs),
337 context_offset (FloatSave.FloatRegisters[0]),
338 context_offset (FloatSave.FloatRegisters[1]),
339 context_offset (FloatSave.FloatRegisters[2]),
340 context_offset (FloatSave.FloatRegisters[3]),
341 context_offset (FloatSave.FloatRegisters[4]),
342 context_offset (FloatSave.FloatRegisters[5]),
343 context_offset (FloatSave.FloatRegisters[6]),
344 context_offset (FloatSave.FloatRegisters[7]),
345 context_offset (FloatSave.ControlWord),
346 context_offset (FloatSave.StatusWord),
347 context_offset (FloatSave.TagWord),
348 context_offset (FloatSave.ErrorSelector),
349 context_offset (FloatSave.ErrorOffset),
350 context_offset (FloatSave.DataSelector),
351 context_offset (FloatSave.DataOffset),
352 context_offset (FloatSave.ErrorSelector)
353 /* XMM0-7 */ ,
354 context_offset (Xmm0),
355 context_offset (Xmm1),
356 context_offset (Xmm2),
357 context_offset (Xmm3),
358 context_offset (Xmm4),
359 context_offset (Xmm5),
360 context_offset (Xmm6),
361 context_offset (Xmm7),
362 context_offset (Xmm8),
363 context_offset (Xmm9),
364 context_offset (Xmm10),
365 context_offset (Xmm11),
366 context_offset (Xmm12),
367 context_offset (Xmm13),
368 context_offset (Xmm14),
369 context_offset (Xmm15),
370 /* MXCSR */
371 context_offset (FloatSave.MxCsr)
372};
373#undef context_offset
374
1c07cc19 375#endif /* __x86_64__ */
54709339 376
34b34921
PA
377/* Fetch register from gdbserver regcache data. */
378static void
442ea881
PA
379i386_fetch_inferior_register (struct regcache *regcache,
380 win32_thread_info *th, int r)
34b34921
PA
381{
382 char *context_offset = (char *) &th->context + mappings[r];
383
384 long l;
385 if (r == FCS_REGNUM)
386 {
387 l = *((long *) context_offset) & 0xffff;
442ea881 388 supply_register (regcache, r, (char *) &l);
34b34921
PA
389 }
390 else if (r == FOP_REGNUM)
391 {
392 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
442ea881 393 supply_register (regcache, r, (char *) &l);
34b34921
PA
394 }
395 else
442ea881 396 supply_register (regcache, r, context_offset);
34b34921
PA
397}
398
399/* Store a new register value into the thread context of TH. */
400static void
442ea881
PA
401i386_store_inferior_register (struct regcache *regcache,
402 win32_thread_info *th, int r)
34b34921
PA
403{
404 char *context_offset = (char *) &th->context + mappings[r];
442ea881 405 collect_register (regcache, r, context_offset);
34b34921
PA
406}
407
912cf4ba
PA
408static const unsigned char i386_win32_breakpoint = 0xcc;
409#define i386_win32_breakpoint_len 1
410
54709339 411static void
3aee8918 412i386_arch_setup (void)
54709339 413{
1c07cc19 414#ifdef __x86_64__
54709339 415 init_registers_amd64 ();
3aee8918 416 win32_tdesc = tdesc_amd64;
54709339
PM
417#else
418 init_registers_i386 ();
3aee8918 419 win32_tdesc = tdesc_i386;
54709339
PM
420#endif
421}
422
68070c10 423struct win32_target_ops the_low_target = {
3aee8918 424 i386_arch_setup,
68070c10 425 sizeof (mappings) / sizeof (mappings[0]),
34b34921
PA
426 i386_initial_stuff,
427 i386_get_thread_context,
428 i386_set_thread_context,
429 i386_thread_added,
430 i386_fetch_inferior_register,
431 i386_store_inferior_register,
432 i386_single_step,
912cf4ba
PA
433 &i386_win32_breakpoint,
434 i386_win32_breakpoint_len,
aa5ca48f
DE
435 i386_insert_point,
436 i386_remove_point,
437 i386_stopped_by_watchpoint,
438 i386_stopped_data_address
68070c10 439};
This page took 0.554773 seconds and 4 git commands to generate.