2 * SPDX-License-Identifier: BSD-3-Clause
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
45 #include "opt_panic.h"
46 #include "opt_printf.h"
47 #include "opt_sched.h"
48 #include "opt_watchdog.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
55 #include <sys/compressor.h>
58 #include <sys/eventhandler.h>
59 #include <sys/filedesc.h>
62 #include <sys/kernel.h>
63 #include <sys/kerneldump.h>
64 #include <sys/kthread.h>
66 #include <sys/malloc.h>
68 #include <sys/mount.h>
71 #include <sys/reboot.h>
72 #include <sys/resourcevar.h>
73 #include <sys/rwlock.h>
75 #include <sys/sched.h>
77 #include <sys/sysctl.h>
78 #include <sys/sysproto.h>
79 #include <sys/taskqueue.h>
80 #include <sys/vnode.h>
81 #include <sys/watchdog.h>
83 #include <crypto/chacha20/chacha.h>
84 #include <crypto/rijndael/rijndael-api-fst.h>
85 #include <crypto/sha2/sha256.h>
89 #include <machine/cpu.h>
90 #include <machine/dump.h>
91 #include <machine/pcb.h>
92 #include <machine/smp.h>
94 #include <security/mac/mac_framework.h>
97 #include <vm/vm_object.h>
98 #include <vm/vm_page.h>
99 #include <vm/vm_pager.h>
100 #include <vm/swap_pager.h>
102 #include <sys/signalvar.h>
104 static MALLOC_DEFINE(M_DUMPER, "dumper", "dumper block buffer");
106 #ifndef PANIC_REBOOT_WAIT_TIME
107 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
109 static int panic_reboot_wait_time = PANIC_REBOOT_WAIT_TIME;
110 SYSCTL_INT(_kern, OID_AUTO, panic_reboot_wait_time, CTLFLAG_RWTUN,
111 &panic_reboot_wait_time, 0,
112 "Seconds to wait before rebooting after a panic");
115 * Note that stdarg.h and the ANSI style va_start macro is used for both
116 * ANSI and traditional C compilers.
118 #include <machine/stdarg.h>
121 #ifdef KDB_UNATTENDED
122 static int debugger_on_panic = 0;
124 static int debugger_on_panic = 1;
126 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic,
127 CTLFLAG_RWTUN | CTLFLAG_SECURE,
128 &debugger_on_panic, 0, "Run debugger on kernel panic");
130 int debugger_on_trap = 0;
131 SYSCTL_INT(_debug, OID_AUTO, debugger_on_trap,
132 CTLFLAG_RWTUN | CTLFLAG_SECURE,
133 &debugger_on_trap, 0, "Run debugger on kernel trap before panic");
136 static int trace_on_panic = 1;
137 static bool trace_all_panics = true;
139 static int trace_on_panic = 0;
140 static bool trace_all_panics = false;
142 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic,
143 CTLFLAG_RWTUN | CTLFLAG_SECURE,
144 &trace_on_panic, 0, "Print stack trace on kernel panic");
145 SYSCTL_BOOL(_debug, OID_AUTO, trace_all_panics, CTLFLAG_RWTUN,
146 &trace_all_panics, 0, "Print stack traces on secondary kernel panics");
149 static int sync_on_panic = 0;
150 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RWTUN,
151 &sync_on_panic, 0, "Do a sync before rebooting from a panic");
153 static bool poweroff_on_panic = 0;
154 SYSCTL_BOOL(_kern, OID_AUTO, poweroff_on_panic, CTLFLAG_RWTUN,
155 &poweroff_on_panic, 0, "Do a power off instead of a reboot on a panic");
157 static bool powercycle_on_panic = 0;
158 SYSCTL_BOOL(_kern, OID_AUTO, powercycle_on_panic, CTLFLAG_RWTUN,
159 &powercycle_on_panic, 0, "Do a power cycle instead of a reboot on a panic");
161 static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,
162 "Shutdown environment");
165 static int show_busybufs;
167 static int show_busybufs = 1;
169 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
170 &show_busybufs, 0, "");
172 int suspend_blocked = 0;
173 SYSCTL_INT(_kern, OID_AUTO, suspend_blocked, CTLFLAG_RW,
174 &suspend_blocked, 0, "Block suspend due to a pending shutdown");
177 FEATURE(ekcd, "Encrypted kernel crash dumps support");
179 MALLOC_DEFINE(M_EKCD, "ekcd", "Encrypted kernel crash dumps data");
181 struct kerneldumpcrypto {
182 uint8_t kdc_encryption;
183 uint8_t kdc_iv[KERNELDUMP_IV_MAX_SIZE];
187 cipherInstance aes_ci;
189 struct chacha_ctx u_chacha;
191 #define kdc_ki u.u_aes.aes_ki
192 #define kdc_ci u.u_aes.aes_ci
193 #define kdc_chacha u.u_chacha
194 uint32_t kdc_dumpkeysize;
195 struct kerneldumpkey kdc_dumpkey[];
199 struct kerneldumpcomp {
201 struct compressor *kdc_stream;
206 static struct kerneldumpcomp *kerneldumpcomp_create(struct dumperinfo *di,
207 uint8_t compression);
208 static void kerneldumpcomp_destroy(struct dumperinfo *di);
209 static int kerneldumpcomp_write_cb(void *base, size_t len, off_t off, void *arg);
211 static int kerneldump_gzlevel = 6;
212 SYSCTL_INT(_kern, OID_AUTO, kerneldump_gzlevel, CTLFLAG_RWTUN,
213 &kerneldump_gzlevel, 0,
214 "Kernel crash dump compression level");
217 * Variable panicstr contains argument to first call to panic; used as flag
218 * to indicate that the kernel has already called panic.
220 const char *panicstr;
222 int dumping; /* system is dumping */
223 int rebooting; /* system is rebooting */
225 * Used to serialize between sysctl kern.shutdown.dumpdevname and list
226 * modifications via ioctl.
228 static struct mtx dumpconf_list_lk;
229 MTX_SYSINIT(dumper_configs, &dumpconf_list_lk, "dumper config list", MTX_DEF);
231 /* Our selected dumper(s). */
232 static TAILQ_HEAD(dumpconflist, dumperinfo) dumper_configs =
233 TAILQ_HEAD_INITIALIZER(dumper_configs);
235 /* Context information for dump-debuggers. */
236 static struct pcb dumppcb; /* Registers. */
237 lwpid_t dumptid; /* Thread ID. */
239 static struct cdevsw reroot_cdevsw = {
240 .d_version = D_VERSION,
244 static void poweroff_wait(void *, int);
245 static void shutdown_halt(void *junk, int howto);
246 static void shutdown_panic(void *junk, int howto);
247 static void shutdown_reset(void *junk, int howto);
248 static int kern_reroot(void);
250 /* register various local shutdown events */
252 shutdown_conf(void *unused)
255 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
257 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
258 SHUTDOWN_PRI_LAST + 100);
259 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
260 SHUTDOWN_PRI_LAST + 100);
261 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
262 SHUTDOWN_PRI_LAST + 200);
265 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
268 * The only reason this exists is to create the /dev/reroot/ directory,
269 * used by reroot code in init(8) as a mountpoint for tmpfs.
272 reroot_conf(void *unused)
277 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &cdev,
278 &reroot_cdevsw, NULL, UID_ROOT, GID_WHEEL, 0600, "reroot/reroot");
280 printf("%s: failed to create device node, error %d",
285 SYSINIT(reroot_conf, SI_SUB_DEVFS, SI_ORDER_ANY, reroot_conf, NULL);
288 * The system call that results in a reboot.
292 sys_reboot(struct thread *td, struct reboot_args *uap)
298 error = mac_system_check_reboot(td->td_ucred, uap->opt);
301 error = priv_check(td, PRIV_REBOOT);
303 if (uap->opt & RB_REROOT)
304 error = kern_reroot();
306 kern_reboot(uap->opt);
312 shutdown_nice_task_fn(void *arg, int pending __unused)
316 howto = (uintptr_t)arg;
317 /* Send a signal to init(8) and have it shutdown the world. */
319 if (howto & RB_POWEROFF)
320 kern_psignal(initproc, SIGUSR2);
321 else if (howto & RB_POWERCYCLE)
322 kern_psignal(initproc, SIGWINCH);
323 else if (howto & RB_HALT)
324 kern_psignal(initproc, SIGUSR1);
326 kern_psignal(initproc, SIGINT);
327 PROC_UNLOCK(initproc);
330 static struct task shutdown_nice_task = TASK_INITIALIZER(0,
331 &shutdown_nice_task_fn, NULL);
334 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC
337 shutdown_nice(int howto)
340 if (initproc != NULL && !SCHEDULER_STOPPED()) {
341 shutdown_nice_task.ta_context = (void *)(uintptr_t)howto;
342 taskqueue_enqueue(taskqueue_fast, &shutdown_nice_task);
345 * No init(8) running, or scheduler would not allow it
346 * to run, so simply reboot.
348 kern_reboot(howto | RB_NOSYNC);
361 if (ts.tv_sec >= 86400) {
362 printf("%ldd", (long)ts.tv_sec / 86400);
366 if (f || ts.tv_sec >= 3600) {
367 printf("%ldh", (long)ts.tv_sec / 3600);
371 if (f || ts.tv_sec >= 60) {
372 printf("%ldm", (long)ts.tv_sec / 60);
376 printf("%lds\n", (long)ts.tv_sec);
380 doadump(boolean_t textdump)
388 if (TAILQ_EMPTY(&dumper_configs))
392 dumptid = curthread->td_tid;
397 if (textdump && textdump_pending) {
399 textdump_dumpsys(TAILQ_FIRST(&dumper_configs));
403 struct dumperinfo *di;
405 TAILQ_FOREACH(di, &dumper_configs, di_next) {
417 * Shutdown the system cleanly to prepare for reboot, halt, or power off.
420 kern_reboot(int howto)
425 * Normal paths here don't hold Giant, but we can wind up here
426 * unexpectedly with it held. Drop it now so we don't have to
427 * drop and pick it up elsewhere. The paths it is locking will
428 * never be returned to, and it is preferable to preclude
429 * deadlock than to lock against code that won't ever
432 while (mtx_owned(&Giant))
437 * Bind us to the first CPU so that all shutdown code runs there. Some
438 * systems don't shutdown properly (i.e., ACPI power off) if we
439 * run on another processor.
441 if (!SCHEDULER_STOPPED()) {
442 thread_lock(curthread);
443 sched_bind(curthread, CPU_FIRST());
444 thread_unlock(curthread);
445 KASSERT(PCPU_GET(cpuid) == CPU_FIRST(),
446 ("boot: not running on cpu 0"));
449 /* We're in the process of rebooting. */
452 /* We are out of the debugger now. */
456 * Do any callouts that should be done BEFORE syncing the filesystems.
458 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
461 * Now sync filesystems
463 if (!cold && (howto & RB_NOSYNC) == 0 && once == 0) {
465 bufshutdown(show_busybufs);
473 * Ok, now do things that assume all filesystem activity has
476 EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
478 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
481 /* Now that we're going to really halt the system... */
482 EVENTHANDLER_INVOKE(shutdown_final, howto);
484 for(;;) ; /* safety against shutdown_reset not working */
489 * The system call that results in changing the rootfs.
494 struct vnode *oldrootvnode, *vp;
495 struct mount *mp, *devmp;
498 if (curproc != initproc)
502 * Mark the filesystem containing currently-running executable
503 * (the temporary copy of init(8)) busy.
505 vp = curproc->p_textvp;
506 error = vn_lock(vp, LK_SHARED);
510 error = vfs_busy(mp, MBF_NOWAIT);
514 error = vfs_busy(mp, 0);
515 vn_lock(vp, LK_SHARED | LK_RETRY);
521 if (vp->v_iflag & VI_DOOMED) {
530 * Remove the filesystem containing currently-running executable
531 * from the mount list, to prevent it from being unmounted
532 * by vfs_unmountall(), and to avoid confusing vfs_mountroot().
534 * Also preserve /dev - forcibly unmounting it could cause driver
542 mtx_lock(&mountlist_mtx);
543 TAILQ_REMOVE(&mountlist, mp, mnt_list);
544 TAILQ_REMOVE(&mountlist, devmp, mnt_list);
545 mtx_unlock(&mountlist_mtx);
547 oldrootvnode = rootvnode;
550 * Unmount everything except for the two filesystems preserved above.
555 * Add /dev back; vfs_mountroot() will move it into its new place.
557 mtx_lock(&mountlist_mtx);
558 TAILQ_INSERT_HEAD(&mountlist, devmp, mnt_list);
559 mtx_unlock(&mountlist_mtx);
564 * Mount the new rootfs.
569 * Update all references to the old rootvnode.
571 mountcheckdirs(oldrootvnode, rootvnode);
574 * Add the temporary filesystem back and unbusy it.
576 mtx_lock(&mountlist_mtx);
577 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
578 mtx_unlock(&mountlist_mtx);
585 * If the shutdown was a clean halt, behave accordingly.
588 shutdown_halt(void *junk, int howto)
591 if (howto & RB_HALT) {
593 printf("The operating system has halted.\n");
594 printf("Please press any key to reboot.\n\n");
596 case -1: /* No console, just die */
606 * Check to see if the system paniced, pause and then reboot
607 * according to the specified delay.
610 shutdown_panic(void *junk, int howto)
614 if (howto & RB_DUMP) {
615 if (panic_reboot_wait_time != 0) {
616 if (panic_reboot_wait_time != -1) {
617 printf("Automatic reboot in %d seconds - "
618 "press a key on the console to abort\n",
619 panic_reboot_wait_time);
620 for (loop = panic_reboot_wait_time * 10;
622 DELAY(1000 * 100); /* 1/10th second */
623 /* Did user type a key? */
624 if (cncheckc() != -1)
630 } else { /* zero time specified - reboot NOW */
633 printf("--> Press a key on the console to reboot,\n");
634 printf("--> or switch off the system now.\n");
640 * Everything done, now reset
643 shutdown_reset(void *junk, int howto)
646 printf("Rebooting...\n");
647 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */
650 * Acquiring smp_ipi_mtx here has a double effect:
651 * - it disables interrupts avoiding CPU0 preemption
652 * by fast handlers (thus deadlocking against other CPUs)
653 * - it avoids deadlocks against smp_rendezvous() or, more
654 * generally, threads busy-waiting, with this spinlock held,
655 * and waiting for responses by threads on other CPUs
656 * (ie. smp_tlb_shootdown()).
658 * For the !SMP case it just needs to handle the former problem.
661 mtx_lock_spin(&smp_ipi_mtx);
666 /* cpu_boot(howto); */ /* doesn't do anything at the moment */
668 /* NOTREACHED */ /* assuming reset worked */
671 #if defined(WITNESS) || defined(INVARIANT_SUPPORT)
672 static int kassert_warn_only = 0;
674 static int kassert_do_kdb = 0;
677 static int kassert_do_ktr = 0;
679 static int kassert_do_log = 1;
680 static int kassert_log_pps_limit = 4;
681 static int kassert_log_mute_at = 0;
682 static int kassert_log_panic_at = 0;
683 static int kassert_suppress_in_panic = 0;
684 static int kassert_warnings = 0;
686 SYSCTL_NODE(_debug, OID_AUTO, kassert, CTLFLAG_RW, NULL, "kassert options");
688 #ifdef KASSERT_PANIC_OPTIONAL
689 #define KASSERT_RWTUN CTLFLAG_RWTUN
691 #define KASSERT_RWTUN CTLFLAG_RDTUN
694 SYSCTL_INT(_debug_kassert, OID_AUTO, warn_only, KASSERT_RWTUN,
695 &kassert_warn_only, 0,
696 "KASSERT triggers a panic (0) or just a warning (1)");
699 SYSCTL_INT(_debug_kassert, OID_AUTO, do_kdb, KASSERT_RWTUN,
700 &kassert_do_kdb, 0, "KASSERT will enter the debugger");
704 SYSCTL_UINT(_debug_kassert, OID_AUTO, do_ktr, KASSERT_RWTUN,
706 "KASSERT does a KTR, set this to the KTRMASK you want");
709 SYSCTL_INT(_debug_kassert, OID_AUTO, do_log, KASSERT_RWTUN,
711 "If warn_only is enabled, log (1) or do not log (0) assertion violations");
713 SYSCTL_INT(_debug_kassert, OID_AUTO, warnings, KASSERT_RWTUN,
714 &kassert_warnings, 0, "number of KASSERTs that have been triggered");
716 SYSCTL_INT(_debug_kassert, OID_AUTO, log_panic_at, KASSERT_RWTUN,
717 &kassert_log_panic_at, 0, "max number of KASSERTS before we will panic");
719 SYSCTL_INT(_debug_kassert, OID_AUTO, log_pps_limit, KASSERT_RWTUN,
720 &kassert_log_pps_limit, 0, "limit number of log messages per second");
722 SYSCTL_INT(_debug_kassert, OID_AUTO, log_mute_at, KASSERT_RWTUN,
723 &kassert_log_mute_at, 0, "max number of KASSERTS to log");
725 SYSCTL_INT(_debug_kassert, OID_AUTO, suppress_in_panic, KASSERT_RWTUN,
726 &kassert_suppress_in_panic, 0,
727 "KASSERTs will be suppressed while handling a panic");
730 static int kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS);
732 SYSCTL_PROC(_debug_kassert, OID_AUTO, kassert,
733 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, NULL, 0,
734 kassert_sysctl_kassert, "I", "set to trigger a test kassert");
737 kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS)
741 error = sysctl_wire_old_buffer(req, sizeof(int));
744 error = sysctl_handle_int(oidp, &i, 0, req);
746 if (error != 0 || req->newptr == NULL)
748 KASSERT(0, ("kassert_sysctl_kassert triggered kassert %d", i));
752 #ifdef KASSERT_PANIC_OPTIONAL
754 * Called by KASSERT, this decides if we will panic
755 * or if we will log via printf and/or ktr.
758 kassert_panic(const char *fmt, ...)
760 static char buf[256];
764 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
768 * If we are suppressing secondary panics, log the warning but do not
769 * re-enter panic/kdb.
771 if (panicstr != NULL && kassert_suppress_in_panic) {
772 if (kassert_do_log) {
773 printf("KASSERT failed: %s\n", buf);
775 if (trace_all_panics && trace_on_panic)
783 * panic if we're not just warning, or if we've exceeded
784 * kassert_log_panic_at warnings.
786 if (!kassert_warn_only ||
787 (kassert_log_panic_at > 0 &&
788 kassert_warnings >= kassert_log_panic_at)) {
798 * log if we've not yet met the mute limit.
800 if (kassert_do_log &&
801 (kassert_log_mute_at == 0 ||
802 kassert_warnings < kassert_log_mute_at)) {
803 static struct timeval lasterr;
806 if (ppsratecheck(&lasterr, &curerr, kassert_log_pps_limit)) {
807 printf("KASSERT failed: %s\n", buf);
812 if (kassert_do_kdb) {
813 kdb_enter(KDB_WHY_KASSERT, buf);
816 atomic_add_int(&kassert_warnings, 1);
818 #endif /* KASSERT_PANIC_OPTIONAL */
822 * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
823 * and then reboots. If we are called twice, then we avoid trying to sync
824 * the disks as this often leads to recursive panics.
827 panic(const char *fmt, ...)
836 vpanic(const char *fmt, va_list ap)
841 struct thread *td = curthread;
842 int bootopt, newpanic;
843 static char buf[256];
849 * stop_cpus_hard(other_cpus) should prevent multiple CPUs from
850 * concurrently entering panic. Only the winner will proceed
853 if (panicstr == NULL && !kdb_active) {
854 other_cpus = all_cpus;
855 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
856 stop_cpus_hard(other_cpus);
861 * Ensure that the scheduler is stopped while panicking, even if panic
862 * has been entered from kdb.
864 td->td_stopsched = 1;
866 bootopt = RB_AUTOBOOT;
869 bootopt |= RB_NOSYNC;
877 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
880 printf("panic: %s\n", buf);
887 printf("cpuid = %d\n", PCPU_GET(cpuid));
889 printf("time = %jd\n", (intmax_t )time_second);
891 if ((newpanic || trace_all_panics) && trace_on_panic)
893 if (debugger_on_panic)
894 kdb_enter(KDB_WHY_PANIC, "panic");
896 /*thread_lock(td); */
897 td->td_flags |= TDF_INPANIC;
898 /* thread_unlock(td); */
900 bootopt |= RB_NOSYNC;
901 if (poweroff_on_panic)
902 bootopt |= RB_POWEROFF;
903 if (powercycle_on_panic)
904 bootopt |= RB_POWERCYCLE;
905 kern_reboot(bootopt);
909 * Support for poweroff delay.
911 * Please note that setting this delay too short might power off your machine
912 * before the write cache on your hard disk has been flushed, leading to
913 * soft-updates inconsistencies.
915 #ifndef POWEROFF_DELAY
916 # define POWEROFF_DELAY 5000
918 static int poweroff_delay = POWEROFF_DELAY;
920 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
921 &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)");
924 poweroff_wait(void *junk, int howto)
927 if ((howto & (RB_POWEROFF | RB_POWERCYCLE)) == 0 || poweroff_delay <= 0)
929 DELAY(poweroff_delay * 1000);
933 * Some system processes (e.g. syncer) need to be stopped at appropriate
934 * points in their main loops prior to a system shutdown, so that they
935 * won't interfere with the shutdown process (e.g. by holding a disk buf
936 * to cause sync to fail). For each of these system processes, register
937 * shutdown_kproc() as a handler for one of shutdown events.
939 static int kproc_shutdown_wait = 60;
940 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
941 &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process");
944 kproc_shutdown(void *arg, int howto)
952 p = (struct proc *)arg;
953 printf("Waiting (max %d seconds) for system process `%s' to stop... ",
954 kproc_shutdown_wait, p->p_comm);
955 error = kproc_suspend(p, kproc_shutdown_wait * hz);
957 if (error == EWOULDBLOCK)
958 printf("timed out\n");
964 kthread_shutdown(void *arg, int howto)
972 td = (struct thread *)arg;
973 printf("Waiting (max %d seconds) for system thread `%s' to stop... ",
974 kproc_shutdown_wait, td->td_name);
975 error = kthread_suspend(td, kproc_shutdown_wait * hz);
977 if (error == EWOULDBLOCK)
978 printf("timed out\n");
984 dumpdevname_sysctl_handler(SYSCTL_HANDLER_ARGS)
987 struct dumperinfo *di;
991 error = sysctl_wire_old_buffer(req, 0);
995 sbuf_new_for_sysctl(&sb, buf, sizeof(buf), req);
997 mtx_lock(&dumpconf_list_lk);
998 TAILQ_FOREACH(di, &dumper_configs, di_next) {
999 if (di != TAILQ_FIRST(&dumper_configs))
1000 sbuf_putc(&sb, ',');
1001 sbuf_cat(&sb, di->di_devname);
1003 mtx_unlock(&dumpconf_list_lk);
1005 error = sbuf_finish(&sb);
1009 SYSCTL_PROC(_kern_shutdown, OID_AUTO, dumpdevname, CTLTYPE_STRING | CTLFLAG_RD,
1010 &dumper_configs, 0, dumpdevname_sysctl_handler, "A",
1011 "Device(s) for kernel dumps");
1013 static int _dump_append(struct dumperinfo *di, void *virtual,
1014 vm_offset_t physical, size_t length);
1017 static struct kerneldumpcrypto *
1018 kerneldumpcrypto_create(size_t blocksize, uint8_t encryption,
1019 const uint8_t *key, uint32_t encryptedkeysize, const uint8_t *encryptedkey)
1021 struct kerneldumpcrypto *kdc;
1022 struct kerneldumpkey *kdk;
1023 uint32_t dumpkeysize;
1025 dumpkeysize = roundup2(sizeof(*kdk) + encryptedkeysize, blocksize);
1026 kdc = malloc(sizeof(*kdc) + dumpkeysize, M_EKCD, M_WAITOK | M_ZERO);
1028 arc4rand(kdc->kdc_iv, sizeof(kdc->kdc_iv), 0);
1030 kdc->kdc_encryption = encryption;
1031 switch (kdc->kdc_encryption) {
1032 case KERNELDUMP_ENC_AES_256_CBC:
1033 if (rijndael_makeKey(&kdc->kdc_ki, DIR_ENCRYPT, 256, key) <= 0)
1036 case KERNELDUMP_ENC_CHACHA20:
1037 chacha_keysetup(&kdc->kdc_chacha, key, 256);
1043 kdc->kdc_dumpkeysize = dumpkeysize;
1044 kdk = kdc->kdc_dumpkey;
1045 kdk->kdk_encryption = kdc->kdc_encryption;
1046 memcpy(kdk->kdk_iv, kdc->kdc_iv, sizeof(kdk->kdk_iv));
1047 kdk->kdk_encryptedkeysize = htod32(encryptedkeysize);
1048 memcpy(kdk->kdk_encryptedkey, encryptedkey, encryptedkeysize);
1052 explicit_bzero(kdc, sizeof(*kdc) + dumpkeysize);
1058 kerneldumpcrypto_init(struct kerneldumpcrypto *kdc)
1060 uint8_t hash[SHA256_DIGEST_LENGTH];
1062 struct kerneldumpkey *kdk;
1071 * When a user enters ddb it can write a crash dump multiple times.
1072 * Each time it should be encrypted using a different IV.
1075 SHA256_Update(&ctx, kdc->kdc_iv, sizeof(kdc->kdc_iv));
1076 SHA256_Final(hash, &ctx);
1077 bcopy(hash, kdc->kdc_iv, sizeof(kdc->kdc_iv));
1079 switch (kdc->kdc_encryption) {
1080 case KERNELDUMP_ENC_AES_256_CBC:
1081 if (rijndael_cipherInit(&kdc->kdc_ci, MODE_CBC,
1082 kdc->kdc_iv) <= 0) {
1087 case KERNELDUMP_ENC_CHACHA20:
1088 chacha_ivsetup(&kdc->kdc_chacha, kdc->kdc_iv, NULL);
1095 kdk = kdc->kdc_dumpkey;
1096 memcpy(kdk->kdk_iv, kdc->kdc_iv, sizeof(kdk->kdk_iv));
1098 explicit_bzero(hash, sizeof(hash));
1103 kerneldumpcrypto_dumpkeysize(const struct kerneldumpcrypto *kdc)
1108 return (kdc->kdc_dumpkeysize);
1112 static struct kerneldumpcomp *
1113 kerneldumpcomp_create(struct dumperinfo *di, uint8_t compression)
1115 struct kerneldumpcomp *kdcomp;
1118 switch (compression) {
1119 case KERNELDUMP_COMP_GZIP:
1120 format = COMPRESS_GZIP;
1122 case KERNELDUMP_COMP_ZSTD:
1123 format = COMPRESS_ZSTD;
1129 kdcomp = malloc(sizeof(*kdcomp), M_DUMPER, M_WAITOK | M_ZERO);
1130 kdcomp->kdc_format = compression;
1131 kdcomp->kdc_stream = compressor_init(kerneldumpcomp_write_cb,
1132 format, di->maxiosize, kerneldump_gzlevel, di);
1133 if (kdcomp->kdc_stream == NULL) {
1134 free(kdcomp, M_DUMPER);
1137 kdcomp->kdc_buf = malloc(di->maxiosize, M_DUMPER, M_WAITOK | M_NODUMP);
1142 kerneldumpcomp_destroy(struct dumperinfo *di)
1144 struct kerneldumpcomp *kdcomp;
1146 kdcomp = di->kdcomp;
1149 compressor_fini(kdcomp->kdc_stream);
1150 explicit_bzero(kdcomp->kdc_buf, di->maxiosize);
1151 free(kdcomp->kdc_buf, M_DUMPER);
1152 free(kdcomp, M_DUMPER);
1156 * Must not be present on global list.
1159 free_single_dumper(struct dumperinfo *di)
1165 if (di->blockbuf != NULL) {
1166 explicit_bzero(di->blockbuf, di->blocksize);
1167 free(di->blockbuf, M_DUMPER);
1170 kerneldumpcomp_destroy(di);
1173 if (di->kdcrypto != NULL) {
1174 explicit_bzero(di->kdcrypto, sizeof(*di->kdcrypto) +
1175 di->kdcrypto->kdc_dumpkeysize);
1176 free(di->kdcrypto, M_EKCD);
1180 explicit_bzero(di, sizeof(*di));
1184 /* Registration of dumpers */
1186 dumper_insert(const struct dumperinfo *di_template, const char *devname,
1187 const struct diocskerneldump_arg *kda)
1189 struct dumperinfo *newdi, *listdi;
1194 index = kda->kda_index;
1195 MPASS(index != KDA_REMOVE && index != KDA_REMOVE_DEV &&
1196 index != KDA_REMOVE_ALL);
1198 error = priv_check(curthread, PRIV_SETDUMPER);
1202 newdi = malloc(sizeof(*newdi) + strlen(devname) + 1, M_DUMPER, M_WAITOK
1204 memcpy(newdi, di_template, sizeof(*newdi));
1205 newdi->blockbuf = NULL;
1206 newdi->kdcrypto = NULL;
1207 newdi->kdcomp = NULL;
1208 strcpy(newdi->di_devname, devname);
1210 if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
1212 newdi->kdcrypto = kerneldumpcrypto_create(di_template->blocksize,
1213 kda->kda_encryption, kda->kda_key,
1214 kda->kda_encryptedkeysize, kda->kda_encryptedkey);
1215 if (newdi->kdcrypto == NULL) {
1224 if (kda->kda_compression != KERNELDUMP_COMP_NONE) {
1226 * We can't support simultaneous unpadded block cipher
1227 * encryption and compression because there is no guarantee the
1228 * length of the compressed result is exactly a multiple of the
1229 * cipher block size.
1231 if (kda->kda_encryption == KERNELDUMP_ENC_AES_256_CBC) {
1235 newdi->kdcomp = kerneldumpcomp_create(newdi,
1236 kda->kda_compression);
1237 if (newdi->kdcomp == NULL) {
1243 newdi->blockbuf = malloc(newdi->blocksize, M_DUMPER, M_WAITOK | M_ZERO);
1245 /* Add the new configuration to the queue */
1246 mtx_lock(&dumpconf_list_lk);
1248 TAILQ_FOREACH(listdi, &dumper_configs, di_next) {
1250 TAILQ_INSERT_BEFORE(listdi, newdi, di_next);
1257 TAILQ_INSERT_TAIL(&dumper_configs, newdi, di_next);
1258 mtx_unlock(&dumpconf_list_lk);
1263 free_single_dumper(newdi);
1268 dumper_config_match(const struct dumperinfo *di, const char *devname,
1269 const struct diocskerneldump_arg *kda)
1271 if (kda->kda_index == KDA_REMOVE_ALL)
1274 if (strcmp(di->di_devname, devname) != 0)
1278 * Allow wildcard removal of configs matching a device on g_dev_orphan.
1280 if (kda->kda_index == KDA_REMOVE_DEV)
1283 if (di->kdcomp != NULL) {
1284 if (di->kdcomp->kdc_format != kda->kda_compression)
1286 } else if (kda->kda_compression != KERNELDUMP_COMP_NONE)
1289 if (di->kdcrypto != NULL) {
1290 if (di->kdcrypto->kdc_encryption != kda->kda_encryption)
1293 * Do we care to verify keys match to delete? It seems weird
1294 * to expect multiple fallback dump configurations on the same
1295 * device that only differ in crypto key.
1299 if (kda->kda_encryption != KERNELDUMP_ENC_NONE)
1306 dumper_remove(const char *devname, const struct diocskerneldump_arg *kda)
1308 struct dumperinfo *di, *sdi;
1312 error = priv_check(curthread, PRIV_SETDUMPER);
1317 * Try to find a matching configuration, and kill it.
1319 * NULL 'kda' indicates remove any configuration matching 'devname',
1320 * which may remove multiple configurations in atypical configurations.
1323 mtx_lock(&dumpconf_list_lk);
1324 TAILQ_FOREACH_SAFE(di, &dumper_configs, di_next, sdi) {
1325 if (dumper_config_match(di, devname, kda)) {
1327 TAILQ_REMOVE(&dumper_configs, di, di_next);
1328 free_single_dumper(di);
1331 mtx_unlock(&dumpconf_list_lk);
1333 /* Only produce ENOENT if a more targeted match didn't match. */
1334 if (!found && kda->kda_index == KDA_REMOVE)
1340 dump_check_bounds(struct dumperinfo *di, off_t offset, size_t length)
1343 if (di->mediasize > 0 && length != 0 && (offset < di->mediaoffset ||
1344 offset - di->mediaoffset + length > di->mediasize)) {
1345 if (di->kdcomp != NULL && offset >= di->mediaoffset) {
1347 "Compressed dump failed to fit in device boundaries.\n");
1351 printf("Attempt to write outside dump device boundaries.\n"
1352 "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
1353 (intmax_t)offset, (intmax_t)di->mediaoffset,
1354 (uintmax_t)length, (intmax_t)di->mediasize);
1357 if (length % di->blocksize != 0) {
1358 printf("Attempt to write partial block of length %ju.\n",
1362 if (offset % di->blocksize != 0) {
1363 printf("Attempt to write at unaligned offset %jd.\n",
1373 dump_encrypt(struct kerneldumpcrypto *kdc, uint8_t *buf, size_t size)
1376 switch (kdc->kdc_encryption) {
1377 case KERNELDUMP_ENC_AES_256_CBC:
1378 if (rijndael_blockEncrypt(&kdc->kdc_ci, &kdc->kdc_ki, buf,
1379 8 * size, buf) <= 0) {
1382 if (rijndael_cipherInit(&kdc->kdc_ci, MODE_CBC,
1383 buf + size - 16 /* IV size for AES-256-CBC */) <= 0) {
1387 case KERNELDUMP_ENC_CHACHA20:
1388 chacha_encrypt_bytes(&kdc->kdc_chacha, buf, buf, size);
1397 /* Encrypt data and call dumper. */
1399 dump_encrypted_write(struct dumperinfo *di, void *virtual,
1400 vm_offset_t physical, off_t offset, size_t length)
1402 static uint8_t buf[KERNELDUMP_BUFFER_SIZE];
1403 struct kerneldumpcrypto *kdc;
1409 while (length > 0) {
1410 nbytes = MIN(length, sizeof(buf));
1411 bcopy(virtual, buf, nbytes);
1413 if (dump_encrypt(kdc, buf, nbytes) != 0)
1416 error = dump_write(di, buf, physical, offset, nbytes);
1421 virtual = (void *)((uint8_t *)virtual + nbytes);
1430 kerneldumpcomp_write_cb(void *base, size_t length, off_t offset, void *arg)
1432 struct dumperinfo *di;
1433 size_t resid, rlength;
1438 if (length % di->blocksize != 0) {
1440 * This must be the final write after flushing the compression
1441 * stream. Write as many full blocks as possible and stash the
1442 * residual data in the dumper's block buffer. It will be
1443 * padded and written in dump_finish().
1445 rlength = rounddown(length, di->blocksize);
1447 error = _dump_append(di, base, 0, rlength);
1451 resid = length - rlength;
1452 memmove(di->blockbuf, (uint8_t *)base + rlength, resid);
1453 di->kdcomp->kdc_resid = resid;
1456 return (_dump_append(di, base, 0, length));
1460 * Write kernel dump headers at the beginning and end of the dump extent.
1461 * Write the kernel dump encryption key after the leading header if we were
1462 * configured to do so.
1465 dump_write_headers(struct dumperinfo *di, struct kerneldumpheader *kdh)
1468 struct kerneldumpcrypto *kdc;
1476 hdrsz = sizeof(*kdh);
1477 if (hdrsz > di->blocksize)
1482 key = kdc->kdc_dumpkey;
1483 keysize = kerneldumpcrypto_dumpkeysize(kdc);
1490 * If the dump device has special handling for headers, let it take care
1491 * of writing them out.
1493 if (di->dumper_hdr != NULL)
1494 return (di->dumper_hdr(di, kdh, key, keysize));
1496 if (hdrsz == di->blocksize)
1500 memset(buf, 0, di->blocksize);
1501 memcpy(buf, kdh, hdrsz);
1504 extent = dtoh64(kdh->dumpextent);
1507 error = dump_write(di, kdc->kdc_dumpkey, 0,
1508 di->mediaoffset + di->mediasize - di->blocksize - extent -
1515 error = dump_write(di, buf, 0,
1516 di->mediaoffset + di->mediasize - 2 * di->blocksize - extent -
1517 keysize, di->blocksize);
1519 error = dump_write(di, buf, 0, di->mediaoffset + di->mediasize -
1520 di->blocksize, di->blocksize);
1525 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This is to
1526 * protect us from metadata and metadata from us.
1528 #define SIZEOF_METADATA (64 * 1024)
1531 * Do some preliminary setup for a kernel dump: initialize state for encryption,
1532 * if requested, and make sure that we have enough space on the dump device.
1534 * We set things up so that the dump ends before the last sector of the dump
1535 * device, at which the trailing header is written.
1537 * +-----------+------+-----+----------------------------+------+
1538 * | | lhdr | key | ... kernel dump ... | thdr |
1539 * +-----------+------+-----+----------------------------+------+
1540 * 1 blk opt <------- dump extent --------> 1 blk
1542 * Dumps written using dump_append() start at the beginning of the extent.
1543 * Uncompressed dumps will use the entire extent, but compressed dumps typically
1544 * will not. The true length of the dump is recorded in the leading and trailing
1545 * headers once the dump has been completed.
1547 * The dump device may provide a callback, in which case it will initialize
1548 * dumpoff and take care of laying out the headers.
1551 dump_start(struct dumperinfo *di, struct kerneldumpheader *kdh)
1553 uint64_t dumpextent, span;
1558 error = kerneldumpcrypto_init(di->kdcrypto);
1561 keysize = kerneldumpcrypto_dumpkeysize(di->kdcrypto);
1567 if (di->dumper_start != NULL) {
1568 error = di->dumper_start(di);
1570 dumpextent = dtoh64(kdh->dumpextent);
1571 span = SIZEOF_METADATA + dumpextent + 2 * di->blocksize +
1573 if (di->mediasize < span) {
1574 if (di->kdcomp == NULL)
1578 * We don't yet know how much space the compressed dump
1579 * will occupy, so try to use the whole swap partition
1580 * (minus the first 64KB) in the hope that the
1581 * compressed dump will fit. If that doesn't turn out to
1582 * be enough, the bounds checking in dump_write()
1583 * will catch us and cause the dump to fail.
1585 dumpextent = di->mediasize - span + dumpextent;
1586 kdh->dumpextent = htod64(dumpextent);
1590 * The offset at which to begin writing the dump.
1592 di->dumpoff = di->mediaoffset + di->mediasize - di->blocksize -
1595 di->origdumpoff = di->dumpoff;
1600 _dump_append(struct dumperinfo *di, void *virtual, vm_offset_t physical,
1606 if (di->kdcrypto != NULL)
1607 error = dump_encrypted_write(di, virtual, physical, di->dumpoff,
1611 error = dump_write(di, virtual, physical, di->dumpoff, length);
1613 di->dumpoff += length;
1618 * Write to the dump device starting at dumpoff. When compression is enabled,
1619 * writes to the device will be performed using a callback that gets invoked
1620 * when the compression stream's output buffer is full.
1623 dump_append(struct dumperinfo *di, void *virtual, vm_offset_t physical,
1628 if (di->kdcomp != NULL) {
1629 /* Bounce through a buffer to avoid CRC errors. */
1630 if (length > di->maxiosize)
1632 buf = di->kdcomp->kdc_buf;
1633 memmove(buf, virtual, length);
1634 return (compressor_write(di->kdcomp->kdc_stream, buf, length));
1636 return (_dump_append(di, virtual, physical, length));
1640 * Write to the dump device at the specified offset.
1643 dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
1644 off_t offset, size_t length)
1648 error = dump_check_bounds(di, offset, length);
1651 return (di->dumper(di->priv, virtual, physical, offset, length));
1655 * Perform kernel dump finalization: flush the compression stream, if necessary,
1656 * write the leading and trailing kernel dump headers now that we know the true
1657 * length of the dump, and optionally write the encryption key following the
1661 dump_finish(struct dumperinfo *di, struct kerneldumpheader *kdh)
1665 if (di->kdcomp != NULL) {
1666 error = compressor_flush(di->kdcomp->kdc_stream);
1667 if (error == EAGAIN) {
1668 /* We have residual data in di->blockbuf. */
1669 error = dump_write(di, di->blockbuf, 0, di->dumpoff,
1671 di->dumpoff += di->kdcomp->kdc_resid;
1672 di->kdcomp->kdc_resid = 0;
1678 * We now know the size of the compressed dump, so update the
1679 * header accordingly and recompute parity.
1681 kdh->dumplength = htod64(di->dumpoff - di->origdumpoff);
1683 kdh->parity = kerneldump_parity(kdh);
1685 compressor_reset(di->kdcomp->kdc_stream);
1688 error = dump_write_headers(di, kdh);
1692 (void)dump_write(di, NULL, 0, 0, 0);
1697 dump_init_header(const struct dumperinfo *di, struct kerneldumpheader *kdh,
1698 char *magic, uint32_t archver, uint64_t dumplen)
1702 bzero(kdh, sizeof(*kdh));
1703 strlcpy(kdh->magic, magic, sizeof(kdh->magic));
1704 strlcpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
1705 kdh->version = htod32(KERNELDUMPVERSION);
1706 kdh->architectureversion = htod32(archver);
1707 kdh->dumplength = htod64(dumplen);
1708 kdh->dumpextent = kdh->dumplength;
1709 kdh->dumptime = htod64(time_second);
1711 kdh->dumpkeysize = htod32(kerneldumpcrypto_dumpkeysize(di->kdcrypto));
1713 kdh->dumpkeysize = 0;
1715 kdh->blocksize = htod32(di->blocksize);
1716 strlcpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
1717 dstsize = sizeof(kdh->versionstring);
1718 if (strlcpy(kdh->versionstring, version, dstsize) >= dstsize)
1719 kdh->versionstring[dstsize - 2] = '\n';
1720 if (panicstr != NULL)
1721 strlcpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
1722 if (di->kdcomp != NULL)
1723 kdh->compression = di->kdcomp->kdc_format;
1724 kdh->parity = kerneldump_parity(kdh);
1728 DB_SHOW_COMMAND(panic, db_show_panic)
1731 if (panicstr == NULL)
1732 db_printf("panicstr not set\n");
1734 db_printf("panic: %s\n", panicstr);