| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  * This program is free software; you can redistribute it and/or modify | 
 | 3 |  * it under the terms of the GNU General Public License version 2 as | 
 | 4 |  * published by the Free Software Foundation. | 
 | 5 |  * | 
 | 6 |  * This program is distributed in the hope that it will be useful, | 
 | 7 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 8 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 9 |  * GNU General Public License for more details. | 
 | 10 |  * | 
 | 11 |  * You should have received a copy of the GNU General Public License | 
 | 12 |  * along with this program; if not, write to the Free Software | 
 | 13 |  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 
 | 14 |  * | 
 | 15 |  * Copyright (C) 2009, 2010 ARM Limited | 
 | 16 |  * | 
 | 17 |  * Author: Will Deacon <will.deacon@arm.com> | 
 | 18 |  */ | 
 | 19 |  | 
 | 20 | /* | 
 | 21 |  * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility, | 
 | 22 |  * using the CPU's debug registers. | 
 | 23 |  */ | 
 | 24 | #define pr_fmt(fmt) "hw-breakpoint: " fmt | 
 | 25 |  | 
 | 26 | #include <linux/errno.h> | 
| Will Deacon | 7e20269 | 2010-11-28 14:57:24 +0000 | [diff] [blame] | 27 | #include <linux/hardirq.h> | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 28 | #include <linux/perf_event.h> | 
 | 29 | #include <linux/hw_breakpoint.h> | 
 | 30 | #include <linux/smp.h> | 
 | 31 |  | 
 | 32 | #include <asm/cacheflush.h> | 
 | 33 | #include <asm/cputype.h> | 
 | 34 | #include <asm/current.h> | 
 | 35 | #include <asm/hw_breakpoint.h> | 
 | 36 | #include <asm/kdebug.h> | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 37 | #include <asm/traps.h> | 
 | 38 |  | 
 | 39 | /* Breakpoint currently in use for each BRP. */ | 
 | 40 | static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]); | 
 | 41 |  | 
 | 42 | /* Watchpoint currently in use for each WRP. */ | 
 | 43 | static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]); | 
 | 44 |  | 
 | 45 | /* Number of BRP/WRP registers on this CPU. */ | 
 | 46 | static int core_num_brps; | 
 | 47 | static int core_num_wrps; | 
 | 48 |  | 
 | 49 | /* Debug architecture version. */ | 
 | 50 | static u8 debug_arch; | 
 | 51 |  | 
 | 52 | /* Maximum supported watchpoint length. */ | 
 | 53 | static u8 max_watchpoint_len; | 
 | 54 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 55 | #define READ_WB_REG_CASE(OP2, M, VAL)		\ | 
 | 56 | 	case ((OP2 << 4) + M):			\ | 
 | 57 | 		ARM_DBG_READ(c ## M, OP2, VAL); \ | 
 | 58 | 		break | 
 | 59 |  | 
 | 60 | #define WRITE_WB_REG_CASE(OP2, M, VAL)		\ | 
 | 61 | 	case ((OP2 << 4) + M):			\ | 
 | 62 | 		ARM_DBG_WRITE(c ## M, OP2, VAL);\ | 
 | 63 | 		break | 
 | 64 |  | 
 | 65 | #define GEN_READ_WB_REG_CASES(OP2, VAL)		\ | 
 | 66 | 	READ_WB_REG_CASE(OP2, 0, VAL);		\ | 
 | 67 | 	READ_WB_REG_CASE(OP2, 1, VAL);		\ | 
 | 68 | 	READ_WB_REG_CASE(OP2, 2, VAL);		\ | 
 | 69 | 	READ_WB_REG_CASE(OP2, 3, VAL);		\ | 
 | 70 | 	READ_WB_REG_CASE(OP2, 4, VAL);		\ | 
 | 71 | 	READ_WB_REG_CASE(OP2, 5, VAL);		\ | 
 | 72 | 	READ_WB_REG_CASE(OP2, 6, VAL);		\ | 
 | 73 | 	READ_WB_REG_CASE(OP2, 7, VAL);		\ | 
 | 74 | 	READ_WB_REG_CASE(OP2, 8, VAL);		\ | 
 | 75 | 	READ_WB_REG_CASE(OP2, 9, VAL);		\ | 
 | 76 | 	READ_WB_REG_CASE(OP2, 10, VAL);		\ | 
 | 77 | 	READ_WB_REG_CASE(OP2, 11, VAL);		\ | 
 | 78 | 	READ_WB_REG_CASE(OP2, 12, VAL);		\ | 
 | 79 | 	READ_WB_REG_CASE(OP2, 13, VAL);		\ | 
 | 80 | 	READ_WB_REG_CASE(OP2, 14, VAL);		\ | 
 | 81 | 	READ_WB_REG_CASE(OP2, 15, VAL) | 
 | 82 |  | 
 | 83 | #define GEN_WRITE_WB_REG_CASES(OP2, VAL)	\ | 
 | 84 | 	WRITE_WB_REG_CASE(OP2, 0, VAL);		\ | 
 | 85 | 	WRITE_WB_REG_CASE(OP2, 1, VAL);		\ | 
 | 86 | 	WRITE_WB_REG_CASE(OP2, 2, VAL);		\ | 
 | 87 | 	WRITE_WB_REG_CASE(OP2, 3, VAL);		\ | 
 | 88 | 	WRITE_WB_REG_CASE(OP2, 4, VAL);		\ | 
 | 89 | 	WRITE_WB_REG_CASE(OP2, 5, VAL);		\ | 
 | 90 | 	WRITE_WB_REG_CASE(OP2, 6, VAL);		\ | 
 | 91 | 	WRITE_WB_REG_CASE(OP2, 7, VAL);		\ | 
 | 92 | 	WRITE_WB_REG_CASE(OP2, 8, VAL);		\ | 
 | 93 | 	WRITE_WB_REG_CASE(OP2, 9, VAL);		\ | 
 | 94 | 	WRITE_WB_REG_CASE(OP2, 10, VAL);	\ | 
 | 95 | 	WRITE_WB_REG_CASE(OP2, 11, VAL);	\ | 
 | 96 | 	WRITE_WB_REG_CASE(OP2, 12, VAL);	\ | 
 | 97 | 	WRITE_WB_REG_CASE(OP2, 13, VAL);	\ | 
 | 98 | 	WRITE_WB_REG_CASE(OP2, 14, VAL);	\ | 
 | 99 | 	WRITE_WB_REG_CASE(OP2, 15, VAL) | 
 | 100 |  | 
 | 101 | static u32 read_wb_reg(int n) | 
 | 102 | { | 
 | 103 | 	u32 val = 0; | 
 | 104 |  | 
 | 105 | 	switch (n) { | 
 | 106 | 	GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val); | 
 | 107 | 	GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val); | 
 | 108 | 	GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val); | 
 | 109 | 	GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val); | 
 | 110 | 	default: | 
 | 111 | 		pr_warning("attempt to read from unknown breakpoint " | 
 | 112 | 				"register %d\n", n); | 
 | 113 | 	} | 
 | 114 |  | 
 | 115 | 	return val; | 
 | 116 | } | 
 | 117 |  | 
 | 118 | static void write_wb_reg(int n, u32 val) | 
 | 119 | { | 
 | 120 | 	switch (n) { | 
 | 121 | 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val); | 
 | 122 | 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val); | 
 | 123 | 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val); | 
 | 124 | 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val); | 
 | 125 | 	default: | 
 | 126 | 		pr_warning("attempt to write to unknown breakpoint " | 
 | 127 | 				"register %d\n", n); | 
 | 128 | 	} | 
 | 129 | 	isb(); | 
 | 130 | } | 
 | 131 |  | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 132 | /* Determine debug architecture. */ | 
 | 133 | static u8 get_debug_arch(void) | 
 | 134 | { | 
 | 135 | 	u32 didr; | 
 | 136 |  | 
 | 137 | 	/* Do we implement the extended CPUID interface? */ | 
| Will Deacon | d124433 | 2011-08-04 14:46:23 +0100 | [diff] [blame] | 138 | 	if (((read_cpuid_id() >> 16) & 0xf) != 0xf) { | 
 | 139 | 		pr_warning("CPUID feature registers not supported. " | 
 | 140 | 			   "Assuming v6 debug is present.\n"); | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 141 | 		return ARM_DEBUG_ARCH_V6; | 
| Will Deacon | d124433 | 2011-08-04 14:46:23 +0100 | [diff] [blame] | 142 | 	} | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 143 |  | 
 | 144 | 	ARM_DBG_READ(c0, 0, didr); | 
 | 145 | 	return (didr >> 16) & 0xf; | 
 | 146 | } | 
 | 147 |  | 
 | 148 | u8 arch_get_debug_arch(void) | 
 | 149 | { | 
 | 150 | 	return debug_arch; | 
 | 151 | } | 
 | 152 |  | 
