Home | History | Annotate | Line # | Download | only in sysmon
sysmon_wdog.c revision 1.26
      1 /*	$NetBSD: sysmon_wdog.c,v 1.26 2015/04/23 23:22:03 pgoyette 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.26 2015/04/23 23:22:03 pgoyette 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 #include <sys/module.h>
     57 
     58 #include <dev/sysmon/sysmonvar.h>
     59 
     60 static LIST_HEAD(, sysmon_wdog) sysmon_wdog_list =
     61     LIST_HEAD_INITIALIZER(&sysmon_wdog_list);
     62 static int sysmon_wdog_count;
     63 static kmutex_t sysmon_wdog_list_mtx, sysmon_wdog_mtx;
     64 static kcondvar_t sysmon_wdog_cv;
     65 static struct sysmon_wdog *sysmon_armed_wdog;
     66 static callout_t sysmon_wdog_callout;
     67 static void *sysmon_wdog_sdhook;
     68 static void *sysmon_wdog_cphook;
     69 
     70 struct sysmon_wdog *sysmon_wdog_find(const char *);
     71 void	sysmon_wdog_release(struct sysmon_wdog *);
     72 int	sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);
     73 void	sysmon_wdog_ktickle(void *);
     74 void	sysmon_wdog_critpoll(void *);
     75 void	sysmon_wdog_shutdown(void *);
     76 void	sysmon_wdog_ref(struct sysmon_wdog *);
     77 
     78 static struct sysmon_opvec sysmon_wdog_opvec = {
     79         sysmonopen_wdog, sysmonclose_wdog, sysmonioctl_wdog,
     80         NULL, NULL, NULL
     81 };
     82 
     83 MODULE(MODULE_CLASS_MISC, sysmon_wdog, "sysmon");
     84 
     85 int
     86 sysmon_wdog_init(void)
     87 {
     88 	int error;
     89 
     90 	mutex_init(&sysmon_wdog_list_mtx, MUTEX_DEFAULT, IPL_NONE);
     91 	mutex_init(&sysmon_wdog_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
     92 	cv_init(&sysmon_wdog_cv, "wdogref");
     93 	sysmon_wdog_sdhook = shutdownhook_establish(sysmon_wdog_shutdown, NULL);
     94 	if (sysmon_wdog_sdhook == NULL)
     95 		printf("WARNING: unable to register watchdog shutdown hook\n");
     96 	sysmon_wdog_cphook = critpollhook_establish(sysmon_wdog_critpoll, NULL);
     97 	if (sysmon_wdog_cphook == NULL)
     98 		printf("WARNING: unable to register watchdog critpoll hook\n");
     99 	callout_init(&sysmon_wdog_callout, 0);
    100 
    101 	error = sysmon_attach_minor(SYSMON_MINOR_WDOG, &sysmon_wdog_opvec);
    102 
    103 	return error;
    104 }
    105 
    106 int
    107 sysmon_wdog_fini(void)
    108 {
    109 	int error;
    110 
    111 	if ( ! LIST_EMPTY(&sysmon_wdog_list))
    112 		return EBUSY;
    113 
    114 	error = sysmon_attach_minor(SYSMON_MINOR_WDOG, NULL);
    115 
    116 	if (error == 0) {
    117 		callout_destroy(&sysmon_wdog_callout);
    118 		critpollhook_disestablish(sysmon_wdog_cphook);
    119 		shutdownhook_disestablish(sysmon_wdog_sdhook);
    120 		cv_destroy(&sysmon_wdog_cv);
    121 		mutex_destroy(&sysmon_wdog_mtx);
    122 		mutex_destroy(&sysmon_wdog_list_mtx);
    123 	}
    124 
    125 	return error;
    126 }
    127 
    128 /*
    129  * sysmonopen_wdog:
    130  *
    131  *	Open the system monitor device.
    132  */
    133 int
    134 sysmonopen_wdog(dev_t dev, int flag, int mode, struct lwp *l)
    135 {
    136 
    137 	return 0;
    138 }
    139 
    140 /*
    141  * sysmonclose_wdog:
    142  *
    143  *	Close the system monitor device.
    144  */
    145 int
    146 sysmonclose_wdog(dev_t dev, int flag, int mode, struct lwp *l)
    147 {
    148 	struct sysmon_wdog *smw;
    149 	int error = 0;
    150 
    151 	/*
    152 	 * If this is the last close, and there is a watchdog
    153 	 * running in UTICKLE mode, we need to disable it,
    154 	 * otherwise the system will reset in short order.
    155 	 *
    156 	 * XXX Maybe we should just go into KTICKLE mode?
    157 	 */
    158 	mutex_enter(&sysmon_wdog_mtx);
    159 	if ((smw = sysmon_armed_wdog) != NULL) {
    160 		if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_UTICKLE) {
    161 			error = sysmon_wdog_setmode(smw,
    162 			    WDOG_MODE_DISARMED, smw->smw_period);
    163 			if (error) {
    164 				printf("WARNING: UNABLE TO DISARM "
    165 				    "WATCHDOG %s ON CLOSE!\n",
    166 				    smw->smw_name);
    167 				/*
    168 				 * ...we will probably reboot soon.
    169 				 */
    170 			}
    171 		}
    172 	}
    173 	mutex_exit(&sysmon_wdog_mtx);
    174 
    175 	return error;
    176 }
    177 
    178 /*
    179  * sysmonioctl_wdog:
    180  *
    181  *	Perform a watchdog control request.
    182  */
    183 int
    184 sysmonioctl_wdog(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    185 {
    186 	struct sysmon_wdog *smw;
    187 	int error = 0;
    188 
    189 	switch (cmd) {
    190 	case WDOGIOC_GMODE:
    191 	    {
    192 		struct wdog_mode *wm = (void *) data;
    193 
    194 		wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
    195 		smw = sysmon_wdog_find(wm->wm_name);
    196 		if (smw == NULL) {
    197 			error = ESRCH;
    198 			break;
    199 		}
    200 
    201 		wm->wm_mode = smw->smw_mode;
    202 		wm->wm_period = smw->smw_period;
    203 		sysmon_wdog_release(smw);
    204 		break;
    205 	    }
    206 
    207 	case WDOGIOC_SMODE:
    208 	    {
    209 		struct wdog_mode *wm = (void *) data;
    210 
    211 		if ((flag & FWRITE) == 0) {
    212 			error = EPERM;
    213 			break;
    214 		}
    215 
    216 		wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
    217 		smw = sysmon_wdog_find(wm->wm_name);
    218 		if (smw == NULL) {
    219 			error = ESRCH;
    220 			break;
    221 		}
    222 
    223 		if (wm->wm_mode & ~(WDOG_MODE_MASK|WDOG_FEATURE_MASK))
    224 			error = EINVAL;
    225 		else {
    226 			mutex_enter(&sysmon_wdog_mtx);
    227 			error = sysmon_wdog_setmode(smw, wm->wm_mode,
    228 			    wm->wm_period);
    229 			mutex_exit(&sysmon_wdog_mtx);
    230 		}
    231 
    232 		sysmon_wdog_release(smw);
    233 		break;
    234 	    }
    235 
    236 	case WDOGIOC_WHICH:
    237 	    {
    238 		struct wdog_mode *wm = (void *) data;
    239 
    240 		mutex_enter(&sysmon_wdog_mtx);
    241 		if ((smw = sysmon_armed_wdog) != NULL) {
    242 			strcpy(wm->wm_name, smw->smw_name);
    243 			wm->wm_mode = smw->smw_mode;
    244 			wm->wm_period = smw->smw_period;
    245 		} else
    246 			error = ESRCH;
    247 		mutex_exit(&sysmon_wdog_mtx);
    248 		break;
    249 	    }
    250 
    251 	case WDOGIOC_TICKLE:
    252 		if ((flag & FWRITE) == 0) {
    253 			error = EPERM;
    254 			break;
    255 		}
    256 
    257 		mutex_enter(&sysmon_wdog_mtx);
    258 		if ((smw = sysmon_armed_wdog) != NULL) {
    259 			error = (*smw->smw_tickle)(smw);
    260 			if (error == 0)
    261 				smw->smw_tickler = l->l_proc->p_pid;
    262 		} else
    263 			error = ESRCH;
    264 		mutex_exit(&sysmon_wdog_mtx);
    265 		break;
    266 
    267 	case WDOGIOC_GTICKLER:
    268 		if ((smw = sysmon_armed_wdog) != NULL)
    269 			*(pid_t *)data = smw->smw_tickler;
    270 		else
    271 			error = ESRCH;
    272 		break;
    273 
    274 	case WDOGIOC_GWDOGS:
    275 	    {
    276 		struct wdog_conf *wc = (void *) data;
    277 		char *cp;
    278 		int i;
    279 
    280 		mutex_enter(&sysmon_wdog_list_mtx);
    281 		if (wc->wc_names == NULL)
    282 			wc->wc_count = sysmon_wdog_count;
    283 		else {
    284 			for (i = 0, cp = wc->wc_names,
    285 			       smw = LIST_FIRST(&sysmon_wdog_list);
    286 			     i < sysmon_wdog_count && smw != NULL && error == 0;
    287 			     i++, cp += WDOG_NAMESIZE,
    288 			       smw = LIST_NEXT(smw, smw_list))
    289 				error = copyout(smw->smw_name, cp,
    290 				    strlen(smw->smw_name) + 1);
    291 			wc->wc_count = i;
    292 		}
    293 		mutex_exit(&sysmon_wdog_list_mtx);
    294 		break;
    295 	    }
    296 
    297 	default:
    298 		error = ENOTTY;
    299 	}
    300 
    301 	return error;
    302 }
    303 
    304 /*
    305  * sysmon_wdog_register:
    306  *
    307  *	Register a watchdog device.
    308  */
    309 int
    310 sysmon_wdog_register(struct sysmon_wdog *smw)
    311 {
    312 	struct sysmon_wdog *lsmw;
    313 	int error = 0;
    314 
    315 	mutex_enter(&sysmon_wdog_list_mtx);
    316 
    317 	LIST_FOREACH(lsmw, &sysmon_wdog_list, smw_list) {
    318 		if (strcmp(lsmw->smw_name, smw->smw_name) == 0) {
    319 			error = EEXIST;
    320 			goto out;
    321 		}
    322 	}
    323 
    324 	smw->smw_mode = WDOG_MODE_DISARMED;
    325 	smw->smw_tickler = (pid_t) -1;
    326 	smw->smw_refcnt = 0;
    327 	sysmon_wdog_count++;
    328 	LIST_INSERT_HEAD(&sysmon_wdog_list, smw, smw_list);
    329 
    330  out:
    331 	mutex_exit(&sysmon_wdog_list_mtx);
    332 	return error;
    333 }
    334 
    335 /*
    336  * sysmon_wdog_unregister:
    337  *
    338  *	Unregister a watchdog device.
    339  */
    340 int
    341 sysmon_wdog_unregister(struct sysmon_wdog *smw)
    342 {
    343 	int rc = 0;
    344 
    345 	mutex_enter(&sysmon_wdog_list_mtx);
    346 	while (smw->smw_refcnt > 0 && rc == 0) {
    347 		aprint_debug("%s: %d users remain\n", smw->smw_name,
    348 		    smw->smw_refcnt);
    349 		rc = cv_wait_sig(&sysmon_wdog_cv, &sysmon_wdog_list_mtx);
    350 	}
    351 	if (rc == 0) {
    352 		sysmon_wdog_count--;
    353 		LIST_REMOVE(smw, smw_list);
    354 	}
    355 	mutex_exit(&sysmon_wdog_list_mtx);
    356 	return rc;
    357 }
    358 
    359 /*
    360  * sysmon_wdog_critpoll:
    361  *
    362  *	Perform critical operations during long polling periods
    363  */
    364 void
    365 sysmon_wdog_critpoll(void *arg)
    366 {
    367 	struct sysmon_wdog *smw = sysmon_armed_wdog;
    368 
    369 	if (smw == NULL)
    370 		return;
    371 
    372 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
    373 		if ((*smw->smw_tickle)(smw) != 0) {
    374 			printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
    375 			    "FAILED!\n", smw->smw_name);
    376 		}
    377 	}
    378 }
    379 
    380 /*
    381  * sysmon_wdog_find:
    382  *
    383  *	Find a watchdog device.  We increase the reference
    384  *	count on a match.
    385  */
    386 struct sysmon_wdog *
    387 sysmon_wdog_find(const char *name)
    388 {
    389 	struct sysmon_wdog *smw;
    390 
    391 	mutex_enter(&sysmon_wdog_list_mtx);
    392 
    393 	LIST_FOREACH(smw, &sysmon_wdog_list, smw_list) {
    394 		if (strcmp(smw->smw_name, name) == 0)
    395 			break;
    396 	}
    397 
    398 	if (smw != NULL)
    399 		smw->smw_refcnt++;
    400 
    401 	mutex_exit(&sysmon_wdog_list_mtx);
    402 	return smw;
    403 }
    404 
    405 /*
    406  * sysmon_wdog_release:
    407  *
    408  *	Release a watchdog device.
    409  */
    410 void
    411 sysmon_wdog_release(struct sysmon_wdog *smw)
    412 {
    413 
    414 	mutex_enter(&sysmon_wdog_list_mtx);
    415 	KASSERT(smw->smw_refcnt != 0);
    416 	smw->smw_refcnt--;
    417 	cv_signal(&sysmon_wdog_cv);
    418 	mutex_exit(&sysmon_wdog_list_mtx);
    419 }
    420 
    421 void
    422 sysmon_wdog_ref(struct sysmon_wdog *smw)
    423 {
    424 	mutex_enter(&sysmon_wdog_list_mtx);
    425 	smw->smw_refcnt++;
    426 	mutex_exit(&sysmon_wdog_list_mtx);
    427 }
    428 
    429 /*
    430  * sysmon_wdog_setmode:
    431  *
    432  *	Set the mode of a watchdog device.
    433  */
    434 int
    435 sysmon_wdog_setmode(struct sysmon_wdog *smw, int mode, u_int period)
    436 {
    437 	u_int operiod = smw->smw_period;
    438 	int omode = smw->smw_mode;
    439 	int error = 0;
    440 
    441 	smw->smw_period = period;
    442 	smw->smw_mode = mode;
    443 
    444 	switch (mode & WDOG_MODE_MASK) {
    445 	case WDOG_MODE_DISARMED:
    446 		if (smw != sysmon_armed_wdog) {
    447 			error = EINVAL;
    448 			goto out;
    449 		}
    450 		break;
    451 
    452 	case WDOG_MODE_KTICKLE:
    453 	case WDOG_MODE_UTICKLE:
    454 	case WDOG_MODE_ETICKLE:
    455 		if (sysmon_armed_wdog != NULL) {
    456 			error = EBUSY;
    457 			goto out;
    458 		}
    459 		break;
    460 
    461 	default:
    462 		error = EINVAL;
    463 		goto out;
    464 	}
    465 
    466 	error = (*smw->smw_setmode)(smw);
    467 
    468  out:
    469 	if (error) {
    470 		smw->smw_period = operiod;
    471 		smw->smw_mode = omode;
    472 	} else {
    473 		if ((mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    474 			sysmon_armed_wdog = NULL;
    475 			smw->smw_tickler = (pid_t) -1;
    476 			sysmon_wdog_release(smw);
    477 			if ((omode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE)
    478 				callout_stop(&sysmon_wdog_callout);
    479 		} else {
    480 			sysmon_armed_wdog = smw;
    481 			sysmon_wdog_ref(smw);
    482 			if ((mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
    483 				callout_reset(&sysmon_wdog_callout,
    484 				    WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
    485 				    sysmon_wdog_ktickle, NULL);
    486 			}
    487 		}
    488 	}
    489 	return error;
    490 }
    491 
    492 /*
    493  * sysmon_wdog_ktickle:
    494  *
    495  *	Kernel watchdog tickle routine.
    496  */
    497 void
    498 sysmon_wdog_ktickle(void *arg)
    499 {
    500 	struct sysmon_wdog *smw;
    501 
    502 	mutex_enter(&sysmon_wdog_mtx);
    503 	if ((smw = sysmon_armed_wdog) != NULL) {
    504 		if ((*smw->smw_tickle)(smw) != 0) {
    505 			printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
    506 			    "FAILED!\n", smw->smw_name);
    507 			/*
    508 			 * ...we will probably reboot soon.
    509 			 */
    510 		}
    511 		callout_reset(&sysmon_wdog_callout,
    512 		    WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
    513 		    sysmon_wdog_ktickle, NULL);
    514 	}
    515 	mutex_exit(&sysmon_wdog_mtx);
    516 }
    517 
    518 /*
    519  * sysmon_wdog_shutdown:
    520  *
    521  *	Perform shutdown-time operations.
    522  */
    523 void
    524 sysmon_wdog_shutdown(void *arg)
    525 {
    526 	struct sysmon_wdog *smw;
    527 
    528 	/*
    529 	 * XXX Locking here?  I don't think it's necessary.
    530 	 */
    531 
    532 	if ((smw = sysmon_armed_wdog) != NULL) {
    533 		if (sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED,
    534 		    smw->smw_period))
    535 			printf("WARNING: FAILED TO SHUTDOWN WATCHDOG %s!\n",
    536 			    smw->smw_name);
    537 	}
    538 }
    539 static
    540 int
    541 sysmon_wdog_modcmd(modcmd_t cmd, void *arg)
    542 {
    543         int ret;
    544 
    545         switch (cmd) {
    546         case MODULE_CMD_INIT:
    547                 ret = sysmon_wdog_init();
    548                 break;
    549 
    550         case MODULE_CMD_FINI:
    551                 ret = sysmon_wdog_fini();
    552                 break;
    553 
    554         case MODULE_CMD_STAT:
    555         default:
    556                 ret = ENOTTY;
    557         }
    558 
    559         return ret;
    560 }
    561