Add extra files found in etc/ sub-directory to ETC_SUPPORT in src-release.sh
[binutils-gdb.git] / gdb / amd64-linux-nat.c
blobaa9295d57230d4f559f6b5c346658b786c0b0688
1 /* Native-dependent code for GNU/Linux x86-64.
3 Copyright (C) 2001-2024 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/>. */
21 #include "inferior.h"
22 #include "regcache.h"
23 #include "elf/common.h"
24 #include <sys/uio.h>
25 #include "nat/gdb_ptrace.h"
26 #include <asm/prctl.h>
27 #include <sys/reg.h>
28 #include "gregset.h"
29 #include "gdb_proc_service.h"
31 #include "amd64-nat.h"
32 #include "amd64-tdep.h"
33 #include "amd64-linux-tdep.h"
34 #include "i386-linux-tdep.h"
35 #include "gdbsupport/x86-xstate.h"
37 #include "x86-linux-nat.h"
38 #include "nat/linux-ptrace.h"
39 #include "nat/amd64-linux-siginfo.h"
41 /* This definition comes from prctl.h. Kernels older than 2.5.64
42 do not have it. */
43 #ifndef PTRACE_ARCH_PRCTL
44 #define PTRACE_ARCH_PRCTL 30
45 #endif
47 struct amd64_linux_nat_target final : public x86_linux_nat_target
49 /* Add our register access methods. */
50 void fetch_registers (struct regcache *, int) override;
51 void store_registers (struct regcache *, int) override;
53 bool low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction)
54 override;
57 static amd64_linux_nat_target the_amd64_linux_nat_target;
59 /* Mapping between the general-purpose registers in GNU/Linux x86-64
60 `struct user' format and GDB's register cache layout for GNU/Linux
61 i386.
63 Note that most GNU/Linux x86-64 registers are 64-bit, while the
64 GNU/Linux i386 registers are all 32-bit, but since we're
65 little-endian we get away with that. */
67 /* From <sys/reg.h> on GNU/Linux i386. */
68 static int amd64_linux_gregset32_reg_offset[] =
70 RAX * 8, RCX * 8, /* %eax, %ecx */
71 RDX * 8, RBX * 8, /* %edx, %ebx */
72 RSP * 8, RBP * 8, /* %esp, %ebp */
73 RSI * 8, RDI * 8, /* %esi, %edi */
74 RIP * 8, EFLAGS * 8, /* %eip, %eflags */
75 CS * 8, SS * 8, /* %cs, %ss */
76 DS * 8, ES * 8, /* %ds, %es */
77 FS * 8, GS * 8, /* %fs, %gs */
78 -1, -1, -1, -1, -1, -1, -1, -1,
79 -1, -1, -1, -1, -1, -1, -1, -1,
80 -1, -1, -1, -1, -1, -1, -1, -1, -1,
81 -1, -1, -1, -1, -1, -1, -1, -1,
82 -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
83 -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
84 -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
85 -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512) */
86 -1, /* PKEYS register PKRU */
87 ORIG_RAX * 8 /* "orig_eax" */
91 /* Transfering the general-purpose registers between GDB, inferiors
92 and core files. */
94 /* See amd64_collect_native_gregset. This linux specific version handles
95 issues with negative EAX values not being restored correctly upon syscall
96 return when debugging 32-bit targets. It has no effect on 64-bit
97 targets. */
99 static void
100 amd64_linux_collect_native_gregset (const struct regcache *regcache,
101 void *gregs, int regnum)
103 amd64_collect_native_gregset (regcache, gregs, regnum);
105 struct gdbarch *gdbarch = regcache->arch ();
106 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
108 /* Sign extend EAX value to avoid potential syscall restart
109 problems.
111 On Linux, when a syscall is interrupted by a signal, the
112 (kernel function implementing the) syscall may return
113 -ERESTARTSYS when a signal occurs. Doing so indicates that
114 the syscall is restartable. Then, depending on settings
115 associated with the signal handler, and after the signal
116 handler is called, the kernel can then either return -EINTR
117 or it can cause the syscall to be restarted. We are
118 concerned with the latter case here.
120 On (32-bit) i386, the status (-ERESTARTSYS) is placed in the
121 EAX register. When debugging a 32-bit process from a 64-bit
122 (amd64) GDB, the debugger fetches 64-bit registers even
123 though the process being debugged is only 32-bit. The
124 register cache is only 32 bits wide though; GDB discards the
125 high 32 bits when placing 64-bit values in the 32-bit
126 regcache. Normally, this is not a problem since the 32-bit
127 process should only care about the lower 32-bit portions of
128 these registers. That said, it can happen that the 64-bit
129 value being restored will be different from the 64-bit value
130 that was originally retrieved from the kernel. The one place
131 (that we know of) where it does matter is in the kernel's
132 syscall restart code. The kernel's code for restarting a
133 syscall after a signal expects to see a negative value
134 (specifically -ERESTARTSYS) in the 64-bit RAX register in
135 order to correctly cause a syscall to be restarted.
137 The call to amd64_collect_native_gregset, above, is setting
138 the high 32 bits of RAX (and other registers too) to 0. For
139 syscall restart, we need to sign extend EAX so that RAX will
140 appear as a negative value when EAX is set to -ERESTARTSYS.
141 This in turn will cause the signal handling code in the
142 kernel to recognize -ERESTARTSYS which will in turn cause the
143 syscall to be restarted.
145 The test case gdb.base/interrupt.exp tests for this problem.
146 Without this sign extension code in place, it'll show
147 a number of failures when testing against unix/-m32. */
149 if (regnum == -1 || regnum == I386_EAX_REGNUM)
151 void *ptr = ((gdb_byte *) gregs
152 + amd64_linux_gregset32_reg_offset[I386_EAX_REGNUM]);
154 *(int64_t *) ptr = *(int32_t *) ptr;
159 /* Fill GDB's register cache with the general-purpose register values
160 in *GREGSETP. */
162 void
163 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
165 amd64_supply_native_gregset (regcache, gregsetp, -1);
168 /* Fill register REGNUM (if it is a general-purpose register) in
169 *GREGSETP with the value in GDB's register cache. If REGNUM is -1,
170 do this for all registers. */
172 void
173 fill_gregset (const struct regcache *regcache,
174 elf_gregset_t *gregsetp, int regnum)
176 amd64_linux_collect_native_gregset (regcache, gregsetp, regnum);
179 /* Transfering floating-point registers between GDB, inferiors and cores. */
181 /* Fill GDB's register cache with the floating-point and SSE register
182 values in *FPREGSETP. */
184 void
185 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
187 amd64_supply_fxsave (regcache, -1, fpregsetp);
190 /* Fill register REGNUM (if it is a floating-point or SSE register) in
191 *FPREGSETP with the value in GDB's register cache. If REGNUM is
192 -1, do this for all registers. */
194 void
195 fill_fpregset (const struct regcache *regcache,
196 elf_fpregset_t *fpregsetp, int regnum)
198 amd64_collect_fxsave (regcache, regnum, fpregsetp);
202 /* Transferring arbitrary registers between GDB and inferior. */
204 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
205 this for all registers (including the floating point and SSE
206 registers). */
208 void
209 amd64_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
211 struct gdbarch *gdbarch = regcache->arch ();
212 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
213 int tid;
215 /* GNU/Linux LWP ID's are process ID's. */
216 tid = regcache->ptid ().lwp ();
217 if (tid == 0)
218 tid = regcache->ptid ().pid (); /* Not a threaded program. */
220 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
222 elf_gregset_t regs;
224 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
225 perror_with_name (_("Couldn't get registers"));
227 amd64_supply_native_gregset (regcache, &regs, -1);
228 if (regnum != -1)
229 return;
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[tdep->xsave_layout.sizeof_xsave];
239 struct iovec iov;
241 /* Pre-4.14 kernels have a bug (fixed by commit 0852b374173b
242 "x86/fpu: Add FPU state copying quirk to handle XRSTOR failure on
243 Intel Skylake CPUs") that sometimes causes the mxcsr location in
244 xstateregs not to be copied by PTRACE_GETREGSET. Make sure that
245 the location is at least initialized with a defined value. */
246 memset (xstateregs, 0, sizeof (xstateregs));
247 iov.iov_base = xstateregs;
248 iov.iov_len = sizeof (xstateregs);
249 if (ptrace (PTRACE_GETREGSET, tid,
250 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
251 perror_with_name (_("Couldn't get extended state status"));
253 amd64_supply_xsave (regcache, -1, xstateregs);
255 else
257 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
258 perror_with_name (_("Couldn't get floating point status"));
260 amd64_supply_fxsave (regcache, -1, &fpregs);
265 /* Store register REGNUM back into the child process. If REGNUM is
266 -1, do this for all registers (including the floating-point and SSE
267 registers). */
269 void
270 amd64_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
272 struct gdbarch *gdbarch = regcache->arch ();
273 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
274 int tid;
276 /* GNU/Linux LWP ID's are process ID's. */
277 tid = regcache->ptid ().lwp ();
278 if (tid == 0)
279 tid = regcache->ptid ().pid (); /* Not a threaded program. */
281 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
283 elf_gregset_t regs;
285 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
286 perror_with_name (_("Couldn't get registers"));
288 amd64_linux_collect_native_gregset (regcache, &regs, regnum);
290 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
291 perror_with_name (_("Couldn't write registers"));
293 if (regnum != -1)
294 return;
297 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
299 elf_fpregset_t fpregs;
301 if (have_ptrace_getregset == TRIBOOL_TRUE)
303 char xstateregs[tdep->xsave_layout.sizeof_xsave];
304 struct iovec iov;
306 iov.iov_base = xstateregs;
307 iov.iov_len = sizeof (xstateregs);
308 if (ptrace (PTRACE_GETREGSET, tid,
309 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
310 perror_with_name (_("Couldn't get extended state status"));
312 amd64_collect_xsave (regcache, regnum, xstateregs, 0);
314 if (ptrace (PTRACE_SETREGSET, tid,
315 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
316 perror_with_name (_("Couldn't write extended state status"));
318 else
320 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
321 perror_with_name (_("Couldn't get floating point status"));
323 amd64_collect_fxsave (regcache, regnum, &fpregs);
325 if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
326 perror_with_name (_("Couldn't write floating point status"));
332 /* This function is called by libthread_db as part of its handling of
333 a request for a thread's local storage address. */
335 ps_err_e
336 ps_get_thread_area (struct ps_prochandle *ph,
337 lwpid_t lwpid, int idx, void **base)
339 if (gdbarch_bfd_arch_info (ph->thread->inf->arch ())->bits_per_word == 32)
341 unsigned int base_addr;
342 ps_err_e result;
344 result = x86_linux_get_thread_area (lwpid, (void *) (long) idx,
345 &base_addr);
346 if (result == PS_OK)
348 /* Extend the value to 64 bits. Here it's assumed that
349 a "long" and a "void *" are the same. */
350 (*base) = (void *) (long) base_addr;
352 return result;
354 else
357 /* FIXME: ezannoni-2003-07-09 see comment above about include
358 file order. We could be getting bogus values for these two. */
359 gdb_assert (FS < ELF_NGREG);
360 gdb_assert (GS < ELF_NGREG);
361 switch (idx)
363 case FS:
365 unsigned long fs;
366 errno = 0;
367 fs = ptrace (PTRACE_PEEKUSER, lwpid,
368 offsetof (struct user_regs_struct, fs_base), 0);
369 if (errno == 0)
371 *base = (void *) fs;
372 return PS_OK;
376 break;
378 case GS:
380 unsigned long gs;
381 errno = 0;
382 gs = ptrace (PTRACE_PEEKUSER, lwpid,
383 offsetof (struct user_regs_struct, gs_base), 0);
384 if (errno == 0)
386 *base = (void *) gs;
387 return PS_OK;
390 break;
392 default: /* Should not happen. */
393 return PS_BADADDR;
396 return PS_ERR; /* ptrace failed. */
400 /* Convert a ptrace/host siginfo object, into/from the siginfo in the
401 layout of the inferiors' architecture. Returns true if any
402 conversion was done; false otherwise. If DIRECTION is 1, then copy
403 from INF to PTRACE. If DIRECTION is 0, copy from PTRACE to
404 INF. */
406 bool
407 amd64_linux_nat_target::low_siginfo_fixup (siginfo_t *ptrace,
408 gdb_byte *inf,
409 int direction)
411 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
413 /* Is the inferior 32-bit? If so, then do fixup the siginfo
414 object. */
415 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
416 return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
417 FIXUP_32);
418 /* No fixup for native x32 GDB. */
419 else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
420 return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
421 FIXUP_X32);
422 else
423 return false;
426 void _initialize_amd64_linux_nat ();
427 void
428 _initialize_amd64_linux_nat ()
430 amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
431 amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
432 amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
433 amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;
435 gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
436 == amd64_native_gregset32_num_regs);
438 linux_target = &the_amd64_linux_nat_target;
440 /* Add the target. */
441 add_inf_child_target (linux_target);