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