Skip to content

Commit d965757

Browse files
VincentZWCpalmer-dabbelt
authored andcommitted
riscv: Use the XML target descriptions to report 3 system registers
The $status, $badaddr, and $cause registers belong to the thread context, so KGDB can obtain their contents from pt_regs in each trap. However, the sequential number of these registers in the gdb register list is far from the general-purpose registers. If riscv port uses the existing method to report these three registers, many trivial registers with sequence numbers in the middle of them will also be packaged to the reply packets. To solve this problem, the riscv port wants to introduce the GDB target description mechanism to customize the reported register list. By the list, the KGDB can ignore the intermediate registers and just reports the general-purpose registers and these three system registers. Signed-off-by: Vincent Chen <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent fe89bd2 commit d965757

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ config RISCV
6969
select HAVE_COPY_THREAD_TLS
7070
select HAVE_ARCH_KASAN if MMU && 64BIT
7171
select HAVE_ARCH_KGDB
72+
select HAVE_ARCH_KGDB_QXFER_PKT
7273

7374
config ARCH_MMAP_RND_BITS_MIN
7475
default 18 if 64BIT

arch/riscv/include/asm/gdb_xml.h

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
3+
#ifndef __ASM_GDB_XML_H_
4+
#define __ASM_GDB_XML_H_
5+
6+
#define kgdb_arch_gdb_stub_feature riscv_gdb_stub_feature
7+
static const char riscv_gdb_stub_feature[64] =
8+
"PacketSize=800;qXfer:features:read+;";
9+
10+
static const char gdb_xfer_read_target[31] = "qXfer:features:read:target.xml:";
11+
12+
#ifdef CONFIG_64BIT
13+
static const char gdb_xfer_read_cpuxml[39] =
14+
"qXfer:features:read:riscv-64bit-cpu.xml";
15+
16+
static const char riscv_gdb_stub_target_desc[256] =
17+
"l<?xml version=\"1.0\"?>"
18+
"<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
19+
"<target>"
20+
"<xi:include href=\"riscv-64bit-cpu.xml\"/>"
21+
"</target>";
22+
23+
static const char riscv_gdb_stub_cpuxml[2048] =
24+
"l<?xml version=\"1.0\"?>"
25+
"<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">"
26+
"<feature name=\"org.gnu.gdb.riscv.cpu\">"
27+
"<reg name=\""DBG_REG_ZERO"\" bitsize=\"64\" type=\"int\" regnum=\"0\"/>"
28+
"<reg name=\""DBG_REG_RA"\" bitsize=\"64\" type=\"code_ptr\"/>"
29+
"<reg name=\""DBG_REG_SP"\" bitsize=\"64\" type=\"data_ptr\"/>"
30+
"<reg name=\""DBG_REG_GP"\" bitsize=\"64\" type=\"data_ptr\"/>"
31+
"<reg name=\""DBG_REG_TP"\" bitsize=\"64\" type=\"data_ptr\"/>"
32+
"<reg name=\""DBG_REG_T0"\" bitsize=\"64\" type=\"int\"/>"
33+
"<reg name=\""DBG_REG_T1"\" bitsize=\"64\" type=\"int\"/>"
34+
"<reg name=\""DBG_REG_T2"\" bitsize=\"64\" type=\"int\"/>"
35+
"<reg name=\""DBG_REG_FP"\" bitsize=\"64\" type=\"data_ptr\"/>"
36+
"<reg name=\""DBG_REG_S1"\" bitsize=\"64\" type=\"int\"/>"
37+
"<reg name=\""DBG_REG_A0"\" bitsize=\"64\" type=\"int\"/>"
38+
"<reg name=\""DBG_REG_A1"\" bitsize=\"64\" type=\"int\"/>"
39+
"<reg name=\""DBG_REG_A2"\" bitsize=\"64\" type=\"int\"/>"
40+
"<reg name=\""DBG_REG_A3"\" bitsize=\"64\" type=\"int\"/>"
41+
"<reg name=\""DBG_REG_A4"\" bitsize=\"64\" type=\"int\"/>"
42+
"<reg name=\""DBG_REG_A5"\" bitsize=\"64\" type=\"int\"/>"
43+
"<reg name=\""DBG_REG_A6"\" bitsize=\"64\" type=\"int\"/>"
44+
"<reg name=\""DBG_REG_A7"\" bitsize=\"64\" type=\"int\"/>"
45+
"<reg name=\""DBG_REG_S2"\" bitsize=\"64\" type=\"int\"/>"
46+
"<reg name=\""DBG_REG_S3"\" bitsize=\"64\" type=\"int\"/>"
47+
"<reg name=\""DBG_REG_S4"\" bitsize=\"64\" type=\"int\"/>"
48+
"<reg name=\""DBG_REG_S5"\" bitsize=\"64\" type=\"int\"/>"
49+
"<reg name=\""DBG_REG_S6"\" bitsize=\"64\" type=\"int\"/>"
50+
"<reg name=\""DBG_REG_S7"\" bitsize=\"64\" type=\"int\"/>"
51+
"<reg name=\""DBG_REG_S8"\" bitsize=\"64\" type=\"int\"/>"
52+
"<reg name=\""DBG_REG_S9"\" bitsize=\"64\" type=\"int\"/>"
53+
"<reg name=\""DBG_REG_S10"\" bitsize=\"64\" type=\"int\"/>"
54+
"<reg name=\""DBG_REG_S11"\" bitsize=\"64\" type=\"int\"/>"
55+
"<reg name=\""DBG_REG_T3"\" bitsize=\"64\" type=\"int\"/>"
56+
"<reg name=\""DBG_REG_T4"\" bitsize=\"64\" type=\"int\"/>"
57+
"<reg name=\""DBG_REG_T5"\" bitsize=\"64\" type=\"int\"/>"
58+
"<reg name=\""DBG_REG_T6"\" bitsize=\"64\" type=\"int\"/>"
59+
"<reg name=\""DBG_REG_EPC"\" bitsize=\"64\" type=\"code_ptr\"/>"
60+
"<reg name=\""DBG_REG_STATUS"\" bitsize=\"64\" type=\"int\"/>"
61+
"<reg name=\""DBG_REG_BADADDR"\" bitsize=\"64\" type=\"int\"/>"
62+
"<reg name=\""DBG_REG_CAUSE"\" bitsize=\"64\" type=\"int\"/>"
63+
"</feature>";
64+
#else
65+
static const char gdb_xfer_read_cpuxml[39] =
66+
"qXfer:features:read:riscv-32bit-cpu.xml";
67+
68+
static const char riscv_gdb_stub_target_desc[256] =
69+
"l<?xml version=\"1.0\"?>"
70+
"<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
71+
"<target>"
72+
"<xi:include href=\"riscv-32bit-cpu.xml\"/>"
73+
"</target>";
74+
75+
static const char riscv_gdb_stub_cpuxml[2048] =
76+
"l<?xml version=\"1.0\"?>"
77+
"<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">"
78+
"<feature name=\"org.gnu.gdb.riscv.cpu\">"
79+
"<reg name=\""DBG_REG_ZERO"\" bitsize=\"32\" type=\"int\" regnum=\"0\"/>"
80+
"<reg name=\""DBG_REG_RA"\" bitsize=\"32\" type=\"code_ptr\"/>"
81+
"<reg name=\""DBG_REG_SP"\" bitsize=\"32\" type=\"data_ptr\"/>"
82+
"<reg name=\""DBG_REG_GP"\" bitsize=\"32\" type=\"data_ptr\"/>"
83+
"<reg name=\""DBG_REG_TP"\" bitsize=\"32\" type=\"data_ptr\"/>"
84+
"<reg name=\""DBG_REG_T0"\" bitsize=\"32\" type=\"int\"/>"
85+
"<reg name=\""DBG_REG_T1"\" bitsize=\"32\" type=\"int\"/>"
86+
"<reg name=\""DBG_REG_T2"\" bitsize=\"32\" type=\"int\"/>"
87+
"<reg name=\""DBG_REG_FP"\" bitsize=\"32\" type=\"data_ptr\"/>"
88+
"<reg name=\""DBG_REG_S1"\" bitsize=\"32\" type=\"int\"/>"
89+
"<reg name=\""DBG_REG_A0"\" bitsize=\"32\" type=\"int\"/>"
90+
"<reg name=\""DBG_REG_A1"\" bitsize=\"32\" type=\"int\"/>"
91+
"<reg name=\""DBG_REG_A2"\" bitsize=\"32\" type=\"int\"/>"
92+
"<reg name=\""DBG_REG_A3"\" bitsize=\"32\" type=\"int\"/>"
93+
"<reg name=\""DBG_REG_A4"\" bitsize=\"32\" type=\"int\"/>"
94+
"<reg name=\""DBG_REG_A5"\" bitsize=\"32\" type=\"int\"/>"
95+
"<reg name=\""DBG_REG_A6"\" bitsize=\"32\" type=\"int\"/>"
96+
"<reg name=\""DBG_REG_A7"\" bitsize=\"32\" type=\"int\"/>"
97+
"<reg name=\""DBG_REG_S2"\" bitsize=\"32\" type=\"int\"/>"
98+
"<reg name=\""DBG_REG_S3"\" bitsize=\"32\" type=\"int\"/>"
99+
"<reg name=\""DBG_REG_S4"\" bitsize=\"32\" type=\"int\"/>"
100+
"<reg name=\""DBG_REG_S5"\" bitsize=\"32\" type=\"int\"/>"
101+
"<reg name=\""DBG_REG_S6"\" bitsize=\"32\" type=\"int\"/>"
102+
"<reg name=\""DBG_REG_S7"\" bitsize=\"32\" type=\"int\"/>"
103+
"<reg name=\""DBG_REG_S8"\" bitsize=\"32\" type=\"int\"/>"
104+
"<reg name=\""DBG_REG_S9"\" bitsize=\"32\" type=\"int\"/>"
105+
"<reg name=\""DBG_REG_S10"\" bitsize=\"32\" type=\"int\"/>"
106+
"<reg name=\""DBG_REG_S11"\" bitsize=\"32\" type=\"int\"/>"
107+
"<reg name=\""DBG_REG_T3"\" bitsize=\"32\" type=\"int\"/>"
108+
"<reg name=\""DBG_REG_T4"\" bitsize=\"32\" type=\"int\"/>"
109+
"<reg name=\""DBG_REG_T5"\" bitsize=\"32\" type=\"int\"/>"
110+
"<reg name=\""DBG_REG_T6"\" bitsize=\"32\" type=\"int\"/>"
111+
"<reg name=\""DBG_REG_EPC"\" bitsize=\"32\" type=\"code_ptr\"/>"
112+
"<reg name=\""DBG_REG_STATUS"\" bitsize=\"32\" type=\"int\"/>"
113+
"<reg name=\""DBG_REG_BADADDR"\" bitsize=\"32\" type=\"int\"/>"
114+
"<reg name=\""DBG_REG_CAUSE"\" bitsize=\"32\" type=\"int\"/>"
115+
"</feature>";
116+
#endif
117+
#endif

