Skip to contentSkip to conversation

Add hardware breakpoint support for i386

PATCH Prasanna S Panchamukhi 1 participant 2 messages Aug 30, 2005 ptrace support

lore
Share & tools
lore
#0 lore raw

Hi,

Quoted 5 lines
Quoted 1 line

This adds hardware breakpoint support for i386. This is not as well tested

as

Quoted 1 line

software breakpoints, but in some minimal testing appears to be

functional.

Quoted 8 lines

This really would need so coordination with user space using them. Otherwise it'll be quite unreliable because any user program can break it.

Long ago (in 2.4 time frame) there used to be a IBM patch floating around to reserve them globally and user space to use specific ones. I guess something like that would be needed again.

Yes, to add hardware breakpoint support for i386, there are two patches.
1. Provides hardware debug register allocation mechanism.
2. Provides light weight interface for kernel-space watchpoint probes These patches have been posted & reviewd on lkml and systemtap mailing lists.

Your comments are welcome.

Thanks
Prasanna

This patch provides debug register allocation mechanism. Useful for debuggers like IOW, kgdb, kdb, kernel watchpoint.
---

---

---

 linux-2.6.13-prasanna/arch/i386/Kconfig.debug     |    8 
 linux-2.6.13-prasanna/arch/i386/kernel/Makefile   |    1 
 linux-2.6.13-prasanna/arch/i386/kernel/debugreg.c |  281 ++++++++++++++++++++++
 linux-2.6.13-prasanna/arch/i386/kernel/process.c  |   31 +-
 linux-2.6.13-prasanna/arch/i386/kernel/ptrace.c   |    5 
 linux-2.6.13-prasanna/arch/i386/kernel/signal.c   |    3 
 linux-2.6.13-prasanna/arch/i386/kernel/traps.c    |    2 
 linux-2.6.13-prasanna/include/asm-i386/debugreg.h |  189 ++++++++++++++
 8 files changed, 511 insertions(+), 9 deletions(-)
arch/i386/Kconfig.debug renamed from arch/i386/Kconfig.debug~kprobes-debug-regs +8 -0
diff -puN arch/i386/Kconfig.debug~kprobes-debug-regs arch/i386/Kconfig.debug
@@ -32,6 +32,14 @@ config KPROBES
 	  for kernel debugging, non-intrusive instrumentation and testing.
 	  If in doubt, say "N".
 
+config DEBUGREG
+	bool "Global Debug Registers"
+	depends on DEBUG_KERNEL
+	default off
+	help
+	  Global debug register allocation mechanism is useful for debuggers
+	  IOW, Kgdb, Kdb, Kernel Watchpoint probes. If in doubt say "N"
+
 config DEBUG_STACK_USAGE
 	bool "Stack utilization instrumentation"
 	depends on DEBUG_KERNEL
arch/i386/kernel/debugreg.c new file +281 -0

281 lines not on this page · raw message

arch/i386/kernel/Makefile renamed from arch/i386/kernel/Makefile~kprobes-debug-regs +1 -0
diff -puN arch/i386/kernel/Makefile~kprobes-debug-regs arch/i386/kernel/Makefile
@@ -34,6 +34,7 @@ obj-$(CONFIG_ACPI_SRAT) 	+= srat.o
 obj-$(CONFIG_HPET_TIMER) 	+= time_hpet.o
 obj-$(CONFIG_EFI) 		+= efi.o efi_stub.o
 obj-$(CONFIG_EARLY_PRINTK)	+= early_printk.o
+obj-$(CONFIG_DEBUGREG)		+= debugreg.o
 
 EXTRA_AFLAGS   := -traditional
 
arch/i386/kernel/process.c renamed from arch/i386/kernel/process.c~kprobes-debug-regs +24 -7
diff -puN arch/i386/kernel/process.c~kprobes-debug-regs arch/i386/kernel/process.c
@@ -52,6 +52,7 @@
 #ifdef CONFIG_MATH_EMULATION
 #include <asm/math_emu.h>
 #endif
+#include <asm/debugreg.h>
 
 #include <linux/irq.h>
 #include <linux/err.h>
@@ -399,6 +400,8 @@ void exit_thread(void)
 		tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
 		put_cpu();
 	}
+	if (tsk->thread.debugreg[7])
+		dr_dec_use_count(tsk->thread.debugreg[7]);
 }
 
 void flush_thread(void)
