2012-06-25 18:13:31 +00:00
|
|
|
/*
|
|
|
|
* QEMU Random Number Generator Backend
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2012
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 17:49:54 +00:00
|
|
|
#include "qemu/osdep.h"
|
2013-04-08 14:55:25 +00:00
|
|
|
#include "sysemu/rng.h"
|
2017-01-26 14:26:44 +00:00
|
|
|
#include "chardev/char-fe.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 08:01:28 +00:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 17:19:43 +00:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2019-05-23 14:35:07 +00:00
|
|
|
#include "qemu/module.h"
|
2012-06-25 18:13:31 +00:00
|
|
|
|
|
|
|
#define TYPE_RNG_EGD "rng-egd"
|
|
|
|
#define RNG_EGD(obj) OBJECT_CHECK(RngEgd, (obj), TYPE_RNG_EGD)
|
|
|
|
|
|
|
|
typedef struct RngEgd
|
|
|
|
{
|
|
|
|
RngBackend parent;
|
|
|
|
|
2016-10-22 09:52:52 +00:00
|
|
|
CharBackend chr;
|
2012-06-25 18:13:31 +00:00
|
|
|
char *chr_name;
|
|
|
|
} RngEgd;
|
|
|
|
|
2016-03-03 08:37:18 +00:00
|
|
|
static void rng_egd_request_entropy(RngBackend *b, RngRequest *req)
|
2012-06-25 18:13:31 +00:00
|
|
|
{
|
|
|
|
RngEgd *s = RNG_EGD(b);
|
2016-03-03 08:37:18 +00:00
|
|
|
size_t size = req->size;
|
2012-06-25 18:13:31 +00:00
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
uint8_t header[2];
|
|
|
|
uint8_t len = MIN(size, 255);
|
|
|
|
|
|
|
|
/* synchronous entropy request */
|
|
|
|
header[0] = 0x02;
|
|
|
|
header[1] = len;
|
|
|
|
|
2016-09-06 13:56:04 +00:00
|
|
|
/* XXX this blocks entire thread. Rewrite to use
|
|
|
|
* qemu_chr_fe_write and background I/O callbacks */
|
2016-10-22 09:52:55 +00:00
|
|
|
qemu_chr_fe_write_all(&s->chr, header, sizeof(header));
|
2012-06-25 18:13:31 +00:00
|
|
|
|
|
|
|
size -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rng_egd_chr_can_read(void *opaque)
|
|
|
|
{
|
|
|
|
RngEgd *s = RNG_EGD(opaque);
|
2016-03-03 13:16:11 +00:00
|
|
|
RngRequest *req;
|
2012-06-25 18:13:31 +00:00
|
|
|
int size = 0;
|
|
|
|
|
2016-03-03 13:16:11 +00:00
|
|
|
QSIMPLEQ_FOREACH(req, &s->parent.requests, next) {
|
2012-06-25 18:13:31 +00:00
|
|
|
size += req->size - req->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
RngEgd *s = RNG_EGD(opaque);
|
2013-11-21 08:42:51 +00:00
|
|
|
size_t buf_offset = 0;
|
2012-06-25 18:13:31 +00:00
|
|
|
|
2016-03-03 13:16:11 +00:00
|
|
|
while (size > 0 && !QSIMPLEQ_EMPTY(&s->parent.requests)) {
|
|
|
|
RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
|
2012-06-25 18:13:31 +00:00
|
|
|
int len = MIN(size, req->size - req->offset);
|
|
|
|
|
2013-11-21 08:42:51 +00:00
|
|
|
memcpy(req->data + req->offset, buf + buf_offset, len);
|
|
|
|
buf_offset += len;
|
2012-06-25 18:13:31 +00:00
|
|
|
req->offset += len;
|
|
|
|
size -= len;
|
|
|
|
|
|
|
|
if (req->offset == req->size) {
|
|
|
|
req->receive_entropy(req->opaque, req->data, req->size);
|
|
|
|
|
2016-03-03 08:37:17 +00:00
|
|
|
rng_backend_finalize_request(&s->parent, req);
|
2012-06-25 18:13:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rng_egd_opened(RngBackend *b, Error **errp)
|
|
|
|
{
|
|
|
|
RngEgd *s = RNG_EGD(b);
|
2016-12-07 13:20:22 +00:00
|
|
|
Chardev *chr;
|
2012-06-25 18:13:31 +00:00
|
|
|
|
|
|
|
if (s->chr_name == NULL) {
|
2015-03-17 10:54:50 +00:00
|
|
|
error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
|
|
|
|
"chardev", "a valid character device");
|
2012-06-25 18:13:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-22 09:52:52 +00:00
|
|
|
chr = qemu_chr_find(s->chr_name);
|
|
|
|
if (chr == NULL) {
|
2015-03-16 07:57:47 +00:00
|
|
|
error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
|
|
|
|
"Device '%s' not found", s->chr_name);
|
2012-06-25 18:13:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-10-22 09:52:52 +00:00
|
|
|
if (!qemu_chr_fe_init(&s->chr, chr, errp)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-27 19:29:40 +00:00
|
|
|
|
2012-06-25 18:13:31 +00:00
|
|
|
/* FIXME we should resubmit pending requests when the CDS reconnects. */
|
2016-10-22 09:52:55 +00:00
|
|
|
qemu_chr_fe_set_handlers(&s->chr, rng_egd_chr_can_read,
|
2017-07-06 12:08:49 +00:00
|
|
|
rng_egd_chr_read, NULL, NULL, s, NULL, true);
|
2012-06-25 18:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
|
|
|
|
{
|
|
|
|
RngBackend *b = RNG_BACKEND(obj);
|
|
|
|
RngEgd *s = RNG_EGD(b);
|
|
|
|
|
|
|
|
if (b->opened) {
|
2015-03-17 10:54:50 +00:00
|
|
|
error_setg(errp, QERR_PERMISSION_DENIED);
|
2012-06-25 18:13:31 +00:00
|
|
|
} else {
|
2014-09-04 16:10:47 +00:00
|
|
|
g_free(s->chr_name);
|
2012-06-25 18:13:31 +00:00
|
|
|
s->chr_name = g_strdup(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *rng_egd_get_chardev(Object *obj, Error **errp)
|
|
|
|
{
|
|
|
|
RngEgd *s = RNG_EGD(obj);
|
2016-12-07 13:20:22 +00:00
|
|
|
Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
|
2012-06-25 18:13:31 +00:00
|
|
|
|
2016-10-22 09:52:55 +00:00
|
|
|
if (chr && chr->label) {
|
|
|
|
return g_strdup(chr->label);
|
2012-06-25 18:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rng_egd_init(Object *obj)
|
|
|
|
{
|
|
|
|
object_property_add_str(obj, "chardev",
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 15:29:22 +00:00
|
|
|
rng_egd_get_chardev, rng_egd_set_chardev);
|
2012-06-25 18:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rng_egd_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
RngEgd *s = RNG_EGD(obj);
|
|
|
|
|
2017-01-26 20:49:13 +00:00
|
|
|
qemu_chr_fe_deinit(&s->chr, false);
|
2012-06-25 18:13:31 +00:00
|
|
|
g_free(s->chr_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rng_egd_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
|
|
|
|
|
|
|
|
rbc->request_entropy = rng_egd_request_entropy;
|
|
|
|
rbc->opened = rng_egd_opened;
|
|
|
|
}
|
|
|
|
|
2013-01-10 15:19:07 +00:00
|
|
|
static const TypeInfo rng_egd_info = {
|
2012-06-25 18:13:31 +00:00
|
|
|
.name = TYPE_RNG_EGD,
|
|
|
|
.parent = TYPE_RNG_BACKEND,
|
|
|
|
.instance_size = sizeof(RngEgd),
|
|
|
|
.class_init = rng_egd_class_init,
|
|
|
|
.instance_init = rng_egd_init,
|
|
|
|
.instance_finalize = rng_egd_finalize,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&rng_egd_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|