kern_shutdown.c revision 39522
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.39 1998/09/15 08:49:52 gibbs 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/malloc.h> 53#include <sys/kernel.h> 54#include <sys/mount.h> 55#include <sys/queue.h> 56#include <sys/sysctl.h> 57#include <sys/conf.h> 58#include <sys/sysproto.h> 59 60#include <machine/pcb.h> 61#include <machine/clock.h> 62#include <machine/cons.h> 63#include <machine/md_var.h> 64#ifdef SMP 65#include <machine/smp.h> /* smp_active, cpuid */ 66#endif 67 68#include <sys/signalvar.h> 69 70#ifndef PANIC_REBOOT_WAIT_TIME 71#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 72#endif 73 74/* 75 * Note that stdarg.h and the ANSI style va_start macro is used for both 76 * ANSI and traditional C compilers. 77 */ 78#include <machine/stdarg.h> 79 80#ifdef DDB 81#ifdef DDB_UNATTENDED 82static int debugger_on_panic = 0; 83#else 84static int debugger_on_panic = 1; 85#endif 86SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 87 &debugger_on_panic, 0, ""); 88#endif 89 90#ifdef HW_WDOG 91/* 92 * If there is a hardware watchdog, point this at the function needed to 93 * hold it off. 94 * It's needed when the kernel needs to do some lengthy operations. 95 * e.g. in wd.c when dumping core.. It's most annoying to have 96 * your precious core-dump only half written because the wdog kicked in. 97 */ 98watchdog_tickle_fn wdog_tickler = NULL; 99#endif /* HW_WDOG */ 100 101/* 102 * Variable panicstr contains argument to first call to panic; used as flag 103 * to indicate that the kernel has already called panic. 104 */ 105const char *panicstr; 106 107/* 108 * callout list for things to do a shutdown 109 */ 110typedef struct shutdown_list_element { 111 LIST_ENTRY(shutdown_list_element) links; 112 bootlist_fn function; 113 void *arg; 114} *sle_p; 115 116/* 117 * There are three shutdown lists. Some things need to be shut down 118 * earlier than others. 119 */ 120LIST_HEAD(shutdown_list, shutdown_list_element); 121 122static struct shutdown_list shutdown_lists[SHUTDOWN_FINAL + 1]; 123 124static void boot __P((int)) __dead2; 125static void dumpsys __P((void)); 126 127#ifndef _SYS_SYSPROTO_H_ 128struct reboot_args { 129 int opt; 130}; 131#endif 132/* ARGSUSED */ 133 134/* 135 * The system call that results in a reboot 136 */ 137int 138reboot(p, uap) 139 struct proc *p; 140 struct reboot_args *uap; 141{ 142 int error; 143 144 if ((error = suser(p->p_ucred, &p->p_acflag))) 145 return (error); 146 147 boot(uap->opt); 148 return (0); 149} 150 151/* 152 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 153 */ 154void 155shutdown_nice() 156{ 157 /* Send a signal to init(8) and have it shutdown the world */ 158 if (initproc != NULL) { 159 psignal(initproc, SIGINT); 160 } else { 161 /* No init(8) running, so simply reboot */ 162 boot(RB_NOSYNC); 163 } 164 return; 165} 166static int waittime = -1; 167static struct pcb dumppcb; 168 169/* 170 * Go through the rigmarole of shutting down.. 171 * this used to be in machdep.c but I'll be dammned if I could see 172 * anything machine dependant in it. 173 */ 174static void 175boot(howto) 176 int howto; 177{ 178 sle_p ep; 179 180#ifdef SMP 181 if (smp_active) { 182 printf("boot() called on cpu#%d\n", cpuid); 183 } 184#endif 185 /* 186 * Do any callouts that should be done BEFORE syncing the filesystems. 187 */ 188 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_PRE_SYNC], links) 189 (*ep->function)(howto, ep->arg); 190 191 /* 192 * Now sync filesystems 193 */ 194 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 195 register struct buf *bp; 196 int iter, nbusy; 197 198 waittime = 0; 199 printf("\nsyncing disks... "); 200 201 sync(&proc0, NULL); 202 203 /* 204 * With soft updates, some buffers that are 205 * written will be remarked as dirty until other 206 * buffers are written. 207 */ 208 for (iter = 0; iter < 20; iter++) { 209 nbusy = 0; 210 for (bp = &buf[nbuf]; --bp >= buf; ) { 211 if ((bp->b_flags & (B_BUSY | B_INVAL)) 212 == B_BUSY) { 213 nbusy++; 214 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 215 == B_DELWRI) { 216 /* bawrite(bp);*/ 217 nbusy++; 218 } 219 } 220 if (nbusy == 0) 221 break; 222 printf("%d ", nbusy); 223 sync(&proc0, NULL); 224 DELAY(50000 * iter); 225 } 226 if (nbusy) { 227 /* 228 * Failed to sync all blocks. Indicate this and don't 229 * unmount filesystems (thus forcing an fsck on reboot). 230 */ 231 printf("giving up\n"); 232#ifdef SHOW_BUSYBUFS 233 nbusy = 0; 234 for (bp = &buf[nbuf]; --bp >= buf; ) { 235 if ((bp->b_flags & (B_BUSY | B_INVAL)) 236 == B_BUSY) { 237 nbusy++; 238 printf( 239 "%d: dev:%08lx, flags:%08lx, blkno:%ld, lblkno:%ld\n", 240 nbusy, (u_long)bp->b_dev, 241 bp->b_flags, (long)bp->b_blkno, 242 (long)bp->b_lblkno); 243 } 244 } 245 DELAY(5000000); /* 5 seconds */ 246#endif 247 } else { 248 printf("done\n"); 249 /* 250 * Unmount filesystems 251 */ 252 if (panicstr == 0) 253 vfs_unmountall(); 254 } 255 DELAY(100000); /* wait for console output to finish */ 256 } 257 258 /* 259 * Ok, now do things that assume all filesystem activity has 260 * been completed. 261 */ 262 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_POST_SYNC], links) 263 (*ep->function)(howto, ep->arg); 264 splhigh(); 265 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) { 266 savectx(&dumppcb); 267#ifdef __i386__ 268 dumppcb.pcb_cr3 = rcr3(); 269#endif 270 dumpsys(); 271 } 272 273 /* Now that we're going to really halt the system... */ 274 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_FINAL], links) 275 (*ep->function)(howto, ep->arg); 276 277 if (howto & RB_HALT) { 278 cpu_power_down(); 279 printf("\n"); 280 printf("The operating system has halted.\n"); 281 printf("Please press any key to reboot.\n\n"); 282 switch (cngetc()) { 283 case -1: /* No console, just die */ 284 cpu_halt(); 285 /* NOTREACHED */ 286 default: 287 howto &= ~RB_HALT; 288 break; 289 } 290 } else if (howto & RB_DUMP) { 291 /* System Paniced */ 292 293 if (PANIC_REBOOT_WAIT_TIME != 0) { 294 if (PANIC_REBOOT_WAIT_TIME != -1) { 295 int loop; 296 printf("Automatic reboot in %d seconds - " 297 "press a key on the console to abort\n", 298 PANIC_REBOOT_WAIT_TIME); 299 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 300 loop > 0; --loop) { 301 DELAY(1000 * 100); /* 1/10th second */ 302 /* Did user type a key? */ 303 if (cncheckc() != -1) 304 break; 305 } 306 if (!loop) 307 goto die; 308 } 309 } else { /* zero time specified - reboot NOW */ 310 goto die; 311 } 312 printf("--> Press a key on the console to reboot <--\n"); 313 cngetc(); 314 } 315die: 316 printf("Rebooting...\n"); 317 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 318 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 319 cpu_reset(); 320 for(;;) ; 321 /* NOTREACHED */ 322} 323 324/* 325 * Magic number for savecore 326 * 327 * exported (symorder) and used at least by savecore(8) 328 * 329 */ 330static u_long const dumpmag = 0x8fca0101UL; 331 332static int dumpsize = 0; /* also for savecore */ 333 334static int dodump = 1; 335SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, ""); 336 337/* ARGSUSED */ 338static void dump_conf __P((void *dummy)); 339static void 340dump_conf(dummy) 341 void *dummy; 342{ 343 cpu_dumpconf(); 344} 345SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 346 347/* 348 * Doadump comes here after turning off memory management and 349 * getting on the dump stack, either when called above, or by 350 * the auto-restart code. 351 */ 352static void 353dumpsys(void) 354{ 355 356 if (!dodump) 357 return; 358 if (dumpdev == NODEV) 359 return; 360 if (!(bdevsw[major(dumpdev)])) 361 return; 362 if (!(bdevsw[major(dumpdev)]->d_dump)) 363 return; 364 dumpsize = Maxmem; 365 printf("\ndumping to dev %lx, offset %ld\n", (u_long)dumpdev, dumplo); 366 printf("dump "); 367 switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) { 368 369 case ENXIO: 370 printf("device bad\n"); 371 break; 372 373 case EFAULT: 374 printf("device not ready\n"); 375 break; 376 377 case EINVAL: 378 printf("area improper\n"); 379 break; 380 381 case EIO: 382 printf("i/o error\n"); 383 break; 384 385 case EINTR: 386 printf("aborted from console\n"); 387 break; 388 389 default: 390 printf("succeeded\n"); 391 break; 392 } 393} 394 395/* 396 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 397 * and then reboots. If we are called twice, then we avoid trying to sync 398 * the disks as this often leads to recursive panics. 399 */ 400void 401panic(const char *fmt, ...) 402{ 403 int bootopt; 404 va_list ap; 405 static char buf[256]; 406 407 bootopt = RB_AUTOBOOT | RB_DUMP; 408 if (panicstr) 409 bootopt |= RB_NOSYNC; 410 else 411 panicstr = fmt; 412 413 va_start(ap, fmt); 414 (void)vsprintf(buf, fmt, ap); 415 if (panicstr == fmt) 416 panicstr = buf; 417 va_end(ap); 418 printf("panic: %s\n", buf); 419#ifdef SMP 420 /* three seperate prints in case of an unmapped page and trap */ 421 printf("mp_lock = %08x; ", mp_lock); 422 printf("cpuid = %d; ", cpuid); 423 printf("lapic.id = %08x\n", lapic.id); 424#endif 425 426#if defined(DDB) 427 if (debugger_on_panic) 428 Debugger ("panic"); 429#endif 430 boot(bootopt); 431} 432 433/* 434 * Two routines to handle adding/deleting items on the 435 * shutdown callout lists 436 * 437 * at_shutdown(): 438 * Take the arguments given and put them onto the shutdown callout list. 439 * However first make sure that it's not already there. 440 * returns 0 on success. 441 */ 442int 443at_shutdown(bootlist_fn function, void *arg, int queue) 444{ 445 sle_p ep; 446 447 if (queue < SHUTDOWN_PRE_SYNC 448 || queue > SHUTDOWN_FINAL) { 449 printf("at_shutdown: bad exit callout queue %d specified\n", 450 queue); 451 return (EINVAL); 452 } 453 if (rm_at_shutdown(function, arg)) 454 printf("at_shutdown: exit callout entry was already present\n"); 455 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 456 if (ep == NULL) 457 return (ENOMEM); 458 ep->function = function; 459 ep->arg = arg; 460 LIST_INSERT_HEAD(&shutdown_lists[queue], ep, links); 461 return (0); 462} 463 464/* 465 * Scan the exit callout lists for the given items and remove them. 466 * Returns the number of items removed. 467 */ 468int 469rm_at_shutdown(bootlist_fn function, void *arg) 470{ 471 sle_p ep; 472 int count; 473 int queue; 474 475 count = 0; 476 for (queue = SHUTDOWN_PRE_SYNC; queue < SHUTDOWN_FINAL; queue++) { 477 LIST_FOREACH(ep, &shutdown_lists[queue], links) { 478 if ((ep->function == function) && (ep->arg == arg)) { 479 LIST_REMOVE(ep, links); 480 free(ep, M_TEMP); 481 count++; 482 } 483 } 484 } 485 return (count); 486} 487