1*f36002f2Sthorpej /* $NetBSD: kern_reboot.c,v 1.5 2024/03/05 14:15:36 thorpej Exp $ */
2ca332959Smrg
3ca332959Smrg /*
4ca332959Smrg * Copyright (c) 1982, 1986, 1989, 1993
5ca332959Smrg * The Regents of the University of California. All rights reserved.
6ca332959Smrg *
7ca332959Smrg * Redistribution and use in source and binary forms, with or without
8ca332959Smrg * modification, are permitted provided that the following conditions
9ca332959Smrg * are met:
10ca332959Smrg * 1. Redistributions of source code must retain the above copyright
11ca332959Smrg * notice, this list of conditions and the following disclaimer.
12ca332959Smrg * 2. Redistributions in binary form must reproduce the above copyright
13ca332959Smrg * notice, this list of conditions and the following disclaimer in the
14ca332959Smrg * documentation and/or other materials provided with the distribution.
15ca332959Smrg * 3. Neither the name of the University nor the names of its contributors
16ca332959Smrg * may be used to endorse or promote products derived from this software
17ca332959Smrg * without specific prior written permission.
18ca332959Smrg *
19ca332959Smrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20ca332959Smrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ca332959Smrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ca332959Smrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23ca332959Smrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ca332959Smrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ca332959Smrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ca332959Smrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ca332959Smrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ca332959Smrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ca332959Smrg * SUCH DAMAGE.
30ca332959Smrg *
31ca332959Smrg * @(#)kern_xxx.c 8.3 (Berkeley) 2/14/95
32ca332959Smrg * from: NetBSD: kern_xxx.c,v 1.74 2017/10/28 00:37:11 pgoyette Exp
33ca332959Smrg */
34ca332959Smrg
35ca332959Smrg #include <sys/cdefs.h>
36*f36002f2Sthorpej __KERNEL_RCSID(0, "$NetBSD: kern_reboot.c,v 1.5 2024/03/05 14:15:36 thorpej Exp $");
37ca332959Smrg
38566d6455Sad #include <sys/atomic.h>
39ca332959Smrg #include <sys/param.h>
40ca332959Smrg #include <sys/systm.h>
41ca332959Smrg #include <sys/proc.h>
42ca332959Smrg #include <sys/reboot.h>
43ca332959Smrg #include <sys/syscall.h>
44ca332959Smrg #include <sys/syscallargs.h>
45599c2405Sthorpej #include <sys/kernel.h>
46ca332959Smrg #include <sys/kauth.h>
47*f36002f2Sthorpej #include <sys/timevar.h>
48ca332959Smrg
49599c2405Sthorpej /*
50599c2405Sthorpej * Reboot / shutdown the system.
51599c2405Sthorpej */
52599c2405Sthorpej void
kern_reboot(int howto,char * bootstr)53599c2405Sthorpej kern_reboot(int howto, char *bootstr)
54599c2405Sthorpej {
55e3b9ca63Sad static lwp_t *rebooter;
56e3b9ca63Sad lwp_t *l;
57599c2405Sthorpej
58566d6455Sad /*
59566d6455Sad * If already rebooting then just hang out. Allow reentry for the
60566d6455Sad * benfit of ddb, even if questionable. It would be better to call
61566d6455Sad * exit1(), but this is called from all sorts of places.
62566d6455Sad */
63566d6455Sad l = atomic_cas_ptr(&rebooter, NULL, curlwp);
64566d6455Sad while (l != NULL && l != curlwp) {
65566d6455Sad (void)kpause("reboot", true, 0, NULL);
66566d6455Sad }
67599c2405Sthorpej shutting_down = 1;
68599c2405Sthorpej
69*f36002f2Sthorpej /* Just cut to the chase if cold. */
70*f36002f2Sthorpej if (cold) {
71*f36002f2Sthorpej goto do_cpu_reboot;
72*f36002f2Sthorpej }
73*f36002f2Sthorpej
74*f36002f2Sthorpej if ((boothowto & RB_NOSYNC) == 0 && panicstr == NULL) {
75*f36002f2Sthorpej /*
76*f36002f2Sthorpej * If we've been adjusting the clock, the todr
77*f36002f2Sthorpej * will be out of synch; adjust it now.
78*f36002f2Sthorpej */
79*f36002f2Sthorpej if (time_adjusted != 0) {
80*f36002f2Sthorpej time_adjusted = 0;
81*f36002f2Sthorpej resettodr();
82*f36002f2Sthorpej }
83*f36002f2Sthorpej }
84*f36002f2Sthorpej
85599c2405Sthorpej /*
86599c2405Sthorpej * XXX We should re-factor out all of the common stuff
87599c2405Sthorpej * that each and every cpu_reboot() does and put it here.
88599c2405Sthorpej */
89599c2405Sthorpej
90*f36002f2Sthorpej do_cpu_reboot:
91599c2405Sthorpej cpu_reboot(howto, bootstr);
92599c2405Sthorpej }
93599c2405Sthorpej
94ca332959Smrg /* ARGSUSED */
95ca332959Smrg int
sys_reboot(struct lwp * l,const struct sys_reboot_args * uap,register_t * retval)96ca332959Smrg sys_reboot(struct lwp *l, const struct sys_reboot_args *uap, register_t *retval)
97ca332959Smrg {
98ca332959Smrg /* {
99ca332959Smrg syscallarg(int) opt;
100ca332959Smrg syscallarg(char *) bootstr;
101ca332959Smrg } */
102ca332959Smrg int error;
103ca332959Smrg char *bootstr, bs[128];
104ca332959Smrg
105ca332959Smrg if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_REBOOT,
106ca332959Smrg 0, NULL, NULL, NULL)) != 0)
107ca332959Smrg return (error);
108ca332959Smrg
109ca332959Smrg /*
110ca332959Smrg * Only use the boot string if RB_STRING is set.
111ca332959Smrg */
112ca332959Smrg if ((SCARG(uap, opt) & RB_STRING) &&
113ca332959Smrg (error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0)) == 0)
114ca332959Smrg bootstr = bs;
115ca332959Smrg else
116ca332959Smrg bootstr = NULL;
117ca332959Smrg /*
118ca332959Smrg * Not all ports use the bootstr currently.
119ca332959Smrg */
120599c2405Sthorpej kern_reboot(SCARG(uap, opt), bootstr);
121ca332959Smrg return (0);
122ca332959Smrg }
123