arc-linux-nat.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* Native-dependent code 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 "defs.h"
  15. #include "frame.h"
  16. #include "inferior.h"
  17. #include "gdbcore.h"
  18. #include "regcache.h"
  19. #include "gdbsupport/gdb_assert.h"
  20. #include "target.h"
  21. #include "linux-nat.h"
  22. #include "nat/gdb_ptrace.h"
  23. #include <stdint.h>
  24. #include <sys/types.h>
  25. #include <sys/param.h>
  26. #include <signal.h>
  27. #include <sys/user.h>
  28. #include <sys/ioctl.h>
  29. #include "gdbsupport/gdb_wait.h"
  30. #include <fcntl.h>
  31. #include <sys/procfs.h>
  32. #include <linux/elf.h>
  33. #include "gregset.h"
  34. #include "arc-tdep.h"
  35. #include "arc-linux-tdep.h"
  36. #include "arch/arc.h"
  37. /* Defines ps_err_e, struct ps_prochandle. */
  38. #include "gdb_proc_service.h"
  39. /* Print an "arc-linux-nat" debug statement. */
  40. #define arc_linux_nat_debug_printf(fmt, ...) \
  41. debug_prefixed_printf_cond (arc_debug, "arc-linux-nat", fmt, ##__VA_ARGS__)
  42. /* Linux starting with 4.12 supports NT_ARC_V2 note type, which adds R30,
  43. R58 and R59 registers, which are specific to ARC HS and aren't
  44. available in ARC 700. */
  45. #if defined (NT_ARC_V2) && defined (__ARCHS__)
  46. #define ARC_HAS_V2_REGSET
  47. #endif
  48. class arc_linux_nat_target final : public linux_nat_target
  49. {
  50. public:
  51. /* Add ARC register access methods. */
  52. void fetch_registers (struct regcache *, int) override;
  53. void store_registers (struct regcache *, int) override;
  54. const struct target_desc *read_description () override;
  55. /* Handle threads */
  56. void low_prepare_to_resume (struct lwp_info *lp) override;
  57. };
  58. static arc_linux_nat_target the_arc_linux_nat_target;
  59. /* Read general registers from target process/thread (via ptrace)
  60. into REGCACHE. */
  61. static void
  62. fetch_gregs (struct regcache *regcache, int regnum)
  63. {
  64. const int tid = get_ptrace_pid (regcache->ptid ());
  65. struct iovec iov;
  66. gdb_gregset_t regs;
  67. iov.iov_base = &regs;
  68. iov.iov_len = sizeof (gdb_gregset_t);
  69. if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, (void *) &iov) < 0)
  70. perror_with_name (_("Couldn't get general registers"));
  71. else
  72. arc_linux_supply_gregset (NULL, regcache, regnum, &regs, 0);
  73. }
  74. #ifdef ARC_HAS_V2_REGSET
  75. /* Read ARC v2 registers from target process/thread (via ptrace)
  76. into REGCACHE. */
  77. static void
  78. fetch_v2_regs (struct regcache *regcache, int regnum)
  79. {
  80. const int tid = get_ptrace_pid (regcache->ptid ());
  81. struct iovec iov;
  82. bfd_byte v2_buffer[ARC_LINUX_SIZEOF_V2_REGSET];
  83. iov.iov_base = &v2_buffer;
  84. iov.iov_len = ARC_LINUX_SIZEOF_V2_REGSET;
  85. if (ptrace (PTRACE_GETREGSET, tid, NT_ARC_V2, (void *) &iov) < 0)
  86. perror_with_name (_("Couldn't get ARC HS registers"));
  87. else
  88. arc_linux_supply_v2_regset (NULL, regcache, regnum, v2_buffer, 0);
  89. }
  90. #endif
  91. /* Store general registers from REGCACHE into the target process/thread. */
  92. static void
  93. store_gregs (const struct regcache *regcache, int regnum)
  94. {
  95. const int tid = get_ptrace_pid (regcache->ptid ());
  96. struct iovec iov;
  97. gdb_gregset_t regs;
  98. iov.iov_base = &regs;
  99. iov.iov_len = sizeof (gdb_gregset_t);
  100. if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, (void *) &iov) < 0)
  101. perror_with_name (_("Couldn't get general registers"));
  102. else
  103. {
  104. arc_linux_collect_gregset (NULL, regcache, regnum, regs, 0);
  105. if (ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, (void *) &iov) < 0)
  106. perror_with_name (_("Couldn't write general registers"));
  107. }
  108. }
  109. #ifdef ARC_HAS_V2_REGSET
  110. /* Store ARC v2 registers from REGCACHE into the target process/thread. */
  111. static void
  112. store_v2_regs (const struct regcache *regcache, int regnum)
  113. {
  114. const int tid = get_ptrace_pid (regcache->ptid ());
  115. struct iovec iov;
  116. bfd_byte v2_buffer[ARC_LINUX_SIZEOF_V2_REGSET];
  117. iov.iov_base = &v2_buffer;
  118. iov.iov_len = ARC_LINUX_SIZEOF_V2_REGSET;
  119. if (ptrace (PTRACE_GETREGSET, tid, NT_ARC_V2, (void *) &iov) < 0)
  120. perror_with_name (_("Couldn't get ARC HS registers"));
  121. else
  122. {
  123. arc_linux_collect_v2_regset (NULL, regcache, regnum, v2_buffer, 0);
  124. if (ptrace (PTRACE_SETREGSET, tid, NT_ARC_V2, (void *) &iov) < 0)
  125. perror_with_name (_("Couldn't write ARC HS registers"));
  126. }
  127. }
  128. #endif
  129. /* Target operation: Read REGNUM register (all registers if REGNUM == -1)
  130. from target process into REGCACHE. */
  131. void
  132. arc_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  133. {
  134. if (regnum == -1 || regnum <= ARC_LAST_REGNUM)
  135. fetch_gregs (regcache, regnum);
  136. #ifdef ARC_HAS_V2_REGSET
  137. if (regnum == -1
  138. || regnum == ARC_R30_REGNUM
  139. || regnum == ARC_R58_REGNUM
  140. || regnum == ARC_R59_REGNUM)
  141. fetch_v2_regs (regcache, regnum);
  142. #endif
  143. }
  144. /* Target operation: Store REGNUM register (all registers if REGNUM == -1)
  145. to the target process from REGCACHE. */
  146. void
  147. arc_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
  148. {
  149. if (regnum == -1 || regnum <= ARC_LAST_REGNUM)
  150. store_gregs (regcache, regnum);
  151. #ifdef ARC_HAS_V2_REGSET
  152. if (regnum == -1
  153. || regnum == ARC_R30_REGNUM
  154. || regnum == ARC_R58_REGNUM
  155. || regnum == ARC_R59_REGNUM)
  156. store_v2_regs (regcache, regnum);
  157. #endif
  158. }
  159. /* Copy general purpose register(s) from REGCACHE into regset GREGS.
  160. This function is exported to proc-service.c */
  161. void
  162. fill_gregset (const struct regcache *regcache,
  163. gdb_gregset_t *gregs, int regnum)
  164. {
  165. arc_linux_collect_gregset (NULL, regcache, regnum, gregs, 0);
  166. }
  167. /* Copy all the general purpose registers from regset GREGS into REGCACHE.
  168. This function is exported to proc-service.c. */
  169. void
  170. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs)
  171. {
  172. arc_linux_supply_gregset (NULL, regcache, -1, gregs, 0);
  173. }
  174. /* ARC doesn't have separate FP registers. This function is exported
  175. to proc-service.c. */
  176. void
  177. fill_fpregset (const struct regcache *regcache,
  178. gdb_fpregset_t *fpregsetp, int regnum)
  179. {
  180. arc_linux_nat_debug_printf ("called");
  181. }
  182. /* ARC doesn't have separate FP registers. This function is exported
  183. to proc-service.c. */
  184. void
  185. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
  186. {
  187. arc_linux_nat_debug_printf ("called");
  188. }
  189. /* Implement the "read_description" method of linux_nat_target. */
  190. const struct target_desc *
  191. arc_linux_nat_target::read_description ()
  192. {
  193. /* This is a native target, hence description is hardcoded. */
  194. #ifdef __ARCHS__
  195. arc_arch_features features (4, ARC_ISA_ARCV2);
  196. #else
  197. arc_arch_features features (4, ARC_ISA_ARCV1);
  198. #endif
  199. return arc_lookup_target_description (features);
  200. }
  201. /* As described in arc_linux_collect_gregset(), we need to write resume-PC
  202. to ERET. However by default GDB for native targets doesn't write
  203. registers if they haven't been changed. This is a callback called by
  204. generic GDB, and in this callback we have to rewrite PC value so it
  205. would force rewrite of register on target. It seems that the only
  206. other arch that utilizes this hook is x86/x86-64 for HW breakpoint
  207. support. But then, AFAIK no other arch has this stop_pc/eret
  208. complexity.
  209. No better way was found, other than this fake write of register value,
  210. to force GDB into writing register to target. Is there any? */
  211. void
  212. arc_linux_nat_target::low_prepare_to_resume (struct lwp_info *lwp)
  213. {
  214. /* When new processes and threads are created we do not have the address
  215. space for them and calling get_thread_regcache will cause an internal
  216. error in GDB. It looks like that checking for last_resume_kind is the
  217. sensible way to determine processes for which we cannot get regcache.
  218. Ultimately, a better way would be removing the need for
  219. low_prepare_to_resume in the first place. */
  220. if (lwp->last_resume_kind == resume_stop)
  221. return;
  222. struct regcache *regcache = get_thread_regcache (this, lwp->ptid);
  223. struct gdbarch *gdbarch = regcache->arch ();
  224. /* Read current PC value, then write it back. It is required to call
  225. invalidate(), otherwise GDB will note that new value is equal to old
  226. value and will skip write. */
  227. ULONGEST new_pc;
  228. regcache_cooked_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch),
  229. &new_pc);
  230. regcache->invalidate (gdbarch_pc_regnum (gdbarch));
  231. regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch),
  232. new_pc);
  233. }
  234. /* Fetch the thread-local storage pointer for libthread_db. Note that
  235. this function is not called from GDB, but is called from libthread_db.
  236. This is required to debug multithreaded applications with NPTL. */
  237. ps_err_e
  238. ps_get_thread_area (struct ps_prochandle *ph, lwpid_t lwpid, int idx,
  239. void **base)
  240. {
  241. arc_linux_nat_debug_printf ("called");
  242. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  243. return PS_ERR;
  244. /* IDX is the bias from the thread pointer to the beginning of the
  245. thread descriptor. It has to be subtracted due to implementation
  246. quirks in libthread_db. */
  247. *base = (void *) ((char *) *base - idx);
  248. return PS_OK;
  249. }
  250. /* Suppress warning from -Wmissing-prototypes. */
  251. void _initialize_arc_linux_nat ();
  252. void
  253. _initialize_arc_linux_nat ()
  254. {
  255. /* Register the target. */
  256. linux_target = &the_arc_linux_nat_target;
  257. add_inf_child_target (&the_arc_linux_nat_target);
  258. }