amd64-fbsd-tdep.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Target-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 "osabi.h"
  16. #include "regset.h"
  17. #include "target.h"
  18. #include "trad-frame.h"
  19. #include "tramp-frame.h"
  20. #include "i386-fbsd-tdep.h"
  21. #include "gdbsupport/x86-xstate.h"
  22. #include "amd64-tdep.h"
  23. #include "amd64-fbsd-tdep.h"
  24. #include "fbsd-tdep.h"
  25. #include "solib-svr4.h"
  26. #include "inferior.h"
  27. /* The general-purpose regset consists of 22 64-bit slots, most of
  28. which contain individual registers, but a few contain multiple
  29. 16-bit segment registers. */
  30. #define AMD64_FBSD_SIZEOF_GREGSET (22 * 8)
  31. /* The segment base register set consists of 2 64-bit registers. */
  32. #define AMD64_FBSD_SIZEOF_SEGBASES_REGSET (2 * 8)
  33. /* Register maps. */
  34. static const struct regcache_map_entry amd64_fbsd_gregmap[] =
  35. {
  36. { 1, AMD64_R15_REGNUM, 0 },
  37. { 1, AMD64_R14_REGNUM, 0 },
  38. { 1, AMD64_R13_REGNUM, 0 },
  39. { 1, AMD64_R12_REGNUM, 0 },
  40. { 1, AMD64_R11_REGNUM, 0 },
  41. { 1, AMD64_R10_REGNUM, 0 },
  42. { 1, AMD64_R9_REGNUM, 0 },
  43. { 1, AMD64_R8_REGNUM, 0 },
  44. { 1, AMD64_RDI_REGNUM, 0 },
  45. { 1, AMD64_RSI_REGNUM, 0 },
  46. { 1, AMD64_RBP_REGNUM, 0 },
  47. { 1, AMD64_RBX_REGNUM, 0 },
  48. { 1, AMD64_RDX_REGNUM, 0 },
  49. { 1, AMD64_RCX_REGNUM, 0 },
  50. { 1, AMD64_RAX_REGNUM, 0 },
  51. { 1, REGCACHE_MAP_SKIP, 4 }, /* trapno */
  52. { 1, AMD64_FS_REGNUM, 2 },
  53. { 1, AMD64_GS_REGNUM, 2 },
  54. { 1, REGCACHE_MAP_SKIP, 4 }, /* err */
  55. { 1, AMD64_ES_REGNUM, 2 },
  56. { 1, AMD64_DS_REGNUM, 2 },
  57. { 1, AMD64_RIP_REGNUM, 0 },
  58. { 1, AMD64_CS_REGNUM, 8 },
  59. { 1, AMD64_EFLAGS_REGNUM, 8 },
  60. { 1, AMD64_RSP_REGNUM, 0 },
  61. { 1, AMD64_SS_REGNUM, 8 },
  62. { 0 }
  63. };
  64. static const struct regcache_map_entry amd64_fbsd_segbases_regmap[] =
  65. {
  66. { 1, AMD64_FSBASE_REGNUM, 0 },
  67. { 1, AMD64_GSBASE_REGNUM, 0 },
  68. { 0 }
  69. };
  70. /* This layout including fsbase and gsbase was adopted in FreeBSD
  71. 8.0. */
  72. static const struct regcache_map_entry amd64_fbsd_mcregmap[] =
  73. {
  74. { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_onstack */
  75. { 1, AMD64_RDI_REGNUM, 0 },
  76. { 1, AMD64_RSI_REGNUM, 0 },
  77. { 1, AMD64_RDX_REGNUM, 0 },
  78. { 1, AMD64_RCX_REGNUM, 0 },
  79. { 1, AMD64_R8_REGNUM, 0 },
  80. { 1, AMD64_R9_REGNUM, 0 },
  81. { 1, AMD64_RAX_REGNUM, 0 },
  82. { 1, AMD64_RBX_REGNUM, 0 },
  83. { 1, AMD64_RBP_REGNUM, 0 },
  84. { 1, AMD64_R10_REGNUM, 0 },
  85. { 1, AMD64_R11_REGNUM, 0 },
  86. { 1, AMD64_R12_REGNUM, 0 },
  87. { 1, AMD64_R13_REGNUM, 0 },
  88. { 1, AMD64_R14_REGNUM, 0 },
  89. { 1, AMD64_R15_REGNUM, 0 },
  90. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_trapno */
  91. { 1, AMD64_FS_REGNUM, 2 },
  92. { 1, AMD64_GS_REGNUM, 2 },
  93. { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_addr */
  94. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_flags */
  95. { 1, AMD64_ES_REGNUM, 2 },
  96. { 1, AMD64_DS_REGNUM, 2 },
  97. { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_err */
  98. { 1, AMD64_RIP_REGNUM, 0 },
  99. { 1, AMD64_CS_REGNUM, 8 },
  100. { 1, AMD64_EFLAGS_REGNUM, 8 },
  101. { 1, AMD64_RSP_REGNUM, 0 },
  102. { 1, AMD64_SS_REGNUM, 8 },
  103. { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_len */
  104. { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_fpformat */
  105. { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_ownedfp */
  106. { 64, REGCACHE_MAP_SKIP, 8 }, /* mc_fpstate */
  107. { 1, AMD64_FSBASE_REGNUM, 0 },
  108. { 1, AMD64_GSBASE_REGNUM, 0 },
  109. { 0 }
  110. };
  111. /* Register set definitions. */
  112. const struct regset amd64_fbsd_gregset =
  113. {
  114. amd64_fbsd_gregmap, regcache_supply_regset, regcache_collect_regset
  115. };
  116. const struct regset amd64_fbsd_segbases_regset =
  117. {
  118. amd64_fbsd_segbases_regmap, regcache_supply_regset, regcache_collect_regset
  119. };
  120. /* Support for signal handlers. */
  121. /* In a signal frame, rsp points to a 'struct sigframe' which is
  122. defined as:
  123. struct sigframe {
  124. union {
  125. __siginfohandler_t *sf_action;
  126. __sighandler_t *sf_handler;
  127. } sf_ahu;
  128. ucontext_t sf_uc;
  129. ...
  130. }
  131. ucontext_t is defined as:
  132. struct __ucontext {
  133. sigset_t uc_sigmask;
  134. mcontext_t uc_mcontext;
  135. ...
  136. };
  137. The mcontext_t contains the general purpose register set as well
  138. as the floating point or XSAVE state. */
  139. /* NB: There is an 8 byte padding hole between sf_ahu and sf_uc. */
  140. #define AMD64_SIGFRAME_UCONTEXT_OFFSET 16
  141. #define AMD64_UCONTEXT_MCONTEXT_OFFSET 16
  142. #define AMD64_SIZEOF_MCONTEXT_T 800
  143. /* Implement the "init" method of struct tramp_frame. */
  144. static void
  145. amd64_fbsd_sigframe_init (const struct tramp_frame *self,
  146. struct frame_info *this_frame,
  147. struct trad_frame_cache *this_cache,
  148. CORE_ADDR func)
  149. {
  150. CORE_ADDR sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
  151. CORE_ADDR mcontext_addr
  152. = (sp
  153. + AMD64_SIGFRAME_UCONTEXT_OFFSET
  154. + AMD64_UCONTEXT_MCONTEXT_OFFSET);
  155. trad_frame_set_reg_regmap (this_cache, amd64_fbsd_mcregmap, mcontext_addr,
  156. AMD64_SIZEOF_MCONTEXT_T);
  157. /* Don't bother with floating point or XSAVE state for now. The
  158. current helper routines for parsing FXSAVE and XSAVE state only
  159. work with regcaches. This could perhaps create a temporary
  160. regcache, collect the register values from mc_fpstate and
  161. mc_xfpustate, and then set register values in the trad_frame. */
  162. trad_frame_set_id (this_cache, frame_id_build (sp, func));
  163. }
  164. static const struct tramp_frame amd64_fbsd_sigframe =
  165. {
  166. SIGTRAMP_FRAME,
  167. 1,
  168. {
  169. {0x48, ULONGEST_MAX}, /* lea SIGF_UC(%rsp),%rdi */
  170. {0x8d, ULONGEST_MAX},
  171. {0x7c, ULONGEST_MAX},
  172. {0x24, ULONGEST_MAX},
  173. {0x10, ULONGEST_MAX},
  174. {0x6a, ULONGEST_MAX}, /* pushq $0 */
  175. {0x00, ULONGEST_MAX},
  176. {0x48, ULONGEST_MAX}, /* movq $SYS_sigreturn,%rax */
  177. {0xc7, ULONGEST_MAX},
  178. {0xc0, ULONGEST_MAX},
  179. {0xa1, ULONGEST_MAX},
  180. {0x01, ULONGEST_MAX},
  181. {0x00, ULONGEST_MAX},
  182. {0x00, ULONGEST_MAX},
  183. {0x0f, ULONGEST_MAX}, /* syscall */
  184. {0x05, ULONGEST_MAX},
  185. {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
  186. },
  187. amd64_fbsd_sigframe_init
  188. };
  189. /* Implement the core_read_description gdbarch method. */
  190. static const struct target_desc *
  191. amd64fbsd_core_read_description (struct gdbarch *gdbarch,
  192. struct target_ops *target,
  193. bfd *abfd)
  194. {
  195. return amd64_target_description (i386fbsd_core_read_xcr0 (abfd), true);
  196. }
  197. /* Similar to amd64_supply_fpregset, but use XSAVE extended state. */
  198. static void
  199. amd64fbsd_supply_xstateregset (const struct regset *regset,
  200. struct regcache *regcache, int regnum,
  201. const void *xstateregs, size_t len)
  202. {
  203. amd64_supply_xsave (regcache, regnum, xstateregs);
  204. }
  205. /* Similar to amd64_collect_fpregset, but use XSAVE extended state. */
  206. static void
  207. amd64fbsd_collect_xstateregset (const struct regset *regset,
  208. const struct regcache *regcache,
  209. int regnum, void *xstateregs, size_t len)
  210. {
  211. amd64_collect_xsave (regcache, regnum, xstateregs, 1);
  212. }
  213. static const struct regset amd64fbsd_xstateregset =
  214. {
  215. NULL,
  216. amd64fbsd_supply_xstateregset,
  217. amd64fbsd_collect_xstateregset
  218. };
  219. /* Iterate over core file register note sections. */
  220. static void
  221. amd64fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  222. iterate_over_regset_sections_cb *cb,
  223. void *cb_data,
  224. const struct regcache *regcache)
  225. {
  226. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  227. cb (".reg", AMD64_FBSD_SIZEOF_GREGSET, AMD64_FBSD_SIZEOF_GREGSET,
  228. &amd64_fbsd_gregset, NULL, cb_data);
  229. cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, &amd64_fpregset,
  230. NULL, cb_data);
  231. cb (".reg-x86-segbases", AMD64_FBSD_SIZEOF_SEGBASES_REGSET,
  232. AMD64_FBSD_SIZEOF_SEGBASES_REGSET, &amd64_fbsd_segbases_regset,
  233. "segment bases", cb_data);
  234. cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0), X86_XSTATE_SIZE (tdep->xcr0),
  235. &amd64fbsd_xstateregset, "XSAVE extended state", cb_data);
  236. }
  237. /* Implement the get_thread_local_address gdbarch method. */
  238. static CORE_ADDR
  239. amd64fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
  240. CORE_ADDR lm_addr, CORE_ADDR offset)
  241. {
  242. struct regcache *regcache;
  243. regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
  244. ptid, gdbarch);
  245. target_fetch_registers (regcache, AMD64_FSBASE_REGNUM);
  246. ULONGEST fsbase;
  247. if (regcache->cooked_read (AMD64_FSBASE_REGNUM, &fsbase) != REG_VALID)
  248. error (_("Unable to fetch %%fsbase"));
  249. CORE_ADDR dtv_addr = fsbase + gdbarch_ptr_bit (gdbarch) / 8;
  250. return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
  251. }
  252. static void
  253. amd64fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  254. {
  255. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  256. /* Generic FreeBSD support. */
  257. fbsd_init_abi (info, gdbarch);
  258. /* Obviously FreeBSD is BSD-based. */
  259. i386bsd_init_abi (info, gdbarch);
  260. amd64_init_abi (info, gdbarch,
  261. amd64_target_description (X86_XSTATE_SSE_MASK, true));
  262. tramp_frame_prepend_unwinder (gdbarch, &amd64_fbsd_sigframe);
  263. tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET;
  264. /* Iterate over core file register note sections. */
  265. set_gdbarch_iterate_over_regset_sections
  266. (gdbarch, amd64fbsd_iterate_over_regset_sections);
  267. set_gdbarch_core_read_description (gdbarch,
  268. amd64fbsd_core_read_description);
  269. /* FreeBSD uses SVR4-style shared libraries. */
  270. set_solib_svr4_fetch_link_map_offsets
  271. (gdbarch, svr4_lp64_fetch_link_map_offsets);
  272. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  273. svr4_fetch_objfile_link_map);
  274. set_gdbarch_get_thread_local_address (gdbarch,
  275. amd64fbsd_get_thread_local_address);
  276. }
  277. void _initialize_amd64fbsd_tdep ();
  278. void
  279. _initialize_amd64fbsd_tdep ()
  280. {
  281. gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
  282. GDB_OSABI_FREEBSD, amd64fbsd_init_abi);
  283. }