xref: /openbmc/linux/arch/powerpc/kernel/hw_breakpoint.c (revision 39413ae009674c6ba745850515b551bbb9d6374b)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25aae8a53SK.Prasad /*
35aae8a53SK.Prasad  * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
45aae8a53SK.Prasad  * using the CPU's debug registers. Derived from
55aae8a53SK.Prasad  * "arch/x86/kernel/hw_breakpoint.c"
65aae8a53SK.Prasad  *
75aae8a53SK.Prasad  * Copyright 2010 IBM Corporation
85aae8a53SK.Prasad  * Author: K.Prasad <prasad@linux.vnet.ibm.com>
95aae8a53SK.Prasad  */
105aae8a53SK.Prasad 
115aae8a53SK.Prasad #include <linux/hw_breakpoint.h>
125aae8a53SK.Prasad #include <linux/notifier.h>
135aae8a53SK.Prasad #include <linux/kprobes.h>
145aae8a53SK.Prasad #include <linux/percpu.h>
155aae8a53SK.Prasad #include <linux/kernel.h>
165aae8a53SK.Prasad #include <linux/sched.h>
175aae8a53SK.Prasad #include <linux/smp.h>
18c1fe190cSMichael Neuling #include <linux/debugfs.h>
19c1fe190cSMichael Neuling #include <linux/init.h>
205aae8a53SK.Prasad 
215aae8a53SK.Prasad #include <asm/hw_breakpoint.h>
225aae8a53SK.Prasad #include <asm/processor.h>
235aae8a53SK.Prasad #include <asm/sstep.h>
2485ce9a5dSMichael Neuling #include <asm/debug.h>
25c1fe190cSMichael Neuling #include <asm/debugfs.h>
26c1fe190cSMichael Neuling #include <asm/hvcall.h>
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
285aae8a53SK.Prasad 
295aae8a53SK.Prasad /*
305aae8a53SK.Prasad  * Stores the breakpoints currently in use on each breakpoint address
315aae8a53SK.Prasad  * register for every cpu
325aae8a53SK.Prasad  */
335aae8a53SK.Prasad static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
345aae8a53SK.Prasad 
355aae8a53SK.Prasad /*
36d09ec738SPaul Mackerras  * Returns total number of data or instruction breakpoints available.
37d09ec738SPaul Mackerras  */
38d09ec738SPaul Mackerras int hw_breakpoint_slots(int type)
39d09ec738SPaul Mackerras {
40d09ec738SPaul Mackerras 	if (type == TYPE_DATA)
41d09ec738SPaul Mackerras 		return HBP_NUM;
42d09ec738SPaul Mackerras 	return 0;		/* no instruction breakpoints available */
43d09ec738SPaul Mackerras }
44d09ec738SPaul Mackerras 
45d09ec738SPaul Mackerras /*
465aae8a53SK.Prasad  * Install a perf counter breakpoint.
475aae8a53SK.Prasad  *
485aae8a53SK.Prasad  * We seek a free debug address register and use it for this
495aae8a53SK.Prasad  * breakpoint.
505aae8a53SK.Prasad  *
515aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
525aae8a53SK.Prasad  * and registers local to this cpu.
535aae8a53SK.Prasad  */
545aae8a53SK.Prasad int arch_install_hw_breakpoint(struct perf_event *bp)
555aae8a53SK.Prasad {
565aae8a53SK.Prasad 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
5769111bacSChristoph Lameter 	struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
585aae8a53SK.Prasad 
595aae8a53SK.Prasad 	*slot = bp;
605aae8a53SK.Prasad 
615aae8a53SK.Prasad 	/*
625aae8a53SK.Prasad 	 * Do not install DABR values if the instruction must be single-stepped.
635aae8a53SK.Prasad 	 * If so, DABR will be populated in single_step_dabr_instruction().
645aae8a53SK.Prasad 	 */
655aae8a53SK.Prasad 	if (current->thread.last_hit_ubp != bp)
6621f58507SPaul Gortmaker 		__set_breakpoint(info);
675aae8a53SK.Prasad 
685aae8a53SK.Prasad 	return 0;
695aae8a53SK.Prasad }
705aae8a53SK.Prasad 
715aae8a53SK.Prasad /*
725aae8a53SK.Prasad  * Uninstall the breakpoint contained in the given counter.
735aae8a53SK.Prasad  *
745aae8a53SK.Prasad  * First we search the debug address register it uses and then we disable
755aae8a53SK.Prasad  * it.
765aae8a53SK.Prasad  *
775aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
785aae8a53SK.Prasad  * and registers local to this cpu.
795aae8a53SK.Prasad  */
805aae8a53SK.Prasad void arch_uninstall_hw_breakpoint(struct perf_event *bp)
815aae8a53SK.Prasad {
8269111bacSChristoph Lameter 	struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
835aae8a53SK.Prasad 
845aae8a53SK.Prasad 	if (*slot != bp) {
855aae8a53SK.Prasad 		WARN_ONCE(1, "Can't find the breakpoint");
865aae8a53SK.Prasad 		return;
875aae8a53SK.Prasad 	}
885aae8a53SK.Prasad 
895aae8a53SK.Prasad 	*slot = NULL;
909422de3eSMichael Neuling 	hw_breakpoint_disable();
915aae8a53SK.Prasad }
925aae8a53SK.Prasad 
935aae8a53SK.Prasad /*
945aae8a53SK.Prasad  * Perform cleanup of arch-specific counters during unregistration
955aae8a53SK.Prasad  * of the perf-event
965aae8a53SK.Prasad  */
975aae8a53SK.Prasad void arch_unregister_hw_breakpoint(struct perf_event *bp)
985aae8a53SK.Prasad {
995aae8a53SK.Prasad 	/*
1005aae8a53SK.Prasad 	 * If the breakpoint is unregistered between a hw_breakpoint_handler()
1015aae8a53SK.Prasad 	 * and the single_step_dabr_instruction(), then cleanup the breakpoint
1025aae8a53SK.Prasad 	 * restoration variables to prevent dangling pointers.
103fb822e60SRavi Bangoria 	 * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
1045aae8a53SK.Prasad 	 */
105fb822e60SRavi Bangoria 	if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
1065aae8a53SK.Prasad 		bp->ctx->task->thread.last_hit_ubp = NULL;
1075aae8a53SK.Prasad }
1085aae8a53SK.Prasad 
1095aae8a53SK.Prasad /*
1105aae8a53SK.Prasad  * Check for virtual address in kernel space.
1115aae8a53SK.Prasad  */
1128e983ff9SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
1135aae8a53SK.Prasad {
1148e983ff9SFrederic Weisbecker 	return is_kernel_addr(hw->address);
1155aae8a53SK.Prasad }
1165aae8a53SK.Prasad 
1175aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type)
1185aae8a53SK.Prasad {
1199422de3eSMichael Neuling 	*gen_bp_type = 0;
1209422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_READ)
1219422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_R;
1229422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_WRITE)
1239422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_W;
1249422de3eSMichael Neuling 	if (*gen_bp_type == 0)
1255aae8a53SK.Prasad 		return -EINVAL;
1265aae8a53SK.Prasad 	return 0;
1275aae8a53SK.Prasad }
1285aae8a53SK.Prasad 
1295aae8a53SK.Prasad /*
130b57aeab8SRavi Bangoria  * Watchpoint match range is always doubleword(8 bytes) aligned on
131b57aeab8SRavi Bangoria  * powerpc. If the given range is crossing doubleword boundary, we
132b57aeab8SRavi Bangoria  * need to increase the length such that next doubleword also get
133b57aeab8SRavi Bangoria  * covered. Ex,
134b57aeab8SRavi Bangoria  *
135b57aeab8SRavi Bangoria  *          address   len = 6 bytes
136b57aeab8SRavi Bangoria  *                |=========.
137b57aeab8SRavi Bangoria  *   |------------v--|------v--------|
138b57aeab8SRavi Bangoria  *   | | | | | | | | | | | | | | | | |
139b57aeab8SRavi Bangoria  *   |---------------|---------------|
140b57aeab8SRavi Bangoria  *    <---8 bytes--->
141b57aeab8SRavi Bangoria  *
142b57aeab8SRavi Bangoria  * In this case, we should configure hw as:
143b57aeab8SRavi Bangoria  *   start_addr = address & ~HW_BREAKPOINT_ALIGN
144b57aeab8SRavi Bangoria  *   len = 16 bytes
145b57aeab8SRavi Bangoria  *
146b57aeab8SRavi Bangoria  * @start_addr and @end_addr are inclusive.
147b57aeab8SRavi Bangoria  */
148b57aeab8SRavi Bangoria static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw)
149b57aeab8SRavi Bangoria {
150b57aeab8SRavi Bangoria 	u16 max_len = DABR_MAX_LEN;
151b57aeab8SRavi Bangoria 	u16 hw_len;
152b57aeab8SRavi Bangoria 	unsigned long start_addr, end_addr;
153b57aeab8SRavi Bangoria 
154b57aeab8SRavi Bangoria 	start_addr = hw->address & ~HW_BREAKPOINT_ALIGN;
155b57aeab8SRavi Bangoria 	end_addr = (hw->address + hw->len - 1) | HW_BREAKPOINT_ALIGN;
156b57aeab8SRavi Bangoria 	hw_len = end_addr - start_addr + 1;
157b57aeab8SRavi Bangoria 
158b57aeab8SRavi Bangoria 	if (dawr_enabled()) {
159b57aeab8SRavi Bangoria 		max_len = DAWR_MAX_LEN;
160b57aeab8SRavi Bangoria 		/* DAWR region can't cross 512 bytes boundary */
161b57aeab8SRavi Bangoria 		if ((start_addr >> 9) != (end_addr >> 9))
162b57aeab8SRavi Bangoria 			return -EINVAL;
163*39413ae0SChristophe Leroy 	} else if (IS_ENABLED(CONFIG_PPC_8xx)) {
164*39413ae0SChristophe Leroy 		/* 8xx can setup a range without limitation */
165*39413ae0SChristophe Leroy 		max_len = U16_MAX;
166b57aeab8SRavi Bangoria 	}
167b57aeab8SRavi Bangoria 
168b57aeab8SRavi Bangoria 	if (hw_len > max_len)
169b57aeab8SRavi Bangoria 		return -EINVAL;
170b57aeab8SRavi Bangoria 
171b57aeab8SRavi Bangoria 	hw->hw_len = hw_len;
172b57aeab8SRavi Bangoria 	return 0;
173b57aeab8SRavi Bangoria }
174b57aeab8SRavi Bangoria 
175b57aeab8SRavi Bangoria /*
1765aae8a53SK.Prasad  * Validate the arch-specific HW Breakpoint register settings
1775aae8a53SK.Prasad  */
1785d5176baSFrederic Weisbecker int hw_breakpoint_arch_parse(struct perf_event *bp,
1795d5176baSFrederic Weisbecker 			     const struct perf_event_attr *attr,
1805d5176baSFrederic Weisbecker 			     struct arch_hw_breakpoint *hw)
1815aae8a53SK.Prasad {
182b57aeab8SRavi Bangoria 	int ret = -EINVAL;
1835aae8a53SK.Prasad 
184b57aeab8SRavi Bangoria 	if (!bp || !attr->bp_len)
1855aae8a53SK.Prasad 		return ret;
1865aae8a53SK.Prasad 
1875d5176baSFrederic Weisbecker 	hw->type = HW_BRK_TYPE_TRANSLATE;
1885d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_R)
1895d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_READ;
1905d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_W)
1915d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_WRITE;
1925d5176baSFrederic Weisbecker 	if (hw->type == HW_BRK_TYPE_TRANSLATE)
1939422de3eSMichael Neuling 		/* must set alteast read or write */
1945aae8a53SK.Prasad 		return ret;
1955d5176baSFrederic Weisbecker 	if (!attr->exclude_user)
1965d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_USER;
1975d5176baSFrederic Weisbecker 	if (!attr->exclude_kernel)
1985d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_KERNEL;
1995d5176baSFrederic Weisbecker 	if (!attr->exclude_hv)
2005d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_HYP;
2015d5176baSFrederic Weisbecker 	hw->address = attr->bp_addr;
2025d5176baSFrederic Weisbecker 	hw->len = attr->bp_len;
2035aae8a53SK.Prasad 
20485ce9a5dSMichael Neuling 	if (!ppc_breakpoint_available())
20585ce9a5dSMichael Neuling 		return -ENODEV;
206b57aeab8SRavi Bangoria 
207b57aeab8SRavi Bangoria 	return hw_breakpoint_validate_len(hw);
2085aae8a53SK.Prasad }
2095aae8a53SK.Prasad 
2105aae8a53SK.Prasad /*
21106532a67SK.Prasad  * Restores the breakpoint on the debug registers.
21206532a67SK.Prasad  * Invoke this function if it is known that the execution context is
21306532a67SK.Prasad  * about to change to cause loss of MSR_SE settings.
21406532a67SK.Prasad  */
21506532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
21606532a67SK.Prasad {
21706532a67SK.Prasad 	struct arch_hw_breakpoint *info;
21806532a67SK.Prasad 
21906532a67SK.Prasad 	if (likely(!tsk->thread.last_hit_ubp))
22006532a67SK.Prasad 		return;
22106532a67SK.Prasad 
22206532a67SK.Prasad 	info = counter_arch_bp(tsk->thread.last_hit_ubp);
22306532a67SK.Prasad 	regs->msr &= ~MSR_SE;
22421f58507SPaul Gortmaker 	__set_breakpoint(info);
22506532a67SK.Prasad 	tsk->thread.last_hit_ubp = NULL;
22606532a67SK.Prasad }
22706532a67SK.Prasad 
22827985b2aSRavi Bangoria static bool dar_within_range(unsigned long dar, struct arch_hw_breakpoint *info)
229bc01bdf6SRavi Bangoria {
23027985b2aSRavi Bangoria 	return ((info->address <= dar) && (dar - info->address < info->len));
23127985b2aSRavi Bangoria }
232bc01bdf6SRavi Bangoria 
23327985b2aSRavi Bangoria static bool
23427985b2aSRavi Bangoria dar_range_overlaps(unsigned long dar, int size, struct arch_hw_breakpoint *info)
23527985b2aSRavi Bangoria {
23627985b2aSRavi Bangoria 	return ((dar <= info->address + info->len - 1) &&
23727985b2aSRavi Bangoria 		(dar + size - 1 >= info->address));
238bc01bdf6SRavi Bangoria }
239bc01bdf6SRavi Bangoria 
24006532a67SK.Prasad /*
2415aae8a53SK.Prasad  * Handle debug exception notifications.
2425aae8a53SK.Prasad  */
243658d029dSChristophe Leroy static bool stepping_handler(struct pt_regs *regs, struct perf_event *bp,
24427985b2aSRavi Bangoria 			     struct arch_hw_breakpoint *info)
245658d029dSChristophe Leroy {
246bc01bdf6SRavi Bangoria 	unsigned int instr = 0;
24727985b2aSRavi Bangoria 	int ret, type, size;
24827985b2aSRavi Bangoria 	struct instruction_op op;
24927985b2aSRavi Bangoria 	unsigned long addr = info->address;
250bc01bdf6SRavi Bangoria 
251bc01bdf6SRavi Bangoria 	if (__get_user_inatomic(instr, (unsigned int *)regs->nip))
252bc01bdf6SRavi Bangoria 		goto fail;
253bc01bdf6SRavi Bangoria 
25427985b2aSRavi Bangoria 	ret = analyse_instr(&op, regs, instr);
25527985b2aSRavi Bangoria 	type = GETTYPE(op.type);
25627985b2aSRavi Bangoria 	size = GETSIZE(op.type);
25727985b2aSRavi Bangoria 
25827985b2aSRavi Bangoria 	if (!ret && (type == LARX || type == STCX)) {
259bc01bdf6SRavi Bangoria 		printk_ratelimited("Breakpoint hit on instruction that can't be emulated."
260bc01bdf6SRavi Bangoria 				   " Breakpoint at 0x%lx will be disabled.\n", addr);
261bc01bdf6SRavi Bangoria 		goto disable;
262bc01bdf6SRavi Bangoria 	}
263658d029dSChristophe Leroy 
26427985b2aSRavi Bangoria 	/*
26527985b2aSRavi Bangoria 	 * If it's extraneous event, we still need to emulate/single-
26627985b2aSRavi Bangoria 	 * step the instruction, but we don't generate an event.
26727985b2aSRavi Bangoria 	 */
26827985b2aSRavi Bangoria 	if (size && !dar_range_overlaps(regs->dar, size, info))
26927985b2aSRavi Bangoria 		info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
27027985b2aSRavi Bangoria 
271658d029dSChristophe Leroy 	/* Do not emulate user-space instructions, instead single-step them */
272658d029dSChristophe Leroy 	if (user_mode(regs)) {
273658d029dSChristophe Leroy 		current->thread.last_hit_ubp = bp;
274658d029dSChristophe Leroy 		regs->msr |= MSR_SE;
275658d029dSChristophe Leroy 		return false;
276658d029dSChristophe Leroy 	}
277658d029dSChristophe Leroy 
278bc01bdf6SRavi Bangoria 	if (!emulate_step(regs, instr))
279bc01bdf6SRavi Bangoria 		goto fail;
280658d029dSChristophe Leroy 
281bc01bdf6SRavi Bangoria 	return true;
282bc01bdf6SRavi Bangoria 
283bc01bdf6SRavi Bangoria fail:
284658d029dSChristophe Leroy 	/*
285bc01bdf6SRavi Bangoria 	 * We've failed in reliably handling the hw-breakpoint. Unregister
286bc01bdf6SRavi Bangoria 	 * it and throw a warning message to let the user know about it.
287658d029dSChristophe Leroy 	 */
288658d029dSChristophe Leroy 	WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
289658d029dSChristophe Leroy 		"0x%lx will be disabled.", addr);
290bc01bdf6SRavi Bangoria 
291bc01bdf6SRavi Bangoria disable:
292658d029dSChristophe Leroy 	perf_event_disable_inatomic(bp);
293658d029dSChristophe Leroy 	return false;
294658d029dSChristophe Leroy }
295658d029dSChristophe Leroy 
29603465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args)
2975aae8a53SK.Prasad {
2985aae8a53SK.Prasad 	int rc = NOTIFY_STOP;
2995aae8a53SK.Prasad 	struct perf_event *bp;
3005aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
3014ad8622dSChristophe Leroy 	struct arch_hw_breakpoint *info;
3025aae8a53SK.Prasad 
3035aae8a53SK.Prasad 	/* Disable breakpoints during exception handling */
3049422de3eSMichael Neuling 	hw_breakpoint_disable();
305574cb248SPaul Mackerras 
3065aae8a53SK.Prasad 	/*
3075aae8a53SK.Prasad 	 * The counter may be concurrently released but that can only
3085aae8a53SK.Prasad 	 * occur from a call_rcu() path. We can then safely fetch
3095aae8a53SK.Prasad 	 * the breakpoint, use its callback, touch its counter
3105aae8a53SK.Prasad 	 * while we are in an rcu_read_lock() path.
3115aae8a53SK.Prasad 	 */
3125aae8a53SK.Prasad 	rcu_read_lock();
3135aae8a53SK.Prasad 
31469111bacSChristoph Lameter 	bp = __this_cpu_read(bp_per_reg);
315c21a493aSRavi Bangoria 	if (!bp) {
316c21a493aSRavi Bangoria 		rc = NOTIFY_DONE;
3175aae8a53SK.Prasad 		goto out;
318c21a493aSRavi Bangoria 	}
3195aae8a53SK.Prasad 	info = counter_arch_bp(bp);
3205aae8a53SK.Prasad 
3215aae8a53SK.Prasad 	/*
3225aae8a53SK.Prasad 	 * Return early after invoking user-callback function without restoring
3235aae8a53SK.Prasad 	 * DABR if the breakpoint is from ptrace which always operates in
3245aae8a53SK.Prasad 	 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
3255aae8a53SK.Prasad 	 * generated in do_dabr().
3265aae8a53SK.Prasad 	 */
327574cb248SPaul Mackerras 	if (bp->overflow_handler == ptrace_triggered) {
3285aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3295aae8a53SK.Prasad 		rc = NOTIFY_DONE;
3305aae8a53SK.Prasad 		goto out;
3315aae8a53SK.Prasad 	}
3325aae8a53SK.Prasad 
333540e07c6SMichael Neuling 	info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
33427985b2aSRavi Bangoria 	if (!dar_within_range(regs->dar, info))
3359422de3eSMichael Neuling 		info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
336*39413ae0SChristophe Leroy 
337*39413ae0SChristophe Leroy 	if (!IS_ENABLED(CONFIG_PPC_8xx) && !stepping_handler(regs, bp, info))
3385aae8a53SK.Prasad 		goto out;
3395aae8a53SK.Prasad 
3405aae8a53SK.Prasad 	/*
3415aae8a53SK.Prasad 	 * As a policy, the callback is invoked in a 'trigger-after-execute'
3425aae8a53SK.Prasad 	 * fashion
3435aae8a53SK.Prasad 	 */
3449422de3eSMichael Neuling 	if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
3455aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3465aae8a53SK.Prasad 
34721f58507SPaul Gortmaker 	__set_breakpoint(info);
3485aae8a53SK.Prasad out:
3495aae8a53SK.Prasad 	rcu_read_unlock();
3505aae8a53SK.Prasad 	return rc;
3515aae8a53SK.Prasad }
35203465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler);
3535aae8a53SK.Prasad 
3545aae8a53SK.Prasad /*
3555aae8a53SK.Prasad  * Handle single-step exceptions following a DABR hit.
3565aae8a53SK.Prasad  */
35703465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args)
3585aae8a53SK.Prasad {
3595aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
3605aae8a53SK.Prasad 	struct perf_event *bp = NULL;
3613f4693eeSMichael Neuling 	struct arch_hw_breakpoint *info;
3625aae8a53SK.Prasad 
3635aae8a53SK.Prasad 	bp = current->thread.last_hit_ubp;
3645aae8a53SK.Prasad 	/*
3655aae8a53SK.Prasad 	 * Check if we are single-stepping as a result of a
3665aae8a53SK.Prasad 	 * previous HW Breakpoint exception
3675aae8a53SK.Prasad 	 */
3685aae8a53SK.Prasad 	if (!bp)
3695aae8a53SK.Prasad 		return NOTIFY_DONE;
3705aae8a53SK.Prasad 
3713f4693eeSMichael Neuling 	info = counter_arch_bp(bp);
3725aae8a53SK.Prasad 
3735aae8a53SK.Prasad 	/*
3745aae8a53SK.Prasad 	 * We shall invoke the user-defined callback function in the single
3755aae8a53SK.Prasad 	 * stepping handler to confirm to 'trigger-after-execute' semantics
3765aae8a53SK.Prasad 	 */
3779422de3eSMichael Neuling 	if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
3785aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3795aae8a53SK.Prasad 
38021f58507SPaul Gortmaker 	__set_breakpoint(info);
3815aae8a53SK.Prasad 	current->thread.last_hit_ubp = NULL;
38276b0f133SPaul Mackerras 
38376b0f133SPaul Mackerras 	/*
38476b0f133SPaul Mackerras 	 * If the process was being single-stepped by ptrace, let the
38576b0f133SPaul Mackerras 	 * other single-step actions occur (e.g. generate SIGTRAP).
38676b0f133SPaul Mackerras 	 */
38776b0f133SPaul Mackerras 	if (test_thread_flag(TIF_SINGLESTEP))
38876b0f133SPaul Mackerras 		return NOTIFY_DONE;
38976b0f133SPaul Mackerras 
3905aae8a53SK.Prasad 	return NOTIFY_STOP;
3915aae8a53SK.Prasad }
39203465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction);
3935aae8a53SK.Prasad 
3945aae8a53SK.Prasad /*
3955aae8a53SK.Prasad  * Handle debug exception notifications.
3965aae8a53SK.Prasad  */
39703465f89SNicholas Piggin int hw_breakpoint_exceptions_notify(
3985aae8a53SK.Prasad 		struct notifier_block *unused, unsigned long val, void *data)
3995aae8a53SK.Prasad {
4005aae8a53SK.Prasad 	int ret = NOTIFY_DONE;
4015aae8a53SK.Prasad 
4025aae8a53SK.Prasad 	switch (val) {
4035aae8a53SK.Prasad 	case DIE_DABR_MATCH:
4045aae8a53SK.Prasad 		ret = hw_breakpoint_handler(data);
4055aae8a53SK.Prasad 		break;
4065aae8a53SK.Prasad 	case DIE_SSTEP:
4075aae8a53SK.Prasad 		ret = single_step_dabr_instruction(data);
4085aae8a53SK.Prasad 		break;
4095aae8a53SK.Prasad 	}
4105aae8a53SK.Prasad 
4115aae8a53SK.Prasad 	return ret;
4125aae8a53SK.Prasad }
41303465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
4145aae8a53SK.Prasad 
4155aae8a53SK.Prasad /*
4165aae8a53SK.Prasad  * Release the user breakpoints used by ptrace
4175aae8a53SK.Prasad  */
4185aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
4195aae8a53SK.Prasad {
4205aae8a53SK.Prasad 	struct thread_struct *t = &tsk->thread;
4215aae8a53SK.Prasad 
4225aae8a53SK.Prasad 	unregister_hw_breakpoint(t->ptrace_bps[0]);
4235aae8a53SK.Prasad 	t->ptrace_bps[0] = NULL;
4245aae8a53SK.Prasad }
4255aae8a53SK.Prasad 
4265aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp)
4275aae8a53SK.Prasad {
4285aae8a53SK.Prasad 	/* TODO */
4295aae8a53SK.Prasad }
430