Skip to content

Commit c91e02b

Browse files
jcmvbkbcczankel
authored andcommitted
xtensa: support hardware breakpoints/watchpoints
Use perf framework to manage hardware instruction and data breakpoints. Add two new ptrace calls: PTRACE_GETHBPREGS and PTRACE_SETHBPREGS to query and set instruction and data breakpoints. Address bit 0 choose instruction (0) or data (1) break register, bits 31..1 are the register number. Both calls transfer two 32-bit words: address (0) and control (1). Instruction breakpoint contorl word is 0 to clear breakpoint, 1 to set. Data breakpoint control word bit 31 is 'trigger on store', bit 30 is 'trigger on load, bits 29..0 are length. Length 0 is used to clear a breakpoint. To set a breakpoint length must be a power of 2 in the range 1..64 and the address must be length-aligned. Introduce new thread_info flag: TIF_DB_DISABLED. Set it if debug exception is raised by the kernel code accessing watched userspace address and disable corresponding data breakpoint. On exit to userspace check that flag and, if set, restore all data breakpoints. Handle debug exceptions raised with PS.EXCM set. This may happen when window overflow/underflow handler or fast exception handler hits data breakpoint, in which case save and disable all data breakpoints, single-step faulting instruction and restore data breakpoints. Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
1 parent 6ec7026 commit c91e02b

15 files changed

Lines changed: 649 additions & 13 deletions

File tree

‎arch/xtensa/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ config XTENSA
1818
select HAVE_DMA_ATTRS
1919
select HAVE_FUNCTION_TRACER
2020
select HAVE_FUTEX_CMPXCHG if !MMU
21+
select HAVE_HW_BREAKPOINT if PERF_EVENTS
2122
select HAVE_IRQ_TIME_ACCOUNTING
2223
select HAVE_OPROFILE
2324
select HAVE_PERF_EVENTS
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Xtensa hardware breakpoints/watchpoints handling functions
3+
*
4+
* This file is subject to the terms and conditions of the GNU General Public
5+
* License. See the file "COPYING" in the main directory of this archive
6+
* for more details.
7+
*
8+
* Copyright (C) 2016 Cadence Design Systems Inc.
9+
*/
10+
11+
#ifndef __ASM_XTENSA_HW_BREAKPOINT_H
12+
#define __ASM_XTENSA_HW_BREAKPOINT_H
13+
14+
#ifdef CONFIG_HAVE_HW_BREAKPOINT
15+
16+
#include <linux/kdebug.h>
17+
#include <linux/types.h>
18+
#include <uapi/linux/hw_breakpoint.h>
19+
20+
/* Breakpoint */
21+
#define XTENSA_BREAKPOINT_EXECUTE 0
22+
23+
/* Watchpoints */
24+
#define XTENSA_BREAKPOINT_LOAD 1
25+
#define XTENSA_BREAKPOINT_STORE 2
26+
27+
struct arch_hw_breakpoint {
28+
unsigned long address;
29+
u16 len;
30+
u16 type;
31+
};
32+
33+
struct perf_event;
34+
struct pt_regs;
35+
struct task_struct;
36+
37+
int hw_breakpoint_slots(int type);
38+
int arch_check_bp_in_kernelspace(struct perf_event *bp);
39+
int arch_validate_hwbkpt_settings(struct perf_event *bp);
40+
int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
41+
unsigned long val, void *data);
42+
43+
int arch_install_hw_breakpoint(struct perf_event *bp);
44+
void arch_uninstall_hw_breakpoint(struct perf_event *bp);
45+
void hw_breakpoint_pmu_read(struct perf_event *bp);
46+
int check_hw_breakpoint(struct pt_regs *regs);
47+
void clear_ptrace_hw_breakpoint(struct task_struct *tsk);
48+
49+
#else
50+
51+
struct task_struct;
52+
53+
static inline void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
54+
{
55+
}
56+
57+
#endif /* CONFIG_HAVE_HW_BREAKPOINT */
58+
#endif /* __ASM_XTENSA_HW_BREAKPOINT_H */

‎arch/xtensa/include/asm/irqflags.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define _XTENSA_IRQFLAGS_H
1414

1515
#include <linux/types.h>
16+
#include <asm/processor.h>
1617

