aarch64-linux-nat.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /* Native-dependent code for GNU/Linux AArch64.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by ARM Ltd.
  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 "defs.h"
  16. #include "inferior.h"
  17. #include "gdbcore.h"
  18. #include "regcache.h"
  19. #include "linux-nat.h"
  20. #include "target-descriptions.h"
  21. #include "auxv.h"
  22. #include "gdbcmd.h"
  23. #include "aarch64-nat.h"
  24. #include "aarch64-tdep.h"
  25. #include "aarch64-linux-tdep.h"
  26. #include "aarch32-linux-nat.h"
  27. #include "aarch32-tdep.h"
  28. #include "arch/arm.h"
  29. #include "nat/aarch64-linux.h"
  30. #include "nat/aarch64-linux-hw-point.h"
  31. #include "nat/aarch64-sve-linux-ptrace.h"
  32. #include "elf/external.h"
  33. #include "elf/common.h"
  34. #include "nat/gdb_ptrace.h"
  35. #include <sys/utsname.h>
  36. #include <asm/ptrace.h>
  37. #include "gregset.h"
  38. #include "linux-tdep.h"
  39. /* Defines ps_err_e, struct ps_prochandle. */
  40. #include "gdb_proc_service.h"
  41. #include "arch-utils.h"
  42. #include "arch/aarch64-mte-linux.h"
  43. #include "nat/aarch64-mte-linux-ptrace.h"
  44. #ifndef TRAP_HWBKPT
  45. #define TRAP_HWBKPT 0x0004
  46. #endif
  47. class aarch64_linux_nat_target final
  48. : public aarch64_nat_target<linux_nat_target>
  49. {
  50. public:
  51. /* Add our 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. /* Add our hardware breakpoint and watchpoint implementation. */
  56. bool stopped_by_watchpoint () override;
  57. bool stopped_data_address (CORE_ADDR *) override;
  58. int can_do_single_step () override;
  59. /* Override the GNU/Linux inferior startup hook. */
  60. void post_startup_inferior (ptid_t) override;
  61. /* Override the GNU/Linux post attach hook. */
  62. void post_attach (int pid) override;
  63. /* These three defer to common nat/ code. */
  64. void low_new_thread (struct lwp_info *lp) override
  65. { aarch64_linux_new_thread (lp); }
  66. void low_delete_thread (struct arch_lwp_info *lp) override
  67. { aarch64_linux_delete_thread (lp); }
  68. void low_prepare_to_resume (struct lwp_info *lp) override
  69. { aarch64_linux_prepare_to_resume (lp); }
  70. void low_new_fork (struct lwp_info *parent, pid_t child_pid) override;
  71. void low_forget_process (pid_t pid) override;
  72. /* Add our siginfo layout converter. */
  73. bool low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction)
  74. override;
  75. struct gdbarch *thread_architecture (ptid_t) override;
  76. bool supports_memory_tagging () override;
  77. /* Read memory allocation tags from memory via PTRACE. */
  78. bool fetch_memtags (CORE_ADDR address, size_t len,
  79. gdb::byte_vector &tags, int type) override;
  80. /* Write allocation tags to memory via PTRACE. */
  81. bool store_memtags (CORE_ADDR address, size_t len,
  82. const gdb::byte_vector &tags, int type) override;
  83. };
  84. static aarch64_linux_nat_target the_aarch64_linux_nat_target;
  85. /* Called whenever GDB is no longer debugging process PID. It deletes
  86. data structures that keep track of debug register state. */
  87. void
  88. aarch64_linux_nat_target::low_forget_process (pid_t pid)
  89. {
  90. aarch64_remove_debug_reg_state (pid);
  91. }
  92. /* Fill GDB's register array with the general-purpose register values
  93. from the current thread. */
  94. static void
  95. fetch_gregs_from_thread (struct regcache *regcache)
  96. {
  97. int ret, tid;
  98. struct gdbarch *gdbarch = regcache->arch ();
  99. elf_gregset_t regs;
  100. struct iovec iovec;
  101. /* Make sure REGS can hold all registers contents on both aarch64
  102. and arm. */
  103. gdb_static_assert (sizeof (regs) >= 18 * 4);
  104. tid = regcache->ptid ().lwp ();
  105. iovec.iov_base = &regs;
  106. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  107. iovec.iov_len = 18 * 4;
  108. else
  109. iovec.iov_len = sizeof (regs);
  110. ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
  111. if (ret < 0)
  112. perror_with_name (_("Unable to fetch general registers."));
  113. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  114. aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, 1);
  115. else
  116. {
  117. int regno;
  118. for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
  119. regcache->raw_supply (regno, &regs[regno - AARCH64_X0_REGNUM]);
  120. }
  121. }
  122. /* Store to the current thread the valid general-purpose register
  123. values in the GDB's register array. */
  124. static void
  125. store_gregs_to_thread (const struct regcache *regcache)
  126. {
  127. int ret, tid;
  128. elf_gregset_t regs;
  129. struct iovec iovec;
  130. struct gdbarch *gdbarch = regcache->arch ();
  131. /* Make sure REGS can hold all registers contents on both aarch64
  132. and arm. */
  133. gdb_static_assert (sizeof (regs) >= 18 * 4);
  134. tid = regcache->ptid ().lwp ();
  135. iovec.iov_base = &regs;
  136. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  137. iovec.iov_len = 18 * 4;
  138. else
  139. iovec.iov_len = sizeof (regs);
  140. ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
  141. if (ret < 0)
  142. perror_with_name (_("Unable to fetch general registers."));
  143. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  144. aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, 1);
  145. else
  146. {
  147. int regno;
  148. for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
  149. if (REG_VALID == regcache->get_register_status (regno))
  150. regcache->raw_collect (regno, &regs[regno - AARCH64_X0_REGNUM]);
  151. }
  152. ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
  153. if (ret < 0)
  154. perror_with_name (_("Unable to store general registers."));
  155. }
  156. /* Fill GDB's register array with the fp/simd register values
  157. from the current thread. */
  158. static void
  159. fetch_fpregs_from_thread (struct regcache *regcache)
  160. {
  161. int ret, tid;
  162. elf_fpregset_t regs;
  163. struct iovec iovec;
  164. struct gdbarch *gdbarch = regcache->arch ();
  165. /* Make sure REGS can hold all VFP registers contents on both aarch64
  166. and arm. */
  167. gdb_static_assert (sizeof regs >= ARM_VFP3_REGS_SIZE);
  168. tid = regcache->ptid ().lwp ();
  169. iovec.iov_base = &regs;
  170. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  171. {
  172. iovec.iov_len = ARM_VFP3_REGS_SIZE;
  173. ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
  174. if (ret < 0)
  175. perror_with_name (_("Unable to fetch VFP registers."));
  176. aarch32_vfp_regcache_supply (regcache, (gdb_byte *) &regs, 32);
  177. }
  178. else
  179. {
  180. int regno;
  181. iovec.iov_len = sizeof (regs);
  182. ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
  183. if (ret < 0)
  184. perror_with_name (_("Unable to fetch vFP/SIMD registers."));
  185. for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
  186. regcache->raw_supply (regno, &regs.vregs[regno - AARCH64_V0_REGNUM]);
  187. regcache->raw_supply (AARCH64_FPSR_REGNUM, &regs.fpsr);
  188. regcache->raw_supply (AARCH64_FPCR_REGNUM, &regs.fpcr);
  189. }
  190. }
  191. /* Store to the current thread the valid fp/simd register
  192. values in the GDB's register array. */
  193. static void
  194. store_fpregs_to_thread (const struct regcache *regcache)
  195. {
  196. int ret, tid;
  197. elf_fpregset_t regs;
  198. struct iovec iovec;
  199. struct gdbarch *gdbarch = regcache->arch ();
  200. /* Make sure REGS can hold all VFP registers contents on both aarch64
  201. and arm. */
  202. gdb_static_assert (sizeof regs >= ARM_VFP3_REGS_SIZE);
  203. tid = regcache->ptid ().lwp ();
  204. iovec.iov_base = &regs;
  205. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  206. {
  207. iovec.iov_len = ARM_VFP3_REGS_SIZE;
  208. ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
  209. if (ret < 0)
  210. perror_with_name (_("Unable to fetch VFP registers."));
  211. aarch32_vfp_regcache_collect (regcache, (gdb_byte *) &regs, 32);
  212. }
  213. else
  214. {
  215. int regno;
  216. iovec.iov_len = sizeof (regs);
  217. ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
  218. if (ret < 0)
  219. perror_with_name (_("Unable to fetch FP/SIMD registers."));
  220. for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
  221. if (REG_VALID == regcache->get_register_status (regno))
  222. regcache->raw_collect
  223. (regno, (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
  224. if (REG_VALID == regcache->get_register_status (AARCH64_FPSR_REGNUM))
  225. regcache->raw_collect (AARCH64_FPSR_REGNUM, (char *) &regs.fpsr);
  226. if (REG_VALID == regcache->get_register_status (AARCH64_FPCR_REGNUM))
  227. regcache->raw_collect (AARCH64_FPCR_REGNUM, (char *) &regs.fpcr);
  228. }
  229. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  230. {
  231. ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iovec);
  232. if (ret < 0)
  233. perror_with_name (_("Unable to store VFP registers."));
  234. }
  235. else
  236. {
  237. ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
  238. if (ret < 0)
  239. perror_with_name (_("Unable to store FP/SIMD registers."));
  240. }
  241. }
  242. /* Fill GDB's register array with the sve register values
  243. from the current thread. */
  244. static void
  245. fetch_sveregs_from_thread (struct regcache *regcache)
  246. {
  247. std::unique_ptr<gdb_byte[]> base
  248. = aarch64_sve_get_sveregs (regcache->ptid ().lwp ());
  249. aarch64_sve_regs_copy_to_reg_buf (regcache, base.get ());
  250. }
  251. /* Store to the current thread the valid sve register
  252. values in the GDB's register array. */
  253. static void
  254. store_sveregs_to_thread (struct regcache *regcache)
  255. {
  256. int ret;
  257. struct iovec iovec;
  258. int tid = regcache->ptid ().lwp ();
  259. /* First store vector length to the thread. This is done first to ensure the
  260. ptrace buffers read from the kernel are the correct size. */
  261. if (!aarch64_sve_set_vq (tid, regcache))
  262. perror_with_name (_("Unable to set VG register."));
  263. /* Obtain a dump of SVE registers from ptrace. */
  264. std::unique_ptr<gdb_byte[]> base = aarch64_sve_get_sveregs (tid);
  265. /* Overwrite with regcache state. */
  266. aarch64_sve_regs_copy_from_reg_buf (regcache, base.get ());
  267. /* Write back to the kernel. */
  268. iovec.iov_base = base.get ();
  269. iovec.iov_len = ((struct user_sve_header *) base.get ())->size;
  270. ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_SVE, &iovec);
  271. if (ret < 0)
  272. perror_with_name (_("Unable to store sve registers"));
  273. }
  274. /* Fill GDB's register array with the pointer authentication mask values from
  275. the current thread. */
  276. static void
  277. fetch_pauth_masks_from_thread (struct regcache *regcache)
  278. {
  279. aarch64_gdbarch_tdep *tdep
  280. = (aarch64_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  281. int ret;
  282. struct iovec iovec;
  283. uint64_t pauth_regset[2] = {0, 0};
  284. int tid = regcache->ptid ().lwp ();
  285. iovec.iov_base = &pauth_regset;
  286. iovec.iov_len = sizeof (pauth_regset);
  287. ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_PAC_MASK, &iovec);
  288. if (ret != 0)
  289. perror_with_name (_("unable to fetch pauth registers."));
  290. regcache->raw_supply (AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base),
  291. &pauth_regset[0]);
  292. regcache->raw_supply (AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base),
  293. &pauth_regset[1]);
  294. }
  295. /* Fill GDB's register array with the MTE register values from
  296. the current thread. */
  297. static void
  298. fetch_mteregs_from_thread (struct regcache *regcache)
  299. {
  300. aarch64_gdbarch_tdep *tdep
  301. = (aarch64_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  302. int regno = tdep->mte_reg_base;
  303. gdb_assert (regno != -1);
  304. uint64_t tag_ctl = 0;
  305. struct iovec iovec;
  306. iovec.iov_base = &tag_ctl;
  307. iovec.iov_len = sizeof (tag_ctl);
  308. int tid = get_ptrace_pid (regcache->ptid ());
  309. if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_TAGGED_ADDR_CTRL, &iovec) != 0)
  310. perror_with_name (_("unable to fetch MTE registers."));
  311. regcache->raw_supply (regno, &tag_ctl);
  312. }
  313. /* Store to the current thread the valid MTE register set in the GDB's
  314. register array. */
  315. static void
  316. store_mteregs_to_thread (struct regcache *regcache)
  317. {
  318. aarch64_gdbarch_tdep *tdep
  319. = (aarch64_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  320. int regno = tdep->mte_reg_base;
  321. gdb_assert (regno != -1);
  322. uint64_t tag_ctl = 0;
  323. if (REG_VALID != regcache->get_register_status (regno))
  324. return;
  325. regcache->raw_collect (regno, (char *) &tag_ctl);
  326. struct iovec iovec;
  327. iovec.iov_base = &tag_ctl;
  328. iovec.iov_len = sizeof (tag_ctl);
  329. int tid = get_ptrace_pid (regcache->ptid ());
  330. if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_TAGGED_ADDR_CTRL, &iovec) != 0)
  331. perror_with_name (_("unable to store MTE registers."));
  332. }
  333. /* Implement the "fetch_registers" target_ops method. */
  334. void
  335. aarch64_linux_nat_target::fetch_registers (struct regcache *regcache,
  336. int regno)
  337. {
  338. aarch64_gdbarch_tdep *tdep
  339. = (aarch64_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  340. if (regno == -1)
  341. {
  342. fetch_gregs_from_thread (regcache);
  343. if (tdep->has_sve ())
  344. fetch_sveregs_from_thread (regcache);
  345. else
  346. fetch_fpregs_from_thread (regcache);
  347. if (tdep->has_pauth ())
  348. fetch_pauth_masks_from_thread (regcache);
  349. if (tdep->has_mte ())
  350. fetch_mteregs_from_thread (regcache);
  351. }
  352. else if (regno < AARCH64_V0_REGNUM)
  353. fetch_gregs_from_thread (regcache);
  354. else if (tdep->has_sve ())
  355. fetch_sveregs_from_thread (regcache);
  356. else
  357. fetch_fpregs_from_thread (regcache);
  358. if (tdep->has_pauth ())
  359. {
  360. if (regno == AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base)
  361. || regno == AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base))
  362. fetch_pauth_masks_from_thread (regcache);
  363. }
  364. /* Fetch individual MTE registers. */
  365. if (tdep->has_mte ()
  366. && (regno == tdep->mte_reg_base))
  367. fetch_mteregs_from_thread (regcache);
  368. }
  369. /* Implement the "store_registers" target_ops method. */
  370. void
  371. aarch64_linux_nat_target::store_registers (struct regcache *regcache,
  372. int regno)
  373. {
  374. aarch64_gdbarch_tdep *tdep
  375. = (aarch64_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  376. if (regno == -1)
  377. {
  378. store_gregs_to_thread (regcache);
  379. if (tdep->has_sve ())
  380. store_sveregs_to_thread (regcache);
  381. else
  382. store_fpregs_to_thread (regcache);
  383. if (tdep->has_mte ())
  384. store_mteregs_to_thread (regcache);
  385. }
  386. else if (regno < AARCH64_V0_REGNUM)
  387. store_gregs_to_thread (regcache);
  388. else if (tdep->has_sve ())
  389. store_sveregs_to_thread (regcache);
  390. else
  391. store_fpregs_to_thread (regcache);
  392. /* Store MTE registers. */
  393. if (tdep->has_mte ()
  394. && (regno == tdep->mte_reg_base))
  395. store_mteregs_to_thread (regcache);
  396. }
  397. /* Fill register REGNO (if it is a general-purpose register) in
  398. *GREGSETPS with the value in GDB's register array. If REGNO is -1,
  399. do this for all registers. */
  400. void
  401. fill_gregset (const struct regcache *regcache,
  402. gdb_gregset_t *gregsetp, int regno)
  403. {
  404. regcache_collect_regset (&aarch64_linux_gregset, regcache,
  405. regno, (gdb_byte *) gregsetp,
  406. AARCH64_LINUX_SIZEOF_GREGSET);
  407. }
  408. /* Fill GDB's register array with the general-purpose register values
  409. in *GREGSETP. */
  410. void
  411. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  412. {
  413. regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
  414. (const gdb_byte *) gregsetp,
  415. AARCH64_LINUX_SIZEOF_GREGSET);
  416. }
  417. /* Fill register REGNO (if it is a floating-point register) in
  418. *FPREGSETP with the value in GDB's register array. If REGNO is -1,
  419. do this for all registers. */
  420. void
  421. fill_fpregset (const struct regcache *regcache,
  422. gdb_fpregset_t *fpregsetp, int regno)
  423. {
  424. regcache_collect_regset (&aarch64_linux_fpregset, regcache,
  425. regno, (gdb_byte *) fpregsetp,
  426. AARCH64_LINUX_SIZEOF_FPREGSET);
  427. }
  428. /* Fill GDB's register array with the floating-point register values
  429. in *FPREGSETP. */
  430. void
  431. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
  432. {
  433. regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
  434. (const gdb_byte *) fpregsetp,
  435. AARCH64_LINUX_SIZEOF_FPREGSET);
  436. }
  437. /* linux_nat_new_fork hook. */
  438. void
  439. aarch64_linux_nat_target::low_new_fork (struct lwp_info *parent,
  440. pid_t child_pid)
  441. {
  442. pid_t parent_pid;
  443. struct aarch64_debug_reg_state *parent_state;
  444. struct aarch64_debug_reg_state *child_state;
  445. /* NULL means no watchpoint has ever been set in the parent. In
  446. that case, there's nothing to do. */
  447. if (parent->arch_private == NULL)
  448. return;
  449. /* GDB core assumes the child inherits the watchpoints/hw
  450. breakpoints of the parent, and will remove them all from the
  451. forked off process. Copy the debug registers mirrors into the
  452. new process so that all breakpoints and watchpoints can be
  453. removed together. */
  454. parent_pid = parent->ptid.pid ();
  455. parent_state = aarch64_get_debug_reg_state (parent_pid);
  456. child_state = aarch64_get_debug_reg_state (child_pid);
  457. *child_state = *parent_state;
  458. }
  459. /* Called by libthread_db. Returns a pointer to the thread local
  460. storage (or its descriptor). */
  461. ps_err_e
  462. ps_get_thread_area (struct ps_prochandle *ph,
  463. lwpid_t lwpid, int idx, void **base)
  464. {
  465. int is_64bit_p
  466. = (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 64);
  467. return aarch64_ps_get_thread_area (ph, lwpid, idx, base, is_64bit_p);
  468. }
  469. /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
  470. void
  471. aarch64_linux_nat_target::post_startup_inferior (ptid_t ptid)
  472. {
  473. low_forget_process (ptid.pid ());
  474. aarch64_linux_get_debug_reg_capacity (ptid.pid ());
  475. linux_nat_target::post_startup_inferior (ptid);
  476. }
  477. /* Implement the "post_attach" target_ops method. */
  478. void
  479. aarch64_linux_nat_target::post_attach (int pid)
  480. {
  481. low_forget_process (pid);
  482. /* Set the hardware debug register capacity. If
  483. aarch64_linux_get_debug_reg_capacity is not called
  484. (as it is in aarch64_linux_child_post_startup_inferior) then
  485. software watchpoints will be used instead of hardware
  486. watchpoints when attaching to a target. */
  487. aarch64_linux_get_debug_reg_capacity (pid);
  488. linux_nat_target::post_attach (pid);
  489. }
  490. /* Implement the "read_description" target_ops method. */
  491. const struct target_desc *
  492. aarch64_linux_nat_target::read_description ()
  493. {
  494. int ret, tid;
  495. gdb_byte regbuf[ARM_VFP3_REGS_SIZE];
  496. struct iovec iovec;
  497. tid = inferior_ptid.pid ();
  498. iovec.iov_base = regbuf;
  499. iovec.iov_len = ARM_VFP3_REGS_SIZE;
  500. ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
  501. if (ret == 0)
  502. return aarch32_read_description ();
  503. CORE_ADDR hwcap = linux_get_hwcap (this);
  504. CORE_ADDR hwcap2 = linux_get_hwcap2 (this);
  505. bool pauth_p = hwcap & AARCH64_HWCAP_PACA;
  506. bool mte_p = hwcap2 & HWCAP2_MTE;
  507. return aarch64_read_description (aarch64_sve_get_vq (tid), pauth_p, mte_p);
  508. }
  509. /* Convert a native/host siginfo object, into/from the siginfo in the
  510. layout of the inferiors' architecture. Returns true if any
  511. conversion was done; false otherwise. If DIRECTION is 1, then copy
  512. from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
  513. INF. */
  514. bool
  515. aarch64_linux_nat_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
  516. int direction)
  517. {
  518. struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
  519. /* Is the inferior 32-bit? If so, then do fixup the siginfo
  520. object. */
  521. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  522. {
  523. if (direction == 0)
  524. aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo *) inf,
  525. native);
  526. else
  527. aarch64_siginfo_from_compat_siginfo (native,
  528. (struct compat_siginfo *) inf);
  529. return true;
  530. }
  531. return false;
  532. }
  533. /* Implement the "stopped_data_address" target_ops method. */
  534. bool
  535. aarch64_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
  536. {
  537. siginfo_t siginfo;
  538. struct aarch64_debug_reg_state *state;
  539. if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
  540. return false;
  541. /* This must be a hardware breakpoint. */
  542. if (siginfo.si_signo != SIGTRAP
  543. || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
  544. return false;
  545. /* Make sure to ignore the top byte, otherwise we may not recognize a
  546. hardware watchpoint hit. The stopped data addresses coming from the
  547. kernel can potentially be tagged addresses. */
  548. struct gdbarch *gdbarch = thread_architecture (inferior_ptid);
  549. const CORE_ADDR addr_trap
  550. = address_significant (gdbarch, (CORE_ADDR) siginfo.si_addr);
  551. /* Check if the address matches any watched address. */
  552. state = aarch64_get_debug_reg_state (inferior_ptid.pid ());
  553. return aarch64_stopped_data_address (state, addr_trap, addr_p);
  554. }
  555. /* Implement the "stopped_by_watchpoint" target_ops method. */
  556. bool
  557. aarch64_linux_nat_target::stopped_by_watchpoint ()
  558. {
  559. CORE_ADDR addr;
  560. return stopped_data_address (&addr);
  561. }
  562. /* Implement the "can_do_single_step" target_ops method. */
  563. int
  564. aarch64_linux_nat_target::can_do_single_step ()
  565. {
  566. return 1;
  567. }
  568. /* Implement the "thread_architecture" target_ops method. */
  569. struct gdbarch *
  570. aarch64_linux_nat_target::thread_architecture (ptid_t ptid)
  571. {
  572. /* Return the gdbarch for the current thread. If the vector length has
  573. changed since the last time this was called, then do a further lookup. */
  574. uint64_t vq = aarch64_sve_get_vq (ptid.lwp ());
  575. /* Find the current gdbarch the same way as process_stratum_target. Only
  576. return it if the current vector length matches the one in the tdep. */
  577. inferior *inf = find_inferior_ptid (this, ptid);
  578. gdb_assert (inf != NULL);
  579. aarch64_gdbarch_tdep *tdep
  580. = (aarch64_gdbarch_tdep *) gdbarch_tdep (inf->gdbarch);
  581. if (vq == tdep->vq)
  582. return inf->gdbarch;
  583. /* We reach here if the vector length for the thread is different from its
  584. value at process start. Lookup gdbarch via info (potentially creating a
  585. new one), stashing the vector length inside id. Use -1 for when SVE
  586. unavailable, to distinguish from an unset value of 0. */
  587. struct gdbarch_info info;
  588. info.bfd_arch_info = bfd_lookup_arch (bfd_arch_aarch64, bfd_mach_aarch64);
  589. info.id = (int *) (vq == 0 ? -1 : vq);
  590. return gdbarch_find_by_info (info);
  591. }
  592. /* Implement the "supports_memory_tagging" target_ops method. */
  593. bool
  594. aarch64_linux_nat_target::supports_memory_tagging ()
  595. {
  596. return (linux_get_hwcap2 (this) & HWCAP2_MTE) != 0;
  597. }
  598. /* Implement the "fetch_memtags" target_ops method. */
  599. bool
  600. aarch64_linux_nat_target::fetch_memtags (CORE_ADDR address, size_t len,
  601. gdb::byte_vector &tags, int type)
  602. {
  603. int tid = get_ptrace_pid (inferior_ptid);
  604. /* Allocation tags? */
  605. if (type == static_cast<int> (aarch64_memtag_type::mte_allocation))
  606. return aarch64_mte_fetch_memtags (tid, address, len, tags);
  607. return false;
  608. }
  609. /* Implement the "store_memtags" target_ops method. */
  610. bool
  611. aarch64_linux_nat_target::store_memtags (CORE_ADDR address, size_t len,
  612. const gdb::byte_vector &tags, int type)
  613. {
  614. int tid = get_ptrace_pid (inferior_ptid);
  615. /* Allocation tags? */
  616. if (type == static_cast<int> (aarch64_memtag_type::mte_allocation))
  617. return aarch64_mte_store_memtags (tid, address, len, tags);
  618. return false;
  619. }
  620. void _initialize_aarch64_linux_nat ();
  621. void
  622. _initialize_aarch64_linux_nat ()
  623. {
  624. aarch64_initialize_hw_point ();
  625. /* Register the target. */
  626. linux_target = &the_aarch64_linux_nat_target;
  627. add_inf_child_target (&the_aarch64_linux_nat_target);
  628. }