linux-nios2-low.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* GNU/Linux/Nios II specific low level interface, for the remote server for
  2. GDB.
  3. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  4. Contributed by Mentor Graphics, Inc.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "server.h"
  17. #include "linux-low.h"
  18. #include "elf/common.h"
  19. #include "nat/gdb_ptrace.h"
  20. #include <endian.h>
  21. #include "gdb_proc_service.h"
  22. #include <asm/ptrace.h>
  23. #ifndef PTRACE_GET_THREAD_AREA
  24. #define PTRACE_GET_THREAD_AREA 25
  25. #endif
  26. /* Linux target op definitions for the NIOS II architecture. */
  27. class nios2_target : public linux_process_target
  28. {
  29. public:
  30. const regs_info *get_regs_info () override;
  31. const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
  32. protected:
  33. void low_arch_setup () override;
  34. bool low_cannot_fetch_register (int regno) override;
  35. bool low_cannot_store_register (int regno) override;
  36. bool low_supports_breakpoints () override;
  37. CORE_ADDR low_get_pc (regcache *regcache) override;
  38. void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
  39. bool low_breakpoint_at (CORE_ADDR pc) override;
  40. };
  41. /* The singleton target ops object. */
  42. static nios2_target the_nios2_target;
  43. bool
  44. nios2_target::low_supports_breakpoints ()
  45. {
  46. return true;
  47. }
  48. CORE_ADDR
  49. nios2_target::low_get_pc (regcache *regcache)
  50. {
  51. return linux_get_pc_32bit (regcache);
  52. }
  53. void
  54. nios2_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
  55. {
  56. linux_set_pc_32bit (regcache, pc);
  57. }
  58. /* The following definition must agree with the number of registers
  59. defined in "struct user_regs" in GLIBC
  60. (sysdeps/unix/sysv/linux/nios2/sys/user.h), and also with
  61. NIOS2_NUM_REGS in GDB proper. */
  62. #define nios2_num_regs 49
  63. /* Defined in auto-generated file nios2-linux.c. */
  64. void init_registers_nios2_linux (void);
  65. extern const struct target_desc *tdesc_nios2_linux;
  66. /* This union is used to convert between int and byte buffer
  67. representations of register contents. */
  68. union nios2_register
  69. {
  70. unsigned char buf[4];
  71. int reg32;
  72. };
  73. /* Return the ptrace ``address'' of register REGNO. */
  74. static int nios2_regmap[] = {
  75. -1, 1, 2, 3, 4, 5, 6, 7,
  76. 8, 9, 10, 11, 12, 13, 14, 15,
  77. 16, 17, 18, 19, 20, 21, 22, 23,
  78. 24, 25, 26, 27, 28, 29, 30, 31,
  79. 32, 33, 34, 35, 36, 37, 38, 39,
  80. 40, 41, 42, 43, 44, 45, 46, 47,
  81. 48,
  82. 0
  83. };
  84. /* Implement the low_arch_setup linux target ops method. */
  85. void
  86. nios2_target::low_arch_setup ()
  87. {
  88. current_process ()->tdesc = tdesc_nios2_linux;
  89. }
  90. /* Implement the low_cannot_fetch_register linux target ops method. */
  91. bool
  92. nios2_target::low_cannot_fetch_register (int regno)
  93. {
  94. return (nios2_regmap[regno] == -1);
  95. }
  96. /* Implement the low_cannot_store_register linux target ops method. */
  97. bool
  98. nios2_target::low_cannot_store_register (int regno)
  99. {
  100. return (nios2_regmap[regno] == -1);
  101. }
  102. /* Breakpoint support. Also see comments on nios2_breakpoint_from_pc
  103. in nios2-tdep.c. */
  104. #if defined(__nios2_arch__) && __nios2_arch__ == 2
  105. #define NIOS2_BREAKPOINT 0xb7fd0020
  106. #define CDX_BREAKPOINT 0xd7c9
  107. #else
  108. #define NIOS2_BREAKPOINT 0x003b6ffa
  109. #endif
  110. /* We only register the 4-byte breakpoint, even on R2 targets which also
  111. support 2-byte breakpoints. Since there is no supports_z_point_type
  112. function provided, gdbserver never inserts software breakpoints itself
  113. and instead relies on GDB to insert the breakpoint of the correct length
  114. via a memory write. */
  115. static const unsigned int nios2_breakpoint = NIOS2_BREAKPOINT;
  116. #define nios2_breakpoint_len 4
  117. /* Implementation of target ops method "sw_breakpoint_from_kind". */
  118. const gdb_byte *
  119. nios2_target::sw_breakpoint_from_kind (int kind, int *size)
  120. {
  121. *size = nios2_breakpoint_len;
  122. return (const gdb_byte *) &nios2_breakpoint;
  123. }
  124. /* Implement the low_breakpoint_at linux target ops method. */
  125. bool
  126. nios2_target::low_breakpoint_at (CORE_ADDR where)
  127. {
  128. unsigned int insn;
  129. /* For R2, first check for the 2-byte CDX trap.n breakpoint encoding. */
  130. #if defined(__nios2_arch__) && __nios2_arch__ == 2
  131. read_memory (where, (unsigned char *) &insn, 2);
  132. if (insn == CDX_BREAKPOINT)
  133. return true;
  134. #endif
  135. read_memory (where, (unsigned char *) &insn, 4);
  136. if (insn == nios2_breakpoint)
  137. return true;
  138. return false;
  139. }
  140. /* Fetch the thread-local storage pointer for libthread_db. */
  141. ps_err_e
  142. ps_get_thread_area (struct ps_prochandle *ph,
  143. lwpid_t lwpid, int idx, void **base)
  144. {
  145. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  146. return PS_ERR;
  147. /* IDX is the bias from the thread pointer to the beginning of the
  148. thread descriptor. It has to be subtracted due to implementation
  149. quirks in libthread_db. */
  150. *base = (void *) ((char *) *base - idx);
  151. return PS_OK;
  152. }
  153. /* Helper functions to collect/supply a single register REGNO. */
  154. static void
  155. nios2_collect_register (struct regcache *regcache, int regno,
  156. union nios2_register *reg)
  157. {
  158. union nios2_register tmp_reg;
  159. collect_register (regcache, regno, &tmp_reg.reg32);
  160. reg->reg32 = tmp_reg.reg32;
  161. }
  162. static void
  163. nios2_supply_register (struct regcache *regcache, int regno,
  164. const union nios2_register *reg)
  165. {
  166. supply_register (regcache, regno, reg->buf);
  167. }
  168. /* We have only a single register set on Nios II. */
  169. static void
  170. nios2_fill_gregset (struct regcache *regcache, void *buf)
  171. {
  172. union nios2_register *regset = (union nios2_register *) buf;
  173. int i;
  174. for (i = 1; i < nios2_num_regs; i++)
  175. nios2_collect_register (regcache, i, regset + i);
  176. }
  177. static void
  178. nios2_store_gregset (struct regcache *regcache, const void *buf)
  179. {
  180. const union nios2_register *regset = (union nios2_register *) buf;
  181. int i;
  182. for (i = 0; i < nios2_num_regs; i++)
  183. nios2_supply_register (regcache, i, regset + i);
  184. }
  185. static struct regset_info nios2_regsets[] =
  186. {
  187. { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
  188. nios2_num_regs * 4, GENERAL_REGS,
  189. nios2_fill_gregset, nios2_store_gregset },
  190. NULL_REGSET
  191. };
  192. static struct regsets_info nios2_regsets_info =
  193. {
  194. nios2_regsets, /* regsets */
  195. 0, /* num_regsets */
  196. NULL, /* disabled_regsets */
  197. };
  198. static struct usrregs_info nios2_usrregs_info =
  199. {
  200. nios2_num_regs,
  201. nios2_regmap,
  202. };
  203. static struct regs_info myregs_info =
  204. {
  205. NULL, /* regset_bitmap */
  206. &nios2_usrregs_info,
  207. &nios2_regsets_info
  208. };
  209. const regs_info *
  210. nios2_target::get_regs_info ()
  211. {
  212. return &myregs_info;
  213. }
  214. /* The linux target ops object. */
  215. linux_process_target *the_linux_target = &the_nios2_target;
  216. void
  217. initialize_low_arch (void)
  218. {
  219. init_registers_nios2_linux ();
  220. initialize_regsets_info (&nios2_regsets_info);
  221. }