kern_shutdown.c revision 82223
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 82223 2001-08-23 20:32:21Z 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> 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> 7082119Sjhb#ifdef DDB 7182119Sjhb#include <ddb/ddb.h> 7282119Sjhb#endif 7317658Sjulian 7417658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME 7517658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 7617658Sjulian#endif 7717658Sjulian 7817658Sjulian/* 7917658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both 8017658Sjulian * ANSI and traditional C compilers. 8117658Sjulian */ 8217658Sjulian#include <machine/stdarg.h> 8317658Sjulian 8428769Sbde#ifdef DDB 8517658Sjulian#ifdef DDB_UNATTENDED 8642135Smsmithint debugger_on_panic = 0; 8717658Sjulian#else 8842135Smsmithint debugger_on_panic = 1; 8917658Sjulian#endif 9017658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 9146381Sbillf &debugger_on_panic, 0, "Run debugger on kernel panic"); 9217658Sjulian#endif 9317658Sjulian 9443436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 9543436Smsmith 9628000Sjulian#ifdef HW_WDOG 9717658Sjulian/* 9827997Sjulian * If there is a hardware watchdog, point this at the function needed to 9927997Sjulian * hold it off. 10027997Sjulian * It's needed when the kernel needs to do some lengthy operations. 10127997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have 10227997Sjulian * your precious core-dump only half written because the wdog kicked in. 10327997Sjulian */ 10427997Sjulianwatchdog_tickle_fn wdog_tickler = NULL; 10528000Sjulian#endif /* HW_WDOG */ 10627997Sjulian 10727997Sjulian/* 10817658Sjulian * Variable panicstr contains argument to first call to panic; used as flag 10917658Sjulian * to indicate that the kernel has already called panic. 11017658Sjulian */ 11117658Sjulianconst char *panicstr; 11217658Sjulian 11367093Spsint dumping; /* system is dumping */ 11467093Sps 11565395Speterstatic void boot(int) __dead2; 11665395Speterstatic void dumpsys(void); 11765395Speterstatic void poweroff_wait(void *, int); 11865395Speterstatic void shutdown_halt(void *junk, int howto); 11965395Speterstatic void shutdown_panic(void *junk, int howto); 12065395Speterstatic void shutdown_reset(void *junk, int howto); 12117658Sjulian 12250107Smsmith/* register various local shutdown events */ 12350107Smsmithstatic void 12450107Smsmithshutdown_conf(void *unused) 12550107Smsmith{ 12650107Smsmith EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 12750107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 12850107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 12950107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 13050107Smsmith} 13148868Sphk 13250107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 13350107Smsmith 13417658Sjulian/* ARGSUSED */ 13517658Sjulian 13617658Sjulian/* 13717658Sjulian * The system call that results in a reboot 13817658Sjulian */ 13917658Sjulianint 14065395Speterreboot(struct proc *p, struct reboot_args *uap) 14117658Sjulian{ 14217658Sjulian int error; 14317658Sjulian 14446112Sphk if ((error = suser(p))) 14517658Sjulian return (error); 14617658Sjulian 14717658Sjulian boot(uap->opt); 14817658Sjulian return (0); 14917658Sjulian} 15017658Sjulian 15117658Sjulian/* 15217658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 15317658Sjulian */ 15465268Smsmithstatic int shutdown_howto = 0; 15565268Smsmith 15617658Sjulianvoid 15765268Smsmithshutdown_nice(int howto) 15817658Sjulian{ 15965268Smsmith shutdown_howto = howto; 16065268Smsmith 16117658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 16217658Sjulian if (initproc != NULL) { 16373913Sjhb PROC_LOCK(initproc); 16417658Sjulian psignal(initproc, SIGINT); 16573913Sjhb PROC_UNLOCK(initproc); 16617658Sjulian } else { 16717658Sjulian /* No init(8) running, so simply reboot */ 16817658Sjulian boot(RB_NOSYNC); 16917658Sjulian } 17017658Sjulian return; 17117658Sjulian} 17217658Sjulianstatic int waittime = -1; 17317658Sjulianstatic struct pcb dumppcb; 17417658Sjulian 17554233Sphkstatic void 17665395Speterprint_uptime(void) 17754233Sphk{ 17854233Sphk int f; 17954233Sphk struct timespec ts; 18054233Sphk 18154233Sphk getnanouptime(&ts); 18254233Sphk printf("Uptime: "); 18354233Sphk f = 0; 18454233Sphk if (ts.tv_sec >= 86400) { 18565764Sjhb printf("%ldd", (long)ts.tv_sec / 86400); 18654233Sphk ts.tv_sec %= 86400; 18754233Sphk f = 1; 18854233Sphk } 18954233Sphk if (f || ts.tv_sec >= 3600) { 19065764Sjhb printf("%ldh", (long)ts.tv_sec / 3600); 19154233Sphk ts.tv_sec %= 3600; 19254233Sphk f = 1; 19354233Sphk } 19454233Sphk if (f || ts.tv_sec >= 60) { 19565764Sjhb printf("%ldm", (long)ts.tv_sec / 60); 19654233Sphk ts.tv_sec %= 60; 19754233Sphk f = 1; 19854233Sphk } 19965764Sjhb printf("%lds\n", (long)ts.tv_sec); 20054233Sphk} 20154233Sphk 20217658Sjulian/* 20317658Sjulian * Go through the rigmarole of shutting down.. 20417658Sjulian * this used to be in machdep.c but I'll be dammned if I could see 20517658Sjulian * anything machine dependant in it. 20617658Sjulian */ 20731275Sbdestatic void 20865395Speterboot(int howto) 20917658Sjulian{ 21017658Sjulian 21165268Smsmith /* collect extra flags that shutdown_nice might have set */ 21265268Smsmith howto |= shutdown_howto; 21365268Smsmith 21482119Sjhb#ifdef DDB 21582119Sjhb /* We are out of the debugger now. */ 21682119Sjhb db_active = 0; 21782119Sjhb#endif 21882119Sjhb 21925164Speter#ifdef SMP 22065395Speter if (smp_active) 22170861Sjake printf("boot() called on cpu#%d\n", PCPU_GET(cpuid)); 22225164Speter#endif 22327997Sjulian /* 22427997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 22527997Sjulian */ 22650107Smsmith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 22727997Sjulian 22827997Sjulian /* 22927997Sjulian * Now sync filesystems 23027997Sjulian */ 23117658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 23217658Sjulian register struct buf *bp; 23365707Sjasone int iter, nbusy, pbusy; 23465707Sjasone int subiter; 23517658Sjulian 23617658Sjulian waittime = 0; 23717658Sjulian printf("\nsyncing disks... "); 23817658Sjulian 23930994Sphk sync(&proc0, NULL); 24017658Sjulian 24134266Sjulian /* 24234266Sjulian * With soft updates, some buffers that are 24334266Sjulian * written will be remarked as dirty until other 24434266Sjulian * buffers are written. 24534266Sjulian */ 24665707Sjasone for (iter = pbusy = 0; iter < 20; iter++) { 24717658Sjulian nbusy = 0; 24817658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 24948225Smckusick if ((bp->b_flags & B_INVAL) == 0 && 25048225Smckusick BUF_REFCNT(bp) > 0) { 25117658Sjulian nbusy++; 25234266Sjulian } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 25334266Sjulian == B_DELWRI) { 25434266Sjulian /* bawrite(bp);*/ 25534266Sjulian nbusy++; 25617658Sjulian } 25717658Sjulian } 25817658Sjulian if (nbusy == 0) 25917658Sjulian break; 26017658Sjulian printf("%d ", nbusy); 26165707Sjasone if (nbusy < pbusy) 26265707Sjasone iter = 0; 26365707Sjasone pbusy = nbusy; 26434266Sjulian sync(&proc0, NULL); 26565707Sjasone if (curproc != NULL) { 26668808Sjhb DROP_GIANT_NOSWITCH(); 26765707Sjasone for (subiter = 0; subiter < 50 * iter; subiter++) { 26872200Sbmilekic mtx_lock_spin(&sched_lock); 26965707Sjasone setrunqueue(curproc); 27078767Sjhb curproc->p_stats->p_ru.ru_nvcsw++; 27165707Sjasone mi_switch(); /* Allow interrupt threads to run */ 27272200Sbmilekic mtx_unlock_spin(&sched_lock); 27365707Sjasone DELAY(1000); 27465707Sjasone } 27568808Sjhb PICKUP_GIANT(); 27665707Sjasone } else 27734266Sjulian DELAY(50000 * iter); 27817658Sjulian } 27953023Sphk printf("\n"); 28041137Smsmith /* 28141137Smsmith * Count only busy local buffers to prevent forcing 28241137Smsmith * a fsck if we're just a client of a wedged NFS server 28341137Smsmith */ 28441137Smsmith nbusy = 0; 28541137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 28648225Smckusick if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 28748225Smckusick ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 28853023Sphk if (bp->b_dev == NODEV) { 28953452Sphk TAILQ_REMOVE(&mountlist, 29048225Smckusick bp->b_vp->v_mount, mnt_list); 29153023Sphk continue; 29253023Sphk } 29353023Sphk nbusy++; 29453023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 29553023Sphk printf( 29653023Sphk "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 29753023Sphk nbusy, devtoname(bp->b_dev), 29853023Sphk bp->b_flags, (long)bp->b_blkno, 29953023Sphk (long)bp->b_lblkno); 30053023Sphk#endif 30146568Speter } 30241137Smsmith } 30317658Sjulian if (nbusy) { 30417658Sjulian /* 30517658Sjulian * Failed to sync all blocks. Indicate this and don't 30617658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 30717658Sjulian */ 30853023Sphk printf("giving up on %d buffers\n", nbusy); 30917658Sjulian DELAY(5000000); /* 5 seconds */ 31017658Sjulian } else { 31117658Sjulian printf("done\n"); 31217658Sjulian /* 31317658Sjulian * Unmount filesystems 31417658Sjulian */ 31517658Sjulian if (panicstr == 0) 31617658Sjulian vfs_unmountall(); 31717658Sjulian } 31839237Sgibbs DELAY(100000); /* wait for console output to finish */ 31917658Sjulian } 32027997Sjulian 32154233Sphk print_uptime(); 32254233Sphk 32327997Sjulian /* 32427997Sjulian * Ok, now do things that assume all filesystem activity has 32527997Sjulian * been completed. 32627997Sjulian */ 32750107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 32839237Sgibbs splhigh(); 32965394Speter if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) 33039237Sgibbs dumpsys(); 33139237Sgibbs 33239237Sgibbs /* Now that we're going to really halt the system... */ 33350107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 33439237Sgibbs 33550107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 33650107Smsmith /* NOTREACHED */ 33750107Smsmith} 33850107Smsmith 33950107Smsmith/* 34050107Smsmith * If the shutdown was a clean halt, behave accordingly. 34150107Smsmith */ 34250107Smsmithstatic void 34350107Smsmithshutdown_halt(void *junk, int howto) 34450107Smsmith{ 34517658Sjulian if (howto & RB_HALT) { 34617658Sjulian printf("\n"); 34717658Sjulian printf("The operating system has halted.\n"); 34817658Sjulian printf("Please press any key to reboot.\n\n"); 34919274Sjulian switch (cngetc()) { 35019274Sjulian case -1: /* No console, just die */ 35119274Sjulian cpu_halt(); 35219274Sjulian /* NOTREACHED */ 35319274Sjulian default: 35439237Sgibbs howto &= ~RB_HALT; 35519274Sjulian break; 35619274Sjulian } 35750107Smsmith } 35850107Smsmith} 35917658Sjulian 36050107Smsmith/* 36150107Smsmith * Check to see if the system paniced, pause and then reboot 36250107Smsmith * according to the specified delay. 36350107Smsmith */ 36450107Smsmithstatic void 36550107Smsmithshutdown_panic(void *junk, int howto) 36650107Smsmith{ 36750107Smsmith int loop; 36850107Smsmith 36950107Smsmith if (howto & RB_DUMP) { 37039237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 37139237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 37239237Sgibbs printf("Automatic reboot in %d seconds - " 37339237Sgibbs "press a key on the console to abort\n", 37439237Sgibbs PANIC_REBOOT_WAIT_TIME); 37539237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 37639237Sgibbs loop > 0; --loop) { 37739237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 37839237Sgibbs /* Did user type a key? */ 37939237Sgibbs if (cncheckc() != -1) 38039237Sgibbs break; 38117658Sjulian } 38239237Sgibbs if (!loop) 38350107Smsmith return; 38417658Sjulian } 38539237Sgibbs } else { /* zero time specified - reboot NOW */ 38650107Smsmith return; 38717658Sjulian } 38839237Sgibbs printf("--> Press a key on the console to reboot <--\n"); 38939237Sgibbs cngetc(); 39017658Sjulian } 39150107Smsmith} 39250107Smsmith 39350107Smsmith/* 39450107Smsmith * Everything done, now reset 39550107Smsmith */ 39650107Smsmithstatic void 39750107Smsmithshutdown_reset(void *junk, int howto) 39850107Smsmith{ 39917658Sjulian printf("Rebooting...\n"); 40017658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 40117677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 40217658Sjulian cpu_reset(); 40350107Smsmith /* NOTREACHED */ /* assuming reset worked */ 40417658Sjulian} 40517658Sjulian 40617658Sjulian/* 40717658Sjulian * Magic number for savecore 40817658Sjulian * 40917658Sjulian * exported (symorder) and used at least by savecore(8) 41017658Sjulian * 41117658Sjulian */ 41217658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 41317658Sjulian 41417658Sjulianstatic int dumpsize = 0; /* also for savecore */ 41517658Sjulian 41617658Sjulianstatic int dodump = 1; 41717658Sjulian 41848868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 41948868Sphk "Try to perform coredump on kernel panic"); 42048868Sphk 42148868Sphkstatic int 42265395Spetersetdumpdev(dev_t dev) 42348868Sphk{ 42450571Sphk int psize; 42548868Sphk long newdumplo; 42648868Sphk 42748868Sphk if (dev == NODEV) { 42848868Sphk dumpdev = dev; 42948868Sphk return (0); 43048868Sphk } 43149679Sphk if (devsw(dev) == NULL) 43248868Sphk return (ENXIO); /* XXX is this right? */ 43349679Sphk if (devsw(dev)->d_psize == NULL) 43448868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 43549679Sphk psize = devsw(dev)->d_psize(dev); 43648868Sphk if (psize == -1) 43748868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 43848868Sphk /* 43950571Sphk * XXX should clean up checking in dumpsys() to be more like this. 44048868Sphk */ 44181688Sbde newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE); 44281688Sbde if (newdumplo <= LABELSECTOR) 44348868Sphk return (ENOSPC); 44448868Sphk dumpdev = dev; 44548868Sphk dumplo = newdumplo; 44648868Sphk return (0); 44748868Sphk} 44848868Sphk 44948868Sphk 45031403Sjulian/* ARGSUSED */ 45131403Sjulianstatic void 45265395Speterdump_conf(void *dummy) 45331403Sjulian{ 45448868Sphk if (setdumpdev(dumpdev) != 0) 45548868Sphk dumpdev = NODEV; 45631403Sjulian} 45748868Sphk 45831403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 45931403Sjulian 46048868Sphkstatic int 46162573Sphksysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 46248868Sphk{ 46348868Sphk int error; 46448868Sphk udev_t ndumpdev; 46548868Sphk 46653838Sphk ndumpdev = dev2udev(dumpdev); 46748868Sphk error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 46848868Sphk if (error == 0 && req->newptr != NULL) 46953838Sphk error = setdumpdev(udev2dev(ndumpdev, 0)); 47048868Sphk return (error); 47148868Sphk} 47248868Sphk 47348868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 47448868Sphk 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", ""); 47548868Sphk 47617658Sjulian/* 47717658Sjulian * Doadump comes here after turning off memory management and 47817658Sjulian * getting on the dump stack, either when called above, or by 47917658Sjulian * the auto-restart code. 48017658Sjulian */ 48117658Sjulianstatic void 48217658Sjuliandumpsys(void) 48317658Sjulian{ 48449627Salfred int error; 48517658Sjulian 48665394Speter savectx(&dumppcb); 48750571Sphk if (dumping++) { 48850571Sphk printf("Dump already in progress, bailing...\n"); 48950571Sphk return; 49050571Sphk } 49117658Sjulian if (!dodump) 49217658Sjulian return; 49317658Sjulian if (dumpdev == NODEV) 49417658Sjulian return; 49549679Sphk if (!(devsw(dumpdev))) 49617658Sjulian return; 49749679Sphk if (!(devsw(dumpdev)->d_dump)) 49817658Sjulian return; 49917658Sjulian dumpsize = Maxmem; 50050571Sphk printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 50117658Sjulian printf("dump "); 50249679Sphk error = (*devsw(dumpdev)->d_dump)(dumpdev); 50349627Salfred if (error == 0) { 50449627Salfred printf("succeeded\n"); 50549627Salfred return; 50649627Salfred } 50749627Salfred printf("failed, reason: "); 50849627Salfred switch (error) { 50949627Salfred case ENODEV: 51049627Salfred printf("device doesn't support a dump routine\n"); 51149627Salfred break; 51217658Sjulian 51317658Sjulian case ENXIO: 51417658Sjulian printf("device bad\n"); 51517658Sjulian break; 51617658Sjulian 51717658Sjulian case EFAULT: 51817658Sjulian printf("device not ready\n"); 51917658Sjulian break; 52017658Sjulian 52117658Sjulian case EINVAL: 52217658Sjulian printf("area improper\n"); 52317658Sjulian break; 52417658Sjulian 52517658Sjulian case EIO: 52617658Sjulian printf("i/o error\n"); 52717658Sjulian break; 52817658Sjulian 52917658Sjulian case EINTR: 53017658Sjulian printf("aborted from console\n"); 53117658Sjulian break; 53217658Sjulian 53317658Sjulian default: 53449627Salfred printf("unknown, error = %d\n", error); 53517658Sjulian break; 53617658Sjulian } 53717658Sjulian} 53817658Sjulian 53974890Spsint 54074890Spsdumpstatus(vm_offset_t addr, long count) 54174890Sps{ 54274890Sps int c; 54374890Sps 54474890Sps if (addr % (1024 * 1024) == 0) { 54574890Sps#ifdef HW_WDOG 54674890Sps if (wdog_tickler) 54774890Sps (*wdog_tickler)(); 54874890Sps#endif 54974890Sps printf("%ld ", count / (1024 * 1024)); 55074890Sps } 55174890Sps 55274890Sps if ((c = cncheckc()) == 0x03) 55374890Sps return -1; 55474890Sps else if (c != -1) 55574890Sps printf("[CTRL-C to abort] "); 55674890Sps 55774890Sps return 0; 55874890Sps} 55974890Sps 56075570Sjhb#ifdef SMP 56175570Sjhbstatic u_int panic_cpu = NOCPU; 56275570Sjhb#endif 56375570Sjhb 56417658Sjulian/* 56517658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 56617658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 56717658Sjulian * the disks as this often leads to recursive panics. 56817658Sjulian */ 56917658Sjulianvoid 57017658Sjulianpanic(const char *fmt, ...) 57117658Sjulian{ 57217658Sjulian int bootopt; 57317658Sjulian va_list ap; 57438874Sache static char buf[256]; 57517658Sjulian 57665557Sjasone#ifdef SMP 57782115Sjhb /* 57882115Sjhb * We don't want multiple CPU's to panic at the same time, so we 57982115Sjhb * use panic_cpu as a simple spinlock. We have to keep checking 58082115Sjhb * panic_cpu if we are spinning in case the panic on the first 58182115Sjhb * CPU is canceled. 58282115Sjhb */ 58382115Sjhb if (panic_cpu != PCPU_GET(cpuid)) 58482115Sjhb while (atomic_cmpset_int(&panic_cpu, NOCPU, 58582115Sjhb PCPU_GET(cpuid)) == 0) 58682115Sjhb while (panic_cpu != NOCPU) 58782115Sjhb ; /* nothing */ 58865557Sjasone#endif 58965557Sjasone 59017658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 59117658Sjulian if (panicstr) 59217658Sjulian bootopt |= RB_NOSYNC; 59317658Sjulian else 59417658Sjulian panicstr = fmt; 59517658Sjulian 59617658Sjulian va_start(ap, fmt); 59741514Sarchie (void)vsnprintf(buf, sizeof(buf), fmt, ap); 59838874Sache if (panicstr == fmt) 59938874Sache panicstr = buf; 60017658Sjulian va_end(ap); 60138874Sache printf("panic: %s\n", buf); 60226100Sfsmp#ifdef SMP 60372091Sasmodai /* two separate prints in case of an unmapped page and trap */ 60470861Sjake printf("cpuid = %d; ", PCPU_GET(cpuid)); 60569335Sjhb#ifdef APIC_IO 60629128Speter printf("lapic.id = %08x\n", lapic.id); 60726100Sfsmp#endif 60869335Sjhb#endif 60917658Sjulian 61017658Sjulian#if defined(DDB) 61117658Sjulian if (debugger_on_panic) 61217658Sjulian Debugger ("panic"); 61382223Sjhb#ifdef RESTARTABLE_PANICS 61482115Sjhb /* See if the user aborted the panic, in which case we continue. */ 61582115Sjhb if (panicstr == NULL) { 61682115Sjhb#ifdef SMP 61782115Sjhb atomic_store_rel_int(&panic_cpu, NOCPU); 61817658Sjulian#endif 61982115Sjhb return; 62082115Sjhb } 62182115Sjhb#endif 62282223Sjhb#endif 62317658Sjulian boot(bootopt); 62417658Sjulian} 62517658Sjulian 62617768Sjulian/* 62743436Smsmith * Support for poweroff delay. 62843436Smsmith */ 62954248Smsmith#ifndef POWEROFF_DELAY 63054248Smsmith# define POWEROFF_DELAY 5000 63154248Smsmith#endif 63254248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 63354248Smsmith 63443436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 63543436Smsmith &poweroff_delay, 0, ""); 63643436Smsmith 63750107Smsmithstatic void 63850107Smsmithpoweroff_wait(void *junk, int howto) 63943436Smsmith{ 64043436Smsmith if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 64143436Smsmith return; 64243436Smsmith DELAY(poweroff_delay * 1000); 64343436Smsmith} 64455539Sluoqi 64555539Sluoqi/* 64655539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 64755539Sluoqi * points in their main loops prior to a system shutdown, so that they 64855539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 64955539Sluoqi * to cause sync to fail). For each of these system processes, register 65055539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 65155539Sluoqi */ 65255539Sluoqistatic int kproc_shutdown_wait = 60; 65355539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 65455539Sluoqi &kproc_shutdown_wait, 0, ""); 65555539Sluoqi 65655539Sluoqivoid 65770063Sjhbkproc_shutdown(void *arg, int howto) 65855539Sluoqi{ 65955539Sluoqi struct proc *p; 66055539Sluoqi int error; 66155539Sluoqi 66255539Sluoqi if (panicstr) 66355539Sluoqi return; 66455539Sluoqi 66555539Sluoqi p = (struct proc *)arg; 66655539Sluoqi printf("Waiting (max %d seconds) for system process `%s' to stop...", 66755862Sluoqi kproc_shutdown_wait, p->p_comm); 66870063Sjhb error = kthread_suspend(p, kproc_shutdown_wait * hz); 66955539Sluoqi 67055539Sluoqi if (error == EWOULDBLOCK) 67155539Sluoqi printf("timed out\n"); 67255539Sluoqi else 67355539Sluoqi printf("stopped\n"); 67455539Sluoqi} 675