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