xref: /freebsd/usr.sbin/bhyve/pci_virtio_rnd.c (revision ef0cb5db0af0d5d5b75b74f8e534fe601b7176d7)
1 /*-
2  * Copyright (c) 2014 Nahanni Systems Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * virtio entropy device emulation.
30  * Randomness is sourced from /dev/random which does not block
31  * once it has been seeded at bootup.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/linker_set.h>
39 #include <sys/uio.h>
40 
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <assert.h>
48 #include <pthread.h>
49 
50 #include "bhyverun.h"
51 #include "pci_emul.h"
52 #include "virtio.h"
53 
54 #define VTRND_RINGSZ	64
55 
56 
57 static int pci_vtrnd_debug;
58 #define DPRINTF(params) if (pci_vtrnd_debug) printf params
59 #define WPRINTF(params) printf params
60 
61 /*
62  * Per-device softc
63  */
64 struct pci_vtrnd_softc {
65 	struct virtio_softc vrsc_vs;
66 	struct vqueue_info  vrsc_vq;
67 	pthread_mutex_t     vrsc_mtx;
68 	uint64_t            vrsc_cfg;
69 	int                 vrsc_fd;
70 };
71 
72 static void pci_vtrnd_reset(void *);
73 static void pci_vtrnd_notify(void *, struct vqueue_info *);
74 
75 static struct virtio_consts vtrnd_vi_consts = {
76 	"vtrnd",		/* our name */
77 	1,			/* we support 1 virtqueue */
78 	0,			/* config reg size */
79 	pci_vtrnd_reset,	/* reset */
80 	pci_vtrnd_notify,	/* device-wide qnotify */
81 	NULL,			/* read virtio config */
82 	NULL,			/* write virtio config */
83 	NULL,			/* apply negotiated features */
84 	0,			/* our capabilities */
85 };
86 
87 
88 static void
89 pci_vtrnd_reset(void *vsc)
90 {
91 	struct pci_vtrnd_softc *sc;
92 
93 	sc = vsc;
94 
95 	DPRINTF(("vtrnd: device reset requested !\n"));
96 	vi_reset_dev(&sc->vrsc_vs);
97 }
98 
99 
100 static void
101 pci_vtrnd_notify(void *vsc, struct vqueue_info *vq)
102 {
103 	struct iovec iov;
104 	struct pci_vtrnd_softc *sc;
105 	int len;
106 	uint16_t idx;
107 
108 	sc = vsc;
109 
110 	if (sc->vrsc_fd < 0) {
111 		vq_endchains(vq, 0);
112 		return;
113 	}
114 
115 	while (vq_has_descs(vq)) {
116 		vq_getchain(vq, &idx, &iov, 1, NULL);
117 
118 		len = read(sc->vrsc_fd, iov.iov_base, iov.iov_len);
119 
120 		DPRINTF(("vtrnd: vtrnd_notify(): %d\r\n", len));
121 
122 		/* Catastrophe if unable to read from /dev/random */
123 		assert(len > 0);
124 
125 		/*
126 		 * Release this chain and handle more
127 		 */
128 		vq_relchain(vq, idx, len);
129 	}
130 	vq_endchains(vq, 1);	/* Generate interrupt if appropriate. */
131 }
132 
133 
134 static int
135 pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
136 {
137 	struct pci_vtrnd_softc *sc;
138 	int fd;
139 	int len;
140 	uint8_t v;
141 
142 	/*
143 	 * Should always be able to open /dev/random.
144 	 */
145 	fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
146 
147 	assert(fd >= 0);
148 
149 	/*
150 	 * Check that device is seeded and non-blocking.
151 	 */
152 	len = read(fd, &v, sizeof(v));
153 	if (len <= 0) {
154 		WPRINTF(("vtrnd: /dev/random not ready, read(): %d", len));
155 		return (1);
156 	}
157 
158 	sc = calloc(1, sizeof(struct pci_vtrnd_softc));
159 
160 	vi_softc_linkup(&sc->vrsc_vs, &vtrnd_vi_consts, sc, pi, &sc->vrsc_vq);
161 	sc->vrsc_vs.vs_mtx = &sc->vrsc_mtx;
162 
163 	sc->vrsc_vq.vq_qsize = VTRND_RINGSZ;
164 
165 	/* keep /dev/random opened while emulating */
166 	sc->vrsc_fd = fd;
167 
168 	/* initialize config space */
169 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_RANDOM);
170 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
171 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_CRYPTO);
172 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_ENTROPY);
173 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
174 
175 	if (vi_intr_init(&sc->vrsc_vs, 1, fbsdrun_virtio_msix()))
176 		return (1);
177 	vi_set_io_bar(&sc->vrsc_vs, 0);
178 
179 	return (0);
180 }
181 
182 
183 struct pci_devemu pci_de_vrnd = {
184 	.pe_emu =	"virtio-rnd",
185 	.pe_init =	pci_vtrnd_init,
186 	.pe_barwrite =	vi_pci_write,
187 	.pe_barread =	vi_pci_read
188 };
189 PCI_EMUL_SET(pci_de_vrnd);
190