1 /*
2  * Copyright (C) 2012 ARM Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 #ifndef __ASM_DEBUG_MONITORS_H
17 #define __ASM_DEBUG_MONITORS_H
18 
19 #ifdef __KERNEL__
20 
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <asm/esr.h>
24 #include <asm/insn.h>
25 #include <asm/ptrace.h>
26 
27 /* Low-level stepping controls. */
28 #define DBG_MDSCR_SS		(1 << 0)
29 #define DBG_SPSR_SS		(1 << 21)
30 
31 /* MDSCR_EL1 enabling bits */
32 #define DBG_MDSCR_KDE		(1 << 13)
33 #define DBG_MDSCR_MDE		(1 << 15)
34 #define DBG_MDSCR_MASK		~(DBG_MDSCR_KDE | DBG_MDSCR_MDE)
35 
36 #define	DBG_ESR_EVT(x)		(((x) >> 27) & 0x7)
37 
38 /* AArch64 */
39 #define DBG_ESR_EVT_HWBP	0x0
40 #define DBG_ESR_EVT_HWSS	0x1
41 #define DBG_ESR_EVT_HWWP	0x2
42 #define DBG_ESR_EVT_BRK		0x6
43 
44 /*
45  * Break point instruction encoding
46  */
47 #define BREAK_INSTR_SIZE		AARCH64_INSN_SIZE
48 
49 /*
50  * #imm16 values used for BRK instruction generation
51  * Allowed values for kgbd are 0x400 - 0x7ff
52  * 0x100: for triggering a fault on purpose (reserved)
53  * 0x400: for dynamic BRK instruction
54  * 0x401: for compile time BRK instruction
55  * 0x800: kernel-mode BUG() and WARN() traps
56  */
57 #define FAULT_BRK_IMM			0x100
58 #define KGDB_DYN_DBG_BRK_IMM		0x400
59 #define KGDB_COMPILED_DBG_BRK_IMM	0x401
60 #define BUG_BRK_IMM			0x800
61 
62 /*
63  * BRK instruction encoding
64  * The #imm16 value should be placed at bits[20:5] within BRK ins
65  */
66 #define AARCH64_BREAK_MON	0xd4200000
67 
68 /*
69  * BRK instruction for provoking a fault on purpose
70  * Unlike kgdb, #imm16 value with unallocated handler is used for faulting.
71  */
72 #define AARCH64_BREAK_FAULT	(AARCH64_BREAK_MON | (FAULT_BRK_IMM << 5))
73 
74 #define AARCH64_BREAK_KGDB_DYN_DBG	\
75 	(AARCH64_BREAK_MON | (KGDB_DYN_DBG_BRK_IMM << 5))
76 #define KGDB_DYN_BRK_INS_BYTE(x)	\
77 	((AARCH64_BREAK_KGDB_DYN_DBG >> (8 * (x))) & 0xff)
78 
79 #define CACHE_FLUSH_IS_SAFE		1
80 
81 /* AArch32 */
82 #define DBG_ESR_EVT_BKPT	0x4
83 #define DBG_ESR_EVT_VECC	0x5
84 
85 #define AARCH32_BREAK_ARM	0x07f001f0
86 #define AARCH32_BREAK_THUMB	0xde01
87 #define AARCH32_BREAK_THUMB2_LO	0xf7f0
88 #define AARCH32_BREAK_THUMB2_HI	0xa000
89 
90 #ifndef __ASSEMBLY__
91 struct task_struct;
92 
93 #define DBG_ARCH_ID_RESERVED	0	/* In case of ptrace ABI updates. */
94 
95 #define DBG_HOOK_HANDLED	0
96 #define DBG_HOOK_ERROR		1
97 
98 struct step_hook {
99 	struct list_head node;
100 	int (*fn)(struct pt_regs *regs, unsigned int esr);
101 };
102 
103 void register_step_hook(struct step_hook *hook);
104 void unregister_step_hook(struct step_hook *hook);
105 
106 struct break_hook {
107 	struct list_head node;
108 	u32 esr_val;
109 	u32 esr_mask;
110 	int (*fn)(struct pt_regs *regs, unsigned int esr);
111 };
112 
113 void register_break_hook(struct break_hook *hook);
114 void unregister_break_hook(struct break_hook *hook);
115 
116 u8 debug_monitors_arch(void);
117 
118 enum dbg_active_el {
119 	DBG_ACTIVE_EL0 = 0,
120 	DBG_ACTIVE_EL1,
121 };
122 
123 void enable_debug_monitors(enum dbg_active_el el);
124 void disable_debug_monitors(enum dbg_active_el el);
125 
126 void user_rewind_single_step(struct task_struct *task);
127 void user_fastforward_single_step(struct task_struct *task);
128 
129 void kernel_enable_single_step(struct pt_regs *regs);
130 void kernel_disable_single_step(void);
131 int kernel_active_single_step(void);
132 
133 #ifdef CONFIG_HAVE_HW_BREAKPOINT
134 int reinstall_suspended_bps(struct pt_regs *regs);
135 #else
136 static inline int reinstall_suspended_bps(struct pt_regs *regs)
137 {
138 	return -ENODEV;
139 }
140 #endif
141 
142 int aarch32_break_handler(struct pt_regs *regs);
143 
144 #endif	/* __ASSEMBLY */
145 #endif	/* __KERNEL__ */
146 #endif	/* __ASM_DEBUG_MONITORS_H */
147