aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorBryan Venteicher <bryanv@FreeBSD.org>2014-01-18 06:14:38 +0000
committerBryan Venteicher <bryanv@FreeBSD.org>2014-01-18 06:14:38 +0000
commit10c401805724439cf6002a20b5da029c62fa90bb (patch)
treec79fe0d99bb20ba4f1b31817bc84bee027d9136a /sys
parent5ed2724da0ad5443c2b533cccaa3e8b9357b4686 (diff)
downloadsrc-10c401805724439cf6002a20b5da029c62fa90bb.tar.gz
src-10c401805724439cf6002a20b5da029c62fa90bb.zip
Add very simple virtio_random(4) driver to harvest entropy from host
Reviewed by: markm (random bits only)
Notes
Notes: svn path=/head/; revision=260847
Diffstat (limited to 'sys')
-rw-r--r--sys/amd64/conf/NOTES1
-rw-r--r--sys/conf/files.amd641
-rw-r--r--sys/conf/files.i3861
-rw-r--r--sys/dev/virtio/random/virtio_random.c231
-rw-r--r--sys/i386/conf/NOTES1
-rw-r--r--sys/modules/virtio/Makefile2
-rw-r--r--sys/modules/virtio/random/Makefile36
-rw-r--r--sys/sys/random.h1
8 files changed, 273 insertions, 1 deletions
diff --git a/sys/amd64/conf/NOTES b/sys/amd64/conf/NOTES
index 28d68c816317..9333c1b51239 100644
--- a/sys/amd64/conf/NOTES
+++ b/sys/amd64/conf/NOTES
@@ -472,6 +472,7 @@ device vtnet # VirtIO Ethernet device
device virtio_blk # VirtIO Block device
device virtio_scsi # VirtIO SCSI device
device virtio_balloon # VirtIO Memory Balloon device
+device virtio_random # VirtIO Entropy device
device hyperv # HyperV drivers
diff --git a/sys/conf/files.amd64 b/sys/conf/files.amd64
index d1bdcd97bfb5..3937823383ac 100644
--- a/sys/conf/files.amd64
+++ b/sys/conf/files.amd64
@@ -448,6 +448,7 @@ dev/virtio/network/if_vtnet.c optional vtnet
dev/virtio/block/virtio_blk.c optional virtio_blk
dev/virtio/balloon/virtio_balloon.c optional virtio_balloon
dev/virtio/scsi/virtio_scsi.c optional virtio_scsi
+dev/virtio/random/virtio_random.c optional virtio_random
isa/syscons_isa.c optional sc
isa/vga_isa.c optional vga
kern/kern_clocksource.c standard
diff --git a/sys/conf/files.i386 b/sys/conf/files.i386
index eb8697cd9d70..77b556542e22 100644
--- a/sys/conf/files.i386
+++ b/sys/conf/files.i386
@@ -414,6 +414,7 @@ dev/virtio/network/if_vtnet.c optional vtnet
dev/virtio/block/virtio_blk.c optional virtio_blk
dev/virtio/balloon/virtio_balloon.c optional virtio_balloon
dev/virtio/scsi/virtio_scsi.c optional virtio_scsi
+dev/virtio/random/virtio_random.c optional virtio_random
i386/acpica/acpi_machdep.c optional acpi
acpi_wakecode.o optional acpi \
dependency "$S/i386/acpica/acpi_wakecode.S assym.s" \
diff --git a/sys/dev/virtio/random/virtio_random.c b/sys/dev/virtio/random/virtio_random.c
new file mode 100644
index 000000000000..f44878f21897
--- /dev/null
+++ b/sys/dev/virtio/random/virtio_random.c
@@ -0,0 +1,231 @@
+/*-
+ * Copyright (c) 2013, Bryan Venteicher <bryanv@FreeBSD.org>
+ * 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 unmodified, 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 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.
+ */
+
+/* Driver for VirtIO entropy device. */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/sglist.h>
+#include <sys/callout.h>
+#include <sys/random.h>
+
+#include <machine/bus.h>
+#include <machine/resource.h>
+#include <sys/bus.h>
+
+#include <dev/virtio/virtio.h>
+#include <dev/virtio/virtqueue.h>
+
+struct vtrnd_softc {
+ device_t vtrnd_dev;
+ uint64_t vtrnd_features;
+ struct callout vtrnd_callout;
+ struct virtqueue *vtrnd_vq;
+};
+
+static int vtrnd_modevent(module_t, int, void *);
+
+static int vtrnd_probe(device_t);
+static int vtrnd_attach(device_t);
+static int vtrnd_detach(device_t);
+
+static void vtrnd_negotiate_features(struct vtrnd_softc *);
+static int vtrnd_alloc_virtqueue(struct vtrnd_softc *);
+static void vtrnd_harvest(struct vtrnd_softc *);
+static void vtrnd_timer(void *);
+
+#define VTRND_FEATURES 0
+
+static struct virtio_feature_desc vtrnd_feature_desc[] = {
+ { 0, NULL }
+};
+
+static device_method_t vtrnd_methods[] = {
+ /* Device methods. */
+ DEVMETHOD(device_probe, vtrnd_probe),
+ DEVMETHOD(device_attach, vtrnd_attach),
+ DEVMETHOD(device_detach, vtrnd_detach),
+
+ DEVMETHOD_END
+};
+
+static driver_t vtrnd_driver = {
+ "vtrnd",
+ vtrnd_methods,
+ sizeof(struct vtrnd_softc)
+};
+static devclass_t vtrnd_devclass;
+
+DRIVER_MODULE(virtio_random, virtio_pci, vtrnd_driver, vtrnd_devclass,
+ vtrnd_modevent, 0);
+MODULE_VERSION(virtio_random, 1);
+MODULE_DEPEND(virtio_random, virtio, 1, 1, 1);
+
+static int
+vtrnd_modevent(module_t mod, int type, void *unused)
+{
+ int error;
+
+ switch (type) {
+ case MOD_LOAD:
+ case MOD_QUIESCE:
+ case MOD_UNLOAD:
+ case MOD_SHUTDOWN:
+ error = 0;
+ break;
+ default:
+ error = EOPNOTSUPP;
+ break;
+ }
+
+ return (error);
+}
+
+static int
+vtrnd_probe(device_t dev)
+{
+
+ if (virtio_get_device_type(dev) != VIRTIO_ID_ENTROPY)
+ return (ENXIO);
+
+ device_set_desc(dev, "VirtIO Entropy Adapter");
+
+ return (BUS_PROBE_DEFAULT);
+}
+
+static int
+vtrnd_attach(device_t dev)
+{
+ struct vtrnd_softc *sc;
+ int error;
+
+ sc = device_get_softc(dev);
+ sc->vtrnd_dev = dev;
+
+ callout_init(&sc->vtrnd_callout, CALLOUT_MPSAFE);
+
+ virtio_set_feature_desc(dev, vtrnd_feature_desc);
+ vtrnd_negotiate_features(sc);
+
+ error = vtrnd_alloc_virtqueue(sc);
+ if (error) {
+ device_printf(dev, "cannot allocate virtqueue\n");
+ goto fail;
+ }
+
+ callout_reset(&sc->vtrnd_callout, 5 * hz, vtrnd_timer, sc);
+
+fail:
+ if (error)
+ vtrnd_detach(dev);
+
+ return (error);
+}
+
+static int
+vtrnd_detach(device_t dev)
+{
+ struct vtrnd_softc *sc;
+
+ sc = device_get_softc(dev);
+
+ callout_stop(&sc->vtrnd_callout);
+
+ return (0);
+}
+
+static void
+vtrnd_negotiate_features(struct vtrnd_softc *sc)
+{
+ device_t dev;
+ uint64_t features;
+
+ dev = sc->vtrnd_dev;
+ features = VTRND_FEATURES;
+
+ sc->vtrnd_features = virtio_negotiate_features(dev, features);
+}
+
+static int
+vtrnd_alloc_virtqueue(struct vtrnd_softc *sc)
+{
+ device_t dev;
+ struct vq_alloc_info vq_info;
+
+ dev = sc->vtrnd_dev;
+
+ VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq,
+ "%s request", device_get_nameunit(dev));
+
+ return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
+}
+
+static void
+vtrnd_harvest(struct vtrnd_softc *sc)
+{
+ struct sglist_seg segs[1];
+ struct sglist sg;
+ struct virtqueue *vq;
+ uint32_t value;
+ int error;
+
+ vq = sc->vtrnd_vq;
+
+ sglist_init(&sg, 1, segs);
+ error = sglist_append(&sg, &value, sizeof(value));
+ KASSERT(error == 0 && sg.sg_nseg == 1,
+ ("%s: error %d adding buffer to sglist", __func__, error));
+
+ if (!virtqueue_empty(vq))
+ return;
+ if (virtqueue_enqueue(vq, &value, &sg, 0, 1) != 0)
+ return;
+
+ /*
+ * Poll for the response, but the command is likely already
+ * done when we return from the notify.
+ */
+ virtqueue_notify(vq);
+ virtqueue_poll(vq, NULL);
+
+ random_harvest(&value, sizeof(value), sizeof(value) * NBBY / 2,
+ RANDOM_PURE_VIRTIO);
+}
+
+static void
+vtrnd_timer(void *xsc)
+{
+ struct vtrnd_softc *sc;
+
+ sc = xsc;
+
+ vtrnd_harvest(sc);
+ callout_schedule(&sc->vtrnd_callout, 5 * hz);
+}
diff --git a/sys/i386/conf/NOTES b/sys/i386/conf/NOTES
index 212dbd9a17bf..2325124cdec3 100644
--- a/sys/i386/conf/NOTES
+++ b/sys/i386/conf/NOTES
@@ -800,6 +800,7 @@ device vtnet # VirtIO Ethernet device
device virtio_blk # VirtIO Block device
device virtio_scsi # VirtIO SCSI device
device virtio_balloon # VirtIO Memory Balloon device
+device virtio_random # VirtIO Entropy device
device hyperv # HyperV drivers
diff --git a/sys/modules/virtio/Makefile b/sys/modules/virtio/Makefile
index 12846b2b8066..ecf9441fe081 100644
--- a/sys/modules/virtio/Makefile
+++ b/sys/modules/virtio/Makefile
@@ -23,6 +23,6 @@
# SUCH DAMAGE.
#
-SUBDIR= virtio pci network block balloon scsi
+SUBDIR= virtio pci network block balloon scsi random
.include <bsd.subdir.mk>
diff --git a/sys/modules/virtio/random/Makefile b/sys/modules/virtio/random/Makefile
new file mode 100644
index 000000000000..fb5b9b0cb22e
--- /dev/null
+++ b/sys/modules/virtio/random/Makefile
@@ -0,0 +1,36 @@
+#
+# $FreeBSD$
+#
+# 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 AUTHOR 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 AUTHOR 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.
+#
+
+.PATH: ${.CURDIR}/../../../dev/virtio/random
+
+KMOD= virtio_random
+SRCS= virtio_random.c
+SRCS+= virtio_bus_if.h virtio_if.h
+SRCS+= bus_if.h device_if.h
+
+MFILES= kern/bus_if.m kern/device_if.m \
+ dev/virtio/virtio_bus_if.m dev/virtio/virtio_if.m
+
+.include <bsd.kmod.mk>
diff --git a/sys/sys/random.h b/sys/sys/random.h
index 9fee143751b8..8bb262ab446a 100644
--- a/sys/sys/random.h
+++ b/sys/sys/random.h
@@ -56,6 +56,7 @@ enum esource {
RANDOM_PURE_RDRAND,
RANDOM_PURE_NEHEMIAH,
RANDOM_PURE_RNDTEST,
+ RANDOM_PURE_VIRTIO,
ENTROPYSOURCE
};
void random_harvest(const void *, u_int, u_int, enum esource);