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