linux-sparc-low.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* Low level interface to ptrace, for the remote server for GDB.
  2. Copyright (C) 1995-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 "server.h"
  15. #include "linux-low.h"
  16. #include "nat/gdb_ptrace.h"
  17. #include "gdb_proc_service.h"
  18. /* The stack pointer is offset from the stack frame by a BIAS of 2047
  19. (0x7ff) for 64-bit code. BIAS is likely to be defined on SPARC
  20. hosts, so undefine it first. */
  21. #undef BIAS
  22. #define BIAS 2047
  23. #ifdef HAVE_SYS_REG_H
  24. #include <sys/reg.h>
  25. #endif
  26. #define INSN_SIZE 4
  27. #define SPARC_R_REGS_NUM 32
  28. #define SPARC_F_REGS_NUM 48
  29. #define SPARC_CONTROL_REGS_NUM 6
  30. #define sparc_num_regs \
  31. (SPARC_R_REGS_NUM + SPARC_F_REGS_NUM + SPARC_CONTROL_REGS_NUM)
  32. /* Linux target op definitions for the SPARC architecture. */
  33. class sparc_target : public linux_process_target
  34. {
  35. public:
  36. const regs_info *get_regs_info () override;
  37. const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
  38. protected:
  39. void low_arch_setup () override;
  40. bool low_cannot_fetch_register (int regno) override;
  41. bool low_cannot_store_register (int regno) override;
  42. bool low_supports_breakpoints () override;
  43. CORE_ADDR low_get_pc (regcache *regcache) override;
  44. /* No low_set_pc is needed. */
  45. bool low_breakpoint_at (CORE_ADDR pc) override;
  46. };
  47. /* The singleton target ops object. */
  48. static sparc_target the_sparc_target;
  49. bool
  50. sparc_target::low_supports_breakpoints ()
  51. {
  52. return true;
  53. }
  54. CORE_ADDR
  55. sparc_target::low_get_pc (regcache *regcache)
  56. {
  57. return linux_get_pc_64bit (regcache);
  58. }
  59. /* Each offset is multiplied by 8, because of the register size.
  60. These offsets apply to the buffer sent/filled by ptrace.
  61. Additionally, the array elements order corresponds to the .dat file, and the
  62. gdb's registers enumeration order. */
  63. static int sparc_regmap[] = {
  64. /* These offsets correspond to GET/SETREGSET. */
  65. -1, 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, /* g0 .. g7 */
  66. 7*8, 8*8, 9*8, 10*8, 11*8, 12*8, 13*8, 14*8, /* o0 .. o5, sp, o7 */
  67. -1, -1, -1, -1, -1, -1, -1, -1, /* l0 .. l7 */
  68. -1, -1, -1, -1, -1, -1, -1, -1, /* i0 .. i5, fp, i7 */
  69. /* Floating point registers offsets correspond to GET/SETFPREGSET. */
  70. 0*4, 1*4, 2*4, 3*4, 4*4, 5*4, 6*4, 7*4, /* f0 .. f7 */
  71. 8*4, 9*4, 10*4, 11*4, 12*4, 13*4, 14*4, 15*4, /* f8 .. f15 */
  72. 16*4, 17*4, 18*4, 19*4, 20*4, 21*4, 22*4, 23*4, /* f16 .. f23 */
  73. 24*4, 25*4, 26*4, 27*4, 28*4, 29*4, 30*4, 31*4, /* f24 .. f31 */
  74. /* F32 offset starts next to f31: 31*4+4 = 16 * 8. */
  75. 16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8, /* f32 .. f46 */
  76. 24*8, 25*8, 26*8, 27*8, 28*8, 29*8, 30*8, 31*8, /* f48 .. f62 */
  77. 17 *8, /* pc */
  78. 18 *8, /* npc */
  79. 16 *8, /* state */
  80. /* FSR offset also corresponds to GET/SETFPREGSET, ans is placed
  81. next to f62. */
  82. 32 *8, /* fsr */
  83. -1, /* fprs */
  84. /* Y register is 32-bits length, but gdb takes care of that. */
  85. 19 *8, /* y */
  86. };
  87. struct regs_range_t
  88. {
  89. int regno_start;
  90. int regno_end;
  91. };
  92. static const struct regs_range_t gregs_ranges[] = {
  93. { 0, 31 }, /* g0 .. i7 */
  94. { 80, 82 }, /* pc .. state */
  95. { 84, 85 } /* fprs .. y */
  96. };
  97. #define N_GREGS_RANGES (sizeof (gregs_ranges) / sizeof (struct regs_range_t))
  98. static const struct regs_range_t fpregs_ranges[] = {
  99. { 32, 79 }, /* f0 .. f62 */
  100. { 83, 83 } /* fsr */
  101. };
  102. #define N_FPREGS_RANGES (sizeof (fpregs_ranges) / sizeof (struct regs_range_t))
  103. /* Defined in auto-generated file reg-sparc64.c. */
  104. void init_registers_sparc64 (void);
  105. extern const struct target_desc *tdesc_sparc64;
  106. bool
  107. sparc_target::low_cannot_store_register (int regno)
  108. {
  109. return (regno >= sparc_num_regs || sparc_regmap[regno] == -1);
  110. }
  111. bool
  112. sparc_target::low_cannot_fetch_register (int regno)
  113. {
  114. return (regno >= sparc_num_regs || sparc_regmap[regno] == -1);
  115. }
  116. static void
  117. sparc_fill_gregset_to_stack (struct regcache *regcache, const void *buf)
  118. {
  119. int i;
  120. CORE_ADDR addr = 0;
  121. unsigned char tmp_reg_buf[8];
  122. const int l0_regno = find_regno (regcache->tdesc, "l0");
  123. const int i7_regno = l0_regno + 15;
  124. /* These registers have to be stored in the stack. */
  125. memcpy (&addr,
  126. ((char *) buf) + sparc_regmap[find_regno (regcache->tdesc, "sp")],
  127. sizeof (addr));
  128. addr += BIAS;
  129. for (i = l0_regno; i <= i7_regno; i++)
  130. {
  131. collect_register (regcache, i, tmp_reg_buf);
  132. the_target->write_memory (addr, tmp_reg_buf, sizeof (tmp_reg_buf));
  133. addr += sizeof (tmp_reg_buf);
  134. }
  135. }
  136. static void
  137. sparc_fill_gregset (struct regcache *regcache, void *buf)
  138. {
  139. int i;
  140. int range;
  141. for (range = 0; range < N_GREGS_RANGES; range++)
  142. for (i = gregs_ranges[range].regno_start;
  143. i <= gregs_ranges[range].regno_end; i++)
  144. if (sparc_regmap[i] != -1)
  145. collect_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
  146. sparc_fill_gregset_to_stack (regcache, buf);
  147. }
  148. static void
  149. sparc_fill_fpregset (struct regcache *regcache, void *buf)
  150. {
  151. int i;
  152. int range;
  153. for (range = 0; range < N_FPREGS_RANGES; range++)
  154. for (i = fpregs_ranges[range].regno_start;
  155. i <= fpregs_ranges[range].regno_end; i++)
  156. collect_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
  157. }
  158. static void
  159. sparc_store_gregset_from_stack (struct regcache *regcache, const void *buf)
  160. {
  161. int i;
  162. CORE_ADDR addr = 0;
  163. unsigned char tmp_reg_buf[8];
  164. const int l0_regno = find_regno (regcache->tdesc, "l0");
  165. const int i7_regno = l0_regno + 15;
  166. /* These registers have to be obtained from the stack. */
  167. memcpy (&addr,
  168. ((char *) buf) + sparc_regmap[find_regno (regcache->tdesc, "sp")],
  169. sizeof (addr));
  170. addr += BIAS;
  171. for (i = l0_regno; i <= i7_regno; i++)
  172. {
  173. the_target->read_memory (addr, tmp_reg_buf, sizeof (tmp_reg_buf));
  174. supply_register (regcache, i, tmp_reg_buf);
  175. addr += sizeof (tmp_reg_buf);
  176. }
  177. }
  178. static void
  179. sparc_store_gregset (struct regcache *regcache, const void *buf)
  180. {
  181. int i;
  182. char zerobuf[8];
  183. int range;
  184. memset (zerobuf, 0, sizeof (zerobuf));
  185. for (range = 0; range < N_GREGS_RANGES; range++)
  186. for (i = gregs_ranges[range].regno_start;
  187. i <= gregs_ranges[range].regno_end; i++)
  188. if (sparc_regmap[i] != -1)
  189. supply_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
  190. else
  191. supply_register (regcache, i, zerobuf);
  192. sparc_store_gregset_from_stack (regcache, buf);
  193. }
  194. static void
  195. sparc_store_fpregset (struct regcache *regcache, const void *buf)
  196. {
  197. int i;
  198. int range;
  199. for (range = 0; range < N_FPREGS_RANGES; range++)
  200. for (i = fpregs_ranges[range].regno_start;
  201. i <= fpregs_ranges[range].regno_end;
  202. i++)
  203. supply_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
  204. }
  205. static const gdb_byte sparc_breakpoint[INSN_SIZE] = {
  206. 0x91, 0xd0, 0x20, 0x01
  207. };
  208. #define sparc_breakpoint_len INSN_SIZE
  209. /* Implementation of target ops method "sw_breakpoint_from_kind". */
  210. const gdb_byte *
  211. sparc_target::sw_breakpoint_from_kind (int kind, int *size)
  212. {
  213. *size = sparc_breakpoint_len;
  214. return sparc_breakpoint;
  215. }
  216. bool
  217. sparc_target::low_breakpoint_at (CORE_ADDR where)
  218. {
  219. unsigned char insn[INSN_SIZE];
  220. read_memory (where, (unsigned char *) insn, sizeof (insn));
  221. if (memcmp (sparc_breakpoint, insn, sizeof (insn)) == 0)
  222. return true;
  223. /* If necessary, recognize more trap instructions here. GDB only
  224. uses TRAP Always. */
  225. return false;
  226. }
  227. void
  228. sparc_target::low_arch_setup ()
  229. {
  230. current_process ()->tdesc = tdesc_sparc64;
  231. }
  232. static struct regset_info sparc_regsets[] = {
  233. { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t),
  234. GENERAL_REGS,
  235. sparc_fill_gregset, sparc_store_gregset },
  236. { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, sizeof (fpregset_t),
  237. FP_REGS,
  238. sparc_fill_fpregset, sparc_store_fpregset },
  239. NULL_REGSET
  240. };
  241. static struct regsets_info sparc_regsets_info =
  242. {
  243. sparc_regsets, /* regsets */
  244. 0, /* num_regsets */
  245. NULL, /* disabled_regsets */
  246. };
  247. static struct usrregs_info sparc_usrregs_info =
  248. {
  249. sparc_num_regs,
  250. /* No regmap needs to be provided since this impl. doesn't use
  251. USRREGS. */
  252. NULL
  253. };
  254. static struct regs_info myregs_info =
  255. {
  256. NULL, /* regset_bitmap */
  257. &sparc_usrregs_info,
  258. &sparc_regsets_info
  259. };
  260. const regs_info *
  261. sparc_target::get_regs_info ()
  262. {
  263. return &myregs_info;
  264. }
  265. /* The linux target ops object. */
  266. linux_process_target *the_linux_target = &the_sparc_target;
  267. void
  268. initialize_low_arch (void)
  269. {
  270. /* Initialize the Linux target descriptions. */
  271. init_registers_sparc64 ();
  272. initialize_regsets_info (&sparc_regsets_info);
  273. }