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/kprobes.h> 360067f129SK.Prasad #include <linux/percpu.h> 370067f129SK.Prasad #include <linux/kdebug.h> 380067f129SK.Prasad #include <linux/kernel.h> 390067f129SK.Prasad #include <linux/module.h> 400067f129SK.Prasad #include <linux/sched.h> 410067f129SK.Prasad #include <linux/init.h> 420067f129SK.Prasad #include <linux/smp.h> 430067f129SK.Prasad 440067f129SK.Prasad #include <asm/hw_breakpoint.h> 450067f129SK.Prasad #include <asm/processor.h> 460067f129SK.Prasad #include <asm/debugreg.h> 470067f129SK.Prasad 4824f1e32cSFrederic Weisbecker /* Per cpu debug control register value */ 4928b4e0d8STejun Heo DEFINE_PER_CPU(unsigned long, cpu_dr7); 5028b4e0d8STejun Heo EXPORT_PER_CPU_SYMBOL(cpu_dr7); 5124f1e32cSFrederic Weisbecker 5224f1e32cSFrederic Weisbecker /* Per cpu debug address registers values */ 5324f1e32cSFrederic Weisbecker static DEFINE_PER_CPU(unsigned long, cpu_debugreg[HBP_NUM]); 540067f129SK.Prasad 550067f129SK.Prasad /* 5624f1e32cSFrederic Weisbecker * Stores the breakpoints currently in use on each breakpoint address 5724f1e32cSFrederic Weisbecker * register for each cpus 580067f129SK.Prasad */ 5924f1e32cSFrederic Weisbecker static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]); 600067f129SK.Prasad 610067f129SK.Prasad 622c31b795SFrederic Weisbecker static inline unsigned long 632c31b795SFrederic Weisbecker __encode_dr7(int drnum, unsigned int len, unsigned int type) 642c31b795SFrederic Weisbecker { 652c31b795SFrederic Weisbecker unsigned long bp_info; 662c31b795SFrederic Weisbecker 672c31b795SFrederic Weisbecker bp_info = (len | type) & 0xf; 682c31b795SFrederic Weisbecker bp_info <<= (DR_CONTROL_SHIFT + drnum * DR_CONTROL_SIZE); 692c31b795SFrederic Weisbecker bp_info |= (DR_GLOBAL_ENABLE << (drnum * DR_ENABLE_SIZE)); 702c31b795SFrederic Weisbecker 712c31b795SFrederic Weisbecker return bp_info; 722c31b795SFrederic Weisbecker } 732c31b795SFrederic Weisbecker 740067f129SK.Prasad /* 750067f129SK.Prasad * Encode the length, type, Exact, and Enable bits for a particular breakpoint 760067f129SK.Prasad * as stored in debug register 7. 770067f129SK.Prasad */ 7824f1e32cSFrederic Weisbecker unsigned long encode_dr7(int drnum, unsigned int len, unsigned int type) 790067f129SK.Prasad { 802c31b795SFrederic Weisbecker return __encode_dr7(drnum, len, type) | DR_GLOBAL_SLOWDOWN; 810067f129SK.Prasad } 820067f129SK.Prasad 8324f1e32cSFrederic Weisbecker /* 8424f1e32cSFrederic Weisbecker * Decode the length and type bits for a particular breakpoint as 8524f1e32cSFrederic Weisbecker * stored in debug register 7. Return the "enabled" status. 860067f129SK.Prasad */ 8724f1e32cSFrederic Weisbecker int decode_dr7(unsigned long dr7, int bpnum, unsigned *len, unsigned *type) 8824f1e32cSFrederic Weisbecker { 8924f1e32cSFrederic Weisbecker int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE); 9024f1e32cSFrederic Weisbecker 9124f1e32cSFrederic Weisbecker *len = (bp_info & 0xc) | 0x40; 9224f1e32cSFrederic Weisbecker *type = (bp_info & 0x3) | 0x80; 9324f1e32cSFrederic Weisbecker 9424f1e32cSFrederic Weisbecker return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3; 950067f129SK.Prasad } 960067f129SK.Prasad 970067f129SK.Prasad /* 9824f1e32cSFrederic Weisbecker * Install a perf counter breakpoint. 9924f1e32cSFrederic Weisbecker * 10024f1e32cSFrederic Weisbecker * We seek a free debug address register and use it for this 10124f1e32cSFrederic Weisbecker * breakpoint. Eventually we enable it in the debug control register. 10224f1e32cSFrederic Weisbecker * 10324f1e32cSFrederic Weisbecker * Atomic: we hold the counter->ctx->lock and we only handle variables 10424f1e32cSFrederic Weisbecker * and registers local to this cpu. 1050067f129SK.Prasad */ 10624f1e32cSFrederic Weisbecker int arch_install_hw_breakpoint(struct perf_event *bp) 1070067f129SK.Prasad { 10824f1e32cSFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 10924f1e32cSFrederic Weisbecker unsigned long *dr7; 11024f1e32cSFrederic Weisbecker int i; 1110067f129SK.Prasad 11224f1e32cSFrederic Weisbecker for (i = 0; i < HBP_NUM; i++) { 11324f1e32cSFrederic Weisbecker struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]); 11424f1e32cSFrederic Weisbecker 11524f1e32cSFrederic Weisbecker if (!*slot) { 11624f1e32cSFrederic Weisbecker *slot = bp; 1170067f129SK.Prasad break; 1180067f129SK.Prasad } 11924f1e32cSFrederic Weisbecker } 1200067f129SK.Prasad 12124f1e32cSFrederic Weisbecker if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot")) 12224f1e32cSFrederic Weisbecker return -EBUSY; 12324f1e32cSFrederic Weisbecker 12424f1e32cSFrederic Weisbecker set_debugreg(info->address, i); 12524f1e32cSFrederic Weisbecker __get_cpu_var(cpu_debugreg[i]) = info->address; 12624f1e32cSFrederic Weisbecker 12728b4e0d8STejun Heo dr7 = &__get_cpu_var(cpu_dr7); 12824f1e32cSFrederic Weisbecker *dr7 |= encode_dr7(i, info->len, info->type); 12924f1e32cSFrederic Weisbecker 13024f1e32cSFrederic Weisbecker set_debugreg(*dr7, 7); 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++) { 15124f1e32cSFrederic Weisbecker struct perf_event **slot = &__get_cpu_var(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 16228b4e0d8STejun Heo dr7 = &__get_cpu_var(cpu_dr7); 1632c31b795SFrederic Weisbecker *dr7 &= ~__encode_dr7(i, info->len, info->type); 16424f1e32cSFrederic Weisbecker 16524f1e32cSFrederic Weisbecker set_debugreg(*dr7, 7); 1660067f129SK.Prasad } 1670067f129SK.Prasad 1680067f129SK.Prasad static int get_hbp_len(u8 hbp_len) 1690067f129SK.Prasad { 1700067f129SK.Prasad unsigned int len_in_bytes = 0; 1710067f129SK.Prasad 1720067f129SK.Prasad switch (hbp_len) { 17324f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_1: 1740067f129SK.Prasad len_in_bytes = 1; 1750067f129SK.Prasad break; 17624f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_2: 1770067f129SK.Prasad len_in_bytes = 2; 1780067f129SK.Prasad break; 17924f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_4: 1800067f129SK.Prasad len_in_bytes = 4; 1810067f129SK.Prasad break; 1820067f129SK.Prasad #ifdef CONFIG_X86_64 18324f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_8: 1840067f129SK.Prasad len_in_bytes = 8; 1850067f129SK.Prasad break; 1860067f129SK.Prasad #endif 1870067f129SK.Prasad } 1880067f129SK.Prasad return len_in_bytes; 1890067f129SK.Prasad } 1900067f129SK.Prasad 1910067f129SK.Prasad /* 1920067f129SK.Prasad * Check for virtual address in user space. 1930067f129SK.Prasad */ 1940067f129SK.Prasad int arch_check_va_in_userspace(unsigned long va, u8 hbp_len) 1950067f129SK.Prasad { 1960067f129SK.Prasad unsigned int len; 1970067f129SK.Prasad 1980067f129SK.Prasad len = get_hbp_len(hbp_len); 1990067f129SK.Prasad 2000067f129SK.Prasad return (va <= TASK_SIZE - len); 2010067f129SK.Prasad } 2020067f129SK.Prasad 2030067f129SK.Prasad /* 2040067f129SK.Prasad * Check for virtual address in kernel space. 2050067f129SK.Prasad */ 2064555835bSJaswinder Singh Rajput static int arch_check_va_in_kernelspace(unsigned long va, u8 hbp_len) 2070067f129SK.Prasad { 2080067f129SK.Prasad unsigned int len; 2090067f129SK.Prasad 2100067f129SK.Prasad len = get_hbp_len(hbp_len); 2110067f129SK.Prasad 2120067f129SK.Prasad return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); 2130067f129SK.Prasad } 2140067f129SK.Prasad 21524f1e32cSFrederic Weisbecker int arch_bp_generic_fields(int x86_len, int x86_type, 21624f1e32cSFrederic Weisbecker int *gen_len, int *gen_type) 21724f1e32cSFrederic Weisbecker { 21824f1e32cSFrederic Weisbecker /* Len */ 21924f1e32cSFrederic Weisbecker switch (x86_len) { 22024f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_1: 22124f1e32cSFrederic Weisbecker *gen_len = HW_BREAKPOINT_LEN_1; 22224f1e32cSFrederic Weisbecker break; 22324f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_2: 22424f1e32cSFrederic Weisbecker *gen_len = HW_BREAKPOINT_LEN_2; 22524f1e32cSFrederic Weisbecker break; 22624f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_4: 22724f1e32cSFrederic Weisbecker *gen_len = HW_BREAKPOINT_LEN_4; 22824f1e32cSFrederic Weisbecker break; 22924f1e32cSFrederic Weisbecker #ifdef CONFIG_X86_64 23024f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_8: 23124f1e32cSFrederic Weisbecker *gen_len = HW_BREAKPOINT_LEN_8; 23224f1e32cSFrederic Weisbecker break; 23324f1e32cSFrederic Weisbecker #endif 23424f1e32cSFrederic Weisbecker default: 23524f1e32cSFrederic Weisbecker return -EINVAL; 23624f1e32cSFrederic Weisbecker } 23724f1e32cSFrederic Weisbecker 23824f1e32cSFrederic Weisbecker /* Type */ 23924f1e32cSFrederic Weisbecker switch (x86_type) { 24024f1e32cSFrederic Weisbecker case X86_BREAKPOINT_EXECUTE: 24124f1e32cSFrederic Weisbecker *gen_type = HW_BREAKPOINT_X; 24224f1e32cSFrederic Weisbecker break; 24324f1e32cSFrederic Weisbecker case X86_BREAKPOINT_WRITE: 24424f1e32cSFrederic Weisbecker *gen_type = HW_BREAKPOINT_W; 24524f1e32cSFrederic Weisbecker break; 24624f1e32cSFrederic Weisbecker case X86_BREAKPOINT_RW: 24724f1e32cSFrederic Weisbecker *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R; 24824f1e32cSFrederic Weisbecker break; 24924f1e32cSFrederic Weisbecker default: 25024f1e32cSFrederic Weisbecker return -EINVAL; 25124f1e32cSFrederic Weisbecker } 25224f1e32cSFrederic Weisbecker 25324f1e32cSFrederic Weisbecker return 0; 25424f1e32cSFrederic Weisbecker } 25524f1e32cSFrederic Weisbecker 25624f1e32cSFrederic Weisbecker 25724f1e32cSFrederic Weisbecker static int arch_build_bp_info(struct perf_event *bp) 25824f1e32cSFrederic Weisbecker { 25924f1e32cSFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 26024f1e32cSFrederic Weisbecker 26124f1e32cSFrederic Weisbecker info->address = bp->attr.bp_addr; 26224f1e32cSFrederic Weisbecker 26324f1e32cSFrederic Weisbecker /* Len */ 26424f1e32cSFrederic Weisbecker switch (bp->attr.bp_len) { 26524f1e32cSFrederic Weisbecker case HW_BREAKPOINT_LEN_1: 26624f1e32cSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_1; 26724f1e32cSFrederic Weisbecker break; 26824f1e32cSFrederic Weisbecker case HW_BREAKPOINT_LEN_2: 26924f1e32cSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_2; 27024f1e32cSFrederic Weisbecker break; 27124f1e32cSFrederic Weisbecker case HW_BREAKPOINT_LEN_4: 27224f1e32cSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_4; 27324f1e32cSFrederic Weisbecker break; 27424f1e32cSFrederic Weisbecker #ifdef CONFIG_X86_64 27524f1e32cSFrederic Weisbecker case HW_BREAKPOINT_LEN_8: 27624f1e32cSFrederic Weisbecker info->len = X86_BREAKPOINT_LEN_8; 27724f1e32cSFrederic Weisbecker break; 27824f1e32cSFrederic Weisbecker #endif 27924f1e32cSFrederic Weisbecker default: 28024f1e32cSFrederic Weisbecker return -EINVAL; 28124f1e32cSFrederic Weisbecker } 28224f1e32cSFrederic Weisbecker 28324f1e32cSFrederic Weisbecker /* Type */ 28424f1e32cSFrederic Weisbecker switch (bp->attr.bp_type) { 28524f1e32cSFrederic Weisbecker case HW_BREAKPOINT_W: 28624f1e32cSFrederic Weisbecker info->type = X86_BREAKPOINT_WRITE; 28724f1e32cSFrederic Weisbecker break; 28824f1e32cSFrederic Weisbecker case HW_BREAKPOINT_W | HW_BREAKPOINT_R: 28924f1e32cSFrederic Weisbecker info->type = X86_BREAKPOINT_RW; 29024f1e32cSFrederic Weisbecker break; 29124f1e32cSFrederic Weisbecker case HW_BREAKPOINT_X: 29224f1e32cSFrederic Weisbecker info->type = X86_BREAKPOINT_EXECUTE; 29324f1e32cSFrederic Weisbecker break; 29424f1e32cSFrederic Weisbecker default: 29524f1e32cSFrederic Weisbecker return -EINVAL; 29624f1e32cSFrederic Weisbecker } 29724f1e32cSFrederic Weisbecker 29824f1e32cSFrederic Weisbecker return 0; 29924f1e32cSFrederic Weisbecker } 3000067f129SK.Prasad /* 3010067f129SK.Prasad * Validate the arch-specific HW Breakpoint register settings 3020067f129SK.Prasad */ 30324f1e32cSFrederic Weisbecker int arch_validate_hwbkpt_settings(struct perf_event *bp, 3040067f129SK.Prasad struct task_struct *tsk) 3050067f129SK.Prasad { 30624f1e32cSFrederic Weisbecker struct arch_hw_breakpoint *info = counter_arch_bp(bp); 3070067f129SK.Prasad unsigned int align; 30824f1e32cSFrederic Weisbecker int ret; 3090067f129SK.Prasad 31024f1e32cSFrederic Weisbecker 31124f1e32cSFrederic Weisbecker ret = arch_build_bp_info(bp); 31224f1e32cSFrederic Weisbecker if (ret) 31324f1e32cSFrederic Weisbecker return ret; 31424f1e32cSFrederic Weisbecker 31524f1e32cSFrederic Weisbecker ret = -EINVAL; 31624f1e32cSFrederic Weisbecker 31724f1e32cSFrederic Weisbecker if (info->type == X86_BREAKPOINT_EXECUTE) 3180067f129SK.Prasad /* 3190067f129SK.Prasad * Ptrace-refactoring code 3200067f129SK.Prasad * For now, we'll allow instruction breakpoint only for user-space 3210067f129SK.Prasad * addresses 3220067f129SK.Prasad */ 32324f1e32cSFrederic Weisbecker if ((!arch_check_va_in_userspace(info->address, info->len)) && 32424f1e32cSFrederic Weisbecker info->len != X86_BREAKPOINT_EXECUTE) 3250067f129SK.Prasad return ret; 3260067f129SK.Prasad 32724f1e32cSFrederic Weisbecker switch (info->len) { 32824f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_1: 3290067f129SK.Prasad align = 0; 3300067f129SK.Prasad break; 33124f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_2: 3320067f129SK.Prasad align = 1; 3330067f129SK.Prasad break; 33424f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_4: 3350067f129SK.Prasad align = 3; 3360067f129SK.Prasad break; 3370067f129SK.Prasad #ifdef CONFIG_X86_64 33824f1e32cSFrederic Weisbecker case X86_BREAKPOINT_LEN_8: 3390067f129SK.Prasad align = 7; 3400067f129SK.Prasad break; 3410067f129SK.Prasad #endif 3420067f129SK.Prasad default: 3430067f129SK.Prasad return ret; 3440067f129SK.Prasad } 3450067f129SK.Prasad 346*84d71092SFrederic Weisbecker /* 347*84d71092SFrederic Weisbecker * For kernel-addresses, either the address or symbol name can be 348*84d71092SFrederic Weisbecker * specified. 349*84d71092SFrederic Weisbecker */ 350*84d71092SFrederic Weisbecker if (info->name) 351*84d71092SFrederic Weisbecker info->address = (unsigned long) 352*84d71092SFrederic Weisbecker kallsyms_lookup_name(info->name); 3530067f129SK.Prasad /* 3540067f129SK.Prasad * Check that the low-order bits of the address are appropriate 3550067f129SK.Prasad * for the alignment implied by len. 3560067f129SK.Prasad */ 35724f1e32cSFrederic Weisbecker if (info->address & align) 3580067f129SK.Prasad return -EINVAL; 3590067f129SK.Prasad 3600067f129SK.Prasad /* Check that the virtual address is in the proper range */ 3610067f129SK.Prasad if (tsk) { 36224f1e32cSFrederic Weisbecker if (!arch_check_va_in_userspace(info->address, info->len)) 3630067f129SK.Prasad return -EFAULT; 3640067f129SK.Prasad } else { 36524f1e32cSFrederic Weisbecker if (!arch_check_va_in_kernelspace(info->address, info->len)) 3660067f129SK.Prasad return -EFAULT; 3670067f129SK.Prasad } 36824f1e32cSFrederic Weisbecker 3690067f129SK.Prasad return 0; 3700067f129SK.Prasad } 3710067f129SK.Prasad 37224f1e32cSFrederic Weisbecker /* 3739f6b3c2cSFrederic Weisbecker * Dump the debug register contents to the user. 3749f6b3c2cSFrederic Weisbecker * We can't dump our per cpu values because it 3759f6b3c2cSFrederic Weisbecker * may contain cpu wide breakpoint, something that 3769f6b3c2cSFrederic Weisbecker * doesn't belong to the current task. 3779f6b3c2cSFrederic Weisbecker * 3789f6b3c2cSFrederic Weisbecker * TODO: include non-ptrace user breakpoints (perf) 3799f6b3c2cSFrederic Weisbecker */ 3809f6b3c2cSFrederic Weisbecker void aout_dump_debugregs(struct user *dump) 3819f6b3c2cSFrederic Weisbecker { 3829f6b3c2cSFrederic Weisbecker int i; 3839f6b3c2cSFrederic Weisbecker int dr7 = 0; 3849f6b3c2cSFrederic Weisbecker struct perf_event *bp; 3859f6b3c2cSFrederic Weisbecker struct arch_hw_breakpoint *info; 3869f6b3c2cSFrederic Weisbecker struct thread_struct *thread = ¤t->thread; 3879f6b3c2cSFrederic Weisbecker 3889f6b3c2cSFrederic Weisbecker for (i = 0; i < HBP_NUM; i++) { 3899f6b3c2cSFrederic Weisbecker bp = thread->ptrace_bps[i]; 3909f6b3c2cSFrederic Weisbecker 3919f6b3c2cSFrederic Weisbecker if (bp && !bp->attr.disabled) { 3929f6b3c2cSFrederic Weisbecker dump->u_debugreg[i] = bp->attr.bp_addr; 3939f6b3c2cSFrederic Weisbecker info = counter_arch_bp(bp); 3949f6b3c2cSFrederic Weisbecker dr7 |= encode_dr7(i, info->len, info->type); 3959f6b3c2cSFrederic Weisbecker } else { 3969f6b3c2cSFrederic Weisbecker dump->u_debugreg[i] = 0; 3979f6b3c2cSFrederic Weisbecker } 3989f6b3c2cSFrederic Weisbecker } 3999f6b3c2cSFrederic Weisbecker 4009f6b3c2cSFrederic Weisbecker dump->u_debugreg[4] = 0; 4019f6b3c2cSFrederic Weisbecker dump->u_debugreg[5] = 0; 4029f6b3c2cSFrederic Weisbecker dump->u_debugreg[6] = current->thread.debugreg6; 4039f6b3c2cSFrederic Weisbecker 4049f6b3c2cSFrederic Weisbecker dump->u_debugreg[7] = dr7; 4059f6b3c2cSFrederic Weisbecker } 40668efa37dSIngo Molnar EXPORT_SYMBOL_GPL(aout_dump_debugregs); 4079f6b3c2cSFrederic Weisbecker 4089f6b3c2cSFrederic Weisbecker /* 40924f1e32cSFrederic Weisbecker * Release the user breakpoints used by ptrace 41024f1e32cSFrederic Weisbecker */ 41124f1e32cSFrederic Weisbecker void flush_ptrace_hw_breakpoint(struct task_struct *tsk) 4120067f129SK.Prasad { 4130067f129SK.Prasad int i; 41424f1e32cSFrederic Weisbecker struct thread_struct *t = &tsk->thread; 4150067f129SK.Prasad 41624f1e32cSFrederic Weisbecker for (i = 0; i < HBP_NUM; i++) { 41724f1e32cSFrederic Weisbecker unregister_hw_breakpoint(t->ptrace_bps[i]); 41824f1e32cSFrederic Weisbecker t->ptrace_bps[i] = NULL; 4190067f129SK.Prasad } 42024f1e32cSFrederic Weisbecker } 42124f1e32cSFrederic Weisbecker 42224f1e32cSFrederic Weisbecker void hw_breakpoint_restore(void) 42324f1e32cSFrederic Weisbecker { 42424f1e32cSFrederic Weisbecker set_debugreg(__get_cpu_var(cpu_debugreg[0]), 0); 42524f1e32cSFrederic Weisbecker set_debugreg(__get_cpu_var(cpu_debugreg[1]), 1); 42624f1e32cSFrederic Weisbecker set_debugreg(__get_cpu_var(cpu_debugreg[2]), 2); 42724f1e32cSFrederic Weisbecker set_debugreg(__get_cpu_var(cpu_debugreg[3]), 3); 42824f1e32cSFrederic Weisbecker set_debugreg(current->thread.debugreg6, 6); 42928b4e0d8STejun Heo set_debugreg(__get_cpu_var(cpu_dr7), 7); 43024f1e32cSFrederic Weisbecker } 43124f1e32cSFrederic Weisbecker EXPORT_SYMBOL_GPL(hw_breakpoint_restore); 4320067f129SK.Prasad 4330067f129SK.Prasad /* 4340067f129SK.Prasad * Handle debug exception notifications. 4350067f129SK.Prasad * 4360067f129SK.Prasad * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below. 4370067f129SK.Prasad * 4380067f129SK.Prasad * NOTIFY_DONE returned if one of the following conditions is true. 4390067f129SK.Prasad * i) When the causative address is from user-space and the exception 4400067f129SK.Prasad * is a valid one, i.e. not triggered as a result of lazy debug register 4410067f129SK.Prasad * switching 4420067f129SK.Prasad * ii) When there are more bits than trap<n> set in DR6 register (such 4430067f129SK.Prasad * as BD, BS or BT) indicating that more than one debug condition is 4440067f129SK.Prasad * met and requires some more action in do_debug(). 4450067f129SK.Prasad * 4460067f129SK.Prasad * NOTIFY_STOP returned for all other cases 4470067f129SK.Prasad * 4480067f129SK.Prasad */ 4494555835bSJaswinder Singh Rajput static int __kprobes hw_breakpoint_handler(struct die_args *args) 4500067f129SK.Prasad { 4510067f129SK.Prasad int i, cpu, rc = NOTIFY_STOP; 45224f1e32cSFrederic Weisbecker struct perf_event *bp; 45362edab90SK.Prasad unsigned long dr7, dr6; 45462edab90SK.Prasad unsigned long *dr6_p; 45562edab90SK.Prasad 45662edab90SK.Prasad /* The DR6 value is pointed by args->err */ 45762edab90SK.Prasad dr6_p = (unsigned long *)ERR_PTR(args->err); 45862edab90SK.Prasad dr6 = *dr6_p; 4590067f129SK.Prasad 4600067f129SK.Prasad /* Do an early return if no trap bits are set in DR6 */ 4610067f129SK.Prasad if ((dr6 & DR_TRAP_BITS) == 0) 4620067f129SK.Prasad return NOTIFY_DONE; 4630067f129SK.Prasad 4640067f129SK.Prasad get_debugreg(dr7, 7); 4650067f129SK.Prasad /* Disable breakpoints during exception handling */ 4660067f129SK.Prasad set_debugreg(0UL, 7); 4670067f129SK.Prasad /* 4680067f129SK.Prasad * Assert that local interrupts are disabled 4690067f129SK.Prasad * Reset the DRn bits in the virtualized register value. 4700067f129SK.Prasad * The ptrace trigger routine will add in whatever is needed. 4710067f129SK.Prasad */ 4720067f129SK.Prasad current->thread.debugreg6 &= ~DR_TRAP_BITS; 4730067f129SK.Prasad cpu = get_cpu(); 4740067f129SK.Prasad 4750067f129SK.Prasad /* Handle all the breakpoints that were triggered */ 4760067f129SK.Prasad for (i = 0; i < HBP_NUM; ++i) { 4770067f129SK.Prasad if (likely(!(dr6 & (DR_TRAP0 << i)))) 4780067f129SK.Prasad continue; 47924f1e32cSFrederic Weisbecker 4800067f129SK.Prasad /* 48124f1e32cSFrederic Weisbecker * The counter may be concurrently released but that can only 48224f1e32cSFrederic Weisbecker * occur from a call_rcu() path. We can then safely fetch 48324f1e32cSFrederic Weisbecker * the breakpoint, use its callback, touch its counter 48424f1e32cSFrederic Weisbecker * while we are in an rcu_read_lock() path. 4850067f129SK.Prasad */ 48624f1e32cSFrederic Weisbecker rcu_read_lock(); 48724f1e32cSFrederic Weisbecker 48824f1e32cSFrederic Weisbecker bp = per_cpu(bp_per_reg[i], cpu); 4890067f129SK.Prasad if (bp) 4900067f129SK.Prasad rc = NOTIFY_DONE; 4910067f129SK.Prasad /* 49262edab90SK.Prasad * Reset the 'i'th TRAP bit in dr6 to denote completion of 49362edab90SK.Prasad * exception handling 49462edab90SK.Prasad */ 49562edab90SK.Prasad (*dr6_p) &= ~(DR_TRAP0 << i); 49662edab90SK.Prasad /* 4970067f129SK.Prasad * bp can be NULL due to lazy debug register switching 49824f1e32cSFrederic Weisbecker * or due to concurrent perf counter removing. 4990067f129SK.Prasad */ 50024f1e32cSFrederic Weisbecker if (!bp) { 50124f1e32cSFrederic Weisbecker rcu_read_unlock(); 50224f1e32cSFrederic Weisbecker break; 50324f1e32cSFrederic Weisbecker } 5040067f129SK.Prasad 505b326e956SFrederic Weisbecker perf_bp_event(bp, args->regs); 50624f1e32cSFrederic Weisbecker 50724f1e32cSFrederic Weisbecker rcu_read_unlock(); 5080067f129SK.Prasad } 5090067f129SK.Prasad if (dr6 & (~DR_TRAP_BITS)) 5100067f129SK.Prasad rc = NOTIFY_DONE; 5110067f129SK.Prasad 5120067f129SK.Prasad set_debugreg(dr7, 7); 513eadb8a09SIngo Molnar put_cpu(); 51424f1e32cSFrederic Weisbecker 5150067f129SK.Prasad return rc; 5160067f129SK.Prasad } 5170067f129SK.Prasad 5180067f129SK.Prasad /* 5190067f129SK.Prasad * Handle debug exception notifications. 5200067f129SK.Prasad */ 5210067f129SK.Prasad int __kprobes hw_breakpoint_exceptions_notify( 5220067f129SK.Prasad struct notifier_block *unused, unsigned long val, void *data) 5230067f129SK.Prasad { 5240067f129SK.Prasad if (val != DIE_DEBUG) 5250067f129SK.Prasad return NOTIFY_DONE; 5260067f129SK.Prasad 5270067f129SK.Prasad return hw_breakpoint_handler(data); 5280067f129SK.Prasad } 52924f1e32cSFrederic Weisbecker 53024f1e32cSFrederic Weisbecker void hw_breakpoint_pmu_read(struct perf_event *bp) 53124f1e32cSFrederic Weisbecker { 53224f1e32cSFrederic Weisbecker /* TODO */ 53324f1e32cSFrederic Weisbecker } 53424f1e32cSFrederic Weisbecker 53524f1e32cSFrederic Weisbecker void hw_breakpoint_pmu_unthrottle(struct perf_event *bp) 53624f1e32cSFrederic Weisbecker { 53724f1e32cSFrederic Weisbecker /* TODO */ 53824f1e32cSFrederic Weisbecker } 539