1 /* 2 * QEMU Builtin Random Number Generator Backend 3 * 4 * This work is licensed under the terms of the GNU GPL, version 2 or later. 5 * See the COPYING file in the top-level directory. 6 */ 7 8 #include "qemu/osdep.h" 9 #include "sysemu/rng.h" 10 #include "qemu/main-loop.h" 11 #include "qemu/guest-random.h" 12 #include "qom/object.h" 13 14 typedef struct RngBuiltin RngBuiltin; 15 DECLARE_INSTANCE_CHECKER(RngBuiltin, RNG_BUILTIN, 16 TYPE_RNG_BUILTIN) 17 18 struct RngBuiltin { 19 RngBackend parent; 20 QEMUBH *bh; 21 }; 22 23 static void rng_builtin_receive_entropy_bh(void *opaque) 24 { 25 RngBuiltin *s = opaque; 26 27 while (!QSIMPLEQ_EMPTY(&s->parent.requests)) { 28 RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests); 29 30 qemu_guest_getrandom_nofail(req->data, req->size); 31 32 req->receive_entropy(req->opaque, req->data, req->size); 33 34 rng_backend_finalize_request(&s->parent, req); 35 } 36 } 37 38 static void rng_builtin_request_entropy(RngBackend *b, RngRequest *req) 39 { 40 RngBuiltin *s = RNG_BUILTIN(b); 41 42 qemu_bh_schedule(s->bh); 43 } 44 45 static void rng_builtin_init(Object *obj) 46 { 47 RngBuiltin *s = RNG_BUILTIN(obj); 48 49 s->bh = qemu_bh_new(rng_builtin_receive_entropy_bh, s); 50 } 51 52 static void rng_builtin_finalize(Object *obj) 53 { 54 RngBuiltin *s = RNG_BUILTIN(obj); 55 56 qemu_bh_delete(s->bh); 57 } 58 59 static void rng_builtin_class_init(ObjectClass *klass, void *data) 60 { 61 RngBackendClass *rbc = RNG_BACKEND_CLASS(klass); 62 63 rbc->request_entropy = rng_builtin_request_entropy; 64 } 65 66 static const TypeInfo rng_builtin_info = { 67 .name = TYPE_RNG_BUILTIN, 68 .parent = TYPE_RNG_BACKEND, 69 .instance_size = sizeof(RngBuiltin), 70 .instance_init = rng_builtin_init, 71 .instance_finalize = rng_builtin_finalize, 72 .class_init = rng_builtin_class_init, 73 }; 74 75 static void register_types(void) 76 { 77 type_register_static(&rng_builtin_info); 78 } 79 80 type_init(register_types); 81