1718
static inline unsigned long arch_local_save_flags(void)
1819
{

‎arch/xtensa/include/asm/processor.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,10 @@ struct thread_struct {
130130
unsigned long bad_vaddr; /* last user fault */
131131
unsigned long bad_uaddr; /* last kernel fault accessing user space */
132132
unsigned long error_code;
133-
134-
unsigned long ibreak[XCHAL_NUM_IBREAK];
135-
unsigned long dbreaka[XCHAL_NUM_DBREAK];
136-
unsigned long dbreakc[XCHAL_NUM_DBREAK];
137-
133+
#ifdef CONFIG_HAVE_HW_BREAKPOINT
134+
struct perf_event *ptrace_bp[XCHAL_NUM_IBREAK];
135+
struct perf_event *ptrace_wp[XCHAL_NUM_DBREAK];
136+
#endif
138137
/* Make structure 16 bytes aligned. */
139138
int align[0] __attribute__ ((aligned(16)));
140139
};

‎arch/xtensa/include/asm/regs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
/* Special registers. */
2929

3030
#define SREG_MR 32
31+
#define SREG_IBREAKENABLE 96
3132
#define SREG_IBREAKA 128
3233
#define SREG_DBREAKA 144
3334
#define SREG_DBREAKC 160
@@ -103,6 +104,8 @@
103104

104105
/* DEBUGCAUSE register fields. */
105106

107+
#define DEBUGCAUSE_DBNUM_MASK 0xf00
108+
#define DEBUGCAUSE_DBNUM_SHIFT 8 /* First bit of DBNUM field */
106109
#define DEBUGCAUSE_DEBUGINT_BIT 5 /* External debug interrupt */
107110
#define DEBUGCAUSE_BREAKN_BIT 4 /* BREAK.N instruction */
108111
#define DEBUGCAUSE_BREAK_BIT 3 /* BREAK instruction */

‎arch/xtensa/include/asm/thread_info.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ static inline struct thread_info *current_thread_info(void)
111111
#define TIF_MEMDIE 5 /* is terminating due to OOM killer */
112112
#define TIF_RESTORE_SIGMASK 6 /* restore signal mask in do_signal() */
113113
#define TIF_NOTIFY_RESUME 7 /* callback before returning to user */
114+
#define TIF_DB_DISABLED 8 /* debug trap disabled for syscall */
114115

115116
#define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
116117
#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)

‎arch/xtensa/include/asm/traps.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ struct debug_table {
7070
void (*debug_exception)(void);
7171
/* Temporary register save area */
7272
unsigned long debug_save[1];
73+
#ifdef CONFIG_HAVE_HW_BREAKPOINT
74+
/* Save area for DBREAKC registers */
75+
unsigned long dbreakc_save[XCHAL_NUM_DBREAK];
76+
/* Saved ICOUNT register */
77+
unsigned long icount_save;
78+
/* Saved ICOUNTLEVEL register */
79+
unsigned long icount_level_save;
80+
#endif
7381
};
7482

7583
void debug_exception(void);

‎arch/xtensa/include/uapi/asm/ptrace.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
#define PTRACE_SETREGS 13
7373
#define PTRACE_GETXTREGS 18
7474
#define PTRACE_SETXTREGS 19
75+
#define PTRACE_GETHBPREGS 20
76+
#define PTRACE_SETHBPREGS 21
7577

7678

7779
#endif /* _UAPI_XTENSA_PTRACE_H */

‎arch/xtensa/kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ obj-$(CONFIG_MODULES) += xtensa_ksyms.o module.o
1313
obj-$(CONFIG_FUNCTION_TRACER) += mcount.o
1414
obj-$(CONFIG_SMP) += smp.o mxhead.o
1515
obj-$(CONFIG_XTENSA_VARIANT_HAVE_PERF_EVENTS) += perf_event.o
16+
obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
1617

1718
AFLAGS_head.o += -mtext-section-literals
1819
AFLAGS_mxhead.o += -mtext-section-literals

‎arch/xtensa/kernel/asm-offsets.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ int main(void)
122122
DEFINE(DT_DEBUG_EXCEPTION,
123123
offsetof(struct debug_table, debug_exception));
124124
DEFINE(DT_DEBUG_SAVE, offsetof(struct debug_table, debug_save));
125+
#ifdef CONFIG_HAVE_HW_BREAKPOINT
126+
DEFINE(DT_DBREAKC_SAVE, offsetof(struct debug_table, dbreakc_save));
127+
DEFINE(DT_ICOUNT_SAVE, offsetof(struct debug_table, icount_save));
128+
DEFINE(DT_ICOUNT_LEVEL_SAVE,
129+
offsetof(struct debug_table, icount_level_save));
130+
#endif
125131

126132
return 0;
127133
}

0 commit comments

Comments
 (0)