Home | History | Annotate | Line # | Download | only in sysmon
sysmon_wdog.c revision 1.24.28.2
      1 /*	$NetBSD: sysmon_wdog.c,v 1.24.28.2 2014/02/15 10:16:32 matt 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.24.28.2 2014/02/15 10:16:32 matt 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/condvar.h>
     51 #include <sys/mutex.h>
     52 #include <sys/callout.h>
     53 #include <sys/kernel.h>
     54 #include <sys/systm.h>
     55 #include <sys/proc.h>
     56 
     57 #include <dev/sysmon/sysmonvar.h>
     58 
     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;
     67 
     68 struct sysmon_wdog *sysmon_wdog_find(const char *);
     69 void	sysmon_wdog_release(struct sysmon_wdog *);
     70 void	sysmon_wdog_ktickle(void *);
     71 void	sysmon_wdog_shutdown(void *);
     72 void	sysmon_wdog_ref(struct sysmon_wdog *);
     73 
     74 void
     75 sysmon_wdog_init(void)
     76 {
     77 	mutex_init(&sysmon_wdog_list_mtx, MUTEX_DEFAULT, IPL_NONE);
     78 	mutex_init(&sysmon_wdog_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
     79 	cv_init(&sysmon_wdog_cv, "wdogref");
     80 	callout_init(&sysmon_wdog_callout, 0);
     81 }
     82 
     83 /*
     84  * sysmonopen_wdog:
     85  *
     86  *	Open the system monitor device.
     87  */
     88 int
     89 sysmonopen_wdog(dev_t dev, int flag, int mode, struct lwp *l)
     90 {
     91 
     92 	mutex_enter(&sysmon_wdog_list_mtx);
     93 	if (sysmon_wdog_sdhook == NULL) {
     94 		sysmon_wdog_sdhook =
     95 		    shutdownhook_establish(sysmon_wdog_shutdown, NULL);
     96 		if (sysmon_wdog_sdhook == NULL)
     97 			printf("WARNING: unable to register watchdog "
     98 			    "shutdown hook\n");
     99 	}
    100 	mutex_exit(&sysmon_wdog_list_mtx);
    101 
    102 	return 0;
    103 }
    104 
    105 /*
    106  * sysmonclose_wdog:
    107  *
    108  *	Close the system monitor device.
    109  */
    110 int
    111 sysmonclose_wdog(dev_t dev, int flag, int mode, struct lwp *l)
    112 {
    113 	struct sysmon_wdog *smw;
    114 	int error = 0;
    115 
    116 	/*
    117 	 * If this is the last close, and there is a watchdog
    118 	 * running in UTICKLE mode, we need to disable it,
    119 	 * otherwise the system will reset in short order.
    120 	 *
    121 	 * XXX Maybe we should just go into KTICKLE mode?
    122 	 */
    123 	mutex_enter(&sysmon_wdog_mtx);
    124 	if ((smw = sysmon_armed_wdog) != NULL) {
    125 		if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_UTICKLE) {
    126 			error = sysmon_wdog_setmode(smw,
    127 			    WDOG_MODE_DISARMED, smw->smw_period);
    128 			if (error) {
    129 				printf("WARNING: UNABLE TO DISARM "
    130 				    "WATCHDOG %s ON CLOSE!\n",
    131 				    smw->smw_name);
    132 				/*
    133 				 * ...we will probably reboot soon.
    134 				 */
    135 			}
    136 		}
    137 	}
    138 	mutex_exit(&sysmon_wdog_mtx);
    139 
    140 	return error;
    141 }
    142 
    143 /*
    144  * sysmonioctl_wdog:
    145  *
    146  *	Perform a watchdog control request.
    147  */
    148 int
    149 sysmonioctl_wdog(dev_t dev, u_long cmd, void *data, int flag, 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 			mutex_enter(&sysmon_wdog_mtx);
    192 			error = sysmon_wdog_setmode(smw, wm->wm_mode,
    193 			    wm->wm_period);
    194 			mutex_exit(&sysmon_wdog_mtx);
    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 		mutex_enter(&sysmon_wdog_mtx);
    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 		mutex_exit(&sysmon_wdog_mtx);
    213 		break;
    214 	    }
    215 
    216 	case WDOGIOC_TICKLE:
    217 		if ((flag & FWRITE) == 0) {
    218 			error = EPERM;
    219 			break;
    220 		}
    221 
    222 		mutex_enter(&sysmon_wdog_mtx);
    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 		mutex_exit(&sysmon_wdog_mtx);
    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_enter(&sysmon_wdog_list_mtx);
    281 
    282 	LIST_FOREACH(lsmw, &sysmon_wdog_list, smw_list) {
    283 		if (strcmp(lsmw->smw_name, smw->smw_name) == 0) {
    284 			error = EEXIST;
    285 			goto out;
    286 		}
    287 	}
    288 
    289 	smw->smw_mode = WDOG_MODE_DISARMED;
    290 	smw->smw_tickler = (pid_t) -1;
    291 	smw->smw_refcnt = 0;
    292 	sysmon_wdog_count++;
    293 	LIST_INSERT_HEAD(&sysmon_wdog_list, smw, smw_list);
    294 
    295  out:
    296 	mutex_exit(&sysmon_wdog_list_mtx);
    297 	return error;
    298 }
    299 
    300 /*
    301  * sysmon_wdog_unregister:
    302  *
    303  *	Unregister a watchdog device.
    304  */
    305 int
    306 sysmon_wdog_unregister(struct sysmon_wdog *smw)
    307 {
    308 	int rc = 0;
    309 
    310 	mutex_enter(&sysmon_wdog_list_mtx);
    311 	while (smw->smw_refcnt > 0 && rc == 0) {
    312 		aprint_debug("%s: %d users remain\n", smw->smw_name,
    313 		    smw->smw_refcnt);
    314 		rc = cv_wait_sig(&sysmon_wdog_cv, &sysmon_wdog_list_mtx);
    315 	}
    316 	if (rc == 0) {
    317 		sysmon_wdog_count--;
    318 		LIST_REMOVE(smw, smw_list);
    319 	}
    320 	mutex_exit(&sysmon_wdog_list_mtx);
    321 	return rc;
    322 }
    323 
    324 /*
    325  * sysmon_wdog_find:
    326  *
    327  *	Find a watchdog device.  We increase the reference
    328  *	count on a match.
    329  */
    330 struct sysmon_wdog *
    331 sysmon_wdog_find(const char *name)
    332 {
    333 	struct sysmon_wdog *smw;
    334 
    335 	mutex_enter(&sysmon_wdog_list_mtx);
    336 
    337 	LIST_FOREACH(smw, &sysmon_wdog_list, smw_list) {
    338 		if (strcmp(smw->smw_name, name) == 0)
    339 			break;
    340 	}
    341 
    342 	if (smw != NULL)
    343 		smw->smw_refcnt++;
    344 
    345 	mutex_exit(&sysmon_wdog_list_mtx);
    346 	return smw;
    347 }
    348 
    349 /*
    350  * sysmon_wdog_release:
    351  *
    352  *	Release a watchdog device.
    353  */
    354 void
    355 sysmon_wdog_release(struct sysmon_wdog *smw)
    356 {
    357 
    358 	mutex_enter(&sysmon_wdog_list_mtx);
    359 	KASSERT(smw->smw_refcnt != 0);
    360 	smw->smw_refcnt--;
    361 	cv_signal(&sysmon_wdog_cv);
    362 	mutex_exit(&sysmon_wdog_list_mtx);
    363 }
    364 
    365 void
    366 sysmon_wdog_ref(struct sysmon_wdog *smw)
    367 {
    368 	mutex_enter(&sysmon_wdog_list_mtx);
    369 	smw->smw_refcnt++;
    370 	mutex_exit(&sysmon_wdog_list_mtx);
    371 }
    372 
    373 /*
    374  * sysmon_wdog_setmode:
    375  *
    376  *	Set the mode of a watchdog device.
    377  */
    378 int
    379 sysmon_wdog_setmode(struct sysmon_wdog *smw, int mode, u_int period)
    380 {
    381 	u_int operiod = smw->smw_period;
    382 	int omode = smw->smw_mode;
    383 	int error = 0;
    384 
    385 	smw->smw_period = period;
    386 	smw->smw_mode = mode;
    387 
    388 	switch (mode & WDOG_MODE_MASK) {
    389 	case WDOG_MODE_DISARMED:
    390 		if (smw != sysmon_armed_wdog) {
    391 			error = EINVAL;
    392 			goto out;
    393 		}
    394 		break;
    395 
    396 	case WDOG_MODE_KTICKLE:
    397 	case WDOG_MODE_UTICKLE:
    398 	case WDOG_MODE_ETICKLE:
    399 		if (sysmon_armed_wdog != NULL) {
    400 			error = EBUSY;
    401 			goto out;
    402 		}
    403 		break;
    404 
    405 	default:
    406 		error = EINVAL;
    407 		goto out;
    408 	}
    409 
    410 	error = (*smw->smw_setmode)(smw);
    411 
    412  out:
    413 	if (error) {
    414 		smw->smw_period = operiod;
    415 		smw->smw_mode = omode;
    416 	} else {
    417 		if ((mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    418 			sysmon_armed_wdog = NULL;
    419 			smw->smw_tickler = (pid_t) -1;
    420 			sysmon_wdog_release(smw);
    421 			if ((omode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE)
    422 				callout_stop(&sysmon_wdog_callout);
    423 		} else {
    424 			sysmon_armed_wdog = smw;
    425 			sysmon_wdog_ref(smw);
    426 			if ((mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
    427 				callout_reset(&sysmon_wdog_callout,
    428 				    WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
    429 				    sysmon_wdog_ktickle, NULL);
    430 			}
    431 		}
    432 	}
    433 	return error;
    434 }
    435 
    436 /*
    437  * sysmon_wdog_ktickle:
    438  *
    439  *	Kernel watchdog tickle routine.
    440  */
    441 void
    442 sysmon_wdog_ktickle(void *arg)
    443 {
    444 	struct sysmon_wdog *smw;
    445 
    446 	mutex_enter(&sysmon_wdog_mtx);
    447 	if ((smw = sysmon_armed_wdog) != NULL) {
    448 		if ((*smw->smw_tickle)(smw) != 0) {
    449 			printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
    450 			    "FAILED!\n", smw->smw_name);
    451 			/*
    452 			 * ...we will probably reboot soon.
    453 			 */
    454 		}
    455 		callout_reset(&sysmon_wdog_callout,
    456 		    WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
    457 		    sysmon_wdog_ktickle, NULL);
    458 	}
    459 	mutex_exit(&sysmon_wdog_mtx);
    460 }
    461 
    462 /*
    463  * sysmon_wdog_shutdown:
    464  *
    465  *	Perform shutdown-time operations.
    466  */
    467 void
    468 sysmon_wdog_shutdown(void *arg)
    469 {
    470 	struct sysmon_wdog *smw;
    471 
    472 	/*
    473 	 * XXX Locking here?  I don't think it's necessary.
    474 	 */
    475 
    476 	if ((smw = sysmon_armed_wdog) != NULL) {
    477 		if (sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED,
    478 		    smw->smw_period))
    479 			printf("WARNING: FAILED TO SHUTDOWN WATCHDOG %s!\n",
    480 			    smw->smw_name);
    481 	}
    482 }
    483