xref: /linux/arch/arm64/kernel/kgdb.c (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AArch64 KGDB support
4  *
5  * Based on arch/arm/kernel/kgdb.c
6  *
7  * Copyright (C) 2013 Cavium Inc.
8  * Author: Vijaya Kumar K <vijaya.kumar@caviumnetworks.com>
9  */
10 
11 #include <linux/bug.h>
12 #include <linux/irq.h>
13 #include <linux/kdebug.h>
14 #include <linux/kgdb.h>
15 #include <linux/kprobes.h>
16 #include <linux/sched/task_stack.h>
17 
18 #include <asm/debug-monitors.h>
19 #include <asm/insn.h>
20 #include <asm/text-patching.h>
21 #include <asm/traps.h>
22 
23 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
24 	{ "x0", 8, offsetof(struct pt_regs, regs[0])},
25 	{ "x1", 8, offsetof(struct pt_regs, regs[1])},
26 	{ "x2", 8, offsetof(struct pt_regs, regs[2])},
27 	{ "x3", 8, offsetof(struct pt_regs, regs[3])},
28 	{ "x4", 8, offsetof(struct pt_regs, regs[4])},
29 	{ "x5", 8, offsetof(struct pt_regs, regs[5])},
30 	{ "x6", 8, offsetof(struct pt_regs, regs[6])},
31 	{ "x7", 8, offsetof(struct pt_regs, regs[7])},
32 	{ "x8", 8, offsetof(struct pt_regs, regs[8])},
33 	{ "x9", 8, offsetof(struct pt_regs, regs[9])},
34 	{ "x10", 8, offsetof(struct pt_regs, regs[10])},
35 	{ "x11", 8, offsetof(struct pt_regs, regs[11])},
36 	{ "x12", 8, offsetof(struct pt_regs, regs[12])},
37 	{ "x13", 8, offsetof(struct pt_regs, regs[13])},
38 	{ "x14", 8, offsetof(struct pt_regs, regs[14])},
39 	{ "x15", 8, offsetof(struct pt_regs, regs[15])},
40 	{ "x16", 8, offsetof(struct pt_regs, regs[16])},
41 	{ "x17", 8, offsetof(struct pt_regs, regs[17])},
42 	{ "x18", 8, offsetof(struct pt_regs, regs[18])},
43 	{ "x19", 8, offsetof(struct pt_regs, regs[19])},
44 	{ "x20", 8, offsetof(struct pt_regs, regs[20])},
45 	{ "x21", 8, offsetof(struct pt_regs, regs[21])},
46 	{ "x22", 8, offsetof(struct pt_regs, regs[22])},
47 	{ "x23", 8, offsetof(struct pt_regs, regs[23])},
48 	{ "x24", 8, offsetof(struct pt_regs, regs[24])},
49 	{ "x25", 8, offsetof(struct pt_regs, regs[25])},
50 	{ "x26", 8, offsetof(struct pt_regs, regs[26])},
51 	{ "x27", 8, offsetof(struct pt_regs, regs[27])},
52 	{ "x28", 8, offsetof(struct pt_regs, regs[28])},
53 	{ "x29", 8, offsetof(struct pt_regs, regs[29])},
54 	{ "x30", 8, offsetof(struct pt_regs, regs[30])},
55 	{ "sp", 8, offsetof(struct pt_regs, sp)},
56 	{ "pc", 8, offsetof(struct pt_regs, pc)},
57 	/*
58 	 * struct pt_regs thinks PSTATE is 64-bits wide but gdb remote
59 	 * protocol disagrees. Therefore we must extract only the lower
60 	 * 32-bits. Look for the big comment in asm/kgdb.h for more
61 	 * detail.
62 	 */
63 	{ "pstate", 4, offsetof(struct pt_regs, pstate)},
64 	{ "v0", 16, -1 },
65 	{ "v1", 16, -1 },
66 	{ "v2", 16, -1 },
67 	{ "v3", 16, -1 },
68 	{ "v4", 16, -1 },
69 	{ "v5", 16, -1 },
70 	{ "v6", 16, -1 },
71 	{ "v7", 16, -1 },
72 	{ "v8", 16, -1 },
73 	{ "v9", 16, -1 },
74 	{ "v10", 16, -1 },
75 	{ "v11", 16, -1 },
76 	{ "v12", 16, -1 },
77 	{ "v13", 16, -1 },
78 	{ "v14", 16, -1 },
79 	{ "v15", 16, -1 },
80 	{ "v16", 16, -1 },
81 	{ "v17", 16, -1 },
82 	{ "v18", 16, -1 },
83 	{ "v19", 16, -1 },
84 	{ "v20", 16, -1 },
85 	{ "v21", 16, -1 },
86 	{ "v22", 16, -1 },
87 	{ "v23", 16, -1 },
88 	{ "v24", 16, -1 },
89 	{ "v25", 16, -1 },
90 	{ "v26", 16, -1 },
91 	{ "v27", 16, -1 },
92 	{ "v28", 16, -1 },
93 	{ "v29", 16, -1 },
94 	{ "v30", 16, -1 },
95 	{ "v31", 16, -1 },
96 	{ "fpsr", 4, -1 },
97 	{ "fpcr", 4, -1 },
98 };
99 
100 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
101 {
102 	if (regno >= DBG_MAX_REG_NUM || regno < 0)
103 		return NULL;
104 
105 	if (dbg_reg_def[regno].offset != -1)
106 		memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
107 		       dbg_reg_def[regno].size);
108 	else
109 		memset(mem, 0, dbg_reg_def[regno].size);
110 	return dbg_reg_def[regno].name;
111 }
112 
113 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
114 {
115 	if (regno >= DBG_MAX_REG_NUM || regno < 0)
116 		return -EINVAL;
117 
118 	if (dbg_reg_def[regno].offset != -1)
119 		memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
120 		       dbg_reg_def[regno].size);
121 	return 0;
122 }
123 
124 void
125 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
126 {
127 	struct cpu_context *cpu_context = &task->thread.cpu_context;
128 
129 	/* Initialize to zero */
130 	memset((char *)gdb_regs, 0, NUMREGBYTES);
131 
132 	gdb_regs[19] = cpu_context->x19;
133 	gdb_regs[20] = cpu_context->x20;
134 	gdb_regs[21] = cpu_context->x21;
135 	gdb_regs[22] = cpu_context->x22;
136 	gdb_regs[23] = cpu_context->x23;
137 	gdb_regs[24] = cpu_context->x24;
138 	gdb_regs[25] = cpu_context->x25;
139 	gdb_regs[26] = cpu_context->x26;
140 	gdb_regs[27] = cpu_context->x27;
141 	gdb_regs[28] = cpu_context->x28;
142 	gdb_regs[29] = cpu_context->fp;
143 
144 	gdb_regs[31] = cpu_context->sp;
145 	gdb_regs[32] = cpu_context->pc;
146 }
147 
148 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
149 {
150 	regs->pc = pc;
151 }
152 
153 static int compiled_break;
154 
155 static void kgdb_arch_update_addr(struct pt_regs *regs,
156 				char *remcom_in_buffer)
157 {
158 	unsigned long addr;
159 	char *ptr;
160 
161 	ptr = &remcom_in_buffer[1];
162 	if (kgdb_hex2long(&ptr, &addr))
163 		kgdb_arch_set_pc(regs, addr);
164 	else if (compiled_break == 1)
165 		kgdb_arch_set_pc(regs, regs->pc + 4);
166 
167 	compiled_break = 0;
168 }
169 
170 int kgdb_arch_handle_exception(int exception_vector, int signo,
171 			       int err_code, char *remcom_in_buffer,
172 			       char *remcom_out_buffer,
173 			       struct pt_regs *linux_regs)
174 {
175 	int err;
176 
177 	switch (remcom_in_buffer[0]) {
178 	case 'D':
179 	case 'k':
180 		/*
181 		 * Packet D (Detach), k (kill). No special handling
182 		 * is required here. Handle same as c packet.
183 		 */
184 	case 'c':
185 		/*
186 		 * Packet c (Continue) to continue executing.
187 		 * Set pc to required address.
188 		 * Try to read optional parameter and set pc.
189 		 * If this was a compiled breakpoint, we need to move
190 		 * to the next instruction else we will just breakpoint
191 		 * over and over again.
192 		 */
193 		kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
194 		atomic_set(&kgdb_cpu_doing_single_step, -1);
195 		kgdb_single_step =  0;
196 
197 		/*
198 		 * Received continue command, disable single step
199 		 */
200 		if (kernel_active_single_step())
201 			kernel_disable_single_step();
202 
203 		err = 0;
204 		break;
205 	case 's':
206 		/*
207 		 * Update step address value with address passed
208 		 * with step packet.
209 		 * On debug exception return PC is copied to ELR
210 		 * So just update PC.
211 		 * If no step address is passed, resume from the address
212 		 * pointed by PC. Do not update PC
213 		 */
214 		kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
215 		atomic_set(&kgdb_cpu_doing_single_step, raw_smp_processor_id());
216 		kgdb_single_step =  1;
217 
218 		/*
219 		 * Enable single step handling
220 		 */
221 		if (!kernel_active_single_step())
222 			kernel_enable_single_step(linux_regs);
223 		else
224 			kernel_rewind_single_step(linux_regs);
225 		err = 0;
226 		break;
227 	default:
228 		err = -1;
229 	}
230 	return err;
231 }
232 
233 int kgdb_brk_handler(struct pt_regs *regs, unsigned long esr)
234 {
235 	kgdb_handle_exception(1, SIGTRAP, 0, regs);
236 	return DBG_HOOK_HANDLED;
237 }
238 NOKPROBE_SYMBOL(kgdb_brk_handler)
239 
240 int kgdb_compiled_brk_handler(struct pt_regs *regs, unsigned long esr)
241 {
242 	compiled_break = 1;
243 	kgdb_handle_exception(1, SIGTRAP, 0, regs);
244 
245 	return DBG_HOOK_HANDLED;
246 }
247 NOKPROBE_SYMBOL(kgdb_compiled_brk_handler);
248 
249 int kgdb_single_step_handler(struct pt_regs *regs, unsigned long esr)
250 {
251 	if (!kgdb_single_step)
252 		return DBG_HOOK_ERROR;
253 
254 	kgdb_handle_exception(0, SIGTRAP, 0, regs);
255 	return DBG_HOOK_HANDLED;
256 }
257 NOKPROBE_SYMBOL(kgdb_single_step_handler);
258 
259 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
260 {
261 	struct pt_regs *regs = args->regs;
262 
263 	if (kgdb_handle_exception(1, args->signr, cmd, regs))
264 		return NOTIFY_DONE;
265 	return NOTIFY_STOP;
266 }
267 
268 static int
269 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
270 {
271 	unsigned long flags;
272 	int ret;
273 
274 	local_irq_save(flags);
275 	ret = __kgdb_notify(ptr, cmd);
276 	local_irq_restore(flags);
277 
278 	return ret;
279 }
280 
281 static struct notifier_block kgdb_notifier = {
282 	.notifier_call	= kgdb_notify,
283 	/*
284 	 * Want to be lowest priority
285 	 */
286 	.priority	= -INT_MAX,
287 };
288 
289 /*
290  * kgdb_arch_init - Perform any architecture specific initialization.
291  * This function will handle the initialization of any architecture
292  * specific callbacks.
293  */
294 int kgdb_arch_init(void)
295 {
296 	return register_die_notifier(&kgdb_notifier);
297 }
298 
299 /*
300  * kgdb_arch_exit - Perform any architecture specific uninitalization.
301  * This function will handle the uninitalization of any architecture
302  * specific callbacks, for dynamic registration and unregistration.
303  */
304 void kgdb_arch_exit(void)
305 {
306 	unregister_die_notifier(&kgdb_notifier);
307 }
308 
309 const struct kgdb_arch arch_kgdb_ops;
310 
311 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
312 {
313 	int err;
314 
315 	BUILD_BUG_ON(AARCH64_INSN_SIZE != BREAK_INSTR_SIZE);
316 
317 	err = aarch64_insn_read((void *)bpt->bpt_addr, (u32 *)bpt->saved_instr);
318 	if (err)
319 		return err;
320 
321 	return aarch64_insn_write((void *)bpt->bpt_addr,
322 			(u32)AARCH64_BREAK_KGDB_DYN_DBG);
323 }
324 
325 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
326 {
327 	return aarch64_insn_write((void *)bpt->bpt_addr,
328 			*(u32 *)bpt->saved_instr);
329 }
330