kern_shutdown.c revision 132866
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 132866 2004-07-30 01:30:05Z njl $"); 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 /* 37417658Sjulian * Unmount filesystems 37517658Sjulian */ 37617658Sjulian if (panicstr == 0) 37717658Sjulian vfs_unmountall(); 37817658Sjulian } 37939237Sgibbs DELAY(100000); /* wait for console output to finish */ 38017658Sjulian } 38127997Sjulian 38254233Sphk print_uptime(); 38354233Sphk 38427997Sjulian /* 38527997Sjulian * Ok, now do things that assume all filesystem activity has 38627997Sjulian * been completed. 38727997Sjulian */ 38850107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 38939237Sgibbs splhigh(); 390132412Sjulian if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 39194169Sphk doadump(); 39239237Sgibbs 39339237Sgibbs /* Now that we're going to really halt the system... */ 39450107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 39539237Sgibbs 39650107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 39750107Smsmith /* NOTREACHED */ 39850107Smsmith} 39950107Smsmith 40050107Smsmith/* 40150107Smsmith * If the shutdown was a clean halt, behave accordingly. 40250107Smsmith */ 40350107Smsmithstatic void 40450107Smsmithshutdown_halt(void *junk, int howto) 40550107Smsmith{ 406110859Salfred 40717658Sjulian if (howto & RB_HALT) { 40817658Sjulian printf("\n"); 40917658Sjulian printf("The operating system has halted.\n"); 41017658Sjulian printf("Please press any key to reboot.\n\n"); 41119274Sjulian switch (cngetc()) { 41219274Sjulian case -1: /* No console, just die */ 41319274Sjulian cpu_halt(); 41419274Sjulian /* NOTREACHED */ 41519274Sjulian default: 41639237Sgibbs howto &= ~RB_HALT; 41719274Sjulian break; 41819274Sjulian } 41950107Smsmith } 42050107Smsmith} 42117658Sjulian 42250107Smsmith/* 42350107Smsmith * Check to see if the system paniced, pause and then reboot 42450107Smsmith * according to the specified delay. 42550107Smsmith */ 42650107Smsmithstatic void 42750107Smsmithshutdown_panic(void *junk, int howto) 42850107Smsmith{ 42950107Smsmith int loop; 43050107Smsmith 43150107Smsmith if (howto & RB_DUMP) { 43239237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 43339237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 43439237Sgibbs printf("Automatic reboot in %d seconds - " 43539237Sgibbs "press a key on the console to abort\n", 43639237Sgibbs PANIC_REBOOT_WAIT_TIME); 43739237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 43839237Sgibbs loop > 0; --loop) { 43939237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 44039237Sgibbs /* Did user type a key? */ 44139237Sgibbs if (cncheckc() != -1) 44239237Sgibbs break; 44317658Sjulian } 44439237Sgibbs if (!loop) 44550107Smsmith return; 44617658Sjulian } 44739237Sgibbs } else { /* zero time specified - reboot NOW */ 44850107Smsmith return; 44917658Sjulian } 45089522Snik printf("--> Press a key on the console to reboot,\n"); 45189522Snik printf("--> or switch off the system now.\n"); 45239237Sgibbs cngetc(); 45317658Sjulian } 45450107Smsmith} 45550107Smsmith 45650107Smsmith/* 45750107Smsmith * Everything done, now reset 45850107Smsmith */ 45950107Smsmithstatic void 46050107Smsmithshutdown_reset(void *junk, int howto) 46150107Smsmith{ 462110859Salfred 46317658Sjulian printf("Rebooting...\n"); 46417658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 46517677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 46617658Sjulian cpu_reset(); 46750107Smsmith /* NOTREACHED */ /* assuming reset worked */ 46817658Sjulian} 46917658Sjulian 47075570Sjhb#ifdef SMP 471101155Sjhbstatic u_int panic_cpu = NOCPU; 47275570Sjhb#endif 47375570Sjhb 47417658Sjulian/* 47517658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 47617658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 47717658Sjulian * the disks as this often leads to recursive panics. 47882749Sdillon * 47982749Sdillon * MPSAFE 48017658Sjulian */ 48117658Sjulianvoid 482130164Sphkpanic(const char *fmt, ...) 48317658Sjulian{ 484100209Sgallatin struct thread *td = curthread; 485103647Sjhb int bootopt, newpanic; 48617658Sjulian va_list ap; 48738874Sache static char buf[256]; 48817658Sjulian 48965557Sjasone#ifdef SMP 49082115Sjhb /* 49182115Sjhb * We don't want multiple CPU's to panic at the same time, so we 492101155Sjhb * use panic_cpu as a simple spinlock. We have to keep checking 493101155Sjhb * panic_cpu if we are spinning in case the panic on the first 49482115Sjhb * CPU is canceled. 49582115Sjhb */ 496101155Sjhb if (panic_cpu != PCPU_GET(cpuid)) 497101155Sjhb while (atomic_cmpset_int(&panic_cpu, NOCPU, 498101155Sjhb PCPU_GET(cpuid)) == 0) 499101155Sjhb while (panic_cpu != NOCPU) 500101155Sjhb ; /* nothing */ 50165557Sjasone#endif 50265557Sjasone 50317658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 504103647Sjhb newpanic = 0; 50517658Sjulian if (panicstr) 50617658Sjulian bootopt |= RB_NOSYNC; 507103647Sjhb else { 50817658Sjulian panicstr = fmt; 509103647Sjhb newpanic = 1; 510103647Sjhb } 51117658Sjulian 51217658Sjulian va_start(ap, fmt); 513116398Siedowse if (newpanic) { 514116398Siedowse (void)vsnprintf(buf, sizeof(buf), fmt, ap); 51538874Sache panicstr = buf; 516130164Sphk printf("panic: %s\n", buf); 517116398Siedowse } else { 518116398Siedowse printf("panic: "); 519116398Siedowse vprintf(fmt, ap); 520130164Sphk printf("\n"); 521116398Siedowse } 52217658Sjulian va_end(ap); 52326100Sfsmp#ifdef SMP 52472091Sasmodai /* two separate prints in case of an unmapped page and trap */ 52570861Sjake printf("cpuid = %d; ", PCPU_GET(cpuid)); 52669335Sjhb#ifdef APIC_IO 52729128Speter printf("lapic.id = %08x\n", lapic.id); 52899828Sjhb#else 52999828Sjhb printf("\n"); 53026100Sfsmp#endif 53169335Sjhb#endif 53217658Sjulian 533131927Smarcel#ifdef KDB 534103647Sjhb if (newpanic && trace_on_panic) 535131927Smarcel kdb_backtrace(); 53617658Sjulian if (debugger_on_panic) 537131927Smarcel kdb_enter("panic"); 53882223Sjhb#ifdef RESTARTABLE_PANICS 53982115Sjhb /* See if the user aborted the panic, in which case we continue. */ 54082115Sjhb if (panicstr == NULL) { 54182115Sjhb#ifdef SMP 542101155Sjhb atomic_store_rel_int(&panic_cpu, NOCPU); 54317658Sjulian#endif 54482115Sjhb return; 54582115Sjhb } 54682115Sjhb#endif 54782223Sjhb#endif 548113633Sjhb mtx_lock_spin(&sched_lock); 549100209Sgallatin td->td_flags |= TDF_INPANIC; 550113633Sjhb mtx_unlock_spin(&sched_lock); 55185202Speter if (!sync_on_panic) 55285202Speter bootopt |= RB_NOSYNC; 55317658Sjulian boot(bootopt); 55417658Sjulian} 55517658Sjulian 55617768Sjulian/* 55743436Smsmith * Support for poweroff delay. 55843436Smsmith */ 55954248Smsmith#ifndef POWEROFF_DELAY 56054248Smsmith# define POWEROFF_DELAY 5000 56154248Smsmith#endif 56254248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 56354248Smsmith 56443436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 56543436Smsmith &poweroff_delay, 0, ""); 56643436Smsmith 567110859Salfredstatic void 56850107Smsmithpoweroff_wait(void *junk, int howto) 56943436Smsmith{ 570110859Salfred 571110859Salfred if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 57243436Smsmith return; 57343436Smsmith DELAY(poweroff_delay * 1000); 57443436Smsmith} 57555539Sluoqi 57655539Sluoqi/* 57755539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 57855539Sluoqi * points in their main loops prior to a system shutdown, so that they 57955539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 58055539Sluoqi * to cause sync to fail). For each of these system processes, register 58155539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 58255539Sluoqi */ 58355539Sluoqistatic int kproc_shutdown_wait = 60; 58455539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 58555539Sluoqi &kproc_shutdown_wait, 0, ""); 58655539Sluoqi 58755539Sluoqivoid 58870063Sjhbkproc_shutdown(void *arg, int howto) 58955539Sluoqi{ 59055539Sluoqi struct proc *p; 591132177Salfred char procname[MAXCOMLEN + 1]; 59255539Sluoqi int error; 59355539Sluoqi 59455539Sluoqi if (panicstr) 59555539Sluoqi return; 59655539Sluoqi 59755539Sluoqi p = (struct proc *)arg; 598132177Salfred strlcpy(procname, p->p_comm, sizeof(procname)); 599132866Snjl printf("Waiting (max %d seconds) for system process `%s' to stop...", 600132177Salfred kproc_shutdown_wait, procname); 60170063Sjhb error = kthread_suspend(p, kproc_shutdown_wait * hz); 60255539Sluoqi 60355539Sluoqi if (error == EWOULDBLOCK) 604132866Snjl printf("timed out\n"); 60555539Sluoqi else 606132866Snjl printf("done\n"); 60755539Sluoqi} 60893496Sphk 60993496Sphk/* Registration of dumpers */ 61093496Sphkint 61193496Sphkset_dumper(struct dumperinfo *di) 61293496Sphk{ 613110859Salfred 61493496Sphk if (di == NULL) { 61593496Sphk bzero(&dumper, sizeof dumper); 61693496Sphk return (0); 61793496Sphk } 61893496Sphk if (dumper.dumper != NULL) 61993496Sphk return (EBUSY); 62093496Sphk dumper = *di; 62193496Sphk return (0); 62293496Sphk} 62393496Sphk 624105531Stmm#if defined(__powerpc__) 62593496Sphkvoid 62693496Sphkdumpsys(struct dumperinfo *di __unused) 62793496Sphk{ 62893496Sphk 62993496Sphk printf("Kernel dumps not implemented on this architecture\n"); 63093496Sphk} 63193496Sphk#endif 632