Revert "MIPS: Use N64 by default for mips*64*-*-linux-gnuabi64"
[binutils-gdb.git] / gdb / aarch64-linux-nat.c
blob2e6541f53c3c7d0563d0361be1cfc6147127c49c
1 /* Native-dependent code for GNU/Linux AArch64.
3 Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 Contributed by ARM Ltd.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "linux-nat.h"
26 #include "target-descriptions.h"
27 #include "auxv.h"
28 #include "cli/cli-cmds.h"
29 #include "aarch64-nat.h"
30 #include "aarch64-tdep.h"
31 #include "aarch64-linux-tdep.h"
32 #include "aarch32-linux-nat.h"
33 #include "aarch32-tdep.h"
34 #include "arch/arm.h"
35 #include "nat/aarch64-linux.h"
36 #include "nat/aarch64-linux-hw-point.h"
37 #include "nat/aarch64-scalable-linux-ptrace.h"
39 #include "elf/external.h"
40 #include "elf/common.h"
42 #include "nat/gdb_ptrace.h"
43 #include <sys/utsname.h>
44 #include <asm/ptrace.h>
46 #include "gregset.h"
47 #include "linux-tdep.h"
48 #include "arm-tdep.h"
50 /* Defines ps_err_e, struct ps_prochandle. */
51 #include "gdb_proc_service.h"
52 #include "arch-utils.h"
54 #include "arch/aarch64-mte-linux.h"
56 #include "nat/aarch64-mte-linux-ptrace.h"
57 #include "arch/aarch64-scalable-linux.h"
59 #include <string.h>
61 #ifndef TRAP_HWBKPT
62 #define TRAP_HWBKPT 0x0004
63 #endif
65 class aarch64_linux_nat_target final
66 : public aarch64_nat_target<linux_nat_target>
68 public:
69 /* Add our register access methods. */
70 void fetch_registers (struct regcache *, int) override;
71 void store_registers (struct regcache *, int) override;
73 const struct target_desc *read_description () override;
75 /* Add our hardware breakpoint and watchpoint implementation. */
76 bool stopped_by_watchpoint () override;
77 bool stopped_data_address (CORE_ADDR *) override;
79 int can_do_single_step () override;
81 /* These three defer to common nat/ code. */
82 void low_new_thread (struct lwp_info *lp) override
83 { aarch64_linux_new_thread (lp); }
84 void low_delete_thread (struct arch_lwp_info *lp) override
85 { aarch64_linux_delete_thread (lp); }
86 void low_prepare_to_resume (struct lwp_info *lp) override
87 { aarch64_linux_prepare_to_resume (lp); }
89 void low_new_fork (struct lwp_info *parent, pid_t child_pid) override;
90 void low_init_process (pid_t pid) override;
91 void low_forget_process (pid_t pid) override;
93 /* Add our siginfo layout converter. */
94 bool low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction)
95 override;
97 struct gdbarch *thread_architecture (ptid_t) override;
99 bool supports_memory_tagging () override;
101 /* Read memory allocation tags from memory via PTRACE. */
102 bool fetch_memtags (CORE_ADDR address, size_t len,
103 gdb::byte_vector &tags, int type) override;
105 /* Write allocation tags to memory via PTRACE. */
106 bool store_memtags (CORE_ADDR address, size_t len,
107 const gdb::byte_vector &tags, int type) override;
108 /* Check if an address is tagged. */
109 bool is_address_tagged (gdbarch *gdbarch, CORE_ADDR address) override;
112 static aarch64_linux_nat_target the_aarch64_linux_nat_target;
114 /* Called whenever GDB is no longer debugging process PID. It deletes
115 data structures that keep track of debug register state. */
117 void
118 aarch64_linux_nat_target::low_forget_process (pid_t pid)
120 aarch64_remove_debug_reg_state (pid);
123 /* Fill GDB's register array with the general-purpose register values
124 from the current thread. */
126 static void
127 fetch_gregs_from_thread (struct regcache *regcache)
129 int ret, tid;
130 struct gdbarch *gdbarch = regcache->arch ();
131 elf_gregset_t regs;
132 struct iovec iovec;
134 /* Make sure REGS can hold all registers contents on both aarch64
135 and arm. */
136 static_assert (sizeof (regs) >= 18 * 4);
138 tid = regcache->ptid ().lwp ();
140 iovec.iov_base = &regs;
141 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
142 iovec.iov_len = 18 * 4;
143 else
144 iovec.iov_len = sizeof (regs);
146 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
147 if (ret < 0)
148 perror_with_name (_("Unable to fetch general registers"));
150 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
151 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, 1);
152 else
154 int regno;
156 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
157 regcache->raw_supply (regno, &regs[regno - AARCH64_X0_REGNUM]);
161 /* Store to the current thread the valid general-purpose register
162 values in the GDB's register array. */
164 static void
165 store_gregs_to_thread (const struct regcache *regcache)
167 int ret, tid;
168 elf_gregset_t regs;
169 struct iovec iovec;
170 struct gdbarch *gdbarch = regcache->arch ();
172 /* Make sure REGS can hold all registers contents on both aarch64
173 and arm. */
174 static_assert (sizeof (regs) >= 18 * 4);
175 tid = regcache->ptid ().lwp ();
177 iovec.iov_base = &regs;
178 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
179 iovec.iov_len = 18 * 4;
180 else
181 iovec.iov_len = sizeof (regs);
183 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
184 if (ret < 0)
185 perror_with_name (_("Unable to fetch general registers"));
187 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
188 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, 1);
189 else
191 int regno;
193 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
194 if (REG_VALID == regcache->get_register_status (regno))
195 regcache->raw_collect (regno, &regs[regno - AARCH64_X0_REGNUM]);
198 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
199 if (ret < 0)
200 perror_with_name (_("Unable to store general registers"));
203 /* Fill GDB's register array with the fp/simd register values
204 from the current thread. */
206 static void
207 fetch_fpregs_from_thread (struct regcache *regcache)
209 int ret, tid;
210 elf_fpregset_t regs;
211 struct iovec iovec;
212 struct gdbarch *gdbarch = regcache->arch ();
214 /* Make sure REGS can hold all VFP registers contents on both aarch64
215 and arm. */
216 static_assert (sizeof regs >= ARM_VFP3_REGS_SIZE);
218 tid = regcache->ptid ().lwp ();
220 iovec.iov_base = &regs;
222 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
224 iovec.iov_len = ARM_VFP3_REGS_SIZE;
226 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
227 if (ret < 0)
228 perror_with_name (_("Unable to fetch VFP registers"));
230 aarch32_vfp_regcache_supply (regcache, (gdb_byte *) &regs, 32);
232 else
234 int regno;
236 iovec.iov_len = sizeof (regs);
238 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
239 if (ret < 0)
240 perror_with_name (_("Unable to fetch vFP/SIMD registers"));
242 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
243 regcache->raw_supply (regno, &regs.vregs[regno - AARCH64_V0_REGNUM]);
245 regcache->raw_supply (AARCH64_FPSR_REGNUM, &regs.fpsr);
246 regcache->raw_supply (AARCH64_FPCR_REGNUM, &regs.fpcr);
250 /* Store to the current thread the valid fp/simd register
251 values in the GDB's register array. */
253 static void
254 store_fpregs_to_thread (const struct regcache *regcache)
256 int ret, tid;
257 elf_fpregset_t regs;
258 struct iovec iovec;
259 struct gdbarch *gdbarch = regcache->arch ();
261 /* Make sure REGS can hold all VFP registers contents on both aarch64
262 and arm. */
263 static_assert (sizeof regs >= ARM_VFP3_REGS_SIZE);
264 tid = regcache->ptid ().lwp ();
266 iovec.iov_base = &regs;
268 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
270 iovec.iov_len = ARM_VFP3_REGS_SIZE;
272 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
273 if (ret < 0)
274 perror_with_name (_("Unable to fetch VFP registers"));
276 aarch32_vfp_regcache_collect (regcache, (gdb_byte *) &regs, 32);
278 else
280 int regno;
282 iovec.iov_len = sizeof (regs);
284 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
285 if (ret < 0)
286 perror_with_name (_("Unable to fetch FP/SIMD registers"));
288 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
289 if (REG_VALID == regcache->get_register_status (regno))
290 regcache->raw_collect
291 (regno, (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
293 if (REG_VALID == regcache->get_register_status (AARCH64_FPSR_REGNUM))
294 regcache->raw_collect (AARCH64_FPSR_REGNUM, (char *) &regs.fpsr);
295 if (REG_VALID == regcache->get_register_status (AARCH64_FPCR_REGNUM))
296 regcache->raw_collect (AARCH64_FPCR_REGNUM, (char *) &regs.fpcr);
299 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
301 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iovec);
302 if (ret < 0)
303 perror_with_name (_("Unable to store VFP registers"));
305 else
307 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
308 if (ret < 0)
309 perror_with_name (_("Unable to store FP/SIMD registers"));
313 /* Fill GDB's REGCACHE with the valid SVE register values from the thread
314 associated with REGCACHE.
316 This function handles reading data from SVE or SSVE states, depending
317 on which state is active at the moment. */
319 static void
320 fetch_sveregs_from_thread (struct regcache *regcache)
322 /* Fetch SVE state from the thread and copy it into the register cache. */
323 aarch64_sve_regs_copy_to_reg_buf (regcache->ptid ().lwp (), regcache);
326 /* Store the valid SVE register values from GDB's REGCACHE to the thread
327 associated with REGCACHE.
329 This function handles writing data to SVE or SSVE states, depending
330 on which state is active at the moment. */
332 static void
333 store_sveregs_to_thread (struct regcache *regcache)
335 /* Fetch SVE state from the register cache and update the thread TID with
336 it. */
337 aarch64_sve_regs_copy_from_reg_buf (regcache->ptid ().lwp (), regcache);
340 /* Fill GDB's REGCACHE with the ZA register set contents from the
341 thread associated with REGCACHE. If there is no active ZA register state,
342 make the ZA register contents zero. */
344 static void
345 fetch_za_from_thread (struct regcache *regcache)
347 aarch64_gdbarch_tdep *tdep
348 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
350 /* Read ZA state from the thread to the register cache. */
351 aarch64_za_regs_copy_to_reg_buf (regcache->ptid ().lwp (),
352 regcache,
353 tdep->sme_za_regnum,
354 tdep->sme_svg_regnum,
355 tdep->sme_svcr_regnum);
358 /* Store the NT_ARM_ZA register set contents from GDB's REGCACHE to the thread
359 associated with REGCACHE. */
361 static void
362 store_za_to_thread (struct regcache *regcache)
364 aarch64_gdbarch_tdep *tdep
365 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
367 /* Write ZA state from the register cache to the thread. */
368 aarch64_za_regs_copy_from_reg_buf (regcache->ptid ().lwp (),
369 regcache,
370 tdep->sme_za_regnum,
371 tdep->sme_svg_regnum,
372 tdep->sme_svcr_regnum);
375 /* Fill GDB's REGCACHE with the ZT register set contents from the
376 thread associated with REGCACHE. If there is no active ZA register state,
377 make the ZT register contents zero. */
379 static void
380 fetch_zt_from_thread (struct regcache *regcache)
382 aarch64_gdbarch_tdep *tdep
383 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
385 /* Read ZT state from the thread to the register cache. */
386 aarch64_zt_regs_copy_to_reg_buf (regcache->ptid ().lwp (),
387 regcache,
388 tdep->sme2_zt0_regnum);
391 /* Store the NT_ARM_ZT register set contents from GDB's REGCACHE to the
392 thread associated with REGCACHE. */
394 static void
395 store_zt_to_thread (struct regcache *regcache)
397 aarch64_gdbarch_tdep *tdep
398 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
400 /* Write ZT state from the register cache to the thread. */
401 aarch64_zt_regs_copy_from_reg_buf (regcache->ptid ().lwp (),
402 regcache,
403 tdep->sme2_zt0_regnum);
406 /* Fill GDB's register array with the pointer authentication mask values from
407 the current thread. */
409 static void
410 fetch_pauth_masks_from_thread (struct regcache *regcache)
412 aarch64_gdbarch_tdep *tdep
413 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
414 int ret;
415 struct iovec iovec;
416 uint64_t pauth_regset[2] = {0, 0};
417 int tid = regcache->ptid ().lwp ();
419 iovec.iov_base = &pauth_regset;
420 iovec.iov_len = sizeof (pauth_regset);
422 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_PAC_MASK, &iovec);
423 if (ret != 0)
424 perror_with_name (_("unable to fetch pauth registers"));
426 regcache->raw_supply (AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base),
427 &pauth_regset[0]);
428 regcache->raw_supply (AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base),
429 &pauth_regset[1]);
432 /* Fill GDB's register array with the MTE register values from
433 the current thread. */
435 static void
436 fetch_mteregs_from_thread (struct regcache *regcache)
438 aarch64_gdbarch_tdep *tdep
439 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
440 int regno = tdep->mte_reg_base;
442 gdb_assert (regno != -1);
444 uint64_t tag_ctl = 0;
445 struct iovec iovec;
447 iovec.iov_base = &tag_ctl;
448 iovec.iov_len = sizeof (tag_ctl);
450 int tid = get_ptrace_pid (regcache->ptid ());
451 if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_TAGGED_ADDR_CTRL, &iovec) != 0)
452 perror_with_name (_("unable to fetch MTE registers"));
454 regcache->raw_supply (regno, &tag_ctl);
457 /* Store to the current thread the valid MTE register set in the GDB's
458 register array. */
460 static void
461 store_mteregs_to_thread (struct regcache *regcache)
463 aarch64_gdbarch_tdep *tdep
464 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
465 int regno = tdep->mte_reg_base;
467 gdb_assert (regno != -1);
469 uint64_t tag_ctl = 0;
471 if (REG_VALID != regcache->get_register_status (regno))
472 return;
474 regcache->raw_collect (regno, (char *) &tag_ctl);
476 struct iovec iovec;
478 iovec.iov_base = &tag_ctl;
479 iovec.iov_len = sizeof (tag_ctl);
481 int tid = get_ptrace_pid (regcache->ptid ());
482 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_TAGGED_ADDR_CTRL, &iovec) != 0)
483 perror_with_name (_("unable to store MTE registers"));
486 /* Fill GDB's register array with the TLS register values from
487 the current thread. */
489 static void
490 fetch_tlsregs_from_thread (struct regcache *regcache)
492 aarch64_gdbarch_tdep *tdep
493 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
494 int regno = tdep->tls_regnum_base;
496 gdb_assert (regno != -1);
497 gdb_assert (tdep->tls_register_count > 0);
499 uint64_t tpidrs[tdep->tls_register_count];
500 memset(tpidrs, 0, sizeof(tpidrs));
502 struct iovec iovec;
503 iovec.iov_base = tpidrs;
504 iovec.iov_len = sizeof (tpidrs);
506 int tid = get_ptrace_pid (regcache->ptid ());
507 if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_TLS, &iovec) != 0)
508 perror_with_name (_("unable to fetch TLS registers"));
510 for (int i = 0; i < tdep->tls_register_count; i++)
511 regcache->raw_supply (regno + i, &tpidrs[i]);
514 /* Store to the current thread the valid TLS register set in GDB's
515 register array. */
517 static void
518 store_tlsregs_to_thread (struct regcache *regcache)
520 aarch64_gdbarch_tdep *tdep
521 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
522 int regno = tdep->tls_regnum_base;
524 gdb_assert (regno != -1);
525 gdb_assert (tdep->tls_register_count > 0);
527 uint64_t tpidrs[tdep->tls_register_count];
528 memset(tpidrs, 0, sizeof(tpidrs));
530 for (int i = 0; i < tdep->tls_register_count; i++)
532 if (REG_VALID != regcache->get_register_status (regno + i))
533 continue;
535 regcache->raw_collect (regno + i, (char *) &tpidrs[i]);
538 struct iovec iovec;
539 iovec.iov_base = &tpidrs;
540 iovec.iov_len = sizeof (tpidrs);
542 int tid = get_ptrace_pid (regcache->ptid ());
543 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_TLS, &iovec) != 0)
544 perror_with_name (_("unable to store TLS register"));
547 /* The AArch64 version of the "fetch_registers" target_ops method. Fetch
548 REGNO from the target and place the result into REGCACHE. */
550 static void
551 aarch64_fetch_registers (struct regcache *regcache, int regno)
553 aarch64_gdbarch_tdep *tdep
554 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
556 /* Do we need to fetch all registers? */
557 if (regno == -1)
559 fetch_gregs_from_thread (regcache);
561 /* We attempt to fetch SVE registers if there is support for either
562 SVE or SME (due to the SSVE state of SME). */
563 if (tdep->has_sve () || tdep->has_sme ())
564 fetch_sveregs_from_thread (regcache);
565 else
566 fetch_fpregs_from_thread (regcache);
568 if (tdep->has_pauth ())
569 fetch_pauth_masks_from_thread (regcache);
571 if (tdep->has_mte ())
572 fetch_mteregs_from_thread (regcache);
574 if (tdep->has_tls ())
575 fetch_tlsregs_from_thread (regcache);
577 if (tdep->has_sme ())
578 fetch_za_from_thread (regcache);
580 if (tdep->has_sme2 ())
581 fetch_zt_from_thread (regcache);
583 /* General purpose register? */
584 else if (regno < AARCH64_V0_REGNUM)
585 fetch_gregs_from_thread (regcache);
586 /* SVE register? */
587 else if ((tdep->has_sve () || tdep->has_sme ())
588 && regno <= AARCH64_SVE_VG_REGNUM)
589 fetch_sveregs_from_thread (regcache);
590 /* FPSIMD register? */
591 else if (regno <= AARCH64_FPCR_REGNUM)
592 fetch_fpregs_from_thread (regcache);
593 /* PAuth register? */
594 else if (tdep->has_pauth ()
595 && (regno == AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base)
596 || regno == AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base)))
597 fetch_pauth_masks_from_thread (regcache);
598 /* SME register? */
599 else if (tdep->has_sme () && regno >= tdep->sme_reg_base
600 && regno < tdep->sme_reg_base + 3)
601 fetch_za_from_thread (regcache);
602 /* SME2 register? */
603 else if (tdep->has_sme2 () && regno == tdep->sme2_zt0_regnum)
604 fetch_zt_from_thread (regcache);
605 /* MTE register? */
606 else if (tdep->has_mte ()
607 && (regno == tdep->mte_reg_base))
608 fetch_mteregs_from_thread (regcache);
609 /* TLS register? */
610 else if (tdep->has_tls ()
611 && regno >= tdep->tls_regnum_base
612 && regno < tdep->tls_regnum_base + tdep->tls_register_count)
613 fetch_tlsregs_from_thread (regcache);
616 /* A version of the "fetch_registers" target_ops method used when running
617 32-bit ARM code on an AArch64 target. Fetch REGNO from the target and
618 place the result into REGCACHE. */
620 static void
621 aarch32_fetch_registers (struct regcache *regcache, int regno)
623 arm_gdbarch_tdep *tdep
624 = gdbarch_tdep<arm_gdbarch_tdep> (regcache->arch ());
626 if (regno == -1)
628 fetch_gregs_from_thread (regcache);
629 if (tdep->vfp_register_count > 0)
630 fetch_fpregs_from_thread (regcache);
632 else if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
633 fetch_gregs_from_thread (regcache);
634 else if (tdep->vfp_register_count > 0
635 && regno >= ARM_D0_REGNUM
636 && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
637 || regno == ARM_FPSCR_REGNUM))
638 fetch_fpregs_from_thread (regcache);
641 /* Implement the "fetch_registers" target_ops method. */
643 void
644 aarch64_linux_nat_target::fetch_registers (struct regcache *regcache,
645 int regno)
647 if (gdbarch_bfd_arch_info (regcache->arch ())->bits_per_word == 32)
648 aarch32_fetch_registers (regcache, regno);
649 else
650 aarch64_fetch_registers (regcache, regno);
653 /* The AArch64 version of the "store_registers" target_ops method. Copy
654 the value of register REGNO from REGCACHE into the the target. */
656 static void
657 aarch64_store_registers (struct regcache *regcache, int regno)
659 aarch64_gdbarch_tdep *tdep
660 = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
662 /* Do we need to store all registers? */
663 if (regno == -1)
665 store_gregs_to_thread (regcache);
667 /* We attempt to store SVE registers if there is support for either
668 SVE or SME (due to the SSVE state of SME). */
669 if (tdep->has_sve () || tdep->has_sme ())
670 store_sveregs_to_thread (regcache);
671 else
672 store_fpregs_to_thread (regcache);
674 if (tdep->has_mte ())
675 store_mteregs_to_thread (regcache);
677 if (tdep->has_tls ())
678 store_tlsregs_to_thread (regcache);
680 if (tdep->has_sme ())
681 store_za_to_thread (regcache);
683 if (tdep->has_sme2 ())
684 store_zt_to_thread (regcache);
686 /* General purpose register? */
687 else if (regno < AARCH64_V0_REGNUM)
688 store_gregs_to_thread (regcache);
689 /* SVE register? */
690 else if ((tdep->has_sve () || tdep->has_sme ())
691 && regno <= AARCH64_SVE_VG_REGNUM)
692 store_sveregs_to_thread (regcache);
693 /* FPSIMD register? */
694 else if (regno <= AARCH64_FPCR_REGNUM)
695 store_fpregs_to_thread (regcache);
696 /* SME register? */
697 else if (tdep->has_sme () && regno >= tdep->sme_reg_base
698 && regno < tdep->sme_reg_base + 3)
699 store_za_to_thread (regcache);
700 else if (tdep->has_sme2 () && regno == tdep->sme2_zt0_regnum)
701 store_zt_to_thread (regcache);
702 /* MTE register? */
703 else if (tdep->has_mte ()
704 && (regno == tdep->mte_reg_base))
705 store_mteregs_to_thread (regcache);
706 /* TLS register? */
707 else if (tdep->has_tls ()
708 && regno >= tdep->tls_regnum_base
709 && regno < tdep->tls_regnum_base + tdep->tls_register_count)
710 store_tlsregs_to_thread (regcache);
712 /* PAuth registers are read-only. */
715 /* A version of the "store_registers" target_ops method used when running
716 32-bit ARM code on an AArch64 target. Copy the value of register REGNO
717 from REGCACHE into the the target. */
719 static void
720 aarch32_store_registers (struct regcache *regcache, int regno)
722 arm_gdbarch_tdep *tdep
723 = gdbarch_tdep<arm_gdbarch_tdep> (regcache->arch ());
725 if (regno == -1)
727 store_gregs_to_thread (regcache);
728 if (tdep->vfp_register_count > 0)
729 store_fpregs_to_thread (regcache);
731 else if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
732 store_gregs_to_thread (regcache);
733 else if (tdep->vfp_register_count > 0
734 && regno >= ARM_D0_REGNUM
735 && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
736 || regno == ARM_FPSCR_REGNUM))
737 store_fpregs_to_thread (regcache);
740 /* Implement the "store_registers" target_ops method. */
742 void
743 aarch64_linux_nat_target::store_registers (struct regcache *regcache,
744 int regno)
746 if (gdbarch_bfd_arch_info (regcache->arch ())->bits_per_word == 32)
747 aarch32_store_registers (regcache, regno);
748 else
749 aarch64_store_registers (regcache, regno);
752 /* Fill register REGNO (if it is a general-purpose register) in
753 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
754 do this for all registers. */
756 void
757 fill_gregset (const struct regcache *regcache,
758 gdb_gregset_t *gregsetp, int regno)
760 regcache_collect_regset (&aarch64_linux_gregset, regcache,
761 regno, (gdb_byte *) gregsetp,
762 AARCH64_LINUX_SIZEOF_GREGSET);
765 /* Fill GDB's register array with the general-purpose register values
766 in *GREGSETP. */
768 void
769 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
771 regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
772 (const gdb_byte *) gregsetp,
773 AARCH64_LINUX_SIZEOF_GREGSET);
776 /* Fill register REGNO (if it is a floating-point register) in
777 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
778 do this for all registers. */
780 void
781 fill_fpregset (const struct regcache *regcache,
782 gdb_fpregset_t *fpregsetp, int regno)
784 regcache_collect_regset (&aarch64_linux_fpregset, regcache,
785 regno, (gdb_byte *) fpregsetp,
786 AARCH64_LINUX_SIZEOF_FPREGSET);
789 /* Fill GDB's register array with the floating-point register values
790 in *FPREGSETP. */
792 void
793 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
795 regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
796 (const gdb_byte *) fpregsetp,
797 AARCH64_LINUX_SIZEOF_FPREGSET);
800 /* linux_nat_new_fork hook. */
802 void
803 aarch64_linux_nat_target::low_new_fork (struct lwp_info *parent,
804 pid_t child_pid)
806 pid_t parent_pid;
807 struct aarch64_debug_reg_state *parent_state;
808 struct aarch64_debug_reg_state *child_state;
810 /* NULL means no watchpoint has ever been set in the parent. In
811 that case, there's nothing to do. */
812 if (parent->arch_private == NULL)
813 return;
815 /* GDB core assumes the child inherits the watchpoints/hw
816 breakpoints of the parent, and will remove them all from the
817 forked off process. Copy the debug registers mirrors into the
818 new process so that all breakpoints and watchpoints can be
819 removed together. */
821 parent_pid = parent->ptid.pid ();
822 parent_state = aarch64_get_debug_reg_state (parent_pid);
823 child_state = aarch64_get_debug_reg_state (child_pid);
824 *child_state = *parent_state;
828 /* Called by libthread_db. Returns a pointer to the thread local
829 storage (or its descriptor). */
831 ps_err_e
832 ps_get_thread_area (struct ps_prochandle *ph,
833 lwpid_t lwpid, int idx, void **base)
835 gdbarch *arch = current_inferior ()->arch ();
836 int is_64bit_p = (gdbarch_bfd_arch_info (arch)->bits_per_word == 64);
838 return aarch64_ps_get_thread_area (ph, lwpid, idx, base, is_64bit_p);
842 /* Implement the "low_init_process" target_ops method. */
844 void
845 aarch64_linux_nat_target::low_init_process (pid_t pid)
847 low_forget_process (pid);
848 /* Set the hardware debug register capacity. This requires the process to be
849 ptrace-stopped, otherwise detection will fail and software watchpoints will
850 be used instead of hardware. If we allow this to be done lazily, we
851 cannot guarantee that it's called when the process is ptrace-stopped, so
852 do it now. */
853 aarch64_linux_get_debug_reg_capacity (pid);
856 /* Implement the "read_description" target_ops method. */
858 const struct target_desc *
859 aarch64_linux_nat_target::read_description ()
861 int ret, tid;
862 gdb_byte regbuf[ARM_VFP3_REGS_SIZE];
863 struct iovec iovec;
865 if (inferior_ptid == null_ptid)
866 return this->beneath ()->read_description ();
868 tid = inferior_ptid.pid ();
870 iovec.iov_base = regbuf;
871 iovec.iov_len = ARM_VFP3_REGS_SIZE;
873 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
874 if (ret == 0)
875 return aarch32_read_description (false);
877 CORE_ADDR hwcap = linux_get_hwcap ();
878 CORE_ADDR hwcap2 = linux_get_hwcap2 ();
880 aarch64_features features;
881 /* SVE/SSVE check. Reading VQ may return either the regular vector length
882 or the streaming vector length, depending on whether streaming mode is
883 active or not. */
884 features.vq = aarch64_sve_get_vq (tid);
885 features.pauth = hwcap & AARCH64_HWCAP_PACA;
886 features.mte = hwcap2 & HWCAP2_MTE;
887 features.tls = aarch64_tls_register_count (tid);
888 /* SME feature check. */
889 features.svq = aarch64_za_get_svq (tid);
891 /* Check for SME2 support. */
892 if ((hwcap2 & HWCAP2_SME2) || (hwcap2 & HWCAP2_SME2P1))
893 features.sme2 = supports_zt_registers (tid);
895 return aarch64_read_description (features);
898 /* Convert a native/host siginfo object, into/from the siginfo in the
899 layout of the inferiors' architecture. Returns true if any
900 conversion was done; false otherwise. If DIRECTION is 1, then copy
901 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
902 INF. */
904 bool
905 aarch64_linux_nat_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
906 int direction)
908 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
910 /* Is the inferior 32-bit? If so, then do fixup the siginfo
911 object. */
912 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
914 if (direction == 0)
915 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo *) inf,
916 native);
917 else
918 aarch64_siginfo_from_compat_siginfo (native,
919 (struct compat_siginfo *) inf);
921 return true;
924 return false;
927 /* Implement the "stopped_data_address" target_ops method. */
929 bool
930 aarch64_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
932 siginfo_t siginfo;
933 struct aarch64_debug_reg_state *state;
935 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
936 return false;
938 /* This must be a hardware breakpoint. */
939 if (siginfo.si_signo != SIGTRAP
940 || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
941 return false;
943 /* Make sure to ignore the top byte, otherwise we may not recognize a
944 hardware watchpoint hit. The stopped data addresses coming from the
945 kernel can potentially be tagged addresses. */
946 struct gdbarch *gdbarch = thread_architecture (inferior_ptid);
947 const CORE_ADDR addr_trap
948 = gdbarch_remove_non_address_bits (gdbarch, (CORE_ADDR) siginfo.si_addr);
950 /* Check if the address matches any watched address. */
951 state = aarch64_get_debug_reg_state (inferior_ptid.pid ());
952 return aarch64_stopped_data_address (state, addr_trap, addr_p);
955 /* Implement the "stopped_by_watchpoint" target_ops method. */
957 bool
958 aarch64_linux_nat_target::stopped_by_watchpoint ()
960 return stopped_data_address (nullptr);
963 /* Implement the "can_do_single_step" target_ops method. */
966 aarch64_linux_nat_target::can_do_single_step ()
968 return 1;
971 /* Implement the "thread_architecture" target_ops method.
973 Returns the gdbarch for the thread identified by PTID. If the thread in
974 question is a 32-bit ARM thread, then the architecture returned will be
975 that of the process itself.
977 If the thread is an AArch64 thread then we need to check the current
978 vector length; if the vector length has changed then we need to lookup a
979 new gdbarch that matches the new vector length. */
981 struct gdbarch *
982 aarch64_linux_nat_target::thread_architecture (ptid_t ptid)
984 /* Find the current gdbarch the same way as process_stratum_target. */
985 inferior *inf = find_inferior_ptid (this, ptid);
986 gdb_assert (inf != NULL);
988 /* If this is a 32-bit architecture, then this is ARM, not AArch64.
989 There's no SVE vectors here, so just return the inferior
990 architecture. */
991 if (gdbarch_bfd_arch_info (inf->arch ())->bits_per_word == 32)
992 return inf->arch ();
994 /* Only return the inferior's gdbarch if both vq and svq match the ones in
995 the tdep. */
996 aarch64_gdbarch_tdep *tdep
997 = gdbarch_tdep<aarch64_gdbarch_tdep> (inf->arch ());
998 uint64_t vq = aarch64_sve_get_vq (ptid.lwp ());
999 uint64_t svq = aarch64_za_get_svq (ptid.lwp ());
1000 if (vq == tdep->vq && svq == tdep->sme_svq)
1001 return inf->arch ();
1003 /* We reach here if any vector length for the thread is different from its
1004 value at process start. Lookup gdbarch via info (potentially creating a
1005 new one) by using a target description that corresponds to the new vq/svq
1006 value and the current architecture features. */
1008 const struct target_desc *tdesc = gdbarch_target_desc (inf->arch ());
1009 aarch64_features features = aarch64_features_from_target_desc (tdesc);
1010 features.vq = vq;
1011 features.svq = svq;
1013 /* Check for the SME2 feature. */
1014 features.sme2 = supports_zt_registers (ptid.lwp ());
1016 struct gdbarch_info info;
1017 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_aarch64, bfd_mach_aarch64);
1018 info.target_desc = aarch64_read_description (features);
1019 return gdbarch_find_by_info (info);
1022 /* Implement the "supports_memory_tagging" target_ops method. */
1024 bool
1025 aarch64_linux_nat_target::supports_memory_tagging ()
1027 return (linux_get_hwcap2 () & HWCAP2_MTE) != 0;
1030 /* Implement the "fetch_memtags" target_ops method. */
1032 bool
1033 aarch64_linux_nat_target::fetch_memtags (CORE_ADDR address, size_t len,
1034 gdb::byte_vector &tags, int type)
1036 int tid = get_ptrace_pid (inferior_ptid);
1038 /* Allocation tags? */
1039 if (type == static_cast<int> (aarch64_memtag_type::mte_allocation))
1040 return aarch64_mte_fetch_memtags (tid, address, len, tags);
1042 return false;
1045 /* Implement the "store_memtags" target_ops method. */
1047 bool
1048 aarch64_linux_nat_target::store_memtags (CORE_ADDR address, size_t len,
1049 const gdb::byte_vector &tags, int type)
1051 int tid = get_ptrace_pid (inferior_ptid);
1053 /* Allocation tags? */
1054 if (type == static_cast<int> (aarch64_memtag_type::mte_allocation))
1055 return aarch64_mte_store_memtags (tid, address, len, tags);
1057 return false;
1060 bool
1061 aarch64_linux_nat_target::is_address_tagged (gdbarch *gdbarch, CORE_ADDR address)
1063 /* Here we take a detour going to linux-tdep layer to read the smaps file,
1064 because currently there isn't a better way to get that information to
1065 check if a given address is tagged or not.
1067 In the future, if this check is made, for instance, available via PTRACE,
1068 it will be possible to drop the smaps path in favor of a PTRACE one for
1069 this check. */
1070 return gdbarch_tagged_address_p (gdbarch, address);
1073 void _initialize_aarch64_linux_nat ();
1074 void
1075 _initialize_aarch64_linux_nat ()
1077 aarch64_initialize_hw_point ();
1079 /* Register the target. */
1080 linux_target = &the_aarch64_linux_nat_target;
1081 add_inf_child_target (&the_aarch64_linux_nat_target);