xref: /freebsd/share/man/man9/kern_reboot.9 (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1.\"	$NetBSD: boot.9,v 1.2 1996/09/24 07:01:26 ghudson Exp $
2.\"
3.\" SPDX-License-Identifier: BSD-4-Clause
4.\"
5.\" Copyright (c) 1997
6.\" 	Mike Pritchard.  All rights reserved.
7.\"
8.\" Copyright (c) 1994 Christopher G. Demetriou
9.\" All rights reserved.
10.\"
11.\" Copyright (c) 2021,2023 The FreeBSD Foundation
12.\"
13.\" Portions of this documentation were written by Mitchell Horne
14.\" under sponsorship from the FreeBSD Foundation.
15.\"
16.\" Redistribution and use in source and binary forms, with or without
17.\" modification, are permitted provided that the following conditions
18.\" are met:
19.\" 1. Redistributions of source code must retain the above copyright
20.\"    notice, this list of conditions and the following disclaimer.
21.\" 2. Redistributions in binary form must reproduce the above copyright
22.\"    notice, this list of conditions and the following disclaimer in the
23.\"    documentation and/or other materials provided with the distribution.
24.\" 3. All advertising materials mentioning features or use of this software
25.\"    must display the following acknowledgement:
26.\"      This product includes software developed by Christopher G. Demetriou
27.\"      for the NetBSD Project.
28.\" 4. The name of the author may not be used to endorse or promote products
29.\"    derived from this software without specific prior written permission
30.\"
31.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41.\"
42.Dd March 20, 2023
43.Dt REBOOT 9
44.Os
45.Sh NAME
46.Nm kern_reboot ,
47.Nm shutdown_nice
48.Nd reboot, halt, or power off the system
49.Sh SYNOPSIS
50.In sys/types.h
51.In sys/systm.h
52.In sys/reboot.h
53.Vt extern int rebooting;
54.Ft void
55.Fn kern_reboot "int howto"
56.Ft void
57.Fn shutdown_nice "int howto"
58.In sys/eventhandler.h
59.Fn EVENTHANDLER_REGISTER "shutdown_pre_sync" "shutdown_fn" "private" "priority"
60.Fn EVENTHANDLER_REGISTER "shutdown_post_sync" "shutdown_fn" "private" "priority"
61.Fn EVENTHANDLER_REGISTER "shutdown_final" "shutdown_fn" "private" "priority"
62.Sh DESCRIPTION
63The
64.Fn kern_reboot
65function handles final system shutdown, and either halts, reboots,
66or powers down the system.
67The exact action to be taken is determined by the flags passed in
68.Fa howto .
69.Pp
70The relevant flags are:
71.Bl -tag -compact -offset indent -width "RB_POWERCYCLE"
72.It Dv RB_HALT
73Halt the system in-place rather than restarting.
74.It Dv RB_POWEROFF
75Power down the system rather than restarting.
76.It Dv RB_POWERCYCLE
77Request a power-cycle in addition to restarting.
78.It Dv RB_NOSYNC
79Do not sync filesystems during shutdown.
80.It Dv RB_DUMP
81Dump kernel memory during shutdown.
82.El
83.Pp
84The
85.Fa howto
86field, and its full list of flags are described in additional detail by
87.Xr reboot 2 .
88.Pp
89.Fn kern_reboot
90performs the following actions:
91.Bl -enum -offset indent
92.It
93Set the
94.Va rebooting
95variable to
96.Dv 1 ,
97indicating that the reboot process has begun and cannot be stopped.
98.It
99Set the
100.Va kdb_active
101variable to
102.Dv 0 ,
103indicating that execution has left the kernel debugger, if it was previously
104active.
105.It
106Unless the
107.Dv RB_NOSYNC
108flag is set in
109.Fa howto ,
110sync and unmount the system's disks by calling
111.Xr vfs_unmountall 9 .
112.It
113If rebooting after a panic
114.Po
115.Dv RB_DUMP
116is set in
117.Fa howto ,
118but
119.Dv RB_HALT
120is not set
121.Pc ,
122initiate a system crash dump via
123.Fn doadump .
124.It
125Print a message indicating that the system is about to be halted
126or rebooted, and a report of the total system uptime.
127.It
128Execute all registered shutdown hooks.
129See
130.Sx SHUTDOWN HOOKS
131below.
132.It
133As a last resort, if none of the shutdown hooks handled the reboot, call the
134machine-dependent
135.Fn cpu_reset
136function.
137In the unlikely case that this is not supported,
138.Fn kern_reboot
139will loop forever at the end of the function.
140This requires a manual reset of the system.
141.El
142.Pp
143.Fn kern_reboot
144may be called from a typical kernel execution context, when the system is
145running normally.
146It may also be called as the final step of a kernel panic, or from the kernel
147debugger.
148Therefore, the code in this function is subject to restrictions described by
149the
150.Sx EXECUTION CONTEXT
151section of the
152.Xr panic 9
153man page.
154.Pp
155The
156.Fn shutdown_nice
157function is the intended path for performing a clean reboot or shutdown when
158the system is operating under normal conditions.
159Calling this function will send a signal to the
160.Xr init 8
161process, instructing it to perform a shutdown.
162When
163.Xr init 8
164has cleanly terminated its children, it will perform the
165.Xr reboot 2
166system call, which in turn calls
167.Fn kern_reboot .
168.Pp
169If
170.Fn shutdown_nice
171is called before the
172.Xr init 8
173process has been spawned, or if the system has panicked or otherwise halted,
174.Fn kern_reboot
175will be called directly.
176.Sh SHUTDOWN HOOKS
177The system defines three separate
178.Xr EVENTHANDLER 9
179events, which are invoked successively during the shutdown procedure.
180These are
181.Va shutdown_pre_sync ,
182.Va shutdown_post_sync ,
183and
184.Va shutdown_final .
185They will be executed unconditionally in the listed order.
186Handler functions registered to any of these events will receive the value of
187.Fa howto
188as their second argument, which may be used to decide what action to take.
189.Pp
190The
191.Va shutdown_pre_sync
192event is invoked before syncing filesystems to disk.
193It enables any action or state transition that must happen before this point to
194take place.
195.Pp
196The
197.Va shutdown_post_sync
198event is invoked at the point immediately after the filesystem sync has
199finished.
200It enables, for example, disk drivers to complete the sync by flushing their
201cache to disk.
202Note that this event still takes place before the optional kernel core dump.
203.Pp
204The
205.Va shutdown_final
206event is invoked as the very last step of
207.Fn kern_reboot .
208Drivers and subsystems such as
209.Xr acpi 4
210can register handlers to this event that will perform the actual reboot,
211power-off, or halt.
212.Pp
213Notably, the
214.Va shutdown_final
215event is also the point at which all kernel modules will have their shutdown
216.Po
217.Dv MOD_SHUTDOWN
218.Pc
219hooks executed, and when the
220.Xr DEVICE_SHUTDOWN 9
221method will be executed recursively on all devices.
222.Pp
223All event handlers, like
224.Fn kern_reboot
225itself, may be run in either normal shutdown context or a kernel panic or
226debugger context.
227Handler functions are expected to take care not to trigger recursive panics.
228.Sh RETURN VALUES
229The
230.Fn kern_reboot
231function does not return.
232.Pp
233The
234.Fn shutdown_nice
235function will usually return to its caller, having initiated the asynchronous
236system shutdown.
237It will not return when called from a panic or debugger context, or during
238early boot.
239.Sh EXAMPLES
240A hypothetical driver, foo(4), defines a
241.Va shutdown_final
242event handler that can handle system power-off by writing to a device register,
243but it does not handle halt or reset.
244.Bd -literal -offset indent
245void
246foo_poweroff_handler(struct void *arg, int howto)
247{
248        struct foo_softc *sc = arg;
249        uint32_t reg;
250
251        if ((howto & RB_POWEROFF) != 0) {
252                reg = FOO_POWEROFF;
253                WRITE4(sc, FOO_POWEROFF_REG, reg);
254        }
255}
256.Ed
257.Pp
258The handler is then registered in the device attach routine:
259.Bd -literal -offset indent
260int
261foo_attach(device_t dev)
262{
263        struct foo_softc *sc;
264
265        ...
266
267        /* Pass the device's software context as the private arg. */
268        EVENTHANDLER_REGISTER(shutdown_final, foo_poweroff_handler, sc,
269            SHUTDOWN_PRI_DEFAULT);
270
271        ...
272}
273.Ed
274.Pp
275This
276.Va shutdown_final
277handler uses the
278.Dv RB_NOSYNC
279flag to detect that a panic or other unusual condition has occurred, and
280returns early:
281.Bd -literal -offset indent
282void
283bar_shutdown_final(struct void *arg, int howto)
284{
285
286        if ((howto & RB_NOSYNC) != 0)
287                return;
288
289        /* Some code that is not panic-safe. */
290        ...
291}
292.Ed
293.Sh SEE ALSO
294.Xr reboot 2 ,
295.Xr init 8 ,
296.Xr DEVICE_SHUTDOWN 9 ,
297.Xr EVENTHANDLER 9 ,
298.Xr module 9 ,
299.Xr panic 9 ,
300.Xr vfs_unmountall 9
301