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): \ 579e962f76SDietmar Eggemann ARM_DBG_READ(c0, 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): \ 629e962f76SDietmar Eggemann ARM_DBG_WRITE(c0, 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) { 1395ad29ea2SWill Deacon pr_warn_once("CPUID feature registers not supported. " 140d1244336SWill Deacon "Assuming v6 debug is present.\n"); 1410017ff42SWill Deacon return ARM_DEBUG_ARCH_V6; 142d1244336SWill Deacon } 1430017ff42SWill Deacon 1449e962f76SDietmar Eggemann ARM_DBG_READ(c0, 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 162bf880114SWill Deacon /* Can we determine the watchpoint access type from the fsr? */ 163bf880114SWill Deacon static int debug_exception_updates_fsr(void) 164bf880114SWill Deacon { 165bf880114SWill Deacon return 0; 166bf880114SWill Deacon } 167bf880114SWill Deacon 168c512de95SWill Deacon /* Determine number of WRP registers available. */ 169c512de95SWill Deacon static int get_num_wrp_resources(void) 170c512de95SWill Deacon { 171c512de95SWill Deacon u32 didr; 1729e962f76SDietmar Eggemann ARM_DBG_READ(c0, 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; 1809e962f76SDietmar Eggemann ARM_DBG_READ(c0, 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 */ 2310daa034eSWill Deacon static int monitor_mode_enabled(void) 2320daa034eSWill Deacon { 2330daa034eSWill Deacon u32 dscr; 2349e962f76SDietmar Eggemann ARM_DBG_READ(c0, c1, 0, dscr); 2350daa034eSWill Deacon return !!(dscr & ARM_DSCR_MDBGEN); 2360daa034eSWill Deacon } 2370daa034eSWill Deacon 238f81ef4a9SWill Deacon static int enable_monitor_mode(void) 239f81ef4a9SWill Deacon { 240f81ef4a9SWill Deacon u32 dscr; 2419e962f76SDietmar Eggemann ARM_DBG_READ(c0, c1, 0, dscr); 242f81ef4a9SWill Deacon 2438fbf397cSWill Deacon /* If monitor mode is already enabled, just return. */ 2448fbf397cSWill Deacon if (dscr & ARM_DSCR_MDBGEN) 2458fbf397cSWill Deacon goto out; 2468fbf397cSWill Deacon 247f81ef4a9SWill Deacon /* Write to the corresponding DSCR. */ 2488fbf397cSWill Deacon switch (get_debug_arch()) { 249f81ef4a9SWill Deacon case ARM_DEBUG_ARCH_V6: 250f81ef4a9SWill Deacon case ARM_DEBUG_ARCH_V6_1: 2519e962f76SDietmar Eggemann ARM_DBG_WRITE(c0, c1, 0, (dscr | ARM_DSCR_MDBGEN)); 252f81ef4a9SWill Deacon break; 253f81ef4a9SWill Deacon case ARM_DEBUG_ARCH_V7_ECP14: 254b5d5b8f9SWill Deacon case ARM_DEBUG_ARCH_V7_1: 2559e962f76SDietmar Eggemann ARM_DBG_WRITE(c0, c2, 2, (dscr | ARM_DSCR_MDBGEN)); 256b59a540cSWill Deacon isb(); 257f81ef4a9SWill Deacon break; 258f81ef4a9SWill Deacon default: 259614bea50SWill Deacon return -ENODEV; 260f81ef4a9SWill Deacon } 261f81ef4a9SWill Deacon 262f81ef4a9SWill Deacon /* Check that the write made it through. */ 2639e962f76SDietmar Eggemann ARM_DBG_READ(c0, c1, 0, dscr); 264*f435ab79SWill Deacon if (!(dscr & ARM_DSCR_MDBGEN)) { 265*f435ab79SWill Deacon pr_warn_once("Failed to enable monitor mode on CPU %d.\n", 266*f435ab79SWill Deacon smp_processor_id()); 267614bea50SWill Deacon return -EPERM; 268*f435ab79SWill Deacon } 269f81ef4a9SWill Deacon 270f81ef4a9SWill Deacon out: 271614bea50SWill Deacon return 0; 272f81ef4a9SWill Deacon } 273f81ef4a9SWill Deacon 2748fbf397cSWill Deacon int hw_breakpoint_slots(int type) 2758fbf397cSWill Deacon { 27666e1cfe6SWill Deacon if (!debug_arch_supported()) 27766e1cfe6SWill Deacon return 0; 27866e1cfe6SWill Deacon 2798fbf397cSWill Deacon /* 2808fbf397cSWill Deacon * We can be called early, so don't rely on 2818fbf397cSWill Deacon * our static variables being initialised. 2828fbf397cSWill Deacon */ 2838fbf397cSWill Deacon switch (type) { 2848fbf397cSWill Deacon case TYPE_INST: 2858fbf397cSWill Deacon return get_num_brps(); 2868fbf397cSWill Deacon case TYPE_DATA: 2878fbf397cSWill Deacon return get_num_wrps(); 2888fbf397cSWill Deacon default: 2898fbf397cSWill Deacon pr_warning("unknown slot type: %d\n", type); 2908fbf397cSWill Deacon return 0; 2918fbf397cSWill Deacon } 2928fbf397cSWill Deacon } 2938fbf397cSWill Deacon 294f81ef4a9SWill Deacon /* 295f81ef4a9SWill Deacon * Check if 8-bit byte-address select is available. 296f81ef4a9SWill Deacon * This clobbers WRP 0. 297f81ef4a9SWill Deacon */ 298f81ef4a9SWill Deacon static u8 get_max_wp_len(void) 299f81ef4a9SWill Deacon { 300f81ef4a9SWill Deacon u32 ctrl_reg; 301f81ef4a9SWill Deacon struct arch_hw_breakpoint_ctrl ctrl; 302f81ef4a9SWill Deacon u8 size = 4; 303f81ef4a9SWill Deacon 304f81ef4a9SWill Deacon if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14) 305f81ef4a9SWill Deacon goto out; 306f81ef4a9SWill Deacon 307f81ef4a9SWill Deacon memset(&ctrl, 0, sizeof(ctrl)); 308f81ef4a9SWill Deacon ctrl.len = ARM_BREAKPOINT_LEN_8; 309f81ef4a9SWill Deacon ctrl_reg = encode_ctrl_reg(ctrl); 310f81ef4a9SWill Deacon 311f81ef4a9SWill Deacon write_wb_reg(ARM_BASE_WVR, 0); 312f81ef4a9SWill Deacon write_wb_reg(ARM_BASE_WCR, ctrl_reg); 313f81ef4a9SWill Deacon if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg) 314f81ef4a9SWill Deacon size = 8; 315f81ef4a9SWill Deacon 316f81ef4a9SWill Deacon out: 317f81ef4a9SWill Deacon return size; 318f81ef4a9SWill Deacon } 319f81ef4a9SWill Deacon 320f81ef4a9SWill Deacon u8 arch_get_max_wp_len(void) 321f81ef4a9SWill Deacon { 322f81ef4a9SWill Deacon return max_watchpoint_len; 323f81ef4a9SWill Deacon } 324f81ef4a9SWill Deacon 325f81ef4a9SWill Deacon /* 326f81ef4a9SWill Deacon * Install a perf counter breakpoint. 327f81ef4a9SWill Deacon */ 328f81ef4a9SWill Deacon int arch_install_hw_breakpoint(struct perf_event *bp) 329f81ef4a9SWill Deacon { 330f81ef4a9SWill Deacon struct arch_hw_breakpoint *info = counter_arch_bp(bp); 331f81ef4a9SWill Deacon struct perf_event **slot, **slots; 3320daa034eSWill Deacon int i, max_slots, ctrl_base, val_base; 33393a04a34SWill Deacon u32 addr, ctrl; 334f81ef4a9SWill Deacon 33593a04a34SWill Deacon addr = info->address; 33693a04a34SWill Deacon ctrl = encode_ctrl_reg(info->ctrl) | 0x1; 33793a04a34SWill Deacon 338f81ef4a9SWill Deacon if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) { 339f81ef4a9SWill Deacon /* Breakpoint */ 340f81ef4a9SWill Deacon ctrl_base = ARM_BASE_BCR; 341f81ef4a9SWill Deacon val_base = ARM_BASE_BVR; 3424a55c18eSWill Deacon slots = (struct perf_event **)__get_cpu_var(bp_on_reg); 3430017ff42SWill Deacon max_slots = core_num_brps; 344f81ef4a9SWill Deacon } else { 345f81ef4a9SWill Deacon /* Watchpoint */ 346f81ef4a9SWill Deacon ctrl_base = ARM_BASE_WCR; 347f81ef4a9SWill Deacon val_base = ARM_BASE_WVR; 3484a55c18eSWill Deacon slots = (struct perf_event **)__get_cpu_var(wp_on_reg); 349f81ef4a9SWill Deacon max_slots = core_num_wrps; 350f81ef4a9SWill Deacon } 351f81ef4a9SWill Deacon 352f81ef4a9SWill Deacon for (i = 0; i < max_slots; ++i) { 353f81ef4a9SWill Deacon slot = &slots[i]; 354f81ef4a9SWill Deacon 355f81ef4a9SWill Deacon if (!*slot) { 356f81ef4a9SWill Deacon *slot = bp; 357f81ef4a9SWill Deacon break; 358f81ef4a9SWill Deacon } 359f81ef4a9SWill Deacon } 360f81ef4a9SWill Deacon 361*f435ab79SWill Deacon if (i == max_slots) { 362*f435ab79SWill Deacon pr_warning("Can't find any breakpoint slot\n"); 3630daa034eSWill Deacon return -EBUSY; 364*f435ab79SWill Deacon } 365f81ef4a9SWill Deacon 3666f26aa05SWill Deacon /* Override the breakpoint data with the step data. */ 3676f26aa05SWill Deacon if (info->step_ctrl.enabled) { 3686f26aa05SWill Deacon addr = info->trigger & ~0x3; 3696f26aa05SWill Deacon ctrl = encode_ctrl_reg(info->step_ctrl); 3706f26aa05SWill Deacon if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE) { 3716f26aa05SWill Deacon i = 0; 3726f26aa05SWill Deacon ctrl_base = ARM_BASE_BCR + core_num_brps; 3736f26aa05SWill Deacon val_base = ARM_BASE_BVR + core_num_brps; 3746f26aa05SWill Deacon } 3756f26aa05SWill Deacon } 3766f26aa05SWill Deacon 377f81ef4a9SWill Deacon /* Setup the address register. */ 37893a04a34SWill Deacon write_wb_reg(val_base + i, addr); 379f81ef4a9SWill Deacon 380f81ef4a9SWill Deacon /* Setup the control register. */ 38193a04a34SWill Deacon write_wb_reg(ctrl_base + i, ctrl); 3820daa034eSWill Deacon return 0; 383f81ef4a9SWill Deacon } 384f81ef4a9SWill Deacon 385f81ef4a9SWill Deacon void arch_uninstall_hw_breakpoint(struct perf_event *bp) 386f81ef4a9SWill Deacon { 387f81ef4a9SWill Deacon struct arch_hw_breakpoint *info = counter_arch_bp(bp); 388f81ef4a9SWill Deacon struct perf_event **slot, **slots; 389f81ef4a9SWill Deacon int i, max_slots, base; 390f81ef4a9SWill Deacon 391f81ef4a9SWill Deacon if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) { 392f81ef4a9SWill Deacon /* Breakpoint */ 393f81ef4a9SWill Deacon base = ARM_BASE_BCR; 3944a55c18eSWill Deacon slots = (struct perf_event **)__get_cpu_var(bp_on_reg); 3950017ff42SWill Deacon max_slots = core_num_brps; 396f81ef4a9SWill Deacon } else { 397f81ef4a9SWill Deacon /* Watchpoint */ 398f81ef4a9SWill Deacon base = ARM_BASE_WCR; 3994a55c18eSWill Deacon slots = (struct perf_event **)__get_cpu_var(wp_on_reg); 400f81ef4a9SWill Deacon max_slots = core_num_wrps; 401f81ef4a9SWill Deacon } 402f81ef4a9SWill Deacon 403f81ef4a9SWill Deacon /* Remove the breakpoint. */ 404f81ef4a9SWill Deacon for (i = 0; i < max_slots; ++i) { 405f81ef4a9SWill Deacon slot = &slots[i]; 406f81ef4a9SWill Deacon 407f81ef4a9SWill Deacon if (*slot == bp) { 408f81ef4a9SWill Deacon *slot = NULL; 409f81ef4a9SWill Deacon break; 410f81ef4a9SWill Deacon } 411f81ef4a9SWill Deacon } 412f81ef4a9SWill Deacon 413*f435ab79SWill Deacon if (i == max_slots) { 414*f435ab79SWill Deacon pr_warning("Can't find any breakpoint slot\n"); 415f81ef4a9SWill Deacon return; 416*f435ab79SWill Deacon } 417f81ef4a9SWill Deacon 4186f26aa05SWill Deacon /* Ensure that we disable the mismatch breakpoint. */ 4196f26aa05SWill Deacon if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE && 4206f26aa05SWill Deacon info->step_ctrl.enabled) { 4216f26aa05SWill Deacon i = 0; 4226f26aa05SWill Deacon base = ARM_BASE_BCR + core_num_brps; 4236f26aa05SWill Deacon } 4246f26aa05SWill Deacon 425f81ef4a9SWill Deacon /* Reset the control register. */ 426f81ef4a9SWill Deacon write_wb_reg(base + i, 0); 427f81ef4a9SWill Deacon } 428f81ef4a9SWill Deacon 429f81ef4a9SWill Deacon static int get_hbp_len(u8 hbp_len) 430f81ef4a9SWill Deacon { 431f81ef4a9SWill Deacon unsigned int len_in_bytes = 0; 432f81ef4a9SWill Deacon 433f81ef4a9SWill Deacon switch (hbp_len) { 434f81ef4a9SWill Deacon case ARM_BREAKPOINT_LEN_1: 435f81ef4a9SWill Deacon len_in_bytes = 1; 436f81ef4a9SWill Deacon break; 437f81ef4a9SWill Deacon case ARM_BREAKPOINT_LEN_2: 438f81ef4a9SWill Deacon len_in_bytes = 2; 439f81ef4a9SWill Deacon break; 440f81ef4a9SWill Deacon case ARM_BREAKPOINT_LEN_4: 441f81ef4a9SWill Deacon len_in_bytes = 4; 442f81ef4a9SWill Deacon break; 443f81ef4a9SWill Deacon case ARM_BREAKPOINT_LEN_8: 444f81ef4a9SWill Deacon len_in_bytes = 8; 445f81ef4a9SWill Deacon break; 446f81ef4a9SWill Deacon } 447f81ef4a9SWill Deacon 448f81ef4a9SWill Deacon return len_in_bytes; 449f81ef4a9SWill Deacon } 450f81ef4a9SWill Deacon 451f81ef4a9SWill Deacon /* 452f81ef4a9SWill Deacon * Check whether bp virtual address is in kernel space. 453f81ef4a9SWill Deacon */ 454f81ef4a9SWill Deacon int arch_check_bp_in_kernelspace(struct perf_event *bp) 455f81ef4a9SWill Deacon { 456f81ef4a9SWill Deacon unsigned int len; 457f81ef4a9SWill Deacon unsigned long va; 458f81ef4a9SWill Deacon struct arch_hw_breakpoint *info = counter_arch_bp(bp); 459f81ef4a9SWill Deacon 460f81ef4a9SWill Deacon va = info->address; 461f81ef4a9SWill Deacon len = get_hbp_len(info->ctrl.len); 462f81ef4a9SWill Deacon 463f81ef4a9SWill Deacon return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); 464f81ef4a9SWill Deacon } 465f81ef4a9SWill Deacon 466f81ef4a9SWill Deacon /* 467f81ef4a9SWill Deacon * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl. 468f81ef4a9SWill Deacon * Hopefully this will disappear when ptrace can bypass the conversion 469f81ef4a9SWill Deacon * to generic breakpoint descriptions. 470f81ef4a9SWill Deacon */ 471f81ef4a9SWill Deacon int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl, 472f81ef4a9SWill Deacon int *gen_len, int *gen_type) 473f81ef4a9SWill Deacon { 474f81ef4a9SWill Deacon /* Type */ 475f81ef4a9SWill Deacon switch (ctrl.type) { 476f81ef4a9SWill Deacon case ARM_BREAKPOINT_EXECUTE: 477f81ef4a9SWill Deacon *gen_type = HW_BREAKPOINT_X; 478f81ef4a9SWill Deacon break; 479f81ef4a9SWill Deacon case ARM_BREAKPOINT_LOAD: 480f81ef4a9SWill Deacon *gen_type = HW_BREAKPOINT_R; 481f81ef4a9SWill Deacon break; 482f81ef4a9SWill Deacon case ARM_BREAKPOINT_STORE: 483f81ef4a9SWill Deacon *gen_type = HW_BREAKPOINT_W; 484f81ef4a9SWill Deacon break; 485f81ef4a9SWill Deacon case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE: 486f81ef4a9SWill Deacon *gen_type = HW_BREAKPOINT_RW; 487f81ef4a9SWill Deacon break; 488f81ef4a9SWill Deacon default: 489f81ef4a9SWill Deacon return -EINVAL; 490f81ef4a9SWill Deacon } 491f81ef4a9SWill Deacon 492f81ef4a9SWill Deacon /* Len */ 493f81ef4a9SWill Deacon switch (ctrl.len) { 494f81ef4a9SWill Deacon case ARM_BREAKPOINT_LEN_1: 495f81ef4a9SWill Deacon *gen_len = HW_BREAKPOINT_LEN_1; 496f81ef4a9SWill Deacon break; 497f81ef4a9SWill Deacon case ARM_BREAKPOINT_LEN_2: 498f81ef4a9SWill Deacon *gen_len = HW_BREAKPOINT_LEN_2; 499f81ef4a9SWill Deacon break; 500f81ef4a9SWill Deacon case ARM_BREAKPOINT_LEN_4: 501f81ef4a9SWill Deacon *gen_len = HW_BREAKPOINT_LEN_4; 502f81ef4a9SWill Deacon break; 503f81ef4a9SWill Deacon case ARM_BREAKPOINT_LEN_8: 504f81ef4a9SWill Deacon *gen_len = HW_BREAKPOINT_LEN_8; 505f81ef4a9SWill Deacon break; 506f81ef4a9SWill Deacon default: 507f81ef4a9SWill Deacon return -EINVAL; 508f81ef4a9SWill Deacon } 509f81ef4a9SWill Deacon 510f81ef4a9SWill Deacon return 0; 511f81ef4a9SWill Deacon } 512f81ef4a9SWill Deacon 513f81ef4a9SWill Deacon /* 514f81ef4a9SWill Deacon * Construct an arch_hw_breakpoint from a perf_event. 515f81ef4a9SWill Deacon */ 516f81ef4a9SWill Deacon static int arch_build_bp_info(struct perf_event *bp) 517f81ef4a9SWill Deacon { 518f81ef4a9SWill Deacon struct arch_hw_breakpoint *info = counter_arch_bp(bp); 519f81ef4a9SWill Deacon 520f81ef4a9SWill Deacon /* Type */ 521f81ef4a9SWill Deacon switch (bp->attr.bp_type) { 522f81ef4a9SWill Deacon case HW_BREAKPOINT_X: 523f81ef4a9SWill Deacon info->ctrl.type = ARM_BREAKPOINT_EXECUTE; 524f81ef4a9SWill Deacon break; 525f81ef4a9SWill Deacon case HW_BREAKPOINT_R: 526f81ef4a9SWill Deacon info->ctrl.type = ARM_BREAKPOINT_LOAD; 527f81ef4a9SWill Deacon break; 528f81ef4a9SWill Deacon case HW_BREAKPOINT_W: 529f81ef4a9SWill Deacon info->ctrl.type = ARM_BREAKPOINT_STORE; 530f81ef4a9SWill Deacon break; 531f81ef4a9SWill Deacon case HW_BREAKPOINT_RW: 532f81ef4a9SWill Deacon info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE; 533f81ef4a9SWill Deacon break; 534f81ef4a9SWill Deacon default: 535f81ef4a9SWill Deacon return -EINVAL; 536f81ef4a9SWill Deacon } 537f81ef4a9SWill Deacon 538f81ef4a9SWill Deacon /* Len */ 539f81ef4a9SWill Deacon switch (bp->attr.bp_len) { 540f81ef4a9SWill Deacon case HW_BREAKPOINT_LEN_1: 541f81ef4a9SWill Deacon info->ctrl.len = ARM_BREAKPOINT_LEN_1; 542f81ef4a9SWill Deacon break; 543f81ef4a9SWill Deacon case HW_BREAKPOINT_LEN_2: 544f81ef4a9SWill Deacon info->ctrl.len = ARM_BREAKPOINT_LEN_2; 545f81ef4a9SWill Deacon break; 546f81ef4a9SWill Deacon case HW_BREAKPOINT_LEN_4: 547f81ef4a9SWill Deacon info->ctrl.len = ARM_BREAKPOINT_LEN_4; 548f81ef4a9SWill Deacon break; 549f81ef4a9SWill Deacon case HW_BREAKPOINT_LEN_8: 550f81ef4a9SWill Deacon info->ctrl.len = ARM_BREAKPOINT_LEN_8; 551f81ef4a9SWill Deacon if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE) 552f81ef4a9SWill Deacon && max_watchpoint_len >= 8) 553f81ef4a9SWill Deacon break; 554f81ef4a9SWill Deacon default: 555f81ef4a9SWill Deacon return -EINVAL; 556f81ef4a9SWill Deacon } 557f81ef4a9SWill Deacon 5586ee33c27SWill Deacon /* 5596ee33c27SWill Deacon * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes. 5606ee33c27SWill Deacon * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported 5616ee33c27SWill Deacon * by the hardware and must be aligned to the appropriate number of 5626ee33c27SWill Deacon * bytes. 5636ee33c27SWill Deacon */ 5646ee33c27SWill Deacon if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE && 5656ee33c27SWill Deacon info->ctrl.len != ARM_BREAKPOINT_LEN_2 && 5666ee33c27SWill Deacon info->ctrl.len != ARM_BREAKPOINT_LEN_4) 5676ee33c27SWill Deacon return -EINVAL; 5686ee33c27SWill Deacon 569f81ef4a9SWill Deacon /* Address */ 570f81ef4a9SWill Deacon info->address = bp->attr.bp_addr; 571f81ef4a9SWill Deacon 572f81ef4a9SWill Deacon /* Privilege */ 573f81ef4a9SWill Deacon info->ctrl.privilege = ARM_BREAKPOINT_USER; 57493a04a34SWill Deacon if (arch_check_bp_in_kernelspace(bp)) 575f81ef4a9SWill Deacon info->ctrl.privilege |= ARM_BREAKPOINT_PRIV; 576f81ef4a9SWill Deacon 577f81ef4a9SWill Deacon /* Enabled? */ 578f81ef4a9SWill Deacon info->ctrl.enabled = !bp->attr.disabled; 579f81ef4a9SWill Deacon 580f81ef4a9SWill Deacon /* Mismatch */ 581f81ef4a9SWill Deacon info->ctrl.mismatch = 0; 582f81ef4a9SWill Deacon 583f81ef4a9SWill Deacon return 0; 584f81ef4a9SWill Deacon } 585f81ef4a9SWill Deacon 586f81ef4a9SWill Deacon /* 587f81ef4a9SWill Deacon * Validate the arch-specific HW Breakpoint register settings. 588f81ef4a9SWill Deacon */ 589f81ef4a9SWill Deacon int arch_validate_hwbkpt_settings(struct perf_event *bp) 590f81ef4a9SWill Deacon { 591f81ef4a9SWill Deacon struct arch_hw_breakpoint *info = counter_arch_bp(bp); 592f81ef4a9SWill Deacon int ret = 0; 5936ee33c27SWill Deacon u32 offset, alignment_mask = 0x3; 594f81ef4a9SWill Deacon 5950daa034eSWill Deacon /* Ensure that we are in monitor debug mode. */ 5960daa034eSWill Deacon if (!monitor_mode_enabled()) 5970daa034eSWill Deacon return -ENODEV; 5980daa034eSWill 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 case 2: 6146ee33c27SWill Deacon /* Allow halfword watchpoints and breakpoints. */ 6156ee33c27SWill Deacon if (info->ctrl.len == ARM_BREAKPOINT_LEN_2) 6166ee33c27SWill Deacon break; 617d968d2b8SWill Deacon case 3: 618d968d2b8SWill Deacon /* Allow single byte watchpoint. */ 619d968d2b8SWill Deacon if (info->ctrl.len == ARM_BREAKPOINT_LEN_1) 620d968d2b8SWill Deacon break; 6216ee33c27SWill Deacon default: 6226ee33c27SWill Deacon ret = -EINVAL; 623f81ef4a9SWill Deacon goto out; 624f81ef4a9SWill Deacon } 625f81ef4a9SWill Deacon 6266ee33c27SWill Deacon info->address &= ~alignment_mask; 627f81ef4a9SWill Deacon info->ctrl.len <<= offset; 628f81ef4a9SWill Deacon 629bf880114SWill Deacon if (!bp->overflow_handler) { 630f81ef4a9SWill Deacon /* 631bf880114SWill Deacon * Mismatch breakpoints are required for single-stepping 632bf880114SWill Deacon * breakpoints. 633f81ef4a9SWill Deacon */ 634bf880114SWill Deacon if (!core_has_mismatch_brps()) 635bf880114SWill Deacon return -EINVAL; 636bf880114SWill Deacon 637bf880114SWill Deacon /* We don't allow mismatch breakpoints in kernel space. */ 638bf880114SWill Deacon if (arch_check_bp_in_kernelspace(bp)) 639bf880114SWill Deacon return -EPERM; 640bf880114SWill Deacon 641bf880114SWill Deacon /* 642bf880114SWill Deacon * Per-cpu breakpoints are not supported by our stepping 643bf880114SWill Deacon * mechanism. 644bf880114SWill Deacon */ 645bf880114SWill Deacon if (!bp->hw.bp_target) 646bf880114SWill Deacon return -EINVAL; 647bf880114SWill Deacon 648bf880114SWill Deacon /* 649bf880114SWill Deacon * We only support specific access types if the fsr 650bf880114SWill Deacon * reports them. 651bf880114SWill Deacon */ 652bf880114SWill Deacon if (!debug_exception_updates_fsr() && 653bf880114SWill Deacon (info->ctrl.type == ARM_BREAKPOINT_LOAD || 654bf880114SWill Deacon info->ctrl.type == ARM_BREAKPOINT_STORE)) 655bf880114SWill Deacon return -EINVAL; 656f81ef4a9SWill Deacon } 657bf880114SWill Deacon 658f81ef4a9SWill Deacon out: 659f81ef4a9SWill Deacon return ret; 660f81ef4a9SWill Deacon } 661f81ef4a9SWill Deacon 6629ebb3cbcSWill Deacon /* 6639ebb3cbcSWill Deacon * Enable/disable single-stepping over the breakpoint bp at address addr. 6649ebb3cbcSWill Deacon */ 6659ebb3cbcSWill Deacon static void enable_single_step(struct perf_event *bp, u32 addr) 666f81ef4a9SWill Deacon { 6679ebb3cbcSWill Deacon struct arch_hw_breakpoint *info = counter_arch_bp(bp); 668f81ef4a9SWill Deacon 6699ebb3cbcSWill Deacon arch_uninstall_hw_breakpoint(bp); 6709ebb3cbcSWill Deacon info->step_ctrl.mismatch = 1; 6719ebb3cbcSWill Deacon info->step_ctrl.len = ARM_BREAKPOINT_LEN_4; 6729ebb3cbcSWill Deacon info->step_ctrl.type = ARM_BREAKPOINT_EXECUTE; 6739ebb3cbcSWill Deacon info->step_ctrl.privilege = info->ctrl.privilege; 6749ebb3cbcSWill Deacon info->step_ctrl.enabled = 1; 6759ebb3cbcSWill Deacon info->trigger = addr; 6769ebb3cbcSWill Deacon arch_install_hw_breakpoint(bp); 677f81ef4a9SWill Deacon } 6789ebb3cbcSWill Deacon 6799ebb3cbcSWill Deacon static void disable_single_step(struct perf_event *bp) 6809ebb3cbcSWill Deacon { 6819ebb3cbcSWill Deacon arch_uninstall_hw_breakpoint(bp); 6829ebb3cbcSWill Deacon counter_arch_bp(bp)->step_ctrl.enabled = 0; 6839ebb3cbcSWill Deacon arch_install_hw_breakpoint(bp); 684f81ef4a9SWill Deacon } 685f81ef4a9SWill Deacon 6866f26aa05SWill Deacon static void watchpoint_handler(unsigned long addr, unsigned int fsr, 6876f26aa05SWill Deacon struct pt_regs *regs) 688f81ef4a9SWill Deacon { 6896f26aa05SWill Deacon int i, access; 6906f26aa05SWill Deacon u32 val, ctrl_reg, alignment_mask; 6914a55c18eSWill Deacon struct perf_event *wp, **slots; 692f81ef4a9SWill Deacon struct arch_hw_breakpoint *info; 6936f26aa05SWill Deacon struct arch_hw_breakpoint_ctrl ctrl; 694f81ef4a9SWill Deacon 6954a55c18eSWill Deacon slots = (struct perf_event **)__get_cpu_var(wp_on_reg); 6964a55c18eSWill Deacon 697f81ef4a9SWill Deacon for (i = 0; i < core_num_wrps; ++i) { 698f81ef4a9SWill Deacon rcu_read_lock(); 699f81ef4a9SWill Deacon 70093a04a34SWill Deacon wp = slots[i]; 70193a04a34SWill Deacon 7026f26aa05SWill Deacon if (wp == NULL) 7036f26aa05SWill Deacon goto unlock; 7046f26aa05SWill Deacon 7056f26aa05SWill Deacon info = counter_arch_bp(wp); 7066f26aa05SWill Deacon /* 7076f26aa05SWill Deacon * The DFAR is an unknown value on debug architectures prior 7086f26aa05SWill Deacon * to 7.1. Since we only allow a single watchpoint on these 7096f26aa05SWill Deacon * older CPUs, we can set the trigger to the lowest possible 7106f26aa05SWill Deacon * faulting address. 7116f26aa05SWill Deacon */ 7126f26aa05SWill Deacon if (debug_arch < ARM_DEBUG_ARCH_V7_1) { 7136f26aa05SWill Deacon BUG_ON(i > 0); 7146f26aa05SWill Deacon info->trigger = wp->attr.bp_addr; 7156f26aa05SWill Deacon } else { 7166f26aa05SWill Deacon if (info->ctrl.len == ARM_BREAKPOINT_LEN_8) 7176f26aa05SWill Deacon alignment_mask = 0x7; 7186f26aa05SWill Deacon else 7196f26aa05SWill Deacon alignment_mask = 0x3; 7206f26aa05SWill Deacon 7216f26aa05SWill Deacon /* Check if the watchpoint value matches. */ 7226f26aa05SWill Deacon val = read_wb_reg(ARM_BASE_WVR + i); 7236f26aa05SWill Deacon if (val != (addr & ~alignment_mask)) 7246f26aa05SWill Deacon goto unlock; 7256f26aa05SWill Deacon 7266f26aa05SWill Deacon /* Possible match, check the byte address select. */ 7276f26aa05SWill Deacon ctrl_reg = read_wb_reg(ARM_BASE_WCR + i); 7286f26aa05SWill Deacon decode_ctrl_reg(ctrl_reg, &ctrl); 7296f26aa05SWill Deacon if (!((1 << (addr & alignment_mask)) & ctrl.len)) 7306f26aa05SWill Deacon goto unlock; 7316f26aa05SWill Deacon 7326f26aa05SWill Deacon /* Check that the access type matches. */ 733bf880114SWill Deacon if (debug_exception_updates_fsr()) { 734bf880114SWill Deacon access = (fsr & ARM_FSR_ACCESS_MASK) ? 735bf880114SWill Deacon HW_BREAKPOINT_W : HW_BREAKPOINT_R; 7366f26aa05SWill Deacon if (!(access & hw_breakpoint_type(wp))) 7376f26aa05SWill Deacon goto unlock; 738bf880114SWill Deacon } 7396f26aa05SWill Deacon 7406f26aa05SWill Deacon /* We have a winner. */ 7416f26aa05SWill Deacon info->trigger = addr; 742f81ef4a9SWill Deacon } 743f81ef4a9SWill Deacon 744f81ef4a9SWill Deacon pr_debug("watchpoint fired: address = 0x%x\n", info->trigger); 74593a04a34SWill Deacon perf_bp_event(wp, regs); 746f81ef4a9SWill Deacon 747f81ef4a9SWill Deacon /* 748f81ef4a9SWill Deacon * If no overflow handler is present, insert a temporary 749f81ef4a9SWill Deacon * mismatch breakpoint so we can single-step over the 750f81ef4a9SWill Deacon * watchpoint trigger. 751f81ef4a9SWill Deacon */ 7529ebb3cbcSWill Deacon if (!wp->overflow_handler) 7539ebb3cbcSWill Deacon enable_single_step(wp, instruction_pointer(regs)); 754f81ef4a9SWill Deacon 7556f26aa05SWill Deacon unlock: 756f81ef4a9SWill Deacon rcu_read_unlock(); 757f81ef4a9SWill Deacon } 758f81ef4a9SWill Deacon } 759f81ef4a9SWill Deacon 76093a04a34SWill Deacon static void watchpoint_single_step_handler(unsigned long pc) 76193a04a34SWill Deacon { 76293a04a34SWill Deacon int i; 7634a55c18eSWill Deacon struct perf_event *wp, **slots; 76493a04a34SWill Deacon struct arch_hw_breakpoint *info; 76593a04a34SWill Deacon 7664a55c18eSWill Deacon slots = (struct perf_event **)__get_cpu_var(wp_on_reg); 7674a55c18eSWill Deacon 768c512de95SWill Deacon for (i = 0; i < core_num_wrps; ++i) { 76993a04a34SWill Deacon rcu_read_lock(); 77093a04a34SWill Deacon 77193a04a34SWill Deacon wp = slots[i]; 77293a04a34SWill Deacon 77393a04a34SWill Deacon if (wp == NULL) 77493a04a34SWill Deacon goto unlock; 77593a04a34SWill Deacon 77693a04a34SWill Deacon info = counter_arch_bp(wp); 77793a04a34SWill Deacon if (!info->step_ctrl.enabled) 77893a04a34SWill Deacon goto unlock; 77993a04a34SWill Deacon 78093a04a34SWill Deacon /* 78193a04a34SWill Deacon * Restore the original watchpoint if we've completed the 78293a04a34SWill Deacon * single-step. 78393a04a34SWill Deacon */ 7849ebb3cbcSWill Deacon if (info->trigger != pc) 7859ebb3cbcSWill Deacon disable_single_step(wp); 78693a04a34SWill Deacon 78793a04a34SWill Deacon unlock: 78893a04a34SWill Deacon rcu_read_unlock(); 78993a04a34SWill Deacon } 79093a04a34SWill Deacon } 79193a04a34SWill Deacon 792f81ef4a9SWill Deacon static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs) 793f81ef4a9SWill Deacon { 794f81ef4a9SWill Deacon int i; 795f81ef4a9SWill Deacon u32 ctrl_reg, val, addr; 7964a55c18eSWill Deacon struct perf_event *bp, **slots; 797f81ef4a9SWill Deacon struct arch_hw_breakpoint *info; 798f81ef4a9SWill Deacon struct arch_hw_breakpoint_ctrl ctrl; 799f81ef4a9SWill Deacon 8004a55c18eSWill Deacon slots = (struct perf_event **)__get_cpu_var(bp_on_reg); 8014a55c18eSWill Deacon 802f81ef4a9SWill Deacon /* The exception entry code places the amended lr in the PC. */ 803f81ef4a9SWill Deacon addr = regs->ARM_pc; 804f81ef4a9SWill Deacon 80593a04a34SWill Deacon /* Check the currently installed breakpoints first. */ 80693a04a34SWill Deacon for (i = 0; i < core_num_brps; ++i) { 807f81ef4a9SWill Deacon rcu_read_lock(); 808f81ef4a9SWill Deacon 809f81ef4a9SWill Deacon bp = slots[i]; 810f81ef4a9SWill Deacon 8119ebb3cbcSWill Deacon if (bp == NULL) 8129ebb3cbcSWill Deacon goto unlock; 813f81ef4a9SWill Deacon 8149ebb3cbcSWill Deacon info = counter_arch_bp(bp); 815f81ef4a9SWill Deacon 816f81ef4a9SWill Deacon /* Check if the breakpoint value matches. */ 817f81ef4a9SWill Deacon val = read_wb_reg(ARM_BASE_BVR + i); 818f81ef4a9SWill Deacon if (val != (addr & ~0x3)) 8199ebb3cbcSWill Deacon goto mismatch; 820f81ef4a9SWill Deacon 821f81ef4a9SWill Deacon /* Possible match, check the byte address select to confirm. */ 822f81ef4a9SWill Deacon ctrl_reg = read_wb_reg(ARM_BASE_BCR + i); 823f81ef4a9SWill Deacon decode_ctrl_reg(ctrl_reg, &ctrl); 824f81ef4a9SWill Deacon if ((1 << (addr & 0x3)) & ctrl.len) { 825f81ef4a9SWill Deacon info->trigger = addr; 826f81ef4a9SWill Deacon pr_debug("breakpoint fired: address = 0x%x\n", addr); 827f81ef4a9SWill Deacon perf_bp_event(bp, regs); 8289ebb3cbcSWill Deacon if (!bp->overflow_handler) 8299ebb3cbcSWill Deacon enable_single_step(bp, addr); 8309ebb3cbcSWill Deacon goto unlock; 831f81ef4a9SWill Deacon } 832f81ef4a9SWill Deacon 8339ebb3cbcSWill Deacon mismatch: 8349ebb3cbcSWill Deacon /* If we're stepping a breakpoint, it can now be restored. */ 8359ebb3cbcSWill Deacon if (info->step_ctrl.enabled) 8369ebb3cbcSWill Deacon disable_single_step(bp); 8379ebb3cbcSWill Deacon unlock: 838f81ef4a9SWill Deacon rcu_read_unlock(); 839f81ef4a9SWill Deacon } 84093a04a34SWill Deacon 84193a04a34SWill Deacon /* Handle any pending watchpoint single-step breakpoints. */ 84293a04a34SWill Deacon watchpoint_single_step_handler(addr); 843f81ef4a9SWill Deacon } 844f81ef4a9SWill Deacon 845f81ef4a9SWill Deacon /* 846f81ef4a9SWill Deacon * Called from either the Data Abort Handler [watchpoint] or the 84702fe2845SRussell King * Prefetch Abort Handler [breakpoint] with interrupts disabled. 848f81ef4a9SWill Deacon */ 849f81ef4a9SWill Deacon static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr, 850f81ef4a9SWill Deacon struct pt_regs *regs) 851f81ef4a9SWill Deacon { 8527e202696SWill Deacon int ret = 0; 853f81ef4a9SWill Deacon u32 dscr; 854f81ef4a9SWill Deacon 85502fe2845SRussell King preempt_disable(); 85602fe2845SRussell King 85702fe2845SRussell King if (interrupts_enabled(regs)) 85802fe2845SRussell King local_irq_enable(); 8597e202696SWill Deacon 860f81ef4a9SWill Deacon /* We only handle watchpoints and hardware breakpoints. */ 8619e962f76SDietmar Eggemann ARM_DBG_READ(c0, c1, 0, dscr); 862f81ef4a9SWill Deacon 863f81ef4a9SWill Deacon /* Perform perf callbacks. */ 864f81ef4a9SWill Deacon switch (ARM_DSCR_MOE(dscr)) { 865f81ef4a9SWill Deacon case ARM_ENTRY_BREAKPOINT: 866f81ef4a9SWill Deacon breakpoint_handler(addr, regs); 867f81ef4a9SWill Deacon break; 868f81ef4a9SWill Deacon case ARM_ENTRY_ASYNC_WATCHPOINT: 869235584b6SJoe Perches WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n"); 870f81ef4a9SWill Deacon case ARM_ENTRY_SYNC_WATCHPOINT: 8716f26aa05SWill Deacon watchpoint_handler(addr, fsr, regs); 872f81ef4a9SWill Deacon break; 873f81ef4a9SWill Deacon default: 8747e202696SWill Deacon ret = 1; /* Unhandled fault. */ 875f81ef4a9SWill Deacon } 876f81ef4a9SWill Deacon 8777e202696SWill Deacon preempt_enable(); 8787e202696SWill Deacon 879f81ef4a9SWill Deacon return ret; 880f81ef4a9SWill Deacon } 881f81ef4a9SWill Deacon 882f81ef4a9SWill Deacon /* 883f81ef4a9SWill Deacon * One-time initialisation. 884f81ef4a9SWill Deacon */ 8850d352e3dSWill Deacon static cpumask_t debug_err_mask; 8860d352e3dSWill Deacon 8870d352e3dSWill Deacon static int debug_reg_trap(struct pt_regs *regs, unsigned int instr) 8880d352e3dSWill Deacon { 8890d352e3dSWill Deacon int cpu = smp_processor_id(); 8900d352e3dSWill Deacon 8910d352e3dSWill Deacon pr_warning("Debug register access (0x%x) caused undefined instruction on CPU %d\n", 8920d352e3dSWill Deacon instr, cpu); 8930d352e3dSWill Deacon 8940d352e3dSWill Deacon /* Set the error flag for this CPU and skip the faulting instruction. */ 8950d352e3dSWill Deacon cpumask_set_cpu(cpu, &debug_err_mask); 8960d352e3dSWill Deacon instruction_pointer(regs) += 4; 8970d352e3dSWill Deacon return 0; 8980d352e3dSWill Deacon } 8990d352e3dSWill Deacon 9000d352e3dSWill Deacon static struct undef_hook debug_reg_hook = { 9010d352e3dSWill Deacon .instr_mask = 0x0fe80f10, 9020d352e3dSWill Deacon .instr_val = 0x0e000e10, 9030d352e3dSWill Deacon .fn = debug_reg_trap, 9040d352e3dSWill Deacon }; 9050d352e3dSWill Deacon 9060d352e3dSWill Deacon static void reset_ctrl_regs(void *unused) 907f81ef4a9SWill Deacon { 908c512de95SWill Deacon int i, raw_num_brps, err = 0, cpu = smp_processor_id(); 909e64877dcSWill Deacon u32 val; 910f81ef4a9SWill Deacon 911ac88e071SWill Deacon /* 912ac88e071SWill Deacon * v7 debug contains save and restore registers so that debug state 913ed19b739SWill Deacon * can be maintained across low-power modes without leaving the debug 914ed19b739SWill Deacon * logic powered up. It is IMPLEMENTATION DEFINED whether we can access 915ed19b739SWill Deacon * the debug registers out of reset, so we must unlock the OS Lock 916ed19b739SWill Deacon * Access Register to avoid taking undefined instruction exceptions 917ed19b739SWill Deacon * later on. 918ac88e071SWill Deacon */ 919b5d5b8f9SWill Deacon switch (debug_arch) { 920a26bce12SWill Deacon case ARM_DEBUG_ARCH_V6: 921a26bce12SWill Deacon case ARM_DEBUG_ARCH_V6_1: 9227f4050a0SWill Deacon /* ARMv6 cores clear the registers out of reset. */ 9237f4050a0SWill Deacon goto out_mdbgen; 924b5d5b8f9SWill Deacon case ARM_DEBUG_ARCH_V7_ECP14: 925ac88e071SWill Deacon /* 926c09bae70SWill Deacon * Ensure sticky power-down is clear (i.e. debug logic is 927c09bae70SWill Deacon * powered up). 928c09bae70SWill Deacon */ 9299e962f76SDietmar Eggemann ARM_DBG_READ(c1, c5, 4, val); 930e64877dcSWill Deacon if ((val & 0x1) == 0) 931b5d5b8f9SWill Deacon err = -EPERM; 932e64877dcSWill Deacon 933e64877dcSWill Deacon /* 934e64877dcSWill Deacon * Check whether we implement OS save and restore. 935e64877dcSWill Deacon */ 9369e962f76SDietmar Eggemann ARM_DBG_READ(c1, c1, 4, val); 937e64877dcSWill Deacon if ((val & 0x9) == 0) 938e64877dcSWill Deacon goto clear_vcr; 939b5d5b8f9SWill Deacon break; 940b5d5b8f9SWill Deacon case ARM_DEBUG_ARCH_V7_1: 941b5d5b8f9SWill Deacon /* 942b5d5b8f9SWill Deacon * Ensure the OS double lock is clear. 943b5d5b8f9SWill Deacon */ 9449e962f76SDietmar Eggemann ARM_DBG_READ(c1, c3, 4, val); 945e64877dcSWill Deacon if ((val & 0x1) == 1) 946b5d5b8f9SWill Deacon err = -EPERM; 947b5d5b8f9SWill Deacon break; 948b5d5b8f9SWill Deacon } 949b5d5b8f9SWill Deacon 950b5d5b8f9SWill Deacon if (err) { 951c09bae70SWill Deacon pr_warning("CPU %d debug is powered down!\n", cpu); 9520d352e3dSWill Deacon cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu)); 953c09bae70SWill Deacon return; 954c09bae70SWill Deacon } 955c09bae70SWill Deacon 956c09bae70SWill Deacon /* 957e64877dcSWill Deacon * Unconditionally clear the OS lock by writing a value 958ac88e071SWill Deacon * other than 0xC5ACCE55 to the access register. 959ac88e071SWill Deacon */ 9609e962f76SDietmar Eggemann ARM_DBG_WRITE(c1, c0, 4, 0); 961ac88e071SWill Deacon isb(); 962e89c0d70SWill Deacon 963e89c0d70SWill Deacon /* 964e89c0d70SWill Deacon * Clear any configured vector-catch events before 965e89c0d70SWill Deacon * enabling monitor mode. 966e89c0d70SWill Deacon */ 967e64877dcSWill Deacon clear_vcr: 9689e962f76SDietmar Eggemann ARM_DBG_WRITE(c0, c7, 0, 0); 969e89c0d70SWill Deacon isb(); 970ac88e071SWill Deacon 971614bea50SWill Deacon if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) { 972614bea50SWill Deacon pr_warning("CPU %d failed to disable vector catch\n", cpu); 973f81ef4a9SWill Deacon return; 974614bea50SWill Deacon } 975f81ef4a9SWill Deacon 976614bea50SWill Deacon /* 977614bea50SWill Deacon * The control/value register pairs are UNKNOWN out of reset so 978614bea50SWill Deacon * clear them to avoid spurious debug events. 979614bea50SWill Deacon */ 980c512de95SWill Deacon raw_num_brps = get_num_brp_resources(); 981c512de95SWill Deacon for (i = 0; i < raw_num_brps; ++i) { 982f81ef4a9SWill Deacon write_wb_reg(ARM_BASE_BCR + i, 0UL); 983f81ef4a9SWill Deacon write_wb_reg(ARM_BASE_BVR + i, 0UL); 984f81ef4a9SWill Deacon } 985f81ef4a9SWill Deacon 986f81ef4a9SWill Deacon for (i = 0; i < core_num_wrps; ++i) { 987f81ef4a9SWill Deacon write_wb_reg(ARM_BASE_WCR + i, 0UL); 988f81ef4a9SWill Deacon write_wb_reg(ARM_BASE_WVR + i, 0UL); 989f81ef4a9SWill Deacon } 990614bea50SWill Deacon 991614bea50SWill Deacon if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) { 992614bea50SWill Deacon pr_warning("CPU %d failed to clear debug register pairs\n", cpu); 993614bea50SWill Deacon return; 994614bea50SWill Deacon } 995614bea50SWill Deacon 996614bea50SWill Deacon /* 997614bea50SWill Deacon * Have a crack at enabling monitor mode. We don't actually need 998614bea50SWill Deacon * it yet, but reporting an error early is useful if it fails. 999614bea50SWill Deacon */ 10007f4050a0SWill Deacon out_mdbgen: 1001614bea50SWill Deacon if (enable_monitor_mode()) 1002614bea50SWill Deacon cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu)); 1003f81ef4a9SWill Deacon } 1004f81ef4a9SWill Deacon 10057d99331eSWill Deacon static int __cpuinit dbg_reset_notify(struct notifier_block *self, 10067d99331eSWill Deacon unsigned long action, void *cpu) 10077d99331eSWill Deacon { 10087d99331eSWill Deacon if (action == CPU_ONLINE) 10097d99331eSWill Deacon smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1); 10100d352e3dSWill Deacon 10117d99331eSWill Deacon return NOTIFY_OK; 10127d99331eSWill Deacon } 10137d99331eSWill Deacon 10147d99331eSWill Deacon static struct notifier_block __cpuinitdata dbg_reset_nb = { 10157d99331eSWill Deacon .notifier_call = dbg_reset_notify, 10167d99331eSWill Deacon }; 10177d99331eSWill Deacon 1018f81ef4a9SWill Deacon static int __init arch_hw_breakpoint_init(void) 1019f81ef4a9SWill Deacon { 1020f81ef4a9SWill Deacon debug_arch = get_debug_arch(); 1021f81ef4a9SWill Deacon 102266e1cfe6SWill Deacon if (!debug_arch_supported()) { 1023f81ef4a9SWill Deacon pr_info("debug architecture 0x%x unsupported.\n", debug_arch); 10248fbf397cSWill Deacon return 0; 1025f81ef4a9SWill Deacon } 1026f81ef4a9SWill Deacon 1027f81ef4a9SWill Deacon /* Determine how many BRPs/WRPs are available. */ 1028f81ef4a9SWill Deacon core_num_brps = get_num_brps(); 1029f81ef4a9SWill Deacon core_num_wrps = get_num_wrps(); 1030f81ef4a9SWill Deacon 10310d352e3dSWill Deacon /* 10320d352e3dSWill Deacon * We need to tread carefully here because DBGSWENABLE may be 10330d352e3dSWill Deacon * driven low on this core and there isn't an architected way to 10340d352e3dSWill Deacon * determine that. 10350d352e3dSWill Deacon */ 10360d352e3dSWill Deacon register_undef_hook(&debug_reg_hook); 1037f81ef4a9SWill Deacon 1038f81ef4a9SWill Deacon /* 1039f81ef4a9SWill Deacon * Reset the breakpoint resources. We assume that a halting 1040f81ef4a9SWill Deacon * debugger will leave the world in a nice state for us. 1041f81ef4a9SWill Deacon */ 10420d352e3dSWill Deacon on_each_cpu(reset_ctrl_regs, NULL, 1); 10430d352e3dSWill Deacon unregister_undef_hook(&debug_reg_hook); 10440d352e3dSWill Deacon if (!cpumask_empty(&debug_err_mask)) { 1045c09bae70SWill Deacon core_num_brps = 0; 1046c09bae70SWill Deacon core_num_wrps = 0; 1047c09bae70SWill Deacon return 0; 1048c09bae70SWill Deacon } 1049ac88e071SWill Deacon 10500d352e3dSWill Deacon pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n", 10510d352e3dSWill Deacon core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " : 10520d352e3dSWill Deacon "", core_num_wrps); 10530d352e3dSWill Deacon 1054ac88e071SWill Deacon /* Work out the maximum supported watchpoint length. */ 1055ac88e071SWill Deacon max_watchpoint_len = get_max_wp_len(); 1056ac88e071SWill Deacon pr_info("maximum watchpoint size is %u bytes.\n", 1057ac88e071SWill Deacon max_watchpoint_len); 1058f81ef4a9SWill Deacon 1059f81ef4a9SWill Deacon /* Register debug fault handler. */ 1060f7b8156dSCatalin Marinas hook_fault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP, 1061f7b8156dSCatalin Marinas TRAP_HWBKPT, "watchpoint debug exception"); 1062f7b8156dSCatalin Marinas hook_ifault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP, 1063f7b8156dSCatalin Marinas TRAP_HWBKPT, "breakpoint debug exception"); 1064f81ef4a9SWill Deacon 10657d99331eSWill Deacon /* Register hotplug notifier. */ 10667d99331eSWill Deacon register_cpu_notifier(&dbg_reset_nb); 10678fbf397cSWill Deacon return 0; 1068f81ef4a9SWill Deacon } 1069f81ef4a9SWill Deacon arch_initcall(arch_hw_breakpoint_init); 1070f81ef4a9SWill Deacon 1071f81ef4a9SWill Deacon void hw_breakpoint_pmu_read(struct perf_event *bp) 1072f81ef4a9SWill Deacon { 1073f81ef4a9SWill Deacon } 1074f81ef4a9SWill Deacon 1075f81ef4a9SWill Deacon /* 1076f81ef4a9SWill Deacon * Dummy function to register with die_notifier. 1077f81ef4a9SWill Deacon */ 1078f81ef4a9SWill Deacon int hw_breakpoint_exceptions_notify(struct notifier_block *unused, 1079f81ef4a9SWill Deacon unsigned long val, void *data) 1080f81ef4a9SWill Deacon { 1081f81ef4a9SWill Deacon return NOTIFY_DONE; 1082f81ef4a9SWill Deacon } 1083