| Will Deacon | 66e1cfe | 2011-02-11 16:01:42 +0100 | [diff] [blame] | 153 | static int debug_arch_supported(void) | 
 | 154 | { | 
 | 155 | 	u8 arch = get_debug_arch(); | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 156 |  | 
 | 157 | 	/* We don't support the memory-mapped interface. */ | 
 | 158 | 	return (arch >= ARM_DEBUG_ARCH_V6 && arch <= ARM_DEBUG_ARCH_V7_ECP14) || | 
 | 159 | 		arch >= ARM_DEBUG_ARCH_V7_1; | 
| Will Deacon | 66e1cfe | 2011-02-11 16:01:42 +0100 | [diff] [blame] | 160 | } | 
 | 161 |  | 
| Will Deacon | c512de9 | 2011-08-02 13:01:17 +0100 | [diff] [blame] | 162 | /* Determine number of WRP registers available. */ | 
 | 163 | static int get_num_wrp_resources(void) | 
 | 164 | { | 
 | 165 | 	u32 didr; | 
 | 166 | 	ARM_DBG_READ(c0, 0, didr); | 
 | 167 | 	return ((didr >> 28) & 0xf) + 1; | 
 | 168 | } | 
 | 169 |  | 
 | 170 | /* Determine number of BRP registers available. */ | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 171 | static int get_num_brp_resources(void) | 
 | 172 | { | 
 | 173 | 	u32 didr; | 
 | 174 | 	ARM_DBG_READ(c0, 0, didr); | 
 | 175 | 	return ((didr >> 24) & 0xf) + 1; | 
 | 176 | } | 
 | 177 |  | 
 | 178 | /* Does this core support mismatch breakpoints? */ | 
 | 179 | static int core_has_mismatch_brps(void) | 
 | 180 | { | 
 | 181 | 	return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 && | 
 | 182 | 		get_num_brp_resources() > 1); | 
 | 183 | } | 
 | 184 |  | 
 | 185 | /* Determine number of usable WRPs available. */ | 
 | 186 | static int get_num_wrps(void) | 
 | 187 | { | 
 | 188 | 	/* | 
| Will Deacon | c512de9 | 2011-08-02 13:01:17 +0100 | [diff] [blame] | 189 | 	 * On debug architectures prior to 7.1, when a watchpoint fires, the | 
 | 190 | 	 * only way to work out which watchpoint it was is by disassembling | 
 | 191 | 	 * the faulting instruction and working out the address of the memory | 
 | 192 | 	 * access. | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 193 | 	 * | 
 | 194 | 	 * Furthermore, we can only do this if the watchpoint was precise | 
 | 195 | 	 * since imprecise watchpoints prevent us from calculating register | 
 | 196 | 	 * based addresses. | 
 | 197 | 	 * | 
 | 198 | 	 * Providing we have more than 1 breakpoint register, we only report | 
 | 199 | 	 * a single watchpoint register for the time being. This way, we always | 
 | 200 | 	 * know which watchpoint fired. In the future we can either add a | 
 | 201 | 	 * disassembler and address generation emulator, or we can insert a | 
 | 202 | 	 * check to see if the DFAR is set on watchpoint exception entry | 
 | 203 | 	 * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows | 
 | 204 | 	 * that it is set on some implementations]. | 
 | 205 | 	 */ | 
| Will Deacon | c512de9 | 2011-08-02 13:01:17 +0100 | [diff] [blame] | 206 | 	if (get_debug_arch() < ARM_DEBUG_ARCH_V7_1) | 
 | 207 | 		return 1; | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 208 |  | 
| Will Deacon | c512de9 | 2011-08-02 13:01:17 +0100 | [diff] [blame] | 209 | 	return get_num_wrp_resources(); | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 210 | } | 
 | 211 |  | 
 | 212 | /* Determine number of usable BRPs available. */ | 
 | 213 | static int get_num_brps(void) | 
 | 214 | { | 
 | 215 | 	int brps = get_num_brp_resources(); | 
| Will Deacon | c512de9 | 2011-08-02 13:01:17 +0100 | [diff] [blame] | 216 | 	return core_has_mismatch_brps() ? brps - 1 : brps; | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 217 | } | 
 | 218 |  | 
| Stephen Boyd | 5607d22 | 2012-09-18 20:23:13 -0700 | [diff] [blame] | 219 | /* Determine if halting mode is enabled */ | 
 | 220 | static int halting_mode_enabled(void) | 
 | 221 | { | 
 | 222 | 	u32 dscr; | 
 | 223 |  | 
 | 224 | 	ARM_DBG_READ(c1, 0, dscr); | 
 | 225 |  | 
 | 226 | 	if (WARN_ONCE(dscr & ARM_DSCR_HDBGEN, | 
 | 227 | 		      "halting debug mode enabled. " | 
 | 228 | 		      "Unable to access hardware resources.\n")) | 
 | 229 | 		return -EPERM; | 
 | 230 | 	return 0; | 
 | 231 | } | 
 | 232 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 233 | /* | 
 | 234 |  * In order to access the breakpoint/watchpoint control registers, | 
 | 235 |  * we must be running in debug monitor mode. Unfortunately, we can | 
 | 236 |  * be put into halting debug mode at any time by an external debugger | 
 | 237 |  * but there is nothing we can do to prevent that. | 
 | 238 |  */ | 
 | 239 | static int enable_monitor_mode(void) | 
 | 240 | { | 
 | 241 | 	u32 dscr; | 
| Stephen Boyd | 5607d22 | 2012-09-18 20:23:13 -0700 | [diff] [blame] | 242 | 	int ret; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 243 |  | 
 | 244 | 	ARM_DBG_READ(c1, 0, dscr); | 
 | 245 |  | 
 | 246 | 	/* Ensure that halting mode is disabled. */ | 
| Stephen Boyd | 5607d22 | 2012-09-18 20:23:13 -0700 | [diff] [blame] | 247 | 	ret = halting_mode_enabled(); | 
 | 248 | 	if (ret) | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 249 | 		goto out; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 250 |  | 
| Will Deacon | 8fbf397 | 2010-12-01 17:37:45 +0000 | [diff] [blame] | 251 | 	/* If monitor mode is already enabled, just return. */ | 
 | 252 | 	if (dscr & ARM_DSCR_MDBGEN) | 
 | 253 | 		goto out; | 
 | 254 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 255 | 	/* Write to the corresponding DSCR. */ | 
| Will Deacon | 8fbf397 | 2010-12-01 17:37:45 +0000 | [diff] [blame] | 256 | 	switch (get_debug_arch()) { | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 257 | 	case ARM_DEBUG_ARCH_V6: | 
 | 258 | 	case ARM_DEBUG_ARCH_V6_1: | 
 | 259 | 		ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN)); | 
 | 260 | 		break; | 
 | 261 | 	case ARM_DEBUG_ARCH_V7_ECP14: | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 262 | 	case ARM_DEBUG_ARCH_V7_1: | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 263 | 		ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN)); | 
 | 264 | 		break; | 
 | 265 | 	default: | 
 | 266 | 		ret = -ENODEV; | 
 | 267 | 		goto out; | 
 | 268 | 	} | 
 | 269 |  | 
 | 270 | 	/* Check that the write made it through. */ | 
 | 271 | 	ARM_DBG_READ(c1, 0, dscr); | 
