kern_shutdown.c revision 30994
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
3930994Sphk * $Id: kern_shutdown.c,v 1.24 1997/09/05 08:54:55 peter 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
13530994Sphkreboot(p, uap)
13617658Sjulian	struct proc *p;
13717658Sjulian	struct reboot_args *uap;
13817658Sjulian{
13917658Sjulian	int error;
14017658Sjulian
14117658Sjulian	if ((error = suser(p->p_ucred, &p->p_acflag)))
14217658Sjulian		return (error);
14317658Sjulian
14417658Sjulian	boot(uap->opt);
14517658Sjulian	return (0);
14617658Sjulian}
14717658Sjulian
14817658Sjulian/*
14917658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
15017658Sjulian */
15117658Sjulianvoid
15228769Sbdeshutdown_nice()
15317658Sjulian{
15417658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
15517658Sjulian	if (initproc != NULL) {
15617658Sjulian		psignal(initproc, SIGINT);
15717658Sjulian	} else {
15817658Sjulian		/* No init(8) running, so simply reboot */
15917658Sjulian		boot(RB_NOSYNC);
16017658Sjulian	}
16117658Sjulian	return;
16217658Sjulian}
16317658Sjulianstatic int	waittime = -1;
16417658Sjulianstatic struct pcb dumppcb;
16517658Sjulian
16617658Sjulian/*
16717658Sjulian *  Go through the rigmarole of shutting down..
16817658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
16917658Sjulian * anything machine dependant in it.
17017658Sjulian */
17118277Sbdevoid
17217658Sjulianboot(howto)
17317658Sjulian	int howto;
17417658Sjulian{
17517768Sjulian	sle_p ep;
17617658Sjulian
17725164Speter#ifdef SMP
17825164Speter	int c, spins;
17925164Speter
18028809Speter	/* The MPSPEC says that the BSP must do the shutdown */
18125164Speter	if (smp_active) {
18228809Speter		smp_active = 0;
18325164Speter
18425164Speter		spins = 100;
18525164Speter
18626812Speter		printf("boot() called on cpu#%d\n", cpuid);
18726812Speter		while ((c = cpuid) != 0) {
18825164Speter			if (spins-- < 1) {
18925164Speter				printf("timeout waiting for cpu #0!\n");
19025164Speter				break;
19125164Speter			}
19228809Speter			printf("I'm on cpu#%d, I need to be on cpu#0, sleeping..\n", c);
19325164Speter			tsleep((caddr_t)&smp_active, PZERO, "cpu0wt", 10);
19425164Speter		}
19525164Speter	}
19625164Speter#endif
19727997Sjulian	/*
19827997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
19927997Sjulian	 */
20017768Sjulian	ep = shutdown_list1;
20117768Sjulian	while (ep) {
20217768Sjulian		shutdown_list1 = ep->next;
20317658Sjulian		(*ep->function)(howto, ep->arg);
20417658Sjulian		ep = ep->next;
20517658Sjulian	}
20627997Sjulian
20727997Sjulian	/*
20827997Sjulian	 * Now sync filesystems
20927997Sjulian	 */
21017658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
21117658Sjulian		register struct buf *bp;
21217658Sjulian		int iter, nbusy;
21317658Sjulian
21417658Sjulian		waittime = 0;
21517658Sjulian		printf("\nsyncing disks... ");
21617658Sjulian
21730994Sphk		sync(&proc0, NULL);
21817658Sjulian
21917658Sjulian		for (iter = 0; iter < 20; iter++) {
22017658Sjulian			nbusy = 0;
22117658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
22217658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
22317658Sjulian					nbusy++;
22417658Sjulian				}
22517658Sjulian			}
22617658Sjulian			if (nbusy == 0)
22717658Sjulian				break;
22817658Sjulian			printf("%d ", nbusy);
22917658Sjulian			DELAY(40000 * iter);
23017658Sjulian		}
23117658Sjulian		if (nbusy) {
23217658Sjulian			/*
23317658Sjulian			 * Failed to sync all blocks. Indicate this and don't
23417658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
23517658Sjulian			 */
23617658Sjulian			printf("giving up\n");
23717658Sjulian#ifdef SHOW_BUSYBUFS
23817658Sjulian			nbusy = 0;
23917658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
24017658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
24117658Sjulian					nbusy++;
24217658Sjulian					printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno);
24317658Sjulian				}
24417658Sjulian			}
24517658Sjulian			DELAY(5000000);	/* 5 seconds */
24617658Sjulian#endif
24717658Sjulian		} else {
24817658Sjulian			printf("done\n");
24917658Sjulian			/*
25017658Sjulian			 * Unmount filesystems
25117658Sjulian			 */
25217658Sjulian			if (panicstr == 0)
25317658Sjulian				vfs_unmountall();
25417658Sjulian		}
25517658Sjulian		DELAY(100000);			/* wait for console output to finish */
25617658Sjulian	}
25727997Sjulian
25827997Sjulian	/*
25927997Sjulian	 * Ok, now do things that assume all filesystem activity has
26027997Sjulian	 * been completed.
26127997Sjulian	 */
26217768Sjulian	ep = shutdown_list2;
26317768Sjulian	while (ep) {
26417768Sjulian		shutdown_list2 = ep->next;
26517768Sjulian		(*ep->function)(howto, ep->arg);
26617768Sjulian		ep = ep->next;
26717768Sjulian	}
26817658Sjulian	splhigh();
26917658Sjulian	if (howto & RB_HALT) {
27026657Swollman		cpu_power_down();
27117658Sjulian		printf("\n");
27217658Sjulian		printf("The operating system has halted.\n");
27317658Sjulian		printf("Please press any key to reboot.\n\n");
27419274Sjulian		switch (cngetc()) {
27519274Sjulian		case -1:		/* No console, just die */
27619274Sjulian			cpu_halt();
27719274Sjulian			/* NOTREACHED */
27819274Sjulian		default:
27919274Sjulian			break;
28019274Sjulian		}
28117658Sjulian	} else {
28217658Sjulian		if (howto & RB_DUMP) {
28317658Sjulian			if (!cold) {
28417658Sjulian				savectx(&dumppcb);
28517658Sjulian				dumppcb.pcb_cr3 = rcr3();
28617658Sjulian				dumpsys();
28717658Sjulian			}
28817658Sjulian
28917658Sjulian			if (PANIC_REBOOT_WAIT_TIME != 0) {
29017658Sjulian				if (PANIC_REBOOT_WAIT_TIME != -1) {
29117658Sjulian					int loop;
29217658Sjulian					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
29317658Sjulian						PANIC_REBOOT_WAIT_TIME);
29417658Sjulian					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
29517658Sjulian						DELAY(1000 * 100); /* 1/10th second */
29618290Sbde						/* Did user type a key? */
29718290Sbde						if (cncheckc() != -1)
29817658Sjulian							break;
29917658Sjulian					}
30017658Sjulian					if (!loop)
30117658Sjulian						goto die;
30217658Sjulian				}
30317658Sjulian			} else { /* zero time specified - reboot NOW */
30417658Sjulian				goto die;
30517658Sjulian			}
30617658Sjulian			printf("--> Press a key on the console to reboot <--\n");
30717658Sjulian			cngetc();
30817658Sjulian		}
30917658Sjulian	}
31017658Sjuliandie:
31117658Sjulian	printf("Rebooting...\n");
31217658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
31317677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
31417658Sjulian	cpu_reset();
31517658Sjulian	for(;;) ;
31617658Sjulian	/* NOTREACHED */
31717658Sjulian}
31817658Sjulian
31917658Sjulian/*
32017658Sjulian * Magic number for savecore
32117658Sjulian *
32217658Sjulian * exported (symorder) and used at least by savecore(8)
32317658Sjulian *
32417658Sjulian */
32517658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
32617658Sjulian
32717658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
32817658Sjulian
32917658Sjulianstatic int	dodump = 1;
33017658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
33117658Sjulian
33217658Sjulian/*
33317658Sjulian * Doadump comes here after turning off memory management and
33417658Sjulian * getting on the dump stack, either when called above, or by
33517658Sjulian * the auto-restart code.
33617658Sjulian */
33717658Sjulianstatic void
33817658Sjuliandumpsys(void)
33917658Sjulian{
34017658Sjulian
34117658Sjulian	if (!dodump)
34217658Sjulian		return;
34317658Sjulian	if (dumpdev == NODEV)
34417658Sjulian		return;
34517658Sjulian	if ((minor(dumpdev)&07) != 1)
34617658Sjulian		return;
34717658Sjulian	if (!(bdevsw[major(dumpdev)]))
34817658Sjulian		return;
34917658Sjulian	if (!(bdevsw[major(dumpdev)]->d_dump))
35017658Sjulian		return;
35117658Sjulian	dumpsize = Maxmem;
35217658Sjulian	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
35317658Sjulian	printf("dump ");
35417658Sjulian	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
35517658Sjulian
35617658Sjulian	case ENXIO:
35717658Sjulian		printf("device bad\n");
35817658Sjulian		break;
35917658Sjulian
36017658Sjulian	case EFAULT:
36117658Sjulian		printf("device not ready\n");
36217658Sjulian		break;
36317658Sjulian
36417658Sjulian	case EINVAL:
36517658Sjulian		printf("area improper\n");
36617658Sjulian		break;
36717658Sjulian
36817658Sjulian	case EIO:
36917658Sjulian		printf("i/o error\n");
37017658Sjulian		break;
37117658Sjulian
37217658Sjulian	case EINTR:
37317658Sjulian		printf("aborted from console\n");
37417658Sjulian		break;
37517658Sjulian
37617658Sjulian	default:
37717658Sjulian		printf("succeeded\n");
37817658Sjulian		break;
37917658Sjulian	}
38017658Sjulian}
38117658Sjulian
38217658Sjulian/*
38317658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
38417658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
38517658Sjulian * the disks as this often leads to recursive panics.
38617658Sjulian */
38717658Sjulianvoid
38817658Sjulianpanic(const char *fmt, ...)
38917658Sjulian{
39017658Sjulian	int bootopt;
39117658Sjulian	va_list ap;
39217658Sjulian
39317658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
39417658Sjulian	if (panicstr)
39517658Sjulian		bootopt |= RB_NOSYNC;
39617658Sjulian	else
39717658Sjulian		panicstr = fmt;
39817658Sjulian
39917658Sjulian	printf("panic: ");
40017658Sjulian	va_start(ap, fmt);
40117658Sjulian	vprintf(fmt, ap);
40217658Sjulian	va_end(ap);
40317658Sjulian	printf("\n");
40426100Sfsmp#ifdef SMP
40529128Speter	/* three seperate prints in case of an unmapped page and trap */
40629128Speter	printf("mp_lock = %08x; ", mp_lock);
40729128Speter	printf("cpuid = %d; ", cpuid);
40829128Speter	printf("lapic.id = %08x\n", lapic.id);
40926100Sfsmp#endif
41017658Sjulian
41117658Sjulian#if defined(DDB)
41217658Sjulian	if (debugger_on_panic)
41317658Sjulian		Debugger ("panic");
41417658Sjulian#endif
41517658Sjulian	boot(bootopt);
41617658Sjulian}
41717658Sjulian
41817768Sjulian/*
41917768Sjulian * Two routines to handle adding/deleting items on the
42017768Sjulian * shutdown callout lists
42117768Sjulian *
42217768Sjulian * at_shutdown():
42317658Sjulian * Take the arguments given and put them onto the shutdown callout list.
42417658Sjulian * However first make sure that it's not already there.
42517658Sjulian * returns 0 on success.
42617658Sjulian */
42717658Sjulianint
42817768Sjulianat_shutdown(bootlist_fn function, void *arg, int position)
42917658Sjulian{
43017768Sjulian	sle_p ep, *epp;
43117768Sjulian
43217768Sjulian	switch(position) {
43317768Sjulian	case SHUTDOWN_PRE_SYNC:
43417768Sjulian		epp = &shutdown_list1;
43517768Sjulian		break;
43617768Sjulian	case SHUTDOWN_POST_SYNC:
43717768Sjulian		epp = &shutdown_list2;
43817768Sjulian		break;
43917768Sjulian	default:
44017768Sjulian		printf("bad exit callout list specified\n");
44117768Sjulian		return (EINVAL);
44217768Sjulian	}
44317768Sjulian	if (rm_at_shutdown(function, arg))
44417658Sjulian		printf("exit callout entry already present\n");
44517768Sjulian	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
44617768Sjulian	if (ep == NULL)
44717768Sjulian		return (ENOMEM);
44817768Sjulian	ep->next = *epp;
44917658Sjulian	ep->function = function;
45017658Sjulian	ep->arg = arg;
45117768Sjulian	*epp = ep;
45217768Sjulian	return (0);
45317658Sjulian}
45417768Sjulian
45517658Sjulian/*
45617768Sjulian * Scan the exit callout lists for the given items and remove them.
45717658Sjulian * Returns the number of items removed.
45817658Sjulian */
45917658Sjulianint
46017658Sjulianrm_at_shutdown(bootlist_fn function, void *arg)
46117658Sjulian{
46217768Sjulian	sle_p *epp, ep;
46317768Sjulian	int count;
46417658Sjulian
46517768Sjulian	count = 0;
46617768Sjulian	epp = &shutdown_list1;
46717658Sjulian	ep = *epp;
46817768Sjulian	while (ep) {
46917834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
47017658Sjulian			*epp = ep->next;
47117768Sjulian			free(ep, M_TEMP);
47217658Sjulian			count++;
47317658Sjulian		} else {
47417658Sjulian			epp = &ep->next;
47517658Sjulian		}
47617658Sjulian		ep = *epp;
47717658Sjulian	}
47817768Sjulian	epp = &shutdown_list2;
47917768Sjulian	ep = *epp;
48017768Sjulian	while (ep) {
48117834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
48217768Sjulian			*epp = ep->next;
48317768Sjulian			free(ep, M_TEMP);
48417768Sjulian			count++;
48517768Sjulian		} else {
48617768Sjulian			epp = &ep->next;
48717768Sjulian		}
48817768Sjulian		ep = *epp;
48917768Sjulian	}
49017768Sjulian	return (count);
49117658Sjulian}
49217658Sjulian
493