Make aarch64_notify_debug_reg_change the same on GDB and GDBserver
[deliverable/binutils-gdb.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-hw-point.h"
34
35 #include "elf/external.h"
36 #include "elf/common.h"
37
38 #include "nat/gdb_ptrace.h"
39 #include <sys/utsname.h>
40 #include <asm/ptrace.h>
41
42 #include "gregset.h"
43
44 /* Defines ps_err_e, struct ps_prochandle. */
45 #include "gdb_proc_service.h"
46
47 #ifndef TRAP_HWBKPT
48 #define TRAP_HWBKPT 0x0004
49 #endif
50
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,
54 checkpoints). */
55
56 struct aarch64_process_info
57 {
58 /* Linked list. */
59 struct aarch64_process_info *next;
60
61 /* The process identifier. */
62 pid_t pid;
63
64 /* Copy of aarch64 hardware debug registers. */
65 struct aarch64_debug_reg_state state;
66 };
67
68 static struct aarch64_process_info *aarch64_process_list = NULL;
69
70 /* Find process data for process PID. */
71
72 static struct aarch64_process_info *
73 aarch64_find_process_pid (pid_t pid)
74 {
75 struct aarch64_process_info *proc;
76
77 for (proc = aarch64_process_list; proc; proc = proc->next)
78 if (proc->pid == pid)
79 return proc;
80
81 return NULL;
82 }
83
84 /* Add process data for process PID. Returns newly allocated info
85 object. */
86
87 static struct aarch64_process_info *
88 aarch64_add_process (pid_t pid)
89 {
90 struct aarch64_process_info *proc;
91
92 proc = xcalloc (1, sizeof (*proc));
93 proc->pid = pid;
94
95 proc->next = aarch64_process_list;
96 aarch64_process_list = proc;
97
98 return proc;
99 }
100
101 /* Get data specific info for process PID, creating it if necessary.
102 Never returns NULL. */
103
104 static struct aarch64_process_info *
105 aarch64_process_info_get (pid_t pid)
106 {
107 struct aarch64_process_info *proc;
108
109 proc = aarch64_find_process_pid (pid);
110 if (proc == NULL)
111 proc = aarch64_add_process (pid);
112
113 return proc;
114 }
115
116 /* Called whenever GDB is no longer debugging process PID. It deletes
117 data structures that keep track of debug register state. */
118
119 static void
120 aarch64_forget_process (pid_t pid)
121 {
122 struct aarch64_process_info *proc, **proc_link;
123
124 proc = aarch64_process_list;
125 proc_link = &aarch64_process_list;
126
127 while (proc != NULL)
128 {
129 if (proc->pid == pid)
130 {
131 *proc_link = proc->next;
132
133 xfree (proc);
134 return;
135 }
136
137 proc_link = &proc->next;
138 proc = *proc_link;
139 }
140 }
141
142 /* Get debug registers state for process PID. */
143
144 static struct aarch64_debug_reg_state *
145 aarch64_get_debug_reg_state (pid_t pid)
146 {
147 return &aarch64_process_info_get (pid)->state;
148 }
149
150 struct aarch64_dr_update_callback_param
151 {
152 int is_watchpoint;
153 unsigned int idx;
154 };
155
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. */
162
163 static int
164 debug_reg_change_callback (struct lwp_info *lwp, void *ptr)
165 {
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;
174
175 if (info == NULL)
176 info = lwp->arch_private = XCNEW (struct arch_lwp_info);
177
178 if (show_debug_regs)
179 {
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));
185 }
186
187 dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp
188 : &info->dr_changed_bp;
189 dr_changed = *dr_changed_ptr;
190
191 gdb_assert (idx >= 0
192 && (idx <= (is_watchpoint ? aarch64_num_wp_regs
193 : aarch64_num_bp_regs)));
194
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;
199
200 /* If the lwp isn't stopped, force it to momentarily pause, so
201 we can update its debug registers. */
202 if (!lwp->stopped)
203 linux_stop_lwp (lwp);
204
205 if (show_debug_regs)
206 {
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));
211 }
212
213 /* Continue the iteration. */
214 return 0;
215 }
216
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. */
221
222 void
223 aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state *state,
224 int is_watchpoint, unsigned int idx)
225 {
226 struct aarch64_dr_update_callback_param param;
227 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (current_lwp_ptid ()));
228
229 param.is_watchpoint = is_watchpoint;
230 param.idx = idx;
231
232 iterate_over_lwps (pid_ptid, debug_reg_change_callback, (void *) &param);
233 }
234
235 /* Fill GDB's register array with the general-purpose register values
236 from the current thread. */
237
238 static void
239 fetch_gregs_from_thread (struct regcache *regcache)
240 {
241 int ret, tid;
242 struct gdbarch *gdbarch = get_regcache_arch (regcache);
243 elf_gregset_t regs;
244 struct iovec iovec;
245
246 /* Make sure REGS can hold all registers contents on both aarch64
247 and arm. */
248 gdb_static_assert (sizeof (regs) >= 18 * 4);
249
250 tid = ptid_get_lwp (inferior_ptid);
251
252 iovec.iov_base = &regs;
253 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
254 iovec.iov_len = 18 * 4;
255 else
256 iovec.iov_len = sizeof (regs);
257
258 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
259 if (ret < 0)
260 perror_with_name (_("Unable to fetch general registers."));
261
262 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
263 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, 1);
264 else
265 {
266 int regno;
267
268 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
269 regcache_raw_supply (regcache, regno, &regs[regno - AARCH64_X0_REGNUM]);
270 }
271 }
272
273 /* Store to the current thread the valid general-purpose register
274 values in the GDB's register array. */
275
276 static void
277 store_gregs_to_thread (const struct regcache *regcache)
278 {
279 int ret, tid;
280 elf_gregset_t regs;
281 struct iovec iovec;
282 struct gdbarch *gdbarch = get_regcache_arch (regcache);
283
284 /* Make sure REGS can hold all registers contents on both aarch64
285 and arm. */
286 gdb_static_assert (sizeof (regs) >= 18 * 4);
287 tid = ptid_get_lwp (inferior_ptid);
288
289 iovec.iov_base = &regs;
290 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
291 iovec.iov_len = 18 * 4;
292 else
293 iovec.iov_len = sizeof (regs);
294
295 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
296 if (ret < 0)
297 perror_with_name (_("Unable to fetch general registers."));
298
299 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
300 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, 1);
301 else
302 {
303 int regno;
304
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 &regs[regno - AARCH64_X0_REGNUM]);
309 }
310
311 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
312 if (ret < 0)
313 perror_with_name (_("Unable to store general registers."));
314 }
315
316 /* Fill GDB's register array with the fp/simd register values
317 from the current thread. */
318
319 static void
320 fetch_fpregs_from_thread (struct regcache *regcache)
321 {
322 int ret, tid;
323 elf_fpregset_t regs;
324 struct iovec iovec;
325 struct gdbarch *gdbarch = get_regcache_arch (regcache);
326
327 /* Make sure REGS can hold all VFP registers contents on both aarch64
328 and arm. */
329 gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
330
331 tid = ptid_get_lwp (inferior_ptid);
332
333 iovec.iov_base = &regs;
334
335 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
336 {
337 iovec.iov_len = VFP_REGS_SIZE;
338
339 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
340 if (ret < 0)
341 perror_with_name (_("Unable to fetch VFP registers."));
342
343 aarch32_vfp_regcache_supply (regcache, (gdb_byte *) &regs, 32);
344 }
345 else
346 {
347 int regno;
348
349 iovec.iov_len = sizeof (regs);
350
351 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
352 if (ret < 0)
353 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
354
355 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
356 regcache_raw_supply (regcache, regno,
357 &regs.vregs[regno - AARCH64_V0_REGNUM]);
358
359 regcache_raw_supply (regcache, AARCH64_FPSR_REGNUM, &regs.fpsr);
360 regcache_raw_supply (regcache, AARCH64_FPCR_REGNUM, &regs.fpcr);
361 }
362 }
363
364 /* Store to the current thread the valid fp/simd register
365 values in the GDB's register array. */
366
367 static void
368 store_fpregs_to_thread (const struct regcache *regcache)
369 {
370 int ret, tid;
371 elf_fpregset_t regs;
372 struct iovec iovec;
373 struct gdbarch *gdbarch = get_regcache_arch (regcache);
374
375 /* Make sure REGS can hold all VFP registers contents on both aarch64
376 and arm. */
377 gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
378 tid = ptid_get_lwp (inferior_ptid);
379
380 iovec.iov_base = &regs;
381
382 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
383 {
384 iovec.iov_len = VFP_REGS_SIZE;
385
386 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
387 if (ret < 0)
388 perror_with_name (_("Unable to fetch VFP registers."));
389
390 aarch32_vfp_regcache_collect (regcache, (gdb_byte *) &regs, 32);
391 }
392 else
393 {
394 int regno;
395
396 iovec.iov_len = sizeof (regs);
397
398 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
399 if (ret < 0)
400 perror_with_name (_("Unable to fetch FP/SIMD registers."));
401
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 *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
406
407 if (REG_VALID == regcache_register_status (regcache, AARCH64_FPSR_REGNUM))
408 regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM,
409 (char *) &regs.fpsr);
410 if (REG_VALID == regcache_register_status (regcache, AARCH64_FPCR_REGNUM))
411 regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM,
412 (char *) &regs.fpcr);
413 }
414
415 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
416 {
417 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iovec);
418 if (ret < 0)
419 perror_with_name (_("Unable to store VFP registers."));
420 }
421 else
422 {
423 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
424 if (ret < 0)
425 perror_with_name (_("Unable to store FP/SIMD registers."));
426 }
427 }
428
429 /* Implement the "to_fetch_register" target_ops method. */
430
431 static void
432 aarch64_linux_fetch_inferior_registers (struct target_ops *ops,
433 struct regcache *regcache,
434 int regno)
435 {
436 if (regno == -1)
437 {
438 fetch_gregs_from_thread (regcache);
439 fetch_fpregs_from_thread (regcache);
440 }
441 else if (regno < AARCH64_V0_REGNUM)
442 fetch_gregs_from_thread (regcache);
443 else
444 fetch_fpregs_from_thread (regcache);
445 }
446
447 /* Implement the "to_store_register" target_ops method. */
448
449 static void
450 aarch64_linux_store_inferior_registers (struct target_ops *ops,
451 struct regcache *regcache,
452 int regno)
453 {
454 if (regno == -1)
455 {
456 store_gregs_to_thread (regcache);
457 store_fpregs_to_thread (regcache);
458 }
459 else if (regno < AARCH64_V0_REGNUM)
460 store_gregs_to_thread (regcache);
461 else
462 store_fpregs_to_thread (regcache);
463 }
464
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. */
468
469 void
470 fill_gregset (const struct regcache *regcache,
471 gdb_gregset_t *gregsetp, int regno)
472 {
473 regcache_collect_regset (&aarch64_linux_gregset, regcache,
474 regno, (gdb_byte *) gregsetp,
475 AARCH64_LINUX_SIZEOF_GREGSET);
476 }
477
478 /* Fill GDB's register array with the general-purpose register values
479 in *GREGSETP. */
480
481 void
482 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
483 {
484 regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
485 (const gdb_byte *) gregsetp,
486 AARCH64_LINUX_SIZEOF_GREGSET);
487 }
488
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. */
492
493 void
494 fill_fpregset (const struct regcache *regcache,
495 gdb_fpregset_t *fpregsetp, int regno)
496 {
497 regcache_collect_regset (&aarch64_linux_fpregset, regcache,
498 regno, (gdb_byte *) fpregsetp,
499 AARCH64_LINUX_SIZEOF_FPREGSET);
500 }
501
502 /* Fill GDB's register array with the floating-point register values
503 in *FPREGSETP. */
504
505 void
506 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
507 {
508 regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
509 (const gdb_byte *) fpregsetp,
510 AARCH64_LINUX_SIZEOF_FPREGSET);
511 }
512
513 /* Called when resuming a thread.
514 The hardware debug registers are updated when there is any change. */
515
516 static void
517 aarch64_linux_prepare_to_resume (struct lwp_info *lwp)
518 {
519 struct arch_lwp_info *info = lwp->arch_private;
520
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
523 nothing to do. */
524 if (info == NULL)
525 return;
526
527 if (DR_HAS_CHANGED (info->dr_changed_bp)
528 || DR_HAS_CHANGED (info->dr_changed_wp))
529 {
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));
533
534 if (show_debug_regs)
535 fprintf_unfiltered (gdb_stdlog, "prepare_to_resume thread %d\n", tid);
536
537 /* Watchpoints. */
538 if (DR_HAS_CHANGED (info->dr_changed_wp))
539 {
540 aarch64_linux_set_debug_regs (state, tid, 1);
541 DR_CLEAR_CHANGED (info->dr_changed_wp);
542 }
543
544 /* Breakpoints. */
545 if (DR_HAS_CHANGED (info->dr_changed_bp))
546 {
547 aarch64_linux_set_debug_regs (state, tid, 0);
548 DR_CLEAR_CHANGED (info->dr_changed_bp);
549 }
550 }
551 }
552
553 static void
554 aarch64_linux_new_thread (struct lwp_info *lp)
555 {
556 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
557
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);
562
563 lp->arch_private = info;
564 }
565
566 /* linux_nat_new_fork hook. */
567
568 static void
569 aarch64_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
570 {
571 pid_t parent_pid;
572 struct aarch64_debug_reg_state *parent_state;
573 struct aarch64_debug_reg_state *child_state;
574
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)
578 return;
579
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
584 removed together. */
585
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;
590 }
591 \f
592
593 /* Called by libthread_db. Returns a pointer to the thread local
594 storage (or its descriptor). */
595
596 ps_err_e
597 ps_get_thread_area (const struct ps_prochandle *ph,
598 lwpid_t lwpid, int idx, void **base)
599 {
600 struct iovec iovec;
601 uint64_t reg;
602
603 iovec.iov_base = &reg;
604 iovec.iov_len = sizeof (reg);
605
606 if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0)
607 return PS_ERR;
608
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);
613
614 return PS_OK;
615 }
616 \f
617
618 static void (*super_post_startup_inferior) (struct target_ops *self,
619 ptid_t ptid);
620
621 /* Implement the "to_post_startup_inferior" target_ops method. */
622
623 static void
624 aarch64_linux_child_post_startup_inferior (struct target_ops *self,
625 ptid_t ptid)
626 {
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);
630 }
631
632 extern struct target_desc *tdesc_arm_with_vfpv3;
633 extern struct target_desc *tdesc_arm_with_neon;
634
635 /* Implement the "to_read_description" target_ops method. */
636
637 static const struct target_desc *
638 aarch64_linux_read_description (struct target_ops *ops)
639 {
640 CORE_ADDR at_phent;
641
642 if (target_auxv_search (ops, AT_PHENT, &at_phent) == 1)
643 {
644 if (at_phent == sizeof (Elf64_External_Phdr))
645 return tdesc_aarch64;
646 else
647 {
648 CORE_ADDR arm_hwcap = 0;
649
650 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
651 return ops->beneath->to_read_description (ops->beneath);
652
653 #ifndef COMPAT_HWCAP_VFP
654 #define COMPAT_HWCAP_VFP (1 << 6)
655 #endif
656 #ifndef COMPAT_HWCAP_NEON
657 #define COMPAT_HWCAP_NEON (1 << 12)
658 #endif
659 #ifndef COMPAT_HWCAP_VFPv3
660 #define COMPAT_HWCAP_VFPv3 (1 << 13)
661 #endif
662
663 if (arm_hwcap & COMPAT_HWCAP_VFP)
664 {
665 char *buf;
666 const struct target_desc *result = NULL;
667
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;
672
673 return result;
674 }
675
676 return NULL;
677 }
678 }
679
680 return tdesc_aarch64;
681 }
682
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. */
692
693 static int
694 aarch64_linux_can_use_hw_breakpoint (struct target_ops *self,
695 enum bptype type,
696 int cnt, int othertype)
697 {
698 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
699 || type == bp_access_watchpoint || type == bp_watchpoint)
700 {
701 if (aarch64_num_wp_regs == 0)
702 return 0;
703 }
704 else if (type == bp_hardware_breakpoint)
705 {
706 if (aarch64_num_bp_regs == 0)
707 return 0;
708 }
709 else
710 gdb_assert_not_reached ("unexpected breakpoint type");
711
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. */
718 return 1;
719 }
720
721 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
722 Return 0 on success, -1 on failure. */
723
724 static int
725 aarch64_linux_insert_hw_breakpoint (struct target_ops *self,
726 struct gdbarch *gdbarch,
727 struct bp_target_info *bp_tgt)
728 {
729 int ret;
730 CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
731 const int len = 4;
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));
735
736 if (show_debug_regs)
737 fprintf_unfiltered
738 (gdb_stdlog,
739 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
740 (unsigned long) addr, len);
741
742 ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */, state);
743
744 if (show_debug_regs)
745 {
746 aarch64_show_debug_reg_state (state,
747 "insert_hw_breakpoint", addr, len, type);
748 }
749
750 return ret;
751 }
752
753 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
754 Return 0 on success, -1 on failure. */
755
756 static int
757 aarch64_linux_remove_hw_breakpoint (struct target_ops *self,
758 struct gdbarch *gdbarch,
759 struct bp_target_info *bp_tgt)
760 {
761 int ret;
762 CORE_ADDR addr = bp_tgt->placed_address;
763 const int len = 4;
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));
767
768 if (show_debug_regs)
769 fprintf_unfiltered
770 (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
771 (unsigned long) addr, len);
772
773 ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */, state);
774
775 if (show_debug_regs)
776 {
777 aarch64_show_debug_reg_state (state,
778 "remove_hw_watchpoint", addr, len, type);
779 }
780
781 return ret;
782 }
783
784 /* Implement the "to_insert_watchpoint" target_ops method.
785
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. */
789
790 static int
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)
795 {
796 int ret;
797 struct aarch64_debug_reg_state *state
798 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
799
800 if (show_debug_regs)
801 fprintf_unfiltered (gdb_stdlog,
802 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
803 (unsigned long) addr, len);
804
805 gdb_assert (type != hw_execute);
806
807 ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */, state);
808
809 if (show_debug_regs)
810 {
811 aarch64_show_debug_reg_state (state,
812 "insert_watchpoint", addr, len, type);
813 }
814
815 return ret;
816 }
817
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. */
822
823 static int
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)
828 {
829 int ret;
830 struct aarch64_debug_reg_state *state
831 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
832
833 if (show_debug_regs)
834 fprintf_unfiltered (gdb_stdlog,
835 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
836 (unsigned long) addr, len);
837
838 gdb_assert (type != hw_execute);
839
840 ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */, state);
841
842 if (show_debug_regs)
843 {
844 aarch64_show_debug_reg_state (state,
845 "remove_watchpoint", addr, len, type);
846 }
847
848 return ret;
849 }
850
851 /* Implement the "to_region_ok_for_hw_watchpoint" target_ops method. */
852
853 static int
854 aarch64_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
855 CORE_ADDR addr, int len)
856 {
857 CORE_ADDR aligned_addr;
858
859 /* Can not set watchpoints for zero or negative lengths. */
860 if (len <= 0)
861 return 0;
862
863 /* Must have hardware watchpoint debug register(s). */
864 if (aarch64_num_wp_regs == 0)
865 return 0;
866
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.
871
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. */
876
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
879 < addr + len)
880 return 0;
881
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. */
888 return 1;
889 }
890
891 /* Implement the "to_stopped_data_address" target_ops method. */
892
893 static int
894 aarch64_linux_stopped_data_address (struct target_ops *target,
895 CORE_ADDR *addr_p)
896 {
897 siginfo_t siginfo;
898 int i, tid;
899 struct aarch64_debug_reg_state *state;
900
901 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
902 return 0;
903
904 /* This must be a hardware breakpoint. */
905 if (siginfo.si_signo != SIGTRAP
906 || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
907 return 0;
908
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)
912 {
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];
916
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)
921 {
922 *addr_p = addr_trap;
923 return 1;
924 }
925 }
926
927 return 0;
928 }
929
930 /* Implement the "to_stopped_by_watchpoint" target_ops method. */
931
932 static int
933 aarch64_linux_stopped_by_watchpoint (struct target_ops *ops)
934 {
935 CORE_ADDR addr;
936
937 return aarch64_linux_stopped_data_address (ops, &addr);
938 }
939
940 /* Implement the "to_watchpoint_addr_within_range" target_ops method. */
941
942 static int
943 aarch64_linux_watchpoint_addr_within_range (struct target_ops *target,
944 CORE_ADDR addr,
945 CORE_ADDR start, int length)
946 {
947 return start <= addr && start + length - 1 >= addr;
948 }
949
950 /* Define AArch64 maintenance commands. */
951
952 static void
953 add_show_debug_regs_command (void)
954 {
955 /* A maintenance command to enable printing the internal DRi mirror
956 variables. */
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."),
965 NULL,
966 NULL,
967 &maintenance_set_cmdlist,
968 &maintenance_show_cmdlist);
969 }
970
971 /* -Wmissing-prototypes. */
972 void _initialize_aarch64_linux_nat (void);
973
974 void
975 _initialize_aarch64_linux_nat (void)
976 {
977 struct target_ops *t;
978
979 /* Fill in the generic GNU/Linux methods. */
980 t = linux_target ();
981
982 add_show_debug_regs_command ();
983
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;
987
988 t->to_read_description = aarch64_linux_read_description;
989
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;
1001
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;
1005
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);
1012 }
This page took 0.052935 seconds and 5 git commands to generate.