kern_shutdown.c revision 289032
150479Speter/*- 226236Swpaul * Copyright (c) 1986, 1988, 1991, 1993 380029Sobrien * The Regents of the University of California. All rights reserved. 480029Sobrien * (c) UNIX System Laboratories, Inc. 526236Swpaul * All or some portions of this file are derived from material licensed 6139103Sru * to the University of California by American Telephone and Telegraph 734131Sbde * Co. or Unix System Laboratories, Inc. and are reproduced herein with 826236Swpaul * the permission of UNIX System Laboratories, Inc. 926236Swpaul * 1026236Swpaul * Redistribution and use in source and binary forms, with or without 11113091Sobrien * modification, are permitted provided that the following conditions 1280029Sobrien * are met: 1326236Swpaul * 1. Redistributions of source code must retain the above copyright 14201390Sed * notice, this list of conditions and the following disclaimer. 15201390Sed * 2. Redistributions in binary form must reproduce the above copyright 1631779Sbde * notice, this list of conditions and the following disclaimer in the 1731779Sbde * documentation and/or other materials provided with the distribution. 1826236Swpaul * 4. Neither the name of the University nor the names of its contributors 1926236Swpaul * may be used to endorse or promote products derived from this software 2026236Swpaul * without specific prior written permission. 2180029Sobrien * 22231118Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2326236Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2426236Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2526236Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2635910Sbde * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2726236Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2826236Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2926236Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3026236Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3126236Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3226236Swpaul * SUCH DAMAGE. 3326236Swpaul * 3426236Swpaul * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: stable/10/sys/kern/kern_shutdown.c 289032 2015-10-08 15:48:44Z cperciva $"); 39 40#include "opt_ddb.h" 41#include "opt_kdb.h" 42#include "opt_panic.h" 43#include "opt_sched.h" 44#include "opt_watchdog.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/bio.h> 49#include <sys/buf.h> 50#include <sys/conf.h> 51#include <sys/cons.h> 52#include <sys/eventhandler.h> 53#include <sys/jail.h> 54#include <sys/kdb.h> 55#include <sys/kernel.h> 56#include <sys/kerneldump.h> 57#include <sys/kthread.h> 58#include <sys/ktr.h> 59#include <sys/malloc.h> 60#include <sys/mount.h> 61#include <sys/priv.h> 62#include <sys/proc.h> 63#include <sys/reboot.h> 64#include <sys/resourcevar.h> 65#include <sys/rwlock.h> 66#include <sys/sched.h> 67#include <sys/smp.h> 68#include <sys/sysctl.h> 69#include <sys/sysproto.h> 70#include <sys/vnode.h> 71#include <sys/watchdog.h> 72 73#include <ddb/ddb.h> 74 75#include <machine/cpu.h> 76#include <machine/pcb.h> 77#include <machine/smp.h> 78 79#include <security/mac/mac_framework.h> 80 81#include <vm/vm.h> 82#include <vm/vm_object.h> 83#include <vm/vm_page.h> 84#include <vm/vm_pager.h> 85#include <vm/swap_pager.h> 86 87#include <sys/signalvar.h> 88 89#ifndef PANIC_REBOOT_WAIT_TIME 90#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 91#endif 92static int panic_reboot_wait_time = PANIC_REBOOT_WAIT_TIME; 93SYSCTL_INT(_kern, OID_AUTO, panic_reboot_wait_time, CTLFLAG_RW | CTLFLAG_TUN, 94 &panic_reboot_wait_time, 0, 95 "Seconds to wait before rebooting after a panic"); 96TUNABLE_INT("kern.panic_reboot_wait_time", &panic_reboot_wait_time); 97 98/* 99 * Note that stdarg.h and the ANSI style va_start macro is used for both 100 * ANSI and traditional C compilers. 101 */ 102#include <machine/stdarg.h> 103 104#ifdef KDB 105#ifdef KDB_UNATTENDED 106int debugger_on_panic = 0; 107#else 108int debugger_on_panic = 1; 109#endif 110SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, 111 CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN, 112 &debugger_on_panic, 0, "Run debugger on kernel panic"); 113TUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic); 114 115#ifdef KDB_TRACE 116static int trace_on_panic = 1; 117#else 118static int trace_on_panic = 0; 119#endif 120SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, 121 CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN, 122 &trace_on_panic, 0, "Print stack trace on kernel panic"); 123TUNABLE_INT("debug.trace_on_panic", &trace_on_panic); 124#endif /* KDB */ 125 126static int sync_on_panic = 0; 127SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN, 128 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 129TUNABLE_INT("kern.sync_on_panic", &sync_on_panic); 130 131static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, 132 "Shutdown environment"); 133 134#ifndef DIAGNOSTIC 135static int show_busybufs; 136#else 137static int show_busybufs = 1; 138#endif 139SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW, 140 &show_busybufs, 0, ""); 141 142int suspend_blocked = 0; 143SYSCTL_INT(_kern, OID_AUTO, suspend_blocked, CTLFLAG_RW, 144 &suspend_blocked, 0, "Block suspend due to a pending shutdown"); 145 146/* 147 * Variable panicstr contains argument to first call to panic; used as flag 148 * to indicate that the kernel has already called panic. 149 */ 150const char *panicstr; 151 152int dumping; /* system is dumping */ 153int rebooting; /* system is rebooting */ 154static struct dumperinfo dumper; /* our selected dumper */ 155 156/* Context information for dump-debuggers. */ 157static struct pcb dumppcb; /* Registers. */ 158lwpid_t dumptid; /* Thread ID. */ 159 160static void poweroff_wait(void *, int); 161static void shutdown_halt(void *junk, int howto); 162static void shutdown_panic(void *junk, int howto); 163static void shutdown_reset(void *junk, int howto); 164 165/* register various local shutdown events */ 166static void 167shutdown_conf(void *unused) 168{ 169 170 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 171 SHUTDOWN_PRI_FIRST); 172 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 173 SHUTDOWN_PRI_LAST + 100); 174 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 175 SHUTDOWN_PRI_LAST + 100); 176 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 177 SHUTDOWN_PRI_LAST + 200); 178} 179 180SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL); 181 182/* 183 * The system call that results in a reboot. 184 */ 185/* ARGSUSED */ 186int 187sys_reboot(struct thread *td, struct reboot_args *uap) 188{ 189 int error; 190 191 error = 0; 192#ifdef MAC 193 error = mac_system_check_reboot(td->td_ucred, uap->opt); 194#endif 195 if (error == 0) 196 error = priv_check(td, PRIV_REBOOT); 197 if (error == 0) { 198 mtx_lock(&Giant); 199 kern_reboot(uap->opt); 200 mtx_unlock(&Giant); 201 } 202 return (error); 203} 204 205/* 206 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 207 */ 208static int shutdown_howto = 0; 209 210void 211shutdown_nice(int howto) 212{ 213 214 shutdown_howto = howto; 215 216 /* Send a signal to init(8) and have it shutdown the world */ 217 if (initproc != NULL) { 218 PROC_LOCK(initproc); 219 kern_psignal(initproc, SIGINT); 220 PROC_UNLOCK(initproc); 221 } else { 222 /* No init(8) running, so simply reboot */ 223 kern_reboot(RB_NOSYNC); 224 } 225 return; 226} 227static int waittime = -1; 228 229static void 230print_uptime(void) 231{ 232 int f; 233 struct timespec ts; 234 235 getnanouptime(&ts); 236 printf("Uptime: "); 237 f = 0; 238 if (ts.tv_sec >= 86400) { 239 printf("%ldd", (long)ts.tv_sec / 86400); 240 ts.tv_sec %= 86400; 241 f = 1; 242 } 243 if (f || ts.tv_sec >= 3600) { 244 printf("%ldh", (long)ts.tv_sec / 3600); 245 ts.tv_sec %= 3600; 246 f = 1; 247 } 248 if (f || ts.tv_sec >= 60) { 249 printf("%ldm", (long)ts.tv_sec / 60); 250 ts.tv_sec %= 60; 251 f = 1; 252 } 253 printf("%lds\n", (long)ts.tv_sec); 254} 255 256int 257doadump(boolean_t textdump) 258{ 259 boolean_t coredump; 260 261 if (dumping) 262 return (EBUSY); 263 if (dumper.dumper == NULL) 264 return (ENXIO); 265 266 savectx(&dumppcb); 267 dumptid = curthread->td_tid; 268 dumping++; 269 270 coredump = TRUE; 271#ifdef DDB 272 if (textdump && textdump_pending) { 273 coredump = FALSE; 274 textdump_dumpsys(&dumper); 275 } 276#endif 277 if (coredump) 278 dumpsys(&dumper); 279 280 dumping--; 281 return (0); 282} 283 284static int 285isbufbusy(struct buf *bp) 286{ 287 if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 && 288 BUF_ISLOCKED(bp)) || 289 ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI)) 290 return (1); 291 return (0); 292} 293 294/* 295 * Shutdown the system cleanly to prepare for reboot, halt, or power off. 296 */ 297void 298kern_reboot(int howto) 299{ 300 static int first_buf_printf = 1; 301 302#if defined(SMP) 303 /* 304 * Bind us to CPU 0 so that all shutdown code runs there. Some 305 * systems don't shutdown properly (i.e., ACPI power off) if we 306 * run on another processor. 307 */ 308 if (!SCHEDULER_STOPPED()) { 309 thread_lock(curthread); 310 sched_bind(curthread, 0); 311 thread_unlock(curthread); 312 KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0")); 313 } 314#endif 315 /* We're in the process of rebooting. */ 316 rebooting = 1; 317 318 /* collect extra flags that shutdown_nice might have set */ 319 howto |= shutdown_howto; 320 321 /* We are out of the debugger now. */ 322 kdb_active = 0; 323 324 /* 325 * Do any callouts that should be done BEFORE syncing the filesystems. 326 */ 327 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 328 329 /* 330 * Now sync filesystems 331 */ 332 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 333 register struct buf *bp; 334 int iter, nbusy, pbusy; 335#ifndef PREEMPTION 336 int subiter; 337#endif 338 339 waittime = 0; 340 341 wdog_kern_pat(WD_LASTVAL); 342 sys_sync(curthread, NULL); 343 344 /* 345 * With soft updates, some buffers that are 346 * written will be remarked as dirty until other 347 * buffers are written. 348 */ 349 for (iter = pbusy = 0; iter < 20; iter++) { 350 nbusy = 0; 351 for (bp = &buf[nbuf]; --bp >= buf; ) 352 if (isbufbusy(bp)) 353 nbusy++; 354 if (nbusy == 0) { 355 if (first_buf_printf) 356 printf("All buffers synced."); 357 break; 358 } 359 if (first_buf_printf) { 360 printf("Syncing disks, buffers remaining... "); 361 first_buf_printf = 0; 362 } 363 printf("%d ", nbusy); 364 if (nbusy < pbusy) 365 iter = 0; 366 pbusy = nbusy; 367 368 wdog_kern_pat(WD_LASTVAL); 369 sys_sync(curthread, NULL); 370 371#ifdef PREEMPTION 372 /* 373 * Drop Giant and spin for a while to allow 374 * interrupt threads to run. 375 */ 376 DROP_GIANT(); 377 DELAY(50000 * iter); 378 PICKUP_GIANT(); 379#else 380 /* 381 * Drop Giant and context switch several times to 382 * allow interrupt threads to run. 383 */ 384 DROP_GIANT(); 385 for (subiter = 0; subiter < 50 * iter; subiter++) { 386 thread_lock(curthread); 387 mi_switch(SW_VOL, NULL); 388 thread_unlock(curthread); 389 DELAY(1000); 390 } 391 PICKUP_GIANT(); 392#endif 393 } 394 printf("\n"); 395 /* 396 * Count only busy local buffers to prevent forcing 397 * a fsck if we're just a client of a wedged NFS server 398 */ 399 nbusy = 0; 400 for (bp = &buf[nbuf]; --bp >= buf; ) { 401 if (isbufbusy(bp)) { 402#if 0 403/* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */ 404 if (bp->b_dev == NULL) { 405 TAILQ_REMOVE(&mountlist, 406 bp->b_vp->v_mount, mnt_list); 407 continue; 408 } 409#endif 410 nbusy++; 411 if (show_busybufs > 0) { 412 printf( 413 "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:", 414 nbusy, bp, bp->b_vp, bp->b_flags, 415 (intmax_t)bp->b_blkno, 416 (intmax_t)bp->b_lblkno); 417 BUF_LOCKPRINTINFO(bp); 418 if (show_busybufs > 1) 419 vn_printf(bp->b_vp, 420 "vnode content: "); 421 } 422 } 423 } 424 if (nbusy) { 425 /* 426 * Failed to sync all blocks. Indicate this and don't 427 * unmount filesystems (thus forcing an fsck on reboot). 428 */ 429 printf("Giving up on %d buffers\n", nbusy); 430 DELAY(5000000); /* 5 seconds */ 431 } else { 432 if (!first_buf_printf) 433 printf("Final sync complete\n"); 434 /* 435 * Unmount filesystems 436 */ 437 if (panicstr == 0) 438 vfs_unmountall(); 439 } 440 swapoff_all(); 441 DELAY(100000); /* wait for console output to finish */ 442 } 443 444 print_uptime(); 445 446 cngrab(); 447 448 /* 449 * Ok, now do things that assume all filesystem activity has 450 * been completed. 451 */ 452 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 453 454 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 455 doadump(TRUE); 456 457 /* Now that we're going to really halt the system... */ 458 EVENTHANDLER_INVOKE(shutdown_final, howto); 459 460 for(;;) ; /* safety against shutdown_reset not working */ 461 /* NOTREACHED */ 462} 463 464/* 465 * If the shutdown was a clean halt, behave accordingly. 466 */ 467static void 468shutdown_halt(void *junk, int howto) 469{ 470 471 if (howto & RB_HALT) { 472 printf("\n"); 473 printf("The operating system has halted.\n"); 474 printf("Please press any key to reboot.\n\n"); 475 switch (cngetc()) { 476 case -1: /* No console, just die */ 477 cpu_halt(); 478 /* NOTREACHED */ 479 default: 480 howto &= ~RB_HALT; 481 break; 482 } 483 } 484} 485 486/* 487 * Check to see if the system paniced, pause and then reboot 488 * according to the specified delay. 489 */ 490static void 491shutdown_panic(void *junk, int howto) 492{ 493 int loop; 494 495 if (howto & RB_DUMP) { 496 if (panic_reboot_wait_time != 0) { 497 if (panic_reboot_wait_time != -1) { 498 printf("Automatic reboot in %d seconds - " 499 "press a key on the console to abort\n", 500 panic_reboot_wait_time); 501 for (loop = panic_reboot_wait_time * 10; 502 loop > 0; --loop) { 503 DELAY(1000 * 100); /* 1/10th second */ 504 /* Did user type a key? */ 505 if (cncheckc() != -1) 506 break; 507 } 508 if (!loop) 509 return; 510 } 511 } else { /* zero time specified - reboot NOW */ 512 return; 513 } 514 printf("--> Press a key on the console to reboot,\n"); 515 printf("--> or switch off the system now.\n"); 516 cngetc(); 517 } 518} 519 520/* 521 * Everything done, now reset 522 */ 523static void 524shutdown_reset(void *junk, int howto) 525{ 526 527 printf("Rebooting...\n"); 528 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 529 530 /* 531 * Acquiring smp_ipi_mtx here has a double effect: 532 * - it disables interrupts avoiding CPU0 preemption 533 * by fast handlers (thus deadlocking against other CPUs) 534 * - it avoids deadlocks against smp_rendezvous() or, more 535 * generally, threads busy-waiting, with this spinlock held, 536 * and waiting for responses by threads on other CPUs 537 * (ie. smp_tlb_shootdown()). 538 * 539 * For the !SMP case it just needs to handle the former problem. 540 */ 541#ifdef SMP 542 mtx_lock_spin(&smp_ipi_mtx); 543#else 544 spinlock_enter(); 545#endif 546 547 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 548 cpu_reset(); 549 /* NOTREACHED */ /* assuming reset worked */ 550} 551 552#if defined(WITNESS) || defined(INVARIANTS) 553static int kassert_warn_only = 0; 554#ifdef KDB 555static int kassert_do_kdb = 0; 556#endif 557#ifdef KTR 558static int kassert_do_ktr = 0; 559#endif 560static int kassert_do_log = 1; 561static int kassert_log_pps_limit = 4; 562static int kassert_log_mute_at = 0; 563static int kassert_log_panic_at = 0; 564static int kassert_warnings = 0; 565 566SYSCTL_NODE(_debug, OID_AUTO, kassert, CTLFLAG_RW, NULL, "kassert options"); 567 568SYSCTL_INT(_debug_kassert, OID_AUTO, warn_only, CTLFLAG_RW | CTLFLAG_TUN, 569 &kassert_warn_only, 0, 570 "KASSERT triggers a panic (1) or just a warning (0)"); 571TUNABLE_INT("debug.kassert.warn_only", &kassert_warn_only); 572 573#ifdef KDB 574SYSCTL_INT(_debug_kassert, OID_AUTO, do_kdb, CTLFLAG_RW | CTLFLAG_TUN, 575 &kassert_do_kdb, 0, "KASSERT will enter the debugger"); 576TUNABLE_INT("debug.kassert.do_kdb", &kassert_do_kdb); 577#endif 578 579#ifdef KTR 580SYSCTL_UINT(_debug_kassert, OID_AUTO, do_ktr, CTLFLAG_RW | CTLFLAG_TUN, 581 &kassert_do_ktr, 0, 582 "KASSERT does a KTR, set this to the KTRMASK you want"); 583TUNABLE_INT("debug.kassert.do_ktr", &kassert_do_ktr); 584#endif 585 586SYSCTL_INT(_debug_kassert, OID_AUTO, do_log, CTLFLAG_RW | CTLFLAG_TUN, 587 &kassert_do_log, 0, "KASSERT triggers a panic (1) or just a warning (0)"); 588TUNABLE_INT("debug.kassert.do_log", &kassert_do_log); 589 590SYSCTL_INT(_debug_kassert, OID_AUTO, warnings, CTLFLAG_RW | CTLFLAG_TUN, 591 &kassert_warnings, 0, "number of KASSERTs that have been triggered"); 592TUNABLE_INT("debug.kassert.warnings", &kassert_warnings); 593 594SYSCTL_INT(_debug_kassert, OID_AUTO, log_panic_at, CTLFLAG_RW | CTLFLAG_TUN, 595 &kassert_log_panic_at, 0, "max number of KASSERTS before we will panic"); 596TUNABLE_INT("debug.kassert.log_panic_at", &kassert_log_panic_at); 597 598SYSCTL_INT(_debug_kassert, OID_AUTO, log_pps_limit, CTLFLAG_RW | CTLFLAG_TUN, 599 &kassert_log_pps_limit, 0, "limit number of log messages per second"); 600TUNABLE_INT("debug.kassert.log_pps_limit", &kassert_log_pps_limit); 601 602SYSCTL_INT(_debug_kassert, OID_AUTO, log_mute_at, CTLFLAG_RW | CTLFLAG_TUN, 603 &kassert_log_mute_at, 0, "max number of KASSERTS to log"); 604TUNABLE_INT("debug.kassert.log_mute_at", &kassert_log_mute_at); 605 606static int kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS); 607 608SYSCTL_PROC(_debug_kassert, OID_AUTO, kassert, 609 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, NULL, 0, 610 kassert_sysctl_kassert, "I", "set to trigger a test kassert"); 611 612static int 613kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS) 614{ 615 int error, i; 616 617 error = sysctl_wire_old_buffer(req, sizeof(int)); 618 if (error == 0) { 619 i = 0; 620 error = sysctl_handle_int(oidp, &i, 0, req); 621 } 622 if (error != 0 || req->newptr == NULL) 623 return (error); 624 KASSERT(0, ("kassert_sysctl_kassert triggered kassert %d", i)); 625 return (0); 626} 627 628/* 629 * Called by KASSERT, this decides if we will panic 630 * or if we will log via printf and/or ktr. 631 */ 632void 633kassert_panic(const char *fmt, ...) 634{ 635 static char buf[256]; 636 va_list ap; 637 638 va_start(ap, fmt); 639 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 640 va_end(ap); 641 642 /* 643 * panic if we're not just warning, or if we've exceeded 644 * kassert_log_panic_at warnings. 645 */ 646 if (!kassert_warn_only || 647 (kassert_log_panic_at > 0 && 648 kassert_warnings >= kassert_log_panic_at)) { 649 va_start(ap, fmt); 650 vpanic(fmt, ap); 651 /* NORETURN */ 652 } 653#ifdef KTR 654 if (kassert_do_ktr) 655 CTR0(ktr_mask, buf); 656#endif /* KTR */ 657 /* 658 * log if we've not yet met the mute limit. 659 */ 660 if (kassert_do_log && 661 (kassert_log_mute_at == 0 || 662 kassert_warnings < kassert_log_mute_at)) { 663 static struct timeval lasterr; 664 static int curerr; 665 666 if (ppsratecheck(&lasterr, &curerr, kassert_log_pps_limit)) { 667 printf("KASSERT failed: %s\n", buf); 668 kdb_backtrace(); 669 } 670 } 671#ifdef KDB 672 if (kassert_do_kdb) { 673 kdb_enter(KDB_WHY_KASSERT, buf); 674 } 675#endif 676 atomic_add_int(&kassert_warnings, 1); 677} 678#endif 679 680/* 681 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 682 * and then reboots. If we are called twice, then we avoid trying to sync 683 * the disks as this often leads to recursive panics. 684 */ 685void 686panic(const char *fmt, ...) 687{ 688 va_list ap; 689 690 va_start(ap, fmt); 691 vpanic(fmt, ap); 692} 693 694void 695vpanic(const char *fmt, va_list ap) 696{ 697#ifdef SMP 698 cpuset_t other_cpus; 699#endif 700 struct thread *td = curthread; 701 int bootopt, newpanic; 702 static char buf[256]; 703 704 spinlock_enter(); 705 706#ifdef SMP 707 /* 708 * stop_cpus_hard(other_cpus) should prevent multiple CPUs from 709 * concurrently entering panic. Only the winner will proceed 710 * further. 711 */ 712 if (panicstr == NULL && !kdb_active) { 713 other_cpus = all_cpus; 714 CPU_CLR(PCPU_GET(cpuid), &other_cpus); 715 stop_cpus_hard(other_cpus); 716 } 717 718 /* 719 * We set stop_scheduler here and not in the block above, 720 * because we want to ensure that if panic has been called and 721 * stop_scheduler_on_panic is true, then stop_scheduler will 722 * always be set. Even if panic has been entered from kdb. 723 */ 724 td->td_stopsched = 1; 725#endif 726 727 bootopt = RB_AUTOBOOT; 728 newpanic = 0; 729 if (panicstr) 730 bootopt |= RB_NOSYNC; 731 else { 732 bootopt |= RB_DUMP; 733 panicstr = fmt; 734 newpanic = 1; 735 } 736 737 if (newpanic) { 738 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 739 panicstr = buf; 740 cngrab(); 741 printf("panic: %s\n", buf); 742 } else { 743 printf("panic: "); 744 vprintf(fmt, ap); 745 printf("\n"); 746 } 747#ifdef SMP 748 printf("cpuid = %d\n", PCPU_GET(cpuid)); 749#endif 750 751#ifdef KDB 752 if (newpanic && trace_on_panic) 753 kdb_backtrace(); 754 if (debugger_on_panic) 755 kdb_enter(KDB_WHY_PANIC, "panic"); 756#endif 757 /*thread_lock(td); */ 758 td->td_flags |= TDF_INPANIC; 759 /* thread_unlock(td); */ 760 if (!sync_on_panic) 761 bootopt |= RB_NOSYNC; 762 kern_reboot(bootopt); 763} 764 765/* 766 * Support for poweroff delay. 767 * 768 * Please note that setting this delay too short might power off your machine 769 * before the write cache on your hard disk has been flushed, leading to 770 * soft-updates inconsistencies. 771 */ 772#ifndef POWEROFF_DELAY 773# define POWEROFF_DELAY 5000 774#endif 775static int poweroff_delay = POWEROFF_DELAY; 776 777SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 778 &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)"); 779 780static void 781poweroff_wait(void *junk, int howto) 782{ 783 784 if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 785 return; 786 DELAY(poweroff_delay * 1000); 787} 788 789/* 790 * Some system processes (e.g. syncer) need to be stopped at appropriate 791 * points in their main loops prior to a system shutdown, so that they 792 * won't interfere with the shutdown process (e.g. by holding a disk buf 793 * to cause sync to fail). For each of these system processes, register 794 * shutdown_kproc() as a handler for one of shutdown events. 795 */ 796static int kproc_shutdown_wait = 60; 797SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 798 &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process"); 799 800void 801kproc_shutdown(void *arg, int howto) 802{ 803 struct proc *p; 804 int error; 805 806 if (panicstr) 807 return; 808 809 p = (struct proc *)arg; 810 printf("Waiting (max %d seconds) for system process `%s' to stop...", 811 kproc_shutdown_wait, p->p_comm); 812 error = kproc_suspend(p, kproc_shutdown_wait * hz); 813 814 if (error == EWOULDBLOCK) 815 printf("timed out\n"); 816 else 817 printf("done\n"); 818} 819 820void 821kthread_shutdown(void *arg, int howto) 822{ 823 struct thread *td; 824 int error; 825 826 if (panicstr) 827 return; 828 829 td = (struct thread *)arg; 830 printf("Waiting (max %d seconds) for system thread `%s' to stop...", 831 kproc_shutdown_wait, td->td_name); 832 error = kthread_suspend(td, kproc_shutdown_wait * hz); 833 834 if (error == EWOULDBLOCK) 835 printf("timed out\n"); 836 else 837 printf("done\n"); 838} 839 840static char dumpdevname[sizeof(((struct cdev*)NULL)->si_name)]; 841SYSCTL_STRING(_kern_shutdown, OID_AUTO, dumpdevname, CTLFLAG_RD, 842 dumpdevname, 0, "Device for kernel dumps"); 843 844/* Registration of dumpers */ 845int 846set_dumper(struct dumperinfo *di, const char *devname) 847{ 848 size_t wantcopy; 849 850 if (di == NULL) { 851 bzero(&dumper, sizeof dumper); 852 dumpdevname[0] = '\0'; 853 return (0); 854 } 855 if (dumper.dumper != NULL) 856 return (EBUSY); 857 dumper = *di; 858 wantcopy = strlcpy(dumpdevname, devname, sizeof(dumpdevname)); 859 if (wantcopy >= sizeof(dumpdevname)) { 860 printf("set_dumper: device name truncated from '%s' -> '%s'\n", 861 devname, dumpdevname); 862 } 863 return (0); 864} 865 866/* Call dumper with bounds checking. */ 867int 868dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical, 869 off_t offset, size_t length) 870{ 871 872 if (length != 0 && (offset < di->mediaoffset || 873 offset - di->mediaoffset + length > di->mediasize)) { 874 printf("Attempt to write outside dump device boundaries.\n" 875 "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n", 876 (intmax_t)offset, (intmax_t)di->mediaoffset, 877 (uintmax_t)length, (intmax_t)di->mediasize); 878 return (ENOSPC); 879 } 880 return (di->dumper(di->priv, virtual, physical, offset, length)); 881} 882 883void 884mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver, 885 uint64_t dumplen, uint32_t blksz) 886{ 887 888 bzero(kdh, sizeof(*kdh)); 889 strlcpy(kdh->magic, magic, sizeof(kdh->magic)); 890 strlcpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); 891 kdh->version = htod32(KERNELDUMPVERSION); 892 kdh->architectureversion = htod32(archver); 893 kdh->dumplength = htod64(dumplen); 894 kdh->dumptime = htod64(time_second); 895 kdh->blocksize = htod32(blksz); 896 strlcpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname)); 897 strlcpy(kdh->versionstring, version, sizeof(kdh->versionstring)); 898 if (panicstr != NULL) 899 strlcpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); 900 kdh->parity = kerneldump_parity(kdh); 901} 902