xref: /openbmc/linux/arch/powerpc/kernel/hw_breakpoint.c (revision e68ef121c1f4c38edf87a3354661ceb99d522729)
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>
2775346251SJordan 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)
42a6ba44e8SRavi Bangoria 		return nr_wp_slots();
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)
674a8a9379SRavi Bangoria 		__set_breakpoint(0, 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 
94c9e82aebSRavi Bangoria static bool is_ptrace_bp(struct perf_event *bp)
95c9e82aebSRavi Bangoria {
96c9e82aebSRavi Bangoria 	return bp->overflow_handler == ptrace_triggered;
97c9e82aebSRavi Bangoria }
98c9e82aebSRavi Bangoria 
995aae8a53SK.Prasad /*
1005aae8a53SK.Prasad  * Perform cleanup of arch-specific counters during unregistration
1015aae8a53SK.Prasad  * of the perf-event
1025aae8a53SK.Prasad  */
1035aae8a53SK.Prasad void arch_unregister_hw_breakpoint(struct perf_event *bp)
1045aae8a53SK.Prasad {
1055aae8a53SK.Prasad 	/*
1065aae8a53SK.Prasad 	 * If the breakpoint is unregistered between a hw_breakpoint_handler()
1075aae8a53SK.Prasad 	 * and the single_step_dabr_instruction(), then cleanup the breakpoint
1085aae8a53SK.Prasad 	 * restoration variables to prevent dangling pointers.
109fb822e60SRavi Bangoria 	 * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
1105aae8a53SK.Prasad 	 */
111fb822e60SRavi Bangoria 	if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
1125aae8a53SK.Prasad 		bp->ctx->task->thread.last_hit_ubp = NULL;
1135aae8a53SK.Prasad }
1145aae8a53SK.Prasad 
1155aae8a53SK.Prasad /*
1165aae8a53SK.Prasad  * Check for virtual address in kernel space.
1175aae8a53SK.Prasad  */
1188e983ff9SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
1195aae8a53SK.Prasad {
1208e983ff9SFrederic Weisbecker 	return is_kernel_addr(hw->address);
1215aae8a53SK.Prasad }
1225aae8a53SK.Prasad 
1235aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type)
1245aae8a53SK.Prasad {
1259422de3eSMichael Neuling 	*gen_bp_type = 0;
1269422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_READ)
1279422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_R;
1289422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_WRITE)
1299422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_W;
1309422de3eSMichael Neuling 	if (*gen_bp_type == 0)
1315aae8a53SK.Prasad 		return -EINVAL;
1325aae8a53SK.Prasad 	return 0;
1335aae8a53SK.Prasad }
1345aae8a53SK.Prasad 
1355aae8a53SK.Prasad /*
136b57aeab8SRavi Bangoria  * Watchpoint match range is always doubleword(8 bytes) aligned on
137b57aeab8SRavi Bangoria  * powerpc. If the given range is crossing doubleword boundary, we
138b57aeab8SRavi Bangoria  * need to increase the length such that next doubleword also get
139b57aeab8SRavi Bangoria  * covered. Ex,
140b57aeab8SRavi Bangoria  *
141b57aeab8SRavi Bangoria  *          address   len = 6 bytes
142b57aeab8SRavi Bangoria  *                |=========.
143b57aeab8SRavi Bangoria  *   |------------v--|------v--------|
144b57aeab8SRavi Bangoria  *   | | | | | | | | | | | | | | | | |
145b57aeab8SRavi Bangoria  *   |---------------|---------------|
146b57aeab8SRavi Bangoria  *    <---8 bytes--->
147b57aeab8SRavi Bangoria  *
148b57aeab8SRavi Bangoria  * In this case, we should configure hw as:
149*e68ef121SRavi Bangoria  *   start_addr = address & ~(HW_BREAKPOINT_SIZE - 1)
150b57aeab8SRavi Bangoria  *   len = 16 bytes
151b57aeab8SRavi Bangoria  *
152*e68ef121SRavi Bangoria  * @start_addr is inclusive but @end_addr is exclusive.
153b57aeab8SRavi Bangoria  */
154b57aeab8SRavi Bangoria static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw)
155b57aeab8SRavi Bangoria {
156b57aeab8SRavi Bangoria 	u16 max_len = DABR_MAX_LEN;
157b57aeab8SRavi Bangoria 	u16 hw_len;
158b57aeab8SRavi Bangoria 	unsigned long start_addr, end_addr;
159b57aeab8SRavi Bangoria 
160*e68ef121SRavi Bangoria 	start_addr = ALIGN_DOWN(hw->address, HW_BREAKPOINT_SIZE);
161*e68ef121SRavi Bangoria 	end_addr = ALIGN(hw->address + hw->len, HW_BREAKPOINT_SIZE);
162*e68ef121SRavi Bangoria 	hw_len = end_addr - start_addr;
163b57aeab8SRavi Bangoria 
164b57aeab8SRavi Bangoria 	if (dawr_enabled()) {
165b57aeab8SRavi Bangoria 		max_len = DAWR_MAX_LEN;
166b57aeab8SRavi Bangoria 		/* DAWR region can't cross 512 bytes boundary */
167*e68ef121SRavi Bangoria 		if (ALIGN(start_addr, SZ_512M) != ALIGN(end_addr - 1, SZ_512M))
168b57aeab8SRavi Bangoria 			return -EINVAL;
16939413ae0SChristophe Leroy 	} else if (IS_ENABLED(CONFIG_PPC_8xx)) {
17039413ae0SChristophe Leroy 		/* 8xx can setup a range without limitation */
17139413ae0SChristophe Leroy 		max_len = U16_MAX;
172b57aeab8SRavi Bangoria 	}
173b57aeab8SRavi Bangoria 
174b57aeab8SRavi Bangoria 	if (hw_len > max_len)
175b57aeab8SRavi Bangoria 		return -EINVAL;
176b57aeab8SRavi Bangoria 
177b57aeab8SRavi Bangoria 	hw->hw_len = hw_len;
178b57aeab8SRavi Bangoria 	return 0;
179b57aeab8SRavi Bangoria }
180b57aeab8SRavi Bangoria 
181b57aeab8SRavi Bangoria /*
1825aae8a53SK.Prasad  * Validate the arch-specific HW Breakpoint register settings
1835aae8a53SK.Prasad  */
1845d5176baSFrederic Weisbecker int hw_breakpoint_arch_parse(struct perf_event *bp,
1855d5176baSFrederic Weisbecker 			     const struct perf_event_attr *attr,
1865d5176baSFrederic Weisbecker 			     struct arch_hw_breakpoint *hw)
1875aae8a53SK.Prasad {
188b57aeab8SRavi Bangoria 	int ret = -EINVAL;
1895aae8a53SK.Prasad 
190b57aeab8SRavi Bangoria 	if (!bp || !attr->bp_len)
1915aae8a53SK.Prasad 		return ret;
1925aae8a53SK.Prasad 
1935d5176baSFrederic Weisbecker 	hw->type = HW_BRK_TYPE_TRANSLATE;
1945d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_R)
1955d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_READ;
1965d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_W)
1975d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_WRITE;
1985d5176baSFrederic Weisbecker 	if (hw->type == HW_BRK_TYPE_TRANSLATE)
1999422de3eSMichael Neuling 		/* must set alteast read or write */
2005aae8a53SK.Prasad 		return ret;
2015d5176baSFrederic Weisbecker 	if (!attr->exclude_user)
2025d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_USER;
2035d5176baSFrederic Weisbecker 	if (!attr->exclude_kernel)
2045d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_KERNEL;
2055d5176baSFrederic Weisbecker 	if (!attr->exclude_hv)
2065d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_HYP;
2075d5176baSFrederic Weisbecker 	hw->address = attr->bp_addr;
2085d5176baSFrederic Weisbecker 	hw->len = attr->bp_len;
2095aae8a53SK.Prasad 
21085ce9a5dSMichael Neuling 	if (!ppc_breakpoint_available())
21185ce9a5dSMichael Neuling 		return -ENODEV;
212b57aeab8SRavi Bangoria 
213b57aeab8SRavi Bangoria 	return hw_breakpoint_validate_len(hw);
2145aae8a53SK.Prasad }
2155aae8a53SK.Prasad 
2165aae8a53SK.Prasad /*
21706532a67SK.Prasad  * Restores the breakpoint on the debug registers.
21806532a67SK.Prasad  * Invoke this function if it is known that the execution context is
21906532a67SK.Prasad  * about to change to cause loss of MSR_SE settings.
22006532a67SK.Prasad  */
22106532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
22206532a67SK.Prasad {
22306532a67SK.Prasad 	struct arch_hw_breakpoint *info;
22406532a67SK.Prasad 
22506532a67SK.Prasad 	if (likely(!tsk->thread.last_hit_ubp))
22606532a67SK.Prasad 		return;
22706532a67SK.Prasad 
22806532a67SK.Prasad 	info = counter_arch_bp(tsk->thread.last_hit_ubp);
22906532a67SK.Prasad 	regs->msr &= ~MSR_SE;
2304a8a9379SRavi Bangoria 	__set_breakpoint(0, info);
23106532a67SK.Prasad 	tsk->thread.last_hit_ubp = NULL;
23206532a67SK.Prasad }
23306532a67SK.Prasad 
23427985b2aSRavi Bangoria static bool dar_within_range(unsigned long dar, struct arch_hw_breakpoint *info)
235bc01bdf6SRavi Bangoria {
23627985b2aSRavi Bangoria 	return ((info->address <= dar) && (dar - info->address < info->len));
23727985b2aSRavi Bangoria }
238bc01bdf6SRavi Bangoria 
23927985b2aSRavi Bangoria static bool
24027985b2aSRavi Bangoria dar_range_overlaps(unsigned long dar, int size, struct arch_hw_breakpoint *info)
24127985b2aSRavi Bangoria {
24227985b2aSRavi Bangoria 	return ((dar <= info->address + info->len - 1) &&
24327985b2aSRavi Bangoria 		(dar + size - 1 >= info->address));
244bc01bdf6SRavi Bangoria }
245bc01bdf6SRavi Bangoria 
24606532a67SK.Prasad /*
2475aae8a53SK.Prasad  * Handle debug exception notifications.
2485aae8a53SK.Prasad  */
249658d029dSChristophe Leroy static bool stepping_handler(struct pt_regs *regs, struct perf_event *bp,
25027985b2aSRavi Bangoria 			     struct arch_hw_breakpoint *info)
251658d029dSChristophe Leroy {
25294afd069SJordan Niethe 	struct ppc_inst instr = ppc_inst(0);
25327985b2aSRavi Bangoria 	int ret, type, size;
25427985b2aSRavi Bangoria 	struct instruction_op op;
25527985b2aSRavi Bangoria 	unsigned long addr = info->address;
256bc01bdf6SRavi Bangoria 
2575249385aSJordan Niethe 	if (__get_user_instr_inatomic(instr, (void __user *)regs->nip))
258bc01bdf6SRavi Bangoria 		goto fail;
259bc01bdf6SRavi Bangoria 
26027985b2aSRavi Bangoria 	ret = analyse_instr(&op, regs, instr);
26127985b2aSRavi Bangoria 	type = GETTYPE(op.type);
26227985b2aSRavi Bangoria 	size = GETSIZE(op.type);
26327985b2aSRavi Bangoria 
26427985b2aSRavi Bangoria 	if (!ret && (type == LARX || type == STCX)) {
265bc01bdf6SRavi Bangoria 		printk_ratelimited("Breakpoint hit on instruction that can't be emulated."
266bc01bdf6SRavi Bangoria 				   " Breakpoint at 0x%lx will be disabled.\n", addr);
267bc01bdf6SRavi Bangoria 		goto disable;
268bc01bdf6SRavi Bangoria 	}
269658d029dSChristophe Leroy 
27027985b2aSRavi Bangoria 	/*
27127985b2aSRavi Bangoria 	 * If it's extraneous event, we still need to emulate/single-
27227985b2aSRavi Bangoria 	 * step the instruction, but we don't generate an event.
27327985b2aSRavi Bangoria 	 */
27427985b2aSRavi Bangoria 	if (size && !dar_range_overlaps(regs->dar, size, info))
27527985b2aSRavi Bangoria 		info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
27627985b2aSRavi Bangoria 
277658d029dSChristophe Leroy 	/* Do not emulate user-space instructions, instead single-step them */
278658d029dSChristophe Leroy 	if (user_mode(regs)) {
279658d029dSChristophe Leroy 		current->thread.last_hit_ubp = bp;
280658d029dSChristophe Leroy 		regs->msr |= MSR_SE;
281658d029dSChristophe Leroy 		return false;
282658d029dSChristophe Leroy 	}
283658d029dSChristophe Leroy 
284bc01bdf6SRavi Bangoria 	if (!emulate_step(regs, instr))
285bc01bdf6SRavi Bangoria 		goto fail;
286658d029dSChristophe Leroy 
287bc01bdf6SRavi Bangoria 	return true;
288bc01bdf6SRavi Bangoria 
289bc01bdf6SRavi Bangoria fail:
290658d029dSChristophe Leroy 	/*
291bc01bdf6SRavi Bangoria 	 * We've failed in reliably handling the hw-breakpoint. Unregister
292bc01bdf6SRavi Bangoria 	 * it and throw a warning message to let the user know about it.
293658d029dSChristophe Leroy 	 */
294658d029dSChristophe Leroy 	WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
295658d029dSChristophe Leroy 		"0x%lx will be disabled.", addr);
296bc01bdf6SRavi Bangoria 
297bc01bdf6SRavi Bangoria disable:
298658d029dSChristophe Leroy 	perf_event_disable_inatomic(bp);
299658d029dSChristophe Leroy 	return false;
300658d029dSChristophe Leroy }
301658d029dSChristophe Leroy 
30203465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args)
3035aae8a53SK.Prasad {
3045aae8a53SK.Prasad 	int rc = NOTIFY_STOP;
3055aae8a53SK.Prasad 	struct perf_event *bp;
3065aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
3074ad8622dSChristophe Leroy 	struct arch_hw_breakpoint *info;
3085aae8a53SK.Prasad 
3095aae8a53SK.Prasad 	/* Disable breakpoints during exception handling */
3109422de3eSMichael Neuling 	hw_breakpoint_disable();
311574cb248SPaul Mackerras 
3125aae8a53SK.Prasad 	/*
3135aae8a53SK.Prasad 	 * The counter may be concurrently released but that can only
3145aae8a53SK.Prasad 	 * occur from a call_rcu() path. We can then safely fetch
3155aae8a53SK.Prasad 	 * the breakpoint, use its callback, touch its counter
3165aae8a53SK.Prasad 	 * while we are in an rcu_read_lock() path.
3175aae8a53SK.Prasad 	 */
3185aae8a53SK.Prasad 	rcu_read_lock();
3195aae8a53SK.Prasad 
32069111bacSChristoph Lameter 	bp = __this_cpu_read(bp_per_reg);
321c21a493aSRavi Bangoria 	if (!bp) {
322c21a493aSRavi Bangoria 		rc = NOTIFY_DONE;
3235aae8a53SK.Prasad 		goto out;
324c21a493aSRavi Bangoria 	}
3255aae8a53SK.Prasad 	info = counter_arch_bp(bp);
3265aae8a53SK.Prasad 
3275aae8a53SK.Prasad 	/*
3285aae8a53SK.Prasad 	 * Return early after invoking user-callback function without restoring
3295aae8a53SK.Prasad 	 * DABR if the breakpoint is from ptrace which always operates in
3305aae8a53SK.Prasad 	 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
3315aae8a53SK.Prasad 	 * generated in do_dabr().
3325aae8a53SK.Prasad 	 */
333c9e82aebSRavi Bangoria 	if (is_ptrace_bp(bp)) {
3345aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3355aae8a53SK.Prasad 		rc = NOTIFY_DONE;
3365aae8a53SK.Prasad 		goto out;
3375aae8a53SK.Prasad 	}
3385aae8a53SK.Prasad 
339540e07c6SMichael Neuling 	info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
340e08658a6SRavi Bangoria 	if (IS_ENABLED(CONFIG_PPC_8xx)) {
34127985b2aSRavi Bangoria 		if (!dar_within_range(regs->dar, info))
3429422de3eSMichael Neuling 			info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
343e08658a6SRavi Bangoria 	} else {
344e08658a6SRavi Bangoria 		if (!stepping_handler(regs, bp, info))
3455aae8a53SK.Prasad 			goto out;
346e08658a6SRavi Bangoria 	}
3475aae8a53SK.Prasad 
3485aae8a53SK.Prasad 	/*
3495aae8a53SK.Prasad 	 * As a policy, the callback is invoked in a 'trigger-after-execute'
3505aae8a53SK.Prasad 	 * fashion
3515aae8a53SK.Prasad 	 */
3529422de3eSMichael Neuling 	if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
3535aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3545aae8a53SK.Prasad 
3554a8a9379SRavi Bangoria 	__set_breakpoint(0, info);
3565aae8a53SK.Prasad out:
3575aae8a53SK.Prasad 	rcu_read_unlock();
3585aae8a53SK.Prasad 	return rc;
3595aae8a53SK.Prasad }
36003465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler);
3615aae8a53SK.Prasad 
3625aae8a53SK.Prasad /*
3635aae8a53SK.Prasad  * Handle single-step exceptions following a DABR hit.
3645aae8a53SK.Prasad  */
36503465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args)
3665aae8a53SK.Prasad {
3675aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
3685aae8a53SK.Prasad 	struct perf_event *bp = NULL;
3693f4693eeSMichael Neuling 	struct arch_hw_breakpoint *info;
3705aae8a53SK.Prasad 
3715aae8a53SK.Prasad 	bp = current->thread.last_hit_ubp;
3725aae8a53SK.Prasad 	/*
3735aae8a53SK.Prasad 	 * Check if we are single-stepping as a result of a
3745aae8a53SK.Prasad 	 * previous HW Breakpoint exception
3755aae8a53SK.Prasad 	 */
3765aae8a53SK.Prasad 	if (!bp)
3775aae8a53SK.Prasad 		return NOTIFY_DONE;
3785aae8a53SK.Prasad 
3793f4693eeSMichael Neuling 	info = counter_arch_bp(bp);
3805aae8a53SK.Prasad 
3815aae8a53SK.Prasad 	/*
3825aae8a53SK.Prasad 	 * We shall invoke the user-defined callback function in the single
3835aae8a53SK.Prasad 	 * stepping handler to confirm to 'trigger-after-execute' semantics
3845aae8a53SK.Prasad 	 */
3859422de3eSMichael Neuling 	if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
3865aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3875aae8a53SK.Prasad 
3884a8a9379SRavi Bangoria 	__set_breakpoint(0, info);
3895aae8a53SK.Prasad 	current->thread.last_hit_ubp = NULL;
39076b0f133SPaul Mackerras 
39176b0f133SPaul Mackerras 	/*
39276b0f133SPaul Mackerras 	 * If the process was being single-stepped by ptrace, let the
39376b0f133SPaul Mackerras 	 * other single-step actions occur (e.g. generate SIGTRAP).
39476b0f133SPaul Mackerras 	 */
39576b0f133SPaul Mackerras 	if (test_thread_flag(TIF_SINGLESTEP))
39676b0f133SPaul Mackerras 		return NOTIFY_DONE;
39776b0f133SPaul Mackerras 
3985aae8a53SK.Prasad 	return NOTIFY_STOP;
3995aae8a53SK.Prasad }
40003465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction);
4015aae8a53SK.Prasad 
4025aae8a53SK.Prasad /*
4035aae8a53SK.Prasad  * Handle debug exception notifications.
4045aae8a53SK.Prasad  */
40503465f89SNicholas Piggin int hw_breakpoint_exceptions_notify(
4065aae8a53SK.Prasad 		struct notifier_block *unused, unsigned long val, void *data)
4075aae8a53SK.Prasad {
4085aae8a53SK.Prasad 	int ret = NOTIFY_DONE;
4095aae8a53SK.Prasad 
4105aae8a53SK.Prasad 	switch (val) {
4115aae8a53SK.Prasad 	case DIE_DABR_MATCH:
4125aae8a53SK.Prasad 		ret = hw_breakpoint_handler(data);
4135aae8a53SK.Prasad 		break;
4145aae8a53SK.Prasad 	case DIE_SSTEP:
4155aae8a53SK.Prasad 		ret = single_step_dabr_instruction(data);
4165aae8a53SK.Prasad 		break;
4175aae8a53SK.Prasad 	}
4185aae8a53SK.Prasad 
4195aae8a53SK.Prasad 	return ret;
4205aae8a53SK.Prasad }
42103465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
4225aae8a53SK.Prasad 
4235aae8a53SK.Prasad /*
4245aae8a53SK.Prasad  * Release the user breakpoints used by ptrace
4255aae8a53SK.Prasad  */
4265aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
4275aae8a53SK.Prasad {
4286b424efaSRavi Bangoria 	int i;
4295aae8a53SK.Prasad 	struct thread_struct *t = &tsk->thread;
4305aae8a53SK.Prasad 
4316b424efaSRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
4326b424efaSRavi Bangoria 		unregister_hw_breakpoint(t->ptrace_bps[i]);
4336b424efaSRavi Bangoria 		t->ptrace_bps[i] = NULL;
4346b424efaSRavi Bangoria 	}
4355aae8a53SK.Prasad }
4365aae8a53SK.Prasad 
4375aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp)
4385aae8a53SK.Prasad {
4395aae8a53SK.Prasad 	/* TODO */
4405aae8a53SK.Prasad }
441ccbed90bSChristophe Leroy 
442ccbed90bSChristophe Leroy void ptrace_triggered(struct perf_event *bp,
443ccbed90bSChristophe Leroy 		      struct perf_sample_data *data, struct pt_regs *regs)
444ccbed90bSChristophe Leroy {
445ccbed90bSChristophe Leroy 	struct perf_event_attr attr;
446ccbed90bSChristophe Leroy 
447ccbed90bSChristophe Leroy 	/*
448ccbed90bSChristophe Leroy 	 * Disable the breakpoint request here since ptrace has defined a
449ccbed90bSChristophe Leroy 	 * one-shot behaviour for breakpoint exceptions in PPC64.
450ccbed90bSChristophe Leroy 	 * The SIGTRAP signal is generated automatically for us in do_dabr().
451ccbed90bSChristophe Leroy 	 * We don't have to do anything about that here
452ccbed90bSChristophe Leroy 	 */
453ccbed90bSChristophe Leroy 	attr = bp->attr;
454ccbed90bSChristophe Leroy 	attr.disabled = true;
455ccbed90bSChristophe Leroy 	modify_user_hw_breakpoint(bp, &attr);
456ccbed90bSChristophe Leroy }
457