linux-tic6x-low.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Target dependent code for GDB on TI C6x systems.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Andrew Jenner <andrew@codesourcery.com>
  4. Contributed by Yao Qi <yao@codesourcery.com>
  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 "arch/tic6x.h"
  19. #include "tdesc.h"
  20. #include "nat/gdb_ptrace.h"
  21. #include <endian.h>
  22. #include "gdb_proc_service.h"
  23. #ifndef PTRACE_GET_THREAD_AREA
  24. #define PTRACE_GET_THREAD_AREA 25
  25. #endif
  26. /* There are at most 69 registers accessible in ptrace. */
  27. #define TIC6X_NUM_REGS 69
  28. #include <asm/ptrace.h>
  29. /* Linux target op definitions for the TI C6x architecture. */
  30. class tic6x_target : public linux_process_target
  31. {
  32. public:
  33. const regs_info *get_regs_info () override;
  34. const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
  35. protected:
  36. void low_arch_setup () override;
  37. bool low_cannot_fetch_register (int regno) override;
  38. bool low_cannot_store_register (int regno) override;
  39. bool low_supports_breakpoints () override;
  40. CORE_ADDR low_get_pc (regcache *regcache) override;
  41. void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
  42. bool low_breakpoint_at (CORE_ADDR pc) override;
  43. };
  44. /* The singleton target ops object. */
  45. static tic6x_target the_tic6x_target;
  46. /* Defined in auto-generated file tic6x-c64xp-linux.c. */
  47. void init_registers_tic6x_c64xp_linux (void);
  48. extern const struct target_desc *tdesc_tic6x_c64xp_linux;
  49. /* Defined in auto-generated file tic6x-c64x-linux.c. */
  50. void init_registers_tic6x_c64x_linux (void);
  51. extern const struct target_desc *tdesc_tic6x_c64x_linux;
  52. /* Defined in auto-generated file tic62x-c6xp-linux.c. */
  53. void init_registers_tic6x_c62x_linux (void);
  54. extern const struct target_desc *tdesc_tic6x_c62x_linux;
  55. union tic6x_register
  56. {
  57. unsigned char buf[4];
  58. int reg32;
  59. };
  60. /* Return the ptrace ``address'' of register REGNO. */
  61. #if __BYTE_ORDER == __BIG_ENDIAN
  62. static int tic6x_regmap_c64xp[] = {
  63. /* A0 - A15 */
  64. 53, 52, 55, 54, 57, 56, 59, 58,
  65. 61, 60, 63, 62, 65, 64, 67, 66,
  66. /* B0 - B15 */
  67. 23, 22, 25, 24, 27, 26, 29, 28,
  68. 31, 30, 33, 32, 35, 34, 69, 68,
  69. /* CSR PC */
  70. 5, 4,
  71. /* A16 - A31 */
  72. 37, 36, 39, 38, 41, 40, 43, 42,
  73. 45, 44, 47, 46, 49, 48, 51, 50,
  74. /* B16 - B31 */
  75. 7, 6, 9, 8, 11, 10, 13, 12,
  76. 15, 14, 17, 16, 19, 18, 21, 20,
  77. /* TSR, ILC, RILC */
  78. 1, 2, 3
  79. };
  80. static int tic6x_regmap_c64x[] = {
  81. /* A0 - A15 */
  82. 51, 50, 53, 52, 55, 54, 57, 56,
  83. 59, 58, 61, 60, 63, 62, 65, 64,
  84. /* B0 - B15 */
  85. 21, 20, 23, 22, 25, 24, 27, 26,
  86. 29, 28, 31, 30, 33, 32, 67, 66,
  87. /* CSR PC */
  88. 3, 2,
  89. /* A16 - A31 */
  90. 35, 34, 37, 36, 39, 38, 41, 40,
  91. 43, 42, 45, 44, 47, 46, 49, 48,
  92. /* B16 - B31 */
  93. 5, 4, 7, 6, 9, 8, 11, 10,
  94. 13, 12, 15, 14, 17, 16, 19, 18,
  95. -1, -1, -1
  96. };
  97. static int tic6x_regmap_c62x[] = {
  98. /* A0 - A15 */
  99. 19, 18, 21, 20, 23, 22, 25, 24,
  100. 27, 26, 29, 28, 31, 30, 33, 32,
  101. /* B0 - B15 */
  102. 5, 4, 7, 6, 9, 8, 11, 10,
  103. 13, 12, 15, 14, 17, 16, 35, 34,
  104. /* CSR, PC */
  105. 3, 2,
  106. -1, -1, -1, -1, -1, -1, -1, -1,
  107. -1, -1, -1, -1, -1, -1, -1, -1,
  108. -1, -1, -1, -1, -1, -1, -1, -1,
  109. -1, -1, -1, -1, -1, -1, -1, -1,
  110. -1, -1, -1
  111. };
  112. #else
  113. static int tic6x_regmap_c64xp[] = {
  114. /* A0 - A15 */
  115. 52, 53, 54, 55, 56, 57, 58, 59,
  116. 60, 61, 62, 63, 64, 65, 66, 67,
  117. /* B0 - B15 */
  118. 22, 23, 24, 25, 26, 27, 28, 29,
  119. 30, 31, 32, 33, 34, 35, 68, 69,
  120. /* CSR PC */
  121. 4, 5,
  122. /* A16 - A31 */
  123. 36, 37, 38, 39, 40, 41, 42, 43,
  124. 44, 45, 46, 47, 48, 49, 50, 51,
  125. /* B16 -B31 */
  126. 6, 7, 8, 9, 10, 11, 12, 13,
  127. 14, 15, 16, 17, 18, 19, 20, 31,
  128. /* TSR, ILC, RILC */
  129. 0, 3, 2
  130. };
  131. static int tic6x_regmap_c64x[] = {
  132. /* A0 - A15 */
  133. 50, 51, 52, 53, 54, 55, 56, 57,
  134. 58, 59, 60, 61, 62, 63, 64, 65,
  135. /* B0 - B15 */
  136. 20, 21, 22, 23, 24, 25, 26, 27,
  137. 28, 29, 30, 31, 32, 33, 66, 67,
  138. /* CSR PC */
  139. 2, 3,
  140. /* A16 - A31 */
  141. 34, 35, 36, 37, 38, 39, 40, 41,
  142. 42, 43, 44, 45, 46, 47, 48, 49,
  143. /* B16 - B31 */
  144. 4, 5, 6, 7, 8, 9, 10, 11,
  145. 12, 13, 14, 15, 16, 17, 18, 19,
  146. -1, -1, -1
  147. };
  148. static int tic6x_regmap_c62x[] = {
  149. /* A0 - A15 */
  150. 18, 19, 20, 21, 22, 23, 24, 25,
  151. 26, 27, 28, 29, 30, 31, 32, 33,
  152. /* B0 - B15 */
  153. 4, 5, 6, 7, 8, 9, 10, 11,
  154. 12, 13, 14, 15, 16, 17, 34, 35,
  155. /* CSR PC */
  156. 2, 3,
  157. -1, -1, -1, -1, -1, -1, -1, -1,
  158. -1, -1, -1, -1, -1, -1, -1, -1,
  159. -1, -1, -1, -1, -1, -1, -1, -1,
  160. -1, -1, -1, -1, -1, -1, -1, -1,
  161. -1, -1, -1
  162. };
  163. #endif
  164. static int *tic6x_regmap;
  165. static unsigned int tic6x_breakpoint;
  166. #define tic6x_breakpoint_len 4
  167. /* Implementation of target ops method "sw_breakpoint_from_kind". */
  168. const gdb_byte *
  169. tic6x_target::sw_breakpoint_from_kind (int kind, int *size)
  170. {
  171. *size = tic6x_breakpoint_len;
  172. return (const gdb_byte *) &tic6x_breakpoint;
  173. }
  174. static struct usrregs_info tic6x_usrregs_info =
  175. {
  176. TIC6X_NUM_REGS,
  177. NULL, /* Set in tic6x_read_description. */
  178. };
  179. static const struct target_desc *
  180. tic6x_read_description (enum c6x_feature feature)
  181. {
  182. static target_desc *tdescs[C6X_LAST] = { };
  183. struct target_desc **tdesc = &tdescs[feature];
  184. if (*tdesc == NULL)
  185. {
  186. *tdesc = tic6x_create_target_description (feature);
  187. static const char *expedite_regs[] = { "A15", "PC", NULL };
  188. init_target_desc (*tdesc, expedite_regs);
  189. }
  190. return *tdesc;
  191. }
  192. bool
  193. tic6x_target::low_cannot_fetch_register (int regno)
  194. {
  195. return (tic6x_regmap[regno] == -1);
  196. }
  197. bool
  198. tic6x_target::low_cannot_store_register (int regno)
  199. {
  200. return (tic6x_regmap[regno] == -1);
  201. }
  202. bool
  203. tic6x_target::low_supports_breakpoints ()
  204. {
  205. return true;
  206. }
  207. CORE_ADDR
  208. tic6x_target::low_get_pc (regcache *regcache)
  209. {
  210. union tic6x_register pc;
  211. collect_register_by_name (regcache, "PC", pc.buf);
  212. return pc.reg32;
  213. }
  214. void
  215. tic6x_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
  216. {
  217. union tic6x_register newpc;
  218. newpc.reg32 = pc;
  219. supply_register_by_name (regcache, "PC", newpc.buf);
  220. }
  221. bool
  222. tic6x_target::low_breakpoint_at (CORE_ADDR where)
  223. {
  224. unsigned int insn;
  225. read_memory (where, (unsigned char *) &insn, 4);
  226. if (insn == tic6x_breakpoint)
  227. return true;
  228. /* If necessary, recognize more trap instructions here. GDB only uses the
  229. one. */
  230. return false;
  231. }
  232. /* Fetch the thread-local storage pointer for libthread_db. */
  233. ps_err_e
  234. ps_get_thread_area (struct ps_prochandle *ph,
  235. lwpid_t lwpid, int idx, void **base)
  236. {
  237. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  238. return PS_ERR;
  239. /* IDX is the bias from the thread pointer to the beginning of the
  240. thread descriptor. It has to be subtracted due to implementation
  241. quirks in libthread_db. */
  242. *base = (void *) ((char *) *base - idx);
  243. return PS_OK;
  244. }
  245. static void
  246. tic6x_collect_register (struct regcache *regcache, int regno,
  247. union tic6x_register *reg)
  248. {
  249. union tic6x_register tmp_reg;
  250. collect_register (regcache, regno, &tmp_reg.reg32);
  251. reg->reg32 = tmp_reg.reg32;
  252. }
  253. static void
  254. tic6x_supply_register (struct regcache *regcache, int regno,
  255. const union tic6x_register *reg)
  256. {
  257. int offset = 0;
  258. supply_register (regcache, regno, reg->buf + offset);
  259. }
  260. static void
  261. tic6x_fill_gregset (struct regcache *regcache, void *buf)
  262. {
  263. auto regset = static_cast<union tic6x_register *> (buf);
  264. int i;
  265. for (i = 0; i < TIC6X_NUM_REGS; i++)
  266. if (tic6x_regmap[i] != -1)
  267. tic6x_collect_register (regcache, i, regset + tic6x_regmap[i]);
  268. }
  269. static void
  270. tic6x_store_gregset (struct regcache *regcache, const void *buf)
  271. {
  272. const auto regset = static_cast<const union tic6x_register *> (buf);
  273. int i;
  274. for (i = 0; i < TIC6X_NUM_REGS; i++)
  275. if (tic6x_regmap[i] != -1)
  276. tic6x_supply_register (regcache, i, regset + tic6x_regmap[i]);
  277. }
  278. static struct regset_info tic6x_regsets[] = {
  279. { PTRACE_GETREGS, PTRACE_SETREGS, 0, TIC6X_NUM_REGS * 4, GENERAL_REGS,
  280. tic6x_fill_gregset, tic6x_store_gregset },
  281. NULL_REGSET
  282. };
  283. void
  284. tic6x_target::low_arch_setup ()
  285. {
  286. register unsigned int csr asm ("B2");
  287. unsigned int cpuid;
  288. enum c6x_feature feature = C6X_CORE;
  289. /* Determine the CPU we're running on to find the register order. */
  290. __asm__ ("MVC .S2 CSR,%0" : "=r" (csr) :);
  291. cpuid = csr >> 24;
  292. switch (cpuid)
  293. {
  294. case 0x00: /* C62x */
  295. case 0x02: /* C67x */
  296. tic6x_regmap = tic6x_regmap_c62x;
  297. tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */
  298. feature = C6X_CORE;
  299. break;
  300. case 0x03: /* C67x+ */
  301. tic6x_regmap = tic6x_regmap_c64x;
  302. tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */
  303. feature = C6X_GP;
  304. break;
  305. case 0x0c: /* C64x */
  306. tic6x_regmap = tic6x_regmap_c64x;
  307. tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */
  308. feature = C6X_GP;
  309. break;
  310. case 0x10: /* C64x+ */
  311. case 0x14: /* C674x */
  312. case 0x15: /* C66x */
  313. tic6x_regmap = tic6x_regmap_c64xp;
  314. tic6x_breakpoint = 0x56454314; /* illegal opcode */
  315. feature = C6X_C6XP;
  316. break;
  317. default:
  318. error ("Unknown CPU ID 0x%02x", cpuid);
  319. }
  320. tic6x_usrregs_info.regmap = tic6x_regmap;
  321. current_process ()->tdesc = tic6x_read_description (feature);
  322. }
  323. static struct regsets_info tic6x_regsets_info =
  324. {
  325. tic6x_regsets, /* regsets */
  326. 0, /* num_regsets */
  327. NULL, /* disabled_regsets */
  328. };
  329. static struct regs_info myregs_info =
  330. {
  331. NULL, /* regset_bitmap */
  332. &tic6x_usrregs_info,
  333. &tic6x_regsets_info
  334. };
  335. const regs_info *
  336. tic6x_target::get_regs_info ()
  337. {
  338. return &myregs_info;
  339. }
  340. #if GDB_SELF_TEST
  341. #include "gdbsupport/selftest.h"
  342. namespace selftests {
  343. namespace tdesc {
  344. static void
  345. tic6x_tdesc_test ()
  346. {
  347. SELF_CHECK (*tdesc_tic6x_c62x_linux == *tic6x_read_description (C6X_CORE));
  348. SELF_CHECK (*tdesc_tic6x_c64x_linux == *tic6x_read_description (C6X_GP));
  349. SELF_CHECK (*tdesc_tic6x_c64xp_linux == *tic6x_read_description (C6X_C6XP));
  350. }
  351. }
  352. }
  353. #endif
  354. /* The linux target ops object. */
  355. linux_process_target *the_linux_target = &the_tic6x_target;
  356. void
  357. initialize_low_arch (void)
  358. {
  359. #if GDB_SELF_TEST
  360. /* Initialize the Linux target descriptions. */
  361. init_registers_tic6x_c64xp_linux ();
  362. init_registers_tic6x_c64x_linux ();
  363. init_registers_tic6x_c62x_linux ();
  364. selftests::register_test ("tic6x-tdesc", selftests::tdesc::tic6x_tdesc_test);
  365. #endif
  366. initialize_regsets_info (&tic6x_regsets_info);
  367. }