kern_shutdown.c revision 228449
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 228449 2011-12-13 00:38:50Z eadler $");
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
105213322SavgSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
10646381Sbillf	&debugger_on_panic, 0, "Run debugger on kernel panic");
107213322SavgTUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic);
108103647Sjhb
109131927Smarcel#ifdef KDB_TRACE
110213322Savgstatic int trace_on_panic = 1;
111103647Sjhb#else
112213322Savgstatic int trace_on_panic = 0;
11317658Sjulian#endif
114213322SavgSYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
115103647Sjhb	&trace_on_panic, 0, "Print stack trace on kernel panic");
116213322SavgTUNABLE_INT("debug.trace_on_panic", &trace_on_panic);
117131927Smarcel#endif /* KDB */
11817658Sjulian
119213322Savgstatic int sync_on_panic = 0;
120213322SavgSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
12185202Speter	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
122213322SavgTUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
12385202Speter
124228424Savgstatic int stop_scheduler_on_panic = 0;
125228424SavgSYSCTL_INT(_kern, OID_AUTO, stop_scheduler_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
126228424Savg    &stop_scheduler_on_panic, 0, "stop scheduler upon entering panic");
127228424SavgTUNABLE_INT("kern.stop_scheduler_on_panic", &stop_scheduler_on_panic);
128228424Savg
129227309Sedstatic SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,
130227309Sed    "Shutdown environment");
13143436Smsmith
132225448Sattilio#ifndef DIAGNOSTIC
133225448Sattiliostatic int show_busybufs;
134225448Sattilio#else
135225448Sattiliostatic int show_busybufs = 1;
136225448Sattilio#endif
137225448SattilioSYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
138225448Sattilio	&show_busybufs, 0, "");
139225448Sattilio
14017658Sjulian/*
14117658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
14217658Sjulian * to indicate that the kernel has already called panic.
14317658Sjulian */
14417658Sjulianconst char *panicstr;
14517658Sjulian
146228424Savgint stop_scheduler;			/* system stopped CPUs for panic */
14793496Sphkint dumping;				/* system is dumping */
148155383Sjeffint rebooting;				/* system is rebooting */
14993496Sphkstatic struct dumperinfo dumper;	/* our selected dumper */
15067093Sps
151131927Smarcel/* Context information for dump-debuggers. */
152131927Smarcelstatic struct pcb dumppcb;		/* Registers. */
153131927Smarcelstatic lwpid_t dumptid;			/* Thread ID. */
154131927Smarcel
15565395Speterstatic void poweroff_wait(void *, int);
15665395Speterstatic void shutdown_halt(void *junk, int howto);
15765395Speterstatic void shutdown_panic(void *junk, int howto);
15865395Speterstatic void shutdown_reset(void *junk, int howto);
15917658Sjulian
16050107Smsmith/* register various local shutdown events */
161110859Salfredstatic void
16250107Smsmithshutdown_conf(void *unused)
16350107Smsmith{
164110859Salfred
165110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
166214279Sbrucec	    SHUTDOWN_PRI_FIRST);
167110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
168110859Salfred	    SHUTDOWN_PRI_LAST + 100);
169110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
170110859Salfred	    SHUTDOWN_PRI_LAST + 100);
171110859Salfred	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
172110859Salfred	    SHUTDOWN_PRI_LAST + 200);
17350107Smsmith}
17448868Sphk
175177253SrwatsonSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
17650107Smsmith
17717658Sjulian/*
178167211Srwatson * The system call that results in a reboot.
17917658Sjulian */
18082749Sdillon/* ARGSUSED */
18117658Sjulianint
182225617Skmacysys_reboot(struct thread *td, struct reboot_args *uap)
18317658Sjulian{
18417658Sjulian	int error;
18517658Sjulian
186106024Srwatson	error = 0;
187106024Srwatson#ifdef MAC
188172930Srwatson	error = mac_system_check_reboot(td->td_ucred, uap->opt);
189106024Srwatson#endif
190106024Srwatson	if (error == 0)
191164033Srwatson		error = priv_check(td, PRIV_REBOOT);
192106024Srwatson	if (error == 0) {
193106024Srwatson		mtx_lock(&Giant);
194214004Smarcel		kern_reboot(uap->opt);
195106024Srwatson		mtx_unlock(&Giant);
196106024Srwatson	}
19782749Sdillon	return (error);
19817658Sjulian}
19917658Sjulian
20017658Sjulian/*
20117658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
20217658Sjulian */
20365268Smsmithstatic int shutdown_howto = 0;
20465268Smsmith
20517658Sjulianvoid
20665268Smsmithshutdown_nice(int howto)
20717658Sjulian{
208110859Salfred
20965268Smsmith	shutdown_howto = howto;
210110859Salfred
21117658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
21217658Sjulian	if (initproc != NULL) {
21373913Sjhb		PROC_LOCK(initproc);
214225617Skmacy		kern_psignal(initproc, SIGINT);
21573913Sjhb		PROC_UNLOCK(initproc);
21617658Sjulian	} else {
21717658Sjulian		/* No init(8) running, so simply reboot */
218214004Smarcel		kern_reboot(RB_NOSYNC);
21917658Sjulian	}
22017658Sjulian	return;
22117658Sjulian}
22217658Sjulianstatic int	waittime = -1;
22317658Sjulian
22454233Sphkstatic void
22565395Speterprint_uptime(void)
22654233Sphk{
22754233Sphk	int f;
22854233Sphk	struct timespec ts;
22954233Sphk
23054233Sphk	getnanouptime(&ts);
23154233Sphk	printf("Uptime: ");
23254233Sphk	f = 0;
23354233Sphk	if (ts.tv_sec >= 86400) {
23465764Sjhb		printf("%ldd", (long)ts.tv_sec / 86400);
23554233Sphk		ts.tv_sec %= 86400;
23654233Sphk		f = 1;
23754233Sphk	}
23854233Sphk	if (f || ts.tv_sec >= 3600) {
23965764Sjhb		printf("%ldh", (long)ts.tv_sec / 3600);
24054233Sphk		ts.tv_sec %= 3600;
24154233Sphk		f = 1;
24254233Sphk	}
24354233Sphk	if (f || ts.tv_sec >= 60) {
24465764Sjhb		printf("%ldm", (long)ts.tv_sec / 60);
24554233Sphk		ts.tv_sec %= 60;
24654233Sphk		f = 1;
24754233Sphk	}
24865764Sjhb	printf("%lds\n", (long)ts.tv_sec);
24954233Sphk}
25054233Sphk
251222801Smarcelint
252222801Smarceldoadump(boolean_t textdump)
25394169Sphk{
254222801Smarcel	boolean_t coredump;
255110859Salfred
256222801Smarcel	if (dumping)
257222801Smarcel		return (EBUSY);
258222801Smarcel	if (dumper.dumper == NULL)
259222801Smarcel		return (ENXIO);
260132412Sjulian
26194169Sphk	savectx(&dumppcb);
262131927Smarcel	dumptid = curthread->td_tid;
26394169Sphk	dumping++;
264222801Smarcel
265222801Smarcel	coredump = TRUE;
266174921Srwatson#ifdef DDB
267222801Smarcel	if (textdump && textdump_pending) {
268222801Smarcel		coredump = FALSE;
269174921Srwatson		textdump_dumpsys(&dumper);
270222801Smarcel	}
271174921Srwatson#endif
272222801Smarcel	if (coredump)
273174921Srwatson		dumpsys(&dumper);
274222801Smarcel
275176788Sru	dumping--;
276222801Smarcel	return (0);
27794169Sphk}
27894169Sphk
279149875Struckmanstatic int
280149875Struckmanisbufbusy(struct buf *bp)
281149875Struckman{
282149875Struckman	if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
283175486Sattilio	    BUF_ISLOCKED(bp)) ||
284149875Struckman	    ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
285149875Struckman		return (1);
286149875Struckman	return (0);
287149875Struckman}
288149875Struckman
28917658Sjulian/*
290137329Snjl * Shutdown the system cleanly to prepare for reboot, halt, or power off.
29117658Sjulian */
292214004Smarcelvoid
293214004Smarcelkern_reboot(int howto)
29417658Sjulian{
295133763Struckman	static int first_buf_printf = 1;
29617658Sjulian
297137375Smarcel#if defined(SMP)
298137329Snjl	/*
299137329Snjl	 * Bind us to CPU 0 so that all shutdown code runs there.  Some
300137329Snjl	 * systems don't shutdown properly (i.e., ACPI power off) if we
301137329Snjl	 * run on another processor.
302137329Snjl	 */
303228424Savg	if (!SCHEDULER_STOPPED()) {
304228424Savg		thread_lock(curthread);
305228424Savg		sched_bind(curthread, 0);
306228424Savg		thread_unlock(curthread);
307228424Savg		KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
308228424Savg	}
309137263Speter#endif
310155383Sjeff	/* We're in the process of rebooting. */
311155383Sjeff	rebooting = 1;
312137263Speter
31365268Smsmith	/* collect extra flags that shutdown_nice might have set */
31465268Smsmith	howto |= shutdown_howto;
31565268Smsmith
31682119Sjhb	/* We are out of the debugger now. */
317131927Smarcel	kdb_active = 0;
31882119Sjhb
31927997Sjulian	/*
32027997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
32127997Sjulian	 */
32250107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
32327997Sjulian
32427997Sjulian	/*
32527997Sjulian	 * Now sync filesystems
32627997Sjulian	 */
32717658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
32817658Sjulian		register struct buf *bp;
32965707Sjasone		int iter, nbusy, pbusy;
330131481Sjhb#ifndef PREEMPTION
33165707Sjasone		int subiter;
332131481Sjhb#endif
33317658Sjulian
33417658Sjulian		waittime = 0;
33517658Sjulian
336221173Sattilio#ifdef SW_WATCHDOG
337221173Sattilio		wdog_kern_pat(WD_LASTVAL);
338221173Sattilio#endif
339225617Skmacy		sys_sync(curthread, NULL);
34017658Sjulian
34134266Sjulian		/*
34234266Sjulian		 * With soft updates, some buffers that are
34334266Sjulian		 * written will be remarked as dirty until other
34434266Sjulian		 * buffers are written.
34534266Sjulian		 */
34665707Sjasone		for (iter = pbusy = 0; iter < 20; iter++) {
34717658Sjulian			nbusy = 0;
348149875Struckman			for (bp = &buf[nbuf]; --bp >= buf; )
349149875Struckman				if (isbufbusy(bp))
35017658Sjulian					nbusy++;
351133763Struckman			if (nbusy == 0) {
352133763Struckman				if (first_buf_printf)
353136115Sphk					printf("All buffers synced.");
35417658Sjulian				break;
355133763Struckman			}
356133763Struckman			if (first_buf_printf) {
357133763Struckman				printf("Syncing disks, buffers remaining... ");
358133763Struckman				first_buf_printf = 0;
359133763Struckman			}
36017658Sjulian			printf("%d ", nbusy);
36165707Sjasone			if (nbusy < pbusy)
36265707Sjasone				iter = 0;
36365707Sjasone			pbusy = nbusy;
364221173Sattilio#ifdef SW_WATCHDOG
365221173Sattilio			wdog_kern_pat(WD_LASTVAL);
366221173Sattilio#endif
367225617Skmacy			sys_sync(curthread, NULL);
368131481Sjhb
369131481Sjhb#ifdef PREEMPTION
370131481Sjhb			/*
371131481Sjhb			 * Drop Giant and spin for a while to allow
372131481Sjhb			 * interrupt threads to run.
373131481Sjhb			 */
374131481Sjhb			DROP_GIANT();
37534266Sjulian			DELAY(50000 * iter);
376131481Sjhb			PICKUP_GIANT();
377131481Sjhb#else
378131481Sjhb			/*
379131481Sjhb			 * Drop Giant and context switch several times to
380131481Sjhb			 * allow interrupt threads to run.
381131481Sjhb			 */
382131481Sjhb			DROP_GIANT();
383131481Sjhb			for (subiter = 0; subiter < 50 * iter; subiter++) {
384170307Sjeff				thread_lock(curthread);
385131481Sjhb				mi_switch(SW_VOL, NULL);
386170307Sjeff				thread_unlock(curthread);
387131481Sjhb				DELAY(1000);
388131481Sjhb			}
389131481Sjhb			PICKUP_GIANT();
390131481Sjhb#endif
39117658Sjulian		}
392133418Snjl		printf("\n");
39341137Smsmith		/*
39441137Smsmith		 * Count only busy local buffers to prevent forcing
39541137Smsmith		 * a fsck if we're just a client of a wedged NFS server
39641137Smsmith		 */
39741137Smsmith		nbusy = 0;
39841137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
399149875Struckman			if (isbufbusy(bp)) {
400137186Sphk#if 0
401137186Sphk/* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
402130640Sphk				if (bp->b_dev == NULL) {
40353452Sphk					TAILQ_REMOVE(&mountlist,
40448225Smckusick					    bp->b_vp->v_mount, mnt_list);
40553023Sphk					continue;
40653023Sphk				}
407137186Sphk#endif
40853023Sphk				nbusy++;
409225448Sattilio				if (show_busybufs > 0) {
410225448Sattilio					printf(
411225448Sattilio	    "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
412225448Sattilio					    nbusy, bp, bp->b_vp, bp->b_flags,
413225448Sattilio					    (intmax_t)bp->b_blkno,
414225448Sattilio					    (intmax_t)bp->b_lblkno);
415225448Sattilio					BUF_LOCKPRINTINFO(bp);
416225448Sattilio					if (show_busybufs > 1)
417225448Sattilio						vn_printf(bp->b_vp,
418225448Sattilio						    "vnode content: ");
419225448Sattilio				}
42046568Speter			}
42141137Smsmith		}
42217658Sjulian		if (nbusy) {
42317658Sjulian			/*
42417658Sjulian			 * Failed to sync all blocks. Indicate this and don't
42517658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
42617658Sjulian			 */
427133763Struckman			printf("Giving up on %d buffers\n", nbusy);
42817658Sjulian			DELAY(5000000);	/* 5 seconds */
42917658Sjulian		} else {
430133763Struckman			if (!first_buf_printf)
431133763Struckman				printf("Final sync complete\n");
43217658Sjulian			/*
43317658Sjulian			 * Unmount filesystems
43417658Sjulian			 */
43517658Sjulian			if (panicstr == 0)
43617658Sjulian				vfs_unmountall();
43717658Sjulian		}
438157628Spjd		swapoff_all();
43939237Sgibbs		DELAY(100000);		/* wait for console output to finish */
44017658Sjulian	}
44127997Sjulian
44254233Sphk	print_uptime();
44354233Sphk
44427997Sjulian	/*
44527997Sjulian	 * Ok, now do things that assume all filesystem activity has
44627997Sjulian	 * been completed.
44727997Sjulian	 */
44850107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
449137329Snjl
450132412Sjulian	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
451222801Smarcel		doadump(TRUE);
45239237Sgibbs
45339237Sgibbs	/* Now that we're going to really halt the system... */
45450107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
45539237Sgibbs
45650107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
45750107Smsmith	/* NOTREACHED */
45850107Smsmith}
45950107Smsmith
46050107Smsmith/*
46150107Smsmith * If the shutdown was a clean halt, behave accordingly.
46250107Smsmith */
46350107Smsmithstatic void
46450107Smsmithshutdown_halt(void *junk, int howto)
46550107Smsmith{
466110859Salfred
46717658Sjulian	if (howto & RB_HALT) {
46817658Sjulian		printf("\n");
46917658Sjulian		printf("The operating system has halted.\n");
47017658Sjulian		printf("Please press any key to reboot.\n\n");
47119274Sjulian		switch (cngetc()) {
47219274Sjulian		case -1:		/* No console, just die */
47319274Sjulian			cpu_halt();
47419274Sjulian			/* NOTREACHED */
47519274Sjulian		default:
47639237Sgibbs			howto &= ~RB_HALT;
47719274Sjulian			break;
47819274Sjulian		}
47950107Smsmith	}
48050107Smsmith}
48117658Sjulian
48250107Smsmith/*
48350107Smsmith * Check to see if the system paniced, pause and then reboot
48450107Smsmith * according to the specified delay.
48550107Smsmith */
48650107Smsmithstatic void
48750107Smsmithshutdown_panic(void *junk, int howto)
48850107Smsmith{
48950107Smsmith	int loop;
49050107Smsmith
49150107Smsmith	if (howto & RB_DUMP) {
49239237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
49339237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
49439237Sgibbs				printf("Automatic reboot in %d seconds - "
49539237Sgibbs				       "press a key on the console to abort\n",
49639237Sgibbs					PANIC_REBOOT_WAIT_TIME);
49739237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
49839237Sgibbs				     loop > 0; --loop) {
49939237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
50039237Sgibbs					/* Did user type a key? */
50139237Sgibbs					if (cncheckc() != -1)
50239237Sgibbs						break;
50317658Sjulian				}
50439237Sgibbs				if (!loop)
50550107Smsmith					return;
50617658Sjulian			}
50739237Sgibbs		} else { /* zero time specified - reboot NOW */
50850107Smsmith			return;
50917658Sjulian		}
51089522Snik		printf("--> Press a key on the console to reboot,\n");
51189522Snik		printf("--> or switch off the system now.\n");
51239237Sgibbs		cngetc();
51317658Sjulian	}
51450107Smsmith}
51550107Smsmith
51650107Smsmith/*
51750107Smsmith * Everything done, now reset
51850107Smsmith */
51950107Smsmithstatic void
52050107Smsmithshutdown_reset(void *junk, int howto)
52150107Smsmith{
522110859Salfred
523206878Sattilio	printf("Rebooting...\n");
524206878Sattilio	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
525206878Sattilio
526196196Sattilio	/*
527206878Sattilio	 * Acquiring smp_ipi_mtx here has a double effect:
528206878Sattilio	 * - it disables interrupts avoiding CPU0 preemption
529206878Sattilio	 *   by fast handlers (thus deadlocking  against other CPUs)
530206878Sattilio	 * - it avoids deadlocks against smp_rendezvous() or, more
531206878Sattilio	 *   generally, threads busy-waiting, with this spinlock held,
532206878Sattilio	 *   and waiting for responses by threads on other CPUs
533206878Sattilio	 *   (ie. smp_tlb_shootdown()).
534206897Sattilio	 *
535206897Sattilio	 * For the !SMP case it just needs to handle the former problem.
536196196Sattilio	 */
537206897Sattilio#ifdef SMP
538206878Sattilio	mtx_lock_spin(&smp_ipi_mtx);
539206897Sattilio#else
540206897Sattilio	spinlock_enter();
541206897Sattilio#endif
542196196Sattilio
54317677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
54417658Sjulian	cpu_reset();
54550107Smsmith	/* NOTREACHED */ /* assuming reset worked */
54617658Sjulian}
54717658Sjulian
54817658Sjulian/*
54917658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
55017658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
55117658Sjulian * the disks as this often leads to recursive panics.
55217658Sjulian */
55317658Sjulianvoid
554130164Sphkpanic(const char *fmt, ...)
55517658Sjulian{
556213648Savg#ifdef SMP
557213648Savg	static volatile u_int panic_cpu = NOCPU;
558228424Savg	cpuset_t other_cpus;
559213648Savg#endif
560100209Sgallatin	struct thread *td = curthread;
561103647Sjhb	int bootopt, newpanic;
56217658Sjulian	va_list ap;
56338874Sache	static char buf[256];
56417658Sjulian
565228424Savg	if (stop_scheduler_on_panic)
566228424Savg		spinlock_enter();
567228424Savg	else
568228424Savg		critical_enter();
569228424Savg
57065557Sjasone#ifdef SMP
57182115Sjhb	/*
57282115Sjhb	 * We don't want multiple CPU's to panic at the same time, so we
573101155Sjhb	 * use panic_cpu as a simple spinlock.  We have to keep checking
574101155Sjhb	 * panic_cpu if we are spinning in case the panic on the first
57582115Sjhb	 * CPU is canceled.
57682115Sjhb	 */
577101155Sjhb	if (panic_cpu != PCPU_GET(cpuid))
578101155Sjhb		while (atomic_cmpset_int(&panic_cpu, NOCPU,
579101155Sjhb		    PCPU_GET(cpuid)) == 0)
580101155Sjhb			while (panic_cpu != NOCPU)
581101155Sjhb				; /* nothing */
582228424Savg
583228424Savg	if (stop_scheduler_on_panic) {
584228424Savg		if (panicstr == NULL && !kdb_active) {
585228424Savg			other_cpus = all_cpus;
586228424Savg			CPU_CLR(PCPU_GET(cpuid), &other_cpus);
587228424Savg			stop_cpus_hard(other_cpus);
588228424Savg		}
589228424Savg
590228424Savg		/*
591228424Savg		 * We set stop_scheduler here and not in the block above,
592228424Savg		 * because we want to ensure that if panic has been called and
593228424Savg		 * stop_scheduler_on_panic is true, then stop_scheduler will
594228424Savg		 * always be set.  Even if panic has been entered from kdb.
595228424Savg		 */
596228424Savg		stop_scheduler = 1;
597228424Savg	}
59865557Sjasone#endif
59965557Sjasone
600222865Sattilio	bootopt = RB_AUTOBOOT;
601103647Sjhb	newpanic = 0;
60217658Sjulian	if (panicstr)
60317658Sjulian		bootopt |= RB_NOSYNC;
604103647Sjhb	else {
605222865Sattilio		bootopt |= RB_DUMP;
60617658Sjulian		panicstr = fmt;
607103647Sjhb		newpanic = 1;
608103647Sjhb	}
60917658Sjulian
61017658Sjulian	va_start(ap, fmt);
611116398Siedowse	if (newpanic) {
612116398Siedowse		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
61338874Sache		panicstr = buf;
614130164Sphk		printf("panic: %s\n", buf);
615116398Siedowse	} else {
616116398Siedowse		printf("panic: ");
617116398Siedowse		vprintf(fmt, ap);
618130164Sphk		printf("\n");
619116398Siedowse	}
62017658Sjulian	va_end(ap);
62126100Sfsmp#ifdef SMP
622134089Sjhb	printf("cpuid = %d\n", PCPU_GET(cpuid));
62326100Sfsmp#endif
62417658Sjulian
625131927Smarcel#ifdef KDB
626103647Sjhb	if (newpanic && trace_on_panic)
627131927Smarcel		kdb_backtrace();
62817658Sjulian	if (debugger_on_panic)
629174898Srwatson		kdb_enter(KDB_WHY_PANIC, "panic");
63017658Sjulian#endif
631170307Sjeff	/*thread_lock(td); */
632100209Sgallatin	td->td_flags |= TDF_INPANIC;
633170307Sjeff	/* thread_unlock(td); */
63485202Speter	if (!sync_on_panic)
63585202Speter		bootopt |= RB_NOSYNC;
636228424Savg	if (!stop_scheduler_on_panic)
637228424Savg		critical_exit();
638214004Smarcel	kern_reboot(bootopt);
63917658Sjulian}
64017658Sjulian
64117768Sjulian/*
64243436Smsmith * Support for poweroff delay.
643197071Sn_hibma *
644197071Sn_hibma * Please note that setting this delay too short might power off your machine
645197071Sn_hibma * before the write cache on your hard disk has been flushed, leading to
646197071Sn_hibma * soft-updates inconsistencies.
64743436Smsmith */
64854248Smsmith#ifndef POWEROFF_DELAY
64954248Smsmith# define POWEROFF_DELAY 5000
65054248Smsmith#endif
65154248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
65254248Smsmith
65343436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
654228449Seadler    &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)");
65543436Smsmith
656110859Salfredstatic void
65750107Smsmithpoweroff_wait(void *junk, int howto)
65843436Smsmith{
659110859Salfred
660110859Salfred	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
66143436Smsmith		return;
66243436Smsmith	DELAY(poweroff_delay * 1000);
66343436Smsmith}
66455539Sluoqi
66555539Sluoqi/*
66655539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
66755539Sluoqi * points in their main loops prior to a system shutdown, so that they
66855539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
66955539Sluoqi * to cause sync to fail).  For each of these system processes, register
67055539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
67155539Sluoqi */
67255539Sluoqistatic int kproc_shutdown_wait = 60;
67355539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
674228449Seadler    &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process");
67555539Sluoqi
67655539Sluoqivoid
67770063Sjhbkproc_shutdown(void *arg, int howto)
67855539Sluoqi{
67955539Sluoqi	struct proc *p;
68055539Sluoqi	int error;
68155539Sluoqi
68255539Sluoqi	if (panicstr)
68355539Sluoqi		return;
68455539Sluoqi
68555539Sluoqi	p = (struct proc *)arg;
686132866Snjl	printf("Waiting (max %d seconds) for system process `%s' to stop...",
687198408Sjhb	    kproc_shutdown_wait, p->p_comm);
688172836Sjulian	error = kproc_suspend(p, kproc_shutdown_wait * hz);
68955539Sluoqi
69055539Sluoqi	if (error == EWOULDBLOCK)
691132866Snjl		printf("timed out\n");
69255539Sluoqi	else
693132866Snjl		printf("done\n");
69455539Sluoqi}
69593496Sphk
696173004Sjulianvoid
697173004Sjuliankthread_shutdown(void *arg, int howto)
698173004Sjulian{
699173004Sjulian	struct thread *td;
700173004Sjulian	int error;
701173004Sjulian
702173004Sjulian	if (panicstr)
703173004Sjulian		return;
704173004Sjulian
705173004Sjulian	td = (struct thread *)arg;
706173004Sjulian	printf("Waiting (max %d seconds) for system thread `%s' to stop...",
707198408Sjhb	    kproc_shutdown_wait, td->td_name);
708173004Sjulian	error = kthread_suspend(td, kproc_shutdown_wait * hz);
709173004Sjulian
710173004Sjulian	if (error == EWOULDBLOCK)
711173004Sjulian		printf("timed out\n");
712173004Sjulian	else
713173004Sjulian		printf("done\n");
714173004Sjulian}
715173004Sjulian
71693496Sphk/* Registration of dumpers */
71793496Sphkint
71893496Sphkset_dumper(struct dumperinfo *di)
71993496Sphk{
720110859Salfred
72193496Sphk	if (di == NULL) {
72293496Sphk		bzero(&dumper, sizeof dumper);
72393496Sphk		return (0);
72493496Sphk	}
72593496Sphk	if (dumper.dumper != NULL)
72693496Sphk		return (EBUSY);
72793496Sphk	dumper = *di;
72893496Sphk	return (0);
72993496Sphk}
73093496Sphk
731175768Sru/* Call dumper with bounds checking. */
732175768Sruint
733175768Srudump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
734175768Sru    off_t offset, size_t length)
735175768Sru{
736175768Sru
737175768Sru	if (length != 0 && (offset < di->mediaoffset ||
738175768Sru	    offset - di->mediaoffset + length > di->mediasize)) {
739225516Sattilio		printf("Attempt to write outside dump device boundaries.\n"
740225516Sattilio	    "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
741225516Sattilio		    (intmax_t)offset, (intmax_t)di->mediaoffset,
742225516Sattilio		    (uintmax_t)length, (intmax_t)di->mediasize);
743225516Sattilio		return (ENOSPC);
744175768Sru	}
745175768Sru	return (di->dumper(di->priv, virtual, physical, offset, length));
746175768Sru}
747175768Sru
74893496Sphkvoid
749183527Spetermkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
750183527Speter    uint64_t dumplen, uint32_t blksz)
751183527Speter{
752183527Speter
753183527Speter	bzero(kdh, sizeof(*kdh));
754183527Speter	strncpy(kdh->magic, magic, sizeof(kdh->magic));
755183527Speter	strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
756183527Speter	kdh->version = htod32(KERNELDUMPVERSION);
757183527Speter	kdh->architectureversion = htod32(archver);
758183527Speter	kdh->dumplength = htod64(dumplen);
759183527Speter	kdh->dumptime = htod64(time_second);
760183527Speter	kdh->blocksize = htod32(blksz);
761194118Sjamie	strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
762183527Speter	strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
763183527Speter	if (panicstr != NULL)
764183527Speter		strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
765183527Speter	kdh->parity = kerneldump_parity(kdh);
766183527Speter}
767