amd64-fbsd-nat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* Native-dependent code for FreeBSD/amd64.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "inferior.h"
  16. #include "regcache.h"
  17. #include "target.h"
  18. #include <signal.h>
  19. #include <sys/types.h>
  20. #include <sys/ptrace.h>
  21. #include <sys/sysctl.h>
  22. #include <sys/user.h>
  23. #include <machine/reg.h>
  24. #include "amd64-tdep.h"
  25. #include "amd64-fbsd-tdep.h"
  26. #include "amd64-nat.h"
  27. #include "x86-nat.h"
  28. #include "gdbsupport/x86-xstate.h"
  29. #include "x86-fbsd-nat.h"
  30. class amd64_fbsd_nat_target final : public x86_fbsd_nat_target
  31. {
  32. public:
  33. void fetch_registers (struct regcache *, int) override;
  34. void store_registers (struct regcache *, int) override;
  35. const struct target_desc *read_description () override;
  36. };
  37. static amd64_fbsd_nat_target the_amd64_fbsd_nat_target;
  38. #ifdef PT_GETXSTATE_INFO
  39. static size_t xsave_len;
  40. #endif
  41. /* This is a layout of the amd64 'struct reg' but with i386
  42. registers. */
  43. static const struct regcache_map_entry amd64_fbsd32_gregmap[] =
  44. {
  45. { 8, REGCACHE_MAP_SKIP, 8 },
  46. { 1, I386_EDI_REGNUM, 8 },
  47. { 1, I386_ESI_REGNUM, 8 },
  48. { 1, I386_EBP_REGNUM, 8 },
  49. { 1, I386_EBX_REGNUM, 8 },
  50. { 1, I386_EDX_REGNUM, 8 },
  51. { 1, I386_ECX_REGNUM, 8 },
  52. { 1, I386_EAX_REGNUM, 8 },
  53. { 1, REGCACHE_MAP_SKIP, 4 }, /* trapno */
  54. { 1, I386_FS_REGNUM, 2 },
  55. { 1, I386_GS_REGNUM, 2 },
  56. { 1, REGCACHE_MAP_SKIP, 4 }, /* err */
  57. { 1, I386_ES_REGNUM, 2 },
  58. { 1, I386_DS_REGNUM, 2 },
  59. { 1, I386_EIP_REGNUM, 8 },
  60. { 1, I386_CS_REGNUM, 8 },
  61. { 1, I386_EFLAGS_REGNUM, 8 },
  62. { 1, I386_ESP_REGNUM, 0 },
  63. { 1, I386_SS_REGNUM, 8 },
  64. { 0 }
  65. };
  66. static const struct regset amd64_fbsd32_gregset =
  67. {
  68. amd64_fbsd32_gregmap, regcache_supply_regset, regcache_collect_regset
  69. };
  70. /* Return the regset to use for 'struct reg' for the GDBARCH. */
  71. static const struct regset *
  72. find_gregset (struct gdbarch *gdbarch)
  73. {
  74. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  75. return &amd64_fbsd32_gregset;
  76. else
  77. return &amd64_fbsd_gregset;
  78. }
  79. /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
  80. for all registers. */
  81. void
  82. amd64_fbsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  83. {
  84. struct gdbarch *gdbarch = regcache->arch ();
  85. #if defined(PT_GETFSBASE) || defined(PT_GETGSBASE)
  86. const i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  87. #endif
  88. pid_t pid = get_ptrace_pid (regcache->ptid ());
  89. const struct regset *gregset = find_gregset (gdbarch);
  90. if (fetch_register_set<struct reg> (regcache, regnum, PT_GETREGS, gregset))
  91. {
  92. if (regnum != -1)
  93. return;
  94. }
  95. #ifdef PT_GETFSBASE
  96. if (regnum == -1 || regnum == tdep->fsbase_regnum)
  97. {
  98. register_t base;
  99. if (ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
  100. perror_with_name (_("Couldn't get segment register fs_base"));
  101. regcache->raw_supply (tdep->fsbase_regnum, &base);
  102. if (regnum != -1)
  103. return;
  104. }
  105. #endif
  106. #ifdef PT_GETGSBASE
  107. if (regnum == -1 || regnum == tdep->fsbase_regnum + 1)
  108. {
  109. register_t base;
  110. if (ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
  111. perror_with_name (_("Couldn't get segment register gs_base"));
  112. regcache->raw_supply (tdep->fsbase_regnum + 1, &base);
  113. if (regnum != -1)
  114. return;
  115. }
  116. #endif
  117. /* There is no amd64_fxsave_supplies or amd64_xsave_supplies.
  118. Instead, the earlier register sets return early if the request
  119. was for a specific register that was already satisified to avoid
  120. fetching the FPU/XSAVE state unnecessarily. */
  121. #ifdef PT_GETXSTATE_INFO
  122. if (xsave_len != 0)
  123. {
  124. void *xstateregs = alloca (xsave_len);
  125. if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
  126. perror_with_name (_("Couldn't get extended state status"));
  127. amd64_supply_xsave (regcache, regnum, xstateregs);
  128. return;
  129. }
  130. #endif
  131. struct fpreg fpregs;
  132. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  133. perror_with_name (_("Couldn't get floating point status"));
  134. amd64_supply_fxsave (regcache, regnum, &fpregs);
  135. }
  136. /* Store register REGNUM back into the inferior. If REGNUM is -1, do
  137. this for all registers. */
  138. void
  139. amd64_fbsd_nat_target::store_registers (struct regcache *regcache, int regnum)
  140. {
  141. struct gdbarch *gdbarch = regcache->arch ();
  142. #if defined(PT_GETFSBASE) || defined(PT_GETGSBASE)
  143. const i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  144. #endif
  145. pid_t pid = get_ptrace_pid (regcache->ptid ());
  146. const struct regset *gregset = find_gregset (gdbarch);
  147. if (store_register_set<struct reg> (regcache, regnum, PT_GETREGS, PT_SETREGS,
  148. gregset))
  149. {
  150. if (regnum != -1)
  151. return;
  152. }
  153. #ifdef PT_SETFSBASE
  154. if (regnum == -1 || regnum == tdep->fsbase_regnum)
  155. {
  156. register_t base;
  157. /* Clear the full base value to support 32-bit targets. */
  158. base = 0;
  159. regcache->raw_collect (tdep->fsbase_regnum, &base);
  160. if (ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
  161. perror_with_name (_("Couldn't write segment register fs_base"));
  162. if (regnum != -1)
  163. return;
  164. }
  165. #endif
  166. #ifdef PT_SETGSBASE
  167. if (regnum == -1 || regnum == tdep->fsbase_regnum + 1)
  168. {
  169. register_t base;
  170. /* Clear the full base value to support 32-bit targets. */
  171. base = 0;
  172. regcache->raw_collect (tdep->fsbase_regnum + 1, &base);
  173. if (ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
  174. perror_with_name (_("Couldn't write segment register gs_base"));
  175. if (regnum != -1)
  176. return;
  177. }
  178. #endif
  179. /* There is no amd64_fxsave_supplies or amd64_xsave_supplies.
  180. Instead, the earlier register sets return early if the request
  181. was for a specific register that was already satisified to avoid
  182. fetching the FPU/XSAVE state unnecessarily. */
  183. #ifdef PT_GETXSTATE_INFO
  184. if (xsave_len != 0)
  185. {
  186. void *xstateregs = alloca (xsave_len);
  187. if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
  188. perror_with_name (_("Couldn't get extended state status"));
  189. amd64_collect_xsave (regcache, regnum, xstateregs, 0);
  190. if (ptrace (PT_SETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs,
  191. xsave_len) == -1)
  192. perror_with_name (_("Couldn't write extended state status"));
  193. return;
  194. }
  195. #endif
  196. struct fpreg fpregs;
  197. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  198. perror_with_name (_("Couldn't get floating point status"));
  199. amd64_collect_fxsave (regcache, regnum, &fpregs);
  200. if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  201. perror_with_name (_("Couldn't write floating point status"));
  202. }
  203. /* Support for debugging kernel virtual memory images. */
  204. #include <machine/pcb.h>
  205. #include <osreldate.h>
  206. #include "bsd-kvm.h"
  207. static int
  208. amd64fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  209. {
  210. /* The following is true for FreeBSD 5.2:
  211. The pcb contains %rip, %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15,
  212. %ds, %es, %fs and %gs. This accounts for all callee-saved
  213. registers specified by the psABI and then some. Here %esp
  214. contains the stack pointer at the point just after the call to
  215. cpu_switch(). From this information we reconstruct the register
  216. state as it would like when we just returned from cpu_switch(). */
  217. /* The stack pointer shouldn't be zero. */
  218. if (pcb->pcb_rsp == 0)
  219. return 0;
  220. pcb->pcb_rsp += 8;
  221. regcache->raw_supply (AMD64_RIP_REGNUM, &pcb->pcb_rip);
  222. regcache->raw_supply (AMD64_RBX_REGNUM, &pcb->pcb_rbx);
  223. regcache->raw_supply (AMD64_RSP_REGNUM, &pcb->pcb_rsp);
  224. regcache->raw_supply (AMD64_RBP_REGNUM, &pcb->pcb_rbp);
  225. regcache->raw_supply (12, &pcb->pcb_r12);
  226. regcache->raw_supply (13, &pcb->pcb_r13);
  227. regcache->raw_supply (14, &pcb->pcb_r14);
  228. regcache->raw_supply (15, &pcb->pcb_r15);
  229. #if (__FreeBSD_version < 800075) && (__FreeBSD_kernel_version < 800075)
  230. /* struct pcb provides the pcb_ds/pcb_es/pcb_fs/pcb_gs fields only
  231. up until __FreeBSD_version 800074: The removal of these fields
  232. occurred on 2009-04-01 while the __FreeBSD_version number was
  233. bumped to 800075 on 2009-04-06. So 800075 is the closest version
  234. number where we should not try to access these fields. */
  235. regcache->raw_supply (AMD64_DS_REGNUM, &pcb->pcb_ds);
  236. regcache->raw_supply (AMD64_ES_REGNUM, &pcb->pcb_es);
  237. regcache->raw_supply (AMD64_FS_REGNUM, &pcb->pcb_fs);
  238. regcache->raw_supply (AMD64_GS_REGNUM, &pcb->pcb_gs);
  239. #endif
  240. return 1;
  241. }
  242. /* Implement the read_description method. */
  243. const struct target_desc *
  244. amd64_fbsd_nat_target::read_description ()
  245. {
  246. #ifdef PT_GETXSTATE_INFO
  247. static int xsave_probed;
  248. static uint64_t xcr0;
  249. #endif
  250. struct reg regs;
  251. int is64;
  252. if (ptrace (PT_GETREGS, inferior_ptid.pid (),
  253. (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  254. perror_with_name (_("Couldn't get registers"));
  255. is64 = (regs.r_cs == GSEL (GUCODE_SEL, SEL_UPL));
  256. #ifdef PT_GETXSTATE_INFO
  257. if (!xsave_probed)
  258. {
  259. struct ptrace_xstate_info info;
  260. if (ptrace (PT_GETXSTATE_INFO, inferior_ptid.pid (),
  261. (PTRACE_TYPE_ARG3) &info, sizeof (info)) == 0)
  262. {
  263. xsave_len = info.xsave_len;
  264. xcr0 = info.xsave_mask;
  265. }
  266. xsave_probed = 1;
  267. }
  268. if (xsave_len != 0)
  269. {
  270. if (is64)
  271. return amd64_target_description (xcr0, true);
  272. else
  273. return i386_target_description (xcr0, true);
  274. }
  275. #endif
  276. if (is64)
  277. return amd64_target_description (X86_XSTATE_SSE_MASK, true);
  278. else
  279. return i386_target_description (X86_XSTATE_SSE_MASK, true);
  280. }
  281. void _initialize_amd64fbsd_nat ();
  282. void
  283. _initialize_amd64fbsd_nat ()
  284. {
  285. add_inf_child_target (&the_amd64_fbsd_nat_target);
  286. /* Support debugging kernel virtual memory images. */
  287. bsd_kvm_add_target (amd64fbsd_supply_pcb);
  288. }