xref: /openbmc/linux/arch/arm/kernel/hw_breakpoint.c (revision 8fbf397c3389c1dedfa9ee412715046ab28fd82d)
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;
480017ff42SWill Deacon static int core_num_reserved_brps;
49f81ef4a9SWill Deacon static int core_num_wrps;
50f81ef4a9SWill Deacon 
51f81ef4a9SWill Deacon /* Debug architecture version. */
52f81ef4a9SWill Deacon static u8 debug_arch;
53f81ef4a9SWill Deacon 
54f81ef4a9SWill Deacon /* Maximum supported watchpoint length. */
55f81ef4a9SWill Deacon static u8 max_watchpoint_len;
56f81ef4a9SWill Deacon 
57f81ef4a9SWill Deacon #define READ_WB_REG_CASE(OP2, M, VAL)		\
58f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):			\
59f81ef4a9SWill Deacon 		ARM_DBG_READ(c ## M, OP2, VAL); \
60f81ef4a9SWill Deacon 		break
61f81ef4a9SWill Deacon 
62f81ef4a9SWill Deacon #define WRITE_WB_REG_CASE(OP2, M, VAL)		\
63f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):			\
64f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c ## M, OP2, VAL);\
65f81ef4a9SWill Deacon 		break
66f81ef4a9SWill Deacon 
67f81ef4a9SWill Deacon #define GEN_READ_WB_REG_CASES(OP2, VAL)		\
68f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 0, VAL);		\
69f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 1, VAL);		\
70f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 2, VAL);		\
71f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 3, VAL);		\
72f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 4, VAL);		\
73f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 5, VAL);		\
74f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 6, VAL);		\
75f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 7, VAL);		\
76f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 8, VAL);		\
77f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 9, VAL);		\
78f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 10, VAL);		\
79f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 11, VAL);		\
80f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 12, VAL);		\
81f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 13, VAL);		\
82f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 14, VAL);		\
83f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 15, VAL)
84f81ef4a9SWill Deacon 
85f81ef4a9SWill Deacon #define GEN_WRITE_WB_REG_CASES(OP2, VAL)	\
86f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 0, VAL);		\
87f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 1, VAL);		\
88f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 2, VAL);		\
89f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 3, VAL);		\
90f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 4, VAL);		\
91f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 5, VAL);		\
92f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 6, VAL);		\
93f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 7, VAL);		\
94f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 8, VAL);		\
95f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 9, VAL);		\
96f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 10, VAL);	\
97f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 11, VAL);	\
98f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 12, VAL);	\
99f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 13, VAL);	\
100f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 14, VAL);	\
101f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 15, VAL)
102f81ef4a9SWill Deacon 
103f81ef4a9SWill Deacon static u32 read_wb_reg(int n)
104f81ef4a9SWill Deacon {
105f81ef4a9SWill Deacon 	u32 val = 0;
106f81ef4a9SWill Deacon 
107f81ef4a9SWill Deacon 	switch (n) {
108f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
109f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
110f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
111f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
112f81ef4a9SWill Deacon 	default:
113f81ef4a9SWill Deacon 		pr_warning("attempt to read from unknown breakpoint "
114f81ef4a9SWill Deacon 				"register %d\n", n);
115f81ef4a9SWill Deacon 	}
116f81ef4a9SWill Deacon 
117f81ef4a9SWill Deacon 	return val;
118f81ef4a9SWill Deacon }
119f81ef4a9SWill Deacon 
120f81ef4a9SWill Deacon static void write_wb_reg(int n, u32 val)
121f81ef4a9SWill Deacon {
122f81ef4a9SWill Deacon 	switch (n) {
123f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
124f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
125f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
126f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
127f81ef4a9SWill Deacon 	default:
128f81ef4a9SWill Deacon 		pr_warning("attempt to write to unknown breakpoint "
129f81ef4a9SWill Deacon 				"register %d\n", n);
130f81ef4a9SWill Deacon 	}
131f81ef4a9SWill Deacon 	isb();
132f81ef4a9SWill Deacon }
133f81ef4a9SWill Deacon 
1340017ff42SWill Deacon /* Determine debug architecture. */
1350017ff42SWill Deacon static u8 get_debug_arch(void)
1360017ff42SWill Deacon {
1370017ff42SWill Deacon 	u32 didr;
1380017ff42SWill Deacon 
1390017ff42SWill Deacon 	/* Do we implement the extended CPUID interface? */
1400017ff42SWill Deacon 	if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
1410017ff42SWill Deacon 		pr_warning("CPUID feature registers not supported. "
1420017ff42SWill Deacon 				"Assuming v6 debug is present.\n");
1430017ff42SWill Deacon 		return ARM_DEBUG_ARCH_V6;
1440017ff42SWill Deacon 	}
1450017ff42SWill Deacon 
1460017ff42SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
1470017ff42SWill Deacon 	return (didr >> 16) & 0xf;
1480017ff42SWill Deacon }
1490017ff42SWill Deacon 
1500017ff42SWill Deacon u8 arch_get_debug_arch(void)
1510017ff42SWill Deacon {
1520017ff42SWill Deacon 	return debug_arch;
1530017ff42SWill Deacon }
1540017ff42SWill Deacon 
1550017ff42SWill Deacon /* Determine number of BRP register available. */
1560017ff42SWill Deacon static int get_num_brp_resources(void)
1570017ff42SWill Deacon {
1580017ff42SWill Deacon 	u32 didr;
1590017ff42SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
1600017ff42SWill Deacon 	return ((didr >> 24) & 0xf) + 1;
1610017ff42SWill Deacon }
1620017ff42SWill Deacon 
1630017ff42SWill Deacon /* Does this core support mismatch breakpoints? */
1640017ff42SWill Deacon static int core_has_mismatch_brps(void)
1650017ff42SWill Deacon {
1660017ff42SWill Deacon 	return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
1670017ff42SWill Deacon 		get_num_brp_resources() > 1);
1680017ff42SWill Deacon }
1690017ff42SWill Deacon 
1700017ff42SWill Deacon /* Determine number of usable WRPs available. */
1710017ff42SWill Deacon static int get_num_wrps(void)
1720017ff42SWill Deacon {
1730017ff42SWill Deacon 	/*
1740017ff42SWill Deacon 	 * FIXME: When a watchpoint fires, the only way to work out which
1750017ff42SWill Deacon 	 * watchpoint it was is by disassembling the faulting instruction
1760017ff42SWill Deacon 	 * and working out the address of the memory access.
1770017ff42SWill Deacon 	 *
1780017ff42SWill Deacon 	 * Furthermore, we can only do this if the watchpoint was precise
1790017ff42SWill Deacon 	 * since imprecise watchpoints prevent us from calculating register
1800017ff42SWill Deacon 	 * based addresses.
1810017ff42SWill Deacon 	 *
1820017ff42SWill Deacon 	 * Providing we have more than 1 breakpoint register, we only report
1830017ff42SWill Deacon 	 * a single watchpoint register for the time being. This way, we always
1840017ff42SWill Deacon 	 * know which watchpoint fired. In the future we can either add a
1850017ff42SWill Deacon 	 * disassembler and address generation emulator, or we can insert a
1860017ff42SWill Deacon 	 * check to see if the DFAR is set on watchpoint exception entry
1870017ff42SWill Deacon 	 * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
1880017ff42SWill Deacon 	 * that it is set on some implementations].
1890017ff42SWill Deacon 	 */
1900017ff42SWill Deacon 
1910017ff42SWill Deacon #if 0
1920017ff42SWill Deacon 	int wrps;
1930017ff42SWill Deacon 	u32 didr;
1940017ff42SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
1950017ff42SWill Deacon 	wrps = ((didr >> 28) & 0xf) + 1;
1960017ff42SWill Deacon #endif
1970017ff42SWill Deacon 	int wrps = 1;
1980017ff42SWill Deacon 
1990017ff42SWill Deacon 	if (core_has_mismatch_brps() && wrps >= get_num_brp_resources())
2000017ff42SWill Deacon 		wrps = get_num_brp_resources() - 1;
2010017ff42SWill Deacon 
2020017ff42SWill Deacon 	return wrps;
2030017ff42SWill Deacon }
2040017ff42SWill Deacon 
2050017ff42SWill Deacon /* We reserve one breakpoint for each watchpoint. */
2060017ff42SWill Deacon static int get_num_reserved_brps(void)
2070017ff42SWill Deacon {
2080017ff42SWill Deacon 	if (core_has_mismatch_brps())
2090017ff42SWill Deacon 		return get_num_wrps();
2100017ff42SWill Deacon 	return 0;
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();
2170017ff42SWill Deacon 	if (core_has_mismatch_brps())
2180017ff42SWill Deacon 		brps -= get_num_reserved_brps();
2190017ff42SWill Deacon 	return brps;
2200017ff42SWill Deacon }
2210017ff42SWill Deacon 
222f81ef4a9SWill Deacon /*
223f81ef4a9SWill Deacon  * In order to access the breakpoint/watchpoint control registers,
224f81ef4a9SWill Deacon  * we must be running in debug monitor mode. Unfortunately, we can
225f81ef4a9SWill Deacon  * be put into halting debug mode at any time by an external debugger
226f81ef4a9SWill Deacon  * but there is nothing we can do to prevent that.
227f81ef4a9SWill Deacon  */
228f81ef4a9SWill Deacon static int enable_monitor_mode(void)
229f81ef4a9SWill Deacon {
230f81ef4a9SWill Deacon 	u32 dscr;
231f81ef4a9SWill Deacon 	int ret = 0;
232f81ef4a9SWill Deacon 
233f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
234f81ef4a9SWill Deacon 
235f81ef4a9SWill Deacon 	/* Ensure that halting mode is disabled. */
236f81ef4a9SWill Deacon 	if (WARN_ONCE(dscr & ARM_DSCR_HDBGEN, "halting debug mode enabled."
237f81ef4a9SWill Deacon 				"Unable to access hardware resources.")) {
238f81ef4a9SWill Deacon 		ret = -EPERM;
239f81ef4a9SWill Deacon 		goto out;
240f81ef4a9SWill Deacon 	}
241f81ef4a9SWill Deacon 
242*8fbf397cSWill Deacon 	/* If monitor mode is already enabled, just return. */
243*8fbf397cSWill Deacon 	if (dscr & ARM_DSCR_MDBGEN)
244*8fbf397cSWill Deacon 		goto out;
245*8fbf397cSWill Deacon 
246f81ef4a9SWill Deacon 	/* Write to the corresponding DSCR. */
247*8fbf397cSWill Deacon 	switch (get_debug_arch()) {
248f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6:
249f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6_1:
250f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN));
251f81ef4a9SWill Deacon 		break;
252f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V7_ECP14:
253f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN));
254f81ef4a9SWill Deacon 		break;
255f81ef4a9SWill Deacon 	default:
256f81ef4a9SWill Deacon 		ret = -ENODEV;
257f81ef4a9SWill Deacon 		goto out;
258f81ef4a9SWill Deacon 	}
259f81ef4a9SWill Deacon 
260f81ef4a9SWill Deacon 	/* Check that the write made it through. */
261f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
262*8fbf397cSWill Deacon 	if (!(dscr & ARM_DSCR_MDBGEN))
263f81ef4a9SWill Deacon 		ret = -EPERM;
264f81ef4a9SWill Deacon 
265f81ef4a9SWill Deacon out:
266f81ef4a9SWill Deacon 	return ret;
267f81ef4a9SWill Deacon }
268f81ef4a9SWill Deacon 
269*8fbf397cSWill Deacon int hw_breakpoint_slots(int type)
270*8fbf397cSWill Deacon {
271*8fbf397cSWill Deacon 	/*
272*8fbf397cSWill Deacon 	 * We can be called early, so don't rely on
273*8fbf397cSWill Deacon 	 * our static variables being initialised.
274*8fbf397cSWill Deacon 	 */
275*8fbf397cSWill Deacon 	switch (type) {
276*8fbf397cSWill Deacon 	case TYPE_INST:
277*8fbf397cSWill Deacon 		return get_num_brps();
278*8fbf397cSWill Deacon 	case TYPE_DATA:
279*8fbf397cSWill Deacon 		return get_num_wrps();
280*8fbf397cSWill Deacon 	default:
281*8fbf397cSWill Deacon 		pr_warning("unknown slot type: %d\n", type);
282*8fbf397cSWill Deacon 		return 0;
283*8fbf397cSWill Deacon 	}
284*8fbf397cSWill Deacon }
285*8fbf397cSWill Deacon 
286f81ef4a9SWill Deacon /*
287f81ef4a9SWill Deacon  * Check if 8-bit byte-address select is available.
288f81ef4a9SWill Deacon  * This clobbers WRP 0.
289f81ef4a9SWill Deacon  */
290f81ef4a9SWill Deacon static u8 get_max_wp_len(void)
291f81ef4a9SWill Deacon {
292f81ef4a9SWill Deacon 	u32 ctrl_reg;
293f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
294f81ef4a9SWill Deacon 	u8 size = 4;
295f81ef4a9SWill Deacon 
296f81ef4a9SWill Deacon 	if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
297f81ef4a9SWill Deacon 		goto out;
298f81ef4a9SWill Deacon 
299f81ef4a9SWill Deacon 	memset(&ctrl, 0, sizeof(ctrl));
300f81ef4a9SWill Deacon 	ctrl.len = ARM_BREAKPOINT_LEN_8;
301f81ef4a9SWill Deacon 	ctrl_reg = encode_ctrl_reg(ctrl);
302f81ef4a9SWill Deacon 
303f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WVR, 0);
304f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WCR, ctrl_reg);
305f81ef4a9SWill Deacon 	if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
306f81ef4a9SWill Deacon 		size = 8;
307f81ef4a9SWill Deacon 
308f81ef4a9SWill Deacon out:
309f81ef4a9SWill Deacon 	return size;
310f81ef4a9SWill Deacon }
311f81ef4a9SWill Deacon 
312f81ef4a9SWill Deacon u8 arch_get_max_wp_len(void)
313f81ef4a9SWill Deacon {
314f81ef4a9SWill Deacon 	return max_watchpoint_len;
315f81ef4a9SWill Deacon }
316f81ef4a9SWill Deacon 
317f81ef4a9SWill Deacon /*
318f81ef4a9SWill Deacon  * Install a perf counter breakpoint.
319f81ef4a9SWill Deacon  */
320f81ef4a9SWill Deacon int arch_install_hw_breakpoint(struct perf_event *bp)
321f81ef4a9SWill Deacon {
322f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
323f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
324f81ef4a9SWill Deacon 	int i, max_slots, ctrl_base, val_base, ret = 0;
32593a04a34SWill Deacon 	u32 addr, ctrl;
326f81ef4a9SWill Deacon 
327f81ef4a9SWill Deacon 	/* Ensure that we are in monitor mode and halting mode is disabled. */
328f81ef4a9SWill Deacon 	ret = enable_monitor_mode();
329f81ef4a9SWill Deacon 	if (ret)
330f81ef4a9SWill Deacon 		goto out;
331f81ef4a9SWill Deacon 
33293a04a34SWill Deacon 	addr = info->address;
33393a04a34SWill Deacon 	ctrl = encode_ctrl_reg(info->ctrl) | 0x1;
33493a04a34SWill Deacon 
335f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
336f81ef4a9SWill Deacon 		/* Breakpoint */
337f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_BCR;
338f81ef4a9SWill Deacon 		val_base = ARM_BASE_BVR;
3394a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
3400017ff42SWill Deacon 		max_slots = core_num_brps;
3419ebb3cbcSWill Deacon 		if (info->step_ctrl.enabled) {
3429ebb3cbcSWill Deacon 			/* Override the breakpoint data with the step data. */
3439ebb3cbcSWill Deacon 			addr = info->trigger & ~0x3;
3449ebb3cbcSWill Deacon 			ctrl = encode_ctrl_reg(info->step_ctrl);
3459ebb3cbcSWill Deacon 		}
346f81ef4a9SWill Deacon 	} else {
347f81ef4a9SWill Deacon 		/* Watchpoint */
34893a04a34SWill Deacon 		if (info->step_ctrl.enabled) {
34993a04a34SWill Deacon 			/* Install into the reserved breakpoint region. */
35093a04a34SWill Deacon 			ctrl_base = ARM_BASE_BCR + core_num_brps;
35193a04a34SWill Deacon 			val_base = ARM_BASE_BVR + core_num_brps;
35293a04a34SWill Deacon 			/* Override the watchpoint data with the step data. */
35393a04a34SWill Deacon 			addr = info->trigger & ~0x3;
35493a04a34SWill Deacon 			ctrl = encode_ctrl_reg(info->step_ctrl);
35593a04a34SWill Deacon 		} else {
356f81ef4a9SWill Deacon 			ctrl_base = ARM_BASE_WCR;
357f81ef4a9SWill Deacon 			val_base = ARM_BASE_WVR;
35893a04a34SWill Deacon 		}
3594a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
360f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
361f81ef4a9SWill Deacon 	}
362f81ef4a9SWill Deacon 
363f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
364f81ef4a9SWill Deacon 		slot = &slots[i];
365f81ef4a9SWill Deacon 
366f81ef4a9SWill Deacon 		if (!*slot) {
367f81ef4a9SWill Deacon 			*slot = bp;
368f81ef4a9SWill Deacon 			break;
369f81ef4a9SWill Deacon 		}
370f81ef4a9SWill Deacon 	}
371f81ef4a9SWill Deacon 
372f81ef4a9SWill Deacon 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot")) {
373f81ef4a9SWill Deacon 		ret = -EBUSY;
374f81ef4a9SWill Deacon 		goto out;
375f81ef4a9SWill Deacon 	}
376f81ef4a9SWill Deacon 
377f81ef4a9SWill Deacon 	/* Setup the address register. */
37893a04a34SWill Deacon 	write_wb_reg(val_base + i, addr);
379f81ef4a9SWill Deacon 
380f81ef4a9SWill Deacon 	/* Setup the control register. */
38193a04a34SWill Deacon 	write_wb_reg(ctrl_base + i, ctrl);
382f81ef4a9SWill Deacon 
383f81ef4a9SWill Deacon out:
384f81ef4a9SWill Deacon 	return ret;
385f81ef4a9SWill Deacon }
386f81ef4a9SWill Deacon 
387f81ef4a9SWill Deacon void arch_uninstall_hw_breakpoint(struct perf_event *bp)
388f81ef4a9SWill Deacon {
389f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
390f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
391f81ef4a9SWill Deacon 	int i, max_slots, base;
392f81ef4a9SWill Deacon 
393f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
394f81ef4a9SWill Deacon 		/* Breakpoint */
395f81ef4a9SWill Deacon 		base = ARM_BASE_BCR;
3964a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
3970017ff42SWill Deacon 		max_slots = core_num_brps;
398f81ef4a9SWill Deacon 	} else {
399f81ef4a9SWill Deacon 		/* Watchpoint */
40093a04a34SWill Deacon 		if (info->step_ctrl.enabled)
40193a04a34SWill Deacon 			base = ARM_BASE_BCR + core_num_brps;
40293a04a34SWill Deacon 		else
403f81ef4a9SWill Deacon 			base = ARM_BASE_WCR;
4044a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
405f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
406f81ef4a9SWill Deacon 	}
407f81ef4a9SWill Deacon 
408f81ef4a9SWill Deacon 	/* Remove the breakpoint. */
409f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
410f81ef4a9SWill Deacon 		slot = &slots[i];
411f81ef4a9SWill Deacon 
412f81ef4a9SWill Deacon 		if (*slot == bp) {
413f81ef4a9SWill Deacon 			*slot = NULL;
414f81ef4a9SWill Deacon 			break;
415f81ef4a9SWill Deacon 		}
416f81ef4a9SWill Deacon 	}
417f81ef4a9SWill Deacon 
418f81ef4a9SWill Deacon 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot"))
419f81ef4a9SWill Deacon 		return;
420f81ef4a9SWill Deacon 
421f81ef4a9SWill Deacon 	/* Reset the control register. */
422f81ef4a9SWill Deacon 	write_wb_reg(base + i, 0);
423f81ef4a9SWill Deacon }
424f81ef4a9SWill Deacon 
425f81ef4a9SWill Deacon static int get_hbp_len(u8 hbp_len)
426f81ef4a9SWill Deacon {
427f81ef4a9SWill Deacon 	unsigned int len_in_bytes = 0;
428f81ef4a9SWill Deacon 
429f81ef4a9SWill Deacon 	switch (hbp_len) {
430f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
431f81ef4a9SWill Deacon 		len_in_bytes = 1;
432f81ef4a9SWill Deacon 		break;
433f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
434f81ef4a9SWill Deacon 		len_in_bytes = 2;
435f81ef4a9SWill Deacon 		break;
436f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
437f81ef4a9SWill Deacon 		len_in_bytes = 4;
438f81ef4a9SWill Deacon 		break;
439f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
440f81ef4a9SWill Deacon 		len_in_bytes = 8;
441f81ef4a9SWill Deacon 		break;
442f81ef4a9SWill Deacon 	}
443f81ef4a9SWill Deacon 
444f81ef4a9SWill Deacon 	return len_in_bytes;
445f81ef4a9SWill Deacon }
446f81ef4a9SWill Deacon 
447f81ef4a9SWill Deacon /*
448f81ef4a9SWill Deacon  * Check whether bp virtual address is in kernel space.
449f81ef4a9SWill Deacon  */
450f81ef4a9SWill Deacon int arch_check_bp_in_kernelspace(struct perf_event *bp)
451f81ef4a9SWill Deacon {
452f81ef4a9SWill Deacon 	unsigned int len;
453f81ef4a9SWill Deacon 	unsigned long va;
454f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
455f81ef4a9SWill Deacon 
456f81ef4a9SWill Deacon 	va = info->address;
457f81ef4a9SWill Deacon 	len = get_hbp_len(info->ctrl.len);
458f81ef4a9SWill Deacon 
459f81ef4a9SWill Deacon 	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
460f81ef4a9SWill Deacon }
461f81ef4a9SWill Deacon 
462f81ef4a9SWill Deacon /*
463f81ef4a9SWill Deacon  * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
464f81ef4a9SWill Deacon  * Hopefully this will disappear when ptrace can bypass the conversion
465f81ef4a9SWill Deacon  * to generic breakpoint descriptions.
466f81ef4a9SWill Deacon  */
467f81ef4a9SWill Deacon int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
468f81ef4a9SWill Deacon 			   int *gen_len, int *gen_type)
469f81ef4a9SWill Deacon {
470f81ef4a9SWill Deacon 	/* Type */
471f81ef4a9SWill Deacon 	switch (ctrl.type) {
472f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_EXECUTE:
473f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_X;
474f81ef4a9SWill Deacon 		break;
475f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD:
476f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_R;
477f81ef4a9SWill Deacon 		break;
478f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_STORE:
479f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_W;
480f81ef4a9SWill Deacon 		break;
481f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
482f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_RW;
483f81ef4a9SWill Deacon 		break;
484f81ef4a9SWill Deacon 	default:
485f81ef4a9SWill Deacon 		return -EINVAL;
486f81ef4a9SWill Deacon 	}
487f81ef4a9SWill Deacon 
488f81ef4a9SWill Deacon 	/* Len */
489f81ef4a9SWill Deacon 	switch (ctrl.len) {
490f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
491f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_1;
492f81ef4a9SWill Deacon 		break;
493f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
494f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_2;
495f81ef4a9SWill Deacon 		break;
496f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
497f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_4;
498f81ef4a9SWill Deacon 		break;
499f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
500f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_8;
501f81ef4a9SWill Deacon 		break;
502f81ef4a9SWill Deacon 	default:
503f81ef4a9SWill Deacon 		return -EINVAL;
504f81ef4a9SWill Deacon 	}
505f81ef4a9SWill Deacon 
506f81ef4a9SWill Deacon 	return 0;
507f81ef4a9SWill Deacon }
508f81ef4a9SWill Deacon 
509f81ef4a9SWill Deacon /*
510f81ef4a9SWill Deacon  * Construct an arch_hw_breakpoint from a perf_event.
511f81ef4a9SWill Deacon  */
512f81ef4a9SWill Deacon static int arch_build_bp_info(struct perf_event *bp)
513f81ef4a9SWill Deacon {
514f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
515f81ef4a9SWill Deacon 
516f81ef4a9SWill Deacon 	/* Type */
517f81ef4a9SWill Deacon 	switch (bp->attr.bp_type) {
518f81ef4a9SWill Deacon 	case HW_BREAKPOINT_X:
519f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
520f81ef4a9SWill Deacon 		break;
521f81ef4a9SWill Deacon 	case HW_BREAKPOINT_R:
522f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD;
523f81ef4a9SWill Deacon 		break;
524f81ef4a9SWill Deacon 	case HW_BREAKPOINT_W:
525f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_STORE;
526f81ef4a9SWill Deacon 		break;
527f81ef4a9SWill Deacon 	case HW_BREAKPOINT_RW:
528f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
529f81ef4a9SWill Deacon 		break;
530f81ef4a9SWill Deacon 	default:
531f81ef4a9SWill Deacon 		return -EINVAL;
532f81ef4a9SWill Deacon 	}
533f81ef4a9SWill Deacon 
534f81ef4a9SWill Deacon 	/* Len */
535f81ef4a9SWill Deacon 	switch (bp->attr.bp_len) {
536f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_1:
537f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_1;
538f81ef4a9SWill Deacon 		break;
539f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_2:
540f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_2;
541f81ef4a9SWill Deacon 		break;
542f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_4:
543f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_4;
544f81ef4a9SWill Deacon 		break;
545f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_8:
546f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_8;
547f81ef4a9SWill Deacon 		if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
548f81ef4a9SWill Deacon 			&& max_watchpoint_len >= 8)
549f81ef4a9SWill Deacon 			break;
550f81ef4a9SWill Deacon 	default:
551f81ef4a9SWill Deacon 		return -EINVAL;
552f81ef4a9SWill Deacon 	}
553f81ef4a9SWill Deacon 
5546ee33c27SWill Deacon 	/*
5556ee33c27SWill Deacon 	 * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
5566ee33c27SWill Deacon 	 * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported
5576ee33c27SWill Deacon 	 * by the hardware and must be aligned to the appropriate number of
5586ee33c27SWill Deacon 	 * bytes.
5596ee33c27SWill Deacon 	 */
5606ee33c27SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
5616ee33c27SWill Deacon 	    info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
5626ee33c27SWill Deacon 	    info->ctrl.len != ARM_BREAKPOINT_LEN_4)
5636ee33c27SWill Deacon 		return -EINVAL;
5646ee33c27SWill Deacon 
565f81ef4a9SWill Deacon 	/* Address */
566f81ef4a9SWill Deacon 	info->address = bp->attr.bp_addr;
567f81ef4a9SWill Deacon 
568f81ef4a9SWill Deacon 	/* Privilege */
569f81ef4a9SWill Deacon 	info->ctrl.privilege = ARM_BREAKPOINT_USER;
57093a04a34SWill Deacon 	if (arch_check_bp_in_kernelspace(bp))
571f81ef4a9SWill Deacon 		info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
572f81ef4a9SWill Deacon 
573f81ef4a9SWill Deacon 	/* Enabled? */
574f81ef4a9SWill Deacon 	info->ctrl.enabled = !bp->attr.disabled;
575f81ef4a9SWill Deacon 
576f81ef4a9SWill Deacon 	/* Mismatch */
577f81ef4a9SWill Deacon 	info->ctrl.mismatch = 0;
578f81ef4a9SWill Deacon 
579f81ef4a9SWill Deacon 	return 0;
580f81ef4a9SWill Deacon }
581f81ef4a9SWill Deacon 
582f81ef4a9SWill Deacon /*
583f81ef4a9SWill Deacon  * Validate the arch-specific HW Breakpoint register settings.
584f81ef4a9SWill Deacon  */
585f81ef4a9SWill Deacon int arch_validate_hwbkpt_settings(struct perf_event *bp)
586f81ef4a9SWill Deacon {
587f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
588f81ef4a9SWill Deacon 	int ret = 0;
5896ee33c27SWill Deacon 	u32 offset, alignment_mask = 0x3;
590f81ef4a9SWill Deacon 
591f81ef4a9SWill Deacon 	/* Build the arch_hw_breakpoint. */
592f81ef4a9SWill Deacon 	ret = arch_build_bp_info(bp);
593f81ef4a9SWill Deacon 	if (ret)
594f81ef4a9SWill Deacon 		goto out;
595f81ef4a9SWill Deacon 
596f81ef4a9SWill Deacon 	/* Check address alignment. */
597f81ef4a9SWill Deacon 	if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
598f81ef4a9SWill Deacon 		alignment_mask = 0x7;
5996ee33c27SWill Deacon 	offset = info->address & alignment_mask;
6006ee33c27SWill Deacon 	switch (offset) {
6016ee33c27SWill Deacon 	case 0:
6026ee33c27SWill Deacon 		/* Aligned */
6036ee33c27SWill Deacon 		break;
6046ee33c27SWill Deacon 	case 1:
6056ee33c27SWill Deacon 		/* Allow single byte watchpoint. */
6066ee33c27SWill Deacon 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
6076ee33c27SWill Deacon 			break;
6086ee33c27SWill Deacon 	case 2:
6096ee33c27SWill Deacon 		/* Allow halfword watchpoints and breakpoints. */
6106ee33c27SWill Deacon 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
6116ee33c27SWill Deacon 			break;
6126ee33c27SWill Deacon 	default:
6136ee33c27SWill Deacon 		ret = -EINVAL;
614f81ef4a9SWill Deacon 		goto out;
615f81ef4a9SWill Deacon 	}
616f81ef4a9SWill Deacon 
6176ee33c27SWill Deacon 	info->address &= ~alignment_mask;
618f81ef4a9SWill Deacon 	info->ctrl.len <<= offset;
619f81ef4a9SWill Deacon 
620f81ef4a9SWill Deacon 	/*
621f81ef4a9SWill Deacon 	 * Currently we rely on an overflow handler to take
622f81ef4a9SWill Deacon 	 * care of single-stepping the breakpoint when it fires.
623f81ef4a9SWill Deacon 	 * In the case of userspace breakpoints on a core with V7 debug,
6243ce70b2eSWill Deacon 	 * we can use the mismatch feature as a poor-man's hardware
6253ce70b2eSWill Deacon 	 * single-step, but this only works for per-task breakpoints.
626f81ef4a9SWill Deacon 	 */
627f81ef4a9SWill Deacon 	if (WARN_ONCE(!bp->overflow_handler &&
6283ce70b2eSWill Deacon 		(arch_check_bp_in_kernelspace(bp) || !core_has_mismatch_brps()
6293ce70b2eSWill Deacon 		 || !bp->hw.bp_target),
630f81ef4a9SWill Deacon 			"overflow handler required but none found")) {
631f81ef4a9SWill Deacon 		ret = -EINVAL;
632f81ef4a9SWill Deacon 	}
633f81ef4a9SWill Deacon out:
634f81ef4a9SWill Deacon 	return ret;
635f81ef4a9SWill Deacon }
636f81ef4a9SWill Deacon 
6379ebb3cbcSWill Deacon /*
6389ebb3cbcSWill Deacon  * Enable/disable single-stepping over the breakpoint bp at address addr.
6399ebb3cbcSWill Deacon  */
6409ebb3cbcSWill Deacon static void enable_single_step(struct perf_event *bp, u32 addr)
641f81ef4a9SWill Deacon {
6429ebb3cbcSWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
643f81ef4a9SWill Deacon 
6449ebb3cbcSWill Deacon 	arch_uninstall_hw_breakpoint(bp);
6459ebb3cbcSWill Deacon 	info->step_ctrl.mismatch  = 1;
6469ebb3cbcSWill Deacon 	info->step_ctrl.len	  = ARM_BREAKPOINT_LEN_4;
6479ebb3cbcSWill Deacon 	info->step_ctrl.type	  = ARM_BREAKPOINT_EXECUTE;
6489ebb3cbcSWill Deacon 	info->step_ctrl.privilege = info->ctrl.privilege;
6499ebb3cbcSWill Deacon 	info->step_ctrl.enabled	  = 1;
6509ebb3cbcSWill Deacon 	info->trigger		  = addr;
6519ebb3cbcSWill Deacon 	arch_install_hw_breakpoint(bp);
652f81ef4a9SWill Deacon }
6539ebb3cbcSWill Deacon 
6549ebb3cbcSWill Deacon static void disable_single_step(struct perf_event *bp)
6559ebb3cbcSWill Deacon {
6569ebb3cbcSWill Deacon 	arch_uninstall_hw_breakpoint(bp);
6579ebb3cbcSWill Deacon 	counter_arch_bp(bp)->step_ctrl.enabled = 0;
6589ebb3cbcSWill Deacon 	arch_install_hw_breakpoint(bp);
659f81ef4a9SWill Deacon }
660f81ef4a9SWill Deacon 
661f81ef4a9SWill Deacon static void watchpoint_handler(unsigned long unknown, struct pt_regs *regs)
662f81ef4a9SWill Deacon {
663f81ef4a9SWill Deacon 	int i;
6644a55c18eSWill Deacon 	struct perf_event *wp, **slots;
665f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
666f81ef4a9SWill Deacon 
6674a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
6684a55c18eSWill Deacon 
669f81ef4a9SWill Deacon 	/* Without a disassembler, we can only handle 1 watchpoint. */
670f81ef4a9SWill Deacon 	BUG_ON(core_num_wrps > 1);
671f81ef4a9SWill Deacon 
672f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
673f81ef4a9SWill Deacon 		rcu_read_lock();
674f81ef4a9SWill Deacon 
67593a04a34SWill Deacon 		wp = slots[i];
67693a04a34SWill Deacon 
67793a04a34SWill Deacon 		if (wp == NULL) {
678f81ef4a9SWill Deacon 			rcu_read_unlock();
679f81ef4a9SWill Deacon 			continue;
680f81ef4a9SWill Deacon 		}
681f81ef4a9SWill Deacon 
682f81ef4a9SWill Deacon 		/*
683f81ef4a9SWill Deacon 		 * The DFAR is an unknown value. Since we only allow a
684f81ef4a9SWill Deacon 		 * single watchpoint, we can set the trigger to the lowest
685f81ef4a9SWill Deacon 		 * possible faulting address.
686f81ef4a9SWill Deacon 		 */
68793a04a34SWill Deacon 		info = counter_arch_bp(wp);
68893a04a34SWill Deacon 		info->trigger = wp->attr.bp_addr;
689f81ef4a9SWill Deacon 		pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
69093a04a34SWill Deacon 		perf_bp_event(wp, regs);
691f81ef4a9SWill Deacon 
692f81ef4a9SWill Deacon 		/*
693f81ef4a9SWill Deacon 		 * If no overflow handler is present, insert a temporary
694f81ef4a9SWill Deacon 		 * mismatch breakpoint so we can single-step over the
695f81ef4a9SWill Deacon 		 * watchpoint trigger.
696f81ef4a9SWill Deacon 		 */
6979ebb3cbcSWill Deacon 		if (!wp->overflow_handler)
6989ebb3cbcSWill Deacon 			enable_single_step(wp, instruction_pointer(regs));
699f81ef4a9SWill Deacon 
700f81ef4a9SWill Deacon 		rcu_read_unlock();
701f81ef4a9SWill Deacon 	}
702f81ef4a9SWill Deacon }
703f81ef4a9SWill Deacon 
70493a04a34SWill Deacon static void watchpoint_single_step_handler(unsigned long pc)
70593a04a34SWill Deacon {
70693a04a34SWill Deacon 	int i;
7074a55c18eSWill Deacon 	struct perf_event *wp, **slots;
70893a04a34SWill Deacon 	struct arch_hw_breakpoint *info;
70993a04a34SWill Deacon 
7104a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
7114a55c18eSWill Deacon 
71293a04a34SWill Deacon 	for (i = 0; i < core_num_reserved_brps; ++i) {
71393a04a34SWill Deacon 		rcu_read_lock();
71493a04a34SWill Deacon 
71593a04a34SWill Deacon 		wp = slots[i];
71693a04a34SWill Deacon 
71793a04a34SWill Deacon 		if (wp == NULL)
71893a04a34SWill Deacon 			goto unlock;
71993a04a34SWill Deacon 
72093a04a34SWill Deacon 		info = counter_arch_bp(wp);
72193a04a34SWill Deacon 		if (!info->step_ctrl.enabled)
72293a04a34SWill Deacon 			goto unlock;
72393a04a34SWill Deacon 
72493a04a34SWill Deacon 		/*
72593a04a34SWill Deacon 		 * Restore the original watchpoint if we've completed the
72693a04a34SWill Deacon 		 * single-step.
72793a04a34SWill Deacon 		 */
7289ebb3cbcSWill Deacon 		if (info->trigger != pc)
7299ebb3cbcSWill Deacon 			disable_single_step(wp);
73093a04a34SWill Deacon 
73193a04a34SWill Deacon unlock:
73293a04a34SWill Deacon 		rcu_read_unlock();
73393a04a34SWill Deacon 	}
73493a04a34SWill Deacon }
73593a04a34SWill Deacon 
736f81ef4a9SWill Deacon static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
737f81ef4a9SWill Deacon {
738f81ef4a9SWill Deacon 	int i;
739f81ef4a9SWill Deacon 	u32 ctrl_reg, val, addr;
7404a55c18eSWill Deacon 	struct perf_event *bp, **slots;
741f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
742f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
743f81ef4a9SWill Deacon 
7444a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
7454a55c18eSWill Deacon 
746f81ef4a9SWill Deacon 	/* The exception entry code places the amended lr in the PC. */
747f81ef4a9SWill Deacon 	addr = regs->ARM_pc;
748f81ef4a9SWill Deacon 
74993a04a34SWill Deacon 	/* Check the currently installed breakpoints first. */
75093a04a34SWill Deacon 	for (i = 0; i < core_num_brps; ++i) {
751f81ef4a9SWill Deacon 		rcu_read_lock();
752f81ef4a9SWill Deacon 
753f81ef4a9SWill Deacon 		bp = slots[i];
754f81ef4a9SWill Deacon 
7559ebb3cbcSWill Deacon 		if (bp == NULL)
7569ebb3cbcSWill Deacon 			goto unlock;
757f81ef4a9SWill Deacon 
7589ebb3cbcSWill Deacon 		info = counter_arch_bp(bp);
759f81ef4a9SWill Deacon 
760f81ef4a9SWill Deacon 		/* Check if the breakpoint value matches. */
761f81ef4a9SWill Deacon 		val = read_wb_reg(ARM_BASE_BVR + i);
762f81ef4a9SWill Deacon 		if (val != (addr & ~0x3))
7639ebb3cbcSWill Deacon 			goto mismatch;
764f81ef4a9SWill Deacon 
765f81ef4a9SWill Deacon 		/* Possible match, check the byte address select to confirm. */
766f81ef4a9SWill Deacon 		ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
767f81ef4a9SWill Deacon 		decode_ctrl_reg(ctrl_reg, &ctrl);
768f81ef4a9SWill Deacon 		if ((1 << (addr & 0x3)) & ctrl.len) {
769f81ef4a9SWill Deacon 			info->trigger = addr;
770f81ef4a9SWill Deacon 			pr_debug("breakpoint fired: address = 0x%x\n", addr);
771f81ef4a9SWill Deacon 			perf_bp_event(bp, regs);
7729ebb3cbcSWill Deacon 			if (!bp->overflow_handler)
7739ebb3cbcSWill Deacon 				enable_single_step(bp, addr);
7749ebb3cbcSWill Deacon 			goto unlock;
775f81ef4a9SWill Deacon 		}
776f81ef4a9SWill Deacon 
7779ebb3cbcSWill Deacon mismatch:
7789ebb3cbcSWill Deacon 		/* If we're stepping a breakpoint, it can now be restored. */
7799ebb3cbcSWill Deacon 		if (info->step_ctrl.enabled)
7809ebb3cbcSWill Deacon 			disable_single_step(bp);
7819ebb3cbcSWill Deacon unlock:
782f81ef4a9SWill Deacon 		rcu_read_unlock();
783f81ef4a9SWill Deacon 	}
78493a04a34SWill Deacon 
78593a04a34SWill Deacon 	/* Handle any pending watchpoint single-step breakpoints. */
78693a04a34SWill Deacon 	watchpoint_single_step_handler(addr);
787f81ef4a9SWill Deacon }
788f81ef4a9SWill Deacon 
789f81ef4a9SWill Deacon /*
790f81ef4a9SWill Deacon  * Called from either the Data Abort Handler [watchpoint] or the
7917e202696SWill Deacon  * Prefetch Abort Handler [breakpoint] with preemption disabled.
792f81ef4a9SWill Deacon  */
793f81ef4a9SWill Deacon static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
794f81ef4a9SWill Deacon 				 struct pt_regs *regs)
795f81ef4a9SWill Deacon {
7967e202696SWill Deacon 	int ret = 0;
797f81ef4a9SWill Deacon 	u32 dscr;
798f81ef4a9SWill Deacon 
7997e202696SWill Deacon 	/* We must be called with preemption disabled. */
8007e202696SWill Deacon 	WARN_ON(preemptible());
8017e202696SWill Deacon 
802f81ef4a9SWill Deacon 	/* We only handle watchpoints and hardware breakpoints. */
803f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
804f81ef4a9SWill Deacon 
805f81ef4a9SWill Deacon 	/* Perform perf callbacks. */
806f81ef4a9SWill Deacon 	switch (ARM_DSCR_MOE(dscr)) {
807f81ef4a9SWill Deacon 	case ARM_ENTRY_BREAKPOINT:
808f81ef4a9SWill Deacon 		breakpoint_handler(addr, regs);
809f81ef4a9SWill Deacon 		break;
810f81ef4a9SWill Deacon 	case ARM_ENTRY_ASYNC_WATCHPOINT:
811235584b6SJoe Perches 		WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
812f81ef4a9SWill Deacon 	case ARM_ENTRY_SYNC_WATCHPOINT:
813f81ef4a9SWill Deacon 		watchpoint_handler(addr, regs);
814f81ef4a9SWill Deacon 		break;
815f81ef4a9SWill Deacon 	default:
8167e202696SWill Deacon 		ret = 1; /* Unhandled fault. */
817f81ef4a9SWill Deacon 	}
818f81ef4a9SWill Deacon 
8197e202696SWill Deacon 	/*
8207e202696SWill Deacon 	 * Re-enable preemption after it was disabled in the
8217e202696SWill Deacon 	 * low-level exception handling code.
8227e202696SWill Deacon 	 */
8237e202696SWill Deacon 	preempt_enable();
8247e202696SWill Deacon 
825f81ef4a9SWill Deacon 	return ret;
826f81ef4a9SWill Deacon }
827f81ef4a9SWill Deacon 
828f81ef4a9SWill Deacon /*
829f81ef4a9SWill Deacon  * One-time initialisation.
830f81ef4a9SWill Deacon  */
8317d99331eSWill Deacon static void reset_ctrl_regs(void *unused)
832f81ef4a9SWill Deacon {
833f81ef4a9SWill Deacon 	int i;
834f81ef4a9SWill Deacon 
835ac88e071SWill Deacon 	/*
836ac88e071SWill Deacon 	 * v7 debug contains save and restore registers so that debug state
837ac88e071SWill Deacon 	 * can be maintained across low-power modes without leaving
838ac88e071SWill Deacon 	 * the debug logic powered up. It is IMPLEMENTATION DEFINED whether
839ac88e071SWill Deacon 	 * we can write to the debug registers out of reset, so we must
840ac88e071SWill Deacon 	 * unlock the OS Lock Access Register to avoid taking undefined
841ac88e071SWill Deacon 	 * instruction exceptions later on.
842ac88e071SWill Deacon 	 */
843ac88e071SWill Deacon 	if (debug_arch >= ARM_DEBUG_ARCH_V7_ECP14) {
844ac88e071SWill Deacon 		/*
845ac88e071SWill Deacon 		 * Unconditionally clear the lock by writing a value
846ac88e071SWill Deacon 		 * other than 0xC5ACCE55 to the access register.
847ac88e071SWill Deacon 		 */
848ac88e071SWill Deacon 		asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0));
849ac88e071SWill Deacon 		isb();
850ac88e071SWill Deacon 	}
851ac88e071SWill Deacon 
852f81ef4a9SWill Deacon 	if (enable_monitor_mode())
853f81ef4a9SWill Deacon 		return;
854f81ef4a9SWill Deacon 
8550017ff42SWill Deacon 	/* We must also reset any reserved registers. */
8560017ff42SWill Deacon 	for (i = 0; i < core_num_brps + core_num_reserved_brps; ++i) {
857f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BCR + i, 0UL);
858f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BVR + i, 0UL);
859f81ef4a9SWill Deacon 	}
860f81ef4a9SWill Deacon 
861f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
862f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WCR + i, 0UL);
863f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WVR + i, 0UL);
864f81ef4a9SWill Deacon 	}
865f81ef4a9SWill Deacon }
866f81ef4a9SWill Deacon 
8677d99331eSWill Deacon static int __cpuinit dbg_reset_notify(struct notifier_block *self,
8687d99331eSWill Deacon 				      unsigned long action, void *cpu)
8697d99331eSWill Deacon {
8707d99331eSWill Deacon 	if (action == CPU_ONLINE)
8717d99331eSWill Deacon 		smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
8727d99331eSWill Deacon 	return NOTIFY_OK;
8737d99331eSWill Deacon }
8747d99331eSWill Deacon 
8757d99331eSWill Deacon static struct notifier_block __cpuinitdata dbg_reset_nb = {
8767d99331eSWill Deacon 	.notifier_call = dbg_reset_notify,
8777d99331eSWill Deacon };
8787d99331eSWill Deacon 
879f81ef4a9SWill Deacon static int __init arch_hw_breakpoint_init(void)
880f81ef4a9SWill Deacon {
881f81ef4a9SWill Deacon 	u32 dscr;
882f81ef4a9SWill Deacon 
883f81ef4a9SWill Deacon 	debug_arch = get_debug_arch();
884f81ef4a9SWill Deacon 
885f81ef4a9SWill Deacon 	if (debug_arch > ARM_DEBUG_ARCH_V7_ECP14) {
886f81ef4a9SWill Deacon 		pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
887*8fbf397cSWill Deacon 		return 0;
888f81ef4a9SWill Deacon 	}
889f81ef4a9SWill Deacon 
890f81ef4a9SWill Deacon 	/* Determine how many BRPs/WRPs are available. */
891f81ef4a9SWill Deacon 	core_num_brps = get_num_brps();
8920017ff42SWill Deacon 	core_num_reserved_brps = get_num_reserved_brps();
893f81ef4a9SWill Deacon 	core_num_wrps = get_num_wrps();
894f81ef4a9SWill Deacon 
895f81ef4a9SWill Deacon 	pr_info("found %d breakpoint and %d watchpoint registers.\n",
8960017ff42SWill Deacon 		core_num_brps + core_num_reserved_brps, core_num_wrps);
897f81ef4a9SWill Deacon 
8980017ff42SWill Deacon 	if (core_num_reserved_brps)
8990017ff42SWill Deacon 		pr_info("%d breakpoint(s) reserved for watchpoint "
9000017ff42SWill Deacon 				"single-step.\n", core_num_reserved_brps);
901f81ef4a9SWill Deacon 
902f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
903f81ef4a9SWill Deacon 	if (dscr & ARM_DSCR_HDBGEN) {
904f81ef4a9SWill Deacon 		pr_warning("halting debug mode enabled. Assuming maximum "
905f81ef4a9SWill Deacon 				"watchpoint size of 4 bytes.");
906f81ef4a9SWill Deacon 	} else {
907f81ef4a9SWill Deacon 		/*
908f81ef4a9SWill Deacon 		 * Reset the breakpoint resources. We assume that a halting
909f81ef4a9SWill Deacon 		 * debugger will leave the world in a nice state for us.
910f81ef4a9SWill Deacon 		 */
911f81ef4a9SWill Deacon 		smp_call_function(reset_ctrl_regs, NULL, 1);
912f81ef4a9SWill Deacon 		reset_ctrl_regs(NULL);
913ac88e071SWill Deacon 
914ac88e071SWill Deacon 		/* Work out the maximum supported watchpoint length. */
915ac88e071SWill Deacon 		max_watchpoint_len = get_max_wp_len();
916ac88e071SWill Deacon 		pr_info("maximum watchpoint size is %u bytes.\n",
917ac88e071SWill Deacon 				max_watchpoint_len);
918f81ef4a9SWill Deacon 	}
919f81ef4a9SWill Deacon 
920f81ef4a9SWill Deacon 	/* Register debug fault handler. */
921f81ef4a9SWill Deacon 	hook_fault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
922f81ef4a9SWill Deacon 			"watchpoint debug exception");
923f81ef4a9SWill Deacon 	hook_ifault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
924f81ef4a9SWill Deacon 			"breakpoint debug exception");
925f81ef4a9SWill Deacon 
9267d99331eSWill Deacon 	/* Register hotplug notifier. */
9277d99331eSWill Deacon 	register_cpu_notifier(&dbg_reset_nb);
928*8fbf397cSWill Deacon 	return 0;
929f81ef4a9SWill Deacon }
930f81ef4a9SWill Deacon arch_initcall(arch_hw_breakpoint_init);
931f81ef4a9SWill Deacon 
932f81ef4a9SWill Deacon void hw_breakpoint_pmu_read(struct perf_event *bp)
933f81ef4a9SWill Deacon {
934f81ef4a9SWill Deacon }
935f81ef4a9SWill Deacon 
936f81ef4a9SWill Deacon /*
937f81ef4a9SWill Deacon  * Dummy function to register with die_notifier.
938f81ef4a9SWill Deacon  */
939f81ef4a9SWill Deacon int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
940f81ef4a9SWill Deacon 					unsigned long val, void *data)
941f81ef4a9SWill Deacon {
942f81ef4a9SWill Deacon 	return NOTIFY_DONE;
943f81ef4a9SWill Deacon }
944