summaryrefslogtreecommitdiff
path: root/sys/kern/kern_crashme.c
blob: 0d6b716354aa88e76da1c59a705895b0eb8ba36e (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
/*	$NetBSD: kern_crashme.c,v 1.10 2022/09/22 14:39:24 riastradh Exp $	*/

/*
 * Copyright (c) 2018, 2019 Matthew R. Green
 * 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. The name of the author 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 ``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 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.
 */

/*
 * kern_crashme.c:  special debugging routines, designed for debugging
 * kernel crashes.
 *
 * supports crashme sysctl nodes, to test various ways the system can
 * panic or crash.  you can add and remove nodes.
 */

#ifdef _KERNEL_OPT
#include "opt_ddb.h"
#endif

#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/kthread.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/crashme.h>

#ifdef DDB
#include <ddb/ddb.h>
#endif

#define DPRINTF(fmt, ...) \
	printf("%s:%d: " fmt "\n", __func__, __LINE__, ## __VA_ARGS__)

static int crashme_sysctl_forwarder(SYSCTLFN_PROTO);

static int crashme_panic(int);
static int crashme_null_deref(int);
static int crashme_null_jump(int);
#ifdef DDB
static int crashme_ddb(int);
#endif
#ifdef LOCKDEBUG
static int crashme_kernel_lock_spinout(int);
#endif
static int crashme_mutex_recursion(int);

#define CMNODE(name, lname, func)	\
    {					\
	.cn_name = name,		\
	.cn_longname = lname,		\
	.cn_fn = func,			\
    }

static crashme_node nodes[] = {
    CMNODE("panic", "plain old panic", crashme_panic),
    CMNODE("null_deref", "null dereference", crashme_null_deref),
    CMNODE("null_jump", "jump to null", crashme_null_jump),
#ifdef DDB
    CMNODE("ddb", "enter ddb directly", crashme_ddb),
#endif
#ifdef LOCKDEBUG
    CMNODE("kernel_lock_spinout", "infinite loop under kernel lock",
	crashme_kernel_lock_spinout),
#endif
    CMNODE("mutex_recursion", "enter the same mutex twice",
	crashme_mutex_recursion),
};
static crashme_node *first_node;
static kmutex_t crashme_lock;
static int crashme_root = -1;
static bool crashme_enable = 0;

/*
 * add a crashme node dynamically.  return -1 on failure, 0 on success.
 */
int
crashme_add(crashme_node *ncn)
{
	int rv = -1;
	crashme_node *cn;
	crashme_node *last = NULL;
	const struct sysctlnode *cnode;

	if (crashme_root == -1)
		return -1;

	mutex_enter(&crashme_lock);
	for (cn = first_node; cn; last = cn, cn = cn->cn_next) {
		if (strcmp(cn->cn_name, ncn->cn_name) == 0)
			break;
	}
	if (!cn) {
		ncn->cn_next = NULL;

		rv = sysctl_createv(NULL, 0, NULL, &cnode,
		    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
		    CTLTYPE_INT, ncn->cn_name,
		    SYSCTL_DESCR(ncn->cn_longname),
		    crashme_sysctl_forwarder, 0, NULL, 0,
		    CTL_DEBUG, crashme_root, CTL_CREATE, CTL_EOL);

		/* don't insert upon failure */
		if (rv == 0) {
			ncn->cn_sysctl = cnode->sysctl_num;
			if (last)
				last->cn_next = ncn;
			if (first_node == NULL)
				first_node = ncn;
		}
	}
	mutex_exit(&crashme_lock);

	return rv == 0 ? 0 : -1;
}

/*
 * remove a crashme node.  return -1 on failure, 0 on success.
 */
int
crashme_remove(crashme_node *rcn)
{
	crashme_node *cn, *prev = NULL;

	mutex_enter(&crashme_lock);
	for (cn = first_node; cn; prev = cn, cn = cn->cn_next) {
		int rv;

		if (cn != rcn)
			continue;

		if (cn == first_node)
			first_node = cn->cn_next;
		if (prev)
			prev->cn_next = cn->cn_next;

		if ((rv = sysctl_destroyv(NULL, CTL_DEBUG, crashme_root,
			    cn->cn_sysctl, CTL_EOL)) == 0)
			printf("%s: unable to remove %s from sysctl\n",
			    __func__, cn->cn_name);
		break;
	}
	mutex_exit(&crashme_lock);

	if (cn == NULL)
		return -1;

	return 0;
}

/*
 * list or execute a crashme node
 */
static int
crashme_sysctl_forwarder(SYSCTLFN_ARGS)
{
	struct sysctlnode node;
	crashme_node *cn;
	int error, arg = 0;

	for (cn = first_node; cn; cn = cn->cn_next) {
		if (cn->cn_sysctl == rnode->sysctl_num)
			break;
	}
	if (!cn) {
		return EINVAL;
	}

	node = *rnode;
	node.sysctl_data = &arg;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return (error);

	if (!crashme_enable)
		return EACCES;

	DPRINTF("invoking \"%s\" (%s)", cn->cn_name, cn->cn_longname);
	if ((*cn->cn_fn)(arg) != 0)
		panic("crashme on %s failed!\n", cn->cn_name);
	return 0;
}

/*
 * register the various nodes with sysctl.
 */
SYSCTL_SETUP(selfdebug_crashme, "sysctl crashme setup")
{
	const struct sysctlnode *rnode;
	int rv;
	size_t n;

	mutex_init(&crashme_lock, MUTEX_DEFAULT, IPL_NONE);

	KASSERT(crashme_root == -1);

	rv = sysctl_createv(NULL, 0, NULL, &rnode,
	    CTLFLAG_PERMANENT,
	    CTLTYPE_NODE, "crashme",
	    SYSCTL_DESCR("Crashme options"),
	    NULL, 0, NULL, 0,
	    CTL_DEBUG, CTL_CREATE, CTL_EOL);

	if (rv != 0) {
		printf("%s: failed to create sysctl debug.crashme: %d\n",
		    __func__, rv);
		return;
	}
	crashme_root = rnode->sysctl_num;

	rv = sysctl_createv(NULL, 0, NULL, NULL,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
	    CTLTYPE_BOOL, "crashme_enable",
	    SYSCTL_DESCR("Enable crashme"),
	    NULL, 0, &crashme_enable, 0,
	    CTL_DEBUG, CTL_CREATE, CTL_EOL);
	if (rv != 0)
		printf("%s: failed to create sysctl debug.crashme_enable:"
		    " %d\n", __func__, rv);

	for (n = 0; n < __arraycount(nodes); n++) {
		if (crashme_add(&nodes[n]))
			printf("%s: failed to create sysctl"
			    " debug.crashme.%s\n", __func__, nodes[n].cn_name);
	}
}

/*
 * actual panic functions below
 */
static int
crashme_panic(int flags)
{

	panic("crashme plain old panic");
	return -1;
}

static int
crashme_null_deref(int flags)
{

	*(volatile char *)0 = 0;
	return -1;
}

static int
crashme_null_jump(int flags)
{
	void (*volatile f)(int) = NULL;

	(*f)(flags);
	/* make sure to have a nontrivial return address here */
	return -1;
}

#ifdef DDB
static int
crashme_ddb(int flags)
{

	Debugger();
	return 0;
}
#endif

#ifdef LOCKDEBUG
static int
crashme_kernel_lock_spinout(int flags)
{

	KERNEL_LOCK(1, NULL);
	for (;;)
		__insn_barrier();
	KERNEL_UNLOCK_ONE(NULL);
	return 0;
}
#endif

static int
crashme_mutex_recursion(int flags)
{
	kmutex_t crashme_spinlock;

	switch (flags) {
	case 0:
		return 0;
	case 1:
	default:
		/*
		 * printf makes the return address of the first
		 * mutex_enter call a little more obvious, so the line
		 * number of the _return address_ for the first
		 * mutex_enter doesn't confusingly point at the second
		 * mutex_enter.
		 */
		mutex_enter(&crashme_lock);
		printf("%s: locked once\n", __func__);
		mutex_enter(&crashme_lock);
		printf("%s: locked twice\n", __func__);
		return -1;
	case 2:
		mutex_init(&crashme_spinlock, MUTEX_DEFAULT, IPL_VM);
		printf("%s: initialized\n", __func__);
		mutex_enter(&crashme_spinlock);
		printf("%s: locked once\n", __func__);
		mutex_enter(&crashme_spinlock);
		printf("%s: locked twice\n", __func__);
		return -1;
	}
}