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
;
256 if (windows_process
.wow64_process
)
257 th
->wow64_context
.ContextFlags
= (CONTEXT_FULL
258 | CONTEXT_FLOATING_POINT
259 | CONTEXT_DEBUG_REGISTERS
260 | extended_registers
);
263 th
->context
.ContextFlags
= (CONTEXT_FULL
264 | CONTEXT_FLOATING_POINT
265 | CONTEXT_DEBUG_REGISTERS
266 | extended_registers
);
270 if (windows_process
.wow64_process
)
271 ret
= Wow64GetThreadContext (th
->h
, &th
->wow64_context
);
274 ret
= GetThreadContext (th
->h
, &th
->context
);
277 DWORD e
= GetLastError ();
279 if (extended_registers
&& e
== ERROR_INVALID_PARAMETER
)
281 extended_registers
= 0;
285 error ("GetThreadContext failure %ld\n", (long) e
);
290 i386_prepare_to_resume (windows_thread_info
*th
)
292 if (th
->debug_registers_changed
)
294 struct x86_debug_reg_state
*dr
= &debug_reg_state
;
296 win32_require_context (th
);
299 if (windows_process
.wow64_process
)
301 th
->wow64_context
.Dr0
= dr
->dr_mirror
[0];
302 th
->wow64_context
.Dr1
= dr
->dr_mirror
[1];
303 th
->wow64_context
.Dr2
= dr
->dr_mirror
[2];
304 th
->wow64_context
.Dr3
= dr
->dr_mirror
[3];
305 /* th->wow64_context.Dr6 = dr->dr_status_mirror;
306 FIXME: should we set dr6 also ?? */
307 th
->wow64_context
.Dr7
= dr
->dr_control_mirror
;
312 th
->context
.Dr0
= dr
->dr_mirror
[0];
313 th
->context
.Dr1
= dr
->dr_mirror
[1];
314 th
->context
.Dr2
= dr
->dr_mirror
[2];
315 th
->context
.Dr3
= dr
->dr_mirror
[3];
316 /* th->context.Dr6 = dr->dr_status_mirror;
317 FIXME: should we set dr6 also ?? */
318 th
->context
.Dr7
= dr
->dr_control_mirror
;
321 th
->debug_registers_changed
= false;
326 i386_thread_added (windows_thread_info
*th
)
328 th
->debug_registers_changed
= true;
332 i386_single_step (windows_thread_info
*th
)
335 if (windows_process
.wow64_process
)
336 th
->wow64_context
.EFlags
|= FLAG_TRACE_BIT
;
339 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
342 /* An array of offset mappings into a Win32 Context structure.
343 This is a one-to-one mapping which is indexed by gdb's register
344 numbers. It retrieves an offset into the context structure where
345 the 4 byte register is located.
346 An offset value of -1 indicates that Win32 does not provide this
347 register in it's CONTEXT structure. In this case regptr will return
348 a pointer into a dummy register. */
350 #define context_offset(x) (offsetof (WOW64_CONTEXT, x))
352 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
354 static const int i386_mappings
[] = {
355 context_offset (Eax
),
356 context_offset (Ecx
),
357 context_offset (Edx
),
358 context_offset (Ebx
),
359 context_offset (Esp
),
360 context_offset (Ebp
),
361 context_offset (Esi
),
362 context_offset (Edi
),
363 context_offset (Eip
),
364 context_offset (EFlags
),
365 context_offset (SegCs
),
366 context_offset (SegSs
),
367 context_offset (SegDs
),
368 context_offset (SegEs
),
369 context_offset (SegFs
),
370 context_offset (SegGs
),
371 context_offset (FloatSave
.RegisterArea
[0 * 10]),
372 context_offset (FloatSave
.RegisterArea
[1 * 10]),
373 context_offset (FloatSave
.RegisterArea
[2 * 10]),
374 context_offset (FloatSave
.RegisterArea
[3 * 10]),
375 context_offset (FloatSave
.RegisterArea
[4 * 10]),
376 context_offset (FloatSave
.RegisterArea
[5 * 10]),
377 context_offset (FloatSave
.RegisterArea
[6 * 10]),
378 context_offset (FloatSave
.RegisterArea
[7 * 10]),
379 context_offset (FloatSave
.ControlWord
),
380 context_offset (FloatSave
.StatusWord
),
381 context_offset (FloatSave
.TagWord
),
382 context_offset (FloatSave
.ErrorSelector
),
383 context_offset (FloatSave
.ErrorOffset
),
384 context_offset (FloatSave
.DataSelector
),
385 context_offset (FloatSave
.DataOffset
),
386 context_offset (FloatSave
.ErrorSelector
),
388 context_offset (ExtendedRegisters
[10 * 16]),
389 context_offset (ExtendedRegisters
[11 * 16]),
390 context_offset (ExtendedRegisters
[12 * 16]),
391 context_offset (ExtendedRegisters
[13 * 16]),
392 context_offset (ExtendedRegisters
[14 * 16]),
393 context_offset (ExtendedRegisters
[15 * 16]),
394 context_offset (ExtendedRegisters
[16 * 16]),
395 context_offset (ExtendedRegisters
[17 * 16]),
397 context_offset (ExtendedRegisters
[24])
399 #undef context_offset
403 #define context_offset(x) (offsetof (CONTEXT, x))
404 static const int amd64_mappings
[] =
406 context_offset (Rax
),
407 context_offset (Rbx
),
408 context_offset (Rcx
),
409 context_offset (Rdx
),
410 context_offset (Rsi
),
411 context_offset (Rdi
),
412 context_offset (Rbp
),
413 context_offset (Rsp
),
416 context_offset (R10
),
417 context_offset (R11
),
418 context_offset (R12
),
419 context_offset (R13
),
420 context_offset (R14
),
421 context_offset (R15
),
422 context_offset (Rip
),
423 context_offset (EFlags
),
424 context_offset (SegCs
),
425 context_offset (SegSs
),
426 context_offset (SegDs
),
427 context_offset (SegEs
),
428 context_offset (SegFs
),
429 context_offset (SegGs
),
430 context_offset (FloatSave
.FloatRegisters
[0]),
431 context_offset (FloatSave
.FloatRegisters
[1]),
432 context_offset (FloatSave
.FloatRegisters
[2]),
433 context_offset (FloatSave
.FloatRegisters
[3]),
434 context_offset (FloatSave
.FloatRegisters
[4]),
435 context_offset (FloatSave
.FloatRegisters
[5]),
436 context_offset (FloatSave
.FloatRegisters
[6]),
437 context_offset (FloatSave
.FloatRegisters
[7]),
438 context_offset (FloatSave
.ControlWord
),
439 context_offset (FloatSave
.StatusWord
),
440 context_offset (FloatSave
.TagWord
),
441 context_offset (FloatSave
.ErrorSelector
),
442 context_offset (FloatSave
.ErrorOffset
),
443 context_offset (FloatSave
.DataSelector
),
444 context_offset (FloatSave
.DataOffset
),
445 context_offset (FloatSave
.ErrorSelector
)
447 context_offset (Xmm0
),
448 context_offset (Xmm1
),
449 context_offset (Xmm2
),
450 context_offset (Xmm3
),
451 context_offset (Xmm4
),
452 context_offset (Xmm5
),
453 context_offset (Xmm6
),
454 context_offset (Xmm7
),
455 context_offset (Xmm8
),
456 context_offset (Xmm9
),
457 context_offset (Xmm10
),
458 context_offset (Xmm11
),
459 context_offset (Xmm12
),
460 context_offset (Xmm13
),
461 context_offset (Xmm14
),
462 context_offset (Xmm15
),
464 context_offset (FloatSave
.MxCsr
)
466 #undef context_offset
468 #endif /* __x86_64__ */
470 /* Return true if R is the FISEG register. */
472 is_fiseg_register (int r
)
475 if (!windows_process
.wow64_process
)
476 return r
== AMD64_FISEG_REGNUM
;
479 return r
== I386_FISEG_REGNUM
;
482 /* Return true if R is the FOP register. */
484 is_fop_register (int r
)
487 if (!windows_process
.wow64_process
)
488 return r
== AMD64_FOP_REGNUM
;
491 return r
== I386_FOP_REGNUM
;
494 /* Return true if R is a segment register. */
496 is_segment_register (int r
)
499 if (!windows_process
.wow64_process
)
500 return r
>= AMD64_CS_REGNUM
&& r
<= AMD64_GS_REGNUM
;
503 return r
>= I386_CS_REGNUM
&& r
<= I386_GS_REGNUM
;
506 /* Fetch register from gdbserver regcache data. */
508 i386_fetch_inferior_register (struct regcache
*regcache
,
509 windows_thread_info
*th
, int r
)
513 if (!windows_process
.wow64_process
)
514 mappings
= amd64_mappings
;
517 mappings
= i386_mappings
;
519 char *context_offset
;
521 if (windows_process
.wow64_process
)
522 context_offset
= (char *) &th
->wow64_context
+ mappings
[r
];
525 context_offset
= (char *) &th
->context
+ mappings
[r
];
527 /* GDB treats some registers as 32-bit, where they are in fact only
528 16 bits long. These cases must be handled specially to avoid
529 reading extraneous bits from the context. */
530 if (is_fiseg_register (r
) || is_segment_register (r
))
532 gdb_byte bytes
[4] = {};
533 memcpy (bytes
, context_offset
, 2);
534 supply_register (regcache
, r
, bytes
);
536 else if (is_fop_register (r
))
538 long l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
539 supply_register (regcache
, r
, (char *) &l
);
542 supply_register (regcache
, r
, context_offset
);
545 /* Store a new register value into the thread context of TH. */
547 i386_store_inferior_register (struct regcache
*regcache
,
548 windows_thread_info
*th
, int r
)
552 if (!windows_process
.wow64_process
)
553 mappings
= amd64_mappings
;
556 mappings
= i386_mappings
;
558 char *context_offset
;
560 if (windows_process
.wow64_process
)
561 context_offset
= (char *) &th
->wow64_context
+ mappings
[r
];
564 context_offset
= (char *) &th
->context
+ mappings
[r
];
566 /* GDB treats some registers as 32-bit, where they are in fact only
567 16 bits long. These cases must be handled specially to avoid
568 overwriting other registers in the context. */
569 if (is_fiseg_register (r
) || is_segment_register (r
))
572 collect_register (regcache
, r
, bytes
);
573 memcpy (context_offset
, bytes
, 2);
575 else if (is_fop_register (r
))
578 collect_register (regcache
, r
, bytes
);
579 /* The value of FOP occupies the top two bytes in the context,
580 so write the two low-order bytes from the cache into the
582 memcpy (context_offset
+ 2, bytes
, 2);
585 collect_register (regcache
, r
, context_offset
);
588 static const unsigned char i386_win32_breakpoint
= 0xcc;
589 #define i386_win32_breakpoint_len 1
592 i386_arch_setup (void)
594 struct target_desc
*tdesc
;
597 tdesc
= amd64_create_target_description (X86_XSTATE_SSE_MASK
, false,
599 init_target_desc (tdesc
, amd64_expedite_regs
, WINDOWS_OSABI
);
603 tdesc
= i386_create_target_description (X86_XSTATE_SSE_MASK
, false, false);
604 init_target_desc (tdesc
, i386_expedite_regs
, WINDOWS_OSABI
);
606 wow64_win32_tdesc
= tdesc
;
612 /* Implement win32_target_ops "num_regs" method. */
615 i386_win32_num_regs (void)
619 if (!windows_process
.wow64_process
)
620 num_regs
= sizeof (amd64_mappings
) / sizeof (amd64_mappings
[0]);
623 num_regs
= sizeof (i386_mappings
) / sizeof (i386_mappings
[0]);
627 /* Implement win32_target_ops "get_pc" method. */
630 i386_win32_get_pc (struct regcache
*regcache
)
632 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
638 collect_register_by_name (regcache
, "rip", &pc
);
639 return (CORE_ADDR
) pc
;
645 collect_register_by_name (regcache
, "eip", &pc
);
646 return (CORE_ADDR
) pc
;
650 /* Implement win32_target_ops "set_pc" method. */
653 i386_win32_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
655 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
661 supply_register_by_name (regcache
, "rip", &newpc
);
667 supply_register_by_name (regcache
, "eip", &newpc
);
671 struct win32_target_ops the_low_target
= {
675 i386_get_thread_context
,
676 i386_prepare_to_resume
,
678 i386_fetch_inferior_register
,
679 i386_store_inferior_register
,
681 &i386_win32_breakpoint
,
682 i386_win32_breakpoint_len
,
686 i386_supports_z_point_type
,
689 x86_stopped_by_watchpoint
,
690 x86_stopped_data_address