xref: /openbmc/linux/arch/arm64/include/asm/debug-monitors.h (revision d7a33f4fbd12ca0a32a24cc46c0d02b47f6b54d1)
1478fcb2cSWill Deacon /*
2478fcb2cSWill Deacon  * Copyright (C) 2012 ARM Ltd.
3478fcb2cSWill Deacon  *
4478fcb2cSWill Deacon  * This program is free software; you can redistribute it and/or modify
5478fcb2cSWill Deacon  * it under the terms of the GNU General Public License version 2 as
6478fcb2cSWill Deacon  * published by the Free Software Foundation.
7478fcb2cSWill Deacon  *
8478fcb2cSWill Deacon  * This program is distributed in the hope that it will be useful,
9478fcb2cSWill Deacon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10478fcb2cSWill Deacon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11478fcb2cSWill Deacon  * GNU General Public License for more details.
12478fcb2cSWill Deacon  *
13478fcb2cSWill Deacon  * You should have received a copy of the GNU General Public License
14478fcb2cSWill Deacon  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15478fcb2cSWill Deacon  */
16478fcb2cSWill Deacon #ifndef __ASM_DEBUG_MONITORS_H
17478fcb2cSWill Deacon #define __ASM_DEBUG_MONITORS_H
18478fcb2cSWill Deacon 
19478fcb2cSWill Deacon #ifdef __KERNEL__
20478fcb2cSWill Deacon 
21*d7a33f4fSDave P Martin #include <linux/errno.h>
22*d7a33f4fSDave P Martin #include <linux/types.h>
2303923696SDave P Martin #include <asm/esr.h>
24951757aeSDave P Martin #include <asm/insn.h>
25*d7a33f4fSDave P Martin #include <asm/ptrace.h>
26951757aeSDave P Martin 
2751ba2481SMarc Zyngier /* Low-level stepping controls. */
2851ba2481SMarc Zyngier #define DBG_MDSCR_SS		(1 << 0)
2951ba2481SMarc Zyngier #define DBG_SPSR_SS		(1 << 21)
3051ba2481SMarc Zyngier 
3151ba2481SMarc Zyngier /* MDSCR_EL1 enabling bits */
3251ba2481SMarc Zyngier #define DBG_MDSCR_KDE		(1 << 13)
3351ba2481SMarc Zyngier #define DBG_MDSCR_MDE		(1 << 15)
3451ba2481SMarc Zyngier #define DBG_MDSCR_MASK		~(DBG_MDSCR_KDE | DBG_MDSCR_MDE)
3551ba2481SMarc Zyngier 
36478fcb2cSWill Deacon #define	DBG_ESR_EVT(x)		(((x) >> 27) & 0x7)
37478fcb2cSWill Deacon 
38478fcb2cSWill Deacon /* AArch64 */
39478fcb2cSWill Deacon #define DBG_ESR_EVT_HWBP	0x0
40478fcb2cSWill Deacon #define DBG_ESR_EVT_HWSS	0x1
41478fcb2cSWill Deacon #define DBG_ESR_EVT_HWWP	0x2
42478fcb2cSWill Deacon #define DBG_ESR_EVT_BRK		0x6
43478fcb2cSWill Deacon 
44bcf5763bSVijaya Kumar K /*
45bcf5763bSVijaya Kumar K  * Break point instruction encoding
46bcf5763bSVijaya Kumar K  */
47951757aeSDave P Martin #define BREAK_INSTR_SIZE		AARCH64_INSN_SIZE
48bcf5763bSVijaya Kumar K 
49bcf5763bSVijaya Kumar K /*
50bcf5763bSVijaya Kumar K  * #imm16 values used for BRK instruction generation
51bcf5763bSVijaya Kumar K  * Allowed values for kgbd are 0x400 - 0x7ff
52a9ae04c9SMark Brown  * 0x100: for triggering a fault on purpose (reserved)
53bcf5763bSVijaya Kumar K  * 0x400: for dynamic BRK instruction
54bcf5763bSVijaya Kumar K  * 0x401: for compile time BRK instruction
55bcf5763bSVijaya Kumar K  */
56a9ae04c9SMark Brown #define FAULT_BRK_IMM			0x100
577acf71d1SCatalin Marinas #define KGDB_DYN_DBG_BRK_IMM		0x400
587acf71d1SCatalin Marinas #define KGDB_COMPILED_DBG_BRK_IMM	0x401
59bcf5763bSVijaya Kumar K 
60bcf5763bSVijaya Kumar K /*
61bcf5763bSVijaya Kumar K  * BRK instruction encoding
62bcf5763bSVijaya Kumar K  * The #imm16 value should be placed at bits[20:5] within BRK ins
63bcf5763bSVijaya Kumar K  */
64bcf5763bSVijaya Kumar K #define AARCH64_BREAK_MON	0xd4200000
65bcf5763bSVijaya Kumar K 
66bcf5763bSVijaya Kumar K /*
67a9ae04c9SMark Brown  * BRK instruction for provoking a fault on purpose
68a9ae04c9SMark Brown  * Unlike kgdb, #imm16 value with unallocated handler is used for faulting.
69a9ae04c9SMark Brown  */
70a9ae04c9SMark Brown #define AARCH64_BREAK_FAULT	(AARCH64_BREAK_MON | (FAULT_BRK_IMM << 5))
71a9ae04c9SMark Brown 
72c696b934SDave P Martin #define AARCH64_BREAK_KGDB_DYN_DBG	\
73c696b934SDave P Martin 	(AARCH64_BREAK_MON | (KGDB_DYN_DBG_BRK_IMM << 5))
74c696b934SDave P Martin #define KGDB_DYN_BRK_INS_BYTE(x)	\
75c696b934SDave P Martin 	((AARCH64_BREAK_KGDB_DYN_DBG >> (8 * (x))) & 0xff)
76bcf5763bSVijaya Kumar K 
77bcf5763bSVijaya Kumar K #define CACHE_FLUSH_IS_SAFE		1
78bcf5763bSVijaya Kumar K 
79478fcb2cSWill Deacon /* AArch32 */
80478fcb2cSWill Deacon #define DBG_ESR_EVT_BKPT	0x4
81478fcb2cSWill Deacon #define DBG_ESR_EVT_VECC	0x5
82478fcb2cSWill Deacon 
83478fcb2cSWill Deacon #define AARCH32_BREAK_ARM	0x07f001f0
84478fcb2cSWill Deacon #define AARCH32_BREAK_THUMB	0xde01
85478fcb2cSWill Deacon #define AARCH32_BREAK_THUMB2_LO	0xf7f0
86478fcb2cSWill Deacon #define AARCH32_BREAK_THUMB2_HI	0xa000
87478fcb2cSWill Deacon 
88478fcb2cSWill Deacon #ifndef __ASSEMBLY__
89478fcb2cSWill Deacon struct task_struct;
90478fcb2cSWill Deacon 
91478fcb2cSWill Deacon #define DBG_ARCH_ID_RESERVED	0	/* In case of ptrace ABI updates. */
92478fcb2cSWill Deacon 
93ee6214ceSSandeepa Prabhu #define DBG_HOOK_HANDLED	0
94ee6214ceSSandeepa Prabhu #define DBG_HOOK_ERROR		1
95ee6214ceSSandeepa Prabhu 
96ee6214ceSSandeepa Prabhu struct step_hook {
97ee6214ceSSandeepa Prabhu 	struct list_head node;
98ee6214ceSSandeepa Prabhu 	int (*fn)(struct pt_regs *regs, unsigned int esr);
99ee6214ceSSandeepa Prabhu };
100ee6214ceSSandeepa Prabhu 
101ee6214ceSSandeepa Prabhu void register_step_hook(struct step_hook *hook);
102ee6214ceSSandeepa Prabhu void unregister_step_hook(struct step_hook *hook);
103ee6214ceSSandeepa Prabhu 
104ee6214ceSSandeepa Prabhu struct break_hook {
105ee6214ceSSandeepa Prabhu 	struct list_head node;
106ee6214ceSSandeepa Prabhu 	u32 esr_val;
107ee6214ceSSandeepa Prabhu 	u32 esr_mask;
108ee6214ceSSandeepa Prabhu 	int (*fn)(struct pt_regs *regs, unsigned int esr);
109ee6214ceSSandeepa Prabhu };
110ee6214ceSSandeepa Prabhu 
111ee6214ceSSandeepa Prabhu void register_break_hook(struct break_hook *hook);
112ee6214ceSSandeepa Prabhu void unregister_break_hook(struct break_hook *hook);
113ee6214ceSSandeepa Prabhu 
114478fcb2cSWill Deacon u8 debug_monitors_arch(void);
115478fcb2cSWill Deacon 
11651ba2481SMarc Zyngier enum debug_el {
11751ba2481SMarc Zyngier 	DBG_ACTIVE_EL0 = 0,
11851ba2481SMarc Zyngier 	DBG_ACTIVE_EL1,
11951ba2481SMarc Zyngier };
12051ba2481SMarc Zyngier 
121478fcb2cSWill Deacon void enable_debug_monitors(enum debug_el el);
122478fcb2cSWill Deacon void disable_debug_monitors(enum debug_el el);
123478fcb2cSWill Deacon 
124478fcb2cSWill Deacon void user_rewind_single_step(struct task_struct *task);
125478fcb2cSWill Deacon void user_fastforward_single_step(struct task_struct *task);
126478fcb2cSWill Deacon 
127478fcb2cSWill Deacon void kernel_enable_single_step(struct pt_regs *regs);
128478fcb2cSWill Deacon void kernel_disable_single_step(void);
129478fcb2cSWill Deacon int kernel_active_single_step(void);
130478fcb2cSWill Deacon 
131478fcb2cSWill Deacon #ifdef CONFIG_HAVE_HW_BREAKPOINT
132478fcb2cSWill Deacon int reinstall_suspended_bps(struct pt_regs *regs);
133478fcb2cSWill Deacon #else
134478fcb2cSWill Deacon static inline int reinstall_suspended_bps(struct pt_regs *regs)
135478fcb2cSWill Deacon {
136478fcb2cSWill Deacon 	return -ENODEV;
137478fcb2cSWill Deacon }
138478fcb2cSWill Deacon #endif
139478fcb2cSWill Deacon 
1401442b6edSWill Deacon int aarch32_break_handler(struct pt_regs *regs);
1411442b6edSWill Deacon 
142478fcb2cSWill Deacon #endif	/* __ASSEMBLY */
143478fcb2cSWill Deacon #endif	/* __KERNEL__ */
144478fcb2cSWill Deacon #endif	/* __ASM_DEBUG_MONITORS_H */
145