]> git.proxmox.com Git - mirror_qemu.git/blame - backends/rng.c
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / backends / rng.c
CommitLineData
a9b7b2ad
AL
1/*
2 * QEMU Random Number Generator Backend
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
9c058332 13#include "qemu/osdep.h"
dccfcd0e 14#include "sysemu/rng.h"
da34e65c 15#include "qapi/error.h"
0b8fa32f 16#include "qemu/module.h"
269e09f3 17#include "qom/object_interfaces.h"
a9b7b2ad
AL
18
19void rng_backend_request_entropy(RngBackend *s, size_t size,
20 EntropyReceiveFunc *receive_entropy,
21 void *opaque)
22{
23 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
60253ed1 24 RngRequest *req;
a9b7b2ad
AL
25
26 if (k->request_entropy) {
60253ed1
LP
27 req = g_malloc(sizeof(*req));
28
29 req->offset = 0;
30 req->size = size;
31 req->receive_entropy = receive_entropy;
32 req->opaque = opaque;
33 req->data = g_malloc(req->size);
34
35 k->request_entropy(s, req);
36
443590c2 37 QSIMPLEQ_INSERT_TAIL(&s->requests, req, next);
a9b7b2ad
AL
38 }
39}
40
a9b7b2ad
AL
41static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
42{
43 RngBackend *s = RNG_BACKEND(obj);
44
45 return s->opened;
46}
47
57d3e1b3 48static void rng_backend_complete(UserCreatable *uc, Error **errp)
a9b7b2ad 49{
6e577937 50 RngBackend *s = RNG_BACKEND(uc);
a9b7b2ad 51 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
65cd9064 52 Error *local_err = NULL;
a9b7b2ad 53
a9b7b2ad 54 if (k->opened) {
65cd9064
MA
55 k->opened(s, &local_err);
56 if (local_err) {
57 error_propagate(errp, local_err);
58 return;
59 }
a9b7b2ad
AL
60 }
61
65cd9064 62 s->opened = true;
a9b7b2ad
AL
63}
64
9f14b0ad
LP
65static void rng_backend_free_request(RngRequest *req)
66{
67 g_free(req->data);
68 g_free(req);
69}
70
71static void rng_backend_free_requests(RngBackend *s)
72{
443590c2 73 RngRequest *req, *next;
9f14b0ad 74
443590c2
LP
75 QSIMPLEQ_FOREACH_SAFE(req, &s->requests, next, next) {
76 rng_backend_free_request(req);
9f14b0ad
LP
77 }
78
443590c2 79 QSIMPLEQ_INIT(&s->requests);
9f14b0ad
LP
80}
81
82void rng_backend_finalize_request(RngBackend *s, RngRequest *req)
83{
443590c2 84 QSIMPLEQ_REMOVE(&s->requests, req, RngRequest, next);
9f14b0ad
LP
85 rng_backend_free_request(req);
86}
87
a9b7b2ad
AL
88static void rng_backend_init(Object *obj)
89{
443590c2
LP
90 RngBackend *s = RNG_BACKEND(obj);
91
92 QSIMPLEQ_INIT(&s->requests);
a9b7b2ad
AL
93}
94
9f14b0ad
LP
95static void rng_backend_finalize(Object *obj)
96{
97 RngBackend *s = RNG_BACKEND(obj);
98
99 rng_backend_free_requests(s);
100}
101
57d3e1b3
IM
102static void rng_backend_class_init(ObjectClass *oc, void *data)
103{
104 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
105
106 ucc->complete = rng_backend_complete;
29ee2a18
EH
107
108 object_class_property_add_bool(oc, "opened",
109 rng_backend_prop_get_opened,
6e577937 110 NULL);
57d3e1b3
IM
111}
112
8c43a6f0 113static const TypeInfo rng_backend_info = {
a9b7b2ad
AL
114 .name = TYPE_RNG_BACKEND,
115 .parent = TYPE_OBJECT,
116 .instance_size = sizeof(RngBackend),
117 .instance_init = rng_backend_init,
9f14b0ad 118 .instance_finalize = rng_backend_finalize,
a9b7b2ad 119 .class_size = sizeof(RngBackendClass),
57d3e1b3 120 .class_init = rng_backend_class_init,
a9b7b2ad 121 .abstract = true,
269e09f3
IM
122 .interfaces = (InterfaceInfo[]) {
123 { TYPE_USER_CREATABLE },
124 { }
125 }
a9b7b2ad
AL
126};
127
128static void register_types(void)
129{
130 type_register_static(&rng_backend_info);
131}
132
133type_init(register_types);