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