linux-riscv-low.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* GNU/Linux/RISC-V specific low level interface, for the remote server
  2. for GDB.
  3. Copyright (C) 2020-2022 Free Software Foundation, 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 "server.h"
  16. #include "linux-low.h"
  17. #include "tdesc.h"
  18. #include "elf/common.h"
  19. #include "nat/riscv-linux-tdesc.h"
  20. #include "opcode/riscv.h"
  21. /* Work around glibc header breakage causing ELF_NFPREG not to be usable. */
  22. #ifndef NFPREG
  23. # define NFPREG 33
  24. #endif
  25. /* Linux target op definitions for the RISC-V architecture. */
  26. class riscv_target : public linux_process_target
  27. {
  28. public:
  29. const regs_info *get_regs_info () override;
  30. int breakpoint_kind_from_pc (CORE_ADDR *pcptr) 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_fetch_register (regcache *regcache, int regno) override;
  37. bool low_supports_breakpoints () override;
  38. CORE_ADDR low_get_pc (regcache *regcache) override;
  39. void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
  40. bool low_breakpoint_at (CORE_ADDR pc) override;
  41. };
  42. /* The singleton target ops object. */
  43. static riscv_target the_riscv_target;
  44. bool
  45. riscv_target::low_cannot_fetch_register (int regno)
  46. {
  47. gdb_assert_not_reached ("linux target op low_cannot_fetch_register "
  48. "is not implemented by the target");
  49. }
  50. bool
  51. riscv_target::low_cannot_store_register (int regno)
  52. {
  53. gdb_assert_not_reached ("linux target op low_cannot_store_register "
  54. "is not implemented by the target");
  55. }
  56. /* Implementation of linux target ops method "low_arch_setup". */
  57. void
  58. riscv_target::low_arch_setup ()
  59. {
  60. static const char *expedite_regs[] = { "sp", "pc", NULL };
  61. const riscv_gdbarch_features features
  62. = riscv_linux_read_features (lwpid_of (current_thread));
  63. target_desc_up tdesc = riscv_create_target_description (features);
  64. if (!tdesc->expedite_regs)
  65. init_target_desc (tdesc.get (), expedite_regs);
  66. current_process ()->tdesc = tdesc.release ();
  67. }
  68. /* Collect GPRs from REGCACHE into BUF. */
  69. static void
  70. riscv_fill_gregset (struct regcache *regcache, void *buf)
  71. {
  72. const struct target_desc *tdesc = regcache->tdesc;
  73. elf_gregset_t *regset = (elf_gregset_t *) buf;
  74. int regno = find_regno (tdesc, "zero");
  75. int i;
  76. collect_register_by_name (regcache, "pc", *regset);
  77. for (i = 1; i < ARRAY_SIZE (*regset); i++)
  78. collect_register (regcache, regno + i, *regset + i);
  79. }
  80. /* Supply GPRs from BUF into REGCACHE. */
  81. static void
  82. riscv_store_gregset (struct regcache *regcache, const void *buf)
  83. {
  84. const elf_gregset_t *regset = (const elf_gregset_t *) buf;
  85. const struct target_desc *tdesc = regcache->tdesc;
  86. int regno = find_regno (tdesc, "zero");
  87. int i;
  88. supply_register_by_name (regcache, "pc", *regset);
  89. supply_register_zeroed (regcache, regno);
  90. for (i = 1; i < ARRAY_SIZE (*regset); i++)
  91. supply_register (regcache, regno + i, *regset + i);
  92. }
  93. /* Collect FPRs from REGCACHE into BUF. */
  94. static void
  95. riscv_fill_fpregset (struct regcache *regcache, void *buf)
  96. {
  97. const struct target_desc *tdesc = regcache->tdesc;
  98. int regno = find_regno (tdesc, "ft0");
  99. int flen = register_size (regcache->tdesc, regno);
  100. gdb_byte *regbuf = (gdb_byte *) buf;
  101. int i;
  102. for (i = 0; i < ELF_NFPREG - 1; i++, regbuf += flen)
  103. collect_register (regcache, regno + i, regbuf);
  104. collect_register_by_name (regcache, "fcsr", regbuf);
  105. }
  106. /* Supply FPRs from BUF into REGCACHE. */
  107. static void
  108. riscv_store_fpregset (struct regcache *regcache, const void *buf)
  109. {
  110. const struct target_desc *tdesc = regcache->tdesc;
  111. int regno = find_regno (tdesc, "ft0");
  112. int flen = register_size (regcache->tdesc, regno);
  113. const gdb_byte *regbuf = (const gdb_byte *) buf;
  114. int i;
  115. for (i = 0; i < ELF_NFPREG - 1; i++, regbuf += flen)
  116. supply_register (regcache, regno + i, regbuf);
  117. supply_register_by_name (regcache, "fcsr", regbuf);
  118. }
  119. /* RISC-V/Linux regsets. FPRs are optional and come in different sizes,
  120. so define multiple regsets for them marking them all as OPTIONAL_REGS
  121. rather than FP_REGS, so that "regsets_fetch_inferior_registers" picks
  122. the right one according to size. */
  123. static struct regset_info riscv_regsets[] = {
  124. { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
  125. sizeof (elf_gregset_t), GENERAL_REGS,
  126. riscv_fill_gregset, riscv_store_gregset },
  127. { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
  128. sizeof (struct __riscv_mc_q_ext_state), OPTIONAL_REGS,
  129. riscv_fill_fpregset, riscv_store_fpregset },
  130. { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
  131. sizeof (struct __riscv_mc_d_ext_state), OPTIONAL_REGS,
  132. riscv_fill_fpregset, riscv_store_fpregset },
  133. { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
  134. sizeof (struct __riscv_mc_f_ext_state), OPTIONAL_REGS,
  135. riscv_fill_fpregset, riscv_store_fpregset },
  136. NULL_REGSET
  137. };
  138. /* RISC-V/Linux regset information. */
  139. static struct regsets_info riscv_regsets_info =
  140. {
  141. riscv_regsets, /* regsets */
  142. 0, /* num_regsets */
  143. NULL, /* disabled_regsets */
  144. };
  145. /* Definition of linux_target_ops data member "regs_info". */
  146. static struct regs_info riscv_regs =
  147. {
  148. NULL, /* regset_bitmap */
  149. NULL, /* usrregs */
  150. &riscv_regsets_info,
  151. };
  152. /* Implementation of linux target ops method "get_regs_info". */
  153. const regs_info *
  154. riscv_target::get_regs_info ()
  155. {
  156. return &riscv_regs;
  157. }
  158. /* Implementation of linux target ops method "low_fetch_register". */
  159. bool
  160. riscv_target::low_fetch_register (regcache *regcache, int regno)
  161. {
  162. const struct target_desc *tdesc = regcache->tdesc;
  163. if (regno != find_regno (tdesc, "zero"))
  164. return false;
  165. supply_register_zeroed (regcache, regno);
  166. return true;
  167. }
  168. bool
  169. riscv_target::low_supports_breakpoints ()
  170. {
  171. return true;
  172. }
  173. /* Implementation of linux target ops method "low_get_pc". */
  174. CORE_ADDR
  175. riscv_target::low_get_pc (regcache *regcache)
  176. {
  177. elf_gregset_t regset;
  178. if (sizeof (regset[0]) == 8)
  179. return linux_get_pc_64bit (regcache);
  180. else
  181. return linux_get_pc_32bit (regcache);
  182. }
  183. /* Implementation of linux target ops method "low_set_pc". */
  184. void
  185. riscv_target::low_set_pc (regcache *regcache, CORE_ADDR newpc)
  186. {
  187. elf_gregset_t regset;
  188. if (sizeof (regset[0]) == 8)
  189. linux_set_pc_64bit (regcache, newpc);
  190. else
  191. linux_set_pc_32bit (regcache, newpc);
  192. }
  193. /* Correct in either endianness. */
  194. static const uint16_t riscv_ibreakpoint[] = { 0x0073, 0x0010 };
  195. static const uint16_t riscv_cbreakpoint = 0x9002;
  196. /* Implementation of target ops method "breakpoint_kind_from_pc". */
  197. int
  198. riscv_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
  199. {
  200. union
  201. {
  202. gdb_byte bytes[2];
  203. uint16_t insn;
  204. }
  205. buf;
  206. if (target_read_memory (*pcptr, buf.bytes, sizeof (buf.insn)) == 0
  207. && riscv_insn_length (buf.insn == sizeof (riscv_ibreakpoint)))
  208. return sizeof (riscv_ibreakpoint);
  209. else
  210. return sizeof (riscv_cbreakpoint);
  211. }
  212. /* Implementation of target ops method "sw_breakpoint_from_kind". */
  213. const gdb_byte *
  214. riscv_target::sw_breakpoint_from_kind (int kind, int *size)
  215. {
  216. *size = kind;
  217. switch (kind)
  218. {
  219. case sizeof (riscv_ibreakpoint):
  220. return (const gdb_byte *) &riscv_ibreakpoint;
  221. default:
  222. return (const gdb_byte *) &riscv_cbreakpoint;
  223. }
  224. }
  225. /* Implementation of linux target ops method "low_breakpoint_at". */
  226. bool
  227. riscv_target::low_breakpoint_at (CORE_ADDR pc)
  228. {
  229. union
  230. {
  231. gdb_byte bytes[2];
  232. uint16_t insn;
  233. }
  234. buf;
  235. if (target_read_memory (pc, buf.bytes, sizeof (buf.insn)) == 0
  236. && (buf.insn == riscv_cbreakpoint
  237. || (buf.insn == riscv_ibreakpoint[0]
  238. && target_read_memory (pc + sizeof (buf.insn), buf.bytes,
  239. sizeof (buf.insn)) == 0
  240. && buf.insn == riscv_ibreakpoint[1])))
  241. return true;
  242. else
  243. return false;
  244. }
  245. /* The linux target ops object. */
  246. linux_process_target *the_linux_target = &the_riscv_target;
  247. /* Initialize the RISC-V/Linux target. */
  248. void
  249. initialize_low_arch ()
  250. {
  251. initialize_regsets_info (&riscv_regsets_info);
  252. }