1 /* Copyright (C) 2007-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
19 #include "win32-low.h"
21 #include "gdbsupport/x86-xstate.h"
23 #include "arch/amd64.h"
25 #include "arch/i386.h"
27 #include "x86-tdesc.h"
29 using namespace windows_nat
;
31 #ifndef CONTEXT_EXTENDED_REGISTERS
32 #define CONTEXT_EXTENDED_REGISTERS 0
35 #define I386_FISEG_REGNUM 27
36 #define I386_FOP_REGNUM 31
38 #define I386_CS_REGNUM 10
39 #define I386_GS_REGNUM 15
41 #define AMD64_FISEG_REGNUM 35
42 #define AMD64_FOP_REGNUM 39
44 #define AMD64_CS_REGNUM 18
45 #define AMD64_GS_REGNUM 23
47 #define FLAG_TRACE_BIT 0x100
49 static struct x86_debug_reg_state debug_reg_state
;
52 update_debug_registers (thread_info
*thread
)
54 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
56 /* The actual update is done later just before resuming the lwp,
57 we just mark that the registers need updating. */
58 th
->debug_registers_changed
= true;
61 /* Update the inferior's debug register REGNUM from STATE. */
64 x86_dr_low_set_addr (int regnum
, CORE_ADDR addr
)
66 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
68 /* Only update the threads of this process. */
69 for_each_thread (current_thread
->id
.pid (), update_debug_registers
);
72 /* Update the inferior's DR7 debug control register from STATE. */
75 x86_dr_low_set_control (unsigned long control
)
77 /* Only update the threads of this process. */
78 for_each_thread (current_thread
->id
.pid (), update_debug_registers
);
81 /* Return the current value of a DR register of the current thread's
85 win32_get_current_dr (int dr
)
87 windows_thread_info
*th
88 = (windows_thread_info
*) thread_target_data (current_thread
);
90 win32_require_context (th
);
95 return th->wow64_context.Dr ## DR
97 if (windows_process
.wow64_process
)
114 return th->context.Dr ## DR
130 gdb_assert_not_reached ("unhandled dr");
134 x86_dr_low_get_addr (int regnum
)
136 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
138 return win32_get_current_dr (regnum
- DR_FIRSTADDR
);
142 x86_dr_low_get_control (void)
144 return win32_get_current_dr (7);
147 /* Get the value of the DR6 debug status register from the inferior
148 and record it in STATE. */
151 x86_dr_low_get_status (void)
153 return win32_get_current_dr (6);
156 /* Low-level function vector. */
157 struct x86_dr_low_type x86_dr_low
=
159 x86_dr_low_set_control
,
162 x86_dr_low_get_status
,
163 x86_dr_low_get_control
,
167 /* Breakpoint/watchpoint support. */
170 i386_supports_z_point_type (char z_type
)
175 case Z_PACKET_WRITE_WP
:
176 case Z_PACKET_ACCESS_WP
:
184 i386_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
185 int size
, struct raw_breakpoint
*bp
)
189 case raw_bkpt_type_hw
:
190 case raw_bkpt_type_write_wp
:
191 case raw_bkpt_type_access_wp
:
193 enum target_hw_bp_type hw_type
194 = raw_bkpt_type_to_target_hw_bp_type (type
);
196 return x86_dr_insert_watchpoint (&debug_reg_state
,
197 hw_type
, addr
, size
);
206 i386_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
207 int size
, struct raw_breakpoint
*bp
)
211 case raw_bkpt_type_hw
:
212 case raw_bkpt_type_write_wp
:
213 case raw_bkpt_type_access_wp
:
215 enum target_hw_bp_type hw_type
216 = raw_bkpt_type_to_target_hw_bp_type (type
);
218 return x86_dr_remove_watchpoint (&debug_reg_state
,
219 hw_type
, addr
, size
);
228 x86_stopped_by_watchpoint (void)
230 return x86_dr_stopped_by_watchpoint (&debug_reg_state
);
234 x86_stopped_data_address (void)
237 if (x86_dr_stopped_data_address (&debug_reg_state
, &addr
))
243 i386_initial_stuff (void)
245 x86_low_init_dregs (&debug_reg_state
);
249 i386_get_thread_context (windows_thread_info
*th
)
251 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
252 the system doesn't support extended registers. */
253 static DWORD extended_registers
= CONTEXT_EXTENDED_REGISTERS
;
257 if (windows_process
.wow64_process
)
258 th
->wow64_context
.ContextFlags
= (CONTEXT_FULL
259 | CONTEXT_FLOATING_POINT
260 | CONTEXT_DEBUG_REGISTERS
261 | extended_registers
);
264 th
->context
.ContextFlags
= (CONTEXT_FULL
265 | CONTEXT_FLOATING_POINT
266 | CONTEXT_DEBUG_REGISTERS
267 | extended_registers
);
271 if (windows_process
.wow64_process
)
272 ret
= Wow64GetThreadContext (th
->h
, &th
->wow64_context
);
275 ret
= GetThreadContext (th
->h
, &th
->context
);
278 DWORD e
= GetLastError ();
280 if (extended_registers
&& e
== ERROR_INVALID_PARAMETER
)
282 extended_registers
= 0;
286 error ("GetThreadContext failure %ld\n", (long) e
);
291 i386_prepare_to_resume (windows_thread_info
*th
)
293 if (th
->debug_registers_changed
)
295 struct x86_debug_reg_state
*dr
= &debug_reg_state
;
297 win32_require_context (th
);
300 if (windows_process
.wow64_process
)
302 th
->wow64_context
.Dr0
= dr
->dr_mirror
[0];
303 th
->wow64_context
.Dr1
= dr
->dr_mirror
[1];
304 th
->wow64_context
.Dr2
= dr
->dr_mirror
[2];
305 th
->wow64_context
.Dr3
= dr
->dr_mirror
[3];
306 /* th->wow64_context.Dr6 = dr->dr_status_mirror;
307 FIXME: should we set dr6 also ?? */
308 th
->wow64_context
.Dr7
= dr
->dr_control_mirror
;
313 th
->context
.Dr0
= dr
->dr_mirror
[0];
314 th
->context
.Dr1
= dr
->dr_mirror
[1];
315 th
->context
.Dr2
= dr
->dr_mirror
[2];
316 th
->context
.Dr3
= dr
->dr_mirror
[3];
317 /* th->context.Dr6 = dr->dr_status_mirror;
318 FIXME: should we set dr6 also ?? */
319 th
->context
.Dr7
= dr
->dr_control_mirror
;
322 th
->debug_registers_changed
= false;
327 i386_thread_added (windows_thread_info
*th
)
329 th
->debug_registers_changed
= true;
333 i386_single_step (windows_thread_info
*th
)
336 if (windows_process
.wow64_process
)
337 th
->wow64_context
.EFlags
|= FLAG_TRACE_BIT
;
340 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
343 /* An array of offset mappings into a Win32 Context structure.
344 This is a one-to-one mapping which is indexed by gdb's register
345 numbers. It retrieves an offset into the context structure where
346 the 4 byte register is located.
347 An offset value of -1 indicates that Win32 does not provide this
348 register in it's CONTEXT structure. In this case regptr will return
349 a pointer into a dummy register. */
351 #define context_offset(x) (offsetof (WOW64_CONTEXT, x))
353 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
355 static const int i386_mappings
[] = {
356 context_offset (Eax
),
357 context_offset (Ecx
),
358 context_offset (Edx
),
359 context_offset (Ebx
),
360 context_offset (Esp
),
361 context_offset (Ebp
),
362 context_offset (Esi
),
363 context_offset (Edi
),
364 context_offset (Eip
),
365 context_offset (EFlags
),
366 context_offset (SegCs
),
367 context_offset (SegSs
),
368 context_offset (SegDs
),
369 context_offset (SegEs
),
370 context_offset (SegFs
),
371 context_offset (SegGs
),
372 context_offset (FloatSave
.RegisterArea
[0 * 10]),
373 context_offset (FloatSave
.RegisterArea
[1 * 10]),
374 context_offset (FloatSave
.RegisterArea
[2 * 10]),
375 context_offset (FloatSave
.RegisterArea
[3 * 10]),
376 context_offset (FloatSave
.RegisterArea
[4 * 10]),
377 context_offset (FloatSave
.RegisterArea
[5 * 10]),
378 context_offset (FloatSave
.RegisterArea
[6 * 10]),
379 context_offset (FloatSave
.RegisterArea
[7 * 10]),
380 context_offset (FloatSave
.ControlWord
),
381 context_offset (FloatSave
.StatusWord
),
382 context_offset (FloatSave
.TagWord
),
383 context_offset (FloatSave
.ErrorSelector
),
384 context_offset (FloatSave
.ErrorOffset
),
385 context_offset (FloatSave
.DataSelector
),
386 context_offset (FloatSave
.DataOffset
),
387 context_offset (FloatSave
.ErrorSelector
),
389 context_offset (ExtendedRegisters
[10 * 16]),
390 context_offset (ExtendedRegisters
[11 * 16]),
391 context_offset (ExtendedRegisters
[12 * 16]),
392 context_offset (ExtendedRegisters
[13 * 16]),
393 context_offset (ExtendedRegisters
[14 * 16]),
394 context_offset (ExtendedRegisters
[15 * 16]),
395 context_offset (ExtendedRegisters
[16 * 16]),
396 context_offset (ExtendedRegisters
[17 * 16]),
398 context_offset (ExtendedRegisters
[24])
400 #undef context_offset
404 #define context_offset(x) (offsetof (CONTEXT, x))
405 static const int amd64_mappings
[] =
407 context_offset (Rax
),
408 context_offset (Rbx
),
409 context_offset (Rcx
),
410 context_offset (Rdx
),
411 context_offset (Rsi
),
412 context_offset (Rdi
),
413 context_offset (Rbp
),
414 context_offset (Rsp
),
417 context_offset (R10
),
418 context_offset (R11
),
419 context_offset (R12
),
420 context_offset (R13
),
421 context_offset (R14
),
422 context_offset (R15
),
423 context_offset (Rip
),
424 context_offset (EFlags
),
425 context_offset (SegCs
),
426 context_offset (SegSs
),
427 context_offset (SegDs
),
428 context_offset (SegEs
),
429 context_offset (SegFs
),
430 context_offset (SegGs
),
431 context_offset (FloatSave
.FloatRegisters
[0]),
432 context_offset (FloatSave
.FloatRegisters
[1]),
433 context_offset (FloatSave
.FloatRegisters
[2]),
434 context_offset (FloatSave
.FloatRegisters
[3]),
435 context_offset (FloatSave
.FloatRegisters
[4]),
436 context_offset (FloatSave
.FloatRegisters
[5]),
437 context_offset (FloatSave
.FloatRegisters
[6]),
438 context_offset (FloatSave
.FloatRegisters
[7]),
439 context_offset (FloatSave
.ControlWord
),
440 context_offset (FloatSave
.StatusWord
),
441 context_offset (FloatSave
.TagWord
),
442 context_offset (FloatSave
.ErrorSelector
),
443 context_offset (FloatSave
.ErrorOffset
),
444 context_offset (FloatSave
.DataSelector
),
445 context_offset (FloatSave
.DataOffset
),
446 context_offset (FloatSave
.ErrorSelector
)
448 context_offset (Xmm0
),
449 context_offset (Xmm1
),
450 context_offset (Xmm2
),
451 context_offset (Xmm3
),
452 context_offset (Xmm4
),
453 context_offset (Xmm5
),
454 context_offset (Xmm6
),
455 context_offset (Xmm7
),
456 context_offset (Xmm8
),
457 context_offset (Xmm9
),
458 context_offset (Xmm10
),
459 context_offset (Xmm11
),
460 context_offset (Xmm12
),
461 context_offset (Xmm13
),
462 context_offset (Xmm14
),
463 context_offset (Xmm15
),
465 context_offset (FloatSave
.MxCsr
)
467 #undef context_offset
469 #endif /* __x86_64__ */
471 /* Return true if R is the FISEG register. */
473 is_fiseg_register (int r
)
476 if (!windows_process
.wow64_process
)
477 return r
== AMD64_FISEG_REGNUM
;
480 return r
== I386_FISEG_REGNUM
;
483 /* Return true if R is the FOP register. */
485 is_fop_register (int r
)
488 if (!windows_process
.wow64_process
)
489 return r
== AMD64_FOP_REGNUM
;
492 return r
== I386_FOP_REGNUM
;
495 /* Return true if R is a segment register. */
497 is_segment_register (int r
)
500 if (!windows_process
.wow64_process
)
501 return r
>= AMD64_CS_REGNUM
&& r
<= AMD64_GS_REGNUM
;
504 return r
>= I386_CS_REGNUM
&& r
<= I386_GS_REGNUM
;
507 /* Fetch register from gdbserver regcache data. */
509 i386_fetch_inferior_register (struct regcache
*regcache
,
510 windows_thread_info
*th
, int r
)
514 if (!windows_process
.wow64_process
)
515 mappings
= amd64_mappings
;
518 mappings
= i386_mappings
;
520 char *context_offset
;
522 if (windows_process
.wow64_process
)
523 context_offset
= (char *) &th
->wow64_context
+ mappings
[r
];
526 context_offset
= (char *) &th
->context
+ mappings
[r
];
528 /* GDB treats some registers as 32-bit, where they are in fact only
529 16 bits long. These cases must be handled specially to avoid
530 reading extraneous bits from the context. */
531 if (is_fiseg_register (r
) || is_segment_register (r
))
533 gdb_byte bytes
[4] = {};
534 memcpy (bytes
, context_offset
, 2);
535 supply_register (regcache
, r
, bytes
);
537 else if (is_fop_register (r
))
539 long l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
540 supply_register (regcache
, r
, (char *) &l
);
543 supply_register (regcache
, r
, context_offset
);
546 /* Store a new register value into the thread context of TH. */
548 i386_store_inferior_register (struct regcache
*regcache
,
549 windows_thread_info
*th
, int r
)
553 if (!windows_process
.wow64_process
)
554 mappings
= amd64_mappings
;
557 mappings
= i386_mappings
;
559 char *context_offset
;
561 if (windows_process
.wow64_process
)
562 context_offset
= (char *) &th
->wow64_context
+ mappings
[r
];
565 context_offset
= (char *) &th
->context
+ mappings
[r
];
567 /* GDB treats some registers as 32-bit, where they are in fact only
568 16 bits long. These cases must be handled specially to avoid
569 overwriting other registers in the context. */
570 if (is_fiseg_register (r
) || is_segment_register (r
))
573 collect_register (regcache
, r
, bytes
);
574 memcpy (context_offset
, bytes
, 2);
576 else if (is_fop_register (r
))
579 collect_register (regcache
, r
, bytes
);
580 /* The value of FOP occupies the top two bytes in the context,
581 so write the two low-order bytes from the cache into the
583 memcpy (context_offset
+ 2, bytes
, 2);
586 collect_register (regcache
, r
, context_offset
);
589 static const unsigned char i386_win32_breakpoint
= 0xcc;
590 #define i386_win32_breakpoint_len 1
593 i386_arch_setup (void)
595 struct target_desc
*tdesc
;
598 tdesc
= amd64_create_target_description (X86_XSTATE_SSE_MASK
, false,
600 init_target_desc (tdesc
, amd64_expedite_regs
);
604 tdesc
= i386_create_target_description (X86_XSTATE_SSE_MASK
, false, false);
605 init_target_desc (tdesc
, i386_expedite_regs
);
607 wow64_win32_tdesc
= tdesc
;
613 /* Implement win32_target_ops "num_regs" method. */
616 i386_win32_num_regs (void)
620 if (!windows_process
.wow64_process
)
621 num_regs
= sizeof (amd64_mappings
) / sizeof (amd64_mappings
[0]);
624 num_regs
= sizeof (i386_mappings
) / sizeof (i386_mappings
[0]);
628 /* Implement win32_target_ops "get_pc" method. */
631 i386_win32_get_pc (struct regcache
*regcache
)
633 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
639 collect_register_by_name (regcache
, "rip", &pc
);
640 return (CORE_ADDR
) pc
;
646 collect_register_by_name (regcache
, "eip", &pc
);
647 return (CORE_ADDR
) pc
;
651 /* Implement win32_target_ops "set_pc" method. */
654 i386_win32_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
656 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
662 supply_register_by_name (regcache
, "rip", &newpc
);
668 supply_register_by_name (regcache
, "eip", &newpc
);
672 struct win32_target_ops the_low_target
= {
676 i386_get_thread_context
,
677 i386_prepare_to_resume
,
679 i386_fetch_inferior_register
,
680 i386_store_inferior_register
,
682 &i386_win32_breakpoint
,
683 i386_win32_breakpoint_len
,
687 i386_supports_z_point_type
,
690 x86_stopped_by_watchpoint
,
691 x86_stopped_data_address