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