Switch gdbserver to gdbsupport event loop
[deliverable/binutils-gdb.git] / gdbserver / win32-i386-low.cc
CommitLineData
b811d2c2 1/* Copyright (C) 2007-2020 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"
df7e5265 20#include "x86-low.h"
268a13a5 21#include "gdbsupport/x86-xstate.h"
22916b07
YQ
22#ifdef __x86_64__
23#include "arch/amd64.h"
24#endif
25#include "arch/i386.h"
cc4d742f 26#include "tdesc.h"
190852c8 27#include "x86-tdesc.h"
68070c10 28
4834dad0
TT
29using namespace windows_nat;
30
54709339
PM
31#ifndef CONTEXT_EXTENDED_REGISTERS
32#define CONTEXT_EXTENDED_REGISTERS 0
33#endif
34
68070c10
PA
35#define FCS_REGNUM 27
36#define FOP_REGNUM 31
37
38#define FLAG_TRACE_BIT 0x100
39
df7e5265 40static struct x86_debug_reg_state debug_reg_state;
68070c10 41
0b360f19
SM
42static void
43update_debug_registers (thread_info *thread)
a2abc7de 44{
e56f8ccb 45 windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
a2abc7de 46
0b360f19
SM
47 /* The actual update is done later just before resuming the lwp,
48 we just mark that the registers need updating. */
62fe396b 49 th->debug_registers_changed = true;
a2abc7de 50}
34b34921 51
aa5ca48f
DE
52/* Update the inferior's debug register REGNUM from STATE. */
53
42995dbd 54static void
df7e5265 55x86_dr_low_set_addr (int regnum, CORE_ADDR addr)
aa5ca48f 56{
0a5b1e09 57 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
964e4306 58
0b360f19
SM
59 /* Only update the threads of this process. */
60 for_each_thread (current_thread->id.pid (), update_debug_registers);
964e4306
PA
61}
62
aa5ca48f
DE
63/* Update the inferior's DR7 debug control register from STATE. */
64
42995dbd 65static void
df7e5265 66x86_dr_low_set_control (unsigned long control)
aa5ca48f 67{
a2abc7de 68 /* Only update the threads of this process. */
0b360f19 69 for_each_thread (current_thread->id.pid (), update_debug_registers);
a2abc7de
PA
70}
71
72/* Return the current value of a DR register of the current thread's
73 context. */
74
75static DWORD64
76win32_get_current_dr (int dr)
77{
e56f8ccb
TT
78 windows_thread_info *th
79 = (windows_thread_info *) thread_target_data (current_thread);
a2abc7de
PA
80
81 win32_require_context (th);
82
83#define RET_DR(DR) \
84 case DR: \
85 return th->context.Dr ## DR
86
87 switch (dr)
88 {
89 RET_DR (0);
90 RET_DR (1);
91 RET_DR (2);
92 RET_DR (3);
93 RET_DR (6);
94 RET_DR (7);
95 }
96
97#undef RET_DR
98
99 gdb_assert_not_reached ("unhandled dr");
100}
101
102static CORE_ADDR
103x86_dr_low_get_addr (int regnum)
104{
105 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
106
107 return win32_get_current_dr (regnum - DR_FIRSTADDR);
aa5ca48f
DE
108}
109
42995dbd 110static unsigned long
df7e5265 111x86_dr_low_get_control (void)
964e4306 112{
a2abc7de 113 return win32_get_current_dr (7);
964e4306
PA
114}
115
aa5ca48f
DE
116/* Get the value of the DR6 debug status register from the inferior
117 and record it in STATE. */
118
42995dbd 119static unsigned long
df7e5265 120x86_dr_low_get_status (void)
aa5ca48f 121{
a2abc7de 122 return win32_get_current_dr (6);
aa5ca48f
DE
123}
124
42995dbd 125/* Low-level function vector. */
df7e5265 126struct x86_dr_low_type x86_dr_low =
42995dbd 127 {
df7e5265
GB
128 x86_dr_low_set_control,
129 x86_dr_low_set_addr,
130 x86_dr_low_get_addr,
131 x86_dr_low_get_status,
132 x86_dr_low_get_control,
42995dbd
GB
133 sizeof (void *),
134 };
135
802e8e6d 136/* Breakpoint/watchpoint support. */
aa5ca48f
DE
137
138static int
802e8e6d
PA
139i386_supports_z_point_type (char z_type)
140{
141 switch (z_type)
142 {
143 case Z_PACKET_WRITE_WP:
144 case Z_PACKET_ACCESS_WP:
145 return 1;
146 default:
147 return 0;
148 }
149}
150
151static int
152i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
153 int size, struct raw_breakpoint *bp)
aa5ca48f
DE
154{
155 switch (type)
156 {
802e8e6d
PA
157 case raw_bkpt_type_write_wp:
158 case raw_bkpt_type_access_wp:
a4165e94 159 {
802e8e6d
PA
160 enum target_hw_bp_type hw_type
161 = raw_bkpt_type_to_target_hw_bp_type (type);
a4165e94 162
df7e5265
GB
163 return x86_dr_insert_watchpoint (&debug_reg_state,
164 hw_type, addr, size);
a4165e94 165 }
aa5ca48f
DE
166 default:
167 /* Unsupported. */
168 return 1;
169 }
170}
171
172static int
802e8e6d
PA
173i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
174 int size, struct raw_breakpoint *bp)
aa5ca48f
DE
175{
176 switch (type)
177 {
802e8e6d
PA
178 case raw_bkpt_type_write_wp:
179 case raw_bkpt_type_access_wp:
a4165e94 180 {
802e8e6d
PA
181 enum target_hw_bp_type hw_type
182 = raw_bkpt_type_to_target_hw_bp_type (type);
a4165e94 183
df7e5265
GB
184 return x86_dr_remove_watchpoint (&debug_reg_state,
185 hw_type, addr, size);
a4165e94 186 }
aa5ca48f
DE
187 default:
188 /* Unsupported. */
189 return 1;
190 }
191}
192
193static int
df7e5265 194x86_stopped_by_watchpoint (void)
aa5ca48f 195{
df7e5265 196 return x86_dr_stopped_by_watchpoint (&debug_reg_state);
aa5ca48f
DE
197}
198
199static CORE_ADDR
df7e5265 200x86_stopped_data_address (void)
aa5ca48f
DE
201{
202 CORE_ADDR addr;
df7e5265 203 if (x86_dr_stopped_data_address (&debug_reg_state, &addr))
aa5ca48f
DE
204 return addr;
205 return 0;
206}
207
68070c10 208static void
34b34921 209i386_initial_stuff (void)
68070c10 210{
df7e5265 211 x86_low_init_dregs (&debug_reg_state);
68070c10
PA
212}
213
214static void
e56f8ccb 215i386_get_thread_context (windows_thread_info *th)
68070c10 216{
912cf4ba
PA
217 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
218 the system doesn't support extended registers. */
219 static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS;
34b34921 220
912cf4ba
PA
221 again:
222 th->context.ContextFlags = (CONTEXT_FULL
223 | CONTEXT_FLOATING_POINT
224 | CONTEXT_DEBUG_REGISTERS
225 | extended_registers);
226
227 if (!GetThreadContext (th->h, &th->context))
228 {
229 DWORD e = GetLastError ();
230
231 if (extended_registers && e == ERROR_INVALID_PARAMETER)
232 {
233 extended_registers = 0;
234 goto again;
235 }
236
237 error ("GetThreadContext failure %ld\n", (long) e);
238 }
68070c10
PA
239}
240
241static void
e56f8ccb 242i386_prepare_to_resume (windows_thread_info *th)
68070c10 243{
a2abc7de 244 if (th->debug_registers_changed)
34b34921 245 {
df7e5265 246 struct x86_debug_reg_state *dr = &debug_reg_state;
a2abc7de
PA
247
248 win32_require_context (th);
249
aa5ca48f
DE
250 th->context.Dr0 = dr->dr_mirror[0];
251 th->context.Dr1 = dr->dr_mirror[1];
252 th->context.Dr2 = dr->dr_mirror[2];
253 th->context.Dr3 = dr->dr_mirror[3];
8d26e50c 254 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 255 FIXME: should we set dr6 also ?? */
8d26e50c 256 th->context.Dr7 = dr->dr_control_mirror;
34b34921 257
62fe396b 258 th->debug_registers_changed = false;
a2abc7de 259 }
68070c10
PA
260}
261
68070c10 262static void
e56f8ccb 263i386_thread_added (windows_thread_info *th)
68070c10 264{
62fe396b 265 th->debug_registers_changed = true;
68070c10
PA
266}
267
268static void
e56f8ccb 269i386_single_step (windows_thread_info *th)
68070c10
PA
270{
271 th->context.EFlags |= FLAG_TRACE_BIT;
272}
273
1c07cc19 274#ifndef __x86_64__
54709339 275
68070c10
PA
276/* An array of offset mappings into a Win32 Context structure.
277 This is a one-to-one mapping which is indexed by gdb's register
278 numbers. It retrieves an offset into the context structure where
279 the 4 byte register is located.
280 An offset value of -1 indicates that Win32 does not provide this
281 register in it's CONTEXT structure. In this case regptr will return
282 a pointer into a dummy register. */
283#define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
284static const int mappings[] = {
285 context_offset (Eax),
286 context_offset (Ecx),
287 context_offset (Edx),
288 context_offset (Ebx),
289 context_offset (Esp),
290 context_offset (Ebp),
291 context_offset (Esi),
292 context_offset (Edi),
293 context_offset (Eip),
294 context_offset (EFlags),
295 context_offset (SegCs),
296 context_offset (SegSs),
297 context_offset (SegDs),
298 context_offset (SegEs),
299 context_offset (SegFs),
300 context_offset (SegGs),
301 context_offset (FloatSave.RegisterArea[0 * 10]),
302 context_offset (FloatSave.RegisterArea[1 * 10]),
303 context_offset (FloatSave.RegisterArea[2 * 10]),
304 context_offset (FloatSave.RegisterArea[3 * 10]),
305 context_offset (FloatSave.RegisterArea[4 * 10]),
306 context_offset (FloatSave.RegisterArea[5 * 10]),
307 context_offset (FloatSave.RegisterArea[6 * 10]),
308 context_offset (FloatSave.RegisterArea[7 * 10]),
309 context_offset (FloatSave.ControlWord),
310 context_offset (FloatSave.StatusWord),
311 context_offset (FloatSave.TagWord),
312 context_offset (FloatSave.ErrorSelector),
313 context_offset (FloatSave.ErrorOffset),
314 context_offset (FloatSave.DataSelector),
315 context_offset (FloatSave.DataOffset),
316 context_offset (FloatSave.ErrorSelector),
317 /* XMM0-7 */
318 context_offset (ExtendedRegisters[10 * 16]),
319 context_offset (ExtendedRegisters[11 * 16]),
320 context_offset (ExtendedRegisters[12 * 16]),
321 context_offset (ExtendedRegisters[13 * 16]),
322 context_offset (ExtendedRegisters[14 * 16]),
323 context_offset (ExtendedRegisters[15 * 16]),
324 context_offset (ExtendedRegisters[16 * 16]),
325 context_offset (ExtendedRegisters[17 * 16]),
326 /* MXCSR */
327 context_offset (ExtendedRegisters[24])
328};
329#undef context_offset
330
1c07cc19 331#else /* __x86_64__ */
54709339
PM
332
333#define context_offset(x) (offsetof (CONTEXT, x))
334static const int mappings[] =
335{
336 context_offset (Rax),
337 context_offset (Rbx),
338 context_offset (Rcx),
339 context_offset (Rdx),
340 context_offset (Rsi),
341 context_offset (Rdi),
342 context_offset (Rbp),
343 context_offset (Rsp),
344 context_offset (R8),
345 context_offset (R9),
346 context_offset (R10),
347 context_offset (R11),
348 context_offset (R12),
349 context_offset (R13),
350 context_offset (R14),
351 context_offset (R15),
352 context_offset (Rip),
353 context_offset (EFlags),
354 context_offset (SegCs),
355 context_offset (SegSs),
356 context_offset (SegDs),
357 context_offset (SegEs),
358 context_offset (SegFs),
359 context_offset (SegGs),
360 context_offset (FloatSave.FloatRegisters[0]),
361 context_offset (FloatSave.FloatRegisters[1]),
362 context_offset (FloatSave.FloatRegisters[2]),
363 context_offset (FloatSave.FloatRegisters[3]),
364 context_offset (FloatSave.FloatRegisters[4]),
365 context_offset (FloatSave.FloatRegisters[5]),
366 context_offset (FloatSave.FloatRegisters[6]),
367 context_offset (FloatSave.FloatRegisters[7]),
368 context_offset (FloatSave.ControlWord),
369 context_offset (FloatSave.StatusWord),
370 context_offset (FloatSave.TagWord),
371 context_offset (FloatSave.ErrorSelector),
372 context_offset (FloatSave.ErrorOffset),
373 context_offset (FloatSave.DataSelector),
374 context_offset (FloatSave.DataOffset),
375 context_offset (FloatSave.ErrorSelector)
376 /* XMM0-7 */ ,
377 context_offset (Xmm0),
378 context_offset (Xmm1),
379 context_offset (Xmm2),
380 context_offset (Xmm3),
381 context_offset (Xmm4),
382 context_offset (Xmm5),
383 context_offset (Xmm6),
384 context_offset (Xmm7),
385 context_offset (Xmm8),
386 context_offset (Xmm9),
387 context_offset (Xmm10),
388 context_offset (Xmm11),
389 context_offset (Xmm12),
390 context_offset (Xmm13),
391 context_offset (Xmm14),
392 context_offset (Xmm15),
393 /* MXCSR */
394 context_offset (FloatSave.MxCsr)
395};
396#undef context_offset
397
1c07cc19 398#endif /* __x86_64__ */
54709339 399
34b34921
PA
400/* Fetch register from gdbserver regcache data. */
401static void
442ea881 402i386_fetch_inferior_register (struct regcache *regcache,
e56f8ccb 403 windows_thread_info *th, int r)
34b34921
PA
404{
405 char *context_offset = (char *) &th->context + mappings[r];
406
407 long l;
408 if (r == FCS_REGNUM)
409 {
410 l = *((long *) context_offset) & 0xffff;
442ea881 411 supply_register (regcache, r, (char *) &l);
34b34921
PA
412 }
413 else if (r == FOP_REGNUM)
414 {
415 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
442ea881 416 supply_register (regcache, r, (char *) &l);
34b34921
PA
417 }
418 else
442ea881 419 supply_register (regcache, r, context_offset);
34b34921
PA
420}
421
422/* Store a new register value into the thread context of TH. */
423static void
442ea881 424i386_store_inferior_register (struct regcache *regcache,
e56f8ccb 425 windows_thread_info *th, int r)
34b34921
PA
426{
427 char *context_offset = (char *) &th->context + mappings[r];
442ea881 428 collect_register (regcache, r, context_offset);
34b34921
PA
429}
430
912cf4ba
PA
431static const unsigned char i386_win32_breakpoint = 0xcc;
432#define i386_win32_breakpoint_len 1
433
54709339 434static void
3aee8918 435i386_arch_setup (void)
54709339 436{
cc4d742f
SM
437 struct target_desc *tdesc;
438
1c07cc19 439#ifdef __x86_64__
cc4d742f 440 tdesc = amd64_create_target_description (X86_XSTATE_SSE_MASK, false,
de52b960 441 false, false);
190852c8 442 const char **expedite_regs = amd64_expedite_regs;
54709339 443#else
1163a4b7 444 tdesc = i386_create_target_description (X86_XSTATE_SSE_MASK, false, false);
190852c8 445 const char **expedite_regs = i386_expedite_regs;
54709339 446#endif
cc4d742f 447
190852c8 448 init_target_desc (tdesc, expedite_regs);
cc4d742f
SM
449
450 win32_tdesc = tdesc;
54709339
PM
451}
452
d6225aff
TT
453/* Implement win32_target_ops "get_pc" method. */
454
455static CORE_ADDR
456i386_win32_get_pc (struct regcache *regcache)
457{
458 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
459
460 if (use_64bit)
461 {
462 uint64_t pc;
463
464 collect_register_by_name (regcache, "rip", &pc);
465 return (CORE_ADDR) pc;
466 }
467 else
468 {
469 uint32_t pc;
470
471 collect_register_by_name (regcache, "eip", &pc);
472 return (CORE_ADDR) pc;
473 }
474}
475
476/* Implement win32_target_ops "set_pc" method. */
477
478static void
479i386_win32_set_pc (struct regcache *regcache, CORE_ADDR pc)
480{
481 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
482
483 if (use_64bit)
484 {
485 uint64_t newpc = pc;
486
487 supply_register_by_name (regcache, "rip", &newpc);
488 }
489 else
490 {
491 uint32_t newpc = pc;
492
493 supply_register_by_name (regcache, "eip", &newpc);
494 }
495}
496
68070c10 497struct win32_target_ops the_low_target = {
3aee8918 498 i386_arch_setup,
68070c10 499 sizeof (mappings) / sizeof (mappings[0]),
34b34921
PA
500 i386_initial_stuff,
501 i386_get_thread_context,
a2abc7de 502 i386_prepare_to_resume,
34b34921
PA
503 i386_thread_added,
504 i386_fetch_inferior_register,
505 i386_store_inferior_register,
506 i386_single_step,
912cf4ba
PA
507 &i386_win32_breakpoint,
508 i386_win32_breakpoint_len,
e54e5929 509 1,
d6225aff
TT
510 i386_win32_get_pc,
511 i386_win32_set_pc,
802e8e6d 512 i386_supports_z_point_type,
aa5ca48f
DE
513 i386_insert_point,
514 i386_remove_point,
df7e5265
GB
515 x86_stopped_by_watchpoint,
516 x86_stopped_data_address
68070c10 517};
This page took 1.059062 seconds and 4 git commands to generate.