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