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