1 // SPDX-License-Identifier: GPL-2.0-only
5 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
7 * Copyright (C) 2000-2001 VERITAS Software Corporation.
8 * Copyright (C) 2002-2004 Timesys Corporation
9 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
10 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz>
11 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
12 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
13 * Copyright (C) 2005-2009 Wind River Systems, Inc.
14 * Copyright (C) 2007 MontaVista Software, Inc.
15 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
17 * Contributors at various stages not listed above:
18 * Jason Wessel ( jason.wessel@windriver.com )
19 * George Anzinger <george@mvista.com>
20 * Anurekh Saxena (anurekh.saxena@timesys.com)
21 * Lake Stevens Instrument Division (Glenn Engel)
22 * Jim Kingdon, Cygnus Support.
24 * Original KGDB stub: David Grothe <dave@gcom.com>,
25 * Tigran Aivazian <tigran@sco.com>
28 #define pr_fmt(fmt) "KGDB: " fmt
30 #include <linux/pid_namespace.h>
31 #include <linux/clocksource.h>
32 #include <linux/serial_core.h>
33 #include <linux/interrupt.h>
34 #include <linux/spinlock.h>
35 #include <linux/console.h>
36 #include <linux/threads.h>
37 #include <linux/uaccess.h>
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/ptrace.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/sched.h>
44 #include <linux/sysrq.h>
45 #include <linux/reboot.h>
46 #include <linux/init.h>
47 #include <linux/kgdb.h>
48 #include <linux/kdb.h>
49 #include <linux/nmi.h>
50 #include <linux/pid.h>
51 #include <linux/smp.h>
53 #include <linux/vmacache.h>
54 #include <linux/rcupdate.h>
55 #include <linux/irq.h>
56 #include <linux/security.h>
58 #include <asm/cacheflush.h>
59 #include <asm/byteorder.h>
60 #include <linux/atomic.h>
62 #include "debug_core.h"
64 static int kgdb_break_asap;
66 struct debuggerinfo_struct kgdb_info[NR_CPUS];
68 /* kgdb_connected - Is a host GDB connected to us? */
70 EXPORT_SYMBOL_GPL(kgdb_connected);
72 /* All the KGDB handlers are installed */
73 int kgdb_io_module_registered;
75 /* Guard for recursive entry */
76 static int exception_level;
78 struct kgdb_io *dbg_io_ops;
79 static DEFINE_SPINLOCK(kgdb_registration_lock);
81 /* Action for the reboot notifier, a global allow kdb to change it */
82 static int kgdbreboot;
83 /* kgdb console driver is loaded */
84 static int kgdb_con_registered;
85 /* determine if kgdb console output should be used */
86 static int kgdb_use_con;
87 /* Flag for alternate operations for early debugging */
88 bool dbg_is_early = true;
89 /* Next cpu to become the master debug core */
92 /* Use kdb or gdbserver mode */
95 module_param(kgdb_use_con, int, 0644);
96 module_param(kgdbreboot, int, 0644);
99 * Holds information about breakpoints in a kernel. These breakpoints are
100 * added and removed by gdb.
102 static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
103 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
107 * The CPU# of the active CPU, or -1 if none:
109 atomic_t kgdb_active = ATOMIC_INIT(-1);
110 EXPORT_SYMBOL_GPL(kgdb_active);
111 static DEFINE_RAW_SPINLOCK(dbg_master_lock);
112 static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
115 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
116 * bootup code (which might not have percpu set up yet):
118 static atomic_t masters_in_kgdb;
119 static atomic_t slaves_in_kgdb;
120 atomic_t kgdb_setting_breakpoint;
122 struct task_struct *kgdb_usethread;
123 struct task_struct *kgdb_contthread;
125 int kgdb_single_step;
126 static pid_t kgdb_sstep_pid;
128 /* to keep track of the CPU which is doing the single stepping*/
129 atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
132 * If you are debugging a problem where roundup (the collection of
133 * all other CPUs) is a problem [this should be extremely rare],
134 * then use the nokgdbroundup option to avoid roundup. In that case
135 * the other CPUs might interfere with your debugging context, so
136 * use this with care:
138 static int kgdb_do_roundup = 1;
140 static int __init opt_nokgdbroundup(char *str)
147 early_param("nokgdbroundup", opt_nokgdbroundup);
150 * Finally, some KGDB code :-)
154 * Weak aliases for breakpoint management,
155 * can be overridden by architectures when needed:
157 int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
161 err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
165 err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
166 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
169 NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
171 int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
173 return copy_to_kernel_nofault((char *)bpt->bpt_addr,
174 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
176 NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
178 int __weak kgdb_validate_break_address(unsigned long addr)
180 struct kgdb_bkpt tmp;
183 if (kgdb_within_blocklist(addr))
186 /* Validate setting the breakpoint and then removing it. If the
187 * remove fails, the kernel needs to emit a bad message because we
188 * are deep trouble not being able to put things back the way we
192 err = kgdb_arch_set_breakpoint(&tmp);
195 err = kgdb_arch_remove_breakpoint(&tmp);
197 pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
202 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
204 return instruction_pointer(regs);
206 NOKPROBE_SYMBOL(kgdb_arch_pc);
208 int __weak kgdb_arch_init(void)
213 int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
217 NOKPROBE_SYMBOL(kgdb_skipexception);
222 * Default (weak) implementation for kgdb_roundup_cpus
225 void __weak kgdb_call_nmi_hook(void *ignored)
228 * NOTE: get_irq_regs() is supposed to get the registers from
229 * before the IPI interrupt happened and so is supposed to
230 * show where the processor was. In some situations it's
231 * possible we might be called without an IPI, so it might be
232 * safer to figure out how to make kgdb_breakpoint() work
235 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
237 NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
239 static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd) =
240 CSD_INIT(kgdb_call_nmi_hook, NULL);
242 void __weak kgdb_roundup_cpus(void)
244 call_single_data_t *csd;
245 int this_cpu = raw_smp_processor_id();
249 for_each_online_cpu(cpu) {
250 /* No need to roundup ourselves */
254 csd = &per_cpu(kgdb_roundup_csd, cpu);
257 * If it didn't round up last time, don't try again
258 * since smp_call_function_single_async() will block.
260 * If rounding_up is false then we know that the
261 * previous call must have at least started and that
262 * means smp_call_function_single_async() won't block.
264 if (kgdb_info[cpu].rounding_up)
266 kgdb_info[cpu].rounding_up = true;
268 ret = smp_call_function_single_async(cpu, csd);
270 kgdb_info[cpu].rounding_up = false;
273 NOKPROBE_SYMBOL(kgdb_roundup_cpus);
278 * Some architectures need cache flushes when we set/clear a
281 static void kgdb_flush_swbreak_addr(unsigned long addr)
283 if (!CACHE_FLUSH_IS_SAFE)
289 for (i = 0; i < VMACACHE_SIZE; i++) {
290 if (!current->vmacache.vmas[i])
292 flush_cache_range(current->vmacache.vmas[i],
293 addr, addr + BREAK_INSTR_SIZE);
297 /* Force flush instruction cache if it was outside the mm */
298 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
300 NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
303 * SW breakpoint management:
305 int dbg_activate_sw_breakpoints(void)
311 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
312 if (kgdb_break[i].state != BP_SET)
315 error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
318 pr_info("BP install failed: %lx\n",
319 kgdb_break[i].bpt_addr);
323 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
324 kgdb_break[i].state = BP_ACTIVE;
328 NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
330 int dbg_set_sw_break(unsigned long addr)
332 int err = kgdb_validate_break_address(addr);
339 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
340 if ((kgdb_break[i].state == BP_SET) &&
341 (kgdb_break[i].bpt_addr == addr))
344 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
345 if (kgdb_break[i].state == BP_REMOVED &&
346 kgdb_break[i].bpt_addr == addr) {
353 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
354 if (kgdb_break[i].state == BP_UNDEFINED) {
364 kgdb_break[breakno].state = BP_SET;
365 kgdb_break[breakno].type = BP_BREAKPOINT;
366 kgdb_break[breakno].bpt_addr = addr;
371 int dbg_deactivate_sw_breakpoints(void)
377 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
378 if (kgdb_break[i].state != BP_ACTIVE)
380 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
382 pr_info("BP remove failed: %lx\n",
383 kgdb_break[i].bpt_addr);
387 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
388 kgdb_break[i].state = BP_SET;
392 NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
394 int dbg_remove_sw_break(unsigned long addr)
398 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
399 if ((kgdb_break[i].state == BP_SET) &&
400 (kgdb_break[i].bpt_addr == addr)) {
401 kgdb_break[i].state = BP_REMOVED;
408 int kgdb_isremovedbreak(unsigned long addr)
412 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
413 if ((kgdb_break[i].state == BP_REMOVED) &&
414 (kgdb_break[i].bpt_addr == addr))
420 int kgdb_has_hit_break(unsigned long addr)
424 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
425 if (kgdb_break[i].state == BP_ACTIVE &&
426 kgdb_break[i].bpt_addr == addr)
432 int dbg_remove_all_break(void)
437 /* Clear memory breakpoints. */
438 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
439 if (kgdb_break[i].state != BP_ACTIVE)
441 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
443 pr_err("breakpoint remove failed: %lx\n",
444 kgdb_break[i].bpt_addr);
446 kgdb_break[i].state = BP_UNDEFINED;
449 /* Clear hardware breakpoints. */
450 if (arch_kgdb_ops.remove_all_hw_break)
451 arch_kgdb_ops.remove_all_hw_break();
456 void kgdb_free_init_mem(void)
460 /* Clear init memory breakpoints. */
461 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
462 if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
463 kgdb_break[i].state = BP_UNDEFINED;
467 #ifdef CONFIG_KGDB_KDB
468 void kdb_dump_stack_on_cpu(int cpu)
470 if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
475 if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) {
476 kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n",
482 * In general, architectures don't support dumping the stack of a
483 * "running" process that's not the current one. From the point of
484 * view of the Linux, kernel processes that are looping in the kgdb
485 * slave loop are still "running". There's also no API (that actually
486 * works across all architectures) that can do a stack crawl based
487 * on registers passed as a parameter.
489 * Solve this conundrum by asking slave CPUs to do the backtrace
492 kgdb_info[cpu].exception_state |= DCPU_WANT_BT;
493 while (kgdb_info[cpu].exception_state & DCPU_WANT_BT)
499 * Return true if there is a valid kgdb I/O module. Also if no
500 * debugger is attached a message can be printed to the console about
501 * waiting for the debugger to attach.
503 * The print_wait argument is only to be true when called from inside
504 * the core kgdb_handle_exception, because it will wait for the
505 * debugger to attach.
507 static int kgdb_io_ready(int print_wait)
513 if (atomic_read(&kgdb_setting_breakpoint))
516 #ifdef CONFIG_KGDB_KDB
518 pr_crit("waiting... or $3#33 for KDB\n");
520 pr_crit("Waiting for remote debugger\n");
525 NOKPROBE_SYMBOL(kgdb_io_ready);
527 static int kgdb_reenter_check(struct kgdb_state *ks)
531 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
534 /* Panic on recursive debugger calls: */
536 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
537 dbg_deactivate_sw_breakpoints();
540 * If the break point removed ok at the place exception
541 * occurred, try to recover and print a warning to the end
542 * user because the user planted a breakpoint in a place that
543 * KGDB needs in order to function.
545 if (dbg_remove_sw_break(addr) == 0) {
547 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
548 dbg_activate_sw_breakpoints();
549 pr_crit("re-enter error: breakpoint removed %lx\n", addr);
554 dbg_remove_all_break();
555 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
557 if (exception_level > 1) {
559 kgdb_io_module_registered = false;
560 panic("Recursive entry to debugger");
563 pr_crit("re-enter exception: ALL breakpoints killed\n");
564 #ifdef CONFIG_KGDB_KDB
565 /* Allow kdb to debug itself one level */
569 panic("Recursive entry to debugger");
573 NOKPROBE_SYMBOL(kgdb_reenter_check);
575 static void dbg_touch_watchdogs(void)
577 touch_softlockup_watchdog_sync();
578 clocksource_touch_watchdog();
579 rcu_cpu_stall_reset();
581 NOKPROBE_SYMBOL(dbg_touch_watchdogs);
583 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
587 int sstep_tries = 100;
591 int online_cpus = num_online_cpus();
594 kgdb_info[ks->cpu].enter_kgdb++;
595 kgdb_info[ks->cpu].exception_state |= exception_state;
597 if (exception_state == DCPU_WANT_MASTER)
598 atomic_inc(&masters_in_kgdb);
600 atomic_inc(&slaves_in_kgdb);
602 if (arch_kgdb_ops.disable_hw_break)
603 arch_kgdb_ops.disable_hw_break(regs);
608 * Interrupts will be restored by the 'trap return' code, except when
611 local_irq_save(flags);
614 kgdb_info[cpu].debuggerinfo = regs;
615 kgdb_info[cpu].task = current;
616 kgdb_info[cpu].ret_state = 0;
617 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
619 /* Make sure the above info reaches the primary CPU */
622 if (exception_level == 1) {
623 if (raw_spin_trylock(&dbg_master_lock))
624 atomic_xchg(&kgdb_active, cpu);
625 goto cpu_master_loop;
629 * CPU will loop if it is a slave or request to become a kgdb
630 * master cpu and acquire the kgdb_active lock:
634 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
635 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
636 goto cpu_master_loop;
637 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
638 if (raw_spin_trylock(&dbg_master_lock)) {
639 atomic_xchg(&kgdb_active, cpu);
642 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
644 kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
645 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
646 if (!raw_spin_is_locked(&dbg_slave_lock))
650 /* Return to normal operation by executing any
651 * hw breakpoint fixup.
653 if (arch_kgdb_ops.correct_hw_break)
654 arch_kgdb_ops.correct_hw_break();
657 kgdb_info[cpu].debuggerinfo = NULL;
658 kgdb_info[cpu].task = NULL;
659 kgdb_info[cpu].exception_state &=
660 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
661 kgdb_info[cpu].enter_kgdb--;
662 smp_mb__before_atomic();
663 atomic_dec(&slaves_in_kgdb);
664 dbg_touch_watchdogs();
665 local_irq_restore(flags);
673 * For single stepping, try to only enter on the processor
674 * that was single stepping. To guard against a deadlock, the
675 * kernel will only try for the value of sstep_tries before
676 * giving up and continuing on.
678 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
679 (kgdb_info[cpu].task &&
680 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
681 atomic_set(&kgdb_active, -1);
682 raw_spin_unlock(&dbg_master_lock);
683 dbg_touch_watchdogs();
684 local_irq_restore(flags);
690 if (!kgdb_io_ready(1)) {
691 kgdb_info[cpu].ret_state = 1;
692 goto kgdb_restore; /* No I/O connection, resume the system */
696 * Don't enter if we have hit a removed breakpoint.
698 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
701 atomic_inc(&ignore_console_lock_warning);
703 /* Call the I/O driver's pre_exception routine */
704 if (dbg_io_ops->pre_exception)
705 dbg_io_ops->pre_exception();
708 * Get the passive CPU lock which will hold all the non-primary
709 * CPU in a spin state while the debugger is active
711 if (!kgdb_single_step)
712 raw_spin_lock(&dbg_slave_lock);
715 /* If send_ready set, slaves are already waiting */
717 atomic_set(ks->send_ready, 1);
719 /* Signal the other CPUs to enter kgdb_wait() */
720 else if ((!kgdb_single_step) && kgdb_do_roundup)
725 * Wait for the other CPUs to be notified and be waiting for us:
727 time_left = MSEC_PER_SEC;
728 while (kgdb_do_roundup && --time_left &&
729 (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
733 pr_crit("Timed out waiting for secondary CPUs.\n");
736 * At this point the primary processor is completely
737 * in the debugger and all secondary CPUs are quiescent
739 dbg_deactivate_sw_breakpoints();
740 kgdb_single_step = 0;
741 kgdb_contthread = current;
743 trace_on = tracing_is_on();
751 error = kdb_stub(ks);
757 * This is a brutal way to interfere with the debugger
758 * and prevent gdb being used to poke at kernel memory.
759 * This could cause trouble if lockdown is applied when
760 * there is already an active gdb session. For now the
761 * answer is simply "don't do that". Typically lockdown
762 * *will* be applied before the debug core gets started
763 * so only developers using kgdb for fairly advanced
764 * early kernel debug can be biten by this. Hopefully
765 * they are sophisticated enough to take care of
766 * themselves, especially with help from the lockdown
767 * message printed on the console!
769 if (security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL)) {
770 if (IS_ENABLED(CONFIG_KGDB_KDB)) {
771 /* Switch back to kdb if possible... */
775 /* ... otherwise just bail */
779 error = gdb_serial_stub(ks);
782 if (error == DBG_PASS_EVENT) {
783 dbg_kdb_mode = !dbg_kdb_mode;
784 } else if (error == DBG_SWITCH_CPU_EVENT) {
785 kgdb_info[dbg_switch_cpu].exception_state |=
789 kgdb_info[cpu].ret_state = error;
794 dbg_activate_sw_breakpoints();
796 /* Call the I/O driver's post_exception routine */
797 if (dbg_io_ops->post_exception)
798 dbg_io_ops->post_exception();
800 atomic_dec(&ignore_console_lock_warning);
802 if (!kgdb_single_step) {
803 raw_spin_unlock(&dbg_slave_lock);
804 /* Wait till all the CPUs have quit from the debugger. */
805 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
810 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
811 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
812 if (kgdb_info[sstep_cpu].task)
813 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
817 if (arch_kgdb_ops.correct_hw_break)
818 arch_kgdb_ops.correct_hw_break();
822 kgdb_info[cpu].debuggerinfo = NULL;
823 kgdb_info[cpu].task = NULL;
824 kgdb_info[cpu].exception_state &=
825 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
826 kgdb_info[cpu].enter_kgdb--;
827 smp_mb__before_atomic();
828 atomic_dec(&masters_in_kgdb);
829 /* Free kgdb_active */
830 atomic_set(&kgdb_active, -1);
831 raw_spin_unlock(&dbg_master_lock);
832 dbg_touch_watchdogs();
833 local_irq_restore(flags);
836 return kgdb_info[cpu].ret_state;
838 NOKPROBE_SYMBOL(kgdb_cpu_enter);
841 * kgdb_handle_exception() - main entry point from a kernel exception
844 * interface locks, if any (begin_session)
845 * kgdb lock (kgdb_active)
848 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
850 struct kgdb_state kgdb_var;
851 struct kgdb_state *ks = &kgdb_var;
854 if (arch_kgdb_ops.enable_nmi)
855 arch_kgdb_ops.enable_nmi(0);
857 * Avoid entering the debugger if we were triggered due to an oops
858 * but panic_timeout indicates the system should automatically
859 * reboot on panic. We don't want to get stuck waiting for input
860 * on such systems, especially if its "just" an oops.
862 if (signo != SIGTRAP && panic_timeout)
865 memset(ks, 0, sizeof(struct kgdb_state));
866 ks->cpu = raw_smp_processor_id();
867 ks->ex_vector = evector;
869 ks->err_code = ecode;
870 ks->linux_regs = regs;
872 if (kgdb_reenter_check(ks))
873 goto out; /* Ouch, double exception ! */
874 if (kgdb_info[ks->cpu].enter_kgdb != 0)
877 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
879 if (arch_kgdb_ops.enable_nmi)
880 arch_kgdb_ops.enable_nmi(1);
883 NOKPROBE_SYMBOL(kgdb_handle_exception);
886 * GDB places a breakpoint at this function to know dynamically loaded objects.
888 static int module_event(struct notifier_block *self, unsigned long val,
894 static struct notifier_block dbg_module_load_nb = {
895 .notifier_call = module_event,
898 int kgdb_nmicallback(int cpu, void *regs)
901 struct kgdb_state kgdb_var;
902 struct kgdb_state *ks = &kgdb_var;
904 kgdb_info[cpu].rounding_up = false;
906 memset(ks, 0, sizeof(struct kgdb_state));
908 ks->linux_regs = regs;
910 if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
911 raw_spin_is_locked(&dbg_master_lock)) {
912 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
918 NOKPROBE_SYMBOL(kgdb_nmicallback);
920 int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
921 atomic_t *send_ready)
924 if (!kgdb_io_ready(0) || !send_ready)
927 if (kgdb_info[cpu].enter_kgdb == 0) {
928 struct kgdb_state kgdb_var;
929 struct kgdb_state *ks = &kgdb_var;
931 memset(ks, 0, sizeof(struct kgdb_state));
933 ks->ex_vector = trapnr;
935 ks->err_code = err_code;
936 ks->linux_regs = regs;
937 ks->send_ready = send_ready;
938 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
944 NOKPROBE_SYMBOL(kgdb_nmicallin);
946 static void kgdb_console_write(struct console *co, const char *s,
951 /* If we're debugging, or KGDB has not connected, don't try
953 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
956 local_irq_save(flags);
957 gdbstub_msg_write(s, count);
958 local_irq_restore(flags);
961 static struct console kgdbcons = {
963 .write = kgdb_console_write,
964 .flags = CON_PRINTBUFFER | CON_ENABLED,
968 static int __init opt_kgdb_con(char *str)
972 if (kgdb_io_module_registered && !kgdb_con_registered) {
973 register_console(&kgdbcons);
974 kgdb_con_registered = 1;
980 early_param("kgdbcon", opt_kgdb_con);
982 #ifdef CONFIG_MAGIC_SYSRQ
983 static void sysrq_handle_dbg(int key)
986 pr_crit("ERROR: No KGDB I/O module available\n");
989 if (!kgdb_connected) {
990 #ifdef CONFIG_KGDB_KDB
992 pr_crit("KGDB or $3#33 for KDB\n");
994 pr_crit("Entering KGDB\n");
1001 static const struct sysrq_key_op sysrq_dbg_op = {
1002 .handler = sysrq_handle_dbg,
1003 .help_msg = "debug(g)",
1004 .action_msg = "DEBUG",
1008 void kgdb_panic(const char *msg)
1010 if (!kgdb_io_module_registered)
1014 * We don't want to get stuck waiting for input from user if
1015 * "panic_timeout" indicates the system should automatically
1022 kdb_printf("PANIC: %s\n", msg);
1027 static void kgdb_initial_breakpoint(void)
1029 kgdb_break_asap = 0;
1031 pr_crit("Waiting for connection from remote gdb...\n");
1035 void __weak kgdb_arch_late(void)
1039 void __init dbg_late_init(void)
1041 dbg_is_early = false;
1042 if (kgdb_io_module_registered)
1044 kdb_init(KDB_INIT_FULL);
1046 if (kgdb_io_module_registered && kgdb_break_asap)
1047 kgdb_initial_breakpoint();
1051 dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
1054 * Take the following action on reboot notify depending on value:
1055 * 1 == Enter debugger
1056 * 0 == [the default] detach debug client
1057 * -1 == Do nothing... and use this until the board resets
1059 switch (kgdbreboot) {
1072 static struct notifier_block dbg_reboot_notifier = {
1073 .notifier_call = dbg_notify_reboot,
1075 .priority = INT_MAX,
1078 static void kgdb_register_callbacks(void)
1080 if (!kgdb_io_module_registered) {
1081 kgdb_io_module_registered = 1;
1085 register_module_notifier(&dbg_module_load_nb);
1086 register_reboot_notifier(&dbg_reboot_notifier);
1087 #ifdef CONFIG_MAGIC_SYSRQ
1088 register_sysrq_key('g', &sysrq_dbg_op);
1090 if (kgdb_use_con && !kgdb_con_registered) {
1091 register_console(&kgdbcons);
1092 kgdb_con_registered = 1;
1097 static void kgdb_unregister_callbacks(void)
1100 * When this routine is called KGDB should unregister from
1101 * handlers and clean up, making sure it is not handling any
1102 * break exceptions at the time.
1104 if (kgdb_io_module_registered) {
1105 kgdb_io_module_registered = 0;
1106 unregister_reboot_notifier(&dbg_reboot_notifier);
1107 unregister_module_notifier(&dbg_module_load_nb);
1109 #ifdef CONFIG_MAGIC_SYSRQ
1110 unregister_sysrq_key('g', &sysrq_dbg_op);
1112 if (kgdb_con_registered) {
1113 unregister_console(&kgdbcons);
1114 kgdb_con_registered = 0;
1120 * kgdb_register_io_module - register KGDB IO module
1121 * @new_dbg_io_ops: the io ops vector
1123 * Register it with the KGDB core.
1125 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
1127 struct kgdb_io *old_dbg_io_ops;
1130 spin_lock(&kgdb_registration_lock);
1132 old_dbg_io_ops = dbg_io_ops;
1133 if (old_dbg_io_ops) {
1134 if (!old_dbg_io_ops->deinit) {
1135 spin_unlock(&kgdb_registration_lock);
1137 pr_err("KGDB I/O driver %s can't replace %s.\n",
1138 new_dbg_io_ops->name, old_dbg_io_ops->name);
1141 pr_info("Replacing I/O driver %s with %s\n",
1142 old_dbg_io_ops->name, new_dbg_io_ops->name);
1145 if (new_dbg_io_ops->init) {
1146 err = new_dbg_io_ops->init();
1148 spin_unlock(&kgdb_registration_lock);
1153 dbg_io_ops = new_dbg_io_ops;
1155 spin_unlock(&kgdb_registration_lock);
1157 if (old_dbg_io_ops) {
1158 old_dbg_io_ops->deinit();
1162 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
1165 kgdb_register_callbacks();
1167 if (kgdb_break_asap &&
1168 (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
1169 kgdb_initial_breakpoint();
1173 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1176 * kgdb_unregister_io_module - unregister KGDB IO module
1177 * @old_dbg_io_ops: the io ops vector
1179 * Unregister it with the KGDB core.
1181 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1183 BUG_ON(kgdb_connected);
1186 * KGDB is no longer able to communicate out, so
1187 * unregister our callbacks and reset state.
1189 kgdb_unregister_callbacks();
1191 spin_lock(&kgdb_registration_lock);
1193 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1196 spin_unlock(&kgdb_registration_lock);
1198 if (old_dbg_io_ops->deinit)
1199 old_dbg_io_ops->deinit();
1201 pr_info("Unregistered I/O driver %s, debugger disabled\n",
1202 old_dbg_io_ops->name);
1204 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1206 int dbg_io_get_char(void)
1208 int ret = dbg_io_ops->read_char();
1209 if (ret == NO_POLL_CHAR)
1219 * kgdb_breakpoint - generate breakpoint exception
1221 * This function will generate a breakpoint exception. It is used at the
1222 * beginning of a program to sync up with a debugger and can be used
1223 * otherwise as a quick means to stop program execution and "break" into
1226 noinline void kgdb_breakpoint(void)
1228 atomic_inc(&kgdb_setting_breakpoint);
1229 wmb(); /* Sync point before breakpoint */
1230 arch_kgdb_breakpoint();
1231 wmb(); /* Sync point after breakpoint */
1232 atomic_dec(&kgdb_setting_breakpoint);
1234 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1236 static int __init opt_kgdb_wait(char *str)
1238 kgdb_break_asap = 1;
1240 kdb_init(KDB_INIT_EARLY);
1241 if (kgdb_io_module_registered &&
1242 IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
1243 kgdb_initial_breakpoint();
1248 early_param("kgdbwait", opt_kgdb_wait);