Skip to content

Commit e14dd07

Browse files
zhangqingmychenhuacai
authored andcommitted
LoongArch: Add basic KGDB & KDB support
KGDB is intended to be used as a source level debugger for the Linux kernel. It is used along with gdb to debug a Linux kernel. GDB can be used to "break in" to the kernel to inspect memory, variables and regs similar to the way an application developer would use GDB to debug an application. KDB is a frontend of KGDB which is similar to GDB. By now, in addition to the generic KGDB features, the LoongArch KGDB implements the following features: - Hardware breakpoints/watchpoints; - Software single-step support for KDB. Signed-off-by: Qing Zhang <[email protected]> # Framework & CoreFeature Signed-off-by: Binbin Zhou <[email protected]> # BreakPoint & SingleStep Signed-off-by: Hui Li <[email protected]> # Some Minor Improvements Signed-off-by: Randy Dunlap <[email protected]> # Some Build Error Fixes Signed-off-by: Huacai Chen <[email protected]>
1 parent bd3c579 commit e14dd07

File tree

8 files changed

+845
-1
lines changed

8 files changed

+845
-1
lines changed

Documentation/features/debug/kgdb/arch-support.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| csky: | TODO |
1414
| hexagon: | ok |
1515
| ia64: | TODO |
16-
| loongarch: | TODO |
16+
| loongarch: | ok |
1717
| m68k: | TODO |
1818
| microblaze: | ok |
1919
| mips: | ok |

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ config LOONGARCH
9191
select HAVE_ARCH_AUDITSYSCALL
9292
select HAVE_ARCH_JUMP_LABEL
9393
select HAVE_ARCH_JUMP_LABEL_RELATIVE
94+
select HAVE_ARCH_KGDB if PERF_EVENTS
9495
select HAVE_ARCH_MMAP_RND_BITS if MMU
9596
select HAVE_ARCH_SECCOMP_FILTER
9697
select HAVE_ARCH_TRACEHOOK

arch/loongarch/include/asm/kgdb.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2023 Loongson Technology Corporation Limited
4+
*/
5+
6+
#ifndef _ASM_LOONGARCH_KGDB_H
7+
#define _ASM_LOONGARCH_KGDB_H
8+
9+
#define GDB_SIZEOF_REG sizeof(u64)
10+
11+
/* gdb remote procotol expects the following register layout. */
12+
13+
/*
14+
* General purpose registers:
15+
* r0-r31: 64 bit
16+
* orig_a0: 64 bit
17+
* pc : 64 bit
18+
* csr_badvaddr: 64 bit
19+
*/
20+
#define DBG_PT_REGS_BASE 0
21+
#define DBG_PT_REGS_NUM 35
22+
#define DBG_PT_REGS_END (DBG_PT_REGS_BASE + DBG_PT_REGS_NUM - 1)
23+
24+
/*
25+
* Floating point registers:
26+
* f0-f31: 64 bit
27+
*/
28+
#define DBG_FPR_BASE (DBG_PT_REGS_END + 1)
29+
#define DBG_FPR_NUM 32
30+
#define DBG_FPR_END (DBG_FPR_BASE + DBG_FPR_NUM - 1)
31+
32+
/*
33+
* Condition Flag registers:
34+
* fcc0-fcc8: 8 bit
35+
*/
36+
#define DBG_FCC_BASE (DBG_FPR_END + 1)
37+
#define DBG_FCC_NUM 8
38+
#define DBG_FCC_END (DBG_FCC_BASE + DBG_FCC_NUM - 1)
39+
40+
/*
41+
* Floating-point Control and Status registers:
42+
* fcsr: 32 bit
43+
*/
44+
#define DBG_FCSR_NUM 1
45+
#define DBG_FCSR (DBG_FCC_END + 1)
46+
47+
#define DBG_MAX_REG_NUM (DBG_FCSR + 1)
48+
49+
/*
50+
* Size of I/O buffer for gdb packet.
51+
* considering to hold all register contents, size is set
52+
*/
53+
#define BUFMAX 2048
54+
55+
/*
56+
* Number of bytes required for gdb_regs buffer.
57+
* PT_REGS and FPR: 8 bytes; FCSR: 4 bytes; FCC: 1 bytes.
58+
* GDB fails to connect for size beyond this with error
59+
* "'g' packet reply is too long"
60+
*/
61+
#define NUMREGBYTES ((DBG_PT_REGS_NUM + DBG_FPR_NUM) * GDB_SIZEOF_REG + DBG_FCC_NUM * 1 + DBG_FCSR_NUM * 4)
62+
63+
#define BREAK_INSTR_SIZE 4
64+
#define CACHE_FLUSH_IS_SAFE 0
65+
66+
/* Register numbers of various important registers. */
67+
enum dbg_loongarch_regnum {
68+
DBG_LOONGARCH_ZERO = 0,
69+
DBG_LOONGARCH_RA,
70+
DBG_LOONGARCH_TP,
71+
DBG_LOONGARCH_SP,
72+
DBG_LOONGARCH_A0,
73+
DBG_LOONGARCH_FP = 22,
74+
DBG_LOONGARCH_S0,
75+
DBG_LOONGARCH_S1,
76+
DBG_LOONGARCH_S2,
77+
DBG_LOONGARCH_S3,
78+
DBG_LOONGARCH_S4,
79+
DBG_LOONGARCH_S5,
80+
DBG_LOONGARCH_S6,
81+
DBG_LOONGARCH_S7,
82+
DBG_LOONGARCH_S8,
83+
DBG_LOONGARCH_ORIG_A0,
84+
DBG_LOONGARCH_PC,
85+
DBG_LOONGARCH_BADV
86+
};
87+
88+
void kgdb_breakinst(void);
89+
void arch_kgdb_breakpoint(void);
90+
91+
#ifdef CONFIG_KGDB
92+
bool kgdb_breakpoint_handler(struct pt_regs *regs);
93+
#else /* !CONFIG_KGDB */
94+
static inline bool kgdb_breakpoint_handler(struct pt_regs *regs) { return false; }
95+
#endif /* CONFIG_KGDB */
96+
97+
#endif /* __ASM_KGDB_H_ */

arch/loongarch/include/asm/stackframe.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@
158158
cfi_st u0, PT_R21, \docfi
159159
csrrd u0, PERCPU_BASE_KS
160160
9:
161+
#ifdef CONFIG_KGDB
162+
li.w t0, CSR_CRMD_WE
163+
csrxchg t0, t0, LOONGARCH_CSR_CRMD
164+
#endif
161165
.endm
162166

163167
.macro SAVE_ALL docfi=0

arch/loongarch/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ obj-$(CONFIG_UNWINDER_PROLOGUE) += unwind_prologue.o
5656
obj-$(CONFIG_PERF_EVENTS) += perf_event.o perf_regs.o
5757
obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
5858

59+
obj-$(CONFIG_KGDB) += kgdb.o
5960
obj-$(CONFIG_KPROBES) += kprobes.o
6061
obj-$(CONFIG_RETHOOK) += rethook.o rethook_trampoline.o
6162
obj-$(CONFIG_UPROBES) += uprobes.o

arch/loongarch/kernel/entry.S

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ SYM_FUNC_START(handle_syscall)
5858

5959
SAVE_STATIC
6060

61+
#ifdef CONFIG_KGDB
62+
li.w t1, CSR_CRMD_WE
63+
csrxchg t1, t1, LOONGARCH_CSR_CRMD
64+
#endif
65+
6166
move u0, t0
6267
li.d tp, ~_THREAD_MASK
6368
and tp, tp, sp

0 commit comments

Comments
 (0)