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 */
hw_breakpoint_slots(int type)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 */
arch_install_hw_breakpoint(struct perf_event * bp)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 */
arch_uninstall_hw_breakpoint(struct perf_event * bp)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
is_ptrace_bp(struct perf_event * bp)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 */
arch_check_bp_in_kernelspace(struct arch_hw_breakpoint * hw)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
arch_bp_generic_fields(int type,int * gen_bp_type)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 */
hw_breakpoint_validate_len(struct arch_hw_breakpoint * hw)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 */
hw_breakpoint_arch_parse(struct perf_event * bp,const struct perf_event_attr * attr,struct arch_hw_breakpoint * hw)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 */
thread_change_pc(struct task_struct * tsk,struct pt_regs * regs)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
233cc879ab3SBenjamin Gray preempt_disable();
234cc879ab3SBenjamin Gray
23574c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
2361e60f356SBenjamin Gray struct perf_event *bp = __this_cpu_read(bp_per_reg[i]);
2371e60f356SBenjamin Gray
2381e60f356SBenjamin Gray if (unlikely(bp && counter_arch_bp(bp)->perf_single_step))
23974c68810SRavi Bangoria goto reset;
24074c68810SRavi Bangoria }
241cc879ab3SBenjamin Gray goto out;
24206532a67SK.Prasad
24374c68810SRavi Bangoria reset:
24459dc5bfcSNicholas Piggin regs_set_return_msr(regs, regs->msr & ~MSR_SE);
24574c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
24674c68810SRavi Bangoria info = counter_arch_bp(__this_cpu_read(bp_per_reg[i]));
24774c68810SRavi Bangoria __set_breakpoint(i, info);
2481e60f356SBenjamin Gray info->perf_single_step = false;
24974c68810SRavi Bangoria }
250cc879ab3SBenjamin Gray
251cc879ab3SBenjamin Gray out:
252cc879ab3SBenjamin Gray preempt_enable();
25306532a67SK.Prasad }
25406532a67SK.Prasad
is_larx_stcx_instr(int type)255f6780ce6SRavi Bangoria static bool is_larx_stcx_instr(int type)
256f6780ce6SRavi Bangoria {
257f6780ce6SRavi Bangoria return type == LARX || type == STCX;
25874c68810SRavi Bangoria }
25974c68810SRavi Bangoria
is_octword_vsx_instr(int type,int size)2603d2ffcddSRavi Bangoria static bool is_octword_vsx_instr(int type, int size)
2613d2ffcddSRavi Bangoria {
2623d2ffcddSRavi Bangoria return ((type == LOAD_VSX || type == STORE_VSX) && size == 32);
2633d2ffcddSRavi Bangoria }
2643d2ffcddSRavi Bangoria
265658d029dSChristophe Leroy /*
266bc01bdf6SRavi Bangoria * We've failed in reliably handling the hw-breakpoint. Unregister
267bc01bdf6SRavi Bangoria * it and throw a warning message to let the user know about it.
268658d029dSChristophe Leroy */
handler_error(struct perf_event * bp)269668a6ec6SBenjamin Gray static void handler_error(struct perf_event *bp)
27074c68810SRavi Bangoria {
27174c68810SRavi Bangoria WARN(1, "Unable to handle hardware breakpoint. Breakpoint at 0x%lx will be disabled.",
272668a6ec6SBenjamin Gray counter_arch_bp(bp)->address);
273658d029dSChristophe Leroy perf_event_disable_inatomic(bp);
27474c68810SRavi Bangoria }
27574c68810SRavi Bangoria
larx_stcx_err(struct perf_event * bp)276668a6ec6SBenjamin Gray static void larx_stcx_err(struct perf_event *bp)
27774c68810SRavi Bangoria {
27874c68810SRavi Bangoria printk_ratelimited("Breakpoint hit on instruction that can't be emulated. Breakpoint at 0x%lx will be disabled.\n",
279668a6ec6SBenjamin Gray counter_arch_bp(bp)->address);
28074c68810SRavi Bangoria perf_event_disable_inatomic(bp);
28174c68810SRavi Bangoria }
28274c68810SRavi Bangoria
stepping_handler(struct pt_regs * regs,struct perf_event ** bp,int * hit,ppc_inst_t instr)28374c68810SRavi Bangoria static bool stepping_handler(struct pt_regs *regs, struct perf_event **bp,
284668a6ec6SBenjamin Gray int *hit, ppc_inst_t instr)
28574c68810SRavi Bangoria {
28674c68810SRavi Bangoria int i;
28774c68810SRavi Bangoria int stepped;
28874c68810SRavi Bangoria
28974c68810SRavi Bangoria /* Do not emulate user-space instructions, instead single-step them */
29074c68810SRavi Bangoria if (user_mode(regs)) {
29174c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
29274c68810SRavi Bangoria if (!hit[i])
29374c68810SRavi Bangoria continue;
2941e60f356SBenjamin Gray
2951e60f356SBenjamin Gray counter_arch_bp(bp[i])->perf_single_step = true;
296668a6ec6SBenjamin Gray bp[i] = NULL;
29774c68810SRavi Bangoria }
29859dc5bfcSNicholas Piggin regs_set_return_msr(regs, regs->msr | MSR_SE);
299658d029dSChristophe Leroy return false;
300658d029dSChristophe Leroy }
301658d029dSChristophe Leroy
30274c68810SRavi Bangoria stepped = emulate_step(regs, instr);
30374c68810SRavi Bangoria if (!stepped) {
30474c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
30574c68810SRavi Bangoria if (!hit[i])
30674c68810SRavi Bangoria continue;
307668a6ec6SBenjamin Gray handler_error(bp[i]);
308668a6ec6SBenjamin Gray bp[i] = NULL;
30974c68810SRavi Bangoria }
31074c68810SRavi Bangoria return false;
31174c68810SRavi Bangoria }
31274c68810SRavi Bangoria return true;
31374c68810SRavi Bangoria }
31474c68810SRavi Bangoria
handle_p10dd1_spurious_exception(struct perf_event ** bp,int * hit,unsigned long ea)315668a6ec6SBenjamin Gray static void handle_p10dd1_spurious_exception(struct perf_event **bp,
3163d2ffcddSRavi Bangoria int *hit, unsigned long ea)
3173d2ffcddSRavi Bangoria {
3183d2ffcddSRavi Bangoria int i;
3193d2ffcddSRavi Bangoria unsigned long hw_end_addr;
3203d2ffcddSRavi Bangoria
3213d2ffcddSRavi Bangoria /*
3223d2ffcddSRavi Bangoria * Handle spurious exception only when any bp_per_reg is set.
3233d2ffcddSRavi Bangoria * Otherwise this might be created by xmon and not actually a
3243d2ffcddSRavi Bangoria * spurious exception.
3253d2ffcddSRavi Bangoria */
3263d2ffcddSRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
327668a6ec6SBenjamin Gray struct arch_hw_breakpoint *info;
328668a6ec6SBenjamin Gray
329668a6ec6SBenjamin Gray if (!bp[i])
3303d2ffcddSRavi Bangoria continue;
3313d2ffcddSRavi Bangoria
332668a6ec6SBenjamin Gray info = counter_arch_bp(bp[i]);
333668a6ec6SBenjamin Gray
334668a6ec6SBenjamin Gray hw_end_addr = ALIGN(info->address + info->len, HW_BREAKPOINT_SIZE);
3353d2ffcddSRavi Bangoria
3363d2ffcddSRavi Bangoria /*
3373d2ffcddSRavi Bangoria * Ending address of DAWR range is less than starting
3383d2ffcddSRavi Bangoria * address of op.
3393d2ffcddSRavi Bangoria */
3403d2ffcddSRavi Bangoria if ((hw_end_addr - 1) >= ea)
3413d2ffcddSRavi Bangoria continue;
3423d2ffcddSRavi Bangoria
3433d2ffcddSRavi Bangoria /*
3443d2ffcddSRavi Bangoria * Those addresses need to be in the same or in two
3453d2ffcddSRavi Bangoria * consecutive 512B blocks;
3463d2ffcddSRavi Bangoria */
3473d2ffcddSRavi Bangoria if (((hw_end_addr - 1) >> 10) != (ea >> 10))
3483d2ffcddSRavi Bangoria continue;
3493d2ffcddSRavi Bangoria
3503d2ffcddSRavi Bangoria /*
3513d2ffcddSRavi Bangoria * 'op address + 64B' generates an address that has a
3523d2ffcddSRavi Bangoria * carry into bit 52 (crosses 2K boundary).
3533d2ffcddSRavi Bangoria */
3543d2ffcddSRavi Bangoria if ((ea & 0x800) == ((ea + 64) & 0x800))
3553d2ffcddSRavi Bangoria continue;
3563d2ffcddSRavi Bangoria
3573d2ffcddSRavi Bangoria break;
3583d2ffcddSRavi Bangoria }
3593d2ffcddSRavi Bangoria
3603d2ffcddSRavi Bangoria if (i == nr_wp_slots())
3613d2ffcddSRavi Bangoria return;
3623d2ffcddSRavi Bangoria
3633d2ffcddSRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
364668a6ec6SBenjamin Gray if (bp[i]) {
3653d2ffcddSRavi Bangoria hit[i] = 1;
366668a6ec6SBenjamin Gray counter_arch_bp(bp[i])->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
3673d2ffcddSRavi Bangoria }
3683d2ffcddSRavi Bangoria }
3693d2ffcddSRavi Bangoria }
3703d2ffcddSRavi Bangoria
371*27646b2eSBenjamin Gray /*
372*27646b2eSBenjamin Gray * Handle a DABR or DAWR exception.
373*27646b2eSBenjamin Gray *
374*27646b2eSBenjamin Gray * Called in atomic context.
375*27646b2eSBenjamin Gray */
hw_breakpoint_handler(struct die_args * args)37603465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args)
3775aae8a53SK.Prasad {
37874c68810SRavi Bangoria bool err = false;
3795aae8a53SK.Prasad int rc = NOTIFY_STOP;
38074c68810SRavi Bangoria struct perf_event *bp[HBP_NUM_MAX] = { NULL };
3815aae8a53SK.Prasad struct pt_regs *regs = args->regs;
38274c68810SRavi Bangoria int i;
38374c68810SRavi Bangoria int hit[HBP_NUM_MAX] = {0};
38474c68810SRavi Bangoria int nr_hit = 0;
38574c68810SRavi Bangoria bool ptrace_bp = false;
386c545b9f0SChristophe Leroy ppc_inst_t instr = ppc_inst(0);
38774c68810SRavi Bangoria int type = 0;
38874c68810SRavi Bangoria int size = 0;
389f6680275SRussell Currey unsigned long ea = 0;
3905aae8a53SK.Prasad
3915aae8a53SK.Prasad /* Disable breakpoints during exception handling */
3929422de3eSMichael Neuling hw_breakpoint_disable();
393574cb248SPaul Mackerras
3945aae8a53SK.Prasad /*
3955aae8a53SK.Prasad * The counter may be concurrently released but that can only
3965aae8a53SK.Prasad * occur from a call_rcu() path. We can then safely fetch
3975aae8a53SK.Prasad * the breakpoint, use its callback, touch its counter
3985aae8a53SK.Prasad * while we are in an rcu_read_lock() path.
3995aae8a53SK.Prasad */
4005aae8a53SK.Prasad rcu_read_lock();
4015aae8a53SK.Prasad
40274c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx))
403edc8dd99SRavi Bangoria wp_get_instr_detail(regs, &instr, &type, &size, &ea);
40474c68810SRavi Bangoria
40574c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
406668a6ec6SBenjamin Gray struct arch_hw_breakpoint *info;
407668a6ec6SBenjamin Gray
40874c68810SRavi Bangoria bp[i] = __this_cpu_read(bp_per_reg[i]);
40974c68810SRavi Bangoria if (!bp[i])
41074c68810SRavi Bangoria continue;
41174c68810SRavi Bangoria
412668a6ec6SBenjamin Gray info = counter_arch_bp(bp[i]);
413668a6ec6SBenjamin Gray info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
41474c68810SRavi Bangoria
415668a6ec6SBenjamin Gray if (wp_check_constraints(regs, instr, ea, type, size, info)) {
41674c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx) &&
41774c68810SRavi Bangoria ppc_inst_equal(instr, ppc_inst(0))) {
418668a6ec6SBenjamin Gray handler_error(bp[i]);
419668a6ec6SBenjamin Gray bp[i] = NULL;
42074c68810SRavi Bangoria err = 1;
42174c68810SRavi Bangoria continue;
42274c68810SRavi Bangoria }
42374c68810SRavi Bangoria
42474c68810SRavi Bangoria if (is_ptrace_bp(bp[i]))
42574c68810SRavi Bangoria ptrace_bp = true;
42674c68810SRavi Bangoria hit[i] = 1;
42774c68810SRavi Bangoria nr_hit++;
42874c68810SRavi Bangoria }
42974c68810SRavi Bangoria }
43074c68810SRavi Bangoria
43174c68810SRavi Bangoria if (err)
43274c68810SRavi Bangoria goto reset;
43374c68810SRavi Bangoria
43474c68810SRavi Bangoria if (!nr_hit) {
4353d2ffcddSRavi Bangoria /* Workaround for Power10 DD1 */
4363d2ffcddSRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx) && mfspr(SPRN_PVR) == 0x800100 &&
4373d2ffcddSRavi Bangoria is_octword_vsx_instr(type, size)) {
438668a6ec6SBenjamin Gray handle_p10dd1_spurious_exception(bp, hit, ea);
4393d2ffcddSRavi Bangoria } else {
440c21a493aSRavi Bangoria rc = NOTIFY_DONE;
4415aae8a53SK.Prasad goto out;
442c21a493aSRavi Bangoria }
4433d2ffcddSRavi Bangoria }
4445aae8a53SK.Prasad
4455aae8a53SK.Prasad /*
4465aae8a53SK.Prasad * Return early after invoking user-callback function without restoring
4475aae8a53SK.Prasad * DABR if the breakpoint is from ptrace which always operates in
4485aae8a53SK.Prasad * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
4495aae8a53SK.Prasad * generated in do_dabr().
4505aae8a53SK.Prasad */
45174c68810SRavi Bangoria if (ptrace_bp) {
45274c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
453bd29813aSBenjamin Gray if (!hit[i] || !is_ptrace_bp(bp[i]))
45474c68810SRavi Bangoria continue;
45574c68810SRavi Bangoria perf_bp_event(bp[i], regs);
456668a6ec6SBenjamin Gray bp[i] = NULL;
45774c68810SRavi Bangoria }
4585aae8a53SK.Prasad rc = NOTIFY_DONE;
45974c68810SRavi Bangoria goto reset;
4605aae8a53SK.Prasad }
4615aae8a53SK.Prasad
46274c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx)) {
463f6780ce6SRavi Bangoria if (is_larx_stcx_instr(type)) {
46474c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
46574c68810SRavi Bangoria if (!hit[i])
46674c68810SRavi Bangoria continue;
467668a6ec6SBenjamin Gray larx_stcx_err(bp[i]);
468668a6ec6SBenjamin Gray bp[i] = NULL;
46974c68810SRavi Bangoria }
47074c68810SRavi Bangoria goto reset;
47174c68810SRavi Bangoria }
47274c68810SRavi Bangoria
473668a6ec6SBenjamin Gray if (!stepping_handler(regs, bp, hit, instr))
47474c68810SRavi Bangoria goto reset;
475e08658a6SRavi Bangoria }
4765aae8a53SK.Prasad
4775aae8a53SK.Prasad /*
4785aae8a53SK.Prasad * As a policy, the callback is invoked in a 'trigger-after-execute'
4795aae8a53SK.Prasad * fashion
4805aae8a53SK.Prasad */
48174c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
48274c68810SRavi Bangoria if (!hit[i])
48374c68810SRavi Bangoria continue;
484668a6ec6SBenjamin Gray if (!(counter_arch_bp(bp[i])->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
48574c68810SRavi Bangoria perf_bp_event(bp[i], regs);
48674c68810SRavi Bangoria }
4875aae8a53SK.Prasad
48874c68810SRavi Bangoria reset:
48974c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
490668a6ec6SBenjamin Gray if (!bp[i])
49174c68810SRavi Bangoria continue;
492668a6ec6SBenjamin Gray __set_breakpoint(i, counter_arch_bp(bp[i]));
49374c68810SRavi Bangoria }
49474c68810SRavi Bangoria
4955aae8a53SK.Prasad out:
4965aae8a53SK.Prasad rcu_read_unlock();
4975aae8a53SK.Prasad return rc;
4985aae8a53SK.Prasad }
49903465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler);
5005aae8a53SK.Prasad
5015aae8a53SK.Prasad /*
5025aae8a53SK.Prasad * Handle single-step exceptions following a DABR hit.
503*27646b2eSBenjamin Gray *
504*27646b2eSBenjamin Gray * Called in atomic context.
5055aae8a53SK.Prasad */
single_step_dabr_instruction(struct die_args * args)50603465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args)
5075aae8a53SK.Prasad {
5085aae8a53SK.Prasad struct pt_regs *regs = args->regs;
50974c68810SRavi Bangoria bool found = false;
5105aae8a53SK.Prasad
5115aae8a53SK.Prasad /*
5125aae8a53SK.Prasad * Check if we are single-stepping as a result of a
5135aae8a53SK.Prasad * previous HW Breakpoint exception
5145aae8a53SK.Prasad */
5151e60f356SBenjamin Gray for (int i = 0; i < nr_wp_slots(); i++) {
5161e60f356SBenjamin Gray struct perf_event *bp;
5171e60f356SBenjamin Gray struct arch_hw_breakpoint *info;
5181e60f356SBenjamin Gray
5191e60f356SBenjamin Gray bp = __this_cpu_read(bp_per_reg[i]);
5205aae8a53SK.Prasad
52174c68810SRavi Bangoria if (!bp)
52274c68810SRavi Bangoria continue;
52374c68810SRavi Bangoria
5243f4693eeSMichael Neuling info = counter_arch_bp(bp);
5255aae8a53SK.Prasad
5261e60f356SBenjamin Gray if (!info->perf_single_step)
5271e60f356SBenjamin Gray continue;
5281e60f356SBenjamin Gray
5291e60f356SBenjamin Gray found = true;
5301e60f356SBenjamin Gray
5315aae8a53SK.Prasad /*
53274c68810SRavi Bangoria * We shall invoke the user-defined callback function in the
53374c68810SRavi Bangoria * single stepping handler to confirm to 'trigger-after-execute'
53474c68810SRavi Bangoria * semantics
5355aae8a53SK.Prasad */
5369422de3eSMichael Neuling if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
5375aae8a53SK.Prasad perf_bp_event(bp, regs);
5381e60f356SBenjamin Gray
5391e60f356SBenjamin Gray info->perf_single_step = false;
5401e60f356SBenjamin Gray __set_breakpoint(i, counter_arch_bp(bp));
54174c68810SRavi Bangoria }
54276b0f133SPaul Mackerras
54376b0f133SPaul Mackerras /*
54476b0f133SPaul Mackerras * If the process was being single-stepped by ptrace, let the
54576b0f133SPaul Mackerras * other single-step actions occur (e.g. generate SIGTRAP).
54676b0f133SPaul Mackerras */
5475a2d8b9cSBenjamin Gray if (!found || test_thread_flag(TIF_SINGLESTEP))
54876b0f133SPaul Mackerras return NOTIFY_DONE;
54976b0f133SPaul Mackerras
5505aae8a53SK.Prasad return NOTIFY_STOP;
5515aae8a53SK.Prasad }
55203465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction);
5535aae8a53SK.Prasad
5545aae8a53SK.Prasad /*
5555aae8a53SK.Prasad * Handle debug exception notifications.
556*27646b2eSBenjamin Gray *
557*27646b2eSBenjamin Gray * Called in atomic context.
5585aae8a53SK.Prasad */
hw_breakpoint_exceptions_notify(struct notifier_block * unused,unsigned long val,void * data)55903465f89SNicholas Piggin int hw_breakpoint_exceptions_notify(
5605aae8a53SK.Prasad struct notifier_block *unused, unsigned long val, void *data)
5615aae8a53SK.Prasad {
5625aae8a53SK.Prasad int ret = NOTIFY_DONE;
5635aae8a53SK.Prasad
5645aae8a53SK.Prasad switch (val) {
5655aae8a53SK.Prasad case DIE_DABR_MATCH:
5665aae8a53SK.Prasad ret = hw_breakpoint_handler(data);
5675aae8a53SK.Prasad break;
5685aae8a53SK.Prasad case DIE_SSTEP:
5695aae8a53SK.Prasad ret = single_step_dabr_instruction(data);
5705aae8a53SK.Prasad break;
5715aae8a53SK.Prasad }
5725aae8a53SK.Prasad
5735aae8a53SK.Prasad return ret;
5745aae8a53SK.Prasad }
57503465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
5765aae8a53SK.Prasad
5775aae8a53SK.Prasad /*
5785aae8a53SK.Prasad * Release the user breakpoints used by ptrace
5795aae8a53SK.Prasad */
flush_ptrace_hw_breakpoint(struct task_struct * tsk)5805aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
5815aae8a53SK.Prasad {
5826b424efaSRavi Bangoria int i;
5835aae8a53SK.Prasad struct thread_struct *t = &tsk->thread;
5845aae8a53SK.Prasad
5856b424efaSRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) {
5866b424efaSRavi Bangoria unregister_hw_breakpoint(t->ptrace_bps[i]);
5876b424efaSRavi Bangoria t->ptrace_bps[i] = NULL;
5886b424efaSRavi Bangoria }
5895aae8a53SK.Prasad }
5905aae8a53SK.Prasad
hw_breakpoint_pmu_read(struct perf_event * bp)5915aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp)
5925aae8a53SK.Prasad {
5935aae8a53SK.Prasad /* TODO */
5945aae8a53SK.Prasad }
595ccbed90bSChristophe Leroy
ptrace_triggered(struct perf_event * bp,struct perf_sample_data * data,struct pt_regs * regs)596ccbed90bSChristophe Leroy void ptrace_triggered(struct perf_event *bp,
597ccbed90bSChristophe Leroy struct perf_sample_data *data, struct pt_regs *regs)
598ccbed90bSChristophe Leroy {
599ccbed90bSChristophe Leroy struct perf_event_attr attr;
600ccbed90bSChristophe Leroy
601ccbed90bSChristophe Leroy /*
602ccbed90bSChristophe Leroy * Disable the breakpoint request here since ptrace has defined a
603ccbed90bSChristophe Leroy * one-shot behaviour for breakpoint exceptions in PPC64.
604ccbed90bSChristophe Leroy * The SIGTRAP signal is generated automatically for us in do_dabr().
605ccbed90bSChristophe Leroy * We don't have to do anything about that here
606ccbed90bSChristophe Leroy */
607ccbed90bSChristophe Leroy attr = bp->attr;
608ccbed90bSChristophe Leroy attr.disabled = true;
609ccbed90bSChristophe Leroy modify_user_hw_breakpoint(bp, &attr);
610ccbed90bSChristophe Leroy }
611