Home | History | Annotate | Line # | Download | only in sysmon
swwdog.c revision 1.2.4.1
      1  1.2.4.1   yamt /*	$NetBSD: swwdog.c,v 1.2.4.1 2006/06/21 15:07:30 yamt Exp $	*/
      2      1.1    smb 
      3      1.1    smb /*
      4      1.1    smb  * Copyright (c) 2004, 2005 Steven M. Bellovin
      5      1.1    smb  * All rights reserved.
      6      1.1    smb  *
      7      1.1    smb  * Redistribution and use in source and binary forms, with or without
      8      1.1    smb  * modification, are permitted provided that the following conditions
      9      1.1    smb  * are met:
     10      1.1    smb  * 1. Redistributions of source code must retain the above copyright
     11      1.1    smb  *    notice, this list of conditions and the following disclaimer.
     12      1.1    smb  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1    smb  *    notice, this list of conditions and the following disclaimer in the
     14      1.1    smb  *    documentation and/or other materials provided with the distribution.
     15      1.1    smb  * 3. All advertising materials mentioning features or use of this software
     16      1.1    smb  *    must display the following acknowledgement:
     17      1.1    smb  *      This product includes software developed by Steven M. Bellovin
     18      1.1    smb  * 4. The name of the author contributors may not be used to endorse or
     19      1.1    smb  *    promote products derived from this software without specific prior
     20      1.1    smb  *    written permission.
     21      1.1    smb  *
     22      1.1    smb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
     23      1.1    smb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24      1.1    smb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25      1.1    smb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     26      1.1    smb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27      1.1    smb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28      1.1    smb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29      1.1    smb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30      1.1    smb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31      1.1    smb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32      1.1    smb  * POSSIBILITY OF SUCH DAMAGE.
     33      1.1    smb  */
     34      1.1    smb 
     35      1.1    smb #include <sys/cdefs.h>
     36  1.2.4.1   yamt __KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.2.4.1 2006/06/21 15:07:30 yamt Exp $");
     37      1.1    smb 
     38      1.1    smb /*
     39      1.1    smb  *
     40      1.1    smb  * Software watchdog timer
     41      1.1    smb  *
     42      1.1    smb  */
     43      1.1    smb #include <sys/param.h>
     44      1.2  perry #include <sys/callout.h>
     45      1.1    smb #include <sys/cdefs.h>
     46      1.2  perry #include <sys/device.h>
     47      1.2  perry #include <sys/kernel.h>
     48      1.1    smb #include <sys/reboot.h>
     49      1.1    smb #include <sys/systm.h>
     50      1.1    smb #include <sys/wdog.h>
     51      1.1    smb #include <dev/sysmon/sysmonvar.h>
     52      1.1    smb 
     53      1.1    smb #include "swwdog.h"
     54      1.1    smb 
     55      1.1    smb struct swwdog_softc {
     56      1.1    smb 	struct sysmon_wdog sc_smw;
     57      1.1    smb 	struct callout sc_c;
     58      1.1    smb 	char sc_name[20];
     59      1.1    smb 	int sc_wdog_armed;
     60      1.1    smb } sc_wdog[NSWWDOG];
     61      1.1    smb 
     62      1.1    smb void	swwdogattach(int);
     63      1.1    smb 
     64      1.1    smb static int swwdog_setmode(struct sysmon_wdog *);
     65      1.1    smb static int swwdog_tickle(struct sysmon_wdog *);
     66      1.1    smb 
     67      1.1    smb static int swwdog_arm(struct swwdog_softc *);
     68      1.1    smb static int swwdog_disarm(struct swwdog_softc *);
     69      1.1    smb 
     70      1.1    smb static void swwdog_panic(void *);
     71      1.1    smb 
     72      1.1    smb int swwdog_reboot = 0;		/* set for panic instead of reboot */
     73      1.1    smb 
     74      1.1    smb #define	SWDOG_DEFAULT	60		/* 60-second default period */
     75      1.1    smb 
     76      1.1    smb void
     77      1.1    smb swwdogattach(int count)
     78      1.1    smb {
     79      1.1    smb 	int i;
     80      1.1    smb 
     81      1.1    smb 	for (i = 0; i < NSWWDOG; i++) {
     82      1.1    smb 		struct swwdog_softc *sc = &sc_wdog[i];
     83      1.1    smb 
     84      1.1    smb 		snprintf(sc->sc_name, sizeof sc->sc_name, "swwdog%d", i);
     85      1.1    smb 		printf("%s: ", sc->sc_name);
     86      1.1    smb 		sc->sc_smw.smw_name = sc->sc_name;
     87      1.1    smb 		sc->sc_smw.smw_cookie = sc;
     88      1.1    smb 		sc->sc_smw.smw_setmode = swwdog_setmode;
     89      1.1    smb 		sc->sc_smw.smw_tickle = swwdog_tickle;
     90      1.1    smb 		sc->sc_smw.smw_period = SWDOG_DEFAULT;
     91      1.1    smb 		callout_init(&sc->sc_c);
     92      1.1    smb 		callout_setfunc(&sc->sc_c, swwdog_panic, sc);
     93      1.1    smb 
     94      1.1    smb 		if (sysmon_wdog_register(&sc->sc_smw) == 0)
     95      1.1    smb 			printf("software watchdog initialized\n");
     96  1.2.4.1   yamt 		else
     97  1.2.4.1   yamt 			printf("unable to register software watchdog "
     98  1.2.4.1   yamt 			    "with sysmon\n");
     99      1.1    smb 	}
    100      1.1    smb }
    101      1.1    smb 
    102      1.1    smb static int
    103      1.1    smb swwdog_setmode(struct sysmon_wdog *smw)
    104      1.1    smb {
    105      1.2  perry 	struct swwdog_softc *sc = smw->smw_cookie;
    106      1.1    smb 	int error = 0;
    107      1.1    smb 
    108      1.1    smb 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    109      1.1    smb 		error = swwdog_disarm(sc);
    110      1.1    smb 	} else {
    111      1.1    smb 		if (smw->smw_period == 0)
    112      1.1    smb 			return EINVAL;
    113      1.1    smb 		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    114      1.1    smb 			sc->sc_smw.smw_period = SWDOG_DEFAULT;
    115  1.2.4.1   yamt 		else
    116  1.2.4.1   yamt 			sc->sc_smw.smw_period = smw->smw_period;
    117      1.1    smb 		error = swwdog_arm(sc);
    118      1.1    smb 	}
    119      1.1    smb 	return error;
    120      1.1    smb }
    121      1.1    smb 
    122      1.1    smb static int
    123      1.1    smb swwdog_tickle(struct sysmon_wdog *smw)
    124      1.1    smb {
    125      1.1    smb 
    126      1.1    smb 	return swwdog_arm(smw->smw_cookie);
    127      1.1    smb }
    128      1.1    smb 
    129      1.1    smb static int
    130      1.1    smb swwdog_arm(struct swwdog_softc *sc)
    131      1.1    smb {
    132      1.1    smb 
    133      1.1    smb 	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
    134      1.1    smb 	return 0;
    135      1.1    smb }
    136      1.1    smb 
    137      1.1    smb static int
    138      1.1    smb swwdog_disarm(struct swwdog_softc *sc)
    139      1.1    smb {
    140      1.1    smb 
    141      1.1    smb 	callout_stop(&sc->sc_c);
    142      1.1    smb 	return 0;
    143      1.1    smb }
    144      1.1    smb 
    145      1.1    smb static void
    146      1.2  perry swwdog_panic(void *vsc)
    147      1.1    smb {
    148      1.1    smb 	struct swwdog_softc *sc = vsc;
    149      1.1    smb 	int do_panic;
    150      1.1    smb 
    151      1.1    smb 	do_panic = swwdog_reboot;
    152      1.1    smb 	swwdog_reboot = 1;
    153      1.1    smb 	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */
    154      1.1    smb 
    155      1.1    smb 	printf("%s: %d second timer expired", sc->sc_name,
    156      1.1    smb 	    sc->sc_smw.smw_period);
    157      1.1    smb 
    158      1.1    smb 	if (do_panic)
    159      1.1    smb 		panic("watchdog timer expired");
    160  1.2.4.1   yamt 	else
    161  1.2.4.1   yamt 		cpu_reboot(0, NULL);
    162      1.1    smb }
    163