xref: /freebsd/sys/dev/virtio/random/virtio_random.c (revision a3557ef0)
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/callout.h>
40 #include <sys/random.h>
41 #include <sys/stdatomic.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <sys/bus.h>
46 
47 #include <dev/random/randomdev.h>
48 #include <dev/random/random_harvestq.h>
49 #include <dev/virtio/virtio.h>
50 #include <dev/virtio/virtqueue.h>
51 
52 struct vtrnd_softc {
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 void	vtrnd_negotiate_features(device_t);
64 static int	vtrnd_alloc_virtqueue(device_t);
65 static int	vtrnd_harvest(struct vtrnd_softc *, void *, size_t *);
66 static unsigned	vtrnd_read(void *, unsigned);
67 
68 #define VTRND_FEATURES	0
69 
70 static struct virtio_feature_desc vtrnd_feature_desc[] = {
71 	{ 0, NULL }
72 };
73 
74 static struct random_source random_vtrnd = {
75 	.rs_ident = "VirtIO Entropy Adapter",
76 	.rs_source = RANDOM_PURE_VIRTIO,
77 	.rs_read = vtrnd_read,
78 };
79 
80 /* Kludge for API limitations of random(4). */
81 static _Atomic(struct vtrnd_softc *) g_vtrnd_softc;
82 
83 static device_method_t vtrnd_methods[] = {
84 	/* Device methods. */
85 	DEVMETHOD(device_probe,		vtrnd_probe),
86 	DEVMETHOD(device_attach,	vtrnd_attach),
87 	DEVMETHOD(device_detach,	vtrnd_detach),
88 
89 	DEVMETHOD_END
90 };
91 
92 static driver_t vtrnd_driver = {
93 	"vtrnd",
94 	vtrnd_methods,
95 	sizeof(struct vtrnd_softc)
96 };
97 static devclass_t vtrnd_devclass;
98 
99 DRIVER_MODULE(virtio_random, virtio_mmio, vtrnd_driver, vtrnd_devclass,
100     vtrnd_modevent, 0);
101 DRIVER_MODULE(virtio_random, virtio_pci, vtrnd_driver, vtrnd_devclass,
102     vtrnd_modevent, 0);
103 MODULE_VERSION(virtio_random, 1);
104 MODULE_DEPEND(virtio_random, virtio, 1, 1, 1);
105 MODULE_DEPEND(virtio_random, random_device, 1, 1, 1);
106 
107 VIRTIO_SIMPLE_PNPTABLE(virtio_random, VIRTIO_ID_ENTROPY,
108     "VirtIO Entropy Adapter");
109 VIRTIO_SIMPLE_PNPINFO(virtio_mmio, virtio_random);
110 VIRTIO_SIMPLE_PNPINFO(virtio_pci, virtio_random);
111 
112 static int
113 vtrnd_modevent(module_t mod, int type, void *unused)
114 {
115 	int error;
116 
117 	switch (type) {
118 	case MOD_LOAD:
119 	case MOD_QUIESCE:
120 	case MOD_UNLOAD:
121 	case MOD_SHUTDOWN:
122 		error = 0;
123 		break;
124 	default:
125 		error = EOPNOTSUPP;
126 		break;
127 	}
128 
129 	return (error);
130 }
131 
132 static int
133 vtrnd_probe(device_t dev)
134 {
135 	return (VIRTIO_SIMPLE_PROBE(dev, virtio_random));
136 }
137 
138 static int
139 vtrnd_attach(device_t dev)
140 {
141 	struct vtrnd_softc *sc, *exp;
142 	int error;
143 
144 	sc = device_get_softc(dev);
145 
146 	virtio_set_feature_desc(dev, vtrnd_feature_desc);
147 	vtrnd_negotiate_features(dev);
148 
149 	error = vtrnd_alloc_virtqueue(dev);
150 	if (error) {
151 		device_printf(dev, "cannot allocate virtqueue\n");
152 		goto fail;
153 	}
154 
155 	exp = NULL;
156 	if (!atomic_compare_exchange_strong_explicit(&g_vtrnd_softc, &exp, sc,
157 	    memory_order_release, memory_order_acquire)) {
158 		error = EEXIST;
159 		goto fail;
160 	}
161 	random_source_register(&random_vtrnd);
162 
163 fail:
164 	if (error)
165 		vtrnd_detach(dev);
166 
167 	return (error);
168 }
169 
170 static int
171 vtrnd_detach(device_t dev)
172 {
173 	struct vtrnd_softc *sc;
174 
175 	sc = device_get_softc(dev);
176 	KASSERT(
177 	    atomic_load_explicit(&g_vtrnd_softc, memory_order_acquire) == sc,
178 	    ("only one global instance at a time"));
179 
180 	random_source_deregister(&random_vtrnd);
181 	atomic_store_explicit(&g_vtrnd_softc, NULL, memory_order_release);
182 	return (0);
183 }
184 
185 static void
186 vtrnd_negotiate_features(device_t dev)
187 {
188 	struct vtrnd_softc *sc;
189 
190 	sc = device_get_softc(dev);
191 	sc->vtrnd_features = virtio_negotiate_features(dev, VTRND_FEATURES);
192 }
193 
194 static int
195 vtrnd_alloc_virtqueue(device_t dev)
196 {
197 	struct vtrnd_softc *sc;
198 	struct vq_alloc_info vq_info;
199 
200 	sc = device_get_softc(dev);
201 
202 	VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq,
203 	    "%s request", device_get_nameunit(dev));
204 
205 	return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
206 }
207 
208 static int
209 vtrnd_harvest(struct vtrnd_softc *sc, void *buf, size_t *sz)
210 {
211 	struct sglist_seg segs[1];
212 	struct sglist sg;
213 	struct virtqueue *vq;
214 	uint32_t value[HARVESTSIZE] __aligned(sizeof(uint32_t) * HARVESTSIZE);
215 	uint32_t rdlen;
216 	int error;
217 
218 	_Static_assert(sizeof(value) < PAGE_SIZE, "sglist assumption");
219 
220 	sglist_init(&sg, 1, segs);
221 	error = sglist_append(&sg, value, *sz);
222 	if (error != 0)
223 		panic("%s: sglist_append error=%d", __func__, error);
224 
225 	vq = sc->vtrnd_vq;
226 	KASSERT(virtqueue_empty(vq), ("%s: non-empty queue", __func__));
227 
228 	error = virtqueue_enqueue(vq, buf, &sg, 0, 1);
229 	if (error != 0)
230 		return (error);
231 
232 	/*
233 	 * Poll for the response, but the command is likely already
234 	 * done when we return from the notify.
235 	 */
236 	virtqueue_notify(vq);
237 	virtqueue_poll(vq, &rdlen);
238 
239 	if (rdlen > *sz)
240 		panic("%s: random device wrote %zu bytes beyond end of provided"
241 		    " buffer %p:%zu", __func__, (size_t)rdlen - *sz,
242 		    (void *)value, *sz);
243 	else if (rdlen == 0)
244 		return (EAGAIN);
245 	*sz = MIN(rdlen, *sz);
246 	memcpy(buf, value, *sz);
247 	explicit_bzero(value, *sz);
248 	return (0);
249 }
250 
251 static unsigned
252 vtrnd_read(void *buf, unsigned usz)
253 {
254 	struct vtrnd_softc *sc;
255 	size_t sz;
256 	int error;
257 
258 	sc = g_vtrnd_softc;
259 	if (sc == NULL)
260 		return (0);
261 
262 	sz = usz;
263 	error = vtrnd_harvest(sc, buf, &sz);
264 	if (error != 0)
265 		return (0);
266 
267 	return (sz);
268 }
269