xref: /openbmc/linux/arch/arm/kernel/hw_breakpoint.c (revision d12443363e590461655d4e9ccc31e40ad9078283)
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>
277e202696SWill Deacon #include <linux/hardirq.h>
28f81ef4a9SWill Deacon #include <linux/perf_event.h>
29f81ef4a9SWill Deacon #include <linux/hw_breakpoint.h>
30f81ef4a9SWill Deacon #include <linux/smp.h>
31f81ef4a9SWill Deacon 
32f81ef4a9SWill Deacon #include <asm/cacheflush.h>
33f81ef4a9SWill Deacon #include <asm/cputype.h>
34f81ef4a9SWill Deacon #include <asm/current.h>
35f81ef4a9SWill Deacon #include <asm/hw_breakpoint.h>
36f81ef4a9SWill Deacon #include <asm/kdebug.h>
37f81ef4a9SWill Deacon #include <asm/system.h>
38f81ef4a9SWill Deacon #include <asm/traps.h>
39f81ef4a9SWill Deacon 
40f81ef4a9SWill Deacon /* Breakpoint currently in use for each BRP. */
41f81ef4a9SWill Deacon static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
42f81ef4a9SWill Deacon 
43f81ef4a9SWill Deacon /* Watchpoint currently in use for each WRP. */
44f81ef4a9SWill Deacon static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
45f81ef4a9SWill Deacon 
46f81ef4a9SWill Deacon /* Number of BRP/WRP registers on this CPU. */
47f81ef4a9SWill Deacon static int core_num_brps;
48f81ef4a9SWill Deacon static int core_num_wrps;
49f81ef4a9SWill Deacon 
50f81ef4a9SWill Deacon /* Debug architecture version. */
51f81ef4a9SWill Deacon static u8 debug_arch;
52f81ef4a9SWill Deacon 
53f81ef4a9SWill Deacon /* Maximum supported watchpoint length. */
54f81ef4a9SWill Deacon static u8 max_watchpoint_len;
55f81ef4a9SWill Deacon 
56f81ef4a9SWill Deacon #define READ_WB_REG_CASE(OP2, M, VAL)		\
57f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):			\
58f81ef4a9SWill Deacon 		ARM_DBG_READ(c ## M, OP2, VAL); \
59f81ef4a9SWill Deacon 		break
60f81ef4a9SWill Deacon 
61f81ef4a9SWill Deacon #define WRITE_WB_REG_CASE(OP2, M, VAL)		\
62f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):			\
63f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c ## M, OP2, VAL);\
64f81ef4a9SWill Deacon 		break
65f81ef4a9SWill Deacon 
66f81ef4a9SWill Deacon #define GEN_READ_WB_REG_CASES(OP2, VAL)		\
67f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 0, VAL);		\
68f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 1, VAL);		\
69f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 2, VAL);		\
70f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 3, VAL);		\
71f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 4, VAL);		\
72f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 5, VAL);		\
73f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 6, VAL);		\
74f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 7, VAL);		\
75f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 8, VAL);		\
76f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 9, VAL);		\
77f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 10, VAL);		\
78f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 11, VAL);		\
79f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 12, VAL);		\
80f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 13, VAL);		\
81f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 14, VAL);		\
82f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 15, VAL)
83f81ef4a9SWill Deacon 
84f81ef4a9SWill Deacon #define GEN_WRITE_WB_REG_CASES(OP2, VAL)	\
85f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 0, VAL);		\
86f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 1, VAL);		\
87f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 2, VAL);		\
88f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 3, VAL);		\
89f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 4, VAL);		\
90f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 5, VAL);		\
91f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 6, VAL);		\
92f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 7, VAL);		\
93f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 8, VAL);		\
94f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 9, VAL);		\
95f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 10, VAL);	\
96f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 11, VAL);	\
97f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 12, VAL);	\
98f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 13, VAL);	\
99f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 14, VAL);	\
100f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 15, VAL)
101f81ef4a9SWill Deacon 
102f81ef4a9SWill Deacon static u32 read_wb_reg(int n)
103f81ef4a9SWill Deacon {
104f81ef4a9SWill Deacon 	u32 val = 0;
105f81ef4a9SWill Deacon 
106f81ef4a9SWill Deacon 	switch (n) {
107f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
108f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
109f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
110f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
111f81ef4a9SWill Deacon 	default:
112f81ef4a9SWill Deacon 		pr_warning("attempt to read from unknown breakpoint "
113f81ef4a9SWill Deacon 				"register %d\n", n);
114f81ef4a9SWill Deacon 	}
115f81ef4a9SWill Deacon 
116f81ef4a9SWill Deacon 	return val;
117f81ef4a9SWill Deacon }
118f81ef4a9SWill Deacon 
119f81ef4a9SWill Deacon static void write_wb_reg(int n, u32 val)
120f81ef4a9SWill Deacon {
121f81ef4a9SWill Deacon 	switch (n) {
122f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
123f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
124f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
125f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
126f81ef4a9SWill Deacon 	default:
127f81ef4a9SWill Deacon 		pr_warning("attempt to write to unknown breakpoint "
128f81ef4a9SWill Deacon 				"register %d\n", n);
129f81ef4a9SWill Deacon 	}
130f81ef4a9SWill Deacon 	isb();
131f81ef4a9SWill Deacon }
132f81ef4a9SWill Deacon 
1330017ff42SWill Deacon /* Determine debug architecture. */
1340017ff42SWill Deacon static u8 get_debug_arch(void)
1350017ff42SWill Deacon {
1360017ff42SWill Deacon 	u32 didr;
1370017ff42SWill Deacon 
1380017ff42SWill Deacon 	/* Do we implement the extended CPUID interface? */
139*d1244336SWill Deacon 	if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
140*d1244336SWill Deacon 		pr_warning("CPUID feature registers not supported. "
141*d1244336SWill Deacon 			   "Assuming v6 debug is present.\n");
1420017ff42SWill Deacon 		return ARM_DEBUG_ARCH_V6;
143*d1244336SWill Deacon 	}
1440017ff42SWill Deacon 
1450017ff42SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
1460017ff42SWill Deacon 	return (didr >> 16) & 0xf;
1470017ff42SWill Deacon }
1480017ff42SWill Deacon 
1490017ff42SWill Deacon u8 arch_get_debug_arch(void)
1500017ff42SWill Deacon {
1510017ff42SWill Deacon 	return debug_arch;
1520017ff42SWill Deacon }
1530017ff42SWill Deacon 
15466e1cfe6SWill Deacon static int debug_arch_supported(void)
15566e1cfe6SWill Deacon {
15666e1cfe6SWill Deacon 	u8 arch = get_debug_arch();
157b5d5b8f9SWill Deacon 
158b5d5b8f9SWill Deacon 	/* We don't support the memory-mapped interface. */
159b5d5b8f9SWill Deacon 	return (arch >= ARM_DEBUG_ARCH_V6 && arch <= ARM_DEBUG_ARCH_V7_ECP14) ||
160b5d5b8f9SWill Deacon 		arch >= ARM_DEBUG_ARCH_V7_1;
16166e1cfe6SWill Deacon }
16266e1cfe6SWill Deacon 
163c512de95SWill Deacon /* Determine number of WRP registers available. */
164c512de95SWill Deacon static int get_num_wrp_resources(void)
165c512de95SWill Deacon {
166c512de95SWill Deacon 	u32 didr;
167c512de95SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
168c512de95SWill Deacon 	return ((didr >> 28) & 0xf) + 1;
169c512de95SWill Deacon }
170c512de95SWill Deacon 
171c512de95SWill Deacon /* Determine number of BRP registers available. */
1720017ff42SWill Deacon static int get_num_brp_resources(void)
1730017ff42SWill Deacon {
1740017ff42SWill Deacon 	u32 didr;
1750017ff42SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
1760017ff42SWill Deacon 	return ((didr >> 24) & 0xf) + 1;
1770017ff42SWill Deacon }
1780017ff42SWill Deacon 
1790017ff42SWill Deacon /* Does this core support mismatch breakpoints? */
1800017ff42SWill Deacon static int core_has_mismatch_brps(void)
1810017ff42SWill Deacon {
1820017ff42SWill Deacon 	return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
1830017ff42SWill Deacon 		get_num_brp_resources() > 1);
1840017ff42SWill Deacon }
1850017ff42SWill Deacon 
1860017ff42SWill Deacon /* Determine number of usable WRPs available. */
1870017ff42SWill Deacon static int get_num_wrps(void)
1880017ff42SWill Deacon {
1890017ff42SWill Deacon 	/*
190c512de95SWill Deacon 	 * On debug architectures prior to 7.1, when a watchpoint fires, the
191c512de95SWill Deacon 	 * only way to work out which watchpoint it was is by disassembling
192c512de95SWill Deacon 	 * the faulting instruction and working out the address of the memory
193c512de95SWill Deacon 	 * access.
1940017ff42SWill Deacon 	 *
1950017ff42SWill Deacon 	 * Furthermore, we can only do this if the watchpoint was precise
1960017ff42SWill Deacon 	 * since imprecise watchpoints prevent us from calculating register
1970017ff42SWill Deacon 	 * based addresses.
1980017ff42SWill Deacon 	 *
1990017ff42SWill Deacon 	 * Providing we have more than 1 breakpoint register, we only report
2000017ff42SWill Deacon 	 * a single watchpoint register for the time being. This way, we always
2010017ff42SWill Deacon 	 * know which watchpoint fired. In the future we can either add a
2020017ff42SWill Deacon 	 * disassembler and address generation emulator, or we can insert a
2030017ff42SWill Deacon 	 * check to see if the DFAR is set on watchpoint exception entry
2040017ff42SWill Deacon 	 * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
2050017ff42SWill Deacon 	 * that it is set on some implementations].
2060017ff42SWill Deacon 	 */
207c512de95SWill Deacon 	if (get_debug_arch() < ARM_DEBUG_ARCH_V7_1)
208c512de95SWill Deacon 		return 1;
2090017ff42SWill Deacon 
210c512de95SWill Deacon 	return get_num_wrp_resources();
2110017ff42SWill Deacon }
2120017ff42SWill Deacon 
2130017ff42SWill Deacon /* Determine number of usable BRPs available. */
2140017ff42SWill Deacon static int get_num_brps(void)
2150017ff42SWill Deacon {
2160017ff42SWill Deacon 	int brps = get_num_brp_resources();
217c512de95SWill Deacon 	return core_has_mismatch_brps() ? brps - 1 : brps;
2180017ff42SWill Deacon }
2190017ff42SWill Deacon 
220f81ef4a9SWill Deacon /*
221f81ef4a9SWill Deacon  * In order to access the breakpoint/watchpoint control registers,
222f81ef4a9SWill Deacon  * we must be running in debug monitor mode. Unfortunately, we can
223f81ef4a9SWill Deacon  * be put into halting debug mode at any time by an external debugger
224f81ef4a9SWill Deacon  * but there is nothing we can do to prevent that.
225f81ef4a9SWill Deacon  */
226f81ef4a9SWill Deacon static int enable_monitor_mode(void)
227f81ef4a9SWill Deacon {
228f81ef4a9SWill Deacon 	u32 dscr;
229f81ef4a9SWill Deacon 	int ret = 0;
230f81ef4a9SWill Deacon 
231f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
232f81ef4a9SWill Deacon 
233f81ef4a9SWill Deacon 	/* Ensure that halting mode is disabled. */
2347d85d61fSStephen Boyd 	if (WARN_ONCE(dscr & ARM_DSCR_HDBGEN,
2357d85d61fSStephen Boyd 		"halting debug mode enabled. Unable to access hardware resources.\n")) {
236f81ef4a9SWill Deacon 		ret = -EPERM;
237f81ef4a9SWill Deacon 		goto out;
238f81ef4a9SWill Deacon 	}
239f81ef4a9SWill Deacon 
2408fbf397cSWill Deacon 	/* If monitor mode is already enabled, just return. */
2418fbf397cSWill Deacon 	if (dscr & ARM_DSCR_MDBGEN)
2428fbf397cSWill Deacon 		goto out;
2438fbf397cSWill Deacon 
244f81ef4a9SWill Deacon 	/* Write to the corresponding DSCR. */
2458fbf397cSWill Deacon 	switch (get_debug_arch()) {
246f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6:
247f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6_1:
248f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN));
249f81ef4a9SWill Deacon 		break;
250f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V7_ECP14:
251b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_1:
252f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN));
253f81ef4a9SWill Deacon 		break;
254f81ef4a9SWill Deacon 	default:
255f81ef4a9SWill Deacon 		ret = -ENODEV;
256f81ef4a9SWill Deacon 		goto out;
257f81ef4a9SWill Deacon 	}
258f81ef4a9SWill Deacon 
259f81ef4a9SWill Deacon 	/* Check that the write made it through. */
260f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
2618fbf397cSWill Deacon 	if (!(dscr & ARM_DSCR_MDBGEN))
262f81ef4a9SWill Deacon 		ret = -EPERM;
263f81ef4a9SWill Deacon 
264f81ef4a9SWill Deacon out:
265f81ef4a9SWill Deacon 	return ret;
266f81ef4a9SWill Deacon }
267f81ef4a9SWill Deacon 
2688fbf397cSWill Deacon int hw_breakpoint_slots(int type)
2698fbf397cSWill Deacon {
27066e1cfe6SWill Deacon 	if (!debug_arch_supported())
27166e1cfe6SWill Deacon 		return 0;
27266e1cfe6SWill Deacon 
2738fbf397cSWill Deacon 	/*
2748fbf397cSWill Deacon 	 * We can be called early, so don't rely on
2758fbf397cSWill Deacon 	 * our static variables being initialised.
2768fbf397cSWill Deacon 	 */
2778fbf397cSWill Deacon 	switch (type) {
2788fbf397cSWill Deacon 	case TYPE_INST:
2798fbf397cSWill Deacon 		return get_num_brps();
2808fbf397cSWill Deacon 	case TYPE_DATA:
2818fbf397cSWill Deacon 		return get_num_wrps();
2828fbf397cSWill Deacon 	default:
2838fbf397cSWill Deacon 		pr_warning("unknown slot type: %d\n", type);
2848fbf397cSWill Deacon 		return 0;
2858fbf397cSWill Deacon 	}
2868fbf397cSWill Deacon }
2878fbf397cSWill Deacon 
288f81ef4a9SWill Deacon /*
289f81ef4a9SWill Deacon  * Check if 8-bit byte-address select is available.
290f81ef4a9SWill Deacon  * This clobbers WRP 0.
291f81ef4a9SWill Deacon  */
292f81ef4a9SWill Deacon static u8 get_max_wp_len(void)
293f81ef4a9SWill Deacon {
294f81ef4a9SWill Deacon 	u32 ctrl_reg;
295f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
296f81ef4a9SWill Deacon 	u8 size = 4;
297f81ef4a9SWill Deacon 
298f81ef4a9SWill Deacon 	if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
299f81ef4a9SWill Deacon 		goto out;
300f81ef4a9SWill Deacon 
301f81ef4a9SWill Deacon 	memset(&ctrl, 0, sizeof(ctrl));
302f81ef4a9SWill Deacon 	ctrl.len = ARM_BREAKPOINT_LEN_8;
303f81ef4a9SWill Deacon 	ctrl_reg = encode_ctrl_reg(ctrl);
304f81ef4a9SWill Deacon 
305f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WVR, 0);
306f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WCR, ctrl_reg);
307f81ef4a9SWill Deacon 	if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
308f81ef4a9SWill Deacon 		size = 8;
309f81ef4a9SWill Deacon 
310f81ef4a9SWill Deacon out:
311f81ef4a9SWill Deacon 	return size;
312f81ef4a9SWill Deacon }
313f81ef4a9SWill Deacon 
314f81ef4a9SWill Deacon u8 arch_get_max_wp_len(void)
315f81ef4a9SWill Deacon {
316f81ef4a9SWill Deacon 	return max_watchpoint_len;
317f81ef4a9SWill Deacon }
318f81ef4a9SWill Deacon 
319f81ef4a9SWill Deacon /*
320f81ef4a9SWill Deacon  * Install a perf counter breakpoint.
321f81ef4a9SWill Deacon  */
322f81ef4a9SWill Deacon int arch_install_hw_breakpoint(struct perf_event *bp)
323f81ef4a9SWill Deacon {
324f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
325f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
326f81ef4a9SWill Deacon 	int i, max_slots, ctrl_base, val_base, ret = 0;
32793a04a34SWill Deacon 	u32 addr, ctrl;
328f81ef4a9SWill Deacon 
329f81ef4a9SWill Deacon 	/* Ensure that we are in monitor mode and halting mode is disabled. */
330f81ef4a9SWill Deacon 	ret = enable_monitor_mode();
331f81ef4a9SWill Deacon 	if (ret)
332f81ef4a9SWill Deacon 		goto out;
333f81ef4a9SWill Deacon 
33493a04a34SWill Deacon 	addr = info->address;
33593a04a34SWill Deacon 	ctrl = encode_ctrl_reg(info->ctrl) | 0x1;
33693a04a34SWill Deacon 
337f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
338f81ef4a9SWill Deacon 		/* Breakpoint */
339f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_BCR;
340f81ef4a9SWill Deacon 		val_base = ARM_BASE_BVR;
3414a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
3420017ff42SWill Deacon 		max_slots = core_num_brps;
343f81ef4a9SWill Deacon 	} else {
344f81ef4a9SWill Deacon 		/* Watchpoint */
345f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_WCR;
346f81ef4a9SWill Deacon 		val_base = ARM_BASE_WVR;
3474a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
348f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
349f81ef4a9SWill Deacon 	}
350f81ef4a9SWill Deacon 
351f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
352f81ef4a9SWill Deacon 		slot = &slots[i];
353f81ef4a9SWill Deacon 
354f81ef4a9SWill Deacon 		if (!*slot) {
355f81ef4a9SWill Deacon 			*slot = bp;
356f81ef4a9SWill Deacon 			break;
357f81ef4a9SWill Deacon 		}
358f81ef4a9SWill Deacon 	}
359f81ef4a9SWill Deacon 
3607d85d61fSStephen Boyd 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n")) {
361f81ef4a9SWill Deacon 		ret = -EBUSY;
362f81ef4a9SWill Deacon 		goto out;
363f81ef4a9SWill Deacon 	}
364f81ef4a9SWill Deacon 
3656f26aa05SWill Deacon 	/* Override the breakpoint data with the step data. */
3666f26aa05SWill Deacon 	if (info->step_ctrl.enabled) {
3676f26aa05SWill Deacon 		addr = info->trigger & ~0x3;
3686f26aa05SWill Deacon 		ctrl = encode_ctrl_reg(info->step_ctrl);
3696f26aa05SWill Deacon 		if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE) {
3706f26aa05SWill Deacon 			i = 0;
3716f26aa05SWill Deacon 			ctrl_base = ARM_BASE_BCR + core_num_brps;
3726f26aa05SWill Deacon 			val_base = ARM_BASE_BVR + core_num_brps;
3736f26aa05SWill Deacon 		}
3746f26aa05SWill Deacon 	}
3756f26aa05SWill Deacon 
376f81ef4a9SWill Deacon 	/* Setup the address register. */
37793a04a34SWill Deacon 	write_wb_reg(val_base + i, addr);
378f81ef4a9SWill Deacon 
379f81ef4a9SWill Deacon 	/* Setup the control register. */
38093a04a34SWill Deacon 	write_wb_reg(ctrl_base + i, ctrl);
381f81ef4a9SWill Deacon 
382f81ef4a9SWill Deacon out:
383f81ef4a9SWill Deacon 	return ret;
384f81ef4a9SWill Deacon }
385f81ef4a9SWill Deacon 
386f81ef4a9SWill Deacon void arch_uninstall_hw_breakpoint(struct perf_event *bp)
387f81ef4a9SWill Deacon {
388f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
389f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
390f81ef4a9SWill Deacon 	int i, max_slots, base;
391f81ef4a9SWill Deacon 
392f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
393f81ef4a9SWill Deacon 		/* Breakpoint */
394f81ef4a9SWill Deacon 		base = ARM_BASE_BCR;
3954a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
3960017ff42SWill Deacon 		max_slots = core_num_brps;
397f81ef4a9SWill Deacon 	} else {
398f81ef4a9SWill Deacon 		/* Watchpoint */
399f81ef4a9SWill Deacon 		base = ARM_BASE_WCR;
4004a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
401f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
402f81ef4a9SWill Deacon 	}
403f81ef4a9SWill Deacon 
404f81ef4a9SWill Deacon 	/* Remove the breakpoint. */
405f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
406f81ef4a9SWill Deacon 		slot = &slots[i];
407f81ef4a9SWill Deacon 
408f81ef4a9SWill Deacon 		if (*slot == bp) {
409f81ef4a9SWill Deacon 			*slot = NULL;
410f81ef4a9SWill Deacon 			break;
411f81ef4a9SWill Deacon 		}
412f81ef4a9SWill Deacon 	}
413f81ef4a9SWill Deacon 
4147d85d61fSStephen Boyd 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n"))
415f81ef4a9SWill Deacon 		return;
416f81ef4a9SWill Deacon 
4176f26aa05SWill Deacon 	/* Ensure that we disable the mismatch breakpoint. */
4186f26aa05SWill Deacon 	if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE &&
4196f26aa05SWill Deacon 	    info->step_ctrl.enabled) {
4206f26aa05SWill Deacon 		i = 0;
4216f26aa05SWill Deacon 		base = ARM_BASE_BCR + core_num_brps;
4226f26aa05SWill Deacon 	}
4236f26aa05SWill Deacon 
424f81ef4a9SWill Deacon 	/* Reset the control register. */
425f81ef4a9SWill Deacon 	write_wb_reg(base + i, 0);
426f81ef4a9SWill Deacon }
427f81ef4a9SWill Deacon 
428f81ef4a9SWill Deacon static int get_hbp_len(u8 hbp_len)
429f81ef4a9SWill Deacon {
430f81ef4a9SWill Deacon 	unsigned int len_in_bytes = 0;
431f81ef4a9SWill Deacon 
432f81ef4a9SWill Deacon 	switch (hbp_len) {
433f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
434f81ef4a9SWill Deacon 		len_in_bytes = 1;
435f81ef4a9SWill Deacon 		break;
436f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
437f81ef4a9SWill Deacon 		len_in_bytes = 2;
438f81ef4a9SWill Deacon 		break;
439f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
440f81ef4a9SWill Deacon 		len_in_bytes = 4;
441f81ef4a9SWill Deacon 		break;
442f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
443f81ef4a9SWill Deacon 		len_in_bytes = 8;
444f81ef4a9SWill Deacon 		break;
445f81ef4a9SWill Deacon 	}
446f81ef4a9SWill Deacon 
447f81ef4a9SWill Deacon 	return len_in_bytes;
448f81ef4a9SWill Deacon }
449f81ef4a9SWill Deacon 
450f81ef4a9SWill Deacon /*
451f81ef4a9SWill Deacon  * Check whether bp virtual address is in kernel space.
452f81ef4a9SWill Deacon  */
453f81ef4a9SWill Deacon int arch_check_bp_in_kernelspace(struct perf_event *bp)
454f81ef4a9SWill Deacon {
455f81ef4a9SWill Deacon 	unsigned int len;
456f81ef4a9SWill Deacon 	unsigned long va;
457f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
458f81ef4a9SWill Deacon 
459f81ef4a9SWill Deacon 	va = info->address;
460f81ef4a9SWill Deacon 	len = get_hbp_len(info->ctrl.len);
461f81ef4a9SWill Deacon 
462f81ef4a9SWill Deacon 	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
463f81ef4a9SWill Deacon }
464f81ef4a9SWill Deacon 
465f81ef4a9SWill Deacon /*
466f81ef4a9SWill Deacon  * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
467f81ef4a9SWill Deacon  * Hopefully this will disappear when ptrace can bypass the conversion
468f81ef4a9SWill Deacon  * to generic breakpoint descriptions.
469f81ef4a9SWill Deacon  */
470f81ef4a9SWill Deacon int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
471f81ef4a9SWill Deacon 			   int *gen_len, int *gen_type)
472f81ef4a9SWill Deacon {
473f81ef4a9SWill Deacon 	/* Type */
474f81ef4a9SWill Deacon 	switch (ctrl.type) {
475f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_EXECUTE:
476f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_X;
477f81ef4a9SWill Deacon 		break;
478f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD:
479f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_R;
480f81ef4a9SWill Deacon 		break;
481f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_STORE:
482f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_W;
483f81ef4a9SWill Deacon 		break;
484f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
485f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_RW;
486f81ef4a9SWill Deacon 		break;
487f81ef4a9SWill Deacon 	default:
488f81ef4a9SWill Deacon 		return -EINVAL;
489f81ef4a9SWill Deacon 	}
490f81ef4a9SWill Deacon 
491f81ef4a9SWill Deacon 	/* Len */
492f81ef4a9SWill Deacon 	switch (ctrl.len) {
493f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
494f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_1;
495f81ef4a9SWill Deacon 		break;
496f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
497f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_2;
498f81ef4a9SWill Deacon 		break;
499f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
500f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_4;
501f81ef4a9SWill Deacon 		break;
502f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
503f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_8;
504f81ef4a9SWill Deacon 		break;
505f81ef4a9SWill Deacon 	default:
506f81ef4a9SWill Deacon 		return -EINVAL;
507f81ef4a9SWill Deacon 	}
508f81ef4a9SWill Deacon 
509f81ef4a9SWill Deacon 	return 0;
510f81ef4a9SWill Deacon }
511f81ef4a9SWill Deacon 
512f81ef4a9SWill Deacon /*
513f81ef4a9SWill Deacon  * Construct an arch_hw_breakpoint from a perf_event.
514f81ef4a9SWill Deacon  */
515f81ef4a9SWill Deacon static int arch_build_bp_info(struct perf_event *bp)
516f81ef4a9SWill Deacon {
517f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
518f81ef4a9SWill Deacon 
519f81ef4a9SWill Deacon 	/* Type */
520f81ef4a9SWill Deacon 	switch (bp->attr.bp_type) {
521f81ef4a9SWill Deacon 	case HW_BREAKPOINT_X:
522f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
523f81ef4a9SWill Deacon 		break;
524f81ef4a9SWill Deacon 	case HW_BREAKPOINT_R:
525f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD;
526f81ef4a9SWill Deacon 		break;
527f81ef4a9SWill Deacon 	case HW_BREAKPOINT_W:
528f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_STORE;
529f81ef4a9SWill Deacon 		break;
530f81ef4a9SWill Deacon 	case HW_BREAKPOINT_RW:
531f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
532f81ef4a9SWill Deacon 		break;
533f81ef4a9SWill Deacon 	default:
534f81ef4a9SWill Deacon 		return -EINVAL;
535f81ef4a9SWill Deacon 	}
536f81ef4a9SWill Deacon 
537f81ef4a9SWill Deacon 	/* Len */
538f81ef4a9SWill Deacon 	switch (bp->attr.bp_len) {
539f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_1:
540f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_1;
541f81ef4a9SWill Deacon 		break;
542f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_2:
543f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_2;
544f81ef4a9SWill Deacon 		break;
545f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_4:
546f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_4;
547f81ef4a9SWill Deacon 		break;
548f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_8:
549f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_8;
550f81ef4a9SWill Deacon 		if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
551f81ef4a9SWill Deacon 			&& max_watchpoint_len >= 8)
552f81ef4a9SWill Deacon 			break;
553f81ef4a9SWill Deacon 	default:
554f81ef4a9SWill Deacon 		return -EINVAL;
555f81ef4a9SWill Deacon 	}
556f81ef4a9SWill Deacon 
5576ee33c27SWill Deacon 	/*
5586ee33c27SWill Deacon 	 * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
5596ee33c27SWill Deacon 	 * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported
5606ee33c27SWill Deacon 	 * by the hardware and must be aligned to the appropriate number of
5616ee33c27SWill Deacon 	 * bytes.
5626ee33c27SWill Deacon 	 */
5636ee33c27SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
5646ee33c27SWill Deacon 	    info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
5656ee33c27SWill Deacon 	    info->ctrl.len != ARM_BREAKPOINT_LEN_4)
5666ee33c27SWill Deacon 		return -EINVAL;
5676ee33c27SWill Deacon 
568f81ef4a9SWill Deacon 	/* Address */
569f81ef4a9SWill Deacon 	info->address = bp->attr.bp_addr;
570f81ef4a9SWill Deacon 
571f81ef4a9SWill Deacon 	/* Privilege */
572f81ef4a9SWill Deacon 	info->ctrl.privilege = ARM_BREAKPOINT_USER;
57393a04a34SWill Deacon 	if (arch_check_bp_in_kernelspace(bp))
574f81ef4a9SWill Deacon 		info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
575f81ef4a9SWill Deacon 
576f81ef4a9SWill Deacon 	/* Enabled? */
577f81ef4a9SWill Deacon 	info->ctrl.enabled = !bp->attr.disabled;
578f81ef4a9SWill Deacon 
579f81ef4a9SWill Deacon 	/* Mismatch */
580f81ef4a9SWill Deacon 	info->ctrl.mismatch = 0;
581f81ef4a9SWill Deacon 
582f81ef4a9SWill Deacon 	return 0;
583f81ef4a9SWill Deacon }
584f81ef4a9SWill Deacon 
585f81ef4a9SWill Deacon /*
586f81ef4a9SWill Deacon  * Validate the arch-specific HW Breakpoint register settings.
587f81ef4a9SWill Deacon  */
588f81ef4a9SWill Deacon int arch_validate_hwbkpt_settings(struct perf_event *bp)
589f81ef4a9SWill Deacon {
590f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
591f81ef4a9SWill Deacon 	int ret = 0;
5926ee33c27SWill Deacon 	u32 offset, alignment_mask = 0x3;
593f81ef4a9SWill Deacon 
594f81ef4a9SWill Deacon 	/* Build the arch_hw_breakpoint. */
595f81ef4a9SWill Deacon 	ret = arch_build_bp_info(bp);
596f81ef4a9SWill Deacon 	if (ret)
597f81ef4a9SWill Deacon 		goto out;
598f81ef4a9SWill Deacon 
599f81ef4a9SWill Deacon 	/* Check address alignment. */
600f81ef4a9SWill Deacon 	if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
601f81ef4a9SWill Deacon 		alignment_mask = 0x7;
6026ee33c27SWill Deacon 	offset = info->address & alignment_mask;
6036ee33c27SWill Deacon 	switch (offset) {
6046ee33c27SWill Deacon 	case 0:
6056ee33c27SWill Deacon 		/* Aligned */
6066ee33c27SWill Deacon 		break;
6076ee33c27SWill Deacon 	case 1:
6086ee33c27SWill Deacon 		/* Allow single byte watchpoint. */
6096ee33c27SWill Deacon 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
6106ee33c27SWill Deacon 			break;
6116ee33c27SWill Deacon 	case 2:
6126ee33c27SWill Deacon 		/* Allow halfword watchpoints and breakpoints. */
6136ee33c27SWill Deacon 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
6146ee33c27SWill Deacon 			break;
6156ee33c27SWill Deacon 	default:
6166ee33c27SWill Deacon 		ret = -EINVAL;
617f81ef4a9SWill Deacon 		goto out;
618f81ef4a9SWill Deacon 	}
619f81ef4a9SWill Deacon 
6206ee33c27SWill Deacon 	info->address &= ~alignment_mask;
621f81ef4a9SWill Deacon 	info->ctrl.len <<= offset;
622f81ef4a9SWill Deacon 
623f81ef4a9SWill Deacon 	/*
624f81ef4a9SWill Deacon 	 * Currently we rely on an overflow handler to take
625f81ef4a9SWill Deacon 	 * care of single-stepping the breakpoint when it fires.
626f81ef4a9SWill Deacon 	 * In the case of userspace breakpoints on a core with V7 debug,
6273ce70b2eSWill Deacon 	 * we can use the mismatch feature as a poor-man's hardware
6283ce70b2eSWill Deacon 	 * single-step, but this only works for per-task breakpoints.
629f81ef4a9SWill Deacon 	 */
630*d1244336SWill Deacon 	if (!bp->overflow_handler && (arch_check_bp_in_kernelspace(bp) ||
631*d1244336SWill Deacon 	    !core_has_mismatch_brps() || !bp->hw.bp_target)) {
632*d1244336SWill Deacon 		pr_warning("overflow handler required but none found\n");
633f81ef4a9SWill Deacon 		ret = -EINVAL;
634f81ef4a9SWill Deacon 	}
635f81ef4a9SWill Deacon out:
636f81ef4a9SWill Deacon 	return ret;
637f81ef4a9SWill Deacon }
638f81ef4a9SWill Deacon 
6399ebb3cbcSWill Deacon /*
6409ebb3cbcSWill Deacon  * Enable/disable single-stepping over the breakpoint bp at address addr.
6419ebb3cbcSWill Deacon  */
6429ebb3cbcSWill Deacon static void enable_single_step(struct perf_event *bp, u32 addr)
643f81ef4a9SWill Deacon {
6449ebb3cbcSWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
645f81ef4a9SWill Deacon 
6469ebb3cbcSWill Deacon 	arch_uninstall_hw_breakpoint(bp);
6479ebb3cbcSWill Deacon 	info->step_ctrl.mismatch  = 1;
6489ebb3cbcSWill Deacon 	info->step_ctrl.len	  = ARM_BREAKPOINT_LEN_4;
6499ebb3cbcSWill Deacon 	info->step_ctrl.type	  = ARM_BREAKPOINT_EXECUTE;
6509ebb3cbcSWill Deacon 	info->step_ctrl.privilege = info->ctrl.privilege;
6519ebb3cbcSWill Deacon 	info->step_ctrl.enabled	  = 1;
6529ebb3cbcSWill Deacon 	info->trigger		  = addr;
6539ebb3cbcSWill Deacon 	arch_install_hw_breakpoint(bp);
654f81ef4a9SWill Deacon }
6559ebb3cbcSWill Deacon 
6569ebb3cbcSWill Deacon static void disable_single_step(struct perf_event *bp)
6579ebb3cbcSWill Deacon {
6589ebb3cbcSWill Deacon 	arch_uninstall_hw_breakpoint(bp);
6599ebb3cbcSWill Deacon 	counter_arch_bp(bp)->step_ctrl.enabled = 0;
6609ebb3cbcSWill Deacon 	arch_install_hw_breakpoint(bp);
661f81ef4a9SWill Deacon }
662f81ef4a9SWill Deacon 
6636f26aa05SWill Deacon static void watchpoint_handler(unsigned long addr, unsigned int fsr,
6646f26aa05SWill Deacon 			       struct pt_regs *regs)
665f81ef4a9SWill Deacon {
6666f26aa05SWill Deacon 	int i, access;
6676f26aa05SWill Deacon 	u32 val, ctrl_reg, alignment_mask;
6684a55c18eSWill Deacon 	struct perf_event *wp, **slots;
669f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
6706f26aa05SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
671f81ef4a9SWill Deacon 
6724a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
6734a55c18eSWill Deacon 
674f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
675f81ef4a9SWill Deacon 		rcu_read_lock();
676f81ef4a9SWill Deacon 
67793a04a34SWill Deacon 		wp = slots[i];
67893a04a34SWill Deacon 
6796f26aa05SWill Deacon 		if (wp == NULL)
6806f26aa05SWill Deacon 			goto unlock;
6816f26aa05SWill Deacon 
6826f26aa05SWill Deacon 		info = counter_arch_bp(wp);
6836f26aa05SWill Deacon 		/*
6846f26aa05SWill Deacon 		 * The DFAR is an unknown value on debug architectures prior
6856f26aa05SWill Deacon 		 * to 7.1. Since we only allow a single watchpoint on these
6866f26aa05SWill Deacon 		 * older CPUs, we can set the trigger to the lowest possible
6876f26aa05SWill Deacon 		 * faulting address.
6886f26aa05SWill Deacon 		 */
6896f26aa05SWill Deacon 		if (debug_arch < ARM_DEBUG_ARCH_V7_1) {
6906f26aa05SWill Deacon 			BUG_ON(i > 0);
6916f26aa05SWill Deacon 			info->trigger = wp->attr.bp_addr;
6926f26aa05SWill Deacon 		} else {
6936f26aa05SWill Deacon 			if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
6946f26aa05SWill Deacon 				alignment_mask = 0x7;
6956f26aa05SWill Deacon 			else
6966f26aa05SWill Deacon 				alignment_mask = 0x3;
6976f26aa05SWill Deacon 
6986f26aa05SWill Deacon 			/* Check if the watchpoint value matches. */
6996f26aa05SWill Deacon 			val = read_wb_reg(ARM_BASE_WVR + i);
7006f26aa05SWill Deacon 			if (val != (addr & ~alignment_mask))
7016f26aa05SWill Deacon 				goto unlock;
7026f26aa05SWill Deacon 
7036f26aa05SWill Deacon 			/* Possible match, check the byte address select. */
7046f26aa05SWill Deacon 			ctrl_reg = read_wb_reg(ARM_BASE_WCR + i);
7056f26aa05SWill Deacon 			decode_ctrl_reg(ctrl_reg, &ctrl);
7066f26aa05SWill Deacon 			if (!((1 << (addr & alignment_mask)) & ctrl.len))
7076f26aa05SWill Deacon 				goto unlock;
7086f26aa05SWill Deacon 
7096f26aa05SWill Deacon 			/* Check that the access type matches. */
7106f26aa05SWill Deacon 			access = (fsr & ARM_FSR_ACCESS_MASK) ? HW_BREAKPOINT_W :
7116f26aa05SWill Deacon 				 HW_BREAKPOINT_R;
7126f26aa05SWill Deacon 			if (!(access & hw_breakpoint_type(wp)))
7136f26aa05SWill Deacon 				goto unlock;
7146f26aa05SWill Deacon 
7156f26aa05SWill Deacon 			/* We have a winner. */
7166f26aa05SWill Deacon 			info->trigger = addr;
717f81ef4a9SWill Deacon 		}
718f81ef4a9SWill Deacon 
719f81ef4a9SWill Deacon 		pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
72093a04a34SWill Deacon 		perf_bp_event(wp, regs);
721f81ef4a9SWill Deacon 
722f81ef4a9SWill Deacon 		/*
723f81ef4a9SWill Deacon 		 * If no overflow handler is present, insert a temporary
724f81ef4a9SWill Deacon 		 * mismatch breakpoint so we can single-step over the
725f81ef4a9SWill Deacon 		 * watchpoint trigger.
726f81ef4a9SWill Deacon 		 */
7279ebb3cbcSWill Deacon 		if (!wp->overflow_handler)
7289ebb3cbcSWill Deacon 			enable_single_step(wp, instruction_pointer(regs));
729f81ef4a9SWill Deacon 
7306f26aa05SWill Deacon unlock:
731f81ef4a9SWill Deacon 		rcu_read_unlock();
732f81ef4a9SWill Deacon 	}
733f81ef4a9SWill Deacon }
734f81ef4a9SWill Deacon 
73593a04a34SWill Deacon static void watchpoint_single_step_handler(unsigned long pc)
73693a04a34SWill Deacon {
73793a04a34SWill Deacon 	int i;
7384a55c18eSWill Deacon 	struct perf_event *wp, **slots;
73993a04a34SWill Deacon 	struct arch_hw_breakpoint *info;
74093a04a34SWill Deacon 
7414a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
7424a55c18eSWill Deacon 
743c512de95SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
74493a04a34SWill Deacon 		rcu_read_lock();
74593a04a34SWill Deacon 
74693a04a34SWill Deacon 		wp = slots[i];
74793a04a34SWill Deacon 
74893a04a34SWill Deacon 		if (wp == NULL)
74993a04a34SWill Deacon 			goto unlock;
75093a04a34SWill Deacon 
75193a04a34SWill Deacon 		info = counter_arch_bp(wp);
75293a04a34SWill Deacon 		if (!info->step_ctrl.enabled)
75393a04a34SWill Deacon 			goto unlock;
75493a04a34SWill Deacon 
75593a04a34SWill Deacon 		/*
75693a04a34SWill Deacon 		 * Restore the original watchpoint if we've completed the
75793a04a34SWill Deacon 		 * single-step.
75893a04a34SWill Deacon 		 */
7599ebb3cbcSWill Deacon 		if (info->trigger != pc)
7609ebb3cbcSWill Deacon 			disable_single_step(wp);
76193a04a34SWill Deacon 
76293a04a34SWill Deacon unlock:
76393a04a34SWill Deacon 		rcu_read_unlock();
76493a04a34SWill Deacon 	}
76593a04a34SWill Deacon }
76693a04a34SWill Deacon 
767f81ef4a9SWill Deacon static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
768f81ef4a9SWill Deacon {
769f81ef4a9SWill Deacon 	int i;
770f81ef4a9SWill Deacon 	u32 ctrl_reg, val, addr;
7714a55c18eSWill Deacon 	struct perf_event *bp, **slots;
772f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
773f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
774f81ef4a9SWill Deacon 
7754a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
7764a55c18eSWill Deacon 
777f81ef4a9SWill Deacon 	/* The exception entry code places the amended lr in the PC. */
778f81ef4a9SWill Deacon 	addr = regs->ARM_pc;
779f81ef4a9SWill Deacon 
78093a04a34SWill Deacon 	/* Check the currently installed breakpoints first. */
78193a04a34SWill Deacon 	for (i = 0; i < core_num_brps; ++i) {
782f81ef4a9SWill Deacon 		rcu_read_lock();
783f81ef4a9SWill Deacon 
784f81ef4a9SWill Deacon 		bp = slots[i];
785f81ef4a9SWill Deacon 
7869ebb3cbcSWill Deacon 		if (bp == NULL)
7879ebb3cbcSWill Deacon 			goto unlock;
788f81ef4a9SWill Deacon 
7899ebb3cbcSWill Deacon 		info = counter_arch_bp(bp);
790f81ef4a9SWill Deacon 
791f81ef4a9SWill Deacon 		/* Check if the breakpoint value matches. */
792f81ef4a9SWill Deacon 		val = read_wb_reg(ARM_BASE_BVR + i);
793f81ef4a9SWill Deacon 		if (val != (addr & ~0x3))
7949ebb3cbcSWill Deacon 			goto mismatch;
795f81ef4a9SWill Deacon 
796f81ef4a9SWill Deacon 		/* Possible match, check the byte address select to confirm. */
797f81ef4a9SWill Deacon 		ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
798f81ef4a9SWill Deacon 		decode_ctrl_reg(ctrl_reg, &ctrl);
799f81ef4a9SWill Deacon 		if ((1 << (addr & 0x3)) & ctrl.len) {
800f81ef4a9SWill Deacon 			info->trigger = addr;
801f81ef4a9SWill Deacon 			pr_debug("breakpoint fired: address = 0x%x\n", addr);
802f81ef4a9SWill Deacon 			perf_bp_event(bp, regs);
8039ebb3cbcSWill Deacon 			if (!bp->overflow_handler)
8049ebb3cbcSWill Deacon 				enable_single_step(bp, addr);
8059ebb3cbcSWill Deacon 			goto unlock;
806f81ef4a9SWill Deacon 		}
807f81ef4a9SWill Deacon 
8089ebb3cbcSWill Deacon mismatch:
8099ebb3cbcSWill Deacon 		/* If we're stepping a breakpoint, it can now be restored. */
8109ebb3cbcSWill Deacon 		if (info->step_ctrl.enabled)
8119ebb3cbcSWill Deacon 			disable_single_step(bp);
8129ebb3cbcSWill Deacon unlock:
813f81ef4a9SWill Deacon 		rcu_read_unlock();
814f81ef4a9SWill Deacon 	}
81593a04a34SWill Deacon 
81693a04a34SWill Deacon 	/* Handle any pending watchpoint single-step breakpoints. */
81793a04a34SWill Deacon 	watchpoint_single_step_handler(addr);
818f81ef4a9SWill Deacon }
819f81ef4a9SWill Deacon 
820f81ef4a9SWill Deacon /*
821f81ef4a9SWill Deacon  * Called from either the Data Abort Handler [watchpoint] or the
82202fe2845SRussell King  * Prefetch Abort Handler [breakpoint] with interrupts disabled.
823f81ef4a9SWill Deacon  */
824f81ef4a9SWill Deacon static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
825f81ef4a9SWill Deacon 				 struct pt_regs *regs)
826f81ef4a9SWill Deacon {
8277e202696SWill Deacon 	int ret = 0;
828f81ef4a9SWill Deacon 	u32 dscr;
829f81ef4a9SWill Deacon 
83002fe2845SRussell King 	preempt_disable();
83102fe2845SRussell King 
83202fe2845SRussell King 	if (interrupts_enabled(regs))
83302fe2845SRussell King 		local_irq_enable();
8347e202696SWill Deacon 
835f81ef4a9SWill Deacon 	/* We only handle watchpoints and hardware breakpoints. */
836f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
837f81ef4a9SWill Deacon 
838f81ef4a9SWill Deacon 	/* Perform perf callbacks. */
839f81ef4a9SWill Deacon 	switch (ARM_DSCR_MOE(dscr)) {
840f81ef4a9SWill Deacon 	case ARM_ENTRY_BREAKPOINT:
841f81ef4a9SWill Deacon 		breakpoint_handler(addr, regs);
842f81ef4a9SWill Deacon 		break;
843f81ef4a9SWill Deacon 	case ARM_ENTRY_ASYNC_WATCHPOINT:
844235584b6SJoe Perches 		WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
845f81ef4a9SWill Deacon 	case ARM_ENTRY_SYNC_WATCHPOINT:
8466f26aa05SWill Deacon 		watchpoint_handler(addr, fsr, regs);
847f81ef4a9SWill Deacon 		break;
848f81ef4a9SWill Deacon 	default:
8497e202696SWill Deacon 		ret = 1; /* Unhandled fault. */
850f81ef4a9SWill Deacon 	}
851f81ef4a9SWill Deacon 
8527e202696SWill Deacon 	preempt_enable();
8537e202696SWill Deacon 
854f81ef4a9SWill Deacon 	return ret;
855f81ef4a9SWill Deacon }
856f81ef4a9SWill Deacon 
857f81ef4a9SWill Deacon /*
858f81ef4a9SWill Deacon  * One-time initialisation.
859f81ef4a9SWill Deacon  */
8600d352e3dSWill Deacon static cpumask_t debug_err_mask;
8610d352e3dSWill Deacon 
8620d352e3dSWill Deacon static int debug_reg_trap(struct pt_regs *regs, unsigned int instr)
8630d352e3dSWill Deacon {
8640d352e3dSWill Deacon 	int cpu = smp_processor_id();
8650d352e3dSWill Deacon 
8660d352e3dSWill Deacon 	pr_warning("Debug register access (0x%x) caused undefined instruction on CPU %d\n",
8670d352e3dSWill Deacon 		   instr, cpu);
8680d352e3dSWill Deacon 
8690d352e3dSWill Deacon 	/* Set the error flag for this CPU and skip the faulting instruction. */
8700d352e3dSWill Deacon 	cpumask_set_cpu(cpu, &debug_err_mask);
8710d352e3dSWill Deacon 	instruction_pointer(regs) += 4;
8720d352e3dSWill Deacon 	return 0;
8730d352e3dSWill Deacon }
8740d352e3dSWill Deacon 
8750d352e3dSWill Deacon static struct undef_hook debug_reg_hook = {
8760d352e3dSWill Deacon 	.instr_mask	= 0x0fe80f10,
8770d352e3dSWill Deacon 	.instr_val	= 0x0e000e10,
8780d352e3dSWill Deacon 	.fn		= debug_reg_trap,
8790d352e3dSWill Deacon };
8800d352e3dSWill Deacon 
8810d352e3dSWill Deacon static void reset_ctrl_regs(void *unused)
882f81ef4a9SWill Deacon {
883c512de95SWill Deacon 	int i, raw_num_brps, err = 0, cpu = smp_processor_id();
884c09bae70SWill Deacon 	u32 dbg_power;
885f81ef4a9SWill Deacon 
886ac88e071SWill Deacon 	/*
887ac88e071SWill Deacon 	 * v7 debug contains save and restore registers so that debug state
888ed19b739SWill Deacon 	 * can be maintained across low-power modes without leaving the debug
889ed19b739SWill Deacon 	 * logic powered up. It is IMPLEMENTATION DEFINED whether we can access
890ed19b739SWill Deacon 	 * the debug registers out of reset, so we must unlock the OS Lock
891ed19b739SWill Deacon 	 * Access Register to avoid taking undefined instruction exceptions
892ed19b739SWill Deacon 	 * later on.
893ac88e071SWill Deacon 	 */
894b5d5b8f9SWill Deacon 	switch (debug_arch) {
895b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_ECP14:
896ac88e071SWill Deacon 		/*
897c09bae70SWill Deacon 		 * Ensure sticky power-down is clear (i.e. debug logic is
898c09bae70SWill Deacon 		 * powered up).
899c09bae70SWill Deacon 		 */
900c09bae70SWill Deacon 		asm volatile("mrc p14, 0, %0, c1, c5, 4" : "=r" (dbg_power));
901b5d5b8f9SWill Deacon 		if ((dbg_power & 0x1) == 0)
902b5d5b8f9SWill Deacon 			err = -EPERM;
903b5d5b8f9SWill Deacon 		break;
904b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_1:
905b5d5b8f9SWill Deacon 		/*
906b5d5b8f9SWill Deacon 		 * Ensure the OS double lock is clear.
907b5d5b8f9SWill Deacon 		 */
908b5d5b8f9SWill Deacon 		asm volatile("mrc p14, 0, %0, c1, c3, 4" : "=r" (dbg_power));
909b5d5b8f9SWill Deacon 		if ((dbg_power & 0x1) == 1)
910b5d5b8f9SWill Deacon 			err = -EPERM;
911b5d5b8f9SWill Deacon 		break;
912b5d5b8f9SWill Deacon 	}
913b5d5b8f9SWill Deacon 
914b5d5b8f9SWill Deacon 	if (err) {
915c09bae70SWill Deacon 		pr_warning("CPU %d debug is powered down!\n", cpu);
9160d352e3dSWill Deacon 		cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
917c09bae70SWill Deacon 		return;
918c09bae70SWill Deacon 	}
919c09bae70SWill Deacon 
920c09bae70SWill Deacon 	/*
921ac88e071SWill Deacon 	 * Unconditionally clear the lock by writing a value
922ac88e071SWill Deacon 	 * other than 0xC5ACCE55 to the access register.
923ac88e071SWill Deacon 	 */
924ac88e071SWill Deacon 	asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0));
925ac88e071SWill Deacon 	isb();
926e89c0d70SWill Deacon 
927e89c0d70SWill Deacon 	/*
928e89c0d70SWill Deacon 	 * Clear any configured vector-catch events before
929e89c0d70SWill Deacon 	 * enabling monitor mode.
930e89c0d70SWill Deacon 	 */
931e89c0d70SWill Deacon 	asm volatile("mcr p14, 0, %0, c0, c7, 0" : : "r" (0));
932e89c0d70SWill Deacon 	isb();
933ac88e071SWill Deacon 
934f81ef4a9SWill Deacon 	if (enable_monitor_mode())
935f81ef4a9SWill Deacon 		return;
936f81ef4a9SWill Deacon 
9370017ff42SWill Deacon 	/* We must also reset any reserved registers. */
938c512de95SWill Deacon 	raw_num_brps = get_num_brp_resources();
939c512de95SWill Deacon 	for (i = 0; i < raw_num_brps; ++i) {
940f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BCR + i, 0UL);
941f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BVR + i, 0UL);
942f81ef4a9SWill Deacon 	}
943f81ef4a9SWill Deacon 
944f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
945f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WCR + i, 0UL);
946f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WVR + i, 0UL);
947f81ef4a9SWill Deacon 	}
948f81ef4a9SWill Deacon }
949f81ef4a9SWill Deacon 
9507d99331eSWill Deacon static int __cpuinit dbg_reset_notify(struct notifier_block *self,
9517d99331eSWill Deacon 				      unsigned long action, void *cpu)
9527d99331eSWill Deacon {
9537d99331eSWill Deacon 	if (action == CPU_ONLINE)
9547d99331eSWill Deacon 		smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
9550d352e3dSWill Deacon 
9567d99331eSWill Deacon 	return NOTIFY_OK;
9577d99331eSWill Deacon }
9587d99331eSWill Deacon 
9597d99331eSWill Deacon static struct notifier_block __cpuinitdata dbg_reset_nb = {
9607d99331eSWill Deacon 	.notifier_call = dbg_reset_notify,
9617d99331eSWill Deacon };
9627d99331eSWill Deacon 
963f81ef4a9SWill Deacon static int __init arch_hw_breakpoint_init(void)
964f81ef4a9SWill Deacon {
965f81ef4a9SWill Deacon 	u32 dscr;
966f81ef4a9SWill Deacon 
967f81ef4a9SWill Deacon 	debug_arch = get_debug_arch();
968f81ef4a9SWill Deacon 
96966e1cfe6SWill Deacon 	if (!debug_arch_supported()) {
970f81ef4a9SWill Deacon 		pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
9718fbf397cSWill Deacon 		return 0;
972f81ef4a9SWill Deacon 	}
973f81ef4a9SWill Deacon 
974f81ef4a9SWill Deacon 	/* Determine how many BRPs/WRPs are available. */
975f81ef4a9SWill Deacon 	core_num_brps = get_num_brps();
976f81ef4a9SWill Deacon 	core_num_wrps = get_num_wrps();
977f81ef4a9SWill Deacon 
9780d352e3dSWill Deacon 	/*
9790d352e3dSWill Deacon 	 * We need to tread carefully here because DBGSWENABLE may be
9800d352e3dSWill Deacon 	 * driven low on this core and there isn't an architected way to
9810d352e3dSWill Deacon 	 * determine that.
9820d352e3dSWill Deacon 	 */
9830d352e3dSWill Deacon 	register_undef_hook(&debug_reg_hook);
984f81ef4a9SWill Deacon 
985f81ef4a9SWill Deacon 	/*
986f81ef4a9SWill Deacon 	 * Reset the breakpoint resources. We assume that a halting
987f81ef4a9SWill Deacon 	 * debugger will leave the world in a nice state for us.
988f81ef4a9SWill Deacon 	 */
9890d352e3dSWill Deacon 	on_each_cpu(reset_ctrl_regs, NULL, 1);
9900d352e3dSWill Deacon 	unregister_undef_hook(&debug_reg_hook);
9910d352e3dSWill Deacon 	if (!cpumask_empty(&debug_err_mask)) {
992c09bae70SWill Deacon 		core_num_brps = 0;
993c09bae70SWill Deacon 		core_num_wrps = 0;
994c09bae70SWill Deacon 		return 0;
995c09bae70SWill Deacon 	}
996ac88e071SWill Deacon 
9970d352e3dSWill Deacon 	pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n",
9980d352e3dSWill Deacon 		core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " :
9990d352e3dSWill Deacon 		"", core_num_wrps);
10000d352e3dSWill Deacon 
1001ed19b739SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
1002ed19b739SWill Deacon 	if (dscr & ARM_DSCR_HDBGEN) {
1003ed19b739SWill Deacon 		max_watchpoint_len = 4;
10047d85d61fSStephen Boyd 		pr_warning("halting debug mode enabled. Assuming maximum watchpoint size of %u bytes.\n",
10057d85d61fSStephen Boyd 			   max_watchpoint_len);
1006ed19b739SWill Deacon 	} else {
1007ac88e071SWill Deacon 		/* Work out the maximum supported watchpoint length. */
1008ac88e071SWill Deacon 		max_watchpoint_len = get_max_wp_len();
1009ac88e071SWill Deacon 		pr_info("maximum watchpoint size is %u bytes.\n",
1010ac88e071SWill Deacon 				max_watchpoint_len);
1011f81ef4a9SWill Deacon 	}
1012f81ef4a9SWill Deacon 
1013f81ef4a9SWill Deacon 	/* Register debug fault handler. */
1014f81ef4a9SWill Deacon 	hook_fault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
1015f81ef4a9SWill Deacon 			"watchpoint debug exception");
1016f81ef4a9SWill Deacon 	hook_ifault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
1017f81ef4a9SWill Deacon 			"breakpoint debug exception");
1018f81ef4a9SWill Deacon 
10197d99331eSWill Deacon 	/* Register hotplug notifier. */
10207d99331eSWill Deacon 	register_cpu_notifier(&dbg_reset_nb);
10218fbf397cSWill Deacon 	return 0;
1022f81ef4a9SWill Deacon }
1023f81ef4a9SWill Deacon arch_initcall(arch_hw_breakpoint_init);
1024f81ef4a9SWill Deacon 
1025f81ef4a9SWill Deacon void hw_breakpoint_pmu_read(struct perf_event *bp)
1026f81ef4a9SWill Deacon {
1027f81ef4a9SWill Deacon }
1028f81ef4a9SWill Deacon 
1029f81ef4a9SWill Deacon /*
1030f81ef4a9SWill Deacon  * Dummy function to register with die_notifier.
1031f81ef4a9SWill Deacon  */
1032f81ef4a9SWill Deacon int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
1033f81ef4a9SWill Deacon 					unsigned long val, void *data)
1034f81ef4a9SWill Deacon {
1035f81ef4a9SWill Deacon 	return NOTIFY_DONE;
1036f81ef4a9SWill Deacon }
1037