1 /* Native-dependent code for GNU/Linux AArch64.
3 Copyright (C) 2011-2021 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/>. */
26 #include "linux-nat.h"
27 #include "target-descriptions.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-sve-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"
49 /* Defines ps_err_e, struct ps_prochandle. */
50 #include "gdb_proc_service.h"
51 #include "arch-utils.h"
53 #include "arch/aarch64-mte-linux.h"
55 #include "nat/aarch64-mte-linux-ptrace.h"
58 #define TRAP_HWBKPT 0x0004
61 class aarch64_linux_nat_target final
: public linux_nat_target
64 /* Add our register access methods. */
65 void fetch_registers (struct regcache
*, int) override
;
66 void store_registers (struct regcache
*, int) override
;
68 const struct target_desc
*read_description () override
;
70 /* Add our hardware breakpoint and watchpoint implementation. */
71 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
72 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
73 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
74 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
75 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
76 struct expression
*) override
;
77 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
78 struct expression
*) override
;
79 bool stopped_by_watchpoint () override
;
80 bool stopped_data_address (CORE_ADDR
*) override
;
81 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
83 int can_do_single_step () override
;
85 /* Override the GNU/Linux inferior startup hook. */
86 void post_startup_inferior (ptid_t
) override
;
88 /* Override the GNU/Linux post attach hook. */
89 void post_attach (int pid
) override
;
91 /* These three defer to common nat/ code. */
92 void low_new_thread (struct lwp_info
*lp
) override
93 { aarch64_linux_new_thread (lp
); }
94 void low_delete_thread (struct arch_lwp_info
*lp
) override
95 { aarch64_linux_delete_thread (lp
); }
96 void low_prepare_to_resume (struct lwp_info
*lp
) override
97 { aarch64_linux_prepare_to_resume (lp
); }
99 void low_new_fork (struct lwp_info
*parent
, pid_t child_pid
) override
;
100 void low_forget_process (pid_t pid
) override
;
102 /* Add our siginfo layout converter. */
103 bool low_siginfo_fixup (siginfo_t
*ptrace
, gdb_byte
*inf
, int direction
)
106 struct gdbarch
*thread_architecture (ptid_t
) override
;
108 bool supports_memory_tagging () override
;
110 /* Read memory allocation tags from memory via PTRACE. */
111 bool fetch_memtags (CORE_ADDR address
, size_t len
,
112 gdb::byte_vector
&tags
, int type
) override
;
114 /* Write allocation tags to memory via PTRACE. */
115 bool store_memtags (CORE_ADDR address
, size_t len
,
116 const gdb::byte_vector
&tags
, int type
) override
;
119 static aarch64_linux_nat_target the_aarch64_linux_nat_target
;
121 /* Per-process data. We don't bind this to a per-inferior registry
122 because of targets like x86 GNU/Linux that need to keep track of
123 processes that aren't bound to any inferior (e.g., fork children,
126 struct aarch64_process_info
129 struct aarch64_process_info
*next
;
131 /* The process identifier. */
134 /* Copy of aarch64 hardware debug registers. */
135 struct aarch64_debug_reg_state state
;
138 static struct aarch64_process_info
*aarch64_process_list
= NULL
;
140 /* Find process data for process PID. */
142 static struct aarch64_process_info
*
143 aarch64_find_process_pid (pid_t pid
)
145 struct aarch64_process_info
*proc
;
147 for (proc
= aarch64_process_list
; proc
; proc
= proc
->next
)
148 if (proc
->pid
== pid
)
154 /* Add process data for process PID. Returns newly allocated info
157 static struct aarch64_process_info
*
158 aarch64_add_process (pid_t pid
)
160 struct aarch64_process_info
*proc
;
162 proc
= XCNEW (struct aarch64_process_info
);
165 proc
->next
= aarch64_process_list
;
166 aarch64_process_list
= proc
;
171 /* Get data specific info for process PID, creating it if necessary.
172 Never returns NULL. */
174 static struct aarch64_process_info
*
175 aarch64_process_info_get (pid_t pid
)
177 struct aarch64_process_info
*proc
;
179 proc
= aarch64_find_process_pid (pid
);
181 proc
= aarch64_add_process (pid
);
186 /* Called whenever GDB is no longer debugging process PID. It deletes
187 data structures that keep track of debug register state. */
190 aarch64_linux_nat_target::low_forget_process (pid_t pid
)
192 struct aarch64_process_info
*proc
, **proc_link
;
194 proc
= aarch64_process_list
;
195 proc_link
= &aarch64_process_list
;
199 if (proc
->pid
== pid
)
201 *proc_link
= proc
->next
;
207 proc_link
= &proc
->next
;
212 /* Get debug registers state for process PID. */
214 struct aarch64_debug_reg_state
*
215 aarch64_get_debug_reg_state (pid_t pid
)
217 return &aarch64_process_info_get (pid
)->state
;
220 /* Fill GDB's register array with the general-purpose register values
221 from the current thread. */
224 fetch_gregs_from_thread (struct regcache
*regcache
)
227 struct gdbarch
*gdbarch
= regcache
->arch ();
231 /* Make sure REGS can hold all registers contents on both aarch64
233 gdb_static_assert (sizeof (regs
) >= 18 * 4);
235 tid
= regcache
->ptid ().lwp ();
237 iovec
.iov_base
= ®s
;
238 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
239 iovec
.iov_len
= 18 * 4;
241 iovec
.iov_len
= sizeof (regs
);
243 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
245 perror_with_name (_("Unable to fetch general registers."));
247 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
248 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, 1);
253 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
254 regcache
->raw_supply (regno
, ®s
[regno
- AARCH64_X0_REGNUM
]);
258 /* Store to the current thread the valid general-purpose register
259 values in the GDB's register array. */
262 store_gregs_to_thread (const struct regcache
*regcache
)
267 struct gdbarch
*gdbarch
= regcache
->arch ();
269 /* Make sure REGS can hold all registers contents on both aarch64
271 gdb_static_assert (sizeof (regs
) >= 18 * 4);
272 tid
= regcache
->ptid ().lwp ();
274 iovec
.iov_base
= ®s
;
275 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
276 iovec
.iov_len
= 18 * 4;
278 iovec
.iov_len
= sizeof (regs
);
280 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
282 perror_with_name (_("Unable to fetch general registers."));
284 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
285 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, 1);
290 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
291 if (REG_VALID
== regcache
->get_register_status (regno
))
292 regcache
->raw_collect (regno
, ®s
[regno
- AARCH64_X0_REGNUM
]);
295 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
297 perror_with_name (_("Unable to store general registers."));
300 /* Fill GDB's register array with the fp/simd register values
301 from the current thread. */
304 fetch_fpregs_from_thread (struct regcache
*regcache
)
309 struct gdbarch
*gdbarch
= regcache
->arch ();
311 /* Make sure REGS can hold all VFP registers contents on both aarch64
313 gdb_static_assert (sizeof regs
>= ARM_VFP3_REGS_SIZE
);
315 tid
= regcache
->ptid ().lwp ();
317 iovec
.iov_base
= ®s
;
319 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
321 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
323 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
325 perror_with_name (_("Unable to fetch VFP registers."));
327 aarch32_vfp_regcache_supply (regcache
, (gdb_byte
*) ®s
, 32);
333 iovec
.iov_len
= sizeof (regs
);
335 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
337 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
339 for (regno
= AARCH64_V0_REGNUM
; regno
<= AARCH64_V31_REGNUM
; regno
++)
340 regcache
->raw_supply (regno
, ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
342 regcache
->raw_supply (AARCH64_FPSR_REGNUM
, ®s
.fpsr
);
343 regcache
->raw_supply (AARCH64_FPCR_REGNUM
, ®s
.fpcr
);
347 /* Store to the current thread the valid fp/simd register
348 values in the GDB's register array. */
351 store_fpregs_to_thread (const struct regcache
*regcache
)
356 struct gdbarch
*gdbarch
= regcache
->arch ();
358 /* Make sure REGS can hold all VFP registers contents on both aarch64
360 gdb_static_assert (sizeof regs
>= ARM_VFP3_REGS_SIZE
);
361 tid
= regcache
->ptid ().lwp ();
363 iovec
.iov_base
= ®s
;
365 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
367 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
369 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
371 perror_with_name (_("Unable to fetch VFP registers."));
373 aarch32_vfp_regcache_collect (regcache
, (gdb_byte
*) ®s
, 32);
379 iovec
.iov_len
= sizeof (regs
);
381 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
383 perror_with_name (_("Unable to fetch FP/SIMD registers."));
385 for (regno
= AARCH64_V0_REGNUM
; regno
<= AARCH64_V31_REGNUM
; regno
++)
386 if (REG_VALID
== regcache
->get_register_status (regno
))
387 regcache
->raw_collect
388 (regno
, (char *) ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
390 if (REG_VALID
== regcache
->get_register_status (AARCH64_FPSR_REGNUM
))
391 regcache
->raw_collect (AARCH64_FPSR_REGNUM
, (char *) ®s
.fpsr
);
392 if (REG_VALID
== regcache
->get_register_status (AARCH64_FPCR_REGNUM
))
393 regcache
->raw_collect (AARCH64_FPCR_REGNUM
, (char *) ®s
.fpcr
);
396 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
398 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
400 perror_with_name (_("Unable to store VFP registers."));
404 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iovec
);
406 perror_with_name (_("Unable to store FP/SIMD registers."));
410 /* Fill GDB's register array with the sve register values
411 from the current thread. */
414 fetch_sveregs_from_thread (struct regcache
*regcache
)
416 std::unique_ptr
<gdb_byte
[]> base
417 = aarch64_sve_get_sveregs (regcache
->ptid ().lwp ());
418 aarch64_sve_regs_copy_to_reg_buf (regcache
, base
.get ());
421 /* Store to the current thread the valid sve register
422 values in the GDB's register array. */
425 store_sveregs_to_thread (struct regcache
*regcache
)
429 int tid
= regcache
->ptid ().lwp ();
431 /* First store vector length to the thread. This is done first to ensure the
432 ptrace buffers read from the kernel are the correct size. */
433 if (!aarch64_sve_set_vq (tid
, regcache
))
434 perror_with_name (_("Unable to set VG register."));
436 /* Obtain a dump of SVE registers from ptrace. */
437 std::unique_ptr
<gdb_byte
[]> base
= aarch64_sve_get_sveregs (tid
);
439 /* Overwrite with regcache state. */
440 aarch64_sve_regs_copy_from_reg_buf (regcache
, base
.get ());
442 /* Write back to the kernel. */
443 iovec
.iov_base
= base
.get ();
444 iovec
.iov_len
= ((struct user_sve_header
*) base
.get ())->size
;
445 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_SVE
, &iovec
);
448 perror_with_name (_("Unable to store sve registers"));
451 /* Fill GDB's register array with the pointer authentication mask values from
452 the current thread. */
455 fetch_pauth_masks_from_thread (struct regcache
*regcache
)
457 aarch64_gdbarch_tdep
*tdep
458 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (regcache
->arch ());
461 uint64_t pauth_regset
[2] = {0, 0};
462 int tid
= regcache
->ptid ().lwp ();
464 iovec
.iov_base
= &pauth_regset
;
465 iovec
.iov_len
= sizeof (pauth_regset
);
467 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_PAC_MASK
, &iovec
);
469 perror_with_name (_("unable to fetch pauth registers."));
471 regcache
->raw_supply (AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
),
473 regcache
->raw_supply (AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
),
477 /* Fill GDB's register array with the MTE register values from
478 the current thread. */
481 fetch_mteregs_from_thread (struct regcache
*regcache
)
483 aarch64_gdbarch_tdep
*tdep
484 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (regcache
->arch ());
485 int regno
= tdep
->mte_reg_base
;
487 gdb_assert (regno
!= -1);
489 uint64_t tag_ctl
= 0;
492 iovec
.iov_base
= &tag_ctl
;
493 iovec
.iov_len
= sizeof (tag_ctl
);
495 int tid
= get_ptrace_pid (regcache
->ptid ());
496 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_TAGGED_ADDR_CTRL
, &iovec
) != 0)
497 perror_with_name (_("unable to fetch MTE registers."));
499 regcache
->raw_supply (regno
, &tag_ctl
);
502 /* Store to the current thread the valid MTE register set in the GDB's
506 store_mteregs_to_thread (struct regcache
*regcache
)
508 aarch64_gdbarch_tdep
*tdep
509 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (regcache
->arch ());
510 int regno
= tdep
->mte_reg_base
;
512 gdb_assert (regno
!= -1);
514 uint64_t tag_ctl
= 0;
516 if (REG_VALID
!= regcache
->get_register_status (regno
))
519 regcache
->raw_collect (regno
, (char *) &tag_ctl
);
523 iovec
.iov_base
= &tag_ctl
;
524 iovec
.iov_len
= sizeof (tag_ctl
);
526 int tid
= get_ptrace_pid (regcache
->ptid ());
527 if (ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_TAGGED_ADDR_CTRL
, &iovec
) != 0)
528 perror_with_name (_("unable to store MTE registers."));
531 /* Implement the "fetch_registers" target_ops method. */
534 aarch64_linux_nat_target::fetch_registers (struct regcache
*regcache
,
537 aarch64_gdbarch_tdep
*tdep
538 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (regcache
->arch ());
542 fetch_gregs_from_thread (regcache
);
543 if (tdep
->has_sve ())
544 fetch_sveregs_from_thread (regcache
);
546 fetch_fpregs_from_thread (regcache
);
548 if (tdep
->has_pauth ())
549 fetch_pauth_masks_from_thread (regcache
);
551 if (tdep
->has_mte ())
552 fetch_mteregs_from_thread (regcache
);
554 else if (regno
< AARCH64_V0_REGNUM
)
555 fetch_gregs_from_thread (regcache
);
556 else if (tdep
->has_sve ())
557 fetch_sveregs_from_thread (regcache
);
559 fetch_fpregs_from_thread (regcache
);
561 if (tdep
->has_pauth ())
563 if (regno
== AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
)
564 || regno
== AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
))
565 fetch_pauth_masks_from_thread (regcache
);
568 /* Fetch individual MTE registers. */
570 && (regno
== tdep
->mte_reg_base
))
571 fetch_mteregs_from_thread (regcache
);
574 /* Implement the "store_registers" target_ops method. */
577 aarch64_linux_nat_target::store_registers (struct regcache
*regcache
,
580 aarch64_gdbarch_tdep
*tdep
581 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (regcache
->arch ());
585 store_gregs_to_thread (regcache
);
586 if (tdep
->has_sve ())
587 store_sveregs_to_thread (regcache
);
589 store_fpregs_to_thread (regcache
);
591 if (tdep
->has_mte ())
592 store_mteregs_to_thread (regcache
);
594 else if (regno
< AARCH64_V0_REGNUM
)
595 store_gregs_to_thread (regcache
);
596 else if (tdep
->has_sve ())
597 store_sveregs_to_thread (regcache
);
599 store_fpregs_to_thread (regcache
);
601 /* Store MTE registers. */
603 && (regno
== tdep
->mte_reg_base
))
604 store_mteregs_to_thread (regcache
);
607 /* Fill register REGNO (if it is a general-purpose register) in
608 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
609 do this for all registers. */
612 fill_gregset (const struct regcache
*regcache
,
613 gdb_gregset_t
*gregsetp
, int regno
)
615 regcache_collect_regset (&aarch64_linux_gregset
, regcache
,
616 regno
, (gdb_byte
*) gregsetp
,
617 AARCH64_LINUX_SIZEOF_GREGSET
);
620 /* Fill GDB's register array with the general-purpose register values
624 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
626 regcache_supply_regset (&aarch64_linux_gregset
, regcache
, -1,
627 (const gdb_byte
*) gregsetp
,
628 AARCH64_LINUX_SIZEOF_GREGSET
);
631 /* Fill register REGNO (if it is a floating-point register) in
632 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
633 do this for all registers. */
636 fill_fpregset (const struct regcache
*regcache
,
637 gdb_fpregset_t
*fpregsetp
, int regno
)
639 regcache_collect_regset (&aarch64_linux_fpregset
, regcache
,
640 regno
, (gdb_byte
*) fpregsetp
,
641 AARCH64_LINUX_SIZEOF_FPREGSET
);
644 /* Fill GDB's register array with the floating-point register values
648 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
650 regcache_supply_regset (&aarch64_linux_fpregset
, regcache
, -1,
651 (const gdb_byte
*) fpregsetp
,
652 AARCH64_LINUX_SIZEOF_FPREGSET
);
655 /* linux_nat_new_fork hook. */
658 aarch64_linux_nat_target::low_new_fork (struct lwp_info
*parent
,
662 struct aarch64_debug_reg_state
*parent_state
;
663 struct aarch64_debug_reg_state
*child_state
;
665 /* NULL means no watchpoint has ever been set in the parent. In
666 that case, there's nothing to do. */
667 if (parent
->arch_private
== NULL
)
670 /* GDB core assumes the child inherits the watchpoints/hw
671 breakpoints of the parent, and will remove them all from the
672 forked off process. Copy the debug registers mirrors into the
673 new process so that all breakpoints and watchpoints can be
676 parent_pid
= parent
->ptid
.pid ();
677 parent_state
= aarch64_get_debug_reg_state (parent_pid
);
678 child_state
= aarch64_get_debug_reg_state (child_pid
);
679 *child_state
= *parent_state
;
683 /* Called by libthread_db. Returns a pointer to the thread local
684 storage (or its descriptor). */
687 ps_get_thread_area (struct ps_prochandle
*ph
,
688 lwpid_t lwpid
, int idx
, void **base
)
691 = (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word
== 64);
693 return aarch64_ps_get_thread_area (ph
, lwpid
, idx
, base
, is_64bit_p
);
697 /* Implement the "post_startup_inferior" target_ops method. */
700 aarch64_linux_nat_target::post_startup_inferior (ptid_t ptid
)
702 low_forget_process (ptid
.pid ());
703 aarch64_linux_get_debug_reg_capacity (ptid
.pid ());
704 linux_nat_target::post_startup_inferior (ptid
);
707 /* Implement the "post_attach" target_ops method. */
710 aarch64_linux_nat_target::post_attach (int pid
)
712 low_forget_process (pid
);
713 /* Set the hardware debug register capacity. If
714 aarch64_linux_get_debug_reg_capacity is not called
715 (as it is in aarch64_linux_child_post_startup_inferior) then
716 software watchpoints will be used instead of hardware
717 watchpoints when attaching to a target. */
718 aarch64_linux_get_debug_reg_capacity (pid
);
719 linux_nat_target::post_attach (pid
);
722 /* Implement the "read_description" target_ops method. */
724 const struct target_desc
*
725 aarch64_linux_nat_target::read_description ()
728 gdb_byte regbuf
[ARM_VFP3_REGS_SIZE
];
731 tid
= inferior_ptid
.pid ();
733 iovec
.iov_base
= regbuf
;
734 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
736 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
738 return aarch32_read_description ();
740 CORE_ADDR hwcap
= linux_get_hwcap (this);
741 CORE_ADDR hwcap2
= linux_get_hwcap2 (this);
743 bool pauth_p
= hwcap
& AARCH64_HWCAP_PACA
;
744 bool mte_p
= hwcap2
& HWCAP2_MTE
;
746 return aarch64_read_description (aarch64_sve_get_vq (tid
), pauth_p
, mte_p
);
749 /* Convert a native/host siginfo object, into/from the siginfo in the
750 layout of the inferiors' architecture. Returns true if any
751 conversion was done; false otherwise. If DIRECTION is 1, then copy
752 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
756 aarch64_linux_nat_target::low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
759 struct gdbarch
*gdbarch
= get_frame_arch (get_current_frame ());
761 /* Is the inferior 32-bit? If so, then do fixup the siginfo
763 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
766 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo
*) inf
,
769 aarch64_siginfo_from_compat_siginfo (native
,
770 (struct compat_siginfo
*) inf
);
778 /* Returns the number of hardware watchpoints of type TYPE that we can
779 set. Value is positive if we can set CNT watchpoints, zero if
780 setting watchpoints of type TYPE is not supported, and negative if
781 CNT is more than the maximum number of watchpoints of type TYPE
782 that we can support. TYPE is one of bp_hardware_watchpoint,
783 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
784 CNT is the number of such watchpoints used so far (including this
785 one). OTHERTYPE is non-zero if other types of watchpoints are
786 currently enabled. */
789 aarch64_linux_nat_target::can_use_hw_breakpoint (enum bptype type
,
790 int cnt
, int othertype
)
792 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
793 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
795 if (aarch64_num_wp_regs
== 0)
798 else if (type
== bp_hardware_breakpoint
)
800 if (aarch64_num_bp_regs
== 0)
804 gdb_assert_not_reached ("unexpected breakpoint type");
806 /* We always return 1 here because we don't have enough information
807 about possible overlap of addresses that they want to watch. As an
808 extreme example, consider the case where all the watchpoints watch
809 the same address and the same region length: then we can handle a
810 virtually unlimited number of watchpoints, due to debug register
811 sharing implemented via reference counts. */
815 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
816 Return 0 on success, -1 on failure. */
819 aarch64_linux_nat_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
820 struct bp_target_info
*bp_tgt
)
823 CORE_ADDR addr
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
825 const enum target_hw_bp_type type
= hw_execute
;
826 struct aarch64_debug_reg_state
*state
827 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
829 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &len
);
834 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
835 (unsigned long) addr
, len
);
837 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 1 /* is_insert */, state
);
841 aarch64_show_debug_reg_state (state
,
842 "insert_hw_breakpoint", addr
, len
, type
);
848 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
849 Return 0 on success, -1 on failure. */
852 aarch64_linux_nat_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
853 struct bp_target_info
*bp_tgt
)
856 CORE_ADDR addr
= bp_tgt
->placed_address
;
858 const enum target_hw_bp_type type
= hw_execute
;
859 struct aarch64_debug_reg_state
*state
860 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
862 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &len
);
866 (gdb_stdlog
, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
867 (unsigned long) addr
, len
);
869 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 0 /* is_insert */, state
);
873 aarch64_show_debug_reg_state (state
,
874 "remove_hw_watchpoint", addr
, len
, type
);
880 /* Implement the "insert_watchpoint" target_ops method.
882 Insert a watchpoint to watch a memory region which starts at
883 address ADDR and whose length is LEN bytes. Watch memory accesses
884 of the type TYPE. Return 0 on success, -1 on failure. */
887 aarch64_linux_nat_target::insert_watchpoint (CORE_ADDR addr
, int len
,
888 enum target_hw_bp_type type
,
889 struct expression
*cond
)
892 struct aarch64_debug_reg_state
*state
893 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
896 fprintf_unfiltered (gdb_stdlog
,
897 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
898 (unsigned long) addr
, len
);
900 gdb_assert (type
!= hw_execute
);
902 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 1 /* is_insert */, state
);
906 aarch64_show_debug_reg_state (state
,
907 "insert_watchpoint", addr
, len
, type
);
913 /* Implement the "remove_watchpoint" target_ops method.
914 Remove a watchpoint that watched the memory region which starts at
915 address ADDR, whose length is LEN bytes, and for accesses of the
916 type TYPE. Return 0 on success, -1 on failure. */
919 aarch64_linux_nat_target::remove_watchpoint (CORE_ADDR addr
, int len
,
920 enum target_hw_bp_type type
,
921 struct expression
*cond
)
924 struct aarch64_debug_reg_state
*state
925 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
928 fprintf_unfiltered (gdb_stdlog
,
929 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
930 (unsigned long) addr
, len
);
932 gdb_assert (type
!= hw_execute
);
934 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 0 /* is_insert */, state
);
938 aarch64_show_debug_reg_state (state
,
939 "remove_watchpoint", addr
, len
, type
);
945 /* Implement the "region_ok_for_hw_watchpoint" target_ops method. */
948 aarch64_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
950 return aarch64_linux_region_ok_for_watchpoint (addr
, len
);
953 /* Implement the "stopped_data_address" target_ops method. */
956 aarch64_linux_nat_target::stopped_data_address (CORE_ADDR
*addr_p
)
960 struct aarch64_debug_reg_state
*state
;
962 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
965 /* This must be a hardware breakpoint. */
966 if (siginfo
.si_signo
!= SIGTRAP
967 || (siginfo
.si_code
& 0xffff) != TRAP_HWBKPT
)
970 /* Make sure to ignore the top byte, otherwise we may not recognize a
971 hardware watchpoint hit. The stopped data addresses coming from the
972 kernel can potentially be tagged addresses. */
973 struct gdbarch
*gdbarch
= thread_architecture (inferior_ptid
);
974 const CORE_ADDR addr_trap
975 = address_significant (gdbarch
, (CORE_ADDR
) siginfo
.si_addr
);
977 /* Check if the address matches any watched address. */
978 state
= aarch64_get_debug_reg_state (inferior_ptid
.pid ());
979 for (i
= aarch64_num_wp_regs
- 1; i
>= 0; --i
)
981 const unsigned int offset
982 = aarch64_watchpoint_offset (state
->dr_ctrl_wp
[i
]);
983 const unsigned int len
= aarch64_watchpoint_length (state
->dr_ctrl_wp
[i
]);
984 const CORE_ADDR addr_watch
= state
->dr_addr_wp
[i
] + offset
;
985 const CORE_ADDR addr_watch_aligned
= align_down (state
->dr_addr_wp
[i
], 8);
986 const CORE_ADDR addr_orig
= state
->dr_addr_orig_wp
[i
];
988 if (state
->dr_ref_count_wp
[i
]
989 && DR_CONTROL_ENABLED (state
->dr_ctrl_wp
[i
])
990 && addr_trap
>= addr_watch_aligned
991 && addr_trap
< addr_watch
+ len
)
993 /* ADDR_TRAP reports the first address of the memory range
994 accessed by the CPU, regardless of what was the memory
995 range watched. Thus, a large CPU access that straddles
996 the ADDR_WATCH..ADDR_WATCH+LEN range may result in an
997 ADDR_TRAP that is lower than the
998 ADDR_WATCH..ADDR_WATCH+LEN range. E.g.:
1000 addr: | 4 | 5 | 6 | 7 | 8 |
1001 |---- range watched ----|
1002 |----------- range accessed ------------|
1004 In this case, ADDR_TRAP will be 4.
1006 To match a watchpoint known to GDB core, we must never
1007 report *ADDR_P outside of any ADDR_WATCH..ADDR_WATCH+LEN
1008 range. ADDR_WATCH <= ADDR_TRAP < ADDR_ORIG is a false
1009 positive on kernels older than 4.10. See PR
1011 *addr_p
= addr_orig
;
1019 /* Implement the "stopped_by_watchpoint" target_ops method. */
1022 aarch64_linux_nat_target::stopped_by_watchpoint ()
1026 return stopped_data_address (&addr
);
1029 /* Implement the "watchpoint_addr_within_range" target_ops method. */
1032 aarch64_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr
,
1033 CORE_ADDR start
, int length
)
1035 return start
<= addr
&& start
+ length
- 1 >= addr
;
1038 /* Implement the "can_do_single_step" target_ops method. */
1041 aarch64_linux_nat_target::can_do_single_step ()
1046 /* Implement the "thread_architecture" target_ops method. */
1049 aarch64_linux_nat_target::thread_architecture (ptid_t ptid
)
1051 /* Return the gdbarch for the current thread. If the vector length has
1052 changed since the last time this was called, then do a further lookup. */
1054 uint64_t vq
= aarch64_sve_get_vq (ptid
.lwp ());
1056 /* Find the current gdbarch the same way as process_stratum_target. Only
1057 return it if the current vector length matches the one in the tdep. */
1058 inferior
*inf
= find_inferior_ptid (this, ptid
);
1059 gdb_assert (inf
!= NULL
);
1060 aarch64_gdbarch_tdep
*tdep
1061 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (inf
->gdbarch
);
1063 return inf
->gdbarch
;
1065 /* We reach here if the vector length for the thread is different from its
1066 value at process start. Lookup gdbarch via info (potentially creating a
1067 new one), stashing the vector length inside id. Use -1 for when SVE
1068 unavailable, to distinguish from an unset value of 0. */
1069 struct gdbarch_info info
;
1070 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_aarch64
, bfd_mach_aarch64
);
1071 info
.id
= (int *) (vq
== 0 ? -1 : vq
);
1072 return gdbarch_find_by_info (info
);
1075 /* Implement the "supports_memory_tagging" target_ops method. */
1078 aarch64_linux_nat_target::supports_memory_tagging ()
1080 return (linux_get_hwcap2 (this) & HWCAP2_MTE
) != 0;
1083 /* Implement the "fetch_memtags" target_ops method. */
1086 aarch64_linux_nat_target::fetch_memtags (CORE_ADDR address
, size_t len
,
1087 gdb::byte_vector
&tags
, int type
)
1089 int tid
= get_ptrace_pid (inferior_ptid
);
1091 /* Allocation tags? */
1092 if (type
== static_cast<int> (aarch64_memtag_type::mte_allocation
))
1093 return aarch64_mte_fetch_memtags (tid
, address
, len
, tags
);
1098 /* Implement the "store_memtags" target_ops method. */
1101 aarch64_linux_nat_target::store_memtags (CORE_ADDR address
, size_t len
,
1102 const gdb::byte_vector
&tags
, int type
)
1104 int tid
= get_ptrace_pid (inferior_ptid
);
1106 /* Allocation tags? */
1107 if (type
== static_cast<int> (aarch64_memtag_type::mte_allocation
))
1108 return aarch64_mte_store_memtags (tid
, address
, len
, tags
);
1113 /* Define AArch64 maintenance commands. */
1116 add_show_debug_regs_command (void)
1118 /* A maintenance command to enable printing the internal DRi mirror
1120 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance
,
1121 &show_debug_regs
, _("\
1122 Set whether to show variables that mirror the AArch64 debug registers."), _("\
1123 Show whether to show variables that mirror the AArch64 debug registers."), _("\
1124 Use \"on\" to enable, \"off\" to disable.\n\
1125 If enabled, the debug registers values are shown when GDB inserts\n\
1126 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
1127 triggers a breakpoint or watchpoint."),
1130 &maintenance_set_cmdlist
,
1131 &maintenance_show_cmdlist
);
1134 void _initialize_aarch64_linux_nat ();
1136 _initialize_aarch64_linux_nat ()
1138 add_show_debug_regs_command ();
1140 /* Register the target. */
1141 linux_target
= &the_aarch64_linux_nat_target
;
1142 add_inf_child_target (&the_aarch64_linux_nat_target
);