1 /* Native-dependent code for GNU/Linux AArch64.
3 Copyright (C) 2011-2019 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"
34 #include "nat/aarch64-linux.h"
35 #include "nat/aarch64-linux-hw-point.h"
36 #include "nat/aarch64-sve-linux-ptrace.h"
38 #include "elf/external.h"
39 #include "elf/common.h"
41 #include "nat/gdb_ptrace.h"
42 #include <sys/utsname.h>
43 #include <asm/ptrace.h>
46 #include "linux-tdep.h"
48 /* Defines ps_err_e, struct ps_prochandle. */
49 #include "gdb_proc_service.h"
50 #include "arch-utils.h"
53 #define TRAP_HWBKPT 0x0004
56 class aarch64_linux_nat_target final
: public linux_nat_target
59 /* Add our register access methods. */
60 void fetch_registers (struct regcache
*, int) override
;
61 void store_registers (struct regcache
*, int) override
;
63 const struct target_desc
*read_description () override
;
65 /* Add our hardware breakpoint and watchpoint implementation. */
66 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
67 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
68 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
69 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
70 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
71 struct expression
*) override
;
72 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
73 struct expression
*) override
;
74 bool stopped_by_watchpoint () override
;
75 bool stopped_data_address (CORE_ADDR
*) override
;
76 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
78 int can_do_single_step () override
;
80 /* Override the GNU/Linux inferior startup hook. */
81 void post_startup_inferior (ptid_t
) override
;
83 /* Override the GNU/Linux post attach hook. */
84 void post_attach (int pid
) override
;
86 /* These three defer to common nat/ code. */
87 void low_new_thread (struct lwp_info
*lp
) override
88 { aarch64_linux_new_thread (lp
); }
89 void low_delete_thread (struct arch_lwp_info
*lp
) override
90 { aarch64_linux_delete_thread (lp
); }
91 void low_prepare_to_resume (struct lwp_info
*lp
) override
92 { aarch64_linux_prepare_to_resume (lp
); }
94 void low_new_fork (struct lwp_info
*parent
, pid_t child_pid
) override
;
95 void low_forget_process (pid_t pid
) override
;
97 /* Add our siginfo layout converter. */
98 bool low_siginfo_fixup (siginfo_t
*ptrace
, gdb_byte
*inf
, int direction
)
101 struct gdbarch
*thread_architecture (ptid_t
) override
;
104 static aarch64_linux_nat_target the_aarch64_linux_nat_target
;
106 /* Per-process data. We don't bind this to a per-inferior registry
107 because of targets like x86 GNU/Linux that need to keep track of
108 processes that aren't bound to any inferior (e.g., fork children,
111 struct aarch64_process_info
114 struct aarch64_process_info
*next
;
116 /* The process identifier. */
119 /* Copy of aarch64 hardware debug registers. */
120 struct aarch64_debug_reg_state state
;
123 static struct aarch64_process_info
*aarch64_process_list
= NULL
;
125 /* Find process data for process PID. */
127 static struct aarch64_process_info
*
128 aarch64_find_process_pid (pid_t pid
)
130 struct aarch64_process_info
*proc
;
132 for (proc
= aarch64_process_list
; proc
; proc
= proc
->next
)
133 if (proc
->pid
== pid
)
139 /* Add process data for process PID. Returns newly allocated info
142 static struct aarch64_process_info
*
143 aarch64_add_process (pid_t pid
)
145 struct aarch64_process_info
*proc
;
147 proc
= XCNEW (struct aarch64_process_info
);
150 proc
->next
= aarch64_process_list
;
151 aarch64_process_list
= proc
;
156 /* Get data specific info for process PID, creating it if necessary.
157 Never returns NULL. */
159 static struct aarch64_process_info
*
160 aarch64_process_info_get (pid_t pid
)
162 struct aarch64_process_info
*proc
;
164 proc
= aarch64_find_process_pid (pid
);
166 proc
= aarch64_add_process (pid
);
171 /* Called whenever GDB is no longer debugging process PID. It deletes
172 data structures that keep track of debug register state. */
175 aarch64_linux_nat_target::low_forget_process (pid_t pid
)
177 struct aarch64_process_info
*proc
, **proc_link
;
179 proc
= aarch64_process_list
;
180 proc_link
= &aarch64_process_list
;
184 if (proc
->pid
== pid
)
186 *proc_link
= proc
->next
;
192 proc_link
= &proc
->next
;
197 /* Get debug registers state for process PID. */
199 struct aarch64_debug_reg_state
*
200 aarch64_get_debug_reg_state (pid_t pid
)
202 return &aarch64_process_info_get (pid
)->state
;
205 /* Fill GDB's register array with the general-purpose register values
206 from the current thread. */
209 fetch_gregs_from_thread (struct regcache
*regcache
)
212 struct gdbarch
*gdbarch
= regcache
->arch ();
216 /* Make sure REGS can hold all registers contents on both aarch64
218 gdb_static_assert (sizeof (regs
) >= 18 * 4);
220 tid
= regcache
->ptid ().lwp ();
222 iovec
.iov_base
= ®s
;
223 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
224 iovec
.iov_len
= 18 * 4;
226 iovec
.iov_len
= sizeof (regs
);
228 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
230 perror_with_name (_("Unable to fetch general registers."));
232 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
233 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, 1);
238 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
239 regcache
->raw_supply (regno
, ®s
[regno
- AARCH64_X0_REGNUM
]);
243 /* Store to the current thread the valid general-purpose register
244 values in the GDB's register array. */
247 store_gregs_to_thread (const struct regcache
*regcache
)
252 struct gdbarch
*gdbarch
= regcache
->arch ();
254 /* Make sure REGS can hold all registers contents on both aarch64
256 gdb_static_assert (sizeof (regs
) >= 18 * 4);
257 tid
= regcache
->ptid ().lwp ();
259 iovec
.iov_base
= ®s
;
260 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
261 iovec
.iov_len
= 18 * 4;
263 iovec
.iov_len
= sizeof (regs
);
265 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
267 perror_with_name (_("Unable to fetch general registers."));
269 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
270 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, 1);
275 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
276 if (REG_VALID
== regcache
->get_register_status (regno
))
277 regcache
->raw_collect (regno
, ®s
[regno
- AARCH64_X0_REGNUM
]);
280 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
282 perror_with_name (_("Unable to store general registers."));
285 /* Fill GDB's register array with the fp/simd register values
286 from the current thread. */
289 fetch_fpregs_from_thread (struct regcache
*regcache
)
294 struct gdbarch
*gdbarch
= regcache
->arch ();
296 /* Make sure REGS can hold all VFP registers contents on both aarch64
298 gdb_static_assert (sizeof regs
>= ARM_VFP3_REGS_SIZE
);
300 tid
= regcache
->ptid ().lwp ();
302 iovec
.iov_base
= ®s
;
304 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
306 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
308 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
310 perror_with_name (_("Unable to fetch VFP registers."));
312 aarch32_vfp_regcache_supply (regcache
, (gdb_byte
*) ®s
, 32);
318 iovec
.iov_len
= sizeof (regs
);
320 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
322 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
324 for (regno
= AARCH64_V0_REGNUM
; regno
<= AARCH64_V31_REGNUM
; regno
++)
325 regcache
->raw_supply (regno
, ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
327 regcache
->raw_supply (AARCH64_FPSR_REGNUM
, ®s
.fpsr
);
328 regcache
->raw_supply (AARCH64_FPCR_REGNUM
, ®s
.fpcr
);
332 /* Store to the current thread the valid fp/simd register
333 values in the GDB's register array. */
336 store_fpregs_to_thread (const struct regcache
*regcache
)
341 struct gdbarch
*gdbarch
= regcache
->arch ();
343 /* Make sure REGS can hold all VFP registers contents on both aarch64
345 gdb_static_assert (sizeof regs
>= ARM_VFP3_REGS_SIZE
);
346 tid
= regcache
->ptid ().lwp ();
348 iovec
.iov_base
= ®s
;
350 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
352 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
354 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
356 perror_with_name (_("Unable to fetch VFP registers."));
358 aarch32_vfp_regcache_collect (regcache
, (gdb_byte
*) ®s
, 32);
364 iovec
.iov_len
= sizeof (regs
);
366 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
368 perror_with_name (_("Unable to fetch FP/SIMD registers."));
370 for (regno
= AARCH64_V0_REGNUM
; regno
<= AARCH64_V31_REGNUM
; regno
++)
371 if (REG_VALID
== regcache
->get_register_status (regno
))
372 regcache
->raw_collect
373 (regno
, (char *) ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
375 if (REG_VALID
== regcache
->get_register_status (AARCH64_FPSR_REGNUM
))
376 regcache
->raw_collect (AARCH64_FPSR_REGNUM
, (char *) ®s
.fpsr
);
377 if (REG_VALID
== regcache
->get_register_status (AARCH64_FPCR_REGNUM
))
378 regcache
->raw_collect (AARCH64_FPCR_REGNUM
, (char *) ®s
.fpcr
);
381 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
383 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
385 perror_with_name (_("Unable to store VFP registers."));
389 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iovec
);
391 perror_with_name (_("Unable to store FP/SIMD registers."));
395 /* Fill GDB's register array with the sve register values
396 from the current thread. */
399 fetch_sveregs_from_thread (struct regcache
*regcache
)
401 std::unique_ptr
<gdb_byte
[]> base
402 = aarch64_sve_get_sveregs (regcache
->ptid ().lwp ());
403 aarch64_sve_regs_copy_to_reg_buf (regcache
, base
.get ());
406 /* Store to the current thread the valid sve register
407 values in the GDB's register array. */
410 store_sveregs_to_thread (struct regcache
*regcache
)
414 int tid
= regcache
->ptid ().lwp ();
416 /* First store vector length to the thread. This is done first to ensure the
417 ptrace buffers read from the kernel are the correct size. */
418 if (!aarch64_sve_set_vq (tid
, regcache
))
419 perror_with_name (_("Unable to set VG register."));
421 /* Obtain a dump of SVE registers from ptrace. */
422 std::unique_ptr
<gdb_byte
[]> base
= aarch64_sve_get_sveregs (tid
);
424 /* Overwrite with regcache state. */
425 aarch64_sve_regs_copy_from_reg_buf (regcache
, base
.get ());
427 /* Write back to the kernel. */
428 iovec
.iov_base
= base
.get ();
429 iovec
.iov_len
= ((struct user_sve_header
*) base
.get ())->size
;
430 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_SVE
, &iovec
);
433 perror_with_name (_("Unable to store sve registers"));
436 /* Fill GDB's register array with the pointer authentication mask values from
437 the current thread. */
440 fetch_pauth_masks_from_thread (struct regcache
*regcache
)
442 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
445 uint64_t pauth_regset
[2] = {0, 0};
446 int tid
= regcache
->ptid ().lwp ();
448 iovec
.iov_base
= &pauth_regset
;
449 iovec
.iov_len
= sizeof (pauth_regset
);
451 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_PAC_MASK
, &iovec
);
453 perror_with_name (_("unable to fetch pauth registers."));
455 regcache
->raw_supply (AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
),
457 regcache
->raw_supply (AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
),
461 /* Implement the "fetch_registers" target_ops method. */
464 aarch64_linux_nat_target::fetch_registers (struct regcache
*regcache
,
467 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
471 fetch_gregs_from_thread (regcache
);
472 if (tdep
->has_sve ())
473 fetch_sveregs_from_thread (regcache
);
475 fetch_fpregs_from_thread (regcache
);
477 if (tdep
->has_pauth ())
478 fetch_pauth_masks_from_thread (regcache
);
480 else if (regno
< AARCH64_V0_REGNUM
)
481 fetch_gregs_from_thread (regcache
);
482 else if (tdep
->has_sve ())
483 fetch_sveregs_from_thread (regcache
);
485 fetch_fpregs_from_thread (regcache
);
487 if (tdep
->has_pauth ())
489 if (regno
== AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
)
490 || regno
== AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
))
491 fetch_pauth_masks_from_thread (regcache
);
495 /* Implement the "store_registers" target_ops method. */
498 aarch64_linux_nat_target::store_registers (struct regcache
*regcache
,
501 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
505 store_gregs_to_thread (regcache
);
506 if (tdep
->has_sve ())
507 store_sveregs_to_thread (regcache
);
509 store_fpregs_to_thread (regcache
);
511 else if (regno
< AARCH64_V0_REGNUM
)
512 store_gregs_to_thread (regcache
);
513 else if (tdep
->has_sve ())
514 store_sveregs_to_thread (regcache
);
516 store_fpregs_to_thread (regcache
);
519 /* Fill register REGNO (if it is a general-purpose register) in
520 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
521 do this for all registers. */
524 fill_gregset (const struct regcache
*regcache
,
525 gdb_gregset_t
*gregsetp
, int regno
)
527 regcache_collect_regset (&aarch64_linux_gregset
, regcache
,
528 regno
, (gdb_byte
*) gregsetp
,
529 AARCH64_LINUX_SIZEOF_GREGSET
);
532 /* Fill GDB's register array with the general-purpose register values
536 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
538 regcache_supply_regset (&aarch64_linux_gregset
, regcache
, -1,
539 (const gdb_byte
*) gregsetp
,
540 AARCH64_LINUX_SIZEOF_GREGSET
);
543 /* Fill register REGNO (if it is a floating-point register) in
544 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
545 do this for all registers. */
548 fill_fpregset (const struct regcache
*regcache
,
549 gdb_fpregset_t
*fpregsetp
, int regno
)
551 regcache_collect_regset (&aarch64_linux_fpregset
, regcache
,
552 regno
, (gdb_byte
*) fpregsetp
,
553 AARCH64_LINUX_SIZEOF_FPREGSET
);
556 /* Fill GDB's register array with the floating-point register values
560 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
562 regcache_supply_regset (&aarch64_linux_fpregset
, regcache
, -1,
563 (const gdb_byte
*) fpregsetp
,
564 AARCH64_LINUX_SIZEOF_FPREGSET
);
567 /* linux_nat_new_fork hook. */
570 aarch64_linux_nat_target::low_new_fork (struct lwp_info
*parent
,
574 struct aarch64_debug_reg_state
*parent_state
;
575 struct aarch64_debug_reg_state
*child_state
;
577 /* NULL means no watchpoint has ever been set in the parent. In
578 that case, there's nothing to do. */
579 if (parent
->arch_private
== NULL
)
582 /* GDB core assumes the child inherits the watchpoints/hw
583 breakpoints of the parent, and will remove them all from the
584 forked off process. Copy the debug registers mirrors into the
585 new process so that all breakpoints and watchpoints can be
588 parent_pid
= parent
->ptid
.pid ();
589 parent_state
= aarch64_get_debug_reg_state (parent_pid
);
590 child_state
= aarch64_get_debug_reg_state (child_pid
);
591 *child_state
= *parent_state
;
595 /* Called by libthread_db. Returns a pointer to the thread local
596 storage (or its descriptor). */
599 ps_get_thread_area (struct ps_prochandle
*ph
,
600 lwpid_t lwpid
, int idx
, void **base
)
603 = (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word
== 64);
605 return aarch64_ps_get_thread_area (ph
, lwpid
, idx
, base
, is_64bit_p
);
609 /* Implement the "post_startup_inferior" target_ops method. */
612 aarch64_linux_nat_target::post_startup_inferior (ptid_t ptid
)
614 low_forget_process (ptid
.pid ());
615 aarch64_linux_get_debug_reg_capacity (ptid
.pid ());
616 linux_nat_target::post_startup_inferior (ptid
);
619 /* Implement the "post_attach" target_ops method. */
622 aarch64_linux_nat_target::post_attach (int pid
)
624 low_forget_process (pid
);
625 /* Set the hardware debug register capacity. If
626 aarch64_linux_get_debug_reg_capacity is not called
627 (as it is in aarch64_linux_child_post_startup_inferior) then
628 software watchpoints will be used instead of hardware
629 watchpoints when attaching to a target. */
630 aarch64_linux_get_debug_reg_capacity (pid
);
631 linux_nat_target::post_attach (pid
);
634 extern struct target_desc
*tdesc_arm_with_neon
;
636 /* Implement the "read_description" target_ops method. */
638 const struct target_desc
*
639 aarch64_linux_nat_target::read_description ()
642 gdb_byte regbuf
[ARM_VFP3_REGS_SIZE
];
645 tid
= inferior_ptid
.lwp ();
647 iovec
.iov_base
= regbuf
;
648 iovec
.iov_len
= ARM_VFP3_REGS_SIZE
;
650 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
652 return tdesc_arm_with_neon
;
654 CORE_ADDR hwcap
= linux_get_hwcap (this);
656 return aarch64_read_description (aarch64_sve_get_vq (tid
),
657 hwcap
& AARCH64_HWCAP_PACA
);
660 /* Convert a native/host siginfo object, into/from the siginfo in the
661 layout of the inferiors' architecture. Returns true if any
662 conversion was done; false otherwise. If DIRECTION is 1, then copy
663 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
667 aarch64_linux_nat_target::low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
670 struct gdbarch
*gdbarch
= get_frame_arch (get_current_frame ());
672 /* Is the inferior 32-bit? If so, then do fixup the siginfo
674 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
677 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo
*) inf
,
680 aarch64_siginfo_from_compat_siginfo (native
,
681 (struct compat_siginfo
*) inf
);
689 /* Returns the number of hardware watchpoints of type TYPE that we can
690 set. Value is positive if we can set CNT watchpoints, zero if
691 setting watchpoints of type TYPE is not supported, and negative if
692 CNT is more than the maximum number of watchpoints of type TYPE
693 that we can support. TYPE is one of bp_hardware_watchpoint,
694 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
695 CNT is the number of such watchpoints used so far (including this
696 one). OTHERTYPE is non-zero if other types of watchpoints are
697 currently enabled. */
700 aarch64_linux_nat_target::can_use_hw_breakpoint (enum bptype type
,
701 int cnt
, int othertype
)
703 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
704 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
706 if (aarch64_num_wp_regs
== 0)
709 else if (type
== bp_hardware_breakpoint
)
711 if (aarch64_num_bp_regs
== 0)
715 gdb_assert_not_reached ("unexpected breakpoint type");
717 /* We always return 1 here because we don't have enough information
718 about possible overlap of addresses that they want to watch. As an
719 extreme example, consider the case where all the watchpoints watch
720 the same address and the same region length: then we can handle a
721 virtually unlimited number of watchpoints, due to debug register
722 sharing implemented via reference counts. */
726 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
727 Return 0 on success, -1 on failure. */
730 aarch64_linux_nat_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
731 struct bp_target_info
*bp_tgt
)
734 CORE_ADDR addr
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
736 const enum target_hw_bp_type type
= hw_execute
;
737 struct aarch64_debug_reg_state
*state
738 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
740 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &len
);
745 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
746 (unsigned long) addr
, len
);
748 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 1 /* is_insert */, state
);
752 aarch64_show_debug_reg_state (state
,
753 "insert_hw_breakpoint", addr
, len
, type
);
759 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
760 Return 0 on success, -1 on failure. */
763 aarch64_linux_nat_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
764 struct bp_target_info
*bp_tgt
)
767 CORE_ADDR addr
= bp_tgt
->placed_address
;
769 const enum target_hw_bp_type type
= hw_execute
;
770 struct aarch64_debug_reg_state
*state
771 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
773 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &len
);
777 (gdb_stdlog
, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
778 (unsigned long) addr
, len
);
780 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 0 /* is_insert */, state
);
784 aarch64_show_debug_reg_state (state
,
785 "remove_hw_watchpoint", addr
, len
, type
);
791 /* Implement the "insert_watchpoint" target_ops method.
793 Insert a watchpoint to watch a memory region which starts at
794 address ADDR and whose length is LEN bytes. Watch memory accesses
795 of the type TYPE. Return 0 on success, -1 on failure. */
798 aarch64_linux_nat_target::insert_watchpoint (CORE_ADDR addr
, int len
,
799 enum target_hw_bp_type type
,
800 struct expression
*cond
)
803 struct aarch64_debug_reg_state
*state
804 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
807 fprintf_unfiltered (gdb_stdlog
,
808 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
809 (unsigned long) addr
, len
);
811 gdb_assert (type
!= hw_execute
);
813 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 1 /* is_insert */, state
);
817 aarch64_show_debug_reg_state (state
,
818 "insert_watchpoint", addr
, len
, type
);
824 /* Implement the "remove_watchpoint" target_ops method.
825 Remove a watchpoint that watched the memory region which starts at
826 address ADDR, whose length is LEN bytes, and for accesses of the
827 type TYPE. Return 0 on success, -1 on failure. */
830 aarch64_linux_nat_target::remove_watchpoint (CORE_ADDR addr
, int len
,
831 enum target_hw_bp_type type
,
832 struct expression
*cond
)
835 struct aarch64_debug_reg_state
*state
836 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
839 fprintf_unfiltered (gdb_stdlog
,
840 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
841 (unsigned long) addr
, len
);
843 gdb_assert (type
!= hw_execute
);
845 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 0 /* is_insert */, state
);
849 aarch64_show_debug_reg_state (state
,
850 "remove_watchpoint", addr
, len
, type
);
856 /* Implement the "region_ok_for_hw_watchpoint" target_ops method. */
859 aarch64_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
861 return aarch64_linux_region_ok_for_watchpoint (addr
, len
);
864 /* Implement the "stopped_data_address" target_ops method. */
867 aarch64_linux_nat_target::stopped_data_address (CORE_ADDR
*addr_p
)
871 struct aarch64_debug_reg_state
*state
;
873 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
876 /* This must be a hardware breakpoint. */
877 if (siginfo
.si_signo
!= SIGTRAP
878 || (siginfo
.si_code
& 0xffff) != TRAP_HWBKPT
)
881 /* Check if the address matches any watched address. */
882 state
= aarch64_get_debug_reg_state (inferior_ptid
.pid ());
883 for (i
= aarch64_num_wp_regs
- 1; i
>= 0; --i
)
885 const unsigned int offset
886 = aarch64_watchpoint_offset (state
->dr_ctrl_wp
[i
]);
887 const unsigned int len
= aarch64_watchpoint_length (state
->dr_ctrl_wp
[i
]);
888 const CORE_ADDR addr_trap
= (CORE_ADDR
) siginfo
.si_addr
;
889 const CORE_ADDR addr_watch
= state
->dr_addr_wp
[i
] + offset
;
890 const CORE_ADDR addr_watch_aligned
= align_down (state
->dr_addr_wp
[i
], 8);
891 const CORE_ADDR addr_orig
= state
->dr_addr_orig_wp
[i
];
893 if (state
->dr_ref_count_wp
[i
]
894 && DR_CONTROL_ENABLED (state
->dr_ctrl_wp
[i
])
895 && addr_trap
>= addr_watch_aligned
896 && addr_trap
< addr_watch
+ len
)
898 /* ADDR_TRAP reports the first address of the memory range
899 accessed by the CPU, regardless of what was the memory
900 range watched. Thus, a large CPU access that straddles
901 the ADDR_WATCH..ADDR_WATCH+LEN range may result in an
902 ADDR_TRAP that is lower than the
903 ADDR_WATCH..ADDR_WATCH+LEN range. E.g.:
905 addr: | 4 | 5 | 6 | 7 | 8 |
906 |---- range watched ----|
907 |----------- range accessed ------------|
909 In this case, ADDR_TRAP will be 4.
911 To match a watchpoint known to GDB core, we must never
912 report *ADDR_P outside of any ADDR_WATCH..ADDR_WATCH+LEN
913 range. ADDR_WATCH <= ADDR_TRAP < ADDR_ORIG is a false
914 positive on kernels older than 4.10. See PR
924 /* Implement the "stopped_by_watchpoint" target_ops method. */
927 aarch64_linux_nat_target::stopped_by_watchpoint ()
931 return stopped_data_address (&addr
);
934 /* Implement the "watchpoint_addr_within_range" target_ops method. */
937 aarch64_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr
,
938 CORE_ADDR start
, int length
)
940 return start
<= addr
&& start
+ length
- 1 >= addr
;
943 /* Implement the "can_do_single_step" target_ops method. */
946 aarch64_linux_nat_target::can_do_single_step ()
951 /* Implement the "thread_architecture" target_ops method. */
954 aarch64_linux_nat_target::thread_architecture (ptid_t ptid
)
956 /* Return the gdbarch for the current thread. If the vector length has
957 changed since the last time this was called, then do a further lookup. */
959 uint64_t vq
= aarch64_sve_get_vq (ptid
.lwp ());
961 /* Find the current gdbarch the same way as process_stratum_target. Only
962 return it if the current vector length matches the one in the tdep. */
963 inferior
*inf
= find_inferior_ptid (ptid
);
964 gdb_assert (inf
!= NULL
);
965 if (vq
== gdbarch_tdep (inf
->gdbarch
)->vq
)
968 /* We reach here if the vector length for the thread is different from its
969 value at process start. Lookup gdbarch via info (potentially creating a
970 new one), stashing the vector length inside id. Use -1 for when SVE
971 unavailable, to distinguish from an unset value of 0. */
972 struct gdbarch_info info
;
973 gdbarch_info_init (&info
);
974 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_spu
, bfd_mach_spu
);
975 info
.id
= (int *) (vq
== 0 ? -1 : vq
);
976 return gdbarch_find_by_info (info
);
979 /* Define AArch64 maintenance commands. */
982 add_show_debug_regs_command (void)
984 /* A maintenance command to enable printing the internal DRi mirror
986 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance
,
987 &show_debug_regs
, _("\
988 Set whether to show variables that mirror the AArch64 debug registers."), _("\
989 Show whether to show variables that mirror the AArch64 debug registers."), _("\
990 Use \"on\" to enable, \"off\" to disable.\n\
991 If enabled, the debug registers values are shown when GDB inserts\n\
992 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
993 triggers a breakpoint or watchpoint."),
996 &maintenance_set_cmdlist
,
997 &maintenance_show_cmdlist
);
1001 _initialize_aarch64_linux_nat (void)
1003 add_show_debug_regs_command ();
1005 /* Register the target. */
1006 linux_target
= &the_aarch64_linux_nat_target
;
1007 add_inf_child_target (&the_aarch64_linux_nat_target
);