riscv-fbsd-tdep.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Target-dependent code for FreeBSD on RISC-V processors.
  2. Copyright (C) 2018-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 "fbsd-tdep.h"
  16. #include "osabi.h"
  17. #include "riscv-tdep.h"
  18. #include "riscv-fbsd-tdep.h"
  19. #include "solib-svr4.h"
  20. #include "target.h"
  21. #include "trad-frame.h"
  22. #include "tramp-frame.h"
  23. #include "gdbarch.h"
  24. #include "inferior.h"
  25. /* Register maps. */
  26. static const struct regcache_map_entry riscv_fbsd_gregmap[] =
  27. {
  28. { 1, RISCV_RA_REGNUM, 0 },
  29. { 1, RISCV_SP_REGNUM, 0 },
  30. { 1, RISCV_GP_REGNUM, 0 },
  31. { 1, RISCV_TP_REGNUM, 0 },
  32. { 3, 5, 0 }, /* t0 - t2 */
  33. { 4, 28, 0 }, /* t3 - t6 */
  34. { 2, RISCV_FP_REGNUM, 0 }, /* s0 - s1 */
  35. { 10, 18, 0 }, /* s2 - s11 */
  36. { 8, RISCV_A0_REGNUM, 0 }, /* a0 - a7 */
  37. { 1, RISCV_PC_REGNUM, 0 },
  38. { 1, RISCV_CSR_SSTATUS_REGNUM, 0 },
  39. { 0 }
  40. };
  41. static const struct regcache_map_entry riscv_fbsd_fpregmap[] =
  42. {
  43. { 32, RISCV_FIRST_FP_REGNUM, 16 },
  44. { 1, RISCV_CSR_FCSR_REGNUM, 8 },
  45. { 0 }
  46. };
  47. /* Register set definitions. */
  48. const struct regset riscv_fbsd_gregset =
  49. {
  50. riscv_fbsd_gregmap, riscv_supply_regset, regcache_collect_regset
  51. };
  52. const struct regset riscv_fbsd_fpregset =
  53. {
  54. riscv_fbsd_fpregmap, riscv_supply_regset, regcache_collect_regset
  55. };
  56. /* Implement the "iterate_over_regset_sections" gdbarch method. */
  57. static void
  58. riscv_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  59. iterate_over_regset_sections_cb *cb,
  60. void *cb_data,
  61. const struct regcache *regcache)
  62. {
  63. cb (".reg", RISCV_FBSD_NUM_GREGS * riscv_isa_xlen (gdbarch),
  64. RISCV_FBSD_NUM_GREGS * riscv_isa_xlen (gdbarch),
  65. &riscv_fbsd_gregset, NULL, cb_data);
  66. cb (".reg2", RISCV_FBSD_SIZEOF_FPREGSET, RISCV_FBSD_SIZEOF_FPREGSET,
  67. &riscv_fbsd_fpregset, NULL, cb_data);
  68. }
  69. /* In a signal frame, sp points to a 'struct sigframe' which is
  70. defined as:
  71. struct sigframe {
  72. siginfo_t sf_si;
  73. ucontext_t sf_uc;
  74. };
  75. ucontext_t is defined as:
  76. struct __ucontext {
  77. sigset_t uc_sigmask;
  78. mcontext_t uc_mcontext;
  79. ...
  80. };
  81. The mcontext_t contains the general purpose register set followed
  82. by the floating point register set. The floating point register
  83. set is only valid if the _MC_FP_VALID flag is set in mc_flags. */
  84. #define RISCV_SIGFRAME_UCONTEXT_OFFSET 80
  85. #define RISCV_UCONTEXT_MCONTEXT_OFFSET 16
  86. #define RISCV_MCONTEXT_FLAG_FP_VALID 0x1
  87. /* Implement the "init" method of struct tramp_frame. */
  88. static void
  89. riscv_fbsd_sigframe_init (const struct tramp_frame *self,
  90. struct frame_info *this_frame,
  91. struct trad_frame_cache *this_cache,
  92. CORE_ADDR func)
  93. {
  94. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  95. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  96. CORE_ADDR sp = get_frame_register_unsigned (this_frame, RISCV_SP_REGNUM);
  97. CORE_ADDR mcontext_addr
  98. = (sp
  99. + RISCV_SIGFRAME_UCONTEXT_OFFSET
  100. + RISCV_UCONTEXT_MCONTEXT_OFFSET);
  101. gdb_byte buf[4];
  102. trad_frame_set_reg_regmap (this_cache, riscv_fbsd_gregmap, mcontext_addr,
  103. RISCV_FBSD_NUM_GREGS * riscv_isa_xlen (gdbarch));
  104. CORE_ADDR fpregs_addr
  105. = mcontext_addr + RISCV_FBSD_NUM_GREGS * riscv_isa_xlen (gdbarch);
  106. CORE_ADDR fp_flags_addr
  107. = fpregs_addr + RISCV_FBSD_SIZEOF_FPREGSET;
  108. if (target_read_memory (fp_flags_addr, buf, 4) == 0
  109. && (extract_unsigned_integer (buf, 4, byte_order)
  110. & RISCV_MCONTEXT_FLAG_FP_VALID))
  111. trad_frame_set_reg_regmap (this_cache, riscv_fbsd_fpregmap, fpregs_addr,
  112. RISCV_FBSD_SIZEOF_FPREGSET);
  113. trad_frame_set_id (this_cache, frame_id_build (sp, func));
  114. }
  115. /* RISC-V supports 16-bit instructions ("C") as well as 32-bit
  116. instructions. The signal trampoline on FreeBSD uses a mix of
  117. these, but tramp_frame assumes a fixed instruction size. To cope,
  118. claim that all instructions are 16 bits and use two "slots" for
  119. 32-bit instructions. */
  120. static const struct tramp_frame riscv_fbsd_sigframe =
  121. {
  122. SIGTRAMP_FRAME,
  123. 2,
  124. {
  125. {0x850a, ULONGEST_MAX}, /* mov a0, sp */
  126. {0x0513, ULONGEST_MAX}, /* addi a0, a0, #SF_UC */
  127. {0x0505, ULONGEST_MAX},
  128. {0x0293, ULONGEST_MAX}, /* li t0, #SYS_sigreturn */
  129. {0x1a10, ULONGEST_MAX},
  130. {0x0073, ULONGEST_MAX}, /* ecall */
  131. {0x0000, ULONGEST_MAX},
  132. {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
  133. },
  134. riscv_fbsd_sigframe_init
  135. };
  136. /* Implement the "get_thread_local_address" gdbarch method. */
  137. static CORE_ADDR
  138. riscv_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
  139. CORE_ADDR lm_addr, CORE_ADDR offset)
  140. {
  141. struct regcache *regcache;
  142. regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
  143. ptid, gdbarch);
  144. target_fetch_registers (regcache, RISCV_TP_REGNUM);
  145. ULONGEST tp;
  146. if (regcache->cooked_read (RISCV_TP_REGNUM, &tp) != REG_VALID)
  147. error (_("Unable to fetch %%tp"));
  148. /* %tp points to the end of the TCB which contains two pointers.
  149. The first pointer in the TCB points to the DTV array. */
  150. CORE_ADDR dtv_addr = tp - (gdbarch_ptr_bit (gdbarch) / 8) * 2;
  151. return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
  152. }
  153. /* Implement the 'init_osabi' method of struct gdb_osabi_handler. */
  154. static void
  155. riscv_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  156. {
  157. /* Generic FreeBSD support. */
  158. fbsd_init_abi (info, gdbarch);
  159. set_gdbarch_software_single_step (gdbarch, riscv_software_single_step);
  160. set_solib_svr4_fetch_link_map_offsets (gdbarch,
  161. (riscv_isa_xlen (gdbarch) == 4
  162. ? svr4_ilp32_fetch_link_map_offsets
  163. : svr4_lp64_fetch_link_map_offsets));
  164. tramp_frame_prepend_unwinder (gdbarch, &riscv_fbsd_sigframe);
  165. set_gdbarch_iterate_over_regset_sections
  166. (gdbarch, riscv_fbsd_iterate_over_regset_sections);
  167. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  168. svr4_fetch_objfile_link_map);
  169. set_gdbarch_get_thread_local_address (gdbarch,
  170. riscv_fbsd_get_thread_local_address);
  171. }
  172. void _initialize_riscv_fbsd_tdep ();
  173. void
  174. _initialize_riscv_fbsd_tdep ()
  175. {
  176. gdbarch_register_osabi (bfd_arch_riscv, 0, GDB_OSABI_FREEBSD,
  177. riscv_fbsd_init_abi);
  178. }