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 233*cc879ab3SBenjamin Gray preempt_disable(); 234*cc879ab3SBenjamin 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 } 241*cc879ab3SBenjamin 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 } 250*cc879ab3SBenjamin Gray 251*cc879ab3SBenjamin Gray out: 252*cc879ab3SBenjamin Gray preempt_enable(); 25306532a67SK.Prasad } 25406532a67SK.Prasad 255f6780ce6SRavi Bangoria static bool is_larx_stcx_instr(int type) 256f6780ce6SRavi Bangoria { 257f6780ce6SRavi Bangoria return type == LARX || type == STCX; 25874c68810SRavi Bangoria } 25974c68810SRavi Bangoria 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 */ 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 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 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 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 37103465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args) 3725aae8a53SK.Prasad { 37374c68810SRavi Bangoria bool err = false; 3745aae8a53SK.Prasad int rc = NOTIFY_STOP; 37574c68810SRavi Bangoria struct perf_event *bp[HBP_NUM_MAX] = { NULL }; 3765aae8a53SK.Prasad struct pt_regs *regs = args->regs; 37774c68810SRavi Bangoria int i; 37874c68810SRavi Bangoria int hit[HBP_NUM_MAX] = {0}; 37974c68810SRavi Bangoria int nr_hit = 0; 38074c68810SRavi Bangoria bool ptrace_bp = false; 381c545b9f0SChristophe Leroy ppc_inst_t instr = ppc_inst(0); 38274c68810SRavi Bangoria int type = 0; 38374c68810SRavi Bangoria int size = 0; 384f6680275SRussell Currey unsigned long ea = 0; 3855aae8a53SK.Prasad 3865aae8a53SK.Prasad /* Disable breakpoints during exception handling */ 3879422de3eSMichael Neuling hw_breakpoint_disable(); 388574cb248SPaul Mackerras 3895aae8a53SK.Prasad /* 3905aae8a53SK.Prasad * The counter may be concurrently released but that can only 3915aae8a53SK.Prasad * occur from a call_rcu() path. We can then safely fetch 3925aae8a53SK.Prasad * the breakpoint, use its callback, touch its counter 3935aae8a53SK.Prasad * while we are in an rcu_read_lock() path. 3945aae8a53SK.Prasad */ 3955aae8a53SK.Prasad rcu_read_lock(); 3965aae8a53SK.Prasad 39774c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx)) 398edc8dd99SRavi Bangoria wp_get_instr_detail(regs, &instr, &type, &size, &ea); 39974c68810SRavi Bangoria 40074c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 401668a6ec6SBenjamin Gray struct arch_hw_breakpoint *info; 402668a6ec6SBenjamin Gray 40374c68810SRavi Bangoria bp[i] = __this_cpu_read(bp_per_reg[i]); 40474c68810SRavi Bangoria if (!bp[i]) 40574c68810SRavi Bangoria continue; 40674c68810SRavi Bangoria 407668a6ec6SBenjamin Gray info = counter_arch_bp(bp[i]); 408668a6ec6SBenjamin Gray info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ; 40974c68810SRavi Bangoria 410668a6ec6SBenjamin Gray if (wp_check_constraints(regs, instr, ea, type, size, info)) { 41174c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx) && 41274c68810SRavi Bangoria ppc_inst_equal(instr, ppc_inst(0))) { 413668a6ec6SBenjamin Gray handler_error(bp[i]); 414668a6ec6SBenjamin Gray bp[i] = NULL; 41574c68810SRavi Bangoria err = 1; 41674c68810SRavi Bangoria continue; 41774c68810SRavi Bangoria } 41874c68810SRavi Bangoria 41974c68810SRavi Bangoria if (is_ptrace_bp(bp[i])) 42074c68810SRavi Bangoria ptrace_bp = true; 42174c68810SRavi Bangoria hit[i] = 1; 42274c68810SRavi Bangoria nr_hit++; 42374c68810SRavi Bangoria } 42474c68810SRavi Bangoria } 42574c68810SRavi Bangoria 42674c68810SRavi Bangoria if (err) 42774c68810SRavi Bangoria goto reset; 42874c68810SRavi Bangoria 42974c68810SRavi Bangoria if (!nr_hit) { 4303d2ffcddSRavi Bangoria /* Workaround for Power10 DD1 */ 4313d2ffcddSRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx) && mfspr(SPRN_PVR) == 0x800100 && 4323d2ffcddSRavi Bangoria is_octword_vsx_instr(type, size)) { 433668a6ec6SBenjamin Gray handle_p10dd1_spurious_exception(bp, hit, ea); 4343d2ffcddSRavi Bangoria } else { 435c21a493aSRavi Bangoria rc = NOTIFY_DONE; 4365aae8a53SK.Prasad goto out; 437c21a493aSRavi Bangoria } 4383d2ffcddSRavi Bangoria } 4395aae8a53SK.Prasad 4405aae8a53SK.Prasad /* 4415aae8a53SK.Prasad * Return early after invoking user-callback function without restoring 4425aae8a53SK.Prasad * DABR if the breakpoint is from ptrace which always operates in 4435aae8a53SK.Prasad * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal 4445aae8a53SK.Prasad * generated in do_dabr(). 4455aae8a53SK.Prasad */ 44674c68810SRavi Bangoria if (ptrace_bp) { 44774c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 448bd29813aSBenjamin Gray if (!hit[i] || !is_ptrace_bp(bp[i])) 44974c68810SRavi Bangoria continue; 45074c68810SRavi Bangoria perf_bp_event(bp[i], regs); 451668a6ec6SBenjamin Gray bp[i] = NULL; 45274c68810SRavi Bangoria } 4535aae8a53SK.Prasad rc = NOTIFY_DONE; 45474c68810SRavi Bangoria goto reset; 4555aae8a53SK.Prasad } 4565aae8a53SK.Prasad 45774c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx)) { 458f6780ce6SRavi Bangoria if (is_larx_stcx_instr(type)) { 45974c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 46074c68810SRavi Bangoria if (!hit[i]) 46174c68810SRavi Bangoria continue; 462668a6ec6SBenjamin Gray larx_stcx_err(bp[i]); 463668a6ec6SBenjamin Gray bp[i] = NULL; 46474c68810SRavi Bangoria } 46574c68810SRavi Bangoria goto reset; 46674c68810SRavi Bangoria } 46774c68810SRavi Bangoria 468668a6ec6SBenjamin Gray if (!stepping_handler(regs, bp, hit, instr)) 46974c68810SRavi Bangoria goto reset; 470e08658a6SRavi Bangoria } 4715aae8a53SK.Prasad 4725aae8a53SK.Prasad /* 4735aae8a53SK.Prasad * As a policy, the callback is invoked in a 'trigger-after-execute' 4745aae8a53SK.Prasad * fashion 4755aae8a53SK.Prasad */ 47674c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 47774c68810SRavi Bangoria if (!hit[i]) 47874c68810SRavi Bangoria continue; 479668a6ec6SBenjamin Gray if (!(counter_arch_bp(bp[i])->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) 48074c68810SRavi Bangoria perf_bp_event(bp[i], regs); 48174c68810SRavi Bangoria } 4825aae8a53SK.Prasad 48374c68810SRavi Bangoria reset: 48474c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 485668a6ec6SBenjamin Gray if (!bp[i]) 48674c68810SRavi Bangoria continue; 487668a6ec6SBenjamin Gray __set_breakpoint(i, counter_arch_bp(bp[i])); 48874c68810SRavi Bangoria } 48974c68810SRavi Bangoria 4905aae8a53SK.Prasad out: 4915aae8a53SK.Prasad rcu_read_unlock(); 4925aae8a53SK.Prasad return rc; 4935aae8a53SK.Prasad } 49403465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler); 4955aae8a53SK.Prasad 4965aae8a53SK.Prasad /* 4975aae8a53SK.Prasad * Handle single-step exceptions following a DABR hit. 4985aae8a53SK.Prasad */ 49903465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args) 5005aae8a53SK.Prasad { 5015aae8a53SK.Prasad struct pt_regs *regs = args->regs; 50274c68810SRavi Bangoria bool found = false; 5035aae8a53SK.Prasad 5045aae8a53SK.Prasad /* 5055aae8a53SK.Prasad * Check if we are single-stepping as a result of a 5065aae8a53SK.Prasad * previous HW Breakpoint exception 5075aae8a53SK.Prasad */ 5081e60f356SBenjamin Gray for (int i = 0; i < nr_wp_slots(); i++) { 5091e60f356SBenjamin Gray struct perf_event *bp; 5101e60f356SBenjamin Gray struct arch_hw_breakpoint *info; 5111e60f356SBenjamin Gray 5121e60f356SBenjamin Gray bp = __this_cpu_read(bp_per_reg[i]); 5135aae8a53SK.Prasad 51474c68810SRavi Bangoria if (!bp) 51574c68810SRavi Bangoria continue; 51674c68810SRavi Bangoria 5173f4693eeSMichael Neuling info = counter_arch_bp(bp); 5185aae8a53SK.Prasad 5191e60f356SBenjamin Gray if (!info->perf_single_step) 5201e60f356SBenjamin Gray continue; 5211e60f356SBenjamin Gray 5221e60f356SBenjamin Gray found = true; 5231e60f356SBenjamin Gray 5245aae8a53SK.Prasad /* 52574c68810SRavi Bangoria * We shall invoke the user-defined callback function in the 52674c68810SRavi Bangoria * single stepping handler to confirm to 'trigger-after-execute' 52774c68810SRavi Bangoria * semantics 5285aae8a53SK.Prasad */ 5299422de3eSMichael Neuling if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) 5305aae8a53SK.Prasad perf_bp_event(bp, regs); 5311e60f356SBenjamin Gray 5321e60f356SBenjamin Gray info->perf_single_step = false; 5331e60f356SBenjamin Gray __set_breakpoint(i, counter_arch_bp(bp)); 53474c68810SRavi Bangoria } 53576b0f133SPaul Mackerras 53676b0f133SPaul Mackerras /* 53776b0f133SPaul Mackerras * If the process was being single-stepped by ptrace, let the 53876b0f133SPaul Mackerras * other single-step actions occur (e.g. generate SIGTRAP). 53976b0f133SPaul Mackerras */ 5405a2d8b9cSBenjamin Gray if (!found || test_thread_flag(TIF_SINGLESTEP)) 54176b0f133SPaul Mackerras return NOTIFY_DONE; 54276b0f133SPaul Mackerras 5435aae8a53SK.Prasad return NOTIFY_STOP; 5445aae8a53SK.Prasad } 54503465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction); 5465aae8a53SK.Prasad 5475aae8a53SK.Prasad /* 5485aae8a53SK.Prasad * Handle debug exception notifications. 5495aae8a53SK.Prasad */ 55003465f89SNicholas Piggin int hw_breakpoint_exceptions_notify( 5515aae8a53SK.Prasad struct notifier_block *unused, unsigned long val, void *data) 5525aae8a53SK.Prasad { 5535aae8a53SK.Prasad int ret = NOTIFY_DONE; 5545aae8a53SK.Prasad 5555aae8a53SK.Prasad switch (val) { 5565aae8a53SK.Prasad case DIE_DABR_MATCH: 5575aae8a53SK.Prasad ret = hw_breakpoint_handler(data); 5585aae8a53SK.Prasad break; 5595aae8a53SK.Prasad case DIE_SSTEP: 5605aae8a53SK.Prasad ret = single_step_dabr_instruction(data); 5615aae8a53SK.Prasad break; 5625aae8a53SK.Prasad } 5635aae8a53SK.Prasad 5645aae8a53SK.Prasad return ret; 5655aae8a53SK.Prasad } 56603465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify); 5675aae8a53SK.Prasad 5685aae8a53SK.Prasad /* 5695aae8a53SK.Prasad * Release the user breakpoints used by ptrace 5705aae8a53SK.Prasad */ 5715aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk) 5725aae8a53SK.Prasad { 5736b424efaSRavi Bangoria int i; 5745aae8a53SK.Prasad struct thread_struct *t = &tsk->thread; 5755aae8a53SK.Prasad 5766b424efaSRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 5776b424efaSRavi Bangoria unregister_hw_breakpoint(t->ptrace_bps[i]); 5786b424efaSRavi Bangoria t->ptrace_bps[i] = NULL; 5796b424efaSRavi Bangoria } 5805aae8a53SK.Prasad } 5815aae8a53SK.Prasad 5825aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp) 5835aae8a53SK.Prasad { 5845aae8a53SK.Prasad /* TODO */ 5855aae8a53SK.Prasad } 586ccbed90bSChristophe Leroy 587ccbed90bSChristophe Leroy void ptrace_triggered(struct perf_event *bp, 588ccbed90bSChristophe Leroy struct perf_sample_data *data, struct pt_regs *regs) 589ccbed90bSChristophe Leroy { 590ccbed90bSChristophe Leroy struct perf_event_attr attr; 591ccbed90bSChristophe Leroy 592ccbed90bSChristophe Leroy /* 593ccbed90bSChristophe Leroy * Disable the breakpoint request here since ptrace has defined a 594ccbed90bSChristophe Leroy * one-shot behaviour for breakpoint exceptions in PPC64. 595ccbed90bSChristophe Leroy * The SIGTRAP signal is generated automatically for us in do_dabr(). 596ccbed90bSChristophe Leroy * We don't have to do anything about that here 597ccbed90bSChristophe Leroy */ 598ccbed90bSChristophe Leroy attr = bp->attr; 599ccbed90bSChristophe Leroy attr.disabled = true; 600ccbed90bSChristophe Leroy modify_user_hw_breakpoint(bp, &attr); 601ccbed90bSChristophe Leroy } 602