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