kern_shutdown.c revision 75570
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 3950477Speter * $FreeBSD: head/sys/kern/kern_shutdown.c 75570 2001-04-17 04:18:08Z jhb $ 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> 4950107Smsmith#include <sys/eventhandler.h> 5060041Sphk#include <sys/bio.h> 5131275Sbde#include <sys/buf.h> 5217658Sjulian#include <sys/reboot.h> 5317658Sjulian#include <sys/proc.h> 5441137Smsmith#include <sys/vnode.h> 5517658Sjulian#include <sys/kernel.h> 5655539Sluoqi#include <sys/kthread.h> 5721776Sbde#include <sys/mount.h> 5867365Sjhb#include <sys/mutex.h> 5939237Sgibbs#include <sys/queue.h> 6017658Sjulian#include <sys/sysctl.h> 6117658Sjulian#include <sys/conf.h> 6217658Sjulian#include <sys/sysproto.h> 6349558Sphk#include <sys/cons.h> 6417658Sjulian 6517658Sjulian#include <machine/pcb.h> 6617658Sjulian#include <machine/md_var.h> 6726812Speter#include <machine/smp.h> /* smp_active, cpuid */ 6817658Sjulian 6917658Sjulian#include <sys/signalvar.h> 7017658Sjulian 7117658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME 7217658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 7317658Sjulian#endif 7417658Sjulian 7517658Sjulian/* 7617658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both 7717658Sjulian * ANSI and traditional C compilers. 7817658Sjulian */ 7917658Sjulian#include <machine/stdarg.h> 8017658Sjulian 8128769Sbde#ifdef DDB 8217658Sjulian#ifdef DDB_UNATTENDED 8342135Smsmithint debugger_on_panic = 0; 8417658Sjulian#else 8542135Smsmithint debugger_on_panic = 1; 8617658Sjulian#endif 8717658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 8846381Sbillf &debugger_on_panic, 0, "Run debugger on kernel panic"); 8917658Sjulian#endif 9017658Sjulian 9143436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 9243436Smsmith 9328000Sjulian#ifdef HW_WDOG 9417658Sjulian/* 9527997Sjulian * If there is a hardware watchdog, point this at the function needed to 9627997Sjulian * hold it off. 9727997Sjulian * It's needed when the kernel needs to do some lengthy operations. 9827997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have 9927997Sjulian * your precious core-dump only half written because the wdog kicked in. 10027997Sjulian */ 10127997Sjulianwatchdog_tickle_fn wdog_tickler = NULL; 10228000Sjulian#endif /* HW_WDOG */ 10327997Sjulian 10427997Sjulian/* 10517658Sjulian * Variable panicstr contains argument to first call to panic; used as flag 10617658Sjulian * to indicate that the kernel has already called panic. 10717658Sjulian */ 10817658Sjulianconst char *panicstr; 10917658Sjulian 11067093Spsint dumping; /* system is dumping */ 11167093Sps 11265395Speterstatic void boot(int) __dead2; 11365395Speterstatic void dumpsys(void); 11465395Speterstatic void poweroff_wait(void *, int); 11565395Speterstatic void shutdown_halt(void *junk, int howto); 11665395Speterstatic void shutdown_panic(void *junk, int howto); 11765395Speterstatic void shutdown_reset(void *junk, int howto); 11817658Sjulian 11950107Smsmith/* register various local shutdown events */ 12050107Smsmithstatic void 12150107Smsmithshutdown_conf(void *unused) 12250107Smsmith{ 12350107Smsmith EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 12450107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 12550107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 12650107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 12750107Smsmith} 12848868Sphk 12950107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 13050107Smsmith 13117658Sjulian/* ARGSUSED */ 13217658Sjulian 13317658Sjulian/* 13417658Sjulian * The system call that results in a reboot 13517658Sjulian */ 13617658Sjulianint 13765395Speterreboot(struct proc *p, struct reboot_args *uap) 13817658Sjulian{ 13917658Sjulian int error; 14017658Sjulian 14146112Sphk if ((error = suser(p))) 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 */ 15165268Smsmithstatic int shutdown_howto = 0; 15265268Smsmith 15317658Sjulianvoid 15465268Smsmithshutdown_nice(int howto) 15517658Sjulian{ 15665268Smsmith shutdown_howto = howto; 15765268Smsmith 15817658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 15917658Sjulian if (initproc != NULL) { 16073913Sjhb PROC_LOCK(initproc); 16117658Sjulian psignal(initproc, SIGINT); 16273913Sjhb PROC_UNLOCK(initproc); 16317658Sjulian } else { 16417658Sjulian /* No init(8) running, so simply reboot */ 16517658Sjulian boot(RB_NOSYNC); 16617658Sjulian } 16717658Sjulian return; 16817658Sjulian} 16917658Sjulianstatic int waittime = -1; 17017658Sjulianstatic struct pcb dumppcb; 17117658Sjulian 17254233Sphkstatic void 17365395Speterprint_uptime(void) 17454233Sphk{ 17554233Sphk int f; 17654233Sphk struct timespec ts; 17754233Sphk 17854233Sphk getnanouptime(&ts); 17954233Sphk printf("Uptime: "); 18054233Sphk f = 0; 18154233Sphk if (ts.tv_sec >= 86400) { 18265764Sjhb printf("%ldd", (long)ts.tv_sec / 86400); 18354233Sphk ts.tv_sec %= 86400; 18454233Sphk f = 1; 18554233Sphk } 18654233Sphk if (f || ts.tv_sec >= 3600) { 18765764Sjhb printf("%ldh", (long)ts.tv_sec / 3600); 18854233Sphk ts.tv_sec %= 3600; 18954233Sphk f = 1; 19054233Sphk } 19154233Sphk if (f || ts.tv_sec >= 60) { 19265764Sjhb printf("%ldm", (long)ts.tv_sec / 60); 19354233Sphk ts.tv_sec %= 60; 19454233Sphk f = 1; 19554233Sphk } 19665764Sjhb printf("%lds\n", (long)ts.tv_sec); 19754233Sphk} 19854233Sphk 19917658Sjulian/* 20017658Sjulian * Go through the rigmarole of shutting down.. 20117658Sjulian * this used to be in machdep.c but I'll be dammned if I could see 20217658Sjulian * anything machine dependant in it. 20317658Sjulian */ 20431275Sbdestatic void 20565395Speterboot(int howto) 20617658Sjulian{ 20717658Sjulian 20865268Smsmith /* collect extra flags that shutdown_nice might have set */ 20965268Smsmith howto |= shutdown_howto; 21065268Smsmith 21125164Speter#ifdef SMP 21265395Speter if (smp_active) 21370861Sjake printf("boot() called on cpu#%d\n", PCPU_GET(cpuid)); 21425164Speter#endif 21527997Sjulian /* 21627997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 21727997Sjulian */ 21850107Smsmith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 21927997Sjulian 22027997Sjulian /* 22127997Sjulian * Now sync filesystems 22227997Sjulian */ 22317658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 22417658Sjulian register struct buf *bp; 22565707Sjasone int iter, nbusy, pbusy; 22665707Sjasone int subiter; 22717658Sjulian 22817658Sjulian waittime = 0; 22917658Sjulian printf("\nsyncing disks... "); 23017658Sjulian 23130994Sphk sync(&proc0, NULL); 23217658Sjulian 23334266Sjulian /* 23434266Sjulian * With soft updates, some buffers that are 23534266Sjulian * written will be remarked as dirty until other 23634266Sjulian * buffers are written. 23734266Sjulian */ 23865707Sjasone for (iter = pbusy = 0; iter < 20; iter++) { 23917658Sjulian nbusy = 0; 24017658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 24148225Smckusick if ((bp->b_flags & B_INVAL) == 0 && 24248225Smckusick BUF_REFCNT(bp) > 0) { 24317658Sjulian nbusy++; 24434266Sjulian } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 24534266Sjulian == B_DELWRI) { 24634266Sjulian /* bawrite(bp);*/ 24734266Sjulian nbusy++; 24817658Sjulian } 24917658Sjulian } 25017658Sjulian if (nbusy == 0) 25117658Sjulian break; 25217658Sjulian printf("%d ", nbusy); 25365707Sjasone if (nbusy < pbusy) 25465707Sjasone iter = 0; 25565707Sjasone pbusy = nbusy; 25634266Sjulian sync(&proc0, NULL); 25765707Sjasone if (curproc != NULL) { 25868808Sjhb DROP_GIANT_NOSWITCH(); 25965707Sjasone for (subiter = 0; subiter < 50 * iter; subiter++) { 26072200Sbmilekic mtx_lock_spin(&sched_lock); 26165707Sjasone setrunqueue(curproc); 26265707Sjasone mi_switch(); /* Allow interrupt threads to run */ 26372200Sbmilekic mtx_unlock_spin(&sched_lock); 26465707Sjasone DELAY(1000); 26565707Sjasone } 26668808Sjhb PICKUP_GIANT(); 26765707Sjasone } else 26834266Sjulian DELAY(50000 * iter); 26917658Sjulian } 27053023Sphk printf("\n"); 27141137Smsmith /* 27241137Smsmith * Count only busy local buffers to prevent forcing 27341137Smsmith * a fsck if we're just a client of a wedged NFS server 27441137Smsmith */ 27541137Smsmith nbusy = 0; 27641137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 27748225Smckusick if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 27848225Smckusick ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 27953023Sphk if (bp->b_dev == NODEV) { 28053452Sphk TAILQ_REMOVE(&mountlist, 28148225Smckusick bp->b_vp->v_mount, mnt_list); 28253023Sphk continue; 28353023Sphk } 28453023Sphk nbusy++; 28553023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 28653023Sphk printf( 28753023Sphk "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 28853023Sphk nbusy, devtoname(bp->b_dev), 28953023Sphk bp->b_flags, (long)bp->b_blkno, 29053023Sphk (long)bp->b_lblkno); 29153023Sphk#endif 29246568Speter } 29341137Smsmith } 29417658Sjulian if (nbusy) { 29517658Sjulian /* 29617658Sjulian * Failed to sync all blocks. Indicate this and don't 29717658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 29817658Sjulian */ 29953023Sphk printf("giving up on %d buffers\n", nbusy); 30017658Sjulian DELAY(5000000); /* 5 seconds */ 30117658Sjulian } else { 30217658Sjulian printf("done\n"); 30317658Sjulian /* 30417658Sjulian * Unmount filesystems 30517658Sjulian */ 30617658Sjulian if (panicstr == 0) 30717658Sjulian vfs_unmountall(); 30817658Sjulian } 30939237Sgibbs DELAY(100000); /* wait for console output to finish */ 31017658Sjulian } 31127997Sjulian 31254233Sphk print_uptime(); 31354233Sphk 31427997Sjulian /* 31527997Sjulian * Ok, now do things that assume all filesystem activity has 31627997Sjulian * been completed. 31727997Sjulian */ 31850107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 31939237Sgibbs splhigh(); 32065394Speter if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) 32139237Sgibbs dumpsys(); 32239237Sgibbs 32339237Sgibbs /* Now that we're going to really halt the system... */ 32450107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 32539237Sgibbs 32650107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 32750107Smsmith /* NOTREACHED */ 32850107Smsmith} 32950107Smsmith 33050107Smsmith/* 33150107Smsmith * If the shutdown was a clean halt, behave accordingly. 33250107Smsmith */ 33350107Smsmithstatic void 33450107Smsmithshutdown_halt(void *junk, int howto) 33550107Smsmith{ 33617658Sjulian if (howto & RB_HALT) { 33717658Sjulian printf("\n"); 33817658Sjulian printf("The operating system has halted.\n"); 33917658Sjulian printf("Please press any key to reboot.\n\n"); 34019274Sjulian switch (cngetc()) { 34119274Sjulian case -1: /* No console, just die */ 34219274Sjulian cpu_halt(); 34319274Sjulian /* NOTREACHED */ 34419274Sjulian default: 34539237Sgibbs howto &= ~RB_HALT; 34619274Sjulian break; 34719274Sjulian } 34850107Smsmith } 34950107Smsmith} 35017658Sjulian 35150107Smsmith/* 35250107Smsmith * Check to see if the system paniced, pause and then reboot 35350107Smsmith * according to the specified delay. 35450107Smsmith */ 35550107Smsmithstatic void 35650107Smsmithshutdown_panic(void *junk, int howto) 35750107Smsmith{ 35850107Smsmith int loop; 35950107Smsmith 36050107Smsmith if (howto & RB_DUMP) { 36139237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 36239237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 36339237Sgibbs printf("Automatic reboot in %d seconds - " 36439237Sgibbs "press a key on the console to abort\n", 36539237Sgibbs PANIC_REBOOT_WAIT_TIME); 36639237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 36739237Sgibbs loop > 0; --loop) { 36839237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 36939237Sgibbs /* Did user type a key? */ 37039237Sgibbs if (cncheckc() != -1) 37139237Sgibbs break; 37217658Sjulian } 37339237Sgibbs if (!loop) 37450107Smsmith return; 37517658Sjulian } 37639237Sgibbs } else { /* zero time specified - reboot NOW */ 37750107Smsmith return; 37817658Sjulian } 37939237Sgibbs printf("--> Press a key on the console to reboot <--\n"); 38039237Sgibbs cngetc(); 38117658Sjulian } 38250107Smsmith} 38350107Smsmith 38450107Smsmith/* 38550107Smsmith * Everything done, now reset 38650107Smsmith */ 38750107Smsmithstatic void 38850107Smsmithshutdown_reset(void *junk, int howto) 38950107Smsmith{ 39017658Sjulian printf("Rebooting...\n"); 39117658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 39217677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 39317658Sjulian cpu_reset(); 39450107Smsmith /* NOTREACHED */ /* assuming reset worked */ 39517658Sjulian} 39617658Sjulian 39717658Sjulian/* 39817658Sjulian * Magic number for savecore 39917658Sjulian * 40017658Sjulian * exported (symorder) and used at least by savecore(8) 40117658Sjulian * 40217658Sjulian */ 40317658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 40417658Sjulian 40517658Sjulianstatic int dumpsize = 0; /* also for savecore */ 40617658Sjulian 40717658Sjulianstatic int dodump = 1; 40817658Sjulian 40948868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 41048868Sphk "Try to perform coredump on kernel panic"); 41148868Sphk 41248868Sphkstatic int 41365395Spetersetdumpdev(dev_t dev) 41448868Sphk{ 41550571Sphk int psize; 41648868Sphk long newdumplo; 41748868Sphk 41848868Sphk if (dev == NODEV) { 41948868Sphk dumpdev = dev; 42048868Sphk return (0); 42148868Sphk } 42249679Sphk if (devsw(dev) == NULL) 42348868Sphk return (ENXIO); /* XXX is this right? */ 42449679Sphk if (devsw(dev)->d_psize == NULL) 42548868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 42649679Sphk psize = devsw(dev)->d_psize(dev); 42748868Sphk if (psize == -1) 42848868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 42948868Sphk /* 43050571Sphk * XXX should clean up checking in dumpsys() to be more like this. 43148868Sphk */ 43248868Sphk newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE; 43348868Sphk if (newdumplo < 0) 43448868Sphk return (ENOSPC); 43548868Sphk dumpdev = dev; 43648868Sphk dumplo = newdumplo; 43748868Sphk return (0); 43848868Sphk} 43948868Sphk 44048868Sphk 44131403Sjulian/* ARGSUSED */ 44231403Sjulianstatic void 44365395Speterdump_conf(void *dummy) 44431403Sjulian{ 44548868Sphk if (setdumpdev(dumpdev) != 0) 44648868Sphk dumpdev = NODEV; 44731403Sjulian} 44848868Sphk 44931403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 45031403Sjulian 45148868Sphkstatic int 45262573Sphksysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 45348868Sphk{ 45448868Sphk int error; 45548868Sphk udev_t ndumpdev; 45648868Sphk 45753838Sphk ndumpdev = dev2udev(dumpdev); 45848868Sphk error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 45948868Sphk if (error == 0 && req->newptr != NULL) 46053838Sphk error = setdumpdev(udev2dev(ndumpdev, 0)); 46148868Sphk return (error); 46248868Sphk} 46348868Sphk 46448868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 46548868Sphk 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", ""); 46648868Sphk 46717658Sjulian/* 46817658Sjulian * Doadump comes here after turning off memory management and 46917658Sjulian * getting on the dump stack, either when called above, or by 47017658Sjulian * the auto-restart code. 47117658Sjulian */ 47217658Sjulianstatic void 47317658Sjuliandumpsys(void) 47417658Sjulian{ 47549627Salfred int error; 47617658Sjulian 47765394Speter savectx(&dumppcb); 47850571Sphk if (dumping++) { 47950571Sphk printf("Dump already in progress, bailing...\n"); 48050571Sphk return; 48150571Sphk } 48217658Sjulian if (!dodump) 48317658Sjulian return; 48417658Sjulian if (dumpdev == NODEV) 48517658Sjulian return; 48649679Sphk if (!(devsw(dumpdev))) 48717658Sjulian return; 48849679Sphk if (!(devsw(dumpdev)->d_dump)) 48917658Sjulian return; 49017658Sjulian dumpsize = Maxmem; 49150571Sphk printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 49217658Sjulian printf("dump "); 49349679Sphk error = (*devsw(dumpdev)->d_dump)(dumpdev); 49449627Salfred if (error == 0) { 49549627Salfred printf("succeeded\n"); 49649627Salfred return; 49749627Salfred } 49849627Salfred printf("failed, reason: "); 49949627Salfred switch (error) { 50049627Salfred case ENODEV: 50149627Salfred printf("device doesn't support a dump routine\n"); 50249627Salfred break; 50317658Sjulian 50417658Sjulian case ENXIO: 50517658Sjulian printf("device bad\n"); 50617658Sjulian break; 50717658Sjulian 50817658Sjulian case EFAULT: 50917658Sjulian printf("device not ready\n"); 51017658Sjulian break; 51117658Sjulian 51217658Sjulian case EINVAL: 51317658Sjulian printf("area improper\n"); 51417658Sjulian break; 51517658Sjulian 51617658Sjulian case EIO: 51717658Sjulian printf("i/o error\n"); 51817658Sjulian break; 51917658Sjulian 52017658Sjulian case EINTR: 52117658Sjulian printf("aborted from console\n"); 52217658Sjulian break; 52317658Sjulian 52417658Sjulian default: 52549627Salfred printf("unknown, error = %d\n", error); 52617658Sjulian break; 52717658Sjulian } 52817658Sjulian} 52917658Sjulian 53074890Spsint 53174890Spsdumpstatus(vm_offset_t addr, long count) 53274890Sps{ 53374890Sps int c; 53474890Sps 53574890Sps if (addr % (1024 * 1024) == 0) { 53674890Sps#ifdef HW_WDOG 53774890Sps if (wdog_tickler) 53874890Sps (*wdog_tickler)(); 53974890Sps#endif 54074890Sps printf("%ld ", count / (1024 * 1024)); 54174890Sps } 54274890Sps 54374890Sps if ((c = cncheckc()) == 0x03) 54474890Sps return -1; 54574890Sps else if (c != -1) 54674890Sps printf("[CTRL-C to abort] "); 54774890Sps 54874890Sps return 0; 54974890Sps} 55074890Sps 55175570Sjhb#ifdef SMP 55275570Sjhbstatic u_int panic_cpu = NOCPU; 55375570Sjhb#endif 55475570Sjhb 55517658Sjulian/* 55617658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 55717658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 55817658Sjulian * the disks as this often leads to recursive panics. 55917658Sjulian */ 56017658Sjulianvoid 56117658Sjulianpanic(const char *fmt, ...) 56217658Sjulian{ 56317658Sjulian int bootopt; 56417658Sjulian va_list ap; 56538874Sache static char buf[256]; 56617658Sjulian 56765557Sjasone#ifdef SMP 56865557Sjasone /* Only 1 CPU can panic at a time */ 56975570Sjhb if (panic_cpu != PCPU_GET(cpuid) && 57075570Sjhb atomic_cmpset_int(&panic_cpu, NOCPU, PCPU_GET(cpuid)) == 0) { 57175570Sjhb for (;;) 57275570Sjhb ; /* nothing */ 57375570Sjhb } 57465557Sjasone#endif 57565557Sjasone 57617658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 57717658Sjulian if (panicstr) 57817658Sjulian bootopt |= RB_NOSYNC; 57917658Sjulian else 58017658Sjulian panicstr = fmt; 58117658Sjulian 58217658Sjulian va_start(ap, fmt); 58341514Sarchie (void)vsnprintf(buf, sizeof(buf), fmt, ap); 58438874Sache if (panicstr == fmt) 58538874Sache panicstr = buf; 58617658Sjulian va_end(ap); 58738874Sache printf("panic: %s\n", buf); 58826100Sfsmp#ifdef SMP 58972091Sasmodai /* two separate prints in case of an unmapped page and trap */ 59070861Sjake printf("cpuid = %d; ", PCPU_GET(cpuid)); 59169335Sjhb#ifdef APIC_IO 59229128Speter printf("lapic.id = %08x\n", lapic.id); 59326100Sfsmp#endif 59469335Sjhb#endif 59517658Sjulian 59617658Sjulian#if defined(DDB) 59717658Sjulian if (debugger_on_panic) 59817658Sjulian Debugger ("panic"); 59917658Sjulian#endif 60017658Sjulian boot(bootopt); 60117658Sjulian} 60217658Sjulian 60317768Sjulian/* 60443436Smsmith * Support for poweroff delay. 60543436Smsmith */ 60654248Smsmith#ifndef POWEROFF_DELAY 60754248Smsmith# define POWEROFF_DELAY 5000 60854248Smsmith#endif 60954248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 61054248Smsmith 61143436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 61243436Smsmith &poweroff_delay, 0, ""); 61343436Smsmith 61450107Smsmithstatic void 61550107Smsmithpoweroff_wait(void *junk, int howto) 61643436Smsmith{ 61743436Smsmith if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 61843436Smsmith return; 61943436Smsmith DELAY(poweroff_delay * 1000); 62043436Smsmith} 62155539Sluoqi 62255539Sluoqi/* 62355539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 62455539Sluoqi * points in their main loops prior to a system shutdown, so that they 62555539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 62655539Sluoqi * to cause sync to fail). For each of these system processes, register 62755539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 62855539Sluoqi */ 62955539Sluoqistatic int kproc_shutdown_wait = 60; 63055539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 63155539Sluoqi &kproc_shutdown_wait, 0, ""); 63255539Sluoqi 63355539Sluoqivoid 63470063Sjhbkproc_shutdown(void *arg, int howto) 63555539Sluoqi{ 63655539Sluoqi struct proc *p; 63755539Sluoqi int error; 63855539Sluoqi 63955539Sluoqi if (panicstr) 64055539Sluoqi return; 64155539Sluoqi 64255539Sluoqi p = (struct proc *)arg; 64355539Sluoqi printf("Waiting (max %d seconds) for system process `%s' to stop...", 64455862Sluoqi kproc_shutdown_wait, p->p_comm); 64570063Sjhb error = kthread_suspend(p, kproc_shutdown_wait * hz); 64655539Sluoqi 64755539Sluoqi if (error == EWOULDBLOCK) 64855539Sluoqi printf("timed out\n"); 64955539Sluoqi else 65055539Sluoqi printf("stopped\n"); 65155539Sluoqi} 652