109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt/* 209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * arch/sh/kernel/hw_breakpoint.c 309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * 409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Unified kernel/user-space hardware breakpoint facility for the on-chip UBC. 509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * 64352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * Copyright (C) 2009 - 2010 Paul Mundt 709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * 809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * This file is subject to the terms and conditions of the GNU General Public 909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * License. See the file "COPYING" in the main directory of this archive 1009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * for more details. 1109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 1209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/init.h> 1309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/perf_event.h> 1409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/hw_breakpoint.h> 1509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/percpu.h> 1609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/kallsyms.h> 1709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/notifier.h> 1809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/kprobes.h> 1909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/kdebug.h> 2009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <linux/io.h> 214352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt#include <linux/clk.h> 2209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <asm/hw_breakpoint.h> 2309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt#include <asm/mmu_context.h> 2434d0b5af50a063cded842716633501b38ff815fbPaul Mundt#include <asm/ptrace.h> 25e839ca528718e68cad32a307dc9aabf01ef3eb05David Howells#include <asm/traps.h> 2609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 2709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt/* 2809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Stores the breakpoints currently in use on each breakpoint address 2909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * register for each cpus 3009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 3109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtstatic DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]); 3209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 334352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt/* 344352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * A dummy placeholder for early accesses until the CPUs get a chance to 354352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * register their UBCs later in the boot process. 364352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt */ 374352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundtstatic struct sh_ubc ubc_dummy = { .num_events = 0 }; 3809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 394352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundtstatic struct sh_ubc *sh_ubc __read_mostly = &ubc_dummy; 4009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 4109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt/* 4209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Install a perf counter breakpoint. 4309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * 4409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * We seek a free UBC channel and use it for this breakpoint. 4509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * 4609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Atomic: we hold the counter->ctx->lock and we only handle variables 4709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * and registers local to this cpu. 4809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 4909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtint arch_install_hw_breakpoint(struct perf_event *bp) 5009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 5109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt struct arch_hw_breakpoint *info = counter_arch_bp(bp); 5209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt int i; 5309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 544352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt for (i = 0; i < sh_ubc->num_events; i++) { 55c473b2c6f6c6d012da98416b5de28cc48c4306c9Christoph Lameter struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]); 5609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 5709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt if (!*slot) { 5809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *slot = bp; 5909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 6009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 6109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 6209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 634352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (WARN_ONCE(i == sh_ubc->num_events, "Can't find any breakpoint slot")) 6409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return -EBUSY; 6509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 664352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt clk_enable(sh_ubc->clk); 674352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt sh_ubc->enable(info, i); 6809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 6909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return 0; 7009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 7109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 7209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt/* 7309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Uninstall the breakpoint contained in the given counter. 7409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * 7509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * First we search the debug address register it uses and then we disable 7609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * it. 7709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * 7809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Atomic: we hold the counter->ctx->lock and we only handle variables 7909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * and registers local to this cpu. 8009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 8109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtvoid arch_uninstall_hw_breakpoint(struct perf_event *bp) 8209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 8309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt struct arch_hw_breakpoint *info = counter_arch_bp(bp); 8409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt int i; 8509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 864352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt for (i = 0; i < sh_ubc->num_events; i++) { 87c473b2c6f6c6d012da98416b5de28cc48c4306c9Christoph Lameter struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]); 8809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 8909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt if (*slot == bp) { 9009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *slot = NULL; 9109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 9209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 9309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 9409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 954352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (WARN_ONCE(i == sh_ubc->num_events, "Can't find any breakpoint slot")) 9609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return; 9709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 984352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt sh_ubc->disable(info, i); 994352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt clk_disable(sh_ubc->clk); 10009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 10109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 10209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtstatic int get_hbp_len(u16 hbp_len) 10309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 10409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt unsigned int len_in_bytes = 0; 10509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 10609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt switch (hbp_len) { 10709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_1: 10809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt len_in_bytes = 1; 10909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 11009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_2: 11109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt len_in_bytes = 2; 11209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 11309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_4: 11409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt len_in_bytes = 4; 11509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 11609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_8: 11709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt len_in_bytes = 8; 11809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 11909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 12009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return len_in_bytes; 12109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 12209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 12309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt/* 12409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Check for virtual address in kernel space. 12509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 126b2812d031dea86926e9c10f7714af33ac2f6b43dFrederic Weisbeckerint arch_check_bp_in_kernelspace(struct perf_event *bp) 12709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 12809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt unsigned int len; 129b2812d031dea86926e9c10f7714af33ac2f6b43dFrederic Weisbecker unsigned long va; 130b2812d031dea86926e9c10f7714af33ac2f6b43dFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 13109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 132b2812d031dea86926e9c10f7714af33ac2f6b43dFrederic Weisbecker va = info->address; 133b2812d031dea86926e9c10f7714af33ac2f6b43dFrederic Weisbecker len = get_hbp_len(info->len); 13409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 13509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); 13609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 13709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 13809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtint arch_bp_generic_fields(int sh_len, int sh_type, 13909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt int *gen_len, int *gen_type) 14009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 14109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt /* Len */ 14209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt switch (sh_len) { 14309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_1: 14409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *gen_len = HW_BREAKPOINT_LEN_1; 14509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 14609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_2: 14709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *gen_len = HW_BREAKPOINT_LEN_2; 14809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 14909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_4: 15009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *gen_len = HW_BREAKPOINT_LEN_4; 15109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 15209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_8: 15309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *gen_len = HW_BREAKPOINT_LEN_8; 15409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 15509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt default: 15609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return -EINVAL; 15709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 15809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 15909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt /* Type */ 16009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt switch (sh_type) { 16109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_READ: 16209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *gen_type = HW_BREAKPOINT_R; 16309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_WRITE: 16409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *gen_type = HW_BREAKPOINT_W; 16509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 16609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_RW: 16709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R; 16809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 16909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt default: 17009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return -EINVAL; 17109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 17209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 17309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return 0; 17409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 17509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 17609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtstatic int arch_build_bp_info(struct perf_event *bp) 17709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 17809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt struct arch_hw_breakpoint *info = counter_arch_bp(bp); 17909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 18009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt info->address = bp->attr.bp_addr; 18109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 18209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt /* Len */ 18309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt switch (bp->attr.bp_len) { 18409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case HW_BREAKPOINT_LEN_1: 18509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt info->len = SH_BREAKPOINT_LEN_1; 18609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 18709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case HW_BREAKPOINT_LEN_2: 18809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt info->len = SH_BREAKPOINT_LEN_2; 18909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 19009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case HW_BREAKPOINT_LEN_4: 19109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt info->len = SH_BREAKPOINT_LEN_4; 19209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 19309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case HW_BREAKPOINT_LEN_8: 19409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt info->len = SH_BREAKPOINT_LEN_8; 19509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 19609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt default: 19709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return -EINVAL; 19809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 19909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 20009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt /* Type */ 20109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt switch (bp->attr.bp_type) { 20209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case HW_BREAKPOINT_R: 20309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt info->type = SH_BREAKPOINT_READ; 20409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 20509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case HW_BREAKPOINT_W: 20609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt info->type = SH_BREAKPOINT_WRITE; 20709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 20809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case HW_BREAKPOINT_W | HW_BREAKPOINT_R: 20909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt info->type = SH_BREAKPOINT_RW; 21009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 21109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt default: 21209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return -EINVAL; 21309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 21409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 21509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return 0; 21609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 21709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 21809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt/* 21909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Validate the arch-specific HW Breakpoint register settings 22009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 221b2812d031dea86926e9c10f7714af33ac2f6b43dFrederic Weisbeckerint arch_validate_hwbkpt_settings(struct perf_event *bp) 22209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 22309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt struct arch_hw_breakpoint *info = counter_arch_bp(bp); 22409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt unsigned int align; 22509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt int ret; 22609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 22709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt ret = arch_build_bp_info(bp); 22809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt if (ret) 22909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return ret; 23009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 23109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt ret = -EINVAL; 23209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 23309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt switch (info->len) { 23409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_1: 23509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt align = 0; 23609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 23709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_2: 23809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt align = 1; 23909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 24009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_4: 24109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt align = 3; 24209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 24309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt case SH_BREAKPOINT_LEN_8: 24409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt align = 7; 24509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 24609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt default: 24709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return ret; 24809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 24909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 250105244ec95590f5f12a90d974650ab5c7bc8ec79Paul Mundt /* 251105244ec95590f5f12a90d974650ab5c7bc8ec79Paul Mundt * For kernel-addresses, either the address or symbol name can be 252105244ec95590f5f12a90d974650ab5c7bc8ec79Paul Mundt * specified. 253105244ec95590f5f12a90d974650ab5c7bc8ec79Paul Mundt */ 254105244ec95590f5f12a90d974650ab5c7bc8ec79Paul Mundt if (info->name) 255105244ec95590f5f12a90d974650ab5c7bc8ec79Paul Mundt info->address = (unsigned long)kallsyms_lookup_name(info->name); 25609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 25709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt /* 25809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Check that the low-order bits of the address are appropriate 25909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * for the alignment implied by len. 26009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 26109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt if (info->address & align) 26209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return -EINVAL; 26309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 26409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return 0; 26509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 26609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 26709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt/* 26809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Release the user breakpoints used by ptrace 26909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 27009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtvoid flush_ptrace_hw_breakpoint(struct task_struct *tsk) 27109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 27209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt int i; 27309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt struct thread_struct *t = &tsk->thread; 27409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 2754352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt for (i = 0; i < sh_ubc->num_events; i++) { 27609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt unregister_hw_breakpoint(t->ptrace_bps[i]); 27709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt t->ptrace_bps[i] = NULL; 27809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 27909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 28009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 28109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtstatic int __kprobes hw_breakpoint_handler(struct die_args *args) 28209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 28309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt int cpu, i, rc = NOTIFY_STOP; 28409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt struct perf_event *bp; 2854352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt unsigned int cmf, resume_mask; 2864352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 2874352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt /* 2884352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * Do an early return if none of the channels triggered. 2894352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt */ 2904352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt cmf = sh_ubc->triggered_mask(); 2914352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (unlikely(!cmf)) 2924352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt return NOTIFY_DONE; 2934352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 2944352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt /* 2954352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * By default, resume all of the active channels. 2964352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt */ 2974352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt resume_mask = sh_ubc->active_mask(); 29809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 2994352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt /* 3004352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * Disable breakpoints during exception handling. 3014352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt */ 3024352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt sh_ubc->disable_all(); 30309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 30409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt cpu = get_cpu(); 3054352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt for (i = 0; i < sh_ubc->num_events; i++) { 3064352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt unsigned long event_mask = (1 << i); 3074352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 3084352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (likely(!(cmf & event_mask))) 3094352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt continue; 3104352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 31109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt /* 31209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * The counter may be concurrently released but that can only 31309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * occur from a call_rcu() path. We can then safely fetch 31409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * the breakpoint, use its callback, touch its counter 31509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * while we are in an rcu_read_lock() path. 31609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 31709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt rcu_read_lock(); 31809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 31909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt bp = per_cpu(bp_per_reg[i], cpu); 3204352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (bp) 32109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt rc = NOTIFY_DONE; 3224352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 3234352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt /* 3244352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * Reset the condition match flag to denote completion of 3254352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * exception handling. 3264352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt */ 3274352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt sh_ubc->clear_triggered_mask(event_mask); 3284352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 3294352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt /* 3304352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * bp can be NULL due to concurrent perf counter 3314352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * removing. 3324352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt */ 3334352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (!bp) { 33409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt rcu_read_unlock(); 33509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt break; 33609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 33709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 3384352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt /* 3394352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * Don't restore the channel if the breakpoint is from 3404352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * ptrace, as it always operates in one-shot mode. 3414352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt */ 3424352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (bp->overflow_handler == ptrace_triggered) 3434352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt resume_mask &= ~(1 << i); 3444352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 345a28b460ec7968f430a2d6ea2809b249ee9fe1d28Paul Mundt perf_bp_event(bp, args->regs); 34609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 3474352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt /* Deliver the signal to userspace */ 348b2812d031dea86926e9c10f7714af33ac2f6b43dFrederic Weisbecker if (!arch_check_bp_in_kernelspace(bp)) { 3494352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt siginfo_t info; 3504352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 3514352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt info.si_signo = args->signr; 3524352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt info.si_errno = notifier_to_errno(rc); 3534352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt info.si_code = TRAP_HWBKPT; 3544352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 3554352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt force_sig_info(args->signr, &info, current); 3564352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt } 3574352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 35809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt rcu_read_unlock(); 35909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt } 36009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 3614352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (cmf == 0) 3624352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt rc = NOTIFY_DONE; 36309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 3644352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt sh_ubc->enable_all(resume_mask); 36509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 36609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt put_cpu(); 36709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 36809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return rc; 36909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 37009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 37109a072947791088b88ae15111cf68fc5aaaf758dPaul MundtBUILD_TRAP_HANDLER(breakpoint) 37209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 37309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt unsigned long ex = lookup_exception_vector(); 37409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt TRAP_HANDLER_DECL; 37509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 3764352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt notify_die(DIE_BREAKPOINT, "breakpoint", regs, 0, ex, SIGTRAP); 37709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 37809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 37909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt/* 38009a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt * Handle debug exception notifications. 38109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt */ 38209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtint __kprobes hw_breakpoint_exceptions_notify(struct notifier_block *unused, 38309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt unsigned long val, void *data) 38409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 385b74ab703b1326aafadb5604ff6200da62c61df49Paul Mundt struct die_args *args = data; 386b74ab703b1326aafadb5604ff6200da62c61df49Paul Mundt 38709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt if (val != DIE_BREAKPOINT) 38809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return NOTIFY_DONE; 38909a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 390b74ab703b1326aafadb5604ff6200da62c61df49Paul Mundt /* 391b74ab703b1326aafadb5604ff6200da62c61df49Paul Mundt * If the breakpoint hasn't been triggered by the UBC, it's 392b74ab703b1326aafadb5604ff6200da62c61df49Paul Mundt * probably from a debugger, so don't do anything more here. 3934352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * 3944352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * This also permits the UBC interface clock to remain off for 3954352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * non-UBC breakpoints, as we don't need to check the triggered 3964352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt * or active channel masks. 397b74ab703b1326aafadb5604ff6200da62c61df49Paul Mundt */ 3984352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (args->trapnr != sh_ubc->trap_nr) 399b74ab703b1326aafadb5604ff6200da62c61df49Paul Mundt return NOTIFY_DONE; 400b74ab703b1326aafadb5604ff6200da62c61df49Paul Mundt 40109a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt return hw_breakpoint_handler(data); 40209a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 40309a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 40409a072947791088b88ae15111cf68fc5aaaf758dPaul Mundtvoid hw_breakpoint_pmu_read(struct perf_event *bp) 40509a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt{ 40609a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt /* TODO */ 40709a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt} 40809a072947791088b88ae15111cf68fc5aaaf758dPaul Mundt 4094352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundtint register_sh_ubc(struct sh_ubc *ubc) 4104352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt{ 4114352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt /* Bail if it's already assigned */ 4124352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt if (sh_ubc != &ubc_dummy) 4134352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt return -EBUSY; 4144352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt sh_ubc = ubc; 4154352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 4164352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt pr_info("HW Breakpoints: %s UBC support registered\n", ubc->name); 4174352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 4184352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt WARN_ON(ubc->num_events > HBP_NUM); 4194352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt 4204352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt return 0; 4214352fc1b12fae4c753a063a2f162ddf9277af774Paul Mundt} 422