Automatic date update in version.in
[binutils-gdb.git] / gdbserver / win32-i386-low.cc
blob7898ac12bcf906508158a202d08165f2906b6a9c
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"
19 #include "x86-low.h"
20 #include "gdbsupport/x86-xstate.h"
21 #ifdef __x86_64__
22 #include "arch/amd64.h"
23 #endif
24 #include "arch/i386.h"
25 #include "tdesc.h"
26 #include "x86-tdesc.h"
28 using namespace windows_nat;
30 #ifndef CONTEXT_EXTENDED_REGISTERS
31 #define CONTEXT_EXTENDED_REGISTERS 0
32 #endif
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;
50 static void
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. */
62 static void
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. */
73 static void
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
81 context. */
83 static DWORD64
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);
91 #ifdef __x86_64__
92 #define RET_DR(DR) \
93 case DR: \
94 return th->wow64_context.Dr ## DR
96 if (windows_process.wow64_process)
98 switch (dr)
100 RET_DR (0);
101 RET_DR (1);
102 RET_DR (2);
103 RET_DR (3);
104 RET_DR (6);
105 RET_DR (7);
108 else
109 #undef RET_DR
110 #endif
111 #define RET_DR(DR) \
112 case DR: \
113 return th->context.Dr ## DR
116 switch (dr)
118 RET_DR (0);
119 RET_DR (1);
120 RET_DR (2);
121 RET_DR (3);
122 RET_DR (6);
123 RET_DR (7);
127 #undef RET_DR
129 gdb_assert_not_reached ("unhandled dr");
132 static CORE_ADDR
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);
140 static unsigned long
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. */
149 static unsigned long
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,
159 x86_dr_low_set_addr,
160 x86_dr_low_get_addr,
161 x86_dr_low_get_status,
162 x86_dr_low_get_control,
163 sizeof (void *),
166 /* Breakpoint/watchpoint support. */
168 static int
169 i386_supports_z_point_type (char z_type)
171 switch (z_type)
173 case Z_PACKET_HW_BP:
174 case Z_PACKET_WRITE_WP:
175 case Z_PACKET_ACCESS_WP:
176 return 1;
177 default:
178 return 0;
182 static int
183 i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
184 int size, struct raw_breakpoint *bp)
186 switch (type)
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);
198 default:
199 /* Unsupported. */
200 return 1;
204 static int
205 i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
206 int size, struct raw_breakpoint *bp)
208 switch (type)
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);
220 default:
221 /* Unsupported. */
222 return 1;
226 static int
227 x86_stopped_by_watchpoint (void)
229 return x86_dr_stopped_by_watchpoint (&debug_reg_state);
232 static CORE_ADDR
233 x86_stopped_data_address (void)
235 CORE_ADDR addr;
236 if (x86_dr_stopped_data_address (&debug_reg_state, &addr))
237 return addr;
238 return 0;
241 static void
242 i386_initial_stuff (void)
244 x86_low_init_dregs (&debug_reg_state);
247 static void
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;
253 #ifdef __x86_64__
254 static DWORD wow64_extended_registers = WOW64_CONTEXT_EXTENDED_REGISTERS;
255 #endif
257 again:
258 #ifdef __x86_64__
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);
264 else
265 #endif
266 th->context.ContextFlags = (CONTEXT_FULL
267 | CONTEXT_FLOATING_POINT
268 | CONTEXT_DEBUG_REGISTERS
269 | extended_registers);
271 BOOL ret;
272 #ifdef __x86_64__
273 if (windows_process.wow64_process)
274 ret = Wow64GetThreadContext (th->h, &th->wow64_context);
275 else
276 #endif
277 ret = GetThreadContext (th->h, &th->context);
278 if (!ret)
280 DWORD e = GetLastError ();
282 #ifdef __x86_64__
283 if (windows_process.wow64_process)
285 if (wow64_extended_registers && e == ERROR_INVALID_PARAMETER)
287 wow64_extended_registers = 0;
288 goto again;
291 else
292 #endif
294 if (extended_registers && e == ERROR_INVALID_PARAMETER)
296 extended_registers = 0;
297 goto again;
301 error ("GetThreadContext failure %ld\n", (long) e);
305 static void
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);
314 #ifdef __x86_64__
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;
325 else
326 #endif
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;
341 static void
342 i386_thread_added (windows_thread_info *th)
344 th->debug_registers_changed = true;
347 static void
348 i386_single_step (windows_thread_info *th)
350 #ifdef __x86_64__
351 if (windows_process.wow64_process)
352 th->wow64_context.EFlags |= FLAG_TRACE_BIT;
353 else
354 #endif
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. */
365 #ifdef __x86_64__
366 #define context_offset(x) (offsetof (WOW64_CONTEXT, x))
367 #else
368 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
369 #endif
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),
403 /* XMM0-7 */
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]),
412 /* MXCSR */
413 context_offset (ExtendedRegisters[24])
415 #undef context_offset
417 #ifdef __x86_64__
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),
430 context_offset (R8),
431 context_offset (R9),
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)
462 /* XMM0-7 */ ,
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),
479 /* MXCSR */
480 context_offset (FloatSave.MxCsr)
482 #undef context_offset
484 #endif /* __x86_64__ */
486 /* Return true if R is the FISEG register. */
487 static bool
488 is_fiseg_register (int r)
490 #ifdef __x86_64__
491 if (!windows_process.wow64_process)
492 return r == AMD64_FISEG_REGNUM;
493 else
494 #endif
495 return r == I386_FISEG_REGNUM;
498 /* Return true if R is the FOP register. */
499 static bool
500 is_fop_register (int r)
502 #ifdef __x86_64__
503 if (!windows_process.wow64_process)
504 return r == AMD64_FOP_REGNUM;
505 else
506 #endif
507 return r == I386_FOP_REGNUM;
510 /* Return true if R is a segment register. */
511 static bool
512 is_segment_register (int r)
514 #ifdef __x86_64__
515 if (!windows_process.wow64_process)
516 return r >= AMD64_CS_REGNUM && r <= AMD64_GS_REGNUM;
517 else
518 #endif
519 return r >= I386_CS_REGNUM && r <= I386_GS_REGNUM;
522 /* Fetch register from gdbserver regcache data. */
523 static void
524 i386_fetch_inferior_register (struct regcache *regcache,
525 windows_thread_info *th, int r)
527 const int *mappings;
528 #ifdef __x86_64__
529 if (!windows_process.wow64_process)
530 mappings = amd64_mappings;
531 else
532 #endif
533 mappings = i386_mappings;
535 char *context_offset;
536 #ifdef __x86_64__
537 if (windows_process.wow64_process)
538 context_offset = (char *) &th->wow64_context + mappings[r];
539 else
540 #endif
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);
557 else
558 supply_register (regcache, r, context_offset);
561 /* Store a new register value into the thread context of TH. */
562 static void
563 i386_store_inferior_register (struct regcache *regcache,
564 windows_thread_info *th, int r)
566 const int *mappings;
567 #ifdef __x86_64__
568 if (!windows_process.wow64_process)
569 mappings = amd64_mappings;
570 else
571 #endif
572 mappings = i386_mappings;
574 char *context_offset;
575 #ifdef __x86_64__
576 if (windows_process.wow64_process)
577 context_offset = (char *) &th->wow64_context + mappings[r];
578 else
579 #endif
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))
587 gdb_byte bytes[4];
588 collect_register (regcache, r, bytes);
589 memcpy (context_offset, bytes, 2);
591 else if (is_fop_register (r))
593 gdb_byte bytes[4];
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
597 appropriate spot. */
598 memcpy (context_offset + 2, bytes, 2);
600 else
601 collect_register (regcache, r, context_offset);
604 static const unsigned char i386_win32_breakpoint = 0xcc;
605 #define i386_win32_breakpoint_len 1
607 static void
608 i386_arch_setup (void)
610 struct target_desc *tdesc;
612 #ifdef __x86_64__
613 tdesc = amd64_create_target_description (X86_XSTATE_SSE_MASK, false,
614 false, false);
615 init_target_desc (tdesc, amd64_expedite_regs, WINDOWS_OSABI);
616 win32_tdesc = tdesc;
617 #endif
619 tdesc = i386_create_target_description (X86_XSTATE_SSE_MASK, false, false);
620 init_target_desc (tdesc, i386_expedite_regs, WINDOWS_OSABI);
621 #ifdef __x86_64__
622 wow64_win32_tdesc = tdesc;
623 #else
624 win32_tdesc = tdesc;
625 #endif
628 /* Implement win32_target_ops "num_regs" method. */
630 static int
631 i386_win32_num_regs (void)
633 int num_regs;
634 #ifdef __x86_64__
635 if (!windows_process.wow64_process)
636 num_regs = sizeof (amd64_mappings) / sizeof (amd64_mappings[0]);
637 else
638 #endif
639 num_regs = sizeof (i386_mappings) / sizeof (i386_mappings[0]);
640 return num_regs;
643 /* Implement win32_target_ops "get_pc" method. */
645 static CORE_ADDR
646 i386_win32_get_pc (struct regcache *regcache)
648 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
650 if (use_64bit)
652 uint64_t pc;
654 collect_register_by_name (regcache, "rip", &pc);
655 return (CORE_ADDR) pc;
657 else
659 uint32_t pc;
661 collect_register_by_name (regcache, "eip", &pc);
662 return (CORE_ADDR) pc;
666 /* Implement win32_target_ops "set_pc" method. */
668 static void
669 i386_win32_set_pc (struct regcache *regcache, CORE_ADDR pc)
671 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
673 if (use_64bit)
675 uint64_t newpc = pc;
677 supply_register_by_name (regcache, "rip", &newpc);
679 else
681 uint32_t newpc = pc;
683 supply_register_by_name (regcache, "eip", &newpc);
687 struct win32_target_ops the_low_target = {
688 i386_arch_setup,
689 i386_win32_num_regs,
690 i386_initial_stuff,
691 i386_get_thread_context,
692 i386_prepare_to_resume,
693 i386_thread_added,
694 i386_fetch_inferior_register,
695 i386_store_inferior_register,
696 i386_single_step,
697 &i386_win32_breakpoint,
698 i386_win32_breakpoint_len,
700 i386_win32_get_pc,
701 i386_win32_set_pc,
702 i386_supports_z_point_type,
703 i386_insert_point,
704 i386_remove_point,
705 x86_stopped_by_watchpoint,
706 x86_stopped_data_address