Add translations for various sub-directories
[binutils-gdb.git] / gdbserver / win32-i386-low.cc
blobb3e68781fcca15b105b95af24f2bb7290b969292
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 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. */
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 auto th
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
93 #define RET_DR(DR) \
94 case DR: \
95 return context->Dr ## DR
97 switch (dr)
99 RET_DR (0);
100 RET_DR (1);
101 RET_DR (2);
102 RET_DR (3);
103 RET_DR (6);
104 RET_DR (7);
106 #undef RET_DR
108 gdb_assert_not_reached ("unhandled dr");
112 static CORE_ADDR
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);
120 static unsigned long
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. */
129 static unsigned long
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,
139 x86_dr_low_set_addr,
140 x86_dr_low_get_addr,
141 x86_dr_low_get_status,
142 x86_dr_low_get_control,
143 sizeof (void *),
146 /* Breakpoint/watchpoint support. */
148 static int
149 i386_supports_z_point_type (char z_type)
151 switch (z_type)
153 case Z_PACKET_HW_BP:
154 case Z_PACKET_WRITE_WP:
155 case Z_PACKET_ACCESS_WP:
156 return 1;
157 default:
158 return 0;
162 static int
163 i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
164 int size, struct raw_breakpoint *bp)
166 switch (type)
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);
178 default:
179 /* Unsupported. */
180 return 1;
184 static int
185 i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
186 int size, struct raw_breakpoint *bp)
188 switch (type)
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);
200 default:
201 /* Unsupported. */
202 return 1;
206 static int
207 x86_stopped_by_watchpoint (void)
209 return x86_dr_stopped_by_watchpoint (&debug_reg_state);
212 static CORE_ADDR
213 x86_stopped_data_address (void)
215 CORE_ADDR addr;
216 if (x86_dr_stopped_data_address (&debug_reg_state, &addr))
217 return addr;
218 return 0;
221 static void
222 i386_initial_stuff (void)
224 x86_low_init_dregs (&debug_reg_state);
227 static void
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;
237 again:
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);
244 if (!ret)
246 DWORD e = GetLastError ();
248 if (extended_registers && e == ERROR_INVALID_PARAMETER)
250 extended_registers = 0;
251 goto again;
254 error ("GetThreadContext failure %ld\n", (long) e);
259 static void
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;
283 static void
284 i386_thread_added (windows_thread_info *th)
286 th->debug_registers_changed = true;
289 static void
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. */
305 #ifdef __x86_64__
306 #define context_offset(x) (offsetof (WOW64_CONTEXT, x))
307 #else
308 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
309 #endif
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),
343 /* XMM0-7 */
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]),
352 /* MXCSR */
353 context_offset (ExtendedRegisters[24])
355 #undef context_offset
357 #ifdef __x86_64__
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),
370 context_offset (R8),
371 context_offset (R9),
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)
402 /* XMM0-7 */ ,
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),
419 /* MXCSR */
420 context_offset (FloatSave.MxCsr)
422 #undef context_offset
424 #endif /* __x86_64__ */
426 /* Return true if R is the FISEG register. */
427 static bool
428 is_fiseg_register (int r)
430 #ifdef __x86_64__
431 if (!windows_process.wow64_process)
432 return r == AMD64_FISEG_REGNUM;
433 else
434 #endif
435 return r == I386_FISEG_REGNUM;
438 /* Return true if R is the FOP register. */
439 static bool
440 is_fop_register (int r)
442 #ifdef __x86_64__
443 if (!windows_process.wow64_process)
444 return r == AMD64_FOP_REGNUM;
445 else
446 #endif
447 return r == I386_FOP_REGNUM;
450 /* Return true if R is a segment register. */
451 static bool
452 is_segment_register (int r)
454 #ifdef __x86_64__
455 if (!windows_process.wow64_process)
456 return r >= AMD64_CS_REGNUM && r <= AMD64_GS_REGNUM;
457 else
458 #endif
459 return r >= I386_CS_REGNUM && r <= I386_GS_REGNUM;
462 /* Fetch register from gdbserver regcache data. */
463 static void
464 i386_fetch_inferior_register (struct regcache *regcache,
465 windows_thread_info *th, int r)
467 const int *mappings;
468 #ifdef __x86_64__
469 if (!windows_process.wow64_process)
470 mappings = amd64_mappings;
471 else
472 #endif
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);
494 else
495 supply_register (regcache, r, context_offset);
498 /* Store a new register value into the thread context of TH. */
499 static void
500 i386_store_inferior_register (struct regcache *regcache,
501 windows_thread_info *th, int r)
503 const int *mappings;
504 #ifdef __x86_64__
505 if (!windows_process.wow64_process)
506 mappings = amd64_mappings;
507 else
508 #endif
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))
521 gdb_byte bytes[4];
522 collect_register (regcache, r, bytes);
523 memcpy (context_offset, bytes, 2);
525 else if (is_fop_register (r))
527 gdb_byte bytes[4];
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
531 appropriate spot. */
532 memcpy (context_offset + 2, bytes, 2);
534 else
535 collect_register (regcache, r, context_offset);
538 static const unsigned char i386_win32_breakpoint = 0xcc;
539 #define i386_win32_breakpoint_len 1
541 static void
542 i386_arch_setup (void)
544 struct target_desc *tdesc;
546 #ifdef __x86_64__
547 tdesc = amd64_create_target_description (X86_XSTATE_SSE_MASK, false,
548 false, false);
549 init_target_desc (tdesc, amd64_expedite_regs, WINDOWS_OSABI);
550 win32_tdesc = tdesc;
551 #endif
553 tdesc = i386_create_target_description (X86_XSTATE_SSE_MASK, false, false);
554 init_target_desc (tdesc, i386_expedite_regs, WINDOWS_OSABI);
555 #ifdef __x86_64__
556 wow64_win32_tdesc = tdesc;
557 #else
558 win32_tdesc = tdesc;
559 #endif
562 /* Implement win32_target_ops "num_regs" method. */
564 static int
565 i386_win32_num_regs (void)
567 int num_regs;
568 #ifdef __x86_64__
569 if (!windows_process.wow64_process)
570 num_regs = sizeof (amd64_mappings) / sizeof (amd64_mappings[0]);
571 else
572 #endif
573 num_regs = sizeof (i386_mappings) / sizeof (i386_mappings[0]);
574 return num_regs;
577 /* Implement win32_target_ops "get_pc" method. */
579 static CORE_ADDR
580 i386_win32_get_pc (struct regcache *regcache)
582 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
584 if (use_64bit)
586 uint64_t pc;
588 collect_register_by_name (regcache, "rip", &pc);
589 return (CORE_ADDR) pc;
591 else
593 uint32_t pc;
595 collect_register_by_name (regcache, "eip", &pc);
596 return (CORE_ADDR) pc;
600 /* Implement win32_target_ops "set_pc" method. */
602 static void
603 i386_win32_set_pc (struct regcache *regcache, CORE_ADDR pc)
605 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
607 if (use_64bit)
609 uint64_t newpc = pc;
611 supply_register_by_name (regcache, "rip", &newpc);
613 else
615 uint32_t newpc = pc;
617 supply_register_by_name (regcache, "eip", &newpc);
621 struct win32_target_ops the_low_target = {
622 i386_arch_setup,
623 i386_win32_num_regs,
624 i386_initial_stuff,
625 i386_get_thread_context,
626 i386_prepare_to_resume,
627 i386_thread_added,
628 i386_fetch_inferior_register,
629 i386_store_inferior_register,
630 i386_single_step,
631 &i386_win32_breakpoint,
632 i386_win32_breakpoint_len,
634 i386_win32_get_pc,
635 i386_win32_set_pc,
636 i386_supports_z_point_type,
637 i386_insert_point,
638 i386_remove_point,
639 x86_stopped_by_watchpoint,
640 x86_stopped_data_address