Home | History | Annotate | Line # | Download | only in sysmon
swwdog.c revision 1.14
      1  1.14  pgoyette /*	$NetBSD: swwdog.c,v 1.14 2015/04/18 10:49:31 pgoyette 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.14  pgoyette __KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.14 2015/04/18 10:49:31 pgoyette 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.14  pgoyette #include <sys/device.h>
     45   1.2     perry #include <sys/callout.h>
     46   1.2     perry #include <sys/kernel.h>
     47  1.10  pgoyette #include <sys/kmem.h>
     48   1.1       smb #include <sys/reboot.h>
     49   1.1       smb #include <sys/systm.h>
     50  1.10  pgoyette #include <sys/sysctl.h>
     51   1.1       smb #include <sys/wdog.h>
     52  1.12     pooka #include <sys/workqueue.h>
     53  1.14  pgoyette #include <sys/module.h>
     54   1.1       smb #include <dev/sysmon/sysmonvar.h>
     55   1.1       smb 
     56  1.14  pgoyette #ifndef _MODULE
     57  1.14  pgoyette #include "opt_modular.h"
     58  1.14  pgoyette #endif
     59  1.10  pgoyette 
     60   1.1       smb struct swwdog_softc {
     61  1.14  pgoyette 	device_t		sc_dev;
     62  1.14  pgoyette 	struct sysmon_wdog	sc_smw;
     63  1.14  pgoyette 	struct callout		sc_c;
     64  1.14  pgoyette 	int			sc_armed;
     65  1.10  pgoyette };
     66  1.10  pgoyette 
     67  1.14  pgoyette bool	swwdog_reboot = false;	/* false --> panic , true  --> reboot */
     68  1.14  pgoyette 
     69  1.14  pgoyette static struct workqueue *wq;
     70  1.14  pgoyette static device_t		swwdog_dev;
     71  1.14  pgoyette 
     72  1.14  pgoyette MODULE(MODULE_CLASS_DRIVER, swwdog, NULL);
     73  1.14  pgoyette 
     74  1.14  pgoyette #ifdef _LKM
     75  1.14  pgoyette CFDRIVER_DECL(swwdog, DV_DULL, NULL);
     76  1.14  pgoyette #endif
     77  1.14  pgoyette 
     78  1.14  pgoyette int swwdogattach(int);
     79  1.14  pgoyette 
     80  1.14  pgoyette static int	swwdog_setmode(struct sysmon_wdog *);
     81  1.14  pgoyette static int	swwdog_tickle(struct sysmon_wdog *);
     82  1.14  pgoyette static bool	swwdog_suspend(device_t, const pmf_qual_t *);
     83  1.14  pgoyette static int	swwdog_arm(struct swwdog_softc *);
     84  1.14  pgoyette static int	swwdog_disarm(struct swwdog_softc *);
     85  1.14  pgoyette 
     86  1.14  pgoyette static void	swwdog_panic(void *);
     87  1.14  pgoyette 
     88  1.14  pgoyette static void	swwdog_sysctl_setup(void);
     89  1.14  pgoyette static struct sysctllog *swwdog_sysctllog = NULL;
     90   1.1       smb 
     91  1.10  pgoyette static int	swwdog_match(device_t, cfdata_t, void *);
     92  1.10  pgoyette static void	swwdog_attach(device_t, device_t, void *);
     93  1.10  pgoyette static int	swwdog_detach(device_t, int);
     94  1.10  pgoyette static bool	swwdog_suspend(device_t, const pmf_qual_t *);
     95   1.1       smb 
     96  1.14  pgoyette static int	swwdog_init(void *);
     97  1.14  pgoyette static int	swwdog_fini(void *);
     98  1.14  pgoyette static int	swwdog_modcmd(modcmd_t, void *);
     99   1.1       smb 
    100  1.10  pgoyette CFATTACH_DECL_NEW(swwdog, sizeof(struct swwdog_softc),
    101  1.10  pgoyette 	swwdog_match, swwdog_attach, swwdog_detach, NULL);
    102  1.14  pgoyette extern struct cfdriver swwdog_cd;
    103  1.10  pgoyette 
    104  1.14  pgoyette #define	SWDOG_DEFAULT	60		/* 60-second default period */
    105  1.11     pooka 
    106  1.12     pooka static void
    107  1.12     pooka doreboot(struct work *wrkwrkwrk, void *p)
    108  1.12     pooka {
    109  1.12     pooka 
    110  1.12     pooka 	cpu_reboot(0, NULL);
    111  1.12     pooka }
    112  1.12     pooka 
    113  1.14  pgoyette int
    114  1.10  pgoyette swwdogattach(int n __unused)
    115   1.1       smb {
    116  1.14  pgoyette 	int error;
    117  1.10  pgoyette 	static struct cfdata cf;
    118  1.10  pgoyette 
    119  1.14  pgoyette printf("%s: entered\n", __func__); /* XXX PRG */
    120  1.12     pooka 	if (workqueue_create(&wq, "swwreboot", doreboot, NULL,
    121  1.12     pooka 	    PRI_NONE, IPL_NONE, 0) != 0) {
    122  1.12     pooka 		aprint_error("failed to create swwdog reboot wq");
    123  1.14  pgoyette 		return 1;
    124  1.12     pooka 	}
    125  1.12     pooka 
    126  1.14  pgoyette 	error = config_cfattach_attach(swwdog_cd.cd_name, &swwdog_ca);
    127  1.14  pgoyette 	if (error) {
    128  1.14  pgoyette 		aprint_error("%s: unable to attach cfattach\n",
    129  1.14  pgoyette 		    swwdog_cd.cd_name);
    130  1.12     pooka 		workqueue_destroy(wq);
    131  1.14  pgoyette 		return error;
    132  1.10  pgoyette 	}
    133   1.1       smb 
    134  1.10  pgoyette 	cf.cf_name = swwdog_cd.cd_name;
    135  1.10  pgoyette 	cf.cf_atname = swwdog_cd.cd_name;
    136  1.10  pgoyette 	cf.cf_unit = 0;
    137  1.10  pgoyette 	cf.cf_fstate = FSTATE_STAR;
    138  1.14  pgoyette 	cf.cf_pspec = NULL;
    139  1.14  pgoyette 	cf.cf_loc = NULL;
    140  1.14  pgoyette 	cf.cf_flags = 0;
    141  1.14  pgoyette 
    142  1.14  pgoyette 	swwdog_dev = config_attach_pseudo(&cf);
    143  1.14  pgoyette 
    144  1.14  pgoyette printf("%s: swwdog_dev = 0x%p\n", __func__, swwdog_dev); /* XXX PRG */
    145  1.14  pgoyette 	if (swwdog_dev == NULL) {
    146  1.14  pgoyette 		config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca);
    147  1.14  pgoyette 		workqueue_destroy(wq);
    148  1.14  pgoyette 		return 1;
    149  1.14  pgoyette 	}
    150  1.14  pgoyette 	return 0;
    151  1.10  pgoyette }
    152  1.10  pgoyette 
    153  1.10  pgoyette static int
    154  1.10  pgoyette swwdog_match(device_t parent, cfdata_t data, void *aux)
    155  1.10  pgoyette {
    156  1.14  pgoyette 
    157  1.14  pgoyette printf("%s: entered\n", __func__); /* XXX PRG */
    158  1.10  pgoyette 	return 1;
    159  1.10  pgoyette }
    160  1.10  pgoyette 
    161  1.10  pgoyette static void
    162  1.10  pgoyette swwdog_attach(device_t parent, device_t self, void *aux)
    163  1.10  pgoyette {
    164  1.10  pgoyette 	struct swwdog_softc *sc = device_private(self);
    165  1.10  pgoyette 
    166  1.14  pgoyette printf("%s: entered\n", __func__); /* XXX PRG */
    167  1.14  pgoyette 	if (workqueue_create(&wq, "swwreboot", doreboot, NULL,
    168  1.14  pgoyette 	    PRI_NONE, IPL_NONE, 0) != 0) {
    169  1.14  pgoyette 		aprint_error_dev(self, "failed to create reboot workqueue");
    170  1.14  pgoyette 	}
    171  1.14  pgoyette 
    172  1.10  pgoyette 	sc->sc_dev = self;
    173  1.10  pgoyette 	sc->sc_smw.smw_name = device_xname(self);
    174  1.10  pgoyette 	sc->sc_smw.smw_cookie = sc;
    175  1.10  pgoyette 	sc->sc_smw.smw_setmode = swwdog_setmode;
    176  1.10  pgoyette 	sc->sc_smw.smw_tickle = swwdog_tickle;
    177  1.10  pgoyette 	sc->sc_smw.smw_period = SWDOG_DEFAULT;
    178  1.14  pgoyette 
    179  1.10  pgoyette 	callout_init(&sc->sc_c, 0);
    180  1.10  pgoyette 	callout_setfunc(&sc->sc_c, swwdog_panic, sc);
    181  1.10  pgoyette 
    182  1.10  pgoyette 	if (sysmon_wdog_register(&sc->sc_smw) == 0)
    183  1.10  pgoyette 		aprint_normal_dev(self, "software watchdog initialized\n");
    184  1.14  pgoyette 	else {
    185  1.10  pgoyette 		aprint_error_dev(self, "unable to register software "
    186  1.10  pgoyette 		    "watchdog with sysmon\n");
    187  1.14  pgoyette 		callout_destroy(&sc->sc_c);
    188  1.14  pgoyette 		workqueue_destroy(wq);
    189  1.14  pgoyette 		return;
    190  1.14  pgoyette 	}
    191   1.1       smb 
    192  1.10  pgoyette 	if (!pmf_device_register(self, swwdog_suspend, NULL))
    193  1.10  pgoyette 		aprint_error_dev(self, "couldn't establish power handler\n");
    194  1.11     pooka 
    195  1.11     pooka 	swwdog_sysctl_setup();
    196  1.10  pgoyette }
    197  1.10  pgoyette 
    198  1.10  pgoyette static int
    199  1.10  pgoyette swwdog_detach(device_t self, int flags)
    200  1.10  pgoyette {
    201  1.10  pgoyette 	struct swwdog_softc *sc = device_private(self);
    202  1.10  pgoyette 
    203  1.14  pgoyette printf("%s: entered\n", __func__); /* XXX PRG */
    204  1.13  pgoyette 	pmf_device_deregister(self);
    205  1.10  pgoyette 	swwdog_disarm(sc);
    206  1.14  pgoyette 	sysctl_teardown(&swwdog_sysctllog);
    207  1.14  pgoyette 	sysmon_wdog_unregister(&sc->sc_smw);
    208  1.10  pgoyette 	callout_destroy(&sc->sc_c);
    209  1.12     pooka 	workqueue_destroy(wq);
    210  1.10  pgoyette 
    211  1.14  pgoyette 	return 0;
    212  1.10  pgoyette }
    213  1.10  pgoyette 
    214  1.10  pgoyette static bool
    215  1.10  pgoyette swwdog_suspend(device_t dev, const pmf_qual_t *qual)
    216  1.10  pgoyette {
    217  1.10  pgoyette 	struct swwdog_softc *sc = device_private(dev);
    218  1.10  pgoyette 
    219  1.10  pgoyette 	/* Don't allow suspend if watchdog is armed */
    220  1.10  pgoyette 	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
    221  1.10  pgoyette 		return false;
    222  1.10  pgoyette 	return true;
    223   1.1       smb }
    224   1.1       smb 
    225   1.1       smb static int
    226   1.1       smb swwdog_setmode(struct sysmon_wdog *smw)
    227   1.1       smb {
    228   1.2     perry 	struct swwdog_softc *sc = smw->smw_cookie;
    229   1.1       smb 	int error = 0;
    230   1.1       smb 
    231   1.1       smb 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    232   1.1       smb 		error = swwdog_disarm(sc);
    233   1.1       smb 	} else {
    234   1.1       smb 		if (smw->smw_period == 0)
    235   1.1       smb 			return EINVAL;
    236   1.1       smb 		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    237   1.1       smb 			sc->sc_smw.smw_period = SWDOG_DEFAULT;
    238   1.3    simonb 		else
    239   1.3    simonb 			sc->sc_smw.smw_period = smw->smw_period;
    240   1.1       smb 		error = swwdog_arm(sc);
    241   1.1       smb 	}
    242   1.1       smb 	return error;
    243   1.1       smb }
    244   1.1       smb 
    245   1.1       smb static int
    246   1.1       smb swwdog_tickle(struct sysmon_wdog *smw)
    247   1.1       smb {
    248   1.1       smb 
    249   1.1       smb 	return swwdog_arm(smw->smw_cookie);
    250   1.1       smb }
    251   1.1       smb 
    252   1.1       smb static int
    253   1.1       smb swwdog_arm(struct swwdog_softc *sc)
    254   1.1       smb {
    255   1.1       smb 
    256   1.1       smb 	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
    257   1.1       smb 	return 0;
    258   1.1       smb }
    259   1.1       smb 
    260   1.1       smb static int
    261   1.1       smb swwdog_disarm(struct swwdog_softc *sc)
    262   1.1       smb {
    263   1.1       smb 
    264   1.1       smb 	callout_stop(&sc->sc_c);
    265   1.1       smb 	return 0;
    266   1.1       smb }
    267   1.1       smb 
    268   1.1       smb static void
    269   1.2     perry swwdog_panic(void *vsc)
    270   1.1       smb {
    271   1.1       smb 	struct swwdog_softc *sc = vsc;
    272  1.12     pooka 	static struct work wk; /* we'll need it max once */
    273  1.10  pgoyette 	bool do_panic;
    274   1.1       smb 
    275  1.11     pooka 	do_panic = !swwdog_reboot;
    276  1.11     pooka 	swwdog_reboot = false;
    277   1.1       smb 	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */
    278   1.1       smb 
    279  1.14  pgoyette 	printf("%s: %d second timer expired\n", "swwdog",
    280   1.1       smb 	    sc->sc_smw.smw_period);
    281   1.1       smb 
    282   1.1       smb 	if (do_panic)
    283   1.1       smb 		panic("watchdog timer expired");
    284   1.3    simonb 	else
    285  1.12     pooka 		workqueue_enqueue(wq, &wk, NULL);
    286   1.1       smb }
    287  1.10  pgoyette 
    288  1.11     pooka static void
    289  1.11     pooka swwdog_sysctl_setup(void)
    290  1.10  pgoyette {
    291  1.10  pgoyette 	const struct sysctlnode *me;
    292  1.10  pgoyette 
    293  1.11     pooka 	KASSERT(swwdog_sysctllog == NULL);
    294  1.11     pooka 
    295  1.11     pooka 	sysctl_createv(&swwdog_sysctllog, 0, NULL, &me, CTLFLAG_READWRITE,
    296  1.11     pooka 	    CTLTYPE_NODE, "swwdog", NULL,
    297  1.10  pgoyette 	    NULL, 0, NULL, 0,
    298  1.11     pooka 	    CTL_HW, CTL_CREATE, CTL_EOL);
    299  1.11     pooka 	sysctl_createv(&swwdog_sysctllog, 0, NULL, NULL, CTLFLAG_READWRITE,
    300  1.11     pooka 	    CTLTYPE_BOOL, "reboot", "reboot if timer expires",
    301  1.11     pooka 	    NULL, 0, &swwdog_reboot, sizeof(bool),
    302  1.11     pooka 	    CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
    303  1.10  pgoyette }
    304  1.14  pgoyette 
    305  1.14  pgoyette /*
    306  1.14  pgoyette  * Module management
    307  1.14  pgoyette  */
    308  1.14  pgoyette 
    309  1.14  pgoyette static
    310  1.14  pgoyette int
    311  1.14  pgoyette swwdog_init(void *arg)
    312  1.14  pgoyette {
    313  1.14  pgoyette #ifdef _LKM
    314  1.14  pgoyette 	/*
    315  1.14  pgoyette 	 * Merge the driver info into the kernel tables and attach the
    316  1.14  pgoyette 	 * pseudo-device
    317  1.14  pgoyette 	 */
    318  1.14  pgoyette 	int error;
    319  1.14  pgoyette 
    320  1.14  pgoyette 	error = config_cfdriver_attach(&swwdog_cd);
    321  1.14  pgoyette 	if (error) {
    322  1.14  pgoyette 		aprint_error("%s: unable to attach cfdriver\n",
    323  1.14  pgoyette 		    swwdog_cd.cd_name);
    324  1.14  pgoyette 		return error;
    325  1.14  pgoyette 	}
    326  1.14  pgoyette 	error = swwdogattach(1);
    327  1.14  pgoyette 	if (error) {
    328  1.14  pgoyette 		aprint_error("%s: device attach failed\n", swwdog_cd.cd_name);
    329  1.14  pgoyette 		config_cfdriver_detach(&swwdog_cd);
    330  1.14  pgoyette 	}
    331  1.14  pgoyette 
    332  1.14  pgoyette 	return error;
    333  1.14  pgoyette #else
    334  1.14  pgoyette 
    335  1.14  pgoyette 	return 0;
    336  1.14  pgoyette #endif
    337  1.14  pgoyette }
    338  1.14  pgoyette 
    339  1.14  pgoyette static
    340  1.14  pgoyette int
    341  1.14  pgoyette swwdog_fini(void *arg)
    342  1.14  pgoyette {
    343  1.14  pgoyette 	int error;
    344  1.14  pgoyette 
    345  1.14  pgoyette 	error = config_detach(swwdog_dev, 0);
    346  1.14  pgoyette 
    347  1.14  pgoyette 	error = config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca);
    348  1.14  pgoyette 	if (error)
    349  1.14  pgoyette 		aprint_error("%s: error detaching cfattach: %d\n",
    350  1.14  pgoyette 		    swwdog_cd.cd_name, error);
    351  1.14  pgoyette 
    352  1.14  pgoyette #ifdef _LKM
    353  1.14  pgoyette 	error = config_cfdriver_detach(&swwdog_cd);
    354  1.14  pgoyette 	if (error)
    355  1.14  pgoyette 		aprint_error("%s: error detaching cfdriver: %d\n",
    356  1.14  pgoyette 		    swwdog_cd.cd_name, error);
    357  1.14  pgoyette #endif
    358  1.14  pgoyette 
    359  1.14  pgoyette         return error;
    360  1.14  pgoyette }
    361  1.14  pgoyette 
    362  1.14  pgoyette static
    363  1.14  pgoyette int
    364  1.14  pgoyette swwdog_modcmd(modcmd_t cmd, void *arg)
    365  1.14  pgoyette {
    366  1.14  pgoyette 	int ret;
    367  1.14  pgoyette 
    368  1.14  pgoyette printf("%s: cmd %d\n", __func__, cmd); /* XXX PRG */
    369  1.14  pgoyette 	switch (cmd) {
    370  1.14  pgoyette 	case MODULE_CMD_INIT:
    371  1.14  pgoyette 		ret = swwdog_init(arg);
    372  1.14  pgoyette 		break;
    373  1.14  pgoyette 
    374  1.14  pgoyette 	case MODULE_CMD_FINI:
    375  1.14  pgoyette 		ret = swwdog_fini(arg);
    376  1.14  pgoyette 		break;
    377  1.14  pgoyette 
    378  1.14  pgoyette 	case MODULE_CMD_STAT:
    379  1.14  pgoyette 	default:
    380  1.14  pgoyette 		ret = ENOTTY;
    381  1.14  pgoyette 	}
    382  1.14  pgoyette 
    383  1.14  pgoyette 	return ret;
    384  1.14  pgoyette }
    385  1.14  pgoyette 
    386