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