4343#include <linux/sysrq.h>
4444#include <linux/init.h>
4545#include <linux/kgdb.h>
46+ #include <linux/kdb.h>
4647#include <linux/pid.h>
4748#include <linux/smp.h>
4849#include <linux/mm.h>
@@ -77,6 +78,11 @@ static DEFINE_SPINLOCK(kgdb_registration_lock);
7778static int kgdb_con_registered ;
7879/* determine if kgdb console output should be used */
7980static int kgdb_use_con ;
81+ /* Next cpu to become the master debug core */
82+ int dbg_switch_cpu ;
83+
84+ /* Use kdb or gdbserver mode */
85+ static int dbg_kdb_mode = 1 ;
8086
8187static int __init opt_kgdb_con (char * str )
8288{
@@ -100,6 +106,7 @@ static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
100106 * The CPU# of the active CPU, or -1 if none:
101107 */
102108atomic_t kgdb_active = ATOMIC_INIT (-1 );
109+ EXPORT_SYMBOL_GPL (kgdb_active );
103110
104111/*
105112 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
@@ -301,7 +308,7 @@ int dbg_set_sw_break(unsigned long addr)
301308 return 0 ;
302309}
303310
304- static int kgdb_deactivate_sw_breakpoints (void )
311+ int dbg_deactivate_sw_breakpoints (void )
305312{
306313 unsigned long addr ;
307314 int error ;
@@ -395,8 +402,14 @@ static int kgdb_io_ready(int print_wait)
395402 return 1 ;
396403 if (atomic_read (& kgdb_setting_breakpoint ))
397404 return 1 ;
398- if (print_wait )
405+ if (print_wait ) {
406+ #ifdef CONFIG_KGDB_KDB
407+ if (!dbg_kdb_mode )
408+ printk (KERN_CRIT "KGDB: waiting... or $3#33 for KDB\n" );
409+ #else
399410 printk (KERN_CRIT "KGDB: Waiting for remote debugger\n" );
411+ #endif
412+ }
400413 return 1 ;
401414}
402415
@@ -410,7 +423,7 @@ static int kgdb_reenter_check(struct kgdb_state *ks)
410423 /* Panic on recursive debugger calls: */
411424 exception_level ++ ;
412425 addr = kgdb_arch_pc (ks -> ex_vector , ks -> linux_regs );
413- kgdb_deactivate_sw_breakpoints ();
426+ dbg_deactivate_sw_breakpoints ();
414427
415428 /*
416429 * If the break point removed ok at the place exception
@@ -443,11 +456,24 @@ static int kgdb_reenter_check(struct kgdb_state *ks)
443456 return 1 ;
444457}
445458
459+ static void dbg_cpu_switch (int cpu , int next_cpu )
460+ {
461+ /* Mark the cpu we are switching away from as a slave when it
462+ * holds the kgdb_active token. This must be done so that the
463+ * that all the cpus wait in for the debug core will not enter
464+ * again as the master. */
465+ if (cpu == atomic_read (& kgdb_active )) {
466+ kgdb_info [cpu ].exception_state |= DCPU_IS_SLAVE ;
467+ kgdb_info [cpu ].exception_state &= ~DCPU_WANT_MASTER ;
468+ }
469+ kgdb_info [next_cpu ].exception_state |= DCPU_NEXT_MASTER ;
470+ }
471+
446472static int kgdb_cpu_enter (struct kgdb_state * ks , struct pt_regs * regs )
447473{
448474 unsigned long flags ;
449475 int sstep_tries = 100 ;
450- int error = 0 ;
476+ int error ;
451477 int i , cpu ;
452478 int trace_on = 0 ;
453479acquirelock :
@@ -460,6 +486,8 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
460486 cpu = ks -> cpu ;
461487 kgdb_info [cpu ].debuggerinfo = regs ;
462488 kgdb_info [cpu ].task = current ;
489+ kgdb_info [cpu ].ret_state = 0 ;
490+ kgdb_info [cpu ].irq_depth = hardirq_count () >> HARDIRQ_SHIFT ;
463491 /*
464492 * Make sure the above info reaches the primary CPU before
465493 * our cpu_in_kgdb[] flag setting does:
@@ -471,7 +499,11 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
471499 * master cpu and acquire the kgdb_active lock:
472500 */
473501 while (1 ) {
474- if (kgdb_info [cpu ].exception_state & DCPU_WANT_MASTER ) {
502+ cpu_loop :
503+ if (kgdb_info [cpu ].exception_state & DCPU_NEXT_MASTER ) {
504+ kgdb_info [cpu ].exception_state &= ~DCPU_NEXT_MASTER ;
505+ goto cpu_master_loop ;
506+ } else if (kgdb_info [cpu ].exception_state & DCPU_WANT_MASTER ) {
475507 if (atomic_cmpxchg (& kgdb_active , -1 , cpu ) == cpu )
476508 break ;
477509 } else if (kgdb_info [cpu ].exception_state & DCPU_IS_SLAVE ) {
@@ -513,7 +545,7 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
513545 }
514546
515547 if (!kgdb_io_ready (1 )) {
516- error = 1 ;
548+ kgdb_info [ cpu ]. ret_state = 1 ;
517549 goto kgdb_restore ; /* No I/O connection, resume the system */
518550 }
519551
@@ -548,7 +580,7 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
548580 * Wait for the other CPUs to be notified and be waiting for us:
549581 */
550582 for_each_online_cpu (i ) {
551- while (!atomic_read (& cpu_in_kgdb [i ]))
583+ while (kgdb_do_roundup && !atomic_read (& cpu_in_kgdb [i ]))
552584 cpu_relax ();
553585 }
554586
@@ -557,16 +589,34 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
557589 * in the debugger and all secondary CPUs are quiescent
558590 */
559591 kgdb_post_primary_code (ks -> linux_regs , ks -> ex_vector , ks -> err_code );
560- kgdb_deactivate_sw_breakpoints ();
592+ dbg_deactivate_sw_breakpoints ();
561593 kgdb_single_step = 0 ;
562594 kgdb_contthread = current ;
563595 exception_level = 0 ;
564596 trace_on = tracing_is_on ();
565597 if (trace_on )
566598 tracing_off ();
567599
568- /* Talk to debugger with gdbserial protocol */
569- error = gdb_serial_stub (ks );
600+ while (1 ) {
601+ cpu_master_loop :
602+ if (dbg_kdb_mode ) {
603+ kgdb_connected = 1 ;
604+ error = kdb_stub (ks );
605+ } else {
606+ error = gdb_serial_stub (ks );
607+ }
608+
609+ if (error == DBG_PASS_EVENT ) {
610+ dbg_kdb_mode = !dbg_kdb_mode ;
611+ kgdb_connected = 0 ;
612+ } else if (error == DBG_SWITCH_CPU_EVENT ) {
613+ dbg_cpu_switch (cpu , dbg_switch_cpu );
614+ goto cpu_loop ;
615+ } else {
616+ kgdb_info [cpu ].ret_state = error ;
617+ break ;
618+ }
619+ }
570620
571621 /* Call the I/O driver's post_exception routine */
572622 if (dbg_io_ops -> post_exception )
@@ -578,11 +628,16 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
578628 for (i = NR_CPUS - 1 ; i >= 0 ; i -- )
579629 atomic_dec (& passive_cpu_wait [i ]);
580630 /*
581- * Wait till all the CPUs have quit
582- * from the debugger.
631+ * Wait till all the CPUs have quit from the debugger,
632+ * but allow a CPU that hit an exception and is
633+ * waiting to become the master to remain in the debug
634+ * core.
583635 */
584636 for_each_online_cpu (i ) {
585- while (atomic_read (& cpu_in_kgdb [i ]))
637+ while (kgdb_do_roundup &&
638+ atomic_read (& cpu_in_kgdb [i ]) &&
639+ !(kgdb_info [i ].exception_state &
640+ DCPU_WANT_MASTER ))
586641 cpu_relax ();
587642 }
588643 }
@@ -603,7 +658,7 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
603658 clocksource_touch_watchdog ();
604659 local_irq_restore (flags );
605660
606- return error ;
661+ return kgdb_info [ cpu ]. ret_state ;
607662}
608663
609664/*
@@ -632,7 +687,8 @@ kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
632687 return 0 ; /* Ouch, double exception ! */
633688 kgdb_info [ks -> cpu ].exception_state |= DCPU_WANT_MASTER ;
634689 ret = kgdb_cpu_enter (ks , regs );
635- kgdb_info [ks -> cpu ].exception_state &= ~DCPU_WANT_MASTER ;
690+ kgdb_info [ks -> cpu ].exception_state &= ~(DCPU_WANT_MASTER |
691+ DCPU_IS_SLAVE );
636692 return ret ;
637693}
638694
@@ -665,7 +721,7 @@ static void kgdb_console_write(struct console *co, const char *s,
665721
666722 /* If we're debugging, or KGDB has not connected, don't try
667723 * and print. */
668- if (!kgdb_connected || atomic_read (& kgdb_active ) != -1 )
724+ if (!kgdb_connected || atomic_read (& kgdb_active ) != -1 || dbg_kdb_mode )
669725 return ;
670726
671727 local_irq_save (flags );
@@ -687,8 +743,14 @@ static void sysrq_handle_dbg(int key, struct tty_struct *tty)
687743 printk (KERN_CRIT "ERROR: No KGDB I/O module available\n" );
688744 return ;
689745 }
690- if (!kgdb_connected )
746+ if (!kgdb_connected ) {
747+ #ifdef CONFIG_KGDB_KDB
748+ if (!dbg_kdb_mode )
749+ printk (KERN_CRIT "KGDB or $3#33 for KDB\n" );
750+ #else
691751 printk (KERN_CRIT "Entering KGDB\n" );
752+ #endif
753+ }
692754
693755 kgdb_breakpoint ();
694756}
@@ -817,6 +879,16 @@ void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
817879}
818880EXPORT_SYMBOL_GPL (kgdb_unregister_io_module );
819881
882+ int dbg_io_get_char (void )
883+ {
884+ int ret = dbg_io_ops -> read_char ();
885+ if (!dbg_kdb_mode )
886+ return ret ;
887+ if (ret == 127 )
888+ return 8 ;
889+ return ret ;
890+ }
891+
820892/**
821893 * kgdb_breakpoint - generate breakpoint exception
822894 *
@@ -839,6 +911,7 @@ static int __init opt_kgdb_wait(char *str)
839911{
840912 kgdb_break_asap = 1 ;
841913
914+ kdb_init (KDB_INIT_EARLY );
842915 if (kgdb_io_module_registered )
843916 kgdb_initial_breakpoint ();
844917
0 commit comments