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 auto th
= static_cast<windows_thread_info
*> (thread
->target_data ());
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
)
87 = static_cast<windows_thread_info
*> (current_thread
->target_data ());
89 win32_require_context (th
);
91 return windows_process
.with_context (th
, [&] (auto *context
) -> DWORD64
95 return context->Dr ## DR
108 gdb_assert_not_reached ("unhandled dr");
113 x86_dr_low_get_addr (int regnum
)
115 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
117 return win32_get_current_dr (regnum
- DR_FIRSTADDR
);
121 x86_dr_low_get_control (void)
123 return win32_get_current_dr (7);
126 /* Get the value of the DR6 debug status register from the inferior
127 and record it in STATE. */
130 x86_dr_low_get_status (void)
132 return win32_get_current_dr (6);
135 /* Low-level function vector. */
136 struct x86_dr_low_type x86_dr_low
=
138 x86_dr_low_set_control
,
141 x86_dr_low_get_status
,
142 x86_dr_low_get_control
,
146 /* Breakpoint/watchpoint support. */
149 i386_supports_z_point_type (char z_type
)
154 case Z_PACKET_WRITE_WP
:
155 case Z_PACKET_ACCESS_WP
:
163 i386_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
164 int size
, struct raw_breakpoint
*bp
)
168 case raw_bkpt_type_hw
:
169 case raw_bkpt_type_write_wp
:
170 case raw_bkpt_type_access_wp
:
172 enum target_hw_bp_type hw_type
173 = raw_bkpt_type_to_target_hw_bp_type (type
);
175 return x86_dr_insert_watchpoint (&debug_reg_state
,
176 hw_type
, addr
, size
);
185 i386_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
186 int size
, struct raw_breakpoint
*bp
)
190 case raw_bkpt_type_hw
:
191 case raw_bkpt_type_write_wp
:
192 case raw_bkpt_type_access_wp
:
194 enum target_hw_bp_type hw_type
195 = raw_bkpt_type_to_target_hw_bp_type (type
);
197 return x86_dr_remove_watchpoint (&debug_reg_state
,
198 hw_type
, addr
, size
);
207 x86_stopped_by_watchpoint (void)
209 return x86_dr_stopped_by_watchpoint (&debug_reg_state
);
213 x86_stopped_data_address (void)
216 if (x86_dr_stopped_data_address (&debug_reg_state
, &addr
))
222 i386_initial_stuff (void)
224 x86_low_init_dregs (&debug_reg_state
);
228 i386_get_thread_context (windows_thread_info
*th
)
230 windows_process
.with_context (th
, [&] (auto *context
)
232 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
233 the system doesn't support extended registers. */
234 static DWORD extended_registers
235 = WindowsContext
<decltype(context
)>::extended
;
238 context
->ContextFlags
= (WindowsContext
<decltype(context
)>::full
239 | WindowsContext
<decltype(context
)>::floating
240 | WindowsContext
<decltype(context
)>::debug
241 | extended_registers
);
243 BOOL ret
= get_thread_context (th
->h
, context
);
246 DWORD e
= GetLastError ();
248 if (extended_registers
&& e
== ERROR_INVALID_PARAMETER
)
250 extended_registers
= 0;
254 error ("GetThreadContext failure %ld\n", (long) e
);
260 i386_prepare_to_resume (windows_thread_info
*th
)
262 if (th
->debug_registers_changed
)
264 struct x86_debug_reg_state
*dr
= &debug_reg_state
;
266 win32_require_context (th
);
268 windows_process
.with_context (th
, [&] (auto *context
)
270 context
->Dr0
= dr
->dr_mirror
[0];
271 context
->Dr1
= dr
->dr_mirror
[1];
272 context
->Dr2
= dr
->dr_mirror
[2];
273 context
->Dr3
= dr
->dr_mirror
[3];
274 /* context->Dr6 = dr->dr_status_mirror;
275 FIXME: should we set dr6 also ?? */
276 context
->Dr7
= dr
->dr_control_mirror
;
279 th
->debug_registers_changed
= false;
284 i386_thread_added (windows_thread_info
*th
)
286 th
->debug_registers_changed
= true;
290 i386_single_step (windows_thread_info
*th
)
292 windows_process
.with_context (th
, [] (auto *context
)
294 context
->EFlags
|= FLAG_TRACE_BIT
;
298 /* An array of offset mappings into a Win32 Context structure.
299 This is a one-to-one mapping which is indexed by gdb's register
300 numbers. It retrieves an offset into the context structure where
301 the 4 byte register is located.
302 An offset value of -1 indicates that Win32 does not provide this
303 register in it's CONTEXT structure. In this case regptr will return
304 a pointer into a dummy register. */
306 #define context_offset(x) (offsetof (WOW64_CONTEXT, x))
308 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
310 static const int i386_mappings
[] = {
311 context_offset (Eax
),
312 context_offset (Ecx
),
313 context_offset (Edx
),
314 context_offset (Ebx
),
315 context_offset (Esp
),
316 context_offset (Ebp
),
317 context_offset (Esi
),
318 context_offset (Edi
),
319 context_offset (Eip
),
320 context_offset (EFlags
),
321 context_offset (SegCs
),
322 context_offset (SegSs
),
323 context_offset (SegDs
),
324 context_offset (SegEs
),
325 context_offset (SegFs
),
326 context_offset (SegGs
),
327 context_offset (FloatSave
.RegisterArea
[0 * 10]),
328 context_offset (FloatSave
.RegisterArea
[1 * 10]),
329 context_offset (FloatSave
.RegisterArea
[2 * 10]),
330 context_offset (FloatSave
.RegisterArea
[3 * 10]),
331 context_offset (FloatSave
.RegisterArea
[4 * 10]),
332 context_offset (FloatSave
.RegisterArea
[5 * 10]),
333 context_offset (FloatSave
.RegisterArea
[6 * 10]),
334 context_offset (FloatSave
.RegisterArea
[7 * 10]),
335 context_offset (FloatSave
.ControlWord
),
336 context_offset (FloatSave
.StatusWord
),
337 context_offset (FloatSave
.TagWord
),
338 context_offset (FloatSave
.ErrorSelector
),
339 context_offset (FloatSave
.ErrorOffset
),
340 context_offset (FloatSave
.DataSelector
),
341 context_offset (FloatSave
.DataOffset
),
342 context_offset (FloatSave
.ErrorSelector
),
344 context_offset (ExtendedRegisters
[10 * 16]),
345 context_offset (ExtendedRegisters
[11 * 16]),
346 context_offset (ExtendedRegisters
[12 * 16]),
347 context_offset (ExtendedRegisters
[13 * 16]),
348 context_offset (ExtendedRegisters
[14 * 16]),
349 context_offset (ExtendedRegisters
[15 * 16]),
350 context_offset (ExtendedRegisters
[16 * 16]),
351 context_offset (ExtendedRegisters
[17 * 16]),
353 context_offset (ExtendedRegisters
[24])
355 #undef context_offset
359 #define context_offset(x) (offsetof (CONTEXT, x))
360 static const int amd64_mappings
[] =
362 context_offset (Rax
),
363 context_offset (Rbx
),
364 context_offset (Rcx
),
365 context_offset (Rdx
),
366 context_offset (Rsi
),
367 context_offset (Rdi
),
368 context_offset (Rbp
),
369 context_offset (Rsp
),
372 context_offset (R10
),
373 context_offset (R11
),
374 context_offset (R12
),
375 context_offset (R13
),
376 context_offset (R14
),
377 context_offset (R15
),
378 context_offset (Rip
),
379 context_offset (EFlags
),
380 context_offset (SegCs
),
381 context_offset (SegSs
),
382 context_offset (SegDs
),
383 context_offset (SegEs
),
384 context_offset (SegFs
),
385 context_offset (SegGs
),
386 context_offset (FloatSave
.FloatRegisters
[0]),
387 context_offset (FloatSave
.FloatRegisters
[1]),
388 context_offset (FloatSave
.FloatRegisters
[2]),
389 context_offset (FloatSave
.FloatRegisters
[3]),
390 context_offset (FloatSave
.FloatRegisters
[4]),
391 context_offset (FloatSave
.FloatRegisters
[5]),
392 context_offset (FloatSave
.FloatRegisters
[6]),
393 context_offset (FloatSave
.FloatRegisters
[7]),
394 context_offset (FloatSave
.ControlWord
),
395 context_offset (FloatSave
.StatusWord
),
396 context_offset (FloatSave
.TagWord
),
397 context_offset (FloatSave
.ErrorSelector
),
398 context_offset (FloatSave
.ErrorOffset
),
399 context_offset (FloatSave
.DataSelector
),
400 context_offset (FloatSave
.DataOffset
),
401 context_offset (FloatSave
.ErrorSelector
)
403 context_offset (Xmm0
),
404 context_offset (Xmm1
),
405 context_offset (Xmm2
),
406 context_offset (Xmm3
),
407 context_offset (Xmm4
),
408 context_offset (Xmm5
),
409 context_offset (Xmm6
),
410 context_offset (Xmm7
),
411 context_offset (Xmm8
),
412 context_offset (Xmm9
),
413 context_offset (Xmm10
),
414 context_offset (Xmm11
),
415 context_offset (Xmm12
),
416 context_offset (Xmm13
),
417 context_offset (Xmm14
),
418 context_offset (Xmm15
),
420 context_offset (FloatSave
.MxCsr
)
422 #undef context_offset
424 #endif /* __x86_64__ */
426 /* Return true if R is the FISEG register. */
428 is_fiseg_register (int r
)
431 if (!windows_process
.wow64_process
)
432 return r
== AMD64_FISEG_REGNUM
;
435 return r
== I386_FISEG_REGNUM
;
438 /* Return true if R is the FOP register. */
440 is_fop_register (int r
)
443 if (!windows_process
.wow64_process
)
444 return r
== AMD64_FOP_REGNUM
;
447 return r
== I386_FOP_REGNUM
;
450 /* Return true if R is a segment register. */
452 is_segment_register (int r
)
455 if (!windows_process
.wow64_process
)
456 return r
>= AMD64_CS_REGNUM
&& r
<= AMD64_GS_REGNUM
;
459 return r
>= I386_CS_REGNUM
&& r
<= I386_GS_REGNUM
;
462 /* Fetch register from gdbserver regcache data. */
464 i386_fetch_inferior_register (struct regcache
*regcache
,
465 windows_thread_info
*th
, int r
)
469 if (!windows_process
.wow64_process
)
470 mappings
= amd64_mappings
;
473 mappings
= i386_mappings
;
475 char *context_offset
= windows_process
.with_context (th
, [&] (auto *context
)
477 return (char *) context
+ mappings
[r
];
480 /* GDB treats some registers as 32-bit, where they are in fact only
481 16 bits long. These cases must be handled specially to avoid
482 reading extraneous bits from the context. */
483 if (is_fiseg_register (r
) || is_segment_register (r
))
485 gdb_byte bytes
[4] = {};
486 memcpy (bytes
, context_offset
, 2);
487 supply_register (regcache
, r
, bytes
);
489 else if (is_fop_register (r
))
491 long l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
492 supply_register (regcache
, r
, (char *) &l
);
495 supply_register (regcache
, r
, context_offset
);
498 /* Store a new register value into the thread context of TH. */
500 i386_store_inferior_register (struct regcache
*regcache
,
501 windows_thread_info
*th
, int r
)
505 if (!windows_process
.wow64_process
)
506 mappings
= amd64_mappings
;
509 mappings
= i386_mappings
;
511 char *context_offset
= windows_process
.with_context (th
, [&] (auto *context
)
513 return (char *) context
+ mappings
[r
];
516 /* GDB treats some registers as 32-bit, where they are in fact only
517 16 bits long. These cases must be handled specially to avoid
518 overwriting other registers in the context. */
519 if (is_fiseg_register (r
) || is_segment_register (r
))
522 collect_register (regcache
, r
, bytes
);
523 memcpy (context_offset
, bytes
, 2);
525 else if (is_fop_register (r
))
528 collect_register (regcache
, r
, bytes
);
529 /* The value of FOP occupies the top two bytes in the context,
530 so write the two low-order bytes from the cache into the
532 memcpy (context_offset
+ 2, bytes
, 2);
535 collect_register (regcache
, r
, context_offset
);
538 static const unsigned char i386_win32_breakpoint
= 0xcc;
539 #define i386_win32_breakpoint_len 1
542 i386_arch_setup (void)
544 struct target_desc
*tdesc
;
547 tdesc
= amd64_create_target_description (X86_XSTATE_SSE_MASK
, false,
549 init_target_desc (tdesc
, amd64_expedite_regs
, WINDOWS_OSABI
);
553 tdesc
= i386_create_target_description (X86_XSTATE_SSE_MASK
, false, false);
554 init_target_desc (tdesc
, i386_expedite_regs
, WINDOWS_OSABI
);
556 wow64_win32_tdesc
= tdesc
;
562 /* Implement win32_target_ops "num_regs" method. */
565 i386_win32_num_regs (void)
569 if (!windows_process
.wow64_process
)
570 num_regs
= sizeof (amd64_mappings
) / sizeof (amd64_mappings
[0]);
573 num_regs
= sizeof (i386_mappings
) / sizeof (i386_mappings
[0]);
577 /* Implement win32_target_ops "get_pc" method. */
580 i386_win32_get_pc (struct regcache
*regcache
)
582 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
588 collect_register_by_name (regcache
, "rip", &pc
);
589 return (CORE_ADDR
) pc
;
595 collect_register_by_name (regcache
, "eip", &pc
);
596 return (CORE_ADDR
) pc
;
600 /* Implement win32_target_ops "set_pc" method. */
603 i386_win32_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
605 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
611 supply_register_by_name (regcache
, "rip", &newpc
);
617 supply_register_by_name (regcache
, "eip", &newpc
);
621 struct win32_target_ops the_low_target
= {
625 i386_get_thread_context
,
626 i386_prepare_to_resume
,
628 i386_fetch_inferior_register
,
629 i386_store_inferior_register
,
631 &i386_win32_breakpoint
,
632 i386_win32_breakpoint_len
,
636 i386_supports_z_point_type
,
639 x86_stopped_by_watchpoint
,
640 x86_stopped_data_address