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$ 40 */ 41 42 #include "opt_ddb.h" 43 #include "opt_hw_wdog.h" 44 #include "opt_panic.h" 45 #include "opt_show_busybufs.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/bio.h> 50 #include <sys/buf.h> 51 #include <sys/conf.h> 52 #include <sys/cons.h> 53 #include <sys/disklabel.h> 54 #include <sys/eventhandler.h> 55 #include <sys/kernel.h> 56 #include <sys/kthread.h> 57 #include <sys/malloc.h> 58 #include <sys/mount.h> 59 #include <sys/proc.h> 60 #include <sys/reboot.h> 61 #include <sys/resourcevar.h> 62 #include <sys/smp.h> /* smp_active */ 63 #include <sys/sysctl.h> 64 #include <sys/sysproto.h> 65 #include <sys/vnode.h> 66 67 #include <machine/pcb.h> 68 #include <machine/md_var.h> 69 #include <machine/smp.h> 70 71 #include <sys/signalvar.h> 72 #ifdef DDB 73 #include <ddb/ddb.h> 74 #endif 75 76 #ifndef PANIC_REBOOT_WAIT_TIME 77 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 78 #endif 79 80 /* 81 * Note that stdarg.h and the ANSI style va_start macro is used for both 82 * ANSI and traditional C compilers. 83 */ 84 #include <machine/stdarg.h> 85 86 #ifdef DDB 87 #ifdef DDB_UNATTENDED 88 int debugger_on_panic = 0; 89 #else 90 int debugger_on_panic = 1; 91 #endif 92 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 93 &debugger_on_panic, 0, "Run debugger on kernel panic"); 94 #endif 95 96 int sync_on_panic = 1; 97 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW, 98 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 99 100 SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 101 102 #ifdef HW_WDOG 103 /* 104 * If there is a hardware watchdog, point this at the function needed to 105 * hold it off. 106 * It's needed when the kernel needs to do some lengthy operations. 107 * e.g. in wd.c when dumping core.. It's most annoying to have 108 * your precious core-dump only half written because the wdog kicked in. 109 */ 110 watchdog_tickle_fn wdog_tickler = NULL; 111 #endif /* HW_WDOG */ 112 113 /* 114 * Variable panicstr contains argument to first call to panic; used as flag 115 * to indicate that the kernel has already called panic. 116 */ 117 const char *panicstr; 118 119 int dumping; /* system is dumping */ 120 121 static void boot(int) __dead2; 122 static void dumpsys(void); 123 static void poweroff_wait(void *, int); 124 static void shutdown_halt(void *junk, int howto); 125 static void shutdown_panic(void *junk, int howto); 126 static void shutdown_reset(void *junk, int howto); 127 128 /* register various local shutdown events */ 129 static void 130 shutdown_conf(void *unused) 131 { 132 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 133 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 134 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 135 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 136 } 137 138 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 139 140 /* 141 * The system call that results in a reboot 142 * 143 * MPSAFE 144 */ 145 /* ARGSUSED */ 146 int 147 reboot(struct thread *td, struct reboot_args *uap) 148 { 149 int error; 150 151 mtx_lock(&Giant); 152 if ((error = suser_td(td)) == 0) 153 boot(uap->opt); 154 mtx_unlock(&Giant); 155 return (error); 156 } 157 158 /* 159 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 160 */ 161 static int shutdown_howto = 0; 162 163 void 164 shutdown_nice(int howto) 165 { 166 shutdown_howto = howto; 167 168 /* Send a signal to init(8) and have it shutdown the world */ 169 if (initproc != NULL) { 170 PROC_LOCK(initproc); 171 psignal(initproc, SIGINT); 172 PROC_UNLOCK(initproc); 173 } else { 174 /* No init(8) running, so simply reboot */ 175 boot(RB_NOSYNC); 176 } 177 return; 178 } 179 static int waittime = -1; 180 static struct pcb dumppcb; 181 182 static void 183 print_uptime(void) 184 { 185 int f; 186 struct timespec ts; 187 188 getnanouptime(&ts); 189 printf("Uptime: "); 190 f = 0; 191 if (ts.tv_sec >= 86400) { 192 printf("%ldd", (long)ts.tv_sec / 86400); 193 ts.tv_sec %= 86400; 194 f = 1; 195 } 196 if (f || ts.tv_sec >= 3600) { 197 printf("%ldh", (long)ts.tv_sec / 3600); 198 ts.tv_sec %= 3600; 199 f = 1; 200 } 201 if (f || ts.tv_sec >= 60) { 202 printf("%ldm", (long)ts.tv_sec / 60); 203 ts.tv_sec %= 60; 204 f = 1; 205 } 206 printf("%lds\n", (long)ts.tv_sec); 207 } 208 209 /* 210 * Go through the rigmarole of shutting down.. 211 * this used to be in machdep.c but I'll be dammned if I could see 212 * anything machine dependant in it. 213 */ 214 static void 215 boot(int howto) 216 { 217 218 /* collect extra flags that shutdown_nice might have set */ 219 howto |= shutdown_howto; 220 221 #ifdef DDB 222 /* We are out of the debugger now. */ 223 db_active = 0; 224 #endif 225 226 #ifdef SMP 227 if (smp_active) 228 printf("boot() called on cpu#%d\n", PCPU_GET(cpuid)); 229 #endif 230 /* 231 * Do any callouts that should be done BEFORE syncing the filesystems. 232 */ 233 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 234 235 /* 236 * Now sync filesystems 237 */ 238 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 239 register struct buf *bp; 240 int iter, nbusy, pbusy; 241 int subiter; 242 243 waittime = 0; 244 printf("\nsyncing disks... "); 245 246 sync(&thread0, NULL); 247 248 /* 249 * With soft updates, some buffers that are 250 * written will be remarked as dirty until other 251 * buffers are written. 252 */ 253 for (iter = pbusy = 0; iter < 20; iter++) { 254 nbusy = 0; 255 for (bp = &buf[nbuf]; --bp >= buf; ) { 256 if ((bp->b_flags & B_INVAL) == 0 && 257 BUF_REFCNT(bp) > 0) { 258 nbusy++; 259 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 260 == B_DELWRI) { 261 /* bawrite(bp);*/ 262 nbusy++; 263 } 264 } 265 if (nbusy == 0) 266 break; 267 printf("%d ", nbusy); 268 if (nbusy < pbusy) 269 iter = 0; 270 pbusy = nbusy; 271 sync(&thread0, NULL); 272 if (curthread != NULL) { 273 DROP_GIANT(); 274 for (subiter = 0; subiter < 50 * iter; subiter++) { 275 mtx_lock_spin(&sched_lock); 276 setrunqueue(curthread); 277 curthread->td_proc->p_stats->p_ru.ru_nvcsw++; 278 mi_switch(); /* Allow interrupt threads to run */ 279 mtx_unlock_spin(&sched_lock); 280 DELAY(1000); 281 } 282 PICKUP_GIANT(); 283 } else 284 DELAY(50000 * iter); 285 } 286 printf("\n"); 287 /* 288 * Count only busy local buffers to prevent forcing 289 * a fsck if we're just a client of a wedged NFS server 290 */ 291 nbusy = 0; 292 for (bp = &buf[nbuf]; --bp >= buf; ) { 293 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 294 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 295 if (bp->b_dev == NODEV) { 296 TAILQ_REMOVE(&mountlist, 297 bp->b_vp->v_mount, mnt_list); 298 continue; 299 } 300 nbusy++; 301 #if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 302 printf( 303 "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 304 nbusy, devtoname(bp->b_dev), 305 bp->b_flags, (long)bp->b_blkno, 306 (long)bp->b_lblkno); 307 #endif 308 } 309 } 310 if (nbusy) { 311 /* 312 * Failed to sync all blocks. Indicate this and don't 313 * unmount filesystems (thus forcing an fsck on reboot). 314 */ 315 printf("giving up on %d buffers\n", nbusy); 316 DELAY(5000000); /* 5 seconds */ 317 } else { 318 printf("done\n"); 319 /* 320 * Unmount filesystems 321 */ 322 if (panicstr == 0) 323 vfs_unmountall(); 324 } 325 DELAY(100000); /* wait for console output to finish */ 326 } 327 328 print_uptime(); 329 330 /* 331 * Ok, now do things that assume all filesystem activity has 332 * been completed. 333 */ 334 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 335 splhigh(); 336 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) 337 dumpsys(); 338 339 /* Now that we're going to really halt the system... */ 340 EVENTHANDLER_INVOKE(shutdown_final, howto); 341 342 for(;;) ; /* safety against shutdown_reset not working */ 343 /* NOTREACHED */ 344 } 345 346 /* 347 * If the shutdown was a clean halt, behave accordingly. 348 */ 349 static void 350 shutdown_halt(void *junk, int howto) 351 { 352 if (howto & RB_HALT) { 353 printf("\n"); 354 printf("The operating system has halted.\n"); 355 printf("Please press any key to reboot.\n\n"); 356 switch (cngetc()) { 357 case -1: /* No console, just die */ 358 cpu_halt(); 359 /* NOTREACHED */ 360 default: 361 howto &= ~RB_HALT; 362 break; 363 } 364 } 365 } 366 367 /* 368 * Check to see if the system paniced, pause and then reboot 369 * according to the specified delay. 370 */ 371 static void 372 shutdown_panic(void *junk, int howto) 373 { 374 int loop; 375 376 if (howto & RB_DUMP) { 377 if (PANIC_REBOOT_WAIT_TIME != 0) { 378 if (PANIC_REBOOT_WAIT_TIME != -1) { 379 printf("Automatic reboot in %d seconds - " 380 "press a key on the console to abort\n", 381 PANIC_REBOOT_WAIT_TIME); 382 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 383 loop > 0; --loop) { 384 DELAY(1000 * 100); /* 1/10th second */ 385 /* Did user type a key? */ 386 if (cncheckc() != -1) 387 break; 388 } 389 if (!loop) 390 return; 391 } 392 } else { /* zero time specified - reboot NOW */ 393 return; 394 } 395 printf("--> Press a key on the console to reboot,\n"); 396 printf("--> or switch off the system now.\n"); 397 cngetc(); 398 } 399 } 400 401 /* 402 * Everything done, now reset 403 */ 404 static void 405 shutdown_reset(void *junk, int howto) 406 { 407 printf("Rebooting...\n"); 408 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 409 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 410 cpu_reset(); 411 /* NOTREACHED */ /* assuming reset worked */ 412 } 413 414 /* 415 * Magic number for savecore 416 * 417 * exported (symorder) and used at least by savecore(8) 418 * 419 */ 420 static u_long const dumpmag = 0x8fca0101UL; 421 422 static int dumpsize = 0; /* also for savecore */ 423 424 static int dodump = 1; 425 426 SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 427 "Try to perform coredump on kernel panic"); 428 429 static int 430 setdumpdev(dev_t dev) 431 { 432 int psize; 433 long newdumplo; 434 435 if (dev == NODEV) { 436 dumpdev = dev; 437 return (0); 438 } 439 if (devsw(dev) == NULL) 440 return (ENXIO); /* XXX is this right? */ 441 if (devsw(dev)->d_psize == NULL) 442 return (ENXIO); /* XXX should be ENODEV ? */ 443 psize = devsw(dev)->d_psize(dev); 444 if (psize == -1) 445 return (ENXIO); /* XXX should be ENODEV ? */ 446 /* 447 * XXX should clean up checking in dumpsys() to be more like this. 448 */ 449 newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE); 450 if (newdumplo <= LABELSECTOR) 451 return (ENOSPC); 452 dumpdev = dev; 453 dumplo = newdumplo; 454 return (0); 455 } 456 457 458 /* ARGSUSED */ 459 static void 460 dump_conf(void *dummy) 461 { 462 char *path; 463 dev_t dev; 464 465 path = malloc(MNAMELEN, M_TEMP, M_WAITOK); 466 if (TUNABLE_STR_FETCH("dumpdev", path, MNAMELEN) != 0) { 467 dev = getdiskbyname(path); 468 if (dev != NODEV) 469 dumpdev = dev; 470 } 471 free(path, M_TEMP); 472 if (setdumpdev(dumpdev) != 0) 473 dumpdev = NODEV; 474 } 475 476 SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 477 478 static int 479 sysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 480 { 481 int error; 482 udev_t ndumpdev; 483 484 ndumpdev = dev2udev(dumpdev); 485 error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 486 if (error == 0 && req->newptr != NULL) 487 error = setdumpdev(udev2dev(ndumpdev, 0)); 488 return (error); 489 } 490 491 SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 492 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", ""); 493 494 /* 495 * Doadump comes here after turning off memory management and 496 * getting on the dump stack, either when called above, or by 497 * the auto-restart code. 498 */ 499 static void 500 dumpsys(void) 501 { 502 int error; 503 504 savectx(&dumppcb); 505 if (!dodump) 506 return; 507 if (dumpdev == NODEV) 508 return; 509 if (!(devsw(dumpdev))) 510 return; 511 if (!(devsw(dumpdev)->d_dump)) 512 return; 513 if (dumping++) { 514 dumping--; 515 printf("Dump already in progress, bailing...\n"); 516 return; 517 } 518 dumpsize = Maxmem; 519 printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 520 printf("dump "); 521 error = (*devsw(dumpdev)->d_dump)(dumpdev); 522 dumping--; 523 if (error == 0) { 524 printf("succeeded\n"); 525 return; 526 } 527 printf("failed, reason: "); 528 switch (error) { 529 case ENODEV: 530 printf("device doesn't support a dump routine\n"); 531 break; 532 533 case ENXIO: 534 printf("device bad\n"); 535 break; 536 537 case EFAULT: 538 printf("device not ready\n"); 539 break; 540 541 case EINVAL: 542 printf("area improper\n"); 543 break; 544 545 case EIO: 546 printf("i/o error\n"); 547 break; 548 549 case EINTR: 550 printf("aborted from console\n"); 551 break; 552 553 default: 554 printf("unknown, error = %d\n", error); 555 break; 556 } 557 } 558 559 int 560 dumpstatus(vm_offset_t addr, off_t count) 561 { 562 int c; 563 564 if (addr % (1024 * 1024) == 0) { 565 #ifdef HW_WDOG 566 if (wdog_tickler) 567 (*wdog_tickler)(); 568 #endif 569 printf("%ld ", (long)(count / (1024 * 1024))); 570 } 571 572 if ((c = cncheckc()) == 0x03) 573 return -1; 574 else if (c != -1) 575 printf("[CTRL-C to abort] "); 576 577 return 0; 578 } 579 580 #ifdef SMP 581 static u_int panic_cpu = NOCPU; 582 #endif 583 584 /* 585 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 586 * and then reboots. If we are called twice, then we avoid trying to sync 587 * the disks as this often leads to recursive panics. 588 * 589 * MPSAFE 590 */ 591 void 592 panic(const char *fmt, ...) 593 { 594 int bootopt; 595 va_list ap; 596 static char buf[256]; 597 598 #ifdef SMP 599 /* 600 * We don't want multiple CPU's to panic at the same time, so we 601 * use panic_cpu as a simple spinlock. We have to keep checking 602 * panic_cpu if we are spinning in case the panic on the first 603 * CPU is canceled. 604 */ 605 if (panic_cpu != PCPU_GET(cpuid)) 606 while (atomic_cmpset_int(&panic_cpu, NOCPU, 607 PCPU_GET(cpuid)) == 0) 608 while (panic_cpu != NOCPU) 609 ; /* nothing */ 610 #endif 611 612 bootopt = RB_AUTOBOOT | RB_DUMP; 613 if (panicstr) 614 bootopt |= RB_NOSYNC; 615 else 616 panicstr = fmt; 617 618 va_start(ap, fmt); 619 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 620 if (panicstr == fmt) 621 panicstr = buf; 622 va_end(ap); 623 printf("panic: %s\n", buf); 624 #ifdef SMP 625 /* two separate prints in case of an unmapped page and trap */ 626 printf("cpuid = %d; ", PCPU_GET(cpuid)); 627 #ifdef APIC_IO 628 printf("lapic.id = %08x\n", lapic.id); 629 #endif 630 #endif 631 632 #if defined(DDB) 633 if (debugger_on_panic) 634 Debugger ("panic"); 635 #ifdef RESTARTABLE_PANICS 636 /* See if the user aborted the panic, in which case we continue. */ 637 if (panicstr == NULL) { 638 #ifdef SMP 639 atomic_store_rel_int(&panic_cpu, NOCPU); 640 #endif 641 return; 642 } 643 #endif 644 #endif 645 if (!sync_on_panic) 646 bootopt |= RB_NOSYNC; 647 boot(bootopt); 648 } 649 650 /* 651 * Support for poweroff delay. 652 */ 653 #ifndef POWEROFF_DELAY 654 # define POWEROFF_DELAY 5000 655 #endif 656 static int poweroff_delay = POWEROFF_DELAY; 657 658 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 659 &poweroff_delay, 0, ""); 660 661 static void 662 poweroff_wait(void *junk, int howto) 663 { 664 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 665 return; 666 DELAY(poweroff_delay * 1000); 667 } 668 669 /* 670 * Some system processes (e.g. syncer) need to be stopped at appropriate 671 * points in their main loops prior to a system shutdown, so that they 672 * won't interfere with the shutdown process (e.g. by holding a disk buf 673 * to cause sync to fail). For each of these system processes, register 674 * shutdown_kproc() as a handler for one of shutdown events. 675 */ 676 static int kproc_shutdown_wait = 60; 677 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 678 &kproc_shutdown_wait, 0, ""); 679 680 void 681 kproc_shutdown(void *arg, int howto) 682 { 683 struct proc *p; 684 int error; 685 686 if (panicstr) 687 return; 688 689 p = (struct proc *)arg; 690 printf("Waiting (max %d seconds) for system process `%s' to stop...", 691 kproc_shutdown_wait, p->p_comm); 692 error = kthread_suspend(p, kproc_shutdown_wait * hz); 693 694 if (error == EWOULDBLOCK) 695 printf("timed out\n"); 696 else 697 printf("stopped\n"); 698 } 699