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