]>
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 | 22 | #include "cpu.h" |
4d9392be | 23 | #include "qemu/error-report.h" |
db725815 | 24 | #include "qemu/main-loop.h" |
0b8fa32f | 25 | #include "qemu/module.h" |
4d9392be TH |
26 | #include "sysemu/device_tree.h" |
27 | #include "sysemu/rng.h" | |
28 | #include "hw/ppc/spapr.h" | |
a27bd6c7 | 29 | #include "hw/qdev-properties.h" |
4d9392be TH |
30 | #include "kvm_ppc.h" |
31 | ||
32 | #define SPAPR_RNG(obj) \ | |
ce2918cb | 33 | OBJECT_CHECK(SpaprRngState, (obj), TYPE_SPAPR_RNG) |
4d9392be | 34 | |
ce2918cb | 35 | struct SpaprRngState { |
4d9392be TH |
36 | /*< private >*/ |
37 | DeviceState ds; | |
38 | RngBackend *backend; | |
39 | bool use_kvm; | |
40 | }; | |
ce2918cb | 41 | typedef struct SpaprRngState SpaprRngState; |
4d9392be TH |
42 | |
43 | struct HRandomData { | |
44 | QemuSemaphore sem; | |
45 | union { | |
46 | uint64_t v64; | |
47 | uint8_t v8[8]; | |
48 | } val; | |
49 | int received; | |
50 | }; | |
51 | typedef struct HRandomData HRandomData; | |
52 | ||
53 | /* Callback function for the RngBackend */ | |
54 | static void random_recv(void *dest, const void *src, size_t size) | |
55 | { | |
56 | HRandomData *hrdp = dest; | |
57 | ||
58 | if (src && size > 0) { | |
59 | assert(size + hrdp->received <= sizeof(hrdp->val.v8)); | |
60 | memcpy(&hrdp->val.v8[hrdp->received], src, size); | |
61 | hrdp->received += size; | |
62 | } | |
63 | ||
64 | qemu_sem_post(&hrdp->sem); | |
65 | } | |
66 | ||
67 | /* Handler for the H_RANDOM hypercall */ | |
ce2918cb | 68 | static target_ulong h_random(PowerPCCPU *cpu, SpaprMachineState *spapr, |
4d9392be TH |
69 | target_ulong opcode, target_ulong *args) |
70 | { | |
ce2918cb | 71 | SpaprRngState *rngstate; |
4d9392be TH |
72 | HRandomData hrdata; |
73 | ||
74 | rngstate = SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)); | |
75 | ||
76 | if (!rngstate || !rngstate->backend) { | |
77 | return H_HARDWARE; | |
78 | } | |
79 | ||
80 | qemu_sem_init(&hrdata.sem, 0); | |
81 | hrdata.val.v64 = 0; | |
82 | hrdata.received = 0; | |
83 | ||
4d9392be TH |
84 | while (hrdata.received < 8) { |
85 | rng_backend_request_entropy(rngstate->backend, 8 - hrdata.received, | |
86 | random_recv, &hrdata); | |
f1a6cf3e | 87 | qemu_mutex_unlock_iothread(); |
4d9392be | 88 | qemu_sem_wait(&hrdata.sem); |
f1a6cf3e | 89 | qemu_mutex_lock_iothread(); |
4d9392be | 90 | } |
4d9392be TH |
91 | |
92 | qemu_sem_destroy(&hrdata.sem); | |
93 | args[0] = hrdata.val.v64; | |
94 | ||
95 | return H_SUCCESS; | |
96 | } | |
97 | ||
98 | static void spapr_rng_instance_init(Object *obj) | |
99 | { | |
4d9392be TH |
100 | if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL) != NULL) { |
101 | error_report("spapr-rng can not be instantiated twice!"); | |
102 | return; | |
103 | } | |
104 | ||
4d9392be | 105 | object_property_set_description(obj, "rng", |
7eecec7d | 106 | "ID of the random number generator backend"); |
4d9392be TH |
107 | } |
108 | ||
109 | static void spapr_rng_realize(DeviceState *dev, Error **errp) | |
110 | { | |
111 | ||
ce2918cb | 112 | SpaprRngState *rngstate = SPAPR_RNG(dev); |
4d9392be TH |
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 | ||
4d9392be | 135 | static Property spapr_rng_properties[] = { |
ce2918cb DG |
136 | DEFINE_PROP_BOOL("use-kvm", SpaprRngState, use_kvm, false), |
137 | DEFINE_PROP_LINK("rng", SpaprRngState, backend, TYPE_RNG_BACKEND, | |
68c761e1 | 138 | RngBackend *), |
4d9392be TH |
139 | DEFINE_PROP_END_OF_LIST(), |
140 | }; | |
141 | ||
142 | static void spapr_rng_class_init(ObjectClass *oc, void *data) | |
143 | { | |
144 | DeviceClass *dc = DEVICE_CLASS(oc); | |
145 | ||
146 | dc->realize = spapr_rng_realize; | |
147 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); | |
4f67d30b | 148 | device_class_set_props(dc, spapr_rng_properties); |
3d0db3e7 | 149 | dc->hotpluggable = false; |
4d9392be TH |
150 | } |
151 | ||
152 | static const TypeInfo spapr_rng_info = { | |
153 | .name = TYPE_SPAPR_RNG, | |
154 | .parent = TYPE_DEVICE, | |
ce2918cb | 155 | .instance_size = sizeof(SpaprRngState), |
4d9392be TH |
156 | .instance_init = spapr_rng_instance_init, |
157 | .class_init = spapr_rng_class_init, | |
158 | }; | |
159 | ||
160 | static void spapr_rng_register_type(void) | |
161 | { | |
162 | type_register_static(&spapr_rng_info); | |
163 | } | |
164 | type_init(spapr_rng_register_type) |