4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
6 * Copyright (C) 2000-2001 VERITAS Software Corporation.
7 * Copyright (C) 2002-2004 Timesys Corporation
8 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
9 * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
12 * Copyright (C) 2005-2009 Wind River Systems, Inc.
13 * Copyright (C) 2007 MontaVista Software, Inc.
14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
16 * Contributors at various stages not listed above:
17 * Jason Wessel ( jason.wessel@windriver.com )
18 * George Anzinger <george@mvista.com>
19 * Anurekh Saxena (anurekh.saxena@timesys.com)
20 * Lake Stevens Instrument Division (Glenn Engel)
21 * Jim Kingdon, Cygnus Support.
23 * Original KGDB stub: David Grothe <dave@gcom.com>,
24 * Tigran Aivazian <tigran@sco.com>
26 * This file is licensed under the terms of the GNU General Public License
27 * version 2. This program is licensed "as is" without any warranty of any
28 * kind, whether express or implied.
30 #include <linux/pid_namespace.h>
31 #include <linux/clocksource.h>
32 #include <linux/interrupt.h>
33 #include <linux/spinlock.h>
34 #include <linux/console.h>
35 #include <linux/threads.h>
36 #include <linux/uaccess.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/ptrace.h>
40 #include <linux/string.h>
41 #include <linux/delay.h>
42 #include <linux/sched.h>
43 #include <linux/sysrq.h>
44 #include <linux/init.h>
45 #include <linux/kgdb.h>
46 #include <linux/pid.h>
47 #include <linux/smp.h>
50 #include <asm/cacheflush.h>
51 #include <asm/byteorder.h>
52 #include <asm/atomic.h>
53 #include <asm/system.h>
55 #include "debug_core.h"
57 static int kgdb_break_asap;
59 struct debuggerinfo_struct kgdb_info[NR_CPUS];
62 * kgdb_connected - Is a host GDB connected to us?
65 EXPORT_SYMBOL_GPL(kgdb_connected);
67 /* All the KGDB handlers are installed */
68 static int kgdb_io_module_registered;
70 /* Guard for recursive entry */
71 static int exception_level;
73 struct kgdb_io *dbg_io_ops;
74 static DEFINE_SPINLOCK(kgdb_registration_lock);
76 /* kgdb console driver is loaded */
77 static int kgdb_con_registered;
78 /* determine if kgdb console output should be used */
79 static int kgdb_use_con;
81 static int __init opt_kgdb_con(char *str)
87 early_param("kgdbcon", opt_kgdb_con);
89 module_param(kgdb_use_con, int, 0644);
92 * Holds information about breakpoints in a kernel. These breakpoints are
93 * added and removed by gdb.
95 static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
96 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
100 * The CPU# of the active CPU, or -1 if none:
102 atomic_t kgdb_active = ATOMIC_INIT(-1);
105 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
106 * bootup code (which might not have percpu set up yet):
108 static atomic_t passive_cpu_wait[NR_CPUS];
109 static atomic_t cpu_in_kgdb[NR_CPUS];
110 atomic_t kgdb_setting_breakpoint;
112 struct task_struct *kgdb_usethread;
113 struct task_struct *kgdb_contthread;
115 int kgdb_single_step;
116 static pid_t kgdb_sstep_pid;
118 /* to keep track of the CPU which is doing the single stepping*/
119 atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
122 * If you are debugging a problem where roundup (the collection of
123 * all other CPUs) is a problem [this should be extremely rare],
124 * then use the nokgdbroundup option to avoid roundup. In that case
125 * the other CPUs might interfere with your debugging context, so
126 * use this with care:
128 static int kgdb_do_roundup = 1;
130 static int __init opt_nokgdbroundup(char *str)
137 early_param("nokgdbroundup", opt_nokgdbroundup);
140 * Finally, some KGDB code :-)
144 * Weak aliases for breakpoint management,
145 * can be overriden by architectures when needed:
147 int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
151 err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE);
155 return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
159 int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
161 return probe_kernel_write((char *)addr,
162 (char *)bundle, BREAK_INSTR_SIZE);
165 int __weak kgdb_validate_break_address(unsigned long addr)
167 char tmp_variable[BREAK_INSTR_SIZE];
169 /* Validate setting the breakpoint and then removing it. In the
170 * remove fails, the kernel needs to emit a bad message because we
171 * are deep trouble not being able to put things back the way we
174 err = kgdb_arch_set_breakpoint(addr, tmp_variable);
177 err = kgdb_arch_remove_breakpoint(addr, tmp_variable);
179 printk(KERN_ERR "KGDB: Critical breakpoint error, kernel "
180 "memory destroyed at: %lx", addr);
184 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
186 return instruction_pointer(regs);
189 int __weak kgdb_arch_init(void)
194 int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
200 kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
206 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
207 * @regs: Current &struct pt_regs.
209 * This function will be called if the particular architecture must
210 * disable hardware debugging while it is processing gdb packets or
211 * handling exception.
213 void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
218 * Some architectures need cache flushes when we set/clear a
221 static void kgdb_flush_swbreak_addr(unsigned long addr)
223 if (!CACHE_FLUSH_IS_SAFE)
226 if (current->mm && current->mm->mmap_cache) {
227 flush_cache_range(current->mm->mmap_cache,
228 addr, addr + BREAK_INSTR_SIZE);
230 /* Force flush instruction cache if it was outside the mm */
231 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
235 * SW breakpoint management:
237 int dbg_activate_sw_breakpoints(void)
244 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
245 if (kgdb_break[i].state != BP_SET)
248 addr = kgdb_break[i].bpt_addr;
249 error = kgdb_arch_set_breakpoint(addr,
250 kgdb_break[i].saved_instr);
253 printk(KERN_INFO "KGDB: BP install failed: %lx", addr);
257 kgdb_flush_swbreak_addr(addr);
258 kgdb_break[i].state = BP_ACTIVE;
263 int dbg_set_sw_break(unsigned long addr)
265 int err = kgdb_validate_break_address(addr);
272 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
273 if ((kgdb_break[i].state == BP_SET) &&
274 (kgdb_break[i].bpt_addr == addr))
277 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
278 if (kgdb_break[i].state == BP_REMOVED &&
279 kgdb_break[i].bpt_addr == addr) {
286 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
287 if (kgdb_break[i].state == BP_UNDEFINED) {
297 kgdb_break[breakno].state = BP_SET;
298 kgdb_break[breakno].type = BP_BREAKPOINT;
299 kgdb_break[breakno].bpt_addr = addr;
304 static int kgdb_deactivate_sw_breakpoints(void)
311 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
312 if (kgdb_break[i].state != BP_ACTIVE)
314 addr = kgdb_break[i].bpt_addr;
315 error = kgdb_arch_remove_breakpoint(addr,
316 kgdb_break[i].saved_instr);
318 printk(KERN_INFO "KGDB: BP remove failed: %lx\n", addr);
322 kgdb_flush_swbreak_addr(addr);
323 kgdb_break[i].state = BP_SET;
328 int dbg_remove_sw_break(unsigned long addr)
332 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
333 if ((kgdb_break[i].state == BP_SET) &&
334 (kgdb_break[i].bpt_addr == addr)) {
335 kgdb_break[i].state = BP_REMOVED;
342 int kgdb_isremovedbreak(unsigned long addr)
346 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
347 if ((kgdb_break[i].state == BP_REMOVED) &&
348 (kgdb_break[i].bpt_addr == addr))
354 int dbg_remove_all_break(void)
360 /* Clear memory breakpoints. */
361 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
362 if (kgdb_break[i].state != BP_ACTIVE)
364 addr = kgdb_break[i].bpt_addr;
365 error = kgdb_arch_remove_breakpoint(addr,
366 kgdb_break[i].saved_instr);
368 printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
371 kgdb_break[i].state = BP_UNDEFINED;
374 /* Clear hardware breakpoints. */
375 if (arch_kgdb_ops.remove_all_hw_break)
376 arch_kgdb_ops.remove_all_hw_break();
382 * Return true if there is a valid kgdb I/O module. Also if no
383 * debugger is attached a message can be printed to the console about
384 * waiting for the debugger to attach.
386 * The print_wait argument is only to be true when called from inside
387 * the core kgdb_handle_exception, because it will wait for the
388 * debugger to attach.
390 static int kgdb_io_ready(int print_wait)
396 if (atomic_read(&kgdb_setting_breakpoint))
399 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
403 static int kgdb_reenter_check(struct kgdb_state *ks)
407 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
410 /* Panic on recursive debugger calls: */
412 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
413 kgdb_deactivate_sw_breakpoints();
416 * If the break point removed ok at the place exception
417 * occurred, try to recover and print a warning to the end
418 * user because the user planted a breakpoint in a place that
419 * KGDB needs in order to function.
421 if (dbg_remove_sw_break(addr) == 0) {
423 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
424 dbg_activate_sw_breakpoints();
425 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
431 dbg_remove_all_break();
432 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
434 if (exception_level > 1) {
436 panic("Recursive entry to debugger");
439 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
441 panic("Recursive entry to debugger");
446 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
449 int sstep_tries = 100;
455 * Interrupts will be restored by the 'trap return' code, except when
458 local_irq_save(flags);
461 kgdb_info[cpu].debuggerinfo = regs;
462 kgdb_info[cpu].task = current;
464 * Make sure the above info reaches the primary CPU before
465 * our cpu_in_kgdb[] flag setting does:
467 atomic_inc(&cpu_in_kgdb[cpu]);
470 * CPU will loop if it is a slave or request to become a kgdb
471 * master cpu and acquire the kgdb_active lock:
474 if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
475 if (atomic_cmpxchg(&kgdb_active, -1, cpu) == cpu)
477 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
478 if (!atomic_read(&passive_cpu_wait[cpu]))
482 /* Return to normal operation by executing any
483 * hw breakpoint fixup.
485 if (arch_kgdb_ops.correct_hw_break)
486 arch_kgdb_ops.correct_hw_break();
489 atomic_dec(&cpu_in_kgdb[cpu]);
490 touch_softlockup_watchdog_sync();
491 clocksource_touch_watchdog();
492 local_irq_restore(flags);
499 * For single stepping, try to only enter on the processor
500 * that was single stepping. To gaurd against a deadlock, the
501 * kernel will only try for the value of sstep_tries before
502 * giving up and continuing on.
504 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
505 (kgdb_info[cpu].task &&
506 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
507 atomic_set(&kgdb_active, -1);
508 touch_softlockup_watchdog_sync();
509 clocksource_touch_watchdog();
510 local_irq_restore(flags);
515 if (!kgdb_io_ready(1)) {
517 goto kgdb_restore; /* No I/O connection, resume the system */
521 * Don't enter if we have hit a removed breakpoint.
523 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
526 /* Call the I/O driver's pre_exception routine */
527 if (dbg_io_ops->pre_exception)
528 dbg_io_ops->pre_exception();
530 kgdb_disable_hw_debug(ks->linux_regs);
533 * Get the passive CPU lock which will hold all the non-primary
534 * CPU in a spin state while the debugger is active
536 if (!kgdb_single_step) {
537 for (i = 0; i < NR_CPUS; i++)
538 atomic_inc(&passive_cpu_wait[i]);
542 /* Signal the other CPUs to enter kgdb_wait() */
543 if ((!kgdb_single_step) && kgdb_do_roundup)
544 kgdb_roundup_cpus(flags);
548 * Wait for the other CPUs to be notified and be waiting for us:
550 for_each_online_cpu(i) {
551 while (!atomic_read(&cpu_in_kgdb[i]))
556 * At this point the primary processor is completely
557 * in the debugger and all secondary CPUs are quiescent
559 kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
560 kgdb_deactivate_sw_breakpoints();
561 kgdb_single_step = 0;
562 kgdb_contthread = current;
564 trace_on = tracing_is_on();
568 /* Talk to debugger with gdbserial protocol */
569 error = gdb_serial_stub(ks);
571 /* Call the I/O driver's post_exception routine */
572 if (dbg_io_ops->post_exception)
573 dbg_io_ops->post_exception();
575 atomic_dec(&cpu_in_kgdb[ks->cpu]);
577 if (!kgdb_single_step) {
578 for (i = NR_CPUS-1; i >= 0; i--)
579 atomic_dec(&passive_cpu_wait[i]);
581 * Wait till all the CPUs have quit
584 for_each_online_cpu(i) {
585 while (atomic_read(&cpu_in_kgdb[i]))
591 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
592 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
593 if (kgdb_info[sstep_cpu].task)
594 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
600 /* Free kgdb_active */
601 atomic_set(&kgdb_active, -1);
602 touch_softlockup_watchdog_sync();
603 clocksource_touch_watchdog();
604 local_irq_restore(flags);
610 * kgdb_handle_exception() - main entry point from a kernel exception
613 * interface locks, if any (begin_session)
614 * kgdb lock (kgdb_active)
617 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
619 struct kgdb_state kgdb_var;
620 struct kgdb_state *ks = &kgdb_var;
623 ks->cpu = raw_smp_processor_id();
624 ks->ex_vector = evector;
626 ks->ex_vector = evector;
627 ks->err_code = ecode;
628 ks->kgdb_usethreadid = 0;
629 ks->linux_regs = regs;
631 if (kgdb_reenter_check(ks))
632 return 0; /* Ouch, double exception ! */
633 kgdb_info[ks->cpu].exception_state |= DCPU_WANT_MASTER;
634 ret = kgdb_cpu_enter(ks, regs);
635 kgdb_info[ks->cpu].exception_state &= ~DCPU_WANT_MASTER;
639 int kgdb_nmicallback(int cpu, void *regs)
642 struct kgdb_state kgdb_var;
643 struct kgdb_state *ks = &kgdb_var;
645 memset(ks, 0, sizeof(struct kgdb_state));
647 ks->linux_regs = regs;
649 if (!atomic_read(&cpu_in_kgdb[cpu]) &&
650 atomic_read(&kgdb_active) != -1 &&
651 atomic_read(&kgdb_active) != cpu) {
652 kgdb_info[cpu].exception_state |= DCPU_IS_SLAVE;
653 kgdb_cpu_enter(ks, regs);
654 kgdb_info[cpu].exception_state &= ~DCPU_IS_SLAVE;
661 static void kgdb_console_write(struct console *co, const char *s,
666 /* If we're debugging, or KGDB has not connected, don't try
668 if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
671 local_irq_save(flags);
672 gdbstub_msg_write(s, count);
673 local_irq_restore(flags);
676 static struct console kgdbcons = {
678 .write = kgdb_console_write,
679 .flags = CON_PRINTBUFFER | CON_ENABLED,
683 #ifdef CONFIG_MAGIC_SYSRQ
684 static void sysrq_handle_dbg(int key, struct tty_struct *tty)
687 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
691 printk(KERN_CRIT "Entering KGDB\n");
696 static struct sysrq_key_op sysrq_dbg_op = {
697 .handler = sysrq_handle_dbg,
698 .help_msg = "debug(G)",
699 .action_msg = "DEBUG",
703 static void kgdb_register_callbacks(void)
705 if (!kgdb_io_module_registered) {
706 kgdb_io_module_registered = 1;
708 #ifdef CONFIG_MAGIC_SYSRQ
709 register_sysrq_key('g', &sysrq_dbg_op);
711 if (kgdb_use_con && !kgdb_con_registered) {
712 register_console(&kgdbcons);
713 kgdb_con_registered = 1;
718 static void kgdb_unregister_callbacks(void)
721 * When this routine is called KGDB should unregister from the
722 * panic handler and clean up, making sure it is not handling any
723 * break exceptions at the time.
725 if (kgdb_io_module_registered) {
726 kgdb_io_module_registered = 0;
728 #ifdef CONFIG_MAGIC_SYSRQ
729 unregister_sysrq_key('g', &sysrq_dbg_op);
731 if (kgdb_con_registered) {
732 unregister_console(&kgdbcons);
733 kgdb_con_registered = 0;
738 static void kgdb_initial_breakpoint(void)
742 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
747 * kgdb_register_io_module - register KGDB IO module
748 * @new_dbg_io_ops: the io ops vector
750 * Register it with the KGDB core.
752 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
756 spin_lock(&kgdb_registration_lock);
759 spin_unlock(&kgdb_registration_lock);
761 printk(KERN_ERR "kgdb: Another I/O driver is already "
762 "registered with KGDB.\n");
766 if (new_dbg_io_ops->init) {
767 err = new_dbg_io_ops->init();
769 spin_unlock(&kgdb_registration_lock);
774 dbg_io_ops = new_dbg_io_ops;
776 spin_unlock(&kgdb_registration_lock);
778 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
779 new_dbg_io_ops->name);
782 kgdb_register_callbacks();
785 kgdb_initial_breakpoint();
789 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
792 * kkgdb_unregister_io_module - unregister KGDB IO module
793 * @old_dbg_io_ops: the io ops vector
795 * Unregister it with the KGDB core.
797 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
799 BUG_ON(kgdb_connected);
802 * KGDB is no longer able to communicate out, so
803 * unregister our callbacks and reset state.
805 kgdb_unregister_callbacks();
807 spin_lock(&kgdb_registration_lock);
809 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
812 spin_unlock(&kgdb_registration_lock);
815 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
816 old_dbg_io_ops->name);
818 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
821 * kgdb_breakpoint - generate breakpoint exception
823 * This function will generate a breakpoint exception. It is used at the
824 * beginning of a program to sync up with a debugger and can be used
825 * otherwise as a quick means to stop program execution and "break" into
828 void kgdb_breakpoint(void)
830 atomic_inc(&kgdb_setting_breakpoint);
831 wmb(); /* Sync point before breakpoint */
832 arch_kgdb_breakpoint();
833 wmb(); /* Sync point after breakpoint */
834 atomic_dec(&kgdb_setting_breakpoint);
836 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
838 static int __init opt_kgdb_wait(char *str)
842 if (kgdb_io_module_registered)
843 kgdb_initial_breakpoint();
848 early_param("kgdbwait", opt_kgdb_wait);