Fix -Wmisleading-indentation warning in top.c
[deliverable/binutils-gdb.git] / gdb / amd64-linux-nat.c
CommitLineData
a4b6fc86 1/* Native-dependent code for GNU/Linux x86-64.
0a65a603 2
42a4f53d 3 Copyright (C) 2001-2019 Free Software Foundation, Inc.
53e95fcf
JS
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
53e95fcf
JS
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
53e95fcf
JS
20
21#include "defs.h"
4de283e4
TT
22#include "inferior.h"
23#include "regcache.h"
24#include "elf/common.h"
25#include <sys/uio.h>
26#include "nat/gdb_ptrace.h"
c43af07c 27#include <asm/prctl.h>
33a0a2ac 28#include <sys/reg.h>
4de283e4
TT
29#include "gregset.h"
30#include "gdb_proc_service.h"
c4f35dd8 31
3116063b 32#include "amd64-nat.h"
9c1488cb 33#include "amd64-tdep.h"
4de283e4 34#include "amd64-linux-tdep.h"
d55e5aa6 35#include "i386-linux-tdep.h"
268a13a5 36#include "gdbsupport/x86-xstate.h"
4de283e4 37
d55e5aa6 38#include "x86-linux-nat.h"
4de283e4
TT
39#include "nat/linux-ptrace.h"
40#include "nat/amd64-linux-siginfo.h"
60fac5b8 41
2735833d
WT
42/* This definition comes from prctl.h. Kernels older than 2.5.64
43 do not have it. */
44#ifndef PTRACE_ARCH_PRCTL
45#define PTRACE_ARCH_PRCTL 30
46#endif
47
f6ac5f3d
PA
48struct amd64_linux_nat_target final : public x86_linux_nat_target
49{
50 /* Add our register access methods. */
51 void fetch_registers (struct regcache *, int) override;
52 void store_registers (struct regcache *, int) override;
135340af
PA
53
54 bool low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction)
55 override;
f6ac5f3d
PA
56};
57
58static amd64_linux_nat_target the_amd64_linux_nat_target;
59
60fac5b8
MK
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
62 i386.
63
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. */
67
68/* From <sys/reg.h> on GNU/Linux i386. */
430eaf2e 69static int amd64_linux_gregset32_reg_offset[] =
60fac5b8 70{
f5859b4d
MK
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 */
60fac5b8
MK
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,
a055a187 82 -1, -1, -1, -1, -1, -1, -1, -1,
01f9f808
MS
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) */
51547df6 87 -1, /* PKEYS register PKRU */
01f9f808 88 ORIG_RAX * 8 /* "orig_eax" */
60fac5b8 89};
53e95fcf
JS
90\f
91
92/* Transfering the general-purpose registers between GDB, inferiors
93 and core files. */
94
3f52fdbc
KB
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
98 targets. */
99
100static void
101amd64_linux_collect_native_gregset (const struct regcache *regcache,
102 void *gregs, int regnum)
103{
104 amd64_collect_native_gregset (regcache, gregs, regnum);
105
106 struct gdbarch *gdbarch = regcache->arch ();
107 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
108 {
109 /* Sign extend EAX value to avoid potential syscall restart
110 problems.
111
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.
120
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.
137
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.
145
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. */
149
150 if (regnum == -1 || regnum == I386_EAX_REGNUM)
151 {
152 void *ptr = ((gdb_byte *) gregs
153 + amd64_linux_gregset32_reg_offset[I386_EAX_REGNUM]);
154
155 *(int64_t *) ptr = *(int32_t *) ptr;
156 }
157 }
158}
159
60fac5b8 160/* Fill GDB's register cache with the general-purpose register values
53e95fcf
JS
161 in *GREGSETP. */
162
163void
7f7fe91e 164supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
53e95fcf 165{
7f7fe91e 166 amd64_supply_native_gregset (regcache, gregsetp, -1);
53e95fcf
JS
167}
168
60fac5b8
MK
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,
53e95fcf
JS
171 do this for all registers. */
172
173void
7f7fe91e
UW
174fill_gregset (const struct regcache *regcache,
175 elf_gregset_t *gregsetp, int regnum)
53e95fcf 176{
3f52fdbc 177 amd64_linux_collect_native_gregset (regcache, gregsetp, regnum);
53e95fcf
JS
178}
179
53e95fcf
JS
180/* Transfering floating-point registers between GDB, inferiors and cores. */
181
60fac5b8 182/* Fill GDB's register cache with the floating-point and SSE register
c4f35dd8 183 values in *FPREGSETP. */
53e95fcf
JS
184
185void
7f7fe91e 186supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
53e95fcf 187{
7f7fe91e 188 amd64_supply_fxsave (regcache, -1, fpregsetp);
53e95fcf
JS
189}
190
8dda9770 191/* Fill register REGNUM (if it is a floating-point or SSE register) in
60fac5b8 192 *FPREGSETP with the value in GDB's register cache. If REGNUM is
c4f35dd8 193 -1, do this for all registers. */
53e95fcf
JS
194
195void
7f7fe91e
UW
196fill_fpregset (const struct regcache *regcache,
197 elf_fpregset_t *fpregsetp, int regnum)
53e95fcf 198{
7f7fe91e 199 amd64_collect_fxsave (regcache, regnum, fpregsetp);
53e95fcf 200}
53e95fcf
JS
201\f
202
203/* Transferring arbitrary registers between GDB and inferior. */
204
60fac5b8 205/* Fetch register REGNUM from the child process. If REGNUM is -1, do
53e95fcf
JS
206 this for all registers (including the floating point and SSE
207 registers). */
208
f6ac5f3d
PA
209void
210amd64_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
53e95fcf 211{
ac7936df 212 struct gdbarch *gdbarch = regcache->arch ();
53e95fcf
JS
213 int tid;
214
a4b6fc86 215 /* GNU/Linux LWP ID's are process ID's. */
e38504b3 216 tid = regcache->ptid ().lwp ();
c4f35dd8 217 if (tid == 0)
e99b03dc 218 tid = regcache->ptid ().pid (); /* Not a threaded program. */
53e95fcf 219
f8028488 220 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
53e95fcf 221 {
99679982
MK
222 elf_gregset_t regs;
223
224 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
edefbb7c 225 perror_with_name (_("Couldn't get registers"));
99679982 226
56be3814 227 amd64_supply_native_gregset (regcache, &regs, -1);
60fac5b8
MK
228 if (regnum != -1)
229 return;
53e95fcf
JS
230 }
231
f8028488 232 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
53e95fcf 233 {
99679982 234 elf_fpregset_t fpregs;
53e95fcf 235
0bdb2f78 236 if (have_ptrace_getregset == TRIBOOL_TRUE)
a055a187 237 {
df7e5265 238 char xstateregs[X86_XSTATE_MAX_SIZE];
a055a187
L
239 struct iovec iov;
240
3d435220
TV
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));
a055a187
L
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"));
99679982 252
a055a187
L
253 amd64_supply_xsave (regcache, -1, xstateregs);
254 }
255 else
256 {
257 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
258 perror_with_name (_("Couldn't get floating point status"));
259
260 amd64_supply_fxsave (regcache, -1, &fpregs);
261 }
2735833d
WT
262#ifndef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
263 {
264 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
265 fs_base and gs_base fields of user_regs_struct can be
266 used directly. */
267 unsigned long base;
268
269 if (regnum == -1 || regnum == AMD64_FSBASE_REGNUM)
270 {
271 if (ptrace (PTRACE_ARCH_PRCTL, tid, &base, ARCH_GET_FS) < 0)
272 perror_with_name (_("Couldn't get segment register fs_base"));
273
73e1c03f 274 regcache->raw_supply (AMD64_FSBASE_REGNUM, &base);
2735833d
WT
275 }
276
277 if (regnum == -1 || regnum == AMD64_GSBASE_REGNUM)
278 {
279 if (ptrace (PTRACE_ARCH_PRCTL, tid, &base, ARCH_GET_GS) < 0)
280 perror_with_name (_("Couldn't get segment register gs_base"));
281
73e1c03f 282 regcache->raw_supply (AMD64_GSBASE_REGNUM, &base);
2735833d
WT
283 }
284 }
285#endif
99679982 286 }
53e95fcf
JS
287}
288
60fac5b8
MK
289/* Store register REGNUM back into the child process. If REGNUM is
290 -1, do this for all registers (including the floating-point and SSE
53e95fcf 291 registers). */
c4f35dd8 292
f6ac5f3d
PA
293void
294amd64_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
53e95fcf 295{
ac7936df 296 struct gdbarch *gdbarch = regcache->arch ();
53e95fcf
JS
297 int tid;
298
a4b6fc86 299 /* GNU/Linux LWP ID's are process ID's. */
e38504b3 300 tid = regcache->ptid ().lwp ();
c4f35dd8 301 if (tid == 0)
e99b03dc 302 tid = regcache->ptid ().pid (); /* Not a threaded program. */
53e95fcf 303
f8028488 304 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
53e95fcf 305 {
99679982
MK
306 elf_gregset_t regs;
307
308 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
edefbb7c 309 perror_with_name (_("Couldn't get registers"));
99679982 310
3f52fdbc 311 amd64_linux_collect_native_gregset (regcache, &regs, regnum);
99679982
MK
312
313 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
edefbb7c 314 perror_with_name (_("Couldn't write registers"));
99679982 315
60fac5b8
MK
316 if (regnum != -1)
317 return;
53e95fcf
JS
318 }
319
f8028488 320 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
53e95fcf 321 {
99679982
MK
322 elf_fpregset_t fpregs;
323
0bdb2f78 324 if (have_ptrace_getregset == TRIBOOL_TRUE)
a055a187 325 {
df7e5265 326 char xstateregs[X86_XSTATE_MAX_SIZE];
a055a187
L
327 struct iovec iov;
328
329 iov.iov_base = xstateregs;
330 iov.iov_len = sizeof (xstateregs);
331 if (ptrace (PTRACE_GETREGSET, tid,
332 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
333 perror_with_name (_("Couldn't get extended state status"));
99679982 334
a055a187
L
335 amd64_collect_xsave (regcache, regnum, xstateregs, 0);
336
337 if (ptrace (PTRACE_SETREGSET, tid,
338 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
339 perror_with_name (_("Couldn't write extended state status"));
340 }
341 else
342 {
343 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
344 perror_with_name (_("Couldn't get floating point status"));
99679982 345
a055a187 346 amd64_collect_fxsave (regcache, regnum, &fpregs);
99679982 347
a055a187
L
348 if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
349 perror_with_name (_("Couldn't write floating point status"));
350 }
2735833d
WT
351
352#ifndef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
353 {
354 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
355 fs_base and gs_base fields of user_regs_struct can be
356 used directly. */
357 void *base;
358
359 if (regnum == -1 || regnum == AMD64_FSBASE_REGNUM)
360 {
34a79281 361 regcache->raw_collect (AMD64_FSBASE_REGNUM, &base);
2735833d
WT
362
363 if (ptrace (PTRACE_ARCH_PRCTL, tid, base, ARCH_SET_FS) < 0)
364 perror_with_name (_("Couldn't write segment register fs_base"));
365 }
366 if (regnum == -1 || regnum == AMD64_GSBASE_REGNUM)
367 {
368
34a79281 369 regcache->raw_collect (AMD64_GSBASE_REGNUM, &base);
2735833d
WT
370 if (ptrace (PTRACE_ARCH_PRCTL, tid, base, ARCH_SET_GS) < 0)
371 perror_with_name (_("Couldn't write segment register gs_base"));
372 }
373 }
374#endif
53e95fcf 375 }
53e95fcf
JS
376}
377\f
1aa7e42c 378
50d71875
AC
379/* This function is called by libthread_db as part of its handling of
380 a request for a thread's local storage address. */
381
5bca7895 382ps_err_e
754653a7 383ps_get_thread_area (struct ps_prochandle *ph,
c43af07c
EZ
384 lwpid_t lwpid, int idx, void **base)
385{
f5656ead 386 if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32)
50d71875 387 {
8c420b8d
GB
388 unsigned int base_addr;
389 ps_err_e result;
390
391 result = x86_linux_get_thread_area (lwpid, (void *) (long) idx,
392 &base_addr);
393 if (result == PS_OK)
394 {
395 /* Extend the value to 64 bits. Here it's assumed that
396 a "long" and a "void *" are the same. */
397 (*base) = (void *) (long) base_addr;
398 }
399 return result;
50d71875
AC
400 }
401 else
402 {
2735833d 403
50d71875
AC
404 /* FIXME: ezannoni-2003-07-09 see comment above about include
405 file order. We could be getting bogus values for these two. */
406 gdb_assert (FS < ELF_NGREG);
407 gdb_assert (GS < ELF_NGREG);
408 switch (idx)
409 {
410 case FS:
6fea9e18
L
411#ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
412 {
413 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
414 fs_base and gs_base fields of user_regs_struct can be
415 used directly. */
416 unsigned long fs;
417 errno = 0;
418 fs = ptrace (PTRACE_PEEKUSER, lwpid,
419 offsetof (struct user_regs_struct, fs_base), 0);
420 if (errno == 0)
421 {
422 *base = (void *) fs;
423 return PS_OK;
424 }
425 }
426#endif
50d71875
AC
427 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
428 return PS_OK;
429 break;
430 case GS:
6fea9e18
L
431#ifdef HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE
432 {
433 unsigned long gs;
434 errno = 0;
435 gs = ptrace (PTRACE_PEEKUSER, lwpid,
436 offsetof (struct user_regs_struct, gs_base), 0);
437 if (errno == 0)
438 {
439 *base = (void *) gs;
440 return PS_OK;
441 }
442 }
443#endif
50d71875
AC
444 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
445 return PS_OK;
446 break;
447 default: /* Should not happen. */
448 return PS_BADADDR;
449 }
c43af07c 450 }
b6d42148 451 return PS_ERR; /* ptrace failed. */
c43af07c 452}
5bca7895 453\f
c43af07c 454
9cf12d57 455/* Convert a ptrace/host siginfo object, into/from the siginfo in the
5b009018
PA
456 layout of the inferiors' architecture. Returns true if any
457 conversion was done; false otherwise. If DIRECTION is 1, then copy
9cf12d57 458 from INF to PTRACE. If DIRECTION is 0, copy from PTRACE to
5b009018
PA
459 INF. */
460
135340af
PA
461bool
462amd64_linux_nat_target::low_siginfo_fixup (siginfo_t *ptrace,
463 gdb_byte *inf,
464 int direction)
5b009018 465{
ba224c70
L
466 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
467
5b009018
PA
468 /* Is the inferior 32-bit? If so, then do fixup the siginfo
469 object. */
ba224c70 470 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
135340af
PA
471 return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
472 FIXUP_32);
ba224c70
L
473 /* No fixup for native x32 GDB. */
474 else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
135340af
PA
475 return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
476 FIXUP_X32);
5b009018 477 else
135340af 478 return false;
5b009018 479}
c1e246a0 480
c1e246a0
GB
481void
482_initialize_amd64_linux_nat (void)
483{
c1e246a0
GB
484 amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
485 amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
486 amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
487 amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;
488
489 gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
490 == amd64_native_gregset32_num_regs);
491
f6ac5f3d 492 linux_target = &the_amd64_linux_nat_target;
c1e246a0
GB
493
494 /* Add the target. */
d9f719f1 495 add_inf_child_target (linux_target);
c1e246a0 496}
This page took 1.27657 seconds and 4 git commands to generate.