arm-fbsd-tdep.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Target-dependent code for FreeBSD/arm.
  2. Copyright (C) 2017-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 "elf/common.h"
  16. #include "target-descriptions.h"
  17. #include "aarch32-tdep.h"
  18. #include "arm-tdep.h"
  19. #include "arm-fbsd-tdep.h"
  20. #include "auxv.h"
  21. #include "fbsd-tdep.h"
  22. #include "gdbcore.h"
  23. #include "osabi.h"
  24. #include "solib-svr4.h"
  25. #include "trad-frame.h"
  26. #include "tramp-frame.h"
  27. /* Register maps. */
  28. static const struct regcache_map_entry arm_fbsd_gregmap[] =
  29. {
  30. { 13, ARM_A1_REGNUM, 4 }, /* r0 ... r12 */
  31. { 1, ARM_SP_REGNUM, 4 },
  32. { 1, ARM_LR_REGNUM, 4 },
  33. { 1, ARM_PC_REGNUM, 4 },
  34. { 1, ARM_PS_REGNUM, 4 },
  35. { 0 }
  36. };
  37. static const struct regcache_map_entry arm_fbsd_vfpregmap[] =
  38. {
  39. { 32, ARM_D0_REGNUM, 8 }, /* d0 ... d31 */
  40. { 1, ARM_FPSCR_REGNUM, 4 },
  41. { 0 }
  42. };
  43. /* In a signal frame, sp points to a 'struct sigframe' which is
  44. defined as:
  45. struct sigframe {
  46. siginfo_t sf_si;
  47. ucontext_t sf_uc;
  48. mcontext_vfp_t sf_vfp;
  49. };
  50. ucontext_t is defined as:
  51. struct __ucontext {
  52. sigset_t uc_sigmask;
  53. mcontext_t uc_mcontext;
  54. ...
  55. };
  56. mcontext_t is defined as:
  57. struct {
  58. unsigned int __gregs[17];
  59. size_t mc_vfp_size;
  60. void *mc_vfp_ptr;
  61. ...
  62. };
  63. mcontext_vfp_t is defined as:
  64. struct {
  65. uint64_t mcv_reg[32];
  66. uint32_t mcv_fpscr;
  67. };
  68. If the VFP state is valid, then mc_vfp_ptr will point to sf_vfp in
  69. the sigframe, otherwise it is NULL. There is no non-VFP floating
  70. point register state saved in the signal frame. */
  71. #define ARM_SIGFRAME_UCONTEXT_OFFSET 64
  72. #define ARM_UCONTEXT_MCONTEXT_OFFSET 16
  73. #define ARM_MCONTEXT_VFP_PTR_OFFSET 72
  74. /* Implement the "init" method of struct tramp_frame. */
  75. static void
  76. arm_fbsd_sigframe_init (const struct tramp_frame *self,
  77. struct frame_info *this_frame,
  78. struct trad_frame_cache *this_cache,
  79. CORE_ADDR func)
  80. {
  81. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  82. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  83. CORE_ADDR sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
  84. CORE_ADDR mcontext_addr = (sp
  85. + ARM_SIGFRAME_UCONTEXT_OFFSET
  86. + ARM_UCONTEXT_MCONTEXT_OFFSET);
  87. ULONGEST mcontext_vfp_addr;
  88. trad_frame_set_reg_regmap (this_cache, arm_fbsd_gregmap, mcontext_addr,
  89. regcache_map_entry_size (arm_fbsd_gregmap));
  90. if (safe_read_memory_unsigned_integer (mcontext_addr
  91. + ARM_MCONTEXT_VFP_PTR_OFFSET, 4,
  92. byte_order,
  93. &mcontext_vfp_addr)
  94. && mcontext_vfp_addr != 0)
  95. trad_frame_set_reg_regmap (this_cache, arm_fbsd_vfpregmap, mcontext_vfp_addr,
  96. regcache_map_entry_size (arm_fbsd_vfpregmap));
  97. trad_frame_set_id (this_cache, frame_id_build (sp, func));
  98. }
  99. static const struct tramp_frame arm_fbsd_sigframe =
  100. {
  101. SIGTRAMP_FRAME,
  102. 4,
  103. {
  104. {0xe1a0000d, ULONGEST_MAX}, /* mov r0, sp */
  105. {0xe2800040, ULONGEST_MAX}, /* add r0, r0, #SIGF_UC */
  106. {0xe59f700c, ULONGEST_MAX}, /* ldr r7, [pc, #12] */
  107. {0xef0001a1, ULONGEST_MAX}, /* swi SYS_sigreturn */
  108. {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
  109. },
  110. arm_fbsd_sigframe_init
  111. };
  112. /* Register set definitions. */
  113. const struct regset arm_fbsd_gregset =
  114. {
  115. arm_fbsd_gregmap,
  116. regcache_supply_regset, regcache_collect_regset
  117. };
  118. const struct regset arm_fbsd_vfpregset =
  119. {
  120. arm_fbsd_vfpregmap,
  121. regcache_supply_regset, regcache_collect_regset
  122. };
  123. /* Implement the "iterate_over_regset_sections" gdbarch method. */
  124. static void
  125. arm_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  126. iterate_over_regset_sections_cb *cb,
  127. void *cb_data,
  128. const struct regcache *regcache)
  129. {
  130. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  131. cb (".reg", ARM_FBSD_SIZEOF_GREGSET, ARM_FBSD_SIZEOF_GREGSET,
  132. &arm_fbsd_gregset, NULL, cb_data);
  133. /* While FreeBSD/arm cores do contain a NT_FPREGSET / ".reg2"
  134. register set, it is not populated with register values by the
  135. kernel but just contains all zeroes. */
  136. if (tdep->vfp_register_count > 0)
  137. cb (".reg-arm-vfp", ARM_FBSD_SIZEOF_VFPREGSET, ARM_FBSD_SIZEOF_VFPREGSET,
  138. &arm_fbsd_vfpregset, "VFP floating-point", cb_data);
  139. }
  140. /* Lookup a target description from a target's AT_HWCAP auxiliary
  141. vector. */
  142. const struct target_desc *
  143. arm_fbsd_read_description_auxv (struct target_ops *target)
  144. {
  145. CORE_ADDR arm_hwcap = 0;
  146. if (target_auxv_search (target, AT_FREEBSD_HWCAP, &arm_hwcap) != 1)
  147. return nullptr;
  148. if (arm_hwcap & HWCAP_VFP)
  149. {
  150. if (arm_hwcap & HWCAP_NEON)
  151. return aarch32_read_description ();
  152. else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPD32))
  153. == (HWCAP_VFPv3 | HWCAP_VFPD32))
  154. return arm_read_description (ARM_FP_TYPE_VFPV3);
  155. else
  156. return arm_read_description (ARM_FP_TYPE_VFPV2);
  157. }
  158. return nullptr;
  159. }
  160. /* Implement the "core_read_description" gdbarch method. */
  161. static const struct target_desc *
  162. arm_fbsd_core_read_description (struct gdbarch *gdbarch,
  163. struct target_ops *target,
  164. bfd *abfd)
  165. {
  166. return arm_fbsd_read_description_auxv (target);
  167. }
  168. /* Implement the 'init_osabi' method of struct gdb_osabi_handler. */
  169. static void
  170. arm_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  171. {
  172. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  173. /* Generic FreeBSD support. */
  174. fbsd_init_abi (info, gdbarch);
  175. if (tdep->fp_model == ARM_FLOAT_AUTO)
  176. tdep->fp_model = ARM_FLOAT_SOFT_VFP;
  177. tramp_frame_prepend_unwinder (gdbarch, &arm_fbsd_sigframe);
  178. set_solib_svr4_fetch_link_map_offsets
  179. (gdbarch, svr4_ilp32_fetch_link_map_offsets);
  180. tdep->jb_pc = 24;
  181. tdep->jb_elt_size = 4;
  182. set_gdbarch_iterate_over_regset_sections
  183. (gdbarch, arm_fbsd_iterate_over_regset_sections);
  184. set_gdbarch_core_read_description (gdbarch, arm_fbsd_core_read_description);
  185. /* Single stepping. */
  186. set_gdbarch_software_single_step (gdbarch, arm_software_single_step);
  187. }
  188. void _initialize_arm_fbsd_tdep ();
  189. void
  190. _initialize_arm_fbsd_tdep ()
  191. {
  192. gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_FREEBSD,
  193. arm_fbsd_init_abi);
  194. }