Mention support for AMD/znver5 in GAS
[binutils-gdb.git] / gdbserver / win32-i386-low.cc
blob44490300b69312ed593df25aaec0c1d722b37f89
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 "server.h"
19 #include "win32-low.h"
20 #include "x86-low.h"
21 #include "gdbsupport/x86-xstate.h"
22 #ifdef __x86_64__
23 #include "arch/amd64.h"
24 #endif
25 #include "arch/i386.h"
26 #include "tdesc.h"
27 #include "x86-tdesc.h"
29 using namespace windows_nat;
31 #ifndef CONTEXT_EXTENDED_REGISTERS
32 #define CONTEXT_EXTENDED_REGISTERS 0
33 #endif
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;
51 static void
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. */
63 static void
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. */
74 static void
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
82 context. */
84 static DWORD64
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);
92 #ifdef __x86_64__
93 #define RET_DR(DR) \
94 case DR: \
95 return th->wow64_context.Dr ## DR
97 if (windows_process.wow64_process)
99 switch (dr)
101 RET_DR (0);
102 RET_DR (1);
103 RET_DR (2);
104 RET_DR (3);
105 RET_DR (6);
106 RET_DR (7);
109 else
110 #undef RET_DR
111 #endif
112 #define RET_DR(DR) \
113 case DR: \
114 return th->context.Dr ## DR
117 switch (dr)
119 RET_DR (0);
120 RET_DR (1);
121 RET_DR (2);
122 RET_DR (3);
123 RET_DR (6);
124 RET_DR (7);
128 #undef RET_DR
130 gdb_assert_not_reached ("unhandled dr");
133 static CORE_ADDR
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);
141 static unsigned long
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. */
150 static unsigned long
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,
160 x86_dr_low_set_addr,
161 x86_dr_low_get_addr,
162 x86_dr_low_get_status,
163 x86_dr_low_get_control,
164 sizeof (void *),
167 /* Breakpoint/watchpoint support. */
169 static int
170 i386_supports_z_point_type (char z_type)
172 switch (z_type)
174 case Z_PACKET_HW_BP:
175 case Z_PACKET_WRITE_WP:
176 case Z_PACKET_ACCESS_WP:
177 return 1;
178 default:
179 return 0;
183 static int
184 i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
185 int size, struct raw_breakpoint *bp)
187 switch (type)
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);
199 default:
200 /* Unsupported. */
201 return 1;
205 static int
206 i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
207 int size, struct raw_breakpoint *bp)
209 switch (type)
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);
221 default:
222 /* Unsupported. */
223 return 1;
227 static int
228 x86_stopped_by_watchpoint (void)
230 return x86_dr_stopped_by_watchpoint (&debug_reg_state);
233 static CORE_ADDR
234 x86_stopped_data_address (void)
236 CORE_ADDR addr;
237 if (x86_dr_stopped_data_address (&debug_reg_state, &addr))
238 return addr;
239 return 0;
242 static void
243 i386_initial_stuff (void)
245 x86_low_init_dregs (&debug_reg_state);
248 static void
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;
255 again:
256 #ifdef __x86_64__
257 if (windows_process.wow64_process)
258 th->wow64_context.ContextFlags = (CONTEXT_FULL
259 | CONTEXT_FLOATING_POINT
260 | CONTEXT_DEBUG_REGISTERS
261 | extended_registers);
262 else
263 #endif
264 th->context.ContextFlags = (CONTEXT_FULL
265 | CONTEXT_FLOATING_POINT
266 | CONTEXT_DEBUG_REGISTERS
267 | extended_registers);
269 BOOL ret;
270 #ifdef __x86_64__
271 if (windows_process.wow64_process)
272 ret = Wow64GetThreadContext (th->h, &th->wow64_context);
273 else
274 #endif
275 ret = GetThreadContext (th->h, &th->context);
276 if (!ret)
278 DWORD e = GetLastError ();
280 if (extended_registers && e == ERROR_INVALID_PARAMETER)
282 extended_registers = 0;
283 goto again;
286 error ("GetThreadContext failure %ld\n", (long) e);
290 static void
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);
299 #ifdef __x86_64__
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;
310 else
311 #endif
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;
326 static void
327 i386_thread_added (windows_thread_info *th)
329 th->debug_registers_changed = true;
332 static void
333 i386_single_step (windows_thread_info *th)
335 #ifdef __x86_64__
336 if (windows_process.wow64_process)
337 th->wow64_context.EFlags |= FLAG_TRACE_BIT;
338 else
339 #endif
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. */
350 #ifdef __x86_64__
351 #define context_offset(x) (offsetof (WOW64_CONTEXT, x))
352 #else
353 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
354 #endif
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),
388 /* XMM0-7 */
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]),
397 /* MXCSR */
398 context_offset (ExtendedRegisters[24])
400 #undef context_offset
402 #ifdef __x86_64__
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),
415 context_offset (R8),
416 context_offset (R9),
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)
447 /* XMM0-7 */ ,
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),
464 /* MXCSR */
465 context_offset (FloatSave.MxCsr)
467 #undef context_offset
469 #endif /* __x86_64__ */
471 /* Return true if R is the FISEG register. */
472 static bool
473 is_fiseg_register (int r)
475 #ifdef __x86_64__
476 if (!windows_process.wow64_process)
477 return r == AMD64_FISEG_REGNUM;
478 else
479 #endif
480 return r == I386_FISEG_REGNUM;
483 /* Return true if R is the FOP register. */
484 static bool
485 is_fop_register (int r)
487 #ifdef __x86_64__
488 if (!windows_process.wow64_process)
489 return r == AMD64_FOP_REGNUM;
490 else
491 #endif
492 return r == I386_FOP_REGNUM;
495 /* Return true if R is a segment register. */
496 static bool
497 is_segment_register (int r)
499 #ifdef __x86_64__
500 if (!windows_process.wow64_process)
501 return r >= AMD64_CS_REGNUM && r <= AMD64_GS_REGNUM;
502 else
503 #endif
504 return r >= I386_CS_REGNUM && r <= I386_GS_REGNUM;
507 /* Fetch register from gdbserver regcache data. */
508 static void
509 i386_fetch_inferior_register (struct regcache *regcache,
510 windows_thread_info *th, int r)
512 const int *mappings;
513 #ifdef __x86_64__
514 if (!windows_process.wow64_process)
515 mappings = amd64_mappings;
516 else
517 #endif
518 mappings = i386_mappings;
520 char *context_offset;
521 #ifdef __x86_64__
522 if (windows_process.wow64_process)
523 context_offset = (char *) &th->wow64_context + mappings[r];
524 else
525 #endif
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);
542 else
543 supply_register (regcache, r, context_offset);
546 /* Store a new register value into the thread context of TH. */
547 static void
548 i386_store_inferior_register (struct regcache *regcache,
549 windows_thread_info *th, int r)
551 const int *mappings;
552 #ifdef __x86_64__
553 if (!windows_process.wow64_process)
554 mappings = amd64_mappings;
555 else
556 #endif
557 mappings = i386_mappings;
559 char *context_offset;
560 #ifdef __x86_64__
561 if (windows_process.wow64_process)
562 context_offset = (char *) &th->wow64_context + mappings[r];
563 else
564 #endif
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))
572 gdb_byte bytes[4];
573 collect_register (regcache, r, bytes);
574 memcpy (context_offset, bytes, 2);
576 else if (is_fop_register (r))
578 gdb_byte bytes[4];
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
582 appropriate spot. */
583 memcpy (context_offset + 2, bytes, 2);
585 else
586 collect_register (regcache, r, context_offset);
589 static const unsigned char i386_win32_breakpoint = 0xcc;
590 #define i386_win32_breakpoint_len 1
592 static void
593 i386_arch_setup (void)
595 struct target_desc *tdesc;
597 #ifdef __x86_64__
598 tdesc = amd64_create_target_description (X86_XSTATE_SSE_MASK, false,
599 false, false);
600 init_target_desc (tdesc, amd64_expedite_regs);
601 win32_tdesc = tdesc;
602 #endif
604 tdesc = i386_create_target_description (X86_XSTATE_SSE_MASK, false, false);
605 init_target_desc (tdesc, i386_expedite_regs);
606 #ifdef __x86_64__
607 wow64_win32_tdesc = tdesc;
608 #else
609 win32_tdesc = tdesc;
610 #endif
613 /* Implement win32_target_ops "num_regs" method. */
615 static int
616 i386_win32_num_regs (void)
618 int num_regs;
619 #ifdef __x86_64__
620 if (!windows_process.wow64_process)
621 num_regs = sizeof (amd64_mappings) / sizeof (amd64_mappings[0]);
622 else
623 #endif
624 num_regs = sizeof (i386_mappings) / sizeof (i386_mappings[0]);
625 return num_regs;
628 /* Implement win32_target_ops "get_pc" method. */
630 static CORE_ADDR
631 i386_win32_get_pc (struct regcache *regcache)
633 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
635 if (use_64bit)
637 uint64_t pc;
639 collect_register_by_name (regcache, "rip", &pc);
640 return (CORE_ADDR) pc;
642 else
644 uint32_t pc;
646 collect_register_by_name (regcache, "eip", &pc);
647 return (CORE_ADDR) pc;
651 /* Implement win32_target_ops "set_pc" method. */
653 static void
654 i386_win32_set_pc (struct regcache *regcache, CORE_ADDR pc)
656 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
658 if (use_64bit)
660 uint64_t newpc = pc;
662 supply_register_by_name (regcache, "rip", &newpc);
664 else
666 uint32_t newpc = pc;
668 supply_register_by_name (regcache, "eip", &newpc);
672 struct win32_target_ops the_low_target = {
673 i386_arch_setup,
674 i386_win32_num_regs,
675 i386_initial_stuff,
676 i386_get_thread_context,
677 i386_prepare_to_resume,
678 i386_thread_added,
679 i386_fetch_inferior_register,
680 i386_store_inferior_register,
681 i386_single_step,
682 &i386_win32_breakpoint,
683 i386_win32_breakpoint_len,
685 i386_win32_get_pc,
686 i386_win32_set_pc,
687 i386_supports_z_point_type,
688 i386_insert_point,
689 i386_remove_point,
690 x86_stopped_by_watchpoint,
691 x86_stopped_data_address