Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add fault(4).



details:   https://anonhg.NetBSD.org/src/rev/21409432a774
branches:  trunk
changeset: 934205:21409432a774
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sun Jun 07 09:45:19 2020 +0000

description:
Add fault(4).

diffstat:

 etc/MAKEDEV.tmpl            |    6 +-
 sys/arch/amd64/conf/ALL     |    7 +-
 sys/arch/amd64/conf/GENERIC |    7 +-
 sys/conf/files              |    3 +-
 sys/conf/majors             |    3 +-
 sys/kern/files.kern         |    3 +-
 sys/kern/subr_fault.c       |  271 ++++++++++++++++++++++++++++++++++++++++++++
 sys/kern/subr_pool.c        |   15 ++-
 sys/sys/fault.h             |   77 ++++++++++++
 9 files changed, 382 insertions(+), 10 deletions(-)

diffs (truncated from 531 to 300 lines):

diff -r 1544296492d4 -r 21409432a774 etc/MAKEDEV.tmpl
--- a/etc/MAKEDEV.tmpl  Sun Jun 07 06:08:20 2020 +0000
+++ b/etc/MAKEDEV.tmpl  Sun Jun 07 09:45:19 2020 +0000
@@ -1,5 +1,5 @@
 #!/bin/sh -
-#      $NetBSD: MAKEDEV.tmpl,v 1.219 2020/06/05 17:20:56 maxv Exp $
+#      $NetBSD: MAKEDEV.tmpl,v 1.220 2020/06/07 09:45:19 maxv Exp $
 #
 # Copyright (c) 2003,2007,2008 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -1761,6 +1761,10 @@
        mkdev vio9p$unit c %vio9p_chr% $unit 660
        ;;
 
+fault)
+       mkdev fault c %fault_chr% 0
+       ;;
+
 nvram)
        mkdev nvram c %nvram_chr% 0 644
        ;;
diff -r 1544296492d4 -r 21409432a774 sys/arch/amd64/conf/ALL
--- a/sys/arch/amd64/conf/ALL   Sun Jun 07 06:08:20 2020 +0000
+++ b/sys/arch/amd64/conf/ALL   Sun Jun 07 09:45:19 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.152 2020/05/30 13:35:31 jdolecek Exp $
+# $NetBSD: ALL,v 1.153 2020/06/07 09:45:19 maxv Exp $
 # From NetBSD: GENERIC,v 1.787 2006/10/01 18:37:54 bouyer Exp
 #
 # ALL machine description file
@@ -17,7 +17,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident         "ALL-$Revision: 1.152 $"
+#ident         "ALL-$Revision: 1.153 $"
 
 maxusers       64              # estimated number of users
 
@@ -137,6 +137,9 @@
 makeoptions    KCOV=1
 options                KCOV
 
+# Fault Injection Driver.
+options        FAULT
+
 # Compatibility options
 options        EXEC_AOUT       # required by binaries from before 1.5
 options        COMPAT_386BSD_MBRPART # recognize old partition ID
diff -r 1544296492d4 -r 21409432a774 sys/arch/amd64/conf/GENERIC
--- a/sys/arch/amd64/conf/GENERIC       Sun Jun 07 06:08:20 2020 +0000
+++ b/sys/arch/amd64/conf/GENERIC       Sun Jun 07 09:45:19 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.568 2020/05/16 18:31:48 christos Exp $
+# $NetBSD: GENERIC,v 1.569 2020/06/07 09:45:19 maxv Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident         "GENERIC-$Revision: 1.568 $"
+#ident         "GENERIC-$Revision: 1.569 $"
 
 maxusers       64              # estimated number of users
 
@@ -158,6 +158,9 @@
 #makeoptions   KCOV=1
 #options       KCOV
 
+# Fault Injection Driver.
+#options       FAULT
+
 # Compatibility options
 # x86_64 never shipped with a.out binaries; the two options below are
 # only relevant to 32-bit i386 binaries
diff -r 1544296492d4 -r 21409432a774 sys/conf/files
--- a/sys/conf/files    Sun Jun 07 06:08:20 2020 +0000
+++ b/sys/conf/files    Sun Jun 07 09:45:19 2020 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files,v 1.1267 2020/05/20 09:18:25 hkenken Exp $
+#      $NetBSD: files,v 1.1268 2020/06/07 09:45:19 maxv Exp $
 #      @(#)files.newconf       7.5 (Berkeley) 5/10/93
 
 version        20171118
@@ -38,6 +38,7 @@
 defflag                                KCOV
 defflag opt_pool.h             POOL_QUARANTINE
 defflag opt_pool.h             POOL_NOCACHE
+defflag                                FAULT
 
 defparam opt_copy_symtab.h     makeoptions_COPY_SYMTAB
 
diff -r 1544296492d4 -r 21409432a774 sys/conf/majors
--- a/sys/conf/majors   Sun Jun 07 06:08:20 2020 +0000
+++ b/sys/conf/majors   Sun Jun 07 09:45:19 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: majors,v 1.93 2020/01/19 20:41:18 riastradh Exp $
+# $NetBSD: majors,v 1.94 2020/06/07 09:45:19 maxv Exp $
 #
 # Device majors for Machine-Independent drivers.
 #
@@ -88,3 +88,4 @@
 device-major ipmi      char 354                   ipmi
 device-major vhci      char 355            vhci
 device-major vio9p     char 356                   vio9p
