kern_shutdown.c revision 33445
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 3933445Seivind * $Id: kern_shutdown.c,v 1.27 1997/11/25 07:07:43 julian Exp $ 4017658Sjulian */ 4117658Sjulian 4217658Sjulian#include "opt_ddb.h" 4333445Seivind#include "opt_hw_wdog.h" 4428976Sbde#include "opt_panic.h" 4528976Sbde#include "opt_show_busybufs.h" 4617658Sjulian 4717658Sjulian#include <sys/param.h> 4817658Sjulian#include <sys/systm.h> 4931275Sbde#include <sys/buf.h> 5017658Sjulian#include <sys/reboot.h> 5117658Sjulian#include <sys/proc.h> 5217658Sjulian#include <sys/malloc.h> 5317658Sjulian#include <sys/kernel.h> 5421776Sbde#include <sys/mount.h> 5517658Sjulian#include <sys/sysctl.h> 5617658Sjulian#include <sys/conf.h> 5717658Sjulian#include <sys/sysproto.h> 5817658Sjulian 5917658Sjulian#include <machine/pcb.h> 6017658Sjulian#include <machine/clock.h> 6117658Sjulian#include <machine/cons.h> 6217658Sjulian#include <machine/md_var.h> 6326812Speter#ifdef SMP 6426812Speter#include <machine/smp.h> /* smp_active, cpuid */ 6526812Speter#endif 6617658Sjulian 6717658Sjulian#include <sys/signalvar.h> 6817658Sjulian 6917658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME 7017658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 7117658Sjulian#endif 7217658Sjulian 7317658Sjulian/* 7417658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both 7517658Sjulian * ANSI and traditional C compilers. 7617658Sjulian */ 7717658Sjulian#include <machine/stdarg.h> 7817658Sjulian 7928769Sbde#ifdef DDB 8017658Sjulian#ifdef DDB_UNATTENDED 8128769Sbdestatic int debugger_on_panic = 0; 8217658Sjulian#else 8328769Sbdestatic int debugger_on_panic = 1; 8417658Sjulian#endif 8517658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 8617658Sjulian &debugger_on_panic, 0, ""); 8717658Sjulian#endif 8817658Sjulian 8928000Sjulian#ifdef HW_WDOG 9017658Sjulian/* 9127997Sjulian * If there is a hardware watchdog, point this at the function needed to 9227997Sjulian * hold it off. 9327997Sjulian * It's needed when the kernel needs to do some lengthy operations. 9427997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have 9527997Sjulian * your precious core-dump only half written because the wdog kicked in. 9627997Sjulian */ 9727997Sjulianwatchdog_tickle_fn wdog_tickler = NULL; 9828000Sjulian#endif /* HW_WDOG */ 9927997Sjulian 10027997Sjulian/* 10117658Sjulian * Variable panicstr contains argument to first call to panic; used as flag 10217658Sjulian * to indicate that the kernel has already called panic. 10317658Sjulian */ 10417658Sjulianconst char *panicstr; 10517658Sjulian 10617658Sjulian/* 10717658Sjulian * callout list for things to do a shutdown 10817658Sjulian */ 10917658Sjuliantypedef struct shutdown_list_element { 11017658Sjulian struct shutdown_list_element *next; 11117658Sjulian bootlist_fn function; 11217658Sjulian void *arg; 11317658Sjulian} *sle_p; 11417658Sjulian 11517768Sjulian/* 11617768Sjulian * there are two shutdown lists. Some things need to be shut down 11717768Sjulian * Earlier than others. 11817768Sjulian */ 11917768Sjulianstatic sle_p shutdown_list1; 12017768Sjulianstatic sle_p shutdown_list2; 12117658Sjulian 12231275Sbdestatic void boot __P((int)) __dead2; 12331275Sbdestatic void dumpsys __P((void)); 12417658Sjulian 12517658Sjulian#ifndef _SYS_SYSPROTO_H_ 12617658Sjulianstruct reboot_args { 12717658Sjulian int opt; 12817658Sjulian}; 12917658Sjulian#endif 13017658Sjulian/* ARGSUSED */ 13117658Sjulian 13217658Sjulian/* 13317658Sjulian * The system call that results in a reboot 13417658Sjulian */ 13517658Sjulianint 13630994Sphkreboot(p, uap) 13717658Sjulian struct proc *p; 13817658Sjulian struct reboot_args *uap; 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 */ 17231275Sbdestatic void 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 21830994Sphk sync(&proc0, 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 33331403Sjulian/* ARGSUSED */ 33431403Sjulianstatic void dump_conf __P((void *dummy)); 33531403Sjulianstatic void 33631403Sjuliandump_conf(dummy) 33731403Sjulian void *dummy; 33831403Sjulian{ 33931403Sjulian cpu_dumpconf(); 34031403Sjulian} 34131403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 34231403Sjulian 34317658Sjulian/* 34417658Sjulian * Doadump comes here after turning off memory management and 34517658Sjulian * getting on the dump stack, either when called above, or by 34617658Sjulian * the auto-restart code. 34717658Sjulian */ 34817658Sjulianstatic void 34917658Sjuliandumpsys(void) 35017658Sjulian{ 35117658Sjulian 35217658Sjulian if (!dodump) 35317658Sjulian return; 35417658Sjulian if (dumpdev == NODEV) 35517658Sjulian return; 35617658Sjulian if ((minor(dumpdev)&07) != 1) 35717658Sjulian return; 35817658Sjulian if (!(bdevsw[major(dumpdev)])) 35917658Sjulian return; 36017658Sjulian if (!(bdevsw[major(dumpdev)]->d_dump)) 36117658Sjulian return; 36217658Sjulian dumpsize = Maxmem; 36317658Sjulian printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo); 36417658Sjulian printf("dump "); 36517658Sjulian switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) { 36617658Sjulian 36717658Sjulian case ENXIO: 36817658Sjulian printf("device bad\n"); 36917658Sjulian break; 37017658Sjulian 37117658Sjulian case EFAULT: 37217658Sjulian printf("device not ready\n"); 37317658Sjulian break; 37417658Sjulian 37517658Sjulian case EINVAL: 37617658Sjulian printf("area improper\n"); 37717658Sjulian break; 37817658Sjulian 37917658Sjulian case EIO: 38017658Sjulian printf("i/o error\n"); 38117658Sjulian break; 38217658Sjulian 38317658Sjulian case EINTR: 38417658Sjulian printf("aborted from console\n"); 38517658Sjulian break; 38617658Sjulian 38717658Sjulian default: 38817658Sjulian printf("succeeded\n"); 38917658Sjulian break; 39017658Sjulian } 39117658Sjulian} 39217658Sjulian 39317658Sjulian/* 39417658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 39517658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 39617658Sjulian * the disks as this often leads to recursive panics. 39717658Sjulian */ 39817658Sjulianvoid 39917658Sjulianpanic(const char *fmt, ...) 40017658Sjulian{ 40117658Sjulian int bootopt; 40217658Sjulian va_list ap; 40317658Sjulian 40417658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 40517658Sjulian if (panicstr) 40617658Sjulian bootopt |= RB_NOSYNC; 40717658Sjulian else 40817658Sjulian panicstr = fmt; 40917658Sjulian 41017658Sjulian printf("panic: "); 41117658Sjulian va_start(ap, fmt); 41217658Sjulian vprintf(fmt, ap); 41317658Sjulian va_end(ap); 41417658Sjulian printf("\n"); 41526100Sfsmp#ifdef SMP 41629128Speter /* three seperate prints in case of an unmapped page and trap */ 41729128Speter printf("mp_lock = %08x; ", mp_lock); 41829128Speter printf("cpuid = %d; ", cpuid); 41929128Speter printf("lapic.id = %08x\n", lapic.id); 42026100Sfsmp#endif 42117658Sjulian 42217658Sjulian#if defined(DDB) 42317658Sjulian if (debugger_on_panic) 42417658Sjulian Debugger ("panic"); 42517658Sjulian#endif 42617658Sjulian boot(bootopt); 42717658Sjulian} 42817658Sjulian 42917768Sjulian/* 43017768Sjulian * Two routines to handle adding/deleting items on the 43117768Sjulian * shutdown callout lists 43217768Sjulian * 43317768Sjulian * at_shutdown(): 43417658Sjulian * Take the arguments given and put them onto the shutdown callout list. 43517658Sjulian * However first make sure that it's not already there. 43617658Sjulian * returns 0 on success. 43717658Sjulian */ 43817658Sjulianint 43917768Sjulianat_shutdown(bootlist_fn function, void *arg, int position) 44017658Sjulian{ 44117768Sjulian sle_p ep, *epp; 44217768Sjulian 44317768Sjulian switch(position) { 44417768Sjulian case SHUTDOWN_PRE_SYNC: 44517768Sjulian epp = &shutdown_list1; 44617768Sjulian break; 44717768Sjulian case SHUTDOWN_POST_SYNC: 44817768Sjulian epp = &shutdown_list2; 44917768Sjulian break; 45017768Sjulian default: 45117768Sjulian printf("bad exit callout list specified\n"); 45217768Sjulian return (EINVAL); 45317768Sjulian } 45417768Sjulian if (rm_at_shutdown(function, arg)) 45517658Sjulian printf("exit callout entry already present\n"); 45617768Sjulian ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 45717768Sjulian if (ep == NULL) 45817768Sjulian return (ENOMEM); 45917768Sjulian ep->next = *epp; 46017658Sjulian ep->function = function; 46117658Sjulian ep->arg = arg; 46217768Sjulian *epp = ep; 46317768Sjulian return (0); 46417658Sjulian} 46517768Sjulian 46617658Sjulian/* 46717768Sjulian * Scan the exit callout lists for the given items and remove them. 46817658Sjulian * Returns the number of items removed. 46917658Sjulian */ 47017658Sjulianint 47117658Sjulianrm_at_shutdown(bootlist_fn function, void *arg) 47217658Sjulian{ 47317768Sjulian sle_p *epp, ep; 47417768Sjulian int count; 47517658Sjulian 47617768Sjulian count = 0; 47717768Sjulian epp = &shutdown_list1; 47817658Sjulian ep = *epp; 47917768Sjulian while (ep) { 48017834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 48117658Sjulian *epp = ep->next; 48217768Sjulian free(ep, M_TEMP); 48317658Sjulian count++; 48417658Sjulian } else { 48517658Sjulian epp = &ep->next; 48617658Sjulian } 48717658Sjulian ep = *epp; 48817658Sjulian } 48917768Sjulian epp = &shutdown_list2; 49017768Sjulian ep = *epp; 49117768Sjulian while (ep) { 49217834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 49317768Sjulian *epp = ep->next; 49417768Sjulian free(ep, M_TEMP); 49517768Sjulian count++; 49617768Sjulian } else { 49717768Sjulian epp = &ep->next; 49817768Sjulian } 49917768Sjulian ep = *epp; 50017768Sjulian } 50117768Sjulian return (count); 50217658Sjulian} 50317658Sjulian 504