| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2007 Alan Stern | 
					
						
							|  |  |  |  * Copyright (C) 2009 IBM Corporation | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  |  * Copyright (C) 2009 Frederic Weisbecker <fweisbec@gmail.com> | 
					
						
							| 
									
										
										
										
											2009-11-23 21:17:13 +05:30
										 |  |  |  * | 
					
						
							|  |  |  |  * Authors: Alan Stern <stern@rowland.harvard.edu> | 
					
						
							|  |  |  |  *          K.Prasad <prasad@linux.vnet.ibm.com> | 
					
						
							|  |  |  |  *          Frederic Weisbecker <fweisbec@gmail.com> | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility, | 
					
						
							|  |  |  |  * using the CPU's debug registers. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | #include <linux/perf_event.h>
 | 
					
						
							|  |  |  | #include <linux/hw_breakpoint.h>
 | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | #include <linux/irqflags.h>
 | 
					
						
							|  |  |  | #include <linux/notifier.h>
 | 
					
						
							|  |  |  | #include <linux/kallsyms.h>
 | 
					
						
							|  |  |  | #include <linux/kprobes.h>
 | 
					
						
							|  |  |  | #include <linux/percpu.h>
 | 
					
						
							|  |  |  | #include <linux/kdebug.h>
 | 
					
						
							|  |  |  | #include <linux/kernel.h>
 | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							|  |  |  | #include <linux/sched.h>
 | 
					
						
							|  |  |  | #include <linux/init.h>
 | 
					
						
							|  |  |  | #include <linux/smp.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <asm/hw_breakpoint.h>
 | 
					
						
							|  |  |  | #include <asm/processor.h>
 | 
					
						
							|  |  |  | #include <asm/debugreg.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | /* Per cpu debug control register value */ | 
					
						
							| 
									
										
										
										
											2009-11-25 22:24:44 +09:00
										 |  |  | DEFINE_PER_CPU(unsigned long, cpu_dr7); | 
					
						
							|  |  |  | EXPORT_PER_CPU_SYMBOL(cpu_dr7); | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Per cpu debug address registers values */ | 
					
						
							|  |  |  | static DEFINE_PER_CPU(unsigned long, cpu_debugreg[HBP_NUM]); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  |  * Stores the breakpoints currently in use on each breakpoint address | 
					
						
							|  |  |  |  * register for each cpus | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-26 06:04:38 +01:00
										 |  |  | static inline unsigned long | 
					
						
							|  |  |  | __encode_dr7(int drnum, unsigned int len, unsigned int type) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned long bp_info; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bp_info = (len | type) & 0xf; | 
					
						
							|  |  |  | 	bp_info <<= (DR_CONTROL_SHIFT + drnum * DR_CONTROL_SIZE); | 
					
						
							| 
									
										
										
										
											2009-11-26 06:04:38 +01:00
										 |  |  | 	bp_info |= (DR_GLOBAL_ENABLE << (drnum * DR_ENABLE_SIZE)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 	return bp_info; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-26 06:04:38 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Encode the length, type, Exact, and Enable bits for a particular breakpoint | 
					
						
							|  |  |  |  * as stored in debug register 7. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | unsigned long encode_dr7(int drnum, unsigned int len, unsigned int type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return __encode_dr7(drnum, len, type) | DR_GLOBAL_SLOWDOWN; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Decode the length and type bits for a particular breakpoint as | 
					
						
							|  |  |  |  * stored in debug register 7.  Return the "enabled" status. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int decode_dr7(unsigned long dr7, int bpnum, unsigned *len, unsigned *type) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	*len = (bp_info & 0xc) | 0x40; | 
					
						
							|  |  |  | 	*type = (bp_info & 0x3) | 0x80; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  |  * Install a perf counter breakpoint. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * We seek a free debug address register and use it for this | 
					
						
							|  |  |  |  * breakpoint. Eventually we enable it in the debug control register. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Atomic: we hold the counter->ctx->lock and we only handle variables | 
					
						
							|  |  |  |  * and registers local to this cpu. | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | int arch_install_hw_breakpoint(struct perf_event *bp) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
					
						
							|  |  |  | 	unsigned long *dr7; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < HBP_NUM; i++) { | 
					
						
							|  |  |  | 		struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!*slot) { | 
					
						
							|  |  |  | 			*slot = bp; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot")) | 
					
						
							|  |  |  | 		return -EBUSY; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set_debugreg(info->address, i); | 
					
						
							| 
									
										
										
										
											2010-12-18 16:28:55 +01:00
										 |  |  | 	__this_cpu_write(cpu_debugreg[i], info->address); | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-25 22:24:44 +09:00
										 |  |  | 	dr7 = &__get_cpu_var(cpu_dr7); | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	*dr7 |= encode_dr7(i, info->len, info->type); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set_debugreg(*dr7, 7); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  |  * Uninstall the breakpoint contained in the given counter. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * First we search the debug address register it uses and then we disable | 
					
						
							|  |  |  |  * it. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Atomic: we hold the counter->ctx->lock and we only handle variables | 
					
						
							|  |  |  |  * and registers local to this cpu. | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | void arch_uninstall_hw_breakpoint(struct perf_event *bp) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
					
						
							|  |  |  | 	unsigned long *dr7; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < HBP_NUM; i++) { | 
					
						
							|  |  |  | 		struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (*slot == bp) { | 
					
						
							|  |  |  | 			*slot = NULL; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot")) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-25 22:24:44 +09:00
										 |  |  | 	dr7 = &__get_cpu_var(cpu_dr7); | 
					
						
							| 
									
										
										
										
											2009-11-26 06:04:38 +01:00
										 |  |  | 	*dr7 &= ~__encode_dr7(i, info->len, info->type); | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	set_debugreg(*dr7, 7); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int get_hbp_len(u8 hbp_len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned int len_in_bytes = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (hbp_len) { | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	case X86_BREAKPOINT_LEN_1: | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		len_in_bytes = 1; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	case X86_BREAKPOINT_LEN_2: | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		len_in_bytes = 2; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	case X86_BREAKPOINT_LEN_4: | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		len_in_bytes = 4; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | #ifdef CONFIG_X86_64
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	case X86_BREAKPOINT_LEN_8: | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		len_in_bytes = 8; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return len_in_bytes; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Check for virtual address in kernel space. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-04-18 18:11:53 +02:00
										 |  |  | int arch_check_bp_in_kernelspace(struct perf_event *bp) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned int len; | 
					
						
							| 
									
										
										
										
											2010-04-18 18:11:53 +02:00
										 |  |  | 	unsigned long va; | 
					
						
							|  |  |  | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-18 18:11:53 +02:00
										 |  |  | 	va = info->address; | 
					
						
							|  |  |  | 	len = get_hbp_len(info->len); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | int arch_bp_generic_fields(int x86_len, int x86_type, | 
					
						
							|  |  |  | 			   int *gen_len, int *gen_type) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-09-17 03:24:13 +02:00
										 |  |  | 	/* Type */ | 
					
						
							|  |  |  | 	switch (x86_type) { | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_EXECUTE: | 
					
						
							|  |  |  | 		if (x86_len != X86_BREAKPOINT_LEN_X) | 
					
						
							|  |  |  | 			return -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		*gen_type = HW_BREAKPOINT_X; | 
					
						
							| 
									
										
										
										
											2010-06-24 10:00:24 +02:00
										 |  |  | 		*gen_len = sizeof(long); | 
					
						
							| 
									
										
										
										
											2010-09-17 03:24:13 +02:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_WRITE: | 
					
						
							|  |  |  | 		*gen_type = HW_BREAKPOINT_W; | 
					
						
							| 
									
										
										
										
											2010-06-24 10:00:24 +02:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2010-09-17 03:24:13 +02:00
										 |  |  | 	case X86_BREAKPOINT_RW: | 
					
						
							|  |  |  | 		*gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Len */ | 
					
						
							|  |  |  | 	switch (x86_len) { | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	case X86_BREAKPOINT_LEN_1: | 
					
						
							|  |  |  | 		*gen_len = HW_BREAKPOINT_LEN_1; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_LEN_2: | 
					
						
							|  |  |  | 		*gen_len = HW_BREAKPOINT_LEN_2; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_LEN_4: | 
					
						
							|  |  |  | 		*gen_len = HW_BREAKPOINT_LEN_4; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | #ifdef CONFIG_X86_64
 | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_LEN_8: | 
					
						
							|  |  |  | 		*gen_len = HW_BREAKPOINT_LEN_8; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int arch_build_bp_info(struct perf_event *bp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	info->address = bp->attr.bp_addr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-24 10:00:24 +02:00
										 |  |  | 	/* Type */ | 
					
						
							|  |  |  | 	switch (bp->attr.bp_type) { | 
					
						
							|  |  |  | 	case HW_BREAKPOINT_W: | 
					
						
							|  |  |  | 		info->type = X86_BREAKPOINT_WRITE; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case HW_BREAKPOINT_W | HW_BREAKPOINT_R: | 
					
						
							|  |  |  | 		info->type = X86_BREAKPOINT_RW; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case HW_BREAKPOINT_X: | 
					
						
							|  |  |  | 		info->type = X86_BREAKPOINT_EXECUTE; | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * x86 inst breakpoints need to have a specific undefined len. | 
					
						
							|  |  |  | 		 * But we still need to check userspace is not trying to setup | 
					
						
							|  |  |  | 		 * an unsupported length, to get a range breakpoint for example. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		if (bp->attr.bp_len == sizeof(long)) { | 
					
						
							|  |  |  | 			info->len = X86_BREAKPOINT_LEN_X; | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	/* Len */ | 
					
						
							|  |  |  | 	switch (bp->attr.bp_len) { | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 	case HW_BREAKPOINT_LEN_1: | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		info->len = X86_BREAKPOINT_LEN_1; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case HW_BREAKPOINT_LEN_2: | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		info->len = X86_BREAKPOINT_LEN_2; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case HW_BREAKPOINT_LEN_4: | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		info->len = X86_BREAKPOINT_LEN_4; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		break; | 
					
						
							|  |  |  | #ifdef CONFIG_X86_64
 | 
					
						
							|  |  |  | 	case HW_BREAKPOINT_LEN_8: | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		info->len = X86_BREAKPOINT_LEN_8; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Validate the arch-specific HW Breakpoint register settings | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-04-18 18:11:53 +02:00
										 |  |  | int arch_validate_hwbkpt_settings(struct perf_event *bp) | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 
					
						
							|  |  |  | 	unsigned int align; | 
					
						
							|  |  |  | 	int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ret = arch_build_bp_info(bp); | 
					
						
							|  |  |  | 	if (ret) | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ret = -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (info->len) { | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_LEN_1: | 
					
						
							|  |  |  | 		align = 0; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_LEN_2: | 
					
						
							|  |  |  | 		align = 1; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_LEN_4: | 
					
						
							|  |  |  | 		align = 3; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | #ifdef CONFIG_X86_64
 | 
					
						
							|  |  |  | 	case X86_BREAKPOINT_LEN_8: | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		align = 7; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Check that the low-order bits of the address are appropriate | 
					
						
							|  |  |  | 	 * for the alignment implied by len. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	if (info->address & align) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-09 21:03:43 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Dump the debug register contents to the user. | 
					
						
							|  |  |  |  * We can't dump our per cpu values because it | 
					
						
							|  |  |  |  * may contain cpu wide breakpoint, something that | 
					
						
							|  |  |  |  * doesn't belong to the current task. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * TODO: include non-ptrace user breakpoints (perf) | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void aout_dump_debugregs(struct user *dump) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	int dr7 = 0; | 
					
						
							|  |  |  | 	struct perf_event *bp; | 
					
						
							|  |  |  | 	struct arch_hw_breakpoint *info; | 
					
						
							|  |  |  | 	struct thread_struct *thread = ¤t->thread; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < HBP_NUM; i++) { | 
					
						
							|  |  |  | 		bp = thread->ptrace_bps[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (bp && !bp->attr.disabled) { | 
					
						
							|  |  |  | 			dump->u_debugreg[i] = bp->attr.bp_addr; | 
					
						
							|  |  |  | 			info = counter_arch_bp(bp); | 
					
						
							|  |  |  | 			dr7 |= encode_dr7(i, info->len, info->type); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			dump->u_debugreg[i] = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dump->u_debugreg[4] = 0; | 
					
						
							|  |  |  | 	dump->u_debugreg[5] = 0; | 
					
						
							|  |  |  | 	dump->u_debugreg[6] = current->thread.debugreg6; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dump->u_debugreg[7] = dr7; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-14 01:35:29 +01:00
										 |  |  | EXPORT_SYMBOL_GPL(aout_dump_debugregs); | 
					
						
							| 
									
										
										
										
											2009-11-09 21:03:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Release the user breakpoints used by ptrace | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void flush_ptrace_hw_breakpoint(struct task_struct *tsk) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 	struct thread_struct *t = &tsk->thread; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < HBP_NUM; i++) { | 
					
						
							|  |  |  | 		unregister_hw_breakpoint(t->ptrace_bps[i]); | 
					
						
							|  |  |  | 		t->ptrace_bps[i] = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-08 16:01:06 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	t->debugreg6 = 0; | 
					
						
							|  |  |  | 	t->ptrace_dr7 = 0; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | void hw_breakpoint_restore(void) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-12-18 16:28:55 +01:00
										 |  |  | 	set_debugreg(__this_cpu_read(cpu_debugreg[0]), 0); | 
					
						
							|  |  |  | 	set_debugreg(__this_cpu_read(cpu_debugreg[1]), 1); | 
					
						
							|  |  |  | 	set_debugreg(__this_cpu_read(cpu_debugreg[2]), 2); | 
					
						
							|  |  |  | 	set_debugreg(__this_cpu_read(cpu_debugreg[3]), 3); | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	set_debugreg(current->thread.debugreg6, 6); | 
					
						
							| 
									
										
										
										
											2010-12-18 16:28:55 +01:00
										 |  |  | 	set_debugreg(__this_cpu_read(cpu_dr7), 7); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | EXPORT_SYMBOL_GPL(hw_breakpoint_restore); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Handle debug exception notifications. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * NOTIFY_DONE returned if one of the following conditions is true. | 
					
						
							|  |  |  |  * i) When the causative address is from user-space and the exception | 
					
						
							|  |  |  |  * is a valid one, i.e. not triggered as a result of lazy debug register | 
					
						
							|  |  |  |  * switching | 
					
						
							|  |  |  |  * ii) When there are more bits than trap<n> set in DR6 register (such | 
					
						
							|  |  |  |  * as BD, BS or BT) indicating that more than one debug condition is | 
					
						
							|  |  |  |  * met and requires some more action in do_debug(). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * NOTIFY_STOP returned for all other cases | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-06-17 14:44:19 +05:30
										 |  |  | static int __kprobes hw_breakpoint_handler(struct die_args *args) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | { | 
					
						
							|  |  |  | 	int i, cpu, rc = NOTIFY_STOP; | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 	struct perf_event *bp; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:47:06 +05:30
										 |  |  | 	unsigned long dr7, dr6; | 
					
						
							|  |  |  | 	unsigned long *dr6_p; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* The DR6 value is pointed by args->err */ | 
					
						
							|  |  |  | 	dr6_p = (unsigned long *)ERR_PTR(args->err); | 
					
						
							|  |  |  | 	dr6 = *dr6_p; | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-11 21:18:43 +01:00
										 |  |  | 	/* If it's a single step, TRAP bits are random */ | 
					
						
							|  |  |  | 	if (dr6 & DR_STEP) | 
					
						
							|  |  |  | 		return NOTIFY_DONE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 	/* Do an early return if no trap bits are set in DR6 */ | 
					
						
							|  |  |  | 	if ((dr6 & DR_TRAP_BITS) == 0) | 
					
						
							|  |  |  | 		return NOTIFY_DONE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get_debugreg(dr7, 7); | 
					
						
							|  |  |  | 	/* Disable breakpoints during exception handling */ | 
					
						
							|  |  |  | 	set_debugreg(0UL, 7); | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Assert that local interrupts are disabled | 
					
						
							|  |  |  | 	 * Reset the DRn bits in the virtualized register value. | 
					
						
							|  |  |  | 	 * The ptrace trigger routine will add in whatever is needed. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	current->thread.debugreg6 &= ~DR_TRAP_BITS; | 
					
						
							|  |  |  | 	cpu = get_cpu(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Handle all the breakpoints that were triggered */ | 
					
						
							|  |  |  | 	for (i = 0; i < HBP_NUM; ++i) { | 
					
						
							|  |  |  | 		if (likely(!(dr6 & (DR_TRAP0 << i)))) | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		/*
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		 * The counter may be concurrently released but that can only | 
					
						
							|  |  |  | 		 * occur from a call_rcu() path. We can then safely fetch | 
					
						
							|  |  |  | 		 * the breakpoint, use its callback, touch its counter | 
					
						
							|  |  |  | 		 * while we are in an rcu_read_lock() path. | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		rcu_read_lock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		bp = per_cpu(bp_per_reg[i], cpu); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:47:06 +05:30
										 |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * Reset the 'i'th TRAP bit in dr6 to denote completion of | 
					
						
							|  |  |  | 		 * exception handling | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		(*dr6_p) &= ~(DR_TRAP0 << i); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * bp can be NULL due to lazy debug register switching | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		 * or due to concurrent perf counter removing. | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		if (!bp) { | 
					
						
							|  |  |  | 			rcu_read_unlock(); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-05 09:44:31 +01:00
										 |  |  | 		perf_bp_event(bp, args->regs); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-24 21:21:27 +02:00
										 |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * Set up resume flag to avoid breakpoint recursion when | 
					
						
							|  |  |  | 		 * returning back to origin. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		if (bp->hw.info.type == X86_BREAKPOINT_EXECUTE) | 
					
						
							|  |  |  | 			args->regs->flags |= X86_EFLAGS_RF; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 		rcu_read_unlock(); | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-01-28 16:44:15 +05:30
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Further processing in do_debug() is needed for a) user-space | 
					
						
							|  |  |  | 	 * breakpoints (to generate signals) and b) when the system has | 
					
						
							|  |  |  | 	 * taken exception due to multiple causes | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	if ((current->thread.debugreg6 & DR_TRAP_BITS) || | 
					
						
							|  |  |  | 	    (dr6 & (~DR_TRAP_BITS))) | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 		rc = NOTIFY_DONE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set_debugreg(dr7, 7); | 
					
						
							| 
									
										
										
										
											2009-06-17 12:52:15 +02:00
										 |  |  | 	put_cpu(); | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-01 23:43:57 +05:30
										 |  |  | 	return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Handle debug exception notifications. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int __kprobes hw_breakpoint_exceptions_notify( | 
					
						
							|  |  |  | 		struct notifier_block *unused, unsigned long val, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (val != DIE_DEBUG) | 
					
						
							|  |  |  | 		return NOTIFY_DONE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return hw_breakpoint_handler(data); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-09-09 19:22:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void hw_breakpoint_pmu_read(struct perf_event *bp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* TODO */ | 
					
						
							|  |  |  | } |