kern_shutdown.c revision 131927
117658Sjulian/*-
217658Sjulian * Copyright (c) 1986, 1988, 1991, 1993
317658Sjulian *	The Regents of the University of California.  All rights reserved.
417658Sjulian * (c) UNIX System Laboratories, Inc.
517658Sjulian * All or some portions of this file are derived from material licensed
617658Sjulian * to the University of California by American Telephone and Telegraph
717658Sjulian * Co. or Unix System Laboratories, Inc. and are reproduced herein with
817658Sjulian * the permission of UNIX System Laboratories, Inc.
917658Sjulian *
1017658Sjulian * Redistribution and use in source and binary forms, with or without
1117658Sjulian * modification, are permitted provided that the following conditions
1217658Sjulian * are met:
1317658Sjulian * 1. Redistributions of source code must retain the above copyright
1417658Sjulian *    notice, this list of conditions and the following disclaimer.
1517658Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1617658Sjulian *    notice, this list of conditions and the following disclaimer in the
1717658Sjulian *    documentation and/or other materials provided with the distribution.
1817658Sjulian * 4. Neither the name of the University nor the names of its contributors
1917658Sjulian *    may be used to endorse or promote products derived from this software
2017658Sjulian *    without specific prior written permission.
2117658Sjulian *
2217658Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2317658Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2417658Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2517658Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2617658Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2717658Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2817658Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2917658Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3017658Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3117658Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3217658Sjulian * SUCH DAMAGE.
3317658Sjulian *
3417658Sjulian *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
3517658Sjulian */
3617658Sjulian
37116182Sobrien#include <sys/cdefs.h>
38116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_shutdown.c 131927 2004-07-10 21:36:01Z marcel $");
39116182Sobrien
40131927Smarcel#include "opt_kdb.h"
4133445Seivind#include "opt_hw_wdog.h"
42106024Srwatson#include "opt_mac.h"
4328976Sbde#include "opt_panic.h"
4428976Sbde#include "opt_show_busybufs.h"
4517658Sjulian
4617658Sjulian#include <sys/param.h>
4717658Sjulian#include <sys/systm.h>
4860041Sphk#include <sys/bio.h>
4931275Sbde#include <sys/buf.h>
5078767Sjhb#include <sys/conf.h>
5178767Sjhb#include <sys/cons.h>
5278767Sjhb#include <sys/eventhandler.h>
53131927Smarcel#include <sys/kdb.h>
5417658Sjulian#include <sys/kernel.h>
5555539Sluoqi#include <sys/kthread.h>
56106024Srwatson#include <sys/mac.h>
5789601Ssobomax#include <sys/malloc.h>
5821776Sbde#include <sys/mount.h>
5978767Sjhb#include <sys/proc.h>
6078767Sjhb#include <sys/reboot.h>
6178767Sjhb#include <sys/resourcevar.h>
6278767Sjhb#include <sys/smp.h>		/* smp_active */
6317658Sjulian#include <sys/sysctl.h>
6417658Sjulian#include <sys/sysproto.h>
6578767Sjhb#include <sys/vnode.h>
6617658Sjulian
67118990Smarcel#include <machine/cpu.h>
6894169Sphk#include <machine/pcb.h>
6991778Sjake#include <machine/smp.h>
7017658Sjulian
7117658Sjulian#include <sys/signalvar.h>
7217658Sjulian
7317658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
7417658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
7517658Sjulian#endif
7617658Sjulian
7717658Sjulian/*
7817658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
7917658Sjulian * ANSI and traditional C compilers.
8017658Sjulian */
8117658Sjulian#include <machine/stdarg.h>
8217658Sjulian
83131927Smarcel#ifdef KDB
84131927Smarcel#ifdef KDB_UNATTENDED
8542135Smsmithint debugger_on_panic = 0;
8617658Sjulian#else
8742135Smsmithint debugger_on_panic = 1;
8817658Sjulian#endif
8917658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
9046381Sbillf	&debugger_on_panic, 0, "Run debugger on kernel panic");
91103647Sjhb
92131927Smarcel#ifdef KDB_TRACE
93103647Sjhbint trace_on_panic = 1;
94103647Sjhb#else
95103647Sjhbint trace_on_panic = 0;
9617658Sjulian#endif
97103647SjhbSYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW,
98103647Sjhb	&trace_on_panic, 0, "Print stack trace on kernel panic");
99131927Smarcel#endif /* KDB */
10017658Sjulian
10185202Speterint sync_on_panic = 1;
10285202SpeterSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW,
10385202Speter	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
10485202Speter
10543436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
10643436Smsmith
10728000Sjulian#ifdef	HW_WDOG
10817658Sjulian/*
10927997Sjulian * If there is a hardware watchdog, point this at the function needed to
11027997Sjulian * hold it off.
11127997Sjulian * It's needed when the kernel needs to do some lengthy operations.
11227997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have
11327997Sjulian * your precious core-dump only half written because the wdog kicked in.
11427997Sjulian */
11527997Sjulianwatchdog_tickle_fn wdog_tickler = NULL;
11628000Sjulian#endif	/* HW_WDOG */
11727997Sjulian
11827997Sjulian/*
11917658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
12017658Sjulian * to indicate that the kernel has already called panic.
12117658Sjulian */
12217658Sjulianconst char *panicstr;
12317658Sjulian
12493496Sphkint dumping;				/* system is dumping */
12593496Sphkstatic struct dumperinfo dumper;	/* our selected dumper */
12667093Sps
127131927Smarcel/* Context information for dump-debuggers. */
128131927Smarcelstatic struct pcb dumppcb;		/* Registers. */
129131927Smarcelstatic lwpid_t dumptid;			/* Thread ID. */
130131927Smarcel
13165395Speterstatic void boot(int) __dead2;
13265395Speterstatic void poweroff_wait(void *, int);
13365395Speterstatic void shutdown_halt(void *junk, int howto);
13465395Speterstatic void shutdown_panic(void *junk, int howto);
13565395Speterstatic void shutdown_reset(void *junk, int howto);
13617658Sjulian
13750107Smsmith/* register various local shutdown events */
138110859Salfredstatic void
13950107Smsmithshutdown_conf(void *unused)
14050107Smsmith{
141110859Salfred
142110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
143110859Salfred	    SHUTDOWN_PRI_FIRST);
144110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
145110859Salfred	    SHUTDOWN_PRI_LAST + 100);
146110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
147110859Salfred	    SHUTDOWN_PRI_LAST + 100);
148110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
149110859Salfred	    SHUTDOWN_PRI_LAST + 200);
15050107Smsmith}
15148868Sphk
15250107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
15350107Smsmith
15417658Sjulian/*
15517658Sjulian * The system call that results in a reboot
15682749Sdillon *
15782749Sdillon * MPSAFE
15817658Sjulian */
15982749Sdillon/* ARGSUSED */
16017658Sjulianint
16183366Sjulianreboot(struct thread *td, struct reboot_args *uap)
16217658Sjulian{
16317658Sjulian	int error;
16417658Sjulian
165106024Srwatson	error = 0;
166106024Srwatson#ifdef MAC
167106024Srwatson	error = mac_check_system_reboot(td->td_ucred, uap->opt);
168106024Srwatson#endif
169106024Srwatson	if (error == 0)
170106024Srwatson		error = suser(td);
171106024Srwatson	if (error == 0) {
172106024Srwatson		mtx_lock(&Giant);
17382749Sdillon		boot(uap->opt);
174106024Srwatson		mtx_unlock(&Giant);
175106024Srwatson	}
17682749Sdillon	return (error);
17717658Sjulian}
17817658Sjulian
17917658Sjulian/*
18017658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
18117658Sjulian */
18265268Smsmithstatic int shutdown_howto = 0;
18365268Smsmith
18417658Sjulianvoid
18565268Smsmithshutdown_nice(int howto)
18617658Sjulian{
187110859Salfred
18865268Smsmith	shutdown_howto = howto;
189110859Salfred
19017658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
19117658Sjulian	if (initproc != NULL) {
19273913Sjhb		PROC_LOCK(initproc);
19317658Sjulian		psignal(initproc, SIGINT);
19473913Sjhb		PROC_UNLOCK(initproc);
19517658Sjulian	} else {
19617658Sjulian		/* No init(8) running, so simply reboot */
19717658Sjulian		boot(RB_NOSYNC);
19817658Sjulian	}
19917658Sjulian	return;
20017658Sjulian}
20117658Sjulianstatic int	waittime = -1;
20217658Sjulian
20354233Sphkstatic void
20465395Speterprint_uptime(void)
20554233Sphk{
20654233Sphk	int f;
20754233Sphk	struct timespec ts;
20854233Sphk
20954233Sphk	getnanouptime(&ts);
21054233Sphk	printf("Uptime: ");
21154233Sphk	f = 0;
21254233Sphk	if (ts.tv_sec >= 86400) {
21365764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
21454233Sphk		ts.tv_sec %= 86400;
21554233Sphk		f = 1;
21654233Sphk	}
21754233Sphk	if (f || ts.tv_sec >= 3600) {
21865764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
21954233Sphk		ts.tv_sec %= 3600;
22054233Sphk		f = 1;
22154233Sphk	}
22254233Sphk	if (f || ts.tv_sec >= 60) {
22365764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
22454233Sphk		ts.tv_sec %= 60;
22554233Sphk		f = 1;
22654233Sphk	}
22765764Sjhb	printf("%lds\n", (long)ts.tv_sec);
22854233Sphk}
22954233Sphk
23094169Sphkstatic void
23194169Sphkdoadump(void)
23294169Sphk{
233110859Salfred
23494169Sphk	savectx(&dumppcb);
235131927Smarcel	dumptid = curthread->td_tid;
23694169Sphk	dumping++;
23794169Sphk	dumpsys(&dumper);
23894169Sphk}
23994169Sphk
24017658Sjulian/*
24117658Sjulian *  Go through the rigmarole of shutting down..
24217658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
24317658Sjulian * anything machine dependant in it.
24417658Sjulian */
24531275Sbdestatic void
24665395Speterboot(int howto)
24717658Sjulian{
24817658Sjulian
24965268Smsmith	/* collect extra flags that shutdown_nice might have set */
25065268Smsmith	howto |= shutdown_howto;
25165268Smsmith
25282119Sjhb	/* We are out of the debugger now. */
253131927Smarcel	kdb_active = 0;
25482119Sjhb
25525164Speter#ifdef SMP
25665395Speter	if (smp_active)
25770861Sjake		printf("boot() called on cpu#%d\n", PCPU_GET(cpuid));
25825164Speter#endif
25927997Sjulian	/*
26027997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
26127997Sjulian	 */
26250107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
26327997Sjulian
26427997Sjulian	/*
26527997Sjulian	 * Now sync filesystems
26627997Sjulian	 */
26717658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
26817658Sjulian		register struct buf *bp;
26965707Sjasone		int iter, nbusy, pbusy;
270131481Sjhb#ifndef PREEMPTION
27165707Sjasone		int subiter;
272131481Sjhb#endif
27317658Sjulian
27417658Sjulian		waittime = 0;
275107036Salfred		printf("\nsyncing disks, buffers remaining... ");
27617658Sjulian
27790361Sjulian		sync(&thread0, NULL);
27817658Sjulian
27934266Sjulian		/*
28034266Sjulian		 * With soft updates, some buffers that are
28134266Sjulian		 * written will be remarked as dirty until other
28234266Sjulian		 * buffers are written.
28334266Sjulian		 */
28465707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
28517658Sjulian			nbusy = 0;
28617658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
28748225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
28848225Smckusick				    BUF_REFCNT(bp) > 0) {
28917658Sjulian					nbusy++;
29034266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
29134266Sjulian						== B_DELWRI) {
29234266Sjulian					/* bawrite(bp);*/
29334266Sjulian					nbusy++;
29417658Sjulian				}
29517658Sjulian			}
29617658Sjulian			if (nbusy == 0)
29717658Sjulian				break;
29817658Sjulian			printf("%d ", nbusy);
29965707Sjasone			if (nbusy < pbusy)
30065707Sjasone				iter = 0;
30165707Sjasone			pbusy = nbusy;
30290361Sjulian			sync(&thread0, NULL);
303131481Sjhb
304131481Sjhb#ifdef PREEMPTION
305131481Sjhb			/*
306131481Sjhb			 * Drop Giant and spin for a while to allow
307131481Sjhb			 * interrupt threads to run.
308131481Sjhb			 */
309131481Sjhb			DROP_GIANT();
31034266Sjulian			DELAY(50000 * iter);
311131481Sjhb			PICKUP_GIANT();
312131481Sjhb#else
313131481Sjhb			/*
314131481Sjhb			 * Drop Giant and context switch several times to
315131481Sjhb			 * allow interrupt threads to run.
316131481Sjhb			 */
317131481Sjhb			DROP_GIANT();
318131481Sjhb			for (subiter = 0; subiter < 50 * iter; subiter++) {
319131481Sjhb				mtx_lock_spin(&sched_lock);
320131481Sjhb				mi_switch(SW_VOL, NULL);
321131481Sjhb				mtx_unlock_spin(&sched_lock);
322131481Sjhb				DELAY(1000);
323131481Sjhb			}
324131481Sjhb			PICKUP_GIANT();
325131481Sjhb#endif
32617658Sjulian		}
32753023Sphk		printf("\n");
32841137Smsmith		/*
32941137Smsmith		 * Count only busy local buffers to prevent forcing
33041137Smsmith		 * a fsck if we're just a client of a wedged NFS server
33141137Smsmith		 */
33241137Smsmith		nbusy = 0;
33341137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
33448225Smckusick			if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
33548225Smckusick			    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
336130640Sphk				if (bp->b_dev == NULL) {
33753452Sphk					TAILQ_REMOVE(&mountlist,
33848225Smckusick					    bp->b_vp->v_mount, mnt_list);
33953023Sphk					continue;
34053023Sphk				}
34153023Sphk				nbusy++;
34253023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
34353023Sphk				printf(
344110585Sjeff			    "%d: dev:%s, flags:%0x, blkno:%ld, lblkno:%ld\n",
34553023Sphk				    nbusy, devtoname(bp->b_dev),
34653023Sphk				    bp->b_flags, (long)bp->b_blkno,
34753023Sphk				    (long)bp->b_lblkno);
34853023Sphk#endif
34946568Speter			}
35041137Smsmith		}
35117658Sjulian		if (nbusy) {
35217658Sjulian			/*
35317658Sjulian			 * Failed to sync all blocks. Indicate this and don't
35417658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
35517658Sjulian			 */
35653023Sphk			printf("giving up on %d buffers\n", nbusy);
35717658Sjulian			DELAY(5000000);	/* 5 seconds */
35817658Sjulian		} else {
35917658Sjulian			printf("done\n");
36017658Sjulian			/*
36117658Sjulian			 * Unmount filesystems
36217658Sjulian			 */
36317658Sjulian			if (panicstr == 0)
36417658Sjulian				vfs_unmountall();
36517658Sjulian		}
36639237Sgibbs		DELAY(100000);		/* wait for console output to finish */
36717658Sjulian	}
36827997Sjulian
36954233Sphk	print_uptime();
37054233Sphk
37127997Sjulian	/*
37227997Sjulian	 * Ok, now do things that assume all filesystem activity has
37327997Sjulian	 * been completed.
37427997Sjulian	 */
37550107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
37639237Sgibbs	splhigh();
37793496Sphk	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP &&
37894169Sphk	    !cold && dumper.dumper != NULL && !dumping)
37994169Sphk		doadump();
38039237Sgibbs
38139237Sgibbs	/* Now that we're going to really halt the system... */
38250107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
38339237Sgibbs
38450107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
38550107Smsmith	/* NOTREACHED */
38650107Smsmith}
38750107Smsmith
38850107Smsmith/*
38950107Smsmith * If the shutdown was a clean halt, behave accordingly.
39050107Smsmith */
39150107Smsmithstatic void
39250107Smsmithshutdown_halt(void *junk, int howto)
39350107Smsmith{
394110859Salfred
39517658Sjulian	if (howto & RB_HALT) {
39617658Sjulian		printf("\n");
39717658Sjulian		printf("The operating system has halted.\n");
39817658Sjulian		printf("Please press any key to reboot.\n\n");
39919274Sjulian		switch (cngetc()) {
40019274Sjulian		case -1:		/* No console, just die */
40119274Sjulian			cpu_halt();
40219274Sjulian			/* NOTREACHED */
40319274Sjulian		default:
40439237Sgibbs			howto &= ~RB_HALT;
40519274Sjulian			break;
40619274Sjulian		}
40750107Smsmith	}
40850107Smsmith}
40917658Sjulian
41050107Smsmith/*
41150107Smsmith * Check to see if the system paniced, pause and then reboot
41250107Smsmith * according to the specified delay.
41350107Smsmith */
41450107Smsmithstatic void
41550107Smsmithshutdown_panic(void *junk, int howto)
41650107Smsmith{
41750107Smsmith	int loop;
41850107Smsmith
41950107Smsmith	if (howto & RB_DUMP) {
42039237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
42139237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
42239237Sgibbs				printf("Automatic reboot in %d seconds - "
42339237Sgibbs				       "press a key on the console to abort\n",
42439237Sgibbs					PANIC_REBOOT_WAIT_TIME);
42539237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
42639237Sgibbs				     loop > 0; --loop) {
42739237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
42839237Sgibbs					/* Did user type a key? */
42939237Sgibbs					if (cncheckc() != -1)
43039237Sgibbs						break;
43117658Sjulian				}
43239237Sgibbs				if (!loop)
43350107Smsmith					return;
43417658Sjulian			}
43539237Sgibbs		} else { /* zero time specified - reboot NOW */
43650107Smsmith			return;
43717658Sjulian		}
43889522Snik		printf("--> Press a key on the console to reboot,\n");
43989522Snik		printf("--> or switch off the system now.\n");
44039237Sgibbs		cngetc();
44117658Sjulian	}
44250107Smsmith}
44350107Smsmith
44450107Smsmith/*
44550107Smsmith * Everything done, now reset
44650107Smsmith */
44750107Smsmithstatic void
44850107Smsmithshutdown_reset(void *junk, int howto)
44950107Smsmith{
450110859Salfred
45117658Sjulian	printf("Rebooting...\n");
45217658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
45317677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
45417658Sjulian	cpu_reset();
45550107Smsmith	/* NOTREACHED */ /* assuming reset worked */
45617658Sjulian}
45717658Sjulian
45875570Sjhb#ifdef SMP
459101155Sjhbstatic u_int panic_cpu = NOCPU;
46075570Sjhb#endif
46175570Sjhb
46217658Sjulian/*
46317658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
46417658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
46517658Sjulian * the disks as this often leads to recursive panics.
46682749Sdillon *
46782749Sdillon * MPSAFE
46817658Sjulian */
46917658Sjulianvoid
470130164Sphkpanic(const char *fmt, ...)
47117658Sjulian{
472100209Sgallatin	struct thread *td = curthread;
473103647Sjhb	int bootopt, newpanic;
47417658Sjulian	va_list ap;
47538874Sache	static char buf[256];
47617658Sjulian
47765557Sjasone#ifdef SMP
47882115Sjhb	/*
47982115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
480101155Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
481101155Sjhb	 * panic_cpu if we are spinning in case the panic on the first
48282115Sjhb	 * CPU is canceled.
48382115Sjhb	 */
484101155Sjhb	if (panic_cpu != PCPU_GET(cpuid))
485101155Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
486101155Sjhb		    PCPU_GET(cpuid)) == 0)
487101155Sjhb			while (panic_cpu != NOCPU)
488101155Sjhb				; /* nothing */
48965557Sjasone#endif
49065557Sjasone
49117658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
492103647Sjhb	newpanic = 0;
49317658Sjulian	if (panicstr)
49417658Sjulian		bootopt |= RB_NOSYNC;
495103647Sjhb	else {
49617658Sjulian		panicstr = fmt;
497103647Sjhb		newpanic = 1;
498103647Sjhb	}
49917658Sjulian
50017658Sjulian	va_start(ap, fmt);
501116398Siedowse	if (newpanic) {
502116398Siedowse		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
50338874Sache		panicstr = buf;
504130164Sphk		printf("panic: %s\n", buf);
505116398Siedowse	} else {
506116398Siedowse		printf("panic: ");
507116398Siedowse		vprintf(fmt, ap);
508130164Sphk		printf("\n");
509116398Siedowse	}
51017658Sjulian	va_end(ap);
51126100Sfsmp#ifdef SMP
51272091Sasmodai	/* two separate prints in case of an unmapped page and trap */
51370861Sjake	printf("cpuid = %d; ", PCPU_GET(cpuid));
51469335Sjhb#ifdef APIC_IO
51529128Speter	printf("lapic.id = %08x\n", lapic.id);
51699828Sjhb#else
51799828Sjhb	printf("\n");
51826100Sfsmp#endif
51969335Sjhb#endif
52017658Sjulian
521131927Smarcel#ifdef KDB
522103647Sjhb	if (newpanic && trace_on_panic)
523131927Smarcel		kdb_backtrace();
52417658Sjulian	if (debugger_on_panic)
525131927Smarcel		kdb_enter("panic");
52682223Sjhb#ifdef RESTARTABLE_PANICS
52782115Sjhb	/* See if the user aborted the panic, in which case we continue. */
52882115Sjhb	if (panicstr == NULL) {
52982115Sjhb#ifdef SMP
530101155Sjhb		atomic_store_rel_int(&panic_cpu, NOCPU);
53117658Sjulian#endif
53282115Sjhb		return;
53382115Sjhb	}
53482115Sjhb#endif
53582223Sjhb#endif
536113633Sjhb	mtx_lock_spin(&sched_lock);
537100209Sgallatin	td->td_flags |= TDF_INPANIC;
538113633Sjhb	mtx_unlock_spin(&sched_lock);
53985202Speter	if (!sync_on_panic)
54085202Speter		bootopt |= RB_NOSYNC;
54117658Sjulian	boot(bootopt);
54217658Sjulian}
54317658Sjulian
54417768Sjulian/*
54543436Smsmith * Support for poweroff delay.
54643436Smsmith */
54754248Smsmith#ifndef POWEROFF_DELAY
54854248Smsmith# define POWEROFF_DELAY 5000
54954248Smsmith#endif
55054248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
55154248Smsmith
55243436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
55343436Smsmith	&poweroff_delay, 0, "");
55443436Smsmith
555110859Salfredstatic void
55650107Smsmithpoweroff_wait(void *junk, int howto)
55743436Smsmith{
558110859Salfred
559110859Salfred	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
56043436Smsmith		return;
56143436Smsmith	DELAY(poweroff_delay * 1000);
56243436Smsmith}
56355539Sluoqi
56455539Sluoqi/*
56555539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
56655539Sluoqi * points in their main loops prior to a system shutdown, so that they
56755539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
56855539Sluoqi * to cause sync to fail).  For each of these system processes, register
56955539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
57055539Sluoqi */
57155539Sluoqistatic int kproc_shutdown_wait = 60;
57255539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
57355539Sluoqi    &kproc_shutdown_wait, 0, "");
57455539Sluoqi
57555539Sluoqivoid
57670063Sjhbkproc_shutdown(void *arg, int howto)
57755539Sluoqi{
57855539Sluoqi	struct proc *p;
57955539Sluoqi	int error;
58055539Sluoqi
58155539Sluoqi	if (panicstr)
58255539Sluoqi		return;
58355539Sluoqi
58455539Sluoqi	p = (struct proc *)arg;
58555539Sluoqi	printf("Waiting (max %d seconds) for system process `%s' to stop...",
58655862Sluoqi	    kproc_shutdown_wait, p->p_comm);
58770063Sjhb	error = kthread_suspend(p, kproc_shutdown_wait * hz);
58855539Sluoqi
58955539Sluoqi	if (error == EWOULDBLOCK)
59055539Sluoqi		printf("timed out\n");
59155539Sluoqi	else
59255539Sluoqi		printf("stopped\n");
59355539Sluoqi}
59493496Sphk
59593496Sphk/* Registration of dumpers */
59693496Sphkint
59793496Sphkset_dumper(struct dumperinfo *di)
59893496Sphk{
599110859Salfred
60093496Sphk	if (di == NULL) {
60193496Sphk		bzero(&dumper, sizeof dumper);
60293496Sphk		return (0);
60393496Sphk	}
60493496Sphk	if (dumper.dumper != NULL)
60593496Sphk		return (EBUSY);
60693496Sphk	dumper = *di;
60793496Sphk	return (0);
60893496Sphk}
60993496Sphk
610105531Stmm#if defined(__powerpc__)
61193496Sphkvoid
61293496Sphkdumpsys(struct dumperinfo *di __unused)
61393496Sphk{
61493496Sphk
61593496Sphk	printf("Kernel dumps not implemented on this architecture\n");
61693496Sphk}
61793496Sphk#endif
618