sparc64-linux-tdep.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* Target-dependent code for GNU/Linux UltraSPARC.
  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 "frame.h"
  16. #include "frame-unwind.h"
  17. #include "dwarf2/frame.h"
  18. #include "regset.h"
  19. #include "regcache.h"
  20. #include "gdbarch.h"
  21. #include "gdbcore.h"
  22. #include "osabi.h"
  23. #include "solib-svr4.h"
  24. #include "symtab.h"
  25. #include "trad-frame.h"
  26. #include "tramp-frame.h"
  27. #include "xml-syscall.h"
  28. #include "linux-tdep.h"
  29. /* ADI specific si_code */
  30. #ifndef SEGV_ACCADI
  31. #define SEGV_ACCADI 3
  32. #endif
  33. #ifndef SEGV_ADIDERR
  34. #define SEGV_ADIDERR 4
  35. #endif
  36. #ifndef SEGV_ADIPERR
  37. #define SEGV_ADIPERR 5
  38. #endif
  39. /* The syscall's XML filename for sparc 64-bit. */
  40. #define XML_SYSCALL_FILENAME_SPARC64 "syscalls/sparc64-linux.xml"
  41. #include "sparc64-tdep.h"
  42. /* Signal trampoline support. */
  43. static void sparc64_linux_sigframe_init (const struct tramp_frame *self,
  44. struct frame_info *this_frame,
  45. struct trad_frame_cache *this_cache,
  46. CORE_ADDR func);
  47. /* See sparc-linux-tdep.c for details. Note that 64-bit binaries only
  48. use RT signals. */
  49. static const struct tramp_frame sparc64_linux_rt_sigframe =
  50. {
  51. SIGTRAMP_FRAME,
  52. 4,
  53. {
  54. { 0x82102065, ULONGEST_MAX }, /* mov __NR_rt_sigreturn, %g1 */
  55. { 0x91d0206d, ULONGEST_MAX }, /* ta 0x6d */
  56. { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
  57. },
  58. sparc64_linux_sigframe_init
  59. };
  60. static void
  61. sparc64_linux_sigframe_init (const struct tramp_frame *self,
  62. struct frame_info *this_frame,
  63. struct trad_frame_cache *this_cache,
  64. CORE_ADDR func)
  65. {
  66. CORE_ADDR base, addr, sp_addr;
  67. int regnum;
  68. base = get_frame_register_unsigned (this_frame, SPARC_O1_REGNUM);
  69. base += 128;
  70. /* Offsets from <bits/sigcontext.h>. */
  71. /* Since %g0 is always zero, keep the identity encoding. */
  72. addr = base + 8;
  73. sp_addr = base + ((SPARC_SP_REGNUM - SPARC_G0_REGNUM) * 8);
  74. for (regnum = SPARC_G1_REGNUM; regnum <= SPARC_O7_REGNUM; regnum++)
  75. {
  76. trad_frame_set_reg_addr (this_cache, regnum, addr);
  77. addr += 8;
  78. }
  79. trad_frame_set_reg_addr (this_cache, SPARC64_STATE_REGNUM, addr + 0);
  80. trad_frame_set_reg_addr (this_cache, SPARC64_PC_REGNUM, addr + 8);
  81. trad_frame_set_reg_addr (this_cache, SPARC64_NPC_REGNUM, addr + 16);
  82. trad_frame_set_reg_addr (this_cache, SPARC64_Y_REGNUM, addr + 24);
  83. trad_frame_set_reg_addr (this_cache, SPARC64_FPRS_REGNUM, addr + 28);
  84. base = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  85. if (base & 1)
  86. base += BIAS;
  87. addr = get_frame_memory_unsigned (this_frame, sp_addr, 8);
  88. if (addr & 1)
  89. addr += BIAS;
  90. for (regnum = SPARC_L0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++)
  91. {
  92. trad_frame_set_reg_addr (this_cache, regnum, addr);
  93. addr += 8;
  94. }
  95. trad_frame_set_id (this_cache, frame_id_build (base, func));
  96. }
  97. /* sparc64 GNU/Linux implementation of the report_signal_info
  98. gdbarch hook.
  99. Displays information related to ADI memory corruptions. */
  100. static void
  101. sparc64_linux_report_signal_info (struct gdbarch *gdbarch, struct ui_out *uiout,
  102. enum gdb_signal siggnal)
  103. {
  104. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word != 64
  105. || siggnal != GDB_SIGNAL_SEGV)
  106. return;
  107. CORE_ADDR addr = 0;
  108. long si_code = 0;
  109. try
  110. {
  111. /* Evaluate si_code to see if the segfault is ADI related. */
  112. si_code = parse_and_eval_long ("$_siginfo.si_code\n");
  113. if (si_code >= SEGV_ACCADI && si_code <= SEGV_ADIPERR)
  114. addr = parse_and_eval_long ("$_siginfo._sifields._sigfault.si_addr");
  115. }
  116. catch (const gdb_exception &exception)
  117. {
  118. return;
  119. }
  120. /* Print out ADI event based on sig_code value */
  121. switch (si_code)
  122. {
  123. case SEGV_ACCADI: /* adi not enabled */
  124. uiout->text ("\n");
  125. uiout->field_string ("sigcode-meaning", _("ADI disabled"));
  126. uiout->text (_(" while accessing address "));
  127. uiout->field_core_addr ("bound-access", gdbarch, addr);
  128. break;
  129. case SEGV_ADIDERR: /* disrupting mismatch */
  130. uiout->text ("\n");
  131. uiout->field_string ("sigcode-meaning", _("ADI deferred mismatch"));
  132. uiout->text (_(" while accessing address "));
  133. uiout->field_core_addr ("bound-access", gdbarch, addr);
  134. break;
  135. case SEGV_ADIPERR: /* precise mismatch */
  136. uiout->text ("\n");
  137. uiout->field_string ("sigcode-meaning", _("ADI precise mismatch"));
  138. uiout->text (_(" while accessing address "));
  139. uiout->field_core_addr ("bound-access", gdbarch, addr);
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. /* Return the address of a system call's alternative return
  146. address. */
  147. static CORE_ADDR
  148. sparc64_linux_step_trap (struct frame_info *frame, unsigned long insn)
  149. {
  150. /* __NR_rt_sigreturn is 101 */
  151. if ((insn == 0x91d0206d)
  152. && (get_frame_register_unsigned (frame, SPARC_G1_REGNUM) == 101))
  153. {
  154. struct gdbarch *gdbarch = get_frame_arch (frame);
  155. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  156. ULONGEST sp = get_frame_register_unsigned (frame, SPARC_SP_REGNUM);
  157. if (sp & 1)
  158. sp += BIAS;
  159. /* The kernel puts the sigreturn registers on the stack,
  160. and this is where the signal unwinding state is take from
  161. when returning from a signal.
  162. A siginfo_t sits 192 bytes from the base of the stack. This
  163. siginfo_t is 128 bytes, and is followed by the sigreturn
  164. register save area. The saved PC sits at a 136 byte offset
  165. into there. */
  166. return read_memory_unsigned_integer (sp + 192 + 128 + 136,
  167. 8, byte_order);
  168. }
  169. return 0;
  170. }
  171. const struct sparc_gregmap sparc64_linux_core_gregmap =
  172. {
  173. 32 * 8, /* %tstate */
  174. 33 * 8, /* %tpc */
  175. 34 * 8, /* %tnpc */
  176. 35 * 8, /* %y */
  177. -1, /* %wim */
  178. -1, /* %tbr */
  179. 1 * 8, /* %g1 */
  180. 16 * 8, /* %l0 */
  181. 8, /* y size */
  182. };
  183. static void
  184. sparc64_linux_supply_core_gregset (const struct regset *regset,
  185. struct regcache *regcache,
  186. int regnum, const void *gregs, size_t len)
  187. {
  188. sparc64_supply_gregset (&sparc64_linux_core_gregmap,
  189. regcache, regnum, gregs);
  190. }
  191. static void
  192. sparc64_linux_collect_core_gregset (const struct regset *regset,
  193. const struct regcache *regcache,
  194. int regnum, void *gregs, size_t len)
  195. {
  196. sparc64_collect_gregset (&sparc64_linux_core_gregmap,
  197. regcache, regnum, gregs);
  198. }
  199. static void
  200. sparc64_linux_supply_core_fpregset (const struct regset *regset,
  201. struct regcache *regcache,
  202. int regnum, const void *fpregs, size_t len)
  203. {
  204. sparc64_supply_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs);
  205. }
  206. static void
  207. sparc64_linux_collect_core_fpregset (const struct regset *regset,
  208. const struct regcache *regcache,
  209. int regnum, void *fpregs, size_t len)
  210. {
  211. sparc64_collect_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs);
  212. }
  213. /* Set the program counter for process PTID to PC. */
  214. #define TSTATE_SYSCALL 0x0000000000000020ULL
  215. static void
  216. sparc64_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
  217. {
  218. gdbarch *arch = regcache->arch ();
  219. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (arch);
  220. ULONGEST state;
  221. regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
  222. regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
  223. /* Clear the "in syscall" bit to prevent the kernel from
  224. messing with the PCs we just installed, if we happen to be
  225. within an interrupted system call that the kernel wants to
  226. restart.
  227. Note that after we return from the dummy call, the TSTATE et al.
  228. registers will be automatically restored, and the kernel
  229. continues to restart the system call at this point. */
  230. regcache_cooked_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
  231. state &= ~TSTATE_SYSCALL;
  232. regcache_cooked_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
  233. }
  234. static LONGEST
  235. sparc64_linux_get_syscall_number (struct gdbarch *gdbarch,
  236. thread_info *thread)
  237. {
  238. struct regcache *regcache = get_thread_regcache (thread);
  239. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  240. /* The content of a register. */
  241. gdb_byte buf[8];
  242. /* The result. */
  243. LONGEST ret;
  244. /* Getting the system call number from the register.
  245. When dealing with the sparc architecture, this information
  246. is stored at the %g1 register. */
  247. regcache->cooked_read (SPARC_G1_REGNUM, buf);
  248. ret = extract_signed_integer (buf, 8, byte_order);
  249. return ret;
  250. }
  251. /* Implement the "get_longjmp_target" gdbarch method. */
  252. static int
  253. sparc64_linux_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  254. {
  255. struct gdbarch *gdbarch = get_frame_arch (frame);
  256. CORE_ADDR jb_addr;
  257. gdb_byte buf[8];
  258. jb_addr = get_frame_register_unsigned (frame, SPARC_O0_REGNUM);
  259. /* setjmp and longjmp in SPARC64 are implemented in glibc using the
  260. setcontext and getcontext system calls respectively. These
  261. system calls operate on ucontext_t structures, which happen to
  262. partially have the same structure than jmp_buf. However the
  263. ucontext returned by getcontext, and thus the jmp_buf structure
  264. returned by setjmp, contains the context of the trap instruction
  265. in the glibc __[sig]setjmp wrapper, not the context of the user
  266. code calling setjmp.
  267. %o7 in the jmp_buf structure is stored at offset 18*8 in the
  268. mc_gregs array, which is itself located at offset 32 into
  269. jmp_buf. See bits/setjmp.h. This register contains the address
  270. of the 'call setjmp' instruction in user code.
  271. In order to determine the longjmp target address in the
  272. initiating frame we need to examine the call instruction itself,
  273. in particular whether the annul bit is set. If it is not set
  274. then we need to jump over the instruction at the delay slot. */
  275. if (target_read_memory (jb_addr + 32 + (18 * 8), buf, 8))
  276. return 0;
  277. *pc = extract_unsigned_integer (buf, 8, gdbarch_byte_order (gdbarch));
  278. if (!sparc_is_annulled_branch_insn (*pc))
  279. *pc += 4; /* delay slot insn */
  280. *pc += 4; /* call insn */
  281. return 1;
  282. }
  283. static const struct regset sparc64_linux_gregset =
  284. {
  285. NULL,
  286. sparc64_linux_supply_core_gregset,
  287. sparc64_linux_collect_core_gregset
  288. };
  289. static const struct regset sparc64_linux_fpregset =
  290. {
  291. NULL,
  292. sparc64_linux_supply_core_fpregset,
  293. sparc64_linux_collect_core_fpregset
  294. };
  295. static void
  296. sparc64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  297. {
  298. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  299. linux_init_abi (info, gdbarch, 0);
  300. tdep->gregset = &sparc64_linux_gregset;
  301. tdep->sizeof_gregset = 288;
  302. tdep->fpregset = &sparc64_linux_fpregset;
  303. tdep->sizeof_fpregset = 280;
  304. tramp_frame_prepend_unwinder (gdbarch, &sparc64_linux_rt_sigframe);
  305. /* Hook in the DWARF CFI frame unwinder. */
  306. dwarf2_append_unwinders (gdbarch);
  307. sparc64_init_abi (info, gdbarch);
  308. /* GNU/Linux has SVR4-style shared libraries... */
  309. set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  310. set_solib_svr4_fetch_link_map_offsets
  311. (gdbarch, linux_lp64_fetch_link_map_offsets);
  312. /* ...which means that we need some special handling when doing
  313. prologue analysis. */
  314. tdep->plt_entry_size = 16;
  315. /* Enable TLS support. */
  316. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  317. svr4_fetch_objfile_link_map);
  318. /* Make sure we can single-step over signal return system calls. */
  319. tdep->step_trap = sparc64_linux_step_trap;
  320. /* Make sure we can single-step over longjmp calls. */
  321. set_gdbarch_get_longjmp_target (gdbarch, sparc64_linux_get_longjmp_target);
  322. set_gdbarch_write_pc (gdbarch, sparc64_linux_write_pc);
  323. /* Functions for 'catch syscall'. */
  324. set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_SPARC64);
  325. set_gdbarch_get_syscall_number (gdbarch,
  326. sparc64_linux_get_syscall_number);
  327. set_gdbarch_report_signal_info (gdbarch, sparc64_linux_report_signal_info);
  328. }
  329. void _initialize_sparc64_linux_tdep ();
  330. void
  331. _initialize_sparc64_linux_tdep ()
  332. {
  333. gdbarch_register_osabi (bfd_arch_sparc, bfd_mach_sparc_v9,
  334. GDB_OSABI_LINUX, sparc64_linux_init_abi);
  335. }