xref: /openbmc/linux/arch/powerpc/kernel/hw_breakpoint.c (revision bd29813ae10698f7bdfb3c68eacbb6464ec701ff)
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>
18f95e5a3dSMarco Elver #include <linux/spinlock.h>
19c1fe190cSMichael Neuling #include <linux/debugfs.h>
20c1fe190cSMichael Neuling #include <linux/init.h>
215aae8a53SK.Prasad 
225aae8a53SK.Prasad #include <asm/hw_breakpoint.h>
235aae8a53SK.Prasad #include <asm/processor.h>
245aae8a53SK.Prasad #include <asm/sstep.h>
2585ce9a5dSMichael Neuling #include <asm/debug.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  */
3474c68810SRavi Bangoria static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM_MAX]);
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 
4674c68810SRavi Bangoria 
47d09ec738SPaul Mackerras /*
485aae8a53SK.Prasad  * Install a perf counter breakpoint.
495aae8a53SK.Prasad  *
505aae8a53SK.Prasad  * We seek a free debug address register and use it for this
515aae8a53SK.Prasad  * breakpoint.
525aae8a53SK.Prasad  *
535aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
545aae8a53SK.Prasad  * and registers local to this cpu.
555aae8a53SK.Prasad  */
565aae8a53SK.Prasad int arch_install_hw_breakpoint(struct perf_event *bp)
575aae8a53SK.Prasad {
585aae8a53SK.Prasad 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
5974c68810SRavi Bangoria 	struct perf_event **slot;
6074c68810SRavi Bangoria 	int i;
615aae8a53SK.Prasad 
6274c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
6374c68810SRavi Bangoria 		slot = this_cpu_ptr(&bp_per_reg[i]);
6474c68810SRavi Bangoria 		if (!*slot) {
655aae8a53SK.Prasad 			*slot = bp;
6674c68810SRavi Bangoria 			break;
6774c68810SRavi Bangoria 		}
6874c68810SRavi Bangoria 	}
6974c68810SRavi Bangoria 
7074c68810SRavi Bangoria 	if (WARN_ONCE(i == nr_wp_slots(), "Can't find any breakpoint slot"))
7174c68810SRavi Bangoria 		return -EBUSY;
725aae8a53SK.Prasad 
735aae8a53SK.Prasad 	/*
745aae8a53SK.Prasad 	 * Do not install DABR values if the instruction must be single-stepped.
755aae8a53SK.Prasad 	 * If so, DABR will be populated in single_step_dabr_instruction().
765aae8a53SK.Prasad 	 */
771e60f356SBenjamin Gray 	if (!info->perf_single_step)
7874c68810SRavi Bangoria 		__set_breakpoint(i, info);
795aae8a53SK.Prasad 
805aae8a53SK.Prasad 	return 0;
815aae8a53SK.Prasad }
825aae8a53SK.Prasad 
835aae8a53SK.Prasad /*
845aae8a53SK.Prasad  * Uninstall the breakpoint contained in the given counter.
855aae8a53SK.Prasad  *
865aae8a53SK.Prasad  * First we search the debug address register it uses and then we disable
875aae8a53SK.Prasad  * it.
885aae8a53SK.Prasad  *
895aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
905aae8a53SK.Prasad  * and registers local to this cpu.
915aae8a53SK.Prasad  */
925aae8a53SK.Prasad void arch_uninstall_hw_breakpoint(struct perf_event *bp)
935aae8a53SK.Prasad {
9474c68810SRavi Bangoria 	struct arch_hw_breakpoint null_brk = {0};
9574c68810SRavi Bangoria 	struct perf_event **slot;
9674c68810SRavi Bangoria 	int i;
975aae8a53SK.Prasad 
9874c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
9974c68810SRavi Bangoria 		slot = this_cpu_ptr(&bp_per_reg[i]);
10074c68810SRavi Bangoria 		if (*slot == bp) {
10174c68810SRavi Bangoria 			*slot = NULL;
10274c68810SRavi Bangoria 			break;
10374c68810SRavi Bangoria 		}
1045aae8a53SK.Prasad 	}
1055aae8a53SK.Prasad 
10674c68810SRavi Bangoria 	if (WARN_ONCE(i == nr_wp_slots(), "Can't find any breakpoint slot"))
10774c68810SRavi Bangoria 		return;
10874c68810SRavi Bangoria 
10974c68810SRavi Bangoria 	__set_breakpoint(i, &null_brk);
1105aae8a53SK.Prasad }
1115aae8a53SK.Prasad 
112c9e82aebSRavi Bangoria static bool is_ptrace_bp(struct perf_event *bp)
113c9e82aebSRavi Bangoria {
114c9e82aebSRavi Bangoria 	return bp->overflow_handler == ptrace_triggered;
115c9e82aebSRavi Bangoria }
116c9e82aebSRavi Bangoria 
1175aae8a53SK.Prasad /*
1185aae8a53SK.Prasad  * Check for virtual address in kernel space.
1195aae8a53SK.Prasad  */
1208e983ff9SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
1215aae8a53SK.Prasad {
1228e983ff9SFrederic Weisbecker 	return is_kernel_addr(hw->address);
1235aae8a53SK.Prasad }
1245aae8a53SK.Prasad 
1255aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type)
1265aae8a53SK.Prasad {
1279422de3eSMichael Neuling 	*gen_bp_type = 0;
1289422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_READ)
1299422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_R;
1309422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_WRITE)
1319422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_W;
1329422de3eSMichael Neuling 	if (*gen_bp_type == 0)
1335aae8a53SK.Prasad 		return -EINVAL;
1345aae8a53SK.Prasad 	return 0;
1355aae8a53SK.Prasad }
1365aae8a53SK.Prasad 
1375aae8a53SK.Prasad /*
138b57aeab8SRavi Bangoria  * Watchpoint match range is always doubleword(8 bytes) aligned on
139b57aeab8SRavi Bangoria  * powerpc. If the given range is crossing doubleword boundary, we
140b57aeab8SRavi Bangoria  * need to increase the length such that next doubleword also get
141b57aeab8SRavi Bangoria  * covered. Ex,
142b57aeab8SRavi Bangoria  *
143b57aeab8SRavi Bangoria  *          address   len = 6 bytes
144b57aeab8SRavi Bangoria  *                |=========.
145b57aeab8SRavi Bangoria  *   |------------v--|------v--------|
146b57aeab8SRavi Bangoria  *   | | | | | | | | | | | | | | | | |
147b57aeab8SRavi Bangoria  *   |---------------|---------------|
148b57aeab8SRavi Bangoria  *    <---8 bytes--->
149b57aeab8SRavi Bangoria  *
150b57aeab8SRavi Bangoria  * In this case, we should configure hw as:
151e68ef121SRavi Bangoria  *   start_addr = address & ~(HW_BREAKPOINT_SIZE - 1)
152b57aeab8SRavi Bangoria  *   len = 16 bytes
153b57aeab8SRavi Bangoria  *
154e68ef121SRavi Bangoria  * @start_addr is inclusive but @end_addr is exclusive.
155b57aeab8SRavi Bangoria  */
156b57aeab8SRavi Bangoria static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw)
157b57aeab8SRavi Bangoria {
158b57aeab8SRavi Bangoria 	u16 max_len = DABR_MAX_LEN;
159b57aeab8SRavi Bangoria 	u16 hw_len;
160b57aeab8SRavi Bangoria 	unsigned long start_addr, end_addr;
161b57aeab8SRavi Bangoria 
162e68ef121SRavi Bangoria 	start_addr = ALIGN_DOWN(hw->address, HW_BREAKPOINT_SIZE);
163e68ef121SRavi Bangoria 	end_addr = ALIGN(hw->address + hw->len, HW_BREAKPOINT_SIZE);
164e68ef121SRavi Bangoria 	hw_len = end_addr - start_addr;
165b57aeab8SRavi Bangoria 
166b57aeab8SRavi Bangoria 	if (dawr_enabled()) {
167b57aeab8SRavi Bangoria 		max_len = DAWR_MAX_LEN;
1683f31e49dSRavi Bangoria 		/* DAWR region can't cross 512 bytes boundary on p10 predecessors */
1693f31e49dSRavi Bangoria 		if (!cpu_has_feature(CPU_FTR_ARCH_31) &&
1703f31e49dSRavi Bangoria 		    (ALIGN_DOWN(start_addr, SZ_512) != ALIGN_DOWN(end_addr - 1, SZ_512)))
171b57aeab8SRavi Bangoria 			return -EINVAL;
17239413ae0SChristophe Leroy 	} else if (IS_ENABLED(CONFIG_PPC_8xx)) {
17339413ae0SChristophe Leroy 		/* 8xx can setup a range without limitation */
17439413ae0SChristophe Leroy 		max_len = U16_MAX;
175b57aeab8SRavi Bangoria 	}
176b57aeab8SRavi Bangoria 
177b57aeab8SRavi Bangoria 	if (hw_len > max_len)
178b57aeab8SRavi Bangoria 		return -EINVAL;
179b57aeab8SRavi Bangoria 
180b57aeab8SRavi Bangoria 	hw->hw_len = hw_len;
181b57aeab8SRavi Bangoria 	return 0;
182b57aeab8SRavi Bangoria }
183b57aeab8SRavi Bangoria 
184b57aeab8SRavi Bangoria /*
1855aae8a53SK.Prasad  * Validate the arch-specific HW Breakpoint register settings
1865aae8a53SK.Prasad  */
1875d5176baSFrederic Weisbecker int hw_breakpoint_arch_parse(struct perf_event *bp,
1885d5176baSFrederic Weisbecker 			     const struct perf_event_attr *attr,
1895d5176baSFrederic Weisbecker 			     struct arch_hw_breakpoint *hw)
1905aae8a53SK.Prasad {
191b57aeab8SRavi Bangoria 	int ret = -EINVAL;
1925aae8a53SK.Prasad 
193b57aeab8SRavi Bangoria 	if (!bp || !attr->bp_len)
1945aae8a53SK.Prasad 		return ret;
1955aae8a53SK.Prasad 
1965d5176baSFrederic Weisbecker 	hw->type = HW_BRK_TYPE_TRANSLATE;
1975d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_R)
1985d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_READ;
1995d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_W)
2005d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_WRITE;
2015d5176baSFrederic Weisbecker 	if (hw->type == HW_BRK_TYPE_TRANSLATE)
2029422de3eSMichael Neuling 		/* must set alteast read or write */
2035aae8a53SK.Prasad 		return ret;
2045d5176baSFrederic Weisbecker 	if (!attr->exclude_user)
2055d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_USER;
2065d5176baSFrederic Weisbecker 	if (!attr->exclude_kernel)
2075d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_KERNEL;
2085d5176baSFrederic Weisbecker 	if (!attr->exclude_hv)
2095d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_HYP;
2105d5176baSFrederic Weisbecker 	hw->address = attr->bp_addr;
2115d5176baSFrederic Weisbecker 	hw->len = attr->bp_len;
2125aae8a53SK.Prasad 
21385ce9a5dSMichael Neuling 	if (!ppc_breakpoint_available())
21485ce9a5dSMichael Neuling 		return -ENODEV;
215b57aeab8SRavi Bangoria 
216b57aeab8SRavi Bangoria 	return hw_breakpoint_validate_len(hw);
2175aae8a53SK.Prasad }
2185aae8a53SK.Prasad 
2195aae8a53SK.Prasad /*
22006532a67SK.Prasad  * Restores the breakpoint on the debug registers.
22106532a67SK.Prasad  * Invoke this function if it is known that the execution context is
22206532a67SK.Prasad  * about to change to cause loss of MSR_SE settings.
2238f8f1cd6SBenjamin Gray  *
2248f8f1cd6SBenjamin Gray  * The perf watchpoint will simply re-trigger once the thread is started again,
2258f8f1cd6SBenjamin Gray  * and the watchpoint handler will set up MSR_SE and perf_single_step as
2268f8f1cd6SBenjamin Gray  * needed.
22706532a67SK.Prasad  */
22806532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
22906532a67SK.Prasad {
23006532a67SK.Prasad 	struct arch_hw_breakpoint *info;
23174c68810SRavi Bangoria 	int i;
23206532a67SK.Prasad 
23374c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
2341e60f356SBenjamin Gray 		struct perf_event *bp = __this_cpu_read(bp_per_reg[i]);
2351e60f356SBenjamin Gray 
2361e60f356SBenjamin Gray 		if (unlikely(bp && counter_arch_bp(bp)->perf_single_step))
23774c68810SRavi Bangoria 			goto reset;
23874c68810SRavi Bangoria 	}
23906532a67SK.Prasad 	return;
24006532a67SK.Prasad 
24174c68810SRavi Bangoria reset:
24259dc5bfcSNicholas Piggin 	regs_set_return_msr(regs, regs->msr & ~MSR_SE);
24374c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
24474c68810SRavi Bangoria 		info = counter_arch_bp(__this_cpu_read(bp_per_reg[i]));
24574c68810SRavi Bangoria 		__set_breakpoint(i, info);
2461e60f356SBenjamin Gray 		info->perf_single_step = false;
24774c68810SRavi Bangoria 	}
24806532a67SK.Prasad }
24906532a67SK.Prasad 
250f6780ce6SRavi Bangoria static bool is_larx_stcx_instr(int type)
251f6780ce6SRavi Bangoria {
252f6780ce6SRavi Bangoria 	return type == LARX || type == STCX;
25374c68810SRavi Bangoria }
25474c68810SRavi Bangoria 
2553d2ffcddSRavi Bangoria static bool is_octword_vsx_instr(int type, int size)
2563d2ffcddSRavi Bangoria {
2573d2ffcddSRavi Bangoria 	return ((type == LOAD_VSX || type == STORE_VSX) && size == 32);
2583d2ffcddSRavi Bangoria }
2593d2ffcddSRavi Bangoria 
260658d029dSChristophe Leroy /*
261bc01bdf6SRavi Bangoria  * We've failed in reliably handling the hw-breakpoint. Unregister
262bc01bdf6SRavi Bangoria  * it and throw a warning message to let the user know about it.
263658d029dSChristophe Leroy  */
264668a6ec6SBenjamin Gray static void handler_error(struct perf_event *bp)
26574c68810SRavi Bangoria {
26674c68810SRavi Bangoria 	WARN(1, "Unable to handle hardware breakpoint. Breakpoint at 0x%lx will be disabled.",
267668a6ec6SBenjamin Gray 	     counter_arch_bp(bp)->address);
268658d029dSChristophe Leroy 	perf_event_disable_inatomic(bp);
26974c68810SRavi Bangoria }
27074c68810SRavi Bangoria 
271668a6ec6SBenjamin Gray static void larx_stcx_err(struct perf_event *bp)
27274c68810SRavi Bangoria {
27374c68810SRavi Bangoria 	printk_ratelimited("Breakpoint hit on instruction that can't be emulated. Breakpoint at 0x%lx will be disabled.\n",
274668a6ec6SBenjamin Gray 			   counter_arch_bp(bp)->address);
27574c68810SRavi Bangoria 	perf_event_disable_inatomic(bp);
27674c68810SRavi Bangoria }
27774c68810SRavi Bangoria 
27874c68810SRavi Bangoria static bool stepping_handler(struct pt_regs *regs, struct perf_event **bp,
279668a6ec6SBenjamin Gray 			     int *hit, ppc_inst_t instr)
28074c68810SRavi Bangoria {
28174c68810SRavi Bangoria 	int i;
28274c68810SRavi Bangoria 	int stepped;
28374c68810SRavi Bangoria 
28474c68810SRavi Bangoria 	/* Do not emulate user-space instructions, instead single-step them */
28574c68810SRavi Bangoria 	if (user_mode(regs)) {
28674c68810SRavi Bangoria 		for (i = 0; i < nr_wp_slots(); i++) {
28774c68810SRavi Bangoria 			if (!hit[i])
28874c68810SRavi Bangoria 				continue;
2891e60f356SBenjamin Gray 
2901e60f356SBenjamin Gray 			counter_arch_bp(bp[i])->perf_single_step = true;
291668a6ec6SBenjamin Gray 			bp[i] = NULL;
29274c68810SRavi Bangoria 		}
29359dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, regs->msr | MSR_SE);
294658d029dSChristophe Leroy 		return false;
295658d029dSChristophe Leroy 	}
296658d029dSChristophe Leroy 
29774c68810SRavi Bangoria 	stepped = emulate_step(regs, instr);
29874c68810SRavi Bangoria 	if (!stepped) {
29974c68810SRavi Bangoria 		for (i = 0; i < nr_wp_slots(); i++) {
30074c68810SRavi Bangoria 			if (!hit[i])
30174c68810SRavi Bangoria 				continue;
302668a6ec6SBenjamin Gray 			handler_error(bp[i]);
303668a6ec6SBenjamin Gray 			bp[i] = NULL;
30474c68810SRavi Bangoria 		}
30574c68810SRavi Bangoria 		return false;
30674c68810SRavi Bangoria 	}
30774c68810SRavi Bangoria 	return true;
30874c68810SRavi Bangoria }
30974c68810SRavi Bangoria 
310668a6ec6SBenjamin Gray static void handle_p10dd1_spurious_exception(struct perf_event **bp,
3113d2ffcddSRavi Bangoria 					     int *hit, unsigned long ea)
3123d2ffcddSRavi Bangoria {
3133d2ffcddSRavi Bangoria 	int i;
3143d2ffcddSRavi Bangoria 	unsigned long hw_end_addr;
3153d2ffcddSRavi Bangoria 
3163d2ffcddSRavi Bangoria 	/*
3173d2ffcddSRavi Bangoria 	 * Handle spurious exception only when any bp_per_reg is set.
3183d2ffcddSRavi Bangoria 	 * Otherwise this might be created by xmon and not actually a
3193d2ffcddSRavi Bangoria 	 * spurious exception.
3203d2ffcddSRavi Bangoria 	 */
3213d2ffcddSRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
322668a6ec6SBenjamin Gray 		struct arch_hw_breakpoint *info;
323668a6ec6SBenjamin Gray 
324668a6ec6SBenjamin Gray 		if (!bp[i])
3253d2ffcddSRavi Bangoria 			continue;
3263d2ffcddSRavi Bangoria 
327668a6ec6SBenjamin Gray 		info = counter_arch_bp(bp[i]);
328668a6ec6SBenjamin Gray 
329668a6ec6SBenjamin Gray 		hw_end_addr = ALIGN(info->address + info->len, HW_BREAKPOINT_SIZE);
3303d2ffcddSRavi Bangoria 
3313d2ffcddSRavi Bangoria 		/*
3323d2ffcddSRavi Bangoria 		 * Ending address of DAWR range is less than starting
3333d2ffcddSRavi Bangoria 		 * address of op.
3343d2ffcddSRavi Bangoria 		 */
3353d2ffcddSRavi Bangoria 		if ((hw_end_addr - 1) >= ea)
3363d2ffcddSRavi Bangoria 			continue;
3373d2ffcddSRavi Bangoria 
3383d2ffcddSRavi Bangoria 		/*
3393d2ffcddSRavi Bangoria 		 * Those addresses need to be in the same or in two
3403d2ffcddSRavi Bangoria 		 * consecutive 512B blocks;
3413d2ffcddSRavi Bangoria 		 */
3423d2ffcddSRavi Bangoria 		if (((hw_end_addr - 1) >> 10) != (ea >> 10))
3433d2ffcddSRavi Bangoria 			continue;
3443d2ffcddSRavi Bangoria 
3453d2ffcddSRavi Bangoria 		/*
3463d2ffcddSRavi Bangoria 		 * 'op address + 64B' generates an address that has a
3473d2ffcddSRavi Bangoria 		 * carry into bit 52 (crosses 2K boundary).
3483d2ffcddSRavi Bangoria 		 */
3493d2ffcddSRavi Bangoria 		if ((ea & 0x800) == ((ea + 64) & 0x800))
3503d2ffcddSRavi Bangoria 			continue;
3513d2ffcddSRavi Bangoria 
3523d2ffcddSRavi Bangoria 		break;
3533d2ffcddSRavi Bangoria 	}
3543d2ffcddSRavi Bangoria 
3553d2ffcddSRavi Bangoria 	if (i == nr_wp_slots())
3563d2ffcddSRavi Bangoria 		return;
3573d2ffcddSRavi Bangoria 
3583d2ffcddSRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
359668a6ec6SBenjamin Gray 		if (bp[i]) {
3603d2ffcddSRavi Bangoria 			hit[i] = 1;
361668a6ec6SBenjamin Gray 			counter_arch_bp(bp[i])->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
3623d2ffcddSRavi Bangoria 		}
3633d2ffcddSRavi Bangoria 	}
3643d2ffcddSRavi Bangoria }
3653d2ffcddSRavi Bangoria 
36603465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args)
3675aae8a53SK.Prasad {
36874c68810SRavi Bangoria 	bool err = false;
3695aae8a53SK.Prasad 	int rc = NOTIFY_STOP;
37074c68810SRavi Bangoria 	struct perf_event *bp[HBP_NUM_MAX] = { NULL };
3715aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
37274c68810SRavi Bangoria 	int i;
37374c68810SRavi Bangoria 	int hit[HBP_NUM_MAX] = {0};
37474c68810SRavi Bangoria 	int nr_hit = 0;
37574c68810SRavi Bangoria 	bool ptrace_bp = false;
376c545b9f0SChristophe Leroy 	ppc_inst_t instr = ppc_inst(0);
37774c68810SRavi Bangoria 	int type = 0;
37874c68810SRavi Bangoria 	int size = 0;
379f6680275SRussell Currey 	unsigned long ea = 0;
3805aae8a53SK.Prasad 
3815aae8a53SK.Prasad 	/* Disable breakpoints during exception handling */
3829422de3eSMichael Neuling 	hw_breakpoint_disable();
383574cb248SPaul Mackerras 
3845aae8a53SK.Prasad 	/*
3855aae8a53SK.Prasad 	 * The counter may be concurrently released but that can only
3865aae8a53SK.Prasad 	 * occur from a call_rcu() path. We can then safely fetch
3875aae8a53SK.Prasad 	 * the breakpoint, use its callback, touch its counter
3885aae8a53SK.Prasad 	 * while we are in an rcu_read_lock() path.
3895aae8a53SK.Prasad 	 */
3905aae8a53SK.Prasad 	rcu_read_lock();
3915aae8a53SK.Prasad 
39274c68810SRavi Bangoria 	if (!IS_ENABLED(CONFIG_PPC_8xx))
393edc8dd99SRavi Bangoria 		wp_get_instr_detail(regs, &instr, &type, &size, &ea);
39474c68810SRavi Bangoria 
39574c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
396668a6ec6SBenjamin Gray 		struct arch_hw_breakpoint *info;
397668a6ec6SBenjamin Gray 
39874c68810SRavi Bangoria 		bp[i] = __this_cpu_read(bp_per_reg[i]);
39974c68810SRavi Bangoria 		if (!bp[i])
40074c68810SRavi Bangoria 			continue;
40174c68810SRavi Bangoria 
402668a6ec6SBenjamin Gray 		info = counter_arch_bp(bp[i]);
403668a6ec6SBenjamin Gray 		info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
40474c68810SRavi Bangoria 
405668a6ec6SBenjamin Gray 		if (wp_check_constraints(regs, instr, ea, type, size, info)) {
40674c68810SRavi Bangoria 			if (!IS_ENABLED(CONFIG_PPC_8xx) &&
40774c68810SRavi Bangoria 			    ppc_inst_equal(instr, ppc_inst(0))) {
408668a6ec6SBenjamin Gray 				handler_error(bp[i]);
409668a6ec6SBenjamin Gray 				bp[i] = NULL;
41074c68810SRavi Bangoria 				err = 1;
41174c68810SRavi Bangoria 				continue;
41274c68810SRavi Bangoria 			}
41374c68810SRavi Bangoria 
41474c68810SRavi Bangoria 			if (is_ptrace_bp(bp[i]))
41574c68810SRavi Bangoria 				ptrace_bp = true;
41674c68810SRavi Bangoria 			hit[i] = 1;
41774c68810SRavi Bangoria 			nr_hit++;
41874c68810SRavi Bangoria 		}
41974c68810SRavi Bangoria 	}
42074c68810SRavi Bangoria 
42174c68810SRavi Bangoria 	if (err)
42274c68810SRavi Bangoria 		goto reset;
42374c68810SRavi Bangoria 
42474c68810SRavi Bangoria 	if (!nr_hit) {
4253d2ffcddSRavi Bangoria 		/* Workaround for Power10 DD1 */
4263d2ffcddSRavi Bangoria 		if (!IS_ENABLED(CONFIG_PPC_8xx) && mfspr(SPRN_PVR) == 0x800100 &&
4273d2ffcddSRavi Bangoria 		    is_octword_vsx_instr(type, size)) {
428668a6ec6SBenjamin Gray 			handle_p10dd1_spurious_exception(bp, hit, ea);
4293d2ffcddSRavi Bangoria 		} else {
430c21a493aSRavi Bangoria 			rc = NOTIFY_DONE;
4315aae8a53SK.Prasad 			goto out;
432c21a493aSRavi Bangoria 		}
4333d2ffcddSRavi Bangoria 	}
4345aae8a53SK.Prasad 
4355aae8a53SK.Prasad 	/*
4365aae8a53SK.Prasad 	 * Return early after invoking user-callback function without restoring
4375aae8a53SK.Prasad 	 * DABR if the breakpoint is from ptrace which always operates in
4385aae8a53SK.Prasad 	 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
4395aae8a53SK.Prasad 	 * generated in do_dabr().
4405aae8a53SK.Prasad 	 */
44174c68810SRavi Bangoria 	if (ptrace_bp) {
44274c68810SRavi Bangoria 		for (i = 0; i < nr_wp_slots(); i++) {
443*bd29813aSBenjamin Gray 			if (!hit[i] || !is_ptrace_bp(bp[i]))
44474c68810SRavi Bangoria 				continue;
44574c68810SRavi Bangoria 			perf_bp_event(bp[i], regs);
446668a6ec6SBenjamin Gray 			bp[i] = NULL;
44774c68810SRavi Bangoria 		}
4485aae8a53SK.Prasad 		rc = NOTIFY_DONE;
44974c68810SRavi Bangoria 		goto reset;
4505aae8a53SK.Prasad 	}
4515aae8a53SK.Prasad 
45274c68810SRavi Bangoria 	if (!IS_ENABLED(CONFIG_PPC_8xx)) {
453f6780ce6SRavi Bangoria 		if (is_larx_stcx_instr(type)) {
45474c68810SRavi Bangoria 			for (i = 0; i < nr_wp_slots(); i++) {
45574c68810SRavi Bangoria 				if (!hit[i])
45674c68810SRavi Bangoria 					continue;
457668a6ec6SBenjamin Gray 				larx_stcx_err(bp[i]);
458668a6ec6SBenjamin Gray 				bp[i] = NULL;
45974c68810SRavi Bangoria 			}
46074c68810SRavi Bangoria 			goto reset;
46174c68810SRavi Bangoria 		}
46274c68810SRavi Bangoria 
463668a6ec6SBenjamin Gray 		if (!stepping_handler(regs, bp, hit, instr))
46474c68810SRavi Bangoria 			goto reset;
465e08658a6SRavi Bangoria 	}
4665aae8a53SK.Prasad 
4675aae8a53SK.Prasad 	/*
4685aae8a53SK.Prasad 	 * As a policy, the callback is invoked in a 'trigger-after-execute'
4695aae8a53SK.Prasad 	 * fashion
4705aae8a53SK.Prasad 	 */
47174c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
47274c68810SRavi Bangoria 		if (!hit[i])
47374c68810SRavi Bangoria 			continue;
474668a6ec6SBenjamin Gray 		if (!(counter_arch_bp(bp[i])->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
47574c68810SRavi Bangoria 			perf_bp_event(bp[i], regs);
47674c68810SRavi Bangoria 	}
4775aae8a53SK.Prasad 
47874c68810SRavi Bangoria reset:
47974c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
480668a6ec6SBenjamin Gray 		if (!bp[i])
48174c68810SRavi Bangoria 			continue;
482668a6ec6SBenjamin Gray 		__set_breakpoint(i, counter_arch_bp(bp[i]));
48374c68810SRavi Bangoria 	}
48474c68810SRavi Bangoria 
4855aae8a53SK.Prasad out:
4865aae8a53SK.Prasad 	rcu_read_unlock();
4875aae8a53SK.Prasad 	return rc;
4885aae8a53SK.Prasad }
48903465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler);
4905aae8a53SK.Prasad 
4915aae8a53SK.Prasad /*
4925aae8a53SK.Prasad  * Handle single-step exceptions following a DABR hit.
4935aae8a53SK.Prasad  */
49403465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args)
4955aae8a53SK.Prasad {
4965aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
49774c68810SRavi Bangoria 	bool found = false;
4985aae8a53SK.Prasad 
4995aae8a53SK.Prasad 	/*
5005aae8a53SK.Prasad 	 * Check if we are single-stepping as a result of a
5015aae8a53SK.Prasad 	 * previous HW Breakpoint exception
5025aae8a53SK.Prasad 	 */
5031e60f356SBenjamin Gray 	for (int i = 0; i < nr_wp_slots(); i++) {
5041e60f356SBenjamin Gray 		struct perf_event *bp;
5051e60f356SBenjamin Gray 		struct arch_hw_breakpoint *info;
5061e60f356SBenjamin Gray 
5071e60f356SBenjamin Gray 		bp = __this_cpu_read(bp_per_reg[i]);
5085aae8a53SK.Prasad 
50974c68810SRavi Bangoria 		if (!bp)
51074c68810SRavi Bangoria 			continue;
51174c68810SRavi Bangoria 
5123f4693eeSMichael Neuling 		info = counter_arch_bp(bp);
5135aae8a53SK.Prasad 
5141e60f356SBenjamin Gray 		if (!info->perf_single_step)
5151e60f356SBenjamin Gray 			continue;
5161e60f356SBenjamin Gray 
5171e60f356SBenjamin Gray 		found = true;
5181e60f356SBenjamin Gray 
5195aae8a53SK.Prasad 		/*
52074c68810SRavi Bangoria 		 * We shall invoke the user-defined callback function in the
52174c68810SRavi Bangoria 		 * single stepping handler to confirm to 'trigger-after-execute'
52274c68810SRavi Bangoria 		 * semantics
5235aae8a53SK.Prasad 		 */
5249422de3eSMichael Neuling 		if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
5255aae8a53SK.Prasad 			perf_bp_event(bp, regs);
5261e60f356SBenjamin Gray 
5271e60f356SBenjamin Gray 		info->perf_single_step = false;
5281e60f356SBenjamin Gray 		__set_breakpoint(i, counter_arch_bp(bp));
52974c68810SRavi Bangoria 	}
53076b0f133SPaul Mackerras 
53176b0f133SPaul Mackerras 	/*
53276b0f133SPaul Mackerras 	 * If the process was being single-stepped by ptrace, let the
53376b0f133SPaul Mackerras 	 * other single-step actions occur (e.g. generate SIGTRAP).
53476b0f133SPaul Mackerras 	 */
5355a2d8b9cSBenjamin Gray 	if (!found || test_thread_flag(TIF_SINGLESTEP))
53676b0f133SPaul Mackerras 		return NOTIFY_DONE;
53776b0f133SPaul Mackerras 
5385aae8a53SK.Prasad 	return NOTIFY_STOP;
5395aae8a53SK.Prasad }
54003465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction);
5415aae8a53SK.Prasad 
5425aae8a53SK.Prasad /*
5435aae8a53SK.Prasad  * Handle debug exception notifications.
5445aae8a53SK.Prasad  */
54503465f89SNicholas Piggin int hw_breakpoint_exceptions_notify(
5465aae8a53SK.Prasad 		struct notifier_block *unused, unsigned long val, void *data)
5475aae8a53SK.Prasad {
5485aae8a53SK.Prasad 	int ret = NOTIFY_DONE;
5495aae8a53SK.Prasad 
5505aae8a53SK.Prasad 	switch (val) {
5515aae8a53SK.Prasad 	case DIE_DABR_MATCH:
5525aae8a53SK.Prasad 		ret = hw_breakpoint_handler(data);
5535aae8a53SK.Prasad 		break;
5545aae8a53SK.Prasad 	case DIE_SSTEP:
5555aae8a53SK.Prasad 		ret = single_step_dabr_instruction(data);
5565aae8a53SK.Prasad 		break;
5575aae8a53SK.Prasad 	}
5585aae8a53SK.Prasad 
5595aae8a53SK.Prasad 	return ret;
5605aae8a53SK.Prasad }
56103465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
5625aae8a53SK.Prasad 
5635aae8a53SK.Prasad /*
5645aae8a53SK.Prasad  * Release the user breakpoints used by ptrace
5655aae8a53SK.Prasad  */
5665aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
5675aae8a53SK.Prasad {
5686b424efaSRavi Bangoria 	int i;
5695aae8a53SK.Prasad 	struct thread_struct *t = &tsk->thread;
5705aae8a53SK.Prasad 
5716b424efaSRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
5726b424efaSRavi Bangoria 		unregister_hw_breakpoint(t->ptrace_bps[i]);
5736b424efaSRavi Bangoria 		t->ptrace_bps[i] = NULL;
5746b424efaSRavi Bangoria 	}
5755aae8a53SK.Prasad }
5765aae8a53SK.Prasad 
5775aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp)
5785aae8a53SK.Prasad {
5795aae8a53SK.Prasad 	/* TODO */
5805aae8a53SK.Prasad }
581ccbed90bSChristophe Leroy 
582ccbed90bSChristophe Leroy void ptrace_triggered(struct perf_event *bp,
583ccbed90bSChristophe Leroy 		      struct perf_sample_data *data, struct pt_regs *regs)
584ccbed90bSChristophe Leroy {
585ccbed90bSChristophe Leroy 	struct perf_event_attr attr;
586ccbed90bSChristophe Leroy 
587ccbed90bSChristophe Leroy 	/*
588ccbed90bSChristophe Leroy 	 * Disable the breakpoint request here since ptrace has defined a
589ccbed90bSChristophe Leroy 	 * one-shot behaviour for breakpoint exceptions in PPC64.
590ccbed90bSChristophe Leroy 	 * The SIGTRAP signal is generated automatically for us in do_dabr().
591ccbed90bSChristophe Leroy 	 * We don't have to do anything about that here
592ccbed90bSChristophe Leroy 	 */
593ccbed90bSChristophe Leroy 	attr = bp->attr;
594ccbed90bSChristophe Leroy 	attr.disabled = true;
595ccbed90bSChristophe Leroy 	modify_user_hw_breakpoint(bp, &attr);
596ccbed90bSChristophe Leroy }
597