i386-fbsd-tdep.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* Target-dependent code for FreeBSD/i386.
  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 "regcache.h"
  17. #include "regset.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 "i386-tdep.h"
  23. #include "i387-tdep.h"
  24. #include "fbsd-tdep.h"
  25. #include "solib-svr4.h"
  26. #include "inferior.h"
  27. /* The general-purpose regset consists of 19 32-bit slots. */
  28. #define I386_FBSD_SIZEOF_GREGSET (19 * 4)
  29. /* The segment base register set consists of 2 32-bit registers. */
  30. #define I386_FBSD_SIZEOF_SEGBASES_REGSET (2 * 4)
  31. /* Register maps. */
  32. static const struct regcache_map_entry i386_fbsd_gregmap[] =
  33. {
  34. { 1, I386_FS_REGNUM, 4 },
  35. { 1, I386_ES_REGNUM, 4 },
  36. { 1, I386_DS_REGNUM, 4 },
  37. { 1, I386_EDI_REGNUM, 0 },
  38. { 1, I386_ESI_REGNUM, 0 },
  39. { 1, I386_EBP_REGNUM, 0 },
  40. { 1, REGCACHE_MAP_SKIP, 4 }, /* isp */
  41. { 1, I386_EBX_REGNUM, 0 },
  42. { 1, I386_EDX_REGNUM, 0 },
  43. { 1, I386_ECX_REGNUM, 0 },
  44. { 1, I386_EAX_REGNUM, 0 },
  45. { 1, REGCACHE_MAP_SKIP, 4 }, /* trapno */
  46. { 1, REGCACHE_MAP_SKIP, 4 }, /* err */
  47. { 1, I386_EIP_REGNUM, 0 },
  48. { 1, I386_CS_REGNUM, 4 },
  49. { 1, I386_EFLAGS_REGNUM, 0 },
  50. { 1, I386_ESP_REGNUM, 0 },
  51. { 1, I386_SS_REGNUM, 4 },
  52. { 1, I386_GS_REGNUM, 4 },
  53. { 0 }
  54. };
  55. static const struct regcache_map_entry i386_fbsd_segbases_regmap[] =
  56. {
  57. { 1, I386_FSBASE_REGNUM, 0 },
  58. { 1, I386_GSBASE_REGNUM, 0 },
  59. { 0 }
  60. };
  61. /* This layout including fsbase and gsbase was adopted in FreeBSD
  62. 8.0. */
  63. static const struct regcache_map_entry i386_fbsd_mcregmap[] =
  64. {
  65. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_onstack */
  66. { 1, I386_GS_REGNUM, 4 },
  67. { 1, I386_FS_REGNUM, 4 },
  68. { 1, I386_ES_REGNUM, 4 },
  69. { 1, I386_DS_REGNUM, 4 },
  70. { 1, I386_EDI_REGNUM, 0 },
  71. { 1, I386_ESI_REGNUM, 0 },
  72. { 1, I386_EBP_REGNUM, 0 },
  73. { 1, REGCACHE_MAP_SKIP, 4 }, /* isp */
  74. { 1, I386_EBX_REGNUM, 0 },
  75. { 1, I386_EDX_REGNUM, 0 },
  76. { 1, I386_ECX_REGNUM, 0 },
  77. { 1, I386_EAX_REGNUM, 0 },
  78. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_trapno */
  79. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_err */
  80. { 1, I386_EIP_REGNUM, 0 },
  81. { 1, I386_CS_REGNUM, 4 },
  82. { 1, I386_EFLAGS_REGNUM, 0 },
  83. { 1, I386_ESP_REGNUM, 0 },
  84. { 1, I386_SS_REGNUM, 4 },
  85. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_len */
  86. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_fpformat */
  87. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_ownedfp */
  88. { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_flags */
  89. { 128, REGCACHE_MAP_SKIP, 4 },/* mc_fpstate */
  90. { 1, I386_FSBASE_REGNUM, 0 },
  91. { 1, I386_GSBASE_REGNUM, 0 },
  92. { 0 }
  93. };
  94. /* Register set definitions. */
  95. const struct regset i386_fbsd_gregset =
  96. {
  97. i386_fbsd_gregmap, regcache_supply_regset, regcache_collect_regset
  98. };
  99. const struct regset i386_fbsd_segbases_regset =
  100. {
  101. i386_fbsd_segbases_regmap, regcache_supply_regset, regcache_collect_regset
  102. };
  103. /* Support for signal handlers. */
  104. /* In a signal frame, esp points to a 'struct sigframe' which is
  105. defined as:
  106. struct sigframe {
  107. register_t sf_signum;
  108. register_t sf_siginfo;
  109. register_t sf_ucontext;
  110. register_t sf_addr;
  111. union {
  112. __siginfohandler_t *sf_action;
  113. __sighandler_t *sf_handler;
  114. } sf_ahu;
  115. ucontext_t sf_uc;
  116. ...
  117. }
  118. ucontext_t is defined as:
  119. struct __ucontext {
  120. sigset_t uc_sigmask;
  121. mcontext_t uc_mcontext;
  122. ...
  123. };
  124. The mcontext_t contains the general purpose register set as well
  125. as the floating point or XSAVE state. */
  126. /* NB: There is a 12 byte padding hole between sf_ahu and sf_uc. */
  127. #define I386_SIGFRAME_UCONTEXT_OFFSET 32
  128. #define I386_UCONTEXT_MCONTEXT_OFFSET 16
  129. #define I386_SIZEOF_MCONTEXT_T 640
  130. /* Implement the "init" method of struct tramp_frame. */
  131. static void
  132. i386_fbsd_sigframe_init (const struct tramp_frame *self,
  133. struct frame_info *this_frame,
  134. struct trad_frame_cache *this_cache,
  135. CORE_ADDR func)
  136. {
  137. CORE_ADDR sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
  138. CORE_ADDR mcontext_addr
  139. = (sp
  140. + I386_SIGFRAME_UCONTEXT_OFFSET
  141. + I386_UCONTEXT_MCONTEXT_OFFSET);
  142. trad_frame_set_reg_regmap (this_cache, i386_fbsd_mcregmap, mcontext_addr,
  143. I386_SIZEOF_MCONTEXT_T);
  144. /* Don't bother with floating point or XSAVE state for now. The
  145. current helper routines for parsing FXSAVE and XSAVE state only
  146. work with regcaches. This could perhaps create a temporary
  147. regcache, collect the register values from mc_fpstate and
  148. mc_xfpustate, and then set register values in the trad_frame. */
  149. trad_frame_set_id (this_cache, frame_id_build (sp, func));
  150. }
  151. static const struct tramp_frame i386_fbsd_sigframe =
  152. {
  153. SIGTRAMP_FRAME,
  154. 1,
  155. {
  156. {0x8d, ULONGEST_MAX}, /* lea SIGF_UC(%esp),%eax */
  157. {0x44, ULONGEST_MAX},
  158. {0x24, ULONGEST_MAX},
  159. {0x20, ULONGEST_MAX},
  160. {0x50, ULONGEST_MAX}, /* pushl %eax */
  161. {0xf7, ULONGEST_MAX}, /* testl $PSL_VM,UC_EFLAGS(%eax) */
  162. {0x40, ULONGEST_MAX},
  163. {0x54, ULONGEST_MAX},
  164. {0x00, ULONGEST_MAX},
  165. {0x00, ULONGEST_MAX},
  166. {0x02, ULONGEST_MAX},
  167. {0x00, ULONGEST_MAX},
  168. {0x75, ULONGEST_MAX}, /* jne +3 */
  169. {0x03, ULONGEST_MAX},
  170. {0x8e, ULONGEST_MAX}, /* mov UC_GS(%eax),%gs */
  171. {0x68, ULONGEST_MAX},
  172. {0x14, ULONGEST_MAX},
  173. {0xb8, ULONGEST_MAX}, /* movl $SYS_sigreturn,%eax */
  174. {0xa1, ULONGEST_MAX},
  175. {0x01, ULONGEST_MAX},
  176. {0x00, ULONGEST_MAX},
  177. {0x00, ULONGEST_MAX},
  178. {0x50, ULONGEST_MAX}, /* pushl %eax */
  179. {0xcd, ULONGEST_MAX}, /* int $0x80 */
  180. {0x80, ULONGEST_MAX},
  181. {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
  182. },
  183. i386_fbsd_sigframe_init
  184. };
  185. /* FreeBSD/i386 binaries running under an amd64 kernel use a different
  186. trampoline. This trampoline differs from the i386 kernel trampoline
  187. in that it omits a middle section that conditionally restores
  188. %gs. */
  189. static const struct tramp_frame i386_fbsd64_sigframe =
  190. {
  191. SIGTRAMP_FRAME,
  192. 1,
  193. {
  194. {0x8d, ULONGEST_MAX}, /* lea SIGF_UC(%esp),%eax */
  195. {0x44, ULONGEST_MAX},
  196. {0x24, ULONGEST_MAX},
  197. {0x20, ULONGEST_MAX},
  198. {0x50, ULONGEST_MAX}, /* pushl %eax */
  199. {0xb8, ULONGEST_MAX}, /* movl $SYS_sigreturn,%eax */
  200. {0xa1, ULONGEST_MAX},
  201. {0x01, ULONGEST_MAX},
  202. {0x00, ULONGEST_MAX},
  203. {0x00, ULONGEST_MAX},
  204. {0x50, ULONGEST_MAX}, /* pushl %eax */
  205. {0xcd, ULONGEST_MAX}, /* int $0x80 */
  206. {0x80, ULONGEST_MAX},
  207. {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
  208. },
  209. i386_fbsd_sigframe_init
  210. };
  211. /* Get XSAVE extended state xcr0 from core dump. */
  212. uint64_t
  213. i386fbsd_core_read_xcr0 (bfd *abfd)
  214. {
  215. asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate");
  216. uint64_t xcr0;
  217. if (xstate)
  218. {
  219. size_t size = bfd_section_size (xstate);
  220. /* Check extended state size. */
  221. if (size < X86_XSTATE_AVX_SIZE)
  222. xcr0 = X86_XSTATE_SSE_MASK;
  223. else
  224. {
  225. char contents[8];
  226. if (! bfd_get_section_contents (abfd, xstate, contents,
  227. I386_FBSD_XSAVE_XCR0_OFFSET,
  228. 8))
  229. {
  230. warning (_("Couldn't read `xcr0' bytes from "
  231. "`.reg-xstate' section in core file."));
  232. return X86_XSTATE_SSE_MASK;
  233. }
  234. xcr0 = bfd_get_64 (abfd, contents);
  235. }
  236. }
  237. else
  238. xcr0 = X86_XSTATE_SSE_MASK;
  239. return xcr0;
  240. }
  241. /* Implement the core_read_description gdbarch method. */
  242. static const struct target_desc *
  243. i386fbsd_core_read_description (struct gdbarch *gdbarch,
  244. struct target_ops *target,
  245. bfd *abfd)
  246. {
  247. return i386_target_description (i386fbsd_core_read_xcr0 (abfd), true);
  248. }
  249. /* Similar to i386_supply_fpregset, but use XSAVE extended state. */
  250. static void
  251. i386fbsd_supply_xstateregset (const struct regset *regset,
  252. struct regcache *regcache, int regnum,
  253. const void *xstateregs, size_t len)
  254. {
  255. i387_supply_xsave (regcache, regnum, xstateregs);
  256. }
  257. /* Similar to i386_collect_fpregset, but use XSAVE extended state. */
  258. static void
  259. i386fbsd_collect_xstateregset (const struct regset *regset,
  260. const struct regcache *regcache,
  261. int regnum, void *xstateregs, size_t len)
  262. {
  263. i387_collect_xsave (regcache, regnum, xstateregs, 1);
  264. }
  265. /* Register set definitions. */
  266. static const struct regset i386fbsd_xstateregset =
  267. {
  268. NULL,
  269. i386fbsd_supply_xstateregset,
  270. i386fbsd_collect_xstateregset
  271. };
  272. /* Iterate over core file register note sections. */
  273. static void
  274. i386fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  275. iterate_over_regset_sections_cb *cb,
  276. void *cb_data,
  277. const struct regcache *regcache)
  278. {
  279. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  280. cb (".reg", I386_FBSD_SIZEOF_GREGSET, I386_FBSD_SIZEOF_GREGSET,
  281. &i386_fbsd_gregset, NULL, cb_data);
  282. cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, &i386_fpregset,
  283. NULL, cb_data);
  284. cb (".reg-x86-segbases", I386_FBSD_SIZEOF_SEGBASES_REGSET,
  285. I386_FBSD_SIZEOF_SEGBASES_REGSET, &i386_fbsd_segbases_regset,
  286. "segment bases", cb_data);
  287. if (tdep->xcr0 & X86_XSTATE_AVX)
  288. cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0),
  289. X86_XSTATE_SIZE (tdep->xcr0), &i386fbsd_xstateregset,
  290. "XSAVE extended state", cb_data);
  291. }
  292. /* Implement the get_thread_local_address gdbarch method. */
  293. static CORE_ADDR
  294. i386fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
  295. CORE_ADDR lm_addr, CORE_ADDR offset)
  296. {
  297. struct regcache *regcache;
  298. regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
  299. ptid, gdbarch);
  300. target_fetch_registers (regcache, I386_GSBASE_REGNUM);
  301. ULONGEST gsbase;
  302. if (regcache->cooked_read (I386_GSBASE_REGNUM, &gsbase) != REG_VALID)
  303. error (_("Unable to fetch %%gsbase"));
  304. CORE_ADDR dtv_addr = gsbase + gdbarch_ptr_bit (gdbarch) / 8;
  305. return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
  306. }
  307. static void
  308. i386fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  309. {
  310. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  311. /* Generic FreeBSD support. */
  312. fbsd_init_abi (info, gdbarch);
  313. /* Obviously FreeBSD is BSD-based. */
  314. i386bsd_init_abi (info, gdbarch);
  315. /* FreeBSD reserves some space for its FPU emulator in
  316. `struct fpreg'. */
  317. tdep->sizeof_fpregset = 176;
  318. /* FreeBSD uses -freg-struct-return by default. */
  319. tdep->struct_return = reg_struct_return;
  320. tramp_frame_prepend_unwinder (gdbarch, &i386_fbsd_sigframe);
  321. tramp_frame_prepend_unwinder (gdbarch, &i386_fbsd64_sigframe);
  322. i386_elf_init_abi (info, gdbarch);
  323. tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET;
  324. /* Iterate over core file register note sections. */
  325. set_gdbarch_iterate_over_regset_sections
  326. (gdbarch, i386fbsd_iterate_over_regset_sections);
  327. set_gdbarch_core_read_description (gdbarch,
  328. i386fbsd_core_read_description);
  329. /* FreeBSD uses SVR4-style shared libraries. */
  330. set_solib_svr4_fetch_link_map_offsets
  331. (gdbarch, svr4_ilp32_fetch_link_map_offsets);
  332. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  333. svr4_fetch_objfile_link_map);
  334. set_gdbarch_get_thread_local_address (gdbarch,
  335. i386fbsd_get_thread_local_address);
  336. }
  337. void _initialize_i386fbsd_tdep ();
  338. void
  339. _initialize_i386fbsd_tdep ()
  340. {
  341. gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_FREEBSD,
  342. i386fbsd_init_abi);
  343. }