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_pid (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 debug_printf ("debug_reg_change_callback: \n\tOn entry:\n");
181 debug_printf ("\tpid%d, dr_changed_bp=0x%s, "
182 "dr_changed_wp=0x%s\n", pid,
183 phex (info->dr_changed_bp, 8),
184 phex (info->dr_changed_wp, 8));
187 dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp
188 : &info->dr_changed_bp;
189 dr_changed = *dr_changed_ptr;
192 && (idx <= (is_watchpoint ? aarch64_num_wp_regs
193 : aarch64_num_bp_regs)));
195 /* The actual update is done later just before resuming the lwp,
196 we just mark that one register pair needs updating. */
197 DR_MARK_N_CHANGED (dr_changed, idx);
198 *dr_changed_ptr = dr_changed;
200 /* If the lwp isn't stopped, force it to momentarily pause, so
201 we can update its debug registers. */
203 linux_stop_lwp (lwp);
207 debug_printf ("\tOn exit:\n\tpid%d, dr_changed_bp=0x%s, "
208 "dr_changed_wp=0x%s\n", pid,
209 phex (info->dr_changed_bp, 8),
210 phex (info->dr_changed_wp, 8));
213 /* Continue the iteration. */
217 /* Notify each thread that their IDXth breakpoint/watchpoint register
218 pair needs to be updated. The message will be recorded in each
219 thread's arch-specific data area, the actual updating will be done
220 when the thread is resumed. */
223 aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state *state,
224 int is_watchpoint, unsigned int idx)
226 struct aarch64_dr_update_callback_param param;
227 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (current_lwp_ptid ()));
229 param.is_watchpoint = is_watchpoint;
232 iterate_over_lwps (pid_ptid, debug_reg_change_callback, (void *) ¶m);
235 /* Fill GDB's register array with the general-purpose register values
236 from the current thread. */
239 fetch_gregs_from_thread (struct regcache *regcache)
242 struct gdbarch *gdbarch = get_regcache_arch (regcache);
246 /* Make sure REGS can hold all registers contents on both aarch64
248 gdb_static_assert (sizeof (regs) >= 18 * 4);
250 tid = ptid_get_lwp (inferior_ptid);
252 iovec.iov_base = ®s;
253 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
254 iovec.iov_len = 18 * 4;
256 iovec.iov_len = sizeof (regs);
258 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
260 perror_with_name (_("Unable to fetch general registers."));
262 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
263 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, 1);
268 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
269 regcache_raw_supply (regcache, regno, ®s[regno - AARCH64_X0_REGNUM]);
273 /* Store to the current thread the valid general-purpose register
274 values in the GDB's register array. */
277 store_gregs_to_thread (const struct regcache *regcache)
282 struct gdbarch *gdbarch = get_regcache_arch (regcache);
284 /* Make sure REGS can hold all registers contents on both aarch64
286 gdb_static_assert (sizeof (regs) >= 18 * 4);
287 tid = ptid_get_lwp (inferior_ptid);
289 iovec.iov_base = ®s;
290 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
291 iovec.iov_len = 18 * 4;
293 iovec.iov_len = sizeof (regs);
295 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
297 perror_with_name (_("Unable to fetch general registers."));
299 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
300 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, 1);
305 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
306 if (REG_VALID == regcache_register_status (regcache, regno))
307 regcache_raw_collect (regcache, regno,
308 ®s[regno - AARCH64_X0_REGNUM]);
311 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
313 perror_with_name (_("Unable to store general registers."));
316 /* Fill GDB's register array with the fp/simd register values
317 from the current thread. */
320 fetch_fpregs_from_thread (struct regcache *regcache)
325 struct gdbarch *gdbarch = get_regcache_arch (regcache);
327 /* Make sure REGS can hold all VFP registers contents on both aarch64
329 gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
331 tid = ptid_get_lwp (inferior_ptid);
333 iovec.iov_base = ®s;
335 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
337 iovec.iov_len = VFP_REGS_SIZE;
339 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
341 perror_with_name (_("Unable to fetch VFP registers."));
343 aarch32_vfp_regcache_supply (regcache, (gdb_byte *) ®s, 32);
349 iovec.iov_len = sizeof (regs);
351 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
353 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
355 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
356 regcache_raw_supply (regcache, regno,
357 ®s.vregs[regno - AARCH64_V0_REGNUM]);
359 regcache_raw_supply (regcache, AARCH64_FPSR_REGNUM, ®s.fpsr);
360 regcache_raw_supply (regcache, AARCH64_FPCR_REGNUM, ®s.fpcr);
364 /* Store to the current thread the valid fp/simd register
365 values in the GDB's register array. */
368 store_fpregs_to_thread (const struct regcache *regcache)
373 struct gdbarch *gdbarch = get_regcache_arch (regcache);
375 /* Make sure REGS can hold all VFP registers contents on both aarch64
377 gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
378 tid = ptid_get_lwp (inferior_ptid);
380 iovec.iov_base = ®s;
382 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
384 iovec.iov_len = VFP_REGS_SIZE;
386 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
388 perror_with_name (_("Unable to fetch VFP registers."));
390 aarch32_vfp_regcache_collect (regcache, (gdb_byte *) ®s, 32);
396 iovec.iov_len = sizeof (regs);
398 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
400 perror_with_name (_("Unable to fetch FP/SIMD registers."));
402 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
403 if (REG_VALID == regcache_register_status (regcache, regno))
404 regcache_raw_collect (regcache, regno,
405 (char *) ®s.vregs[regno - AARCH64_V0_REGNUM]);
407 if (REG_VALID == regcache_register_status (regcache, AARCH64_FPSR_REGNUM))
408 regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM,
409 (char *) ®s.fpsr);
410 if (REG_VALID == regcache_register_status (regcache, AARCH64_FPCR_REGNUM))
411 regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM,
412 (char *) ®s.fpcr);
415 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
417 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iovec);
419 perror_with_name (_("Unable to store VFP registers."));
423 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
425 perror_with_name (_("Unable to store FP/SIMD registers."));
429 /* Implement the "to_fetch_register" target_ops method. */
432 aarch64_linux_fetch_inferior_registers (struct target_ops *ops,
433 struct regcache *regcache,
438 fetch_gregs_from_thread (regcache);
439 fetch_fpregs_from_thread (regcache);
441 else if (regno < AARCH64_V0_REGNUM)
442 fetch_gregs_from_thread (regcache);
444 fetch_fpregs_from_thread (regcache);
447 /* Implement the "to_store_register" target_ops method. */
450 aarch64_linux_store_inferior_registers (struct target_ops *ops,
451 struct regcache *regcache,
456 store_gregs_to_thread (regcache);
457 store_fpregs_to_thread (regcache);
459 else if (regno < AARCH64_V0_REGNUM)
460 store_gregs_to_thread (regcache);
462 store_fpregs_to_thread (regcache);
465 /* Fill register REGNO (if it is a general-purpose register) in
466 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
467 do this for all registers. */
470 fill_gregset (const struct regcache *regcache,
471 gdb_gregset_t *gregsetp, int regno)
473 regcache_collect_regset (&aarch64_linux_gregset, regcache,
474 regno, (gdb_byte *) gregsetp,
475 AARCH64_LINUX_SIZEOF_GREGSET);
478 /* Fill GDB's register array with the general-purpose register values
482 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
484 regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
485 (const gdb_byte *) gregsetp,
486 AARCH64_LINUX_SIZEOF_GREGSET);
489 /* Fill register REGNO (if it is a floating-point register) in
490 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
491 do this for all registers. */
494 fill_fpregset (const struct regcache *regcache,
495 gdb_fpregset_t *fpregsetp, int regno)
497 regcache_collect_regset (&aarch64_linux_fpregset, regcache,
498 regno, (gdb_byte *) fpregsetp,
499 AARCH64_LINUX_SIZEOF_FPREGSET);
502 /* Fill GDB's register array with the floating-point register values
506 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
508 regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
509 (const gdb_byte *) fpregsetp,
510 AARCH64_LINUX_SIZEOF_FPREGSET);
513 /* Called when resuming a thread.
514 The hardware debug registers are updated when there is any change. */
517 aarch64_linux_prepare_to_resume (struct lwp_info *lwp)
519 struct arch_lwp_info *info = lwp->arch_private;
521 /* NULL means this is the main thread still going through the shell,
522 or, no watchpoint has been set yet. In that case, there's
527 if (DR_HAS_CHANGED (info->dr_changed_bp)
528 || DR_HAS_CHANGED (info->dr_changed_wp))
530 int tid = ptid_get_lwp (lwp->ptid);
531 struct aarch64_debug_reg_state *state
532 = aarch64_get_debug_reg_state (ptid_get_pid (lwp->ptid));
535 fprintf_unfiltered (gdb_stdlog, "prepare_to_resume thread %d\n", tid);
538 if (DR_HAS_CHANGED (info->dr_changed_wp))
540 aarch64_linux_set_debug_regs (state, tid, 1);
541 DR_CLEAR_CHANGED (info->dr_changed_wp);
545 if (DR_HAS_CHANGED (info->dr_changed_bp))
547 aarch64_linux_set_debug_regs (state, tid, 0);
548 DR_CLEAR_CHANGED (info->dr_changed_bp);
554 aarch64_linux_new_thread (struct lwp_info *lp)
556 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
558 /* Mark that all the hardware breakpoint/watchpoint register pairs
559 for this thread need to be initialized. */
560 DR_MARK_ALL_CHANGED (info->dr_changed_bp, aarch64_num_bp_regs);
561 DR_MARK_ALL_CHANGED (info->dr_changed_wp, aarch64_num_wp_regs);
563 lp->arch_private = info;
566 /* linux_nat_new_fork hook. */
569 aarch64_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
572 struct aarch64_debug_reg_state *parent_state;
573 struct aarch64_debug_reg_state *child_state;
575 /* NULL means no watchpoint has ever been set in the parent. In
576 that case, there's nothing to do. */
577 if (parent->arch_private == NULL)
580 /* GDB core assumes the child inherits the watchpoints/hw
581 breakpoints of the parent, and will remove them all from the
582 forked off process. Copy the debug registers mirrors into the
583 new process so that all breakpoints and watchpoints can be
586 parent_pid = ptid_get_pid (parent->ptid);
587 parent_state = aarch64_get_debug_reg_state (parent_pid);
588 child_state = aarch64_get_debug_reg_state (child_pid);
589 *child_state = *parent_state;
593 /* Called by libthread_db. Returns a pointer to the thread local
594 storage (or its descriptor). */
597 ps_get_thread_area (const struct ps_prochandle *ph,
598 lwpid_t lwpid, int idx, void **base)
603 iovec.iov_base = ®
604 iovec.iov_len = sizeof (reg);
606 if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0)
609 /* IDX is the bias from the thread pointer to the beginning of the
610 thread descriptor. It has to be subtracted due to implementation
611 quirks in libthread_db. */
612 *base = (void *) (reg - idx);
618 static void (*super_post_startup_inferior) (struct target_ops *self,
621 /* Implement the "to_post_startup_inferior" target_ops method. */
624 aarch64_linux_child_post_startup_inferior (struct target_ops *self,
627 aarch64_forget_process (ptid_get_pid (ptid));
628 aarch64_linux_get_debug_reg_capacity (ptid_get_pid (ptid));
629 super_post_startup_inferior (self, ptid);
632 extern struct target_desc *tdesc_arm_with_vfpv3;
633 extern struct target_desc *tdesc_arm_with_neon;
635 /* Implement the "to_read_description" target_ops method. */
637 static const struct target_desc *
638 aarch64_linux_read_description (struct target_ops *ops)
642 if (target_auxv_search (ops, AT_PHENT, &at_phent) == 1)
644 if (at_phent == sizeof (Elf64_External_Phdr))
645 return tdesc_aarch64;
648 CORE_ADDR arm_hwcap = 0;
650 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
651 return ops->beneath->to_read_description (ops->beneath);
653 #ifndef COMPAT_HWCAP_VFP
654 #define COMPAT_HWCAP_VFP (1 << 6)
656 #ifndef COMPAT_HWCAP_NEON
657 #define COMPAT_HWCAP_NEON (1 << 12)
659 #ifndef COMPAT_HWCAP_VFPv3
660 #define COMPAT_HWCAP_VFPv3 (1 << 13)
663 if (arm_hwcap & COMPAT_HWCAP_VFP)
666 const struct target_desc *result = NULL;
668 if (arm_hwcap & COMPAT_HWCAP_NEON)
669 result = tdesc_arm_with_neon;
670 else if (arm_hwcap & COMPAT_HWCAP_VFPv3)
671 result = tdesc_arm_with_vfpv3;
680 return tdesc_aarch64;
683 /* Returns the number of hardware watchpoints of type TYPE that we can
684 set. Value is positive if we can set CNT watchpoints, zero if
685 setting watchpoints of type TYPE is not supported, and negative if
686 CNT is more than the maximum number of watchpoints of type TYPE
687 that we can support. TYPE is one of bp_hardware_watchpoint,
688 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
689 CNT is the number of such watchpoints used so far (including this
690 one). OTHERTYPE is non-zero if other types of watchpoints are
691 currently enabled. */
694 aarch64_linux_can_use_hw_breakpoint (struct target_ops *self,
696 int cnt, int othertype)
698 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
699 || type == bp_access_watchpoint || type == bp_watchpoint)
701 if (aarch64_num_wp_regs == 0)
704 else if (type == bp_hardware_breakpoint)
706 if (aarch64_num_bp_regs == 0)
710 gdb_assert_not_reached ("unexpected breakpoint type");
712 /* We always return 1 here because we don't have enough information
713 about possible overlap of addresses that they want to watch. As an
714 extreme example, consider the case where all the watchpoints watch
715 the same address and the same region length: then we can handle a
716 virtually unlimited number of watchpoints, due to debug register
717 sharing implemented via reference counts. */
721 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
722 Return 0 on success, -1 on failure. */
725 aarch64_linux_insert_hw_breakpoint (struct target_ops *self,
726 struct gdbarch *gdbarch,
727 struct bp_target_info *bp_tgt)
730 CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
732 const enum target_hw_bp_type type = hw_execute;
733 struct aarch64_debug_reg_state *state
734 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
739 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
740 (unsigned long) addr, len);
742 ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */, state);
746 aarch64_show_debug_reg_state (state,
747 "insert_hw_breakpoint", addr, len, type);
753 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
754 Return 0 on success, -1 on failure. */
757 aarch64_linux_remove_hw_breakpoint (struct target_ops *self,
758 struct gdbarch *gdbarch,
759 struct bp_target_info *bp_tgt)
762 CORE_ADDR addr = bp_tgt->placed_address;
764 const enum target_hw_bp_type type = hw_execute;
765 struct aarch64_debug_reg_state *state
766 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
770 (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
771 (unsigned long) addr, len);
773 ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */, state);
777 aarch64_show_debug_reg_state (state,
778 "remove_hw_watchpoint", addr, len, type);
784 /* Implement the "to_insert_watchpoint" target_ops method.
786 Insert a watchpoint to watch a memory region which starts at
787 address ADDR and whose length is LEN bytes. Watch memory accesses
788 of the type TYPE. Return 0 on success, -1 on failure. */
791 aarch64_linux_insert_watchpoint (struct target_ops *self,
792 CORE_ADDR addr, int len,
793 enum target_hw_bp_type type,
794 struct expression *cond)
797 struct aarch64_debug_reg_state *state
798 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
801 fprintf_unfiltered (gdb_stdlog,
802 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
803 (unsigned long) addr, len);
805 gdb_assert (type != hw_execute);
807 ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */, state);
811 aarch64_show_debug_reg_state (state,
812 "insert_watchpoint", addr, len, type);
818 /* Implement the "to_remove_watchpoint" target_ops method.
819 Remove a watchpoint that watched the memory region which starts at
820 address ADDR, whose length is LEN bytes, and for accesses of the
821 type TYPE. Return 0 on success, -1 on failure. */
824 aarch64_linux_remove_watchpoint (struct target_ops *self,
825 CORE_ADDR addr, int len,
826 enum target_hw_bp_type type,
827 struct expression *cond)
830 struct aarch64_debug_reg_state *state
831 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
834 fprintf_unfiltered (gdb_stdlog,
835 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
836 (unsigned long) addr, len);
838 gdb_assert (type != hw_execute);
840 ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */, state);
844 aarch64_show_debug_reg_state (state,
845 "remove_watchpoint", addr, len, type);
851 /* Implement the "to_region_ok_for_hw_watchpoint" target_ops method. */
854 aarch64_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
855 CORE_ADDR addr, int len)
857 CORE_ADDR aligned_addr;
859 /* Can not set watchpoints for zero or negative lengths. */
863 /* Must have hardware watchpoint debug register(s). */
864 if (aarch64_num_wp_regs == 0)
867 /* We support unaligned watchpoint address and arbitrary length,
868 as long as the size of the whole watched area after alignment
869 doesn't exceed size of the total area that all watchpoint debug
870 registers can watch cooperatively.
872 This is a very relaxed rule, but unfortunately there are
873 limitations, e.g. false-positive hits, due to limited support of
874 hardware debug registers in the kernel. See comment above
875 aarch64_align_watchpoint for more information. */
877 aligned_addr = addr & ~(AARCH64_HWP_MAX_LEN_PER_REG - 1);
878 if (aligned_addr + aarch64_num_wp_regs * AARCH64_HWP_MAX_LEN_PER_REG
882 /* All tests passed so we are likely to be able to set the watchpoint.
883 The reason that it is 'likely' rather than 'must' is because
884 we don't check the current usage of the watchpoint registers, and
885 there may not be enough registers available for this watchpoint.
886 Ideally we should check the cached debug register state, however
887 the checking is costly. */
891 /* Implement the "to_stopped_data_address" target_ops method. */
894 aarch64_linux_stopped_data_address (struct target_ops *target,
899 struct aarch64_debug_reg_state *state;
901 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
904 /* This must be a hardware breakpoint. */
905 if (siginfo.si_signo != SIGTRAP
906 || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
909 /* Check if the address matches any watched address. */
910 state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
911 for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
913 const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
914 const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
915 const CORE_ADDR addr_watch = state->dr_addr_wp[i];
917 if (state->dr_ref_count_wp[i]
918 && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
919 && addr_trap >= addr_watch
920 && addr_trap < addr_watch + len)
930 /* Implement the "to_stopped_by_watchpoint" target_ops method. */
933 aarch64_linux_stopped_by_watchpoint (struct target_ops *ops)
937 return aarch64_linux_stopped_data_address (ops, &addr);
940 /* Implement the "to_watchpoint_addr_within_range" target_ops method. */
943 aarch64_linux_watchpoint_addr_within_range (struct target_ops *target,
945 CORE_ADDR start, int length)
947 return start <= addr && start + length - 1 >= addr;
950 /* Define AArch64 maintenance commands. */
953 add_show_debug_regs_command (void)
955 /* A maintenance command to enable printing the internal DRi mirror
957 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
958 &show_debug_regs, _("\
959 Set whether to show variables that mirror the AArch64 debug registers."), _("\
960 Show whether to show variables that mirror the AArch64 debug registers."), _("\
961 Use \"on\" to enable, \"off\" to disable.\n\
962 If enabled, the debug registers values are shown when GDB inserts\n\
963 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
964 triggers a breakpoint or watchpoint."),
967 &maintenance_set_cmdlist,
968 &maintenance_show_cmdlist);
971 /* -Wmissing-prototypes. */
972 void _initialize_aarch64_linux_nat (void);
975 _initialize_aarch64_linux_nat (void)
977 struct target_ops *t;
979 /* Fill in the generic GNU/Linux methods. */
982 add_show_debug_regs_command ();
984 /* Add our register access methods. */
985 t->to_fetch_registers = aarch64_linux_fetch_inferior_registers;
986 t->to_store_registers = aarch64_linux_store_inferior_registers;
988 t->to_read_description = aarch64_linux_read_description;
990 t->to_can_use_hw_breakpoint = aarch64_linux_can_use_hw_breakpoint;
991 t->to_insert_hw_breakpoint = aarch64_linux_insert_hw_breakpoint;
992 t->to_remove_hw_breakpoint = aarch64_linux_remove_hw_breakpoint;
993 t->to_region_ok_for_hw_watchpoint =
994 aarch64_linux_region_ok_for_hw_watchpoint;
995 t->to_insert_watchpoint = aarch64_linux_insert_watchpoint;
996 t->to_remove_watchpoint = aarch64_linux_remove_watchpoint;
997 t->to_stopped_by_watchpoint = aarch64_linux_stopped_by_watchpoint;
998 t->to_stopped_data_address = aarch64_linux_stopped_data_address;
999 t->to_watchpoint_addr_within_range =
1000 aarch64_linux_watchpoint_addr_within_range;
1002 /* Override the GNU/Linux inferior startup hook. */
1003 super_post_startup_inferior = t->to_post_startup_inferior;
1004 t->to_post_startup_inferior = aarch64_linux_child_post_startup_inferior;
1006 /* Register the target. */
1007 linux_nat_add_target (t);
1008 linux_nat_set_new_thread (t, aarch64_linux_new_thread);
1009 linux_nat_set_new_fork (t, aarch64_linux_new_fork);
1010 linux_nat_set_forget_process (t, aarch64_forget_process);
1011 linux_nat_set_prepare_to_resume (t, aarch64_linux_prepare_to_resume);