kern_shutdown.c revision 69335
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 69335 2000-11-29 01:33:15Z jhb $
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>
5060041Sphk#include <sys/bio.h>
5131275Sbde#include <sys/buf.h>
5217658Sjulian#include <sys/reboot.h>
5317658Sjulian#include <sys/proc.h>
5441137Smsmith#include <sys/vnode.h>
5517658Sjulian#include <sys/kernel.h>
5655539Sluoqi#include <sys/kthread.h>
5721776Sbde#include <sys/mount.h>
5867365Sjhb#include <sys/mutex.h>
5939237Sgibbs#include <sys/queue.h>
6017658Sjulian#include <sys/sysctl.h>
6117658Sjulian#include <sys/conf.h>
6217658Sjulian#include <sys/sysproto.h>
6349558Sphk#include <sys/cons.h>
6417658Sjulian
6517658Sjulian#include <machine/pcb.h>
6665557Sjasone#include <machine/lock.h>
6717658Sjulian#include <machine/md_var.h>
6826812Speter#include <machine/smp.h>		/* smp_active, cpuid */
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
11167093Spsint dumping;				 /* system is dumping */
11267093Sps
11365395Speterstatic void boot(int) __dead2;
11465395Speterstatic void dumpsys(void);
11565395Speterstatic void poweroff_wait(void *, int);
11665395Speterstatic void shutdown_halt(void *junk, int howto);
11765395Speterstatic void shutdown_panic(void *junk, int howto);
11865395Speterstatic void shutdown_reset(void *junk, int howto);
11917658Sjulian
12050107Smsmith/* register various local shutdown events */
12150107Smsmithstatic void
12250107Smsmithshutdown_conf(void *unused)
12350107Smsmith{
12450107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST);
12550107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100);
12650107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100);
12750107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200);
12850107Smsmith}
12948868Sphk
13050107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
13150107Smsmith
13217658Sjulian/* ARGSUSED */
13317658Sjulian
13417658Sjulian/*
13517658Sjulian * The system call that results in a reboot
13617658Sjulian */
13717658Sjulianint
13865395Speterreboot(struct proc *p, 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 */
15265268Smsmithstatic int shutdown_howto = 0;
15365268Smsmith
15417658Sjulianvoid
15565268Smsmithshutdown_nice(int howto)
15617658Sjulian{
15765268Smsmith	shutdown_howto = howto;
15865268Smsmith
15917658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
16017658Sjulian	if (initproc != NULL) {
16117658Sjulian		psignal(initproc, SIGINT);
16217658Sjulian	} else {
16317658Sjulian		/* No init(8) running, so simply reboot */
16417658Sjulian		boot(RB_NOSYNC);
16517658Sjulian	}
16617658Sjulian	return;
16717658Sjulian}
16817658Sjulianstatic int	waittime = -1;
16917658Sjulianstatic struct pcb dumppcb;
17017658Sjulian
17154233Sphkstatic void
17265395Speterprint_uptime(void)
17354233Sphk{
17454233Sphk	int f;
17554233Sphk	struct timespec ts;
17654233Sphk
17754233Sphk	getnanouptime(&ts);
17854233Sphk	printf("Uptime: ");
17954233Sphk	f = 0;
18054233Sphk	if (ts.tv_sec >= 86400) {
18165764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
18254233Sphk		ts.tv_sec %= 86400;
18354233Sphk		f = 1;
18454233Sphk	}
18554233Sphk	if (f || ts.tv_sec >= 3600) {
18665764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
18754233Sphk		ts.tv_sec %= 3600;
18854233Sphk		f = 1;
18954233Sphk	}
19054233Sphk	if (f || ts.tv_sec >= 60) {
19165764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
19254233Sphk		ts.tv_sec %= 60;
19354233Sphk		f = 1;
19454233Sphk	}
19565764Sjhb	printf("%lds\n", (long)ts.tv_sec);
19654233Sphk}
19754233Sphk
19817658Sjulian/*
19917658Sjulian *  Go through the rigmarole of shutting down..
20017658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
20117658Sjulian * anything machine dependant in it.
20217658Sjulian */
20331275Sbdestatic void
20465395Speterboot(int howto)
20517658Sjulian{
20617658Sjulian
20765268Smsmith	/* collect extra flags that shutdown_nice might have set */
20865268Smsmith	howto |= shutdown_howto;
20965268Smsmith
21025164Speter#ifdef SMP
21165395Speter	if (smp_active)
21226812Speter		printf("boot() called on cpu#%d\n", cpuid);
21325164Speter#endif
21427997Sjulian	/*
21527997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
21627997Sjulian	 */
21750107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
21827997Sjulian
21927997Sjulian	/*
22027997Sjulian	 * Now sync filesystems
22127997Sjulian	 */
22217658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
22317658Sjulian		register struct buf *bp;
22465707Sjasone		int iter, nbusy, pbusy;
22565707Sjasone		int subiter;
22617658Sjulian
22717658Sjulian		waittime = 0;
22817658Sjulian		printf("\nsyncing disks... ");
22917658Sjulian
23030994Sphk		sync(&proc0, NULL);
23117658Sjulian
23234266Sjulian		/*
23334266Sjulian		 * With soft updates, some buffers that are
23434266Sjulian		 * written will be remarked as dirty until other
23534266Sjulian		 * buffers are written.
23634266Sjulian		 */
23765707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
23817658Sjulian			nbusy = 0;
23917658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
24048225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
24148225Smckusick				    BUF_REFCNT(bp) > 0) {
24217658Sjulian					nbusy++;
24334266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
24434266Sjulian						== B_DELWRI) {
24534266Sjulian					/* bawrite(bp);*/
24634266Sjulian					nbusy++;
24717658Sjulian				}
24817658Sjulian			}
24917658Sjulian			if (nbusy == 0)
25017658Sjulian				break;
25117658Sjulian			printf("%d ", nbusy);
25265707Sjasone			if (nbusy < pbusy)
25365707Sjasone				iter = 0;
25465707Sjasone			pbusy = nbusy;
25534266Sjulian			sync(&proc0, NULL);
25665707Sjasone 			if (curproc != NULL) {
25768808Sjhb				DROP_GIANT_NOSWITCH();
25865707Sjasone   				for (subiter = 0; subiter < 50 * iter; subiter++) {
25965707Sjasone     					mtx_enter(&sched_lock, MTX_SPIN);
26065707Sjasone     					setrunqueue(curproc);
26165707Sjasone     					mi_switch(); /* Allow interrupt threads to run */
26265707Sjasone     					mtx_exit(&sched_lock, MTX_SPIN);
26365707Sjasone     					DELAY(1000);
26465707Sjasone   				}
26568808Sjhb				PICKUP_GIANT();
26665707Sjasone 			} else
26734266Sjulian			DELAY(50000 * iter);
26817658Sjulian		}
26953023Sphk		printf("\n");
27041137Smsmith		/*
27141137Smsmith		 * Count only busy local buffers to prevent forcing
27241137Smsmith		 * a fsck if we're just a client of a wedged NFS server
27341137Smsmith		 */
27441137Smsmith		nbusy = 0;
27541137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
27648225Smckusick			if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
27748225Smckusick			    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
27853023Sphk				if (bp->b_dev == NODEV) {
27953452Sphk					TAILQ_REMOVE(&mountlist,
28048225Smckusick					    bp->b_vp->v_mount, mnt_list);
28153023Sphk					continue;
28253023Sphk				}
28353023Sphk				nbusy++;
28453023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
28553023Sphk				printf(
28653023Sphk			    "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n",
28753023Sphk				    nbusy, devtoname(bp->b_dev),
28853023Sphk				    bp->b_flags, (long)bp->b_blkno,
28953023Sphk				    (long)bp->b_lblkno);
29053023Sphk#endif
29146568Speter			}
29241137Smsmith		}
29317658Sjulian		if (nbusy) {
29417658Sjulian			/*
29517658Sjulian			 * Failed to sync all blocks. Indicate this and don't
29617658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
29717658Sjulian			 */
29853023Sphk			printf("giving up on %d buffers\n", nbusy);
29917658Sjulian			DELAY(5000000);	/* 5 seconds */
30017658Sjulian		} else {
30117658Sjulian			printf("done\n");
30217658Sjulian			/*
30317658Sjulian			 * Unmount filesystems
30417658Sjulian			 */
30517658Sjulian			if (panicstr == 0)
30617658Sjulian				vfs_unmountall();
30717658Sjulian		}
30839237Sgibbs		DELAY(100000);		/* wait for console output to finish */
30917658Sjulian	}
31027997Sjulian
31154233Sphk	print_uptime();
31254233Sphk
31327997Sjulian	/*
31427997Sjulian	 * Ok, now do things that assume all filesystem activity has
31527997Sjulian	 * been completed.
31627997Sjulian	 */
31750107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
31839237Sgibbs	splhigh();
31965394Speter	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold)
32039237Sgibbs		dumpsys();
32139237Sgibbs
32239237Sgibbs	/* Now that we're going to really halt the system... */
32350107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
32439237Sgibbs
32550107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
32650107Smsmith	/* NOTREACHED */
32750107Smsmith}
32850107Smsmith
32950107Smsmith/*
33050107Smsmith * If the shutdown was a clean halt, behave accordingly.
33150107Smsmith */
33250107Smsmithstatic void
33350107Smsmithshutdown_halt(void *junk, int howto)
33450107Smsmith{
33517658Sjulian	if (howto & RB_HALT) {
33617658Sjulian		printf("\n");
33717658Sjulian		printf("The operating system has halted.\n");
33817658Sjulian		printf("Please press any key to reboot.\n\n");
33919274Sjulian		switch (cngetc()) {
34019274Sjulian		case -1:		/* No console, just die */
34119274Sjulian			cpu_halt();
34219274Sjulian			/* NOTREACHED */
34319274Sjulian		default:
34439237Sgibbs			howto &= ~RB_HALT;
34519274Sjulian			break;
34619274Sjulian		}
34750107Smsmith	}
34850107Smsmith}
34917658Sjulian
35050107Smsmith/*
35150107Smsmith * Check to see if the system paniced, pause and then reboot
35250107Smsmith * according to the specified delay.
35350107Smsmith */
35450107Smsmithstatic void
35550107Smsmithshutdown_panic(void *junk, int howto)
35650107Smsmith{
35750107Smsmith	int loop;
35850107Smsmith
35950107Smsmith	if (howto & RB_DUMP) {
36039237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
36139237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
36239237Sgibbs				printf("Automatic reboot in %d seconds - "
36339237Sgibbs				       "press a key on the console to abort\n",
36439237Sgibbs					PANIC_REBOOT_WAIT_TIME);
36539237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
36639237Sgibbs				     loop > 0; --loop) {
36739237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
36839237Sgibbs					/* Did user type a key? */
36939237Sgibbs					if (cncheckc() != -1)
37039237Sgibbs						break;
37117658Sjulian				}
37239237Sgibbs				if (!loop)
37350107Smsmith					return;
37417658Sjulian			}
37539237Sgibbs		} else { /* zero time specified - reboot NOW */
37650107Smsmith			return;
37717658Sjulian		}
37839237Sgibbs		printf("--> Press a key on the console to reboot <--\n");
37939237Sgibbs		cngetc();
38017658Sjulian	}
38150107Smsmith}
38250107Smsmith
38350107Smsmith/*
38450107Smsmith * Everything done, now reset
38550107Smsmith */
38650107Smsmithstatic void
38750107Smsmithshutdown_reset(void *junk, int howto)
38850107Smsmith{
38917658Sjulian	printf("Rebooting...\n");
39017658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
39117677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
39217658Sjulian	cpu_reset();
39350107Smsmith	/* NOTREACHED */ /* assuming reset worked */
39417658Sjulian}
39517658Sjulian
39617658Sjulian/*
39717658Sjulian * Magic number for savecore
39817658Sjulian *
39917658Sjulian * exported (symorder) and used at least by savecore(8)
40017658Sjulian *
40117658Sjulian */
40217658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
40317658Sjulian
40417658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
40517658Sjulian
40617658Sjulianstatic int	dodump = 1;
40717658Sjulian
40848868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
40948868Sphk    "Try to perform coredump on kernel panic");
41048868Sphk
41148868Sphkstatic int
41265395Spetersetdumpdev(dev_t dev)
41348868Sphk{
41450571Sphk	int psize;
41548868Sphk	long newdumplo;
41648868Sphk
41748868Sphk	if (dev == NODEV) {
41848868Sphk		dumpdev = dev;
41948868Sphk		return (0);
42048868Sphk	}
42149679Sphk	if (devsw(dev) == NULL)
42248868Sphk		return (ENXIO);		/* XXX is this right? */
42349679Sphk	if (devsw(dev)->d_psize == NULL)
42448868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
42549679Sphk	psize = devsw(dev)->d_psize(dev);
42648868Sphk	if (psize == -1)
42748868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
42848868Sphk	/*
42950571Sphk	 * XXX should clean up checking in dumpsys() to be more like this.
43048868Sphk	 */
43148868Sphk	newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE;
43248868Sphk	if (newdumplo < 0)
43348868Sphk		return (ENOSPC);
43448868Sphk	dumpdev = dev;
43548868Sphk	dumplo = newdumplo;
43648868Sphk	return (0);
43748868Sphk}
43848868Sphk
43948868Sphk
44031403Sjulian/* ARGSUSED */
44131403Sjulianstatic void
44265395Speterdump_conf(void *dummy)
44331403Sjulian{
44448868Sphk	if (setdumpdev(dumpdev) != 0)
44548868Sphk		dumpdev = NODEV;
44631403Sjulian}
44748868Sphk
44831403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
44931403Sjulian
45048868Sphkstatic int
45162573Sphksysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS)
45248868Sphk{
45348868Sphk	int error;
45448868Sphk	udev_t ndumpdev;
45548868Sphk
45653838Sphk	ndumpdev = dev2udev(dumpdev);
45748868Sphk	error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
45848868Sphk	if (error == 0 && req->newptr != NULL)
45953838Sphk		error = setdumpdev(udev2dev(ndumpdev, 0));
46048868Sphk	return (error);
46148868Sphk}
46248868Sphk
46348868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
46448868Sphk	0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", "");
46548868Sphk
46617658Sjulian/*
46717658Sjulian * Doadump comes here after turning off memory management and
46817658Sjulian * getting on the dump stack, either when called above, or by
46917658Sjulian * the auto-restart code.
47017658Sjulian */
47117658Sjulianstatic void
47217658Sjuliandumpsys(void)
47317658Sjulian{
47449627Salfred	int	error;
47517658Sjulian
47665394Speter	savectx(&dumppcb);
47750571Sphk	if (dumping++) {
47850571Sphk		printf("Dump already in progress, bailing...\n");
47950571Sphk		return;
48050571Sphk	}
48117658Sjulian	if (!dodump)
48217658Sjulian		return;
48317658Sjulian	if (dumpdev == NODEV)
48417658Sjulian		return;
48549679Sphk	if (!(devsw(dumpdev)))
48617658Sjulian		return;
48749679Sphk	if (!(devsw(dumpdev)->d_dump))
48817658Sjulian		return;
48917658Sjulian	dumpsize = Maxmem;
49050571Sphk	printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo);
49117658Sjulian	printf("dump ");
49249679Sphk	error = (*devsw(dumpdev)->d_dump)(dumpdev);
49349627Salfred	if (error == 0) {
49449627Salfred		printf("succeeded\n");
49549627Salfred		return;
49649627Salfred	}
49749627Salfred	printf("failed, reason: ");
49849627Salfred	switch (error) {
49949627Salfred	case ENODEV:
50049627Salfred		printf("device doesn't support a dump routine\n");
50149627Salfred		break;
50217658Sjulian
50317658Sjulian	case ENXIO:
50417658Sjulian		printf("device bad\n");
50517658Sjulian		break;
50617658Sjulian
50717658Sjulian	case EFAULT:
50817658Sjulian		printf("device not ready\n");
50917658Sjulian		break;
51017658Sjulian
51117658Sjulian	case EINVAL:
51217658Sjulian		printf("area improper\n");
51317658Sjulian		break;
51417658Sjulian
51517658Sjulian	case EIO:
51617658Sjulian		printf("i/o error\n");
51717658Sjulian		break;
51817658Sjulian
51917658Sjulian	case EINTR:
52017658Sjulian		printf("aborted from console\n");
52117658Sjulian		break;
52217658Sjulian
52317658Sjulian	default:
52449627Salfred		printf("unknown, error = %d\n", error);
52517658Sjulian		break;
52617658Sjulian	}
52717658Sjulian}
52817658Sjulian
52917658Sjulian/*
53017658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
53117658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
53217658Sjulian * the disks as this often leads to recursive panics.
53317658Sjulian */
53417658Sjulianvoid
53517658Sjulianpanic(const char *fmt, ...)
53617658Sjulian{
53717658Sjulian	int bootopt;
53817658Sjulian	va_list ap;
53938874Sache	static char buf[256];
54017658Sjulian
54165557Sjasone#ifdef SMP
54265557Sjasone	/* Only 1 CPU can panic at a time */
54365557Sjasone	s_lock(&panic_lock);
54465557Sjasone#endif
54565557Sjasone
54617658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
54717658Sjulian	if (panicstr)
54817658Sjulian		bootopt |= RB_NOSYNC;
54917658Sjulian	else
55017658Sjulian		panicstr = fmt;
55117658Sjulian
55217658Sjulian	va_start(ap, fmt);
55341514Sarchie	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
55438874Sache	if (panicstr == fmt)
55538874Sache		panicstr = buf;
55617658Sjulian	va_end(ap);
55738874Sache	printf("panic: %s\n", buf);
55826100Sfsmp#ifdef SMP
55965557Sjasone	/* two seperate prints in case of an unmapped page and trap */
56029128Speter	printf("cpuid = %d; ", cpuid);
56169335Sjhb#ifdef APIC_IO
56229128Speter	printf("lapic.id = %08x\n", lapic.id);
56326100Sfsmp#endif
56469335Sjhb#endif
56517658Sjulian
56617658Sjulian#if defined(DDB)
56717658Sjulian	if (debugger_on_panic)
56817658Sjulian		Debugger ("panic");
56917658Sjulian#endif
57017658Sjulian	boot(bootopt);
57117658Sjulian}
57217658Sjulian
57317768Sjulian/*
57443436Smsmith * Support for poweroff delay.
57543436Smsmith */
57654248Smsmith#ifndef POWEROFF_DELAY
57754248Smsmith# define POWEROFF_DELAY 5000
57854248Smsmith#endif
57954248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
58054248Smsmith
58143436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
58243436Smsmith	&poweroff_delay, 0, "");
58343436Smsmith
58450107Smsmithstatic void
58550107Smsmithpoweroff_wait(void *junk, int howto)
58643436Smsmith{
58743436Smsmith	if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
58843436Smsmith		return;
58943436Smsmith	DELAY(poweroff_delay * 1000);
59043436Smsmith}
59155539Sluoqi
59255539Sluoqi/*
59355539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
59455539Sluoqi * points in their main loops prior to a system shutdown, so that they
59555539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
59655539Sluoqi * to cause sync to fail).  For each of these system processes, register
59755539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
59855539Sluoqi */
59955539Sluoqistatic int kproc_shutdown_wait = 60;
60055539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
60155539Sluoqi    &kproc_shutdown_wait, 0, "");
60255539Sluoqi
60355539Sluoqivoid
60455539Sluoqishutdown_kproc(void *arg, int howto)
60555539Sluoqi{
60655539Sluoqi	struct proc *p;
60755539Sluoqi	int error;
60855539Sluoqi
60955539Sluoqi	if (panicstr)
61055539Sluoqi		return;
61155539Sluoqi
61255539Sluoqi	p = (struct proc *)arg;
61355539Sluoqi	printf("Waiting (max %d seconds) for system process `%s' to stop...",
61455862Sluoqi	    kproc_shutdown_wait, p->p_comm);
61555862Sluoqi	error = suspend_kproc(p, kproc_shutdown_wait * hz);
61655539Sluoqi
61755539Sluoqi	if (error == EWOULDBLOCK)
61855539Sluoqi		printf("timed out\n");
61955539Sluoqi	else
62055539Sluoqi		printf("stopped\n");
62155539Sluoqi}
622