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