| Will Deacon | 8fbf397 | 2010-12-01 17:37:45 +0000 | [diff] [blame] | 272 | 	if (!(dscr & ARM_DSCR_MDBGEN)) | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 273 | 		ret = -EPERM; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 274 |  | 
 | 275 | out: | 
 | 276 | 	return ret; | 
 | 277 | } | 
 | 278 |  | 
| Will Deacon | 8fbf397 | 2010-12-01 17:37:45 +0000 | [diff] [blame] | 279 | int hw_breakpoint_slots(int type) | 
 | 280 | { | 
| Will Deacon | 66e1cfe | 2011-02-11 16:01:42 +0100 | [diff] [blame] | 281 | 	if (!debug_arch_supported()) | 
 | 282 | 		return 0; | 
 | 283 |  | 
| Will Deacon | 8fbf397 | 2010-12-01 17:37:45 +0000 | [diff] [blame] | 284 | 	/* | 
 | 285 | 	 * We can be called early, so don't rely on | 
 | 286 | 	 * our static variables being initialised. | 
 | 287 | 	 */ | 
 | 288 | 	switch (type) { | 
 | 289 | 	case TYPE_INST: | 
 | 290 | 		return get_num_brps(); | 
 | 291 | 	case TYPE_DATA: | 
 | 292 | 		return get_num_wrps(); | 
 | 293 | 	default: | 
 | 294 | 		pr_warning("unknown slot type: %d\n", type); | 
 | 295 | 		return 0; | 
 | 296 | 	} | 
 | 297 | } | 
 | 298 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 299 | /* | 
 | 300 |  * Check if 8-bit byte-address select is available. | 
 | 301 |  * This clobbers WRP 0. | 
 | 302 |  */ | 
 | 303 | static u8 get_max_wp_len(void) | 
 | 304 | { | 
 | 305 | 	u32 ctrl_reg; | 
 | 306 | 	struct arch_hw_breakpoint_ctrl ctrl; | 
 | 307 | 	u8 size = 4; | 
 | 308 |  | 
 | 309 | 	if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14) | 
 | 310 | 		goto out; | 
 | 311 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 312 | 	memset(&ctrl, 0, sizeof(ctrl)); | 
 | 313 | 	ctrl.len = ARM_BREAKPOINT_LEN_8; | 
 | 314 | 	ctrl_reg = encode_ctrl_reg(ctrl); | 
 | 315 |  | 
 | 316 | 	write_wb_reg(ARM_BASE_WVR, 0); | 
 | 317 | 	write_wb_reg(ARM_BASE_WCR, ctrl_reg); | 
 | 318 | 	if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg) | 
 | 319 | 		size = 8; | 
 | 320 |  | 
 | 321 | out: | 
 | 322 | 	return size; | 
 | 323 | } | 
 | 324 |  | 
 | 325 | u8 arch_get_max_wp_len(void) | 
 | 326 | { | 
 | 327 | 	return max_watchpoint_len; | 
 | 328 | } | 
 | 329 |  | 
 | 330 | /* | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 331 |  * Install a perf counter breakpoint. | 
 | 332 |  */ | 
 | 333 | int arch_install_hw_breakpoint(struct perf_event *bp) | 
 | 334 | { | 
 | 335 | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
 | 336 | 	struct perf_event **slot, **slots; | 
 | 337 | 	int i, max_slots, ctrl_base, val_base, ret = 0; | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 338 | 	u32 addr, ctrl; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 339 |  | 
 | 340 | 	/* Ensure that we are in monitor mode and halting mode is disabled. */ | 
 | 341 | 	ret = enable_monitor_mode(); | 
 | 342 | 	if (ret) | 
 | 343 | 		goto out; | 
 | 344 |  | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 345 | 	addr = info->address; | 
 | 346 | 	ctrl = encode_ctrl_reg(info->ctrl) | 0x1; | 
 | 347 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 348 | 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) { | 
 | 349 | 		/* Breakpoint */ | 
 | 350 | 		ctrl_base = ARM_BASE_BCR; | 
 | 351 | 		val_base = ARM_BASE_BVR; | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 352 | 		slots = (struct perf_event **)__get_cpu_var(bp_on_reg); | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 353 | 		max_slots = core_num_brps; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 354 | 	} else { | 
 | 355 | 		/* Watchpoint */ | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 356 | 		ctrl_base = ARM_BASE_WCR; | 
 | 357 | 		val_base = ARM_BASE_WVR; | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 358 | 		slots = (struct perf_event **)__get_cpu_var(wp_on_reg); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 359 | 		max_slots = core_num_wrps; | 
 | 360 | 	} | 
 | 361 |  | 
 | 362 | 	for (i = 0; i < max_slots; ++i) { | 
 | 363 | 		slot = &slots[i]; | 
 | 364 |  | 
 | 365 | 		if (!*slot) { | 
 | 366 | 			*slot = bp; | 
 | 367 | 			break; | 
 | 368 | 		} | 
 | 369 | 	} | 
 | 370 |  | 
| Stephen Boyd | 7d85d61 | 2011-03-10 05:15:00 +0100 | [diff] [blame] | 371 | 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n")) { | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 372 | 		ret = -EBUSY; | 
 | 373 | 		goto out; | 
 | 374 | 	} | 
 | 375 |  | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 376 | 	/* Override the breakpoint data with the step data. */ | 
 | 377 | 	if (info->step_ctrl.enabled) { | 
 | 378 | 		addr = info->trigger & ~0x3; | 
 | 379 | 		ctrl = encode_ctrl_reg(info->step_ctrl); | 
 | 380 | 		if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE) { | 
 | 381 | 			i = 0; | 
 | 382 | 			ctrl_base = ARM_BASE_BCR + core_num_brps; | 
 | 383 | 			val_base = ARM_BASE_BVR + core_num_brps; | 
 | 384 | 		} | 
 | 385 | 	} | 
 | 386 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 387 | 	/* Setup the address register. */ | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 388 | 	write_wb_reg(val_base + i, addr); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 389 |  | 
 | 390 | 	/* Setup the control register. */ | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 391 | 	write_wb_reg(ctrl_base + i, ctrl); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 392 |  | 
 | 393 | out: | 
 | 394 | 	return ret; | 
 | 395 | } | 
 | 396 |  | 
 | 397 | void arch_uninstall_hw_breakpoint(struct perf_event *bp) | 
 | 398 | { | 
 | 399 | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
 | 400 | 	struct perf_event **slot, **slots; | 
 | 401 | 	int i, max_slots, base; | 
 | 402 |  | 
 | 403 | 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) { | 
 | 404 | 		/* Breakpoint */ | 
 | 405 | 		base = ARM_BASE_BCR; | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 406 | 		slots = (struct perf_event **)__get_cpu_var(bp_on_reg); | 
| Will Deacon | 0017ff4 | 2010-11-28 15:09:36 +0000 | [diff] [blame] | 407 | 		max_slots = core_num_brps; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 408 | 	} else { | 
 | 409 | 		/* Watchpoint */ | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 410 | 		base = ARM_BASE_WCR; | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 411 | 		slots = (struct perf_event **)__get_cpu_var(wp_on_reg); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 412 | 		max_slots = core_num_wrps; | 
 | 413 | 	} | 
 | 414 |  | 
 | 415 | 	/* Remove the breakpoint. */ | 
 | 416 | 	for (i = 0; i < max_slots; ++i) { | 
 | 417 | 		slot = &slots[i]; | 
 | 418 |  | 
 | 419 | 		if (*slot == bp) { | 
 | 420 | 			*slot = NULL; | 
 | 421 | 			break; | 
 | 422 | 		} | 
 | 423 | 	} | 
 | 424 |  | 
