1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
/* $NetBSD: sysmon_wdog.c,v 1.29 2015/12/14 01:08:47 pgoyette Exp $ */
/*-
* Copyright (c) 2000 Zembu Labs, Inc.
* All rights reserved.
*
* Author: Jason R. Thorpe <thorpej@zembu.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Zembu Labs, Inc.
* 4. Neither the name of Zembu Labs nor the names of its employees may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
* RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
* CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Watchdog timer framework for sysmon. Hardware (and software)
* watchdog timers can register themselves here to provide a
* watchdog function, which provides an abstract interface to the
* user.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sysmon_wdog.c,v 1.29 2015/12/14 01:08:47 pgoyette Exp $");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/condvar.h>
#include <sys/mutex.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/once.h>
#include <dev/sysmon/sysmonvar.h>
static LIST_HEAD(, sysmon_wdog) sysmon_wdog_list =
LIST_HEAD_INITIALIZER(&sysmon_wdog_list);
static int sysmon_wdog_count;
static kmutex_t sysmon_wdog_list_mtx, sysmon_wdog_mtx;
static kcondvar_t sysmon_wdog_cv;
static struct sysmon_wdog *sysmon_armed_wdog;
static callout_t sysmon_wdog_callout;
static void *sysmon_wdog_sdhook;
static void *sysmon_wdog_cphook;
struct sysmon_wdog *sysmon_wdog_find(const char *);
void sysmon_wdog_release(struct sysmon_wdog *);
int sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);
void sysmon_wdog_ktickle(void *);
void sysmon_wdog_critpoll(void *);
void sysmon_wdog_shutdown(void *);
void sysmon_wdog_ref(struct sysmon_wdog *);
static struct sysmon_opvec sysmon_wdog_opvec = {
sysmonopen_wdog, sysmonclose_wdog, sysmonioctl_wdog,
NULL, NULL, NULL
};
MODULE(MODULE_CLASS_DRIVER, sysmon_wdog, "sysmon");
ONCE_DECL(once_wdog);
static int
wdog_preinit(void)
{
mutex_init(&sysmon_wdog_list_mtx, MUTEX_DEFAULT, IPL_NONE);
mutex_init(&sysmon_wdog_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
cv_init(&sysmon_wdog_cv, "wdogref");
callout_init(&sysmon_wdog_callout, 0);
return 0;
}
int
sysmon_wdog_init(void)
{
int error;
(void)RUN_ONCE(&once_wdog, wdog_preinit);
sysmon_wdog_sdhook = shutdownhook_establish(sysmon_wdog_shutdown, NULL);
if (sysmon_wdog_sdhook == NULL)
printf("WARNING: unable to register watchdog shutdown hook\n");
sysmon_wdog_cphook = critpollhook_establish(sysmon_wdog_critpoll, NULL);
if (sysmon_wdog_cphook == NULL)
printf("WARNING: unable to register watchdog critpoll hook\n");
error = sysmon_attach_minor(SYSMON_MINOR_WDOG, &sysmon_wdog_opvec);
return error;
}
int
sysmon_wdog_fini(void)
{
int error;
if ( ! LIST_EMPTY(&sysmon_wdog_list))
return EBUSY;
error = sysmon_attach_minor(SYSMON_MINOR_WDOG, NULL);
if (error == 0) {
callout_destroy(&sysmon_wdog_callout);
critpollhook_disestablish(sysmon_wdog_cphook);
shutdownhook_disestablish(sysmon_wdog_sdhook);
cv_destroy(&sysmon_wdog_cv);
mutex_destroy(&sysmon_wdog_mtx);
mutex_destroy(&sysmon_wdog_list_mtx);
}
return error;
}
/*
* sysmonopen_wdog:
*
* Open the system monitor device.
*/
int
sysmonopen_wdog(dev_t dev, int flag, int mode, struct lwp *l)
{
return 0;
}
/*
* sysmonclose_wdog:
*
* Close the system monitor device.
*/
int
sysmonclose_wdog(dev_t dev, int flag, int mode, struct lwp *l)
{
struct sysmon_wdog *smw;
int error = 0;
/*
* If this is the last close, and there is a watchdog
* running in UTICKLE mode, we need to disable it,
* otherwise the system will reset in short order.
*
* XXX Maybe we should just go into KTICKLE mode?
*/
mutex_enter(&sysmon_wdog_mtx);
if ((smw = sysmon_armed_wdog) != NULL) {
if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_UTICKLE) {
error = sysmon_wdog_setmode(smw,
WDOG_MODE_DISARMED, smw->smw_period);
if (error) {
printf("WARNING: UNABLE TO DISARM "
"WATCHDOG %s ON CLOSE!\n",
smw->smw_name);
/*
* ...we will probably reboot soon.
*/
}
}
}
mutex_exit(&sysmon_wdog_mtx);
return error;
}
/*
* sysmonioctl_wdog:
*
* Perform a watchdog control request.
*/
int
sysmonioctl_wdog(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct sysmon_wdog *smw;
int error = 0;
switch (cmd) {
case WDOGIOC_GMODE:
{
struct wdog_mode *wm = (void *) data;
wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
smw = sysmon_wdog_find(wm->wm_name);
if (smw == NULL) {
error = ESRCH;
break;
}
wm->wm_mode = smw->smw_mode;
wm->wm_period = smw->smw_period;
sysmon_wdog_release(smw);
break;
}
case WDOGIOC_SMODE:
{
struct wdog_mode *wm = (void *) data;
if ((flag & FWRITE) == 0) {
error = EPERM;
break;
}
wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
smw = sysmon_wdog_find(wm->wm_name);
if (smw == NULL) {
error = ESRCH;
break;
}
if (wm->wm_mode & ~(WDOG_MODE_MASK|WDOG_FEATURE_MASK))
error = EINVAL;
else {
mutex_enter(&sysmon_wdog_mtx);
error = sysmon_wdog_setmode(smw, wm->wm_mode,
wm->wm_period);
mutex_exit(&sysmon_wdog_mtx);
}
sysmon_wdog_release(smw);
break;
}
case WDOGIOC_WHICH:
{
struct wdog_mode *wm = (void *) data;
mutex_enter(&sysmon_wdog_mtx);
if ((smw = sysmon_armed_wdog) != NULL) {
strcpy(wm->wm_name, smw->smw_name);
wm->wm_mode = smw->smw_mode;
wm->wm_period = smw->smw_period;
} else
error = ESRCH;
mutex_exit(&sysmon_wdog_mtx);
break;
}
case WDOGIOC_TICKLE:
if ((flag & FWRITE) == 0) {
error = EPERM;
break;
}
mutex_enter(&sysmon_wdog_mtx);
if ((smw = sysmon_armed_wdog) != NULL) {
error = (*smw->smw_tickle)(smw);
if (error == 0)
smw->smw_tickler = l->l_proc->p_pid;
} else
error = ESRCH;
mutex_exit(&sysmon_wdog_mtx);
break;
case WDOGIOC_GTICKLER:
if ((smw = sysmon_armed_wdog) != NULL)
*(pid_t *)data = smw->smw_tickler;
else
error = ESRCH;
break;
case WDOGIOC_GWDOGS:
{
struct wdog_conf *wc = (void *) data;
char *cp;
int i;
mutex_enter(&sysmon_wdog_list_mtx);
if (wc->wc_names == NULL)
wc->wc_count = sysmon_wdog_count;
else {
for (i = 0, cp = wc->wc_names,
smw = LIST_FIRST(&sysmon_wdog_list);
i < sysmon_wdog_count && smw != NULL && error == 0;
i++, cp += WDOG_NAMESIZE,
smw = LIST_NEXT(smw, smw_list))
error = copyout(smw->smw_name, cp,
strlen(smw->smw_name) + 1);
wc->wc_count = i;
}
mutex_exit(&sysmon_wdog_list_mtx);
break;
}
default:
error = ENOTTY;
}
return error;
}
/*
* sysmon_wdog_register:
*
* Register a watchdog device.
*/
int
sysmon_wdog_register(struct sysmon_wdog *smw)
{
struct sysmon_wdog *lsmw;
int error = 0;
(void)RUN_ONCE(&once_wdog, wdog_preinit);
mutex_enter(&sysmon_wdog_list_mtx);
LIST_FOREACH(lsmw, &sysmon_wdog_list, smw_list) {
if (strcmp(lsmw->smw_name, smw->smw_name) == 0) {
error = EEXIST;
goto out;
}
}
smw->smw_mode = WDOG_MODE_DISARMED;
smw->smw_tickler = (pid_t) -1;
smw->smw_refcnt = 0;
sysmon_wdog_count++;
LIST_INSERT_HEAD(&sysmon_wdog_list, smw, smw_list);
out:
mutex_exit(&sysmon_wdog_list_mtx);
return error;
}
/*
* sysmon_wdog_unregister:
*
* Unregister a watchdog device.
*/
int
sysmon_wdog_unregister(struct sysmon_wdog *smw)
{
int rc = 0;
mutex_enter(&sysmon_wdog_list_mtx);
while (smw->smw_refcnt > 0 && rc == 0) {
aprint_debug("%s: %d users remain\n", smw->smw_name,
smw->smw_refcnt);
rc = cv_wait_sig(&sysmon_wdog_cv, &sysmon_wdog_list_mtx);
}
if (rc == 0) {
sysmon_wdog_count--;
LIST_REMOVE(smw, smw_list);
}
mutex_exit(&sysmon_wdog_list_mtx);
return rc;
}
/*
* sysmon_wdog_critpoll:
*
* Perform critical operations during long polling periods
*/
void
sysmon_wdog_critpoll(void *arg)
{
struct sysmon_wdog *smw = sysmon_armed_wdog;
if (smw == NULL)
return;
if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
if ((*smw->smw_tickle)(smw) != 0) {
printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
"FAILED!\n", smw->smw_name);
}
}
}
/*
* sysmon_wdog_find:
*
* Find a watchdog device. We increase the reference
* count on a match.
*/
struct sysmon_wdog *
sysmon_wdog_find(const char *name)
{
struct sysmon_wdog *smw;
mutex_enter(&sysmon_wdog_list_mtx);
LIST_FOREACH(smw, &sysmon_wdog_list, smw_list) {
if (strcmp(smw->smw_name, name) == 0)
break;
}
if (smw != NULL)
smw->smw_refcnt++;
mutex_exit(&sysmon_wdog_list_mtx);
return smw;
}
/*
* sysmon_wdog_release:
*
* Release a watchdog device.
*/
void
sysmon_wdog_release(struct sysmon_wdog *smw)
{
mutex_enter(&sysmon_wdog_list_mtx);
KASSERT(smw->smw_refcnt != 0);
smw->smw_refcnt--;
cv_signal(&sysmon_wdog_cv);
mutex_exit(&sysmon_wdog_list_mtx);
}
void
sysmon_wdog_ref(struct sysmon_wdog *smw)
{
mutex_enter(&sysmon_wdog_list_mtx);
smw->smw_refcnt++;
mutex_exit(&sysmon_wdog_list_mtx);
}
/*
* sysmon_wdog_setmode:
*
* Set the mode of a watchdog device.
*/
int
sysmon_wdog_setmode(struct sysmon_wdog *smw, int mode, u_int period)
{
u_int operiod = smw->smw_period;
int omode = smw->smw_mode;
int error = 0;
smw->smw_period = period;
smw->smw_mode = mode;
switch (mode & WDOG_MODE_MASK) {
case WDOG_MODE_DISARMED:
if (smw != sysmon_armed_wdog) {
error = EINVAL;
goto out;
}
break;
case WDOG_MODE_KTICKLE:
case WDOG_MODE_UTICKLE:
case WDOG_MODE_ETICKLE:
if (sysmon_armed_wdog != NULL) {
error = EBUSY;
goto out;
}
break;
default:
error = EINVAL;
goto out;
}
error = (*smw->smw_setmode)(smw);
out:
if (error) {
smw->smw_period = operiod;
smw->smw_mode = omode;
} else {
if ((mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
sysmon_armed_wdog = NULL;
smw->smw_tickler = (pid_t) -1;
sysmon_wdog_release(smw);
if ((omode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE)
callout_stop(&sysmon_wdog_callout);
} else {
sysmon_armed_wdog = smw;
sysmon_wdog_ref(smw);
if ((mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
callout_reset(&sysmon_wdog_callout,
WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
sysmon_wdog_ktickle, NULL);
}
}
}
return error;
}
/*
* sysmon_wdog_ktickle:
*
* Kernel watchdog tickle routine.
*/
void
sysmon_wdog_ktickle(void *arg)
{
struct sysmon_wdog *smw;
mutex_enter(&sysmon_wdog_mtx);
if ((smw = sysmon_armed_wdog) != NULL) {
if ((*smw->smw_tickle)(smw) != 0) {
printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
"FAILED!\n", smw->smw_name);
/*
* ...we will probably reboot soon.
*/
}
callout_reset(&sysmon_wdog_callout,
WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
sysmon_wdog_ktickle, NULL);
}
mutex_exit(&sysmon_wdog_mtx);
}
/*
* sysmon_wdog_shutdown:
*
* Perform shutdown-time operations.
*/
void
sysmon_wdog_shutdown(void *arg)
{
struct sysmon_wdog *smw;
/*
* XXX Locking here? I don't think it's necessary.
*/
if ((smw = sysmon_armed_wdog) != NULL) {
if (sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED,
smw->smw_period))
printf("WARNING: FAILED TO SHUTDOWN WATCHDOG %s!\n",
smw->smw_name);
}
}
static
int
sysmon_wdog_modcmd(modcmd_t cmd, void *arg)
{
int ret;
switch (cmd) {
case MODULE_CMD_INIT:
ret = sysmon_wdog_init();
break;
case MODULE_CMD_FINI:
ret = sysmon_wdog_fini();
break;
case MODULE_CMD_STAT:
default:
ret = ENOTTY;
}
return ret;
}
|