amd64-linux-nat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* Native-dependent code for GNU/Linux x86-64.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. Contributed by Jiri Smid, SuSE Labs.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "inferior.h"
  17. #include "regcache.h"
  18. #include "elf/common.h"
  19. #include <sys/uio.h>
  20. #include "nat/gdb_ptrace.h"
  21. #include <asm/prctl.h>
  22. #include <sys/reg.h>
  23. #include "gregset.h"
  24. #include "gdb_proc_service.h"
  25. #include "amd64-nat.h"
  26. #include "amd64-tdep.h"
  27. #include "amd64-linux-tdep.h"
  28. #include "i386-linux-tdep.h"
  29. #include "gdbsupport/x86-xstate.h"
  30. #include "x86-linux-nat.h"
  31. #include "nat/linux-ptrace.h"
  32. #include "nat/amd64-linux-siginfo.h"
  33. /* This definition comes from prctl.h. Kernels older than 2.5.64
  34. do not have it. */
  35. #ifndef PTRACE_ARCH_PRCTL
  36. #define PTRACE_ARCH_PRCTL 30
  37. #endif
  38. struct amd64_linux_nat_target final : public x86_linux_nat_target
  39. {
  40. /* Add our register access methods. */
  41. void fetch_registers (struct regcache *, int) override;
  42. void store_registers (struct regcache *, int) override;
  43. bool low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction)
  44. override;
  45. };
  46. static amd64_linux_nat_target the_amd64_linux_nat_target;
  47. /* Mapping between the general-purpose registers in GNU/Linux x86-64
  48. `struct user' format and GDB's register cache layout for GNU/Linux
  49. i386.
  50. Note that most GNU/Linux x86-64 registers are 64-bit, while the
  51. GNU/Linux i386 registers are all 32-bit, but since we're
  52. little-endian we get away with that. */
  53. /* From <sys/reg.h> on GNU/Linux i386. */
  54. static int amd64_linux_gregset32_reg_offset[] =
  55. {
  56. RAX * 8, RCX * 8, /* %eax, %ecx */
  57. RDX * 8, RBX * 8, /* %edx, %ebx */
  58. RSP * 8, RBP * 8, /* %esp, %ebp */
  59. RSI * 8, RDI * 8, /* %esi, %edi */
  60. RIP * 8, EFLAGS * 8, /* %eip, %eflags */
  61. CS * 8, SS * 8, /* %cs, %ss */
  62. DS * 8, ES * 8, /* %ds, %es */
  63. FS * 8, GS * 8, /* %fs, %gs */
  64. -1, -1, -1, -1, -1, -1, -1, -1,
  65. -1, -1, -1, -1, -1, -1, -1, -1,
  66. -1, -1, -1, -1, -1, -1, -1, -1, -1,
  67. -1, -1, -1, -1, -1, -1, -1, -1,
  68. -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
  69. -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
  70. -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
  71. -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512) */
  72. -1, /* PKEYS register PKRU */
  73. ORIG_RAX * 8 /* "orig_eax" */
  74. };
  75. /* Transfering the general-purpose registers between GDB, inferiors
  76. and core files. */
  77. /* See amd64_collect_native_gregset. This linux specific version handles
  78. issues with negative EAX values not being restored correctly upon syscall
  79. return when debugging 32-bit targets. It has no effect on 64-bit
  80. targets. */
  81. static void
  82. amd64_linux_collect_native_gregset (const struct regcache *regcache,
  83. void *gregs, int regnum)
  84. {
  85. amd64_collect_native_gregset (regcache, gregs, regnum);
  86. struct gdbarch *gdbarch = regcache->arch ();
  87. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  88. {
  89. /* Sign extend EAX value to avoid potential syscall restart
  90. problems.
  91. On Linux, when a syscall is interrupted by a signal, the
  92. (kernel function implementing the) syscall may return
  93. -ERESTARTSYS when a signal occurs. Doing so indicates that
  94. the syscall is restartable. Then, depending on settings
  95. associated with the signal handler, and after the signal
  96. handler is called, the kernel can then either return -EINTR
  97. or it can cause the syscall to be restarted. We are
  98. concerned with the latter case here.
  99. On (32-bit) i386, the status (-ERESTARTSYS) is placed in the
  100. EAX register. When debugging a 32-bit process from a 64-bit
  101. (amd64) GDB, the debugger fetches 64-bit registers even
  102. though the process being debugged is only 32-bit. The
  103. register cache is only 32 bits wide though; GDB discards the
  104. high 32 bits when placing 64-bit values in the 32-bit
  105. regcache. Normally, this is not a problem since the 32-bit
  106. process should only care about the lower 32-bit portions of
  107. these registers. That said, it can happen that the 64-bit
  108. value being restored will be different from the 64-bit value
  109. that was originally retrieved from the kernel. The one place
  110. (that we know of) where it does matter is in the kernel's
  111. syscall restart code. The kernel's code for restarting a
  112. syscall after a signal expects to see a negative value
  113. (specifically -ERESTARTSYS) in the 64-bit RAX register in
  114. order to correctly cause a syscall to be restarted.
  115. The call to amd64_collect_native_gregset, above, is setting
  116. the high 32 bits of RAX (and other registers too) to 0. For
  117. syscall restart, we need to sign extend EAX so that RAX will
  118. appear as a negative value when EAX is set to -ERESTARTSYS.
  119. This in turn will cause the signal handling code in the
  120. kernel to recognize -ERESTARTSYS which will in turn cause the
  121. syscall to be restarted.
  122. The test case gdb.base/interrupt.exp tests for this problem.
  123. Without this sign extension code in place, it'll show
  124. a number of failures when testing against unix/-m32. */
  125. if (regnum == -1 || regnum == I386_EAX_REGNUM)
  126. {
  127. void *ptr = ((gdb_byte *) gregs
  128. + amd64_linux_gregset32_reg_offset[I386_EAX_REGNUM]);
  129. *(int64_t *) ptr = *(int32_t *) ptr;
  130. }
  131. }
  132. }
  133. /* Fill GDB's register cache with the general-purpose register values
  134. in *GREGSETP. */
  135. void
  136. supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
  137. {
  138. amd64_supply_native_gregset (regcache, gregsetp, -1);
  139. }
  140. /* Fill register REGNUM (if it is a general-purpose register) in
  141. *GREGSETP with the value in GDB's register cache. If REGNUM is -1,
  142. do this for all registers. */
  143. void
  144. fill_gregset (const struct regcache *regcache,
  145. elf_gregset_t *gregsetp, int regnum)
  146. {
  147. amd64_linux_collect_native_gregset (regcache, gregsetp, regnum);
  148. }
  149. /* Transfering floating-point registers between GDB, inferiors and cores. */
  150. /* Fill GDB's register cache with the floating-point and SSE register
  151. values in *FPREGSETP. */
  152. void
  153. supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
  154. {
  155. amd64_supply_fxsave (regcache, -1, fpregsetp);
  156. }
  157. /* Fill register REGNUM (if it is a floating-point or SSE register) in
  158. *FPREGSETP with the value in GDB's register cache. If REGNUM is
  159. -1, do this for all registers. */
  160. void
  161. fill_fpregset (const struct regcache *regcache,
  162. elf_fpregset_t *fpregsetp, int regnum)
  163. {
  164. amd64_collect_fxsave (regcache, regnum, fpregsetp);
  165. }
  166. /* Transferring arbitrary registers between GDB and inferior. */
  167. /* Fetch register REGNUM from the child process. If REGNUM is -1, do
  168. this for all registers (including the floating point and SSE
  169. registers). */
  170. void
  171. amd64_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  172. {
  173. struct gdbarch *gdbarch = regcache->arch ();
  174. int tid;
  175. /* GNU/Linux LWP ID's are process ID's. */
  176. tid = regcache->ptid ().lwp ();
  177. if (tid == 0)
  178. tid = regcache->ptid ().pid (); /* Not a threaded program. */
  179. if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
  180. {
  181. elf_gregset_t regs;
  182. if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
  183. perror_with_name (_("Couldn't get registers"));
  184. amd64_supply_native_gregset (regcache, &regs, -1);
  185. if (regnum != -1)
  186. return;
  187. }
  188. if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
  189. {
  190. elf_fpregset_t fpregs;
  191. if (have_ptrace_getregset == TRIBOOL_TRUE)
  192. {
  193. char xstateregs[X86_XSTATE_MAX_SIZE];
  194. struct iovec iov;
  195. /* Pre-4.14 kernels have a bug (fixed by commit 0852b374173b
  196. "x86/fpu: Add FPU state copying quirk to handle XRSTOR failure on
  197. Intel Skylake CPUs") that sometimes causes the mxcsr location in
  198. xstateregs not to be copied by PTRACE_GETREGSET. Make sure that
  199. the location is at least initialized with a defined value. */
  200. memset (xstateregs, 0, sizeof (xstateregs));
  201. iov.iov_base = xstateregs;
  202. iov.iov_len = sizeof (xstateregs);
  203. if (ptrace (PTRACE_GETREGSET, tid,
  204. (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
  205. perror_with_name (_("Couldn't get extended state status"));
  206. amd64_supply_xsave (regcache, -1, xstateregs);
  207. }
  208. else
  209. {
  210. if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
  211. perror_with_name (_("Couldn't get floating point status"));
  212. amd64_supply_fxsave (regcache, -1, &fpregs);
  213. }
  214. }
  215. }
  216. /* Store register REGNUM back into the child process. If REGNUM is
  217. -1, do this for all registers (including the floating-point and SSE
  218. registers). */
  219. void
  220. amd64_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
  221. {
  222. struct gdbarch *gdbarch = regcache->arch ();
  223. int tid;
  224. /* GNU/Linux LWP ID's are process ID's. */
  225. tid = regcache->ptid ().lwp ();
  226. if (tid == 0)
  227. tid = regcache->ptid ().pid (); /* Not a threaded program. */
  228. if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
  229. {
  230. elf_gregset_t regs;
  231. if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
  232. perror_with_name (_("Couldn't get registers"));
  233. amd64_linux_collect_native_gregset (regcache, &regs, regnum);
  234. if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
  235. perror_with_name (_("Couldn't write registers"));
  236. if (regnum != -1)
  237. return;
  238. }
  239. if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
  240. {
  241. elf_fpregset_t fpregs;
  242. if (have_ptrace_getregset == TRIBOOL_TRUE)
  243. {
  244. char xstateregs[X86_XSTATE_MAX_SIZE];
  245. struct iovec iov;
  246. iov.iov_base = xstateregs;
  247. iov.iov_len = sizeof (xstateregs);
  248. if (ptrace (PTRACE_GETREGSET, tid,
  249. (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
  250. perror_with_name (_("Couldn't get extended state status"));
  251. amd64_collect_xsave (regcache, regnum, xstateregs, 0);
  252. if (ptrace (PTRACE_SETREGSET, tid,
  253. (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
  254. perror_with_name (_("Couldn't write extended state status"));
  255. }
  256. else
  257. {
  258. if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
  259. perror_with_name (_("Couldn't get floating point status"));
  260. amd64_collect_fxsave (regcache, regnum, &fpregs);
  261. if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
  262. perror_with_name (_("Couldn't write floating point status"));
  263. }
  264. }
  265. }
  266. /* This function is called by libthread_db as part of its handling of
  267. a request for a thread's local storage address. */
  268. ps_err_e
  269. ps_get_thread_area (struct ps_prochandle *ph,
  270. lwpid_t lwpid, int idx, void **base)
  271. {
  272. if (gdbarch_bfd_arch_info (ph->thread->inf->gdbarch)->bits_per_word == 32)
  273. {
  274. unsigned int base_addr;
  275. ps_err_e result;
  276. result = x86_linux_get_thread_area (lwpid, (void *) (long) idx,
  277. &base_addr);
  278. if (result == PS_OK)
  279. {
  280. /* Extend the value to 64 bits. Here it's assumed that
  281. a "long" and a "void *" are the same. */
  282. (*base) = (void *) (long) base_addr;
  283. }
  284. return result;
  285. }
  286. else
  287. {
  288. /* FIXME: ezannoni-2003-07-09 see comment above about include
  289. file order. We could be getting bogus values for these two. */
  290. gdb_assert (FS < ELF_NGREG);
  291. gdb_assert (GS < ELF_NGREG);
  292. switch (idx)
  293. {
  294. case FS:
  295. {
  296. unsigned long fs;
  297. errno = 0;
  298. fs = ptrace (PTRACE_PEEKUSER, lwpid,
  299. offsetof (struct user_regs_struct, fs_base), 0);
  300. if (errno == 0)
  301. {
  302. *base = (void *) fs;
  303. return PS_OK;
  304. }
  305. }
  306. break;
  307. case GS:
  308. {
  309. unsigned long gs;
  310. errno = 0;
  311. gs = ptrace (PTRACE_PEEKUSER, lwpid,
  312. offsetof (struct user_regs_struct, gs_base), 0);
  313. if (errno == 0)
  314. {
  315. *base = (void *) gs;
  316. return PS_OK;
  317. }
  318. }
  319. break;
  320. default: /* Should not happen. */
  321. return PS_BADADDR;
  322. }
  323. }
  324. return PS_ERR; /* ptrace failed. */
  325. }
  326. /* Convert a ptrace/host siginfo object, into/from the siginfo in the
  327. layout of the inferiors' architecture. Returns true if any
  328. conversion was done; false otherwise. If DIRECTION is 1, then copy
  329. from INF to PTRACE. If DIRECTION is 0, copy from PTRACE to
  330. INF. */
  331. bool
  332. amd64_linux_nat_target::low_siginfo_fixup (siginfo_t *ptrace,
  333. gdb_byte *inf,
  334. int direction)
  335. {
  336. struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
  337. /* Is the inferior 32-bit? If so, then do fixup the siginfo
  338. object. */
  339. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  340. return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
  341. FIXUP_32);
  342. /* No fixup for native x32 GDB. */
  343. else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
  344. return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
  345. FIXUP_X32);
  346. else
  347. return false;
  348. }
  349. void _initialize_amd64_linux_nat ();
  350. void
  351. _initialize_amd64_linux_nat ()
  352. {
  353. amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
  354. amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
  355. amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
  356. amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;
  357. gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
  358. == amd64_native_gregset32_num_regs);
  359. linux_target = &the_amd64_linux_nat_target;
  360. /* Add the target. */
  361. add_inf_child_target (linux_target);
  362. }