kern_shutdown.c revision 37555
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 3937555Sbde * $Id: kern_shutdown.c,v 1.33 1998/06/07 17:11:35 dfr 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 if (smp_active) { 18026812Speter printf("boot() called on cpu#%d\n", cpuid); 18125164Speter } 18225164Speter#endif 18327997Sjulian /* 18427997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 18527997Sjulian */ 18617768Sjulian ep = shutdown_list1; 18717768Sjulian while (ep) { 18817768Sjulian shutdown_list1 = ep->next; 18917658Sjulian (*ep->function)(howto, ep->arg); 19017658Sjulian ep = ep->next; 19117658Sjulian } 19227997Sjulian 19327997Sjulian /* 19427997Sjulian * Now sync filesystems 19527997Sjulian */ 19617658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 19717658Sjulian register struct buf *bp; 19817658Sjulian int iter, nbusy; 19917658Sjulian 20017658Sjulian waittime = 0; 20117658Sjulian printf("\nsyncing disks... "); 20217658Sjulian 20330994Sphk sync(&proc0, NULL); 20417658Sjulian 20534266Sjulian /* 20634266Sjulian * With soft updates, some buffers that are 20734266Sjulian * written will be remarked as dirty until other 20834266Sjulian * buffers are written. 20934266Sjulian */ 21017658Sjulian for (iter = 0; iter < 20; iter++) { 21117658Sjulian nbusy = 0; 21217658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 21317658Sjulian if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) { 21417658Sjulian nbusy++; 21534266Sjulian } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 21634266Sjulian == B_DELWRI) { 21734266Sjulian /* bawrite(bp);*/ 21834266Sjulian nbusy++; 21917658Sjulian } 22017658Sjulian } 22117658Sjulian if (nbusy == 0) 22217658Sjulian break; 22317658Sjulian printf("%d ", nbusy); 22434266Sjulian sync(&proc0, NULL); 22534266Sjulian DELAY(50000 * iter); 22617658Sjulian } 22717658Sjulian if (nbusy) { 22817658Sjulian /* 22917658Sjulian * Failed to sync all blocks. Indicate this and don't 23017658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 23117658Sjulian */ 23217658Sjulian printf("giving up\n"); 23317658Sjulian#ifdef SHOW_BUSYBUFS 23417658Sjulian nbusy = 0; 23517658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 23617658Sjulian if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) { 23717658Sjulian nbusy++; 23837555Sbde printf( 23937555Sbde "%d: dev:%08lx, flags:%08lx, blkno:%ld, lblkno:%ld\n", 24037555Sbde nbusy, (u_long)bp->b_dev, 24137555Sbde bp->b_flags, (long)bp->b_blkno, 24237555Sbde (long)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); 28536735Sdfr#ifdef __i386__ 28617658Sjulian dumppcb.pcb_cr3 = rcr3(); 28736735Sdfr#endif 28817658Sjulian dumpsys(); 28917658Sjulian } 29017658Sjulian 29117658Sjulian if (PANIC_REBOOT_WAIT_TIME != 0) { 29217658Sjulian if (PANIC_REBOOT_WAIT_TIME != -1) { 29317658Sjulian int loop; 29417658Sjulian printf("Automatic reboot in %d seconds - press a key on the console to abort\n", 29517658Sjulian PANIC_REBOOT_WAIT_TIME); 29617658Sjulian for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) { 29717658Sjulian DELAY(1000 * 100); /* 1/10th second */ 29818290Sbde /* Did user type a key? */ 29918290Sbde if (cncheckc() != -1) 30017658Sjulian break; 30117658Sjulian } 30217658Sjulian if (!loop) 30317658Sjulian goto die; 30417658Sjulian } 30517658Sjulian } else { /* zero time specified - reboot NOW */ 30617658Sjulian goto die; 30717658Sjulian } 30817658Sjulian printf("--> Press a key on the console to reboot <--\n"); 30917658Sjulian cngetc(); 31017658Sjulian } 31117658Sjulian } 31217658Sjuliandie: 31317658Sjulian printf("Rebooting...\n"); 31417658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 31517677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 31617658Sjulian cpu_reset(); 31717658Sjulian for(;;) ; 31817658Sjulian /* NOTREACHED */ 31917658Sjulian} 32017658Sjulian 32117658Sjulian/* 32217658Sjulian * Magic number for savecore 32317658Sjulian * 32417658Sjulian * exported (symorder) and used at least by savecore(8) 32517658Sjulian * 32617658Sjulian */ 32717658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 32817658Sjulian 32917658Sjulianstatic int dumpsize = 0; /* also for savecore */ 33017658Sjulian 33117658Sjulianstatic int dodump = 1; 33217658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, ""); 33317658Sjulian 33431403Sjulian/* ARGSUSED */ 33531403Sjulianstatic void dump_conf __P((void *dummy)); 33631403Sjulianstatic void 33731403Sjuliandump_conf(dummy) 33831403Sjulian void *dummy; 33931403Sjulian{ 34031403Sjulian cpu_dumpconf(); 34131403Sjulian} 34231403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 34331403Sjulian 34417658Sjulian/* 34517658Sjulian * Doadump comes here after turning off memory management and 34617658Sjulian * getting on the dump stack, either when called above, or by 34717658Sjulian * the auto-restart code. 34817658Sjulian */ 34917658Sjulianstatic void 35017658Sjuliandumpsys(void) 35117658Sjulian{ 35217658Sjulian 35317658Sjulian if (!dodump) 35417658Sjulian return; 35517658Sjulian if (dumpdev == NODEV) 35617658Sjulian return; 35735974Sbde if ((minor(dumpdev)&07) != 1) 35835974Sbde return; 35917658Sjulian if (!(bdevsw[major(dumpdev)])) 36017658Sjulian return; 36117658Sjulian if (!(bdevsw[major(dumpdev)]->d_dump)) 36217658Sjulian return; 36317658Sjulian dumpsize = Maxmem; 36437555Sbde printf("\ndumping to dev %lx, offset %ld\n", (u_long)dumpdev, dumplo); 36517658Sjulian printf("dump "); 36617658Sjulian switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) { 36717658Sjulian 36817658Sjulian case ENXIO: 36917658Sjulian printf("device bad\n"); 37017658Sjulian break; 37117658Sjulian 37217658Sjulian case EFAULT: 37317658Sjulian printf("device not ready\n"); 37417658Sjulian break; 37517658Sjulian 37617658Sjulian case EINVAL: 37717658Sjulian printf("area improper\n"); 37817658Sjulian break; 37917658Sjulian 38017658Sjulian case EIO: 38117658Sjulian printf("i/o error\n"); 38217658Sjulian break; 38317658Sjulian 38417658Sjulian case EINTR: 38517658Sjulian printf("aborted from console\n"); 38617658Sjulian break; 38717658Sjulian 38817658Sjulian default: 38917658Sjulian printf("succeeded\n"); 39017658Sjulian break; 39117658Sjulian } 39217658Sjulian} 39317658Sjulian 39417658Sjulian/* 39517658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 39617658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 39717658Sjulian * the disks as this often leads to recursive panics. 39817658Sjulian */ 39917658Sjulianvoid 40017658Sjulianpanic(const char *fmt, ...) 40117658Sjulian{ 40217658Sjulian int bootopt; 40317658Sjulian va_list ap; 40417658Sjulian 40517658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 40617658Sjulian if (panicstr) 40717658Sjulian bootopt |= RB_NOSYNC; 40817658Sjulian else 40917658Sjulian panicstr = fmt; 41017658Sjulian 41117658Sjulian printf("panic: "); 41217658Sjulian va_start(ap, fmt); 41317658Sjulian vprintf(fmt, ap); 41417658Sjulian va_end(ap); 41517658Sjulian printf("\n"); 41626100Sfsmp#ifdef SMP 41729128Speter /* three seperate prints in case of an unmapped page and trap */ 41829128Speter printf("mp_lock = %08x; ", mp_lock); 41929128Speter printf("cpuid = %d; ", cpuid); 42029128Speter printf("lapic.id = %08x\n", lapic.id); 42126100Sfsmp#endif 42217658Sjulian 42317658Sjulian#if defined(DDB) 42417658Sjulian if (debugger_on_panic) 42517658Sjulian Debugger ("panic"); 42617658Sjulian#endif 42717658Sjulian boot(bootopt); 42817658Sjulian} 42917658Sjulian 43017768Sjulian/* 43117768Sjulian * Two routines to handle adding/deleting items on the 43217768Sjulian * shutdown callout lists 43317768Sjulian * 43417768Sjulian * at_shutdown(): 43517658Sjulian * Take the arguments given and put them onto the shutdown callout list. 43617658Sjulian * However first make sure that it's not already there. 43717658Sjulian * returns 0 on success. 43817658Sjulian */ 43917658Sjulianint 44017768Sjulianat_shutdown(bootlist_fn function, void *arg, int position) 44117658Sjulian{ 44217768Sjulian sle_p ep, *epp; 44317768Sjulian 44417768Sjulian switch(position) { 44517768Sjulian case SHUTDOWN_PRE_SYNC: 44617768Sjulian epp = &shutdown_list1; 44717768Sjulian break; 44817768Sjulian case SHUTDOWN_POST_SYNC: 44917768Sjulian epp = &shutdown_list2; 45017768Sjulian break; 45117768Sjulian default: 45217768Sjulian printf("bad exit callout list specified\n"); 45317768Sjulian return (EINVAL); 45417768Sjulian } 45517768Sjulian if (rm_at_shutdown(function, arg)) 45617658Sjulian printf("exit callout entry already present\n"); 45717768Sjulian ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 45817768Sjulian if (ep == NULL) 45917768Sjulian return (ENOMEM); 46017768Sjulian ep->next = *epp; 46117658Sjulian ep->function = function; 46217658Sjulian ep->arg = arg; 46317768Sjulian *epp = ep; 46417768Sjulian return (0); 46517658Sjulian} 46617768Sjulian 46717658Sjulian/* 46817768Sjulian * Scan the exit callout lists for the given items and remove them. 46917658Sjulian * Returns the number of items removed. 47017658Sjulian */ 47117658Sjulianint 47217658Sjulianrm_at_shutdown(bootlist_fn function, void *arg) 47317658Sjulian{ 47417768Sjulian sle_p *epp, ep; 47517768Sjulian int count; 47617658Sjulian 47717768Sjulian count = 0; 47817768Sjulian epp = &shutdown_list1; 47917658Sjulian ep = *epp; 48017768Sjulian while (ep) { 48117834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 48217658Sjulian *epp = ep->next; 48317768Sjulian free(ep, M_TEMP); 48417658Sjulian count++; 48517658Sjulian } else { 48617658Sjulian epp = &ep->next; 48717658Sjulian } 48817658Sjulian ep = *epp; 48917658Sjulian } 49017768Sjulian epp = &shutdown_list2; 49117768Sjulian ep = *epp; 49217768Sjulian while (ep) { 49317834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 49417768Sjulian *epp = ep->next; 49517768Sjulian free(ep, M_TEMP); 49617768Sjulian count++; 49717768Sjulian } else { 49817768Sjulian epp = &ep->next; 49917768Sjulian } 50017768Sjulian ep = *epp; 50117768Sjulian } 50217768Sjulian return (count); 50317658Sjulian} 50417658Sjulian 505