arch/riscv/include/asm/kgdb.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#define GDB_SIZEOF_REG sizeof(unsigned long)
99

10-
#define DBG_MAX_REG_NUM (33)
10+
#define DBG_MAX_REG_NUM (36)
1111
#define NUMREGBYTES ((DBG_MAX_REG_NUM) * GDB_SIZEOF_REG)
1212
#define CACHE_FLUSH_IS_SAFE 1
1313
#define BUFMAX 2048
@@ -65,6 +65,9 @@ static inline void arch_kgdb_breakpoint(void)
6565
#define DBG_REG_T5 "t5"
6666
#define DBG_REG_T6 "t6"
6767
#define DBG_REG_EPC "pc"
68+
#define DBG_REG_STATUS "sstatus"
69+
#define DBG_REG_BADADDR "stval"
70+
#define DBG_REG_CAUSE "scause"
6871

6972
#define DBG_REG_ZERO_OFF 0
7073
#define DBG_REG_RA_OFF 1
@@ -102,5 +105,8 @@ static inline void arch_kgdb_breakpoint(void)
102105
#define DBG_REG_STATUS_OFF 33
103106
#define DBG_REG_BADADDR_OFF 34
104107
#define DBG_REG_CAUSE_OFF 35
108+
109+
#include <asm/gdb_xml.h>
110+
105111
#endif
106112
#endif

