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