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"
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.
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.
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.
20 * Copyright 2010 IBM Corporation
21 * Author: K.Prasad <prasad@linux.vnet.ibm.com>
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>
30 #include <linux/sched.h>
31 #include <linux/smp.h>
32 #include <linux/debugfs.h>
33 #include <linux/init.h>
35 #include <asm/hw_breakpoint.h>
36 #include <asm/processor.h>
37 #include <asm/sstep.h>
38 #include <asm/debug.h>
39 #include <asm/debugfs.h>
40 #include <asm/hvcall.h>
41 #include <linux/uaccess.h>
44 * Stores the breakpoints currently in use on each breakpoint address
45 * register for every cpu
47 static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
50 * Returns total number of data or instruction breakpoints available.
52 int hw_breakpoint_slots(int type)
54 if (type == TYPE_DATA)
56 return 0; /* no instruction breakpoints available */
60 * Install a perf counter breakpoint.
62 * We seek a free debug address register and use it for this
65 * Atomic: we hold the counter->ctx->lock and we only handle variables
66 * and registers local to this cpu.
68 int arch_install_hw_breakpoint(struct perf_event *bp)
70 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
71 struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
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().
79 if (current->thread.last_hit_ubp != bp)
80 __set_breakpoint(info);
86 * Uninstall the breakpoint contained in the given counter.
88 * First we search the debug address register it uses and then we disable
91 * Atomic: we hold the counter->ctx->lock and we only handle variables
92 * and registers local to this cpu.
94 void arch_uninstall_hw_breakpoint(struct perf_event *bp)
96 struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
99 WARN_ONCE(1, "Can't find the breakpoint");
104 hw_breakpoint_disable();
108 * Perform cleanup of arch-specific counters during unregistration
111 void arch_unregister_hw_breakpoint(struct perf_event *bp)
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.
117 * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
119 if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
120 bp->ctx->task->thread.last_hit_ubp = NULL;
124 * Check for virtual address in kernel space.
126 int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
128 return is_kernel_addr(hw->address);
131 int arch_bp_generic_fields(int type, int *gen_bp_type)
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)
144 * Validate the arch-specific HW Breakpoint register settings
146 int hw_breakpoint_arch_parse(struct perf_event *bp,
147 const struct perf_event_attr *attr,
148 struct arch_hw_breakpoint *hw)
150 int ret = -EINVAL, length_max;
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)
161 /* must set alteast read or write */
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;
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.
178 if (!ppc_breakpoint_available())
180 length_max = 8; /* DABR */
181 if (dawr_enabled()) {
182 length_max = 512 ; /* 64 doublewords */
183 /* DAWR region can't cross 512 boundary */
184 if ((attr->bp_addr >> 9) !=
185 ((attr->bp_addr + attr->bp_len - 1) >> 9))
189 (length_max - (hw->address & HW_BREAKPOINT_ALIGN)))
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.
199 void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
201 struct arch_hw_breakpoint *info;
203 if (likely(!tsk->thread.last_hit_ubp))
206 info = counter_arch_bp(tsk->thread.last_hit_ubp);
207 regs->msr &= ~MSR_SE;
208 __set_breakpoint(info);
209 tsk->thread.last_hit_ubp = NULL;
213 * Handle debug exception notifications.
215 int hw_breakpoint_handler(struct die_args *args)
217 int rc = NOTIFY_STOP;
218 struct perf_event *bp;
219 struct pt_regs *regs = args->regs;
220 #ifndef CONFIG_PPC_8xx
224 struct arch_hw_breakpoint *info;
225 unsigned long dar = regs->dar;
227 /* Disable breakpoints during exception handling */
228 hw_breakpoint_disable();
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.
238 bp = __this_cpu_read(bp_per_reg);
243 info = counter_arch_bp(bp);
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().
251 if (bp->overflow_handler == ptrace_triggered) {
252 perf_bp_event(bp, regs);
258 * Verify if dar lies within the address range occupied by the symbol
259 * being watched to filter extraneous exceptions. If it doesn't,
260 * we still need to single-step the instruction, but we don't
263 info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
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;
268 #ifndef CONFIG_PPC_8xx
269 /* Do not emulate user-space instructions, instead single-step them */
270 if (user_mode(regs)) {
271 current->thread.last_hit_ubp = bp;
278 if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
279 stepped = emulate_step(regs, instr);
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.
287 WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
288 "0x%lx will be disabled.", info->address);
289 perf_event_disable_inatomic(bp);
294 * As a policy, the callback is invoked in a 'trigger-after-execute'
297 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
298 perf_bp_event(bp, regs);
300 __set_breakpoint(info);
305 NOKPROBE_SYMBOL(hw_breakpoint_handler);
308 * Handle single-step exceptions following a DABR hit.
310 static int single_step_dabr_instruction(struct die_args *args)
312 struct pt_regs *regs = args->regs;
313 struct perf_event *bp = NULL;
314 struct arch_hw_breakpoint *info;
316 bp = current->thread.last_hit_ubp;
318 * Check if we are single-stepping as a result of a
319 * previous HW Breakpoint exception
324 info = counter_arch_bp(bp);
327 * We shall invoke the user-defined callback function in the single
328 * stepping handler to confirm to 'trigger-after-execute' semantics
330 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
331 perf_bp_event(bp, regs);
333 __set_breakpoint(info);
334 current->thread.last_hit_ubp = NULL;
337 * If the process was being single-stepped by ptrace, let the
338 * other single-step actions occur (e.g. generate SIGTRAP).
340 if (test_thread_flag(TIF_SINGLESTEP))
345 NOKPROBE_SYMBOL(single_step_dabr_instruction);
348 * Handle debug exception notifications.
350 int hw_breakpoint_exceptions_notify(
351 struct notifier_block *unused, unsigned long val, void *data)
353 int ret = NOTIFY_DONE;
357 ret = hw_breakpoint_handler(data);
360 ret = single_step_dabr_instruction(data);
366 NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
369 * Release the user breakpoints used by ptrace
371 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
373 struct thread_struct *t = &tsk->thread;
375 unregister_hw_breakpoint(t->ptrace_bps[0]);
376 t->ptrace_bps[0] = NULL;
379 void hw_breakpoint_pmu_read(struct perf_event *bp)
384 bool dawr_force_enable;
385 EXPORT_SYMBOL_GPL(dawr_force_enable);
387 static ssize_t dawr_write_file_bool(struct file *file,
388 const char __user *user_buf,
389 size_t count, loff_t *ppos)
391 struct arch_hw_breakpoint null_brk = {0, 0, 0};
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))
400 rc = debugfs_write_file_bool(file, user_buf, count, ppos);
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);
411 static const struct file_operations dawr_enable_fops = {
412 .read = debugfs_read_file_bool,
413 .write = dawr_write_file_bool,
415 .llseek = default_llseek,
418 static int __init dawr_force_setup(void)
420 dawr_force_enable = false;
422 if (cpu_has_feature(CPU_FTR_DAWR)) {
423 /* Don't setup sysfs file for user control on P8 */
424 dawr_force_enable = true;
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,
438 arch_initcall(dawr_force_setup);