1 /* 2 * Kernel Debug Core 3 * 4 * Maintainer: Jason Wessel <[email protected]> 5 * 6 * Copyright (C) 2000-2001 VERITAS Software Corporation. 7 * Copyright (C) 2002-2004 Timesys Corporation 8 * Copyright (C) 2003-2004 Amit S. Kale <[email protected]> 9 * Copyright (C) 2004 Pavel Machek <[email protected]> 10 * Copyright (C) 2004-2006 Tom Rini <[email protected]> 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 <[email protected]> 15 * 16 * Contributors at various stages not listed above: 17 * Jason Wessel ( [email protected] ) 18 * George Anzinger <[email protected]> 19 * Anurekh Saxena ([email protected]) 20 * Lake Stevens Instrument Division (Glenn Engel) 21 * Jim Kingdon, Cygnus Support. 22 * 23 * Original KGDB stub: David Grothe <[email protected]>, 24 * Tigran Aivazian <[email protected]> 25 * 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. 29 */ 30 31 #define pr_fmt(fmt) "KGDB: " fmt 32 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> 55 #include <linux/mm.h> 56 #include <linux/vmacache.h> 57 #include <linux/rcupdate.h> 58 #include <linux/irq.h> 59 60 #include <asm/cacheflush.h> 61 #include <asm/byteorder.h> 62 #include <linux/atomic.h> 63 64 #include "debug_core.h" 65 66 static int kgdb_break_asap; 67 68 struct debuggerinfo_struct kgdb_info[NR_CPUS]; 69 70 /* kgdb_connected - Is a host GDB connected to us? */ 71 int kgdb_connected; 72 EXPORT_SYMBOL_GPL(kgdb_connected); 73 74 /* All the KGDB handlers are installed */ 75 int kgdb_io_module_registered; 76 77 /* Guard for recursive entry */ 78 static int exception_level; 79 80 struct kgdb_io *dbg_io_ops; 81 static DEFINE_SPINLOCK(kgdb_registration_lock); 82 83 /* Action for the reboot notifiter, a global allow kdb to change it */ 84 static int kgdbreboot; 85 /* kgdb console driver is loaded */ 86 static int kgdb_con_registered; 87 /* determine if kgdb console output should be used */ 88 static int kgdb_use_con; 89 /* Flag for alternate operations for early debugging */ 90 bool dbg_is_early = true; 91 /* Next cpu to become the master debug core */ 92 int dbg_switch_cpu; 93 94 /* Use kdb or gdbserver mode */ 95 int dbg_kdb_mode = 1; 96 97 module_param(kgdb_use_con, int, 0644); 98 module_param(kgdbreboot, int, 0644); 99 100 /* 101 * Holds information about breakpoints in a kernel. These breakpoints are 102 * added and removed by gdb. 103 */ 104 static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = { 105 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED } 106 }; 107 108 /* 109 * The CPU# of the active CPU, or -1 if none: 110 */ 111 atomic_t kgdb_active = ATOMIC_INIT(-1); 112 EXPORT_SYMBOL_GPL(kgdb_active); 113 static DEFINE_RAW_SPINLOCK(dbg_master_lock); 114 static DEFINE_RAW_SPINLOCK(dbg_slave_lock); 115 116 /* 117 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early 118 * bootup code (which might not have percpu set up yet): 119 */ 120 static atomic_t masters_in_kgdb; 121 static atomic_t slaves_in_kgdb; 122 static atomic_t kgdb_break_tasklet_var; 123 atomic_t kgdb_setting_breakpoint; 124 125 struct task_struct *kgdb_usethread; 126 struct task_struct *kgdb_contthread; 127 128 int kgdb_single_step; 129 static pid_t kgdb_sstep_pid; 130 131 /* to keep track of the CPU which is doing the single stepping*/ 132 atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1); 133 134 /* 135 * If you are debugging a problem where roundup (the collection of 136 * all other CPUs) is a problem [this should be extremely rare], 137 * then use the nokgdbroundup option to avoid roundup. In that case 138 * the other CPUs might interfere with your debugging context, so 139 * use this with care: 140 */ 141 static int kgdb_do_roundup = 1; 142 143 static int __init opt_nokgdbroundup(char *str) 144 { 145 kgdb_do_roundup = 0; 146 147 return 0; 148 } 149 150 early_param("nokgdbroundup", opt_nokgdbroundup); 151 152 /* 153 * Finally, some KGDB code :-) 154 */ 155 156 /* 157 * Weak aliases for breakpoint management, 158 * can be overriden by architectures when needed: 159 */ 160 int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) 161 { 162 int err; 163 164 err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr, 165 BREAK_INSTR_SIZE); 166 if (err) 167 return err; 168 err = copy_to_kernel_nofault((char *)bpt->bpt_addr, 169 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); 170 return err; 171 } 172 173 int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt) 174 { 175 return copy_to_kernel_nofault((char *)bpt->bpt_addr, 176 (char *)bpt->saved_instr, BREAK_INSTR_SIZE); 177 } 178 179 int __weak kgdb_validate_break_address(unsigned long addr) 180 { 181 struct kgdb_bkpt tmp; 182 int err; 183 /* Validate setting the breakpoint and then removing it. If the 184 * remove fails, the kernel needs to emit a bad message because we 185 * are deep trouble not being able to put things back the way we 186 * found them. 187 */ 188 tmp.bpt_addr = addr; 189 err = kgdb_arch_set_breakpoint(&tmp); 190 if (err) 191 return err; 192 err = kgdb_arch_remove_breakpoint(&tmp); 193 if (err) 194 pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n", 195 addr); 196 return err; 197 } 198 199 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs) 200 { 201 return instruction_pointer(regs); 202 } 203 204 int __weak kgdb_arch_init(void) 205 { 206 return 0; 207 } 208 209 int __weak kgdb_skipexception(int exception, struct pt_regs *regs) 210 { 211 return 0; 212 } 213 214 #ifdef CONFIG_SMP 215 216 /* 217 * Default (weak) implementation for kgdb_roundup_cpus 218 */ 219 220 static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd); 221 222 void __weak kgdb_call_nmi_hook(void *ignored) 223 { 224 /* 225 * NOTE: get_irq_regs() is supposed to get the registers from 226 * before the IPI interrupt happened and so is supposed to 227 * show where the processor was. In some situations it's 228 * possible we might be called without an IPI, so it might be 229 * safer to figure out how to make kgdb_breakpoint() work 230 * properly here. 231 */ 232 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs()); 233 } 234 235 void __weak kgdb_roundup_cpus(void) 236 { 237 call_single_data_t *csd; 238 int this_cpu = raw_smp_processor_id(); 239 int cpu; 240 int ret; 241 242 for_each_online_cpu(cpu) { 243 /* No need to roundup ourselves */ 244 if (cpu == this_cpu) 245 continue; 246 247 csd = &per_cpu(kgdb_roundup_csd, cpu); 248 249 /* 250 * If it didn't round up last time, don't try again 251 * since smp_call_function_single_async() will block. 252 * 253 * If rounding_up is false then we know that the 254 * previous call must have at least started and that 255 * means smp_call_function_single_async() won't block. 256 */ 257 if (kgdb_info[cpu].rounding_up) 258 continue; 259 kgdb_info[cpu].rounding_up = true; 260 261 csd->func = kgdb_call_nmi_hook; 262 ret = smp_call_function_single_async(cpu, csd); 263 if (ret) 264 kgdb_info[cpu].rounding_up = false; 265 } 266 } 267 268 #endif 269 270 /* 271 * Some architectures need cache flushes when we set/clear a 272 * breakpoint: 273 */ 274 static void kgdb_flush_swbreak_addr(unsigned long addr) 275 { 276 if (!CACHE_FLUSH_IS_SAFE) 277 return; 278 279 if (current->mm) { 280 int i; 281 282 for (i = 0; i < VMACACHE_SIZE; i++) { 283 if (!current->vmacache.vmas[i]) 284 continue; 285 flush_cache_range(current->vmacache.vmas[i], 286 addr, addr + BREAK_INSTR_SIZE); 287 } 288 } 289 290 /* Force flush instruction cache if it was outside the mm */ 291 flush_icache_range(addr, addr + BREAK_INSTR_SIZE); 292 } 293 294 /* 295 * SW breakpoint management: 296 */ 297 int dbg_activate_sw_breakpoints(void) 298 { 299 int error; 300 int ret = 0; 301 int i; 302 303 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 304 if (kgdb_break[i].state != BP_SET) 305 continue; 306 307 error = kgdb_arch_set_breakpoint(&kgdb_break[i]); 308 if (error) { 309 ret = error; 310 pr_info("BP install failed: %lx\n", 311 kgdb_break[i].bpt_addr); 312 continue; 313 } 314 315 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr); 316 kgdb_break[i].state = BP_ACTIVE; 317 } 318 return ret; 319 } 320 321 int dbg_set_sw_break(unsigned long addr) 322 { 323 int err = kgdb_validate_break_address(addr); 324 int breakno = -1; 325 int i; 326 327 if (err) 328 return err; 329 330 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 331 if ((kgdb_break[i].state == BP_SET) && 332 (kgdb_break[i].bpt_addr == addr)) 333 return -EEXIST; 334 } 335 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 336 if (kgdb_break[i].state == BP_REMOVED && 337 kgdb_break[i].bpt_addr == addr) { 338 breakno = i; 339 break; 340 } 341 } 342 343 if (breakno == -1) { 344 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 345 if (kgdb_break[i].state == BP_UNDEFINED) { 346 breakno = i; 347 break; 348 } 349 } 350 } 351 352 if (breakno == -1) 353 return -E2BIG; 354 355 kgdb_break[breakno].state = BP_SET; 356 kgdb_break[breakno].type = BP_BREAKPOINT; 357 kgdb_break[breakno].bpt_addr = addr; 358 359 return 0; 360 } 361 362 int dbg_deactivate_sw_breakpoints(void) 363 { 364 int error; 365 int ret = 0; 366 int i; 367 368 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 369 if (kgdb_break[i].state != BP_ACTIVE) 370 continue; 371 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]); 372 if (error) { 373 pr_info("BP remove failed: %lx\n", 374 kgdb_break[i].bpt_addr); 375 ret = error; 376 } 377 378 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr); 379 kgdb_break[i].state = BP_SET; 380 } 381 return ret; 382 } 383 384 int dbg_remove_sw_break(unsigned long addr) 385 { 386 int i; 387 388 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 389 if ((kgdb_break[i].state == BP_SET) && 390 (kgdb_break[i].bpt_addr == addr)) { 391 kgdb_break[i].state = BP_REMOVED; 392 return 0; 393 } 394 } 395 return -ENOENT; 396 } 397 398 int kgdb_isremovedbreak(unsigned long addr) 399 { 400 int i; 401 402 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 403 if ((kgdb_break[i].state == BP_REMOVED) && 404 (kgdb_break[i].bpt_addr == addr)) 405 return 1; 406 } 407 return 0; 408 } 409 410 int kgdb_has_hit_break(unsigned long addr) 411 { 412 int i; 413 414 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 415 if (kgdb_break[i].state == BP_ACTIVE && 416 kgdb_break[i].bpt_addr == addr) 417 return 1; 418 } 419 return 0; 420 } 421 422 int dbg_remove_all_break(void) 423 { 424 int error; 425 int i; 426 427 /* Clear memory breakpoints. */ 428 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 429 if (kgdb_break[i].state != BP_ACTIVE) 430 goto setundefined; 431 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]); 432 if (error) 433 pr_err("breakpoint remove failed: %lx\n", 434 kgdb_break[i].bpt_addr); 435 setundefined: 436 kgdb_break[i].state = BP_UNDEFINED; 437 } 438 439 /* Clear hardware breakpoints. */ 440 if (arch_kgdb_ops.remove_all_hw_break) 441 arch_kgdb_ops.remove_all_hw_break(); 442 443 return 0; 444 } 445 446 #ifdef CONFIG_KGDB_KDB 447 void kdb_dump_stack_on_cpu(int cpu) 448 { 449 if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) { 450 dump_stack(); 451 return; 452 } 453 454 if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) { 455 kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n", 456 cpu); 457 return; 458 } 459 460 /* 461 * In general, architectures don't support dumping the stack of a 462 * "running" process that's not the current one. From the point of 463 * view of the Linux, kernel processes that are looping in the kgdb 464 * slave loop are still "running". There's also no API (that actually 465 * works across all architectures) that can do a stack crawl based 466 * on registers passed as a parameter. 467 * 468 * Solve this conundrum by asking slave CPUs to do the backtrace 469 * themselves. 470 */ 471 kgdb_info[cpu].exception_state |= DCPU_WANT_BT; 472 while (kgdb_info[cpu].exception_state & DCPU_WANT_BT) 473 cpu_relax(); 474 } 475 #endif 476 477 /* 478 * Return true if there is a valid kgdb I/O module. Also if no 479 * debugger is attached a message can be printed to the console about 480 * waiting for the debugger to attach. 481 * 482 * The print_wait argument is only to be true when called from inside 483 * the core kgdb_handle_exception, because it will wait for the 484 * debugger to attach. 485 */ 486 static int kgdb_io_ready(int print_wait) 487 { 488 if (!dbg_io_ops) 489 return 0; 490 if (kgdb_connected) 491 return 1; 492 if (atomic_read(&kgdb_setting_breakpoint)) 493 return 1; 494 if (print_wait) { 495 #ifdef CONFIG_KGDB_KDB 496 if (!dbg_kdb_mode) 497 pr_crit("waiting... or $3#33 for KDB\n"); 498 #else 499 pr_crit("Waiting for remote debugger\n"); 500 #endif 501 } 502 return 1; 503 } 504 505 static int kgdb_reenter_check(struct kgdb_state *ks) 506 { 507 unsigned long addr; 508 509 if (atomic_read(&kgdb_active) != raw_smp_processor_id()) 510 return 0; 511 512 /* Panic on recursive debugger calls: */ 513 exception_level++; 514 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); 515 dbg_deactivate_sw_breakpoints(); 516 517 /* 518 * If the break point removed ok at the place exception 519 * occurred, try to recover and print a warning to the end 520 * user because the user planted a breakpoint in a place that 521 * KGDB needs in order to function. 522 */ 523 if (dbg_remove_sw_break(addr) == 0) { 524 exception_level = 0; 525 kgdb_skipexception(ks->ex_vector, ks->linux_regs); 526 dbg_activate_sw_breakpoints(); 527 pr_crit("re-enter error: breakpoint removed %lx\n", addr); 528 WARN_ON_ONCE(1); 529 530 return 1; 531 } 532 dbg_remove_all_break(); 533 kgdb_skipexception(ks->ex_vector, ks->linux_regs); 534 535 if (exception_level > 1) { 536 dump_stack(); 537 kgdb_io_module_registered = false; 538 panic("Recursive entry to debugger"); 539 } 540 541 pr_crit("re-enter exception: ALL breakpoints killed\n"); 542 #ifdef CONFIG_KGDB_KDB 543 /* Allow kdb to debug itself one level */ 544 return 0; 545 #endif 546 dump_stack(); 547 panic("Recursive entry to debugger"); 548 549 return 1; 550 } 551 552 static void dbg_touch_watchdogs(void) 553 { 554 touch_softlockup_watchdog_sync(); 555 clocksource_touch_watchdog(); 556 rcu_cpu_stall_reset(); 557 } 558 559 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs, 560 int exception_state) 561 { 562 unsigned long flags; 563 int sstep_tries = 100; 564 int error; 565 int cpu; 566 int trace_on = 0; 567 int online_cpus = num_online_cpus(); 568 u64 time_left; 569 570 kgdb_info[ks->cpu].enter_kgdb++; 571 kgdb_info[ks->cpu].exception_state |= exception_state; 572 573 if (exception_state == DCPU_WANT_MASTER) 574 atomic_inc(&masters_in_kgdb); 575 else 576 atomic_inc(&slaves_in_kgdb); 577 578 if (arch_kgdb_ops.disable_hw_break) 579 arch_kgdb_ops.disable_hw_break(regs); 580 581 acquirelock: 582 rcu_read_lock(); 583 /* 584 * Interrupts will be restored by the 'trap return' code, except when 585 * single stepping. 586 */ 587 local_irq_save(flags); 588 589 cpu = ks->cpu; 590 kgdb_info[cpu].debuggerinfo = regs; 591 kgdb_info[cpu].task = current; 592 kgdb_info[cpu].ret_state = 0; 593 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT; 594 595 /* Make sure the above info reaches the primary CPU */ 596 smp_mb(); 597 598 if (exception_level == 1) { 599 if (raw_spin_trylock(&dbg_master_lock)) 600 atomic_xchg(&kgdb_active, cpu); 601 goto cpu_master_loop; 602 } 603 604 /* 605 * CPU will loop if it is a slave or request to become a kgdb 606 * master cpu and acquire the kgdb_active lock: 607 */ 608 while (1) { 609 cpu_loop: 610 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) { 611 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER; 612 goto cpu_master_loop; 613 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) { 614 if (raw_spin_trylock(&dbg_master_lock)) { 615 atomic_xchg(&kgdb_active, cpu); 616 break; 617 } 618 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) { 619 dump_stack(); 620 kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT; 621 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) { 622 if (!raw_spin_is_locked(&dbg_slave_lock)) 623 goto return_normal; 624 } else { 625 return_normal: 626 /* Return to normal operation by executing any 627 * hw breakpoint fixup. 628 */ 629 if (arch_kgdb_ops.correct_hw_break) 630 arch_kgdb_ops.correct_hw_break(); 631 if (trace_on) 632 tracing_on(); 633 kgdb_info[cpu].debuggerinfo = NULL; 634 kgdb_info[cpu].task = NULL; 635 kgdb_info[cpu].exception_state &= 636 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE); 637 kgdb_info[cpu].enter_kgdb--; 638 smp_mb__before_atomic(); 639 atomic_dec(&slaves_in_kgdb); 640 dbg_touch_watchdogs(); 641 local_irq_restore(flags); 642 rcu_read_unlock(); 643 return 0; 644 } 645 cpu_relax(); 646 } 647 648 /* 649 * For single stepping, try to only enter on the processor 650 * that was single stepping. To guard against a deadlock, the 651 * kernel will only try for the value of sstep_tries before 652 * giving up and continuing on. 653 */ 654 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 && 655 (kgdb_info[cpu].task && 656 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) { 657 atomic_set(&kgdb_active, -1); 658 raw_spin_unlock(&dbg_master_lock); 659 dbg_touch_watchdogs(); 660 local_irq_restore(flags); 661 rcu_read_unlock(); 662 663 goto acquirelock; 664 } 665 666 if (!kgdb_io_ready(1)) { 667 kgdb_info[cpu].ret_state = 1; 668 goto kgdb_restore; /* No I/O connection, resume the system */ 669 } 670 671 /* 672 * Don't enter if we have hit a removed breakpoint. 673 */ 674 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs)) 675 goto kgdb_restore; 676 677 atomic_inc(&ignore_console_lock_warning); 678 679 /* Call the I/O driver's pre_exception routine */ 680 if (dbg_io_ops->pre_exception) 681 dbg_io_ops->pre_exception(); 682 683 /* 684 * Get the passive CPU lock which will hold all the non-primary 685 * CPU in a spin state while the debugger is active 686 */ 687 if (!kgdb_single_step) 688 raw_spin_lock(&dbg_slave_lock); 689 690 #ifdef CONFIG_SMP 691 /* If send_ready set, slaves are already waiting */ 692 if (ks->send_ready) 693 atomic_set(ks->send_ready, 1); 694 695 /* Signal the other CPUs to enter kgdb_wait() */ 696 else if ((!kgdb_single_step) && kgdb_do_roundup) 697 kgdb_roundup_cpus(); 698 #endif 699 700 /* 701 * Wait for the other CPUs to be notified and be waiting for us: 702 */ 703 time_left = MSEC_PER_SEC; 704 while (kgdb_do_roundup && --time_left && 705 (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) != 706 online_cpus) 707 udelay(1000); 708 if (!time_left) 709 pr_crit("Timed out waiting for secondary CPUs.\n"); 710 711 /* 712 * At this point the primary processor is completely 713 * in the debugger and all secondary CPUs are quiescent 714 */ 715 dbg_deactivate_sw_breakpoints(); 716 kgdb_single_step = 0; 717 kgdb_contthread = current; 718 exception_level = 0; 719 trace_on = tracing_is_on(); 720 if (trace_on) 721 tracing_off(); 722 723 while (1) { 724 cpu_master_loop: 725 if (dbg_kdb_mode) { 726 kgdb_connected = 1; 727 error = kdb_stub(ks); 728 if (error == -1) 729 continue; 730 kgdb_connected = 0; 731 } else { 732 error = gdb_serial_stub(ks); 733 } 734 735 if (error == DBG_PASS_EVENT) { 736 dbg_kdb_mode = !dbg_kdb_mode; 737 } else if (error == DBG_SWITCH_CPU_EVENT) { 738 kgdb_info[dbg_switch_cpu].exception_state |= 739 DCPU_NEXT_MASTER; 740 goto cpu_loop; 741 } else { 742 kgdb_info[cpu].ret_state = error; 743 break; 744 } 745 } 746 747 /* Call the I/O driver's post_exception routine */ 748 if (dbg_io_ops->post_exception) 749 dbg_io_ops->post_exception(); 750 751 atomic_dec(&ignore_console_lock_warning); 752 753 if (!kgdb_single_step) { 754 raw_spin_unlock(&dbg_slave_lock); 755 /* Wait till all the CPUs have quit from the debugger. */ 756 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb)) 757 cpu_relax(); 758 } 759 760 kgdb_restore: 761 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) { 762 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step); 763 if (kgdb_info[sstep_cpu].task) 764 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid; 765 else 766 kgdb_sstep_pid = 0; 767 } 768 if (arch_kgdb_ops.correct_hw_break) 769 arch_kgdb_ops.correct_hw_break(); 770 if (trace_on) 771 tracing_on(); 772 773 kgdb_info[cpu].debuggerinfo = NULL; 774 kgdb_info[cpu].task = NULL; 775 kgdb_info[cpu].exception_state &= 776 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE); 777 kgdb_info[cpu].enter_kgdb--; 778 smp_mb__before_atomic(); 779 atomic_dec(&masters_in_kgdb); 780 /* Free kgdb_active */ 781 atomic_set(&kgdb_active, -1); 782 raw_spin_unlock(&dbg_master_lock); 783 dbg_touch_watchdogs(); 784 local_irq_restore(flags); 785 rcu_read_unlock(); 786 787 return kgdb_info[cpu].ret_state; 788 } 789 790 /* 791 * kgdb_handle_exception() - main entry point from a kernel exception 792 * 793 * Locking hierarchy: 794 * interface locks, if any (begin_session) 795 * kgdb lock (kgdb_active) 796 */ 797 int 798 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs) 799 { 800 struct kgdb_state kgdb_var; 801 struct kgdb_state *ks = &kgdb_var; 802 int ret = 0; 803 804 if (arch_kgdb_ops.enable_nmi) 805 arch_kgdb_ops.enable_nmi(0); 806 /* 807 * Avoid entering the debugger if we were triggered due to an oops 808 * but panic_timeout indicates the system should automatically 809 * reboot on panic. We don't want to get stuck waiting for input 810 * on such systems, especially if its "just" an oops. 811 */ 812 if (signo != SIGTRAP && panic_timeout) 813 return 1; 814 815 memset(ks, 0, sizeof(struct kgdb_state)); 816 ks->cpu = raw_smp_processor_id(); 817 ks->ex_vector = evector; 818 ks->signo = signo; 819 ks->err_code = ecode; 820 ks->linux_regs = regs; 821 822 if (kgdb_reenter_check(ks)) 823 goto out; /* Ouch, double exception ! */ 824 if (kgdb_info[ks->cpu].enter_kgdb != 0) 825 goto out; 826 827 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER); 828 out: 829 if (arch_kgdb_ops.enable_nmi) 830 arch_kgdb_ops.enable_nmi(1); 831 return ret; 832 } 833 834 /* 835 * GDB places a breakpoint at this function to know dynamically loaded objects. 836 */ 837 static int module_event(struct notifier_block *self, unsigned long val, 838 void *data) 839 { 840 return 0; 841 } 842 843 static struct notifier_block dbg_module_load_nb = { 844 .notifier_call = module_event, 845 }; 846 847 int kgdb_nmicallback(int cpu, void *regs) 848 { 849 #ifdef CONFIG_SMP 850 struct kgdb_state kgdb_var; 851 struct kgdb_state *ks = &kgdb_var; 852 853 kgdb_info[cpu].rounding_up = false; 854 855 memset(ks, 0, sizeof(struct kgdb_state)); 856 ks->cpu = cpu; 857 ks->linux_regs = regs; 858 859 if (kgdb_info[ks->cpu].enter_kgdb == 0 && 860 raw_spin_is_locked(&dbg_master_lock)) { 861 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE); 862 return 0; 863 } 864 #endif 865 return 1; 866 } 867 868 int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code, 869 atomic_t *send_ready) 870 { 871 #ifdef CONFIG_SMP 872 if (!kgdb_io_ready(0) || !send_ready) 873 return 1; 874 875 if (kgdb_info[cpu].enter_kgdb == 0) { 876 struct kgdb_state kgdb_var; 877 struct kgdb_state *ks = &kgdb_var; 878 879 memset(ks, 0, sizeof(struct kgdb_state)); 880 ks->cpu = cpu; 881 ks->ex_vector = trapnr; 882 ks->signo = SIGTRAP; 883 ks->err_code = err_code; 884 ks->linux_regs = regs; 885 ks->send_ready = send_ready; 886 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER); 887 return 0; 888 } 889 #endif 890 return 1; 891 } 892 893 static void kgdb_console_write(struct console *co, const char *s, 894 unsigned count) 895 { 896 unsigned long flags; 897 898 /* If we're debugging, or KGDB has not connected, don't try 899 * and print. */ 900 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode) 901 return; 902 903 local_irq_save(flags); 904 gdbstub_msg_write(s, count); 905 local_irq_restore(flags); 906 } 907 908 static struct console kgdbcons = { 909 .name = "kgdb", 910 .write = kgdb_console_write, 911 .flags = CON_PRINTBUFFER | CON_ENABLED, 912 .index = -1, 913 }; 914 915 static int __init opt_kgdb_con(char *str) 916 { 917 kgdb_use_con = 1; 918 919 if (kgdb_io_module_registered && !kgdb_con_registered) { 920 register_console(&kgdbcons); 921 kgdb_con_registered = 1; 922 } 923 924 return 0; 925 } 926 927 early_param("kgdbcon", opt_kgdb_con); 928 929 #ifdef CONFIG_MAGIC_SYSRQ 930 static void sysrq_handle_dbg(int key) 931 { 932 if (!dbg_io_ops) { 933 pr_crit("ERROR: No KGDB I/O module available\n"); 934 return; 935 } 936 if (!kgdb_connected) { 937 #ifdef CONFIG_KGDB_KDB 938 if (!dbg_kdb_mode) 939 pr_crit("KGDB or $3#33 for KDB\n"); 940 #else 941 pr_crit("Entering KGDB\n"); 942 #endif 943 } 944 945 kgdb_breakpoint(); 946 } 947 948 static const struct sysrq_key_op sysrq_dbg_op = { 949 .handler = sysrq_handle_dbg, 950 .help_msg = "debug(g)", 951 .action_msg = "DEBUG", 952 }; 953 #endif 954 955 void kgdb_panic(const char *msg) 956 { 957 if (!kgdb_io_module_registered) 958 return; 959 960 /* 961 * We don't want to get stuck waiting for input from user if 962 * "panic_timeout" indicates the system should automatically 963 * reboot on panic. 964 */ 965 if (panic_timeout) 966 return; 967 968 if (dbg_kdb_mode) 969 kdb_printf("PANIC: %s\n", msg); 970 971 kgdb_breakpoint(); 972 } 973 974 static void kgdb_initial_breakpoint(void) 975 { 976 kgdb_break_asap = 0; 977 978 pr_crit("Waiting for connection from remote gdb...\n"); 979 kgdb_breakpoint(); 980 } 981 982 void __weak kgdb_arch_late(void) 983 { 984 } 985 986 void __init dbg_late_init(void) 987 { 988 dbg_is_early = false; 989 if (kgdb_io_module_registered) 990 kgdb_arch_late(); 991 kdb_init(KDB_INIT_FULL); 992 993 if (kgdb_io_module_registered && kgdb_break_asap) 994 kgdb_initial_breakpoint(); 995 } 996 997 static int 998 dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x) 999 { 1000 /* 1001 * Take the following action on reboot notify depending on value: 1002 * 1 == Enter debugger 1003 * 0 == [the default] detatch debug client 1004 * -1 == Do nothing... and use this until the board resets 1005 */ 1006 switch (kgdbreboot) { 1007 case 1: 1008 kgdb_breakpoint(); 1009 case -1: 1010 goto done; 1011 } 1012 if (!dbg_kdb_mode) 1013 gdbstub_exit(code); 1014 done: 1015 return NOTIFY_DONE; 1016 } 1017 1018 static struct notifier_block dbg_reboot_notifier = { 1019 .notifier_call = dbg_notify_reboot, 1020 .next = NULL, 1021 .priority = INT_MAX, 1022 }; 1023 1024 static void kgdb_register_callbacks(void) 1025 { 1026 if (!kgdb_io_module_registered) { 1027 kgdb_io_module_registered = 1; 1028 kgdb_arch_init(); 1029 if (!dbg_is_early) 1030 kgdb_arch_late(); 1031 register_module_notifier(&dbg_module_load_nb); 1032 register_reboot_notifier(&dbg_reboot_notifier); 1033 #ifdef CONFIG_MAGIC_SYSRQ 1034 register_sysrq_key('g', &sysrq_dbg_op); 1035 #endif 1036 if (kgdb_use_con && !kgdb_con_registered) { 1037 register_console(&kgdbcons); 1038 kgdb_con_registered = 1; 1039 } 1040 } 1041 } 1042 1043 static void kgdb_unregister_callbacks(void) 1044 { 1045 /* 1046 * When this routine is called KGDB should unregister from 1047 * handlers and clean up, making sure it is not handling any 1048 * break exceptions at the time. 1049 */ 1050 if (kgdb_io_module_registered) { 1051 kgdb_io_module_registered = 0; 1052 unregister_reboot_notifier(&dbg_reboot_notifier); 1053 unregister_module_notifier(&dbg_module_load_nb); 1054 kgdb_arch_exit(); 1055 #ifdef CONFIG_MAGIC_SYSRQ 1056 unregister_sysrq_key('g', &sysrq_dbg_op); 1057 #endif 1058 if (kgdb_con_registered) { 1059 unregister_console(&kgdbcons); 1060 kgdb_con_registered = 0; 1061 } 1062 } 1063 } 1064 1065 /* 1066 * There are times a tasklet needs to be used vs a compiled in 1067 * break point so as to cause an exception outside a kgdb I/O module, 1068 * such as is the case with kgdboe, where calling a breakpoint in the 1069 * I/O driver itself would be fatal. 1070 */ 1071 static void kgdb_tasklet_bpt(unsigned long ing) 1072 { 1073 kgdb_breakpoint(); 1074 atomic_set(&kgdb_break_tasklet_var, 0); 1075 } 1076 1077 static DECLARE_TASKLET_OLD(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt); 1078 1079 void kgdb_schedule_breakpoint(void) 1080 { 1081 if (atomic_read(&kgdb_break_tasklet_var) || 1082 atomic_read(&kgdb_active) != -1 || 1083 atomic_read(&kgdb_setting_breakpoint)) 1084 return; 1085 atomic_inc(&kgdb_break_tasklet_var); 1086 tasklet_schedule(&kgdb_tasklet_breakpoint); 1087 } 1088 EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint); 1089 1090 /** 1091 * kgdb_register_io_module - register KGDB IO module 1092 * @new_dbg_io_ops: the io ops vector 1093 * 1094 * Register it with the KGDB core. 1095 */ 1096 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops) 1097 { 1098 struct kgdb_io *old_dbg_io_ops; 1099 int err; 1100 1101 spin_lock(&kgdb_registration_lock); 1102 1103 old_dbg_io_ops = dbg_io_ops; 1104 if (old_dbg_io_ops) { 1105 if (!old_dbg_io_ops->deinit) { 1106 spin_unlock(&kgdb_registration_lock); 1107 1108 pr_err("KGDB I/O driver %s can't replace %s.\n", 1109 new_dbg_io_ops->name, old_dbg_io_ops->name); 1110 return -EBUSY; 1111 } 1112 pr_info("Replacing I/O driver %s with %s\n", 1113 old_dbg_io_ops->name, new_dbg_io_ops->name); 1114 } 1115 1116 if (new_dbg_io_ops->init) { 1117 err = new_dbg_io_ops->init(); 1118 if (err) { 1119 spin_unlock(&kgdb_registration_lock); 1120 return err; 1121 } 1122 } 1123 1124 dbg_io_ops = new_dbg_io_ops; 1125 1126 spin_unlock(&kgdb_registration_lock); 1127 1128 if (old_dbg_io_ops) { 1129 old_dbg_io_ops->deinit(); 1130 return 0; 1131 } 1132 1133 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name); 1134 1135 /* Arm KGDB now. */ 1136 kgdb_register_callbacks(); 1137 1138 if (kgdb_break_asap && 1139 (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))) 1140 kgdb_initial_breakpoint(); 1141 1142 return 0; 1143 } 1144 EXPORT_SYMBOL_GPL(kgdb_register_io_module); 1145 1146 /** 1147 * kkgdb_unregister_io_module - unregister KGDB IO module 1148 * @old_dbg_io_ops: the io ops vector 1149 * 1150 * Unregister it with the KGDB core. 1151 */ 1152 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops) 1153 { 1154 BUG_ON(kgdb_connected); 1155 1156 /* 1157 * KGDB is no longer able to communicate out, so 1158 * unregister our callbacks and reset state. 1159 */ 1160 kgdb_unregister_callbacks(); 1161 1162 spin_lock(&kgdb_registration_lock); 1163 1164 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops); 1165 dbg_io_ops = NULL; 1166 1167 spin_unlock(&kgdb_registration_lock); 1168 1169 if (old_dbg_io_ops->deinit) 1170 old_dbg_io_ops->deinit(); 1171 1172 pr_info("Unregistered I/O driver %s, debugger disabled\n", 1173 old_dbg_io_ops->name); 1174 } 1175 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module); 1176 1177 int dbg_io_get_char(void) 1178 { 1179 int ret = dbg_io_ops->read_char(); 1180 if (ret == NO_POLL_CHAR) 1181 return -1; 1182 if (!dbg_kdb_mode) 1183 return ret; 1184 if (ret == 127) 1185 return 8; 1186 return ret; 1187 } 1188 1189 /** 1190 * kgdb_breakpoint - generate breakpoint exception 1191 * 1192 * This function will generate a breakpoint exception. It is used at the 1193 * beginning of a program to sync up with a debugger and can be used 1194 * otherwise as a quick means to stop program execution and "break" into 1195 * the debugger. 1196 */ 1197 noinline void kgdb_breakpoint(void) 1198 { 1199 atomic_inc(&kgdb_setting_breakpoint); 1200 wmb(); /* Sync point before breakpoint */ 1201 arch_kgdb_breakpoint(); 1202 wmb(); /* Sync point after breakpoint */ 1203 atomic_dec(&kgdb_setting_breakpoint); 1204 } 1205 EXPORT_SYMBOL_GPL(kgdb_breakpoint); 1206 1207 static int __init opt_kgdb_wait(char *str) 1208 { 1209 kgdb_break_asap = 1; 1210 1211 kdb_init(KDB_INIT_EARLY); 1212 if (kgdb_io_module_registered && 1213 IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)) 1214 kgdb_initial_breakpoint(); 1215 1216 return 0; 1217 } 1218 1219 early_param("kgdbwait", opt_kgdb_wait); 1220