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