1ad4240feSJulian Elischer /*- 2ad4240feSJulian Elischer * Copyright (c) 1986, 1988, 1991, 1993 3ad4240feSJulian Elischer * The Regents of the University of California. All rights reserved. 4ad4240feSJulian Elischer * (c) UNIX System Laboratories, Inc. 5ad4240feSJulian Elischer * All or some portions of this file are derived from material licensed 6ad4240feSJulian Elischer * to the University of California by American Telephone and Telegraph 7ad4240feSJulian Elischer * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8ad4240feSJulian Elischer * the permission of UNIX System Laboratories, Inc. 9ad4240feSJulian Elischer * 10ad4240feSJulian Elischer * Redistribution and use in source and binary forms, with or without 11ad4240feSJulian Elischer * modification, are permitted provided that the following conditions 12ad4240feSJulian Elischer * are met: 13ad4240feSJulian Elischer * 1. Redistributions of source code must retain the above copyright 14ad4240feSJulian Elischer * notice, this list of conditions and the following disclaimer. 15ad4240feSJulian Elischer * 2. Redistributions in binary form must reproduce the above copyright 16ad4240feSJulian Elischer * notice, this list of conditions and the following disclaimer in the 17ad4240feSJulian Elischer * documentation and/or other materials provided with the distribution. 18ad4240feSJulian Elischer * 4. Neither the name of the University nor the names of its contributors 19ad4240feSJulian Elischer * may be used to endorse or promote products derived from this software 20ad4240feSJulian Elischer * without specific prior written permission. 21ad4240feSJulian Elischer * 22ad4240feSJulian Elischer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23ad4240feSJulian Elischer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24ad4240feSJulian Elischer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25ad4240feSJulian Elischer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26ad4240feSJulian Elischer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27ad4240feSJulian Elischer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28ad4240feSJulian Elischer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29ad4240feSJulian Elischer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30ad4240feSJulian Elischer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31ad4240feSJulian Elischer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32ad4240feSJulian Elischer * SUCH DAMAGE. 33ad4240feSJulian Elischer * 34ad4240feSJulian Elischer * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 35ad4240feSJulian Elischer */ 36ad4240feSJulian Elischer 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 40618c7db3SRobert Watson #include "opt_ddb.h" 412d50560aSMarcel Moolenaar #include "opt_kdb.h" 426d58e6cbSBruce Evans #include "opt_panic.h" 436d58e6cbSBruce Evans #include "opt_show_busybufs.h" 449923b511SScott Long #include "opt_sched.h" 45*2be767e0SAttilio Rao #include "opt_watchdog.h" 46ad4240feSJulian Elischer 47ad4240feSJulian Elischer #include <sys/param.h> 48ad4240feSJulian Elischer #include <sys/systm.h> 499626b608SPoul-Henning Kamp #include <sys/bio.h> 50fc8f7066SBruce Evans #include <sys/buf.h> 511d79f1bbSJohn Baldwin #include <sys/conf.h> 521d79f1bbSJohn Baldwin #include <sys/cons.h> 531d79f1bbSJohn Baldwin #include <sys/eventhandler.h> 5476ca6f88SJamie Gritton #include <sys/jail.h> 552d50560aSMarcel Moolenaar #include <sys/kdb.h> 56ad4240feSJulian Elischer #include <sys/kernel.h> 57e6592ee5SPeter Wemm #include <sys/kerneldump.h> 585e950839SLuoqi Chen #include <sys/kthread.h> 59dcd7d9b7SMaxim Sobolev #include <sys/malloc.h> 60ac0ad63fSBruce Evans #include <sys/mount.h> 61acd3428bSRobert Watson #include <sys/priv.h> 621d79f1bbSJohn Baldwin #include <sys/proc.h> 631d79f1bbSJohn Baldwin #include <sys/reboot.h> 641d79f1bbSJohn Baldwin #include <sys/resourcevar.h> 6520e25d7dSPeter Wemm #include <sys/sched.h> 66248bb937SAttilio Rao #include <sys/smp.h> 67ad4240feSJulian Elischer #include <sys/sysctl.h> 68ad4240feSJulian Elischer #include <sys/sysproto.h> 69*2be767e0SAttilio Rao #ifdef SW_WATCHDOG 70*2be767e0SAttilio Rao #include <sys/watchdog.h> 71*2be767e0SAttilio Rao #endif 72ad4240feSJulian Elischer 73618c7db3SRobert Watson #include <ddb/ddb.h> 74618c7db3SRobert Watson 7526502503SMarcel Moolenaar #include <machine/cpu.h> 76d39e457bSPoul-Henning Kamp #include <machine/pcb.h> 77752dff3dSJake Burkholder #include <machine/smp.h> 78ad4240feSJulian Elischer 79aed55708SRobert Watson #include <security/mac/mac_framework.h> 80aed55708SRobert Watson 810909f38aSPawel Jakub Dawidek #include <vm/vm.h> 820909f38aSPawel Jakub Dawidek #include <vm/vm_object.h> 830909f38aSPawel Jakub Dawidek #include <vm/vm_page.h> 840909f38aSPawel Jakub Dawidek #include <vm/vm_pager.h> 850909f38aSPawel Jakub Dawidek #include <vm/swap_pager.h> 860909f38aSPawel Jakub Dawidek 87ad4240feSJulian Elischer #include <sys/signalvar.h> 88ad4240feSJulian Elischer 89ad4240feSJulian Elischer #ifndef PANIC_REBOOT_WAIT_TIME 90ad4240feSJulian Elischer #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 91ad4240feSJulian Elischer #endif 92ad4240feSJulian Elischer 93ad4240feSJulian Elischer /* 94ad4240feSJulian Elischer * Note that stdarg.h and the ANSI style va_start macro is used for both 95ad4240feSJulian Elischer * ANSI and traditional C compilers. 96ad4240feSJulian Elischer */ 97ad4240feSJulian Elischer #include <machine/stdarg.h> 98ad4240feSJulian Elischer 992d50560aSMarcel Moolenaar #ifdef KDB 1002d50560aSMarcel Moolenaar #ifdef KDB_UNATTENDED 1019959b1a8SMike Smith int debugger_on_panic = 0; 102ad4240feSJulian Elischer #else 1039959b1a8SMike Smith int debugger_on_panic = 1; 104ad4240feSJulian Elischer #endif 10508a9c205SAndriy Gapon SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW | CTLFLAG_TUN, 1063d177f46SBill Fumerola &debugger_on_panic, 0, "Run debugger on kernel panic"); 10708a9c205SAndriy Gapon TUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic); 108e485b64bSJohn Baldwin 1092d50560aSMarcel Moolenaar #ifdef KDB_TRACE 11008a9c205SAndriy Gapon static int trace_on_panic = 1; 111e485b64bSJohn Baldwin #else 11208a9c205SAndriy Gapon static int trace_on_panic = 0; 113e485b64bSJohn Baldwin #endif 11408a9c205SAndriy Gapon SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW | CTLFLAG_TUN, 115e485b64bSJohn Baldwin &trace_on_panic, 0, "Print stack trace on kernel panic"); 11608a9c205SAndriy Gapon TUNABLE_INT("debug.trace_on_panic", &trace_on_panic); 1172d50560aSMarcel Moolenaar #endif /* KDB */ 118ad4240feSJulian Elischer 11908a9c205SAndriy Gapon static int sync_on_panic = 0; 12008a9c205SAndriy Gapon SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN, 121259ed917SPeter Wemm &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 12208a9c205SAndriy Gapon TUNABLE_INT("kern.sync_on_panic", &sync_on_panic); 123259ed917SPeter Wemm 124db82a982SMike Smith SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 125db82a982SMike Smith 1265230cfd2SJulian Elischer /* 127ad4240feSJulian Elischer * Variable panicstr contains argument to first call to panic; used as flag 128ad4240feSJulian Elischer * to indicate that the kernel has already called panic. 129ad4240feSJulian Elischer */ 130ad4240feSJulian Elischer const char *panicstr; 131ad4240feSJulian Elischer 13216a011f9SPaul Saab int dumping; /* system is dumping */ 13336a52c3cSJeff Roberson int rebooting; /* system is rebooting */ 13481661c94SPoul-Henning Kamp static struct dumperinfo dumper; /* our selected dumper */ 1352d50560aSMarcel Moolenaar 1362d50560aSMarcel Moolenaar /* Context information for dump-debuggers. */ 1372d50560aSMarcel Moolenaar static struct pcb dumppcb; /* Registers. */ 1382d50560aSMarcel Moolenaar static lwpid_t dumptid; /* Thread ID. */ 13916a011f9SPaul Saab 14082acbcf5SPeter Wemm static void poweroff_wait(void *, int); 14182acbcf5SPeter Wemm static void shutdown_halt(void *junk, int howto); 14282acbcf5SPeter Wemm static void shutdown_panic(void *junk, int howto); 14382acbcf5SPeter Wemm static void shutdown_reset(void *junk, int howto); 144f06a54f0SPoul-Henning Kamp 145fcb893a8SMike Smith /* register various local shutdown events */ 146fcb893a8SMike Smith static void 147fcb893a8SMike Smith shutdown_conf(void *unused) 148fcb893a8SMike Smith { 149e95499bdSAlfred Perlstein 150e95499bdSAlfred Perlstein EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 151fd104c15SRebecca Cran SHUTDOWN_PRI_FIRST); 152e95499bdSAlfred Perlstein EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 153e95499bdSAlfred Perlstein SHUTDOWN_PRI_LAST + 100); 154e95499bdSAlfred Perlstein EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 155e95499bdSAlfred Perlstein SHUTDOWN_PRI_LAST + 100); 156e95499bdSAlfred Perlstein EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 157e95499bdSAlfred Perlstein SHUTDOWN_PRI_LAST + 200); 158fcb893a8SMike Smith } 159ad4240feSJulian Elischer 160237fdd78SRobert Watson SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL); 161fcb893a8SMike Smith 162ad4240feSJulian Elischer /* 1630c14ff0eSRobert Watson * The system call that results in a reboot. 164ad4240feSJulian Elischer */ 165835a82eeSMatthew Dillon /* ARGSUSED */ 166ad4240feSJulian Elischer int 167b40ce416SJulian Elischer reboot(struct thread *td, struct reboot_args *uap) 168ad4240feSJulian Elischer { 169ad4240feSJulian Elischer int error; 170ad4240feSJulian Elischer 171a2ecb9b7SRobert Watson error = 0; 172a2ecb9b7SRobert Watson #ifdef MAC 17330d239bcSRobert Watson error = mac_system_check_reboot(td->td_ucred, uap->opt); 174a2ecb9b7SRobert Watson #endif 175a2ecb9b7SRobert Watson if (error == 0) 176acd3428bSRobert Watson error = priv_check(td, PRIV_REBOOT); 177a2ecb9b7SRobert Watson if (error == 0) { 178835a82eeSMatthew Dillon mtx_lock(&Giant); 17976e18b25SMarcel Moolenaar kern_reboot(uap->opt); 180835a82eeSMatthew Dillon mtx_unlock(&Giant); 181a2ecb9b7SRobert Watson } 182835a82eeSMatthew Dillon return (error); 183ad4240feSJulian Elischer } 184ad4240feSJulian Elischer 185ad4240feSJulian Elischer /* 186ad4240feSJulian Elischer * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 187ad4240feSJulian Elischer */ 1883e755f76SMike Smith static int shutdown_howto = 0; 1893e755f76SMike Smith 190ad4240feSJulian Elischer void 1913e755f76SMike Smith shutdown_nice(int howto) 192ad4240feSJulian Elischer { 193e95499bdSAlfred Perlstein 1943e755f76SMike Smith shutdown_howto = howto; 1953e755f76SMike Smith 196ad4240feSJulian Elischer /* Send a signal to init(8) and have it shutdown the world */ 197ad4240feSJulian Elischer if (initproc != NULL) { 19887729a2bSJohn Baldwin PROC_LOCK(initproc); 199ad4240feSJulian Elischer psignal(initproc, SIGINT); 20087729a2bSJohn Baldwin PROC_UNLOCK(initproc); 201ad4240feSJulian Elischer } else { 202ad4240feSJulian Elischer /* No init(8) running, so simply reboot */ 20376e18b25SMarcel Moolenaar kern_reboot(RB_NOSYNC); 204ad4240feSJulian Elischer } 205ad4240feSJulian Elischer return; 206ad4240feSJulian Elischer } 207ad4240feSJulian Elischer static int waittime = -1; 208ad4240feSJulian Elischer 20972dfe7a3SPoul-Henning Kamp static void 21082acbcf5SPeter Wemm print_uptime(void) 21172dfe7a3SPoul-Henning Kamp { 21272dfe7a3SPoul-Henning Kamp int f; 21372dfe7a3SPoul-Henning Kamp struct timespec ts; 21472dfe7a3SPoul-Henning Kamp 21572dfe7a3SPoul-Henning Kamp getnanouptime(&ts); 21672dfe7a3SPoul-Henning Kamp printf("Uptime: "); 21772dfe7a3SPoul-Henning Kamp f = 0; 21872dfe7a3SPoul-Henning Kamp if (ts.tv_sec >= 86400) { 2194a6404dfSJohn Baldwin printf("%ldd", (long)ts.tv_sec / 86400); 22072dfe7a3SPoul-Henning Kamp ts.tv_sec %= 86400; 22172dfe7a3SPoul-Henning Kamp f = 1; 22272dfe7a3SPoul-Henning Kamp } 22372dfe7a3SPoul-Henning Kamp if (f || ts.tv_sec >= 3600) { 2244a6404dfSJohn Baldwin printf("%ldh", (long)ts.tv_sec / 3600); 22572dfe7a3SPoul-Henning Kamp ts.tv_sec %= 3600; 22672dfe7a3SPoul-Henning Kamp f = 1; 22772dfe7a3SPoul-Henning Kamp } 22872dfe7a3SPoul-Henning Kamp if (f || ts.tv_sec >= 60) { 2294a6404dfSJohn Baldwin printf("%ldm", (long)ts.tv_sec / 60); 23072dfe7a3SPoul-Henning Kamp ts.tv_sec %= 60; 23172dfe7a3SPoul-Henning Kamp f = 1; 23272dfe7a3SPoul-Henning Kamp } 2334a6404dfSJohn Baldwin printf("%lds\n", (long)ts.tv_sec); 23472dfe7a3SPoul-Henning Kamp } 23572dfe7a3SPoul-Henning Kamp 236d39e457bSPoul-Henning Kamp static void 237d39e457bSPoul-Henning Kamp doadump(void) 238d39e457bSPoul-Henning Kamp { 239e95499bdSAlfred Perlstein 240f6449d9dSJulian Elischer /* 241f6449d9dSJulian Elischer * Sometimes people have to call this from the kernel debugger. 242f6449d9dSJulian Elischer * (if 'panic' can not dump) 243f6449d9dSJulian Elischer * Give them a clue as to why they can't dump. 244f6449d9dSJulian Elischer */ 245f6449d9dSJulian Elischer if (dumper.dumper == NULL) { 24627d68f90SDavid Malone printf("Cannot dump. Device not defined or unavailable.\n"); 247f6449d9dSJulian Elischer return; 248f6449d9dSJulian Elischer } 249f6449d9dSJulian Elischer 250d39e457bSPoul-Henning Kamp savectx(&dumppcb); 2512d50560aSMarcel Moolenaar dumptid = curthread->td_tid; 252d39e457bSPoul-Henning Kamp dumping++; 253618c7db3SRobert Watson #ifdef DDB 254618c7db3SRobert Watson if (textdump_pending) 255618c7db3SRobert Watson textdump_dumpsys(&dumper); 256618c7db3SRobert Watson else 257618c7db3SRobert Watson #endif 258d39e457bSPoul-Henning Kamp dumpsys(&dumper); 2599e473363SRuslan Ermilov dumping--; 260d39e457bSPoul-Henning Kamp } 261d39e457bSPoul-Henning Kamp 262d07f87a2SDon Lewis static int 263d07f87a2SDon Lewis isbufbusy(struct buf *bp) 264d07f87a2SDon Lewis { 265d07f87a2SDon Lewis if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 && 266d638e093SAttilio Rao BUF_ISLOCKED(bp)) || 267d07f87a2SDon Lewis ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI)) 268d07f87a2SDon Lewis return (1); 269d07f87a2SDon Lewis return (0); 270d07f87a2SDon Lewis } 271d07f87a2SDon Lewis 272ad4240feSJulian Elischer /* 27370ce93f4SNate Lawson * Shutdown the system cleanly to prepare for reboot, halt, or power off. 274ad4240feSJulian Elischer */ 27576e18b25SMarcel Moolenaar void 27676e18b25SMarcel Moolenaar kern_reboot(int howto) 277ad4240feSJulian Elischer { 278b6915bdbSDon Lewis static int first_buf_printf = 1; 279ad4240feSJulian Elischer 280f7ebc7ceSMarcel Moolenaar #if defined(SMP) 28170ce93f4SNate Lawson /* 28270ce93f4SNate Lawson * Bind us to CPU 0 so that all shutdown code runs there. Some 28370ce93f4SNate Lawson * systems don't shutdown properly (i.e., ACPI power off) if we 28470ce93f4SNate Lawson * run on another processor. 28570ce93f4SNate Lawson */ 286982d11f8SJeff Roberson thread_lock(curthread); 28720e25d7dSPeter Wemm sched_bind(curthread, 0); 288982d11f8SJeff Roberson thread_unlock(curthread); 28976e18b25SMarcel Moolenaar KASSERT(PCPU_GET(cpuid) == 0, ("%s: not running on cpu 0", __func__)); 29020e25d7dSPeter Wemm #endif 29136a52c3cSJeff Roberson /* We're in the process of rebooting. */ 29236a52c3cSJeff Roberson rebooting = 1; 29320e25d7dSPeter Wemm 2943e755f76SMike Smith /* collect extra flags that shutdown_nice might have set */ 2953e755f76SMike Smith howto |= shutdown_howto; 2963e755f76SMike Smith 29761e96500SJohn Baldwin /* We are out of the debugger now. */ 2982d50560aSMarcel Moolenaar kdb_active = 0; 29961e96500SJohn Baldwin 3005230cfd2SJulian Elischer /* 3015230cfd2SJulian Elischer * Do any callouts that should be done BEFORE syncing the filesystems. 3025230cfd2SJulian Elischer */ 303fcb893a8SMike Smith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 3045230cfd2SJulian Elischer 3055230cfd2SJulian Elischer /* 3065230cfd2SJulian Elischer * Now sync filesystems 3075230cfd2SJulian Elischer */ 308ad4240feSJulian Elischer if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 309ad4240feSJulian Elischer register struct buf *bp; 31062820f25SJason Evans int iter, nbusy, pbusy; 3110c0b25aeSJohn Baldwin #ifndef PREEMPTION 31262820f25SJason Evans int subiter; 3130c0b25aeSJohn Baldwin #endif 314ad4240feSJulian Elischer 315ad4240feSJulian Elischer waittime = 0; 316ad4240feSJulian Elischer 317*2be767e0SAttilio Rao #ifdef SW_WATCHDOG 318*2be767e0SAttilio Rao wdog_kern_pat(WD_LASTVAL); 319*2be767e0SAttilio Rao #endif 3203fafa27bSStephan Uphoff sync(curthread, NULL); 321ad4240feSJulian Elischer 322b1897c19SJulian Elischer /* 323b1897c19SJulian Elischer * With soft updates, some buffers that are 324b1897c19SJulian Elischer * written will be remarked as dirty until other 325b1897c19SJulian Elischer * buffers are written. 326b1897c19SJulian Elischer */ 32762820f25SJason Evans for (iter = pbusy = 0; iter < 20; iter++) { 328ad4240feSJulian Elischer nbusy = 0; 329d07f87a2SDon Lewis for (bp = &buf[nbuf]; --bp >= buf; ) 330d07f87a2SDon Lewis if (isbufbusy(bp)) 331ad4240feSJulian Elischer nbusy++; 332b6915bdbSDon Lewis if (nbusy == 0) { 333b6915bdbSDon Lewis if (first_buf_printf) 33437abb77fSPoul-Henning Kamp printf("All buffers synced."); 335ad4240feSJulian Elischer break; 336b6915bdbSDon Lewis } 337b6915bdbSDon Lewis if (first_buf_printf) { 338b6915bdbSDon Lewis printf("Syncing disks, buffers remaining... "); 339b6915bdbSDon Lewis first_buf_printf = 0; 340b6915bdbSDon Lewis } 341ad4240feSJulian Elischer printf("%d ", nbusy); 34262820f25SJason Evans if (nbusy < pbusy) 34362820f25SJason Evans iter = 0; 34462820f25SJason Evans pbusy = nbusy; 345*2be767e0SAttilio Rao #ifdef SW_WATCHDOG 346*2be767e0SAttilio Rao wdog_kern_pat(WD_LASTVAL); 347*2be767e0SAttilio Rao #endif 3483fafa27bSStephan Uphoff sync(curthread, NULL); 3490c0b25aeSJohn Baldwin 3500c0b25aeSJohn Baldwin #ifdef PREEMPTION 3510c0b25aeSJohn Baldwin /* 3520c0b25aeSJohn Baldwin * Drop Giant and spin for a while to allow 3530c0b25aeSJohn Baldwin * interrupt threads to run. 3540c0b25aeSJohn Baldwin */ 3550c0b25aeSJohn Baldwin DROP_GIANT(); 3560c0b25aeSJohn Baldwin DELAY(50000 * iter); 3570c0b25aeSJohn Baldwin PICKUP_GIANT(); 3580c0b25aeSJohn Baldwin #else 3590c0b25aeSJohn Baldwin /* 3600c0b25aeSJohn Baldwin * Drop Giant and context switch several times to 3610c0b25aeSJohn Baldwin * allow interrupt threads to run. 3620c0b25aeSJohn Baldwin */ 363c86b6ff5SJohn Baldwin DROP_GIANT(); 36462820f25SJason Evans for (subiter = 0; subiter < 50 * iter; subiter++) { 365982d11f8SJeff Roberson thread_lock(curthread); 366bf0acc27SJohn Baldwin mi_switch(SW_VOL, NULL); 367982d11f8SJeff Roberson thread_unlock(curthread); 36862820f25SJason Evans DELAY(1000); 36962820f25SJason Evans } 37020cdcc5bSJohn Baldwin PICKUP_GIANT(); 3710c0b25aeSJohn Baldwin #endif 372ad4240feSJulian Elischer } 373c8c216d5SNate Lawson printf("\n"); 374d02d6d04SMike Smith /* 375d02d6d04SMike Smith * Count only busy local buffers to prevent forcing 376d02d6d04SMike Smith * a fsck if we're just a client of a wedged NFS server 377d02d6d04SMike Smith */ 378d02d6d04SMike Smith nbusy = 0; 379d02d6d04SMike Smith for (bp = &buf[nbuf]; --bp >= buf; ) { 380d07f87a2SDon Lewis if (isbufbusy(bp)) { 381c5690651SPoul-Henning Kamp #if 0 382c5690651SPoul-Henning Kamp /* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */ 383f3732fd1SPoul-Henning Kamp if (bp->b_dev == NULL) { 3840429e37aSPoul-Henning Kamp TAILQ_REMOVE(&mountlist, 38567812eacSKirk McKusick bp->b_vp->v_mount, mnt_list); 3869c111b31SPoul-Henning Kamp continue; 387dfd5dee1SPeter Wemm } 388c5690651SPoul-Henning Kamp #endif 3899c111b31SPoul-Henning Kamp nbusy++; 3909c111b31SPoul-Henning Kamp #if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 3919c111b31SPoul-Henning Kamp printf( 392c5690651SPoul-Henning Kamp "%d: bufobj:%p, flags:%0x, blkno:%ld, lblkno:%ld\n", 393c5690651SPoul-Henning Kamp nbusy, bp->b_bufobj, 3949c111b31SPoul-Henning Kamp bp->b_flags, (long)bp->b_blkno, 3959c111b31SPoul-Henning Kamp (long)bp->b_lblkno); 3969c111b31SPoul-Henning Kamp #endif 3979c111b31SPoul-Henning Kamp } 398d02d6d04SMike Smith } 399ad4240feSJulian Elischer if (nbusy) { 400ad4240feSJulian Elischer /* 401ad4240feSJulian Elischer * Failed to sync all blocks. Indicate this and don't 402ad4240feSJulian Elischer * unmount filesystems (thus forcing an fsck on reboot). 403ad4240feSJulian Elischer */ 404b6915bdbSDon Lewis printf("Giving up on %d buffers\n", nbusy); 405ad4240feSJulian Elischer DELAY(5000000); /* 5 seconds */ 406ad4240feSJulian Elischer } else { 407b6915bdbSDon Lewis if (!first_buf_printf) 408b6915bdbSDon Lewis printf("Final sync complete\n"); 409ad4240feSJulian Elischer /* 410ad4240feSJulian Elischer * Unmount filesystems 411ad4240feSJulian Elischer */ 412ad4240feSJulian Elischer if (panicstr == 0) 413ad4240feSJulian Elischer vfs_unmountall(); 414ad4240feSJulian Elischer } 4150909f38aSPawel Jakub Dawidek swapoff_all(); 416ad4240feSJulian Elischer DELAY(100000); /* wait for console output to finish */ 417ad4240feSJulian Elischer } 4185230cfd2SJulian Elischer 41972dfe7a3SPoul-Henning Kamp print_uptime(); 42072dfe7a3SPoul-Henning Kamp 4215230cfd2SJulian Elischer /* 4225230cfd2SJulian Elischer * Ok, now do things that assume all filesystem activity has 4235230cfd2SJulian Elischer * been completed. 4245230cfd2SJulian Elischer */ 425fcb893a8SMike Smith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 42670ce93f4SNate Lawson 427f6449d9dSJulian Elischer if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 428d39e457bSPoul-Henning Kamp doadump(); 4292cfa0a03SJustin T. Gibbs 4302cfa0a03SJustin T. Gibbs /* Now that we're going to really halt the system... */ 431fcb893a8SMike Smith EVENTHANDLER_INVOKE(shutdown_final, howto); 4322cfa0a03SJustin T. Gibbs 433fcb893a8SMike Smith for(;;) ; /* safety against shutdown_reset not working */ 434fcb893a8SMike Smith /* NOTREACHED */ 435fcb893a8SMike Smith } 436fcb893a8SMike Smith 437fcb893a8SMike Smith /* 438fcb893a8SMike Smith * If the shutdown was a clean halt, behave accordingly. 439fcb893a8SMike Smith */ 440fcb893a8SMike Smith static void 441fcb893a8SMike Smith shutdown_halt(void *junk, int howto) 442fcb893a8SMike Smith { 443e95499bdSAlfred Perlstein 444ad4240feSJulian Elischer if (howto & RB_HALT) { 445ad4240feSJulian Elischer printf("\n"); 446ad4240feSJulian Elischer printf("The operating system has halted.\n"); 447ad4240feSJulian Elischer printf("Please press any key to reboot.\n\n"); 448d13d3630SJulian Elischer switch (cngetc()) { 449d13d3630SJulian Elischer case -1: /* No console, just die */ 450d13d3630SJulian Elischer cpu_halt(); 451d13d3630SJulian Elischer /* NOTREACHED */ 452d13d3630SJulian Elischer default: 4532cfa0a03SJustin T. Gibbs howto &= ~RB_HALT; 454d13d3630SJulian Elischer break; 455d13d3630SJulian Elischer } 456fcb893a8SMike Smith } 457fcb893a8SMike Smith } 458ad4240feSJulian Elischer 459fcb893a8SMike Smith /* 460fcb893a8SMike Smith * Check to see if the system paniced, pause and then reboot 461fcb893a8SMike Smith * according to the specified delay. 462fcb893a8SMike Smith */ 463fcb893a8SMike Smith static void 464fcb893a8SMike Smith shutdown_panic(void *junk, int howto) 465fcb893a8SMike Smith { 466fcb893a8SMike Smith int loop; 467fcb893a8SMike Smith 468fcb893a8SMike Smith if (howto & RB_DUMP) { 469ad4240feSJulian Elischer if (PANIC_REBOOT_WAIT_TIME != 0) { 470ad4240feSJulian Elischer if (PANIC_REBOOT_WAIT_TIME != -1) { 4712cfa0a03SJustin T. Gibbs printf("Automatic reboot in %d seconds - " 4722cfa0a03SJustin T. Gibbs "press a key on the console to abort\n", 473ad4240feSJulian Elischer PANIC_REBOOT_WAIT_TIME); 4742cfa0a03SJustin T. Gibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 4752cfa0a03SJustin T. Gibbs loop > 0; --loop) { 476ad4240feSJulian Elischer DELAY(1000 * 100); /* 1/10th second */ 477a7f8f2abSBruce Evans /* Did user type a key? */ 478a7f8f2abSBruce Evans if (cncheckc() != -1) 479ad4240feSJulian Elischer break; 480ad4240feSJulian Elischer } 481ad4240feSJulian Elischer if (!loop) 482fcb893a8SMike Smith return; 483ad4240feSJulian Elischer } 484ad4240feSJulian Elischer } else { /* zero time specified - reboot NOW */ 485fcb893a8SMike Smith return; 486ad4240feSJulian Elischer } 487422702e9SNik Clayton printf("--> Press a key on the console to reboot,\n"); 488422702e9SNik Clayton printf("--> or switch off the system now.\n"); 489ad4240feSJulian Elischer cngetc(); 490ad4240feSJulian Elischer } 491fcb893a8SMike Smith } 492fcb893a8SMike Smith 493fcb893a8SMike Smith /* 494fcb893a8SMike Smith * Everything done, now reset 495fcb893a8SMike Smith */ 496fcb893a8SMike Smith static void 497fcb893a8SMike Smith shutdown_reset(void *junk, int howto) 498fcb893a8SMike Smith { 499e95499bdSAlfred Perlstein 500ad4240feSJulian Elischer printf("Rebooting...\n"); 501ad4240feSJulian Elischer DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 502248bb937SAttilio Rao 503248bb937SAttilio Rao /* 504248bb937SAttilio Rao * Acquiring smp_ipi_mtx here has a double effect: 505248bb937SAttilio Rao * - it disables interrupts avoiding CPU0 preemption 506248bb937SAttilio Rao * by fast handlers (thus deadlocking against other CPUs) 507248bb937SAttilio Rao * - it avoids deadlocks against smp_rendezvous() or, more 508248bb937SAttilio Rao * generally, threads busy-waiting, with this spinlock held, 509248bb937SAttilio Rao * and waiting for responses by threads on other CPUs 510248bb937SAttilio Rao * (ie. smp_tlb_shootdown()). 5110a2d5feaSAttilio Rao * 5120a2d5feaSAttilio Rao * For the !SMP case it just needs to handle the former problem. 513248bb937SAttilio Rao */ 5140a2d5feaSAttilio Rao #ifdef SMP 515248bb937SAttilio Rao mtx_lock_spin(&smp_ipi_mtx); 5160a2d5feaSAttilio Rao #else 5170a2d5feaSAttilio Rao spinlock_enter(); 5180a2d5feaSAttilio Rao #endif 519248bb937SAttilio Rao 520269fb9d7SJulian Elischer /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 521ad4240feSJulian Elischer cpu_reset(); 522fcb893a8SMike Smith /* NOTREACHED */ /* assuming reset worked */ 523ad4240feSJulian Elischer } 524ad4240feSJulian Elischer 525ad4240feSJulian Elischer /* 526ad4240feSJulian Elischer * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 527ad4240feSJulian Elischer * and then reboots. If we are called twice, then we avoid trying to sync 528ad4240feSJulian Elischer * the disks as this often leads to recursive panics. 529ad4240feSJulian Elischer */ 530ad4240feSJulian Elischer void 5319a6dc4b6SPoul-Henning Kamp panic(const char *fmt, ...) 532ad4240feSJulian Elischer { 53364dd590eSAndriy Gapon #ifdef SMP 53464dd590eSAndriy Gapon static volatile u_int panic_cpu = NOCPU; 53564dd590eSAndriy Gapon #endif 536fe799533SAndrew Gallatin struct thread *td = curthread; 537e485b64bSJohn Baldwin int bootopt, newpanic; 538ad4240feSJulian Elischer va_list ap; 53999237364SAndrey A. Chernov static char buf[256]; 540ad4240feSJulian Elischer 54141a4e90eSKonstantin Belousov critical_enter(); 5420384fff8SJason Evans #ifdef SMP 5431a5333c3SJohn Baldwin /* 5441a5333c3SJohn Baldwin * We don't want multiple CPU's to panic at the same time, so we 5450711ca46SJohn Baldwin * use panic_cpu as a simple spinlock. We have to keep checking 5460711ca46SJohn Baldwin * panic_cpu if we are spinning in case the panic on the first 5471a5333c3SJohn Baldwin * CPU is canceled. 5481a5333c3SJohn Baldwin */ 5490711ca46SJohn Baldwin if (panic_cpu != PCPU_GET(cpuid)) 5500711ca46SJohn Baldwin while (atomic_cmpset_int(&panic_cpu, NOCPU, 5510711ca46SJohn Baldwin PCPU_GET(cpuid)) == 0) 5520711ca46SJohn Baldwin while (panic_cpu != NOCPU) 5530711ca46SJohn Baldwin ; /* nothing */ 5540384fff8SJason Evans #endif 5550384fff8SJason Evans 556ad4240feSJulian Elischer bootopt = RB_AUTOBOOT | RB_DUMP; 557e485b64bSJohn Baldwin newpanic = 0; 558ad4240feSJulian Elischer if (panicstr) 559ad4240feSJulian Elischer bootopt |= RB_NOSYNC; 560e485b64bSJohn Baldwin else { 561ad4240feSJulian Elischer panicstr = fmt; 562e485b64bSJohn Baldwin newpanic = 1; 563e485b64bSJohn Baldwin } 564ad4240feSJulian Elischer 565ad4240feSJulian Elischer va_start(ap, fmt); 5664f1b4577SIan Dowse if (newpanic) { 5672127f260SArchie Cobbs (void)vsnprintf(buf, sizeof(buf), fmt, ap); 56899237364SAndrey A. Chernov panicstr = buf; 5699a6dc4b6SPoul-Henning Kamp printf("panic: %s\n", buf); 5704f1b4577SIan Dowse } else { 5714f1b4577SIan Dowse printf("panic: "); 5724f1b4577SIan Dowse vprintf(fmt, ap); 5739a6dc4b6SPoul-Henning Kamp printf("\n"); 5744f1b4577SIan Dowse } 5754f1b4577SIan Dowse va_end(ap); 57647d81897SSteve Passe #ifdef SMP 57755c45354SJohn Baldwin printf("cpuid = %d\n", PCPU_GET(cpuid)); 5782bcc63c5SJohn Baldwin #endif 579ad4240feSJulian Elischer 5802d50560aSMarcel Moolenaar #ifdef KDB 581e485b64bSJohn Baldwin if (newpanic && trace_on_panic) 5822d50560aSMarcel Moolenaar kdb_backtrace(); 583ad4240feSJulian Elischer if (debugger_on_panic) 5843de213ccSRobert Watson kdb_enter(KDB_WHY_PANIC, "panic"); 5851432aa0cSJohn Baldwin #ifdef RESTARTABLE_PANICS 5861a5333c3SJohn Baldwin /* See if the user aborted the panic, in which case we continue. */ 5871a5333c3SJohn Baldwin if (panicstr == NULL) { 5881a5333c3SJohn Baldwin #ifdef SMP 5890711ca46SJohn Baldwin atomic_store_rel_int(&panic_cpu, NOCPU); 5901a5333c3SJohn Baldwin #endif 5911a5333c3SJohn Baldwin return; 5921a5333c3SJohn Baldwin } 593ad4240feSJulian Elischer #endif 5941432aa0cSJohn Baldwin #endif 595982d11f8SJeff Roberson /*thread_lock(td); */ 596fe799533SAndrew Gallatin td->td_flags |= TDF_INPANIC; 597982d11f8SJeff Roberson /* thread_unlock(td); */ 598259ed917SPeter Wemm if (!sync_on_panic) 599259ed917SPeter Wemm bootopt |= RB_NOSYNC; 60041a4e90eSKonstantin Belousov critical_exit(); 60176e18b25SMarcel Moolenaar kern_reboot(bootopt); 602ad4240feSJulian Elischer } 603ad4240feSJulian Elischer 604e0d898b4SJulian Elischer /* 605db82a982SMike Smith * Support for poweroff delay. 606b22692bdSNick Hibma * 607b22692bdSNick Hibma * Please note that setting this delay too short might power off your machine 608b22692bdSNick Hibma * before the write cache on your hard disk has been flushed, leading to 609b22692bdSNick Hibma * soft-updates inconsistencies. 610db82a982SMike Smith */ 6119eec6969SMike Smith #ifndef POWEROFF_DELAY 6129eec6969SMike Smith # define POWEROFF_DELAY 5000 6139eec6969SMike Smith #endif 6149eec6969SMike Smith static int poweroff_delay = POWEROFF_DELAY; 6159eec6969SMike Smith 616db82a982SMike Smith SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 617db82a982SMike Smith &poweroff_delay, 0, ""); 618db82a982SMike Smith 619fcb893a8SMike Smith static void 620fcb893a8SMike Smith poweroff_wait(void *junk, int howto) 621db82a982SMike Smith { 622e95499bdSAlfred Perlstein 623db82a982SMike Smith if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 624db82a982SMike Smith return; 625db82a982SMike Smith DELAY(poweroff_delay * 1000); 626db82a982SMike Smith } 6275e950839SLuoqi Chen 6285e950839SLuoqi Chen /* 6295e950839SLuoqi Chen * Some system processes (e.g. syncer) need to be stopped at appropriate 6305e950839SLuoqi Chen * points in their main loops prior to a system shutdown, so that they 6315e950839SLuoqi Chen * won't interfere with the shutdown process (e.g. by holding a disk buf 6325e950839SLuoqi Chen * to cause sync to fail). For each of these system processes, register 6335e950839SLuoqi Chen * shutdown_kproc() as a handler for one of shutdown events. 6345e950839SLuoqi Chen */ 6355e950839SLuoqi Chen static int kproc_shutdown_wait = 60; 6365e950839SLuoqi Chen SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 6375e950839SLuoqi Chen &kproc_shutdown_wait, 0, ""); 6385e950839SLuoqi Chen 6395e950839SLuoqi Chen void 640ffc831daSJohn Baldwin kproc_shutdown(void *arg, int howto) 6415e950839SLuoqi Chen { 6425e950839SLuoqi Chen struct proc *p; 6435e950839SLuoqi Chen int error; 6445e950839SLuoqi Chen 6455e950839SLuoqi Chen if (panicstr) 6465e950839SLuoqi Chen return; 6475e950839SLuoqi Chen 6485e950839SLuoqi Chen p = (struct proc *)arg; 649b1c81391SNate Lawson printf("Waiting (max %d seconds) for system process `%s' to stop...", 6504f9d48e4SJohn Baldwin kproc_shutdown_wait, p->p_comm); 6513745c395SJulian Elischer error = kproc_suspend(p, kproc_shutdown_wait * hz); 6525e950839SLuoqi Chen 6535e950839SLuoqi Chen if (error == EWOULDBLOCK) 654b1c81391SNate Lawson printf("timed out\n"); 6555e950839SLuoqi Chen else 656b1c81391SNate Lawson printf("done\n"); 6575e950839SLuoqi Chen } 65881661c94SPoul-Henning Kamp 6597ab24ea3SJulian Elischer void 6607ab24ea3SJulian Elischer kthread_shutdown(void *arg, int howto) 6617ab24ea3SJulian Elischer { 6627ab24ea3SJulian Elischer struct thread *td; 6637ab24ea3SJulian Elischer int error; 6647ab24ea3SJulian Elischer 6657ab24ea3SJulian Elischer if (panicstr) 6667ab24ea3SJulian Elischer return; 6677ab24ea3SJulian Elischer 6687ab24ea3SJulian Elischer td = (struct thread *)arg; 6697ab24ea3SJulian Elischer printf("Waiting (max %d seconds) for system thread `%s' to stop...", 6704f9d48e4SJohn Baldwin kproc_shutdown_wait, td->td_name); 6717ab24ea3SJulian Elischer error = kthread_suspend(td, kproc_shutdown_wait * hz); 6727ab24ea3SJulian Elischer 6737ab24ea3SJulian Elischer if (error == EWOULDBLOCK) 6747ab24ea3SJulian Elischer printf("timed out\n"); 6757ab24ea3SJulian Elischer else 6767ab24ea3SJulian Elischer printf("done\n"); 6777ab24ea3SJulian Elischer } 6787ab24ea3SJulian Elischer 67981661c94SPoul-Henning Kamp /* Registration of dumpers */ 68081661c94SPoul-Henning Kamp int 68181661c94SPoul-Henning Kamp set_dumper(struct dumperinfo *di) 68281661c94SPoul-Henning Kamp { 683e95499bdSAlfred Perlstein 68481661c94SPoul-Henning Kamp if (di == NULL) { 68581661c94SPoul-Henning Kamp bzero(&dumper, sizeof dumper); 68681661c94SPoul-Henning Kamp return (0); 68781661c94SPoul-Henning Kamp } 68881661c94SPoul-Henning Kamp if (dumper.dumper != NULL) 68981661c94SPoul-Henning Kamp return (EBUSY); 69081661c94SPoul-Henning Kamp dumper = *di; 69181661c94SPoul-Henning Kamp return (0); 69281661c94SPoul-Henning Kamp } 69381661c94SPoul-Henning Kamp 694007b1b7bSRuslan Ermilov /* Call dumper with bounds checking. */ 695007b1b7bSRuslan Ermilov int 696007b1b7bSRuslan Ermilov dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical, 697007b1b7bSRuslan Ermilov off_t offset, size_t length) 698007b1b7bSRuslan Ermilov { 699007b1b7bSRuslan Ermilov 700007b1b7bSRuslan Ermilov if (length != 0 && (offset < di->mediaoffset || 701007b1b7bSRuslan Ermilov offset - di->mediaoffset + length > di->mediasize)) { 702007b1b7bSRuslan Ermilov printf("Attempt to write outside dump device boundaries.\n"); 703007b1b7bSRuslan Ermilov return (ENXIO); 704007b1b7bSRuslan Ermilov } 705007b1b7bSRuslan Ermilov return (di->dumper(di->priv, virtual, physical, offset, length)); 706007b1b7bSRuslan Ermilov } 707007b1b7bSRuslan Ermilov 708e6592ee5SPeter Wemm void 709e6592ee5SPeter Wemm mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver, 710e6592ee5SPeter Wemm uint64_t dumplen, uint32_t blksz) 711e6592ee5SPeter Wemm { 712e6592ee5SPeter Wemm 713e6592ee5SPeter Wemm bzero(kdh, sizeof(*kdh)); 714e6592ee5SPeter Wemm strncpy(kdh->magic, magic, sizeof(kdh->magic)); 715e6592ee5SPeter Wemm strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); 716e6592ee5SPeter Wemm kdh->version = htod32(KERNELDUMPVERSION); 717e6592ee5SPeter Wemm kdh->architectureversion = htod32(archver); 718e6592ee5SPeter Wemm kdh->dumplength = htod64(dumplen); 719e6592ee5SPeter Wemm kdh->dumptime = htod64(time_second); 720e6592ee5SPeter Wemm kdh->blocksize = htod32(blksz); 721c1f19219SJamie Gritton strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname)); 722e6592ee5SPeter Wemm strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); 723e6592ee5SPeter Wemm if (panicstr != NULL) 724e6592ee5SPeter Wemm strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); 725e6592ee5SPeter Wemm kdh->parity = kerneldump_parity(kdh); 726e6592ee5SPeter Wemm } 727