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"
56 #define TRAP_HWBKPT 0x0004
59 class aarch64_linux_nat_target final
: public linux_nat_target
62 /* Add our register access methods. */
63 void fetch_registers (struct regcache
*, int) override
;
64 void store_registers (struct regcache
*, int) override
;
66 const struct target_desc
*read_description () override
;
68 /* Add our hardware breakpoint and watchpoint implementation. */
69 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
70 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
71 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
72 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
73 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
74 struct expression
*) override
;
75 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
76 struct expression
*) override
;
77 bool stopped_by_watchpoint () override
;
78 bool stopped_data_address (CORE_ADDR
*) override
;
79 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
81 int can_do_single_step () override
;
83 /* Override the GNU/Linux inferior startup hook. */
84 void post_startup_inferior (ptid_t
) override
;
86 /* Override the GNU/Linux post attach hook. */
87 void post_attach (int pid
) override
;
89 /* These three defer to common nat/ code. */
90 void low_new_thread (struct lwp_info
*lp
) override
91 { aarch64_linux_new_thread (lp
); }
92 void low_delete_thread (struct arch_lwp_info
*lp
) override
93 { aarch64_linux_delete_thread (lp
); }
94 void low_prepare_to_resume (struct lwp_info
*lp
) override
95 { aarch64_linux_prepare_to_resume (lp
); }
97 void low_new_fork (struct lwp_info
*parent
, pid_t child_pid
) override
;
98 void low_forget_process (pid_t pid
) override
;
100 /* Add our siginfo layout converter. */
101 bool low_siginfo_fixup (siginfo_t
*ptrace
, gdb_byte
*inf
, int direction
)
104 struct gdbarch
*thread_architecture (ptid_t
) override
;
107 static aarch64_linux_nat_target the_aarch64_linux_nat_target
;
109 /* Per-process data. We don't bind this to a per-inferior registry
110 because of targets like x86 GNU/Linux that need to keep track of
111 processes that aren't bound to any inferior (e.g., fork children,
114 struct aarch64_process_info
117 struct aarch64_process_info
*next
;
119 /* The process identifier. */
122 /* Copy of aarch64 hardware debug registers. */
123 struct aarch64_debug_reg_state state
;
126 static struct aarch64_process_info
*aarch64_process_list
= NULL
;
128 /* Find process data for process PID. */
130 static struct aarch64_process_info
*
131 aarch64_find_process_pid (pid_t pid
)
133 struct aarch64_process_info
*proc
;
135 for (proc
= aarch64_process_list
; proc
; proc
= proc
->next
)
136 if (proc
->pid
== pid
)
142 /* Add process data for process PID. Returns newly allocated info
145 static struct aarch64_process_info
*
146 aarch64_add_process (pid_t pid
)
148 struct aarch64_process_info
*proc
;
150 proc
= XCNEW (struct aarch64_process_info
);
153 proc
->next
= aarch64_process_list
;
154 aarch64_process_list
= proc
;
159 /* Get data specific info for process PID, creating it if necessary.
160 Never returns NULL. */
162 static struct aarch64_process_info
*
163 aarch64_process_info_get (pid_t pid
)
165 struct aarch64_process_info
*proc
;
167 proc
= aarch64_find_process_pid (pid
);
169 proc
= aarch64_add_process (pid
);
174 /* Called whenever GDB is no longer debugging process PID. It deletes
175 data structures that keep track of debug register state. */
178 aarch64_linux_nat_target::low_forget_process (pid_t pid
)
180 struct aarch64_process_info
*proc
, **proc_link
;
182 proc
= aarch64_process_list
;
183 proc_link
= &aarch64_process_list
;
187 if (proc
->pid
== pid
)
189 *proc_link
= proc
->next
;
195 proc_link
= &proc
->next
;
200 /* Get debug registers state for process PID. */
202 struct aarch64_debug_reg_state
*
203 aarch64_get_debug_reg_state (pid_t pid
)
205 return &aarch64_process_info_get (pid
)->state
;
208 /* Fill GDB's register array with the general-purpose register values
209 from the current thread. */
212 fetch_gregs_from_thread (struct regcache
*regcache
)
215 struct gdbarch
*gdbarch
= regcache
->arch ();
219 /* Make sure REGS can hold all registers contents on both aarch64
221 gdb_static_assert (sizeof (regs
) >= 18 * 4);
223 tid
= regcache
->ptid ().lwp ();
225 iovec
.iov_base
= ®s
;
226 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
227 iovec
.iov_len
= 18 * 4;
229 iovec
.iov_len
= sizeof (regs
);
231 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
233 perror_with_name (_("Unable to fetch general registers."));
235 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
236 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, 1);
241 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
242 regcache
->raw_supply (regno
, ®s
[regno
- AARCH64_X0_REGNUM
]);
246 /* Store to the current thread the valid general-purpose register
247 values in the GDB's register array. */
250 store_gregs_to_thread (const struct regcache
*regcache
)
255 struct gdbarch
*gdbarch
= regcache
->arch ();
257 /* Make sure REGS can hold all registers contents on both aarch64
259 gdb_static_assert (sizeof (regs
) >= 18 * 4);
260 tid
= regcache
->ptid ().lwp ();
262 iovec
.iov_base
= ®s
;
263 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
264 iovec
.iov_len
= 18 * 4;
266 iovec
.iov_len
= sizeof (regs
);
268 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
270 perror_with_name (_("Unable to fetch general registers."));
272 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
273 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, 1);
278 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
279 if (REG_VALID
== regcache
->get_register_status (regno
))
280 regcache
->raw_collect (regno
, ®s
[regno
- AARCH64_X0_REGNUM
]);
283 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
285 perror_with_name (_("Unable to store general registers."));
288 /* Fill GDB's register array with the fp/simd register values
289 from the current thread. */
292 fetch_fpregs_from_thread (struct regcache
*regcache
)
297 struct gdbarch
*gdbarch
= regcache
->arch ();
299 /* Make sure REGS can hold all VFP registers contents on both aarch64
301 gdb_static_assert (sizeof regs
>= ARM_VFP3_REGS_SIZE
);
303 tid
= regcache
->ptid ().lwp ();
305 iovec
.iov_base
= ®s
;
307 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
309 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
311 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
313 perror_with_name (_("Unable to fetch VFP registers."));
315 aarch32_vfp_regcache_supply (regcache
, (gdb_byte
*) ®s
, 32);
321 iovec
.iov_len
= sizeof (regs
);
323 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
325 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
327 for (regno
= AARCH64_V0_REGNUM
; regno
<= AARCH64_V31_REGNUM
; regno
++)
328 regcache
->raw_supply (regno
, ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
330 regcache
->raw_supply (AARCH64_FPSR_REGNUM
, ®s
.fpsr
);
331 regcache
->raw_supply (AARCH64_FPCR_REGNUM
, ®s
.fpcr
);
335 /* Store to the current thread the valid fp/simd register
336 values in the GDB's register array. */
339 store_fpregs_to_thread (const struct regcache
*regcache
)
344 struct gdbarch
*gdbarch
= regcache
->arch ();
346 /* Make sure REGS can hold all VFP registers contents on both aarch64
348 gdb_static_assert (sizeof regs
>= ARM_VFP3_REGS_SIZE
);
349 tid
= regcache
->ptid ().lwp ();
351 iovec
.iov_base
= ®s
;
353 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
355 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
357 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
359 perror_with_name (_("Unable to fetch VFP registers."));
361 aarch32_vfp_regcache_collect (regcache
, (gdb_byte
*) ®s
, 32);
367 iovec
.iov_len
= sizeof (regs
);
369 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
371 perror_with_name (_("Unable to fetch FP/SIMD registers."));
373 for (regno
= AARCH64_V0_REGNUM
; regno
<= AARCH64_V31_REGNUM
; regno
++)
374 if (REG_VALID
== regcache
->get_register_status (regno
))
375 regcache
->raw_collect
376 (regno
, (char *) ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
378 if (REG_VALID
== regcache
->get_register_status (AARCH64_FPSR_REGNUM
))
379 regcache
->raw_collect (AARCH64_FPSR_REGNUM
, (char *) ®s
.fpsr
);
380 if (REG_VALID
== regcache
->get_register_status (AARCH64_FPCR_REGNUM
))
381 regcache
->raw_collect (AARCH64_FPCR_REGNUM
, (char *) ®s
.fpcr
);
384 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
386 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
388 perror_with_name (_("Unable to store VFP registers."));
392 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iovec
);
394 perror_with_name (_("Unable to store FP/SIMD registers."));
398 /* Fill GDB's register array with the sve register values
399 from the current thread. */
402 fetch_sveregs_from_thread (struct regcache
*regcache
)
404 std::unique_ptr
<gdb_byte
[]> base
405 = aarch64_sve_get_sveregs (regcache
->ptid ().lwp ());
406 aarch64_sve_regs_copy_to_reg_buf (regcache
, base
.get ());
409 /* Store to the current thread the valid sve register
410 values in the GDB's register array. */
413 store_sveregs_to_thread (struct regcache
*regcache
)
417 int tid
= regcache
->ptid ().lwp ();
419 /* First store vector length to the thread. This is done first to ensure the
420 ptrace buffers read from the kernel are the correct size. */
421 if (!aarch64_sve_set_vq (tid
, regcache
))
422 perror_with_name (_("Unable to set VG register."));
424 /* Obtain a dump of SVE registers from ptrace. */
425 std::unique_ptr
<gdb_byte
[]> base
= aarch64_sve_get_sveregs (tid
);
427 /* Overwrite with regcache state. */
428 aarch64_sve_regs_copy_from_reg_buf (regcache
, base
.get ());
430 /* Write back to the kernel. */
431 iovec
.iov_base
= base
.get ();
432 iovec
.iov_len
= ((struct user_sve_header
*) base
.get ())->size
;
433 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_SVE
, &iovec
);
436 perror_with_name (_("Unable to store sve registers"));
439 /* Fill GDB's register array with the pointer authentication mask values from
440 the current thread. */
443 fetch_pauth_masks_from_thread (struct regcache
*regcache
)
445 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
448 uint64_t pauth_regset
[2] = {0, 0};
449 int tid
= regcache
->ptid ().lwp ();
451 iovec
.iov_base
= &pauth_regset
;
452 iovec
.iov_len
= sizeof (pauth_regset
);
454 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_PAC_MASK
, &iovec
);
456 perror_with_name (_("unable to fetch pauth registers."));
458 regcache
->raw_supply (AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
),
460 regcache
->raw_supply (AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
),
464 /* Implement the "fetch_registers" target_ops method. */
467 aarch64_linux_nat_target::fetch_registers (struct regcache
*regcache
,
470 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
474 fetch_gregs_from_thread (regcache
);
475 if (tdep
->has_sve ())
476 fetch_sveregs_from_thread (regcache
);
478 fetch_fpregs_from_thread (regcache
);
480 if (tdep
->has_pauth ())
481 fetch_pauth_masks_from_thread (regcache
);
483 else if (regno
< AARCH64_V0_REGNUM
)
484 fetch_gregs_from_thread (regcache
);
485 else if (tdep
->has_sve ())
486 fetch_sveregs_from_thread (regcache
);
488 fetch_fpregs_from_thread (regcache
);
490 if (tdep
->has_pauth ())
492 if (regno
== AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
)
493 || regno
== AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
))
494 fetch_pauth_masks_from_thread (regcache
);
498 /* Implement the "store_registers" target_ops method. */
501 aarch64_linux_nat_target::store_registers (struct regcache
*regcache
,
504 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
508 store_gregs_to_thread (regcache
);
509 if (tdep
->has_sve ())
510 store_sveregs_to_thread (regcache
);
512 store_fpregs_to_thread (regcache
);
514 else if (regno
< AARCH64_V0_REGNUM
)
515 store_gregs_to_thread (regcache
);
516 else if (tdep
->has_sve ())
517 store_sveregs_to_thread (regcache
);
519 store_fpregs_to_thread (regcache
);
522 /* Fill register REGNO (if it is a general-purpose register) in
523 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
524 do this for all registers. */
527 fill_gregset (const struct regcache
*regcache
,
528 gdb_gregset_t
*gregsetp
, int regno
)
530 regcache_collect_regset (&aarch64_linux_gregset
, regcache
,
531 regno
, (gdb_byte
*) gregsetp
,
532 AARCH64_LINUX_SIZEOF_GREGSET
);
535 /* Fill GDB's register array with the general-purpose register values
539 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
541 regcache_supply_regset (&aarch64_linux_gregset
, regcache
, -1,
542 (const gdb_byte
*) gregsetp
,
543 AARCH64_LINUX_SIZEOF_GREGSET
);
546 /* Fill register REGNO (if it is a floating-point register) in
547 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
548 do this for all registers. */
551 fill_fpregset (const struct regcache
*regcache
,
552 gdb_fpregset_t
*fpregsetp
, int regno
)
554 regcache_collect_regset (&aarch64_linux_fpregset
, regcache
,
555 regno
, (gdb_byte
*) fpregsetp
,
556 AARCH64_LINUX_SIZEOF_FPREGSET
);
559 /* Fill GDB's register array with the floating-point register values
563 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
565 regcache_supply_regset (&aarch64_linux_fpregset
, regcache
, -1,
566 (const gdb_byte
*) fpregsetp
,
567 AARCH64_LINUX_SIZEOF_FPREGSET
);
570 /* linux_nat_new_fork hook. */
573 aarch64_linux_nat_target::low_new_fork (struct lwp_info
*parent
,
577 struct aarch64_debug_reg_state
*parent_state
;
578 struct aarch64_debug_reg_state
*child_state
;
580 /* NULL means no watchpoint has ever been set in the parent. In
581 that case, there's nothing to do. */
582 if (parent
->arch_private
== NULL
)
585 /* GDB core assumes the child inherits the watchpoints/hw
586 breakpoints of the parent, and will remove them all from the
587 forked off process. Copy the debug registers mirrors into the
588 new process so that all breakpoints and watchpoints can be
591 parent_pid
= parent
->ptid
.pid ();
592 parent_state
= aarch64_get_debug_reg_state (parent_pid
);
593 child_state
= aarch64_get_debug_reg_state (child_pid
);
594 *child_state
= *parent_state
;
598 /* Called by libthread_db. Returns a pointer to the thread local
599 storage (or its descriptor). */
602 ps_get_thread_area (struct ps_prochandle
*ph
,
603 lwpid_t lwpid
, int idx
, void **base
)
606 = (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word
== 64);
608 return aarch64_ps_get_thread_area (ph
, lwpid
, idx
, base
, is_64bit_p
);
612 /* Implement the "post_startup_inferior" target_ops method. */
615 aarch64_linux_nat_target::post_startup_inferior (ptid_t ptid
)
617 low_forget_process (ptid
.pid ());
618 aarch64_linux_get_debug_reg_capacity (ptid
.pid ());
619 linux_nat_target::post_startup_inferior (ptid
);
622 /* Implement the "post_attach" target_ops method. */
625 aarch64_linux_nat_target::post_attach (int pid
)
627 low_forget_process (pid
);
628 /* Set the hardware debug register capacity. If
629 aarch64_linux_get_debug_reg_capacity is not called
630 (as it is in aarch64_linux_child_post_startup_inferior) then
631 software watchpoints will be used instead of hardware
632 watchpoints when attaching to a target. */
633 aarch64_linux_get_debug_reg_capacity (pid
);
634 linux_nat_target::post_attach (pid
);
637 /* Implement the "read_description" target_ops method. */
639 const struct target_desc
*
640 aarch64_linux_nat_target::read_description ()
643 gdb_byte regbuf
[ARM_VFP3_REGS_SIZE
];
646 tid
= inferior_ptid
.lwp ();
648 iovec
.iov_base
= regbuf
;
649 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
651 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
653 return aarch32_read_description ();
655 CORE_ADDR hwcap
= linux_get_hwcap (this);
657 return aarch64_read_description (aarch64_sve_get_vq (tid
),
658 hwcap
& AARCH64_HWCAP_PACA
);
661 /* Convert a native/host siginfo object, into/from the siginfo in the
662 layout of the inferiors' architecture. Returns true if any
663 conversion was done; false otherwise. If DIRECTION is 1, then copy
664 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
668 aarch64_linux_nat_target::low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
671 struct gdbarch
*gdbarch
= get_frame_arch (get_current_frame ());
673 /* Is the inferior 32-bit? If so, then do fixup the siginfo
675 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
678 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo
*) inf
,
681 aarch64_siginfo_from_compat_siginfo (native
,
682 (struct compat_siginfo
*) inf
);
690 /* Returns the number of hardware watchpoints of type TYPE that we can
691 set. Value is positive if we can set CNT watchpoints, zero if
692 setting watchpoints of type TYPE is not supported, and negative if
693 CNT is more than the maximum number of watchpoints of type TYPE
694 that we can support. TYPE is one of bp_hardware_watchpoint,
695 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
696 CNT is the number of such watchpoints used so far (including this
697 one). OTHERTYPE is non-zero if other types of watchpoints are
698 currently enabled. */
701 aarch64_linux_nat_target::can_use_hw_breakpoint (enum bptype type
,
702 int cnt
, int othertype
)
704 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
705 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
707 if (aarch64_num_wp_regs
== 0)
710 else if (type
== bp_hardware_breakpoint
)
712 if (aarch64_num_bp_regs
== 0)
716 gdb_assert_not_reached ("unexpected breakpoint type");
718 /* We always return 1 here because we don't have enough information
719 about possible overlap of addresses that they want to watch. As an
720 extreme example, consider the case where all the watchpoints watch
721 the same address and the same region length: then we can handle a
722 virtually unlimited number of watchpoints, due to debug register
723 sharing implemented via reference counts. */
727 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
728 Return 0 on success, -1 on failure. */
731 aarch64_linux_nat_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
732 struct bp_target_info
*bp_tgt
)
735 CORE_ADDR addr
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
737 const enum target_hw_bp_type type
= hw_execute
;
738 struct aarch64_debug_reg_state
*state
739 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
741 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &len
);
746 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
747 (unsigned long) addr
, len
);
749 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 1 /* is_insert */, state
);
753 aarch64_show_debug_reg_state (state
,
754 "insert_hw_breakpoint", addr
, len
, type
);
760 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
761 Return 0 on success, -1 on failure. */
764 aarch64_linux_nat_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
765 struct bp_target_info
*bp_tgt
)
768 CORE_ADDR addr
= bp_tgt
->placed_address
;
770 const enum target_hw_bp_type type
= hw_execute
;
771 struct aarch64_debug_reg_state
*state
772 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
774 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &len
);
778 (gdb_stdlog
, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
779 (unsigned long) addr
, len
);
781 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 0 /* is_insert */, state
);
785 aarch64_show_debug_reg_state (state
,
786 "remove_hw_watchpoint", addr
, len
, type
);
792 /* Implement the "insert_watchpoint" target_ops method.
794 Insert a watchpoint to watch a memory region which starts at
795 address ADDR and whose length is LEN bytes. Watch memory accesses
796 of the type TYPE. Return 0 on success, -1 on failure. */
799 aarch64_linux_nat_target::insert_watchpoint (CORE_ADDR addr
, int len
,
800 enum target_hw_bp_type type
,
801 struct expression
*cond
)
804 struct aarch64_debug_reg_state
*state
805 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
808 fprintf_unfiltered (gdb_stdlog
,
809 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
810 (unsigned long) addr
, len
);
812 gdb_assert (type
!= hw_execute
);
814 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 1 /* is_insert */, state
);
818 aarch64_show_debug_reg_state (state
,
819 "insert_watchpoint", addr
, len
, type
);
825 /* Implement the "remove_watchpoint" target_ops method.
826 Remove a watchpoint that watched the memory region which starts at
827 address ADDR, whose length is LEN bytes, and for accesses of the
828 type TYPE. Return 0 on success, -1 on failure. */
831 aarch64_linux_nat_target::remove_watchpoint (CORE_ADDR addr
, int len
,
832 enum target_hw_bp_type type
,
833 struct expression
*cond
)
836 struct aarch64_debug_reg_state
*state
837 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
840 fprintf_unfiltered (gdb_stdlog
,
841 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
842 (unsigned long) addr
, len
);
844 gdb_assert (type
!= hw_execute
);
846 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 0 /* is_insert */, state
);
850 aarch64_show_debug_reg_state (state
,
851 "remove_watchpoint", addr
, len
, type
);
857 /* Implement the "region_ok_for_hw_watchpoint" target_ops method. */
860 aarch64_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
862 return aarch64_linux_region_ok_for_watchpoint (addr
, len
);
865 /* Implement the "stopped_data_address" target_ops method. */
868 aarch64_linux_nat_target::stopped_data_address (CORE_ADDR
*addr_p
)
872 struct aarch64_debug_reg_state
*state
;
874 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
877 /* This must be a hardware breakpoint. */
878 if (siginfo
.si_signo
!= SIGTRAP
879 || (siginfo
.si_code
& 0xffff) != TRAP_HWBKPT
)
882 /* Make sure to ignore the top byte, otherwise we may not recognize a
883 hardware watchpoint hit. The stopped data addresses coming from the
884 kernel can potentially be tagged addresses. */
885 struct gdbarch
*gdbarch
= thread_architecture (inferior_ptid
);
886 const CORE_ADDR addr_trap
887 = address_significant (gdbarch
, (CORE_ADDR
) siginfo
.si_addr
);
889 /* Check if the address matches any watched address. */
890 state
= aarch64_get_debug_reg_state (inferior_ptid
.pid ());
891 for (i
= aarch64_num_wp_regs
- 1; i
>= 0; --i
)
893 const unsigned int offset
894 = aarch64_watchpoint_offset (state
->dr_ctrl_wp
[i
]);
895 const unsigned int len
= aarch64_watchpoint_length (state
->dr_ctrl_wp
[i
]);
896 const CORE_ADDR addr_watch
= state
->dr_addr_wp
[i
] + offset
;
897 const CORE_ADDR addr_watch_aligned
= align_down (state
->dr_addr_wp
[i
], 8);
898 const CORE_ADDR addr_orig
= state
->dr_addr_orig_wp
[i
];
900 if (state
->dr_ref_count_wp
[i
]
901 && DR_CONTROL_ENABLED (state
->dr_ctrl_wp
[i
])
902 && addr_trap
>= addr_watch_aligned
903 && addr_trap
< addr_watch
+ len
)
905 /* ADDR_TRAP reports the first address of the memory range
906 accessed by the CPU, regardless of what was the memory
907 range watched. Thus, a large CPU access that straddles
908 the ADDR_WATCH..ADDR_WATCH+LEN range may result in an
909 ADDR_TRAP that is lower than the
910 ADDR_WATCH..ADDR_WATCH+LEN range. E.g.:
912 addr: | 4 | 5 | 6 | 7 | 8 |
913 |---- range watched ----|
914 |----------- range accessed ------------|
916 In this case, ADDR_TRAP will be 4.
918 To match a watchpoint known to GDB core, we must never
919 report *ADDR_P outside of any ADDR_WATCH..ADDR_WATCH+LEN
920 range. ADDR_WATCH <= ADDR_TRAP < ADDR_ORIG is a false
921 positive on kernels older than 4.10. See PR
931 /* Implement the "stopped_by_watchpoint" target_ops method. */
934 aarch64_linux_nat_target::stopped_by_watchpoint ()
938 return stopped_data_address (&addr
);
941 /* Implement the "watchpoint_addr_within_range" target_ops method. */
944 aarch64_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr
,
945 CORE_ADDR start
, int length
)
947 return start
<= addr
&& start
+ length
- 1 >= addr
;
950 /* Implement the "can_do_single_step" target_ops method. */
953 aarch64_linux_nat_target::can_do_single_step ()
958 /* Implement the "thread_architecture" target_ops method. */
961 aarch64_linux_nat_target::thread_architecture (ptid_t ptid
)
963 /* Return the gdbarch for the current thread. If the vector length has
964 changed since the last time this was called, then do a further lookup. */
966 uint64_t vq
= aarch64_sve_get_vq (ptid
.lwp ());
968 /* Find the current gdbarch the same way as process_stratum_target. Only
969 return it if the current vector length matches the one in the tdep. */
970 inferior
*inf
= find_inferior_ptid (this, ptid
);
971 gdb_assert (inf
!= NULL
);
972 if (vq
== gdbarch_tdep (inf
->gdbarch
)->vq
)
975 /* We reach here if the vector length for the thread is different from its
976 value at process start. Lookup gdbarch via info (potentially creating a
977 new one), stashing the vector length inside id. Use -1 for when SVE
978 unavailable, to distinguish from an unset value of 0. */
979 struct gdbarch_info info
;
980 gdbarch_info_init (&info
);
981 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_aarch64
, bfd_mach_aarch64
);
982 info
.id
= (int *) (vq
== 0 ? -1 : vq
);
983 return gdbarch_find_by_info (info
);
986 /* Define AArch64 maintenance commands. */
989 add_show_debug_regs_command (void)
991 /* A maintenance command to enable printing the internal DRi mirror
993 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance
,
994 &show_debug_regs
, _("\
995 Set whether to show variables that mirror the AArch64 debug registers."), _("\
996 Show whether to show variables that mirror the AArch64 debug registers."), _("\
997 Use \"on\" to enable, \"off\" to disable.\n\
998 If enabled, the debug registers values are shown when GDB inserts\n\
999 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
1000 triggers a breakpoint or watchpoint."),
1003 &maintenance_set_cmdlist
,
1004 &maintenance_show_cmdlist
);
1007 void _initialize_aarch64_linux_nat ();
1009 _initialize_aarch64_linux_nat ()
1011 add_show_debug_regs_command ();
1013 /* Register the target. */
1014 linux_target
= &the_aarch64_linux_nat_target
;
1015 add_inf_child_target (&the_aarch64_linux_nat_target
);