| Stephen Boyd | 7d85d61 | 2011-03-10 05:15:00 +0100 | [diff] [blame] | 425 | 	if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n")) | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 426 | 		return; | 
 | 427 |  | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 428 | 	/* Ensure that we disable the mismatch breakpoint. */ | 
 | 429 | 	if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE && | 
 | 430 | 	    info->step_ctrl.enabled) { | 
 | 431 | 		i = 0; | 
 | 432 | 		base = ARM_BASE_BCR + core_num_brps; | 
 | 433 | 	} | 
 | 434 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 435 | 	/* Reset the control register. */ | 
 | 436 | 	write_wb_reg(base + i, 0); | 
 | 437 | } | 
 | 438 |  | 
 | 439 | static int get_hbp_len(u8 hbp_len) | 
 | 440 | { | 
 | 441 | 	unsigned int len_in_bytes = 0; | 
 | 442 |  | 
 | 443 | 	switch (hbp_len) { | 
 | 444 | 	case ARM_BREAKPOINT_LEN_1: | 
 | 445 | 		len_in_bytes = 1; | 
 | 446 | 		break; | 
 | 447 | 	case ARM_BREAKPOINT_LEN_2: | 
 | 448 | 		len_in_bytes = 2; | 
 | 449 | 		break; | 
 | 450 | 	case ARM_BREAKPOINT_LEN_4: | 
 | 451 | 		len_in_bytes = 4; | 
 | 452 | 		break; | 
 | 453 | 	case ARM_BREAKPOINT_LEN_8: | 
 | 454 | 		len_in_bytes = 8; | 
 | 455 | 		break; | 
 | 456 | 	} | 
 | 457 |  | 
 | 458 | 	return len_in_bytes; | 
 | 459 | } | 
 | 460 |  | 
 | 461 | /* | 
 | 462 |  * Check whether bp virtual address is in kernel space. | 
 | 463 |  */ | 
 | 464 | int arch_check_bp_in_kernelspace(struct perf_event *bp) | 
 | 465 | { | 
 | 466 | 	unsigned int len; | 
 | 467 | 	unsigned long va; | 
 | 468 | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
 | 469 |  | 
 | 470 | 	va = info->address; | 
 | 471 | 	len = get_hbp_len(info->ctrl.len); | 
 | 472 |  | 
 | 473 | 	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); | 
 | 474 | } | 
 | 475 |  | 
 | 476 | /* | 
 | 477 |  * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl. | 
 | 478 |  * Hopefully this will disappear when ptrace can bypass the conversion | 
 | 479 |  * to generic breakpoint descriptions. | 
 | 480 |  */ | 
 | 481 | int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl, | 
 | 482 | 			   int *gen_len, int *gen_type) | 
 | 483 | { | 
 | 484 | 	/* Type */ | 
 | 485 | 	switch (ctrl.type) { | 
 | 486 | 	case ARM_BREAKPOINT_EXECUTE: | 
 | 487 | 		*gen_type = HW_BREAKPOINT_X; | 
 | 488 | 		break; | 
 | 489 | 	case ARM_BREAKPOINT_LOAD: | 
 | 490 | 		*gen_type = HW_BREAKPOINT_R; | 
 | 491 | 		break; | 
 | 492 | 	case ARM_BREAKPOINT_STORE: | 
 | 493 | 		*gen_type = HW_BREAKPOINT_W; | 
 | 494 | 		break; | 
 | 495 | 	case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE: | 
 | 496 | 		*gen_type = HW_BREAKPOINT_RW; | 
 | 497 | 		break; | 
 | 498 | 	default: | 
 | 499 | 		return -EINVAL; | 
 | 500 | 	} | 
 | 501 |  | 
 | 502 | 	/* Len */ | 
 | 503 | 	switch (ctrl.len) { | 
 | 504 | 	case ARM_BREAKPOINT_LEN_1: | 
 | 505 | 		*gen_len = HW_BREAKPOINT_LEN_1; | 
 | 506 | 		break; | 
 | 507 | 	case ARM_BREAKPOINT_LEN_2: | 
 | 508 | 		*gen_len = HW_BREAKPOINT_LEN_2; | 
 | 509 | 		break; | 
 | 510 | 	case ARM_BREAKPOINT_LEN_4: | 
 | 511 | 		*gen_len = HW_BREAKPOINT_LEN_4; | 
 | 512 | 		break; | 
 | 513 | 	case ARM_BREAKPOINT_LEN_8: | 
 | 514 | 		*gen_len = HW_BREAKPOINT_LEN_8; | 
 | 515 | 		break; | 
 | 516 | 	default: | 
 | 517 | 		return -EINVAL; | 
 | 518 | 	} | 
 | 519 |  | 
 | 520 | 	return 0; | 
 | 521 | } | 
 | 522 |  | 
 | 523 | /* | 
 | 524 |  * Construct an arch_hw_breakpoint from a perf_event. | 
 | 525 |  */ | 
 | 526 | static int arch_build_bp_info(struct perf_event *bp) | 
 | 527 | { | 
 | 528 | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
 | 529 |  | 
 | 530 | 	/* Type */ | 
 | 531 | 	switch (bp->attr.bp_type) { | 
 | 532 | 	case HW_BREAKPOINT_X: | 
 | 533 | 		info->ctrl.type = ARM_BREAKPOINT_EXECUTE; | 
 | 534 | 		break; | 
 | 535 | 	case HW_BREAKPOINT_R: | 
 | 536 | 		info->ctrl.type = ARM_BREAKPOINT_LOAD; | 
 | 537 | 		break; | 
 | 538 | 	case HW_BREAKPOINT_W: | 
 | 539 | 		info->ctrl.type = ARM_BREAKPOINT_STORE; | 
 | 540 | 		break; | 
 | 541 | 	case HW_BREAKPOINT_RW: | 
 | 542 | 		info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE; | 
 | 543 | 		break; | 
 | 544 | 	default: | 
 | 545 | 		return -EINVAL; | 
 | 546 | 	} | 
 | 547 |  | 
 | 548 | 	/* Len */ | 
 | 549 | 	switch (bp->attr.bp_len) { | 
 | 550 | 	case HW_BREAKPOINT_LEN_1: | 
 | 551 | 		info->ctrl.len = ARM_BREAKPOINT_LEN_1; | 
 | 552 | 		break; | 
 | 553 | 	case HW_BREAKPOINT_LEN_2: | 
 | 554 | 		info->ctrl.len = ARM_BREAKPOINT_LEN_2; | 
 | 555 | 		break; | 
 | 556 | 	case HW_BREAKPOINT_LEN_4: | 
 | 557 | 		info->ctrl.len = ARM_BREAKPOINT_LEN_4; | 
 | 558 | 		break; | 
 | 559 | 	case HW_BREAKPOINT_LEN_8: | 
 | 560 | 		info->ctrl.len = ARM_BREAKPOINT_LEN_8; | 
 | 561 | 		if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE) | 
 | 562 | 			&& max_watchpoint_len >= 8) | 
 | 563 | 			break; | 
 | 564 | 	default: | 
 | 565 | 		return -EINVAL; | 
 | 566 | 	} | 
 | 567 |  | 
