xref: /freebsd/sys/dev/virtio/random/virtio_random.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013, Bryan Venteicher <bryanv@FreeBSD.org>
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 unmodified, this list of conditions, and the following
12  *    disclaimer.
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 ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* Driver for VirtIO entropy device. */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sglist.h>
39 #include <sys/random.h>
40 #include <sys/stdatomic.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/bus.h>
45 
46 #include <dev/random/randomdev.h>
47 #include <dev/random/random_harvestq.h>
48 #include <dev/virtio/virtio.h>
49 #include <dev/virtio/virtqueue.h>
50 
51 struct vtrnd_softc {
52 	device_t		 vtrnd_dev;
53 	uint64_t		 vtrnd_features;
54 	struct virtqueue	*vtrnd_vq;
55 };
56 
57 static int	vtrnd_modevent(module_t, int, void *);
58 
59 static int	vtrnd_probe(device_t);
60 static int	vtrnd_attach(device_t);
61 static int	vtrnd_detach(device_t);
62 
63 static int	vtrnd_negotiate_features(struct vtrnd_softc *);
64 static int	vtrnd_setup_features(struct vtrnd_softc *);
65 static int	vtrnd_alloc_virtqueue(struct vtrnd_softc *);
66 static int	vtrnd_harvest(struct vtrnd_softc *, void *, size_t *);
67 static unsigned	vtrnd_read(void *, unsigned);
68 
69 #define VTRND_FEATURES	0
70 
71 static struct virtio_feature_desc vtrnd_feature_desc[] = {
72 	{ 0, NULL }
73 };
74 
75 static struct random_source random_vtrnd = {
76 	.rs_ident = "VirtIO Entropy Adapter",
77 	.rs_source = RANDOM_PURE_VIRTIO,
78 	.rs_read = vtrnd_read,
79 };
80 
81 /* Kludge for API limitations of random(4). */
82 static _Atomic(struct vtrnd_softc *) g_vtrnd_softc;
83 
84 static device_method_t vtrnd_methods[] = {
85 	/* Device methods. */
86 	DEVMETHOD(device_probe,		vtrnd_probe),
87 	DEVMETHOD(device_attach,	vtrnd_attach),
88 	DEVMETHOD(device_detach,	vtrnd_detach),
89 
90 	DEVMETHOD_END
91 };
92 
93 static driver_t vtrnd_driver = {
94 	"vtrnd",
95 	vtrnd_methods,
96 	sizeof(struct vtrnd_softc)
97 };
98 static devclass_t vtrnd_devclass;
99 
100 VIRTIO_DRIVER_MODULE(virtio_random, vtrnd_driver, vtrnd_devclass,
101     vtrnd_modevent, 0);
102 MODULE_VERSION(virtio_random, 1);
103 MODULE_DEPEND(virtio_random, virtio, 1, 1, 1);
104 MODULE_DEPEND(virtio_random, random_device, 1, 1, 1);
105 
106 VIRTIO_SIMPLE_PNPINFO(virtio_random, VIRTIO_ID_ENTROPY,
107     "VirtIO Entropy Adapter");
108 
109 static int
110 vtrnd_modevent(module_t mod, int type, void *unused)
111 {
112 	int error;
113 
114 	switch (type) {
115 	case MOD_LOAD:
116 	case MOD_QUIESCE:
117 	case MOD_UNLOAD:
118 	case MOD_SHUTDOWN:
119 		error = 0;
120 		break;
121 	default:
122 		error = EOPNOTSUPP;
123 		break;
124 	}
125 
126 	return (error);
127 }
128 
129 static int
130 vtrnd_probe(device_t dev)
131 {
132 	return (VIRTIO_SIMPLE_PROBE(dev, virtio_random));
133 }
134 
135 static int
136 vtrnd_attach(device_t dev)
137 {
138 	struct vtrnd_softc *sc, *exp;
139 	int error;
140 
141 	sc = device_get_softc(dev);
142 	sc->vtrnd_dev = dev;
143 	virtio_set_feature_desc(dev, vtrnd_feature_desc);
144 
145 	error = vtrnd_setup_features(sc);
146 	if (error) {
147 		device_printf(dev, "cannot setup features\n");
148 		goto fail;
149 	}
150 
151 	error = vtrnd_alloc_virtqueue(sc);
152 	if (error) {
153 		device_printf(dev, "cannot allocate virtqueue\n");
154 		goto fail;
155 	}
156 
157 	exp = NULL;
158 	if (!atomic_compare_exchange_strong_explicit(&g_vtrnd_softc, &exp, sc,
159 	    memory_order_release, memory_order_acquire)) {
160 		error = EEXIST;
161 		goto fail;
162 	}
163 	random_source_register(&random_vtrnd);
164 
165 fail:
166 	if (error)
167 		vtrnd_detach(dev);
168 
169 	return (error);
170 }
171 
172 static int
173 vtrnd_detach(device_t dev)
174 {
175 	struct vtrnd_softc *sc;
176 
177 	sc = device_get_softc(dev);
178 	KASSERT(
179 	    atomic_load_explicit(&g_vtrnd_softc, memory_order_acquire) == sc,
180 	    ("only one global instance at a time"));
181 
182 	random_source_deregister(&random_vtrnd);
183 	atomic_store_explicit(&g_vtrnd_softc, NULL, memory_order_release);
184 	return (0);
185 }
186 
187 static int
188 vtrnd_negotiate_features(struct vtrnd_softc *sc)
189 {
190 	device_t dev;
191 	uint64_t features;
192 
193 	dev = sc->vtrnd_dev;
194 	features = VTRND_FEATURES;
195 
196 	sc->vtrnd_features = virtio_negotiate_features(dev, features);
197 	return (virtio_finalize_features(dev));
198 }
199 
200 static int
201 vtrnd_setup_features(struct vtrnd_softc *sc)
202 {
203 	int error;
204 
205 	error = vtrnd_negotiate_features(sc);
206 	if (error)
207 		return (error);
208 
209 	return (0);
210 }
211 
212 static int
213 vtrnd_alloc_virtqueue(struct vtrnd_softc *sc)
214 {
215 	device_t dev;
216 	struct vq_alloc_info vq_info;
217 
218 	dev = sc->vtrnd_dev;
219 
220 	VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq,
221 	    "%s request", device_get_nameunit(dev));
222 
223 	return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
224 }
225 
226 static int
227 vtrnd_harvest(struct vtrnd_softc *sc, void *buf, size_t *sz)
228 {
229 	struct sglist_seg segs[1];
230 	struct sglist sg;
231 	struct virtqueue *vq;
232 	uint32_t value[HARVESTSIZE] __aligned(sizeof(uint32_t) * HARVESTSIZE);
233 	uint32_t rdlen;
234 	int error;
235 
236 	_Static_assert(sizeof(value) < PAGE_SIZE, "sglist assumption");
237 
238 	sglist_init(&sg, 1, segs);
239 	error = sglist_append(&sg, value, *sz);
240 	if (error != 0)
241 		panic("%s: sglist_append error=%d", __func__, error);
242 
243 	vq = sc->vtrnd_vq;
244 	KASSERT(virtqueue_empty(vq), ("%s: non-empty queue", __func__));
245 
246 	error = virtqueue_enqueue(vq, buf, &sg, 0, 1);
247 	if (error != 0)
248 		return (error);
249 
250 	/*
251 	 * Poll for the response, but the command is likely already
252 	 * done when we return from the notify.
253 	 */
254 	virtqueue_notify(vq);
255 	virtqueue_poll(vq, &rdlen);
256 
257 	if (rdlen > *sz)
258 		panic("%s: random device wrote %zu bytes beyond end of provided"
259 		    " buffer %p:%zu", __func__, (size_t)rdlen - *sz,
260 		    (void *)value, *sz);
261 	else if (rdlen == 0)
262 		return (EAGAIN);
263 	*sz = MIN(rdlen, *sz);
264 	memcpy(buf, value, *sz);
265 	explicit_bzero(value, *sz);
266 	return (0);
267 }
268 
269 static unsigned
270 vtrnd_read(void *buf, unsigned usz)
271 {
272 	struct vtrnd_softc *sc;
273 	size_t sz;
274 	int error;
275 
276 	sc = g_vtrnd_softc;
277 	if (sc == NULL)
278 		return (0);
279 
280 	sz = usz;
281 	error = vtrnd_harvest(sc, buf, &sz);
282 	if (error != 0)
283 		return (0);
284 
285 	return (sz);
286 }
287