Move aarch64_linux_prepare_to_resume to nat/aarch64-linux.c
[external/binutils.git] / gdb / aarch64-linux-nat.c
1 /* Native-dependent code for GNU/Linux AArch64.
2
3    Copyright (C) 2011-2015 Free Software Foundation, Inc.
4    Contributed by ARM Ltd.
5
6    This file is part of GDB.
7
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.
12
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.
17
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/>.  */
20
21 #include "defs.h"
22
23 #include "inferior.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "linux-nat.h"
27 #include "target-descriptions.h"
28 #include "auxv.h"
29 #include "gdbcmd.h"
30 #include "aarch64-tdep.h"
31 #include "aarch64-linux-tdep.h"
32 #include "aarch32-linux-nat.h"
33 #include "nat/aarch64-linux.h"
34 #include "nat/aarch64-linux-hw-point.h"
35
36 #include "elf/external.h"
37 #include "elf/common.h"
38
39 #include "nat/gdb_ptrace.h"
40 #include <sys/utsname.h>
41 #include <asm/ptrace.h>
42
43 #include "gregset.h"
44
45 /* Defines ps_err_e, struct ps_prochandle.  */
46 #include "gdb_proc_service.h"
47
48 #ifndef TRAP_HWBKPT
49 #define TRAP_HWBKPT 0x0004
50 #endif
51
52 /* Per-process data.  We don't bind this to a per-inferior registry
53    because of targets like x86 GNU/Linux that need to keep track of
54    processes that aren't bound to any inferior (e.g., fork children,
55    checkpoints).  */
56
57 struct aarch64_process_info
58 {
59   /* Linked list.  */
60   struct aarch64_process_info *next;
61
62   /* The process identifier.  */
63   pid_t pid;
64
65   /* Copy of aarch64 hardware debug registers.  */
66   struct aarch64_debug_reg_state state;
67 };
68
69 static struct aarch64_process_info *aarch64_process_list = NULL;
70
71 /* Find process data for process PID.  */
72
73 static struct aarch64_process_info *
74 aarch64_find_process_pid (pid_t pid)
75 {
76   struct aarch64_process_info *proc;
77
78   for (proc = aarch64_process_list; proc; proc = proc->next)
79     if (proc->pid == pid)
80       return proc;
81
82   return NULL;
83 }
84
85 /* Add process data for process PID.  Returns newly allocated info
86    object.  */
87
88 static struct aarch64_process_info *
89 aarch64_add_process (pid_t pid)
90 {
91   struct aarch64_process_info *proc;
92
93   proc = xcalloc (1, sizeof (*proc));
94   proc->pid = pid;
95
96   proc->next = aarch64_process_list;
97   aarch64_process_list = proc;
98
99   return proc;
100 }
101
102 /* Get data specific info for process PID, creating it if necessary.
103    Never returns NULL.  */
104
105 static struct aarch64_process_info *
106 aarch64_process_info_get (pid_t pid)
107 {
108   struct aarch64_process_info *proc;
109
110   proc = aarch64_find_process_pid (pid);
111   if (proc == NULL)
112     proc = aarch64_add_process (pid);
113
114   return proc;
115 }
116
117 /* Called whenever GDB is no longer debugging process PID.  It deletes
118    data structures that keep track of debug register state.  */
119
120 static void
121 aarch64_forget_process (pid_t pid)
122 {
123   struct aarch64_process_info *proc, **proc_link;
124
125   proc = aarch64_process_list;
126   proc_link = &aarch64_process_list;
127
128   while (proc != NULL)
129     {
130       if (proc->pid == pid)
131         {
132           *proc_link = proc->next;
133
134           xfree (proc);
135           return;
136         }
137
138       proc_link = &proc->next;
139       proc = *proc_link;
140     }
141 }
142
143 /* Get debug registers state for process PID.  */
144
145 struct aarch64_debug_reg_state *
146 aarch64_get_debug_reg_state (pid_t pid)
147 {
148   return &aarch64_process_info_get (pid)->state;
149 }
150
151 /* Fill GDB's register array with the general-purpose register values
152    from the current thread.  */
153
154 static void
155 fetch_gregs_from_thread (struct regcache *regcache)
156 {
157   int ret, tid;
158   struct gdbarch *gdbarch = get_regcache_arch (regcache);
159   elf_gregset_t regs;
160   struct iovec iovec;
161
162   /* Make sure REGS can hold all registers contents on both aarch64
163      and arm.  */
164   gdb_static_assert (sizeof (regs) >= 18 * 4);
165
166   tid = ptid_get_lwp (inferior_ptid);
167
168   iovec.iov_base = &regs;
169   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
170     iovec.iov_len = 18 * 4;
171   else
172     iovec.iov_len = sizeof (regs);
173
174   ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
175   if (ret < 0)
176     perror_with_name (_("Unable to fetch general registers."));
177
178   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
179     aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, 1);
180   else
181     {
182       int regno;
183
184       for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
185         regcache_raw_supply (regcache, regno, &regs[regno - AARCH64_X0_REGNUM]);
186     }
187 }
188
189 /* Store to the current thread the valid general-purpose register
190    values in the GDB's register array.  */
191
192 static void
193 store_gregs_to_thread (const struct regcache *regcache)
194 {
195   int ret, tid;
196   elf_gregset_t regs;
197   struct iovec iovec;
198   struct gdbarch *gdbarch = get_regcache_arch (regcache);
199
200   /* Make sure REGS can hold all registers contents on both aarch64
201      and arm.  */
202   gdb_static_assert (sizeof (regs) >= 18 * 4);
203   tid = ptid_get_lwp (inferior_ptid);
204
205   iovec.iov_base = &regs;
206   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
207     iovec.iov_len = 18 * 4;
208   else
209     iovec.iov_len = sizeof (regs);
210
211   ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
212   if (ret < 0)
213     perror_with_name (_("Unable to fetch general registers."));
214
215   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
216     aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, 1);
217   else
218     {
219       int regno;
220
221       for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
222         if (REG_VALID == regcache_register_status (regcache, regno))
223           regcache_raw_collect (regcache, regno,
224                                 &regs[regno - AARCH64_X0_REGNUM]);
225     }
226
227   ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
228   if (ret < 0)
229     perror_with_name (_("Unable to store general registers."));
230 }
231
232 /* Fill GDB's register array with the fp/simd register values
233    from the current thread.  */
234
235 static void
236 fetch_fpregs_from_thread (struct regcache *regcache)
237 {
238   int ret, tid;
239   elf_fpregset_t regs;
240   struct iovec iovec;
241   struct gdbarch *gdbarch = get_regcache_arch (regcache);
242
243   /* Make sure REGS can hold all VFP registers contents on both aarch64
244      and arm.  */
245   gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
246
247   tid = ptid_get_lwp (inferior_ptid);
248
249   iovec.iov_base = &regs;
250
251   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
252     {
253       iovec.iov_len = VFP_REGS_SIZE;
254
255       ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
256       if (ret < 0)
257         perror_with_name (_("Unable to fetch VFP registers."));
258
259       aarch32_vfp_regcache_supply (regcache, (gdb_byte *) &regs, 32);
260     }
261   else
262     {
263       int regno;
264
265       iovec.iov_len = sizeof (regs);
266
267       ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
268       if (ret < 0)
269         perror_with_name (_("Unable to fetch vFP/SIMD registers."));
270
271       for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
272         regcache_raw_supply (regcache, regno,
273                              &regs.vregs[regno - AARCH64_V0_REGNUM]);
274
275       regcache_raw_supply (regcache, AARCH64_FPSR_REGNUM, &regs.fpsr);
276       regcache_raw_supply (regcache, AARCH64_FPCR_REGNUM, &regs.fpcr);
277     }
278 }
279
280 /* Store to the current thread the valid fp/simd register
281    values in the GDB's register array.  */
282
283 static void
284 store_fpregs_to_thread (const struct regcache *regcache)
285 {
286   int ret, tid;
287   elf_fpregset_t regs;
288   struct iovec iovec;
289   struct gdbarch *gdbarch = get_regcache_arch (regcache);
290
291   /* Make sure REGS can hold all VFP registers contents on both aarch64
292      and arm.  */
293   gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
294   tid = ptid_get_lwp (inferior_ptid);
295
296   iovec.iov_base = &regs;
297
298   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
299     {
300       iovec.iov_len = VFP_REGS_SIZE;
301
302       ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
303       if (ret < 0)
304         perror_with_name (_("Unable to fetch VFP registers."));
305
306       aarch32_vfp_regcache_collect (regcache, (gdb_byte *) &regs, 32);
307     }
308   else
309     {
310       int regno;
311
312       iovec.iov_len = sizeof (regs);
313
314       ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
315       if (ret < 0)
316         perror_with_name (_("Unable to fetch FP/SIMD registers."));
317
318       for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
319         if (REG_VALID == regcache_register_status (regcache, regno))
320           regcache_raw_collect (regcache, regno,
321                                 (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
322
323       if (REG_VALID == regcache_register_status (regcache, AARCH64_FPSR_REGNUM))
324         regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM,
325                               (char *) &regs.fpsr);
326       if (REG_VALID == regcache_register_status (regcache, AARCH64_FPCR_REGNUM))
327         regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM,
328                               (char *) &regs.fpcr);
329     }
330
331   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
332     {
333       ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iovec);
334       if (ret < 0)
335         perror_with_name (_("Unable to store VFP registers."));
336     }
337   else
338     {
339       ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
340       if (ret < 0)
341         perror_with_name (_("Unable to store FP/SIMD registers."));
342     }
343 }
344
345 /* Implement the "to_fetch_register" target_ops method.  */
346
347 static void
348 aarch64_linux_fetch_inferior_registers (struct target_ops *ops,
349                                         struct regcache *regcache,
350                                         int regno)
351 {
352   if (regno == -1)
353     {
354       fetch_gregs_from_thread (regcache);
355       fetch_fpregs_from_thread (regcache);
356     }
357   else if (regno < AARCH64_V0_REGNUM)
358     fetch_gregs_from_thread (regcache);
359   else
360     fetch_fpregs_from_thread (regcache);
361 }
362
363 /* Implement the "to_store_register" target_ops method.  */
364
365 static void
366 aarch64_linux_store_inferior_registers (struct target_ops *ops,
367                                         struct regcache *regcache,
368                                         int regno)
369 {
370   if (regno == -1)
371     {
372       store_gregs_to_thread (regcache);
373       store_fpregs_to_thread (regcache);
374     }
375   else if (regno < AARCH64_V0_REGNUM)
376     store_gregs_to_thread (regcache);
377   else
378     store_fpregs_to_thread (regcache);
379 }
380
381 /* Fill register REGNO (if it is a general-purpose register) in
382    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
383    do this for all registers.  */
384
385 void
386 fill_gregset (const struct regcache *regcache,
387               gdb_gregset_t *gregsetp, int regno)
388 {
389   regcache_collect_regset (&aarch64_linux_gregset, regcache,
390                            regno, (gdb_byte *) gregsetp,
391                            AARCH64_LINUX_SIZEOF_GREGSET);
392 }
393
394 /* Fill GDB's register array with the general-purpose register values
395    in *GREGSETP.  */
396
397 void
398 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
399 {
400   regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
401                           (const gdb_byte *) gregsetp,
402                           AARCH64_LINUX_SIZEOF_GREGSET);
403 }
404
405 /* Fill register REGNO (if it is a floating-point register) in
406    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
407    do this for all registers.  */
408
409 void
410 fill_fpregset (const struct regcache *regcache,
411                gdb_fpregset_t *fpregsetp, int regno)
412 {
413   regcache_collect_regset (&aarch64_linux_fpregset, regcache,
414                            regno, (gdb_byte *) fpregsetp,
415                            AARCH64_LINUX_SIZEOF_FPREGSET);
416 }
417
418 /* Fill GDB's register array with the floating-point register values
419    in *FPREGSETP.  */
420
421 void
422 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
423 {
424   regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
425                           (const gdb_byte *) fpregsetp,
426                           AARCH64_LINUX_SIZEOF_FPREGSET);
427 }
428
429 static void
430 aarch64_linux_new_thread (struct lwp_info *lp)
431 {
432   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
433
434   /* Mark that all the hardware breakpoint/watchpoint register pairs
435      for this thread need to be initialized.  */
436   DR_MARK_ALL_CHANGED (info->dr_changed_bp, aarch64_num_bp_regs);
437   DR_MARK_ALL_CHANGED (info->dr_changed_wp, aarch64_num_wp_regs);
438
439   lp->arch_private = info;
440 }
441
442 /* linux_nat_new_fork hook.   */
443
444 static void
445 aarch64_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
446 {
447   pid_t parent_pid;
448   struct aarch64_debug_reg_state *parent_state;
449   struct aarch64_debug_reg_state *child_state;
450
451   /* NULL means no watchpoint has ever been set in the parent.  In
452      that case, there's nothing to do.  */
453   if (parent->arch_private == NULL)
454     return;
455
456   /* GDB core assumes the child inherits the watchpoints/hw
457      breakpoints of the parent, and will remove them all from the
458      forked off process.  Copy the debug registers mirrors into the
459      new process so that all breakpoints and watchpoints can be
460      removed together.  */
461
462   parent_pid = ptid_get_pid (parent->ptid);
463   parent_state = aarch64_get_debug_reg_state (parent_pid);
464   child_state = aarch64_get_debug_reg_state (child_pid);
465   *child_state = *parent_state;
466 }
467 \f
468
469 /* Called by libthread_db.  Returns a pointer to the thread local
470    storage (or its descriptor).  */
471
472 ps_err_e
473 ps_get_thread_area (const struct ps_prochandle *ph,
474                     lwpid_t lwpid, int idx, void **base)
475 {
476   struct iovec iovec;
477   uint64_t reg;
478
479   iovec.iov_base = &reg;
480   iovec.iov_len = sizeof (reg);
481
482   if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0)
483     return PS_ERR;
484
485   /* IDX is the bias from the thread pointer to the beginning of the
486      thread descriptor.  It has to be subtracted due to implementation
487      quirks in libthread_db.  */
488   *base = (void *) (reg - idx);
489
490   return PS_OK;
491 }
492 \f
493
494 static void (*super_post_startup_inferior) (struct target_ops *self,
495                                             ptid_t ptid);
496
497 /* Implement the "to_post_startup_inferior" target_ops method.  */
498
499 static void
500 aarch64_linux_child_post_startup_inferior (struct target_ops *self,
501                                            ptid_t ptid)
502 {
503   aarch64_forget_process (ptid_get_pid (ptid));
504   aarch64_linux_get_debug_reg_capacity (ptid_get_pid (ptid));
505   super_post_startup_inferior (self, ptid);
506 }
507
508 extern struct target_desc *tdesc_arm_with_vfpv3;
509 extern struct target_desc *tdesc_arm_with_neon;
510
511 /* Implement the "to_read_description" target_ops method.  */
512
513 static const struct target_desc *
514 aarch64_linux_read_description (struct target_ops *ops)
515 {
516   CORE_ADDR at_phent;
517
518   if (target_auxv_search (ops, AT_PHENT, &at_phent) == 1)
519     {
520       if (at_phent == sizeof (Elf64_External_Phdr))
521         return tdesc_aarch64;
522       else
523         {
524           CORE_ADDR arm_hwcap = 0;
525
526           if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
527             return ops->beneath->to_read_description (ops->beneath);
528
529 #ifndef COMPAT_HWCAP_VFP
530 #define COMPAT_HWCAP_VFP        (1 << 6)
531 #endif
532 #ifndef COMPAT_HWCAP_NEON
533 #define COMPAT_HWCAP_NEON       (1 << 12)
534 #endif
535 #ifndef COMPAT_HWCAP_VFPv3
536 #define COMPAT_HWCAP_VFPv3      (1 << 13)
537 #endif
538
539           if (arm_hwcap & COMPAT_HWCAP_VFP)
540             {
541               char *buf;
542               const struct target_desc *result = NULL;
543
544               if (arm_hwcap & COMPAT_HWCAP_NEON)
545                 result = tdesc_arm_with_neon;
546               else if (arm_hwcap & COMPAT_HWCAP_VFPv3)
547                 result = tdesc_arm_with_vfpv3;
548
549               return result;
550             }
551
552           return NULL;
553         }
554     }
555
556   return tdesc_aarch64;
557 }
558
559 /* Returns the number of hardware watchpoints of type TYPE that we can
560    set.  Value is positive if we can set CNT watchpoints, zero if
561    setting watchpoints of type TYPE is not supported, and negative if
562    CNT is more than the maximum number of watchpoints of type TYPE
563    that we can support.  TYPE is one of bp_hardware_watchpoint,
564    bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
565    CNT is the number of such watchpoints used so far (including this
566    one).  OTHERTYPE is non-zero if other types of watchpoints are
567    currently enabled.  */
568
569 static int
570 aarch64_linux_can_use_hw_breakpoint (struct target_ops *self,
571                                      enum bptype type,
572                                      int cnt, int othertype)
573 {
574   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
575       || type == bp_access_watchpoint || type == bp_watchpoint)
576     {
577       if (aarch64_num_wp_regs == 0)
578         return 0;
579     }
580   else if (type == bp_hardware_breakpoint)
581     {
582       if (aarch64_num_bp_regs == 0)
583         return 0;
584     }
585   else
586     gdb_assert_not_reached ("unexpected breakpoint type");
587
588   /* We always return 1 here because we don't have enough information
589      about possible overlap of addresses that they want to watch.  As an
590      extreme example, consider the case where all the watchpoints watch
591      the same address and the same region length: then we can handle a
592      virtually unlimited number of watchpoints, due to debug register
593      sharing implemented via reference counts.  */
594   return 1;
595 }
596
597 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
598    Return 0 on success, -1 on failure.  */
599
600 static int
601 aarch64_linux_insert_hw_breakpoint (struct target_ops *self,
602                                     struct gdbarch *gdbarch,
603                                     struct bp_target_info *bp_tgt)
604 {
605   int ret;
606   CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
607   const int len = 4;
608   const enum target_hw_bp_type type = hw_execute;
609   struct aarch64_debug_reg_state *state
610     = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
611
612   if (show_debug_regs)
613     fprintf_unfiltered
614       (gdb_stdlog,
615        "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
616        (unsigned long) addr, len);
617
618   ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */, state);
619
620   if (show_debug_regs)
621     {
622       aarch64_show_debug_reg_state (state,
623                                     "insert_hw_breakpoint", addr, len, type);
624     }
625
626   return ret;
627 }
628
629 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
630    Return 0 on success, -1 on failure.  */
631
632 static int
633 aarch64_linux_remove_hw_breakpoint (struct target_ops *self,
634                                     struct gdbarch *gdbarch,
635                                     struct bp_target_info *bp_tgt)
636 {
637   int ret;
638   CORE_ADDR addr = bp_tgt->placed_address;
639   const int len = 4;
640   const enum target_hw_bp_type type = hw_execute;
641   struct aarch64_debug_reg_state *state
642     = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
643
644   if (show_debug_regs)
645     fprintf_unfiltered
646       (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
647        (unsigned long) addr, len);
648
649   ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */, state);
650
651   if (show_debug_regs)
652     {
653       aarch64_show_debug_reg_state (state,
654                                     "remove_hw_watchpoint", addr, len, type);
655     }
656
657   return ret;
658 }
659
660 /* Implement the "to_insert_watchpoint" target_ops method.
661
662    Insert a watchpoint to watch a memory region which starts at
663    address ADDR and whose length is LEN bytes.  Watch memory accesses
664    of the type TYPE.  Return 0 on success, -1 on failure.  */
665
666 static int
667 aarch64_linux_insert_watchpoint (struct target_ops *self,
668                                  CORE_ADDR addr, int len,
669                                  enum target_hw_bp_type type,
670                                  struct expression *cond)
671 {
672   int ret;
673   struct aarch64_debug_reg_state *state
674     = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
675
676   if (show_debug_regs)
677     fprintf_unfiltered (gdb_stdlog,
678                         "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
679                         (unsigned long) addr, len);
680
681   gdb_assert (type != hw_execute);
682
683   ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */, state);
684
685   if (show_debug_regs)
686     {
687       aarch64_show_debug_reg_state (state,
688                                     "insert_watchpoint", addr, len, type);
689     }
690
691   return ret;
692 }
693
694 /* Implement the "to_remove_watchpoint" target_ops method.
695    Remove a watchpoint that watched the memory region which starts at
696    address ADDR, whose length is LEN bytes, and for accesses of the
697    type TYPE.  Return 0 on success, -1 on failure.  */
698
699 static int
700 aarch64_linux_remove_watchpoint (struct target_ops *self,
701                                  CORE_ADDR addr, int len,
702                                  enum target_hw_bp_type type,
703                                  struct expression *cond)
704 {
705   int ret;
706   struct aarch64_debug_reg_state *state
707     = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
708
709   if (show_debug_regs)
710     fprintf_unfiltered (gdb_stdlog,
711                         "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
712                         (unsigned long) addr, len);
713
714   gdb_assert (type != hw_execute);
715
716   ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */, state);
717
718   if (show_debug_regs)
719     {
720       aarch64_show_debug_reg_state (state,
721                                     "remove_watchpoint", addr, len, type);
722     }
723
724   return ret;
725 }
726
727 /* Implement the "to_region_ok_for_hw_watchpoint" target_ops method.  */
728
729 static int
730 aarch64_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
731                                            CORE_ADDR addr, int len)
732 {
733   CORE_ADDR aligned_addr;
734
735   /* Can not set watchpoints for zero or negative lengths.  */
736   if (len <= 0)
737     return 0;
738
739   /* Must have hardware watchpoint debug register(s).  */
740   if (aarch64_num_wp_regs == 0)
741     return 0;
742
743   /* We support unaligned watchpoint address and arbitrary length,
744      as long as the size of the whole watched area after alignment
745      doesn't exceed size of the total area that all watchpoint debug
746      registers can watch cooperatively.
747
748      This is a very relaxed rule, but unfortunately there are
749      limitations, e.g. false-positive hits, due to limited support of
750      hardware debug registers in the kernel.  See comment above
751      aarch64_align_watchpoint for more information.  */
752
753   aligned_addr = addr & ~(AARCH64_HWP_MAX_LEN_PER_REG - 1);
754   if (aligned_addr + aarch64_num_wp_regs * AARCH64_HWP_MAX_LEN_PER_REG
755       < addr + len)
756     return 0;
757
758   /* All tests passed so we are likely to be able to set the watchpoint.
759      The reason that it is 'likely' rather than 'must' is because
760      we don't check the current usage of the watchpoint registers, and
761      there may not be enough registers available for this watchpoint.
762      Ideally we should check the cached debug register state, however
763      the checking is costly.  */
764   return 1;
765 }
766
767 /* Implement the "to_stopped_data_address" target_ops method.  */
768
769 static int
770 aarch64_linux_stopped_data_address (struct target_ops *target,
771                                     CORE_ADDR *addr_p)
772 {
773   siginfo_t siginfo;
774   int i, tid;
775   struct aarch64_debug_reg_state *state;
776
777   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
778     return 0;
779
780   /* This must be a hardware breakpoint.  */
781   if (siginfo.si_signo != SIGTRAP
782       || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
783     return 0;
784
785   /* Check if the address matches any watched address.  */
786   state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
787   for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
788     {
789       const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
790       const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
791       const CORE_ADDR addr_watch = state->dr_addr_wp[i];
792
793       if (state->dr_ref_count_wp[i]
794           && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
795           && addr_trap >= addr_watch
796           && addr_trap < addr_watch + len)
797         {
798           *addr_p = addr_trap;
799           return 1;
800         }
801     }
802
803   return 0;
804 }
805
806 /* Implement the "to_stopped_by_watchpoint" target_ops method.  */
807
808 static int
809 aarch64_linux_stopped_by_watchpoint (struct target_ops *ops)
810 {
811   CORE_ADDR addr;
812
813   return aarch64_linux_stopped_data_address (ops, &addr);
814 }
815
816 /* Implement the "to_watchpoint_addr_within_range" target_ops method.  */
817
818 static int
819 aarch64_linux_watchpoint_addr_within_range (struct target_ops *target,
820                                             CORE_ADDR addr,
821                                             CORE_ADDR start, int length)
822 {
823   return start <= addr && start + length - 1 >= addr;
824 }
825
826 /* Define AArch64 maintenance commands.  */
827
828 static void
829 add_show_debug_regs_command (void)
830 {
831   /* A maintenance command to enable printing the internal DRi mirror
832      variables.  */
833   add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
834                            &show_debug_regs, _("\
835 Set whether to show variables that mirror the AArch64 debug registers."), _("\
836 Show whether to show variables that mirror the AArch64 debug registers."), _("\
837 Use \"on\" to enable, \"off\" to disable.\n\
838 If enabled, the debug registers values are shown when GDB inserts\n\
839 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
840 triggers a breakpoint or watchpoint."),
841                            NULL,
842                            NULL,
843                            &maintenance_set_cmdlist,
844                            &maintenance_show_cmdlist);
845 }
846
847 /* -Wmissing-prototypes.  */
848 void _initialize_aarch64_linux_nat (void);
849
850 void
851 _initialize_aarch64_linux_nat (void)
852 {
853   struct target_ops *t;
854
855   /* Fill in the generic GNU/Linux methods.  */
856   t = linux_target ();
857
858   add_show_debug_regs_command ();
859
860   /* Add our register access methods.  */
861   t->to_fetch_registers = aarch64_linux_fetch_inferior_registers;
862   t->to_store_registers = aarch64_linux_store_inferior_registers;
863
864   t->to_read_description = aarch64_linux_read_description;
865
866   t->to_can_use_hw_breakpoint = aarch64_linux_can_use_hw_breakpoint;
867   t->to_insert_hw_breakpoint = aarch64_linux_insert_hw_breakpoint;
868   t->to_remove_hw_breakpoint = aarch64_linux_remove_hw_breakpoint;
869   t->to_region_ok_for_hw_watchpoint =
870     aarch64_linux_region_ok_for_hw_watchpoint;
871   t->to_insert_watchpoint = aarch64_linux_insert_watchpoint;
872   t->to_remove_watchpoint = aarch64_linux_remove_watchpoint;
873   t->to_stopped_by_watchpoint = aarch64_linux_stopped_by_watchpoint;
874   t->to_stopped_data_address = aarch64_linux_stopped_data_address;
875   t->to_watchpoint_addr_within_range =
876     aarch64_linux_watchpoint_addr_within_range;
877
878   /* Override the GNU/Linux inferior startup hook.  */
879   super_post_startup_inferior = t->to_post_startup_inferior;
880   t->to_post_startup_inferior = aarch64_linux_child_post_startup_inferior;
881
882   /* Register the target.  */
883   linux_nat_add_target (t);
884   linux_nat_set_new_thread (t, aarch64_linux_new_thread);
885   linux_nat_set_new_fork (t, aarch64_linux_new_fork);
886   linux_nat_set_forget_process (t, aarch64_forget_process);
887   linux_nat_set_prepare_to_resume (t, aarch64_linux_prepare_to_resume);
888 }