| Will Deacon | 6ee33c27 | 2010-11-25 12:01:54 +0000 | [diff] [blame] | 568 | 	/* | 
 | 569 | 	 * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes. | 
 | 570 | 	 * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported | 
 | 571 | 	 * by the hardware and must be aligned to the appropriate number of | 
 | 572 | 	 * bytes. | 
 | 573 | 	 */ | 
 | 574 | 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE && | 
 | 575 | 	    info->ctrl.len != ARM_BREAKPOINT_LEN_2 && | 
 | 576 | 	    info->ctrl.len != ARM_BREAKPOINT_LEN_4) | 
 | 577 | 		return -EINVAL; | 
 | 578 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 579 | 	/* Address */ | 
 | 580 | 	info->address = bp->attr.bp_addr; | 
 | 581 |  | 
 | 582 | 	/* Privilege */ | 
 | 583 | 	info->ctrl.privilege = ARM_BREAKPOINT_USER; | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 584 | 	if (arch_check_bp_in_kernelspace(bp)) | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 585 | 		info->ctrl.privilege |= ARM_BREAKPOINT_PRIV; | 
 | 586 |  | 
 | 587 | 	/* Enabled? */ | 
 | 588 | 	info->ctrl.enabled = !bp->attr.disabled; | 
 | 589 |  | 
 | 590 | 	/* Mismatch */ | 
 | 591 | 	info->ctrl.mismatch = 0; | 
 | 592 |  | 
 | 593 | 	return 0; | 
 | 594 | } | 
 | 595 |  | 
 | 596 | /* | 
 | 597 |  * Validate the arch-specific HW Breakpoint register settings. | 
 | 598 |  */ | 
 | 599 | int arch_validate_hwbkpt_settings(struct perf_event *bp) | 
 | 600 | { | 
 | 601 | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
 | 602 | 	int ret = 0; | 
| Will Deacon | 6ee33c27 | 2010-11-25 12:01:54 +0000 | [diff] [blame] | 603 | 	u32 offset, alignment_mask = 0x3; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 604 |  | 
 | 605 | 	/* Build the arch_hw_breakpoint. */ | 
 | 606 | 	ret = arch_build_bp_info(bp); | 
 | 607 | 	if (ret) | 
 | 608 | 		goto out; | 
 | 609 |  | 
 | 610 | 	/* Check address alignment. */ | 
 | 611 | 	if (info->ctrl.len == ARM_BREAKPOINT_LEN_8) | 
 | 612 | 		alignment_mask = 0x7; | 
| Will Deacon | 6ee33c27 | 2010-11-25 12:01:54 +0000 | [diff] [blame] | 613 | 	offset = info->address & alignment_mask; | 
 | 614 | 	switch (offset) { | 
 | 615 | 	case 0: | 
 | 616 | 		/* Aligned */ | 
 | 617 | 		break; | 
 | 618 | 	case 1: | 
 | 619 | 		/* Allow single byte watchpoint. */ | 
 | 620 | 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_1) | 
 | 621 | 			break; | 
 | 622 | 	case 2: | 
 | 623 | 		/* Allow halfword watchpoints and breakpoints. */ | 
 | 624 | 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_2) | 
 | 625 | 			break; | 
 | 626 | 	default: | 
 | 627 | 		ret = -EINVAL; | 
 | 628 | 		goto out; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 629 | 	} | 
 | 630 |  | 
| Will Deacon | 6ee33c27 | 2010-11-25 12:01:54 +0000 | [diff] [blame] | 631 | 	info->address &= ~alignment_mask; | 
 | 632 | 	info->ctrl.len <<= offset; | 
 | 633 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 634 | 	/* | 
 | 635 | 	 * Currently we rely on an overflow handler to take | 
 | 636 | 	 * care of single-stepping the breakpoint when it fires. | 
 | 637 | 	 * In the case of userspace breakpoints on a core with V7 debug, | 
| Will Deacon | 3ce70b2 | 2010-12-01 17:05:24 +0000 | [diff] [blame] | 638 | 	 * we can use the mismatch feature as a poor-man's hardware | 
 | 639 | 	 * single-step, but this only works for per-task breakpoints. | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 640 | 	 */ | 
