kern_shutdown.c revision 137263
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 137263 2004-11-05 18:29:10Z peter $");
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>
62137263Speter#include <sys/sched.h>
6378767Sjhb#include <sys/smp.h>		/* smp_active */
6417658Sjulian#include <sys/sysctl.h>
6517658Sjulian#include <sys/sysproto.h>
6678767Sjhb#include <sys/vnode.h>
6717658Sjulian
68118990Smarcel#include <machine/cpu.h>
6994169Sphk#include <machine/pcb.h>
7091778Sjake#include <machine/smp.h>
7117658Sjulian
7217658Sjulian#include <sys/signalvar.h>
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
84131927Smarcel#ifdef KDB
85131927Smarcel#ifdef KDB_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");
92103647Sjhb
93131927Smarcel#ifdef KDB_TRACE
94103647Sjhbint trace_on_panic = 1;
95103647Sjhb#else
96103647Sjhbint trace_on_panic = 0;
9717658Sjulian#endif
98103647SjhbSYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW,
99103647Sjhb	&trace_on_panic, 0, "Print stack trace on kernel panic");
100131927Smarcel#endif /* KDB */
10117658Sjulian
102132506Srwatsonint sync_on_panic = 0;
10385202SpeterSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW,
10485202Speter	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
10585202Speter
10643436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
10743436Smsmith
10817658Sjulian/*
10917658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
11017658Sjulian * to indicate that the kernel has already called panic.
11117658Sjulian */
11217658Sjulianconst char *panicstr;
11317658Sjulian
11493496Sphkint dumping;				/* system is dumping */
11593496Sphkstatic struct dumperinfo dumper;	/* our selected dumper */
11667093Sps
117131927Smarcel/* Context information for dump-debuggers. */
118131927Smarcelstatic struct pcb dumppcb;		/* Registers. */
119131927Smarcelstatic lwpid_t dumptid;			/* Thread ID. */
120131927Smarcel
12165395Speterstatic void boot(int) __dead2;
12265395Speterstatic void poweroff_wait(void *, int);
12365395Speterstatic void shutdown_halt(void *junk, int howto);
12465395Speterstatic void shutdown_panic(void *junk, int howto);
12565395Speterstatic void shutdown_reset(void *junk, int howto);
12617658Sjulian
12750107Smsmith/* register various local shutdown events */
128110859Salfredstatic void
12950107Smsmithshutdown_conf(void *unused)
13050107Smsmith{
131110859Salfred
132110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
133110859Salfred	    SHUTDOWN_PRI_FIRST);
134110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
135110859Salfred	    SHUTDOWN_PRI_LAST + 100);
136110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
137110859Salfred	    SHUTDOWN_PRI_LAST + 100);
138110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
139110859Salfred	    SHUTDOWN_PRI_LAST + 200);
14050107Smsmith}
14148868Sphk
14250107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
14350107Smsmith
14417658Sjulian/*
14517658Sjulian * The system call that results in a reboot
14682749Sdillon *
14782749Sdillon * MPSAFE
14817658Sjulian */
14982749Sdillon/* ARGSUSED */
15017658Sjulianint
15183366Sjulianreboot(struct thread *td, struct reboot_args *uap)
15217658Sjulian{
15317658Sjulian	int error;
15417658Sjulian
155106024Srwatson	error = 0;
156106024Srwatson#ifdef MAC
157106024Srwatson	error = mac_check_system_reboot(td->td_ucred, uap->opt);
158106024Srwatson#endif
159106024Srwatson	if (error == 0)
160106024Srwatson		error = suser(td);
161106024Srwatson	if (error == 0) {
162106024Srwatson		mtx_lock(&Giant);
16382749Sdillon		boot(uap->opt);
164106024Srwatson		mtx_unlock(&Giant);
165106024Srwatson	}
16682749Sdillon	return (error);
16717658Sjulian}
16817658Sjulian
16917658Sjulian/*
17017658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
17117658Sjulian */
17265268Smsmithstatic int shutdown_howto = 0;
17365268Smsmith
17417658Sjulianvoid
17565268Smsmithshutdown_nice(int howto)
17617658Sjulian{
177110859Salfred
17865268Smsmith	shutdown_howto = howto;
179110859Salfred
18017658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
18117658Sjulian	if (initproc != NULL) {
18273913Sjhb		PROC_LOCK(initproc);
18317658Sjulian		psignal(initproc, SIGINT);
18473913Sjhb		PROC_UNLOCK(initproc);
18517658Sjulian	} else {
18617658Sjulian		/* No init(8) running, so simply reboot */
18717658Sjulian		boot(RB_NOSYNC);
18817658Sjulian	}
18917658Sjulian	return;
19017658Sjulian}
19117658Sjulianstatic int	waittime = -1;
19217658Sjulian
19354233Sphkstatic void
19465395Speterprint_uptime(void)
19554233Sphk{
19654233Sphk	int f;
19754233Sphk	struct timespec ts;
19854233Sphk
19954233Sphk	getnanouptime(&ts);
20054233Sphk	printf("Uptime: ");
20154233Sphk	f = 0;
20254233Sphk	if (ts.tv_sec >= 86400) {
20365764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
20454233Sphk		ts.tv_sec %= 86400;
20554233Sphk		f = 1;
20654233Sphk	}
20754233Sphk	if (f || ts.tv_sec >= 3600) {
20865764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
20954233Sphk		ts.tv_sec %= 3600;
21054233Sphk		f = 1;
21154233Sphk	}
21254233Sphk	if (f || ts.tv_sec >= 60) {
21365764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
21454233Sphk		ts.tv_sec %= 60;
21554233Sphk		f = 1;
21654233Sphk	}
21765764Sjhb	printf("%lds\n", (long)ts.tv_sec);
21854233Sphk}
21954233Sphk
22094169Sphkstatic void
22194169Sphkdoadump(void)
22294169Sphk{
223110859Salfred
224132412Sjulian	/*
225132412Sjulian	 * Sometimes people have to call this from the kernel debugger.
226132412Sjulian	 * (if 'panic' can not dump)
227132412Sjulian	 * Give them a clue as to why they can't dump.
228132412Sjulian	 */
229132412Sjulian	if (dumper.dumper == NULL) {
230132413Sjulian		printf("Cannot dump. No dump device defined.\n");
231132412Sjulian		return;
232132412Sjulian	}
233132412Sjulian
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{
248133763Struckman	static int first_buf_printf = 1;
24917658Sjulian
250137263Speter#ifdef SMP
251137263Speter	/* Do all shutdown processing on cpu0 */
252137263Speter	mtx_lock_spin(&sched_lock);
253137263Speter	sched_bind(curthread, 0);
254137263Speter	mtx_unlock_spin(&sched_lock);
255137263Speter#endif
256137263Speter
25765268Smsmith	/* collect extra flags that shutdown_nice might have set */
25865268Smsmith	howto |= shutdown_howto;
25965268Smsmith
26082119Sjhb	/* We are out of the debugger now. */
261131927Smarcel	kdb_active = 0;
26282119Sjhb
26325164Speter#ifdef SMP
26465395Speter	if (smp_active)
26570861Sjake		printf("boot() called on cpu#%d\n", PCPU_GET(cpuid));
26625164Speter#endif
26727997Sjulian	/*
26827997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
26927997Sjulian	 */
27050107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
27127997Sjulian
27227997Sjulian	/*
27327997Sjulian	 * Now sync filesystems
27427997Sjulian	 */
27517658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
27617658Sjulian		register struct buf *bp;
27765707Sjasone		int iter, nbusy, pbusy;
278131481Sjhb#ifndef PREEMPTION
27965707Sjasone		int subiter;
280131481Sjhb#endif
28117658Sjulian
28217658Sjulian		waittime = 0;
28317658Sjulian
28490361Sjulian		sync(&thread0, NULL);
28517658Sjulian
28634266Sjulian		/*
28734266Sjulian		 * With soft updates, some buffers that are
28834266Sjulian		 * written will be remarked as dirty until other
28934266Sjulian		 * buffers are written.
29034266Sjulian		 */
29165707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
29217658Sjulian			nbusy = 0;
29317658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
29448225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
29548225Smckusick				    BUF_REFCNT(bp) > 0) {
29617658Sjulian					nbusy++;
29734266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
29834266Sjulian						== B_DELWRI) {
29934266Sjulian					/* bawrite(bp);*/
30034266Sjulian					nbusy++;
30117658Sjulian				}
30217658Sjulian			}
303133763Struckman			if (nbusy == 0) {
304133763Struckman				if (first_buf_printf)
305136115Sphk					printf("All buffers synced.");
30617658Sjulian				break;
307133763Struckman			}
308133763Struckman			if (first_buf_printf) {
309133763Struckman				printf("Syncing disks, buffers remaining... ");
310133763Struckman				first_buf_printf = 0;
311133763Struckman			}
31217658Sjulian			printf("%d ", nbusy);
31365707Sjasone			if (nbusy < pbusy)
31465707Sjasone				iter = 0;
31565707Sjasone			pbusy = nbusy;
31690361Sjulian			sync(&thread0, NULL);
317131481Sjhb
318131481Sjhb#ifdef PREEMPTION
319131481Sjhb			/*
320131481Sjhb			 * Drop Giant and spin for a while to allow
321131481Sjhb			 * interrupt threads to run.
322131481Sjhb			 */
323131481Sjhb			DROP_GIANT();
32434266Sjulian			DELAY(50000 * iter);
325131481Sjhb			PICKUP_GIANT();
326131481Sjhb#else
327131481Sjhb			/*
328131481Sjhb			 * Drop Giant and context switch several times to
329131481Sjhb			 * allow interrupt threads to run.
330131481Sjhb			 */
331131481Sjhb			DROP_GIANT();
332131481Sjhb			for (subiter = 0; subiter < 50 * iter; subiter++) {
333131481Sjhb				mtx_lock_spin(&sched_lock);
334131481Sjhb				mi_switch(SW_VOL, NULL);
335131481Sjhb				mtx_unlock_spin(&sched_lock);
336131481Sjhb				DELAY(1000);
337131481Sjhb			}
338131481Sjhb			PICKUP_GIANT();
339131481Sjhb#endif
34017658Sjulian		}
341133418Snjl		printf("\n");
34241137Smsmith		/*
34341137Smsmith		 * Count only busy local buffers to prevent forcing
34441137Smsmith		 * a fsck if we're just a client of a wedged NFS server
34541137Smsmith		 */
34641137Smsmith		nbusy = 0;
34741137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
34848225Smckusick			if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
34948225Smckusick			    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
350137186Sphk#if 0
351137186Sphk/* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
352130640Sphk				if (bp->b_dev == NULL) {
35353452Sphk					TAILQ_REMOVE(&mountlist,
35448225Smckusick					    bp->b_vp->v_mount, mnt_list);
35553023Sphk					continue;
35653023Sphk				}
357137186Sphk#endif
35853023Sphk				nbusy++;
35953023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
36053023Sphk				printf(
361137186Sphk			    "%d: bufobj:%p, flags:%0x, blkno:%ld, lblkno:%ld\n",
362137186Sphk				    nbusy, bp->b_bufobj,
36353023Sphk				    bp->b_flags, (long)bp->b_blkno,
36453023Sphk				    (long)bp->b_lblkno);
36553023Sphk#endif
36646568Speter			}
36741137Smsmith		}
36817658Sjulian		if (nbusy) {
36917658Sjulian			/*
37017658Sjulian			 * Failed to sync all blocks. Indicate this and don't
37117658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
37217658Sjulian			 */
373133763Struckman			printf("Giving up on %d buffers\n", nbusy);
37417658Sjulian			DELAY(5000000);	/* 5 seconds */
37517658Sjulian		} else {
376133763Struckman			if (!first_buf_printf)
377133763Struckman				printf("Final sync complete\n");
37817658Sjulian			/*
37917658Sjulian			 * Unmount filesystems
38017658Sjulian			 */
38117658Sjulian			if (panicstr == 0)
38217658Sjulian				vfs_unmountall();
38317658Sjulian		}
38439237Sgibbs		DELAY(100000);		/* wait for console output to finish */
38517658Sjulian	}
38627997Sjulian
38754233Sphk	print_uptime();
38854233Sphk
38927997Sjulian	/*
39027997Sjulian	 * Ok, now do things that assume all filesystem activity has
39127997Sjulian	 * been completed.
39227997Sjulian	 */
39350107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
39439237Sgibbs	splhigh();
395132412Sjulian	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
39694169Sphk		doadump();
39739237Sgibbs
39839237Sgibbs	/* Now that we're going to really halt the system... */
39950107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
40039237Sgibbs
40150107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
40250107Smsmith	/* NOTREACHED */
40350107Smsmith}
40450107Smsmith
40550107Smsmith/*
40650107Smsmith * If the shutdown was a clean halt, behave accordingly.
40750107Smsmith */
40850107Smsmithstatic void
40950107Smsmithshutdown_halt(void *junk, int howto)
41050107Smsmith{
411110859Salfred
41217658Sjulian	if (howto & RB_HALT) {
41317658Sjulian		printf("\n");
41417658Sjulian		printf("The operating system has halted.\n");
41517658Sjulian		printf("Please press any key to reboot.\n\n");
41619274Sjulian		switch (cngetc()) {
41719274Sjulian		case -1:		/* No console, just die */
41819274Sjulian			cpu_halt();
41919274Sjulian			/* NOTREACHED */
42019274Sjulian		default:
42139237Sgibbs			howto &= ~RB_HALT;
42219274Sjulian			break;
42319274Sjulian		}
42450107Smsmith	}
42550107Smsmith}
42617658Sjulian
42750107Smsmith/*
42850107Smsmith * Check to see if the system paniced, pause and then reboot
42950107Smsmith * according to the specified delay.
43050107Smsmith */
43150107Smsmithstatic void
43250107Smsmithshutdown_panic(void *junk, int howto)
43350107Smsmith{
43450107Smsmith	int loop;
43550107Smsmith
43650107Smsmith	if (howto & RB_DUMP) {
43739237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
43839237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
43939237Sgibbs				printf("Automatic reboot in %d seconds - "
44039237Sgibbs				       "press a key on the console to abort\n",
44139237Sgibbs					PANIC_REBOOT_WAIT_TIME);
44239237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
44339237Sgibbs				     loop > 0; --loop) {
44439237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
44539237Sgibbs					/* Did user type a key? */
44639237Sgibbs					if (cncheckc() != -1)
44739237Sgibbs						break;
44817658Sjulian				}
44939237Sgibbs				if (!loop)
45050107Smsmith					return;
45117658Sjulian			}
45239237Sgibbs		} else { /* zero time specified - reboot NOW */
45350107Smsmith			return;
45417658Sjulian		}
45589522Snik		printf("--> Press a key on the console to reboot,\n");
45689522Snik		printf("--> or switch off the system now.\n");
45739237Sgibbs		cngetc();
45817658Sjulian	}
45950107Smsmith}
46050107Smsmith
46150107Smsmith/*
46250107Smsmith * Everything done, now reset
46350107Smsmith */
46450107Smsmithstatic void
46550107Smsmithshutdown_reset(void *junk, int howto)
46650107Smsmith{
467110859Salfred
46817658Sjulian	printf("Rebooting...\n");
46917658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
47017677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
47117658Sjulian	cpu_reset();
47250107Smsmith	/* NOTREACHED */ /* assuming reset worked */
47317658Sjulian}
47417658Sjulian
47575570Sjhb#ifdef SMP
476101155Sjhbstatic u_int panic_cpu = NOCPU;
47775570Sjhb#endif
47875570Sjhb
47917658Sjulian/*
48017658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
48117658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
48217658Sjulian * the disks as this often leads to recursive panics.
48382749Sdillon *
48482749Sdillon * MPSAFE
48517658Sjulian */
48617658Sjulianvoid
487130164Sphkpanic(const char *fmt, ...)
48817658Sjulian{
489100209Sgallatin	struct thread *td = curthread;
490103647Sjhb	int bootopt, newpanic;
49117658Sjulian	va_list ap;
49238874Sache	static char buf[256];
49317658Sjulian
49465557Sjasone#ifdef SMP
49582115Sjhb	/*
49682115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
497101155Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
498101155Sjhb	 * panic_cpu if we are spinning in case the panic on the first
49982115Sjhb	 * CPU is canceled.
50082115Sjhb	 */
501101155Sjhb	if (panic_cpu != PCPU_GET(cpuid))
502101155Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
503101155Sjhb		    PCPU_GET(cpuid)) == 0)
504101155Sjhb			while (panic_cpu != NOCPU)
505101155Sjhb				; /* nothing */
50665557Sjasone#endif
50765557Sjasone
50817658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
509103647Sjhb	newpanic = 0;
51017658Sjulian	if (panicstr)
51117658Sjulian		bootopt |= RB_NOSYNC;
512103647Sjhb	else {
51317658Sjulian		panicstr = fmt;
514103647Sjhb		newpanic = 1;
515103647Sjhb	}
51617658Sjulian
51717658Sjulian	va_start(ap, fmt);
518116398Siedowse	if (newpanic) {
519116398Siedowse		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
52038874Sache		panicstr = buf;
521130164Sphk		printf("panic: %s\n", buf);
522116398Siedowse	} else {
523116398Siedowse		printf("panic: ");
524116398Siedowse		vprintf(fmt, ap);
525130164Sphk		printf("\n");
526116398Siedowse	}
52717658Sjulian	va_end(ap);
52826100Sfsmp#ifdef SMP
529134089Sjhb	printf("cpuid = %d\n", PCPU_GET(cpuid));
53026100Sfsmp#endif
53117658Sjulian
532131927Smarcel#ifdef KDB
533103647Sjhb	if (newpanic && trace_on_panic)
534131927Smarcel		kdb_backtrace();
53517658Sjulian	if (debugger_on_panic)
536131927Smarcel		kdb_enter("panic");
53782223Sjhb#ifdef RESTARTABLE_PANICS
53882115Sjhb	/* See if the user aborted the panic, in which case we continue. */
53982115Sjhb	if (panicstr == NULL) {
54082115Sjhb#ifdef SMP
541101155Sjhb		atomic_store_rel_int(&panic_cpu, NOCPU);
54217658Sjulian#endif
54382115Sjhb		return;
54482115Sjhb	}
54582115Sjhb#endif
54682223Sjhb#endif
547113633Sjhb	mtx_lock_spin(&sched_lock);
548100209Sgallatin	td->td_flags |= TDF_INPANIC;
549113633Sjhb	mtx_unlock_spin(&sched_lock);
55085202Speter	if (!sync_on_panic)
55185202Speter		bootopt |= RB_NOSYNC;
55217658Sjulian	boot(bootopt);
55317658Sjulian}
55417658Sjulian
55517768Sjulian/*
55643436Smsmith * Support for poweroff delay.
55743436Smsmith */
55854248Smsmith#ifndef POWEROFF_DELAY
55954248Smsmith# define POWEROFF_DELAY 5000
56054248Smsmith#endif
56154248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
56254248Smsmith
56343436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
56443436Smsmith	&poweroff_delay, 0, "");
56543436Smsmith
566110859Salfredstatic void
56750107Smsmithpoweroff_wait(void *junk, int howto)
56843436Smsmith{
569110859Salfred
570110859Salfred	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
57143436Smsmith		return;
57243436Smsmith	DELAY(poweroff_delay * 1000);
57343436Smsmith}
57455539Sluoqi
57555539Sluoqi/*
57655539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
57755539Sluoqi * points in their main loops prior to a system shutdown, so that they
57855539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
57955539Sluoqi * to cause sync to fail).  For each of these system processes, register
58055539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
58155539Sluoqi */
58255539Sluoqistatic int kproc_shutdown_wait = 60;
58355539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
58455539Sluoqi    &kproc_shutdown_wait, 0, "");
58555539Sluoqi
58655539Sluoqivoid
58770063Sjhbkproc_shutdown(void *arg, int howto)
58855539Sluoqi{
58955539Sluoqi	struct proc *p;
590132177Salfred	char procname[MAXCOMLEN + 1];
59155539Sluoqi	int error;
59255539Sluoqi
59355539Sluoqi	if (panicstr)
59455539Sluoqi		return;
59555539Sluoqi
59655539Sluoqi	p = (struct proc *)arg;
597132177Salfred	strlcpy(procname, p->p_comm, sizeof(procname));
598132866Snjl	printf("Waiting (max %d seconds) for system process `%s' to stop...",
599132177Salfred	    kproc_shutdown_wait, procname);
60070063Sjhb	error = kthread_suspend(p, kproc_shutdown_wait * hz);
60155539Sluoqi
60255539Sluoqi	if (error == EWOULDBLOCK)
603132866Snjl		printf("timed out\n");
60455539Sluoqi	else
605132866Snjl		printf("done\n");
60655539Sluoqi}
60793496Sphk
60893496Sphk/* Registration of dumpers */
60993496Sphkint
61093496Sphkset_dumper(struct dumperinfo *di)
61193496Sphk{
612110859Salfred
61393496Sphk	if (di == NULL) {
61493496Sphk		bzero(&dumper, sizeof dumper);
61593496Sphk		return (0);
61693496Sphk	}
61793496Sphk	if (dumper.dumper != NULL)
61893496Sphk		return (EBUSY);
61993496Sphk	dumper = *di;
62093496Sphk	return (0);
62193496Sphk}
62293496Sphk
623105531Stmm#if defined(__powerpc__)
62493496Sphkvoid
62593496Sphkdumpsys(struct dumperinfo *di __unused)
62693496Sphk{
62793496Sphk
62893496Sphk	printf("Kernel dumps not implemented on this architecture\n");
62993496Sphk}
63093496Sphk#endif
631