sparc-obsd-tdep.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Target-dependent code for OpenBSD/sparc.
  2. Copyright (C) 2004-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 "frame.h"
  16. #include "frame-unwind.h"
  17. #include "gdbcore.h"
  18. #include "osabi.h"
  19. #include "regcache.h"
  20. #include "symtab.h"
  21. #include "trad-frame.h"
  22. #include "inferior.h"
  23. #include "obsd-tdep.h"
  24. #include "sparc-tdep.h"
  25. #include "solib-svr4.h"
  26. #include "bsd-uthread.h"
  27. #include "gdbarch.h"
  28. /* Signal trampolines. */
  29. /* The OpenBSD kernel maps the signal trampoline at some random
  30. location in user space, which means that the traditional BSD way of
  31. detecting it won't work.
  32. The signal trampoline will be mapped at an address that is page
  33. aligned. We recognize the signal trampoline by looking for the
  34. sigreturn system call. */
  35. static const int sparc32obsd_page_size = 4096;
  36. static int
  37. sparc32obsd_pc_in_sigtramp (CORE_ADDR pc, const char *name)
  38. {
  39. CORE_ADDR start_pc = (pc & ~(sparc32obsd_page_size - 1));
  40. unsigned long insn;
  41. if (name)
  42. return 0;
  43. /* Check for "restore %g0, SYS_sigreturn, %g1". */
  44. insn = sparc_fetch_instruction (start_pc + 0xec);
  45. if (insn != 0x83e82067)
  46. return 0;
  47. /* Check for "t ST_SYSCALL". */
  48. insn = sparc_fetch_instruction (start_pc + 0xf4);
  49. if (insn != 0x91d02000)
  50. return 0;
  51. return 1;
  52. }
  53. static struct sparc_frame_cache *
  54. sparc32obsd_sigtramp_frame_cache (struct frame_info *this_frame,
  55. void **this_cache)
  56. {
  57. struct sparc_frame_cache *cache;
  58. CORE_ADDR addr;
  59. if (*this_cache)
  60. return (struct sparc_frame_cache *) *this_cache;
  61. cache = sparc_frame_cache (this_frame, this_cache);
  62. gdb_assert (cache == *this_cache);
  63. /* If we couldn't find the frame's function, we're probably dealing
  64. with an on-stack signal trampoline. */
  65. if (cache->pc == 0)
  66. {
  67. cache->pc = get_frame_pc (this_frame);
  68. cache->pc &= ~(sparc32obsd_page_size - 1);
  69. /* Since we couldn't find the frame's function, the cache was
  70. initialized under the assumption that we're frameless. */
  71. sparc_record_save_insn (cache);
  72. addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  73. cache->base = addr;
  74. }
  75. cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (this_frame);
  76. return cache;
  77. }
  78. static void
  79. sparc32obsd_sigtramp_frame_this_id (struct frame_info *this_frame,
  80. void **this_cache,
  81. struct frame_id *this_id)
  82. {
  83. struct sparc_frame_cache *cache =
  84. sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);
  85. (*this_id) = frame_id_build (cache->base, cache->pc);
  86. }
  87. static struct value *
  88. sparc32obsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
  89. void **this_cache, int regnum)
  90. {
  91. struct sparc_frame_cache *cache =
  92. sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);
  93. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  94. }
  95. static int
  96. sparc32obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
  97. struct frame_info *this_frame,
  98. void **this_cache)
  99. {
  100. CORE_ADDR pc = get_frame_pc (this_frame);
  101. const char *name;
  102. find_pc_partial_function (pc, &name, NULL, NULL);
  103. if (sparc32obsd_pc_in_sigtramp (pc, name))
  104. return 1;
  105. return 0;
  106. }
  107. static const struct frame_unwind sparc32obsd_sigtramp_frame_unwind =
  108. {
  109. "sparc32 openbsd sigtramp",
  110. SIGTRAMP_FRAME,
  111. default_frame_unwind_stop_reason,
  112. sparc32obsd_sigtramp_frame_this_id,
  113. sparc32obsd_sigtramp_frame_prev_register,
  114. NULL,
  115. sparc32obsd_sigtramp_frame_sniffer
  116. };
  117. /* Offset wthin the thread structure where we can find %fp and %i7. */
  118. #define SPARC32OBSD_UTHREAD_FP_OFFSET 128
  119. #define SPARC32OBSD_UTHREAD_PC_OFFSET 132
  120. static void
  121. sparc32obsd_supply_uthread (struct regcache *regcache,
  122. int regnum, CORE_ADDR addr)
  123. {
  124. struct gdbarch *gdbarch = regcache->arch ();
  125. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  126. CORE_ADDR fp, fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
  127. gdb_byte buf[4];
  128. /* This function calls functions that depend on the global current thread. */
  129. gdb_assert (regcache->ptid () == inferior_ptid);
  130. gdb_assert (regnum >= -1);
  131. fp = read_memory_unsigned_integer (fp_addr, 4, byte_order);
  132. if (regnum == SPARC_SP_REGNUM || regnum == -1)
  133. {
  134. store_unsigned_integer (buf, 4, byte_order, fp);
  135. regcache->raw_supply (SPARC_SP_REGNUM, buf);
  136. if (regnum == SPARC_SP_REGNUM)
  137. return;
  138. }
  139. if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM
  140. || regnum == -1)
  141. {
  142. CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
  143. i7 = read_memory_unsigned_integer (i7_addr, 4, byte_order);
  144. if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  145. {
  146. store_unsigned_integer (buf, 4, byte_order, i7 + 8);
  147. regcache->raw_supply (SPARC32_PC_REGNUM, buf);
  148. }
  149. if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  150. {
  151. store_unsigned_integer (buf, 4, byte_order, i7 + 12);
  152. regcache->raw_supply (SPARC32_NPC_REGNUM, buf);
  153. }
  154. if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
  155. return;
  156. }
  157. sparc_supply_rwindow (regcache, fp, regnum);
  158. }
  159. static void
  160. sparc32obsd_collect_uthread(const struct regcache *regcache,
  161. int regnum, CORE_ADDR addr)
  162. {
  163. struct gdbarch *gdbarch = regcache->arch ();
  164. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  165. CORE_ADDR sp;
  166. gdb_byte buf[4];
  167. /* This function calls functions that depend on the global current thread. */
  168. gdb_assert (regcache->ptid () == inferior_ptid);
  169. gdb_assert (regnum >= -1);
  170. if (regnum == SPARC_SP_REGNUM || regnum == -1)
  171. {
  172. CORE_ADDR fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
  173. regcache->raw_collect (SPARC_SP_REGNUM, buf);
  174. write_memory (fp_addr,buf, 4);
  175. }
  176. if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  177. {
  178. CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
  179. regcache->raw_collect (SPARC32_PC_REGNUM, buf);
  180. i7 = extract_unsigned_integer (buf, 4, byte_order) - 8;
  181. write_memory_unsigned_integer (i7_addr, 4, byte_order, i7);
  182. if (regnum == SPARC32_PC_REGNUM)
  183. return;
  184. }
  185. regcache->raw_collect (SPARC_SP_REGNUM, buf);
  186. sp = extract_unsigned_integer (buf, 4, byte_order);
  187. sparc_collect_rwindow (regcache, sp, regnum);
  188. }
  189. static void
  190. sparc32obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  191. {
  192. /* OpenBSD/sparc is very similar to NetBSD/sparc ELF. */
  193. sparc32nbsd_init_abi (info, gdbarch);
  194. set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);
  195. frame_unwind_append_unwinder (gdbarch, &sparc32obsd_sigtramp_frame_unwind);
  196. /* OpenBSD provides a user-level threads implementation. */
  197. bsd_uthread_set_supply_uthread (gdbarch, sparc32obsd_supply_uthread);
  198. bsd_uthread_set_collect_uthread (gdbarch, sparc32obsd_collect_uthread);
  199. }
  200. void _initialize_sparc32obsd_tdep ();
  201. void
  202. _initialize_sparc32obsd_tdep ()
  203. {
  204. gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_OPENBSD,
  205. sparc32obsd_init_abi);
  206. }