sparc64-obsd-tdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* Target-dependent code for OpenBSD/sparc64.
  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 "regset.h"
  21. #include "symtab.h"
  22. #include "objfiles.h"
  23. #include "trad-frame.h"
  24. #include "inferior.h"
  25. #include "obsd-tdep.h"
  26. #include "sparc64-tdep.h"
  27. #include "solib-svr4.h"
  28. #include "bsd-uthread.h"
  29. /* Older OpenBSD versions used the traditional NetBSD core file
  30. format, even for ports that use ELF. These core files don't use
  31. multiple register sets. Instead, the general-purpose and
  32. floating-point registers are lumped together in a single section.
  33. Unlike on NetBSD, OpenBSD uses a different layout for its
  34. general-purpose registers than the layout used for ptrace(2).
  35. Newer OpenBSD versions use ELF core files. Here the register sets
  36. match the ptrace(2) layout. */
  37. /* From <machine/reg.h>. */
  38. const struct sparc_gregmap sparc64obsd_gregmap =
  39. {
  40. 0 * 8, /* "tstate" */
  41. 1 * 8, /* %pc */
  42. 2 * 8, /* %npc */
  43. 3 * 8, /* %y */
  44. -1, /* %fprs */
  45. -1,
  46. 5 * 8, /* %g1 */
  47. 20 * 8, /* %l0 */
  48. 4 /* sizeof (%y) */
  49. };
  50. const struct sparc_gregmap sparc64obsd_core_gregmap =
  51. {
  52. 0 * 8, /* "tstate" */
  53. 1 * 8, /* %pc */
  54. 2 * 8, /* %npc */
  55. 3 * 8, /* %y */
  56. -1, /* %fprs */
  57. -1,
  58. 7 * 8, /* %g1 */
  59. 22 * 8, /* %l0 */
  60. 4 /* sizeof (%y) */
  61. };
  62. static void
  63. sparc64obsd_supply_gregset (const struct regset *regset,
  64. struct regcache *regcache,
  65. int regnum, const void *gregs, size_t len)
  66. {
  67. const void *fpregs = (char *)gregs + 288;
  68. if (len < 832)
  69. {
  70. sparc64_supply_gregset (&sparc64obsd_gregmap, regcache, regnum, gregs);
  71. return;
  72. }
  73. sparc64_supply_gregset (&sparc64obsd_core_gregmap, regcache, regnum, gregs);
  74. sparc64_supply_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs);
  75. }
  76. static void
  77. sparc64obsd_supply_fpregset (const struct regset *regset,
  78. struct regcache *regcache,
  79. int regnum, const void *fpregs, size_t len)
  80. {
  81. sparc64_supply_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs);
  82. }
  83. /* Signal trampolines. */
  84. /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
  85. in virtual memory. The randomness makes it somewhat tricky to
  86. detect it, but fortunately we can rely on the fact that the start
  87. of the sigtramp routine is page-aligned. We recognize the
  88. trampoline by looking for the code that invokes the sigreturn
  89. system call. The offset where we can find that code varies from
  90. release to release.
  91. By the way, the mapping mentioned above is read-only, so you cannot
  92. place a breakpoint in the signal trampoline. */
  93. /* Default page size. */
  94. static const int sparc64obsd_page_size = 8192;
  95. /* Offset for sigreturn(2). */
  96. static const int sparc64obsd_sigreturn_offset[] = {
  97. 0xf0, /* OpenBSD 3.8 */
  98. 0xec, /* OpenBSD 3.6 */
  99. 0xe8, /* OpenBSD 3.2 */
  100. -1
  101. };
  102. static int
  103. sparc64obsd_pc_in_sigtramp (CORE_ADDR pc, const char *name)
  104. {
  105. CORE_ADDR start_pc = (pc & ~(sparc64obsd_page_size - 1));
  106. unsigned long insn;
  107. const int *offset;
  108. if (name)
  109. return 0;
  110. for (offset = sparc64obsd_sigreturn_offset; *offset != -1; offset++)
  111. {
  112. /* Check for "restore %g0, SYS_sigreturn, %g1". */
  113. insn = sparc_fetch_instruction (start_pc + *offset);
  114. if (insn != 0x83e82067)
  115. continue;
  116. /* Check for "t ST_SYSCALL". */
  117. insn = sparc_fetch_instruction (start_pc + *offset + 8);
  118. if (insn != 0x91d02000)
  119. continue;
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. static struct sparc_frame_cache *
  125. sparc64obsd_frame_cache (struct frame_info *this_frame, void **this_cache)
  126. {
  127. struct sparc_frame_cache *cache;
  128. CORE_ADDR addr;
  129. if (*this_cache)
  130. return (struct sparc_frame_cache *) *this_cache;
  131. cache = sparc_frame_cache (this_frame, this_cache);
  132. gdb_assert (cache == *this_cache);
  133. /* If we couldn't find the frame's function, we're probably dealing
  134. with an on-stack signal trampoline. */
  135. if (cache->pc == 0)
  136. {
  137. cache->pc = get_frame_pc (this_frame);
  138. cache->pc &= ~(sparc64obsd_page_size - 1);
  139. /* Since we couldn't find the frame's function, the cache was
  140. initialized under the assumption that we're frameless. */
  141. sparc_record_save_insn (cache);
  142. addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  143. if (addr & 1)
  144. addr += BIAS;
  145. cache->base = addr;
  146. }
  147. /* We find the appropriate instance of `struct sigcontext' at a
  148. fixed offset in the signal frame. */
  149. addr = cache->base + 128 + 16;
  150. cache->saved_regs = sparc64nbsd_sigcontext_saved_regs (addr, this_frame);
  151. return cache;
  152. }
  153. static void
  154. sparc64obsd_frame_this_id (struct frame_info *this_frame, void **this_cache,
  155. struct frame_id *this_id)
  156. {
  157. struct sparc_frame_cache *cache =
  158. sparc64obsd_frame_cache (this_frame, this_cache);
  159. (*this_id) = frame_id_build (cache->base, cache->pc);
  160. }
  161. static struct value *
  162. sparc64obsd_frame_prev_register (struct frame_info *this_frame,
  163. void **this_cache, int regnum)
  164. {
  165. struct sparc_frame_cache *cache =
  166. sparc64obsd_frame_cache (this_frame, this_cache);
  167. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  168. }
  169. static int
  170. sparc64obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
  171. struct frame_info *this_frame,
  172. void **this_cache)
  173. {
  174. CORE_ADDR pc = get_frame_pc (this_frame);
  175. const char *name;
  176. find_pc_partial_function (pc, &name, NULL, NULL);
  177. if (sparc64obsd_pc_in_sigtramp (pc, name))
  178. return 1;
  179. return 0;
  180. }
  181. static const struct frame_unwind sparc64obsd_frame_unwind =
  182. {
  183. "sparc64 openbsd sigtramp",
  184. SIGTRAMP_FRAME,
  185. default_frame_unwind_stop_reason,
  186. sparc64obsd_frame_this_id,
  187. sparc64obsd_frame_prev_register,
  188. NULL,
  189. sparc64obsd_sigtramp_frame_sniffer
  190. };
  191. /* Kernel debugging support. */
  192. static struct sparc_frame_cache *
  193. sparc64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
  194. {
  195. struct sparc_frame_cache *cache;
  196. CORE_ADDR sp, trapframe_addr;
  197. int regnum;
  198. if (*this_cache)
  199. return (struct sparc_frame_cache *) *this_cache;
  200. cache = sparc_frame_cache (this_frame, this_cache);
  201. gdb_assert (cache == *this_cache);
  202. sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  203. trapframe_addr = sp + BIAS + 176;
  204. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  205. cache->saved_regs[SPARC64_STATE_REGNUM].set_addr (trapframe_addr);
  206. cache->saved_regs[SPARC64_PC_REGNUM].set_addr (trapframe_addr + 8);
  207. cache->saved_regs[SPARC64_NPC_REGNUM].set_addr (trapframe_addr + 16);
  208. for (regnum = SPARC_G0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++)
  209. cache->saved_regs[regnum].set_addr (trapframe_addr + 48
  210. + (regnum - SPARC_G0_REGNUM) * 8);
  211. return cache;
  212. }
  213. static void
  214. sparc64obsd_trapframe_this_id (struct frame_info *this_frame,
  215. void **this_cache, struct frame_id *this_id)
  216. {
  217. struct sparc_frame_cache *cache =
  218. sparc64obsd_trapframe_cache (this_frame, this_cache);
  219. (*this_id) = frame_id_build (cache->base, cache->pc);
  220. }
  221. static struct value *
  222. sparc64obsd_trapframe_prev_register (struct frame_info *this_frame,
  223. void **this_cache, int regnum)
  224. {
  225. struct sparc_frame_cache *cache =
  226. sparc64obsd_trapframe_cache (this_frame, this_cache);
  227. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  228. }
  229. static int
  230. sparc64obsd_trapframe_sniffer (const struct frame_unwind *self,
  231. struct frame_info *this_frame,
  232. void **this_cache)
  233. {
  234. CORE_ADDR pc;
  235. ULONGEST pstate;
  236. const char *name;
  237. /* Check whether we are in privileged mode, and bail out if we're not. */
  238. pstate = get_frame_register_unsigned (this_frame, SPARC64_PSTATE_REGNUM);
  239. if ((pstate & SPARC64_PSTATE_PRIV) == 0)
  240. return 0;
  241. pc = get_frame_address_in_block (this_frame);
  242. find_pc_partial_function (pc, &name, NULL, NULL);
  243. if (name && strcmp (name, "Lslowtrap_reenter") == 0)
  244. return 1;
  245. return 0;
  246. }
  247. static const struct frame_unwind sparc64obsd_trapframe_unwind =
  248. {
  249. "sparc64 openbsd trap",
  250. NORMAL_FRAME,
  251. default_frame_unwind_stop_reason,
  252. sparc64obsd_trapframe_this_id,
  253. sparc64obsd_trapframe_prev_register,
  254. NULL,
  255. sparc64obsd_trapframe_sniffer
  256. };
  257. /* Threads support. */
  258. /* Offset wthin the thread structure where we can find %fp and %i7. */
  259. #define SPARC64OBSD_UTHREAD_FP_OFFSET 232
  260. #define SPARC64OBSD_UTHREAD_PC_OFFSET 240
  261. static void
  262. sparc64obsd_supply_uthread (struct regcache *regcache,
  263. int regnum, CORE_ADDR addr)
  264. {
  265. struct gdbarch *gdbarch = regcache->arch ();
  266. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  267. CORE_ADDR fp, fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;
  268. gdb_byte buf[8];
  269. /* This function calls functions that depend on the global current thread. */
  270. gdb_assert (regcache->ptid () == inferior_ptid);
  271. gdb_assert (regnum >= -1);
  272. fp = read_memory_unsigned_integer (fp_addr, 8, byte_order);
  273. if (regnum == SPARC_SP_REGNUM || regnum == -1)
  274. {
  275. store_unsigned_integer (buf, 8, byte_order, fp);
  276. regcache->raw_supply (SPARC_SP_REGNUM, buf);
  277. if (regnum == SPARC_SP_REGNUM)
  278. return;
  279. }
  280. if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM
  281. || regnum == -1)
  282. {
  283. CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET;
  284. i7 = read_memory_unsigned_integer (i7_addr, 8, byte_order);
  285. if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  286. {
  287. store_unsigned_integer (buf, 8, byte_order, i7 + 8);
  288. regcache->raw_supply (SPARC64_PC_REGNUM, buf);
  289. }
  290. if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
  291. {
  292. store_unsigned_integer (buf, 8, byte_order, i7 + 12);
  293. regcache->raw_supply (SPARC64_NPC_REGNUM, buf);
  294. }
  295. if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
  296. return;
  297. }
  298. sparc_supply_rwindow (regcache, fp, regnum);
  299. }
  300. static void
  301. sparc64obsd_collect_uthread(const struct regcache *regcache,
  302. int regnum, CORE_ADDR addr)
  303. {
  304. struct gdbarch *gdbarch = regcache->arch ();
  305. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  306. CORE_ADDR sp;
  307. gdb_byte buf[8];
  308. /* This function calls functions that depend on the global current thread. */
  309. gdb_assert (regcache->ptid () == inferior_ptid);
  310. gdb_assert (regnum >= -1);
  311. if (regnum == SPARC_SP_REGNUM || regnum == -1)
  312. {
  313. CORE_ADDR fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;
  314. regcache->raw_collect (SPARC_SP_REGNUM, buf);
  315. write_memory (fp_addr,buf, 8);
  316. }
  317. if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  318. {
  319. CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET;
  320. regcache->raw_collect (SPARC64_PC_REGNUM, buf);
  321. i7 = extract_unsigned_integer (buf, 8, byte_order) - 8;
  322. write_memory_unsigned_integer (i7_addr, 8, byte_order, i7);
  323. if (regnum == SPARC64_PC_REGNUM)
  324. return;
  325. }
  326. regcache->raw_collect (SPARC_SP_REGNUM, buf);
  327. sp = extract_unsigned_integer (buf, 8, byte_order);
  328. sparc_collect_rwindow (regcache, sp, regnum);
  329. }
  330. static const struct regset sparc64obsd_gregset =
  331. {
  332. NULL, sparc64obsd_supply_gregset, NULL
  333. };
  334. static const struct regset sparc64obsd_fpregset =
  335. {
  336. NULL, sparc64obsd_supply_fpregset, NULL
  337. };
  338. static void
  339. sparc64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  340. {
  341. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  342. tdep->gregset = &sparc64obsd_gregset;
  343. tdep->sizeof_gregset = 288;
  344. tdep->fpregset = &sparc64obsd_fpregset;
  345. tdep->sizeof_fpregset = 272;
  346. /* Make sure we can single-step "new" syscalls. */
  347. tdep->step_trap = sparcnbsd_step_trap;
  348. frame_unwind_append_unwinder (gdbarch, &sparc64obsd_frame_unwind);
  349. frame_unwind_append_unwinder (gdbarch, &sparc64obsd_trapframe_unwind);
  350. sparc64_init_abi (info, gdbarch);
  351. obsd_init_abi (info, gdbarch);
  352. /* OpenBSD/sparc64 has SVR4-style shared libraries. */
  353. set_solib_svr4_fetch_link_map_offsets
  354. (gdbarch, svr4_lp64_fetch_link_map_offsets);
  355. set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);
  356. /* OpenBSD provides a user-level threads implementation. */
  357. bsd_uthread_set_supply_uthread (gdbarch, sparc64obsd_supply_uthread);
  358. bsd_uthread_set_collect_uthread (gdbarch, sparc64obsd_collect_uthread);
  359. }
  360. void _initialize_sparc64obsd_tdep ();
  361. void
  362. _initialize_sparc64obsd_tdep ()
  363. {
  364. gdbarch_register_osabi (bfd_arch_sparc, bfd_mach_sparc_v9,
  365. GDB_OSABI_OPENBSD, sparc64obsd_init_abi);
  366. }