1 /* GNU/Linux/AArch64 specific low level interface, for the remote server for
4 Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 Contributed by ARM Ltd.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "linux-low.h"
24 #include "elf/common.h"
28 #include <sys/ptrace.h>
29 #include <asm/ptrace.h>
32 #include "gdb_proc_service.h"
34 /* Defined in auto-generated files. */
35 void init_registers_aarch64 (void);
36 extern const struct target_desc
*tdesc_aarch64
;
42 #define AARCH64_X_REGS_NUM 31
43 #define AARCH64_V_REGS_NUM 32
44 #define AARCH64_X0_REGNO 0
45 #define AARCH64_SP_REGNO 31
46 #define AARCH64_PC_REGNO 32
47 #define AARCH64_CPSR_REGNO 33
48 #define AARCH64_V0_REGNO 34
50 #define AARCH64_NUM_REGS (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM)
55 /* These offsets correspond to GET/SETREGSET */
57 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8,
58 8*8, 9*8, 10*8, 11*8, 12*8, 13*8, 14*8, 15*8,
59 16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8,
60 24*8, 25*8, 26*8, 27*8, 28*8,
65 33*8, /* cpsr 4 bytes!*/
67 /* FP register offsets correspond to GET/SETFPREGSET */
68 0*16, 1*16, 2*16, 3*16, 4*16, 5*16, 6*16, 7*16,
69 8*16, 9*16, 10*16, 11*16, 12*16, 13*16, 14*16, 15*16,
70 16*16, 17*16, 18*16, 19*16, 20*16, 21*16, 22*16, 23*16,
71 24*16, 25*16, 26*16, 27*16, 28*16, 29*16, 30*16, 31*16
74 /* Here starts the macro definitions, data structures, and code for
75 the hardware breakpoint and hardware watchpoint support. The
76 following is the abbreviations that are used frequently in the code
83 /* Maximum number of hardware breakpoint and watchpoint registers.
84 Neither of these values may exceed the width of dr_changed_t
87 #define AARCH64_HBP_MAX_NUM 16
88 #define AARCH64_HWP_MAX_NUM 16
90 /* Alignment requirement in bytes of hardware breakpoint and
91 watchpoint address. This is the requirement for the addresses that
92 can be written to the hardware breakpoint/watchpoint value
93 registers. The kernel currently does not do any alignment on
94 addresses when receiving a writing request (via ptrace call) to
95 these debug registers, and it will reject any address that is
97 Some limited support has been provided in this gdbserver port for
98 unaligned watchpoints, so that from a gdb user point of view, an
99 unaligned watchpoint can still be set. This is achieved by
100 minimally enlarging the watched area to meet the alignment
101 requirement, and if necessary, splitting the watchpoint over
102 several hardware watchpoint registers. */
104 #define AARCH64_HBP_ALIGNMENT 4
105 #define AARCH64_HWP_ALIGNMENT 8
107 /* The maximum length of a memory region that can be watched by one
108 hardware watchpoint register. */
110 #define AARCH64_HWP_MAX_LEN_PER_REG 8
112 /* Each bit of a variable of this type is used to indicate whether a
113 hardware breakpoint or watchpoint setting has been changed since
114 the last updating. Bit N corresponds to the Nth hardware
115 breakpoint or watchpoint setting which is managed in
116 aarch64_debug_reg_state. Where N is valid between 0 and the total
117 number of the hardware breakpoint or watchpoint debug registers
118 minus 1. When the bit N is 1, it indicates the corresponding
119 breakpoint or watchpoint setting is changed, and thus the
120 corresponding hardware debug register needs to be updated via the
123 In the per-thread arch-specific data area, we define two such
124 variables for per-thread hardware breakpoint and watchpoint
125 settings respectively.
127 This type is part of the mechanism which helps reduce the number of
128 ptrace calls to the kernel, i.e. avoid asking the kernel to write
129 to the debug registers with unchanged values. */
131 typedef unsigned long long dr_changed_t
;
133 /* Set each of the lower M bits of X to 1; assert X is wide enough. */
135 #define DR_MARK_ALL_CHANGED(x, m) \
138 gdb_assert (sizeof ((x)) * 8 >= (m)); \
139 (x) = (((dr_changed_t)1 << (m)) - 1); \
142 #define DR_MARK_N_CHANGED(x, n) \
145 (x) |= ((dr_changed_t)1 << (n)); \
148 #define DR_CLEAR_CHANGED(x) \
154 #define DR_HAS_CHANGED(x) ((x) != 0)
155 #define DR_N_HAS_CHANGED(x, n) ((x) & ((dr_changed_t)1 << (n)))
157 /* Structure for managing the hardware breakpoint/watchpoint resources.
158 DR_ADDR_* stores the address, DR_CTRL_* stores the control register
159 content, and DR_REF_COUNT_* counts the numbers of references to the
160 corresponding bp/wp, by which way the limited hardware resources
161 are not wasted on duplicated bp/wp settings (though so far gdb has
162 done a good job by not sending duplicated bp/wp requests). */
164 struct aarch64_debug_reg_state
166 /* hardware breakpoint */
167 CORE_ADDR dr_addr_bp
[AARCH64_HBP_MAX_NUM
];
168 unsigned int dr_ctrl_bp
[AARCH64_HBP_MAX_NUM
];
169 unsigned int dr_ref_count_bp
[AARCH64_HBP_MAX_NUM
];
171 /* hardware watchpoint */
172 CORE_ADDR dr_addr_wp
[AARCH64_HWP_MAX_NUM
];
173 unsigned int dr_ctrl_wp
[AARCH64_HWP_MAX_NUM
];
174 unsigned int dr_ref_count_wp
[AARCH64_HWP_MAX_NUM
];
177 /* Per-process arch-specific data we want to keep. */
179 struct arch_process_info
181 /* Hardware breakpoint/watchpoint data.
182 The reason for them to be per-process rather than per-thread is
183 due to the lack of information in the gdbserver environment;
184 gdbserver is not told that whether a requested hardware
185 breakpoint/watchpoint is thread specific or not, so it has to set
186 each hw bp/wp for every thread in the current process. The
187 higher level bp/wp management in gdb will resume a thread if a hw
188 bp/wp trap is not expected for it. Since the hw bp/wp setting is
189 same for each thread, it is reasonable for the data to live here.
191 struct aarch64_debug_reg_state debug_reg_state
;
194 /* Per-thread arch-specific data we want to keep. */
198 /* When bit N is 1, it indicates the Nth hardware breakpoint or
199 watchpoint register pair needs to be updated when the thread is
200 resumed; see aarch64_linux_prepare_to_resume. */
201 dr_changed_t dr_changed_bp
;
202 dr_changed_t dr_changed_wp
;
205 /* Number of hardware breakpoints/watchpoints the target supports.
206 They are initialized with values obtained via the ptrace calls
207 with NT_ARM_HW_BREAK and NT_ARM_HW_WATCH respectively. */
209 static int aarch64_num_bp_regs
;
210 static int aarch64_num_wp_regs
;
213 aarch64_cannot_store_register (int regno
)
215 return regno
>= AARCH64_NUM_REGS
;
219 aarch64_cannot_fetch_register (int regno
)
221 return regno
>= AARCH64_NUM_REGS
;
225 aarch64_fill_gregset (struct regcache
*regcache
, void *buf
)
227 struct user_pt_regs
*regset
= buf
;
230 for (i
= 0; i
< AARCH64_X_REGS_NUM
; i
++)
231 collect_register (regcache
, AARCH64_X0_REGNO
+ i
, ®set
->regs
[i
]);
232 collect_register (regcache
, AARCH64_SP_REGNO
, ®set
->sp
);
233 collect_register (regcache
, AARCH64_PC_REGNO
, ®set
->pc
);
234 collect_register (regcache
, AARCH64_CPSR_REGNO
, ®set
->pstate
);
238 aarch64_store_gregset (struct regcache
*regcache
, const void *buf
)
240 const struct user_pt_regs
*regset
= buf
;
243 for (i
= 0; i
< AARCH64_X_REGS_NUM
; i
++)
244 supply_register (regcache
, AARCH64_X0_REGNO
+ i
, ®set
->regs
[i
]);
245 supply_register (regcache
, AARCH64_SP_REGNO
, ®set
->sp
);
246 supply_register (regcache
, AARCH64_PC_REGNO
, ®set
->pc
);
247 supply_register (regcache
, AARCH64_CPSR_REGNO
, ®set
->pstate
);
251 aarch64_fill_fpregset (struct regcache
*regcache
, void *buf
)
253 struct user_fpsimd_state
*regset
= buf
;
256 for (i
= 0; i
< AARCH64_V_REGS_NUM
; i
++)
257 collect_register (regcache
, AARCH64_V0_REGNO
+ i
, ®set
->vregs
[i
]);
261 aarch64_store_fpregset (struct regcache
*regcache
, const void *buf
)
263 const struct user_fpsimd_state
*regset
= buf
;
266 for (i
= 0; i
< AARCH64_V_REGS_NUM
; i
++)
267 supply_register (regcache
, AARCH64_V0_REGNO
+ i
, ®set
->vregs
[i
]);
270 /* Enable miscellaneous debugging output. The name is historical - it
271 was originally used to debug LinuxThreads support. */
272 extern int debug_threads
;
275 aarch64_get_pc (struct regcache
*regcache
)
279 collect_register_by_name (regcache
, "pc", &pc
);
281 debug_printf ("stop pc is %08lx\n", pc
);
286 aarch64_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
288 unsigned long newpc
= pc
;
289 supply_register_by_name (regcache
, "pc", &newpc
);
292 /* Correct in either endianness. */
294 #define aarch64_breakpoint_len 4
296 static const unsigned long aarch64_breakpoint
= 0x00800011;
299 aarch64_breakpoint_at (CORE_ADDR where
)
303 (*the_target
->read_memory
) (where
, (unsigned char *) &insn
, 4);
304 if (insn
== aarch64_breakpoint
)
310 /* Print the values of the cached breakpoint/watchpoint registers.
311 This is enabled via the "set debug-hw-points" monitor command. */
314 aarch64_show_debug_reg_state (struct aarch64_debug_reg_state
*state
,
315 const char *func
, CORE_ADDR addr
,
316 int len
, enum target_hw_bp_type type
)
320 fprintf (stderr
, "%s", func
);
322 fprintf (stderr
, " (addr=0x%08lx, len=%d, type=%s)",
323 (unsigned long) addr
, len
,
324 type
== hw_write
? "hw-write-watchpoint"
325 : (type
== hw_read
? "hw-read-watchpoint"
326 : (type
== hw_access
? "hw-access-watchpoint"
327 : (type
== hw_execute
? "hw-breakpoint"
329 fprintf (stderr
, ":\n");
331 fprintf (stderr
, "\tBREAKPOINTs:\n");
332 for (i
= 0; i
< aarch64_num_bp_regs
; i
++)
333 fprintf (stderr
, "\tBP%d: addr=0x%s, ctrl=0x%08x, ref.count=%d\n",
334 i
, paddress (state
->dr_addr_bp
[i
]),
335 state
->dr_ctrl_bp
[i
], state
->dr_ref_count_bp
[i
]);
337 fprintf (stderr
, "\tWATCHPOINTs:\n");
338 for (i
= 0; i
< aarch64_num_wp_regs
; i
++)
339 fprintf (stderr
, "\tWP%d: addr=0x%s, ctrl=0x%08x, ref.count=%d\n",
340 i
, paddress (state
->dr_addr_wp
[i
]),
341 state
->dr_ctrl_wp
[i
], state
->dr_ref_count_wp
[i
]);
345 aarch64_init_debug_reg_state (struct aarch64_debug_reg_state
*state
)
349 for (i
= 0; i
< AARCH64_HBP_MAX_NUM
; ++i
)
351 state
->dr_addr_bp
[i
] = 0;
352 state
->dr_ctrl_bp
[i
] = 0;
353 state
->dr_ref_count_bp
[i
] = 0;
356 for (i
= 0; i
< AARCH64_HWP_MAX_NUM
; ++i
)
358 state
->dr_addr_wp
[i
] = 0;
359 state
->dr_ctrl_wp
[i
] = 0;
360 state
->dr_ref_count_wp
[i
] = 0;
364 /* ptrace expects control registers to be formatted as follows:
367 +--------------------------------+----------+------+------+----+
368 | RESERVED (SBZ) | LENGTH | TYPE | PRIV | EN |
369 +--------------------------------+----------+------+------+----+
371 The TYPE field is ignored for breakpoints. */
373 #define DR_CONTROL_ENABLED(ctrl) (((ctrl) & 0x1) == 1)
374 #define DR_CONTROL_LENGTH(ctrl) (((ctrl) >> 5) & 0xff)
376 /* Utility function that returns the length in bytes of a watchpoint
377 according to the content of a hardware debug control register CTRL.
378 Note that the kernel currently only supports the following Byte
379 Address Select (BAS) values: 0x1, 0x3, 0xf and 0xff, which means
380 that for a hardware watchpoint, its valid length can only be 1
381 byte, 2 bytes, 4 bytes or 8 bytes. */
383 static inline unsigned int
384 aarch64_watchpoint_length (unsigned int ctrl
)
386 switch (DR_CONTROL_LENGTH (ctrl
))
401 /* Given the hardware breakpoint or watchpoint type TYPE and its
402 length LEN, return the expected encoding for a hardware
403 breakpoint/watchpoint control register. */
406 aarch64_point_encode_ctrl_reg (enum target_hw_bp_type type
, int len
)
408 unsigned int ctrl
, ttype
;
426 perror_with_name (_("Unrecognized breakpoint/watchpoint type"));
432 ctrl
|= ((1 << len
) - 1) << 5;
434 ctrl
|= (2 << 1) | 1;
439 /* Addresses to be written to the hardware breakpoint and watchpoint
440 value registers need to be aligned; the alignment is 4-byte and
441 8-type respectively. Linux kernel rejects any non-aligned address
442 it receives from the related ptrace call. Furthermore, the kernel
443 currently only supports the following Byte Address Select (BAS)
444 values: 0x1, 0x3, 0xf and 0xff, which means that for a hardware
445 watchpoint to be accepted by the kernel (via ptrace call), its
446 valid length can only be 1 byte, 2 bytes, 4 bytes or 8 bytes.
447 Despite these limitations, the unaligned watchpoint is supported in
450 Return 0 for any non-compliant ADDR and/or LEN; return 1 otherwise. */
453 aarch64_point_is_aligned (int is_watchpoint
, CORE_ADDR addr
, int len
)
455 unsigned int alignment
= is_watchpoint
? AARCH64_HWP_ALIGNMENT
456 : AARCH64_HBP_ALIGNMENT
;
458 if (addr
& (alignment
- 1))
461 if (len
!= 8 && len
!= 4 && len
!= 2 && len
!= 1)
467 /* Given the (potentially unaligned) watchpoint address in ADDR and
468 length in LEN, return the aligned address and aligned length in
469 *ALIGNED_ADDR_P and *ALIGNED_LEN_P, respectively. The returned
470 aligned address and length will be valid to be written to the
471 hardware watchpoint value and control registers. See the comment
472 above aarch64_point_is_aligned for the information about the
473 alignment requirement. The given watchpoint may get truncated if
474 more than one hardware register is needed to cover the watched
475 region. *NEXT_ADDR_P and *NEXT_LEN_P, if non-NULL, will return the
476 address and length of the remaining part of the watchpoint (which
477 can be processed by calling this routine again to generate another
478 aligned address and length pair.
480 Essentially, unaligned watchpoint is achieved by minimally
481 enlarging the watched area to meet the alignment requirement, and
482 if necessary, splitting the watchpoint over several hardware
483 watchpoint registers. The trade-off is that there will be
484 false-positive hits for the read-type or the access-type hardware
485 watchpoints; for the write type, which is more commonly used, there
486 will be no such issues, as the higher-level breakpoint management
487 in gdb always examines the exact watched region for any content
488 change, and transparently resumes a thread from a watchpoint trap
489 if there is no change to the watched region.
491 Another limitation is that because the watched region is enlarged,
492 the watchpoint fault address returned by
493 aarch64_stopped_data_address may be outside of the original watched
494 region, especially when the triggering instruction is accessing a
495 larger region. When the fault address is not within any known
496 range, watchpoints_triggered in gdb will get confused, as the
497 higher-level watchpoint management is only aware of original
498 watched regions, and will think that some unknown watchpoint has
499 been triggered. In such a case, gdb may stop without displaying
500 any detailed information.
502 Once the kernel provides the full support for Byte Address Select
503 (BAS) in the hardware watchpoint control register, these
504 limitations can be largely relaxed with some further work. */
507 aarch64_align_watchpoint (CORE_ADDR addr
, int len
, CORE_ADDR
*aligned_addr_p
,
508 int *aligned_len_p
, CORE_ADDR
*next_addr_p
,
513 CORE_ADDR aligned_addr
;
514 const unsigned int alignment
= AARCH64_HWP_ALIGNMENT
;
515 const unsigned int max_wp_len
= AARCH64_HWP_MAX_LEN_PER_REG
;
517 /* As assumed by the algorithm. */
518 gdb_assert (alignment
== max_wp_len
);
523 /* Address to be put into the hardware watchpoint value register
525 offset
= addr
& (alignment
- 1);
526 aligned_addr
= addr
- offset
;
528 gdb_assert (offset
>= 0 && offset
< alignment
);
529 gdb_assert (aligned_addr
>= 0 && aligned_addr
<= addr
);
530 gdb_assert ((offset
+ len
) > 0);
532 if (offset
+ len
>= max_wp_len
)
534 /* Need more than one watchpoint registers; truncate it at the
535 alignment boundary. */
536 aligned_len
= max_wp_len
;
537 len
-= (max_wp_len
- offset
);
538 addr
+= (max_wp_len
- offset
);
539 gdb_assert ((addr
& (alignment
- 1)) == 0);
543 /* Find the smallest valid length that is large enough to
544 accommodate this watchpoint. */
545 static const unsigned char
546 aligned_len_array
[AARCH64_HWP_MAX_LEN_PER_REG
] =
547 { 1, 2, 4, 4, 8, 8, 8, 8 };
549 aligned_len
= aligned_len_array
[offset
+ len
- 1];
554 if (aligned_addr_p
!= NULL
)
555 *aligned_addr_p
= aligned_addr
;
556 if (aligned_len_p
!= NULL
)
557 *aligned_len_p
= aligned_len
;
558 if (next_addr_p
!= NULL
)
560 if (next_len_p
!= NULL
)
564 /* Call ptrace to set the thread TID's hardware breakpoint/watchpoint
565 registers with data from *STATE. */
568 aarch64_linux_set_debug_regs (const struct aarch64_debug_reg_state
*state
,
569 int tid
, int watchpoint
)
573 struct user_hwdebug_state regs
;
574 const CORE_ADDR
*addr
;
575 const unsigned int *ctrl
;
577 memset (®s
, 0, sizeof (regs
));
578 iov
.iov_base
= ®s
;
579 count
= watchpoint
? aarch64_num_wp_regs
: aarch64_num_bp_regs
;
580 addr
= watchpoint
? state
->dr_addr_wp
: state
->dr_addr_bp
;
581 ctrl
= watchpoint
? state
->dr_ctrl_wp
: state
->dr_ctrl_bp
;
584 iov
.iov_len
= (offsetof (struct user_hwdebug_state
, dbg_regs
[count
- 1])
585 + sizeof (regs
.dbg_regs
[count
- 1]));
587 for (i
= 0; i
< count
; i
++)
589 regs
.dbg_regs
[i
].addr
= addr
[i
];
590 regs
.dbg_regs
[i
].ctrl
= ctrl
[i
];
593 if (ptrace (PTRACE_SETREGSET
, tid
,
594 watchpoint
? NT_ARM_HW_WATCH
: NT_ARM_HW_BREAK
,
596 error (_("Unexpected error setting hardware debug registers"));
599 struct aarch64_dr_update_callback_param
606 /* Callback function which records the information about the change of
607 one hardware breakpoint/watchpoint setting for the thread ENTRY.
608 The information is passed in via PTR.
609 N.B. The actual updating of hardware debug registers is not
610 carried out until the moment the thread is resumed. */
613 debug_reg_change_callback (struct inferior_list_entry
*entry
, void *ptr
)
615 struct thread_info
*thread
= (struct thread_info
*) entry
;
616 struct lwp_info
*lwp
= get_thread_lwp (thread
);
617 struct aarch64_dr_update_callback_param
*param_p
618 = (struct aarch64_dr_update_callback_param
*) ptr
;
619 int pid
= param_p
->pid
;
620 int idx
= param_p
->idx
;
621 int is_watchpoint
= param_p
->is_watchpoint
;
622 struct arch_lwp_info
*info
= lwp
->arch_private
;
623 dr_changed_t
*dr_changed_ptr
;
624 dr_changed_t dr_changed
;
628 fprintf (stderr
, "debug_reg_change_callback: \n\tOn entry:\n");
629 fprintf (stderr
, "\tpid%d, tid: %ld, dr_changed_bp=0x%llx, "
630 "dr_changed_wp=0x%llx\n",
631 pid
, lwpid_of (thread
), info
->dr_changed_bp
,
632 info
->dr_changed_wp
);
635 dr_changed_ptr
= is_watchpoint
? &info
->dr_changed_wp
636 : &info
->dr_changed_bp
;
637 dr_changed
= *dr_changed_ptr
;
639 /* Only update the threads of this process. */
640 if (pid_of (thread
) == pid
)
643 && (idx
<= (is_watchpoint
? aarch64_num_wp_regs
644 : aarch64_num_bp_regs
)));
646 /* The following assertion is not right, as there can be changes
647 that have not been made to the hardware debug registers
648 before new changes overwrite the old ones. This can happen,
649 for instance, when the breakpoint/watchpoint hit one of the
650 threads and the user enters continue; then what happens is:
651 1) all breakpoints/watchpoints are removed for all threads;
652 2) a single step is carried out for the thread that was hit;
653 3) all of the points are inserted again for all threads;
654 4) all threads are resumed.
655 The 2nd step will only affect the one thread in which the
656 bp/wp was hit, which means only that one thread is resumed;
657 remember that the actual updating only happen in
658 aarch64_linux_prepare_to_resume, so other threads remain
659 stopped during the removal and insertion of bp/wp. Therefore
660 for those threads, the change of insertion of the bp/wp
661 overwrites that of the earlier removals. (The situation may
662 be different when bp/wp is steppable, or in the non-stop
664 /* gdb_assert (DR_N_HAS_CHANGED (dr_changed, idx) == 0); */
666 /* The actual update is done later just before resuming the lwp,
667 we just mark that one register pair needs updating. */
668 DR_MARK_N_CHANGED (dr_changed
, idx
);
669 *dr_changed_ptr
= dr_changed
;
671 /* If the lwp isn't stopped, force it to momentarily pause, so
672 we can update its debug registers. */
674 linux_stop_lwp (lwp
);
679 fprintf (stderr
, "\tOn exit:\n\tpid%d, tid: %ld, dr_changed_bp=0x%llx, "
680 "dr_changed_wp=0x%llx\n",
681 pid
, lwpid_of (thread
), info
->dr_changed_bp
,
682 info
->dr_changed_wp
);
688 /* Notify each thread that their IDXth breakpoint/watchpoint register
689 pair needs to be updated. The message will be recorded in each
690 thread's arch-specific data area, the actual updating will be done
691 when the thread is resumed. */
694 aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state
*state
,
695 int is_watchpoint
, unsigned int idx
)
697 struct aarch64_dr_update_callback_param param
;
699 /* Only update the threads of this process. */
700 param
.pid
= pid_of (current_inferior
);
702 param
.is_watchpoint
= is_watchpoint
;
705 find_inferior (&all_threads
, debug_reg_change_callback
, (void *) ¶m
);
709 /* Return the pointer to the debug register state structure in the
710 current process' arch-specific data area. */
712 static struct aarch64_debug_reg_state
*
713 aarch64_get_debug_reg_state ()
715 struct process_info
*proc
;
717 proc
= current_process ();
718 return &proc
->private->arch_private
->debug_reg_state
;
721 /* Record the insertion of one breakpoint/watchpoint, as represented
722 by ADDR and CTRL, in the process' arch-specific data area *STATE. */
725 aarch64_dr_state_insert_one_point (struct aarch64_debug_reg_state
*state
,
726 enum target_hw_bp_type type
,
727 CORE_ADDR addr
, int len
)
729 int i
, idx
, num_regs
, is_watchpoint
;
730 unsigned int ctrl
, *dr_ctrl_p
, *dr_ref_count
;
731 CORE_ADDR
*dr_addr_p
;
733 /* Set up state pointers. */
734 is_watchpoint
= (type
!= hw_execute
);
735 gdb_assert (aarch64_point_is_aligned (is_watchpoint
, addr
, len
));
738 num_regs
= aarch64_num_wp_regs
;
739 dr_addr_p
= state
->dr_addr_wp
;
740 dr_ctrl_p
= state
->dr_ctrl_wp
;
741 dr_ref_count
= state
->dr_ref_count_wp
;
745 num_regs
= aarch64_num_bp_regs
;
746 dr_addr_p
= state
->dr_addr_bp
;
747 dr_ctrl_p
= state
->dr_ctrl_bp
;
748 dr_ref_count
= state
->dr_ref_count_bp
;
751 ctrl
= aarch64_point_encode_ctrl_reg (type
, len
);
753 /* Find an existing or free register in our cache. */
755 for (i
= 0; i
< num_regs
; ++i
)
757 if ((dr_ctrl_p
[i
] & 1) == 0)
759 gdb_assert (dr_ref_count
[i
] == 0);
761 /* no break; continue hunting for an exising one. */
763 else if (dr_addr_p
[i
] == addr
&& dr_ctrl_p
[i
] == ctrl
)
765 gdb_assert (dr_ref_count
[i
] != 0);
775 /* Update our cache. */
776 if ((dr_ctrl_p
[idx
] & 1) == 0)
779 dr_addr_p
[idx
] = addr
;
780 dr_ctrl_p
[idx
] = ctrl
;
781 dr_ref_count
[idx
] = 1;
782 /* Notify the change. */
783 aarch64_notify_debug_reg_change (state
, is_watchpoint
, idx
);
794 /* Record the removal of one breakpoint/watchpoint, as represented by
795 ADDR and CTRL, in the process' arch-specific data area *STATE. */
798 aarch64_dr_state_remove_one_point (struct aarch64_debug_reg_state
*state
,
799 enum target_hw_bp_type type
,
800 CORE_ADDR addr
, int len
)
802 int i
, num_regs
, is_watchpoint
;
803 unsigned int ctrl
, *dr_ctrl_p
, *dr_ref_count
;
804 CORE_ADDR
*dr_addr_p
;
806 /* Set up state pointers. */
807 is_watchpoint
= (type
!= hw_execute
);
808 gdb_assert (aarch64_point_is_aligned (is_watchpoint
, addr
, len
));
811 num_regs
= aarch64_num_wp_regs
;
812 dr_addr_p
= state
->dr_addr_wp
;
813 dr_ctrl_p
= state
->dr_ctrl_wp
;
814 dr_ref_count
= state
->dr_ref_count_wp
;
818 num_regs
= aarch64_num_bp_regs
;
819 dr_addr_p
= state
->dr_addr_bp
;
820 dr_ctrl_p
= state
->dr_ctrl_bp
;
821 dr_ref_count
= state
->dr_ref_count_bp
;
824 ctrl
= aarch64_point_encode_ctrl_reg (type
, len
);
826 /* Find the entry that matches the ADDR and CTRL. */
827 for (i
= 0; i
< num_regs
; ++i
)
828 if (dr_addr_p
[i
] == addr
&& dr_ctrl_p
[i
] == ctrl
)
830 gdb_assert (dr_ref_count
[i
] != 0);
838 /* Clear our cache. */
839 if (--dr_ref_count
[i
] == 0)
841 /* Clear the enable bit. */
845 /* Notify the change. */
846 aarch64_notify_debug_reg_change (state
, is_watchpoint
, i
);
853 aarch64_handle_breakpoint (enum target_hw_bp_type type
, CORE_ADDR addr
,
854 int len
, int is_insert
)
856 struct aarch64_debug_reg_state
*state
;
858 /* The hardware breakpoint on AArch64 should always be 4-byte
860 if (!aarch64_point_is_aligned (0 /* is_watchpoint */ , addr
, len
))
863 state
= aarch64_get_debug_reg_state ();
866 return aarch64_dr_state_insert_one_point (state
, type
, addr
, len
);
868 return aarch64_dr_state_remove_one_point (state
, type
, addr
, len
);
871 /* This is essentially the same as aarch64_handle_breakpoint, apart
872 from that it is an aligned watchpoint to be handled. */
875 aarch64_handle_aligned_watchpoint (enum target_hw_bp_type type
,
876 CORE_ADDR addr
, int len
, int is_insert
)
878 struct aarch64_debug_reg_state
*state
;
880 state
= aarch64_get_debug_reg_state ();
883 return aarch64_dr_state_insert_one_point (state
, type
, addr
, len
);
885 return aarch64_dr_state_remove_one_point (state
, type
, addr
, len
);
888 /* Insert/remove unaligned watchpoint by calling
889 aarch64_align_watchpoint repeatedly until the whole watched region,
890 as represented by ADDR and LEN, has been properly aligned and ready
891 to be written to one or more hardware watchpoint registers.
892 IS_INSERT indicates whether this is an insertion or a deletion.
893 Return 0 if succeed. */
896 aarch64_handle_unaligned_watchpoint (enum target_hw_bp_type type
,
897 CORE_ADDR addr
, int len
, int is_insert
)
899 struct aarch64_debug_reg_state
*state
900 = aarch64_get_debug_reg_state ();
904 CORE_ADDR aligned_addr
;
905 int aligned_len
, ret
;
907 aarch64_align_watchpoint (addr
, len
, &aligned_addr
, &aligned_len
,
911 ret
= aarch64_dr_state_insert_one_point (state
, type
, aligned_addr
,
914 ret
= aarch64_dr_state_remove_one_point (state
, type
, aligned_addr
,
919 "handle_unaligned_watchpoint: is_insert: %d\n"
920 " aligned_addr: 0x%s, aligned_len: %d\n"
921 " next_addr: 0x%s, next_len: %d\n",
922 is_insert
, paddress (aligned_addr
), aligned_len
,
923 paddress (addr
), len
);
933 aarch64_handle_watchpoint (enum target_hw_bp_type type
, CORE_ADDR addr
,
934 int len
, int is_insert
)
936 if (aarch64_point_is_aligned (1 /* is_watchpoint */ , addr
, len
))
937 return aarch64_handle_aligned_watchpoint (type
, addr
, len
, is_insert
);
939 return aarch64_handle_unaligned_watchpoint (type
, addr
, len
, is_insert
);
943 aarch64_supports_z_point_type (char z_type
)
948 case Z_PACKET_WRITE_WP
:
949 case Z_PACKET_READ_WP
:
950 case Z_PACKET_ACCESS_WP
:
953 /* Leave the handling of sw breakpoints with the gdb client. */
958 /* Insert a hardware breakpoint/watchpoint.
959 It actually only records the info of the to-be-inserted bp/wp;
960 the actual insertion will happen when threads are resumed.
963 Return 1 if TYPE is unsupported type;
964 Return -1 if an error occurs. */
967 aarch64_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
968 int len
, struct raw_breakpoint
*bp
)
971 enum target_hw_bp_type targ_type
;
974 fprintf (stderr
, "insert_point on entry (addr=0x%08lx, len=%d)\n",
975 (unsigned long) addr
, len
);
977 /* Determine the type from the raw breakpoint type. */
978 targ_type
= raw_bkpt_type_to_target_hw_bp_type (type
);
980 if (targ_type
!= hw_execute
)
982 aarch64_handle_watchpoint (targ_type
, addr
, len
, 1 /* is_insert */);
985 aarch64_handle_breakpoint (targ_type
, addr
, len
, 1 /* is_insert */);
987 if (show_debug_regs
> 1)
988 aarch64_show_debug_reg_state (aarch64_get_debug_reg_state (),
989 "insert_point", addr
, len
, targ_type
);
994 /* Remove a hardware breakpoint/watchpoint.
995 It actually only records the info of the to-be-removed bp/wp,
996 the actual removal will be done when threads are resumed.
999 Return 1 if TYPE is an unsupported type;
1000 Return -1 if an error occurs. */
1003 aarch64_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
1004 int len
, struct raw_breakpoint
*bp
)
1007 enum target_hw_bp_type targ_type
;
1009 if (show_debug_regs
)
1010 fprintf (stderr
, "remove_point on entry (addr=0x%08lx, len=%d)\n",
1011 (unsigned long) addr
, len
);
1013 /* Determine the type from the raw breakpoint type. */
1014 targ_type
= raw_bkpt_type_to_target_hw_bp_type (type
);
1016 /* Set up state pointers. */
1017 if (targ_type
!= hw_execute
)
1019 aarch64_handle_watchpoint (targ_type
, addr
, len
, 0 /* is_insert */);
1022 aarch64_handle_breakpoint (targ_type
, addr
, len
, 0 /* is_insert */);
1024 if (show_debug_regs
> 1)
1025 aarch64_show_debug_reg_state (aarch64_get_debug_reg_state (),
1026 "remove_point", addr
, len
, targ_type
);
1031 /* Returns the address associated with the watchpoint that hit, if
1032 any; returns 0 otherwise. */
1035 aarch64_stopped_data_address (void)
1039 struct aarch64_debug_reg_state
*state
;
1041 pid
= lwpid_of (current_inferior
);
1043 /* Get the siginfo. */
1044 if (ptrace (PTRACE_GETSIGINFO
, pid
, NULL
, &siginfo
) != 0)
1045 return (CORE_ADDR
) 0;
1047 /* Need to be a hardware breakpoint/watchpoint trap. */
1048 if (siginfo
.si_signo
!= SIGTRAP
1049 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1050 return (CORE_ADDR
) 0;
1052 /* Check if the address matches any watched address. */
1053 state
= aarch64_get_debug_reg_state ();
1054 for (i
= aarch64_num_wp_regs
- 1; i
>= 0; --i
)
1056 const unsigned int len
= aarch64_watchpoint_length (state
->dr_ctrl_wp
[i
]);
1057 const CORE_ADDR addr_trap
= (CORE_ADDR
) siginfo
.si_addr
;
1058 const CORE_ADDR addr_watch
= state
->dr_addr_wp
[i
];
1059 if (state
->dr_ref_count_wp
[i
]
1060 && DR_CONTROL_ENABLED (state
->dr_ctrl_wp
[i
])
1061 && addr_trap
>= addr_watch
1062 && addr_trap
< addr_watch
+ len
)
1066 return (CORE_ADDR
) 0;
1069 /* Returns 1 if target was stopped due to a watchpoint hit, 0
1073 aarch64_stopped_by_watchpoint (void)
1075 if (aarch64_stopped_data_address () != 0)
1081 /* Fetch the thread-local storage pointer for libthread_db. */
1084 ps_get_thread_area (const struct ps_prochandle
*ph
,
1085 lwpid_t lwpid
, int idx
, void **base
)
1090 iovec
.iov_base
= ®
;
1091 iovec
.iov_len
= sizeof (reg
);
1093 if (ptrace (PTRACE_GETREGSET
, lwpid
, NT_ARM_TLS
, &iovec
) != 0)
1096 /* IDX is the bias from the thread pointer to the beginning of the
1097 thread descriptor. It has to be subtracted due to implementation
1098 quirks in libthread_db. */
1099 *base
= (void *) (reg
- idx
);
1104 /* Called when a new process is created. */
1106 static struct arch_process_info
*
1107 aarch64_linux_new_process (void)
1109 struct arch_process_info
*info
= xcalloc (1, sizeof (*info
));
1111 aarch64_init_debug_reg_state (&info
->debug_reg_state
);
1116 /* Called when a new thread is detected. */
1118 static struct arch_lwp_info
*
1119 aarch64_linux_new_thread (void)
1121 struct arch_lwp_info
*info
= xcalloc (1, sizeof (*info
));
1123 /* Mark that all the hardware breakpoint/watchpoint register pairs
1124 for this thread need to be initialized (with data from
1125 aarch_process_info.debug_reg_state). */
1126 DR_MARK_ALL_CHANGED (info
->dr_changed_bp
, aarch64_num_bp_regs
);
1127 DR_MARK_ALL_CHANGED (info
->dr_changed_wp
, aarch64_num_wp_regs
);
1132 /* Called when resuming a thread.
1133 If the debug regs have changed, update the thread's copies. */
1136 aarch64_linux_prepare_to_resume (struct lwp_info
*lwp
)
1138 struct thread_info
*thread
= get_lwp_thread (lwp
);
1139 ptid_t ptid
= ptid_of (thread
);
1140 struct arch_lwp_info
*info
= lwp
->arch_private
;
1142 if (DR_HAS_CHANGED (info
->dr_changed_bp
)
1143 || DR_HAS_CHANGED (info
->dr_changed_wp
))
1145 int tid
= ptid_get_lwp (ptid
);
1146 struct process_info
*proc
= find_process_pid (ptid_get_pid (ptid
));
1147 struct aarch64_debug_reg_state
*state
1148 = &proc
->private->arch_private
->debug_reg_state
;
1150 if (show_debug_regs
)
1151 fprintf (stderr
, "prepare_to_resume thread %ld\n", lwpid_of (thread
));
1154 if (DR_HAS_CHANGED (info
->dr_changed_wp
))
1156 aarch64_linux_set_debug_regs (state
, tid
, 1);
1157 DR_CLEAR_CHANGED (info
->dr_changed_wp
);
1161 if (DR_HAS_CHANGED (info
->dr_changed_bp
))
1163 aarch64_linux_set_debug_regs (state
, tid
, 0);
1164 DR_CLEAR_CHANGED (info
->dr_changed_bp
);
1169 /* ptrace hardware breakpoint resource info is formatted as follows:
1172 +---------------+--------------+---------------+---------------+
1173 | RESERVED | RESERVED | DEBUG_ARCH | NUM_SLOTS |
1174 +---------------+--------------+---------------+---------------+ */
1176 #define AARCH64_DEBUG_NUM_SLOTS(x) ((x) & 0xff)
1177 #define AARCH64_DEBUG_ARCH(x) (((x) >> 8) & 0xff)
1178 #define AARCH64_DEBUG_ARCH_V8 0x6
1181 aarch64_arch_setup (void)
1185 struct user_hwdebug_state dreg_state
;
1187 current_process ()->tdesc
= tdesc_aarch64
;
1189 pid
= lwpid_of (current_inferior
);
1190 iov
.iov_base
= &dreg_state
;
1191 iov
.iov_len
= sizeof (dreg_state
);
1193 /* Get hardware watchpoint register info. */
1194 if (ptrace (PTRACE_GETREGSET
, pid
, NT_ARM_HW_WATCH
, &iov
) == 0
1195 && AARCH64_DEBUG_ARCH (dreg_state
.dbg_info
) == AARCH64_DEBUG_ARCH_V8
)
1197 aarch64_num_wp_regs
= AARCH64_DEBUG_NUM_SLOTS (dreg_state
.dbg_info
);
1198 if (aarch64_num_wp_regs
> AARCH64_HWP_MAX_NUM
)
1200 warning ("Unexpected number of hardware watchpoint registers reported"
1201 " by ptrace, got %d, expected %d.",
1202 aarch64_num_wp_regs
, AARCH64_HWP_MAX_NUM
);
1203 aarch64_num_wp_regs
= AARCH64_HWP_MAX_NUM
;
1208 warning ("Unable to determine the number of hardware watchpoints"
1210 aarch64_num_wp_regs
= 0;
1213 /* Get hardware breakpoint register info. */
1214 if (ptrace (PTRACE_GETREGSET
, pid
, NT_ARM_HW_BREAK
, &iov
) == 0
1215 && AARCH64_DEBUG_ARCH (dreg_state
.dbg_info
) == AARCH64_DEBUG_ARCH_V8
)
1217 aarch64_num_bp_regs
= AARCH64_DEBUG_NUM_SLOTS (dreg_state
.dbg_info
);
1218 if (aarch64_num_bp_regs
> AARCH64_HBP_MAX_NUM
)
1220 warning ("Unexpected number of hardware breakpoint registers reported"
1221 " by ptrace, got %d, expected %d.",
1222 aarch64_num_bp_regs
, AARCH64_HBP_MAX_NUM
);
1223 aarch64_num_bp_regs
= AARCH64_HBP_MAX_NUM
;
1228 warning ("Unable to determine the number of hardware breakpoints"
1230 aarch64_num_bp_regs
= 0;
1234 static struct regset_info aarch64_regsets
[] =
1236 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_PRSTATUS
,
1237 sizeof (struct user_pt_regs
), GENERAL_REGS
,
1238 aarch64_fill_gregset
, aarch64_store_gregset
},
1239 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_FPREGSET
,
1240 sizeof (struct user_fpsimd_state
), FP_REGS
,
1241 aarch64_fill_fpregset
, aarch64_store_fpregset
1243 { 0, 0, 0, -1, -1, NULL
, NULL
}
1246 static struct regsets_info aarch64_regsets_info
=
1248 aarch64_regsets
, /* regsets */
1249 0, /* num_regsets */
1250 NULL
, /* disabled_regsets */
1253 static struct usrregs_info aarch64_usrregs_info
=
1259 static struct regs_info regs_info
=
1261 NULL
, /* regset_bitmap */
1262 &aarch64_usrregs_info
,
1263 &aarch64_regsets_info
,
1266 static const struct regs_info
*
1267 aarch64_regs_info (void)
1272 struct linux_target_ops the_low_target
=
1276 aarch64_cannot_fetch_register
,
1277 aarch64_cannot_store_register
,
1281 (const unsigned char *) &aarch64_breakpoint
,
1282 aarch64_breakpoint_len
,
1285 aarch64_breakpoint_at
,
1286 aarch64_supports_z_point_type
,
1287 aarch64_insert_point
,
1288 aarch64_remove_point
,
1289 aarch64_stopped_by_watchpoint
,
1290 aarch64_stopped_data_address
,
1294 aarch64_linux_new_process
,
1295 aarch64_linux_new_thread
,
1296 aarch64_linux_prepare_to_resume
,
1300 initialize_low_arch (void)
1302 init_registers_aarch64 ();
1304 initialize_regsets_info (&aarch64_regsets_info
);