xref: /linux/arch/powerpc/kernel/hw_breakpoint.c (revision 3f31e49dc4588d396023028791e36c23235e1334)
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>
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>
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 static bool single_step_pending(void)
4774c68810SRavi Bangoria {
4874c68810SRavi Bangoria 	int i;
4974c68810SRavi Bangoria 
5074c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
5174c68810SRavi Bangoria 		if (current->thread.last_hit_ubp[i])
5274c68810SRavi Bangoria 			return true;
5374c68810SRavi Bangoria 	}
5474c68810SRavi Bangoria 	return false;
5574c68810SRavi Bangoria }
5674c68810SRavi Bangoria 
57d09ec738SPaul Mackerras /*
585aae8a53SK.Prasad  * Install a perf counter breakpoint.
595aae8a53SK.Prasad  *
605aae8a53SK.Prasad  * We seek a free debug address register and use it for this
615aae8a53SK.Prasad  * breakpoint.
625aae8a53SK.Prasad  *
635aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
645aae8a53SK.Prasad  * and registers local to this cpu.
655aae8a53SK.Prasad  */
665aae8a53SK.Prasad int arch_install_hw_breakpoint(struct perf_event *bp)
675aae8a53SK.Prasad {
685aae8a53SK.Prasad 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
6974c68810SRavi Bangoria 	struct perf_event **slot;
7074c68810SRavi Bangoria 	int i;
715aae8a53SK.Prasad 
7274c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
7374c68810SRavi Bangoria 		slot = this_cpu_ptr(&bp_per_reg[i]);
7474c68810SRavi Bangoria 		if (!*slot) {
755aae8a53SK.Prasad 			*slot = bp;
7674c68810SRavi Bangoria 			break;
7774c68810SRavi Bangoria 		}
7874c68810SRavi Bangoria 	}
7974c68810SRavi Bangoria 
8074c68810SRavi Bangoria 	if (WARN_ONCE(i == nr_wp_slots(), "Can't find any breakpoint slot"))
8174c68810SRavi Bangoria 		return -EBUSY;
825aae8a53SK.Prasad 
835aae8a53SK.Prasad 	/*
845aae8a53SK.Prasad 	 * Do not install DABR values if the instruction must be single-stepped.
855aae8a53SK.Prasad 	 * If so, DABR will be populated in single_step_dabr_instruction().
865aae8a53SK.Prasad 	 */
8774c68810SRavi Bangoria 	if (!single_step_pending())
8874c68810SRavi Bangoria 		__set_breakpoint(i, info);
895aae8a53SK.Prasad 
905aae8a53SK.Prasad 	return 0;
915aae8a53SK.Prasad }
925aae8a53SK.Prasad 
935aae8a53SK.Prasad /*
945aae8a53SK.Prasad  * Uninstall the breakpoint contained in the given counter.
955aae8a53SK.Prasad  *
965aae8a53SK.Prasad  * First we search the debug address register it uses and then we disable
975aae8a53SK.Prasad  * it.
985aae8a53SK.Prasad  *
995aae8a53SK.Prasad  * Atomic: we hold the counter->ctx->lock and we only handle variables
1005aae8a53SK.Prasad  * and registers local to this cpu.
1015aae8a53SK.Prasad  */
1025aae8a53SK.Prasad void arch_uninstall_hw_breakpoint(struct perf_event *bp)
1035aae8a53SK.Prasad {
10474c68810SRavi Bangoria 	struct arch_hw_breakpoint null_brk = {0};
10574c68810SRavi Bangoria 	struct perf_event **slot;
10674c68810SRavi Bangoria 	int i;
1075aae8a53SK.Prasad 
10874c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
10974c68810SRavi Bangoria 		slot = this_cpu_ptr(&bp_per_reg[i]);
11074c68810SRavi Bangoria 		if (*slot == bp) {
11174c68810SRavi Bangoria 			*slot = NULL;
11274c68810SRavi Bangoria 			break;
11374c68810SRavi Bangoria 		}
1145aae8a53SK.Prasad 	}
1155aae8a53SK.Prasad 
11674c68810SRavi Bangoria 	if (WARN_ONCE(i == nr_wp_slots(), "Can't find any breakpoint slot"))
11774c68810SRavi Bangoria 		return;
11874c68810SRavi Bangoria 
11974c68810SRavi Bangoria 	__set_breakpoint(i, &null_brk);
1205aae8a53SK.Prasad }
1215aae8a53SK.Prasad 
122c9e82aebSRavi Bangoria static bool is_ptrace_bp(struct perf_event *bp)
123c9e82aebSRavi Bangoria {
124c9e82aebSRavi Bangoria 	return bp->overflow_handler == ptrace_triggered;
125c9e82aebSRavi Bangoria }
126c9e82aebSRavi Bangoria 
12729da4f91SRavi Bangoria struct breakpoint {
12829da4f91SRavi Bangoria 	struct list_head list;
12929da4f91SRavi Bangoria 	struct perf_event *bp;
13029da4f91SRavi Bangoria 	bool ptrace_bp;
13129da4f91SRavi Bangoria };
13229da4f91SRavi Bangoria 
13329da4f91SRavi Bangoria static DEFINE_PER_CPU(struct breakpoint *, cpu_bps[HBP_NUM_MAX]);
13429da4f91SRavi Bangoria static LIST_HEAD(task_bps);
13529da4f91SRavi Bangoria 
13629da4f91SRavi Bangoria static struct breakpoint *alloc_breakpoint(struct perf_event *bp)
13729da4f91SRavi Bangoria {
13829da4f91SRavi Bangoria 	struct breakpoint *tmp;
13929da4f91SRavi Bangoria 
14029da4f91SRavi Bangoria 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
14129da4f91SRavi Bangoria 	if (!tmp)
14229da4f91SRavi Bangoria 		return ERR_PTR(-ENOMEM);
14329da4f91SRavi Bangoria 	tmp->bp = bp;
14429da4f91SRavi Bangoria 	tmp->ptrace_bp = is_ptrace_bp(bp);
14529da4f91SRavi Bangoria 	return tmp;
14629da4f91SRavi Bangoria }
14729da4f91SRavi Bangoria 
14829da4f91SRavi Bangoria static bool bp_addr_range_overlap(struct perf_event *bp1, struct perf_event *bp2)
14929da4f91SRavi Bangoria {
15029da4f91SRavi Bangoria 	__u64 bp1_saddr, bp1_eaddr, bp2_saddr, bp2_eaddr;
15129da4f91SRavi Bangoria 
15229da4f91SRavi Bangoria 	bp1_saddr = ALIGN_DOWN(bp1->attr.bp_addr, HW_BREAKPOINT_SIZE);
15329da4f91SRavi Bangoria 	bp1_eaddr = ALIGN(bp1->attr.bp_addr + bp1->attr.bp_len, HW_BREAKPOINT_SIZE);
15429da4f91SRavi Bangoria 	bp2_saddr = ALIGN_DOWN(bp2->attr.bp_addr, HW_BREAKPOINT_SIZE);
15529da4f91SRavi Bangoria 	bp2_eaddr = ALIGN(bp2->attr.bp_addr + bp2->attr.bp_len, HW_BREAKPOINT_SIZE);
15629da4f91SRavi Bangoria 
15729da4f91SRavi Bangoria 	return (bp1_saddr < bp2_eaddr && bp1_eaddr > bp2_saddr);
15829da4f91SRavi Bangoria }
15929da4f91SRavi Bangoria 
16029da4f91SRavi Bangoria static bool alternate_infra_bp(struct breakpoint *b, struct perf_event *bp)
16129da4f91SRavi Bangoria {
16229da4f91SRavi Bangoria 	return is_ptrace_bp(bp) ? !b->ptrace_bp : b->ptrace_bp;
16329da4f91SRavi Bangoria }
16429da4f91SRavi Bangoria 
16529da4f91SRavi Bangoria static bool can_co_exist(struct breakpoint *b, struct perf_event *bp)
16629da4f91SRavi Bangoria {
16729da4f91SRavi Bangoria 	return !(alternate_infra_bp(b, bp) && bp_addr_range_overlap(b->bp, bp));
16829da4f91SRavi Bangoria }
16929da4f91SRavi Bangoria 
17029da4f91SRavi Bangoria static int task_bps_add(struct perf_event *bp)
17129da4f91SRavi Bangoria {
17229da4f91SRavi Bangoria 	struct breakpoint *tmp;
17329da4f91SRavi Bangoria 
17429da4f91SRavi Bangoria 	tmp = alloc_breakpoint(bp);
17529da4f91SRavi Bangoria 	if (IS_ERR(tmp))
17629da4f91SRavi Bangoria 		return PTR_ERR(tmp);
17729da4f91SRavi Bangoria 
17829da4f91SRavi Bangoria 	list_add(&tmp->list, &task_bps);
17929da4f91SRavi Bangoria 	return 0;
18029da4f91SRavi Bangoria }
18129da4f91SRavi Bangoria 
18229da4f91SRavi Bangoria static void task_bps_remove(struct perf_event *bp)
18329da4f91SRavi Bangoria {
18429da4f91SRavi Bangoria 	struct list_head *pos, *q;
18529da4f91SRavi Bangoria 
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 	}
19529da4f91SRavi Bangoria }
19629da4f91SRavi Bangoria 
19729da4f91SRavi Bangoria /*
19829da4f91SRavi Bangoria  * If any task has breakpoint from alternate infrastructure,
19929da4f91SRavi Bangoria  * return true. Otherwise return false.
20029da4f91SRavi Bangoria  */
20129da4f91SRavi Bangoria static bool all_task_bps_check(struct perf_event *bp)
20229da4f91SRavi Bangoria {
20329da4f91SRavi Bangoria 	struct breakpoint *tmp;
20429da4f91SRavi Bangoria 
20529da4f91SRavi Bangoria 	list_for_each_entry(tmp, &task_bps, list) {
20629da4f91SRavi Bangoria 		if (!can_co_exist(tmp, bp))
20729da4f91SRavi Bangoria 			return true;
20829da4f91SRavi Bangoria 	}
20929da4f91SRavi Bangoria 	return false;
21029da4f91SRavi Bangoria }
21129da4f91SRavi Bangoria 
21229da4f91SRavi Bangoria /*
21329da4f91SRavi Bangoria  * If same task has breakpoint from alternate infrastructure,
21429da4f91SRavi Bangoria  * return true. Otherwise return false.
21529da4f91SRavi Bangoria  */
21629da4f91SRavi Bangoria static bool same_task_bps_check(struct perf_event *bp)
21729da4f91SRavi Bangoria {
21829da4f91SRavi Bangoria 	struct breakpoint *tmp;
21929da4f91SRavi Bangoria 
22029da4f91SRavi Bangoria 	list_for_each_entry(tmp, &task_bps, list) {
22129da4f91SRavi Bangoria 		if (tmp->bp->hw.target == bp->hw.target &&
22229da4f91SRavi Bangoria 		    !can_co_exist(tmp, bp))
22329da4f91SRavi Bangoria 			return true;
22429da4f91SRavi Bangoria 	}
22529da4f91SRavi Bangoria 	return false;
22629da4f91SRavi Bangoria }
22729da4f91SRavi Bangoria 
22829da4f91SRavi Bangoria static int cpu_bps_add(struct perf_event *bp)
22929da4f91SRavi Bangoria {
23029da4f91SRavi Bangoria 	struct breakpoint **cpu_bp;
23129da4f91SRavi Bangoria 	struct breakpoint *tmp;
23229da4f91SRavi Bangoria 	int i = 0;
23329da4f91SRavi Bangoria 
23429da4f91SRavi Bangoria 	tmp = alloc_breakpoint(bp);
23529da4f91SRavi Bangoria 	if (IS_ERR(tmp))
23629da4f91SRavi Bangoria 		return PTR_ERR(tmp);
23729da4f91SRavi Bangoria 
23829da4f91SRavi Bangoria 	cpu_bp = per_cpu_ptr(cpu_bps, bp->cpu);
23929da4f91SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
24029da4f91SRavi Bangoria 		if (!cpu_bp[i]) {
24129da4f91SRavi Bangoria 			cpu_bp[i] = tmp;
24229da4f91SRavi Bangoria 			break;
24329da4f91SRavi Bangoria 		}
24429da4f91SRavi Bangoria 	}
24529da4f91SRavi Bangoria 	return 0;
24629da4f91SRavi Bangoria }
24729da4f91SRavi Bangoria 
24829da4f91SRavi Bangoria static void cpu_bps_remove(struct perf_event *bp)
24929da4f91SRavi Bangoria {
25029da4f91SRavi Bangoria 	struct breakpoint **cpu_bp;
25129da4f91SRavi Bangoria 	int i = 0;
25229da4f91SRavi Bangoria 
25329da4f91SRavi Bangoria 	cpu_bp = per_cpu_ptr(cpu_bps, bp->cpu);
25429da4f91SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
25529da4f91SRavi Bangoria 		if (!cpu_bp[i])
25629da4f91SRavi Bangoria 			continue;
25729da4f91SRavi Bangoria 
25829da4f91SRavi Bangoria 		if (cpu_bp[i]->bp == bp) {
25929da4f91SRavi Bangoria 			kfree(cpu_bp[i]);
26029da4f91SRavi Bangoria 			cpu_bp[i] = NULL;
26129da4f91SRavi Bangoria 			break;
26229da4f91SRavi Bangoria 		}
26329da4f91SRavi Bangoria 	}
26429da4f91SRavi Bangoria }
26529da4f91SRavi Bangoria 
26629da4f91SRavi Bangoria static bool cpu_bps_check(int cpu, struct perf_event *bp)
26729da4f91SRavi Bangoria {
26829da4f91SRavi Bangoria 	struct breakpoint **cpu_bp;
26929da4f91SRavi Bangoria 	int i;
27029da4f91SRavi Bangoria 
27129da4f91SRavi Bangoria 	cpu_bp = per_cpu_ptr(cpu_bps, cpu);
27229da4f91SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
27329da4f91SRavi Bangoria 		if (cpu_bp[i] && !can_co_exist(cpu_bp[i], bp))
27429da4f91SRavi Bangoria 			return true;
27529da4f91SRavi Bangoria 	}
27629da4f91SRavi Bangoria 	return false;
27729da4f91SRavi Bangoria }
27829da4f91SRavi Bangoria 
27929da4f91SRavi Bangoria static bool all_cpu_bps_check(struct perf_event *bp)
28029da4f91SRavi Bangoria {
28129da4f91SRavi Bangoria 	int cpu;
28229da4f91SRavi Bangoria 
28329da4f91SRavi Bangoria 	for_each_online_cpu(cpu) {
28429da4f91SRavi Bangoria 		if (cpu_bps_check(cpu, bp))
28529da4f91SRavi Bangoria 			return true;
28629da4f91SRavi Bangoria 	}
28729da4f91SRavi Bangoria 	return false;
28829da4f91SRavi Bangoria }
28929da4f91SRavi Bangoria 
29029da4f91SRavi Bangoria /*
29129da4f91SRavi Bangoria  * We don't use any locks to serialize accesses to cpu_bps or task_bps
29229da4f91SRavi Bangoria  * because are already inside nr_bp_mutex.
29329da4f91SRavi Bangoria  */
29429da4f91SRavi Bangoria int arch_reserve_bp_slot(struct perf_event *bp)
29529da4f91SRavi Bangoria {
29629da4f91SRavi Bangoria 	int ret;
29729da4f91SRavi Bangoria 
29829da4f91SRavi Bangoria 	/* ptrace breakpoint */
29929da4f91SRavi Bangoria 	if (is_ptrace_bp(bp)) {
30029da4f91SRavi Bangoria 		if (all_cpu_bps_check(bp))
30129da4f91SRavi Bangoria 			return -ENOSPC;
30229da4f91SRavi Bangoria 
30329da4f91SRavi Bangoria 		if (same_task_bps_check(bp))
30429da4f91SRavi Bangoria 			return -ENOSPC;
30529da4f91SRavi Bangoria 
30629da4f91SRavi Bangoria 		return task_bps_add(bp);
30729da4f91SRavi Bangoria 	}
30829da4f91SRavi Bangoria 
30929da4f91SRavi Bangoria 	/* perf breakpoint */
31029da4f91SRavi Bangoria 	if (is_kernel_addr(bp->attr.bp_addr))
31129da4f91SRavi Bangoria 		return 0;
31229da4f91SRavi Bangoria 
31329da4f91SRavi Bangoria 	if (bp->hw.target && bp->cpu == -1) {
31429da4f91SRavi Bangoria 		if (same_task_bps_check(bp))
31529da4f91SRavi Bangoria 			return -ENOSPC;
31629da4f91SRavi Bangoria 
31729da4f91SRavi Bangoria 		return task_bps_add(bp);
31829da4f91SRavi Bangoria 	} else if (!bp->hw.target && bp->cpu != -1) {
31929da4f91SRavi Bangoria 		if (all_task_bps_check(bp))
32029da4f91SRavi Bangoria 			return -ENOSPC;
32129da4f91SRavi Bangoria 
32229da4f91SRavi Bangoria 		return cpu_bps_add(bp);
32329da4f91SRavi Bangoria 	}
32429da4f91SRavi Bangoria 
32529da4f91SRavi Bangoria 	if (same_task_bps_check(bp))
32629da4f91SRavi Bangoria 		return -ENOSPC;
32729da4f91SRavi Bangoria 
32829da4f91SRavi Bangoria 	ret = cpu_bps_add(bp);
32929da4f91SRavi Bangoria 	if (ret)
33029da4f91SRavi Bangoria 		return ret;
33129da4f91SRavi Bangoria 	ret = task_bps_add(bp);
33229da4f91SRavi Bangoria 	if (ret)
33329da4f91SRavi Bangoria 		cpu_bps_remove(bp);
33429da4f91SRavi Bangoria 
33529da4f91SRavi Bangoria 	return ret;
33629da4f91SRavi Bangoria }
33729da4f91SRavi Bangoria 
33829da4f91SRavi Bangoria void arch_release_bp_slot(struct perf_event *bp)
33929da4f91SRavi Bangoria {
34029da4f91SRavi Bangoria 	if (!is_kernel_addr(bp->attr.bp_addr)) {
34129da4f91SRavi Bangoria 		if (bp->hw.target)
34229da4f91SRavi Bangoria 			task_bps_remove(bp);
34329da4f91SRavi Bangoria 		if (bp->cpu != -1)
34429da4f91SRavi Bangoria 			cpu_bps_remove(bp);
34529da4f91SRavi Bangoria 	}
34629da4f91SRavi Bangoria }
34729da4f91SRavi Bangoria 
3485aae8a53SK.Prasad /*
3495aae8a53SK.Prasad  * Perform cleanup of arch-specific counters during unregistration
3505aae8a53SK.Prasad  * of the perf-event
3515aae8a53SK.Prasad  */
3525aae8a53SK.Prasad void arch_unregister_hw_breakpoint(struct perf_event *bp)
3535aae8a53SK.Prasad {
3545aae8a53SK.Prasad 	/*
3555aae8a53SK.Prasad 	 * If the breakpoint is unregistered between a hw_breakpoint_handler()
3565aae8a53SK.Prasad 	 * and the single_step_dabr_instruction(), then cleanup the breakpoint
3575aae8a53SK.Prasad 	 * restoration variables to prevent dangling pointers.
358fb822e60SRavi Bangoria 	 * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
3595aae8a53SK.Prasad 	 */
36074c68810SRavi Bangoria 	if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L)) {
36174c68810SRavi Bangoria 		int i;
36274c68810SRavi Bangoria 
36374c68810SRavi Bangoria 		for (i = 0; i < nr_wp_slots(); i++) {
36474c68810SRavi Bangoria 			if (bp->ctx->task->thread.last_hit_ubp[i] == bp)
36574c68810SRavi Bangoria 				bp->ctx->task->thread.last_hit_ubp[i] = NULL;
36674c68810SRavi Bangoria 		}
36774c68810SRavi Bangoria 	}
3685aae8a53SK.Prasad }
3695aae8a53SK.Prasad 
3705aae8a53SK.Prasad /*
3715aae8a53SK.Prasad  * Check for virtual address in kernel space.
3725aae8a53SK.Prasad  */
3738e983ff9SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
3745aae8a53SK.Prasad {
3758e983ff9SFrederic Weisbecker 	return is_kernel_addr(hw->address);
3765aae8a53SK.Prasad }
3775aae8a53SK.Prasad 
3785aae8a53SK.Prasad int arch_bp_generic_fields(int type, int *gen_bp_type)
3795aae8a53SK.Prasad {
3809422de3eSMichael Neuling 	*gen_bp_type = 0;
3819422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_READ)
3829422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_R;
3839422de3eSMichael Neuling 	if (type & HW_BRK_TYPE_WRITE)
3849422de3eSMichael Neuling 		*gen_bp_type |= HW_BREAKPOINT_W;
3859422de3eSMichael Neuling 	if (*gen_bp_type == 0)
3865aae8a53SK.Prasad 		return -EINVAL;
3875aae8a53SK.Prasad 	return 0;
3885aae8a53SK.Prasad }
3895aae8a53SK.Prasad 
3905aae8a53SK.Prasad /*
391b57aeab8SRavi Bangoria  * Watchpoint match range is always doubleword(8 bytes) aligned on
392b57aeab8SRavi Bangoria  * powerpc. If the given range is crossing doubleword boundary, we
393b57aeab8SRavi Bangoria  * need to increase the length such that next doubleword also get
394b57aeab8SRavi Bangoria  * covered. Ex,
395b57aeab8SRavi Bangoria  *
396b57aeab8SRavi Bangoria  *          address   len = 6 bytes
397b57aeab8SRavi Bangoria  *                |=========.
398b57aeab8SRavi Bangoria  *   |------------v--|------v--------|
399b57aeab8SRavi Bangoria  *   | | | | | | | | | | | | | | | | |
400b57aeab8SRavi Bangoria  *   |---------------|---------------|
401b57aeab8SRavi Bangoria  *    <---8 bytes--->
402b57aeab8SRavi Bangoria  *
403b57aeab8SRavi Bangoria  * In this case, we should configure hw as:
404e68ef121SRavi Bangoria  *   start_addr = address & ~(HW_BREAKPOINT_SIZE - 1)
405b57aeab8SRavi Bangoria  *   len = 16 bytes
406b57aeab8SRavi Bangoria  *
407e68ef121SRavi Bangoria  * @start_addr is inclusive but @end_addr is exclusive.
408b57aeab8SRavi Bangoria  */
409b57aeab8SRavi Bangoria static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw)
410b57aeab8SRavi Bangoria {
411b57aeab8SRavi Bangoria 	u16 max_len = DABR_MAX_LEN;
412b57aeab8SRavi Bangoria 	u16 hw_len;
413b57aeab8SRavi Bangoria 	unsigned long start_addr, end_addr;
414b57aeab8SRavi Bangoria 
415e68ef121SRavi Bangoria 	start_addr = ALIGN_DOWN(hw->address, HW_BREAKPOINT_SIZE);
416e68ef121SRavi Bangoria 	end_addr = ALIGN(hw->address + hw->len, HW_BREAKPOINT_SIZE);
417e68ef121SRavi Bangoria 	hw_len = end_addr - start_addr;
418b57aeab8SRavi Bangoria 
419b57aeab8SRavi Bangoria 	if (dawr_enabled()) {
420b57aeab8SRavi Bangoria 		max_len = DAWR_MAX_LEN;
421*3f31e49dSRavi Bangoria 		/* DAWR region can't cross 512 bytes boundary on p10 predecessors */
422*3f31e49dSRavi Bangoria 		if (!cpu_has_feature(CPU_FTR_ARCH_31) &&
423*3f31e49dSRavi Bangoria 		    (ALIGN_DOWN(start_addr, SZ_512) != ALIGN_DOWN(end_addr - 1, SZ_512)))
424b57aeab8SRavi Bangoria 			return -EINVAL;
42539413ae0SChristophe Leroy 	} else if (IS_ENABLED(CONFIG_PPC_8xx)) {
42639413ae0SChristophe Leroy 		/* 8xx can setup a range without limitation */
42739413ae0SChristophe Leroy 		max_len = U16_MAX;
428b57aeab8SRavi Bangoria 	}
429b57aeab8SRavi Bangoria 
430b57aeab8SRavi Bangoria 	if (hw_len > max_len)
431b57aeab8SRavi Bangoria 		return -EINVAL;
432b57aeab8SRavi Bangoria 
433b57aeab8SRavi Bangoria 	hw->hw_len = hw_len;
434b57aeab8SRavi Bangoria 	return 0;
435b57aeab8SRavi Bangoria }
436b57aeab8SRavi Bangoria 
437b57aeab8SRavi Bangoria /*
4385aae8a53SK.Prasad  * Validate the arch-specific HW Breakpoint register settings
4395aae8a53SK.Prasad  */
4405d5176baSFrederic Weisbecker int hw_breakpoint_arch_parse(struct perf_event *bp,
4415d5176baSFrederic Weisbecker 			     const struct perf_event_attr *attr,
4425d5176baSFrederic Weisbecker 			     struct arch_hw_breakpoint *hw)
4435aae8a53SK.Prasad {
444b57aeab8SRavi Bangoria 	int ret = -EINVAL;
4455aae8a53SK.Prasad 
446b57aeab8SRavi Bangoria 	if (!bp || !attr->bp_len)
4475aae8a53SK.Prasad 		return ret;
4485aae8a53SK.Prasad 
4495d5176baSFrederic Weisbecker 	hw->type = HW_BRK_TYPE_TRANSLATE;
4505d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_R)
4515d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_READ;
4525d5176baSFrederic Weisbecker 	if (attr->bp_type & HW_BREAKPOINT_W)
4535d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_WRITE;
4545d5176baSFrederic Weisbecker 	if (hw->type == HW_BRK_TYPE_TRANSLATE)
4559422de3eSMichael Neuling 		/* must set alteast read or write */
4565aae8a53SK.Prasad 		return ret;
4575d5176baSFrederic Weisbecker 	if (!attr->exclude_user)
4585d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_USER;
4595d5176baSFrederic Weisbecker 	if (!attr->exclude_kernel)
4605d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_KERNEL;
4615d5176baSFrederic Weisbecker 	if (!attr->exclude_hv)
4625d5176baSFrederic Weisbecker 		hw->type |= HW_BRK_TYPE_HYP;
4635d5176baSFrederic Weisbecker 	hw->address = attr->bp_addr;
4645d5176baSFrederic Weisbecker 	hw->len = attr->bp_len;
4655aae8a53SK.Prasad 
46685ce9a5dSMichael Neuling 	if (!ppc_breakpoint_available())
46785ce9a5dSMichael Neuling 		return -ENODEV;
468b57aeab8SRavi Bangoria 
469b57aeab8SRavi Bangoria 	return hw_breakpoint_validate_len(hw);
4705aae8a53SK.Prasad }
4715aae8a53SK.Prasad 
4725aae8a53SK.Prasad /*
47306532a67SK.Prasad  * Restores the breakpoint on the debug registers.
47406532a67SK.Prasad  * Invoke this function if it is known that the execution context is
47506532a67SK.Prasad  * about to change to cause loss of MSR_SE settings.
47606532a67SK.Prasad  */
47706532a67SK.Prasad void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
47806532a67SK.Prasad {
47906532a67SK.Prasad 	struct arch_hw_breakpoint *info;
48074c68810SRavi Bangoria 	int i;
48106532a67SK.Prasad 
48274c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
48374c68810SRavi Bangoria 		if (unlikely(tsk->thread.last_hit_ubp[i]))
48474c68810SRavi Bangoria 			goto reset;
48574c68810SRavi Bangoria 	}
48606532a67SK.Prasad 	return;
48706532a67SK.Prasad 
48874c68810SRavi Bangoria reset:
48906532a67SK.Prasad 	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);
49374c68810SRavi Bangoria 		tsk->thread.last_hit_ubp[i] = NULL;
49474c68810SRavi Bangoria 	}
49506532a67SK.Prasad }
49606532a67SK.Prasad 
49774c68810SRavi Bangoria static bool dar_in_user_range(unsigned long dar, struct arch_hw_breakpoint *info)
498bc01bdf6SRavi Bangoria {
49927985b2aSRavi Bangoria 	return ((info->address <= dar) && (dar - info->address < info->len));
50027985b2aSRavi Bangoria }
501bc01bdf6SRavi Bangoria 
502f6780ce6SRavi Bangoria static bool ea_user_range_overlaps(unsigned long ea, int size,
50327985b2aSRavi Bangoria 				   struct arch_hw_breakpoint *info)
504658d029dSChristophe Leroy {
505f6780ce6SRavi Bangoria 	return ((ea < info->address + info->len) &&
506f6780ce6SRavi Bangoria 		(ea + size > info->address));
50774c68810SRavi Bangoria }
508bc01bdf6SRavi Bangoria 
50974c68810SRavi Bangoria static bool dar_in_hw_range(unsigned long dar, struct arch_hw_breakpoint *info)
51074c68810SRavi Bangoria {
51174c68810SRavi Bangoria 	unsigned long hw_start_addr, hw_end_addr;
512bc01bdf6SRavi Bangoria 
51374c68810SRavi Bangoria 	hw_start_addr = ALIGN_DOWN(info->address, HW_BREAKPOINT_SIZE);
51474c68810SRavi Bangoria 	hw_end_addr = ALIGN(info->address + info->len, HW_BREAKPOINT_SIZE);
51527985b2aSRavi Bangoria 
51674c68810SRavi Bangoria 	return ((hw_start_addr <= dar) && (hw_end_addr > dar));
51774c68810SRavi Bangoria }
51874c68810SRavi Bangoria 
519f6780ce6SRavi Bangoria static bool ea_hw_range_overlaps(unsigned long ea, int size,
52074c68810SRavi Bangoria 				 struct arch_hw_breakpoint *info)
52174c68810SRavi Bangoria {
52274c68810SRavi Bangoria 	unsigned long hw_start_addr, hw_end_addr;
52374c68810SRavi Bangoria 
52474c68810SRavi Bangoria 	hw_start_addr = ALIGN_DOWN(info->address, HW_BREAKPOINT_SIZE);
52574c68810SRavi Bangoria 	hw_end_addr = ALIGN(info->address + info->len, HW_BREAKPOINT_SIZE);
52674c68810SRavi Bangoria 
527f6780ce6SRavi Bangoria 	return ((ea < hw_end_addr) && (ea + size > hw_start_addr));
528bc01bdf6SRavi Bangoria }
529658d029dSChristophe Leroy 
53027985b2aSRavi Bangoria /*
53174c68810SRavi Bangoria  * If hw has multiple DAWR registers, we also need to check all
53274c68810SRavi Bangoria  * dawrx constraint bits to confirm this is _really_ a valid event.
533f6780ce6SRavi Bangoria  * If type is UNKNOWN, but privilege level matches, consider it as
534f6780ce6SRavi Bangoria  * a positive match.
53527985b2aSRavi Bangoria  */
53674c68810SRavi Bangoria static bool check_dawrx_constraints(struct pt_regs *regs, int type,
53774c68810SRavi Bangoria 				    struct arch_hw_breakpoint *info)
53874c68810SRavi Bangoria {
53974c68810SRavi Bangoria 	if (OP_IS_LOAD(type) && !(info->type & HW_BRK_TYPE_READ))
54074c68810SRavi Bangoria 		return false;
54127985b2aSRavi Bangoria 
542f3c832f1SRavi Bangoria 	/*
543f3c832f1SRavi Bangoria 	 * The Cache Management instructions other than dcbz never
544f3c832f1SRavi Bangoria 	 * cause a match. i.e. if type is CACHEOP, the instruction
545f3c832f1SRavi Bangoria 	 * is dcbz, and dcbz is treated as Store.
546f3c832f1SRavi Bangoria 	 */
547f3c832f1SRavi Bangoria 	if ((OP_IS_STORE(type) || type == CACHEOP) && !(info->type & HW_BRK_TYPE_WRITE))
54874c68810SRavi Bangoria 		return false;
54974c68810SRavi Bangoria 
55074c68810SRavi Bangoria 	if (is_kernel_addr(regs->nip) && !(info->type & HW_BRK_TYPE_KERNEL))
55174c68810SRavi Bangoria 		return false;
55274c68810SRavi Bangoria 
55374c68810SRavi Bangoria 	if (user_mode(regs) && !(info->type & HW_BRK_TYPE_USER))
55474c68810SRavi Bangoria 		return false;
55574c68810SRavi Bangoria 
55674c68810SRavi Bangoria 	return true;
55774c68810SRavi Bangoria }
55874c68810SRavi Bangoria 
55974c68810SRavi Bangoria /*
56074c68810SRavi Bangoria  * Return true if the event is valid wrt dawr configuration,
56174c68810SRavi Bangoria  * including extraneous exception. Otherwise return false.
56274c68810SRavi Bangoria  */
56374c68810SRavi Bangoria static bool check_constraints(struct pt_regs *regs, struct ppc_inst instr,
564f6780ce6SRavi Bangoria 			      unsigned long ea, int type, int size,
565f6780ce6SRavi Bangoria 			      struct arch_hw_breakpoint *info)
56674c68810SRavi Bangoria {
56774c68810SRavi Bangoria 	bool in_user_range = dar_in_user_range(regs->dar, info);
56874c68810SRavi Bangoria 	bool dawrx_constraints;
56974c68810SRavi Bangoria 
57074c68810SRavi Bangoria 	/*
57174c68810SRavi Bangoria 	 * 8xx supports only one breakpoint and thus we can
57274c68810SRavi Bangoria 	 * unconditionally return true.
57374c68810SRavi Bangoria 	 */
57474c68810SRavi Bangoria 	if (IS_ENABLED(CONFIG_PPC_8xx)) {
57574c68810SRavi Bangoria 		if (!in_user_range)
57674c68810SRavi Bangoria 			info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
57774c68810SRavi Bangoria 		return true;
57874c68810SRavi Bangoria 	}
57974c68810SRavi Bangoria 
58074c68810SRavi Bangoria 	if (unlikely(ppc_inst_equal(instr, ppc_inst(0)))) {
581f6780ce6SRavi Bangoria 		if (cpu_has_feature(CPU_FTR_ARCH_31) &&
582f6780ce6SRavi Bangoria 		    !dar_in_hw_range(regs->dar, info))
583658d029dSChristophe Leroy 			return false;
584f6780ce6SRavi Bangoria 
585f6780ce6SRavi Bangoria 		return true;
586658d029dSChristophe Leroy 	}
587658d029dSChristophe Leroy 
58874c68810SRavi Bangoria 	dawrx_constraints = check_dawrx_constraints(regs, type, info);
589658d029dSChristophe Leroy 
590f6780ce6SRavi Bangoria 	if (type == UNKNOWN) {
591f6780ce6SRavi Bangoria 		if (cpu_has_feature(CPU_FTR_ARCH_31) &&
592f6780ce6SRavi Bangoria 		    !dar_in_hw_range(regs->dar, info))
593f6780ce6SRavi Bangoria 			return false;
594f6780ce6SRavi Bangoria 
595f6780ce6SRavi Bangoria 		return dawrx_constraints;
596f6780ce6SRavi Bangoria 	}
597f6780ce6SRavi Bangoria 
598f6780ce6SRavi Bangoria 	if (ea_user_range_overlaps(ea, size, info))
59974c68810SRavi Bangoria 		return dawrx_constraints;
60074c68810SRavi Bangoria 
601f6780ce6SRavi Bangoria 	if (ea_hw_range_overlaps(ea, size, info)) {
60274c68810SRavi Bangoria 		if (dawrx_constraints) {
60374c68810SRavi Bangoria 			info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
604bc01bdf6SRavi Bangoria 			return true;
60574c68810SRavi Bangoria 		}
60674c68810SRavi Bangoria 	}
60774c68810SRavi Bangoria 	return false;
60874c68810SRavi Bangoria }
609bc01bdf6SRavi Bangoria 
610f3c832f1SRavi Bangoria static int cache_op_size(void)
611f3c832f1SRavi Bangoria {
612f3c832f1SRavi Bangoria #ifdef __powerpc64__
613f3c832f1SRavi Bangoria 	return ppc64_caches.l1d.block_size;
614f3c832f1SRavi Bangoria #else
615f3c832f1SRavi Bangoria 	return L1_CACHE_BYTES;
616f3c832f1SRavi Bangoria #endif
617f3c832f1SRavi Bangoria }
618f3c832f1SRavi Bangoria 
61974c68810SRavi Bangoria static void get_instr_detail(struct pt_regs *regs, struct ppc_inst *instr,
620f6780ce6SRavi Bangoria 			     int *type, int *size, unsigned long *ea)
62174c68810SRavi Bangoria {
62274c68810SRavi Bangoria 	struct instruction_op op;
62374c68810SRavi Bangoria 
62474c68810SRavi Bangoria 	if (__get_user_instr_inatomic(*instr, (void __user *)regs->nip))
62574c68810SRavi Bangoria 		return;
62674c68810SRavi Bangoria 
62774c68810SRavi Bangoria 	analyse_instr(&op, regs, *instr);
62874c68810SRavi Bangoria 	*type = GETTYPE(op.type);
629f6780ce6SRavi Bangoria 	*ea = op.ea;
630f6780ce6SRavi Bangoria #ifdef __powerpc64__
631f6780ce6SRavi Bangoria 	if (!(regs->msr & MSR_64BIT))
632f6780ce6SRavi Bangoria 		*ea &= 0xffffffffUL;
633f6780ce6SRavi Bangoria #endif
634f3c832f1SRavi Bangoria 
635f6780ce6SRavi Bangoria 	*size = GETSIZE(op.type);
636f3c832f1SRavi Bangoria 	if (*type == CACHEOP) {
637f3c832f1SRavi Bangoria 		*size = cache_op_size();
638f3c832f1SRavi Bangoria 		*ea &= ~(*size - 1);
639f3c832f1SRavi Bangoria 	}
640f6780ce6SRavi Bangoria }
641f6780ce6SRavi Bangoria 
642f6780ce6SRavi Bangoria static bool is_larx_stcx_instr(int type)
643f6780ce6SRavi Bangoria {
644f6780ce6SRavi Bangoria 	return type == LARX || type == STCX;
64574c68810SRavi Bangoria }
64674c68810SRavi Bangoria 
647658d029dSChristophe Leroy /*
648bc01bdf6SRavi Bangoria  * We've failed in reliably handling the hw-breakpoint. Unregister
649bc01bdf6SRavi Bangoria  * it and throw a warning message to let the user know about it.
650658d029dSChristophe Leroy  */
65174c68810SRavi Bangoria static void handler_error(struct perf_event *bp, struct arch_hw_breakpoint *info)
65274c68810SRavi Bangoria {
65374c68810SRavi Bangoria 	WARN(1, "Unable to handle hardware breakpoint. Breakpoint at 0x%lx will be disabled.",
65474c68810SRavi Bangoria 	     info->address);
655658d029dSChristophe Leroy 	perf_event_disable_inatomic(bp);
65674c68810SRavi Bangoria }
65774c68810SRavi Bangoria 
65874c68810SRavi Bangoria static void larx_stcx_err(struct perf_event *bp, struct arch_hw_breakpoint *info)
65974c68810SRavi Bangoria {
66074c68810SRavi Bangoria 	printk_ratelimited("Breakpoint hit on instruction that can't be emulated. Breakpoint at 0x%lx will be disabled.\n",
66174c68810SRavi Bangoria 			   info->address);
66274c68810SRavi Bangoria 	perf_event_disable_inatomic(bp);
66374c68810SRavi Bangoria }
66474c68810SRavi Bangoria 
66574c68810SRavi Bangoria static bool stepping_handler(struct pt_regs *regs, struct perf_event **bp,
66674c68810SRavi Bangoria 			     struct arch_hw_breakpoint **info, int *hit,
66774c68810SRavi Bangoria 			     struct ppc_inst instr)
66874c68810SRavi Bangoria {
66974c68810SRavi Bangoria 	int i;
67074c68810SRavi Bangoria 	int stepped;
67174c68810SRavi Bangoria 
67274c68810SRavi Bangoria 	/* Do not emulate user-space instructions, instead single-step them */
67374c68810SRavi Bangoria 	if (user_mode(regs)) {
67474c68810SRavi Bangoria 		for (i = 0; i < nr_wp_slots(); i++) {
67574c68810SRavi Bangoria 			if (!hit[i])
67674c68810SRavi Bangoria 				continue;
67774c68810SRavi Bangoria 			current->thread.last_hit_ubp[i] = bp[i];
67874c68810SRavi Bangoria 			info[i] = NULL;
67974c68810SRavi Bangoria 		}
68074c68810SRavi Bangoria 		regs->msr |= MSR_SE;
681658d029dSChristophe Leroy 		return false;
682658d029dSChristophe Leroy 	}
683658d029dSChristophe Leroy 
68474c68810SRavi Bangoria 	stepped = emulate_step(regs, instr);
68574c68810SRavi Bangoria 	if (!stepped) {
68674c68810SRavi Bangoria 		for (i = 0; i < nr_wp_slots(); i++) {
68774c68810SRavi Bangoria 			if (!hit[i])
68874c68810SRavi Bangoria 				continue;
68974c68810SRavi Bangoria 			handler_error(bp[i], info[i]);
69074c68810SRavi Bangoria 			info[i] = NULL;
69174c68810SRavi Bangoria 		}
69274c68810SRavi Bangoria 		return false;
69374c68810SRavi Bangoria 	}
69474c68810SRavi Bangoria 	return true;
69574c68810SRavi Bangoria }
69674c68810SRavi Bangoria 
69703465f89SNicholas Piggin int hw_breakpoint_handler(struct die_args *args)
6985aae8a53SK.Prasad {
69974c68810SRavi Bangoria 	bool err = false;
7005aae8a53SK.Prasad 	int rc = NOTIFY_STOP;
70174c68810SRavi Bangoria 	struct perf_event *bp[HBP_NUM_MAX] = { NULL };
7025aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
70374c68810SRavi Bangoria 	struct arch_hw_breakpoint *info[HBP_NUM_MAX] = { NULL };
70474c68810SRavi Bangoria 	int i;
70574c68810SRavi Bangoria 	int hit[HBP_NUM_MAX] = {0};
70674c68810SRavi Bangoria 	int nr_hit = 0;
70774c68810SRavi Bangoria 	bool ptrace_bp = false;
70874c68810SRavi Bangoria 	struct ppc_inst instr = ppc_inst(0);
70974c68810SRavi Bangoria 	int type = 0;
71074c68810SRavi Bangoria 	int size = 0;
711f6780ce6SRavi Bangoria 	unsigned long ea;
7125aae8a53SK.Prasad 
7135aae8a53SK.Prasad 	/* Disable breakpoints during exception handling */
7149422de3eSMichael Neuling 	hw_breakpoint_disable();
715574cb248SPaul Mackerras 
7165aae8a53SK.Prasad 	/*
7175aae8a53SK.Prasad 	 * The counter may be concurrently released but that can only
7185aae8a53SK.Prasad 	 * occur from a call_rcu() path. We can then safely fetch
7195aae8a53SK.Prasad 	 * the breakpoint, use its callback, touch its counter
7205aae8a53SK.Prasad 	 * while we are in an rcu_read_lock() path.
7215aae8a53SK.Prasad 	 */
7225aae8a53SK.Prasad 	rcu_read_lock();
7235aae8a53SK.Prasad 
72474c68810SRavi Bangoria 	if (!IS_ENABLED(CONFIG_PPC_8xx))
725f6780ce6SRavi Bangoria 		get_instr_detail(regs, &instr, &type, &size, &ea);
72674c68810SRavi Bangoria 
72774c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
72874c68810SRavi Bangoria 		bp[i] = __this_cpu_read(bp_per_reg[i]);
72974c68810SRavi Bangoria 		if (!bp[i])
73074c68810SRavi Bangoria 			continue;
73174c68810SRavi Bangoria 
73274c68810SRavi Bangoria 		info[i] = counter_arch_bp(bp[i]);
73374c68810SRavi Bangoria 		info[i]->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
73474c68810SRavi Bangoria 
735f6780ce6SRavi Bangoria 		if (check_constraints(regs, instr, ea, type, size, info[i])) {
73674c68810SRavi Bangoria 			if (!IS_ENABLED(CONFIG_PPC_8xx) &&
73774c68810SRavi Bangoria 			    ppc_inst_equal(instr, ppc_inst(0))) {
73874c68810SRavi Bangoria 				handler_error(bp[i], info[i]);
73974c68810SRavi Bangoria 				info[i] = NULL;
74074c68810SRavi Bangoria 				err = 1;
74174c68810SRavi Bangoria 				continue;
74274c68810SRavi Bangoria 			}
74374c68810SRavi Bangoria 
74474c68810SRavi Bangoria 			if (is_ptrace_bp(bp[i]))
74574c68810SRavi Bangoria 				ptrace_bp = true;
74674c68810SRavi Bangoria 			hit[i] = 1;
74774c68810SRavi Bangoria 			nr_hit++;
74874c68810SRavi Bangoria 		}
74974c68810SRavi Bangoria 	}
75074c68810SRavi Bangoria 
75174c68810SRavi Bangoria 	if (err)
75274c68810SRavi Bangoria 		goto reset;
75374c68810SRavi Bangoria 
75474c68810SRavi Bangoria 	if (!nr_hit) {
755c21a493aSRavi Bangoria 		rc = NOTIFY_DONE;
7565aae8a53SK.Prasad 		goto out;
757c21a493aSRavi Bangoria 	}
7585aae8a53SK.Prasad 
7595aae8a53SK.Prasad 	/*
7605aae8a53SK.Prasad 	 * Return early after invoking user-callback function without restoring
7615aae8a53SK.Prasad 	 * DABR if the breakpoint is from ptrace which always operates in
7625aae8a53SK.Prasad 	 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
7635aae8a53SK.Prasad 	 * generated in do_dabr().
7645aae8a53SK.Prasad 	 */
76574c68810SRavi Bangoria 	if (ptrace_bp) {
76674c68810SRavi Bangoria 		for (i = 0; i < nr_wp_slots(); i++) {
76774c68810SRavi Bangoria 			if (!hit[i])
76874c68810SRavi Bangoria 				continue;
76974c68810SRavi Bangoria 			perf_bp_event(bp[i], regs);
77074c68810SRavi Bangoria 			info[i] = NULL;
77174c68810SRavi Bangoria 		}
7725aae8a53SK.Prasad 		rc = NOTIFY_DONE;
77374c68810SRavi Bangoria 		goto reset;
7745aae8a53SK.Prasad 	}
7755aae8a53SK.Prasad 
77674c68810SRavi Bangoria 	if (!IS_ENABLED(CONFIG_PPC_8xx)) {
777f6780ce6SRavi Bangoria 		if (is_larx_stcx_instr(type)) {
77874c68810SRavi Bangoria 			for (i = 0; i < nr_wp_slots(); i++) {
77974c68810SRavi Bangoria 				if (!hit[i])
78074c68810SRavi Bangoria 					continue;
78174c68810SRavi Bangoria 				larx_stcx_err(bp[i], info[i]);
78274c68810SRavi Bangoria 				info[i] = NULL;
78374c68810SRavi Bangoria 			}
78474c68810SRavi Bangoria 			goto reset;
78574c68810SRavi Bangoria 		}
78674c68810SRavi Bangoria 
78774c68810SRavi Bangoria 		if (!stepping_handler(regs, bp, info, hit, instr))
78874c68810SRavi Bangoria 			goto reset;
789e08658a6SRavi Bangoria 	}
7905aae8a53SK.Prasad 
7915aae8a53SK.Prasad 	/*
7925aae8a53SK.Prasad 	 * As a policy, the callback is invoked in a 'trigger-after-execute'
7935aae8a53SK.Prasad 	 * fashion
7945aae8a53SK.Prasad 	 */
79574c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
79674c68810SRavi Bangoria 		if (!hit[i])
79774c68810SRavi Bangoria 			continue;
79874c68810SRavi Bangoria 		if (!(info[i]->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
79974c68810SRavi Bangoria 			perf_bp_event(bp[i], regs);
80074c68810SRavi Bangoria 	}
8015aae8a53SK.Prasad 
80274c68810SRavi Bangoria reset:
80374c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
80474c68810SRavi Bangoria 		if (!info[i])
80574c68810SRavi Bangoria 			continue;
80674c68810SRavi Bangoria 		__set_breakpoint(i, info[i]);
80774c68810SRavi Bangoria 	}
80874c68810SRavi Bangoria 
8095aae8a53SK.Prasad out:
8105aae8a53SK.Prasad 	rcu_read_unlock();
8115aae8a53SK.Prasad 	return rc;
8125aae8a53SK.Prasad }
81303465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_handler);
8145aae8a53SK.Prasad 
8155aae8a53SK.Prasad /*
8165aae8a53SK.Prasad  * Handle single-step exceptions following a DABR hit.
8175aae8a53SK.Prasad  */
81803465f89SNicholas Piggin static int single_step_dabr_instruction(struct die_args *args)
8195aae8a53SK.Prasad {
8205aae8a53SK.Prasad 	struct pt_regs *regs = args->regs;
8215aae8a53SK.Prasad 	struct perf_event *bp = NULL;
8223f4693eeSMichael Neuling 	struct arch_hw_breakpoint *info;
82374c68810SRavi Bangoria 	int i;
82474c68810SRavi Bangoria 	bool found = false;
8255aae8a53SK.Prasad 
8265aae8a53SK.Prasad 	/*
8275aae8a53SK.Prasad 	 * Check if we are single-stepping as a result of a
8285aae8a53SK.Prasad 	 * previous HW Breakpoint exception
8295aae8a53SK.Prasad 	 */
83074c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
83174c68810SRavi Bangoria 		bp = current->thread.last_hit_ubp[i];
8325aae8a53SK.Prasad 
83374c68810SRavi Bangoria 		if (!bp)
83474c68810SRavi Bangoria 			continue;
83574c68810SRavi Bangoria 
83674c68810SRavi Bangoria 		found = true;
8373f4693eeSMichael Neuling 		info = counter_arch_bp(bp);
8385aae8a53SK.Prasad 
8395aae8a53SK.Prasad 		/*
84074c68810SRavi Bangoria 		 * We shall invoke the user-defined callback function in the
84174c68810SRavi Bangoria 		 * single stepping handler to confirm to 'trigger-after-execute'
84274c68810SRavi Bangoria 		 * semantics
8435aae8a53SK.Prasad 		 */
8449422de3eSMichael Neuling 		if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
8455aae8a53SK.Prasad 			perf_bp_event(bp, regs);
84674c68810SRavi Bangoria 		current->thread.last_hit_ubp[i] = NULL;
84774c68810SRavi Bangoria 	}
8485aae8a53SK.Prasad 
84974c68810SRavi Bangoria 	if (!found)
85074c68810SRavi Bangoria 		return NOTIFY_DONE;
85174c68810SRavi Bangoria 
85274c68810SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
85374c68810SRavi Bangoria 		bp = __this_cpu_read(bp_per_reg[i]);
85474c68810SRavi Bangoria 		if (!bp)
85574c68810SRavi Bangoria 			continue;
85674c68810SRavi Bangoria 
85774c68810SRavi Bangoria 		info = counter_arch_bp(bp);
85874c68810SRavi Bangoria 		__set_breakpoint(i, info);
85974c68810SRavi Bangoria 	}
86076b0f133SPaul Mackerras 
86176b0f133SPaul Mackerras 	/*
86276b0f133SPaul Mackerras 	 * If the process was being single-stepped by ptrace, let the
86376b0f133SPaul Mackerras 	 * other single-step actions occur (e.g. generate SIGTRAP).
86476b0f133SPaul Mackerras 	 */
86576b0f133SPaul Mackerras 	if (test_thread_flag(TIF_SINGLESTEP))
86676b0f133SPaul Mackerras 		return NOTIFY_DONE;
86776b0f133SPaul Mackerras 
8685aae8a53SK.Prasad 	return NOTIFY_STOP;
8695aae8a53SK.Prasad }
87003465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_dabr_instruction);
8715aae8a53SK.Prasad 
8725aae8a53SK.Prasad /*
8735aae8a53SK.Prasad  * Handle debug exception notifications.
8745aae8a53SK.Prasad  */
87503465f89SNicholas Piggin int hw_breakpoint_exceptions_notify(
8765aae8a53SK.Prasad 		struct notifier_block *unused, unsigned long val, void *data)
8775aae8a53SK.Prasad {
8785aae8a53SK.Prasad 	int ret = NOTIFY_DONE;
8795aae8a53SK.Prasad 
8805aae8a53SK.Prasad 	switch (val) {
8815aae8a53SK.Prasad 	case DIE_DABR_MATCH:
8825aae8a53SK.Prasad 		ret = hw_breakpoint_handler(data);
8835aae8a53SK.Prasad 		break;
8845aae8a53SK.Prasad 	case DIE_SSTEP:
8855aae8a53SK.Prasad 		ret = single_step_dabr_instruction(data);
8865aae8a53SK.Prasad 		break;
8875aae8a53SK.Prasad 	}
8885aae8a53SK.Prasad 
8895aae8a53SK.Prasad 	return ret;
8905aae8a53SK.Prasad }
89103465f89SNicholas Piggin NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
8925aae8a53SK.Prasad 
8935aae8a53SK.Prasad /*
8945aae8a53SK.Prasad  * Release the user breakpoints used by ptrace
8955aae8a53SK.Prasad  */
8965aae8a53SK.Prasad void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
8975aae8a53SK.Prasad {
8986b424efaSRavi Bangoria 	int i;
8995aae8a53SK.Prasad 	struct thread_struct *t = &tsk->thread;
9005aae8a53SK.Prasad 
9016b424efaSRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
9026b424efaSRavi Bangoria 		unregister_hw_breakpoint(t->ptrace_bps[i]);
9036b424efaSRavi Bangoria 		t->ptrace_bps[i] = NULL;
9046b424efaSRavi Bangoria 	}
9055aae8a53SK.Prasad }
9065aae8a53SK.Prasad 
9075aae8a53SK.Prasad void hw_breakpoint_pmu_read(struct perf_event *bp)
9085aae8a53SK.Prasad {
9095aae8a53SK.Prasad 	/* TODO */
9105aae8a53SK.Prasad }
911ccbed90bSChristophe Leroy 
912ccbed90bSChristophe Leroy void ptrace_triggered(struct perf_event *bp,
913ccbed90bSChristophe Leroy 		      struct perf_sample_data *data, struct pt_regs *regs)
914ccbed90bSChristophe Leroy {
915ccbed90bSChristophe Leroy 	struct perf_event_attr attr;
916ccbed90bSChristophe Leroy 
917ccbed90bSChristophe Leroy 	/*
918ccbed90bSChristophe Leroy 	 * Disable the breakpoint request here since ptrace has defined a
919ccbed90bSChristophe Leroy 	 * one-shot behaviour for breakpoint exceptions in PPC64.
920ccbed90bSChristophe Leroy 	 * The SIGTRAP signal is generated automatically for us in do_dabr().
921ccbed90bSChristophe Leroy 	 * We don't have to do anything about that here
922ccbed90bSChristophe Leroy 	 */
923ccbed90bSChristophe Leroy 	attr = bp->attr;
924ccbed90bSChristophe Leroy 	attr.disabled = true;
925ccbed90bSChristophe Leroy 	modify_user_hw_breakpoint(bp, &attr);
926ccbed90bSChristophe Leroy }
927