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.46 2006/12/28 18:29:03 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 147 static void boot (int) __dead2; 148 static void dumpsys (void); 149 static int setdumpdev (cdev_t dev); 150 static void poweroff_wait (void *, int); 151 static void print_uptime (void); 152 static void shutdown_halt (void *junk, int howto); 153 static void shutdown_panic (void *junk, int howto); 154 static void shutdown_reset (void *junk, int howto); 155 static int shutdown_busycount1(struct buf *bp, void *info); 156 static int shutdown_busycount2(struct buf *bp, void *info); 157 static void shutdown_cleanup_proc(struct proc *p); 158 159 /* register various local shutdown events */ 160 static void 161 shutdown_conf(void *unused) 162 { 163 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 164 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 165 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 166 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 167 } 168 169 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 170 171 /* ARGSUSED */ 172 173 /* 174 * The system call that results in a reboot 175 */ 176 int 177 sys_reboot(struct reboot_args *uap) 178 { 179 struct thread *td = curthread; 180 int error; 181 182 if ((error = suser(td))) 183 return (error); 184 185 boot(uap->opt); 186 return (0); 187 } 188 189 /* 190 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 191 */ 192 static int shutdown_howto = 0; 193 194 void 195 shutdown_nice(int howto) 196 { 197 shutdown_howto = howto; 198 199 /* Send a signal to init(8) and have it shutdown the world */ 200 if (initproc != NULL) { 201 ksignal(initproc, SIGINT); 202 } else { 203 /* No init(8) running, so simply reboot */ 204 boot(RB_NOSYNC); 205 } 206 return; 207 } 208 static int waittime = -1; 209 static struct thread *dumpthread; 210 static struct pcb dumppcb; 211 212 static void 213 print_uptime(void) 214 { 215 int f; 216 struct timespec ts; 217 218 getnanouptime(&ts); 219 kprintf("Uptime: "); 220 f = 0; 221 if (ts.tv_sec >= 86400) { 222 kprintf("%ldd", ts.tv_sec / 86400); 223 ts.tv_sec %= 86400; 224 f = 1; 225 } 226 if (f || ts.tv_sec >= 3600) { 227 kprintf("%ldh", ts.tv_sec / 3600); 228 ts.tv_sec %= 3600; 229 f = 1; 230 } 231 if (f || ts.tv_sec >= 60) { 232 kprintf("%ldm", ts.tv_sec / 60); 233 ts.tv_sec %= 60; 234 f = 1; 235 } 236 kprintf("%lds\n", ts.tv_sec); 237 } 238 239 /* 240 * Go through the rigmarole of shutting down.. 241 * this used to be in machdep.c but I'll be dammned if I could see 242 * anything machine dependant in it. 243 */ 244 static void 245 boot(int howto) 246 { 247 /* 248 * Get rid of any user scheduler baggage and then give 249 * us a high priority. 250 */ 251 if (curthread->td_release) 252 curthread->td_release(curthread); 253 lwkt_setpri_self(TDPRI_MAX); 254 255 /* collect extra flags that shutdown_nice might have set */ 256 howto |= shutdown_howto; 257 258 #ifdef SMP 259 /* 260 * We really want to shutdown on the BSP. Subsystems such as ACPI 261 * can't power-down the box otherwise. 262 */ 263 if (smp_active_mask > 1) { 264 kprintf("boot() called on cpu#%d\n", mycpu->gd_cpuid); 265 } 266 if (panicstr == NULL && mycpu->gd_cpuid != 0) { 267 kprintf("Switching to cpu #0 for shutdown\n"); 268 lwkt_setcpu_self(globaldata_find(0)); 269 } 270 #endif 271 /* 272 * Do any callouts that should be done BEFORE syncing the filesystems. 273 */ 274 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 275 276 /* 277 * Try to get rid of any remaining FS references. The calling 278 * process, proc0, and init may still hold references. The 279 * VFS cache subsystem may still hold a root reference to root. 280 */ 281 if (panicstr == NULL) { 282 shutdown_cleanup_proc(curproc); 283 shutdown_cleanup_proc(&proc0); 284 if (initproc) 285 shutdown_cleanup_proc(initproc); 286 vfs_cache_setroot(NULL, NULL); 287 } 288 289 /* 290 * Now sync filesystems 291 */ 292 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 293 int iter, nbusy, pbusy; 294 295 waittime = 0; 296 kprintf("\nsyncing disks... "); 297 298 sys_sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */ 299 300 /* 301 * With soft updates, some buffers that are 302 * written will be remarked as dirty until other 303 * buffers are written. 304 */ 305 for (iter = pbusy = 0; iter < 20; iter++) { 306 nbusy = scan_all_buffers(shutdown_busycount1, NULL); 307 if (nbusy == 0) 308 break; 309 kprintf("%d ", nbusy); 310 if (nbusy < pbusy) 311 iter = 0; 312 pbusy = nbusy; 313 /* 314 * XXX: 315 * Process soft update work queue if buffers don't sync 316 * after 6 iterations by permitting the syncer to run. 317 */ 318 if (iter > 5 && bioops.io_sync) 319 (*bioops.io_sync)(NULL); 320 sys_sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */ 321 tsleep(boot, 0, "shutdn", hz * iter / 20 + 1); 322 } 323 kprintf("\n"); 324 /* 325 * Count only busy local buffers to prevent forcing 326 * a fsck if we're just a client of a wedged NFS server 327 */ 328 nbusy = scan_all_buffers(shutdown_busycount2, NULL); 329 if (nbusy) { 330 /* 331 * Failed to sync all blocks. Indicate this and don't 332 * unmount filesystems (thus forcing an fsck on reboot). 333 */ 334 kprintf("giving up on %d buffers\n", nbusy); 335 #ifdef DDB 336 Debugger("busy buffer problem"); 337 #endif /* DDB */ 338 tsleep(boot, 0, "shutdn", hz * 5 + 1); 339 } else { 340 kprintf("done\n"); 341 /* 342 * Unmount filesystems 343 */ 344 if (panicstr == NULL) 345 vfs_unmountall(); 346 } 347 tsleep(boot, 0, "shutdn", hz / 10 + 1); 348 } 349 350 print_uptime(); 351 352 /* 353 * Ok, now do things that assume all filesystem activity has 354 * been completed. 355 */ 356 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 357 crit_enter(); 358 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) 359 dumpsys(); 360 361 /* Now that we're going to really halt the system... */ 362 EVENTHANDLER_INVOKE(shutdown_final, howto); 363 364 for(;;) ; /* safety against shutdown_reset not working */ 365 /* NOTREACHED */ 366 } 367 368 static int 369 shutdown_busycount1(struct buf *bp, void *info) 370 { 371 if ((bp->b_flags & B_INVAL) == 0 && BUF_REFCNT(bp) > 0) 372 return(1); 373 if ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) 374 return (1); 375 return (0); 376 } 377 378 static int 379 shutdown_busycount2(struct buf *bp, void *info) 380 { 381 if (((bp->b_flags & B_INVAL) == 0 && BUF_REFCNT(bp)) || 382 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 383 /* 384 * Only count buffers undergoing write I/O 385 * on the related vnode. 386 */ 387 if (bp->b_vp == NULL || 388 bp->b_vp->v_track_write.bk_active == 0) { 389 return (0); 390 } 391 #if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 392 kprintf( 393 "%p dev:?, flags:%08x, loffset:%lld, doffset:%lld\n", 394 bp, 395 bp->b_flags, bp->b_loffset, 396 bp->b_bio2.bio_offset); 397 #endif 398 return(1); 399 } 400 return(0); 401 } 402 403 /* 404 * If the shutdown was a clean halt, behave accordingly. 405 */ 406 static void 407 shutdown_halt(void *junk, int howto) 408 { 409 if (howto & RB_HALT) { 410 kprintf("\n"); 411 kprintf("The operating system has halted.\n"); 412 kprintf("Please press any key to reboot.\n\n"); 413 switch (cngetc()) { 414 case -1: /* No console, just die */ 415 cpu_halt(); 416 /* NOTREACHED */ 417 default: 418 howto &= ~RB_HALT; 419 break; 420 } 421 } 422 } 423 424 /* 425 * Check to see if the system paniced, pause and then reboot 426 * according to the specified delay. 427 */ 428 static void 429 shutdown_panic(void *junk, int howto) 430 { 431 int loop; 432 433 if (howto & RB_DUMP) { 434 if (PANIC_REBOOT_WAIT_TIME != 0) { 435 if (PANIC_REBOOT_WAIT_TIME != -1) { 436 kprintf("Automatic reboot in %d seconds - " 437 "press a key on the console to abort\n", 438 PANIC_REBOOT_WAIT_TIME); 439 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 440 loop > 0; --loop) { 441 DELAY(1000 * 100); /* 1/10th second */ 442 /* Did user type a key? */ 443 if (cncheckc() != -1) 444 break; 445 } 446 if (!loop) 447 return; 448 } 449 } else { /* zero time specified - reboot NOW */ 450 return; 451 } 452 kprintf("--> Press a key on the console to reboot,\n"); 453 kprintf("--> or switch off the system now.\n"); 454 cngetc(); 455 } 456 } 457 458 /* 459 * Everything done, now reset 460 */ 461 static void 462 shutdown_reset(void *junk, int howto) 463 { 464 kprintf("Rebooting...\n"); 465 DELAY(1000000); /* wait 1 sec for kprintf's to complete and be read */ 466 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 467 cpu_reset(); 468 /* NOTREACHED */ /* assuming reset worked */ 469 } 470 471 /* 472 * Try to remove FS references in the specified process. This function 473 * is used during shutdown 474 */ 475 static 476 void 477 shutdown_cleanup_proc(struct proc *p) 478 { 479 struct filedesc *fdp; 480 struct vmspace *vm; 481 482 if (p == NULL) 483 return; 484 if ((fdp = p->p_fd) != NULL) { 485 kern_closefrom(0); 486 if (fdp->fd_cdir) { 487 cache_drop(&fdp->fd_ncdir); 488 vrele(fdp->fd_cdir); 489 fdp->fd_cdir = NULL; 490 } 491 if (fdp->fd_rdir) { 492 cache_drop(&fdp->fd_nrdir); 493 vrele(fdp->fd_rdir); 494 fdp->fd_rdir = NULL; 495 } 496 if (fdp->fd_jdir) { 497 cache_drop(&fdp->fd_njdir); 498 vrele(fdp->fd_jdir); 499 fdp->fd_jdir = NULL; 500 } 501 } 502 if (p->p_vkernel) 503 vkernel_exit(p); 504 if (p->p_textvp) { 505 vrele(p->p_textvp); 506 p->p_textvp = NULL; 507 } 508 vm = p->p_vmspace; 509 if (vm != NULL) { 510 pmap_remove_pages(vmspace_pmap(vm), 511 VM_MIN_USER_ADDRESS, 512 VM_MAX_USER_ADDRESS); 513 vm_map_remove(&vm->vm_map, 514 VM_MIN_USER_ADDRESS, 515 VM_MAX_USER_ADDRESS); 516 } 517 } 518 519 /* 520 * Magic number for savecore 521 * 522 * exported (symorder) and used at least by savecore(8) 523 * 524 */ 525 static u_long const dumpmag = 0x8fca0101UL; 526 527 static int dumpsize = 0; /* also for savecore */ 528 529 static int dodump = 1; 530 531 SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 532 "Try to perform coredump on kernel panic"); 533 534 static int 535 setdumpdev(cdev_t dev) 536 { 537 int psize; 538 long newdumplo; 539 540 if (dev == NOCDEV) { 541 dumpdev = dev; 542 return (0); 543 } 544 psize = dev_dpsize(dev); 545 if (psize == -1) 546 return (ENXIO); 547 /* 548 * XXX should clean up checking in dumpsys() to be more like this. 549 */ 550 newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE); 551 if (newdumplo <= LABELSECTOR) 552 return (ENOSPC); 553 dumpdev = dev; 554 dumplo = newdumplo; 555 return (0); 556 } 557 558 559 /* ARGSUSED */ 560 static void dump_conf (void *dummy); 561 static void 562 dump_conf(void *dummy) 563 { 564 char *path; 565 cdev_t dev; 566 567 path = kmalloc(MNAMELEN, M_TEMP, M_WAITOK); 568 if (TUNABLE_STR_FETCH("dumpdev", path, MNAMELEN) != 0) { 569 dev = kgetdiskbyname(path); 570 if (dev != NOCDEV) 571 dumpdev = dev; 572 } 573 kfree(path, M_TEMP); 574 if (setdumpdev(dumpdev) != 0) 575 dumpdev = NOCDEV; 576 } 577 578 SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 579 580 static int 581 sysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 582 { 583 int error; 584 udev_t ndumpdev; 585 586 ndumpdev = dev2udev(dumpdev); 587 error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 588 if (error == 0 && req->newptr != NULL) 589 error = setdumpdev(udev2dev(ndumpdev, 0)); 590 return (error); 591 } 592 593 SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 594 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,udev_t", ""); 595 596 /* 597 * Doadump comes here after turning off memory management and 598 * getting on the dump stack, either when called above, or by 599 * the auto-restart code. 600 */ 601 static void 602 dumpsys(void) 603 { 604 int error; 605 606 savectx(&dumppcb); 607 dumpthread = curthread; 608 if (dumping++) { 609 kprintf("Dump already in progress, bailing...\n"); 610 return; 611 } 612 if (!dodump) 613 return; 614 if (dumpdev == NOCDEV) 615 return; 616 dumpsize = Maxmem; 617 kprintf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 618 kprintf("dump "); 619 error = dev_ddump(dumpdev); 620 if (error == 0) { 621 kprintf("succeeded\n"); 622 return; 623 } 624 kprintf("failed, reason: "); 625 switch (error) { 626 case ENOSYS: 627 case ENODEV: 628 kprintf("device doesn't support a dump routine\n"); 629 break; 630 631 case ENXIO: 632 kprintf("device bad\n"); 633 break; 634 635 case EFAULT: 636 kprintf("device not ready\n"); 637 break; 638 639 case EINVAL: 640 kprintf("area improper\n"); 641 break; 642 643 case EIO: 644 kprintf("i/o error\n"); 645 break; 646 647 case EINTR: 648 kprintf("aborted from console\n"); 649 break; 650 651 default: 652 kprintf("unknown, error = %d\n", error); 653 break; 654 } 655 } 656 657 int 658 dumpstatus(vm_offset_t addr, off_t count) 659 { 660 int c; 661 662 if (addr % (1024 * 1024) == 0) { 663 #ifdef HW_WDOG 664 if (wdog_tickler) 665 (*wdog_tickler)(); 666 #endif 667 kprintf("%ld ", (long)(count / (1024 * 1024))); 668 } 669 670 if ((c = cncheckc()) == 0x03) 671 return -1; 672 else if (c != -1) 673 kprintf("[CTRL-C to abort] "); 674 675 return 0; 676 } 677 678 /* 679 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 680 * and then reboots. If we are called twice, then we avoid trying to sync 681 * the disks as this often leads to recursive panics. 682 */ 683 void 684 panic(const char *fmt, ...) 685 { 686 int bootopt, newpanic; 687 __va_list ap; 688 static char buf[256]; 689 690 #ifdef SMP 691 /* 692 * If a panic occurs on multiple cpus before the first is able to 693 * halt the other cpus, only one cpu is allowed to take the panic. 694 * Attempt to be verbose about this situation but if the kprintf() 695 * itself panics don't let us overrun the kernel stack. 696 * 697 * Be very nasty about descheduling our thread at the lowest 698 * level possible in an attempt to freeze the thread without 699 * inducing further panics. 700 * 701 * Bumping gd_trap_nesting_level will also bypass assertions in 702 * lwkt_switch() and allow us to switch away even if we are a 703 * FAST interrupt or IPI. 704 */ 705 if (atomic_poll_acquire_int(&panic_cpu_interlock)) { 706 panic_cpu_gd = mycpu; 707 } else if (panic_cpu_gd != mycpu) { 708 crit_enter(); 709 ++mycpu->gd_trap_nesting_level; 710 if (mycpu->gd_trap_nesting_level < 25) { 711 kprintf("SECONDARY PANIC ON CPU %d THREAD %p\n", 712 mycpu->gd_cpuid, curthread); 713 } 714 curthread->td_release = NULL; /* be a grinch */ 715 for (;;) { 716 lwkt_deschedule_self(curthread); 717 lwkt_switch(); 718 } 719 /* NOT REACHED */ 720 /* --mycpu->gd_trap_nesting_level */ 721 /* crit_exit() */ 722 } 723 #endif 724 bootopt = RB_AUTOBOOT | RB_DUMP; 725 if (sync_on_panic == 0) 726 bootopt |= RB_NOSYNC; 727 newpanic = 0; 728 if (panicstr) 729 bootopt |= RB_NOSYNC; 730 else { 731 panicstr = fmt; 732 newpanic = 1; 733 } 734 735 __va_start(ap, fmt); 736 kvsnprintf(buf, sizeof(buf), fmt, ap); 737 if (panicstr == fmt) 738 panicstr = buf; 739 __va_end(ap); 740 kprintf("panic: %s\n", buf); 741 #ifdef SMP 742 /* three separate prints in case of an unmapped page and trap */ 743 kprintf("mp_lock = %08x; ", mp_lock); 744 kprintf("cpuid = %d; ", mycpu->gd_cpuid); 745 kprintf("lapic.id = %08x\n", lapic.id); 746 #endif 747 748 #if defined(DDB) 749 if (newpanic && trace_on_panic) 750 db_print_backtrace(); 751 if (debugger_on_panic) 752 Debugger ("panic"); 753 #endif 754 boot(bootopt); 755 } 756 757 /* 758 * Support for poweroff delay. 759 */ 760 #ifndef POWEROFF_DELAY 761 # define POWEROFF_DELAY 5000 762 #endif 763 static int poweroff_delay = POWEROFF_DELAY; 764 765 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 766 &poweroff_delay, 0, ""); 767 768 static void 769 poweroff_wait(void *junk, int howto) 770 { 771 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 772 return; 773 DELAY(poweroff_delay * 1000); 774 } 775 776 /* 777 * Some system processes (e.g. syncer) need to be stopped at appropriate 778 * points in their main loops prior to a system shutdown, so that they 779 * won't interfere with the shutdown process (e.g. by holding a disk buf 780 * to cause sync to fail). For each of these system processes, register 781 * shutdown_kproc() as a handler for one of shutdown events. 782 */ 783 static int kproc_shutdown_wait = 60; 784 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 785 &kproc_shutdown_wait, 0, ""); 786 787 void 788 shutdown_kproc(void *arg, int howto) 789 { 790 struct thread *td; 791 struct proc *p; 792 int error; 793 794 if (panicstr) 795 return; 796 797 td = (struct thread *)arg; 798 if ((p = td->td_proc) != NULL) { 799 kprintf("Waiting (max %d seconds) for system process `%s' to stop...", 800 kproc_shutdown_wait, p->p_comm); 801 } else { 802 kprintf("Waiting (max %d seconds) for system thread %s to stop...", 803 kproc_shutdown_wait, td->td_comm); 804 } 805 error = suspend_kproc(td, kproc_shutdown_wait * hz); 806 807 if (error == EWOULDBLOCK) 808 kprintf("timed out\n"); 809 else 810 kprintf("stopped\n"); 811 } 812