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 */ 77*1e60f356SBenjamin 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 11729da4f91SRavi Bangoria struct breakpoint { 11829da4f91SRavi Bangoria struct list_head list; 11929da4f91SRavi Bangoria struct perf_event *bp; 12029da4f91SRavi Bangoria bool ptrace_bp; 12129da4f91SRavi Bangoria }; 12229da4f91SRavi Bangoria 123f95e5a3dSMarco Elver /* 124f95e5a3dSMarco Elver * While kernel/events/hw_breakpoint.c does its own synchronization, we cannot 125f95e5a3dSMarco Elver * rely on it safely synchronizing internals here; however, we can rely on it 126f95e5a3dSMarco Elver * not requesting more breakpoints than available. 127f95e5a3dSMarco Elver */ 128f95e5a3dSMarco Elver static DEFINE_SPINLOCK(cpu_bps_lock); 12929da4f91SRavi Bangoria static DEFINE_PER_CPU(struct breakpoint *, cpu_bps[HBP_NUM_MAX]); 130f95e5a3dSMarco Elver static DEFINE_SPINLOCK(task_bps_lock); 13129da4f91SRavi Bangoria static LIST_HEAD(task_bps); 13229da4f91SRavi Bangoria 13329da4f91SRavi Bangoria static struct breakpoint *alloc_breakpoint(struct perf_event *bp) 13429da4f91SRavi Bangoria { 13529da4f91SRavi Bangoria struct breakpoint *tmp; 13629da4f91SRavi Bangoria 13729da4f91SRavi Bangoria tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 13829da4f91SRavi Bangoria if (!tmp) 13929da4f91SRavi Bangoria return ERR_PTR(-ENOMEM); 14029da4f91SRavi Bangoria tmp->bp = bp; 14129da4f91SRavi Bangoria tmp->ptrace_bp = is_ptrace_bp(bp); 14229da4f91SRavi Bangoria return tmp; 14329da4f91SRavi Bangoria } 14429da4f91SRavi Bangoria 14529da4f91SRavi Bangoria static bool bp_addr_range_overlap(struct perf_event *bp1, struct perf_event *bp2) 14629da4f91SRavi Bangoria { 14729da4f91SRavi Bangoria __u64 bp1_saddr, bp1_eaddr, bp2_saddr, bp2_eaddr; 14829da4f91SRavi Bangoria 14929da4f91SRavi Bangoria bp1_saddr = ALIGN_DOWN(bp1->attr.bp_addr, HW_BREAKPOINT_SIZE); 15029da4f91SRavi Bangoria bp1_eaddr = ALIGN(bp1->attr.bp_addr + bp1->attr.bp_len, HW_BREAKPOINT_SIZE); 15129da4f91SRavi Bangoria bp2_saddr = ALIGN_DOWN(bp2->attr.bp_addr, HW_BREAKPOINT_SIZE); 15229da4f91SRavi Bangoria bp2_eaddr = ALIGN(bp2->attr.bp_addr + bp2->attr.bp_len, HW_BREAKPOINT_SIZE); 15329da4f91SRavi Bangoria 15429da4f91SRavi Bangoria return (bp1_saddr < bp2_eaddr && bp1_eaddr > bp2_saddr); 15529da4f91SRavi Bangoria } 15629da4f91SRavi Bangoria 15729da4f91SRavi Bangoria static bool alternate_infra_bp(struct breakpoint *b, struct perf_event *bp) 15829da4f91SRavi Bangoria { 15929da4f91SRavi Bangoria return is_ptrace_bp(bp) ? !b->ptrace_bp : b->ptrace_bp; 16029da4f91SRavi Bangoria } 16129da4f91SRavi Bangoria 16229da4f91SRavi Bangoria static bool can_co_exist(struct breakpoint *b, struct perf_event *bp) 16329da4f91SRavi Bangoria { 16429da4f91SRavi Bangoria return !(alternate_infra_bp(b, bp) && bp_addr_range_overlap(b->bp, bp)); 16529da4f91SRavi Bangoria } 16629da4f91SRavi Bangoria 16729da4f91SRavi Bangoria static int task_bps_add(struct perf_event *bp) 16829da4f91SRavi Bangoria { 16929da4f91SRavi Bangoria struct breakpoint *tmp; 17029da4f91SRavi Bangoria 17129da4f91SRavi Bangoria tmp = alloc_breakpoint(bp); 17229da4f91SRavi Bangoria if (IS_ERR(tmp)) 17329da4f91SRavi Bangoria return PTR_ERR(tmp); 17429da4f91SRavi Bangoria 175f95e5a3dSMarco Elver spin_lock(&task_bps_lock); 17629da4f91SRavi Bangoria list_add(&tmp->list, &task_bps); 177f95e5a3dSMarco Elver spin_unlock(&task_bps_lock); 17829da4f91SRavi Bangoria return 0; 17929da4f91SRavi Bangoria } 18029da4f91SRavi Bangoria 18129da4f91SRavi Bangoria static void task_bps_remove(struct perf_event *bp) 18229da4f91SRavi Bangoria { 18329da4f91SRavi Bangoria struct list_head *pos, *q; 18429da4f91SRavi Bangoria 185f95e5a3dSMarco Elver spin_lock(&task_bps_lock); 18629da4f91SRavi Bangoria list_for_each_safe(pos, q, &task_bps) { 18729da4f91SRavi Bangoria struct breakpoint *tmp = list_entry(pos, struct breakpoint, list); 18829da4f91SRavi Bangoria 18929da4f91SRavi Bangoria if (tmp->bp == bp) { 19029da4f91SRavi Bangoria list_del(&tmp->list); 19129da4f91SRavi Bangoria kfree(tmp); 19229da4f91SRavi Bangoria break; 19329da4f91SRavi Bangoria } 19429da4f91SRavi Bangoria } 195f95e5a3dSMarco Elver spin_unlock(&task_bps_lock); 19629da4f91SRavi Bangoria } 19729da4f91SRavi Bangoria 19829da4f91SRavi Bangoria /* 19929da4f91SRavi Bangoria * If any task has breakpoint from alternate infrastructure, 20029da4f91SRavi Bangoria * return true. Otherwise return false. 20129da4f91SRavi Bangoria */ 20229da4f91SRavi Bangoria static bool all_task_bps_check(struct perf_event *bp) 20329da4f91SRavi Bangoria { 20429da4f91SRavi Bangoria struct breakpoint *tmp; 205f95e5a3dSMarco Elver bool ret = false; 20629da4f91SRavi Bangoria 207f95e5a3dSMarco Elver spin_lock(&task_bps_lock); 20829da4f91SRavi Bangoria list_for_each_entry(tmp, &task_bps, list) { 209f95e5a3dSMarco Elver if (!can_co_exist(tmp, bp)) { 210f95e5a3dSMarco Elver ret = true; 211f95e5a3dSMarco Elver break; 21229da4f91SRavi Bangoria } 213f95e5a3dSMarco Elver } 214f95e5a3dSMarco Elver spin_unlock(&task_bps_lock); 215f95e5a3dSMarco Elver return ret; 21629da4f91SRavi Bangoria } 21729da4f91SRavi Bangoria 21829da4f91SRavi Bangoria /* 21929da4f91SRavi Bangoria * If same task has breakpoint from alternate infrastructure, 22029da4f91SRavi Bangoria * return true. Otherwise return false. 22129da4f91SRavi Bangoria */ 22229da4f91SRavi Bangoria static bool same_task_bps_check(struct perf_event *bp) 22329da4f91SRavi Bangoria { 22429da4f91SRavi Bangoria struct breakpoint *tmp; 225f95e5a3dSMarco Elver bool ret = false; 22629da4f91SRavi Bangoria 227f95e5a3dSMarco Elver spin_lock(&task_bps_lock); 22829da4f91SRavi Bangoria list_for_each_entry(tmp, &task_bps, list) { 22929da4f91SRavi Bangoria if (tmp->bp->hw.target == bp->hw.target && 230f95e5a3dSMarco Elver !can_co_exist(tmp, bp)) { 231f95e5a3dSMarco Elver ret = true; 232f95e5a3dSMarco Elver break; 23329da4f91SRavi Bangoria } 234f95e5a3dSMarco Elver } 235f95e5a3dSMarco Elver spin_unlock(&task_bps_lock); 236f95e5a3dSMarco Elver return ret; 23729da4f91SRavi Bangoria } 23829da4f91SRavi Bangoria 23929da4f91SRavi Bangoria static int cpu_bps_add(struct perf_event *bp) 24029da4f91SRavi Bangoria { 24129da4f91SRavi Bangoria struct breakpoint **cpu_bp; 24229da4f91SRavi Bangoria struct breakpoint *tmp; 24329da4f91SRavi Bangoria int i = 0; 24429da4f91SRavi Bangoria 24529da4f91SRavi Bangoria tmp = alloc_breakpoint(bp); 24629da4f91SRavi Bangoria if (IS_ERR(tmp)) 24729da4f91SRavi Bangoria return PTR_ERR(tmp); 24829da4f91SRavi Bangoria 249f95e5a3dSMarco Elver spin_lock(&cpu_bps_lock); 25029da4f91SRavi Bangoria cpu_bp = per_cpu_ptr(cpu_bps, bp->cpu); 25129da4f91SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 25229da4f91SRavi Bangoria if (!cpu_bp[i]) { 25329da4f91SRavi Bangoria cpu_bp[i] = tmp; 25429da4f91SRavi Bangoria break; 25529da4f91SRavi Bangoria } 25629da4f91SRavi Bangoria } 257f95e5a3dSMarco Elver spin_unlock(&cpu_bps_lock); 25829da4f91SRavi Bangoria return 0; 25929da4f91SRavi Bangoria } 26029da4f91SRavi Bangoria 26129da4f91SRavi Bangoria static void cpu_bps_remove(struct perf_event *bp) 26229da4f91SRavi Bangoria { 26329da4f91SRavi Bangoria struct breakpoint **cpu_bp; 26429da4f91SRavi Bangoria int i = 0; 26529da4f91SRavi Bangoria 266f95e5a3dSMarco Elver spin_lock(&cpu_bps_lock); 26729da4f91SRavi Bangoria cpu_bp = per_cpu_ptr(cpu_bps, bp->cpu); 26829da4f91SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 26929da4f91SRavi Bangoria if (!cpu_bp[i]) 27029da4f91SRavi Bangoria continue; 27129da4f91SRavi Bangoria 27229da4f91SRavi Bangoria if (cpu_bp[i]->bp == bp) { 27329da4f91SRavi Bangoria kfree(cpu_bp[i]); 27429da4f91SRavi Bangoria cpu_bp[i] = NULL; 27529da4f91SRavi Bangoria break; 27629da4f91SRavi Bangoria } 27729da4f91SRavi Bangoria } 278f95e5a3dSMarco Elver spin_unlock(&cpu_bps_lock); 27929da4f91SRavi Bangoria } 28029da4f91SRavi Bangoria 28129da4f91SRavi Bangoria static bool cpu_bps_check(int cpu, struct perf_event *bp) 28229da4f91SRavi Bangoria { 28329da4f91SRavi Bangoria struct breakpoint **cpu_bp; 284f95e5a3dSMarco Elver bool ret = false; 28529da4f91SRavi Bangoria int i; 28629da4f91SRavi Bangoria 287f95e5a3dSMarco Elver spin_lock(&cpu_bps_lock); 28829da4f91SRavi Bangoria cpu_bp = per_cpu_ptr(cpu_bps, cpu); 28929da4f91SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 290f95e5a3dSMarco Elver if (cpu_bp[i] && !can_co_exist(cpu_bp[i], bp)) { 291f95e5a3dSMarco Elver ret = true; 292f95e5a3dSMarco Elver break; 29329da4f91SRavi Bangoria } 294f95e5a3dSMarco Elver } 295f95e5a3dSMarco Elver spin_unlock(&cpu_bps_lock); 296f95e5a3dSMarco Elver return ret; 29729da4f91SRavi Bangoria } 29829da4f91SRavi Bangoria 29929da4f91SRavi Bangoria static bool all_cpu_bps_check(struct perf_event *bp) 30029da4f91SRavi Bangoria { 30129da4f91SRavi Bangoria int cpu; 30229da4f91SRavi Bangoria 30329da4f91SRavi Bangoria for_each_online_cpu(cpu) { 30429da4f91SRavi Bangoria if (cpu_bps_check(cpu, bp)) 30529da4f91SRavi Bangoria return true; 30629da4f91SRavi Bangoria } 30729da4f91SRavi Bangoria return false; 30829da4f91SRavi Bangoria } 30929da4f91SRavi Bangoria 31029da4f91SRavi Bangoria int arch_reserve_bp_slot(struct perf_event *bp) 31129da4f91SRavi Bangoria { 31229da4f91SRavi Bangoria int ret; 31329da4f91SRavi Bangoria 31429da4f91SRavi Bangoria /* ptrace breakpoint */ 31529da4f91SRavi Bangoria if (is_ptrace_bp(bp)) { 31629da4f91SRavi Bangoria if (all_cpu_bps_check(bp)) 31729da4f91SRavi Bangoria return -ENOSPC; 31829da4f91SRavi Bangoria 31929da4f91SRavi Bangoria if (same_task_bps_check(bp)) 32029da4f91SRavi Bangoria return -ENOSPC; 32129da4f91SRavi Bangoria 32229da4f91SRavi Bangoria return task_bps_add(bp); 32329da4f91SRavi Bangoria } 32429da4f91SRavi Bangoria 32529da4f91SRavi Bangoria /* perf breakpoint */ 32629da4f91SRavi Bangoria if (is_kernel_addr(bp->attr.bp_addr)) 32729da4f91SRavi Bangoria return 0; 32829da4f91SRavi Bangoria 32929da4f91SRavi Bangoria if (bp->hw.target && bp->cpu == -1) { 33029da4f91SRavi Bangoria if (same_task_bps_check(bp)) 33129da4f91SRavi Bangoria return -ENOSPC; 33229da4f91SRavi Bangoria 33329da4f91SRavi Bangoria return task_bps_add(bp); 33429da4f91SRavi Bangoria } else if (!bp->hw.target && bp->cpu != -1) { 33529da4f91SRavi Bangoria if (all_task_bps_check(bp)) 33629da4f91SRavi Bangoria return -ENOSPC; 33729da4f91SRavi Bangoria 33829da4f91SRavi Bangoria return cpu_bps_add(bp); 33929da4f91SRavi Bangoria } 34029da4f91SRavi Bangoria 34129da4f91SRavi Bangoria if (same_task_bps_check(bp)) 34229da4f91SRavi Bangoria return -ENOSPC; 34329da4f91SRavi Bangoria 34429da4f91SRavi Bangoria ret = cpu_bps_add(bp); 34529da4f91SRavi Bangoria if (ret) 34629da4f91SRavi Bangoria return ret; 34729da4f91SRavi Bangoria ret = task_bps_add(bp); 34829da4f91SRavi Bangoria if (ret) 34929da4f91SRavi Bangoria cpu_bps_remove(bp); 35029da4f91SRavi Bangoria 35129da4f91SRavi Bangoria return ret; 35229da4f91SRavi Bangoria } 35329da4f91SRavi Bangoria 35429da4f91SRavi Bangoria void arch_release_bp_slot(struct perf_event *bp) 35529da4f91SRavi Bangoria { 35629da4f91SRavi Bangoria if (!is_kernel_addr(bp->attr.bp_addr)) { 35729da4f91SRavi Bangoria if (bp->hw.target) 35829da4f91SRavi Bangoria task_bps_remove(bp); 35929da4f91SRavi Bangoria if (bp->cpu != -1) 36029da4f91SRavi Bangoria cpu_bps_remove(bp); 36129da4f91SRavi Bangoria } 36229da4f91SRavi Bangoria } 36329da4f91SRavi Bangoria 3645aae8a53SK.Prasad /* 3655aae8a53SK.Prasad * Check for virtual address in kernel space. 3665aae8a53SK.Prasad */ 3678e983ff9SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw) 3685aae8a53SK.Prasad { 3698e983ff9SFrederic Weisbecker return is_kernel_addr(hw->address); 3705aae8a53SK.Prasad } 3715aae8a53SK.Prasad 3725aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type) 3735aae8a53SK.Prasad { 3749422de3eSMichael Neuling *gen_bp_type = 0; 3759422de3eSMichael Neuling if (type & HW_BRK_TYPE_READ) 3769422de3eSMichael Neuling *gen_bp_type |= HW_BREAKPOINT_R; 3779422de3eSMichael Neuling if (type & HW_BRK_TYPE_WRITE) 3789422de3eSMichael Neuling *gen_bp_type |= HW_BREAKPOINT_W; 3799422de3eSMichael Neuling if (*gen_bp_type == 0) 3805aae8a53SK.Prasad return -EINVAL; 3815aae8a53SK.Prasad return 0; 3825aae8a53SK.Prasad } 3835aae8a53SK.Prasad 3845aae8a53SK.Prasad /* 385b57aeab8SRavi Bangoria * Watchpoint match range is always doubleword(8 bytes) aligned on 386b57aeab8SRavi Bangoria * powerpc. If the given range is crossing doubleword boundary, we 387b57aeab8SRavi Bangoria * need to increase the length such that next doubleword also get 388b57aeab8SRavi Bangoria * covered. Ex, 389b57aeab8SRavi Bangoria * 390b57aeab8SRavi Bangoria * address len = 6 bytes 391b57aeab8SRavi Bangoria * |=========. 392b57aeab8SRavi Bangoria * |------------v--|------v--------| 393b57aeab8SRavi Bangoria * | | | | | | | | | | | | | | | | | 394b57aeab8SRavi Bangoria * |---------------|---------------| 395b57aeab8SRavi Bangoria * <---8 bytes---> 396b57aeab8SRavi Bangoria * 397b57aeab8SRavi Bangoria * In this case, we should configure hw as: 398e68ef121SRavi Bangoria * start_addr = address & ~(HW_BREAKPOINT_SIZE - 1) 399b57aeab8SRavi Bangoria * len = 16 bytes 400b57aeab8SRavi Bangoria * 401e68ef121SRavi Bangoria * @start_addr is inclusive but @end_addr is exclusive. 402b57aeab8SRavi Bangoria */ 403b57aeab8SRavi Bangoria static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw) 404b57aeab8SRavi Bangoria { 405b57aeab8SRavi Bangoria u16 max_len = DABR_MAX_LEN; 406b57aeab8SRavi Bangoria u16 hw_len; 407b57aeab8SRavi Bangoria unsigned long start_addr, end_addr; 408b57aeab8SRavi Bangoria 409e68ef121SRavi Bangoria start_addr = ALIGN_DOWN(hw->address, HW_BREAKPOINT_SIZE); 410e68ef121SRavi Bangoria end_addr = ALIGN(hw->address + hw->len, HW_BREAKPOINT_SIZE); 411e68ef121SRavi Bangoria hw_len = end_addr - start_addr; 412b57aeab8SRavi Bangoria 413b57aeab8SRavi Bangoria if (dawr_enabled()) { 414b57aeab8SRavi Bangoria max_len = DAWR_MAX_LEN; 4153f31e49dSRavi Bangoria /* DAWR region can't cross 512 bytes boundary on p10 predecessors */ 4163f31e49dSRavi Bangoria if (!cpu_has_feature(CPU_FTR_ARCH_31) && 4173f31e49dSRavi Bangoria (ALIGN_DOWN(start_addr, SZ_512) != ALIGN_DOWN(end_addr - 1, SZ_512))) 418b57aeab8SRavi Bangoria return -EINVAL; 41939413ae0SChristophe Leroy } else if (IS_ENABLED(CONFIG_PPC_8xx)) { 42039413ae0SChristophe Leroy /* 8xx can setup a range without limitation */ 42139413ae0SChristophe Leroy max_len = U16_MAX; 422b57aeab8SRavi Bangoria } 423b57aeab8SRavi Bangoria 424b57aeab8SRavi Bangoria if (hw_len > max_len) 425b57aeab8SRavi Bangoria return -EINVAL; 426b57aeab8SRavi Bangoria 427b57aeab8SRavi Bangoria hw->hw_len = hw_len; 428b57aeab8SRavi Bangoria return 0; 429b57aeab8SRavi Bangoria } 430b57aeab8SRavi Bangoria 431b57aeab8SRavi Bangoria /* 4325aae8a53SK.Prasad * Validate the arch-specific HW Breakpoint register settings 4335aae8a53SK.Prasad */ 4345d5176baSFrederic Weisbecker int hw_breakpoint_arch_parse(struct perf_event *bp, 4355d5176baSFrederic Weisbecker const struct perf_event_attr *attr, 4365d5176baSFrederic Weisbecker struct arch_hw_breakpoint *hw) 4375aae8a53SK.Prasad { 438b57aeab8SRavi Bangoria int ret = -EINVAL; 4395aae8a53SK.Prasad 440b57aeab8SRavi Bangoria if (!bp || !attr->bp_len) 4415aae8a53SK.Prasad return ret; 4425aae8a53SK.Prasad 4435d5176baSFrederic Weisbecker hw->type = HW_BRK_TYPE_TRANSLATE; 4445d5176baSFrederic Weisbecker if (attr->bp_type & HW_BREAKPOINT_R) 4455d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_READ; 4465d5176baSFrederic Weisbecker if (attr->bp_type & HW_BREAKPOINT_W) 4475d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_WRITE; 4485d5176baSFrederic Weisbecker if (hw->type == HW_BRK_TYPE_TRANSLATE) 4499422de3eSMichael Neuling /* must set alteast read or write */ 4505aae8a53SK.Prasad return ret; 4515d5176baSFrederic Weisbecker if (!attr->exclude_user) 4525d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_USER; 4535d5176baSFrederic Weisbecker if (!attr->exclude_kernel) 4545d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_KERNEL; 4555d5176baSFrederic Weisbecker if (!attr->exclude_hv) 4565d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_HYP; 4575d5176baSFrederic Weisbecker hw->address = attr->bp_addr; 4585d5176baSFrederic Weisbecker hw->len = attr->bp_len; 4595aae8a53SK.Prasad 46085ce9a5dSMichael Neuling if (!ppc_breakpoint_available()) 46185ce9a5dSMichael Neuling return -ENODEV; 462b57aeab8SRavi Bangoria 463b57aeab8SRavi Bangoria return hw_breakpoint_validate_len(hw); 4645aae8a53SK.Prasad } 4655aae8a53SK.Prasad 4665aae8a53SK.Prasad /* 46706532a67SK.Prasad * Restores the breakpoint on the debug registers. 46806532a67SK.Prasad * Invoke this function if it is known that the execution context is 46906532a67SK.Prasad * about to change to cause loss of MSR_SE settings. 4708f8f1cd6SBenjamin Gray * 4718f8f1cd6SBenjamin Gray * The perf watchpoint will simply re-trigger once the thread is started again, 4728f8f1cd6SBenjamin Gray * and the watchpoint handler will set up MSR_SE and perf_single_step as 4738f8f1cd6SBenjamin Gray * needed. 47406532a67SK.Prasad */ 47506532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs) 47606532a67SK.Prasad { 47706532a67SK.Prasad struct arch_hw_breakpoint *info; 47874c68810SRavi Bangoria int i; 47906532a67SK.Prasad 48074c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 481*1e60f356SBenjamin Gray struct perf_event *bp = __this_cpu_read(bp_per_reg[i]); 482*1e60f356SBenjamin Gray 483*1e60f356SBenjamin Gray if (unlikely(bp && counter_arch_bp(bp)->perf_single_step)) 48474c68810SRavi Bangoria goto reset; 48574c68810SRavi Bangoria } 48606532a67SK.Prasad return; 48706532a67SK.Prasad 48874c68810SRavi Bangoria reset: 48959dc5bfcSNicholas Piggin regs_set_return_msr(regs, regs->msr & ~MSR_SE); 49074c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 49174c68810SRavi Bangoria info = counter_arch_bp(__this_cpu_read(bp_per_reg[i])); 49274c68810SRavi Bangoria __set_breakpoint(i, info); 493*1e60f356SBenjamin Gray info->perf_single_step = false; 49474c68810SRavi Bangoria } 49506532a67SK.Prasad } 49606532a67SK.Prasad 497f6780ce6SRavi Bangoria static bool is_larx_stcx_instr(int type) 498f6780ce6SRavi Bangoria { 499f6780ce6SRavi Bangoria return type == LARX || type == STCX; 50074c68810SRavi Bangoria } 50174c68810SRavi Bangoria 5023d2ffcddSRavi Bangoria static bool is_octword_vsx_instr(int type, int size) 5033d2ffcddSRavi Bangoria { 5043d2ffcddSRavi Bangoria return ((type == LOAD_VSX || type == STORE_VSX) && size == 32); 5053d2ffcddSRavi Bangoria } 5063d2ffcddSRavi Bangoria 507658d029dSChristophe Leroy /* 508bc01bdf6SRavi Bangoria * We've failed in reliably handling the hw-breakpoint. Unregister 509bc01bdf6SRavi Bangoria * it and throw a warning message to let the user know about it. 510658d029dSChristophe Leroy */ 511668a6ec6SBenjamin Gray static void handler_error(struct perf_event *bp) 51274c68810SRavi Bangoria { 51374c68810SRavi Bangoria WARN(1, "Unable to handle hardware breakpoint. Breakpoint at 0x%lx will be disabled.", 514668a6ec6SBenjamin Gray counter_arch_bp(bp)->address); 515658d029dSChristophe Leroy perf_event_disable_inatomic(bp); 51674c68810SRavi Bangoria } 51774c68810SRavi Bangoria 518668a6ec6SBenjamin Gray static void larx_stcx_err(struct perf_event *bp) 51974c68810SRavi Bangoria { 52074c68810SRavi Bangoria printk_ratelimited("Breakpoint hit on instruction that can't be emulated. Breakpoint at 0x%lx will be disabled.\n", 521668a6ec6SBenjamin Gray counter_arch_bp(bp)->address); 52274c68810SRavi Bangoria perf_event_disable_inatomic(bp); 52374c68810SRavi Bangoria } 52474c68810SRavi Bangoria 52574c68810SRavi Bangoria static bool stepping_handler(struct pt_regs *regs, struct perf_event **bp, 526668a6ec6SBenjamin Gray int *hit, ppc_inst_t instr) 52774c68810SRavi Bangoria { 52874c68810SRavi Bangoria int i; 52974c68810SRavi Bangoria int stepped; 53074c68810SRavi Bangoria 53174c68810SRavi Bangoria /* Do not emulate user-space instructions, instead single-step them */ 53274c68810SRavi Bangoria if (user_mode(regs)) { 53374c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 53474c68810SRavi Bangoria if (!hit[i]) 53574c68810SRavi Bangoria continue; 536*1e60f356SBenjamin Gray 537*1e60f356SBenjamin Gray counter_arch_bp(bp[i])->perf_single_step = true; 538668a6ec6SBenjamin Gray bp[i] = NULL; 53974c68810SRavi Bangoria } 54059dc5bfcSNicholas Piggin regs_set_return_msr(regs, regs->msr | MSR_SE); 541658d029dSChristophe Leroy return false; 542658d029dSChristophe Leroy } 543658d029dSChristophe Leroy 54474c68810SRavi Bangoria stepped = emulate_step(regs, instr); 54574c68810SRavi Bangoria if (!stepped) { 54674c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 54774c68810SRavi Bangoria if (!hit[i]) 54874c68810SRavi Bangoria continue; 549668a6ec6SBenjamin Gray handler_error(bp[i]); 550668a6ec6SBenjamin Gray bp[i] = NULL; 55174c68810SRavi Bangoria } 55274c68810SRavi Bangoria return false; 55374c68810SRavi Bangoria } 55474c68810SRavi Bangoria return true; 55574c68810SRavi Bangoria } 55674c68810SRavi Bangoria 557668a6ec6SBenjamin Gray static void handle_p10dd1_spurious_exception(struct perf_event **bp, 5583d2ffcddSRavi Bangoria int *hit, unsigned long ea) 5593d2ffcddSRavi Bangoria { 5603d2ffcddSRavi Bangoria int i; 5613d2ffcddSRavi Bangoria unsigned long hw_end_addr; 5623d2ffcddSRavi Bangoria 5633d2ffcddSRavi Bangoria /* 5643d2ffcddSRavi Bangoria * Handle spurious exception only when any bp_per_reg is set. 5653d2ffcddSRavi Bangoria * Otherwise this might be created by xmon and not actually a 5663d2ffcddSRavi Bangoria * spurious exception. 5673d2ffcddSRavi Bangoria */ 5683d2ffcddSRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 569668a6ec6SBenjamin Gray struct arch_hw_breakpoint *info; 570668a6ec6SBenjamin Gray 571668a6ec6SBenjamin Gray if (!bp[i]) 5723d2ffcddSRavi Bangoria continue; 5733d2ffcddSRavi Bangoria 574668a6ec6SBenjamin Gray info = counter_arch_bp(bp[i]); 575668a6ec6SBenjamin Gray 576668a6ec6SBenjamin Gray hw_end_addr = ALIGN(info->address + info->len, HW_BREAKPOINT_SIZE); 5773d2ffcddSRavi Bangoria 5783d2ffcddSRavi Bangoria /* 5793d2ffcddSRavi Bangoria * Ending address of DAWR range is less than starting 5803d2ffcddSRavi Bangoria * address of op. 5813d2ffcddSRavi Bangoria */ 5823d2ffcddSRavi Bangoria if ((hw_end_addr - 1) >= ea) 5833d2ffcddSRavi Bangoria continue; 5843d2ffcddSRavi Bangoria 5853d2ffcddSRavi Bangoria /* 5863d2ffcddSRavi Bangoria * Those addresses need to be in the same or in two 5873d2ffcddSRavi Bangoria * consecutive 512B blocks; 5883d2ffcddSRavi Bangoria */ 5893d2ffcddSRavi Bangoria if (((hw_end_addr - 1) >> 10) != (ea >> 10)) 5903d2ffcddSRavi Bangoria continue; 5913d2ffcddSRavi Bangoria 5923d2ffcddSRavi Bangoria /* 5933d2ffcddSRavi Bangoria * 'op address + 64B' generates an address that has a 5943d2ffcddSRavi Bangoria * carry into bit 52 (crosses 2K boundary). 5953d2ffcddSRavi Bangoria */ 5963d2ffcddSRavi Bangoria if ((ea & 0x800) == ((ea + 64) & 0x800)) 5973d2ffcddSRavi Bangoria continue; 5983d2ffcddSRavi Bangoria 5993d2ffcddSRavi Bangoria break; 6003d2ffcddSRavi Bangoria } 6013d2ffcddSRavi Bangoria 6023d2ffcddSRavi Bangoria if (i == nr_wp_slots()) 6033d2ffcddSRavi Bangoria return; 6043d2ffcddSRavi Bangoria 6053d2ffcddSRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 606668a6ec6SBenjamin Gray if (bp[i]) { 6073d2ffcddSRavi Bangoria hit[i] = 1; 608668a6ec6SBenjamin Gray counter_arch_bp(bp[i])->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ; 6093d2ffcddSRavi Bangoria } 6103d2ffcddSRavi Bangoria } 6113d2ffcddSRavi Bangoria } 6123d2ffcddSRavi Bangoria 61303465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args) 6145aae8a53SK.Prasad { 61574c68810SRavi Bangoria bool err = false; 6165aae8a53SK.Prasad int rc = NOTIFY_STOP; 61774c68810SRavi Bangoria struct perf_event *bp[HBP_NUM_MAX] = { NULL }; 6185aae8a53SK.Prasad struct pt_regs *regs = args->regs; 61974c68810SRavi Bangoria int i; 62074c68810SRavi Bangoria int hit[HBP_NUM_MAX] = {0}; 62174c68810SRavi Bangoria int nr_hit = 0; 62274c68810SRavi Bangoria bool ptrace_bp = false; 623c545b9f0SChristophe Leroy ppc_inst_t instr = ppc_inst(0); 62474c68810SRavi Bangoria int type = 0; 62574c68810SRavi Bangoria int size = 0; 626f6680275SRussell Currey unsigned long ea = 0; 6275aae8a53SK.Prasad 6285aae8a53SK.Prasad /* Disable breakpoints during exception handling */ 6299422de3eSMichael Neuling hw_breakpoint_disable(); 630574cb248SPaul Mackerras 6315aae8a53SK.Prasad /* 6325aae8a53SK.Prasad * The counter may be concurrently released but that can only 6335aae8a53SK.Prasad * occur from a call_rcu() path. We can then safely fetch 6345aae8a53SK.Prasad * the breakpoint, use its callback, touch its counter 6355aae8a53SK.Prasad * while we are in an rcu_read_lock() path. 6365aae8a53SK.Prasad */ 6375aae8a53SK.Prasad rcu_read_lock(); 6385aae8a53SK.Prasad 63974c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx)) 640edc8dd99SRavi Bangoria wp_get_instr_detail(regs, &instr, &type, &size, &ea); 64174c68810SRavi Bangoria 64274c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 643668a6ec6SBenjamin Gray struct arch_hw_breakpoint *info; 644668a6ec6SBenjamin Gray 64574c68810SRavi Bangoria bp[i] = __this_cpu_read(bp_per_reg[i]); 64674c68810SRavi Bangoria if (!bp[i]) 64774c68810SRavi Bangoria continue; 64874c68810SRavi Bangoria 649668a6ec6SBenjamin Gray info = counter_arch_bp(bp[i]); 650668a6ec6SBenjamin Gray info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ; 65174c68810SRavi Bangoria 652668a6ec6SBenjamin Gray if (wp_check_constraints(regs, instr, ea, type, size, info)) { 65374c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx) && 65474c68810SRavi Bangoria ppc_inst_equal(instr, ppc_inst(0))) { 655668a6ec6SBenjamin Gray handler_error(bp[i]); 656668a6ec6SBenjamin Gray bp[i] = NULL; 65774c68810SRavi Bangoria err = 1; 65874c68810SRavi Bangoria continue; 65974c68810SRavi Bangoria } 66074c68810SRavi Bangoria 66174c68810SRavi Bangoria if (is_ptrace_bp(bp[i])) 66274c68810SRavi Bangoria ptrace_bp = true; 66374c68810SRavi Bangoria hit[i] = 1; 66474c68810SRavi Bangoria nr_hit++; 66574c68810SRavi Bangoria } 66674c68810SRavi Bangoria } 66774c68810SRavi Bangoria 66874c68810SRavi Bangoria if (err) 66974c68810SRavi Bangoria goto reset; 67074c68810SRavi Bangoria 67174c68810SRavi Bangoria if (!nr_hit) { 6723d2ffcddSRavi Bangoria /* Workaround for Power10 DD1 */ 6733d2ffcddSRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx) && mfspr(SPRN_PVR) == 0x800100 && 6743d2ffcddSRavi Bangoria is_octword_vsx_instr(type, size)) { 675668a6ec6SBenjamin Gray handle_p10dd1_spurious_exception(bp, hit, ea); 6763d2ffcddSRavi Bangoria } else { 677c21a493aSRavi Bangoria rc = NOTIFY_DONE; 6785aae8a53SK.Prasad goto out; 679c21a493aSRavi Bangoria } 6803d2ffcddSRavi Bangoria } 6815aae8a53SK.Prasad 6825aae8a53SK.Prasad /* 6835aae8a53SK.Prasad * Return early after invoking user-callback function without restoring 6845aae8a53SK.Prasad * DABR if the breakpoint is from ptrace which always operates in 6855aae8a53SK.Prasad * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal 6865aae8a53SK.Prasad * generated in do_dabr(). 6875aae8a53SK.Prasad */ 68874c68810SRavi Bangoria if (ptrace_bp) { 68974c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 69074c68810SRavi Bangoria if (!hit[i]) 69174c68810SRavi Bangoria continue; 69274c68810SRavi Bangoria perf_bp_event(bp[i], regs); 693668a6ec6SBenjamin Gray bp[i] = NULL; 69474c68810SRavi Bangoria } 6955aae8a53SK.Prasad rc = NOTIFY_DONE; 69674c68810SRavi Bangoria goto reset; 6975aae8a53SK.Prasad } 6985aae8a53SK.Prasad 69974c68810SRavi Bangoria if (!IS_ENABLED(CONFIG_PPC_8xx)) { 700f6780ce6SRavi Bangoria if (is_larx_stcx_instr(type)) { 70174c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 70274c68810SRavi Bangoria if (!hit[i]) 70374c68810SRavi Bangoria continue; 704668a6ec6SBenjamin Gray larx_stcx_err(bp[i]); 705668a6ec6SBenjamin Gray bp[i] = NULL; 70674c68810SRavi Bangoria } 70774c68810SRavi Bangoria goto reset; 70874c68810SRavi Bangoria } 70974c68810SRavi Bangoria 710668a6ec6SBenjamin Gray if (!stepping_handler(regs, bp, hit, instr)) 71174c68810SRavi Bangoria goto reset; 712e08658a6SRavi Bangoria } 7135aae8a53SK.Prasad 7145aae8a53SK.Prasad /* 7155aae8a53SK.Prasad * As a policy, the callback is invoked in a 'trigger-after-execute' 7165aae8a53SK.Prasad * fashion 7175aae8a53SK.Prasad */ 71874c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 71974c68810SRavi Bangoria if (!hit[i]) 72074c68810SRavi Bangoria continue; 721668a6ec6SBenjamin Gray if (!(counter_arch_bp(bp[i])->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) 72274c68810SRavi Bangoria perf_bp_event(bp[i], regs); 72374c68810SRavi Bangoria } 7245aae8a53SK.Prasad 72574c68810SRavi Bangoria reset: 72674c68810SRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 727668a6ec6SBenjamin Gray if (!bp[i]) 72874c68810SRavi Bangoria continue; 729668a6ec6SBenjamin Gray __set_breakpoint(i, counter_arch_bp(bp[i])); 73074c68810SRavi Bangoria } 73174c68810SRavi Bangoria 7325aae8a53SK.Prasad out: 7335aae8a53SK.Prasad rcu_read_unlock(); 7345aae8a53SK.Prasad return rc; 7355aae8a53SK.Prasad } 73603465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler); 7375aae8a53SK.Prasad 7385aae8a53SK.Prasad /* 7395aae8a53SK.Prasad * Handle single-step exceptions following a DABR hit. 7405aae8a53SK.Prasad */ 74103465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args) 7425aae8a53SK.Prasad { 7435aae8a53SK.Prasad struct pt_regs *regs = args->regs; 74474c68810SRavi Bangoria bool found = false; 7455aae8a53SK.Prasad 7465aae8a53SK.Prasad /* 7475aae8a53SK.Prasad * Check if we are single-stepping as a result of a 7485aae8a53SK.Prasad * previous HW Breakpoint exception 7495aae8a53SK.Prasad */ 750*1e60f356SBenjamin Gray for (int i = 0; i < nr_wp_slots(); i++) { 751*1e60f356SBenjamin Gray struct perf_event *bp; 752*1e60f356SBenjamin Gray struct arch_hw_breakpoint *info; 753*1e60f356SBenjamin Gray 754*1e60f356SBenjamin Gray bp = __this_cpu_read(bp_per_reg[i]); 7555aae8a53SK.Prasad 75674c68810SRavi Bangoria if (!bp) 75774c68810SRavi Bangoria continue; 75874c68810SRavi Bangoria 7593f4693eeSMichael Neuling info = counter_arch_bp(bp); 7605aae8a53SK.Prasad 761*1e60f356SBenjamin Gray if (!info->perf_single_step) 762*1e60f356SBenjamin Gray continue; 763*1e60f356SBenjamin Gray 764*1e60f356SBenjamin Gray found = true; 765*1e60f356SBenjamin Gray 7665aae8a53SK.Prasad /* 76774c68810SRavi Bangoria * We shall invoke the user-defined callback function in the 76874c68810SRavi Bangoria * single stepping handler to confirm to 'trigger-after-execute' 76974c68810SRavi Bangoria * semantics 7705aae8a53SK.Prasad */ 7719422de3eSMichael Neuling if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) 7725aae8a53SK.Prasad perf_bp_event(bp, regs); 773*1e60f356SBenjamin Gray 774*1e60f356SBenjamin Gray info->perf_single_step = false; 77574c68810SRavi Bangoria } 7765aae8a53SK.Prasad 77774c68810SRavi Bangoria if (!found) 77874c68810SRavi Bangoria return NOTIFY_DONE; 77974c68810SRavi Bangoria 780*1e60f356SBenjamin Gray for (int i = 0; i < nr_wp_slots(); i++) { 781*1e60f356SBenjamin Gray struct perf_event *bp = __this_cpu_read(bp_per_reg[i]); 78274c68810SRavi Bangoria if (!bp) 78374c68810SRavi Bangoria continue; 78474c68810SRavi Bangoria 785*1e60f356SBenjamin Gray __set_breakpoint(i, counter_arch_bp(bp)); 78674c68810SRavi Bangoria } 78776b0f133SPaul Mackerras 78876b0f133SPaul Mackerras /* 78976b0f133SPaul Mackerras * If the process was being single-stepped by ptrace, let the 79076b0f133SPaul Mackerras * other single-step actions occur (e.g. generate SIGTRAP). 79176b0f133SPaul Mackerras */ 79276b0f133SPaul Mackerras if (test_thread_flag(TIF_SINGLESTEP)) 79376b0f133SPaul Mackerras return NOTIFY_DONE; 79476b0f133SPaul Mackerras 7955aae8a53SK.Prasad return NOTIFY_STOP; 7965aae8a53SK.Prasad } 79703465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction); 7985aae8a53SK.Prasad 7995aae8a53SK.Prasad /* 8005aae8a53SK.Prasad * Handle debug exception notifications. 8015aae8a53SK.Prasad */ 80203465f89SNicholas Piggin int hw_breakpoint_exceptions_notify( 8035aae8a53SK.Prasad struct notifier_block *unused, unsigned long val, void *data) 8045aae8a53SK.Prasad { 8055aae8a53SK.Prasad int ret = NOTIFY_DONE; 8065aae8a53SK.Prasad 8075aae8a53SK.Prasad switch (val) { 8085aae8a53SK.Prasad case DIE_DABR_MATCH: 8095aae8a53SK.Prasad ret = hw_breakpoint_handler(data); 8105aae8a53SK.Prasad break; 8115aae8a53SK.Prasad case DIE_SSTEP: 8125aae8a53SK.Prasad ret = single_step_dabr_instruction(data); 8135aae8a53SK.Prasad break; 8145aae8a53SK.Prasad } 8155aae8a53SK.Prasad 8165aae8a53SK.Prasad return ret; 8175aae8a53SK.Prasad } 81803465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify); 8195aae8a53SK.Prasad 8205aae8a53SK.Prasad /* 8215aae8a53SK.Prasad * Release the user breakpoints used by ptrace 8225aae8a53SK.Prasad */ 8235aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk) 8245aae8a53SK.Prasad { 8256b424efaSRavi Bangoria int i; 8265aae8a53SK.Prasad struct thread_struct *t = &tsk->thread; 8275aae8a53SK.Prasad 8286b424efaSRavi Bangoria for (i = 0; i < nr_wp_slots(); i++) { 8296b424efaSRavi Bangoria unregister_hw_breakpoint(t->ptrace_bps[i]); 8306b424efaSRavi Bangoria t->ptrace_bps[i] = NULL; 8316b424efaSRavi Bangoria } 8325aae8a53SK.Prasad } 8335aae8a53SK.Prasad 8345aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp) 8355aae8a53SK.Prasad { 8365aae8a53SK.Prasad /* TODO */ 8375aae8a53SK.Prasad } 838ccbed90bSChristophe Leroy 839ccbed90bSChristophe Leroy void ptrace_triggered(struct perf_event *bp, 840ccbed90bSChristophe Leroy struct perf_sample_data *data, struct pt_regs *regs) 841ccbed90bSChristophe Leroy { 842ccbed90bSChristophe Leroy struct perf_event_attr attr; 843ccbed90bSChristophe Leroy 844ccbed90bSChristophe Leroy /* 845ccbed90bSChristophe Leroy * Disable the breakpoint request here since ptrace has defined a 846ccbed90bSChristophe Leroy * one-shot behaviour for breakpoint exceptions in PPC64. 847ccbed90bSChristophe Leroy * The SIGTRAP signal is generated automatically for us in do_dabr(). 848ccbed90bSChristophe Leroy * We don't have to do anything about that here 849ccbed90bSChristophe Leroy */ 850ccbed90bSChristophe Leroy attr = bp->attr; 851ccbed90bSChristophe Leroy attr.disabled = true; 852ccbed90bSChristophe Leroy modify_user_hw_breakpoint(bp, &attr); 853ccbed90bSChristophe Leroy } 854