kern_shutdown.c revision 235777
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 235777 2012-05-22 07:23:41Z harti $");
39116182Sobrien
40174921Srwatson#include "opt_ddb.h"
41131927Smarcel#include "opt_kdb.h"
4228976Sbde#include "opt_panic.h"
43134649Sscottl#include "opt_sched.h"
44221173Sattilio#include "opt_watchdog.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>
68225448Sattilio#include <sys/vnode.h>
69221173Sattilio#ifdef SW_WATCHDOG
70221173Sattilio#include <sys/watchdog.h>
71221173Sattilio#endif
7217658Sjulian
73174921Srwatson#include <ddb/ddb.h>
74174921Srwatson
75118990Smarcel#include <machine/cpu.h>
7694169Sphk#include <machine/pcb.h>
7791778Sjake#include <machine/smp.h>
7817658Sjulian
79163606Srwatson#include <security/mac/mac_framework.h>
80163606Srwatson
81157628Spjd#include <vm/vm.h>
82157628Spjd#include <vm/vm_object.h>
83157628Spjd#include <vm/vm_page.h>
84157628Spjd#include <vm/vm_pager.h>
85157628Spjd#include <vm/swap_pager.h>
86157628Spjd
8717658Sjulian#include <sys/signalvar.h>
8817658Sjulian
8917658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
9017658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
9117658Sjulian#endif
9217658Sjulian
9317658Sjulian/*
9417658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
9517658Sjulian * ANSI and traditional C compilers.
9617658Sjulian */
9717658Sjulian#include <machine/stdarg.h>
9817658Sjulian
99131927Smarcel#ifdef KDB
100131927Smarcel#ifdef KDB_UNATTENDED
10142135Smsmithint debugger_on_panic = 0;
10217658Sjulian#else
10342135Smsmithint debugger_on_panic = 1;
10417658Sjulian#endif
105228475SobrienSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic,
106228487Sobrien    CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN,
107228487Sobrien    &debugger_on_panic, 0, "Run debugger on kernel panic");
108213322SavgTUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic);
109103647Sjhb
110131927Smarcel#ifdef KDB_TRACE
111213322Savgstatic int trace_on_panic = 1;
112103647Sjhb#else
113213322Savgstatic int trace_on_panic = 0;
11417658Sjulian#endif
115228475SobrienSYSCTL_INT(_debug, OID_AUTO, trace_on_panic,
116228487Sobrien    CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN,
117228487Sobrien    &trace_on_panic, 0, "Print stack trace on kernel panic");
118213322SavgTUNABLE_INT("debug.trace_on_panic", &trace_on_panic);
119131927Smarcel#endif /* KDB */
12017658Sjulian
121213322Savgstatic int sync_on_panic = 0;
122213322SavgSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
12385202Speter	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
124213322SavgTUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
12585202Speter
126229854Savgstatic int stop_scheduler_on_panic = 1;
127228424SavgSYSCTL_INT(_kern, OID_AUTO, stop_scheduler_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
128228424Savg    &stop_scheduler_on_panic, 0, "stop scheduler upon entering panic");
129228424SavgTUNABLE_INT("kern.stop_scheduler_on_panic", &stop_scheduler_on_panic);
130228424Savg
131227309Sedstatic SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,
132227309Sed    "Shutdown environment");
13343436Smsmith
134225448Sattilio#ifndef DIAGNOSTIC
135225448Sattiliostatic int show_busybufs;
136225448Sattilio#else
137225448Sattiliostatic int show_busybufs = 1;
138225448Sattilio#endif
139225448SattilioSYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
140225448Sattilio	&show_busybufs, 0, "");
141225448Sattilio
14217658Sjulian/*
14317658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
14417658Sjulian * to indicate that the kernel has already called panic.
14517658Sjulian */
14617658Sjulianconst char *panicstr;
14717658Sjulian
14893496Sphkint dumping;				/* system is dumping */
149155383Sjeffint rebooting;				/* system is rebooting */
15093496Sphkstatic struct dumperinfo dumper;	/* our selected dumper */
15167093Sps
152131927Smarcel/* Context information for dump-debuggers. */
153131927Smarcelstatic struct pcb dumppcb;		/* Registers. */
154235777Shartilwpid_t dumptid;			/* Thread ID. */
155131927Smarcel
15665395Speterstatic void poweroff_wait(void *, int);
15765395Speterstatic void shutdown_halt(void *junk, int howto);
15865395Speterstatic void shutdown_panic(void *junk, int howto);
15965395Speterstatic void shutdown_reset(void *junk, int howto);
16017658Sjulian
16150107Smsmith/* register various local shutdown events */
162110859Salfredstatic void
16350107Smsmithshutdown_conf(void *unused)
16450107Smsmith{
165110859Salfred
166110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
167214279Sbrucec	    SHUTDOWN_PRI_FIRST);
168110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
169110859Salfred	    SHUTDOWN_PRI_LAST + 100);
170110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
171110859Salfred	    SHUTDOWN_PRI_LAST + 100);
172110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
173110859Salfred	    SHUTDOWN_PRI_LAST + 200);
17450107Smsmith}
17548868Sphk
176177253SrwatsonSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
17750107Smsmith
17817658Sjulian/*
179167211Srwatson * The system call that results in a reboot.
18017658Sjulian */
18182749Sdillon/* ARGSUSED */
18217658Sjulianint
183225617Skmacysys_reboot(struct thread *td, struct reboot_args *uap)
18417658Sjulian{
18517658Sjulian	int error;
18617658Sjulian
187106024Srwatson	error = 0;
188106024Srwatson#ifdef MAC
189172930Srwatson	error = mac_system_check_reboot(td->td_ucred, uap->opt);
190106024Srwatson#endif
191106024Srwatson	if (error == 0)
192164033Srwatson		error = priv_check(td, PRIV_REBOOT);
193106024Srwatson	if (error == 0) {
194106024Srwatson		mtx_lock(&Giant);
195214004Smarcel		kern_reboot(uap->opt);
196106024Srwatson		mtx_unlock(&Giant);
197106024Srwatson	}
19882749Sdillon	return (error);
19917658Sjulian}
20017658Sjulian
20117658Sjulian/*
20217658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
20317658Sjulian */
20465268Smsmithstatic int shutdown_howto = 0;
20565268Smsmith
20617658Sjulianvoid
20765268Smsmithshutdown_nice(int howto)
20817658Sjulian{
209110859Salfred
21065268Smsmith	shutdown_howto = howto;
211110859Salfred
21217658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
21317658Sjulian	if (initproc != NULL) {
21473913Sjhb		PROC_LOCK(initproc);
215225617Skmacy		kern_psignal(initproc, SIGINT);
21673913Sjhb		PROC_UNLOCK(initproc);
21717658Sjulian	} else {
21817658Sjulian		/* No init(8) running, so simply reboot */
219214004Smarcel		kern_reboot(RB_NOSYNC);
22017658Sjulian	}
22117658Sjulian	return;
22217658Sjulian}
22317658Sjulianstatic int	waittime = -1;
22417658Sjulian
22554233Sphkstatic void
22665395Speterprint_uptime(void)
22754233Sphk{
22854233Sphk	int f;
22954233Sphk	struct timespec ts;
23054233Sphk
23154233Sphk	getnanouptime(&ts);
23254233Sphk	printf("Uptime: ");
23354233Sphk	f = 0;
23454233Sphk	if (ts.tv_sec >= 86400) {
23565764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
23654233Sphk		ts.tv_sec %= 86400;
23754233Sphk		f = 1;
23854233Sphk	}
23954233Sphk	if (f || ts.tv_sec >= 3600) {
24065764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
24154233Sphk		ts.tv_sec %= 3600;
24254233Sphk		f = 1;
24354233Sphk	}
24454233Sphk	if (f || ts.tv_sec >= 60) {
24565764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
24654233Sphk		ts.tv_sec %= 60;
24754233Sphk		f = 1;
24854233Sphk	}
24965764Sjhb	printf("%lds\n", (long)ts.tv_sec);
25054233Sphk}
25154233Sphk
252222801Smarcelint
253222801Smarceldoadump(boolean_t textdump)
25494169Sphk{
255222801Smarcel	boolean_t coredump;
256110859Salfred
257222801Smarcel	if (dumping)
258222801Smarcel		return (EBUSY);
259222801Smarcel	if (dumper.dumper == NULL)
260222801Smarcel		return (ENXIO);
261132412Sjulian
26294169Sphk	savectx(&dumppcb);
263131927Smarcel	dumptid = curthread->td_tid;
26494169Sphk	dumping++;
265222801Smarcel
266222801Smarcel	coredump = TRUE;
267174921Srwatson#ifdef DDB
268222801Smarcel	if (textdump && textdump_pending) {
269222801Smarcel		coredump = FALSE;
270174921Srwatson		textdump_dumpsys(&dumper);
271222801Smarcel	}
272174921Srwatson#endif
273222801Smarcel	if (coredump)
274174921Srwatson		dumpsys(&dumper);
275222801Smarcel
276176788Sru	dumping--;
277222801Smarcel	return (0);
27894169Sphk}
27994169Sphk
280149875Struckmanstatic int
281149875Struckmanisbufbusy(struct buf *bp)
282149875Struckman{
283149875Struckman	if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
284175486Sattilio	    BUF_ISLOCKED(bp)) ||
285149875Struckman	    ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
286149875Struckman		return (1);
287149875Struckman	return (0);
288149875Struckman}
289149875Struckman
29017658Sjulian/*
291137329Snjl * Shutdown the system cleanly to prepare for reboot, halt, or power off.
29217658Sjulian */
293214004Smarcelvoid
294214004Smarcelkern_reboot(int howto)
29517658Sjulian{
296133763Struckman	static int first_buf_printf = 1;
29717658Sjulian
298137375Smarcel#if defined(SMP)
299137329Snjl	/*
300137329Snjl	 * Bind us to CPU 0 so that all shutdown code runs there.  Some
301137329Snjl	 * systems don't shutdown properly (i.e., ACPI power off) if we
302137329Snjl	 * run on another processor.
303137329Snjl	 */
304228424Savg	if (!SCHEDULER_STOPPED()) {
305228424Savg		thread_lock(curthread);
306228424Savg		sched_bind(curthread, 0);
307228424Savg		thread_unlock(curthread);
308228424Savg		KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
309228424Savg	}
310137263Speter#endif
311155383Sjeff	/* We're in the process of rebooting. */
312155383Sjeff	rebooting = 1;
313137263Speter
31465268Smsmith	/* collect extra flags that shutdown_nice might have set */
31565268Smsmith	howto |= shutdown_howto;
31665268Smsmith
31782119Sjhb	/* We are out of the debugger now. */
318131927Smarcel	kdb_active = 0;
31982119Sjhb
32027997Sjulian	/*
32127997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
32227997Sjulian	 */
32350107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
32427997Sjulian
32527997Sjulian	/*
32627997Sjulian	 * Now sync filesystems
32727997Sjulian	 */
32817658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
32917658Sjulian		register struct buf *bp;
33065707Sjasone		int iter, nbusy, pbusy;
331131481Sjhb#ifndef PREEMPTION
33265707Sjasone		int subiter;
333131481Sjhb#endif
33417658Sjulian
33517658Sjulian		waittime = 0;
33617658Sjulian
337221173Sattilio#ifdef SW_WATCHDOG
338221173Sattilio		wdog_kern_pat(WD_LASTVAL);
339221173Sattilio#endif
340225617Skmacy		sys_sync(curthread, NULL);
34117658Sjulian
34234266Sjulian		/*
34334266Sjulian		 * With soft updates, some buffers that are
34434266Sjulian		 * written will be remarked as dirty until other
34534266Sjulian		 * buffers are written.
34634266Sjulian		 */
34765707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
34817658Sjulian			nbusy = 0;
349149875Struckman			for (bp = &buf[nbuf]; --bp >= buf; )
350149875Struckman				if (isbufbusy(bp))
35117658Sjulian					nbusy++;
352133763Struckman			if (nbusy == 0) {
353133763Struckman				if (first_buf_printf)
354136115Sphk					printf("All buffers synced.");
35517658Sjulian				break;
356133763Struckman			}
357133763Struckman			if (first_buf_printf) {
358133763Struckman				printf("Syncing disks, buffers remaining... ");
359133763Struckman				first_buf_printf = 0;
360133763Struckman			}
36117658Sjulian			printf("%d ", nbusy);
36265707Sjasone			if (nbusy < pbusy)
36365707Sjasone				iter = 0;
36465707Sjasone			pbusy = nbusy;
365221173Sattilio#ifdef SW_WATCHDOG
366221173Sattilio			wdog_kern_pat(WD_LASTVAL);
367221173Sattilio#endif
368225617Skmacy			sys_sync(curthread, NULL);
369131481Sjhb
370131481Sjhb#ifdef PREEMPTION
371131481Sjhb			/*
372131481Sjhb			 * Drop Giant and spin for a while to allow
373131481Sjhb			 * interrupt threads to run.
374131481Sjhb			 */
375131481Sjhb			DROP_GIANT();
37634266Sjulian			DELAY(50000 * iter);
377131481Sjhb			PICKUP_GIANT();
378131481Sjhb#else
379131481Sjhb			/*
380131481Sjhb			 * Drop Giant and context switch several times to
381131481Sjhb			 * allow interrupt threads to run.
382131481Sjhb			 */
383131481Sjhb			DROP_GIANT();
384131481Sjhb			for (subiter = 0; subiter < 50 * iter; subiter++) {
385170307Sjeff				thread_lock(curthread);
386131481Sjhb				mi_switch(SW_VOL, NULL);
387170307Sjeff				thread_unlock(curthread);
388131481Sjhb				DELAY(1000);
389131481Sjhb			}
390131481Sjhb			PICKUP_GIANT();
391131481Sjhb#endif
39217658Sjulian		}
393133418Snjl		printf("\n");
39441137Smsmith		/*
39541137Smsmith		 * Count only busy local buffers to prevent forcing
39641137Smsmith		 * a fsck if we're just a client of a wedged NFS server
39741137Smsmith		 */
39841137Smsmith		nbusy = 0;
39941137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
400149875Struckman			if (isbufbusy(bp)) {
401137186Sphk#if 0
402137186Sphk/* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
403130640Sphk				if (bp->b_dev == NULL) {
40453452Sphk					TAILQ_REMOVE(&mountlist,
40548225Smckusick					    bp->b_vp->v_mount, mnt_list);
40653023Sphk					continue;
40753023Sphk				}
408137186Sphk#endif
40953023Sphk				nbusy++;
410225448Sattilio				if (show_busybufs > 0) {
411225448Sattilio					printf(
412225448Sattilio	    "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
413225448Sattilio					    nbusy, bp, bp->b_vp, bp->b_flags,
414225448Sattilio					    (intmax_t)bp->b_blkno,
415225448Sattilio					    (intmax_t)bp->b_lblkno);
416225448Sattilio					BUF_LOCKPRINTINFO(bp);
417225448Sattilio					if (show_busybufs > 1)
418225448Sattilio						vn_printf(bp->b_vp,
419225448Sattilio						    "vnode content: ");
420225448Sattilio				}
42146568Speter			}
42241137Smsmith		}
42317658Sjulian		if (nbusy) {
42417658Sjulian			/*
42517658Sjulian			 * Failed to sync all blocks. Indicate this and don't
42617658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
42717658Sjulian			 */
428133763Struckman			printf("Giving up on %d buffers\n", nbusy);
42917658Sjulian			DELAY(5000000);	/* 5 seconds */
43017658Sjulian		} else {
431133763Struckman			if (!first_buf_printf)
432133763Struckman				printf("Final sync complete\n");
43317658Sjulian			/*
43417658Sjulian			 * Unmount filesystems
43517658Sjulian			 */
43617658Sjulian			if (panicstr == 0)
43717658Sjulian				vfs_unmountall();
43817658Sjulian		}
439157628Spjd		swapoff_all();
44039237Sgibbs		DELAY(100000);		/* wait for console output to finish */
44117658Sjulian	}
44227997Sjulian
44354233Sphk	print_uptime();
44454233Sphk
445228632Savg	cngrab();
446228632Savg
44727997Sjulian	/*
44827997Sjulian	 * Ok, now do things that assume all filesystem activity has
44927997Sjulian	 * been completed.
45027997Sjulian	 */
45150107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
452137329Snjl
453132412Sjulian	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
454222801Smarcel		doadump(TRUE);
45539237Sgibbs
45639237Sgibbs	/* Now that we're going to really halt the system... */
45750107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
45839237Sgibbs
45950107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
46050107Smsmith	/* NOTREACHED */
46150107Smsmith}
46250107Smsmith
46350107Smsmith/*
46450107Smsmith * If the shutdown was a clean halt, behave accordingly.
46550107Smsmith */
46650107Smsmithstatic void
46750107Smsmithshutdown_halt(void *junk, int howto)
46850107Smsmith{
469110859Salfred
47017658Sjulian	if (howto & RB_HALT) {
47117658Sjulian		printf("\n");
47217658Sjulian		printf("The operating system has halted.\n");
47317658Sjulian		printf("Please press any key to reboot.\n\n");
47419274Sjulian		switch (cngetc()) {
47519274Sjulian		case -1:		/* No console, just die */
47619274Sjulian			cpu_halt();
47719274Sjulian			/* NOTREACHED */
47819274Sjulian		default:
47939237Sgibbs			howto &= ~RB_HALT;
48019274Sjulian			break;
48119274Sjulian		}
48250107Smsmith	}
48350107Smsmith}
48417658Sjulian
48550107Smsmith/*
48650107Smsmith * Check to see if the system paniced, pause and then reboot
48750107Smsmith * according to the specified delay.
48850107Smsmith */
48950107Smsmithstatic void
49050107Smsmithshutdown_panic(void *junk, int howto)
49150107Smsmith{
49250107Smsmith	int loop;
49350107Smsmith
49450107Smsmith	if (howto & RB_DUMP) {
49539237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
49639237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
49739237Sgibbs				printf("Automatic reboot in %d seconds - "
49839237Sgibbs				       "press a key on the console to abort\n",
49939237Sgibbs					PANIC_REBOOT_WAIT_TIME);
50039237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
50139237Sgibbs				     loop > 0; --loop) {
50239237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
50339237Sgibbs					/* Did user type a key? */
50439237Sgibbs					if (cncheckc() != -1)
50539237Sgibbs						break;
50617658Sjulian				}
50739237Sgibbs				if (!loop)
50850107Smsmith					return;
50917658Sjulian			}
51039237Sgibbs		} else { /* zero time specified - reboot NOW */
51150107Smsmith			return;
51217658Sjulian		}
51389522Snik		printf("--> Press a key on the console to reboot,\n");
51489522Snik		printf("--> or switch off the system now.\n");
51539237Sgibbs		cngetc();
51617658Sjulian	}
51750107Smsmith}
51850107Smsmith
51950107Smsmith/*
52050107Smsmith * Everything done, now reset
52150107Smsmith */
52250107Smsmithstatic void
52350107Smsmithshutdown_reset(void *junk, int howto)
52450107Smsmith{
525110859Salfred
526206878Sattilio	printf("Rebooting...\n");
527206878Sattilio	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
528206878Sattilio
529196196Sattilio	/*
530206878Sattilio	 * Acquiring smp_ipi_mtx here has a double effect:
531206878Sattilio	 * - it disables interrupts avoiding CPU0 preemption
532206878Sattilio	 *   by fast handlers (thus deadlocking  against other CPUs)
533206878Sattilio	 * - it avoids deadlocks against smp_rendezvous() or, more
534206878Sattilio	 *   generally, threads busy-waiting, with this spinlock held,
535206878Sattilio	 *   and waiting for responses by threads on other CPUs
536206878Sattilio	 *   (ie. smp_tlb_shootdown()).
537206897Sattilio	 *
538206897Sattilio	 * For the !SMP case it just needs to handle the former problem.
539196196Sattilio	 */
540206897Sattilio#ifdef SMP
541206878Sattilio	mtx_lock_spin(&smp_ipi_mtx);
542206897Sattilio#else
543206897Sattilio	spinlock_enter();
544206897Sattilio#endif
545196196Sattilio
54617677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
54717658Sjulian	cpu_reset();
54850107Smsmith	/* NOTREACHED */ /* assuming reset worked */
54917658Sjulian}
55017658Sjulian
55117658Sjulian/*
55217658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
55317658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
55417658Sjulian * the disks as this often leads to recursive panics.
55517658Sjulian */
55617658Sjulianvoid
557130164Sphkpanic(const char *fmt, ...)
55817658Sjulian{
559213648Savg#ifdef SMP
560213648Savg	static volatile u_int panic_cpu = NOCPU;
561228424Savg	cpuset_t other_cpus;
562213648Savg#endif
563100209Sgallatin	struct thread *td = curthread;
564103647Sjhb	int bootopt, newpanic;
56517658Sjulian	va_list ap;
56638874Sache	static char buf[256];
56717658Sjulian
568228424Savg	if (stop_scheduler_on_panic)
569228424Savg		spinlock_enter();
570228424Savg	else
571228424Savg		critical_enter();
572228424Savg
57365557Sjasone#ifdef SMP
57482115Sjhb	/*
57582115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
576101155Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
577101155Sjhb	 * panic_cpu if we are spinning in case the panic on the first
57882115Sjhb	 * CPU is canceled.
57982115Sjhb	 */
580101155Sjhb	if (panic_cpu != PCPU_GET(cpuid))
581101155Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
582101155Sjhb		    PCPU_GET(cpuid)) == 0)
583101155Sjhb			while (panic_cpu != NOCPU)
584101155Sjhb				; /* nothing */
585228424Savg
586228424Savg	if (stop_scheduler_on_panic) {
587228424Savg		if (panicstr == NULL && !kdb_active) {
588228424Savg			other_cpus = all_cpus;
589228424Savg			CPU_CLR(PCPU_GET(cpuid), &other_cpus);
590228424Savg			stop_cpus_hard(other_cpus);
591228424Savg		}
592228424Savg
593228424Savg		/*
594228424Savg		 * We set stop_scheduler here and not in the block above,
595228424Savg		 * because we want to ensure that if panic has been called and
596228424Savg		 * stop_scheduler_on_panic is true, then stop_scheduler will
597228424Savg		 * always be set.  Even if panic has been entered from kdb.
598228424Savg		 */
599230643Sattilio		td->td_stopsched = 1;
600228424Savg	}
60165557Sjasone#endif
60265557Sjasone
603222865Sattilio	bootopt = RB_AUTOBOOT;
604103647Sjhb	newpanic = 0;
60517658Sjulian	if (panicstr)
60617658Sjulian		bootopt |= RB_NOSYNC;
607103647Sjhb	else {
608222865Sattilio		bootopt |= RB_DUMP;
60917658Sjulian		panicstr = fmt;
610103647Sjhb		newpanic = 1;
611103647Sjhb	}
61217658Sjulian
61317658Sjulian	va_start(ap, fmt);
614116398Siedowse	if (newpanic) {
615116398Siedowse		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
61638874Sache		panicstr = buf;
617228632Savg		cngrab();
618130164Sphk		printf("panic: %s\n", buf);
619116398Siedowse	} else {
620116398Siedowse		printf("panic: ");
621116398Siedowse		vprintf(fmt, ap);
622130164Sphk		printf("\n");
623116398Siedowse	}
62417658Sjulian	va_end(ap);
62526100Sfsmp#ifdef SMP
626134089Sjhb	printf("cpuid = %d\n", PCPU_GET(cpuid));
62726100Sfsmp#endif
62817658Sjulian
629131927Smarcel#ifdef KDB
630103647Sjhb	if (newpanic && trace_on_panic)
631131927Smarcel		kdb_backtrace();
63217658Sjulian	if (debugger_on_panic)
633174898Srwatson		kdb_enter(KDB_WHY_PANIC, "panic");
63417658Sjulian#endif
635170307Sjeff	/*thread_lock(td); */
636100209Sgallatin	td->td_flags |= TDF_INPANIC;
637170307Sjeff	/* thread_unlock(td); */
63885202Speter	if (!sync_on_panic)
63985202Speter		bootopt |= RB_NOSYNC;
640228424Savg	if (!stop_scheduler_on_panic)
641228424Savg		critical_exit();
642214004Smarcel	kern_reboot(bootopt);
64317658Sjulian}
64417658Sjulian
64517768Sjulian/*
64643436Smsmith * Support for poweroff delay.
647197071Sn_hibma *
648197071Sn_hibma * Please note that setting this delay too short might power off your machine
649197071Sn_hibma * before the write cache on your hard disk has been flushed, leading to
650197071Sn_hibma * soft-updates inconsistencies.
65143436Smsmith */
65254248Smsmith#ifndef POWEROFF_DELAY
65354248Smsmith# define POWEROFF_DELAY 5000
65454248Smsmith#endif
65554248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
65654248Smsmith
65743436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
658228449Seadler    &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)");
65943436Smsmith
660110859Salfredstatic void
66150107Smsmithpoweroff_wait(void *junk, int howto)
66243436Smsmith{
663110859Salfred
664110859Salfred	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
66543436Smsmith		return;
66643436Smsmith	DELAY(poweroff_delay * 1000);
66743436Smsmith}
66855539Sluoqi
66955539Sluoqi/*
67055539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
67155539Sluoqi * points in their main loops prior to a system shutdown, so that they
67255539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
67355539Sluoqi * to cause sync to fail).  For each of these system processes, register
67455539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
67555539Sluoqi */
67655539Sluoqistatic int kproc_shutdown_wait = 60;
67755539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
678228449Seadler    &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process");
67955539Sluoqi
68055539Sluoqivoid
68170063Sjhbkproc_shutdown(void *arg, int howto)
68255539Sluoqi{
68355539Sluoqi	struct proc *p;
68455539Sluoqi	int error;
68555539Sluoqi
68655539Sluoqi	if (panicstr)
68755539Sluoqi		return;
68855539Sluoqi
68955539Sluoqi	p = (struct proc *)arg;
690132866Snjl	printf("Waiting (max %d seconds) for system process `%s' to stop...",
691198408Sjhb	    kproc_shutdown_wait, p->p_comm);
692172836Sjulian	error = kproc_suspend(p, kproc_shutdown_wait * hz);
69355539Sluoqi
69455539Sluoqi	if (error == EWOULDBLOCK)
695132866Snjl		printf("timed out\n");
69655539Sluoqi	else
697132866Snjl		printf("done\n");
69855539Sluoqi}
69993496Sphk
700173004Sjulianvoid
701173004Sjuliankthread_shutdown(void *arg, int howto)
702173004Sjulian{
703173004Sjulian	struct thread *td;
704173004Sjulian	int error;
705173004Sjulian
706173004Sjulian	if (panicstr)
707173004Sjulian		return;
708173004Sjulian
709173004Sjulian	td = (struct thread *)arg;
710173004Sjulian	printf("Waiting (max %d seconds) for system thread `%s' to stop...",
711198408Sjhb	    kproc_shutdown_wait, td->td_name);
712173004Sjulian	error = kthread_suspend(td, kproc_shutdown_wait * hz);
713173004Sjulian
714173004Sjulian	if (error == EWOULDBLOCK)
715173004Sjulian		printf("timed out\n");
716173004Sjulian	else
717173004Sjulian		printf("done\n");
718173004Sjulian}
719173004Sjulian
72093496Sphk/* Registration of dumpers */
72193496Sphkint
72293496Sphkset_dumper(struct dumperinfo *di)
72393496Sphk{
724110859Salfred
72593496Sphk	if (di == NULL) {
72693496Sphk		bzero(&dumper, sizeof dumper);
72793496Sphk		return (0);
72893496Sphk	}
72993496Sphk	if (dumper.dumper != NULL)
73093496Sphk		return (EBUSY);
73193496Sphk	dumper = *di;
73293496Sphk	return (0);
73393496Sphk}
73493496Sphk
735175768Sru/* Call dumper with bounds checking. */
736175768Sruint
737175768Srudump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
738175768Sru    off_t offset, size_t length)
739175768Sru{
740175768Sru
741175768Sru	if (length != 0 && (offset < di->mediaoffset ||
742175768Sru	    offset - di->mediaoffset + length > di->mediasize)) {
743225516Sattilio		printf("Attempt to write outside dump device boundaries.\n"
744225516Sattilio	    "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
745225516Sattilio		    (intmax_t)offset, (intmax_t)di->mediaoffset,
746225516Sattilio		    (uintmax_t)length, (intmax_t)di->mediasize);
747225516Sattilio		return (ENOSPC);
748175768Sru	}
749175768Sru	return (di->dumper(di->priv, virtual, physical, offset, length));
750175768Sru}
751175768Sru
75293496Sphkvoid
753183527Spetermkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
754183527Speter    uint64_t dumplen, uint32_t blksz)
755183527Speter{
756183527Speter
757183527Speter	bzero(kdh, sizeof(*kdh));
758183527Speter	strncpy(kdh->magic, magic, sizeof(kdh->magic));
759183527Speter	strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
760183527Speter	kdh->version = htod32(KERNELDUMPVERSION);
761183527Speter	kdh->architectureversion = htod32(archver);
762183527Speter	kdh->dumplength = htod64(dumplen);
763183527Speter	kdh->dumptime = htod64(time_second);
764183527Speter	kdh->blocksize = htod32(blksz);
765194118Sjamie	strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
766183527Speter	strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
767183527Speter	if (panicstr != NULL)
768183527Speter		strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
769183527Speter	kdh->parity = kerneldump_parity(kdh);
770183527Speter}
771