xref: /linux/arch/powerpc/kernel/hw_breakpoint.c (revision 06532a6743d83fac4b79389fc8c86c88cb4e3302)
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/module.h>
315aae8a53SK.Prasad #include <linux/sched.h>
325aae8a53SK.Prasad #include <linux/init.h>
335aae8a53SK.Prasad #include <linux/smp.h>
345aae8a53SK.Prasad 
355aae8a53SK.Prasad #include <asm/hw_breakpoint.h>
365aae8a53SK.Prasad #include <asm/processor.h>
375aae8a53SK.Prasad #include <asm/sstep.h>
385aae8a53SK.Prasad #include <asm/uaccess.h>
395aae8a53SK.Prasad 
405aae8a53SK.Prasad /*
415aae8a53SK.Prasad  * Stores the breakpoints currently in use on each breakpoint address
425aae8a53SK.Prasad  * register for every cpu
435aae8a53SK.Prasad  */
445aae8a53SK.Prasad static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
455aae8a53SK.Prasad 
465aae8a53SK.Prasad /*
475aae8a53SK.Prasad  * Install a perf counter breakpoint.
485aae8a53SK.Prasad  *
495aae8a53SK.Prasad  * We seek a free debug address register and use it for this
505aae8a53SK.Prasad  * breakpoint.
515aae8a53SK.Prasad  *
525aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
535aae8a53SK.Prasad  * and registers local to this cpu.
545aae8a53SK.Prasad  */
555aae8a53SK.Prasad int arch_install_hw_breakpoint(struct perf_event *bp)
565aae8a53SK.Prasad {
575aae8a53SK.Prasad 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
585aae8a53SK.Prasad 	struct perf_event **slot = &__get_cpu_var(bp_per_reg);
595aae8a53SK.Prasad 
605aae8a53SK.Prasad 	*slot = bp;
615aae8a53SK.Prasad 
625aae8a53SK.Prasad 	/*
635aae8a53SK.Prasad 	 * Do not install DABR values if the instruction must be single-stepped.
645aae8a53SK.Prasad 	 * If so, DABR will be populated in single_step_dabr_instruction().
655aae8a53SK.Prasad 	 */
665aae8a53SK.Prasad 	if (current->thread.last_hit_ubp != bp)
675aae8a53SK.Prasad 		set_dabr(info->address | info->type | DABR_TRANSLATION);
685aae8a53SK.Prasad 
695aae8a53SK.Prasad 	return 0;
705aae8a53SK.Prasad }
715aae8a53SK.Prasad 
725aae8a53SK.Prasad /*
735aae8a53SK.Prasad  * Uninstall the breakpoint contained in the given counter.
745aae8a53SK.Prasad  *
755aae8a53SK.Prasad  * First we search the debug address register it uses and then we disable
765aae8a53SK.Prasad  * it.
775aae8a53SK.Prasad  *
785aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
795aae8a53SK.Prasad  * and registers local to this cpu.
805aae8a53SK.Prasad  */
815aae8a53SK.Prasad void arch_uninstall_hw_breakpoint(struct perf_event *bp)
825aae8a53SK.Prasad {
835aae8a53SK.Prasad 	struct perf_event **slot = &__get_cpu_var(bp_per_reg);
845aae8a53SK.Prasad 
855aae8a53SK.Prasad 	if (*slot != bp) {
865aae8a53SK.Prasad 		WARN_ONCE(1, "Can't find the breakpoint");
875aae8a53SK.Prasad 		return;
885aae8a53SK.Prasad 	}
895aae8a53SK.Prasad 
905aae8a53SK.Prasad 	*slot = NULL;
915aae8a53SK.Prasad 	set_dabr(0);
925aae8a53SK.Prasad }
935aae8a53SK.Prasad 
945aae8a53SK.Prasad /*
955aae8a53SK.Prasad  * Perform cleanup of arch-specific counters during unregistration
965aae8a53SK.Prasad  * of the perf-event
975aae8a53SK.Prasad  */
985aae8a53SK.Prasad void arch_unregister_hw_breakpoint(struct perf_event *bp)
995aae8a53SK.Prasad {
1005aae8a53SK.Prasad 	/*
1015aae8a53SK.Prasad 	 * If the breakpoint is unregistered between a hw_breakpoint_handler()
1025aae8a53SK.Prasad 	 * and the single_step_dabr_instruction(), then cleanup the breakpoint
1035aae8a53SK.Prasad 	 * restoration variables to prevent dangling pointers.
1045aae8a53SK.Prasad 	 */
1055aae8a53SK.Prasad 	if (bp->ctx->task)
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  */
1125aae8a53SK.Prasad int arch_check_bp_in_kernelspace(struct perf_event *bp)
1135aae8a53SK.Prasad {
1145aae8a53SK.Prasad 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
1155aae8a53SK.Prasad 
1165aae8a53SK.Prasad 	return is_kernel_addr(info->address);
1175aae8a53SK.Prasad }
1185aae8a53SK.Prasad 
1195aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type)
1205aae8a53SK.Prasad {
1215aae8a53SK.Prasad 	switch (type) {
1225aae8a53SK.Prasad 	case DABR_DATA_READ:
1235aae8a53SK.Prasad 		*gen_bp_type = HW_BREAKPOINT_R;
1245aae8a53SK.Prasad 		break;
1255aae8a53SK.Prasad 	case DABR_DATA_WRITE:
1265aae8a53SK.Prasad 		*gen_bp_type = HW_BREAKPOINT_W;
1275aae8a53SK.Prasad 		break;
1285aae8a53SK.Prasad 	case (DABR_DATA_WRITE | DABR_DATA_READ):
1295aae8a53SK.Prasad 		*gen_bp_type = (HW_BREAKPOINT_W | HW_BREAKPOINT_R);
1305aae8a53SK.Prasad 		break;
1315aae8a53SK.Prasad 	default:
1325aae8a53SK.Prasad 		return -EINVAL;
1335aae8a53SK.Prasad 	}
1345aae8a53SK.Prasad 	return 0;
1355aae8a53SK.Prasad }
1365aae8a53SK.Prasad 
1375aae8a53SK.Prasad /*
1385aae8a53SK.Prasad  * Validate the arch-specific HW Breakpoint register settings
1395aae8a53SK.Prasad  */
1405aae8a53SK.Prasad int arch_validate_hwbkpt_settings(struct perf_event *bp)
1415aae8a53SK.Prasad {
1425aae8a53SK.Prasad 	int ret = -EINVAL;
1435aae8a53SK.Prasad 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
1445aae8a53SK.Prasad 
1455aae8a53SK.Prasad 	if (!bp)
1465aae8a53SK.Prasad 		return ret;
1475aae8a53SK.Prasad 
1485aae8a53SK.Prasad 	switch (bp->attr.bp_type) {
1495aae8a53SK.Prasad 	case HW_BREAKPOINT_R:
1505aae8a53SK.Prasad 		info->type = DABR_DATA_READ;
1515aae8a53SK.Prasad 		break;
1525aae8a53SK.Prasad 	case HW_BREAKPOINT_W:
1535aae8a53SK.Prasad 		info->type = DABR_DATA_WRITE;
1545aae8a53SK.Prasad 		break;
1555aae8a53SK.Prasad 	case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
1565aae8a53SK.Prasad 		info->type = (DABR_DATA_READ | DABR_DATA_WRITE);
1575aae8a53SK.Prasad 		break;
1585aae8a53SK.Prasad 	default:
1595aae8a53SK.Prasad 		return ret;
1605aae8a53SK.Prasad 	}
1615aae8a53SK.Prasad 
1625aae8a53SK.Prasad 	info->address = bp->attr.bp_addr;
1635aae8a53SK.Prasad 	info->len = bp->attr.bp_len;
1645aae8a53SK.Prasad 
1655aae8a53SK.Prasad 	/*
1665aae8a53SK.Prasad 	 * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8)
1675aae8a53SK.Prasad 	 * and breakpoint addresses are aligned to nearest double-word
1685aae8a53SK.Prasad 	 * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the
1695aae8a53SK.Prasad 	 * 'symbolsize' should satisfy the check below.
1705aae8a53SK.Prasad 	 */
1715aae8a53SK.Prasad 	if (info->len >
1725aae8a53SK.Prasad 	    (HW_BREAKPOINT_LEN - (info->address & HW_BREAKPOINT_ALIGN)))
1735aae8a53SK.Prasad 		return -EINVAL;
1745aae8a53SK.Prasad 	return 0;
1755aae8a53SK.Prasad }
1765aae8a53SK.Prasad 
1775aae8a53SK.Prasad /*
178*06532a67SK.Prasad  * Restores the breakpoint on the debug registers.
179*06532a67SK.Prasad  * Invoke this function if it is known that the execution context is
180*06532a67SK.Prasad  * about to change to cause loss of MSR_SE settings.
181*06532a67SK.Prasad  */
182*06532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
183*06532a67SK.Prasad {
184*06532a67SK.Prasad 	struct arch_hw_breakpoint *info;
185*06532a67SK.Prasad 
186*06532a67SK.Prasad 	if (likely(!tsk->thread.last_hit_ubp))
187*06532a67SK.Prasad 		return;
188*06532a67SK.Prasad 
189*06532a67SK.Prasad 	info = counter_arch_bp(tsk->thread.last_hit_ubp);
190*06532a67SK.Prasad 	regs->msr &= ~MSR_SE;
191*06532a67SK.Prasad 	set_dabr(info->address | info->type | DABR_TRANSLATION);
192*06532a67SK.Prasad 	tsk->thread.last_hit_ubp = NULL;
193*06532a67SK.Prasad }
194*06532a67SK.Prasad 
195*06532a67SK.Prasad /*
1965aae8a53SK.Prasad  * Handle debug exception notifications.
1975aae8a53SK.Prasad  */
1985aae8a53SK.Prasad int __kprobes hw_breakpoint_handler(struct die_args *args)
1995aae8a53SK.Prasad {
2005aae8a53SK.Prasad 	bool is_ptrace_bp = false;
2015aae8a53SK.Prasad 	int rc = NOTIFY_STOP;
2025aae8a53SK.Prasad 	struct perf_event *bp;
2035aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
2045aae8a53SK.Prasad 	int stepped = 1;
2055aae8a53SK.Prasad 	struct arch_hw_breakpoint *info;
2065aae8a53SK.Prasad 	unsigned int instr;
2075aae8a53SK.Prasad 
2085aae8a53SK.Prasad 	/* Disable breakpoints during exception handling */
2095aae8a53SK.Prasad 	set_dabr(0);
2105aae8a53SK.Prasad 	/*
2115aae8a53SK.Prasad 	 * The counter may be concurrently released but that can only
2125aae8a53SK.Prasad 	 * occur from a call_rcu() path. We can then safely fetch
2135aae8a53SK.Prasad 	 * the breakpoint, use its callback, touch its counter
2145aae8a53SK.Prasad 	 * while we are in an rcu_read_lock() path.
2155aae8a53SK.Prasad 	 */
2165aae8a53SK.Prasad 	rcu_read_lock();
2175aae8a53SK.Prasad 
2185aae8a53SK.Prasad 	bp = __get_cpu_var(bp_per_reg);
2195aae8a53SK.Prasad 	if (!bp)
2205aae8a53SK.Prasad 		goto out;
2215aae8a53SK.Prasad 	info = counter_arch_bp(bp);
2225aae8a53SK.Prasad 	is_ptrace_bp = (bp->overflow_handler == ptrace_triggered) ?
2235aae8a53SK.Prasad 			true : false;
2245aae8a53SK.Prasad 
2255aae8a53SK.Prasad 	/*
2265aae8a53SK.Prasad 	 * Return early after invoking user-callback function without restoring
2275aae8a53SK.Prasad 	 * DABR if the breakpoint is from ptrace which always operates in
2285aae8a53SK.Prasad 	 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
2295aae8a53SK.Prasad 	 * generated in do_dabr().
2305aae8a53SK.Prasad 	 */
2315aae8a53SK.Prasad 	if (is_ptrace_bp) {
2325aae8a53SK.Prasad 		perf_bp_event(bp, regs);
2335aae8a53SK.Prasad 		rc = NOTIFY_DONE;
2345aae8a53SK.Prasad 		goto out;
2355aae8a53SK.Prasad 	}
2365aae8a53SK.Prasad 
2375aae8a53SK.Prasad 	/* Do not emulate user-space instructions, instead single-step them */
2385aae8a53SK.Prasad 	if (user_mode(regs)) {
2395aae8a53SK.Prasad 		bp->ctx->task->thread.last_hit_ubp = bp;
2405aae8a53SK.Prasad 		regs->msr |= MSR_SE;
2415aae8a53SK.Prasad 		goto out;
2425aae8a53SK.Prasad 	}
2435aae8a53SK.Prasad 
2445aae8a53SK.Prasad 	stepped = 0;
2455aae8a53SK.Prasad 	instr = 0;
2465aae8a53SK.Prasad 	if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
2475aae8a53SK.Prasad 		stepped = emulate_step(regs, instr);
2485aae8a53SK.Prasad 
2495aae8a53SK.Prasad 	/*
2505aae8a53SK.Prasad 	 * emulate_step() could not execute it. We've failed in reliably
2515aae8a53SK.Prasad 	 * handling the hw-breakpoint. Unregister it and throw a warning
2525aae8a53SK.Prasad 	 * message to let the user know about it.
2535aae8a53SK.Prasad 	 */
2545aae8a53SK.Prasad 	if (!stepped) {
2555aae8a53SK.Prasad 		WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
2565aae8a53SK.Prasad 			"0x%lx will be disabled.", info->address);
2575aae8a53SK.Prasad 		perf_event_disable(bp);
2585aae8a53SK.Prasad 		goto out;
2595aae8a53SK.Prasad 	}
2605aae8a53SK.Prasad 	/*
2615aae8a53SK.Prasad 	 * As a policy, the callback is invoked in a 'trigger-after-execute'
2625aae8a53SK.Prasad 	 * fashion
2635aae8a53SK.Prasad 	 */
2645aae8a53SK.Prasad 	perf_bp_event(bp, regs);
2655aae8a53SK.Prasad 
2665aae8a53SK.Prasad 	set_dabr(info->address | info->type | DABR_TRANSLATION);
2675aae8a53SK.Prasad out:
2685aae8a53SK.Prasad 	rcu_read_unlock();
2695aae8a53SK.Prasad 	return rc;
2705aae8a53SK.Prasad }
2715aae8a53SK.Prasad 
2725aae8a53SK.Prasad /*
2735aae8a53SK.Prasad  * Handle single-step exceptions following a DABR hit.
2745aae8a53SK.Prasad  */
2755aae8a53SK.Prasad int __kprobes single_step_dabr_instruction(struct die_args *args)
2765aae8a53SK.Prasad {
2775aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
2785aae8a53SK.Prasad 	struct perf_event *bp = NULL;
2795aae8a53SK.Prasad 	struct arch_hw_breakpoint *bp_info;
2805aae8a53SK.Prasad 
2815aae8a53SK.Prasad 	bp = current->thread.last_hit_ubp;
2825aae8a53SK.Prasad 	/*
2835aae8a53SK.Prasad 	 * Check if we are single-stepping as a result of a
2845aae8a53SK.Prasad 	 * previous HW Breakpoint exception
2855aae8a53SK.Prasad 	 */
2865aae8a53SK.Prasad 	if (!bp)
2875aae8a53SK.Prasad 		return NOTIFY_DONE;
2885aae8a53SK.Prasad 
2895aae8a53SK.Prasad 	bp_info = counter_arch_bp(bp);
2905aae8a53SK.Prasad 
2915aae8a53SK.Prasad 	/*
2925aae8a53SK.Prasad 	 * We shall invoke the user-defined callback function in the single
2935aae8a53SK.Prasad 	 * stepping handler to confirm to 'trigger-after-execute' semantics
2945aae8a53SK.Prasad 	 */
2955aae8a53SK.Prasad 	perf_bp_event(bp, regs);
2965aae8a53SK.Prasad 
2975aae8a53SK.Prasad 	/*
2985aae8a53SK.Prasad 	 * Do not disable MSR_SE if the process was already in
2995aae8a53SK.Prasad 	 * single-stepping mode.
3005aae8a53SK.Prasad 	 */
3015aae8a53SK.Prasad 	if (!test_thread_flag(TIF_SINGLESTEP))
3025aae8a53SK.Prasad 		regs->msr &= ~MSR_SE;
3035aae8a53SK.Prasad 
3045aae8a53SK.Prasad 	set_dabr(bp_info->address | bp_info->type | DABR_TRANSLATION);
3055aae8a53SK.Prasad 	current->thread.last_hit_ubp = NULL;
3065aae8a53SK.Prasad 	return NOTIFY_STOP;
3075aae8a53SK.Prasad }
3085aae8a53SK.Prasad 
3095aae8a53SK.Prasad /*
3105aae8a53SK.Prasad  * Handle debug exception notifications.
3115aae8a53SK.Prasad  */
3125aae8a53SK.Prasad int __kprobes hw_breakpoint_exceptions_notify(
3135aae8a53SK.Prasad 		struct notifier_block *unused, unsigned long val, void *data)
3145aae8a53SK.Prasad {
3155aae8a53SK.Prasad 	int ret = NOTIFY_DONE;
3165aae8a53SK.Prasad 
3175aae8a53SK.Prasad 	switch (val) {
3185aae8a53SK.Prasad 	case DIE_DABR_MATCH:
3195aae8a53SK.Prasad 		ret = hw_breakpoint_handler(data);
3205aae8a53SK.Prasad 		break;
3215aae8a53SK.Prasad 	case DIE_SSTEP:
3225aae8a53SK.Prasad 		ret = single_step_dabr_instruction(data);
3235aae8a53SK.Prasad 		break;
3245aae8a53SK.Prasad 	}
3255aae8a53SK.Prasad 
3265aae8a53SK.Prasad 	return ret;
3275aae8a53SK.Prasad }
3285aae8a53SK.Prasad 
3295aae8a53SK.Prasad /*
3305aae8a53SK.Prasad  * Release the user breakpoints used by ptrace
3315aae8a53SK.Prasad  */
3325aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
3335aae8a53SK.Prasad {
3345aae8a53SK.Prasad 	struct thread_struct *t = &tsk->thread;
3355aae8a53SK.Prasad 
3365aae8a53SK.Prasad 	unregister_hw_breakpoint(t->ptrace_bps[0]);
3375aae8a53SK.Prasad 	t->ptrace_bps[0] = NULL;
3385aae8a53SK.Prasad }
3395aae8a53SK.Prasad 
3405aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp)
3415aae8a53SK.Prasad {
3425aae8a53SK.Prasad 	/* TODO */
3435aae8a53SK.Prasad }
344