kern_crashme.c revision 1.7 1 /* $NetBSD: kern_crashme.c,v 1.7 2022/08/30 22:38:26 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2018, 2019 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * kern_crashme.c: special debugging routines, designed for debugging
33 * kernel crashes.
34 *
35 * supports crashme sysctl nodes, to test various ways the system can
36 * panic or crash. you can add and remove nodes.
37 */
38
39 #ifdef _KERNEL_OPT
40 #include "opt_ddb.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46 #include <sys/kthread.h>
47 #include <sys/kmem.h>
48 #include <sys/mutex.h>
49 #include <sys/crashme.h>
50
51 #ifdef DDB
52 #include <ddb/ddb.h>
53 #endif
54
55 #define DPRINTF(fmt, ...) \
56 printf("%s:%d: " fmt "\n", __func__, __LINE__, ## __VA_ARGS__)
57
58 static int crashme_sysctl_forwarder(SYSCTLFN_PROTO);
59
60 static int crashme_panic(int);
61 static int crashme_null_deref(int);
62 static int crashme_null_jump(int);
63 #ifdef DDB
64 static int crashme_ddb(int);
65 #endif
66 #ifdef LOCKDEBUG
67 static int crashme_kernel_lock_spinout(int);
68 #endif
69 static int crashme_mutex_recursion(int);
70
71 #define CMNODE(name, lname, func) \
72 { \
73 .cn_name = name, \
74 .cn_longname = lname, \
75 .cn_fn = func, \
76 }
77
78 static crashme_node nodes[] = {
79 CMNODE("panic", "plain old panic", crashme_panic),
80 CMNODE("null_deref", "null dereference", crashme_null_deref),
81 CMNODE("null_jump", "jump to null", crashme_null_jump),
82 #ifdef DDB
83 CMNODE("ddb", "enter ddb directly", crashme_ddb),
84 #endif
85 #ifdef LOCKDEBUG
86 CMNODE("kernel_lock_spinout", "infinite kernel lock",
87 crashme_kernel_lock_spinout),
88 #endif
89 CMNODE("mutex_recursion", "enter the same mutex twice",
90 crashme_mutex_recursion),
91 };
92 static crashme_node *first_node;
93 static kmutex_t crashme_lock;
94 static const struct sysctlnode *crashme_root = NULL;
95 static bool crashme_enable = 0;
96
97 /*
98 * add a crashme node dynamically. return -1 on failure, 0 on success.
99 */
100 int
101 crashme_add(crashme_node *ncn)
102 {
103 int rv = -1;
104 crashme_node *cn;
105 crashme_node *last = NULL;
106
107 if (crashme_root == NULL)
108 return -1;
109
110 mutex_enter(&crashme_lock);
111 for (cn = first_node; cn; last = cn, cn = cn->cn_next) {
112 if (strcmp(cn->cn_name, ncn->cn_name) == 0)
113 break;
114 }
115 if (!cn) {
116 ncn->cn_next = NULL;
117
118 rv = sysctl_createv(NULL, 0,
119 &crashme_root, &ncn->cn_sysctl,
120 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
121 CTLTYPE_INT, ncn->cn_name,
122 SYSCTL_DESCR(ncn->cn_longname),
123 crashme_sysctl_forwarder, 0,
124 NULL, 0,
125 CTL_CREATE, CTL_EOL);
126
127 /* don't insert upon failure */
128 if (rv == 0) {
129 if (last)
130 last->cn_next = ncn;
131 if (first_node == NULL)
132 first_node = ncn;
133 }
134 }
135 mutex_exit(&crashme_lock);
136
137 return rv;
138 }
139
140 /*
141 * remove a crashme node. return -1 on failure, 0 on success.
142 */
143 int
144 crashme_remove(crashme_node *rcn)
145 {
146 crashme_node *cn, *prev = NULL;
147
148 mutex_enter(&crashme_lock);
149 for (cn = first_node; cn; prev = cn, cn = cn->cn_next) {
150 int rv;
151
152 if (cn != rcn)
153 continue;
154
155 if (cn == first_node)
156 first_node = cn->cn_next;
157 if (prev)
158 prev->cn_next = cn->cn_next;
159
160 if ((rv = sysctl_destroyv(NULL, CTL_DEBUG, crashme_root,
161 cn->cn_name, CTL_EOL)) == 0)
162 printf("%s: unable to remove %s from sysctl\n",
163 __func__, cn->cn_name);
164 break;
165 }
166 mutex_exit(&crashme_lock);
167
168 if (cn == NULL)
169 return -1;
170
171 return 0;
172 }
173
174 /*
175 * list or execute a crashme node
176 */
177 static int
178 crashme_sysctl_forwarder(SYSCTLFN_ARGS)
179 {
180 struct sysctlnode node;
181 crashme_node *cn;
182 int error, arg = 0;
183
184 for (cn = first_node; cn; cn = cn->cn_next) {
185 if (cn->cn_sysctl == rnode)
186 break;
187 }
188 if (!cn) {
189 return EINVAL;
190 }
191
192 node = *rnode;
193 node.sysctl_data = &arg;
194 error = sysctl_lookup(SYSCTLFN_CALL(&node));
195 if (error || newp == NULL)
196 return (error);
197
198 if (!crashme_enable)
199 return EACCES;
200
201 DPRINTF("invoking \"%s\" (%s)", cn->cn_name, cn->cn_longname);
202 if ((*cn->cn_fn)(arg) != 0)
203 panic("crashme on %s failed!\n", cn->cn_name);
204 return 0;
205 }
206
207 /*
208 * register the various nodes with sysctl.
209 */
210 SYSCTL_SETUP(selfdebug_crashme, "sysctl crashme setup")
211 {
212 int rv;
213 size_t n;
214
215 mutex_init(&crashme_lock, MUTEX_DEFAULT, IPL_NONE);
216
217 rv = sysctl_createv(NULL, 0, NULL, &crashme_root,
218 CTLFLAG_PERMANENT,
219 CTLTYPE_NODE, "crashme",
220 SYSCTL_DESCR("Crashme options"),
221 NULL, 0, NULL, 0,
222 CTL_DEBUG, CTL_CREATE, CTL_EOL);
223
224 if (rv != 0 || crashme_root == NULL)
225 return;
226
227 rv = sysctl_createv(NULL, 0, NULL, NULL,
228 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
229 CTLTYPE_BOOL, "crashme_enable",
230 SYSCTL_DESCR("Enable crashme"),
231 NULL, 0, &crashme_enable, 0,
232 CTL_DEBUG, CTL_CREATE, CTL_EOL);
233
234 for (n = 0; n < __arraycount(nodes); n++) {
235 crashme_node *cn = &nodes[n];
236
237 rv = crashme_add(cn);
238
239 /* don't insert */
240 if (rv != 0)
241 continue;
242 }
243 }
244
245 /*
246 * actual panic functions below
247 */
248 static int
249 crashme_panic(int flags)
250 {
251
252 panic("crashme plain old panic");
253 return -1;
254 }
255
256 static int
257 crashme_null_deref(int flags)
258 {
259
260 *(volatile char *)0 = 0;
261 return -1;
262 }
263
264 static int
265 crashme_null_jump(int flags)
266 {
267 void (*volatile f)(int) = NULL;
268
269 (*f)(flags);
270 /* make sure to have a nontrivial return address here */
271 return -1;
272 }
273
274 #ifdef DDB
275 static int
276 crashme_ddb(int flags)
277 {
278
279 Debugger();
280 return 0;
281 }
282 #endif
283
284 #ifdef LOCKDEBUG
285 static int
286 crashme_kernel_lock_spinout(int flags)
287 {
288
289 KERNEL_LOCK(1, NULL);
290 for (;;)
291 __insn_barrier();
292 KERNEL_UNLOCK_ONE(NULL);
293 return 0;
294 }
295 #endif
296
297 static int
298 crashme_mutex_recursion(int flags)
299 {
300 kmutex_t crashme_spinlock;
301
302 switch (flags) {
303 case 0:
304 return 0;
305 case 1:
306 default:
307 /*
308 * printf makes the return address of the first
309 * mutex_enter call a little more obvious, so the line
310 * number of the _return address_ for the first
311 * mutex_enter doesn't confusingly point at the second
312 * mutex_enter.
313 */
314 mutex_enter(&crashme_lock);
315 printf("%s: locked once\n", __func__);
316 mutex_enter(&crashme_lock);
317 printf("%s: locked twice\n", __func__);
318 return -1;
319 case 2:
320 mutex_init(&crashme_spinlock, MUTEX_DEFAULT, IPL_VM);
321 printf("%s: initialized\n", __func__);
322 mutex_enter(&crashme_spinlock);
323 printf("%s: locked once\n", __func__);
324 mutex_enter(&crashme_spinlock);
325 printf("%s: locked twice\n", __func__);
326 return -1;
327 }
328 }
329