kern_shutdown.c revision 134649
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 134649 2004-09-02 18:59:15Z scottl $");
39116182Sobrien
40131927Smarcel#include "opt_kdb.h"
41106024Srwatson#include "opt_mac.h"
4228976Sbde#include "opt_panic.h"
4328976Sbde#include "opt_show_busybufs.h"
44134649Sscottl#include "opt_sched.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
101132506Srwatsonint sync_on_panic = 0;
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
10717658Sjulian/*
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
11393496Sphkint dumping;				/* system is dumping */
11493496Sphkstatic struct dumperinfo dumper;	/* our selected dumper */
11567093Sps
116131927Smarcel/* Context information for dump-debuggers. */
117131927Smarcelstatic struct pcb dumppcb;		/* Registers. */
118131927Smarcelstatic lwpid_t dumptid;			/* Thread ID. */
119131927Smarcel
12065395Speterstatic void boot(int) __dead2;
12165395Speterstatic void poweroff_wait(void *, int);
12265395Speterstatic void shutdown_halt(void *junk, int howto);
12365395Speterstatic void shutdown_panic(void *junk, int howto);
12465395Speterstatic void shutdown_reset(void *junk, int howto);
12517658Sjulian
12650107Smsmith/* register various local shutdown events */
127110859Salfredstatic void
12850107Smsmithshutdown_conf(void *unused)
12950107Smsmith{
130110859Salfred
131110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
132110859Salfred	    SHUTDOWN_PRI_FIRST);
133110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
134110859Salfred	    SHUTDOWN_PRI_LAST + 100);
135110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
136110859Salfred	    SHUTDOWN_PRI_LAST + 100);
137110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
138110859Salfred	    SHUTDOWN_PRI_LAST + 200);
13950107Smsmith}
14048868Sphk
14150107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
14250107Smsmith
14317658Sjulian/*
14417658Sjulian * The system call that results in a reboot
14582749Sdillon *
14682749Sdillon * MPSAFE
14717658Sjulian */
14882749Sdillon/* ARGSUSED */
14917658Sjulianint
15083366Sjulianreboot(struct thread *td, struct reboot_args *uap)
15117658Sjulian{
15217658Sjulian	int error;
15317658Sjulian
154106024Srwatson	error = 0;
155106024Srwatson#ifdef MAC
156106024Srwatson	error = mac_check_system_reboot(td->td_ucred, uap->opt);
157106024Srwatson#endif
158106024Srwatson	if (error == 0)
159106024Srwatson		error = suser(td);
160106024Srwatson	if (error == 0) {
161106024Srwatson		mtx_lock(&Giant);
16282749Sdillon		boot(uap->opt);
163106024Srwatson		mtx_unlock(&Giant);
164106024Srwatson	}
16582749Sdillon	return (error);
16617658Sjulian}
16717658Sjulian
16817658Sjulian/*
16917658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
17017658Sjulian */
17165268Smsmithstatic int shutdown_howto = 0;
17265268Smsmith
17317658Sjulianvoid
17465268Smsmithshutdown_nice(int howto)
17517658Sjulian{
176110859Salfred
17765268Smsmith	shutdown_howto = howto;
178110859Salfred
17917658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
18017658Sjulian	if (initproc != NULL) {
18173913Sjhb		PROC_LOCK(initproc);
18217658Sjulian		psignal(initproc, SIGINT);
18373913Sjhb		PROC_UNLOCK(initproc);
18417658Sjulian	} else {
18517658Sjulian		/* No init(8) running, so simply reboot */
18617658Sjulian		boot(RB_NOSYNC);
18717658Sjulian	}
18817658Sjulian	return;
18917658Sjulian}
19017658Sjulianstatic int	waittime = -1;
19117658Sjulian
19254233Sphkstatic void
19365395Speterprint_uptime(void)
19454233Sphk{
19554233Sphk	int f;
19654233Sphk	struct timespec ts;
19754233Sphk
19854233Sphk	getnanouptime(&ts);
19954233Sphk	printf("Uptime: ");
20054233Sphk	f = 0;
20154233Sphk	if (ts.tv_sec >= 86400) {
20265764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
20354233Sphk		ts.tv_sec %= 86400;
20454233Sphk		f = 1;
20554233Sphk	}
20654233Sphk	if (f || ts.tv_sec >= 3600) {
20765764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
20854233Sphk		ts.tv_sec %= 3600;
20954233Sphk		f = 1;
21054233Sphk	}
21154233Sphk	if (f || ts.tv_sec >= 60) {
21265764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
21354233Sphk		ts.tv_sec %= 60;
21454233Sphk		f = 1;
21554233Sphk	}
21665764Sjhb	printf("%lds\n", (long)ts.tv_sec);
21754233Sphk}
21854233Sphk
21994169Sphkstatic void
22094169Sphkdoadump(void)
22194169Sphk{
222110859Salfred
223132412Sjulian	/*
224132412Sjulian	 * Sometimes people have to call this from the kernel debugger.
225132412Sjulian	 * (if 'panic' can not dump)
226132412Sjulian	 * Give them a clue as to why they can't dump.
227132412Sjulian	 */
228132412Sjulian	if (dumper.dumper == NULL) {
229132413Sjulian		printf("Cannot dump. No dump device defined.\n");
230132412Sjulian		return;
231132412Sjulian	}
232132412Sjulian
23394169Sphk	savectx(&dumppcb);
234131927Smarcel	dumptid = curthread->td_tid;
23594169Sphk	dumping++;
23694169Sphk	dumpsys(&dumper);
23794169Sphk}
23894169Sphk
23917658Sjulian/*
24017658Sjulian *  Go through the rigmarole of shutting down..
24117658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
24217658Sjulian * anything machine dependant in it.
24317658Sjulian */
24431275Sbdestatic void
24565395Speterboot(int howto)
24617658Sjulian{
247133763Struckman	static int first_buf_printf = 1;
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;
27517658Sjulian
27690361Sjulian		sync(&thread0, NULL);
27717658Sjulian
27834266Sjulian		/*
27934266Sjulian		 * With soft updates, some buffers that are
28034266Sjulian		 * written will be remarked as dirty until other
28134266Sjulian		 * buffers are written.
28234266Sjulian		 */
28365707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
28417658Sjulian			nbusy = 0;
28517658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
28648225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
28748225Smckusick				    BUF_REFCNT(bp) > 0) {
28817658Sjulian					nbusy++;
28934266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
29034266Sjulian						== B_DELWRI) {
29134266Sjulian					/* bawrite(bp);*/
29234266Sjulian					nbusy++;
29317658Sjulian				}
29417658Sjulian			}
295133763Struckman			if (nbusy == 0) {
296133763Struckman				if (first_buf_printf)
297133763Struckman					printf("No buffers busy after final sync");
29817658Sjulian				break;
299133763Struckman			}
300133763Struckman			if (first_buf_printf) {
301133763Struckman				printf("Syncing disks, buffers remaining... ");
302133763Struckman				first_buf_printf = 0;
303133763Struckman			}
30417658Sjulian			printf("%d ", nbusy);
30565707Sjasone			if (nbusy < pbusy)
30665707Sjasone				iter = 0;
30765707Sjasone			pbusy = nbusy;
30890361Sjulian			sync(&thread0, NULL);
309131481Sjhb
310131481Sjhb#ifdef PREEMPTION
311131481Sjhb			/*
312131481Sjhb			 * Drop Giant and spin for a while to allow
313131481Sjhb			 * interrupt threads to run.
314131481Sjhb			 */
315131481Sjhb			DROP_GIANT();
31634266Sjulian			DELAY(50000 * iter);
317131481Sjhb			PICKUP_GIANT();
318131481Sjhb#else
319131481Sjhb			/*
320131481Sjhb			 * Drop Giant and context switch several times to
321131481Sjhb			 * allow interrupt threads to run.
322131481Sjhb			 */
323131481Sjhb			DROP_GIANT();
324131481Sjhb			for (subiter = 0; subiter < 50 * iter; subiter++) {
325131481Sjhb				mtx_lock_spin(&sched_lock);
326131481Sjhb				mi_switch(SW_VOL, NULL);
327131481Sjhb				mtx_unlock_spin(&sched_lock);
328131481Sjhb				DELAY(1000);
329131481Sjhb			}
330131481Sjhb			PICKUP_GIANT();
331131481Sjhb#endif
33217658Sjulian		}
333133418Snjl		printf("\n");
33441137Smsmith		/*
33541137Smsmith		 * Count only busy local buffers to prevent forcing
33641137Smsmith		 * a fsck if we're just a client of a wedged NFS server
33741137Smsmith		 */
33841137Smsmith		nbusy = 0;
33941137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
34048225Smckusick			if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
34148225Smckusick			    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
342130640Sphk				if (bp->b_dev == NULL) {
34353452Sphk					TAILQ_REMOVE(&mountlist,
34448225Smckusick					    bp->b_vp->v_mount, mnt_list);
34553023Sphk					continue;
34653023Sphk				}
34753023Sphk				nbusy++;
34853023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
34953023Sphk				printf(
350110585Sjeff			    "%d: dev:%s, flags:%0x, blkno:%ld, lblkno:%ld\n",
35153023Sphk				    nbusy, devtoname(bp->b_dev),
35253023Sphk				    bp->b_flags, (long)bp->b_blkno,
35353023Sphk				    (long)bp->b_lblkno);
35453023Sphk#endif
35546568Speter			}
35641137Smsmith		}
35717658Sjulian		if (nbusy) {
35817658Sjulian			/*
35917658Sjulian			 * Failed to sync all blocks. Indicate this and don't
36017658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
36117658Sjulian			 */
362133763Struckman			printf("Giving up on %d buffers\n", nbusy);
36317658Sjulian			DELAY(5000000);	/* 5 seconds */
36417658Sjulian		} else {
365133763Struckman			if (!first_buf_printf)
366133763Struckman				printf("Final sync complete\n");
36717658Sjulian			/*
36817658Sjulian			 * Unmount filesystems
36917658Sjulian			 */
37017658Sjulian			if (panicstr == 0)
37117658Sjulian				vfs_unmountall();
37217658Sjulian		}
37339237Sgibbs		DELAY(100000);		/* wait for console output to finish */
37417658Sjulian	}
37527997Sjulian
37654233Sphk	print_uptime();
37754233Sphk
37827997Sjulian	/*
37927997Sjulian	 * Ok, now do things that assume all filesystem activity has
38027997Sjulian	 * been completed.
38127997Sjulian	 */
38250107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
38339237Sgibbs	splhigh();
384132412Sjulian	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
38594169Sphk		doadump();
38639237Sgibbs
38739237Sgibbs	/* Now that we're going to really halt the system... */
38850107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
38939237Sgibbs
39050107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
39150107Smsmith	/* NOTREACHED */
39250107Smsmith}
39350107Smsmith
39450107Smsmith/*
39550107Smsmith * If the shutdown was a clean halt, behave accordingly.
39650107Smsmith */
39750107Smsmithstatic void
39850107Smsmithshutdown_halt(void *junk, int howto)
39950107Smsmith{
400110859Salfred
40117658Sjulian	if (howto & RB_HALT) {
40217658Sjulian		printf("\n");
40317658Sjulian		printf("The operating system has halted.\n");
40417658Sjulian		printf("Please press any key to reboot.\n\n");
40519274Sjulian		switch (cngetc()) {
40619274Sjulian		case -1:		/* No console, just die */
40719274Sjulian			cpu_halt();
40819274Sjulian			/* NOTREACHED */
40919274Sjulian		default:
41039237Sgibbs			howto &= ~RB_HALT;
41119274Sjulian			break;
41219274Sjulian		}
41350107Smsmith	}
41450107Smsmith}
41517658Sjulian
41650107Smsmith/*
41750107Smsmith * Check to see if the system paniced, pause and then reboot
41850107Smsmith * according to the specified delay.
41950107Smsmith */
42050107Smsmithstatic void
42150107Smsmithshutdown_panic(void *junk, int howto)
42250107Smsmith{
42350107Smsmith	int loop;
42450107Smsmith
42550107Smsmith	if (howto & RB_DUMP) {
42639237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
42739237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
42839237Sgibbs				printf("Automatic reboot in %d seconds - "
42939237Sgibbs				       "press a key on the console to abort\n",
43039237Sgibbs					PANIC_REBOOT_WAIT_TIME);
43139237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
43239237Sgibbs				     loop > 0; --loop) {
43339237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
43439237Sgibbs					/* Did user type a key? */
43539237Sgibbs					if (cncheckc() != -1)
43639237Sgibbs						break;
43717658Sjulian				}
43839237Sgibbs				if (!loop)
43950107Smsmith					return;
44017658Sjulian			}
44139237Sgibbs		} else { /* zero time specified - reboot NOW */
44250107Smsmith			return;
44317658Sjulian		}
44489522Snik		printf("--> Press a key on the console to reboot,\n");
44589522Snik		printf("--> or switch off the system now.\n");
44639237Sgibbs		cngetc();
44717658Sjulian	}
44850107Smsmith}
44950107Smsmith
45050107Smsmith/*
45150107Smsmith * Everything done, now reset
45250107Smsmith */
45350107Smsmithstatic void
45450107Smsmithshutdown_reset(void *junk, int howto)
45550107Smsmith{
456110859Salfred
45717658Sjulian	printf("Rebooting...\n");
45817658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
45917677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
46017658Sjulian	cpu_reset();
46150107Smsmith	/* NOTREACHED */ /* assuming reset worked */
46217658Sjulian}
46317658Sjulian
46475570Sjhb#ifdef SMP
465101155Sjhbstatic u_int panic_cpu = NOCPU;
46675570Sjhb#endif
46775570Sjhb
46817658Sjulian/*
46917658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
47017658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
47117658Sjulian * the disks as this often leads to recursive panics.
47282749Sdillon *
47382749Sdillon * MPSAFE
47417658Sjulian */
47517658Sjulianvoid
476130164Sphkpanic(const char *fmt, ...)
47717658Sjulian{
478100209Sgallatin	struct thread *td = curthread;
479103647Sjhb	int bootopt, newpanic;
48017658Sjulian	va_list ap;
48138874Sache	static char buf[256];
48217658Sjulian
48365557Sjasone#ifdef SMP
48482115Sjhb	/*
48582115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
486101155Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
487101155Sjhb	 * panic_cpu if we are spinning in case the panic on the first
48882115Sjhb	 * CPU is canceled.
48982115Sjhb	 */
490101155Sjhb	if (panic_cpu != PCPU_GET(cpuid))
491101155Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
492101155Sjhb		    PCPU_GET(cpuid)) == 0)
493101155Sjhb			while (panic_cpu != NOCPU)
494101155Sjhb				; /* nothing */
49565557Sjasone#endif
49665557Sjasone
49717658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
498103647Sjhb	newpanic = 0;
49917658Sjulian	if (panicstr)
50017658Sjulian		bootopt |= RB_NOSYNC;
501103647Sjhb	else {
50217658Sjulian		panicstr = fmt;
503103647Sjhb		newpanic = 1;
504103647Sjhb	}
50517658Sjulian
50617658Sjulian	va_start(ap, fmt);
507116398Siedowse	if (newpanic) {
508116398Siedowse		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
50938874Sache		panicstr = buf;
510130164Sphk		printf("panic: %s\n", buf);
511116398Siedowse	} else {
512116398Siedowse		printf("panic: ");
513116398Siedowse		vprintf(fmt, ap);
514130164Sphk		printf("\n");
515116398Siedowse	}
51617658Sjulian	va_end(ap);
51726100Sfsmp#ifdef SMP
518134089Sjhb	printf("cpuid = %d\n", PCPU_GET(cpuid));
51926100Sfsmp#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;
579132177Salfred	char procname[MAXCOMLEN + 1];
58055539Sluoqi	int error;
58155539Sluoqi
58255539Sluoqi	if (panicstr)
58355539Sluoqi		return;
58455539Sluoqi
58555539Sluoqi	p = (struct proc *)arg;
586132177Salfred	strlcpy(procname, p->p_comm, sizeof(procname));
587132866Snjl	printf("Waiting (max %d seconds) for system process `%s' to stop...",
588132177Salfred	    kproc_shutdown_wait, procname);
58970063Sjhb	error = kthread_suspend(p, kproc_shutdown_wait * hz);
59055539Sluoqi
59155539Sluoqi	if (error == EWOULDBLOCK)
592132866Snjl		printf("timed out\n");
59355539Sluoqi	else
594132866Snjl		printf("done\n");
59555539Sluoqi}
59693496Sphk
59793496Sphk/* Registration of dumpers */
59893496Sphkint
59993496Sphkset_dumper(struct dumperinfo *di)
60093496Sphk{
601110859Salfred
60293496Sphk	if (di == NULL) {
60393496Sphk		bzero(&dumper, sizeof dumper);
60493496Sphk		return (0);
60593496Sphk	}
60693496Sphk	if (dumper.dumper != NULL)
60793496Sphk		return (EBUSY);
60893496Sphk	dumper = *di;
60993496Sphk	return (0);
61093496Sphk}
61193496Sphk
612105531Stmm#if defined(__powerpc__)
61393496Sphkvoid
61493496Sphkdumpsys(struct dumperinfo *di __unused)
61593496Sphk{
61693496Sphk
61793496Sphk	printf("Kernel dumps not implemented on this architecture\n");
61893496Sphk}
61993496Sphk#endif
620