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/>. */
25 #include "linux-nat.h"
26 #include "target-descriptions.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"
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>
47 #include "linux-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"
62 #define TRAP_HWBKPT 0x0004
65 class aarch64_linux_nat_target final
66 : public aarch64_nat_target
<linux_nat_target
>
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
)
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. */
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. */
127 fetch_gregs_from_thread (struct regcache
*regcache
)
130 struct gdbarch
*gdbarch
= regcache
->arch ();
134 /* Make sure REGS can hold all registers contents on both aarch64
136 static_assert (sizeof (regs
) >= 18 * 4);
138 tid
= regcache
->ptid ().lwp ();
140 iovec
.iov_base
= ®s
;
141 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
142 iovec
.iov_len
= 18 * 4;
144 iovec
.iov_len
= sizeof (regs
);
146 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
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);
156 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
157 regcache
->raw_supply (regno
, ®s
[regno
- AARCH64_X0_REGNUM
]);
161 /* Store to the current thread the valid general-purpose register
162 values in the GDB's register array. */
165 store_gregs_to_thread (const struct regcache
*regcache
)
170 struct gdbarch
*gdbarch
= regcache
->arch ();
172 /* Make sure REGS can hold all registers contents on both aarch64
174 static_assert (sizeof (regs
) >= 18 * 4);
175 tid
= regcache
->ptid ().lwp ();
177 iovec
.iov_base
= ®s
;
178 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
179 iovec
.iov_len
= 18 * 4;
181 iovec
.iov_len
= sizeof (regs
);
183 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
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);
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
, ®s
[regno
- AARCH64_X0_REGNUM
]);
198 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
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. */
207 fetch_fpregs_from_thread (struct regcache
*regcache
)
212 struct gdbarch
*gdbarch
= regcache
->arch ();
214 /* Make sure REGS can hold all VFP registers contents on both aarch64
216 static_assert (sizeof regs
>= ARM_VFP3_REGS_SIZE
);
218 tid
= regcache
->ptid ().lwp ();
220 iovec
.iov_base
= ®s
;
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
);
228 perror_with_name (_("Unable to fetch VFP registers"));
230 aarch32_vfp_regcache_supply (regcache
, (gdb_byte
*) ®s
, 32);
236 iovec
.iov_len
= sizeof (regs
);
238 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
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
, ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
245 regcache
->raw_supply (AARCH64_FPSR_REGNUM
, ®s
.fpsr
);
246 regcache
->raw_supply (AARCH64_FPCR_REGNUM
, ®s
.fpcr
);
250 /* Store to the current thread the valid fp/simd register
251 values in the GDB's register array. */
254 store_fpregs_to_thread (const struct regcache
*regcache
)
259 struct gdbarch
*gdbarch
= regcache
->arch ();
261 /* Make sure REGS can hold all VFP registers contents on both aarch64
263 static_assert (sizeof regs
>= ARM_VFP3_REGS_SIZE
);
264 tid
= regcache
->ptid ().lwp ();
266 iovec
.iov_base
= ®s
;
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
);
274 perror_with_name (_("Unable to fetch VFP registers"));
276 aarch32_vfp_regcache_collect (regcache
, (gdb_byte
*) ®s
, 32);
282 iovec
.iov_len
= sizeof (regs
);
284 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
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 *) ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
293 if (REG_VALID
== regcache
->get_register_status (AARCH64_FPSR_REGNUM
))
294 regcache
->raw_collect (AARCH64_FPSR_REGNUM
, (char *) ®s
.fpsr
);
295 if (REG_VALID
== regcache
->get_register_status (AARCH64_FPCR_REGNUM
))
296 regcache
->raw_collect (AARCH64_FPCR_REGNUM
, (char *) ®s
.fpcr
);
299 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
301 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
303 perror_with_name (_("Unable to store VFP registers"));
307 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iovec
);
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. */
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. */
333 store_sveregs_to_thread (struct regcache
*regcache
)
335 /* Fetch SVE state from the register cache and update the thread TID with
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. */
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 (),
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. */
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 (),
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. */
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 (),
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. */
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 (),
403 tdep
->sme2_zt0_regnum
);
406 /* Fill GDB's register array with the pointer authentication mask values from
407 the current thread. */
410 fetch_pauth_masks_from_thread (struct regcache
*regcache
)
412 aarch64_gdbarch_tdep
*tdep
413 = gdbarch_tdep
<aarch64_gdbarch_tdep
> (regcache
->arch ());
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
);
424 perror_with_name (_("unable to fetch pauth registers"));
426 regcache
->raw_supply (AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
),
428 regcache
->raw_supply (AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
),
432 /* Fill GDB's register array with the MTE register values from
433 the current thread. */
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;
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
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
))
474 regcache
->raw_collect (regno
, (char *) &tag_ctl
);
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. */
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 std::vector
<uint64_t> tpidrs (tdep
->tls_register_count
);
502 iovec
.iov_base
= tpidrs
.data ();
503 iovec
.iov_len
= tpidrs
.size () * sizeof (tpidrs
[0]);
505 int tid
= get_ptrace_pid (regcache
->ptid ());
506 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_TLS
, &iovec
) != 0)
507 perror_with_name (_("unable to fetch TLS registers"));
509 for (int i
= 0; i
< tdep
->tls_register_count
; i
++)
510 regcache
->raw_supply (regno
+ i
, &tpidrs
[i
]);
513 /* Store to the current thread the valid TLS register set in GDB's
517 store_tlsregs_to_thread (struct regcache
*regcache
)
519 aarch64_gdbarch_tdep
*tdep
520 = gdbarch_tdep
<aarch64_gdbarch_tdep
> (regcache
->arch ());
521 int regno
= tdep
->tls_regnum_base
;
523 gdb_assert (regno
!= -1);
524 gdb_assert (tdep
->tls_register_count
> 0);
526 std::vector
<uint64_t> tpidrs (tdep
->tls_register_count
);
528 for (int i
= 0; i
< tdep
->tls_register_count
; i
++)
530 if (REG_VALID
!= regcache
->get_register_status (regno
+ i
))
533 regcache
->raw_collect (regno
+ i
, (char *) &tpidrs
[i
]);
537 iovec
.iov_base
= tpidrs
.data ();
538 iovec
.iov_len
= tpidrs
.size () * sizeof (tpidrs
[0]);
540 int tid
= get_ptrace_pid (regcache
->ptid ());
541 if (ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_TLS
, &iovec
) != 0)
542 perror_with_name (_("unable to store TLS register"));
545 /* The AArch64 version of the "fetch_registers" target_ops method. Fetch
546 REGNO from the target and place the result into REGCACHE. */
549 aarch64_fetch_registers (struct regcache
*regcache
, int regno
)
551 aarch64_gdbarch_tdep
*tdep
552 = gdbarch_tdep
<aarch64_gdbarch_tdep
> (regcache
->arch ());
554 /* Do we need to fetch all registers? */
557 fetch_gregs_from_thread (regcache
);
559 /* We attempt to fetch SVE registers if there is support for either
560 SVE or SME (due to the SSVE state of SME). */
561 if (tdep
->has_sve () || tdep
->has_sme ())
562 fetch_sveregs_from_thread (regcache
);
564 fetch_fpregs_from_thread (regcache
);
566 if (tdep
->has_pauth ())
567 fetch_pauth_masks_from_thread (regcache
);
569 if (tdep
->has_mte ())
570 fetch_mteregs_from_thread (regcache
);
572 if (tdep
->has_tls ())
573 fetch_tlsregs_from_thread (regcache
);
575 if (tdep
->has_sme ())
576 fetch_za_from_thread (regcache
);
578 if (tdep
->has_sme2 ())
579 fetch_zt_from_thread (regcache
);
581 /* General purpose register? */
582 else if (regno
< AARCH64_V0_REGNUM
)
583 fetch_gregs_from_thread (regcache
);
585 else if ((tdep
->has_sve () || tdep
->has_sme ())
586 && regno
<= AARCH64_SVE_VG_REGNUM
)
587 fetch_sveregs_from_thread (regcache
);
588 /* FPSIMD register? */
589 else if (regno
<= AARCH64_FPCR_REGNUM
)
590 fetch_fpregs_from_thread (regcache
);
591 /* PAuth register? */
592 else if (tdep
->has_pauth ()
593 && (regno
== AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
)
594 || regno
== AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
)))
595 fetch_pauth_masks_from_thread (regcache
);
597 else if (tdep
->has_sme () && regno
>= tdep
->sme_reg_base
598 && regno
< tdep
->sme_reg_base
+ 3)
599 fetch_za_from_thread (regcache
);
601 else if (tdep
->has_sme2 () && regno
== tdep
->sme2_zt0_regnum
)
602 fetch_zt_from_thread (regcache
);
604 else if (tdep
->has_mte ()
605 && (regno
== tdep
->mte_reg_base
))
606 fetch_mteregs_from_thread (regcache
);
608 else if (tdep
->has_tls ()
609 && regno
>= tdep
->tls_regnum_base
610 && regno
< tdep
->tls_regnum_base
+ tdep
->tls_register_count
)
611 fetch_tlsregs_from_thread (regcache
);
614 /* A version of the "fetch_registers" target_ops method used when running
615 32-bit ARM code on an AArch64 target. Fetch REGNO from the target and
616 place the result into REGCACHE. */
619 aarch32_fetch_registers (struct regcache
*regcache
, int regno
)
621 arm_gdbarch_tdep
*tdep
622 = gdbarch_tdep
<arm_gdbarch_tdep
> (regcache
->arch ());
626 fetch_gregs_from_thread (regcache
);
627 if (tdep
->vfp_register_count
> 0)
628 fetch_fpregs_from_thread (regcache
);
630 else if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
631 fetch_gregs_from_thread (regcache
);
632 else if (tdep
->vfp_register_count
> 0
633 && regno
>= ARM_D0_REGNUM
634 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
635 || regno
== ARM_FPSCR_REGNUM
))
636 fetch_fpregs_from_thread (regcache
);
639 /* Implement the "fetch_registers" target_ops method. */
642 aarch64_linux_nat_target::fetch_registers (struct regcache
*regcache
,
645 if (gdbarch_bfd_arch_info (regcache
->arch ())->bits_per_word
== 32)
646 aarch32_fetch_registers (regcache
, regno
);
648 aarch64_fetch_registers (regcache
, regno
);
651 /* The AArch64 version of the "store_registers" target_ops method. Copy
652 the value of register REGNO from REGCACHE into the the target. */
655 aarch64_store_registers (struct regcache
*regcache
, int regno
)
657 aarch64_gdbarch_tdep
*tdep
658 = gdbarch_tdep
<aarch64_gdbarch_tdep
> (regcache
->arch ());
660 /* Do we need to store all registers? */
663 store_gregs_to_thread (regcache
);
665 /* We attempt to store SVE registers if there is support for either
666 SVE or SME (due to the SSVE state of SME). */
667 if (tdep
->has_sve () || tdep
->has_sme ())
668 store_sveregs_to_thread (regcache
);
670 store_fpregs_to_thread (regcache
);
672 if (tdep
->has_mte ())
673 store_mteregs_to_thread (regcache
);
675 if (tdep
->has_tls ())
676 store_tlsregs_to_thread (regcache
);
678 if (tdep
->has_sme ())
679 store_za_to_thread (regcache
);
681 if (tdep
->has_sme2 ())
682 store_zt_to_thread (regcache
);
684 /* General purpose register? */
685 else if (regno
< AARCH64_V0_REGNUM
)
686 store_gregs_to_thread (regcache
);
688 else if ((tdep
->has_sve () || tdep
->has_sme ())
689 && regno
<= AARCH64_SVE_VG_REGNUM
)
690 store_sveregs_to_thread (regcache
);
691 /* FPSIMD register? */
692 else if (regno
<= AARCH64_FPCR_REGNUM
)
693 store_fpregs_to_thread (regcache
);
695 else if (tdep
->has_sme () && regno
>= tdep
->sme_reg_base
696 && regno
< tdep
->sme_reg_base
+ 3)
697 store_za_to_thread (regcache
);
698 else if (tdep
->has_sme2 () && regno
== tdep
->sme2_zt0_regnum
)
699 store_zt_to_thread (regcache
);
701 else if (tdep
->has_mte ()
702 && (regno
== tdep
->mte_reg_base
))
703 store_mteregs_to_thread (regcache
);
705 else if (tdep
->has_tls ()
706 && regno
>= tdep
->tls_regnum_base
707 && regno
< tdep
->tls_regnum_base
+ tdep
->tls_register_count
)
708 store_tlsregs_to_thread (regcache
);
710 /* PAuth registers are read-only. */
713 /* A version of the "store_registers" target_ops method used when running
714 32-bit ARM code on an AArch64 target. Copy the value of register REGNO
715 from REGCACHE into the the target. */
718 aarch32_store_registers (struct regcache
*regcache
, int regno
)
720 arm_gdbarch_tdep
*tdep
721 = gdbarch_tdep
<arm_gdbarch_tdep
> (regcache
->arch ());
725 store_gregs_to_thread (regcache
);
726 if (tdep
->vfp_register_count
> 0)
727 store_fpregs_to_thread (regcache
);
729 else if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
730 store_gregs_to_thread (regcache
);
731 else if (tdep
->vfp_register_count
> 0
732 && regno
>= ARM_D0_REGNUM
733 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
734 || regno
== ARM_FPSCR_REGNUM
))
735 store_fpregs_to_thread (regcache
);
738 /* Implement the "store_registers" target_ops method. */
741 aarch64_linux_nat_target::store_registers (struct regcache
*regcache
,
744 if (gdbarch_bfd_arch_info (regcache
->arch ())->bits_per_word
== 32)
745 aarch32_store_registers (regcache
, regno
);
747 aarch64_store_registers (regcache
, regno
);
750 /* Fill register REGNO (if it is a general-purpose register) in
751 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
752 do this for all registers. */
755 fill_gregset (const struct regcache
*regcache
,
756 gdb_gregset_t
*gregsetp
, int regno
)
758 regcache_collect_regset (&aarch64_linux_gregset
, regcache
,
759 regno
, (gdb_byte
*) gregsetp
,
760 AARCH64_LINUX_SIZEOF_GREGSET
);
763 /* Fill GDB's register array with the general-purpose register values
767 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
769 regcache_supply_regset (&aarch64_linux_gregset
, regcache
, -1,
770 (const gdb_byte
*) gregsetp
,
771 AARCH64_LINUX_SIZEOF_GREGSET
);
774 /* Fill register REGNO (if it is a floating-point register) in
775 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
776 do this for all registers. */
779 fill_fpregset (const struct regcache
*regcache
,
780 gdb_fpregset_t
*fpregsetp
, int regno
)
782 regcache_collect_regset (&aarch64_linux_fpregset
, regcache
,
783 regno
, (gdb_byte
*) fpregsetp
,
784 AARCH64_LINUX_SIZEOF_FPREGSET
);
787 /* Fill GDB's register array with the floating-point register values
791 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
793 regcache_supply_regset (&aarch64_linux_fpregset
, regcache
, -1,
794 (const gdb_byte
*) fpregsetp
,
795 AARCH64_LINUX_SIZEOF_FPREGSET
);
798 /* linux_nat_new_fork hook. */
801 aarch64_linux_nat_target::low_new_fork (struct lwp_info
*parent
,
805 struct aarch64_debug_reg_state
*parent_state
;
806 struct aarch64_debug_reg_state
*child_state
;
808 /* NULL means no watchpoint has ever been set in the parent. In
809 that case, there's nothing to do. */
810 if (parent
->arch_private
== NULL
)
813 /* GDB core assumes the child inherits the watchpoints/hw
814 breakpoints of the parent, and will remove them all from the
815 forked off process. Copy the debug registers mirrors into the
816 new process so that all breakpoints and watchpoints can be
819 parent_pid
= parent
->ptid
.pid ();
820 parent_state
= aarch64_get_debug_reg_state (parent_pid
);
821 child_state
= aarch64_get_debug_reg_state (child_pid
);
822 *child_state
= *parent_state
;
826 /* Called by libthread_db. Returns a pointer to the thread local
827 storage (or its descriptor). */
830 ps_get_thread_area (struct ps_prochandle
*ph
,
831 lwpid_t lwpid
, int idx
, void **base
)
833 gdbarch
*arch
= current_inferior ()->arch ();
834 int is_64bit_p
= (gdbarch_bfd_arch_info (arch
)->bits_per_word
== 64);
836 return aarch64_ps_get_thread_area (ph
, lwpid
, idx
, base
, is_64bit_p
);
840 /* Implement the "low_init_process" target_ops method. */
843 aarch64_linux_nat_target::low_init_process (pid_t pid
)
845 low_forget_process (pid
);
846 /* Set the hardware debug register capacity. This requires the process to be
847 ptrace-stopped, otherwise detection will fail and software watchpoints will
848 be used instead of hardware. If we allow this to be done lazily, we
849 cannot guarantee that it's called when the process is ptrace-stopped, so
851 aarch64_linux_get_debug_reg_capacity (pid
);
854 /* Implement the "read_description" target_ops method. */
856 const struct target_desc
*
857 aarch64_linux_nat_target::read_description ()
860 gdb_byte regbuf
[ARM_VFP3_REGS_SIZE
];
863 if (inferior_ptid
== null_ptid
)
864 return this->beneath ()->read_description ();
866 tid
= inferior_ptid
.pid ();
868 iovec
.iov_base
= regbuf
;
869 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
871 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
873 return aarch32_read_description (false);
875 CORE_ADDR hwcap
= linux_get_hwcap ();
876 CORE_ADDR hwcap2
= linux_get_hwcap2 ();
878 aarch64_features features
;
879 /* SVE/SSVE check. Reading VQ may return either the regular vector length
880 or the streaming vector length, depending on whether streaming mode is
882 features
.vq
= aarch64_sve_get_vq (tid
);
883 features
.pauth
= hwcap
& AARCH64_HWCAP_PACA
;
884 features
.mte
= hwcap2
& HWCAP2_MTE
;
885 features
.tls
= aarch64_tls_register_count (tid
);
886 /* SME feature check. */
887 features
.svq
= aarch64_za_get_svq (tid
);
889 /* Check for SME2 support. */
890 if ((hwcap2
& HWCAP2_SME2
) || (hwcap2
& HWCAP2_SME2P1
))
891 features
.sme2
= supports_zt_registers (tid
);
893 return aarch64_read_description (features
);
896 /* Convert a native/host siginfo object, into/from the siginfo in the
897 layout of the inferiors' architecture. Returns true if any
898 conversion was done; false otherwise. If DIRECTION is 1, then copy
899 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
903 aarch64_linux_nat_target::low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
906 struct gdbarch
*gdbarch
= get_frame_arch (get_current_frame ());
908 /* Is the inferior 32-bit? If so, then do fixup the siginfo
910 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
913 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo
*) inf
,
916 aarch64_siginfo_from_compat_siginfo (native
,
917 (struct compat_siginfo
*) inf
);
925 /* Implement the "stopped_data_address" target_ops method. */
928 aarch64_linux_nat_target::stopped_data_address (CORE_ADDR
*addr_p
)
931 struct aarch64_debug_reg_state
*state
;
933 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
936 /* This must be a hardware breakpoint. */
937 if (siginfo
.si_signo
!= SIGTRAP
938 || (siginfo
.si_code
& 0xffff) != TRAP_HWBKPT
)
941 /* Make sure to ignore the top byte, otherwise we may not recognize a
942 hardware watchpoint hit. The stopped data addresses coming from the
943 kernel can potentially be tagged addresses. */
944 struct gdbarch
*gdbarch
= thread_architecture (inferior_ptid
);
945 const CORE_ADDR addr_trap
946 = gdbarch_remove_non_address_bits (gdbarch
, (CORE_ADDR
) siginfo
.si_addr
);
948 /* Check if the address matches any watched address. */
949 state
= aarch64_get_debug_reg_state (inferior_ptid
.pid ());
950 return aarch64_stopped_data_address (state
, addr_trap
, addr_p
);
953 /* Implement the "stopped_by_watchpoint" target_ops method. */
956 aarch64_linux_nat_target::stopped_by_watchpoint ()
958 return stopped_data_address (nullptr);
961 /* Implement the "can_do_single_step" target_ops method. */
964 aarch64_linux_nat_target::can_do_single_step ()
969 /* Implement the "thread_architecture" target_ops method.
971 Returns the gdbarch for the thread identified by PTID. If the thread in
972 question is a 32-bit ARM thread, then the architecture returned will be
973 that of the process itself.
975 If the thread is an AArch64 thread then we need to check the current
976 vector length; if the vector length has changed then we need to lookup a
977 new gdbarch that matches the new vector length. */
980 aarch64_linux_nat_target::thread_architecture (ptid_t ptid
)
982 /* Find the current gdbarch the same way as process_stratum_target. */
983 inferior
*inf
= find_inferior_ptid (this, ptid
);
984 gdb_assert (inf
!= NULL
);
986 /* If this is a 32-bit architecture, then this is ARM, not AArch64.
987 There's no SVE vectors here, so just return the inferior
989 if (gdbarch_bfd_arch_info (inf
->arch ())->bits_per_word
== 32)
992 /* Only return the inferior's gdbarch if both vq and svq match the ones in
994 aarch64_gdbarch_tdep
*tdep
995 = gdbarch_tdep
<aarch64_gdbarch_tdep
> (inf
->arch ());
996 uint64_t vq
= aarch64_sve_get_vq (ptid
.lwp ());
997 uint64_t svq
= aarch64_za_get_svq (ptid
.lwp ());
998 if (vq
== tdep
->vq
&& svq
== tdep
->sme_svq
)
1001 /* We reach here if any vector length for the thread is different from its
1002 value at process start. Lookup gdbarch via info (potentially creating a
1003 new one) by using a target description that corresponds to the new vq/svq
1004 value and the current architecture features. */
1006 const struct target_desc
*tdesc
= gdbarch_target_desc (inf
->arch ());
1007 aarch64_features features
= aarch64_features_from_target_desc (tdesc
);
1011 /* Check for the SME2 feature. */
1012 features
.sme2
= supports_zt_registers (ptid
.lwp ());
1014 struct gdbarch_info info
;
1015 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_aarch64
, bfd_mach_aarch64
);
1016 info
.target_desc
= aarch64_read_description (features
);
1017 return gdbarch_find_by_info (info
);
1020 /* Implement the "supports_memory_tagging" target_ops method. */
1023 aarch64_linux_nat_target::supports_memory_tagging ()
1025 return (linux_get_hwcap2 () & HWCAP2_MTE
) != 0;
1028 /* Implement the "fetch_memtags" target_ops method. */
1031 aarch64_linux_nat_target::fetch_memtags (CORE_ADDR address
, size_t len
,
1032 gdb::byte_vector
&tags
, int type
)
1034 int tid
= get_ptrace_pid (inferior_ptid
);
1036 /* Allocation tags? */
1037 if (type
== static_cast<int> (aarch64_memtag_type::mte_allocation
))
1038 return aarch64_mte_fetch_memtags (tid
, address
, len
, tags
);
1043 /* Implement the "store_memtags" target_ops method. */
1046 aarch64_linux_nat_target::store_memtags (CORE_ADDR address
, size_t len
,
1047 const gdb::byte_vector
&tags
, int type
)
1049 int tid
= get_ptrace_pid (inferior_ptid
);
1051 /* Allocation tags? */
1052 if (type
== static_cast<int> (aarch64_memtag_type::mte_allocation
))
1053 return aarch64_mte_store_memtags (tid
, address
, len
, tags
);
1059 aarch64_linux_nat_target::is_address_tagged (gdbarch
*gdbarch
, CORE_ADDR address
)
1061 /* Here we take a detour going to linux-tdep layer to read the smaps file,
1062 because currently there isn't a better way to get that information to
1063 check if a given address is tagged or not.
1065 In the future, if this check is made, for instance, available via PTRACE,
1066 it will be possible to drop the smaps path in favor of a PTRACE one for
1068 return gdbarch_tagged_address_p (gdbarch
, address
);
1071 void _initialize_aarch64_linux_nat ();
1073 _initialize_aarch64_linux_nat ()
1075 aarch64_initialize_hw_point ();
1077 /* Register the target. */
1078 linux_target
= &the_aarch64_linux_nat_target
;
1079 add_inf_child_target (&the_aarch64_linux_nat_target
);