K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 1 | /* |
| 2 | * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility, |
| 3 | * using the CPU's debug registers. Derived from |
| 4 | * "arch/x86/kernel/hw_breakpoint.c" |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | * |
| 20 | * Copyright 2010 IBM Corporation |
| 21 | * Author: K.Prasad <prasad@linux.vnet.ibm.com> |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/hw_breakpoint.h> |
| 26 | #include <linux/notifier.h> |
| 27 | #include <linux/kprobes.h> |
| 28 | #include <linux/percpu.h> |
| 29 | #include <linux/kernel.h> |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 30 | #include <linux/sched.h> |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 31 | #include <linux/smp.h> |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame^] | 32 | #include <linux/debugfs.h> |
| 33 | #include <linux/init.h> |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 34 | |
| 35 | #include <asm/hw_breakpoint.h> |
| 36 | #include <asm/processor.h> |
| 37 | #include <asm/sstep.h> |
Michael Neuling | 85ce9a5 | 2018-03-27 15:37:18 +1100 | [diff] [blame] | 38 | #include <asm/debug.h> |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame^] | 39 | #include <asm/debugfs.h> |
| 40 | #include <asm/hvcall.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 41 | #include <linux/uaccess.h> |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Stores the breakpoints currently in use on each breakpoint address |
| 45 | * register for every cpu |
| 46 | */ |
| 47 | static DEFINE_PER_CPU(struct perf_event *, bp_per_reg); |
| 48 | |
| 49 | /* |
Paul Mackerras | d09ec73 | 2010-06-29 12:50:32 +1000 | [diff] [blame] | 50 | * Returns total number of data or instruction breakpoints available. |
| 51 | */ |
| 52 | int hw_breakpoint_slots(int type) |
| 53 | { |
| 54 | if (type == TYPE_DATA) |
| 55 | return HBP_NUM; |
| 56 | return 0; /* no instruction breakpoints available */ |
| 57 | } |
| 58 | |
| 59 | /* |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 60 | * Install a perf counter breakpoint. |
| 61 | * |
| 62 | * We seek a free debug address register and use it for this |
| 63 | * breakpoint. |
| 64 | * |
| 65 | * Atomic: we hold the counter->ctx->lock and we only handle variables |
| 66 | * and registers local to this cpu. |
| 67 | */ |
| 68 | int arch_install_hw_breakpoint(struct perf_event *bp) |
| 69 | { |
| 70 | struct arch_hw_breakpoint *info = counter_arch_bp(bp); |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 71 | struct perf_event **slot = this_cpu_ptr(&bp_per_reg); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 72 | |
| 73 | *slot = bp; |
| 74 | |
| 75 | /* |
| 76 | * Do not install DABR values if the instruction must be single-stepped. |
| 77 | * If so, DABR will be populated in single_step_dabr_instruction(). |
| 78 | */ |
| 79 | if (current->thread.last_hit_ubp != bp) |
Paul Gortmaker | 21f5850 | 2014-04-29 15:25:17 -0400 | [diff] [blame] | 80 | __set_breakpoint(info); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Uninstall the breakpoint contained in the given counter. |
| 87 | * |
| 88 | * First we search the debug address register it uses and then we disable |
| 89 | * it. |
| 90 | * |
| 91 | * Atomic: we hold the counter->ctx->lock and we only handle variables |
| 92 | * and registers local to this cpu. |
| 93 | */ |
| 94 | void arch_uninstall_hw_breakpoint(struct perf_event *bp) |
| 95 | { |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 96 | struct perf_event **slot = this_cpu_ptr(&bp_per_reg); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 97 | |
| 98 | if (*slot != bp) { |
| 99 | WARN_ONCE(1, "Can't find the breakpoint"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | *slot = NULL; |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 104 | hw_breakpoint_disable(); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Perform cleanup of arch-specific counters during unregistration |
| 109 | * of the perf-event |
| 110 | */ |
| 111 | void arch_unregister_hw_breakpoint(struct perf_event *bp) |
| 112 | { |
| 113 | /* |
| 114 | * If the breakpoint is unregistered between a hw_breakpoint_handler() |
| 115 | * and the single_step_dabr_instruction(), then cleanup the breakpoint |
| 116 | * restoration variables to prevent dangling pointers. |
Ravi Bangoria | fb822e6 | 2016-03-02 15:25:17 +0530 | [diff] [blame] | 117 | * FIXME, this should not be using bp->ctx at all! Sayeth peterz. |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 118 | */ |
Ravi Bangoria | fb822e6 | 2016-03-02 15:25:17 +0530 | [diff] [blame] | 119 | if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L)) |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 120 | bp->ctx->task->thread.last_hit_ubp = NULL; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Check for virtual address in kernel space. |
| 125 | */ |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 126 | int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw) |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 127 | { |
Frederic Weisbecker | 8e983ff | 2018-06-26 04:58:49 +0200 | [diff] [blame] | 128 | return is_kernel_addr(hw->address); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | int arch_bp_generic_fields(int type, int *gen_bp_type) |
| 132 | { |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 133 | *gen_bp_type = 0; |
| 134 | if (type & HW_BRK_TYPE_READ) |
| 135 | *gen_bp_type |= HW_BREAKPOINT_R; |
| 136 | if (type & HW_BRK_TYPE_WRITE) |
| 137 | *gen_bp_type |= HW_BREAKPOINT_W; |
| 138 | if (*gen_bp_type == 0) |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 139 | return -EINVAL; |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Validate the arch-specific HW Breakpoint register settings |
| 145 | */ |
Frederic Weisbecker | 5d5176b | 2018-06-26 04:58:51 +0200 | [diff] [blame] | 146 | int hw_breakpoint_arch_parse(struct perf_event *bp, |
| 147 | const struct perf_event_attr *attr, |
| 148 | struct arch_hw_breakpoint *hw) |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 149 | { |
Michael Neuling | 4ae7ebe | 2013-01-24 15:02:59 +0000 | [diff] [blame] | 150 | int ret = -EINVAL, length_max; |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 151 | |
| 152 | if (!bp) |
| 153 | return ret; |
| 154 | |
Frederic Weisbecker | 5d5176b | 2018-06-26 04:58:51 +0200 | [diff] [blame] | 155 | hw->type = HW_BRK_TYPE_TRANSLATE; |
| 156 | if (attr->bp_type & HW_BREAKPOINT_R) |
| 157 | hw->type |= HW_BRK_TYPE_READ; |
| 158 | if (attr->bp_type & HW_BREAKPOINT_W) |
| 159 | hw->type |= HW_BRK_TYPE_WRITE; |
| 160 | if (hw->type == HW_BRK_TYPE_TRANSLATE) |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 161 | /* must set alteast read or write */ |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 162 | return ret; |
Frederic Weisbecker | 5d5176b | 2018-06-26 04:58:51 +0200 | [diff] [blame] | 163 | if (!attr->exclude_user) |
| 164 | hw->type |= HW_BRK_TYPE_USER; |
| 165 | if (!attr->exclude_kernel) |
| 166 | hw->type |= HW_BRK_TYPE_KERNEL; |
| 167 | if (!attr->exclude_hv) |
| 168 | hw->type |= HW_BRK_TYPE_HYP; |
| 169 | hw->address = attr->bp_addr; |
| 170 | hw->len = attr->bp_len; |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8) |
| 174 | * and breakpoint addresses are aligned to nearest double-word |
| 175 | * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the |
| 176 | * 'symbolsize' should satisfy the check below. |
| 177 | */ |
Michael Neuling | 85ce9a5 | 2018-03-27 15:37:18 +1100 | [diff] [blame] | 178 | if (!ppc_breakpoint_available()) |
| 179 | return -ENODEV; |
Michael Neuling | 4ae7ebe | 2013-01-24 15:02:59 +0000 | [diff] [blame] | 180 | length_max = 8; /* DABR */ |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame^] | 181 | if (dawr_enabled()) { |
Michael Neuling | 4ae7ebe | 2013-01-24 15:02:59 +0000 | [diff] [blame] | 182 | length_max = 512 ; /* 64 doublewords */ |
| 183 | /* DAWR region can't cross 512 boundary */ |
Frederic Weisbecker | 5d5176b | 2018-06-26 04:58:51 +0200 | [diff] [blame] | 184 | if ((attr->bp_addr >> 9) != |
| 185 | ((attr->bp_addr + attr->bp_len - 1) >> 9)) |
Michael Neuling | 4ae7ebe | 2013-01-24 15:02:59 +0000 | [diff] [blame] | 186 | return -EINVAL; |
| 187 | } |
Frederic Weisbecker | 5d5176b | 2018-06-26 04:58:51 +0200 | [diff] [blame] | 188 | if (hw->len > |
| 189 | (length_max - (hw->address & HW_BREAKPOINT_ALIGN))) |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 190 | return -EINVAL; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* |
K.Prasad | 06532a6 | 2010-06-15 11:35:41 +0530 | [diff] [blame] | 195 | * Restores the breakpoint on the debug registers. |
| 196 | * Invoke this function if it is known that the execution context is |
| 197 | * about to change to cause loss of MSR_SE settings. |
| 198 | */ |
| 199 | void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs) |
| 200 | { |
| 201 | struct arch_hw_breakpoint *info; |
| 202 | |
| 203 | if (likely(!tsk->thread.last_hit_ubp)) |
| 204 | return; |
| 205 | |
| 206 | info = counter_arch_bp(tsk->thread.last_hit_ubp); |
| 207 | regs->msr &= ~MSR_SE; |
Paul Gortmaker | 21f5850 | 2014-04-29 15:25:17 -0400 | [diff] [blame] | 208 | __set_breakpoint(info); |
K.Prasad | 06532a6 | 2010-06-15 11:35:41 +0530 | [diff] [blame] | 209 | tsk->thread.last_hit_ubp = NULL; |
| 210 | } |
| 211 | |
| 212 | /* |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 213 | * Handle debug exception notifications. |
| 214 | */ |
Nicholas Piggin | 03465f8 | 2016-09-16 20:48:08 +1000 | [diff] [blame] | 215 | int hw_breakpoint_handler(struct die_args *args) |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 216 | { |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 217 | int rc = NOTIFY_STOP; |
| 218 | struct perf_event *bp; |
| 219 | struct pt_regs *regs = args->regs; |
Christophe Leroy | 4ad8622 | 2016-11-29 09:52:15 +0100 | [diff] [blame] | 220 | #ifndef CONFIG_PPC_8xx |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 221 | int stepped = 1; |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 222 | unsigned int instr; |
Christophe Leroy | 4ad8622 | 2016-11-29 09:52:15 +0100 | [diff] [blame] | 223 | #endif |
| 224 | struct arch_hw_breakpoint *info; |
K.Prasad | e3e9408 | 2010-06-15 11:36:12 +0530 | [diff] [blame] | 225 | unsigned long dar = regs->dar; |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 226 | |
| 227 | /* Disable breakpoints during exception handling */ |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 228 | hw_breakpoint_disable(); |
Paul Mackerras | 574cb24 | 2010-06-23 15:42:43 +1000 | [diff] [blame] | 229 | |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 230 | /* |
| 231 | * The counter may be concurrently released but that can only |
| 232 | * occur from a call_rcu() path. We can then safely fetch |
| 233 | * the breakpoint, use its callback, touch its counter |
| 234 | * while we are in an rcu_read_lock() path. |
| 235 | */ |
| 236 | rcu_read_lock(); |
| 237 | |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 238 | bp = __this_cpu_read(bp_per_reg); |
Ravi Bangoria | c21a493 | 2016-11-22 14:55:59 +0530 | [diff] [blame] | 239 | if (!bp) { |
| 240 | rc = NOTIFY_DONE; |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 241 | goto out; |
Ravi Bangoria | c21a493 | 2016-11-22 14:55:59 +0530 | [diff] [blame] | 242 | } |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 243 | info = counter_arch_bp(bp); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * Return early after invoking user-callback function without restoring |
| 247 | * DABR if the breakpoint is from ptrace which always operates in |
| 248 | * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal |
| 249 | * generated in do_dabr(). |
| 250 | */ |
Paul Mackerras | 574cb24 | 2010-06-23 15:42:43 +1000 | [diff] [blame] | 251 | if (bp->overflow_handler == ptrace_triggered) { |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 252 | perf_bp_event(bp, regs); |
| 253 | rc = NOTIFY_DONE; |
| 254 | goto out; |
| 255 | } |
| 256 | |
K.Prasad | e3e9408 | 2010-06-15 11:36:12 +0530 | [diff] [blame] | 257 | /* |
| 258 | * Verify if dar lies within the address range occupied by the symbol |
Paul Mackerras | 574cb24 | 2010-06-23 15:42:43 +1000 | [diff] [blame] | 259 | * being watched to filter extraneous exceptions. If it doesn't, |
| 260 | * we still need to single-step the instruction, but we don't |
| 261 | * generate an event. |
K.Prasad | e3e9408 | 2010-06-15 11:36:12 +0530 | [diff] [blame] | 262 | */ |
Michael Neuling | 540e07c | 2013-06-24 15:47:23 +1000 | [diff] [blame] | 263 | info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ; |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 264 | if (!((bp->attr.bp_addr <= dar) && |
| 265 | (dar - bp->attr.bp_addr < bp->attr.bp_len))) |
| 266 | info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ; |
K.Prasad | e3e9408 | 2010-06-15 11:36:12 +0530 | [diff] [blame] | 267 | |
Christophe Leroy | 4ad8622 | 2016-11-29 09:52:15 +0100 | [diff] [blame] | 268 | #ifndef CONFIG_PPC_8xx |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 269 | /* Do not emulate user-space instructions, instead single-step them */ |
| 270 | if (user_mode(regs)) { |
Michael Neuling | 6d9c00c | 2012-08-22 20:30:43 +0000 | [diff] [blame] | 271 | current->thread.last_hit_ubp = bp; |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 272 | regs->msr |= MSR_SE; |
| 273 | goto out; |
| 274 | } |
| 275 | |
| 276 | stepped = 0; |
| 277 | instr = 0; |
| 278 | if (!__get_user_inatomic(instr, (unsigned int *) regs->nip)) |
| 279 | stepped = emulate_step(regs, instr); |
| 280 | |
| 281 | /* |
| 282 | * emulate_step() could not execute it. We've failed in reliably |
| 283 | * handling the hw-breakpoint. Unregister it and throw a warning |
| 284 | * message to let the user know about it. |
| 285 | */ |
| 286 | if (!stepped) { |
| 287 | WARN(1, "Unable to handle hardware breakpoint. Breakpoint at " |
| 288 | "0x%lx will be disabled.", info->address); |
Jiri Olsa | 5aab90c | 2016-10-26 11:48:24 +0200 | [diff] [blame] | 289 | perf_event_disable_inatomic(bp); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 290 | goto out; |
| 291 | } |
Christophe Leroy | 4ad8622 | 2016-11-29 09:52:15 +0100 | [diff] [blame] | 292 | #endif |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 293 | /* |
| 294 | * As a policy, the callback is invoked in a 'trigger-after-execute' |
| 295 | * fashion |
| 296 | */ |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 297 | if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) |
K.Prasad | e3e9408 | 2010-06-15 11:36:12 +0530 | [diff] [blame] | 298 | perf_bp_event(bp, regs); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 299 | |
Paul Gortmaker | 21f5850 | 2014-04-29 15:25:17 -0400 | [diff] [blame] | 300 | __set_breakpoint(info); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 301 | out: |
| 302 | rcu_read_unlock(); |
| 303 | return rc; |
| 304 | } |
Nicholas Piggin | 03465f8 | 2016-09-16 20:48:08 +1000 | [diff] [blame] | 305 | NOKPROBE_SYMBOL(hw_breakpoint_handler); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * Handle single-step exceptions following a DABR hit. |
| 309 | */ |
Nicholas Piggin | 03465f8 | 2016-09-16 20:48:08 +1000 | [diff] [blame] | 310 | static int single_step_dabr_instruction(struct die_args *args) |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 311 | { |
| 312 | struct pt_regs *regs = args->regs; |
| 313 | struct perf_event *bp = NULL; |
Michael Neuling | 3f4693e | 2012-09-05 19:17:48 +0000 | [diff] [blame] | 314 | struct arch_hw_breakpoint *info; |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 315 | |
| 316 | bp = current->thread.last_hit_ubp; |
| 317 | /* |
| 318 | * Check if we are single-stepping as a result of a |
| 319 | * previous HW Breakpoint exception |
| 320 | */ |
| 321 | if (!bp) |
| 322 | return NOTIFY_DONE; |
| 323 | |
Michael Neuling | 3f4693e | 2012-09-05 19:17:48 +0000 | [diff] [blame] | 324 | info = counter_arch_bp(bp); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * We shall invoke the user-defined callback function in the single |
| 328 | * stepping handler to confirm to 'trigger-after-execute' semantics |
| 329 | */ |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 330 | if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) |
K.Prasad | e3e9408 | 2010-06-15 11:36:12 +0530 | [diff] [blame] | 331 | perf_bp_event(bp, regs); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 332 | |
Paul Gortmaker | 21f5850 | 2014-04-29 15:25:17 -0400 | [diff] [blame] | 333 | __set_breakpoint(info); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 334 | current->thread.last_hit_ubp = NULL; |
Paul Mackerras | 76b0f13 | 2010-06-23 15:46:55 +1000 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * If the process was being single-stepped by ptrace, let the |
| 338 | * other single-step actions occur (e.g. generate SIGTRAP). |
| 339 | */ |
| 340 | if (test_thread_flag(TIF_SINGLESTEP)) |
| 341 | return NOTIFY_DONE; |
| 342 | |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 343 | return NOTIFY_STOP; |
| 344 | } |
Nicholas Piggin | 03465f8 | 2016-09-16 20:48:08 +1000 | [diff] [blame] | 345 | NOKPROBE_SYMBOL(single_step_dabr_instruction); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * Handle debug exception notifications. |
| 349 | */ |
Nicholas Piggin | 03465f8 | 2016-09-16 20:48:08 +1000 | [diff] [blame] | 350 | int hw_breakpoint_exceptions_notify( |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 351 | struct notifier_block *unused, unsigned long val, void *data) |
| 352 | { |
| 353 | int ret = NOTIFY_DONE; |
| 354 | |
| 355 | switch (val) { |
| 356 | case DIE_DABR_MATCH: |
| 357 | ret = hw_breakpoint_handler(data); |
| 358 | break; |
| 359 | case DIE_SSTEP: |
| 360 | ret = single_step_dabr_instruction(data); |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | return ret; |
| 365 | } |
Nicholas Piggin | 03465f8 | 2016-09-16 20:48:08 +1000 | [diff] [blame] | 366 | NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify); |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * Release the user breakpoints used by ptrace |
| 370 | */ |
| 371 | void flush_ptrace_hw_breakpoint(struct task_struct *tsk) |
| 372 | { |
| 373 | struct thread_struct *t = &tsk->thread; |
| 374 | |
| 375 | unregister_hw_breakpoint(t->ptrace_bps[0]); |
| 376 | t->ptrace_bps[0] = NULL; |
| 377 | } |
| 378 | |
| 379 | void hw_breakpoint_pmu_read(struct perf_event *bp) |
| 380 | { |
| 381 | /* TODO */ |
| 382 | } |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame^] | 383 | |
| 384 | bool dawr_force_enable; |
| 385 | EXPORT_SYMBOL_GPL(dawr_force_enable); |
| 386 | |
| 387 | static ssize_t dawr_write_file_bool(struct file *file, |
| 388 | const char __user *user_buf, |
| 389 | size_t count, loff_t *ppos) |
| 390 | { |
| 391 | struct arch_hw_breakpoint null_brk = {0, 0, 0}; |
| 392 | size_t rc; |
| 393 | |
| 394 | /* Send error to user if they hypervisor won't allow us to write DAWR */ |
| 395 | if ((!dawr_force_enable) && |
| 396 | (firmware_has_feature(FW_FEATURE_LPAR)) && |
| 397 | (set_dawr(&null_brk) != H_SUCCESS)) |
| 398 | return -1; |
| 399 | |
| 400 | rc = debugfs_write_file_bool(file, user_buf, count, ppos); |
| 401 | if (rc) |
| 402 | return rc; |
| 403 | |
| 404 | /* If we are clearing, make sure all CPUs have the DAWR cleared */ |
| 405 | if (!dawr_force_enable) |
| 406 | smp_call_function((smp_call_func_t)set_dawr, &null_brk, 0); |
| 407 | |
| 408 | return rc; |
| 409 | } |
| 410 | |
| 411 | static const struct file_operations dawr_enable_fops = { |
| 412 | .read = debugfs_read_file_bool, |
| 413 | .write = dawr_write_file_bool, |
| 414 | .open = simple_open, |
| 415 | .llseek = default_llseek, |
| 416 | }; |
| 417 | |
| 418 | static int __init dawr_force_setup(void) |
| 419 | { |
| 420 | dawr_force_enable = false; |
| 421 | |
| 422 | if (cpu_has_feature(CPU_FTR_DAWR)) { |
| 423 | /* Don't setup sysfs file for user control on P8 */ |
| 424 | dawr_force_enable = true; |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | if (PVR_VER(mfspr(SPRN_PVR)) == PVR_POWER9) { |
| 429 | /* Turn DAWR off by default, but allow admin to turn it on */ |
| 430 | dawr_force_enable = false; |
| 431 | debugfs_create_file_unsafe("dawr_enable_dangerous", 0600, |
| 432 | powerpc_debugfs_root, |
| 433 | &dawr_force_enable, |
| 434 | &dawr_enable_fops); |
| 435 | } |
| 436 | return 0; |
| 437 | } |
| 438 | arch_initcall(dawr_force_setup); |