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