kern_shutdown.c revision 81688
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 81688 2001-08-15 11:35:45Z bde $ 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> 4960041Sphk#include <sys/bio.h> 5031275Sbde#include <sys/buf.h> 5178767Sjhb#include <sys/conf.h> 5278767Sjhb#include <sys/cons.h> 5381688Sbde#include <sys/disklabel.h> 5478767Sjhb#include <sys/eventhandler.h> 5517658Sjulian#include <sys/kernel.h> 5655539Sluoqi#include <sys/kthread.h> 5721776Sbde#include <sys/mount.h> 5878767Sjhb#include <sys/proc.h> 5978767Sjhb#include <sys/reboot.h> 6078767Sjhb#include <sys/resourcevar.h> 6178767Sjhb#include <sys/smp.h> /* smp_active */ 6217658Sjulian#include <sys/sysctl.h> 6317658Sjulian#include <sys/sysproto.h> 6478767Sjhb#include <sys/vnode.h> 6517658Sjulian 6617658Sjulian#include <machine/pcb.h> 6717658Sjulian#include <machine/md_var.h> 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); 26278767Sjhb curproc->p_stats->p_ru.ru_nvcsw++; 26365707Sjasone mi_switch(); /* Allow interrupt threads to run */ 26472200Sbmilekic mtx_unlock_spin(&sched_lock); 26565707Sjasone DELAY(1000); 26665707Sjasone } 26768808Sjhb PICKUP_GIANT(); 26865707Sjasone } else 26934266Sjulian DELAY(50000 * iter); 27017658Sjulian } 27153023Sphk printf("\n"); 27241137Smsmith /* 27341137Smsmith * Count only busy local buffers to prevent forcing 27441137Smsmith * a fsck if we're just a client of a wedged NFS server 27541137Smsmith */ 27641137Smsmith nbusy = 0; 27741137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 27848225Smckusick if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 27948225Smckusick ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 28053023Sphk if (bp->b_dev == NODEV) { 28153452Sphk TAILQ_REMOVE(&mountlist, 28248225Smckusick bp->b_vp->v_mount, mnt_list); 28353023Sphk continue; 28453023Sphk } 28553023Sphk nbusy++; 28653023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 28753023Sphk printf( 28853023Sphk "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 28953023Sphk nbusy, devtoname(bp->b_dev), 29053023Sphk bp->b_flags, (long)bp->b_blkno, 29153023Sphk (long)bp->b_lblkno); 29253023Sphk#endif 29346568Speter } 29441137Smsmith } 29517658Sjulian if (nbusy) { 29617658Sjulian /* 29717658Sjulian * Failed to sync all blocks. Indicate this and don't 29817658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 29917658Sjulian */ 30053023Sphk printf("giving up on %d buffers\n", nbusy); 30117658Sjulian DELAY(5000000); /* 5 seconds */ 30217658Sjulian } else { 30317658Sjulian printf("done\n"); 30417658Sjulian /* 30517658Sjulian * Unmount filesystems 30617658Sjulian */ 30717658Sjulian if (panicstr == 0) 30817658Sjulian vfs_unmountall(); 30917658Sjulian } 31039237Sgibbs DELAY(100000); /* wait for console output to finish */ 31117658Sjulian } 31227997Sjulian 31354233Sphk print_uptime(); 31454233Sphk 31527997Sjulian /* 31627997Sjulian * Ok, now do things that assume all filesystem activity has 31727997Sjulian * been completed. 31827997Sjulian */ 31950107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 32039237Sgibbs splhigh(); 32165394Speter if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) 32239237Sgibbs dumpsys(); 32339237Sgibbs 32439237Sgibbs /* Now that we're going to really halt the system... */ 32550107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 32639237Sgibbs 32750107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 32850107Smsmith /* NOTREACHED */ 32950107Smsmith} 33050107Smsmith 33150107Smsmith/* 33250107Smsmith * If the shutdown was a clean halt, behave accordingly. 33350107Smsmith */ 33450107Smsmithstatic void 33550107Smsmithshutdown_halt(void *junk, int howto) 33650107Smsmith{ 33717658Sjulian if (howto & RB_HALT) { 33817658Sjulian printf("\n"); 33917658Sjulian printf("The operating system has halted.\n"); 34017658Sjulian printf("Please press any key to reboot.\n\n"); 34119274Sjulian switch (cngetc()) { 34219274Sjulian case -1: /* No console, just die */ 34319274Sjulian cpu_halt(); 34419274Sjulian /* NOTREACHED */ 34519274Sjulian default: 34639237Sgibbs howto &= ~RB_HALT; 34719274Sjulian break; 34819274Sjulian } 34950107Smsmith } 35050107Smsmith} 35117658Sjulian 35250107Smsmith/* 35350107Smsmith * Check to see if the system paniced, pause and then reboot 35450107Smsmith * according to the specified delay. 35550107Smsmith */ 35650107Smsmithstatic void 35750107Smsmithshutdown_panic(void *junk, int howto) 35850107Smsmith{ 35950107Smsmith int loop; 36050107Smsmith 36150107Smsmith if (howto & RB_DUMP) { 36239237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 36339237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 36439237Sgibbs printf("Automatic reboot in %d seconds - " 36539237Sgibbs "press a key on the console to abort\n", 36639237Sgibbs PANIC_REBOOT_WAIT_TIME); 36739237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 36839237Sgibbs loop > 0; --loop) { 36939237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 37039237Sgibbs /* Did user type a key? */ 37139237Sgibbs if (cncheckc() != -1) 37239237Sgibbs break; 37317658Sjulian } 37439237Sgibbs if (!loop) 37550107Smsmith return; 37617658Sjulian } 37739237Sgibbs } else { /* zero time specified - reboot NOW */ 37850107Smsmith return; 37917658Sjulian } 38039237Sgibbs printf("--> Press a key on the console to reboot <--\n"); 38139237Sgibbs cngetc(); 38217658Sjulian } 38350107Smsmith} 38450107Smsmith 38550107Smsmith/* 38650107Smsmith * Everything done, now reset 38750107Smsmith */ 38850107Smsmithstatic void 38950107Smsmithshutdown_reset(void *junk, int howto) 39050107Smsmith{ 39117658Sjulian printf("Rebooting...\n"); 39217658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 39317677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 39417658Sjulian cpu_reset(); 39550107Smsmith /* NOTREACHED */ /* assuming reset worked */ 39617658Sjulian} 39717658Sjulian 39817658Sjulian/* 39917658Sjulian * Magic number for savecore 40017658Sjulian * 40117658Sjulian * exported (symorder) and used at least by savecore(8) 40217658Sjulian * 40317658Sjulian */ 40417658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 40517658Sjulian 40617658Sjulianstatic int dumpsize = 0; /* also for savecore */ 40717658Sjulian 40817658Sjulianstatic int dodump = 1; 40917658Sjulian 41048868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 41148868Sphk "Try to perform coredump on kernel panic"); 41248868Sphk 41348868Sphkstatic int 41465395Spetersetdumpdev(dev_t dev) 41548868Sphk{ 41650571Sphk int psize; 41748868Sphk long newdumplo; 41848868Sphk 41948868Sphk if (dev == NODEV) { 42048868Sphk dumpdev = dev; 42148868Sphk return (0); 42248868Sphk } 42349679Sphk if (devsw(dev) == NULL) 42448868Sphk return (ENXIO); /* XXX is this right? */ 42549679Sphk if (devsw(dev)->d_psize == NULL) 42648868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 42749679Sphk psize = devsw(dev)->d_psize(dev); 42848868Sphk if (psize == -1) 42948868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 43048868Sphk /* 43150571Sphk * XXX should clean up checking in dumpsys() to be more like this. 43248868Sphk */ 43381688Sbde newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE); 43481688Sbde if (newdumplo <= LABELSECTOR) 43548868Sphk return (ENOSPC); 43648868Sphk dumpdev = dev; 43748868Sphk dumplo = newdumplo; 43848868Sphk return (0); 43948868Sphk} 44048868Sphk 44148868Sphk 44231403Sjulian/* ARGSUSED */ 44331403Sjulianstatic void 44465395Speterdump_conf(void *dummy) 44531403Sjulian{ 44648868Sphk if (setdumpdev(dumpdev) != 0) 44748868Sphk dumpdev = NODEV; 44831403Sjulian} 44948868Sphk 45031403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 45131403Sjulian 45248868Sphkstatic int 45362573Sphksysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 45448868Sphk{ 45548868Sphk int error; 45648868Sphk udev_t ndumpdev; 45748868Sphk 45853838Sphk ndumpdev = dev2udev(dumpdev); 45948868Sphk error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 46048868Sphk if (error == 0 && req->newptr != NULL) 46153838Sphk error = setdumpdev(udev2dev(ndumpdev, 0)); 46248868Sphk return (error); 46348868Sphk} 46448868Sphk 46548868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 46648868Sphk 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", ""); 46748868Sphk 46817658Sjulian/* 46917658Sjulian * Doadump comes here after turning off memory management and 47017658Sjulian * getting on the dump stack, either when called above, or by 47117658Sjulian * the auto-restart code. 47217658Sjulian */ 47317658Sjulianstatic void 47417658Sjuliandumpsys(void) 47517658Sjulian{ 47649627Salfred int error; 47717658Sjulian 47865394Speter savectx(&dumppcb); 47950571Sphk if (dumping++) { 48050571Sphk printf("Dump already in progress, bailing...\n"); 48150571Sphk return; 48250571Sphk } 48317658Sjulian if (!dodump) 48417658Sjulian return; 48517658Sjulian if (dumpdev == NODEV) 48617658Sjulian return; 48749679Sphk if (!(devsw(dumpdev))) 48817658Sjulian return; 48949679Sphk if (!(devsw(dumpdev)->d_dump)) 49017658Sjulian return; 49117658Sjulian dumpsize = Maxmem; 49250571Sphk printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 49317658Sjulian printf("dump "); 49449679Sphk error = (*devsw(dumpdev)->d_dump)(dumpdev); 49549627Salfred if (error == 0) { 49649627Salfred printf("succeeded\n"); 49749627Salfred return; 49849627Salfred } 49949627Salfred printf("failed, reason: "); 50049627Salfred switch (error) { 50149627Salfred case ENODEV: 50249627Salfred printf("device doesn't support a dump routine\n"); 50349627Salfred break; 50417658Sjulian 50517658Sjulian case ENXIO: 50617658Sjulian printf("device bad\n"); 50717658Sjulian break; 50817658Sjulian 50917658Sjulian case EFAULT: 51017658Sjulian printf("device not ready\n"); 51117658Sjulian break; 51217658Sjulian 51317658Sjulian case EINVAL: 51417658Sjulian printf("area improper\n"); 51517658Sjulian break; 51617658Sjulian 51717658Sjulian case EIO: 51817658Sjulian printf("i/o error\n"); 51917658Sjulian break; 52017658Sjulian 52117658Sjulian case EINTR: 52217658Sjulian printf("aborted from console\n"); 52317658Sjulian break; 52417658Sjulian 52517658Sjulian default: 52649627Salfred printf("unknown, error = %d\n", error); 52717658Sjulian break; 52817658Sjulian } 52917658Sjulian} 53017658Sjulian 53174890Spsint 53274890Spsdumpstatus(vm_offset_t addr, long count) 53374890Sps{ 53474890Sps int c; 53574890Sps 53674890Sps if (addr % (1024 * 1024) == 0) { 53774890Sps#ifdef HW_WDOG 53874890Sps if (wdog_tickler) 53974890Sps (*wdog_tickler)(); 54074890Sps#endif 54174890Sps printf("%ld ", count / (1024 * 1024)); 54274890Sps } 54374890Sps 54474890Sps if ((c = cncheckc()) == 0x03) 54574890Sps return -1; 54674890Sps else if (c != -1) 54774890Sps printf("[CTRL-C to abort] "); 54874890Sps 54974890Sps return 0; 55074890Sps} 55174890Sps 55275570Sjhb#ifdef SMP 55375570Sjhbstatic u_int panic_cpu = NOCPU; 55475570Sjhb#endif 55575570Sjhb 55617658Sjulian/* 55717658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 55817658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 55917658Sjulian * the disks as this often leads to recursive panics. 56017658Sjulian */ 56117658Sjulianvoid 56217658Sjulianpanic(const char *fmt, ...) 56317658Sjulian{ 56417658Sjulian int bootopt; 56517658Sjulian va_list ap; 56638874Sache static char buf[256]; 56717658Sjulian 56865557Sjasone#ifdef SMP 56965557Sjasone /* Only 1 CPU can panic at a time */ 57075570Sjhb if (panic_cpu != PCPU_GET(cpuid) && 57175570Sjhb atomic_cmpset_int(&panic_cpu, NOCPU, PCPU_GET(cpuid)) == 0) { 57275570Sjhb for (;;) 57375570Sjhb ; /* nothing */ 57475570Sjhb } 57565557Sjasone#endif 57665557Sjasone 57717658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 57817658Sjulian if (panicstr) 57917658Sjulian bootopt |= RB_NOSYNC; 58017658Sjulian else 58117658Sjulian panicstr = fmt; 58217658Sjulian 58317658Sjulian va_start(ap, fmt); 58441514Sarchie (void)vsnprintf(buf, sizeof(buf), fmt, ap); 58538874Sache if (panicstr == fmt) 58638874Sache panicstr = buf; 58717658Sjulian va_end(ap); 58838874Sache printf("panic: %s\n", buf); 58926100Sfsmp#ifdef SMP 59072091Sasmodai /* two separate prints in case of an unmapped page and trap */ 59170861Sjake printf("cpuid = %d; ", PCPU_GET(cpuid)); 59269335Sjhb#ifdef APIC_IO 59329128Speter printf("lapic.id = %08x\n", lapic.id); 59426100Sfsmp#endif 59569335Sjhb#endif 59617658Sjulian 59717658Sjulian#if defined(DDB) 59817658Sjulian if (debugger_on_panic) 59917658Sjulian Debugger ("panic"); 60017658Sjulian#endif 60117658Sjulian boot(bootopt); 60217658Sjulian} 60317658Sjulian 60417768Sjulian/* 60543436Smsmith * Support for poweroff delay. 60643436Smsmith */ 60754248Smsmith#ifndef POWEROFF_DELAY 60854248Smsmith# define POWEROFF_DELAY 5000 60954248Smsmith#endif 61054248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 61154248Smsmith 61243436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 61343436Smsmith &poweroff_delay, 0, ""); 61443436Smsmith 61550107Smsmithstatic void 61650107Smsmithpoweroff_wait(void *junk, int howto) 61743436Smsmith{ 61843436Smsmith if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 61943436Smsmith return; 62043436Smsmith DELAY(poweroff_delay * 1000); 62143436Smsmith} 62255539Sluoqi 62355539Sluoqi/* 62455539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 62555539Sluoqi * points in their main loops prior to a system shutdown, so that they 62655539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 62755539Sluoqi * to cause sync to fail). For each of these system processes, register 62855539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 62955539Sluoqi */ 63055539Sluoqistatic int kproc_shutdown_wait = 60; 63155539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 63255539Sluoqi &kproc_shutdown_wait, 0, ""); 63355539Sluoqi 63455539Sluoqivoid 63570063Sjhbkproc_shutdown(void *arg, int howto) 63655539Sluoqi{ 63755539Sluoqi struct proc *p; 63855539Sluoqi int error; 63955539Sluoqi 64055539Sluoqi if (panicstr) 64155539Sluoqi return; 64255539Sluoqi 64355539Sluoqi p = (struct proc *)arg; 64455539Sluoqi printf("Waiting (max %d seconds) for system process `%s' to stop...", 64555862Sluoqi kproc_shutdown_wait, p->p_comm); 64670063Sjhb error = kthread_suspend(p, kproc_shutdown_wait * hz); 64755539Sluoqi 64855539Sluoqi if (error == EWOULDBLOCK) 64955539Sluoqi printf("timed out\n"); 65055539Sluoqi else 65155539Sluoqi printf("stopped\n"); 65255539Sluoqi} 653