10067f129SK.Prasad /* 20067f129SK.Prasad * This program is free software; you can redistribute it and/or modify 30067f129SK.Prasad * it under the terms of the GNU General Public License as published by 40067f129SK.Prasad * the Free Software Foundation; either version 2 of the License, or 50067f129SK.Prasad * (at your option) any later version. 60067f129SK.Prasad * 70067f129SK.Prasad * This program is distributed in the hope that it will be useful, 80067f129SK.Prasad * but WITHOUT ANY WARRANTY; without even the implied warranty of 90067f129SK.Prasad * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 100067f129SK.Prasad * GNU General Public License for more details. 110067f129SK.Prasad * 120067f129SK.Prasad * You should have received a copy of the GNU General Public License 130067f129SK.Prasad * along with this program; if not, write to the Free Software 140067f129SK.Prasad * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 150067f129SK.Prasad * 160067f129SK.Prasad * Copyright (C) 2007 Alan Stern 170067f129SK.Prasad * Copyright (C) 2009 IBM Corporation 1824f1e32cSFrederic Weisbecker * Copyright (C) 2009 Frederic Weisbecker <fweisbec@gmail.com> 19ba6909b7SK.Prasad * 20ba6909b7SK.Prasad * Authors: Alan Stern <stern@rowland.harvard.edu> 21ba6909b7SK.Prasad * K.Prasad <prasad@linux.vnet.ibm.com> 22ba6909b7SK.Prasad * Frederic Weisbecker <fweisbec@gmail.com> 230067f129SK.Prasad */ 240067f129SK.Prasad 250067f129SK.Prasad /* 260067f129SK.Prasad * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility, 270067f129SK.Prasad * using the CPU's debug registers. 280067f129SK.Prasad */ 290067f129SK.Prasad 3024f1e32cSFrederic Weisbecker #include <linux/perf_event.h> 3124f1e32cSFrederic Weisbecker #include <linux/hw_breakpoint.h> 320067f129SK.Prasad #include <linux/irqflags.h> 330067f129SK.Prasad #include <linux/notifier.h> 340067f129SK.Prasad #include <linux/kallsyms.h> 350067f129SK.Prasad #include <linux/percpu.h> 360067f129SK.Prasad #include <linux/kdebug.h> 370067f129SK.Prasad #include <linux/kernel.h> 380067f129SK.Prasad #include <linux/module.h> 390067f129SK.Prasad #include <linux/sched.h> 400067f129SK.Prasad #include <linux/smp.h> 410067f129SK.Prasad 420067f129SK.Prasad #include <asm/hw_breakpoint.h> 430067f129SK.Prasad #include <asm/processor.h> 440067f129SK.Prasad #include <asm/debugreg.h> 450067f129SK.Prasad 4624f1e32cSFrederic Weisbecker /* Per cpu debug control register value */ 4728b4e0d8STejun Heo DEFINE_PER_CPU(unsigned long, cpu_dr7); 4828b4e0d8STejun Heo EXPORT_PER_CPU_SYMBOL(cpu_dr7); 4924f1e32cSFrederic Weisbecker 5024f1e32cSFrederic Weisbecker /* Per cpu debug address registers values */ 5124f1e32cSFrederic Weisbecker static DEFINE_PER_CPU(unsigned long, cpu_debugreg[HBP_NUM]); 520067f129SK.Prasad 530067f129SK.Prasad /* 5424f1e32cSFrederic Weisbecker * Stores the breakpoints currently in use on each breakpoint address 5524f1e32cSFrederic Weisbecker * register for each cpus 560067f129SK.Prasad */ 5724f1e32cSFrederic Weisbecker static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]); 580067f129SK.Prasad 590067f129SK.Prasad 602c31b795SFrederic Weisbecker static inline unsigned long 612c31b795SFrederic Weisbecker __encode_dr7(int drnum, unsigned int len, unsigned int type) 622c31b795SFrederic Weisbecker { 632c31b795SFrederic Weisbecker unsigned long bp_info; 642c31b795SFrederic Weisbecker 652c31b795SFrederic Weisbecker bp_info = (len | type) & 0xf; 662c31b795SFrederic Weisbecker bp_info <<= (DR_CONTROL_SHIFT + drnum * DR_CONTROL_SIZE); 672c31b795SFrederic Weisbecker bp_info |= (DR_GLOBAL_ENABLE << (drnum * DR_ENABLE_SIZE)); 682c31b795SFrederic Weisbecker 692c31b795SFrederic Weisbecker return bp_info; 702c31b795SFrederic Weisbecker } 712c31b795SFrederic Weisbecker 720067f129SK.Prasad /* 730067f129SK.Prasad * Encode the length, type, Exact, and Enable bits for a particular breakpoint 740067f129SK.Prasad * as stored in debug register 7. 750067f129SK.Prasad */ 7624f1e32cSFrederic Weisbecker unsigned long encode_dr7(int drnum, unsigned int len, unsigned int type) 770067f129SK.Prasad { 782c31b795SFrederic Weisbecker return __encode_dr7(drnum, len, type) | DR_GLOBAL_SLOWDOWN; 790067f129SK.Prasad } 800067f129SK.Prasad 8124f1e32cSFrederic Weisbecker /* 8224f1e32cSFrederic Weisbecker * Decode the length and type bits for a particular breakpoint as 8324f1e32cSFrederic Weisbecker * stored in debug register 7. Return the "enabled" status. 840067f129SK.Prasad */ 8524f1e32cSFrederic Weisbecker int decode_dr7(unsigned long dr7, int bpnum, unsigned *len, unsigned *type) 8624f1e32cSFrederic Weisbecker { 8724f1e32cSFrederic Weisbecker int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE); 8824f1e32cSFrederic Weisbecker 8924f1e32cSFrederic Weisbecker *len = (bp_info & 0xc) | 0x40; 9024f1e32cSFrederic Weisbecker *type = (bp_info & 0x3) | 0x80; 9124f1e32cSFrederic Weisbecker 9224f1e32cSFrederic Weisbecker return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3; 930067f129SK.Prasad } 940067f129SK.Prasad 950067f129SK.Prasad /* 9624f1e32cSFrederic Weisbecker * Install a perf counter breakpoint. 9724f1e32cSFrederic Weisbecker * 9824f1e32cSFrederic Weisbecker * We seek a free debug address register and use it for this 9924f1e32cSFrederic Weisbecker * breakpoint. Eventually we enable it in the debug control register. 10024f1e32cSFrederic Weisbecker * 10124f1e32cSFrederic Weisbecker * Atomic: we hold the counter->ctx->lock and we only handle variables 10224f1e32cSFrederic Weisbecker * and registers local to this cpu. 1030067f129SK.Prasad */ 10424f1e32cSFrederic Weisbecker int arch_install_hw_breakpoint(struct perf_event *bp) 1050067f129SK.Prasad { 10624f1e32cSFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 10724f1e32cSFrederic Weisbecker unsigned long *dr7; 10824f1e32cSFrederic Weisbecker int i; 1090067f129SK.Prasad 11024f1e32cSFrederic Weisbecker for (i = 0; i < HBP_NUM; i++) { 11189cbc767SChristoph Lameter struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]); 11224f1e32cSFrederic Weisbecker 11324f1e32cSFrederic Weisbecker if (!*slot) { 11424f1e32cSFrederic Weisbecker *slot = bp; 1150067f129SK.Prasad break; 1160067f129SK.Prasad } 11724f1e32cSFrederic Weisbecker } 1180067f129SK.Prasad 11924f1e32cSFrederic Weisbecker if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot")) 12024f1e32cSFrederic Weisbecker return -EBUSY; 12124f1e32cSFrederic Weisbecker 12224f1e32cSFrederic Weisbecker set_debugreg(info->address, i); 1230a3aee0dSTejun Heo __this_cpu_write(cpu_debugreg[i], info->address); 12424f1e32cSFrederic Weisbecker 12589cbc767SChristoph Lameter dr7 = this_cpu_ptr(&cpu_dr7); 12624f1e32cSFrederic Weisbecker *dr7 |= encode_dr7(i, info->len, info->type); 12724f1e32cSFrederic Weisbecker 12824f1e32cSFrederic Weisbecker set_debugreg(*dr7, 7); 129*d6d55f0bSJacob Shin if (info->mask) 130*d6d55f0bSJacob Shin set_dr_addr_mask(info->mask, i); 13124f1e32cSFrederic Weisbecker 13224f1e32cSFrederic Weisbecker return 0; 1330067f129SK.Prasad } 1340067f129SK.Prasad 1350067f129SK.Prasad /* 13624f1e32cSFrederic Weisbecker * Uninstall the breakpoint contained in the given counter. 13724f1e32cSFrederic Weisbecker * 13824f1e32cSFrederic Weisbecker * First we search the debug address register it uses and then we disable 13924f1e32cSFrederic Weisbecker * it. 14024f1e32cSFrederic Weisbecker * 14124f1e32cSFrederic Weisbecker * Atomic: we hold the counter->ctx->lock and we only handle variables 14224f1e32cSFrederic Weisbecker * and registers local to this cpu. 1430067f129SK.Prasad */ 14424f1e32cSFrederic Weisbecker void arch_uninstall_hw_breakpoint(struct perf_event *bp) 1450067f129SK.Prasad { 14624f1e32cSFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 14724f1e32cSFrederic Weisbecker unsigned long *dr7; 14824f1e32cSFrederic Weisbecker int i; 1490067f129SK.Prasad 15024f1e32cSFrederic Weisbecker for (i = 0; i < HBP_NUM; i++) { 15189cbc767SChristoph Lameter struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]); 15224f1e32cSFrederic Weisbecker 15324f1e32cSFrederic Weisbecker if (*slot == bp) { 15424f1e32cSFrederic Weisbecker *slot = NULL; 15524f1e32cSFrederic Weisbecker break; 15624f1e32cSFrederic Weisbecker } 15724f1e32cSFrederic Weisbecker } 15824f1e32cSFrederic Weisbecker 15924f1e32cSFrederic Weisbecker if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot")) 16024f1e32cSFrederic Weisbecker return; 16124f1e32cSFrederic Weisbecker 16289cbc767SChristoph Lameter dr7 = this_cpu_ptr(&cpu_dr7); 1632c31b795SFrederic Weisbecker *dr7 &= ~__encode_dr7(i, info->len, info->type); 16424f1e32cSFrederic Weisbecker 16524f1e32cSFrederic Weisbecker set_debugreg(*dr7, 7); 166*d6d55f0bSJacob Shin if (info->mask) 167*d6d55f0bSJacob Shin set_dr_addr_mask(0, i); 1680067f129SK.Prasad } 1690067f129SK.Prasad 1700067f129SK.Prasad static int get_hbp_len(u8 hbp_len) 1710067f129SK.Prasad { 1720067f129SK.Prasad unsigned int len_in_bytes = 0; 1730067f129SK.Prasad 1740067f129SK.Prasad switch (hbp_len) { 17524f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_1: 1760067f129SK.Prasad len_in_bytes = 1; 1770067f129SK.Prasad break; 17824f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_2: 1790067f129SK.Prasad len_in_bytes = 2; 1800067f129SK.Prasad break; 18124f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_4: 1820067f129SK.Prasad len_in_bytes = 4; 1830067f129SK.Prasad break; 1840067f129SK.Prasad #ifdef CONFIG_X86_64 18524f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_8: 1860067f129SK.Prasad len_in_bytes = 8; 1870067f129SK.Prasad break; 1880067f129SK.Prasad #endif 1890067f129SK.Prasad } 1900067f129SK.Prasad return len_in_bytes; 1910067f129SK.Prasad } 1920067f129SK.Prasad 1930067f129SK.Prasad /* 1940067f129SK.Prasad * Check for virtual address in kernel space. 1950067f129SK.Prasad */ 196b2812d03SFrederic Weisbecker int arch_check_bp_in_kernelspace(struct perf_event *bp) 1970067f129SK.Prasad { 1980067f129SK.Prasad unsigned int len; 199b2812d03SFrederic Weisbecker unsigned long va; 200b2812d03SFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 2010067f129SK.Prasad 202b2812d03SFrederic Weisbecker va = info->address; 203b2812d03SFrederic Weisbecker len = get_hbp_len(info->len); 2040067f129SK.Prasad 2050067f129SK.Prasad return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); 2060067f129SK.Prasad } 2070067f129SK.Prasad 20824f1e32cSFrederic Weisbecker int arch_bp_generic_fields(int x86_len, int x86_type, 20924f1e32cSFrederic Weisbecker int *gen_len, int *gen_type) 21024f1e32cSFrederic Weisbecker { 21189e45aacSFrederic Weisbecker /* Type */ 21289e45aacSFrederic Weisbecker switch (x86_type) { 21389e45aacSFrederic Weisbecker case X86_BREAKPOINT_EXECUTE: 21489e45aacSFrederic Weisbecker if (x86_len != X86_BREAKPOINT_LEN_X) 21589e45aacSFrederic Weisbecker return -EINVAL; 21689e45aacSFrederic Weisbecker 21789e45aacSFrederic Weisbecker *gen_type = HW_BREAKPOINT_X; 21889e45aacSFrederic Weisbecker *gen_len = sizeof(long); 21989e45aacSFrederic Weisbecker return 0; 22089e45aacSFrederic Weisbecker case X86_BREAKPOINT_WRITE: 22189e45aacSFrederic Weisbecker *gen_type = HW_BREAKPOINT_W; 22289e45aacSFrederic Weisbecker break; 22389e45aacSFrederic Weisbecker case X86_BREAKPOINT_RW: 22489e45aacSFrederic Weisbecker *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R; 22589e45aacSFrederic Weisbecker break; 22689e45aacSFrederic Weisbecker default: 22789e45aacSFrederic Weisbecker return -EINVAL; 22889e45aacSFrederic Weisbecker } 22989e45aacSFrederic Weisbecker 23024f1e32cSFrederic Weisbecker /* Len */ 23124f1e32cSFrederic Weisbecker switch (x86_len) { 23224f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_1: 23324f1e32cSFrederic Weisbecker *gen_len = HW_BREAKPOINT_LEN_1; 23424f1e32cSFrederic Weisbecker break; 23524f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_2: 23624f1e32cSFrederic Weisbecker *gen_len = HW_BREAKPOINT_LEN_2; 23724f1e32cSFrederic Weisbecker break; 23824f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_4: 23924f1e32cSFrederic Weisbecker *gen_len = HW_BREAKPOINT_LEN_4; 24024f1e32cSFrederic Weisbecker break; 24124f1e32cSFrederic Weisbecker #ifdef CONFIG_X86_64 24224f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_8: 24324f1e32cSFrederic Weisbecker *gen_len = HW_BREAKPOINT_LEN_8; 24424f1e32cSFrederic Weisbecker break; 24524f1e32cSFrederic Weisbecker #endif 24624f1e32cSFrederic Weisbecker default: 24724f1e32cSFrederic Weisbecker return -EINVAL; 24824f1e32cSFrederic Weisbecker } 24924f1e32cSFrederic Weisbecker 25024f1e32cSFrederic Weisbecker return 0; 25124f1e32cSFrederic Weisbecker } 25224f1e32cSFrederic Weisbecker 25324f1e32cSFrederic Weisbecker 25424f1e32cSFrederic Weisbecker static int arch_build_bp_info(struct perf_event *bp) 25524f1e32cSFrederic Weisbecker { 25624f1e32cSFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 25724f1e32cSFrederic Weisbecker 25824f1e32cSFrederic Weisbecker info->address = bp->attr.bp_addr; 25924f1e32cSFrederic Weisbecker 260f7809dafSFrederic Weisbecker /* Type */ 261f7809dafSFrederic Weisbecker switch (bp->attr.bp_type) { 262f7809dafSFrederic Weisbecker case HW_BREAKPOINT_W: 263f7809dafSFrederic Weisbecker info->type = X86_BREAKPOINT_WRITE; 264f7809dafSFrederic Weisbecker break; 265f7809dafSFrederic Weisbecker case HW_BREAKPOINT_W | HW_BREAKPOINT_R: 266f7809dafSFrederic Weisbecker info->type = X86_BREAKPOINT_RW; 267f7809dafSFrederic Weisbecker break; 268f7809dafSFrederic Weisbecker case HW_BREAKPOINT_X: 269f7809dafSFrederic Weisbecker info->type = X86_BREAKPOINT_EXECUTE; 270f7809dafSFrederic Weisbecker /* 271f7809dafSFrederic Weisbecker * x86 inst breakpoints need to have a specific undefined len. 272f7809dafSFrederic Weisbecker * But we still need to check userspace is not trying to setup 273f7809dafSFrederic Weisbecker * an unsupported length, to get a range breakpoint for example. 274f7809dafSFrederic Weisbecker */ 275f7809dafSFrederic Weisbecker if (bp->attr.bp_len == sizeof(long)) { 276f7809dafSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_X; 277f7809dafSFrederic Weisbecker return 0; 278f7809dafSFrederic Weisbecker } 279f7809dafSFrederic Weisbecker default: 280f7809dafSFrederic Weisbecker return -EINVAL; 281f7809dafSFrederic Weisbecker } 282f7809dafSFrederic Weisbecker 28324f1e32cSFrederic Weisbecker /* Len */ 284*d6d55f0bSJacob Shin info->mask = 0; 285*d6d55f0bSJacob Shin 28624f1e32cSFrederic Weisbecker switch (bp->attr.bp_len) { 28724f1e32cSFrederic Weisbecker case HW_BREAKPOINT_LEN_1: 28824f1e32cSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_1; 28924f1e32cSFrederic Weisbecker break; 29024f1e32cSFrederic Weisbecker case HW_BREAKPOINT_LEN_2: 29124f1e32cSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_2; 29224f1e32cSFrederic Weisbecker break; 29324f1e32cSFrederic Weisbecker case HW_BREAKPOINT_LEN_4: 29424f1e32cSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_4; 29524f1e32cSFrederic Weisbecker break; 29624f1e32cSFrederic Weisbecker #ifdef CONFIG_X86_64 29724f1e32cSFrederic Weisbecker case HW_BREAKPOINT_LEN_8: 29824f1e32cSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_8; 29924f1e32cSFrederic Weisbecker break; 30024f1e32cSFrederic Weisbecker #endif 30124f1e32cSFrederic Weisbecker default: 302*d6d55f0bSJacob Shin if (!is_power_of_2(bp->attr.bp_len)) 30324f1e32cSFrederic Weisbecker return -EINVAL; 304*d6d55f0bSJacob Shin if (!cpu_has_bpext) 305*d6d55f0bSJacob Shin return -EOPNOTSUPP; 306*d6d55f0bSJacob Shin info->mask = bp->attr.bp_len - 1; 307*d6d55f0bSJacob Shin info->len = X86_BREAKPOINT_LEN_1; 30824f1e32cSFrederic Weisbecker } 30924f1e32cSFrederic Weisbecker 31024f1e32cSFrederic Weisbecker return 0; 31124f1e32cSFrederic Weisbecker } 312*d6d55f0bSJacob Shin 3130067f129SK.Prasad /* 3140067f129SK.Prasad * Validate the arch-specific HW Breakpoint register settings 3150067f129SK.Prasad */ 316b2812d03SFrederic Weisbecker int arch_validate_hwbkpt_settings(struct perf_event *bp) 3170067f129SK.Prasad { 31824f1e32cSFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 3190067f129SK.Prasad unsigned int align; 32024f1e32cSFrederic Weisbecker int ret; 3210067f129SK.Prasad 32224f1e32cSFrederic Weisbecker 32324f1e32cSFrederic Weisbecker ret = arch_build_bp_info(bp); 32424f1e32cSFrederic Weisbecker if (ret) 32524f1e32cSFrederic Weisbecker return ret; 32624f1e32cSFrederic Weisbecker 32724f1e32cSFrederic Weisbecker switch (info->len) { 32824f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_1: 3290067f129SK.Prasad align = 0; 330*d6d55f0bSJacob Shin if (info->mask) 331*d6d55f0bSJacob Shin align = info->mask; 3320067f129SK.Prasad break; 33324f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_2: 3340067f129SK.Prasad align = 1; 3350067f129SK.Prasad break; 33624f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_4: 3370067f129SK.Prasad align = 3; 3380067f129SK.Prasad break; 3390067f129SK.Prasad #ifdef CONFIG_X86_64 34024f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_8: 3410067f129SK.Prasad align = 7; 3420067f129SK.Prasad break; 3430067f129SK.Prasad #endif 3440067f129SK.Prasad default: 345*d6d55f0bSJacob Shin WARN_ON_ONCE(1); 3460067f129SK.Prasad } 3470067f129SK.Prasad 34884d71092SFrederic Weisbecker /* 3490067f129SK.Prasad * Check that the low-order bits of the address are appropriate 3500067f129SK.Prasad * for the alignment implied by len. 3510067f129SK.Prasad */ 35224f1e32cSFrederic Weisbecker if (info->address & align) 3530067f129SK.Prasad return -EINVAL; 3540067f129SK.Prasad 3550067f129SK.Prasad return 0; 3560067f129SK.Prasad } 3570067f129SK.Prasad 35824f1e32cSFrederic Weisbecker /* 3599f6b3c2cSFrederic Weisbecker * Dump the debug register contents to the user. 3609f6b3c2cSFrederic Weisbecker * We can't dump our per cpu values because it 3619f6b3c2cSFrederic Weisbecker * may contain cpu wide breakpoint, something that 3629f6b3c2cSFrederic Weisbecker * doesn't belong to the current task. 3639f6b3c2cSFrederic Weisbecker * 3649f6b3c2cSFrederic Weisbecker * TODO: include non-ptrace user breakpoints (perf) 3659f6b3c2cSFrederic Weisbecker */ 3669f6b3c2cSFrederic Weisbecker void aout_dump_debugregs(struct user *dump) 3679f6b3c2cSFrederic Weisbecker { 3689f6b3c2cSFrederic Weisbecker int i; 3699f6b3c2cSFrederic Weisbecker int dr7 = 0; 3709f6b3c2cSFrederic Weisbecker struct perf_event *bp; 3719f6b3c2cSFrederic Weisbecker struct arch_hw_breakpoint *info; 3729f6b3c2cSFrederic Weisbecker struct thread_struct *thread = ¤t->thread; 3739f6b3c2cSFrederic Weisbecker 3749f6b3c2cSFrederic Weisbecker for (i = 0; i < HBP_NUM; i++) { 3759f6b3c2cSFrederic Weisbecker bp = thread->ptrace_bps[i]; 3769f6b3c2cSFrederic Weisbecker 3779f6b3c2cSFrederic Weisbecker if (bp && !bp->attr.disabled) { 3789f6b3c2cSFrederic Weisbecker dump->u_debugreg[i] = bp->attr.bp_addr; 3799f6b3c2cSFrederic Weisbecker info = counter_arch_bp(bp); 3809f6b3c2cSFrederic Weisbecker dr7 |= encode_dr7(i, info->len, info->type); 3819f6b3c2cSFrederic Weisbecker } else { 3829f6b3c2cSFrederic Weisbecker dump->u_debugreg[i] = 0; 3839f6b3c2cSFrederic Weisbecker } 3849f6b3c2cSFrederic Weisbecker } 3859f6b3c2cSFrederic Weisbecker 3869f6b3c2cSFrederic Weisbecker dump->u_debugreg[4] = 0; 3879f6b3c2cSFrederic Weisbecker dump->u_debugreg[5] = 0; 3889f6b3c2cSFrederic Weisbecker dump->u_debugreg[6] = current->thread.debugreg6; 3899f6b3c2cSFrederic Weisbecker 3909f6b3c2cSFrederic Weisbecker dump->u_debugreg[7] = dr7; 3919f6b3c2cSFrederic Weisbecker } 39268efa37dSIngo Molnar EXPORT_SYMBOL_GPL(aout_dump_debugregs); 3939f6b3c2cSFrederic Weisbecker 3949f6b3c2cSFrederic Weisbecker /* 39524f1e32cSFrederic Weisbecker * Release the user breakpoints used by ptrace 39624f1e32cSFrederic Weisbecker */ 39724f1e32cSFrederic Weisbecker void flush_ptrace_hw_breakpoint(struct task_struct *tsk) 3980067f129SK.Prasad { 3990067f129SK.Prasad int i; 40024f1e32cSFrederic Weisbecker struct thread_struct *t = &tsk->thread; 4010067f129SK.Prasad 40224f1e32cSFrederic Weisbecker for (i = 0; i < HBP_NUM; i++) { 40324f1e32cSFrederic Weisbecker unregister_hw_breakpoint(t->ptrace_bps[i]); 40424f1e32cSFrederic Weisbecker t->ptrace_bps[i] = NULL; 4050067f129SK.Prasad } 406f7da04c9SOleg Nesterov 407f7da04c9SOleg Nesterov t->debugreg6 = 0; 408f7da04c9SOleg Nesterov t->ptrace_dr7 = 0; 40924f1e32cSFrederic Weisbecker } 41024f1e32cSFrederic Weisbecker 41124f1e32cSFrederic Weisbecker void hw_breakpoint_restore(void) 41224f1e32cSFrederic Weisbecker { 4130a3aee0dSTejun Heo set_debugreg(__this_cpu_read(cpu_debugreg[0]), 0); 4140a3aee0dSTejun Heo set_debugreg(__this_cpu_read(cpu_debugreg[1]), 1); 4150a3aee0dSTejun Heo set_debugreg(__this_cpu_read(cpu_debugreg[2]), 2); 4160a3aee0dSTejun Heo set_debugreg(__this_cpu_read(cpu_debugreg[3]), 3); 41724f1e32cSFrederic Weisbecker set_debugreg(current->thread.debugreg6, 6); 4180a3aee0dSTejun Heo set_debugreg(__this_cpu_read(cpu_dr7), 7); 41924f1e32cSFrederic Weisbecker } 42024f1e32cSFrederic Weisbecker EXPORT_SYMBOL_GPL(hw_breakpoint_restore); 4210067f129SK.Prasad 4220067f129SK.Prasad /* 4230067f129SK.Prasad * Handle debug exception notifications. 4240067f129SK.Prasad * 4250067f129SK.Prasad * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below. 4260067f129SK.Prasad * 4270067f129SK.Prasad * NOTIFY_DONE returned if one of the following conditions is true. 4280067f129SK.Prasad * i) When the causative address is from user-space and the exception 4290067f129SK.Prasad * is a valid one, i.e. not triggered as a result of lazy debug register 4300067f129SK.Prasad * switching 4310067f129SK.Prasad * ii) When there are more bits than trap<n> set in DR6 register (such 4320067f129SK.Prasad * as BD, BS or BT) indicating that more than one debug condition is 4330067f129SK.Prasad * met and requires some more action in do_debug(). 4340067f129SK.Prasad * 4350067f129SK.Prasad * NOTIFY_STOP returned for all other cases 4360067f129SK.Prasad * 4370067f129SK.Prasad */ 4389c54b616SMasami Hiramatsu static int hw_breakpoint_handler(struct die_args *args) 4390067f129SK.Prasad { 4400067f129SK.Prasad int i, cpu, rc = NOTIFY_STOP; 44124f1e32cSFrederic Weisbecker struct perf_event *bp; 44262edab90SK.Prasad unsigned long dr7, dr6; 44362edab90SK.Prasad unsigned long *dr6_p; 44462edab90SK.Prasad 44562edab90SK.Prasad /* The DR6 value is pointed by args->err */ 44662edab90SK.Prasad dr6_p = (unsigned long *)ERR_PTR(args->err); 44762edab90SK.Prasad dr6 = *dr6_p; 4480067f129SK.Prasad 4496c0aca28SFrederic Weisbecker /* If it's a single step, TRAP bits are random */ 4506c0aca28SFrederic Weisbecker if (dr6 & DR_STEP) 4516c0aca28SFrederic Weisbecker return NOTIFY_DONE; 4526c0aca28SFrederic Weisbecker 4530067f129SK.Prasad /* Do an early return if no trap bits are set in DR6 */ 4540067f129SK.Prasad if ((dr6 & DR_TRAP_BITS) == 0) 4550067f129SK.Prasad return NOTIFY_DONE; 4560067f129SK.Prasad 4570067f129SK.Prasad get_debugreg(dr7, 7); 4580067f129SK.Prasad /* Disable breakpoints during exception handling */ 4590067f129SK.Prasad set_debugreg(0UL, 7); 4600067f129SK.Prasad /* 4610067f129SK.Prasad * Assert that local interrupts are disabled 4620067f129SK.Prasad * Reset the DRn bits in the virtualized register value. 4630067f129SK.Prasad * The ptrace trigger routine will add in whatever is needed. 4640067f129SK.Prasad */ 4650067f129SK.Prasad current->thread.debugreg6 &= ~DR_TRAP_BITS; 4660067f129SK.Prasad cpu = get_cpu(); 4670067f129SK.Prasad 4680067f129SK.Prasad /* Handle all the breakpoints that were triggered */ 4690067f129SK.Prasad for (i = 0; i < HBP_NUM; ++i) { 4700067f129SK.Prasad if (likely(!(dr6 & (DR_TRAP0 << i)))) 4710067f129SK.Prasad continue; 47224f1e32cSFrederic Weisbecker 4730067f129SK.Prasad /* 47424f1e32cSFrederic Weisbecker * The counter may be concurrently released but that can only 47524f1e32cSFrederic Weisbecker * occur from a call_rcu() path. We can then safely fetch 47624f1e32cSFrederic Weisbecker * the breakpoint, use its callback, touch its counter 47724f1e32cSFrederic Weisbecker * while we are in an rcu_read_lock() path. 4780067f129SK.Prasad */ 47924f1e32cSFrederic Weisbecker rcu_read_lock(); 48024f1e32cSFrederic Weisbecker 48124f1e32cSFrederic Weisbecker bp = per_cpu(bp_per_reg[i], cpu); 4820067f129SK.Prasad /* 48362edab90SK.Prasad * Reset the 'i'th TRAP bit in dr6 to denote completion of 48462edab90SK.Prasad * exception handling 48562edab90SK.Prasad */ 48662edab90SK.Prasad (*dr6_p) &= ~(DR_TRAP0 << i); 48762edab90SK.Prasad /* 4880067f129SK.Prasad * bp can be NULL due to lazy debug register switching 48924f1e32cSFrederic Weisbecker * or due to concurrent perf counter removing. 4900067f129SK.Prasad */ 49124f1e32cSFrederic Weisbecker if (!bp) { 49224f1e32cSFrederic Weisbecker rcu_read_unlock(); 49324f1e32cSFrederic Weisbecker break; 49424f1e32cSFrederic Weisbecker } 4950067f129SK.Prasad 496b326e956SFrederic Weisbecker perf_bp_event(bp, args->regs); 49724f1e32cSFrederic Weisbecker 4980c4519e8SFrederic Weisbecker /* 4990c4519e8SFrederic Weisbecker * Set up resume flag to avoid breakpoint recursion when 5000c4519e8SFrederic Weisbecker * returning back to origin. 5010c4519e8SFrederic Weisbecker */ 5020c4519e8SFrederic Weisbecker if (bp->hw.info.type == X86_BREAKPOINT_EXECUTE) 5030c4519e8SFrederic Weisbecker args->regs->flags |= X86_EFLAGS_RF; 5040c4519e8SFrederic Weisbecker 50524f1e32cSFrederic Weisbecker rcu_read_unlock(); 5060067f129SK.Prasad } 507e0e53db6SK.Prasad /* 508e0e53db6SK.Prasad * Further processing in do_debug() is needed for a) user-space 509e0e53db6SK.Prasad * breakpoints (to generate signals) and b) when the system has 510e0e53db6SK.Prasad * taken exception due to multiple causes 511e0e53db6SK.Prasad */ 512e0e53db6SK.Prasad if ((current->thread.debugreg6 & DR_TRAP_BITS) || 513e0e53db6SK.Prasad (dr6 & (~DR_TRAP_BITS))) 5140067f129SK.Prasad rc = NOTIFY_DONE; 5150067f129SK.Prasad 5160067f129SK.Prasad set_debugreg(dr7, 7); 517eadb8a09SIngo Molnar put_cpu(); 51824f1e32cSFrederic Weisbecker 5190067f129SK.Prasad return rc; 5200067f129SK.Prasad } 5210067f129SK.Prasad 5220067f129SK.Prasad /* 5230067f129SK.Prasad * Handle debug exception notifications. 5240067f129SK.Prasad */ 5259c54b616SMasami Hiramatsu int hw_breakpoint_exceptions_notify( 5260067f129SK.Prasad struct notifier_block *unused, unsigned long val, void *data) 5270067f129SK.Prasad { 5280067f129SK.Prasad if (val != DIE_DEBUG) 5290067f129SK.Prasad return NOTIFY_DONE; 5300067f129SK.Prasad 5310067f129SK.Prasad return hw_breakpoint_handler(data); 5320067f129SK.Prasad } 53324f1e32cSFrederic Weisbecker 53424f1e32cSFrederic Weisbecker void hw_breakpoint_pmu_read(struct perf_event *bp) 53524f1e32cSFrederic Weisbecker { 53624f1e32cSFrederic Weisbecker /* TODO */ 53724f1e32cSFrederic Weisbecker } 538