+device-major fault     char 357                   fault
diff -r 1544296492d4 -r 21409432a774 sys/kern/files.kern
--- a/sys/kern/files.kern       Sun Jun 07 06:08:20 2020 +0000
+++ b/sys/kern/files.kern       Sun Jun 07 09:45:19 2020 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.kern,v 1.48 2020/05/16 18:31:50 christos Exp $
+#      $NetBSD: files.kern,v 1.49 2020/06/07 09:45:19 maxv Exp $
 
 #
 # kernel sources
@@ -118,6 +118,7 @@
 file   kern/subr_evcnt.c               kern
 file   kern/subr_exec_fd.c             kern
 file   kern/subr_extent.c              kern
+file   kern/subr_fault.c               fault
 file   kern/subr_hash.c                kern
 file   kern/subr_humanize.c            kern
 file   kern/subr_interrupt.c           kern
diff -r 1544296492d4 -r 21409432a774 sys/kern/subr_fault.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/kern/subr_fault.c     Sun Jun 07 09:45:19 2020 +0000
@@ -0,0 +1,271 @@
+/*     $NetBSD: subr_fault.c,v 1.1 2020/06/07 09:45:19 maxv Exp $      */
+
+/*
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Maxime Villard.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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: subr_fault.c,v 1.1 2020/06/07 09:45:19 maxv Exp $");
+
+#include <sys/module.h>
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+
+#include <sys/conf.h>
+#include <sys/types.h>
+#include <sys/specificdata.h>
+#include <sys/kmem.h>
+#include <sys/atomic.h>
+#include <sys/ioccom.h>
+#include <sys/lwp.h>
+#include <sys/fault.h>
+
+typedef struct {
+       volatile bool enabled;
+       volatile unsigned long nth;
+       volatile unsigned long cnt;
+       volatile unsigned long nfaults;
+} fault_t;
+
+static fault_t fault_global __cacheline_aligned = {
+       .enabled = false,
+       .nth = 2,
+       .cnt = 0,
+       .nfaults = 0
+};
+
+static kmutex_t fault_global_lock __cacheline_aligned;
+static specificdata_key_t fault_lwp_key;
+
+/* -------------------------------------------------------------------------- */
+
+bool
+fault_inject(void)
+{
+       volatile unsigned long cnt;
+       fault_t *f;
+
+       if (__predict_false(cold))
+               return false;
+
+       if (__predict_false(atomic_load_acquire(&fault_global.enabled))) {
+               f = &fault_global;
+       } else {
+               f = lwp_getspecific(fault_lwp_key);
+               if (__predict_true(f == NULL))
+                       return false;
+               if (__predict_false(!f->enabled))
+                       return false;
+       }
+
+       cnt = atomic_inc_ulong_nv(&f->cnt);
+       if (__predict_false(cnt % atomic_load_relaxed(&f->nth) == 0)) {
+               atomic_inc_ulong(&f->nfaults);
+               return true;
+       }
+
+       return false;
+}
+
+/* -------------------------------------------------------------------------- */
+
+static int
+fault_open(dev_t dev, int flag, int mode, struct lwp *l)
+{
+       return 0;
+}
+
+static int
+fault_close(dev_t dev, int flag, int mode, struct lwp *l)
+{
+       return 0;
+}
+
+static int
+fault_ioc_enable(struct fault_ioc_enable *args)
+{
+       fault_t *f;
+
+       if (args->mode != FAULT_MODE_NTH)
+               return EINVAL;
+       if (args->nth < 2)
+               return EINVAL;
+
+       switch (args->scope) {
+       case FAULT_SCOPE_GLOBAL:
+               mutex_enter(&fault_global_lock);
+               if (fault_global.enabled) {
+                       mutex_exit(&fault_global_lock);
+                       return EEXIST;
+               }
+               atomic_store_relaxed(&fault_global.nth, args->nth);
+               fault_global.cnt = 0;
+               fault_global.nfaults = 0;
+               atomic_store_release(&fault_global.enabled, true);
+               mutex_exit(&fault_global_lock);
+               break;
+       case FAULT_SCOPE_LWP:
+               f = lwp_getspecific(fault_lwp_key);
+               if (f != NULL) {
+                       if (f->enabled)
+                               return EEXIST;
+               } else {
+                       f = kmem_zalloc(sizeof(*f), KM_SLEEP);
+                       lwp_setspecific(fault_lwp_key, f);
+               }
+               atomic_store_relaxed(&f->nth, args->nth);
+               f->cnt = 0;
+               f->nfaults = 0;
+               atomic_store_release(&f->enabled, true);
+               break;
+       default:
+               return EINVAL;
+       }
+
+       return 0;
+}
+
+static int
+fault_ioc_disable(struct fault_ioc_disable *args)
+{
+       fault_t *f;
+
+       switch (args->scope) {
+       case FAULT_SCOPE_GLOBAL:
+               mutex_enter(&fault_global_lock);
+               if (!fault_global.enabled) {
+                       mutex_exit(&fault_global_lock);
+                       return ENOENT;
+               }
+               atomic_store_release(&fault_global.enabled, false);
+               mutex_exit(&fault_global_lock);
+               break;
+       case FAULT_SCOPE_LWP:
+               f = lwp_getspecific(fault_lwp_key);
+               if (f == NULL)



Home | Main Index | Thread Index | Old Index