37 #include <sys/cdefs.h>
42 #include "opt_panic.h"
43 #include "opt_sched.h"
44 #include "opt_watchdog.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
52 #include <sys/eventhandler.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>
62 #include <sys/reboot.h>
63 #include <sys/resourcevar.h>
64 #include <sys/sched.h>
66 #include <sys/sysctl.h>
67 #include <sys/sysproto.h>
68 #include <sys/vnode.h>
69 #include <sys/watchdog.h>
73 #include <machine/cpu.h>
74 #include <machine/pcb.h>
75 #include <machine/smp.h>
77 #include <security/mac/mac_framework.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_pager.h>
83 #include <vm/swap_pager.h>
85 #include <sys/signalvar.h>
87 #ifndef PANIC_REBOOT_WAIT_TIME
88 #define PANIC_REBOOT_WAIT_TIME 15
93 "Seconds to wait before rebooting after a panic");
100 #include <machine/stdarg.h>
103 #ifdef KDB_UNATTENDED
104 int debugger_on_panic = 0;
106 int debugger_on_panic = 1;
108 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic,
109 CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN,
110 &debugger_on_panic, 0,
"Run debugger on kernel panic");
111 TUNABLE_INT(
"debug.debugger_on_panic", &debugger_on_panic);
114 static int trace_on_panic = 1;
116 static int trace_on_panic = 0;
119 CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN,
120 &trace_on_panic, 0,
"Print stack trace on kernel panic");
121 TUNABLE_INT(
"debug.trace_on_panic", &trace_on_panic);
126 &
sync_on_panic, 0,
"Do a sync before rebooting from a panic");
135 "Shutdown environment");
172 SHUTDOWN_PRI_LAST + 100);
174 SHUTDOWN_PRI_LAST + 100);
176 SHUTDOWN_PRI_LAST + 200);
192 error = mac_system_check_reboot(td->td_ucred, uap->opt);
237 if (ts.tv_sec >= 86400) {
238 printf(
"%ldd", (
long)ts.tv_sec / 86400);
242 if (f || ts.tv_sec >= 3600) {
243 printf(
"%ldh", (
long)ts.tv_sec / 3600);
247 if (f || ts.tv_sec >= 60) {
248 printf(
"%ldm", (
long)ts.tv_sec / 60);
252 printf(
"%lds\n", (
long)ts.tv_sec);
262 if (
dumper.dumper == NULL)
271 if (textdump && textdump_pending) {
273 textdump_dumpsys(&
dumper);
286 if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
288 ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
299 static int first_buf_printf = 1;
307 if (!SCHEDULER_STOPPED()) {
308 thread_lock(curthread);
310 thread_unlock(curthread);
311 KASSERT(PCPU_GET(cpuid) == 0, (
"boot: not running on cpu 0"));
326 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
331 if (!cold && (howto & RB_NOSYNC) == 0 &&
waittime < 0) {
332 register struct buf *bp;
333 int iter, nbusy, pbusy;
340 wdog_kern_pat(WD_LASTVAL);
348 for (iter = pbusy = 0; iter < 20; iter++) {
354 if (first_buf_printf)
355 printf(
"All buffers synced.");
358 if (first_buf_printf) {
359 printf(
"Syncing disks, buffers remaining... ");
360 first_buf_printf = 0;
367 wdog_kern_pat(WD_LASTVAL);
384 for (subiter = 0; subiter < 50 * iter; subiter++) {
385 thread_lock(curthread);
387 thread_unlock(curthread);
403 if (bp->b_dev == NULL) {
405 bp->b_vp->v_mount, mnt_list);
412 "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
413 nbusy, bp, bp->b_vp, bp->b_flags,
414 (intmax_t)bp->b_blkno,
415 (intmax_t)bp->b_lblkno);
416 BUF_LOCKPRINTINFO(bp);
428 printf(
"Giving up on %d buffers\n", nbusy);
431 if (!first_buf_printf)
432 printf(
"Final sync complete\n");
451 EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
453 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !
dumping)
457 EVENTHANDLER_INVOKE(shutdown_final, howto);
470 if (howto & RB_HALT) {
472 printf(
"The operating system has halted.\n");
473 printf(
"Please press any key to reboot.\n\n");
494 if (howto & RB_DUMP) {
497 printf(
"Automatic reboot in %d seconds - "
498 "press a key on the console to abort\n",
513 printf(
"--> Press a key on the console to reboot,\n");
514 printf(
"--> or switch off the system now.\n");
541 mtx_lock_spin(&smp_ipi_mtx);
560 static volatile u_int panic_cpu = NOCPU;
563 struct thread *td = curthread;
564 int bootopt, newpanic;
566 static char buf[256];
580 if (panic_cpu != PCPU_GET(cpuid))
581 while (atomic_cmpset_int(&panic_cpu, NOCPU,
582 PCPU_GET(cpuid)) == 0)
583 while (panic_cpu != NOCPU)
589 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
590 stop_cpus_hard(other_cpus);
599 td->td_stopsched = 1;
603 bootopt = RB_AUTOBOOT;
606 bootopt |= RB_NOSYNC;
615 (void)
vsnprintf(buf,
sizeof(buf), fmt, ap);
618 printf(
"panic: %s\n", buf);
626 printf(
"cpuid = %d\n", PCPU_GET(cpuid));
630 if (newpanic && trace_on_panic)
632 if (debugger_on_panic)
636 td->td_flags |= TDF_INPANIC;
639 bootopt |= RB_NOSYNC;
652 #ifndef POWEROFF_DELAY
653 # define POWEROFF_DELAY 5000
689 p = (
struct proc *)arg;
690 printf(
"Waiting (max %d seconds) for system process `%s' to stop...",
694 if (error == EWOULDBLOCK)
709 td = (
struct thread *)arg;
710 printf(
"Waiting (max %d seconds) for system thread `%s' to stop...",
714 if (error == EWOULDBLOCK)
729 if (
dumper.dumper != NULL)
737 dump_write(
struct dumperinfo *di,
void *
virtual, vm_offset_t physical,
738 off_t offset,
size_t length)
741 if (length != 0 && (offset < di->mediaoffset ||
742 offset - di->mediaoffset + length > di->mediasize)) {
743 printf(
"Attempt to write outside dump device boundaries.\n"
744 "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
745 (intmax_t)offset, (intmax_t)di->mediaoffset,
746 (uintmax_t)length, (intmax_t)di->mediasize);
749 return (di->dumper(di->priv,
virtual, physical, offset, length));
753 mkdumpheader(
struct kerneldumpheader *kdh,
char *magic, uint32_t archver,
754 uint64_t dumplen, uint32_t blksz)
757 bzero(kdh,
sizeof(*kdh));
758 strncpy(kdh->magic, magic,
sizeof(kdh->magic));
759 strncpy(kdh->architecture, MACHINE_ARCH,
sizeof(kdh->architecture));
760 kdh->version = htod32(KERNELDUMPVERSION);
761 kdh->architectureversion = htod32(archver);
762 kdh->dumplength = htod64(dumplen);
764 kdh->blocksize = htod32(blksz);
765 strncpy(kdh->hostname,
prison0.pr_hostname,
sizeof(kdh->hostname));
766 strncpy(kdh->versionstring, version,
sizeof(kdh->versionstring));
768 strncpy(kdh->panicstring,
panicstr,
sizeof(kdh->panicstring));
769 kdh->parity = kerneldump_parity(kdh);
static int isbufbusy(struct buf *bp)
int __elfN() coredump(struct thread *td, struct vnode *vp, off_t limit, int flags)
volatile time_t time_second
void kern_reboot(int howto)
static void shutdown_conf(void *unused)
int set_dumper(struct dumperinfo *di)
SYSCTL_INT(_kern, OID_AUTO, panic_reboot_wait_time, CTLFLAG_RW|CTLFLAG_TUN,&panic_reboot_wait_time, 0,"Seconds to wait before rebooting after a panic")
static int stop_scheduler_on_panic
void panic(const char *fmt,...)
static int poweroff_delay
void mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver, uint64_t dumplen, uint32_t blksz)
void kproc_shutdown(void *arg, int howto)
static void shutdown_reset(void *junk, int howto)
void mi_switch(int flags, struct thread *newtd)
void kthread_shutdown(void *arg, int howto)
void kern_psignal(struct proc *p, int sig)
static void poweroff_wait(void *, int)
SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
int vsnprintf(char *str, size_t size, const char *format, va_list ap)
void getnanouptime(struct timespec *tsp)
int dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical, off_t offset, size_t length)
int kproc_suspend(struct proc *p, int timo)
int priv_check(struct thread *td, int priv)
static int kproc_shutdown_wait
static void shutdown_halt(void *junk, int howto)
static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,"Shutdown environment")
static int panic_reboot_wait_time
void vn_printf(struct vnode *vp, const char *fmt,...)
void shutdown_nice(int howto)
static int shutdown_howto
int sys_reboot(struct thread *td, struct reboot_args *uap)
METHOD int shutdown
Called during system shutdown.
static void shutdown_panic(void *junk, int howto)
void kdb_enter(const char *why, const char *msg)
int printf(const char *fmt,...)
static struct dumperinfo dumper
int sys_sync(struct thread *td, struct sync_args *uap)
void sched_bind(struct thread *td, int cpu)
int kthread_suspend(struct thread *td, int timo)
int vprintf(const char *fmt, va_list ap)
void vfs_unmountall(void)
#define PANIC_REBOOT_WAIT_TIME
int doadump(boolean_t textdump)
static struct pcb dumppcb
TUNABLE_INT("kern.panic_reboot_wait_time",&panic_reboot_wait_time)
void critical_enter(void)
static void print_uptime(void)