1 /* Native-dependent code for GNU/Linux x86-64.
3 Copyright (C) 2001-2019 Free Software Foundation, Inc.
4 Contributed by Jiri Smid, SuSE Labs.
6 This file is part of GDB.
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.
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.
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/>. */
24 #include "elf/common.h"
26 #include "nat/gdb_ptrace.h"
27 #include <asm/prctl.h>
30 #include "gdb_proc_service.h"
32 #include "amd64-nat.h"
33 #include "amd64-tdep.h"
34 #include "amd64-linux-tdep.h"
35 #include "i386-linux-tdep.h"
36 #include "gdbsupport/x86-xstate.h"
38 #include "x86-linux-nat.h"
39 #include "nat/linux-ptrace.h"
40 #include "nat/amd64-linux-siginfo.h"
42 /* This definition comes from prctl.h. Kernels older than 2.5.64
44 #ifndef PTRACE_ARCH_PRCTL
45 #define PTRACE_ARCH_PRCTL 30
48 struct amd64_linux_nat_target final : public x86_linux_nat_target
50 /* Add our register access methods. */
51 void fetch_registers (struct regcache *, int) override;
52 void store_registers (struct regcache *, int) override;
54 bool low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction)
58 static amd64_linux_nat_target the_amd64_linux_nat_target;
60 /* Mapping between the general-purpose registers in GNU/Linux x86-64
61 `struct user' format and GDB's register cache layout for GNU/Linux
64 Note that most GNU/Linux x86-64 registers are 64-bit, while the
65 GNU/Linux i386 registers are all 32-bit, but since we're
66 little-endian we get away with that. */
68 /* From <sys/reg.h> on GNU/Linux i386. */
69 static int amd64_linux_gregset32_reg_offset[] =
71 RAX * 8, RCX * 8, /* %eax, %ecx */
72 RDX * 8, RBX * 8, /* %edx, %ebx */
73 RSP * 8, RBP * 8, /* %esp, %ebp */
74 RSI * 8, RDI * 8, /* %esi, %edi */
75 RIP * 8, EFLAGS * 8, /* %eip, %eflags */
76 CS * 8, SS * 8, /* %cs, %ss */
77 DS * 8, ES * 8, /* %ds, %es */
78 FS * 8, GS * 8, /* %fs, %gs */
79 -1, -1, -1, -1, -1, -1, -1, -1,
80 -1, -1, -1, -1, -1, -1, -1, -1,
81 -1, -1, -1, -1, -1, -1, -1, -1, -1,
82 -1, -1, -1, -1, -1, -1, -1, -1,
83 -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
84 -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
85 -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
86 -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512) */
87 -1, /* PKEYS register PKRU */
88 ORIG_RAX * 8 /* "orig_eax" */
92 /* Transfering the general-purpose registers between GDB, inferiors
95 /* See amd64_collect_native_gregset. This linux specific version handles
96 issues with negative EAX values not being restored correctly upon syscall
97 return when debugging 32-bit targets. It has no effect on 64-bit
101 amd64_linux_collect_native_gregset (const struct regcache *regcache,
102 void *gregs, int regnum)
104 amd64_collect_native_gregset (regcache, gregs, regnum);
106 struct gdbarch *gdbarch = regcache->arch ();
107 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
109 /* Sign extend EAX value to avoid potential syscall restart
112 On Linux, when a syscall is interrupted by a signal, the
113 (kernel function implementing the) syscall may return
114 -ERESTARTSYS when a signal occurs. Doing so indicates that
115 the syscall is restartable. Then, depending on settings
116 associated with the signal handler, and after the signal
117 handler is called, the kernel can then either return -EINTR
118 or it can cause the syscall to be restarted. We are
119 concerned with the latter case here.
121 On (32-bit) i386, the status (-ERESTARTSYS) is placed in the
122 EAX register. When debugging a 32-bit process from a 64-bit
123 (amd64) GDB, the debugger fetches 64-bit registers even
124 though the process being debugged is only 32-bit. The
125 register cache is only 32 bits wide though; GDB discards the
126 high 32 bits when placing 64-bit values in the 32-bit
127 regcache. Normally, this is not a problem since the 32-bit
128 process should only care about the lower 32-bit portions of
129 these registers. That said, it can happen that the 64-bit
130 value being restored will be different from the 64-bit value
131 that was originally retrieved from the kernel. The one place
132 (that we know of) where it does matter is in the kernel's
133 syscall restart code. The kernel's code for restarting a
134 syscall after a signal expects to see a negative value
135 (specifically -ERESTARTSYS) in the 64-bit RAX register in
136 order to correctly cause a syscall to be restarted.
138 The call to amd64_collect_native_gregset, above, is setting
139 the high 32 bits of RAX (and other registers too) to 0. For
140 syscall restart, we need to sign extend EAX so that RAX will
141 appear as a negative value when EAX is set to -ERESTARTSYS.
142 This in turn will cause the signal handling code in the
143 kernel to recognize -ERESTARTSYS which will in turn cause the
144 syscall to be restarted.
146 The test case gdb.base/interrupt.exp tests for this problem.
147 Without this sign extension code in place, it'll show
148 a number of failures when testing against unix/-m32. */
150 if (regnum == -1 || regnum == I386_EAX_REGNUM)
152 void *ptr = ((gdb_byte *) gregs
153 + amd64_linux_gregset32_reg_offset[I386_EAX_REGNUM]);
155 *(int64_t *) ptr = *(int32_t *) ptr;
160 /* Fill GDB's register cache with the general-purpose register values
164 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
166 amd64_supply_native_gregset (regcache, gregsetp, -1);
169 /* Fill register REGNUM (if it is a general-purpose register) in
170 *GREGSETP with the value in GDB's register cache. If REGNUM is -1,
171 do this for all registers. */
174 fill_gregset (const struct regcache *regcache,
175 elf_gregset_t *gregsetp, int regnum)
177 amd64_linux_collect_native_gregset (regcache, gregsetp, regnum);
180 /* Transfering floating-point registers between GDB, inferiors and cores. */
182 /* Fill GDB's register cache with the floating-point and SSE register
183 values in *FPREGSETP. */
186 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
188 amd64_supply_fxsave (regcache, -1, fpregsetp);
191 /* Fill register REGNUM (if it is a floating-point or SSE register) in
192 *FPREGSETP with the value in GDB's register cache. If REGNUM is
193 -1, do this for all registers. */
196 fill_fpregset (const struct regcache *regcache,
197 elf_fpregset_t *fpregsetp, int regnum)
199 amd64_collect_fxsave (regcache, regnum, fpregsetp);
203 /* Transferring arbitrary registers between GDB and inferior. */
205 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
206 this for all registers (including the floating point and SSE
210 amd64_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
212 struct gdbarch *gdbarch = regcache->arch ();
215 /* GNU/Linux LWP ID's are process ID's. */
216 tid = regcache->ptid ().lwp ();
218 tid = regcache->ptid ().pid (); /* Not a threaded program. */
220 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
224 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
225 perror_with_name (_("Couldn't get registers"));
227 amd64_supply_native_gregset (regcache, ®s, -1);
232 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
234 elf_fpregset_t fpregs;
236 if (have_ptrace_getregset == TRIBOOL_TRUE)
238 char xstateregs[X86_XSTATE_MAX_SIZE];
241 iov.iov_base = xstateregs;
242 iov.iov_len = sizeof (xstateregs);
243 if (ptrace (PTRACE_GETREGSET, tid,
244 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
245 perror_with_name (_("Couldn't get extended state status"));
247 amd64_supply_xsave (regcache, -1, xstateregs);
251 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
252 perror_with_name (_("Couldn't get floating point status"));
254 amd64_supply_fxsave (regcache, -1, &fpregs);
256 #ifndef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
258 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
259 fs_base and gs_base fields of user_regs_struct can be
263 if (regnum == -1 || regnum == AMD64_FSBASE_REGNUM)
265 if (ptrace (PTRACE_ARCH_PRCTL, tid, &base, ARCH_GET_FS) < 0)
266 perror_with_name (_("Couldn't get segment register fs_base"));
268 regcache->raw_supply (AMD64_FSBASE_REGNUM, &base);
271 if (regnum == -1 || regnum == AMD64_GSBASE_REGNUM)
273 if (ptrace (PTRACE_ARCH_PRCTL, tid, &base, ARCH_GET_GS) < 0)
274 perror_with_name (_("Couldn't get segment register gs_base"));
276 regcache->raw_supply (AMD64_GSBASE_REGNUM, &base);
283 /* Store register REGNUM back into the child process. If REGNUM is
284 -1, do this for all registers (including the floating-point and SSE
288 amd64_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
290 struct gdbarch *gdbarch = regcache->arch ();
293 /* GNU/Linux LWP ID's are process ID's. */
294 tid = regcache->ptid ().lwp ();
296 tid = regcache->ptid ().pid (); /* Not a threaded program. */
298 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
302 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
303 perror_with_name (_("Couldn't get registers"));
305 amd64_linux_collect_native_gregset (regcache, ®s, regnum);
307 if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0)
308 perror_with_name (_("Couldn't write registers"));
314 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
316 elf_fpregset_t fpregs;
318 if (have_ptrace_getregset == TRIBOOL_TRUE)
320 char xstateregs[X86_XSTATE_MAX_SIZE];
323 iov.iov_base = xstateregs;
324 iov.iov_len = sizeof (xstateregs);
325 if (ptrace (PTRACE_GETREGSET, tid,
326 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
327 perror_with_name (_("Couldn't get extended state status"));
329 amd64_collect_xsave (regcache, regnum, xstateregs, 0);
331 if (ptrace (PTRACE_SETREGSET, tid,
332 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
333 perror_with_name (_("Couldn't write extended state status"));
337 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
338 perror_with_name (_("Couldn't get floating point status"));
340 amd64_collect_fxsave (regcache, regnum, &fpregs);
342 if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
343 perror_with_name (_("Couldn't write floating point status"));
346 #ifndef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
348 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
349 fs_base and gs_base fields of user_regs_struct can be
353 if (regnum == -1 || regnum == AMD64_FSBASE_REGNUM)
355 regcache->raw_collect (AMD64_FSBASE_REGNUM, &base);
357 if (ptrace (PTRACE_ARCH_PRCTL, tid, base, ARCH_SET_FS) < 0)
358 perror_with_name (_("Couldn't write segment register fs_base"));
360 if (regnum == -1 || regnum == AMD64_GSBASE_REGNUM)
363 regcache->raw_collect (AMD64_GSBASE_REGNUM, &base);
364 if (ptrace (PTRACE_ARCH_PRCTL, tid, base, ARCH_SET_GS) < 0)
365 perror_with_name (_("Couldn't write segment register gs_base"));
373 /* This function is called by libthread_db as part of its handling of
374 a request for a thread's local storage address. */
377 ps_get_thread_area (struct ps_prochandle *ph,
378 lwpid_t lwpid, int idx, void **base)
380 if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32)
382 unsigned int base_addr;
385 result = x86_linux_get_thread_area (lwpid, (void *) (long) idx,
389 /* Extend the value to 64 bits. Here it's assumed that
390 a "long" and a "void *" are the same. */
391 (*base) = (void *) (long) base_addr;
398 /* FIXME: ezannoni-2003-07-09 see comment above about include
399 file order. We could be getting bogus values for these two. */
400 gdb_assert (FS < ELF_NGREG);
401 gdb_assert (GS < ELF_NGREG);
405 #ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
407 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
408 fs_base and gs_base fields of user_regs_struct can be
412 fs = ptrace (PTRACE_PEEKUSER, lwpid,
413 offsetof (struct user_regs_struct, fs_base), 0);
421 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
425 #ifdef HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE
429 gs = ptrace (PTRACE_PEEKUSER, lwpid,
430 offsetof (struct user_regs_struct, gs_base), 0);
438 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
441 default: /* Should not happen. */
445 return PS_ERR; /* ptrace failed. */
449 /* Convert a ptrace/host siginfo object, into/from the siginfo in the
450 layout of the inferiors' architecture. Returns true if any
451 conversion was done; false otherwise. If DIRECTION is 1, then copy
452 from INF to PTRACE. If DIRECTION is 0, copy from PTRACE to
456 amd64_linux_nat_target::low_siginfo_fixup (siginfo_t *ptrace,
460 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
462 /* Is the inferior 32-bit? If so, then do fixup the siginfo
464 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
465 return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
467 /* No fixup for native x32 GDB. */
468 else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
469 return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
476 _initialize_amd64_linux_nat (void)
478 amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
479 amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
480 amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
481 amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;
483 gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
484 == amd64_native_gregset32_num_regs);
486 linux_target = &the_amd64_linux_nat_target;
488 /* Add the target. */
489 add_inf_child_target (linux_target);