@@ -412,6 +415,8 @@ void flush_thread(void)
 	 */
 	kprobe_flush_task(tsk);
 
+	if (tsk->thread.debugreg[7])
+		dr_dec_use_count(tsk->thread.debugreg[7]);
 	memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
 	memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));	
 	/*
@@ -513,6 +518,9 @@ int copy_thread(int nr, unsigned long cl
 		desc->b = LDT_entry_b(&info);
 	}
 
+	if (tsk->thread.debugreg[7])
+		dr_inc_use_count(tsk->thread.debugreg[7]);
+
 	err = 0;
  out:
 	if (err && p->thread.io_bitmap_ptr) {
@@ -676,6 +684,7 @@ struct task_struct fastcall * __switch_t
 				 *next = &next_p->thread;
 	int cpu = smp_processor_id();
 	struct tss_struct *tss = &per_cpu(init_tss, cpu);
+	unsigned long next_dr7 = next->debugreg[7];
 
 	/* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
 
@@ -713,14 +722,22 @@ struct task_struct fastcall * __switch_t
 	/*
 	 * Now maybe reload the debug registers
 	 */
-	if (unlikely(next->debugreg[7])) {
-		set_debugreg(next->debugreg[0], 0);
-		set_debugreg(next->debugreg[1], 1);
-		set_debugreg(next->debugreg[2], 2);
-		set_debugreg(next->debugreg[3], 3);
+	/*
+	 * Don't reload global debug registers. Don't touch the global debug
+	 * register settings in dr7.
+	 */
+	if (unlikely(next_dr7)) {
+		if (prev->debugreg[0] != next->debugreg[0])
+			set_debugreg(current->thread.debugreg[0], 0);
+		if (prev->debugreg[1] != next->debugreg[1])
+			set_debugreg(current->thread.debugreg[1], 1);
+		if (prev->debugreg[2] != next->debugreg[2])
+			set_debugreg(current->thread.debugreg[2], 2);
+		if (prev->debugreg[3] != next->debugreg[3])
+			set_debugreg(current->thread.debugreg[3], 3);
 		/* no 4 and 5 */
-		set_debugreg(next->debugreg[6], 6);
-		set_debugreg(next->debugreg[7], 7);
+		set_debugreg(current->thread.debugreg[6], 6);
+		load_process_dr7(next_dr7);
 	}
 
 	if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr))
arch/i386/kernel/ptrace.c renamed from arch/i386/kernel/ptrace.c~kprobes-debug-regs +5 -0
diff -puN arch/i386/kernel/ptrace.c~kprobes-debug-regs arch/i386/kernel/ptrace.c
@@ -504,6 +504,11 @@ asmlinkage int sys_ptrace(long request, 
 
 			  addr -= (long) &dummy->u_debugreg;
 			  addr = addr >> 2;
+
+			  if (addr == 7 && (enable_debugreg(child->thread.debugreg[addr], data)) < 0) {
+				  ret = -EBUSY;
+				  break;
+			  }
 			  child->thread.debugreg[addr] = data;
 			  ret = 0;
 		  }
arch/i386/kernel/signal.c renamed from arch/i386/kernel/signal.c~kprobes-debug-regs +2 -1
diff -puN arch/i386/kernel/signal.c~kprobes-debug-regs arch/i386/kernel/signal.c
@@ -25,6 +25,7 @@
 #include <asm/ucontext.h>
 #include <asm/uaccess.h>
 #include <asm/i387.h>
+#include <asm/debugreg.h>
 #include "sigframe.h"
 
 #define DEBUG_SIG 0
