xref: /dragonfly/sys/kern/kern_shutdown.c (revision adb6cc9d)
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
35  * $FreeBSD: src/sys/kern/kern_shutdown.c,v 1.72.2.12 2002/02/21 19:15:10 dillon Exp $
36  */
37 
38 #include "opt_ddb.h"
39 #include "opt_ddb_trace.h"
40 #include "opt_panic.h"
41 #include "use_gpio.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/eventhandler.h>
46 #include <sys/buf.h>
47 #include <sys/disk.h>
48 #include <sys/diskslice.h>
49 #include <sys/reboot.h>
50 #include <sys/proc.h>
51 #include <sys/priv.h>
52 #include <sys/fcntl.h>		/* FREAD	*/
53 #include <sys/stat.h>		/* S_IFCHR	*/
54 #include <sys/vnode.h>
55 #include <sys/kernel.h>
56 #include <sys/kerneldump.h>
57 #include <sys/kthread.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/queue.h>
61 #include <sys/sysctl.h>
62 #include <sys/vkernel.h>
63 #include <sys/conf.h>
64 #include <sys/sysproto.h>
65 #include <sys/device.h>
66 #include <sys/cons.h>
67 #include <sys/kbio.h>
68 #include <sys/shm.h>
69 #include <sys/kern_syscall.h>
70 #include <vm/vm_map.h>
71 #include <vm/pmap.h>
72 
73 #include <sys/thread2.h>
74 #include <sys/buf2.h>
75 #include <sys/mplock2.h>
76 
77 #include <machine/cpu.h>
78 #include <machine/clock.h>
79 #include <machine/md_var.h>
80 #include <machine/smp.h>		/* smp_active_mask, cpuid */
81 #include <machine/vmparam.h>
82 #include <machine/thread.h>
83 
84 #include <sys/signalvar.h>
85 
86 #if defined(WDOG_DISABLE_ON_PANIC)
87 #include <sys/wdog.h>
88 #endif
89 #include <dev/acpica/acpi_pvpanic/panic_notifier.h>
90 #if (NGPIO > 0) && defined(ERROR_LED_ON_PANIC)
91 #include <dev/misc/gpio/gpio.h>
92 #endif
93 
94 #ifndef PANIC_REBOOT_WAIT_TIME
95 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
96 #endif
97 
98 /*
99  * Note that stdarg.h and the ANSI style va_start macro is used for both
100  * ANSI and traditional C compilers.  We use the machine version to stay
101  * within the confines of the kernel header files.
102  */
103 #include <machine/stdarg.h>
104 
105 #ifdef DDB
106 #include <ddb/ddb.h>
107 #ifdef DDB_UNATTENDED
108 int debugger_on_panic = 0;
109 #else
110 int debugger_on_panic = 1;
111 #endif
112 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
113 	&debugger_on_panic, 0, "Run debugger on kernel panic");
114 
115 #ifdef DDB_TRACE
116 int trace_on_panic = 1;
117 #else
118 int trace_on_panic = 0;
119 #endif
120 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW,
121 	&trace_on_panic, 0, "Print stack trace on kernel panic");
122 #endif
123 
124 static int sync_on_panic = 0;
125 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW,
126 	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
127 
128 SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
129 
130 /*
131  * Variable panicstr contains argument to first call to panic; used as flag
132  * to indicate that the kernel has already called panic.
133  */
134 const char *panicstr;
135 
136 int dumping;				/* system is dumping */
137 static struct dumperinfo dumper;	/* selected dumper */
138 
139 __read_frequently globaldata_t panic_cpu_gd;	/* used in lock assertion */
140 struct lwkt_tokref panic_tokens[LWKT_MAXTOKENS];
141 int panic_tokens_count;
142 
143 int bootverbose = 0;			/* note: assignment to force non-bss */
144 SYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW,
145 	   &bootverbose, 0, "Verbose kernel messages");
146 
147 int cold = 1;				/* note: assignment to force non-bss */
148 int dumplo;				/* OBSOLETE - savecore compat */
149 u_int64_t dumplo64;
150 
151 static void boot (int) __dead2;
152 static int setdumpdev (cdev_t dev);
153 static void poweroff_wait (void *, int);
154 static void print_uptime (void);
155 static void shutdown_halt (void *junk, int howto);
156 static void shutdown_panic (void *junk, int howto);
157 static void shutdown_reset (void *junk, int howto);
158 static int shutdown_busycount1(struct buf *bp, void *info);
159 static int shutdown_busycount2(struct buf *bp, void *info);
160 static void shutdown_cleanup_proc(struct proc *p);
161 
162 /* register various local shutdown events */
163 static void
164 shutdown_conf(void *unused)
165 {
166 	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST);
167 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100);
168 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100);
169 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200);
170 }
171 
172 SYSINIT(shutdown_conf, SI_BOOT2_MACHDEP, SI_ORDER_ANY, shutdown_conf, NULL);
173 
174 /* ARGSUSED */
175 
176 /*
177  * The system call that results in a reboot
178  *
179  * MPALMOSTSAFE
180  */
181 int
182 sys_reboot(struct reboot_args *uap)
183 {
184 	struct thread *td = curthread;
185 	int error;
186 
187 	if ((error = priv_check(td, PRIV_REBOOT)))
188 		return (error);
189 
190 	get_mplock();
191 	boot(uap->opt);
192 	rel_mplock();
193 	return (0);
194 }
195 
196 /*
197  * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
198  */
199 static int shutdown_howto = 0;
200 
201 void
202 shutdown_nice(int howto)
203 {
204 	shutdown_howto = howto;
205 
206 	/* Send a signal to init(8) and have it shutdown the world */
207 	if (initproc != NULL) {
208 		ksignal(initproc, SIGINT);
209 	} else {
210 		/* No init(8) running, so simply reboot */
211 		boot(RB_NOSYNC);
212 	}
213 	return;
214 }
215 static int	waittime = -1;
216 struct pcb	dumppcb;
217 struct thread	*dumpthread;
218 
219 static void
220 print_uptime(void)
221 {
222 	int f;
223 	struct timespec ts;
224 
225 	getnanouptime(&ts);
226 	kprintf("Uptime: ");
227 	f = 0;
228 	if (ts.tv_sec >= 86400) {
229 		kprintf("%ldd", ts.tv_sec / 86400);
230 		ts.tv_sec %= 86400;
231 		f = 1;
232 	}
233 	if (f || ts.tv_sec >= 3600) {
234 		kprintf("%ldh", ts.tv_sec / 3600);
235 		ts.tv_sec %= 3600;
236 		f = 1;
237 	}
238 	if (f || ts.tv_sec >= 60) {
239 		kprintf("%ldm", ts.tv_sec / 60);
240 		ts.tv_sec %= 60;
241 		f = 1;
242 	}
243 	kprintf("%lds\n", ts.tv_sec);
244 }
245 
246 /*
247  *  Go through the rigmarole of shutting down..
248  * this used to be in machdep.c but I'll be dammned if I could see
249  * anything machine dependant in it.
250  */
251 static void
252 boot(int howto)
253 {
254 	/*
255 	 * Get rid of any user scheduler baggage and then give
256 	 * us a high priority.
257 	 */
258 	if (curthread->td_release)
259 		curthread->td_release(curthread);
260 	lwkt_setpri_self(TDPRI_MAX);
261 
262 	/* collect extra flags that shutdown_nice might have set */
263 	howto |= shutdown_howto;
264 
265 	/*
266 	 * We really want to shutdown on the BSP.  Subsystems such as ACPI
267 	 * can't power-down the box otherwise.
268 	 */
269 	if (!CPUMASK_ISUP(smp_active_mask)) {
270 		kprintf("boot() called on cpu#%d\n", mycpu->gd_cpuid);
271 	}
272 	if (panicstr == NULL && mycpu->gd_cpuid != 0) {
273 		kprintf("Switching to cpu #0 for shutdown\n");
274 		lwkt_setcpu_self(globaldata_find(0));
275 	}
276 	/*
277 	 * Do any callouts that should be done BEFORE syncing the filesystems.
278 	 */
279 	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
280 
281 	/*
282 	 * Try to get rid of any remaining FS references.  The calling
283 	 * process, proc0, and init may still hold references.  The
284 	 * VFS cache subsystem may still hold a root reference to root.
285 	 *
286 	 * XXX this needs work.  We really need to SIGSTOP all remaining
287 	 * processes in order to avoid blowups due to proc0's filesystem
288 	 * references going away.  For now just make sure that the init
289 	 * process is stopped.
290 	 */
291 	if (panicstr == NULL) {
292 		shutdown_cleanup_proc(curproc);
293 		shutdown_cleanup_proc(&proc0);
294 		if (initproc) {
295 			if (initproc != curproc) {
296 				ksignal(initproc, SIGSTOP);
297 				tsleep(boot, 0, "shutdn", hz / 20);
298 			}
299 			shutdown_cleanup_proc(initproc);
300 		}
301 		vfs_cache_setroot(NULL, NULL);
302 	}
303 
304 	/*
305 	 * Now sync filesystems
306 	 */
307 	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
308 		int iter, nbusy, pbusy;
309 		int zcount;
310 
311 		waittime = 0;
312 		zcount = 0;
313 		kprintf("\nsyncing disks... ");
314 
315 		sys_sync(NULL);
316 
317 		/*
318 		 * With soft updates, some buffers that are written will be
319 		 * remarked as dirty until other buffers are written.
320 		 *
321 		 * sys_sync() usually runs asynchronously, to give us a
322 		 * better chance of syncing the rest of the filesystems when
323 		 * one or more of them are stuck.
324 		 */
325 		for (iter = pbusy = 0; iter < 20 + zcount; iter++) {
326 			if (iter <= 10)
327 				nbusy = scan_all_buffers(shutdown_busycount1,
328 							 &iter);
329 			else
330 				nbusy = scan_all_buffers(shutdown_busycount2,
331 							 &iter);
332 			kprintf("%d ", nbusy);
333 			if (nbusy == 0) {
334 				if (++zcount == 3)
335 					break;
336 			} else {
337 				zcount = 0;
338 			}
339 
340 			/*
341 			 * There could be a lot to sync, only allow iter to
342 			 * proceed while there is progress.
343 			 */
344 			if (nbusy < pbusy) {
345 				if (iter > 10)
346 					iter = 10;
347 				else
348 					iter = 0;
349 			}
350 			pbusy = nbusy;
351 
352 			/*
353 			 * XXX:
354 			 * Process soft update work queue if buffers don't sync
355 			 * after 6 iterations by permitting the syncer to run.
356 			 */
357 			if (iter > 5)
358 				bio_ops_sync(NULL);
359 
360 			sys_sync(NULL);
361 			tsleep(boot, 0, "shutdn", hz * iter / 20 + 1);
362 		}
363 		kprintf("\n");
364 
365 		if (zcount < 3) {
366 			/*
367 			 * Failed to sync all blocks. Indicate this and don't
368 			 * unmount filesystems (thus forcing an fsck on reboot).
369 			 */
370 			kprintf("giving up on %d buffers\n", nbusy);
371 #ifdef DDB
372 			if (debugger_on_panic)
373 				Debugger("busy buffer problem");
374 #endif /* DDB */
375 			tsleep(boot, 0, "shutdn", hz * 5 + 1);
376 		} else {
377 			kprintf("done\n");
378 
379 			/*
380 			 * Unmount filesystems
381 			 */
382 			if (panicstr == NULL)
383 				vfs_unmountall(1);
384 		}
385 		tsleep(boot, 0, "shutdn", hz / 10 + 1);
386 	}
387 
388 	print_uptime();
389 
390 	/*
391 	 * Dump before doing post_sync shutdown ops
392 	 */
393 	crit_enter();
394 	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) {
395 		dumpsys();
396 	}
397 
398 	/*
399 	 * Ok, now do things that assume all filesystem activity has
400 	 * been completed.  This will also call the device shutdown
401 	 * methods.
402 	 */
403 	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
404 
405 	/* Now that we're going to really halt the system... */
406 	EVENTHANDLER_INVOKE(shutdown_final, howto);
407 
408 	for(;;) ;	/* safety against shutdown_reset not working */
409 	/* NOTREACHED */
410 }
411 
412 /*
413  * Pass 1 - Figure out if there are any busy or dirty buffers still present.
414  *
415  *	We ignore TMPFS mounts in this pass.
416  */
417 static int
418 shutdown_busycount1(struct buf *bp, void *info __unused)
419 {
420 	struct vnode *vp;
421 
422 	if ((vp = bp->b_vp) != NULL && vp->v_tag == VT_TMPFS)
423 		return (0);
424 	if ((bp->b_flags & B_INVAL) == 0 && BUF_LOCKINUSE(bp))
425 		return(1);
426 	if ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI)
427 		return (1);
428 	return (0);
429 }
430 
431 /*
432  * Pass 2 - only run after pass 1 has completed or has given up
433  *
434  *	We ignore TMPFS, NFS, MFS, and SMBFS mounts in this pass.
435  */
436 static int
437 shutdown_busycount2(struct buf *bp, void *info)
438 {
439 	struct vnode *vp;
440 	int *iterp = info;
441 	const char *mpath;
442 
443 	/*
444 	 * Ignore tmpfs and nfs mounts
445 	 */
446 	if ((vp = bp->b_vp) != NULL) {
447 		if (vp->v_tag == VT_TMPFS)
448 			return (0);
449 		if (vp->v_tag == VT_NFS)
450 			return (0);
451 		if (vp->v_tag == VT_MFS)
452 			return (0);
453 		if (vp->v_tag == VT_SMBFS)
454 			return (0);
455 	}
456 
457 	/*
458 	 * Only count buffers stuck on I/O, ignore everything else
459 	 */
460 	if (((bp->b_flags & B_INVAL) == 0 && BUF_LOCKINUSE(bp)) ||
461 	    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
462 		/*
463 		 * Only count buffers undergoing write I/O
464 		 * on the related vnode.
465 		 */
466 		if (bp->b_vp == NULL ||
467 		    bio_track_active(&bp->b_vp->v_track_write) == 0) {
468 			return (0);
469 		}
470 		if (*iterp > 15) {
471 			mpath = "?";
472 			if (bp->b_vp->v_mount)
473 				mpath = bp->b_vp->v_mount->mnt_stat.f_mntonname;
474 
475 			kprintf("%p on %s, flags:%08x, loffset:%jd, "
476 				"doffset:%jd\n",
477 				bp,
478 				mpath,
479 				bp->b_flags,
480 				(intmax_t)bp->b_loffset,
481 				(intmax_t)bp->b_bio2.bio_offset);
482 		}
483 		return(1);
484 	}
485 	return(0);
486 }
487 
488 /*
489  * If the shutdown was a clean halt, behave accordingly.
490  */
491 static void
492 shutdown_halt(void *junk, int howto)
493 {
494 	if (howto & RB_HALT) {
495 		kprintf("\n");
496 		kprintf("The operating system has halted.\n");
497 #ifdef _KERNEL_VIRTUAL
498 		cpu_halt();
499 #else
500 		kprintf("Please press any key to reboot.\n\n");
501 		cnpoll(TRUE);
502 		switch (cngetc()) {
503 		case -1:		/* No console, just die */
504 			cpu_halt();
505 			/* NOTREACHED */
506 		default:
507 			howto &= ~RB_HALT;
508 			break;
509 		}
510 #endif
511 	}
512 }
513 
514 /*
515  * Check to see if the system paniced, pause and then reboot
516  * according to the specified delay.
517  */
518 static void
519 shutdown_panic(void *junk, int howto)
520 {
521 	int loop;
522 	int c;
523 
524 	if (howto & RB_DUMP) {
525 		if (PANIC_REBOOT_WAIT_TIME != 0) {
526 			if (PANIC_REBOOT_WAIT_TIME != -1) {
527 				kprintf("Automatic reboot in %d seconds - "
528 				       "press a key on the console to abort\n",
529 					PANIC_REBOOT_WAIT_TIME);
530 				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
531 				     loop > 0; --loop) {
532 					DELAY(1000 * 100); /* 1/10th second */
533 					/* Did user type a key? */
534 					c = cncheckc();
535 					if (c != -1 && c != NOKEY)
536 						break;
537 				}
538 				if (!loop)
539 					return;
540 			}
541 		} else { /* zero time specified - reboot NOW */
542 			return;
543 		}
544 		kprintf("--> Press a key on the console to reboot,\n");
545 		kprintf("--> or switch off the system now.\n");
546 		cngetc();
547 	}
548 }
549 
550 /*
551  * Everything done, now reset
552  */
553 static void
554 shutdown_reset(void *junk, int howto)
555 {
556 	kprintf("Rebooting...\n");
557 	DELAY(1000000);	/* wait 1 sec for kprintf's to complete and be read */
558 	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
559 	cpu_reset();
560 	/* NOTREACHED */ /* assuming reset worked */
561 }
562 
563 /*
564  * Try to remove FS references in the specified process.  This function
565  * is used during shutdown
566  */
567 static
568 void
569 shutdown_cleanup_proc(struct proc *p)
570 {
571 	struct filedesc *fdp;
572 	struct vmspace *vm;
573 
574 	if (p == NULL)
575 		return;
576 	if ((fdp = p->p_fd) != NULL) {
577 		kern_closefrom(0);
578 		if (fdp->fd_cdir) {
579 			cache_drop(&fdp->fd_ncdir);
580 			vrele(fdp->fd_cdir);
581 			fdp->fd_cdir = NULL;
582 		}
583 		if (fdp->fd_rdir) {
584 			cache_drop(&fdp->fd_nrdir);
585 			vrele(fdp->fd_rdir);
586 			fdp->fd_rdir = NULL;
587 		}
588 		if (fdp->fd_jdir) {
589 			cache_drop(&fdp->fd_njdir);
590 			vrele(fdp->fd_jdir);
591 			fdp->fd_jdir = NULL;
592 		}
593 	}
594 	if (p->p_vkernel)
595 		vkernel_exit(p);
596 	if (p->p_textvp) {
597 		vrele(p->p_textvp);
598 		p->p_textvp = NULL;
599 	}
600 	if (p->p_textnch.ncp)
601 		cache_drop(&p->p_textnch);
602 	vm = p->p_vmspace;
603 	if (vm != NULL) {
604 		pmap_remove_pages(vmspace_pmap(vm),
605 				  VM_MIN_USER_ADDRESS,
606 				  VM_MAX_USER_ADDRESS);
607 		vm_map_remove(&vm->vm_map,
608 			      VM_MIN_USER_ADDRESS,
609 			      VM_MAX_USER_ADDRESS);
610 	}
611 }
612 
613 /*
614  * Magic number for savecore
615  *
616  * exported (symorder) and used at least by savecore(8)
617  *
618  * Mark it as used so that gcc doesn't optimize it away.
619  */
620 __attribute__((__used__))
621 	static u_long const dumpmag = 0x8fca0101UL;
622 
623 __attribute__((__used__))
624 	static int	dumpsize = 0;		/* also for savecore */
625 
626 static int	dodump = 1;
627 
628 SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
629     "Try to perform coredump on kernel panic");
630 
631 void
632 mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
633     uint64_t dumplen, uint32_t blksz)
634 {
635 	bzero(kdh, sizeof(*kdh));
636 	strncpy(kdh->magic, magic, sizeof(kdh->magic));
637 	strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
638 	kdh->version = htod32(KERNELDUMPVERSION);
639 	kdh->architectureversion = htod32(archver);
640 	kdh->dumplength = htod64(dumplen);
641 	kdh->dumptime = htod64(time_second);
642 	kdh->blocksize = htod32(blksz);
643 	strncpy(kdh->hostname, hostname, sizeof(kdh->hostname));
644 	strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
645 	if (panicstr != NULL)
646 		strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
647 	kdh->parity = kerneldump_parity(kdh);
648 }
649 
650 static int
651 setdumpdev(cdev_t dev)
652 {
653 	int error;
654 	int doopen;
655 
656 	if (dev == NULL) {
657 		disk_dumpconf(NULL, 0/*off*/);
658 		dumpdev = NULL;
659 		return (0);
660 	}
661 
662 	/*
663 	 * We have to open the device before we can perform ioctls on it,
664 	 * or the slice/label data may not be present.  Device opens are
665 	 * usually tracked by specfs, but the dump device can be set in
666 	 * early boot and may not be open so this is somewhat of a hack.
667 	 */
668 	doopen = (dev->si_sysref.refcnt == 1);
669 	if (doopen) {
670 		error = dev_dopen(dev, FREAD, S_IFCHR, proc0.p_ucred, NULL, NULL);
671 		if (error)
672 			return (error);
673 	}
674 	error = disk_dumpconf(dev, 1/*on*/);
675 	if (error == 0)
676 		dumpdev = dev;
677 
678 	return error;
679 }
680 
681 /* ARGSUSED */
682 static void dump_conf (void *dummy);
683 static void
684 dump_conf(void *dummy)
685 {
686 	char *path;
687 	cdev_t dev;
688 	int _dummy;
689 
690 	path = kmalloc(MNAMELEN, M_TEMP, M_WAITOK);
691 	if (TUNABLE_STR_FETCH("dumpdev", path, MNAMELEN) != 0) {
692 		/*
693 		 * Make sure all disk devices created so far have also been
694 		 * probed, and also make sure that the newly created device
695 		 * nodes for probed disks are ready, too.
696 		 *
697 		 * XXX - Delay an additional 2 seconds to help drivers which
698 		 *	 pickup devices asynchronously and are not caught by
699 		 *	 CAM's initial probe.
700 		 */
701 		sync_devs();
702 		tsleep(&_dummy, 0, "syncer", hz*2);
703 
704 		dev = kgetdiskbyname(path);
705 		if (dev != NULL)
706 			dumpdev = dev;
707 	}
708 	kfree(path, M_TEMP);
709 	if (setdumpdev(dumpdev) != 0)
710 		dumpdev = NULL;
711 }
712 
713 SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL);
714 
715 static int
716 sysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS)
717 {
718 	int error;
719 	dev_t ndumpdev;
720 
721 	ndumpdev = devid_from_dev(dumpdev);
722 	error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
723 	if (error == 0 && req->newptr != NULL)
724 		error = setdumpdev(dev_from_devid(ndumpdev, 0));
725 	return (error);
726 }
727 
728 SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
729 	0, sizeof dumpdev, sysctl_kern_dumpdev, "T,udev_t", "");
730 
731 static struct panicerinfo *panic_notifier;
732 
733 int
734 set_panic_notifier(struct panicerinfo *info)
735 {
736 	if (info == NULL)
737 		panic_notifier = NULL;
738 	else if (panic_notifier != NULL)
739 		return 1;
740 	else
741 		panic_notifier = info;
742 
743 	return 0;
744 }
745 
746 /*
747  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
748  * and then reboots.  If we are called twice, then we avoid trying to sync
749  * the disks as this often leads to recursive panics.
750  */
751 void
752 panic(const char *fmt, ...)
753 {
754 	int bootopt, newpanic;
755 	globaldata_t gd = mycpu;
756 	thread_t td = gd->gd_curthread;
757 	__va_list ap;
758 	static char buf[256];
759 
760 	/*
761 	 * If a panic occurs on multiple cpus before the first is able to
762 	 * halt the other cpus, only one cpu is allowed to take the panic.
763 	 * Attempt to be verbose about this situation but if the kprintf()
764 	 * itself panics don't let us overrun the kernel stack.
765 	 *
766 	 * Be very nasty about descheduling our thread at the lowest
767 	 * level possible in an attempt to freeze the thread without
768 	 * inducing further panics.
769 	 *
770 	 * Bumping gd_trap_nesting_level will also bypass assertions in
771 	 * lwkt_switch() and allow us to switch away even if we are a
772 	 * FAST interrupt or IPI.
773 	 *
774 	 * The setting of panic_cpu_gd also determines how kprintf()
775 	 * spin-locks itself.  DDB can set panic_cpu_gd as well.
776 	 */
777 	for (;;) {
778 		globaldata_t xgd = panic_cpu_gd;
779 
780 		/*
781 		 * Someone else got the panic cpu
782 		 */
783 		if (xgd && xgd != gd) {
784 			crit_enter();
785 			++mycpu->gd_trap_nesting_level;
786 			if (mycpu->gd_trap_nesting_level < 25) {
787 				kprintf("SECONDARY PANIC ON CPU %d THREAD %p\n",
788 					mycpu->gd_cpuid, td);
789 			}
790 			td->td_release = NULL;	/* be a grinch */
791 			for (;;) {
792 				lwkt_deschedule_self(td);
793 				lwkt_switch();
794 			}
795 			/* NOT REACHED */
796 			/* --mycpu->gd_trap_nesting_level */
797 			/* crit_exit() */
798 		}
799 
800 		/*
801 		 * Reentrant panic
802 		 */
803 		if (xgd && xgd == gd)
804 			break;
805 
806 		/*
807 		 * We got it
808 		 */
809 		if (atomic_cmpset_ptr(&panic_cpu_gd, NULL, gd))
810 			break;
811 	}
812 	/*
813 	 * Try to get the system into a working state.  Save information
814 	 * we are about to destroy.
815 	 */
816 	kvcreinitspin();
817 	if (panicstr == NULL) {
818 		bcopy(td->td_toks_array, panic_tokens, sizeof(panic_tokens));
819 		panic_tokens_count = td->td_toks_stop - &td->td_toks_base;
820 	}
821 	lwkt_relalltokens(td);
822 	td->td_toks_stop = &td->td_toks_base;
823 	if (gd->gd_spinlocks)
824 		kprintf("panic with %d spinlocks held\n", gd->gd_spinlocks);
825 	gd->gd_spinlocks = 0;
826 
827 	/*
828 	 * Setup
829 	 */
830 	bootopt = RB_AUTOBOOT | RB_DUMP;
831 	if (sync_on_panic == 0)
832 		bootopt |= RB_NOSYNC;
833 	newpanic = 0;
834 	if (panicstr) {
835 		bootopt |= RB_NOSYNC;
836 	} else {
837 		panicstr = fmt;
838 		newpanic = 1;
839 	}
840 
841 	/*
842 	 * Format the panic string.
843 	 */
844 	__va_start(ap, fmt);
845 	kvsnprintf(buf, sizeof(buf), fmt, ap);
846 	if (panicstr == fmt)
847 		panicstr = buf;
848 	__va_end(ap);
849 	if (panic_notifier != NULL)
850 		panic_notifier->notifier(panic_notifier->arg);
851 	kprintf("panic: %s\n", buf);
852 	/* two separate prints in case of an unmapped page and trap */
853 	kprintf("cpuid = %d\n", mycpu->gd_cpuid);
854 
855 #if (NGPIO > 0) && defined(ERROR_LED_ON_PANIC)
856 	led_switch("error", 1);
857 #endif
858 
859 #if defined(WDOG_DISABLE_ON_PANIC)
860 	wdog_disable();
861 #endif
862 
863 	/*
864 	 * Make sure kgdb knows who we are, there won't be a stoppcbs[]
865 	 * entry since our cpu wasn't stopped.
866 	 */
867 	savectx(&dumppcb);
868 	dumpthread = curthread;
869 
870 	/*
871 	 * Enter the debugger or fall through & dump.  Entering the
872 	 * debugger will stop cpus.  If not entering the debugger stop
873 	 * cpus here.
874 	 *
875 	 * Limit the trace history to leave more panic data on a
876 	 * potentially row-limited console.
877 	 */
878 
879 #if defined(DDB)
880 	if (newpanic && trace_on_panic)
881 		print_backtrace(6);
882 	if (debugger_on_panic)
883 		Debugger("panic");
884 	else
885 #endif
886 	if (newpanic)
887 		stop_cpus(mycpu->gd_other_cpus);
888 	boot(bootopt);
889 }
890 
891 /*
892  * Support for poweroff delay.
893  */
894 #ifndef POWEROFF_DELAY
895 # define POWEROFF_DELAY 5000
896 #endif
897 static int poweroff_delay = POWEROFF_DELAY;
898 
899 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
900 	&poweroff_delay, 0, "");
901 
902 static void
903 poweroff_wait(void *junk, int howto)
904 {
905 	if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
906 		return;
907 	DELAY(poweroff_delay * 1000);
908 }
909 
910 /*
911  * Some system processes (e.g. syncer) need to be stopped at appropriate
912  * points in their main loops prior to a system shutdown, so that they
913  * won't interfere with the shutdown process (e.g. by holding a disk buf
914  * to cause sync to fail).  For each of these system processes, register
915  * shutdown_kproc() as a handler for one of shutdown events.
916  */
917 static int kproc_shutdown_wait = 60;
918 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
919     &kproc_shutdown_wait, 0, "");
920 
921 void
922 shutdown_kproc(void *arg, int howto)
923 {
924 	struct thread *td;
925 	struct proc *p;
926 	int error;
927 
928 	if (panicstr)
929 		return;
930 
931 	td = (struct thread *)arg;
932 	if ((p = td->td_proc) != NULL) {
933 	    kprintf("Waiting (max %d seconds) for system process `%s' to stop...",
934 		kproc_shutdown_wait, p->p_comm);
935 	} else {
936 	    kprintf("Waiting (max %d seconds) for system thread %s to stop...",
937 		kproc_shutdown_wait, td->td_comm);
938 	}
939 	error = suspend_kproc(td, kproc_shutdown_wait * hz);
940 
941 	if (error == EWOULDBLOCK)
942 		kprintf("timed out\n");
943 	else
944 		kprintf("stopped\n");
945 }
946 
947 /* Registration of dumpers */
948 int
949 set_dumper(struct dumperinfo *di)
950 {
951 	if (di == NULL) {
952 		bzero(&dumper, sizeof(dumper));
953 		return 0;
954 	}
955 
956 	if (dumper.dumper != NULL)
957 		return (EBUSY);
958 
959 	dumper = *di;
960 	return 0;
961 }
962 
963 void
964 dumpsys(void)
965 {
966 #if defined (_KERNEL_VIRTUAL)
967 	/* vkernels don't support dumps */
968 	kprintf("vkernels don't support dumps\n");
969 	return;
970 #endif
971 	/*
972 	 * If there is a dumper registered and we aren't dumping already, call
973 	 * the machine dependent dumpsys (md_dumpsys) to do the hard work.
974 	 *
975 	 * XXX: while right now the md_dumpsys() of x86 and x86_64 could be
976 	 *      factored out completely into here, I rather keep them machine
977 	 *      dependent in case we ever add a platform which does not share
978 	 *      the same dumpsys() code, such as arm.
979 	 */
980 	if (dumper.dumper != NULL && !dumping) {
981 		dumping++;
982 		md_dumpsys(&dumper);
983 	}
984 }
985 
986 int dump_stop_usertds = 0;
987 
988 static
989 void
990 need_user_resched_remote(void *dummy)
991 {
992 	need_user_resched();
993 }
994 
995 void
996 dump_reactivate_cpus(void)
997 {
998 	globaldata_t gd;
999 	int cpu, seq;
1000 
1001 	dump_stop_usertds = 1;
1002 
1003 	need_user_resched();
1004 
1005 	for (cpu = 0; cpu < ncpus; cpu++) {
1006 		gd = globaldata_find(cpu);
1007 		seq = lwkt_send_ipiq(gd, need_user_resched_remote, NULL);
1008 		lwkt_wait_ipiq(gd, seq);
1009 	}
1010 
1011 	restart_cpus(stopped_cpus);
1012 }
1013