1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1986, 1988, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "opt_ddb.h" 43 #include "opt_ekcd.h" 44 #include "opt_kdb.h" 45 #include "opt_panic.h" 46 #include "opt_sched.h" 47 #include "opt_watchdog.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/bio.h> 52 #include <sys/buf.h> 53 #include <sys/conf.h> 54 #include <sys/compressor.h> 55 #include <sys/cons.h> 56 #include <sys/eventhandler.h> 57 #include <sys/filedesc.h> 58 #include <sys/jail.h> 59 #include <sys/kdb.h> 60 #include <sys/kernel.h> 61 #include <sys/kerneldump.h> 62 #include <sys/kthread.h> 63 #include <sys/ktr.h> 64 #include <sys/malloc.h> 65 #include <sys/mbuf.h> 66 #include <sys/mount.h> 67 #include <sys/priv.h> 68 #include <sys/proc.h> 69 #include <sys/reboot.h> 70 #include <sys/resourcevar.h> 71 #include <sys/rwlock.h> 72 #include <sys/sched.h> 73 #include <sys/smp.h> 74 #include <sys/sysctl.h> 75 #include <sys/sysproto.h> 76 #include <sys/taskqueue.h> 77 #include <sys/vnode.h> 78 #include <sys/watchdog.h> 79 80 #include <crypto/rijndael/rijndael-api-fst.h> 81 #include <crypto/sha2/sha256.h> 82 83 #include <ddb/ddb.h> 84 85 #include <machine/cpu.h> 86 #include <machine/dump.h> 87 #include <machine/pcb.h> 88 #include <machine/smp.h> 89 90 #include <security/mac/mac_framework.h> 91 92 #include <vm/vm.h> 93 #include <vm/vm_object.h> 94 #include <vm/vm_page.h> 95 #include <vm/vm_pager.h> 96 #include <vm/swap_pager.h> 97 98 #include <sys/signalvar.h> 99 100 static MALLOC_DEFINE(M_DUMPER, "dumper", "dumper block buffer"); 101 102 #ifndef PANIC_REBOOT_WAIT_TIME 103 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 104 #endif 105 static int panic_reboot_wait_time = PANIC_REBOOT_WAIT_TIME; 106 SYSCTL_INT(_kern, OID_AUTO, panic_reboot_wait_time, CTLFLAG_RWTUN, 107 &panic_reboot_wait_time, 0, 108 "Seconds to wait before rebooting after a panic"); 109 110 /* 111 * Note that stdarg.h and the ANSI style va_start macro is used for both 112 * ANSI and traditional C compilers. 113 */ 114 #include <machine/stdarg.h> 115 116 #ifdef KDB 117 #ifdef KDB_UNATTENDED 118 int debugger_on_panic = 0; 119 #else 120 int debugger_on_panic = 1; 121 #endif 122 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, 123 CTLFLAG_RWTUN | CTLFLAG_SECURE, 124 &debugger_on_panic, 0, "Run debugger on kernel panic"); 125 126 #ifdef KDB_TRACE 127 static int trace_on_panic = 1; 128 static bool trace_all_panics = true; 129 #else 130 static int trace_on_panic = 0; 131 static bool trace_all_panics = false; 132 #endif 133 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, 134 CTLFLAG_RWTUN | CTLFLAG_SECURE, 135 &trace_on_panic, 0, "Print stack trace on kernel panic"); 136 SYSCTL_BOOL(_debug, OID_AUTO, trace_all_panics, CTLFLAG_RWTUN, 137 &trace_all_panics, 0, "Print stack traces on secondary kernel panics"); 138 #endif /* KDB */ 139 140 static int sync_on_panic = 0; 141 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RWTUN, 142 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 143 144 static bool poweroff_on_panic = 0; 145 SYSCTL_BOOL(_kern, OID_AUTO, poweroff_on_panic, CTLFLAG_RWTUN, 146 &poweroff_on_panic, 0, "Do a power off instead of a reboot on a panic"); 147 148 static bool powercycle_on_panic = 0; 149 SYSCTL_BOOL(_kern, OID_AUTO, powercycle_on_panic, CTLFLAG_RWTUN, 150 &powercycle_on_panic, 0, "Do a power cycle instead of a reboot on a panic"); 151 152 static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, 153 "Shutdown environment"); 154 155 #ifndef DIAGNOSTIC 156 static int show_busybufs; 157 #else 158 static int show_busybufs = 1; 159 #endif 160 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW, 161 &show_busybufs, 0, ""); 162 163 int suspend_blocked = 0; 164 SYSCTL_INT(_kern, OID_AUTO, suspend_blocked, CTLFLAG_RW, 165 &suspend_blocked, 0, "Block suspend due to a pending shutdown"); 166 167 #ifdef EKCD 168 FEATURE(ekcd, "Encrypted kernel crash dumps support"); 169 170 MALLOC_DEFINE(M_EKCD, "ekcd", "Encrypted kernel crash dumps data"); 171 172 struct kerneldumpcrypto { 173 uint8_t kdc_encryption; 174 uint8_t kdc_iv[KERNELDUMP_IV_MAX_SIZE]; 175 keyInstance kdc_ki; 176 cipherInstance kdc_ci; 177 uint32_t kdc_dumpkeysize; 178 struct kerneldumpkey kdc_dumpkey[]; 179 }; 180 #endif 181 182 struct kerneldumpcomp { 183 uint8_t kdc_format; 184 struct compressor *kdc_stream; 185 uint8_t *kdc_buf; 186 size_t kdc_resid; 187 }; 188 189 static struct kerneldumpcomp *kerneldumpcomp_create(struct dumperinfo *di, 190 uint8_t compression); 191 static void kerneldumpcomp_destroy(struct dumperinfo *di); 192 static int kerneldumpcomp_write_cb(void *base, size_t len, off_t off, void *arg); 193 194 static int kerneldump_gzlevel = 6; 195 SYSCTL_INT(_kern, OID_AUTO, kerneldump_gzlevel, CTLFLAG_RWTUN, 196 &kerneldump_gzlevel, 0, 197 "Kernel crash dump compression level"); 198 199 /* 200 * Variable panicstr contains argument to first call to panic; used as flag 201 * to indicate that the kernel has already called panic. 202 */ 203 const char *panicstr; 204 205 int dumping; /* system is dumping */ 206 int rebooting; /* system is rebooting */ 207 static struct dumperinfo dumper; /* our selected dumper */ 208 209 /* Context information for dump-debuggers. */ 210 static struct pcb dumppcb; /* Registers. */ 211 lwpid_t dumptid; /* Thread ID. */ 212 213 static struct cdevsw reroot_cdevsw = { 214 .d_version = D_VERSION, 215 .d_name = "reroot", 216 }; 217 218 static void poweroff_wait(void *, int); 219 static void shutdown_halt(void *junk, int howto); 220 static void shutdown_panic(void *junk, int howto); 221 static void shutdown_reset(void *junk, int howto); 222 static int kern_reroot(void); 223 224 /* register various local shutdown events */ 225 static void 226 shutdown_conf(void *unused) 227 { 228 229 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 230 SHUTDOWN_PRI_FIRST); 231 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 232 SHUTDOWN_PRI_LAST + 100); 233 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 234 SHUTDOWN_PRI_LAST + 100); 235 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 236 SHUTDOWN_PRI_LAST + 200); 237 } 238 239 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL); 240 241 /* 242 * The only reason this exists is to create the /dev/reroot/ directory, 243 * used by reroot code in init(8) as a mountpoint for tmpfs. 244 */ 245 static void 246 reroot_conf(void *unused) 247 { 248 int error; 249 struct cdev *cdev; 250 251 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &cdev, 252 &reroot_cdevsw, NULL, UID_ROOT, GID_WHEEL, 0600, "reroot/reroot"); 253 if (error != 0) { 254 printf("%s: failed to create device node, error %d", 255 __func__, error); 256 } 257 } 258 259 SYSINIT(reroot_conf, SI_SUB_DEVFS, SI_ORDER_ANY, reroot_conf, NULL); 260 261 /* 262 * The system call that results in a reboot. 263 */ 264 /* ARGSUSED */ 265 int 266 sys_reboot(struct thread *td, struct reboot_args *uap) 267 { 268 int error; 269 270 error = 0; 271 #ifdef MAC 272 error = mac_system_check_reboot(td->td_ucred, uap->opt); 273 #endif 274 if (error == 0) 275 error = priv_check(td, PRIV_REBOOT); 276 if (error == 0) { 277 if (uap->opt & RB_REROOT) 278 error = kern_reroot(); 279 else 280 kern_reboot(uap->opt); 281 } 282 return (error); 283 } 284 285 static void 286 shutdown_nice_task_fn(void *arg, int pending __unused) 287 { 288 int howto; 289 290 howto = (uintptr_t)arg; 291 /* Send a signal to init(8) and have it shutdown the world. */ 292 PROC_LOCK(initproc); 293 if (howto & RB_POWEROFF) 294 kern_psignal(initproc, SIGUSR2); 295 else if (howto & RB_POWERCYCLE) 296 kern_psignal(initproc, SIGWINCH); 297 else if (howto & RB_HALT) 298 kern_psignal(initproc, SIGUSR1); 299 else 300 kern_psignal(initproc, SIGINT); 301 PROC_UNLOCK(initproc); 302 } 303 304 static struct task shutdown_nice_task = TASK_INITIALIZER(0, 305 &shutdown_nice_task_fn, NULL); 306 307 /* 308 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 309 */ 310 void 311 shutdown_nice(int howto) 312 { 313 314 if (initproc != NULL && !SCHEDULER_STOPPED()) { 315 shutdown_nice_task.ta_context = (void *)(uintptr_t)howto; 316 taskqueue_enqueue(taskqueue_fast, &shutdown_nice_task); 317 } else { 318 /* 319 * No init(8) running, or scheduler would not allow it 320 * to run, so simply reboot. 321 */ 322 kern_reboot(howto | RB_NOSYNC); 323 } 324 } 325 326 static void 327 print_uptime(void) 328 { 329 int f; 330 struct timespec ts; 331 332 getnanouptime(&ts); 333 printf("Uptime: "); 334 f = 0; 335 if (ts.tv_sec >= 86400) { 336 printf("%ldd", (long)ts.tv_sec / 86400); 337 ts.tv_sec %= 86400; 338 f = 1; 339 } 340 if (f || ts.tv_sec >= 3600) { 341 printf("%ldh", (long)ts.tv_sec / 3600); 342 ts.tv_sec %= 3600; 343 f = 1; 344 } 345 if (f || ts.tv_sec >= 60) { 346 printf("%ldm", (long)ts.tv_sec / 60); 347 ts.tv_sec %= 60; 348 f = 1; 349 } 350 printf("%lds\n", (long)ts.tv_sec); 351 } 352 353 int 354 doadump(boolean_t textdump) 355 { 356 boolean_t coredump; 357 int error; 358 359 error = 0; 360 if (dumping) 361 return (EBUSY); 362 if (dumper.dumper == NULL) 363 return (ENXIO); 364 365 savectx(&dumppcb); 366 dumptid = curthread->td_tid; 367 dumping++; 368 369 coredump = TRUE; 370 #ifdef DDB 371 if (textdump && textdump_pending) { 372 coredump = FALSE; 373 textdump_dumpsys(&dumper); 374 } 375 #endif 376 if (coredump) 377 error = dumpsys(&dumper); 378 379 dumping--; 380 return (error); 381 } 382 383 /* 384 * Shutdown the system cleanly to prepare for reboot, halt, or power off. 385 */ 386 void 387 kern_reboot(int howto) 388 { 389 static int once = 0; 390 391 /* 392 * Normal paths here don't hold Giant, but we can wind up here 393 * unexpectedly with it held. Drop it now so we don't have to 394 * drop and pick it up elsewhere. The paths it is locking will 395 * never be returned to, and it is preferable to preclude 396 * deadlock than to lock against code that won't ever 397 * continue. 398 */ 399 while (mtx_owned(&Giant)) 400 mtx_unlock(&Giant); 401 402 #if defined(SMP) 403 /* 404 * Bind us to the first CPU so that all shutdown code runs there. Some 405 * systems don't shutdown properly (i.e., ACPI power off) if we 406 * run on another processor. 407 */ 408 if (!SCHEDULER_STOPPED()) { 409 thread_lock(curthread); 410 sched_bind(curthread, CPU_FIRST()); 411 thread_unlock(curthread); 412 KASSERT(PCPU_GET(cpuid) == CPU_FIRST(), 413 ("boot: not running on cpu 0")); 414 } 415 #endif 416 /* We're in the process of rebooting. */ 417 rebooting = 1; 418 419 /* We are out of the debugger now. */ 420 kdb_active = 0; 421 422 /* 423 * Do any callouts that should be done BEFORE syncing the filesystems. 424 */ 425 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 426 427 /* 428 * Now sync filesystems 429 */ 430 if (!cold && (howto & RB_NOSYNC) == 0 && once == 0) { 431 once = 1; 432 bufshutdown(show_busybufs); 433 } 434 435 print_uptime(); 436 437 cngrab(); 438 439 /* 440 * Ok, now do things that assume all filesystem activity has 441 * been completed. 442 */ 443 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 444 445 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 446 doadump(TRUE); 447 448 /* Now that we're going to really halt the system... */ 449 EVENTHANDLER_INVOKE(shutdown_final, howto); 450 451 for(;;) ; /* safety against shutdown_reset not working */ 452 /* NOTREACHED */ 453 } 454 455 /* 456 * The system call that results in changing the rootfs. 457 */ 458 static int 459 kern_reroot(void) 460 { 461 struct vnode *oldrootvnode, *vp; 462 struct mount *mp, *devmp; 463 int error; 464 465 if (curproc != initproc) 466 return (EPERM); 467 468 /* 469 * Mark the filesystem containing currently-running executable 470 * (the temporary copy of init(8)) busy. 471 */ 472 vp = curproc->p_textvp; 473 error = vn_lock(vp, LK_SHARED); 474 if (error != 0) 475 return (error); 476 mp = vp->v_mount; 477 error = vfs_busy(mp, MBF_NOWAIT); 478 if (error != 0) { 479 vfs_ref(mp); 480 VOP_UNLOCK(vp, 0); 481 error = vfs_busy(mp, 0); 482 vn_lock(vp, LK_SHARED | LK_RETRY); 483 vfs_rel(mp); 484 if (error != 0) { 485 VOP_UNLOCK(vp, 0); 486 return (ENOENT); 487 } 488 if (vp->v_iflag & VI_DOOMED) { 489 VOP_UNLOCK(vp, 0); 490 vfs_unbusy(mp); 491 return (ENOENT); 492 } 493 } 494 VOP_UNLOCK(vp, 0); 495 496 /* 497 * Remove the filesystem containing currently-running executable 498 * from the mount list, to prevent it from being unmounted 499 * by vfs_unmountall(), and to avoid confusing vfs_mountroot(). 500 * 501 * Also preserve /dev - forcibly unmounting it could cause driver 502 * reinitialization. 503 */ 504 505 vfs_ref(rootdevmp); 506 devmp = rootdevmp; 507 rootdevmp = NULL; 508 509 mtx_lock(&mountlist_mtx); 510 TAILQ_REMOVE(&mountlist, mp, mnt_list); 511 TAILQ_REMOVE(&mountlist, devmp, mnt_list); 512 mtx_unlock(&mountlist_mtx); 513 514 oldrootvnode = rootvnode; 515 516 /* 517 * Unmount everything except for the two filesystems preserved above. 518 */ 519 vfs_unmountall(); 520 521 /* 522 * Add /dev back; vfs_mountroot() will move it into its new place. 523 */ 524 mtx_lock(&mountlist_mtx); 525 TAILQ_INSERT_HEAD(&mountlist, devmp, mnt_list); 526 mtx_unlock(&mountlist_mtx); 527 rootdevmp = devmp; 528 vfs_rel(rootdevmp); 529 530 /* 531 * Mount the new rootfs. 532 */ 533 vfs_mountroot(); 534 535 /* 536 * Update all references to the old rootvnode. 537 */ 538 mountcheckdirs(oldrootvnode, rootvnode); 539 540 /* 541 * Add the temporary filesystem back and unbusy it. 542 */ 543 mtx_lock(&mountlist_mtx); 544 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 545 mtx_unlock(&mountlist_mtx); 546 vfs_unbusy(mp); 547 548 return (0); 549 } 550 551 /* 552 * If the shutdown was a clean halt, behave accordingly. 553 */ 554 static void 555 shutdown_halt(void *junk, int howto) 556 { 557 558 if (howto & RB_HALT) { 559 printf("\n"); 560 printf("The operating system has halted.\n"); 561 printf("Please press any key to reboot.\n\n"); 562 switch (cngetc()) { 563 case -1: /* No console, just die */ 564 cpu_halt(); 565 /* NOTREACHED */ 566 default: 567 break; 568 } 569 } 570 } 571 572 /* 573 * Check to see if the system paniced, pause and then reboot 574 * according to the specified delay. 575 */ 576 static void 577 shutdown_panic(void *junk, int howto) 578 { 579 int loop; 580 581 if (howto & RB_DUMP) { 582 if (panic_reboot_wait_time != 0) { 583 if (panic_reboot_wait_time != -1) { 584 printf("Automatic reboot in %d seconds - " 585 "press a key on the console to abort\n", 586 panic_reboot_wait_time); 587 for (loop = panic_reboot_wait_time * 10; 588 loop > 0; --loop) { 589 DELAY(1000 * 100); /* 1/10th second */ 590 /* Did user type a key? */ 591 if (cncheckc() != -1) 592 break; 593 } 594 if (!loop) 595 return; 596 } 597 } else { /* zero time specified - reboot NOW */ 598 return; 599 } 600 printf("--> Press a key on the console to reboot,\n"); 601 printf("--> or switch off the system now.\n"); 602 cngetc(); 603 } 604 } 605 606 /* 607 * Everything done, now reset 608 */ 609 static void 610 shutdown_reset(void *junk, int howto) 611 { 612 613 printf("Rebooting...\n"); 614 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 615 616 /* 617 * Acquiring smp_ipi_mtx here has a double effect: 618 * - it disables interrupts avoiding CPU0 preemption 619 * by fast handlers (thus deadlocking against other CPUs) 620 * - it avoids deadlocks against smp_rendezvous() or, more 621 * generally, threads busy-waiting, with this spinlock held, 622 * and waiting for responses by threads on other CPUs 623 * (ie. smp_tlb_shootdown()). 624 * 625 * For the !SMP case it just needs to handle the former problem. 626 */ 627 #ifdef SMP 628 mtx_lock_spin(&smp_ipi_mtx); 629 #else 630 spinlock_enter(); 631 #endif 632 633 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 634 cpu_reset(); 635 /* NOTREACHED */ /* assuming reset worked */ 636 } 637 638 #if defined(WITNESS) || defined(INVARIANT_SUPPORT) 639 static int kassert_warn_only = 0; 640 #ifdef KDB 641 static int kassert_do_kdb = 0; 642 #endif 643 #ifdef KTR 644 static int kassert_do_ktr = 0; 645 #endif 646 static int kassert_do_log = 1; 647 static int kassert_log_pps_limit = 4; 648 static int kassert_log_mute_at = 0; 649 static int kassert_log_panic_at = 0; 650 static int kassert_suppress_in_panic = 0; 651 static int kassert_warnings = 0; 652 653 SYSCTL_NODE(_debug, OID_AUTO, kassert, CTLFLAG_RW, NULL, "kassert options"); 654 655 SYSCTL_INT(_debug_kassert, OID_AUTO, warn_only, CTLFLAG_RWTUN, 656 &kassert_warn_only, 0, 657 "KASSERT triggers a panic (1) or just a warning (0)"); 658 659 #ifdef KDB 660 SYSCTL_INT(_debug_kassert, OID_AUTO, do_kdb, CTLFLAG_RWTUN, 661 &kassert_do_kdb, 0, "KASSERT will enter the debugger"); 662 #endif 663 664 #ifdef KTR 665 SYSCTL_UINT(_debug_kassert, OID_AUTO, do_ktr, CTLFLAG_RWTUN, 666 &kassert_do_ktr, 0, 667 "KASSERT does a KTR, set this to the KTRMASK you want"); 668 #endif 669 670 SYSCTL_INT(_debug_kassert, OID_AUTO, do_log, CTLFLAG_RWTUN, 671 &kassert_do_log, 0, 672 "If warn_only is enabled, log (1) or do not log (0) assertion violations"); 673 674 SYSCTL_INT(_debug_kassert, OID_AUTO, warnings, CTLFLAG_RWTUN, 675 &kassert_warnings, 0, "number of KASSERTs that have been triggered"); 676 677 SYSCTL_INT(_debug_kassert, OID_AUTO, log_panic_at, CTLFLAG_RWTUN, 678 &kassert_log_panic_at, 0, "max number of KASSERTS before we will panic"); 679 680 SYSCTL_INT(_debug_kassert, OID_AUTO, log_pps_limit, CTLFLAG_RWTUN, 681 &kassert_log_pps_limit, 0, "limit number of log messages per second"); 682 683 SYSCTL_INT(_debug_kassert, OID_AUTO, log_mute_at, CTLFLAG_RWTUN, 684 &kassert_log_mute_at, 0, "max number of KASSERTS to log"); 685 686 SYSCTL_INT(_debug_kassert, OID_AUTO, suppress_in_panic, CTLFLAG_RWTUN, 687 &kassert_suppress_in_panic, 0, 688 "KASSERTs will be suppressed while handling a panic"); 689 690 static int kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS); 691 692 SYSCTL_PROC(_debug_kassert, OID_AUTO, kassert, 693 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, NULL, 0, 694 kassert_sysctl_kassert, "I", "set to trigger a test kassert"); 695 696 static int 697 kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS) 698 { 699 int error, i; 700 701 error = sysctl_wire_old_buffer(req, sizeof(int)); 702 if (error == 0) { 703 i = 0; 704 error = sysctl_handle_int(oidp, &i, 0, req); 705 } 706 if (error != 0 || req->newptr == NULL) 707 return (error); 708 KASSERT(0, ("kassert_sysctl_kassert triggered kassert %d", i)); 709 return (0); 710 } 711 712 /* 713 * Called by KASSERT, this decides if we will panic 714 * or if we will log via printf and/or ktr. 715 */ 716 void 717 kassert_panic(const char *fmt, ...) 718 { 719 static char buf[256]; 720 va_list ap; 721 722 va_start(ap, fmt); 723 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 724 va_end(ap); 725 726 /* 727 * If we are suppressing secondary panics, log the warning but do not 728 * re-enter panic/kdb. 729 */ 730 if (panicstr != NULL && kassert_suppress_in_panic) { 731 if (kassert_do_log) { 732 printf("KASSERT failed: %s\n", buf); 733 #ifdef KDB 734 if (trace_all_panics && trace_on_panic) 735 kdb_backtrace(); 736 #endif 737 } 738 return; 739 } 740 741 /* 742 * panic if we're not just warning, or if we've exceeded 743 * kassert_log_panic_at warnings. 744 */ 745 if (!kassert_warn_only || 746 (kassert_log_panic_at > 0 && 747 kassert_warnings >= kassert_log_panic_at)) { 748 va_start(ap, fmt); 749 vpanic(fmt, ap); 750 /* NORETURN */ 751 } 752 #ifdef KTR 753 if (kassert_do_ktr) 754 CTR0(ktr_mask, buf); 755 #endif /* KTR */ 756 /* 757 * log if we've not yet met the mute limit. 758 */ 759 if (kassert_do_log && 760 (kassert_log_mute_at == 0 || 761 kassert_warnings < kassert_log_mute_at)) { 762 static struct timeval lasterr; 763 static int curerr; 764 765 if (ppsratecheck(&lasterr, &curerr, kassert_log_pps_limit)) { 766 printf("KASSERT failed: %s\n", buf); 767 kdb_backtrace(); 768 } 769 } 770 #ifdef KDB 771 if (kassert_do_kdb) { 772 kdb_enter(KDB_WHY_KASSERT, buf); 773 } 774 #endif 775 atomic_add_int(&kassert_warnings, 1); 776 } 777 #endif 778 779 /* 780 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 781 * and then reboots. If we are called twice, then we avoid trying to sync 782 * the disks as this often leads to recursive panics. 783 */ 784 void 785 panic(const char *fmt, ...) 786 { 787 va_list ap; 788 789 va_start(ap, fmt); 790 vpanic(fmt, ap); 791 } 792 793 void 794 vpanic(const char *fmt, va_list ap) 795 { 796 #ifdef SMP 797 cpuset_t other_cpus; 798 #endif 799 struct thread *td = curthread; 800 int bootopt, newpanic; 801 static char buf[256]; 802 803 spinlock_enter(); 804 805 #ifdef SMP 806 /* 807 * stop_cpus_hard(other_cpus) should prevent multiple CPUs from 808 * concurrently entering panic. Only the winner will proceed 809 * further. 810 */ 811 if (panicstr == NULL && !kdb_active) { 812 other_cpus = all_cpus; 813 CPU_CLR(PCPU_GET(cpuid), &other_cpus); 814 stop_cpus_hard(other_cpus); 815 } 816 #endif 817 818 /* 819 * Ensure that the scheduler is stopped while panicking, even if panic 820 * has been entered from kdb. 821 */ 822 td->td_stopsched = 1; 823 824 bootopt = RB_AUTOBOOT; 825 newpanic = 0; 826 if (panicstr) 827 bootopt |= RB_NOSYNC; 828 else { 829 bootopt |= RB_DUMP; 830 panicstr = fmt; 831 newpanic = 1; 832 } 833 834 if (newpanic) { 835 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 836 panicstr = buf; 837 cngrab(); 838 printf("panic: %s\n", buf); 839 } else { 840 printf("panic: "); 841 vprintf(fmt, ap); 842 printf("\n"); 843 } 844 #ifdef SMP 845 printf("cpuid = %d\n", PCPU_GET(cpuid)); 846 #endif 847 printf("time = %jd\n", (intmax_t )time_second); 848 #ifdef KDB 849 if ((newpanic || trace_all_panics) && trace_on_panic) 850 kdb_backtrace(); 851 if (debugger_on_panic) 852 kdb_enter(KDB_WHY_PANIC, "panic"); 853 #endif 854 /*thread_lock(td); */ 855 td->td_flags |= TDF_INPANIC; 856 /* thread_unlock(td); */ 857 if (!sync_on_panic) 858 bootopt |= RB_NOSYNC; 859 if (poweroff_on_panic) 860 bootopt |= RB_POWEROFF; 861 if (powercycle_on_panic) 862 bootopt |= RB_POWERCYCLE; 863 kern_reboot(bootopt); 864 } 865 866 /* 867 * Support for poweroff delay. 868 * 869 * Please note that setting this delay too short might power off your machine 870 * before the write cache on your hard disk has been flushed, leading to 871 * soft-updates inconsistencies. 872 */ 873 #ifndef POWEROFF_DELAY 874 # define POWEROFF_DELAY 5000 875 #endif 876 static int poweroff_delay = POWEROFF_DELAY; 877 878 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 879 &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)"); 880 881 static void 882 poweroff_wait(void *junk, int howto) 883 { 884 885 if ((howto & (RB_POWEROFF | RB_POWERCYCLE)) == 0 || poweroff_delay <= 0) 886 return; 887 DELAY(poweroff_delay * 1000); 888 } 889 890 /* 891 * Some system processes (e.g. syncer) need to be stopped at appropriate 892 * points in their main loops prior to a system shutdown, so that they 893 * won't interfere with the shutdown process (e.g. by holding a disk buf 894 * to cause sync to fail). For each of these system processes, register 895 * shutdown_kproc() as a handler for one of shutdown events. 896 */ 897 static int kproc_shutdown_wait = 60; 898 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 899 &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process"); 900 901 void 902 kproc_shutdown(void *arg, int howto) 903 { 904 struct proc *p; 905 int error; 906 907 if (panicstr) 908 return; 909 910 p = (struct proc *)arg; 911 printf("Waiting (max %d seconds) for system process `%s' to stop... ", 912 kproc_shutdown_wait, p->p_comm); 913 error = kproc_suspend(p, kproc_shutdown_wait * hz); 914 915 if (error == EWOULDBLOCK) 916 printf("timed out\n"); 917 else 918 printf("done\n"); 919 } 920 921 void 922 kthread_shutdown(void *arg, int howto) 923 { 924 struct thread *td; 925 int error; 926 927 if (panicstr) 928 return; 929 930 td = (struct thread *)arg; 931 printf("Waiting (max %d seconds) for system thread `%s' to stop... ", 932 kproc_shutdown_wait, td->td_name); 933 error = kthread_suspend(td, kproc_shutdown_wait * hz); 934 935 if (error == EWOULDBLOCK) 936 printf("timed out\n"); 937 else 938 printf("done\n"); 939 } 940 941 static char dumpdevname[sizeof(((struct cdev*)NULL)->si_name)]; 942 SYSCTL_STRING(_kern_shutdown, OID_AUTO, dumpdevname, CTLFLAG_RD, 943 dumpdevname, 0, "Device for kernel dumps"); 944 945 static int _dump_append(struct dumperinfo *di, void *virtual, 946 vm_offset_t physical, size_t length); 947 948 #ifdef EKCD 949 static struct kerneldumpcrypto * 950 kerneldumpcrypto_create(size_t blocksize, uint8_t encryption, 951 const uint8_t *key, uint32_t encryptedkeysize, const uint8_t *encryptedkey) 952 { 953 struct kerneldumpcrypto *kdc; 954 struct kerneldumpkey *kdk; 955 uint32_t dumpkeysize; 956 957 dumpkeysize = roundup2(sizeof(*kdk) + encryptedkeysize, blocksize); 958 kdc = malloc(sizeof(*kdc) + dumpkeysize, M_EKCD, M_WAITOK | M_ZERO); 959 960 arc4rand(kdc->kdc_iv, sizeof(kdc->kdc_iv), 0); 961 962 kdc->kdc_encryption = encryption; 963 switch (kdc->kdc_encryption) { 964 case KERNELDUMP_ENC_AES_256_CBC: 965 if (rijndael_makeKey(&kdc->kdc_ki, DIR_ENCRYPT, 256, key) <= 0) 966 goto failed; 967 break; 968 default: 969 goto failed; 970 } 971 972 kdc->kdc_dumpkeysize = dumpkeysize; 973 kdk = kdc->kdc_dumpkey; 974 kdk->kdk_encryption = kdc->kdc_encryption; 975 memcpy(kdk->kdk_iv, kdc->kdc_iv, sizeof(kdk->kdk_iv)); 976 kdk->kdk_encryptedkeysize = htod32(encryptedkeysize); 977 memcpy(kdk->kdk_encryptedkey, encryptedkey, encryptedkeysize); 978 979 return (kdc); 980 failed: 981 explicit_bzero(kdc, sizeof(*kdc) + dumpkeysize); 982 free(kdc, M_EKCD); 983 return (NULL); 984 } 985 986 static int 987 kerneldumpcrypto_init(struct kerneldumpcrypto *kdc) 988 { 989 uint8_t hash[SHA256_DIGEST_LENGTH]; 990 SHA256_CTX ctx; 991 struct kerneldumpkey *kdk; 992 int error; 993 994 error = 0; 995 996 if (kdc == NULL) 997 return (0); 998 999 /* 1000 * When a user enters ddb it can write a crash dump multiple times. 1001 * Each time it should be encrypted using a different IV. 1002 */ 1003 SHA256_Init(&ctx); 1004 SHA256_Update(&ctx, kdc->kdc_iv, sizeof(kdc->kdc_iv)); 1005 SHA256_Final(hash, &ctx); 1006 bcopy(hash, kdc->kdc_iv, sizeof(kdc->kdc_iv)); 1007 1008 switch (kdc->kdc_encryption) { 1009 case KERNELDUMP_ENC_AES_256_CBC: 1010 if (rijndael_cipherInit(&kdc->kdc_ci, MODE_CBC, 1011 kdc->kdc_iv) <= 0) { 1012 error = EINVAL; 1013 goto out; 1014 } 1015 break; 1016 default: 1017 error = EINVAL; 1018 goto out; 1019 } 1020 1021 kdk = kdc->kdc_dumpkey; 1022 memcpy(kdk->kdk_iv, kdc->kdc_iv, sizeof(kdk->kdk_iv)); 1023 out: 1024 explicit_bzero(hash, sizeof(hash)); 1025 return (error); 1026 } 1027 1028 static uint32_t 1029 kerneldumpcrypto_dumpkeysize(const struct kerneldumpcrypto *kdc) 1030 { 1031 1032 if (kdc == NULL) 1033 return (0); 1034 return (kdc->kdc_dumpkeysize); 1035 } 1036 #endif /* EKCD */ 1037 1038 static struct kerneldumpcomp * 1039 kerneldumpcomp_create(struct dumperinfo *di, uint8_t compression) 1040 { 1041 struct kerneldumpcomp *kdcomp; 1042 int format; 1043 1044 switch (compression) { 1045 case KERNELDUMP_COMP_GZIP: 1046 format = COMPRESS_GZIP; 1047 break; 1048 case KERNELDUMP_COMP_ZSTD: 1049 format = COMPRESS_ZSTD; 1050 break; 1051 default: 1052 return (NULL); 1053 } 1054 1055 kdcomp = malloc(sizeof(*kdcomp), M_DUMPER, M_WAITOK | M_ZERO); 1056 kdcomp->kdc_format = compression; 1057 kdcomp->kdc_stream = compressor_init(kerneldumpcomp_write_cb, 1058 format, di->maxiosize, kerneldump_gzlevel, di); 1059 if (kdcomp->kdc_stream == NULL) { 1060 free(kdcomp, M_DUMPER); 1061 return (NULL); 1062 } 1063 kdcomp->kdc_buf = malloc(di->maxiosize, M_DUMPER, M_WAITOK | M_NODUMP); 1064 return (kdcomp); 1065 } 1066 1067 static void 1068 kerneldumpcomp_destroy(struct dumperinfo *di) 1069 { 1070 struct kerneldumpcomp *kdcomp; 1071 1072 kdcomp = di->kdcomp; 1073 if (kdcomp == NULL) 1074 return; 1075 compressor_fini(kdcomp->kdc_stream); 1076 explicit_bzero(kdcomp->kdc_buf, di->maxiosize); 1077 free(kdcomp->kdc_buf, M_DUMPER); 1078 free(kdcomp, M_DUMPER); 1079 } 1080 1081 /* Registration of dumpers */ 1082 int 1083 set_dumper(struct dumperinfo *di, const char *devname, struct thread *td, 1084 uint8_t compression, uint8_t encryption, const uint8_t *key, 1085 uint32_t encryptedkeysize, const uint8_t *encryptedkey) 1086 { 1087 size_t wantcopy; 1088 int error; 1089 1090 error = priv_check(td, PRIV_SETDUMPER); 1091 if (error != 0) 1092 return (error); 1093 1094 if (dumper.dumper != NULL) 1095 return (EBUSY); 1096 dumper = *di; 1097 dumper.blockbuf = NULL; 1098 dumper.kdcrypto = NULL; 1099 dumper.kdcomp = NULL; 1100 1101 if (encryption != KERNELDUMP_ENC_NONE) { 1102 #ifdef EKCD 1103 dumper.kdcrypto = kerneldumpcrypto_create(di->blocksize, 1104 encryption, key, encryptedkeysize, encryptedkey); 1105 if (dumper.kdcrypto == NULL) { 1106 error = EINVAL; 1107 goto cleanup; 1108 } 1109 #else 1110 error = EOPNOTSUPP; 1111 goto cleanup; 1112 #endif 1113 } 1114 1115 wantcopy = strlcpy(dumpdevname, devname, sizeof(dumpdevname)); 1116 if (wantcopy >= sizeof(dumpdevname)) { 1117 printf("set_dumper: device name truncated from '%s' -> '%s'\n", 1118 devname, dumpdevname); 1119 } 1120 1121 if (compression != KERNELDUMP_COMP_NONE) { 1122 /* 1123 * We currently can't support simultaneous encryption and 1124 * compression. 1125 */ 1126 if (encryption != KERNELDUMP_ENC_NONE) { 1127 error = EOPNOTSUPP; 1128 goto cleanup; 1129 } 1130 dumper.kdcomp = kerneldumpcomp_create(&dumper, compression); 1131 if (dumper.kdcomp == NULL) { 1132 error = EINVAL; 1133 goto cleanup; 1134 } 1135 } 1136 1137 dumper.blockbuf = malloc(di->blocksize, M_DUMPER, M_WAITOK | M_ZERO); 1138 return (0); 1139 1140 cleanup: 1141 (void)clear_dumper(td); 1142 return (error); 1143 } 1144 1145 int 1146 clear_dumper(struct thread *td) 1147 { 1148 int error; 1149 1150 error = priv_check(td, PRIV_SETDUMPER); 1151 if (error != 0) 1152 return (error); 1153 1154 #ifdef NETDUMP 1155 netdump_mbuf_drain(); 1156 #endif 1157 1158 #ifdef EKCD 1159 if (dumper.kdcrypto != NULL) { 1160 explicit_bzero(dumper.kdcrypto, sizeof(*dumper.kdcrypto) + 1161 dumper.kdcrypto->kdc_dumpkeysize); 1162 free(dumper.kdcrypto, M_EKCD); 1163 } 1164 #endif 1165 1166 kerneldumpcomp_destroy(&dumper); 1167 1168 if (dumper.blockbuf != NULL) { 1169 explicit_bzero(dumper.blockbuf, dumper.blocksize); 1170 free(dumper.blockbuf, M_DUMPER); 1171 } 1172 explicit_bzero(&dumper, sizeof(dumper)); 1173 dumpdevname[0] = '\0'; 1174 return (0); 1175 } 1176 1177 static int 1178 dump_check_bounds(struct dumperinfo *di, off_t offset, size_t length) 1179 { 1180 1181 if (di->mediasize > 0 && length != 0 && (offset < di->mediaoffset || 1182 offset - di->mediaoffset + length > di->mediasize)) { 1183 if (di->kdcomp != NULL && offset >= di->mediaoffset) { 1184 printf( 1185 "Compressed dump failed to fit in device boundaries.\n"); 1186 return (E2BIG); 1187 } 1188 1189 printf("Attempt to write outside dump device boundaries.\n" 1190 "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n", 1191 (intmax_t)offset, (intmax_t)di->mediaoffset, 1192 (uintmax_t)length, (intmax_t)di->mediasize); 1193 return (ENOSPC); 1194 } 1195 if (length % di->blocksize != 0) { 1196 printf("Attempt to write partial block of length %ju.\n", 1197 (uintmax_t)length); 1198 return (EINVAL); 1199 } 1200 if (offset % di->blocksize != 0) { 1201 printf("Attempt to write at unaligned offset %jd.\n", 1202 (intmax_t)offset); 1203 return (EINVAL); 1204 } 1205 1206 return (0); 1207 } 1208 1209 #ifdef EKCD 1210 static int 1211 dump_encrypt(struct kerneldumpcrypto *kdc, uint8_t *buf, size_t size) 1212 { 1213 1214 switch (kdc->kdc_encryption) { 1215 case KERNELDUMP_ENC_AES_256_CBC: 1216 if (rijndael_blockEncrypt(&kdc->kdc_ci, &kdc->kdc_ki, buf, 1217 8 * size, buf) <= 0) { 1218 return (EIO); 1219 } 1220 if (rijndael_cipherInit(&kdc->kdc_ci, MODE_CBC, 1221 buf + size - 16 /* IV size for AES-256-CBC */) <= 0) { 1222 return (EIO); 1223 } 1224 break; 1225 default: 1226 return (EINVAL); 1227 } 1228 1229 return (0); 1230 } 1231 1232 /* Encrypt data and call dumper. */ 1233 static int 1234 dump_encrypted_write(struct dumperinfo *di, void *virtual, 1235 vm_offset_t physical, off_t offset, size_t length) 1236 { 1237 static uint8_t buf[KERNELDUMP_BUFFER_SIZE]; 1238 struct kerneldumpcrypto *kdc; 1239 int error; 1240 size_t nbytes; 1241 1242 kdc = di->kdcrypto; 1243 1244 while (length > 0) { 1245 nbytes = MIN(length, sizeof(buf)); 1246 bcopy(virtual, buf, nbytes); 1247 1248 if (dump_encrypt(kdc, buf, nbytes) != 0) 1249 return (EIO); 1250 1251 error = dump_write(di, buf, physical, offset, nbytes); 1252 if (error != 0) 1253 return (error); 1254 1255 offset += nbytes; 1256 virtual = (void *)((uint8_t *)virtual + nbytes); 1257 length -= nbytes; 1258 } 1259 1260 return (0); 1261 } 1262 #endif /* EKCD */ 1263 1264 static int 1265 kerneldumpcomp_write_cb(void *base, size_t length, off_t offset, void *arg) 1266 { 1267 struct dumperinfo *di; 1268 size_t resid, rlength; 1269 int error; 1270 1271 di = arg; 1272 1273 if (length % di->blocksize != 0) { 1274 /* 1275 * This must be the final write after flushing the compression 1276 * stream. Write as many full blocks as possible and stash the 1277 * residual data in the dumper's block buffer. It will be 1278 * padded and written in dump_finish(). 1279 */ 1280 rlength = rounddown(length, di->blocksize); 1281 if (rlength != 0) { 1282 error = _dump_append(di, base, 0, rlength); 1283 if (error != 0) 1284 return (error); 1285 } 1286 resid = length - rlength; 1287 memmove(di->blockbuf, (uint8_t *)base + rlength, resid); 1288 di->kdcomp->kdc_resid = resid; 1289 return (EAGAIN); 1290 } 1291 return (_dump_append(di, base, 0, length)); 1292 } 1293 1294 /* 1295 * Write kernel dump headers at the beginning and end of the dump extent. 1296 * Write the kernel dump encryption key after the leading header if we were 1297 * configured to do so. 1298 */ 1299 static int 1300 dump_write_headers(struct dumperinfo *di, struct kerneldumpheader *kdh) 1301 { 1302 #ifdef EKCD 1303 struct kerneldumpcrypto *kdc; 1304 #endif 1305 void *buf, *key; 1306 size_t hdrsz; 1307 uint64_t extent; 1308 uint32_t keysize; 1309 int error; 1310 1311 hdrsz = sizeof(*kdh); 1312 if (hdrsz > di->blocksize) 1313 return (ENOMEM); 1314 1315 #ifdef EKCD 1316 kdc = di->kdcrypto; 1317 key = kdc->kdc_dumpkey; 1318 keysize = kerneldumpcrypto_dumpkeysize(kdc); 1319 #else 1320 key = NULL; 1321 keysize = 0; 1322 #endif 1323 1324 /* 1325 * If the dump device has special handling for headers, let it take care 1326 * of writing them out. 1327 */ 1328 if (di->dumper_hdr != NULL) 1329 return (di->dumper_hdr(di, kdh, key, keysize)); 1330 1331 if (hdrsz == di->blocksize) 1332 buf = kdh; 1333 else { 1334 buf = di->blockbuf; 1335 memset(buf, 0, di->blocksize); 1336 memcpy(buf, kdh, hdrsz); 1337 } 1338 1339 extent = dtoh64(kdh->dumpextent); 1340 #ifdef EKCD 1341 if (kdc != NULL) { 1342 error = dump_write(di, kdc->kdc_dumpkey, 0, 1343 di->mediaoffset + di->mediasize - di->blocksize - extent - 1344 keysize, keysize); 1345 if (error != 0) 1346 return (error); 1347 } 1348 #endif 1349 1350 error = dump_write(di, buf, 0, 1351 di->mediaoffset + di->mediasize - 2 * di->blocksize - extent - 1352 keysize, di->blocksize); 1353 if (error == 0) 1354 error = dump_write(di, buf, 0, di->mediaoffset + di->mediasize - 1355 di->blocksize, di->blocksize); 1356 return (error); 1357 } 1358 1359 /* 1360 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This is to 1361 * protect us from metadata and metadata from us. 1362 */ 1363 #define SIZEOF_METADATA (64 * 1024) 1364 1365 /* 1366 * Do some preliminary setup for a kernel dump: initialize state for encryption, 1367 * if requested, and make sure that we have enough space on the dump device. 1368 * 1369 * We set things up so that the dump ends before the last sector of the dump 1370 * device, at which the trailing header is written. 1371 * 1372 * +-----------+------+-----+----------------------------+------+ 1373 * | | lhdr | key | ... kernel dump ... | thdr | 1374 * +-----------+------+-----+----------------------------+------+ 1375 * 1 blk opt <------- dump extent --------> 1 blk 1376 * 1377 * Dumps written using dump_append() start at the beginning of the extent. 1378 * Uncompressed dumps will use the entire extent, but compressed dumps typically 1379 * will not. The true length of the dump is recorded in the leading and trailing 1380 * headers once the dump has been completed. 1381 * 1382 * The dump device may provide a callback, in which case it will initialize 1383 * dumpoff and take care of laying out the headers. 1384 */ 1385 int 1386 dump_start(struct dumperinfo *di, struct kerneldumpheader *kdh) 1387 { 1388 uint64_t dumpextent, span; 1389 uint32_t keysize; 1390 int error; 1391 1392 #ifdef EKCD 1393 error = kerneldumpcrypto_init(di->kdcrypto); 1394 if (error != 0) 1395 return (error); 1396 keysize = kerneldumpcrypto_dumpkeysize(di->kdcrypto); 1397 #else 1398 error = 0; 1399 keysize = 0; 1400 #endif 1401 1402 if (di->dumper_start != NULL) { 1403 error = di->dumper_start(di); 1404 } else { 1405 dumpextent = dtoh64(kdh->dumpextent); 1406 span = SIZEOF_METADATA + dumpextent + 2 * di->blocksize + 1407 keysize; 1408 if (di->mediasize < span) { 1409 if (di->kdcomp == NULL) 1410 return (E2BIG); 1411 1412 /* 1413 * We don't yet know how much space the compressed dump 1414 * will occupy, so try to use the whole swap partition 1415 * (minus the first 64KB) in the hope that the 1416 * compressed dump will fit. If that doesn't turn out to 1417 * be enough, the bounds checking in dump_write() 1418 * will catch us and cause the dump to fail. 1419 */ 1420 dumpextent = di->mediasize - span + dumpextent; 1421 kdh->dumpextent = htod64(dumpextent); 1422 } 1423 1424 /* 1425 * The offset at which to begin writing the dump. 1426 */ 1427 di->dumpoff = di->mediaoffset + di->mediasize - di->blocksize - 1428 dumpextent; 1429 } 1430 di->origdumpoff = di->dumpoff; 1431 return (error); 1432 } 1433 1434 static int 1435 _dump_append(struct dumperinfo *di, void *virtual, vm_offset_t physical, 1436 size_t length) 1437 { 1438 int error; 1439 1440 #ifdef EKCD 1441 if (di->kdcrypto != NULL) 1442 error = dump_encrypted_write(di, virtual, physical, di->dumpoff, 1443 length); 1444 else 1445 #endif 1446 error = dump_write(di, virtual, physical, di->dumpoff, length); 1447 if (error == 0) 1448 di->dumpoff += length; 1449 return (error); 1450 } 1451 1452 /* 1453 * Write to the dump device starting at dumpoff. When compression is enabled, 1454 * writes to the device will be performed using a callback that gets invoked 1455 * when the compression stream's output buffer is full. 1456 */ 1457 int 1458 dump_append(struct dumperinfo *di, void *virtual, vm_offset_t physical, 1459 size_t length) 1460 { 1461 void *buf; 1462 1463 if (di->kdcomp != NULL) { 1464 /* Bounce through a buffer to avoid CRC errors. */ 1465 if (length > di->maxiosize) 1466 return (EINVAL); 1467 buf = di->kdcomp->kdc_buf; 1468 memmove(buf, virtual, length); 1469 return (compressor_write(di->kdcomp->kdc_stream, buf, length)); 1470 } 1471 return (_dump_append(di, virtual, physical, length)); 1472 } 1473 1474 /* 1475 * Write to the dump device at the specified offset. 1476 */ 1477 int 1478 dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical, 1479 off_t offset, size_t length) 1480 { 1481 int error; 1482 1483 error = dump_check_bounds(di, offset, length); 1484 if (error != 0) 1485 return (error); 1486 return (di->dumper(di->priv, virtual, physical, offset, length)); 1487 } 1488 1489 /* 1490 * Perform kernel dump finalization: flush the compression stream, if necessary, 1491 * write the leading and trailing kernel dump headers now that we know the true 1492 * length of the dump, and optionally write the encryption key following the 1493 * leading header. 1494 */ 1495 int 1496 dump_finish(struct dumperinfo *di, struct kerneldumpheader *kdh) 1497 { 1498 int error; 1499 1500 if (di->kdcomp != NULL) { 1501 error = compressor_flush(di->kdcomp->kdc_stream); 1502 if (error == EAGAIN) { 1503 /* We have residual data in di->blockbuf. */ 1504 error = dump_write(di, di->blockbuf, 0, di->dumpoff, 1505 di->blocksize); 1506 di->dumpoff += di->kdcomp->kdc_resid; 1507 di->kdcomp->kdc_resid = 0; 1508 } 1509 if (error != 0) 1510 return (error); 1511 1512 /* 1513 * We now know the size of the compressed dump, so update the 1514 * header accordingly and recompute parity. 1515 */ 1516 kdh->dumplength = htod64(di->dumpoff - di->origdumpoff); 1517 kdh->parity = 0; 1518 kdh->parity = kerneldump_parity(kdh); 1519 1520 compressor_reset(di->kdcomp->kdc_stream); 1521 } 1522 1523 error = dump_write_headers(di, kdh); 1524 if (error != 0) 1525 return (error); 1526 1527 (void)dump_write(di, NULL, 0, 0, 0); 1528 return (0); 1529 } 1530 1531 void 1532 dump_init_header(const struct dumperinfo *di, struct kerneldumpheader *kdh, 1533 char *magic, uint32_t archver, uint64_t dumplen) 1534 { 1535 size_t dstsize; 1536 1537 bzero(kdh, sizeof(*kdh)); 1538 strlcpy(kdh->magic, magic, sizeof(kdh->magic)); 1539 strlcpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); 1540 kdh->version = htod32(KERNELDUMPVERSION); 1541 kdh->architectureversion = htod32(archver); 1542 kdh->dumplength = htod64(dumplen); 1543 kdh->dumpextent = kdh->dumplength; 1544 kdh->dumptime = htod64(time_second); 1545 #ifdef EKCD 1546 kdh->dumpkeysize = htod32(kerneldumpcrypto_dumpkeysize(di->kdcrypto)); 1547 #else 1548 kdh->dumpkeysize = 0; 1549 #endif 1550 kdh->blocksize = htod32(di->blocksize); 1551 strlcpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname)); 1552 dstsize = sizeof(kdh->versionstring); 1553 if (strlcpy(kdh->versionstring, version, dstsize) >= dstsize) 1554 kdh->versionstring[dstsize - 2] = '\n'; 1555 if (panicstr != NULL) 1556 strlcpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); 1557 if (di->kdcomp != NULL) 1558 kdh->compression = di->kdcomp->kdc_format; 1559 kdh->parity = kerneldump_parity(kdh); 1560 } 1561 1562 #ifdef DDB 1563 DB_SHOW_COMMAND(panic, db_show_panic) 1564 { 1565 1566 if (panicstr == NULL) 1567 db_printf("panicstr not set\n"); 1568 else 1569 db_printf("panic: %s\n", panicstr); 1570 } 1571 #endif 1572