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" 439923b511SScott Long #include "opt_sched.h" 442be767e0SAttilio Rao #include "opt_watchdog.h" 45ad4240feSJulian Elischer 46ad4240feSJulian Elischer #include <sys/param.h> 47ad4240feSJulian Elischer #include <sys/systm.h> 489626b608SPoul-Henning Kamp #include <sys/bio.h> 49fc8f7066SBruce Evans #include <sys/buf.h> 501d79f1bbSJohn Baldwin #include <sys/conf.h> 511d79f1bbSJohn Baldwin #include <sys/cons.h> 521d79f1bbSJohn Baldwin #include <sys/eventhandler.h> 5376ca6f88SJamie Gritton #include <sys/jail.h> 542d50560aSMarcel Moolenaar #include <sys/kdb.h> 55ad4240feSJulian Elischer #include <sys/kernel.h> 56e6592ee5SPeter Wemm #include <sys/kerneldump.h> 575e950839SLuoqi Chen #include <sys/kthread.h> 58dcd7d9b7SMaxim Sobolev #include <sys/malloc.h> 59ac0ad63fSBruce Evans #include <sys/mount.h> 60acd3428bSRobert Watson #include <sys/priv.h> 611d79f1bbSJohn Baldwin #include <sys/proc.h> 621d79f1bbSJohn Baldwin #include <sys/reboot.h> 631d79f1bbSJohn Baldwin #include <sys/resourcevar.h> 6420e25d7dSPeter Wemm #include <sys/sched.h> 65248bb937SAttilio Rao #include <sys/smp.h> 66ad4240feSJulian Elischer #include <sys/sysctl.h> 67ad4240feSJulian Elischer #include <sys/sysproto.h> 68*fa2b39a1SAttilio Rao #include <sys/vnode.h> 692be767e0SAttilio Rao #ifdef SW_WATCHDOG 702be767e0SAttilio Rao #include <sys/watchdog.h> 712be767e0SAttilio 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 126*fa2b39a1SAttilio Rao #ifndef DIAGNOSTIC 127*fa2b39a1SAttilio Rao static int show_busybufs; 128*fa2b39a1SAttilio Rao #else 129*fa2b39a1SAttilio Rao static int show_busybufs = 1; 130*fa2b39a1SAttilio Rao #endif 131*fa2b39a1SAttilio Rao SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW, 132*fa2b39a1SAttilio Rao &show_busybufs, 0, ""); 133*fa2b39a1SAttilio Rao 1345230cfd2SJulian Elischer /* 135ad4240feSJulian Elischer * Variable panicstr contains argument to first call to panic; used as flag 136ad4240feSJulian Elischer * to indicate that the kernel has already called panic. 137ad4240feSJulian Elischer */ 138ad4240feSJulian Elischer const char *panicstr; 139ad4240feSJulian Elischer 14016a011f9SPaul Saab int dumping; /* system is dumping */ 14136a52c3cSJeff Roberson int rebooting; /* system is rebooting */ 14281661c94SPoul-Henning Kamp static struct dumperinfo dumper; /* our selected dumper */ 1432d50560aSMarcel Moolenaar 1442d50560aSMarcel Moolenaar /* Context information for dump-debuggers. */ 1452d50560aSMarcel Moolenaar static struct pcb dumppcb; /* Registers. */ 1462d50560aSMarcel Moolenaar static lwpid_t dumptid; /* Thread ID. */ 14716a011f9SPaul Saab 14882acbcf5SPeter Wemm static void poweroff_wait(void *, int); 14982acbcf5SPeter Wemm static void shutdown_halt(void *junk, int howto); 15082acbcf5SPeter Wemm static void shutdown_panic(void *junk, int howto); 15182acbcf5SPeter Wemm static void shutdown_reset(void *junk, int howto); 152f06a54f0SPoul-Henning Kamp 153fcb893a8SMike Smith /* register various local shutdown events */ 154fcb893a8SMike Smith static void 155fcb893a8SMike Smith shutdown_conf(void *unused) 156fcb893a8SMike Smith { 157e95499bdSAlfred Perlstein 158e95499bdSAlfred Perlstein EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 159fd104c15SRebecca Cran SHUTDOWN_PRI_FIRST); 160e95499bdSAlfred Perlstein EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 161e95499bdSAlfred Perlstein SHUTDOWN_PRI_LAST + 100); 162e95499bdSAlfred Perlstein EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 163e95499bdSAlfred Perlstein SHUTDOWN_PRI_LAST + 100); 164e95499bdSAlfred Perlstein EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 165e95499bdSAlfred Perlstein SHUTDOWN_PRI_LAST + 200); 166fcb893a8SMike Smith } 167ad4240feSJulian Elischer 168237fdd78SRobert Watson SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL); 169fcb893a8SMike Smith 170ad4240feSJulian Elischer /* 1710c14ff0eSRobert Watson * The system call that results in a reboot. 172ad4240feSJulian Elischer */ 173835a82eeSMatthew Dillon /* ARGSUSED */ 174ad4240feSJulian Elischer int 175b40ce416SJulian Elischer reboot(struct thread *td, struct reboot_args *uap) 176ad4240feSJulian Elischer { 177ad4240feSJulian Elischer int error; 178ad4240feSJulian Elischer 179a2ecb9b7SRobert Watson error = 0; 180a2ecb9b7SRobert Watson #ifdef MAC 18130d239bcSRobert Watson error = mac_system_check_reboot(td->td_ucred, uap->opt); 182a2ecb9b7SRobert Watson #endif 183a2ecb9b7SRobert Watson if (error == 0) 184acd3428bSRobert Watson error = priv_check(td, PRIV_REBOOT); 185a2ecb9b7SRobert Watson if (error == 0) { 186835a82eeSMatthew Dillon mtx_lock(&Giant); 18776e18b25SMarcel Moolenaar kern_reboot(uap->opt); 188835a82eeSMatthew Dillon mtx_unlock(&Giant); 189a2ecb9b7SRobert Watson } 190835a82eeSMatthew Dillon return (error); 191ad4240feSJulian Elischer } 192ad4240feSJulian Elischer 193ad4240feSJulian Elischer /* 194ad4240feSJulian Elischer * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 195ad4240feSJulian Elischer */ 1963e755f76SMike Smith static int shutdown_howto = 0; 1973e755f76SMike Smith 198ad4240feSJulian Elischer void 1993e755f76SMike Smith shutdown_nice(int howto) 200ad4240feSJulian Elischer { 201e95499bdSAlfred Perlstein 2023e755f76SMike Smith shutdown_howto = howto; 2033e755f76SMike Smith 204ad4240feSJulian Elischer /* Send a signal to init(8) and have it shutdown the world */ 205ad4240feSJulian Elischer if (initproc != NULL) { 20687729a2bSJohn Baldwin PROC_LOCK(initproc); 207ad4240feSJulian Elischer psignal(initproc, SIGINT); 20887729a2bSJohn Baldwin PROC_UNLOCK(initproc); 209ad4240feSJulian Elischer } else { 210ad4240feSJulian Elischer /* No init(8) running, so simply reboot */ 21176e18b25SMarcel Moolenaar kern_reboot(RB_NOSYNC); 212ad4240feSJulian Elischer } 213ad4240feSJulian Elischer return; 214ad4240feSJulian Elischer } 215ad4240feSJulian Elischer static int waittime = -1; 216ad4240feSJulian Elischer 21772dfe7a3SPoul-Henning Kamp static void 21882acbcf5SPeter Wemm print_uptime(void) 21972dfe7a3SPoul-Henning Kamp { 22072dfe7a3SPoul-Henning Kamp int f; 22172dfe7a3SPoul-Henning Kamp struct timespec ts; 22272dfe7a3SPoul-Henning Kamp 22372dfe7a3SPoul-Henning Kamp getnanouptime(&ts); 22472dfe7a3SPoul-Henning Kamp printf("Uptime: "); 22572dfe7a3SPoul-Henning Kamp f = 0; 22672dfe7a3SPoul-Henning Kamp if (ts.tv_sec >= 86400) { 2274a6404dfSJohn Baldwin printf("%ldd", (long)ts.tv_sec / 86400); 22872dfe7a3SPoul-Henning Kamp ts.tv_sec %= 86400; 22972dfe7a3SPoul-Henning Kamp f = 1; 23072dfe7a3SPoul-Henning Kamp } 23172dfe7a3SPoul-Henning Kamp if (f || ts.tv_sec >= 3600) { 2324a6404dfSJohn Baldwin printf("%ldh", (long)ts.tv_sec / 3600); 23372dfe7a3SPoul-Henning Kamp ts.tv_sec %= 3600; 23472dfe7a3SPoul-Henning Kamp f = 1; 23572dfe7a3SPoul-Henning Kamp } 23672dfe7a3SPoul-Henning Kamp if (f || ts.tv_sec >= 60) { 2374a6404dfSJohn Baldwin printf("%ldm", (long)ts.tv_sec / 60); 23872dfe7a3SPoul-Henning Kamp ts.tv_sec %= 60; 23972dfe7a3SPoul-Henning Kamp f = 1; 24072dfe7a3SPoul-Henning Kamp } 2414a6404dfSJohn Baldwin printf("%lds\n", (long)ts.tv_sec); 24272dfe7a3SPoul-Henning Kamp } 24372dfe7a3SPoul-Henning Kamp 244299cceefSMarcel Moolenaar int 245299cceefSMarcel Moolenaar doadump(boolean_t textdump) 246d39e457bSPoul-Henning Kamp { 247299cceefSMarcel Moolenaar boolean_t coredump; 248e95499bdSAlfred Perlstein 249299cceefSMarcel Moolenaar if (dumping) 250299cceefSMarcel Moolenaar return (EBUSY); 251299cceefSMarcel Moolenaar if (dumper.dumper == NULL) 252299cceefSMarcel Moolenaar return (ENXIO); 253f6449d9dSJulian Elischer 254d39e457bSPoul-Henning Kamp savectx(&dumppcb); 2552d50560aSMarcel Moolenaar dumptid = curthread->td_tid; 256d39e457bSPoul-Henning Kamp dumping++; 257299cceefSMarcel Moolenaar 258299cceefSMarcel Moolenaar coredump = TRUE; 259618c7db3SRobert Watson #ifdef DDB 260299cceefSMarcel Moolenaar if (textdump && textdump_pending) { 261299cceefSMarcel Moolenaar coredump = FALSE; 262618c7db3SRobert Watson textdump_dumpsys(&dumper); 263299cceefSMarcel Moolenaar } 264618c7db3SRobert Watson #endif 265299cceefSMarcel Moolenaar if (coredump) 266d39e457bSPoul-Henning Kamp dumpsys(&dumper); 267299cceefSMarcel Moolenaar 2689e473363SRuslan Ermilov dumping--; 269299cceefSMarcel Moolenaar return (0); 270d39e457bSPoul-Henning Kamp } 271d39e457bSPoul-Henning Kamp 272d07f87a2SDon Lewis static int 273d07f87a2SDon Lewis isbufbusy(struct buf *bp) 274d07f87a2SDon Lewis { 275d07f87a2SDon Lewis if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 && 276d638e093SAttilio Rao BUF_ISLOCKED(bp)) || 277d07f87a2SDon Lewis ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI)) 278d07f87a2SDon Lewis return (1); 279d07f87a2SDon Lewis return (0); 280d07f87a2SDon Lewis } 281d07f87a2SDon Lewis 282ad4240feSJulian Elischer /* 28370ce93f4SNate Lawson * Shutdown the system cleanly to prepare for reboot, halt, or power off. 284ad4240feSJulian Elischer */ 28576e18b25SMarcel Moolenaar void 28676e18b25SMarcel Moolenaar kern_reboot(int howto) 287ad4240feSJulian Elischer { 288b6915bdbSDon Lewis static int first_buf_printf = 1; 289ad4240feSJulian Elischer 290f7ebc7ceSMarcel Moolenaar #if defined(SMP) 29170ce93f4SNate Lawson /* 29270ce93f4SNate Lawson * Bind us to CPU 0 so that all shutdown code runs there. Some 29370ce93f4SNate Lawson * systems don't shutdown properly (i.e., ACPI power off) if we 29470ce93f4SNate Lawson * run on another processor. 29570ce93f4SNate Lawson */ 296982d11f8SJeff Roberson thread_lock(curthread); 29720e25d7dSPeter Wemm sched_bind(curthread, 0); 298982d11f8SJeff Roberson thread_unlock(curthread); 29976e18b25SMarcel Moolenaar KASSERT(PCPU_GET(cpuid) == 0, ("%s: not running on cpu 0", __func__)); 30020e25d7dSPeter Wemm #endif 30136a52c3cSJeff Roberson /* We're in the process of rebooting. */ 30236a52c3cSJeff Roberson rebooting = 1; 30320e25d7dSPeter Wemm 3043e755f76SMike Smith /* collect extra flags that shutdown_nice might have set */ 3053e755f76SMike Smith howto |= shutdown_howto; 3063e755f76SMike Smith 30761e96500SJohn Baldwin /* We are out of the debugger now. */ 3082d50560aSMarcel Moolenaar kdb_active = 0; 30961e96500SJohn Baldwin 3105230cfd2SJulian Elischer /* 3115230cfd2SJulian Elischer * Do any callouts that should be done BEFORE syncing the filesystems. 3125230cfd2SJulian Elischer */ 313fcb893a8SMike Smith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 3145230cfd2SJulian Elischer 3155230cfd2SJulian Elischer /* 3165230cfd2SJulian Elischer * Now sync filesystems 3175230cfd2SJulian Elischer */ 318ad4240feSJulian Elischer if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 319ad4240feSJulian Elischer register struct buf *bp; 32062820f25SJason Evans int iter, nbusy, pbusy; 3210c0b25aeSJohn Baldwin #ifndef PREEMPTION 32262820f25SJason Evans int subiter; 3230c0b25aeSJohn Baldwin #endif 324ad4240feSJulian Elischer 325ad4240feSJulian Elischer waittime = 0; 326ad4240feSJulian Elischer 3272be767e0SAttilio Rao #ifdef SW_WATCHDOG 3282be767e0SAttilio Rao wdog_kern_pat(WD_LASTVAL); 3292be767e0SAttilio Rao #endif 3303fafa27bSStephan Uphoff sync(curthread, NULL); 331ad4240feSJulian Elischer 332b1897c19SJulian Elischer /* 333b1897c19SJulian Elischer * With soft updates, some buffers that are 334b1897c19SJulian Elischer * written will be remarked as dirty until other 335b1897c19SJulian Elischer * buffers are written. 336b1897c19SJulian Elischer */ 33762820f25SJason Evans for (iter = pbusy = 0; iter < 20; iter++) { 338ad4240feSJulian Elischer nbusy = 0; 339d07f87a2SDon Lewis for (bp = &buf[nbuf]; --bp >= buf; ) 340d07f87a2SDon Lewis if (isbufbusy(bp)) 341ad4240feSJulian Elischer nbusy++; 342b6915bdbSDon Lewis if (nbusy == 0) { 343b6915bdbSDon Lewis if (first_buf_printf) 34437abb77fSPoul-Henning Kamp printf("All buffers synced."); 345ad4240feSJulian Elischer break; 346b6915bdbSDon Lewis } 347b6915bdbSDon Lewis if (first_buf_printf) { 348b6915bdbSDon Lewis printf("Syncing disks, buffers remaining... "); 349b6915bdbSDon Lewis first_buf_printf = 0; 350b6915bdbSDon Lewis } 351ad4240feSJulian Elischer printf("%d ", nbusy); 35262820f25SJason Evans if (nbusy < pbusy) 35362820f25SJason Evans iter = 0; 35462820f25SJason Evans pbusy = nbusy; 3552be767e0SAttilio Rao #ifdef SW_WATCHDOG 3562be767e0SAttilio Rao wdog_kern_pat(WD_LASTVAL); 3572be767e0SAttilio Rao #endif 3583fafa27bSStephan Uphoff sync(curthread, NULL); 3590c0b25aeSJohn Baldwin 3600c0b25aeSJohn Baldwin #ifdef PREEMPTION 3610c0b25aeSJohn Baldwin /* 3620c0b25aeSJohn Baldwin * Drop Giant and spin for a while to allow 3630c0b25aeSJohn Baldwin * interrupt threads to run. 3640c0b25aeSJohn Baldwin */ 3650c0b25aeSJohn Baldwin DROP_GIANT(); 3660c0b25aeSJohn Baldwin DELAY(50000 * iter); 3670c0b25aeSJohn Baldwin PICKUP_GIANT(); 3680c0b25aeSJohn Baldwin #else 3690c0b25aeSJohn Baldwin /* 3700c0b25aeSJohn Baldwin * Drop Giant and context switch several times to 3710c0b25aeSJohn Baldwin * allow interrupt threads to run. 3720c0b25aeSJohn Baldwin */ 373c86b6ff5SJohn Baldwin DROP_GIANT(); 37462820f25SJason Evans for (subiter = 0; subiter < 50 * iter; subiter++) { 375982d11f8SJeff Roberson thread_lock(curthread); 376bf0acc27SJohn Baldwin mi_switch(SW_VOL, NULL); 377982d11f8SJeff Roberson thread_unlock(curthread); 37862820f25SJason Evans DELAY(1000); 37962820f25SJason Evans } 38020cdcc5bSJohn Baldwin PICKUP_GIANT(); 3810c0b25aeSJohn Baldwin #endif 382ad4240feSJulian Elischer } 383c8c216d5SNate Lawson printf("\n"); 384d02d6d04SMike Smith /* 385d02d6d04SMike Smith * Count only busy local buffers to prevent forcing 386d02d6d04SMike Smith * a fsck if we're just a client of a wedged NFS server 387d02d6d04SMike Smith */ 388d02d6d04SMike Smith nbusy = 0; 389d02d6d04SMike Smith for (bp = &buf[nbuf]; --bp >= buf; ) { 390d07f87a2SDon Lewis if (isbufbusy(bp)) { 391c5690651SPoul-Henning Kamp #if 0 392c5690651SPoul-Henning Kamp /* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */ 393f3732fd1SPoul-Henning Kamp if (bp->b_dev == NULL) { 3940429e37aSPoul-Henning Kamp TAILQ_REMOVE(&mountlist, 39567812eacSKirk McKusick bp->b_vp->v_mount, mnt_list); 3969c111b31SPoul-Henning Kamp continue; 397dfd5dee1SPeter Wemm } 398c5690651SPoul-Henning Kamp #endif 3999c111b31SPoul-Henning Kamp nbusy++; 400*fa2b39a1SAttilio Rao if (show_busybufs > 0) { 4019c111b31SPoul-Henning Kamp printf( 402*fa2b39a1SAttilio Rao "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:", 403*fa2b39a1SAttilio Rao nbusy, bp, bp->b_vp, bp->b_flags, 404*fa2b39a1SAttilio Rao (intmax_t)bp->b_blkno, 405*fa2b39a1SAttilio Rao (intmax_t)bp->b_lblkno); 406*fa2b39a1SAttilio Rao BUF_LOCKPRINTINFO(bp); 407*fa2b39a1SAttilio Rao if (show_busybufs > 1) 408*fa2b39a1SAttilio Rao vn_printf(bp->b_vp, 409*fa2b39a1SAttilio Rao "vnode content: "); 410*fa2b39a1SAttilio Rao } 4119c111b31SPoul-Henning Kamp } 412d02d6d04SMike Smith } 413ad4240feSJulian Elischer if (nbusy) { 414ad4240feSJulian Elischer /* 415ad4240feSJulian Elischer * Failed to sync all blocks. Indicate this and don't 416ad4240feSJulian Elischer * unmount filesystems (thus forcing an fsck on reboot). 417ad4240feSJulian Elischer */ 418b6915bdbSDon Lewis printf("Giving up on %d buffers\n", nbusy); 419ad4240feSJulian Elischer DELAY(5000000); /* 5 seconds */ 420ad4240feSJulian Elischer } else { 421b6915bdbSDon Lewis if (!first_buf_printf) 422b6915bdbSDon Lewis printf("Final sync complete\n"); 423ad4240feSJulian Elischer /* 424ad4240feSJulian Elischer * Unmount filesystems 425ad4240feSJulian Elischer */ 426ad4240feSJulian Elischer if (panicstr == 0) 427ad4240feSJulian Elischer vfs_unmountall(); 428ad4240feSJulian Elischer } 4290909f38aSPawel Jakub Dawidek swapoff_all(); 430ad4240feSJulian Elischer DELAY(100000); /* wait for console output to finish */ 431ad4240feSJulian Elischer } 4325230cfd2SJulian Elischer 43372dfe7a3SPoul-Henning Kamp print_uptime(); 43472dfe7a3SPoul-Henning Kamp 4355230cfd2SJulian Elischer /* 4365230cfd2SJulian Elischer * Ok, now do things that assume all filesystem activity has 4375230cfd2SJulian Elischer * been completed. 4385230cfd2SJulian Elischer */ 439fcb893a8SMike Smith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 44070ce93f4SNate Lawson 441f6449d9dSJulian Elischer if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 442299cceefSMarcel Moolenaar doadump(TRUE); 4432cfa0a03SJustin T. Gibbs 4442cfa0a03SJustin T. Gibbs /* Now that we're going to really halt the system... */ 445fcb893a8SMike Smith EVENTHANDLER_INVOKE(shutdown_final, howto); 4462cfa0a03SJustin T. Gibbs 447fcb893a8SMike Smith for(;;) ; /* safety against shutdown_reset not working */ 448fcb893a8SMike Smith /* NOTREACHED */ 449fcb893a8SMike Smith } 450fcb893a8SMike Smith 451fcb893a8SMike Smith /* 452fcb893a8SMike Smith * If the shutdown was a clean halt, behave accordingly. 453fcb893a8SMike Smith */ 454fcb893a8SMike Smith static void 455fcb893a8SMike Smith shutdown_halt(void *junk, int howto) 456fcb893a8SMike Smith { 457e95499bdSAlfred Perlstein 458ad4240feSJulian Elischer if (howto & RB_HALT) { 459ad4240feSJulian Elischer printf("\n"); 460ad4240feSJulian Elischer printf("The operating system has halted.\n"); 461ad4240feSJulian Elischer printf("Please press any key to reboot.\n\n"); 462d13d3630SJulian Elischer switch (cngetc()) { 463d13d3630SJulian Elischer case -1: /* No console, just die */ 464d13d3630SJulian Elischer cpu_halt(); 465d13d3630SJulian Elischer /* NOTREACHED */ 466d13d3630SJulian Elischer default: 4672cfa0a03SJustin T. Gibbs howto &= ~RB_HALT; 468d13d3630SJulian Elischer break; 469d13d3630SJulian Elischer } 470fcb893a8SMike Smith } 471fcb893a8SMike Smith } 472ad4240feSJulian Elischer 473fcb893a8SMike Smith /* 474fcb893a8SMike Smith * Check to see if the system paniced, pause and then reboot 475fcb893a8SMike Smith * according to the specified delay. 476fcb893a8SMike Smith */ 477fcb893a8SMike Smith static void 478fcb893a8SMike Smith shutdown_panic(void *junk, int howto) 479fcb893a8SMike Smith { 480fcb893a8SMike Smith int loop; 481fcb893a8SMike Smith 482fcb893a8SMike Smith if (howto & RB_DUMP) { 483ad4240feSJulian Elischer if (PANIC_REBOOT_WAIT_TIME != 0) { 484ad4240feSJulian Elischer if (PANIC_REBOOT_WAIT_TIME != -1) { 4852cfa0a03SJustin T. Gibbs printf("Automatic reboot in %d seconds - " 4862cfa0a03SJustin T. Gibbs "press a key on the console to abort\n", 487ad4240feSJulian Elischer PANIC_REBOOT_WAIT_TIME); 4882cfa0a03SJustin T. Gibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 4892cfa0a03SJustin T. Gibbs loop > 0; --loop) { 490ad4240feSJulian Elischer DELAY(1000 * 100); /* 1/10th second */ 491a7f8f2abSBruce Evans /* Did user type a key? */ 492a7f8f2abSBruce Evans if (cncheckc() != -1) 493ad4240feSJulian Elischer break; 494ad4240feSJulian Elischer } 495ad4240feSJulian Elischer if (!loop) 496fcb893a8SMike Smith return; 497ad4240feSJulian Elischer } 498ad4240feSJulian Elischer } else { /* zero time specified - reboot NOW */ 499fcb893a8SMike Smith return; 500ad4240feSJulian Elischer } 501422702e9SNik Clayton printf("--> Press a key on the console to reboot,\n"); 502422702e9SNik Clayton printf("--> or switch off the system now.\n"); 503ad4240feSJulian Elischer cngetc(); 504ad4240feSJulian Elischer } 505fcb893a8SMike Smith } 506fcb893a8SMike Smith 507fcb893a8SMike Smith /* 508fcb893a8SMike Smith * Everything done, now reset 509fcb893a8SMike Smith */ 510fcb893a8SMike Smith static void 511fcb893a8SMike Smith shutdown_reset(void *junk, int howto) 512fcb893a8SMike Smith { 513e95499bdSAlfred Perlstein 514ad4240feSJulian Elischer printf("Rebooting...\n"); 515ad4240feSJulian Elischer DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 516248bb937SAttilio Rao 517248bb937SAttilio Rao /* 518248bb937SAttilio Rao * Acquiring smp_ipi_mtx here has a double effect: 519248bb937SAttilio Rao * - it disables interrupts avoiding CPU0 preemption 520248bb937SAttilio Rao * by fast handlers (thus deadlocking against other CPUs) 521248bb937SAttilio Rao * - it avoids deadlocks against smp_rendezvous() or, more 522248bb937SAttilio Rao * generally, threads busy-waiting, with this spinlock held, 523248bb937SAttilio Rao * and waiting for responses by threads on other CPUs 524248bb937SAttilio Rao * (ie. smp_tlb_shootdown()). 5250a2d5feaSAttilio Rao * 5260a2d5feaSAttilio Rao * For the !SMP case it just needs to handle the former problem. 527248bb937SAttilio Rao */ 5280a2d5feaSAttilio Rao #ifdef SMP 529248bb937SAttilio Rao mtx_lock_spin(&smp_ipi_mtx); 5300a2d5feaSAttilio Rao #else 5310a2d5feaSAttilio Rao spinlock_enter(); 5320a2d5feaSAttilio Rao #endif 533248bb937SAttilio Rao 534269fb9d7SJulian Elischer /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 535ad4240feSJulian Elischer cpu_reset(); 536fcb893a8SMike Smith /* NOTREACHED */ /* assuming reset worked */ 537ad4240feSJulian Elischer } 538ad4240feSJulian Elischer 539ad4240feSJulian Elischer /* 540ad4240feSJulian Elischer * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 541ad4240feSJulian Elischer * and then reboots. If we are called twice, then we avoid trying to sync 542ad4240feSJulian Elischer * the disks as this often leads to recursive panics. 543ad4240feSJulian Elischer */ 544ad4240feSJulian Elischer void 5459a6dc4b6SPoul-Henning Kamp panic(const char *fmt, ...) 546ad4240feSJulian Elischer { 54764dd590eSAndriy Gapon #ifdef SMP 54864dd590eSAndriy Gapon static volatile u_int panic_cpu = NOCPU; 54964dd590eSAndriy Gapon #endif 550fe799533SAndrew Gallatin struct thread *td = curthread; 551e485b64bSJohn Baldwin int bootopt, newpanic; 552ad4240feSJulian Elischer va_list ap; 55399237364SAndrey A. Chernov static char buf[256]; 554ad4240feSJulian Elischer 55541a4e90eSKonstantin Belousov critical_enter(); 5560384fff8SJason Evans #ifdef SMP 5571a5333c3SJohn Baldwin /* 5581a5333c3SJohn Baldwin * We don't want multiple CPU's to panic at the same time, so we 5590711ca46SJohn Baldwin * use panic_cpu as a simple spinlock. We have to keep checking 5600711ca46SJohn Baldwin * panic_cpu if we are spinning in case the panic on the first 5611a5333c3SJohn Baldwin * CPU is canceled. 5621a5333c3SJohn Baldwin */ 5630711ca46SJohn Baldwin if (panic_cpu != PCPU_GET(cpuid)) 5640711ca46SJohn Baldwin while (atomic_cmpset_int(&panic_cpu, NOCPU, 5650711ca46SJohn Baldwin PCPU_GET(cpuid)) == 0) 5660711ca46SJohn Baldwin while (panic_cpu != NOCPU) 5670711ca46SJohn Baldwin ; /* nothing */ 5680384fff8SJason Evans #endif 5690384fff8SJason Evans 570e3adb685SAttilio Rao bootopt = RB_AUTOBOOT; 571e485b64bSJohn Baldwin newpanic = 0; 572ad4240feSJulian Elischer if (panicstr) 573ad4240feSJulian Elischer bootopt |= RB_NOSYNC; 574e485b64bSJohn Baldwin else { 575e3adb685SAttilio Rao bootopt |= RB_DUMP; 576ad4240feSJulian Elischer panicstr = fmt; 577e485b64bSJohn Baldwin newpanic = 1; 578e485b64bSJohn Baldwin } 579ad4240feSJulian Elischer 580ad4240feSJulian Elischer va_start(ap, fmt); 5814f1b4577SIan Dowse if (newpanic) { 5822127f260SArchie Cobbs (void)vsnprintf(buf, sizeof(buf), fmt, ap); 58399237364SAndrey A. Chernov panicstr = buf; 5849a6dc4b6SPoul-Henning Kamp printf("panic: %s\n", buf); 5854f1b4577SIan Dowse } else { 5864f1b4577SIan Dowse printf("panic: "); 5874f1b4577SIan Dowse vprintf(fmt, ap); 5889a6dc4b6SPoul-Henning Kamp printf("\n"); 5894f1b4577SIan Dowse } 5904f1b4577SIan Dowse va_end(ap); 59147d81897SSteve Passe #ifdef SMP 59255c45354SJohn Baldwin printf("cpuid = %d\n", PCPU_GET(cpuid)); 5932bcc63c5SJohn Baldwin #endif 594ad4240feSJulian Elischer 5952d50560aSMarcel Moolenaar #ifdef KDB 596e485b64bSJohn Baldwin if (newpanic && trace_on_panic) 5972d50560aSMarcel Moolenaar kdb_backtrace(); 598ad4240feSJulian Elischer if (debugger_on_panic) 5993de213ccSRobert Watson kdb_enter(KDB_WHY_PANIC, "panic"); 6001432aa0cSJohn Baldwin #endif 601982d11f8SJeff Roberson /*thread_lock(td); */ 602fe799533SAndrew Gallatin td->td_flags |= TDF_INPANIC; 603982d11f8SJeff Roberson /* thread_unlock(td); */ 604259ed917SPeter Wemm if (!sync_on_panic) 605259ed917SPeter Wemm bootopt |= RB_NOSYNC; 60641a4e90eSKonstantin Belousov critical_exit(); 60776e18b25SMarcel Moolenaar kern_reboot(bootopt); 608ad4240feSJulian Elischer } 609ad4240feSJulian Elischer 610e0d898b4SJulian Elischer /* 611db82a982SMike Smith * Support for poweroff delay. 612b22692bdSNick Hibma * 613b22692bdSNick Hibma * Please note that setting this delay too short might power off your machine 614b22692bdSNick Hibma * before the write cache on your hard disk has been flushed, leading to 615b22692bdSNick Hibma * soft-updates inconsistencies. 616db82a982SMike Smith */ 6179eec6969SMike Smith #ifndef POWEROFF_DELAY 6189eec6969SMike Smith # define POWEROFF_DELAY 5000 6199eec6969SMike Smith #endif 6209eec6969SMike Smith static int poweroff_delay = POWEROFF_DELAY; 6219eec6969SMike Smith 622db82a982SMike Smith SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 623db82a982SMike Smith &poweroff_delay, 0, ""); 624db82a982SMike Smith 625fcb893a8SMike Smith static void 626fcb893a8SMike Smith poweroff_wait(void *junk, int howto) 627db82a982SMike Smith { 628e95499bdSAlfred Perlstein 629db82a982SMike Smith if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 630db82a982SMike Smith return; 631db82a982SMike Smith DELAY(poweroff_delay * 1000); 632db82a982SMike Smith } 6335e950839SLuoqi Chen 6345e950839SLuoqi Chen /* 6355e950839SLuoqi Chen * Some system processes (e.g. syncer) need to be stopped at appropriate 6365e950839SLuoqi Chen * points in their main loops prior to a system shutdown, so that they 6375e950839SLuoqi Chen * won't interfere with the shutdown process (e.g. by holding a disk buf 6385e950839SLuoqi Chen * to cause sync to fail). For each of these system processes, register 6395e950839SLuoqi Chen * shutdown_kproc() as a handler for one of shutdown events. 6405e950839SLuoqi Chen */ 6415e950839SLuoqi Chen static int kproc_shutdown_wait = 60; 6425e950839SLuoqi Chen SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 6435e950839SLuoqi Chen &kproc_shutdown_wait, 0, ""); 6445e950839SLuoqi Chen 6455e950839SLuoqi Chen void 646ffc831daSJohn Baldwin kproc_shutdown(void *arg, int howto) 6475e950839SLuoqi Chen { 6485e950839SLuoqi Chen struct proc *p; 6495e950839SLuoqi Chen int error; 6505e950839SLuoqi Chen 6515e950839SLuoqi Chen if (panicstr) 6525e950839SLuoqi Chen return; 6535e950839SLuoqi Chen 6545e950839SLuoqi Chen p = (struct proc *)arg; 655b1c81391SNate Lawson printf("Waiting (max %d seconds) for system process `%s' to stop...", 6564f9d48e4SJohn Baldwin kproc_shutdown_wait, p->p_comm); 6573745c395SJulian Elischer error = kproc_suspend(p, kproc_shutdown_wait * hz); 6585e950839SLuoqi Chen 6595e950839SLuoqi Chen if (error == EWOULDBLOCK) 660b1c81391SNate Lawson printf("timed out\n"); 6615e950839SLuoqi Chen else 662b1c81391SNate Lawson printf("done\n"); 6635e950839SLuoqi Chen } 66481661c94SPoul-Henning Kamp 6657ab24ea3SJulian Elischer void 6667ab24ea3SJulian Elischer kthread_shutdown(void *arg, int howto) 6677ab24ea3SJulian Elischer { 6687ab24ea3SJulian Elischer struct thread *td; 6697ab24ea3SJulian Elischer int error; 6707ab24ea3SJulian Elischer 6717ab24ea3SJulian Elischer if (panicstr) 6727ab24ea3SJulian Elischer return; 6737ab24ea3SJulian Elischer 6747ab24ea3SJulian Elischer td = (struct thread *)arg; 6757ab24ea3SJulian Elischer printf("Waiting (max %d seconds) for system thread `%s' to stop...", 6764f9d48e4SJohn Baldwin kproc_shutdown_wait, td->td_name); 6777ab24ea3SJulian Elischer error = kthread_suspend(td, kproc_shutdown_wait * hz); 6787ab24ea3SJulian Elischer 6797ab24ea3SJulian Elischer if (error == EWOULDBLOCK) 6807ab24ea3SJulian Elischer printf("timed out\n"); 6817ab24ea3SJulian Elischer else 6827ab24ea3SJulian Elischer printf("done\n"); 6837ab24ea3SJulian Elischer } 6847ab24ea3SJulian Elischer 68581661c94SPoul-Henning Kamp /* Registration of dumpers */ 68681661c94SPoul-Henning Kamp int 68781661c94SPoul-Henning Kamp set_dumper(struct dumperinfo *di) 68881661c94SPoul-Henning Kamp { 689e95499bdSAlfred Perlstein 69081661c94SPoul-Henning Kamp if (di == NULL) { 69181661c94SPoul-Henning Kamp bzero(&dumper, sizeof dumper); 69281661c94SPoul-Henning Kamp return (0); 69381661c94SPoul-Henning Kamp } 69481661c94SPoul-Henning Kamp if (dumper.dumper != NULL) 69581661c94SPoul-Henning Kamp return (EBUSY); 69681661c94SPoul-Henning Kamp dumper = *di; 69781661c94SPoul-Henning Kamp return (0); 69881661c94SPoul-Henning Kamp } 69981661c94SPoul-Henning Kamp 700007b1b7bSRuslan Ermilov /* Call dumper with bounds checking. */ 701007b1b7bSRuslan Ermilov int 702007b1b7bSRuslan Ermilov dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical, 703007b1b7bSRuslan Ermilov off_t offset, size_t length) 704007b1b7bSRuslan Ermilov { 705007b1b7bSRuslan Ermilov 706007b1b7bSRuslan Ermilov if (length != 0 && (offset < di->mediaoffset || 707007b1b7bSRuslan Ermilov offset - di->mediaoffset + length > di->mediasize)) { 708007b1b7bSRuslan Ermilov printf("Attempt to write outside dump device boundaries.\n"); 709007b1b7bSRuslan Ermilov return (ENXIO); 710007b1b7bSRuslan Ermilov } 711007b1b7bSRuslan Ermilov return (di->dumper(di->priv, virtual, physical, offset, length)); 712007b1b7bSRuslan Ermilov } 713007b1b7bSRuslan Ermilov 714e6592ee5SPeter Wemm void 715e6592ee5SPeter Wemm mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver, 716e6592ee5SPeter Wemm uint64_t dumplen, uint32_t blksz) 717e6592ee5SPeter Wemm { 718e6592ee5SPeter Wemm 719e6592ee5SPeter Wemm bzero(kdh, sizeof(*kdh)); 720e6592ee5SPeter Wemm strncpy(kdh->magic, magic, sizeof(kdh->magic)); 721e6592ee5SPeter Wemm strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); 722e6592ee5SPeter Wemm kdh->version = htod32(KERNELDUMPVERSION); 723e6592ee5SPeter Wemm kdh->architectureversion = htod32(archver); 724e6592ee5SPeter Wemm kdh->dumplength = htod64(dumplen); 725e6592ee5SPeter Wemm kdh->dumptime = htod64(time_second); 726e6592ee5SPeter Wemm kdh->blocksize = htod32(blksz); 727c1f19219SJamie Gritton strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname)); 728e6592ee5SPeter Wemm strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); 729e6592ee5SPeter Wemm if (panicstr != NULL) 730e6592ee5SPeter Wemm strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); 731e6592ee5SPeter Wemm kdh->parity = kerneldump_parity(kdh); 732e6592ee5SPeter Wemm } 733