Replace NOCDEV with NULL. NOCDEV was ((void *)-1) and as inherited
[games.git] / sys / kern / kern_shutdown.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/kern/kern_shutdown.c,v 1.72.2.12 2002/02/21 19:15:10 dillon Exp $
028066b1 40 * $DragonFly: src/sys/kern/kern_shutdown.c,v 1.51 2007/05/08 02:31:42 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_ddb.h"
b1e04573 44#include "opt_ddb_trace.h"
984263bc
MD
45#include "opt_hw_wdog.h"
46#include "opt_panic.h"
47#include "opt_show_busybufs.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/eventhandler.h>
52#include <sys/buf.h>
53#include <sys/disklabel.h>
54#include <sys/reboot.h>
55#include <sys/proc.h>
56#include <sys/vnode.h>
57#include <sys/kernel.h>
58#include <sys/kthread.h>
59#include <sys/malloc.h>
60#include <sys/mount.h>
61#include <sys/queue.h>
62#include <sys/sysctl.h>
f6a22062 63#include <sys/vkernel.h>
984263bc
MD
64#include <sys/conf.h>
65#include <sys/sysproto.h>
335dda38 66#include <sys/device.h>
984263bc 67#include <sys/cons.h>
f6a22062
MD
68#include <sys/shm.h>
69#include <sys/kern_syscall.h>
70#include <vm/vm_map.h>
71#include <vm/pmap.h>
72
9ec81f83 73#include <sys/thread2.h>
3020e3be 74#include <sys/buf2.h>
984263bc
MD
75
76#include <machine/pcb.h>
77#include <machine/clock.h>
78#include <machine/md_var.h>
0f7a3396 79#include <machine/smp.h> /* smp_active_mask, cpuid */
c439ad8f 80#include <machine/vmparam.h>
984263bc
MD
81
82#include <sys/signalvar.h>
83
84#ifndef PANIC_REBOOT_WAIT_TIME
85#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
86#endif
87
88/*
89 * Note that stdarg.h and the ANSI style va_start macro is used for both
e2565a42
MD
90 * ANSI and traditional C compilers. We use the machine version to stay
91 * within the confines of the kernel header files.
984263bc
MD
92 */
93#include <machine/stdarg.h>
94
95#ifdef DDB
96#ifdef DDB_UNATTENDED
97int debugger_on_panic = 0;
98#else
99int debugger_on_panic = 1;
100#endif
101SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
102 &debugger_on_panic, 0, "Run debugger on kernel panic");
b1e04573
JH
103
104extern void db_print_backtrace(void);
105
106#ifdef DDB_TRACE
107int trace_on_panic = 1;
108#else
109int trace_on_panic = 0;
110#endif
111SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW,
112 &trace_on_panic, 0, "Print stack trace on kernel panic");
984263bc
MD
113#endif
114
baf88b6b
MD
115static int sync_on_panic = 1;
116SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW,
117 &sync_on_panic, 0, "Do a sync before rebooting from a panic");
118
984263bc
MD
119SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
120
121#ifdef HW_WDOG
122/*
123 * If there is a hardware watchdog, point this at the function needed to
124 * hold it off.
125 * It's needed when the kernel needs to do some lengthy operations.
126 * e.g. in wd.c when dumping core.. It's most annoying to have
127 * your precious core-dump only half written because the wdog kicked in.
128 */
129watchdog_tickle_fn wdog_tickler = NULL;
130#endif /* HW_WDOG */
131
132/*
133 * Variable panicstr contains argument to first call to panic; used as flag
134 * to indicate that the kernel has already called panic.
135 */
136const char *panicstr;
137
138int dumping; /* system is dumping */
ddcc5e10
MD
139#ifdef SMP
140u_int panic_cpu_interlock; /* panic interlock */
141globaldata_t panic_cpu_gd; /* which cpu took the panic */
142#endif
984263bc 143
a8a86718
MD
144int bootverbose = 0; /* note: assignment to force non-bss */
145int cold = 1; /* note: assignment to force non-bss */
1184892f 146long dumplo;
a8a86718 147
402ed7e1
RG
148static void boot (int) __dead2;
149static void dumpsys (void);
b13267a5 150static int setdumpdev (cdev_t dev);
402ed7e1
RG
151static void poweroff_wait (void *, int);
152static void print_uptime (void);
153static void shutdown_halt (void *junk, int howto);
154static void shutdown_panic (void *junk, int howto);
155static void shutdown_reset (void *junk, int howto);
41cbf46c
MD
156static int shutdown_busycount1(struct buf *bp, void *info);
157static int shutdown_busycount2(struct buf *bp, void *info);
f6a22062 158static void shutdown_cleanup_proc(struct proc *p);
984263bc
MD
159
160/* register various local shutdown events */
161static void
162shutdown_conf(void *unused)
163{
164 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST);
165 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100);
166 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100);
167 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200);
168}
169
ba39e2e0 170SYSINIT(shutdown_conf, SI_BOOT2_MACHDEP, SI_ORDER_ANY, shutdown_conf, NULL)
984263bc
MD
171
172/* ARGSUSED */
173
174/*
175 * The system call that results in a reboot
176 */
177int
753fd850 178sys_reboot(struct reboot_args *uap)
984263bc 179{
dadab5e9 180 struct thread *td = curthread;
984263bc
MD
181 int error;
182
dadab5e9 183 if ((error = suser(td)))
984263bc
MD
184 return (error);
185
186 boot(uap->opt);
187 return (0);
188}
189
190/*
191 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC
192 */
193static int shutdown_howto = 0;
194
195void
196shutdown_nice(int howto)
197{
198 shutdown_howto = howto;
199
200 /* Send a signal to init(8) and have it shutdown the world */
201 if (initproc != NULL) {
84204577 202 ksignal(initproc, SIGINT);
984263bc
MD
203 } else {
204 /* No init(8) running, so simply reboot */
205 boot(RB_NOSYNC);
206 }
207 return;
208}
209static int waittime = -1;
8b6a428f 210static struct thread *dumpthread;
984263bc
MD
211static struct pcb dumppcb;
212
213static void
c972a82f 214print_uptime(void)
984263bc
MD
215{
216 int f;
217 struct timespec ts;
218
219 getnanouptime(&ts);
6ea70f76 220 kprintf("Uptime: ");
984263bc
MD
221 f = 0;
222 if (ts.tv_sec >= 86400) {
6ea70f76 223 kprintf("%ldd", ts.tv_sec / 86400);
984263bc
MD
224 ts.tv_sec %= 86400;
225 f = 1;
226 }
227 if (f || ts.tv_sec >= 3600) {
6ea70f76 228 kprintf("%ldh", ts.tv_sec / 3600);
984263bc
MD
229 ts.tv_sec %= 3600;
230 f = 1;
231 }
232 if (f || ts.tv_sec >= 60) {
6ea70f76 233 kprintf("%ldm", ts.tv_sec / 60);
984263bc
MD
234 ts.tv_sec %= 60;
235 f = 1;
236 }
6ea70f76 237 kprintf("%lds\n", ts.tv_sec);
984263bc
MD
238}
239
240/*
241 * Go through the rigmarole of shutting down..
242 * this used to be in machdep.c but I'll be dammned if I could see
243 * anything machine dependant in it.
244 */
245static void
41c20dac 246boot(int howto)
984263bc 247{
3ad8cc28
MD
248 /*
249 * Get rid of any user scheduler baggage and then give
250 * us a high priority.
251 */
252 if (curthread->td_release)
253 curthread->td_release(curthread);
254 lwkt_setpri_self(TDPRI_MAX);
984263bc
MD
255
256 /* collect extra flags that shutdown_nice might have set */
257 howto |= shutdown_howto;
258
259#ifdef SMP
7710b6e4
MD
260 /*
261 * We really want to shutdown on the BSP. Subsystems such as ACPI
262 * can't power-down the box otherwise.
263 */
0f7a3396 264 if (smp_active_mask > 1) {
6ea70f76 265 kprintf("boot() called on cpu#%d\n", mycpu->gd_cpuid);
984263bc 266 }
7710b6e4 267 if (panicstr == NULL && mycpu->gd_cpuid != 0) {
6ea70f76 268 kprintf("Switching to cpu #0 for shutdown\n");
7710b6e4
MD
269 lwkt_setcpu_self(globaldata_find(0));
270 }
984263bc
MD
271#endif
272 /*
273 * Do any callouts that should be done BEFORE syncing the filesystems.
274 */
275 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
276
f6a22062
MD
277 /*
278 * Try to get rid of any remaining FS references. The calling
279 * process, proc0, and init may still hold references. The
280 * VFS cache subsystem may still hold a root reference to root.
ee92d6d6
MD
281 *
282 * XXX this needs work. We really need to SIGSTOP all remaining
283 * processes in order to avoid blowups due to proc0's filesystem
284 * references going away. For now just make sure that the init
285 * process is stopped.
f6a22062
MD
286 */
287 if (panicstr == NULL) {
288 shutdown_cleanup_proc(curproc);
289 shutdown_cleanup_proc(&proc0);
ee92d6d6
MD
290 if (initproc) {
291 if (initproc != curproc) {
292 ksignal(initproc, SIGSTOP);
293 tsleep(boot, 0, "shutdn", hz / 20);
294 }
f6a22062 295 shutdown_cleanup_proc(initproc);
ee92d6d6 296 }
f6a22062
MD
297 vfs_cache_setroot(NULL, NULL);
298 }
299
984263bc
MD
300 /*
301 * Now sync filesystems
302 */
303 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
984263bc
MD
304 int iter, nbusy, pbusy;
305
306 waittime = 0;
6ea70f76 307 kprintf("\nsyncing disks... ");
984263bc 308
753fd850 309 sys_sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */
984263bc
MD
310
311 /*
312 * With soft updates, some buffers that are
313 * written will be remarked as dirty until other
314 * buffers are written.
315 */
316 for (iter = pbusy = 0; iter < 20; iter++) {
41cbf46c 317 nbusy = scan_all_buffers(shutdown_busycount1, NULL);
984263bc
MD
318 if (nbusy == 0)
319 break;
6ea70f76 320 kprintf("%d ", nbusy);
984263bc
MD
321 if (nbusy < pbusy)
322 iter = 0;
323 pbusy = nbusy;
e200c40d
DR
324 /*
325 * XXX:
326 * Process soft update work queue if buffers don't sync
327 * after 6 iterations by permitting the syncer to run.
328 */
984263bc
MD
329 if (iter > 5 && bioops.io_sync)
330 (*bioops.io_sync)(NULL);
753fd850 331 sys_sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */
3ad8cc28 332 tsleep(boot, 0, "shutdn", hz * iter / 20 + 1);
984263bc 333 }
6ea70f76 334 kprintf("\n");
984263bc
MD
335 /*
336 * Count only busy local buffers to prevent forcing
337 * a fsck if we're just a client of a wedged NFS server
338 */
41cbf46c 339 nbusy = scan_all_buffers(shutdown_busycount2, NULL);
984263bc
MD
340 if (nbusy) {
341 /*
342 * Failed to sync all blocks. Indicate this and don't
343 * unmount filesystems (thus forcing an fsck on reboot).
344 */
6ea70f76 345 kprintf("giving up on %d buffers\n", nbusy);
a88429e8
DR
346#ifdef DDB
347 Debugger("busy buffer problem");
348#endif /* DDB */
3ad8cc28 349 tsleep(boot, 0, "shutdn", hz * 5 + 1);
984263bc 350 } else {
6ea70f76 351 kprintf("done\n");
984263bc
MD
352 /*
353 * Unmount filesystems
354 */
f6a22062 355 if (panicstr == NULL)
984263bc
MD
356 vfs_unmountall();
357 }
3ad8cc28 358 tsleep(boot, 0, "shutdn", hz / 10 + 1);
984263bc
MD
359 }
360
361 print_uptime();
362
363 /*
364 * Ok, now do things that assume all filesystem activity has
365 * been completed.
366 */
367 EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
9ec81f83 368 crit_enter();
984263bc
MD
369 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold)
370 dumpsys();
371
372 /* Now that we're going to really halt the system... */
373 EVENTHANDLER_INVOKE(shutdown_final, howto);
374
375 for(;;) ; /* safety against shutdown_reset not working */
376 /* NOTREACHED */
377}
378
41cbf46c
MD
379static int
380shutdown_busycount1(struct buf *bp, void *info)
381{
382 if ((bp->b_flags & B_INVAL) == 0 && BUF_REFCNT(bp) > 0)
383 return(1);
384 if ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI)
385 return (1);
386 return (0);
387}
388
389static int
390shutdown_busycount2(struct buf *bp, void *info)
391{
392 if (((bp->b_flags & B_INVAL) == 0 && BUF_REFCNT(bp)) ||
393 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
394 /*
395 * Only count buffers undergoing write I/O
396 * on the related vnode.
397 */
398 if (bp->b_vp == NULL ||
399 bp->b_vp->v_track_write.bk_active == 0) {
400 return (0);
401 }
402#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
6ea70f76 403 kprintf(
ce54c037 404 "%p dev:?, flags:%08x, loffset:%lld, doffset:%lld\n",
2dc6c129 405 bp,
41cbf46c
MD
406 bp->b_flags, bp->b_loffset,
407 bp->b_bio2.bio_offset);
408#endif
409 return(1);
410 }
411 return(0);
412}
413
984263bc
MD
414/*
415 * If the shutdown was a clean halt, behave accordingly.
416 */
417static void
418shutdown_halt(void *junk, int howto)
419{
420 if (howto & RB_HALT) {
6ea70f76
SW
421 kprintf("\n");
422 kprintf("The operating system has halted.\n");
423 kprintf("Please press any key to reboot.\n\n");
984263bc
MD
424 switch (cngetc()) {
425 case -1: /* No console, just die */
426 cpu_halt();
427 /* NOTREACHED */
428 default:
429 howto &= ~RB_HALT;
430 break;
431 }
432 }
433}
434
435/*
436 * Check to see if the system paniced, pause and then reboot
437 * according to the specified delay.
438 */
439static void
440shutdown_panic(void *junk, int howto)
441{
442 int loop;
443
444 if (howto & RB_DUMP) {
445 if (PANIC_REBOOT_WAIT_TIME != 0) {
446 if (PANIC_REBOOT_WAIT_TIME != -1) {
6ea70f76 447 kprintf("Automatic reboot in %d seconds - "
984263bc
MD
448 "press a key on the console to abort\n",
449 PANIC_REBOOT_WAIT_TIME);
450 for (loop = PANIC_REBOOT_WAIT_TIME * 10;
451 loop > 0; --loop) {
452 DELAY(1000 * 100); /* 1/10th second */
453 /* Did user type a key? */
454 if (cncheckc() != -1)
455 break;
456 }
457 if (!loop)
458 return;
459 }
460 } else { /* zero time specified - reboot NOW */
461 return;
462 }
6ea70f76
SW
463 kprintf("--> Press a key on the console to reboot,\n");
464 kprintf("--> or switch off the system now.\n");
984263bc
MD
465 cngetc();
466 }
467}
468
469/*
470 * Everything done, now reset
471 */
472static void
473shutdown_reset(void *junk, int howto)
474{
6ea70f76
SW
475 kprintf("Rebooting...\n");
476 DELAY(1000000); /* wait 1 sec for kprintf's to complete and be read */
984263bc
MD
477 /* cpu_boot(howto); */ /* doesn't do anything at the moment */
478 cpu_reset();
479 /* NOTREACHED */ /* assuming reset worked */
480}
481
f6a22062
MD
482/*
483 * Try to remove FS references in the specified process. This function
484 * is used during shutdown
485 */
486static
487void
488shutdown_cleanup_proc(struct proc *p)
489{
490 struct filedesc *fdp;
491 struct vmspace *vm;
492
493 if (p == NULL)
494 return;
495 if ((fdp = p->p_fd) != NULL) {
496 kern_closefrom(0);
497 if (fdp->fd_cdir) {
28623bf9 498 cache_drop(&fdp->fd_ncdir);
f6a22062
MD
499 vrele(fdp->fd_cdir);
500 fdp->fd_cdir = NULL;
f6a22062
MD
501 }
502 if (fdp->fd_rdir) {
28623bf9 503 cache_drop(&fdp->fd_nrdir);
f6a22062
MD
504 vrele(fdp->fd_rdir);
505 fdp->fd_rdir = NULL;
f6a22062
MD
506 }
507 if (fdp->fd_jdir) {
28623bf9 508 cache_drop(&fdp->fd_njdir);
f6a22062
MD
509 vrele(fdp->fd_jdir);
510 fdp->fd_jdir = NULL;
f6a22062
MD
511 }
512 }
4a22e893
MD
513 if (p->p_vkernel)
514 vkernel_exit(p);
f6a22062
MD
515 if (p->p_textvp) {
516 vrele(p->p_textvp);
517 p->p_textvp = NULL;
518 }
519 vm = p->p_vmspace;
520 if (vm != NULL) {
521 pmap_remove_pages(vmspace_pmap(vm),
88181b08
MD
522 VM_MIN_USER_ADDRESS,
523 VM_MAX_USER_ADDRESS);
f6a22062 524 vm_map_remove(&vm->vm_map,
88181b08
MD
525 VM_MIN_USER_ADDRESS,
526 VM_MAX_USER_ADDRESS);
f6a22062
MD
527 }
528}
529
984263bc
MD
530/*
531 * Magic number for savecore
532 *
533 * exported (symorder) and used at least by savecore(8)
534 *
04a77147 535 * Mark it as used so that gcc doesn't optimize it away.
984263bc 536 */
04a77147
SS
537__attribute__((__used__))
538 static u_long const dumpmag = 0x8fca0101UL;
984263bc
MD
539
540static int dumpsize = 0; /* also for savecore */
541
542static int dodump = 1;
543
544SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
545 "Try to perform coredump on kernel panic");
546
547static int
c972a82f 548setdumpdev(cdev_t dev)
984263bc
MD
549{
550 int psize;
551 long newdumplo;
552
028066b1 553 if (dev == NULL) {
984263bc
MD
554 dumpdev = dev;
555 return (0);
556 }
335dda38 557 psize = dev_dpsize(dev);
984263bc 558 if (psize == -1)
335dda38 559 return (ENXIO);
984263bc
MD
560 /*
561 * XXX should clean up checking in dumpsys() to be more like this.
562 */
563 newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE);
564 if (newdumplo <= LABELSECTOR)
565 return (ENOSPC);
566 dumpdev = dev;
567 dumplo = newdumplo;
568 return (0);
569}
570
571
572/* ARGSUSED */
402ed7e1 573static void dump_conf (void *dummy);
984263bc 574static void
c972a82f 575dump_conf(void *dummy)
984263bc
MD
576{
577 char *path;
b13267a5 578 cdev_t dev;
984263bc 579
efda3bd0 580 path = kmalloc(MNAMELEN, M_TEMP, M_WAITOK);
984263bc 581 if (TUNABLE_STR_FETCH("dumpdev", path, MNAMELEN) != 0) {
bc01a404 582 dev = kgetdiskbyname(path);
028066b1 583 if (dev != NULL)
984263bc
MD
584 dumpdev = dev;
585 }
efda3bd0 586 kfree(path, M_TEMP);
984263bc 587 if (setdumpdev(dumpdev) != 0)
028066b1 588 dumpdev = NULL;
984263bc
MD
589}
590
591SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
592
593static int
594sysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS)
595{
596 int error;
597 udev_t ndumpdev;
598
599 ndumpdev = dev2udev(dumpdev);
600 error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
601 if (error == 0 && req->newptr != NULL)
602 error = setdumpdev(udev2dev(ndumpdev, 0));
603 return (error);
604}
605
606SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
936c90c4 607 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,udev_t", "");
984263bc
MD
608
609/*
610 * Doadump comes here after turning off memory management and
611 * getting on the dump stack, either when called above, or by
612 * the auto-restart code.
613 */
614static void
615dumpsys(void)
616{
617 int error;
618
619 savectx(&dumppcb);
8b6a428f 620 dumpthread = curthread;
984263bc 621 if (dumping++) {
6ea70f76 622 kprintf("Dump already in progress, bailing...\n");
984263bc
MD
623 return;
624 }
625 if (!dodump)
626 return;
028066b1 627 if (dumpdev == NULL)
984263bc 628 return;
984263bc 629 dumpsize = Maxmem;
6ea70f76
SW
630 kprintf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo);
631 kprintf("dump ");
335dda38 632 error = dev_ddump(dumpdev);
984263bc 633 if (error == 0) {
6ea70f76 634 kprintf("succeeded\n");
984263bc
MD
635 return;
636 }
6ea70f76 637 kprintf("failed, reason: ");
984263bc 638 switch (error) {
335dda38 639 case ENOSYS:
984263bc 640 case ENODEV:
6ea70f76 641 kprintf("device doesn't support a dump routine\n");
984263bc
MD
642 break;
643
644 case ENXIO:
6ea70f76 645 kprintf("device bad\n");
984263bc
MD
646 break;
647
648 case EFAULT:
6ea70f76 649 kprintf("device not ready\n");
984263bc
MD
650 break;
651
652 case EINVAL:
6ea70f76 653 kprintf("area improper\n");
984263bc
MD
654 break;
655
656 case EIO:
6ea70f76 657 kprintf("i/o error\n");
984263bc
MD
658 break;
659
660 case EINTR:
6ea70f76 661 kprintf("aborted from console\n");
984263bc
MD
662 break;
663
664 default:
6ea70f76 665 kprintf("unknown, error = %d\n", error);
984263bc
MD
666 break;
667 }
668}
669
670int
671dumpstatus(vm_offset_t addr, off_t count)
672{
673 int c;
674
675 if (addr % (1024 * 1024) == 0) {
676#ifdef HW_WDOG
677 if (wdog_tickler)
678 (*wdog_tickler)();
679#endif
6ea70f76 680 kprintf("%ld ", (long)(count / (1024 * 1024)));
984263bc
MD
681 }
682
683 if ((c = cncheckc()) == 0x03)
684 return -1;
685 else if (c != -1)
6ea70f76 686 kprintf("[CTRL-C to abort] ");
984263bc
MD
687
688 return 0;
689}
690
691/*
692 * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
693 * and then reboots. If we are called twice, then we avoid trying to sync
694 * the disks as this often leads to recursive panics.
695 */
696void
697panic(const char *fmt, ...)
698{
b1e04573 699 int bootopt, newpanic;
e2565a42 700 __va_list ap;
984263bc
MD
701 static char buf[256];
702
ddcc5e10
MD
703#ifdef SMP
704 /*
705 * If a panic occurs on multiple cpus before the first is able to
706 * halt the other cpus, only one cpu is allowed to take the panic.
6ea70f76 707 * Attempt to be verbose about this situation but if the kprintf()
ddcc5e10
MD
708 * itself panics don't let us overrun the kernel stack.
709 *
710 * Be very nasty about descheduling our thread at the lowest
711 * level possible in an attempt to freeze the thread without
712 * inducing further panics.
713 *
714 * Bumping gd_trap_nesting_level will also bypass assertions in
715 * lwkt_switch() and allow us to switch away even if we are a
716 * FAST interrupt or IPI.
717 */
718 if (atomic_poll_acquire_int(&panic_cpu_interlock)) {
719 panic_cpu_gd = mycpu;
720 } else if (panic_cpu_gd != mycpu) {
721 crit_enter();
722 ++mycpu->gd_trap_nesting_level;
723 if (mycpu->gd_trap_nesting_level < 25) {
6ea70f76 724 kprintf("SECONDARY PANIC ON CPU %d THREAD %p\n",
ddcc5e10
MD
725 mycpu->gd_cpuid, curthread);
726 }
727 curthread->td_release = NULL; /* be a grinch */
728 for (;;) {
729 lwkt_deschedule_self(curthread);
730 lwkt_switch();
731 }
732 /* NOT REACHED */
733 /* --mycpu->gd_trap_nesting_level */
734 /* crit_exit() */
735 }
736#endif
984263bc 737 bootopt = RB_AUTOBOOT | RB_DUMP;
baf88b6b
MD
738 if (sync_on_panic == 0)
739 bootopt |= RB_NOSYNC;
b1e04573 740 newpanic = 0;
984263bc
MD
741 if (panicstr)
742 bootopt |= RB_NOSYNC;
b1e04573 743 else {
984263bc 744 panicstr = fmt;
b1e04573
JH
745 newpanic = 1;
746 }
984263bc 747
e2565a42 748 __va_start(ap, fmt);
379210cb 749 kvsnprintf(buf, sizeof(buf), fmt, ap);
984263bc
MD
750 if (panicstr == fmt)
751 panicstr = buf;
e2565a42 752 __va_end(ap);
6ea70f76 753 kprintf("panic: %s\n", buf);
984263bc 754#ifdef SMP
7a44d1cb 755 /* three separate prints in case of an unmapped page and trap */
6ea70f76
SW
756 kprintf("mp_lock = %08x; ", mp_lock);
757 kprintf("cpuid = %d; ", mycpu->gd_cpuid);
758 kprintf("lapic.id = %08x\n", lapic.id);
984263bc
MD
759#endif
760
761#if defined(DDB)
b1e04573
JH
762 if (newpanic && trace_on_panic)
763 db_print_backtrace();
984263bc
MD
764 if (debugger_on_panic)
765 Debugger ("panic");
766#endif
767 boot(bootopt);
768}
769
770/*
771 * Support for poweroff delay.
772 */
773#ifndef POWEROFF_DELAY
774# define POWEROFF_DELAY 5000
775#endif
776static int poweroff_delay = POWEROFF_DELAY;
777
778SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
779 &poweroff_delay, 0, "");
780
781static void
782poweroff_wait(void *junk, int howto)
783{
784 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
785 return;
786 DELAY(poweroff_delay * 1000);
787}
788
789/*
790 * Some system processes (e.g. syncer) need to be stopped at appropriate
791 * points in their main loops prior to a system shutdown, so that they
792 * won't interfere with the shutdown process (e.g. by holding a disk buf
793 * to cause sync to fail). For each of these system processes, register
794 * shutdown_kproc() as a handler for one of shutdown events.
795 */
796static int kproc_shutdown_wait = 60;
797SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
798 &kproc_shutdown_wait, 0, "");
799
800void
801shutdown_kproc(void *arg, int howto)
802{
bc6dffab 803 struct thread *td;
984263bc
MD
804 struct proc *p;
805 int error;
806
807 if (panicstr)
808 return;
809
bc6dffab
MD
810 td = (struct thread *)arg;
811 if ((p = td->td_proc) != NULL) {
6ea70f76 812 kprintf("Waiting (max %d seconds) for system process `%s' to stop...",
bc6dffab
MD
813 kproc_shutdown_wait, p->p_comm);
814 } else {
6ea70f76 815 kprintf("Waiting (max %d seconds) for system thread %s to stop...",
0cfcada1 816 kproc_shutdown_wait, td->td_comm);
bc6dffab
MD
817 }
818 error = suspend_kproc(td, kproc_shutdown_wait * hz);
984263bc
MD
819
820 if (error == EWOULDBLOCK)
6ea70f76 821 kprintf("timed out\n");
984263bc 822 else
6ea70f76 823 kprintf("stopped\n");
984263bc 824}