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