Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/virtio/virtio-rng.c
Line
Count
Source
1
/*
2
 * A virtio device implementing a hardware random number generator.
3
 *
4
 * Copyright 2012 Red Hat, Inc.
5
 * Copyright 2012 Amit Shah <amit.shah@redhat.com>
6
 *
7
 * This work is licensed under the terms of the GNU GPL, version 2 or
8
 * (at your option) any later version.  See the COPYING file in the
9
 * top-level directory.
10
 */
11
12
#include "qemu/osdep.h"
13
#include "qapi/error.h"
14
#include "qemu/iov.h"
15
#include "qemu/module.h"
16
#include "qemu/timer.h"
17
#include "hw/virtio/virtio.h"
18
#include "hw/core/qdev-properties.h"
19
#include "hw/virtio/virtio-rng.h"
20
#include "system/rng.h"
21
#include "system/runstate.h"
22
#include "qom/object_interfaces.h"
23
#include "trace.h"
24
25
static bool is_guest_ready(VirtIORNG *vrng)
26
2
{
27
2
    VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
28
2
    if (virtio_queue_ready(vrng->vq)
29
0
        && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
30
0
        return true;
31
0
    }
32
2
    trace_virtio_rng_guest_not_ready(vrng);
33
2
    return false;
34
2
}
35
36
static size_t get_request_size(VirtQueue *vq, unsigned quota)
37
0
{
38
0
    unsigned int in, out;
39
40
0
    virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
41
0
    return in;
42
0
}
43
44
static void virtio_rng_process(VirtIORNG *vrng);
45
46
/* Send data from a char device over to the guest */
47
static void chr_read(void *opaque, const void *buf, size_t size)
48
0
{
49
0
    VirtIORNG *vrng = opaque;
50
0
    VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
51
0
    VirtQueueElement *elem;
52
0
    size_t len;
53
0
    int offset;
54
55
0
    if (!is_guest_ready(vrng)) {
56
0
        return;
57
0
    }
58
59
    /* we can't modify the virtqueue until
60
     * our state is fully synced
61
     */
62
63
0
    if (!runstate_check(RUN_STATE_RUNNING)) {
64
0
        trace_virtio_rng_cpu_is_stopped(vrng, size);
65
0
        return;
66
0
    }
67
68
0
    vrng->quota_remaining -= size;
69
70
0
    offset = 0;
71
0
    while (offset < size) {
72
0
        elem = virtqueue_pop(vrng->vq, sizeof(VirtQueueElement));
73
0
        if (!elem) {
74
0
            break;
75
0
        }
76
0
        trace_virtio_rng_popped(vrng);
77
0
        len = iov_from_buf(elem->in_sg, elem->in_num,
78
0
                           0, buf + offset, size - offset);
79
0
        offset += len;
80
81
0
        virtqueue_push(vrng->vq, elem, len);
82
0
        trace_virtio_rng_pushed(vrng, len);
83
0
        g_free(elem);
84
0
    }
85
0
    virtio_notify(vdev, vrng->vq);
86
87
0
    if (!virtio_queue_empty(vrng->vq)) {
88
        /* If we didn't drain the queue, call virtio_rng_process
89
         * to take care of asking for more data as appropriate.
90
         */
91
0
        virtio_rng_process(vrng);
92
0
    }
93
0
}
94
95
static void virtio_rng_process(VirtIORNG *vrng)
96
1
{
97
1
    size_t size;
98
1
    unsigned quota;
99
100
1
    if (!is_guest_ready(vrng)) {
101
1
        return;
102
1
    }
103
104
0
    if (vrng->activate_timer) {
105
0
        timer_mod(vrng->rate_limit_timer,
106
0
                  qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
107
0
        vrng->activate_timer = false;
108
0
    }
109
110
0
    if (vrng->quota_remaining < 0) {
111
0
        quota = 0;
112
0
    } else {
113
0
        quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
114
0
    }
115
0
    size = get_request_size(vrng->vq, quota);
116
117
0
    trace_virtio_rng_request(vrng, size, quota);
118
119
0
    size = MIN(vrng->quota_remaining, size);
120
0
    if (size) {
121
0
        rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
122
0
    }
123
0
}
124
125
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
126
0
{
127
0
    VirtIORNG *vrng = VIRTIO_RNG(vdev);
128
0
    virtio_rng_process(vrng);
129
0
}
130
131
static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp)
132
1
{
133
1
    return f;
134
1
}
135
136
static void virtio_rng_vm_state_change(void *opaque, bool running,
137
                                       RunState state)
