blob: 7cf31da071fcdf45ced9c1b012a563d9c60bd42c [file] [log] [blame]
Amit Shah16c915b2012-06-20 12:29:32 +05301/*
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
Peter Maydell9b8bfe22016-01-26 18:17:07 +000012#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010013#include "qapi/error.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010014#include "qemu/iov.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020015#include "qemu/module.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020016#include "qemu/timer.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010017#include "hw/virtio/virtio.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020018#include "hw/qdev-properties.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010019#include "hw/virtio/virtio-rng.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020020#include "sysemu/rng.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020021#include "sysemu/runstate.h"
Igor Mammedov57d3e1b2014-01-16 17:34:39 +010022#include "qom/object_interfaces.h"
Amit Shah4ac44582014-08-04 16:22:44 +053023#include "trace.h"
Amit Shah16c915b2012-06-20 12:29:32 +053024
Amit Shah16c915b2012-06-20 12:29:32 +053025static bool is_guest_ready(VirtIORNG *vrng)
26{
KONRAD Frederic611aa332013-04-24 10:08:01 +020027 VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
Amit Shah16c915b2012-06-20 12:29:32 +053028 if (virtio_queue_ready(vrng->vq)
KONRAD Frederic611aa332013-04-24 10:08:01 +020029 && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
Amit Shah16c915b2012-06-20 12:29:32 +053030 return true;
31 }
Amit Shah4ac44582014-08-04 16:22:44 +053032 trace_virtio_rng_guest_not_ready(vrng);
Amit Shah16c915b2012-06-20 12:29:32 +053033 return false;
34}
35
Michael S. Tsirkine1f7b482012-11-30 00:02:56 +020036static size_t get_request_size(VirtQueue *vq, unsigned quota)
Amit Shah16c915b2012-06-20 12:29:32 +053037{
Amit Shah14417032012-11-21 11:21:18 +053038 unsigned int in, out;
Amit Shah16c915b2012-06-20 12:29:32 +053039
Michael S. Tsirkine1f7b482012-11-30 00:02:56 +020040 virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
Amit Shah14417032012-11-21 11:21:18 +053041 return in;
Amit Shah16c915b2012-06-20 12:29:32 +053042}
43
Anthony Liguori904d6f52012-10-30 17:45:05 -050044static void virtio_rng_process(VirtIORNG *vrng);
45
Amit Shah16c915b2012-06-20 12:29:32 +053046/* Send data from a char device over to the guest */
47static void chr_read(void *opaque, const void *buf, size_t size)
48{
49 VirtIORNG *vrng = opaque;
KONRAD Frederic611aa332013-04-24 10:08:01 +020050 VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
Paolo Bonzini51b19eb2016-02-04 16:26:51 +020051 VirtQueueElement *elem;
Amit Shah16c915b2012-06-20 12:29:32 +053052 size_t len;
53 int offset;
54
55 if (!is_guest_ready(vrng)) {
56 return;
57 }
58
Laurent Viviera23a6d12017-04-12 15:53:12 +020059 /* we can't modify the virtqueue until
60 * our state is fully synced
61 */
62
63 if (!runstate_check(RUN_STATE_RUNNING)) {
64 trace_virtio_rng_cpu_is_stopped(vrng, size);
65 return;
66 }
67
Anthony Liguori904d6f52012-10-30 17:45:05 -050068 vrng->quota_remaining -= size;
69
Amit Shah16c915b2012-06-20 12:29:32 +053070 offset = 0;
71 while (offset < size) {
Paolo Bonzini51b19eb2016-02-04 16:26:51 +020072 elem = virtqueue_pop(vrng->vq, sizeof(VirtQueueElement));
73 if (!elem) {
Amit Shah16c915b2012-06-20 12:29:32 +053074 break;
75 }
Laurent Viviera23a6d12017-04-12 15:53:12 +020076 trace_virtio_rng_popped(vrng);
Paolo Bonzini51b19eb2016-02-04 16:26:51 +020077 len = iov_from_buf(elem->in_sg, elem->in_num,
Amit Shah16c915b2012-06-20 12:29:32 +053078 0, buf + offset, size - offset);
79 offset += len;
80
Paolo Bonzini51b19eb2016-02-04 16:26:51 +020081 virtqueue_push(vrng->vq, elem, len);
Amit Shah4ac44582014-08-04 16:22:44 +053082 trace_virtio_rng_pushed(vrng, len);
Paolo Bonzini51b19eb2016-02-04 16:26:51 +020083 g_free(elem);
Amit Shah16c915b2012-06-20 12:29:32 +053084 }
KONRAD Frederic611aa332013-04-24 10:08:01 +020085 virtio_notify(vdev, vrng->vq);
Ladi Prosekf8693c22016-03-03 10:48:34 +010086
87 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 virtio_rng_process(vrng);
92 }
Anthony Liguori904d6f52012-10-30 17:45:05 -050093}
94
95static void virtio_rng_process(VirtIORNG *vrng)
96{
Amit Shah14417032012-11-21 11:21:18 +053097 size_t size;
Michael S. Tsirkine1f7b482012-11-30 00:02:56 +020098 unsigned quota;
Anthony Liguori904d6f52012-10-30 17:45:05 -050099
100 if (!is_guest_ready(vrng)) {
101 return;
102 }
103
Pankaj Gupta621a20e2015-07-15 17:46:47 +0530104 if (vrng->activate_timer) {
105 timer_mod(vrng->rate_limit_timer,
106 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
107 vrng->activate_timer = false;
108 }
109
Michael S. Tsirkine1f7b482012-11-30 00:02:56 +0200110 if (vrng->quota_remaining < 0) {
111 quota = 0;
112 } else {
113 quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
114 }
115 size = get_request_size(vrng->vq, quota);
Amit Shah4ac44582014-08-04 16:22:44 +0530116
117 trace_virtio_rng_request(vrng, size, quota);
118
Anthony Liguori904d6f52012-10-30 17:45:05 -0500119 size = MIN(vrng->quota_remaining, size);
Amit Shah14417032012-11-21 11:21:18 +0530120 if (size) {
Amit Shah16c915b2012-06-20 12:29:32 +0530121 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
122 }
123}
124
125static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
126{
KONRAD Frederic611aa332013-04-24 10:08:01 +0200127 VirtIORNG *vrng = VIRTIO_RNG(vdev);
Anthony Liguori904d6f52012-10-30 17:45:05 -0500128 virtio_rng_process(vrng);
Amit Shah16c915b2012-06-20 12:29:32 +0530129}
130
Jason Wang9d5b7312015-07-27 17:49:19 +0800131static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp)
Amit Shah16c915b2012-06-20 12:29:32 +0530132{
133 return f;
134}
135
Philippe Mathieu-Daudé538f0492021-01-11 16:20:20 +0100136static void virtio_rng_vm_state_change(void *opaque, bool running,
Laurent Viviera23a6d12017-04-12 15:53:12 +0200137 RunState state)
Amit Shah16c915b2012-06-20 12:29:32 +0530138{
David Gibsondb124512014-11-27 16:48:10 +1100139 VirtIORNG *vrng = opaque;
Amit Shah16c915b2012-06-20 12:29:32 +0530140
Laurent Viviera23a6d12017-04-12 15:53:12 +0200141 trace_virtio_rng_vm_state_change(vrng, running, state);
Anthony Liguori904d6f52012-10-30 17:45:05 -0500142
Laurent Viviera23a6d12017-04-12 15:53:12 +0200143 /* 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 if (running && is_guest_ready(vrng)) {
149 virtio_rng_process(vrng);
150 }
Amit Shah16c915b2012-06-20 12:29:32 +0530151}
152
Anthony Liguori904d6f52012-10-30 17:45:05 -0500153static void check_rate_limit(void *opaque)
154{
KONRAD Frederic611aa332013-04-24 10:08:01 +0200155 VirtIORNG *vrng = opaque;
Anthony Liguori904d6f52012-10-30 17:45:05 -0500156
KONRAD Frederic611aa332013-04-24 10:08:01 +0200157 vrng->quota_remaining = vrng->conf.max_bytes;
158 virtio_rng_process(vrng);
Pankaj Gupta621a20e2015-07-15 17:46:47 +0530159 vrng->activate_timer = true;
Anthony Liguori904d6f52012-10-30 17:45:05 -0500160}
161
Pankaj Gupta5d9c9ea2018-06-27 16:55:20 +0530162static void virtio_rng_set_status(VirtIODevice *vdev, uint8_t status)
163{
164 VirtIORNG *vrng = VIRTIO_RNG(vdev);
165
166 if (!vdev->vm_running) {
167 return;
168 }
169 vdev->status = status;
170
171 /* Something changed, try to process buffers */
172 virtio_rng_process(vrng);
173}
174
Andreas Färbera8d57df2013-07-30 02:57:37 +0200175static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200176{
Andreas Färbera8d57df2013-07-30 02:57:37 +0200177 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
Andreas Färberaf7671f2013-07-30 05:12:47 +0200178 VirtIORNG *vrng = VIRTIO_RNG(dev);
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200179
Amit Shaha3a292c2014-12-11 13:17:42 +0530180 if (vrng->conf.period_ms <= 0) {
John Snowc617dd32014-07-29 19:28:58 -0400181 error_setg(errp, "'period' parameter expects a positive integer");
Andreas Färbera8d57df2013-07-30 02:57:37 +0200182 return;
Amos Kongd44bb862013-11-21 18:53:23 +0800183 }
184
John Snow1efd6e02014-07-29 19:28:57 -0400185 /* Workaround: Property parsing does not enforce unsigned integers,
186 * So this is a hack to reject such numbers. */
Michael S. Tsirkin024d0462024-07-24 06:48:59 -0400187 if (vrng->conf.max_bytes == 0 ||
188 vrng->conf.max_bytes > INT64_MAX) {
189 error_setg(errp, "'max-bytes' parameter must be positive, "
John Snowc617dd32014-07-29 19:28:58 -0400190 "and less than 2^63");
John Snow1efd6e02014-07-29 19:28:57 -0400191 return;
192 }
193
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200194 if (vrng->conf.rng == NULL) {
Laurent Vivier0198c262019-08-20 18:06:15 +0200195 Object *default_backend = object_new(TYPE_RNG_BUILTIN);
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200196
Markus Armbruster778a2dc2020-07-07 18:05:56 +0200197 if (!user_creatable_complete(USER_CREATABLE(default_backend),
Markus Armbruster668f62e2020-07-07 18:06:02 +0200198 errp)) {
Markus Armbruster5f7655f2019-08-20 18:06:14 +0200199 object_unref(default_backend);
Igor Mammedov57d3e1b2014-01-16 17:34:39 +0100200 return;
201 }
202
Markus Armbruster5f7655f2019-08-20 18:06:14 +0200203 object_property_add_child(OBJECT(dev), "default-backend",
Markus Armbrusterd2623122020-05-05 17:29:22 +0200204 default_backend);
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200205
Stefan Hajnocziabdffd12014-03-19 08:58:57 +0100206 /* The child property took a reference, we can safely drop ours now */
Markus Armbruster5f7655f2019-08-20 18:06:14 +0200207 object_unref(default_backend);
Stefan Hajnocziabdffd12014-03-19 08:58:57 +0100208
Markus Armbruster5325cc32020-07-07 18:05:54 +0200209 object_property_set_link(OBJECT(dev), "rng", default_backend,
210 &error_abort);
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200211 }
212
KONRAD Frederic46a5a892013-04-24 10:07:59 +0200213 vrng->rng = vrng->conf.rng;
214 if (vrng->rng == NULL) {
John Snowc617dd32014-07-29 19:28:58 -0400215 error_setg(errp, "'rng' parameter expects a valid object");
Andreas Färbera8d57df2013-07-30 02:57:37 +0200216 return;
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200217 }
KONRAD Frederic46a5a892013-04-24 10:07:59 +0200218
Jonah Palmer3857cd52022-04-01 09:23:18 -0400219 virtio_init(vdev, VIRTIO_ID_RNG, 0);
KONRAD Frederic46a5a892013-04-24 10:07:59 +0200220
John Snow1efd6e02014-07-29 19:28:57 -0400221 vrng->vq = virtio_add_queue(vdev, 8, handle_input);
KONRAD Frederic46a5a892013-04-24 10:07:59 +0200222 vrng->quota_remaining = vrng->conf.max_bytes;
Alex Blighbc72ad62013-08-21 16:03:08 +0100223 vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
KONRAD Frederic46a5a892013-04-24 10:07:59 +0200224 check_rate_limit, vrng);
Pankaj Gupta621a20e2015-07-15 17:46:47 +0530225 vrng->activate_timer = true;
Laurent Viviera23a6d12017-04-12 15:53:12 +0200226
227 vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change,
228 vrng);
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200229}
230
Markus Armbrusterb69c3c22020-05-05 17:29:24 +0200231static void virtio_rng_device_unrealize(DeviceState *dev)
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200232{
Andreas Färber306ec6c2013-07-30 03:50:44 +0200233 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
234 VirtIORNG *vrng = VIRTIO_RNG(dev);
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200235
Laurent Viviera23a6d12017-04-12 15:53:12 +0200236 qemu_del_vm_change_state_handler(vrng->vmstate);
Alex Blighbc72ad62013-08-21 16:03:08 +0100237 timer_free(vrng->rate_limit_timer);
Eugenio Pérez522bbb12019-10-25 10:35:23 +0200238 virtio_del_queue(vdev, 0);
KONRAD Frederic6a1a8cc2013-04-24 10:21:22 +0200239 virtio_cleanup(vdev);
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200240}
241
Halil Pasicb7de81f2016-10-06 14:55:48 +0200242static const VMStateDescription vmstate_virtio_rng = {
243 .name = "virtio-rng",
244 .minimum_version_id = 1,
245 .version_id = 1,
Richard Hendersonca02a172023-12-21 14:16:41 +1100246 .fields = (const VMStateField[]) {
Halil Pasicb7de81f2016-10-06 14:55:48 +0200247 VMSTATE_VIRTIO_DEVICE,
248 VMSTATE_END_OF_LIST()
249 },
Halil Pasicb7de81f2016-10-06 14:55:48 +0200250};
Dr. David Alan Gilbertb6075792016-07-14 18:22:48 +0100251
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200252static Property virtio_rng_properties[] = {
Shannon Zhaofe704802015-06-10 23:04:33 +0800253 /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
254 * you have an entropy source capable of generating more entropy than this
255 * and you can pass it through via virtio-rng, then hats off to you. Until
256 * then, this is unlimited for all practical purposes.
257 */
258 DEFINE_PROP_UINT64("max-bytes", VirtIORNG, conf.max_bytes, INT64_MAX),
259 DEFINE_PROP_UINT32("period", VirtIORNG, conf.period_ms, 1 << 16),
Fam Zhengd1fd7f72017-07-14 10:14:57 +0800260 DEFINE_PROP_LINK("rng", VirtIORNG, conf.rng, TYPE_RNG_BACKEND, RngBackend *),
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200261 DEFINE_PROP_END_OF_LIST(),
262};
263
264static void virtio_rng_class_init(ObjectClass *klass, void *data)
265{
266 DeviceClass *dc = DEVICE_CLASS(klass);
267 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
Andreas Färbera8d57df2013-07-30 02:57:37 +0200268
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400269 device_class_set_props(dc, virtio_rng_properties);
Dr. David Alan Gilbertb6075792016-07-14 18:22:48 +0100270 dc->vmsd = &vmstate_virtio_rng;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300271 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Andreas Färbera8d57df2013-07-30 02:57:37 +0200272 vdc->realize = virtio_rng_device_realize;
Andreas Färber306ec6c2013-07-30 03:50:44 +0200273 vdc->unrealize = virtio_rng_device_unrealize;
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200274 vdc->get_features = get_features;
Pankaj Gupta5d9c9ea2018-06-27 16:55:20 +0530275 vdc->set_status = virtio_rng_set_status;
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200276}
277
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200278static const TypeInfo virtio_rng_info = {
279 .name = TYPE_VIRTIO_RNG,
280 .parent = TYPE_VIRTIO_DEVICE,
281 .instance_size = sizeof(VirtIORNG),
KONRAD Frederic6eac8ae2013-04-24 10:07:55 +0200282 .class_init = virtio_rng_class_init,
283};
284
285static void virtio_register_types(void)
286{
287 type_register_static(&virtio_rng_info);
288}
289
290type_init(virtio_register_types)