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