xref: /linux/arch/powerpc/kernel/hw_breakpoint.c (revision 753462512868674a788ecc77bb96752efb818785)
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>
27*75346251SJordan Niethe #include <asm/inst.h>
287c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
295aae8a53SK.Prasad 
305aae8a53SK.Prasad /*
315aae8a53SK.Prasad  * Stores the breakpoints currently in use on each breakpoint address
325aae8a53SK.Prasad  * register for every cpu
335aae8a53SK.Prasad  */
345aae8a53SK.Prasad static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
355aae8a53SK.Prasad 
365aae8a53SK.Prasad /*
37d09ec738SPaul Mackerras  * Returns total number of data or instruction breakpoints available.
38d09ec738SPaul Mackerras  */
39d09ec738SPaul Mackerras int hw_breakpoint_slots(int type)
40d09ec738SPaul Mackerras {
41d09ec738SPaul Mackerras 	if (type == TYPE_DATA)
42d09ec738SPaul Mackerras 		return HBP_NUM;
43d09ec738SPaul Mackerras 	return 0;		/* no instruction breakpoints available */
44d09ec738SPaul Mackerras }
45d09ec738SPaul Mackerras 
46d09ec738SPaul Mackerras /*
475aae8a53SK.Prasad  * Install a perf counter breakpoint.
485aae8a53SK.Prasad  *
495aae8a53SK.Prasad  * We seek a free debug address register and use it for this
505aae8a53SK.Prasad  * breakpoint.
515aae8a53SK.Prasad  *
525aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
535aae8a53SK.Prasad  * and registers local to this cpu.
545aae8a53SK.Prasad  */
555aae8a53SK.Prasad int arch_install_hw_breakpoint(struct perf_event *bp)
565aae8a53SK.Prasad {
575aae8a53SK.Prasad 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
5869111bacSChristoph Lameter 	struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
595aae8a53SK.Prasad 
605aae8a53SK.Prasad 	*slot = bp;
615aae8a53SK.Prasad 
625aae8a53SK.Prasad 	/*
635aae8a53SK.Prasad 	 * Do not install DABR values if the instruction must be single-stepped.
645aae8a53SK.Prasad 	 * If so, DABR will be populated in single_step_dabr_instruction().
655aae8a53SK.Prasad 	 */
665aae8a53SK.Prasad 	if (current->thread.last_hit_ubp != bp)
6721f58507SPaul Gortmaker 		__set_breakpoint(info);
685aae8a53SK.Prasad 
695aae8a53SK.Prasad 	return 0;
705aae8a53SK.Prasad }
715aae8a53SK.Prasad 
725aae8a53SK.Prasad /*
735aae8a53SK.Prasad  * Uninstall the breakpoint contained in the given counter.
745aae8a53SK.Prasad  *
755aae8a53SK.Prasad  * First we search the debug address register it uses and then we disable
765aae8a53SK.Prasad  * it.
775aae8a53SK.Prasad  *
785aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
795aae8a53SK.Prasad  * and registers local to this cpu.
805aae8a53SK.Prasad  */
815aae8a53SK.Prasad void arch_uninstall_hw_breakpoint(struct perf_event *bp)
825aae8a53SK.Prasad {
8369111bacSChristoph Lameter 	struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
845aae8a53SK.Prasad 
855aae8a53SK.Prasad 	if (*slot != bp) {
865aae8a53SK.Prasad 		WARN_ONCE(1, "Can't find the breakpoint");
875aae8a53SK.Prasad 		return;
885aae8a53SK.Prasad 	}
895aae8a53SK.Prasad 
905aae8a53SK.Prasad 	*slot = NULL;
919422de3eSMichael Neuling 	hw_breakpoint_disable();
925aae8a53SK.Prasad }
935aae8a53SK.Prasad 
945aae8a53SK.Prasad /*
955aae8a53SK.Prasad  * Perform cleanup of arch-specific counters during unregistration
965aae8a53SK.Prasad  * of the perf-event
975aae8a53SK.Prasad  */
985aae8a53SK.Prasad void arch_unregister_hw_breakpoint(struct perf_event *bp)
995aae8a53SK.Prasad {
1005aae8a53SK.Prasad 	/*
1015aae8a53SK.Prasad 	 * If the breakpoint is unregistered between a hw_breakpoint_handler()
1025aae8a53SK.Prasad 	 * and the single_step_dabr_instruction(), then cleanup the breakpoint
1035aae8a53SK.Prasad 	 * restoration variables to prevent dangling pointers.
104fb822e60SRavi Bangoria 	 * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
1055aae8a53SK.Prasad 	 */
106fb822e60SRavi Bangoria 	if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
1075aae8a53SK.Prasad 		bp->ctx->task->thread.last_hit_ubp = NULL;
1085aae8a53SK.Prasad }
1095aae8a53SK.Prasad 
1105aae8a53SK.Prasad /*
1115aae8a53SK.Prasad  * Check for virtual address in kernel space.
1125aae8a53SK.Prasad  */
1138e983ff9SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
1145aae8a53SK.Prasad {
1158e983ff9SFrederic Weisbecker 	return is_kernel_addr(hw->address);
1165aae8a53SK.Prasad }
1175aae8a53SK.Prasad 
1185aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type)
1195aae8a53SK.Prasad {
1209422de3eSMichael Neuling 	*gen_bp_type = 0;
1219422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_READ)
1229422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_R;
1239422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_WRITE)
1249422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_W;
1259422de3eSMichael Neuling 	if (*gen_bp_type == 0)
1265aae8a53SK.Prasad 		return -EINVAL;
1275aae8a53SK.Prasad 	return 0;
1285aae8a53SK.Prasad }
1295aae8a53SK.Prasad 
1305aae8a53SK.Prasad /*
131b57aeab8SRavi Bangoria  * Watchpoint match range is always doubleword(8 bytes) aligned on
132b57aeab8SRavi Bangoria  * powerpc. If the given range is crossing doubleword boundary, we
133b57aeab8SRavi Bangoria  * need to increase the length such that next doubleword also get
134b57aeab8SRavi Bangoria  * covered. Ex,
135b57aeab8SRavi Bangoria  *
136b57aeab8SRavi Bangoria  *          address   len = 6 bytes
137b57aeab8SRavi Bangoria  *                |=========.
138b57aeab8SRavi Bangoria  *   |------------v--|------v--------|
139b57aeab8SRavi Bangoria  *   | | | | | | | | | | | | | | | | |
140b57aeab8SRavi Bangoria  *   |---------------|---------------|
141b57aeab8SRavi Bangoria  *    <---8 bytes--->
142b57aeab8SRavi Bangoria  *
143b57aeab8SRavi Bangoria  * In this case, we should configure hw as:
144b57aeab8SRavi Bangoria  *   start_addr = address & ~HW_BREAKPOINT_ALIGN
145b57aeab8SRavi Bangoria  *   len = 16 bytes
146b57aeab8SRavi Bangoria  *
147b57aeab8SRavi Bangoria  * @start_addr and @end_addr are inclusive.
148b57aeab8SRavi Bangoria  */
149b57aeab8SRavi Bangoria static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw)
150b57aeab8SRavi Bangoria {
151b57aeab8SRavi Bangoria 	u16 max_len = DABR_MAX_LEN;
152b57aeab8SRavi Bangoria 	u16 hw_len;
153b57aeab8SRavi Bangoria 	unsigned long start_addr, end_addr;
154b57aeab8SRavi Bangoria 
155b57aeab8SRavi Bangoria 	start_addr = hw->address & ~HW_BREAKPOINT_ALIGN;
156b57aeab8SRavi Bangoria 	end_addr = (hw->address + hw->len - 1) | HW_BREAKPOINT_ALIGN;
157b57aeab8SRavi Bangoria 	hw_len = end_addr - start_addr + 1;
158b57aeab8SRavi Bangoria 
159b57aeab8SRavi Bangoria 	if (dawr_enabled()) {
160b57aeab8SRavi Bangoria 		max_len = DAWR_MAX_LEN;
161b57aeab8SRavi Bangoria 		/* DAWR region can't cross 512 bytes boundary */
162b57aeab8SRavi Bangoria 		if ((start_addr >> 9) != (end_addr >> 9))
163b57aeab8SRavi Bangoria 			return -EINVAL;
16439413ae0SChristophe Leroy 	} else if (IS_ENABLED(CONFIG_PPC_8xx)) {
16539413ae0SChristophe Leroy 		/* 8xx can setup a range without limitation */
16639413ae0SChristophe Leroy 		max_len = U16_MAX;
167b57aeab8SRavi Bangoria 	}
168b57aeab8SRavi Bangoria 
169b57aeab8SRavi Bangoria 	if (hw_len > max_len)
170b57aeab8SRavi Bangoria 		return -EINVAL;
171b57aeab8SRavi Bangoria 
172b57aeab8SRavi Bangoria 	hw->hw_len = hw_len;
173b57aeab8SRavi Bangoria 	return 0;
174b57aeab8SRavi Bangoria }
175b57aeab8SRavi Bangoria 
176b57aeab8SRavi Bangoria /*
1775aae8a53SK.Prasad  * Validate the arch-specific HW Breakpoint register settings
1785aae8a53SK.Prasad  */
1795d5176baSFrederic Weisbecker int hw_breakpoint_arch_parse(struct perf_event *bp,
1805d5176baSFrederic Weisbecker 			     const struct perf_event_attr *attr,
1815d5176baSFrederic Weisbecker 			     struct arch_hw_breakpoint *hw)
1825aae8a53SK.Prasad {
183b57aeab8SRavi Bangoria 	int ret = -EINVAL;
1845aae8a53SK.Prasad 
185b57aeab8SRavi Bangoria 	if (!bp || !attr->bp_len)
1865aae8a53SK.Prasad 		return ret;
1875aae8a53SK.Prasad 
1885d5176baSFrederic Weisbecker 	hw->type = HW_BRK_TYPE_TRANSLATE;
1895d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_R)
1905d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_READ;
1915d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_W)
1925d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_WRITE;
1935d5176baSFrederic Weisbecker 	if (hw->type == HW_BRK_TYPE_TRANSLATE)
1949422de3eSMichael Neuling 		/* must set alteast read or write */
1955aae8a53SK.Prasad 		return ret;
1965d5176baSFrederic Weisbecker 	if (!attr->exclude_user)
1975d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_USER;
1985d5176baSFrederic Weisbecker 	if (!attr->exclude_kernel)
1995d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_KERNEL;
2005d5176baSFrederic Weisbecker 	if (!attr->exclude_hv)
2015d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_HYP;
2025d5176baSFrederic Weisbecker 	hw->address = attr->bp_addr;
2035d5176baSFrederic Weisbecker 	hw->len = attr->bp_len;
2045aae8a53SK.Prasad 
20585ce9a5dSMichael Neuling 	if (!ppc_breakpoint_available())
20685ce9a5dSMichael Neuling 		return -ENODEV;
207b57aeab8SRavi Bangoria 
208b57aeab8SRavi Bangoria 	return hw_breakpoint_validate_len(hw);
2095aae8a53SK.Prasad }
2105aae8a53SK.Prasad 
2115aae8a53SK.Prasad /*
21206532a67SK.Prasad  * Restores the breakpoint on the debug registers.
21306532a67SK.Prasad  * Invoke this function if it is known that the execution context is
21406532a67SK.Prasad  * about to change to cause loss of MSR_SE settings.
21506532a67SK.Prasad  */
21606532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
21706532a67SK.Prasad {
21806532a67SK.Prasad 	struct arch_hw_breakpoint *info;
21906532a67SK.Prasad 
22006532a67SK.Prasad 	if (likely(!tsk->thread.last_hit_ubp))
22106532a67SK.Prasad 		return;
22206532a67SK.Prasad 
22306532a67SK.Prasad 	info = counter_arch_bp(tsk->thread.last_hit_ubp);
22406532a67SK.Prasad 	regs->msr &= ~MSR_SE;
22521f58507SPaul Gortmaker 	__set_breakpoint(info);
22606532a67SK.Prasad 	tsk->thread.last_hit_ubp = NULL;
22706532a67SK.Prasad }
22806532a67SK.Prasad 
22927985b2aSRavi Bangoria static bool dar_within_range(unsigned long dar, struct arch_hw_breakpoint *info)
230bc01bdf6SRavi Bangoria {
23127985b2aSRavi Bangoria 	return ((info->address <= dar) && (dar - info->address < info->len));
23227985b2aSRavi Bangoria }
233bc01bdf6SRavi Bangoria 
23427985b2aSRavi Bangoria static bool
23527985b2aSRavi Bangoria dar_range_overlaps(unsigned long dar, int size, struct arch_hw_breakpoint *info)
23627985b2aSRavi Bangoria {
23727985b2aSRavi Bangoria 	return ((dar <= info->address + info->len - 1) &&
23827985b2aSRavi Bangoria 		(dar + size - 1 >= info->address));
239bc01bdf6SRavi Bangoria }
240bc01bdf6SRavi Bangoria 
24106532a67SK.Prasad /*
2425aae8a53SK.Prasad  * Handle debug exception notifications.
2435aae8a53SK.Prasad  */
244658d029dSChristophe Leroy static bool stepping_handler(struct pt_regs *regs, struct perf_event *bp,
24527985b2aSRavi Bangoria 			     struct arch_hw_breakpoint *info)
246658d029dSChristophe Leroy {
247*75346251SJordan Niethe 	unsigned int instr = ppc_inst(0);
24827985b2aSRavi Bangoria 	int ret, type, size;
24927985b2aSRavi Bangoria 	struct instruction_op op;
25027985b2aSRavi Bangoria 	unsigned long addr = info->address;
251bc01bdf6SRavi Bangoria 
252bc01bdf6SRavi Bangoria 	if (__get_user_inatomic(instr, (unsigned int *)regs->nip))
253bc01bdf6SRavi Bangoria 		goto fail;
254bc01bdf6SRavi Bangoria 
25527985b2aSRavi Bangoria 	ret = analyse_instr(&op, regs, instr);
25627985b2aSRavi Bangoria 	type = GETTYPE(op.type);
25727985b2aSRavi Bangoria 	size = GETSIZE(op.type);
25827985b2aSRavi Bangoria 
25927985b2aSRavi Bangoria 	if (!ret && (type == LARX || type == STCX)) {
260bc01bdf6SRavi Bangoria 		printk_ratelimited("Breakpoint hit on instruction that can't be emulated."
261bc01bdf6SRavi Bangoria 				   " Breakpoint at 0x%lx will be disabled.\n", addr);
262bc01bdf6SRavi Bangoria 		goto disable;
263bc01bdf6SRavi Bangoria 	}
264658d029dSChristophe Leroy 
26527985b2aSRavi Bangoria 	/*
26627985b2aSRavi Bangoria 	 * If it's extraneous event, we still need to emulate/single-
26727985b2aSRavi Bangoria 	 * step the instruction, but we don't generate an event.
26827985b2aSRavi Bangoria 	 */
26927985b2aSRavi Bangoria 	if (size && !dar_range_overlaps(regs->dar, size, info))
27027985b2aSRavi Bangoria 		info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
27127985b2aSRavi Bangoria 
272658d029dSChristophe Leroy 	/* Do not emulate user-space instructions, instead single-step them */
273658d029dSChristophe Leroy 	if (user_mode(regs)) {
274658d029dSChristophe Leroy 		current->thread.last_hit_ubp = bp;
275658d029dSChristophe Leroy 		regs->msr |= MSR_SE;
276658d029dSChristophe Leroy 		return false;
277658d029dSChristophe Leroy 	}
278658d029dSChristophe Leroy 
279bc01bdf6SRavi Bangoria 	if (!emulate_step(regs, instr))
280bc01bdf6SRavi Bangoria 		goto fail;
281658d029dSChristophe Leroy 
282bc01bdf6SRavi Bangoria 	return true;
283bc01bdf6SRavi Bangoria 
284bc01bdf6SRavi Bangoria fail:
285658d029dSChristophe Leroy 	/*
286bc01bdf6SRavi Bangoria 	 * We've failed in reliably handling the hw-breakpoint. Unregister
287bc01bdf6SRavi Bangoria 	 * it and throw a warning message to let the user know about it.
288658d029dSChristophe Leroy 	 */
289658d029dSChristophe Leroy 	WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
290658d029dSChristophe Leroy 		"0x%lx will be disabled.", addr);
291bc01bdf6SRavi Bangoria 
292bc01bdf6SRavi Bangoria disable:
293658d029dSChristophe Leroy 	perf_event_disable_inatomic(bp);
294658d029dSChristophe Leroy 	return false;
295658d029dSChristophe Leroy }
296658d029dSChristophe Leroy 
29703465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args)
2985aae8a53SK.Prasad {
2995aae8a53SK.Prasad 	int rc = NOTIFY_STOP;
3005aae8a53SK.Prasad 	struct perf_event *bp;
3015aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
3024ad8622dSChristophe Leroy 	struct arch_hw_breakpoint *info;
3035aae8a53SK.Prasad 
3045aae8a53SK.Prasad 	/* Disable breakpoints during exception handling */
3059422de3eSMichael Neuling 	hw_breakpoint_disable();
306574cb248SPaul Mackerras 
3075aae8a53SK.Prasad 	/*
3085aae8a53SK.Prasad 	 * The counter may be concurrently released but that can only
3095aae8a53SK.Prasad 	 * occur from a call_rcu() path. We can then safely fetch
3105aae8a53SK.Prasad 	 * the breakpoint, use its callback, touch its counter
3115aae8a53SK.Prasad 	 * while we are in an rcu_read_lock() path.
3125aae8a53SK.Prasad 	 */
3135aae8a53SK.Prasad 	rcu_read_lock();
3145aae8a53SK.Prasad 
31569111bacSChristoph Lameter 	bp = __this_cpu_read(bp_per_reg);
316c21a493aSRavi Bangoria 	if (!bp) {
317c21a493aSRavi Bangoria 		rc = NOTIFY_DONE;
3185aae8a53SK.Prasad 		goto out;
319c21a493aSRavi Bangoria 	}
3205aae8a53SK.Prasad 	info = counter_arch_bp(bp);
3215aae8a53SK.Prasad 
3225aae8a53SK.Prasad 	/*
3235aae8a53SK.Prasad 	 * Return early after invoking user-callback function without restoring
3245aae8a53SK.Prasad 	 * DABR if the breakpoint is from ptrace which always operates in
3255aae8a53SK.Prasad 	 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
3265aae8a53SK.Prasad 	 * generated in do_dabr().
3275aae8a53SK.Prasad 	 */
328574cb248SPaul Mackerras 	if (bp->overflow_handler == ptrace_triggered) {
3295aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3305aae8a53SK.Prasad 		rc = NOTIFY_DONE;
3315aae8a53SK.Prasad 		goto out;
3325aae8a53SK.Prasad 	}
3335aae8a53SK.Prasad 
334540e07c6SMichael Neuling 	info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
335e08658a6SRavi Bangoria 	if (IS_ENABLED(CONFIG_PPC_8xx)) {
33627985b2aSRavi Bangoria 		if (!dar_within_range(regs->dar, info))
3379422de3eSMichael Neuling 			info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
338e08658a6SRavi Bangoria 	} else {
339e08658a6SRavi Bangoria 		if (!stepping_handler(regs, bp, info))
3405aae8a53SK.Prasad 			goto out;
341e08658a6SRavi Bangoria 	}
3425aae8a53SK.Prasad 
3435aae8a53SK.Prasad 	/*
3445aae8a53SK.Prasad 	 * As a policy, the callback is invoked in a 'trigger-after-execute'
3455aae8a53SK.Prasad 	 * fashion
3465aae8a53SK.Prasad 	 */
3479422de3eSMichael Neuling 	if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
3485aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3495aae8a53SK.Prasad 
35021f58507SPaul Gortmaker 	__set_breakpoint(info);
3515aae8a53SK.Prasad out:
3525aae8a53SK.Prasad 	rcu_read_unlock();
3535aae8a53SK.Prasad 	return rc;
3545aae8a53SK.Prasad }
35503465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler);
3565aae8a53SK.Prasad 
3575aae8a53SK.Prasad /*
3585aae8a53SK.Prasad  * Handle single-step exceptions following a DABR hit.
3595aae8a53SK.Prasad  */
36003465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args)
3615aae8a53SK.Prasad {
3625aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
3635aae8a53SK.Prasad 	struct perf_event *bp = NULL;
3643f4693eeSMichael Neuling 	struct arch_hw_breakpoint *info;
3655aae8a53SK.Prasad 
3665aae8a53SK.Prasad 	bp = current->thread.last_hit_ubp;
3675aae8a53SK.Prasad 	/*
3685aae8a53SK.Prasad 	 * Check if we are single-stepping as a result of a
3695aae8a53SK.Prasad 	 * previous HW Breakpoint exception
3705aae8a53SK.Prasad 	 */
3715aae8a53SK.Prasad 	if (!bp)
3725aae8a53SK.Prasad 		return NOTIFY_DONE;
3735aae8a53SK.Prasad 
3743f4693eeSMichael Neuling 	info = counter_arch_bp(bp);
3755aae8a53SK.Prasad 
3765aae8a53SK.Prasad 	/*
3775aae8a53SK.Prasad 	 * We shall invoke the user-defined callback function in the single
3785aae8a53SK.Prasad 	 * stepping handler to confirm to 'trigger-after-execute' semantics
3795aae8a53SK.Prasad 	 */
3809422de3eSMichael Neuling 	if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
3815aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3825aae8a53SK.Prasad 
38321f58507SPaul Gortmaker 	__set_breakpoint(info);
3845aae8a53SK.Prasad 	current->thread.last_hit_ubp = NULL;
38576b0f133SPaul Mackerras 
38676b0f133SPaul Mackerras 	/*
38776b0f133SPaul Mackerras 	 * If the process was being single-stepped by ptrace, let the
38876b0f133SPaul Mackerras 	 * other single-step actions occur (e.g. generate SIGTRAP).
38976b0f133SPaul Mackerras 	 */
39076b0f133SPaul Mackerras 	if (test_thread_flag(TIF_SINGLESTEP))
39176b0f133SPaul Mackerras 		return NOTIFY_DONE;
39276b0f133SPaul Mackerras 
3935aae8a53SK.Prasad 	return NOTIFY_STOP;
3945aae8a53SK.Prasad }
39503465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction);
3965aae8a53SK.Prasad 
3975aae8a53SK.Prasad /*
3985aae8a53SK.Prasad  * Handle debug exception notifications.
3995aae8a53SK.Prasad  */
40003465f89SNicholas Piggin int hw_breakpoint_exceptions_notify(
4015aae8a53SK.Prasad 		struct notifier_block *unused, unsigned long val, void *data)
4025aae8a53SK.Prasad {
4035aae8a53SK.Prasad 	int ret = NOTIFY_DONE;
4045aae8a53SK.Prasad 
4055aae8a53SK.Prasad 	switch (val) {
4065aae8a53SK.Prasad 	case DIE_DABR_MATCH:
4075aae8a53SK.Prasad 		ret = hw_breakpoint_handler(data);
4085aae8a53SK.Prasad 		break;
4095aae8a53SK.Prasad 	case DIE_SSTEP:
4105aae8a53SK.Prasad 		ret = single_step_dabr_instruction(data);
4115aae8a53SK.Prasad 		break;
4125aae8a53SK.Prasad 	}
4135aae8a53SK.Prasad 
4145aae8a53SK.Prasad 	return ret;
4155aae8a53SK.Prasad }
41603465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
4175aae8a53SK.Prasad 
4185aae8a53SK.Prasad /*
4195aae8a53SK.Prasad  * Release the user breakpoints used by ptrace
4205aae8a53SK.Prasad  */
4215aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
4225aae8a53SK.Prasad {
4235aae8a53SK.Prasad 	struct thread_struct *t = &tsk->thread;
4245aae8a53SK.Prasad 
4255aae8a53SK.Prasad 	unregister_hw_breakpoint(t->ptrace_bps[0]);
4265aae8a53SK.Prasad 	t->ptrace_bps[0] = NULL;
4275aae8a53SK.Prasad }
4285aae8a53SK.Prasad 
4295aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp)
4305aae8a53SK.Prasad {
4315aae8a53SK.Prasad 	/* TODO */
4325aae8a53SK.Prasad }
433ccbed90bSChristophe Leroy 
434ccbed90bSChristophe Leroy void ptrace_triggered(struct perf_event *bp,
435ccbed90bSChristophe Leroy 		      struct perf_sample_data *data, struct pt_regs *regs)
436ccbed90bSChristophe Leroy {
437ccbed90bSChristophe Leroy 	struct perf_event_attr attr;
438ccbed90bSChristophe Leroy 
439ccbed90bSChristophe Leroy 	/*
440ccbed90bSChristophe Leroy 	 * Disable the breakpoint request here since ptrace has defined a
441ccbed90bSChristophe Leroy 	 * one-shot behaviour for breakpoint exceptions in PPC64.
442ccbed90bSChristophe Leroy 	 * The SIGTRAP signal is generated automatically for us in do_dabr().
443ccbed90bSChristophe Leroy 	 * We don't have to do anything about that here
444ccbed90bSChristophe Leroy 	 */
445ccbed90bSChristophe Leroy 	attr = bp->attr;
446ccbed90bSChristophe Leroy 	attr.disabled = true;
447ccbed90bSChristophe Leroy 	modify_user_hw_breakpoint(bp, &attr);
448ccbed90bSChristophe Leroy }
449