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 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_ddb.h" 41 #include "opt_ddb_trace.h" 42 #include "opt_ddb_unattended.h" 43 #include "opt_hw_wdog.h" 44 #include "opt_mac.h" 45 #include "opt_panic.h" 46 #include "opt_show_busybufs.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/bio.h> 51 #include <sys/buf.h> 52 #include <sys/conf.h> 53 #include <sys/cons.h> 54 #include <sys/eventhandler.h> 55 #include <sys/kernel.h> 56 #include <sys/kthread.h> 57 #include <sys/mac.h> 58 #include <sys/malloc.h> 59 #include <sys/mount.h> 60 #include <sys/proc.h> 61 #include <sys/reboot.h> 62 #include <sys/resourcevar.h> 63 #include <sys/smp.h> /* smp_active */ 64 #include <sys/sysctl.h> 65 #include <sys/sysproto.h> 66 #include <sys/vnode.h> 67 68 #include <machine/cpu.h> 69 #include <machine/pcb.h> 70 #include <machine/smp.h> 71 72 #include <sys/signalvar.h> 73 #ifdef DDB 74 #include <ddb/ddb.h> 75 #endif 76 77 #ifndef PANIC_REBOOT_WAIT_TIME 78 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 79 #endif 80 81 /* 82 * Note that stdarg.h and the ANSI style va_start macro is used for both 83 * ANSI and traditional C compilers. 84 */ 85 #include <machine/stdarg.h> 86 87 #ifdef DDB 88 #ifdef DDB_UNATTENDED 89 int debugger_on_panic = 0; 90 #else 91 int debugger_on_panic = 1; 92 #endif 93 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 94 &debugger_on_panic, 0, "Run debugger on kernel panic"); 95 96 #ifdef DDB_TRACE 97 int trace_on_panic = 1; 98 #else 99 int trace_on_panic = 0; 100 #endif 101 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW, 102 &trace_on_panic, 0, "Print stack trace on kernel panic"); 103 #endif 104 105 int sync_on_panic = 1; 106 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW, 107 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 108 109 SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 110 111 #ifdef HW_WDOG 112 /* 113 * If there is a hardware watchdog, point this at the function needed to 114 * hold it off. 115 * It's needed when the kernel needs to do some lengthy operations. 116 * e.g. in wd.c when dumping core.. It's most annoying to have 117 * your precious core-dump only half written because the wdog kicked in. 118 */ 119 watchdog_tickle_fn wdog_tickler = NULL; 120 #endif /* HW_WDOG */ 121 122 /* 123 * Variable panicstr contains argument to first call to panic; used as flag 124 * to indicate that the kernel has already called panic. 125 */ 126 const char *panicstr; 127 128 int dumping; /* system is dumping */ 129 static struct dumperinfo dumper; /* our selected dumper */ 130 static struct pcb dumppcb; /* "You Are Here" sign for dump-debuggers */ 131 132 static void boot(int) __dead2; 133 static void poweroff_wait(void *, int); 134 static void shutdown_halt(void *junk, int howto); 135 static void shutdown_panic(void *junk, int howto); 136 static void shutdown_reset(void *junk, int howto); 137 138 /* register various local shutdown events */ 139 static void 140 shutdown_conf(void *unused) 141 { 142 143 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 144 SHUTDOWN_PRI_FIRST); 145 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 146 SHUTDOWN_PRI_LAST + 100); 147 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 148 SHUTDOWN_PRI_LAST + 100); 149 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 150 SHUTDOWN_PRI_LAST + 200); 151 } 152 153 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 154 155 /* 156 * The system call that results in a reboot 157 * 158 * MPSAFE 159 */ 160 /* ARGSUSED */ 161 int 162 reboot(struct thread *td, struct reboot_args *uap) 163 { 164 int error; 165 166 error = 0; 167 #ifdef MAC 168 error = mac_check_system_reboot(td->td_ucred, uap->opt); 169 #endif 170 if (error == 0) 171 error = suser(td); 172 if (error == 0) { 173 mtx_lock(&Giant); 174 boot(uap->opt); 175 mtx_unlock(&Giant); 176 } 177 return (error); 178 } 179 180 /* 181 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 182 */ 183 static int shutdown_howto = 0; 184 185 void 186 shutdown_nice(int howto) 187 { 188 189 shutdown_howto = howto; 190 191 /* Send a signal to init(8) and have it shutdown the world */ 192 if (initproc != NULL) { 193 PROC_LOCK(initproc); 194 psignal(initproc, SIGINT); 195 PROC_UNLOCK(initproc); 196 } else { 197 /* No init(8) running, so simply reboot */ 198 boot(RB_NOSYNC); 199 } 200 return; 201 } 202 static int waittime = -1; 203 204 static void 205 print_uptime(void) 206 { 207 int f; 208 struct timespec ts; 209 210 getnanouptime(&ts); 211 printf("Uptime: "); 212 f = 0; 213 if (ts.tv_sec >= 86400) { 214 printf("%ldd", (long)ts.tv_sec / 86400); 215 ts.tv_sec %= 86400; 216 f = 1; 217 } 218 if (f || ts.tv_sec >= 3600) { 219 printf("%ldh", (long)ts.tv_sec / 3600); 220 ts.tv_sec %= 3600; 221 f = 1; 222 } 223 if (f || ts.tv_sec >= 60) { 224 printf("%ldm", (long)ts.tv_sec / 60); 225 ts.tv_sec %= 60; 226 f = 1; 227 } 228 printf("%lds\n", (long)ts.tv_sec); 229 } 230 231 static void 232 doadump(void) 233 { 234 235 savectx(&dumppcb); 236 dumping++; 237 dumpsys(&dumper); 238 } 239 240 /* 241 * Go through the rigmarole of shutting down.. 242 * this used to be in machdep.c but I'll be dammned if I could see 243 * anything machine dependant in it. 244 */ 245 static void 246 boot(int howto) 247 { 248 249 /* collect extra flags that shutdown_nice might have set */ 250 howto |= shutdown_howto; 251 252 #ifdef DDB 253 /* We are out of the debugger now. */ 254 db_active = 0; 255 #endif 256 257 #ifdef SMP 258 if (smp_active) 259 printf("boot() called on cpu#%d\n", PCPU_GET(cpuid)); 260 #endif 261 /* 262 * Do any callouts that should be done BEFORE syncing the filesystems. 263 */ 264 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 265 266 /* 267 * Now sync filesystems 268 */ 269 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 270 register struct buf *bp; 271 int iter, nbusy, pbusy; 272 int subiter; 273 274 waittime = 0; 275 printf("\nsyncing disks, buffers remaining... "); 276 277 sync(&thread0, NULL); 278 279 /* 280 * With soft updates, some buffers that are 281 * written will be remarked as dirty until other 282 * buffers are written. 283 */ 284 for (iter = pbusy = 0; iter < 20; iter++) { 285 nbusy = 0; 286 for (bp = &buf[nbuf]; --bp >= buf; ) { 287 if ((bp->b_flags & B_INVAL) == 0 && 288 BUF_REFCNT(bp) > 0) { 289 nbusy++; 290 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 291 == B_DELWRI) { 292 /* bawrite(bp);*/ 293 nbusy++; 294 } 295 } 296 if (nbusy == 0) 297 break; 298 printf("%d ", nbusy); 299 if (nbusy < pbusy) 300 iter = 0; 301 pbusy = nbusy; 302 sync(&thread0, NULL); 303 if (curthread != NULL) { 304 DROP_GIANT(); 305 for (subiter = 0; subiter < 50 * iter; subiter++) { 306 mtx_lock_spin(&sched_lock); 307 /* 308 * Allow interrupt threads to run 309 */ 310 mi_switch(SW_VOL); 311 mtx_unlock_spin(&sched_lock); 312 DELAY(1000); 313 } 314 PICKUP_GIANT(); 315 } else 316 DELAY(50000 * iter); 317 } 318 printf("\n"); 319 /* 320 * Count only busy local buffers to prevent forcing 321 * a fsck if we're just a client of a wedged NFS server 322 */ 323 nbusy = 0; 324 for (bp = &buf[nbuf]; --bp >= buf; ) { 325 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 326 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 327 if (bp->b_dev == NULL) { 328 TAILQ_REMOVE(&mountlist, 329 bp->b_vp->v_mount, mnt_list); 330 continue; 331 } 332 nbusy++; 333 #if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 334 printf( 335 "%d: dev:%s, flags:%0x, blkno:%ld, lblkno:%ld\n", 336 nbusy, devtoname(bp->b_dev), 337 bp->b_flags, (long)bp->b_blkno, 338 (long)bp->b_lblkno); 339 #endif 340 } 341 } 342 if (nbusy) { 343 /* 344 * Failed to sync all blocks. Indicate this and don't 345 * unmount filesystems (thus forcing an fsck on reboot). 346 */ 347 printf("giving up on %d buffers\n", nbusy); 348 DELAY(5000000); /* 5 seconds */ 349 } else { 350 printf("done\n"); 351 /* 352 * Unmount filesystems 353 */ 354 if (panicstr == 0) 355 vfs_unmountall(); 356 } 357 DELAY(100000); /* wait for console output to finish */ 358 } 359 360 print_uptime(); 361 362 /* 363 * Ok, now do things that assume all filesystem activity has 364 * been completed. 365 */ 366 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 367 splhigh(); 368 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && 369 !cold && dumper.dumper != NULL && !dumping) 370 doadump(); 371 372 /* Now that we're going to really halt the system... */ 373 EVENTHANDLER_INVOKE(shutdown_final, howto); 374 375 for(;;) ; /* safety against shutdown_reset not working */ 376 /* NOTREACHED */ 377 } 378 379 /* 380 * If the shutdown was a clean halt, behave accordingly. 381 */ 382 static void 383 shutdown_halt(void *junk, int howto) 384 { 385 386 if (howto & RB_HALT) { 387 printf("\n"); 388 printf("The operating system has halted.\n"); 389 printf("Please press any key to reboot.\n\n"); 390 switch (cngetc()) { 391 case -1: /* No console, just die */ 392 cpu_halt(); 393 /* NOTREACHED */ 394 default: 395 howto &= ~RB_HALT; 396 break; 397 } 398 } 399 } 400 401 /* 402 * Check to see if the system paniced, pause and then reboot 403 * according to the specified delay. 404 */ 405 static void 406 shutdown_panic(void *junk, int howto) 407 { 408 int loop; 409 410 if (howto & RB_DUMP) { 411 if (PANIC_REBOOT_WAIT_TIME != 0) { 412 if (PANIC_REBOOT_WAIT_TIME != -1) { 413 printf("Automatic reboot in %d seconds - " 414 "press a key on the console to abort\n", 415 PANIC_REBOOT_WAIT_TIME); 416 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 417 loop > 0; --loop) { 418 DELAY(1000 * 100); /* 1/10th second */ 419 /* Did user type a key? */ 420 if (cncheckc() != -1) 421 break; 422 } 423 if (!loop) 424 return; 425 } 426 } else { /* zero time specified - reboot NOW */ 427 return; 428 } 429 printf("--> Press a key on the console to reboot,\n"); 430 printf("--> or switch off the system now.\n"); 431 cngetc(); 432 } 433 } 434 435 /* 436 * Everything done, now reset 437 */ 438 static void 439 shutdown_reset(void *junk, int howto) 440 { 441 442 printf("Rebooting...\n"); 443 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 444 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 445 cpu_reset(); 446 /* NOTREACHED */ /* assuming reset worked */ 447 } 448 449 /* 450 * Print a backtrace if we can. 451 */ 452 453 void 454 backtrace(void) 455 { 456 457 #ifdef DDB 458 printf("Stack backtrace:\n"); 459 db_print_backtrace(); 460 #else 461 printf("Sorry, need DDB option to print backtrace"); 462 #endif 463 } 464 465 #ifdef SMP 466 static u_int panic_cpu = NOCPU; 467 #endif 468 469 /* 470 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 471 * and then reboots. If we are called twice, then we avoid trying to sync 472 * the disks as this often leads to recursive panics. 473 * 474 * MPSAFE 475 */ 476 void 477 panic(const char *fmt, ...) 478 { 479 struct thread *td = curthread; 480 int bootopt, newpanic; 481 va_list ap; 482 static char buf[256]; 483 484 #ifdef SMP 485 /* 486 * We don't want multiple CPU's to panic at the same time, so we 487 * use panic_cpu as a simple spinlock. We have to keep checking 488 * panic_cpu if we are spinning in case the panic on the first 489 * CPU is canceled. 490 */ 491 if (panic_cpu != PCPU_GET(cpuid)) 492 while (atomic_cmpset_int(&panic_cpu, NOCPU, 493 PCPU_GET(cpuid)) == 0) 494 while (panic_cpu != NOCPU) 495 ; /* nothing */ 496 #endif 497 498 bootopt = RB_AUTOBOOT | RB_DUMP; 499 newpanic = 0; 500 if (panicstr) 501 bootopt |= RB_NOSYNC; 502 else { 503 panicstr = fmt; 504 newpanic = 1; 505 } 506 507 va_start(ap, fmt); 508 if (newpanic) { 509 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 510 panicstr = buf; 511 printf("panic: %s\n", buf); 512 } else { 513 printf("panic: "); 514 vprintf(fmt, ap); 515 printf("\n"); 516 } 517 va_end(ap); 518 #ifdef SMP 519 /* two separate prints in case of an unmapped page and trap */ 520 printf("cpuid = %d; ", PCPU_GET(cpuid)); 521 #ifdef APIC_IO 522 printf("lapic.id = %08x\n", lapic.id); 523 #else 524 printf("\n"); 525 #endif 526 #endif 527 528 #if defined(DDB) 529 if (newpanic && trace_on_panic) 530 backtrace(); 531 if (debugger_on_panic) 532 Debugger ("panic"); 533 #ifdef RESTARTABLE_PANICS 534 /* See if the user aborted the panic, in which case we continue. */ 535 if (panicstr == NULL) { 536 #ifdef SMP 537 atomic_store_rel_int(&panic_cpu, NOCPU); 538 #endif 539 return; 540 } 541 #endif 542 #endif 543 mtx_lock_spin(&sched_lock); 544 td->td_flags |= TDF_INPANIC; 545 mtx_unlock_spin(&sched_lock); 546 if (!sync_on_panic) 547 bootopt |= RB_NOSYNC; 548 boot(bootopt); 549 } 550 551 /* 552 * Support for poweroff delay. 553 */ 554 #ifndef POWEROFF_DELAY 555 # define POWEROFF_DELAY 5000 556 #endif 557 static int poweroff_delay = POWEROFF_DELAY; 558 559 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 560 &poweroff_delay, 0, ""); 561 562 static void 563 poweroff_wait(void *junk, int howto) 564 { 565 566 if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 567 return; 568 DELAY(poweroff_delay * 1000); 569 } 570 571 /* 572 * Some system processes (e.g. syncer) need to be stopped at appropriate 573 * points in their main loops prior to a system shutdown, so that they 574 * won't interfere with the shutdown process (e.g. by holding a disk buf 575 * to cause sync to fail). For each of these system processes, register 576 * shutdown_kproc() as a handler for one of shutdown events. 577 */ 578 static int kproc_shutdown_wait = 60; 579 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 580 &kproc_shutdown_wait, 0, ""); 581 582 void 583 kproc_shutdown(void *arg, int howto) 584 { 585 struct proc *p; 586 int error; 587 588 if (panicstr) 589 return; 590 591 p = (struct proc *)arg; 592 printf("Waiting (max %d seconds) for system process `%s' to stop...", 593 kproc_shutdown_wait, p->p_comm); 594 error = kthread_suspend(p, kproc_shutdown_wait * hz); 595 596 if (error == EWOULDBLOCK) 597 printf("timed out\n"); 598 else 599 printf("stopped\n"); 600 } 601 602 /* Registration of dumpers */ 603 int 604 set_dumper(struct dumperinfo *di) 605 { 606 607 if (di == NULL) { 608 bzero(&dumper, sizeof dumper); 609 return (0); 610 } 611 if (dumper.dumper != NULL) 612 return (EBUSY); 613 dumper = *di; 614 return (0); 615 } 616 617 #if defined(__powerpc__) 618 void 619 dumpsys(struct dumperinfo *di __unused) 620 { 621 622 printf("Kernel dumps not implemented on this architecture\n"); 623 } 624 #endif 625