xref: /freebsd/usr.sbin/bhyve/pci_virtio_rnd.c (revision a35f04fba2ebb8f86d4cbdc710c89a094572b08e)
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 #ifndef WITHOUT_CAPSICUM
39 #include <sys/capsicum.h>
40 #endif
41 #include <sys/linker_set.h>
42 #include <sys/uio.h>
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <assert.h>
52 #include <pthread.h>
53 #include <sysexits.h>
54 
55 #include "bhyverun.h"
56 #include "pci_emul.h"
57 #include "virtio.h"
58 
59 #define VTRND_RINGSZ	64
60 
61 
62 static int pci_vtrnd_debug;
63 #define DPRINTF(params) if (pci_vtrnd_debug) printf params
64 #define WPRINTF(params) printf params
65 
66 /*
67  * Per-device softc
68  */
69 struct pci_vtrnd_softc {
70 	struct virtio_softc vrsc_vs;
71 	struct vqueue_info  vrsc_vq;
72 	pthread_mutex_t     vrsc_mtx;
73 	uint64_t            vrsc_cfg;
74 	int                 vrsc_fd;
75 };
76 
77 static void pci_vtrnd_reset(void *);
78 static void pci_vtrnd_notify(void *, struct vqueue_info *);
79 
80 static struct virtio_consts vtrnd_vi_consts = {
81 	"vtrnd",		/* our name */
82 	1,			/* we support 1 virtqueue */
83 	0,			/* config reg size */
84 	pci_vtrnd_reset,	/* reset */
85 	pci_vtrnd_notify,	/* device-wide qnotify */
86 	NULL,			/* read virtio config */
87 	NULL,			/* write virtio config */
88 	NULL,			/* apply negotiated features */
89 	0,			/* our capabilities */
90 };
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 !\n"));
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 	int len;
111 	uint16_t idx;
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 		vq_getchain(vq, &idx, &iov, 1, NULL);
122 
123 		len = read(sc->vrsc_fd, iov.iov_base, iov.iov_len);
124 
125 		DPRINTF(("vtrnd: vtrnd_notify(): %d\r\n", len));
126 
127 		/* Catastrophe if unable to read from /dev/random */
128 		assert(len > 0);
129 
130 		/*
131 		 * Release this chain and handle more
132 		 */
133 		vq_relchain(vq, idx, len);
134 	}
135 	vq_endchains(vq, 1);	/* Generate interrupt if appropriate. */
136 }
137 
138 
139 static int
140 pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
141 {
142 	struct pci_vtrnd_softc *sc;
143 	int fd;
144 	int len;
145 	uint8_t v;
146 #ifndef WITHOUT_CAPSICUM
147 	cap_rights_t rights;
148 #endif
149 
150 	/*
151 	 * Should always be able to open /dev/random.
152 	 */
153 	fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
154 
155 	assert(fd >= 0);
156 
157 #ifndef WITHOUT_CAPSICUM
158 	cap_rights_init(&rights, CAP_READ);
159 	if (cap_rights_limit(fd, &rights) == -1 && errno != ENOSYS)
160 		errx(EX_OSERR, "Unable to apply rights for sandbox");
161 #endif
162 
163 	/*
164 	 * Check that device is seeded and non-blocking.
165 	 */
166 	len = read(fd, &v, sizeof(v));
167 	if (len <= 0) {
168 		WPRINTF(("vtrnd: /dev/random not ready, read(): %d", len));
169 		return (1);
170 	}
171 
172 	sc = calloc(1, sizeof(struct pci_vtrnd_softc));
173 
174 	vi_softc_linkup(&sc->vrsc_vs, &vtrnd_vi_consts, sc, pi, &sc->vrsc_vq);
175 	sc->vrsc_vs.vs_mtx = &sc->vrsc_mtx;
176 
177 	sc->vrsc_vq.vq_qsize = VTRND_RINGSZ;
178 
179 	/* keep /dev/random opened while emulating */
180 	sc->vrsc_fd = fd;
181 
182 	/* initialize config space */
183 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_RANDOM);
184 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
185 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_CRYPTO);
186 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_ENTROPY);
187 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
188 
189 	if (vi_intr_init(&sc->vrsc_vs, 1, fbsdrun_virtio_msix()))
190 		return (1);
191 	vi_set_io_bar(&sc->vrsc_vs, 0);
192 
193 	return (0);
194 }
195 
196 
197 struct pci_devemu pci_de_vrnd = {
198 	.pe_emu =	"virtio-rnd",
199 	.pe_init =	pci_vtrnd_init,
200 	.pe_barwrite =	vi_pci_write,
201 	.pe_barread =	vi_pci_read
202 };
203 PCI_EMUL_SET(pci_de_vrnd);
204