nios2-linux-tdep.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Target-dependent code for GNU/Linux on Nios II.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Contributed by Mentor Graphics, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "frame.h"
  17. #include "osabi.h"
  18. #include "solib-svr4.h"
  19. #include "trad-frame.h"
  20. #include "tramp-frame.h"
  21. #include "symtab.h"
  22. #include "regset.h"
  23. #include "regcache.h"
  24. #include "linux-tdep.h"
  25. #include "glibc-tdep.h"
  26. #include "nios2-tdep.h"
  27. #include "gdbarch.h"
  28. /* Core file and register set support. */
  29. /* Map from the normal register enumeration order to the order that
  30. registers appear in core files, which corresponds to the order
  31. of the register slots in the kernel's struct pt_regs. */
  32. static const int reg_offsets[NIOS2_NUM_REGS] =
  33. {
  34. -1, 8, 9, 10, 11, 12, 13, 14, /* r0 - r7 */
  35. 0, 1, 2, 3, 4, 5, 6, 7, /* r8 - r15 */
  36. 23, 24, 25, 26, 27, 28, 29, 30, /* r16 - r23 */
  37. -1, -1, 19, 18, 17, 21, -1, 16, /* et bt gp sp fp ea sstatus ra */
  38. 21, /* pc */
  39. -1, 20, -1, -1, -1, -1, -1, -1, /* status estatus ... */
  40. -1, -1, -1, -1, -1, -1, -1, -1
  41. };
  42. /* General register set size. Should match sizeof (struct pt_regs) +
  43. sizeof (struct switch_stack) from the NIOS2 Linux kernel patch. */
  44. #define NIOS2_GREGS_SIZE (4 * 34)
  45. /* Implement the supply_regset hook for core files. */
  46. static void
  47. nios2_supply_gregset (const struct regset *regset,
  48. struct regcache *regcache,
  49. int regnum, const void *gregs_buf, size_t len)
  50. {
  51. const gdb_byte *gregs = (const gdb_byte *) gregs_buf;
  52. int regno;
  53. static const gdb_byte zero_buf[4] = {0, 0, 0, 0};
  54. for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
  55. if (regnum == -1 || regnum == regno)
  56. {
  57. if (reg_offsets[regno] != -1)
  58. regcache->raw_supply (regno, gregs + 4 * reg_offsets[regno]);
  59. else
  60. regcache->raw_supply (regno, zero_buf);
  61. }
  62. }
  63. /* Implement the collect_regset hook for core files. */
  64. static void
  65. nios2_collect_gregset (const struct regset *regset,
  66. const struct regcache *regcache,
  67. int regnum, void *gregs_buf, size_t len)
  68. {
  69. gdb_byte *gregs = (gdb_byte *) gregs_buf;
  70. int regno;
  71. for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
  72. if (regnum == -1 || regnum == regno)
  73. {
  74. if (reg_offsets[regno] != -1)
  75. regcache->raw_collect (regno, gregs + 4 * reg_offsets[regno]);
  76. }
  77. }
  78. static const struct regset nios2_core_regset =
  79. {
  80. NULL,
  81. nios2_supply_gregset,
  82. nios2_collect_gregset
  83. };
  84. /* Iterate over core file register note sections. */
  85. static void
  86. nios2_iterate_over_regset_sections (struct gdbarch *gdbarch,
  87. iterate_over_regset_sections_cb *cb,
  88. void *cb_data,
  89. const struct regcache *regcache)
  90. {
  91. cb (".reg", NIOS2_GREGS_SIZE, NIOS2_GREGS_SIZE, &nios2_core_regset, NULL,
  92. cb_data);
  93. }
  94. /* Initialize a trad-frame cache corresponding to the tramp-frame.
  95. FUNC is the address of the instruction TRAMP[0] in memory.
  96. This ABI is not documented. It corresponds to rt_setup_ucontext in
  97. the kernel arch/nios2/kernel/signal.c file.
  98. The key points are:
  99. - The kernel creates a trampoline at the hard-wired address 0x1044.
  100. - The stack pointer points to an object of type struct rt_sigframe.
  101. The definition of this structure is not exported from the kernel.
  102. The register save area is located at offset 152 bytes (as determined
  103. by inspection of the stack contents in the debugger), and the
  104. registers are saved as r1-r23, ra, fp, gp, ea, sp.
  105. This interface was implemented with kernel version 3.19 (the first
  106. official mainline kernel). Older unofficial kernel versions used
  107. incompatible conventions; we do not support those here. */
  108. #define NIOS2_SIGRETURN_TRAMP_ADDR 0x1044
  109. #define NIOS2_SIGRETURN_REGSAVE_OFFSET 152
  110. static void
  111. nios2_linux_rt_sigreturn_init (const struct tramp_frame *self,
  112. struct frame_info *next_frame,
  113. struct trad_frame_cache *this_cache,
  114. CORE_ADDR func)
  115. {
  116. CORE_ADDR sp = get_frame_register_unsigned (next_frame, NIOS2_SP_REGNUM);
  117. CORE_ADDR base = sp + NIOS2_SIGRETURN_REGSAVE_OFFSET;
  118. int i;
  119. for (i = 0; i < 23; i++)
  120. trad_frame_set_reg_addr (this_cache, i + 1, base + i * 4);
  121. trad_frame_set_reg_addr (this_cache, NIOS2_RA_REGNUM, base + 23 * 4);
  122. trad_frame_set_reg_addr (this_cache, NIOS2_FP_REGNUM, base + 24 * 4);
  123. trad_frame_set_reg_addr (this_cache, NIOS2_GP_REGNUM, base + 25 * 4);
  124. trad_frame_set_reg_addr (this_cache, NIOS2_PC_REGNUM, base + 27 * 4);
  125. trad_frame_set_reg_addr (this_cache, NIOS2_SP_REGNUM, base + 28 * 4);
  126. /* Save a frame ID. */
  127. trad_frame_set_id (this_cache, frame_id_build (base, func));
  128. }
  129. /* Trampoline for sigreturn. This has the form
  130. movi r2, __NR_rt_sigreturn
  131. trap 0
  132. appropriately encoded for R1 or R2. */
  133. static struct tramp_frame nios2_r1_linux_rt_sigreturn_tramp_frame =
  134. {
  135. SIGTRAMP_FRAME,
  136. 4,
  137. {
  138. { MATCH_R1_MOVI | SET_IW_I_B (2) | SET_IW_I_IMM16 (139), ULONGEST_MAX },
  139. { MATCH_R1_TRAP | SET_IW_R_IMM5 (0), ULONGEST_MAX},
  140. { TRAMP_SENTINEL_INSN }
  141. },
  142. nios2_linux_rt_sigreturn_init
  143. };
  144. static struct tramp_frame nios2_r2_linux_rt_sigreturn_tramp_frame =
  145. {
  146. SIGTRAMP_FRAME,
  147. 4,
  148. {
  149. { MATCH_R2_MOVI | SET_IW_F2I16_B (2) | SET_IW_F2I16_IMM16 (139), ULONGEST_MAX },
  150. { MATCH_R2_TRAP | SET_IW_X2L5_IMM5 (0), ULONGEST_MAX},
  151. { TRAMP_SENTINEL_INSN }
  152. },
  153. nios2_linux_rt_sigreturn_init
  154. };
  155. /* When FRAME is at a syscall instruction, return the PC of the next
  156. instruction to be executed. */
  157. static CORE_ADDR
  158. nios2_linux_syscall_next_pc (struct frame_info *frame,
  159. const struct nios2_opcode *op)
  160. {
  161. CORE_ADDR pc = get_frame_pc (frame);
  162. ULONGEST syscall_nr = get_frame_register_unsigned (frame, NIOS2_R2_REGNUM);
  163. /* If we are about to make a sigreturn syscall, use the unwinder to
  164. decode the signal frame. */
  165. if (syscall_nr == 139 /* rt_sigreturn */)
  166. return frame_unwind_caller_pc (frame);
  167. return pc + op->size;
  168. }
  169. /* Return true if PC is a kernel helper, a function mapped by the kernel
  170. into user space on an unwritable page. Currently the only such function
  171. is __kuser_cmpxchg at 0x1004. See arch/nios2/kernel/entry.S in the Linux
  172. kernel sources and sysdeps/unix/sysv/linux/nios2/atomic-machine.h in
  173. GLIBC. */
  174. static bool
  175. nios2_linux_is_kernel_helper (CORE_ADDR pc)
  176. {
  177. return pc == 0x1004;
  178. }
  179. /* Hook function for gdbarch_register_osabi. */
  180. static void
  181. nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  182. {
  183. nios2_gdbarch_tdep *tdep = (nios2_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  184. linux_init_abi (info, gdbarch, 0);
  185. /* Shared library handling. */
  186. set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  187. set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
  188. set_solib_svr4_fetch_link_map_offsets (gdbarch,
  189. linux_ilp32_fetch_link_map_offsets);
  190. /* Enable TLS support. */
  191. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  192. svr4_fetch_objfile_link_map);
  193. /* Core file support. */
  194. set_gdbarch_iterate_over_regset_sections
  195. (gdbarch, nios2_iterate_over_regset_sections);
  196. /* Linux signal frame unwinders. */
  197. if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_nios2r2)
  198. tramp_frame_prepend_unwinder (gdbarch,
  199. &nios2_r2_linux_rt_sigreturn_tramp_frame);
  200. else
  201. tramp_frame_prepend_unwinder (gdbarch,
  202. &nios2_r1_linux_rt_sigreturn_tramp_frame);
  203. tdep->syscall_next_pc = nios2_linux_syscall_next_pc;
  204. tdep->is_kernel_helper = nios2_linux_is_kernel_helper;
  205. /* Index of target address word in glibc jmp_buf. */
  206. tdep->jb_pc = 10;
  207. }
  208. void _initialize_nios2_linux_tdep ();
  209. void
  210. _initialize_nios2_linux_tdep ()
  211. {
  212. const struct bfd_arch_info *arch_info;
  213. for (arch_info = bfd_lookup_arch (bfd_arch_nios2, 0);
  214. arch_info != NULL;
  215. arch_info = arch_info->next)
  216. gdbarch_register_osabi (bfd_arch_nios2, arch_info->mach,
  217. GDB_OSABI_LINUX, nios2_linux_init_abi);
  218. }