xref: /openbmc/linux/arch/arm/kernel/hw_breakpoint.c (revision bf8801145c01ab600f8df66e8c879ac642fa5846)
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/traps.h>
38f81ef4a9SWill Deacon 
39f81ef4a9SWill Deacon /* Breakpoint currently in use for each BRP. */
40f81ef4a9SWill Deacon static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
41f81ef4a9SWill Deacon 
42f81ef4a9SWill Deacon /* Watchpoint currently in use for each WRP. */
43f81ef4a9SWill Deacon static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
44f81ef4a9SWill Deacon 
45f81ef4a9SWill Deacon /* Number of BRP/WRP registers on this CPU. */
46f81ef4a9SWill Deacon static int core_num_brps;
47f81ef4a9SWill Deacon static int core_num_wrps;
48f81ef4a9SWill Deacon 
49f81ef4a9SWill Deacon /* Debug architecture version. */
50f81ef4a9SWill Deacon static u8 debug_arch;
51f81ef4a9SWill Deacon 
52f81ef4a9SWill Deacon /* Maximum supported watchpoint length. */
53f81ef4a9SWill Deacon static u8 max_watchpoint_len;
54f81ef4a9SWill Deacon 
55f81ef4a9SWill Deacon #define READ_WB_REG_CASE(OP2, M, VAL)		\
56f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):			\
57f81ef4a9SWill Deacon 		ARM_DBG_READ(c ## M, OP2, VAL); \
58f81ef4a9SWill Deacon 		break
59f81ef4a9SWill Deacon 
60f81ef4a9SWill Deacon #define WRITE_WB_REG_CASE(OP2, M, VAL)		\
61f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):			\
62f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c ## M, OP2, VAL);\
63f81ef4a9SWill Deacon 		break
64f81ef4a9SWill Deacon 
65f81ef4a9SWill Deacon #define GEN_READ_WB_REG_CASES(OP2, VAL)		\
66f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 0, VAL);		\
67f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 1, VAL);		\
68f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 2, VAL);		\
69f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 3, VAL);		\
70f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 4, VAL);		\
71f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 5, VAL);		\
72f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 6, VAL);		\
73f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 7, VAL);		\
74f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 8, VAL);		\
75f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 9, VAL);		\
76f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 10, VAL);		\
77f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 11, VAL);		\
78f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 12, VAL);		\
79f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 13, VAL);		\
80f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 14, VAL);		\
81f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 15, VAL)
82f81ef4a9SWill Deacon 
83f81ef4a9SWill Deacon #define GEN_WRITE_WB_REG_CASES(OP2, VAL)	\
84f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 0, VAL);		\
85f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 1, VAL);		\
86f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 2, VAL);		\
87f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 3, VAL);		\
88f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 4, VAL);		\
89f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 5, VAL);		\
90f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 6, VAL);		\
91f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 7, VAL);		\
92f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 8, VAL);		\
93f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 9, VAL);		\
94f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 10, VAL);	\
95f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 11, VAL);	\
96f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 12, VAL);	\
97f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 13, VAL);	\
98f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 14, VAL);	\
99f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 15, VAL)
100f81ef4a9SWill Deacon 
101f81ef4a9SWill Deacon static u32 read_wb_reg(int n)
102f81ef4a9SWill Deacon {
103f81ef4a9SWill Deacon 	u32 val = 0;
104f81ef4a9SWill Deacon 
105f81ef4a9SWill Deacon 	switch (n) {
106f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
107f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
108f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
109f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
110f81ef4a9SWill Deacon 	default:
111f81ef4a9SWill Deacon 		pr_warning("attempt to read from unknown breakpoint "
112f81ef4a9SWill Deacon 				"register %d\n", n);
113f81ef4a9SWill Deacon 	}
114f81ef4a9SWill Deacon 
115f81ef4a9SWill Deacon 	return val;
116f81ef4a9SWill Deacon }
117f81ef4a9SWill Deacon 
118f81ef4a9SWill Deacon static void write_wb_reg(int n, u32 val)
119f81ef4a9SWill Deacon {
120f81ef4a9SWill Deacon 	switch (n) {
121f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
122f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
123f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
124f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
125f81ef4a9SWill Deacon 	default:
126f81ef4a9SWill Deacon 		pr_warning("attempt to write to unknown breakpoint "
127f81ef4a9SWill Deacon 				"register %d\n", n);
128f81ef4a9SWill Deacon 	}
129f81ef4a9SWill Deacon 	isb();
130f81ef4a9SWill Deacon }
131f81ef4a9SWill Deacon 
1320017ff42SWill Deacon /* Determine debug architecture. */
1330017ff42SWill Deacon static u8 get_debug_arch(void)
1340017ff42SWill Deacon {
1350017ff42SWill Deacon 	u32 didr;
1360017ff42SWill Deacon 
1370017ff42SWill Deacon 	/* Do we implement the extended CPUID interface? */
138d1244336SWill Deacon 	if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
139d1244336SWill Deacon 		pr_warning("CPUID feature registers not supported. "
140d1244336SWill Deacon 			   "Assuming v6 debug is present.\n");
1410017ff42SWill Deacon 		return ARM_DEBUG_ARCH_V6;
142d1244336SWill Deacon 	}
1430017ff42SWill Deacon 
1440017ff42SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
1450017ff42SWill Deacon 	return (didr >> 16) & 0xf;
1460017ff42SWill Deacon }
1470017ff42SWill Deacon 
1480017ff42SWill Deacon u8 arch_get_debug_arch(void)
1490017ff42SWill Deacon {
1500017ff42SWill Deacon 	return debug_arch;
1510017ff42SWill Deacon }
1520017ff42SWill Deacon 
15366e1cfe6SWill Deacon static int debug_arch_supported(void)
15466e1cfe6SWill Deacon {
15566e1cfe6SWill Deacon 	u8 arch = get_debug_arch();
156b5d5b8f9SWill Deacon 
157b5d5b8f9SWill Deacon 	/* We don't support the memory-mapped interface. */
158b5d5b8f9SWill Deacon 	return (arch >= ARM_DEBUG_ARCH_V6 && arch <= ARM_DEBUG_ARCH_V7_ECP14) ||
159b5d5b8f9SWill Deacon 		arch >= ARM_DEBUG_ARCH_V7_1;
16066e1cfe6SWill Deacon }
16166e1cfe6SWill Deacon 
162*bf880114SWill Deacon /* Can we determine the watchpoint access type from the fsr? */
163*bf880114SWill Deacon static int debug_exception_updates_fsr(void)
164*bf880114SWill Deacon {
165*bf880114SWill Deacon 	return 0;
166*bf880114SWill Deacon }
167*bf880114SWill Deacon 
168c512de95SWill Deacon /* Determine number of WRP registers available. */
169c512de95SWill Deacon static int get_num_wrp_resources(void)
170c512de95SWill Deacon {
171c512de95SWill Deacon 	u32 didr;
172c512de95SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
173c512de95SWill Deacon 	return ((didr >> 28) & 0xf) + 1;
174c512de95SWill Deacon }
175c512de95SWill Deacon 
176c512de95SWill Deacon /* Determine number of BRP registers available. */
1770017ff42SWill Deacon static int get_num_brp_resources(void)
1780017ff42SWill Deacon {
1790017ff42SWill Deacon 	u32 didr;
1800017ff42SWill Deacon 	ARM_DBG_READ(c0, 0, didr);
1810017ff42SWill Deacon 	return ((didr >> 24) & 0xf) + 1;
1820017ff42SWill Deacon }
1830017ff42SWill Deacon 
1840017ff42SWill Deacon /* Does this core support mismatch breakpoints? */
1850017ff42SWill Deacon static int core_has_mismatch_brps(void)
1860017ff42SWill Deacon {
1870017ff42SWill Deacon 	return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
1880017ff42SWill Deacon 		get_num_brp_resources() > 1);
1890017ff42SWill Deacon }
1900017ff42SWill Deacon 
1910017ff42SWill Deacon /* Determine number of usable WRPs available. */
1920017ff42SWill Deacon static int get_num_wrps(void)
1930017ff42SWill Deacon {
1940017ff42SWill Deacon 	/*
195c512de95SWill Deacon 	 * On debug architectures prior to 7.1, when a watchpoint fires, the
196c512de95SWill Deacon 	 * only way to work out which watchpoint it was is by disassembling
197c512de95SWill Deacon 	 * the faulting instruction and working out the address of the memory
198c512de95SWill Deacon 	 * access.
1990017ff42SWill Deacon 	 *
2000017ff42SWill Deacon 	 * Furthermore, we can only do this if the watchpoint was precise
2010017ff42SWill Deacon 	 * since imprecise watchpoints prevent us from calculating register
2020017ff42SWill Deacon 	 * based addresses.
2030017ff42SWill Deacon 	 *
2040017ff42SWill Deacon 	 * Providing we have more than 1 breakpoint register, we only report
2050017ff42SWill Deacon 	 * a single watchpoint register for the time being. This way, we always
2060017ff42SWill Deacon 	 * know which watchpoint fired. In the future we can either add a
2070017ff42SWill Deacon 	 * disassembler and address generation emulator, or we can insert a
2080017ff42SWill Deacon 	 * check to see if the DFAR is set on watchpoint exception entry
2090017ff42SWill Deacon 	 * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
2100017ff42SWill Deacon 	 * that it is set on some implementations].
2110017ff42SWill Deacon 	 */
212c512de95SWill Deacon 	if (get_debug_arch() < ARM_DEBUG_ARCH_V7_1)
213c512de95SWill Deacon 		return 1;
2140017ff42SWill Deacon 
215c512de95SWill Deacon 	return get_num_wrp_resources();
2160017ff42SWill Deacon }
2170017ff42SWill Deacon 
2180017ff42SWill Deacon /* Determine number of usable BRPs available. */
2190017ff42SWill Deacon static int get_num_brps(void)
2200017ff42SWill Deacon {
2210017ff42SWill Deacon 	int brps = get_num_brp_resources();
222c512de95SWill Deacon 	return core_has_mismatch_brps() ? brps - 1 : brps;
2230017ff42SWill Deacon }
2240017ff42SWill Deacon 
225f81ef4a9SWill Deacon /*
226f81ef4a9SWill Deacon  * In order to access the breakpoint/watchpoint control registers,
227f81ef4a9SWill Deacon  * we must be running in debug monitor mode. Unfortunately, we can
228f81ef4a9SWill Deacon  * be put into halting debug mode at any time by an external debugger
229f81ef4a9SWill Deacon  * but there is nothing we can do to prevent that.
230f81ef4a9SWill Deacon  */
231f81ef4a9SWill Deacon static int enable_monitor_mode(void)
232f81ef4a9SWill Deacon {
233f81ef4a9SWill Deacon 	u32 dscr;
234f81ef4a9SWill Deacon 	int ret = 0;
235f81ef4a9SWill Deacon 
236f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
237f81ef4a9SWill Deacon 
238f81ef4a9SWill Deacon 	/* Ensure that halting mode is disabled. */
2397d85d61fSStephen Boyd 	if (WARN_ONCE(dscr & ARM_DSCR_HDBGEN,
2407d85d61fSStephen Boyd 		"halting debug mode enabled. Unable to access hardware resources.\n")) {
241f81ef4a9SWill Deacon 		ret = -EPERM;
242f81ef4a9SWill Deacon 		goto out;
243f81ef4a9SWill Deacon 	}
244f81ef4a9SWill Deacon 
2458fbf397cSWill Deacon 	/* If monitor mode is already enabled, just return. */
2468fbf397cSWill Deacon 	if (dscr & ARM_DSCR_MDBGEN)
2478fbf397cSWill Deacon 		goto out;
2488fbf397cSWill Deacon 
249f81ef4a9SWill Deacon 	/* Write to the corresponding DSCR. */
2508fbf397cSWill Deacon 	switch (get_debug_arch()) {
251f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6:
252f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6_1:
253f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN));
254f81ef4a9SWill Deacon 		break;
255f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V7_ECP14:
256b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_1:
257f81ef4a9SWill Deacon 		ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN));
258f81ef4a9SWill Deacon 		break;
259f81ef4a9SWill Deacon 	default:
260f81ef4a9SWill Deacon 		ret = -ENODEV;
261f81ef4a9SWill Deacon 		goto out;
262f81ef4a9SWill Deacon 	}
263f81ef4a9SWill Deacon 
264f81ef4a9SWill Deacon 	/* Check that the write made it through. */
265f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
2668fbf397cSWill Deacon 	if (!(dscr & ARM_DSCR_MDBGEN))
267f81ef4a9SWill Deacon 		ret = -EPERM;
268f81ef4a9SWill Deacon 
269f81ef4a9SWill Deacon out:
270f81ef4a9SWill Deacon 	return ret;
271f81ef4a9SWill Deacon }
272f81ef4a9SWill Deacon 
2738fbf397cSWill Deacon int hw_breakpoint_slots(int type)
2748fbf397cSWill Deacon {
27566e1cfe6SWill Deacon 	if (!debug_arch_supported())
27666e1cfe6SWill Deacon 		return 0;
27766e1cfe6SWill Deacon 
2788fbf397cSWill Deacon 	/*
2798fbf397cSWill Deacon 	 * We can be called early, so don't rely on
2808fbf397cSWill Deacon 	 * our static variables being initialised.
2818fbf397cSWill Deacon 	 */
2828fbf397cSWill Deacon 	switch (type) {
2838fbf397cSWill Deacon 	case TYPE_INST:
2848fbf397cSWill Deacon 		return get_num_brps();
2858fbf397cSWill Deacon 	case TYPE_DATA:
2868fbf397cSWill Deacon 		return get_num_wrps();
2878fbf397cSWill Deacon 	default:
2888fbf397cSWill Deacon 		pr_warning("unknown slot type: %d\n", type);
2898fbf397cSWill Deacon 		return 0;
2908fbf397cSWill Deacon 	}
2918fbf397cSWill Deacon }
2928fbf397cSWill Deacon 
293f81ef4a9SWill Deacon /*
294f81ef4a9SWill Deacon  * Check if 8-bit byte-address select is available.
295f81ef4a9SWill Deacon  * This clobbers WRP 0.
296f81ef4a9SWill Deacon  */
297f81ef4a9SWill Deacon static u8 get_max_wp_len(void)
298f81ef4a9SWill Deacon {
299f81ef4a9SWill Deacon 	u32 ctrl_reg;
300f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
301f81ef4a9SWill Deacon 	u8 size = 4;
302f81ef4a9SWill Deacon 
303f81ef4a9SWill Deacon 	if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
304f81ef4a9SWill Deacon 		goto out;
305f81ef4a9SWill Deacon 
306f81ef4a9SWill Deacon 	memset(&ctrl, 0, sizeof(ctrl));
307f81ef4a9SWill Deacon 	ctrl.len = ARM_BREAKPOINT_LEN_8;
308f81ef4a9SWill Deacon 	ctrl_reg = encode_ctrl_reg(ctrl);
309f81ef4a9SWill Deacon 
310f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WVR, 0);
311f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WCR, ctrl_reg);
312f81ef4a9SWill Deacon 	if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
313f81ef4a9SWill Deacon 		size = 8;
314f81ef4a9SWill Deacon 
315f81ef4a9SWill Deacon out:
316f81ef4a9SWill Deacon 	return size;
317f81ef4a9SWill Deacon }
318f81ef4a9SWill Deacon 
319f81ef4a9SWill Deacon u8 arch_get_max_wp_len(void)
320f81ef4a9SWill Deacon {
321f81ef4a9SWill Deacon 	return max_watchpoint_len;
322f81ef4a9SWill Deacon }
323f81ef4a9SWill Deacon 
324f81ef4a9SWill Deacon /*
325f81ef4a9SWill Deacon  * Install a perf counter breakpoint.
326f81ef4a9SWill Deacon  */
327f81ef4a9SWill Deacon int arch_install_hw_breakpoint(struct perf_event *bp)
328f81ef4a9SWill Deacon {
329f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
330f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
331f81ef4a9SWill Deacon 	int i, max_slots, ctrl_base, val_base, ret = 0;
33293a04a34SWill Deacon 	u32 addr, ctrl;
333f81ef4a9SWill Deacon 
334f81ef4a9SWill Deacon 	/* Ensure that we are in monitor mode and halting mode is disabled. */
335f81ef4a9SWill Deacon 	ret = enable_monitor_mode();
336f81ef4a9SWill Deacon 	if (ret)
337f81ef4a9SWill Deacon 		goto out;
338f81ef4a9SWill Deacon 
33993a04a34SWill Deacon 	addr = info->address;
34093a04a34SWill Deacon 	ctrl = encode_ctrl_reg(info->ctrl) | 0x1;
34193a04a34SWill Deacon 
342f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
343f81ef4a9SWill Deacon 		/* Breakpoint */
344f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_BCR;
345f81ef4a9SWill Deacon 		val_base = ARM_BASE_BVR;
3464a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
3470017ff42SWill Deacon 		max_slots = core_num_brps;
348f81ef4a9SWill Deacon 	} else {
349f81ef4a9SWill Deacon 		/* Watchpoint */
350f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_WCR;
351f81ef4a9SWill Deacon 		val_base = ARM_BASE_WVR;
3524a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
353f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
354f81ef4a9SWill Deacon 	}
355f81ef4a9SWill Deacon 
356f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
357f81ef4a9SWill Deacon 		slot = &slots[i];
358f81ef4a9SWill Deacon 
359f81ef4a9SWill Deacon 		if (!*slot) {
360f81ef4a9SWill Deacon 			*slot = bp;
361f81ef4a9SWill Deacon 			break;
362f81ef4a9SWill Deacon 		}
363f81ef4a9SWill Deacon 	}
364f81ef4a9SWill Deacon 
3657d85d61fSStephen Boyd 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n")) {
366f81ef4a9SWill Deacon 		ret = -EBUSY;
367f81ef4a9SWill Deacon 		goto out;
368f81ef4a9SWill Deacon 	}
369f81ef4a9SWill Deacon 
3706f26aa05SWill Deacon 	/* Override the breakpoint data with the step data. */
3716f26aa05SWill Deacon 	if (info->step_ctrl.enabled) {
3726f26aa05SWill Deacon 		addr = info->trigger & ~0x3;
3736f26aa05SWill Deacon 		ctrl = encode_ctrl_reg(info->step_ctrl);
3746f26aa05SWill Deacon 		if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE) {
3756f26aa05SWill Deacon 			i = 0;
3766f26aa05SWill Deacon 			ctrl_base = ARM_BASE_BCR + core_num_brps;
3776f26aa05SWill Deacon 			val_base = ARM_BASE_BVR + core_num_brps;
3786f26aa05SWill Deacon 		}
3796f26aa05SWill Deacon 	}
3806f26aa05SWill Deacon 
381f81ef4a9SWill Deacon 	/* Setup the address register. */
38293a04a34SWill Deacon 	write_wb_reg(val_base + i, addr);
383f81ef4a9SWill Deacon 
384f81ef4a9SWill Deacon 	/* Setup the control register. */
38593a04a34SWill Deacon 	write_wb_reg(ctrl_base + i, ctrl);
386f81ef4a9SWill Deacon 
387f81ef4a9SWill Deacon out:
388f81ef4a9SWill Deacon 	return ret;
389f81ef4a9SWill Deacon }
390f81ef4a9SWill Deacon 
391f81ef4a9SWill Deacon void arch_uninstall_hw_breakpoint(struct perf_event *bp)
392f81ef4a9SWill Deacon {
393f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
394f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
395f81ef4a9SWill Deacon 	int i, max_slots, base;
396f81ef4a9SWill Deacon 
397f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
398f81ef4a9SWill Deacon 		/* Breakpoint */
399f81ef4a9SWill Deacon 		base = ARM_BASE_BCR;
4004a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
4010017ff42SWill Deacon 		max_slots = core_num_brps;
402f81ef4a9SWill Deacon 	} else {
403f81ef4a9SWill Deacon 		/* Watchpoint */
404f81ef4a9SWill Deacon 		base = ARM_BASE_WCR;
4054a55c18eSWill Deacon 		slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
406f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
407f81ef4a9SWill Deacon 	}
408f81ef4a9SWill Deacon 
409f81ef4a9SWill Deacon 	/* Remove the breakpoint. */
410f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
411f81ef4a9SWill Deacon 		slot = &slots[i];
412f81ef4a9SWill Deacon 
413f81ef4a9SWill Deacon 		if (*slot == bp) {
414f81ef4a9SWill Deacon 			*slot = NULL;
415f81ef4a9SWill Deacon 			break;
416f81ef4a9SWill Deacon 		}
417f81ef4a9SWill Deacon 	}
418f81ef4a9SWill Deacon 
4197d85d61fSStephen Boyd 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n"))
420f81ef4a9SWill Deacon 		return;
421f81ef4a9SWill Deacon 
4226f26aa05SWill Deacon 	/* Ensure that we disable the mismatch breakpoint. */
4236f26aa05SWill Deacon 	if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE &&
4246f26aa05SWill Deacon 	    info->step_ctrl.enabled) {
4256f26aa05SWill Deacon 		i = 0;
4266f26aa05SWill Deacon 		base = ARM_BASE_BCR + core_num_brps;
4276f26aa05SWill Deacon 	}
4286f26aa05SWill Deacon 
429f81ef4a9SWill Deacon 	/* Reset the control register. */
430f81ef4a9SWill Deacon 	write_wb_reg(base + i, 0);
431f81ef4a9SWill Deacon }
432f81ef4a9SWill Deacon 
433f81ef4a9SWill Deacon static int get_hbp_len(u8 hbp_len)
434f81ef4a9SWill Deacon {
435f81ef4a9SWill Deacon 	unsigned int len_in_bytes = 0;
436f81ef4a9SWill Deacon 
437f81ef4a9SWill Deacon 	switch (hbp_len) {
438f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
439f81ef4a9SWill Deacon 		len_in_bytes = 1;
440f81ef4a9SWill Deacon 		break;
441f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
442f81ef4a9SWill Deacon 		len_in_bytes = 2;
443f81ef4a9SWill Deacon 		break;
444f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
445f81ef4a9SWill Deacon 		len_in_bytes = 4;
446f81ef4a9SWill Deacon 		break;
447f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
448f81ef4a9SWill Deacon 		len_in_bytes = 8;
449f81ef4a9SWill Deacon 		break;
450f81ef4a9SWill Deacon 	}
451f81ef4a9SWill Deacon 
452f81ef4a9SWill Deacon 	return len_in_bytes;
453f81ef4a9SWill Deacon }
454f81ef4a9SWill Deacon 
455f81ef4a9SWill Deacon /*
456f81ef4a9SWill Deacon  * Check whether bp virtual address is in kernel space.
457f81ef4a9SWill Deacon  */
458f81ef4a9SWill Deacon int arch_check_bp_in_kernelspace(struct perf_event *bp)
459f81ef4a9SWill Deacon {
460f81ef4a9SWill Deacon 	unsigned int len;
461f81ef4a9SWill Deacon 	unsigned long va;
462f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
463f81ef4a9SWill Deacon 
464f81ef4a9SWill Deacon 	va = info->address;
465f81ef4a9SWill Deacon 	len = get_hbp_len(info->ctrl.len);
466f81ef4a9SWill Deacon 
467f81ef4a9SWill Deacon 	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
468f81ef4a9SWill Deacon }
469f81ef4a9SWill Deacon 
470f81ef4a9SWill Deacon /*
471f81ef4a9SWill Deacon  * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
472f81ef4a9SWill Deacon  * Hopefully this will disappear when ptrace can bypass the conversion
473f81ef4a9SWill Deacon  * to generic breakpoint descriptions.
474f81ef4a9SWill Deacon  */
475f81ef4a9SWill Deacon int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
476f81ef4a9SWill Deacon 			   int *gen_len, int *gen_type)
477f81ef4a9SWill Deacon {
478f81ef4a9SWill Deacon 	/* Type */
479f81ef4a9SWill Deacon 	switch (ctrl.type) {
480f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_EXECUTE:
481f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_X;
482f81ef4a9SWill Deacon 		break;
483f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD:
484f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_R;
485f81ef4a9SWill Deacon 		break;
486f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_STORE:
487f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_W;
488f81ef4a9SWill Deacon 		break;
489f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
490f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_RW;
491f81ef4a9SWill Deacon 		break;
492f81ef4a9SWill Deacon 	default:
493f81ef4a9SWill Deacon 		return -EINVAL;
494f81ef4a9SWill Deacon 	}
495f81ef4a9SWill Deacon 
496f81ef4a9SWill Deacon 	/* Len */
497f81ef4a9SWill Deacon 	switch (ctrl.len) {
498f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
499f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_1;
500f81ef4a9SWill Deacon 		break;
501f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
502f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_2;
503f81ef4a9SWill Deacon 		break;
504f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
505f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_4;
506f81ef4a9SWill Deacon 		break;
507f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
508f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_8;
509f81ef4a9SWill Deacon 		break;
510f81ef4a9SWill Deacon 	default:
511f81ef4a9SWill Deacon 		return -EINVAL;
512f81ef4a9SWill Deacon 	}
513f81ef4a9SWill Deacon 
514f81ef4a9SWill Deacon 	return 0;
515f81ef4a9SWill Deacon }
516f81ef4a9SWill Deacon 
517f81ef4a9SWill Deacon /*
518f81ef4a9SWill Deacon  * Construct an arch_hw_breakpoint from a perf_event.
519f81ef4a9SWill Deacon  */
520f81ef4a9SWill Deacon static int arch_build_bp_info(struct perf_event *bp)
521f81ef4a9SWill Deacon {
522f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
523f81ef4a9SWill Deacon 
524f81ef4a9SWill Deacon 	/* Type */
525f81ef4a9SWill Deacon 	switch (bp->attr.bp_type) {
526f81ef4a9SWill Deacon 	case HW_BREAKPOINT_X:
527f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
528f81ef4a9SWill Deacon 		break;
529f81ef4a9SWill Deacon 	case HW_BREAKPOINT_R:
530f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD;
531f81ef4a9SWill Deacon 		break;
532f81ef4a9SWill Deacon 	case HW_BREAKPOINT_W:
533f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_STORE;
534f81ef4a9SWill Deacon 		break;
535f81ef4a9SWill Deacon 	case HW_BREAKPOINT_RW:
536f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
537f81ef4a9SWill Deacon 		break;
538f81ef4a9SWill Deacon 	default:
539f81ef4a9SWill Deacon 		return -EINVAL;
540f81ef4a9SWill Deacon 	}
541f81ef4a9SWill Deacon 
542f81ef4a9SWill Deacon 	/* Len */
543f81ef4a9SWill Deacon 	switch (bp->attr.bp_len) {
544f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_1:
545f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_1;
546f81ef4a9SWill Deacon 		break;
547f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_2:
548f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_2;
549f81ef4a9SWill Deacon 		break;
550f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_4:
551f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_4;
552f81ef4a9SWill Deacon 		break;
553f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_8:
554f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_8;
555f81ef4a9SWill Deacon 		if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
556f81ef4a9SWill Deacon 			&& max_watchpoint_len >= 8)
557f81ef4a9SWill Deacon 			break;
558f81ef4a9SWill Deacon 	default:
559f81ef4a9SWill Deacon 		return -EINVAL;
560f81ef4a9SWill Deacon 	}
561f81ef4a9SWill Deacon 
5626ee33c27SWill Deacon 	/*
5636ee33c27SWill Deacon 	 * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
5646ee33c27SWill Deacon 	 * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported
5656ee33c27SWill Deacon 	 * by the hardware and must be aligned to the appropriate number of
5666ee33c27SWill Deacon 	 * bytes.
5676ee33c27SWill Deacon 	 */
5686ee33c27SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
5696ee33c27SWill Deacon 	    info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
5706ee33c27SWill Deacon 	    info->ctrl.len != ARM_BREAKPOINT_LEN_4)
5716ee33c27SWill Deacon 		return -EINVAL;
5726ee33c27SWill Deacon 
573f81ef4a9SWill Deacon 	/* Address */
574f81ef4a9SWill Deacon 	info->address = bp->attr.bp_addr;
575f81ef4a9SWill Deacon 
576f81ef4a9SWill Deacon 	/* Privilege */
577f81ef4a9SWill Deacon 	info->ctrl.privilege = ARM_BREAKPOINT_USER;
57893a04a34SWill Deacon 	if (arch_check_bp_in_kernelspace(bp))
579f81ef4a9SWill Deacon 		info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
580f81ef4a9SWill Deacon 
581f81ef4a9SWill Deacon 	/* Enabled? */
582f81ef4a9SWill Deacon 	info->ctrl.enabled = !bp->attr.disabled;
583f81ef4a9SWill Deacon 
584f81ef4a9SWill Deacon 	/* Mismatch */
585f81ef4a9SWill Deacon 	info->ctrl.mismatch = 0;
586f81ef4a9SWill Deacon 
587f81ef4a9SWill Deacon 	return 0;
588f81ef4a9SWill Deacon }
589f81ef4a9SWill Deacon 
590f81ef4a9SWill Deacon /*
591f81ef4a9SWill Deacon  * Validate the arch-specific HW Breakpoint register settings.
592f81ef4a9SWill Deacon  */
593f81ef4a9SWill Deacon int arch_validate_hwbkpt_settings(struct perf_event *bp)
594f81ef4a9SWill Deacon {
595f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
596f81ef4a9SWill Deacon 	int ret = 0;
5976ee33c27SWill Deacon 	u32 offset, alignment_mask = 0x3;
598f81ef4a9SWill Deacon 
599f81ef4a9SWill Deacon 	/* Build the arch_hw_breakpoint. */
600f81ef4a9SWill Deacon 	ret = arch_build_bp_info(bp);
601f81ef4a9SWill Deacon 	if (ret)
602f81ef4a9SWill Deacon 		goto out;
603f81ef4a9SWill Deacon 
604f81ef4a9SWill Deacon 	/* Check address alignment. */
605f81ef4a9SWill Deacon 	if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
606f81ef4a9SWill Deacon 		alignment_mask = 0x7;
6076ee33c27SWill Deacon 	offset = info->address & alignment_mask;
6086ee33c27SWill Deacon 	switch (offset) {
6096ee33c27SWill Deacon 	case 0:
6106ee33c27SWill Deacon 		/* Aligned */
6116ee33c27SWill Deacon 		break;
6126ee33c27SWill Deacon 	case 1:
6136ee33c27SWill Deacon 		/* Allow single byte watchpoint. */
6146ee33c27SWill Deacon 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
6156ee33c27SWill Deacon 			break;
6166ee33c27SWill Deacon 	case 2:
6176ee33c27SWill Deacon 		/* Allow halfword watchpoints and breakpoints. */
6186ee33c27SWill Deacon 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
6196ee33c27SWill Deacon 			break;
6206ee33c27SWill Deacon 	default:
6216ee33c27SWill Deacon 		ret = -EINVAL;
622f81ef4a9SWill Deacon 		goto out;
623f81ef4a9SWill Deacon 	}
624f81ef4a9SWill Deacon 
6256ee33c27SWill Deacon 	info->address &= ~alignment_mask;
626f81ef4a9SWill Deacon 	info->ctrl.len <<= offset;
627f81ef4a9SWill Deacon 
628*bf880114SWill Deacon 	if (!bp->overflow_handler) {
629f81ef4a9SWill Deacon 		/*
630*bf880114SWill Deacon 		 * Mismatch breakpoints are required for single-stepping
631*bf880114SWill Deacon 		 * breakpoints.
632f81ef4a9SWill Deacon 		 */
633*bf880114SWill Deacon 		if (!core_has_mismatch_brps())
634*bf880114SWill Deacon 			return -EINVAL;
635*bf880114SWill Deacon 
636*bf880114SWill Deacon 		/* We don't allow mismatch breakpoints in kernel space. */
637*bf880114SWill Deacon 		if (arch_check_bp_in_kernelspace(bp))
638*bf880114SWill Deacon 			return -EPERM;
639*bf880114SWill Deacon 
640*bf880114SWill Deacon 		/*
641*bf880114SWill Deacon 		 * Per-cpu breakpoints are not supported by our stepping
642*bf880114SWill Deacon 		 * mechanism.
643*bf880114SWill Deacon 		 */
644*bf880114SWill Deacon 		if (!bp->hw.bp_target)
645*bf880114SWill Deacon 			return -EINVAL;
646*bf880114SWill Deacon 
647*bf880114SWill Deacon 		/*
648*bf880114SWill Deacon 		 * We only support specific access types if the fsr
649*bf880114SWill Deacon 		 * reports them.
650*bf880114SWill Deacon 		 */
651*bf880114SWill Deacon 		if (!debug_exception_updates_fsr() &&
652*bf880114SWill Deacon 		    (info->ctrl.type == ARM_BREAKPOINT_LOAD ||
653*bf880114SWill Deacon 		     info->ctrl.type == ARM_BREAKPOINT_STORE))
654*bf880114SWill Deacon 			return -EINVAL;
655f81ef4a9SWill Deacon 	}
656*bf880114SWill Deacon 
657f81ef4a9SWill Deacon out:
658f81ef4a9SWill Deacon 	return ret;
659f81ef4a9SWill Deacon }
660f81ef4a9SWill Deacon 
6619ebb3cbcSWill Deacon /*
6629ebb3cbcSWill Deacon  * Enable/disable single-stepping over the breakpoint bp at address addr.
6639ebb3cbcSWill Deacon  */
6649ebb3cbcSWill Deacon static void enable_single_step(struct perf_event *bp, u32 addr)
665f81ef4a9SWill Deacon {
6669ebb3cbcSWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
667f81ef4a9SWill Deacon 
6689ebb3cbcSWill Deacon 	arch_uninstall_hw_breakpoint(bp);
6699ebb3cbcSWill Deacon 	info->step_ctrl.mismatch  = 1;
6709ebb3cbcSWill Deacon 	info->step_ctrl.len	  = ARM_BREAKPOINT_LEN_4;
6719ebb3cbcSWill Deacon 	info->step_ctrl.type	  = ARM_BREAKPOINT_EXECUTE;
6729ebb3cbcSWill Deacon 	info->step_ctrl.privilege = info->ctrl.privilege;
6739ebb3cbcSWill Deacon 	info->step_ctrl.enabled	  = 1;
6749ebb3cbcSWill Deacon 	info->trigger		  = addr;
6759ebb3cbcSWill Deacon 	arch_install_hw_breakpoint(bp);
676f81ef4a9SWill Deacon }
6779ebb3cbcSWill Deacon 
6789ebb3cbcSWill Deacon static void disable_single_step(struct perf_event *bp)
6799ebb3cbcSWill Deacon {
6809ebb3cbcSWill Deacon 	arch_uninstall_hw_breakpoint(bp);
6819ebb3cbcSWill Deacon 	counter_arch_bp(bp)->step_ctrl.enabled = 0;
6829ebb3cbcSWill Deacon 	arch_install_hw_breakpoint(bp);
683f81ef4a9SWill Deacon }
684f81ef4a9SWill Deacon 
6856f26aa05SWill Deacon static void watchpoint_handler(unsigned long addr, unsigned int fsr,
6866f26aa05SWill Deacon 			       struct pt_regs *regs)
687f81ef4a9SWill Deacon {
6886f26aa05SWill Deacon 	int i, access;
6896f26aa05SWill Deacon 	u32 val, ctrl_reg, alignment_mask;
6904a55c18eSWill Deacon 	struct perf_event *wp, **slots;
691f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
6926f26aa05SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
693f81ef4a9SWill Deacon 
6944a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
6954a55c18eSWill Deacon 
696f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
697f81ef4a9SWill Deacon 		rcu_read_lock();
698f81ef4a9SWill Deacon 
69993a04a34SWill Deacon 		wp = slots[i];
70093a04a34SWill Deacon 
7016f26aa05SWill Deacon 		if (wp == NULL)
7026f26aa05SWill Deacon 			goto unlock;
7036f26aa05SWill Deacon 
7046f26aa05SWill Deacon 		info = counter_arch_bp(wp);
7056f26aa05SWill Deacon 		/*
7066f26aa05SWill Deacon 		 * The DFAR is an unknown value on debug architectures prior
7076f26aa05SWill Deacon 		 * to 7.1. Since we only allow a single watchpoint on these
7086f26aa05SWill Deacon 		 * older CPUs, we can set the trigger to the lowest possible
7096f26aa05SWill Deacon 		 * faulting address.
7106f26aa05SWill Deacon 		 */
7116f26aa05SWill Deacon 		if (debug_arch < ARM_DEBUG_ARCH_V7_1) {
7126f26aa05SWill Deacon 			BUG_ON(i > 0);
7136f26aa05SWill Deacon 			info->trigger = wp->attr.bp_addr;
7146f26aa05SWill Deacon 		} else {
7156f26aa05SWill Deacon 			if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
7166f26aa05SWill Deacon 				alignment_mask = 0x7;
7176f26aa05SWill Deacon 			else
7186f26aa05SWill Deacon 				alignment_mask = 0x3;
7196f26aa05SWill Deacon 
7206f26aa05SWill Deacon 			/* Check if the watchpoint value matches. */
7216f26aa05SWill Deacon 			val = read_wb_reg(ARM_BASE_WVR + i);
7226f26aa05SWill Deacon 			if (val != (addr & ~alignment_mask))
7236f26aa05SWill Deacon 				goto unlock;
7246f26aa05SWill Deacon 
7256f26aa05SWill Deacon 			/* Possible match, check the byte address select. */
7266f26aa05SWill Deacon 			ctrl_reg = read_wb_reg(ARM_BASE_WCR + i);
7276f26aa05SWill Deacon 			decode_ctrl_reg(ctrl_reg, &ctrl);
7286f26aa05SWill Deacon 			if (!((1 << (addr & alignment_mask)) & ctrl.len))
7296f26aa05SWill Deacon 				goto unlock;
7306f26aa05SWill Deacon 
7316f26aa05SWill Deacon 			/* Check that the access type matches. */
732*bf880114SWill Deacon 			if (debug_exception_updates_fsr()) {
733*bf880114SWill Deacon 				access = (fsr & ARM_FSR_ACCESS_MASK) ?
734*bf880114SWill Deacon 					  HW_BREAKPOINT_W : HW_BREAKPOINT_R;
7356f26aa05SWill Deacon 				if (!(access & hw_breakpoint_type(wp)))
7366f26aa05SWill Deacon 					goto unlock;
737*bf880114SWill Deacon 			}
7386f26aa05SWill Deacon 
7396f26aa05SWill Deacon 			/* We have a winner. */
7406f26aa05SWill Deacon 			info->trigger = addr;
741f81ef4a9SWill Deacon 		}
742f81ef4a9SWill Deacon 
743f81ef4a9SWill Deacon 		pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
74493a04a34SWill Deacon 		perf_bp_event(wp, regs);
745f81ef4a9SWill Deacon 
746f81ef4a9SWill Deacon 		/*
747f81ef4a9SWill Deacon 		 * If no overflow handler is present, insert a temporary
748f81ef4a9SWill Deacon 		 * mismatch breakpoint so we can single-step over the
749f81ef4a9SWill Deacon 		 * watchpoint trigger.
750f81ef4a9SWill Deacon 		 */
7519ebb3cbcSWill Deacon 		if (!wp->overflow_handler)
7529ebb3cbcSWill Deacon 			enable_single_step(wp, instruction_pointer(regs));
753f81ef4a9SWill Deacon 
7546f26aa05SWill Deacon unlock:
755f81ef4a9SWill Deacon 		rcu_read_unlock();
756f81ef4a9SWill Deacon 	}
757f81ef4a9SWill Deacon }
758f81ef4a9SWill Deacon 
75993a04a34SWill Deacon static void watchpoint_single_step_handler(unsigned long pc)
76093a04a34SWill Deacon {
76193a04a34SWill Deacon 	int i;
7624a55c18eSWill Deacon 	struct perf_event *wp, **slots;
76393a04a34SWill Deacon 	struct arch_hw_breakpoint *info;
76493a04a34SWill Deacon 
7654a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
7664a55c18eSWill Deacon 
767c512de95SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
76893a04a34SWill Deacon 		rcu_read_lock();
76993a04a34SWill Deacon 
77093a04a34SWill Deacon 		wp = slots[i];
77193a04a34SWill Deacon 
77293a04a34SWill Deacon 		if (wp == NULL)
77393a04a34SWill Deacon 			goto unlock;
77493a04a34SWill Deacon 
77593a04a34SWill Deacon 		info = counter_arch_bp(wp);
77693a04a34SWill Deacon 		if (!info->step_ctrl.enabled)
77793a04a34SWill Deacon 			goto unlock;
77893a04a34SWill Deacon 
77993a04a34SWill Deacon 		/*
78093a04a34SWill Deacon 		 * Restore the original watchpoint if we've completed the
78193a04a34SWill Deacon 		 * single-step.
78293a04a34SWill Deacon 		 */
7839ebb3cbcSWill Deacon 		if (info->trigger != pc)
7849ebb3cbcSWill Deacon 			disable_single_step(wp);
78593a04a34SWill Deacon 
78693a04a34SWill Deacon unlock:
78793a04a34SWill Deacon 		rcu_read_unlock();
78893a04a34SWill Deacon 	}
78993a04a34SWill Deacon }
79093a04a34SWill Deacon 
791f81ef4a9SWill Deacon static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
792f81ef4a9SWill Deacon {
793f81ef4a9SWill Deacon 	int i;
794f81ef4a9SWill Deacon 	u32 ctrl_reg, val, addr;
7954a55c18eSWill Deacon 	struct perf_event *bp, **slots;
796f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
797f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
798f81ef4a9SWill Deacon 
7994a55c18eSWill Deacon 	slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
8004a55c18eSWill Deacon 
801f81ef4a9SWill Deacon 	/* The exception entry code places the amended lr in the PC. */
802f81ef4a9SWill Deacon 	addr = regs->ARM_pc;
803f81ef4a9SWill Deacon 
80493a04a34SWill Deacon 	/* Check the currently installed breakpoints first. */
80593a04a34SWill Deacon 	for (i = 0; i < core_num_brps; ++i) {
806f81ef4a9SWill Deacon 		rcu_read_lock();
807f81ef4a9SWill Deacon 
808f81ef4a9SWill Deacon 		bp = slots[i];
809f81ef4a9SWill Deacon 
8109ebb3cbcSWill Deacon 		if (bp == NULL)
8119ebb3cbcSWill Deacon 			goto unlock;
812f81ef4a9SWill Deacon 
8139ebb3cbcSWill Deacon 		info = counter_arch_bp(bp);
814f81ef4a9SWill Deacon 
815f81ef4a9SWill Deacon 		/* Check if the breakpoint value matches. */
816f81ef4a9SWill Deacon 		val = read_wb_reg(ARM_BASE_BVR + i);
817f81ef4a9SWill Deacon 		if (val != (addr & ~0x3))
8189ebb3cbcSWill Deacon 			goto mismatch;
819f81ef4a9SWill Deacon 
820f81ef4a9SWill Deacon 		/* Possible match, check the byte address select to confirm. */
821f81ef4a9SWill Deacon 		ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
822f81ef4a9SWill Deacon 		decode_ctrl_reg(ctrl_reg, &ctrl);
823f81ef4a9SWill Deacon 		if ((1 << (addr & 0x3)) & ctrl.len) {
824f81ef4a9SWill Deacon 			info->trigger = addr;
825f81ef4a9SWill Deacon 			pr_debug("breakpoint fired: address = 0x%x\n", addr);
826f81ef4a9SWill Deacon 			perf_bp_event(bp, regs);
8279ebb3cbcSWill Deacon 			if (!bp->overflow_handler)
8289ebb3cbcSWill Deacon 				enable_single_step(bp, addr);
8299ebb3cbcSWill Deacon 			goto unlock;
830f81ef4a9SWill Deacon 		}
831f81ef4a9SWill Deacon 
8329ebb3cbcSWill Deacon mismatch:
8339ebb3cbcSWill Deacon 		/* If we're stepping a breakpoint, it can now be restored. */
8349ebb3cbcSWill Deacon 		if (info->step_ctrl.enabled)
8359ebb3cbcSWill Deacon 			disable_single_step(bp);
8369ebb3cbcSWill Deacon unlock:
837f81ef4a9SWill Deacon 		rcu_read_unlock();
838f81ef4a9SWill Deacon 	}
83993a04a34SWill Deacon 
84093a04a34SWill Deacon 	/* Handle any pending watchpoint single-step breakpoints. */
84193a04a34SWill Deacon 	watchpoint_single_step_handler(addr);
842f81ef4a9SWill Deacon }
843f81ef4a9SWill Deacon 
844f81ef4a9SWill Deacon /*
845f81ef4a9SWill Deacon  * Called from either the Data Abort Handler [watchpoint] or the
84602fe2845SRussell King  * Prefetch Abort Handler [breakpoint] with interrupts disabled.
847f81ef4a9SWill Deacon  */
848f81ef4a9SWill Deacon static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
849f81ef4a9SWill Deacon 				 struct pt_regs *regs)
850f81ef4a9SWill Deacon {
8517e202696SWill Deacon 	int ret = 0;
852f81ef4a9SWill Deacon 	u32 dscr;
853f81ef4a9SWill Deacon 
85402fe2845SRussell King 	preempt_disable();
85502fe2845SRussell King 
85602fe2845SRussell King 	if (interrupts_enabled(regs))
85702fe2845SRussell King 		local_irq_enable();
8587e202696SWill Deacon 
859f81ef4a9SWill Deacon 	/* We only handle watchpoints and hardware breakpoints. */
860f81ef4a9SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
861f81ef4a9SWill Deacon 
862f81ef4a9SWill Deacon 	/* Perform perf callbacks. */
863f81ef4a9SWill Deacon 	switch (ARM_DSCR_MOE(dscr)) {
864f81ef4a9SWill Deacon 	case ARM_ENTRY_BREAKPOINT:
865f81ef4a9SWill Deacon 		breakpoint_handler(addr, regs);
866f81ef4a9SWill Deacon 		break;
867f81ef4a9SWill Deacon 	case ARM_ENTRY_ASYNC_WATCHPOINT:
868235584b6SJoe Perches 		WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
869f81ef4a9SWill Deacon 	case ARM_ENTRY_SYNC_WATCHPOINT:
8706f26aa05SWill Deacon 		watchpoint_handler(addr, fsr, regs);
871f81ef4a9SWill Deacon 		break;
872f81ef4a9SWill Deacon 	default:
8737e202696SWill Deacon 		ret = 1; /* Unhandled fault. */
874f81ef4a9SWill Deacon 	}
875f81ef4a9SWill Deacon 
8767e202696SWill Deacon 	preempt_enable();
8777e202696SWill Deacon 
878f81ef4a9SWill Deacon 	return ret;
879f81ef4a9SWill Deacon }
880f81ef4a9SWill Deacon 
881f81ef4a9SWill Deacon /*
882f81ef4a9SWill Deacon  * One-time initialisation.
883f81ef4a9SWill Deacon  */
8840d352e3dSWill Deacon static cpumask_t debug_err_mask;
8850d352e3dSWill Deacon 
8860d352e3dSWill Deacon static int debug_reg_trap(struct pt_regs *regs, unsigned int instr)
8870d352e3dSWill Deacon {
8880d352e3dSWill Deacon 	int cpu = smp_processor_id();
8890d352e3dSWill Deacon 
8900d352e3dSWill Deacon 	pr_warning("Debug register access (0x%x) caused undefined instruction on CPU %d\n",
8910d352e3dSWill Deacon 		   instr, cpu);
8920d352e3dSWill Deacon 
8930d352e3dSWill Deacon 	/* Set the error flag for this CPU and skip the faulting instruction. */
8940d352e3dSWill Deacon 	cpumask_set_cpu(cpu, &debug_err_mask);
8950d352e3dSWill Deacon 	instruction_pointer(regs) += 4;
8960d352e3dSWill Deacon 	return 0;
8970d352e3dSWill Deacon }
8980d352e3dSWill Deacon 
8990d352e3dSWill Deacon static struct undef_hook debug_reg_hook = {
9000d352e3dSWill Deacon 	.instr_mask	= 0x0fe80f10,
9010d352e3dSWill Deacon 	.instr_val	= 0x0e000e10,
9020d352e3dSWill Deacon 	.fn		= debug_reg_trap,
9030d352e3dSWill Deacon };
9040d352e3dSWill Deacon 
9050d352e3dSWill Deacon static void reset_ctrl_regs(void *unused)
906f81ef4a9SWill Deacon {
907c512de95SWill Deacon 	int i, raw_num_brps, err = 0, cpu = smp_processor_id();
908c09bae70SWill Deacon 	u32 dbg_power;
909f81ef4a9SWill Deacon 
910ac88e071SWill Deacon 	/*
911ac88e071SWill Deacon 	 * v7 debug contains save and restore registers so that debug state
912ed19b739SWill Deacon 	 * can be maintained across low-power modes without leaving the debug
913ed19b739SWill Deacon 	 * logic powered up. It is IMPLEMENTATION DEFINED whether we can access
914ed19b739SWill Deacon 	 * the debug registers out of reset, so we must unlock the OS Lock
915ed19b739SWill Deacon 	 * Access Register to avoid taking undefined instruction exceptions
916ed19b739SWill Deacon 	 * later on.
917ac88e071SWill Deacon 	 */
918b5d5b8f9SWill Deacon 	switch (debug_arch) {
919a26bce12SWill Deacon 	case ARM_DEBUG_ARCH_V6:
920a26bce12SWill Deacon 	case ARM_DEBUG_ARCH_V6_1:
921a26bce12SWill Deacon 		/* ARMv6 cores just need to reset the registers. */
922a26bce12SWill Deacon 		goto reset_regs;
923b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_ECP14:
924ac88e071SWill Deacon 		/*
925c09bae70SWill Deacon 		 * Ensure sticky power-down is clear (i.e. debug logic is
926c09bae70SWill Deacon 		 * powered up).
927c09bae70SWill Deacon 		 */
928c09bae70SWill Deacon 		asm volatile("mrc p14, 0, %0, c1, c5, 4" : "=r" (dbg_power));
929b5d5b8f9SWill Deacon 		if ((dbg_power & 0x1) == 0)
930b5d5b8f9SWill Deacon 			err = -EPERM;
931b5d5b8f9SWill Deacon 		break;
932b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_1:
933b5d5b8f9SWill Deacon 		/*
934b5d5b8f9SWill Deacon 		 * Ensure the OS double lock is clear.
935b5d5b8f9SWill Deacon 		 */
936b5d5b8f9SWill Deacon 		asm volatile("mrc p14, 0, %0, c1, c3, 4" : "=r" (dbg_power));
937b5d5b8f9SWill Deacon 		if ((dbg_power & 0x1) == 1)
938b5d5b8f9SWill Deacon 			err = -EPERM;
939b5d5b8f9SWill Deacon 		break;
940b5d5b8f9SWill Deacon 	}
941b5d5b8f9SWill Deacon 
942b5d5b8f9SWill Deacon 	if (err) {
943c09bae70SWill Deacon 		pr_warning("CPU %d debug is powered down!\n", cpu);
9440d352e3dSWill Deacon 		cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
945c09bae70SWill Deacon 		return;
946c09bae70SWill Deacon 	}
947c09bae70SWill Deacon 
948c09bae70SWill Deacon 	/*
949ac88e071SWill Deacon 	 * Unconditionally clear the lock by writing a value
950ac88e071SWill Deacon 	 * other than 0xC5ACCE55 to the access register.
951ac88e071SWill Deacon 	 */
952ac88e071SWill Deacon 	asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0));
953ac88e071SWill Deacon 	isb();
954e89c0d70SWill Deacon 
955e89c0d70SWill Deacon 	/*
956e89c0d70SWill Deacon 	 * Clear any configured vector-catch events before
957e89c0d70SWill Deacon 	 * enabling monitor mode.
958e89c0d70SWill Deacon 	 */
959e89c0d70SWill Deacon 	asm volatile("mcr p14, 0, %0, c0, c7, 0" : : "r" (0));
960e89c0d70SWill Deacon 	isb();
961ac88e071SWill Deacon 
962a26bce12SWill Deacon reset_regs:
963f81ef4a9SWill Deacon 	if (enable_monitor_mode())
964f81ef4a9SWill Deacon 		return;
965f81ef4a9SWill Deacon 
9660017ff42SWill Deacon 	/* We must also reset any reserved registers. */
967c512de95SWill Deacon 	raw_num_brps = get_num_brp_resources();
968c512de95SWill Deacon 	for (i = 0; i < raw_num_brps; ++i) {
969f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BCR + i, 0UL);
970f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BVR + i, 0UL);
971f81ef4a9SWill Deacon 	}
972f81ef4a9SWill Deacon 
973f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
974f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WCR + i, 0UL);
975f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WVR + i, 0UL);
976f81ef4a9SWill Deacon 	}
977f81ef4a9SWill Deacon }
978f81ef4a9SWill Deacon 
9797d99331eSWill Deacon static int __cpuinit dbg_reset_notify(struct notifier_block *self,
9807d99331eSWill Deacon 				      unsigned long action, void *cpu)
9817d99331eSWill Deacon {
9827d99331eSWill Deacon 	if (action == CPU_ONLINE)
9837d99331eSWill Deacon 		smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
9840d352e3dSWill Deacon 
9857d99331eSWill Deacon 	return NOTIFY_OK;
9867d99331eSWill Deacon }
9877d99331eSWill Deacon 
9887d99331eSWill Deacon static struct notifier_block __cpuinitdata dbg_reset_nb = {
9897d99331eSWill Deacon 	.notifier_call = dbg_reset_notify,
9907d99331eSWill Deacon };
9917d99331eSWill Deacon 
992f81ef4a9SWill Deacon static int __init arch_hw_breakpoint_init(void)
993f81ef4a9SWill Deacon {
994f81ef4a9SWill Deacon 	u32 dscr;
995f81ef4a9SWill Deacon 
996f81ef4a9SWill Deacon 	debug_arch = get_debug_arch();
997f81ef4a9SWill Deacon 
99866e1cfe6SWill Deacon 	if (!debug_arch_supported()) {
999f81ef4a9SWill Deacon 		pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
10008fbf397cSWill Deacon 		return 0;
1001f81ef4a9SWill Deacon 	}
1002f81ef4a9SWill Deacon 
1003f81ef4a9SWill Deacon 	/* Determine how many BRPs/WRPs are available. */
1004f81ef4a9SWill Deacon 	core_num_brps = get_num_brps();
1005f81ef4a9SWill Deacon 	core_num_wrps = get_num_wrps();
1006f81ef4a9SWill Deacon 
10070d352e3dSWill Deacon 	/*
10080d352e3dSWill Deacon 	 * We need to tread carefully here because DBGSWENABLE may be
10090d352e3dSWill Deacon 	 * driven low on this core and there isn't an architected way to
10100d352e3dSWill Deacon 	 * determine that.
10110d352e3dSWill Deacon 	 */
10120d352e3dSWill Deacon 	register_undef_hook(&debug_reg_hook);
1013f81ef4a9SWill Deacon 
1014f81ef4a9SWill Deacon 	/*
1015f81ef4a9SWill Deacon 	 * Reset the breakpoint resources. We assume that a halting
1016f81ef4a9SWill Deacon 	 * debugger will leave the world in a nice state for us.
1017f81ef4a9SWill Deacon 	 */
10180d352e3dSWill Deacon 	on_each_cpu(reset_ctrl_regs, NULL, 1);
10190d352e3dSWill Deacon 	unregister_undef_hook(&debug_reg_hook);
10200d352e3dSWill Deacon 	if (!cpumask_empty(&debug_err_mask)) {
1021c09bae70SWill Deacon 		core_num_brps = 0;
1022c09bae70SWill Deacon 		core_num_wrps = 0;
1023c09bae70SWill Deacon 		return 0;
1024c09bae70SWill Deacon 	}
1025ac88e071SWill Deacon 
10260d352e3dSWill Deacon 	pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n",
10270d352e3dSWill Deacon 		core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " :
10280d352e3dSWill Deacon 		"", core_num_wrps);
10290d352e3dSWill Deacon 
1030ed19b739SWill Deacon 	ARM_DBG_READ(c1, 0, dscr);
1031ed19b739SWill Deacon 	if (dscr & ARM_DSCR_HDBGEN) {
1032ed19b739SWill Deacon 		max_watchpoint_len = 4;
10337d85d61fSStephen Boyd 		pr_warning("halting debug mode enabled. Assuming maximum watchpoint size of %u bytes.\n",
10347d85d61fSStephen Boyd 			   max_watchpoint_len);
1035ed19b739SWill Deacon 	} else {
1036ac88e071SWill Deacon 		/* Work out the maximum supported watchpoint length. */
1037ac88e071SWill Deacon 		max_watchpoint_len = get_max_wp_len();
1038ac88e071SWill Deacon 		pr_info("maximum watchpoint size is %u bytes.\n",
1039ac88e071SWill Deacon 				max_watchpoint_len);
1040f81ef4a9SWill Deacon 	}
1041f81ef4a9SWill Deacon 
1042f81ef4a9SWill Deacon 	/* Register debug fault handler. */
1043f7b8156dSCatalin Marinas 	hook_fault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
1044f7b8156dSCatalin Marinas 			TRAP_HWBKPT, "watchpoint debug exception");
1045f7b8156dSCatalin Marinas 	hook_ifault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
1046f7b8156dSCatalin Marinas 			TRAP_HWBKPT, "breakpoint debug exception");
1047f81ef4a9SWill Deacon 
10487d99331eSWill Deacon 	/* Register hotplug notifier. */
10497d99331eSWill Deacon 	register_cpu_notifier(&dbg_reset_nb);
10508fbf397cSWill Deacon 	return 0;
1051f81ef4a9SWill Deacon }
1052f81ef4a9SWill Deacon arch_initcall(arch_hw_breakpoint_init);
1053f81ef4a9SWill Deacon 
1054f81ef4a9SWill Deacon void hw_breakpoint_pmu_read(struct perf_event *bp)
1055f81ef4a9SWill Deacon {
1056f81ef4a9SWill Deacon }
1057f81ef4a9SWill Deacon 
1058f81ef4a9SWill Deacon /*
1059f81ef4a9SWill Deacon  * Dummy function to register with die_notifier.
1060f81ef4a9SWill Deacon  */
1061f81ef4a9SWill Deacon int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
1062f81ef4a9SWill Deacon 					unsigned long val, void *data)
1063f81ef4a9SWill Deacon {
1064f81ef4a9SWill Deacon 	return NOTIFY_DONE;
1065f81ef4a9SWill Deacon }
1066