aefae46aa6464ddc6e89c3f691aa8359771bb49f
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / kgdb.c
1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by the
4  * Free Software Foundation; either version 2, or (at your option) any
5  * later version.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * General Public License for more details.
11  *
12  */
13
14 /*
15  * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com>
16  * Copyright (C) 2000-2001 VERITAS Software Corporation.
17  * Copyright (C) 2002 Andi Kleen, SuSE Labs
18  * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd.
19  * Copyright (C) 2007 MontaVista Software, Inc.
20  * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc.
21  */
22 /****************************************************************************
23  *  Contributor:     Lake Stevens Instrument Division$
24  *  Written by:      Glenn Engel $
25  *  Updated by:      Amit Kale<akale@veritas.com>
26  *  Updated by:      Tom Rini <trini@kernel.crashing.org>
27  *  Updated by:      Jason Wessel <jason.wessel@windriver.com>
28  *  Modified for 386 by Jim Kingdon, Cygnus Support.
29  *  Origianl kgdb, compatibility with 2.1.xx kernel by
30  *  David Grothe <dave@gcom.com>
31  *  Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com>
32  *  X86_64 changes from Andi Kleen's patch merged by Jim Houston
33  */
34 #include <linux/spinlock.h>
35 #include <linux/kdebug.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
38 #include <linux/ptrace.h>
39 #include <linux/sched.h>
40 #include <linux/delay.h>
41 #include <linux/kgdb.h>
42 #include <linux/init.h>
43 #include <linux/smp.h>
44 #include <linux/nmi.h>
45
46 #include <asm/debugreg.h>
47 #include <asm/apicdef.h>
48 #include <asm/system.h>
49
50 #include <asm/apic.h>
51
52 /*
53  * Put the error code here just in case the user cares:
54  */
55 static int gdb_x86errcode;
56
57 /*
58  * Likewise, the vector number here (since GDB only gets the signal
59  * number through the usual means, and that's not very specific):
60  */
61 static int gdb_x86vector = -1;
62
63 /**
64  *      pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
65  *      @gdb_regs: A pointer to hold the registers in the order GDB wants.
66  *      @regs: The &struct pt_regs of the current process.
67  *
68  *      Convert the pt_regs in @regs into the format for registers that
69  *      GDB expects, stored in @gdb_regs.
70  */
71 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
72 {
73 #ifndef CONFIG_X86_32
74         u32 *gdb_regs32 = (u32 *)gdb_regs;
75 #endif
76         gdb_regs[GDB_AX]        = regs->ax;
77         gdb_regs[GDB_BX]        = regs->bx;
78         gdb_regs[GDB_CX]        = regs->cx;
79         gdb_regs[GDB_DX]        = regs->dx;
80         gdb_regs[GDB_SI]        = regs->si;
81         gdb_regs[GDB_DI]        = regs->di;
82         gdb_regs[GDB_BP]        = regs->bp;
83         gdb_regs[GDB_PC]        = regs->ip;
84 #ifdef CONFIG_X86_32
85         gdb_regs[GDB_PS]        = regs->flags;
86         gdb_regs[GDB_DS]        = regs->ds;
87         gdb_regs[GDB_ES]        = regs->es;
88         gdb_regs[GDB_CS]        = regs->cs;
89         gdb_regs[GDB_FS]        = 0xFFFF;
90         gdb_regs[GDB_GS]        = 0xFFFF;
91         if (user_mode_vm(regs)) {
92                 gdb_regs[GDB_SS] = regs->ss;
93                 gdb_regs[GDB_SP] = regs->sp;
94         } else {
95                 gdb_regs[GDB_SS] = __KERNEL_DS;
96                 gdb_regs[GDB_SP] = kernel_stack_pointer(regs);
97         }
98 #else
99         gdb_regs[GDB_R8]        = regs->r8;
100         gdb_regs[GDB_R9]        = regs->r9;
101         gdb_regs[GDB_R10]       = regs->r10;
102         gdb_regs[GDB_R11]       = regs->r11;
103         gdb_regs[GDB_R12]       = regs->r12;
104         gdb_regs[GDB_R13]       = regs->r13;
105         gdb_regs[GDB_R14]       = regs->r14;
106         gdb_regs[GDB_R15]       = regs->r15;
107         gdb_regs32[GDB_PS]      = regs->flags;
108         gdb_regs32[GDB_CS]      = regs->cs;
109         gdb_regs32[GDB_SS]      = regs->ss;
110         gdb_regs[GDB_SP]        = kernel_stack_pointer(regs);
111 #endif
112 }
113
114 /**
115  *      sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
116  *      @gdb_regs: A pointer to hold the registers in the order GDB wants.
117  *      @p: The &struct task_struct of the desired process.
118  *
119  *      Convert the register values of the sleeping process in @p to
120  *      the format that GDB expects.
121  *      This function is called when kgdb does not have access to the
122  *      &struct pt_regs and therefore it should fill the gdb registers
123  *      @gdb_regs with what has been saved in &struct thread_struct
124  *      thread field during switch_to.
125  */
126 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
127 {
128 #ifndef CONFIG_X86_32
129         u32 *gdb_regs32 = (u32 *)gdb_regs;
130 #endif
131         gdb_regs[GDB_AX]        = 0;
132         gdb_regs[GDB_BX]        = 0;
133         gdb_regs[GDB_CX]        = 0;
134         gdb_regs[GDB_DX]        = 0;
135         gdb_regs[GDB_SI]        = 0;
136         gdb_regs[GDB_DI]        = 0;
137         gdb_regs[GDB_BP]        = *(unsigned long *)p->thread.sp;
138 #ifdef CONFIG_X86_32
139         gdb_regs[GDB_DS]        = __KERNEL_DS;
140         gdb_regs[GDB_ES]        = __KERNEL_DS;
141         gdb_regs[GDB_PS]        = 0;
142         gdb_regs[GDB_CS]        = __KERNEL_CS;
143         gdb_regs[GDB_PC]        = p->thread.ip;
144         gdb_regs[GDB_SS]        = __KERNEL_DS;
145         gdb_regs[GDB_FS]        = 0xFFFF;
146         gdb_regs[GDB_GS]        = 0xFFFF;
147 #else
148         gdb_regs32[GDB_PS]      = *(unsigned long *)(p->thread.sp + 8);
149         gdb_regs32[GDB_CS]      = __KERNEL_CS;
150         gdb_regs32[GDB_SS]      = __KERNEL_DS;
151         gdb_regs[GDB_PC]        = 0;
152         gdb_regs[GDB_R8]        = 0;
153         gdb_regs[GDB_R9]        = 0;
154         gdb_regs[GDB_R10]       = 0;
155         gdb_regs[GDB_R11]       = 0;
156         gdb_regs[GDB_R12]       = 0;
157         gdb_regs[GDB_R13]       = 0;
158         gdb_regs[GDB_R14]       = 0;
159         gdb_regs[GDB_R15]       = 0;
160 #endif
161         gdb_regs[GDB_SP]        = p->thread.sp;
162 }
163
164 /**
165  *      gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
166  *      @gdb_regs: A pointer to hold the registers we've received from GDB.
167  *      @regs: A pointer to a &struct pt_regs to hold these values in.
168  *
169  *      Convert the GDB regs in @gdb_regs into the pt_regs, and store them
170  *      in @regs.
171  */
172 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
173 {
174 #ifndef CONFIG_X86_32
175         u32 *gdb_regs32 = (u32 *)gdb_regs;
176 #endif
177         regs->ax                = gdb_regs[GDB_AX];
178         regs->bx                = gdb_regs[GDB_BX];
179         regs->cx                = gdb_regs[GDB_CX];
180         regs->dx                = gdb_regs[GDB_DX];
181         regs->si                = gdb_regs[GDB_SI];
182         regs->di                = gdb_regs[GDB_DI];
183         regs->bp                = gdb_regs[GDB_BP];
184         regs->ip                = gdb_regs[GDB_PC];
185 #ifdef CONFIG_X86_32
186         regs->flags             = gdb_regs[GDB_PS];
187         regs->ds                = gdb_regs[GDB_DS];
188         regs->es                = gdb_regs[GDB_ES];
189         regs->cs                = gdb_regs[GDB_CS];
190 #else
191         regs->r8                = gdb_regs[GDB_R8];
192         regs->r9                = gdb_regs[GDB_R9];
193         regs->r10               = gdb_regs[GDB_R10];
194         regs->r11               = gdb_regs[GDB_R11];
195         regs->r12               = gdb_regs[GDB_R12];
196         regs->r13               = gdb_regs[GDB_R13];
197         regs->r14               = gdb_regs[GDB_R14];
198         regs->r15               = gdb_regs[GDB_R15];
199         regs->flags             = gdb_regs32[GDB_PS];
200         regs->cs                = gdb_regs32[GDB_CS];
201         regs->ss                = gdb_regs32[GDB_SS];
202 #endif
203 }
204
205 static struct hw_breakpoint {
206         unsigned                enabled;
207         unsigned                type;
208         unsigned                len;
209         unsigned long           addr;
210 } breakinfo[4];
211
212 static void kgdb_correct_hw_break(void)
213 {
214         unsigned long dr7;
215         int correctit = 0;
216         int breakbit;
217         int breakno;
218
219         get_debugreg(dr7, 7);
220         for (breakno = 0; breakno < 4; breakno++) {
221                 breakbit = 2 << (breakno << 1);
222                 if (!(dr7 & breakbit) && breakinfo[breakno].enabled) {
223                         correctit = 1;
224                         dr7 |= breakbit;
225                         dr7 &= ~(0xf0000 << (breakno << 2));
226                         dr7 |= ((breakinfo[breakno].len << 2) |
227                                  breakinfo[breakno].type) <<
228                                ((breakno << 2) + 16);
229                         set_debugreg(breakinfo[breakno].addr, breakno);
230
231                 } else {
232                         if ((dr7 & breakbit) && !breakinfo[breakno].enabled) {
233                                 correctit = 1;
234                                 dr7 &= ~breakbit;
235                                 dr7 &= ~(0xf0000 << (breakno << 2));
236                         }
237                 }
238         }
239         if (correctit)
240                 set_debugreg(dr7, 7);
241 }
242
243 static int
244 kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
245 {
246         int i;
247
248         for (i = 0; i < 4; i++)
249                 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
250                         break;
251         if (i == 4)
252                 return -1;
253
254         breakinfo[i].enabled = 0;
255
256         return 0;
257 }
258
259 static void kgdb_remove_all_hw_break(void)
260 {
261         int i;
262
263         for (i = 0; i < 4; i++)
264                 memset(&breakinfo[i], 0, sizeof(struct hw_breakpoint));
265 }
266
267 static int
268 kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
269 {
270         unsigned type;
271         int i;
272
273         for (i = 0; i < 4; i++)
274                 if (!breakinfo[i].enabled)
275                         break;
276         if (i == 4)
277                 return -1;
278
279         switch (bptype) {
280         case BP_HARDWARE_BREAKPOINT:
281                 type = 0;
282                 len  = 1;
283                 break;
284         case BP_WRITE_WATCHPOINT:
285                 type = 1;
286                 break;
287         case BP_ACCESS_WATCHPOINT:
288                 type = 3;
289                 break;
290         default:
291                 return -1;
292         }
293
294         if (len == 1 || len == 2 || len == 4)
295                 breakinfo[i].len  = len - 1;
296         else
297                 return -1;
298
299         breakinfo[i].enabled = 1;
300         breakinfo[i].addr = addr;
301         breakinfo[i].type = type;
302
303         return 0;
304 }
305
306 /**
307  *      kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
308  *      @regs: Current &struct pt_regs.
309  *
310  *      This function will be called if the particular architecture must
311  *      disable hardware debugging while it is processing gdb packets or
312  *      handling exception.
313  */
314 void kgdb_disable_hw_debug(struct pt_regs *regs)
315 {
316         /* Disable hardware debugging while we are in kgdb: */
317         set_debugreg(0UL, 7);
318 }
319
320 /**
321  *      kgdb_post_primary_code - Save error vector/code numbers.
322  *      @regs: Original pt_regs.
323  *      @e_vector: Original error vector.
324  *      @err_code: Original error code.
325  *
326  *      This is needed on architectures which support SMP and KGDB.
327  *      This function is called after all the slave cpus have been put
328  *      to a know spin state and the primary CPU has control over KGDB.
329  */
330 void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
331 {
332         /* primary processor is completely in the debugger */
333         gdb_x86vector = e_vector;
334         gdb_x86errcode = err_code;
335 }
336
337 #ifdef CONFIG_SMP
338 /**
339  *      kgdb_roundup_cpus - Get other CPUs into a holding pattern
340  *      @flags: Current IRQ state
341  *
342  *      On SMP systems, we need to get the attention of the other CPUs
343  *      and get them be in a known state.  This should do what is needed
344  *      to get the other CPUs to call kgdb_wait(). Note that on some arches,
345  *      the NMI approach is not used for rounding up all the CPUs. For example,
346  *      in case of MIPS, smp_call_function() is used to roundup CPUs. In
347  *      this case, we have to make sure that interrupts are enabled before
348  *      calling smp_call_function(). The argument to this function is
349  *      the flags that will be used when restoring the interrupts. There is
350  *      local_irq_save() call before kgdb_roundup_cpus().
351  *
352  *      On non-SMP systems, this is not called.
353  */
354 void kgdb_roundup_cpus(unsigned long flags)
355 {
356         apic->send_IPI_allbutself(APIC_DM_NMI);
357 }
358 #endif
359
360 /**
361  *      kgdb_arch_handle_exception - Handle architecture specific GDB packets.
362  *      @vector: The error vector of the exception that happened.
363  *      @signo: The signal number of the exception that happened.
364  *      @err_code: The error code of the exception that happened.
365  *      @remcom_in_buffer: The buffer of the packet we have read.
366  *      @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
367  *      @regs: The &struct pt_regs of the current process.
368  *
369  *      This function MUST handle the 'c' and 's' command packets,
370  *      as well packets to set / remove a hardware breakpoint, if used.
371  *      If there are additional packets which the hardware needs to handle,
372  *      they are handled here.  The code should return -1 if it wants to
373  *      process more packets, and a %0 or %1 if it wants to exit from the
374  *      kgdb callback.
375  */
376 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
377                                char *remcomInBuffer, char *remcomOutBuffer,
378                                struct pt_regs *linux_regs)
379 {
380         unsigned long addr;
381         unsigned long dr6;
382         char *ptr;
383         int newPC;
384
385         switch (remcomInBuffer[0]) {
386         case 'c':
387         case 's':
388                 /* try to read optional parameter, pc unchanged if no parm */
389                 ptr = &remcomInBuffer[1];
390                 if (kgdb_hex2long(&ptr, &addr))
391                         linux_regs->ip = addr;
392         case 'D':
393         case 'k':
394                 newPC = linux_regs->ip;
395
396                 /* clear the trace bit */
397                 linux_regs->flags &= ~X86_EFLAGS_TF;
398                 atomic_set(&kgdb_cpu_doing_single_step, -1);
399
400                 /* set the trace bit if we're stepping */
401                 if (remcomInBuffer[0] == 's') {
402                         linux_regs->flags |= X86_EFLAGS_TF;
403                         kgdb_single_step = 1;
404                         atomic_set(&kgdb_cpu_doing_single_step,
405                                    raw_smp_processor_id());
406                 }
407
408                 get_debugreg(dr6, 6);
409                 if (!(dr6 & 0x4000)) {
410                         int breakno;
411
412                         for (breakno = 0; breakno < 4; breakno++) {
413                                 if (dr6 & (1 << breakno) &&
414                                     breakinfo[breakno].type == 0) {
415                                         /* Set restore flag: */
416                                         linux_regs->flags |= X86_EFLAGS_RF;
417                                         break;
418                                 }
419                         }
420                 }
421                 set_debugreg(0UL, 6);
422                 kgdb_correct_hw_break();
423
424                 return 0;
425         }
426
427         /* this means that we do not want to exit from the handler: */
428         return -1;
429 }
430
431 static inline int
432 single_step_cont(struct pt_regs *regs, struct die_args *args)
433 {
434         /*
435          * Single step exception from kernel space to user space so
436          * eat the exception and continue the process:
437          */
438         printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
439                         "resuming...\n");
440         kgdb_arch_handle_exception(args->trapnr, args->signr,
441                                    args->err, "c", "", regs);
442         /*
443          * Reset the BS bit in dr6 (pointed by args->err) to
444          * denote completion of processing
445          */
446         (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
447
448         return NOTIFY_STOP;
449 }
450
451 static int was_in_debug_nmi[NR_CPUS];
452
453 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
454 {
455         struct pt_regs *regs = args->regs;
456
457         switch (cmd) {
458         case DIE_NMI:
459                 if (atomic_read(&kgdb_active) != -1) {
460                         /* KGDB CPU roundup */
461                         kgdb_nmicallback(raw_smp_processor_id(), regs);
462                         was_in_debug_nmi[raw_smp_processor_id()] = 1;
463                         touch_nmi_watchdog();
464                         return NOTIFY_STOP;
465                 }
466                 return NOTIFY_DONE;
467
468         case DIE_NMI_IPI:
469                 /* Just ignore, we will handle the roundup on DIE_NMI. */
470                 return NOTIFY_DONE;
471
472         case DIE_NMIUNKNOWN:
473                 if (was_in_debug_nmi[raw_smp_processor_id()]) {
474                         was_in_debug_nmi[raw_smp_processor_id()] = 0;
475                         return NOTIFY_STOP;
476                 }
477                 return NOTIFY_DONE;
478
479         case DIE_NMIWATCHDOG:
480                 if (atomic_read(&kgdb_active) != -1) {
481                         /* KGDB CPU roundup: */
482                         kgdb_nmicallback(raw_smp_processor_id(), regs);
483                         return NOTIFY_STOP;
484                 }
485                 /* Enter debugger: */
486                 break;
487
488         case DIE_DEBUG:
489                 if (atomic_read(&kgdb_cpu_doing_single_step) ==
490                     raw_smp_processor_id()) {
491                         if (user_mode(regs))
492                                 return single_step_cont(regs, args);
493                         break;
494                 } else if (test_thread_flag(TIF_SINGLESTEP))
495                         /* This means a user thread is single stepping
496                          * a system call which should be ignored
497                          */
498                         return NOTIFY_DONE;
499                 /* fall through */
500         default:
501                 if (user_mode(regs))
502                         return NOTIFY_DONE;
503         }
504
505         if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
506                 return NOTIFY_DONE;
507
508         /* Must touch watchdog before return to normal operation */
509         touch_nmi_watchdog();
510         return NOTIFY_STOP;
511 }
512
513 static int
514 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
515 {
516         unsigned long flags;
517         int ret;
518
519         local_irq_save(flags);
520         ret = __kgdb_notify(ptr, cmd);
521         local_irq_restore(flags);
522
523         return ret;
524 }
525
526 static struct notifier_block kgdb_notifier = {
527         .notifier_call  = kgdb_notify,
528
529         /*
530          * Lowest-prio notifier priority, we want to be notified last:
531          */
532         .priority       = -INT_MAX,
533 };
534
535 /**
536  *      kgdb_arch_init - Perform any architecture specific initalization.
537  *
538  *      This function will handle the initalization of any architecture
539  *      specific callbacks.
540  */
541 int kgdb_arch_init(void)
542 {
543         return register_die_notifier(&kgdb_notifier);
544 }
545
546 /**
547  *      kgdb_arch_exit - Perform any architecture specific uninitalization.
548  *
549  *      This function will handle the uninitalization of any architecture
550  *      specific callbacks, for dynamic registration and unregistration.
551  */
552 void kgdb_arch_exit(void)
553 {
554         unregister_die_notifier(&kgdb_notifier);
555 }
556
557 /**
558  *
559  *      kgdb_skipexception - Bail out of KGDB when we've been triggered.
560  *      @exception: Exception vector number
561  *      @regs: Current &struct pt_regs.
562  *
563  *      On some architectures we need to skip a breakpoint exception when
564  *      it occurs after a breakpoint has been removed.
565  *
566  * Skip an int3 exception when it occurs after a breakpoint has been
567  * removed. Backtrack eip by 1 since the int3 would have caused it to
568  * increment by 1.
569  */
570 int kgdb_skipexception(int exception, struct pt_regs *regs)
571 {
572         if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
573                 regs->ip -= 1;
574                 return 1;
575         }
576         return 0;
577 }
578
579 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
580 {
581         if (exception == 3)
582                 return instruction_pointer(regs) - 1;
583         return instruction_pointer(regs);
584 }
585
586 struct kgdb_arch arch_kgdb_ops = {
587         /* Breakpoint instruction: */
588         .gdb_bpt_instr          = { 0xcc },
589         .flags                  = KGDB_HW_BREAKPOINT,
590         .set_hw_breakpoint      = kgdb_set_hw_break,
591         .remove_hw_breakpoint   = kgdb_remove_hw_break,
592         .remove_all_hw_break    = kgdb_remove_all_hw_break,
593         .correct_hw_break       = kgdb_correct_hw_break,
594 };