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
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.
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.
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
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 #include <linux/hw_breakpoint.h>
47 #include <asm/debugreg.h>
48 #include <asm/apicdef.h>
52 struct dbg_reg_def_t dbg_reg_def
[DBG_MAX_REG_NUM
] =
55 { "ax", 4, offsetof(struct pt_regs
, ax
) },
56 { "cx", 4, offsetof(struct pt_regs
, cx
) },
57 { "dx", 4, offsetof(struct pt_regs
, dx
) },
58 { "bx", 4, offsetof(struct pt_regs
, bx
) },
59 { "sp", 4, offsetof(struct pt_regs
, sp
) },
60 { "bp", 4, offsetof(struct pt_regs
, bp
) },
61 { "si", 4, offsetof(struct pt_regs
, si
) },
62 { "di", 4, offsetof(struct pt_regs
, di
) },
63 { "ip", 4, offsetof(struct pt_regs
, ip
) },
64 { "flags", 4, offsetof(struct pt_regs
, flags
) },
65 { "cs", 4, offsetof(struct pt_regs
, cs
) },
66 { "ss", 4, offsetof(struct pt_regs
, ss
) },
67 { "ds", 4, offsetof(struct pt_regs
, ds
) },
68 { "es", 4, offsetof(struct pt_regs
, es
) },
72 { "ax", 8, offsetof(struct pt_regs
, ax
) },
73 { "bx", 8, offsetof(struct pt_regs
, bx
) },
74 { "cx", 8, offsetof(struct pt_regs
, cx
) },
75 { "dx", 8, offsetof(struct pt_regs
, dx
) },
76 { "si", 8, offsetof(struct pt_regs
, dx
) },
77 { "di", 8, offsetof(struct pt_regs
, di
) },
78 { "bp", 8, offsetof(struct pt_regs
, bp
) },
79 { "sp", 8, offsetof(struct pt_regs
, sp
) },
80 { "r8", 8, offsetof(struct pt_regs
, r8
) },
81 { "r9", 8, offsetof(struct pt_regs
, r9
) },
82 { "r10", 8, offsetof(struct pt_regs
, r10
) },
83 { "r11", 8, offsetof(struct pt_regs
, r11
) },
84 { "r12", 8, offsetof(struct pt_regs
, r12
) },
85 { "r13", 8, offsetof(struct pt_regs
, r13
) },
86 { "r14", 8, offsetof(struct pt_regs
, r14
) },
87 { "r15", 8, offsetof(struct pt_regs
, r15
) },
88 { "ip", 8, offsetof(struct pt_regs
, ip
) },
89 { "flags", 4, offsetof(struct pt_regs
, flags
) },
90 { "cs", 4, offsetof(struct pt_regs
, cs
) },
91 { "ss", 4, offsetof(struct pt_regs
, ss
) },
95 int dbg_set_reg(int regno
, void *mem
, struct pt_regs
*regs
)
99 regno
== GDB_SS
|| regno
== GDB_FS
|| regno
== GDB_GS
||
101 regno
== GDB_SP
|| regno
== GDB_ORIG_AX
)
104 if (dbg_reg_def
[regno
].offset
!= -1)
105 memcpy((void *)regs
+ dbg_reg_def
[regno
].offset
, mem
,
106 dbg_reg_def
[regno
].size
);
110 char *dbg_get_reg(int regno
, void *mem
, struct pt_regs
*regs
)
112 if (regno
== GDB_ORIG_AX
) {
113 memcpy(mem
, ®s
->orig_ax
, sizeof(regs
->orig_ax
));
116 if (regno
>= DBG_MAX_REG_NUM
|| regno
< 0)
119 if (dbg_reg_def
[regno
].offset
!= -1)
120 memcpy(mem
, (void *)regs
+ dbg_reg_def
[regno
].offset
,
121 dbg_reg_def
[regno
].size
);
126 if (!user_mode_vm(regs
))
127 *(unsigned long *)mem
= __KERNEL_DS
;
130 if (!user_mode_vm(regs
))
131 *(unsigned long *)mem
= kernel_stack_pointer(regs
);
135 *(unsigned long *)mem
= 0xFFFF;
139 return dbg_reg_def
[regno
].name
;
143 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
144 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
145 * @p: The &struct task_struct of the desired process.
147 * Convert the register values of the sleeping process in @p to
148 * the format that GDB expects.
149 * This function is called when kgdb does not have access to the
150 * &struct pt_regs and therefore it should fill the gdb registers
151 * @gdb_regs with what has been saved in &struct thread_struct
152 * thread field during switch_to.
154 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs
, struct task_struct
*p
)
156 #ifndef CONFIG_X86_32
157 u32
*gdb_regs32
= (u32
*)gdb_regs
;
159 gdb_regs
[GDB_AX
] = 0;
160 gdb_regs
[GDB_BX
] = 0;
161 gdb_regs
[GDB_CX
] = 0;
162 gdb_regs
[GDB_DX
] = 0;
163 gdb_regs
[GDB_SI
] = 0;
164 gdb_regs
[GDB_DI
] = 0;
165 gdb_regs
[GDB_BP
] = *(unsigned long *)p
->thread
.sp
;
167 gdb_regs
[GDB_DS
] = __KERNEL_DS
;
168 gdb_regs
[GDB_ES
] = __KERNEL_DS
;
169 gdb_regs
[GDB_PS
] = 0;
170 gdb_regs
[GDB_CS
] = __KERNEL_CS
;
171 gdb_regs
[GDB_PC
] = p
->thread
.ip
;
172 gdb_regs
[GDB_SS
] = __KERNEL_DS
;
173 gdb_regs
[GDB_FS
] = 0xFFFF;
174 gdb_regs
[GDB_GS
] = 0xFFFF;
176 gdb_regs32
[GDB_PS
] = *(unsigned long *)(p
->thread
.sp
+ 8);
177 gdb_regs32
[GDB_CS
] = __KERNEL_CS
;
178 gdb_regs32
[GDB_SS
] = __KERNEL_DS
;
179 gdb_regs
[GDB_PC
] = 0;
180 gdb_regs
[GDB_R8
] = 0;
181 gdb_regs
[GDB_R9
] = 0;
182 gdb_regs
[GDB_R10
] = 0;
183 gdb_regs
[GDB_R11
] = 0;
184 gdb_regs
[GDB_R12
] = 0;
185 gdb_regs
[GDB_R13
] = 0;
186 gdb_regs
[GDB_R14
] = 0;
187 gdb_regs
[GDB_R15
] = 0;
189 gdb_regs
[GDB_SP
] = p
->thread
.sp
;
192 static struct hw_breakpoint
{
197 struct perf_event
* __percpu
*pev
;
198 } breakinfo
[HBP_NUM
];
200 static unsigned long early_dr7
;
202 static void kgdb_correct_hw_break(void)
206 for (breakno
= 0; breakno
< HBP_NUM
; breakno
++) {
207 struct perf_event
*bp
;
208 struct arch_hw_breakpoint
*info
;
210 int cpu
= raw_smp_processor_id();
211 if (!breakinfo
[breakno
].enabled
)
214 set_debugreg(breakinfo
[breakno
].addr
, breakno
);
215 early_dr7
|= encode_dr7(breakno
,
216 breakinfo
[breakno
].len
,
217 breakinfo
[breakno
].type
);
218 set_debugreg(early_dr7
, 7);
221 bp
= *per_cpu_ptr(breakinfo
[breakno
].pev
, cpu
);
222 info
= counter_arch_bp(bp
);
223 if (bp
->attr
.disabled
!= 1)
225 bp
->attr
.bp_addr
= breakinfo
[breakno
].addr
;
226 bp
->attr
.bp_len
= breakinfo
[breakno
].len
;
227 bp
->attr
.bp_type
= breakinfo
[breakno
].type
;
228 info
->address
= breakinfo
[breakno
].addr
;
229 info
->len
= breakinfo
[breakno
].len
;
230 info
->type
= breakinfo
[breakno
].type
;
231 val
= arch_install_hw_breakpoint(bp
);
233 bp
->attr
.disabled
= 0;
236 hw_breakpoint_restore();
239 static int hw_break_reserve_slot(int breakno
)
243 struct perf_event
**pevent
;
248 for_each_online_cpu(cpu
) {
250 pevent
= per_cpu_ptr(breakinfo
[breakno
].pev
, cpu
);
251 if (dbg_reserve_bp_slot(*pevent
))
258 for_each_online_cpu(cpu
) {
262 pevent
= per_cpu_ptr(breakinfo
[breakno
].pev
, cpu
);
263 dbg_release_bp_slot(*pevent
);
268 static int hw_break_release_slot(int breakno
)
270 struct perf_event
**pevent
;
276 for_each_online_cpu(cpu
) {
277 pevent
= per_cpu_ptr(breakinfo
[breakno
].pev
, cpu
);
278 if (dbg_release_bp_slot(*pevent
))
280 * The debugger is responsible for handing the retry on
289 kgdb_remove_hw_break(unsigned long addr
, int len
, enum kgdb_bptype bptype
)
293 for (i
= 0; i
< HBP_NUM
; i
++)
294 if (breakinfo
[i
].addr
== addr
&& breakinfo
[i
].enabled
)
299 if (hw_break_release_slot(i
)) {
300 printk(KERN_ERR
"Cannot remove hw breakpoint at %lx\n", addr
);
303 breakinfo
[i
].enabled
= 0;
308 static void kgdb_remove_all_hw_break(void)
311 int cpu
= raw_smp_processor_id();
312 struct perf_event
*bp
;
314 for (i
= 0; i
< HBP_NUM
; i
++) {
315 if (!breakinfo
[i
].enabled
)
317 bp
= *per_cpu_ptr(breakinfo
[i
].pev
, cpu
);
318 if (!bp
->attr
.disabled
) {
319 arch_uninstall_hw_breakpoint(bp
);
320 bp
->attr
.disabled
= 1;
324 early_dr7
&= ~encode_dr7(i
, breakinfo
[i
].len
,
326 else if (hw_break_release_slot(i
))
327 printk(KERN_ERR
"KGDB: hw bpt remove failed %lx\n",
329 breakinfo
[i
].enabled
= 0;
334 kgdb_set_hw_break(unsigned long addr
, int len
, enum kgdb_bptype bptype
)
338 for (i
= 0; i
< HBP_NUM
; i
++)
339 if (!breakinfo
[i
].enabled
)
345 case BP_HARDWARE_BREAKPOINT
:
347 breakinfo
[i
].type
= X86_BREAKPOINT_EXECUTE
;
349 case BP_WRITE_WATCHPOINT
:
350 breakinfo
[i
].type
= X86_BREAKPOINT_WRITE
;
352 case BP_ACCESS_WATCHPOINT
:
353 breakinfo
[i
].type
= X86_BREAKPOINT_RW
;
360 breakinfo
[i
].len
= X86_BREAKPOINT_LEN_1
;
363 breakinfo
[i
].len
= X86_BREAKPOINT_LEN_2
;
366 breakinfo
[i
].len
= X86_BREAKPOINT_LEN_4
;
370 breakinfo
[i
].len
= X86_BREAKPOINT_LEN_8
;
376 breakinfo
[i
].addr
= addr
;
377 if (hw_break_reserve_slot(i
)) {
378 breakinfo
[i
].addr
= 0;
381 breakinfo
[i
].enabled
= 1;
387 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
388 * @regs: Current &struct pt_regs.
390 * This function will be called if the particular architecture must
391 * disable hardware debugging while it is processing gdb packets or
392 * handling exception.
394 static void kgdb_disable_hw_debug(struct pt_regs
*regs
)
397 int cpu
= raw_smp_processor_id();
398 struct perf_event
*bp
;
400 /* Disable hardware debugging while we are in kgdb: */
401 set_debugreg(0UL, 7);
402 for (i
= 0; i
< HBP_NUM
; i
++) {
403 if (!breakinfo
[i
].enabled
)
406 early_dr7
&= ~encode_dr7(i
, breakinfo
[i
].len
,
410 bp
= *per_cpu_ptr(breakinfo
[i
].pev
, cpu
);
411 if (bp
->attr
.disabled
== 1)
413 arch_uninstall_hw_breakpoint(bp
);
414 bp
->attr
.disabled
= 1;
420 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
421 * @flags: Current IRQ state
423 * On SMP systems, we need to get the attention of the other CPUs
424 * and get them be in a known state. This should do what is needed
425 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
426 * the NMI approach is not used for rounding up all the CPUs. For example,
427 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
428 * this case, we have to make sure that interrupts are enabled before
429 * calling smp_call_function(). The argument to this function is
430 * the flags that will be used when restoring the interrupts. There is
431 * local_irq_save() call before kgdb_roundup_cpus().
433 * On non-SMP systems, this is not called.
435 void kgdb_roundup_cpus(unsigned long flags
)
437 apic
->send_IPI_allbutself(APIC_DM_NMI
);
442 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
443 * @vector: The error vector of the exception that happened.
444 * @signo: The signal number of the exception that happened.
445 * @err_code: The error code of the exception that happened.
446 * @remcom_in_buffer: The buffer of the packet we have read.
447 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
448 * @regs: The &struct pt_regs of the current process.
450 * This function MUST handle the 'c' and 's' command packets,
451 * as well packets to set / remove a hardware breakpoint, if used.
452 * If there are additional packets which the hardware needs to handle,
453 * they are handled here. The code should return -1 if it wants to
454 * process more packets, and a %0 or %1 if it wants to exit from the
457 int kgdb_arch_handle_exception(int e_vector
, int signo
, int err_code
,
458 char *remcomInBuffer
, char *remcomOutBuffer
,
459 struct pt_regs
*linux_regs
)
464 switch (remcomInBuffer
[0]) {
467 /* try to read optional parameter, pc unchanged if no parm */
468 ptr
= &remcomInBuffer
[1];
469 if (kgdb_hex2long(&ptr
, &addr
))
470 linux_regs
->ip
= addr
;
473 /* clear the trace bit */
474 linux_regs
->flags
&= ~X86_EFLAGS_TF
;
475 atomic_set(&kgdb_cpu_doing_single_step
, -1);
477 /* set the trace bit if we're stepping */
478 if (remcomInBuffer
[0] == 's') {
479 linux_regs
->flags
|= X86_EFLAGS_TF
;
480 atomic_set(&kgdb_cpu_doing_single_step
,
481 raw_smp_processor_id());
487 /* this means that we do not want to exit from the handler: */
492 single_step_cont(struct pt_regs
*regs
, struct die_args
*args
)
495 * Single step exception from kernel space to user space so
496 * eat the exception and continue the process:
498 printk(KERN_ERR
"KGDB: trap/step from kernel to user space, "
500 kgdb_arch_handle_exception(args
->trapnr
, args
->signr
,
501 args
->err
, "c", "", regs
);
503 * Reset the BS bit in dr6 (pointed by args->err) to
504 * denote completion of processing
506 (*(unsigned long *)ERR_PTR(args
->err
)) &= ~DR_STEP
;
511 static int was_in_debug_nmi
[NR_CPUS
];
513 static int kgdb_nmi_handler(unsigned int cmd
, struct pt_regs
*regs
)
517 if (atomic_read(&kgdb_active
) != -1) {
518 /* KGDB CPU roundup */
519 kgdb_nmicallback(raw_smp_processor_id(), regs
);
520 was_in_debug_nmi
[raw_smp_processor_id()] = 1;
521 touch_nmi_watchdog();
527 if (was_in_debug_nmi
[raw_smp_processor_id()]) {
528 was_in_debug_nmi
[raw_smp_processor_id()] = 0;
539 static int __kgdb_notify(struct die_args
*args
, unsigned long cmd
)
541 struct pt_regs
*regs
= args
->regs
;
545 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1) {
547 return single_step_cont(regs
, args
);
549 } else if (test_thread_flag(TIF_SINGLESTEP
))
550 /* This means a user thread is single stepping
551 * a system call which should be ignored
560 if (kgdb_handle_exception(args
->trapnr
, args
->signr
, cmd
, regs
))
563 /* Must touch watchdog before return to normal operation */
564 touch_nmi_watchdog();
568 int kgdb_ll_trap(int cmd
, const char *str
,
569 struct pt_regs
*regs
, long err
, int trap
, int sig
)
571 struct die_args args
= {
580 if (!kgdb_io_module_registered
)
583 return __kgdb_notify(&args
, cmd
);
587 kgdb_notify(struct notifier_block
*self
, unsigned long cmd
, void *ptr
)
592 local_irq_save(flags
);
593 ret
= __kgdb_notify(ptr
, cmd
);
594 local_irq_restore(flags
);
599 static struct notifier_block kgdb_notifier
= {
600 .notifier_call
= kgdb_notify
,
604 * kgdb_arch_init - Perform any architecture specific initalization.
606 * This function will handle the initalization of any architecture
607 * specific callbacks.
609 int kgdb_arch_init(void)
613 retval
= register_die_notifier(&kgdb_notifier
);
617 retval
= register_nmi_handler(NMI_LOCAL
, kgdb_nmi_handler
,
622 retval
= register_nmi_handler(NMI_UNKNOWN
, kgdb_nmi_handler
,
631 unregister_nmi_handler(NMI_LOCAL
, "kgdb");
633 unregister_die_notifier(&kgdb_notifier
);
638 static void kgdb_hw_overflow_handler(struct perf_event
*event
,
639 struct perf_sample_data
*data
, struct pt_regs
*regs
)
641 struct task_struct
*tsk
= current
;
644 for (i
= 0; i
< 4; i
++)
645 if (breakinfo
[i
].enabled
)
646 tsk
->thread
.debugreg6
|= (DR_TRAP0
<< i
);
649 void kgdb_arch_late(void)
652 struct perf_event_attr attr
;
653 struct perf_event
**pevent
;
656 * Pre-allocate the hw breakpoint structions in the non-atomic
657 * portion of kgdb because this operation requires mutexs to
660 hw_breakpoint_init(&attr
);
661 attr
.bp_addr
= (unsigned long)kgdb_arch_init
;
662 attr
.bp_len
= HW_BREAKPOINT_LEN_1
;
663 attr
.bp_type
= HW_BREAKPOINT_W
;
665 for (i
= 0; i
< HBP_NUM
; i
++) {
666 if (breakinfo
[i
].pev
)
668 breakinfo
[i
].pev
= register_wide_hw_breakpoint(&attr
, NULL
, NULL
);
669 if (IS_ERR((void * __force
)breakinfo
[i
].pev
)) {
670 printk(KERN_ERR
"kgdb: Could not allocate hw"
671 "breakpoints\nDisabling the kernel debugger\n");
672 breakinfo
[i
].pev
= NULL
;
676 for_each_online_cpu(cpu
) {
677 pevent
= per_cpu_ptr(breakinfo
[i
].pev
, cpu
);
678 pevent
[0]->hw
.sample_period
= 1;
679 pevent
[0]->overflow_handler
= kgdb_hw_overflow_handler
;
680 if (pevent
[0]->destroy
!= NULL
) {
681 pevent
[0]->destroy
= NULL
;
682 release_bp_slot(*pevent
);
689 * kgdb_arch_exit - Perform any architecture specific uninitalization.
691 * This function will handle the uninitalization of any architecture
692 * specific callbacks, for dynamic registration and unregistration.
694 void kgdb_arch_exit(void)
697 for (i
= 0; i
< 4; i
++) {
698 if (breakinfo
[i
].pev
) {
699 unregister_wide_hw_breakpoint(breakinfo
[i
].pev
);
700 breakinfo
[i
].pev
= NULL
;
703 unregister_nmi_handler(NMI_UNKNOWN
, "kgdb");
704 unregister_nmi_handler(NMI_LOCAL
, "kgdb");
705 unregister_die_notifier(&kgdb_notifier
);
710 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
711 * @exception: Exception vector number
712 * @regs: Current &struct pt_regs.
714 * On some architectures we need to skip a breakpoint exception when
715 * it occurs after a breakpoint has been removed.
717 * Skip an int3 exception when it occurs after a breakpoint has been
718 * removed. Backtrack eip by 1 since the int3 would have caused it to
721 int kgdb_skipexception(int exception
, struct pt_regs
*regs
)
723 if (exception
== 3 && kgdb_isremovedbreak(regs
->ip
- 1)) {
730 unsigned long kgdb_arch_pc(int exception
, struct pt_regs
*regs
)
733 return instruction_pointer(regs
) - 1;
734 return instruction_pointer(regs
);
737 void kgdb_arch_set_pc(struct pt_regs
*regs
, unsigned long ip
)
742 struct kgdb_arch arch_kgdb_ops
= {
743 /* Breakpoint instruction: */
744 .gdb_bpt_instr
= { 0xcc },
745 .flags
= KGDB_HW_BREAKPOINT
,
746 .set_hw_breakpoint
= kgdb_set_hw_break
,
747 .remove_hw_breakpoint
= kgdb_remove_hw_break
,
748 .disable_hw_break
= kgdb_disable_hw_debug
,
749 .remove_all_hw_break
= kgdb_remove_all_hw_break
,
750 .correct_hw_break
= kgdb_correct_hw_break
,