15aae8a53SK.Prasad /* 25aae8a53SK.Prasad * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility, 35aae8a53SK.Prasad * using the CPU's debug registers. Derived from 45aae8a53SK.Prasad * "arch/x86/kernel/hw_breakpoint.c" 55aae8a53SK.Prasad * 65aae8a53SK.Prasad * This program is free software; you can redistribute it and/or modify 75aae8a53SK.Prasad * it under the terms of the GNU General Public License as published by 85aae8a53SK.Prasad * the Free Software Foundation; either version 2 of the License, or 95aae8a53SK.Prasad * (at your option) any later version. 105aae8a53SK.Prasad * 115aae8a53SK.Prasad * This program is distributed in the hope that it will be useful, 125aae8a53SK.Prasad * but WITHOUT ANY WARRANTY; without even the implied warranty of 135aae8a53SK.Prasad * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 145aae8a53SK.Prasad * GNU General Public License for more details. 155aae8a53SK.Prasad * 165aae8a53SK.Prasad * You should have received a copy of the GNU General Public License 175aae8a53SK.Prasad * along with this program; if not, write to the Free Software 185aae8a53SK.Prasad * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 195aae8a53SK.Prasad * 205aae8a53SK.Prasad * Copyright 2010 IBM Corporation 215aae8a53SK.Prasad * Author: K.Prasad <prasad@linux.vnet.ibm.com> 225aae8a53SK.Prasad * 235aae8a53SK.Prasad */ 245aae8a53SK.Prasad 255aae8a53SK.Prasad #include <linux/hw_breakpoint.h> 265aae8a53SK.Prasad #include <linux/notifier.h> 275aae8a53SK.Prasad #include <linux/kprobes.h> 285aae8a53SK.Prasad #include <linux/percpu.h> 295aae8a53SK.Prasad #include <linux/kernel.h> 305aae8a53SK.Prasad #include <linux/sched.h> 315aae8a53SK.Prasad #include <linux/smp.h> 32*c1fe190cSMichael Neuling #include <linux/debugfs.h> 33*c1fe190cSMichael Neuling #include <linux/init.h> 345aae8a53SK.Prasad 355aae8a53SK.Prasad #include <asm/hw_breakpoint.h> 365aae8a53SK.Prasad #include <asm/processor.h> 375aae8a53SK.Prasad #include <asm/sstep.h> 3885ce9a5dSMichael Neuling #include <asm/debug.h> 39*c1fe190cSMichael Neuling #include <asm/debugfs.h> 40*c1fe190cSMichael Neuling #include <asm/hvcall.h> 417c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 425aae8a53SK.Prasad 435aae8a53SK.Prasad /* 445aae8a53SK.Prasad * Stores the breakpoints currently in use on each breakpoint address 455aae8a53SK.Prasad * register for every cpu 465aae8a53SK.Prasad */ 475aae8a53SK.Prasad static DEFINE_PER_CPU(struct perf_event *, bp_per_reg); 485aae8a53SK.Prasad 495aae8a53SK.Prasad /* 50d09ec738SPaul Mackerras * Returns total number of data or instruction breakpoints available. 51d09ec738SPaul Mackerras */ 52d09ec738SPaul Mackerras int hw_breakpoint_slots(int type) 53d09ec738SPaul Mackerras { 54d09ec738SPaul Mackerras if (type == TYPE_DATA) 55d09ec738SPaul Mackerras return HBP_NUM; 56d09ec738SPaul Mackerras return 0; /* no instruction breakpoints available */ 57d09ec738SPaul Mackerras } 58d09ec738SPaul Mackerras 59d09ec738SPaul Mackerras /* 605aae8a53SK.Prasad * Install a perf counter breakpoint. 615aae8a53SK.Prasad * 625aae8a53SK.Prasad * We seek a free debug address register and use it for this 635aae8a53SK.Prasad * breakpoint. 645aae8a53SK.Prasad * 655aae8a53SK.Prasad * Atomic: we hold the counter->ctx->lock and we only handle variables 665aae8a53SK.Prasad * and registers local to this cpu. 675aae8a53SK.Prasad */ 685aae8a53SK.Prasad int arch_install_hw_breakpoint(struct perf_event *bp) 695aae8a53SK.Prasad { 705aae8a53SK.Prasad struct arch_hw_breakpoint *info = counter_arch_bp(bp); 7169111bacSChristoph Lameter struct perf_event **slot = this_cpu_ptr(&bp_per_reg); 725aae8a53SK.Prasad 735aae8a53SK.Prasad *slot = bp; 745aae8a53SK.Prasad 755aae8a53SK.Prasad /* 765aae8a53SK.Prasad * Do not install DABR values if the instruction must be single-stepped. 775aae8a53SK.Prasad * If so, DABR will be populated in single_step_dabr_instruction(). 785aae8a53SK.Prasad */ 795aae8a53SK.Prasad if (current->thread.last_hit_ubp != bp) 8021f58507SPaul Gortmaker __set_breakpoint(info); 815aae8a53SK.Prasad 825aae8a53SK.Prasad return 0; 835aae8a53SK.Prasad } 845aae8a53SK.Prasad 855aae8a53SK.Prasad /* 865aae8a53SK.Prasad * Uninstall the breakpoint contained in the given counter. 875aae8a53SK.Prasad * 885aae8a53SK.Prasad * First we search the debug address register it uses and then we disable 895aae8a53SK.Prasad * it. 905aae8a53SK.Prasad * 915aae8a53SK.Prasad * Atomic: we hold the counter->ctx->lock and we only handle variables 925aae8a53SK.Prasad * and registers local to this cpu. 935aae8a53SK.Prasad */ 945aae8a53SK.Prasad void arch_uninstall_hw_breakpoint(struct perf_event *bp) 955aae8a53SK.Prasad { 9669111bacSChristoph Lameter struct perf_event **slot = this_cpu_ptr(&bp_per_reg); 975aae8a53SK.Prasad 985aae8a53SK.Prasad if (*slot != bp) { 995aae8a53SK.Prasad WARN_ONCE(1, "Can't find the breakpoint"); 1005aae8a53SK.Prasad return; 1015aae8a53SK.Prasad } 1025aae8a53SK.Prasad 1035aae8a53SK.Prasad *slot = NULL; 1049422de3eSMichael Neuling hw_breakpoint_disable(); 1055aae8a53SK.Prasad } 1065aae8a53SK.Prasad 1075aae8a53SK.Prasad /* 1085aae8a53SK.Prasad * Perform cleanup of arch-specific counters during unregistration 1095aae8a53SK.Prasad * of the perf-event 1105aae8a53SK.Prasad */ 1115aae8a53SK.Prasad void arch_unregister_hw_breakpoint(struct perf_event *bp) 1125aae8a53SK.Prasad { 1135aae8a53SK.Prasad /* 1145aae8a53SK.Prasad * If the breakpoint is unregistered between a hw_breakpoint_handler() 1155aae8a53SK.Prasad * and the single_step_dabr_instruction(), then cleanup the breakpoint 1165aae8a53SK.Prasad * restoration variables to prevent dangling pointers. 117fb822e60SRavi Bangoria * FIXME, this should not be using bp->ctx at all! Sayeth peterz. 1185aae8a53SK.Prasad */ 119fb822e60SRavi Bangoria if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L)) 1205aae8a53SK.Prasad bp->ctx->task->thread.last_hit_ubp = NULL; 1215aae8a53SK.Prasad } 1225aae8a53SK.Prasad 1235aae8a53SK.Prasad /* 1245aae8a53SK.Prasad * Check for virtual address in kernel space. 1255aae8a53SK.Prasad */ 1268e983ff9SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw) 1275aae8a53SK.Prasad { 1288e983ff9SFrederic Weisbecker return is_kernel_addr(hw->address); 1295aae8a53SK.Prasad } 1305aae8a53SK.Prasad 1315aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type) 1325aae8a53SK.Prasad { 1339422de3eSMichael Neuling *gen_bp_type = 0; 1349422de3eSMichael Neuling if (type & HW_BRK_TYPE_READ) 1359422de3eSMichael Neuling *gen_bp_type |= HW_BREAKPOINT_R; 1369422de3eSMichael Neuling if (type & HW_BRK_TYPE_WRITE) 1379422de3eSMichael Neuling *gen_bp_type |= HW_BREAKPOINT_W; 1389422de3eSMichael Neuling if (*gen_bp_type == 0) 1395aae8a53SK.Prasad return -EINVAL; 1405aae8a53SK.Prasad return 0; 1415aae8a53SK.Prasad } 1425aae8a53SK.Prasad 1435aae8a53SK.Prasad /* 1445aae8a53SK.Prasad * Validate the arch-specific HW Breakpoint register settings 1455aae8a53SK.Prasad */ 1465d5176baSFrederic Weisbecker int hw_breakpoint_arch_parse(struct perf_event *bp, 1475d5176baSFrederic Weisbecker const struct perf_event_attr *attr, 1485d5176baSFrederic Weisbecker struct arch_hw_breakpoint *hw) 1495aae8a53SK.Prasad { 1504ae7ebe9SMichael Neuling int ret = -EINVAL, length_max; 1515aae8a53SK.Prasad 1525aae8a53SK.Prasad if (!bp) 1535aae8a53SK.Prasad return ret; 1545aae8a53SK.Prasad 1555d5176baSFrederic Weisbecker hw->type = HW_BRK_TYPE_TRANSLATE; 1565d5176baSFrederic Weisbecker if (attr->bp_type & HW_BREAKPOINT_R) 1575d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_READ; 1585d5176baSFrederic Weisbecker if (attr->bp_type & HW_BREAKPOINT_W) 1595d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_WRITE; 1605d5176baSFrederic Weisbecker if (hw->type == HW_BRK_TYPE_TRANSLATE) 1619422de3eSMichael Neuling /* must set alteast read or write */ 1625aae8a53SK.Prasad return ret; 1635d5176baSFrederic Weisbecker if (!attr->exclude_user) 1645d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_USER; 1655d5176baSFrederic Weisbecker if (!attr->exclude_kernel) 1665d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_KERNEL; 1675d5176baSFrederic Weisbecker if (!attr->exclude_hv) 1685d5176baSFrederic Weisbecker hw->type |= HW_BRK_TYPE_HYP; 1695d5176baSFrederic Weisbecker hw->address = attr->bp_addr; 1705d5176baSFrederic Weisbecker hw->len = attr->bp_len; 1715aae8a53SK.Prasad 1725aae8a53SK.Prasad /* 1735aae8a53SK.Prasad * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8) 1745aae8a53SK.Prasad * and breakpoint addresses are aligned to nearest double-word 1755aae8a53SK.Prasad * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the 1765aae8a53SK.Prasad * 'symbolsize' should satisfy the check below. 1775aae8a53SK.Prasad */ 17885ce9a5dSMichael Neuling if (!ppc_breakpoint_available()) 17985ce9a5dSMichael Neuling return -ENODEV; 1804ae7ebe9SMichael Neuling length_max = 8; /* DABR */ 181*c1fe190cSMichael Neuling if (dawr_enabled()) { 1824ae7ebe9SMichael Neuling length_max = 512 ; /* 64 doublewords */ 1834ae7ebe9SMichael Neuling /* DAWR region can't cross 512 boundary */ 1845d5176baSFrederic Weisbecker if ((attr->bp_addr >> 9) != 1855d5176baSFrederic Weisbecker ((attr->bp_addr + attr->bp_len - 1) >> 9)) 1864ae7ebe9SMichael Neuling return -EINVAL; 1874ae7ebe9SMichael Neuling } 1885d5176baSFrederic Weisbecker if (hw->len > 1895d5176baSFrederic Weisbecker (length_max - (hw->address & HW_BREAKPOINT_ALIGN))) 1905aae8a53SK.Prasad return -EINVAL; 1915aae8a53SK.Prasad return 0; 1925aae8a53SK.Prasad } 1935aae8a53SK.Prasad 1945aae8a53SK.Prasad /* 19506532a67SK.Prasad * Restores the breakpoint on the debug registers. 19606532a67SK.Prasad * Invoke this function if it is known that the execution context is 19706532a67SK.Prasad * about to change to cause loss of MSR_SE settings. 19806532a67SK.Prasad */ 19906532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs) 20006532a67SK.Prasad { 20106532a67SK.Prasad struct arch_hw_breakpoint *info; 20206532a67SK.Prasad 20306532a67SK.Prasad if (likely(!tsk->thread.last_hit_ubp)) 20406532a67SK.Prasad return; 20506532a67SK.Prasad 20606532a67SK.Prasad info = counter_arch_bp(tsk->thread.last_hit_ubp); 20706532a67SK.Prasad regs->msr &= ~MSR_SE; 20821f58507SPaul Gortmaker __set_breakpoint(info); 20906532a67SK.Prasad tsk->thread.last_hit_ubp = NULL; 21006532a67SK.Prasad } 21106532a67SK.Prasad 21206532a67SK.Prasad /* 2135aae8a53SK.Prasad * Handle debug exception notifications. 2145aae8a53SK.Prasad */ 21503465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args) 2165aae8a53SK.Prasad { 2175aae8a53SK.Prasad int rc = NOTIFY_STOP; 2185aae8a53SK.Prasad struct perf_event *bp; 2195aae8a53SK.Prasad struct pt_regs *regs = args->regs; 2204ad8622dSChristophe Leroy #ifndef CONFIG_PPC_8xx 2215aae8a53SK.Prasad int stepped = 1; 2225aae8a53SK.Prasad unsigned int instr; 2234ad8622dSChristophe Leroy #endif 2244ad8622dSChristophe Leroy struct arch_hw_breakpoint *info; 225e3e94084SK.Prasad unsigned long dar = regs->dar; 2265aae8a53SK.Prasad 2275aae8a53SK.Prasad /* Disable breakpoints during exception handling */ 2289422de3eSMichael Neuling hw_breakpoint_disable(); 229574cb248SPaul Mackerras 2305aae8a53SK.Prasad /* 2315aae8a53SK.Prasad * The counter may be concurrently released but that can only 2325aae8a53SK.Prasad * occur from a call_rcu() path. We can then safely fetch 2335aae8a53SK.Prasad * the breakpoint, use its callback, touch its counter 2345aae8a53SK.Prasad * while we are in an rcu_read_lock() path. 2355aae8a53SK.Prasad */ 2365aae8a53SK.Prasad rcu_read_lock(); 2375aae8a53SK.Prasad 23869111bacSChristoph Lameter bp = __this_cpu_read(bp_per_reg); 239c21a493aSRavi Bangoria if (!bp) { 240c21a493aSRavi Bangoria rc = NOTIFY_DONE; 2415aae8a53SK.Prasad goto out; 242c21a493aSRavi Bangoria } 2435aae8a53SK.Prasad info = counter_arch_bp(bp); 2445aae8a53SK.Prasad 2455aae8a53SK.Prasad /* 2465aae8a53SK.Prasad * Return early after invoking user-callback function without restoring 2475aae8a53SK.Prasad * DABR if the breakpoint is from ptrace which always operates in 2485aae8a53SK.Prasad * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal 2495aae8a53SK.Prasad * generated in do_dabr(). 2505aae8a53SK.Prasad */ 251574cb248SPaul Mackerras if (bp->overflow_handler == ptrace_triggered) { 2525aae8a53SK.Prasad perf_bp_event(bp, regs); 2535aae8a53SK.Prasad rc = NOTIFY_DONE; 2545aae8a53SK.Prasad goto out; 2555aae8a53SK.Prasad } 2565aae8a53SK.Prasad 257e3e94084SK.Prasad /* 258e3e94084SK.Prasad * Verify if dar lies within the address range occupied by the symbol 259574cb248SPaul Mackerras * being watched to filter extraneous exceptions. If it doesn't, 260574cb248SPaul Mackerras * we still need to single-step the instruction, but we don't 261574cb248SPaul Mackerras * generate an event. 262e3e94084SK.Prasad */ 263540e07c6SMichael Neuling info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ; 2649422de3eSMichael Neuling if (!((bp->attr.bp_addr <= dar) && 2659422de3eSMichael Neuling (dar - bp->attr.bp_addr < bp->attr.bp_len))) 2669422de3eSMichael Neuling info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ; 267e3e94084SK.Prasad 2684ad8622dSChristophe Leroy #ifndef CONFIG_PPC_8xx 2695aae8a53SK.Prasad /* Do not emulate user-space instructions, instead single-step them */ 2705aae8a53SK.Prasad if (user_mode(regs)) { 2716d9c00c6SMichael Neuling current->thread.last_hit_ubp = bp; 2725aae8a53SK.Prasad regs->msr |= MSR_SE; 2735aae8a53SK.Prasad goto out; 2745aae8a53SK.Prasad } 2755aae8a53SK.Prasad 2765aae8a53SK.Prasad stepped = 0; 2775aae8a53SK.Prasad instr = 0; 2785aae8a53SK.Prasad if (!__get_user_inatomic(instr, (unsigned int *) regs->nip)) 2795aae8a53SK.Prasad stepped = emulate_step(regs, instr); 2805aae8a53SK.Prasad 2815aae8a53SK.Prasad /* 2825aae8a53SK.Prasad * emulate_step() could not execute it. We've failed in reliably 2835aae8a53SK.Prasad * handling the hw-breakpoint. Unregister it and throw a warning 2845aae8a53SK.Prasad * message to let the user know about it. 2855aae8a53SK.Prasad */ 2865aae8a53SK.Prasad if (!stepped) { 2875aae8a53SK.Prasad WARN(1, "Unable to handle hardware breakpoint. Breakpoint at " 2885aae8a53SK.Prasad "0x%lx will be disabled.", info->address); 2895aab90ceSJiri Olsa perf_event_disable_inatomic(bp); 2905aae8a53SK.Prasad goto out; 2915aae8a53SK.Prasad } 2924ad8622dSChristophe Leroy #endif 2935aae8a53SK.Prasad /* 2945aae8a53SK.Prasad * As a policy, the callback is invoked in a 'trigger-after-execute' 2955aae8a53SK.Prasad * fashion 2965aae8a53SK.Prasad */ 2979422de3eSMichael Neuling if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) 2985aae8a53SK.Prasad perf_bp_event(bp, regs); 2995aae8a53SK.Prasad 30021f58507SPaul Gortmaker __set_breakpoint(info); 3015aae8a53SK.Prasad out: 3025aae8a53SK.Prasad rcu_read_unlock(); 3035aae8a53SK.Prasad return rc; 3045aae8a53SK.Prasad } 30503465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler); 3065aae8a53SK.Prasad 3075aae8a53SK.Prasad /* 3085aae8a53SK.Prasad * Handle single-step exceptions following a DABR hit. 3095aae8a53SK.Prasad */ 31003465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args) 3115aae8a53SK.Prasad { 3125aae8a53SK.Prasad struct pt_regs *regs = args->regs; 3135aae8a53SK.Prasad struct perf_event *bp = NULL; 3143f4693eeSMichael Neuling struct arch_hw_breakpoint *info; 3155aae8a53SK.Prasad 3165aae8a53SK.Prasad bp = current->thread.last_hit_ubp; 3175aae8a53SK.Prasad /* 3185aae8a53SK.Prasad * Check if we are single-stepping as a result of a 3195aae8a53SK.Prasad * previous HW Breakpoint exception 3205aae8a53SK.Prasad */ 3215aae8a53SK.Prasad if (!bp) 3225aae8a53SK.Prasad return NOTIFY_DONE; 3235aae8a53SK.Prasad 3243f4693eeSMichael Neuling info = counter_arch_bp(bp); 3255aae8a53SK.Prasad 3265aae8a53SK.Prasad /* 3275aae8a53SK.Prasad * We shall invoke the user-defined callback function in the single 3285aae8a53SK.Prasad * stepping handler to confirm to 'trigger-after-execute' semantics 3295aae8a53SK.Prasad */ 3309422de3eSMichael Neuling if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) 3315aae8a53SK.Prasad perf_bp_event(bp, regs); 3325aae8a53SK.Prasad 33321f58507SPaul Gortmaker __set_breakpoint(info); 3345aae8a53SK.Prasad current->thread.last_hit_ubp = NULL; 33576b0f133SPaul Mackerras 33676b0f133SPaul Mackerras /* 33776b0f133SPaul Mackerras * If the process was being single-stepped by ptrace, let the 33876b0f133SPaul Mackerras * other single-step actions occur (e.g. generate SIGTRAP). 33976b0f133SPaul Mackerras */ 34076b0f133SPaul Mackerras if (test_thread_flag(TIF_SINGLESTEP)) 34176b0f133SPaul Mackerras return NOTIFY_DONE; 34276b0f133SPaul Mackerras 3435aae8a53SK.Prasad return NOTIFY_STOP; 3445aae8a53SK.Prasad } 34503465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction); 3465aae8a53SK.Prasad 3475aae8a53SK.Prasad /* 3485aae8a53SK.Prasad * Handle debug exception notifications. 3495aae8a53SK.Prasad */ 35003465f89SNicholas Piggin int hw_breakpoint_exceptions_notify( 3515aae8a53SK.Prasad struct notifier_block *unused, unsigned long val, void *data) 3525aae8a53SK.Prasad { 3535aae8a53SK.Prasad int ret = NOTIFY_DONE; 3545aae8a53SK.Prasad 3555aae8a53SK.Prasad switch (val) { 3565aae8a53SK.Prasad case DIE_DABR_MATCH: 3575aae8a53SK.Prasad ret = hw_breakpoint_handler(data); 3585aae8a53SK.Prasad break; 3595aae8a53SK.Prasad case DIE_SSTEP: 3605aae8a53SK.Prasad ret = single_step_dabr_instruction(data); 3615aae8a53SK.Prasad break; 3625aae8a53SK.Prasad } 3635aae8a53SK.Prasad 3645aae8a53SK.Prasad return ret; 3655aae8a53SK.Prasad } 36603465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify); 3675aae8a53SK.Prasad 3685aae8a53SK.Prasad /* 3695aae8a53SK.Prasad * Release the user breakpoints used by ptrace 3705aae8a53SK.Prasad */ 3715aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk) 3725aae8a53SK.Prasad { 3735aae8a53SK.Prasad struct thread_struct *t = &tsk->thread; 3745aae8a53SK.Prasad 3755aae8a53SK.Prasad unregister_hw_breakpoint(t->ptrace_bps[0]); 3765aae8a53SK.Prasad t->ptrace_bps[0] = NULL; 3775aae8a53SK.Prasad } 3785aae8a53SK.Prasad 3795aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp) 3805aae8a53SK.Prasad { 3815aae8a53SK.Prasad /* TODO */ 3825aae8a53SK.Prasad } 383*c1fe190cSMichael Neuling 384*c1fe190cSMichael Neuling bool dawr_force_enable; 385*c1fe190cSMichael Neuling EXPORT_SYMBOL_GPL(dawr_force_enable); 386*c1fe190cSMichael Neuling 387*c1fe190cSMichael Neuling static ssize_t dawr_write_file_bool(struct file *file, 388*c1fe190cSMichael Neuling const char __user *user_buf, 389*c1fe190cSMichael Neuling size_t count, loff_t *ppos) 390*c1fe190cSMichael Neuling { 391*c1fe190cSMichael Neuling struct arch_hw_breakpoint null_brk = {0, 0, 0}; 392*c1fe190cSMichael Neuling size_t rc; 393*c1fe190cSMichael Neuling 394*c1fe190cSMichael Neuling /* Send error to user if they hypervisor won't allow us to write DAWR */ 395*c1fe190cSMichael Neuling if ((!dawr_force_enable) && 396*c1fe190cSMichael Neuling (firmware_has_feature(FW_FEATURE_LPAR)) && 397*c1fe190cSMichael Neuling (set_dawr(&null_brk) != H_SUCCESS)) 398*c1fe190cSMichael Neuling return -1; 399*c1fe190cSMichael Neuling 400*c1fe190cSMichael Neuling rc = debugfs_write_file_bool(file, user_buf, count, ppos); 401*c1fe190cSMichael Neuling if (rc) 402*c1fe190cSMichael Neuling return rc; 403*c1fe190cSMichael Neuling 404*c1fe190cSMichael Neuling /* If we are clearing, make sure all CPUs have the DAWR cleared */ 405*c1fe190cSMichael Neuling if (!dawr_force_enable) 406*c1fe190cSMichael Neuling smp_call_function((smp_call_func_t)set_dawr, &null_brk, 0); 407*c1fe190cSMichael Neuling 408*c1fe190cSMichael Neuling return rc; 409*c1fe190cSMichael Neuling } 410*c1fe190cSMichael Neuling 411*c1fe190cSMichael Neuling static const struct file_operations dawr_enable_fops = { 412*c1fe190cSMichael Neuling .read = debugfs_read_file_bool, 413*c1fe190cSMichael Neuling .write = dawr_write_file_bool, 414*c1fe190cSMichael Neuling .open = simple_open, 415*c1fe190cSMichael Neuling .llseek = default_llseek, 416*c1fe190cSMichael Neuling }; 417*c1fe190cSMichael Neuling 418*c1fe190cSMichael Neuling static int __init dawr_force_setup(void) 419*c1fe190cSMichael Neuling { 420*c1fe190cSMichael Neuling dawr_force_enable = false; 421*c1fe190cSMichael Neuling 422*c1fe190cSMichael Neuling if (cpu_has_feature(CPU_FTR_DAWR)) { 423*c1fe190cSMichael Neuling /* Don't setup sysfs file for user control on P8 */ 424*c1fe190cSMichael Neuling dawr_force_enable = true; 425*c1fe190cSMichael Neuling return 0; 426*c1fe190cSMichael Neuling } 427*c1fe190cSMichael Neuling 428*c1fe190cSMichael Neuling if (PVR_VER(mfspr(SPRN_PVR)) == PVR_POWER9) { 429*c1fe190cSMichael Neuling /* Turn DAWR off by default, but allow admin to turn it on */ 430*c1fe190cSMichael Neuling dawr_force_enable = false; 431*c1fe190cSMichael Neuling debugfs_create_file_unsafe("dawr_enable_dangerous", 0600, 432*c1fe190cSMichael Neuling powerpc_debugfs_root, 433*c1fe190cSMichael Neuling &dawr_force_enable, 434*c1fe190cSMichael Neuling &dawr_enable_fops); 435*c1fe190cSMichael Neuling } 436*c1fe190cSMichael Neuling return 0; 437*c1fe190cSMichael Neuling } 438*c1fe190cSMichael Neuling arch_initcall(dawr_force_setup); 439