kern_shutdown.c revision 174898
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 174898 2007-12-25 17:52:02Z rwatson $");
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>
5689601Ssobomax#include <sys/malloc.h>
5721776Sbde#include <sys/mount.h>
58164033Srwatson#include <sys/priv.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>
6617658Sjulian
67118990Smarcel#include <machine/cpu.h>
6894169Sphk#include <machine/pcb.h>
6991778Sjake#include <machine/smp.h>
7017658Sjulian
71163606Srwatson#include <security/mac/mac_framework.h>
72163606Srwatson
73157628Spjd#include <vm/vm.h>
74157628Spjd#include <vm/vm_object.h>
75157628Spjd#include <vm/vm_page.h>
76157628Spjd#include <vm/vm_pager.h>
77157628Spjd#include <vm/swap_pager.h>
78157628Spjd
7917658Sjulian#include <sys/signalvar.h>
8017658Sjulian
8117658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
8217658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
8317658Sjulian#endif
8417658Sjulian
8517658Sjulian/*
8617658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
8717658Sjulian * ANSI and traditional C compilers.
8817658Sjulian */
8917658Sjulian#include <machine/stdarg.h>
9017658Sjulian
91131927Smarcel#ifdef KDB
92131927Smarcel#ifdef KDB_UNATTENDED
9342135Smsmithint debugger_on_panic = 0;
9417658Sjulian#else
9542135Smsmithint debugger_on_panic = 1;
9617658Sjulian#endif
9717658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
9846381Sbillf	&debugger_on_panic, 0, "Run debugger on kernel panic");
99103647Sjhb
100131927Smarcel#ifdef KDB_TRACE
101103647Sjhbint trace_on_panic = 1;
102103647Sjhb#else
103103647Sjhbint trace_on_panic = 0;
10417658Sjulian#endif
105103647SjhbSYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW,
106103647Sjhb	&trace_on_panic, 0, "Print stack trace on kernel panic");
107131927Smarcel#endif /* KDB */
10817658Sjulian
109132506Srwatsonint sync_on_panic = 0;
11085202SpeterSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW,
11185202Speter	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
11285202Speter
11343436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
11443436Smsmith
11517658Sjulian/*
11617658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
11717658Sjulian * to indicate that the kernel has already called panic.
11817658Sjulian */
11917658Sjulianconst char *panicstr;
12017658Sjulian
12193496Sphkint dumping;				/* system is dumping */
122155383Sjeffint rebooting;				/* system is rebooting */
12393496Sphkstatic struct dumperinfo dumper;	/* our selected dumper */
12467093Sps
125131927Smarcel/* Context information for dump-debuggers. */
126131927Smarcelstatic struct pcb dumppcb;		/* Registers. */
127131927Smarcelstatic lwpid_t dumptid;			/* Thread ID. */
128131927Smarcel
12965395Speterstatic void boot(int) __dead2;
13065395Speterstatic void poweroff_wait(void *, int);
13165395Speterstatic void shutdown_halt(void *junk, int howto);
13265395Speterstatic void shutdown_panic(void *junk, int howto);
13365395Speterstatic void shutdown_reset(void *junk, int howto);
13417658Sjulian
13550107Smsmith/* register various local shutdown events */
136110859Salfredstatic void
13750107Smsmithshutdown_conf(void *unused)
13850107Smsmith{
139110859Salfred
140110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
141110859Salfred	    SHUTDOWN_PRI_FIRST);
142110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
143110859Salfred	    SHUTDOWN_PRI_LAST + 100);
144110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
145110859Salfred	    SHUTDOWN_PRI_LAST + 100);
146110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
147110859Salfred	    SHUTDOWN_PRI_LAST + 200);
14850107Smsmith}
14948868Sphk
15050107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
15150107Smsmith
15217658Sjulian/*
153167211Srwatson * The system call that results in a reboot.
15417658Sjulian */
15582749Sdillon/* ARGSUSED */
15617658Sjulianint
15783366Sjulianreboot(struct thread *td, struct reboot_args *uap)
15817658Sjulian{
15917658Sjulian	int error;
16017658Sjulian
161106024Srwatson	error = 0;
162106024Srwatson#ifdef MAC
163172930Srwatson	error = mac_system_check_reboot(td->td_ucred, uap->opt);
164106024Srwatson#endif
165106024Srwatson	if (error == 0)
166164033Srwatson		error = priv_check(td, PRIV_REBOOT);
167106024Srwatson	if (error == 0) {
168106024Srwatson		mtx_lock(&Giant);
16982749Sdillon		boot(uap->opt);
170106024Srwatson		mtx_unlock(&Giant);
171106024Srwatson	}
17282749Sdillon	return (error);
17317658Sjulian}
17417658Sjulian
17517658Sjulian/*
17617658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
17717658Sjulian */
17865268Smsmithstatic int shutdown_howto = 0;
17965268Smsmith
18017658Sjulianvoid
18165268Smsmithshutdown_nice(int howto)
18217658Sjulian{
183110859Salfred
18465268Smsmith	shutdown_howto = howto;
185110859Salfred
18617658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
18717658Sjulian	if (initproc != NULL) {
18873913Sjhb		PROC_LOCK(initproc);
18917658Sjulian		psignal(initproc, SIGINT);
19073913Sjhb		PROC_UNLOCK(initproc);
19117658Sjulian	} else {
19217658Sjulian		/* No init(8) running, so simply reboot */
19317658Sjulian		boot(RB_NOSYNC);
19417658Sjulian	}
19517658Sjulian	return;
19617658Sjulian}
19717658Sjulianstatic int	waittime = -1;
19817658Sjulian
19954233Sphkstatic void
20065395Speterprint_uptime(void)
20154233Sphk{
20254233Sphk	int f;
20354233Sphk	struct timespec ts;
20454233Sphk
20554233Sphk	getnanouptime(&ts);
20654233Sphk	printf("Uptime: ");
20754233Sphk	f = 0;
20854233Sphk	if (ts.tv_sec >= 86400) {
20965764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
21054233Sphk		ts.tv_sec %= 86400;
21154233Sphk		f = 1;
21254233Sphk	}
21354233Sphk	if (f || ts.tv_sec >= 3600) {
21465764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
21554233Sphk		ts.tv_sec %= 3600;
21654233Sphk		f = 1;
21754233Sphk	}
21854233Sphk	if (f || ts.tv_sec >= 60) {
21965764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
22054233Sphk		ts.tv_sec %= 60;
22154233Sphk		f = 1;
22254233Sphk	}
22365764Sjhb	printf("%lds\n", (long)ts.tv_sec);
22454233Sphk}
22554233Sphk
22694169Sphkstatic void
22794169Sphkdoadump(void)
22894169Sphk{
229110859Salfred
230132412Sjulian	/*
231132412Sjulian	 * Sometimes people have to call this from the kernel debugger.
232132412Sjulian	 * (if 'panic' can not dump)
233132412Sjulian	 * Give them a clue as to why they can't dump.
234132412Sjulian	 */
235132412Sjulian	if (dumper.dumper == NULL) {
236132413Sjulian		printf("Cannot dump. No dump device defined.\n");
237132412Sjulian		return;
238132412Sjulian	}
239132412Sjulian
24094169Sphk	savectx(&dumppcb);
241131927Smarcel	dumptid = curthread->td_tid;
24294169Sphk	dumping++;
24394169Sphk	dumpsys(&dumper);
24494169Sphk}
24594169Sphk
246149875Struckmanstatic int
247149875Struckmanisbufbusy(struct buf *bp)
248149875Struckman{
249149875Struckman	if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
250149875Struckman	    BUF_REFCNT(bp) > 0) ||
251149875Struckman	    ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
252149875Struckman		return (1);
253149875Struckman	return (0);
254149875Struckman}
255149875Struckman
25617658Sjulian/*
257137329Snjl * Shutdown the system cleanly to prepare for reboot, halt, or power off.
25817658Sjulian */
25931275Sbdestatic void
26065395Speterboot(int howto)
26117658Sjulian{
262133763Struckman	static int first_buf_printf = 1;
26317658Sjulian
264137375Smarcel#if defined(SMP)
265137329Snjl	/*
266137329Snjl	 * Bind us to CPU 0 so that all shutdown code runs there.  Some
267137329Snjl	 * systems don't shutdown properly (i.e., ACPI power off) if we
268137329Snjl	 * run on another processor.
269137329Snjl	 */
270170307Sjeff	thread_lock(curthread);
271137263Speter	sched_bind(curthread, 0);
272170307Sjeff	thread_unlock(curthread);
273138217Snjl	KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
274137263Speter#endif
275155383Sjeff	/* We're in the process of rebooting. */
276155383Sjeff	rebooting = 1;
277137263Speter
27865268Smsmith	/* collect extra flags that shutdown_nice might have set */
27965268Smsmith	howto |= shutdown_howto;
28065268Smsmith
28182119Sjhb	/* We are out of the debugger now. */
282131927Smarcel	kdb_active = 0;
28382119Sjhb
28427997Sjulian	/*
28527997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
28627997Sjulian	 */
28750107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
28827997Sjulian
28927997Sjulian	/*
29027997Sjulian	 * Now sync filesystems
29127997Sjulian	 */
29217658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
29317658Sjulian		register struct buf *bp;
29465707Sjasone		int iter, nbusy, pbusy;
295131481Sjhb#ifndef PREEMPTION
29665707Sjasone		int subiter;
297131481Sjhb#endif
29817658Sjulian
29917658Sjulian		waittime = 0;
30017658Sjulian
301150472Sups		sync(curthread, NULL);
30217658Sjulian
30334266Sjulian		/*
30434266Sjulian		 * With soft updates, some buffers that are
30534266Sjulian		 * written will be remarked as dirty until other
30634266Sjulian		 * buffers are written.
30734266Sjulian		 */
30865707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
30917658Sjulian			nbusy = 0;
310149875Struckman			for (bp = &buf[nbuf]; --bp >= buf; )
311149875Struckman				if (isbufbusy(bp))
31217658Sjulian					nbusy++;
313133763Struckman			if (nbusy == 0) {
314133763Struckman				if (first_buf_printf)
315136115Sphk					printf("All buffers synced.");
31617658Sjulian				break;
317133763Struckman			}
318133763Struckman			if (first_buf_printf) {
319133763Struckman				printf("Syncing disks, buffers remaining... ");
320133763Struckman				first_buf_printf = 0;
321133763Struckman			}
32217658Sjulian			printf("%d ", nbusy);
32365707Sjasone			if (nbusy < pbusy)
32465707Sjasone				iter = 0;
32565707Sjasone			pbusy = nbusy;
326150472Sups			sync(curthread, NULL);
327131481Sjhb
328131481Sjhb#ifdef PREEMPTION
329131481Sjhb			/*
330131481Sjhb			 * Drop Giant and spin for a while to allow
331131481Sjhb			 * interrupt threads to run.
332131481Sjhb			 */
333131481Sjhb			DROP_GIANT();
33434266Sjulian			DELAY(50000 * iter);
335131481Sjhb			PICKUP_GIANT();
336131481Sjhb#else
337131481Sjhb			/*
338131481Sjhb			 * Drop Giant and context switch several times to
339131481Sjhb			 * allow interrupt threads to run.
340131481Sjhb			 */
341131481Sjhb			DROP_GIANT();
342131481Sjhb			for (subiter = 0; subiter < 50 * iter; subiter++) {
343170307Sjeff				thread_lock(curthread);
344131481Sjhb				mi_switch(SW_VOL, NULL);
345170307Sjeff				thread_unlock(curthread);
346131481Sjhb				DELAY(1000);
347131481Sjhb			}
348131481Sjhb			PICKUP_GIANT();
349131481Sjhb#endif
35017658Sjulian		}
351133418Snjl		printf("\n");
35241137Smsmith		/*
35341137Smsmith		 * Count only busy local buffers to prevent forcing
35441137Smsmith		 * a fsck if we're just a client of a wedged NFS server
35541137Smsmith		 */
35641137Smsmith		nbusy = 0;
35741137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
358149875Struckman			if (isbufbusy(bp)) {
359137186Sphk#if 0
360137186Sphk/* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
361130640Sphk				if (bp->b_dev == NULL) {
36253452Sphk					TAILQ_REMOVE(&mountlist,
36348225Smckusick					    bp->b_vp->v_mount, mnt_list);
36453023Sphk					continue;
36553023Sphk				}
366137186Sphk#endif
36753023Sphk				nbusy++;
36853023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
36953023Sphk				printf(
370137186Sphk			    "%d: bufobj:%p, flags:%0x, blkno:%ld, lblkno:%ld\n",
371137186Sphk				    nbusy, bp->b_bufobj,
37253023Sphk				    bp->b_flags, (long)bp->b_blkno,
37353023Sphk				    (long)bp->b_lblkno);
37453023Sphk#endif
37546568Speter			}
37641137Smsmith		}
37717658Sjulian		if (nbusy) {
37817658Sjulian			/*
37917658Sjulian			 * Failed to sync all blocks. Indicate this and don't
38017658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
38117658Sjulian			 */
382133763Struckman			printf("Giving up on %d buffers\n", nbusy);
38317658Sjulian			DELAY(5000000);	/* 5 seconds */
38417658Sjulian		} else {
385133763Struckman			if (!first_buf_printf)
386133763Struckman				printf("Final sync complete\n");
38717658Sjulian			/*
38817658Sjulian			 * Unmount filesystems
38917658Sjulian			 */
39017658Sjulian			if (panicstr == 0)
39117658Sjulian				vfs_unmountall();
39217658Sjulian		}
393157628Spjd		swapoff_all();
39439237Sgibbs		DELAY(100000);		/* wait for console output to finish */
39517658Sjulian	}
39627997Sjulian
39754233Sphk	print_uptime();
39854233Sphk
39927997Sjulian	/*
40027997Sjulian	 * Ok, now do things that assume all filesystem activity has
40127997Sjulian	 * been completed.
40227997Sjulian	 */
40350107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
404137329Snjl
405137329Snjl	/* XXX This doesn't disable interrupts any more.  Reconsider? */
40639237Sgibbs	splhigh();
407137329Snjl
408132412Sjulian	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
40994169Sphk		doadump();
41039237Sgibbs
41139237Sgibbs	/* Now that we're going to really halt the system... */
41250107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
41339237Sgibbs
41450107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
41550107Smsmith	/* NOTREACHED */
41650107Smsmith}
41750107Smsmith
41850107Smsmith/*
41950107Smsmith * If the shutdown was a clean halt, behave accordingly.
42050107Smsmith */
42150107Smsmithstatic void
42250107Smsmithshutdown_halt(void *junk, int howto)
42350107Smsmith{
424110859Salfred
42517658Sjulian	if (howto & RB_HALT) {
42617658Sjulian		printf("\n");
42717658Sjulian		printf("The operating system has halted.\n");
42817658Sjulian		printf("Please press any key to reboot.\n\n");
42919274Sjulian		switch (cngetc()) {
43019274Sjulian		case -1:		/* No console, just die */
43119274Sjulian			cpu_halt();
43219274Sjulian			/* NOTREACHED */
43319274Sjulian		default:
43439237Sgibbs			howto &= ~RB_HALT;
43519274Sjulian			break;
43619274Sjulian		}
43750107Smsmith	}
43850107Smsmith}
43917658Sjulian
44050107Smsmith/*
44150107Smsmith * Check to see if the system paniced, pause and then reboot
44250107Smsmith * according to the specified delay.
44350107Smsmith */
44450107Smsmithstatic void
44550107Smsmithshutdown_panic(void *junk, int howto)
44650107Smsmith{
44750107Smsmith	int loop;
44850107Smsmith
44950107Smsmith	if (howto & RB_DUMP) {
45039237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
45139237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
45239237Sgibbs				printf("Automatic reboot in %d seconds - "
45339237Sgibbs				       "press a key on the console to abort\n",
45439237Sgibbs					PANIC_REBOOT_WAIT_TIME);
45539237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
45639237Sgibbs				     loop > 0; --loop) {
45739237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
45839237Sgibbs					/* Did user type a key? */
45939237Sgibbs					if (cncheckc() != -1)
46039237Sgibbs						break;
46117658Sjulian				}
46239237Sgibbs				if (!loop)
46350107Smsmith					return;
46417658Sjulian			}
46539237Sgibbs		} else { /* zero time specified - reboot NOW */
46650107Smsmith			return;
46717658Sjulian		}
46889522Snik		printf("--> Press a key on the console to reboot,\n");
46989522Snik		printf("--> or switch off the system now.\n");
47039237Sgibbs		cngetc();
47117658Sjulian	}
47250107Smsmith}
47350107Smsmith
47450107Smsmith/*
47550107Smsmith * Everything done, now reset
47650107Smsmith */
47750107Smsmithstatic void
47850107Smsmithshutdown_reset(void *junk, int howto)
47950107Smsmith{
480110859Salfred
48117658Sjulian	printf("Rebooting...\n");
48217658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
48317677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
48417658Sjulian	cpu_reset();
48550107Smsmith	/* NOTREACHED */ /* assuming reset worked */
48617658Sjulian}
48717658Sjulian
48875570Sjhb#ifdef SMP
489101155Sjhbstatic u_int panic_cpu = NOCPU;
49075570Sjhb#endif
49175570Sjhb
49217658Sjulian/*
49317658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
49417658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
49517658Sjulian * the disks as this often leads to recursive panics.
49617658Sjulian */
49717658Sjulianvoid
498130164Sphkpanic(const char *fmt, ...)
49917658Sjulian{
500100209Sgallatin	struct thread *td = curthread;
501103647Sjhb	int bootopt, newpanic;
50217658Sjulian	va_list ap;
50338874Sache	static char buf[256];
50417658Sjulian
50565557Sjasone#ifdef SMP
50682115Sjhb	/*
50782115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
508101155Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
509101155Sjhb	 * panic_cpu if we are spinning in case the panic on the first
51082115Sjhb	 * CPU is canceled.
51182115Sjhb	 */
512101155Sjhb	if (panic_cpu != PCPU_GET(cpuid))
513101155Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
514101155Sjhb		    PCPU_GET(cpuid)) == 0)
515101155Sjhb			while (panic_cpu != NOCPU)
516101155Sjhb				; /* nothing */
51765557Sjasone#endif
51865557Sjasone
51917658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
520103647Sjhb	newpanic = 0;
52117658Sjulian	if (panicstr)
52217658Sjulian		bootopt |= RB_NOSYNC;
523103647Sjhb	else {
52417658Sjulian		panicstr = fmt;
525103647Sjhb		newpanic = 1;
526103647Sjhb	}
52717658Sjulian
52817658Sjulian	va_start(ap, fmt);
529116398Siedowse	if (newpanic) {
530116398Siedowse		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
53138874Sache		panicstr = buf;
532130164Sphk		printf("panic: %s\n", buf);
533116398Siedowse	} else {
534116398Siedowse		printf("panic: ");
535116398Siedowse		vprintf(fmt, ap);
536130164Sphk		printf("\n");
537116398Siedowse	}
53817658Sjulian	va_end(ap);
53926100Sfsmp#ifdef SMP
540134089Sjhb	printf("cpuid = %d\n", PCPU_GET(cpuid));
54126100Sfsmp#endif
54217658Sjulian
543131927Smarcel#ifdef KDB
544103647Sjhb	if (newpanic && trace_on_panic)
545131927Smarcel		kdb_backtrace();
54617658Sjulian	if (debugger_on_panic)
547174898Srwatson		kdb_enter(KDB_WHY_PANIC, "panic");
54882223Sjhb#ifdef RESTARTABLE_PANICS
54982115Sjhb	/* See if the user aborted the panic, in which case we continue. */
55082115Sjhb	if (panicstr == NULL) {
55182115Sjhb#ifdef SMP
552101155Sjhb		atomic_store_rel_int(&panic_cpu, NOCPU);
55317658Sjulian#endif
55482115Sjhb		return;
55582115Sjhb	}
55682115Sjhb#endif
55782223Sjhb#endif
558170307Sjeff	/*thread_lock(td); */
559100209Sgallatin	td->td_flags |= TDF_INPANIC;
560170307Sjeff	/* thread_unlock(td); */
56185202Speter	if (!sync_on_panic)
56285202Speter		bootopt |= RB_NOSYNC;
56317658Sjulian	boot(bootopt);
56417658Sjulian}
56517658Sjulian
56617768Sjulian/*
56743436Smsmith * Support for poweroff delay.
56843436Smsmith */
56954248Smsmith#ifndef POWEROFF_DELAY
57054248Smsmith# define POWEROFF_DELAY 5000
57154248Smsmith#endif
57254248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
57354248Smsmith
57443436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
57543436Smsmith	&poweroff_delay, 0, "");
57643436Smsmith
577110859Salfredstatic void
57850107Smsmithpoweroff_wait(void *junk, int howto)
57943436Smsmith{
580110859Salfred
581110859Salfred	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
58243436Smsmith		return;
58343436Smsmith	DELAY(poweroff_delay * 1000);
58443436Smsmith}
58555539Sluoqi
58655539Sluoqi/*
58755539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
58855539Sluoqi * points in their main loops prior to a system shutdown, so that they
58955539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
59055539Sluoqi * to cause sync to fail).  For each of these system processes, register
59155539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
59255539Sluoqi */
59355539Sluoqistatic int kproc_shutdown_wait = 60;
59455539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
59555539Sluoqi    &kproc_shutdown_wait, 0, "");
59655539Sluoqi
59755539Sluoqivoid
59870063Sjhbkproc_shutdown(void *arg, int howto)
59955539Sluoqi{
60055539Sluoqi	struct proc *p;
601132177Salfred	char procname[MAXCOMLEN + 1];
60255539Sluoqi	int error;
60355539Sluoqi
60455539Sluoqi	if (panicstr)
60555539Sluoqi		return;
60655539Sluoqi
60755539Sluoqi	p = (struct proc *)arg;
608132177Salfred	strlcpy(procname, p->p_comm, sizeof(procname));
609132866Snjl	printf("Waiting (max %d seconds) for system process `%s' to stop...",
610132177Salfred	    kproc_shutdown_wait, procname);
611172836Sjulian	error = kproc_suspend(p, kproc_shutdown_wait * hz);
61255539Sluoqi
61355539Sluoqi	if (error == EWOULDBLOCK)
614132866Snjl		printf("timed out\n");
61555539Sluoqi	else
616132866Snjl		printf("done\n");
61755539Sluoqi}
61893496Sphk
619173004Sjulianvoid
620173004Sjuliankthread_shutdown(void *arg, int howto)
621173004Sjulian{
622173004Sjulian	struct thread *td;
623173004Sjulian	char procname[MAXCOMLEN + 1];
624173004Sjulian	int error;
625173004Sjulian
626173004Sjulian	if (panicstr)
627173004Sjulian		return;
628173004Sjulian
629173004Sjulian	td = (struct thread *)arg;
630173004Sjulian	strlcpy(procname, td->td_name, sizeof(procname));
631173004Sjulian	printf("Waiting (max %d seconds) for system thread `%s' to stop...",
632173004Sjulian	    kproc_shutdown_wait, procname);
633173004Sjulian	error = kthread_suspend(td, kproc_shutdown_wait * hz);
634173004Sjulian
635173004Sjulian	if (error == EWOULDBLOCK)
636173004Sjulian		printf("timed out\n");
637173004Sjulian	else
638173004Sjulian		printf("done\n");
639173004Sjulian}
640173004Sjulian
64193496Sphk/* Registration of dumpers */
64293496Sphkint
64393496Sphkset_dumper(struct dumperinfo *di)
64493496Sphk{
645110859Salfred
64693496Sphk	if (di == NULL) {
64793496Sphk		bzero(&dumper, sizeof dumper);
64893496Sphk		return (0);
64993496Sphk	}
65093496Sphk	if (dumper.dumper != NULL)
65193496Sphk		return (EBUSY);
65293496Sphk	dumper = *di;
65393496Sphk	return (0);
65493496Sphk}
65593496Sphk
656105531Stmm#if defined(__powerpc__)
65793496Sphkvoid
65893496Sphkdumpsys(struct dumperinfo *di __unused)
65993496Sphk{
66093496Sphk
66193496Sphk	printf("Kernel dumps not implemented on this architecture\n");
66293496Sphk}
66393496Sphk#endif
664