kern_shutdown.c revision 50253
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.61 1999/08/21 06:24:20 msmith 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/eventhandler.h> 50#include <sys/buf.h> 51#include <sys/reboot.h> 52#include <sys/proc.h> 53#include <sys/vnode.h> 54#include <sys/malloc.h> 55#include <sys/kernel.h> 56#include <sys/mount.h> 57#include <sys/queue.h> 58#include <sys/sysctl.h> 59#include <sys/conf.h> 60#include <sys/sysproto.h> 61#include <sys/cons.h> 62 63#include <machine/pcb.h> 64#include <machine/clock.h> 65#include <machine/md_var.h> 66#ifdef SMP 67#include <machine/smp.h> /* smp_active, cpuid */ 68#endif 69 70#include <sys/signalvar.h> 71 72#ifndef PANIC_REBOOT_WAIT_TIME 73#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 74#endif 75 76/* 77 * Note that stdarg.h and the ANSI style va_start macro is used for both 78 * ANSI and traditional C compilers. 79 */ 80#include <machine/stdarg.h> 81 82#ifdef DDB 83#ifdef DDB_UNATTENDED 84int debugger_on_panic = 0; 85#else 86int debugger_on_panic = 1; 87#endif 88SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 89 &debugger_on_panic, 0, "Run debugger on kernel panic"); 90#endif 91 92SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 93 94#ifdef HW_WDOG 95/* 96 * If there is a hardware watchdog, point this at the function needed to 97 * hold it off. 98 * It's needed when the kernel needs to do some lengthy operations. 99 * e.g. in wd.c when dumping core.. It's most annoying to have 100 * your precious core-dump only half written because the wdog kicked in. 101 */ 102watchdog_tickle_fn wdog_tickler = NULL; 103#endif /* HW_WDOG */ 104 105/* 106 * Variable panicstr contains argument to first call to panic; used as flag 107 * to indicate that the kernel has already called panic. 108 */ 109const char *panicstr; 110 111static void boot __P((int)) __dead2; 112static void dumpsys __P((void)); 113static int setdumpdev __P((dev_t dev)); 114static void poweroff_wait __P((void *, int)); 115static void shutdown_halt __P((void *junk, int howto)); 116static void shutdown_panic __P((void *junk, int howto)); 117static void shutdown_reset __P((void *junk, int howto)); 118 119/* register various local shutdown events */ 120static void 121shutdown_conf(void *unused) 122{ 123 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 124 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 125 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 126 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 127} 128 129SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 130 131/* ARGSUSED */ 132 133/* 134 * The system call that results in a reboot 135 */ 136int 137reboot(p, uap) 138 struct proc *p; 139 struct reboot_args *uap; 140{ 141 int error; 142 143 if ((error = suser(p))) 144 return (error); 145 146 boot(uap->opt); 147 return (0); 148} 149 150/* 151 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 152 */ 153void 154shutdown_nice() 155{ 156 /* Send a signal to init(8) and have it shutdown the world */ 157 if (initproc != NULL) { 158 psignal(initproc, SIGINT); 159 } else { 160 /* No init(8) running, so simply reboot */ 161 boot(RB_NOSYNC); 162 } 163 return; 164} 165static int waittime = -1; 166static struct pcb dumppcb; 167 168/* 169 * Go through the rigmarole of shutting down.. 170 * this used to be in machdep.c but I'll be dammned if I could see 171 * anything machine dependant in it. 172 */ 173static void 174boot(howto) 175 int howto; 176{ 177 178#ifdef SMP 179 if (smp_active) { 180 printf("boot() called on cpu#%d\n", cpuid); 181 } 182#endif 183 /* 184 * Do any callouts that should be done BEFORE syncing the filesystems. 185 */ 186 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 187 188 /* 189 * Now sync filesystems 190 */ 191 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 192 register struct buf *bp; 193 int iter, nbusy; 194 195 waittime = 0; 196 printf("\nsyncing disks... "); 197 198 sync(&proc0, NULL); 199 200 /* 201 * With soft updates, some buffers that are 202 * written will be remarked as dirty until other 203 * buffers are written. 204 */ 205 for (iter = 0; iter < 20; iter++) { 206 nbusy = 0; 207 for (bp = &buf[nbuf]; --bp >= buf; ) { 208 if ((bp->b_flags & B_INVAL) == 0 && 209 BUF_REFCNT(bp) > 0) { 210 nbusy++; 211 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 212 == B_DELWRI) { 213 /* bawrite(bp);*/ 214 nbusy++; 215 } 216 } 217 if (nbusy == 0) 218 break; 219 printf("%d ", nbusy); 220 sync(&proc0, NULL); 221 DELAY(50000 * iter); 222 } 223 /* 224 * Count only busy local buffers to prevent forcing 225 * a fsck if we're just a client of a wedged NFS server 226 */ 227 nbusy = 0; 228 for (bp = &buf[nbuf]; --bp >= buf; ) { 229 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 230 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 231 if (bp->b_dev == NODEV) 232 CIRCLEQ_REMOVE(&mountlist, 233 bp->b_vp->v_mount, mnt_list); 234 else 235 nbusy++; 236 } 237 238 239 } 240 if (nbusy) { 241 /* 242 * Failed to sync all blocks. Indicate this and don't 243 * unmount filesystems (thus forcing an fsck on reboot). 244 */ 245 printf("giving up\n"); 246#ifdef SHOW_BUSYBUFS 247 nbusy = 0; 248 for (bp = &buf[nbuf]; --bp >= buf; ) { 249 if ((bp->b_flags & B_INVAL) == 0 && 250 BUF_REFCNT(bp) > 0) { 251 nbusy++; 252 printf( 253 "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 254 nbusy, devtoname(bp->b_dev), 255 bp->b_flags, (long)bp->b_blkno, 256 (long)bp->b_lblkno); 257 } 258 } 259 DELAY(5000000); /* 5 seconds */ 260#endif 261 } else { 262 printf("done\n"); 263 /* 264 * Unmount filesystems 265 */ 266 if (panicstr == 0) 267 vfs_unmountall(); 268 } 269 DELAY(100000); /* wait for console output to finish */ 270 } 271 272 /* 273 * Ok, now do things that assume all filesystem activity has 274 * been completed. 275 */ 276 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 277 splhigh(); 278 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) { 279 savectx(&dumppcb); 280#ifdef __i386__ 281 dumppcb.pcb_cr3 = rcr3(); 282#endif 283 dumpsys(); 284 } 285 286 /* Now that we're going to really halt the system... */ 287 EVENTHANDLER_INVOKE(shutdown_final, howto); 288 289 for(;;) ; /* safety against shutdown_reset not working */ 290 /* NOTREACHED */ 291} 292 293/* 294 * If the shutdown was a clean halt, behave accordingly. 295 */ 296static void 297shutdown_halt(void *junk, int howto) 298{ 299 if (howto & RB_HALT) { 300 printf("\n"); 301 printf("The operating system has halted.\n"); 302 printf("Please press any key to reboot.\n\n"); 303 switch (cngetc()) { 304 case -1: /* No console, just die */ 305 cpu_halt(); 306 /* NOTREACHED */ 307 default: 308 howto &= ~RB_HALT; 309 break; 310 } 311 } 312} 313 314/* 315 * Check to see if the system paniced, pause and then reboot 316 * according to the specified delay. 317 */ 318static void 319shutdown_panic(void *junk, int howto) 320{ 321 int loop; 322 323 if (howto & RB_DUMP) { 324 if (PANIC_REBOOT_WAIT_TIME != 0) { 325 if (PANIC_REBOOT_WAIT_TIME != -1) { 326 printf("Automatic reboot in %d seconds - " 327 "press a key on the console to abort\n", 328 PANIC_REBOOT_WAIT_TIME); 329 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 330 loop > 0; --loop) { 331 DELAY(1000 * 100); /* 1/10th second */ 332 /* Did user type a key? */ 333 if (cncheckc() != -1) 334 break; 335 } 336 if (!loop) 337 return; 338 } 339 } else { /* zero time specified - reboot NOW */ 340 return; 341 } 342 printf("--> Press a key on the console to reboot <--\n"); 343 cngetc(); 344 } 345} 346 347/* 348 * Everything done, now reset 349 */ 350static void 351shutdown_reset(void *junk, int howto) 352{ 353 printf("Rebooting...\n"); 354 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 355 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 356 cpu_reset(); 357 /* NOTREACHED */ /* assuming reset worked */ 358} 359 360/* 361 * Magic number for savecore 362 * 363 * exported (symorder) and used at least by savecore(8) 364 * 365 */ 366static u_long const dumpmag = 0x8fca0101UL; 367 368static int dumpsize = 0; /* also for savecore */ 369 370static int dodump = 1; 371 372SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 373 "Try to perform coredump on kernel panic"); 374 375static int 376setdumpdev(dev) 377 dev_t dev; 378{ 379 int maj, psize; 380 long newdumplo; 381 382 if (dev == NODEV) { 383 dumpdev = dev; 384 return (0); 385 } 386 maj = major(dev); 387 if (devsw(dev) == NULL) 388 return (ENXIO); /* XXX is this right? */ 389 if (devsw(dev)->d_psize == NULL) 390 return (ENXIO); /* XXX should be ENODEV ? */ 391 psize = devsw(dev)->d_psize(dev); 392 if (psize == -1) 393 return (ENXIO); /* XXX should be ENODEV ? */ 394 /* 395 * XXX should clean up checking in dumpsys() to be more like this, 396 * and nuke dodump sysctl (too many knobs). 397 */ 398 newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE; 399 if (newdumplo < 0) 400 return (ENOSPC); 401 dumpdev = dev; 402 dumplo = newdumplo; 403 return (0); 404} 405 406 407/* ARGSUSED */ 408static void dump_conf __P((void *dummy)); 409static void 410dump_conf(dummy) 411 void *dummy; 412{ 413 if (setdumpdev(dumpdev) != 0) 414 dumpdev = NODEV; 415} 416 417SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 418 419static int 420sysctl_kern_dumpdev SYSCTL_HANDLER_ARGS 421{ 422 int error; 423 udev_t ndumpdev; 424 425 ndumpdev = dev2budev(dumpdev); 426 error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 427 if (error == 0 && req->newptr != NULL) 428 error = setdumpdev(udev2dev(ndumpdev, 1)); 429 return (error); 430} 431 432SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 433 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", ""); 434 435/* 436 * Doadump comes here after turning off memory management and 437 * getting on the dump stack, either when called above, or by 438 * the auto-restart code. 439 */ 440static void 441dumpsys(void) 442{ 443 int error; 444 445 if (!dodump) 446 return; 447 if (dumpdev == NODEV) 448 return; 449 if (!(devsw(dumpdev))) 450 return; 451 if (!(devsw(dumpdev)->d_dump)) 452 return; 453 dumpsize = Maxmem; 454 printf("\ndumping to dev (%d,%d), offset %ld\n", 455 major(dumpdev), minor(dumpdev), dumplo); 456 printf("dump "); 457 error = (*devsw(dumpdev)->d_dump)(dumpdev); 458 if (error == 0) { 459 printf("succeeded\n"); 460 return; 461 } 462 printf("failed, reason: "); 463 switch (error) { 464 case ENODEV: 465 printf("device doesn't support a dump routine\n"); 466 break; 467 468 case ENXIO: 469 printf("device bad\n"); 470 break; 471 472 case EFAULT: 473 printf("device not ready\n"); 474 break; 475 476 case EINVAL: 477 printf("area improper\n"); 478 break; 479 480 case EIO: 481 printf("i/o error\n"); 482 break; 483 484 case EINTR: 485 printf("aborted from console\n"); 486 break; 487 488 default: 489 printf("unknown, error = %d\n", error); 490 break; 491 } 492} 493 494/* 495 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 496 * and then reboots. If we are called twice, then we avoid trying to sync 497 * the disks as this often leads to recursive panics. 498 */ 499void 500panic(const char *fmt, ...) 501{ 502 int bootopt; 503 va_list ap; 504 static char buf[256]; 505 506 bootopt = RB_AUTOBOOT | RB_DUMP; 507 if (panicstr) 508 bootopt |= RB_NOSYNC; 509 else 510 panicstr = fmt; 511 512 va_start(ap, fmt); 513 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 514 if (panicstr == fmt) 515 panicstr = buf; 516 va_end(ap); 517 printf("panic: %s\n", buf); 518#ifdef SMP 519 /* three seperate prints in case of an unmapped page and trap */ 520 printf("mp_lock = %08x; ", mp_lock); 521 printf("cpuid = %d; ", cpuid); 522 printf("lapic.id = %08x\n", lapic.id); 523#endif 524 525#if defined(DDB) 526 if (debugger_on_panic) 527 Debugger ("panic"); 528#endif 529 boot(bootopt); 530} 531 532/* 533 * Support for poweroff delay. 534 */ 535static int poweroff_delay = 0; 536SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 537 &poweroff_delay, 0, ""); 538 539static void 540poweroff_wait(void *junk, int howto) 541{ 542 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 543 return; 544 DELAY(poweroff_delay * 1000); 545} 546