-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathvirtio-rng.c
264 lines (238 loc) · 7.22 KB
/
virtio-rng.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "device.h"
#include "riscv.h"
#include "riscv_private.h"
#include "virtio.h"
#define VIRTIO_F_VERSION_1 1
#define VRNG_FEATURES_0 0
#define VRNG_FEATURES_1 1 /* VIRTIO_F_VERSION_1 */
#define VRNG_QUEUE_NUM_MAX 1024
#define VRNG_QUEUE (vrng->queues[vrng->QueueSel])
static int rng_fd = -1;
static void virtio_rng_set_fail(virtio_rng_state_t *vrng)
{
vrng->Status |= VIRTIO_STATUS__DEVICE_NEEDS_RESET;
if (vrng->Status & VIRTIO_STATUS__DRIVER_OK)
vrng->InterruptStatus |= VIRTIO_INT__CONF_CHANGE;
}
static inline uint32_t vrng_preprocess(virtio_rng_state_t *vrng, uint32_t addr)
{
if ((addr >= RAM_SIZE) || (addr & 0b11))
return virtio_rng_set_fail(vrng), 0;
return addr >> 2;
}
static void virtio_rng_update_status(virtio_rng_state_t *vrng, uint32_t status)
{
vrng->Status |= status;
if (status)
return;
/* Reset */
uint32_t *ram = vrng->ram;
memset(vrng, 0, sizeof(*vrng));
vrng->ram = ram;
}
static void virtio_queue_notify_handler(virtio_rng_state_t *vrng,
virtio_rng_queue_t *queue)
{
uint32_t *ram = vrng->ram;
/* Calculate available ring index */
uint16_t queue_idx = queue->last_avail % queue->QueueNum;
uint16_t buffer_idx =
ram[queue->QueueAvail + 1 + queue_idx / 2] >> (16 * (queue_idx % 2));
/* Update available ring pointer */
VRNG_QUEUE.last_avail++;
/* Read descriptor */
struct virtq_desc *vq_desc =
(struct virtq_desc *) &vrng->ram[queue->QueueDesc + buffer_idx * 4];
/* Write entropy buffer */
void *entropy_buf =
(void *) ((uintptr_t) vrng->ram + (uintptr_t) vq_desc->addr);
ssize_t total = read(rng_fd, entropy_buf, vq_desc->len);
/* Clear write flag */
vq_desc->flags = 0;
/* Get virtq_used.idx (le16) */
uint16_t used = ram[queue->QueueUsed] >> 16;
/* Update used ring information */
uint32_t vq_used_addr =
VRNG_QUEUE.QueueUsed + 1 + (used % queue->QueueNum) * 2;
ram[vq_used_addr] = buffer_idx;
ram[vq_used_addr + 1] = total;
used++;
/* Reset used ring flag to zero (virtq_used.flags) */
vrng->ram[VRNG_QUEUE.QueueUsed] &= MASK(16);
/* Update the used ring pointer (virtq_used.idx) */
vrng->ram[VRNG_QUEUE.QueueUsed] |= ((uint32_t) used) << 16;
/* Send interrupt, unless VIRTQ_AVAIL_F_NO_INTERRUPT is set */
if (!(ram[VRNG_QUEUE.QueueAvail] & 1))
vrng->InterruptStatus |= VIRTIO_INT__USED_RING;
}
static bool virtio_rng_reg_read(virtio_rng_state_t *vrng,
uint32_t addr,
uint32_t *value)
{
#define _(reg) VIRTIO_##reg
switch (addr) {
case _(MagicValue):
*value = 0x74726976;
return true;
case _(Version):
*value = 2;
return true;
case _(DeviceID):
*value = 4;
return true;
case _(VendorID):
*value = VIRTIO_VENDOR_ID;
return true;
case _(DeviceFeatures):
*value = vrng->DeviceFeaturesSel == 0
? VRNG_FEATURES_0
: (vrng->DeviceFeaturesSel == 1 ? VRNG_FEATURES_1 : 0);
return true;
case _(QueueNumMax):
*value = VRNG_QUEUE_NUM_MAX;
return true;
case _(QueueReady):
*value = VRNG_QUEUE.ready ? 1 : 0;
return true;
case _(InterruptStatus):
*value = vrng->InterruptStatus;
return true;
case _(Status):
*value = vrng->Status;
return true;
case _(ConfigGeneration):
*value = 0;
return true;
default:
/* No other readable registers */
return false;
}
#undef _
}
static bool virtio_rng_reg_write(virtio_rng_state_t *vrng,
uint32_t addr,
uint32_t value)
{
#define _(reg) VIRTIO_##reg
switch (addr) {
case _(DeviceFeaturesSel):
vrng->DeviceFeaturesSel = value;
return true;
case _(DriverFeatures):
vrng->DriverFeaturesSel == 0 ? (vrng->DriverFeatures = value) : 0;
return true;
case _(DriverFeaturesSel):
vrng->DriverFeaturesSel = value;
return true;
case _(QueueSel):
if (value < ARRAY_SIZE(vrng->queues))
vrng->QueueSel = value;
else
virtio_rng_set_fail(vrng);
return true;
case _(QueueNum):
if (value > 0 && value <= VRNG_QUEUE_NUM_MAX)
VRNG_QUEUE.QueueNum = value;
else
virtio_rng_set_fail(vrng);
return true;
case _(QueueReady):
VRNG_QUEUE.ready = value & 1;
if (value & 1)
VRNG_QUEUE.last_avail = vrng->ram[VRNG_QUEUE.QueueAvail] >> 16;
return true;
case _(QueueDescLow):
VRNG_QUEUE.QueueDesc = vrng_preprocess(vrng, value);
return true;
case _(QueueDescHigh):
if (value)
virtio_rng_set_fail(vrng);
return true;
case _(QueueDriverLow):
VRNG_QUEUE.QueueAvail = vrng_preprocess(vrng, value);
return true;
case _(QueueDriverHigh):
if (value)
virtio_rng_set_fail(vrng);
return true;
case _(QueueDeviceLow):
VRNG_QUEUE.QueueUsed = vrng_preprocess(vrng, value);
return true;
case _(QueueDeviceHigh):
if (value)
virtio_rng_set_fail(vrng);
return true;
case _(QueueNotify):
if (value < ARRAY_SIZE(vrng->queues))
virtio_queue_notify_handler(vrng, &VRNG_QUEUE);
else
virtio_rng_set_fail(vrng);
return true;
case _(InterruptACK):
vrng->InterruptStatus &= ~value;
return true;
case _(Status):
virtio_rng_update_status(vrng, value);
return true;
default:
/* No other writable registers */
return false;
}
#undef _
}
void virtio_rng_read(hart_t *vm,
virtio_rng_state_t *vrng,
uint32_t addr,
uint8_t width,
uint32_t *value)
{
switch (width) {
case RV_MEM_LW:
if (!virtio_rng_reg_read(vrng, addr >> 2, value))
vm_set_exception(vm, RV_EXC_LOAD_FAULT, vm->exc_val);
break;
case RV_MEM_LBU:
case RV_MEM_LB:
case RV_MEM_LHU:
case RV_MEM_LH:
vm_set_exception(vm, RV_EXC_LOAD_MISALIGN, vm->exc_val);
return;
default:
vm_set_exception(vm, RV_EXC_ILLEGAL_INSN, 0);
return;
}
}
void virtio_rng_write(hart_t *vm,
virtio_rng_state_t *vrng,
uint32_t addr,
uint8_t width,
uint32_t value)
{
switch (width) {
case RV_MEM_SW:
if (!virtio_rng_reg_write(vrng, addr >> 2, value))
vm_set_exception(vm, RV_EXC_STORE_FAULT, vm->exc_val);
break;
case RV_MEM_SB:
case RV_MEM_SH:
vm_set_exception(vm, RV_EXC_STORE_MISALIGN, vm->exc_val);
return;
default:
vm_set_exception(vm, RV_EXC_ILLEGAL_INSN, 0);
return;
}
}
void virtio_rng_init(void)
{
rng_fd = open("/dev/random", O_RDONLY);
if (rng_fd < 0) {
fprintf(stderr, "Could not open /dev/random\n");
exit(2);
}
}