kern_shutdown.c revision 132506
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 * 4. Neither the name of the University nor the names of its contributors 1917658Sjulian * may be used to endorse or promote products derived from this software 2017658Sjulian * without specific prior written permission. 2117658Sjulian * 2217658Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2317658Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2417658Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2517658Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2617658Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2717658Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2817658Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2917658Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3017658Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3117658Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3217658Sjulian * SUCH DAMAGE. 3317658Sjulian * 3417658Sjulian * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 3517658Sjulian */ 3617658Sjulian 37116182Sobrien#include <sys/cdefs.h> 38116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_shutdown.c 132506 2004-07-21 16:04:46Z rwatson $"); 39116182Sobrien 40131927Smarcel#include "opt_kdb.h" 4133445Seivind#include "opt_hw_wdog.h" 42106024Srwatson#include "opt_mac.h" 4328976Sbde#include "opt_panic.h" 4428976Sbde#include "opt_show_busybufs.h" 4517658Sjulian 4617658Sjulian#include <sys/param.h> 4717658Sjulian#include <sys/systm.h> 4860041Sphk#include <sys/bio.h> 4931275Sbde#include <sys/buf.h> 5078767Sjhb#include <sys/conf.h> 5178767Sjhb#include <sys/cons.h> 5278767Sjhb#include <sys/eventhandler.h> 53131927Smarcel#include <sys/kdb.h> 5417658Sjulian#include <sys/kernel.h> 5555539Sluoqi#include <sys/kthread.h> 56106024Srwatson#include <sys/mac.h> 5789601Ssobomax#include <sys/malloc.h> 5821776Sbde#include <sys/mount.h> 5978767Sjhb#include <sys/proc.h> 6078767Sjhb#include <sys/reboot.h> 6178767Sjhb#include <sys/resourcevar.h> 6278767Sjhb#include <sys/smp.h> /* smp_active */ 6317658Sjulian#include <sys/sysctl.h> 6417658Sjulian#include <sys/sysproto.h> 6578767Sjhb#include <sys/vnode.h> 6617658Sjulian 67118990Smarcel#include <machine/cpu.h> 6894169Sphk#include <machine/pcb.h> 6991778Sjake#include <machine/smp.h> 7017658Sjulian 7117658Sjulian#include <sys/signalvar.h> 7217658Sjulian 7317658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME 7417658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 7517658Sjulian#endif 7617658Sjulian 7717658Sjulian/* 7817658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both 7917658Sjulian * ANSI and traditional C compilers. 8017658Sjulian */ 8117658Sjulian#include <machine/stdarg.h> 8217658Sjulian 83131927Smarcel#ifdef KDB 84131927Smarcel#ifdef KDB_UNATTENDED 8542135Smsmithint debugger_on_panic = 0; 8617658Sjulian#else 8742135Smsmithint debugger_on_panic = 1; 8817658Sjulian#endif 8917658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 9046381Sbillf &debugger_on_panic, 0, "Run debugger on kernel panic"); 91103647Sjhb 92131927Smarcel#ifdef KDB_TRACE 93103647Sjhbint trace_on_panic = 1; 94103647Sjhb#else 95103647Sjhbint trace_on_panic = 0; 9617658Sjulian#endif 97103647SjhbSYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW, 98103647Sjhb &trace_on_panic, 0, "Print stack trace on kernel panic"); 99131927Smarcel#endif /* KDB */ 10017658Sjulian 101132506Srwatsonint sync_on_panic = 0; 10285202SpeterSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW, 10385202Speter &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 10485202Speter 10543436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 10643436Smsmith 10728000Sjulian#ifdef HW_WDOG 10817658Sjulian/* 10927997Sjulian * If there is a hardware watchdog, point this at the function needed to 11027997Sjulian * hold it off. 11127997Sjulian * It's needed when the kernel needs to do some lengthy operations. 11227997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have 11327997Sjulian * your precious core-dump only half written because the wdog kicked in. 11427997Sjulian */ 11527997Sjulianwatchdog_tickle_fn wdog_tickler = NULL; 11628000Sjulian#endif /* HW_WDOG */ 11727997Sjulian 11827997Sjulian/* 11917658Sjulian * Variable panicstr contains argument to first call to panic; used as flag 12017658Sjulian * to indicate that the kernel has already called panic. 12117658Sjulian */ 12217658Sjulianconst char *panicstr; 12317658Sjulian 12493496Sphkint dumping; /* system is dumping */ 12593496Sphkstatic struct dumperinfo dumper; /* our selected dumper */ 12667093Sps 127131927Smarcel/* Context information for dump-debuggers. */ 128131927Smarcelstatic struct pcb dumppcb; /* Registers. */ 129131927Smarcelstatic lwpid_t dumptid; /* Thread ID. */ 130131927Smarcel 13165395Speterstatic void boot(int) __dead2; 13265395Speterstatic void poweroff_wait(void *, int); 13365395Speterstatic void shutdown_halt(void *junk, int howto); 13465395Speterstatic void shutdown_panic(void *junk, int howto); 13565395Speterstatic void shutdown_reset(void *junk, int howto); 13617658Sjulian 13750107Smsmith/* register various local shutdown events */ 138110859Salfredstatic void 13950107Smsmithshutdown_conf(void *unused) 14050107Smsmith{ 141110859Salfred 142110859Salfred EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 143110859Salfred SHUTDOWN_PRI_FIRST); 144110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 145110859Salfred SHUTDOWN_PRI_LAST + 100); 146110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 147110859Salfred SHUTDOWN_PRI_LAST + 100); 148110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 149110859Salfred SHUTDOWN_PRI_LAST + 200); 15050107Smsmith} 15148868Sphk 15250107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 15350107Smsmith 15417658Sjulian/* 15517658Sjulian * The system call that results in a reboot 15682749Sdillon * 15782749Sdillon * MPSAFE 15817658Sjulian */ 15982749Sdillon/* ARGSUSED */ 16017658Sjulianint 16183366Sjulianreboot(struct thread *td, struct reboot_args *uap) 16217658Sjulian{ 16317658Sjulian int error; 16417658Sjulian 165106024Srwatson error = 0; 166106024Srwatson#ifdef MAC 167106024Srwatson error = mac_check_system_reboot(td->td_ucred, uap->opt); 168106024Srwatson#endif 169106024Srwatson if (error == 0) 170106024Srwatson error = suser(td); 171106024Srwatson if (error == 0) { 172106024Srwatson mtx_lock(&Giant); 17382749Sdillon boot(uap->opt); 174106024Srwatson mtx_unlock(&Giant); 175106024Srwatson } 17682749Sdillon return (error); 17717658Sjulian} 17817658Sjulian 17917658Sjulian/* 18017658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 18117658Sjulian */ 18265268Smsmithstatic int shutdown_howto = 0; 18365268Smsmith 18417658Sjulianvoid 18565268Smsmithshutdown_nice(int howto) 18617658Sjulian{ 187110859Salfred 18865268Smsmith shutdown_howto = howto; 189110859Salfred 19017658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 19117658Sjulian if (initproc != NULL) { 19273913Sjhb PROC_LOCK(initproc); 19317658Sjulian psignal(initproc, SIGINT); 19473913Sjhb PROC_UNLOCK(initproc); 19517658Sjulian } else { 19617658Sjulian /* No init(8) running, so simply reboot */ 19717658Sjulian boot(RB_NOSYNC); 19817658Sjulian } 19917658Sjulian return; 20017658Sjulian} 20117658Sjulianstatic int waittime = -1; 20217658Sjulian 20354233Sphkstatic void 20465395Speterprint_uptime(void) 20554233Sphk{ 20654233Sphk int f; 20754233Sphk struct timespec ts; 20854233Sphk 20954233Sphk getnanouptime(&ts); 21054233Sphk printf("Uptime: "); 21154233Sphk f = 0; 21254233Sphk if (ts.tv_sec >= 86400) { 21365764Sjhb printf("%ldd", (long)ts.tv_sec / 86400); 21454233Sphk ts.tv_sec %= 86400; 21554233Sphk f = 1; 21654233Sphk } 21754233Sphk if (f || ts.tv_sec >= 3600) { 21865764Sjhb printf("%ldh", (long)ts.tv_sec / 3600); 21954233Sphk ts.tv_sec %= 3600; 22054233Sphk f = 1; 22154233Sphk } 22254233Sphk if (f || ts.tv_sec >= 60) { 22365764Sjhb printf("%ldm", (long)ts.tv_sec / 60); 22454233Sphk ts.tv_sec %= 60; 22554233Sphk f = 1; 22654233Sphk } 22765764Sjhb printf("%lds\n", (long)ts.tv_sec); 22854233Sphk} 22954233Sphk 23094169Sphkstatic void 23194169Sphkdoadump(void) 23294169Sphk{ 233110859Salfred 234132412Sjulian /* 235132412Sjulian * Sometimes people have to call this from the kernel debugger. 236132412Sjulian * (if 'panic' can not dump) 237132412Sjulian * Give them a clue as to why they can't dump. 238132412Sjulian */ 239132412Sjulian if (dumper.dumper == NULL) { 240132413Sjulian printf("Cannot dump. No dump device defined.\n"); 241132412Sjulian return; 242132412Sjulian } 243132412Sjulian 24494169Sphk savectx(&dumppcb); 245131927Smarcel dumptid = curthread->td_tid; 24694169Sphk dumping++; 24794169Sphk dumpsys(&dumper); 24894169Sphk} 24994169Sphk 25017658Sjulian/* 25117658Sjulian * Go through the rigmarole of shutting down.. 25217658Sjulian * this used to be in machdep.c but I'll be dammned if I could see 25317658Sjulian * anything machine dependant in it. 25417658Sjulian */ 25531275Sbdestatic void 25665395Speterboot(int howto) 25717658Sjulian{ 258132197Salfred static int first_buf_printf = 1; 25917658Sjulian 26065268Smsmith /* collect extra flags that shutdown_nice might have set */ 26165268Smsmith howto |= shutdown_howto; 26265268Smsmith 26382119Sjhb /* We are out of the debugger now. */ 264131927Smarcel kdb_active = 0; 26582119Sjhb 26625164Speter#ifdef SMP 26765395Speter if (smp_active) 26870861Sjake printf("boot() called on cpu#%d\n", PCPU_GET(cpuid)); 26925164Speter#endif 27027997Sjulian /* 27127997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 27227997Sjulian */ 27350107Smsmith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 27427997Sjulian 27527997Sjulian /* 27627997Sjulian * Now sync filesystems 27727997Sjulian */ 27817658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 27917658Sjulian register struct buf *bp; 28065707Sjasone int iter, nbusy, pbusy; 281131481Sjhb#ifndef PREEMPTION 28265707Sjasone int subiter; 283131481Sjhb#endif 28417658Sjulian 28517658Sjulian waittime = 0; 28617658Sjulian 28790361Sjulian sync(&thread0, NULL); 28817658Sjulian 28934266Sjulian /* 29034266Sjulian * With soft updates, some buffers that are 29134266Sjulian * written will be remarked as dirty until other 29234266Sjulian * buffers are written. 29334266Sjulian */ 29465707Sjasone for (iter = pbusy = 0; iter < 20; iter++) { 29517658Sjulian nbusy = 0; 29617658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 29748225Smckusick if ((bp->b_flags & B_INVAL) == 0 && 29848225Smckusick BUF_REFCNT(bp) > 0) { 29917658Sjulian nbusy++; 30034266Sjulian } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 30134266Sjulian == B_DELWRI) { 30234266Sjulian /* bawrite(bp);*/ 30334266Sjulian nbusy++; 30417658Sjulian } 30517658Sjulian } 30617658Sjulian if (nbusy == 0) 30717658Sjulian break; 308132177Salfred if (first_buf_printf) { 309132177Salfred printf("syncing disks, buffers remaining... "); 310132177Salfred first_buf_printf = 0; 311132177Salfred } 31217658Sjulian printf("%d ", nbusy); 31365707Sjasone if (nbusy < pbusy) 31465707Sjasone iter = 0; 31565707Sjasone pbusy = nbusy; 31690361Sjulian sync(&thread0, NULL); 317131481Sjhb 318131481Sjhb#ifdef PREEMPTION 319131481Sjhb /* 320131481Sjhb * Drop Giant and spin for a while to allow 321131481Sjhb * interrupt threads to run. 322131481Sjhb */ 323131481Sjhb DROP_GIANT(); 32434266Sjulian DELAY(50000 * iter); 325131481Sjhb PICKUP_GIANT(); 326131481Sjhb#else 327131481Sjhb /* 328131481Sjhb * Drop Giant and context switch several times to 329131481Sjhb * allow interrupt threads to run. 330131481Sjhb */ 331131481Sjhb DROP_GIANT(); 332131481Sjhb for (subiter = 0; subiter < 50 * iter; subiter++) { 333131481Sjhb mtx_lock_spin(&sched_lock); 334131481Sjhb mi_switch(SW_VOL, NULL); 335131481Sjhb mtx_unlock_spin(&sched_lock); 336131481Sjhb DELAY(1000); 337131481Sjhb } 338131481Sjhb PICKUP_GIANT(); 339131481Sjhb#endif 34017658Sjulian } 341132171Snjl 34241137Smsmith /* 34341137Smsmith * Count only busy local buffers to prevent forcing 34441137Smsmith * a fsck if we're just a client of a wedged NFS server 34541137Smsmith */ 34641137Smsmith nbusy = 0; 34741137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 34848225Smckusick if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 34948225Smckusick ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 350130640Sphk if (bp->b_dev == NULL) { 35153452Sphk TAILQ_REMOVE(&mountlist, 35248225Smckusick bp->b_vp->v_mount, mnt_list); 35353023Sphk continue; 35453023Sphk } 35553023Sphk nbusy++; 35653023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 35753023Sphk printf( 358110585Sjeff "%d: dev:%s, flags:%0x, blkno:%ld, lblkno:%ld\n", 35953023Sphk nbusy, devtoname(bp->b_dev), 36053023Sphk bp->b_flags, (long)bp->b_blkno, 36153023Sphk (long)bp->b_lblkno); 36253023Sphk#endif 36346568Speter } 36441137Smsmith } 36517658Sjulian if (nbusy) { 36617658Sjulian /* 36717658Sjulian * Failed to sync all blocks. Indicate this and don't 36817658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 36917658Sjulian */ 37053023Sphk printf("giving up on %d buffers\n", nbusy); 37117658Sjulian DELAY(5000000); /* 5 seconds */ 37217658Sjulian } else { 37317658Sjulian printf("done\n"); 37417658Sjulian /* 37517658Sjulian * Unmount filesystems 37617658Sjulian */ 37717658Sjulian if (panicstr == 0) 37817658Sjulian vfs_unmountall(); 37917658Sjulian } 38039237Sgibbs DELAY(100000); /* wait for console output to finish */ 38117658Sjulian } 38227997Sjulian 38354233Sphk print_uptime(); 38454233Sphk 38527997Sjulian /* 38627997Sjulian * Ok, now do things that assume all filesystem activity has 38727997Sjulian * been completed. 38827997Sjulian */ 38950107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 39039237Sgibbs splhigh(); 391132412Sjulian if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 39294169Sphk doadump(); 39339237Sgibbs 39439237Sgibbs /* Now that we're going to really halt the system... */ 39550107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 39639237Sgibbs 39750107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 39850107Smsmith /* NOTREACHED */ 39950107Smsmith} 40050107Smsmith 40150107Smsmith/* 40250107Smsmith * If the shutdown was a clean halt, behave accordingly. 40350107Smsmith */ 40450107Smsmithstatic void 40550107Smsmithshutdown_halt(void *junk, int howto) 40650107Smsmith{ 407110859Salfred 40817658Sjulian if (howto & RB_HALT) { 40917658Sjulian printf("\n"); 41017658Sjulian printf("The operating system has halted.\n"); 41117658Sjulian printf("Please press any key to reboot.\n\n"); 41219274Sjulian switch (cngetc()) { 41319274Sjulian case -1: /* No console, just die */ 41419274Sjulian cpu_halt(); 41519274Sjulian /* NOTREACHED */ 41619274Sjulian default: 41739237Sgibbs howto &= ~RB_HALT; 41819274Sjulian break; 41919274Sjulian } 42050107Smsmith } 42150107Smsmith} 42217658Sjulian 42350107Smsmith/* 42450107Smsmith * Check to see if the system paniced, pause and then reboot 42550107Smsmith * according to the specified delay. 42650107Smsmith */ 42750107Smsmithstatic void 42850107Smsmithshutdown_panic(void *junk, int howto) 42950107Smsmith{ 43050107Smsmith int loop; 43150107Smsmith 43250107Smsmith if (howto & RB_DUMP) { 43339237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 43439237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 43539237Sgibbs printf("Automatic reboot in %d seconds - " 43639237Sgibbs "press a key on the console to abort\n", 43739237Sgibbs PANIC_REBOOT_WAIT_TIME); 43839237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 43939237Sgibbs loop > 0; --loop) { 44039237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 44139237Sgibbs /* Did user type a key? */ 44239237Sgibbs if (cncheckc() != -1) 44339237Sgibbs break; 44417658Sjulian } 44539237Sgibbs if (!loop) 44650107Smsmith return; 44717658Sjulian } 44839237Sgibbs } else { /* zero time specified - reboot NOW */ 44950107Smsmith return; 45017658Sjulian } 45189522Snik printf("--> Press a key on the console to reboot,\n"); 45289522Snik printf("--> or switch off the system now.\n"); 45339237Sgibbs cngetc(); 45417658Sjulian } 45550107Smsmith} 45650107Smsmith 45750107Smsmith/* 45850107Smsmith * Everything done, now reset 45950107Smsmith */ 46050107Smsmithstatic void 46150107Smsmithshutdown_reset(void *junk, int howto) 46250107Smsmith{ 463110859Salfred 46417658Sjulian printf("Rebooting...\n"); 46517658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 46617677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 46717658Sjulian cpu_reset(); 46850107Smsmith /* NOTREACHED */ /* assuming reset worked */ 46917658Sjulian} 47017658Sjulian 47175570Sjhb#ifdef SMP 472101155Sjhbstatic u_int panic_cpu = NOCPU; 47375570Sjhb#endif 47475570Sjhb 47517658Sjulian/* 47617658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 47717658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 47817658Sjulian * the disks as this often leads to recursive panics. 47982749Sdillon * 48082749Sdillon * MPSAFE 48117658Sjulian */ 48217658Sjulianvoid 483130164Sphkpanic(const char *fmt, ...) 48417658Sjulian{ 485100209Sgallatin struct thread *td = curthread; 486103647Sjhb int bootopt, newpanic; 48717658Sjulian va_list ap; 48838874Sache static char buf[256]; 48917658Sjulian 49065557Sjasone#ifdef SMP 49182115Sjhb /* 49282115Sjhb * We don't want multiple CPU's to panic at the same time, so we 493101155Sjhb * use panic_cpu as a simple spinlock. We have to keep checking 494101155Sjhb * panic_cpu if we are spinning in case the panic on the first 49582115Sjhb * CPU is canceled. 49682115Sjhb */ 497101155Sjhb if (panic_cpu != PCPU_GET(cpuid)) 498101155Sjhb while (atomic_cmpset_int(&panic_cpu, NOCPU, 499101155Sjhb PCPU_GET(cpuid)) == 0) 500101155Sjhb while (panic_cpu != NOCPU) 501101155Sjhb ; /* nothing */ 50265557Sjasone#endif 50365557Sjasone 50417658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 505103647Sjhb newpanic = 0; 50617658Sjulian if (panicstr) 50717658Sjulian bootopt |= RB_NOSYNC; 508103647Sjhb else { 50917658Sjulian panicstr = fmt; 510103647Sjhb newpanic = 1; 511103647Sjhb } 51217658Sjulian 51317658Sjulian va_start(ap, fmt); 514116398Siedowse if (newpanic) { 515116398Siedowse (void)vsnprintf(buf, sizeof(buf), fmt, ap); 51638874Sache panicstr = buf; 517130164Sphk printf("panic: %s\n", buf); 518116398Siedowse } else { 519116398Siedowse printf("panic: "); 520116398Siedowse vprintf(fmt, ap); 521130164Sphk printf("\n"); 522116398Siedowse } 52317658Sjulian va_end(ap); 52426100Sfsmp#ifdef SMP 52572091Sasmodai /* two separate prints in case of an unmapped page and trap */ 52670861Sjake printf("cpuid = %d; ", PCPU_GET(cpuid)); 52769335Sjhb#ifdef APIC_IO 52829128Speter printf("lapic.id = %08x\n", lapic.id); 52999828Sjhb#else 53099828Sjhb printf("\n"); 53126100Sfsmp#endif 53269335Sjhb#endif 53317658Sjulian 534131927Smarcel#ifdef KDB 535103647Sjhb if (newpanic && trace_on_panic) 536131927Smarcel kdb_backtrace(); 53717658Sjulian if (debugger_on_panic) 538131927Smarcel kdb_enter("panic"); 53982223Sjhb#ifdef RESTARTABLE_PANICS 54082115Sjhb /* See if the user aborted the panic, in which case we continue. */ 54182115Sjhb if (panicstr == NULL) { 54282115Sjhb#ifdef SMP 543101155Sjhb atomic_store_rel_int(&panic_cpu, NOCPU); 54417658Sjulian#endif 54582115Sjhb return; 54682115Sjhb } 54782115Sjhb#endif 54882223Sjhb#endif 549113633Sjhb mtx_lock_spin(&sched_lock); 550100209Sgallatin td->td_flags |= TDF_INPANIC; 551113633Sjhb mtx_unlock_spin(&sched_lock); 55285202Speter if (!sync_on_panic) 55385202Speter bootopt |= RB_NOSYNC; 55417658Sjulian boot(bootopt); 55517658Sjulian} 55617658Sjulian 55717768Sjulian/* 55843436Smsmith * Support for poweroff delay. 55943436Smsmith */ 56054248Smsmith#ifndef POWEROFF_DELAY 56154248Smsmith# define POWEROFF_DELAY 5000 56254248Smsmith#endif 56354248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 56454248Smsmith 56543436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 56643436Smsmith &poweroff_delay, 0, ""); 56743436Smsmith 568110859Salfredstatic void 56950107Smsmithpoweroff_wait(void *junk, int howto) 57043436Smsmith{ 571110859Salfred 572110859Salfred if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 57343436Smsmith return; 57443436Smsmith DELAY(poweroff_delay * 1000); 57543436Smsmith} 57655539Sluoqi 57755539Sluoqi/* 57855539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 57955539Sluoqi * points in their main loops prior to a system shutdown, so that they 58055539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 58155539Sluoqi * to cause sync to fail). For each of these system processes, register 58255539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 58355539Sluoqi */ 58455539Sluoqistatic int kproc_shutdown_wait = 60; 58555539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 58655539Sluoqi &kproc_shutdown_wait, 0, ""); 58755539Sluoqi 58855539Sluoqivoid 58970063Sjhbkproc_shutdown(void *arg, int howto) 59055539Sluoqi{ 59155539Sluoqi struct proc *p; 592132177Salfred char procname[MAXCOMLEN + 1]; 59355539Sluoqi int error; 59455539Sluoqi 59555539Sluoqi if (panicstr) 59655539Sluoqi return; 59755539Sluoqi 59855539Sluoqi p = (struct proc *)arg; 599132177Salfred strlcpy(procname, p->p_comm, sizeof(procname)); 600132177Salfred printf("Waiting (max %d seconds) for system process `%s' to stop...\n", 601132177Salfred kproc_shutdown_wait, procname); 60270063Sjhb error = kthread_suspend(p, kproc_shutdown_wait * hz); 60355539Sluoqi 60455539Sluoqi if (error == EWOULDBLOCK) 605132197Salfred printf("Stop of '%s' timed out.\n", procname); 60655539Sluoqi else 607132197Salfred printf("Process '%s' stopped.\n", procname); 60855539Sluoqi} 60993496Sphk 61093496Sphk/* Registration of dumpers */ 61193496Sphkint 61293496Sphkset_dumper(struct dumperinfo *di) 61393496Sphk{ 614110859Salfred 61593496Sphk if (di == NULL) { 61693496Sphk bzero(&dumper, sizeof dumper); 61793496Sphk return (0); 61893496Sphk } 61993496Sphk if (dumper.dumper != NULL) 62093496Sphk return (EBUSY); 62193496Sphk dumper = *di; 62293496Sphk return (0); 62393496Sphk} 62493496Sphk 625105531Stmm#if defined(__powerpc__) 62693496Sphkvoid 62793496Sphkdumpsys(struct dumperinfo *di __unused) 62893496Sphk{ 62993496Sphk 63093496Sphk printf("Kernel dumps not implemented on this architecture\n"); 63193496Sphk} 63293496Sphk#endif 633