1 /* Native-dependent code for GNU/Linux x86-64.
3 Copyright (C) 2001-2016 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 "linux-nat.h"
34 #include "amd64-tdep.h"
35 #include "amd64-linux-tdep.h"
36 #include "i386-linux-tdep.h"
37 #include "x86-xstate.h"
39 #include "x86-linux-nat.h"
40 #include "nat/linux-ptrace.h"
41 #include "nat/amd64-linux-siginfo.h"
43 /* Mapping between the general-purpose registers in GNU/Linux x86-64
44 `struct user' format and GDB's register cache layout for GNU/Linux
47 Note that most GNU/Linux x86-64 registers are 64-bit, while the
48 GNU/Linux i386 registers are all 32-bit, but since we're
49 little-endian we get away with that. */
51 /* From <sys/reg.h> on GNU/Linux i386. */
52 static int amd64_linux_gregset32_reg_offset[] =
54 RAX * 8, RCX * 8, /* %eax, %ecx */
55 RDX * 8, RBX * 8, /* %edx, %ebx */
56 RSP * 8, RBP * 8, /* %esp, %ebp */
57 RSI * 8, RDI * 8, /* %esi, %edi */
58 RIP * 8, EFLAGS * 8, /* %eip, %eflags */
59 CS * 8, SS * 8, /* %cs, %ss */
60 DS * 8, ES * 8, /* %ds, %es */
61 FS * 8, GS * 8, /* %fs, %gs */
62 -1, -1, -1, -1, -1, -1, -1, -1,
63 -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, -1, -1, -1, -1, -1, -1, -1,
66 -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
67 -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
68 -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
69 -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512) */
70 ORIG_RAX * 8 /* "orig_eax" */
74 /* Transfering the general-purpose registers between GDB, inferiors
77 /* Fill GDB's register cache with the general-purpose register values
81 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
83 amd64_supply_native_gregset (regcache, gregsetp, -1);
86 /* Fill register REGNUM (if it is a general-purpose register) in
87 *GREGSETP with the value in GDB's register cache. If REGNUM is -1,
88 do this for all registers. */
91 fill_gregset (const struct regcache *regcache,
92 elf_gregset_t *gregsetp, int regnum)
94 amd64_collect_native_gregset (regcache, gregsetp, regnum);
97 /* Transfering floating-point registers between GDB, inferiors and cores. */
99 /* Fill GDB's register cache with the floating-point and SSE register
100 values in *FPREGSETP. */
103 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
105 amd64_supply_fxsave (regcache, -1, fpregsetp);
108 /* Fill register REGNUM (if it is a floating-point or SSE register) in
109 *FPREGSETP with the value in GDB's register cache. If REGNUM is
110 -1, do this for all registers. */
113 fill_fpregset (const struct regcache *regcache,
114 elf_fpregset_t *fpregsetp, int regnum)
116 amd64_collect_fxsave (regcache, regnum, fpregsetp);
120 /* Transferring arbitrary registers between GDB and inferior. */
122 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
123 this for all registers (including the floating point and SSE
127 amd64_linux_fetch_inferior_registers (struct target_ops *ops,
128 struct regcache *regcache, int regnum)
130 struct gdbarch *gdbarch = get_regcache_arch (regcache);
133 /* GNU/Linux LWP ID's are process ID's. */
134 tid = ptid_get_lwp (inferior_ptid);
136 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
138 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
142 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
143 perror_with_name (_("Couldn't get registers"));
145 amd64_supply_native_gregset (regcache, ®s, -1);
150 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
152 elf_fpregset_t fpregs;
154 if (have_ptrace_getregset == TRIBOOL_TRUE)
156 char xstateregs[X86_XSTATE_MAX_SIZE];
159 iov.iov_base = xstateregs;
160 iov.iov_len = sizeof (xstateregs);
161 if (ptrace (PTRACE_GETREGSET, tid,
162 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
163 perror_with_name (_("Couldn't get extended state status"));
165 amd64_supply_xsave (regcache, -1, xstateregs);
169 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
170 perror_with_name (_("Couldn't get floating point status"));
172 amd64_supply_fxsave (regcache, -1, &fpregs);
177 /* Store register REGNUM back into the child process. If REGNUM is
178 -1, do this for all registers (including the floating-point and SSE
182 amd64_linux_store_inferior_registers (struct target_ops *ops,
183 struct regcache *regcache, int regnum)
185 struct gdbarch *gdbarch = get_regcache_arch (regcache);
188 /* GNU/Linux LWP ID's are process ID's. */
189 tid = ptid_get_lwp (inferior_ptid);
191 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
193 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
197 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
198 perror_with_name (_("Couldn't get registers"));
200 amd64_collect_native_gregset (regcache, ®s, regnum);
202 if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0)
203 perror_with_name (_("Couldn't write registers"));
209 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
211 elf_fpregset_t fpregs;
213 if (have_ptrace_getregset == TRIBOOL_TRUE)
215 char xstateregs[X86_XSTATE_MAX_SIZE];
218 iov.iov_base = xstateregs;
219 iov.iov_len = sizeof (xstateregs);
220 if (ptrace (PTRACE_GETREGSET, tid,
221 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
222 perror_with_name (_("Couldn't get extended state status"));
224 amd64_collect_xsave (regcache, regnum, xstateregs, 0);
226 if (ptrace (PTRACE_SETREGSET, tid,
227 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
228 perror_with_name (_("Couldn't write extended state status"));
232 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
233 perror_with_name (_("Couldn't get floating point status"));
235 amd64_collect_fxsave (regcache, regnum, &fpregs);
237 if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
238 perror_with_name (_("Couldn't write floating point status"));
244 /* This function is called by libthread_db as part of its handling of
245 a request for a thread's local storage address. */
248 ps_get_thread_area (const struct ps_prochandle *ph,
249 lwpid_t lwpid, int idx, void **base)
251 if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32)
253 unsigned int base_addr;
256 result = x86_linux_get_thread_area (lwpid, (void *) (long) idx,
260 /* Extend the value to 64 bits. Here it's assumed that
261 a "long" and a "void *" are the same. */
262 (*base) = (void *) (long) base_addr;
268 /* This definition comes from prctl.h, but some kernels may not
270 #ifndef PTRACE_ARCH_PRCTL
271 #define PTRACE_ARCH_PRCTL 30
273 /* FIXME: ezannoni-2003-07-09 see comment above about include
274 file order. We could be getting bogus values for these two. */
275 gdb_assert (FS < ELF_NGREG);
276 gdb_assert (GS < ELF_NGREG);
280 #ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
282 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
283 fs_base and gs_base fields of user_regs_struct can be
287 fs = ptrace (PTRACE_PEEKUSER, lwpid,
288 offsetof (struct user_regs_struct, fs_base), 0);
296 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
300 #ifdef HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE
304 gs = ptrace (PTRACE_PEEKUSER, lwpid,
305 offsetof (struct user_regs_struct, gs_base), 0);
313 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
316 default: /* Should not happen. */
320 return PS_ERR; /* ptrace failed. */
324 /* Convert a native/host siginfo object, into/from the siginfo in the
325 layout of the inferiors' architecture. Returns true if any
326 conversion was done; false otherwise. If DIRECTION is 1, then copy
327 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
331 amd64_linux_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction)
333 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
335 /* Is the inferior 32-bit? If so, then do fixup the siginfo
337 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
338 return amd64_linux_siginfo_fixup_common (native, inf, direction,
340 /* No fixup for native x32 GDB. */
341 else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
342 return amd64_linux_siginfo_fixup_common (native, inf, direction,
348 /* Provide a prototype to silence -Wmissing-prototypes. */
349 void _initialize_amd64_linux_nat (void);
352 _initialize_amd64_linux_nat (void)
354 struct target_ops *t;
356 amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
357 amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
358 amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
359 amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;
361 gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
362 == amd64_native_gregset32_num_regs);
364 /* Create a generic x86 GNU/Linux target. */
365 t = x86_linux_create_target ();
367 /* Add our register access methods. */
368 t->to_fetch_registers = amd64_linux_fetch_inferior_registers;
369 t->to_store_registers = amd64_linux_store_inferior_registers;
371 /* Add the target. */
372 x86_linux_add_target (t);
374 /* Add our siginfo layout converter. */
375 linux_nat_set_siginfo_fixup (t, amd64_linux_siginfo_fixup);