[GDBserver] Move Z packet defines and type convertion routines to shared code.
[external/binutils.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 /* Watchpoint support.  */
99
100 static int
101 i386_insert_point (char type, CORE_ADDR addr, int len)
102 {
103   switch (type)
104     {
105     case '2':
106     case '3':
107     case '4':
108       {
109         enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
110
111         return i386_low_insert_watchpoint (&debug_reg_state,
112                                            hw_type, addr, len);
113       }
114     default:
115       /* Unsupported.  */
116       return 1;
117     }
118 }
119
120 static int
121 i386_remove_point (char type, CORE_ADDR addr, int len)
122 {
123   switch (type)
124     {
125     case '2':
126     case '3':
127     case '4':
128       {
129         enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
130
131         return i386_low_remove_watchpoint (&debug_reg_state,
132                                            hw_type, addr, len);
133       }
134     default:
135       /* Unsupported.  */
136       return 1;
137     }
138 }
139
140 static int
141 i386_stopped_by_watchpoint (void)
142 {
143   return i386_low_stopped_by_watchpoint (&debug_reg_state);
144 }
145
146 static CORE_ADDR
147 i386_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
155 static void
156 i386_initial_stuff (void)
157 {
158   i386_low_init_dregs (&debug_reg_state);
159   debug_registers_changed = 0;
160   debug_registers_used = 0;
161 }
162
163 static void
164 i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
165 {
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;
169
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     }
188
189   debug_registers_changed = 0;
190
191   if (th->tid == current_event->dwThreadId)
192     {
193       /* Copy dr values from the current thread.  */
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;
199       dr->dr_status_mirror = th->context.Dr6;
200       dr->dr_control_mirror = th->context.Dr7;
201     }
202 }
203
204 static void
205 i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
206 {
207   if (debug_registers_changed)
208     {
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];
214       /* th->context.Dr6 = dr->dr_status_mirror;
215          FIXME: should we set dr6 also ?? */
216       th->context.Dr7 = dr->dr_control_mirror;
217     }
218
219   SetThreadContext (th->h, &th->context);
220 }
221
222 static void
223 i386_thread_added (win32_thread_info *th)
224 {
225   /* Set the debug registers for the new thread if they are used.  */
226   if (debug_registers_used)
227     {
228       struct i386_debug_reg_state *dr = &debug_reg_state;
229       th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
230       GetThreadContext (th->h, &th->context);
231
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];
236       /* th->context.Dr6 = dr->dr_status_mirror;
237          FIXME: should we set dr6 also ?? */
238       th->context.Dr7 = dr->dr_control_mirror;
239
240       SetThreadContext (th->h, &th->context);
241       th->context.ContextFlags = 0;
242     }
243 }
244
245 static void
246 i386_single_step (win32_thread_info *th)
247 {
248   th->context.EFlags |= FLAG_TRACE_BIT;
249 }
250
251 #ifndef __x86_64__
252
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))
261 static 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
308 #else /* __x86_64__ */
309
310 #define context_offset(x) (offsetof (CONTEXT, x))
311 static 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
375 #endif /* __x86_64__ */
376
377 /* Fetch register from gdbserver regcache data.  */
378 static void
379 i386_fetch_inferior_register (struct regcache *regcache,
380                               win32_thread_info *th, int r)
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;
388       supply_register (regcache, r, (char *) &l);
389     }
390   else if (r == FOP_REGNUM)
391     {
392       l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
393       supply_register (regcache, r, (char *) &l);
394     }
395   else
396     supply_register (regcache, r, context_offset);
397 }
398
399 /* Store a new register value into the thread context of TH.  */
400 static void
401 i386_store_inferior_register (struct regcache *regcache,
402                               win32_thread_info *th, int r)
403 {
404   char *context_offset = (char *) &th->context + mappings[r];
405   collect_register (regcache, r, context_offset);
406 }
407
408 static const unsigned char i386_win32_breakpoint = 0xcc;
409 #define i386_win32_breakpoint_len 1
410
411 static void
412 i386_arch_setup (void)
413 {
414 #ifdef __x86_64__
415   init_registers_amd64 ();
416   win32_tdesc = tdesc_amd64;
417 #else
418   init_registers_i386 ();
419   win32_tdesc = tdesc_i386;
420 #endif
421 }
422
423 struct win32_target_ops the_low_target = {
424   i386_arch_setup,
425   sizeof (mappings) / sizeof (mappings[0]),
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,
433   &i386_win32_breakpoint,
434   i386_win32_breakpoint_len,
435   i386_insert_point,
436   i386_remove_point,
437   i386_stopped_by_watchpoint,
438   i386_stopped_data_address
439 };