18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * AArch64 KGDB support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Based on arch/arm/include/kgdb.h
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2013 Cavium Inc.
88c2ecf20Sopenharmony_ci * Author: Vijaya Kumar K <vijaya.kumar@caviumnetworks.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifndef __ARM_KGDB_H
128c2ecf20Sopenharmony_ci#define __ARM_KGDB_H
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/ptrace.h>
158c2ecf20Sopenharmony_ci#include <asm/debug-monitors.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#ifndef	__ASSEMBLY__
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic inline void arch_kgdb_breakpoint(void)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	asm ("brk %0" : : "I" (KGDB_COMPILED_DBG_BRK_IMM));
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciextern void kgdb_handle_bus_error(void);
258c2ecf20Sopenharmony_ciextern int kgdb_fault_expected;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#endif /* !__ASSEMBLY__ */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * gdb remote procotol (well most versions of it) expects the following
318c2ecf20Sopenharmony_ci * register layout.
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * General purpose regs:
348c2ecf20Sopenharmony_ci *     r0-r30: 64 bit
358c2ecf20Sopenharmony_ci *     sp,pc : 64 bit
368c2ecf20Sopenharmony_ci *     pstate  : 32 bit
378c2ecf20Sopenharmony_ci *     Total: 33 + 1
388c2ecf20Sopenharmony_ci * FPU regs:
398c2ecf20Sopenharmony_ci *     f0-f31: 128 bit
408c2ecf20Sopenharmony_ci *     fpsr & fpcr: 32 bit
418c2ecf20Sopenharmony_ci *     Total: 32 + 2
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * To expand a little on the "most versions of it"... when the gdb remote
448c2ecf20Sopenharmony_ci * protocol for AArch64 was developed it depended on a statement in the
458c2ecf20Sopenharmony_ci * Architecture Reference Manual that claimed "SPSR_ELx is a 32-bit register".
468c2ecf20Sopenharmony_ci * and, as a result, allocated only 32-bits for the PSTATE in the remote
478c2ecf20Sopenharmony_ci * protocol. In fact this statement is still present in ARM DDI 0487A.i.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * Unfortunately "is a 32-bit register" has a very special meaning for
508c2ecf20Sopenharmony_ci * system registers. It means that "the upper bits, bits[63:32], are
518c2ecf20Sopenharmony_ci * RES0.". RES0 is heavily used in the ARM architecture documents as a
528c2ecf20Sopenharmony_ci * way to leave space for future architecture changes. So to translate a
538c2ecf20Sopenharmony_ci * little for people who don't spend their spare time reading ARM architecture
548c2ecf20Sopenharmony_ci * manuals, what "is a 32-bit register" actually means in this context is
558c2ecf20Sopenharmony_ci * "is a 64-bit register but one with no meaning allocated to any of the
568c2ecf20Sopenharmony_ci * upper 32-bits... *yet*".
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * Perhaps then we should not be surprised that this has led to some
598c2ecf20Sopenharmony_ci * confusion. Specifically a patch, influenced by the above translation,
608c2ecf20Sopenharmony_ci * that extended PSTATE to 64-bit was accepted into gdb-7.7 but the patch
618c2ecf20Sopenharmony_ci * was reverted in gdb-7.8.1 and all later releases, when this was
628c2ecf20Sopenharmony_ci * discovered to be an undocumented protocol change.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * So... it is *not* wrong for us to only allocate 32-bits to PSTATE
658c2ecf20Sopenharmony_ci * here even though the kernel itself allocates 64-bits for the same
668c2ecf20Sopenharmony_ci * state. That is because this bit of code tells the kernel how the gdb
678c2ecf20Sopenharmony_ci * remote protocol (well most versions of it) describes the register state.
688c2ecf20Sopenharmony_ci *
698c2ecf20Sopenharmony_ci * Note that if you are using one of the versions of gdb that supports
708c2ecf20Sopenharmony_ci * the gdb-7.7 version of the protocol you cannot use kgdb directly
718c2ecf20Sopenharmony_ci * without providing a custom register description (gdb can load new
728c2ecf20Sopenharmony_ci * protocol descriptions at runtime).
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define _GP_REGS		33
768c2ecf20Sopenharmony_ci#define _FP_REGS		32
778c2ecf20Sopenharmony_ci#define _EXTRA_REGS		3
788c2ecf20Sopenharmony_ci/*
798c2ecf20Sopenharmony_ci * general purpose registers size in bytes.
808c2ecf20Sopenharmony_ci * pstate is only 4 bytes. subtract 4 bytes
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci#define GP_REG_BYTES		(_GP_REGS * 8)
838c2ecf20Sopenharmony_ci#define DBG_MAX_REG_NUM		(_GP_REGS + _FP_REGS + _EXTRA_REGS)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Size of I/O buffer for gdb packet.
878c2ecf20Sopenharmony_ci * considering to hold all register contents, size is set
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define BUFMAX			2048
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * Number of bytes required for gdb_regs buffer.
948c2ecf20Sopenharmony_ci * _GP_REGS: 8 bytes, _FP_REGS: 16 bytes and _EXTRA_REGS: 4 bytes each
958c2ecf20Sopenharmony_ci * GDB fails to connect for size beyond this with error
968c2ecf20Sopenharmony_ci * "'g' packet reply is too long"
978c2ecf20Sopenharmony_ci */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define NUMREGBYTES	((_GP_REGS * 8) + (_FP_REGS * 16) + \
1008c2ecf20Sopenharmony_ci			(_EXTRA_REGS * 4))
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#endif /* __ASM_KGDB_H */
103