GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / mips / kernel / kgdb.c
1 /*
2  *  Originally written by Glenn Engel, Lake Stevens Instrument Division
3  *
4  *  Contributed by HP Systems
5  *
6  *  Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
7  *  Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
8  *
9  *  Copyright (C) 1995 Andreas Busse
10  *
11  *  Copyright (C) 2003 MontaVista Software Inc.
12  *  Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
13  *
14  *  Copyright (C) 2004-2005 MontaVista Software Inc.
15  *  Author: Manish Lachwani, mlachwani@mvista.com or manish@koffee-break.com
16  *
17  *  Copyright (C) 2007-2008 Wind River Systems, Inc.
18  *  Author/Maintainer: Jason Wessel, jason.wessel@windriver.com
19  *
20  *  This file is licensed under the terms of the GNU General Public License
21  *  version 2. This program is licensed "as is" without any warranty of any
22  *  kind, whether express or implied.
23  */
24
25 #include <linux/ptrace.h>               /* for linux pt_regs struct */
26 #include <linux/kgdb.h>
27 #include <linux/kdebug.h>
28 #include <linux/sched.h>
29 #include <linux/smp.h>
30 #include <asm/inst.h>
31 #include <asm/fpu.h>
32 #include <asm/cacheflush.h>
33 #include <asm/processor.h>
34 #include <asm/sigcontext.h>
35 #include <linux/uaccess.h>
36 #include <asm/irq_regs.h>
37
38 static struct hard_trap_info {
39         unsigned char tt;       /* Trap type code for MIPS R3xxx and R4xxx */
40         unsigned char signo;    /* Signal that we map this trap into */
41 } hard_trap_info[] = {
42         { 6, SIGBUS },          /* instruction bus error */
43         { 7, SIGBUS },          /* data bus error */
44         { 9, SIGTRAP },         /* break */
45 /*      { 11, SIGILL }, */      /* CPU unusable */
46         { 12, SIGFPE },         /* overflow */
47         { 13, SIGTRAP },        /* trap */
48         { 14, SIGSEGV },        /* virtual instruction cache coherency */
49         { 15, SIGFPE },         /* floating point exception */
50         { 23, SIGSEGV },        /* watch */
51         { 31, SIGSEGV },        /* virtual data cache coherency */
52         { 0, 0}                 /* Must be last */
53 };
54
55 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
56 {
57         { "zero", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[0]) },
58         { "at", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[1]) },
59         { "v0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[2]) },
60         { "v1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[3]) },
61         { "a0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[4]) },
62         { "a1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[5]) },
63         { "a2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[6]) },
64         { "a3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[7]) },
65         { "t0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[8]) },
66         { "t1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[9]) },
67         { "t2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[10]) },
68         { "t3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[11]) },
69         { "t4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[12]) },
70         { "t5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[13]) },
71         { "t6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[14]) },
72         { "t7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[15]) },
73         { "s0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[16]) },
74         { "s1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[17]) },
75         { "s2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[18]) },
76         { "s3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[19]) },
77         { "s4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[20]) },
78         { "s5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[21]) },
79         { "s6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[22]) },
80         { "s7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[23]) },
81         { "t8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[24]) },
82         { "t9", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[25]) },
83         { "k0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[26]) },
84         { "k1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[27]) },
85         { "gp", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[28]) },
86         { "sp", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[29]) },
87         { "s8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[30]) },
88         { "ra", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[31]) },
89         { "sr", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_status) },
90         { "lo", GDB_SIZEOF_REG, offsetof(struct pt_regs, lo) },
91         { "hi", GDB_SIZEOF_REG, offsetof(struct pt_regs, hi) },
92         { "bad", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_badvaddr) },
93         { "cause", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_cause) },
94         { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_epc) },
95         { "f0", GDB_SIZEOF_REG, 0 },
96         { "f1", GDB_SIZEOF_REG, 1 },
97         { "f2", GDB_SIZEOF_REG, 2 },
98         { "f3", GDB_SIZEOF_REG, 3 },
99         { "f4", GDB_SIZEOF_REG, 4 },
100         { "f5", GDB_SIZEOF_REG, 5 },
101         { "f6", GDB_SIZEOF_REG, 6 },
102         { "f7", GDB_SIZEOF_REG, 7 },
103         { "f8", GDB_SIZEOF_REG, 8 },
104         { "f9", GDB_SIZEOF_REG, 9 },
105         { "f10", GDB_SIZEOF_REG, 10 },
106         { "f11", GDB_SIZEOF_REG, 11 },
107         { "f12", GDB_SIZEOF_REG, 12 },
108         { "f13", GDB_SIZEOF_REG, 13 },
109         { "f14", GDB_SIZEOF_REG, 14 },
110         { "f15", GDB_SIZEOF_REG, 15 },
111         { "f16", GDB_SIZEOF_REG, 16 },
112         { "f17", GDB_SIZEOF_REG, 17 },
113         { "f18", GDB_SIZEOF_REG, 18 },
114         { "f19", GDB_SIZEOF_REG, 19 },
115         { "f20", GDB_SIZEOF_REG, 20 },
116         { "f21", GDB_SIZEOF_REG, 21 },
117         { "f22", GDB_SIZEOF_REG, 22 },
118         { "f23", GDB_SIZEOF_REG, 23 },
119         { "f24", GDB_SIZEOF_REG, 24 },
120         { "f25", GDB_SIZEOF_REG, 25 },
121         { "f26", GDB_SIZEOF_REG, 26 },
122         { "f27", GDB_SIZEOF_REG, 27 },
123         { "f28", GDB_SIZEOF_REG, 28 },
124         { "f29", GDB_SIZEOF_REG, 29 },
125         { "f30", GDB_SIZEOF_REG, 30 },
126         { "f31", GDB_SIZEOF_REG, 31 },
127         { "fsr", GDB_SIZEOF_REG, 0 },
128         { "fir", GDB_SIZEOF_REG, 0 },
129 };
130
131 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
132 {
133         int fp_reg;
134
135         if (regno < 0 || regno >= DBG_MAX_REG_NUM)
136                 return -EINVAL;
137
138         if (dbg_reg_def[regno].offset != -1 && regno < 38) {
139                 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
140                        dbg_reg_def[regno].size);
141         } else if (current && dbg_reg_def[regno].offset != -1 && regno < 72) {
142                 /* FP registers 38 -> 69 */
143                 if (!(regs->cp0_status & ST0_CU1))
144                         return 0;
145                 if (regno == 70) {
146                         /* Process the fcr31/fsr (register 70) */
147                         memcpy((void *)&current->thread.fpu.fcr31, mem,
148                                dbg_reg_def[regno].size);
149                         goto out_save;
150                 } else if (regno == 71) {
151                         /* Ignore the fir (register 71) */
152                         goto out_save;
153                 }
154                 fp_reg = dbg_reg_def[regno].offset;
155                 memcpy((void *)&current->thread.fpu.fpr[fp_reg], mem,
156                        dbg_reg_def[regno].size);
157 out_save:
158                 restore_fp(current);
159         }
160
161         return 0;
162 }
163
164 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
165 {
166         int fp_reg;
167
168         if (regno >= DBG_MAX_REG_NUM || regno < 0)
169                 return NULL;
170
171         if (dbg_reg_def[regno].offset != -1 && regno < 38) {
172                 /* First 38 registers */
173                 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
174                        dbg_reg_def[regno].size);
175         } else if (current && dbg_reg_def[regno].offset != -1 && regno < 72) {
176                 /* FP registers 38 -> 69 */
177                 if (!(regs->cp0_status & ST0_CU1))
178                         goto out;
179                 save_fp(current);
180                 if (regno == 70) {
181                         /* Process the fcr31/fsr (register 70) */
182                         memcpy(mem, (void *)&current->thread.fpu.fcr31,
183                                dbg_reg_def[regno].size);
184                         goto out;
185                 } else if (regno == 71) {
186                         /* Ignore the fir (register 71) */
187                         memset(mem, 0, dbg_reg_def[regno].size);
188                         goto out;
189                 }
190                 fp_reg = dbg_reg_def[regno].offset;
191                 memcpy(mem, (void *)&current->thread.fpu.fpr[fp_reg],
192                        dbg_reg_def[regno].size);
193         }
194
195 out:
196         return dbg_reg_def[regno].name;
197
198 }
199
200 void arch_kgdb_breakpoint(void)
201 {
202         __asm__ __volatile__(
203                 ".globl breakinst\n\t"
204                 ".set\tnoreorder\n\t"
205                 "nop\n"
206                 "breakinst:\tbreak\n\t"
207                 "nop\n\t"
208                 ".set\treorder");
209 }
210
211 static void kgdb_call_nmi_hook(void *ignored)
212 {
213         mm_segment_t old_fs;
214
215         old_fs = get_fs();
216         set_fs(get_ds());
217
218         kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
219
220         set_fs(old_fs);
221 }
222
223 void kgdb_roundup_cpus(unsigned long flags)
224 {
225         local_irq_enable();
226         smp_call_function(kgdb_call_nmi_hook, NULL, 0);
227         local_irq_disable();
228 }
229
230 static int compute_signal(int tt)
231 {
232         struct hard_trap_info *ht;
233
234         for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
235                 if (ht->tt == tt)
236                         return ht->signo;
237
238         return SIGHUP;          /* default for things we don't know about */
239 }
240
241 /*
242  * Similar to regs_to_gdb_regs() except that process is sleeping and so
243  * we may not be able to get all the info.
244  */
245 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
246 {
247         int reg;
248 #if (KGDB_GDB_REG_SIZE == 32)
249         u32 *ptr = (u32 *)gdb_regs;
250 #else
251         u64 *ptr = (u64 *)gdb_regs;
252 #endif
253
254         for (reg = 0; reg < 16; reg++)
255                 *(ptr++) = 0;
256
257         /* S0 - S7 */
258         *(ptr++) = p->thread.reg16;
259         *(ptr++) = p->thread.reg17;
260         *(ptr++) = p->thread.reg18;
261         *(ptr++) = p->thread.reg19;
262         *(ptr++) = p->thread.reg20;
263         *(ptr++) = p->thread.reg21;
264         *(ptr++) = p->thread.reg22;
265         *(ptr++) = p->thread.reg23;
266
267         for (reg = 24; reg < 28; reg++)
268                 *(ptr++) = 0;
269
270         /* GP, SP, FP, RA */
271         *(ptr++) = (long)p;
272         *(ptr++) = p->thread.reg29;
273         *(ptr++) = p->thread.reg30;
274         *(ptr++) = p->thread.reg31;
275
276         *(ptr++) = p->thread.cp0_status;
277
278         /* lo, hi */
279         *(ptr++) = 0;
280         *(ptr++) = 0;
281
282         /*
283          * BadVAddr, Cause
284          * Ideally these would come from the last exception frame up the stack
285          * but that requires unwinding, otherwise we can't know much for sure.
286          */
287         *(ptr++) = 0;
288         *(ptr++) = 0;
289
290         /*
291          * PC
292          * use return address (RA), i.e. the moment after return from resume()
293          */
294         *(ptr++) = p->thread.reg31;
295 }
296
297 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
298 {
299         regs->cp0_epc = pc;
300 }
301
302 /*
303  * Calls linux_debug_hook before the kernel dies. If KGDB is enabled,
304  * then try to fall into the debugger
305  */
306 static int kgdb_mips_notify(struct notifier_block *self, unsigned long cmd,
307                             void *ptr)
308 {
309         struct die_args *args = (struct die_args *)ptr;
310         struct pt_regs *regs = args->regs;
311         int trap = (regs->cp0_cause & 0x7c) >> 2;
312         mm_segment_t old_fs;
313
314 #ifdef CONFIG_KPROBES
315         /*
316          * Return immediately if the kprobes fault notifier has set
317          * DIE_PAGE_FAULT.
318          */
319         if (cmd == DIE_PAGE_FAULT)
320                 return NOTIFY_DONE;
321 #endif /* CONFIG_KPROBES */
322
323         /* Userspace events, ignore. */
324         if (user_mode(regs))
325                 return NOTIFY_DONE;
326
327         /* Kernel mode. Set correct address limit */
328         old_fs = get_fs();
329         set_fs(get_ds());
330
331         if (atomic_read(&kgdb_active) != -1)
332                 kgdb_nmicallback(smp_processor_id(), regs);
333
334         if (kgdb_handle_exception(trap, compute_signal(trap), cmd, regs)) {
335                 set_fs(old_fs);
336                 return NOTIFY_DONE;
337         }
338
339         if (atomic_read(&kgdb_setting_breakpoint))
340                 if ((trap == 9) && (regs->cp0_epc == (unsigned long)breakinst))
341                         regs->cp0_epc += 4;
342
343         /* In SMP mode, __flush_cache_all does IPI */
344         local_irq_enable();
345         __flush_cache_all();
346
347         set_fs(old_fs);
348         return NOTIFY_STOP;
349 }
350
351 #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
352 int kgdb_ll_trap(int cmd, const char *str,
353                  struct pt_regs *regs, long err, int trap, int sig)
354 {
355         struct die_args args = {
356                 .regs   = regs,
357                 .str    = str,
358                 .err    = err,
359                 .trapnr = trap,
360                 .signr  = sig,
361
362         };
363
364         if (!kgdb_io_module_registered)
365                 return NOTIFY_DONE;
366
367         return kgdb_mips_notify(NULL, cmd, &args);
368 }
369 #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
370
371 static struct notifier_block kgdb_notifier = {
372         .notifier_call = kgdb_mips_notify,
373 };
374
375 /*
376  * Handle the 'c' command
377  */
378 int kgdb_arch_handle_exception(int vector, int signo, int err_code,
379                                char *remcom_in_buffer, char *remcom_out_buffer,
380                                struct pt_regs *regs)
381 {
382         char *ptr;
383         unsigned long address;
384
385         switch (remcom_in_buffer[0]) {
386         case 'c':
387                 /* handle the optional parameter */
388                 ptr = &remcom_in_buffer[1];
389                 if (kgdb_hex2long(&ptr, &address))
390                         regs->cp0_epc = address;
391
392                 return 0;
393         }
394
395         return -1;
396 }
397
398 struct kgdb_arch arch_kgdb_ops;
399
400 int kgdb_arch_init(void)
401 {
402         union mips_instruction insn = {
403                 .r_format = {
404                         .opcode = spec_op,
405                         .func   = break_op,
406                 }
407         };
408         memcpy(arch_kgdb_ops.gdb_bpt_instr, insn.byte, BREAK_INSTR_SIZE);
409
410         register_die_notifier(&kgdb_notifier);
411
412         return 0;
413 }
414
415 /*
416  *      kgdb_arch_exit - Perform any architecture specific uninitalization.
417  *
418  *      This function will handle the uninitalization of any architecture
419  *      specific callbacks, for dynamic registration and unregistration.
420  */
421 void kgdb_arch_exit(void)
422 {
423         unregister_die_notifier(&kgdb_notifier);
424 }