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