kern_shutdown.c revision 50107
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 * 3. All advertising materials mentioning features or use of this software
1917658Sjulian *    must display the following acknowledgement:
2017658Sjulian *	This product includes software developed by the University of
2117658Sjulian *	California, Berkeley and its contributors.
2217658Sjulian * 4. Neither the name of the University nor the names of its contributors
2317658Sjulian *    may be used to endorse or promote products derived from this software
2417658Sjulian *    without specific prior written permission.
2517658Sjulian *
2617658Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2717658Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2817658Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2917658Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3017658Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3117658Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3217658Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3317658Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3417658Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3517658Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3617658Sjulian * SUCH DAMAGE.
3717658Sjulian *
3817658Sjulian *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
3950107Smsmith * $Id: kern_shutdown.c,v 1.60 1999/08/13 10:29:21 phk Exp $
4017658Sjulian */
4117658Sjulian
4217658Sjulian#include "opt_ddb.h"
4333445Seivind#include "opt_hw_wdog.h"
4428976Sbde#include "opt_panic.h"
4528976Sbde#include "opt_show_busybufs.h"
4617658Sjulian
4717658Sjulian#include <sys/param.h>
4817658Sjulian#include <sys/systm.h>
4950107Smsmith#include <sys/eventhandler.h>
5031275Sbde#include <sys/buf.h>
5117658Sjulian#include <sys/reboot.h>
5217658Sjulian#include <sys/proc.h>
5341137Smsmith#include <sys/vnode.h>
5417658Sjulian#include <sys/malloc.h>
5517658Sjulian#include <sys/kernel.h>
5621776Sbde#include <sys/mount.h>
5739237Sgibbs#include <sys/queue.h>
5817658Sjulian#include <sys/sysctl.h>
5917658Sjulian#include <sys/conf.h>
6017658Sjulian#include <sys/sysproto.h>
6149558Sphk#include <sys/cons.h>
6217658Sjulian
6317658Sjulian#include <machine/pcb.h>
6417658Sjulian#include <machine/clock.h>
6517658Sjulian#include <machine/md_var.h>
6626812Speter#ifdef SMP
6726812Speter#include <machine/smp.h>		/* smp_active, cpuid */
6826812Speter#endif
6917658Sjulian
7017658Sjulian#include <sys/signalvar.h>
7117658Sjulian
7217658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
7317658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
7417658Sjulian#endif
7517658Sjulian
7617658Sjulian/*
7717658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
7817658Sjulian * ANSI and traditional C compilers.
7917658Sjulian */
8017658Sjulian#include <machine/stdarg.h>
8117658Sjulian
8228769Sbde#ifdef DDB
8317658Sjulian#ifdef DDB_UNATTENDED
8442135Smsmithint debugger_on_panic = 0;
8517658Sjulian#else
8642135Smsmithint debugger_on_panic = 1;
8717658Sjulian#endif
8817658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
8946381Sbillf	&debugger_on_panic, 0, "Run debugger on kernel panic");
9017658Sjulian#endif
9117658Sjulian
9243436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
9343436Smsmith
9428000Sjulian#ifdef	HW_WDOG
9517658Sjulian/*
9627997Sjulian * If there is a hardware watchdog, point this at the function needed to
9727997Sjulian * hold it off.
9827997Sjulian * It's needed when the kernel needs to do some lengthy operations.
9927997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have
10027997Sjulian * your precious core-dump only half written because the wdog kicked in.
10127997Sjulian */
10227997Sjulianwatchdog_tickle_fn wdog_tickler = NULL;
10328000Sjulian#endif	/* HW_WDOG */
10427997Sjulian
10527997Sjulian/*
10617658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
10717658Sjulian * to indicate that the kernel has already called panic.
10817658Sjulian */
10917658Sjulianconst char *panicstr;
11017658Sjulian
11131275Sbdestatic void boot __P((int)) __dead2;
11231275Sbdestatic void dumpsys __P((void));
11348868Sphkstatic int setdumpdev __P((dev_t dev));
11450107Smsmithstatic void poweroff_wait __P((void *, int));
11550107Smsmithstatic void shutdown_halt __P((void *junk, int howto));
11650107Smsmithstatic void shutdown_panic __P((void *junk, int howto));
11750107Smsmithstatic void shutdown_reset __P((void *junk, int howto));
11817658Sjulian
11950107Smsmith/* register various local shutdown events */
12050107Smsmithstatic void
12150107Smsmithshutdown_conf(void *unused)
12250107Smsmith{
12350107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST);
12450107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100);
12550107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100);
12650107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200);
12750107Smsmith}
12848868Sphk
12950107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
13050107Smsmith
13117658Sjulian/* ARGSUSED */
13217658Sjulian
13317658Sjulian/*
13417658Sjulian * The system call that results in a reboot
13517658Sjulian */
13617658Sjulianint
13730994Sphkreboot(p, uap)
13817658Sjulian	struct proc *p;
13917658Sjulian	struct reboot_args *uap;
14017658Sjulian{
14117658Sjulian	int error;
14217658Sjulian
14346112Sphk	if ((error = suser(p)))
14417658Sjulian		return (error);
14517658Sjulian
14617658Sjulian	boot(uap->opt);
14717658Sjulian	return (0);
14817658Sjulian}
14917658Sjulian
15017658Sjulian/*
15117658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
15217658Sjulian */
15317658Sjulianvoid
15428769Sbdeshutdown_nice()
15517658Sjulian{
15617658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
15717658Sjulian	if (initproc != NULL) {
15817658Sjulian		psignal(initproc, SIGINT);
15917658Sjulian	} else {
16017658Sjulian		/* No init(8) running, so simply reboot */
16117658Sjulian		boot(RB_NOSYNC);
16217658Sjulian	}
16317658Sjulian	return;
16417658Sjulian}
16517658Sjulianstatic int	waittime = -1;
16617658Sjulianstatic struct pcb dumppcb;
16717658Sjulian
16817658Sjulian/*
16917658Sjulian *  Go through the rigmarole of shutting down..
17017658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
17117658Sjulian * anything machine dependant in it.
17217658Sjulian */
17331275Sbdestatic void
17417658Sjulianboot(howto)
17517658Sjulian	int howto;
17617658Sjulian{
17717658Sjulian
17825164Speter#ifdef SMP
17925164Speter	if (smp_active) {
18026812Speter		printf("boot() called on cpu#%d\n", cpuid);
18125164Speter	}
18225164Speter#endif
18327997Sjulian	/*
18427997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
18527997Sjulian	 */
18650107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
18727997Sjulian
18827997Sjulian	/*
18927997Sjulian	 * Now sync filesystems
19027997Sjulian	 */
19117658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
19217658Sjulian		register struct buf *bp;
19317658Sjulian		int iter, nbusy;
19417658Sjulian
19517658Sjulian		waittime = 0;
19617658Sjulian		printf("\nsyncing disks... ");
19717658Sjulian
19830994Sphk		sync(&proc0, NULL);
19917658Sjulian
20034266Sjulian		/*
20134266Sjulian		 * With soft updates, some buffers that are
20234266Sjulian		 * written will be remarked as dirty until other
20334266Sjulian		 * buffers are written.
20434266Sjulian		 */
20517658Sjulian		for (iter = 0; iter < 20; iter++) {
20617658Sjulian			nbusy = 0;
20717658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
20848225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
20948225Smckusick				    BUF_REFCNT(bp) > 0) {
21017658Sjulian					nbusy++;
21134266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
21234266Sjulian						== B_DELWRI) {
21334266Sjulian					/* bawrite(bp);*/
21434266Sjulian					nbusy++;
21517658Sjulian				}
21617658Sjulian			}
21717658Sjulian			if (nbusy == 0)
21817658Sjulian				break;
21917658Sjulian			printf("%d ", nbusy);
22034266Sjulian			sync(&proc0, NULL);
22134266Sjulian			DELAY(50000 * iter);
22217658Sjulian		}
22341137Smsmith		/*
22441137Smsmith		 * Count only busy local buffers to prevent forcing
22541137Smsmith		 * a fsck if we're just a client of a wedged NFS server
22641137Smsmith		 */
22741137Smsmith		nbusy = 0;
22841137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
22948225Smckusick			if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
23048225Smckusick			    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
23148225Smckusick				if (bp->b_dev == NODEV)
23248225Smckusick					CIRCLEQ_REMOVE(&mountlist,
23348225Smckusick					    bp->b_vp->v_mount, mnt_list);
23441137Smsmith				else
23541137Smsmith					nbusy++;
23646568Speter			}
23741137Smsmith
23841137Smsmith
23941137Smsmith		}
24017658Sjulian		if (nbusy) {
24117658Sjulian			/*
24217658Sjulian			 * Failed to sync all blocks. Indicate this and don't
24317658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
24417658Sjulian			 */
24517658Sjulian			printf("giving up\n");
24617658Sjulian#ifdef SHOW_BUSYBUFS
24717658Sjulian			nbusy = 0;
24817658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
24948225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
25048225Smckusick				    BUF_REFCNT(bp) > 0) {
25117658Sjulian					nbusy++;
25237555Sbde					printf(
25337555Sbde			"%d: dev:%08lx, flags:%08lx, blkno:%ld, lblkno:%ld\n",
25437555Sbde					    nbusy, (u_long)bp->b_dev,
25537555Sbde					    bp->b_flags, (long)bp->b_blkno,
25637555Sbde					    (long)bp->b_lblkno);
25717658Sjulian				}
25817658Sjulian			}
25917658Sjulian			DELAY(5000000);	/* 5 seconds */
26017658Sjulian#endif
26117658Sjulian		} else {
26217658Sjulian			printf("done\n");
26317658Sjulian			/*
26417658Sjulian			 * Unmount filesystems
26517658Sjulian			 */
26617658Sjulian			if (panicstr == 0)
26717658Sjulian				vfs_unmountall();
26817658Sjulian		}
26939237Sgibbs		DELAY(100000);		/* wait for console output to finish */
27017658Sjulian	}
27127997Sjulian
27227997Sjulian	/*
27327997Sjulian	 * Ok, now do things that assume all filesystem activity has
27427997Sjulian	 * been completed.
27527997Sjulian	 */
27650107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
27739237Sgibbs	splhigh();
27839522Sdt	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) {
27939237Sgibbs		savectx(&dumppcb);
28039237Sgibbs#ifdef __i386__
28139237Sgibbs		dumppcb.pcb_cr3 = rcr3();
28239237Sgibbs#endif
28339237Sgibbs		dumpsys();
28417768Sjulian	}
28539237Sgibbs
28639237Sgibbs	/* Now that we're going to really halt the system... */
28750107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
28839237Sgibbs
28950107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
29050107Smsmith	/* NOTREACHED */
29150107Smsmith}
29250107Smsmith
29350107Smsmith/*
29450107Smsmith * If the shutdown was a clean halt, behave accordingly.
29550107Smsmith */
29650107Smsmithstatic void
29750107Smsmithshutdown_halt(void *junk, int howto)
29850107Smsmith{
29917658Sjulian	if (howto & RB_HALT) {
30017658Sjulian		printf("\n");
30117658Sjulian		printf("The operating system has halted.\n");
30217658Sjulian		printf("Please press any key to reboot.\n\n");
30319274Sjulian		switch (cngetc()) {
30419274Sjulian		case -1:		/* No console, just die */
30519274Sjulian			cpu_halt();
30619274Sjulian			/* NOTREACHED */
30719274Sjulian		default:
30839237Sgibbs			howto &= ~RB_HALT;
30919274Sjulian			break;
31019274Sjulian		}
31150107Smsmith	}
31250107Smsmith}
31317658Sjulian
31450107Smsmith/*
31550107Smsmith * Check to see if the system paniced, pause and then reboot
31650107Smsmith * according to the specified delay.
31750107Smsmith */
31850107Smsmithstatic void
31950107Smsmithshutdown_panic(void *junk, int howto)
32050107Smsmith{
32150107Smsmith	int loop;
32250107Smsmith
32350107Smsmith	if (howto & RB_DUMP) {
32439237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
32539237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
32639237Sgibbs				printf("Automatic reboot in %d seconds - "
32739237Sgibbs				       "press a key on the console to abort\n",
32839237Sgibbs					PANIC_REBOOT_WAIT_TIME);
32939237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
33039237Sgibbs				     loop > 0; --loop) {
33139237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
33239237Sgibbs					/* Did user type a key? */
33339237Sgibbs					if (cncheckc() != -1)
33439237Sgibbs						break;
33517658Sjulian				}
33639237Sgibbs				if (!loop)
33750107Smsmith					return;
33817658Sjulian			}
33939237Sgibbs		} else { /* zero time specified - reboot NOW */
34050107Smsmith			return;
34117658Sjulian		}
34239237Sgibbs		printf("--> Press a key on the console to reboot <--\n");
34339237Sgibbs		cngetc();
34417658Sjulian	}
34550107Smsmith}
34650107Smsmith
34750107Smsmith/*
34850107Smsmith * Everything done, now reset
34950107Smsmith */
35050107Smsmithstatic void
35150107Smsmithshutdown_reset(void *junk, int howto)
35250107Smsmith{
35317658Sjulian	printf("Rebooting...\n");
35417658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
35517677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
35617658Sjulian	cpu_reset();
35750107Smsmith	/* NOTREACHED */ /* assuming reset worked */
35817658Sjulian}
35917658Sjulian
36017658Sjulian/*
36117658Sjulian * Magic number for savecore
36217658Sjulian *
36317658Sjulian * exported (symorder) and used at least by savecore(8)
36417658Sjulian *
36517658Sjulian */
36617658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
36717658Sjulian
36817658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
36917658Sjulian
37017658Sjulianstatic int	dodump = 1;
37117658Sjulian
37248868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
37348868Sphk    "Try to perform coredump on kernel panic");
37448868Sphk
37548868Sphkstatic int
37648868Sphksetdumpdev(dev)
37748868Sphk	dev_t dev;
37848868Sphk{
37948868Sphk	int maj, psize;
38048868Sphk	long newdumplo;
38148868Sphk
38248868Sphk	if (dev == NODEV) {
38348868Sphk		dumpdev = dev;
38448868Sphk		return (0);
38548868Sphk	}
38648868Sphk	maj = major(dev);
38749679Sphk	if (devsw(dev) == NULL)
38848868Sphk		return (ENXIO);		/* XXX is this right? */
38949679Sphk	if (devsw(dev)->d_psize == NULL)
39048868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
39149679Sphk	psize = devsw(dev)->d_psize(dev);
39248868Sphk	if (psize == -1)
39348868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
39448868Sphk	/*
39548868Sphk	 * XXX should clean up checking in dumpsys() to be more like this,
39648868Sphk	 * and nuke dodump sysctl (too many knobs).
39748868Sphk	 */
39848868Sphk	newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE;
39948868Sphk	if (newdumplo < 0)
40048868Sphk		return (ENOSPC);
40148868Sphk	dumpdev = dev;
40248868Sphk	dumplo = newdumplo;
40348868Sphk	return (0);
40448868Sphk}
40548868Sphk
40648868Sphk
40731403Sjulian/* ARGSUSED */
40831403Sjulianstatic void dump_conf __P((void *dummy));
40931403Sjulianstatic void
41031403Sjuliandump_conf(dummy)
41131403Sjulian	void *dummy;
41231403Sjulian{
41348868Sphk	if (setdumpdev(dumpdev) != 0)
41448868Sphk		dumpdev = NODEV;
41531403Sjulian}
41648868Sphk
41731403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
41831403Sjulian
41948868Sphkstatic int
42048868Sphksysctl_kern_dumpdev SYSCTL_HANDLER_ARGS
42148868Sphk{
42248868Sphk	int error;
42348868Sphk	udev_t ndumpdev;
42448868Sphk
42548948Sgreen	ndumpdev = dev2budev(dumpdev);
42648868Sphk	error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
42748868Sphk	if (error == 0 && req->newptr != NULL)
42848868Sphk		error = setdumpdev(udev2dev(ndumpdev, 1));
42948868Sphk	return (error);
43048868Sphk}
43148868Sphk
43248868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
43348868Sphk	0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", "");
43448868Sphk
43517658Sjulian/*
43617658Sjulian * Doadump comes here after turning off memory management and
43717658Sjulian * getting on the dump stack, either when called above, or by
43817658Sjulian * the auto-restart code.
43917658Sjulian */
44017658Sjulianstatic void
44117658Sjuliandumpsys(void)
44217658Sjulian{
44349627Salfred	int	error;
44417658Sjulian
44517658Sjulian	if (!dodump)
44617658Sjulian		return;
44717658Sjulian	if (dumpdev == NODEV)
44817658Sjulian		return;
44949679Sphk	if (!(devsw(dumpdev)))
45017658Sjulian		return;
45149679Sphk	if (!(devsw(dumpdev)->d_dump))
45217658Sjulian		return;
45317658Sjulian	dumpsize = Maxmem;
45447084Speter	printf("\ndumping to dev (%d,%d), offset %ld\n",
45547084Speter		major(dumpdev), minor(dumpdev), dumplo);
45617658Sjulian	printf("dump ");
45749679Sphk	error = (*devsw(dumpdev)->d_dump)(dumpdev);
45849627Salfred	if (error == 0) {
45949627Salfred		printf("succeeded\n");
46049627Salfred		return;
46149627Salfred	}
46249627Salfred	printf("failed, reason: ");
46349627Salfred	switch (error) {
46449627Salfred	case ENODEV:
46549627Salfred		printf("device doesn't support a dump routine\n");
46649627Salfred		break;
46717658Sjulian
46817658Sjulian	case ENXIO:
46917658Sjulian		printf("device bad\n");
47017658Sjulian		break;
47117658Sjulian
47217658Sjulian	case EFAULT:
47317658Sjulian		printf("device not ready\n");
47417658Sjulian		break;
47517658Sjulian
47617658Sjulian	case EINVAL:
47717658Sjulian		printf("area improper\n");
47817658Sjulian		break;
47917658Sjulian
48017658Sjulian	case EIO:
48117658Sjulian		printf("i/o error\n");
48217658Sjulian		break;
48317658Sjulian
48417658Sjulian	case EINTR:
48517658Sjulian		printf("aborted from console\n");
48617658Sjulian		break;
48717658Sjulian
48817658Sjulian	default:
48949627Salfred		printf("unknown, error = %d\n", error);
49017658Sjulian		break;
49117658Sjulian	}
49217658Sjulian}
49317658Sjulian
49417658Sjulian/*
49517658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
49617658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
49717658Sjulian * the disks as this often leads to recursive panics.
49817658Sjulian */
49917658Sjulianvoid
50017658Sjulianpanic(const char *fmt, ...)
50117658Sjulian{
50217658Sjulian	int bootopt;
50317658Sjulian	va_list ap;
50438874Sache	static char buf[256];
50517658Sjulian
50617658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
50717658Sjulian	if (panicstr)
50817658Sjulian		bootopt |= RB_NOSYNC;
50917658Sjulian	else
51017658Sjulian		panicstr = fmt;
51117658Sjulian
51217658Sjulian	va_start(ap, fmt);
51341514Sarchie	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
51438874Sache	if (panicstr == fmt)
51538874Sache		panicstr = buf;
51617658Sjulian	va_end(ap);
51738874Sache	printf("panic: %s\n", buf);
51826100Sfsmp#ifdef SMP
51929128Speter	/* three seperate prints in case of an unmapped page and trap */
52029128Speter	printf("mp_lock = %08x; ", mp_lock);
52129128Speter	printf("cpuid = %d; ", cpuid);
52229128Speter	printf("lapic.id = %08x\n", lapic.id);
52326100Sfsmp#endif
52417658Sjulian
52517658Sjulian#if defined(DDB)
52617658Sjulian	if (debugger_on_panic)
52717658Sjulian		Debugger ("panic");
52817658Sjulian#endif
52917658Sjulian	boot(bootopt);
53017658Sjulian}
53117658Sjulian
53217768Sjulian/*
53343436Smsmith * Support for poweroff delay.
53443436Smsmith */
53543436Smsmithstatic int poweroff_delay = 0;
53643436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
53743436Smsmith	&poweroff_delay, 0, "");
53843436Smsmith
53950107Smsmithstatic void
54050107Smsmithpoweroff_wait(void *junk, int howto)
54143436Smsmith{
54243436Smsmith	if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
54343436Smsmith		return;
54443436Smsmith	DELAY(poweroff_delay * 1000);
54543436Smsmith}
546