1 // SPDX-License-Identifier: GPL-2.0-only
3 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
4 * using the CPU's debug registers.
6 * Copyright (C) 2012 ARM Limited
7 * Author: Will Deacon <will.deacon@arm.com>
10 #define pr_fmt(fmt) "hw-breakpoint: " fmt
12 #include <linux/compat.h>
13 #include <linux/cpu_pm.h>
14 #include <linux/errno.h>
15 #include <linux/hw_breakpoint.h>
16 #include <linux/kprobes.h>
17 #include <linux/perf_event.h>
18 #include <linux/ptrace.h>
19 #include <linux/smp.h>
20 #include <linux/uaccess.h>
22 #include <asm/current.h>
23 #include <asm/debug-monitors.h>
25 #include <asm/hw_breakpoint.h>
26 #include <asm/traps.h>
27 #include <asm/cputype.h>
28 #include <asm/system_misc.h>
30 /* Breakpoint currently in use for each BRP. */
31 static DEFINE_PER_CPU(struct perf_event
*, bp_on_reg
[ARM_MAX_BRP
]);
33 /* Watchpoint currently in use for each WRP. */
34 static DEFINE_PER_CPU(struct perf_event
*, wp_on_reg
[ARM_MAX_WRP
]);
36 /* Currently stepping a per-CPU kernel breakpoint. */
37 static DEFINE_PER_CPU(int, stepping_kernel_bp
);
39 /* Number of BRP/WRP registers on this CPU. */
40 static int core_num_brps
;
41 static int core_num_wrps
;
43 int hw_breakpoint_slots(int type
)
46 * We can be called early, so don't rely on
47 * our static variables being initialised.
51 return get_num_brps();
53 return get_num_wrps();
55 pr_warn("unknown slot type: %d\n", type
);
60 #define READ_WB_REG_CASE(OFF, N, REG, VAL) \
62 AARCH64_DBG_READ(N, REG, VAL); \
65 #define WRITE_WB_REG_CASE(OFF, N, REG, VAL) \
67 AARCH64_DBG_WRITE(N, REG, VAL); \
70 #define GEN_READ_WB_REG_CASES(OFF, REG, VAL) \
71 READ_WB_REG_CASE(OFF, 0, REG, VAL); \
72 READ_WB_REG_CASE(OFF, 1, REG, VAL); \
73 READ_WB_REG_CASE(OFF, 2, REG, VAL); \
74 READ_WB_REG_CASE(OFF, 3, REG, VAL); \
75 READ_WB_REG_CASE(OFF, 4, REG, VAL); \
76 READ_WB_REG_CASE(OFF, 5, REG, VAL); \
77 READ_WB_REG_CASE(OFF, 6, REG, VAL); \
78 READ_WB_REG_CASE(OFF, 7, REG, VAL); \
79 READ_WB_REG_CASE(OFF, 8, REG, VAL); \
80 READ_WB_REG_CASE(OFF, 9, REG, VAL); \
81 READ_WB_REG_CASE(OFF, 10, REG, VAL); \
82 READ_WB_REG_CASE(OFF, 11, REG, VAL); \
83 READ_WB_REG_CASE(OFF, 12, REG, VAL); \
84 READ_WB_REG_CASE(OFF, 13, REG, VAL); \
85 READ_WB_REG_CASE(OFF, 14, REG, VAL); \
86 READ_WB_REG_CASE(OFF, 15, REG, VAL)
88 #define GEN_WRITE_WB_REG_CASES(OFF, REG, VAL) \
89 WRITE_WB_REG_CASE(OFF, 0, REG, VAL); \
90 WRITE_WB_REG_CASE(OFF, 1, REG, VAL); \
91 WRITE_WB_REG_CASE(OFF, 2, REG, VAL); \
92 WRITE_WB_REG_CASE(OFF, 3, REG, VAL); \
93 WRITE_WB_REG_CASE(OFF, 4, REG, VAL); \
94 WRITE_WB_REG_CASE(OFF, 5, REG, VAL); \
95 WRITE_WB_REG_CASE(OFF, 6, REG, VAL); \
96 WRITE_WB_REG_CASE(OFF, 7, REG, VAL); \
97 WRITE_WB_REG_CASE(OFF, 8, REG, VAL); \
98 WRITE_WB_REG_CASE(OFF, 9, REG, VAL); \
99 WRITE_WB_REG_CASE(OFF, 10, REG, VAL); \
100 WRITE_WB_REG_CASE(OFF, 11, REG, VAL); \
101 WRITE_WB_REG_CASE(OFF, 12, REG, VAL); \
102 WRITE_WB_REG_CASE(OFF, 13, REG, VAL); \
103 WRITE_WB_REG_CASE(OFF, 14, REG, VAL); \
104 WRITE_WB_REG_CASE(OFF, 15, REG, VAL)
106 static u64
read_wb_reg(int reg
, int n
)
111 GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_BVR
, AARCH64_DBG_REG_NAME_BVR
, val
);
112 GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_BCR
, AARCH64_DBG_REG_NAME_BCR
, val
);
113 GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_WVR
, AARCH64_DBG_REG_NAME_WVR
, val
);
114 GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_WCR
, AARCH64_DBG_REG_NAME_WCR
, val
);
116 pr_warn("attempt to read from unknown breakpoint register %d\n", n
);
121 NOKPROBE_SYMBOL(read_wb_reg
);
123 static void write_wb_reg(int reg
, int n
, u64 val
)
126 GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_BVR
, AARCH64_DBG_REG_NAME_BVR
, val
);
127 GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_BCR
, AARCH64_DBG_REG_NAME_BCR
, val
);
128 GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_WVR
, AARCH64_DBG_REG_NAME_WVR
, val
);
129 GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_WCR
, AARCH64_DBG_REG_NAME_WCR
, val
);
131 pr_warn("attempt to write to unknown breakpoint register %d\n", n
);
135 NOKPROBE_SYMBOL(write_wb_reg
);
138 * Convert a breakpoint privilege level to the corresponding exception
141 static enum dbg_active_el
debug_exception_level(int privilege
)
144 case AARCH64_BREAKPOINT_EL0
:
145 return DBG_ACTIVE_EL0
;
146 case AARCH64_BREAKPOINT_EL1
:
147 return DBG_ACTIVE_EL1
;
149 pr_warn("invalid breakpoint privilege level %d\n", privilege
);
153 NOKPROBE_SYMBOL(debug_exception_level
);
155 enum hw_breakpoint_ops
{
156 HW_BREAKPOINT_INSTALL
,
157 HW_BREAKPOINT_UNINSTALL
,
158 HW_BREAKPOINT_RESTORE
161 static int is_compat_bp(struct perf_event
*bp
)
163 struct task_struct
*tsk
= bp
->hw
.target
;
166 * tsk can be NULL for per-cpu (non-ptrace) breakpoints.
167 * In this case, use the native interface, since we don't have
168 * the notion of a "compat CPU" and could end up relying on
169 * deprecated behaviour if we use unaligned watchpoints in
172 return tsk
&& is_compat_thread(task_thread_info(tsk
));
176 * hw_breakpoint_slot_setup - Find and setup a perf slot according to
179 * @slots: pointer to array of slots
180 * @max_slots: max number of slots
181 * @bp: perf_event to setup
182 * @ops: operation to be carried out on the slot
185 * slot index on success
186 * -ENOSPC if no slot is available/matches
187 * -EINVAL on wrong operations parameter
189 static int hw_breakpoint_slot_setup(struct perf_event
**slots
, int max_slots
,
190 struct perf_event
*bp
,
191 enum hw_breakpoint_ops ops
)
194 struct perf_event
**slot
;
196 for (i
= 0; i
< max_slots
; ++i
) {
199 case HW_BREAKPOINT_INSTALL
:
205 case HW_BREAKPOINT_UNINSTALL
:
211 case HW_BREAKPOINT_RESTORE
:
216 pr_warn_once("Unhandled hw breakpoint ops %d\n", ops
);
223 static int hw_breakpoint_control(struct perf_event
*bp
,
224 enum hw_breakpoint_ops ops
)
226 struct arch_hw_breakpoint
*info
= counter_arch_bp(bp
);
227 struct perf_event
**slots
;
228 struct debug_info
*debug_info
= ¤t
->thread
.debug
;
229 int i
, max_slots
, ctrl_reg
, val_reg
, reg_enable
;
230 enum dbg_active_el dbg_el
= debug_exception_level(info
->ctrl
.privilege
);
233 if (info
->ctrl
.type
== ARM_BREAKPOINT_EXECUTE
) {
235 ctrl_reg
= AARCH64_DBG_REG_BCR
;
236 val_reg
= AARCH64_DBG_REG_BVR
;
237 slots
= this_cpu_ptr(bp_on_reg
);
238 max_slots
= core_num_brps
;
239 reg_enable
= !debug_info
->bps_disabled
;
242 ctrl_reg
= AARCH64_DBG_REG_WCR
;
243 val_reg
= AARCH64_DBG_REG_WVR
;
244 slots
= this_cpu_ptr(wp_on_reg
);
245 max_slots
= core_num_wrps
;
246 reg_enable
= !debug_info
->wps_disabled
;
249 i
= hw_breakpoint_slot_setup(slots
, max_slots
, bp
, ops
);
251 if (WARN_ONCE(i
< 0, "Can't find any breakpoint slot"))
255 case HW_BREAKPOINT_INSTALL
:
257 * Ensure debug monitors are enabled at the correct exception
260 enable_debug_monitors(dbg_el
);
262 case HW_BREAKPOINT_RESTORE
:
263 /* Setup the address register. */
264 write_wb_reg(val_reg
, i
, info
->address
);
266 /* Setup the control register. */
267 ctrl
= encode_ctrl_reg(info
->ctrl
);
268 write_wb_reg(ctrl_reg
, i
,
269 reg_enable
? ctrl
| 0x1 : ctrl
& ~0x1);
271 case HW_BREAKPOINT_UNINSTALL
:
272 /* Reset the control register. */
273 write_wb_reg(ctrl_reg
, i
, 0);
276 * Release the debug monitors for the correct exception
279 disable_debug_monitors(dbg_el
);
287 * Install a perf counter breakpoint.
289 int arch_install_hw_breakpoint(struct perf_event
*bp
)
291 return hw_breakpoint_control(bp
, HW_BREAKPOINT_INSTALL
);
294 void arch_uninstall_hw_breakpoint(struct perf_event
*bp
)
296 hw_breakpoint_control(bp
, HW_BREAKPOINT_UNINSTALL
);
299 static int get_hbp_len(u8 hbp_len
)
301 unsigned int len_in_bytes
= 0;
304 case ARM_BREAKPOINT_LEN_1
:
307 case ARM_BREAKPOINT_LEN_2
:
310 case ARM_BREAKPOINT_LEN_3
:
313 case ARM_BREAKPOINT_LEN_4
:
316 case ARM_BREAKPOINT_LEN_5
:
319 case ARM_BREAKPOINT_LEN_6
:
322 case ARM_BREAKPOINT_LEN_7
:
325 case ARM_BREAKPOINT_LEN_8
:
334 * Check whether bp virtual address is in kernel space.
336 int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint
*hw
)
342 len
= get_hbp_len(hw
->ctrl
.len
);
344 return (va
>= TASK_SIZE
) && ((va
+ len
- 1) >= TASK_SIZE
);
348 * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
349 * Hopefully this will disappear when ptrace can bypass the conversion
350 * to generic breakpoint descriptions.
352 int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl
,
353 int *gen_len
, int *gen_type
, int *offset
)
357 case ARM_BREAKPOINT_EXECUTE
:
358 *gen_type
= HW_BREAKPOINT_X
;
360 case ARM_BREAKPOINT_LOAD
:
361 *gen_type
= HW_BREAKPOINT_R
;
363 case ARM_BREAKPOINT_STORE
:
364 *gen_type
= HW_BREAKPOINT_W
;
366 case ARM_BREAKPOINT_LOAD
| ARM_BREAKPOINT_STORE
:
367 *gen_type
= HW_BREAKPOINT_RW
;
375 *offset
= __ffs(ctrl
.len
);
378 switch (ctrl
.len
>> *offset
) {
379 case ARM_BREAKPOINT_LEN_1
:
380 *gen_len
= HW_BREAKPOINT_LEN_1
;
382 case ARM_BREAKPOINT_LEN_2
:
383 *gen_len
= HW_BREAKPOINT_LEN_2
;
385 case ARM_BREAKPOINT_LEN_3
:
386 *gen_len
= HW_BREAKPOINT_LEN_3
;
388 case ARM_BREAKPOINT_LEN_4
:
389 *gen_len
= HW_BREAKPOINT_LEN_4
;
391 case ARM_BREAKPOINT_LEN_5
:
392 *gen_len
= HW_BREAKPOINT_LEN_5
;
394 case ARM_BREAKPOINT_LEN_6
:
395 *gen_len
= HW_BREAKPOINT_LEN_6
;
397 case ARM_BREAKPOINT_LEN_7
:
398 *gen_len
= HW_BREAKPOINT_LEN_7
;
400 case ARM_BREAKPOINT_LEN_8
:
401 *gen_len
= HW_BREAKPOINT_LEN_8
;
411 * Construct an arch_hw_breakpoint from a perf_event.
413 static int arch_build_bp_info(struct perf_event
*bp
,
414 const struct perf_event_attr
*attr
,
415 struct arch_hw_breakpoint
*hw
)
418 switch (attr
->bp_type
) {
419 case HW_BREAKPOINT_X
:
420 hw
->ctrl
.type
= ARM_BREAKPOINT_EXECUTE
;
422 case HW_BREAKPOINT_R
:
423 hw
->ctrl
.type
= ARM_BREAKPOINT_LOAD
;
425 case HW_BREAKPOINT_W
:
426 hw
->ctrl
.type
= ARM_BREAKPOINT_STORE
;
428 case HW_BREAKPOINT_RW
:
429 hw
->ctrl
.type
= ARM_BREAKPOINT_LOAD
| ARM_BREAKPOINT_STORE
;
436 switch (attr
->bp_len
) {
437 case HW_BREAKPOINT_LEN_1
:
438 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_1
;
440 case HW_BREAKPOINT_LEN_2
:
441 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_2
;
443 case HW_BREAKPOINT_LEN_3
:
444 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_3
;
446 case HW_BREAKPOINT_LEN_4
:
447 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_4
;
449 case HW_BREAKPOINT_LEN_5
:
450 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_5
;
452 case HW_BREAKPOINT_LEN_6
:
453 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_6
;
455 case HW_BREAKPOINT_LEN_7
:
456 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_7
;
458 case HW_BREAKPOINT_LEN_8
:
459 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_8
;
466 * On AArch64, we only permit breakpoints of length 4, whereas
467 * AArch32 also requires breakpoints of length 2 for Thumb.
468 * Watchpoints can be of length 1, 2, 4 or 8 bytes.
470 if (hw
->ctrl
.type
== ARM_BREAKPOINT_EXECUTE
) {
471 if (is_compat_bp(bp
)) {
472 if (hw
->ctrl
.len
!= ARM_BREAKPOINT_LEN_2
&&
473 hw
->ctrl
.len
!= ARM_BREAKPOINT_LEN_4
)
475 } else if (hw
->ctrl
.len
!= ARM_BREAKPOINT_LEN_4
) {
477 * FIXME: Some tools (I'm looking at you perf) assume
478 * that breakpoints should be sizeof(long). This
479 * is nonsense. For now, we fix up the parameter
480 * but we should probably return -EINVAL instead.
482 hw
->ctrl
.len
= ARM_BREAKPOINT_LEN_4
;
487 hw
->address
= attr
->bp_addr
;
491 * Note that we disallow combined EL0/EL1 breakpoints because
492 * that would complicate the stepping code.
494 if (arch_check_bp_in_kernelspace(hw
))
495 hw
->ctrl
.privilege
= AARCH64_BREAKPOINT_EL1
;
497 hw
->ctrl
.privilege
= AARCH64_BREAKPOINT_EL0
;
500 hw
->ctrl
.enabled
= !attr
->disabled
;
506 * Validate the arch-specific HW Breakpoint register settings.
508 int hw_breakpoint_arch_parse(struct perf_event
*bp
,
509 const struct perf_event_attr
*attr
,
510 struct arch_hw_breakpoint
*hw
)
513 u64 alignment_mask
, offset
;
515 /* Build the arch_hw_breakpoint. */
516 ret
= arch_build_bp_info(bp
, attr
, hw
);
521 * Check address alignment.
522 * We don't do any clever alignment correction for watchpoints
523 * because using 64-bit unaligned addresses is deprecated for
526 * AArch32 tasks expect some simple alignment fixups, so emulate
529 if (is_compat_bp(bp
)) {
530 if (hw
->ctrl
.len
== ARM_BREAKPOINT_LEN_8
)
531 alignment_mask
= 0x7;
533 alignment_mask
= 0x3;
534 offset
= hw
->address
& alignment_mask
;
541 /* Allow halfword watchpoints and breakpoints. */
542 if (hw
->ctrl
.len
== ARM_BREAKPOINT_LEN_2
)
547 /* Allow single byte watchpoint. */
548 if (hw
->ctrl
.len
== ARM_BREAKPOINT_LEN_1
)
556 if (hw
->ctrl
.type
== ARM_BREAKPOINT_EXECUTE
)
557 alignment_mask
= 0x3;
559 alignment_mask
= 0x7;
560 offset
= hw
->address
& alignment_mask
;
563 hw
->address
&= ~alignment_mask
;
564 hw
->ctrl
.len
<<= offset
;
567 * Disallow per-task kernel breakpoints since these would
568 * complicate the stepping code.
570 if (hw
->ctrl
.privilege
== AARCH64_BREAKPOINT_EL1
&& bp
->hw
.target
)
577 * Enable/disable all of the breakpoints active at the specified
578 * exception level at the register level.
579 * This is used when single-stepping after a breakpoint exception.
581 static void toggle_bp_registers(int reg
, enum dbg_active_el el
, int enable
)
583 int i
, max_slots
, privilege
;
585 struct perf_event
**slots
;
588 case AARCH64_DBG_REG_BCR
:
589 slots
= this_cpu_ptr(bp_on_reg
);
590 max_slots
= core_num_brps
;
592 case AARCH64_DBG_REG_WCR
:
593 slots
= this_cpu_ptr(wp_on_reg
);
594 max_slots
= core_num_wrps
;
600 for (i
= 0; i
< max_slots
; ++i
) {
604 privilege
= counter_arch_bp(slots
[i
])->ctrl
.privilege
;
605 if (debug_exception_level(privilege
) != el
)
608 ctrl
= read_wb_reg(reg
, i
);
613 write_wb_reg(reg
, i
, ctrl
);
616 NOKPROBE_SYMBOL(toggle_bp_registers
);
619 * Debug exception handlers.
621 static int breakpoint_handler(unsigned long unused
, unsigned long esr
,
622 struct pt_regs
*regs
)
624 int i
, step
= 0, *kernel_step
;
627 struct perf_event
*bp
, **slots
;
628 struct debug_info
*debug_info
;
629 struct arch_hw_breakpoint_ctrl ctrl
;
631 slots
= this_cpu_ptr(bp_on_reg
);
632 addr
= instruction_pointer(regs
);
633 debug_info
= ¤t
->thread
.debug
;
635 for (i
= 0; i
< core_num_brps
; ++i
) {
643 /* Check if the breakpoint value matches. */
644 val
= read_wb_reg(AARCH64_DBG_REG_BVR
, i
);
645 if (val
!= (addr
& ~0x3))
648 /* Possible match, check the byte address select to confirm. */
649 ctrl_reg
= read_wb_reg(AARCH64_DBG_REG_BCR
, i
);
650 decode_ctrl_reg(ctrl_reg
, &ctrl
);
651 if (!((1 << (addr
& 0x3)) & ctrl
.len
))
654 counter_arch_bp(bp
)->trigger
= addr
;
655 perf_bp_event(bp
, regs
);
657 /* Do we need to handle the stepping? */
658 if (is_default_overflow_handler(bp
))
667 if (user_mode(regs
)) {
668 debug_info
->bps_disabled
= 1;
669 toggle_bp_registers(AARCH64_DBG_REG_BCR
, DBG_ACTIVE_EL0
, 0);
671 /* If we're already stepping a watchpoint, just return. */
672 if (debug_info
->wps_disabled
)
675 if (test_thread_flag(TIF_SINGLESTEP
))
676 debug_info
->suspended_step
= 1;
678 user_enable_single_step(current
);
680 toggle_bp_registers(AARCH64_DBG_REG_BCR
, DBG_ACTIVE_EL1
, 0);
681 kernel_step
= this_cpu_ptr(&stepping_kernel_bp
);
683 if (*kernel_step
!= ARM_KERNEL_STEP_NONE
)
686 if (kernel_active_single_step()) {
687 *kernel_step
= ARM_KERNEL_STEP_SUSPEND
;
689 *kernel_step
= ARM_KERNEL_STEP_ACTIVE
;
690 kernel_enable_single_step(regs
);
696 NOKPROBE_SYMBOL(breakpoint_handler
);
699 * Arm64 hardware does not always report a watchpoint hit address that matches
700 * one of the watchpoints set. It can also report an address "near" the
701 * watchpoint if a single instruction access both watched and unwatched
702 * addresses. There is no straight-forward way, short of disassembling the
703 * offending instruction, to map that address back to the watchpoint. This
704 * function computes the distance of the memory access from the watchpoint as a
705 * heuristic for the likelihood that a given access triggered the watchpoint.
707 * See Section D2.10.5 "Determining the memory location that caused a Watchpoint
708 * exception" of ARMv8 Architecture Reference Manual for details.
710 * The function returns the distance of the address from the bytes watched by
711 * the watchpoint. In case of an exact match, it returns 0.
713 static u64
get_distance_from_watchpoint(unsigned long addr
, u64 val
,
714 struct arch_hw_breakpoint_ctrl
*ctrl
)
719 addr
= untagged_addr(addr
);
721 lens
= __ffs(ctrl
->len
);
722 lene
= __fls(ctrl
->len
);
725 wp_high
= val
+ lene
;
727 return wp_low
- addr
;
728 else if (addr
> wp_high
)
729 return addr
- wp_high
;
734 static int watchpoint_report(struct perf_event
*wp
, unsigned long addr
,
735 struct pt_regs
*regs
)
737 int step
= is_default_overflow_handler(wp
);
738 struct arch_hw_breakpoint
*info
= counter_arch_bp(wp
);
740 info
->trigger
= addr
;
743 * If we triggered a user watchpoint from a uaccess routine, then
744 * handle the stepping ourselves since userspace really can't help
747 if (!user_mode(regs
) && info
->ctrl
.privilege
== AARCH64_BREAKPOINT_EL0
)
750 perf_bp_event(wp
, regs
);
755 static int watchpoint_handler(unsigned long addr
, unsigned long esr
,
756 struct pt_regs
*regs
)
758 int i
, step
= 0, *kernel_step
, access
, closest_match
= 0;
759 u64 min_dist
= -1, dist
;
762 struct perf_event
*wp
, **slots
;
763 struct debug_info
*debug_info
;
764 struct arch_hw_breakpoint_ctrl ctrl
;
766 slots
= this_cpu_ptr(wp_on_reg
);
767 debug_info
= ¤t
->thread
.debug
;
770 * Find all watchpoints that match the reported address. If no exact
771 * match is found. Attribute the hit to the closest watchpoint.
774 for (i
= 0; i
< core_num_wrps
; ++i
) {
780 * Check that the access type matches.
781 * 0 => load, otherwise => store
783 access
= (esr
& ESR_ELx_WNR
) ? HW_BREAKPOINT_W
:
785 if (!(access
& hw_breakpoint_type(wp
)))
788 /* Check if the watchpoint value and byte select match. */
789 val
= read_wb_reg(AARCH64_DBG_REG_WVR
, i
);
790 ctrl_reg
= read_wb_reg(AARCH64_DBG_REG_WCR
, i
);
791 decode_ctrl_reg(ctrl_reg
, &ctrl
);
792 dist
= get_distance_from_watchpoint(addr
, val
, &ctrl
);
793 if (dist
< min_dist
) {
797 /* Is this an exact match? */
801 step
= watchpoint_report(wp
, addr
, regs
);
804 /* No exact match found? */
805 if (min_dist
> 0 && min_dist
!= -1)
806 step
= watchpoint_report(slots
[closest_match
], addr
, regs
);
814 * We always disable EL0 watchpoints because the kernel can
815 * cause these to fire via an unprivileged access.
817 toggle_bp_registers(AARCH64_DBG_REG_WCR
, DBG_ACTIVE_EL0
, 0);
819 if (user_mode(regs
)) {
820 debug_info
->wps_disabled
= 1;
822 /* If we're already stepping a breakpoint, just return. */
823 if (debug_info
->bps_disabled
)
826 if (test_thread_flag(TIF_SINGLESTEP
))
827 debug_info
->suspended_step
= 1;
829 user_enable_single_step(current
);
831 toggle_bp_registers(AARCH64_DBG_REG_WCR
, DBG_ACTIVE_EL1
, 0);
832 kernel_step
= this_cpu_ptr(&stepping_kernel_bp
);
834 if (*kernel_step
!= ARM_KERNEL_STEP_NONE
)
837 if (kernel_active_single_step()) {
838 *kernel_step
= ARM_KERNEL_STEP_SUSPEND
;
840 *kernel_step
= ARM_KERNEL_STEP_ACTIVE
;
841 kernel_enable_single_step(regs
);
847 NOKPROBE_SYMBOL(watchpoint_handler
);
850 * Handle single-step exception.
852 int reinstall_suspended_bps(struct pt_regs
*regs
)
854 struct debug_info
*debug_info
= ¤t
->thread
.debug
;
855 int handled_exception
= 0, *kernel_step
;
857 kernel_step
= this_cpu_ptr(&stepping_kernel_bp
);
860 * Called from single-step exception handler.
861 * Return 0 if execution can resume, 1 if a SIGTRAP should be
864 if (user_mode(regs
)) {
865 if (debug_info
->bps_disabled
) {
866 debug_info
->bps_disabled
= 0;
867 toggle_bp_registers(AARCH64_DBG_REG_BCR
, DBG_ACTIVE_EL0
, 1);
868 handled_exception
= 1;
871 if (debug_info
->wps_disabled
) {
872 debug_info
->wps_disabled
= 0;
873 toggle_bp_registers(AARCH64_DBG_REG_WCR
, DBG_ACTIVE_EL0
, 1);
874 handled_exception
= 1;
877 if (handled_exception
) {
878 if (debug_info
->suspended_step
) {
879 debug_info
->suspended_step
= 0;
880 /* Allow exception handling to fall-through. */
881 handled_exception
= 0;
883 user_disable_single_step(current
);
886 } else if (*kernel_step
!= ARM_KERNEL_STEP_NONE
) {
887 toggle_bp_registers(AARCH64_DBG_REG_BCR
, DBG_ACTIVE_EL1
, 1);
888 toggle_bp_registers(AARCH64_DBG_REG_WCR
, DBG_ACTIVE_EL1
, 1);
890 if (!debug_info
->wps_disabled
)
891 toggle_bp_registers(AARCH64_DBG_REG_WCR
, DBG_ACTIVE_EL0
, 1);
893 if (*kernel_step
!= ARM_KERNEL_STEP_SUSPEND
) {
894 kernel_disable_single_step();
895 handled_exception
= 1;
897 handled_exception
= 0;
900 *kernel_step
= ARM_KERNEL_STEP_NONE
;
903 return !handled_exception
;
905 NOKPROBE_SYMBOL(reinstall_suspended_bps
);
908 * Context-switcher for restoring suspended breakpoints.
910 void hw_breakpoint_thread_switch(struct task_struct
*next
)
914 * disabled: 0 0 => The usual case, NOTIFY_DONE
915 * 0 1 => Disable the registers
916 * 1 0 => Enable the registers
917 * 1 1 => NOTIFY_DONE. per-task bps will
918 * get taken care of by perf.
921 struct debug_info
*current_debug_info
, *next_debug_info
;
923 current_debug_info
= ¤t
->thread
.debug
;
924 next_debug_info
= &next
->thread
.debug
;
926 /* Update breakpoints. */
927 if (current_debug_info
->bps_disabled
!= next_debug_info
->bps_disabled
)
928 toggle_bp_registers(AARCH64_DBG_REG_BCR
,
930 !next_debug_info
->bps_disabled
);
932 /* Update watchpoints. */
933 if (current_debug_info
->wps_disabled
!= next_debug_info
->wps_disabled
)
934 toggle_bp_registers(AARCH64_DBG_REG_WCR
,
936 !next_debug_info
->wps_disabled
);
940 * CPU initialisation.
942 static int hw_breakpoint_reset(unsigned int cpu
)
945 struct perf_event
**slots
;
947 * When a CPU goes through cold-boot, it does not have any installed
948 * slot, so it is safe to share the same function for restoring and
949 * resetting breakpoints; when a CPU is hotplugged in, it goes
950 * through the slots, which are all empty, hence it just resets control
951 * and value for debug registers.
952 * When this function is triggered on warm-boot through a CPU PM
953 * notifier some slots might be initialized; if so they are
954 * reprogrammed according to the debug slots content.
956 for (slots
= this_cpu_ptr(bp_on_reg
), i
= 0; i
< core_num_brps
; ++i
) {
958 hw_breakpoint_control(slots
[i
], HW_BREAKPOINT_RESTORE
);
960 write_wb_reg(AARCH64_DBG_REG_BCR
, i
, 0UL);
961 write_wb_reg(AARCH64_DBG_REG_BVR
, i
, 0UL);
965 for (slots
= this_cpu_ptr(wp_on_reg
), i
= 0; i
< core_num_wrps
; ++i
) {
967 hw_breakpoint_control(slots
[i
], HW_BREAKPOINT_RESTORE
);
969 write_wb_reg(AARCH64_DBG_REG_WCR
, i
, 0UL);
970 write_wb_reg(AARCH64_DBG_REG_WVR
, i
, 0UL);
978 * One-time initialisation.
980 static int __init
arch_hw_breakpoint_init(void)
984 core_num_brps
= get_num_brps();
985 core_num_wrps
= get_num_wrps();
987 pr_info("found %d breakpoint and %d watchpoint registers.\n",
988 core_num_brps
, core_num_wrps
);
990 /* Register debug fault handlers. */
991 hook_debug_fault_code(DBG_ESR_EVT_HWBP
, breakpoint_handler
, SIGTRAP
,
992 TRAP_HWBKPT
, "hw-breakpoint handler");
993 hook_debug_fault_code(DBG_ESR_EVT_HWWP
, watchpoint_handler
, SIGTRAP
,
994 TRAP_HWBKPT
, "hw-watchpoint handler");
997 * Reset the breakpoint resources. We assume that a halting
998 * debugger will leave the world in a nice state for us.
1000 ret
= cpuhp_setup_state(CPUHP_AP_PERF_ARM_HW_BREAKPOINT_STARTING
,
1001 "perf/arm64/hw_breakpoint:starting",
1002 hw_breakpoint_reset
, NULL
);
1004 pr_err("failed to register CPU hotplug notifier: %d\n", ret
);
1006 /* Register cpu_suspend hw breakpoint restore hook */
1007 cpu_suspend_set_dbg_restorer(hw_breakpoint_reset
);
1011 arch_initcall(arch_hw_breakpoint_init
);
1013 void hw_breakpoint_pmu_read(struct perf_event
*bp
)
1018 * Dummy function to register with die_notifier.
1020 int hw_breakpoint_exceptions_notify(struct notifier_block
*unused
,
1021 unsigned long val
, void *data
)