2 * QEMU sPAPR random number generator "device" for H_RANDOM hypercall
4 * Copyright 2015 Thomas Huth, Red Hat Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
23 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
25 #include "qemu/module.h"
26 #include "sysemu/sysemu.h"
27 #include "sysemu/device_tree.h"
28 #include "sysemu/rng.h"
29 #include "hw/ppc/spapr.h"
30 #include "hw/qdev-properties.h"
33 #define SPAPR_RNG(obj) \
34 OBJECT_CHECK(SpaprRngState, (obj), TYPE_SPAPR_RNG)
36 struct SpaprRngState
{
42 typedef struct SpaprRngState SpaprRngState
;
52 typedef struct HRandomData HRandomData
;
54 /* Callback function for the RngBackend */
55 static void random_recv(void *dest
, const void *src
, size_t size
)
57 HRandomData
*hrdp
= dest
;
59 if (src
&& size
> 0) {
60 assert(size
+ hrdp
->received
<= sizeof(hrdp
->val
.v8
));
61 memcpy(&hrdp
->val
.v8
[hrdp
->received
], src
, size
);
62 hrdp
->received
+= size
;
65 qemu_sem_post(&hrdp
->sem
);
68 /* Handler for the H_RANDOM hypercall */
69 static target_ulong
h_random(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
70 target_ulong opcode
, target_ulong
*args
)
72 SpaprRngState
*rngstate
;
75 rngstate
= SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG
, NULL
));
77 if (!rngstate
|| !rngstate
->backend
) {
81 qemu_sem_init(&hrdata
.sem
, 0);
85 while (hrdata
.received
< 8) {
86 rng_backend_request_entropy(rngstate
->backend
, 8 - hrdata
.received
,
87 random_recv
, &hrdata
);
88 qemu_mutex_unlock_iothread();
89 qemu_sem_wait(&hrdata
.sem
);
90 qemu_mutex_lock_iothread();
93 qemu_sem_destroy(&hrdata
.sem
);
94 args
[0] = hrdata
.val
.v64
;
99 static void spapr_rng_instance_init(Object
*obj
)
101 if (object_resolve_path_type("", TYPE_SPAPR_RNG
, NULL
) != NULL
) {
102 error_report("spapr-rng can not be instantiated twice!");
106 object_property_set_description(obj
, "rng",
107 "ID of the random number generator backend",
111 static void spapr_rng_realize(DeviceState
*dev
, Error
**errp
)
114 SpaprRngState
*rngstate
= SPAPR_RNG(dev
);
116 if (rngstate
->use_kvm
) {
117 if (kvmppc_enable_hwrng() == 0) {
121 * If user specified both, use-kvm and a backend, we fall back to
122 * the backend now. If not, provide an appropriate error message.
124 if (!rngstate
->backend
) {
125 error_setg(errp
, "Could not initialize in-kernel H_RANDOM call!");
130 if (rngstate
->backend
) {
131 spapr_register_hypercall(H_RANDOM
, h_random
);
133 error_setg(errp
, "spapr-rng needs an RNG backend!");
137 static Property spapr_rng_properties
[] = {
138 DEFINE_PROP_BOOL("use-kvm", SpaprRngState
, use_kvm
, false),
139 DEFINE_PROP_LINK("rng", SpaprRngState
, backend
, TYPE_RNG_BACKEND
,
141 DEFINE_PROP_END_OF_LIST(),
144 static void spapr_rng_class_init(ObjectClass
*oc
, void *data
)
146 DeviceClass
*dc
= DEVICE_CLASS(oc
);
148 dc
->realize
= spapr_rng_realize
;
149 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
150 dc
->props
= spapr_rng_properties
;
151 dc
->hotpluggable
= false;
154 static const TypeInfo spapr_rng_info
= {
155 .name
= TYPE_SPAPR_RNG
,
156 .parent
= TYPE_DEVICE
,
157 .instance_size
= sizeof(SpaprRngState
),
158 .instance_init
= spapr_rng_instance_init
,
159 .class_init
= spapr_rng_class_init
,
162 static void spapr_rng_register_type(void)
164 type_register_static(&spapr_rng_info
);
166 type_init(spapr_rng_register_type
)