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 setrunqueue(curthread); 285 curthread->td_proc->p_stats->p_ru.ru_nvcsw++; 286 mi_switch(); /* Allow interrupt threads to run */ 287 mtx_unlock_spin(&sched_lock); 288 DELAY(1000); 289 } 290 PICKUP_GIANT(); 291 } else 292 DELAY(50000 * iter); 293 } 294 printf("\n"); 295 /* 296 * Count only busy local buffers to prevent forcing 297 * a fsck if we're just a client of a wedged NFS server 298 */ 299 nbusy = 0; 300 for (bp = &buf[nbuf]; --bp >= buf; ) { 301 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 302 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 303 if (bp->b_dev == NODEV) { 304 TAILQ_REMOVE(&mountlist, 305 bp->b_vp->v_mount, mnt_list); 306 continue; 307 } 308 nbusy++; 309 #if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 310 printf( 311 "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 312 nbusy, devtoname(bp->b_dev), 313 bp->b_flags, (long)bp->b_blkno, 314 (long)bp->b_lblkno); 315 #endif 316 } 317 } 318 if (nbusy) { 319 /* 320 * Failed to sync all blocks. Indicate this and don't 321 * unmount filesystems (thus forcing an fsck on reboot). 322 */ 323 printf("giving up on %d buffers\n", nbusy); 324 DELAY(5000000); /* 5 seconds */ 325 } else { 326 printf("done\n"); 327 /* 328 * Unmount filesystems 329 */ 330 if (panicstr == 0) 331 vfs_unmountall(); 332 } 333 DELAY(100000); /* wait for console output to finish */ 334 } 335 336 print_uptime(); 337 338 /* 339 * Ok, now do things that assume all filesystem activity has 340 * been completed. 341 */ 342 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 343 splhigh(); 344 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && 345 !cold && dumper.dumper != NULL && !dumping) 346 doadump(); 347 348 /* Now that we're going to really halt the system... */ 349 EVENTHANDLER_INVOKE(shutdown_final, howto); 350 351 for(;;) ; /* safety against shutdown_reset not working */ 352 /* NOTREACHED */ 353 } 354 355 /* 356 * If the shutdown was a clean halt, behave accordingly. 357 */ 358 static void 359 shutdown_halt(void *junk, int howto) 360 { 361 if (howto & RB_HALT) { 362 printf("\n"); 363 printf("The operating system has halted.\n"); 364 printf("Please press any key to reboot.\n\n"); 365 switch (cngetc()) { 366 case -1: /* No console, just die */ 367 cpu_halt(); 368 /* NOTREACHED */ 369 default: 370 howto &= ~RB_HALT; 371 break; 372 } 373 } 374 } 375 376 /* 377 * Check to see if the system paniced, pause and then reboot 378 * according to the specified delay. 379 */ 380 static void 381 shutdown_panic(void *junk, int howto) 382 { 383 int loop; 384 385 if (howto & RB_DUMP) { 386 if (PANIC_REBOOT_WAIT_TIME != 0) { 387 if (PANIC_REBOOT_WAIT_TIME != -1) { 388 printf("Automatic reboot in %d seconds - " 389 "press a key on the console to abort\n", 390 PANIC_REBOOT_WAIT_TIME); 391 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 392 loop > 0; --loop) { 393 DELAY(1000 * 100); /* 1/10th second */ 394 /* Did user type a key? */ 395 if (cncheckc() != -1) 396 break; 397 } 398 if (!loop) 399 return; 400 } 401 } else { /* zero time specified - reboot NOW */ 402 return; 403 } 404 printf("--> Press a key on the console to reboot,\n"); 405 printf("--> or switch off the system now.\n"); 406 cngetc(); 407 } 408 } 409 410 /* 411 * Everything done, now reset 412 */ 413 static void 414 shutdown_reset(void *junk, int howto) 415 { 416 printf("Rebooting...\n"); 417 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 418 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 419 cpu_reset(); 420 /* NOTREACHED */ /* assuming reset worked */ 421 } 422 423 #ifdef SMP 424 static u_int panic_cpu = NOCPU; 425 #endif 426 427 /* 428 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 429 * and then reboots. If we are called twice, then we avoid trying to sync 430 * the disks as this often leads to recursive panics. 431 * 432 * MPSAFE 433 */ 434 void 435 panic(const char *fmt, ...) 436 { 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 #endif 473 #endif 474 475 #if defined(DDB) 476 if (debugger_on_panic) 477 Debugger ("panic"); 478 #ifdef RESTARTABLE_PANICS 479 /* See if the user aborted the panic, in which case we continue. */ 480 if (panicstr == NULL) { 481 #ifdef SMP 482 atomic_store_rel_int(&panic_cpu, NOCPU); 483 #endif 484 return; 485 } 486 #endif 487 #endif 488 if (!sync_on_panic) 489 bootopt |= RB_NOSYNC; 490 boot(bootopt); 491 } 492 493 /* 494 * Support for poweroff delay. 495 */ 496 #ifndef POWEROFF_DELAY 497 # define POWEROFF_DELAY 5000 498 #endif 499 static int poweroff_delay = POWEROFF_DELAY; 500 501 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 502 &poweroff_delay, 0, ""); 503 504 static void 505 poweroff_wait(void *junk, int howto) 506 { 507 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 508 return; 509 DELAY(poweroff_delay * 1000); 510 } 511 512 /* 513 * Some system processes (e.g. syncer) need to be stopped at appropriate 514 * points in their main loops prior to a system shutdown, so that they 515 * won't interfere with the shutdown process (e.g. by holding a disk buf 516 * to cause sync to fail). For each of these system processes, register 517 * shutdown_kproc() as a handler for one of shutdown events. 518 */ 519 static int kproc_shutdown_wait = 60; 520 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 521 &kproc_shutdown_wait, 0, ""); 522 523 void 524 kproc_shutdown(void *arg, int howto) 525 { 526 struct proc *p; 527 int error; 528 529 if (panicstr) 530 return; 531 532 p = (struct proc *)arg; 533 printf("Waiting (max %d seconds) for system process `%s' to stop...", 534 kproc_shutdown_wait, p->p_comm); 535 error = kthread_suspend(p, kproc_shutdown_wait * hz); 536 537 if (error == EWOULDBLOCK) 538 printf("timed out\n"); 539 else 540 printf("stopped\n"); 541 } 542 543 /* Registration of dumpers */ 544 int 545 set_dumper(struct dumperinfo *di) 546 { 547 if (di == NULL) { 548 bzero(&dumper, sizeof dumper); 549 return (0); 550 } 551 if (dumper.dumper != NULL) 552 return (EBUSY); 553 dumper = *di; 554 return (0); 555 } 556 557 #if defined(__powerpc__) || defined(__sparc64__) 558 void 559 dumpsys(struct dumperinfo *di __unused) 560 { 561 562 printf("Kernel dumps not implemented on this architecture\n"); 563 } 564 #endif 565