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" 21da34e65cSMarkus Armbruster #include "qapi/error.h" 224771d756SPaolo Bonzini #include "cpu.h" 234d9392beSThomas Huth #include "qemu/error-report.h" 24db725815SMarkus Armbruster #include "qemu/main-loop.h" 250b8fa32fSMarkus Armbruster #include "qemu/module.h" 264d9392beSThomas Huth #include "sysemu/device_tree.h" 274d9392beSThomas Huth #include "sysemu/rng.h" 284d9392beSThomas Huth #include "hw/ppc/spapr.h" 29a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 304d9392beSThomas Huth #include "kvm_ppc.h" 31db1015e9SEduardo Habkost #include "qom/object.h" 324d9392beSThomas Huth 33*8063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(SpaprRngState, SPAPR_RNG) 344d9392beSThomas Huth 35ce2918cbSDavid Gibson struct SpaprRngState { 364d9392beSThomas Huth /*< private >*/ 374d9392beSThomas Huth DeviceState ds; 384d9392beSThomas Huth RngBackend *backend; 394d9392beSThomas Huth bool use_kvm; 404d9392beSThomas Huth }; 414d9392beSThomas Huth 424d9392beSThomas Huth struct HRandomData { 434d9392beSThomas Huth QemuSemaphore sem; 444d9392beSThomas Huth union { 454d9392beSThomas Huth uint64_t v64; 464d9392beSThomas Huth uint8_t v8[8]; 474d9392beSThomas Huth } val; 484d9392beSThomas Huth int received; 494d9392beSThomas Huth }; 504d9392beSThomas Huth typedef struct HRandomData HRandomData; 514d9392beSThomas Huth 524d9392beSThomas Huth /* Callback function for the RngBackend */ 534d9392beSThomas Huth static void random_recv(void *dest, const void *src, size_t size) 544d9392beSThomas Huth { 554d9392beSThomas Huth HRandomData *hrdp = dest; 564d9392beSThomas Huth 574d9392beSThomas Huth if (src && size > 0) { 584d9392beSThomas Huth assert(size + hrdp->received <= sizeof(hrdp->val.v8)); 594d9392beSThomas Huth memcpy(&hrdp->val.v8[hrdp->received], src, size); 604d9392beSThomas Huth hrdp->received += size; 614d9392beSThomas Huth } 624d9392beSThomas Huth 634d9392beSThomas Huth qemu_sem_post(&hrdp->sem); 644d9392beSThomas Huth } 654d9392beSThomas Huth 664d9392beSThomas Huth /* Handler for the H_RANDOM hypercall */ 67ce2918cbSDavid Gibson static target_ulong h_random(PowerPCCPU *cpu, SpaprMachineState *spapr, 684d9392beSThomas Huth target_ulong opcode, target_ulong *args) 694d9392beSThomas Huth { 70ce2918cbSDavid Gibson SpaprRngState *rngstate; 714d9392beSThomas Huth HRandomData hrdata; 724d9392beSThomas Huth 734d9392beSThomas Huth rngstate = SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)); 744d9392beSThomas Huth 754d9392beSThomas Huth if (!rngstate || !rngstate->backend) { 764d9392beSThomas Huth return H_HARDWARE; 774d9392beSThomas Huth } 784d9392beSThomas Huth 794d9392beSThomas Huth qemu_sem_init(&hrdata.sem, 0); 804d9392beSThomas Huth hrdata.val.v64 = 0; 814d9392beSThomas Huth hrdata.received = 0; 824d9392beSThomas Huth 834d9392beSThomas Huth while (hrdata.received < 8) { 844d9392beSThomas Huth rng_backend_request_entropy(rngstate->backend, 8 - hrdata.received, 854d9392beSThomas Huth random_recv, &hrdata); 86f1a6cf3eSGreg Kurz qemu_mutex_unlock_iothread(); 874d9392beSThomas Huth qemu_sem_wait(&hrdata.sem); 884d9392beSThomas Huth qemu_mutex_lock_iothread(); 89f1a6cf3eSGreg Kurz } 904d9392beSThomas Huth 914d9392beSThomas Huth qemu_sem_destroy(&hrdata.sem); 924d9392beSThomas Huth args[0] = hrdata.val.v64; 934d9392beSThomas Huth 944d9392beSThomas Huth return H_SUCCESS; 954d9392beSThomas Huth } 964d9392beSThomas Huth 974d9392beSThomas Huth static void spapr_rng_instance_init(Object *obj) 984d9392beSThomas Huth { 994d9392beSThomas Huth if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL) != NULL) { 1004d9392beSThomas Huth error_report("spapr-rng can not be instantiated twice!"); 1014d9392beSThomas Huth return; 1024d9392beSThomas Huth } 1034d9392beSThomas Huth 1044d9392beSThomas Huth object_property_set_description(obj, "rng", 1057eecec7dSMarkus Armbruster "ID of the random number generator backend"); 1064d9392beSThomas Huth } 1074d9392beSThomas Huth 1084d9392beSThomas Huth static void spapr_rng_realize(DeviceState *dev, Error **errp) 1094d9392beSThomas Huth { 1104d9392beSThomas Huth 111ce2918cbSDavid Gibson SpaprRngState *rngstate = SPAPR_RNG(dev); 1124d9392beSThomas Huth 1134d9392beSThomas Huth if (rngstate->use_kvm) { 1144d9392beSThomas Huth if (kvmppc_enable_hwrng() == 0) { 1154d9392beSThomas Huth return; 1164d9392beSThomas Huth } 1174d9392beSThomas Huth /* 1184d9392beSThomas Huth * If user specified both, use-kvm and a backend, we fall back to 1194d9392beSThomas Huth * the backend now. If not, provide an appropriate error message. 1204d9392beSThomas Huth */ 1214d9392beSThomas Huth if (!rngstate->backend) { 1224d9392beSThomas Huth error_setg(errp, "Could not initialize in-kernel H_RANDOM call!"); 1234d9392beSThomas Huth return; 1244d9392beSThomas Huth } 1254d9392beSThomas Huth } 1264d9392beSThomas Huth 1274d9392beSThomas Huth if (rngstate->backend) { 1284d9392beSThomas Huth spapr_register_hypercall(H_RANDOM, h_random); 1294d9392beSThomas Huth } else { 1304d9392beSThomas Huth error_setg(errp, "spapr-rng needs an RNG backend!"); 1314d9392beSThomas Huth } 1324d9392beSThomas Huth } 1334d9392beSThomas Huth 1344d9392beSThomas Huth static Property spapr_rng_properties[] = { 135ce2918cbSDavid Gibson DEFINE_PROP_BOOL("use-kvm", SpaprRngState, use_kvm, false), 136ce2918cbSDavid Gibson DEFINE_PROP_LINK("rng", SpaprRngState, backend, TYPE_RNG_BACKEND, 13768c761e1SFam Zheng RngBackend *), 1384d9392beSThomas Huth DEFINE_PROP_END_OF_LIST(), 1394d9392beSThomas Huth }; 1404d9392beSThomas Huth 1414d9392beSThomas Huth static void spapr_rng_class_init(ObjectClass *oc, void *data) 1424d9392beSThomas Huth { 1434d9392beSThomas Huth DeviceClass *dc = DEVICE_CLASS(oc); 1444d9392beSThomas Huth 1454d9392beSThomas Huth dc->realize = spapr_rng_realize; 1464d9392beSThomas Huth set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1474f67d30bSMarc-André Lureau device_class_set_props(dc, spapr_rng_properties); 1483d0db3e7SGreg Kurz dc->hotpluggable = false; 1494d9392beSThomas Huth } 1504d9392beSThomas Huth 1514d9392beSThomas Huth static const TypeInfo spapr_rng_info = { 1524d9392beSThomas Huth .name = TYPE_SPAPR_RNG, 1534d9392beSThomas Huth .parent = TYPE_DEVICE, 154ce2918cbSDavid Gibson .instance_size = sizeof(SpaprRngState), 1554d9392beSThomas Huth .instance_init = spapr_rng_instance_init, 1564d9392beSThomas Huth .class_init = spapr_rng_class_init, 1574d9392beSThomas Huth }; 1584d9392beSThomas Huth 1594d9392beSThomas Huth static void spapr_rng_register_type(void) 1604d9392beSThomas Huth { 1614d9392beSThomas Huth type_register_static(&spapr_rng_info); 1624d9392beSThomas Huth } 1634d9392beSThomas Huth type_init(spapr_rng_register_type) 164