kern_shutdown.c revision 213322
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 213322 2010-10-01 09:34:41Z avg $");
39116182Sobrien
40174921Srwatson#include "opt_ddb.h"
41131927Smarcel#include "opt_kdb.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>
53193066Sjamie#include <sys/jail.h>
54131927Smarcel#include <sys/kdb.h>
5517658Sjulian#include <sys/kernel.h>
56183527Speter#include <sys/kerneldump.h>
5755539Sluoqi#include <sys/kthread.h>
5889601Ssobomax#include <sys/malloc.h>
5921776Sbde#include <sys/mount.h>
60164033Srwatson#include <sys/priv.h>
6178767Sjhb#include <sys/proc.h>
6278767Sjhb#include <sys/reboot.h>
6378767Sjhb#include <sys/resourcevar.h>
64137263Speter#include <sys/sched.h>
65206878Sattilio#include <sys/smp.h>
6617658Sjulian#include <sys/sysctl.h>
6717658Sjulian#include <sys/sysproto.h>
6817658Sjulian
69174921Srwatson#include <ddb/ddb.h>
70174921Srwatson
71118990Smarcel#include <machine/cpu.h>
7294169Sphk#include <machine/pcb.h>
7391778Sjake#include <machine/smp.h>
7417658Sjulian
75163606Srwatson#include <security/mac/mac_framework.h>
76163606Srwatson
77157628Spjd#include <vm/vm.h>
78157628Spjd#include <vm/vm_object.h>
79157628Spjd#include <vm/vm_page.h>
80157628Spjd#include <vm/vm_pager.h>
81157628Spjd#include <vm/swap_pager.h>
82157628Spjd
8317658Sjulian#include <sys/signalvar.h>
8417658Sjulian
8517658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
8617658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
8717658Sjulian#endif
8817658Sjulian
8917658Sjulian/*
9017658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
9117658Sjulian * ANSI and traditional C compilers.
9217658Sjulian */
9317658Sjulian#include <machine/stdarg.h>
9417658Sjulian
95131927Smarcel#ifdef KDB
96131927Smarcel#ifdef KDB_UNATTENDED
9742135Smsmithint debugger_on_panic = 0;
9817658Sjulian#else
9942135Smsmithint debugger_on_panic = 1;
10017658Sjulian#endif
101213322SavgSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
10246381Sbillf	&debugger_on_panic, 0, "Run debugger on kernel panic");
103213322SavgTUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic);
104103647Sjhb
105131927Smarcel#ifdef KDB_TRACE
106213322Savgstatic int trace_on_panic = 1;
107103647Sjhb#else
108213322Savgstatic int trace_on_panic = 0;
10917658Sjulian#endif
110213322SavgSYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
111103647Sjhb	&trace_on_panic, 0, "Print stack trace on kernel panic");
112213322SavgTUNABLE_INT("debug.trace_on_panic", &trace_on_panic);
113131927Smarcel#endif /* KDB */
11417658Sjulian
115213322Savgstatic int sync_on_panic = 0;
116213322SavgSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
11785202Speter	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
118213322SavgTUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
11985202Speter
12043436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
12143436Smsmith
12217658Sjulian/*
12317658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
12417658Sjulian * to indicate that the kernel has already called panic.
12517658Sjulian */
12617658Sjulianconst char *panicstr;
12717658Sjulian
12893496Sphkint dumping;				/* system is dumping */
129155383Sjeffint rebooting;				/* system is rebooting */
13093496Sphkstatic struct dumperinfo dumper;	/* our selected dumper */
13167093Sps
132131927Smarcel/* Context information for dump-debuggers. */
133131927Smarcelstatic struct pcb dumppcb;		/* Registers. */
134131927Smarcelstatic lwpid_t dumptid;			/* Thread ID. */
135131927Smarcel
13665395Speterstatic void boot(int) __dead2;
13765395Speterstatic void poweroff_wait(void *, int);
13865395Speterstatic void shutdown_halt(void *junk, int howto);
13965395Speterstatic void shutdown_panic(void *junk, int howto);
14065395Speterstatic void shutdown_reset(void *junk, int howto);
14117658Sjulian
14250107Smsmith/* register various local shutdown events */
143110859Salfredstatic void
14450107Smsmithshutdown_conf(void *unused)
14550107Smsmith{
146110859Salfred
147110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
148203420Smav	    SHUTDOWN_PRI_FIRST + 100);
149110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
150110859Salfred	    SHUTDOWN_PRI_LAST + 100);
151110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
152110859Salfred	    SHUTDOWN_PRI_LAST + 100);
153110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
154110859Salfred	    SHUTDOWN_PRI_LAST + 200);
15550107Smsmith}
15648868Sphk
157177253SrwatsonSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
15850107Smsmith
15917658Sjulian/*
160167211Srwatson * The system call that results in a reboot.
16117658Sjulian */
16282749Sdillon/* ARGSUSED */
16317658Sjulianint
16483366Sjulianreboot(struct thread *td, struct reboot_args *uap)
16517658Sjulian{
16617658Sjulian	int error;
16717658Sjulian
168106024Srwatson	error = 0;
169106024Srwatson#ifdef MAC
170172930Srwatson	error = mac_system_check_reboot(td->td_ucred, uap->opt);
171106024Srwatson#endif
172106024Srwatson	if (error == 0)
173164033Srwatson		error = priv_check(td, PRIV_REBOOT);
174106024Srwatson	if (error == 0) {
175106024Srwatson		mtx_lock(&Giant);
17682749Sdillon		boot(uap->opt);
177106024Srwatson		mtx_unlock(&Giant);
178106024Srwatson	}
17982749Sdillon	return (error);
18017658Sjulian}
18117658Sjulian
18217658Sjulian/*
18317658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
18417658Sjulian */
18565268Smsmithstatic int shutdown_howto = 0;
18665268Smsmith
18717658Sjulianvoid
18865268Smsmithshutdown_nice(int howto)
18917658Sjulian{
190110859Salfred
19165268Smsmith	shutdown_howto = howto;
192110859Salfred
19317658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
19417658Sjulian	if (initproc != NULL) {
19573913Sjhb		PROC_LOCK(initproc);
19617658Sjulian		psignal(initproc, SIGINT);
19773913Sjhb		PROC_UNLOCK(initproc);
19817658Sjulian	} else {
19917658Sjulian		/* No init(8) running, so simply reboot */
20017658Sjulian		boot(RB_NOSYNC);
20117658Sjulian	}
20217658Sjulian	return;
20317658Sjulian}
20417658Sjulianstatic int	waittime = -1;
20517658Sjulian
20654233Sphkstatic void
20765395Speterprint_uptime(void)
20854233Sphk{
20954233Sphk	int f;
21054233Sphk	struct timespec ts;
21154233Sphk
21254233Sphk	getnanouptime(&ts);
21354233Sphk	printf("Uptime: ");
21454233Sphk	f = 0;
21554233Sphk	if (ts.tv_sec >= 86400) {
21665764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
21754233Sphk		ts.tv_sec %= 86400;
21854233Sphk		f = 1;
21954233Sphk	}
22054233Sphk	if (f || ts.tv_sec >= 3600) {
22165764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
22254233Sphk		ts.tv_sec %= 3600;
22354233Sphk		f = 1;
22454233Sphk	}
22554233Sphk	if (f || ts.tv_sec >= 60) {
22665764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
22754233Sphk		ts.tv_sec %= 60;
22854233Sphk		f = 1;
22954233Sphk	}
23065764Sjhb	printf("%lds\n", (long)ts.tv_sec);
23154233Sphk}
23254233Sphk
23394169Sphkstatic void
23494169Sphkdoadump(void)
23594169Sphk{
236110859Salfred
237132412Sjulian	/*
238132412Sjulian	 * Sometimes people have to call this from the kernel debugger.
239132412Sjulian	 * (if 'panic' can not dump)
240132412Sjulian	 * Give them a clue as to why they can't dump.
241132412Sjulian	 */
242132412Sjulian	if (dumper.dumper == NULL) {
243185234Sdwmalone		printf("Cannot dump. Device not defined or unavailable.\n");
244132412Sjulian		return;
245132412Sjulian	}
246132412Sjulian
24794169Sphk	savectx(&dumppcb);
248131927Smarcel	dumptid = curthread->td_tid;
24994169Sphk	dumping++;
250174921Srwatson#ifdef DDB
251174921Srwatson	if (textdump_pending)
252174921Srwatson		textdump_dumpsys(&dumper);
253174921Srwatson	else
254174921Srwatson#endif
255174921Srwatson		dumpsys(&dumper);
256176788Sru	dumping--;
25794169Sphk}
25894169Sphk
259149875Struckmanstatic int
260149875Struckmanisbufbusy(struct buf *bp)
261149875Struckman{
262149875Struckman	if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
263175486Sattilio	    BUF_ISLOCKED(bp)) ||
264149875Struckman	    ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
265149875Struckman		return (1);
266149875Struckman	return (0);
267149875Struckman}
268149875Struckman
26917658Sjulian/*
270137329Snjl * Shutdown the system cleanly to prepare for reboot, halt, or power off.
27117658Sjulian */
27231275Sbdestatic void
27365395Speterboot(int howto)
27417658Sjulian{
275133763Struckman	static int first_buf_printf = 1;
27617658Sjulian
277137375Smarcel#if defined(SMP)
278137329Snjl	/*
279137329Snjl	 * Bind us to CPU 0 so that all shutdown code runs there.  Some
280137329Snjl	 * systems don't shutdown properly (i.e., ACPI power off) if we
281137329Snjl	 * run on another processor.
282137329Snjl	 */
283170307Sjeff	thread_lock(curthread);
284137263Speter	sched_bind(curthread, 0);
285170307Sjeff	thread_unlock(curthread);
286138217Snjl	KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
287137263Speter#endif
288155383Sjeff	/* We're in the process of rebooting. */
289155383Sjeff	rebooting = 1;
290137263Speter
29165268Smsmith	/* collect extra flags that shutdown_nice might have set */
29265268Smsmith	howto |= shutdown_howto;
29365268Smsmith
29482119Sjhb	/* We are out of the debugger now. */
295131927Smarcel	kdb_active = 0;
29682119Sjhb
29727997Sjulian	/*
29827997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
29927997Sjulian	 */
30050107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
30127997Sjulian
30227997Sjulian	/*
30327997Sjulian	 * Now sync filesystems
30427997Sjulian	 */
30517658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
30617658Sjulian		register struct buf *bp;
30765707Sjasone		int iter, nbusy, pbusy;
308131481Sjhb#ifndef PREEMPTION
30965707Sjasone		int subiter;
310131481Sjhb#endif
31117658Sjulian
31217658Sjulian		waittime = 0;
31317658Sjulian
314150472Sups		sync(curthread, NULL);
31517658Sjulian
31634266Sjulian		/*
31734266Sjulian		 * With soft updates, some buffers that are
31834266Sjulian		 * written will be remarked as dirty until other
31934266Sjulian		 * buffers are written.
32034266Sjulian		 */
32165707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
32217658Sjulian			nbusy = 0;
323149875Struckman			for (bp = &buf[nbuf]; --bp >= buf; )
324149875Struckman				if (isbufbusy(bp))
32517658Sjulian					nbusy++;
326133763Struckman			if (nbusy == 0) {
327133763Struckman				if (first_buf_printf)
328136115Sphk					printf("All buffers synced.");
32917658Sjulian				break;
330133763Struckman			}
331133763Struckman			if (first_buf_printf) {
332133763Struckman				printf("Syncing disks, buffers remaining... ");
333133763Struckman				first_buf_printf = 0;
334133763Struckman			}
33517658Sjulian			printf("%d ", nbusy);
33665707Sjasone			if (nbusy < pbusy)
33765707Sjasone				iter = 0;
33865707Sjasone			pbusy = nbusy;
339150472Sups			sync(curthread, NULL);
340131481Sjhb
341131481Sjhb#ifdef PREEMPTION
342131481Sjhb			/*
343131481Sjhb			 * Drop Giant and spin for a while to allow
344131481Sjhb			 * interrupt threads to run.
345131481Sjhb			 */
346131481Sjhb			DROP_GIANT();
34734266Sjulian			DELAY(50000 * iter);
348131481Sjhb			PICKUP_GIANT();
349131481Sjhb#else
350131481Sjhb			/*
351131481Sjhb			 * Drop Giant and context switch several times to
352131481Sjhb			 * allow interrupt threads to run.
353131481Sjhb			 */
354131481Sjhb			DROP_GIANT();
355131481Sjhb			for (subiter = 0; subiter < 50 * iter; subiter++) {
356170307Sjeff				thread_lock(curthread);
357131481Sjhb				mi_switch(SW_VOL, NULL);
358170307Sjeff				thread_unlock(curthread);
359131481Sjhb				DELAY(1000);
360131481Sjhb			}
361131481Sjhb			PICKUP_GIANT();
362131481Sjhb#endif
36317658Sjulian		}
364133418Snjl		printf("\n");
36541137Smsmith		/*
36641137Smsmith		 * Count only busy local buffers to prevent forcing
36741137Smsmith		 * a fsck if we're just a client of a wedged NFS server
36841137Smsmith		 */
36941137Smsmith		nbusy = 0;
37041137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
371149875Struckman			if (isbufbusy(bp)) {
372137186Sphk#if 0
373137186Sphk/* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
374130640Sphk				if (bp->b_dev == NULL) {
37553452Sphk					TAILQ_REMOVE(&mountlist,
37648225Smckusick					    bp->b_vp->v_mount, mnt_list);
37753023Sphk					continue;
37853023Sphk				}
379137186Sphk#endif
38053023Sphk				nbusy++;
38153023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
38253023Sphk				printf(
383137186Sphk			    "%d: bufobj:%p, flags:%0x, blkno:%ld, lblkno:%ld\n",
384137186Sphk				    nbusy, bp->b_bufobj,
38553023Sphk				    bp->b_flags, (long)bp->b_blkno,
38653023Sphk				    (long)bp->b_lblkno);
38753023Sphk#endif
38846568Speter			}
38941137Smsmith		}
39017658Sjulian		if (nbusy) {
39117658Sjulian			/*
39217658Sjulian			 * Failed to sync all blocks. Indicate this and don't
39317658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
39417658Sjulian			 */
395133763Struckman			printf("Giving up on %d buffers\n", nbusy);
39617658Sjulian			DELAY(5000000);	/* 5 seconds */
39717658Sjulian		} else {
398133763Struckman			if (!first_buf_printf)
399133763Struckman				printf("Final sync complete\n");
40017658Sjulian			/*
40117658Sjulian			 * Unmount filesystems
40217658Sjulian			 */
40317658Sjulian			if (panicstr == 0)
40417658Sjulian				vfs_unmountall();
40517658Sjulian		}
406157628Spjd		swapoff_all();
40739237Sgibbs		DELAY(100000);		/* wait for console output to finish */
40817658Sjulian	}
40927997Sjulian
41054233Sphk	print_uptime();
41154233Sphk
41227997Sjulian	/*
41327997Sjulian	 * Ok, now do things that assume all filesystem activity has
41427997Sjulian	 * been completed.
41527997Sjulian	 */
41650107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
417137329Snjl
418132412Sjulian	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
41994169Sphk		doadump();
42039237Sgibbs
42139237Sgibbs	/* Now that we're going to really halt the system... */
42250107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
42339237Sgibbs
42450107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
42550107Smsmith	/* NOTREACHED */
42650107Smsmith}
42750107Smsmith
42850107Smsmith/*
42950107Smsmith * If the shutdown was a clean halt, behave accordingly.
43050107Smsmith */
43150107Smsmithstatic void
43250107Smsmithshutdown_halt(void *junk, int howto)
43350107Smsmith{
434110859Salfred
43517658Sjulian	if (howto & RB_HALT) {
43617658Sjulian		printf("\n");
43717658Sjulian		printf("The operating system has halted.\n");
43817658Sjulian		printf("Please press any key to reboot.\n\n");
43919274Sjulian		switch (cngetc()) {
44019274Sjulian		case -1:		/* No console, just die */
44119274Sjulian			cpu_halt();
44219274Sjulian			/* NOTREACHED */
44319274Sjulian		default:
44439237Sgibbs			howto &= ~RB_HALT;
44519274Sjulian			break;
44619274Sjulian		}
44750107Smsmith	}
44850107Smsmith}
44917658Sjulian
45050107Smsmith/*
45150107Smsmith * Check to see if the system paniced, pause and then reboot
45250107Smsmith * according to the specified delay.
45350107Smsmith */
45450107Smsmithstatic void
45550107Smsmithshutdown_panic(void *junk, int howto)
45650107Smsmith{
45750107Smsmith	int loop;
45850107Smsmith
45950107Smsmith	if (howto & RB_DUMP) {
46039237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
46139237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
46239237Sgibbs				printf("Automatic reboot in %d seconds - "
46339237Sgibbs				       "press a key on the console to abort\n",
46439237Sgibbs					PANIC_REBOOT_WAIT_TIME);
46539237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
46639237Sgibbs				     loop > 0; --loop) {
46739237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
46839237Sgibbs					/* Did user type a key? */
46939237Sgibbs					if (cncheckc() != -1)
47039237Sgibbs						break;
47117658Sjulian				}
47239237Sgibbs				if (!loop)
47350107Smsmith					return;
47417658Sjulian			}
47539237Sgibbs		} else { /* zero time specified - reboot NOW */
47650107Smsmith			return;
47717658Sjulian		}
47889522Snik		printf("--> Press a key on the console to reboot,\n");
47989522Snik		printf("--> or switch off the system now.\n");
48039237Sgibbs		cngetc();
48117658Sjulian	}
48250107Smsmith}
48350107Smsmith
48450107Smsmith/*
48550107Smsmith * Everything done, now reset
48650107Smsmith */
48750107Smsmithstatic void
48850107Smsmithshutdown_reset(void *junk, int howto)
48950107Smsmith{
490110859Salfred
491206878Sattilio	printf("Rebooting...\n");
492206878Sattilio	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
493206878Sattilio
494196196Sattilio	/*
495206878Sattilio	 * Acquiring smp_ipi_mtx here has a double effect:
496206878Sattilio	 * - it disables interrupts avoiding CPU0 preemption
497206878Sattilio	 *   by fast handlers (thus deadlocking  against other CPUs)
498206878Sattilio	 * - it avoids deadlocks against smp_rendezvous() or, more
499206878Sattilio	 *   generally, threads busy-waiting, with this spinlock held,
500206878Sattilio	 *   and waiting for responses by threads on other CPUs
501206878Sattilio	 *   (ie. smp_tlb_shootdown()).
502206897Sattilio	 *
503206897Sattilio	 * For the !SMP case it just needs to handle the former problem.
504196196Sattilio	 */
505206897Sattilio#ifdef SMP
506206878Sattilio	mtx_lock_spin(&smp_ipi_mtx);
507206897Sattilio#else
508206897Sattilio	spinlock_enter();
509206897Sattilio#endif
510196196Sattilio
51117677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
51217658Sjulian	cpu_reset();
51350107Smsmith	/* NOTREACHED */ /* assuming reset worked */
51417658Sjulian}
51517658Sjulian
51675570Sjhb#ifdef SMP
517101155Sjhbstatic u_int panic_cpu = NOCPU;
51875570Sjhb#endif
51975570Sjhb
52017658Sjulian/*
52117658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
52217658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
52317658Sjulian * the disks as this often leads to recursive panics.
52417658Sjulian */
52517658Sjulianvoid
526130164Sphkpanic(const char *fmt, ...)
52717658Sjulian{
528100209Sgallatin	struct thread *td = curthread;
529103647Sjhb	int bootopt, newpanic;
53017658Sjulian	va_list ap;
53138874Sache	static char buf[256];
53217658Sjulian
533183412Skib	critical_enter();
53465557Sjasone#ifdef SMP
53582115Sjhb	/*
53682115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
537101155Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
538101155Sjhb	 * panic_cpu if we are spinning in case the panic on the first
53982115Sjhb	 * CPU is canceled.
54082115Sjhb	 */
541101155Sjhb	if (panic_cpu != PCPU_GET(cpuid))
542101155Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
543101155Sjhb		    PCPU_GET(cpuid)) == 0)
544101155Sjhb			while (panic_cpu != NOCPU)
545101155Sjhb				; /* nothing */
54665557Sjasone#endif
54765557Sjasone
54817658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
549103647Sjhb	newpanic = 0;
55017658Sjulian	if (panicstr)
55117658Sjulian		bootopt |= RB_NOSYNC;
552103647Sjhb	else {
55317658Sjulian		panicstr = fmt;
554103647Sjhb		newpanic = 1;
555103647Sjhb	}
55617658Sjulian
55717658Sjulian	va_start(ap, fmt);
558116398Siedowse	if (newpanic) {
559116398Siedowse		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
56038874Sache		panicstr = buf;
561130164Sphk		printf("panic: %s\n", buf);
562116398Siedowse	} else {
563116398Siedowse		printf("panic: ");
564116398Siedowse		vprintf(fmt, ap);
565130164Sphk		printf("\n");
566116398Siedowse	}
56717658Sjulian	va_end(ap);
56826100Sfsmp#ifdef SMP
569134089Sjhb	printf("cpuid = %d\n", PCPU_GET(cpuid));
57026100Sfsmp#endif
57117658Sjulian
572131927Smarcel#ifdef KDB
573103647Sjhb	if (newpanic && trace_on_panic)
574131927Smarcel		kdb_backtrace();
57517658Sjulian	if (debugger_on_panic)
576174898Srwatson		kdb_enter(KDB_WHY_PANIC, "panic");
57782223Sjhb#ifdef RESTARTABLE_PANICS
57882115Sjhb	/* See if the user aborted the panic, in which case we continue. */
57982115Sjhb	if (panicstr == NULL) {
58082115Sjhb#ifdef SMP
581101155Sjhb		atomic_store_rel_int(&panic_cpu, NOCPU);
58217658Sjulian#endif
58382115Sjhb		return;
58482115Sjhb	}
58582115Sjhb#endif
58682223Sjhb#endif
587170307Sjeff	/*thread_lock(td); */
588100209Sgallatin	td->td_flags |= TDF_INPANIC;
589170307Sjeff	/* thread_unlock(td); */
59085202Speter	if (!sync_on_panic)
59185202Speter		bootopt |= RB_NOSYNC;
592183412Skib	critical_exit();
59317658Sjulian	boot(bootopt);
59417658Sjulian}
59517658Sjulian
59617768Sjulian/*
59743436Smsmith * Support for poweroff delay.
598197071Sn_hibma *
599197071Sn_hibma * Please note that setting this delay too short might power off your machine
600197071Sn_hibma * before the write cache on your hard disk has been flushed, leading to
601197071Sn_hibma * soft-updates inconsistencies.
60243436Smsmith */
60354248Smsmith#ifndef POWEROFF_DELAY
60454248Smsmith# define POWEROFF_DELAY 5000
60554248Smsmith#endif
60654248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
60754248Smsmith
60843436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
60943436Smsmith	&poweroff_delay, 0, "");
61043436Smsmith
611110859Salfredstatic void
61250107Smsmithpoweroff_wait(void *junk, int howto)
61343436Smsmith{
614110859Salfred
615110859Salfred	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
61643436Smsmith		return;
61743436Smsmith	DELAY(poweroff_delay * 1000);
61843436Smsmith}
61955539Sluoqi
62055539Sluoqi/*
62155539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
62255539Sluoqi * points in their main loops prior to a system shutdown, so that they
62355539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
62455539Sluoqi * to cause sync to fail).  For each of these system processes, register
62555539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
62655539Sluoqi */
62755539Sluoqistatic int kproc_shutdown_wait = 60;
62855539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
62955539Sluoqi    &kproc_shutdown_wait, 0, "");
63055539Sluoqi
63155539Sluoqivoid
63270063Sjhbkproc_shutdown(void *arg, int howto)
63355539Sluoqi{
63455539Sluoqi	struct proc *p;
63555539Sluoqi	int error;
63655539Sluoqi
63755539Sluoqi	if (panicstr)
63855539Sluoqi		return;
63955539Sluoqi
64055539Sluoqi	p = (struct proc *)arg;
641132866Snjl	printf("Waiting (max %d seconds) for system process `%s' to stop...",
642198408Sjhb	    kproc_shutdown_wait, p->p_comm);
643172836Sjulian	error = kproc_suspend(p, kproc_shutdown_wait * hz);
64455539Sluoqi
64555539Sluoqi	if (error == EWOULDBLOCK)
646132866Snjl		printf("timed out\n");
64755539Sluoqi	else
648132866Snjl		printf("done\n");
64955539Sluoqi}
65093496Sphk
651173004Sjulianvoid
652173004Sjuliankthread_shutdown(void *arg, int howto)
653173004Sjulian{
654173004Sjulian	struct thread *td;
655173004Sjulian	int error;
656173004Sjulian
657173004Sjulian	if (panicstr)
658173004Sjulian		return;
659173004Sjulian
660173004Sjulian	td = (struct thread *)arg;
661173004Sjulian	printf("Waiting (max %d seconds) for system thread `%s' to stop...",
662198408Sjhb	    kproc_shutdown_wait, td->td_name);
663173004Sjulian	error = kthread_suspend(td, kproc_shutdown_wait * hz);
664173004Sjulian
665173004Sjulian	if (error == EWOULDBLOCK)
666173004Sjulian		printf("timed out\n");
667173004Sjulian	else
668173004Sjulian		printf("done\n");
669173004Sjulian}
670173004Sjulian
67193496Sphk/* Registration of dumpers */
67293496Sphkint
67393496Sphkset_dumper(struct dumperinfo *di)
67493496Sphk{
675110859Salfred
67693496Sphk	if (di == NULL) {
67793496Sphk		bzero(&dumper, sizeof dumper);
67893496Sphk		return (0);
67993496Sphk	}
68093496Sphk	if (dumper.dumper != NULL)
68193496Sphk		return (EBUSY);
68293496Sphk	dumper = *di;
68393496Sphk	return (0);
68493496Sphk}
68593496Sphk
686175768Sru/* Call dumper with bounds checking. */
687175768Sruint
688175768Srudump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
689175768Sru    off_t offset, size_t length)
690175768Sru{
691175768Sru
692175768Sru	if (length != 0 && (offset < di->mediaoffset ||
693175768Sru	    offset - di->mediaoffset + length > di->mediasize)) {
694175768Sru		printf("Attempt to write outside dump device boundaries.\n");
695175768Sru		return (ENXIO);
696175768Sru	}
697175768Sru	return (di->dumper(di->priv, virtual, physical, offset, length));
698175768Sru}
699175768Sru
70093496Sphkvoid
701183527Spetermkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
702183527Speter    uint64_t dumplen, uint32_t blksz)
703183527Speter{
704183527Speter
705183527Speter	bzero(kdh, sizeof(*kdh));
706183527Speter	strncpy(kdh->magic, magic, sizeof(kdh->magic));
707183527Speter	strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
708183527Speter	kdh->version = htod32(KERNELDUMPVERSION);
709183527Speter	kdh->architectureversion = htod32(archver);
710183527Speter	kdh->dumplength = htod64(dumplen);
711183527Speter	kdh->dumptime = htod64(time_second);
712183527Speter	kdh->blocksize = htod32(blksz);
713194118Sjamie	strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
714183527Speter	strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
715183527Speter	if (panicstr != NULL)
716183527Speter		strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
717183527Speter	kdh->parity = kerneldump_parity(kdh);
718183527Speter}
719