xref: /freebsd/sys/kern/kern_shutdown.c (revision 1c5151f3f8236186e502cd3c15745aab2043f995)
1ad4240feSJulian Elischer /*-
2ad4240feSJulian Elischer  * Copyright (c) 1986, 1988, 1991, 1993
3ad4240feSJulian Elischer  *	The Regents of the University of California.  All rights reserved.
4ad4240feSJulian Elischer  * (c) UNIX System Laboratories, Inc.
5ad4240feSJulian Elischer  * All or some portions of this file are derived from material licensed
6ad4240feSJulian Elischer  * to the University of California by American Telephone and Telegraph
7ad4240feSJulian Elischer  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8ad4240feSJulian Elischer  * the permission of UNIX System Laboratories, Inc.
9ad4240feSJulian Elischer  *
10ad4240feSJulian Elischer  * Redistribution and use in source and binary forms, with or without
11ad4240feSJulian Elischer  * modification, are permitted provided that the following conditions
12ad4240feSJulian Elischer  * are met:
13ad4240feSJulian Elischer  * 1. Redistributions of source code must retain the above copyright
14ad4240feSJulian Elischer  *    notice, this list of conditions and the following disclaimer.
15ad4240feSJulian Elischer  * 2. Redistributions in binary form must reproduce the above copyright
16ad4240feSJulian Elischer  *    notice, this list of conditions and the following disclaimer in the
17ad4240feSJulian Elischer  *    documentation and/or other materials provided with the distribution.
18ad4240feSJulian Elischer  * 4. Neither the name of the University nor the names of its contributors
19ad4240feSJulian Elischer  *    may be used to endorse or promote products derived from this software
20ad4240feSJulian Elischer  *    without specific prior written permission.
21ad4240feSJulian Elischer  *
22ad4240feSJulian Elischer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23ad4240feSJulian Elischer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ad4240feSJulian Elischer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ad4240feSJulian Elischer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26ad4240feSJulian Elischer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27ad4240feSJulian Elischer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28ad4240feSJulian Elischer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29ad4240feSJulian Elischer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30ad4240feSJulian Elischer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31ad4240feSJulian Elischer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ad4240feSJulian Elischer  * SUCH DAMAGE.
33ad4240feSJulian Elischer  *
34ad4240feSJulian Elischer  *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
35ad4240feSJulian Elischer  */
36ad4240feSJulian Elischer 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40618c7db3SRobert Watson #include "opt_ddb.h"
412d50560aSMarcel Moolenaar #include "opt_kdb.h"
426d58e6cbSBruce Evans #include "opt_panic.h"
439923b511SScott Long #include "opt_sched.h"
442be767e0SAttilio Rao #include "opt_watchdog.h"
45ad4240feSJulian Elischer 
46ad4240feSJulian Elischer #include <sys/param.h>
47ad4240feSJulian Elischer #include <sys/systm.h>
489626b608SPoul-Henning Kamp #include <sys/bio.h>
49fc8f7066SBruce Evans #include <sys/buf.h>
501d79f1bbSJohn Baldwin #include <sys/conf.h>
511d79f1bbSJohn Baldwin #include <sys/cons.h>
521d79f1bbSJohn Baldwin #include <sys/eventhandler.h>
5376ca6f88SJamie Gritton #include <sys/jail.h>
542d50560aSMarcel Moolenaar #include <sys/kdb.h>
55ad4240feSJulian Elischer #include <sys/kernel.h>
56e6592ee5SPeter Wemm #include <sys/kerneldump.h>
575e950839SLuoqi Chen #include <sys/kthread.h>
58dcd7d9b7SMaxim Sobolev #include <sys/malloc.h>
59ac0ad63fSBruce Evans #include <sys/mount.h>
60acd3428bSRobert Watson #include <sys/priv.h>
611d79f1bbSJohn Baldwin #include <sys/proc.h>
621d79f1bbSJohn Baldwin #include <sys/reboot.h>
631d79f1bbSJohn Baldwin #include <sys/resourcevar.h>
6420e25d7dSPeter Wemm #include <sys/sched.h>
65248bb937SAttilio Rao #include <sys/smp.h>
66ad4240feSJulian Elischer #include <sys/sysctl.h>
67ad4240feSJulian Elischer #include <sys/sysproto.h>
68fa2b39a1SAttilio Rao #include <sys/vnode.h>
692be767e0SAttilio Rao #ifdef SW_WATCHDOG
702be767e0SAttilio Rao #include <sys/watchdog.h>
712be767e0SAttilio Rao #endif
72ad4240feSJulian Elischer 
73618c7db3SRobert Watson #include <ddb/ddb.h>
74618c7db3SRobert Watson 
7526502503SMarcel Moolenaar #include <machine/cpu.h>
76d39e457bSPoul-Henning Kamp #include <machine/pcb.h>
77752dff3dSJake Burkholder #include <machine/smp.h>
78ad4240feSJulian Elischer 
79aed55708SRobert Watson #include <security/mac/mac_framework.h>
80aed55708SRobert Watson 
810909f38aSPawel Jakub Dawidek #include <vm/vm.h>
820909f38aSPawel Jakub Dawidek #include <vm/vm_object.h>
830909f38aSPawel Jakub Dawidek #include <vm/vm_page.h>
840909f38aSPawel Jakub Dawidek #include <vm/vm_pager.h>
850909f38aSPawel Jakub Dawidek #include <vm/swap_pager.h>
860909f38aSPawel Jakub Dawidek 
87ad4240feSJulian Elischer #include <sys/signalvar.h>
88ad4240feSJulian Elischer 
89ad4240feSJulian Elischer #ifndef PANIC_REBOOT_WAIT_TIME
90ad4240feSJulian Elischer #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
91ad4240feSJulian Elischer #endif
92ad4240feSJulian Elischer 
93ad4240feSJulian Elischer /*
94ad4240feSJulian Elischer  * Note that stdarg.h and the ANSI style va_start macro is used for both
95ad4240feSJulian Elischer  * ANSI and traditional C compilers.
96ad4240feSJulian Elischer  */
97ad4240feSJulian Elischer #include <machine/stdarg.h>
98ad4240feSJulian Elischer 
992d50560aSMarcel Moolenaar #ifdef KDB
1002d50560aSMarcel Moolenaar #ifdef KDB_UNATTENDED
1019959b1a8SMike Smith int debugger_on_panic = 0;
102ad4240feSJulian Elischer #else
1039959b1a8SMike Smith int debugger_on_panic = 1;
104ad4240feSJulian Elischer #endif
1053d7618d8SDavid E. O'Brien SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic,
106*1c5151f3SDavid E. O'Brien     CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN,
107*1c5151f3SDavid E. O'Brien     &debugger_on_panic, 0, "Run debugger on kernel panic");
10808a9c205SAndriy Gapon TUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic);
109e485b64bSJohn Baldwin 
1102d50560aSMarcel Moolenaar #ifdef KDB_TRACE
11108a9c205SAndriy Gapon static int trace_on_panic = 1;
112e485b64bSJohn Baldwin #else
11308a9c205SAndriy Gapon static int trace_on_panic = 0;
114e485b64bSJohn Baldwin #endif
1153d7618d8SDavid E. O'Brien SYSCTL_INT(_debug, OID_AUTO, trace_on_panic,
116*1c5151f3SDavid E. O'Brien     CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN,
117*1c5151f3SDavid E. O'Brien     &trace_on_panic, 0, "Print stack trace on kernel panic");
11808a9c205SAndriy Gapon TUNABLE_INT("debug.trace_on_panic", &trace_on_panic);
1192d50560aSMarcel Moolenaar #endif /* KDB */
120ad4240feSJulian Elischer 
12108a9c205SAndriy Gapon static int sync_on_panic = 0;
12208a9c205SAndriy Gapon SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
123259ed917SPeter Wemm 	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
12408a9c205SAndriy Gapon TUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
125259ed917SPeter Wemm 
12635370593SAndriy Gapon static int stop_scheduler_on_panic = 0;
12735370593SAndriy Gapon SYSCTL_INT(_kern, OID_AUTO, stop_scheduler_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
12835370593SAndriy Gapon     &stop_scheduler_on_panic, 0, "stop scheduler upon entering panic");
12935370593SAndriy Gapon TUNABLE_INT("kern.stop_scheduler_on_panic", &stop_scheduler_on_panic);
13035370593SAndriy Gapon 
1316472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,
1326472ac3dSEd Schouten     "Shutdown environment");
133db82a982SMike Smith 
134fa2b39a1SAttilio Rao #ifndef DIAGNOSTIC
135fa2b39a1SAttilio Rao static int show_busybufs;
136fa2b39a1SAttilio Rao #else
137fa2b39a1SAttilio Rao static int show_busybufs = 1;
138fa2b39a1SAttilio Rao #endif
139fa2b39a1SAttilio Rao SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
140fa2b39a1SAttilio Rao 	&show_busybufs, 0, "");
141fa2b39a1SAttilio Rao 
1425230cfd2SJulian Elischer /*
143ad4240feSJulian Elischer  * Variable panicstr contains argument to first call to panic; used as flag
144ad4240feSJulian Elischer  * to indicate that the kernel has already called panic.
145ad4240feSJulian Elischer  */
146ad4240feSJulian Elischer const char *panicstr;
147ad4240feSJulian Elischer 
14835370593SAndriy Gapon int stop_scheduler;			/* system stopped CPUs for panic */
14916a011f9SPaul Saab int dumping;				/* system is dumping */
15036a52c3cSJeff Roberson int rebooting;				/* system is rebooting */
15181661c94SPoul-Henning Kamp static struct dumperinfo dumper;	/* our selected dumper */
1522d50560aSMarcel Moolenaar 
1532d50560aSMarcel Moolenaar /* Context information for dump-debuggers. */
1542d50560aSMarcel Moolenaar static struct pcb dumppcb;		/* Registers. */
1552d50560aSMarcel Moolenaar static lwpid_t dumptid;			/* Thread ID. */
15616a011f9SPaul Saab 
15782acbcf5SPeter Wemm static void poweroff_wait(void *, int);
15882acbcf5SPeter Wemm static void shutdown_halt(void *junk, int howto);
15982acbcf5SPeter Wemm static void shutdown_panic(void *junk, int howto);
16082acbcf5SPeter Wemm static void shutdown_reset(void *junk, int howto);
161f06a54f0SPoul-Henning Kamp 
162fcb893a8SMike Smith /* register various local shutdown events */
163fcb893a8SMike Smith static void
164fcb893a8SMike Smith shutdown_conf(void *unused)
165fcb893a8SMike Smith {
166e95499bdSAlfred Perlstein 
167e95499bdSAlfred Perlstein 	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
168fd104c15SRebecca Cran 	    SHUTDOWN_PRI_FIRST);
169e95499bdSAlfred Perlstein 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
170e95499bdSAlfred Perlstein 	    SHUTDOWN_PRI_LAST + 100);
171e95499bdSAlfred Perlstein 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
172e95499bdSAlfred Perlstein 	    SHUTDOWN_PRI_LAST + 100);
173e95499bdSAlfred Perlstein 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
174e95499bdSAlfred Perlstein 	    SHUTDOWN_PRI_LAST + 200);
175fcb893a8SMike Smith }
176ad4240feSJulian Elischer 
177237fdd78SRobert Watson SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
178fcb893a8SMike Smith 
179ad4240feSJulian Elischer /*
1800c14ff0eSRobert Watson  * The system call that results in a reboot.
181ad4240feSJulian Elischer  */
182835a82eeSMatthew Dillon /* ARGSUSED */
183ad4240feSJulian Elischer int
1848451d0ddSKip Macy sys_reboot(struct thread *td, struct reboot_args *uap)
185ad4240feSJulian Elischer {
186ad4240feSJulian Elischer 	int error;
187ad4240feSJulian Elischer 
188a2ecb9b7SRobert Watson 	error = 0;
189a2ecb9b7SRobert Watson #ifdef MAC
19030d239bcSRobert Watson 	error = mac_system_check_reboot(td->td_ucred, uap->opt);
191a2ecb9b7SRobert Watson #endif
192a2ecb9b7SRobert Watson 	if (error == 0)
193acd3428bSRobert Watson 		error = priv_check(td, PRIV_REBOOT);
194a2ecb9b7SRobert Watson 	if (error == 0) {
195835a82eeSMatthew Dillon 		mtx_lock(&Giant);
19676e18b25SMarcel Moolenaar 		kern_reboot(uap->opt);
197835a82eeSMatthew Dillon 		mtx_unlock(&Giant);
198a2ecb9b7SRobert Watson 	}
199835a82eeSMatthew Dillon 	return (error);
200ad4240feSJulian Elischer }
201ad4240feSJulian Elischer 
202ad4240feSJulian Elischer /*
203ad4240feSJulian Elischer  * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
204ad4240feSJulian Elischer  */
2053e755f76SMike Smith static int shutdown_howto = 0;
2063e755f76SMike Smith 
207ad4240feSJulian Elischer void
2083e755f76SMike Smith shutdown_nice(int howto)
209ad4240feSJulian Elischer {
210e95499bdSAlfred Perlstein 
2113e755f76SMike Smith 	shutdown_howto = howto;
2123e755f76SMike Smith 
213ad4240feSJulian Elischer 	/* Send a signal to init(8) and have it shutdown the world */
214ad4240feSJulian Elischer 	if (initproc != NULL) {
21587729a2bSJohn Baldwin 		PROC_LOCK(initproc);
2168451d0ddSKip Macy 		kern_psignal(initproc, SIGINT);
21787729a2bSJohn Baldwin 		PROC_UNLOCK(initproc);
218ad4240feSJulian Elischer 	} else {
219ad4240feSJulian Elischer 		/* No init(8) running, so simply reboot */
22076e18b25SMarcel Moolenaar 		kern_reboot(RB_NOSYNC);
221ad4240feSJulian Elischer 	}
222ad4240feSJulian Elischer 	return;
223ad4240feSJulian Elischer }
224ad4240feSJulian Elischer static int	waittime = -1;
225ad4240feSJulian Elischer 
22672dfe7a3SPoul-Henning Kamp static void
22782acbcf5SPeter Wemm print_uptime(void)
22872dfe7a3SPoul-Henning Kamp {
22972dfe7a3SPoul-Henning Kamp 	int f;
23072dfe7a3SPoul-Henning Kamp 	struct timespec ts;
23172dfe7a3SPoul-Henning Kamp 
23272dfe7a3SPoul-Henning Kamp 	getnanouptime(&ts);
23372dfe7a3SPoul-Henning Kamp 	printf("Uptime: ");
23472dfe7a3SPoul-Henning Kamp 	f = 0;
23572dfe7a3SPoul-Henning Kamp 	if (ts.tv_sec >= 86400) {
2364a6404dfSJohn Baldwin 		printf("%ldd", (long)ts.tv_sec / 86400);
23772dfe7a3SPoul-Henning Kamp 		ts.tv_sec %= 86400;
23872dfe7a3SPoul-Henning Kamp 		f = 1;
23972dfe7a3SPoul-Henning Kamp 	}
24072dfe7a3SPoul-Henning Kamp 	if (f || ts.tv_sec >= 3600) {
2414a6404dfSJohn Baldwin 		printf("%ldh", (long)ts.tv_sec / 3600);
24272dfe7a3SPoul-Henning Kamp 		ts.tv_sec %= 3600;
24372dfe7a3SPoul-Henning Kamp 		f = 1;
24472dfe7a3SPoul-Henning Kamp 	}
24572dfe7a3SPoul-Henning Kamp 	if (f || ts.tv_sec >= 60) {
2464a6404dfSJohn Baldwin 		printf("%ldm", (long)ts.tv_sec / 60);
24772dfe7a3SPoul-Henning Kamp 		ts.tv_sec %= 60;
24872dfe7a3SPoul-Henning Kamp 		f = 1;
24972dfe7a3SPoul-Henning Kamp 	}
2504a6404dfSJohn Baldwin 	printf("%lds\n", (long)ts.tv_sec);
25172dfe7a3SPoul-Henning Kamp }
25272dfe7a3SPoul-Henning Kamp 
253299cceefSMarcel Moolenaar int
254299cceefSMarcel Moolenaar doadump(boolean_t textdump)
255d39e457bSPoul-Henning Kamp {
256299cceefSMarcel Moolenaar 	boolean_t coredump;
257e95499bdSAlfred Perlstein 
258299cceefSMarcel Moolenaar 	if (dumping)
259299cceefSMarcel Moolenaar 		return (EBUSY);
260299cceefSMarcel Moolenaar 	if (dumper.dumper == NULL)
261299cceefSMarcel Moolenaar 		return (ENXIO);
262f6449d9dSJulian Elischer 
263d39e457bSPoul-Henning Kamp 	savectx(&dumppcb);
2642d50560aSMarcel Moolenaar 	dumptid = curthread->td_tid;
265d39e457bSPoul-Henning Kamp 	dumping++;
266299cceefSMarcel Moolenaar 
267299cceefSMarcel Moolenaar 	coredump = TRUE;
268618c7db3SRobert Watson #ifdef DDB
269299cceefSMarcel Moolenaar 	if (textdump && textdump_pending) {
270299cceefSMarcel Moolenaar 		coredump = FALSE;
271618c7db3SRobert Watson 		textdump_dumpsys(&dumper);
272299cceefSMarcel Moolenaar 	}
273618c7db3SRobert Watson #endif
274299cceefSMarcel Moolenaar 	if (coredump)
275d39e457bSPoul-Henning Kamp 		dumpsys(&dumper);
276299cceefSMarcel Moolenaar 
2779e473363SRuslan Ermilov 	dumping--;
278299cceefSMarcel Moolenaar 	return (0);
279d39e457bSPoul-Henning Kamp }
280d39e457bSPoul-Henning Kamp 
281d07f87a2SDon Lewis static int
282d07f87a2SDon Lewis isbufbusy(struct buf *bp)
283d07f87a2SDon Lewis {
284d07f87a2SDon Lewis 	if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
285d638e093SAttilio Rao 	    BUF_ISLOCKED(bp)) ||
286d07f87a2SDon Lewis 	    ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
287d07f87a2SDon Lewis 		return (1);
288d07f87a2SDon Lewis 	return (0);
289d07f87a2SDon Lewis }
290d07f87a2SDon Lewis 
291ad4240feSJulian Elischer /*
29270ce93f4SNate Lawson  * Shutdown the system cleanly to prepare for reboot, halt, or power off.
293ad4240feSJulian Elischer  */
29476e18b25SMarcel Moolenaar void
29576e18b25SMarcel Moolenaar kern_reboot(int howto)
296ad4240feSJulian Elischer {
297b6915bdbSDon Lewis 	static int first_buf_printf = 1;
298ad4240feSJulian Elischer 
299f7ebc7ceSMarcel Moolenaar #if defined(SMP)
30070ce93f4SNate Lawson 	/*
30170ce93f4SNate Lawson 	 * Bind us to CPU 0 so that all shutdown code runs there.  Some
30270ce93f4SNate Lawson 	 * systems don't shutdown properly (i.e., ACPI power off) if we
30370ce93f4SNate Lawson 	 * run on another processor.
30470ce93f4SNate Lawson 	 */
30535370593SAndriy Gapon 	if (!SCHEDULER_STOPPED()) {
306982d11f8SJeff Roberson 		thread_lock(curthread);
30720e25d7dSPeter Wemm 		sched_bind(curthread, 0);
308982d11f8SJeff Roberson 		thread_unlock(curthread);
30935370593SAndriy Gapon 		KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
31035370593SAndriy Gapon 	}
31120e25d7dSPeter Wemm #endif
31236a52c3cSJeff Roberson 	/* We're in the process of rebooting. */
31336a52c3cSJeff Roberson 	rebooting = 1;
31420e25d7dSPeter Wemm 
3153e755f76SMike Smith 	/* collect extra flags that shutdown_nice might have set */
3163e755f76SMike Smith 	howto |= shutdown_howto;
3173e755f76SMike Smith 
31861e96500SJohn Baldwin 	/* We are out of the debugger now. */
3192d50560aSMarcel Moolenaar 	kdb_active = 0;
32061e96500SJohn Baldwin 
3215230cfd2SJulian Elischer 	/*
3225230cfd2SJulian Elischer 	 * Do any callouts that should be done BEFORE syncing the filesystems.
3235230cfd2SJulian Elischer 	 */
324fcb893a8SMike Smith 	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
3255230cfd2SJulian Elischer 
3265230cfd2SJulian Elischer 	/*
3275230cfd2SJulian Elischer 	 * Now sync filesystems
3285230cfd2SJulian Elischer 	 */
329ad4240feSJulian Elischer 	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
330ad4240feSJulian Elischer 		register struct buf *bp;
33162820f25SJason Evans 		int iter, nbusy, pbusy;
3320c0b25aeSJohn Baldwin #ifndef PREEMPTION
33362820f25SJason Evans 		int subiter;
3340c0b25aeSJohn Baldwin #endif
335ad4240feSJulian Elischer 
336ad4240feSJulian Elischer 		waittime = 0;
337ad4240feSJulian Elischer 
3382be767e0SAttilio Rao #ifdef SW_WATCHDOG
3392be767e0SAttilio Rao 		wdog_kern_pat(WD_LASTVAL);
3402be767e0SAttilio Rao #endif
3418451d0ddSKip Macy 		sys_sync(curthread, NULL);
342ad4240feSJulian Elischer 
343b1897c19SJulian Elischer 		/*
344b1897c19SJulian Elischer 		 * With soft updates, some buffers that are
345b1897c19SJulian Elischer 		 * written will be remarked as dirty until other
346b1897c19SJulian Elischer 		 * buffers are written.
347b1897c19SJulian Elischer 		 */
34862820f25SJason Evans 		for (iter = pbusy = 0; iter < 20; iter++) {
349ad4240feSJulian Elischer 			nbusy = 0;
350d07f87a2SDon Lewis 			for (bp = &buf[nbuf]; --bp >= buf; )
351d07f87a2SDon Lewis 				if (isbufbusy(bp))
352ad4240feSJulian Elischer 					nbusy++;
353b6915bdbSDon Lewis 			if (nbusy == 0) {
354b6915bdbSDon Lewis 				if (first_buf_printf)
35537abb77fSPoul-Henning Kamp 					printf("All buffers synced.");
356ad4240feSJulian Elischer 				break;
357b6915bdbSDon Lewis 			}
358b6915bdbSDon Lewis 			if (first_buf_printf) {
359b6915bdbSDon Lewis 				printf("Syncing disks, buffers remaining... ");
360b6915bdbSDon Lewis 				first_buf_printf = 0;
361b6915bdbSDon Lewis 			}
362ad4240feSJulian Elischer 			printf("%d ", nbusy);
36362820f25SJason Evans 			if (nbusy < pbusy)
36462820f25SJason Evans 				iter = 0;
36562820f25SJason Evans 			pbusy = nbusy;
3662be767e0SAttilio Rao #ifdef SW_WATCHDOG
3672be767e0SAttilio Rao 			wdog_kern_pat(WD_LASTVAL);
3682be767e0SAttilio Rao #endif
3698451d0ddSKip Macy 			sys_sync(curthread, NULL);
3700c0b25aeSJohn Baldwin 
3710c0b25aeSJohn Baldwin #ifdef PREEMPTION
3720c0b25aeSJohn Baldwin 			/*
3730c0b25aeSJohn Baldwin 			 * Drop Giant and spin for a while to allow
3740c0b25aeSJohn Baldwin 			 * interrupt threads to run.
3750c0b25aeSJohn Baldwin 			 */
3760c0b25aeSJohn Baldwin 			DROP_GIANT();
3770c0b25aeSJohn Baldwin 			DELAY(50000 * iter);
3780c0b25aeSJohn Baldwin 			PICKUP_GIANT();
3790c0b25aeSJohn Baldwin #else
3800c0b25aeSJohn Baldwin 			/*
3810c0b25aeSJohn Baldwin 			 * Drop Giant and context switch several times to
3820c0b25aeSJohn Baldwin 			 * allow interrupt threads to run.
3830c0b25aeSJohn Baldwin 			 */
384c86b6ff5SJohn Baldwin 			DROP_GIANT();
38562820f25SJason Evans 			for (subiter = 0; subiter < 50 * iter; subiter++) {
386982d11f8SJeff Roberson 				thread_lock(curthread);
387bf0acc27SJohn Baldwin 				mi_switch(SW_VOL, NULL);
388982d11f8SJeff Roberson 				thread_unlock(curthread);
38962820f25SJason Evans 				DELAY(1000);
39062820f25SJason Evans 			}
39120cdcc5bSJohn Baldwin 			PICKUP_GIANT();
3920c0b25aeSJohn Baldwin #endif
393ad4240feSJulian Elischer 		}
394c8c216d5SNate Lawson 		printf("\n");
395d02d6d04SMike Smith 		/*
396d02d6d04SMike Smith 		 * Count only busy local buffers to prevent forcing
397d02d6d04SMike Smith 		 * a fsck if we're just a client of a wedged NFS server
398d02d6d04SMike Smith 		 */
399d02d6d04SMike Smith 		nbusy = 0;
400d02d6d04SMike Smith 		for (bp = &buf[nbuf]; --bp >= buf; ) {
401d07f87a2SDon Lewis 			if (isbufbusy(bp)) {
402c5690651SPoul-Henning Kamp #if 0
403c5690651SPoul-Henning Kamp /* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
404f3732fd1SPoul-Henning Kamp 				if (bp->b_dev == NULL) {
4050429e37aSPoul-Henning Kamp 					TAILQ_REMOVE(&mountlist,
40667812eacSKirk McKusick 					    bp->b_vp->v_mount, mnt_list);
4079c111b31SPoul-Henning Kamp 					continue;
408dfd5dee1SPeter Wemm 				}
409c5690651SPoul-Henning Kamp #endif
4109c111b31SPoul-Henning Kamp 				nbusy++;
411fa2b39a1SAttilio Rao 				if (show_busybufs > 0) {
4129c111b31SPoul-Henning Kamp 					printf(
413fa2b39a1SAttilio Rao 	    "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
414fa2b39a1SAttilio Rao 					    nbusy, bp, bp->b_vp, bp->b_flags,
415fa2b39a1SAttilio Rao 					    (intmax_t)bp->b_blkno,
416fa2b39a1SAttilio Rao 					    (intmax_t)bp->b_lblkno);
417fa2b39a1SAttilio Rao 					BUF_LOCKPRINTINFO(bp);
418fa2b39a1SAttilio Rao 					if (show_busybufs > 1)
419fa2b39a1SAttilio Rao 						vn_printf(bp->b_vp,
420fa2b39a1SAttilio Rao 						    "vnode content: ");
421fa2b39a1SAttilio Rao 				}
4229c111b31SPoul-Henning Kamp 			}
423d02d6d04SMike Smith 		}
424ad4240feSJulian Elischer 		if (nbusy) {
425ad4240feSJulian Elischer 			/*
426ad4240feSJulian Elischer 			 * Failed to sync all blocks. Indicate this and don't
427ad4240feSJulian Elischer 			 * unmount filesystems (thus forcing an fsck on reboot).
428ad4240feSJulian Elischer 			 */
429b6915bdbSDon Lewis 			printf("Giving up on %d buffers\n", nbusy);
430ad4240feSJulian Elischer 			DELAY(5000000);	/* 5 seconds */
431ad4240feSJulian Elischer 		} else {
432b6915bdbSDon Lewis 			if (!first_buf_printf)
433b6915bdbSDon Lewis 				printf("Final sync complete\n");
434ad4240feSJulian Elischer 			/*
435ad4240feSJulian Elischer 			 * Unmount filesystems
436ad4240feSJulian Elischer 			 */
437ad4240feSJulian Elischer 			if (panicstr == 0)
438ad4240feSJulian Elischer 				vfs_unmountall();
439ad4240feSJulian Elischer 		}
4400909f38aSPawel Jakub Dawidek 		swapoff_all();
441ad4240feSJulian Elischer 		DELAY(100000);		/* wait for console output to finish */
442ad4240feSJulian Elischer 	}
4435230cfd2SJulian Elischer 
44472dfe7a3SPoul-Henning Kamp 	print_uptime();
44572dfe7a3SPoul-Henning Kamp 
4465230cfd2SJulian Elischer 	/*
4475230cfd2SJulian Elischer 	 * Ok, now do things that assume all filesystem activity has
4485230cfd2SJulian Elischer 	 * been completed.
4495230cfd2SJulian Elischer 	 */
450fcb893a8SMike Smith 	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
45170ce93f4SNate Lawson 
452f6449d9dSJulian Elischer 	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
453299cceefSMarcel Moolenaar 		doadump(TRUE);
4542cfa0a03SJustin T. Gibbs 
4552cfa0a03SJustin T. Gibbs 	/* Now that we're going to really halt the system... */
456fcb893a8SMike Smith 	EVENTHANDLER_INVOKE(shutdown_final, howto);
4572cfa0a03SJustin T. Gibbs 
458fcb893a8SMike Smith 	for(;;) ;	/* safety against shutdown_reset not working */
459fcb893a8SMike Smith 	/* NOTREACHED */
460fcb893a8SMike Smith }
461fcb893a8SMike Smith 
462fcb893a8SMike Smith /*
463fcb893a8SMike Smith  * If the shutdown was a clean halt, behave accordingly.
464fcb893a8SMike Smith  */
465fcb893a8SMike Smith static void
466fcb893a8SMike Smith shutdown_halt(void *junk, int howto)
467fcb893a8SMike Smith {
468e95499bdSAlfred Perlstein 
469ad4240feSJulian Elischer 	if (howto & RB_HALT) {
470ad4240feSJulian Elischer 		printf("\n");
471ad4240feSJulian Elischer 		printf("The operating system has halted.\n");
472ad4240feSJulian Elischer 		printf("Please press any key to reboot.\n\n");
473d13d3630SJulian Elischer 		switch (cngetc()) {
474d13d3630SJulian Elischer 		case -1:		/* No console, just die */
475d13d3630SJulian Elischer 			cpu_halt();
476d13d3630SJulian Elischer 			/* NOTREACHED */
477d13d3630SJulian Elischer 		default:
4782cfa0a03SJustin T. Gibbs 			howto &= ~RB_HALT;
479d13d3630SJulian Elischer 			break;
480d13d3630SJulian Elischer 		}
481fcb893a8SMike Smith 	}
482fcb893a8SMike Smith }
483ad4240feSJulian Elischer 
484fcb893a8SMike Smith /*
485fcb893a8SMike Smith  * Check to see if the system paniced, pause and then reboot
486fcb893a8SMike Smith  * according to the specified delay.
487fcb893a8SMike Smith  */
488fcb893a8SMike Smith static void
489fcb893a8SMike Smith shutdown_panic(void *junk, int howto)
490fcb893a8SMike Smith {
491fcb893a8SMike Smith 	int loop;
492fcb893a8SMike Smith 
493fcb893a8SMike Smith 	if (howto & RB_DUMP) {
494ad4240feSJulian Elischer 		if (PANIC_REBOOT_WAIT_TIME != 0) {
495ad4240feSJulian Elischer 			if (PANIC_REBOOT_WAIT_TIME != -1) {
4962cfa0a03SJustin T. Gibbs 				printf("Automatic reboot in %d seconds - "
4972cfa0a03SJustin T. Gibbs 				       "press a key on the console to abort\n",
498ad4240feSJulian Elischer 					PANIC_REBOOT_WAIT_TIME);
4992cfa0a03SJustin T. Gibbs 				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
5002cfa0a03SJustin T. Gibbs 				     loop > 0; --loop) {
501ad4240feSJulian Elischer 					DELAY(1000 * 100); /* 1/10th second */
502a7f8f2abSBruce Evans 					/* Did user type a key? */
503a7f8f2abSBruce Evans 					if (cncheckc() != -1)
504ad4240feSJulian Elischer 						break;
505ad4240feSJulian Elischer 				}
506ad4240feSJulian Elischer 				if (!loop)
507fcb893a8SMike Smith 					return;
508ad4240feSJulian Elischer 			}
509ad4240feSJulian Elischer 		} else { /* zero time specified - reboot NOW */
510fcb893a8SMike Smith 			return;
511ad4240feSJulian Elischer 		}
512422702e9SNik Clayton 		printf("--> Press a key on the console to reboot,\n");
513422702e9SNik Clayton 		printf("--> or switch off the system now.\n");
514ad4240feSJulian Elischer 		cngetc();
515ad4240feSJulian Elischer 	}
516fcb893a8SMike Smith }
517fcb893a8SMike Smith 
518fcb893a8SMike Smith /*
519fcb893a8SMike Smith  * Everything done, now reset
520fcb893a8SMike Smith  */
521fcb893a8SMike Smith static void
522fcb893a8SMike Smith shutdown_reset(void *junk, int howto)
523fcb893a8SMike Smith {
524e95499bdSAlfred Perlstein 
525ad4240feSJulian Elischer 	printf("Rebooting...\n");
526ad4240feSJulian Elischer 	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
527248bb937SAttilio Rao 
528248bb937SAttilio Rao 	/*
529248bb937SAttilio Rao 	 * Acquiring smp_ipi_mtx here has a double effect:
530248bb937SAttilio Rao 	 * - it disables interrupts avoiding CPU0 preemption
531248bb937SAttilio Rao 	 *   by fast handlers (thus deadlocking  against other CPUs)
532248bb937SAttilio Rao 	 * - it avoids deadlocks against smp_rendezvous() or, more
533248bb937SAttilio Rao 	 *   generally, threads busy-waiting, with this spinlock held,
534248bb937SAttilio Rao 	 *   and waiting for responses by threads on other CPUs
535248bb937SAttilio Rao 	 *   (ie. smp_tlb_shootdown()).
5360a2d5feaSAttilio Rao 	 *
5370a2d5feaSAttilio Rao 	 * For the !SMP case it just needs to handle the former problem.
538248bb937SAttilio Rao 	 */
5390a2d5feaSAttilio Rao #ifdef SMP
540248bb937SAttilio Rao 	mtx_lock_spin(&smp_ipi_mtx);
5410a2d5feaSAttilio Rao #else
5420a2d5feaSAttilio Rao 	spinlock_enter();
5430a2d5feaSAttilio Rao #endif
544248bb937SAttilio Rao 
545269fb9d7SJulian Elischer 	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
546ad4240feSJulian Elischer 	cpu_reset();
547fcb893a8SMike Smith 	/* NOTREACHED */ /* assuming reset worked */
548ad4240feSJulian Elischer }
549ad4240feSJulian Elischer 
550ad4240feSJulian Elischer /*
551ad4240feSJulian Elischer  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
552ad4240feSJulian Elischer  * and then reboots.  If we are called twice, then we avoid trying to sync
553ad4240feSJulian Elischer  * the disks as this often leads to recursive panics.
554ad4240feSJulian Elischer  */
555ad4240feSJulian Elischer void
5569a6dc4b6SPoul-Henning Kamp panic(const char *fmt, ...)
557ad4240feSJulian Elischer {
55864dd590eSAndriy Gapon #ifdef SMP
55964dd590eSAndriy Gapon 	static volatile u_int panic_cpu = NOCPU;
56035370593SAndriy Gapon 	cpuset_t other_cpus;
56164dd590eSAndriy Gapon #endif
562fe799533SAndrew Gallatin 	struct thread *td = curthread;
563e485b64bSJohn Baldwin 	int bootopt, newpanic;
564ad4240feSJulian Elischer 	va_list ap;
56599237364SAndrey A. Chernov 	static char buf[256];
566ad4240feSJulian Elischer 
56735370593SAndriy Gapon 	if (stop_scheduler_on_panic)
56835370593SAndriy Gapon 		spinlock_enter();
56935370593SAndriy Gapon 	else
57041a4e90eSKonstantin Belousov 		critical_enter();
57135370593SAndriy Gapon 
5720384fff8SJason Evans #ifdef SMP
5731a5333c3SJohn Baldwin 	/*
5741a5333c3SJohn Baldwin 	 * We don't want multiple CPU's to panic at the same time, so we
5750711ca46SJohn Baldwin 	 * use panic_cpu as a simple spinlock.  We have to keep checking
5760711ca46SJohn Baldwin 	 * panic_cpu if we are spinning in case the panic on the first
5771a5333c3SJohn Baldwin 	 * CPU is canceled.
5781a5333c3SJohn Baldwin 	 */
5790711ca46SJohn Baldwin 	if (panic_cpu != PCPU_GET(cpuid))
5800711ca46SJohn Baldwin 		while (atomic_cmpset_int(&panic_cpu, NOCPU,
5810711ca46SJohn Baldwin 		    PCPU_GET(cpuid)) == 0)
5820711ca46SJohn Baldwin 			while (panic_cpu != NOCPU)
5830711ca46SJohn Baldwin 				; /* nothing */
58435370593SAndriy Gapon 
58535370593SAndriy Gapon 	if (stop_scheduler_on_panic) {
58635370593SAndriy Gapon 		if (panicstr == NULL && !kdb_active) {
58735370593SAndriy Gapon 			other_cpus = all_cpus;
58835370593SAndriy Gapon 			CPU_CLR(PCPU_GET(cpuid), &other_cpus);
58935370593SAndriy Gapon 			stop_cpus_hard(other_cpus);
59035370593SAndriy Gapon 		}
59135370593SAndriy Gapon 
59235370593SAndriy Gapon 		/*
59335370593SAndriy Gapon 		 * We set stop_scheduler here and not in the block above,
59435370593SAndriy Gapon 		 * because we want to ensure that if panic has been called and
59535370593SAndriy Gapon 		 * stop_scheduler_on_panic is true, then stop_scheduler will
59635370593SAndriy Gapon 		 * always be set.  Even if panic has been entered from kdb.
59735370593SAndriy Gapon 		 */
59835370593SAndriy Gapon 		stop_scheduler = 1;
59935370593SAndriy Gapon 	}
6000384fff8SJason Evans #endif
6010384fff8SJason Evans 
602e3adb685SAttilio Rao 	bootopt = RB_AUTOBOOT;
603e485b64bSJohn Baldwin 	newpanic = 0;
604ad4240feSJulian Elischer 	if (panicstr)
605ad4240feSJulian Elischer 		bootopt |= RB_NOSYNC;
606e485b64bSJohn Baldwin 	else {
607e3adb685SAttilio Rao 		bootopt |= RB_DUMP;
608ad4240feSJulian Elischer 		panicstr = fmt;
609e485b64bSJohn Baldwin 		newpanic = 1;
610e485b64bSJohn Baldwin 	}
611ad4240feSJulian Elischer 
612ad4240feSJulian Elischer 	va_start(ap, fmt);
6134f1b4577SIan Dowse 	if (newpanic) {
6142127f260SArchie Cobbs 		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
61599237364SAndrey A. Chernov 		panicstr = buf;
6169a6dc4b6SPoul-Henning Kamp 		printf("panic: %s\n", buf);
6174f1b4577SIan Dowse 	} else {
6184f1b4577SIan Dowse 		printf("panic: ");
6194f1b4577SIan Dowse 		vprintf(fmt, ap);
6209a6dc4b6SPoul-Henning Kamp 		printf("\n");
6214f1b4577SIan Dowse 	}
6224f1b4577SIan Dowse 	va_end(ap);
62347d81897SSteve Passe #ifdef SMP
62455c45354SJohn Baldwin 	printf("cpuid = %d\n", PCPU_GET(cpuid));
6252bcc63c5SJohn Baldwin #endif
626ad4240feSJulian Elischer 
6272d50560aSMarcel Moolenaar #ifdef KDB
628e485b64bSJohn Baldwin 	if (newpanic && trace_on_panic)
6292d50560aSMarcel Moolenaar 		kdb_backtrace();
630ad4240feSJulian Elischer 	if (debugger_on_panic)
6313de213ccSRobert Watson 		kdb_enter(KDB_WHY_PANIC, "panic");
6321432aa0cSJohn Baldwin #endif
633982d11f8SJeff Roberson 	/*thread_lock(td); */
634fe799533SAndrew Gallatin 	td->td_flags |= TDF_INPANIC;
635982d11f8SJeff Roberson 	/* thread_unlock(td); */
636259ed917SPeter Wemm 	if (!sync_on_panic)
637259ed917SPeter Wemm 		bootopt |= RB_NOSYNC;
63835370593SAndriy Gapon 	if (!stop_scheduler_on_panic)
63941a4e90eSKonstantin Belousov 		critical_exit();
64076e18b25SMarcel Moolenaar 	kern_reboot(bootopt);
641ad4240feSJulian Elischer }
642ad4240feSJulian Elischer 
643e0d898b4SJulian Elischer /*
644db82a982SMike Smith  * Support for poweroff delay.
645b22692bdSNick Hibma  *
646b22692bdSNick Hibma  * Please note that setting this delay too short might power off your machine
647b22692bdSNick Hibma  * before the write cache on your hard disk has been flushed, leading to
648b22692bdSNick Hibma  * soft-updates inconsistencies.
649db82a982SMike Smith  */
6509eec6969SMike Smith #ifndef POWEROFF_DELAY
6519eec6969SMike Smith # define POWEROFF_DELAY 5000
6529eec6969SMike Smith #endif
6539eec6969SMike Smith static int poweroff_delay = POWEROFF_DELAY;
6549eec6969SMike Smith 
655db82a982SMike Smith SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
6563eb9ab52SEitan Adler     &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)");
657db82a982SMike Smith 
658fcb893a8SMike Smith static void
659fcb893a8SMike Smith poweroff_wait(void *junk, int howto)
660db82a982SMike Smith {
661e95499bdSAlfred Perlstein 
662db82a982SMike Smith 	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
663db82a982SMike Smith 		return;
664db82a982SMike Smith 	DELAY(poweroff_delay * 1000);
665db82a982SMike Smith }
6665e950839SLuoqi Chen 
6675e950839SLuoqi Chen /*
6685e950839SLuoqi Chen  * Some system processes (e.g. syncer) need to be stopped at appropriate
6695e950839SLuoqi Chen  * points in their main loops prior to a system shutdown, so that they
6705e950839SLuoqi Chen  * won't interfere with the shutdown process (e.g. by holding a disk buf
6715e950839SLuoqi Chen  * to cause sync to fail).  For each of these system processes, register
6725e950839SLuoqi Chen  * shutdown_kproc() as a handler for one of shutdown events.
6735e950839SLuoqi Chen  */
6745e950839SLuoqi Chen static int kproc_shutdown_wait = 60;
6755e950839SLuoqi Chen SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
6763eb9ab52SEitan Adler     &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process");
6775e950839SLuoqi Chen 
6785e950839SLuoqi Chen void
679ffc831daSJohn Baldwin kproc_shutdown(void *arg, int howto)
6805e950839SLuoqi Chen {
6815e950839SLuoqi Chen 	struct proc *p;
6825e950839SLuoqi Chen 	int error;
6835e950839SLuoqi Chen 
6845e950839SLuoqi Chen 	if (panicstr)
6855e950839SLuoqi Chen 		return;
6865e950839SLuoqi Chen 
6875e950839SLuoqi Chen 	p = (struct proc *)arg;
688b1c81391SNate Lawson 	printf("Waiting (max %d seconds) for system process `%s' to stop...",
6894f9d48e4SJohn Baldwin 	    kproc_shutdown_wait, p->p_comm);
6903745c395SJulian Elischer 	error = kproc_suspend(p, kproc_shutdown_wait * hz);
6915e950839SLuoqi Chen 
6925e950839SLuoqi Chen 	if (error == EWOULDBLOCK)
693b1c81391SNate Lawson 		printf("timed out\n");
6945e950839SLuoqi Chen 	else
695b1c81391SNate Lawson 		printf("done\n");
6965e950839SLuoqi Chen }
69781661c94SPoul-Henning Kamp 
6987ab24ea3SJulian Elischer void
6997ab24ea3SJulian Elischer kthread_shutdown(void *arg, int howto)
7007ab24ea3SJulian Elischer {
7017ab24ea3SJulian Elischer 	struct thread *td;
7027ab24ea3SJulian Elischer 	int error;
7037ab24ea3SJulian Elischer 
7047ab24ea3SJulian Elischer 	if (panicstr)
7057ab24ea3SJulian Elischer 		return;
7067ab24ea3SJulian Elischer 
7077ab24ea3SJulian Elischer 	td = (struct thread *)arg;
7087ab24ea3SJulian Elischer 	printf("Waiting (max %d seconds) for system thread `%s' to stop...",
7094f9d48e4SJohn Baldwin 	    kproc_shutdown_wait, td->td_name);
7107ab24ea3SJulian Elischer 	error = kthread_suspend(td, kproc_shutdown_wait * hz);
7117ab24ea3SJulian Elischer 
7127ab24ea3SJulian Elischer 	if (error == EWOULDBLOCK)
7137ab24ea3SJulian Elischer 		printf("timed out\n");
7147ab24ea3SJulian Elischer 	else
7157ab24ea3SJulian Elischer 		printf("done\n");
7167ab24ea3SJulian Elischer }
7177ab24ea3SJulian Elischer 
71881661c94SPoul-Henning Kamp /* Registration of dumpers */
71981661c94SPoul-Henning Kamp int
72081661c94SPoul-Henning Kamp set_dumper(struct dumperinfo *di)
72181661c94SPoul-Henning Kamp {
722e95499bdSAlfred Perlstein 
72381661c94SPoul-Henning Kamp 	if (di == NULL) {
72481661c94SPoul-Henning Kamp 		bzero(&dumper, sizeof dumper);
72581661c94SPoul-Henning Kamp 		return (0);
72681661c94SPoul-Henning Kamp 	}
72781661c94SPoul-Henning Kamp 	if (dumper.dumper != NULL)
72881661c94SPoul-Henning Kamp 		return (EBUSY);
72981661c94SPoul-Henning Kamp 	dumper = *di;
73081661c94SPoul-Henning Kamp 	return (0);
73181661c94SPoul-Henning Kamp }
73281661c94SPoul-Henning Kamp 
733007b1b7bSRuslan Ermilov /* Call dumper with bounds checking. */
734007b1b7bSRuslan Ermilov int
735007b1b7bSRuslan Ermilov dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
736007b1b7bSRuslan Ermilov     off_t offset, size_t length)
737007b1b7bSRuslan Ermilov {
738007b1b7bSRuslan Ermilov 
739007b1b7bSRuslan Ermilov 	if (length != 0 && (offset < di->mediaoffset ||
740007b1b7bSRuslan Ermilov 	    offset - di->mediaoffset + length > di->mediasize)) {
74158379067SAttilio Rao 		printf("Attempt to write outside dump device boundaries.\n"
74258379067SAttilio Rao 	    "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
74358379067SAttilio Rao 		    (intmax_t)offset, (intmax_t)di->mediaoffset,
74458379067SAttilio Rao 		    (uintmax_t)length, (intmax_t)di->mediasize);
74558379067SAttilio Rao 		return (ENOSPC);
746007b1b7bSRuslan Ermilov 	}
747007b1b7bSRuslan Ermilov 	return (di->dumper(di->priv, virtual, physical, offset, length));
748007b1b7bSRuslan Ermilov }
749007b1b7bSRuslan Ermilov 
750e6592ee5SPeter Wemm void
751e6592ee5SPeter Wemm mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
752e6592ee5SPeter Wemm     uint64_t dumplen, uint32_t blksz)
753e6592ee5SPeter Wemm {
754e6592ee5SPeter Wemm 
755e6592ee5SPeter Wemm 	bzero(kdh, sizeof(*kdh));
756e6592ee5SPeter Wemm 	strncpy(kdh->magic, magic, sizeof(kdh->magic));
757e6592ee5SPeter Wemm 	strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
758e6592ee5SPeter Wemm 	kdh->version = htod32(KERNELDUMPVERSION);
759e6592ee5SPeter Wemm 	kdh->architectureversion = htod32(archver);
760e6592ee5SPeter Wemm 	kdh->dumplength = htod64(dumplen);
761e6592ee5SPeter Wemm 	kdh->dumptime = htod64(time_second);
762e6592ee5SPeter Wemm 	kdh->blocksize = htod32(blksz);
763c1f19219SJamie Gritton 	strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
764e6592ee5SPeter Wemm 	strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
765e6592ee5SPeter Wemm 	if (panicstr != NULL)
766e6592ee5SPeter Wemm 		strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
767e6592ee5SPeter Wemm 	kdh->parity = kerneldump_parity(kdh);
768e6592ee5SPeter Wemm }
769