Merge branch 'next-spi' of git://git.secretlab.ca/git/linux-2.6
[linux-2.6-block.git] / arch / x86 / kernel / kgdb.c
CommitLineData
82da3ff8
IM
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>
d3597524 44#include <linux/nmi.h>
82da3ff8 45
62edab90 46#include <asm/debugreg.h>
82da3ff8
IM
47#include <asm/apicdef.h>
48#include <asm/system.h>
49
7b6aa335 50#include <asm/apic.h>
82da3ff8
IM
51
52/*
53 * Put the error code here just in case the user cares:
54 */
55static 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 */
61static 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 */
71void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
72{
703a1edc
JW
73#ifndef CONFIG_X86_32
74 u32 *gdb_regs32 = (u32 *)gdb_regs;
75#endif
82da3ff8
IM
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;
82da3ff8
IM
83 gdb_regs[GDB_PC] = regs->ip;
84#ifdef CONFIG_X86_32
703a1edc 85 gdb_regs[GDB_PS] = regs->flags;
82da3ff8
IM
86 gdb_regs[GDB_DS] = regs->ds;
87 gdb_regs[GDB_ES] = regs->es;
88 gdb_regs[GDB_CS] = regs->cs;
82da3ff8
IM
89 gdb_regs[GDB_FS] = 0xFFFF;
90 gdb_regs[GDB_GS] = 0xFFFF;
cf6f196d
JW
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 }
82da3ff8
IM
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;
703a1edc
JW
107 gdb_regs32[GDB_PS] = regs->flags;
108 gdb_regs32[GDB_CS] = regs->cs;
109 gdb_regs32[GDB_SS] = regs->ss;
5ca6c0ca 110 gdb_regs[GDB_SP] = kernel_stack_pointer(regs);
cf6f196d 111#endif
82da3ff8
IM
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 */
126void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
127{
703a1edc
JW
128#ifndef CONFIG_X86_32
129 u32 *gdb_regs32 = (u32 *)gdb_regs;
130#endif
82da3ff8
IM
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
703a1edc
JW
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;
0c23590f 151 gdb_regs[GDB_PC] = 0;
82da3ff8
IM
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 */
172void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
173{
703a1edc
JW
174#ifndef CONFIG_X86_32
175 u32 *gdb_regs32 = (u32 *)gdb_regs;
176#endif
82da3ff8
IM
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];
82da3ff8
IM
184 regs->ip = gdb_regs[GDB_PC];
185#ifdef CONFIG_X86_32
703a1edc 186 regs->flags = gdb_regs[GDB_PS];
82da3ff8
IM
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];
703a1edc
JW
199 regs->flags = gdb_regs32[GDB_PS];
200 regs->cs = gdb_regs32[GDB_CS];
201 regs->ss = gdb_regs32[GDB_SS];
82da3ff8
IM
202#endif
203}
204
64e9ee30
JW
205static struct hw_breakpoint {
206 unsigned enabled;
207 unsigned type;
208 unsigned len;
209 unsigned long addr;
210} breakinfo[4];
211
212static 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);
a5d09d68 229 set_debugreg(breakinfo[breakno].addr, breakno);
64e9ee30
JW
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
243static int
244kgdb_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
259static 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
267static int
268kgdb_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 */
314void 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
82da3ff8
IM
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 */
330void 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 */
354void kgdb_roundup_cpus(unsigned long flags)
355{
dac5f412 356 apic->send_IPI_allbutself(APIC_DM_NMI);
82da3ff8
IM
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 */
376int 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;
64e9ee30 381 unsigned long dr6;
82da3ff8
IM
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;
737a460f
JW
392 case 'D':
393 case 'k':
82da3ff8
IM
394 newPC = linux_regs->ip;
395
396 /* clear the trace bit */
fda31d7d 397 linux_regs->flags &= ~X86_EFLAGS_TF;
82da3ff8
IM
398 atomic_set(&kgdb_cpu_doing_single_step, -1);
399
400 /* set the trace bit if we're stepping */
401 if (remcomInBuffer[0] == 's') {
fda31d7d 402 linux_regs->flags |= X86_EFLAGS_TF;
d7161a65
JW
403 atomic_set(&kgdb_cpu_doing_single_step,
404 raw_smp_processor_id());
82da3ff8
IM
405 }
406
64e9ee30
JW
407 get_debugreg(dr6, 6);
408 if (!(dr6 & 0x4000)) {
409 int breakno;
410
411 for (breakno = 0; breakno < 4; breakno++) {
412 if (dr6 & (1 << breakno) &&
413 breakinfo[breakno].type == 0) {
414 /* Set restore flag: */
415 linux_regs->flags |= X86_EFLAGS_RF;
416 break;
417 }
418 }
419 }
420 set_debugreg(0UL, 6);
421 kgdb_correct_hw_break();
422
82da3ff8
IM
423 return 0;
424 }
425
426 /* this means that we do not want to exit from the handler: */
427 return -1;
428}
429
430static inline int
431single_step_cont(struct pt_regs *regs, struct die_args *args)
432{
433 /*
434 * Single step exception from kernel space to user space so
435 * eat the exception and continue the process:
436 */
437 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
438 "resuming...\n");
439 kgdb_arch_handle_exception(args->trapnr, args->signr,
440 args->err, "c", "", regs);
62edab90
P
441 /*
442 * Reset the BS bit in dr6 (pointed by args->err) to
443 * denote completion of processing
444 */
445 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
82da3ff8
IM
446
447 return NOTIFY_STOP;
448}
449
d3597524
JW
450static int was_in_debug_nmi[NR_CPUS];
451
82da3ff8
IM
452static int __kgdb_notify(struct die_args *args, unsigned long cmd)
453{
454 struct pt_regs *regs = args->regs;
455
456 switch (cmd) {
457 case DIE_NMI:
458 if (atomic_read(&kgdb_active) != -1) {
459 /* KGDB CPU roundup */
460 kgdb_nmicallback(raw_smp_processor_id(), regs);
d3597524
JW
461 was_in_debug_nmi[raw_smp_processor_id()] = 1;
462 touch_nmi_watchdog();
82da3ff8
IM
463 return NOTIFY_STOP;
464 }
465 return NOTIFY_DONE;
466
467 case DIE_NMI_IPI:
e85ceae9 468 /* Just ignore, we will handle the roundup on DIE_NMI. */
d3597524
JW
469 return NOTIFY_DONE;
470
471 case DIE_NMIUNKNOWN:
472 if (was_in_debug_nmi[raw_smp_processor_id()]) {
473 was_in_debug_nmi[raw_smp_processor_id()] = 0;
82da3ff8
IM
474 return NOTIFY_STOP;
475 }
476 return NOTIFY_DONE;
477
478 case DIE_NMIWATCHDOG:
479 if (atomic_read(&kgdb_active) != -1) {
480 /* KGDB CPU roundup: */
481 kgdb_nmicallback(raw_smp_processor_id(), regs);
482 return NOTIFY_STOP;
483 }
484 /* Enter debugger: */
485 break;
486
487 case DIE_DEBUG:
488 if (atomic_read(&kgdb_cpu_doing_single_step) ==
d7161a65
JW
489 raw_smp_processor_id()) {
490 if (user_mode(regs))
491 return single_step_cont(regs, args);
492 break;
493 } else if (test_thread_flag(TIF_SINGLESTEP))
494 /* This means a user thread is single stepping
495 * a system call which should be ignored
496 */
497 return NOTIFY_DONE;
82da3ff8
IM
498 /* fall through */
499 default:
500 if (user_mode(regs))
501 return NOTIFY_DONE;
502 }
503
504 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
505 return NOTIFY_DONE;
506
737a460f
JW
507 /* Must touch watchdog before return to normal operation */
508 touch_nmi_watchdog();
82da3ff8
IM
509 return NOTIFY_STOP;
510}
511
512static int
513kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
514{
515 unsigned long flags;
516 int ret;
517
518 local_irq_save(flags);
519 ret = __kgdb_notify(ptr, cmd);
520 local_irq_restore(flags);
521
522 return ret;
523}
524
525static struct notifier_block kgdb_notifier = {
526 .notifier_call = kgdb_notify,
527
528 /*
529 * Lowest-prio notifier priority, we want to be notified last:
530 */
531 .priority = -INT_MAX,
532};
533
534/**
535 * kgdb_arch_init - Perform any architecture specific initalization.
536 *
537 * This function will handle the initalization of any architecture
538 * specific callbacks.
539 */
540int kgdb_arch_init(void)
541{
542 return register_die_notifier(&kgdb_notifier);
543}
544
545/**
546 * kgdb_arch_exit - Perform any architecture specific uninitalization.
547 *
548 * This function will handle the uninitalization of any architecture
549 * specific callbacks, for dynamic registration and unregistration.
550 */
551void kgdb_arch_exit(void)
552{
553 unregister_die_notifier(&kgdb_notifier);
554}
555
556/**
557 *
558 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
559 * @exception: Exception vector number
560 * @regs: Current &struct pt_regs.
561 *
562 * On some architectures we need to skip a breakpoint exception when
563 * it occurs after a breakpoint has been removed.
564 *
565 * Skip an int3 exception when it occurs after a breakpoint has been
566 * removed. Backtrack eip by 1 since the int3 would have caused it to
567 * increment by 1.
568 */
569int kgdb_skipexception(int exception, struct pt_regs *regs)
570{
571 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
572 regs->ip -= 1;
573 return 1;
574 }
575 return 0;
576}
577
578unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
579{
580 if (exception == 3)
581 return instruction_pointer(regs) - 1;
582 return instruction_pointer(regs);
583}
584
585struct kgdb_arch arch_kgdb_ops = {
586 /* Breakpoint instruction: */
587 .gdb_bpt_instr = { 0xcc },
64e9ee30
JW
588 .flags = KGDB_HW_BREAKPOINT,
589 .set_hw_breakpoint = kgdb_set_hw_break,
590 .remove_hw_breakpoint = kgdb_remove_hw_break,
591 .remove_all_hw_break = kgdb_remove_all_hw_break,
592 .correct_hw_break = kgdb_correct_hw_break,
82da3ff8 593};