1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/* $OpenBSD: viornd.c,v 1.4 2020/05/29 04:42:25 deraadt Exp $ */
/*
* Copyright (c) 2014 Stefan Fritsch <sf@sfritsch.de>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/timeout.h>
#include <machine/bus.h>
#include <sys/device.h>
#include <sys/pool.h>
#include <dev/pv/virtioreg.h>
#include <dev/pv/virtiovar.h>
/*
* The host may not have an unlimited supply of entropy. Therefore, we must
* not blindly get as much entropy as we can. Instead, we just request
* VIORND_BUFSIZE bytes at boot and every 15 * (1 << interval_shift) seconds.
* XXX There should be an API to check if we actually need more entropy.
*
* The lowest byte in the flags is used for transport specific settings.
* Therefore we use the second byte.
*/
#define VIORND_INTERVAL_SHIFT(f) ((f >> 8) & 0xf)
#define VIORND_INTERVAL_SHIFT_DEFAULT 5
#define VIORND_ONESHOT 0x1000
#define VIORND_BUFSIZE 16
#define VIORND_DEBUG 0
struct viornd_softc {
struct device sc_dev;
struct virtio_softc *sc_virtio;
struct virtqueue sc_vq;
int *sc_buf;
bus_dmamap_t sc_dmamap;
unsigned int sc_interval;
struct timeout sc_tick;
};
int viornd_match(struct device *, void *, void *);
void viornd_attach(struct device *, struct device *, void *);
int viornd_vq_done(struct virtqueue *);
void viornd_tick(void *);
struct cfattach viornd_ca = {
sizeof(struct viornd_softc),
viornd_match,
viornd_attach,
NULL
};
struct cfdriver viornd_cd = {
NULL, "viornd", DV_DULL
};
int viornd_match(struct device *parent, void *match, void *aux)
{
struct virtio_softc *va = aux;
if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_ENTROPY)
return 1;
return 0;
}
void
viornd_attach(struct device *parent, struct device *self, void *aux)
{
struct viornd_softc *sc = (struct viornd_softc *)self;
struct virtio_softc *vsc = (struct virtio_softc *)parent;
unsigned int shift;
vsc->sc_vqs = &sc->sc_vq;
vsc->sc_nvqs = 1;
vsc->sc_config_change = 0;
if (vsc->sc_child != NULL)
panic("already attached to something else");
vsc->sc_child = self;
vsc->sc_ipl = IPL_NET;
sc->sc_virtio = vsc;
virtio_negotiate_features(vsc, NULL);
if (sc->sc_dev.dv_cfdata->cf_flags & VIORND_ONESHOT) {
sc->sc_interval = 0;
} else {
shift = VIORND_INTERVAL_SHIFT(sc->sc_dev.dv_cfdata->cf_flags);
if (shift == 0)
shift = VIORND_INTERVAL_SHIFT_DEFAULT;
sc->sc_interval = 15 * (1 << shift);
}
#if VIORND_DEBUG
printf(": request interval: %us\n", sc->sc_interval);
#endif
sc->sc_buf = dma_alloc(VIORND_BUFSIZE, PR_NOWAIT|PR_ZERO);
if (sc->sc_buf == NULL) {
printf(": Can't alloc dma buffer\n");
goto err;
}
if (bus_dmamap_create(sc->sc_virtio->sc_dmat, VIORND_BUFSIZE, 1,
VIORND_BUFSIZE, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
&sc->sc_dmamap)) {
printf(": Can't alloc dmamap\n");
goto err;
}
if (bus_dmamap_load(sc->sc_virtio->sc_dmat, sc->sc_dmamap,
sc->sc_buf, VIORND_BUFSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_READ)) {
printf(": Can't load dmamap\n");
goto err2;
}
if (virtio_alloc_vq(vsc, &sc->sc_vq, 0, VIORND_BUFSIZE, 1,
"Entropy request") != 0) {
printf(": Can't alloc virtqueue\n");
goto err2;
}
sc->sc_vq.vq_done = viornd_vq_done;
virtio_start_vq_intr(vsc, &sc->sc_vq);
timeout_set(&sc->sc_tick, viornd_tick, sc);
timeout_add(&sc->sc_tick, 1);
printf("\n");
return;
err2:
bus_dmamap_destroy(vsc->sc_dmat, sc->sc_dmamap);
err:
vsc->sc_child = VIRTIO_CHILD_ERROR;
if (sc->sc_buf != NULL) {
dma_free(sc->sc_buf, VIORND_BUFSIZE);
sc->sc_buf = NULL;
}
return;
}
int
viornd_vq_done(struct virtqueue *vq)
{
struct virtio_softc *vsc = vq->vq_owner;
struct viornd_softc *sc = (struct viornd_softc *)vsc->sc_child;
int slot, len, i;
if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
return 0;
bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE,
BUS_DMASYNC_POSTREAD);
if (len > VIORND_BUFSIZE) {
printf("%s: inconsistent descriptor length %d > %d\n",
sc->sc_dev.dv_xname, len, VIORND_BUFSIZE);
goto out;
}
#if VIORND_DEBUG
printf("%s: got %d bytes of entropy\n", __func__, len);
#endif
for (i = 0; (i + 1) * sizeof(int) <= len; i++)
enqueue_randomness(sc->sc_buf[i]);
if (sc->sc_interval)
timeout_add_sec(&sc->sc_tick, sc->sc_interval);
out:
virtio_dequeue_commit(vq, slot);
return 1;
}
void
viornd_tick(void *arg)
{
struct viornd_softc *sc = arg;
struct virtio_softc *vsc = sc->sc_virtio;
struct virtqueue *vq = &sc->sc_vq;
int slot;
bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE,
BUS_DMASYNC_PREREAD);
if (virtio_enqueue_prep(vq, &slot) != 0 ||
virtio_enqueue_reserve(vq, slot, 1) != 0) {
panic("%s: virtqueue enqueue failed", sc->sc_dev.dv_xname);
}
virtio_enqueue(vq, slot, sc->sc_dmamap, 0);
virtio_enqueue_commit(vsc, vq, slot, 1);
}
|