1/*-
2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94
39 * $Id: kern_shutdown.c,v 1.47 1999/04/27 11:16:04 phk Exp $
40 */
41
42#include "opt_ddb.h"
43#include "opt_hw_wdog.h"
44#include "opt_panic.h"
45#include "opt_show_busybufs.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/buf.h>
50#include <sys/reboot.h>
51#include <sys/proc.h>
52#include <sys/vnode.h>
53#include <sys/malloc.h>
54#include <sys/kernel.h>
55#include <sys/mount.h>
56#include <sys/queue.h>
57#include <sys/sysctl.h>
58#include <sys/conf.h>
59#include <sys/sysproto.h>
60
61#include <machine/pcb.h>
62#include <machine/clock.h>
63#include <machine/cons.h>
64#include <machine/md_var.h>
65#ifdef SMP
66#include <machine/smp.h> /* smp_active, cpuid */
67#endif
68
69#include <sys/signalvar.h>
70
71#ifndef PANIC_REBOOT_WAIT_TIME
72#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
73#endif
74
75/*
76 * Note that stdarg.h and the ANSI style va_start macro is used for both
77 * ANSI and traditional C compilers.
78 */
79#include <machine/stdarg.h>
80
81#ifdef DDB
82#ifdef DDB_UNATTENDED
83int debugger_on_panic = 0;
84#else
85int debugger_on_panic = 1;
86#endif
87SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
88 &debugger_on_panic, 0, "Run debugger on kernel panic");
89#endif
90
91SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
92
93#ifdef HW_WDOG
94/*
95 * If there is a hardware watchdog, point this at the function needed to
96 * hold it off.
97 * It's needed when the kernel needs to do some lengthy operations.
98 * e.g. in wd.c when dumping core.. It's most annoying to have
99 * your precious core-dump only half written because the wdog kicked in.
100 */
101watchdog_tickle_fn wdog_tickler = NULL;
102#endif /* HW_WDOG */
103
104/*
105 * Variable panicstr contains argument to first call to panic; used as flag
106 * to indicate that the kernel has already called panic.
107 */
108const char *panicstr;
109
110/*
111 * callout list for things to do a shutdown
112 */
113typedef struct shutdown_list_element {
114 LIST_ENTRY(shutdown_list_element) links;
115 bootlist_fn function;
116 void *arg;
117 int priority;
118} *sle_p;
119
120/*
121 * There are three shutdown lists. Some things need to be shut down
122 * earlier than others.
123 */
124LIST_HEAD(shutdown_list, shutdown_list_element);
125
126static struct shutdown_list shutdown_lists[SHUTDOWN_FINAL + 1];
127
128static void boot __P((int)) __dead2;
129static void dumpsys __P((void));
130
131#ifndef _SYS_SYSPROTO_H_
132struct reboot_args {
133 int opt;
134};
135#endif
136/* ARGSUSED */
137
138/*
139 * The system call that results in a reboot
140 */
141int
142reboot(p, uap)
143 struct proc *p;
144 struct reboot_args *uap;
145{
146 int error;
147
148 if ((error = suser(p)))
149 return (error);
150
151 boot(uap->opt);
152 return (0);
153}
154
155/*
156 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC
157 */
158void
159shutdown_nice()
160{
161 /* Send a signal to init(8) and have it shutdown the world */
162 if (initproc != NULL) {
163 psignal(initproc, SIGINT);
164 } else {
165 /* No init(8) running, so simply reboot */
166 boot(RB_NOSYNC);
167 }
168 return;
169}
170static int waittime = -1;
171static struct pcb dumppcb;
172
173/*
174 * Go through the rigmarole of shutting down..
175 * this used to be in machdep.c but I'll be dammned if I could see
176 * anything machine dependant in it.
177 */
178static void
179boot(howto)
180 int howto;
181{
182 sle_p ep;
183
184#ifdef SMP
185 if (smp_active) {
186 printf("boot() called on cpu#%d\n", cpuid);
187 }
188#endif
189 /*
190 * Do any callouts that should be done BEFORE syncing the filesystems.
191 */
192 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_PRE_SYNC], links)
193 (*ep->function)(howto, ep->arg);
194
195 /*
196 * Now sync filesystems
197 */
198 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
199 register struct buf *bp;
200 int iter, nbusy;
201
202 waittime = 0;
203 printf("\nsyncing disks... ");
204
205 sync(&proc0, NULL);
206
207 /*
208 * With soft updates, some buffers that are
209 * written will be remarked as dirty until other
210 * buffers are written.
211 */
212 for (iter = 0; iter < 20; iter++) {
213 nbusy = 0;
214 for (bp = &buf[nbuf]; --bp >= buf; ) {
215 if ((bp->b_flags & (B_BUSY | B_INVAL))
216 == B_BUSY) {
217 nbusy++;
218 } else if ((bp->b_flags & (B_DELWRI | B_INVAL))
219 == B_DELWRI) {
220 /* bawrite(bp);*/
221 nbusy++;
222 }
223 }
224 if (nbusy == 0)
225 break;
226 printf("%d ", nbusy);
227 sync(&proc0, NULL);
228 DELAY(50000 * iter);
229 }
230 /*
231 * Count only busy local buffers to prevent forcing
232 * a fsck if we're just a client of a wedged NFS server
233 */
234 nbusy = 0;
235 for (bp = &buf[nbuf]; --bp >= buf; ) {
236 if (((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY)
237 ||((bp->b_flags & (B_DELWRI | B_INVAL))== B_DELWRI))
238 if(bp->b_dev == NODEV)
239 CIRCLEQ_REMOVE(&mountlist, bp->b_vp->v_mount, mnt_list);
240 else
241 nbusy++;
242
243
244 }
245 if (nbusy) {
246 /*
247 * Failed to sync all blocks. Indicate this and don't
248 * unmount filesystems (thus forcing an fsck on reboot).
249 */
250 printf("giving up\n");
251#ifdef SHOW_BUSYBUFS
252 nbusy = 0;
253 for (bp = &buf[nbuf]; --bp >= buf; ) {
254 if ((bp->b_flags & (B_BUSY | B_INVAL))
255 == B_BUSY) {
256 nbusy++;
257 printf(
258 "%d: dev:%08lx, flags:%08lx, blkno:%ld, lblkno:%ld\n",
259 nbusy, (u_long)bp->b_dev,
260 bp->b_flags, (long)bp->b_blkno,
261 (long)bp->b_lblkno);
262 }
263 }
264 DELAY(5000000); /* 5 seconds */
265#endif
266 } else {
267 printf("done\n");
268 /*
269 * Unmount filesystems
270 */
271 if (panicstr == 0)
272 vfs_unmountall();
273 }
274 DELAY(100000); /* wait for console output to finish */
275 }
276
277 /*
278 * Ok, now do things that assume all filesystem activity has
279 * been completed.
280 */
281 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_POST_SYNC], links)
282 (*ep->function)(howto, ep->arg);
283 splhigh();
284 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) {
285 savectx(&dumppcb);
286#ifdef __i386__
287 dumppcb.pcb_cr3 = rcr3();
288#endif
289 dumpsys();
290 }
291
292 /* Now that we're going to really halt the system... */
293 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_FINAL], links)
294 (*ep->function)(howto, ep->arg);
295
296 if (howto & RB_HALT) {
297 printf("\n");
298 printf("The operating system has halted.\n");
299 printf("Please press any key to reboot.\n\n");
300 switch (cngetc()) {
301 case -1: /* No console, just die */
302 cpu_halt();
303 /* NOTREACHED */
304 default:
305 howto &= ~RB_HALT;
306 break;
307 }
308 } else if (howto & RB_DUMP) {
309 /* System Paniced */
310
311 if (PANIC_REBOOT_WAIT_TIME != 0) {
312 if (PANIC_REBOOT_WAIT_TIME != -1) {
313 int loop;
314 printf("Automatic reboot in %d seconds - "
315 "press a key on the console to abort\n",
316 PANIC_REBOOT_WAIT_TIME);
317 for (loop = PANIC_REBOOT_WAIT_TIME * 10;
318 loop > 0; --loop) {
319 DELAY(1000 * 100); /* 1/10th second */
320 /* Did user type a key? */
321 if (cncheckc() != -1)
322 break;
323 }
324 if (!loop)
325 goto die;
326 }
327 } else { /* zero time specified - reboot NOW */
328 goto die;
329 }
330 printf("--> Press a key on the console to reboot <--\n");
331 cngetc();
332 }
333die:
334 printf("Rebooting...\n");
335 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */
336 /* cpu_boot(howto); */ /* doesn't do anything at the moment */
337 cpu_reset();
338 for(;;) ;
339 /* NOTREACHED */
340}
341
342/*
343 * Magic number for savecore
344 *
345 * exported (symorder) and used at least by savecore(8)
346 *
347 */
348static u_long const dumpmag = 0x8fca0101UL;
349
350static int dumpsize = 0; /* also for savecore */
351
352static int dodump = 1;
353SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW,
354 &dodump, 0, "Do coredump on kernel panic");
355
356/* ARGSUSED */
357static void dump_conf __P((void *dummy));
358static void
359dump_conf(dummy)
360 void *dummy;
361{
362 cpu_dumpconf();
363}
364SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
365
366/*
367 * Doadump comes here after turning off memory management and
368 * getting on the dump stack, either when called above, or by
369 * the auto-restart code.
370 */
371static void
372dumpsys(void)
373{
374
375 if (!dodump)
376 return;
377 if (dumpdev == NODEV)
378 return;
379 if (!(bdevsw[major(dumpdev)]))
380 return;
381 if (!(bdevsw[major(dumpdev)]->d_dump))
382 return;
383 dumpsize = Maxmem;
384 printf("\ndumping to dev %lx, offset %ld\n", (u_long)dumpdev, dumplo);
385 printf("dump ");
386 switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
387
388 case ENXIO:
389 printf("device bad\n");
390 break;
391
392 case EFAULT:
393 printf("device not ready\n");
394 break;
395
396 case EINVAL:
397 printf("area improper\n");
398 break;
399
400 case EIO:
401 printf("i/o error\n");
402 break;
403
404 case EINTR:
405 printf("aborted from console\n");
406 break;
407
408 default:
409 printf("succeeded\n");
410 break;
411 }
412}
413
414/*
415 * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
416 * and then reboots. If we are called twice, then we avoid trying to sync
417 * the disks as this often leads to recursive panics.
418 */
419void
420panic(const char *fmt, ...)
421{
422 int bootopt;
423 va_list ap;
424 static char buf[256];
425
426 bootopt = RB_AUTOBOOT | RB_DUMP;
427 if (panicstr)
428 bootopt |= RB_NOSYNC;
429 else
430 panicstr = fmt;
431
432 va_start(ap, fmt);
433 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
434 if (panicstr == fmt)
435 panicstr = buf;
436 va_end(ap);
437 printf("panic: %s\n", buf);
438#ifdef SMP
439 /* three seperate prints in case of an unmapped page and trap */
440 printf("mp_lock = %08x; ", mp_lock);
441 printf("cpuid = %d; ", cpuid);
442 printf("lapic.id = %08x\n", lapic.id);
443#endif
444
445#if defined(DDB)
446 if (debugger_on_panic)
447 Debugger ("panic");
448#endif
449 boot(bootopt);
450}
451
452/*
453 * Three routines to handle adding/deleting items on the
454 * shutdown callout lists
455 *
456 * at_shutdown():
457 * Take the arguments given and put them onto the shutdown callout list.
458 * However first make sure that it's not already there.
459 * returns 0 on success.
460 */
461int
462at_shutdown(bootlist_fn function, void *arg, int queue)
463{
464 return(at_shutdown_pri(function, arg, queue, SHUTDOWN_PRI_DEFAULT));
465}
466
467/*
468 * at_shutdown_pri():
469 * Take the arguments given and put them onto the shutdown callout list
470 * with the given execution priority.
471 * returns 0 on success.
472 */
473int
474at_shutdown_pri(bootlist_fn function, void *arg, int queue, int pri)
475{
476 sle_p op, ep, ip;
477
478 if (queue < SHUTDOWN_PRE_SYNC
479 || queue > SHUTDOWN_FINAL) {
480 printf("at_shutdown: bad exit callout queue %d specified\n",
481 queue);
482 return (EINVAL);
483 }
484 if (rm_at_shutdown(function, arg))
485 printf("at_shutdown: exit callout entry was already present\n");
486 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
487 if (ep == NULL)
488 return (ENOMEM);
489 ep->function = function;
490 ep->arg = arg;
491 ep->priority = pri;
492
493 /* Sort into list of items on this queue */
494 ip = LIST_FIRST(&shutdown_lists[queue]);
495 if (ip == NULL) {
496 LIST_INSERT_HEAD(&shutdown_lists[queue], ep, links);
497 } else {
498 for (; ip != NULL; op = ip, ip = LIST_NEXT(ip, links)) {
499 if (ep->priority < ip->priority) {
500 LIST_INSERT_BEFORE(ip, ep, links);
501 ep = NULL;
502 break;
503 }
504 }
505 if (ep != NULL)
506 LIST_INSERT_AFTER(op, ep, links);
507 }
508 return (0);
509}
510
511/*
512 * Scan the exit callout lists for the given items and remove them.
513 * Returns the number of items removed.
514 */
515int
516rm_at_shutdown(bootlist_fn function, void *arg)
517{
518 sle_p ep;
519 int count;
520 int queue;
521
522 count = 0;
523 for (queue = SHUTDOWN_PRE_SYNC; queue < SHUTDOWN_FINAL; queue++) {
524 LIST_FOREACH(ep, &shutdown_lists[queue], links) {
525 if ((ep->function == function) && (ep->arg == arg)) {
526 LIST_REMOVE(ep, links);
527 free(ep, M_TEMP);
528 count++;
529 }
530 }
531 }
532 return (count);
533}
534
535/*
536 * Support for poweroff delay.
537 */
538static int poweroff_delay = 0;
539SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
540 &poweroff_delay, 0, "");
541
542static void poweroff_wait(int howto, void *unused)
543{
544 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
545 return;
546 DELAY(poweroff_delay * 1000);
547}
548
549/*
550 * XXX OK? This implies I know SHUTDOWN_PRI_LAST > SHUTDOWN_PRI_FIRST
551 */
552static void poweroff_conf(void *unused)
553{
554 at_shutdown_pri(poweroff_wait, NULL, SHUTDOWN_FINAL, SHUTDOWN_PRI_FIRST);
555}
556
557SYSINIT(poweroff_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, poweroff_conf, NULL)
2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94
39 * $Id: kern_shutdown.c,v 1.47 1999/04/27 11:16:04 phk Exp $
40 */
41
42#include "opt_ddb.h"
43#include "opt_hw_wdog.h"
44#include "opt_panic.h"
45#include "opt_show_busybufs.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/buf.h>
50#include <sys/reboot.h>
51#include <sys/proc.h>
52#include <sys/vnode.h>
53#include <sys/malloc.h>
54#include <sys/kernel.h>
55#include <sys/mount.h>
56#include <sys/queue.h>
57#include <sys/sysctl.h>
58#include <sys/conf.h>
59#include <sys/sysproto.h>
60
61#include <machine/pcb.h>
62#include <machine/clock.h>
63#include <machine/cons.h>
64#include <machine/md_var.h>
65#ifdef SMP
66#include <machine/smp.h> /* smp_active, cpuid */
67#endif
68
69#include <sys/signalvar.h>
70
71#ifndef PANIC_REBOOT_WAIT_TIME
72#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
73#endif
74
75/*
76 * Note that stdarg.h and the ANSI style va_start macro is used for both
77 * ANSI and traditional C compilers.
78 */
79#include <machine/stdarg.h>
80
81#ifdef DDB
82#ifdef DDB_UNATTENDED
83int debugger_on_panic = 0;
84#else
85int debugger_on_panic = 1;
86#endif
87SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
88 &debugger_on_panic, 0, "Run debugger on kernel panic");
89#endif
90
91SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
92
93#ifdef HW_WDOG
94/*
95 * If there is a hardware watchdog, point this at the function needed to
96 * hold it off.
97 * It's needed when the kernel needs to do some lengthy operations.
98 * e.g. in wd.c when dumping core.. It's most annoying to have
99 * your precious core-dump only half written because the wdog kicked in.
100 */
101watchdog_tickle_fn wdog_tickler = NULL;
102#endif /* HW_WDOG */
103
104/*
105 * Variable panicstr contains argument to first call to panic; used as flag
106 * to indicate that the kernel has already called panic.
107 */
108const char *panicstr;
109
110/*
111 * callout list for things to do a shutdown
112 */
113typedef struct shutdown_list_element {
114 LIST_ENTRY(shutdown_list_element) links;
115 bootlist_fn function;
116 void *arg;
117 int priority;
118} *sle_p;
119
120/*
121 * There are three shutdown lists. Some things need to be shut down
122 * earlier than others.
123 */
124LIST_HEAD(shutdown_list, shutdown_list_element);
125
126static struct shutdown_list shutdown_lists[SHUTDOWN_FINAL + 1];
127
128static void boot __P((int)) __dead2;
129static void dumpsys __P((void));
130
131#ifndef _SYS_SYSPROTO_H_
132struct reboot_args {
133 int opt;
134};
135#endif
136/* ARGSUSED */
137
138/*
139 * The system call that results in a reboot
140 */
141int
142reboot(p, uap)
143 struct proc *p;
144 struct reboot_args *uap;
145{
146 int error;
147
148 if ((error = suser(p)))
149 return (error);
150
151 boot(uap->opt);
152 return (0);
153}
154
155/*
156 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC
157 */
158void
159shutdown_nice()
160{
161 /* Send a signal to init(8) and have it shutdown the world */
162 if (initproc != NULL) {
163 psignal(initproc, SIGINT);
164 } else {
165 /* No init(8) running, so simply reboot */
166 boot(RB_NOSYNC);
167 }
168 return;
169}
170static int waittime = -1;
171static struct pcb dumppcb;
172
173/*
174 * Go through the rigmarole of shutting down..
175 * this used to be in machdep.c but I'll be dammned if I could see
176 * anything machine dependant in it.
177 */
178static void
179boot(howto)
180 int howto;
181{
182 sle_p ep;
183
184#ifdef SMP
185 if (smp_active) {
186 printf("boot() called on cpu#%d\n", cpuid);
187 }
188#endif
189 /*
190 * Do any callouts that should be done BEFORE syncing the filesystems.
191 */
192 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_PRE_SYNC], links)
193 (*ep->function)(howto, ep->arg);
194
195 /*
196 * Now sync filesystems
197 */
198 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
199 register struct buf *bp;
200 int iter, nbusy;
201
202 waittime = 0;
203 printf("\nsyncing disks... ");
204
205 sync(&proc0, NULL);
206
207 /*
208 * With soft updates, some buffers that are
209 * written will be remarked as dirty until other
210 * buffers are written.
211 */
212 for (iter = 0; iter < 20; iter++) {
213 nbusy = 0;
214 for (bp = &buf[nbuf]; --bp >= buf; ) {
215 if ((bp->b_flags & (B_BUSY | B_INVAL))
216 == B_BUSY) {
217 nbusy++;
218 } else if ((bp->b_flags & (B_DELWRI | B_INVAL))
219 == B_DELWRI) {
220 /* bawrite(bp);*/
221 nbusy++;
222 }
223 }
224 if (nbusy == 0)
225 break;
226 printf("%d ", nbusy);
227 sync(&proc0, NULL);
228 DELAY(50000 * iter);
229 }
230 /*
231 * Count only busy local buffers to prevent forcing
232 * a fsck if we're just a client of a wedged NFS server
233 */
234 nbusy = 0;
235 for (bp = &buf[nbuf]; --bp >= buf; ) {
236 if (((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY)
237 ||((bp->b_flags & (B_DELWRI | B_INVAL))== B_DELWRI))
238 if(bp->b_dev == NODEV)
239 CIRCLEQ_REMOVE(&mountlist, bp->b_vp->v_mount, mnt_list);
240 else
241 nbusy++;
242
243
244 }
245 if (nbusy) {
246 /*
247 * Failed to sync all blocks. Indicate this and don't
248 * unmount filesystems (thus forcing an fsck on reboot).
249 */
250 printf("giving up\n");
251#ifdef SHOW_BUSYBUFS
252 nbusy = 0;
253 for (bp = &buf[nbuf]; --bp >= buf; ) {
254 if ((bp->b_flags & (B_BUSY | B_INVAL))
255 == B_BUSY) {
256 nbusy++;
257 printf(
258 "%d: dev:%08lx, flags:%08lx, blkno:%ld, lblkno:%ld\n",
259 nbusy, (u_long)bp->b_dev,
260 bp->b_flags, (long)bp->b_blkno,
261 (long)bp->b_lblkno);
262 }
263 }
264 DELAY(5000000); /* 5 seconds */
265#endif
266 } else {
267 printf("done\n");
268 /*
269 * Unmount filesystems
270 */
271 if (panicstr == 0)
272 vfs_unmountall();
273 }
274 DELAY(100000); /* wait for console output to finish */
275 }
276
277 /*
278 * Ok, now do things that assume all filesystem activity has
279 * been completed.
280 */
281 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_POST_SYNC], links)
282 (*ep->function)(howto, ep->arg);
283 splhigh();
284 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) {
285 savectx(&dumppcb);
286#ifdef __i386__
287 dumppcb.pcb_cr3 = rcr3();
288#endif
289 dumpsys();
290 }
291
292 /* Now that we're going to really halt the system... */
293 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_FINAL], links)
294 (*ep->function)(howto, ep->arg);
295
296 if (howto & RB_HALT) {
297 printf("\n");
298 printf("The operating system has halted.\n");
299 printf("Please press any key to reboot.\n\n");
300 switch (cngetc()) {
301 case -1: /* No console, just die */
302 cpu_halt();
303 /* NOTREACHED */
304 default:
305 howto &= ~RB_HALT;
306 break;
307 }
308 } else if (howto & RB_DUMP) {
309 /* System Paniced */
310
311 if (PANIC_REBOOT_WAIT_TIME != 0) {
312 if (PANIC_REBOOT_WAIT_TIME != -1) {
313 int loop;
314 printf("Automatic reboot in %d seconds - "
315 "press a key on the console to abort\n",
316 PANIC_REBOOT_WAIT_TIME);
317 for (loop = PANIC_REBOOT_WAIT_TIME * 10;
318 loop > 0; --loop) {
319 DELAY(1000 * 100); /* 1/10th second */
320 /* Did user type a key? */
321 if (cncheckc() != -1)
322 break;
323 }
324 if (!loop)
325 goto die;
326 }
327 } else { /* zero time specified - reboot NOW */
328 goto die;
329 }
330 printf("--> Press a key on the console to reboot <--\n");
331 cngetc();
332 }
333die:
334 printf("Rebooting...\n");
335 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */
336 /* cpu_boot(howto); */ /* doesn't do anything at the moment */
337 cpu_reset();
338 for(;;) ;
339 /* NOTREACHED */
340}
341
342/*
343 * Magic number for savecore
344 *
345 * exported (symorder) and used at least by savecore(8)
346 *
347 */
348static u_long const dumpmag = 0x8fca0101UL;
349
350static int dumpsize = 0; /* also for savecore */
351
352static int dodump = 1;
353SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW,
354 &dodump, 0, "Do coredump on kernel panic");
355
356/* ARGSUSED */
357static void dump_conf __P((void *dummy));
358static void
359dump_conf(dummy)
360 void *dummy;
361{
362 cpu_dumpconf();
363}
364SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
365
366/*
367 * Doadump comes here after turning off memory management and
368 * getting on the dump stack, either when called above, or by
369 * the auto-restart code.
370 */
371static void
372dumpsys(void)
373{
374
375 if (!dodump)
376 return;
377 if (dumpdev == NODEV)
378 return;
379 if (!(bdevsw[major(dumpdev)]))
380 return;
381 if (!(bdevsw[major(dumpdev)]->d_dump))
382 return;
383 dumpsize = Maxmem;
384 printf("\ndumping to dev %lx, offset %ld\n", (u_long)dumpdev, dumplo);
385 printf("dump ");
386 switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
387
388 case ENXIO:
389 printf("device bad\n");
390 break;
391
392 case EFAULT:
393 printf("device not ready\n");
394 break;
395
396 case EINVAL:
397 printf("area improper\n");
398 break;
399
400 case EIO:
401 printf("i/o error\n");
402 break;
403
404 case EINTR:
405 printf("aborted from console\n");
406 break;
407
408 default:
409 printf("succeeded\n");
410 break;
411 }
412}
413
414/*
415 * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
416 * and then reboots. If we are called twice, then we avoid trying to sync
417 * the disks as this often leads to recursive panics.
418 */
419void
420panic(const char *fmt, ...)
421{
422 int bootopt;
423 va_list ap;
424 static char buf[256];
425
426 bootopt = RB_AUTOBOOT | RB_DUMP;
427 if (panicstr)
428 bootopt |= RB_NOSYNC;
429 else
430 panicstr = fmt;
431
432 va_start(ap, fmt);
433 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
434 if (panicstr == fmt)
435 panicstr = buf;
436 va_end(ap);
437 printf("panic: %s\n", buf);
438#ifdef SMP
439 /* three seperate prints in case of an unmapped page and trap */
440 printf("mp_lock = %08x; ", mp_lock);
441 printf("cpuid = %d; ", cpuid);
442 printf("lapic.id = %08x\n", lapic.id);
443#endif
444
445#if defined(DDB)
446 if (debugger_on_panic)
447 Debugger ("panic");
448#endif
449 boot(bootopt);
450}
451
452/*
453 * Three routines to handle adding/deleting items on the
454 * shutdown callout lists
455 *
456 * at_shutdown():
457 * Take the arguments given and put them onto the shutdown callout list.
458 * However first make sure that it's not already there.
459 * returns 0 on success.
460 */
461int
462at_shutdown(bootlist_fn function, void *arg, int queue)
463{
464 return(at_shutdown_pri(function, arg, queue, SHUTDOWN_PRI_DEFAULT));
465}
466
467/*
468 * at_shutdown_pri():
469 * Take the arguments given and put them onto the shutdown callout list
470 * with the given execution priority.
471 * returns 0 on success.
472 */
473int
474at_shutdown_pri(bootlist_fn function, void *arg, int queue, int pri)
475{
476 sle_p op, ep, ip;
477
478 if (queue < SHUTDOWN_PRE_SYNC
479 || queue > SHUTDOWN_FINAL) {
480 printf("at_shutdown: bad exit callout queue %d specified\n",
481 queue);
482 return (EINVAL);
483 }
484 if (rm_at_shutdown(function, arg))
485 printf("at_shutdown: exit callout entry was already present\n");
486 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
487 if (ep == NULL)
488 return (ENOMEM);
489 ep->function = function;
490 ep->arg = arg;
491 ep->priority = pri;
492
493 /* Sort into list of items on this queue */
494 ip = LIST_FIRST(&shutdown_lists[queue]);
495 if (ip == NULL) {
496 LIST_INSERT_HEAD(&shutdown_lists[queue], ep, links);
497 } else {
498 for (; ip != NULL; op = ip, ip = LIST_NEXT(ip, links)) {
499 if (ep->priority < ip->priority) {
500 LIST_INSERT_BEFORE(ip, ep, links);
501 ep = NULL;
502 break;
503 }
504 }
505 if (ep != NULL)
506 LIST_INSERT_AFTER(op, ep, links);
507 }
508 return (0);
509}
510
511/*
512 * Scan the exit callout lists for the given items and remove them.
513 * Returns the number of items removed.
514 */
515int
516rm_at_shutdown(bootlist_fn function, void *arg)
517{
518 sle_p ep;
519 int count;
520 int queue;
521
522 count = 0;
523 for (queue = SHUTDOWN_PRE_SYNC; queue < SHUTDOWN_FINAL; queue++) {
524 LIST_FOREACH(ep, &shutdown_lists[queue], links) {
525 if ((ep->function == function) && (ep->arg == arg)) {
526 LIST_REMOVE(ep, links);
527 free(ep, M_TEMP);
528 count++;
529 }
530 }
531 }
532 return (count);
533}
534
535/*
536 * Support for poweroff delay.
537 */
538static int poweroff_delay = 0;
539SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
540 &poweroff_delay, 0, "");
541
542static void poweroff_wait(int howto, void *unused)
543{
544 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
545 return;
546 DELAY(poweroff_delay * 1000);
547}
548
549/*
550 * XXX OK? This implies I know SHUTDOWN_PRI_LAST > SHUTDOWN_PRI_FIRST
551 */
552static void poweroff_conf(void *unused)
553{
554 at_shutdown_pri(poweroff_wait, NULL, SHUTDOWN_FINAL, SHUTDOWN_PRI_FIRST);
555}
556
557SYSINIT(poweroff_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, poweroff_conf, NULL)