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@ucw.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.
31 #define pr_fmt(fmt) "KGDB: " fmt
33 #include <linux/pid_namespace.h>
34 #include <linux/clocksource.h>
35 #include <linux/serial_core.h>
36 #include <linux/interrupt.h>
37 #include <linux/spinlock.h>
38 #include <linux/console.h>
39 #include <linux/threads.h>
40 #include <linux/uaccess.h>
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/ptrace.h>
44 #include <linux/string.h>
45 #include <linux/delay.h>
46 #include <linux/sched.h>
47 #include <linux/sysrq.h>
48 #include <linux/reboot.h>
49 #include <linux/init.h>
50 #include <linux/kgdb.h>
51 #include <linux/kdb.h>
52 #include <linux/nmi.h>
53 #include <linux/pid.h>
54 #include <linux/smp.h>
56 #include <linux/vmacache.h>
57 #include <linux/rcupdate.h>
58 #include <linux/irq.h>
59 #include <linux/security.h>
61 #include <asm/cacheflush.h>
62 #include <asm/byteorder.h>
63 #include <linux/atomic.h>
65 #include "debug_core.h"
67 static int kgdb_break_asap;
69 struct debuggerinfo_struct kgdb_info[NR_CPUS];
71 /* kgdb_connected - Is a host GDB connected to us? */
73 EXPORT_SYMBOL_GPL(kgdb_connected);
75 /* All the KGDB handlers are installed */
76 int kgdb_io_module_registered;
78 /* Guard for recursive entry */
79 static int exception_level;
81 struct kgdb_io *dbg_io_ops;
82 static DEFINE_SPINLOCK(kgdb_registration_lock);
84 /* Action for the reboot notifier, a global allow kdb to change it */
85 static int kgdbreboot;
86 /* kgdb console driver is loaded */
87 static int kgdb_con_registered;
88 /* determine if kgdb console output should be used */
89 static int kgdb_use_con;
90 /* Flag for alternate operations for early debugging */
91 bool dbg_is_early = true;
92 /* Next cpu to become the master debug core */
95 /* Use kdb or gdbserver mode */
98 module_param(kgdb_use_con, int, 0644);
99 module_param(kgdbreboot, int, 0644);
102 * Holds information about breakpoints in a kernel. These breakpoints are
103 * added and removed by gdb.
105 static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
106 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
110 * The CPU# of the active CPU, or -1 if none:
112 atomic_t kgdb_active = ATOMIC_INIT(-1);
113 EXPORT_SYMBOL_GPL(kgdb_active);
114 static DEFINE_RAW_SPINLOCK(dbg_master_lock);
115 static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
118 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
119 * bootup code (which might not have percpu set up yet):
121 static atomic_t masters_in_kgdb;
122 static atomic_t slaves_in_kgdb;
123 static atomic_t kgdb_break_tasklet_var;
124 atomic_t kgdb_setting_breakpoint;
126 struct task_struct *kgdb_usethread;
127 struct task_struct *kgdb_contthread;
129 int kgdb_single_step;
130 static pid_t kgdb_sstep_pid;
132 /* to keep track of the CPU which is doing the single stepping*/
133 atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
136 * If you are debugging a problem where roundup (the collection of
137 * all other CPUs) is a problem [this should be extremely rare],
138 * then use the nokgdbroundup option to avoid roundup. In that case
139 * the other CPUs might interfere with your debugging context, so
140 * use this with care:
142 static int kgdb_do_roundup = 1;
144 static int __init opt_nokgdbroundup(char *str)
151 early_param("nokgdbroundup", opt_nokgdbroundup);
154 * Finally, some KGDB code :-)
158 * Weak aliases for breakpoint management,
159 * can be overridden by architectures when needed:
161 int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
165 err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
169 err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
170 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
173 NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
175 int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
177 return copy_to_kernel_nofault((char *)bpt->bpt_addr,
178 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
180 NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
182 int __weak kgdb_validate_break_address(unsigned long addr)
184 struct kgdb_bkpt tmp;
187 if (kgdb_within_blocklist(addr))
190 /* Validate setting the breakpoint and then removing it. If the
191 * remove fails, the kernel needs to emit a bad message because we
192 * are deep trouble not being able to put things back the way we
196 err = kgdb_arch_set_breakpoint(&tmp);
199 err = kgdb_arch_remove_breakpoint(&tmp);
201 pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
206 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
208 return instruction_pointer(regs);
210 NOKPROBE_SYMBOL(kgdb_arch_pc);
212 int __weak kgdb_arch_init(void)
217 int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
221 NOKPROBE_SYMBOL(kgdb_skipexception);
226 * Default (weak) implementation for kgdb_roundup_cpus
229 static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd);
231 void __weak kgdb_call_nmi_hook(void *ignored)
234 * NOTE: get_irq_regs() is supposed to get the registers from
235 * before the IPI interrupt happened and so is supposed to
236 * show where the processor was. In some situations it's
237 * possible we might be called without an IPI, so it might be
238 * safer to figure out how to make kgdb_breakpoint() work
241 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
243 NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
245 void __weak kgdb_roundup_cpus(void)
247 call_single_data_t *csd;
248 int this_cpu = raw_smp_processor_id();
252 for_each_online_cpu(cpu) {
253 /* No need to roundup ourselves */
257 csd = &per_cpu(kgdb_roundup_csd, cpu);
260 * If it didn't round up last time, don't try again
261 * since smp_call_function_single_async() will block.
263 * If rounding_up is false then we know that the
264 * previous call must have at least started and that
265 * means smp_call_function_single_async() won't block.
267 if (kgdb_info[cpu].rounding_up)
269 kgdb_info[cpu].rounding_up = true;
271 csd->func = kgdb_call_nmi_hook;
272 ret = smp_call_function_single_async(cpu, csd);
274 kgdb_info[cpu].rounding_up = false;
277 NOKPROBE_SYMBOL(kgdb_roundup_cpus);
282 * Some architectures need cache flushes when we set/clear a
285 static void kgdb_flush_swbreak_addr(unsigned long addr)
287 if (!CACHE_FLUSH_IS_SAFE)
293 for (i = 0; i < VMACACHE_SIZE; i++) {
294 if (!current->vmacache.vmas[i])
296 flush_cache_range(current->vmacache.vmas[i],
297 addr, addr + BREAK_INSTR_SIZE);
301 /* Force flush instruction cache if it was outside the mm */
302 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
304 NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
307 * SW breakpoint management:
309 int dbg_activate_sw_breakpoints(void)
315 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
316 if (kgdb_break[i].state != BP_SET)
319 error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
322 pr_info("BP install failed: %lx\n",
323 kgdb_break[i].bpt_addr);
327 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
328 kgdb_break[i].state = BP_ACTIVE;
332 NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
334 int dbg_set_sw_break(unsigned long addr)
336 int err = kgdb_validate_break_address(addr);
343 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
344 if ((kgdb_break[i].state == BP_SET) &&
345 (kgdb_break[i].bpt_addr == addr))
348 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
349 if (kgdb_break[i].state == BP_REMOVED &&
350 kgdb_break[i].bpt_addr == addr) {
357 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
358 if (kgdb_break[i].state == BP_UNDEFINED) {
368 kgdb_break[breakno].state = BP_SET;
369 kgdb_break[breakno].type = BP_BREAKPOINT;
370 kgdb_break[breakno].bpt_addr = addr;
375 int dbg_deactivate_sw_breakpoints(void)
381 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
382 if (kgdb_break[i].state != BP_ACTIVE)
384 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
386 pr_info("BP remove failed: %lx\n",
387 kgdb_break[i].bpt_addr);
391 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
392 kgdb_break[i].state = BP_SET;
396 NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
398 int dbg_remove_sw_break(unsigned long addr)
402 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
403 if ((kgdb_break[i].state == BP_SET) &&
404 (kgdb_break[i].bpt_addr == addr)) {
405 kgdb_break[i].state = BP_REMOVED;
412 int kgdb_isremovedbreak(unsigned long addr)
416 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
417 if ((kgdb_break[i].state == BP_REMOVED) &&
418 (kgdb_break[i].bpt_addr == addr))
424 int kgdb_has_hit_break(unsigned long addr)
428 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
429 if (kgdb_break[i].state == BP_ACTIVE &&
430 kgdb_break[i].bpt_addr == addr)
436 int dbg_remove_all_break(void)
441 /* Clear memory breakpoints. */
442 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
443 if (kgdb_break[i].state != BP_ACTIVE)
445 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
447 pr_err("breakpoint remove failed: %lx\n",
448 kgdb_break[i].bpt_addr);
450 kgdb_break[i].state = BP_UNDEFINED;
453 /* Clear hardware breakpoints. */
454 if (arch_kgdb_ops.remove_all_hw_break)
455 arch_kgdb_ops.remove_all_hw_break();
460 void kgdb_free_init_mem(void)
464 /* Clear init memory breakpoints. */
465 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
466 if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
467 kgdb_break[i].state = BP_UNDEFINED;
471 #ifdef CONFIG_KGDB_KDB
472 void kdb_dump_stack_on_cpu(int cpu)
474 if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
479 if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) {
480 kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n",
486 * In general, architectures don't support dumping the stack of a
487 * "running" process that's not the current one. From the point of
488 * view of the Linux, kernel processes that are looping in the kgdb
489 * slave loop are still "running". There's also no API (that actually
490 * works across all architectures) that can do a stack crawl based
491 * on registers passed as a parameter.
493 * Solve this conundrum by asking slave CPUs to do the backtrace
496 kgdb_info[cpu].exception_state |= DCPU_WANT_BT;
497 while (kgdb_info[cpu].exception_state & DCPU_WANT_BT)
503 * Return true if there is a valid kgdb I/O module. Also if no
504 * debugger is attached a message can be printed to the console about
505 * waiting for the debugger to attach.
507 * The print_wait argument is only to be true when called from inside
508 * the core kgdb_handle_exception, because it will wait for the
509 * debugger to attach.
511 static int kgdb_io_ready(int print_wait)
517 if (atomic_read(&kgdb_setting_breakpoint))
520 #ifdef CONFIG_KGDB_KDB
522 pr_crit("waiting... or $3#33 for KDB\n");
524 pr_crit("Waiting for remote debugger\n");
529 NOKPROBE_SYMBOL(kgdb_io_ready);
531 static int kgdb_reenter_check(struct kgdb_state *ks)
535 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
538 /* Panic on recursive debugger calls: */
540 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
541 dbg_deactivate_sw_breakpoints();
544 * If the break point removed ok at the place exception
545 * occurred, try to recover and print a warning to the end
546 * user because the user planted a breakpoint in a place that
547 * KGDB needs in order to function.
549 if (dbg_remove_sw_break(addr) == 0) {
551 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
552 dbg_activate_sw_breakpoints();
553 pr_crit("re-enter error: breakpoint removed %lx\n", addr);
558 dbg_remove_all_break();
559 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
561 if (exception_level > 1) {
563 kgdb_io_module_registered = false;
564 panic("Recursive entry to debugger");
567 pr_crit("re-enter exception: ALL breakpoints killed\n");
568 #ifdef CONFIG_KGDB_KDB
569 /* Allow kdb to debug itself one level */
573 panic("Recursive entry to debugger");
577 NOKPROBE_SYMBOL(kgdb_reenter_check);
579 static void dbg_touch_watchdogs(void)
581 touch_softlockup_watchdog_sync();
582 clocksource_touch_watchdog();
583 rcu_cpu_stall_reset();
585 NOKPROBE_SYMBOL(dbg_touch_watchdogs);
587 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
591 int sstep_tries = 100;
595 int online_cpus = num_online_cpus();
598 kgdb_info[ks->cpu].enter_kgdb++;
599 kgdb_info[ks->cpu].exception_state |= exception_state;
601 if (exception_state == DCPU_WANT_MASTER)
602 atomic_inc(&masters_in_kgdb);
604 atomic_inc(&slaves_in_kgdb);
606 if (arch_kgdb_ops.disable_hw_break)
607 arch_kgdb_ops.disable_hw_break(regs);
612 * Interrupts will be restored by the 'trap return' code, except when
615 local_irq_save(flags);
618 kgdb_info[cpu].debuggerinfo = regs;
619 kgdb_info[cpu].task = current;
620 kgdb_info[cpu].ret_state = 0;
621 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
623 /* Make sure the above info reaches the primary CPU */
626 if (exception_level == 1) {
627 if (raw_spin_trylock(&dbg_master_lock))
628 atomic_xchg(&kgdb_active, cpu);
629 goto cpu_master_loop;
633 * CPU will loop if it is a slave or request to become a kgdb
634 * master cpu and acquire the kgdb_active lock:
638 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
639 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
640 goto cpu_master_loop;
641 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
642 if (raw_spin_trylock(&dbg_master_lock)) {
643 atomic_xchg(&kgdb_active, cpu);
646 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
648 kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
649 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
650 if (!raw_spin_is_locked(&dbg_slave_lock))
654 /* Return to normal operation by executing any
655 * hw breakpoint fixup.
657 if (arch_kgdb_ops.correct_hw_break)
658 arch_kgdb_ops.correct_hw_break();
661 kgdb_info[cpu].debuggerinfo = NULL;
662 kgdb_info[cpu].task = NULL;
663 kgdb_info[cpu].exception_state &=
664 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
665 kgdb_info[cpu].enter_kgdb--;
666 smp_mb__before_atomic();
667 atomic_dec(&slaves_in_kgdb);
668 dbg_touch_watchdogs();
669 local_irq_restore(flags);
677 * For single stepping, try to only enter on the processor
678 * that was single stepping. To guard against a deadlock, the
679 * kernel will only try for the value of sstep_tries before
680 * giving up and continuing on.
682 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
683 (kgdb_info[cpu].task &&
684 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
685 atomic_set(&kgdb_active, -1);
686 raw_spin_unlock(&dbg_master_lock);
687 dbg_touch_watchdogs();
688 local_irq_restore(flags);
694 if (!kgdb_io_ready(1)) {
695 kgdb_info[cpu].ret_state = 1;
696 goto kgdb_restore; /* No I/O connection, resume the system */
700 * Don't enter if we have hit a removed breakpoint.
702 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
705 atomic_inc(&ignore_console_lock_warning);
707 /* Call the I/O driver's pre_exception routine */
708 if (dbg_io_ops->pre_exception)
709 dbg_io_ops->pre_exception();
712 * Get the passive CPU lock which will hold all the non-primary
713 * CPU in a spin state while the debugger is active
715 if (!kgdb_single_step)
716 raw_spin_lock(&dbg_slave_lock);
719 /* If send_ready set, slaves are already waiting */
721 atomic_set(ks->send_ready, 1);
723 /* Signal the other CPUs to enter kgdb_wait() */
724 else if ((!kgdb_single_step) && kgdb_do_roundup)
729 * Wait for the other CPUs to be notified and be waiting for us:
731 time_left = MSEC_PER_SEC;
732 while (kgdb_do_roundup && --time_left &&
733 (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
737 pr_crit("Timed out waiting for secondary CPUs.\n");
740 * At this point the primary processor is completely
741 * in the debugger and all secondary CPUs are quiescent
743 dbg_deactivate_sw_breakpoints();
744 kgdb_single_step = 0;
745 kgdb_contthread = current;
747 trace_on = tracing_is_on();
755 error = kdb_stub(ks);
761 * This is a brutal way to interfere with the debugger
762 * and prevent gdb being used to poke at kernel memory.
763 * This could cause trouble if lockdown is applied when
764 * there is already an active gdb session. For now the
765 * answer is simply "don't do that". Typically lockdown
766 * *will* be applied before the debug core gets started
767 * so only developers using kgdb for fairly advanced
768 * early kernel debug can be biten by this. Hopefully
769 * they are sophisticated enough to take care of
770 * themselves, especially with help from the lockdown
771 * message printed on the console!
773 if (security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL)) {
774 if (IS_ENABLED(CONFIG_KGDB_KDB)) {
775 /* Switch back to kdb if possible... */
779 /* ... otherwise just bail */
783 error = gdb_serial_stub(ks);
786 if (error == DBG_PASS_EVENT) {
787 dbg_kdb_mode = !dbg_kdb_mode;
788 } else if (error == DBG_SWITCH_CPU_EVENT) {
789 kgdb_info[dbg_switch_cpu].exception_state |=
793 kgdb_info[cpu].ret_state = error;
798 dbg_activate_sw_breakpoints();
800 /* Call the I/O driver's post_exception routine */
801 if (dbg_io_ops->post_exception)
802 dbg_io_ops->post_exception();
804 atomic_dec(&ignore_console_lock_warning);
806 if (!kgdb_single_step) {
807 raw_spin_unlock(&dbg_slave_lock);
808 /* Wait till all the CPUs have quit from the debugger. */
809 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
814 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
815 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
816 if (kgdb_info[sstep_cpu].task)
817 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
821 if (arch_kgdb_ops.correct_hw_break)
822 arch_kgdb_ops.correct_hw_break();
826 kgdb_info[cpu].debuggerinfo = NULL;
827 kgdb_info[cpu].task = NULL;
828 kgdb_info[cpu].exception_state &=
829 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
830 kgdb_info[cpu].enter_kgdb--;
831 smp_mb__before_atomic();
832 atomic_dec(&masters_in_kgdb);
833 /* Free kgdb_active */
834 atomic_set(&kgdb_active, -1);
835 raw_spin_unlock(&dbg_master_lock);
836 dbg_touch_watchdogs();
837 local_irq_restore(flags);
840 return kgdb_info[cpu].ret_state;
842 NOKPROBE_SYMBOL(kgdb_cpu_enter);
845 * kgdb_handle_exception() - main entry point from a kernel exception
848 * interface locks, if any (begin_session)
849 * kgdb lock (kgdb_active)
852 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
854 struct kgdb_state kgdb_var;
855 struct kgdb_state *ks = &kgdb_var;
858 if (arch_kgdb_ops.enable_nmi)
859 arch_kgdb_ops.enable_nmi(0);
861 * Avoid entering the debugger if we were triggered due to an oops
862 * but panic_timeout indicates the system should automatically
863 * reboot on panic. We don't want to get stuck waiting for input
864 * on such systems, especially if its "just" an oops.
866 if (signo != SIGTRAP && panic_timeout)
869 memset(ks, 0, sizeof(struct kgdb_state));
870 ks->cpu = raw_smp_processor_id();
871 ks->ex_vector = evector;
873 ks->err_code = ecode;
874 ks->linux_regs = regs;
876 if (kgdb_reenter_check(ks))
877 goto out; /* Ouch, double exception ! */
878 if (kgdb_info[ks->cpu].enter_kgdb != 0)
881 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
883 if (arch_kgdb_ops.enable_nmi)
884 arch_kgdb_ops.enable_nmi(1);
887 NOKPROBE_SYMBOL(kgdb_handle_exception);
890 * GDB places a breakpoint at this function to know dynamically loaded objects.
892 static int module_event(struct notifier_block *self, unsigned long val,
898 static struct notifier_block dbg_module_load_nb = {
899 .notifier_call = module_event,
902 int kgdb_nmicallback(int cpu, void *regs)
905 struct kgdb_state kgdb_var;
906 struct kgdb_state *ks = &kgdb_var;
908 kgdb_info[cpu].rounding_up = false;
910 memset(ks, 0, sizeof(struct kgdb_state));
912 ks->linux_regs = regs;
914 if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
915 raw_spin_is_locked(&dbg_master_lock)) {
916 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
922 NOKPROBE_SYMBOL(kgdb_nmicallback);
924 int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
925 atomic_t *send_ready)
928 if (!kgdb_io_ready(0) || !send_ready)
931 if (kgdb_info[cpu].enter_kgdb == 0) {
932 struct kgdb_state kgdb_var;
933 struct kgdb_state *ks = &kgdb_var;
935 memset(ks, 0, sizeof(struct kgdb_state));
937 ks->ex_vector = trapnr;
939 ks->err_code = err_code;
940 ks->linux_regs = regs;
941 ks->send_ready = send_ready;
942 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
948 NOKPROBE_SYMBOL(kgdb_nmicallin);
950 static void kgdb_console_write(struct console *co, const char *s,
955 /* If we're debugging, or KGDB has not connected, don't try
957 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
960 local_irq_save(flags);
961 gdbstub_msg_write(s, count);
962 local_irq_restore(flags);
965 static struct console kgdbcons = {
967 .write = kgdb_console_write,
968 .flags = CON_PRINTBUFFER | CON_ENABLED,
972 static int __init opt_kgdb_con(char *str)
976 if (kgdb_io_module_registered && !kgdb_con_registered) {
977 register_console(&kgdbcons);
978 kgdb_con_registered = 1;
984 early_param("kgdbcon", opt_kgdb_con);
986 #ifdef CONFIG_MAGIC_SYSRQ
987 static void sysrq_handle_dbg(int key)
990 pr_crit("ERROR: No KGDB I/O module available\n");
993 if (!kgdb_connected) {
994 #ifdef CONFIG_KGDB_KDB
996 pr_crit("KGDB or $3#33 for KDB\n");
998 pr_crit("Entering KGDB\n");
1005 static const struct sysrq_key_op sysrq_dbg_op = {
1006 .handler = sysrq_handle_dbg,
1007 .help_msg = "debug(g)",
1008 .action_msg = "DEBUG",
1012 void kgdb_panic(const char *msg)
1014 if (!kgdb_io_module_registered)
1018 * We don't want to get stuck waiting for input from user if
1019 * "panic_timeout" indicates the system should automatically
1026 console_flush_on_panic(CONSOLE_FLUSH_PENDING);
1029 kdb_printf("PANIC: %s\n", msg);
1034 static void kgdb_initial_breakpoint(void)
1036 kgdb_break_asap = 0;
1038 pr_crit("Waiting for connection from remote gdb...\n");
1042 void __weak kgdb_arch_late(void)
1046 void __init dbg_late_init(void)
1048 dbg_is_early = false;
1049 if (kgdb_io_module_registered)
1051 kdb_init(KDB_INIT_FULL);
1053 if (kgdb_io_module_registered && kgdb_break_asap)
1054 kgdb_initial_breakpoint();
1058 dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
1061 * Take the following action on reboot notify depending on value:
1062 * 1 == Enter debugger
1063 * 0 == [the default] detatch debug client
1064 * -1 == Do nothing... and use this until the board resets
1066 switch (kgdbreboot) {
1078 static struct notifier_block dbg_reboot_notifier = {
1079 .notifier_call = dbg_notify_reboot,
1081 .priority = INT_MAX,
1084 static void kgdb_register_callbacks(void)
1086 if (!kgdb_io_module_registered) {
1087 kgdb_io_module_registered = 1;
1091 register_module_notifier(&dbg_module_load_nb);
1092 register_reboot_notifier(&dbg_reboot_notifier);
1093 #ifdef CONFIG_MAGIC_SYSRQ
1094 register_sysrq_key('g', &sysrq_dbg_op);
1096 if (kgdb_use_con && !kgdb_con_registered) {
1097 register_console(&kgdbcons);
1098 kgdb_con_registered = 1;
1103 static void kgdb_unregister_callbacks(void)
1106 * When this routine is called KGDB should unregister from
1107 * handlers and clean up, making sure it is not handling any
1108 * break exceptions at the time.
1110 if (kgdb_io_module_registered) {
1111 kgdb_io_module_registered = 0;
1112 unregister_reboot_notifier(&dbg_reboot_notifier);
1113 unregister_module_notifier(&dbg_module_load_nb);
1115 #ifdef CONFIG_MAGIC_SYSRQ
1116 unregister_sysrq_key('g', &sysrq_dbg_op);
1118 if (kgdb_con_registered) {
1119 unregister_console(&kgdbcons);
1120 kgdb_con_registered = 0;
1126 * There are times a tasklet needs to be used vs a compiled in
1127 * break point so as to cause an exception outside a kgdb I/O module,
1128 * such as is the case with kgdboe, where calling a breakpoint in the
1129 * I/O driver itself would be fatal.
1131 static void kgdb_tasklet_bpt(unsigned long ing)
1134 atomic_set(&kgdb_break_tasklet_var, 0);
1137 static DECLARE_TASKLET_OLD(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt);
1139 void kgdb_schedule_breakpoint(void)
1141 if (atomic_read(&kgdb_break_tasklet_var) ||
1142 atomic_read(&kgdb_active) != -1 ||
1143 atomic_read(&kgdb_setting_breakpoint))
1145 atomic_inc(&kgdb_break_tasklet_var);
1146 tasklet_schedule(&kgdb_tasklet_breakpoint);
1148 EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint);
1151 * kgdb_register_io_module - register KGDB IO module
1152 * @new_dbg_io_ops: the io ops vector
1154 * Register it with the KGDB core.
1156 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
1158 struct kgdb_io *old_dbg_io_ops;
1161 spin_lock(&kgdb_registration_lock);
1163 old_dbg_io_ops = dbg_io_ops;
1164 if (old_dbg_io_ops) {
1165 if (!old_dbg_io_ops->deinit) {
1166 spin_unlock(&kgdb_registration_lock);
1168 pr_err("KGDB I/O driver %s can't replace %s.\n",
1169 new_dbg_io_ops->name, old_dbg_io_ops->name);
1172 pr_info("Replacing I/O driver %s with %s\n",
1173 old_dbg_io_ops->name, new_dbg_io_ops->name);
1176 if (new_dbg_io_ops->init) {
1177 err = new_dbg_io_ops->init();
1179 spin_unlock(&kgdb_registration_lock);
1184 dbg_io_ops = new_dbg_io_ops;
1186 spin_unlock(&kgdb_registration_lock);
1188 if (old_dbg_io_ops) {
1189 old_dbg_io_ops->deinit();
1193 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
1196 kgdb_register_callbacks();
1198 if (kgdb_break_asap &&
1199 (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
1200 kgdb_initial_breakpoint();
1204 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1207 * kkgdb_unregister_io_module - unregister KGDB IO module
1208 * @old_dbg_io_ops: the io ops vector
1210 * Unregister it with the KGDB core.
1212 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1214 BUG_ON(kgdb_connected);
1217 * KGDB is no longer able to communicate out, so
1218 * unregister our callbacks and reset state.
1220 kgdb_unregister_callbacks();
1222 spin_lock(&kgdb_registration_lock);
1224 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1227 spin_unlock(&kgdb_registration_lock);
1229 if (old_dbg_io_ops->deinit)
1230 old_dbg_io_ops->deinit();
1232 pr_info("Unregistered I/O driver %s, debugger disabled\n",
1233 old_dbg_io_ops->name);
1235 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1237 int dbg_io_get_char(void)
1239 int ret = dbg_io_ops->read_char();
1240 if (ret == NO_POLL_CHAR)
1250 * kgdb_breakpoint - generate breakpoint exception
1252 * This function will generate a breakpoint exception. It is used at the
1253 * beginning of a program to sync up with a debugger and can be used
1254 * otherwise as a quick means to stop program execution and "break" into
1257 noinline void kgdb_breakpoint(void)
1259 atomic_inc(&kgdb_setting_breakpoint);
1260 wmb(); /* Sync point before breakpoint */
1261 arch_kgdb_breakpoint();
1262 wmb(); /* Sync point after breakpoint */
1263 atomic_dec(&kgdb_setting_breakpoint);
1265 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1267 static int __init opt_kgdb_wait(char *str)
1269 kgdb_break_asap = 1;
1271 kdb_init(KDB_INIT_EARLY);
1272 if (kgdb_io_module_registered &&
1273 IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
1274 kgdb_initial_breakpoint();
1279 early_param("kgdbwait", opt_kgdb_wait);