1800e7495SMitchell Horne.\" $NetBSD: boot.9,v 1.2 1996/09/24 07:01:26 ghudson Exp $ 2800e7495SMitchell Horne.\" 3d55c1877SMitchell Horne.\" SPDX-License-Identifier: BSD-4-Clause 4d55c1877SMitchell Horne.\" 5800e7495SMitchell Horne.\" Copyright (c) 1997 6800e7495SMitchell Horne.\" Mike Pritchard. All rights reserved. 7800e7495SMitchell Horne.\" 8800e7495SMitchell Horne.\" Copyright (c) 1994 Christopher G. Demetriou 9800e7495SMitchell Horne.\" All rights reserved. 10800e7495SMitchell Horne.\" 11d55c1877SMitchell Horne.\" Copyright (c) 2021,2023 The FreeBSD Foundation 12d55c1877SMitchell Horne.\" 13d55c1877SMitchell Horne.\" Portions of this documentation were written by Mitchell Horne 14d55c1877SMitchell Horne.\" under sponsorship from the FreeBSD Foundation. 15d55c1877SMitchell Horne.\" 16800e7495SMitchell Horne.\" Redistribution and use in source and binary forms, with or without 17800e7495SMitchell Horne.\" modification, are permitted provided that the following conditions 18800e7495SMitchell Horne.\" are met: 19800e7495SMitchell Horne.\" 1. Redistributions of source code must retain the above copyright 20800e7495SMitchell Horne.\" notice, this list of conditions and the following disclaimer. 21800e7495SMitchell Horne.\" 2. Redistributions in binary form must reproduce the above copyright 22800e7495SMitchell Horne.\" notice, this list of conditions and the following disclaimer in the 23800e7495SMitchell Horne.\" documentation and/or other materials provided with the distribution. 24800e7495SMitchell Horne.\" 3. All advertising materials mentioning features or use of this software 25800e7495SMitchell Horne.\" must display the following acknowledgement: 26800e7495SMitchell Horne.\" This product includes software developed by Christopher G. Demetriou 27800e7495SMitchell Horne.\" for the NetBSD Project. 28d55c1877SMitchell Horne.\" 4. The name of the author may not be used to endorse or promote products 29800e7495SMitchell Horne.\" derived from this software without specific prior written permission 30800e7495SMitchell Horne.\" 31800e7495SMitchell Horne.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 32800e7495SMitchell Horne.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 33800e7495SMitchell Horne.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 34800e7495SMitchell Horne.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 35800e7495SMitchell Horne.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36800e7495SMitchell Horne.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37800e7495SMitchell Horne.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38800e7495SMitchell Horne.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39800e7495SMitchell Horne.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 40800e7495SMitchell Horne.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41800e7495SMitchell Horne.\" 42*ae943767SMitchell Horne.Dd November 23, 2023 43d55c1877SMitchell Horne.Dt REBOOT 9 44800e7495SMitchell Horne.Os 45800e7495SMitchell Horne.Sh NAME 46b7caa912SMitchell Horne.Nm kern_reboot , 47b7caa912SMitchell Horne.Nm shutdown_nice 48d55c1877SMitchell Horne.Nd reboot, halt, or power off the system 49800e7495SMitchell Horne.Sh SYNOPSIS 50800e7495SMitchell Horne.In sys/types.h 51800e7495SMitchell Horne.In sys/systm.h 52800e7495SMitchell Horne.In sys/reboot.h 53d55c1877SMitchell Horne.Vt extern int rebooting; 54800e7495SMitchell Horne.Ft void 55800e7495SMitchell Horne.Fn kern_reboot "int howto" 56b7caa912SMitchell Horne.Ft void 57b7caa912SMitchell Horne.Fn shutdown_nice "int howto" 58800e7495SMitchell Horne.In sys/eventhandler.h 59800e7495SMitchell Horne.Fn EVENTHANDLER_REGISTER "shutdown_pre_sync" "shutdown_fn" "private" "priority" 60800e7495SMitchell Horne.Fn EVENTHANDLER_REGISTER "shutdown_post_sync" "shutdown_fn" "private" "priority" 61800e7495SMitchell Horne.Fn EVENTHANDLER_REGISTER "shutdown_final" "shutdown_fn" "private" "priority" 62800e7495SMitchell Horne.Sh DESCRIPTION 63800e7495SMitchell HorneThe 64800e7495SMitchell Horne.Fn kern_reboot 65d55c1877SMitchell Hornefunction handles final system shutdown, and either halts, reboots, 66d55c1877SMitchell Horneor powers down the system. 67800e7495SMitchell HorneThe exact action to be taken is determined by the flags passed in 68d55c1877SMitchell Horne.Fa howto . 69800e7495SMitchell Horne.Pp 70d55c1877SMitchell HorneThe relevant flags are: 71d55c1877SMitchell Horne.Bl -tag -compact -offset indent -width "RB_POWERCYCLE" 72d55c1877SMitchell Horne.It Dv RB_HALT 73d55c1877SMitchell HorneHalt the system in-place rather than restarting. 74d55c1877SMitchell Horne.It Dv RB_POWEROFF 75d55c1877SMitchell HornePower down the system rather than restarting. 76d55c1877SMitchell Horne.It Dv RB_POWERCYCLE 77d55c1877SMitchell HorneRequest a power-cycle in addition to restarting. 78d55c1877SMitchell Horne.It Dv RB_NOSYNC 79d55c1877SMitchell HorneDo not sync filesystems during shutdown. 80d55c1877SMitchell Horne.It Dv RB_DUMP 81d55c1877SMitchell HorneDump kernel memory during shutdown. 82d55c1877SMitchell Horne.El 83d55c1877SMitchell Horne.Pp 84d55c1877SMitchell HorneThe 85d55c1877SMitchell Horne.Fa howto 86d55c1877SMitchell Hornefield, and its full list of flags are described in additional detail by 87d55c1877SMitchell Horne.Xr reboot 2 . 88d55c1877SMitchell Horne.Pp 89800e7495SMitchell Horne.Fn kern_reboot 90d55c1877SMitchell Horneperforms the following actions: 91800e7495SMitchell Horne.Bl -enum -offset indent 92800e7495SMitchell Horne.It 93d55c1877SMitchell HorneSet the 94d55c1877SMitchell Horne.Va rebooting 95d55c1877SMitchell Hornevariable to 96d55c1877SMitchell Horne.Dv 1 , 97d55c1877SMitchell Horneindicating that the reboot process has begun and cannot be stopped. 98d55c1877SMitchell Horne.It 99d55c1877SMitchell HorneUnless the 100800e7495SMitchell Horne.Dv RB_NOSYNC 101d55c1877SMitchell Horneflag is set in 102800e7495SMitchell Horne.Fa howto , 103d55c1877SMitchell Hornesync and unmount the system's disks by calling 104800e7495SMitchell Horne.Xr vfs_unmountall 9 . 105800e7495SMitchell Horne.It 106d55c1877SMitchell HorneIf rebooting after a panic 107d55c1877SMitchell Horne.Po 108800e7495SMitchell Horne.Dv RB_DUMP 109800e7495SMitchell Horneis set in 110800e7495SMitchell Horne.Fa howto , 111800e7495SMitchell Hornebut 112800e7495SMitchell Horne.Dv RB_HALT 113d55c1877SMitchell Horneis not set 114d55c1877SMitchell Horne.Pc , 115d55c1877SMitchell Horneinitiate a system crash dump via 116d55c1877SMitchell Horne.Fn doadump . 117800e7495SMitchell Horne.It 118d55c1877SMitchell HornePrint a message indicating that the system is about to be halted 119d55c1877SMitchell Horneor rebooted, and a report of the total system uptime. 120800e7495SMitchell Horne.It 121d55c1877SMitchell HorneExecute all registered shutdown hooks. 122f6f8cbdaSMitchell HorneSee 123f6f8cbdaSMitchell Horne.Sx SHUTDOWN HOOKS 124f6f8cbdaSMitchell Hornebelow. 125800e7495SMitchell Horne.It 126d55c1877SMitchell HorneAs a last resort, if none of the shutdown hooks handled the reboot, call the 127d55c1877SMitchell Hornemachine-dependent 128d55c1877SMitchell Horne.Fn cpu_reset 129d55c1877SMitchell Hornefunction. 130d55c1877SMitchell HorneIn the unlikely case that this is not supported, 131d55c1877SMitchell Horne.Fn kern_reboot 132d55c1877SMitchell Hornewill loop forever at the end of the function. 133d55c1877SMitchell HorneThis requires a manual reset of the system. 134800e7495SMitchell Horne.El 135800e7495SMitchell Horne.Pp 136800e7495SMitchell Horne.Fn kern_reboot 137d55c1877SMitchell Hornemay be called from a typical kernel execution context, when the system is 138d55c1877SMitchell Hornerunning normally. 139d55c1877SMitchell HorneIt may also be called as the final step of a kernel panic, or from the kernel 140d55c1877SMitchell Hornedebugger. 141d55c1877SMitchell HorneTherefore, the code in this function is subject to restrictions described by 142d55c1877SMitchell Hornethe 143d55c1877SMitchell Horne.Sx EXECUTION CONTEXT 144d55c1877SMitchell Hornesection of the 145d55c1877SMitchell Horne.Xr panic 9 146d55c1877SMitchell Horneman page. 147b7caa912SMitchell Horne.Pp 148b7caa912SMitchell HorneThe 149b7caa912SMitchell Horne.Fn shutdown_nice 150b7caa912SMitchell Hornefunction is the intended path for performing a clean reboot or shutdown when 151b7caa912SMitchell Hornethe system is operating under normal conditions. 152b7caa912SMitchell HorneCalling this function will send a signal to the 153b7caa912SMitchell Horne.Xr init 8 154b7caa912SMitchell Horneprocess, instructing it to perform a shutdown. 155b7caa912SMitchell HorneWhen 156b7caa912SMitchell Horne.Xr init 8 157b7caa912SMitchell Hornehas cleanly terminated its children, it will perform the 158b7caa912SMitchell Horne.Xr reboot 2 159b7caa912SMitchell Hornesystem call, which in turn calls 160b7caa912SMitchell Horne.Fn kern_reboot . 161b7caa912SMitchell Horne.Pp 162b7caa912SMitchell HorneIf 163b7caa912SMitchell Horne.Fn shutdown_nice 164b7caa912SMitchell Horneis called before the 165b7caa912SMitchell Horne.Xr init 8 166b7caa912SMitchell Horneprocess has been spawned, or if the system has panicked or otherwise halted, 167b7caa912SMitchell Horne.Fn kern_reboot 168b7caa912SMitchell Hornewill be called directly. 169f6f8cbdaSMitchell Horne.Sh SHUTDOWN HOOKS 170f6f8cbdaSMitchell HorneThe system defines three separate 171f6f8cbdaSMitchell Horne.Xr EVENTHANDLER 9 172f6f8cbdaSMitchell Horneevents, which are invoked successively during the shutdown procedure. 173f6f8cbdaSMitchell HorneThese are 174f6f8cbdaSMitchell Horne.Va shutdown_pre_sync , 175f6f8cbdaSMitchell Horne.Va shutdown_post_sync , 176f6f8cbdaSMitchell Horneand 177f6f8cbdaSMitchell Horne.Va shutdown_final . 178f6f8cbdaSMitchell HorneThey will be executed unconditionally in the listed order. 179f6f8cbdaSMitchell HorneHandler functions registered to any of these events will receive the value of 180f6f8cbdaSMitchell Horne.Fa howto 181f6f8cbdaSMitchell Horneas their second argument, which may be used to decide what action to take. 182f6f8cbdaSMitchell Horne.Pp 183f6f8cbdaSMitchell HorneThe 184f6f8cbdaSMitchell Horne.Va shutdown_pre_sync 185f6f8cbdaSMitchell Horneevent is invoked before syncing filesystems to disk. 186f6f8cbdaSMitchell HorneIt enables any action or state transition that must happen before this point to 187f6f8cbdaSMitchell Hornetake place. 188f6f8cbdaSMitchell Horne.Pp 189f6f8cbdaSMitchell HorneThe 190f6f8cbdaSMitchell Horne.Va shutdown_post_sync 191f6f8cbdaSMitchell Horneevent is invoked at the point immediately after the filesystem sync has 192f6f8cbdaSMitchell Hornefinished. 193f6f8cbdaSMitchell HorneIt enables, for example, disk drivers to complete the sync by flushing their 194f6f8cbdaSMitchell Hornecache to disk. 195f6f8cbdaSMitchell HorneNote that this event still takes place before the optional kernel core dump. 196f6f8cbdaSMitchell Horne.Pp 197f6f8cbdaSMitchell HorneThe 198f6f8cbdaSMitchell Horne.Va shutdown_final 199f6f8cbdaSMitchell Horneevent is invoked as the very last step of 200f6f8cbdaSMitchell Horne.Fn kern_reboot . 201f6f8cbdaSMitchell HorneDrivers and subsystems such as 202f6f8cbdaSMitchell Horne.Xr acpi 4 203f6f8cbdaSMitchell Hornecan register handlers to this event that will perform the actual reboot, 204f6f8cbdaSMitchell Hornepower-off, or halt. 205f6f8cbdaSMitchell Horne.Pp 206f6f8cbdaSMitchell HorneNotably, the 207f6f8cbdaSMitchell Horne.Va shutdown_final 208f6f8cbdaSMitchell Horneevent is also the point at which all kernel modules will have their shutdown 209f6f8cbdaSMitchell Horne.Po 210f6f8cbdaSMitchell Horne.Dv MOD_SHUTDOWN 211f6f8cbdaSMitchell Horne.Pc 212f6f8cbdaSMitchell Hornehooks executed, and when the 213f6f8cbdaSMitchell Horne.Xr DEVICE_SHUTDOWN 9 214f6f8cbdaSMitchell Hornemethod will be executed recursively on all devices. 215f6f8cbdaSMitchell Horne.Pp 216f6f8cbdaSMitchell HorneAll event handlers, like 217f6f8cbdaSMitchell Horne.Fn kern_reboot 218f6f8cbdaSMitchell Horneitself, may be run in either normal shutdown context or a kernel panic or 219f6f8cbdaSMitchell Hornedebugger context. 220f6f8cbdaSMitchell HorneHandler functions are expected to take care not to trigger recursive panics. 221800e7495SMitchell Horne.Sh RETURN VALUES 222800e7495SMitchell HorneThe 223800e7495SMitchell Horne.Fn kern_reboot 224800e7495SMitchell Hornefunction does not return. 225b7caa912SMitchell Horne.Pp 226b7caa912SMitchell HorneThe 227b7caa912SMitchell Horne.Fn shutdown_nice 228b7caa912SMitchell Hornefunction will usually return to its caller, having initiated the asynchronous 229b7caa912SMitchell Hornesystem shutdown. 230b7caa912SMitchell HorneIt will not return when called from a panic or debugger context, or during 231b7caa912SMitchell Horneearly boot. 232f6f8cbdaSMitchell Horne.Sh EXAMPLES 233f6f8cbdaSMitchell HorneA hypothetical driver, foo(4), defines a 234f6f8cbdaSMitchell Horne.Va shutdown_final 235f6f8cbdaSMitchell Horneevent handler that can handle system power-off by writing to a device register, 236f6f8cbdaSMitchell Hornebut it does not handle halt or reset. 237f6f8cbdaSMitchell Horne.Bd -literal -offset indent 238f6f8cbdaSMitchell Hornevoid 239f6f8cbdaSMitchell Hornefoo_poweroff_handler(struct void *arg, int howto) 240f6f8cbdaSMitchell Horne{ 241f6f8cbdaSMitchell Horne struct foo_softc *sc = arg; 242f6f8cbdaSMitchell Horne uint32_t reg; 243f6f8cbdaSMitchell Horne 244f6f8cbdaSMitchell Horne if ((howto & RB_POWEROFF) != 0) { 245f6f8cbdaSMitchell Horne reg = FOO_POWEROFF; 246f6f8cbdaSMitchell Horne WRITE4(sc, FOO_POWEROFF_REG, reg); 247f6f8cbdaSMitchell Horne } 248f6f8cbdaSMitchell Horne} 249f6f8cbdaSMitchell Horne.Ed 250f6f8cbdaSMitchell Horne.Pp 251f6f8cbdaSMitchell HorneThe handler is then registered in the device attach routine: 252f6f8cbdaSMitchell Horne.Bd -literal -offset indent 253f6f8cbdaSMitchell Horneint 254f6f8cbdaSMitchell Hornefoo_attach(device_t dev) 255f6f8cbdaSMitchell Horne{ 256f6f8cbdaSMitchell Horne struct foo_softc *sc; 257f6f8cbdaSMitchell Horne 258f6f8cbdaSMitchell Horne ... 259f6f8cbdaSMitchell Horne 260f6f8cbdaSMitchell Horne /* Pass the device's software context as the private arg. */ 261f6f8cbdaSMitchell Horne EVENTHANDLER_REGISTER(shutdown_final, foo_poweroff_handler, sc, 262f6f8cbdaSMitchell Horne SHUTDOWN_PRI_DEFAULT); 263f6f8cbdaSMitchell Horne 264f6f8cbdaSMitchell Horne ... 265f6f8cbdaSMitchell Horne} 266f6f8cbdaSMitchell Horne.Ed 267f6f8cbdaSMitchell Horne.Pp 268f6f8cbdaSMitchell HorneThis 269f6f8cbdaSMitchell Horne.Va shutdown_final 270f6f8cbdaSMitchell Hornehandler uses the 271f6f8cbdaSMitchell Horne.Dv RB_NOSYNC 272f6f8cbdaSMitchell Horneflag to detect that a panic or other unusual condition has occurred, and 273f6f8cbdaSMitchell Hornereturns early: 274f6f8cbdaSMitchell Horne.Bd -literal -offset indent 275f6f8cbdaSMitchell Hornevoid 276f6f8cbdaSMitchell Hornebar_shutdown_final(struct void *arg, int howto) 277f6f8cbdaSMitchell Horne{ 278f6f8cbdaSMitchell Horne 279f6f8cbdaSMitchell Horne if ((howto & RB_NOSYNC) != 0) 280f6f8cbdaSMitchell Horne return; 281f6f8cbdaSMitchell Horne 282f6f8cbdaSMitchell Horne /* Some code that is not panic-safe. */ 283f6f8cbdaSMitchell Horne ... 284f6f8cbdaSMitchell Horne} 285f6f8cbdaSMitchell Horne.Ed 286800e7495SMitchell Horne.Sh SEE ALSO 28732068667SChristian Brueffer.Xr reboot 2 , 288b7caa912SMitchell Horne.Xr init 8 , 289f6f8cbdaSMitchell Horne.Xr DEVICE_SHUTDOWN 9 , 29032068667SChristian Brueffer.Xr EVENTHANDLER 9 , 291f6f8cbdaSMitchell Horne.Xr module 9 , 292d55c1877SMitchell Horne.Xr panic 9 , 293800e7495SMitchell Horne.Xr vfs_unmountall 9 294