xref: /linux/arch/powerpc/kernel/hw_breakpoint.c (revision 1a59d1b8e05ea6ab45f7e18897de1ef0e6bc3da6)
1*1a59d1b8SThomas 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>
18c1fe190cSMichael Neuling #include <linux/debugfs.h>
19c1fe190cSMichael Neuling #include <linux/init.h>
205aae8a53SK.Prasad 
215aae8a53SK.Prasad #include <asm/hw_breakpoint.h>
225aae8a53SK.Prasad #include <asm/processor.h>
235aae8a53SK.Prasad #include <asm/sstep.h>
2485ce9a5dSMichael Neuling #include <asm/debug.h>
25c1fe190cSMichael Neuling #include <asm/debugfs.h>
26c1fe190cSMichael Neuling #include <asm/hvcall.h>
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
285aae8a53SK.Prasad 
295aae8a53SK.Prasad /*
305aae8a53SK.Prasad  * Stores the breakpoints currently in use on each breakpoint address
315aae8a53SK.Prasad  * register for every cpu
325aae8a53SK.Prasad  */
335aae8a53SK.Prasad static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
345aae8a53SK.Prasad 
355aae8a53SK.Prasad /*
36d09ec738SPaul Mackerras  * Returns total number of data or instruction breakpoints available.
37d09ec738SPaul Mackerras  */
38d09ec738SPaul Mackerras int hw_breakpoint_slots(int type)
39d09ec738SPaul Mackerras {
40d09ec738SPaul Mackerras 	if (type == TYPE_DATA)
41d09ec738SPaul Mackerras 		return HBP_NUM;
42d09ec738SPaul Mackerras 	return 0;		/* no instruction breakpoints available */
43d09ec738SPaul Mackerras }
44d09ec738SPaul Mackerras 
45d09ec738SPaul Mackerras /*
465aae8a53SK.Prasad  * Install a perf counter breakpoint.
475aae8a53SK.Prasad  *
485aae8a53SK.Prasad  * We seek a free debug address register and use it for this
495aae8a53SK.Prasad  * breakpoint.
505aae8a53SK.Prasad  *
515aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
525aae8a53SK.Prasad  * and registers local to this cpu.
535aae8a53SK.Prasad  */
545aae8a53SK.Prasad int arch_install_hw_breakpoint(struct perf_event *bp)
555aae8a53SK.Prasad {
565aae8a53SK.Prasad 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
5769111bacSChristoph Lameter 	struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
585aae8a53SK.Prasad 
595aae8a53SK.Prasad 	*slot = bp;
605aae8a53SK.Prasad 
615aae8a53SK.Prasad 	/*
625aae8a53SK.Prasad 	 * Do not install DABR values if the instruction must be single-stepped.
635aae8a53SK.Prasad 	 * If so, DABR will be populated in single_step_dabr_instruction().
645aae8a53SK.Prasad 	 */
655aae8a53SK.Prasad 	if (current->thread.last_hit_ubp != bp)
6621f58507SPaul Gortmaker 		__set_breakpoint(info);
675aae8a53SK.Prasad 
685aae8a53SK.Prasad 	return 0;
695aae8a53SK.Prasad }
705aae8a53SK.Prasad 
715aae8a53SK.Prasad /*
725aae8a53SK.Prasad  * Uninstall the breakpoint contained in the given counter.
735aae8a53SK.Prasad  *
745aae8a53SK.Prasad  * First we search the debug address register it uses and then we disable
755aae8a53SK.Prasad  * it.
765aae8a53SK.Prasad  *
775aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
785aae8a53SK.Prasad  * and registers local to this cpu.
795aae8a53SK.Prasad  */
805aae8a53SK.Prasad void arch_uninstall_hw_breakpoint(struct perf_event *bp)
815aae8a53SK.Prasad {
8269111bacSChristoph Lameter 	struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
835aae8a53SK.Prasad 
845aae8a53SK.Prasad 	if (*slot != bp) {
855aae8a53SK.Prasad 		WARN_ONCE(1, "Can't find the breakpoint");
865aae8a53SK.Prasad 		return;
875aae8a53SK.Prasad 	}
885aae8a53SK.Prasad 
895aae8a53SK.Prasad 	*slot = NULL;
909422de3eSMichael Neuling 	hw_breakpoint_disable();
915aae8a53SK.Prasad }
925aae8a53SK.Prasad 
935aae8a53SK.Prasad /*
945aae8a53SK.Prasad  * Perform cleanup of arch-specific counters during unregistration
955aae8a53SK.Prasad  * of the perf-event
965aae8a53SK.Prasad  */
975aae8a53SK.Prasad void arch_unregister_hw_breakpoint(struct perf_event *bp)
985aae8a53SK.Prasad {
995aae8a53SK.Prasad 	/*
1005aae8a53SK.Prasad 	 * If the breakpoint is unregistered between a hw_breakpoint_handler()
1015aae8a53SK.Prasad 	 * and the single_step_dabr_instruction(), then cleanup the breakpoint
1025aae8a53SK.Prasad 	 * restoration variables to prevent dangling pointers.
103fb822e60SRavi Bangoria 	 * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
1045aae8a53SK.Prasad 	 */
105fb822e60SRavi Bangoria 	if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
1065aae8a53SK.Prasad 		bp->ctx->task->thread.last_hit_ubp = NULL;
1075aae8a53SK.Prasad }
1085aae8a53SK.Prasad 
1095aae8a53SK.Prasad /*
1105aae8a53SK.Prasad  * Check for virtual address in kernel space.
1115aae8a53SK.Prasad  */
1128e983ff9SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
1135aae8a53SK.Prasad {
1148e983ff9SFrederic Weisbecker 	return is_kernel_addr(hw->address);
1155aae8a53SK.Prasad }
1165aae8a53SK.Prasad 
1175aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type)
1185aae8a53SK.Prasad {
1199422de3eSMichael Neuling 	*gen_bp_type = 0;
1209422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_READ)
1219422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_R;
1229422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_WRITE)
1239422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_W;
1249422de3eSMichael Neuling 	if (*gen_bp_type == 0)
1255aae8a53SK.Prasad 		return -EINVAL;
1265aae8a53SK.Prasad 	return 0;
1275aae8a53SK.Prasad }
1285aae8a53SK.Prasad 
1295aae8a53SK.Prasad /*
1305aae8a53SK.Prasad  * Validate the arch-specific HW Breakpoint register settings
1315aae8a53SK.Prasad  */
1325d5176baSFrederic Weisbecker int hw_breakpoint_arch_parse(struct perf_event *bp,
1335d5176baSFrederic Weisbecker 			     const struct perf_event_attr *attr,
1345d5176baSFrederic Weisbecker 			     struct arch_hw_breakpoint *hw)
1355aae8a53SK.Prasad {
1364ae7ebe9SMichael Neuling 	int ret = -EINVAL, length_max;
1375aae8a53SK.Prasad 
1385aae8a53SK.Prasad 	if (!bp)
1395aae8a53SK.Prasad 		return ret;
1405aae8a53SK.Prasad 
1415d5176baSFrederic Weisbecker 	hw->type = HW_BRK_TYPE_TRANSLATE;
1425d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_R)
1435d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_READ;
1445d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_W)
1455d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_WRITE;
1465d5176baSFrederic Weisbecker 	if (hw->type == HW_BRK_TYPE_TRANSLATE)
1479422de3eSMichael Neuling 		/* must set alteast read or write */
1485aae8a53SK.Prasad 		return ret;
1495d5176baSFrederic Weisbecker 	if (!attr->exclude_user)
1505d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_USER;
1515d5176baSFrederic Weisbecker 	if (!attr->exclude_kernel)
1525d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_KERNEL;
1535d5176baSFrederic Weisbecker 	if (!attr->exclude_hv)
1545d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_HYP;
1555d5176baSFrederic Weisbecker 	hw->address = attr->bp_addr;
1565d5176baSFrederic Weisbecker 	hw->len = attr->bp_len;
1575aae8a53SK.Prasad 
1585aae8a53SK.Prasad 	/*
1595aae8a53SK.Prasad 	 * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8)
1605aae8a53SK.Prasad 	 * and breakpoint addresses are aligned to nearest double-word
1615aae8a53SK.Prasad 	 * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the
1625aae8a53SK.Prasad 	 * 'symbolsize' should satisfy the check below.
1635aae8a53SK.Prasad 	 */
16485ce9a5dSMichael Neuling 	if (!ppc_breakpoint_available())
16585ce9a5dSMichael Neuling 		return -ENODEV;
1664ae7ebe9SMichael Neuling 	length_max = 8; /* DABR */
167c1fe190cSMichael Neuling 	if (dawr_enabled()) {
1684ae7ebe9SMichael Neuling 		length_max = 512 ; /* 64 doublewords */
1694ae7ebe9SMichael Neuling 		/* DAWR region can't cross 512 boundary */
1705d5176baSFrederic Weisbecker 		if ((attr->bp_addr >> 9) !=
1715d5176baSFrederic Weisbecker 		    ((attr->bp_addr + attr->bp_len - 1) >> 9))
1724ae7ebe9SMichael Neuling 			return -EINVAL;
1734ae7ebe9SMichael Neuling 	}
1745d5176baSFrederic Weisbecker 	if (hw->len >
1755d5176baSFrederic Weisbecker 	    (length_max - (hw->address & HW_BREAKPOINT_ALIGN)))
1765aae8a53SK.Prasad 		return -EINVAL;
1775aae8a53SK.Prasad 	return 0;
1785aae8a53SK.Prasad }
1795aae8a53SK.Prasad 
1805aae8a53SK.Prasad /*
18106532a67SK.Prasad  * Restores the breakpoint on the debug registers.
18206532a67SK.Prasad  * Invoke this function if it is known that the execution context is
18306532a67SK.Prasad  * about to change to cause loss of MSR_SE settings.
18406532a67SK.Prasad  */
18506532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
18606532a67SK.Prasad {
18706532a67SK.Prasad 	struct arch_hw_breakpoint *info;
18806532a67SK.Prasad 
18906532a67SK.Prasad 	if (likely(!tsk->thread.last_hit_ubp))
19006532a67SK.Prasad 		return;
19106532a67SK.Prasad 
19206532a67SK.Prasad 	info = counter_arch_bp(tsk->thread.last_hit_ubp);
19306532a67SK.Prasad 	regs->msr &= ~MSR_SE;
19421f58507SPaul Gortmaker 	__set_breakpoint(info);
19506532a67SK.Prasad 	tsk->thread.last_hit_ubp = NULL;
19606532a67SK.Prasad }
19706532a67SK.Prasad 
19806532a67SK.Prasad /*
1995aae8a53SK.Prasad  * Handle debug exception notifications.
2005aae8a53SK.Prasad  */
20103465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args)
2025aae8a53SK.Prasad {
2035aae8a53SK.Prasad 	int rc = NOTIFY_STOP;
2045aae8a53SK.Prasad 	struct perf_event *bp;
2055aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
2064ad8622dSChristophe Leroy #ifndef CONFIG_PPC_8xx
2075aae8a53SK.Prasad 	int stepped = 1;
2085aae8a53SK.Prasad 	unsigned int instr;
2094ad8622dSChristophe Leroy #endif
2104ad8622dSChristophe Leroy 	struct arch_hw_breakpoint *info;
211e3e94084SK.Prasad 	unsigned long dar = regs->dar;
2125aae8a53SK.Prasad 
2135aae8a53SK.Prasad 	/* Disable breakpoints during exception handling */
2149422de3eSMichael Neuling 	hw_breakpoint_disable();
215574cb248SPaul Mackerras 
2165aae8a53SK.Prasad 	/*
2175aae8a53SK.Prasad 	 * The counter may be concurrently released but that can only
2185aae8a53SK.Prasad 	 * occur from a call_rcu() path. We can then safely fetch
2195aae8a53SK.Prasad 	 * the breakpoint, use its callback, touch its counter
2205aae8a53SK.Prasad 	 * while we are in an rcu_read_lock() path.
2215aae8a53SK.Prasad 	 */
2225aae8a53SK.Prasad 	rcu_read_lock();
2235aae8a53SK.Prasad 
22469111bacSChristoph Lameter 	bp = __this_cpu_read(bp_per_reg);
225c21a493aSRavi Bangoria 	if (!bp) {
226c21a493aSRavi Bangoria 		rc = NOTIFY_DONE;
2275aae8a53SK.Prasad 		goto out;
228c21a493aSRavi Bangoria 	}
2295aae8a53SK.Prasad 	info = counter_arch_bp(bp);
2305aae8a53SK.Prasad 
2315aae8a53SK.Prasad 	/*
2325aae8a53SK.Prasad 	 * Return early after invoking user-callback function without restoring
2335aae8a53SK.Prasad 	 * DABR if the breakpoint is from ptrace which always operates in
2345aae8a53SK.Prasad 	 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
2355aae8a53SK.Prasad 	 * generated in do_dabr().
2365aae8a53SK.Prasad 	 */
237574cb248SPaul Mackerras 	if (bp->overflow_handler == ptrace_triggered) {
2385aae8a53SK.Prasad 		perf_bp_event(bp, regs);
2395aae8a53SK.Prasad 		rc = NOTIFY_DONE;
2405aae8a53SK.Prasad 		goto out;
2415aae8a53SK.Prasad 	}
2425aae8a53SK.Prasad 
243e3e94084SK.Prasad 	/*
244e3e94084SK.Prasad 	 * Verify if dar lies within the address range occupied by the symbol
245574cb248SPaul Mackerras 	 * being watched to filter extraneous exceptions.  If it doesn't,
246574cb248SPaul Mackerras 	 * we still need to single-step the instruction, but we don't
247574cb248SPaul Mackerras 	 * generate an event.
248e3e94084SK.Prasad 	 */
249540e07c6SMichael Neuling 	info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
2509422de3eSMichael Neuling 	if (!((bp->attr.bp_addr <= dar) &&
2519422de3eSMichael Neuling 	      (dar - bp->attr.bp_addr < bp->attr.bp_len)))
2529422de3eSMichael Neuling 		info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
253e3e94084SK.Prasad 
2544ad8622dSChristophe Leroy #ifndef CONFIG_PPC_8xx
2555aae8a53SK.Prasad 	/* Do not emulate user-space instructions, instead single-step them */
2565aae8a53SK.Prasad 	if (user_mode(regs)) {
2576d9c00c6SMichael Neuling 		current->thread.last_hit_ubp = bp;
2585aae8a53SK.Prasad 		regs->msr |= MSR_SE;
2595aae8a53SK.Prasad 		goto out;
2605aae8a53SK.Prasad 	}
2615aae8a53SK.Prasad 
2625aae8a53SK.Prasad 	stepped = 0;
2635aae8a53SK.Prasad 	instr = 0;
2645aae8a53SK.Prasad 	if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
2655aae8a53SK.Prasad 		stepped = emulate_step(regs, instr);
2665aae8a53SK.Prasad 
2675aae8a53SK.Prasad 	/*
2685aae8a53SK.Prasad 	 * emulate_step() could not execute it. We've failed in reliably
2695aae8a53SK.Prasad 	 * handling the hw-breakpoint. Unregister it and throw a warning
2705aae8a53SK.Prasad 	 * message to let the user know about it.
2715aae8a53SK.Prasad 	 */
2725aae8a53SK.Prasad 	if (!stepped) {
2735aae8a53SK.Prasad 		WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
2745aae8a53SK.Prasad 			"0x%lx will be disabled.", info->address);
2755aab90ceSJiri Olsa 		perf_event_disable_inatomic(bp);
2765aae8a53SK.Prasad 		goto out;
2775aae8a53SK.Prasad 	}
2784ad8622dSChristophe Leroy #endif
2795aae8a53SK.Prasad 	/*
2805aae8a53SK.Prasad 	 * As a policy, the callback is invoked in a 'trigger-after-execute'
2815aae8a53SK.Prasad 	 * fashion
2825aae8a53SK.Prasad 	 */
2839422de3eSMichael Neuling 	if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
2845aae8a53SK.Prasad 		perf_bp_event(bp, regs);
2855aae8a53SK.Prasad 
28621f58507SPaul Gortmaker 	__set_breakpoint(info);
2875aae8a53SK.Prasad out:
2885aae8a53SK.Prasad 	rcu_read_unlock();
2895aae8a53SK.Prasad 	return rc;
2905aae8a53SK.Prasad }
29103465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler);
2925aae8a53SK.Prasad 
2935aae8a53SK.Prasad /*
2945aae8a53SK.Prasad  * Handle single-step exceptions following a DABR hit.
2955aae8a53SK.Prasad  */
29603465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args)
2975aae8a53SK.Prasad {
2985aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
2995aae8a53SK.Prasad 	struct perf_event *bp = NULL;
3003f4693eeSMichael Neuling 	struct arch_hw_breakpoint *info;
3015aae8a53SK.Prasad 
3025aae8a53SK.Prasad 	bp = current->thread.last_hit_ubp;
3035aae8a53SK.Prasad 	/*
3045aae8a53SK.Prasad 	 * Check if we are single-stepping as a result of a
3055aae8a53SK.Prasad 	 * previous HW Breakpoint exception
3065aae8a53SK.Prasad 	 */
3075aae8a53SK.Prasad 	if (!bp)
3085aae8a53SK.Prasad 		return NOTIFY_DONE;
3095aae8a53SK.Prasad 
3103f4693eeSMichael Neuling 	info = counter_arch_bp(bp);
3115aae8a53SK.Prasad 
3125aae8a53SK.Prasad 	/*
3135aae8a53SK.Prasad 	 * We shall invoke the user-defined callback function in the single
3145aae8a53SK.Prasad 	 * stepping handler to confirm to 'trigger-after-execute' semantics
3155aae8a53SK.Prasad 	 */
3169422de3eSMichael Neuling 	if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
3175aae8a53SK.Prasad 		perf_bp_event(bp, regs);
3185aae8a53SK.Prasad 
31921f58507SPaul Gortmaker 	__set_breakpoint(info);
3205aae8a53SK.Prasad 	current->thread.last_hit_ubp = NULL;
32176b0f133SPaul Mackerras 
32276b0f133SPaul Mackerras 	/*
32376b0f133SPaul Mackerras 	 * If the process was being single-stepped by ptrace, let the
32476b0f133SPaul Mackerras 	 * other single-step actions occur (e.g. generate SIGTRAP).
32576b0f133SPaul Mackerras 	 */
32676b0f133SPaul Mackerras 	if (test_thread_flag(TIF_SINGLESTEP))
32776b0f133SPaul Mackerras 		return NOTIFY_DONE;
32876b0f133SPaul Mackerras 
3295aae8a53SK.Prasad 	return NOTIFY_STOP;
3305aae8a53SK.Prasad }
33103465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction);
3325aae8a53SK.Prasad 
3335aae8a53SK.Prasad /*
3345aae8a53SK.Prasad  * Handle debug exception notifications.
3355aae8a53SK.Prasad  */
33603465f89SNicholas Piggin int hw_breakpoint_exceptions_notify(
3375aae8a53SK.Prasad 		struct notifier_block *unused, unsigned long val, void *data)
3385aae8a53SK.Prasad {
3395aae8a53SK.Prasad 	int ret = NOTIFY_DONE;
3405aae8a53SK.Prasad 
3415aae8a53SK.Prasad 	switch (val) {
3425aae8a53SK.Prasad 	case DIE_DABR_MATCH:
3435aae8a53SK.Prasad 		ret = hw_breakpoint_handler(data);
3445aae8a53SK.Prasad 		break;
3455aae8a53SK.Prasad 	case DIE_SSTEP:
3465aae8a53SK.Prasad 		ret = single_step_dabr_instruction(data);
3475aae8a53SK.Prasad 		break;
3485aae8a53SK.Prasad 	}
3495aae8a53SK.Prasad 
3505aae8a53SK.Prasad 	return ret;
3515aae8a53SK.Prasad }
35203465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
3535aae8a53SK.Prasad 
3545aae8a53SK.Prasad /*
3555aae8a53SK.Prasad  * Release the user breakpoints used by ptrace
3565aae8a53SK.Prasad  */
3575aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
3585aae8a53SK.Prasad {
3595aae8a53SK.Prasad 	struct thread_struct *t = &tsk->thread;
3605aae8a53SK.Prasad 
3615aae8a53SK.Prasad 	unregister_hw_breakpoint(t->ptrace_bps[0]);
3625aae8a53SK.Prasad 	t->ptrace_bps[0] = NULL;
3635aae8a53SK.Prasad }
3645aae8a53SK.Prasad 
3655aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp)
3665aae8a53SK.Prasad {
3675aae8a53SK.Prasad 	/* TODO */
3685aae8a53SK.Prasad }
369c1fe190cSMichael Neuling 
370c1fe190cSMichael Neuling bool dawr_force_enable;
371c1fe190cSMichael Neuling EXPORT_SYMBOL_GPL(dawr_force_enable);
372c1fe190cSMichael Neuling 
373c1fe190cSMichael Neuling static ssize_t dawr_write_file_bool(struct file *file,
374c1fe190cSMichael Neuling 				    const char __user *user_buf,
375c1fe190cSMichael Neuling 				    size_t count, loff_t *ppos)
376c1fe190cSMichael Neuling {
377c1fe190cSMichael Neuling 	struct arch_hw_breakpoint null_brk = {0, 0, 0};
378c1fe190cSMichael Neuling 	size_t rc;
379c1fe190cSMichael Neuling 
380c1fe190cSMichael Neuling 	/* Send error to user if they hypervisor won't allow us to write DAWR */
381c1fe190cSMichael Neuling 	if ((!dawr_force_enable) &&
382c1fe190cSMichael Neuling 	    (firmware_has_feature(FW_FEATURE_LPAR)) &&
383c1fe190cSMichael Neuling 	    (set_dawr(&null_brk) != H_SUCCESS))
384c1fe190cSMichael Neuling 		return -1;
385c1fe190cSMichael Neuling 
386c1fe190cSMichael Neuling 	rc = debugfs_write_file_bool(file, user_buf, count, ppos);
387c1fe190cSMichael Neuling 	if (rc)
388c1fe190cSMichael Neuling 		return rc;
389c1fe190cSMichael Neuling 
390c1fe190cSMichael Neuling 	/* If we are clearing, make sure all CPUs have the DAWR cleared */
391c1fe190cSMichael Neuling 	if (!dawr_force_enable)
392c1fe190cSMichael Neuling 		smp_call_function((smp_call_func_t)set_dawr, &null_brk, 0);
393c1fe190cSMichael Neuling 
394c1fe190cSMichael Neuling 	return rc;
395c1fe190cSMichael Neuling }
396c1fe190cSMichael Neuling 
397c1fe190cSMichael Neuling static const struct file_operations dawr_enable_fops = {
398c1fe190cSMichael Neuling 	.read =		debugfs_read_file_bool,
399c1fe190cSMichael Neuling 	.write =	dawr_write_file_bool,
400c1fe190cSMichael Neuling 	.open =		simple_open,
401c1fe190cSMichael Neuling 	.llseek =	default_llseek,
402c1fe190cSMichael Neuling };
403c1fe190cSMichael Neuling 
404c1fe190cSMichael Neuling static int __init dawr_force_setup(void)
405c1fe190cSMichael Neuling {
406c1fe190cSMichael Neuling 	dawr_force_enable = false;
407c1fe190cSMichael Neuling 
408c1fe190cSMichael Neuling 	if (cpu_has_feature(CPU_FTR_DAWR)) {
409c1fe190cSMichael Neuling 		/* Don't setup sysfs file for user control on P8 */
410c1fe190cSMichael Neuling 		dawr_force_enable = true;
411c1fe190cSMichael Neuling 		return 0;
412c1fe190cSMichael Neuling 	}
413c1fe190cSMichael Neuling 
414c1fe190cSMichael Neuling 	if (PVR_VER(mfspr(SPRN_PVR)) == PVR_POWER9) {
415c1fe190cSMichael Neuling 		/* Turn DAWR off by default, but allow admin to turn it on */
416c1fe190cSMichael Neuling 		dawr_force_enable = false;
417c1fe190cSMichael Neuling 		debugfs_create_file_unsafe("dawr_enable_dangerous", 0600,
418c1fe190cSMichael Neuling 					   powerpc_debugfs_root,
419c1fe190cSMichael Neuling 					   &dawr_force_enable,
420c1fe190cSMichael Neuling 					   &dawr_enable_fops);
421c1fe190cSMichael Neuling 	}
422c1fe190cSMichael Neuling 	return 0;
423c1fe190cSMichael Neuling }
424c1fe190cSMichael Neuling arch_initcall(dawr_force_setup);
425