kern_shutdown.c revision 155383
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 155383 2006-02-06 10:12:00Z jeff $"); 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 */ 114155383Sjeffint rebooting; /* system is rebooting */ 11593496Sphkstatic struct dumperinfo dumper; /* our selected dumper */ 11667093Sps 117131927Smarcel/* Context information for dump-debuggers. */ 118131927Smarcelstatic struct pcb dumppcb; /* Registers. */ 119131927Smarcelstatic lwpid_t dumptid; /* Thread ID. */ 120131927Smarcel 12165395Speterstatic void boot(int) __dead2; 12265395Speterstatic void poweroff_wait(void *, int); 12365395Speterstatic void shutdown_halt(void *junk, int howto); 12465395Speterstatic void shutdown_panic(void *junk, int howto); 12565395Speterstatic void shutdown_reset(void *junk, int howto); 12617658Sjulian 12750107Smsmith/* register various local shutdown events */ 128110859Salfredstatic void 12950107Smsmithshutdown_conf(void *unused) 13050107Smsmith{ 131110859Salfred 132110859Salfred EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 133110859Salfred SHUTDOWN_PRI_FIRST); 134110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 135110859Salfred SHUTDOWN_PRI_LAST + 100); 136110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 137110859Salfred SHUTDOWN_PRI_LAST + 100); 138110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 139110859Salfred SHUTDOWN_PRI_LAST + 200); 14050107Smsmith} 14148868Sphk 14250107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 14350107Smsmith 14417658Sjulian/* 14517658Sjulian * The system call that results in a reboot 14682749Sdillon * 14782749Sdillon * MPSAFE 14817658Sjulian */ 14982749Sdillon/* ARGSUSED */ 15017658Sjulianint 15183366Sjulianreboot(struct thread *td, struct reboot_args *uap) 15217658Sjulian{ 15317658Sjulian int error; 15417658Sjulian 155106024Srwatson error = 0; 156106024Srwatson#ifdef MAC 157106024Srwatson error = mac_check_system_reboot(td->td_ucred, uap->opt); 158106024Srwatson#endif 159106024Srwatson if (error == 0) 160106024Srwatson error = suser(td); 161106024Srwatson if (error == 0) { 162106024Srwatson mtx_lock(&Giant); 16382749Sdillon boot(uap->opt); 164106024Srwatson mtx_unlock(&Giant); 165106024Srwatson } 16682749Sdillon return (error); 16717658Sjulian} 16817658Sjulian 16917658Sjulian/* 17017658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 17117658Sjulian */ 17265268Smsmithstatic int shutdown_howto = 0; 17365268Smsmith 17417658Sjulianvoid 17565268Smsmithshutdown_nice(int howto) 17617658Sjulian{ 177110859Salfred 17865268Smsmith shutdown_howto = howto; 179110859Salfred 18017658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 18117658Sjulian if (initproc != NULL) { 18273913Sjhb PROC_LOCK(initproc); 18317658Sjulian psignal(initproc, SIGINT); 18473913Sjhb PROC_UNLOCK(initproc); 18517658Sjulian } else { 18617658Sjulian /* No init(8) running, so simply reboot */ 18717658Sjulian boot(RB_NOSYNC); 18817658Sjulian } 18917658Sjulian return; 19017658Sjulian} 19117658Sjulianstatic int waittime = -1; 19217658Sjulian 19354233Sphkstatic void 19465395Speterprint_uptime(void) 19554233Sphk{ 19654233Sphk int f; 19754233Sphk struct timespec ts; 19854233Sphk 19954233Sphk getnanouptime(&ts); 20054233Sphk printf("Uptime: "); 20154233Sphk f = 0; 20254233Sphk if (ts.tv_sec >= 86400) { 20365764Sjhb printf("%ldd", (long)ts.tv_sec / 86400); 20454233Sphk ts.tv_sec %= 86400; 20554233Sphk f = 1; 20654233Sphk } 20754233Sphk if (f || ts.tv_sec >= 3600) { 20865764Sjhb printf("%ldh", (long)ts.tv_sec / 3600); 20954233Sphk ts.tv_sec %= 3600; 21054233Sphk f = 1; 21154233Sphk } 21254233Sphk if (f || ts.tv_sec >= 60) { 21365764Sjhb printf("%ldm", (long)ts.tv_sec / 60); 21454233Sphk ts.tv_sec %= 60; 21554233Sphk f = 1; 21654233Sphk } 21765764Sjhb printf("%lds\n", (long)ts.tv_sec); 21854233Sphk} 21954233Sphk 22094169Sphkstatic void 22194169Sphkdoadump(void) 22294169Sphk{ 223110859Salfred 224132412Sjulian /* 225132412Sjulian * Sometimes people have to call this from the kernel debugger. 226132412Sjulian * (if 'panic' can not dump) 227132412Sjulian * Give them a clue as to why they can't dump. 228132412Sjulian */ 229132412Sjulian if (dumper.dumper == NULL) { 230132413Sjulian printf("Cannot dump. No dump device defined.\n"); 231132412Sjulian return; 232132412Sjulian } 233132412Sjulian 23494169Sphk savectx(&dumppcb); 235131927Smarcel dumptid = curthread->td_tid; 23694169Sphk dumping++; 23794169Sphk dumpsys(&dumper); 23894169Sphk} 23994169Sphk 240149875Struckmanstatic int 241149875Struckmanisbufbusy(struct buf *bp) 242149875Struckman{ 243149875Struckman if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 && 244149875Struckman BUF_REFCNT(bp) > 0) || 245149875Struckman ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI)) 246149875Struckman return (1); 247149875Struckman return (0); 248149875Struckman} 249149875Struckman 25017658Sjulian/* 251137329Snjl * Shutdown the system cleanly to prepare for reboot, halt, or power off. 25217658Sjulian */ 25331275Sbdestatic void 25465395Speterboot(int howto) 25517658Sjulian{ 256133763Struckman static int first_buf_printf = 1; 25717658Sjulian 258137375Smarcel#if defined(SMP) 259137329Snjl /* 260137329Snjl * Bind us to CPU 0 so that all shutdown code runs there. Some 261137329Snjl * systems don't shutdown properly (i.e., ACPI power off) if we 262137329Snjl * run on another processor. 263137329Snjl */ 264137263Speter mtx_lock_spin(&sched_lock); 265137263Speter sched_bind(curthread, 0); 266137263Speter mtx_unlock_spin(&sched_lock); 267138217Snjl KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0")); 268137263Speter#endif 269155383Sjeff /* We're in the process of rebooting. */ 270155383Sjeff rebooting = 1; 271137263Speter 27265268Smsmith /* collect extra flags that shutdown_nice might have set */ 27365268Smsmith howto |= shutdown_howto; 27465268Smsmith 27582119Sjhb /* We are out of the debugger now. */ 276131927Smarcel kdb_active = 0; 27782119Sjhb 27827997Sjulian /* 27927997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 28027997Sjulian */ 28150107Smsmith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 28227997Sjulian 28327997Sjulian /* 28427997Sjulian * Now sync filesystems 28527997Sjulian */ 28617658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 28717658Sjulian register struct buf *bp; 28865707Sjasone int iter, nbusy, pbusy; 289131481Sjhb#ifndef PREEMPTION 29065707Sjasone int subiter; 291131481Sjhb#endif 29217658Sjulian 29317658Sjulian waittime = 0; 29417658Sjulian 295150472Sups sync(curthread, NULL); 29617658Sjulian 29734266Sjulian /* 29834266Sjulian * With soft updates, some buffers that are 29934266Sjulian * written will be remarked as dirty until other 30034266Sjulian * buffers are written. 30134266Sjulian */ 30265707Sjasone for (iter = pbusy = 0; iter < 20; iter++) { 30317658Sjulian nbusy = 0; 304149875Struckman for (bp = &buf[nbuf]; --bp >= buf; ) 305149875Struckman if (isbufbusy(bp)) 30617658Sjulian nbusy++; 307133763Struckman if (nbusy == 0) { 308133763Struckman if (first_buf_printf) 309136115Sphk printf("All buffers synced."); 31017658Sjulian break; 311133763Struckman } 312133763Struckman if (first_buf_printf) { 313133763Struckman printf("Syncing disks, buffers remaining... "); 314133763Struckman first_buf_printf = 0; 315133763Struckman } 31617658Sjulian printf("%d ", nbusy); 31765707Sjasone if (nbusy < pbusy) 31865707Sjasone iter = 0; 31965707Sjasone pbusy = nbusy; 320150472Sups sync(curthread, NULL); 321131481Sjhb 322131481Sjhb#ifdef PREEMPTION 323131481Sjhb /* 324131481Sjhb * Drop Giant and spin for a while to allow 325131481Sjhb * interrupt threads to run. 326131481Sjhb */ 327131481Sjhb DROP_GIANT(); 32834266Sjulian DELAY(50000 * iter); 329131481Sjhb PICKUP_GIANT(); 330131481Sjhb#else 331131481Sjhb /* 332131481Sjhb * Drop Giant and context switch several times to 333131481Sjhb * allow interrupt threads to run. 334131481Sjhb */ 335131481Sjhb DROP_GIANT(); 336131481Sjhb for (subiter = 0; subiter < 50 * iter; subiter++) { 337131481Sjhb mtx_lock_spin(&sched_lock); 338131481Sjhb mi_switch(SW_VOL, NULL); 339131481Sjhb mtx_unlock_spin(&sched_lock); 340131481Sjhb DELAY(1000); 341131481Sjhb } 342131481Sjhb PICKUP_GIANT(); 343131481Sjhb#endif 34417658Sjulian } 345133418Snjl printf("\n"); 34641137Smsmith /* 34741137Smsmith * Count only busy local buffers to prevent forcing 34841137Smsmith * a fsck if we're just a client of a wedged NFS server 34941137Smsmith */ 35041137Smsmith nbusy = 0; 35141137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 352149875Struckman if (isbufbusy(bp)) { 353137186Sphk#if 0 354137186Sphk/* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */ 355130640Sphk if (bp->b_dev == NULL) { 35653452Sphk TAILQ_REMOVE(&mountlist, 35748225Smckusick bp->b_vp->v_mount, mnt_list); 35853023Sphk continue; 35953023Sphk } 360137186Sphk#endif 36153023Sphk nbusy++; 36253023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 36353023Sphk printf( 364137186Sphk "%d: bufobj:%p, flags:%0x, blkno:%ld, lblkno:%ld\n", 365137186Sphk nbusy, bp->b_bufobj, 36653023Sphk bp->b_flags, (long)bp->b_blkno, 36753023Sphk (long)bp->b_lblkno); 36853023Sphk#endif 36946568Speter } 37041137Smsmith } 37117658Sjulian if (nbusy) { 37217658Sjulian /* 37317658Sjulian * Failed to sync all blocks. Indicate this and don't 37417658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 37517658Sjulian */ 376133763Struckman printf("Giving up on %d buffers\n", nbusy); 37717658Sjulian DELAY(5000000); /* 5 seconds */ 37817658Sjulian } else { 379133763Struckman if (!first_buf_printf) 380133763Struckman printf("Final sync complete\n"); 38117658Sjulian /* 38217658Sjulian * Unmount filesystems 38317658Sjulian */ 38417658Sjulian if (panicstr == 0) 38517658Sjulian vfs_unmountall(); 38617658Sjulian } 38739237Sgibbs DELAY(100000); /* wait for console output to finish */ 38817658Sjulian } 38927997Sjulian 39054233Sphk print_uptime(); 39154233Sphk 39227997Sjulian /* 39327997Sjulian * Ok, now do things that assume all filesystem activity has 39427997Sjulian * been completed. 39527997Sjulian */ 39650107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 397137329Snjl 398137329Snjl /* XXX This doesn't disable interrupts any more. Reconsider? */ 39939237Sgibbs splhigh(); 400137329Snjl 401132412Sjulian if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 40294169Sphk doadump(); 40339237Sgibbs 40439237Sgibbs /* Now that we're going to really halt the system... */ 40550107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 40639237Sgibbs 40750107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 40850107Smsmith /* NOTREACHED */ 40950107Smsmith} 41050107Smsmith 41150107Smsmith/* 41250107Smsmith * If the shutdown was a clean halt, behave accordingly. 41350107Smsmith */ 41450107Smsmithstatic void 41550107Smsmithshutdown_halt(void *junk, int howto) 41650107Smsmith{ 417110859Salfred 41817658Sjulian if (howto & RB_HALT) { 41917658Sjulian printf("\n"); 42017658Sjulian printf("The operating system has halted.\n"); 42117658Sjulian printf("Please press any key to reboot.\n\n"); 42219274Sjulian switch (cngetc()) { 42319274Sjulian case -1: /* No console, just die */ 42419274Sjulian cpu_halt(); 42519274Sjulian /* NOTREACHED */ 42619274Sjulian default: 42739237Sgibbs howto &= ~RB_HALT; 42819274Sjulian break; 42919274Sjulian } 43050107Smsmith } 43150107Smsmith} 43217658Sjulian 43350107Smsmith/* 43450107Smsmith * Check to see if the system paniced, pause and then reboot 43550107Smsmith * according to the specified delay. 43650107Smsmith */ 43750107Smsmithstatic void 43850107Smsmithshutdown_panic(void *junk, int howto) 43950107Smsmith{ 44050107Smsmith int loop; 44150107Smsmith 44250107Smsmith if (howto & RB_DUMP) { 44339237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 44439237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 44539237Sgibbs printf("Automatic reboot in %d seconds - " 44639237Sgibbs "press a key on the console to abort\n", 44739237Sgibbs PANIC_REBOOT_WAIT_TIME); 44839237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 44939237Sgibbs loop > 0; --loop) { 45039237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 45139237Sgibbs /* Did user type a key? */ 45239237Sgibbs if (cncheckc() != -1) 45339237Sgibbs break; 45417658Sjulian } 45539237Sgibbs if (!loop) 45650107Smsmith return; 45717658Sjulian } 45839237Sgibbs } else { /* zero time specified - reboot NOW */ 45950107Smsmith return; 46017658Sjulian } 46189522Snik printf("--> Press a key on the console to reboot,\n"); 46289522Snik printf("--> or switch off the system now.\n"); 46339237Sgibbs cngetc(); 46417658Sjulian } 46550107Smsmith} 46650107Smsmith 46750107Smsmith/* 46850107Smsmith * Everything done, now reset 46950107Smsmith */ 47050107Smsmithstatic void 47150107Smsmithshutdown_reset(void *junk, int howto) 47250107Smsmith{ 473110859Salfred 47417658Sjulian printf("Rebooting...\n"); 47517658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 47617677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 47717658Sjulian cpu_reset(); 47850107Smsmith /* NOTREACHED */ /* assuming reset worked */ 47917658Sjulian} 48017658Sjulian 48175570Sjhb#ifdef SMP 482101155Sjhbstatic u_int panic_cpu = NOCPU; 48375570Sjhb#endif 48475570Sjhb 48517658Sjulian/* 48617658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 48717658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 48817658Sjulian * the disks as this often leads to recursive panics. 48982749Sdillon * 49082749Sdillon * MPSAFE 49117658Sjulian */ 49217658Sjulianvoid 493130164Sphkpanic(const char *fmt, ...) 49417658Sjulian{ 495100209Sgallatin struct thread *td = curthread; 496103647Sjhb int bootopt, newpanic; 49717658Sjulian va_list ap; 49838874Sache static char buf[256]; 49917658Sjulian 50065557Sjasone#ifdef SMP 50182115Sjhb /* 50282115Sjhb * We don't want multiple CPU's to panic at the same time, so we 503101155Sjhb * use panic_cpu as a simple spinlock. We have to keep checking 504101155Sjhb * panic_cpu if we are spinning in case the panic on the first 50582115Sjhb * CPU is canceled. 50682115Sjhb */ 507101155Sjhb if (panic_cpu != PCPU_GET(cpuid)) 508101155Sjhb while (atomic_cmpset_int(&panic_cpu, NOCPU, 509101155Sjhb PCPU_GET(cpuid)) == 0) 510101155Sjhb while (panic_cpu != NOCPU) 511101155Sjhb ; /* nothing */ 51265557Sjasone#endif 51365557Sjasone 51417658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 515103647Sjhb newpanic = 0; 51617658Sjulian if (panicstr) 51717658Sjulian bootopt |= RB_NOSYNC; 518103647Sjhb else { 51917658Sjulian panicstr = fmt; 520103647Sjhb newpanic = 1; 521103647Sjhb } 52217658Sjulian 52317658Sjulian va_start(ap, fmt); 524116398Siedowse if (newpanic) { 525116398Siedowse (void)vsnprintf(buf, sizeof(buf), fmt, ap); 52638874Sache panicstr = buf; 527130164Sphk printf("panic: %s\n", buf); 528116398Siedowse } else { 529116398Siedowse printf("panic: "); 530116398Siedowse vprintf(fmt, ap); 531130164Sphk printf("\n"); 532116398Siedowse } 53317658Sjulian va_end(ap); 53426100Sfsmp#ifdef SMP 535134089Sjhb printf("cpuid = %d\n", PCPU_GET(cpuid)); 53626100Sfsmp#endif 53717658Sjulian 538131927Smarcel#ifdef KDB 539103647Sjhb if (newpanic && trace_on_panic) 540131927Smarcel kdb_backtrace(); 54117658Sjulian if (debugger_on_panic) 542131927Smarcel kdb_enter("panic"); 54382223Sjhb#ifdef RESTARTABLE_PANICS 54482115Sjhb /* See if the user aborted the panic, in which case we continue. */ 54582115Sjhb if (panicstr == NULL) { 54682115Sjhb#ifdef SMP 547101155Sjhb atomic_store_rel_int(&panic_cpu, NOCPU); 54817658Sjulian#endif 54982115Sjhb return; 55082115Sjhb } 55182115Sjhb#endif 55282223Sjhb#endif 553113633Sjhb mtx_lock_spin(&sched_lock); 554100209Sgallatin td->td_flags |= TDF_INPANIC; 555113633Sjhb mtx_unlock_spin(&sched_lock); 55685202Speter if (!sync_on_panic) 55785202Speter bootopt |= RB_NOSYNC; 55817658Sjulian boot(bootopt); 55917658Sjulian} 56017658Sjulian 56117768Sjulian/* 56243436Smsmith * Support for poweroff delay. 56343436Smsmith */ 56454248Smsmith#ifndef POWEROFF_DELAY 56554248Smsmith# define POWEROFF_DELAY 5000 56654248Smsmith#endif 56754248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 56854248Smsmith 56943436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 57043436Smsmith &poweroff_delay, 0, ""); 57143436Smsmith 572110859Salfredstatic void 57350107Smsmithpoweroff_wait(void *junk, int howto) 57443436Smsmith{ 575110859Salfred 576110859Salfred if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 57743436Smsmith return; 57843436Smsmith DELAY(poweroff_delay * 1000); 57943436Smsmith} 58055539Sluoqi 58155539Sluoqi/* 58255539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 58355539Sluoqi * points in their main loops prior to a system shutdown, so that they 58455539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 58555539Sluoqi * to cause sync to fail). For each of these system processes, register 58655539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 58755539Sluoqi */ 58855539Sluoqistatic int kproc_shutdown_wait = 60; 58955539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 59055539Sluoqi &kproc_shutdown_wait, 0, ""); 59155539Sluoqi 59255539Sluoqivoid 59370063Sjhbkproc_shutdown(void *arg, int howto) 59455539Sluoqi{ 59555539Sluoqi struct proc *p; 596132177Salfred char procname[MAXCOMLEN + 1]; 59755539Sluoqi int error; 59855539Sluoqi 59955539Sluoqi if (panicstr) 60055539Sluoqi return; 60155539Sluoqi 60255539Sluoqi p = (struct proc *)arg; 603132177Salfred strlcpy(procname, p->p_comm, sizeof(procname)); 604132866Snjl printf("Waiting (max %d seconds) for system process `%s' to stop...", 605132177Salfred kproc_shutdown_wait, procname); 60670063Sjhb error = kthread_suspend(p, kproc_shutdown_wait * hz); 60755539Sluoqi 60855539Sluoqi if (error == EWOULDBLOCK) 609132866Snjl printf("timed out\n"); 61055539Sluoqi else 611132866Snjl printf("done\n"); 61255539Sluoqi} 61393496Sphk 61493496Sphk/* Registration of dumpers */ 61593496Sphkint 61693496Sphkset_dumper(struct dumperinfo *di) 61793496Sphk{ 618110859Salfred 61993496Sphk if (di == NULL) { 62093496Sphk bzero(&dumper, sizeof dumper); 62193496Sphk return (0); 62293496Sphk } 62393496Sphk if (dumper.dumper != NULL) 62493496Sphk return (EBUSY); 62593496Sphk dumper = *di; 62693496Sphk return (0); 62793496Sphk} 62893496Sphk 629105531Stmm#if defined(__powerpc__) 63093496Sphkvoid 63193496Sphkdumpsys(struct dumperinfo *di __unused) 63293496Sphk{ 63393496Sphk 63493496Sphk printf("Kernel dumps not implemented on this architecture\n"); 63593496Sphk} 63693496Sphk#endif 637