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"
224d9392beSThomas Huth #include "qemu/error-report.h"
23db725815SMarkus Armbruster #include "qemu/main-loop.h"
240b8fa32fSMarkus Armbruster #include "qemu/module.h"
254d9392beSThomas Huth #include "sysemu/device_tree.h"
264d9392beSThomas Huth #include "sysemu/rng.h"
274d9392beSThomas Huth #include "hw/ppc/spapr.h"
28a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
294d9392beSThomas Huth #include "kvm_ppc.h"
30db1015e9SEduardo Habkost #include "qom/object.h"
314d9392beSThomas Huth
328063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(SpaprRngState, SPAPR_RNG)
334d9392beSThomas Huth
34ce2918cbSDavid Gibson struct SpaprRngState {
354d9392beSThomas Huth /*< private >*/
364d9392beSThomas Huth DeviceState ds;
374d9392beSThomas Huth RngBackend *backend;
384d9392beSThomas Huth bool use_kvm;
394d9392beSThomas Huth };
404d9392beSThomas Huth
414d9392beSThomas Huth struct HRandomData {
424d9392beSThomas Huth QemuSemaphore sem;
434d9392beSThomas Huth union {
444d9392beSThomas Huth uint64_t v64;
454d9392beSThomas Huth uint8_t v8[8];
464d9392beSThomas Huth } val;
474d9392beSThomas Huth int received;
484d9392beSThomas Huth };
494d9392beSThomas Huth typedef struct HRandomData HRandomData;
504d9392beSThomas Huth
514d9392beSThomas Huth /* Callback function for the RngBackend */
random_recv(void * dest,const void * src,size_t size)524d9392beSThomas Huth static void random_recv(void *dest, const void *src, size_t size)
534d9392beSThomas Huth {
544d9392beSThomas Huth HRandomData *hrdp = dest;
554d9392beSThomas Huth
564d9392beSThomas Huth if (src && size > 0) {
574d9392beSThomas Huth assert(size + hrdp->received <= sizeof(hrdp->val.v8));
584d9392beSThomas Huth memcpy(&hrdp->val.v8[hrdp->received], src, size);
594d9392beSThomas Huth hrdp->received += size;
604d9392beSThomas Huth }
614d9392beSThomas Huth
624d9392beSThomas Huth qemu_sem_post(&hrdp->sem);
634d9392beSThomas Huth }
644d9392beSThomas Huth
654d9392beSThomas Huth /* Handler for the H_RANDOM hypercall */
h_random(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)66ce2918cbSDavid Gibson static target_ulong h_random(PowerPCCPU *cpu, SpaprMachineState *spapr,
674d9392beSThomas Huth target_ulong opcode, target_ulong *args)
684d9392beSThomas Huth {
69ce2918cbSDavid Gibson SpaprRngState *rngstate;
704d9392beSThomas Huth HRandomData hrdata;
714d9392beSThomas Huth
724d9392beSThomas Huth rngstate = SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG, NULL));
734d9392beSThomas Huth
744d9392beSThomas Huth if (!rngstate || !rngstate->backend) {
754d9392beSThomas Huth return H_HARDWARE;
764d9392beSThomas Huth }
774d9392beSThomas Huth
784d9392beSThomas Huth qemu_sem_init(&hrdata.sem, 0);
794d9392beSThomas Huth hrdata.val.v64 = 0;
804d9392beSThomas Huth hrdata.received = 0;
814d9392beSThomas Huth
824d9392beSThomas Huth while (hrdata.received < 8) {
834d9392beSThomas Huth rng_backend_request_entropy(rngstate->backend, 8 - hrdata.received,
844d9392beSThomas Huth random_recv, &hrdata);
85*195801d7SStefan Hajnoczi bql_unlock();
864d9392beSThomas Huth qemu_sem_wait(&hrdata.sem);
87*195801d7SStefan Hajnoczi bql_lock();
88f1a6cf3eSGreg Kurz }
894d9392beSThomas Huth
904d9392beSThomas Huth qemu_sem_destroy(&hrdata.sem);
914d9392beSThomas Huth args[0] = hrdata.val.v64;
924d9392beSThomas Huth
934d9392beSThomas Huth return H_SUCCESS;
944d9392beSThomas Huth }
954d9392beSThomas Huth
spapr_rng_instance_init(Object * obj)964d9392beSThomas Huth static void spapr_rng_instance_init(Object *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_set_description(obj, "rng",
1047eecec7dSMarkus Armbruster "ID of the random number generator backend");
1054d9392beSThomas Huth }
1064d9392beSThomas Huth
spapr_rng_realize(DeviceState * dev,Error ** errp)1074d9392beSThomas Huth static void spapr_rng_realize(DeviceState *dev, Error **errp)
1084d9392beSThomas Huth {
1094d9392beSThomas Huth
110ce2918cbSDavid Gibson SpaprRngState *rngstate = SPAPR_RNG(dev);
1114d9392beSThomas Huth
1124d9392beSThomas Huth if (rngstate->use_kvm) {
1134d9392beSThomas Huth if (kvmppc_enable_hwrng() == 0) {
1144d9392beSThomas Huth return;
1154d9392beSThomas Huth }
1164d9392beSThomas Huth /*
1174d9392beSThomas Huth * If user specified both, use-kvm and a backend, we fall back to
1184d9392beSThomas Huth * the backend now. If not, provide an appropriate error message.
1194d9392beSThomas Huth */
1204d9392beSThomas Huth if (!rngstate->backend) {
1214d9392beSThomas Huth error_setg(errp, "Could not initialize in-kernel H_RANDOM call!");
1224d9392beSThomas Huth return;
1234d9392beSThomas Huth }
1244d9392beSThomas Huth }
1254d9392beSThomas Huth
1264d9392beSThomas Huth if (rngstate->backend) {
1274d9392beSThomas Huth spapr_register_hypercall(H_RANDOM, h_random);
1284d9392beSThomas Huth } else {
1294d9392beSThomas Huth error_setg(errp, "spapr-rng needs an RNG backend!");
1304d9392beSThomas Huth }
1314d9392beSThomas Huth }
1324d9392beSThomas Huth
1334d9392beSThomas Huth static Property spapr_rng_properties[] = {
134ce2918cbSDavid Gibson DEFINE_PROP_BOOL("use-kvm", SpaprRngState, use_kvm, false),
135ce2918cbSDavid Gibson DEFINE_PROP_LINK("rng", SpaprRngState, backend, TYPE_RNG_BACKEND,
13668c761e1SFam Zheng RngBackend *),
1374d9392beSThomas Huth DEFINE_PROP_END_OF_LIST(),
1384d9392beSThomas Huth };
1394d9392beSThomas Huth
spapr_rng_class_init(ObjectClass * oc,void * data)1404d9392beSThomas Huth static void spapr_rng_class_init(ObjectClass *oc, void *data)
1414d9392beSThomas Huth {
1424d9392beSThomas Huth DeviceClass *dc = DEVICE_CLASS(oc);
1434d9392beSThomas Huth
1444d9392beSThomas Huth dc->realize = spapr_rng_realize;
1454d9392beSThomas Huth set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1464f67d30bSMarc-André Lureau device_class_set_props(dc, spapr_rng_properties);
1473d0db3e7SGreg Kurz dc->hotpluggable = false;
1484d9392beSThomas Huth }
1494d9392beSThomas Huth
1504d9392beSThomas Huth static const TypeInfo spapr_rng_info = {
1514d9392beSThomas Huth .name = TYPE_SPAPR_RNG,
1524d9392beSThomas Huth .parent = TYPE_DEVICE,
153ce2918cbSDavid Gibson .instance_size = sizeof(SpaprRngState),
1544d9392beSThomas Huth .instance_init = spapr_rng_instance_init,
1554d9392beSThomas Huth .class_init = spapr_rng_class_init,
1564d9392beSThomas Huth };
1574d9392beSThomas Huth
spapr_rng_register_type(void)1584d9392beSThomas Huth static void spapr_rng_register_type(void)
1594d9392beSThomas Huth {
1604d9392beSThomas Huth type_register_static(&spapr_rng_info);
1614d9392beSThomas Huth }
1624d9392beSThomas Huth type_init(spapr_rng_register_type)
163