1 /* $NetBSD: sysmon_wdog.c,v 1.23 2007/12/15 04:58:39 dyoung Exp $ */
4 * Copyright (c) 2000 Zembu Labs, Inc.
7 * Author: Jason R. Thorpe <thorpej@zembu.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
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
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: sysmon_wdog.c,v 1.23 2007/12/15 04:58:39 dyoung Exp $");
46 #include <sys/param.h>
48 #include <sys/errno.h>
49 #include <sys/fcntl.h>
50 #include <sys/condvar.h>
51 #include <sys/mutex.h>
52 #include <sys/callout.h>
53 #include <sys/kernel.h>
54 #include <sys/systm.h>
57 #include <dev/sysmon/sysmonvar.h>
59 static LIST_HEAD(, sysmon_wdog
) sysmon_wdog_list
=
60 LIST_HEAD_INITIALIZER(&sysmon_wdog_list
);
61 static int sysmon_wdog_count
;
62 static kmutex_t sysmon_wdog_list_mtx
, sysmon_wdog_mtx
;
63 static kcondvar_t sysmon_wdog_cv
;
64 static struct sysmon_wdog
*sysmon_armed_wdog
;
65 static callout_t sysmon_wdog_callout
;
66 static void *sysmon_wdog_sdhook
;
68 struct sysmon_wdog
*sysmon_wdog_find(const char *);
69 void sysmon_wdog_release(struct sysmon_wdog
*);
70 int sysmon_wdog_setmode(struct sysmon_wdog
*, int, u_int
);
71 void sysmon_wdog_ktickle(void *);
72 void sysmon_wdog_shutdown(void *);
73 void sysmon_wdog_ref(struct sysmon_wdog
*);
76 sysmon_wdog_init(void)
78 mutex_init(&sysmon_wdog_list_mtx
, MUTEX_DEFAULT
, IPL_NONE
);
79 mutex_init(&sysmon_wdog_mtx
, MUTEX_DEFAULT
, IPL_SOFTCLOCK
);
80 cv_init(&sysmon_wdog_cv
, "wdogref");
86 * Open the system monitor device.
89 sysmonopen_wdog(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
92 mutex_enter(&sysmon_wdog_list_mtx
);
93 if (sysmon_wdog_sdhook
== NULL
) {
95 shutdownhook_establish(sysmon_wdog_shutdown
, NULL
);
96 if (sysmon_wdog_sdhook
== NULL
)
97 printf("WARNING: unable to register watchdog "
99 callout_init(&sysmon_wdog_callout
, 0);
101 mutex_exit(&sysmon_wdog_list_mtx
);
109 * Close the system monitor device.
112 sysmonclose_wdog(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
114 struct sysmon_wdog
*smw
;
118 * If this is the last close, and there is a watchdog
119 * running in UTICKLE mode, we need to disable it,
120 * otherwise the system will reset in short order.
122 * XXX Maybe we should just go into KTICKLE mode?
124 mutex_enter(&sysmon_wdog_mtx
);
125 if ((smw
= sysmon_armed_wdog
) != NULL
) {
126 if ((smw
->smw_mode
& WDOG_MODE_MASK
) == WDOG_MODE_UTICKLE
) {
127 error
= sysmon_wdog_setmode(smw
,
128 WDOG_MODE_DISARMED
, smw
->smw_period
);
130 printf("WARNING: UNABLE TO DISARM "
131 "WATCHDOG %s ON CLOSE!\n",
134 * ...we will probably reboot soon.
139 mutex_exit(&sysmon_wdog_mtx
);
147 * Perform a watchdog control request.
150 sysmonioctl_wdog(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
152 struct sysmon_wdog
*smw
;
158 struct wdog_mode
*wm
= (void *) data
;
160 wm
->wm_name
[sizeof(wm
->wm_name
) - 1] = '\0';
161 smw
= sysmon_wdog_find(wm
->wm_name
);
167 wm
->wm_mode
= smw
->smw_mode
;
168 wm
->wm_period
= smw
->smw_period
;
169 sysmon_wdog_release(smw
);
175 struct wdog_mode
*wm
= (void *) data
;
177 if ((flag
& FWRITE
) == 0) {
182 wm
->wm_name
[sizeof(wm
->wm_name
) - 1] = '\0';
183 smw
= sysmon_wdog_find(wm
->wm_name
);
189 if (wm
->wm_mode
& ~(WDOG_MODE_MASK
|WDOG_FEATURE_MASK
))
192 mutex_enter(&sysmon_wdog_mtx
);
193 error
= sysmon_wdog_setmode(smw
, wm
->wm_mode
,
195 mutex_exit(&sysmon_wdog_mtx
);
198 sysmon_wdog_release(smw
);
204 struct wdog_mode
*wm
= (void *) data
;
206 mutex_enter(&sysmon_wdog_mtx
);
207 if ((smw
= sysmon_armed_wdog
) != NULL
) {
208 strcpy(wm
->wm_name
, smw
->smw_name
);
209 wm
->wm_mode
= smw
->smw_mode
;
210 wm
->wm_period
= smw
->smw_period
;
213 mutex_exit(&sysmon_wdog_mtx
);
218 if ((flag
& FWRITE
) == 0) {
223 mutex_enter(&sysmon_wdog_mtx
);
224 if ((smw
= sysmon_armed_wdog
) != NULL
) {
225 error
= (*smw
->smw_tickle
)(smw
);
227 smw
->smw_tickler
= l
->l_proc
->p_pid
;
230 mutex_exit(&sysmon_wdog_mtx
);
233 case WDOGIOC_GTICKLER
:
234 if ((smw
= sysmon_armed_wdog
) != NULL
)
235 *(pid_t
*)data
= smw
->smw_tickler
;
242 struct wdog_conf
*wc
= (void *) data
;
246 mutex_enter(&sysmon_wdog_list_mtx
);
247 if (wc
->wc_names
== NULL
)
248 wc
->wc_count
= sysmon_wdog_count
;
250 for (i
= 0, cp
= wc
->wc_names
,
251 smw
= LIST_FIRST(&sysmon_wdog_list
);
252 i
< sysmon_wdog_count
&& smw
!= NULL
&& error
== 0;
253 i
++, cp
+= WDOG_NAMESIZE
,
254 smw
= LIST_NEXT(smw
, smw_list
))
255 error
= copyout(smw
->smw_name
, cp
,
256 strlen(smw
->smw_name
) + 1);
259 mutex_exit(&sysmon_wdog_list_mtx
);
271 * sysmon_wdog_register:
273 * Register a watchdog device.
276 sysmon_wdog_register(struct sysmon_wdog
*smw
)
278 struct sysmon_wdog
*lsmw
;
281 mutex_enter(&sysmon_wdog_list_mtx
);
283 LIST_FOREACH(lsmw
, &sysmon_wdog_list
, smw_list
) {
284 if (strcmp(lsmw
->smw_name
, smw
->smw_name
) == 0) {
290 smw
->smw_mode
= WDOG_MODE_DISARMED
;
291 smw
->smw_tickler
= (pid_t
) -1;
294 LIST_INSERT_HEAD(&sysmon_wdog_list
, smw
, smw_list
);
297 mutex_exit(&sysmon_wdog_list_mtx
);
302 * sysmon_wdog_unregister:
304 * Unregister a watchdog device.
307 sysmon_wdog_unregister(struct sysmon_wdog
*smw
)
311 mutex_enter(&sysmon_wdog_list_mtx
);
312 while (smw
->smw_refcnt
> 0 && rc
== 0) {
313 aprint_debug("%s: %d users remain\n", smw
->smw_name
,
315 rc
= cv_wait_sig(&sysmon_wdog_cv
, &sysmon_wdog_list_mtx
);
319 LIST_REMOVE(smw
, smw_list
);
321 mutex_exit(&sysmon_wdog_list_mtx
);
328 * Find a watchdog device. We increase the reference
332 sysmon_wdog_find(const char *name
)
334 struct sysmon_wdog
*smw
;
336 mutex_enter(&sysmon_wdog_list_mtx
);
338 LIST_FOREACH(smw
, &sysmon_wdog_list
, smw_list
) {
339 if (strcmp(smw
->smw_name
, name
) == 0)
346 mutex_exit(&sysmon_wdog_list_mtx
);
351 * sysmon_wdog_release:
353 * Release a watchdog device.
356 sysmon_wdog_release(struct sysmon_wdog
*smw
)
359 mutex_enter(&sysmon_wdog_list_mtx
);
360 KASSERT(smw
->smw_refcnt
!= 0);
362 cv_signal(&sysmon_wdog_cv
);
363 mutex_exit(&sysmon_wdog_list_mtx
);
367 sysmon_wdog_ref(struct sysmon_wdog
*smw
)
369 mutex_enter(&sysmon_wdog_list_mtx
);
371 mutex_exit(&sysmon_wdog_list_mtx
);
375 * sysmon_wdog_setmode:
377 * Set the mode of a watchdog device.
380 sysmon_wdog_setmode(struct sysmon_wdog
*smw
, int mode
, u_int period
)
382 u_int operiod
= smw
->smw_period
;
383 int omode
= smw
->smw_mode
;
386 smw
->smw_period
= period
;
387 smw
->smw_mode
= mode
;
389 switch (mode
& WDOG_MODE_MASK
) {
390 case WDOG_MODE_DISARMED
:
391 if (smw
!= sysmon_armed_wdog
) {
397 case WDOG_MODE_KTICKLE
:
398 case WDOG_MODE_UTICKLE
:
399 case WDOG_MODE_ETICKLE
:
400 if (sysmon_armed_wdog
!= NULL
) {
411 error
= (*smw
->smw_setmode
)(smw
);
415 smw
->smw_period
= operiod
;
416 smw
->smw_mode
= omode
;
418 if ((mode
& WDOG_MODE_MASK
) == WDOG_MODE_DISARMED
) {
419 sysmon_armed_wdog
= NULL
;
420 smw
->smw_tickler
= (pid_t
) -1;
421 sysmon_wdog_release(smw
);
422 if ((omode
& WDOG_MODE_MASK
) == WDOG_MODE_KTICKLE
)
423 callout_stop(&sysmon_wdog_callout
);
425 sysmon_armed_wdog
= smw
;
426 sysmon_wdog_ref(smw
);
427 if ((mode
& WDOG_MODE_MASK
) == WDOG_MODE_KTICKLE
) {
428 callout_reset(&sysmon_wdog_callout
,
429 WDOG_PERIOD_TO_TICKS(smw
->smw_period
) / 2,
430 sysmon_wdog_ktickle
, NULL
);
438 * sysmon_wdog_ktickle:
440 * Kernel watchdog tickle routine.
443 sysmon_wdog_ktickle(void *arg
)
445 struct sysmon_wdog
*smw
;
447 mutex_enter(&sysmon_wdog_mtx
);
448 if ((smw
= sysmon_armed_wdog
) != NULL
) {
449 if ((*smw
->smw_tickle
)(smw
) != 0) {
450 printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
451 "FAILED!\n", smw
->smw_name
);
453 * ...we will probably reboot soon.
456 callout_reset(&sysmon_wdog_callout
,
457 WDOG_PERIOD_TO_TICKS(smw
->smw_period
) / 2,
458 sysmon_wdog_ktickle
, NULL
);
460 mutex_exit(&sysmon_wdog_mtx
);
464 * sysmon_wdog_shutdown:
466 * Perform shutdown-time operations.
469 sysmon_wdog_shutdown(void *arg
)
471 struct sysmon_wdog
*smw
;
474 * XXX Locking here? I don't think it's necessary.
477 if ((smw
= sysmon_armed_wdog
) != NULL
) {
478 if (sysmon_wdog_setmode(smw
, WDOG_MODE_DISARMED
,
480 printf("WARNING: FAILED TO SHUTDOWN WATCHDOG %s!\n",