kern_shutdown.c revision 53023
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
3950477Speter * $FreeBSD: head/sys/kern/kern_shutdown.c 53023 1999-11-08 19:36:45Z phk $
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/kernel.h>
5521776Sbde#include <sys/mount.h>
5639237Sgibbs#include <sys/queue.h>
5717658Sjulian#include <sys/sysctl.h>
5817658Sjulian#include <sys/conf.h>
5917658Sjulian#include <sys/sysproto.h>
6049558Sphk#include <sys/cons.h>
6117658Sjulian
6217658Sjulian#include <machine/pcb.h>
6317658Sjulian#include <machine/clock.h>
6417658Sjulian#include <machine/md_var.h>
6526812Speter#ifdef SMP
6626812Speter#include <machine/smp.h>		/* smp_active, cpuid */
6726812Speter#endif
6817658Sjulian
6917658Sjulian#include <sys/signalvar.h>
7017658Sjulian
7117658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
7217658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
7317658Sjulian#endif
7417658Sjulian
7517658Sjulian/*
7617658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
7717658Sjulian * ANSI and traditional C compilers.
7817658Sjulian */
7917658Sjulian#include <machine/stdarg.h>
8017658Sjulian
8128769Sbde#ifdef DDB
8217658Sjulian#ifdef DDB_UNATTENDED
8342135Smsmithint debugger_on_panic = 0;
8417658Sjulian#else
8542135Smsmithint debugger_on_panic = 1;
8617658Sjulian#endif
8717658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
8846381Sbillf	&debugger_on_panic, 0, "Run debugger on kernel panic");
8917658Sjulian#endif
9017658Sjulian
9143436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
9243436Smsmith
9328000Sjulian#ifdef	HW_WDOG
9417658Sjulian/*
9527997Sjulian * If there is a hardware watchdog, point this at the function needed to
9627997Sjulian * hold it off.
9727997Sjulian * It's needed when the kernel needs to do some lengthy operations.
9827997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have
9927997Sjulian * your precious core-dump only half written because the wdog kicked in.
10027997Sjulian */
10127997Sjulianwatchdog_tickle_fn wdog_tickler = NULL;
10228000Sjulian#endif	/* HW_WDOG */
10327997Sjulian
10427997Sjulian/*
10517658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
10617658Sjulian * to indicate that the kernel has already called panic.
10717658Sjulian */
10817658Sjulianconst char *panicstr;
10917658Sjulian
11031275Sbdestatic void boot __P((int)) __dead2;
11131275Sbdestatic void dumpsys __P((void));
11248868Sphkstatic int setdumpdev __P((dev_t dev));
11350107Smsmithstatic void poweroff_wait __P((void *, int));
11450107Smsmithstatic void shutdown_halt __P((void *junk, int howto));
11550107Smsmithstatic void shutdown_panic __P((void *junk, int howto));
11650107Smsmithstatic void shutdown_reset __P((void *junk, int howto));
11717658Sjulian
11850107Smsmith/* register various local shutdown events */
11950107Smsmithstatic void
12050107Smsmithshutdown_conf(void *unused)
12150107Smsmith{
12250107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST);
12350107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100);
12450107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100);
12550107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200);
12650107Smsmith}
12748868Sphk
12850107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
12950107Smsmith
13017658Sjulian/* ARGSUSED */
13117658Sjulian
13217658Sjulian/*
13317658Sjulian * The system call that results in a reboot
13417658Sjulian */
13517658Sjulianint
13630994Sphkreboot(p, uap)
13717658Sjulian	struct proc *p;
13817658Sjulian	struct reboot_args *uap;
13917658Sjulian{
14017658Sjulian	int error;
14117658Sjulian
14246112Sphk	if ((error = suser(p)))
14317658Sjulian		return (error);
14417658Sjulian
14517658Sjulian	boot(uap->opt);
14617658Sjulian	return (0);
14717658Sjulian}
14817658Sjulian
14917658Sjulian/*
15017658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
15117658Sjulian */
15217658Sjulianvoid
15328769Sbdeshutdown_nice()
15417658Sjulian{
15517658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
15617658Sjulian	if (initproc != NULL) {
15717658Sjulian		psignal(initproc, SIGINT);
15817658Sjulian	} else {
15917658Sjulian		/* No init(8) running, so simply reboot */
16017658Sjulian		boot(RB_NOSYNC);
16117658Sjulian	}
16217658Sjulian	return;
16317658Sjulian}
16417658Sjulianstatic int	waittime = -1;
16517658Sjulianstatic struct pcb dumppcb;
16617658Sjulian
16717658Sjulian/*
16817658Sjulian *  Go through the rigmarole of shutting down..
16917658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
17017658Sjulian * anything machine dependant in it.
17117658Sjulian */
17231275Sbdestatic void
17317658Sjulianboot(howto)
17417658Sjulian	int howto;
17517658Sjulian{
17617658Sjulian
17725164Speter#ifdef SMP
17825164Speter	if (smp_active) {
17926812Speter		printf("boot() called on cpu#%d\n", cpuid);
18025164Speter	}
18125164Speter#endif
18227997Sjulian	/*
18327997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
18427997Sjulian	 */
18550107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
18627997Sjulian
18727997Sjulian	/*
18827997Sjulian	 * Now sync filesystems
18927997Sjulian	 */
19017658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
19117658Sjulian		register struct buf *bp;
19217658Sjulian		int iter, nbusy;
19317658Sjulian
19417658Sjulian		waittime = 0;
19517658Sjulian		printf("\nsyncing disks... ");
19617658Sjulian
19730994Sphk		sync(&proc0, NULL);
19817658Sjulian
19934266Sjulian		/*
20034266Sjulian		 * With soft updates, some buffers that are
20134266Sjulian		 * written will be remarked as dirty until other
20234266Sjulian		 * buffers are written.
20334266Sjulian		 */
20417658Sjulian		for (iter = 0; iter < 20; iter++) {
20517658Sjulian			nbusy = 0;
20617658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
20748225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
20848225Smckusick				    BUF_REFCNT(bp) > 0) {
20917658Sjulian					nbusy++;
21034266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
21134266Sjulian						== B_DELWRI) {
21234266Sjulian					/* bawrite(bp);*/
21334266Sjulian					nbusy++;
21417658Sjulian				}
21517658Sjulian			}
21617658Sjulian			if (nbusy == 0)
21717658Sjulian				break;
21817658Sjulian			printf("%d ", nbusy);
21934266Sjulian			sync(&proc0, NULL);
22034266Sjulian			DELAY(50000 * iter);
22117658Sjulian		}
22253023Sphk		printf("\n");
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)) {
23153023Sphk				if (bp->b_dev == NODEV) {
23248225Smckusick					CIRCLEQ_REMOVE(&mountlist,
23348225Smckusick					    bp->b_vp->v_mount, mnt_list);
23453023Sphk					continue;
23553023Sphk				}
23653023Sphk				nbusy++;
23753023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
23853023Sphk				printf(
23953023Sphk			    "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n",
24053023Sphk				    nbusy, devtoname(bp->b_dev),
24153023Sphk				    bp->b_flags, (long)bp->b_blkno,
24253023Sphk				    (long)bp->b_lblkno);
24353023Sphk#endif
24446568Speter			}
24541137Smsmith		}
24617658Sjulian		if (nbusy) {
24717658Sjulian			/*
24817658Sjulian			 * Failed to sync all blocks. Indicate this and don't
24917658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
25017658Sjulian			 */
25153023Sphk			printf("giving up on %d buffers\n", nbusy);
25217658Sjulian			DELAY(5000000);	/* 5 seconds */
25317658Sjulian		} else {
25417658Sjulian			printf("done\n");
25517658Sjulian			/*
25617658Sjulian			 * Unmount filesystems
25717658Sjulian			 */
25817658Sjulian			if (panicstr == 0)
25917658Sjulian				vfs_unmountall();
26017658Sjulian		}
26139237Sgibbs		DELAY(100000);		/* wait for console output to finish */
26217658Sjulian	}
26327997Sjulian
26427997Sjulian	/*
26527997Sjulian	 * Ok, now do things that assume all filesystem activity has
26627997Sjulian	 * been completed.
26727997Sjulian	 */
26850107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
26939237Sgibbs	splhigh();
27039522Sdt	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) {
27139237Sgibbs		savectx(&dumppcb);
27239237Sgibbs#ifdef __i386__
27339237Sgibbs		dumppcb.pcb_cr3 = rcr3();
27439237Sgibbs#endif
27539237Sgibbs		dumpsys();
27617768Sjulian	}
27739237Sgibbs
27839237Sgibbs	/* Now that we're going to really halt the system... */
27950107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
28039237Sgibbs
28150107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
28250107Smsmith	/* NOTREACHED */
28350107Smsmith}
28450107Smsmith
28550107Smsmith/*
28650107Smsmith * If the shutdown was a clean halt, behave accordingly.
28750107Smsmith */
28850107Smsmithstatic void
28950107Smsmithshutdown_halt(void *junk, int howto)
29050107Smsmith{
29117658Sjulian	if (howto & RB_HALT) {
29217658Sjulian		printf("\n");
29317658Sjulian		printf("The operating system has halted.\n");
29417658Sjulian		printf("Please press any key to reboot.\n\n");
29519274Sjulian		switch (cngetc()) {
29619274Sjulian		case -1:		/* No console, just die */
29719274Sjulian			cpu_halt();
29819274Sjulian			/* NOTREACHED */
29919274Sjulian		default:
30039237Sgibbs			howto &= ~RB_HALT;
30119274Sjulian			break;
30219274Sjulian		}
30350107Smsmith	}
30450107Smsmith}
30517658Sjulian
30650107Smsmith/*
30750107Smsmith * Check to see if the system paniced, pause and then reboot
30850107Smsmith * according to the specified delay.
30950107Smsmith */
31050107Smsmithstatic void
31150107Smsmithshutdown_panic(void *junk, int howto)
31250107Smsmith{
31350107Smsmith	int loop;
31450107Smsmith
31550107Smsmith	if (howto & RB_DUMP) {
31639237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
31739237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
31839237Sgibbs				printf("Automatic reboot in %d seconds - "
31939237Sgibbs				       "press a key on the console to abort\n",
32039237Sgibbs					PANIC_REBOOT_WAIT_TIME);
32139237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
32239237Sgibbs				     loop > 0; --loop) {
32339237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
32439237Sgibbs					/* Did user type a key? */
32539237Sgibbs					if (cncheckc() != -1)
32639237Sgibbs						break;
32717658Sjulian				}
32839237Sgibbs				if (!loop)
32950107Smsmith					return;
33017658Sjulian			}
33139237Sgibbs		} else { /* zero time specified - reboot NOW */
33250107Smsmith			return;
33317658Sjulian		}
33439237Sgibbs		printf("--> Press a key on the console to reboot <--\n");
33539237Sgibbs		cngetc();
33617658Sjulian	}
33750107Smsmith}
33850107Smsmith
33950107Smsmith/*
34050107Smsmith * Everything done, now reset
34150107Smsmith */
34250107Smsmithstatic void
34350107Smsmithshutdown_reset(void *junk, int howto)
34450107Smsmith{
34517658Sjulian	printf("Rebooting...\n");
34617658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
34717677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
34817658Sjulian	cpu_reset();
34950107Smsmith	/* NOTREACHED */ /* assuming reset worked */
35017658Sjulian}
35117658Sjulian
35217658Sjulian/*
35317658Sjulian * Magic number for savecore
35417658Sjulian *
35517658Sjulian * exported (symorder) and used at least by savecore(8)
35617658Sjulian *
35717658Sjulian */
35817658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
35917658Sjulian
36017658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
36117658Sjulian
36217658Sjulianstatic int	dodump = 1;
36317658Sjulian
36448868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
36548868Sphk    "Try to perform coredump on kernel panic");
36648868Sphk
36748868Sphkstatic int
36848868Sphksetdumpdev(dev)
36948868Sphk	dev_t dev;
37048868Sphk{
37150571Sphk	int psize;
37248868Sphk	long newdumplo;
37348868Sphk
37448868Sphk	if (dev == NODEV) {
37548868Sphk		dumpdev = dev;
37648868Sphk		return (0);
37748868Sphk	}
37849679Sphk	if (devsw(dev) == NULL)
37948868Sphk		return (ENXIO);		/* XXX is this right? */
38049679Sphk	if (devsw(dev)->d_psize == NULL)
38148868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
38249679Sphk	psize = devsw(dev)->d_psize(dev);
38348868Sphk	if (psize == -1)
38448868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
38548868Sphk	/*
38650571Sphk	 * XXX should clean up checking in dumpsys() to be more like this.
38748868Sphk	 */
38848868Sphk	newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE;
38948868Sphk	if (newdumplo < 0)
39048868Sphk		return (ENOSPC);
39148868Sphk	dumpdev = dev;
39248868Sphk	dumplo = newdumplo;
39348868Sphk	return (0);
39448868Sphk}
39548868Sphk
39648868Sphk
39731403Sjulian/* ARGSUSED */
39831403Sjulianstatic void dump_conf __P((void *dummy));
39931403Sjulianstatic void
40031403Sjuliandump_conf(dummy)
40131403Sjulian	void *dummy;
40231403Sjulian{
40348868Sphk	if (setdumpdev(dumpdev) != 0)
40448868Sphk		dumpdev = NODEV;
40531403Sjulian}
40648868Sphk
40731403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
40831403Sjulian
40948868Sphkstatic int
41048868Sphksysctl_kern_dumpdev SYSCTL_HANDLER_ARGS
41148868Sphk{
41248868Sphk	int error;
41348868Sphk	udev_t ndumpdev;
41448868Sphk
41548948Sgreen	ndumpdev = dev2budev(dumpdev);
41648868Sphk	error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
41748868Sphk	if (error == 0 && req->newptr != NULL)
41848868Sphk		error = setdumpdev(udev2dev(ndumpdev, 1));
41948868Sphk	return (error);
42048868Sphk}
42148868Sphk
42248868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
42348868Sphk	0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", "");
42448868Sphk
42517658Sjulian/*
42617658Sjulian * Doadump comes here after turning off memory management and
42717658Sjulian * getting on the dump stack, either when called above, or by
42817658Sjulian * the auto-restart code.
42917658Sjulian */
43017658Sjulianstatic void
43117658Sjuliandumpsys(void)
43217658Sjulian{
43349627Salfred	int	error;
43450571Sphk	static int dumping;
43517658Sjulian
43650571Sphk	if (dumping++) {
43750571Sphk		printf("Dump already in progress, bailing...\n");
43850571Sphk		return;
43950571Sphk	}
44017658Sjulian	if (!dodump)
44117658Sjulian		return;
44217658Sjulian	if (dumpdev == NODEV)
44317658Sjulian		return;
44449679Sphk	if (!(devsw(dumpdev)))
44517658Sjulian		return;
44649679Sphk	if (!(devsw(dumpdev)->d_dump))
44717658Sjulian		return;
44817658Sjulian	dumpsize = Maxmem;
44950571Sphk	printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo);
45017658Sjulian	printf("dump ");
45149679Sphk	error = (*devsw(dumpdev)->d_dump)(dumpdev);
45249627Salfred	if (error == 0) {
45349627Salfred		printf("succeeded\n");
45449627Salfred		return;
45549627Salfred	}
45649627Salfred	printf("failed, reason: ");
45749627Salfred	switch (error) {
45849627Salfred	case ENODEV:
45949627Salfred		printf("device doesn't support a dump routine\n");
46049627Salfred		break;
46117658Sjulian
46217658Sjulian	case ENXIO:
46317658Sjulian		printf("device bad\n");
46417658Sjulian		break;
46517658Sjulian
46617658Sjulian	case EFAULT:
46717658Sjulian		printf("device not ready\n");
46817658Sjulian		break;
46917658Sjulian
47017658Sjulian	case EINVAL:
47117658Sjulian		printf("area improper\n");
47217658Sjulian		break;
47317658Sjulian
47417658Sjulian	case EIO:
47517658Sjulian		printf("i/o error\n");
47617658Sjulian		break;
47717658Sjulian
47817658Sjulian	case EINTR:
47917658Sjulian		printf("aborted from console\n");
48017658Sjulian		break;
48117658Sjulian
48217658Sjulian	default:
48349627Salfred		printf("unknown, error = %d\n", error);
48417658Sjulian		break;
48517658Sjulian	}
48617658Sjulian}
48717658Sjulian
48817658Sjulian/*
48917658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
49017658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
49117658Sjulian * the disks as this often leads to recursive panics.
49217658Sjulian */
49317658Sjulianvoid
49417658Sjulianpanic(const char *fmt, ...)
49517658Sjulian{
49617658Sjulian	int bootopt;
49717658Sjulian	va_list ap;
49838874Sache	static char buf[256];
49917658Sjulian
50017658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
50117658Sjulian	if (panicstr)
50217658Sjulian		bootopt |= RB_NOSYNC;
50317658Sjulian	else
50417658Sjulian		panicstr = fmt;
50517658Sjulian
50617658Sjulian	va_start(ap, fmt);
50741514Sarchie	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
50838874Sache	if (panicstr == fmt)
50938874Sache		panicstr = buf;
51017658Sjulian	va_end(ap);
51138874Sache	printf("panic: %s\n", buf);
51226100Sfsmp#ifdef SMP
51329128Speter	/* three seperate prints in case of an unmapped page and trap */
51429128Speter	printf("mp_lock = %08x; ", mp_lock);
51529128Speter	printf("cpuid = %d; ", cpuid);
51629128Speter	printf("lapic.id = %08x\n", lapic.id);
51726100Sfsmp#endif
51817658Sjulian
51917658Sjulian#if defined(DDB)
52017658Sjulian	if (debugger_on_panic)
52117658Sjulian		Debugger ("panic");
52217658Sjulian#endif
52317658Sjulian	boot(bootopt);
52417658Sjulian}
52517658Sjulian
52617768Sjulian/*
52743436Smsmith * Support for poweroff delay.
52843436Smsmith */
52943436Smsmithstatic int poweroff_delay = 0;
53043436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
53143436Smsmith	&poweroff_delay, 0, "");
53243436Smsmith
53350107Smsmithstatic void
53450107Smsmithpoweroff_wait(void *junk, int howto)
53543436Smsmith{
53643436Smsmith	if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
53743436Smsmith		return;
53843436Smsmith	DELAY(poweroff_delay * 1000);
53943436Smsmith}
540