blob: e017a9493b92e907235cc19e7df7e53397c3f56e [file] [log] [blame]
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +05301/*
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
AKASHI Takahiro67787b62016-09-23 16:42:08 +090022#include <linux/bug.h>
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +053023#include <linux/irq.h>
24#include <linux/kdebug.h>
25#include <linux/kgdb.h>
Pratyush Anand44b53f62016-07-08 12:35:49 -040026#include <linux/kprobes.h>
AKASHI Takahiro67787b62016-09-23 16:42:08 +090027#include <asm/debug-monitors.h>
28#include <asm/insn.h>
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +053029#include <asm/traps.h>
30
31struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
32 { "x0", 8, offsetof(struct pt_regs, regs[0])},
33 { "x1", 8, offsetof(struct pt_regs, regs[1])},
34 { "x2", 8, offsetof(struct pt_regs, regs[2])},
35 { "x3", 8, offsetof(struct pt_regs, regs[3])},
36 { "x4", 8, offsetof(struct pt_regs, regs[4])},
37 { "x5", 8, offsetof(struct pt_regs, regs[5])},
38 { "x6", 8, offsetof(struct pt_regs, regs[6])},
39 { "x7", 8, offsetof(struct pt_regs, regs[7])},
40 { "x8", 8, offsetof(struct pt_regs, regs[8])},
41 { "x9", 8, offsetof(struct pt_regs, regs[9])},
42 { "x10", 8, offsetof(struct pt_regs, regs[10])},
43 { "x11", 8, offsetof(struct pt_regs, regs[11])},
44 { "x12", 8, offsetof(struct pt_regs, regs[12])},
45 { "x13", 8, offsetof(struct pt_regs, regs[13])},
46 { "x14", 8, offsetof(struct pt_regs, regs[14])},
47 { "x15", 8, offsetof(struct pt_regs, regs[15])},
48 { "x16", 8, offsetof(struct pt_regs, regs[16])},
49 { "x17", 8, offsetof(struct pt_regs, regs[17])},
50 { "x18", 8, offsetof(struct pt_regs, regs[18])},
51 { "x19", 8, offsetof(struct pt_regs, regs[19])},
52 { "x20", 8, offsetof(struct pt_regs, regs[20])},
53 { "x21", 8, offsetof(struct pt_regs, regs[21])},
54 { "x22", 8, offsetof(struct pt_regs, regs[22])},
55 { "x23", 8, offsetof(struct pt_regs, regs[23])},
56 { "x24", 8, offsetof(struct pt_regs, regs[24])},
57 { "x25", 8, offsetof(struct pt_regs, regs[25])},
58 { "x26", 8, offsetof(struct pt_regs, regs[26])},
59 { "x27", 8, offsetof(struct pt_regs, regs[27])},
60 { "x28", 8, offsetof(struct pt_regs, regs[28])},
61 { "x29", 8, offsetof(struct pt_regs, regs[29])},
62 { "x30", 8, offsetof(struct pt_regs, regs[30])},
63 { "sp", 8, offsetof(struct pt_regs, sp)},
64 { "pc", 8, offsetof(struct pt_regs, pc)},
Daniel Thompson0d15ef62016-06-16 16:51:52 +010065 /*
66 * struct pt_regs thinks PSTATE is 64-bits wide but gdb remote
67 * protocol disagrees. Therefore we must extract only the lower
68 * 32-bits. Look for the big comment in asm/kgdb.h for more
69 * detail.
70 */
71 { "pstate", 4, offsetof(struct pt_regs, pstate)
72#ifdef CONFIG_CPU_BIG_ENDIAN
73 + 4
74#endif
75 },
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +053076 { "v0", 16, -1 },
77 { "v1", 16, -1 },
78 { "v2", 16, -1 },
79 { "v3", 16, -1 },
80 { "v4", 16, -1 },
81 { "v5", 16, -1 },
82 { "v6", 16, -1 },
83 { "v7", 16, -1 },
84 { "v8", 16, -1 },
85 { "v9", 16, -1 },
86 { "v10", 16, -1 },
87 { "v11", 16, -1 },
88 { "v12", 16, -1 },
89 { "v13", 16, -1 },
90 { "v14", 16, -1 },
91 { "v15", 16, -1 },
92 { "v16", 16, -1 },
93 { "v17", 16, -1 },
94 { "v18", 16, -1 },
95 { "v19", 16, -1 },
96 { "v20", 16, -1 },
97 { "v21", 16, -1 },
98 { "v22", 16, -1 },
99 { "v23", 16, -1 },
100 { "v24", 16, -1 },
101 { "v25", 16, -1 },
102 { "v26", 16, -1 },
103 { "v27", 16, -1 },
104 { "v28", 16, -1 },
105 { "v29", 16, -1 },
106 { "v30", 16, -1 },
107 { "v31", 16, -1 },
108 { "fpsr", 4, -1 },
109 { "fpcr", 4, -1 },
110};
111
112char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
113{
114 if (regno >= DBG_MAX_REG_NUM || regno < 0)
115 return NULL;
116
117 if (dbg_reg_def[regno].offset != -1)
118 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
119 dbg_reg_def[regno].size);
120 else
121 memset(mem, 0, dbg_reg_def[regno].size);
122 return dbg_reg_def[regno].name;
123}
124
125int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
126{
127 if (regno >= DBG_MAX_REG_NUM || regno < 0)
128 return -EINVAL;
129
130 if (dbg_reg_def[regno].offset != -1)
131 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
132 dbg_reg_def[regno].size);
133 return 0;
134}
135
136void
137sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
138{
139 struct pt_regs *thread_regs;
140
141 /* Initialize to zero */
142 memset((char *)gdb_regs, 0, NUMREGBYTES);
143 thread_regs = task_pt_regs(task);
144 memcpy((void *)gdb_regs, (void *)thread_regs->regs, GP_REG_BYTES);
Daniel Thompson0d15ef62016-06-16 16:51:52 +0100145 /* Special case for PSTATE (check comments in asm/kgdb.h for details) */
146 dbg_get_reg(33, gdb_regs + GP_REG_BYTES, thread_regs);
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530147}
148
149void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
150{
151 regs->pc = pc;
152}
153
154static int compiled_break;
155
Vijaya Kumar K44679a4f2014-01-28 11:20:19 +0000156static void kgdb_arch_update_addr(struct pt_regs *regs,
157 char *remcom_in_buffer)
158{
159 unsigned long addr;
160 char *ptr;
161
162 ptr = &remcom_in_buffer[1];
163 if (kgdb_hex2long(&ptr, &addr))
164 kgdb_arch_set_pc(regs, addr);
165 else if (compiled_break == 1)
166 kgdb_arch_set_pc(regs, regs->pc + 4);
167
168 compiled_break = 0;
169}
170
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530171int kgdb_arch_handle_exception(int exception_vector, int signo,
172 int err_code, char *remcom_in_buffer,
173 char *remcom_out_buffer,
174 struct pt_regs *linux_regs)
175{
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530176 int err;
177
178 switch (remcom_in_buffer[0]) {
179 case 'D':
180 case 'k':
181 /*
182 * Packet D (Detach), k (kill). No special handling
183 * is required here. Handle same as c packet.
184 */
185 case 'c':
186 /*
187 * Packet c (Continue) to continue executing.
188 * Set pc to required address.
189 * Try to read optional parameter and set pc.
190 * If this was a compiled breakpoint, we need to move
191 * to the next instruction else we will just breakpoint
192 * over and over again.
193 */
Vijaya Kumar K44679a4f2014-01-28 11:20:19 +0000194 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
195 atomic_set(&kgdb_cpu_doing_single_step, -1);
196 kgdb_single_step = 0;
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530197
Vijaya Kumar K44679a4f2014-01-28 11:20:19 +0000198 /*
199 * Received continue command, disable single step
200 */
201 if (kernel_active_single_step())
202 kernel_disable_single_step();
203
204 err = 0;
205 break;
206 case 's':
207 /*
208 * Update step address value with address passed
209 * with step packet.
210 * On debug exception return PC is copied to ELR
211 * So just update PC.
212 * If no step address is passed, resume from the address
213 * pointed by PC. Do not update PC
214 */
215 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
216 atomic_set(&kgdb_cpu_doing_single_step, raw_smp_processor_id());
217 kgdb_single_step = 1;
218
219 /*
220 * Enable single step handling
221 */
222 if (!kernel_active_single_step())
223 kernel_enable_single_step(linux_regs);
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530224 err = 0;
225 break;
226 default:
227 err = -1;
228 }
229 return err;
230}
231
232static int kgdb_brk_fn(struct pt_regs *regs, unsigned int esr)
233{
234 kgdb_handle_exception(1, SIGTRAP, 0, regs);
235 return 0;
236}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400237NOKPROBE_SYMBOL(kgdb_brk_fn)
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530238
239static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr)
240{
241 compiled_break = 1;
242 kgdb_handle_exception(1, SIGTRAP, 0, regs);
243
244 return 0;
245}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400246NOKPROBE_SYMBOL(kgdb_compiled_brk_fn);
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530247
Vijaya Kumar K44679a4f2014-01-28 11:20:19 +0000248static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
249{
250 kgdb_handle_exception(1, SIGTRAP, 0, regs);
251 return 0;
252}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400253NOKPROBE_SYMBOL(kgdb_step_brk_fn);
Vijaya Kumar K44679a4f2014-01-28 11:20:19 +0000254
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530255static struct break_hook kgdb_brkpt_hook = {
256 .esr_mask = 0xffffffff,
Dave P Martinc696b932015-07-24 16:37:46 +0100257 .esr_val = (u32)ESR_ELx_VAL_BRK64(KGDB_DYN_DBG_BRK_IMM),
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530258 .fn = kgdb_brk_fn
259};
260
261static struct break_hook kgdb_compiled_brkpt_hook = {
262 .esr_mask = 0xffffffff,
Dave P Martinc696b932015-07-24 16:37:46 +0100263 .esr_val = (u32)ESR_ELx_VAL_BRK64(KGDB_COMPILED_DBG_BRK_IMM),
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530264 .fn = kgdb_compiled_brk_fn
265};
266
Vijaya Kumar K44679a4f2014-01-28 11:20:19 +0000267static struct step_hook kgdb_step_hook = {
268 .fn = kgdb_step_brk_fn
269};
270
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530271static void kgdb_call_nmi_hook(void *ignored)
272{
273 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
274}
275
276void kgdb_roundup_cpus(unsigned long flags)
277{
278 local_irq_enable();
279 smp_call_function(kgdb_call_nmi_hook, NULL, 0);
280 local_irq_disable();
281}
282
283static int __kgdb_notify(struct die_args *args, unsigned long cmd)
284{
285 struct pt_regs *regs = args->regs;
286
287 if (kgdb_handle_exception(1, args->signr, cmd, regs))
288 return NOTIFY_DONE;
289 return NOTIFY_STOP;
290}
291
292static int
293kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
294{
295 unsigned long flags;
296 int ret;
297
298 local_irq_save(flags);
299 ret = __kgdb_notify(ptr, cmd);
300 local_irq_restore(flags);
301
302 return ret;
303}
304
305static struct notifier_block kgdb_notifier = {
306 .notifier_call = kgdb_notify,
307 /*
308 * Want to be lowest priority
309 */
310 .priority = -INT_MAX,
311};
312
313/*
Adam Buchbinderef769e32016-02-24 09:52:41 -0800314 * kgdb_arch_init - Perform any architecture specific initialization.
315 * This function will handle the initialization of any architecture
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530316 * specific callbacks.
317 */
318int kgdb_arch_init(void)
319{
320 int ret = register_die_notifier(&kgdb_notifier);
321
322 if (ret != 0)
323 return ret;
324
325 register_break_hook(&kgdb_brkpt_hook);
326 register_break_hook(&kgdb_compiled_brkpt_hook);
Vijaya Kumar K44679a4f2014-01-28 11:20:19 +0000327 register_step_hook(&kgdb_step_hook);
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530328 return 0;
329}
330
331/*
332 * kgdb_arch_exit - Perform any architecture specific uninitalization.
333 * This function will handle the uninitalization of any architecture
334 * specific callbacks, for dynamic registration and unregistration.
335 */
336void kgdb_arch_exit(void)
337{
338 unregister_break_hook(&kgdb_brkpt_hook);
339 unregister_break_hook(&kgdb_compiled_brkpt_hook);
Vijaya Kumar K44679a4f2014-01-28 11:20:19 +0000340 unregister_step_hook(&kgdb_step_hook);
Vijaya Kumar Kbcf57632014-01-28 16:50:18 +0530341 unregister_die_notifier(&kgdb_notifier);
342}
343
AKASHI Takahiro67787b62016-09-23 16:42:08 +0900344struct kgdb_arch arch_kgdb_ops;
345
346int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
347{
348 int err;
349
350 BUILD_BUG_ON(AARCH64_INSN_SIZE != BREAK_INSTR_SIZE);
351
352 err = aarch64_insn_read((void *)bpt->bpt_addr, (u32 *)bpt->saved_instr);
353 if (err)
354 return err;
355
356 return aarch64_insn_write((void *)bpt->bpt_addr,
357 (u32)AARCH64_BREAK_KGDB_DYN_DBG);
358}
359
360int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
361{
362 return aarch64_insn_write((void *)bpt->bpt_addr,
363 *(u32 *)bpt->saved_instr);
364}