2021-12-31 14:05:41 +03:00
|
|
|
/* $NetBSD: swwdog.c,v 1.23 2021/12/31 11:05:41 riastradh Exp $ */
|
2005-01-10 01:51:32 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2004, 2005 Steven M. Bellovin
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Steven M. Bellovin
|
|
|
|
* 4. The name of the author contributors may not be used to endorse or
|
|
|
|
* promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2021-12-31 14:05:41 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.23 2021/12/31 11:05:41 riastradh Exp $");
|
2005-01-10 01:51:32 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Software watchdog timer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
2005-02-27 03:26:58 +03:00
|
|
|
#include <sys/device.h>
|
2015-04-18 13:49:31 +03:00
|
|
|
#include <sys/callout.h>
|
2005-02-27 03:26:58 +03:00
|
|
|
#include <sys/kernel.h>
|
2010-07-22 18:10:14 +04:00
|
|
|
#include <sys/kmem.h>
|
2005-01-10 01:51:32 +03:00
|
|
|
#include <sys/reboot.h>
|
|
|
|
#include <sys/systm.h>
|
2010-07-22 18:10:14 +04:00
|
|
|
#include <sys/sysctl.h>
|
2005-01-10 01:51:32 +03:00
|
|
|
#include <sys/wdog.h>
|
2010-11-12 00:55:04 +03:00
|
|
|
#include <sys/workqueue.h>
|
2015-04-18 13:49:31 +03:00
|
|
|
#include <sys/module.h>
|
2005-01-10 01:51:32 +03:00
|
|
|
#include <dev/sysmon/sysmonvar.h>
|
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
#ifndef _MODULE
|
|
|
|
#include "opt_modular.h"
|
|
|
|
#endif
|
2010-07-22 18:10:14 +04:00
|
|
|
|
2005-01-10 01:51:32 +03:00
|
|
|
struct swwdog_softc {
|
2015-04-18 13:49:31 +03:00
|
|
|
device_t sc_dev;
|
|
|
|
struct sysmon_wdog sc_smw;
|
|
|
|
struct callout sc_c;
|
|
|
|
int sc_armed;
|
2010-07-22 18:10:14 +04:00
|
|
|
};
|
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
bool swwdog_reboot = false; /* false --> panic , true --> reboot */
|
2005-01-10 01:51:32 +03:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
static struct workqueue *wq;
|
|
|
|
static device_t swwdog_dev;
|
|
|
|
|
2015-04-24 02:23:00 +03:00
|
|
|
MODULE(MODULE_CLASS_DRIVER, swwdog, "sysmon_wdog");
|
2005-01-10 01:51:32 +03:00
|
|
|
|
2015-04-24 22:49:24 +03:00
|
|
|
#ifdef _MODULE
|
2015-04-18 13:49:31 +03:00
|
|
|
CFDRIVER_DECL(swwdog, DV_DULL, NULL);
|
2015-04-24 22:49:24 +03:00
|
|
|
#endif
|
2005-01-10 01:51:32 +03:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
int swwdogattach(int);
|
2005-01-10 01:51:32 +03:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
static int swwdog_setmode(struct sysmon_wdog *);
|
|
|
|
static int swwdog_tickle(struct sysmon_wdog *);
|
|
|
|
static bool swwdog_suspend(device_t, const pmf_qual_t *);
|
|
|
|
static int swwdog_arm(struct swwdog_softc *);
|
|
|
|
static int swwdog_disarm(struct swwdog_softc *);
|
2005-01-10 01:51:32 +03:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
static void swwdog_panic(void *);
|
2005-01-10 01:51:32 +03:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
static int swwdog_match(device_t, cfdata_t, void *);
|
|
|
|
static void swwdog_attach(device_t, device_t, void *);
|
|
|
|
static int swwdog_detach(device_t, int);
|
|
|
|
static bool swwdog_suspend(device_t, const pmf_qual_t *);
|
|
|
|
|
|
|
|
static int swwdog_init(void *);
|
|
|
|
static int swwdog_fini(void *);
|
|
|
|
static int swwdog_modcmd(modcmd_t, void *);
|
2005-01-10 01:51:32 +03:00
|
|
|
|
2010-07-22 18:10:14 +04:00
|
|
|
CFATTACH_DECL_NEW(swwdog, sizeof(struct swwdog_softc),
|
|
|
|
swwdog_match, swwdog_attach, swwdog_detach, NULL);
|
2015-04-18 13:49:31 +03:00
|
|
|
extern struct cfdriver swwdog_cd;
|
2010-07-22 18:10:14 +04:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
#define SWDOG_DEFAULT 60 /* 60-second default period */
|
2010-08-06 20:02:56 +04:00
|
|
|
|
2010-11-12 00:55:04 +03:00
|
|
|
static void
|
|
|
|
doreboot(struct work *wrkwrkwrk, void *p)
|
|
|
|
{
|
|
|
|
|
2020-01-02 01:57:16 +03:00
|
|
|
kern_reboot(0, NULL);
|
2010-11-12 00:55:04 +03:00
|
|
|
}
|
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
int
|
2010-07-22 18:10:14 +04:00
|
|
|
swwdogattach(int n __unused)
|
2005-01-10 01:51:32 +03:00
|
|
|
{
|
2015-04-18 13:49:31 +03:00
|
|
|
int error;
|
2010-07-22 18:10:14 +04:00
|
|
|
static struct cfdata cf;
|
|
|
|
|
2010-11-12 00:55:04 +03:00
|
|
|
if (workqueue_create(&wq, "swwreboot", doreboot, NULL,
|
|
|
|
PRI_NONE, IPL_NONE, 0) != 0) {
|
|
|
|
aprint_error("failed to create swwdog reboot wq");
|
2015-04-18 13:49:31 +03:00
|
|
|
return 1;
|
2010-11-12 00:55:04 +03:00
|
|
|
}
|
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
error = config_cfattach_attach(swwdog_cd.cd_name, &swwdog_ca);
|
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to attach cfattach\n",
|
|
|
|
swwdog_cd.cd_name);
|
2010-11-12 00:55:04 +03:00
|
|
|
workqueue_destroy(wq);
|
2015-04-18 13:49:31 +03:00
|
|
|
return error;
|
2005-01-10 01:51:32 +03:00
|
|
|
}
|
2010-07-22 18:10:14 +04:00
|
|
|
|
|
|
|
cf.cf_name = swwdog_cd.cd_name;
|
|
|
|
cf.cf_atname = swwdog_cd.cd_name;
|
|
|
|
cf.cf_unit = 0;
|
|
|
|
cf.cf_fstate = FSTATE_STAR;
|
2015-04-18 13:49:31 +03:00
|
|
|
cf.cf_pspec = NULL;
|
|
|
|
cf.cf_loc = NULL;
|
|
|
|
cf.cf_flags = 0;
|
2010-07-22 18:10:14 +04:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
swwdog_dev = config_attach_pseudo(&cf);
|
2010-07-22 18:10:14 +04:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
if (swwdog_dev == NULL) {
|
|
|
|
config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca);
|
|
|
|
workqueue_destroy(wq);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-07-22 18:10:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swwdog_match(device_t parent, cfdata_t data, void *aux)
|
|
|
|
{
|
2015-04-18 13:49:31 +03:00
|
|
|
|
2010-07-22 18:10:14 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
swwdog_attach(device_t parent, device_t self, void *aux)
|
|
|
|
{
|
|
|
|
struct swwdog_softc *sc = device_private(self);
|
|
|
|
|
|
|
|
sc->sc_dev = self;
|
|
|
|
sc->sc_smw.smw_name = device_xname(self);
|
|
|
|
sc->sc_smw.smw_cookie = sc;
|
|
|
|
sc->sc_smw.smw_setmode = swwdog_setmode;
|
|
|
|
sc->sc_smw.smw_tickle = swwdog_tickle;
|
|
|
|
sc->sc_smw.smw_period = SWDOG_DEFAULT;
|
2015-04-18 13:49:31 +03:00
|
|
|
|
2010-07-22 18:10:14 +04:00
|
|
|
callout_init(&sc->sc_c, 0);
|
|
|
|
callout_setfunc(&sc->sc_c, swwdog_panic, sc);
|
|
|
|
|
|
|
|
if (sysmon_wdog_register(&sc->sc_smw) == 0)
|
|
|
|
aprint_normal_dev(self, "software watchdog initialized\n");
|
2015-04-18 13:49:31 +03:00
|
|
|
else {
|
2010-07-22 18:10:14 +04:00
|
|
|
aprint_error_dev(self, "unable to register software "
|
|
|
|
"watchdog with sysmon\n");
|
2015-04-18 13:49:31 +03:00
|
|
|
callout_destroy(&sc->sc_c);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-22 18:10:14 +04:00
|
|
|
|
|
|
|
if (!pmf_device_register(self, swwdog_suspend, NULL))
|
|
|
|
aprint_error_dev(self, "couldn't establish power handler\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swwdog_detach(device_t self, int flags)
|
|
|
|
{
|
|
|
|
struct swwdog_softc *sc = device_private(self);
|
|
|
|
|
2014-04-13 17:19:50 +04:00
|
|
|
pmf_device_deregister(self);
|
2010-07-22 18:10:14 +04:00
|
|
|
swwdog_disarm(sc);
|
2015-04-18 13:49:31 +03:00
|
|
|
sysmon_wdog_unregister(&sc->sc_smw);
|
|
|
|
callout_destroy(&sc->sc_c);
|
2010-11-12 00:55:04 +03:00
|
|
|
workqueue_destroy(wq);
|
2010-07-22 18:10:14 +04:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
return 0;
|
2010-07-22 18:10:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
swwdog_suspend(device_t dev, const pmf_qual_t *qual)
|
|
|
|
{
|
|
|
|
struct swwdog_softc *sc = device_private(dev);
|
|
|
|
|
|
|
|
/* Don't allow suspend if watchdog is armed */
|
|
|
|
if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
|
|
|
|
return false;
|
|
|
|
return true;
|
2005-01-10 01:51:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swwdog_setmode(struct sysmon_wdog *smw)
|
|
|
|
{
|
2005-02-27 03:26:58 +03:00
|
|
|
struct swwdog_softc *sc = smw->smw_cookie;
|
2005-01-10 01:51:32 +03:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
|
|
|
|
error = swwdog_disarm(sc);
|
|
|
|
} else {
|
|
|
|
if (smw->smw_period == 0)
|
|
|
|
return EINVAL;
|
|
|
|
else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
|
|
|
|
sc->sc_smw.smw_period = SWDOG_DEFAULT;
|
2005-10-17 07:08:24 +04:00
|
|
|
else
|
|
|
|
sc->sc_smw.smw_period = smw->smw_period;
|
2005-01-10 01:51:32 +03:00
|
|
|
error = swwdog_arm(sc);
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swwdog_tickle(struct sysmon_wdog *smw)
|
|
|
|
{
|
|
|
|
|
|
|
|
return swwdog_arm(smw->smw_cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swwdog_arm(struct swwdog_softc *sc)
|
|
|
|
{
|
|
|
|
|
|
|
|
callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swwdog_disarm(struct swwdog_softc *sc)
|
|
|
|
{
|
|
|
|
|
|
|
|
callout_stop(&sc->sc_c);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-02-27 03:26:58 +03:00
|
|
|
swwdog_panic(void *vsc)
|
2005-01-10 01:51:32 +03:00
|
|
|
{
|
|
|
|
struct swwdog_softc *sc = vsc;
|
2010-11-12 00:55:04 +03:00
|
|
|
static struct work wk; /* we'll need it max once */
|
2010-07-22 18:10:14 +04:00
|
|
|
bool do_panic;
|
2005-01-10 01:51:32 +03:00
|
|
|
|
2010-08-06 20:02:56 +04:00
|
|
|
do_panic = !swwdog_reboot;
|
|
|
|
swwdog_reboot = false;
|
2005-01-10 01:51:32 +03:00
|
|
|
callout_schedule(&sc->sc_c, 60 * hz); /* deliberate double-panic */
|
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
printf("%s: %d second timer expired\n", "swwdog",
|
2005-01-10 01:51:32 +03:00
|
|
|
sc->sc_smw.smw_period);
|
|
|
|
|
|
|
|
if (do_panic)
|
|
|
|
panic("watchdog timer expired");
|
2005-10-17 07:08:24 +04:00
|
|
|
else
|
2010-11-12 00:55:04 +03:00
|
|
|
workqueue_enqueue(wq, &wk, NULL);
|
2005-01-10 01:51:32 +03:00
|
|
|
}
|
2010-07-22 18:10:14 +04:00
|
|
|
|
2020-03-17 00:20:09 +03:00
|
|
|
SYSCTL_SETUP(swwdog_sysctl_setup, "swwdog sysctl")
|
2010-07-22 18:10:14 +04:00
|
|
|
{
|
|
|
|
const struct sysctlnode *me;
|
|
|
|
|
2020-03-17 00:20:09 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, &me, CTLFLAG_READWRITE,
|
2010-08-06 20:02:56 +04:00
|
|
|
CTLTYPE_NODE, "swwdog", NULL,
|
2010-07-22 18:10:14 +04:00
|
|
|
NULL, 0, NULL, 0,
|
2010-08-06 20:02:56 +04:00
|
|
|
CTL_HW, CTL_CREATE, CTL_EOL);
|
2020-03-17 00:20:09 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL, CTLFLAG_READWRITE,
|
2010-08-06 20:02:56 +04:00
|
|
|
CTLTYPE_BOOL, "reboot", "reboot if timer expires",
|
|
|
|
NULL, 0, &swwdog_reboot, sizeof(bool),
|
|
|
|
CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
|
2010-07-22 18:10:14 +04:00
|
|
|
}
|
2015-04-18 13:49:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Module management
|
|
|
|
*/
|
|
|
|
|
2021-12-31 14:05:41 +03:00
|
|
|
static int
|
2015-04-18 13:49:31 +03:00
|
|
|
swwdog_init(void *arg)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Merge the driver info into the kernel tables and attach the
|
|
|
|
* pseudo-device
|
|
|
|
*/
|
2015-05-12 13:20:14 +03:00
|
|
|
int error = 0;
|
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
|
2015-05-12 05:38:00 +03:00
|
|
|
#ifdef _MODULE
|
2015-04-18 13:49:31 +03:00
|
|
|
error = config_cfdriver_attach(&swwdog_cd);
|
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to attach cfdriver\n",
|
|
|
|
swwdog_cd.cd_name);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
error = swwdogattach(1);
|
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: device attach failed\n", swwdog_cd.cd_name);
|
|
|
|
config_cfdriver_detach(&swwdog_cd);
|
|
|
|
}
|
2015-05-12 05:38:00 +03:00
|
|
|
#endif
|
2015-04-18 13:49:31 +03:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:05:41 +03:00
|
|
|
static int
|
2015-04-18 13:49:31 +03:00
|
|
|
swwdog_fini(void *arg)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = config_detach(swwdog_dev, 0);
|
|
|
|
|
2015-05-12 05:38:00 +03:00
|
|
|
#ifdef _MODULE
|
2015-04-18 13:49:31 +03:00
|
|
|
error = config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca);
|
|
|
|
if (error)
|
|
|
|
aprint_error("%s: error detaching cfattach: %d\n",
|
|
|
|
swwdog_cd.cd_name, error);
|
|
|
|
|
|
|
|
error = config_cfdriver_detach(&swwdog_cd);
|
|
|
|
if (error)
|
|
|
|
aprint_error("%s: error detaching cfdriver: %d\n",
|
|
|
|
swwdog_cd.cd_name, error);
|
2015-05-12 05:38:00 +03:00
|
|
|
#endif
|
2015-04-18 13:49:31 +03:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:05:41 +03:00
|
|
|
static int
|
2015-04-18 13:49:31 +03:00
|
|
|
swwdog_modcmd(modcmd_t cmd, void *arg)
|
|
|
|
{
|
|
|
|
int ret;
|
2021-12-31 14:05:41 +03:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
|
|
|
ret = swwdog_init(arg);
|
|
|
|
break;
|
|
|
|
case MODULE_CMD_FINI:
|
|
|
|
ret = swwdog_fini(arg);
|
|
|
|
break;
|
|
|
|
case MODULE_CMD_STAT:
|
|
|
|
default:
|
|
|
|
ret = ENOTTY;
|
|
|
|
}
|
2021-12-31 14:05:41 +03:00
|
|
|
|
2015-04-18 13:49:31 +03:00
|
|
|
return ret;
|
|
|
|
}
|