1 /* $NetBSD: swwdog.c,v 1.24 2024/11/07 09:47:40 rin Exp $ */ 2 3 /* 4 * Copyright (c) 2004, 2005 Steven M. Bellovin 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Steven M. Bellovin 18 * 4. The name of the author contributors may not be used to endorse or 19 * promote products derived from this software without specific prior 20 * written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.24 2024/11/07 09:47:40 rin Exp $"); 37 38 /* 39 * 40 * Software watchdog timer 41 * 42 */ 43 #include <sys/param.h> 44 #include <sys/device.h> 45 #include <sys/callout.h> 46 #include <sys/kernel.h> 47 #include <sys/kmem.h> 48 #include <sys/reboot.h> 49 #include <sys/systm.h> 50 #include <sys/sysctl.h> 51 #include <sys/wdog.h> 52 #include <sys/workqueue.h> 53 #include <sys/module.h> 54 #include <dev/sysmon/sysmonvar.h> 55 56 #ifndef _MODULE 57 #include "opt_modular.h" 58 #endif 59 60 struct swwdog_softc { 61 device_t sc_dev; 62 struct sysmon_wdog sc_smw; 63 struct callout sc_c; 64 int sc_armed; 65 }; 66 67 bool swwdog_reboot = false; /* false --> panic , true --> reboot */ 68 69 static struct workqueue *wq; 70 static device_t swwdog_dev; 71 72 MODULE(MODULE_CLASS_DRIVER, swwdog, "sysmon_wdog"); 73 74 #ifdef _MODULE 75 CFDRIVER_DECL(swwdog, DV_DULL, NULL); 76 #endif 77 78 int swwdogattach(int); 79 80 static int swwdog_setmode(struct sysmon_wdog *); 81 static int swwdog_tickle(struct sysmon_wdog *); 82 static bool swwdog_suspend(device_t, const pmf_qual_t *); 83 static int swwdog_arm(struct swwdog_softc *); 84 static int swwdog_disarm(struct swwdog_softc *); 85 86 static void swwdog_panic(void *); 87 88 static int swwdog_match(device_t, cfdata_t, void *); 89 static void swwdog_attach(device_t, device_t, void *); 90 static int swwdog_detach(device_t, int); 91 static bool swwdog_suspend(device_t, const pmf_qual_t *); 92 93 static int swwdog_init(void *); 94 static int swwdog_fini(void *); 95 static int swwdog_modcmd(modcmd_t, void *); 96 97 CFATTACH_DECL_NEW(swwdog, sizeof(struct swwdog_softc), 98 swwdog_match, swwdog_attach, swwdog_detach, NULL); 99 extern struct cfdriver swwdog_cd; 100 101 #define SWDOG_DEFAULT 60 /* 60-second default period */ 102 103 static void 104 doreboot(struct work *wrkwrkwrk, void *p) 105 { 106 107 kern_reboot(0, NULL); 108 } 109 110 int 111 swwdogattach(int n __unused) 112 { 113 int error; 114 static struct cfdata cf; 115 116 if (workqueue_create(&wq, "swwreboot", doreboot, NULL, 117 PRI_NONE, IPL_NONE, 0) != 0) { 118 aprint_error("failed to create swwdog reboot wq"); 119 return 1; 120 } 121 122 error = config_cfattach_attach(swwdog_cd.cd_name, &swwdog_ca); 123 if (error) { 124 aprint_error("%s: unable to attach cfattach\n", 125 swwdog_cd.cd_name); 126 workqueue_destroy(wq); 127 return error; 128 } 129 130 cf.cf_name = swwdog_cd.cd_name; 131 cf.cf_atname = swwdog_cd.cd_name; 132 cf.cf_unit = 0; 133 cf.cf_fstate = FSTATE_STAR; 134 cf.cf_pspec = NULL; 135 cf.cf_loc = NULL; 136 cf.cf_flags = 0; 137 138 swwdog_dev = config_attach_pseudo(&cf); 139 140 if (swwdog_dev == NULL) { 141 config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca); 142 workqueue_destroy(wq); 143 return 1; 144 } 145 return 0; 146 } 147 148 static int 149 swwdog_match(device_t parent, cfdata_t data, void *aux) 150 { 151 152 return 1; 153 } 154 155 static void 156 swwdog_attach(device_t parent, device_t self, void *aux) 157 { 158 struct swwdog_softc *sc = device_private(self); 159 160 sc->sc_dev = self; 161 sc->sc_smw.smw_name = device_xname(self); 162 sc->sc_smw.smw_cookie = sc; 163 sc->sc_smw.smw_setmode = swwdog_setmode; 164 sc->sc_smw.smw_tickle = swwdog_tickle; 165 sc->sc_smw.smw_period = SWDOG_DEFAULT; 166 167 callout_init(&sc->sc_c, 0); 168 callout_setfunc(&sc->sc_c, swwdog_panic, sc); 169 170 if (sysmon_wdog_register(&sc->sc_smw) == 0) 171 aprint_normal_dev(self, "software watchdog initialized\n"); 172 else { 173 aprint_error_dev(self, "unable to register software " 174 "watchdog with sysmon\n"); 175 callout_destroy(&sc->sc_c); 176 return; 177 } 178 179 if (!pmf_device_register(self, swwdog_suspend, NULL)) 180 aprint_error_dev(self, "couldn't establish power handler\n"); 181 } 182 183 static int 184 swwdog_detach(device_t self, int flags) 185 { 186 struct swwdog_softc *sc = device_private(self); 187 188 pmf_device_deregister(self); 189 swwdog_disarm(sc); 190 sysmon_wdog_unregister(&sc->sc_smw); 191 callout_destroy(&sc->sc_c); 192 workqueue_destroy(wq); 193 194 return 0; 195 } 196 197 static bool 198 swwdog_suspend(device_t dev, const pmf_qual_t *qual) 199 { 200 struct swwdog_softc *sc = device_private(dev); 201 202 /* Don't allow suspend if watchdog is armed */ 203 if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED) 204 return false; 205 return true; 206 } 207 208 static int 209 swwdog_setmode(struct sysmon_wdog *smw) 210 { 211 struct swwdog_softc *sc = smw->smw_cookie; 212 int error = 0; 213 214 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 215 error = swwdog_disarm(sc); 216 } else { 217 if (smw->smw_period == 0) 218 return EINVAL; 219 else if (smw->smw_period == WDOG_PERIOD_DEFAULT) 220 sc->sc_smw.smw_period = SWDOG_DEFAULT; 221 else 222 sc->sc_smw.smw_period = smw->smw_period; 223 error = swwdog_arm(sc); 224 } 225 return error; 226 } 227 228 static int 229 swwdog_tickle(struct sysmon_wdog *smw) 230 { 231 232 return swwdog_arm(smw->smw_cookie); 233 } 234 235 static int 236 swwdog_arm(struct swwdog_softc *sc) 237 { 238 239 callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz); 240 return 0; 241 } 242 243 static int 244 swwdog_disarm(struct swwdog_softc *sc) 245 { 246 247 callout_stop(&sc->sc_c); 248 return 0; 249 } 250 251 static void 252 swwdog_panic(void *vsc) 253 { 254 struct swwdog_softc *sc = vsc; 255 static struct work wk; /* we'll need it max once */ 256 bool do_panic; 257 258 do_panic = !swwdog_reboot; 259 swwdog_reboot = false; 260 callout_schedule(&sc->sc_c, 60 * hz); /* deliberate double-panic */ 261 262 printf("%s: %d second timer expired\n", "swwdog", 263 sc->sc_smw.smw_period); 264 265 if (do_panic) 266 panic("watchdog timer expired"); 267 else 268 workqueue_enqueue(wq, &wk, NULL); 269 } 270 271 SYSCTL_SETUP(swwdog_sysctl_setup, "swwdog sysctl") 272 { 273 const struct sysctlnode *me; 274 275 sysctl_createv(clog, 0, NULL, &me, CTLFLAG_READWRITE, 276 CTLTYPE_NODE, "swwdog", NULL, 277 NULL, 0, NULL, 0, 278 CTL_HW, CTL_CREATE, CTL_EOL); 279 sysctl_createv(clog, 0, NULL, NULL, CTLFLAG_READWRITE, 280 CTLTYPE_BOOL, "reboot", SYSCTL_DESCR("reboot if timer expires"), 281 NULL, 0, &swwdog_reboot, sizeof(bool), 282 CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL); 283 } 284 285 /* 286 * Module management 287 */ 288 289 static int 290 swwdog_init(void *arg) 291 { 292 /* 293 * Merge the driver info into the kernel tables and attach the 294 * pseudo-device 295 */ 296 int error = 0; 297 298 299 #ifdef _MODULE 300 error = config_cfdriver_attach(&swwdog_cd); 301 if (error) { 302 aprint_error("%s: unable to attach cfdriver\n", 303 swwdog_cd.cd_name); 304 return error; 305 } 306 error = swwdogattach(1); 307 if (error) { 308 aprint_error("%s: device attach failed\n", swwdog_cd.cd_name); 309 config_cfdriver_detach(&swwdog_cd); 310 } 311 #endif 312 313 return error; 314 } 315 316 static int 317 swwdog_fini(void *arg) 318 { 319 int error; 320 321 error = config_detach(swwdog_dev, 0); 322 323 #ifdef _MODULE 324 error = config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca); 325 if (error) 326 aprint_error("%s: error detaching cfattach: %d\n", 327 swwdog_cd.cd_name, error); 328 329 error = config_cfdriver_detach(&swwdog_cd); 330 if (error) 331 aprint_error("%s: error detaching cfdriver: %d\n", 332 swwdog_cd.cd_name, error); 333 #endif 334 335 return error; 336 } 337 338 static int 339 swwdog_modcmd(modcmd_t cmd, void *arg) 340 { 341 int ret; 342 343 switch (cmd) { 344 case MODULE_CMD_INIT: 345 ret = swwdog_init(arg); 346 break; 347 case MODULE_CMD_FINI: 348 ret = swwdog_fini(arg); 349 break; 350 case MODULE_CMD_STAT: 351 default: 352 ret = ENOTTY; 353 } 354 355 return ret; 356 } 357