kern_shutdown.c revision 29128
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 * 3. All advertising materials mentioning features or use of this software
1917658Sjulian *    must display the following acknowledgement:
2017658Sjulian *	This product includes software developed by the University of
2117658Sjulian *	California, Berkeley and its contributors.
2217658Sjulian * 4. Neither the name of the University nor the names of its contributors
2317658Sjulian *    may be used to endorse or promote products derived from this software
2417658Sjulian *    without specific prior written permission.
2517658Sjulian *
2617658Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2717658Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2817658Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2917658Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3017658Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3117658Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3217658Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3317658Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3417658Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3517658Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3617658Sjulian * SUCH DAMAGE.
3717658Sjulian *
3817658Sjulian *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
3929128Speter * $Id: kern_shutdown.c,v 1.23 1997/09/02 20:05:41 bde Exp $
4017658Sjulian */
4117658Sjulian
4217658Sjulian#include "opt_ddb.h"
4328976Sbde#include "opt_panic.h"
4428976Sbde#include "opt_show_busybufs.h"
4517658Sjulian
4617658Sjulian#include <sys/param.h>
4717658Sjulian#include <sys/systm.h>
4817658Sjulian#include <sys/reboot.h>
4917658Sjulian#include <sys/proc.h>
5017658Sjulian#include <sys/vnode.h>
5117658Sjulian#include <sys/malloc.h>
5217658Sjulian#include <sys/kernel.h>
5321776Sbde#include <sys/mount.h>
5417658Sjulian#include <sys/sysctl.h>
5517658Sjulian#include <sys/conf.h>
5617658Sjulian#include <sys/sysproto.h>
5717658Sjulian
5817658Sjulian#include <machine/pcb.h>
5917658Sjulian#include <machine/clock.h>
6017658Sjulian#include <machine/cons.h>
6117658Sjulian#include <machine/md_var.h>
6226812Speter#ifdef SMP
6326812Speter#include <machine/smp.h>		/* smp_active, cpuid */
6426812Speter#endif
6517658Sjulian
6617658Sjulian#include <sys/signalvar.h>
6717658Sjulian
6817658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
6917658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
7017658Sjulian#endif
7117658Sjulian
7217658Sjulian/*
7317658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
7417658Sjulian * ANSI and traditional C compilers.
7517658Sjulian */
7617658Sjulian#include <machine/stdarg.h>
7717658Sjulian
7828769Sbde#ifdef DDB
7917658Sjulian#ifdef DDB_UNATTENDED
8028769Sbdestatic int debugger_on_panic = 0;
8117658Sjulian#else
8228769Sbdestatic int debugger_on_panic = 1;
8317658Sjulian#endif
8417658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
8517658Sjulian	&debugger_on_panic, 0, "");
8617658Sjulian#endif
8717658Sjulian
8828000Sjulian#ifdef	HW_WDOG
8917658Sjulian/*
9027997Sjulian * If there is a hardware watchdog, point this at the function needed to
9127997Sjulian * hold it off.
9227997Sjulian * It's needed when the kernel needs to do some lengthy operations.
9327997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have
9427997Sjulian * your precious core-dump only half written because the wdog kicked in.
9527997Sjulian */
9627997Sjulianwatchdog_tickle_fn wdog_tickler = NULL;
9728000Sjulian#endif	/* HW_WDOG */
9827997Sjulian
9927997Sjulian/*
10017658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
10117658Sjulian * to indicate that the kernel has already called panic.
10217658Sjulian */
10317658Sjulianconst char *panicstr;
10417658Sjulian
10517658Sjulian/*
10617658Sjulian * callout list for things to do a shutdown
10717658Sjulian */
10817658Sjuliantypedef struct shutdown_list_element {
10917658Sjulian	struct shutdown_list_element *next;
11017658Sjulian	bootlist_fn function;
11117658Sjulian	void *arg;
11217658Sjulian} *sle_p;
11317658Sjulian
11417768Sjulian/*
11517768Sjulian * there are two shutdown lists. Some things need to be shut down
11617768Sjulian * Earlier than others.
11717768Sjulian */
11817768Sjulianstatic sle_p shutdown_list1;
11917768Sjulianstatic sle_p shutdown_list2;
12017658Sjulian
12117658Sjulian
12217658Sjulianstatic void dumpsys(void);
12317658Sjulian
12417658Sjulian#ifndef _SYS_SYSPROTO_H_
12517658Sjulianstruct reboot_args {
12617658Sjulian	int	opt;
12717658Sjulian};
12817658Sjulian#endif
12917658Sjulian/* ARGSUSED */
13017658Sjulian
13117658Sjulian/*
13217658Sjulian * The system call that results in a reboot
13317658Sjulian */
13417658Sjulianint
13517658Sjulianreboot(p, uap, retval)
13617658Sjulian	struct proc *p;
13717658Sjulian	struct reboot_args *uap;
13817658Sjulian	int *retval;
13917658Sjulian{
14017658Sjulian	int error;
14117658Sjulian
14217658Sjulian	if ((error = suser(p->p_ucred, &p->p_acflag)))
14317658Sjulian		return (error);
14417658Sjulian
14517658Sjulian	boot(uap->opt);
14617658Sjulian	return (0);
14717658Sjulian}
14817658Sjulian
14917658Sjulian/*
15017658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
15117658Sjulian */
15217658Sjulianvoid
15328769Sbdeshutdown_nice()
15417658Sjulian{
15517658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
15617658Sjulian	if (initproc != NULL) {
15717658Sjulian		psignal(initproc, SIGINT);
15817658Sjulian	} else {
15917658Sjulian		/* No init(8) running, so simply reboot */
16017658Sjulian		boot(RB_NOSYNC);
16117658Sjulian	}
16217658Sjulian	return;
16317658Sjulian}
16417658Sjulianstatic int	waittime = -1;
16517658Sjulianstatic struct pcb dumppcb;
16617658Sjulian
16717658Sjulian/*
16817658Sjulian *  Go through the rigmarole of shutting down..
16917658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
17017658Sjulian * anything machine dependant in it.
17117658Sjulian */
17218277Sbdevoid
17317658Sjulianboot(howto)
17417658Sjulian	int howto;
17517658Sjulian{
17617768Sjulian	sle_p ep;
17717658Sjulian
17825164Speter#ifdef SMP
17925164Speter	int c, spins;
18025164Speter
18128809Speter	/* The MPSPEC says that the BSP must do the shutdown */
18225164Speter	if (smp_active) {
18328809Speter		smp_active = 0;
18425164Speter
18525164Speter		spins = 100;
18625164Speter
18726812Speter		printf("boot() called on cpu#%d\n", cpuid);
18826812Speter		while ((c = cpuid) != 0) {
18925164Speter			if (spins-- < 1) {
19025164Speter				printf("timeout waiting for cpu #0!\n");
19125164Speter				break;
19225164Speter			}
19328809Speter			printf("I'm on cpu#%d, I need to be on cpu#0, sleeping..\n", c);
19425164Speter			tsleep((caddr_t)&smp_active, PZERO, "cpu0wt", 10);
19525164Speter		}
19625164Speter	}
19725164Speter#endif
19827997Sjulian	/*
19927997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
20027997Sjulian	 */
20117768Sjulian	ep = shutdown_list1;
20217768Sjulian	while (ep) {
20317768Sjulian		shutdown_list1 = ep->next;
20417658Sjulian		(*ep->function)(howto, ep->arg);
20517658Sjulian		ep = ep->next;
20617658Sjulian	}
20727997Sjulian
20827997Sjulian	/*
20927997Sjulian	 * Now sync filesystems
21027997Sjulian	 */
21117658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
21217658Sjulian		register struct buf *bp;
21317658Sjulian		int iter, nbusy;
21417658Sjulian
21517658Sjulian		waittime = 0;
21617658Sjulian		printf("\nsyncing disks... ");
21717658Sjulian
21817658Sjulian		sync(&proc0, NULL, NULL);
21917658Sjulian
22017658Sjulian		for (iter = 0; iter < 20; iter++) {
22117658Sjulian			nbusy = 0;
22217658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
22317658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
22417658Sjulian					nbusy++;
22517658Sjulian				}
22617658Sjulian			}
22717658Sjulian			if (nbusy == 0)
22817658Sjulian				break;
22917658Sjulian			printf("%d ", nbusy);
23017658Sjulian			DELAY(40000 * iter);
23117658Sjulian		}
23217658Sjulian		if (nbusy) {
23317658Sjulian			/*
23417658Sjulian			 * Failed to sync all blocks. Indicate this and don't
23517658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
23617658Sjulian			 */
23717658Sjulian			printf("giving up\n");
23817658Sjulian#ifdef SHOW_BUSYBUFS
23917658Sjulian			nbusy = 0;
24017658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
24117658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
24217658Sjulian					nbusy++;
24317658Sjulian					printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno);
24417658Sjulian				}
24517658Sjulian			}
24617658Sjulian			DELAY(5000000);	/* 5 seconds */
24717658Sjulian#endif
24817658Sjulian		} else {
24917658Sjulian			printf("done\n");
25017658Sjulian			/*
25117658Sjulian			 * Unmount filesystems
25217658Sjulian			 */
25317658Sjulian			if (panicstr == 0)
25417658Sjulian				vfs_unmountall();
25517658Sjulian		}
25617658Sjulian		DELAY(100000);			/* wait for console output to finish */
25717658Sjulian	}
25827997Sjulian
25927997Sjulian	/*
26027997Sjulian	 * Ok, now do things that assume all filesystem activity has
26127997Sjulian	 * been completed.
26227997Sjulian	 */
26317768Sjulian	ep = shutdown_list2;
26417768Sjulian	while (ep) {
26517768Sjulian		shutdown_list2 = ep->next;
26617768Sjulian		(*ep->function)(howto, ep->arg);
26717768Sjulian		ep = ep->next;
26817768Sjulian	}
26917658Sjulian	splhigh();
27017658Sjulian	if (howto & RB_HALT) {
27126657Swollman		cpu_power_down();
27217658Sjulian		printf("\n");
27317658Sjulian		printf("The operating system has halted.\n");
27417658Sjulian		printf("Please press any key to reboot.\n\n");
27519274Sjulian		switch (cngetc()) {
27619274Sjulian		case -1:		/* No console, just die */
27719274Sjulian			cpu_halt();
27819274Sjulian			/* NOTREACHED */
27919274Sjulian		default:
28019274Sjulian			break;
28119274Sjulian		}
28217658Sjulian	} else {
28317658Sjulian		if (howto & RB_DUMP) {
28417658Sjulian			if (!cold) {
28517658Sjulian				savectx(&dumppcb);
28617658Sjulian				dumppcb.pcb_cr3 = rcr3();
28717658Sjulian				dumpsys();
28817658Sjulian			}
28917658Sjulian
29017658Sjulian			if (PANIC_REBOOT_WAIT_TIME != 0) {
29117658Sjulian				if (PANIC_REBOOT_WAIT_TIME != -1) {
29217658Sjulian					int loop;
29317658Sjulian					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
29417658Sjulian						PANIC_REBOOT_WAIT_TIME);
29517658Sjulian					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
29617658Sjulian						DELAY(1000 * 100); /* 1/10th second */
29718290Sbde						/* Did user type a key? */
29818290Sbde						if (cncheckc() != -1)
29917658Sjulian							break;
30017658Sjulian					}
30117658Sjulian					if (!loop)
30217658Sjulian						goto die;
30317658Sjulian				}
30417658Sjulian			} else { /* zero time specified - reboot NOW */
30517658Sjulian				goto die;
30617658Sjulian			}
30717658Sjulian			printf("--> Press a key on the console to reboot <--\n");
30817658Sjulian			cngetc();
30917658Sjulian		}
31017658Sjulian	}
31117658Sjuliandie:
31217658Sjulian	printf("Rebooting...\n");
31317658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
31417677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
31517658Sjulian	cpu_reset();
31617658Sjulian	for(;;) ;
31717658Sjulian	/* NOTREACHED */
31817658Sjulian}
31917658Sjulian
32017658Sjulian/*
32117658Sjulian * Magic number for savecore
32217658Sjulian *
32317658Sjulian * exported (symorder) and used at least by savecore(8)
32417658Sjulian *
32517658Sjulian */
32617658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
32717658Sjulian
32817658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
32917658Sjulian
33017658Sjulianstatic int	dodump = 1;
33117658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
33217658Sjulian
33317658Sjulian/*
33417658Sjulian * Doadump comes here after turning off memory management and
33517658Sjulian * getting on the dump stack, either when called above, or by
33617658Sjulian * the auto-restart code.
33717658Sjulian */
33817658Sjulianstatic void
33917658Sjuliandumpsys(void)
34017658Sjulian{
34117658Sjulian
34217658Sjulian	if (!dodump)
34317658Sjulian		return;
34417658Sjulian	if (dumpdev == NODEV)
34517658Sjulian		return;
34617658Sjulian	if ((minor(dumpdev)&07) != 1)
34717658Sjulian		return;
34817658Sjulian	if (!(bdevsw[major(dumpdev)]))
34917658Sjulian		return;
35017658Sjulian	if (!(bdevsw[major(dumpdev)]->d_dump))
35117658Sjulian		return;
35217658Sjulian	dumpsize = Maxmem;
35317658Sjulian	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
35417658Sjulian	printf("dump ");
35517658Sjulian	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
35617658Sjulian
35717658Sjulian	case ENXIO:
35817658Sjulian		printf("device bad\n");
35917658Sjulian		break;
36017658Sjulian
36117658Sjulian	case EFAULT:
36217658Sjulian		printf("device not ready\n");
36317658Sjulian		break;
36417658Sjulian
36517658Sjulian	case EINVAL:
36617658Sjulian		printf("area improper\n");
36717658Sjulian		break;
36817658Sjulian
36917658Sjulian	case EIO:
37017658Sjulian		printf("i/o error\n");
37117658Sjulian		break;
37217658Sjulian
37317658Sjulian	case EINTR:
37417658Sjulian		printf("aborted from console\n");
37517658Sjulian		break;
37617658Sjulian
37717658Sjulian	default:
37817658Sjulian		printf("succeeded\n");
37917658Sjulian		break;
38017658Sjulian	}
38117658Sjulian}
38217658Sjulian
38317658Sjulian/*
38417658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
38517658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
38617658Sjulian * the disks as this often leads to recursive panics.
38717658Sjulian */
38817658Sjulianvoid
38917658Sjulianpanic(const char *fmt, ...)
39017658Sjulian{
39117658Sjulian	int bootopt;
39217658Sjulian	va_list ap;
39317658Sjulian
39417658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
39517658Sjulian	if (panicstr)
39617658Sjulian		bootopt |= RB_NOSYNC;
39717658Sjulian	else
39817658Sjulian		panicstr = fmt;
39917658Sjulian
40017658Sjulian	printf("panic: ");
40117658Sjulian	va_start(ap, fmt);
40217658Sjulian	vprintf(fmt, ap);
40317658Sjulian	va_end(ap);
40417658Sjulian	printf("\n");
40526100Sfsmp#ifdef SMP
40629128Speter	/* three seperate prints in case of an unmapped page and trap */
40729128Speter	printf("mp_lock = %08x; ", mp_lock);
40829128Speter	printf("cpuid = %d; ", cpuid);
40929128Speter	printf("lapic.id = %08x\n", lapic.id);
41026100Sfsmp#endif
41117658Sjulian
41217658Sjulian#if defined(DDB)
41317658Sjulian	if (debugger_on_panic)
41417658Sjulian		Debugger ("panic");
41517658Sjulian#endif
41617658Sjulian	boot(bootopt);
41717658Sjulian}
41817658Sjulian
41917768Sjulian/*
42017768Sjulian * Two routines to handle adding/deleting items on the
42117768Sjulian * shutdown callout lists
42217768Sjulian *
42317768Sjulian * at_shutdown():
42417658Sjulian * Take the arguments given and put them onto the shutdown callout list.
42517658Sjulian * However first make sure that it's not already there.
42617658Sjulian * returns 0 on success.
42717658Sjulian */
42817658Sjulianint
42917768Sjulianat_shutdown(bootlist_fn function, void *arg, int position)
43017658Sjulian{
43117768Sjulian	sle_p ep, *epp;
43217768Sjulian
43317768Sjulian	switch(position) {
43417768Sjulian	case SHUTDOWN_PRE_SYNC:
43517768Sjulian		epp = &shutdown_list1;
43617768Sjulian		break;
43717768Sjulian	case SHUTDOWN_POST_SYNC:
43817768Sjulian		epp = &shutdown_list2;
43917768Sjulian		break;
44017768Sjulian	default:
44117768Sjulian		printf("bad exit callout list specified\n");
44217768Sjulian		return (EINVAL);
44317768Sjulian	}
44417768Sjulian	if (rm_at_shutdown(function, arg))
44517658Sjulian		printf("exit callout entry already present\n");
44617768Sjulian	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
44717768Sjulian	if (ep == NULL)
44817768Sjulian		return (ENOMEM);
44917768Sjulian	ep->next = *epp;
45017658Sjulian	ep->function = function;
45117658Sjulian	ep->arg = arg;
45217768Sjulian	*epp = ep;
45317768Sjulian	return (0);
45417658Sjulian}
45517768Sjulian
45617658Sjulian/*
45717768Sjulian * Scan the exit callout lists for the given items and remove them.
45817658Sjulian * Returns the number of items removed.
45917658Sjulian */
46017658Sjulianint
46117658Sjulianrm_at_shutdown(bootlist_fn function, void *arg)
46217658Sjulian{
46317768Sjulian	sle_p *epp, ep;
46417768Sjulian	int count;
46517658Sjulian
46617768Sjulian	count = 0;
46717768Sjulian	epp = &shutdown_list1;
46817658Sjulian	ep = *epp;
46917768Sjulian	while (ep) {
47017834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
47117658Sjulian			*epp = ep->next;
47217768Sjulian			free(ep, M_TEMP);
47317658Sjulian			count++;
47417658Sjulian		} else {
47517658Sjulian			epp = &ep->next;
47617658Sjulian		}
47717658Sjulian		ep = *epp;
47817658Sjulian	}
47917768Sjulian	epp = &shutdown_list2;
48017768Sjulian	ep = *epp;
48117768Sjulian	while (ep) {
48217834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
48317768Sjulian			*epp = ep->next;
48417768Sjulian			free(ep, M_TEMP);
48517768Sjulian			count++;
48617768Sjulian		} else {
48717768Sjulian			epp = &ep->next;
48817768Sjulian		}
48917768Sjulian		ep = *epp;
49017768Sjulian	}
49117768Sjulian	return (count);
49217658Sjulian}
49317658Sjulian
494