2 * QEMU Random Number Generator Backend
4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
13 #include "qemu/osdep.h"
14 #include "sysemu/rng.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qemu/module.h"
18 #include "qom/object_interfaces.h"
20 void rng_backend_request_entropy(RngBackend
*s
, size_t size
,
21 EntropyReceiveFunc
*receive_entropy
,
24 RngBackendClass
*k
= RNG_BACKEND_GET_CLASS(s
);
27 if (k
->request_entropy
) {
28 req
= g_malloc(sizeof(*req
));
32 req
->receive_entropy
= receive_entropy
;
34 req
->data
= g_malloc(req
->size
);
36 k
->request_entropy(s
, req
);
38 QSIMPLEQ_INSERT_TAIL(&s
->requests
, req
, next
);
42 static bool rng_backend_prop_get_opened(Object
*obj
, Error
**errp
)
44 RngBackend
*s
= RNG_BACKEND(obj
);
49 static void rng_backend_complete(UserCreatable
*uc
, Error
**errp
)
51 RngBackend
*s
= RNG_BACKEND(uc
);
52 RngBackendClass
*k
= RNG_BACKEND_GET_CLASS(s
);
53 Error
*local_err
= NULL
;
56 k
->opened(s
, &local_err
);
58 error_propagate(errp
, local_err
);
66 static void rng_backend_free_request(RngRequest
*req
)
72 static void rng_backend_free_requests(RngBackend
*s
)
74 RngRequest
*req
, *next
;
76 QSIMPLEQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
77 rng_backend_free_request(req
);
80 QSIMPLEQ_INIT(&s
->requests
);
83 void rng_backend_finalize_request(RngBackend
*s
, RngRequest
*req
)
85 QSIMPLEQ_REMOVE(&s
->requests
, req
, RngRequest
, next
);
86 rng_backend_free_request(req
);
89 static void rng_backend_init(Object
*obj
)
91 RngBackend
*s
= RNG_BACKEND(obj
);
93 QSIMPLEQ_INIT(&s
->requests
);
96 static void rng_backend_finalize(Object
*obj
)
98 RngBackend
*s
= RNG_BACKEND(obj
);
100 rng_backend_free_requests(s
);
103 static void rng_backend_class_init(ObjectClass
*oc
, void *data
)
105 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
107 ucc
->complete
= rng_backend_complete
;
109 object_class_property_add_bool(oc
, "opened",
110 rng_backend_prop_get_opened
,
114 static const TypeInfo rng_backend_info
= {
115 .name
= TYPE_RNG_BACKEND
,
116 .parent
= TYPE_OBJECT
,
117 .instance_size
= sizeof(RngBackend
),
118 .instance_init
= rng_backend_init
,
119 .instance_finalize
= rng_backend_finalize
,
120 .class_size
= sizeof(RngBackendClass
),
121 .class_init
= rng_backend_class_init
,
123 .interfaces
= (InterfaceInfo
[]) {
124 { TYPE_USER_CREATABLE
},
129 static void register_types(void)
131 type_register_static(&rng_backend_info
);
134 type_init(register_types
);