1 /*- 2 * Copyright (c) 1986, 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_ddb.h" 41 #include "opt_ekcd.h" 42 #include "opt_gzio.h" 43 #include "opt_kdb.h" 44 #include "opt_panic.h" 45 #include "opt_sched.h" 46 #include "opt_watchdog.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/bio.h> 51 #include <sys/buf.h> 52 #include <sys/conf.h> 53 #include <sys/cons.h> 54 #include <sys/eventhandler.h> 55 #include <sys/filedesc.h> 56 #include <sys/gzio.h> 57 #include <sys/jail.h> 58 #include <sys/kdb.h> 59 #include <sys/kernel.h> 60 #include <sys/kerneldump.h> 61 #include <sys/kthread.h> 62 #include <sys/ktr.h> 63 #include <sys/malloc.h> 64 #include <sys/mount.h> 65 #include <sys/priv.h> 66 #include <sys/proc.h> 67 #include <sys/reboot.h> 68 #include <sys/resourcevar.h> 69 #include <sys/rwlock.h> 70 #include <sys/sched.h> 71 #include <sys/smp.h> 72 #include <sys/sysctl.h> 73 #include <sys/sysproto.h> 74 #include <sys/vnode.h> 75 #include <sys/watchdog.h> 76 77 #include <crypto/rijndael/rijndael-api-fst.h> 78 #include <crypto/sha2/sha256.h> 79 80 #include <ddb/ddb.h> 81 82 #include <machine/cpu.h> 83 #include <machine/dump.h> 84 #include <machine/pcb.h> 85 #include <machine/smp.h> 86 87 #include <security/mac/mac_framework.h> 88 89 #include <vm/vm.h> 90 #include <vm/vm_object.h> 91 #include <vm/vm_page.h> 92 #include <vm/vm_pager.h> 93 #include <vm/swap_pager.h> 94 95 #include <sys/signalvar.h> 96 97 static MALLOC_DEFINE(M_DUMPER, "dumper", "dumper block buffer"); 98 99 #ifndef PANIC_REBOOT_WAIT_TIME 100 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 101 #endif 102 static int panic_reboot_wait_time = PANIC_REBOOT_WAIT_TIME; 103 SYSCTL_INT(_kern, OID_AUTO, panic_reboot_wait_time, CTLFLAG_RWTUN, 104 &panic_reboot_wait_time, 0, 105 "Seconds to wait before rebooting after a panic"); 106 107 /* 108 * Note that stdarg.h and the ANSI style va_start macro is used for both 109 * ANSI and traditional C compilers. 110 */ 111 #include <machine/stdarg.h> 112 113 #ifdef KDB 114 #ifdef KDB_UNATTENDED 115 int debugger_on_panic = 0; 116 #else 117 int debugger_on_panic = 1; 118 #endif 119 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, 120 CTLFLAG_RWTUN | CTLFLAG_SECURE, 121 &debugger_on_panic, 0, "Run debugger on kernel panic"); 122 123 #ifdef KDB_TRACE 124 static int trace_on_panic = 1; 125 #else 126 static int trace_on_panic = 0; 127 #endif 128 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, 129 CTLFLAG_RWTUN | CTLFLAG_SECURE, 130 &trace_on_panic, 0, "Print stack trace on kernel panic"); 131 #endif /* KDB */ 132 133 static int sync_on_panic = 0; 134 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RWTUN, 135 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 136 137 static bool poweroff_on_panic = 0; 138 SYSCTL_BOOL(_kern, OID_AUTO, poweroff_on_panic, CTLFLAG_RWTUN, 139 &poweroff_on_panic, 0, "Do a power off instead of a reboot on a panic"); 140 141 static bool powercycle_on_panic = 0; 142 SYSCTL_BOOL(_kern, OID_AUTO, powercycle_on_panic, CTLFLAG_RWTUN, 143 &powercycle_on_panic, 0, "Do a power cycle instead of a reboot on a panic"); 144 145 static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, 146 "Shutdown environment"); 147 148 #ifndef DIAGNOSTIC 149 static int show_busybufs; 150 #else 151 static int show_busybufs = 1; 152 #endif 153 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW, 154 &show_busybufs, 0, ""); 155 156 int suspend_blocked = 0; 157 SYSCTL_INT(_kern, OID_AUTO, suspend_blocked, CTLFLAG_RW, 158 &suspend_blocked, 0, "Block suspend due to a pending shutdown"); 159 160 #ifdef EKCD 161 FEATURE(ekcd, "Encrypted kernel crash dumps support"); 162 163 MALLOC_DEFINE(M_EKCD, "ekcd", "Encrypted kernel crash dumps data"); 164 165 struct kerneldumpcrypto { 166 uint8_t kdc_encryption; 167 uint8_t kdc_iv[KERNELDUMP_IV_MAX_SIZE]; 168 keyInstance kdc_ki; 169 cipherInstance kdc_ci; 170 uint32_t kdc_dumpkeysize; 171 struct kerneldumpkey kdc_dumpkey[]; 172 }; 173 #endif 174 175 #ifdef GZIO 176 struct kerneldumpgz { 177 struct gzio_stream *kdgz_stream; 178 uint8_t *kdgz_buf; 179 size_t kdgz_resid; 180 }; 181 182 static struct kerneldumpgz *kerneldumpgz_create(struct dumperinfo *di, 183 uint8_t compression); 184 static void kerneldumpgz_destroy(struct dumperinfo *di); 185 static int kerneldumpgz_write_cb(void *cb, size_t len, off_t off, void *arg); 186 187 static int kerneldump_gzlevel = 6; 188 SYSCTL_INT(_kern, OID_AUTO, kerneldump_gzlevel, CTLFLAG_RWTUN, 189 &kerneldump_gzlevel, 0, 190 "Kernel crash dump gzip compression level"); 191 #endif /* GZIO */ 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 CPU 0 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, 0); 381 thread_unlock(curthread); 382 KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0")); 383 } 384 #endif 385 /* We're in the process of rebooting. */ 386 rebooting = 1; 387 388 /* We are out of the debugger now. */ 389 kdb_active = 0; 390 391 /* 392 * Do any callouts that should be done BEFORE syncing the filesystems. 393 */ 394 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 395 396 /* 397 * Now sync filesystems 398 */ 399 if (!cold && (howto & RB_NOSYNC) == 0 && once == 0) { 400 once = 1; 401 bufshutdown(show_busybufs); 402 } 403 404 print_uptime(); 405 406 cngrab(); 407 408 /* 409 * Ok, now do things that assume all filesystem activity has 410 * been completed. 411 */ 412 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 413 414 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 415 doadump(TRUE); 416 417 /* Now that we're going to really halt the system... */ 418 EVENTHANDLER_INVOKE(shutdown_final, howto); 419 420 for(;;) ; /* safety against shutdown_reset not working */ 421 /* NOTREACHED */ 422 } 423 424 /* 425 * The system call that results in changing the rootfs. 426 */ 427 static int 428 kern_reroot(void) 429 { 430 struct vnode *oldrootvnode, *vp; 431 struct mount *mp, *devmp; 432 int error; 433 434 if (curproc != initproc) 435 return (EPERM); 436 437 /* 438 * Mark the filesystem containing currently-running executable 439 * (the temporary copy of init(8)) busy. 440 */ 441 vp = curproc->p_textvp; 442 error = vn_lock(vp, LK_SHARED); 443 if (error != 0) 444 return (error); 445 mp = vp->v_mount; 446 error = vfs_busy(mp, MBF_NOWAIT); 447 if (error != 0) { 448 vfs_ref(mp); 449 VOP_UNLOCK(vp, 0); 450 error = vfs_busy(mp, 0); 451 vn_lock(vp, LK_SHARED | LK_RETRY); 452 vfs_rel(mp); 453 if (error != 0) { 454 VOP_UNLOCK(vp, 0); 455 return (ENOENT); 456 } 457 if (vp->v_iflag & VI_DOOMED) { 458 VOP_UNLOCK(vp, 0); 459 vfs_unbusy(mp); 460 return (ENOENT); 461 } 462 } 463 VOP_UNLOCK(vp, 0); 464 465 /* 466 * Remove the filesystem containing currently-running executable 467 * from the mount list, to prevent it from being unmounted 468 * by vfs_unmountall(), and to avoid confusing vfs_mountroot(). 469 * 470 * Also preserve /dev - forcibly unmounting it could cause driver 471 * reinitialization. 472 */ 473 474 vfs_ref(rootdevmp); 475 devmp = rootdevmp; 476 rootdevmp = NULL; 477 478 mtx_lock(&mountlist_mtx); 479 TAILQ_REMOVE(&mountlist, mp, mnt_list); 480 TAILQ_REMOVE(&mountlist, devmp, mnt_list); 481 mtx_unlock(&mountlist_mtx); 482 483 oldrootvnode = rootvnode; 484 485 /* 486 * Unmount everything except for the two filesystems preserved above. 487 */ 488 vfs_unmountall(); 489 490 /* 491 * Add /dev back; vfs_mountroot() will move it into its new place. 492 */ 493 mtx_lock(&mountlist_mtx); 494 TAILQ_INSERT_HEAD(&mountlist, devmp, mnt_list); 495 mtx_unlock(&mountlist_mtx); 496 rootdevmp = devmp; 497 vfs_rel(rootdevmp); 498 499 /* 500 * Mount the new rootfs. 501 */ 502 vfs_mountroot(); 503 504 /* 505 * Update all references to the old rootvnode. 506 */ 507 mountcheckdirs(oldrootvnode, rootvnode); 508 509 /* 510 * Add the temporary filesystem back and unbusy it. 511 */ 512 mtx_lock(&mountlist_mtx); 513 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 514 mtx_unlock(&mountlist_mtx); 515 vfs_unbusy(mp); 516 517 return (0); 518 } 519 520 /* 521 * If the shutdown was a clean halt, behave accordingly. 522 */ 523 static void 524 shutdown_halt(void *junk, int howto) 525 { 526 527 if (howto & RB_HALT) { 528 printf("\n"); 529 printf("The operating system has halted.\n"); 530 printf("Please press any key to reboot.\n\n"); 531 switch (cngetc()) { 532 case -1: /* No console, just die */ 533 cpu_halt(); 534 /* NOTREACHED */ 535 default: 536 howto &= ~RB_HALT; 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 #ifdef GZIO 988 static struct kerneldumpgz * 989 kerneldumpgz_create(struct dumperinfo *di, uint8_t compression) 990 { 991 struct kerneldumpgz *kdgz; 992 993 if (compression != KERNELDUMP_COMP_GZIP) 994 return (NULL); 995 kdgz = malloc(sizeof(*kdgz), M_DUMPER, M_WAITOK | M_ZERO); 996 kdgz->kdgz_stream = gzio_init(kerneldumpgz_write_cb, GZIO_DEFLATE, 997 di->maxiosize, kerneldump_gzlevel, di); 998 if (kdgz->kdgz_stream == NULL) { 999 free(kdgz, M_DUMPER); 1000 return (NULL); 1001 } 1002 kdgz->kdgz_buf = malloc(di->maxiosize, M_DUMPER, M_WAITOK | M_NODUMP); 1003 return (kdgz); 1004 } 1005 1006 static void 1007 kerneldumpgz_destroy(struct dumperinfo *di) 1008 { 1009 struct kerneldumpgz *kdgz; 1010 1011 kdgz = di->kdgz; 1012 if (kdgz == NULL) 1013 return; 1014 gzio_fini(kdgz->kdgz_stream); 1015 explicit_bzero(kdgz->kdgz_buf, di->maxiosize); 1016 free(kdgz->kdgz_buf, M_DUMPER); 1017 free(kdgz, M_DUMPER); 1018 } 1019 #endif /* GZIO */ 1020 1021 /* Registration of dumpers */ 1022 int 1023 set_dumper(struct dumperinfo *di, const char *devname, struct thread *td, 1024 uint8_t compression, uint8_t encryption, const uint8_t *key, 1025 uint32_t encryptedkeysize, const uint8_t *encryptedkey) 1026 { 1027 size_t wantcopy; 1028 int error; 1029 1030 error = priv_check(td, PRIV_SETDUMPER); 1031 if (error != 0) 1032 return (error); 1033 1034 if (di == NULL) { 1035 error = 0; 1036 goto cleanup; 1037 } 1038 if (dumper.dumper != NULL) 1039 return (EBUSY); 1040 dumper = *di; 1041 dumper.blockbuf = NULL; 1042 dumper.kdc = NULL; 1043 dumper.kdgz = NULL; 1044 1045 if (encryption != KERNELDUMP_ENC_NONE) { 1046 #ifdef EKCD 1047 dumper.kdc = kerneldumpcrypto_create(di->blocksize, encryption, 1048 key, encryptedkeysize, encryptedkey); 1049 if (dumper.kdc == NULL) { 1050 error = EINVAL; 1051 goto cleanup; 1052 } 1053 #else 1054 error = EOPNOTSUPP; 1055 goto cleanup; 1056 #endif 1057 } 1058 1059 wantcopy = strlcpy(dumpdevname, devname, sizeof(dumpdevname)); 1060 if (wantcopy >= sizeof(dumpdevname)) { 1061 printf("set_dumper: device name truncated from '%s' -> '%s'\n", 1062 devname, dumpdevname); 1063 } 1064 1065 if (compression != KERNELDUMP_COMP_NONE) { 1066 #ifdef GZIO 1067 /* 1068 * We currently can't support simultaneous encryption and 1069 * compression. 1070 */ 1071 if (encryption != KERNELDUMP_ENC_NONE) { 1072 error = EOPNOTSUPP; 1073 goto cleanup; 1074 } 1075 dumper.kdgz = kerneldumpgz_create(&dumper, compression); 1076 if (dumper.kdgz == NULL) { 1077 error = EINVAL; 1078 goto cleanup; 1079 } 1080 #else 1081 error = EOPNOTSUPP; 1082 goto cleanup; 1083 #endif 1084 } 1085 1086 dumper.blockbuf = malloc(di->blocksize, M_DUMPER, M_WAITOK | M_ZERO); 1087 return (0); 1088 cleanup: 1089 #ifdef EKCD 1090 if (dumper.kdc != NULL) { 1091 explicit_bzero(dumper.kdc, sizeof(*dumper.kdc) + 1092 dumper.kdc->kdc_dumpkeysize); 1093 free(dumper.kdc, M_EKCD); 1094 } 1095 #endif 1096 1097 #ifdef GZIO 1098 kerneldumpgz_destroy(&dumper); 1099 #endif 1100 1101 if (dumper.blockbuf != NULL) { 1102 explicit_bzero(dumper.blockbuf, dumper.blocksize); 1103 free(dumper.blockbuf, M_DUMPER); 1104 } 1105 explicit_bzero(&dumper, sizeof(dumper)); 1106 dumpdevname[0] = '\0'; 1107 return (error); 1108 } 1109 1110 static int 1111 dump_check_bounds(struct dumperinfo *di, off_t offset, size_t length) 1112 { 1113 1114 if (length != 0 && (offset < di->mediaoffset || 1115 offset - di->mediaoffset + length > di->mediasize)) { 1116 printf("Attempt to write outside dump device boundaries.\n" 1117 "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n", 1118 (intmax_t)offset, (intmax_t)di->mediaoffset, 1119 (uintmax_t)length, (intmax_t)di->mediasize); 1120 return (ENOSPC); 1121 } 1122 if (length % di->blocksize != 0) { 1123 printf("Attempt to write partial block of length %ju.\n", 1124 (uintmax_t)length); 1125 return (EINVAL); 1126 } 1127 if (offset % di->blocksize != 0) { 1128 printf("Attempt to write at unaligned offset %jd.\n", 1129 (intmax_t)offset); 1130 return (EINVAL); 1131 } 1132 1133 return (0); 1134 } 1135 1136 #ifdef EKCD 1137 static int 1138 dump_encrypt(struct kerneldumpcrypto *kdc, uint8_t *buf, size_t size) 1139 { 1140 1141 switch (kdc->kdc_encryption) { 1142 case KERNELDUMP_ENC_AES_256_CBC: 1143 if (rijndael_blockEncrypt(&kdc->kdc_ci, &kdc->kdc_ki, buf, 1144 8 * size, buf) <= 0) { 1145 return (EIO); 1146 } 1147 if (rijndael_cipherInit(&kdc->kdc_ci, MODE_CBC, 1148 buf + size - 16 /* IV size for AES-256-CBC */) <= 0) { 1149 return (EIO); 1150 } 1151 break; 1152 default: 1153 return (EINVAL); 1154 } 1155 1156 return (0); 1157 } 1158 1159 /* Encrypt data and call dumper. */ 1160 static int 1161 dump_encrypted_write(struct dumperinfo *di, void *virtual, 1162 vm_offset_t physical, off_t offset, size_t length) 1163 { 1164 static uint8_t buf[KERNELDUMP_BUFFER_SIZE]; 1165 struct kerneldumpcrypto *kdc; 1166 int error; 1167 size_t nbytes; 1168 1169 kdc = di->kdc; 1170 1171 while (length > 0) { 1172 nbytes = MIN(length, sizeof(buf)); 1173 bcopy(virtual, buf, nbytes); 1174 1175 if (dump_encrypt(kdc, buf, nbytes) != 0) 1176 return (EIO); 1177 1178 error = dump_write(di, buf, physical, offset, nbytes); 1179 if (error != 0) 1180 return (error); 1181 1182 offset += nbytes; 1183 virtual = (void *)((uint8_t *)virtual + nbytes); 1184 length -= nbytes; 1185 } 1186 1187 return (0); 1188 } 1189 1190 static int 1191 dump_write_key(struct dumperinfo *di, off_t offset) 1192 { 1193 struct kerneldumpcrypto *kdc; 1194 1195 kdc = di->kdc; 1196 if (kdc == NULL) 1197 return (0); 1198 return (dump_write(di, kdc->kdc_dumpkey, 0, offset, 1199 kdc->kdc_dumpkeysize)); 1200 } 1201 #endif /* EKCD */ 1202 1203 #ifdef GZIO 1204 static int 1205 kerneldumpgz_write_cb(void *base, size_t length, off_t offset, void *arg) 1206 { 1207 struct dumperinfo *di; 1208 size_t resid, rlength; 1209 int error; 1210 1211 di = arg; 1212 1213 if (length % di->blocksize != 0) { 1214 /* 1215 * This must be the final write after flushing the compression 1216 * stream. Write as many full blocks as possible and stash the 1217 * residual data in the dumper's block buffer. It will be 1218 * padded and written in dump_finish(). 1219 */ 1220 rlength = rounddown(length, di->blocksize); 1221 if (rlength != 0) { 1222 error = _dump_append(di, base, 0, rlength); 1223 if (error != 0) 1224 return (error); 1225 } 1226 resid = length - rlength; 1227 memmove(di->blockbuf, (uint8_t *)base + rlength, resid); 1228 di->kdgz->kdgz_resid = resid; 1229 return (EAGAIN); 1230 } 1231 return (_dump_append(di, base, 0, length)); 1232 } 1233 #endif /* GZIO */ 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->kdc); 1292 if (error != 0) 1293 return (error); 1294 keysize = kerneldumpcrypto_dumpkeysize(di->kdc); 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 #ifdef GZIO 1303 if (di->kdgz != NULL) { 1304 /* 1305 * We don't yet know how much space the compressed dump 1306 * will occupy, so try to use the whole swap partition 1307 * (minus the first 64KB) in the hope that the 1308 * compressed dump will fit. If that doesn't turn out to 1309 * be enouch, the bounds checking in dump_write() 1310 * will catch us and cause the dump to fail. 1311 */ 1312 dumpextent = di->mediasize - SIZEOF_METADATA - 1313 2 * di->blocksize - keysize; 1314 kdh->dumpextent = htod64(dumpextent); 1315 } else 1316 #endif 1317 return (E2BIG); 1318 } 1319 1320 /* The offset at which to begin writing the dump. */ 1321 di->dumpoff = di->mediaoffset + di->mediasize - di->blocksize - 1322 dumpextent; 1323 1324 return (0); 1325 } 1326 1327 static int 1328 _dump_append(struct dumperinfo *di, void *virtual, vm_offset_t physical, 1329 size_t length) 1330 { 1331 int error; 1332 1333 #ifdef EKCD 1334 if (di->kdc != NULL) 1335 error = dump_encrypted_write(di, virtual, physical, di->dumpoff, 1336 length); 1337 else 1338 #endif 1339 error = dump_write(di, virtual, physical, di->dumpoff, length); 1340 if (error == 0) 1341 di->dumpoff += length; 1342 return (error); 1343 } 1344 1345 /* 1346 * Write to the dump device starting at dumpoff. When compression is enabled, 1347 * writes to the device will be performed using a callback that gets invoked 1348 * when the compression stream's output buffer is full. 1349 */ 1350 int 1351 dump_append(struct dumperinfo *di, void *virtual, vm_offset_t physical, 1352 size_t length) 1353 { 1354 #ifdef GZIO 1355 void *buf; 1356 1357 if (di->kdgz != NULL) { 1358 /* Bounce through a buffer to avoid gzip CRC errors. */ 1359 if (length > di->maxiosize) 1360 return (EINVAL); 1361 buf = di->kdgz->kdgz_buf; 1362 memmove(buf, virtual, length); 1363 return (gzio_write(di->kdgz->kdgz_stream, buf, length)); 1364 } 1365 #endif 1366 return (_dump_append(di, virtual, physical, length)); 1367 } 1368 1369 /* 1370 * Write to the dump device at the specified offset. 1371 */ 1372 int 1373 dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical, 1374 off_t offset, size_t length) 1375 { 1376 int error; 1377 1378 error = dump_check_bounds(di, offset, length); 1379 if (error != 0) 1380 return (error); 1381 return (di->dumper(di->priv, virtual, physical, offset, length)); 1382 } 1383 1384 /* 1385 * Perform kernel dump finalization: flush the compression stream, if necessary, 1386 * write the leading and trailing kernel dump headers now that we know the true 1387 * length of the dump, and optionally write the encryption key following the 1388 * leading header. 1389 */ 1390 int 1391 dump_finish(struct dumperinfo *di, struct kerneldumpheader *kdh) 1392 { 1393 uint64_t extent; 1394 uint32_t keysize; 1395 int error; 1396 1397 extent = dtoh64(kdh->dumpextent); 1398 1399 #ifdef EKCD 1400 keysize = kerneldumpcrypto_dumpkeysize(di->kdc); 1401 #else 1402 keysize = 0; 1403 #endif 1404 1405 #ifdef GZIO 1406 if (di->kdgz != NULL) { 1407 error = gzio_flush(di->kdgz->kdgz_stream); 1408 if (error == EAGAIN) { 1409 /* We have residual data in di->blockbuf. */ 1410 error = dump_write(di, di->blockbuf, 0, di->dumpoff, 1411 di->blocksize); 1412 di->dumpoff += di->kdgz->kdgz_resid; 1413 di->kdgz->kdgz_resid = 0; 1414 } 1415 if (error != 0) 1416 return (error); 1417 1418 /* 1419 * We now know the size of the compressed dump, so update the 1420 * header accordingly and recompute parity. 1421 */ 1422 kdh->dumplength = htod64(di->dumpoff - 1423 (di->mediaoffset + di->mediasize - di->blocksize - extent)); 1424 kdh->parity = 0; 1425 kdh->parity = kerneldump_parity(kdh); 1426 1427 gzio_reset(di->kdgz->kdgz_stream); 1428 } 1429 #endif 1430 1431 /* 1432 * Write kerneldump headers at the beginning and end of the dump extent. 1433 * Write the key after the leading header. 1434 */ 1435 error = dump_write_header(di, kdh, 1436 di->mediaoffset + di->mediasize - 2 * di->blocksize - extent - 1437 keysize); 1438 if (error != 0) 1439 return (error); 1440 1441 #ifdef EKCD 1442 error = dump_write_key(di, 1443 di->mediaoffset + di->mediasize - di->blocksize - extent - keysize); 1444 if (error != 0) 1445 return (error); 1446 #endif 1447 1448 error = dump_write_header(di, kdh, 1449 di->mediaoffset + di->mediasize - di->blocksize); 1450 if (error != 0) 1451 return (error); 1452 1453 (void)dump_write(di, NULL, 0, 0, 0); 1454 return (0); 1455 } 1456 1457 void 1458 dump_init_header(const struct dumperinfo *di, struct kerneldumpheader *kdh, 1459 char *magic, uint32_t archver, uint64_t dumplen) 1460 { 1461 size_t dstsize; 1462 1463 bzero(kdh, sizeof(*kdh)); 1464 strlcpy(kdh->magic, magic, sizeof(kdh->magic)); 1465 strlcpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); 1466 kdh->version = htod32(KERNELDUMPVERSION); 1467 kdh->architectureversion = htod32(archver); 1468 kdh->dumplength = htod64(dumplen); 1469 kdh->dumpextent = kdh->dumplength; 1470 kdh->dumptime = htod64(time_second); 1471 #ifdef EKCD 1472 kdh->dumpkeysize = htod32(kerneldumpcrypto_dumpkeysize(di->kdc)); 1473 #else 1474 kdh->dumpkeysize = 0; 1475 #endif 1476 kdh->blocksize = htod32(di->blocksize); 1477 strlcpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname)); 1478 dstsize = sizeof(kdh->versionstring); 1479 if (strlcpy(kdh->versionstring, version, dstsize) >= dstsize) 1480 kdh->versionstring[dstsize - 2] = '\n'; 1481 if (panicstr != NULL) 1482 strlcpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); 1483 #ifdef GZIO 1484 if (di->kdgz != NULL) 1485 kdh->compression = KERNELDUMP_COMP_GZIP; 1486 #endif 1487 kdh->parity = kerneldump_parity(kdh); 1488 } 1489 1490 #ifdef DDB 1491 DB_SHOW_COMMAND(panic, db_show_panic) 1492 { 1493 1494 if (panicstr == NULL) 1495 db_printf("panicstr not set\n"); 1496 else 1497 db_printf("panic: %s\n", panicstr); 1498 } 1499 #endif 1500