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();
749 if (security_locked_down(LOCKDOWN_KGDB
))
753 error
= kdb_stub(ks
);
758 error
= gdb_serial_stub(ks
);
761 if (error
== DBG_PASS_EVENT
) {
762 dbg_kdb_mode
= !dbg_kdb_mode
;
763 } else if (error
== DBG_SWITCH_CPU_EVENT
) {
764 kgdb_info
[dbg_switch_cpu
].exception_state
|=
768 kgdb_info
[cpu
].ret_state
= error
;
773 dbg_activate_sw_breakpoints();
775 /* Call the I/O driver's post_exception routine */
776 if (dbg_io_ops
->post_exception
)
777 dbg_io_ops
->post_exception();
779 atomic_dec(&ignore_console_lock_warning
);
781 if (!kgdb_single_step
) {
782 raw_spin_unlock(&dbg_slave_lock
);
783 /* Wait till all the CPUs have quit from the debugger. */
784 while (kgdb_do_roundup
&& atomic_read(&slaves_in_kgdb
))
789 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1) {
790 int sstep_cpu
= atomic_read(&kgdb_cpu_doing_single_step
);
791 if (kgdb_info
[sstep_cpu
].task
)
792 kgdb_sstep_pid
= kgdb_info
[sstep_cpu
].task
->pid
;
796 if (arch_kgdb_ops
.correct_hw_break
)
797 arch_kgdb_ops
.correct_hw_break();
801 kgdb_info
[cpu
].debuggerinfo
= NULL
;
802 kgdb_info
[cpu
].task
= NULL
;
803 kgdb_info
[cpu
].exception_state
&=
804 ~(DCPU_WANT_MASTER
| DCPU_IS_SLAVE
);
805 kgdb_info
[cpu
].enter_kgdb
--;
806 smp_mb__before_atomic();
807 atomic_dec(&masters_in_kgdb
);
808 /* Free kgdb_active */
809 atomic_set(&kgdb_active
, -1);
810 raw_spin_unlock(&dbg_master_lock
);
811 dbg_touch_watchdogs();
812 local_irq_restore(flags
);
815 return kgdb_info
[cpu
].ret_state
;
817 NOKPROBE_SYMBOL(kgdb_cpu_enter
);
820 * kgdb_handle_exception() - main entry point from a kernel exception
823 * interface locks, if any (begin_session)
824 * kgdb lock (kgdb_active)
827 kgdb_handle_exception(int evector
, int signo
, int ecode
, struct pt_regs
*regs
)
829 struct kgdb_state kgdb_var
;
830 struct kgdb_state
*ks
= &kgdb_var
;
833 if (arch_kgdb_ops
.enable_nmi
)
834 arch_kgdb_ops
.enable_nmi(0);
836 * Avoid entering the debugger if we were triggered due to an oops
837 * but panic_timeout indicates the system should automatically
838 * reboot on panic. We don't want to get stuck waiting for input
839 * on such systems, especially if its "just" an oops.
841 if (signo
!= SIGTRAP
&& panic_timeout
)
844 memset(ks
, 0, sizeof(struct kgdb_state
));
845 ks
->cpu
= raw_smp_processor_id();
846 ks
->ex_vector
= evector
;
848 ks
->err_code
= ecode
;
849 ks
->linux_regs
= regs
;
851 if (kgdb_reenter_check(ks
))
852 goto out
; /* Ouch, double exception ! */
853 if (kgdb_info
[ks
->cpu
].enter_kgdb
!= 0)
856 ret
= kgdb_cpu_enter(ks
, regs
, DCPU_WANT_MASTER
);
858 if (arch_kgdb_ops
.enable_nmi
)
859 arch_kgdb_ops
.enable_nmi(1);
862 NOKPROBE_SYMBOL(kgdb_handle_exception
);
865 * GDB places a breakpoint at this function to know dynamically loaded objects.
867 static int module_event(struct notifier_block
*self
, unsigned long val
,
873 static struct notifier_block dbg_module_load_nb
= {
874 .notifier_call
= module_event
,
877 int kgdb_nmicallback(int cpu
, void *regs
)
880 struct kgdb_state kgdb_var
;
881 struct kgdb_state
*ks
= &kgdb_var
;
883 kgdb_info
[cpu
].rounding_up
= false;
885 memset(ks
, 0, sizeof(struct kgdb_state
));
887 ks
->linux_regs
= regs
;
889 if (kgdb_info
[ks
->cpu
].enter_kgdb
== 0 &&
890 raw_spin_is_locked(&dbg_master_lock
)) {
891 kgdb_cpu_enter(ks
, regs
, DCPU_IS_SLAVE
);
897 NOKPROBE_SYMBOL(kgdb_nmicallback
);
899 int kgdb_nmicallin(int cpu
, int trapnr
, void *regs
, int err_code
,
900 atomic_t
*send_ready
)
903 if (!kgdb_io_ready(0) || !send_ready
)
906 if (kgdb_info
[cpu
].enter_kgdb
== 0) {
907 struct kgdb_state kgdb_var
;
908 struct kgdb_state
*ks
= &kgdb_var
;
910 memset(ks
, 0, sizeof(struct kgdb_state
));
912 ks
->ex_vector
= trapnr
;
914 ks
->err_code
= err_code
;
915 ks
->linux_regs
= regs
;
916 ks
->send_ready
= send_ready
;
917 kgdb_cpu_enter(ks
, regs
, DCPU_WANT_MASTER
);
923 NOKPROBE_SYMBOL(kgdb_nmicallin
);
925 static void kgdb_console_write(struct console
*co
, const char *s
,
930 /* If we're debugging, or KGDB has not connected, don't try
932 if (!kgdb_connected
|| atomic_read(&kgdb_active
) != -1 || dbg_kdb_mode
)
935 local_irq_save(flags
);
936 gdbstub_msg_write(s
, count
);
937 local_irq_restore(flags
);
940 static struct console kgdbcons
= {
942 .write
= kgdb_console_write
,
943 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
947 static int __init
opt_kgdb_con(char *str
)
951 if (kgdb_io_module_registered
&& !kgdb_con_registered
) {
952 register_console(&kgdbcons
);
953 kgdb_con_registered
= 1;
959 early_param("kgdbcon", opt_kgdb_con
);
961 #ifdef CONFIG_MAGIC_SYSRQ
962 static void sysrq_handle_dbg(int key
)
965 pr_crit("ERROR: No KGDB I/O module available\n");
968 if (!kgdb_connected
) {
969 #ifdef CONFIG_KGDB_KDB
971 pr_crit("KGDB or $3#33 for KDB\n");
973 pr_crit("Entering KGDB\n");
980 static const struct sysrq_key_op sysrq_dbg_op
= {
981 .handler
= sysrq_handle_dbg
,
982 .help_msg
= "debug(g)",
983 .action_msg
= "DEBUG",
987 void kgdb_panic(const char *msg
)
989 if (!kgdb_io_module_registered
)
993 * We don't want to get stuck waiting for input from user if
994 * "panic_timeout" indicates the system should automatically
1001 kdb_printf("PANIC: %s\n", msg
);
1006 static void kgdb_initial_breakpoint(void)
1008 kgdb_break_asap
= 0;
1010 pr_crit("Waiting for connection from remote gdb...\n");
1014 void __weak
kgdb_arch_late(void)
1018 void __init
dbg_late_init(void)
1020 dbg_is_early
= false;
1021 if (kgdb_io_module_registered
)
1023 kdb_init(KDB_INIT_FULL
);
1025 if (kgdb_io_module_registered
&& kgdb_break_asap
)
1026 kgdb_initial_breakpoint();
1030 dbg_notify_reboot(struct notifier_block
*this, unsigned long code
, void *x
)
1033 * Take the following action on reboot notify depending on value:
1034 * 1 == Enter debugger
1035 * 0 == [the default] detach debug client
1036 * -1 == Do nothing... and use this until the board resets
1038 switch (kgdbreboot
) {
1051 static struct notifier_block dbg_reboot_notifier
= {
1052 .notifier_call
= dbg_notify_reboot
,
1054 .priority
= INT_MAX
,
1057 static void kgdb_register_callbacks(void)
1059 if (!kgdb_io_module_registered
) {
1060 kgdb_io_module_registered
= 1;
1064 register_module_notifier(&dbg_module_load_nb
);
1065 register_reboot_notifier(&dbg_reboot_notifier
);
1066 #ifdef CONFIG_MAGIC_SYSRQ
1067 register_sysrq_key('g', &sysrq_dbg_op
);
1069 if (kgdb_use_con
&& !kgdb_con_registered
) {
1070 register_console(&kgdbcons
);
1071 kgdb_con_registered
= 1;
1076 static void kgdb_unregister_callbacks(void)
1079 * When this routine is called KGDB should unregister from
1080 * handlers and clean up, making sure it is not handling any
1081 * break exceptions at the time.
1083 if (kgdb_io_module_registered
) {
1084 kgdb_io_module_registered
= 0;
1085 unregister_reboot_notifier(&dbg_reboot_notifier
);
1086 unregister_module_notifier(&dbg_module_load_nb
);
1088 #ifdef CONFIG_MAGIC_SYSRQ
1089 unregister_sysrq_key('g', &sysrq_dbg_op
);
1091 if (kgdb_con_registered
) {
1092 unregister_console(&kgdbcons
);
1093 kgdb_con_registered
= 0;
1099 * kgdb_register_io_module - register KGDB IO module
1100 * @new_dbg_io_ops: the io ops vector
1102 * Register it with the KGDB core.
1104 int kgdb_register_io_module(struct kgdb_io
*new_dbg_io_ops
)
1106 struct kgdb_io
*old_dbg_io_ops
;
1109 spin_lock(&kgdb_registration_lock
);
1111 old_dbg_io_ops
= dbg_io_ops
;
1112 if (old_dbg_io_ops
) {
1113 if (!old_dbg_io_ops
->deinit
) {
1114 spin_unlock(&kgdb_registration_lock
);
1116 pr_err("KGDB I/O driver %s can't replace %s.\n",
1117 new_dbg_io_ops
->name
, old_dbg_io_ops
->name
);
1120 pr_info("Replacing I/O driver %s with %s\n",
1121 old_dbg_io_ops
->name
, new_dbg_io_ops
->name
);
1124 if (new_dbg_io_ops
->init
) {
1125 err
= new_dbg_io_ops
->init();
1127 spin_unlock(&kgdb_registration_lock
);
1132 dbg_io_ops
= new_dbg_io_ops
;
1134 spin_unlock(&kgdb_registration_lock
);
1136 if (old_dbg_io_ops
) {
1137 old_dbg_io_ops
->deinit();
1141 pr_info("Registered I/O driver %s\n", new_dbg_io_ops
->name
);
1144 kgdb_register_callbacks();
1146 if (kgdb_break_asap
&&
1147 (!dbg_is_early
|| IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG
)))
1148 kgdb_initial_breakpoint();
1152 EXPORT_SYMBOL_GPL(kgdb_register_io_module
);
1155 * kgdb_unregister_io_module - unregister KGDB IO module
1156 * @old_dbg_io_ops: the io ops vector
1158 * Unregister it with the KGDB core.
1160 void kgdb_unregister_io_module(struct kgdb_io
*old_dbg_io_ops
)
1162 BUG_ON(kgdb_connected
);
1165 * KGDB is no longer able to communicate out, so
1166 * unregister our callbacks and reset state.
1168 kgdb_unregister_callbacks();
1170 spin_lock(&kgdb_registration_lock
);
1172 WARN_ON_ONCE(dbg_io_ops
!= old_dbg_io_ops
);
1175 spin_unlock(&kgdb_registration_lock
);
1177 if (old_dbg_io_ops
->deinit
)
1178 old_dbg_io_ops
->deinit();
1180 pr_info("Unregistered I/O driver %s, debugger disabled\n",
1181 old_dbg_io_ops
->name
);
1183 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module
);
1185 int dbg_io_get_char(void)
1187 int ret
= dbg_io_ops
->read_char();
1188 if (ret
== NO_POLL_CHAR
)
1198 * kgdb_breakpoint - generate breakpoint exception
1200 * This function will generate a breakpoint exception. It is used at the
1201 * beginning of a program to sync up with a debugger and can be used
1202 * otherwise as a quick means to stop program execution and "break" into
1205 noinline
void kgdb_breakpoint(void)
1207 atomic_inc(&kgdb_setting_breakpoint
);
1208 wmb(); /* Sync point before breakpoint */
1209 arch_kgdb_breakpoint();
1210 wmb(); /* Sync point after breakpoint */
1211 atomic_dec(&kgdb_setting_breakpoint
);
1213 EXPORT_SYMBOL_GPL(kgdb_breakpoint
);
1215 static int __init
opt_kgdb_wait(char *str
)
1217 kgdb_break_asap
= 1;
1219 kdb_init(KDB_INIT_EARLY
);
1220 if (kgdb_io_module_registered
&&
1221 IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG
))
1222 kgdb_initial_breakpoint();
1227 early_param("kgdbwait", opt_kgdb_wait
);