138
1
{
139
1
    VirtIORNG *vrng = opaque;
140
141
1
    trace_virtio_rng_vm_state_change(vrng, running, state);
142
143
    /* We may have an element ready but couldn't process it due to a quota
144
     * limit or because CPU was stopped.  Make sure to try again when the
145
     * CPU restart.
146
     */
147
148
1
    if (running && is_guest_ready(vrng)) {
149
0
        virtio_rng_process(vrng);
150
0
    }
151
1
}
152
153
static void check_rate_limit(void *opaque)
154
0
{
155
0
    VirtIORNG *vrng = opaque;
156
157
0
    vrng->quota_remaining = vrng->conf.max_bytes;
158
0
    virtio_rng_process(vrng);
159
0
    vrng->activate_timer = true;
160
0
}
161
162
static int virtio_rng_set_status(VirtIODevice *vdev, uint8_t status)
163
2
{
164
2
    VirtIORNG *vrng = VIRTIO_RNG(vdev);
165
166
2
    if (!vdev->vm_running) {
167
1
        return 0;
168
1
    }
169
1
    vdev->status = status;
170
171
    /* Something changed, try to process buffers */
172
1
    virtio_rng_process(vrng);
173
1
    return 0;
174
2
}
175
176
static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
177
1
{
178
1
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
179
1
    VirtIORNG *vrng = VIRTIO_RNG(dev);
180
181
1
    if (vrng->conf.period_ms <= 0) {
182
0
        error_setg(errp, "'period' parameter expects a positive integer");
183
0
        return;
184
0
    }
185
186
    /* Workaround: Property parsing does not enforce unsigned integers,
187
     * So this is a hack to reject such numbers. */
188
1
    if (vrng->conf.max_bytes == 0 ||
189
1
        vrng->conf.max_bytes > INT64_MAX) {
190
0
        error_setg(errp, "'max-bytes' parameter must be positive, "
191
0
                   "and less than 2^63");
192
0
        return;
193
0
    }
194
195
1
    if (vrng->conf.rng == NULL) {
196
1
        Object *default_backend = object_new(TYPE_RNG_BUILTIN);
197
198
1
        if (!user_creatable_complete(USER_CREATABLE(default_backend),
199
1
                                     errp)) {
200
0
            object_unref(default_backend);
201
0
            return;
202
0
        }
203
204
1
        object_property_add_child(OBJECT(dev), "default-backend",
205
1
                                  default_backend);
206
207
        /* The child property took a reference, we can safely drop ours now */
208
1
        object_unref(default_backend);
209
210
1
        object_property_set_link(OBJECT(dev), "rng", default_backend,
211
1
                                 &error_abort);
212
1
    }
213
214
1
    vrng->rng = vrng->conf.rng;
215
1
    if (vrng->rng == NULL) {
216
0
        error_setg(errp, "'rng' parameter expects a valid object");
217
0
        return;
218
0
    }
219
220
1
    virtio_init(vdev, VIRTIO_ID_RNG, 0);
221
222
1
    vrng->vq = virtio_add_queue(vdev, 8, handle_input);
223
1
    vrng->quota_remaining = vrng->conf.max_bytes;
224
1
    vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
225
1
                                               check_rate_limit, vrng);
226
1
    vrng->activate_timer = true;
227
228
1
    vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change,
229
1
                                                     vrng);
230
1
}
231
232
static void virtio_rng_device_unrealize(DeviceState *dev)
233
0
{
234
0
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
235
0
    VirtIORNG *vrng = VIRTIO_RNG(dev);
236
237
0
    rng_backend_cancel_requests(vrng->rng, chr_read, vrng);
238
239
0
    qemu_del_vm_change_state_handler(vrng->vmstate);
240
0
    timer_free(vrng->rate_limit_timer);
241
0
    virtio_del_queue(vdev, 0);
242
0
    virtio_cleanup(vdev);
243
0
}
244
245
static const VMStateDescription vmstate_virtio_rng = {
246
    .name = "virtio-rng",
247
    .minimum_version_id = 1,
248
    .version_id = 1,
249
    .fields = (const VMStateField[]) {
250
        VMSTATE_VIRTIO_DEVICE,
251
        VMSTATE_END_OF_LIST()
252
    },
253
};
254
255
static const Property virtio_rng_properties[] = {
256
    /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s.  If
257
     * you have an entropy source capable of generating more entropy than this
258
     * and you can pass it through via virtio-rng, then hats off to you.  Until
259
     * then, this is unlimited for all practical purposes.
260
     */
261
    DEFINE_PROP_UINT64("max-bytes", VirtIORNG, conf.max_bytes, INT64_MAX),
262
    DEFINE_PROP_UINT32("period", VirtIORNG, conf.period_ms, 1 << 16),
263
    DEFINE_PROP_LINK("rng", VirtIORNG, conf.rng, TYPE_RNG_BACKEND, RngBackend *),
264
};
265
266
static void virtio_rng_class_init(ObjectClass *klass, const void *data)
267
63
{
268
63
    DeviceClass *dc = DEVICE_CLASS(klass);
269
63
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
270
271
63
    device_class_set_props(dc, virtio_rng_properties);
272
63
    dc->vmsd = &vmstate_virtio_rng;
273
63
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
274
63
    vdc->realize = virtio_rng_device_realize;
275
63
    vdc->unrealize = virtio_rng_device_unrealize;
276
63
    vdc->get_features = get_features;
277
63
    vdc->set_status = virtio_rng_set_status;
278
63
}
279
280
static const TypeInfo virtio_rng_info = {
281
    .name = TYPE_VIRTIO_RNG,
282
    .parent = TYPE_VIRTIO_DEVICE,
283
    .instance_size = sizeof(VirtIORNG),
284
    .class_init = virtio_rng_class_init,
285
    .secure = true,
286
};
287
288
static void virtio_register_types(void)
289
63
{
290
63
    type_register_static(&virtio_rng_info);
291
63
}
292
293
type_init(virtio_register_types)