linux-arc-low.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* Target dependent code for the remote server for GNU/Linux ARC.
  2. Copyright 2020-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 "regdef.h"
  16. #include "linux-low.h"
  17. #include "tdesc.h"
  18. #include "arch/arc.h"
  19. #include <linux/elf.h>
  20. #include <arpa/inet.h>
  21. /* Linux starting with 4.12 supports NT_ARC_V2 note type, which adds R30,
  22. R58 and R59 registers. */
  23. #ifdef NT_ARC_V2
  24. #define ARC_HAS_V2_REGSET
  25. #endif
  26. /* The encoding of the instruction "TRAP_S 1" (endianness agnostic). */
  27. #define TRAP_S_1_OPCODE 0x783e
  28. #define TRAP_S_1_SIZE 2
  29. /* Using a mere "uint16_t arc_linux_traps_s = TRAP_S_1_OPCODE" would
  30. work as well, because the endianness will end up correctly when
  31. the code is compiled for the same endianness as the target (see
  32. the notes for "low_breakpoint_at" in this file). However, this
  33. illustrates how the __BIG_ENDIAN__ macro can be used to make
  34. easy-to-understand codes. */
  35. #if defined(__BIG_ENDIAN__)
  36. /* 0x78, 0x3e. */
  37. static gdb_byte arc_linux_trap_s[TRAP_S_1_SIZE]
  38. = {TRAP_S_1_OPCODE >> 8, TRAP_S_1_OPCODE & 0xFF};
  39. #else
  40. /* 0x3e, 0x78. */
  41. static gdb_byte arc_linux_trap_s[TRAP_S_1_SIZE]
  42. = {TRAP_S_1_OPCODE && 0xFF, TRAP_S_1_OPCODE >> 8};
  43. #endif
  44. /* Linux target op definitions for the ARC architecture.
  45. Note for future: in case of adding the protected method low_get_next_pcs(),
  46. the public method supports_software_single_step() should be added to return
  47. "true". */
  48. class arc_target : public linux_process_target
  49. {
  50. public:
  51. const regs_info *get_regs_info () override;
  52. const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
  53. protected:
  54. void low_arch_setup () override;
  55. bool low_cannot_fetch_register (int regno) override;
  56. bool low_cannot_store_register (int regno) override;
  57. bool low_supports_breakpoints () override;
  58. CORE_ADDR low_get_pc (regcache *regcache) override;
  59. void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
  60. bool low_breakpoint_at (CORE_ADDR where) override;
  61. };
  62. /* The singleton target ops object. */
  63. static arc_target the_arc_target;
  64. bool
  65. arc_target::low_supports_breakpoints ()
  66. {
  67. return true;
  68. }
  69. CORE_ADDR
  70. arc_target::low_get_pc (regcache *regcache)
  71. {
  72. return linux_get_pc_32bit (regcache);
  73. }
  74. void
  75. arc_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
  76. {
  77. linux_set_pc_32bit (regcache, pc);
  78. }
  79. static const struct target_desc *
  80. arc_linux_read_description (void)
  81. {
  82. #ifdef __ARC700__
  83. arc_arch_features features (4, ARC_ISA_ARCV1);
  84. #else
  85. arc_arch_features features (4, ARC_ISA_ARCV2);
  86. #endif
  87. target_desc_up tdesc = arc_create_target_description (features);
  88. static const char *expedite_regs[] = { "sp", "status32", nullptr };
  89. init_target_desc (tdesc.get (), expedite_regs);
  90. return tdesc.release ();
  91. }
  92. void
  93. arc_target::low_arch_setup ()
  94. {
  95. current_process ()->tdesc = arc_linux_read_description ();
  96. }
  97. bool
  98. arc_target::low_cannot_fetch_register (int regno)
  99. {
  100. return (regno >= current_process ()->tdesc->reg_defs.size ());
  101. }
  102. bool
  103. arc_target::low_cannot_store_register (int regno)
  104. {
  105. return (regno >= current_process ()->tdesc->reg_defs.size ());
  106. }
  107. /* This works for both endianness. Below you see an illustration of how
  108. the "trap_s 1" instruction encoded for both endianness in the memory
  109. will end up as the TRAP_S_1_OPCODE constant:
  110. BE: 0x78 0x3e --> at INSN addr: 0x78 0x3e --> INSN = 0x783e
  111. LE: 0x3e 0x78 --> at INSN addr: 0x3e 0x78 --> INSN = 0x783e
  112. One can employ "memcmp()" for comparing the arrays too. */
  113. bool
  114. arc_target::low_breakpoint_at (CORE_ADDR where)
  115. {
  116. uint16_t insn;
  117. /* "the_target" global variable is the current object at hand. */
  118. this->read_memory (where, (gdb_byte *) &insn, TRAP_S_1_SIZE);
  119. return (insn == TRAP_S_1_OPCODE);
  120. }
  121. /* PTRACE_GETREGSET/NT_PRSTATUS and PTRACE_SETREGSET/NT_PRSTATUS work with
  122. regsets in a struct, "user_regs_struct", defined in the
  123. linux/arch/arc/include/uapi/asm/ptrace.h header. This code supports
  124. ARC Linux ABI v3 and v4. */
  125. /* Populate a ptrace NT_PRSTATUS regset from a regcache.
  126. This appears to be a unique approach to populating the buffer, but
  127. being name, rather than offset based, it is robust to future API
  128. changes, as there is no need to create a regmap of registers in the
  129. user_regs_struct. */
  130. static void
  131. arc_fill_gregset (struct regcache *regcache, void *buf)
  132. {
  133. struct user_regs_struct *regbuf = (struct user_regs_struct *) buf;
  134. /* Core registers. */
  135. collect_register_by_name (regcache, "r0", &(regbuf->scratch.r0));
  136. collect_register_by_name (regcache, "r1", &(regbuf->scratch.r1));
  137. collect_register_by_name (regcache, "r2", &(regbuf->scratch.r2));
  138. collect_register_by_name (regcache, "r3", &(regbuf->scratch.r3));
  139. collect_register_by_name (regcache, "r4", &(regbuf->scratch.r4));
  140. collect_register_by_name (regcache, "r5", &(regbuf->scratch.r5));
  141. collect_register_by_name (regcache, "r6", &(regbuf->scratch.r6));
  142. collect_register_by_name (regcache, "r7", &(regbuf->scratch.r7));
  143. collect_register_by_name (regcache, "r8", &(regbuf->scratch.r8));
  144. collect_register_by_name (regcache, "r9", &(regbuf->scratch.r9));
  145. collect_register_by_name (regcache, "r10", &(regbuf->scratch.r10));
  146. collect_register_by_name (regcache, "r11", &(regbuf->scratch.r11));
  147. collect_register_by_name (regcache, "r12", &(regbuf->scratch.r12));
  148. collect_register_by_name (regcache, "r13", &(regbuf->callee.r13));
  149. collect_register_by_name (regcache, "r14", &(regbuf->callee.r14));
  150. collect_register_by_name (regcache, "r15", &(regbuf->callee.r15));
  151. collect_register_by_name (regcache, "r16", &(regbuf->callee.r16));
  152. collect_register_by_name (regcache, "r17", &(regbuf->callee.r17));
  153. collect_register_by_name (regcache, "r18", &(regbuf->callee.r18));
  154. collect_register_by_name (regcache, "r19", &(regbuf->callee.r19));
  155. collect_register_by_name (regcache, "r20", &(regbuf->callee.r20));
  156. collect_register_by_name (regcache, "r21", &(regbuf->callee.r21));
  157. collect_register_by_name (regcache, "r22", &(regbuf->callee.r22));
  158. collect_register_by_name (regcache, "r23", &(regbuf->callee.r23));
  159. collect_register_by_name (regcache, "r24", &(regbuf->callee.r24));
  160. collect_register_by_name (regcache, "r25", &(regbuf->callee.r25));
  161. collect_register_by_name (regcache, "gp", &(regbuf->scratch.gp));
  162. collect_register_by_name (regcache, "fp", &(regbuf->scratch.fp));
  163. collect_register_by_name (regcache, "sp", &(regbuf->scratch.sp));
  164. collect_register_by_name (regcache, "blink", &(regbuf->scratch.blink));
  165. /* Loop registers. */
  166. collect_register_by_name (regcache, "lp_count", &(regbuf->scratch.lp_count));
  167. collect_register_by_name (regcache, "lp_start", &(regbuf->scratch.lp_start));
  168. collect_register_by_name (regcache, "lp_end", &(regbuf->scratch.lp_end));
  169. /* The current "pc" value must be written to "eret" (exception return
  170. address) register, because that is the address that the kernel code
  171. will jump back to after a breakpoint exception has been raised.
  172. The "pc_stop" value is ignored by the genregs_set() in
  173. linux/arch/arc/kernel/ptrace.c. */
  174. collect_register_by_name (regcache, "pc", &(regbuf->scratch.ret));
  175. /* Currently ARC Linux ptrace doesn't allow writes to status32 because
  176. some of its bits are kernel mode-only and shoudn't be writable from
  177. user-space. Writing status32 from debugger could be useful, though,
  178. so ability to write non-priviliged bits will be added to kernel
  179. sooner or later. */
  180. /* BTA. */
  181. collect_register_by_name (regcache, "bta", &(regbuf->scratch.bta));
  182. }
  183. /* Populate a regcache from a ptrace NT_PRSTATUS regset. */
  184. static void
  185. arc_store_gregset (struct regcache *regcache, const void *buf)
  186. {
  187. const struct user_regs_struct *regbuf = (const struct user_regs_struct *) buf;
  188. /* Core registers. */
  189. supply_register_by_name (regcache, "r0", &(regbuf->scratch.r0));
  190. supply_register_by_name (regcache, "r1", &(regbuf->scratch.r1));
  191. supply_register_by_name (regcache, "r2", &(regbuf->scratch.r2));
  192. supply_register_by_name (regcache, "r3", &(regbuf->scratch.r3));
  193. supply_register_by_name (regcache, "r4", &(regbuf->scratch.r4));
  194. supply_register_by_name (regcache, "r5", &(regbuf->scratch.r5));
  195. supply_register_by_name (regcache, "r6", &(regbuf->scratch.r6));
  196. supply_register_by_name (regcache, "r7", &(regbuf->scratch.r7));
  197. supply_register_by_name (regcache, "r8", &(regbuf->scratch.r8));
  198. supply_register_by_name (regcache, "r9", &(regbuf->scratch.r9));
  199. supply_register_by_name (regcache, "r10", &(regbuf->scratch.r10));
  200. supply_register_by_name (regcache, "r11", &(regbuf->scratch.r11));
  201. supply_register_by_name (regcache, "r12", &(regbuf->scratch.r12));
  202. supply_register_by_name (regcache, "r13", &(regbuf->callee.r13));
  203. supply_register_by_name (regcache, "r14", &(regbuf->callee.r14));
  204. supply_register_by_name (regcache, "r15", &(regbuf->callee.r15));
  205. supply_register_by_name (regcache, "r16", &(regbuf->callee.r16));
  206. supply_register_by_name (regcache, "r17", &(regbuf->callee.r17));
  207. supply_register_by_name (regcache, "r18", &(regbuf->callee.r18));
  208. supply_register_by_name (regcache, "r19", &(regbuf->callee.r19));
  209. supply_register_by_name (regcache, "r20", &(regbuf->callee.r20));
  210. supply_register_by_name (regcache, "r21", &(regbuf->callee.r21));
  211. supply_register_by_name (regcache, "r22", &(regbuf->callee.r22));
  212. supply_register_by_name (regcache, "r23", &(regbuf->callee.r23));
  213. supply_register_by_name (regcache, "r24", &(regbuf->callee.r24));
  214. supply_register_by_name (regcache, "r25", &(regbuf->callee.r25));
  215. supply_register_by_name (regcache, "gp", &(regbuf->scratch.gp));
  216. supply_register_by_name (regcache, "fp", &(regbuf->scratch.fp));
  217. supply_register_by_name (regcache, "sp", &(regbuf->scratch.sp));
  218. supply_register_by_name (regcache, "blink", &(regbuf->scratch.blink));
  219. /* Loop registers. */
  220. supply_register_by_name (regcache, "lp_count", &(regbuf->scratch.lp_count));
  221. supply_register_by_name (regcache, "lp_start", &(regbuf->scratch.lp_start));
  222. supply_register_by_name (regcache, "lp_end", &(regbuf->scratch.lp_end));
  223. /* The genregs_get() in linux/arch/arc/kernel/ptrace.c populates the
  224. pseudo register "stop_pc" with the "efa" (exception fault address)
  225. register. This was deemed necessary, because the breakpoint
  226. instruction, "trap_s 1", is a committing one; i.e. the "eret"
  227. (exception return address) register will be pointing to the next
  228. instruction, while "efa" points to the address that raised the
  229. breakpoint. */
  230. supply_register_by_name (regcache, "pc", &(regbuf->stop_pc));
  231. unsigned long pcl = regbuf->stop_pc & ~3L;
  232. supply_register_by_name (regcache, "pcl", &pcl);
  233. /* Other auxilliary registers. */
  234. supply_register_by_name (regcache, "status32", &(regbuf->scratch.status32));
  235. /* BTA. */
  236. supply_register_by_name (regcache, "bta", &(regbuf->scratch.bta));
  237. }
  238. #ifdef ARC_HAS_V2_REGSET
  239. /* Look through a regcache's TDESC for a register named NAME.
  240. If found, return true; false, otherwise. */
  241. static bool
  242. is_reg_name_available_p (const struct target_desc *tdesc,
  243. const char *name)
  244. {
  245. for (const gdb::reg &reg : tdesc->reg_defs)
  246. if (strcmp (name, reg.name) == 0)
  247. return true;
  248. return false;
  249. }
  250. /* Copy registers from regcache to user_regs_arcv2. */
  251. static void
  252. arc_fill_v2_regset (struct regcache *regcache, void *buf)
  253. {
  254. struct user_regs_arcv2 *regbuf = (struct user_regs_arcv2 *) buf;
  255. if (is_reg_name_available_p (regcache->tdesc, "r30"))
  256. collect_register_by_name (regcache, "r30", &(regbuf->r30));
  257. if (is_reg_name_available_p (regcache->tdesc, "r58"))
  258. collect_register_by_name (regcache, "r58", &(regbuf->r58));
  259. if (is_reg_name_available_p (regcache->tdesc, "r59"))
  260. collect_register_by_name (regcache, "r59", &(regbuf->r59));
  261. }
  262. /* Copy registers from user_regs_arcv2 to regcache. */
  263. static void
  264. arc_store_v2_regset (struct regcache *regcache, const void *buf)
  265. {
  266. struct user_regs_arcv2 *regbuf = (struct user_regs_arcv2 *) buf;
  267. if (is_reg_name_available_p (regcache->tdesc, "r30"))
  268. supply_register_by_name (regcache, "r30", &(regbuf->r30));
  269. if (is_reg_name_available_p (regcache->tdesc, "r58"))
  270. supply_register_by_name (regcache, "r58", &(regbuf->r58));
  271. if (is_reg_name_available_p (regcache->tdesc, "r59"))
  272. supply_register_by_name (regcache, "r59", &(regbuf->r59));
  273. }
  274. #endif
  275. /* Fetch the thread-local storage pointer for libthread_db. Note that
  276. this function is not called from GDB, but is called from libthread_db.
  277. This is the same function as for other architectures, for example in
  278. linux-arm-low.c. */
  279. ps_err_e
  280. ps_get_thread_area (struct ps_prochandle *ph, lwpid_t lwpid,
  281. int idx, void **base)
  282. {
  283. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, nullptr, base) != 0)
  284. return PS_ERR;
  285. /* IDX is the bias from the thread pointer to the beginning of the
  286. thread descriptor. It has to be subtracted due to implementation
  287. quirks in libthread_db. */
  288. *base = (void *) ((char *) *base - idx);
  289. return PS_OK;
  290. }
  291. static struct regset_info arc_regsets[] =
  292. {
  293. { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
  294. sizeof (struct user_regs_struct), GENERAL_REGS,
  295. arc_fill_gregset, arc_store_gregset
  296. },
  297. #ifdef ARC_HAS_V2_REGSET
  298. { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_ARC_V2,
  299. sizeof (struct user_regs_arcv2), GENERAL_REGS,
  300. arc_fill_v2_regset, arc_store_v2_regset
  301. },
  302. #endif
  303. NULL_REGSET
  304. };
  305. static struct regsets_info arc_regsets_info =
  306. {
  307. arc_regsets, /* regsets */
  308. 0, /* num_regsets */
  309. nullptr, /* disabled regsets */
  310. };
  311. static struct regs_info arc_regs_info =
  312. {
  313. nullptr, /* regset_bitmap */
  314. nullptr, /* usrregs */
  315. &arc_regsets_info
  316. };
  317. const regs_info *
  318. arc_target::get_regs_info ()
  319. {
  320. return &arc_regs_info;
  321. }
  322. /* One of the methods necessary for Z0 packet support. */
  323. const gdb_byte *
  324. arc_target::sw_breakpoint_from_kind (int kind, int *size)
  325. {
  326. gdb_assert (kind == TRAP_S_1_SIZE);
  327. *size = kind;
  328. return arc_linux_trap_s;
  329. }
  330. /* The linux target ops object. */
  331. linux_process_target *the_linux_target = &the_arc_target;
  332. void
  333. initialize_low_arch (void)
  334. {
  335. initialize_regsets_info (&arc_regsets_info);
  336. }