blob: 899bcec3f3c8431619058ddc9bcec3f88befd000 [file] [log] [blame]
K.Prasad5aae8a52010-06-15 11:35:19 +05301/*
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.Prasad5aae8a52010-06-15 11:35:19 +053030#include <linux/sched.h>
K.Prasad5aae8a52010-06-15 11:35:19 +053031#include <linux/smp.h>
32
33#include <asm/hw_breakpoint.h>
34#include <asm/processor.h>
35#include <asm/sstep.h>
Michael Neuling85ce9a52018-03-27 15:37:18 +110036#include <asm/debug.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080037#include <linux/uaccess.h>
K.Prasad5aae8a52010-06-15 11:35:19 +053038
39/*
40 * Stores the breakpoints currently in use on each breakpoint address
41 * register for every cpu
42 */
43static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
44
45/*
Paul Mackerrasd09ec732010-06-29 12:50:32 +100046 * Returns total number of data or instruction breakpoints available.
47 */
48int hw_breakpoint_slots(int type)
49{
50 if (type == TYPE_DATA)
51 return HBP_NUM;
52 return 0; /* no instruction breakpoints available */
53}
54
55/*
K.Prasad5aae8a52010-06-15 11:35:19 +053056 * Install a perf counter breakpoint.
57 *
58 * We seek a free debug address register and use it for this
59 * breakpoint.
60 *
61 * Atomic: we hold the counter->ctx->lock and we only handle variables
62 * and registers local to this cpu.
63 */
64int arch_install_hw_breakpoint(struct perf_event *bp)
65{
66 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
Christoph Lameter69111ba2014-10-21 15:23:25 -050067 struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
K.Prasad5aae8a52010-06-15 11:35:19 +053068
69 *slot = bp;
70
71 /*
72 * Do not install DABR values if the instruction must be single-stepped.
73 * If so, DABR will be populated in single_step_dabr_instruction().
74 */
75 if (current->thread.last_hit_ubp != bp)
Paul Gortmaker21f58502014-04-29 15:25:17 -040076 __set_breakpoint(info);
K.Prasad5aae8a52010-06-15 11:35:19 +053077
78 return 0;
79}
80
81/*
82 * Uninstall the breakpoint contained in the given counter.
83 *
84 * First we search the debug address register it uses and then we disable
85 * it.
86 *
87 * Atomic: we hold the counter->ctx->lock and we only handle variables
88 * and registers local to this cpu.
89 */
90void arch_uninstall_hw_breakpoint(struct perf_event *bp)
91{
Christoph Lameter69111ba2014-10-21 15:23:25 -050092 struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
K.Prasad5aae8a52010-06-15 11:35:19 +053093
94 if (*slot != bp) {
95 WARN_ONCE(1, "Can't find the breakpoint");
96 return;
97 }
98
99 *slot = NULL;
Michael Neuling9422de32012-12-20 14:06:44 +0000100 hw_breakpoint_disable();
K.Prasad5aae8a52010-06-15 11:35:19 +0530101}
102
103/*
104 * Perform cleanup of arch-specific counters during unregistration
105 * of the perf-event
106 */
107void arch_unregister_hw_breakpoint(struct perf_event *bp)
108{
109 /*
110 * If the breakpoint is unregistered between a hw_breakpoint_handler()
111 * and the single_step_dabr_instruction(), then cleanup the breakpoint
112 * restoration variables to prevent dangling pointers.
Ravi Bangoriafb822e62016-03-02 15:25:17 +0530113 * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
K.Prasad5aae8a52010-06-15 11:35:19 +0530114 */
Ravi Bangoriafb822e62016-03-02 15:25:17 +0530115 if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
K.Prasad5aae8a52010-06-15 11:35:19 +0530116 bp->ctx->task->thread.last_hit_ubp = NULL;
117}
118
119/*
120 * Check for virtual address in kernel space.
121 */
Frederic Weisbecker8e983ff2018-06-26 04:58:49 +0200122int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
K.Prasad5aae8a52010-06-15 11:35:19 +0530123{
Frederic Weisbecker8e983ff2018-06-26 04:58:49 +0200124 return is_kernel_addr(hw->address);
K.Prasad5aae8a52010-06-15 11:35:19 +0530125}
126
127int arch_bp_generic_fields(int type, int *gen_bp_type)
128{
Michael Neuling9422de32012-12-20 14:06:44 +0000129 *gen_bp_type = 0;
130 if (type & HW_BRK_TYPE_READ)
131 *gen_bp_type |= HW_BREAKPOINT_R;
132 if (type & HW_BRK_TYPE_WRITE)
133 *gen_bp_type |= HW_BREAKPOINT_W;
134 if (*gen_bp_type == 0)
K.Prasad5aae8a52010-06-15 11:35:19 +0530135 return -EINVAL;
K.Prasad5aae8a52010-06-15 11:35:19 +0530136 return 0;
137}
138
139/*
140 * Validate the arch-specific HW Breakpoint register settings
141 */
142int arch_validate_hwbkpt_settings(struct perf_event *bp)
143{
Michael Neuling4ae7ebe2013-01-24 15:02:59 +0000144 int ret = -EINVAL, length_max;
K.Prasad5aae8a52010-06-15 11:35:19 +0530145 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
146
147 if (!bp)
148 return ret;
149
Michael Neuling9422de32012-12-20 14:06:44 +0000150 info->type = HW_BRK_TYPE_TRANSLATE;
151 if (bp->attr.bp_type & HW_BREAKPOINT_R)
152 info->type |= HW_BRK_TYPE_READ;
153 if (bp->attr.bp_type & HW_BREAKPOINT_W)
154 info->type |= HW_BRK_TYPE_WRITE;
155 if (info->type == HW_BRK_TYPE_TRANSLATE)
156 /* must set alteast read or write */
K.Prasad5aae8a52010-06-15 11:35:19 +0530157 return ret;
Michael Neuling9422de32012-12-20 14:06:44 +0000158 if (!(bp->attr.exclude_user))
159 info->type |= HW_BRK_TYPE_USER;
160 if (!(bp->attr.exclude_kernel))
161 info->type |= HW_BRK_TYPE_KERNEL;
162 if (!(bp->attr.exclude_hv))
163 info->type |= HW_BRK_TYPE_HYP;
K.Prasad5aae8a52010-06-15 11:35:19 +0530164 info->address = bp->attr.bp_addr;
165 info->len = bp->attr.bp_len;
166
167 /*
168 * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8)
169 * and breakpoint addresses are aligned to nearest double-word
170 * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the
171 * 'symbolsize' should satisfy the check below.
172 */
Michael Neuling85ce9a52018-03-27 15:37:18 +1100173 if (!ppc_breakpoint_available())
174 return -ENODEV;
Michael Neuling4ae7ebe2013-01-24 15:02:59 +0000175 length_max = 8; /* DABR */
176 if (cpu_has_feature(CPU_FTR_DAWR)) {
177 length_max = 512 ; /* 64 doublewords */
178 /* DAWR region can't cross 512 boundary */
Michael Neulingcd6ef7e2018-05-17 15:37:14 +1000179 if ((bp->attr.bp_addr >> 9) !=
180 ((bp->attr.bp_addr + bp->attr.bp_len - 1) >> 9))
Michael Neuling4ae7ebe2013-01-24 15:02:59 +0000181 return -EINVAL;
182 }
K.Prasad5aae8a52010-06-15 11:35:19 +0530183 if (info->len >
Michael Neuling4ae7ebe2013-01-24 15:02:59 +0000184 (length_max - (info->address & HW_BREAKPOINT_ALIGN)))
K.Prasad5aae8a52010-06-15 11:35:19 +0530185 return -EINVAL;
186 return 0;
187}
188
189/*
K.Prasad06532a62010-06-15 11:35:41 +0530190 * Restores the breakpoint on the debug registers.
191 * Invoke this function if it is known that the execution context is
192 * about to change to cause loss of MSR_SE settings.
193 */
194void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
195{
196 struct arch_hw_breakpoint *info;
197
198 if (likely(!tsk->thread.last_hit_ubp))
199 return;
200
201 info = counter_arch_bp(tsk->thread.last_hit_ubp);
202 regs->msr &= ~MSR_SE;
Paul Gortmaker21f58502014-04-29 15:25:17 -0400203 __set_breakpoint(info);
K.Prasad06532a62010-06-15 11:35:41 +0530204 tsk->thread.last_hit_ubp = NULL;
205}
206
207/*
K.Prasad5aae8a52010-06-15 11:35:19 +0530208 * Handle debug exception notifications.
209 */
Nicholas Piggin03465f82016-09-16 20:48:08 +1000210int hw_breakpoint_handler(struct die_args *args)
K.Prasad5aae8a52010-06-15 11:35:19 +0530211{
K.Prasad5aae8a52010-06-15 11:35:19 +0530212 int rc = NOTIFY_STOP;
213 struct perf_event *bp;
214 struct pt_regs *regs = args->regs;
Christophe Leroy4ad86222016-11-29 09:52:15 +0100215#ifndef CONFIG_PPC_8xx
K.Prasad5aae8a52010-06-15 11:35:19 +0530216 int stepped = 1;
K.Prasad5aae8a52010-06-15 11:35:19 +0530217 unsigned int instr;
Christophe Leroy4ad86222016-11-29 09:52:15 +0100218#endif
219 struct arch_hw_breakpoint *info;
K.Prasade3e94082010-06-15 11:36:12 +0530220 unsigned long dar = regs->dar;
K.Prasad5aae8a52010-06-15 11:35:19 +0530221
222 /* Disable breakpoints during exception handling */
Michael Neuling9422de32012-12-20 14:06:44 +0000223 hw_breakpoint_disable();
Paul Mackerras574cb242010-06-23 15:42:43 +1000224
K.Prasad5aae8a52010-06-15 11:35:19 +0530225 /*
226 * The counter may be concurrently released but that can only
227 * occur from a call_rcu() path. We can then safely fetch
228 * the breakpoint, use its callback, touch its counter
229 * while we are in an rcu_read_lock() path.
230 */
231 rcu_read_lock();
232
Christoph Lameter69111ba2014-10-21 15:23:25 -0500233 bp = __this_cpu_read(bp_per_reg);
Ravi Bangoriac21a4932016-11-22 14:55:59 +0530234 if (!bp) {
235 rc = NOTIFY_DONE;
K.Prasad5aae8a52010-06-15 11:35:19 +0530236 goto out;
Ravi Bangoriac21a4932016-11-22 14:55:59 +0530237 }
K.Prasad5aae8a52010-06-15 11:35:19 +0530238 info = counter_arch_bp(bp);
K.Prasad5aae8a52010-06-15 11:35:19 +0530239
240 /*
241 * Return early after invoking user-callback function without restoring
242 * DABR if the breakpoint is from ptrace which always operates in
243 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
244 * generated in do_dabr().
245 */
Paul Mackerras574cb242010-06-23 15:42:43 +1000246 if (bp->overflow_handler == ptrace_triggered) {
K.Prasad5aae8a52010-06-15 11:35:19 +0530247 perf_bp_event(bp, regs);
248 rc = NOTIFY_DONE;
249 goto out;
250 }
251
K.Prasade3e94082010-06-15 11:36:12 +0530252 /*
253 * Verify if dar lies within the address range occupied by the symbol
Paul Mackerras574cb242010-06-23 15:42:43 +1000254 * being watched to filter extraneous exceptions. If it doesn't,
255 * we still need to single-step the instruction, but we don't
256 * generate an event.
K.Prasade3e94082010-06-15 11:36:12 +0530257 */
Michael Neuling540e07c2013-06-24 15:47:23 +1000258 info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
Michael Neuling9422de32012-12-20 14:06:44 +0000259 if (!((bp->attr.bp_addr <= dar) &&
260 (dar - bp->attr.bp_addr < bp->attr.bp_len)))
261 info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
K.Prasade3e94082010-06-15 11:36:12 +0530262
Christophe Leroy4ad86222016-11-29 09:52:15 +0100263#ifndef CONFIG_PPC_8xx
K.Prasad5aae8a52010-06-15 11:35:19 +0530264 /* Do not emulate user-space instructions, instead single-step them */
265 if (user_mode(regs)) {
Michael Neuling6d9c00c2012-08-22 20:30:43 +0000266 current->thread.last_hit_ubp = bp;
K.Prasad5aae8a52010-06-15 11:35:19 +0530267 regs->msr |= MSR_SE;
268 goto out;
269 }
270
271 stepped = 0;
272 instr = 0;
273 if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
274 stepped = emulate_step(regs, instr);
275
276 /*
277 * emulate_step() could not execute it. We've failed in reliably
278 * handling the hw-breakpoint. Unregister it and throw a warning
279 * message to let the user know about it.
280 */
281 if (!stepped) {
282 WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
283 "0x%lx will be disabled.", info->address);
Jiri Olsa5aab90c2016-10-26 11:48:24 +0200284 perf_event_disable_inatomic(bp);
K.Prasad5aae8a52010-06-15 11:35:19 +0530285 goto out;
286 }
Christophe Leroy4ad86222016-11-29 09:52:15 +0100287#endif
K.Prasad5aae8a52010-06-15 11:35:19 +0530288 /*
289 * As a policy, the callback is invoked in a 'trigger-after-execute'
290 * fashion
291 */
Michael Neuling9422de32012-12-20 14:06:44 +0000292 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
K.Prasade3e94082010-06-15 11:36:12 +0530293 perf_bp_event(bp, regs);
K.Prasad5aae8a52010-06-15 11:35:19 +0530294
Paul Gortmaker21f58502014-04-29 15:25:17 -0400295 __set_breakpoint(info);
K.Prasad5aae8a52010-06-15 11:35:19 +0530296out:
297 rcu_read_unlock();
298 return rc;
299}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000300NOKPROBE_SYMBOL(hw_breakpoint_handler);
K.Prasad5aae8a52010-06-15 11:35:19 +0530301
302/*
303 * Handle single-step exceptions following a DABR hit.
304 */
Nicholas Piggin03465f82016-09-16 20:48:08 +1000305static int single_step_dabr_instruction(struct die_args *args)
K.Prasad5aae8a52010-06-15 11:35:19 +0530306{
307 struct pt_regs *regs = args->regs;
308 struct perf_event *bp = NULL;
Michael Neuling3f4693e2012-09-05 19:17:48 +0000309 struct arch_hw_breakpoint *info;
K.Prasad5aae8a52010-06-15 11:35:19 +0530310
311 bp = current->thread.last_hit_ubp;
312 /*
313 * Check if we are single-stepping as a result of a
314 * previous HW Breakpoint exception
315 */
316 if (!bp)
317 return NOTIFY_DONE;
318
Michael Neuling3f4693e2012-09-05 19:17:48 +0000319 info = counter_arch_bp(bp);
K.Prasad5aae8a52010-06-15 11:35:19 +0530320
321 /*
322 * We shall invoke the user-defined callback function in the single
323 * stepping handler to confirm to 'trigger-after-execute' semantics
324 */
Michael Neuling9422de32012-12-20 14:06:44 +0000325 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
K.Prasade3e94082010-06-15 11:36:12 +0530326 perf_bp_event(bp, regs);
K.Prasad5aae8a52010-06-15 11:35:19 +0530327
Paul Gortmaker21f58502014-04-29 15:25:17 -0400328 __set_breakpoint(info);
K.Prasad5aae8a52010-06-15 11:35:19 +0530329 current->thread.last_hit_ubp = NULL;
Paul Mackerras76b0f132010-06-23 15:46:55 +1000330
331 /*
332 * If the process was being single-stepped by ptrace, let the
333 * other single-step actions occur (e.g. generate SIGTRAP).
334 */
335 if (test_thread_flag(TIF_SINGLESTEP))
336 return NOTIFY_DONE;
337
K.Prasad5aae8a52010-06-15 11:35:19 +0530338 return NOTIFY_STOP;
339}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000340NOKPROBE_SYMBOL(single_step_dabr_instruction);
K.Prasad5aae8a52010-06-15 11:35:19 +0530341
342/*
343 * Handle debug exception notifications.
344 */
Nicholas Piggin03465f82016-09-16 20:48:08 +1000345int hw_breakpoint_exceptions_notify(
K.Prasad5aae8a52010-06-15 11:35:19 +0530346 struct notifier_block *unused, unsigned long val, void *data)
347{
348 int ret = NOTIFY_DONE;
349
350 switch (val) {
351 case DIE_DABR_MATCH:
352 ret = hw_breakpoint_handler(data);
353 break;
354 case DIE_SSTEP:
355 ret = single_step_dabr_instruction(data);
356 break;
357 }
358
359 return ret;
360}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000361NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
K.Prasad5aae8a52010-06-15 11:35:19 +0530362
363/*
364 * Release the user breakpoints used by ptrace
365 */
366void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
367{
368 struct thread_struct *t = &tsk->thread;
369
370 unregister_hw_breakpoint(t->ptrace_bps[0]);
371 t->ptrace_bps[0] = NULL;
372}
373
374void hw_breakpoint_pmu_read(struct perf_event *bp)
375{
376 /* TODO */
377}