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/>. */
18 #include "win32-low.h"
20 #include "gdbsupport/x86-xstate.h"
22 #include "arch/amd64.h"
24 #include "arch/i386.h"
26 #include "x86-tdesc.h"
28 using namespace windows_nat
;
30 #ifndef CONTEXT_EXTENDED_REGISTERS
31 #define CONTEXT_EXTENDED_REGISTERS 0
34 #define I386_FISEG_REGNUM 27
35 #define I386_FOP_REGNUM 31
37 #define I386_CS_REGNUM 10
38 #define I386_GS_REGNUM 15
40 #define AMD64_FISEG_REGNUM 35
41 #define AMD64_FOP_REGNUM 39
43 #define AMD64_CS_REGNUM 18
44 #define AMD64_GS_REGNUM 23
46 #define FLAG_TRACE_BIT 0x100
48 static struct x86_debug_reg_state debug_reg_state
;
51 update_debug_registers (thread_info
*thread
)
53 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
55 /* The actual update is done later just before resuming the lwp,
56 we just mark that the registers need updating. */
57 th
->debug_registers_changed
= true;
60 /* Update the inferior's debug register REGNUM from STATE. */
63 x86_dr_low_set_addr (int regnum
, CORE_ADDR addr
)
65 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
67 /* Only update the threads of this process. */
68 current_process ()->for_each_thread (update_debug_registers
);
71 /* Update the inferior's DR7 debug control register from STATE. */
74 x86_dr_low_set_control (unsigned long control
)
76 /* Only update the threads of this process. */
77 current_process ()->for_each_thread (update_debug_registers
);
80 /* Return the current value of a DR register of the current thread's
84 win32_get_current_dr (int dr
)
86 windows_thread_info
*th
87 = (windows_thread_info
*) thread_target_data (current_thread
);
89 win32_require_context (th
);
94 return th->wow64_context.Dr ## DR
96 if (windows_process
.wow64_process
)
113 return th->context.Dr ## DR
129 gdb_assert_not_reached ("unhandled dr");
133 x86_dr_low_get_addr (int regnum
)
135 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
137 return win32_get_current_dr (regnum
- DR_FIRSTADDR
);
141 x86_dr_low_get_control (void)
143 return win32_get_current_dr (7);
146 /* Get the value of the DR6 debug status register from the inferior
147 and record it in STATE. */
150 x86_dr_low_get_status (void)
152 return win32_get_current_dr (6);
155 /* Low-level function vector. */
156 struct x86_dr_low_type x86_dr_low
=
158 x86_dr_low_set_control
,
161 x86_dr_low_get_status
,
162 x86_dr_low_get_control
,
166 /* Breakpoint/watchpoint support. */
169 i386_supports_z_point_type (char z_type
)
174 case Z_PACKET_WRITE_WP
:
175 case Z_PACKET_ACCESS_WP
:
183 i386_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
184 int size
, struct raw_breakpoint
*bp
)
188 case raw_bkpt_type_hw
:
189 case raw_bkpt_type_write_wp
:
190 case raw_bkpt_type_access_wp
:
192 enum target_hw_bp_type hw_type
193 = raw_bkpt_type_to_target_hw_bp_type (type
);
195 return x86_dr_insert_watchpoint (&debug_reg_state
,
196 hw_type
, addr
, size
);
205 i386_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
206 int size
, struct raw_breakpoint
*bp
)
210 case raw_bkpt_type_hw
:
211 case raw_bkpt_type_write_wp
:
212 case raw_bkpt_type_access_wp
:
214 enum target_hw_bp_type hw_type
215 = raw_bkpt_type_to_target_hw_bp_type (type
);
217 return x86_dr_remove_watchpoint (&debug_reg_state
,
218 hw_type
, addr
, size
);
227 x86_stopped_by_watchpoint (void)
229 return x86_dr_stopped_by_watchpoint (&debug_reg_state
);
233 x86_stopped_data_address (void)
236 if (x86_dr_stopped_data_address (&debug_reg_state
, &addr
))
242 i386_initial_stuff (void)
244 x86_low_init_dregs (&debug_reg_state
);
248 i386_get_thread_context (windows_thread_info
*th
)
250 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
251 the system doesn't support extended registers. */
252 static DWORD extended_registers
= CONTEXT_EXTENDED_REGISTERS
;
254 static DWORD wow64_extended_registers
= WOW64_CONTEXT_EXTENDED_REGISTERS
;
259 if (windows_process
.wow64_process
)
260 th
->wow64_context
.ContextFlags
= (WOW64_CONTEXT_FULL
261 | WOW64_CONTEXT_FLOATING_POINT
262 | WOW64_CONTEXT_DEBUG_REGISTERS
263 | wow64_extended_registers
);
266 th
->context
.ContextFlags
= (CONTEXT_FULL
267 | CONTEXT_FLOATING_POINT
268 | CONTEXT_DEBUG_REGISTERS
269 | extended_registers
);
273 if (windows_process
.wow64_process
)
274 ret
= Wow64GetThreadContext (th
->h
, &th
->wow64_context
);
277 ret
= GetThreadContext (th
->h
, &th
->context
);
280 DWORD e
= GetLastError ();
283 if (windows_process
.wow64_process
)
285 if (wow64_extended_registers
&& e
== ERROR_INVALID_PARAMETER
)
287 wow64_extended_registers
= 0;
294 if (extended_registers
&& e
== ERROR_INVALID_PARAMETER
)
296 extended_registers
= 0;
301 error ("GetThreadContext failure %ld\n", (long) e
);
306 i386_prepare_to_resume (windows_thread_info
*th
)
308 if (th
->debug_registers_changed
)
310 struct x86_debug_reg_state
*dr
= &debug_reg_state
;
312 win32_require_context (th
);
315 if (windows_process
.wow64_process
)
317 th
->wow64_context
.Dr0
= dr
->dr_mirror
[0];
318 th
->wow64_context
.Dr1
= dr
->dr_mirror
[1];
319 th
->wow64_context
.Dr2
= dr
->dr_mirror
[2];
320 th
->wow64_context
.Dr3
= dr
->dr_mirror
[3];
321 /* th->wow64_context.Dr6 = dr->dr_status_mirror;
322 FIXME: should we set dr6 also ?? */
323 th
->wow64_context
.Dr7
= dr
->dr_control_mirror
;
328 th
->context
.Dr0
= dr
->dr_mirror
[0];
329 th
->context
.Dr1
= dr
->dr_mirror
[1];
330 th
->context
.Dr2
= dr
->dr_mirror
[2];
331 th
->context
.Dr3
= dr
->dr_mirror
[3];
332 /* th->context.Dr6 = dr->dr_status_mirror;
333 FIXME: should we set dr6 also ?? */
334 th
->context
.Dr7
= dr
->dr_control_mirror
;
337 th
->debug_registers_changed
= false;
342 i386_thread_added (windows_thread_info
*th
)
344 th
->debug_registers_changed
= true;
348 i386_single_step (windows_thread_info
*th
)
351 if (windows_process
.wow64_process
)
352 th
->wow64_context
.EFlags
|= FLAG_TRACE_BIT
;
355 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
358 /* An array of offset mappings into a Win32 Context structure.
359 This is a one-to-one mapping which is indexed by gdb's register
360 numbers. It retrieves an offset into the context structure where
361 the 4 byte register is located.
362 An offset value of -1 indicates that Win32 does not provide this
363 register in it's CONTEXT structure. In this case regptr will return
364 a pointer into a dummy register. */
366 #define context_offset(x) (offsetof (WOW64_CONTEXT, x))
368 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
370 static const int i386_mappings
[] = {
371 context_offset (Eax
),
372 context_offset (Ecx
),
373 context_offset (Edx
),
374 context_offset (Ebx
),
375 context_offset (Esp
),
376 context_offset (Ebp
),
377 context_offset (Esi
),
378 context_offset (Edi
),
379 context_offset (Eip
),
380 context_offset (EFlags
),
381 context_offset (SegCs
),
382 context_offset (SegSs
),
383 context_offset (SegDs
),
384 context_offset (SegEs
),
385 context_offset (SegFs
),
386 context_offset (SegGs
),
387 context_offset (FloatSave
.RegisterArea
[0 * 10]),
388 context_offset (FloatSave
.RegisterArea
[1 * 10]),
389 context_offset (FloatSave
.RegisterArea
[2 * 10]),
390 context_offset (FloatSave
.RegisterArea
[3 * 10]),
391 context_offset (FloatSave
.RegisterArea
[4 * 10]),
392 context_offset (FloatSave
.RegisterArea
[5 * 10]),
393 context_offset (FloatSave
.RegisterArea
[6 * 10]),
394 context_offset (FloatSave
.RegisterArea
[7 * 10]),
395 context_offset (FloatSave
.ControlWord
),
396 context_offset (FloatSave
.StatusWord
),
397 context_offset (FloatSave
.TagWord
),
398 context_offset (FloatSave
.ErrorSelector
),
399 context_offset (FloatSave
.ErrorOffset
),
400 context_offset (FloatSave
.DataSelector
),
401 context_offset (FloatSave
.DataOffset
),
402 context_offset (FloatSave
.ErrorSelector
),
404 context_offset (ExtendedRegisters
[10 * 16]),
405 context_offset (ExtendedRegisters
[11 * 16]),
406 context_offset (ExtendedRegisters
[12 * 16]),
407 context_offset (ExtendedRegisters
[13 * 16]),
408 context_offset (ExtendedRegisters
[14 * 16]),
409 context_offset (ExtendedRegisters
[15 * 16]),
410 context_offset (ExtendedRegisters
[16 * 16]),
411 context_offset (ExtendedRegisters
[17 * 16]),
413 context_offset (ExtendedRegisters
[24])
415 #undef context_offset
419 #define context_offset(x) (offsetof (CONTEXT, x))
420 static const int amd64_mappings
[] =
422 context_offset (Rax
),
423 context_offset (Rbx
),
424 context_offset (Rcx
),
425 context_offset (Rdx
),
426 context_offset (Rsi
),
427 context_offset (Rdi
),
428 context_offset (Rbp
),
429 context_offset (Rsp
),
432 context_offset (R10
),
433 context_offset (R11
),
434 context_offset (R12
),
435 context_offset (R13
),
436 context_offset (R14
),
437 context_offset (R15
),
438 context_offset (Rip
),
439 context_offset (EFlags
),
440 context_offset (SegCs
),
441 context_offset (SegSs
),
442 context_offset (SegDs
),
443 context_offset (SegEs
),
444 context_offset (SegFs
),
445 context_offset (SegGs
),
446 context_offset (FloatSave
.FloatRegisters
[0]),
447 context_offset (FloatSave
.FloatRegisters
[1]),
448 context_offset (FloatSave
.FloatRegisters
[2]),
449 context_offset (FloatSave
.FloatRegisters
[3]),
450 context_offset (FloatSave
.FloatRegisters
[4]),
451 context_offset (FloatSave
.FloatRegisters
[5]),
452 context_offset (FloatSave
.FloatRegisters
[6]),
453 context_offset (FloatSave
.FloatRegisters
[7]),
454 context_offset (FloatSave
.ControlWord
),
455 context_offset (FloatSave
.StatusWord
),
456 context_offset (FloatSave
.TagWord
),
457 context_offset (FloatSave
.ErrorSelector
),
458 context_offset (FloatSave
.ErrorOffset
),
459 context_offset (FloatSave
.DataSelector
),
460 context_offset (FloatSave
.DataOffset
),
461 context_offset (FloatSave
.ErrorSelector
)
463 context_offset (Xmm0
),
464 context_offset (Xmm1
),
465 context_offset (Xmm2
),
466 context_offset (Xmm3
),
467 context_offset (Xmm4
),
468 context_offset (Xmm5
),
469 context_offset (Xmm6
),
470 context_offset (Xmm7
),
471 context_offset (Xmm8
),
472 context_offset (Xmm9
),
473 context_offset (Xmm10
),
474 context_offset (Xmm11
),
475 context_offset (Xmm12
),
476 context_offset (Xmm13
),
477 context_offset (Xmm14
),
478 context_offset (Xmm15
),
480 context_offset (FloatSave
.MxCsr
)
482 #undef context_offset
484 #endif /* __x86_64__ */
486 /* Return true if R is the FISEG register. */
488 is_fiseg_register (int r
)
491 if (!windows_process
.wow64_process
)
492 return r
== AMD64_FISEG_REGNUM
;
495 return r
== I386_FISEG_REGNUM
;
498 /* Return true if R is the FOP register. */
500 is_fop_register (int r
)
503 if (!windows_process
.wow64_process
)
504 return r
== AMD64_FOP_REGNUM
;
507 return r
== I386_FOP_REGNUM
;
510 /* Return true if R is a segment register. */
512 is_segment_register (int r
)
515 if (!windows_process
.wow64_process
)
516 return r
>= AMD64_CS_REGNUM
&& r
<= AMD64_GS_REGNUM
;
519 return r
>= I386_CS_REGNUM
&& r
<= I386_GS_REGNUM
;
522 /* Fetch register from gdbserver regcache data. */
524 i386_fetch_inferior_register (struct regcache
*regcache
,
525 windows_thread_info
*th
, int r
)
529 if (!windows_process
.wow64_process
)
530 mappings
= amd64_mappings
;
533 mappings
= i386_mappings
;
535 char *context_offset
;
537 if (windows_process
.wow64_process
)
538 context_offset
= (char *) &th
->wow64_context
+ mappings
[r
];
541 context_offset
= (char *) &th
->context
+ mappings
[r
];
543 /* GDB treats some registers as 32-bit, where they are in fact only
544 16 bits long. These cases must be handled specially to avoid
545 reading extraneous bits from the context. */
546 if (is_fiseg_register (r
) || is_segment_register (r
))
548 gdb_byte bytes
[4] = {};
549 memcpy (bytes
, context_offset
, 2);
550 supply_register (regcache
, r
, bytes
);
552 else if (is_fop_register (r
))
554 long l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
555 supply_register (regcache
, r
, (char *) &l
);
558 supply_register (regcache
, r
, context_offset
);
561 /* Store a new register value into the thread context of TH. */
563 i386_store_inferior_register (struct regcache
*regcache
,
564 windows_thread_info
*th
, int r
)
568 if (!windows_process
.wow64_process
)
569 mappings
= amd64_mappings
;
572 mappings
= i386_mappings
;
574 char *context_offset
;
576 if (windows_process
.wow64_process
)
577 context_offset
= (char *) &th
->wow64_context
+ mappings
[r
];
580 context_offset
= (char *) &th
->context
+ mappings
[r
];
582 /* GDB treats some registers as 32-bit, where they are in fact only
583 16 bits long. These cases must be handled specially to avoid
584 overwriting other registers in the context. */
585 if (is_fiseg_register (r
) || is_segment_register (r
))
588 collect_register (regcache
, r
, bytes
);
589 memcpy (context_offset
, bytes
, 2);
591 else if (is_fop_register (r
))
594 collect_register (regcache
, r
, bytes
);
595 /* The value of FOP occupies the top two bytes in the context,
596 so write the two low-order bytes from the cache into the
598 memcpy (context_offset
+ 2, bytes
, 2);
601 collect_register (regcache
, r
, context_offset
);
604 static const unsigned char i386_win32_breakpoint
= 0xcc;
605 #define i386_win32_breakpoint_len 1
608 i386_arch_setup (void)
610 struct target_desc
*tdesc
;
613 tdesc
= amd64_create_target_description (X86_XSTATE_SSE_MASK
, false,
615 init_target_desc (tdesc
, amd64_expedite_regs
, WINDOWS_OSABI
);
619 tdesc
= i386_create_target_description (X86_XSTATE_SSE_MASK
, false, false);
620 init_target_desc (tdesc
, i386_expedite_regs
, WINDOWS_OSABI
);
622 wow64_win32_tdesc
= tdesc
;
628 /* Implement win32_target_ops "num_regs" method. */
631 i386_win32_num_regs (void)
635 if (!windows_process
.wow64_process
)
636 num_regs
= sizeof (amd64_mappings
) / sizeof (amd64_mappings
[0]);
639 num_regs
= sizeof (i386_mappings
) / sizeof (i386_mappings
[0]);
643 /* Implement win32_target_ops "get_pc" method. */
646 i386_win32_get_pc (struct regcache
*regcache
)
648 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
654 collect_register_by_name (regcache
, "rip", &pc
);
655 return (CORE_ADDR
) pc
;
661 collect_register_by_name (regcache
, "eip", &pc
);
662 return (CORE_ADDR
) pc
;
666 /* Implement win32_target_ops "set_pc" method. */
669 i386_win32_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
671 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
677 supply_register_by_name (regcache
, "rip", &newpc
);
683 supply_register_by_name (regcache
, "eip", &newpc
);
687 struct win32_target_ops the_low_target
= {
691 i386_get_thread_context
,
692 i386_prepare_to_resume
,
694 i386_fetch_inferior_register
,
695 i386_store_inferior_register
,
697 &i386_win32_breakpoint
,
698 i386_win32_breakpoint_len
,
702 i386_supports_z_point_type
,
705 x86_stopped_by_watchpoint
,
706 x86_stopped_data_address