@@ -622,7 +623,7 @@ int fastcall do_signal(struct pt_regs *r
 		 * inside the kernel.
 		 */
 		if (unlikely(current->thread.debugreg[7])) {
-			set_debugreg(current->thread.debugreg[7], 7);
+			load_process_dr7(current->thread.debugreg[7]);
 		}
 
 		/* Whee!  Actually deliver the signal.  */
arch/i386/kernel/traps.c renamed from arch/i386/kernel/traps.c~kprobes-debug-regs +1 -1
diff -puN arch/i386/kernel/traps.c~kprobes-debug-regs arch/i386/kernel/traps.c
@@ -756,7 +756,7 @@ fastcall void do_debug(struct pt_regs * 
 	 * the signal is delivered.
 	 */
 clear_dr7:
-	set_debugreg(0, 7);
+	load_process_dr7(0);
 	return;
 
 debug_vm86:
include/asm-i386/debugreg.h renamed from include/asm-i386/debugreg.h~kprobes-debug-regs +189 -0

193 lines not on this page · raw message

_

Signature · 5 lines
Prasanna S Panchamukhi
Linux Technology Center
India Software Labs, IBM Bangalore
Ph: 91-80-25044636
<[email protected]>
#1 lore raw

This patch provides a simple interface for kernel-space watchpoints
using processor's debug registers. Using Kwatch interface users can monitor kernel global variables and dump the debugging information such as kernel stack, global variables, processor registers.

int register_kwatch(unsigned long addr, u8 length, u8 type,
kwatch_handler_t handler)

-length of the breakpoint can be 1,2 or 4 bytes long. -type can be read, write, execute.
0 Break on instruction execution only.
1 Break on data writes only.
3 Break on data reads or writes but not instruction fetches.

-return value is the debug register number allocated/used for setting up this watch point.

Sample code:

	This sample code sets a watchpoint on the pid_max
	and registers a call back function if any writes 
	happen to pid_max.

	struct kwatch kp;
	void kwatch_handler(struct kwatch *p, struct pt_regs *regs)
	{
		.......<do-any-thing>........
	}
	
	Register watchpoint probe from init_module:
	
	static int debug_regs_num;
	int init_module(void)
	{
		..........<do-any-thing>............

		debug_regs_num = register_kwatch(kallsyms_lookup_name(pid_max),
					 4, 1, kwatch_handler);
		..........<do-any-thing>............
	}

	Test this by changing the value of pid_max in 
	/proc/sys/kernel/pid_max
	
	echo 1000 > /proc/sys/kernel/pid_max
	
	You see the call back function being called.

	Unregister the watchpoint from cleanup_module:

	void cleanup_module(void)
	{
		..........<do-any-thing>............
		unregister_kwatch(debug_regs_num);
		..........<do-any-thing>............
	}

---

---

 linux-2.6.13-prasanna/arch/i386/Kconfig.debug   |    8 +
 linux-2.6.13-prasanna/arch/i386/kernel/Makefile |    1 
 linux-2.6.13-prasanna/arch/i386/kernel/kwatch.c |  189 ++++++++++++++++++++++++
 linux-2.6.13-prasanna/include/asm-i386/kwatch.h |   60 +++++++
 4 files changed, 258 insertions(+)
arch/i386/Kconfig.debug renamed from arch/i386/Kconfig.debug~kernel-watchpoint +8 -0
diff -puN arch/i386/Kconfig.debug~kernel-watchpoint arch/i386/Kconfig.debug
@@ -32,6 +32,14 @@ config KPROBES
 	  for kernel debugging, non-intrusive instrumentation and testing.
 	  If in doubt, say "N".
 
+config KWATCH
+	bool "Kwatch points"
+	depends on DEBUG_KERNEL
+	select DEBUGREG
+	help
+	  This enables kernel-space watchpoints using processor's debug
+	  registers. If in doubt, say "N".
+
 config DEBUGREG
 	bool "Global Debug Registers"
 	depends on DEBUG_KERNEL
arch/i386/kernel/kwatch.c new file +189 -0

189 lines not on this page · raw message

arch/i386/kernel/Makefile renamed from arch/i386/kernel/Makefile~kernel-watchpoint +1 -0
diff -puN arch/i386/kernel/Makefile~kernel-watchpoint arch/i386/kernel/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_HPET_TIMER) 	+= time_hpet.o
 obj-$(CONFIG_EFI) 		+= efi.o efi_stub.o
 obj-$(CONFIG_EARLY_PRINTK)	+= early_printk.o
 obj-$(CONFIG_DEBUGREG)		+= debugreg.o
+obj-$(CONFIG_KWATCH)		+= kwatch.o
 
 EXTRA_AFLAGS   := -traditional
 
include/asm-i386/kwatch.h new file +60 -0

60 lines not on this page · raw message

_

Signature · 5 lines
Prasanna S Panchamukhi
Linux Technology Center
India Software Labs, IBM Bangalore
Ph: 91-80-25044636
<[email protected]>