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