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