xref: /qemu/hw/ppc/spapr_rng.c (revision 68c761e1)
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 "qemu-common.h"
234771d756SPaolo Bonzini #include "cpu.h"
244d9392beSThomas Huth #include "qemu/error-report.h"
254d9392beSThomas Huth #include "sysemu/sysemu.h"
264d9392beSThomas Huth #include "sysemu/device_tree.h"
274d9392beSThomas Huth #include "sysemu/rng.h"
284d9392beSThomas Huth #include "hw/ppc/spapr.h"
294d9392beSThomas Huth #include "kvm_ppc.h"
304d9392beSThomas Huth 
314d9392beSThomas Huth #define SPAPR_RNG(obj) \
324d9392beSThomas Huth     OBJECT_CHECK(sPAPRRngState, (obj), TYPE_SPAPR_RNG)
334d9392beSThomas Huth 
344d9392beSThomas Huth struct sPAPRRngState {
354d9392beSThomas Huth     /*< private >*/
364d9392beSThomas Huth     DeviceState ds;
374d9392beSThomas Huth     RngBackend *backend;
384d9392beSThomas Huth     bool use_kvm;
394d9392beSThomas Huth };
404d9392beSThomas Huth typedef struct sPAPRRngState sPAPRRngState;
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 */
674d9392beSThomas Huth static target_ulong h_random(PowerPCCPU *cpu, sPAPRMachineState *spapr,
684d9392beSThomas Huth                              target_ulong opcode, target_ulong *args)
694d9392beSThomas Huth {
704d9392beSThomas Huth     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",
1054d9392beSThomas Huth                                     "ID of the random number generator backend",
1064d9392beSThomas Huth                                     NULL);
1074d9392beSThomas Huth }
1084d9392beSThomas Huth 
1094d9392beSThomas Huth static void spapr_rng_realize(DeviceState *dev, Error **errp)
1104d9392beSThomas Huth {
1114d9392beSThomas Huth 
1124d9392beSThomas Huth     sPAPRRngState *rngstate = SPAPR_RNG(dev);
1134d9392beSThomas Huth 
1144d9392beSThomas Huth     if (rngstate->use_kvm) {
1154d9392beSThomas Huth         if (kvmppc_enable_hwrng() == 0) {
1164d9392beSThomas Huth             return;
1174d9392beSThomas Huth         }
1184d9392beSThomas Huth         /*
1194d9392beSThomas Huth          * If user specified both, use-kvm and a backend, we fall back to
1204d9392beSThomas Huth          * the backend now. If not, provide an appropriate error message.
1214d9392beSThomas Huth          */
1224d9392beSThomas Huth         if (!rngstate->backend) {
1234d9392beSThomas Huth             error_setg(errp, "Could not initialize in-kernel H_RANDOM call!");
1244d9392beSThomas Huth             return;
1254d9392beSThomas Huth         }
1264d9392beSThomas Huth     }
1274d9392beSThomas Huth 
1284d9392beSThomas Huth     if (rngstate->backend) {
1294d9392beSThomas Huth         spapr_register_hypercall(H_RANDOM, h_random);
1304d9392beSThomas Huth     } else {
1314d9392beSThomas Huth         error_setg(errp, "spapr-rng needs an RNG backend!");
1324d9392beSThomas Huth     }
1334d9392beSThomas Huth }
1344d9392beSThomas Huth 
1354d9392beSThomas Huth int spapr_rng_populate_dt(void *fdt)
1364d9392beSThomas Huth {
1374d9392beSThomas Huth     int node;
1384d9392beSThomas Huth     int ret;
1394d9392beSThomas Huth 
1404d9392beSThomas Huth     node = qemu_fdt_add_subnode(fdt, "/ibm,platform-facilities");
1414d9392beSThomas Huth     if (node <= 0) {
1424d9392beSThomas Huth         return -1;
1434d9392beSThomas Huth     }
1444d9392beSThomas Huth     ret = fdt_setprop_string(fdt, node, "device_type",
1454d9392beSThomas Huth                              "ibm,platform-facilities");
1464d9392beSThomas Huth     ret |= fdt_setprop_cell(fdt, node, "#address-cells", 0x1);
1474d9392beSThomas Huth     ret |= fdt_setprop_cell(fdt, node, "#size-cells", 0x0);
1484d9392beSThomas Huth 
1494d9392beSThomas Huth     node = fdt_add_subnode(fdt, node, "ibm,random-v1");
1504d9392beSThomas Huth     if (node <= 0) {
1514d9392beSThomas Huth         return -1;
1524d9392beSThomas Huth     }
1534d9392beSThomas Huth     ret |= fdt_setprop_string(fdt, node, "compatible", "ibm,random");
1544d9392beSThomas Huth 
1554d9392beSThomas Huth     return ret ? -1 : 0;
1564d9392beSThomas Huth }
1574d9392beSThomas Huth 
1584d9392beSThomas Huth static Property spapr_rng_properties[] = {
1594d9392beSThomas Huth     DEFINE_PROP_BOOL("use-kvm", sPAPRRngState, use_kvm, false),
160*68c761e1SFam Zheng     DEFINE_PROP_LINK("rng", sPAPRRngState, backend, TYPE_RNG_BACKEND,
161*68c761e1SFam Zheng                      RngBackend *),
1624d9392beSThomas Huth     DEFINE_PROP_END_OF_LIST(),
1634d9392beSThomas Huth };
1644d9392beSThomas Huth 
1654d9392beSThomas Huth static void spapr_rng_class_init(ObjectClass *oc, void *data)
1664d9392beSThomas Huth {
1674d9392beSThomas Huth     DeviceClass *dc = DEVICE_CLASS(oc);
1684d9392beSThomas Huth 
1694d9392beSThomas Huth     dc->realize = spapr_rng_realize;
1704d9392beSThomas Huth     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1714d9392beSThomas Huth     dc->props = spapr_rng_properties;
1723d0db3e7SGreg Kurz     dc->hotpluggable = false;
1734d9392beSThomas Huth }
1744d9392beSThomas Huth 
1754d9392beSThomas Huth static const TypeInfo spapr_rng_info = {
1764d9392beSThomas Huth     .name          = TYPE_SPAPR_RNG,
1774d9392beSThomas Huth     .parent        = TYPE_DEVICE,
1784d9392beSThomas Huth     .instance_size = sizeof(sPAPRRngState),
1794d9392beSThomas Huth     .instance_init = spapr_rng_instance_init,
1804d9392beSThomas Huth     .class_init    = spapr_rng_class_init,
1814d9392beSThomas Huth };
1824d9392beSThomas Huth 
1834d9392beSThomas Huth static void spapr_rng_register_type(void)
1844d9392beSThomas Huth {
1854d9392beSThomas Huth     type_register_static(&spapr_rng_info);
1864d9392beSThomas Huth }
1874d9392beSThomas Huth type_init(spapr_rng_register_type)
188