xref: /netbsd-src/sys/dev/sysmon/sysmon_wdog.c (revision 0df165c04d0a9ca1adde9ed2b890344c937954a6)
1 /*	$NetBSD: sysmon_wdog.c,v 1.21 2007/09/02 00:44:07 xtraeme Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 Zembu Labs, Inc.
5  * All rights reserved.
6  *
7  * Author: Jason R. Thorpe <thorpej@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.21 2007/09/02 00:44:07 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/mutex.h>
51 #include <sys/callout.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55 
56 #include <dev/sysmon/sysmonvar.h>
57 
58 static LIST_HEAD(, sysmon_wdog) sysmon_wdog_list =
59     LIST_HEAD_INITIALIZER(&sysmon_wdog_list);
60 static int sysmon_wdog_count;
61 static kmutex_t sysmon_wdog_list_mtx, sysmon_wdog_mtx;
62 static struct sysmon_wdog *sysmon_armed_wdog;
63 static callout_t sysmon_wdog_callout;
64 static void *sysmon_wdog_sdhook;
65 
66 struct sysmon_wdog *sysmon_wdog_find(const char *);
67 void	sysmon_wdog_release(struct sysmon_wdog *);
68 int	sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);
69 void	sysmon_wdog_ktickle(void *);
70 void	sysmon_wdog_shutdown(void *);
71 
72 void
73 sysmon_wdog_init(void)
74 {
75 	mutex_init(&sysmon_wdog_list_mtx, MUTEX_DEFAULT, IPL_NONE);
76 	mutex_init(&sysmon_wdog_mtx, MUTEX_SPIN, IPL_SOFTCLOCK);
77 }
78 
79 /*
80  * sysmonopen_wdog:
81  *
82  *	Open the system monitor device.
83  */
84 int
85 sysmonopen_wdog(dev_t dev, int flag, int mode, struct lwp *l)
86 {
87 
88 	mutex_enter(&sysmon_wdog_list_mtx);
89 	if (sysmon_wdog_sdhook == NULL) {
90 		sysmon_wdog_sdhook =
91 		    shutdownhook_establish(sysmon_wdog_shutdown, NULL);
92 		if (sysmon_wdog_sdhook == NULL)
93 			printf("WARNING: unable to register watchdog "
94 			    "shutdown hook\n");
95 		callout_init(&sysmon_wdog_callout, 0);
96 	}
97 	mutex_exit(&sysmon_wdog_list_mtx);
98 
99 	return 0;
100 }
101 
102 /*
103  * sysmonclose_wdog:
104  *
105  *	Close the system monitor device.
106  */
107 int
108 sysmonclose_wdog(dev_t dev, int flag, int mode, struct lwp *l)
109 {
110 	struct sysmon_wdog *smw;
111 	int error = 0;
112 
113 	/*
114 	 * If this is the last close, and there is a watchdog
115 	 * running in UTICKLE mode, we need to disable it,
116 	 * otherwise the system will reset in short order.
117 	 *
118 	 * XXX Maybe we should just go into KTICKLE mode?
119 	 */
120 	mutex_enter(&sysmon_wdog_mtx);
121 	if ((smw = sysmon_armed_wdog) != NULL) {
122 		if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_UTICKLE) {
123 			error = sysmon_wdog_setmode(smw,
124 			    WDOG_MODE_DISARMED, smw->smw_period);
125 			if (error) {
126 				printf("WARNING: UNABLE TO DISARM "
127 				    "WATCHDOG %s ON CLOSE!\n",
128 				    smw->smw_name);
129 				/*
130 				 * ...we will probably reboot soon.
131 				 */
132 			}
133 		}
134 	}
135 	mutex_exit(&sysmon_wdog_mtx);
136 
137 	return error;
138 }
139 
140 /*
141  * sysmonioctl_wdog:
142  *
143  *	Perform a watchdog control request.
144  */
145 int
146 sysmonioctl_wdog(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
147 {
148 	struct sysmon_wdog *smw;
149 	int error = 0;
150 
151 	switch (cmd) {
152 	case WDOGIOC_GMODE:
153 	    {
154 		struct wdog_mode *wm = (void *) data;
155 
156 		wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
157 		smw = sysmon_wdog_find(wm->wm_name);
158 		if (smw == NULL) {
159 			error = ESRCH;
160 			break;
161 		}
162 
163 		wm->wm_mode = smw->smw_mode;
164 		wm->wm_period = smw->smw_period;
165 		sysmon_wdog_release(smw);
166 		break;
167 	    }
168 
169 	case WDOGIOC_SMODE:
170 	    {
171 		struct wdog_mode *wm = (void *) data;
172 
173 		if ((flag & FWRITE) == 0) {
174 			error = EPERM;
175 			break;
176 		}
177 
178 		wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
179 		smw = sysmon_wdog_find(wm->wm_name);
180 		if (smw == NULL) {
181 			error = ESRCH;
182 			break;
183 		}
184 
185 		if (wm->wm_mode & ~(WDOG_MODE_MASK|WDOG_FEATURE_MASK))
186 			error = EINVAL;
187 		else {
188 			mutex_enter(&sysmon_wdog_mtx);
189 			error = sysmon_wdog_setmode(smw, wm->wm_mode,
190 			    wm->wm_period);
191 			mutex_exit(&sysmon_wdog_mtx);
192 		}
193 
194 		sysmon_wdog_release(smw);
195 		break;
196 	    }
197 
198 	case WDOGIOC_WHICH:
199 	    {
200 		struct wdog_mode *wm = (void *) data;
201 
202 		mutex_enter(&sysmon_wdog_mtx);
203 		if ((smw = sysmon_armed_wdog) != NULL) {
204 			strcpy(wm->wm_name, smw->smw_name);
205 			wm->wm_mode = smw->smw_mode;
206 			wm->wm_period = smw->smw_period;
207 		} else
208 			error = ESRCH;
209 		mutex_exit(&sysmon_wdog_mtx);
210 		break;
211 	    }
212 
213 	case WDOGIOC_TICKLE:
214 		if ((flag & FWRITE) == 0) {
215 			error = EPERM;
216 			break;
217 		}
218 
219 		mutex_enter(&sysmon_wdog_mtx);
220 		if ((smw = sysmon_armed_wdog) != NULL) {
221 			error = (*smw->smw_tickle)(smw);
222 			if (error == 0)
223 				smw->smw_tickler = l->l_proc->p_pid;
224 		} else
225 			error = ESRCH;
226 		mutex_exit(&sysmon_wdog_mtx);
227 		break;
228 
229 	case WDOGIOC_GTICKLER:
230 		if ((smw = sysmon_armed_wdog) != NULL)
231 			*(pid_t *)data = smw->smw_tickler;
232 		else
233 			error = ESRCH;
234 		break;
235 
236 	case WDOGIOC_GWDOGS:
237 	    {
238 		struct wdog_conf *wc = (void *) data;
239 		char *cp;
240 		int i;
241 
242 		mutex_enter(&sysmon_wdog_list_mtx);
243 		if (wc->wc_names == NULL)
244 			wc->wc_count = sysmon_wdog_count;
245 		else {
246 			for (i = 0, cp = wc->wc_names,
247 			       smw = LIST_FIRST(&sysmon_wdog_list);
248 			     i < sysmon_wdog_count && smw != NULL && error == 0;
249 			     i++, cp += WDOG_NAMESIZE,
250 			       smw = LIST_NEXT(smw, smw_list))
251 				error = copyout(smw->smw_name, cp,
252 				    strlen(smw->smw_name) + 1);
253 			wc->wc_count = i;
254 		}
255 		mutex_exit(&sysmon_wdog_list_mtx);
256 		break;
257 	    }
258 
259 	default:
260 		error = ENOTTY;
261 	}
262 
263 	return error;
264 }
265 
266 /*
267  * sysmon_wdog_register:
268  *
269  *	Register a watchdog device.
270  */
271 int
272 sysmon_wdog_register(struct sysmon_wdog *smw)
273 {
274 	struct sysmon_wdog *lsmw;
275 	int error = 0;
276 
277 	mutex_enter(&sysmon_wdog_list_mtx);
278 
279 	for (lsmw = LIST_FIRST(&sysmon_wdog_list); lsmw != NULL;
280 	     lsmw = LIST_NEXT(lsmw, smw_list)) {
281 		if (strcmp(lsmw->smw_name, smw->smw_name) == 0) {
282 			error = EEXIST;
283 			goto out;
284 		}
285 	}
286 
287 	smw->smw_mode = WDOG_MODE_DISARMED;
288 	smw->smw_tickler = (pid_t) -1;
289 	smw->smw_refcnt = 0;
290 	sysmon_wdog_count++;
291 	LIST_INSERT_HEAD(&sysmon_wdog_list, smw, smw_list);
292 
293  out:
294 	mutex_exit(&sysmon_wdog_list_mtx);
295 	return error;
296 }
297 
298 /*
299  * sysmon_wdog_unregister:
300  *
301  *	Unregister a watchdog device.
302  */
303 void
304 sysmon_wdog_unregister(struct sysmon_wdog *smw)
305 {
306 
307 	mutex_enter(&sysmon_wdog_list_mtx);
308 	sysmon_wdog_count--;
309 	LIST_REMOVE(smw, smw_list);
310 	mutex_exit(&sysmon_wdog_list_mtx);
311 }
312 
313 /*
314  * sysmon_wdog_find:
315  *
316  *	Find a watchdog device.  We increase the reference
317  *	count on a match.
318  */
319 struct sysmon_wdog *
320 sysmon_wdog_find(const char *name)
321 {
322 	struct sysmon_wdog *smw;
323 
324 	mutex_enter(&sysmon_wdog_list_mtx);
325 
326 	for (smw = LIST_FIRST(&sysmon_wdog_list); smw != NULL;
327 	     smw = LIST_NEXT(smw, smw_list)) {
328 		if (strcmp(smw->smw_name, name) == 0)
329 			break;
330 	}
331 
332 	if (smw != NULL)
333 		smw->smw_refcnt++;
334 
335 	mutex_exit(&sysmon_wdog_list_mtx);
336 	return smw;
337 }
338 
339 /*
340  * sysmon_wdog_release:
341  *
342  *	Release a watchdog device.
343  */
344 void
345 sysmon_wdog_release(struct sysmon_wdog *smw)
346 {
347 
348 	mutex_enter(&sysmon_wdog_list_mtx);
349 	KASSERT(smw->smw_refcnt != 0);
350 	smw->smw_refcnt--;
351 	mutex_exit(&sysmon_wdog_list_mtx);
352 }
353 
354 /*
355  * sysmon_wdog_setmode:
356  *
357  *	Set the mode of a watchdog device.
358  */
359 int
360 sysmon_wdog_setmode(struct sysmon_wdog *smw, int mode, u_int period)
361 {
362 	u_int operiod = smw->smw_period;
363 	int omode = smw->smw_mode;
364 	int error = 0;
365 
366 	smw->smw_period = period;
367 	smw->smw_mode = mode;
368 
369 	switch (mode & WDOG_MODE_MASK) {
370 	case WDOG_MODE_DISARMED:
371 		if (smw != sysmon_armed_wdog) {
372 			error = EINVAL;
373 			goto out;
374 		}
375 		break;
376 
377 	case WDOG_MODE_KTICKLE:
378 	case WDOG_MODE_UTICKLE:
379 	case WDOG_MODE_ETICKLE:
380 		if (sysmon_armed_wdog != NULL) {
381 			error = EBUSY;
382 			goto out;
383 		}
384 		break;
385 
386 	default:
387 		error = EINVAL;
388 		goto out;
389 	}
390 
391 	error = (*smw->smw_setmode)(smw);
392 
393  out:
394 	if (error) {
395 		smw->smw_period = operiod;
396 		smw->smw_mode = omode;
397 	} else {
398 		if ((mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
399 			sysmon_armed_wdog = NULL;
400 			smw->smw_tickler = (pid_t) -1;
401 			smw->smw_refcnt--;
402 			if ((omode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE)
403 				callout_stop(&sysmon_wdog_callout);
404 		} else {
405 			sysmon_armed_wdog = smw;
406 			smw->smw_refcnt++;
407 			if ((mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
408 				callout_reset(&sysmon_wdog_callout,
409 				    WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
410 				    sysmon_wdog_ktickle, NULL);
411 			}
412 		}
413 	}
414 	return error;
415 }
416 
417 /*
418  * sysmon_wdog_ktickle:
419  *
420  *	Kernel watchdog tickle routine.
421  */
422 void
423 sysmon_wdog_ktickle(void *arg)
424 {
425 	struct sysmon_wdog *smw;
426 
427 	mutex_enter(&sysmon_wdog_mtx);
428 	if ((smw = sysmon_armed_wdog) != NULL) {
429 		if ((*smw->smw_tickle)(smw) != 0) {
430 			printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
431 			    "FAILED!\n", smw->smw_name);
432 			/*
433 			 * ...we will probably reboot soon.
434 			 */
435 		}
436 		callout_reset(&sysmon_wdog_callout,
437 		    WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
438 		    sysmon_wdog_ktickle, NULL);
439 	}
440 	mutex_exit(&sysmon_wdog_mtx);
441 }
442 
443 /*
444  * sysmon_wdog_shutdown:
445  *
446  *	Perform shutdown-time operations.
447  */
448 void
449 sysmon_wdog_shutdown(void *arg)
450 {
451 	struct sysmon_wdog *smw;
452 
453 	/*
454 	 * XXX Locking here?  I don't think it's necessary.
455 	 */
456 
457 	if ((smw = sysmon_armed_wdog) != NULL) {
458 		if (sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED,
459 		    smw->smw_period))
460 			printf("WARNING: FAILED TO SHUTDOWN WATCHDOG %s!\n",
461 			    smw->smw_name);
462 	}
463 }
464