| Will Deacon | d124433 | 2011-08-04 14:46:23 +0100 | [diff] [blame] | 641 | 	if (!bp->overflow_handler && (arch_check_bp_in_kernelspace(bp) || | 
 | 642 | 	    !core_has_mismatch_brps() || !bp->hw.bp_target)) { | 
 | 643 | 		pr_warning("overflow handler required but none found\n"); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 644 | 		ret = -EINVAL; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 645 | 	} | 
 | 646 | out: | 
 | 647 | 	return ret; | 
 | 648 | } | 
 | 649 |  | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 650 | /* | 
 | 651 |  * Enable/disable single-stepping over the breakpoint bp at address addr. | 
 | 652 |  */ | 
 | 653 | static void enable_single_step(struct perf_event *bp, u32 addr) | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 654 | { | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 655 | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 656 |  | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 657 | 	arch_uninstall_hw_breakpoint(bp); | 
 | 658 | 	info->step_ctrl.mismatch  = 1; | 
 | 659 | 	info->step_ctrl.len	  = ARM_BREAKPOINT_LEN_4; | 
 | 660 | 	info->step_ctrl.type	  = ARM_BREAKPOINT_EXECUTE; | 
 | 661 | 	info->step_ctrl.privilege = info->ctrl.privilege; | 
 | 662 | 	info->step_ctrl.enabled	  = 1; | 
 | 663 | 	info->trigger		  = addr; | 
 | 664 | 	arch_install_hw_breakpoint(bp); | 
 | 665 | } | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 666 |  | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 667 | static void disable_single_step(struct perf_event *bp) | 
 | 668 | { | 
 | 669 | 	arch_uninstall_hw_breakpoint(bp); | 
 | 670 | 	counter_arch_bp(bp)->step_ctrl.enabled = 0; | 
 | 671 | 	arch_install_hw_breakpoint(bp); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 672 | } | 
 | 673 |  | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 674 | static void watchpoint_handler(unsigned long addr, unsigned int fsr, | 
 | 675 | 			       struct pt_regs *regs) | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 676 | { | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 677 | 	int i, access; | 
 | 678 | 	u32 val, ctrl_reg, alignment_mask; | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 679 | 	struct perf_event *wp, **slots; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 680 | 	struct arch_hw_breakpoint *info; | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 681 | 	struct arch_hw_breakpoint_ctrl ctrl; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 682 |  | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 683 | 	slots = (struct perf_event **)__get_cpu_var(wp_on_reg); | 
 | 684 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 685 | 	for (i = 0; i < core_num_wrps; ++i) { | 
 | 686 | 		rcu_read_lock(); | 
 | 687 |  | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 688 | 		wp = slots[i]; | 
 | 689 |  | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 690 | 		if (wp == NULL) | 
 | 691 | 			goto unlock; | 
 | 692 |  | 
 | 693 | 		info = counter_arch_bp(wp); | 
 | 694 | 		/* | 
 | 695 | 		 * The DFAR is an unknown value on debug architectures prior | 
 | 696 | 		 * to 7.1. Since we only allow a single watchpoint on these | 
 | 697 | 		 * older CPUs, we can set the trigger to the lowest possible | 
 | 698 | 		 * faulting address. | 
 | 699 | 		 */ | 
 | 700 | 		if (debug_arch < ARM_DEBUG_ARCH_V7_1) { | 
 | 701 | 			BUG_ON(i > 0); | 
 | 702 | 			info->trigger = wp->attr.bp_addr; | 
 | 703 | 		} else { | 
 | 704 | 			if (info->ctrl.len == ARM_BREAKPOINT_LEN_8) | 
 | 705 | 				alignment_mask = 0x7; | 
 | 706 | 			else | 
 | 707 | 				alignment_mask = 0x3; | 
 | 708 |  | 
 | 709 | 			/* Check if the watchpoint value matches. */ | 
 | 710 | 			val = read_wb_reg(ARM_BASE_WVR + i); | 
 | 711 | 			if (val != (addr & ~alignment_mask)) | 
 | 712 | 				goto unlock; | 
 | 713 |  | 
 | 714 | 			/* Possible match, check the byte address select. */ | 
 | 715 | 			ctrl_reg = read_wb_reg(ARM_BASE_WCR + i); | 
 | 716 | 			decode_ctrl_reg(ctrl_reg, &ctrl); | 
 | 717 | 			if (!((1 << (addr & alignment_mask)) & ctrl.len)) | 
 | 718 | 				goto unlock; | 
 | 719 |  | 
 | 720 | 			/* Check that the access type matches. */ | 
 | 721 | 			access = (fsr & ARM_FSR_ACCESS_MASK) ? HW_BREAKPOINT_W : | 
 | 722 | 				 HW_BREAKPOINT_R; | 
 | 723 | 			if (!(access & hw_breakpoint_type(wp))) | 
 | 724 | 				goto unlock; | 
 | 725 |  | 
 | 726 | 			/* We have a winner. */ | 
 | 727 | 			info->trigger = addr; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 728 | 		} | 
 | 729 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 730 | 		pr_debug("watchpoint fired: address = 0x%x\n", info->trigger); | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 731 | 		perf_bp_event(wp, regs); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 732 |  | 
 | 733 | 		/* | 
 | 734 | 		 * If no overflow handler is present, insert a temporary | 
 | 735 | 		 * mismatch breakpoint so we can single-step over the | 
 | 736 | 		 * watchpoint trigger. | 
 | 737 | 		 */ | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 738 | 		if (!wp->overflow_handler) | 
 | 739 | 			enable_single_step(wp, instruction_pointer(regs)); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 740 |  | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 741 | unlock: | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 742 | 		rcu_read_unlock(); | 
 | 743 | 	} | 
 | 744 | } | 
 | 745 |  | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 746 | static void watchpoint_single_step_handler(unsigned long pc) | 
 | 747 | { | 
 | 748 | 	int i; | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 749 | 	struct perf_event *wp, **slots; | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 750 | 	struct arch_hw_breakpoint *info; | 
 | 751 |  | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 752 | 	slots = (struct perf_event **)__get_cpu_var(wp_on_reg); | 
 | 753 |  | 
| Will Deacon | c512de9 | 2011-08-02 13:01:17 +0100 | [diff] [blame] | 754 | 	for (i = 0; i < core_num_wrps; ++i) { | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 755 | 		rcu_read_lock(); | 
 | 756 |  | 
 | 757 | 		wp = slots[i]; | 
 | 758 |  | 
 | 759 | 		if (wp == NULL) | 
 | 760 | 			goto unlock; | 
 | 761 |  | 
 | 762 | 		info = counter_arch_bp(wp); | 
 | 763 | 		if (!info->step_ctrl.enabled) | 
 | 764 | 			goto unlock; | 
 | 765 |  | 
 | 766 | 		/* | 
 | 767 | 		 * Restore the original watchpoint if we've completed the | 
 | 768 | 		 * single-step. | 
 | 769 | 		 */ | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 770 | 		if (info->trigger != pc) | 
 | 771 | 			disable_single_step(wp); | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 772 |  | 
 | 773 | unlock: | 
 | 774 | 		rcu_read_unlock(); | 
 | 775 | 	} | 
 | 776 | } | 
 | 777 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 778 | static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs) | 
 | 779 | { | 
 | 780 | 	int i; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 781 | 	u32 ctrl_reg, val, addr; | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 782 | 	struct perf_event *bp, **slots; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 783 | 	struct arch_hw_breakpoint *info; | 
 | 784 | 	struct arch_hw_breakpoint_ctrl ctrl; | 
 | 785 |  | 
| Will Deacon | 4a55c18 | 2010-11-29 17:06:53 +0000 | [diff] [blame] | 786 | 	slots = (struct perf_event **)__get_cpu_var(bp_on_reg); | 
 | 787 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 788 | 	/* The exception entry code places the amended lr in the PC. */ | 
 | 789 | 	addr = regs->ARM_pc; | 
 | 790 |  | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 791 | 	/* Check the currently installed breakpoints first. */ | 
 | 792 | 	for (i = 0; i < core_num_brps; ++i) { | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 793 | 		rcu_read_lock(); | 
 | 794 |  | 
 | 795 | 		bp = slots[i]; | 
 | 796 |  | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 797 | 		if (bp == NULL) | 
 | 798 | 			goto unlock; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 799 |  | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 800 | 		info = counter_arch_bp(bp); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 801 |  | 
 | 802 | 		/* Check if the breakpoint value matches. */ | 
 | 803 | 		val = read_wb_reg(ARM_BASE_BVR + i); | 
 | 804 | 		if (val != (addr & ~0x3)) | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 805 | 			goto mismatch; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 806 |  | 
 | 807 | 		/* Possible match, check the byte address select to confirm. */ | 
 | 808 | 		ctrl_reg = read_wb_reg(ARM_BASE_BCR + i); | 
 | 809 | 		decode_ctrl_reg(ctrl_reg, &ctrl); | 
 | 810 | 		if ((1 << (addr & 0x3)) & ctrl.len) { | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 811 | 			info->trigger = addr; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 812 | 			pr_debug("breakpoint fired: address = 0x%x\n", addr); | 
 | 813 | 			perf_bp_event(bp, regs); | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 814 | 			if (!bp->overflow_handler) | 
 | 815 | 				enable_single_step(bp, addr); | 
 | 816 | 			goto unlock; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 817 | 		} | 
 | 818 |  | 
| Will Deacon | 9ebb3cb | 2010-12-01 14:12:13 +0000 | [diff] [blame] | 819 | mismatch: | 
 | 820 | 		/* If we're stepping a breakpoint, it can now be restored. */ | 
 | 821 | 		if (info->step_ctrl.enabled) | 
 | 822 | 			disable_single_step(bp); | 
 | 823 | unlock: | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 824 | 		rcu_read_unlock(); | 
 | 825 | 	} | 
| Will Deacon | 93a04a3 | 2010-11-29 16:56:01 +0000 | [diff] [blame] | 826 |  | 
 | 827 | 	/* Handle any pending watchpoint single-step breakpoints. */ | 
 | 828 | 	watchpoint_single_step_handler(addr); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 829 | } | 
 | 830 |  | 
 | 831 | /* | 
 | 832 |  * Called from either the Data Abort Handler [watchpoint] or the | 
| Russell King | 02fe284 | 2011-06-25 11:44:06 +0100 | [diff] [blame] | 833 |  * Prefetch Abort Handler [breakpoint] with interrupts disabled. | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 834 |  */ | 
 | 835 | static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr, | 
 | 836 | 				 struct pt_regs *regs) | 
 | 837 | { | 
| Will Deacon | 7e20269 | 2010-11-28 14:57:24 +0000 | [diff] [blame] | 838 | 	int ret = 0; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 839 | 	u32 dscr; | 
 | 840 |  | 
| Russell King | 02fe284 | 2011-06-25 11:44:06 +0100 | [diff] [blame] | 841 | 	preempt_disable(); | 
 | 842 |  | 
 | 843 | 	if (interrupts_enabled(regs)) | 
 | 844 | 		local_irq_enable(); | 
| Will Deacon | 7e20269 | 2010-11-28 14:57:24 +0000 | [diff] [blame] | 845 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 846 | 	/* We only handle watchpoints and hardware breakpoints. */ | 
 | 847 | 	ARM_DBG_READ(c1, 0, dscr); | 
 | 848 |  | 
 | 849 | 	/* Perform perf callbacks. */ | 
 | 850 | 	switch (ARM_DSCR_MOE(dscr)) { | 
 | 851 | 	case ARM_ENTRY_BREAKPOINT: | 
 | 852 | 		breakpoint_handler(addr, regs); | 
 | 853 | 		break; | 
 | 854 | 	case ARM_ENTRY_ASYNC_WATCHPOINT: | 
| Joe Perches | 235584b | 2010-10-30 14:21:24 -0700 | [diff] [blame] | 855 | 		WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n"); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 856 | 	case ARM_ENTRY_SYNC_WATCHPOINT: | 
| Will Deacon | 6f26aa0 | 2011-08-02 16:16:57 +0100 | [diff] [blame] | 857 | 		watchpoint_handler(addr, fsr, regs); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 858 | 		break; | 
 | 859 | 	default: | 
| Will Deacon | 7e20269 | 2010-11-28 14:57:24 +0000 | [diff] [blame] | 860 | 		ret = 1; /* Unhandled fault. */ | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 861 | 	} | 
 | 862 |  | 
| Will Deacon | 7e20269 | 2010-11-28 14:57:24 +0000 | [diff] [blame] | 863 | 	preempt_enable(); | 
 | 864 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 865 | 	return ret; | 
 | 866 | } | 
 | 867 |  | 
 | 868 | /* | 
 | 869 |  * One-time initialisation. | 
 | 870 |  */ | 
