summaryrefslogtreecommitdiff
path: root/sys/dev/sysmon/swwdog.c
blob: 74801c6f242a6fca9f97209b9505ea1351ffaa8c (plain)
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
/*	$NetBSD: swwdog.c,v 1.23 2021/12/31 11:05:41 riastradh Exp $	*/

/*
 * Copyright (c) 2004, 2005 Steven M. Bellovin
 * All rights reserved.
 *
 * 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 Steven M. Bellovin
 * 4. The name of the author contributors may not be used to endorse or
 *    promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.23 2021/12/31 11:05:41 riastradh Exp $");

/*
 *
 * Software watchdog timer
 *
 */
#include <sys/param.h>
#include <sys/device.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/reboot.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/wdog.h>
#include <sys/workqueue.h>
#include <sys/module.h>
#include <dev/sysmon/sysmonvar.h>

#ifndef _MODULE
#include "opt_modular.h"
#endif

struct swwdog_softc {
	device_t		sc_dev;
	struct sysmon_wdog	sc_smw;
	struct callout		sc_c;
	int			sc_armed;
};

bool	swwdog_reboot = false;	/* false --> panic , true  --> reboot */

static struct workqueue *wq;
static device_t		swwdog_dev;

MODULE(MODULE_CLASS_DRIVER, swwdog, "sysmon_wdog");

#ifdef _MODULE
CFDRIVER_DECL(swwdog, DV_DULL, NULL);
#endif

int swwdogattach(int);

static int	swwdog_setmode(struct sysmon_wdog *);
static int	swwdog_tickle(struct sysmon_wdog *);
static bool	swwdog_suspend(device_t, const pmf_qual_t *);
static int	swwdog_arm(struct swwdog_softc *);
static int	swwdog_disarm(struct swwdog_softc *);

static void	swwdog_panic(void *);

static int	swwdog_match(device_t, cfdata_t, void *);
static void	swwdog_attach(device_t, device_t, void *);
static int	swwdog_detach(device_t, int);
static bool	swwdog_suspend(device_t, const pmf_qual_t *);

static int	swwdog_init(void *);
static int	swwdog_fini(void *);
static int	swwdog_modcmd(modcmd_t, void *);

CFATTACH_DECL_NEW(swwdog, sizeof(struct swwdog_softc),
	swwdog_match, swwdog_attach, swwdog_detach, NULL);
extern struct cfdriver swwdog_cd;

#define	SWDOG_DEFAULT	60		/* 60-second default period */

static void
doreboot(struct work *wrkwrkwrk, void *p)
{

	kern_reboot(0, NULL);
}

int
swwdogattach(int n __unused)
{
	int error;
	static struct cfdata cf;

	if (workqueue_create(&wq, "swwreboot", doreboot, NULL,
	    PRI_NONE, IPL_NONE, 0) != 0) {
		aprint_error("failed to create swwdog reboot wq");
		return 1;
	}

	error = config_cfattach_attach(swwdog_cd.cd_name, &swwdog_ca);
	if (error) {
		aprint_error("%s: unable to attach cfattach\n",
		    swwdog_cd.cd_name);
		workqueue_destroy(wq);
		return error;
	}

	cf.cf_name = swwdog_cd.cd_name;
	cf.cf_atname = swwdog_cd.cd_name;
	cf.cf_unit = 0;
	cf.cf_fstate = FSTATE_STAR;
	cf.cf_pspec = NULL;
	cf.cf_loc = NULL;
	cf.cf_flags = 0;

	swwdog_dev = config_attach_pseudo(&cf);

	if (swwdog_dev == NULL) {
		config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca);
		workqueue_destroy(wq);
		return 1;
	}
	return 0;
}

static int
swwdog_match(device_t parent, cfdata_t data, void *aux)
{

	return 1;
}

static void
swwdog_attach(device_t parent, device_t self, void *aux)
{
	struct swwdog_softc *sc = device_private(self);

	sc->sc_dev = self;
	sc->sc_smw.smw_name = device_xname(self);
	sc->sc_smw.smw_cookie = sc;
	sc->sc_smw.smw_setmode = swwdog_setmode;
	sc->sc_smw.smw_tickle = swwdog_tickle;
	sc->sc_smw.smw_period = SWDOG_DEFAULT;

	callout_init(&sc->sc_c, 0);
	callout_setfunc(&sc->sc_c, swwdog_panic, sc);

	if (sysmon_wdog_register(&sc->sc_smw) == 0)
		aprint_normal_dev(self, "software watchdog initialized\n");
	else {
		aprint_error_dev(self, "unable to register software "
		    "watchdog with sysmon\n");
		callout_destroy(&sc->sc_c);
		return;
	}

	if (!pmf_device_register(self, swwdog_suspend, NULL))
		aprint_error_dev(self, "couldn't establish power handler\n");
}

