Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 2 | /* |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007 Alan Stern |
| 5 | * Copyright (C) 2009 IBM Corporation |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2009 Frederic Weisbecker <fweisbec@gmail.com> |
K.Prasad | ba6909b | 2009-11-23 21:17:13 +0530 | [diff] [blame] | 7 | * |
| 8 | * Authors: Alan Stern <stern@rowland.harvard.edu> |
| 9 | * K.Prasad <prasad@linux.vnet.ibm.com> |
| 10 | * Frederic Weisbecker <fweisbec@gmail.com> |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility, |
| 15 | * using the CPU's debug registers. |
| 16 | */ |
| 17 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 18 | #include <linux/perf_event.h> |
| 19 | #include <linux/hw_breakpoint.h> |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 20 | #include <linux/irqflags.h> |
| 21 | #include <linux/notifier.h> |
| 22 | #include <linux/kallsyms.h> |
Andy Lutomirski | e5779e8 | 2015-07-30 20:32:40 -0700 | [diff] [blame] | 23 | #include <linux/kprobes.h> |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 24 | #include <linux/percpu.h> |
| 25 | #include <linux/kdebug.h> |
| 26 | #include <linux/kernel.h> |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 28 | #include <linux/sched.h> |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 29 | #include <linux/smp.h> |
| 30 | |
| 31 | #include <asm/hw_breakpoint.h> |
| 32 | #include <asm/processor.h> |
| 33 | #include <asm/debugreg.h> |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 34 | #include <asm/user.h> |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 35 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 36 | /* Per cpu debug control register value */ |
Tejun Heo | 28b4e0d | 2009-11-25 22:24:44 +0900 | [diff] [blame] | 37 | DEFINE_PER_CPU(unsigned long, cpu_dr7); |
| 38 | EXPORT_PER_CPU_SYMBOL(cpu_dr7); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 39 | |
| 40 | /* Per cpu debug address registers values */ |
| 41 | static DEFINE_PER_CPU(unsigned long, cpu_debugreg[HBP_NUM]); |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 42 | |
| 43 | /* |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 44 | * Stores the breakpoints currently in use on each breakpoint address |
| 45 | * register for each cpus |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 46 | */ |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 47 | static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]); |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 48 | |
| 49 | |
Frederic Weisbecker | 2c31b79 | 2009-11-26 06:04:38 +0100 | [diff] [blame] | 50 | static inline unsigned long |
| 51 | __encode_dr7(int drnum, unsigned int len, unsigned int type) |
| 52 | { |
| 53 | unsigned long bp_info; |
| 54 | |
| 55 | bp_info = (len | type) & 0xf; |
| 56 | bp_info <<= (DR_CONTROL_SHIFT + drnum * DR_CONTROL_SIZE); |
| 57 | bp_info |= (DR_GLOBAL_ENABLE << (drnum * DR_ENABLE_SIZE)); |
| 58 | |
| 59 | return bp_info; |
| 60 | } |
| 61 | |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 62 | /* |
| 63 | * Encode the length, type, Exact, and Enable bits for a particular breakpoint |
| 64 | * as stored in debug register 7. |
| 65 | */ |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 66 | unsigned long encode_dr7(int drnum, unsigned int len, unsigned int type) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 67 | { |
Frederic Weisbecker | 2c31b79 | 2009-11-26 06:04:38 +0100 | [diff] [blame] | 68 | return __encode_dr7(drnum, len, type) | DR_GLOBAL_SLOWDOWN; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 69 | } |
| 70 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 71 | /* |
| 72 | * Decode the length and type bits for a particular breakpoint as |
| 73 | * stored in debug register 7. Return the "enabled" status. |
| 74 | */ |
| 75 | int decode_dr7(unsigned long dr7, int bpnum, unsigned *len, unsigned *type) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 76 | { |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 77 | int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE); |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 78 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 79 | *len = (bp_info & 0xc) | 0x40; |
| 80 | *type = (bp_info & 0x3) | 0x80; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 81 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 82 | return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Install a perf counter breakpoint. |
| 87 | * |
| 88 | * We seek a free debug address register and use it for this |
| 89 | * breakpoint. Eventually we enable it in the debug control register. |
| 90 | * |
| 91 | * Atomic: we hold the counter->ctx->lock and we only handle variables |
| 92 | * and registers local to this cpu. |
| 93 | */ |
| 94 | int arch_install_hw_breakpoint(struct perf_event *bp) |
| 95 | { |
| 96 | struct arch_hw_breakpoint *info = counter_arch_bp(bp); |
| 97 | unsigned long *dr7; |
| 98 | int i; |
| 99 | |
| 100 | for (i = 0; i < HBP_NUM; i++) { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 101 | struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 102 | |
| 103 | if (!*slot) { |
| 104 | *slot = bp; |
| 105 | break; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 109 | if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot")) |
| 110 | return -EBUSY; |
| 111 | |
| 112 | set_debugreg(info->address, i); |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 113 | __this_cpu_write(cpu_debugreg[i], info->address); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 114 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 115 | dr7 = this_cpu_ptr(&cpu_dr7); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 116 | *dr7 |= encode_dr7(i, info->len, info->type); |
| 117 | |
| 118 | set_debugreg(*dr7, 7); |
Jacob Shin | d6d55f0 | 2014-05-29 17:26:50 +0200 | [diff] [blame] | 119 | if (info->mask) |
| 120 | set_dr_addr_mask(info->mask, i); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 121 | |
| 122 | return 0; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 126 | * Uninstall the breakpoint contained in the given counter. |
| 127 | * |
| 128 | * First we search the debug address register it uses and then we disable |
| 129 | * it. |
| 130 | * |
| 131 | * Atomic: we hold the counter->ctx->lock and we only handle variables |
| 132 | * and registers local to this cpu. |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 133 | */ |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 134 | void arch_uninstall_hw_breakpoint(struct perf_event *bp) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 135 | { |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 136 | struct arch_hw_breakpoint *info = counter_arch_bp(bp); |
| 137 | unsigned long *dr7; |
| 138 | int i; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 139 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 140 | for (i = 0; i < HBP_NUM; i++) { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 141 | struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 142 | |
| 143 | if (*slot == bp) { |
| 144 | *slot = NULL; |
| 145 | break; |
| 146 | } |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 147 | } |
| 148 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 149 | if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot")) |
| 150 | return; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 151 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 152 | dr7 = this_cpu_ptr(&cpu_dr7); |
Frederic Weisbecker | 2c31b79 | 2009-11-26 06:04:38 +0100 | [diff] [blame] | 153 | *dr7 &= ~__encode_dr7(i, info->len, info->type); |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 154 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 155 | set_debugreg(*dr7, 7); |
Jacob Shin | d6d55f0 | 2014-05-29 17:26:50 +0200 | [diff] [blame] | 156 | if (info->mask) |
| 157 | set_dr_addr_mask(0, i); |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 158 | } |
| 159 | |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 160 | static int arch_bp_generic_len(int x86_len) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 161 | { |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 162 | switch (x86_len) { |
| 163 | case X86_BREAKPOINT_LEN_1: |
| 164 | return HW_BREAKPOINT_LEN_1; |
| 165 | case X86_BREAKPOINT_LEN_2: |
| 166 | return HW_BREAKPOINT_LEN_2; |
| 167 | case X86_BREAKPOINT_LEN_4: |
| 168 | return HW_BREAKPOINT_LEN_4; |
| 169 | #ifdef CONFIG_X86_64 |
| 170 | case X86_BREAKPOINT_LEN_8: |
| 171 | return HW_BREAKPOINT_LEN_8; |
| 172 | #endif |
| 173 | default: |
| 174 | return -EINVAL; |
| 175 | } |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 176 | } |
| 177 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 178 | int arch_bp_generic_fields(int x86_len, int x86_type, |
| 179 | int *gen_len, int *gen_type) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 180 | { |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 181 | int len; |
| 182 | |
Frederic Weisbecker | 89e45aa | 2010-09-17 03:24:13 +0200 | [diff] [blame] | 183 | /* Type */ |
| 184 | switch (x86_type) { |
| 185 | case X86_BREAKPOINT_EXECUTE: |
| 186 | if (x86_len != X86_BREAKPOINT_LEN_X) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | *gen_type = HW_BREAKPOINT_X; |
| 190 | *gen_len = sizeof(long); |
| 191 | return 0; |
| 192 | case X86_BREAKPOINT_WRITE: |
| 193 | *gen_type = HW_BREAKPOINT_W; |
| 194 | break; |
| 195 | case X86_BREAKPOINT_RW: |
| 196 | *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R; |
| 197 | break; |
| 198 | default: |
| 199 | return -EINVAL; |
| 200 | } |
| 201 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 202 | /* Len */ |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 203 | len = arch_bp_generic_len(x86_len); |
| 204 | if (len < 0) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 205 | return -EINVAL; |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 206 | *gen_len = len; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 207 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 211 | /* |
| 212 | * Check for virtual address in kernel space. |
| 213 | */ |
| 214 | int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw) |
| 215 | { |
| 216 | unsigned long va; |
| 217 | int len; |
| 218 | |
| 219 | va = hw->address; |
| 220 | len = arch_bp_generic_len(hw->len); |
| 221 | WARN_ON_ONCE(len < 0); |
| 222 | |
| 223 | /* |
| 224 | * We don't need to worry about va + len - 1 overflowing: |
| 225 | * we already require that va is aligned to a multiple of len. |
| 226 | */ |
| 227 | return (va >= TASK_SIZE_MAX) || ((va + len - 1) >= TASK_SIZE_MAX); |
| 228 | } |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 229 | |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 230 | static int arch_build_bp_info(struct perf_event *bp, |
| 231 | const struct perf_event_attr *attr, |
| 232 | struct arch_hw_breakpoint *hw) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 233 | { |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 234 | hw->address = attr->bp_addr; |
| 235 | hw->mask = 0; |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 236 | |
Frederic Weisbecker | f7809da | 2010-06-24 10:00:24 +0200 | [diff] [blame] | 237 | /* Type */ |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 238 | switch (attr->bp_type) { |
Frederic Weisbecker | f7809da | 2010-06-24 10:00:24 +0200 | [diff] [blame] | 239 | case HW_BREAKPOINT_W: |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 240 | hw->type = X86_BREAKPOINT_WRITE; |
Frederic Weisbecker | f7809da | 2010-06-24 10:00:24 +0200 | [diff] [blame] | 241 | break; |
| 242 | case HW_BREAKPOINT_W | HW_BREAKPOINT_R: |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 243 | hw->type = X86_BREAKPOINT_RW; |
Frederic Weisbecker | f7809da | 2010-06-24 10:00:24 +0200 | [diff] [blame] | 244 | break; |
| 245 | case HW_BREAKPOINT_X: |
Andy Lutomirski | e5779e8 | 2015-07-30 20:32:40 -0700 | [diff] [blame] | 246 | /* |
| 247 | * We don't allow kernel breakpoints in places that are not |
| 248 | * acceptable for kprobes. On non-kprobes kernels, we don't |
| 249 | * allow kernel breakpoints at all. |
| 250 | */ |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 251 | if (attr->bp_addr >= TASK_SIZE_MAX) { |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 252 | if (within_kprobe_blacklist(attr->bp_addr)) |
Andy Lutomirski | e5779e8 | 2015-07-30 20:32:40 -0700 | [diff] [blame] | 253 | return -EINVAL; |
Andy Lutomirski | e5779e8 | 2015-07-30 20:32:40 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 256 | hw->type = X86_BREAKPOINT_EXECUTE; |
Frederic Weisbecker | f7809da | 2010-06-24 10:00:24 +0200 | [diff] [blame] | 257 | /* |
| 258 | * x86 inst breakpoints need to have a specific undefined len. |
| 259 | * But we still need to check userspace is not trying to setup |
| 260 | * an unsupported length, to get a range breakpoint for example. |
| 261 | */ |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 262 | if (attr->bp_len == sizeof(long)) { |
| 263 | hw->len = X86_BREAKPOINT_LEN_X; |
Frederic Weisbecker | f7809da | 2010-06-24 10:00:24 +0200 | [diff] [blame] | 264 | return 0; |
| 265 | } |
Gustavo A. R. Silva | 6fcebf1 | 2019-01-25 12:39:03 -0600 | [diff] [blame] | 266 | /* fall through */ |
Frederic Weisbecker | f7809da | 2010-06-24 10:00:24 +0200 | [diff] [blame] | 267 | default: |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 271 | /* Len */ |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 272 | switch (attr->bp_len) { |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 273 | case HW_BREAKPOINT_LEN_1: |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 274 | hw->len = X86_BREAKPOINT_LEN_1; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 275 | break; |
| 276 | case HW_BREAKPOINT_LEN_2: |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 277 | hw->len = X86_BREAKPOINT_LEN_2; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 278 | break; |
| 279 | case HW_BREAKPOINT_LEN_4: |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 280 | hw->len = X86_BREAKPOINT_LEN_4; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 281 | break; |
| 282 | #ifdef CONFIG_X86_64 |
| 283 | case HW_BREAKPOINT_LEN_8: |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 284 | hw->len = X86_BREAKPOINT_LEN_8; |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 285 | break; |
| 286 | #endif |
| 287 | default: |
Andy Lutomirski | ab51392 | 2015-07-30 20:32:41 -0700 | [diff] [blame] | 288 | /* AMD range breakpoint */ |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 289 | if (!is_power_of_2(attr->bp_len)) |
Jacob Shin | d6d55f0 | 2014-05-29 17:26:50 +0200 | [diff] [blame] | 290 | return -EINVAL; |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 291 | if (attr->bp_addr & (attr->bp_len - 1)) |
Andy Lutomirski | ab51392 | 2015-07-30 20:32:41 -0700 | [diff] [blame] | 292 | return -EINVAL; |
Borislav Petkov | 362f924 | 2015-12-07 10:39:41 +0100 | [diff] [blame] | 293 | |
| 294 | if (!boot_cpu_has(X86_FEATURE_BPEXT)) |
| 295 | return -EOPNOTSUPP; |
| 296 | |
Andy Lutomirski | ab51392 | 2015-07-30 20:32:41 -0700 | [diff] [blame] | 297 | /* |
| 298 | * It's impossible to use a range breakpoint to fake out |
| 299 | * user vs kernel detection because bp_len - 1 can't |
| 300 | * have the high bit set. If we ever allow range instruction |
| 301 | * breakpoints, then we'll have to check for kprobe-blacklisted |
| 302 | * addresses anywhere in the range. |
| 303 | */ |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 304 | hw->mask = attr->bp_len - 1; |
| 305 | hw->len = X86_BREAKPOINT_LEN_1; |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 308 | return 0; |
| 309 | } |
Jacob Shin | d6d55f0 | 2014-05-29 17:26:50 +0200 | [diff] [blame] | 310 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 311 | /* |
| 312 | * Validate the arch-specific HW Breakpoint register settings |
| 313 | */ |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 314 | int hw_breakpoint_arch_parse(struct perf_event *bp, |
| 315 | const struct perf_event_attr *attr, |
| 316 | struct arch_hw_breakpoint *hw) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 317 | { |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 318 | unsigned int align; |
| 319 | int ret; |
| 320 | |
| 321 | |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 322 | ret = arch_build_bp_info(bp, attr, hw); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 323 | if (ret) |
| 324 | return ret; |
| 325 | |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 326 | switch (hw->len) { |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 327 | case X86_BREAKPOINT_LEN_1: |
| 328 | align = 0; |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 329 | if (hw->mask) |
| 330 | align = hw->mask; |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 331 | break; |
| 332 | case X86_BREAKPOINT_LEN_2: |
| 333 | align = 1; |
| 334 | break; |
| 335 | case X86_BREAKPOINT_LEN_4: |
| 336 | align = 3; |
| 337 | break; |
| 338 | #ifdef CONFIG_X86_64 |
| 339 | case X86_BREAKPOINT_LEN_8: |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 340 | align = 7; |
| 341 | break; |
| 342 | #endif |
| 343 | default: |
Jacob Shin | d6d55f0 | 2014-05-29 17:26:50 +0200 | [diff] [blame] | 344 | WARN_ON_ONCE(1); |
Nathan Chancellor | e898e69 | 2019-03-07 14:27:56 -0700 | [diff] [blame] | 345 | return -EINVAL; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 346 | } |
| 347 | |
Frederic Weisbecker | 84d7109 | 2010-02-18 16:00:59 +0100 | [diff] [blame] | 348 | /* |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 349 | * Check that the low-order bits of the address are appropriate |
| 350 | * for the alignment implied by len. |
| 351 | */ |
Frederic Weisbecker | a0baf04 | 2018-06-26 04:58:50 +0200 | [diff] [blame] | 352 | if (hw->address & align) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 353 | return -EINVAL; |
| 354 | |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 358 | /* |
Frederic Weisbecker | 9f6b3c2 | 2009-11-09 21:03:43 +0100 | [diff] [blame] | 359 | * Dump the debug register contents to the user. |
| 360 | * We can't dump our per cpu values because it |
| 361 | * may contain cpu wide breakpoint, something that |
| 362 | * doesn't belong to the current task. |
| 363 | * |
| 364 | * TODO: include non-ptrace user breakpoints (perf) |
| 365 | */ |
| 366 | void aout_dump_debugregs(struct user *dump) |
| 367 | { |
| 368 | int i; |
| 369 | int dr7 = 0; |
| 370 | struct perf_event *bp; |
| 371 | struct arch_hw_breakpoint *info; |
| 372 | struct thread_struct *thread = ¤t->thread; |
| 373 | |
| 374 | for (i = 0; i < HBP_NUM; i++) { |
| 375 | bp = thread->ptrace_bps[i]; |
| 376 | |
| 377 | if (bp && !bp->attr.disabled) { |
| 378 | dump->u_debugreg[i] = bp->attr.bp_addr; |
| 379 | info = counter_arch_bp(bp); |
| 380 | dr7 |= encode_dr7(i, info->len, info->type); |
| 381 | } else { |
| 382 | dump->u_debugreg[i] = 0; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | dump->u_debugreg[4] = 0; |
| 387 | dump->u_debugreg[5] = 0; |
| 388 | dump->u_debugreg[6] = current->thread.debugreg6; |
| 389 | |
| 390 | dump->u_debugreg[7] = dr7; |
| 391 | } |
Ingo Molnar | 68efa37 | 2009-11-14 01:35:29 +0100 | [diff] [blame] | 392 | EXPORT_SYMBOL_GPL(aout_dump_debugregs); |
Frederic Weisbecker | 9f6b3c2 | 2009-11-09 21:03:43 +0100 | [diff] [blame] | 393 | |
| 394 | /* |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 395 | * Release the user breakpoints used by ptrace |
| 396 | */ |
| 397 | void flush_ptrace_hw_breakpoint(struct task_struct *tsk) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 398 | { |
| 399 | int i; |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 400 | struct thread_struct *t = &tsk->thread; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 401 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 402 | for (i = 0; i < HBP_NUM; i++) { |
| 403 | unregister_hw_breakpoint(t->ptrace_bps[i]); |
| 404 | t->ptrace_bps[i] = NULL; |
| 405 | } |
Oleg Nesterov | f7da04c | 2013-07-08 16:01:06 -0700 | [diff] [blame] | 406 | |
| 407 | t->debugreg6 = 0; |
| 408 | t->ptrace_dr7 = 0; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 409 | } |
| 410 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 411 | void hw_breakpoint_restore(void) |
| 412 | { |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 413 | set_debugreg(__this_cpu_read(cpu_debugreg[0]), 0); |
| 414 | set_debugreg(__this_cpu_read(cpu_debugreg[1]), 1); |
| 415 | set_debugreg(__this_cpu_read(cpu_debugreg[2]), 2); |
| 416 | set_debugreg(__this_cpu_read(cpu_debugreg[3]), 3); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 417 | set_debugreg(current->thread.debugreg6, 6); |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 418 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 419 | } |
| 420 | EXPORT_SYMBOL_GPL(hw_breakpoint_restore); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 421 | |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 422 | /* |
| 423 | * Handle debug exception notifications. |
| 424 | * |
| 425 | * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below. |
| 426 | * |
| 427 | * NOTIFY_DONE returned if one of the following conditions is true. |
| 428 | * i) When the causative address is from user-space and the exception |
| 429 | * is a valid one, i.e. not triggered as a result of lazy debug register |
| 430 | * switching |
| 431 | * ii) When there are more bits than trap<n> set in DR6 register (such |
| 432 | * as BD, BS or BT) indicating that more than one debug condition is |
| 433 | * met and requires some more action in do_debug(). |
| 434 | * |
| 435 | * NOTIFY_STOP returned for all other cases |
| 436 | * |
| 437 | */ |
Masami Hiramatsu | 9c54b61 | 2014-04-17 17:18:07 +0900 | [diff] [blame] | 438 | static int hw_breakpoint_handler(struct die_args *args) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 439 | { |
| 440 | int i, cpu, rc = NOTIFY_STOP; |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 441 | struct perf_event *bp; |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 442 | unsigned long dr7, dr6; |
| 443 | unsigned long *dr6_p; |
| 444 | |
| 445 | /* The DR6 value is pointed by args->err */ |
| 446 | dr6_p = (unsigned long *)ERR_PTR(args->err); |
| 447 | dr6 = *dr6_p; |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 448 | |
Frederic Weisbecker | 6c0aca2 | 2010-11-11 21:18:43 +0100 | [diff] [blame] | 449 | /* If it's a single step, TRAP bits are random */ |
| 450 | if (dr6 & DR_STEP) |
| 451 | return NOTIFY_DONE; |
| 452 | |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 453 | /* Do an early return if no trap bits are set in DR6 */ |
| 454 | if ((dr6 & DR_TRAP_BITS) == 0) |
| 455 | return NOTIFY_DONE; |
| 456 | |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 457 | get_debugreg(dr7, 7); |
| 458 | /* Disable breakpoints during exception handling */ |
| 459 | set_debugreg(0UL, 7); |
| 460 | /* |
| 461 | * Assert that local interrupts are disabled |
| 462 | * Reset the DRn bits in the virtualized register value. |
| 463 | * The ptrace trigger routine will add in whatever is needed. |
| 464 | */ |
| 465 | current->thread.debugreg6 &= ~DR_TRAP_BITS; |
| 466 | cpu = get_cpu(); |
| 467 | |
| 468 | /* Handle all the breakpoints that were triggered */ |
| 469 | for (i = 0; i < HBP_NUM; ++i) { |
| 470 | if (likely(!(dr6 & (DR_TRAP0 << i)))) |
| 471 | continue; |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 472 | |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 473 | /* |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 474 | * The counter may be concurrently released but that can only |
| 475 | * occur from a call_rcu() path. We can then safely fetch |
| 476 | * the breakpoint, use its callback, touch its counter |
| 477 | * while we are in an rcu_read_lock() path. |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 478 | */ |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 479 | rcu_read_lock(); |
| 480 | |
| 481 | bp = per_cpu(bp_per_reg[i], cpu); |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 482 | /* |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 483 | * Reset the 'i'th TRAP bit in dr6 to denote completion of |
| 484 | * exception handling |
| 485 | */ |
| 486 | (*dr6_p) &= ~(DR_TRAP0 << i); |
| 487 | /* |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 488 | * bp can be NULL due to lazy debug register switching |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 489 | * or due to concurrent perf counter removing. |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 490 | */ |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 491 | if (!bp) { |
| 492 | rcu_read_unlock(); |
| 493 | break; |
| 494 | } |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 495 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 496 | perf_bp_event(bp, args->regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 497 | |
Frederic Weisbecker | 0c4519e | 2010-06-24 21:21:27 +0200 | [diff] [blame] | 498 | /* |
| 499 | * Set up resume flag to avoid breakpoint recursion when |
| 500 | * returning back to origin. |
| 501 | */ |
| 502 | if (bp->hw.info.type == X86_BREAKPOINT_EXECUTE) |
| 503 | args->regs->flags |= X86_EFLAGS_RF; |
| 504 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 505 | rcu_read_unlock(); |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 506 | } |
K.Prasad | e0e53db | 2010-01-28 16:44:15 +0530 | [diff] [blame] | 507 | /* |
| 508 | * Further processing in do_debug() is needed for a) user-space |
| 509 | * breakpoints (to generate signals) and b) when the system has |
| 510 | * taken exception due to multiple causes |
| 511 | */ |
| 512 | if ((current->thread.debugreg6 & DR_TRAP_BITS) || |
| 513 | (dr6 & (~DR_TRAP_BITS))) |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 514 | rc = NOTIFY_DONE; |
| 515 | |
| 516 | set_debugreg(dr7, 7); |
Ingo Molnar | eadb8a0 | 2009-06-17 12:52:15 +0200 | [diff] [blame] | 517 | put_cpu(); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 518 | |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 519 | return rc; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Handle debug exception notifications. |
| 524 | */ |
Masami Hiramatsu | 9c54b61 | 2014-04-17 17:18:07 +0900 | [diff] [blame] | 525 | int hw_breakpoint_exceptions_notify( |
K.Prasad | 0067f12 | 2009-06-01 23:43:57 +0530 | [diff] [blame] | 526 | struct notifier_block *unused, unsigned long val, void *data) |
| 527 | { |
| 528 | if (val != DIE_DEBUG) |
| 529 | return NOTIFY_DONE; |
| 530 | |
| 531 | return hw_breakpoint_handler(data); |
| 532 | } |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 533 | |
| 534 | void hw_breakpoint_pmu_read(struct perf_event *bp) |
| 535 | { |
| 536 | /* TODO */ |
| 537 | } |