42af924ad941e281f46b6275dc93550426b2fa48
[deliverable/binutils-gdb.git] / gdb / amd64-linux-nat.c
1 /* Native-dependent code for GNU/Linux x86-64.
2
3 Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 Contributed by Jiri Smid, SuSE Labs.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "regset.h"
26 #include "linux-nat.h"
27 #include "amd64-linux-tdep.h"
28 #include "nat/linux-btrace.h"
29 #include "btrace.h"
30
31 #include "gdb_assert.h"
32 #include <string.h>
33 #include "elf/common.h"
34 #include <sys/uio.h>
35 #include <sys/ptrace.h>
36 #include <sys/debugreg.h>
37 #include <sys/syscall.h>
38 #include <sys/procfs.h>
39 #include <sys/user.h>
40 #include <asm/prctl.h>
41 /* FIXME ezannoni-2003-07-09: we need <sys/reg.h> to be included after
42 <asm/ptrace.h> because the latter redefines FS and GS for no apparent
43 reason, and those definitions don't match the ones that libpthread_db
44 uses, which come from <sys/reg.h>. */
45 /* ezannoni-2003-07-09: I think this is fixed. The extraneous defs have
46 been removed from ptrace.h in the kernel. However, better safe than
47 sorry. */
48 #include <asm/ptrace.h>
49 #include <sys/reg.h>
50 #include "gdb_proc_service.h"
51
52 /* Prototypes for supply_gregset etc. */
53 #include "gregset.h"
54
55 #include "amd64-tdep.h"
56 #include "i386-linux-tdep.h"
57 #include "amd64-nat.h"
58 #include "i386-nat.h"
59 #include "i386-xstate.h"
60
61 #ifndef PTRACE_GETREGSET
62 #define PTRACE_GETREGSET 0x4204
63 #endif
64
65 #ifndef PTRACE_SETREGSET
66 #define PTRACE_SETREGSET 0x4205
67 #endif
68
69 /* Per-thread arch-specific data we want to keep. */
70
71 struct arch_lwp_info
72 {
73 /* Non-zero if our copy differs from what's recorded in the thread. */
74 int debug_registers_changed;
75 };
76
77 /* Does the current host support PTRACE_GETREGSET? */
78 static int have_ptrace_getregset = -1;
79
80 /* Mapping between the general-purpose registers in GNU/Linux x86-64
81 `struct user' format and GDB's register cache layout for GNU/Linux
82 i386.
83
84 Note that most GNU/Linux x86-64 registers are 64-bit, while the
85 GNU/Linux i386 registers are all 32-bit, but since we're
86 little-endian we get away with that. */
87
88 /* From <sys/reg.h> on GNU/Linux i386. */
89 static int amd64_linux_gregset32_reg_offset[] =
90 {
91 RAX * 8, RCX * 8, /* %eax, %ecx */
92 RDX * 8, RBX * 8, /* %edx, %ebx */
93 RSP * 8, RBP * 8, /* %esp, %ebp */
94 RSI * 8, RDI * 8, /* %esi, %edi */
95 RIP * 8, EFLAGS * 8, /* %eip, %eflags */
96 CS * 8, SS * 8, /* %cs, %ss */
97 DS * 8, ES * 8, /* %ds, %es */
98 FS * 8, GS * 8, /* %fs, %gs */
99 -1, -1, -1, -1, -1, -1, -1, -1,
100 -1, -1, -1, -1, -1, -1, -1, -1,
101 -1, -1, -1, -1, -1, -1, -1, -1, -1,
102 -1, -1, -1, -1, -1, -1, -1, -1,
103 -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
104 -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
105 -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
106 -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512) */
107 ORIG_RAX * 8 /* "orig_eax" */
108 };
109 \f
110
111 /* Transfering the general-purpose registers between GDB, inferiors
112 and core files. */
113
114 /* Fill GDB's register cache with the general-purpose register values
115 in *GREGSETP. */
116
117 void
118 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
119 {
120 amd64_supply_native_gregset (regcache, gregsetp, -1);
121 }
122
123 /* Fill register REGNUM (if it is a general-purpose register) in
124 *GREGSETP with the value in GDB's register cache. If REGNUM is -1,
125 do this for all registers. */
126
127 void
128 fill_gregset (const struct regcache *regcache,
129 elf_gregset_t *gregsetp, int regnum)
130 {
131 amd64_collect_native_gregset (regcache, gregsetp, regnum);
132 }
133
134 /* Transfering floating-point registers between GDB, inferiors and cores. */
135
136 /* Fill GDB's register cache with the floating-point and SSE register
137 values in *FPREGSETP. */
138
139 void
140 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
141 {
142 amd64_supply_fxsave (regcache, -1, fpregsetp);
143 }
144
145 /* Fill register REGNUM (if it is a floating-point or SSE register) in
146 *FPREGSETP with the value in GDB's register cache. If REGNUM is
147 -1, do this for all registers. */
148
149 void
150 fill_fpregset (const struct regcache *regcache,
151 elf_fpregset_t *fpregsetp, int regnum)
152 {
153 amd64_collect_fxsave (regcache, regnum, fpregsetp);
154 }
155 \f
156
157 /* Transferring arbitrary registers between GDB and inferior. */
158
159 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
160 this for all registers (including the floating point and SSE
161 registers). */
162
163 static void
164 amd64_linux_fetch_inferior_registers (struct target_ops *ops,
165 struct regcache *regcache, int regnum)
166 {
167 struct gdbarch *gdbarch = get_regcache_arch (regcache);
168 int tid;
169
170 /* GNU/Linux LWP ID's are process ID's. */
171 tid = ptid_get_lwp (inferior_ptid);
172 if (tid == 0)
173 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
174
175 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
176 {
177 elf_gregset_t regs;
178
179 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
180 perror_with_name (_("Couldn't get registers"));
181
182 amd64_supply_native_gregset (regcache, &regs, -1);
183 if (regnum != -1)
184 return;
185 }
186
187 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
188 {
189 elf_fpregset_t fpregs;
190
191 if (have_ptrace_getregset)
192 {
193 char xstateregs[I386_XSTATE_MAX_SIZE];
194 struct iovec iov;
195
196 iov.iov_base = xstateregs;
197 iov.iov_len = sizeof (xstateregs);
198 if (ptrace (PTRACE_GETREGSET, tid,
199 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
200 perror_with_name (_("Couldn't get extended state status"));
201
202 amd64_supply_xsave (regcache, -1, xstateregs);
203 }
204 else
205 {
206 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
207 perror_with_name (_("Couldn't get floating point status"));
208
209 amd64_supply_fxsave (regcache, -1, &fpregs);
210 }
211 }
212 }
213
214 /* Store register REGNUM back into the child process. If REGNUM is
215 -1, do this for all registers (including the floating-point and SSE
216 registers). */
217
218 static void
219 amd64_linux_store_inferior_registers (struct target_ops *ops,
220 struct regcache *regcache, int regnum)
221 {
222 struct gdbarch *gdbarch = get_regcache_arch (regcache);
223 int tid;
224
225 /* GNU/Linux LWP ID's are process ID's. */
226 tid = ptid_get_lwp (inferior_ptid);
227 if (tid == 0)
228 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
229
230 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
231 {
232 elf_gregset_t regs;
233
234 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
235 perror_with_name (_("Couldn't get registers"));
236
237 amd64_collect_native_gregset (regcache, &regs, regnum);
238
239 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
240 perror_with_name (_("Couldn't write registers"));
241
242 if (regnum != -1)
243 return;
244 }
245
246 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
247 {
248 elf_fpregset_t fpregs;
249
250 if (have_ptrace_getregset)
251 {
252 char xstateregs[I386_XSTATE_MAX_SIZE];
253 struct iovec iov;
254
255 iov.iov_base = xstateregs;
256 iov.iov_len = sizeof (xstateregs);
257 if (ptrace (PTRACE_GETREGSET, tid,
258 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
259 perror_with_name (_("Couldn't get extended state status"));
260
261 amd64_collect_xsave (regcache, regnum, xstateregs, 0);
262
263 if (ptrace (PTRACE_SETREGSET, tid,
264 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
265 perror_with_name (_("Couldn't write extended state status"));
266 }
267 else
268 {
269 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
270 perror_with_name (_("Couldn't get floating point status"));
271
272 amd64_collect_fxsave (regcache, regnum, &fpregs);
273
274 if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
275 perror_with_name (_("Couldn't write floating point status"));
276 }
277 }
278 }
279 \f
280
281 /* Support for debug registers. */
282
283 /* Get debug register REGNUM value from only the one LWP of PTID. */
284
285 static unsigned long
286 x86_linux_dr_get (ptid_t ptid, int regnum)
287 {
288 int tid;
289 unsigned long value;
290
291 tid = ptid_get_lwp (ptid);
292 if (tid == 0)
293 tid = ptid_get_pid (ptid);
294
295 errno = 0;
296 value = ptrace (PTRACE_PEEKUSER, tid,
297 offsetof (struct user, u_debugreg[regnum]), 0);
298 if (errno != 0)
299 perror_with_name (_("Couldn't read debug register"));
300
301 return value;
302 }
303
304 /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
305
306 static void
307 x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
308 {
309 int tid;
310
311 tid = ptid_get_lwp (ptid);
312 if (tid == 0)
313 tid = ptid_get_pid (ptid);
314
315 errno = 0;
316 ptrace (PTRACE_POKEUSER, tid,
317 offsetof (struct user, u_debugreg[regnum]), value);
318 if (errno != 0)
319 perror_with_name (_("Couldn't write debug register"));
320 }
321
322 /* Return the inferior's debug register REGNUM. */
323
324 static CORE_ADDR
325 x86_linux_dr_get_addr (int regnum)
326 {
327 /* DR6 and DR7 are retrieved with some other way. */
328 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
329
330 return x86_linux_dr_get (inferior_ptid, regnum);
331 }
332
333 /* Return the inferior's DR7 debug control register. */
334
335 static unsigned long
336 x86_linux_dr_get_control (void)
337 {
338 return x86_linux_dr_get (inferior_ptid, DR_CONTROL);
339 }
340
341 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
342
343 static unsigned long
344 x86_linux_dr_get_status (void)
345 {
346 return x86_linux_dr_get (inferior_ptid, DR_STATUS);
347 }
348
349 /* Callback for iterate_over_lwps. Update the debug registers of
350 LWP. */
351
352 static int
353 update_debug_registers_callback (struct lwp_info *lwp, void *arg)
354 {
355 if (lwp->arch_private == NULL)
356 lwp->arch_private = XCNEW (struct arch_lwp_info);
357
358 /* The actual update is done later just before resuming the lwp, we
359 just mark that the registers need updating. */
360 lwp->arch_private->debug_registers_changed = 1;
361
362 /* If the lwp isn't stopped, force it to momentarily pause, so we
363 can update its debug registers. */
364 if (!lwp->stopped)
365 linux_stop_lwp (lwp);
366
367 /* Continue the iteration. */
368 return 0;
369 }
370
371 /* Set DR_CONTROL to CONTROL in all LWPs of the current inferior. */
372
373 static void
374 x86_linux_dr_set_control (unsigned long control)
375 {
376 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
377
378 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
379 }
380
381 /* Set address REGNUM (zero based) to ADDR in all LWPs of the current
382 inferior. */
383
384 static void
385 x86_linux_dr_set_addr (int regnum, CORE_ADDR addr)
386 {
387 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
388
389 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
390
391 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
392 }
393
394 /* Called when resuming a thread.
395 If the debug regs have changed, update the thread's copies. */
396
397 static void
398 x86_linux_prepare_to_resume (struct lwp_info *lwp)
399 {
400 int clear_status = 0;
401
402 /* NULL means this is the main thread still going through the shell,
403 or, no watchpoint has been set yet. In that case, there's
404 nothing to do. */
405 if (lwp->arch_private == NULL)
406 return;
407
408 if (lwp->arch_private->debug_registers_changed)
409 {
410 struct i386_debug_reg_state *state
411 = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
412 int i;
413
414 /* On Linux kernel before 2.6.33 commit
415 72f674d203cd230426437cdcf7dd6f681dad8b0d
416 if you enable a breakpoint by the DR_CONTROL bits you need to have
417 already written the corresponding DR_FIRSTADDR...DR_LASTADDR registers.
418
419 Ensure DR_CONTROL gets written as the very last register here. */
420
421 /* Clear DR_CONTROL first. In some cases, setting DR0-3 to a
422 value that doesn't match what is enabled in DR_CONTROL
423 results in EINVAL. */
424 x86_linux_dr_set (lwp->ptid, DR_CONTROL, 0);
425
426 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
427 if (state->dr_ref_count[i] > 0)
428 {
429 x86_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
430
431 /* If we're setting a watchpoint, any change the inferior
432 had done itself to the debug registers needs to be
433 discarded, otherwise, i386_stopped_data_address can get
434 confused. */
435 clear_status = 1;
436 }
437
438 /* If DR_CONTROL is supposed to be zero, we've already set it
439 above. */
440 if (state->dr_control_mirror != 0)
441 x86_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
442
443 lwp->arch_private->debug_registers_changed = 0;
444 }
445
446 if (clear_status || lwp->stopped_by_watchpoint)
447 x86_linux_dr_set (lwp->ptid, DR_STATUS, 0);
448 }
449
450 static void
451 x86_linux_new_thread (struct lwp_info *lp)
452 {
453 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
454
455 info->debug_registers_changed = 1;
456
457 lp->arch_private = info;
458 }
459
460 /* linux_nat_new_fork hook. */
461
462 static void
463 x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
464 {
465 pid_t parent_pid;
466 struct i386_debug_reg_state *parent_state;
467 struct i386_debug_reg_state *child_state;
468
469 /* NULL means no watchpoint has ever been set in the parent. In
470 that case, there's nothing to do. */
471 if (parent->arch_private == NULL)
472 return;
473
474 /* Linux kernel before 2.6.33 commit
475 72f674d203cd230426437cdcf7dd6f681dad8b0d
476 will inherit hardware debug registers from parent
477 on fork/vfork/clone. Newer Linux kernels create such tasks with
478 zeroed debug registers.
479
480 GDB core assumes the child inherits the watchpoints/hw
481 breakpoints of the parent, and will remove them all from the
482 forked off process. Copy the debug registers mirrors into the
483 new process so that all breakpoints and watchpoints can be
484 removed together. The debug registers mirror will become zeroed
485 in the end before detaching the forked off process, thus making
486 this compatible with older Linux kernels too. */
487
488 parent_pid = ptid_get_pid (parent->ptid);
489 parent_state = i386_debug_reg_state (parent_pid);
490 child_state = i386_debug_reg_state (child_pid);
491 *child_state = *parent_state;
492 }
493
494 \f
495
496 /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
497 the thread local storage (or its descriptor) and returns PS_OK
498 on success. Returns PS_ERR on failure. */
499
500 static ps_err_e
501 x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
502 {
503 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
504 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
505 4 byte integers in size: `entry_number', `base_addr', `limit',
506 and a bunch of status bits.
507
508 The values returned by this ptrace call should be part of the
509 regcache buffer, and ps_get_thread_area should channel its
510 request through the regcache. That way remote targets could
511 provide the value using the remote protocol and not this direct
512 call.
513
514 Is this function needed? I'm guessing that the `base' is the
515 address of a descriptor that libthread_db uses to find the
516 thread local address base that GDB needs. Perhaps that
517 descriptor is defined by the ABI. Anyway, given that
518 libthread_db calls this function without prompting (gdb
519 requesting tls base) I guess it needs info in there anyway. */
520 unsigned int desc[4];
521
522 /* This code assumes that "int" is 32 bits and that
523 GET_THREAD_AREA returns no more than 4 int values. */
524 gdb_assert (sizeof (int) == 4);
525
526 #ifndef PTRACE_GET_THREAD_AREA
527 #define PTRACE_GET_THREAD_AREA 25
528 #endif
529
530 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
531 return PS_ERR;
532
533 *base_addr = desc[1];
534 return PS_OK;
535 }
536
537 /* This function is called by libthread_db as part of its handling of
538 a request for a thread's local storage address. */
539
540 ps_err_e
541 ps_get_thread_area (const struct ps_prochandle *ph,
542 lwpid_t lwpid, int idx, void **base)
543 {
544 if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32)
545 {
546 unsigned int base_addr;
547 ps_err_e result;
548
549 result = x86_linux_get_thread_area (lwpid, (void *) (long) idx,
550 &base_addr);
551 if (result == PS_OK)
552 {
553 /* Extend the value to 64 bits. Here it's assumed that
554 a "long" and a "void *" are the same. */
555 (*base) = (void *) (long) base_addr;
556 }
557 return result;
558 }
559 else
560 {
561 /* This definition comes from prctl.h, but some kernels may not
562 have it. */
563 #ifndef PTRACE_ARCH_PRCTL
564 #define PTRACE_ARCH_PRCTL 30
565 #endif
566 /* FIXME: ezannoni-2003-07-09 see comment above about include
567 file order. We could be getting bogus values for these two. */
568 gdb_assert (FS < ELF_NGREG);
569 gdb_assert (GS < ELF_NGREG);
570 switch (idx)
571 {
572 case FS:
573 #ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
574 {
575 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
576 fs_base and gs_base fields of user_regs_struct can be
577 used directly. */
578 unsigned long fs;
579 errno = 0;
580 fs = ptrace (PTRACE_PEEKUSER, lwpid,
581 offsetof (struct user_regs_struct, fs_base), 0);
582 if (errno == 0)
583 {
584 *base = (void *) fs;
585 return PS_OK;
586 }
587 }
588 #endif
589 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
590 return PS_OK;
591 break;
592 case GS:
593 #ifdef HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE
594 {
595 unsigned long gs;
596 errno = 0;
597 gs = ptrace (PTRACE_PEEKUSER, lwpid,
598 offsetof (struct user_regs_struct, gs_base), 0);
599 if (errno == 0)
600 {
601 *base = (void *) gs;
602 return PS_OK;
603 }
604 }
605 #endif
606 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
607 return PS_OK;
608 break;
609 default: /* Should not happen. */
610 return PS_BADADDR;
611 }
612 }
613 return PS_ERR; /* ptrace failed. */
614 }
615 \f
616
617 static void (*super_post_startup_inferior) (struct target_ops *self,
618 ptid_t ptid);
619
620 static void
621 x86_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
622 {
623 i386_cleanup_dregs ();
624 super_post_startup_inferior (self, ptid);
625 }
626 \f
627
628 /* When GDB is built as a 64-bit application on linux, the
629 PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since
630 debugging a 32-bit inferior with a 64-bit GDB should look the same
631 as debugging it with a 32-bit GDB, we do the 32-bit <-> 64-bit
632 conversion in-place ourselves. */
633
634 /* These types below (compat_*) define a siginfo type that is layout
635 compatible with the siginfo type exported by the 32-bit userspace
636 support. */
637
638 typedef int compat_int_t;
639 typedef unsigned int compat_uptr_t;
640
641 typedef int compat_time_t;
642 typedef int compat_timer_t;
643 typedef int compat_clock_t;
644
645 struct compat_timeval
646 {
647 compat_time_t tv_sec;
648 int tv_usec;
649 };
650
651 typedef union compat_sigval
652 {
653 compat_int_t sival_int;
654 compat_uptr_t sival_ptr;
655 } compat_sigval_t;
656
657 typedef struct compat_siginfo
658 {
659 int si_signo;
660 int si_errno;
661 int si_code;
662
663 union
664 {
665 int _pad[((128 / sizeof (int)) - 3)];
666
667 /* kill() */
668 struct
669 {
670 unsigned int _pid;
671 unsigned int _uid;
672 } _kill;
673
674 /* POSIX.1b timers */
675 struct
676 {
677 compat_timer_t _tid;
678 int _overrun;
679 compat_sigval_t _sigval;
680 } _timer;
681
682 /* POSIX.1b signals */
683 struct
684 {
685 unsigned int _pid;
686 unsigned int _uid;
687 compat_sigval_t _sigval;
688 } _rt;
689
690 /* SIGCHLD */
691 struct
692 {
693 unsigned int _pid;
694 unsigned int _uid;
695 int _status;
696 compat_clock_t _utime;
697 compat_clock_t _stime;
698 } _sigchld;
699
700 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
701 struct
702 {
703 unsigned int _addr;
704 } _sigfault;
705
706 /* SIGPOLL */
707 struct
708 {
709 int _band;
710 int _fd;
711 } _sigpoll;
712 } _sifields;
713 } compat_siginfo_t;
714
715 /* For x32, clock_t in _sigchld is 64bit aligned at 4 bytes. */
716 typedef struct compat_x32_clock
717 {
718 int lower;
719 int upper;
720 } compat_x32_clock_t;
721
722 typedef struct compat_x32_siginfo
723 {
724 int si_signo;
725 int si_errno;
726 int si_code;
727
728 union
729 {
730 int _pad[((128 / sizeof (int)) - 3)];
731
732 /* kill() */
733 struct
734 {
735 unsigned int _pid;
736 unsigned int _uid;
737 } _kill;
738
739 /* POSIX.1b timers */
740 struct
741 {
742 compat_timer_t _tid;
743 int _overrun;
744 compat_sigval_t _sigval;
745 } _timer;
746
747 /* POSIX.1b signals */
748 struct
749 {
750 unsigned int _pid;
751 unsigned int _uid;
752 compat_sigval_t _sigval;
753 } _rt;
754
755 /* SIGCHLD */
756 struct
757 {
758 unsigned int _pid;
759 unsigned int _uid;
760 int _status;
761 compat_x32_clock_t _utime;
762 compat_x32_clock_t _stime;
763 } _sigchld;
764
765 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
766 struct
767 {
768 unsigned int _addr;
769 } _sigfault;
770
771 /* SIGPOLL */
772 struct
773 {
774 int _band;
775 int _fd;
776 } _sigpoll;
777 } _sifields;
778 } compat_x32_siginfo_t;
779
780 #define cpt_si_pid _sifields._kill._pid
781 #define cpt_si_uid _sifields._kill._uid
782 #define cpt_si_timerid _sifields._timer._tid
783 #define cpt_si_overrun _sifields._timer._overrun
784 #define cpt_si_status _sifields._sigchld._status
785 #define cpt_si_utime _sifields._sigchld._utime
786 #define cpt_si_stime _sifields._sigchld._stime
787 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
788 #define cpt_si_addr _sifields._sigfault._addr
789 #define cpt_si_band _sifields._sigpoll._band
790 #define cpt_si_fd _sifields._sigpoll._fd
791
792 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
793 In their place is si_timer1,si_timer2. */
794 #ifndef si_timerid
795 #define si_timerid si_timer1
796 #endif
797 #ifndef si_overrun
798 #define si_overrun si_timer2
799 #endif
800
801 static void
802 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
803 {
804 memset (to, 0, sizeof (*to));
805
806 to->si_signo = from->si_signo;
807 to->si_errno = from->si_errno;
808 to->si_code = from->si_code;
809
810 if (to->si_code == SI_TIMER)
811 {
812 to->cpt_si_timerid = from->si_timerid;
813 to->cpt_si_overrun = from->si_overrun;
814 to->cpt_si_ptr = (intptr_t) from->si_ptr;
815 }
816 else if (to->si_code == SI_USER)
817 {
818 to->cpt_si_pid = from->si_pid;
819 to->cpt_si_uid = from->si_uid;
820 }
821 else if (to->si_code < 0)
822 {
823 to->cpt_si_pid = from->si_pid;
824 to->cpt_si_uid = from->si_uid;
825 to->cpt_si_ptr = (intptr_t) from->si_ptr;
826 }
827 else
828 {
829 switch (to->si_signo)
830 {
831 case SIGCHLD:
832 to->cpt_si_pid = from->si_pid;
833 to->cpt_si_uid = from->si_uid;
834 to->cpt_si_status = from->si_status;
835 to->cpt_si_utime = from->si_utime;
836 to->cpt_si_stime = from->si_stime;
837 break;
838 case SIGILL:
839 case SIGFPE:
840 case SIGSEGV:
841 case SIGBUS:
842 to->cpt_si_addr = (intptr_t) from->si_addr;
843 break;
844 case SIGPOLL:
845 to->cpt_si_band = from->si_band;
846 to->cpt_si_fd = from->si_fd;
847 break;
848 default:
849 to->cpt_si_pid = from->si_pid;
850 to->cpt_si_uid = from->si_uid;
851 to->cpt_si_ptr = (intptr_t) from->si_ptr;
852 break;
853 }
854 }
855 }
856
857 static void
858 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
859 {
860 memset (to, 0, sizeof (*to));
861
862 to->si_signo = from->si_signo;
863 to->si_errno = from->si_errno;
864 to->si_code = from->si_code;
865
866 if (to->si_code == SI_TIMER)
867 {
868 to->si_timerid = from->cpt_si_timerid;
869 to->si_overrun = from->cpt_si_overrun;
870 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
871 }
872 else if (to->si_code == SI_USER)
873 {
874 to->si_pid = from->cpt_si_pid;
875 to->si_uid = from->cpt_si_uid;
876 }
877 if (to->si_code < 0)
878 {
879 to->si_pid = from->cpt_si_pid;
880 to->si_uid = from->cpt_si_uid;
881 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
882 }
883 else
884 {
885 switch (to->si_signo)
886 {
887 case SIGCHLD:
888 to->si_pid = from->cpt_si_pid;
889 to->si_uid = from->cpt_si_uid;
890 to->si_status = from->cpt_si_status;
891 to->si_utime = from->cpt_si_utime;
892 to->si_stime = from->cpt_si_stime;
893 break;
894 case SIGILL:
895 case SIGFPE:
896 case SIGSEGV:
897 case SIGBUS:
898 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
899 break;
900 case SIGPOLL:
901 to->si_band = from->cpt_si_band;
902 to->si_fd = from->cpt_si_fd;
903 break;
904 default:
905 to->si_pid = from->cpt_si_pid;
906 to->si_uid = from->cpt_si_uid;
907 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
908 break;
909 }
910 }
911 }
912
913 static void
914 compat_x32_siginfo_from_siginfo (compat_x32_siginfo_t *to,
915 siginfo_t *from)
916 {
917 memset (to, 0, sizeof (*to));
918
919 to->si_signo = from->si_signo;
920 to->si_errno = from->si_errno;
921 to->si_code = from->si_code;
922
923 if (to->si_code == SI_TIMER)
924 {
925 to->cpt_si_timerid = from->si_timerid;
926 to->cpt_si_overrun = from->si_overrun;
927 to->cpt_si_ptr = (intptr_t) from->si_ptr;
928 }
929 else if (to->si_code == SI_USER)
930 {
931 to->cpt_si_pid = from->si_pid;
932 to->cpt_si_uid = from->si_uid;
933 }
934 else if (to->si_code < 0)
935 {
936 to->cpt_si_pid = from->si_pid;
937 to->cpt_si_uid = from->si_uid;
938 to->cpt_si_ptr = (intptr_t) from->si_ptr;
939 }
940 else
941 {
942 switch (to->si_signo)
943 {
944 case SIGCHLD:
945 to->cpt_si_pid = from->si_pid;
946 to->cpt_si_uid = from->si_uid;
947 to->cpt_si_status = from->si_status;
948 memcpy (&to->cpt_si_utime, &from->si_utime,
949 sizeof (to->cpt_si_utime));
950 memcpy (&to->cpt_si_stime, &from->si_stime,
951 sizeof (to->cpt_si_stime));
952 break;
953 case SIGILL:
954 case SIGFPE:
955 case SIGSEGV:
956 case SIGBUS:
957 to->cpt_si_addr = (intptr_t) from->si_addr;
958 break;
959 case SIGPOLL:
960 to->cpt_si_band = from->si_band;
961 to->cpt_si_fd = from->si_fd;
962 break;
963 default:
964 to->cpt_si_pid = from->si_pid;
965 to->cpt_si_uid = from->si_uid;
966 to->cpt_si_ptr = (intptr_t) from->si_ptr;
967 break;
968 }
969 }
970 }
971
972 static void
973 siginfo_from_compat_x32_siginfo (siginfo_t *to,
974 compat_x32_siginfo_t *from)
975 {
976 memset (to, 0, sizeof (*to));
977
978 to->si_signo = from->si_signo;
979 to->si_errno = from->si_errno;
980 to->si_code = from->si_code;
981
982 if (to->si_code == SI_TIMER)
983 {
984 to->si_timerid = from->cpt_si_timerid;
985 to->si_overrun = from->cpt_si_overrun;
986 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
987 }
988 else if (to->si_code == SI_USER)
989 {
990 to->si_pid = from->cpt_si_pid;
991 to->si_uid = from->cpt_si_uid;
992 }
993 if (to->si_code < 0)
994 {
995 to->si_pid = from->cpt_si_pid;
996 to->si_uid = from->cpt_si_uid;
997 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
998 }
999 else
1000 {
1001 switch (to->si_signo)
1002 {
1003 case SIGCHLD:
1004 to->si_pid = from->cpt_si_pid;
1005 to->si_uid = from->cpt_si_uid;
1006 to->si_status = from->cpt_si_status;
1007 memcpy (&to->si_utime, &from->cpt_si_utime,
1008 sizeof (to->si_utime));
1009 memcpy (&to->si_stime, &from->cpt_si_stime,
1010 sizeof (to->si_stime));
1011 break;
1012 case SIGILL:
1013 case SIGFPE:
1014 case SIGSEGV:
1015 case SIGBUS:
1016 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
1017 break;
1018 case SIGPOLL:
1019 to->si_band = from->cpt_si_band;
1020 to->si_fd = from->cpt_si_fd;
1021 break;
1022 default:
1023 to->si_pid = from->cpt_si_pid;
1024 to->si_uid = from->cpt_si_uid;
1025 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
1026 break;
1027 }
1028 }
1029 }
1030
1031 /* Convert a native/host siginfo object, into/from the siginfo in the
1032 layout of the inferiors' architecture. Returns true if any
1033 conversion was done; false otherwise. If DIRECTION is 1, then copy
1034 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
1035 INF. */
1036
1037 static int
1038 amd64_linux_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction)
1039 {
1040 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
1041
1042 /* Is the inferior 32-bit? If so, then do fixup the siginfo
1043 object. */
1044 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
1045 {
1046 gdb_assert (sizeof (siginfo_t) == sizeof (compat_siginfo_t));
1047
1048 if (direction == 0)
1049 compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
1050 else
1051 siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
1052
1053 return 1;
1054 }
1055 /* No fixup for native x32 GDB. */
1056 else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
1057 {
1058 gdb_assert (sizeof (siginfo_t) == sizeof (compat_x32_siginfo_t));
1059
1060 if (direction == 0)
1061 compat_x32_siginfo_from_siginfo ((struct compat_x32_siginfo *) inf,
1062 native);
1063 else
1064 siginfo_from_compat_x32_siginfo (native,
1065 (struct compat_x32_siginfo *) inf);
1066
1067 return 1;
1068 }
1069 else
1070 return 0;
1071 }
1072
1073 #ifdef __x86_64__
1074 /* Value of CS segment register:
1075 64bit process: 0x33
1076 32bit process: 0x23 */
1077 #define AMD64_LINUX_USER64_CS 0x33
1078
1079 /* Value of DS segment register:
1080 LP64 process: 0x0
1081 X32 process: 0x2b */
1082 #define AMD64_LINUX_X32_DS 0x2b
1083 #endif
1084
1085 /* Get Linux/x86 target description from running target. */
1086
1087 static const struct target_desc *
1088 x86_linux_read_description (struct target_ops *ops)
1089 {
1090 int tid;
1091 int is_64bit = 0;
1092 #ifdef __x86_64__
1093 int is_x32;
1094 #endif
1095 static uint64_t xcr0;
1096 uint64_t xcr0_features_bits;
1097
1098 /* GNU/Linux LWP ID's are process ID's. */
1099 tid = ptid_get_lwp (inferior_ptid);
1100 if (tid == 0)
1101 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
1102
1103 #ifdef __x86_64__
1104 {
1105 unsigned long cs;
1106 unsigned long ds;
1107
1108 /* Get CS register. */
1109 errno = 0;
1110 cs = ptrace (PTRACE_PEEKUSER, tid,
1111 offsetof (struct user_regs_struct, cs), 0);
1112 if (errno != 0)
1113 perror_with_name (_("Couldn't get CS register"));
1114
1115 is_64bit = cs == AMD64_LINUX_USER64_CS;
1116
1117 /* Get DS register. */
1118 errno = 0;
1119 ds = ptrace (PTRACE_PEEKUSER, tid,
1120 offsetof (struct user_regs_struct, ds), 0);
1121 if (errno != 0)
1122 perror_with_name (_("Couldn't get DS register"));
1123
1124 is_x32 = ds == AMD64_LINUX_X32_DS;
1125
1126 if (sizeof (void *) == 4 && is_64bit && !is_x32)
1127 error (_("Can't debug 64-bit process with 32-bit GDB"));
1128 }
1129 #elif HAVE_PTRACE_GETFPXREGS
1130 if (have_ptrace_getfpxregs == -1)
1131 {
1132 elf_fpxregset_t fpxregs;
1133
1134 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
1135 {
1136 have_ptrace_getfpxregs = 0;
1137 have_ptrace_getregset = 0;
1138 return tdesc_i386_mmx_linux;
1139 }
1140 }
1141 #endif
1142
1143 if (have_ptrace_getregset == -1)
1144 {
1145 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
1146 struct iovec iov;
1147
1148 iov.iov_base = xstateregs;
1149 iov.iov_len = sizeof (xstateregs);
1150
1151 /* Check if PTRACE_GETREGSET works. */
1152 if (ptrace (PTRACE_GETREGSET, tid,
1153 (unsigned int) NT_X86_XSTATE, &iov) < 0)
1154 have_ptrace_getregset = 0;
1155 else
1156 {
1157 have_ptrace_getregset = 1;
1158
1159 /* Get XCR0 from XSAVE extended state. */
1160 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
1161 / sizeof (uint64_t))];
1162 }
1163 }
1164
1165 /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
1166 PTRACE_GETREGSET is not available then set xcr0_features_bits to
1167 zero so that the "no-features" descriptions are returned by the
1168 switches below. */
1169 if (have_ptrace_getregset)
1170 xcr0_features_bits = xcr0 & I386_XSTATE_ALL_MASK;
1171 else
1172 xcr0_features_bits = 0;
1173
1174 if (is_64bit)
1175 {
1176 #ifdef __x86_64__
1177 switch (xcr0_features_bits)
1178 {
1179 case I386_XSTATE_MPX_AVX512_MASK:
1180 case I386_XSTATE_AVX512_MASK:
1181 if (is_x32)
1182 return tdesc_x32_avx512_linux;
1183 else
1184 return tdesc_amd64_avx512_linux;
1185 case I386_XSTATE_MPX_MASK:
1186 if (is_x32)
1187 return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
1188 else
1189 return tdesc_amd64_mpx_linux;
1190 case I386_XSTATE_AVX_MASK:
1191 if (is_x32)
1192 return tdesc_x32_avx_linux;
1193 else
1194 return tdesc_amd64_avx_linux;
1195 default:
1196 if (is_x32)
1197 return tdesc_x32_linux;
1198 else
1199 return tdesc_amd64_linux;
1200 }
1201 #endif
1202 }
1203 else
1204 {
1205 switch (xcr0_features_bits)
1206 {
1207 case I386_XSTATE_MPX_AVX512_MASK:
1208 case I386_XSTATE_AVX512_MASK:
1209 return tdesc_i386_avx512_linux;
1210 case I386_XSTATE_MPX_MASK:
1211 return tdesc_i386_mpx_linux;
1212 case I386_XSTATE_AVX_MASK:
1213 return tdesc_i386_avx_linux;
1214 default:
1215 return tdesc_i386_linux;
1216 }
1217 }
1218
1219 gdb_assert_not_reached ("failed to return tdesc");
1220 }
1221
1222 /* Enable branch tracing. */
1223
1224 static struct btrace_target_info *
1225 x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid)
1226 {
1227 struct btrace_target_info *tinfo;
1228 struct gdbarch *gdbarch;
1229
1230 errno = 0;
1231 tinfo = linux_enable_btrace (ptid);
1232
1233 if (tinfo == NULL)
1234 error (_("Could not enable branch tracing for %s: %s."),
1235 target_pid_to_str (ptid), safe_strerror (errno));
1236
1237 /* Fill in the size of a pointer in bits. */
1238 gdbarch = target_thread_architecture (ptid);
1239 tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
1240
1241 return tinfo;
1242 }
1243
1244 /* Disable branch tracing. */
1245
1246 static void
1247 x86_linux_disable_btrace (struct target_ops *self,
1248 struct btrace_target_info *tinfo)
1249 {
1250 enum btrace_error errcode = linux_disable_btrace (tinfo);
1251
1252 if (errcode != BTRACE_ERR_NONE)
1253 error (_("Could not disable branch tracing."));
1254 }
1255
1256 /* Teardown branch tracing. */
1257
1258 static void
1259 x86_linux_teardown_btrace (struct target_ops *self,
1260 struct btrace_target_info *tinfo)
1261 {
1262 /* Ignore errors. */
1263 linux_disable_btrace (tinfo);
1264 }
1265
1266 static enum btrace_error
1267 x86_linux_read_btrace (struct target_ops *self,
1268 VEC (btrace_block_s) **data,
1269 struct btrace_target_info *btinfo,
1270 enum btrace_read_type type)
1271 {
1272 return linux_read_btrace (data, btinfo, type);
1273 }
1274
1275 /* Create an x86 GNU/Linux target. */
1276
1277 static struct target_ops *
1278 x86_linux_create_target (void)
1279 {
1280 /* Fill in the generic GNU/Linux methods. */
1281 struct target_ops *t = linux_target ();
1282
1283 /* Initialize the debug register function vectors. */
1284 i386_use_watchpoints (t);
1285 i386_dr_low.set_control = x86_linux_dr_set_control;
1286 i386_dr_low.set_addr = x86_linux_dr_set_addr;
1287 i386_dr_low.get_addr = x86_linux_dr_get_addr;
1288 i386_dr_low.get_status = x86_linux_dr_get_status;
1289 i386_dr_low.get_control = x86_linux_dr_get_control;
1290 i386_set_debug_register_length (sizeof (void *));
1291
1292 /* Override the GNU/Linux inferior startup hook. */
1293 super_post_startup_inferior = t->to_post_startup_inferior;
1294 t->to_post_startup_inferior = x86_linux_child_post_startup_inferior;
1295
1296 /* Add the description reader. */
1297 t->to_read_description = x86_linux_read_description;
1298
1299 /* Add btrace methods. */
1300 t->to_supports_btrace = linux_supports_btrace;
1301 t->to_enable_btrace = x86_linux_enable_btrace;
1302 t->to_disable_btrace = x86_linux_disable_btrace;
1303 t->to_teardown_btrace = x86_linux_teardown_btrace;
1304 t->to_read_btrace = x86_linux_read_btrace;
1305
1306 return t;
1307 }
1308
1309 /* Add an x86 GNU/Linux target. */
1310
1311 static void
1312 x86_linux_add_target (struct target_ops *t)
1313 {
1314 linux_nat_add_target (t);
1315 linux_nat_set_new_thread (t, x86_linux_new_thread);
1316 linux_nat_set_new_fork (t, x86_linux_new_fork);
1317 linux_nat_set_forget_process (t, i386_forget_process);
1318 linux_nat_set_prepare_to_resume (t, x86_linux_prepare_to_resume);
1319 }
1320
1321 /* Provide a prototype to silence -Wmissing-prototypes. */
1322 void _initialize_amd64_linux_nat (void);
1323
1324 void
1325 _initialize_amd64_linux_nat (void)
1326 {
1327 struct target_ops *t;
1328
1329 amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
1330 amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
1331 amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
1332 amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;
1333
1334 gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
1335 == amd64_native_gregset32_num_regs);
1336
1337 /* Create a generic x86 GNU/Linux target. */
1338 t = x86_linux_create_target ();
1339
1340 /* Add our register access methods. */
1341 t->to_fetch_registers = amd64_linux_fetch_inferior_registers;
1342 t->to_store_registers = amd64_linux_store_inferior_registers;
1343
1344 /* Add the target. */
1345 x86_linux_add_target (t);
1346
1347 /* Add our siginfo layout converter. */
1348 linux_nat_set_siginfo_fixup (t, amd64_linux_siginfo_fixup);
1349 }
This page took 0.058427 seconds and 4 git commands to generate.