xref: /openbmc/qemu/hw/ppc/spapr_rng.c (revision 7183128b)
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 
20 #include "qemu/error-report.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/device_tree.h"
23 #include "sysemu/rng.h"
24 #include "hw/ppc/spapr.h"
25 #include "kvm_ppc.h"
26 
27 #define SPAPR_RNG(obj) \
28     OBJECT_CHECK(sPAPRRngState, (obj), TYPE_SPAPR_RNG)
29 
30 struct sPAPRRngState {
31     /*< private >*/
32     DeviceState ds;
33     RngBackend *backend;
34     bool use_kvm;
35 };
36 typedef struct sPAPRRngState sPAPRRngState;
37 
38 struct HRandomData {
39     QemuSemaphore sem;
40     union {
41         uint64_t v64;
42         uint8_t v8[8];
43     } val;
44     int received;
45 };
46 typedef struct HRandomData HRandomData;
47 
48 /* Callback function for the RngBackend */
49 static void random_recv(void *dest, const void *src, size_t size)
50 {
51     HRandomData *hrdp = dest;
52 
53     if (src && size > 0) {
54         assert(size + hrdp->received <= sizeof(hrdp->val.v8));
55         memcpy(&hrdp->val.v8[hrdp->received], src, size);
56         hrdp->received += size;
57     }
58 
59     qemu_sem_post(&hrdp->sem);
60 }
61 
62 /* Handler for the H_RANDOM hypercall */
63 static target_ulong h_random(PowerPCCPU *cpu, sPAPRMachineState *spapr,
64                              target_ulong opcode, target_ulong *args)
65 {
66     sPAPRRngState *rngstate;
67     HRandomData hrdata;
68 
69     rngstate = SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG, NULL));
70 
71     if (!rngstate || !rngstate->backend) {
72         return H_HARDWARE;
73     }
74 
75     qemu_sem_init(&hrdata.sem, 0);
76     hrdata.val.v64 = 0;
77     hrdata.received = 0;
78 
79     qemu_mutex_unlock_iothread();
80     while (hrdata.received < 8) {
81         rng_backend_request_entropy(rngstate->backend, 8 - hrdata.received,
82                                     random_recv, &hrdata);
83         qemu_sem_wait(&hrdata.sem);
84     }
85     qemu_mutex_lock_iothread();
86 
87     qemu_sem_destroy(&hrdata.sem);
88     args[0] = hrdata.val.v64;
89 
90     return H_SUCCESS;
91 }
92 
93 static void spapr_rng_instance_init(Object *obj)
94 {
95     sPAPRRngState *rngstate = SPAPR_RNG(obj);
96 
97     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL) != NULL) {
98         error_report("spapr-rng can not be instantiated twice!");
99         return;
100     }
101 
102     object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
103                              (Object **)&rngstate->backend,
104                              object_property_allow_set_link,
105                              OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
106     object_property_set_description(obj, "rng",
107                                     "ID of the random number generator backend",
108                                     NULL);
109 }
110 
111 static void spapr_rng_realize(DeviceState *dev, Error **errp)
112 {
113 
114     sPAPRRngState *rngstate = SPAPR_RNG(dev);
115 
116     if (rngstate->use_kvm) {
117         if (kvmppc_enable_hwrng() == 0) {
118             return;
119         }
120         /*
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.
123          */
124         if (!rngstate->backend) {
125             error_setg(errp, "Could not initialize in-kernel H_RANDOM call!");
126             return;
127         }
128     }
129 
130     if (rngstate->backend) {
131         spapr_register_hypercall(H_RANDOM, h_random);
132     } else {
133         error_setg(errp, "spapr-rng needs an RNG backend!");
134     }
135 }
136 
137 int spapr_rng_populate_dt(void *fdt)
138 {
139     int node;
140     int ret;
141 
142     node = qemu_fdt_add_subnode(fdt, "/ibm,platform-facilities");
143     if (node <= 0) {
144         return -1;
145     }
146     ret = fdt_setprop_string(fdt, node, "device_type",
147                              "ibm,platform-facilities");
148     ret |= fdt_setprop_cell(fdt, node, "#address-cells", 0x1);
149     ret |= fdt_setprop_cell(fdt, node, "#size-cells", 0x0);
150 
151     node = fdt_add_subnode(fdt, node, "ibm,random-v1");
152     if (node <= 0) {
153         return -1;
154     }
155     ret |= fdt_setprop_string(fdt, node, "compatible", "ibm,random");
156 
157     return ret ? -1 : 0;
158 }
159 
160 static Property spapr_rng_properties[] = {
161     DEFINE_PROP_BOOL("use-kvm", sPAPRRngState, use_kvm, false),
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;
172 }
173 
174 static const TypeInfo spapr_rng_info = {
175     .name          = TYPE_SPAPR_RNG,
176     .parent        = TYPE_DEVICE,
177     .instance_size = sizeof(sPAPRRngState),
178     .instance_init = spapr_rng_instance_init,
179     .class_init    = spapr_rng_class_init,
180 };
181 
182 static void spapr_rng_register_type(void)
183 {
184     type_register_static(&spapr_rng_info);
185 }
186 type_init(spapr_rng_register_type)
187