Home | History | Annotate | Line # | Download | only in kern
subr_fault.c revision 1.1
      1  1.1  maxv /*	$NetBSD: subr_fault.c,v 1.1 2020/06/07 09:45:19 maxv Exp $	*/
      2  1.1  maxv 
      3  1.1  maxv /*
      4  1.1  maxv  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.1  maxv  * All rights reserved.
      6  1.1  maxv  *
      7  1.1  maxv  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  maxv  * by Maxime Villard.
      9  1.1  maxv  *
     10  1.1  maxv  * Redistribution and use in source and binary forms, with or without
     11  1.1  maxv  * modification, are permitted provided that the following conditions
     12  1.1  maxv  * are met:
     13  1.1  maxv  * 1. Redistributions of source code must retain the above copyright
     14  1.1  maxv  *    notice, this list of conditions and the following disclaimer.
     15  1.1  maxv  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  maxv  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  maxv  *    documentation and/or other materials provided with the distribution.
     18  1.1  maxv  *
     19  1.1  maxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  maxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  maxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  maxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  maxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  maxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  maxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  maxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  maxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  maxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  maxv  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  maxv  */
     31  1.1  maxv 
     32  1.1  maxv #include <sys/cdefs.h>
     33  1.1  maxv __KERNEL_RCSID(0, "$NetBSD: subr_fault.c,v 1.1 2020/06/07 09:45:19 maxv Exp $");
     34  1.1  maxv 
     35  1.1  maxv #include <sys/module.h>
     36  1.1  maxv #include <sys/param.h>
     37  1.1  maxv #include <sys/systm.h>
     38  1.1  maxv #include <sys/kernel.h>
     39  1.1  maxv 
     40  1.1  maxv #include <sys/conf.h>
     41  1.1  maxv #include <sys/types.h>
     42  1.1  maxv #include <sys/specificdata.h>
     43  1.1  maxv #include <sys/kmem.h>
     44  1.1  maxv #include <sys/atomic.h>
     45  1.1  maxv #include <sys/ioccom.h>
     46  1.1  maxv #include <sys/lwp.h>
     47  1.1  maxv #include <sys/fault.h>
     48  1.1  maxv 
     49  1.1  maxv typedef struct {
     50  1.1  maxv 	volatile bool enabled;
     51  1.1  maxv 	volatile unsigned long nth;
     52  1.1  maxv 	volatile unsigned long cnt;
     53  1.1  maxv 	volatile unsigned long nfaults;
     54  1.1  maxv } fault_t;
     55  1.1  maxv 
     56  1.1  maxv static fault_t fault_global __cacheline_aligned = {
     57  1.1  maxv 	.enabled = false,
     58  1.1  maxv 	.nth = 2,
     59  1.1  maxv 	.cnt = 0,
     60  1.1  maxv 	.nfaults = 0
     61  1.1  maxv };
     62  1.1  maxv 
     63  1.1  maxv static kmutex_t fault_global_lock __cacheline_aligned;
     64  1.1  maxv static specificdata_key_t fault_lwp_key;
     65  1.1  maxv 
     66  1.1  maxv /* -------------------------------------------------------------------------- */
     67  1.1  maxv 
     68  1.1  maxv bool
     69  1.1  maxv fault_inject(void)
     70  1.1  maxv {
     71  1.1  maxv 	volatile unsigned long cnt;
     72  1.1  maxv 	fault_t *f;
     73  1.1  maxv 
     74  1.1  maxv 	if (__predict_false(cold))
     75  1.1  maxv 		return false;
     76  1.1  maxv 
     77  1.1  maxv 	if (__predict_false(atomic_load_acquire(&fault_global.enabled))) {
     78  1.1  maxv 		f = &fault_global;
     79  1.1  maxv 	} else {
     80  1.1  maxv 		f = lwp_getspecific(fault_lwp_key);
     81  1.1  maxv 		if (__predict_true(f == NULL))
     82  1.1  maxv 			return false;
     83  1.1  maxv 		if (__predict_false(!f->enabled))
     84  1.1  maxv 			return false;
     85  1.1  maxv 	}
     86  1.1  maxv 
     87  1.1  maxv 	cnt = atomic_inc_ulong_nv(&f->cnt);
     88  1.1  maxv 	if (__predict_false(cnt % atomic_load_relaxed(&f->nth) == 0)) {
     89  1.1  maxv 		atomic_inc_ulong(&f->nfaults);
     90  1.1  maxv 		return true;
     91  1.1  maxv 	}
     92  1.1  maxv 
     93  1.1  maxv 	return false;
     94  1.1  maxv }
     95  1.1  maxv 
     96  1.1  maxv /* -------------------------------------------------------------------------- */
     97  1.1  maxv 
     98  1.1  maxv static int
     99  1.1  maxv fault_open(dev_t dev, int flag, int mode, struct lwp *l)
    100  1.1  maxv {
    101  1.1  maxv 	return 0;
    102  1.1  maxv }
    103  1.1  maxv 
    104  1.1  maxv static int
    105  1.1  maxv fault_close(dev_t dev, int flag, int mode, struct lwp *l)
    106  1.1  maxv {
    107  1.1  maxv 	return 0;
    108  1.1  maxv }
    109  1.1  maxv 
    110  1.1  maxv static int
    111  1.1  maxv fault_ioc_enable(struct fault_ioc_enable *args)
    112  1.1  maxv {
    113  1.1  maxv 	fault_t *f;
    114  1.1  maxv 
    115  1.1  maxv 	if (args->mode != FAULT_MODE_NTH)
    116  1.1  maxv 		return EINVAL;
    117  1.1  maxv 	if (args->nth < 2)
    118  1.1  maxv 		return EINVAL;
    119  1.1  maxv 
    120  1.1  maxv 	switch (args->scope) {
    121  1.1  maxv 	case FAULT_SCOPE_GLOBAL:
    122  1.1  maxv 		mutex_enter(&fault_global_lock);
    123  1.1  maxv 		if (fault_global.enabled) {
    124  1.1  maxv 			mutex_exit(&fault_global_lock);
    125  1.1  maxv 			return EEXIST;
    126  1.1  maxv 		}
    127  1.1  maxv 		atomic_store_relaxed(&fault_global.nth, args->nth);
    128  1.1  maxv 		fault_global.cnt = 0;
    129  1.1  maxv 		fault_global.nfaults = 0;
    130  1.1  maxv 		atomic_store_release(&fault_global.enabled, true);
    131  1.1  maxv 		mutex_exit(&fault_global_lock);
    132  1.1  maxv 		break;
    133  1.1  maxv 	case FAULT_SCOPE_LWP:
    134  1.1  maxv 		f = lwp_getspecific(fault_lwp_key);
    135  1.1  maxv 		if (f != NULL) {
    136  1.1  maxv 			if (f->enabled)
    137  1.1  maxv 				return EEXIST;
    138  1.1  maxv 		} else {
    139  1.1  maxv 			f = kmem_zalloc(sizeof(*f), KM_SLEEP);
    140  1.1  maxv 			lwp_setspecific(fault_lwp_key, f);
    141  1.1  maxv 		}
    142  1.1  maxv 		atomic_store_relaxed(&f->nth, args->nth);
    143  1.1  maxv 		f->cnt = 0;
    144  1.1  maxv 		f->nfaults = 0;
    145  1.1  maxv 		atomic_store_release(&f->enabled, true);
    146  1.1  maxv 		break;
    147  1.1  maxv 	default:
    148  1.1  maxv 		return EINVAL;
    149  1.1  maxv 	}
    150  1.1  maxv 
    151  1.1  maxv 	return 0;
    152  1.1  maxv }
    153  1.1  maxv 
    154  1.1  maxv static int
    155  1.1  maxv fault_ioc_disable(struct fault_ioc_disable *args)
    156  1.1  maxv {
    157  1.1  maxv 	fault_t *f;
    158  1.1  maxv 
    159  1.1  maxv 	switch (args->scope) {
    160  1.1  maxv 	case FAULT_SCOPE_GLOBAL:
    161  1.1  maxv 		mutex_enter(&fault_global_lock);
    162  1.1  maxv 		if (!fault_global.enabled) {
    163  1.1  maxv 			mutex_exit(&fault_global_lock);
    164  1.1  maxv 			return ENOENT;
    165  1.1  maxv 		}
    166  1.1  maxv 		atomic_store_release(&fault_global.enabled, false);
    167  1.1  maxv 		mutex_exit(&fault_global_lock);
    168  1.1  maxv 		break;
    169  1.1  maxv 	case FAULT_SCOPE_LWP:
    170  1.1  maxv 		f = lwp_getspecific(fault_lwp_key);
    171  1.1  maxv 		if (f == NULL)
    172  1.1  maxv 			return ENOENT;
    173  1.1  maxv 		if (!f->enabled)
    174  1.1  maxv 			return ENOENT;
    175  1.1  maxv 		atomic_store_release(&f->enabled, false);
    176  1.1  maxv 		break;
    177  1.1  maxv 	default:
    178  1.1  maxv 		return EINVAL;
    179  1.1  maxv 	}
    180  1.1  maxv 
    181  1.1  maxv 	return 0;
    182  1.1  maxv }
    183  1.1  maxv 
    184  1.1  maxv static int
    185  1.1  maxv fault_ioc_getinfo(struct fault_ioc_getinfo *args)
    186  1.1  maxv {
    187  1.1  maxv 	fault_t *f;
    188  1.1  maxv 
    189  1.1  maxv 	switch (args->scope) {
    190  1.1  maxv 	case FAULT_SCOPE_GLOBAL:
    191  1.1  maxv 		args->nfaults = atomic_load_relaxed(&fault_global.nfaults);
    192  1.1  maxv 		break;
    193  1.1  maxv 	case FAULT_SCOPE_LWP:
    194  1.1  maxv 		f = lwp_getspecific(fault_lwp_key);
    195  1.1  maxv 		if (f == NULL)
    196  1.1  maxv 			return ENOENT;
    197  1.1  maxv 		args->nfaults = atomic_load_relaxed(&f->nfaults);
    198  1.1  maxv 		break;
    199  1.1  maxv 	default:
    200  1.1  maxv 		return EINVAL;
    201  1.1  maxv 	}
    202  1.1  maxv 
    203  1.1  maxv 	return 0;
    204  1.1  maxv }
    205  1.1  maxv 
    206  1.1  maxv static int
    207  1.1  maxv fault_ioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    208  1.1  maxv {
    209  1.1  maxv 	switch (cmd) {
    210  1.1  maxv 	case FAULT_IOC_ENABLE:
    211  1.1  maxv 		return fault_ioc_enable(addr);
    212  1.1  maxv 	case FAULT_IOC_DISABLE:
    213  1.1  maxv 		return fault_ioc_disable(addr);
    214  1.1  maxv 	case FAULT_IOC_GETINFO:
    215  1.1  maxv 		return fault_ioc_getinfo(addr);
    216  1.1  maxv 	default:
    217  1.1  maxv 		return EINVAL;
    218  1.1  maxv 	}
    219  1.1  maxv }
    220  1.1  maxv 
    221  1.1  maxv const struct cdevsw fault_cdevsw = {
    222  1.1  maxv 	.d_open = fault_open,
    223  1.1  maxv 	.d_close = fault_close,
    224  1.1  maxv 	.d_read = noread,
    225  1.1  maxv 	.d_write = nowrite,
    226  1.1  maxv 	.d_ioctl = fault_ioctl,
    227  1.1  maxv 	.d_stop = nostop,
    228  1.1  maxv 	.d_tty = notty,
    229  1.1  maxv 	.d_poll = nopoll,
    230  1.1  maxv 	.d_mmap = nommap,
    231  1.1  maxv 	.d_kqfilter = nokqfilter,
    232  1.1  maxv 	.d_discard = nodiscard,
    233  1.1  maxv 	.d_flag = D_OTHER | D_MPSAFE
    234  1.1  maxv };
    235  1.1  maxv 
    236  1.1  maxv /* -------------------------------------------------------------------------- */
    237  1.1  maxv 
    238  1.1  maxv MODULE(MODULE_CLASS_MISC, fault, NULL);
    239  1.1  maxv 
    240  1.1  maxv static void
    241  1.1  maxv fault_lwp_free(void *arg)
    242  1.1  maxv {
    243  1.1  maxv 	fault_t *f = (fault_t *)arg;
    244  1.1  maxv 
    245  1.1  maxv 	if (f == NULL) {
    246  1.1  maxv 		return;
    247  1.1  maxv 	}
    248  1.1  maxv 
    249  1.1  maxv 	kmem_free(f, sizeof(*f));
    250  1.1  maxv }
    251  1.1  maxv 
    252  1.1  maxv static void
    253  1.1  maxv fault_init(void)
    254  1.1  maxv {
    255  1.1  maxv 	mutex_init(&fault_global_lock, MUTEX_DEFAULT, IPL_NONE);
    256  1.1  maxv 	lwp_specific_key_create(&fault_lwp_key, fault_lwp_free);
    257  1.1  maxv }
    258  1.1  maxv 
    259  1.1  maxv static int
    260  1.1  maxv fault_modcmd(modcmd_t cmd, void *arg)
    261  1.1  maxv {
    262  1.1  maxv    	switch (cmd) {
    263  1.1  maxv 	case MODULE_CMD_INIT:
    264  1.1  maxv 		fault_init();
    265  1.1  maxv 		return 0;
    266  1.1  maxv 	case MODULE_CMD_FINI:
    267  1.1  maxv 		return EINVAL;
    268  1.1  maxv 	default:
    269  1.1  maxv 		return ENOTTY;
    270  1.1  maxv 	}
    271  1.1  maxv }
    272