sys/kern/kern_wdog.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | /* * Copyright (c) 2009 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Alex Hornung <ahornung@gmail.com> * * 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. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 * COPYRIGHT HOLDERS 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/param.h> #include <sys/kernel.h> #include <sys/systm.h> #include <sys/malloc.h> #include <sys/sysmsg.h> #include <sys/spinlock2.h> #include <sys/conf.h> #include <sys/device.h> #include <sys/filedesc.h> #include <sys/sysctl.h> #include <sys/unistd.h> #include <sys/event.h> #include <sys/queue.h> #include <sys/wdog.h> #include <machine/limits.h> static LIST_HEAD(, watchdog) wdoglist = LIST_HEAD_INITIALIZER(&wdoglist); static struct spinlock wdogmtx; static struct callout wdog_callout; static int wdog_auto_enable = 1; static int wdog_auto_period = WDOG_DEFAULT_PERIOD; static void wdog_reset_all(void *unused); void wdog_register(struct watchdog *wd) { spin_lock(&wdogmtx); wd->period = WDOG_DEFAULT_PERIOD; LIST_INSERT_HEAD(&wdoglist, wd, link); spin_unlock(&wdogmtx); wdog_reset_all(NULL); kprintf("wdog: Watchdog %s registered, max period = %ds , period = %ds\n", wd->name, wd->period_max, wd->period); } void wdog_unregister(struct watchdog *wd) { spin_lock(&wdogmtx); LIST_REMOVE(wd, link); spin_unlock(&wdogmtx); kprintf("wdog: Watchdog %s unregistered\n", wd->name); } static int wdog_reset(struct watchdog *wd) { return (wd->period = wd->wdog_fn(wd->arg, wd->period)); } static void wdog_reset_all(void *unused) { struct watchdog *wd; int period, min_period = INT_MAX; spin_lock(&wdogmtx); if (LIST_EMPTY(&wdoglist)) goto done; LIST_FOREACH(wd, &wdoglist, link) { period = wdog_reset(wd); if (period < min_period) min_period = period; } if (wdog_auto_enable) { callout_reset(&wdog_callout, min_period * hz / 2, wdog_reset_all, NULL); } wdog_auto_period = min_period; done: spin_unlock(&wdogmtx); } static void wdog_set_period(int period) { struct watchdog *wd; spin_lock(&wdogmtx); LIST_FOREACH(wd, &wdoglist, link) { /* XXX: check for period_max */ wd->period = period; } spin_unlock(&wdogmtx); } static int wdog_sysctl_auto(SYSCTL_HANDLER_ARGS) { int error; error = sysctl_handle_int(oidp, &wdog_auto_enable, 1, req); if (error || req->newptr == NULL) return error; /* has changed, do something */ callout_stop(&wdog_callout); if (wdog_auto_enable) { wdog_reset_all(NULL); } kprintf("wdog: In-kernel automatic watchdog reset %s\n", (wdog_auto_enable)?"enabled":"disabled"); return 0; } static int wdog_sysctl_period(SYSCTL_HANDLER_ARGS) { int error; error = sysctl_handle_int(oidp, &wdog_auto_period, WDOG_DEFAULT_PERIOD, req); if (error || req->newptr == NULL) return error; /* has changed, do something */ callout_stop(&wdog_callout); wdog_set_period(wdog_auto_period); wdog_reset_all(NULL); if (wdog_auto_period != 0) kprintf("wdog: Watchdog period set to %ds\n", wdog_auto_period); else kprintf("wdog: Disabled watchdog(s)\n"); return 0; } void wdog_disable(void) { callout_stop(&wdog_callout); wdog_set_period(0); wdog_reset_all(NULL); } static SYSCTL_NODE(_kern, OID_AUTO, watchdog, CTLFLAG_RW, 0, "watchdog"); SYSCTL_PROC(_kern_watchdog, OID_AUTO, auto, CTLTYPE_INT | CTLFLAG_RW, NULL, 0, wdog_sysctl_auto, "I", "auto in-kernel watchdog reset " "(0 = disabled, 1 = enabled)"); SYSCTL_PROC(_kern_watchdog, OID_AUTO, period, CTLTYPE_INT | CTLFLAG_RW, NULL, 0, wdog_sysctl_period, "I", "watchdog period " "(value in seconds)"); static int wdog_ioctl(struct dev_ioctl_args *ap) { if (wdog_auto_enable) return EINVAL; if (ap->a_cmd == WDIOCRESET) { wdog_reset_all(NULL); } else { return EINVAL; } return 0; } static struct dev_ops wdog_ops = { { "wdog", 0, 0 }, .d_ioctl = wdog_ioctl, }; static void wdog_init(void) { spin_init(&wdogmtx, "wdog"); make_dev(&wdog_ops, 0, UID_ROOT, GID_WHEEL, 0600, "wdog"); callout_init_mp(&wdog_callout); kprintf("wdog: In-kernel automatic watchdog reset %s\n", (wdog_auto_enable)?"enabled":"disabled"); } static void wdog_uninit(void) { callout_cancel(&wdog_callout); callout_terminate(&wdog_callout); dev_ops_remove_all(&wdog_ops); spin_uninit(&wdogmtx); } SYSINIT(wdog_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, wdog_init, NULL); SYSUNINIT(wdog_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, wdog_uninit, NULL); |