kern_shutdown.c revision 138217
117658Sjulian/*-
217658Sjulian * Copyright (c) 1986, 1988, 1991, 1993
317658Sjulian *	The Regents of the University of California.  All rights reserved.
417658Sjulian * (c) UNIX System Laboratories, Inc.
517658Sjulian * All or some portions of this file are derived from material licensed
617658Sjulian * to the University of California by American Telephone and Telegraph
717658Sjulian * Co. or Unix System Laboratories, Inc. and are reproduced herein with
817658Sjulian * the permission of UNIX System Laboratories, Inc.
917658Sjulian *
1017658Sjulian * Redistribution and use in source and binary forms, with or without
1117658Sjulian * modification, are permitted provided that the following conditions
1217658Sjulian * are met:
1317658Sjulian * 1. Redistributions of source code must retain the above copyright
1417658Sjulian *    notice, this list of conditions and the following disclaimer.
1517658Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1617658Sjulian *    notice, this list of conditions and the following disclaimer in the
1717658Sjulian *    documentation and/or other materials provided with the distribution.
1817658Sjulian * 4. Neither the name of the University nor the names of its contributors
1917658Sjulian *    may be used to endorse or promote products derived from this software
2017658Sjulian *    without specific prior written permission.
2117658Sjulian *
2217658Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2317658Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2417658Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2517658Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2617658Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2717658Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2817658Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2917658Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3017658Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3117658Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3217658Sjulian * SUCH DAMAGE.
3317658Sjulian *
3417658Sjulian *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
3517658Sjulian */
3617658Sjulian
37116182Sobrien#include <sys/cdefs.h>
38116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_shutdown.c 138217 2004-11-30 06:21:38Z njl $");
39116182Sobrien
40131927Smarcel#include "opt_kdb.h"
41106024Srwatson#include "opt_mac.h"
4228976Sbde#include "opt_panic.h"
4328976Sbde#include "opt_show_busybufs.h"
44134649Sscottl#include "opt_sched.h"
4517658Sjulian
4617658Sjulian#include <sys/param.h>
4717658Sjulian#include <sys/systm.h>
4860041Sphk#include <sys/bio.h>
4931275Sbde#include <sys/buf.h>
5078767Sjhb#include <sys/conf.h>
5178767Sjhb#include <sys/cons.h>
5278767Sjhb#include <sys/eventhandler.h>
53131927Smarcel#include <sys/kdb.h>
5417658Sjulian#include <sys/kernel.h>
5555539Sluoqi#include <sys/kthread.h>
56106024Srwatson#include <sys/mac.h>
5789601Ssobomax#include <sys/malloc.h>
5821776Sbde#include <sys/mount.h>
5978767Sjhb#include <sys/proc.h>
6078767Sjhb#include <sys/reboot.h>
6178767Sjhb#include <sys/resourcevar.h>
62137263Speter#include <sys/sched.h>
6378767Sjhb#include <sys/smp.h>		/* smp_active */
6417658Sjulian#include <sys/sysctl.h>
6517658Sjulian#include <sys/sysproto.h>
6678767Sjhb#include <sys/vnode.h>
6717658Sjulian
68118990Smarcel#include <machine/cpu.h>
6994169Sphk#include <machine/pcb.h>
7091778Sjake#include <machine/smp.h>
7117658Sjulian
7217658Sjulian#include <sys/signalvar.h>
7317658Sjulian
7417658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
7517658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
7617658Sjulian#endif
7717658Sjulian
7817658Sjulian/*
7917658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
8017658Sjulian * ANSI and traditional C compilers.
8117658Sjulian */
8217658Sjulian#include <machine/stdarg.h>
8317658Sjulian
84131927Smarcel#ifdef KDB
85131927Smarcel#ifdef KDB_UNATTENDED
8642135Smsmithint debugger_on_panic = 0;
8717658Sjulian#else
8842135Smsmithint debugger_on_panic = 1;
8917658Sjulian#endif
9017658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
9146381Sbillf	&debugger_on_panic, 0, "Run debugger on kernel panic");
92103647Sjhb
93131927Smarcel#ifdef KDB_TRACE
94103647Sjhbint trace_on_panic = 1;
95103647Sjhb#else
96103647Sjhbint trace_on_panic = 0;
9717658Sjulian#endif
98103647SjhbSYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW,
99103647Sjhb	&trace_on_panic, 0, "Print stack trace on kernel panic");
100131927Smarcel#endif /* KDB */
10117658Sjulian
102132506Srwatsonint sync_on_panic = 0;
10385202SpeterSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW,
10485202Speter	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
10585202Speter
10643436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
10743436Smsmith
10817658Sjulian/*
10917658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
11017658Sjulian * to indicate that the kernel has already called panic.
11117658Sjulian */
11217658Sjulianconst char *panicstr;
11317658Sjulian
11493496Sphkint dumping;				/* system is dumping */
11593496Sphkstatic struct dumperinfo dumper;	/* our selected dumper */
11667093Sps
117131927Smarcel/* Context information for dump-debuggers. */
118131927Smarcelstatic struct pcb dumppcb;		/* Registers. */
119131927Smarcelstatic lwpid_t dumptid;			/* Thread ID. */
120131927Smarcel
12165395Speterstatic void boot(int) __dead2;
12265395Speterstatic void poweroff_wait(void *, int);
12365395Speterstatic void shutdown_halt(void *junk, int howto);
12465395Speterstatic void shutdown_panic(void *junk, int howto);
12565395Speterstatic void shutdown_reset(void *junk, int howto);
12617658Sjulian
12750107Smsmith/* register various local shutdown events */
128110859Salfredstatic void
12950107Smsmithshutdown_conf(void *unused)
13050107Smsmith{
131110859Salfred
132110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
133110859Salfred	    SHUTDOWN_PRI_FIRST);
134110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
135110859Salfred	    SHUTDOWN_PRI_LAST + 100);
136110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
137110859Salfred	    SHUTDOWN_PRI_LAST + 100);
138110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
139110859Salfred	    SHUTDOWN_PRI_LAST + 200);
14050107Smsmith}
14148868Sphk
14250107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
14350107Smsmith
14417658Sjulian/*
14517658Sjulian * The system call that results in a reboot
14682749Sdillon *
14782749Sdillon * MPSAFE
14817658Sjulian */
14982749Sdillon/* ARGSUSED */
15017658Sjulianint
15183366Sjulianreboot(struct thread *td, struct reboot_args *uap)
15217658Sjulian{
15317658Sjulian	int error;
15417658Sjulian
155106024Srwatson	error = 0;
156106024Srwatson#ifdef MAC
157106024Srwatson	error = mac_check_system_reboot(td->td_ucred, uap->opt);
158106024Srwatson#endif
159106024Srwatson	if (error == 0)
160106024Srwatson		error = suser(td);
161106024Srwatson	if (error == 0) {
162106024Srwatson		mtx_lock(&Giant);
16382749Sdillon		boot(uap->opt);
164106024Srwatson		mtx_unlock(&Giant);
165106024Srwatson	}
16682749Sdillon	return (error);
16717658Sjulian}
16817658Sjulian
16917658Sjulian/*
17017658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
17117658Sjulian */
17265268Smsmithstatic int shutdown_howto = 0;
17365268Smsmith
17417658Sjulianvoid
17565268Smsmithshutdown_nice(int howto)
17617658Sjulian{
177110859Salfred
17865268Smsmith	shutdown_howto = howto;
179110859Salfred
18017658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
18117658Sjulian	if (initproc != NULL) {
18273913Sjhb		PROC_LOCK(initproc);
18317658Sjulian		psignal(initproc, SIGINT);
18473913Sjhb		PROC_UNLOCK(initproc);
18517658Sjulian	} else {
18617658Sjulian		/* No init(8) running, so simply reboot */
18717658Sjulian		boot(RB_NOSYNC);
18817658Sjulian	}
18917658Sjulian	return;
19017658Sjulian}
19117658Sjulianstatic int	waittime = -1;
19217658Sjulian
19354233Sphkstatic void
19465395Speterprint_uptime(void)
19554233Sphk{
19654233Sphk	int f;
19754233Sphk	struct timespec ts;
19854233Sphk
19954233Sphk	getnanouptime(&ts);
20054233Sphk	printf("Uptime: ");
20154233Sphk	f = 0;
20254233Sphk	if (ts.tv_sec >= 86400) {
20365764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
20454233Sphk		ts.tv_sec %= 86400;
20554233Sphk		f = 1;
20654233Sphk	}
20754233Sphk	if (f || ts.tv_sec >= 3600) {
20865764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
20954233Sphk		ts.tv_sec %= 3600;
21054233Sphk		f = 1;
21154233Sphk	}
21254233Sphk	if (f || ts.tv_sec >= 60) {
21365764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
21454233Sphk		ts.tv_sec %= 60;
21554233Sphk		f = 1;
21654233Sphk	}
21765764Sjhb	printf("%lds\n", (long)ts.tv_sec);
21854233Sphk}
21954233Sphk
22094169Sphkstatic void
22194169Sphkdoadump(void)
22294169Sphk{
223110859Salfred
224132412Sjulian	/*
225132412Sjulian	 * Sometimes people have to call this from the kernel debugger.
226132412Sjulian	 * (if 'panic' can not dump)
227132412Sjulian	 * Give them a clue as to why they can't dump.
228132412Sjulian	 */
229132412Sjulian	if (dumper.dumper == NULL) {
230132413Sjulian		printf("Cannot dump. No dump device defined.\n");
231132412Sjulian		return;
232132412Sjulian	}
233132412Sjulian
23494169Sphk	savectx(&dumppcb);
235131927Smarcel	dumptid = curthread->td_tid;
23694169Sphk	dumping++;
23794169Sphk	dumpsys(&dumper);
23894169Sphk}
23994169Sphk
24017658Sjulian/*
241137329Snjl * Shutdown the system cleanly to prepare for reboot, halt, or power off.
24217658Sjulian */
24331275Sbdestatic void
24465395Speterboot(int howto)
24517658Sjulian{
246133763Struckman	static int first_buf_printf = 1;
24717658Sjulian
248137375Smarcel#if defined(SMP)
249137329Snjl	/*
250137329Snjl	 * Bind us to CPU 0 so that all shutdown code runs there.  Some
251137329Snjl	 * systems don't shutdown properly (i.e., ACPI power off) if we
252137329Snjl	 * run on another processor.
253137329Snjl	 */
254137263Speter	mtx_lock_spin(&sched_lock);
255137263Speter	sched_bind(curthread, 0);
256137263Speter	mtx_unlock_spin(&sched_lock);
257138217Snjl	KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
258137263Speter#endif
259137263Speter
26065268Smsmith	/* collect extra flags that shutdown_nice might have set */
26165268Smsmith	howto |= shutdown_howto;
26265268Smsmith
26382119Sjhb	/* We are out of the debugger now. */
264131927Smarcel	kdb_active = 0;
26582119Sjhb
26627997Sjulian	/*
26727997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
26827997Sjulian	 */
26950107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
27027997Sjulian
27127997Sjulian	/*
27227997Sjulian	 * Now sync filesystems
27327997Sjulian	 */
27417658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
27517658Sjulian		register struct buf *bp;
27665707Sjasone		int iter, nbusy, pbusy;
277131481Sjhb#ifndef PREEMPTION
27865707Sjasone		int subiter;
279131481Sjhb#endif
28017658Sjulian
28117658Sjulian		waittime = 0;
28217658Sjulian
28390361Sjulian		sync(&thread0, NULL);
28417658Sjulian
28534266Sjulian		/*
28634266Sjulian		 * With soft updates, some buffers that are
28734266Sjulian		 * written will be remarked as dirty until other
28834266Sjulian		 * buffers are written.
28934266Sjulian		 */
29065707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
29117658Sjulian			nbusy = 0;
29217658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
29348225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
29448225Smckusick				    BUF_REFCNT(bp) > 0) {
29517658Sjulian					nbusy++;
29634266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
29734266Sjulian						== B_DELWRI) {
29834266Sjulian					/* bawrite(bp);*/
29934266Sjulian					nbusy++;
30017658Sjulian				}
30117658Sjulian			}
302133763Struckman			if (nbusy == 0) {
303133763Struckman				if (first_buf_printf)
304136115Sphk					printf("All buffers synced.");
30517658Sjulian				break;
306133763Struckman			}
307133763Struckman			if (first_buf_printf) {
308133763Struckman				printf("Syncing disks, buffers remaining... ");
309133763Struckman				first_buf_printf = 0;
310133763Struckman			}
31117658Sjulian			printf("%d ", nbusy);
31265707Sjasone			if (nbusy < pbusy)
31365707Sjasone				iter = 0;
31465707Sjasone			pbusy = nbusy;
31590361Sjulian			sync(&thread0, NULL);
316131481Sjhb
317131481Sjhb#ifdef PREEMPTION
318131481Sjhb			/*
319131481Sjhb			 * Drop Giant and spin for a while to allow
320131481Sjhb			 * interrupt threads to run.
321131481Sjhb			 */
322131481Sjhb			DROP_GIANT();
32334266Sjulian			DELAY(50000 * iter);
324131481Sjhb			PICKUP_GIANT();
325131481Sjhb#else
326131481Sjhb			/*
327131481Sjhb			 * Drop Giant and context switch several times to
328131481Sjhb			 * allow interrupt threads to run.
329131481Sjhb			 */
330131481Sjhb			DROP_GIANT();
331131481Sjhb			for (subiter = 0; subiter < 50 * iter; subiter++) {
332131481Sjhb				mtx_lock_spin(&sched_lock);
333131481Sjhb				mi_switch(SW_VOL, NULL);
334131481Sjhb				mtx_unlock_spin(&sched_lock);
335131481Sjhb				DELAY(1000);
336131481Sjhb			}
337131481Sjhb			PICKUP_GIANT();
338131481Sjhb#endif
33917658Sjulian		}
340133418Snjl		printf("\n");
34141137Smsmith		/*
34241137Smsmith		 * Count only busy local buffers to prevent forcing
34341137Smsmith		 * a fsck if we're just a client of a wedged NFS server
34441137Smsmith		 */
34541137Smsmith		nbusy = 0;
34641137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
34748225Smckusick			if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
34848225Smckusick			    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
349137186Sphk#if 0
350137186Sphk/* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
351130640Sphk				if (bp->b_dev == NULL) {
35253452Sphk					TAILQ_REMOVE(&mountlist,
35348225Smckusick					    bp->b_vp->v_mount, mnt_list);
35453023Sphk					continue;
35553023Sphk				}
356137186Sphk#endif
35753023Sphk				nbusy++;
35853023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
35953023Sphk				printf(
360137186Sphk			    "%d: bufobj:%p, flags:%0x, blkno:%ld, lblkno:%ld\n",
361137186Sphk				    nbusy, bp->b_bufobj,
36253023Sphk				    bp->b_flags, (long)bp->b_blkno,
36353023Sphk				    (long)bp->b_lblkno);
36453023Sphk#endif
36546568Speter			}
36641137Smsmith		}
36717658Sjulian		if (nbusy) {
36817658Sjulian			/*
36917658Sjulian			 * Failed to sync all blocks. Indicate this and don't
37017658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
37117658Sjulian			 */
372133763Struckman			printf("Giving up on %d buffers\n", nbusy);
37317658Sjulian			DELAY(5000000);	/* 5 seconds */
37417658Sjulian		} else {
375133763Struckman			if (!first_buf_printf)
376133763Struckman				printf("Final sync complete\n");
37717658Sjulian			/*
37817658Sjulian			 * Unmount filesystems
37917658Sjulian			 */
38017658Sjulian			if (panicstr == 0)
38117658Sjulian				vfs_unmountall();
38217658Sjulian		}
38339237Sgibbs		DELAY(100000);		/* wait for console output to finish */
38417658Sjulian	}
38527997Sjulian
38654233Sphk	print_uptime();
38754233Sphk
38827997Sjulian	/*
38927997Sjulian	 * Ok, now do things that assume all filesystem activity has
39027997Sjulian	 * been completed.
39127997Sjulian	 */
39250107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
393137329Snjl
394137329Snjl	/* XXX This doesn't disable interrupts any more.  Reconsider? */
39539237Sgibbs	splhigh();
396137329Snjl
397132412Sjulian	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
39894169Sphk		doadump();
39939237Sgibbs
40039237Sgibbs	/* Now that we're going to really halt the system... */
40150107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
40239237Sgibbs
40350107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
40450107Smsmith	/* NOTREACHED */
40550107Smsmith}
40650107Smsmith
40750107Smsmith/*
40850107Smsmith * If the shutdown was a clean halt, behave accordingly.
40950107Smsmith */
41050107Smsmithstatic void
41150107Smsmithshutdown_halt(void *junk, int howto)
41250107Smsmith{
413110859Salfred
41417658Sjulian	if (howto & RB_HALT) {
41517658Sjulian		printf("\n");
41617658Sjulian		printf("The operating system has halted.\n");
41717658Sjulian		printf("Please press any key to reboot.\n\n");
41819274Sjulian		switch (cngetc()) {
41919274Sjulian		case -1:		/* No console, just die */
42019274Sjulian			cpu_halt();
42119274Sjulian			/* NOTREACHED */
42219274Sjulian		default:
42339237Sgibbs			howto &= ~RB_HALT;
42419274Sjulian			break;
42519274Sjulian		}
42650107Smsmith	}
42750107Smsmith}
42817658Sjulian
42950107Smsmith/*
43050107Smsmith * Check to see if the system paniced, pause and then reboot
43150107Smsmith * according to the specified delay.
43250107Smsmith */
43350107Smsmithstatic void
43450107Smsmithshutdown_panic(void *junk, int howto)
43550107Smsmith{
43650107Smsmith	int loop;
43750107Smsmith
43850107Smsmith	if (howto & RB_DUMP) {
43939237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
44039237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
44139237Sgibbs				printf("Automatic reboot in %d seconds - "
44239237Sgibbs				       "press a key on the console to abort\n",
44339237Sgibbs					PANIC_REBOOT_WAIT_TIME);
44439237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
44539237Sgibbs				     loop > 0; --loop) {
44639237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
44739237Sgibbs					/* Did user type a key? */
44839237Sgibbs					if (cncheckc() != -1)
44939237Sgibbs						break;
45017658Sjulian				}
45139237Sgibbs				if (!loop)
45250107Smsmith					return;
45317658Sjulian			}
45439237Sgibbs		} else { /* zero time specified - reboot NOW */
45550107Smsmith			return;
45617658Sjulian		}
45789522Snik		printf("--> Press a key on the console to reboot,\n");
45889522Snik		printf("--> or switch off the system now.\n");
45939237Sgibbs		cngetc();
46017658Sjulian	}
46150107Smsmith}
46250107Smsmith
46350107Smsmith/*
46450107Smsmith * Everything done, now reset
46550107Smsmith */
46650107Smsmithstatic void
46750107Smsmithshutdown_reset(void *junk, int howto)
46850107Smsmith{
469110859Salfred
47017658Sjulian	printf("Rebooting...\n");
47117658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
47217677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
47317658Sjulian	cpu_reset();
47450107Smsmith	/* NOTREACHED */ /* assuming reset worked */
47517658Sjulian}
47617658Sjulian
47775570Sjhb#ifdef SMP
478101155Sjhbstatic u_int panic_cpu = NOCPU;
47975570Sjhb#endif
48075570Sjhb
48117658Sjulian/*
48217658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
48317658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
48417658Sjulian * the disks as this often leads to recursive panics.
48582749Sdillon *
48682749Sdillon * MPSAFE
48717658Sjulian */
48817658Sjulianvoid
489130164Sphkpanic(const char *fmt, ...)
49017658Sjulian{
491100209Sgallatin	struct thread *td = curthread;
492103647Sjhb	int bootopt, newpanic;
49317658Sjulian	va_list ap;
49438874Sache	static char buf[256];
49517658Sjulian
49665557Sjasone#ifdef SMP
49782115Sjhb	/*
49882115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
499101155Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
500101155Sjhb	 * panic_cpu if we are spinning in case the panic on the first
50182115Sjhb	 * CPU is canceled.
50282115Sjhb	 */
503101155Sjhb	if (panic_cpu != PCPU_GET(cpuid))
504101155Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
505101155Sjhb		    PCPU_GET(cpuid)) == 0)
506101155Sjhb			while (panic_cpu != NOCPU)
507101155Sjhb				; /* nothing */
50865557Sjasone#endif
50965557Sjasone
51017658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
511103647Sjhb	newpanic = 0;
51217658Sjulian	if (panicstr)
51317658Sjulian		bootopt |= RB_NOSYNC;
514103647Sjhb	else {
51517658Sjulian		panicstr = fmt;
516103647Sjhb		newpanic = 1;
517103647Sjhb	}
51817658Sjulian
51917658Sjulian	va_start(ap, fmt);
520116398Siedowse	if (newpanic) {
521116398Siedowse		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
52238874Sache		panicstr = buf;
523130164Sphk		printf("panic: %s\n", buf);
524116398Siedowse	} else {
525116398Siedowse		printf("panic: ");
526116398Siedowse		vprintf(fmt, ap);
527130164Sphk		printf("\n");
528116398Siedowse	}
52917658Sjulian	va_end(ap);
53026100Sfsmp#ifdef SMP
531134089Sjhb	printf("cpuid = %d\n", PCPU_GET(cpuid));
53226100Sfsmp#endif
53317658Sjulian
534131927Smarcel#ifdef KDB
535103647Sjhb	if (newpanic && trace_on_panic)
536131927Smarcel		kdb_backtrace();
53717658Sjulian	if (debugger_on_panic)
538131927Smarcel		kdb_enter("panic");
53982223Sjhb#ifdef RESTARTABLE_PANICS
54082115Sjhb	/* See if the user aborted the panic, in which case we continue. */
54182115Sjhb	if (panicstr == NULL) {
54282115Sjhb#ifdef SMP
543101155Sjhb		atomic_store_rel_int(&panic_cpu, NOCPU);
54417658Sjulian#endif
54582115Sjhb		return;
54682115Sjhb	}
54782115Sjhb#endif
54882223Sjhb#endif
549113633Sjhb	mtx_lock_spin(&sched_lock);
550100209Sgallatin	td->td_flags |= TDF_INPANIC;
551113633Sjhb	mtx_unlock_spin(&sched_lock);
55285202Speter	if (!sync_on_panic)
55385202Speter		bootopt |= RB_NOSYNC;
55417658Sjulian	boot(bootopt);
55517658Sjulian}
55617658Sjulian
55717768Sjulian/*
55843436Smsmith * Support for poweroff delay.
55943436Smsmith */
56054248Smsmith#ifndef POWEROFF_DELAY
56154248Smsmith# define POWEROFF_DELAY 5000
56254248Smsmith#endif
56354248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
56454248Smsmith
56543436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
56643436Smsmith	&poweroff_delay, 0, "");
56743436Smsmith
568110859Salfredstatic void
56950107Smsmithpoweroff_wait(void *junk, int howto)
57043436Smsmith{
571110859Salfred
572110859Salfred	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
57343436Smsmith		return;
57443436Smsmith	DELAY(poweroff_delay * 1000);
57543436Smsmith}
57655539Sluoqi
57755539Sluoqi/*
57855539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
57955539Sluoqi * points in their main loops prior to a system shutdown, so that they
58055539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
58155539Sluoqi * to cause sync to fail).  For each of these system processes, register
58255539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
58355539Sluoqi */
58455539Sluoqistatic int kproc_shutdown_wait = 60;
58555539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
58655539Sluoqi    &kproc_shutdown_wait, 0, "");
58755539Sluoqi
58855539Sluoqivoid
58970063Sjhbkproc_shutdown(void *arg, int howto)
59055539Sluoqi{
59155539Sluoqi	struct proc *p;
592132177Salfred	char procname[MAXCOMLEN + 1];
59355539Sluoqi	int error;
59455539Sluoqi
59555539Sluoqi	if (panicstr)
59655539Sluoqi		return;
59755539Sluoqi
59855539Sluoqi	p = (struct proc *)arg;
599132177Salfred	strlcpy(procname, p->p_comm, sizeof(procname));
600132866Snjl	printf("Waiting (max %d seconds) for system process `%s' to stop...",
601132177Salfred	    kproc_shutdown_wait, procname);
60270063Sjhb	error = kthread_suspend(p, kproc_shutdown_wait * hz);
60355539Sluoqi
60455539Sluoqi	if (error == EWOULDBLOCK)
605132866Snjl		printf("timed out\n");
60655539Sluoqi	else
607132866Snjl		printf("done\n");
60855539Sluoqi}
60993496Sphk
61093496Sphk/* Registration of dumpers */
61193496Sphkint
61293496Sphkset_dumper(struct dumperinfo *di)
61393496Sphk{
614110859Salfred
61593496Sphk	if (di == NULL) {
61693496Sphk		bzero(&dumper, sizeof dumper);
61793496Sphk		return (0);
61893496Sphk	}
61993496Sphk	if (dumper.dumper != NULL)
62093496Sphk		return (EBUSY);
62193496Sphk	dumper = *di;
62293496Sphk	return (0);
62393496Sphk}
62493496Sphk
625105531Stmm#if defined(__powerpc__)
62693496Sphkvoid
62793496Sphkdumpsys(struct dumperinfo *di __unused)
62893496Sphk{
62993496Sphk
63093496Sphk	printf("Kernel dumps not implemented on this architecture\n");
63193496Sphk}
63293496Sphk#endif
633