14d9392beSThomas Huth /* 24d9392beSThomas Huth * QEMU sPAPR random number generator "device" for H_RANDOM hypercall 34d9392beSThomas Huth * 44d9392beSThomas Huth * Copyright 2015 Thomas Huth, Red Hat Inc. 54d9392beSThomas Huth * 64d9392beSThomas Huth * This program is free software; you can redistribute it and/or modify 74d9392beSThomas Huth * it under the terms of the GNU General Public License as published by 84d9392beSThomas Huth * the Free Software Foundation; either version 2 of the License, 94d9392beSThomas Huth * or (at your option) any later version. 104d9392beSThomas Huth * 114d9392beSThomas Huth * This program is distributed in the hope that it will be useful, 124d9392beSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 134d9392beSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 144d9392beSThomas Huth * GNU General Public License for more details. 154d9392beSThomas Huth * 164d9392beSThomas Huth * You should have received a copy of the GNU General Public License 174d9392beSThomas Huth * along with this program; if not, see <http://www.gnu.org/licenses/>. 184d9392beSThomas Huth */ 194d9392beSThomas Huth 200d75590dSPeter Maydell #include "qemu/osdep.h" 214d9392beSThomas Huth #include "qemu/error-report.h" 224d9392beSThomas Huth #include "sysemu/sysemu.h" 234d9392beSThomas Huth #include "sysemu/device_tree.h" 244d9392beSThomas Huth #include "sysemu/rng.h" 254d9392beSThomas Huth #include "hw/ppc/spapr.h" 264d9392beSThomas Huth #include "kvm_ppc.h" 274d9392beSThomas Huth 284d9392beSThomas Huth #define SPAPR_RNG(obj) \ 294d9392beSThomas Huth OBJECT_CHECK(sPAPRRngState, (obj), TYPE_SPAPR_RNG) 304d9392beSThomas Huth 314d9392beSThomas Huth struct sPAPRRngState { 324d9392beSThomas Huth /*< private >*/ 334d9392beSThomas Huth DeviceState ds; 344d9392beSThomas Huth RngBackend *backend; 354d9392beSThomas Huth bool use_kvm; 364d9392beSThomas Huth }; 374d9392beSThomas Huth typedef struct sPAPRRngState sPAPRRngState; 384d9392beSThomas Huth 394d9392beSThomas Huth struct HRandomData { 404d9392beSThomas Huth QemuSemaphore sem; 414d9392beSThomas Huth union { 424d9392beSThomas Huth uint64_t v64; 434d9392beSThomas Huth uint8_t v8[8]; 444d9392beSThomas Huth } val; 454d9392beSThomas Huth int received; 464d9392beSThomas Huth }; 474d9392beSThomas Huth typedef struct HRandomData HRandomData; 484d9392beSThomas Huth 494d9392beSThomas Huth /* Callback function for the RngBackend */ 504d9392beSThomas Huth static void random_recv(void *dest, const void *src, size_t size) 514d9392beSThomas Huth { 524d9392beSThomas Huth HRandomData *hrdp = dest; 534d9392beSThomas Huth 544d9392beSThomas Huth if (src && size > 0) { 554d9392beSThomas Huth assert(size + hrdp->received <= sizeof(hrdp->val.v8)); 564d9392beSThomas Huth memcpy(&hrdp->val.v8[hrdp->received], src, size); 574d9392beSThomas Huth hrdp->received += size; 584d9392beSThomas Huth } 594d9392beSThomas Huth 604d9392beSThomas Huth qemu_sem_post(&hrdp->sem); 614d9392beSThomas Huth } 624d9392beSThomas Huth 634d9392beSThomas Huth /* Handler for the H_RANDOM hypercall */ 644d9392beSThomas Huth static target_ulong h_random(PowerPCCPU *cpu, sPAPRMachineState *spapr, 654d9392beSThomas Huth target_ulong opcode, target_ulong *args) 664d9392beSThomas Huth { 674d9392beSThomas Huth sPAPRRngState *rngstate; 684d9392beSThomas Huth HRandomData hrdata; 694d9392beSThomas Huth 704d9392beSThomas Huth rngstate = SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)); 714d9392beSThomas Huth 724d9392beSThomas Huth if (!rngstate || !rngstate->backend) { 734d9392beSThomas Huth return H_HARDWARE; 744d9392beSThomas Huth } 754d9392beSThomas Huth 764d9392beSThomas Huth qemu_sem_init(&hrdata.sem, 0); 774d9392beSThomas Huth hrdata.val.v64 = 0; 784d9392beSThomas Huth hrdata.received = 0; 794d9392beSThomas Huth 804d9392beSThomas Huth while (hrdata.received < 8) { 814d9392beSThomas Huth rng_backend_request_entropy(rngstate->backend, 8 - hrdata.received, 824d9392beSThomas Huth random_recv, &hrdata); 83*f1a6cf3eSGreg Kurz qemu_mutex_unlock_iothread(); 844d9392beSThomas Huth qemu_sem_wait(&hrdata.sem); 854d9392beSThomas Huth qemu_mutex_lock_iothread(); 86*f1a6cf3eSGreg Kurz } 874d9392beSThomas Huth 884d9392beSThomas Huth qemu_sem_destroy(&hrdata.sem); 894d9392beSThomas Huth args[0] = hrdata.val.v64; 904d9392beSThomas Huth 914d9392beSThomas Huth return H_SUCCESS; 924d9392beSThomas Huth } 934d9392beSThomas Huth 944d9392beSThomas Huth static void spapr_rng_instance_init(Object *obj) 954d9392beSThomas Huth { 964d9392beSThomas Huth sPAPRRngState *rngstate = SPAPR_RNG(obj); 974d9392beSThomas Huth 984d9392beSThomas Huth if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL) != NULL) { 994d9392beSThomas Huth error_report("spapr-rng can not be instantiated twice!"); 1004d9392beSThomas Huth return; 1014d9392beSThomas Huth } 1024d9392beSThomas Huth 1034d9392beSThomas Huth object_property_add_link(obj, "rng", TYPE_RNG_BACKEND, 1044d9392beSThomas Huth (Object **)&rngstate->backend, 1054d9392beSThomas Huth object_property_allow_set_link, 1064d9392beSThomas Huth OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL); 1074d9392beSThomas Huth object_property_set_description(obj, "rng", 1084d9392beSThomas Huth "ID of the random number generator backend", 1094d9392beSThomas Huth NULL); 1104d9392beSThomas Huth } 1114d9392beSThomas Huth 1124d9392beSThomas Huth static void spapr_rng_realize(DeviceState *dev, Error **errp) 1134d9392beSThomas Huth { 1144d9392beSThomas Huth 1154d9392beSThomas Huth sPAPRRngState *rngstate = SPAPR_RNG(dev); 1164d9392beSThomas Huth 1174d9392beSThomas Huth if (rngstate->use_kvm) { 1184d9392beSThomas Huth if (kvmppc_enable_hwrng() == 0) { 1194d9392beSThomas Huth return; 1204d9392beSThomas Huth } 1214d9392beSThomas Huth /* 1224d9392beSThomas Huth * If user specified both, use-kvm and a backend, we fall back to 1234d9392beSThomas Huth * the backend now. If not, provide an appropriate error message. 1244d9392beSThomas Huth */ 1254d9392beSThomas Huth if (!rngstate->backend) { 1264d9392beSThomas Huth error_setg(errp, "Could not initialize in-kernel H_RANDOM call!"); 1274d9392beSThomas Huth return; 1284d9392beSThomas Huth } 1294d9392beSThomas Huth } 1304d9392beSThomas Huth 1314d9392beSThomas Huth if (rngstate->backend) { 1324d9392beSThomas Huth spapr_register_hypercall(H_RANDOM, h_random); 1334d9392beSThomas Huth } else { 1344d9392beSThomas Huth error_setg(errp, "spapr-rng needs an RNG backend!"); 1354d9392beSThomas Huth } 1364d9392beSThomas Huth } 1374d9392beSThomas Huth 1384d9392beSThomas Huth int spapr_rng_populate_dt(void *fdt) 1394d9392beSThomas Huth { 1404d9392beSThomas Huth int node; 1414d9392beSThomas Huth int ret; 1424d9392beSThomas Huth 1434d9392beSThomas Huth node = qemu_fdt_add_subnode(fdt, "/ibm,platform-facilities"); 1444d9392beSThomas Huth if (node <= 0) { 1454d9392beSThomas Huth return -1; 1464d9392beSThomas Huth } 1474d9392beSThomas Huth ret = fdt_setprop_string(fdt, node, "device_type", 1484d9392beSThomas Huth "ibm,platform-facilities"); 1494d9392beSThomas Huth ret |= fdt_setprop_cell(fdt, node, "#address-cells", 0x1); 1504d9392beSThomas Huth ret |= fdt_setprop_cell(fdt, node, "#size-cells", 0x0); 1514d9392beSThomas Huth 1524d9392beSThomas Huth node = fdt_add_subnode(fdt, node, "ibm,random-v1"); 1534d9392beSThomas Huth if (node <= 0) { 1544d9392beSThomas Huth return -1; 1554d9392beSThomas Huth } 1564d9392beSThomas Huth ret |= fdt_setprop_string(fdt, node, "compatible", "ibm,random"); 1574d9392beSThomas Huth 1584d9392beSThomas Huth return ret ? -1 : 0; 1594d9392beSThomas Huth } 1604d9392beSThomas Huth 1614d9392beSThomas Huth static Property spapr_rng_properties[] = { 1624d9392beSThomas Huth DEFINE_PROP_BOOL("use-kvm", sPAPRRngState, use_kvm, false), 1634d9392beSThomas Huth DEFINE_PROP_END_OF_LIST(), 1644d9392beSThomas Huth }; 1654d9392beSThomas Huth 1664d9392beSThomas Huth static void spapr_rng_class_init(ObjectClass *oc, void *data) 1674d9392beSThomas Huth { 1684d9392beSThomas Huth DeviceClass *dc = DEVICE_CLASS(oc); 1694d9392beSThomas Huth 1704d9392beSThomas Huth dc->realize = spapr_rng_realize; 1714d9392beSThomas Huth set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1724d9392beSThomas Huth dc->props = spapr_rng_properties; 1733d0db3e7SGreg Kurz dc->hotpluggable = false; 1744d9392beSThomas Huth } 1754d9392beSThomas Huth 1764d9392beSThomas Huth static const TypeInfo spapr_rng_info = { 1774d9392beSThomas Huth .name = TYPE_SPAPR_RNG, 1784d9392beSThomas Huth .parent = TYPE_DEVICE, 1794d9392beSThomas Huth .instance_size = sizeof(sPAPRRngState), 1804d9392beSThomas Huth .instance_init = spapr_rng_instance_init, 1814d9392beSThomas Huth .class_init = spapr_rng_class_init, 1824d9392beSThomas Huth }; 1834d9392beSThomas Huth 1844d9392beSThomas Huth static void spapr_rng_register_type(void) 1854d9392beSThomas Huth { 1864d9392beSThomas Huth type_register_static(&spapr_rng_info); 1874d9392beSThomas Huth } 1884d9392beSThomas Huth type_init(spapr_rng_register_type) 189