| Will Deacon | 0d352e3 | 2011-08-08 14:26:53 +0100 | [diff] [blame] | 871 | static cpumask_t debug_err_mask; | 
 | 872 |  | 
 | 873 | static int debug_reg_trap(struct pt_regs *regs, unsigned int instr) | 
 | 874 | { | 
 | 875 | 	int cpu = smp_processor_id(); | 
 | 876 |  | 
 | 877 | 	pr_warning("Debug register access (0x%x) caused undefined instruction on CPU %d\n", | 
 | 878 | 		   instr, cpu); | 
 | 879 |  | 
 | 880 | 	/* Set the error flag for this CPU and skip the faulting instruction. */ | 
 | 881 | 	cpumask_set_cpu(cpu, &debug_err_mask); | 
 | 882 | 	instruction_pointer(regs) += 4; | 
 | 883 | 	return 0; | 
 | 884 | } | 
 | 885 |  | 
 | 886 | static struct undef_hook debug_reg_hook = { | 
 | 887 | 	.instr_mask	= 0x0fe80f10, | 
 | 888 | 	.instr_val	= 0x0e000e10, | 
 | 889 | 	.fn		= debug_reg_trap, | 
 | 890 | }; | 
 | 891 |  | 
 | 892 | static void reset_ctrl_regs(void *unused) | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 893 | { | 
| Jin Hong | 8ec5375 | 2012-09-10 09:37:25 -0700 | [diff] [blame] | 894 | 	int i, raw_num_brps, err = 0, cpu = smp_processor_id(); | 
| Will Deacon | c09bae7 | 2011-02-25 20:20:42 +0100 | [diff] [blame] | 895 | 	u32 dbg_power; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 896 |  | 
| Will Deacon | ac88e07 | 2010-11-24 16:51:17 +0000 | [diff] [blame] | 897 | 	/* | 
 | 898 | 	 * v7 debug contains save and restore registers so that debug state | 
| Will Deacon | ed19b73 | 2011-02-11 15:55:12 +0100 | [diff] [blame] | 899 | 	 * can be maintained across low-power modes without leaving the debug | 
 | 900 | 	 * logic powered up. It is IMPLEMENTATION DEFINED whether we can access | 
 | 901 | 	 * the debug registers out of reset, so we must unlock the OS Lock | 
 | 902 | 	 * Access Register to avoid taking undefined instruction exceptions | 
 | 903 | 	 * later on. | 
| Will Deacon | ac88e07 | 2010-11-24 16:51:17 +0000 | [diff] [blame] | 904 | 	 */ | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 905 | 	switch (debug_arch) { | 
| Will Deacon | a26bce1 | 2011-10-07 15:57:55 +0100 | [diff] [blame] | 906 | 	case ARM_DEBUG_ARCH_V6: | 
 | 907 | 	case ARM_DEBUG_ARCH_V6_1: | 
 | 908 | 		/* ARMv6 cores just need to reset the registers. */ | 
 | 909 | 		goto reset_regs; | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 910 | 	case ARM_DEBUG_ARCH_V7_ECP14: | 
| Will Deacon | ac88e07 | 2010-11-24 16:51:17 +0000 | [diff] [blame] | 911 | 		/* | 
| Will Deacon | c09bae7 | 2011-02-25 20:20:42 +0100 | [diff] [blame] | 912 | 		 * Ensure sticky power-down is clear (i.e. debug logic is | 
 | 913 | 		 * powered up). | 
 | 914 | 		 */ | 
 | 915 | 		asm volatile("mrc p14, 0, %0, c1, c5, 4" : "=r" (dbg_power)); | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 916 | 		if ((dbg_power & 0x1) == 0) | 
 | 917 | 			err = -EPERM; | 
 | 918 | 		break; | 
 | 919 | 	case ARM_DEBUG_ARCH_V7_1: | 
| Will Deacon | c09bae7 | 2011-02-25 20:20:42 +0100 | [diff] [blame] | 920 | 		/* | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 921 | 		 * Ensure the OS double lock is clear. | 
| Will Deacon | ac88e07 | 2010-11-24 16:51:17 +0000 | [diff] [blame] | 922 | 		 */ | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 923 | 		asm volatile("mrc p14, 0, %0, c1, c3, 4" : "=r" (dbg_power)); | 
 | 924 | 		if ((dbg_power & 0x1) == 1) | 
 | 925 | 			err = -EPERM; | 
 | 926 | 		break; | 
| Will Deacon | ac88e07 | 2010-11-24 16:51:17 +0000 | [diff] [blame] | 927 | 	} | 
 | 928 |  | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 929 | 	if (err) { | 
 | 930 | 		pr_warning("CPU %d debug is powered down!\n", cpu); | 
| Will Deacon | 0d352e3 | 2011-08-08 14:26:53 +0100 | [diff] [blame] | 931 | 		cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu)); | 
| Will Deacon | b5d5b8f | 2011-07-22 18:27:37 +0100 | [diff] [blame] | 932 | 		return; | 
 | 933 | 	} | 
 | 934 |  | 
 | 935 | 	/* | 
 | 936 | 	 * Unconditionally clear the lock by writing a value | 
 | 937 | 	 * other than 0xC5ACCE55 to the access register. | 
 | 938 | 	 */ | 
 | 939 | 	asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0)); | 
 | 940 | 	isb(); | 
 | 941 |  | 
 | 942 | 	/* | 
 | 943 | 	 * Clear any configured vector-catch events before | 
 | 944 | 	 * enabling monitor mode. | 
 | 945 | 	 */ | 
 | 946 | 	asm volatile("mcr p14, 0, %0, c0, c7, 0" : : "r" (0)); | 
 | 947 | 	isb(); | 
 | 948 |  | 
| Will Deacon | a26bce1 | 2011-10-07 15:57:55 +0100 | [diff] [blame] | 949 | reset_regs: | 
| Stephen Boyd | 5607d22 | 2012-09-18 20:23:13 -0700 | [diff] [blame] | 950 | 	if (halting_mode_enabled()) | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 951 | 		return; | 
 | 952 |  | 
