]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdbserver/win32-i386-low.cc
New Romanian translation for gas sub-directory
[thirdparty/binutils-gdb.git] / gdbserver / win32-i386-low.cc
1 /* Copyright (C) 2007-2024 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
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.
9
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.
14
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/>. */
17
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"
28
29 using namespace windows_nat;
30
31 #ifndef CONTEXT_EXTENDED_REGISTERS
32 #define CONTEXT_EXTENDED_REGISTERS 0
33 #endif
34
35 #define I386_FISEG_REGNUM 27
36 #define I386_FOP_REGNUM 31
37
38 #define I386_CS_REGNUM 10
39 #define I386_GS_REGNUM 15
40
41 #define AMD64_FISEG_REGNUM 35
42 #define AMD64_FOP_REGNUM 39
43
44 #define AMD64_CS_REGNUM 18
45 #define AMD64_GS_REGNUM 23
46
47 #define FLAG_TRACE_BIT 0x100
48
49 static struct x86_debug_reg_state debug_reg_state;
50
51 static void
52 update_debug_registers (thread_info *thread)
53 {
54 windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
55
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;
59 }
60
61 /* Update the inferior's debug register REGNUM from STATE. */
62
63 static void
64 x86_dr_low_set_addr (int regnum, CORE_ADDR addr)
65 {
66 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
67
68 /* Only update the threads of this process. */
69 for_each_thread (current_thread->id.pid (), update_debug_registers);
70 }
71
72 /* Update the inferior's DR7 debug control register from STATE. */
73
74 static void
75 x86_dr_low_set_control (unsigned long control)
76 {
77 /* Only update the threads of this process. */
78 for_each_thread (current_thread->id.pid (), update_debug_registers);
79 }
80
81 /* Return the current value of a DR register of the current thread's
82 context. */
83
84 static DWORD64
85 win32_get_current_dr (int dr)
86 {
87 windows_thread_info *th
88 = (windows_thread_info *) thread_target_data (current_thread);
89
90 win32_require_context (th);
91
92 #ifdef __x86_64__
93 #define RET_DR(DR) \
94 case DR: \
95 return th->wow64_context.Dr ## DR
96
97 if (windows_process.wow64_process)
98 {
99 switch (dr)
100 {
101 RET_DR (0);
102 RET_DR (1);
103 RET_DR (2);
104 RET_DR (3);
105 RET_DR (6);
106 RET_DR (7);
107 }
108 }
109 else
110 #undef RET_DR
111 #endif
112 #define RET_DR(DR) \
113 case DR: \
114 return th->context.Dr ## DR
115
116 {
117 switch (dr)
118 {
119 RET_DR (0);
120 RET_DR (1);
121 RET_DR (2);
122 RET_DR (3);
123 RET_DR (6);
124 RET_DR (7);
125 }
126 }
127
128 #undef RET_DR
129
130 gdb_assert_not_reached ("unhandled dr");
131 }
132
133 static CORE_ADDR
134 x86_dr_low_get_addr (int regnum)
135 {
136 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
137
138 return win32_get_current_dr (regnum - DR_FIRSTADDR);
139 }
140
141 static unsigned long
142 x86_dr_low_get_control (void)
143 {
144 return win32_get_current_dr (7);
145 }
146
147 /* Get the value of the DR6 debug status register from the inferior
148 and record it in STATE. */
149
150 static unsigned long
151 x86_dr_low_get_status (void)
152 {
153 return win32_get_current_dr (6);
154 }
155
156 /* Low-level function vector. */
157 struct x86_dr_low_type x86_dr_low =
158 {
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 *),
165 };
166
167 /* Breakpoint/watchpoint support. */
168
169 static int
170 i386_supports_z_point_type (char z_type)
171 {
172 switch (z_type)
173 {
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;
180 }
181 }
182
183 static int
184 i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
185 int size, struct raw_breakpoint *bp)
186 {
187 switch (type)
188 {
189 case raw_bkpt_type_hw:
190 case raw_bkpt_type_write_wp:
191 case raw_bkpt_type_access_wp:
192 {
193 enum target_hw_bp_type hw_type
194 = raw_bkpt_type_to_target_hw_bp_type (type);
195
196 return x86_dr_insert_watchpoint (&debug_reg_state,
197 hw_type, addr, size);
198 }
199 default:
200 /* Unsupported. */
201 return 1;
202 }
203 }
204
205 static int
206 i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
207 int size, struct raw_breakpoint *bp)
208 {
209 switch (type)
210 {
211 case raw_bkpt_type_hw:
212 case raw_bkpt_type_write_wp:
213 case raw_bkpt_type_access_wp:
214 {
215 enum target_hw_bp_type hw_type
216 = raw_bkpt_type_to_target_hw_bp_type (type);
217
218 return x86_dr_remove_watchpoint (&debug_reg_state,
219 hw_type, addr, size);
220 }
221 default:
222 /* Unsupported. */
223 return 1;
224 }
225 }
226
227 static int
228 x86_stopped_by_watchpoint (void)
229 {
230 return x86_dr_stopped_by_watchpoint (&debug_reg_state);
231 }
232
233 static CORE_ADDR
234 x86_stopped_data_address (void)
235 {
236 CORE_ADDR addr;
237 if (x86_dr_stopped_data_address (&debug_reg_state, &addr))
238 return addr;
239 return 0;
240 }
241
242 static void
243 i386_initial_stuff (void)
244 {
245 x86_low_init_dregs (&debug_reg_state);
246 }
247
248 static void
249 i386_get_thread_context (windows_thread_info *th)
250 {
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;
254
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);
268
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)
277 {
278 DWORD e = GetLastError ();
279
280 if (extended_registers && e == ERROR_INVALID_PARAMETER)
281 {
282 extended_registers = 0;
283 goto again;
284 }
285
286 error ("GetThreadContext failure %ld\n", (long) e);
287 }
288 }
289
290 static void
291 i386_prepare_to_resume (windows_thread_info *th)
292 {
293 if (th->debug_registers_changed)
294 {
295 struct x86_debug_reg_state *dr = &debug_reg_state;
296
297 win32_require_context (th);
298
299 #ifdef __x86_64__
300 if (windows_process.wow64_process)
301 {
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;
309 }
310 else
311 #endif
312 {
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;
320 }
321
322 th->debug_registers_changed = false;
323 }
324 }
325
326 static void
327 i386_thread_added (windows_thread_info *th)
328 {
329 th->debug_registers_changed = true;
330 }
331
332 static void
333 i386_single_step (windows_thread_info *th)
334 {
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;
341 }
342
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])
399 };
400 #undef context_offset
401
402 #ifdef __x86_64__
403
404 #define context_offset(x) (offsetof (CONTEXT, x))
405 static const int amd64_mappings[] =
406 {
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)
466 };
467 #undef context_offset
468
469 #endif /* __x86_64__ */
470
471 /* Return true if R is the FISEG register. */
472 static bool
473 is_fiseg_register (int r)
474 {
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;
481 }
482
483 /* Return true if R is the FOP register. */
484 static bool
485 is_fop_register (int r)
486 {
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;
493 }
494
495 /* Return true if R is a segment register. */
496 static bool
497 is_segment_register (int r)
498 {
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;
505 }
506
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)
511 {
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;
519
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];
527
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))
532 {
533 gdb_byte bytes[4] = {};
534 memcpy (bytes, context_offset, 2);
535 supply_register (regcache, r, bytes);
536 }
537 else if (is_fop_register (r))
538 {
539 long l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
540 supply_register (regcache, r, (char *) &l);
541 }
542 else
543 supply_register (regcache, r, context_offset);
544 }
545
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)
550 {
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;
558
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];
566
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))
571 {
572 gdb_byte bytes[4];
573 collect_register (regcache, r, bytes);
574 memcpy (context_offset, bytes, 2);
575 }
576 else if (is_fop_register (r))
577 {
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);
584 }
585 else
586 collect_register (regcache, r, context_offset);
587 }
588
589 static const unsigned char i386_win32_breakpoint = 0xcc;
590 #define i386_win32_breakpoint_len 1
591
592 static void
593 i386_arch_setup (void)
594 {
595 struct target_desc *tdesc;
596
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
603
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
611 }
612
613 /* Implement win32_target_ops "num_regs" method. */
614
615 static int
616 i386_win32_num_regs (void)
617 {
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;
626 }
627
628 /* Implement win32_target_ops "get_pc" method. */
629
630 static CORE_ADDR
631 i386_win32_get_pc (struct regcache *regcache)
632 {
633 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
634
635 if (use_64bit)
636 {
637 uint64_t pc;
638
639 collect_register_by_name (regcache, "rip", &pc);
640 return (CORE_ADDR) pc;
641 }
642 else
643 {
644 uint32_t pc;
645
646 collect_register_by_name (regcache, "eip", &pc);
647 return (CORE_ADDR) pc;
648 }
649 }
650
651 /* Implement win32_target_ops "set_pc" method. */
652
653 static void
654 i386_win32_set_pc (struct regcache *regcache, CORE_ADDR pc)
655 {
656 bool use_64bit = register_size (regcache->tdesc, 0) == 8;
657
658 if (use_64bit)
659 {
660 uint64_t newpc = pc;
661
662 supply_register_by_name (regcache, "rip", &newpc);
663 }
664 else
665 {
666 uint32_t newpc = pc;
667
668 supply_register_by_name (regcache, "eip", &newpc);
669 }
670 }
671
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,
684 1,
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
692 };