diff options
-rw-r--r-- | backends/Makefile.objs | 2 | ||||
-rw-r--r-- | backends/rng-builtin.c | 77 | ||||
-rw-r--r-- | contrib/libvhost-user/libvhost-user.c | 19 | ||||
-rw-r--r-- | hw/core/machine.c | 4 | ||||
-rw-r--r-- | hw/virtio/virtio-pci.c | 10 | ||||
-rw-r--r-- | hw/virtio/virtio-pci.h | 4 | ||||
-rw-r--r-- | hw/virtio/virtio-rng.c | 19 | ||||
-rw-r--r-- | include/hw/virtio/virtio-rng.h | 2 | ||||
-rw-r--r-- | include/sysemu/rng.h | 2 | ||||
-rw-r--r-- | qemu-options.hx | 7 |
10 files changed, 122 insertions, 24 deletions
diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 981e8e122f..f0691116e8 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -1,4 +1,4 @@ -common-obj-y += rng.o rng-egd.o +common-obj-y += rng.o rng-egd.o rng-builtin.o common-obj-$(CONFIG_POSIX) += rng-random.o common-obj-$(CONFIG_TPM) += tpm.o diff --git a/backends/rng-builtin.c b/backends/rng-builtin.c new file mode 100644 index 0000000000..ba1b8d66b8 --- /dev/null +++ b/backends/rng-builtin.c @@ -0,0 +1,77 @@ +/* + * QEMU Builtin Random Number Generator Backend + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "sysemu/rng.h" +#include "qemu/main-loop.h" +#include "qemu/guest-random.h" + +#define RNG_BUILTIN(obj) OBJECT_CHECK(RngBuiltin, (obj), TYPE_RNG_BUILTIN) + +typedef struct RngBuiltin { + RngBackend parent; + QEMUBH *bh; +} RngBuiltin; + +static void rng_builtin_receive_entropy_bh(void *opaque) +{ + RngBuiltin *s = opaque; + + while (!QSIMPLEQ_EMPTY(&s->parent.requests)) { + RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests); + + qemu_guest_getrandom_nofail(req->data, req->size); + + req->receive_entropy(req->opaque, req->data, req->size); + + rng_backend_finalize_request(&s->parent, req); + } +} + +static void rng_builtin_request_entropy(RngBackend *b, RngRequest *req) +{ + RngBuiltin *s = RNG_BUILTIN(b); + + qemu_bh_schedule(s->bh); +} + +static void rng_builtin_init(Object *obj) +{ + RngBuiltin *s = RNG_BUILTIN(obj); + + s->bh = qemu_bh_new(rng_builtin_receive_entropy_bh, s); +} + +static void rng_builtin_finalize(Object *obj) +{ + RngBuiltin *s = RNG_BUILTIN(obj); + + qemu_bh_delete(s->bh); +} + +static void rng_builtin_class_init(ObjectClass *klass, void *data) +{ + RngBackendClass *rbc = RNG_BACKEND_CLASS(klass); + + rbc->request_entropy = rng_builtin_request_entropy; +} + +static const TypeInfo rng_builtin_info = { + .name = TYPE_RNG_BUILTIN, + .parent = TYPE_RNG_BACKEND, + .instance_size = sizeof(RngBuiltin), + .instance_init = rng_builtin_init, + .instance_finalize = rng_builtin_finalize, + .class_init = rng_builtin_class_init, +}; + +static void register_types(void) +{ + type_register_static(&rng_builtin_info); +} + +type_init(register_types); diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c index 4b36e35a82..68c27136ae 100644 --- a/contrib/libvhost-user/libvhost-user.c +++ b/contrib/libvhost-user/libvhost-user.c @@ -94,6 +94,11 @@ bool vu_has_feature(VuDev *dev, return has_feature(dev->features, fbit); } +static inline bool vu_has_protocol_feature(VuDev *dev, unsigned int fbit) +{ + return has_feature(dev->protocol_features, fbit); +} + static const char * vu_request_to_string(unsigned int req) { @@ -951,8 +956,7 @@ vu_check_queue_inflights(VuDev *dev, VuVirtq *vq) { int i = 0; - if (!has_feature(dev->protocol_features, - VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { + if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { return 0; } @@ -1097,7 +1101,7 @@ bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd, vmsg.fd_num = fd_num; - if ((dev->protocol_features & VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) == 0) { + if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) { return false; } @@ -2190,8 +2194,7 @@ vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned int idx, size_t sz) static int vu_queue_inflight_get(VuDev *dev, VuVirtq *vq, int desc_idx) { - if (!has_feature(dev->protocol_features, - VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { + if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { return 0; } @@ -2208,8 +2211,7 @@ vu_queue_inflight_get(VuDev *dev, VuVirtq *vq, int desc_idx) static int vu_queue_inflight_pre_put(VuDev *dev, VuVirtq *vq, int desc_idx) { - if (!has_feature(dev->protocol_features, - VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { + if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { return 0; } @@ -2225,8 +2227,7 @@ vu_queue_inflight_pre_put(VuDev *dev, VuVirtq *vq, int desc_idx) static int vu_queue_inflight_post_put(VuDev *dev, VuVirtq *vq, int desc_idx) { - if (!has_feature(dev->protocol_features, - VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { + if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { return 0; } diff --git a/hw/core/machine.c b/hw/core/machine.c index c5e0d52fbc..1689ad3bf8 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -27,7 +27,9 @@ #include "hw/pci/pci.h" #include "hw/mem/nvdimm.h" -GlobalProperty hw_compat_4_1[] = {}; +GlobalProperty hw_compat_4_1[] = { + { "virtio-pci", "x-pcie-flr-init", "off" }, +}; const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1); GlobalProperty hw_compat_4_0[] = { diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index ffb03728f9..c6b47a9c73 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -604,6 +604,10 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, pci_default_write_config(pci_dev, address, val, len); + if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) { + pcie_cap_flr_write_config(pci_dev, address, val, len); + } + if (range_covers_byte(address, len, PCI_COMMAND) && !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { virtio_pci_stop_ioeventfd(proxy); @@ -1780,6 +1784,10 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp) pcie_ats_init(pci_dev, 256); } + if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) { + /* Set Function Level Reset capability bit */ + pcie_cap_flr_init(pci_dev); + } } else { /* * make future invocations of pci_is_express() return false @@ -1847,6 +1855,8 @@ static Property virtio_pci_properties[] = { VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, true), DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_INIT_PM_BIT, true), + DEFINE_PROP_BIT("x-pcie-flr-init", VirtIOPCIProxy, flags, + VIRTIO_PCI_FLAG_INIT_FLR_BIT, true), DEFINE_PROP_END_OF_LIST(), }; diff --git a/hw/virtio/virtio-pci.h b/hw/virtio/virtio-pci.h index 292275acb1..e2eaaa9182 100644 --- a/hw/virtio/virtio-pci.h +++ b/hw/virtio/virtio-pci.h @@ -44,6 +44,7 @@ enum { VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, VIRTIO_PCI_FLAG_INIT_PM_BIT, + VIRTIO_PCI_FLAG_INIT_FLR_BIT, }; /* Need to activate work-arounds for buggy guests at vmstate load. */ @@ -80,6 +81,9 @@ enum { /* Init Power Management */ #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT) +/* Init Function Level Reset capability */ +#define VIRTIO_PCI_FLAG_INIT_FLR (1 << VIRTIO_PCI_FLAG_INIT_FLR_BIT) + typedef struct { MSIMessage msg; int virq; diff --git a/hw/virtio/virtio-rng.c b/hw/virtio/virtio-rng.c index d1650e1dee..e93bed020f 100644 --- a/hw/virtio/virtio-rng.c +++ b/hw/virtio/virtio-rng.c @@ -192,27 +192,24 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) } if (vrng->conf.rng == NULL) { - vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM)); + Object *default_backend = object_new(TYPE_RNG_BUILTIN); - user_creatable_complete(USER_CREATABLE(vrng->conf.default_backend), + user_creatable_complete(USER_CREATABLE(default_backend), &local_err); if (local_err) { error_propagate(errp, local_err); - object_unref(OBJECT(vrng->conf.default_backend)); + object_unref(default_backend); return; } - object_property_add_child(OBJECT(dev), - "default-backend", - OBJECT(vrng->conf.default_backend), - NULL); + object_property_add_child(OBJECT(dev), "default-backend", + default_backend, &error_abort); /* The child property took a reference, we can safely drop ours now */ - object_unref(OBJECT(vrng->conf.default_backend)); + object_unref(default_backend); - object_property_set_link(OBJECT(dev), - OBJECT(vrng->conf.default_backend), - "rng", NULL); + object_property_set_link(OBJECT(dev), default_backend, + "rng", &error_abort); } vrng->rng = vrng->conf.rng; diff --git a/include/hw/virtio/virtio-rng.h b/include/hw/virtio/virtio-rng.h index ff699335e3..bd05d734b8 100644 --- a/include/hw/virtio/virtio-rng.h +++ b/include/hw/virtio/virtio-rng.h @@ -14,7 +14,6 @@ #include "hw/virtio/virtio.h" #include "sysemu/rng.h" -#include "sysemu/rng-random.h" #include "standard-headers/linux/virtio_rng.h" #define TYPE_VIRTIO_RNG "virtio-rng-device" @@ -27,7 +26,6 @@ struct VirtIORNGConf { RngBackend *rng; uint64_t max_bytes; uint32_t period_ms; - RngRandom *default_backend; }; typedef struct VirtIORNG { diff --git a/include/sysemu/rng.h b/include/sysemu/rng.h index 9b22c156f8..fa6eada78c 100644 --- a/include/sysemu/rng.h +++ b/include/sysemu/rng.h @@ -24,6 +24,8 @@ #define RNG_BACKEND_CLASS(klass) \ OBJECT_CLASS_CHECK(RngBackendClass, (klass), TYPE_RNG_BACKEND) +#define TYPE_RNG_BUILTIN "rng-builtin" + typedef struct RngRequest RngRequest; typedef struct RngBackendClass RngBackendClass; typedef struct RngBackend RngBackend; diff --git a/qemu-options.hx b/qemu-options.hx index ea0638e92d..b2ba7c501d 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -4332,6 +4332,13 @@ other options. The @option{share} boolean option is @var{on} by default with memfd. +@item -object rng-builtin,id=@var{id} + +Creates a random number generator backend which obtains entropy from +QEMU builtin functions. The @option{id} parameter is a unique ID that +will be used to reference this entropy backend from the @option{virtio-rng} +device. By default, the @option{virtio-rng} device uses this RNG backend. + @item -object rng-random,id=@var{id},filename=@var{/dev/random} Creates a random number generator backend which obtains entropy from |