arch/riscv/kernel/kgdb.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/irqflags.h>
1111
#include <linux/string.h>
1212
#include <asm/cacheflush.h>
13+
#include <asm/gdb_xml.h>
1314

1415
enum {
1516
NOT_KGDB_BREAK = 0,
@@ -51,6 +52,9 @@ struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
5152
{DBG_REG_T5, GDB_SIZEOF_REG, offsetof(struct pt_regs, t5)},
5253
{DBG_REG_T6, GDB_SIZEOF_REG, offsetof(struct pt_regs, t6)},
5354
{DBG_REG_EPC, GDB_SIZEOF_REG, offsetof(struct pt_regs, epc)},
55+
{DBG_REG_STATUS, GDB_SIZEOF_REG, offsetof(struct pt_regs, status)},
56+
{DBG_REG_BADADDR, GDB_SIZEOF_REG, offsetof(struct pt_regs, badaddr)},
57+
{DBG_REG_CAUSE, GDB_SIZEOF_REG, offsetof(struct pt_regs, cause)},
5458
};
5559

5660
char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
@@ -103,6 +107,17 @@ void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
103107
regs->epc = pc;
104108
}
105109

110+
void kgdb_arch_handle_qxfer_pkt(char *remcom_in_buffer,
111+
char *remcom_out_buffer)
112+
{
113+
if (!strncmp(remcom_in_buffer, gdb_xfer_read_target,
114+
sizeof(gdb_xfer_read_target)))
115+
strcpy(remcom_out_buffer, riscv_gdb_stub_target_desc);
116+
else if (!strncmp(remcom_in_buffer, gdb_xfer_read_cpuxml,
117+
sizeof(gdb_xfer_read_cpuxml)))
118+
strcpy(remcom_out_buffer, riscv_gdb_stub_cpuxml);
119+
}
120+
106121
static inline void kgdb_arch_update_addr(struct pt_regs *regs,
107122
char *remcom_in_buffer)
108123
{

0 commit comments

Comments
 (0)