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 * 4. 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 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: releng/11.2/sys/kern/kern_shutdown.c 331736 2018-03-29 15:12:03Z kib $");
39
40 #include "opt_ddb.h"
41 #include "opt_kdb.h"
42 #include "opt_panic.h"
43 #include "opt_sched.h"
44 #include "opt_watchdog.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/conf.h>
51 #include <sys/cons.h>
52 #include <sys/eventhandler.h>
53 #include <sys/filedesc.h>
54 #include <sys/jail.h>
55 #include <sys/kdb.h>
56 #include <sys/kernel.h>
57 #include <sys/kerneldump.h>
58 #include <sys/kthread.h>
59 #include <sys/ktr.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/priv.h>
63 #include <sys/proc.h>
64 #include <sys/reboot.h>
65 #include <sys/resourcevar.h>
66 #include <sys/rwlock.h>
67 #include <sys/sched.h>
68 #include <sys/smp.h>
69 #include <sys/sysctl.h>
70 #include <sys/sysproto.h>
71 #include <sys/taskqueue.h>
72 #include <sys/vnode.h>
73 #include <sys/watchdog.h>
74
75 #include <ddb/ddb.h>
76
77 #include <machine/cpu.h>
78 #include <machine/dump.h>
79 #include <machine/pcb.h>
80 #include <machine/smp.h>
81
82 #include <security/mac/mac_framework.h>
83
84 #include <vm/vm.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_pager.h>
88 #include <vm/swap_pager.h>
89
90 #include <sys/signalvar.h>
91
92 static MALLOC_DEFINE(M_DUMPER, "dumper", "dumper block buffer");
93
94 #ifndef PANIC_REBOOT_WAIT_TIME
95 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
96 #endif
97 static int panic_reboot_wait_time = PANIC_REBOOT_WAIT_TIME;
98 SYSCTL_INT(_kern, OID_AUTO, panic_reboot_wait_time, CTLFLAG_RWTUN,
99 &panic_reboot_wait_time, 0,
100 "Seconds to wait before rebooting after a panic");
101
102 /*
103 * Note that stdarg.h and the ANSI style va_start macro is used for both
104 * ANSI and traditional C compilers.
105 */
106 #include <machine/stdarg.h>
107
108 #ifdef KDB
109 #ifdef KDB_UNATTENDED
110 int debugger_on_panic = 0;
111 #else
112 int debugger_on_panic = 1;
113 #endif
114 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic,
115 CTLFLAG_RWTUN | CTLFLAG_SECURE,
116 &debugger_on_panic, 0, "Run debugger on kernel panic");
117
118 #ifdef KDB_TRACE
119 static int trace_on_panic = 1;
120 #else
121 static int trace_on_panic = 0;
122 #endif
123 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic,
124 CTLFLAG_RWTUN | CTLFLAG_SECURE,
125 &trace_on_panic, 0, "Print stack trace on kernel panic");
126 #endif /* KDB */
127
128 static int sync_on_panic = 0;
129 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RWTUN,
130 &sync_on_panic, 0, "Do a sync before rebooting from a panic");
131
132 static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,
133 "Shutdown environment");
134
135 #ifndef DIAGNOSTIC
136 static int show_busybufs;
137 #else
138 static int show_busybufs = 1;
139 #endif
140 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
141 &show_busybufs, 0, "");
142
143 int suspend_blocked = 0;
144 SYSCTL_INT(_kern, OID_AUTO, suspend_blocked, CTLFLAG_RW,
145 &suspend_blocked, 0, "Block suspend due to a pending shutdown");
146
147 /*
148 * Variable panicstr contains argument to first call to panic; used as flag
149 * to indicate that the kernel has already called panic.
150 */
151 const char *panicstr;
152
153 int dumping; /* system is dumping */
154 int rebooting; /* system is rebooting */
155 static struct dumperinfo dumper; /* our selected dumper */
156
157 /* Context information for dump-debuggers. */
158 static struct pcb dumppcb; /* Registers. */
159 lwpid_t dumptid; /* Thread ID. */
160
161 static struct cdevsw reroot_cdevsw = {
162 .d_version = D_VERSION,
163 .d_name = "reroot",
164 };
165
166 static void poweroff_wait(void *, int);
167 static void shutdown_halt(void *junk, int howto);
168 static void shutdown_panic(void *junk, int howto);
169 static void shutdown_reset(void *junk, int howto);
170 static int kern_reroot(void);
171
172 /* register various local shutdown events */
173 static void
174 shutdown_conf(void *unused)
175 {
176
177 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
178 SHUTDOWN_PRI_FIRST);
179 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
180 SHUTDOWN_PRI_LAST + 100);
181 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
182 SHUTDOWN_PRI_LAST + 100);
183 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
184 SHUTDOWN_PRI_LAST + 200);
185 }
186
187 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
188
189 /*
190 * The only reason this exists is to create the /dev/reroot/ directory,
191 * used by reroot code in init(8) as a mountpoint for tmpfs.
192 */
193 static void
194 reroot_conf(void *unused)
195 {
196 int error;
197 struct cdev *cdev;
198
199 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &cdev,
200 &reroot_cdevsw, NULL, UID_ROOT, GID_WHEEL, 0600, "reroot/reroot");
201 if (error != 0) {
202 printf("%s: failed to create device node, error %d",
203 __func__, error);
204 }
205 }
206
207 SYSINIT(reroot_conf, SI_SUB_DEVFS, SI_ORDER_ANY, reroot_conf, NULL);
208
209 /*
210 * The system call that results in a reboot.
211 */
212 /* ARGSUSED */
213 int
214 sys_reboot(struct thread *td, struct reboot_args *uap)
215 {
216 int error;
217
218 error = 0;
219 #ifdef MAC
220 error = mac_system_check_reboot(td->td_ucred, uap->opt);
221 #endif
222 if (error == 0)
223 error = priv_check(td, PRIV_REBOOT);
224 if (error == 0) {
225 if (uap->opt & RB_REROOT) {
226 error = kern_reroot();
227 } else {
228 mtx_lock(&Giant);
229 kern_reboot(uap->opt);
230 mtx_unlock(&Giant);
231 }
232 }
233 return (error);
234 }
235
236 static void
237 shutdown_nice_task_fn(void *arg, int pending __unused)
238 {
239 int howto;
240
241 howto = (uintptr_t)arg;
242 /* Send a signal to init(8) and have it shutdown the world. */
243 PROC_LOCK(initproc);
244 if (howto & RB_POWEROFF)
245 kern_psignal(initproc, SIGUSR2);
246 else if (howto & RB_HALT)
247 kern_psignal(initproc, SIGUSR1);
248 else
249 kern_psignal(initproc, SIGINT);
250 PROC_UNLOCK(initproc);
251 }
252
253 static struct task shutdown_nice_task = TASK_INITIALIZER(0,
254 &shutdown_nice_task_fn, NULL);
255
256 /*
257 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC
258 */
259 void
260 shutdown_nice(int howto)
261 {
262
263 if (initproc != NULL && !SCHEDULER_STOPPED()) {
264 shutdown_nice_task.ta_context = (void *)(uintptr_t)howto;
265 taskqueue_enqueue(taskqueue_fast, &shutdown_nice_task);
266 } else {
267 /*
268 * No init(8) running, or scheduler would not allow it
269 * to run, so simply reboot.
270 */
271 kern_reboot(howto | RB_NOSYNC);
272 }
273 }
274
275 static void
276 print_uptime(void)
277 {
278 int f;
279 struct timespec ts;
280
281 getnanouptime(&ts);
282 printf("Uptime: ");
283 f = 0;
284 if (ts.tv_sec >= 86400) {
285 printf("%ldd", (long)ts.tv_sec / 86400);
286 ts.tv_sec %= 86400;
287 f = 1;
288 }
289 if (f || ts.tv_sec >= 3600) {
290 printf("%ldh", (long)ts.tv_sec / 3600);
291 ts.tv_sec %= 3600;
292 f = 1;
293 }
294 if (f || ts.tv_sec >= 60) {
295 printf("%ldm", (long)ts.tv_sec / 60);
296 ts.tv_sec %= 60;
297 f = 1;
298 }
299 printf("%lds\n", (long)ts.tv_sec);
300 }
301
302 int
303 doadump(boolean_t textdump)
304 {
305 boolean_t coredump;
306 int error;
307
308 error = 0;
309 if (dumping)
310 return (EBUSY);
311 if (dumper.dumper == NULL)
312 return (ENXIO);
313
314 savectx(&dumppcb);
315 dumptid = curthread->td_tid;
316 dumping++;
317
318 coredump = TRUE;
319 #ifdef DDB
320 if (textdump && textdump_pending) {
321 coredump = FALSE;
322 textdump_dumpsys(&dumper);
323 }
324 #endif
325 if (coredump)
326 error = dumpsys(&dumper);
327
328 dumping--;
329 return (error);
330 }
331
332 /*
333 * Shutdown the system cleanly to prepare for reboot, halt, or power off.
334 */
335 void
336 kern_reboot(int howto)
337 {
338 static int once = 0;
339
340 #if defined(SMP)
341 /*
342 * Bind us to CPU 0 so that all shutdown code runs there. Some
343 * systems don't shutdown properly (i.e., ACPI power off) if we
344 * run on another processor.
345 */
346 if (!SCHEDULER_STOPPED()) {
347 thread_lock(curthread);
348 sched_bind(curthread, 0);
349 thread_unlock(curthread);
350 KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
351 }
352 #endif
353 /* We're in the process of rebooting. */
354 rebooting = 1;
355
356 /* We are out of the debugger now. */
357 kdb_active = 0;
358
359 /*
360 * Do any callouts that should be done BEFORE syncing the filesystems.
361 */
362 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
363
364 /*
365 * Now sync filesystems
366 */
367 if (!cold && (howto & RB_NOSYNC) == 0 && once == 0) {
368 once = 1;
369 bufshutdown(show_busybufs);
370 }
371
372 print_uptime();
373
374 cngrab();
375
376 /*
377 * Ok, now do things that assume all filesystem activity has
378 * been completed.
379 */
380 EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
381
382 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
383 doadump(TRUE);
384
385 /* Now that we're going to really halt the system... */
386 EVENTHANDLER_INVOKE(shutdown_final, howto);
387
388 for(;;) ; /* safety against shutdown_reset not working */
389 /* NOTREACHED */
390 }
391
392 /*
393 * The system call that results in changing the rootfs.
394 */
395 static int
396 kern_reroot(void)
397 {
398 struct vnode *oldrootvnode, *vp;
399 struct mount *mp, *devmp;
400 int error;
401
402 if (curproc != initproc)
403 return (EPERM);
404
405 /*
406 * Mark the filesystem containing currently-running executable
407 * (the temporary copy of init(8)) busy.
408 */
409 vp = curproc->p_textvp;
410 error = vn_lock(vp, LK_SHARED);
411 if (error != 0)
412 return (error);
413 mp = vp->v_mount;
414 error = vfs_busy(mp, MBF_NOWAIT);
415 if (error != 0) {
416 vfs_ref(mp);
417 VOP_UNLOCK(vp, 0);
418 error = vfs_busy(mp, 0);
419 vn_lock(vp, LK_SHARED | LK_RETRY);
420 vfs_rel(mp);
421 if (error != 0) {
422 VOP_UNLOCK(vp, 0);
423 return (ENOENT);
424 }
425 if (vp->v_iflag & VI_DOOMED) {
426 VOP_UNLOCK(vp, 0);
427 vfs_unbusy(mp);
428 return (ENOENT);
429 }
430 }
431 VOP_UNLOCK(vp, 0);
432
433 /*
434 * Remove the filesystem containing currently-running executable
435 * from the mount list, to prevent it from being unmounted
436 * by vfs_unmountall(), and to avoid confusing vfs_mountroot().
437 *
438 * Also preserve /dev - forcibly unmounting it could cause driver
439 * reinitialization.
440 */
441
442 vfs_ref(rootdevmp);
443 devmp = rootdevmp;
444 rootdevmp = NULL;
445
446 mtx_lock(&mountlist_mtx);
447 TAILQ_REMOVE(&mountlist, mp, mnt_list);
448 TAILQ_REMOVE(&mountlist, devmp, mnt_list);
449 mtx_unlock(&mountlist_mtx);
450
451 oldrootvnode = rootvnode;
452
453 /*
454 * Unmount everything except for the two filesystems preserved above.
455 */
456 vfs_unmountall();
457
458 /*
459 * Add /dev back; vfs_mountroot() will move it into its new place.
460 */
461 mtx_lock(&mountlist_mtx);
462 TAILQ_INSERT_HEAD(&mountlist, devmp, mnt_list);
463 mtx_unlock(&mountlist_mtx);
464 rootdevmp = devmp;
465 vfs_rel(rootdevmp);
466
467 /*
468 * Mount the new rootfs.
469 */
470 vfs_mountroot();
471
472 /*
473 * Update all references to the old rootvnode.
474 */
475 mountcheckdirs(oldrootvnode, rootvnode);
476
477 /*
478 * Add the temporary filesystem back and unbusy it.
479 */
480 mtx_lock(&mountlist_mtx);
481 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
482 mtx_unlock(&mountlist_mtx);
483 vfs_unbusy(mp);
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
495 if (howto & RB_HALT) {
496 printf("\n");
497 printf("The operating system has halted.\n");
498 printf("Please press any key to reboot.\n\n");
499 switch (cngetc()) {
500 case -1: /* No console, just die */
501 cpu_halt();
502 /* NOTREACHED */
503 default:
504 howto &= ~RB_HALT;
505 break;
506 }
507 }
508 }
509
510 /*
511 * Check to see if the system paniced, pause and then reboot
512 * according to the specified delay.
513 */
514 static void
515 shutdown_panic(void *junk, int howto)
516 {
517 int loop;
518
519 if (howto & RB_DUMP) {
520 if (panic_reboot_wait_time != 0) {
521 if (panic_reboot_wait_time != -1) {
522 printf("Automatic reboot in %d seconds - "
523 "press a key on the console to abort\n",
524 panic_reboot_wait_time);
525 for (loop = panic_reboot_wait_time * 10;
526 loop > 0; --loop) {
527 DELAY(1000 * 100); /* 1/10th second */
528 /* Did user type a key? */
529 if (cncheckc() != -1)
530 break;
531 }
532 if (!loop)
533 return;
534 }
535 } else { /* zero time specified - reboot NOW */
536 return;
537 }
538 printf("--> Press a key on the console to reboot,\n");
539 printf("--> or switch off the system now.\n");
540 cngetc();
541 }
542 }
543
544 /*
545 * Everything done, now reset
546 */
547 static void
548 shutdown_reset(void *junk, int howto)
549 {
550
551 printf("Rebooting...\n");
552 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */
553
554 /*
555 * Acquiring smp_ipi_mtx here has a double effect:
556 * - it disables interrupts avoiding CPU0 preemption
557 * by fast handlers (thus deadlocking against other CPUs)
558 * - it avoids deadlocks against smp_rendezvous() or, more
559 * generally, threads busy-waiting, with this spinlock held,
560 * and waiting for responses by threads on other CPUs
561 * (ie. smp_tlb_shootdown()).
562 *
563 * For the !SMP case it just needs to handle the former problem.
564 */
565 #ifdef SMP
566 mtx_lock_spin(&smp_ipi_mtx);
567 #else
568 spinlock_enter();
569 #endif
570
571 /* cpu_boot(howto); */ /* doesn't do anything at the moment */
572 cpu_reset();
573 /* NOTREACHED */ /* assuming reset worked */
574 }
575
576 #if defined(WITNESS) || defined(INVARIANT_SUPPORT)
577 static int kassert_warn_only = 0;
578 #ifdef KDB
579 static int kassert_do_kdb = 0;
580 #endif
581 #ifdef KTR
582 static int kassert_do_ktr = 0;
583 #endif
584 static int kassert_do_log = 1;
585 static int kassert_log_pps_limit = 4;
586 static int kassert_log_mute_at = 0;
587 static int kassert_log_panic_at = 0;
588 static int kassert_warnings = 0;
589
590 SYSCTL_NODE(_debug, OID_AUTO, kassert, CTLFLAG_RW, NULL, "kassert options");
591
592 SYSCTL_INT(_debug_kassert, OID_AUTO, warn_only, CTLFLAG_RWTUN,
593 &kassert_warn_only, 0,
594 "KASSERT triggers a panic (1) or just a warning (0)");
595
596 #ifdef KDB
597 SYSCTL_INT(_debug_kassert, OID_AUTO, do_kdb, CTLFLAG_RWTUN,
598 &kassert_do_kdb, 0, "KASSERT will enter the debugger");
599 #endif
600
601 #ifdef KTR
602 SYSCTL_UINT(_debug_kassert, OID_AUTO, do_ktr, CTLFLAG_RWTUN,
603 &kassert_do_ktr, 0,
604 "KASSERT does a KTR, set this to the KTRMASK you want");
605 #endif
606
607 SYSCTL_INT(_debug_kassert, OID_AUTO, do_log, CTLFLAG_RWTUN,
608 &kassert_do_log, 0, "KASSERT triggers a panic (1) or just a warning (0)");
609
610 SYSCTL_INT(_debug_kassert, OID_AUTO, warnings, CTLFLAG_RWTUN,
611 &kassert_warnings, 0, "number of KASSERTs that have been triggered");
612
613 SYSCTL_INT(_debug_kassert, OID_AUTO, log_panic_at, CTLFLAG_RWTUN,
614 &kassert_log_panic_at, 0, "max number of KASSERTS before we will panic");
615
616 SYSCTL_INT(_debug_kassert, OID_AUTO, log_pps_limit, CTLFLAG_RWTUN,
617 &kassert_log_pps_limit, 0, "limit number of log messages per second");
618
619 SYSCTL_INT(_debug_kassert, OID_AUTO, log_mute_at, CTLFLAG_RWTUN,
620 &kassert_log_mute_at, 0, "max number of KASSERTS to log");
621
622 static int kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS);
623
624 SYSCTL_PROC(_debug_kassert, OID_AUTO, kassert,
625 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, NULL, 0,
626 kassert_sysctl_kassert, "I", "set to trigger a test kassert");
627
628 static int
629 kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS)
630 {
631 int error, i;
632
633 error = sysctl_wire_old_buffer(req, sizeof(int));
634 if (error == 0) {
635 i = 0;
636 error = sysctl_handle_int(oidp, &i, 0, req);
637 }
638 if (error != 0 || req->newptr == NULL)
639 return (error);
640 KASSERT(0, ("kassert_sysctl_kassert triggered kassert %d", i));
641 return (0);
642 }
643
644 /*
645 * Called by KASSERT, this decides if we will panic
646 * or if we will log via printf and/or ktr.
647 */
648 void
649 kassert_panic(const char *fmt, ...)
650 {
651 static char buf[256];
652 va_list ap;
653
654 va_start(ap, fmt);
655 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
656 va_end(ap);
657
658 /*
659 * panic if we're not just warning, or if we've exceeded
660 * kassert_log_panic_at warnings.
661 */
662 if (!kassert_warn_only ||
663 (kassert_log_panic_at > 0 &&
664 kassert_warnings >= kassert_log_panic_at)) {
665 va_start(ap, fmt);
666 vpanic(fmt, ap);
667 /* NORETURN */
668 }
669 #ifdef KTR
670 if (kassert_do_ktr)
671 CTR0(ktr_mask, buf);
672 #endif /* KTR */
673 /*
674 * log if we've not yet met the mute limit.
675 */
676 if (kassert_do_log &&
677 (kassert_log_mute_at == 0 ||
678 kassert_warnings < kassert_log_mute_at)) {
679 static struct timeval lasterr;
680 static int curerr;
681
682 if (ppsratecheck(&lasterr, &curerr, kassert_log_pps_limit)) {
683 printf("KASSERT failed: %s\n", buf);
684 kdb_backtrace();
685 }
686 }
687 #ifdef KDB
688 if (kassert_do_kdb) {
689 kdb_enter(KDB_WHY_KASSERT, buf);
690 }
691 #endif
692 atomic_add_int(&kassert_warnings, 1);
693 }
694 #endif
695
696 /*
697 * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
698 * and then reboots. If we are called twice, then we avoid trying to sync
699 * the disks as this often leads to recursive panics.
700 */
701 void
702 panic(const char *fmt, ...)
703 {
704 va_list ap;
705
706 va_start(ap, fmt);
707 vpanic(fmt, ap);
708 }
709
710 void
711 vpanic(const char *fmt, va_list ap)
712 {
713 #ifdef SMP
714 cpuset_t other_cpus;
715 #endif
716 struct thread *td = curthread;
717 int bootopt, newpanic;
718 static char buf[256];
719
720 spinlock_enter();
721
722 #ifdef SMP
723 /*
724 * stop_cpus_hard(other_cpus) should prevent multiple CPUs from
725 * concurrently entering panic. Only the winner will proceed
726 * further.
727 */
728 if (panicstr == NULL && !kdb_active) {
729 other_cpus = all_cpus;
730 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
731 stop_cpus_hard(other_cpus);
732 }
733 #endif
734
735 /*
736 * Ensure that the scheduler is stopped while panicking, even if panic
737 * has been entered from kdb.
738 */
739 td->td_stopsched = 1;
740
741 bootopt = RB_AUTOBOOT;
742 newpanic = 0;
743 if (panicstr)
744 bootopt |= RB_NOSYNC;
745 else {
746 bootopt |= RB_DUMP;
747 panicstr = fmt;
748 newpanic = 1;
749 }
750
751 if (newpanic) {
752 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
753 panicstr = buf;
754 cngrab();
755 printf("panic: %s\n", buf);
756 } else {
757 printf("panic: ");
758 vprintf(fmt, ap);
759 printf("\n");
760 }
761 #ifdef SMP
762 printf("cpuid = %d\n", PCPU_GET(cpuid));
763 #endif
764
765 #ifdef KDB
766 if (newpanic && trace_on_panic)
767 kdb_backtrace();
768 if (debugger_on_panic)
769 kdb_enter(KDB_WHY_PANIC, "panic");
770 #endif
771 /*thread_lock(td); */
772 td->td_flags |= TDF_INPANIC;
773 /* thread_unlock(td); */
774 if (!sync_on_panic)
775 bootopt |= RB_NOSYNC;
776 kern_reboot(bootopt);
777 }
778
779 /*
780 * Support for poweroff delay.
781 *
782 * Please note that setting this delay too short might power off your machine
783 * before the write cache on your hard disk has been flushed, leading to
784 * soft-updates inconsistencies.
785 */
786 #ifndef POWEROFF_DELAY
787 # define POWEROFF_DELAY 5000
788 #endif
789 static int poweroff_delay = POWEROFF_DELAY;
790
791 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
792 &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)");
793
794 static void
795 poweroff_wait(void *junk, int howto)
796 {
797
798 if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
799 return;
800 DELAY(poweroff_delay * 1000);
801 }
802
803 /*
804 * Some system processes (e.g. syncer) need to be stopped at appropriate
805 * points in their main loops prior to a system shutdown, so that they
806 * won't interfere with the shutdown process (e.g. by holding a disk buf
807 * to cause sync to fail). For each of these system processes, register
808 * shutdown_kproc() as a handler for one of shutdown events.
809 */
810 static int kproc_shutdown_wait = 60;
811 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
812 &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process");
813
814 void
815 kproc_shutdown(void *arg, int howto)
816 {
817 struct proc *p;
818 int error;
819
820 if (panicstr)
821 return;
822
823 p = (struct proc *)arg;
824 printf("Waiting (max %d seconds) for system process `%s' to stop... ",
825 kproc_shutdown_wait, p->p_comm);
826 error = kproc_suspend(p, kproc_shutdown_wait * hz);
827
828 if (error == EWOULDBLOCK)
829 printf("timed out\n");
830 else
831 printf("done\n");
832 }
833
834 void
835 kthread_shutdown(void *arg, int howto)
836 {
837 struct thread *td;
838 int error;
839
840 if (panicstr)
841 return;
842
843 td = (struct thread *)arg;
844 printf("Waiting (max %d seconds) for system thread `%s' to stop... ",
845 kproc_shutdown_wait, td->td_name);
846 error = kthread_suspend(td, kproc_shutdown_wait * hz);
847
848 if (error == EWOULDBLOCK)
849 printf("timed out\n");
850 else
851 printf("done\n");
852 }
853
854 static char dumpdevname[sizeof(((struct cdev*)NULL)->si_name)];
855 SYSCTL_STRING(_kern_shutdown, OID_AUTO, dumpdevname, CTLFLAG_RD,
856 dumpdevname, 0, "Device for kernel dumps");
857
858 /* Registration of dumpers */
859 int
860 set_dumper(struct dumperinfo *di, const char *devname, struct thread *td)
861 {
862 size_t wantcopy;
863 int error;
864
865 error = priv_check(td, PRIV_SETDUMPER);
866 if (error != 0)
867 return (error);
868
869 if (di == NULL) {
870 if (dumper.blockbuf != NULL)
871 free(dumper.blockbuf, M_DUMPER);
872 bzero(&dumper, sizeof(dumper));
873 dumpdevname[0] = '\0';
874 return (0);
875 }
876 if (dumper.dumper != NULL)
877 return (EBUSY);
878 dumper = *di;
879 wantcopy = strlcpy(dumpdevname, devname, sizeof(dumpdevname));
880 if (wantcopy >= sizeof(dumpdevname)) {
881 printf("set_dumper: device name truncated from '%s' -> '%s'\n",
882 devname, dumpdevname);
883 }
884 dumper.blockbuf = malloc(di->blocksize, M_DUMPER, M_WAITOK | M_ZERO);
885 return (0);
886 }
887
888 /* Call dumper with bounds checking. */
889 int
890 dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
891 off_t offset, size_t length)
892 {
893
894 if (length != 0 && (offset < di->mediaoffset ||
895 offset - di->mediaoffset + length > di->mediasize)) {
896 printf("Attempt to write outside dump device boundaries.\n"
897 "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
898 (intmax_t)offset, (intmax_t)di->mediaoffset,
899 (uintmax_t)length, (intmax_t)di->mediasize);
900 return (ENOSPC);
901 }
902 return (di->dumper(di->priv, virtual, physical, offset, length));
903 }
904
905 /* Call dumper with bounds checking. */
906 int
907 dump_write_pad(struct dumperinfo *di, void *virtual, vm_offset_t physical,
908 off_t offset, size_t length, size_t *size)
909 {
910 char *temp;
911 int ret;
912
913 if (length > di->blocksize)
914 return (ENOMEM);
915
916 *size = di->blocksize;
917 if (length == di->blocksize)
918 temp = virtual;
919 else {
920 temp = di->blockbuf;
921 memset(temp + length, 0, di->blocksize - length);
922 memcpy(temp, virtual, length);
923 }
924 ret = dump_write(di, temp, physical, offset, *size);
925
926 return (ret);
927 }
928
929
930 void
931 mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
932 uint64_t dumplen, uint32_t blksz)
933 {
934 size_t dstsize;
935
936 bzero(kdh, sizeof(*kdh));
937 strlcpy(kdh->magic, magic, sizeof(kdh->magic));
938 strlcpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
939 kdh->version = htod32(KERNELDUMPVERSION);
940 kdh->architectureversion = htod32(archver);
941 kdh->dumplength = htod64(dumplen);
942 kdh->dumptime = htod64(time_second);
943 kdh->blocksize = htod32(blksz);
944 strlcpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
945 dstsize = sizeof(kdh->versionstring);
946 if (strlcpy(kdh->versionstring, version, dstsize) >= dstsize)
947 kdh->versionstring[dstsize - 2] = '\n';
948 if (panicstr != NULL)
949 strlcpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
950 kdh->parity = kerneldump_parity(kdh);
951 }
952
953 #ifdef DDB
954 DB_SHOW_COMMAND(panic, db_show_panic)
955 {
956
957 if (panicstr == NULL)
958 db_printf("panicstr not set\n");
959 else
960 db_printf("panic: %s\n", panicstr);
961 }
962 #endif
Cache object: a741623f1eec839309182058e01d82f5
|