18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 ARM Ltd.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#ifndef __ASM_DEBUG_MONITORS_H
68c2ecf20Sopenharmony_ci#define __ASM_DEBUG_MONITORS_H
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/errno.h>
98c2ecf20Sopenharmony_ci#include <linux/types.h>
108c2ecf20Sopenharmony_ci#include <asm/brk-imm.h>
118c2ecf20Sopenharmony_ci#include <asm/esr.h>
128c2ecf20Sopenharmony_ci#include <asm/insn.h>
138c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* Low-level stepping controls. */
168c2ecf20Sopenharmony_ci#define DBG_MDSCR_SS		(1 << 0)
178c2ecf20Sopenharmony_ci#define DBG_SPSR_SS		(1 << 21)
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* MDSCR_EL1 enabling bits */
208c2ecf20Sopenharmony_ci#define DBG_MDSCR_KDE		(1 << 13)
218c2ecf20Sopenharmony_ci#define DBG_MDSCR_MDE		(1 << 15)
228c2ecf20Sopenharmony_ci#define DBG_MDSCR_MASK		~(DBG_MDSCR_KDE | DBG_MDSCR_MDE)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define	DBG_ESR_EVT(x)		(((x) >> 27) & 0x7)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* AArch64 */
278c2ecf20Sopenharmony_ci#define DBG_ESR_EVT_HWBP	0x0
288c2ecf20Sopenharmony_ci#define DBG_ESR_EVT_HWSS	0x1
298c2ecf20Sopenharmony_ci#define DBG_ESR_EVT_HWWP	0x2
308c2ecf20Sopenharmony_ci#define DBG_ESR_EVT_BRK		0x6
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * Break point instruction encoding
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci#define BREAK_INSTR_SIZE		AARCH64_INSN_SIZE
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * BRK instruction encoding
398c2ecf20Sopenharmony_ci * The #imm16 value should be placed at bits[20:5] within BRK ins
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_ci#define AARCH64_BREAK_MON	0xd4200000
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * BRK instruction for provoking a fault on purpose
458c2ecf20Sopenharmony_ci * Unlike kgdb, #imm16 value with unallocated handler is used for faulting.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci#define AARCH64_BREAK_FAULT	(AARCH64_BREAK_MON | (FAULT_BRK_IMM << 5))
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define AARCH64_BREAK_KGDB_DYN_DBG	\
508c2ecf20Sopenharmony_ci	(AARCH64_BREAK_MON | (KGDB_DYN_DBG_BRK_IMM << 5))
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define CACHE_FLUSH_IS_SAFE		1
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* kprobes BRK opcodes with ESR encoding  */
558c2ecf20Sopenharmony_ci#define BRK64_OPCODE_KPROBES	(AARCH64_BREAK_MON | (KPROBES_BRK_IMM << 5))
568c2ecf20Sopenharmony_ci#define BRK64_OPCODE_KPROBES_SS	(AARCH64_BREAK_MON | (KPROBES_BRK_SS_IMM << 5))
578c2ecf20Sopenharmony_ci/* uprobes BRK opcodes with ESR encoding  */
588c2ecf20Sopenharmony_ci#define BRK64_OPCODE_UPROBES	(AARCH64_BREAK_MON | (UPROBES_BRK_IMM << 5))
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* AArch32 */
618c2ecf20Sopenharmony_ci#define DBG_ESR_EVT_BKPT	0x4
628c2ecf20Sopenharmony_ci#define DBG_ESR_EVT_VECC	0x5
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define AARCH32_BREAK_ARM	0x07f001f0
658c2ecf20Sopenharmony_ci#define AARCH32_BREAK_THUMB	0xde01
668c2ecf20Sopenharmony_ci#define AARCH32_BREAK_THUMB2_LO	0xf7f0
678c2ecf20Sopenharmony_ci#define AARCH32_BREAK_THUMB2_HI	0xa000
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
708c2ecf20Sopenharmony_cistruct task_struct;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define DBG_ARCH_ID_RESERVED	0	/* In case of ptrace ABI updates. */
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define DBG_HOOK_HANDLED	0
758c2ecf20Sopenharmony_ci#define DBG_HOOK_ERROR		1
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistruct step_hook {
788c2ecf20Sopenharmony_ci	struct list_head node;
798c2ecf20Sopenharmony_ci	int (*fn)(struct pt_regs *regs, unsigned int esr);
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_civoid register_user_step_hook(struct step_hook *hook);
838c2ecf20Sopenharmony_civoid unregister_user_step_hook(struct step_hook *hook);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_civoid register_kernel_step_hook(struct step_hook *hook);
868c2ecf20Sopenharmony_civoid unregister_kernel_step_hook(struct step_hook *hook);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistruct break_hook {
898c2ecf20Sopenharmony_ci	struct list_head node;
908c2ecf20Sopenharmony_ci	int (*fn)(struct pt_regs *regs, unsigned int esr);
918c2ecf20Sopenharmony_ci	u16 imm;
928c2ecf20Sopenharmony_ci	u16 mask; /* These bits are ignored when comparing with imm */
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_civoid register_user_break_hook(struct break_hook *hook);
968c2ecf20Sopenharmony_civoid unregister_user_break_hook(struct break_hook *hook);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_civoid register_kernel_break_hook(struct break_hook *hook);
998c2ecf20Sopenharmony_civoid unregister_kernel_break_hook(struct break_hook *hook);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciu8 debug_monitors_arch(void);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cienum dbg_active_el {
1048c2ecf20Sopenharmony_ci	DBG_ACTIVE_EL0 = 0,
1058c2ecf20Sopenharmony_ci	DBG_ACTIVE_EL1,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_civoid enable_debug_monitors(enum dbg_active_el el);
1098c2ecf20Sopenharmony_civoid disable_debug_monitors(enum dbg_active_el el);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_civoid user_rewind_single_step(struct task_struct *task);
1128c2ecf20Sopenharmony_civoid user_fastforward_single_step(struct task_struct *task);
1138c2ecf20Sopenharmony_civoid user_regs_reset_single_step(struct user_pt_regs *regs,
1148c2ecf20Sopenharmony_ci				 struct task_struct *task);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_civoid kernel_enable_single_step(struct pt_regs *regs);
1178c2ecf20Sopenharmony_civoid kernel_disable_single_step(void);
1188c2ecf20Sopenharmony_ciint kernel_active_single_step(void);
1198c2ecf20Sopenharmony_civoid kernel_rewind_single_step(struct pt_regs *regs);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_HW_BREAKPOINT
1228c2ecf20Sopenharmony_ciint reinstall_suspended_bps(struct pt_regs *regs);
1238c2ecf20Sopenharmony_ci#else
1248c2ecf20Sopenharmony_cistatic inline int reinstall_suspended_bps(struct pt_regs *regs)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	return -ENODEV;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci#endif
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciint aarch32_break_handler(struct pt_regs *regs);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_civoid debug_traps_init(void);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#endif	/* __ASSEMBLY */
1358c2ecf20Sopenharmony_ci#endif	/* __ASM_DEBUG_MONITORS_H */
136