kern_shutdown.c revision 149875
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 149875 2005-09-08 06:30:05Z truckman $"); 39116182Sobrien 40131927Smarcel#include "opt_kdb.h" 41106024Srwatson#include "opt_mac.h" 4228976Sbde#include "opt_panic.h" 4328976Sbde#include "opt_show_busybufs.h" 44134649Sscottl#include "opt_sched.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> 62137263Speter#include <sys/sched.h> 6378767Sjhb#include <sys/smp.h> /* smp_active */ 6417658Sjulian#include <sys/sysctl.h> 6517658Sjulian#include <sys/sysproto.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 10717658Sjulian/* 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 11393496Sphkint dumping; /* system is dumping */ 11493496Sphkstatic struct dumperinfo dumper; /* our selected dumper */ 11567093Sps 116131927Smarcel/* Context information for dump-debuggers. */ 117131927Smarcelstatic struct pcb dumppcb; /* Registers. */ 118131927Smarcelstatic lwpid_t dumptid; /* Thread ID. */ 119131927Smarcel 12065395Speterstatic void boot(int) __dead2; 12165395Speterstatic void poweroff_wait(void *, int); 12265395Speterstatic void shutdown_halt(void *junk, int howto); 12365395Speterstatic void shutdown_panic(void *junk, int howto); 12465395Speterstatic void shutdown_reset(void *junk, int howto); 12517658Sjulian 12650107Smsmith/* register various local shutdown events */ 127110859Salfredstatic void 12850107Smsmithshutdown_conf(void *unused) 12950107Smsmith{ 130110859Salfred 131110859Salfred EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 132110859Salfred SHUTDOWN_PRI_FIRST); 133110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 134110859Salfred SHUTDOWN_PRI_LAST + 100); 135110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 136110859Salfred SHUTDOWN_PRI_LAST + 100); 137110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 138110859Salfred SHUTDOWN_PRI_LAST + 200); 13950107Smsmith} 14048868Sphk 14150107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 14250107Smsmith 14317658Sjulian/* 14417658Sjulian * The system call that results in a reboot 14582749Sdillon * 14682749Sdillon * MPSAFE 14717658Sjulian */ 14882749Sdillon/* ARGSUSED */ 14917658Sjulianint 15083366Sjulianreboot(struct thread *td, struct reboot_args *uap) 15117658Sjulian{ 15217658Sjulian int error; 15317658Sjulian 154106024Srwatson error = 0; 155106024Srwatson#ifdef MAC 156106024Srwatson error = mac_check_system_reboot(td->td_ucred, uap->opt); 157106024Srwatson#endif 158106024Srwatson if (error == 0) 159106024Srwatson error = suser(td); 160106024Srwatson if (error == 0) { 161106024Srwatson mtx_lock(&Giant); 16282749Sdillon boot(uap->opt); 163106024Srwatson mtx_unlock(&Giant); 164106024Srwatson } 16582749Sdillon return (error); 16617658Sjulian} 16717658Sjulian 16817658Sjulian/* 16917658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 17017658Sjulian */ 17165268Smsmithstatic int shutdown_howto = 0; 17265268Smsmith 17317658Sjulianvoid 17465268Smsmithshutdown_nice(int howto) 17517658Sjulian{ 176110859Salfred 17765268Smsmith shutdown_howto = howto; 178110859Salfred 17917658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 18017658Sjulian if (initproc != NULL) { 18173913Sjhb PROC_LOCK(initproc); 18217658Sjulian psignal(initproc, SIGINT); 18373913Sjhb PROC_UNLOCK(initproc); 18417658Sjulian } else { 18517658Sjulian /* No init(8) running, so simply reboot */ 18617658Sjulian boot(RB_NOSYNC); 18717658Sjulian } 18817658Sjulian return; 18917658Sjulian} 19017658Sjulianstatic int waittime = -1; 19117658Sjulian 19254233Sphkstatic void 19365395Speterprint_uptime(void) 19454233Sphk{ 19554233Sphk int f; 19654233Sphk struct timespec ts; 19754233Sphk 19854233Sphk getnanouptime(&ts); 19954233Sphk printf("Uptime: "); 20054233Sphk f = 0; 20154233Sphk if (ts.tv_sec >= 86400) { 20265764Sjhb printf("%ldd", (long)ts.tv_sec / 86400); 20354233Sphk ts.tv_sec %= 86400; 20454233Sphk f = 1; 20554233Sphk } 20654233Sphk if (f || ts.tv_sec >= 3600) { 20765764Sjhb printf("%ldh", (long)ts.tv_sec / 3600); 20854233Sphk ts.tv_sec %= 3600; 20954233Sphk f = 1; 21054233Sphk } 21154233Sphk if (f || ts.tv_sec >= 60) { 21265764Sjhb printf("%ldm", (long)ts.tv_sec / 60); 21354233Sphk ts.tv_sec %= 60; 21454233Sphk f = 1; 21554233Sphk } 21665764Sjhb printf("%lds\n", (long)ts.tv_sec); 21754233Sphk} 21854233Sphk 21994169Sphkstatic void 22094169Sphkdoadump(void) 22194169Sphk{ 222110859Salfred 223132412Sjulian /* 224132412Sjulian * Sometimes people have to call this from the kernel debugger. 225132412Sjulian * (if 'panic' can not dump) 226132412Sjulian * Give them a clue as to why they can't dump. 227132412Sjulian */ 228132412Sjulian if (dumper.dumper == NULL) { 229132413Sjulian printf("Cannot dump. No dump device defined.\n"); 230132412Sjulian return; 231132412Sjulian } 232132412Sjulian 23394169Sphk savectx(&dumppcb); 234131927Smarcel dumptid = curthread->td_tid; 23594169Sphk dumping++; 23694169Sphk dumpsys(&dumper); 23794169Sphk} 23894169Sphk 239149875Struckmanstatic int 240149875Struckmanisbufbusy(struct buf *bp) 241149875Struckman{ 242149875Struckman if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 && 243149875Struckman BUF_REFCNT(bp) > 0) || 244149875Struckman ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI)) 245149875Struckman return (1); 246149875Struckman return (0); 247149875Struckman} 248149875Struckman 24917658Sjulian/* 250137329Snjl * Shutdown the system cleanly to prepare for reboot, halt, or power off. 25117658Sjulian */ 25231275Sbdestatic void 25365395Speterboot(int howto) 25417658Sjulian{ 255133763Struckman static int first_buf_printf = 1; 25617658Sjulian 257137375Smarcel#if defined(SMP) 258137329Snjl /* 259137329Snjl * Bind us to CPU 0 so that all shutdown code runs there. Some 260137329Snjl * systems don't shutdown properly (i.e., ACPI power off) if we 261137329Snjl * run on another processor. 262137329Snjl */ 263137263Speter mtx_lock_spin(&sched_lock); 264137263Speter sched_bind(curthread, 0); 265137263Speter mtx_unlock_spin(&sched_lock); 266138217Snjl KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0")); 267137263Speter#endif 268137263Speter 26965268Smsmith /* collect extra flags that shutdown_nice might have set */ 27065268Smsmith howto |= shutdown_howto; 27165268Smsmith 27282119Sjhb /* We are out of the debugger now. */ 273131927Smarcel kdb_active = 0; 27482119Sjhb 27527997Sjulian /* 27627997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 27727997Sjulian */ 27850107Smsmith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 27927997Sjulian 28027997Sjulian /* 28127997Sjulian * Now sync filesystems 28227997Sjulian */ 28317658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 28417658Sjulian register struct buf *bp; 28565707Sjasone int iter, nbusy, pbusy; 286131481Sjhb#ifndef PREEMPTION 28765707Sjasone int subiter; 288131481Sjhb#endif 28917658Sjulian 29017658Sjulian waittime = 0; 29117658Sjulian 29290361Sjulian sync(&thread0, NULL); 29317658Sjulian 29434266Sjulian /* 29534266Sjulian * With soft updates, some buffers that are 29634266Sjulian * written will be remarked as dirty until other 29734266Sjulian * buffers are written. 29834266Sjulian */ 29965707Sjasone for (iter = pbusy = 0; iter < 20; iter++) { 30017658Sjulian nbusy = 0; 301149875Struckman for (bp = &buf[nbuf]; --bp >= buf; ) 302149875Struckman if (isbufbusy(bp)) 30317658Sjulian nbusy++; 304133763Struckman if (nbusy == 0) { 305133763Struckman if (first_buf_printf) 306136115Sphk printf("All buffers synced."); 30717658Sjulian break; 308133763Struckman } 309133763Struckman if (first_buf_printf) { 310133763Struckman printf("Syncing disks, buffers remaining... "); 311133763Struckman first_buf_printf = 0; 312133763Struckman } 31317658Sjulian printf("%d ", nbusy); 31465707Sjasone if (nbusy < pbusy) 31565707Sjasone iter = 0; 31665707Sjasone pbusy = nbusy; 31790361Sjulian sync(&thread0, NULL); 318131481Sjhb 319131481Sjhb#ifdef PREEMPTION 320131481Sjhb /* 321131481Sjhb * Drop Giant and spin for a while to allow 322131481Sjhb * interrupt threads to run. 323131481Sjhb */ 324131481Sjhb DROP_GIANT(); 32534266Sjulian DELAY(50000 * iter); 326131481Sjhb PICKUP_GIANT(); 327131481Sjhb#else 328131481Sjhb /* 329131481Sjhb * Drop Giant and context switch several times to 330131481Sjhb * allow interrupt threads to run. 331131481Sjhb */ 332131481Sjhb DROP_GIANT(); 333131481Sjhb for (subiter = 0; subiter < 50 * iter; subiter++) { 334131481Sjhb mtx_lock_spin(&sched_lock); 335131481Sjhb mi_switch(SW_VOL, NULL); 336131481Sjhb mtx_unlock_spin(&sched_lock); 337131481Sjhb DELAY(1000); 338131481Sjhb } 339131481Sjhb PICKUP_GIANT(); 340131481Sjhb#endif 34117658Sjulian } 342133418Snjl printf("\n"); 34341137Smsmith /* 34441137Smsmith * Count only busy local buffers to prevent forcing 34541137Smsmith * a fsck if we're just a client of a wedged NFS server 34641137Smsmith */ 34741137Smsmith nbusy = 0; 34841137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 349149875Struckman if (isbufbusy(bp)) { 350137186Sphk#if 0 351137186Sphk/* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */ 352130640Sphk if (bp->b_dev == NULL) { 35353452Sphk TAILQ_REMOVE(&mountlist, 35448225Smckusick bp->b_vp->v_mount, mnt_list); 35553023Sphk continue; 35653023Sphk } 357137186Sphk#endif 35853023Sphk nbusy++; 35953023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 36053023Sphk printf( 361137186Sphk "%d: bufobj:%p, flags:%0x, blkno:%ld, lblkno:%ld\n", 362137186Sphk nbusy, bp->b_bufobj, 36353023Sphk bp->b_flags, (long)bp->b_blkno, 36453023Sphk (long)bp->b_lblkno); 36553023Sphk#endif 36646568Speter } 36741137Smsmith } 36817658Sjulian if (nbusy) { 36917658Sjulian /* 37017658Sjulian * Failed to sync all blocks. Indicate this and don't 37117658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 37217658Sjulian */ 373133763Struckman printf("Giving up on %d buffers\n", nbusy); 37417658Sjulian DELAY(5000000); /* 5 seconds */ 37517658Sjulian } else { 376133763Struckman if (!first_buf_printf) 377133763Struckman printf("Final sync complete\n"); 37817658Sjulian /* 37917658Sjulian * Unmount filesystems 38017658Sjulian */ 38117658Sjulian if (panicstr == 0) 38217658Sjulian vfs_unmountall(); 38317658Sjulian } 38439237Sgibbs DELAY(100000); /* wait for console output to finish */ 38517658Sjulian } 38627997Sjulian 38754233Sphk print_uptime(); 38854233Sphk 38927997Sjulian /* 39027997Sjulian * Ok, now do things that assume all filesystem activity has 39127997Sjulian * been completed. 39227997Sjulian */ 39350107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 394137329Snjl 395137329Snjl /* XXX This doesn't disable interrupts any more. Reconsider? */ 39639237Sgibbs splhigh(); 397137329Snjl 398132412Sjulian if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 39994169Sphk doadump(); 40039237Sgibbs 40139237Sgibbs /* Now that we're going to really halt the system... */ 40250107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 40339237Sgibbs 40450107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 40550107Smsmith /* NOTREACHED */ 40650107Smsmith} 40750107Smsmith 40850107Smsmith/* 40950107Smsmith * If the shutdown was a clean halt, behave accordingly. 41050107Smsmith */ 41150107Smsmithstatic void 41250107Smsmithshutdown_halt(void *junk, int howto) 41350107Smsmith{ 414110859Salfred 41517658Sjulian if (howto & RB_HALT) { 41617658Sjulian printf("\n"); 41717658Sjulian printf("The operating system has halted.\n"); 41817658Sjulian printf("Please press any key to reboot.\n\n"); 41919274Sjulian switch (cngetc()) { 42019274Sjulian case -1: /* No console, just die */ 42119274Sjulian cpu_halt(); 42219274Sjulian /* NOTREACHED */ 42319274Sjulian default: 42439237Sgibbs howto &= ~RB_HALT; 42519274Sjulian break; 42619274Sjulian } 42750107Smsmith } 42850107Smsmith} 42917658Sjulian 43050107Smsmith/* 43150107Smsmith * Check to see if the system paniced, pause and then reboot 43250107Smsmith * according to the specified delay. 43350107Smsmith */ 43450107Smsmithstatic void 43550107Smsmithshutdown_panic(void *junk, int howto) 43650107Smsmith{ 43750107Smsmith int loop; 43850107Smsmith 43950107Smsmith if (howto & RB_DUMP) { 44039237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 44139237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 44239237Sgibbs printf("Automatic reboot in %d seconds - " 44339237Sgibbs "press a key on the console to abort\n", 44439237Sgibbs PANIC_REBOOT_WAIT_TIME); 44539237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 44639237Sgibbs loop > 0; --loop) { 44739237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 44839237Sgibbs /* Did user type a key? */ 44939237Sgibbs if (cncheckc() != -1) 45039237Sgibbs break; 45117658Sjulian } 45239237Sgibbs if (!loop) 45350107Smsmith return; 45417658Sjulian } 45539237Sgibbs } else { /* zero time specified - reboot NOW */ 45650107Smsmith return; 45717658Sjulian } 45889522Snik printf("--> Press a key on the console to reboot,\n"); 45989522Snik printf("--> or switch off the system now.\n"); 46039237Sgibbs cngetc(); 46117658Sjulian } 46250107Smsmith} 46350107Smsmith 46450107Smsmith/* 46550107Smsmith * Everything done, now reset 46650107Smsmith */ 46750107Smsmithstatic void 46850107Smsmithshutdown_reset(void *junk, int howto) 46950107Smsmith{ 470110859Salfred 47117658Sjulian printf("Rebooting...\n"); 47217658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 47317677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 47417658Sjulian cpu_reset(); 47550107Smsmith /* NOTREACHED */ /* assuming reset worked */ 47617658Sjulian} 47717658Sjulian 47875570Sjhb#ifdef SMP 479101155Sjhbstatic u_int panic_cpu = NOCPU; 48075570Sjhb#endif 48175570Sjhb 48217658Sjulian/* 48317658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 48417658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 48517658Sjulian * the disks as this often leads to recursive panics. 48682749Sdillon * 48782749Sdillon * MPSAFE 48817658Sjulian */ 48917658Sjulianvoid 490130164Sphkpanic(const char *fmt, ...) 49117658Sjulian{ 492100209Sgallatin struct thread *td = curthread; 493103647Sjhb int bootopt, newpanic; 49417658Sjulian va_list ap; 49538874Sache static char buf[256]; 49617658Sjulian 49765557Sjasone#ifdef SMP 49882115Sjhb /* 49982115Sjhb * We don't want multiple CPU's to panic at the same time, so we 500101155Sjhb * use panic_cpu as a simple spinlock. We have to keep checking 501101155Sjhb * panic_cpu if we are spinning in case the panic on the first 50282115Sjhb * CPU is canceled. 50382115Sjhb */ 504101155Sjhb if (panic_cpu != PCPU_GET(cpuid)) 505101155Sjhb while (atomic_cmpset_int(&panic_cpu, NOCPU, 506101155Sjhb PCPU_GET(cpuid)) == 0) 507101155Sjhb while (panic_cpu != NOCPU) 508101155Sjhb ; /* nothing */ 50965557Sjasone#endif 51065557Sjasone 51117658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 512103647Sjhb newpanic = 0; 51317658Sjulian if (panicstr) 51417658Sjulian bootopt |= RB_NOSYNC; 515103647Sjhb else { 51617658Sjulian panicstr = fmt; 517103647Sjhb newpanic = 1; 518103647Sjhb } 51917658Sjulian 52017658Sjulian va_start(ap, fmt); 521116398Siedowse if (newpanic) { 522116398Siedowse (void)vsnprintf(buf, sizeof(buf), fmt, ap); 52338874Sache panicstr = buf; 524130164Sphk printf("panic: %s\n", buf); 525116398Siedowse } else { 526116398Siedowse printf("panic: "); 527116398Siedowse vprintf(fmt, ap); 528130164Sphk printf("\n"); 529116398Siedowse } 53017658Sjulian va_end(ap); 53126100Sfsmp#ifdef SMP 532134089Sjhb printf("cpuid = %d\n", PCPU_GET(cpuid)); 53326100Sfsmp#endif 53417658Sjulian 535131927Smarcel#ifdef KDB 536103647Sjhb if (newpanic && trace_on_panic) 537131927Smarcel kdb_backtrace(); 53817658Sjulian if (debugger_on_panic) 539131927Smarcel kdb_enter("panic"); 54082223Sjhb#ifdef RESTARTABLE_PANICS 54182115Sjhb /* See if the user aborted the panic, in which case we continue. */ 54282115Sjhb if (panicstr == NULL) { 54382115Sjhb#ifdef SMP 544101155Sjhb atomic_store_rel_int(&panic_cpu, NOCPU); 54517658Sjulian#endif 54682115Sjhb return; 54782115Sjhb } 54882115Sjhb#endif 54982223Sjhb#endif 550113633Sjhb mtx_lock_spin(&sched_lock); 551100209Sgallatin td->td_flags |= TDF_INPANIC; 552113633Sjhb mtx_unlock_spin(&sched_lock); 55385202Speter if (!sync_on_panic) 55485202Speter bootopt |= RB_NOSYNC; 55517658Sjulian boot(bootopt); 55617658Sjulian} 55717658Sjulian 55817768Sjulian/* 55943436Smsmith * Support for poweroff delay. 56043436Smsmith */ 56154248Smsmith#ifndef POWEROFF_DELAY 56254248Smsmith# define POWEROFF_DELAY 5000 56354248Smsmith#endif 56454248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 56554248Smsmith 56643436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 56743436Smsmith &poweroff_delay, 0, ""); 56843436Smsmith 569110859Salfredstatic void 57050107Smsmithpoweroff_wait(void *junk, int howto) 57143436Smsmith{ 572110859Salfred 573110859Salfred if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 57443436Smsmith return; 57543436Smsmith DELAY(poweroff_delay * 1000); 57643436Smsmith} 57755539Sluoqi 57855539Sluoqi/* 57955539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 58055539Sluoqi * points in their main loops prior to a system shutdown, so that they 58155539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 58255539Sluoqi * to cause sync to fail). For each of these system processes, register 58355539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 58455539Sluoqi */ 58555539Sluoqistatic int kproc_shutdown_wait = 60; 58655539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 58755539Sluoqi &kproc_shutdown_wait, 0, ""); 58855539Sluoqi 58955539Sluoqivoid 59070063Sjhbkproc_shutdown(void *arg, int howto) 59155539Sluoqi{ 59255539Sluoqi struct proc *p; 593132177Salfred char procname[MAXCOMLEN + 1]; 59455539Sluoqi int error; 59555539Sluoqi 59655539Sluoqi if (panicstr) 59755539Sluoqi return; 59855539Sluoqi 59955539Sluoqi p = (struct proc *)arg; 600132177Salfred strlcpy(procname, p->p_comm, sizeof(procname)); 601132866Snjl printf("Waiting (max %d seconds) for system process `%s' to stop...", 602132177Salfred kproc_shutdown_wait, procname); 60370063Sjhb error = kthread_suspend(p, kproc_shutdown_wait * hz); 60455539Sluoqi 60555539Sluoqi if (error == EWOULDBLOCK) 606132866Snjl printf("timed out\n"); 60755539Sluoqi else 608132866Snjl printf("done\n"); 60955539Sluoqi} 61093496Sphk 61193496Sphk/* Registration of dumpers */ 61293496Sphkint 61393496Sphkset_dumper(struct dumperinfo *di) 61493496Sphk{ 615110859Salfred 61693496Sphk if (di == NULL) { 61793496Sphk bzero(&dumper, sizeof dumper); 61893496Sphk return (0); 61993496Sphk } 62093496Sphk if (dumper.dumper != NULL) 62193496Sphk return (EBUSY); 62293496Sphk dumper = *di; 62393496Sphk return (0); 62493496Sphk} 62593496Sphk 626105531Stmm#if defined(__powerpc__) 62793496Sphkvoid 62893496Sphkdumpsys(struct dumperinfo *di __unused) 62993496Sphk{ 63093496Sphk 63193496Sphk printf("Kernel dumps not implemented on this architecture\n"); 63293496Sphk} 63393496Sphk#endif 634