kern_shutdown.c revision 83703
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 83703 2001-09-20 06:08:53Z peter $
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>
4960041Sphk#include <sys/bio.h>
5031275Sbde#include <sys/buf.h>
5178767Sjhb#include <sys/conf.h>
5278767Sjhb#include <sys/cons.h>
5381688Sbde#include <sys/disklabel.h>
5478767Sjhb#include <sys/eventhandler.h>
5517658Sjulian#include <sys/kernel.h>
5655539Sluoqi#include <sys/kthread.h>
5721776Sbde#include <sys/mount.h>
5878767Sjhb#include <sys/proc.h>
5978767Sjhb#include <sys/reboot.h>
6078767Sjhb#include <sys/resourcevar.h>
6178767Sjhb#include <sys/smp.h>		/* smp_active */
6217658Sjulian#include <sys/sysctl.h>
6317658Sjulian#include <sys/sysproto.h>
6478767Sjhb#include <sys/vnode.h>
6517658Sjulian
6617658Sjulian#include <machine/pcb.h>
6717658Sjulian#include <machine/md_var.h>
6817658Sjulian
6917658Sjulian#include <sys/signalvar.h>
7082119Sjhb#ifdef DDB
7182119Sjhb#include <ddb/ddb.h>
7282119Sjhb#endif
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
8428769Sbde#ifdef DDB
8517658Sjulian#ifdef DDB_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");
9217658Sjulian#endif
9317658Sjulian
9443436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
9543436Smsmith
9628000Sjulian#ifdef	HW_WDOG
9717658Sjulian/*
9827997Sjulian * If there is a hardware watchdog, point this at the function needed to
9927997Sjulian * hold it off.
10027997Sjulian * It's needed when the kernel needs to do some lengthy operations.
10127997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have
10227997Sjulian * your precious core-dump only half written because the wdog kicked in.
10327997Sjulian */
10427997Sjulianwatchdog_tickle_fn wdog_tickler = NULL;
10528000Sjulian#endif	/* HW_WDOG */
10627997Sjulian
10727997Sjulian/*
10817658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
10917658Sjulian * to indicate that the kernel has already called panic.
11017658Sjulian */
11117658Sjulianconst char *panicstr;
11217658Sjulian
11367093Spsint dumping;				 /* system is dumping */
11467093Sps
11565395Speterstatic void boot(int) __dead2;
11665395Speterstatic void dumpsys(void);
11765395Speterstatic void poweroff_wait(void *, int);
11865395Speterstatic void shutdown_halt(void *junk, int howto);
11965395Speterstatic void shutdown_panic(void *junk, int howto);
12065395Speterstatic void shutdown_reset(void *junk, int howto);
12117658Sjulian
12250107Smsmith/* register various local shutdown events */
12350107Smsmithstatic void
12450107Smsmithshutdown_conf(void *unused)
12550107Smsmith{
12650107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST);
12750107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100);
12850107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100);
12950107Smsmith	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200);
13050107Smsmith}
13148868Sphk
13250107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
13350107Smsmith
13417658Sjulian/*
13517658Sjulian * The system call that results in a reboot
13682749Sdillon *
13782749Sdillon * MPSAFE
13817658Sjulian */
13982749Sdillon/* ARGSUSED */
14017658Sjulianint
14183366Sjulianreboot(struct thread *td, struct reboot_args *uap)
14217658Sjulian{
14317658Sjulian	int error;
14417658Sjulian
14582749Sdillon	mtx_lock(&Giant);
14683366Sjulian	if ((error = suser_td(td)) == 0)
14782749Sdillon		boot(uap->opt);
14882749Sdillon	mtx_unlock(&Giant);
14982749Sdillon	return (error);
15017658Sjulian}
15117658Sjulian
15217658Sjulian/*
15317658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
15417658Sjulian */
15565268Smsmithstatic int shutdown_howto = 0;
15665268Smsmith
15717658Sjulianvoid
15865268Smsmithshutdown_nice(int howto)
15917658Sjulian{
16065268Smsmith	shutdown_howto = howto;
16165268Smsmith
16217658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
16317658Sjulian	if (initproc != NULL) {
16473913Sjhb		PROC_LOCK(initproc);
16517658Sjulian		psignal(initproc, SIGINT);
16673913Sjhb		PROC_UNLOCK(initproc);
16717658Sjulian	} else {
16817658Sjulian		/* No init(8) running, so simply reboot */
16917658Sjulian		boot(RB_NOSYNC);
17017658Sjulian	}
17117658Sjulian	return;
17217658Sjulian}
17317658Sjulianstatic int	waittime = -1;
17417658Sjulianstatic struct pcb dumppcb;
17517658Sjulian
17654233Sphkstatic void
17765395Speterprint_uptime(void)
17854233Sphk{
17954233Sphk	int f;
18054233Sphk	struct timespec ts;
18154233Sphk
18254233Sphk	getnanouptime(&ts);
18354233Sphk	printf("Uptime: ");
18454233Sphk	f = 0;
18554233Sphk	if (ts.tv_sec >= 86400) {
18665764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
18754233Sphk		ts.tv_sec %= 86400;
18854233Sphk		f = 1;
18954233Sphk	}
19054233Sphk	if (f || ts.tv_sec >= 3600) {
19165764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
19254233Sphk		ts.tv_sec %= 3600;
19354233Sphk		f = 1;
19454233Sphk	}
19554233Sphk	if (f || ts.tv_sec >= 60) {
19665764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
19754233Sphk		ts.tv_sec %= 60;
19854233Sphk		f = 1;
19954233Sphk	}
20065764Sjhb	printf("%lds\n", (long)ts.tv_sec);
20154233Sphk}
20254233Sphk
20317658Sjulian/*
20417658Sjulian *  Go through the rigmarole of shutting down..
20517658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
20617658Sjulian * anything machine dependant in it.
20717658Sjulian */
20831275Sbdestatic void
20965395Speterboot(int howto)
21017658Sjulian{
21117658Sjulian
21265268Smsmith	/* collect extra flags that shutdown_nice might have set */
21365268Smsmith	howto |= shutdown_howto;
21465268Smsmith
21582119Sjhb#ifdef DDB
21682119Sjhb	/* We are out of the debugger now. */
21782119Sjhb	db_active = 0;
21882119Sjhb#endif
21982119Sjhb
22025164Speter#ifdef SMP
22165395Speter	if (smp_active)
22270861Sjake		printf("boot() called on cpu#%d\n", PCPU_GET(cpuid));
22325164Speter#endif
22427997Sjulian	/*
22527997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
22627997Sjulian	 */
22750107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
22827997Sjulian
22927997Sjulian	/*
23027997Sjulian	 * Now sync filesystems
23127997Sjulian	 */
23217658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
23317658Sjulian		register struct buf *bp;
23465707Sjasone		int iter, nbusy, pbusy;
23565707Sjasone		int subiter;
23617658Sjulian
23717658Sjulian		waittime = 0;
23817658Sjulian		printf("\nsyncing disks... ");
23917658Sjulian
24083366Sjulian		sync(thread0, NULL);
24117658Sjulian
24234266Sjulian		/*
24334266Sjulian		 * With soft updates, some buffers that are
24434266Sjulian		 * written will be remarked as dirty until other
24534266Sjulian		 * buffers are written.
24634266Sjulian		 */
24765707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
24817658Sjulian			nbusy = 0;
24917658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
25048225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
25148225Smckusick				    BUF_REFCNT(bp) > 0) {
25217658Sjulian					nbusy++;
25334266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
25434266Sjulian						== B_DELWRI) {
25534266Sjulian					/* bawrite(bp);*/
25634266Sjulian					nbusy++;
25717658Sjulian				}
25817658Sjulian			}
25917658Sjulian			if (nbusy == 0)
26017658Sjulian				break;
26117658Sjulian			printf("%d ", nbusy);
26265707Sjasone			if (nbusy < pbusy)
26365707Sjasone				iter = 0;
26465707Sjasone			pbusy = nbusy;
26583366Sjulian			sync(thread0, NULL);
26683366Sjulian 			if (curthread != NULL) {
26768808Sjhb				DROP_GIANT_NOSWITCH();
26865707Sjasone   				for (subiter = 0; subiter < 50 * iter; subiter++) {
26972200Sbmilekic     					mtx_lock_spin(&sched_lock);
27083366Sjulian     					setrunqueue(curthread);
27183366Sjulian					curthread->td_proc->p_stats->p_ru.ru_nvcsw++;
27265707Sjasone     					mi_switch(); /* Allow interrupt threads to run */
27372200Sbmilekic     					mtx_unlock_spin(&sched_lock);
27465707Sjasone     					DELAY(1000);
27565707Sjasone   				}
27668808Sjhb				PICKUP_GIANT();
27765707Sjasone 			} else
27834266Sjulian			DELAY(50000 * iter);
27917658Sjulian		}
28053023Sphk		printf("\n");
28141137Smsmith		/*
28241137Smsmith		 * Count only busy local buffers to prevent forcing
28341137Smsmith		 * a fsck if we're just a client of a wedged NFS server
28441137Smsmith		 */
28541137Smsmith		nbusy = 0;
28641137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
28748225Smckusick			if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
28848225Smckusick			    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
28953023Sphk				if (bp->b_dev == NODEV) {
29053452Sphk					TAILQ_REMOVE(&mountlist,
29148225Smckusick					    bp->b_vp->v_mount, mnt_list);
29253023Sphk					continue;
29353023Sphk				}
29453023Sphk				nbusy++;
29553023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
29653023Sphk				printf(
29753023Sphk			    "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n",
29853023Sphk				    nbusy, devtoname(bp->b_dev),
29953023Sphk				    bp->b_flags, (long)bp->b_blkno,
30053023Sphk				    (long)bp->b_lblkno);
30153023Sphk#endif
30246568Speter			}
30341137Smsmith		}
30417658Sjulian		if (nbusy) {
30517658Sjulian			/*
30617658Sjulian			 * Failed to sync all blocks. Indicate this and don't
30717658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
30817658Sjulian			 */
30953023Sphk			printf("giving up on %d buffers\n", nbusy);
31017658Sjulian			DELAY(5000000);	/* 5 seconds */
31117658Sjulian		} else {
31217658Sjulian			printf("done\n");
31317658Sjulian			/*
31417658Sjulian			 * Unmount filesystems
31517658Sjulian			 */
31617658Sjulian			if (panicstr == 0)
31717658Sjulian				vfs_unmountall();
31817658Sjulian		}
31939237Sgibbs		DELAY(100000);		/* wait for console output to finish */
32017658Sjulian	}
32127997Sjulian
32254233Sphk	print_uptime();
32354233Sphk
32427997Sjulian	/*
32527997Sjulian	 * Ok, now do things that assume all filesystem activity has
32627997Sjulian	 * been completed.
32727997Sjulian	 */
32850107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
32939237Sgibbs	splhigh();
33065394Speter	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold)
33139237Sgibbs		dumpsys();
33239237Sgibbs
33339237Sgibbs	/* Now that we're going to really halt the system... */
33450107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
33539237Sgibbs
33650107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
33750107Smsmith	/* NOTREACHED */
33850107Smsmith}
33950107Smsmith
34050107Smsmith/*
34150107Smsmith * If the shutdown was a clean halt, behave accordingly.
34250107Smsmith */
34350107Smsmithstatic void
34450107Smsmithshutdown_halt(void *junk, int howto)
34550107Smsmith{
34617658Sjulian	if (howto & RB_HALT) {
34717658Sjulian		printf("\n");
34817658Sjulian		printf("The operating system has halted.\n");
34917658Sjulian		printf("Please press any key to reboot.\n\n");
35019274Sjulian		switch (cngetc()) {
35119274Sjulian		case -1:		/* No console, just die */
35219274Sjulian			cpu_halt();
35319274Sjulian			/* NOTREACHED */
35419274Sjulian		default:
35539237Sgibbs			howto &= ~RB_HALT;
35619274Sjulian			break;
35719274Sjulian		}
35850107Smsmith	}
35950107Smsmith}
36017658Sjulian
36150107Smsmith/*
36250107Smsmith * Check to see if the system paniced, pause and then reboot
36350107Smsmith * according to the specified delay.
36450107Smsmith */
36550107Smsmithstatic void
36650107Smsmithshutdown_panic(void *junk, int howto)
36750107Smsmith{
36850107Smsmith	int loop;
36950107Smsmith
37050107Smsmith	if (howto & RB_DUMP) {
37139237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
37239237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
37339237Sgibbs				printf("Automatic reboot in %d seconds - "
37439237Sgibbs				       "press a key on the console to abort\n",
37539237Sgibbs					PANIC_REBOOT_WAIT_TIME);
37639237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
37739237Sgibbs				     loop > 0; --loop) {
37839237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
37939237Sgibbs					/* Did user type a key? */
38039237Sgibbs					if (cncheckc() != -1)
38139237Sgibbs						break;
38217658Sjulian				}
38339237Sgibbs				if (!loop)
38450107Smsmith					return;
38517658Sjulian			}
38639237Sgibbs		} else { /* zero time specified - reboot NOW */
38750107Smsmith			return;
38817658Sjulian		}
38939237Sgibbs		printf("--> Press a key on the console to reboot <--\n");
39039237Sgibbs		cngetc();
39117658Sjulian	}
39250107Smsmith}
39350107Smsmith
39450107Smsmith/*
39550107Smsmith * Everything done, now reset
39650107Smsmith */
39750107Smsmithstatic void
39850107Smsmithshutdown_reset(void *junk, int howto)
39950107Smsmith{
40017658Sjulian	printf("Rebooting...\n");
40117658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
40217677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
40317658Sjulian	cpu_reset();
40450107Smsmith	/* NOTREACHED */ /* assuming reset worked */
40517658Sjulian}
40617658Sjulian
40717658Sjulian/*
40817658Sjulian * Magic number for savecore
40917658Sjulian *
41017658Sjulian * exported (symorder) and used at least by savecore(8)
41117658Sjulian *
41217658Sjulian */
41317658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
41417658Sjulian
41517658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
41617658Sjulian
41717658Sjulianstatic int	dodump = 1;
41817658Sjulian
41948868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
42048868Sphk    "Try to perform coredump on kernel panic");
42148868Sphk
42248868Sphkstatic int
42365395Spetersetdumpdev(dev_t dev)
42448868Sphk{
42550571Sphk	int psize;
42648868Sphk	long newdumplo;
42748868Sphk
42848868Sphk	if (dev == NODEV) {
42948868Sphk		dumpdev = dev;
43048868Sphk		return (0);
43148868Sphk	}
43249679Sphk	if (devsw(dev) == NULL)
43348868Sphk		return (ENXIO);		/* XXX is this right? */
43449679Sphk	if (devsw(dev)->d_psize == NULL)
43548868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
43649679Sphk	psize = devsw(dev)->d_psize(dev);
43748868Sphk	if (psize == -1)
43848868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
43948868Sphk	/*
44050571Sphk	 * XXX should clean up checking in dumpsys() to be more like this.
44148868Sphk	 */
44281688Sbde	newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE);
44381688Sbde	if (newdumplo <= LABELSECTOR)
44448868Sphk		return (ENOSPC);
44548868Sphk	dumpdev = dev;
44648868Sphk	dumplo = newdumplo;
44748868Sphk	return (0);
44848868Sphk}
44948868Sphk
45048868Sphk
45131403Sjulian/* ARGSUSED */
45231403Sjulianstatic void
45365395Speterdump_conf(void *dummy)
45431403Sjulian{
45548868Sphk	if (setdumpdev(dumpdev) != 0)
45648868Sphk		dumpdev = NODEV;
45731403Sjulian}
45848868Sphk
45931403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
46031403Sjulian
46148868Sphkstatic int
46262573Sphksysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS)
46348868Sphk{
46448868Sphk	int error;
46548868Sphk	udev_t ndumpdev;
46648868Sphk
46753838Sphk	ndumpdev = dev2udev(dumpdev);
46848868Sphk	error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
46948868Sphk	if (error == 0 && req->newptr != NULL)
47053838Sphk		error = setdumpdev(udev2dev(ndumpdev, 0));
47148868Sphk	return (error);
47248868Sphk}
47348868Sphk
47448868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
47548868Sphk	0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", "");
47648868Sphk
47717658Sjulian/*
47817658Sjulian * Doadump comes here after turning off memory management and
47917658Sjulian * getting on the dump stack, either when called above, or by
48017658Sjulian * the auto-restart code.
48117658Sjulian */
48217658Sjulianstatic void
48317658Sjuliandumpsys(void)
48417658Sjulian{
48549627Salfred	int	error;
48617658Sjulian
48765394Speter	savectx(&dumppcb);
48817658Sjulian	if (!dodump)
48917658Sjulian		return;
49017658Sjulian	if (dumpdev == NODEV)
49117658Sjulian		return;
49249679Sphk	if (!(devsw(dumpdev)))
49317658Sjulian		return;
49449679Sphk	if (!(devsw(dumpdev)->d_dump))
49517658Sjulian		return;
49683703Speter	if (dumping++) {
49783703Speter		dumping--;
49883703Speter		printf("Dump already in progress, bailing...\n");
49983703Speter		return;
50083703Speter	}
50117658Sjulian	dumpsize = Maxmem;
50250571Sphk	printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo);
50317658Sjulian	printf("dump ");
50449679Sphk	error = (*devsw(dumpdev)->d_dump)(dumpdev);
50583703Speter	dumping--;
50649627Salfred	if (error == 0) {
50749627Salfred		printf("succeeded\n");
50849627Salfred		return;
50949627Salfred	}
51049627Salfred	printf("failed, reason: ");
51149627Salfred	switch (error) {
51249627Salfred	case ENODEV:
51349627Salfred		printf("device doesn't support a dump routine\n");
51449627Salfred		break;
51517658Sjulian
51617658Sjulian	case ENXIO:
51717658Sjulian		printf("device bad\n");
51817658Sjulian		break;
51917658Sjulian
52017658Sjulian	case EFAULT:
52117658Sjulian		printf("device not ready\n");
52217658Sjulian		break;
52317658Sjulian
52417658Sjulian	case EINVAL:
52517658Sjulian		printf("area improper\n");
52617658Sjulian		break;
52717658Sjulian
52817658Sjulian	case EIO:
52917658Sjulian		printf("i/o error\n");
53017658Sjulian		break;
53117658Sjulian
53217658Sjulian	case EINTR:
53317658Sjulian		printf("aborted from console\n");
53417658Sjulian		break;
53517658Sjulian
53617658Sjulian	default:
53749627Salfred		printf("unknown, error = %d\n", error);
53817658Sjulian		break;
53917658Sjulian	}
54017658Sjulian}
54117658Sjulian
54274890Spsint
54374890Spsdumpstatus(vm_offset_t addr, long count)
54474890Sps{
54574890Sps	int c;
54674890Sps
54774890Sps	if (addr % (1024 * 1024) == 0) {
54874890Sps#ifdef HW_WDOG
54974890Sps		if (wdog_tickler)
55074890Sps			(*wdog_tickler)();
55174890Sps#endif
55274890Sps		printf("%ld ", count / (1024 * 1024));
55374890Sps	}
55474890Sps
55574890Sps	if ((c = cncheckc()) == 0x03)
55674890Sps		return -1;
55774890Sps	else if (c != -1)
55874890Sps		printf("[CTRL-C to abort] ");
55974890Sps
56074890Sps	return 0;
56174890Sps}
56274890Sps
56375570Sjhb#ifdef SMP
56475570Sjhbstatic u_int panic_cpu = NOCPU;
56575570Sjhb#endif
56675570Sjhb
56717658Sjulian/*
56817658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
56917658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
57017658Sjulian * the disks as this often leads to recursive panics.
57182749Sdillon *
57282749Sdillon * MPSAFE
57317658Sjulian */
57417658Sjulianvoid
57517658Sjulianpanic(const char *fmt, ...)
57617658Sjulian{
57783312Sjhb	int bootopt;
57817658Sjulian	va_list ap;
57938874Sache	static char buf[256];
58017658Sjulian
58165557Sjasone#ifdef SMP
58282115Sjhb	/*
58382115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
58482115Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
58582115Sjhb	 * panic_cpu if we are spinning in case the panic on the first
58682115Sjhb	 * CPU is canceled.
58782115Sjhb	 */
58883312Sjhb	if (panic_cpu != PCPU_GET(cpuid))
58982115Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
59083312Sjhb		    PCPU_GET(cpuid)) == 0)
59182115Sjhb			while (panic_cpu != NOCPU)
59282115Sjhb				; /* nothing */
59365557Sjasone#endif
59465557Sjasone
59517658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
59617658Sjulian	if (panicstr)
59717658Sjulian		bootopt |= RB_NOSYNC;
59817658Sjulian	else
59917658Sjulian		panicstr = fmt;
60017658Sjulian
60117658Sjulian	va_start(ap, fmt);
60241514Sarchie	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
60338874Sache	if (panicstr == fmt)
60438874Sache		panicstr = buf;
60517658Sjulian	va_end(ap);
60638874Sache	printf("panic: %s\n", buf);
60726100Sfsmp#ifdef SMP
60872091Sasmodai	/* two separate prints in case of an unmapped page and trap */
60970861Sjake	printf("cpuid = %d; ", PCPU_GET(cpuid));
61069335Sjhb#ifdef APIC_IO
61129128Speter	printf("lapic.id = %08x\n", lapic.id);
61226100Sfsmp#endif
61369335Sjhb#endif
61417658Sjulian
61517658Sjulian#if defined(DDB)
61617658Sjulian	if (debugger_on_panic)
61717658Sjulian		Debugger ("panic");
61882223Sjhb#ifdef RESTARTABLE_PANICS
61982115Sjhb	/* See if the user aborted the panic, in which case we continue. */
62082115Sjhb	if (panicstr == NULL) {
62182115Sjhb#ifdef SMP
62282115Sjhb		atomic_store_rel_int(&panic_cpu, NOCPU);
62317658Sjulian#endif
62482115Sjhb		return;
62582115Sjhb	}
62682115Sjhb#endif
62782223Sjhb#endif
62817658Sjulian	boot(bootopt);
62917658Sjulian}
63017658Sjulian
63117768Sjulian/*
63243436Smsmith * Support for poweroff delay.
63343436Smsmith */
63454248Smsmith#ifndef POWEROFF_DELAY
63554248Smsmith# define POWEROFF_DELAY 5000
63654248Smsmith#endif
63754248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
63854248Smsmith
63943436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
64043436Smsmith	&poweroff_delay, 0, "");
64143436Smsmith
64250107Smsmithstatic void
64350107Smsmithpoweroff_wait(void *junk, int howto)
64443436Smsmith{
64543436Smsmith	if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
64643436Smsmith		return;
64743436Smsmith	DELAY(poweroff_delay * 1000);
64843436Smsmith}
64955539Sluoqi
65055539Sluoqi/*
65155539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
65255539Sluoqi * points in their main loops prior to a system shutdown, so that they
65355539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
65455539Sluoqi * to cause sync to fail).  For each of these system processes, register
65555539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
65655539Sluoqi */
65755539Sluoqistatic int kproc_shutdown_wait = 60;
65855539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
65955539Sluoqi    &kproc_shutdown_wait, 0, "");
66055539Sluoqi
66155539Sluoqivoid
66270063Sjhbkproc_shutdown(void *arg, int howto)
66355539Sluoqi{
66455539Sluoqi	struct proc *p;
66555539Sluoqi	int error;
66655539Sluoqi
66755539Sluoqi	if (panicstr)
66855539Sluoqi		return;
66955539Sluoqi
67055539Sluoqi	p = (struct proc *)arg;
67155539Sluoqi	printf("Waiting (max %d seconds) for system process `%s' to stop...",
67255862Sluoqi	    kproc_shutdown_wait, p->p_comm);
67370063Sjhb	error = kthread_suspend(p, kproc_shutdown_wait * hz);
67455539Sluoqi
67555539Sluoqi	if (error == EWOULDBLOCK)
67655539Sluoqi		printf("timed out\n");
67755539Sluoqi	else
67855539Sluoqi		printf("stopped\n");
67955539Sluoqi}
680