| Jin Hong | 8ec5375 | 2012-09-10 09:37:25 -0700 | [diff] [blame] | 953 | 	/* We must also reset any reserved registers. */ | 
 | 954 | 	raw_num_brps = get_num_brp_resources(); | 
 | 955 | 	for (i = 0; i < raw_num_brps; ++i) { | 
 | 956 | 		write_wb_reg(ARM_BASE_BCR + i, 0UL); | 
 | 957 | 		write_wb_reg(ARM_BASE_BVR + i, 0UL); | 
 | 958 | 	} | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 959 |  | 
 | 960 | 	for (i = 0; i < core_num_wrps; ++i) { | 
 | 961 | 		write_wb_reg(ARM_BASE_WCR + i, 0UL); | 
 | 962 | 		write_wb_reg(ARM_BASE_WVR + i, 0UL); | 
 | 963 | 	} | 
| Stephen Boyd | 5607d22 | 2012-09-18 20:23:13 -0700 | [diff] [blame] | 964 | 	enable_monitor_mode(); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 965 | } | 
 | 966 |  | 
| Will Deacon | 7d99331 | 2010-11-24 17:45:49 +0000 | [diff] [blame] | 967 | static int __cpuinit dbg_reset_notify(struct notifier_block *self, | 
 | 968 | 				      unsigned long action, void *cpu) | 
 | 969 | { | 
 | 970 | 	if (action == CPU_ONLINE) | 
 | 971 | 		smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1); | 
| Will Deacon | 0d352e3 | 2011-08-08 14:26:53 +0100 | [diff] [blame] | 972 |  | 
| Will Deacon | 7d99331 | 2010-11-24 17:45:49 +0000 | [diff] [blame] | 973 | 	return NOTIFY_OK; | 
 | 974 | } | 
 | 975 |  | 
 | 976 | static struct notifier_block __cpuinitdata dbg_reset_nb = { | 
 | 977 | 	.notifier_call = dbg_reset_notify, | 
 | 978 | }; | 
 | 979 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 980 | static int __init arch_hw_breakpoint_init(void) | 
 | 981 | { | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 982 | 	u32 dscr; | 
 | 983 |  | 
 | 984 | 	debug_arch = get_debug_arch(); | 
 | 985 |  | 
| Will Deacon | 66e1cfe | 2011-02-11 16:01:42 +0100 | [diff] [blame] | 986 | 	if (!debug_arch_supported()) { | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 987 | 		pr_info("debug architecture 0x%x unsupported.\n", debug_arch); | 
| Will Deacon | 8fbf397 | 2010-12-01 17:37:45 +0000 | [diff] [blame] | 988 | 		return 0; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 989 | 	} | 
 | 990 |  | 
 | 991 | 	/* Determine how many BRPs/WRPs are available. */ | 
 | 992 | 	core_num_brps = get_num_brps(); | 
 | 993 | 	core_num_wrps = get_num_wrps(); | 
 | 994 |  | 
| Will Deacon | 0d352e3 | 2011-08-08 14:26:53 +0100 | [diff] [blame] | 995 | 	/* | 
 | 996 | 	 * We need to tread carefully here because DBGSWENABLE may be | 
 | 997 | 	 * driven low on this core and there isn't an architected way to | 
 | 998 | 	 * determine that. | 
 | 999 | 	 */ | 
 | 1000 | 	register_undef_hook(&debug_reg_hook); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 1001 |  | 
| Will Deacon | ed19b73 | 2011-02-11 15:55:12 +0100 | [diff] [blame] | 1002 | 	/* | 
 | 1003 | 	 * Reset the breakpoint resources. We assume that a halting | 
 | 1004 | 	 * debugger will leave the world in a nice state for us. | 
 | 1005 | 	 */ | 
| Will Deacon | 0d352e3 | 2011-08-08 14:26:53 +0100 | [diff] [blame] | 1006 | 	on_each_cpu(reset_ctrl_regs, NULL, 1); | 
 | 1007 | 	unregister_undef_hook(&debug_reg_hook); | 
 | 1008 | 	if (!cpumask_empty(&debug_err_mask)) { | 
| Will Deacon | c09bae7 | 2011-02-25 20:20:42 +0100 | [diff] [blame] | 1009 | 		core_num_brps = 0; | 
| Will Deacon | c09bae7 | 2011-02-25 20:20:42 +0100 | [diff] [blame] | 1010 | 		core_num_wrps = 0; | 
 | 1011 | 		return 0; | 
 | 1012 | 	} | 
| Will Deacon | ed19b73 | 2011-02-11 15:55:12 +0100 | [diff] [blame] | 1013 |  | 
| Will Deacon | 0d352e3 | 2011-08-08 14:26:53 +0100 | [diff] [blame] | 1014 | 	pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n", | 
 | 1015 | 		core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " : | 
 | 1016 | 		"", core_num_wrps); | 
 | 1017 |  | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 1018 | 	ARM_DBG_READ(c1, 0, dscr); | 
 | 1019 | 	if (dscr & ARM_DSCR_HDBGEN) { | 
| Will Deacon | ed19b73 | 2011-02-11 15:55:12 +0100 | [diff] [blame] | 1020 | 		max_watchpoint_len = 4; | 
| Stephen Boyd | 7d85d61 | 2011-03-10 05:15:00 +0100 | [diff] [blame] | 1021 | 		pr_warning("halting debug mode enabled. Assuming maximum watchpoint size of %u bytes.\n", | 
 | 1022 | 			   max_watchpoint_len); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 1023 | 	} else { | 
| Will Deacon | ac88e07 | 2010-11-24 16:51:17 +0000 | [diff] [blame] | 1024 | 		/* Work out the maximum supported watchpoint length. */ | 
 | 1025 | 		max_watchpoint_len = get_max_wp_len(); | 
 | 1026 | 		pr_info("maximum watchpoint size is %u bytes.\n", | 
 | 1027 | 				max_watchpoint_len); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 1028 | 	} | 
 | 1029 |  | 
 | 1030 | 	/* Register debug fault handler. */ | 
| Catalin Marinas | f7b8156 | 2011-11-22 17:30:31 +0000 | [diff] [blame] | 1031 | 	hook_fault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP, | 
 | 1032 | 			TRAP_HWBKPT, "watchpoint debug exception"); | 
 | 1033 | 	hook_ifault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP, | 
 | 1034 | 			TRAP_HWBKPT, "breakpoint debug exception"); | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 1035 |  | 
| Will Deacon | 7d99331 | 2010-11-24 17:45:49 +0000 | [diff] [blame] | 1036 | 	/* Register hotplug notifier. */ | 
 | 1037 | 	register_cpu_notifier(&dbg_reset_nb); | 
| Will Deacon | 8fbf397 | 2010-12-01 17:37:45 +0000 | [diff] [blame] | 1038 | 	return 0; | 
| Will Deacon | f81ef4a | 2010-09-03 10:41:08 +0100 | [diff] [blame] | 1039 | } | 
 | 1040 | arch_initcall(arch_hw_breakpoint_init); | 
 | 1041 |  | 
 | 1042 | void hw_breakpoint_pmu_read(struct perf_event *bp) | 
 | 1043 | { | 
 | 1044 | } | 
 | 1045 |  | 
 | 1046 | /* | 
 | 1047 |  * Dummy function to register with die_notifier. | 
 | 1048 |  */ | 
 | 1049 | int hw_breakpoint_exceptions_notify(struct notifier_block *unused, | 
 | 1050 | 					unsigned long val, void *data) | 
 | 1051 | { | 
 | 1052 | 	return NOTIFY_DONE; | 
 | 1053 | } |