sysmon_wdog.c revision 1.15 1 /* $NetBSD: sysmon_wdog.c,v 1.15 2007/02/18 23:36:39 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Zembu Labs, Inc.
5 * All rights reserved.
6 *
7 * Author: Jason R. Thorpe <thorpej (at) zembu.com>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Zembu Labs, Inc.
20 * 4. Neither the name of Zembu Labs nor the names of its employees may
21 * be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
25 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
26 * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
27 * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Watchdog timer framework for sysmon. Hardware (and software)
38 * watchdog timers can register themselves here to provide a
39 * watchdog function, which provides an abstract interface to the
40 * user.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: sysmon_wdog.c,v 1.15 2007/02/18 23:36:39 xtraeme Exp $");
45
46 #include <sys/param.h>
47 #include <sys/conf.h>
48 #include <sys/errno.h>
49 #include <sys/fcntl.h>
50 #include <sys/callout.h>
51 #include <sys/kernel.h>
52 #include <sys/mutex.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55
56 #include <dev/sysmon/sysmonvar.h>
57
58 LIST_HEAD(, sysmon_wdog) sysmon_wdog_list =
59 LIST_HEAD_INITIALIZER(&sysmon_wdog_list);
60 int sysmon_wdog_count;
61 kmutex_t sysmon_wdog_list_mtx;
62
63 kmutex_t sysmon_wdog_mtx;
64 struct sysmon_wdog *sysmon_armed_wdog;
65 struct callout sysmon_wdog_callout = CALLOUT_INITIALIZER;
66 void *sysmon_wdog_sdhook;
67
68 #define SYSMON_WDOG_LOCK() \
69 mutex_enter(&sysmon_wdog_mtx)
70
71 #define SYSMON_WDOG_UNLOCK() \
72 mutex_exit(&sysmon_wdog_mtx)
73
74 struct sysmon_wdog *sysmon_wdog_find(const char *);
75 void sysmon_wdog_release(struct sysmon_wdog *);
76 int sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);
77 void sysmon_wdog_ktickle(void *);
78 void sysmon_wdog_shutdown(void *);
79
80 /*
81 * sysmonopen_wdog:
82 *
83 * Open the system monitor device.
84 */
85 int
86 sysmonopen_wdog(dev_t dev, int flag, int mode,
87 struct lwp *l)
88 {
89
90 mutex_enter(&sysmon_wdog_list_mtx);
91 if (sysmon_wdog_sdhook == NULL) {
92 sysmon_wdog_sdhook =
93 shutdownhook_establish(sysmon_wdog_shutdown, NULL);
94 if (sysmon_wdog_sdhook == NULL)
95 printf("WARNING: unable to register watchdog "
96 "shutdown hook\n");
97 }
98 mutex_exit(&sysmon_wdog_list_mtx);
99
100 return (0);
101 }
102
103 /*
104 * sysmonclose_wdog:
105 *
106 * Close the system monitor device.
107 */
108 int
109 sysmonclose_wdog(dev_t dev, int flag, int mode,
110 struct lwp *l)
111 {
112 struct sysmon_wdog *smw;
113 int error = 0;
114
115 /*
116 * If this is the last close, and there is a watchdog
117 * running in UTICKLE mode, we need to disable it,
118 * otherwise the system will reset in short order.
119 *
120 * XXX Maybe we should just go into KTICKLE mode?
121 */
122 SYSMON_WDOG_LOCK();
123 if ((smw = sysmon_armed_wdog) != NULL) {
124 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_UTICKLE) {
125 error = sysmon_wdog_setmode(smw,
126 WDOG_MODE_DISARMED, smw->smw_period);
127 if (error) {
128 printf("WARNING: UNABLE TO DISARM "
129 "WATCHDOG %s ON CLOSE!\n",
130 smw->smw_name);
131 /*
132 * ...we will probably reboot soon.
133 */
134 }
135 }
136 }
137 SYSMON_WDOG_UNLOCK();
138
139 return (error);
140 }
141
142 /*
143 * sysmonioctl_wdog:
144 *
145 * Perform a watchdog control request.
146 */
147 int
148 sysmonioctl_wdog(dev_t dev, u_long cmd, caddr_t data, int flag,
149 struct lwp *l)
150 {
151 struct sysmon_wdog *smw;
152 int error = 0;
153
154 switch (cmd) {
155 case WDOGIOC_GMODE:
156 {
157 struct wdog_mode *wm = (void *) data;
158
159 wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
160 smw = sysmon_wdog_find(wm->wm_name);
161 if (smw == NULL) {
162 error = ESRCH;
163 break;
164 }
165
166 wm->wm_mode = smw->smw_mode;
167 wm->wm_period = smw->smw_period;
168 sysmon_wdog_release(smw);
169 break;
170 }
171
172 case WDOGIOC_SMODE:
173 {
174 struct wdog_mode *wm = (void *) data;
175
176 if ((flag & FWRITE) == 0) {
177 error = EPERM;
178 break;
179 }
180
181 wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
182 smw = sysmon_wdog_find(wm->wm_name);
183 if (smw == NULL) {
184 error = ESRCH;
185 break;
186 }
187
188 if (wm->wm_mode & ~(WDOG_MODE_MASK|WDOG_FEATURE_MASK))
189 error = EINVAL;
190 else {
191 SYSMON_WDOG_LOCK();
192 error = sysmon_wdog_setmode(smw, wm->wm_mode,
193 wm->wm_period);
194 SYSMON_WDOG_UNLOCK();
195 }
196
197 sysmon_wdog_release(smw);
198 break;
199 }
200
201 case WDOGIOC_WHICH:
202 {
203 struct wdog_mode *wm = (void *) data;
204
205 SYSMON_WDOG_LOCK();
206 if ((smw = sysmon_armed_wdog) != NULL) {
207 strcpy(wm->wm_name, smw->smw_name);
208 wm->wm_mode = smw->smw_mode;
209 wm->wm_period = smw->smw_period;
210 } else
211 error = ESRCH;
212 SYSMON_WDOG_UNLOCK();
213 break;
214 }
215
216 case WDOGIOC_TICKLE:
217 if ((flag & FWRITE) == 0) {
218 error = EPERM;
219 break;
220 }
221
222 SYSMON_WDOG_LOCK();
223 if ((smw = sysmon_armed_wdog) != NULL) {
224 error = (*smw->smw_tickle)(smw);
225 if (error == 0)
226 smw->smw_tickler = l->l_proc->p_pid;
227 } else
228 error = ESRCH;
229 SYSMON_WDOG_UNLOCK();
230 break;
231
232 case WDOGIOC_GTICKLER:
233 if ((smw = sysmon_armed_wdog) != NULL)
234 *(pid_t *)data = smw->smw_tickler;
235 else
236 error = ESRCH;
237 break;
238
239 case WDOGIOC_GWDOGS:
240 {
241 struct wdog_conf *wc = (void *) data;
242 char *cp;
243 int i;
244
245 mutex_enter(&sysmon_wdog_list_mtx);
246 if (wc->wc_names == NULL)
247 wc->wc_count = sysmon_wdog_count;
248 else {
249 for (i = 0, cp = wc->wc_names,
250 smw = LIST_FIRST(&sysmon_wdog_list);
251 i < sysmon_wdog_count && smw != NULL && error == 0;
252 i++, cp += WDOG_NAMESIZE,
253 smw = LIST_NEXT(smw, smw_list))
254 error = copyout(smw->smw_name, cp,
255 strlen(smw->smw_name) + 1);
256 wc->wc_count = i;
257 }
258 mutex_exit(&sysmon_wdog_list_mtx);
259 break;
260 }
261
262 default:
263 error = ENOTTY;
264 }
265
266 return (error);
267 }
268
269 /*
270 * sysmon_wdog_register:
271 *
272 * Register a watchdog device.
273 */
274 int
275 sysmon_wdog_register(struct sysmon_wdog *smw)
276 {
277 struct sysmon_wdog *lsmw;
278 int error = 0;
279
280 mutex_init(&sysmon_wdog_mtx, MUTEX_DRIVER, IPL_NONE);
281 mutex_init(&sysmon_wdog_list_mtx, MUTEX_DRIVER, IPL_NONE);
282 mutex_enter(&sysmon_wdog_list_mtx);
283
284 for (lsmw = LIST_FIRST(&sysmon_wdog_list); lsmw != NULL;
285 lsmw = LIST_NEXT(lsmw, smw_list)) {
286 if (strcmp(lsmw->smw_name, smw->smw_name) == 0) {
287 error = EEXIST;
288 goto out;
289 }
290 }
291
292 smw->smw_mode = WDOG_MODE_DISARMED;
293 smw->smw_tickler = (pid_t) -1;
294 smw->smw_refcnt = 0;
295 sysmon_wdog_count++;
296 LIST_INSERT_HEAD(&sysmon_wdog_list, smw, smw_list);
297
298 out:
299 mutex_exit(&sysmon_wdog_list_mtx);
300 return (error);
301 }
302
303 /*
304 * sysmon_wdog_unregister:
305 *
306 * Unregister a watchdog device.
307 */
308 void
309 sysmon_wdog_unregister(struct sysmon_wdog *smw)
310 {
311
312 mutex_enter(&sysmon_wdog_list_mtx);
313 sysmon_wdog_count--;
314 LIST_REMOVE(smw, smw_list);
315 mutex_exit(&sysmon_wdog_list_mtx);
316 }
317
318 /*
319 * sysmon_wdog_find:
320 *
321 * Find a watchdog device. We increase the reference
322 * count on a match.
323 */
324 struct sysmon_wdog *
325 sysmon_wdog_find(const char *name)
326 {
327 struct sysmon_wdog *smw;
328
329 mutex_enter(&sysmon_wdog_list_mtx);
330
331 for (smw = LIST_FIRST(&sysmon_wdog_list); smw != NULL;
332 smw = LIST_NEXT(smw, smw_list)) {
333 if (strcmp(smw->smw_name, name) == 0)
334 break;
335 }
336
337 if (smw != NULL)
338 smw->smw_refcnt++;
339
340 mutex_exit(&sysmon_wdog_list_mtx);
341 return (smw);
342 }
343
344 /*
345 * sysmon_wdog_release:
346 *
347 * Release a watchdog device.
348 */
349 void
350 sysmon_wdog_release(struct sysmon_wdog *smw)
351 {
352
353 mutex_enter(&sysmon_wdog_list_mtx);
354 KASSERT(smw->smw_refcnt != 0);
355 smw->smw_refcnt--;
356 mutex_exit(&sysmon_wdog_list_mtx);
357 }
358
359 /*
360 * sysmon_wdog_setmode:
361 *
362 * Set the mode of a watchdog device.
363 */
364 int
365 sysmon_wdog_setmode(struct sysmon_wdog *smw, int mode, u_int period)
366 {
367 u_int operiod = smw->smw_period;
368 int omode = smw->smw_mode;
369 int error = 0;
370
371 smw->smw_period = period;
372 smw->smw_mode = mode;
373
374 switch (mode & WDOG_MODE_MASK) {
375 case WDOG_MODE_DISARMED:
376 if (smw != sysmon_armed_wdog) {
377 error = EINVAL;
378 goto out;
379 }
380 break;
381
382 case WDOG_MODE_KTICKLE:
383 case WDOG_MODE_UTICKLE:
384 case WDOG_MODE_ETICKLE:
385 if (sysmon_armed_wdog != NULL) {
386 error = EBUSY;
387 goto out;
388 }
389 break;
390
391 default:
392 error = EINVAL;
393 goto out;
394 }
395
396 error = (*smw->smw_setmode)(smw);
397
398 out:
399 if (error) {
400 smw->smw_period = operiod;
401 smw->smw_mode = omode;
402 } else {
403 if ((mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
404 sysmon_armed_wdog = NULL;
405 smw->smw_tickler = (pid_t) -1;
406 smw->smw_refcnt--;
407 if ((omode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE)
408 callout_stop(&sysmon_wdog_callout);
409 } else {
410 sysmon_armed_wdog = smw;
411 smw->smw_refcnt++;
412 if ((mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
413 callout_reset(&sysmon_wdog_callout,
414 WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
415 sysmon_wdog_ktickle, NULL);
416 }
417 }
418 }
419 return (error);
420 }
421
422 /*
423 * sysmon_wdog_ktickle:
424 *
425 * Kernel watchdog tickle routine.
426 */
427 void
428 sysmon_wdog_ktickle(void *arg)
429 {
430 struct sysmon_wdog *smw;
431
432 SYSMON_WDOG_LOCK();
433 if ((smw = sysmon_armed_wdog) != NULL) {
434 if ((*smw->smw_tickle)(smw) != 0) {
435 printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
436 "FAILED!\n", smw->smw_name);
437 /*
438 * ...we will probably reboot soon.
439 */
440 }
441 callout_reset(&sysmon_wdog_callout,
442 WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
443 sysmon_wdog_ktickle, NULL);
444 }
445 SYSMON_WDOG_UNLOCK();
446 }
447
448 /*
449 * sysmon_wdog_shutdown:
450 *
451 * Perform shutdown-time operations.
452 */
453 void
454 sysmon_wdog_shutdown(void *arg)
455 {
456 struct sysmon_wdog *smw;
457
458 /*
459 * XXX Locking here? I don't think it's necessary.
460 */
461
462 if ((smw = sysmon_armed_wdog) != NULL) {
463 if (sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED,
464 smw->smw_period))
465 printf("WARNING: FAILED TO SHUTDOWN WATCHDOG %s!\n",
466 smw->smw_name);
467 }
468 }
469