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