blob: 146eaa9b350e37cef38f584b47611446460e2050 [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080036#include <linux/uaccess.h>
K.Prasad5aae8a52010-06-15 11:35:19 +053037
38/*
39 * Stores the breakpoints currently in use on each breakpoint address
40 * register for every cpu
41 */
42static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
43
44/*
Paul Mackerrasd09ec732010-06-29 12:50:32 +100045 * Returns total number of data or instruction breakpoints available.
46 */
47int hw_breakpoint_slots(int type)
48{
49 if (type == TYPE_DATA)
50 return HBP_NUM;
51 return 0; /* no instruction breakpoints available */
52}
53
54/*
K.Prasad5aae8a52010-06-15 11:35:19 +053055 * Install a perf counter breakpoint.
56 *
57 * We seek a free debug address register and use it for this
58 * breakpoint.
59 *
60 * Atomic: we hold the counter->ctx->lock and we only handle variables
61 * and registers local to this cpu.
62 */
63int arch_install_hw_breakpoint(struct perf_event *bp)
64{
65 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
Christoph Lameter69111ba2014-10-21 15:23:25 -050066 struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
K.Prasad5aae8a52010-06-15 11:35:19 +053067
68 *slot = bp;
69
70 /*
71 * Do not install DABR values if the instruction must be single-stepped.
72 * If so, DABR will be populated in single_step_dabr_instruction().
73 */
74 if (current->thread.last_hit_ubp != bp)
Paul Gortmaker21f58502014-04-29 15:25:17 -040075 __set_breakpoint(info);
K.Prasad5aae8a52010-06-15 11:35:19 +053076
77 return 0;
78}
79
80/*
81 * Uninstall the breakpoint contained in the given counter.
82 *
83 * First we search the debug address register it uses and then we disable
84 * it.
85 *
86 * Atomic: we hold the counter->ctx->lock and we only handle variables
87 * and registers local to this cpu.
88 */
89void arch_uninstall_hw_breakpoint(struct perf_event *bp)
90{
Christoph Lameter69111ba2014-10-21 15:23:25 -050091 struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
K.Prasad5aae8a52010-06-15 11:35:19 +053092
93 if (*slot != bp) {
94 WARN_ONCE(1, "Can't find the breakpoint");
95 return;
96 }
97
98 *slot = NULL;
Michael Neuling9422de32012-12-20 14:06:44 +000099 hw_breakpoint_disable();
K.Prasad5aae8a52010-06-15 11:35:19 +0530100}
101
102/*
103 * Perform cleanup of arch-specific counters during unregistration
104 * of the perf-event
105 */
106void arch_unregister_hw_breakpoint(struct perf_event *bp)
107{
108 /*
109 * If the breakpoint is unregistered between a hw_breakpoint_handler()
110 * and the single_step_dabr_instruction(), then cleanup the breakpoint
111 * restoration variables to prevent dangling pointers.
Ravi Bangoriafb822e62016-03-02 15:25:17 +0530112 * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
K.Prasad5aae8a52010-06-15 11:35:19 +0530113 */
Ravi Bangoriafb822e62016-03-02 15:25:17 +0530114 if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
K.Prasad5aae8a52010-06-15 11:35:19 +0530115 bp->ctx->task->thread.last_hit_ubp = NULL;
116}
117
118/*
119 * Check for virtual address in kernel space.
120 */
121int arch_check_bp_in_kernelspace(struct perf_event *bp)
122{
123 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
124
125 return is_kernel_addr(info->address);
126}
127
128int arch_bp_generic_fields(int type, int *gen_bp_type)
129{
Michael Neuling9422de32012-12-20 14:06:44 +0000130 *gen_bp_type = 0;
131 if (type & HW_BRK_TYPE_READ)
132 *gen_bp_type |= HW_BREAKPOINT_R;
133 if (type & HW_BRK_TYPE_WRITE)
134 *gen_bp_type |= HW_BREAKPOINT_W;
135 if (*gen_bp_type == 0)
K.Prasad5aae8a52010-06-15 11:35:19 +0530136 return -EINVAL;
K.Prasad5aae8a52010-06-15 11:35:19 +0530137 return 0;
138}
139
140/*
141 * Validate the arch-specific HW Breakpoint register settings
142 */
143int arch_validate_hwbkpt_settings(struct perf_event *bp)
144{
Michael Neuling4ae7ebe2013-01-24 15:02:59 +0000145 int ret = -EINVAL, length_max;
K.Prasad5aae8a52010-06-15 11:35:19 +0530146 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
147
148 if (!bp)
149 return ret;
150
Michael Neuling9422de32012-12-20 14:06:44 +0000151 info->type = HW_BRK_TYPE_TRANSLATE;
152 if (bp->attr.bp_type & HW_BREAKPOINT_R)
153 info->type |= HW_BRK_TYPE_READ;
154 if (bp->attr.bp_type & HW_BREAKPOINT_W)
155 info->type |= HW_BRK_TYPE_WRITE;
156 if (info->type == HW_BRK_TYPE_TRANSLATE)
157 /* must set alteast read or write */
K.Prasad5aae8a52010-06-15 11:35:19 +0530158 return ret;
Michael Neuling9422de32012-12-20 14:06:44 +0000159 if (!(bp->attr.exclude_user))
160 info->type |= HW_BRK_TYPE_USER;
161 if (!(bp->attr.exclude_kernel))
162 info->type |= HW_BRK_TYPE_KERNEL;
163 if (!(bp->attr.exclude_hv))
164 info->type |= HW_BRK_TYPE_HYP;
K.Prasad5aae8a52010-06-15 11:35:19 +0530165 info->address = bp->attr.bp_addr;
166 info->len = bp->attr.bp_len;
167
168 /*
169 * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8)
170 * and breakpoint addresses are aligned to nearest double-word
171 * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the
172 * 'symbolsize' should satisfy the check below.
173 */
Michael Neuling4ae7ebe2013-01-24 15:02:59 +0000174 length_max = 8; /* DABR */
175 if (cpu_has_feature(CPU_FTR_DAWR)) {
176 length_max = 512 ; /* 64 doublewords */
177 /* DAWR region can't cross 512 boundary */
178 if ((bp->attr.bp_addr >> 10) !=
Michael Neulinge2a800b2013-07-01 14:19:50 +1000179 ((bp->attr.bp_addr + bp->attr.bp_len - 1) >> 10))
Michael Neuling4ae7ebe2013-01-24 15:02:59 +0000180 return -EINVAL;
181 }
K.Prasad5aae8a52010-06-15 11:35:19 +0530182 if (info->len >
Michael Neuling4ae7ebe2013-01-24 15:02:59 +0000183 (length_max - (info->address & HW_BREAKPOINT_ALIGN)))
K.Prasad5aae8a52010-06-15 11:35:19 +0530184 return -EINVAL;
185 return 0;
186}
187
188/*
K.Prasad06532a62010-06-15 11:35:41 +0530189 * Restores the breakpoint on the debug registers.
190 * Invoke this function if it is known that the execution context is
191 * about to change to cause loss of MSR_SE settings.
192 */
193void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
194{
195 struct arch_hw_breakpoint *info;
196
197 if (likely(!tsk->thread.last_hit_ubp))
198 return;
199
200 info = counter_arch_bp(tsk->thread.last_hit_ubp);
201 regs->msr &= ~MSR_SE;
Paul Gortmaker21f58502014-04-29 15:25:17 -0400202 __set_breakpoint(info);
K.Prasad06532a62010-06-15 11:35:41 +0530203 tsk->thread.last_hit_ubp = NULL;
204}
205
206/*
K.Prasad5aae8a52010-06-15 11:35:19 +0530207 * Handle debug exception notifications.
208 */
Nicholas Piggin03465f82016-09-16 20:48:08 +1000209int hw_breakpoint_handler(struct die_args *args)
K.Prasad5aae8a52010-06-15 11:35:19 +0530210{
K.Prasad5aae8a52010-06-15 11:35:19 +0530211 int rc = NOTIFY_STOP;
212 struct perf_event *bp;
213 struct pt_regs *regs = args->regs;
Christophe Leroy4ad86222016-11-29 09:52:15 +0100214#ifndef CONFIG_PPC_8xx
K.Prasad5aae8a52010-06-15 11:35:19 +0530215 int stepped = 1;
K.Prasad5aae8a52010-06-15 11:35:19 +0530216 unsigned int instr;
Christophe Leroy4ad86222016-11-29 09:52:15 +0100217#endif
218 struct arch_hw_breakpoint *info;
K.Prasade3e94082010-06-15 11:36:12 +0530219 unsigned long dar = regs->dar;
K.Prasad5aae8a52010-06-15 11:35:19 +0530220
221 /* Disable breakpoints during exception handling */
Michael Neuling9422de32012-12-20 14:06:44 +0000222 hw_breakpoint_disable();
Paul Mackerras574cb242010-06-23 15:42:43 +1000223
K.Prasad5aae8a52010-06-15 11:35:19 +0530224 /*
225 * The counter may be concurrently released but that can only
226 * occur from a call_rcu() path. We can then safely fetch
227 * the breakpoint, use its callback, touch its counter
228 * while we are in an rcu_read_lock() path.
229 */
230 rcu_read_lock();
231
Christoph Lameter69111ba2014-10-21 15:23:25 -0500232 bp = __this_cpu_read(bp_per_reg);
K.Prasad5aae8a52010-06-15 11:35:19 +0530233 if (!bp)
234 goto out;
235 info = counter_arch_bp(bp);
K.Prasad5aae8a52010-06-15 11:35:19 +0530236
237 /*
238 * Return early after invoking user-callback function without restoring
239 * DABR if the breakpoint is from ptrace which always operates in
240 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
241 * generated in do_dabr().
242 */
Paul Mackerras574cb242010-06-23 15:42:43 +1000243 if (bp->overflow_handler == ptrace_triggered) {
K.Prasad5aae8a52010-06-15 11:35:19 +0530244 perf_bp_event(bp, regs);
245 rc = NOTIFY_DONE;
246 goto out;
247 }
248
K.Prasade3e94082010-06-15 11:36:12 +0530249 /*
250 * Verify if dar lies within the address range occupied by the symbol
Paul Mackerras574cb242010-06-23 15:42:43 +1000251 * being watched to filter extraneous exceptions. If it doesn't,
252 * we still need to single-step the instruction, but we don't
253 * generate an event.
K.Prasade3e94082010-06-15 11:36:12 +0530254 */
Michael Neuling540e07c2013-06-24 15:47:23 +1000255 info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
Michael Neuling9422de32012-12-20 14:06:44 +0000256 if (!((bp->attr.bp_addr <= dar) &&
257 (dar - bp->attr.bp_addr < bp->attr.bp_len)))
258 info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
K.Prasade3e94082010-06-15 11:36:12 +0530259
Christophe Leroy4ad86222016-11-29 09:52:15 +0100260#ifndef CONFIG_PPC_8xx
K.Prasad5aae8a52010-06-15 11:35:19 +0530261 /* Do not emulate user-space instructions, instead single-step them */
262 if (user_mode(regs)) {
Michael Neuling6d9c00c2012-08-22 20:30:43 +0000263 current->thread.last_hit_ubp = bp;
K.Prasad5aae8a52010-06-15 11:35:19 +0530264 regs->msr |= MSR_SE;
265 goto out;
266 }
267
268 stepped = 0;
269 instr = 0;
270 if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
271 stepped = emulate_step(regs, instr);
272
273 /*
274 * emulate_step() could not execute it. We've failed in reliably
275 * handling the hw-breakpoint. Unregister it and throw a warning
276 * message to let the user know about it.
277 */
278 if (!stepped) {
279 WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
280 "0x%lx will be disabled.", info->address);
Jiri Olsa5aab90c2016-10-26 11:48:24 +0200281 perf_event_disable_inatomic(bp);
K.Prasad5aae8a52010-06-15 11:35:19 +0530282 goto out;
283 }
Christophe Leroy4ad86222016-11-29 09:52:15 +0100284#endif
K.Prasad5aae8a52010-06-15 11:35:19 +0530285 /*
286 * As a policy, the callback is invoked in a 'trigger-after-execute'
287 * fashion
288 */
Michael Neuling9422de32012-12-20 14:06:44 +0000289 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
K.Prasade3e94082010-06-15 11:36:12 +0530290 perf_bp_event(bp, regs);
K.Prasad5aae8a52010-06-15 11:35:19 +0530291
Paul Gortmaker21f58502014-04-29 15:25:17 -0400292 __set_breakpoint(info);
K.Prasad5aae8a52010-06-15 11:35:19 +0530293out:
294 rcu_read_unlock();
295 return rc;
296}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000297NOKPROBE_SYMBOL(hw_breakpoint_handler);
K.Prasad5aae8a52010-06-15 11:35:19 +0530298
299/*
300 * Handle single-step exceptions following a DABR hit.
301 */
Nicholas Piggin03465f82016-09-16 20:48:08 +1000302static int single_step_dabr_instruction(struct die_args *args)
K.Prasad5aae8a52010-06-15 11:35:19 +0530303{
304 struct pt_regs *regs = args->regs;
305 struct perf_event *bp = NULL;
Michael Neuling3f4693e2012-09-05 19:17:48 +0000306 struct arch_hw_breakpoint *info;
K.Prasad5aae8a52010-06-15 11:35:19 +0530307
308 bp = current->thread.last_hit_ubp;
309 /*
310 * Check if we are single-stepping as a result of a
311 * previous HW Breakpoint exception
312 */
313 if (!bp)
314 return NOTIFY_DONE;
315
Michael Neuling3f4693e2012-09-05 19:17:48 +0000316 info = counter_arch_bp(bp);
K.Prasad5aae8a52010-06-15 11:35:19 +0530317
318 /*
319 * We shall invoke the user-defined callback function in the single
320 * stepping handler to confirm to 'trigger-after-execute' semantics
321 */
Michael Neuling9422de32012-12-20 14:06:44 +0000322 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
K.Prasade3e94082010-06-15 11:36:12 +0530323 perf_bp_event(bp, regs);
K.Prasad5aae8a52010-06-15 11:35:19 +0530324
Paul Gortmaker21f58502014-04-29 15:25:17 -0400325 __set_breakpoint(info);
K.Prasad5aae8a52010-06-15 11:35:19 +0530326 current->thread.last_hit_ubp = NULL;
Paul Mackerras76b0f132010-06-23 15:46:55 +1000327
328 /*
329 * If the process was being single-stepped by ptrace, let the
330 * other single-step actions occur (e.g. generate SIGTRAP).
331 */
332 if (test_thread_flag(TIF_SINGLESTEP))
333 return NOTIFY_DONE;
334
K.Prasad5aae8a52010-06-15 11:35:19 +0530335 return NOTIFY_STOP;
336}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000337NOKPROBE_SYMBOL(single_step_dabr_instruction);
K.Prasad5aae8a52010-06-15 11:35:19 +0530338
339/*
340 * Handle debug exception notifications.
341 */
Nicholas Piggin03465f82016-09-16 20:48:08 +1000342int hw_breakpoint_exceptions_notify(
K.Prasad5aae8a52010-06-15 11:35:19 +0530343 struct notifier_block *unused, unsigned long val, void *data)
344{
345 int ret = NOTIFY_DONE;
346
347 switch (val) {
348 case DIE_DABR_MATCH:
349 ret = hw_breakpoint_handler(data);
350 break;
351 case DIE_SSTEP:
352 ret = single_step_dabr_instruction(data);
353 break;
354 }
355
356 return ret;
357}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000358NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
K.Prasad5aae8a52010-06-15 11:35:19 +0530359
360/*
361 * Release the user breakpoints used by ptrace
362 */
363void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
364{
365 struct thread_struct *t = &tsk->thread;
366
367 unregister_hw_breakpoint(t->ptrace_bps[0]);
368 t->ptrace_bps[0] = NULL;
369}
370
371void hw_breakpoint_pmu_read(struct perf_event *bp)
372{
373 /* TODO */
374}