1 /*- 2 * Copyright (c) 1986, 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 39 * $FreeBSD: src/sys/kern/kern_shutdown.c,v 1.72.2.12 2002/02/21 19:15:10 dillon Exp $ 40 * $DragonFly: src/sys/kern/kern_shutdown.c,v 1.51 2007/05/08 02:31:42 dillon Exp $ 41 */ 42 43 #include "opt_ddb.h" 44 #include "opt_ddb_trace.h" 45 #include "opt_hw_wdog.h" 46 #include "opt_panic.h" 47 #include "opt_show_busybufs.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/eventhandler.h> 52 #include <sys/buf.h> 53 #include <sys/disklabel.h> 54 #include <sys/reboot.h> 55 #include <sys/proc.h> 56 #include <sys/vnode.h> 57 #include <sys/kernel.h> 58 #include <sys/kthread.h> 59 #include <sys/malloc.h> 60 #include <sys/mount.h> 61 #include <sys/queue.h> 62 #include <sys/sysctl.h> 63 #include <sys/vkernel.h> 64 #include <sys/conf.h> 65 #include <sys/sysproto.h> 66 #include <sys/device.h> 67 #include <sys/cons.h> 68 #include <sys/shm.h> 69 #include <sys/kern_syscall.h> 70 #include <vm/vm_map.h> 71 #include <vm/pmap.h> 72 73 #include <sys/thread2.h> 74 #include <sys/buf2.h> 75 76 #include <machine/pcb.h> 77 #include <machine/clock.h> 78 #include <machine/md_var.h> 79 #include <machine/smp.h> /* smp_active_mask, cpuid */ 80 #include <machine/vmparam.h> 81 82 #include <sys/signalvar.h> 83 84 #ifndef PANIC_REBOOT_WAIT_TIME 85 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 86 #endif 87 88 /* 89 * Note that stdarg.h and the ANSI style va_start macro is used for both 90 * ANSI and traditional C compilers. We use the machine version to stay 91 * within the confines of the kernel header files. 92 */ 93 #include <machine/stdarg.h> 94 95 #ifdef DDB 96 #ifdef DDB_UNATTENDED 97 int debugger_on_panic = 0; 98 #else 99 int debugger_on_panic = 1; 100 #endif 101 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 102 &debugger_on_panic, 0, "Run debugger on kernel panic"); 103 104 extern void db_print_backtrace(void); 105 106 #ifdef DDB_TRACE 107 int trace_on_panic = 1; 108 #else 109 int trace_on_panic = 0; 110 #endif 111 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW, 112 &trace_on_panic, 0, "Print stack trace on kernel panic"); 113 #endif 114 115 static int sync_on_panic = 1; 116 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW, 117 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 118 119 SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 120 121 #ifdef HW_WDOG 122 /* 123 * If there is a hardware watchdog, point this at the function needed to 124 * hold it off. 125 * It's needed when the kernel needs to do some lengthy operations. 126 * e.g. in wd.c when dumping core.. It's most annoying to have 127 * your precious core-dump only half written because the wdog kicked in. 128 */ 129 watchdog_tickle_fn wdog_tickler = NULL; 130 #endif /* HW_WDOG */ 131 132 /* 133 * Variable panicstr contains argument to first call to panic; used as flag 134 * to indicate that the kernel has already called panic. 135 */ 136 const char *panicstr; 137 138 int dumping; /* system is dumping */ 139 #ifdef SMP 140 u_int panic_cpu_interlock; /* panic interlock */ 141 globaldata_t panic_cpu_gd; /* which cpu took the panic */ 142 #endif 143 144 int bootverbose = 0; /* note: assignment to force non-bss */ 145 int cold = 1; /* note: assignment to force non-bss */ 146 long dumplo; 147 148 static void boot (int) __dead2; 149 static void dumpsys (void); 150 static int setdumpdev (cdev_t dev); 151 static void poweroff_wait (void *, int); 152 static void print_uptime (void); 153 static void shutdown_halt (void *junk, int howto); 154 static void shutdown_panic (void *junk, int howto); 155 static void shutdown_reset (void *junk, int howto); 156 static int shutdown_busycount1(struct buf *bp, void *info); 157 static int shutdown_busycount2(struct buf *bp, void *info); 158 static void shutdown_cleanup_proc(struct proc *p); 159 160 /* register various local shutdown events */ 161 static void 162 shutdown_conf(void *unused) 163 { 164 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 165 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 166 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 167 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 168 } 169 170 SYSINIT(shutdown_conf, SI_BOOT2_MACHDEP, SI_ORDER_ANY, shutdown_conf, NULL) 171 172 /* ARGSUSED */ 173 174 /* 175 * The system call that results in a reboot 176 */ 177 int 178 sys_reboot(struct reboot_args *uap) 179 { 180 struct thread *td = curthread; 181 int error; 182 183 if ((error = suser(td))) 184 return (error); 185 186 boot(uap->opt); 187 return (0); 188 } 189 190 /* 191 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 192 */ 193 static int shutdown_howto = 0; 194 195 void 196 shutdown_nice(int howto) 197 { 198 shutdown_howto = howto; 199 200 /* Send a signal to init(8) and have it shutdown the world */ 201 if (initproc != NULL) { 202 ksignal(initproc, SIGINT); 203 } else { 204 /* No init(8) running, so simply reboot */ 205 boot(RB_NOSYNC); 206 } 207 return; 208 } 209 static int waittime = -1; 210 static struct thread *dumpthread; 211 static struct pcb dumppcb; 212 213 static void 214 print_uptime(void) 215 { 216 int f; 217 struct timespec ts; 218 219 getnanouptime(&ts); 220 kprintf("Uptime: "); 221 f = 0; 222 if (ts.tv_sec >= 86400) { 223 kprintf("%ldd", ts.tv_sec / 86400); 224 ts.tv_sec %= 86400; 225 f = 1; 226 } 227 if (f || ts.tv_sec >= 3600) { 228 kprintf("%ldh", ts.tv_sec / 3600); 229 ts.tv_sec %= 3600; 230 f = 1; 231 } 232 if (f || ts.tv_sec >= 60) { 233 kprintf("%ldm", ts.tv_sec / 60); 234 ts.tv_sec %= 60; 235 f = 1; 236 } 237 kprintf("%lds\n", ts.tv_sec); 238 } 239 240 /* 241 * Go through the rigmarole of shutting down.. 242 * this used to be in machdep.c but I'll be dammned if I could see 243 * anything machine dependant in it. 244 */ 245 static void 246 boot(int howto) 247 { 248 /* 249 * Get rid of any user scheduler baggage and then give 250 * us a high priority. 251 */ 252 if (curthread->td_release) 253 curthread->td_release(curthread); 254 lwkt_setpri_self(TDPRI_MAX); 255 256 /* collect extra flags that shutdown_nice might have set */ 257 howto |= shutdown_howto; 258 259 #ifdef SMP 260 /* 261 * We really want to shutdown on the BSP. Subsystems such as ACPI 262 * can't power-down the box otherwise. 263 */ 264 if (smp_active_mask > 1) { 265 kprintf("boot() called on cpu#%d\n", mycpu->gd_cpuid); 266 } 267 if (panicstr == NULL && mycpu->gd_cpuid != 0) { 268 kprintf("Switching to cpu #0 for shutdown\n"); 269 lwkt_setcpu_self(globaldata_find(0)); 270 } 271 #endif 272 /* 273 * Do any callouts that should be done BEFORE syncing the filesystems. 274 */ 275 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 276 277 /* 278 * Try to get rid of any remaining FS references. The calling 279 * process, proc0, and init may still hold references. The 280 * VFS cache subsystem may still hold a root reference to root. 281 * 282 * XXX this needs work. We really need to SIGSTOP all remaining 283 * processes in order to avoid blowups due to proc0's filesystem 284 * references going away. For now just make sure that the init 285 * process is stopped. 286 */ 287 if (panicstr == NULL) { 288 shutdown_cleanup_proc(curproc); 289 shutdown_cleanup_proc(&proc0); 290 if (initproc) { 291 if (initproc != curproc) { 292 ksignal(initproc, SIGSTOP); 293 tsleep(boot, 0, "shutdn", hz / 20); 294 } 295 shutdown_cleanup_proc(initproc); 296 } 297 vfs_cache_setroot(NULL, NULL); 298 } 299 300 /* 301 * Now sync filesystems 302 */ 303 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 304 int iter, nbusy, pbusy; 305 306 waittime = 0; 307 kprintf("\nsyncing disks... "); 308 309 sys_sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */ 310 311 /* 312 * With soft updates, some buffers that are 313 * written will be remarked as dirty until other 314 * buffers are written. 315 */ 316 for (iter = pbusy = 0; iter < 20; iter++) { 317 nbusy = scan_all_buffers(shutdown_busycount1, NULL); 318 if (nbusy == 0) 319 break; 320 kprintf("%d ", nbusy); 321 if (nbusy < pbusy) 322 iter = 0; 323 pbusy = nbusy; 324 /* 325 * XXX: 326 * Process soft update work queue if buffers don't sync 327 * after 6 iterations by permitting the syncer to run. 328 */ 329 if (iter > 5 && bioops.io_sync) 330 (*bioops.io_sync)(NULL); 331 sys_sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */ 332 tsleep(boot, 0, "shutdn", hz * iter / 20 + 1); 333 } 334 kprintf("\n"); 335 /* 336 * Count only busy local buffers to prevent forcing 337 * a fsck if we're just a client of a wedged NFS server 338 */ 339 nbusy = scan_all_buffers(shutdown_busycount2, NULL); 340 if (nbusy) { 341 /* 342 * Failed to sync all blocks. Indicate this and don't 343 * unmount filesystems (thus forcing an fsck on reboot). 344 */ 345 kprintf("giving up on %d buffers\n", nbusy); 346 #ifdef DDB 347 Debugger("busy buffer problem"); 348 #endif /* DDB */ 349 tsleep(boot, 0, "shutdn", hz * 5 + 1); 350 } else { 351 kprintf("done\n"); 352 /* 353 * Unmount filesystems 354 */ 355 if (panicstr == NULL) 356 vfs_unmountall(); 357 } 358 tsleep(boot, 0, "shutdn", hz / 10 + 1); 359 } 360 361 print_uptime(); 362 363 /* 364 * Ok, now do things that assume all filesystem activity has 365 * been completed. 366 */ 367 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 368 crit_enter(); 369 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) 370 dumpsys(); 371 372 /* Now that we're going to really halt the system... */ 373 EVENTHANDLER_INVOKE(shutdown_final, howto); 374 375 for(;;) ; /* safety against shutdown_reset not working */ 376 /* NOTREACHED */ 377 } 378 379 static int 380 shutdown_busycount1(struct buf *bp, void *info) 381 { 382 if ((bp->b_flags & B_INVAL) == 0 && BUF_REFCNT(bp) > 0) 383 return(1); 384 if ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) 385 return (1); 386 return (0); 387 } 388 389 static int 390 shutdown_busycount2(struct buf *bp, void *info) 391 { 392 if (((bp->b_flags & B_INVAL) == 0 && BUF_REFCNT(bp)) || 393 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 394 /* 395 * Only count buffers undergoing write I/O 396 * on the related vnode. 397 */ 398 if (bp->b_vp == NULL || 399 bp->b_vp->v_track_write.bk_active == 0) { 400 return (0); 401 } 402 #if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 403 kprintf( 404 "%p dev:?, flags:%08x, loffset:%lld, doffset:%lld\n", 405 bp, 406 bp->b_flags, bp->b_loffset, 407 bp->b_bio2.bio_offset); 408 #endif 409 return(1); 410 } 411 return(0); 412 } 413 414 /* 415 * If the shutdown was a clean halt, behave accordingly. 416 */ 417 static void 418 shutdown_halt(void *junk, int howto) 419 { 420 if (howto & RB_HALT) { 421 kprintf("\n"); 422 kprintf("The operating system has halted.\n"); 423 kprintf("Please press any key to reboot.\n\n"); 424 switch (cngetc()) { 425 case -1: /* No console, just die */ 426 cpu_halt(); 427 /* NOTREACHED */ 428 default: 429 howto &= ~RB_HALT; 430 break; 431 } 432 } 433 } 434 435 /* 436 * Check to see if the system paniced, pause and then reboot 437 * according to the specified delay. 438 */ 439 static void 440 shutdown_panic(void *junk, int howto) 441 { 442 int loop; 443 444 if (howto & RB_DUMP) { 445 if (PANIC_REBOOT_WAIT_TIME != 0) { 446 if (PANIC_REBOOT_WAIT_TIME != -1) { 447 kprintf("Automatic reboot in %d seconds - " 448 "press a key on the console to abort\n", 449 PANIC_REBOOT_WAIT_TIME); 450 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 451 loop > 0; --loop) { 452 DELAY(1000 * 100); /* 1/10th second */ 453 /* Did user type a key? */ 454 if (cncheckc() != -1) 455 break; 456 } 457 if (!loop) 458 return; 459 } 460 } else { /* zero time specified - reboot NOW */ 461 return; 462 } 463 kprintf("--> Press a key on the console to reboot,\n"); 464 kprintf("--> or switch off the system now.\n"); 465 cngetc(); 466 } 467 } 468 469 /* 470 * Everything done, now reset 471 */ 472 static void 473 shutdown_reset(void *junk, int howto) 474 { 475 kprintf("Rebooting...\n"); 476 DELAY(1000000); /* wait 1 sec for kprintf's to complete and be read */ 477 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 478 cpu_reset(); 479 /* NOTREACHED */ /* assuming reset worked */ 480 } 481 482 /* 483 * Try to remove FS references in the specified process. This function 484 * is used during shutdown 485 */ 486 static 487 void 488 shutdown_cleanup_proc(struct proc *p) 489 { 490 struct filedesc *fdp; 491 struct vmspace *vm; 492 493 if (p == NULL) 494 return; 495 if ((fdp = p->p_fd) != NULL) { 496 kern_closefrom(0); 497 if (fdp->fd_cdir) { 498 cache_drop(&fdp->fd_ncdir); 499 vrele(fdp->fd_cdir); 500 fdp->fd_cdir = NULL; 501 } 502 if (fdp->fd_rdir) { 503 cache_drop(&fdp->fd_nrdir); 504 vrele(fdp->fd_rdir); 505 fdp->fd_rdir = NULL; 506 } 507 if (fdp->fd_jdir) { 508 cache_drop(&fdp->fd_njdir); 509 vrele(fdp->fd_jdir); 510 fdp->fd_jdir = NULL; 511 } 512 } 513 if (p->p_vkernel) 514 vkernel_exit(p); 515 if (p->p_textvp) { 516 vrele(p->p_textvp); 517 p->p_textvp = NULL; 518 } 519 vm = p->p_vmspace; 520 if (vm != NULL) { 521 pmap_remove_pages(vmspace_pmap(vm), 522 VM_MIN_USER_ADDRESS, 523 VM_MAX_USER_ADDRESS); 524 vm_map_remove(&vm->vm_map, 525 VM_MIN_USER_ADDRESS, 526 VM_MAX_USER_ADDRESS); 527 } 528 } 529 530 /* 531 * Magic number for savecore 532 * 533 * exported (symorder) and used at least by savecore(8) 534 * 535 * Mark it as used so that gcc doesn't optimize it away. 536 */ 537 __attribute__((__used__)) 538 static u_long const dumpmag = 0x8fca0101UL; 539 540 static int dumpsize = 0; /* also for savecore */ 541 542 static int dodump = 1; 543 544 SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 545 "Try to perform coredump on kernel panic"); 546 547 static int 548 setdumpdev(cdev_t dev) 549 { 550 int psize; 551 long newdumplo; 552 553 if (dev == NULL) { 554 dumpdev = dev; 555 return (0); 556 } 557 psize = dev_dpsize(dev); 558 if (psize == -1) 559 return (ENXIO); 560 /* 561 * XXX should clean up checking in dumpsys() to be more like this. 562 */ 563 newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE); 564 if (newdumplo <= LABELSECTOR) 565 return (ENOSPC); 566 dumpdev = dev; 567 dumplo = newdumplo; 568 return (0); 569 } 570 571 572 /* ARGSUSED */ 573 static void dump_conf (void *dummy); 574 static void 575 dump_conf(void *dummy) 576 { 577 char *path; 578 cdev_t dev; 579 580 path = kmalloc(MNAMELEN, M_TEMP, M_WAITOK); 581 if (TUNABLE_STR_FETCH("dumpdev", path, MNAMELEN) != 0) { 582 dev = kgetdiskbyname(path); 583 if (dev != NULL) 584 dumpdev = dev; 585 } 586 kfree(path, M_TEMP); 587 if (setdumpdev(dumpdev) != 0) 588 dumpdev = NULL; 589 } 590 591 SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 592 593 static int 594 sysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 595 { 596 int error; 597 udev_t ndumpdev; 598 599 ndumpdev = dev2udev(dumpdev); 600 error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 601 if (error == 0 && req->newptr != NULL) 602 error = setdumpdev(udev2dev(ndumpdev, 0)); 603 return (error); 604 } 605 606 SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 607 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,udev_t", ""); 608 609 /* 610 * Doadump comes here after turning off memory management and 611 * getting on the dump stack, either when called above, or by 612 * the auto-restart code. 613 */ 614 static void 615 dumpsys(void) 616 { 617 int error; 618 619 savectx(&dumppcb); 620 dumpthread = curthread; 621 if (dumping++) { 622 kprintf("Dump already in progress, bailing...\n"); 623 return; 624 } 625 if (!dodump) 626 return; 627 if (dumpdev == NULL) 628 return; 629 dumpsize = Maxmem; 630 kprintf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 631 kprintf("dump "); 632 error = dev_ddump(dumpdev); 633 if (error == 0) { 634 kprintf("succeeded\n"); 635 return; 636 } 637 kprintf("failed, reason: "); 638 switch (error) { 639 case ENOSYS: 640 case ENODEV: 641 kprintf("device doesn't support a dump routine\n"); 642 break; 643 644 case ENXIO: 645 kprintf("device bad\n"); 646 break; 647 648 case EFAULT: 649 kprintf("device not ready\n"); 650 break; 651 652 case EINVAL: 653 kprintf("area improper\n"); 654 break; 655 656 case EIO: 657 kprintf("i/o error\n"); 658 break; 659 660 case EINTR: 661 kprintf("aborted from console\n"); 662 break; 663 664 default: 665 kprintf("unknown, error = %d\n", error); 666 break; 667 } 668 } 669 670 int 671 dumpstatus(vm_offset_t addr, off_t count) 672 { 673 int c; 674 675 if (addr % (1024 * 1024) == 0) { 676 #ifdef HW_WDOG 677 if (wdog_tickler) 678 (*wdog_tickler)(); 679 #endif 680 kprintf("%ld ", (long)(count / (1024 * 1024))); 681 } 682 683 if ((c = cncheckc()) == 0x03) 684 return -1; 685 else if (c != -1) 686 kprintf("[CTRL-C to abort] "); 687 688 return 0; 689 } 690 691 /* 692 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 693 * and then reboots. If we are called twice, then we avoid trying to sync 694 * the disks as this often leads to recursive panics. 695 */ 696 void 697 panic(const char *fmt, ...) 698 { 699 int bootopt, newpanic; 700 __va_list ap; 701 static char buf[256]; 702 703 #ifdef SMP 704 /* 705 * If a panic occurs on multiple cpus before the first is able to 706 * halt the other cpus, only one cpu is allowed to take the panic. 707 * Attempt to be verbose about this situation but if the kprintf() 708 * itself panics don't let us overrun the kernel stack. 709 * 710 * Be very nasty about descheduling our thread at the lowest 711 * level possible in an attempt to freeze the thread without 712 * inducing further panics. 713 * 714 * Bumping gd_trap_nesting_level will also bypass assertions in 715 * lwkt_switch() and allow us to switch away even if we are a 716 * FAST interrupt or IPI. 717 */ 718 if (atomic_poll_acquire_int(&panic_cpu_interlock)) { 719 panic_cpu_gd = mycpu; 720 } else if (panic_cpu_gd != mycpu) { 721 crit_enter(); 722 ++mycpu->gd_trap_nesting_level; 723 if (mycpu->gd_trap_nesting_level < 25) { 724 kprintf("SECONDARY PANIC ON CPU %d THREAD %p\n", 725 mycpu->gd_cpuid, curthread); 726 } 727 curthread->td_release = NULL; /* be a grinch */ 728 for (;;) { 729 lwkt_deschedule_self(curthread); 730 lwkt_switch(); 731 } 732 /* NOT REACHED */ 733 /* --mycpu->gd_trap_nesting_level */ 734 /* crit_exit() */ 735 } 736 #endif 737 bootopt = RB_AUTOBOOT | RB_DUMP; 738 if (sync_on_panic == 0) 739 bootopt |= RB_NOSYNC; 740 newpanic = 0; 741 if (panicstr) 742 bootopt |= RB_NOSYNC; 743 else { 744 panicstr = fmt; 745 newpanic = 1; 746 } 747 748 __va_start(ap, fmt); 749 kvsnprintf(buf, sizeof(buf), fmt, ap); 750 if (panicstr == fmt) 751 panicstr = buf; 752 __va_end(ap); 753 kprintf("panic: %s\n", buf); 754 #ifdef SMP 755 /* three separate prints in case of an unmapped page and trap */ 756 kprintf("mp_lock = %08x; ", mp_lock); 757 kprintf("cpuid = %d; ", mycpu->gd_cpuid); 758 kprintf("lapic.id = %08x\n", lapic.id); 759 #endif 760 761 #if defined(DDB) 762 if (newpanic && trace_on_panic) 763 db_print_backtrace(); 764 if (debugger_on_panic) 765 Debugger ("panic"); 766 #endif 767 boot(bootopt); 768 } 769 770 /* 771 * Support for poweroff delay. 772 */ 773 #ifndef POWEROFF_DELAY 774 # define POWEROFF_DELAY 5000 775 #endif 776 static int poweroff_delay = POWEROFF_DELAY; 777 778 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 779 &poweroff_delay, 0, ""); 780 781 static void 782 poweroff_wait(void *junk, int howto) 783 { 784 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 785 return; 786 DELAY(poweroff_delay * 1000); 787 } 788 789 /* 790 * Some system processes (e.g. syncer) need to be stopped at appropriate 791 * points in their main loops prior to a system shutdown, so that they 792 * won't interfere with the shutdown process (e.g. by holding a disk buf 793 * to cause sync to fail). For each of these system processes, register 794 * shutdown_kproc() as a handler for one of shutdown events. 795 */ 796 static int kproc_shutdown_wait = 60; 797 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 798 &kproc_shutdown_wait, 0, ""); 799 800 void 801 shutdown_kproc(void *arg, int howto) 802 { 803 struct thread *td; 804 struct proc *p; 805 int error; 806 807 if (panicstr) 808 return; 809 810 td = (struct thread *)arg; 811 if ((p = td->td_proc) != NULL) { 812 kprintf("Waiting (max %d seconds) for system process `%s' to stop...", 813 kproc_shutdown_wait, p->p_comm); 814 } else { 815 kprintf("Waiting (max %d seconds) for system thread %s to stop...", 816 kproc_shutdown_wait, td->td_comm); 817 } 818 error = suspend_kproc(td, kproc_shutdown_wait * hz); 819 820 if (error == EWOULDBLOCK) 821 kprintf("timed out\n"); 822 else 823 kprintf("stopped\n"); 824 } 825