xref: /freebsd/usr.sbin/bhyve/pci_virtio_rnd.c (revision c7046f76c2c027b00c0e6ba57cfd28f1a78f5e23)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2014 Nahanni Systems Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * virtio entropy device emulation.
32  * Randomness is sourced from /dev/random which does not block
33  * once it has been seeded at bootup.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #ifndef WITHOUT_CAPSICUM
41 #include <sys/capsicum.h>
42 #endif
43 #include <sys/linker_set.h>
44 #include <sys/uio.h>
45 
46 #ifndef WITHOUT_CAPSICUM
47 #include <capsicum_helpers.h>
48 #endif
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <assert.h>
57 #include <pthread.h>
58 #include <sysexits.h>
59 
60 #include "bhyverun.h"
61 #include "debug.h"
62 #include "pci_emul.h"
63 #include "virtio.h"
64 
65 #define VTRND_RINGSZ	64
66 
67 
68 static int pci_vtrnd_debug;
69 #define DPRINTF(params) if (pci_vtrnd_debug) PRINTLN params
70 #define WPRINTF(params) PRINTLN params
71 
72 /*
73  * Per-device softc
74  */
75 struct pci_vtrnd_softc {
76 	struct virtio_softc vrsc_vs;
77 	struct vqueue_info  vrsc_vq;
78 	pthread_mutex_t     vrsc_mtx;
79 	uint64_t            vrsc_cfg;
80 	int                 vrsc_fd;
81 };
82 
83 static void pci_vtrnd_reset(void *);
84 static void pci_vtrnd_notify(void *, struct vqueue_info *);
85 
86 static struct virtio_consts vtrnd_vi_consts = {
87 	"vtrnd",		/* our name */
88 	1,			/* we support 1 virtqueue */
89 	0,			/* config reg size */
90 	pci_vtrnd_reset,	/* reset */
91 	pci_vtrnd_notify,	/* device-wide qnotify */
92 	NULL,			/* read virtio config */
93 	NULL,			/* write virtio config */
94 	NULL,			/* apply negotiated features */
95 	0,			/* our capabilities */
96 };
97 
98 
99 static void
100 pci_vtrnd_reset(void *vsc)
101 {
102 	struct pci_vtrnd_softc *sc;
103 
104 	sc = vsc;
105 
106 	DPRINTF(("vtrnd: device reset requested !"));
107 	vi_reset_dev(&sc->vrsc_vs);
108 }
109 
110 
111 static void
112 pci_vtrnd_notify(void *vsc, struct vqueue_info *vq)
113 {
114 	struct iovec iov;
115 	struct pci_vtrnd_softc *sc;
116 	struct vi_req req;
117 	int len, n;
118 
119 	sc = vsc;
120 
121 	if (sc->vrsc_fd < 0) {
122 		vq_endchains(vq, 0);
123 		return;
124 	}
125 
126 	while (vq_has_descs(vq)) {
127 		n = vq_getchain(vq, &iov, 1, &req);
128 		assert(n == 1);
129 
130 		len = read(sc->vrsc_fd, iov.iov_base, iov.iov_len);
131 
132 		DPRINTF(("vtrnd: vtrnd_notify(): %d", len));
133 
134 		/* Catastrophe if unable to read from /dev/random */
135 		assert(len > 0);
136 
137 		/*
138 		 * Release this chain and handle more
139 		 */
140 		vq_relchain(vq, req.idx, len);
141 	}
142 	vq_endchains(vq, 1);	/* Generate interrupt if appropriate. */
143 }
144 
145 
146 static int
147 pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
148 {
149 	struct pci_vtrnd_softc *sc;
150 	int fd;
151 	int len;
152 	uint8_t v;
153 #ifndef WITHOUT_CAPSICUM
154 	cap_rights_t rights;
155 #endif
156 
157 	/*
158 	 * Should always be able to open /dev/random.
159 	 */
160 	fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
161 
162 	assert(fd >= 0);
163 
164 #ifndef WITHOUT_CAPSICUM
165 	cap_rights_init(&rights, CAP_READ);
166 	if (caph_rights_limit(fd, &rights) == -1)
167 		errx(EX_OSERR, "Unable to apply rights for sandbox");
168 #endif
169 
170 	/*
171 	 * Check that device is seeded and non-blocking.
172 	 */
173 	len = read(fd, &v, sizeof(v));
174 	if (len <= 0) {
175 		WPRINTF(("vtrnd: /dev/random not ready, read(): %d", len));
176 		close(fd);
177 		return (1);
178 	}
179 
180 	sc = calloc(1, sizeof(struct pci_vtrnd_softc));
181 
182 	pthread_mutex_init(&sc->vrsc_mtx, NULL);
183 
184 	vi_softc_linkup(&sc->vrsc_vs, &vtrnd_vi_consts, sc, pi, &sc->vrsc_vq);
185 	sc->vrsc_vs.vs_mtx = &sc->vrsc_mtx;
186 
187 	sc->vrsc_vq.vq_qsize = VTRND_RINGSZ;
188 
189 	/* keep /dev/random opened while emulating */
190 	sc->vrsc_fd = fd;
191 
192 	/* initialize config space */
193 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_RANDOM);
194 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
195 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_CRYPTO);
196 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_ENTROPY);
197 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
198 
199 	if (vi_intr_init(&sc->vrsc_vs, 1, fbsdrun_virtio_msix()))
200 		return (1);
201 	vi_set_io_bar(&sc->vrsc_vs, 0);
202 
203 	return (0);
204 }
205 
206 
207 static const struct pci_devemu pci_de_vrnd = {
208 	.pe_emu =	"virtio-rnd",
209 	.pe_init =	pci_vtrnd_init,
210 	.pe_barwrite =	vi_pci_write,
211 	.pe_barread =	vi_pci_read,
212 #ifdef BHYVE_SNAPSHOT
213 	.pe_snapshot =	vi_pci_snapshot,
214 #endif
215 };
216 PCI_EMUL_SET(pci_de_vrnd);
217