xref: /linux/arch/x86/kernel/hw_breakpoint.c (revision 24f1e32c60c45c89a997c73395b69c8af6f0a84e)
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
18*24f1e32cSFrederic Weisbecker  * Copyright (C) 2009 Frederic Weisbecker <fweisbec@gmail.com>
190067f129SK.Prasad  */
200067f129SK.Prasad 
210067f129SK.Prasad /*
220067f129SK.Prasad  * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
230067f129SK.Prasad  * using the CPU's debug registers.
240067f129SK.Prasad  */
250067f129SK.Prasad 
26*24f1e32cSFrederic Weisbecker #include <linux/perf_event.h>
27*24f1e32cSFrederic Weisbecker #include <linux/hw_breakpoint.h>
280067f129SK.Prasad #include <linux/irqflags.h>
290067f129SK.Prasad #include <linux/notifier.h>
300067f129SK.Prasad #include <linux/kallsyms.h>
310067f129SK.Prasad #include <linux/kprobes.h>
320067f129SK.Prasad #include <linux/percpu.h>
330067f129SK.Prasad #include <linux/kdebug.h>
340067f129SK.Prasad #include <linux/kernel.h>
350067f129SK.Prasad #include <linux/module.h>
360067f129SK.Prasad #include <linux/sched.h>
370067f129SK.Prasad #include <linux/init.h>
380067f129SK.Prasad #include <linux/smp.h>
390067f129SK.Prasad 
400067f129SK.Prasad #include <asm/hw_breakpoint.h>
410067f129SK.Prasad #include <asm/processor.h>
420067f129SK.Prasad #include <asm/debugreg.h>
430067f129SK.Prasad 
44*24f1e32cSFrederic Weisbecker /* Per cpu debug control register value */
45*24f1e32cSFrederic Weisbecker DEFINE_PER_CPU(unsigned long, dr7);
46*24f1e32cSFrederic Weisbecker 
47*24f1e32cSFrederic Weisbecker /* Per cpu debug address registers values */
48*24f1e32cSFrederic Weisbecker static DEFINE_PER_CPU(unsigned long, cpu_debugreg[HBP_NUM]);
490067f129SK.Prasad 
500067f129SK.Prasad /*
51*24f1e32cSFrederic Weisbecker  * Stores the breakpoints currently in use on each breakpoint address
52*24f1e32cSFrederic Weisbecker  * register for each cpus
530067f129SK.Prasad  */
54*24f1e32cSFrederic Weisbecker static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]);
550067f129SK.Prasad 
560067f129SK.Prasad 
570067f129SK.Prasad /*
580067f129SK.Prasad  * Encode the length, type, Exact, and Enable bits for a particular breakpoint
590067f129SK.Prasad  * as stored in debug register 7.
600067f129SK.Prasad  */
61*24f1e32cSFrederic Weisbecker unsigned long encode_dr7(int drnum, unsigned int len, unsigned int type)
620067f129SK.Prasad {
630067f129SK.Prasad 	unsigned long bp_info;
640067f129SK.Prasad 
650067f129SK.Prasad 	bp_info = (len | type) & 0xf;
660067f129SK.Prasad 	bp_info <<= (DR_CONTROL_SHIFT + drnum * DR_CONTROL_SIZE);
670067f129SK.Prasad 	bp_info |= (DR_GLOBAL_ENABLE << (drnum * DR_ENABLE_SIZE)) |
680067f129SK.Prasad 				DR_GLOBAL_SLOWDOWN;
690067f129SK.Prasad 	return bp_info;
700067f129SK.Prasad }
710067f129SK.Prasad 
72*24f1e32cSFrederic Weisbecker /*
73*24f1e32cSFrederic Weisbecker  * Decode the length and type bits for a particular breakpoint as
74*24f1e32cSFrederic Weisbecker  * stored in debug register 7.  Return the "enabled" status.
750067f129SK.Prasad  */
76*24f1e32cSFrederic Weisbecker int decode_dr7(unsigned long dr7, int bpnum, unsigned *len, unsigned *type)
77*24f1e32cSFrederic Weisbecker {
78*24f1e32cSFrederic Weisbecker 	int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE);
79*24f1e32cSFrederic Weisbecker 
80*24f1e32cSFrederic Weisbecker 	*len = (bp_info & 0xc) | 0x40;
81*24f1e32cSFrederic Weisbecker 	*type = (bp_info & 0x3) | 0x80;
82*24f1e32cSFrederic Weisbecker 
83*24f1e32cSFrederic Weisbecker 	return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3;
840067f129SK.Prasad }
850067f129SK.Prasad 
860067f129SK.Prasad /*
87*24f1e32cSFrederic Weisbecker  * Install a perf counter breakpoint.
88*24f1e32cSFrederic Weisbecker  *
89*24f1e32cSFrederic Weisbecker  * We seek a free debug address register and use it for this
90*24f1e32cSFrederic Weisbecker  * breakpoint. Eventually we enable it in the debug control register.
91*24f1e32cSFrederic Weisbecker  *
92*24f1e32cSFrederic Weisbecker  * Atomic: we hold the counter->ctx->lock and we only handle variables
93*24f1e32cSFrederic Weisbecker  * and registers local to this cpu.
940067f129SK.Prasad  */
95*24f1e32cSFrederic Weisbecker int arch_install_hw_breakpoint(struct perf_event *bp)
960067f129SK.Prasad {
97*24f1e32cSFrederic Weisbecker 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
98*24f1e32cSFrederic Weisbecker 	unsigned long *dr7;
99*24f1e32cSFrederic Weisbecker 	int i;
1000067f129SK.Prasad 
101*24f1e32cSFrederic Weisbecker 	for (i = 0; i < HBP_NUM; i++) {
102*24f1e32cSFrederic Weisbecker 		struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]);
103*24f1e32cSFrederic Weisbecker 
104*24f1e32cSFrederic Weisbecker 		if (!*slot) {
105*24f1e32cSFrederic Weisbecker 			*slot = bp;
1060067f129SK.Prasad 			break;
1070067f129SK.Prasad 		}
108*24f1e32cSFrederic Weisbecker 	}
1090067f129SK.Prasad 
110*24f1e32cSFrederic Weisbecker 	if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
111*24f1e32cSFrederic Weisbecker 		return -EBUSY;
112*24f1e32cSFrederic Weisbecker 
113*24f1e32cSFrederic Weisbecker 	set_debugreg(info->address, i);
114*24f1e32cSFrederic Weisbecker 	__get_cpu_var(cpu_debugreg[i]) = info->address;
115*24f1e32cSFrederic Weisbecker 
116*24f1e32cSFrederic Weisbecker 	dr7 = &__get_cpu_var(dr7);
117*24f1e32cSFrederic Weisbecker 	*dr7 |= encode_dr7(i, info->len, info->type);
118*24f1e32cSFrederic Weisbecker 
119*24f1e32cSFrederic Weisbecker 	set_debugreg(*dr7, 7);
120*24f1e32cSFrederic Weisbecker 
121*24f1e32cSFrederic Weisbecker 	return 0;
1220067f129SK.Prasad }
1230067f129SK.Prasad 
1240067f129SK.Prasad /*
125*24f1e32cSFrederic Weisbecker  * Uninstall the breakpoint contained in the given counter.
126*24f1e32cSFrederic Weisbecker  *
127*24f1e32cSFrederic Weisbecker  * First we search the debug address register it uses and then we disable
128*24f1e32cSFrederic Weisbecker  * it.
129*24f1e32cSFrederic Weisbecker  *
130*24f1e32cSFrederic Weisbecker  * Atomic: we hold the counter->ctx->lock and we only handle variables
131*24f1e32cSFrederic Weisbecker  * and registers local to this cpu.
1320067f129SK.Prasad  */
133*24f1e32cSFrederic Weisbecker void arch_uninstall_hw_breakpoint(struct perf_event *bp)
1340067f129SK.Prasad {
135*24f1e32cSFrederic Weisbecker 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
136*24f1e32cSFrederic Weisbecker 	unsigned long *dr7;
137*24f1e32cSFrederic Weisbecker 	int i;
1380067f129SK.Prasad 
139*24f1e32cSFrederic Weisbecker 	for (i = 0; i < HBP_NUM; i++) {
140*24f1e32cSFrederic Weisbecker 		struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]);
141*24f1e32cSFrederic Weisbecker 
142*24f1e32cSFrederic Weisbecker 		if (*slot == bp) {
143*24f1e32cSFrederic Weisbecker 			*slot = NULL;
144*24f1e32cSFrederic Weisbecker 			break;
145*24f1e32cSFrederic Weisbecker 		}
146*24f1e32cSFrederic Weisbecker 	}
147*24f1e32cSFrederic Weisbecker 
148*24f1e32cSFrederic Weisbecker 	if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
149*24f1e32cSFrederic Weisbecker 		return;
150*24f1e32cSFrederic Weisbecker 
151*24f1e32cSFrederic Weisbecker 	dr7 = &__get_cpu_var(dr7);
152*24f1e32cSFrederic Weisbecker 	*dr7 &= ~encode_dr7(i, info->len, info->type);
153*24f1e32cSFrederic Weisbecker 
154*24f1e32cSFrederic Weisbecker 	set_debugreg(*dr7, 7);
1550067f129SK.Prasad }
1560067f129SK.Prasad 
1570067f129SK.Prasad static int get_hbp_len(u8 hbp_len)
1580067f129SK.Prasad {
1590067f129SK.Prasad 	unsigned int len_in_bytes = 0;
1600067f129SK.Prasad 
1610067f129SK.Prasad 	switch (hbp_len) {
162*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_1:
1630067f129SK.Prasad 		len_in_bytes = 1;
1640067f129SK.Prasad 		break;
165*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_2:
1660067f129SK.Prasad 		len_in_bytes = 2;
1670067f129SK.Prasad 		break;
168*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_4:
1690067f129SK.Prasad 		len_in_bytes = 4;
1700067f129SK.Prasad 		break;
1710067f129SK.Prasad #ifdef CONFIG_X86_64
172*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_8:
1730067f129SK.Prasad 		len_in_bytes = 8;
1740067f129SK.Prasad 		break;
1750067f129SK.Prasad #endif
1760067f129SK.Prasad 	}
1770067f129SK.Prasad 	return len_in_bytes;
1780067f129SK.Prasad }
1790067f129SK.Prasad 
1800067f129SK.Prasad /*
1810067f129SK.Prasad  * Check for virtual address in user space.
1820067f129SK.Prasad  */
1830067f129SK.Prasad int arch_check_va_in_userspace(unsigned long va, u8 hbp_len)
1840067f129SK.Prasad {
1850067f129SK.Prasad 	unsigned int len;
1860067f129SK.Prasad 
1870067f129SK.Prasad 	len = get_hbp_len(hbp_len);
1880067f129SK.Prasad 
1890067f129SK.Prasad 	return (va <= TASK_SIZE - len);
1900067f129SK.Prasad }
1910067f129SK.Prasad 
1920067f129SK.Prasad /*
1930067f129SK.Prasad  * Check for virtual address in kernel space.
1940067f129SK.Prasad  */
1954555835bSJaswinder Singh Rajput static int arch_check_va_in_kernelspace(unsigned long va, u8 hbp_len)
1960067f129SK.Prasad {
1970067f129SK.Prasad 	unsigned int len;
1980067f129SK.Prasad 
1990067f129SK.Prasad 	len = get_hbp_len(hbp_len);
2000067f129SK.Prasad 
2010067f129SK.Prasad 	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
2020067f129SK.Prasad }
2030067f129SK.Prasad 
2040067f129SK.Prasad /*
2050067f129SK.Prasad  * Store a breakpoint's encoded address, length, and type.
2060067f129SK.Prasad  */
207*24f1e32cSFrederic Weisbecker static int arch_store_info(struct perf_event *bp)
2080067f129SK.Prasad {
209*24f1e32cSFrederic Weisbecker 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
2100067f129SK.Prasad 	/*
2110067f129SK.Prasad 	 * For kernel-addresses, either the address or symbol name can be
2120067f129SK.Prasad 	 * specified.
2130067f129SK.Prasad 	 */
214*24f1e32cSFrederic Weisbecker 	if (info->name)
215*24f1e32cSFrederic Weisbecker 		info->address = (unsigned long)
216*24f1e32cSFrederic Weisbecker 				kallsyms_lookup_name(info->name);
217*24f1e32cSFrederic Weisbecker 	if (info->address)
2180067f129SK.Prasad 		return 0;
219*24f1e32cSFrederic Weisbecker 
2200067f129SK.Prasad 	return -EINVAL;
2210067f129SK.Prasad }
2220067f129SK.Prasad 
223*24f1e32cSFrederic Weisbecker int arch_bp_generic_fields(int x86_len, int x86_type,
224*24f1e32cSFrederic Weisbecker 			   int *gen_len, int *gen_type)
225*24f1e32cSFrederic Weisbecker {
226*24f1e32cSFrederic Weisbecker 	/* Len */
227*24f1e32cSFrederic Weisbecker 	switch (x86_len) {
228*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_1:
229*24f1e32cSFrederic Weisbecker 		*gen_len = HW_BREAKPOINT_LEN_1;
230*24f1e32cSFrederic Weisbecker 		break;
231*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_2:
232*24f1e32cSFrederic Weisbecker 		*gen_len = HW_BREAKPOINT_LEN_2;
233*24f1e32cSFrederic Weisbecker 		break;
234*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_4:
235*24f1e32cSFrederic Weisbecker 		*gen_len = HW_BREAKPOINT_LEN_4;
236*24f1e32cSFrederic Weisbecker 		break;
237*24f1e32cSFrederic Weisbecker #ifdef CONFIG_X86_64
238*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_8:
239*24f1e32cSFrederic Weisbecker 		*gen_len = HW_BREAKPOINT_LEN_8;
240*24f1e32cSFrederic Weisbecker 		break;
241*24f1e32cSFrederic Weisbecker #endif
242*24f1e32cSFrederic Weisbecker 	default:
243*24f1e32cSFrederic Weisbecker 		return -EINVAL;
244*24f1e32cSFrederic Weisbecker 	}
245*24f1e32cSFrederic Weisbecker 
246*24f1e32cSFrederic Weisbecker 	/* Type */
247*24f1e32cSFrederic Weisbecker 	switch (x86_type) {
248*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_EXECUTE:
249*24f1e32cSFrederic Weisbecker 		*gen_type = HW_BREAKPOINT_X;
250*24f1e32cSFrederic Weisbecker 		break;
251*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_WRITE:
252*24f1e32cSFrederic Weisbecker 		*gen_type = HW_BREAKPOINT_W;
253*24f1e32cSFrederic Weisbecker 		break;
254*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_RW:
255*24f1e32cSFrederic Weisbecker 		*gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
256*24f1e32cSFrederic Weisbecker 		break;
257*24f1e32cSFrederic Weisbecker 	default:
258*24f1e32cSFrederic Weisbecker 		return -EINVAL;
259*24f1e32cSFrederic Weisbecker 	}
260*24f1e32cSFrederic Weisbecker 
261*24f1e32cSFrederic Weisbecker 	return 0;
262*24f1e32cSFrederic Weisbecker }
263*24f1e32cSFrederic Weisbecker 
264*24f1e32cSFrederic Weisbecker 
265*24f1e32cSFrederic Weisbecker static int arch_build_bp_info(struct perf_event *bp)
266*24f1e32cSFrederic Weisbecker {
267*24f1e32cSFrederic Weisbecker 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
268*24f1e32cSFrederic Weisbecker 
269*24f1e32cSFrederic Weisbecker 	info->address = bp->attr.bp_addr;
270*24f1e32cSFrederic Weisbecker 
271*24f1e32cSFrederic Weisbecker 	/* Len */
272*24f1e32cSFrederic Weisbecker 	switch (bp->attr.bp_len) {
273*24f1e32cSFrederic Weisbecker 	case HW_BREAKPOINT_LEN_1:
274*24f1e32cSFrederic Weisbecker 		info->len = X86_BREAKPOINT_LEN_1;
275*24f1e32cSFrederic Weisbecker 		break;
276*24f1e32cSFrederic Weisbecker 	case HW_BREAKPOINT_LEN_2:
277*24f1e32cSFrederic Weisbecker 		info->len = X86_BREAKPOINT_LEN_2;
278*24f1e32cSFrederic Weisbecker 		break;
279*24f1e32cSFrederic Weisbecker 	case HW_BREAKPOINT_LEN_4:
280*24f1e32cSFrederic Weisbecker 		info->len = X86_BREAKPOINT_LEN_4;
281*24f1e32cSFrederic Weisbecker 		break;
282*24f1e32cSFrederic Weisbecker #ifdef CONFIG_X86_64
283*24f1e32cSFrederic Weisbecker 	case HW_BREAKPOINT_LEN_8:
284*24f1e32cSFrederic Weisbecker 		info->len = X86_BREAKPOINT_LEN_8;
285*24f1e32cSFrederic Weisbecker 		break;
286*24f1e32cSFrederic Weisbecker #endif
287*24f1e32cSFrederic Weisbecker 	default:
288*24f1e32cSFrederic Weisbecker 		return -EINVAL;
289*24f1e32cSFrederic Weisbecker 	}
290*24f1e32cSFrederic Weisbecker 
291*24f1e32cSFrederic Weisbecker 	/* Type */
292*24f1e32cSFrederic Weisbecker 	switch (bp->attr.bp_type) {
293*24f1e32cSFrederic Weisbecker 	case HW_BREAKPOINT_W:
294*24f1e32cSFrederic Weisbecker 		info->type = X86_BREAKPOINT_WRITE;
295*24f1e32cSFrederic Weisbecker 		break;
296*24f1e32cSFrederic Weisbecker 	case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
297*24f1e32cSFrederic Weisbecker 		info->type = X86_BREAKPOINT_RW;
298*24f1e32cSFrederic Weisbecker 		break;
299*24f1e32cSFrederic Weisbecker 	case HW_BREAKPOINT_X:
300*24f1e32cSFrederic Weisbecker 		info->type = X86_BREAKPOINT_EXECUTE;
301*24f1e32cSFrederic Weisbecker 		break;
302*24f1e32cSFrederic Weisbecker 	default:
303*24f1e32cSFrederic Weisbecker 		return -EINVAL;
304*24f1e32cSFrederic Weisbecker 	}
305*24f1e32cSFrederic Weisbecker 
306*24f1e32cSFrederic Weisbecker 	return 0;
307*24f1e32cSFrederic Weisbecker }
3080067f129SK.Prasad /*
3090067f129SK.Prasad  * Validate the arch-specific HW Breakpoint register settings
3100067f129SK.Prasad  */
311*24f1e32cSFrederic Weisbecker int arch_validate_hwbkpt_settings(struct perf_event *bp,
3120067f129SK.Prasad 				  struct task_struct *tsk)
3130067f129SK.Prasad {
314*24f1e32cSFrederic Weisbecker 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
3150067f129SK.Prasad 	unsigned int align;
316*24f1e32cSFrederic Weisbecker 	int ret;
3170067f129SK.Prasad 
318*24f1e32cSFrederic Weisbecker 
319*24f1e32cSFrederic Weisbecker 	ret = arch_build_bp_info(bp);
320*24f1e32cSFrederic Weisbecker 	if (ret)
321*24f1e32cSFrederic Weisbecker 		return ret;
322*24f1e32cSFrederic Weisbecker 
323*24f1e32cSFrederic Weisbecker 	ret = -EINVAL;
324*24f1e32cSFrederic Weisbecker 
325*24f1e32cSFrederic Weisbecker 	if (info->type == X86_BREAKPOINT_EXECUTE)
3260067f129SK.Prasad 		/*
3270067f129SK.Prasad 		 * Ptrace-refactoring code
3280067f129SK.Prasad 		 * For now, we'll allow instruction breakpoint only for user-space
3290067f129SK.Prasad 		 * addresses
3300067f129SK.Prasad 		 */
331*24f1e32cSFrederic Weisbecker 		if ((!arch_check_va_in_userspace(info->address, info->len)) &&
332*24f1e32cSFrederic Weisbecker 			info->len != X86_BREAKPOINT_EXECUTE)
3330067f129SK.Prasad 			return ret;
3340067f129SK.Prasad 
335*24f1e32cSFrederic Weisbecker 	switch (info->len) {
336*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_1:
3370067f129SK.Prasad 		align = 0;
3380067f129SK.Prasad 		break;
339*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_2:
3400067f129SK.Prasad 		align = 1;
3410067f129SK.Prasad 		break;
342*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_4:
3430067f129SK.Prasad 		align = 3;
3440067f129SK.Prasad 		break;
3450067f129SK.Prasad #ifdef CONFIG_X86_64
346*24f1e32cSFrederic Weisbecker 	case X86_BREAKPOINT_LEN_8:
3470067f129SK.Prasad 		align = 7;
3480067f129SK.Prasad 		break;
3490067f129SK.Prasad #endif
3500067f129SK.Prasad 	default:
3510067f129SK.Prasad 		return ret;
3520067f129SK.Prasad 	}
3530067f129SK.Prasad 
354*24f1e32cSFrederic Weisbecker 	if (bp->callback)
355*24f1e32cSFrederic Weisbecker 		ret = arch_store_info(bp);
3560067f129SK.Prasad 
3570067f129SK.Prasad 	if (ret < 0)
3580067f129SK.Prasad 		return ret;
3590067f129SK.Prasad 	/*
3600067f129SK.Prasad 	 * Check that the low-order bits of the address are appropriate
3610067f129SK.Prasad 	 * for the alignment implied by len.
3620067f129SK.Prasad 	 */
363*24f1e32cSFrederic Weisbecker 	if (info->address & align)
3640067f129SK.Prasad 		return -EINVAL;
3650067f129SK.Prasad 
3660067f129SK.Prasad 	/* Check that the virtual address is in the proper range */
3670067f129SK.Prasad 	if (tsk) {
368*24f1e32cSFrederic Weisbecker 		if (!arch_check_va_in_userspace(info->address, info->len))
3690067f129SK.Prasad 			return -EFAULT;
3700067f129SK.Prasad 	} else {
371*24f1e32cSFrederic Weisbecker 		if (!arch_check_va_in_kernelspace(info->address, info->len))
3720067f129SK.Prasad 			return -EFAULT;
3730067f129SK.Prasad 	}
374*24f1e32cSFrederic Weisbecker 
3750067f129SK.Prasad 	return 0;
3760067f129SK.Prasad }
3770067f129SK.Prasad 
378*24f1e32cSFrederic Weisbecker /*
379*24f1e32cSFrederic Weisbecker  * Release the user breakpoints used by ptrace
380*24f1e32cSFrederic Weisbecker  */
381*24f1e32cSFrederic Weisbecker void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
3820067f129SK.Prasad {
3830067f129SK.Prasad 	int i;
384*24f1e32cSFrederic Weisbecker 	struct thread_struct *t = &tsk->thread;
3850067f129SK.Prasad 
386*24f1e32cSFrederic Weisbecker 	for (i = 0; i < HBP_NUM; i++) {
387*24f1e32cSFrederic Weisbecker 		unregister_hw_breakpoint(t->ptrace_bps[i]);
388*24f1e32cSFrederic Weisbecker 		t->ptrace_bps[i] = NULL;
3890067f129SK.Prasad 	}
390*24f1e32cSFrederic Weisbecker }
391*24f1e32cSFrederic Weisbecker 
392*24f1e32cSFrederic Weisbecker #ifdef CONFIG_KVM
393*24f1e32cSFrederic Weisbecker void hw_breakpoint_restore(void)
394*24f1e32cSFrederic Weisbecker {
395*24f1e32cSFrederic Weisbecker 	set_debugreg(__get_cpu_var(cpu_debugreg[0]), 0);
396*24f1e32cSFrederic Weisbecker 	set_debugreg(__get_cpu_var(cpu_debugreg[1]), 1);
397*24f1e32cSFrederic Weisbecker 	set_debugreg(__get_cpu_var(cpu_debugreg[2]), 2);
398*24f1e32cSFrederic Weisbecker 	set_debugreg(__get_cpu_var(cpu_debugreg[3]), 3);
399*24f1e32cSFrederic Weisbecker 	set_debugreg(current->thread.debugreg6, 6);
400*24f1e32cSFrederic Weisbecker 	set_debugreg(__get_cpu_var(dr7), 7);
401*24f1e32cSFrederic Weisbecker }
402*24f1e32cSFrederic Weisbecker EXPORT_SYMBOL_GPL(hw_breakpoint_restore);
403*24f1e32cSFrederic Weisbecker #endif
4040067f129SK.Prasad 
4050067f129SK.Prasad /*
4060067f129SK.Prasad  * Handle debug exception notifications.
4070067f129SK.Prasad  *
4080067f129SK.Prasad  * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below.
4090067f129SK.Prasad  *
4100067f129SK.Prasad  * NOTIFY_DONE returned if one of the following conditions is true.
4110067f129SK.Prasad  * i) When the causative address is from user-space and the exception
4120067f129SK.Prasad  * is a valid one, i.e. not triggered as a result of lazy debug register
4130067f129SK.Prasad  * switching
4140067f129SK.Prasad  * ii) When there are more bits than trap<n> set in DR6 register (such
4150067f129SK.Prasad  * as BD, BS or BT) indicating that more than one debug condition is
4160067f129SK.Prasad  * met and requires some more action in do_debug().
4170067f129SK.Prasad  *
4180067f129SK.Prasad  * NOTIFY_STOP returned for all other cases
4190067f129SK.Prasad  *
4200067f129SK.Prasad  */
4214555835bSJaswinder Singh Rajput static int __kprobes hw_breakpoint_handler(struct die_args *args)
4220067f129SK.Prasad {
4230067f129SK.Prasad 	int i, cpu, rc = NOTIFY_STOP;
424*24f1e32cSFrederic Weisbecker 	struct perf_event *bp;
42562edab90SK.Prasad 	unsigned long dr7, dr6;
42662edab90SK.Prasad 	unsigned long *dr6_p;
42762edab90SK.Prasad 
42862edab90SK.Prasad 	/* The DR6 value is pointed by args->err */
42962edab90SK.Prasad 	dr6_p = (unsigned long *)ERR_PTR(args->err);
43062edab90SK.Prasad 	dr6 = *dr6_p;
4310067f129SK.Prasad 
4320067f129SK.Prasad 	/* Do an early return if no trap bits are set in DR6 */
4330067f129SK.Prasad 	if ((dr6 & DR_TRAP_BITS) == 0)
4340067f129SK.Prasad 		return NOTIFY_DONE;
4350067f129SK.Prasad 
4360067f129SK.Prasad 	get_debugreg(dr7, 7);
4370067f129SK.Prasad 	/* Disable breakpoints during exception handling */
4380067f129SK.Prasad 	set_debugreg(0UL, 7);
4390067f129SK.Prasad 	/*
4400067f129SK.Prasad 	 * Assert that local interrupts are disabled
4410067f129SK.Prasad 	 * Reset the DRn bits in the virtualized register value.
4420067f129SK.Prasad 	 * The ptrace trigger routine will add in whatever is needed.
4430067f129SK.Prasad 	 */
4440067f129SK.Prasad 	current->thread.debugreg6 &= ~DR_TRAP_BITS;
4450067f129SK.Prasad 	cpu = get_cpu();
4460067f129SK.Prasad 
4470067f129SK.Prasad 	/* Handle all the breakpoints that were triggered */
4480067f129SK.Prasad 	for (i = 0; i < HBP_NUM; ++i) {
4490067f129SK.Prasad 		if (likely(!(dr6 & (DR_TRAP0 << i))))
4500067f129SK.Prasad 			continue;
451*24f1e32cSFrederic Weisbecker 
4520067f129SK.Prasad 		/*
453*24f1e32cSFrederic Weisbecker 		 * The counter may be concurrently released but that can only
454*24f1e32cSFrederic Weisbecker 		 * occur from a call_rcu() path. We can then safely fetch
455*24f1e32cSFrederic Weisbecker 		 * the breakpoint, use its callback, touch its counter
456*24f1e32cSFrederic Weisbecker 		 * while we are in an rcu_read_lock() path.
4570067f129SK.Prasad 		 */
458*24f1e32cSFrederic Weisbecker 		rcu_read_lock();
459*24f1e32cSFrederic Weisbecker 
460*24f1e32cSFrederic Weisbecker 		bp = per_cpu(bp_per_reg[i], cpu);
4610067f129SK.Prasad 		if (bp)
4620067f129SK.Prasad 			rc = NOTIFY_DONE;
4630067f129SK.Prasad 		/*
46462edab90SK.Prasad 		 * Reset the 'i'th TRAP bit in dr6 to denote completion of
46562edab90SK.Prasad 		 * exception handling
46662edab90SK.Prasad 		 */
46762edab90SK.Prasad 		(*dr6_p) &= ~(DR_TRAP0 << i);
46862edab90SK.Prasad 		/*
4690067f129SK.Prasad 		 * bp can be NULL due to lazy debug register switching
470*24f1e32cSFrederic Weisbecker 		 * or due to concurrent perf counter removing.
4710067f129SK.Prasad 		 */
472*24f1e32cSFrederic Weisbecker 		if (!bp) {
473*24f1e32cSFrederic Weisbecker 			rcu_read_unlock();
474*24f1e32cSFrederic Weisbecker 			break;
475*24f1e32cSFrederic Weisbecker 		}
4760067f129SK.Prasad 
477*24f1e32cSFrederic Weisbecker 		(bp->callback)(bp, args->regs);
478*24f1e32cSFrederic Weisbecker 
479*24f1e32cSFrederic Weisbecker 		rcu_read_unlock();
4800067f129SK.Prasad 	}
4810067f129SK.Prasad 	if (dr6 & (~DR_TRAP_BITS))
4820067f129SK.Prasad 		rc = NOTIFY_DONE;
4830067f129SK.Prasad 
4840067f129SK.Prasad 	set_debugreg(dr7, 7);
485eadb8a09SIngo Molnar 	put_cpu();
486*24f1e32cSFrederic Weisbecker 
4870067f129SK.Prasad 	return rc;
4880067f129SK.Prasad }
4890067f129SK.Prasad 
4900067f129SK.Prasad /*
4910067f129SK.Prasad  * Handle debug exception notifications.
4920067f129SK.Prasad  */
4930067f129SK.Prasad int __kprobes hw_breakpoint_exceptions_notify(
4940067f129SK.Prasad 		struct notifier_block *unused, unsigned long val, void *data)
4950067f129SK.Prasad {
4960067f129SK.Prasad 	if (val != DIE_DEBUG)
4970067f129SK.Prasad 		return NOTIFY_DONE;
4980067f129SK.Prasad 
4990067f129SK.Prasad 	return hw_breakpoint_handler(data);
5000067f129SK.Prasad }
501*24f1e32cSFrederic Weisbecker 
502*24f1e32cSFrederic Weisbecker void hw_breakpoint_pmu_read(struct perf_event *bp)
503*24f1e32cSFrederic Weisbecker {
504*24f1e32cSFrederic Weisbecker 	/* TODO */
505*24f1e32cSFrederic Weisbecker }
506*24f1e32cSFrederic Weisbecker 
507*24f1e32cSFrederic Weisbecker void hw_breakpoint_pmu_unthrottle(struct perf_event *bp)
508*24f1e32cSFrederic Weisbecker {
509*24f1e32cSFrederic Weisbecker 	/* TODO */
510*24f1e32cSFrederic Weisbecker }
511