Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 2 | /* |
| 3 | * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility, |
| 4 | * using the CPU's debug registers. |
| 5 | * |
| 6 | * Copyright (C) 2012 ARM Limited |
| 7 | * Author: Will Deacon <will.deacon@arm.com> |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) "hw-breakpoint: " fmt |
| 11 | |
AKASHI Takahiro | fd92d4a | 2014-04-30 10:51:32 +0100 | [diff] [blame] | 12 | #include <linux/compat.h> |
Lorenzo Pieralisi | 60fc694 | 2013-08-05 15:20:35 +0100 | [diff] [blame] | 13 | #include <linux/cpu_pm.h> |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 14 | #include <linux/errno.h> |
| 15 | #include <linux/hw_breakpoint.h> |
Pratyush Anand | 44b53f6 | 2016-07-08 12:35:49 -0400 | [diff] [blame] | 16 | #include <linux/kprobes.h> |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 17 | #include <linux/perf_event.h> |
| 18 | #include <linux/ptrace.h> |
| 19 | #include <linux/smp.h> |
Will Deacon | 0e17cad | 2017-12-12 11:53:26 +0000 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 21 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 22 | #include <asm/current.h> |
| 23 | #include <asm/debug-monitors.h> |
| 24 | #include <asm/hw_breakpoint.h> |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 25 | #include <asm/traps.h> |
| 26 | #include <asm/cputype.h> |
| 27 | #include <asm/system_misc.h> |
| 28 | |
| 29 | /* Breakpoint currently in use for each BRP. */ |
| 30 | static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]); |
| 31 | |
| 32 | /* Watchpoint currently in use for each WRP. */ |
| 33 | static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]); |
| 34 | |
| 35 | /* Currently stepping a per-CPU kernel breakpoint. */ |
| 36 | static DEFINE_PER_CPU(int, stepping_kernel_bp); |
| 37 | |
| 38 | /* Number of BRP/WRP registers on this CPU. */ |
| 39 | static int core_num_brps; |
| 40 | static int core_num_wrps; |
| 41 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 42 | int hw_breakpoint_slots(int type) |
| 43 | { |
| 44 | /* |
| 45 | * We can be called early, so don't rely on |
| 46 | * our static variables being initialised. |
| 47 | */ |
| 48 | switch (type) { |
| 49 | case TYPE_INST: |
| 50 | return get_num_brps(); |
| 51 | case TYPE_DATA: |
| 52 | return get_num_wrps(); |
| 53 | default: |
Kefeng Wang | a74ec64 | 2019-10-18 11:18:19 +0800 | [diff] [blame] | 54 | pr_warn("unknown slot type: %d\n", type); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #define READ_WB_REG_CASE(OFF, N, REG, VAL) \ |
| 60 | case (OFF + N): \ |
| 61 | AARCH64_DBG_READ(N, REG, VAL); \ |
| 62 | break |
| 63 | |
| 64 | #define WRITE_WB_REG_CASE(OFF, N, REG, VAL) \ |
| 65 | case (OFF + N): \ |
| 66 | AARCH64_DBG_WRITE(N, REG, VAL); \ |
| 67 | break |
| 68 | |
| 69 | #define GEN_READ_WB_REG_CASES(OFF, REG, VAL) \ |
| 70 | READ_WB_REG_CASE(OFF, 0, REG, VAL); \ |
| 71 | READ_WB_REG_CASE(OFF, 1, REG, VAL); \ |
| 72 | READ_WB_REG_CASE(OFF, 2, REG, VAL); \ |
| 73 | READ_WB_REG_CASE(OFF, 3, REG, VAL); \ |
| 74 | READ_WB_REG_CASE(OFF, 4, REG, VAL); \ |
| 75 | READ_WB_REG_CASE(OFF, 5, REG, VAL); \ |
| 76 | READ_WB_REG_CASE(OFF, 6, REG, VAL); \ |
| 77 | READ_WB_REG_CASE(OFF, 7, REG, VAL); \ |
| 78 | READ_WB_REG_CASE(OFF, 8, REG, VAL); \ |
| 79 | READ_WB_REG_CASE(OFF, 9, REG, VAL); \ |
| 80 | READ_WB_REG_CASE(OFF, 10, REG, VAL); \ |
| 81 | READ_WB_REG_CASE(OFF, 11, REG, VAL); \ |
| 82 | READ_WB_REG_CASE(OFF, 12, REG, VAL); \ |
| 83 | READ_WB_REG_CASE(OFF, 13, REG, VAL); \ |
| 84 | READ_WB_REG_CASE(OFF, 14, REG, VAL); \ |
| 85 | READ_WB_REG_CASE(OFF, 15, REG, VAL) |
| 86 | |
| 87 | #define GEN_WRITE_WB_REG_CASES(OFF, REG, VAL) \ |
| 88 | WRITE_WB_REG_CASE(OFF, 0, REG, VAL); \ |
| 89 | WRITE_WB_REG_CASE(OFF, 1, REG, VAL); \ |
| 90 | WRITE_WB_REG_CASE(OFF, 2, REG, VAL); \ |
| 91 | WRITE_WB_REG_CASE(OFF, 3, REG, VAL); \ |
| 92 | WRITE_WB_REG_CASE(OFF, 4, REG, VAL); \ |
| 93 | WRITE_WB_REG_CASE(OFF, 5, REG, VAL); \ |
| 94 | WRITE_WB_REG_CASE(OFF, 6, REG, VAL); \ |
| 95 | WRITE_WB_REG_CASE(OFF, 7, REG, VAL); \ |
| 96 | WRITE_WB_REG_CASE(OFF, 8, REG, VAL); \ |
| 97 | WRITE_WB_REG_CASE(OFF, 9, REG, VAL); \ |
| 98 | WRITE_WB_REG_CASE(OFF, 10, REG, VAL); \ |
| 99 | WRITE_WB_REG_CASE(OFF, 11, REG, VAL); \ |
| 100 | WRITE_WB_REG_CASE(OFF, 12, REG, VAL); \ |
| 101 | WRITE_WB_REG_CASE(OFF, 13, REG, VAL); \ |
| 102 | WRITE_WB_REG_CASE(OFF, 14, REG, VAL); \ |
| 103 | WRITE_WB_REG_CASE(OFF, 15, REG, VAL) |
| 104 | |
| 105 | static u64 read_wb_reg(int reg, int n) |
| 106 | { |
| 107 | u64 val = 0; |
| 108 | |
| 109 | switch (reg + n) { |
| 110 | GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_BVR, AARCH64_DBG_REG_NAME_BVR, val); |
| 111 | GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_BCR, AARCH64_DBG_REG_NAME_BCR, val); |
| 112 | GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_WVR, AARCH64_DBG_REG_NAME_WVR, val); |
| 113 | GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_WCR, AARCH64_DBG_REG_NAME_WCR, val); |
| 114 | default: |
Kefeng Wang | a74ec64 | 2019-10-18 11:18:19 +0800 | [diff] [blame] | 115 | pr_warn("attempt to read from unknown breakpoint register %d\n", n); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return val; |
| 119 | } |
Pratyush Anand | 44b53f6 | 2016-07-08 12:35:49 -0400 | [diff] [blame] | 120 | NOKPROBE_SYMBOL(read_wb_reg); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 121 | |
| 122 | static void write_wb_reg(int reg, int n, u64 val) |
| 123 | { |
| 124 | switch (reg + n) { |
| 125 | GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_BVR, AARCH64_DBG_REG_NAME_BVR, val); |
| 126 | GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_BCR, AARCH64_DBG_REG_NAME_BCR, val); |
| 127 | GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_WVR, AARCH64_DBG_REG_NAME_WVR, val); |
| 128 | GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_WCR, AARCH64_DBG_REG_NAME_WCR, val); |
| 129 | default: |
Kefeng Wang | a74ec64 | 2019-10-18 11:18:19 +0800 | [diff] [blame] | 130 | pr_warn("attempt to write to unknown breakpoint register %d\n", n); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 131 | } |
| 132 | isb(); |
| 133 | } |
Pratyush Anand | 44b53f6 | 2016-07-08 12:35:49 -0400 | [diff] [blame] | 134 | NOKPROBE_SYMBOL(write_wb_reg); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Convert a breakpoint privilege level to the corresponding exception |
| 138 | * level. |
| 139 | */ |
Will Deacon | 6f883d1 | 2015-07-27 18:36:54 +0100 | [diff] [blame] | 140 | static enum dbg_active_el debug_exception_level(int privilege) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 141 | { |
| 142 | switch (privilege) { |
| 143 | case AARCH64_BREAKPOINT_EL0: |
| 144 | return DBG_ACTIVE_EL0; |
| 145 | case AARCH64_BREAKPOINT_EL1: |
| 146 | return DBG_ACTIVE_EL1; |
| 147 | default: |
Kefeng Wang | a74ec64 | 2019-10-18 11:18:19 +0800 | [diff] [blame] | 148 | pr_warn("invalid breakpoint privilege level %d\n", privilege); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 149 | return -EINVAL; |
| 150 | } |
| 151 | } |
Pratyush Anand | 44b53f6 | 2016-07-08 12:35:49 -0400 | [diff] [blame] | 152 | NOKPROBE_SYMBOL(debug_exception_level); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 153 | |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 154 | enum hw_breakpoint_ops { |
| 155 | HW_BREAKPOINT_INSTALL, |
Lorenzo Pieralisi | 60fc694 | 2013-08-05 15:20:35 +0100 | [diff] [blame] | 156 | HW_BREAKPOINT_UNINSTALL, |
| 157 | HW_BREAKPOINT_RESTORE |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 158 | }; |
| 159 | |
Will Deacon | 8f48c06 | 2015-10-07 11:37:36 +0100 | [diff] [blame] | 160 | static int is_compat_bp(struct perf_event *bp) |
| 161 | { |
| 162 | struct task_struct *tsk = bp->hw.target; |
| 163 | |
| 164 | /* |
| 165 | * tsk can be NULL for per-cpu (non-ptrace) breakpoints. |
| 166 | * In this case, use the native interface, since we don't have |
| 167 | * the notion of a "compat CPU" and could end up relying on |
| 168 | * deprecated behaviour if we use unaligned watchpoints in |
| 169 | * AArch64 state. |
| 170 | */ |
| 171 | return tsk && is_compat_thread(task_thread_info(tsk)); |
| 172 | } |
| 173 | |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 174 | /** |
| 175 | * hw_breakpoint_slot_setup - Find and setup a perf slot according to |
| 176 | * operations |
| 177 | * |
| 178 | * @slots: pointer to array of slots |
| 179 | * @max_slots: max number of slots |
| 180 | * @bp: perf_event to setup |
| 181 | * @ops: operation to be carried out on the slot |
| 182 | * |
| 183 | * Return: |
| 184 | * slot index on success |
| 185 | * -ENOSPC if no slot is available/matches |
| 186 | * -EINVAL on wrong operations parameter |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 187 | */ |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 188 | static int hw_breakpoint_slot_setup(struct perf_event **slots, int max_slots, |
| 189 | struct perf_event *bp, |
| 190 | enum hw_breakpoint_ops ops) |
| 191 | { |
| 192 | int i; |
| 193 | struct perf_event **slot; |
| 194 | |
| 195 | for (i = 0; i < max_slots; ++i) { |
| 196 | slot = &slots[i]; |
| 197 | switch (ops) { |
| 198 | case HW_BREAKPOINT_INSTALL: |
| 199 | if (!*slot) { |
| 200 | *slot = bp; |
| 201 | return i; |
| 202 | } |
| 203 | break; |
| 204 | case HW_BREAKPOINT_UNINSTALL: |
| 205 | if (*slot == bp) { |
| 206 | *slot = NULL; |
| 207 | return i; |
| 208 | } |
| 209 | break; |
Lorenzo Pieralisi | 60fc694 | 2013-08-05 15:20:35 +0100 | [diff] [blame] | 210 | case HW_BREAKPOINT_RESTORE: |
| 211 | if (*slot == bp) |
| 212 | return i; |
| 213 | break; |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 214 | default: |
| 215 | pr_warn_once("Unhandled hw breakpoint ops %d\n", ops); |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | } |
| 219 | return -ENOSPC; |
| 220 | } |
| 221 | |
| 222 | static int hw_breakpoint_control(struct perf_event *bp, |
| 223 | enum hw_breakpoint_ops ops) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 224 | { |
| 225 | struct arch_hw_breakpoint *info = counter_arch_bp(bp); |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 226 | struct perf_event **slots; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 227 | struct debug_info *debug_info = ¤t->thread.debug; |
| 228 | int i, max_slots, ctrl_reg, val_reg, reg_enable; |
Will Deacon | 6f883d1 | 2015-07-27 18:36:54 +0100 | [diff] [blame] | 229 | enum dbg_active_el dbg_el = debug_exception_level(info->ctrl.privilege); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 230 | u32 ctrl; |
| 231 | |
| 232 | if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) { |
| 233 | /* Breakpoint */ |
| 234 | ctrl_reg = AARCH64_DBG_REG_BCR; |
| 235 | val_reg = AARCH64_DBG_REG_BVR; |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 236 | slots = this_cpu_ptr(bp_on_reg); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 237 | max_slots = core_num_brps; |
| 238 | reg_enable = !debug_info->bps_disabled; |
| 239 | } else { |
| 240 | /* Watchpoint */ |
| 241 | ctrl_reg = AARCH64_DBG_REG_WCR; |
| 242 | val_reg = AARCH64_DBG_REG_WVR; |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 243 | slots = this_cpu_ptr(wp_on_reg); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 244 | max_slots = core_num_wrps; |
| 245 | reg_enable = !debug_info->wps_disabled; |
| 246 | } |
| 247 | |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 248 | i = hw_breakpoint_slot_setup(slots, max_slots, bp, ops); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 249 | |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 250 | if (WARN_ONCE(i < 0, "Can't find any breakpoint slot")) |
| 251 | return i; |
| 252 | |
| 253 | switch (ops) { |
| 254 | case HW_BREAKPOINT_INSTALL: |
| 255 | /* |
| 256 | * Ensure debug monitors are enabled at the correct exception |
| 257 | * level. |
| 258 | */ |
| 259 | enable_debug_monitors(dbg_el); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 260 | fallthrough; |
Lorenzo Pieralisi | 60fc694 | 2013-08-05 15:20:35 +0100 | [diff] [blame] | 261 | case HW_BREAKPOINT_RESTORE: |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 262 | /* Setup the address register. */ |
| 263 | write_wb_reg(val_reg, i, info->address); |
| 264 | |
| 265 | /* Setup the control register. */ |
| 266 | ctrl = encode_ctrl_reg(info->ctrl); |
| 267 | write_wb_reg(ctrl_reg, i, |
| 268 | reg_enable ? ctrl | 0x1 : ctrl & ~0x1); |
| 269 | break; |
| 270 | case HW_BREAKPOINT_UNINSTALL: |
| 271 | /* Reset the control register. */ |
| 272 | write_wb_reg(ctrl_reg, i, 0); |
| 273 | |
| 274 | /* |
| 275 | * Release the debug monitors for the correct exception |
| 276 | * level. |
| 277 | */ |
| 278 | disable_debug_monitors(dbg_el); |
| 279 | break; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 285 | /* |
| 286 | * Install a perf counter breakpoint. |
| 287 | */ |
| 288 | int arch_install_hw_breakpoint(struct perf_event *bp) |
| 289 | { |
| 290 | return hw_breakpoint_control(bp, HW_BREAKPOINT_INSTALL); |
| 291 | } |
| 292 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 293 | void arch_uninstall_hw_breakpoint(struct perf_event *bp) |
| 294 | { |
Lorenzo Pieralisi | 2f04304 | 2013-08-13 10:45:19 +0100 | [diff] [blame] | 295 | hw_breakpoint_control(bp, HW_BREAKPOINT_UNINSTALL); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static int get_hbp_len(u8 hbp_len) |
| 299 | { |
| 300 | unsigned int len_in_bytes = 0; |
| 301 | |
| 302 | switch (hbp_len) { |
| 303 | case ARM_BREAKPOINT_LEN_1: |
| 304 | len_in_bytes = 1; |
| 305 | break; |
| 306 | case ARM_BREAKPOINT_LEN_2: |
| 307 | len_in_bytes = 2; |
| 308 | break; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 309 | case ARM_BREAKPOINT_LEN_3: |
| 310 | len_in_bytes = 3; |
| 311 | break; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 312 | case ARM_BREAKPOINT_LEN_4: |
| 313 | len_in_bytes = 4; |
| 314 | break; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 315 | case ARM_BREAKPOINT_LEN_5: |
| 316 | len_in_bytes = 5; |
| 317 | break; |
| 318 | case ARM_BREAKPOINT_LEN_6: |
| 319 | len_in_bytes = 6; |
| 320 | break; |
| 321 | case ARM_BREAKPOINT_LEN_7: |
| 322 | len_in_bytes = 7; |
| 323 | break; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 324 | case ARM_BREAKPOINT_LEN_8: |
| 325 | len_in_bytes = 8; |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | return len_in_bytes; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Check whether bp virtual address is in kernel space. |
| 334 | */ |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 335 | int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 336 | { |
| 337 | unsigned int len; |
| 338 | unsigned long va; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 339 | |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 340 | va = hw->address; |
| 341 | len = get_hbp_len(hw->ctrl.len); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 342 | |
| 343 | return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl. |
| 348 | * Hopefully this will disappear when ptrace can bypass the conversion |
| 349 | * to generic breakpoint descriptions. |
| 350 | */ |
| 351 | int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl, |
Pratyush Anand | b08fb18 | 2016-11-14 19:32:43 +0530 | [diff] [blame] | 352 | int *gen_len, int *gen_type, int *offset) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 353 | { |
| 354 | /* Type */ |
| 355 | switch (ctrl.type) { |
| 356 | case ARM_BREAKPOINT_EXECUTE: |
| 357 | *gen_type = HW_BREAKPOINT_X; |
| 358 | break; |
| 359 | case ARM_BREAKPOINT_LOAD: |
| 360 | *gen_type = HW_BREAKPOINT_R; |
| 361 | break; |
| 362 | case ARM_BREAKPOINT_STORE: |
| 363 | *gen_type = HW_BREAKPOINT_W; |
| 364 | break; |
| 365 | case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE: |
| 366 | *gen_type = HW_BREAKPOINT_RW; |
| 367 | break; |
| 368 | default: |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | |
Pratyush Anand | b08fb18 | 2016-11-14 19:32:43 +0530 | [diff] [blame] | 372 | if (!ctrl.len) |
| 373 | return -EINVAL; |
| 374 | *offset = __ffs(ctrl.len); |
| 375 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 376 | /* Len */ |
Pratyush Anand | b08fb18 | 2016-11-14 19:32:43 +0530 | [diff] [blame] | 377 | switch (ctrl.len >> *offset) { |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 378 | case ARM_BREAKPOINT_LEN_1: |
| 379 | *gen_len = HW_BREAKPOINT_LEN_1; |
| 380 | break; |
| 381 | case ARM_BREAKPOINT_LEN_2: |
| 382 | *gen_len = HW_BREAKPOINT_LEN_2; |
| 383 | break; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 384 | case ARM_BREAKPOINT_LEN_3: |
| 385 | *gen_len = HW_BREAKPOINT_LEN_3; |
| 386 | break; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 387 | case ARM_BREAKPOINT_LEN_4: |
| 388 | *gen_len = HW_BREAKPOINT_LEN_4; |
| 389 | break; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 390 | case ARM_BREAKPOINT_LEN_5: |
| 391 | *gen_len = HW_BREAKPOINT_LEN_5; |
| 392 | break; |
| 393 | case ARM_BREAKPOINT_LEN_6: |
| 394 | *gen_len = HW_BREAKPOINT_LEN_6; |
| 395 | break; |
| 396 | case ARM_BREAKPOINT_LEN_7: |
| 397 | *gen_len = HW_BREAKPOINT_LEN_7; |
| 398 | break; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 399 | case ARM_BREAKPOINT_LEN_8: |
| 400 | *gen_len = HW_BREAKPOINT_LEN_8; |
| 401 | break; |
| 402 | default: |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Construct an arch_hw_breakpoint from a perf_event. |
| 411 | */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 412 | static int arch_build_bp_info(struct perf_event *bp, |
| 413 | const struct perf_event_attr *attr, |
| 414 | struct arch_hw_breakpoint *hw) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 415 | { |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 416 | /* Type */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 417 | switch (attr->bp_type) { |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 418 | case HW_BREAKPOINT_X: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 419 | hw->ctrl.type = ARM_BREAKPOINT_EXECUTE; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 420 | break; |
| 421 | case HW_BREAKPOINT_R: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 422 | hw->ctrl.type = ARM_BREAKPOINT_LOAD; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 423 | break; |
| 424 | case HW_BREAKPOINT_W: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 425 | hw->ctrl.type = ARM_BREAKPOINT_STORE; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 426 | break; |
| 427 | case HW_BREAKPOINT_RW: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 428 | hw->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 429 | break; |
| 430 | default: |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | |
| 434 | /* Len */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 435 | switch (attr->bp_len) { |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 436 | case HW_BREAKPOINT_LEN_1: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 437 | hw->ctrl.len = ARM_BREAKPOINT_LEN_1; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 438 | break; |
| 439 | case HW_BREAKPOINT_LEN_2: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 440 | hw->ctrl.len = ARM_BREAKPOINT_LEN_2; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 441 | break; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 442 | case HW_BREAKPOINT_LEN_3: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 443 | hw->ctrl.len = ARM_BREAKPOINT_LEN_3; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 444 | break; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 445 | case HW_BREAKPOINT_LEN_4: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 446 | hw->ctrl.len = ARM_BREAKPOINT_LEN_4; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 447 | break; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 448 | case HW_BREAKPOINT_LEN_5: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 449 | hw->ctrl.len = ARM_BREAKPOINT_LEN_5; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 450 | break; |
| 451 | case HW_BREAKPOINT_LEN_6: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 452 | hw->ctrl.len = ARM_BREAKPOINT_LEN_6; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 453 | break; |
| 454 | case HW_BREAKPOINT_LEN_7: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 455 | hw->ctrl.len = ARM_BREAKPOINT_LEN_7; |
Pratyush Anand | 0ddb8e0 | 2016-11-14 19:32:45 +0530 | [diff] [blame] | 456 | break; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 457 | case HW_BREAKPOINT_LEN_8: |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 458 | hw->ctrl.len = ARM_BREAKPOINT_LEN_8; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 459 | break; |
| 460 | default: |
| 461 | return -EINVAL; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * On AArch64, we only permit breakpoints of length 4, whereas |
| 466 | * AArch32 also requires breakpoints of length 2 for Thumb. |
| 467 | * Watchpoints can be of length 1, 2, 4 or 8 bytes. |
| 468 | */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 469 | if (hw->ctrl.type == ARM_BREAKPOINT_EXECUTE) { |
Will Deacon | 8f48c06 | 2015-10-07 11:37:36 +0100 | [diff] [blame] | 470 | if (is_compat_bp(bp)) { |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 471 | if (hw->ctrl.len != ARM_BREAKPOINT_LEN_2 && |
| 472 | hw->ctrl.len != ARM_BREAKPOINT_LEN_4) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 473 | return -EINVAL; |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 474 | } else if (hw->ctrl.len != ARM_BREAKPOINT_LEN_4) { |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 475 | /* |
| 476 | * FIXME: Some tools (I'm looking at you perf) assume |
| 477 | * that breakpoints should be sizeof(long). This |
| 478 | * is nonsense. For now, we fix up the parameter |
| 479 | * but we should probably return -EINVAL instead. |
| 480 | */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 481 | hw->ctrl.len = ARM_BREAKPOINT_LEN_4; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | /* Address */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 486 | hw->address = attr->bp_addr; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 487 | |
| 488 | /* |
| 489 | * Privilege |
| 490 | * Note that we disallow combined EL0/EL1 breakpoints because |
| 491 | * that would complicate the stepping code. |
| 492 | */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 493 | if (arch_check_bp_in_kernelspace(hw)) |
| 494 | hw->ctrl.privilege = AARCH64_BREAKPOINT_EL1; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 495 | else |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 496 | hw->ctrl.privilege = AARCH64_BREAKPOINT_EL0; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 497 | |
| 498 | /* Enabled? */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 499 | hw->ctrl.enabled = !attr->disabled; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Validate the arch-specific HW Breakpoint register settings. |
| 506 | */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 507 | int hw_breakpoint_arch_parse(struct perf_event *bp, |
| 508 | const struct perf_event_attr *attr, |
| 509 | struct arch_hw_breakpoint *hw) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 510 | { |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 511 | int ret; |
| 512 | u64 alignment_mask, offset; |
| 513 | |
| 514 | /* Build the arch_hw_breakpoint. */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 515 | ret = arch_build_bp_info(bp, attr, hw); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 516 | if (ret) |
| 517 | return ret; |
| 518 | |
| 519 | /* |
| 520 | * Check address alignment. |
| 521 | * We don't do any clever alignment correction for watchpoints |
| 522 | * because using 64-bit unaligned addresses is deprecated for |
| 523 | * AArch64. |
| 524 | * |
| 525 | * AArch32 tasks expect some simple alignment fixups, so emulate |
| 526 | * that here. |
| 527 | */ |
Will Deacon | 8f48c06 | 2015-10-07 11:37:36 +0100 | [diff] [blame] | 528 | if (is_compat_bp(bp)) { |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 529 | if (hw->ctrl.len == ARM_BREAKPOINT_LEN_8) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 530 | alignment_mask = 0x7; |
| 531 | else |
| 532 | alignment_mask = 0x3; |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 533 | offset = hw->address & alignment_mask; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 534 | switch (offset) { |
| 535 | case 0: |
| 536 | /* Aligned */ |
| 537 | break; |
| 538 | case 1: |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 539 | case 2: |
| 540 | /* Allow halfword watchpoints and breakpoints. */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 541 | if (hw->ctrl.len == ARM_BREAKPOINT_LEN_2) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 542 | break; |
Will Deacon | 75a382f | 2019-07-29 11:39:45 +0100 | [diff] [blame] | 543 | |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 544 | fallthrough; |
Will Deacon | 849adec | 2019-07-29 11:06:17 +0100 | [diff] [blame] | 545 | case 3: |
| 546 | /* Allow single byte watchpoint. */ |
| 547 | if (hw->ctrl.len == ARM_BREAKPOINT_LEN_1) |
| 548 | break; |
Will Deacon | 75a382f | 2019-07-29 11:39:45 +0100 | [diff] [blame] | 549 | |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 550 | fallthrough; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 551 | default: |
| 552 | return -EINVAL; |
| 553 | } |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 554 | } else { |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 555 | if (hw->ctrl.type == ARM_BREAKPOINT_EXECUTE) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 556 | alignment_mask = 0x3; |
| 557 | else |
| 558 | alignment_mask = 0x7; |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 559 | offset = hw->address & alignment_mask; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 562 | hw->address &= ~alignment_mask; |
| 563 | hw->ctrl.len <<= offset; |
Pratyush Anand | b08fb18 | 2016-11-14 19:32:43 +0530 | [diff] [blame] | 564 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 565 | /* |
| 566 | * Disallow per-task kernel breakpoints since these would |
| 567 | * complicate the stepping code. |
| 568 | */ |
Frederic Weisbecker | 8c44975 | 2018-06-26 04:58:53 +0200 | [diff] [blame] | 569 | if (hw->ctrl.privilege == AARCH64_BREAKPOINT_EL1 && bp->hw.target) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 570 | return -EINVAL; |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Enable/disable all of the breakpoints active at the specified |
| 577 | * exception level at the register level. |
| 578 | * This is used when single-stepping after a breakpoint exception. |
| 579 | */ |
Will Deacon | 6f883d1 | 2015-07-27 18:36:54 +0100 | [diff] [blame] | 580 | static void toggle_bp_registers(int reg, enum dbg_active_el el, int enable) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 581 | { |
| 582 | int i, max_slots, privilege; |
| 583 | u32 ctrl; |
| 584 | struct perf_event **slots; |
| 585 | |
| 586 | switch (reg) { |
| 587 | case AARCH64_DBG_REG_BCR: |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 588 | slots = this_cpu_ptr(bp_on_reg); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 589 | max_slots = core_num_brps; |
| 590 | break; |
| 591 | case AARCH64_DBG_REG_WCR: |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 592 | slots = this_cpu_ptr(wp_on_reg); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 593 | max_slots = core_num_wrps; |
| 594 | break; |
| 595 | default: |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | for (i = 0; i < max_slots; ++i) { |
| 600 | if (!slots[i]) |
| 601 | continue; |
| 602 | |
| 603 | privilege = counter_arch_bp(slots[i])->ctrl.privilege; |
| 604 | if (debug_exception_level(privilege) != el) |
| 605 | continue; |
| 606 | |
| 607 | ctrl = read_wb_reg(reg, i); |
| 608 | if (enable) |
| 609 | ctrl |= 0x1; |
| 610 | else |
| 611 | ctrl &= ~0x1; |
| 612 | write_wb_reg(reg, i, ctrl); |
| 613 | } |
| 614 | } |
Pratyush Anand | 44b53f6 | 2016-07-08 12:35:49 -0400 | [diff] [blame] | 615 | NOKPROBE_SYMBOL(toggle_bp_registers); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * Debug exception handlers. |
| 619 | */ |
| 620 | static int breakpoint_handler(unsigned long unused, unsigned int esr, |
| 621 | struct pt_regs *regs) |
| 622 | { |
| 623 | int i, step = 0, *kernel_step; |
| 624 | u32 ctrl_reg; |
| 625 | u64 addr, val; |
| 626 | struct perf_event *bp, **slots; |
| 627 | struct debug_info *debug_info; |
| 628 | struct arch_hw_breakpoint_ctrl ctrl; |
| 629 | |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 630 | slots = this_cpu_ptr(bp_on_reg); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 631 | addr = instruction_pointer(regs); |
| 632 | debug_info = ¤t->thread.debug; |
| 633 | |
| 634 | for (i = 0; i < core_num_brps; ++i) { |
| 635 | rcu_read_lock(); |
| 636 | |
| 637 | bp = slots[i]; |
| 638 | |
| 639 | if (bp == NULL) |
| 640 | goto unlock; |
| 641 | |
| 642 | /* Check if the breakpoint value matches. */ |
| 643 | val = read_wb_reg(AARCH64_DBG_REG_BVR, i); |
| 644 | if (val != (addr & ~0x3)) |
| 645 | goto unlock; |
| 646 | |
| 647 | /* Possible match, check the byte address select to confirm. */ |
| 648 | ctrl_reg = read_wb_reg(AARCH64_DBG_REG_BCR, i); |
| 649 | decode_ctrl_reg(ctrl_reg, &ctrl); |
| 650 | if (!((1 << (addr & 0x3)) & ctrl.len)) |
| 651 | goto unlock; |
| 652 | |
| 653 | counter_arch_bp(bp)->trigger = addr; |
| 654 | perf_bp_event(bp, regs); |
| 655 | |
| 656 | /* Do we need to handle the stepping? */ |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 657 | if (is_default_overflow_handler(bp)) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 658 | step = 1; |
| 659 | unlock: |
| 660 | rcu_read_unlock(); |
| 661 | } |
| 662 | |
| 663 | if (!step) |
| 664 | return 0; |
| 665 | |
| 666 | if (user_mode(regs)) { |
| 667 | debug_info->bps_disabled = 1; |
| 668 | toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL0, 0); |
| 669 | |
| 670 | /* If we're already stepping a watchpoint, just return. */ |
| 671 | if (debug_info->wps_disabled) |
| 672 | return 0; |
| 673 | |
| 674 | if (test_thread_flag(TIF_SINGLESTEP)) |
| 675 | debug_info->suspended_step = 1; |
| 676 | else |
| 677 | user_enable_single_step(current); |
| 678 | } else { |
| 679 | toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL1, 0); |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 680 | kernel_step = this_cpu_ptr(&stepping_kernel_bp); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 681 | |
| 682 | if (*kernel_step != ARM_KERNEL_STEP_NONE) |
| 683 | return 0; |
| 684 | |
| 685 | if (kernel_active_single_step()) { |
| 686 | *kernel_step = ARM_KERNEL_STEP_SUSPEND; |
| 687 | } else { |
| 688 | *kernel_step = ARM_KERNEL_STEP_ACTIVE; |
| 689 | kernel_enable_single_step(regs); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |
Pratyush Anand | 44b53f6 | 2016-07-08 12:35:49 -0400 | [diff] [blame] | 695 | NOKPROBE_SYMBOL(breakpoint_handler); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 696 | |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 697 | /* |
| 698 | * Arm64 hardware does not always report a watchpoint hit address that matches |
| 699 | * one of the watchpoints set. It can also report an address "near" the |
| 700 | * watchpoint if a single instruction access both watched and unwatched |
| 701 | * addresses. There is no straight-forward way, short of disassembling the |
| 702 | * offending instruction, to map that address back to the watchpoint. This |
| 703 | * function computes the distance of the memory access from the watchpoint as a |
| 704 | * heuristic for the likelyhood that a given access triggered the watchpoint. |
| 705 | * |
| 706 | * See Section D2.10.5 "Determining the memory location that caused a Watchpoint |
| 707 | * exception" of ARMv8 Architecture Reference Manual for details. |
| 708 | * |
| 709 | * The function returns the distance of the address from the bytes watched by |
| 710 | * the watchpoint. In case of an exact match, it returns 0. |
| 711 | */ |
| 712 | static u64 get_distance_from_watchpoint(unsigned long addr, u64 val, |
| 713 | struct arch_hw_breakpoint_ctrl *ctrl) |
| 714 | { |
| 715 | u64 wp_low, wp_high; |
| 716 | u32 lens, lene; |
| 717 | |
Kristina Martsenko | 7dcd9dd8 | 2017-05-03 16:37:46 +0100 | [diff] [blame] | 718 | addr = untagged_addr(addr); |
| 719 | |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 720 | lens = __ffs(ctrl->len); |
| 721 | lene = __fls(ctrl->len); |
| 722 | |
| 723 | wp_low = val + lens; |
| 724 | wp_high = val + lene; |
| 725 | if (addr < wp_low) |
| 726 | return wp_low - addr; |
| 727 | else if (addr > wp_high) |
| 728 | return addr - wp_high; |
| 729 | else |
| 730 | return 0; |
| 731 | } |
| 732 | |
Will Deacon | 24ebec2 | 2020-05-29 14:12:18 +0100 | [diff] [blame] | 733 | static int watchpoint_report(struct perf_event *wp, unsigned long addr, |
| 734 | struct pt_regs *regs) |
| 735 | { |
| 736 | int step = is_default_overflow_handler(wp); |
| 737 | struct arch_hw_breakpoint *info = counter_arch_bp(wp); |
| 738 | |
| 739 | info->trigger = addr; |
| 740 | |
| 741 | /* |
| 742 | * If we triggered a user watchpoint from a uaccess routine, then |
| 743 | * handle the stepping ourselves since userspace really can't help |
| 744 | * us with this. |
| 745 | */ |
| 746 | if (!user_mode(regs) && info->ctrl.privilege == AARCH64_BREAKPOINT_EL0) |
| 747 | step = 1; |
| 748 | else |
| 749 | perf_bp_event(wp, regs); |
| 750 | |
| 751 | return step; |
| 752 | } |
| 753 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 754 | static int watchpoint_handler(unsigned long addr, unsigned int esr, |
| 755 | struct pt_regs *regs) |
| 756 | { |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 757 | int i, step = 0, *kernel_step, access, closest_match = 0; |
| 758 | u64 min_dist = -1, dist; |
| 759 | u32 ctrl_reg; |
Pratyush Anand | b08fb18 | 2016-11-14 19:32:43 +0530 | [diff] [blame] | 760 | u64 val; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 761 | struct perf_event *wp, **slots; |
| 762 | struct debug_info *debug_info; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 763 | struct arch_hw_breakpoint_ctrl ctrl; |
| 764 | |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 765 | slots = this_cpu_ptr(wp_on_reg); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 766 | debug_info = ¤t->thread.debug; |
| 767 | |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 768 | /* |
| 769 | * Find all watchpoints that match the reported address. If no exact |
| 770 | * match is found. Attribute the hit to the closest watchpoint. |
| 771 | */ |
| 772 | rcu_read_lock(); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 773 | for (i = 0; i < core_num_wrps; ++i) { |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 774 | wp = slots[i]; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 775 | if (wp == NULL) |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 776 | continue; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 777 | |
| 778 | /* |
| 779 | * Check that the access type matches. |
| 780 | * 0 => load, otherwise => store |
| 781 | */ |
| 782 | access = (esr & AARCH64_ESR_ACCESS_MASK) ? HW_BREAKPOINT_W : |
| 783 | HW_BREAKPOINT_R; |
| 784 | if (!(access & hw_breakpoint_type(wp))) |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 785 | continue; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 786 | |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 787 | /* Check if the watchpoint value and byte select match. */ |
| 788 | val = read_wb_reg(AARCH64_DBG_REG_WVR, i); |
| 789 | ctrl_reg = read_wb_reg(AARCH64_DBG_REG_WCR, i); |
| 790 | decode_ctrl_reg(ctrl_reg, &ctrl); |
| 791 | dist = get_distance_from_watchpoint(addr, val, &ctrl); |
| 792 | if (dist < min_dist) { |
| 793 | min_dist = dist; |
| 794 | closest_match = i; |
| 795 | } |
| 796 | /* Is this an exact match? */ |
| 797 | if (dist != 0) |
| 798 | continue; |
| 799 | |
Will Deacon | 24ebec2 | 2020-05-29 14:12:18 +0100 | [diff] [blame] | 800 | step = watchpoint_report(wp, addr, regs); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 801 | } |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 802 | |
Will Deacon | 24ebec2 | 2020-05-29 14:12:18 +0100 | [diff] [blame] | 803 | /* No exact match found? */ |
| 804 | if (min_dist > 0 && min_dist != -1) |
| 805 | step = watchpoint_report(slots[closest_match], addr, regs); |
| 806 | |
Pavel Labath | fdfeff0 | 2016-11-14 19:32:44 +0530 | [diff] [blame] | 807 | rcu_read_unlock(); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 808 | |
| 809 | if (!step) |
| 810 | return 0; |
| 811 | |
| 812 | /* |
| 813 | * We always disable EL0 watchpoints because the kernel can |
| 814 | * cause these to fire via an unprivileged access. |
| 815 | */ |
| 816 | toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 0); |
| 817 | |
| 818 | if (user_mode(regs)) { |
| 819 | debug_info->wps_disabled = 1; |
| 820 | |
| 821 | /* If we're already stepping a breakpoint, just return. */ |
| 822 | if (debug_info->bps_disabled) |
| 823 | return 0; |
| 824 | |
| 825 | if (test_thread_flag(TIF_SINGLESTEP)) |
| 826 | debug_info->suspended_step = 1; |
| 827 | else |
| 828 | user_enable_single_step(current); |
| 829 | } else { |
| 830 | toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL1, 0); |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 831 | kernel_step = this_cpu_ptr(&stepping_kernel_bp); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 832 | |
| 833 | if (*kernel_step != ARM_KERNEL_STEP_NONE) |
| 834 | return 0; |
| 835 | |
| 836 | if (kernel_active_single_step()) { |
| 837 | *kernel_step = ARM_KERNEL_STEP_SUSPEND; |
| 838 | } else { |
| 839 | *kernel_step = ARM_KERNEL_STEP_ACTIVE; |
| 840 | kernel_enable_single_step(regs); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | return 0; |
| 845 | } |
Pratyush Anand | 44b53f6 | 2016-07-08 12:35:49 -0400 | [diff] [blame] | 846 | NOKPROBE_SYMBOL(watchpoint_handler); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 847 | |
| 848 | /* |
| 849 | * Handle single-step exception. |
| 850 | */ |
| 851 | int reinstall_suspended_bps(struct pt_regs *regs) |
| 852 | { |
| 853 | struct debug_info *debug_info = ¤t->thread.debug; |
| 854 | int handled_exception = 0, *kernel_step; |
| 855 | |
Christoph Lameter | 1436c1a | 2013-10-21 13:17:08 +0100 | [diff] [blame] | 856 | kernel_step = this_cpu_ptr(&stepping_kernel_bp); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 857 | |
| 858 | /* |
| 859 | * Called from single-step exception handler. |
| 860 | * Return 0 if execution can resume, 1 if a SIGTRAP should be |
| 861 | * reported. |
| 862 | */ |
| 863 | if (user_mode(regs)) { |
| 864 | if (debug_info->bps_disabled) { |
| 865 | debug_info->bps_disabled = 0; |
| 866 | toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL0, 1); |
| 867 | handled_exception = 1; |
| 868 | } |
| 869 | |
| 870 | if (debug_info->wps_disabled) { |
| 871 | debug_info->wps_disabled = 0; |
| 872 | toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 1); |
| 873 | handled_exception = 1; |
| 874 | } |
| 875 | |
| 876 | if (handled_exception) { |
| 877 | if (debug_info->suspended_step) { |
| 878 | debug_info->suspended_step = 0; |
| 879 | /* Allow exception handling to fall-through. */ |
| 880 | handled_exception = 0; |
| 881 | } else { |
| 882 | user_disable_single_step(current); |
| 883 | } |
| 884 | } |
| 885 | } else if (*kernel_step != ARM_KERNEL_STEP_NONE) { |
| 886 | toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL1, 1); |
| 887 | toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL1, 1); |
| 888 | |
| 889 | if (!debug_info->wps_disabled) |
| 890 | toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 1); |
| 891 | |
| 892 | if (*kernel_step != ARM_KERNEL_STEP_SUSPEND) { |
| 893 | kernel_disable_single_step(); |
| 894 | handled_exception = 1; |
| 895 | } else { |
| 896 | handled_exception = 0; |
| 897 | } |
| 898 | |
| 899 | *kernel_step = ARM_KERNEL_STEP_NONE; |
| 900 | } |
| 901 | |
| 902 | return !handled_exception; |
| 903 | } |
Pratyush Anand | 44b53f6 | 2016-07-08 12:35:49 -0400 | [diff] [blame] | 904 | NOKPROBE_SYMBOL(reinstall_suspended_bps); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 905 | |
| 906 | /* |
| 907 | * Context-switcher for restoring suspended breakpoints. |
| 908 | */ |
| 909 | void hw_breakpoint_thread_switch(struct task_struct *next) |
| 910 | { |
| 911 | /* |
| 912 | * current next |
| 913 | * disabled: 0 0 => The usual case, NOTIFY_DONE |
| 914 | * 0 1 => Disable the registers |
| 915 | * 1 0 => Enable the registers |
| 916 | * 1 1 => NOTIFY_DONE. per-task bps will |
| 917 | * get taken care of by perf. |
| 918 | */ |
| 919 | |
| 920 | struct debug_info *current_debug_info, *next_debug_info; |
| 921 | |
| 922 | current_debug_info = ¤t->thread.debug; |
| 923 | next_debug_info = &next->thread.debug; |
| 924 | |
| 925 | /* Update breakpoints. */ |
| 926 | if (current_debug_info->bps_disabled != next_debug_info->bps_disabled) |
| 927 | toggle_bp_registers(AARCH64_DBG_REG_BCR, |
| 928 | DBG_ACTIVE_EL0, |
| 929 | !next_debug_info->bps_disabled); |
| 930 | |
| 931 | /* Update watchpoints. */ |
| 932 | if (current_debug_info->wps_disabled != next_debug_info->wps_disabled) |
| 933 | toggle_bp_registers(AARCH64_DBG_REG_WCR, |
| 934 | DBG_ACTIVE_EL0, |
| 935 | !next_debug_info->wps_disabled); |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * CPU initialisation. |
| 940 | */ |
Will Deacon | d7a83d1 | 2016-08-15 18:55:11 +0100 | [diff] [blame] | 941 | static int hw_breakpoint_reset(unsigned int cpu) |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 942 | { |
| 943 | int i; |
Lorenzo Pieralisi | 60fc694 | 2013-08-05 15:20:35 +0100 | [diff] [blame] | 944 | struct perf_event **slots; |
| 945 | /* |
| 946 | * When a CPU goes through cold-boot, it does not have any installed |
| 947 | * slot, so it is safe to share the same function for restoring and |
| 948 | * resetting breakpoints; when a CPU is hotplugged in, it goes |
| 949 | * through the slots, which are all empty, hence it just resets control |
| 950 | * and value for debug registers. |
| 951 | * When this function is triggered on warm-boot through a CPU PM |
| 952 | * notifier some slots might be initialized; if so they are |
| 953 | * reprogrammed according to the debug slots content. |
| 954 | */ |
| 955 | for (slots = this_cpu_ptr(bp_on_reg), i = 0; i < core_num_brps; ++i) { |
| 956 | if (slots[i]) { |
| 957 | hw_breakpoint_control(slots[i], HW_BREAKPOINT_RESTORE); |
| 958 | } else { |
| 959 | write_wb_reg(AARCH64_DBG_REG_BCR, i, 0UL); |
| 960 | write_wb_reg(AARCH64_DBG_REG_BVR, i, 0UL); |
| 961 | } |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Lorenzo Pieralisi | 60fc694 | 2013-08-05 15:20:35 +0100 | [diff] [blame] | 964 | for (slots = this_cpu_ptr(wp_on_reg), i = 0; i < core_num_wrps; ++i) { |
| 965 | if (slots[i]) { |
| 966 | hw_breakpoint_control(slots[i], HW_BREAKPOINT_RESTORE); |
| 967 | } else { |
| 968 | write_wb_reg(AARCH64_DBG_REG_WCR, i, 0UL); |
| 969 | write_wb_reg(AARCH64_DBG_REG_WVR, i, 0UL); |
| 970 | } |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 971 | } |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 972 | |
Will Deacon | d7a83d1 | 2016-08-15 18:55:11 +0100 | [diff] [blame] | 973 | return 0; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Lorenzo Pieralisi | af3cfdb | 2015-01-26 18:33:44 +0000 | [diff] [blame] | 976 | #ifdef CONFIG_CPU_PM |
Will Deacon | d7a83d1 | 2016-08-15 18:55:11 +0100 | [diff] [blame] | 977 | extern void cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int)); |
Lorenzo Pieralisi | 60fc694 | 2013-08-05 15:20:35 +0100 | [diff] [blame] | 978 | #else |
Will Deacon | d7a83d1 | 2016-08-15 18:55:11 +0100 | [diff] [blame] | 979 | static inline void cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int)) |
Lorenzo Pieralisi | 60fc694 | 2013-08-05 15:20:35 +0100 | [diff] [blame] | 980 | { |
| 981 | } |
| 982 | #endif |
| 983 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 984 | /* |
| 985 | * One-time initialisation. |
| 986 | */ |
| 987 | static int __init arch_hw_breakpoint_init(void) |
| 988 | { |
Will Deacon | d7a83d1 | 2016-08-15 18:55:11 +0100 | [diff] [blame] | 989 | int ret; |
| 990 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 991 | core_num_brps = get_num_brps(); |
| 992 | core_num_wrps = get_num_wrps(); |
| 993 | |
| 994 | pr_info("found %d breakpoint and %d watchpoint registers.\n", |
| 995 | core_num_brps, core_num_wrps); |
| 996 | |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 997 | /* Register debug fault handlers. */ |
| 998 | hook_debug_fault_code(DBG_ESR_EVT_HWBP, breakpoint_handler, SIGTRAP, |
| 999 | TRAP_HWBKPT, "hw-breakpoint handler"); |
| 1000 | hook_debug_fault_code(DBG_ESR_EVT_HWWP, watchpoint_handler, SIGTRAP, |
| 1001 | TRAP_HWBKPT, "hw-watchpoint handler"); |
| 1002 | |
Will Deacon | d7a83d1 | 2016-08-15 18:55:11 +0100 | [diff] [blame] | 1003 | /* |
| 1004 | * Reset the breakpoint resources. We assume that a halting |
| 1005 | * debugger will leave the world in a nice state for us. |
| 1006 | */ |
| 1007 | ret = cpuhp_setup_state(CPUHP_AP_PERF_ARM_HW_BREAKPOINT_STARTING, |
Thomas Gleixner | 73c1b41 | 2016-12-21 20:19:54 +0100 | [diff] [blame] | 1008 | "perf/arm64/hw_breakpoint:starting", |
Will Deacon | d7a83d1 | 2016-08-15 18:55:11 +0100 | [diff] [blame] | 1009 | hw_breakpoint_reset, NULL); |
| 1010 | if (ret) |
| 1011 | pr_err("failed to register CPU hotplug notifier: %d\n", ret); |
Srivatsa S. Bhat | 3d0dc64 | 2014-03-11 02:09:08 +0530 | [diff] [blame] | 1012 | |
Lorenzo Pieralisi | 65c021b | 2014-01-10 13:15:05 +0000 | [diff] [blame] | 1013 | /* Register cpu_suspend hw breakpoint restore hook */ |
| 1014 | cpu_suspend_set_dbg_restorer(hw_breakpoint_reset); |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 1015 | |
Will Deacon | d7a83d1 | 2016-08-15 18:55:11 +0100 | [diff] [blame] | 1016 | return ret; |
Will Deacon | 478fcb2 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 1017 | } |
| 1018 | arch_initcall(arch_hw_breakpoint_init); |
| 1019 | |
| 1020 | void hw_breakpoint_pmu_read(struct perf_event *bp) |
| 1021 | { |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * Dummy function to register with die_notifier. |
| 1026 | */ |
| 1027 | int hw_breakpoint_exceptions_notify(struct notifier_block *unused, |
| 1028 | unsigned long val, void *data) |
| 1029 | { |
| 1030 | return NOTIFY_DONE; |
| 1031 | } |