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 #ifndef PREEMPTION 273 int subiter; 274 #endif 275 276 waittime = 0; 277 printf("\nsyncing disks, buffers remaining... "); 278 279 sync(&thread0, NULL); 280 281 /* 282 * With soft updates, some buffers that are 283 * written will be remarked as dirty until other 284 * buffers are written. 285 */ 286 for (iter = pbusy = 0; iter < 20; iter++) { 287 nbusy = 0; 288 for (bp = &buf[nbuf]; --bp >= buf; ) { 289 if ((bp->b_flags & B_INVAL) == 0 && 290 BUF_REFCNT(bp) > 0) { 291 nbusy++; 292 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 293 == B_DELWRI) { 294 /* bawrite(bp);*/ 295 nbusy++; 296 } 297 } 298 if (nbusy == 0) 299 break; 300 printf("%d ", nbusy); 301 if (nbusy < pbusy) 302 iter = 0; 303 pbusy = nbusy; 304 sync(&thread0, NULL); 305 306 #ifdef PREEMPTION 307 /* 308 * Drop Giant and spin for a while to allow 309 * interrupt threads to run. 310 */ 311 DROP_GIANT(); 312 DELAY(50000 * iter); 313 PICKUP_GIANT(); 314 #else 315 /* 316 * Drop Giant and context switch several times to 317 * allow interrupt threads to run. 318 */ 319 DROP_GIANT(); 320 for (subiter = 0; subiter < 50 * iter; subiter++) { 321 mtx_lock_spin(&sched_lock); 322 mi_switch(SW_VOL, NULL); 323 mtx_unlock_spin(&sched_lock); 324 DELAY(1000); 325 } 326 PICKUP_GIANT(); 327 #endif 328 } 329 printf("\n"); 330 /* 331 * Count only busy local buffers to prevent forcing 332 * a fsck if we're just a client of a wedged NFS server 333 */ 334 nbusy = 0; 335 for (bp = &buf[nbuf]; --bp >= buf; ) { 336 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 337 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 338 if (bp->b_dev == NULL) { 339 TAILQ_REMOVE(&mountlist, 340 bp->b_vp->v_mount, mnt_list); 341 continue; 342 } 343 nbusy++; 344 #if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 345 printf( 346 "%d: dev:%s, flags:%0x, blkno:%ld, lblkno:%ld\n", 347 nbusy, devtoname(bp->b_dev), 348 bp->b_flags, (long)bp->b_blkno, 349 (long)bp->b_lblkno); 350 #endif 351 } 352 } 353 if (nbusy) { 354 /* 355 * Failed to sync all blocks. Indicate this and don't 356 * unmount filesystems (thus forcing an fsck on reboot). 357 */ 358 printf("giving up on %d buffers\n", nbusy); 359 DELAY(5000000); /* 5 seconds */ 360 } else { 361 printf("done\n"); 362 /* 363 * Unmount filesystems 364 */ 365 if (panicstr == 0) 366 vfs_unmountall(); 367 } 368 DELAY(100000); /* wait for console output to finish */ 369 } 370 371 print_uptime(); 372 373 /* 374 * Ok, now do things that assume all filesystem activity has 375 * been completed. 376 */ 377 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 378 splhigh(); 379 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && 380 !cold && dumper.dumper != NULL && !dumping) 381 doadump(); 382 383 /* Now that we're going to really halt the system... */ 384 EVENTHANDLER_INVOKE(shutdown_final, howto); 385 386 for(;;) ; /* safety against shutdown_reset not working */ 387 /* NOTREACHED */ 388 } 389 390 /* 391 * If the shutdown was a clean halt, behave accordingly. 392 */ 393 static void 394 shutdown_halt(void *junk, int howto) 395 { 396 397 if (howto & RB_HALT) { 398 printf("\n"); 399 printf("The operating system has halted.\n"); 400 printf("Please press any key to reboot.\n\n"); 401 switch (cngetc()) { 402 case -1: /* No console, just die */ 403 cpu_halt(); 404 /* NOTREACHED */ 405 default: 406 howto &= ~RB_HALT; 407 break; 408 } 409 } 410 } 411 412 /* 413 * Check to see if the system paniced, pause and then reboot 414 * according to the specified delay. 415 */ 416 static void 417 shutdown_panic(void *junk, int howto) 418 { 419 int loop; 420 421 if (howto & RB_DUMP) { 422 if (PANIC_REBOOT_WAIT_TIME != 0) { 423 if (PANIC_REBOOT_WAIT_TIME != -1) { 424 printf("Automatic reboot in %d seconds - " 425 "press a key on the console to abort\n", 426 PANIC_REBOOT_WAIT_TIME); 427 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 428 loop > 0; --loop) { 429 DELAY(1000 * 100); /* 1/10th second */ 430 /* Did user type a key? */ 431 if (cncheckc() != -1) 432 break; 433 } 434 if (!loop) 435 return; 436 } 437 } else { /* zero time specified - reboot NOW */ 438 return; 439 } 440 printf("--> Press a key on the console to reboot,\n"); 441 printf("--> or switch off the system now.\n"); 442 cngetc(); 443 } 444 } 445 446 /* 447 * Everything done, now reset 448 */ 449 static void 450 shutdown_reset(void *junk, int howto) 451 { 452 453 printf("Rebooting...\n"); 454 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 455 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 456 cpu_reset(); 457 /* NOTREACHED */ /* assuming reset worked */ 458 } 459 460 /* 461 * Print a backtrace if we can. 462 */ 463 464 void 465 backtrace(void) 466 { 467 468 #ifdef DDB 469 printf("Stack backtrace:\n"); 470 db_print_backtrace(); 471 #else 472 printf("Sorry, need DDB option to print backtrace"); 473 #endif 474 } 475 476 #ifdef SMP 477 static u_int panic_cpu = NOCPU; 478 #endif 479 480 /* 481 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 482 * and then reboots. If we are called twice, then we avoid trying to sync 483 * the disks as this often leads to recursive panics. 484 * 485 * MPSAFE 486 */ 487 void 488 panic(const char *fmt, ...) 489 { 490 struct thread *td = curthread; 491 int bootopt, newpanic; 492 va_list ap; 493 static char buf[256]; 494 495 #ifdef SMP 496 /* 497 * We don't want multiple CPU's to panic at the same time, so we 498 * use panic_cpu as a simple spinlock. We have to keep checking 499 * panic_cpu if we are spinning in case the panic on the first 500 * CPU is canceled. 501 */ 502 if (panic_cpu != PCPU_GET(cpuid)) 503 while (atomic_cmpset_int(&panic_cpu, NOCPU, 504 PCPU_GET(cpuid)) == 0) 505 while (panic_cpu != NOCPU) 506 ; /* nothing */ 507 #endif 508 509 bootopt = RB_AUTOBOOT | RB_DUMP; 510 newpanic = 0; 511 if (panicstr) 512 bootopt |= RB_NOSYNC; 513 else { 514 panicstr = fmt; 515 newpanic = 1; 516 } 517 518 va_start(ap, fmt); 519 if (newpanic) { 520 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 521 panicstr = buf; 522 printf("panic: %s\n", buf); 523 } else { 524 printf("panic: "); 525 vprintf(fmt, ap); 526 printf("\n"); 527 } 528 va_end(ap); 529 #ifdef SMP 530 /* two separate prints in case of an unmapped page and trap */ 531 printf("cpuid = %d; ", PCPU_GET(cpuid)); 532 #ifdef APIC_IO 533 printf("lapic.id = %08x\n", lapic.id); 534 #else 535 printf("\n"); 536 #endif 537 #endif 538 539 #if defined(DDB) 540 if (newpanic && trace_on_panic) 541 backtrace(); 542 if (debugger_on_panic) 543 Debugger ("panic"); 544 #ifdef RESTARTABLE_PANICS 545 /* See if the user aborted the panic, in which case we continue. */ 546 if (panicstr == NULL) { 547 #ifdef SMP 548 atomic_store_rel_int(&panic_cpu, NOCPU); 549 #endif 550 return; 551 } 552 #endif 553 #endif 554 mtx_lock_spin(&sched_lock); 555 td->td_flags |= TDF_INPANIC; 556 mtx_unlock_spin(&sched_lock); 557 if (!sync_on_panic) 558 bootopt |= RB_NOSYNC; 559 boot(bootopt); 560 } 561 562 /* 563 * Support for poweroff delay. 564 */ 565 #ifndef POWEROFF_DELAY 566 # define POWEROFF_DELAY 5000 567 #endif 568 static int poweroff_delay = POWEROFF_DELAY; 569 570 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 571 &poweroff_delay, 0, ""); 572 573 static void 574 poweroff_wait(void *junk, int howto) 575 { 576 577 if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 578 return; 579 DELAY(poweroff_delay * 1000); 580 } 581 582 /* 583 * Some system processes (e.g. syncer) need to be stopped at appropriate 584 * points in their main loops prior to a system shutdown, so that they 585 * won't interfere with the shutdown process (e.g. by holding a disk buf 586 * to cause sync to fail). For each of these system processes, register 587 * shutdown_kproc() as a handler for one of shutdown events. 588 */ 589 static int kproc_shutdown_wait = 60; 590 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 591 &kproc_shutdown_wait, 0, ""); 592 593 void 594 kproc_shutdown(void *arg, int howto) 595 { 596 struct proc *p; 597 int error; 598 599 if (panicstr) 600 return; 601 602 p = (struct proc *)arg; 603 printf("Waiting (max %d seconds) for system process `%s' to stop...", 604 kproc_shutdown_wait, p->p_comm); 605 error = kthread_suspend(p, kproc_shutdown_wait * hz); 606 607 if (error == EWOULDBLOCK) 608 printf("timed out\n"); 609 else 610 printf("stopped\n"); 611 } 612 613 /* Registration of dumpers */ 614 int 615 set_dumper(struct dumperinfo *di) 616 { 617 618 if (di == NULL) { 619 bzero(&dumper, sizeof dumper); 620 return (0); 621 } 622 if (dumper.dumper != NULL) 623 return (EBUSY); 624 dumper = *di; 625 return (0); 626 } 627 628 #if defined(__powerpc__) 629 void 630 dumpsys(struct dumperinfo *di __unused) 631 { 632 633 printf("Kernel dumps not implemented on this architecture\n"); 634 } 635 #endif 636