ARM: add support for hw-breakpoints [v6]
Subsystems (3)
cover ARM: add support for hw-breakpoints [v6]
This is version 6 of the patches originally posted at:
v1.) http://lists.infradead.org/pipermail/linux-arm-kernel/2010-February/009084.html
v2.) http://lists.infradead.org/pipermail/linux-arm-kernel/2010-March/011170.html
v3.) http://lists.infradead.org/pipermail/linux-arm-kernel/2010-June/017680.html
v4.) http://lists.infradead.org/pipermail/linux-arm-kernel/2010-July/019882.html
v5.) http://lists.infradead.org/pipermail/linux-arm-kernel/2010-August/022882.html
Changes from v5 include:
- Based on v2.6.36-rc2
- ptrace breakpoints are now flushed before the thread state is zeroed
- ptrace breakpoint pointers are zeroed in copy_thread
- Debug registers are now zeroed on all cores during boot
- Userspace breakpoint and watchpoint single-stepping is supported on v7 processors. This means the perf tool can be used to monitor function calls and variable accesses.
As with v5, these patches have been shown to work with GDB using the patches posted here:
http://sourceware.org/ml/gdb-patches/2010-07/msg00110.html
Will Deacon (4):
ARM: hw-breakpoint: add mechanism for hooking into prefetch aborts
ARM: hw-breakpoint: add ARM backend for the hw-breakpoint framework
ARM: hw-breakpoint: add new ptrace requests for hw-breakpoint
interaction
ARM: hw-breakpoint: add HAVE_HW_BREAKPOINT to Kconfig
arch/arm/Kconfig | 1 + arch/arm/include/asm/hw_breakpoint.h | 132 ++++++ arch/arm/include/asm/processor.h | 4 + arch/arm/include/asm/ptrace.h | 2 + arch/arm/include/asm/system.h | 4 + arch/arm/kernel/Makefile | 1 + arch/arm/kernel/hw_breakpoint.c | 849 ++++++++++++++++++++++++++++++++++ arch/arm/kernel/process.c | 5 + arch/arm/kernel/ptrace.c | 239 ++++++++++ arch/arm/mm/fault.c | 13 + 10 files changed, 1250 insertions(+), 0 deletions(-) create mode 100644 arch/arm/include/asm/hw_breakpoint.h create mode 100644 arch/arm/kernel/hw_breakpoint.c
PATCH 1/4 ARM: hw-breakpoint: add mechanism for hooking into prefetch aborts
On ARM processors with hardware breakpoint and watchpoint support, triggering these events results in a debug exception. These manifest as prefetch and data aborts respectively.
arch/arm/mm/fault.c already provides hook_fault_code for hooking into data aborts dependent on the DFSR. This patch adds a new function, hook_ifault_code for hooking into prefetch aborts in the same manner.
--- arch/arm/include/asm/system.h | 4 ++++ arch/arm/mm/fault.c | 13 +++++++++++++ 2 files changed, 17 insertions(+), 0 deletions(-)
arch/arm/include/asm/system.h +4 -0
@@ -85,6 +85,10 @@ void hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *), int sig, int code, const char *name); +void hook_ifault_code (int nr,int (*fn)(unsigned long ,unsigned int , +struct pt_regs *), +int sig,int code,const char *name); +#define xchg (ptr,x) \ ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
arch/arm/mm/fault.c +13 -0
@@ -581,6 +581,19 @@ static struct fsr_info ifsr_info[] = { { do_bad, SIGBUS, 0, "unknown 31" }, }; +void __init +hook_ifault_code (int nr,int (*fn)(unsigned long ,unsigned int ,struct pt_regs *), +int sig,int code,const char *name) +{ +if (nr <0 || nr >=ARRAY_SIZE (ifsr_info)) +BUG (); + + ifsr_info[nr].fn = fn; + ifsr_info[nr].sig = sig; + ifsr_info[nr].code = code; + ifsr_info[nr].name = name; +} + asmlinkage void __exceptiondo_PrefetchAbort (unsigned long addr,unsigned int ifsr,struct pt_regs *regs) {
Signature · 1 line
1.6.3.3
PATCH 2/4 ARM: hw-breakpoint: add ARM backend for the hw-breakpoint framework
The hw-breakpoint framework in the kernel requires architecture-specific support in order to install, remove, validate and manage hardware breakpoints.
This patch adds initial support for this framework to the ARM architecture, but restricts the number of watchpoints to a single resource to get around the fact that the Data Fault Address Register is unknown when a watchpoint debug exception is taken.
On cores with v7 debug, the Kernel can handle breakpoint and watchpoint exceptions occuring from userspace. Older cores require clients to handle the exception themselves by registering an appropriate overflow handler or, in the case of ptrace, handling the raised SIGTRAP.
The memory-mapped extended debug interface is unsupported due to its unreliability in real implementations.
--- arch/arm/include/asm/hw_breakpoint.h | 122 +++++ arch/arm/kernel/hw_breakpoint.c | 849 ++++++++++++++++++++++++++++++++++ 2 files changed, 971 insertions(+), 0 deletions(-) create mode 100644 arch/arm/include/asm/hw_breakpoint.h create mode 100644 arch/arm/kernel/hw_breakpoint.c
arch/arm/include/asm/hw_breakpoint.h new file +122 -0
@@ -0,0 +1,122 @@ +#ifndef _ARM_HW_BREAKPOINT_H +#define _ARM_HW_BREAKPOINT_H + +#ifdef __KERNEL__ +struct arch_hw_breakpoint_ctrl { +u32 __reserved :9 , + mismatch : 1, + :9 , + len :8 , + type :2 , + privilege :2 , + enabled :1 ; +}; + +struct arch_hw_breakpoint { +u32 address; +u32 trigger; +struct perf_event *suspended_wp; +struct arch_hw_breakpoint_ctrl ctrl; +}; + +static inline u32 encode_ctrl_reg (struct arch_hw_breakpoint_ctrl ctrl) +{ +return (ctrl.mismatch <<22 ) | (ctrl.len <<5 ) | (ctrl.type <<3 ) | + (ctrl.privilege <<1 ) | ctrl.enabled; +} + +static inline void decode_ctrl_reg (u32 reg, +struct arch_hw_breakpoint_ctrl *ctrl) +{ + ctrl->enabled = reg &0x1 ; + reg >>=1 ; + ctrl->privilege = reg &0x3 ; + reg >>=2 ; + ctrl->type = reg &0x3 ; + reg >>=2 ; + ctrl->len = reg &0xff ; + reg >>=17 ; + ctrl->mismatch = reg &0x1 ; +} + +/* Debug architecture numbers. */ +#define ARM_DEBUG_ARCH_V6 1 +#define ARM_DEBUG_ARCH_V6_1 2 +#define ARM_DEBUG_ARCH_V7_ECP14 3 +#define ARM_DEBUG_ARCH_V7_MM 4 + +/* Breakpoint */ +#define ARM_BREAKPOINT_EXECUTE 0 + +/* Watchpoints */ +#define ARM_BREAKPOINT_LOAD 1 +#define ARM_BREAKPOINT_STORE 2 + +/* Privilege Levels */ +#define ARM_BREAKPOINT_PRIV 1 +#define ARM_BREAKPOINT_USER 2 + +/* Lengths */ +#define ARM_BREAKPOINT_LEN_1 0x1 +#define ARM_BREAKPOINT_LEN_2 0x3 +#define ARM_BREAKPOINT_LEN_4 0xf +#define ARM_BREAKPOINT_LEN_8 0xff + +/* Limits */ +#define ARM_MAX_BRP 16 +#define ARM_MAX_WRP 16 +#define ARM_MAX_HBP_SLOTS (ARM_MAX_BRP + ARM_MAX_WRP) + +/* DSCR method of entry bits. */ +#define ARM_DSCR_MOE (x) ((x >> 2) & 0xf) +#define ARM_ENTRY_BREAKPOINT 0x1 +#define ARM_ENTRY_ASYNC_WATCHPOINT 0x2 +#define ARM_ENTRY_SYNC_WATCHPOINT 0xa + +/* DSCR monitor/halting bits. */ +#define ARM_DSCR_HDBGEN (1 << 14) +#define ARM_DSCR_MDBGEN (1 << 15) + +/* opcode2 numbers for the co-processor instructions. */ +#define ARM_OP2_BVR 4 +#define ARM_OP2_BCR 5 +#define ARM_OP2_WVR 6 +#define ARM_OP2_WCR 7 + +/* Base register numbers for the debug registers. */ +#define ARM_BASE_BVR 64 +#define ARM_BASE_BCR 80 +#define ARM_BASE_WVR 96 +#define ARM_BASE_WCR 112 + +/* Accessor macros for the debug registers. */ +#define ARM_DBG_READ (M ,OP2 ,VAL ) do {\ + asm volatile("mrc p14, 0, %0, c0," #M ", " #OP2 : "=r" (VAL));\ +} while (0) + +#define ARM_DBG_WRITE (M ,OP2 ,VAL ) do {\ + asm volatile("mcr p14, 0, %0, c0," #M ", " #OP2 : : "r" (VAL));\ +} while (0) + +struct notifier_block ; +struct perf_event ; +struct pmu ; +struct task_struct ; + +extern struct pmu perf_ops_bp; +extern int arch_bp_generic_fields (struct arch_hw_breakpoint_ctrl ctrl, +int *gen_len,int *gen_type); +extern int arch_check_bp_in_kernelspace (struct perf_event *bp); +extern int arch_validate_hwbkpt_settings (struct perf_event *bp); +extern int hw_breakpoint_exceptions_notify (struct notifier_block *unused, +unsigned long val,void *data); +extern u8 arch_get_debug_arch (void ); +extern u8 arch_get_max_wp_len (void ); + +int arch_install_hw_breakpoint (struct perf_event *bp); +void arch_uninstall_hw_breakpoint (struct perf_event *bp); +void hw_breakpoint_pmu_read (struct perf_event *bp); +int hw_breakpoint_slots (int type); + +#endif /* __KERNEL__ */ +#endif /* _ARM_HW_BREAKPOINT_H */
arch/arm/kernel/hw_breakpoint.c new file +849 -0
849 lines not on this page · raw message
Signature · 1 line
1.6.3.3
PATCH 3/4 ARM: hw-breakpoint: add new ptrace requests for hw-breakpoint interaction
For debuggers to take advantage of the hw-breakpoint framework in the kernel, it is necessary to expose the API calls via a ptrace interface.
This patch exposes the hardware breakpoints framework as a collection of virtual registers, accesible using PTRACE_SETHBPREGS and PTRACE_GETHBPREGS requests. The breakpoints are stored in the debug_info struct of the running thread.
--- arch/arm/include/asm/hw_breakpoint.h | 12 ++- arch/arm/include/asm/processor.h | 4 + arch/arm/include/asm/ptrace.h | 2 + arch/arm/kernel/process.c | 5 + arch/arm/kernel/ptrace.c | 239 ++++++++++++++++++++++++++++++++++ 5 files changed, 261 insertions(+), 1 deletions(-)
arch/arm/include/asm/hw_breakpoint.h +11 -1
@@ -2,6 +2,11 @@#define _ARM_HW_BREAKPOINT_H #ifdef __KERNEL__ + +struct task_struct; + +#ifdef CONFIG_HAVE_HW_BREAKPOINT + struct arch_hw_breakpoint_ctrl { u32 __reserved : 9, mismatch : 1,
@@ -101,7 +106,6 @@ static inline void decode_ctrl_reg(u32 reg,struct notifier_block ;struct perf_event ;struct pmu ; -struct task_struct ;extern struct pmu perf_ops_bp; extern int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
@@ -110,13 +114,19 @@ extern int arch_check_bp_in_kernelspace(struct perf_event *bp);extern int arch_validate_hwbkpt_settings (struct perf_event *bp);extern int hw_breakpoint_exceptions_notify (struct notifier_block *unused,unsigned long val,void *data); +extern u8 arch_get_debug_arch (void );extern u8 arch_get_max_wp_len (void ); +extern void clear_ptrace_hw_breakpoint (struct task_struct *tsk);int arch_install_hw_breakpoint (struct perf_event *bp);void arch_uninstall_hw_breakpoint (struct perf_event *bp);void hw_breakpoint_pmu_read (struct perf_event *bp);int hw_breakpoint_slots (int type); +#else +static inline void clear_ptrace_hw_breakpoint (struct task_struct *tsk) {} + +#endif /* CONFIG_HAVE_HW_BREAKPOINT */ #endif /* __KERNEL__ */ #endif /* _ARM_HW_BREAKPOINT_H */
arch/arm/include/asm/processor.h +4 -0
@@ -19,6 +19,7 @@#ifdef __KERNEL__ +#include <asm/hw_breakpoint.h> #include <asm/ptrace.h> #include <asm/types.h>
@@ -41,6 +42,9 @@ struct debug_entry {struct debug_info {int nsaved;struct debug_entry bp[2 ]; +#ifdef CONFIG_HAVE_HW_BREAKPOINT +struct perf_event *hbp[ARM_MAX_HBP_SLOTS ]; +#endif };struct thread_struct {
arch/arm/include/asm/ptrace.h +2 -0
@@ -29,6 +29,8 @@#define PTRACE_SETCRUNCHREGS 26#define PTRACE_GETVFPREGS 27#define PTRACE_SETVFPREGS 28 +#define PTRACE_GETHBPREGS 29 +#define PTRACE_SETHBPREGS 30/* * PSR bits
arch/arm/kernel/process.c +5 -0
@@ -29,6 +29,7 @@#include <linux/utsname.h> #include <linux/uaccess.h> #include <linux/random.h> +#include <linux/hw_breakpoint.h> #include <asm/cacheflush.h> #include <asm/leds.h>
@@ -317,6 +318,8 @@ void flush_thread(void)struct thread_info *thread =current_thread_info ();struct task_struct *tsk = current; +flush_ptrace_hw_breakpoint (tsk); +memset (thread->used_cp,0 ,sizeof (thread->used_cp));memset (&tsk->thread.debug,0 ,sizeof (struct debug_info ));memset (&thread->fpstate,0 ,sizeof (union fp_state ));
@@ -345,6 +348,8 @@ copy_thread(unsigned long clone_flags, unsigned long stack_start, thread->cpu_context.sp = (unsigned long )childregs; thread->cpu_context.pc = (unsigned long )ret_from_fork; +clear_ptrace_hw_breakpoint (p); +if (clone_flags &CLONE_SETTLS ) thread->tp_value = regs->ARM_r3;
arch/arm/kernel/ptrace.c +239 -0
257 lines not on this page · raw message
Signature · 1 line
1.6.3.3
PATCH 4/4 ARM: hw-breakpoint: add HAVE_HW_BREAKPOINT to Kconfig
If we're targetting a v6 or v7 core and have at least software perf events available, then automatically add support for hardware breakpoints.
--- arch/arm/Kconfig | 1 + arch/arm/kernel/Makefile | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)
arch/arm/Kconfig +1 -0
@@ -26,6 +26,7 @@ config ARM select HAVE_PERF_EVENTS select PERF_USE_VMALLOC select HAVE_REGS_AND_STACK_ACCESS_API + select HAVE_HW_BREAKPOINT if (PERF_EVENTS && (CPU_V6 || CPU_V7)) help The ARM series is a line of low-power-consumption RISC chip designs licensed by ARM Ltd and targeted at embedded applications and
arch/arm/kernel/Makefile +1 -0
@@ -42,6 +42,7 @@ obj-$(CONFIG_KGDB) += kgdb.o obj-$(CONFIG_ARM_UNWIND) += unwind.o obj-$(CONFIG_HAVE_TCM) += tcm.o obj-$(CONFIG_CRASH_DUMP) += crash_dump.o +obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o obj-$(CONFIG_CRUNCH) += crunch.o crunch-bits.o AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312
Signature · 1 line
1.6.3.3
PATCH 1/4 ARM: hw-breakpoint: add mechanism for hooking into prefetch aborts
In reply to Will DeaconQuoted 4 lines · Will Deacon
On ARM processors with hardware breakpoint and watchpoint support, triggering these events results in a debug exception. These manifest as prefetch and data aborts respectively.
Quoted 8 lines
arch/arm/mm/fault.calready provideshook_fault_codefor hooking into data aborts dependent on the DFSR. This patch adds a new function,hook_ifault_codefor hooking into prefetch aborts in the same manner.Cc Russell King - ARM Linux <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc S. Karthikeyan <[email protected]>Signed-off-by Will Deacon <[email protected]>
Ok. (please drop my cc: when committing.)