2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 Nahanni Systems Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * virtio entropy device emulation.
32 * Randomness is sourced from /dev/random which does not block
33 * once it has been seeded at bootup.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #ifndef WITHOUT_CAPSICUM
41 #include <sys/capsicum.h>
43 #include <sys/linker_set.h>
61 #define VTRND_RINGSZ 64
64 static int pci_vtrnd_debug;
65 #define DPRINTF(params) if (pci_vtrnd_debug) printf params
66 #define WPRINTF(params) printf params
71 struct pci_vtrnd_softc {
72 struct virtio_softc vrsc_vs;
73 struct vqueue_info vrsc_vq;
74 pthread_mutex_t vrsc_mtx;
79 static void pci_vtrnd_reset(void *);
80 static void pci_vtrnd_notify(void *, struct vqueue_info *);
82 static struct virtio_consts vtrnd_vi_consts = {
83 "vtrnd", /* our name */
84 1, /* we support 1 virtqueue */
85 0, /* config reg size */
86 pci_vtrnd_reset, /* reset */
87 pci_vtrnd_notify, /* device-wide qnotify */
88 NULL, /* read virtio config */
89 NULL, /* write virtio config */
90 NULL, /* apply negotiated features */
91 0, /* our capabilities */
96 pci_vtrnd_reset(void *vsc)
98 struct pci_vtrnd_softc *sc;
102 DPRINTF(("vtrnd: device reset requested !\n"));
103 vi_reset_dev(&sc->vrsc_vs);
108 pci_vtrnd_notify(void *vsc, struct vqueue_info *vq)
111 struct pci_vtrnd_softc *sc;
117 if (sc->vrsc_fd < 0) {
122 while (vq_has_descs(vq)) {
123 vq_getchain(vq, &idx, &iov, 1, NULL);
125 len = read(sc->vrsc_fd, iov.iov_base, iov.iov_len);
127 DPRINTF(("vtrnd: vtrnd_notify(): %d\r\n", len));
129 /* Catastrophe if unable to read from /dev/random */
133 * Release this chain and handle more
135 vq_relchain(vq, idx, len);
137 vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
142 pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
144 struct pci_vtrnd_softc *sc;
148 #ifndef WITHOUT_CAPSICUM
153 * Should always be able to open /dev/random.
155 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
159 #ifndef WITHOUT_CAPSICUM
160 cap_rights_init(&rights, CAP_READ);
161 if (cap_rights_limit(fd, &rights) == -1 && errno != ENOSYS)
162 errx(EX_OSERR, "Unable to apply rights for sandbox");
166 * Check that device is seeded and non-blocking.
168 len = read(fd, &v, sizeof(v));
170 WPRINTF(("vtrnd: /dev/random not ready, read(): %d", len));
175 sc = calloc(1, sizeof(struct pci_vtrnd_softc));
177 vi_softc_linkup(&sc->vrsc_vs, &vtrnd_vi_consts, sc, pi, &sc->vrsc_vq);
178 sc->vrsc_vs.vs_mtx = &sc->vrsc_mtx;
180 sc->vrsc_vq.vq_qsize = VTRND_RINGSZ;
182 /* keep /dev/random opened while emulating */
185 /* initialize config space */
186 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_RANDOM);
187 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
188 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_CRYPTO);
189 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_ENTROPY);
190 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
192 if (vi_intr_init(&sc->vrsc_vs, 1, fbsdrun_virtio_msix()))
194 vi_set_io_bar(&sc->vrsc_vs, 0);
200 struct pci_devemu pci_de_vrnd = {
201 .pe_emu = "virtio-rnd",
202 .pe_init = pci_vtrnd_init,
203 .pe_barwrite = vi_pci_write,
204 .pe_barread = vi_pci_read
206 PCI_EMUL_SET(pci_de_vrnd);