]>
Commit | Line | Data |
---|---|---|
4d9392be TH |
1 | /* |
2 | * QEMU sPAPR random number generator "device" for H_RANDOM hypercall | |
3 | * | |
4 | * Copyright 2015 Thomas Huth, Red Hat Inc. | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
0d75590d | 20 | #include "qemu/osdep.h" |
da34e65c | 21 | #include "qapi/error.h" |
4771d756 PB |
22 | #include "qemu-common.h" |
23 | #include "cpu.h" | |
4d9392be TH |
24 | #include "qemu/error-report.h" |
25 | #include "sysemu/sysemu.h" | |
26 | #include "sysemu/device_tree.h" | |
27 | #include "sysemu/rng.h" | |
28 | #include "hw/ppc/spapr.h" | |
29 | #include "kvm_ppc.h" | |
30 | ||
31 | #define SPAPR_RNG(obj) \ | |
32 | OBJECT_CHECK(sPAPRRngState, (obj), TYPE_SPAPR_RNG) | |
33 | ||
34 | struct sPAPRRngState { | |
35 | /*< private >*/ | |
36 | DeviceState ds; | |
37 | RngBackend *backend; | |
38 | bool use_kvm; | |
39 | }; | |
40 | typedef struct sPAPRRngState sPAPRRngState; | |
41 | ||
42 | struct HRandomData { | |
43 | QemuSemaphore sem; | |
44 | union { | |
45 | uint64_t v64; | |
46 | uint8_t v8[8]; | |
47 | } val; | |
48 | int received; | |
49 | }; | |
50 | typedef struct HRandomData HRandomData; | |
51 | ||
52 | /* Callback function for the RngBackend */ | |
53 | static void random_recv(void *dest, const void *src, size_t size) | |
54 | { | |
55 | HRandomData *hrdp = dest; | |
56 | ||
57 | if (src && size > 0) { | |
58 | assert(size + hrdp->received <= sizeof(hrdp->val.v8)); | |
59 | memcpy(&hrdp->val.v8[hrdp->received], src, size); | |
60 | hrdp->received += size; | |
61 | } | |
62 | ||
63 | qemu_sem_post(&hrdp->sem); | |
64 | } | |
65 | ||
66 | /* Handler for the H_RANDOM hypercall */ | |
67 | static target_ulong h_random(PowerPCCPU *cpu, sPAPRMachineState *spapr, | |
68 | target_ulong opcode, target_ulong *args) | |
69 | { | |
70 | sPAPRRngState *rngstate; | |
71 | HRandomData hrdata; | |
72 | ||
73 | rngstate = SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)); | |
74 | ||
75 | if (!rngstate || !rngstate->backend) { | |
76 | return H_HARDWARE; | |
77 | } | |
78 | ||
79 | qemu_sem_init(&hrdata.sem, 0); | |
80 | hrdata.val.v64 = 0; | |
81 | hrdata.received = 0; | |
82 | ||
4d9392be TH |
83 | while (hrdata.received < 8) { |
84 | rng_backend_request_entropy(rngstate->backend, 8 - hrdata.received, | |
85 | random_recv, &hrdata); | |
f1a6cf3e | 86 | qemu_mutex_unlock_iothread(); |
4d9392be | 87 | qemu_sem_wait(&hrdata.sem); |
f1a6cf3e | 88 | qemu_mutex_lock_iothread(); |
4d9392be | 89 | } |
4d9392be TH |
90 | |
91 | qemu_sem_destroy(&hrdata.sem); | |
92 | args[0] = hrdata.val.v64; | |
93 | ||
94 | return H_SUCCESS; | |
95 | } | |
96 | ||
97 | static void spapr_rng_instance_init(Object *obj) | |
98 | { | |
4d9392be TH |
99 | if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL) != NULL) { |
100 | error_report("spapr-rng can not be instantiated twice!"); | |
101 | return; | |
102 | } | |
103 | ||
4d9392be TH |
104 | object_property_set_description(obj, "rng", |
105 | "ID of the random number generator backend", | |
106 | NULL); | |
107 | } | |
108 | ||
109 | static void spapr_rng_realize(DeviceState *dev, Error **errp) | |
110 | { | |
111 | ||
112 | sPAPRRngState *rngstate = SPAPR_RNG(dev); | |
113 | ||
114 | if (rngstate->use_kvm) { | |
115 | if (kvmppc_enable_hwrng() == 0) { | |
116 | return; | |
117 | } | |
118 | /* | |
119 | * If user specified both, use-kvm and a backend, we fall back to | |
120 | * the backend now. If not, provide an appropriate error message. | |
121 | */ | |
122 | if (!rngstate->backend) { | |
123 | error_setg(errp, "Could not initialize in-kernel H_RANDOM call!"); | |
124 | return; | |
125 | } | |
126 | } | |
127 | ||
128 | if (rngstate->backend) { | |
129 | spapr_register_hypercall(H_RANDOM, h_random); | |
130 | } else { | |
131 | error_setg(errp, "spapr-rng needs an RNG backend!"); | |
132 | } | |
133 | } | |
134 | ||
135 | int spapr_rng_populate_dt(void *fdt) | |
136 | { | |
137 | int node; | |
138 | int ret; | |
139 | ||
140 | node = qemu_fdt_add_subnode(fdt, "/ibm,platform-facilities"); | |
141 | if (node <= 0) { | |
142 | return -1; | |
143 | } | |
144 | ret = fdt_setprop_string(fdt, node, "device_type", | |
145 | "ibm,platform-facilities"); | |
146 | ret |= fdt_setprop_cell(fdt, node, "#address-cells", 0x1); | |
147 | ret |= fdt_setprop_cell(fdt, node, "#size-cells", 0x0); | |
148 | ||
149 | node = fdt_add_subnode(fdt, node, "ibm,random-v1"); | |
150 | if (node <= 0) { | |
151 | return -1; | |
152 | } | |
153 | ret |= fdt_setprop_string(fdt, node, "compatible", "ibm,random"); | |
154 | ||
155 | return ret ? -1 : 0; | |
156 | } | |
157 | ||
158 | static Property spapr_rng_properties[] = { | |
159 | DEFINE_PROP_BOOL("use-kvm", sPAPRRngState, use_kvm, false), | |
68c761e1 FZ |
160 | DEFINE_PROP_LINK("rng", sPAPRRngState, backend, TYPE_RNG_BACKEND, |
161 | RngBackend *), | |
4d9392be TH |
162 | DEFINE_PROP_END_OF_LIST(), |
163 | }; | |
164 | ||
165 | static void spapr_rng_class_init(ObjectClass *oc, void *data) | |
166 | { | |
167 | DeviceClass *dc = DEVICE_CLASS(oc); | |
168 | ||
169 | dc->realize = spapr_rng_realize; | |
170 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); | |
171 | dc->props = spapr_rng_properties; | |
3d0db3e7 | 172 | dc->hotpluggable = false; |
4d9392be TH |
173 | } |
174 | ||
175 | static const TypeInfo spapr_rng_info = { | |
176 | .name = TYPE_SPAPR_RNG, | |
177 | .parent = TYPE_DEVICE, | |
178 | .instance_size = sizeof(sPAPRRngState), | |
179 | .instance_init = spapr_rng_instance_init, | |
180 | .class_init = spapr_rng_class_init, | |
181 | }; | |
182 | ||
183 | static void spapr_rng_register_type(void) | |
184 | { | |
185 | type_register_static(&spapr_rng_info); | |
186 | } | |
187 | type_init(spapr_rng_register_type) |