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