static int
swwdog_detach(device_t self, int flags)
{
	struct swwdog_softc *sc = device_private(self);

	pmf_device_deregister(self);
	swwdog_disarm(sc);
	sysmon_wdog_unregister(&sc->sc_smw);
	callout_destroy(&sc->sc_c);
	workqueue_destroy(wq);

	return 0;
}

static bool
swwdog_suspend(device_t dev, const pmf_qual_t *qual)
{
	struct swwdog_softc *sc = device_private(dev);

	/* Don't allow suspend if watchdog is armed */
	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
		return false;
	return true;
}

static int
swwdog_setmode(struct sysmon_wdog *smw)
{
	struct swwdog_softc *sc = smw->smw_cookie;
	int error = 0;

	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
		error = swwdog_disarm(sc);
	} else {
		if (smw->smw_period == 0)
			return EINVAL;
		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
			sc->sc_smw.smw_period = SWDOG_DEFAULT;
		else
			sc->sc_smw.smw_period = smw->smw_period;
		error = swwdog_arm(sc);
	}
	return error;
}

static int
swwdog_tickle(struct sysmon_wdog *smw)
{

	return swwdog_arm(smw->smw_cookie);
}

static int
swwdog_arm(struct swwdog_softc *sc)
{

	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
	return 0;
}

static int
swwdog_disarm(struct swwdog_softc *sc)
{

	callout_stop(&sc->sc_c);
	return 0;
}

static void
swwdog_panic(void *vsc)
{
	struct swwdog_softc *sc = vsc;
	static struct work wk; /* we'll need it max once */
	bool do_panic;

	do_panic = !swwdog_reboot;
	swwdog_reboot = false;
	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */

	printf("%s: %d second timer expired\n", "swwdog",
	    sc->sc_smw.smw_period);

	if (do_panic)
		panic("watchdog timer expired");
	else
		workqueue_enqueue(wq, &wk, NULL);
}

SYSCTL_SETUP(swwdog_sysctl_setup, "swwdog sysctl")
{
	const struct sysctlnode *me;

	sysctl_createv(clog, 0, NULL, &me, CTLFLAG_READWRITE,
	    CTLTYPE_NODE, "swwdog", NULL,
	    NULL, 0, NULL, 0,
	    CTL_HW, CTL_CREATE, CTL_EOL);
	sysctl_createv(clog, 0, NULL, NULL, CTLFLAG_READWRITE,
	    CTLTYPE_BOOL, "reboot", "reboot if timer expires",
	    NULL, 0, &swwdog_reboot, sizeof(bool),
	    CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
}

/*
 * Module management
 */

static int
swwdog_init(void *arg)
{
	/*
	 * Merge the driver info into the kernel tables and attach the
	 * pseudo-device
	 */
	int error = 0;


#ifdef _MODULE
	error = config_cfdriver_attach(&swwdog_cd);
	if (error) {
		aprint_error("%s: unable to attach cfdriver\n",
		    swwdog_cd.cd_name);
		return error;
	}
	error = swwdogattach(1);
	if (error) {
		aprint_error("%s: device attach failed\n", swwdog_cd.cd_name);
		config_cfdriver_detach(&swwdog_cd);
	}
#endif

	return error;
}

static int
swwdog_fini(void *arg)
{
	int error;

	error = config_detach(swwdog_dev, 0);

#ifdef _MODULE
	error = config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca);
	if (error)
		aprint_error("%s: error detaching cfattach: %d\n",
		    swwdog_cd.cd_name, error);

	error = config_cfdriver_detach(&swwdog_cd);
	if (error)
		aprint_error("%s: error detaching cfdriver: %d\n",
		    swwdog_cd.cd_name, error);
#endif

        return error;
}

static int
swwdog_modcmd(modcmd_t cmd, void *arg)
{
	int ret;

	switch (cmd) {
	case MODULE_CMD_INIT:
		ret = swwdog_init(arg);
		break;
	case MODULE_CMD_FINI:
		ret = swwdog_fini(arg);
		break;
	case MODULE_CMD_STAT:
	default:
		ret = ENOTTY;
	}

	return ret;
}