1 /* Native-dependent code for GNU/Linux AArch64.
3 Copyright (C) 2011-2015 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 "nat/aarch64-linux-hw-point.h"
35 #include "elf/external.h"
36 #include "elf/common.h"
38 #include "nat/gdb_ptrace.h"
39 #include <sys/utsname.h>
40 #include <asm/ptrace.h>
44 /* Defines ps_err_e, struct ps_prochandle. */
45 #include "gdb_proc_service.h"
48 #define TRAP_HWBKPT 0x0004
51 /* Per-process data. We don't bind this to a per-inferior registry
52 because of targets like x86 GNU/Linux that need to keep track of
53 processes that aren't bound to any inferior (e.g., fork children,
56 struct aarch64_process_info
59 struct aarch64_process_info
*next
;
61 /* The process identifier. */
64 /* Copy of aarch64 hardware debug registers. */
65 struct aarch64_debug_reg_state state
;
68 static struct aarch64_process_info
*aarch64_process_list
= NULL
;
70 /* Find process data for process PID. */
72 static struct aarch64_process_info
*
73 aarch64_find_process_pid (pid_t pid
)
75 struct aarch64_process_info
*proc
;
77 for (proc
= aarch64_process_list
; proc
; proc
= proc
->next
)
84 /* Add process data for process PID. Returns newly allocated info
87 static struct aarch64_process_info
*
88 aarch64_add_process (pid_t pid
)
90 struct aarch64_process_info
*proc
;
92 proc
= xcalloc (1, sizeof (*proc
));
95 proc
->next
= aarch64_process_list
;
96 aarch64_process_list
= proc
;
101 /* Get data specific info for process PID, creating it if necessary.
102 Never returns NULL. */
104 static struct aarch64_process_info
*
105 aarch64_process_info_get (pid_t pid
)
107 struct aarch64_process_info
*proc
;
109 proc
= aarch64_find_process_pid (pid
);
111 proc
= aarch64_add_process (pid
);
116 /* Called whenever GDB is no longer debugging process PID. It deletes
117 data structures that keep track of debug register state. */
120 aarch64_forget_process (pid_t pid
)
122 struct aarch64_process_info
*proc
, **proc_link
;
124 proc
= aarch64_process_list
;
125 proc_link
= &aarch64_process_list
;
129 if (proc
->pid
== pid
)
131 *proc_link
= proc
->next
;
137 proc_link
= &proc
->next
;
142 /* Get debug registers state for process PID. */
144 static struct aarch64_debug_reg_state
*
145 aarch64_get_debug_reg_state (pid_t pid
)
147 return &aarch64_process_info_get (pid
)->state
;
150 struct aarch64_dr_update_callback_param
156 /* Callback for iterate_over_lwps. Records the
157 information about the change of one hardware breakpoint/watchpoint
158 setting for the thread LWP.
159 The information is passed in via PTR.
160 N.B. The actual updating of hardware debug registers is not
161 carried out until the moment the thread is resumed. */
164 debug_reg_change_callback (struct lwp_info
*lwp
, void *ptr
)
166 struct aarch64_dr_update_callback_param
*param_p
167 = (struct aarch64_dr_update_callback_param
*) ptr
;
168 int pid
= ptid_get_lwp (lwp
->ptid
);
169 int idx
= param_p
->idx
;
170 int is_watchpoint
= param_p
->is_watchpoint
;
171 struct arch_lwp_info
*info
= lwp
->arch_private
;
172 dr_changed_t
*dr_changed_ptr
;
173 dr_changed_t dr_changed
;
176 info
= lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
180 fprintf_unfiltered (gdb_stdlog
,
181 "debug_reg_change_callback: \n\tOn entry:\n");
182 fprintf_unfiltered (gdb_stdlog
,
183 "\tpid%d, dr_changed_bp=0x%s, "
184 "dr_changed_wp=0x%s\n",
185 pid
, phex (info
->dr_changed_bp
, 8),
186 phex (info
->dr_changed_wp
, 8));
189 dr_changed_ptr
= is_watchpoint
? &info
->dr_changed_wp
190 : &info
->dr_changed_bp
;
191 dr_changed
= *dr_changed_ptr
;
194 && (idx
<= (is_watchpoint
? aarch64_num_wp_regs
195 : aarch64_num_bp_regs
)));
197 /* The actual update is done later just before resuming the lwp,
198 we just mark that one register pair needs updating. */
199 DR_MARK_N_CHANGED (dr_changed
, idx
);
200 *dr_changed_ptr
= dr_changed
;
202 /* If the lwp isn't stopped, force it to momentarily pause, so
203 we can update its debug registers. */
205 linux_stop_lwp (lwp
);
209 fprintf_unfiltered (gdb_stdlog
,
210 "\tOn exit:\n\tpid%d, dr_changed_bp=0x%s, "
211 "dr_changed_wp=0x%s\n",
212 pid
, phex (info
->dr_changed_bp
, 8),
213 phex (info
->dr_changed_wp
, 8));
216 /* Continue the iteration. */
220 /* Notify each thread that their IDXth breakpoint/watchpoint register
221 pair needs to be updated. The message will be recorded in each
222 thread's arch-specific data area, the actual updating will be done
223 when the thread is resumed. */
226 aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state
*state
,
227 int is_watchpoint
, unsigned int idx
)
229 struct aarch64_dr_update_callback_param param
;
230 ptid_t pid_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
232 param
.is_watchpoint
= is_watchpoint
;
235 iterate_over_lwps (pid_ptid
, debug_reg_change_callback
, (void *) ¶m
);
238 /* Fill GDB's register array with the general-purpose register values
239 from the current thread. */
242 fetch_gregs_from_thread (struct regcache
*regcache
)
245 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
249 /* Make sure REGS can hold all registers contents on both aarch64
251 gdb_static_assert (sizeof (regs
) >= 18 * 4);
253 tid
= ptid_get_lwp (inferior_ptid
);
255 iovec
.iov_base
= ®s
;
256 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
257 iovec
.iov_len
= 18 * 4;
259 iovec
.iov_len
= sizeof (regs
);
261 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
263 perror_with_name (_("Unable to fetch general registers."));
265 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
266 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, 1);
271 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
272 regcache_raw_supply (regcache
, regno
, ®s
[regno
- AARCH64_X0_REGNUM
]);
276 /* Store to the current thread the valid general-purpose register
277 values in the GDB's register array. */
280 store_gregs_to_thread (const struct regcache
*regcache
)
285 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
287 /* Make sure REGS can hold all registers contents on both aarch64
289 gdb_static_assert (sizeof (regs
) >= 18 * 4);
290 tid
= ptid_get_lwp (inferior_ptid
);
292 iovec
.iov_base
= ®s
;
293 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
294 iovec
.iov_len
= 18 * 4;
296 iovec
.iov_len
= sizeof (regs
);
298 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
300 perror_with_name (_("Unable to fetch general registers."));
302 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
303 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, 1);
308 for (regno
= AARCH64_X0_REGNUM
; regno
<= AARCH64_CPSR_REGNUM
; regno
++)
309 if (REG_VALID
== regcache_register_status (regcache
, regno
))
310 regcache_raw_collect (regcache
, regno
,
311 ®s
[regno
- AARCH64_X0_REGNUM
]);
314 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iovec
);
316 perror_with_name (_("Unable to store general registers."));
319 /* Fill GDB's register array with the fp/simd register values
320 from the current thread. */
323 fetch_fpregs_from_thread (struct regcache
*regcache
)
328 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
330 /* Make sure REGS can hold all VFP registers contents on both aarch64
332 gdb_static_assert (sizeof regs
>= VFP_REGS_SIZE
);
334 tid
= ptid_get_lwp (inferior_ptid
);
336 iovec
.iov_base
= ®s
;
338 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
340 iovec
.iov_len
= VFP_REGS_SIZE
;
342 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
344 perror_with_name (_("Unable to fetch VFP registers."));
346 aarch32_vfp_regcache_supply (regcache
, (gdb_byte
*) ®s
, 32);
352 iovec
.iov_len
= sizeof (regs
);
354 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
356 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
358 for (regno
= AARCH64_V0_REGNUM
; regno
<= AARCH64_V31_REGNUM
; regno
++)
359 regcache_raw_supply (regcache
, regno
,
360 ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
362 regcache_raw_supply (regcache
, AARCH64_FPSR_REGNUM
, ®s
.fpsr
);
363 regcache_raw_supply (regcache
, AARCH64_FPCR_REGNUM
, ®s
.fpcr
);
367 /* Store to the current thread the valid fp/simd register
368 values in the GDB's register array. */
371 store_fpregs_to_thread (const struct regcache
*regcache
)
376 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
378 /* Make sure REGS can hold all VFP registers contents on both aarch64
380 gdb_static_assert (sizeof regs
>= VFP_REGS_SIZE
);
381 tid
= ptid_get_lwp (inferior_ptid
);
383 iovec
.iov_base
= ®s
;
385 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
387 iovec
.iov_len
= VFP_REGS_SIZE
;
389 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
391 perror_with_name (_("Unable to fetch VFP registers."));
393 aarch32_vfp_regcache_collect (regcache
, (gdb_byte
*) ®s
, 32);
399 iovec
.iov_len
= sizeof (regs
);
401 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iovec
);
403 perror_with_name (_("Unable to fetch FP/SIMD registers."));
405 for (regno
= AARCH64_V0_REGNUM
; regno
<= AARCH64_V31_REGNUM
; regno
++)
406 if (REG_VALID
== regcache_register_status (regcache
, regno
))
407 regcache_raw_collect (regcache
, regno
,
408 (char *) ®s
.vregs
[regno
- AARCH64_V0_REGNUM
]);
410 if (REG_VALID
== regcache_register_status (regcache
, AARCH64_FPSR_REGNUM
))
411 regcache_raw_collect (regcache
, AARCH64_FPSR_REGNUM
,
412 (char *) ®s
.fpsr
);
413 if (REG_VALID
== regcache_register_status (regcache
, AARCH64_FPCR_REGNUM
))
414 regcache_raw_collect (regcache
, AARCH64_FPCR_REGNUM
,
415 (char *) ®s
.fpcr
);
418 if (gdbarch_bfd_arch_info (gdbarch
)->bits_per_word
== 32)
420 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iovec
);
422 perror_with_name (_("Unable to store VFP registers."));
426 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iovec
);
428 perror_with_name (_("Unable to store FP/SIMD registers."));
432 /* Implement the "to_fetch_register" target_ops method. */
435 aarch64_linux_fetch_inferior_registers (struct target_ops
*ops
,
436 struct regcache
*regcache
,
441 fetch_gregs_from_thread (regcache
);
442 fetch_fpregs_from_thread (regcache
);
444 else if (regno
< AARCH64_V0_REGNUM
)
445 fetch_gregs_from_thread (regcache
);
447 fetch_fpregs_from_thread (regcache
);
450 /* Implement the "to_store_register" target_ops method. */
453 aarch64_linux_store_inferior_registers (struct target_ops
*ops
,
454 struct regcache
*regcache
,
459 store_gregs_to_thread (regcache
);
460 store_fpregs_to_thread (regcache
);
462 else if (regno
< AARCH64_V0_REGNUM
)
463 store_gregs_to_thread (regcache
);
465 store_fpregs_to_thread (regcache
);
468 /* Fill register REGNO (if it is a general-purpose register) in
469 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
470 do this for all registers. */
473 fill_gregset (const struct regcache
*regcache
,
474 gdb_gregset_t
*gregsetp
, int regno
)
476 regcache_collect_regset (&aarch64_linux_gregset
, regcache
,
477 regno
, (gdb_byte
*) gregsetp
,
478 AARCH64_LINUX_SIZEOF_GREGSET
);
481 /* Fill GDB's register array with the general-purpose register values
485 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
487 regcache_supply_regset (&aarch64_linux_gregset
, regcache
, -1,
488 (const gdb_byte
*) gregsetp
,
489 AARCH64_LINUX_SIZEOF_GREGSET
);
492 /* Fill register REGNO (if it is a floating-point register) in
493 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
494 do this for all registers. */
497 fill_fpregset (const struct regcache
*regcache
,
498 gdb_fpregset_t
*fpregsetp
, int regno
)
500 regcache_collect_regset (&aarch64_linux_fpregset
, regcache
,
501 regno
, (gdb_byte
*) fpregsetp
,
502 AARCH64_LINUX_SIZEOF_FPREGSET
);
505 /* Fill GDB's register array with the floating-point register values
509 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
511 regcache_supply_regset (&aarch64_linux_fpregset
, regcache
, -1,
512 (const gdb_byte
*) fpregsetp
,
513 AARCH64_LINUX_SIZEOF_FPREGSET
);
516 /* Called when resuming a thread.
517 The hardware debug registers are updated when there is any change. */
520 aarch64_linux_prepare_to_resume (struct lwp_info
*lwp
)
522 struct arch_lwp_info
*info
= lwp
->arch_private
;
524 /* NULL means this is the main thread still going through the shell,
525 or, no watchpoint has been set yet. In that case, there's
530 if (DR_HAS_CHANGED (info
->dr_changed_bp
)
531 || DR_HAS_CHANGED (info
->dr_changed_wp
))
533 int tid
= ptid_get_lwp (lwp
->ptid
);
534 struct aarch64_debug_reg_state
*state
535 = aarch64_get_debug_reg_state (ptid_get_pid (lwp
->ptid
));
538 fprintf_unfiltered (gdb_stdlog
, "prepare_to_resume thread %d\n", tid
);
541 if (DR_HAS_CHANGED (info
->dr_changed_wp
))
543 aarch64_linux_set_debug_regs (state
, tid
, 1);
544 DR_CLEAR_CHANGED (info
->dr_changed_wp
);
548 if (DR_HAS_CHANGED (info
->dr_changed_bp
))
550 aarch64_linux_set_debug_regs (state
, tid
, 0);
551 DR_CLEAR_CHANGED (info
->dr_changed_bp
);
557 aarch64_linux_new_thread (struct lwp_info
*lp
)
559 struct arch_lwp_info
*info
= XCNEW (struct arch_lwp_info
);
561 /* Mark that all the hardware breakpoint/watchpoint register pairs
562 for this thread need to be initialized. */
563 DR_MARK_ALL_CHANGED (info
->dr_changed_bp
, aarch64_num_bp_regs
);
564 DR_MARK_ALL_CHANGED (info
->dr_changed_wp
, aarch64_num_wp_regs
);
566 lp
->arch_private
= info
;
569 /* linux_nat_new_fork hook. */
572 aarch64_linux_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
575 struct aarch64_debug_reg_state
*parent_state
;
576 struct aarch64_debug_reg_state
*child_state
;
578 /* NULL means no watchpoint has ever been set in the parent. In
579 that case, there's nothing to do. */
580 if (parent
->arch_private
== NULL
)
583 /* GDB core assumes the child inherits the watchpoints/hw
584 breakpoints of the parent, and will remove them all from the
585 forked off process. Copy the debug registers mirrors into the
586 new process so that all breakpoints and watchpoints can be
589 parent_pid
= ptid_get_pid (parent
->ptid
);
590 parent_state
= aarch64_get_debug_reg_state (parent_pid
);
591 child_state
= aarch64_get_debug_reg_state (child_pid
);
592 *child_state
= *parent_state
;
596 /* Called by libthread_db. Returns a pointer to the thread local
597 storage (or its descriptor). */
600 ps_get_thread_area (const struct ps_prochandle
*ph
,
601 lwpid_t lwpid
, int idx
, void **base
)
606 iovec
.iov_base
= ®
;
607 iovec
.iov_len
= sizeof (reg
);
609 if (ptrace (PTRACE_GETREGSET
, lwpid
, NT_ARM_TLS
, &iovec
) != 0)
612 /* IDX is the bias from the thread pointer to the beginning of the
613 thread descriptor. It has to be subtracted due to implementation
614 quirks in libthread_db. */
615 *base
= (void *) (reg
- idx
);
621 static void (*super_post_startup_inferior
) (struct target_ops
*self
,
624 /* Implement the "to_post_startup_inferior" target_ops method. */
627 aarch64_linux_child_post_startup_inferior (struct target_ops
*self
,
630 aarch64_forget_process (ptid_get_pid (ptid
));
631 aarch64_linux_get_debug_reg_capacity (ptid_get_pid (ptid
));
632 super_post_startup_inferior (self
, ptid
);
635 extern struct target_desc
*tdesc_arm_with_vfpv3
;
636 extern struct target_desc
*tdesc_arm_with_neon
;
638 /* Implement the "to_read_description" target_ops method. */
640 static const struct target_desc
*
641 aarch64_linux_read_description (struct target_ops
*ops
)
645 if (target_auxv_search (ops
, AT_PHENT
, &at_phent
) == 1)
647 if (at_phent
== sizeof (Elf64_External_Phdr
))
648 return tdesc_aarch64
;
651 CORE_ADDR arm_hwcap
= 0;
653 if (target_auxv_search (ops
, AT_HWCAP
, &arm_hwcap
) != 1)
654 return ops
->beneath
->to_read_description (ops
->beneath
);
656 #ifndef COMPAT_HWCAP_VFP
657 #define COMPAT_HWCAP_VFP (1 << 6)
659 #ifndef COMPAT_HWCAP_NEON
660 #define COMPAT_HWCAP_NEON (1 << 12)
662 #ifndef COMPAT_HWCAP_VFPv3
663 #define COMPAT_HWCAP_VFPv3 (1 << 13)
666 if (arm_hwcap
& COMPAT_HWCAP_VFP
)
669 const struct target_desc
*result
= NULL
;
671 if (arm_hwcap
& COMPAT_HWCAP_NEON
)
672 result
= tdesc_arm_with_neon
;
673 else if (arm_hwcap
& COMPAT_HWCAP_VFPv3
)
674 result
= tdesc_arm_with_vfpv3
;
683 return tdesc_aarch64
;
686 /* Returns the number of hardware watchpoints of type TYPE that we can
687 set. Value is positive if we can set CNT watchpoints, zero if
688 setting watchpoints of type TYPE is not supported, and negative if
689 CNT is more than the maximum number of watchpoints of type TYPE
690 that we can support. TYPE is one of bp_hardware_watchpoint,
691 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
692 CNT is the number of such watchpoints used so far (including this
693 one). OTHERTYPE is non-zero if other types of watchpoints are
694 currently enabled. */
697 aarch64_linux_can_use_hw_breakpoint (struct target_ops
*self
,
699 int cnt
, int othertype
)
701 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
702 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
704 if (aarch64_num_wp_regs
== 0)
707 else if (type
== bp_hardware_breakpoint
)
709 if (aarch64_num_bp_regs
== 0)
713 gdb_assert_not_reached ("unexpected breakpoint type");
715 /* We always return 1 here because we don't have enough information
716 about possible overlap of addresses that they want to watch. As an
717 extreme example, consider the case where all the watchpoints watch
718 the same address and the same region length: then we can handle a
719 virtually unlimited number of watchpoints, due to debug register
720 sharing implemented via reference counts. */
724 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
725 Return 0 on success, -1 on failure. */
728 aarch64_linux_insert_hw_breakpoint (struct target_ops
*self
,
729 struct gdbarch
*gdbarch
,
730 struct bp_target_info
*bp_tgt
)
733 CORE_ADDR addr
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
735 const enum target_hw_bp_type type
= hw_execute
;
736 struct aarch64_debug_reg_state
*state
737 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid
));
742 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
743 (unsigned long) addr
, len
);
745 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 1 /* is_insert */, state
);
749 aarch64_show_debug_reg_state (state
,
750 "insert_hw_breakpoint", addr
, len
, type
);
756 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
757 Return 0 on success, -1 on failure. */
760 aarch64_linux_remove_hw_breakpoint (struct target_ops
*self
,
761 struct gdbarch
*gdbarch
,
762 struct bp_target_info
*bp_tgt
)
765 CORE_ADDR addr
= bp_tgt
->placed_address
;
767 const enum target_hw_bp_type type
= hw_execute
;
768 struct aarch64_debug_reg_state
*state
769 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid
));
773 (gdb_stdlog
, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
774 (unsigned long) addr
, len
);
776 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 0 /* is_insert */, state
);
780 aarch64_show_debug_reg_state (state
,
781 "remove_hw_watchpoint", addr
, len
, type
);
787 /* Implement the "to_insert_watchpoint" target_ops method.
789 Insert a watchpoint to watch a memory region which starts at
790 address ADDR and whose length is LEN bytes. Watch memory accesses
791 of the type TYPE. Return 0 on success, -1 on failure. */
794 aarch64_linux_insert_watchpoint (struct target_ops
*self
,
795 CORE_ADDR addr
, int len
,
796 enum target_hw_bp_type type
,
797 struct expression
*cond
)
800 struct aarch64_debug_reg_state
*state
801 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid
));
804 fprintf_unfiltered (gdb_stdlog
,
805 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
806 (unsigned long) addr
, len
);
808 gdb_assert (type
!= hw_execute
);
810 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 1 /* is_insert */, state
);
814 aarch64_show_debug_reg_state (state
,
815 "insert_watchpoint", addr
, len
, type
);
821 /* Implement the "to_remove_watchpoint" target_ops method.
822 Remove a watchpoint that watched the memory region which starts at
823 address ADDR, whose length is LEN bytes, and for accesses of the
824 type TYPE. Return 0 on success, -1 on failure. */
827 aarch64_linux_remove_watchpoint (struct target_ops
*self
,
828 CORE_ADDR addr
, int len
,
829 enum target_hw_bp_type type
,
830 struct expression
*cond
)
833 struct aarch64_debug_reg_state
*state
834 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid
));
837 fprintf_unfiltered (gdb_stdlog
,
838 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
839 (unsigned long) addr
, len
);
841 gdb_assert (type
!= hw_execute
);
843 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 0 /* is_insert */, state
);
847 aarch64_show_debug_reg_state (state
,
848 "remove_watchpoint", addr
, len
, type
);
854 /* Implement the "to_region_ok_for_hw_watchpoint" target_ops method. */
857 aarch64_linux_region_ok_for_hw_watchpoint (struct target_ops
*self
,
858 CORE_ADDR addr
, int len
)
860 CORE_ADDR aligned_addr
;
862 /* Can not set watchpoints for zero or negative lengths. */
866 /* Must have hardware watchpoint debug register(s). */
867 if (aarch64_num_wp_regs
== 0)
870 /* We support unaligned watchpoint address and arbitrary length,
871 as long as the size of the whole watched area after alignment
872 doesn't exceed size of the total area that all watchpoint debug
873 registers can watch cooperatively.
875 This is a very relaxed rule, but unfortunately there are
876 limitations, e.g. false-positive hits, due to limited support of
877 hardware debug registers in the kernel. See comment above
878 aarch64_align_watchpoint for more information. */
880 aligned_addr
= addr
& ~(AARCH64_HWP_MAX_LEN_PER_REG
- 1);
881 if (aligned_addr
+ aarch64_num_wp_regs
* AARCH64_HWP_MAX_LEN_PER_REG
885 /* All tests passed so we are likely to be able to set the watchpoint.
886 The reason that it is 'likely' rather than 'must' is because
887 we don't check the current usage of the watchpoint registers, and
888 there may not be enough registers available for this watchpoint.
889 Ideally we should check the cached debug register state, however
890 the checking is costly. */
894 /* Implement the "to_stopped_data_address" target_ops method. */
897 aarch64_linux_stopped_data_address (struct target_ops
*target
,
902 struct aarch64_debug_reg_state
*state
;
904 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
907 /* This must be a hardware breakpoint. */
908 if (siginfo
.si_signo
!= SIGTRAP
909 || (siginfo
.si_code
& 0xffff) != TRAP_HWBKPT
)
912 /* Check if the address matches any watched address. */
913 state
= aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid
));
914 for (i
= aarch64_num_wp_regs
- 1; i
>= 0; --i
)
916 const unsigned int len
= aarch64_watchpoint_length (state
->dr_ctrl_wp
[i
]);
917 const CORE_ADDR addr_trap
= (CORE_ADDR
) siginfo
.si_addr
;
918 const CORE_ADDR addr_watch
= state
->dr_addr_wp
[i
];
920 if (state
->dr_ref_count_wp
[i
]
921 && DR_CONTROL_ENABLED (state
->dr_ctrl_wp
[i
])
922 && addr_trap
>= addr_watch
923 && addr_trap
< addr_watch
+ len
)
933 /* Implement the "to_stopped_by_watchpoint" target_ops method. */
936 aarch64_linux_stopped_by_watchpoint (struct target_ops
*ops
)
940 return aarch64_linux_stopped_data_address (ops
, &addr
);
943 /* Implement the "to_watchpoint_addr_within_range" target_ops method. */
946 aarch64_linux_watchpoint_addr_within_range (struct target_ops
*target
,
948 CORE_ADDR start
, int length
)
950 return start
<= addr
&& start
+ length
- 1 >= addr
;
953 /* Define AArch64 maintenance commands. */
956 add_show_debug_regs_command (void)
958 /* A maintenance command to enable printing the internal DRi mirror
960 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance
,
961 &show_debug_regs
, _("\
962 Set whether to show variables that mirror the AArch64 debug registers."), _("\
963 Show whether to show variables that mirror the AArch64 debug registers."), _("\
964 Use \"on\" to enable, \"off\" to disable.\n\
965 If enabled, the debug registers values are shown when GDB inserts\n\
966 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
967 triggers a breakpoint or watchpoint."),
970 &maintenance_set_cmdlist
,
971 &maintenance_show_cmdlist
);
974 /* -Wmissing-prototypes. */
975 void _initialize_aarch64_linux_nat (void);
978 _initialize_aarch64_linux_nat (void)
980 struct target_ops
*t
;
982 /* Fill in the generic GNU/Linux methods. */
985 add_show_debug_regs_command ();
987 /* Add our register access methods. */
988 t
->to_fetch_registers
= aarch64_linux_fetch_inferior_registers
;
989 t
->to_store_registers
= aarch64_linux_store_inferior_registers
;
991 t
->to_read_description
= aarch64_linux_read_description
;
993 t
->to_can_use_hw_breakpoint
= aarch64_linux_can_use_hw_breakpoint
;
994 t
->to_insert_hw_breakpoint
= aarch64_linux_insert_hw_breakpoint
;
995 t
->to_remove_hw_breakpoint
= aarch64_linux_remove_hw_breakpoint
;
996 t
->to_region_ok_for_hw_watchpoint
=
997 aarch64_linux_region_ok_for_hw_watchpoint
;
998 t
->to_insert_watchpoint
= aarch64_linux_insert_watchpoint
;
999 t
->to_remove_watchpoint
= aarch64_linux_remove_watchpoint
;
1000 t
->to_stopped_by_watchpoint
= aarch64_linux_stopped_by_watchpoint
;
1001 t
->to_stopped_data_address
= aarch64_linux_stopped_data_address
;
1002 t
->to_watchpoint_addr_within_range
=
1003 aarch64_linux_watchpoint_addr_within_range
;
1005 /* Override the GNU/Linux inferior startup hook. */
1006 super_post_startup_inferior
= t
->to_post_startup_inferior
;
1007 t
->to_post_startup_inferior
= aarch64_linux_child_post_startup_inferior
;
1009 /* Register the target. */
1010 linux_nat_add_target (t
);
1011 linux_nat_set_new_thread (t
, aarch64_linux_new_thread
);
1012 linux_nat_set_new_fork (t
, aarch64_linux_new_fork
);
1013 linux_nat_set_forget_process (t
, aarch64_forget_process
);
1014 linux_nat_set_prepare_to_resume (t
, aarch64_linux_prepare_to_resume
);