| @@ -43,6 +43,7 @@ · 42 unmodified lines |
| 43 | 43 | #include <linux/sysrq.h> |
| 44 | 44 | #include <linux/init.h> |
| 45 | 45 | #include <linux/kgdb.h> |
| 46 | +#include <linux/kdb.h> |
| 46 | 47 | #include <linux/pid.h> |
| 47 | 48 | #include <linux/smp.h> |
| 48 | 49 | #include <linux/mm.h> |
| @@ -77,6 +78,11 @@ static DEFINE_SPINLOCK(kgdb_registration_lock); · 28 unmodified lines |
| 77 | 78 | static int kgdb_con_registered; |
| 78 | 79 | /* determine if kgdb console output should be used */ |
| 79 | 80 | static 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; |
| 80 | 86 | |
| 81 | 87 | static int __init opt_kgdb_con(char *str) |
| 82 | 88 | { |
| @@ -100,6 +106,7 @@ static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = { · 17 unmodified lines |
| 100 | 106 | * The CPU# of the active CPU, or -1 if none: |
| 101 | 107 | */ |
| 102 | 108 | atomic_t kgdb_active = ATOMIC_INIT(-1); |
| 109 | +EXPORT_SYMBOL_GPL(kgdb_active); |
| 103 | 110 | |
| 104 | 111 | /* |
| 105 | 112 | * 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) · 195 unmodified lines |
| 301 | 308 | return 0; |
| 302 | 309 | } |
| 303 | 310 | |
| 304 | | −static int kgdb_deactivate_sw_breakpoints(void) |
| 311 | +int dbg_deactivate_sw_breakpoints(void) |
| 305 | 312 | { |
| 306 | 313 | unsigned long addr; |
| 307 | 314 | int error; |
| @@ -395,8 +402,14 @@ static int kgdb_io_ready(int print_wait) · 87 unmodified lines |
| 395 | 402 | return 1; |
| 396 | 403 | if (atomic_read(&kgdb_setting_breakpoint)) |
| 397 | 404 | 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 |
| 399 | 410 | printk(KERN_CRIT "KGDB: Waiting for remote debugger\n"); |
| 411 | +#endif |
| 412 | + } |
| 400 | 413 | return 1; |
| 401 | 414 | } |
| 402 | 415 | |
| @@ -410,7 +423,7 @@ static int kgdb_reenter_check(struct kgdb_state *ks) · 7 unmodified lines |
| 410 | 423 | /* Panic on recursive debugger calls: */ |
| 411 | 424 | exception_level++; |
| 412 | 425 | addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); |
| 413 | | − kgdb_deactivate_sw_breakpoints(); |
| 426 | + dbg_deactivate_sw_breakpoints(); |
| 414 | 427 | |
| 415 | 428 | /* |
| 416 | 429 | * If the break point removed ok at the place exception |
| @@ -443,11 +456,24 @@ static int kgdb_reenter_check(struct kgdb_state *ks) · 26 unmodified lines |
| 443 | 456 | return 1; |
| 444 | 457 | } |
| 445 | 458 | |
| 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 | + |
| 446 | 472 | static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs) |
| 447 | 473 | { |
| 448 | 474 | unsigned long flags; |
| 449 | 475 | int sstep_tries = 100; |
| 450 | | − int error = 0; |
| 476 | + int error; |
| 451 | 477 | int i, cpu; |
| 452 | 478 | int trace_on = 0; |
| 453 | 479 | acquirelock: |
| @@ -460,6 +486,8 @@ acquirelock: · 6 unmodified lines |
| 460 | 486 | cpu = ks->cpu; |
| 461 | 487 | kgdb_info[cpu].debuggerinfo = regs; |
| 462 | 488 | kgdb_info[cpu].task = current; |
| 489 | + kgdb_info[cpu].ret_state = 0; |
| 490 | + kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT; |
| 463 | 491 | /* |
| 464 | 492 | * Make sure the above info reaches the primary CPU before |
| 465 | 493 | * our cpu_in_kgdb[] flag setting does: |
| @@ -471,7 +499,11 @@ acquirelock: · 5 unmodified lines |
| 471 | 499 | * master cpu and acquire the kgdb_active lock: |
| 472 | 500 | */ |
| 473 | 501 | 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) { |
| 475 | 507 | if (atomic_cmpxchg(&kgdb_active, -1, cpu) == cpu) |
| 476 | 508 | break; |
| 477 | 509 | } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) { |
| @@ -513,7 +545,7 @@ return_normal: · 35 unmodified lines |
| 513 | 545 | } |
| 514 | 546 | |
| 515 | 547 | if (!kgdb_io_ready(1)) { |
| 516 | | − error = 1; |
| 548 | + kgdb_info[cpu].ret_state = 1; |
| 517 | 549 | goto kgdb_restore; /* No I/O connection, resume the system */ |
| 518 | 550 | } |
| 519 | 551 | |
| @@ -548,7 +580,7 @@ return_normal: · 28 unmodified lines |
| 548 | 580 | * Wait for the other CPUs to be notified and be waiting for us: |
| 549 | 581 | */ |
| 550 | 582 | for_each_online_cpu(i) { |
| 551 | | − while (!atomic_read(&cpu_in_kgdb[i])) |
| 583 | + while (kgdb_do_roundup && !atomic_read(&cpu_in_kgdb[i])) |
| 552 | 584 | cpu_relax(); |
| 553 | 585 | } |
| 554 | 586 | |
| @@ -557,7 +589,7 @@ return_normal: · 2 unmodified lines |
| 557 | 589 | * in the debugger and all secondary CPUs are quiescent |
| 558 | 590 | */ |
| 559 | 591 | kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code); |
| 560 | | − kgdb_deactivate_sw_breakpoints(); |
| 592 | + dbg_deactivate_sw_breakpoints(); |
| 561 | 593 | kgdb_single_step = 0; |
| 562 | 594 | kgdb_contthread = current; |
| 563 | 595 | exception_level = 0; |
| @@ -565,8 +597,26 @@ return_normal: · 1 unmodified line |
| 565 | 597 | if (trace_on) |
| 566 | 598 | tracing_off(); |
| 567 | 599 | |
| 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 | + } |
| 570 | 620 | |
| 571 | 621 | /* Call the I/O driver's post_exception routine */ |
| 572 | 622 | if (dbg_io_ops->post_exception) |
| @@ -578,11 +628,16 @@ return_normal: · 5 unmodified lines |
| 578 | 628 | for (i = NR_CPUS-1; i >= 0; i--) |
| 579 | 629 | atomic_dec(&passive_cpu_wait[i]); |
| 580 | 630 | /* |
| 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. |
| 583 | 635 | */ |
| 584 | 636 | 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)) |
| 586 | 641 | cpu_relax(); |
| 587 | 642 | } |
| 588 | 643 | } |
| @@ -603,7 +658,7 @@ kgdb_restore: · 14 unmodified lines |
| 603 | 658 | clocksource_touch_watchdog(); |
| 604 | 659 | local_irq_restore(flags); |
| 605 | 660 | |
| 606 | | − return error; |
| 661 | + return kgdb_info[cpu].ret_state; |
| 607 | 662 | } |
| 608 | 663 | |
| 609 | 664 | /* |
| @@ -632,7 +687,8 @@ kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs) · 22 unmodified lines |
| 632 | 687 | return 0; /* Ouch, double exception ! */ |
| 633 | 688 | kgdb_info[ks->cpu].exception_state |= DCPU_WANT_MASTER; |
| 634 | 689 | 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); |
| 636 | 692 | return ret; |
| 637 | 693 | } |
| 638 | 694 | |
| @@ -665,7 +721,7 @@ static void kgdb_console_write(struct console *co, const char *s, · 26 unmodified lines |
| 665 | 721 | |
| 666 | 722 | /* If we're debugging, or KGDB has not connected, don't try |
| 667 | 723 | * and print. */ |
| 668 | | − if (!kgdb_connected || atomic_read(&kgdb_active) != -1) |
| 724 | + if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode) |
| 669 | 725 | return; |
| 670 | 726 | |
| 671 | 727 | local_irq_save(flags); |
| @@ -687,8 +743,14 @@ static void sysrq_handle_dbg(int key, struct tty_struct *tty) · 15 unmodified lines |
| 687 | 743 | printk(KERN_CRIT "ERROR: No KGDB I/O module available\n"); |
| 688 | 744 | return; |
| 689 | 745 | } |
| 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 |
| 691 | 751 | printk(KERN_CRIT "Entering KGDB\n"); |
| 752 | +#endif |
| 753 | + } |
| 692 | 754 | |
| 693 | 755 | kgdb_breakpoint(); |
| 694 | 756 | } |
| @@ -817,6 +879,16 @@ void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops) · 122 unmodified lines |
| 817 | 879 | } |
| 818 | 880 | EXPORT_SYMBOL_GPL(kgdb_unregister_io_module); |
| 819 | 881 | |
| 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 | + |
| 820 | 892 | /** |
| 821 | 893 | * kgdb_breakpoint - generate breakpoint exception |
| 822 | 894 | * |
| @@ -839,6 +911,7 @@ static int __init opt_kgdb_wait(char *str) · 16 unmodified lines |
| 839 | 911 | { |
| 840 | 912 | kgdb_break_asap = 1; |
| 841 | 913 | |
| 914 | + kdb_init(KDB_INIT_EARLY); |
| 842 | 915 | if (kgdb_io_module_registered) |
| 843 | 916 | kgdb_initial_breakpoint(); |
| 844 | 917 | |