xref: /openbmc/linux/arch/arm/kernel/hw_breakpoint.c (revision 7d99331e4793b52d488e911876ef11d843c6c8c9)
1f81ef4a9SWill Deacon /*
2f81ef4a9SWill Deacon  * This program is free software; you can redistribute it and/or modify
3f81ef4a9SWill Deacon  * it under the terms of the GNU General Public License version 2 as
4f81ef4a9SWill Deacon  * published by the Free Software Foundation.
5f81ef4a9SWill Deacon  *
6f81ef4a9SWill Deacon  * This program is distributed in the hope that it will be useful,
7f81ef4a9SWill Deacon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8f81ef4a9SWill Deacon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9f81ef4a9SWill Deacon  * GNU General Public License for more details.
10f81ef4a9SWill Deacon  *
11f81ef4a9SWill Deacon  * You should have received a copy of the GNU General Public License
12f81ef4a9SWill Deacon  * along with this program; if not, write to the Free Software
13f81ef4a9SWill Deacon  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14f81ef4a9SWill Deacon  *
15f81ef4a9SWill Deacon  * Copyright (C) 2009, 2010 ARM Limited
16f81ef4a9SWill Deacon  *
17f81ef4a9SWill Deacon  * Author: Will Deacon <will.deacon@arm.com>
18f81ef4a9SWill Deacon  */
19f81ef4a9SWill Deacon 
20f81ef4a9SWill Deacon /*
21f81ef4a9SWill Deacon  * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
22f81ef4a9SWill Deacon  * using the CPU's debug registers.
23f81ef4a9SWill Deacon  */
24f81ef4a9SWill Deacon #define pr_fmt(fmt) "hw-breakpoint: " fmt
25f81ef4a9SWill Deacon 
26f81ef4a9SWill Deacon #include <linux/errno.h>
27f81ef4a9SWill Deacon #include <linux/perf_event.h>
28f81ef4a9SWill Deacon #include <linux/hw_breakpoint.h>
29f81ef4a9SWill Deacon #include <linux/smp.h>
30f81ef4a9SWill Deacon 
31f81ef4a9SWill Deacon #include <asm/cacheflush.h>
32f81ef4a9SWill Deacon #include <asm/cputype.h>
33f81ef4a9SWill Deacon #include <asm/current.h>
34f81ef4a9SWill Deacon #include <asm/hw_breakpoint.h>
35f81ef4a9SWill Deacon #include <asm/kdebug.h>
36f81ef4a9SWill Deacon #include <asm/system.h>
37f81ef4a9SWill Deacon #include <asm/traps.h>
38f81ef4a9SWill Deacon 
39f81ef4a9SWill Deacon /* Breakpoint currently in use for each BRP. */
40f81ef4a9SWill Deacon static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
41f81ef4a9SWill Deacon 
42f81ef4a9SWill Deacon /* Watchpoint currently in use for each WRP. */
43f81ef4a9SWill Deacon static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
44f81ef4a9SWill Deacon 
45f81ef4a9SWill Deacon /* Number of BRP/WRP registers on this CPU. */
46f81ef4a9SWill Deacon static int core_num_brps;
47f81ef4a9SWill Deacon static int core_num_wrps;
48f81ef4a9SWill Deacon 
49f81ef4a9SWill Deacon /* Debug architecture version. */
50f81ef4a9SWill Deacon static u8 debug_arch;
51f81ef4a9SWill Deacon 
52f81ef4a9SWill Deacon /* Maximum supported watchpoint length. */
53f81ef4a9SWill Deacon static u8 max_watchpoint_len;
54f81ef4a9SWill Deacon 
55f81ef4a9SWill Deacon /* Determine number of BRP registers available. */
56f81ef4a9SWill Deacon static int get_num_brps(void)
57f81ef4a9SWill Deacon {
58f81ef4a9SWill Deacon 	u32 didr;
59f81ef4a9SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
60f81ef4a9SWill Deacon 	return ((didr >> 24) & 0xf) + 1;
61f81ef4a9SWill Deacon }
62f81ef4a9SWill Deacon 
63f81ef4a9SWill Deacon /* Determine number of WRP registers available. */
64f81ef4a9SWill Deacon static int get_num_wrps(void)
65f81ef4a9SWill Deacon {
66f81ef4a9SWill Deacon 	/*
67f81ef4a9SWill Deacon 	 * FIXME: When a watchpoint fires, the only way to work out which
68f81ef4a9SWill Deacon 	 * watchpoint it was is by disassembling the faulting instruction
69f81ef4a9SWill Deacon 	 * and working out the address of the memory access.
70f81ef4a9SWill Deacon 	 *
71f81ef4a9SWill Deacon 	 * Furthermore, we can only do this if the watchpoint was precise
72f81ef4a9SWill Deacon 	 * since imprecise watchpoints prevent us from calculating register
73f81ef4a9SWill Deacon 	 * based addresses.
74f81ef4a9SWill Deacon 	 *
75f81ef4a9SWill Deacon 	 * For the time being, we only report 1 watchpoint register so we
76f81ef4a9SWill Deacon 	 * always know which watchpoint fired. In the future we can either
77f81ef4a9SWill Deacon 	 * add a disassembler and address generation emulator, or we can
78f81ef4a9SWill Deacon 	 * insert a check to see if the DFAR is set on watchpoint exception
79f81ef4a9SWill Deacon 	 * entry [the ARM ARM states that the DFAR is UNKNOWN, but
80f81ef4a9SWill Deacon 	 * experience shows that it is set on some implementations].
81f81ef4a9SWill Deacon 	 */
82f81ef4a9SWill Deacon 
83f81ef4a9SWill Deacon #if 0
84f81ef4a9SWill Deacon 	u32 didr, wrps;
85f81ef4a9SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
86f81ef4a9SWill Deacon 	return ((didr >> 28) & 0xf) + 1;
87f81ef4a9SWill Deacon #endif
88f81ef4a9SWill Deacon 
89f81ef4a9SWill Deacon 	return 1;
90f81ef4a9SWill Deacon }
91f81ef4a9SWill Deacon 
92f81ef4a9SWill Deacon int hw_breakpoint_slots(int type)
93f81ef4a9SWill Deacon {
94f81ef4a9SWill Deacon 	/*
95f81ef4a9SWill Deacon 	 * We can be called early, so don't rely on
96f81ef4a9SWill Deacon 	 * our static variables being initialised.
97f81ef4a9SWill Deacon 	 */
98f81ef4a9SWill Deacon 	switch (type) {
99f81ef4a9SWill Deacon 	case TYPE_INST:
100f81ef4a9SWill Deacon 		return get_num_brps();
101f81ef4a9SWill Deacon 	case TYPE_DATA:
102f81ef4a9SWill Deacon 		return get_num_wrps();
103f81ef4a9SWill Deacon 	default:
104f81ef4a9SWill Deacon 		pr_warning("unknown slot type: %d\n", type);
105f81ef4a9SWill Deacon 		return 0;
106f81ef4a9SWill Deacon 	}
107f81ef4a9SWill Deacon }
108f81ef4a9SWill Deacon 
109f81ef4a9SWill Deacon /* Determine debug architecture. */
110f81ef4a9SWill Deacon static u8 get_debug_arch(void)
111f81ef4a9SWill Deacon {
112f81ef4a9SWill Deacon 	u32 didr;
113f81ef4a9SWill Deacon 
114f81ef4a9SWill Deacon 	/* Do we implement the extended CPUID interface? */
115f81ef4a9SWill Deacon 	if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
116f81ef4a9SWill Deacon 		pr_warning("CPUID feature registers not supported. "
117f81ef4a9SWill Deacon 				"Assuming v6 debug is present.\n");
118f81ef4a9SWill Deacon 		return ARM_DEBUG_ARCH_V6;
119f81ef4a9SWill Deacon 	}
120f81ef4a9SWill Deacon 
121f81ef4a9SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
122f81ef4a9SWill Deacon 	return (didr >> 16) & 0xf;
123f81ef4a9SWill Deacon }
124f81ef4a9SWill Deacon 
125f81ef4a9SWill Deacon /* Does this core support mismatch breakpoints? */
126f81ef4a9SWill Deacon static int core_has_mismatch_bps(void)
127f81ef4a9SWill Deacon {
128f81ef4a9SWill Deacon 	return debug_arch >= ARM_DEBUG_ARCH_V7_ECP14 && core_num_brps > 1;
129f81ef4a9SWill Deacon }
130f81ef4a9SWill Deacon 
131f81ef4a9SWill Deacon u8 arch_get_debug_arch(void)
132f81ef4a9SWill Deacon {
133f81ef4a9SWill Deacon 	return debug_arch;
134f81ef4a9SWill Deacon }
135f81ef4a9SWill Deacon 
136f81ef4a9SWill Deacon #define READ_WB_REG_CASE(OP2, M, VAL)		\
137f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):			\
138f81ef4a9SWill Deacon 		ARM_DBG_READ(c ## M, OP2, VAL); \
139f81ef4a9SWill Deacon 		break
140f81ef4a9SWill Deacon 
141f81ef4a9SWill Deacon #define WRITE_WB_REG_CASE(OP2, M, VAL)		\
142f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):			\
143f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c ## M, OP2, VAL);\
144f81ef4a9SWill Deacon 		break
145f81ef4a9SWill Deacon 
146f81ef4a9SWill Deacon #define GEN_READ_WB_REG_CASES(OP2, VAL)		\
147f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 0, VAL);		\
148f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 1, VAL);		\
149f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 2, VAL);		\
150f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 3, VAL);		\
151f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 4, VAL);		\
152f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 5, VAL);		\
153f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 6, VAL);		\
154f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 7, VAL);		\
155f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 8, VAL);		\
156f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 9, VAL);		\
157f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 10, VAL);		\
158f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 11, VAL);		\
159f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 12, VAL);		\
160f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 13, VAL);		\
161f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 14, VAL);		\
162f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 15, VAL)
163f81ef4a9SWill Deacon 
164f81ef4a9SWill Deacon #define GEN_WRITE_WB_REG_CASES(OP2, VAL)	\
165f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 0, VAL);		\
166f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 1, VAL);		\
167f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 2, VAL);		\
168f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 3, VAL);		\
169f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 4, VAL);		\
170f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 5, VAL);		\
171f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 6, VAL);		\
172f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 7, VAL);		\
173f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 8, VAL);		\
174f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 9, VAL);		\
175f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 10, VAL);	\
176f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 11, VAL);	\
177f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 12, VAL);	\
178f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 13, VAL);	\
179f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 14, VAL);	\
180f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 15, VAL)
181f81ef4a9SWill Deacon 
182f81ef4a9SWill Deacon static u32 read_wb_reg(int n)
183f81ef4a9SWill Deacon {
184f81ef4a9SWill Deacon 	u32 val = 0;
185f81ef4a9SWill Deacon 
186f81ef4a9SWill Deacon 	switch (n) {
187f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
188f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
189f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
190f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
191f81ef4a9SWill Deacon 	default:
192f81ef4a9SWill Deacon 		pr_warning("attempt to read from unknown breakpoint "
193f81ef4a9SWill Deacon 				"register %d\n", n);
194f81ef4a9SWill Deacon 	}
195f81ef4a9SWill Deacon 
196f81ef4a9SWill Deacon 	return val;
197f81ef4a9SWill Deacon }
198f81ef4a9SWill Deacon 
199f81ef4a9SWill Deacon static void write_wb_reg(int n, u32 val)
200f81ef4a9SWill Deacon {
201f81ef4a9SWill Deacon 	switch (n) {
202f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
203f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
204f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
205f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
206f81ef4a9SWill Deacon 	default:
207f81ef4a9SWill Deacon 		pr_warning("attempt to write to unknown breakpoint "
208f81ef4a9SWill Deacon 				"register %d\n", n);
209f81ef4a9SWill Deacon 	}
210f81ef4a9SWill Deacon 	isb();
211f81ef4a9SWill Deacon }
212f81ef4a9SWill Deacon 
213f81ef4a9SWill Deacon /*
214f81ef4a9SWill Deacon  * In order to access the breakpoint/watchpoint control registers,
215f81ef4a9SWill Deacon  * we must be running in debug monitor mode. Unfortunately, we can
216f81ef4a9SWill Deacon  * be put into halting debug mode at any time by an external debugger
217f81ef4a9SWill Deacon  * but there is nothing we can do to prevent that.
218f81ef4a9SWill Deacon  */
219f81ef4a9SWill Deacon static int enable_monitor_mode(void)
220f81ef4a9SWill Deacon {
221f81ef4a9SWill Deacon 	u32 dscr;
222f81ef4a9SWill Deacon 	int ret = 0;
223f81ef4a9SWill Deacon 
224f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
225f81ef4a9SWill Deacon 
226f81ef4a9SWill Deacon 	/* Ensure that halting mode is disabled. */
227f81ef4a9SWill Deacon 	if (WARN_ONCE(dscr & ARM_DSCR_HDBGEN, "halting debug mode enabled."
228f81ef4a9SWill Deacon 				"Unable to access hardware resources.")) {
229f81ef4a9SWill Deacon 		ret = -EPERM;
230f81ef4a9SWill Deacon 		goto out;
231f81ef4a9SWill Deacon 	}
232f81ef4a9SWill Deacon 
233f81ef4a9SWill Deacon 	/* Write to the corresponding DSCR. */
234f81ef4a9SWill Deacon 	switch (debug_arch) {
235f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6:
236f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6_1:
237f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN));
238f81ef4a9SWill Deacon 		break;
239f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V7_ECP14:
240f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN));
241f81ef4a9SWill Deacon 		break;
242f81ef4a9SWill Deacon 	default:
243f81ef4a9SWill Deacon 		ret = -ENODEV;
244f81ef4a9SWill Deacon 		goto out;
245f81ef4a9SWill Deacon 	}
246f81ef4a9SWill Deacon 
247f81ef4a9SWill Deacon 	/* Check that the write made it through. */
248f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
249f81ef4a9SWill Deacon 	if (WARN_ONCE(!(dscr & ARM_DSCR_MDBGEN),
250f81ef4a9SWill Deacon 				"failed to enable monitor mode.")) {
251f81ef4a9SWill Deacon 		ret = -EPERM;
252f81ef4a9SWill Deacon 	}
253f81ef4a9SWill Deacon 
254f81ef4a9SWill Deacon out:
255f81ef4a9SWill Deacon 	return ret;
256f81ef4a9SWill Deacon }
257f81ef4a9SWill Deacon 
258f81ef4a9SWill Deacon /*
259f81ef4a9SWill Deacon  * Check if 8-bit byte-address select is available.
260f81ef4a9SWill Deacon  * This clobbers WRP 0.
261f81ef4a9SWill Deacon  */
262f81ef4a9SWill Deacon static u8 get_max_wp_len(void)
263f81ef4a9SWill Deacon {
264f81ef4a9SWill Deacon 	u32 ctrl_reg;
265f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
266f81ef4a9SWill Deacon 	u8 size = 4;
267f81ef4a9SWill Deacon 
268f81ef4a9SWill Deacon 	if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
269f81ef4a9SWill Deacon 		goto out;
270f81ef4a9SWill Deacon 
271f81ef4a9SWill Deacon 	if (enable_monitor_mode())
272f81ef4a9SWill Deacon 		goto out;
273f81ef4a9SWill Deacon 
274f81ef4a9SWill Deacon 	memset(&ctrl, 0, sizeof(ctrl));
275f81ef4a9SWill Deacon 	ctrl.len = ARM_BREAKPOINT_LEN_8;
276f81ef4a9SWill Deacon 	ctrl_reg = encode_ctrl_reg(ctrl);
277f81ef4a9SWill Deacon 
278f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WVR, 0);
279f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WCR, ctrl_reg);
280f81ef4a9SWill Deacon 	if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
281f81ef4a9SWill Deacon 		size = 8;
282f81ef4a9SWill Deacon 
283f81ef4a9SWill Deacon out:
284f81ef4a9SWill Deacon 	return size;
285f81ef4a9SWill Deacon }
286f81ef4a9SWill Deacon 
287f81ef4a9SWill Deacon u8 arch_get_max_wp_len(void)
288f81ef4a9SWill Deacon {
289f81ef4a9SWill Deacon 	return max_watchpoint_len;
290f81ef4a9SWill Deacon }
291f81ef4a9SWill Deacon 
292f81ef4a9SWill Deacon /*
293f81ef4a9SWill Deacon  * Handler for reactivating a suspended watchpoint when the single
294f81ef4a9SWill Deacon  * step `mismatch' breakpoint is triggered.
295f81ef4a9SWill Deacon  */
296f81ef4a9SWill Deacon static void wp_single_step_handler(struct perf_event *bp, int unused,
297f81ef4a9SWill Deacon 				   struct perf_sample_data *data,
298f81ef4a9SWill Deacon 				   struct pt_regs *regs)
299f81ef4a9SWill Deacon {
300f81ef4a9SWill Deacon 	perf_event_enable(counter_arch_bp(bp)->suspended_wp);
301f81ef4a9SWill Deacon 	unregister_hw_breakpoint(bp);
302f81ef4a9SWill Deacon }
303f81ef4a9SWill Deacon 
304f81ef4a9SWill Deacon static int bp_is_single_step(struct perf_event *bp)
305f81ef4a9SWill Deacon {
306f81ef4a9SWill Deacon 	return bp->overflow_handler == wp_single_step_handler;
307f81ef4a9SWill Deacon }
308f81ef4a9SWill Deacon 
309f81ef4a9SWill Deacon /*
310f81ef4a9SWill Deacon  * Install a perf counter breakpoint.
311f81ef4a9SWill Deacon  */
312f81ef4a9SWill Deacon int arch_install_hw_breakpoint(struct perf_event *bp)
313f81ef4a9SWill Deacon {
314f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
315f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
316f81ef4a9SWill Deacon 	int i, max_slots, ctrl_base, val_base, ret = 0;
317f81ef4a9SWill Deacon 
318f81ef4a9SWill Deacon 	/* Ensure that we are in monitor mode and halting mode is disabled. */
319f81ef4a9SWill Deacon 	ret = enable_monitor_mode();
320f81ef4a9SWill Deacon 	if (ret)
321f81ef4a9SWill Deacon 		goto out;
322f81ef4a9SWill Deacon 
323f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
324f81ef4a9SWill Deacon 		/* Breakpoint */
325f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_BCR;
326f81ef4a9SWill Deacon 		val_base = ARM_BASE_BVR;
327f81ef4a9SWill Deacon 		slots = __get_cpu_var(bp_on_reg);
328f81ef4a9SWill Deacon 		max_slots = core_num_brps - 1;
329f81ef4a9SWill Deacon 
330f81ef4a9SWill Deacon 		if (bp_is_single_step(bp)) {
331f81ef4a9SWill Deacon 			info->ctrl.mismatch = 1;
332f81ef4a9SWill Deacon 			i = max_slots;
333f81ef4a9SWill Deacon 			slots[i] = bp;
334f81ef4a9SWill Deacon 			goto setup;
335f81ef4a9SWill Deacon 		}
336f81ef4a9SWill Deacon 	} else {
337f81ef4a9SWill Deacon 		/* Watchpoint */
338f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_WCR;
339f81ef4a9SWill Deacon 		val_base = ARM_BASE_WVR;
340f81ef4a9SWill Deacon 		slots = __get_cpu_var(wp_on_reg);
341f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
342f81ef4a9SWill Deacon 	}
343f81ef4a9SWill Deacon 
344f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
345f81ef4a9SWill Deacon 		slot = &slots[i];
346f81ef4a9SWill Deacon 
347f81ef4a9SWill Deacon 		if (!*slot) {
348f81ef4a9SWill Deacon 			*slot = bp;
349f81ef4a9SWill Deacon 			break;
350f81ef4a9SWill Deacon 		}
351f81ef4a9SWill Deacon 	}
352f81ef4a9SWill Deacon 
353f81ef4a9SWill Deacon 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot")) {
354f81ef4a9SWill Deacon 		ret = -EBUSY;
355f81ef4a9SWill Deacon 		goto out;
356f81ef4a9SWill Deacon 	}
357f81ef4a9SWill Deacon 
358f81ef4a9SWill Deacon setup:
359f81ef4a9SWill Deacon 	/* Setup the address register. */
360f81ef4a9SWill Deacon 	write_wb_reg(val_base + i, info->address);
361f81ef4a9SWill Deacon 
362f81ef4a9SWill Deacon 	/* Setup the control register. */
363f81ef4a9SWill Deacon 	write_wb_reg(ctrl_base + i, encode_ctrl_reg(info->ctrl) | 0x1);
364f81ef4a9SWill Deacon 
365f81ef4a9SWill Deacon out:
366f81ef4a9SWill Deacon 	return ret;
367f81ef4a9SWill Deacon }
368f81ef4a9SWill Deacon 
369f81ef4a9SWill Deacon void arch_uninstall_hw_breakpoint(struct perf_event *bp)
370f81ef4a9SWill Deacon {
371f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
372f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
373f81ef4a9SWill Deacon 	int i, max_slots, base;
374f81ef4a9SWill Deacon 
375f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
376f81ef4a9SWill Deacon 		/* Breakpoint */
377f81ef4a9SWill Deacon 		base = ARM_BASE_BCR;
378f81ef4a9SWill Deacon 		slots = __get_cpu_var(bp_on_reg);
379f81ef4a9SWill Deacon 		max_slots = core_num_brps - 1;
380f81ef4a9SWill Deacon 
381f81ef4a9SWill Deacon 		if (bp_is_single_step(bp)) {
382f81ef4a9SWill Deacon 			i = max_slots;
383f81ef4a9SWill Deacon 			slots[i] = NULL;
384f81ef4a9SWill Deacon 			goto reset;
385f81ef4a9SWill Deacon 		}
386f81ef4a9SWill Deacon 	} else {
387f81ef4a9SWill Deacon 		/* Watchpoint */
388f81ef4a9SWill Deacon 		base = ARM_BASE_WCR;
389f81ef4a9SWill Deacon 		slots = __get_cpu_var(wp_on_reg);
390f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
391f81ef4a9SWill Deacon 	}
392f81ef4a9SWill Deacon 
393f81ef4a9SWill Deacon 	/* Remove the breakpoint. */
394f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
395f81ef4a9SWill Deacon 		slot = &slots[i];
396f81ef4a9SWill Deacon 
397f81ef4a9SWill Deacon 		if (*slot == bp) {
398f81ef4a9SWill Deacon 			*slot = NULL;
399f81ef4a9SWill Deacon 			break;
400f81ef4a9SWill Deacon 		}
401f81ef4a9SWill Deacon 	}
402f81ef4a9SWill Deacon 
403f81ef4a9SWill Deacon 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot"))
404f81ef4a9SWill Deacon 		return;
405f81ef4a9SWill Deacon 
406f81ef4a9SWill Deacon reset:
407f81ef4a9SWill Deacon 	/* Reset the control register. */
408f81ef4a9SWill Deacon 	write_wb_reg(base + i, 0);
409f81ef4a9SWill Deacon }
410f81ef4a9SWill Deacon 
411f81ef4a9SWill Deacon static int get_hbp_len(u8 hbp_len)
412f81ef4a9SWill Deacon {
413f81ef4a9SWill Deacon 	unsigned int len_in_bytes = 0;
414f81ef4a9SWill Deacon 
415f81ef4a9SWill Deacon 	switch (hbp_len) {
416f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
417f81ef4a9SWill Deacon 		len_in_bytes = 1;
418f81ef4a9SWill Deacon 		break;
419f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
420f81ef4a9SWill Deacon 		len_in_bytes = 2;
421f81ef4a9SWill Deacon 		break;
422f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
423f81ef4a9SWill Deacon 		len_in_bytes = 4;
424f81ef4a9SWill Deacon 		break;
425f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
426f81ef4a9SWill Deacon 		len_in_bytes = 8;
427f81ef4a9SWill Deacon 		break;
428f81ef4a9SWill Deacon 	}
429f81ef4a9SWill Deacon 
430f81ef4a9SWill Deacon 	return len_in_bytes;
431f81ef4a9SWill Deacon }
432f81ef4a9SWill Deacon 
433f81ef4a9SWill Deacon /*
434f81ef4a9SWill Deacon  * Check whether bp virtual address is in kernel space.
435f81ef4a9SWill Deacon  */
436f81ef4a9SWill Deacon int arch_check_bp_in_kernelspace(struct perf_event *bp)
437f81ef4a9SWill Deacon {
438f81ef4a9SWill Deacon 	unsigned int len;
439f81ef4a9SWill Deacon 	unsigned long va;
440f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
441f81ef4a9SWill Deacon 
442f81ef4a9SWill Deacon 	va = info->address;
443f81ef4a9SWill Deacon 	len = get_hbp_len(info->ctrl.len);
444f81ef4a9SWill Deacon 
445f81ef4a9SWill Deacon 	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
446f81ef4a9SWill Deacon }
447f81ef4a9SWill Deacon 
448f81ef4a9SWill Deacon /*
449f81ef4a9SWill Deacon  * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
450f81ef4a9SWill Deacon  * Hopefully this will disappear when ptrace can bypass the conversion
451f81ef4a9SWill Deacon  * to generic breakpoint descriptions.
452f81ef4a9SWill Deacon  */
453f81ef4a9SWill Deacon int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
454f81ef4a9SWill Deacon 			   int *gen_len, int *gen_type)
455f81ef4a9SWill Deacon {
456f81ef4a9SWill Deacon 	/* Type */
457f81ef4a9SWill Deacon 	switch (ctrl.type) {
458f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_EXECUTE:
459f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_X;
460f81ef4a9SWill Deacon 		break;
461f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD:
462f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_R;
463f81ef4a9SWill Deacon 		break;
464f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_STORE:
465f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_W;
466f81ef4a9SWill Deacon 		break;
467f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
468f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_RW;
469f81ef4a9SWill Deacon 		break;
470f81ef4a9SWill Deacon 	default:
471f81ef4a9SWill Deacon 		return -EINVAL;
472f81ef4a9SWill Deacon 	}
473f81ef4a9SWill Deacon 
474f81ef4a9SWill Deacon 	/* Len */
475f81ef4a9SWill Deacon 	switch (ctrl.len) {
476f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
477f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_1;
478f81ef4a9SWill Deacon 		break;
479f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
480f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_2;
481f81ef4a9SWill Deacon 		break;
482f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
483f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_4;
484f81ef4a9SWill Deacon 		break;
485f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
486f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_8;
487f81ef4a9SWill Deacon 		break;
488f81ef4a9SWill Deacon 	default:
489f81ef4a9SWill Deacon 		return -EINVAL;
490f81ef4a9SWill Deacon 	}
491f81ef4a9SWill Deacon 
492f81ef4a9SWill Deacon 	return 0;
493f81ef4a9SWill Deacon }
494f81ef4a9SWill Deacon 
495f81ef4a9SWill Deacon /*
496f81ef4a9SWill Deacon  * Construct an arch_hw_breakpoint from a perf_event.
497f81ef4a9SWill Deacon  */
498f81ef4a9SWill Deacon static int arch_build_bp_info(struct perf_event *bp)
499f81ef4a9SWill Deacon {
500f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
501f81ef4a9SWill Deacon 
502f81ef4a9SWill Deacon 	/* Type */
503f81ef4a9SWill Deacon 	switch (bp->attr.bp_type) {
504f81ef4a9SWill Deacon 	case HW_BREAKPOINT_X:
505f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
506f81ef4a9SWill Deacon 		break;
507f81ef4a9SWill Deacon 	case HW_BREAKPOINT_R:
508f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD;
509f81ef4a9SWill Deacon 		break;
510f81ef4a9SWill Deacon 	case HW_BREAKPOINT_W:
511f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_STORE;
512f81ef4a9SWill Deacon 		break;
513f81ef4a9SWill Deacon 	case HW_BREAKPOINT_RW:
514f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
515f81ef4a9SWill Deacon 		break;
516f81ef4a9SWill Deacon 	default:
517f81ef4a9SWill Deacon 		return -EINVAL;
518f81ef4a9SWill Deacon 	}
519f81ef4a9SWill Deacon 
520f81ef4a9SWill Deacon 	/* Len */
521f81ef4a9SWill Deacon 	switch (bp->attr.bp_len) {
522f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_1:
523f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_1;
524f81ef4a9SWill Deacon 		break;
525f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_2:
526f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_2;
527f81ef4a9SWill Deacon 		break;
528f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_4:
529f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_4;
530f81ef4a9SWill Deacon 		break;
531f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_8:
532f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_8;
533f81ef4a9SWill Deacon 		if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
534f81ef4a9SWill Deacon 			&& max_watchpoint_len >= 8)
535f81ef4a9SWill Deacon 			break;
536f81ef4a9SWill Deacon 	default:
537f81ef4a9SWill Deacon 		return -EINVAL;
538f81ef4a9SWill Deacon 	}
539f81ef4a9SWill Deacon 
540f81ef4a9SWill Deacon 	/* Address */
541f81ef4a9SWill Deacon 	info->address = bp->attr.bp_addr;
542f81ef4a9SWill Deacon 
543f81ef4a9SWill Deacon 	/* Privilege */
544f81ef4a9SWill Deacon 	info->ctrl.privilege = ARM_BREAKPOINT_USER;
545f81ef4a9SWill Deacon 	if (arch_check_bp_in_kernelspace(bp) && !bp_is_single_step(bp))
546f81ef4a9SWill Deacon 		info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
547f81ef4a9SWill Deacon 
548f81ef4a9SWill Deacon 	/* Enabled? */
549f81ef4a9SWill Deacon 	info->ctrl.enabled = !bp->attr.disabled;
550f81ef4a9SWill Deacon 
551f81ef4a9SWill Deacon 	/* Mismatch */
552f81ef4a9SWill Deacon 	info->ctrl.mismatch = 0;
553f81ef4a9SWill Deacon 
554f81ef4a9SWill Deacon 	return 0;
555f81ef4a9SWill Deacon }
556f81ef4a9SWill Deacon 
557f81ef4a9SWill Deacon /*
558f81ef4a9SWill Deacon  * Validate the arch-specific HW Breakpoint register settings.
559f81ef4a9SWill Deacon  */
560f81ef4a9SWill Deacon int arch_validate_hwbkpt_settings(struct perf_event *bp)
561f81ef4a9SWill Deacon {
562f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
563f81ef4a9SWill Deacon 	int ret = 0;
564f81ef4a9SWill Deacon 	u32 bytelen, max_len, offset, alignment_mask = 0x3;
565f81ef4a9SWill Deacon 
566f81ef4a9SWill Deacon 	/* Build the arch_hw_breakpoint. */
567f81ef4a9SWill Deacon 	ret = arch_build_bp_info(bp);
568f81ef4a9SWill Deacon 	if (ret)
569f81ef4a9SWill Deacon 		goto out;
570f81ef4a9SWill Deacon 
571f81ef4a9SWill Deacon 	/* Check address alignment. */
572f81ef4a9SWill Deacon 	if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
573f81ef4a9SWill Deacon 		alignment_mask = 0x7;
574f81ef4a9SWill Deacon 	if (info->address & alignment_mask) {
575f81ef4a9SWill Deacon 		/*
576f81ef4a9SWill Deacon 		 * Try to fix the alignment. This may result in a length
577f81ef4a9SWill Deacon 		 * that is too large, so we must check for that.
578f81ef4a9SWill Deacon 		 */
579f81ef4a9SWill Deacon 		bytelen = get_hbp_len(info->ctrl.len);
580f81ef4a9SWill Deacon 		max_len = info->ctrl.type == ARM_BREAKPOINT_EXECUTE ? 4 :
581f81ef4a9SWill Deacon 				max_watchpoint_len;
582f81ef4a9SWill Deacon 
583f81ef4a9SWill Deacon 		if (max_len >= 8)
584f81ef4a9SWill Deacon 			offset = info->address & 0x7;
585f81ef4a9SWill Deacon 		else
586f81ef4a9SWill Deacon 			offset = info->address & 0x3;
587f81ef4a9SWill Deacon 
588f81ef4a9SWill Deacon 		if (bytelen > (1 << ((max_len - (offset + 1)) >> 1))) {
589f81ef4a9SWill Deacon 			ret = -EFBIG;
590f81ef4a9SWill Deacon 			goto out;
591f81ef4a9SWill Deacon 		}
592f81ef4a9SWill Deacon 
593f81ef4a9SWill Deacon 		info->ctrl.len <<= offset;
594f81ef4a9SWill Deacon 		info->address &= ~offset;
595f81ef4a9SWill Deacon 
596f81ef4a9SWill Deacon 		pr_debug("breakpoint alignment fixup: length = 0x%x, "
597f81ef4a9SWill Deacon 			"address = 0x%x\n", info->ctrl.len, info->address);
598f81ef4a9SWill Deacon 	}
599f81ef4a9SWill Deacon 
600f81ef4a9SWill Deacon 	/*
601f81ef4a9SWill Deacon 	 * Currently we rely on an overflow handler to take
602f81ef4a9SWill Deacon 	 * care of single-stepping the breakpoint when it fires.
603f81ef4a9SWill Deacon 	 * In the case of userspace breakpoints on a core with V7 debug,
604f81ef4a9SWill Deacon 	 * we can use the mismatch feature as a poor-man's hardware single-step.
605f81ef4a9SWill Deacon 	 */
606f81ef4a9SWill Deacon 	if (WARN_ONCE(!bp->overflow_handler &&
607f81ef4a9SWill Deacon 		(arch_check_bp_in_kernelspace(bp) || !core_has_mismatch_bps()),
608f81ef4a9SWill Deacon 			"overflow handler required but none found")) {
609f81ef4a9SWill Deacon 		ret = -EINVAL;
610f81ef4a9SWill Deacon 		goto out;
611f81ef4a9SWill Deacon 	}
612f81ef4a9SWill Deacon out:
613f81ef4a9SWill Deacon 	return ret;
614f81ef4a9SWill Deacon }
615f81ef4a9SWill Deacon 
616f81ef4a9SWill Deacon static void update_mismatch_flag(int idx, int flag)
617f81ef4a9SWill Deacon {
618f81ef4a9SWill Deacon 	struct perf_event *bp = __get_cpu_var(bp_on_reg[idx]);
619f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
620f81ef4a9SWill Deacon 
621f81ef4a9SWill Deacon 	if (bp == NULL)
622f81ef4a9SWill Deacon 		return;
623f81ef4a9SWill Deacon 
624f81ef4a9SWill Deacon 	info = counter_arch_bp(bp);
625f81ef4a9SWill Deacon 
626f81ef4a9SWill Deacon 	/* Update the mismatch field to enter/exit `single-step' mode */
627f81ef4a9SWill Deacon 	if (!bp->overflow_handler && info->ctrl.mismatch != flag) {
628f81ef4a9SWill Deacon 		info->ctrl.mismatch = flag;
629f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BCR + idx, encode_ctrl_reg(info->ctrl) | 0x1);
630f81ef4a9SWill Deacon 	}
631f81ef4a9SWill Deacon }
632f81ef4a9SWill Deacon 
633f81ef4a9SWill Deacon static void watchpoint_handler(unsigned long unknown, struct pt_regs *regs)
634f81ef4a9SWill Deacon {
635f81ef4a9SWill Deacon 	int i;
636f81ef4a9SWill Deacon 	struct perf_event *bp, **slots = __get_cpu_var(wp_on_reg);
637f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
638f81ef4a9SWill Deacon 	struct perf_event_attr attr;
639f81ef4a9SWill Deacon 
640f81ef4a9SWill Deacon 	/* Without a disassembler, we can only handle 1 watchpoint. */
641f81ef4a9SWill Deacon 	BUG_ON(core_num_wrps > 1);
642f81ef4a9SWill Deacon 
643f81ef4a9SWill Deacon 	hw_breakpoint_init(&attr);
644f81ef4a9SWill Deacon 	attr.bp_addr	= regs->ARM_pc & ~0x3;
645f81ef4a9SWill Deacon 	attr.bp_len	= HW_BREAKPOINT_LEN_4;
646f81ef4a9SWill Deacon 	attr.bp_type	= HW_BREAKPOINT_X;
647f81ef4a9SWill Deacon 
648f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
649f81ef4a9SWill Deacon 		rcu_read_lock();
650f81ef4a9SWill Deacon 
651f81ef4a9SWill Deacon 		if (slots[i] == NULL) {
652f81ef4a9SWill Deacon 			rcu_read_unlock();
653f81ef4a9SWill Deacon 			continue;
654f81ef4a9SWill Deacon 		}
655f81ef4a9SWill Deacon 
656f81ef4a9SWill Deacon 		/*
657f81ef4a9SWill Deacon 		 * The DFAR is an unknown value. Since we only allow a
658f81ef4a9SWill Deacon 		 * single watchpoint, we can set the trigger to the lowest
659f81ef4a9SWill Deacon 		 * possible faulting address.
660f81ef4a9SWill Deacon 		 */
661f81ef4a9SWill Deacon 		info = counter_arch_bp(slots[i]);
662f81ef4a9SWill Deacon 		info->trigger = slots[i]->attr.bp_addr;
663f81ef4a9SWill Deacon 		pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
664f81ef4a9SWill Deacon 		perf_bp_event(slots[i], regs);
665f81ef4a9SWill Deacon 
666f81ef4a9SWill Deacon 		/*
667f81ef4a9SWill Deacon 		 * If no overflow handler is present, insert a temporary
668f81ef4a9SWill Deacon 		 * mismatch breakpoint so we can single-step over the
669f81ef4a9SWill Deacon 		 * watchpoint trigger.
670f81ef4a9SWill Deacon 		 */
671f81ef4a9SWill Deacon 		if (!slots[i]->overflow_handler) {
672f81ef4a9SWill Deacon 			bp = register_user_hw_breakpoint(&attr,
673f81ef4a9SWill Deacon 							 wp_single_step_handler,
674f81ef4a9SWill Deacon 							 current);
675f81ef4a9SWill Deacon 			counter_arch_bp(bp)->suspended_wp = slots[i];
676f81ef4a9SWill Deacon 			perf_event_disable(slots[i]);
677f81ef4a9SWill Deacon 		}
678f81ef4a9SWill Deacon 
679f81ef4a9SWill Deacon 		rcu_read_unlock();
680f81ef4a9SWill Deacon 	}
681f81ef4a9SWill Deacon }
682f81ef4a9SWill Deacon 
683f81ef4a9SWill Deacon static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
684f81ef4a9SWill Deacon {
685f81ef4a9SWill Deacon 	int i;
686f81ef4a9SWill Deacon 	int mismatch;
687f81ef4a9SWill Deacon 	u32 ctrl_reg, val, addr;
688f81ef4a9SWill Deacon 	struct perf_event *bp, **slots = __get_cpu_var(bp_on_reg);
689f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
690f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
691f81ef4a9SWill Deacon 
692f81ef4a9SWill Deacon 	/* The exception entry code places the amended lr in the PC. */
693f81ef4a9SWill Deacon 	addr = regs->ARM_pc;
694f81ef4a9SWill Deacon 
695f81ef4a9SWill Deacon 	for (i = 0; i < core_num_brps; ++i) {
696f81ef4a9SWill Deacon 		rcu_read_lock();
697f81ef4a9SWill Deacon 
698f81ef4a9SWill Deacon 		bp = slots[i];
699f81ef4a9SWill Deacon 
700f81ef4a9SWill Deacon 		if (bp == NULL) {
701f81ef4a9SWill Deacon 			rcu_read_unlock();
702f81ef4a9SWill Deacon 			continue;
703f81ef4a9SWill Deacon 		}
704f81ef4a9SWill Deacon 
705f81ef4a9SWill Deacon 		mismatch = 0;
706f81ef4a9SWill Deacon 
707f81ef4a9SWill Deacon 		/* Check if the breakpoint value matches. */
708f81ef4a9SWill Deacon 		val = read_wb_reg(ARM_BASE_BVR + i);
709f81ef4a9SWill Deacon 		if (val != (addr & ~0x3))
710f81ef4a9SWill Deacon 			goto unlock;
711f81ef4a9SWill Deacon 
712f81ef4a9SWill Deacon 		/* Possible match, check the byte address select to confirm. */
713f81ef4a9SWill Deacon 		ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
714f81ef4a9SWill Deacon 		decode_ctrl_reg(ctrl_reg, &ctrl);
715f81ef4a9SWill Deacon 		if ((1 << (addr & 0x3)) & ctrl.len) {
716f81ef4a9SWill Deacon 			mismatch = 1;
717f81ef4a9SWill Deacon 			info = counter_arch_bp(bp);
718f81ef4a9SWill Deacon 			info->trigger = addr;
719f81ef4a9SWill Deacon 		}
720f81ef4a9SWill Deacon 
721f81ef4a9SWill Deacon unlock:
722f81ef4a9SWill Deacon 		if ((mismatch && !info->ctrl.mismatch) || bp_is_single_step(bp)) {
723f81ef4a9SWill Deacon 			pr_debug("breakpoint fired: address = 0x%x\n", addr);
724f81ef4a9SWill Deacon 			perf_bp_event(bp, regs);
725f81ef4a9SWill Deacon 		}
726f81ef4a9SWill Deacon 
727f81ef4a9SWill Deacon 		update_mismatch_flag(i, mismatch);
728f81ef4a9SWill Deacon 		rcu_read_unlock();
729f81ef4a9SWill Deacon 	}
730f81ef4a9SWill Deacon }
731f81ef4a9SWill Deacon 
732f81ef4a9SWill Deacon /*
733f81ef4a9SWill Deacon  * Called from either the Data Abort Handler [watchpoint] or the
734f81ef4a9SWill Deacon  * Prefetch Abort Handler [breakpoint].
735f81ef4a9SWill Deacon  */
736f81ef4a9SWill Deacon static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
737f81ef4a9SWill Deacon 				 struct pt_regs *regs)
738f81ef4a9SWill Deacon {
739f81ef4a9SWill Deacon 	int ret = 1; /* Unhandled fault. */
740f81ef4a9SWill Deacon 	u32 dscr;
741f81ef4a9SWill Deacon 
742f81ef4a9SWill Deacon 	/* We only handle watchpoints and hardware breakpoints. */
743f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
744f81ef4a9SWill Deacon 
745f81ef4a9SWill Deacon 	/* Perform perf callbacks. */
746f81ef4a9SWill Deacon 	switch (ARM_DSCR_MOE(dscr)) {
747f81ef4a9SWill Deacon 	case ARM_ENTRY_BREAKPOINT:
748f81ef4a9SWill Deacon 		breakpoint_handler(addr, regs);
749f81ef4a9SWill Deacon 		break;
750f81ef4a9SWill Deacon 	case ARM_ENTRY_ASYNC_WATCHPOINT:
751235584b6SJoe Perches 		WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
752f81ef4a9SWill Deacon 	case ARM_ENTRY_SYNC_WATCHPOINT:
753f81ef4a9SWill Deacon 		watchpoint_handler(addr, regs);
754f81ef4a9SWill Deacon 		break;
755f81ef4a9SWill Deacon 	default:
756f81ef4a9SWill Deacon 		goto out;
757f81ef4a9SWill Deacon 	}
758f81ef4a9SWill Deacon 
759f81ef4a9SWill Deacon 	ret = 0;
760f81ef4a9SWill Deacon out:
761f81ef4a9SWill Deacon 	return ret;
762f81ef4a9SWill Deacon }
763f81ef4a9SWill Deacon 
764f81ef4a9SWill Deacon /*
765f81ef4a9SWill Deacon  * One-time initialisation.
766f81ef4a9SWill Deacon  */
767*7d99331eSWill Deacon static void reset_ctrl_regs(void *unused)
768f81ef4a9SWill Deacon {
769f81ef4a9SWill Deacon 	int i;
770f81ef4a9SWill Deacon 
771ac88e071SWill Deacon 	/*
772ac88e071SWill Deacon 	 * v7 debug contains save and restore registers so that debug state
773ac88e071SWill Deacon 	 * can be maintained across low-power modes without leaving
774ac88e071SWill Deacon 	 * the debug logic powered up. It is IMPLEMENTATION DEFINED whether
775ac88e071SWill Deacon 	 * we can write to the debug registers out of reset, so we must
776ac88e071SWill Deacon 	 * unlock the OS Lock Access Register to avoid taking undefined
777ac88e071SWill Deacon 	 * instruction exceptions later on.
778ac88e071SWill Deacon 	 */
779ac88e071SWill Deacon 	if (debug_arch >= ARM_DEBUG_ARCH_V7_ECP14) {
780ac88e071SWill Deacon 		/*
781ac88e071SWill Deacon 		 * Unconditionally clear the lock by writing a value
782ac88e071SWill Deacon 		 * other than 0xC5ACCE55 to the access register.
783ac88e071SWill Deacon 		 */
784ac88e071SWill Deacon 		asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0));
785ac88e071SWill Deacon 		isb();
786ac88e071SWill Deacon 	}
787ac88e071SWill Deacon 
788f81ef4a9SWill Deacon 	if (enable_monitor_mode())
789f81ef4a9SWill Deacon 		return;
790f81ef4a9SWill Deacon 
791f81ef4a9SWill Deacon 	for (i = 0; i < core_num_brps; ++i) {
792f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BCR + i, 0UL);
793f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BVR + i, 0UL);
794f81ef4a9SWill Deacon 	}
795f81ef4a9SWill Deacon 
796f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
797f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WCR + i, 0UL);
798f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WVR + i, 0UL);
799f81ef4a9SWill Deacon 	}
800f81ef4a9SWill Deacon }
801f81ef4a9SWill Deacon 
802*7d99331eSWill Deacon static int __cpuinit dbg_reset_notify(struct notifier_block *self,
803*7d99331eSWill Deacon 				      unsigned long action, void *cpu)
804*7d99331eSWill Deacon {
805*7d99331eSWill Deacon 	if (action == CPU_ONLINE)
806*7d99331eSWill Deacon 		smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
807*7d99331eSWill Deacon 	return NOTIFY_OK;
808*7d99331eSWill Deacon }
809*7d99331eSWill Deacon 
810*7d99331eSWill Deacon static struct notifier_block __cpuinitdata dbg_reset_nb = {
811*7d99331eSWill Deacon 	.notifier_call = dbg_reset_notify,
812*7d99331eSWill Deacon };
813*7d99331eSWill Deacon 
814f81ef4a9SWill Deacon static int __init arch_hw_breakpoint_init(void)
815f81ef4a9SWill Deacon {
816f81ef4a9SWill Deacon 	int ret = 0;
817f81ef4a9SWill Deacon 	u32 dscr;
818f81ef4a9SWill Deacon 
819f81ef4a9SWill Deacon 	debug_arch = get_debug_arch();
820f81ef4a9SWill Deacon 
821f81ef4a9SWill Deacon 	if (debug_arch > ARM_DEBUG_ARCH_V7_ECP14) {
822f81ef4a9SWill Deacon 		pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
823f81ef4a9SWill Deacon 		ret = -ENODEV;
824f81ef4a9SWill Deacon 		goto out;
825f81ef4a9SWill Deacon 	}
826f81ef4a9SWill Deacon 
827f81ef4a9SWill Deacon 	/* Determine how many BRPs/WRPs are available. */
828f81ef4a9SWill Deacon 	core_num_brps = get_num_brps();
829f81ef4a9SWill Deacon 	core_num_wrps = get_num_wrps();
830f81ef4a9SWill Deacon 
831f81ef4a9SWill Deacon 	pr_info("found %d breakpoint and %d watchpoint registers.\n",
832f81ef4a9SWill Deacon 			core_num_brps, core_num_wrps);
833f81ef4a9SWill Deacon 
834f81ef4a9SWill Deacon 	if (core_has_mismatch_bps())
835f81ef4a9SWill Deacon 		pr_info("1 breakpoint reserved for watchpoint single-step.\n");
836f81ef4a9SWill Deacon 
837f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
838f81ef4a9SWill Deacon 	if (dscr & ARM_DSCR_HDBGEN) {
839f81ef4a9SWill Deacon 		pr_warning("halting debug mode enabled. Assuming maximum "
840f81ef4a9SWill Deacon 				"watchpoint size of 4 bytes.");
841f81ef4a9SWill Deacon 	} else {
842f81ef4a9SWill Deacon 		/*
843f81ef4a9SWill Deacon 		 * Reset the breakpoint resources. We assume that a halting
844f81ef4a9SWill Deacon 		 * debugger will leave the world in a nice state for us.
845f81ef4a9SWill Deacon 		 */
846f81ef4a9SWill Deacon 		smp_call_function(reset_ctrl_regs, NULL, 1);
847f81ef4a9SWill Deacon 		reset_ctrl_regs(NULL);
848ac88e071SWill Deacon 
849ac88e071SWill Deacon 		/* Work out the maximum supported watchpoint length. */
850ac88e071SWill Deacon 		max_watchpoint_len = get_max_wp_len();
851ac88e071SWill Deacon 		pr_info("maximum watchpoint size is %u bytes.\n",
852ac88e071SWill Deacon 				max_watchpoint_len);
853f81ef4a9SWill Deacon 	}
854f81ef4a9SWill Deacon 
855f81ef4a9SWill Deacon 	/* Register debug fault handler. */
856f81ef4a9SWill Deacon 	hook_fault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
857f81ef4a9SWill Deacon 			"watchpoint debug exception");
858f81ef4a9SWill Deacon 	hook_ifault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
859f81ef4a9SWill Deacon 			"breakpoint debug exception");
860f81ef4a9SWill Deacon 
861*7d99331eSWill Deacon 	/* Register hotplug notifier. */
862*7d99331eSWill Deacon 	register_cpu_notifier(&dbg_reset_nb);
863f81ef4a9SWill Deacon out:
864f81ef4a9SWill Deacon 	return ret;
865f81ef4a9SWill Deacon }
866f81ef4a9SWill Deacon arch_initcall(arch_hw_breakpoint_init);
867f81ef4a9SWill Deacon 
868f81ef4a9SWill Deacon void hw_breakpoint_pmu_read(struct perf_event *bp)
869f81ef4a9SWill Deacon {
870f81ef4a9SWill Deacon }
871f81ef4a9SWill Deacon 
872f81ef4a9SWill Deacon /*
873f81ef4a9SWill Deacon  * Dummy function to register with die_notifier.
874f81ef4a9SWill Deacon  */
875f81ef4a9SWill Deacon int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
876f81ef4a9SWill Deacon 					unsigned long val, void *data)
877f81ef4a9SWill Deacon {
878f81ef4a9SWill Deacon 	return NOTIFY_DONE;
879f81ef4a9SWill Deacon }
880