qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

spapr_rng.c (4567B)


      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 "qapi/error.h"
     22 #include "qemu/error-report.h"
     23 #include "qemu/main-loop.h"
     24 #include "qemu/module.h"
     25 #include "sysemu/device_tree.h"
     26 #include "sysemu/rng.h"
     27 #include "hw/ppc/spapr.h"
     28 #include "hw/qdev-properties.h"
     29 #include "kvm_ppc.h"
     30 #include "qom/object.h"
     31 
     32 OBJECT_DECLARE_SIMPLE_TYPE(SpaprRngState, SPAPR_RNG)
     33 
     34 struct SpaprRngState {
     35     /*< private >*/
     36     DeviceState ds;
     37     RngBackend *backend;
     38     bool use_kvm;
     39 };
     40 
     41 struct HRandomData {
     42     QemuSemaphore sem;
     43     union {
     44         uint64_t v64;
     45         uint8_t v8[8];
     46     } val;
     47     int received;
     48 };
     49 typedef struct HRandomData HRandomData;
     50 
     51 /* Callback function for the RngBackend */
     52 static void random_recv(void *dest, const void *src, size_t size)
     53 {
     54     HRandomData *hrdp = dest;
     55 
     56     if (src && size > 0) {
     57         assert(size + hrdp->received <= sizeof(hrdp->val.v8));
     58         memcpy(&hrdp->val.v8[hrdp->received], src, size);
     59         hrdp->received += size;
     60     }
     61 
     62     qemu_sem_post(&hrdp->sem);
     63 }
     64 
     65 /* Handler for the H_RANDOM hypercall */
     66 static target_ulong h_random(PowerPCCPU *cpu, SpaprMachineState *spapr,
     67                              target_ulong opcode, target_ulong *args)
     68 {
     69     SpaprRngState *rngstate;
     70     HRandomData hrdata;
     71 
     72     rngstate = SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG, NULL));
     73 
     74     if (!rngstate || !rngstate->backend) {
     75         return H_HARDWARE;
     76     }
     77 
     78     qemu_sem_init(&hrdata.sem, 0);
     79     hrdata.val.v64 = 0;
     80     hrdata.received = 0;
     81 
     82     while (hrdata.received < 8) {
     83         rng_backend_request_entropy(rngstate->backend, 8 - hrdata.received,
     84                                     random_recv, &hrdata);
     85         qemu_mutex_unlock_iothread();
     86         qemu_sem_wait(&hrdata.sem);
     87         qemu_mutex_lock_iothread();
     88     }
     89 
     90     qemu_sem_destroy(&hrdata.sem);
     91     args[0] = hrdata.val.v64;
     92 
     93     return H_SUCCESS;
     94 }
     95 
     96 static void spapr_rng_instance_init(Object *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_set_description(obj, "rng",
    104                                     "ID of the random number generator backend");
    105 }
    106 
    107 static void spapr_rng_realize(DeviceState *dev, Error **errp)
    108 {
    109 
    110     SpaprRngState *rngstate = SPAPR_RNG(dev);
    111 
    112     if (rngstate->use_kvm) {
    113         if (kvmppc_enable_hwrng() == 0) {
    114             return;
    115         }
    116         /*
    117          * If user specified both, use-kvm and a backend, we fall back to
    118          * the backend now. If not, provide an appropriate error message.
    119          */
    120         if (!rngstate->backend) {
    121             error_setg(errp, "Could not initialize in-kernel H_RANDOM call!");
    122             return;
    123         }
    124     }
    125 
    126     if (rngstate->backend) {
    127         spapr_register_hypercall(H_RANDOM, h_random);
    128     } else {
    129         error_setg(errp, "spapr-rng needs an RNG backend!");
    130     }
    131 }
    132 
    133 static Property spapr_rng_properties[] = {
    134     DEFINE_PROP_BOOL("use-kvm", SpaprRngState, use_kvm, false),
    135     DEFINE_PROP_LINK("rng", SpaprRngState, backend, TYPE_RNG_BACKEND,
    136                      RngBackend *),
    137     DEFINE_PROP_END_OF_LIST(),
    138 };
    139 
    140 static void spapr_rng_class_init(ObjectClass *oc, void *data)
    141 {
    142     DeviceClass *dc = DEVICE_CLASS(oc);
    143 
    144     dc->realize = spapr_rng_realize;
    145     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
    146     device_class_set_props(dc, spapr_rng_properties);
    147     dc->hotpluggable = false;
    148 }
    149 
    150 static const TypeInfo spapr_rng_info = {
    151     .name          = TYPE_SPAPR_RNG,
    152     .parent        = TYPE_DEVICE,
    153     .instance_size = sizeof(SpaprRngState),
    154     .instance_init = spapr_rng_instance_init,
    155     .class_init    = spapr_rng_class_init,
    156 };
    157 
    158 static void spapr_rng_register_type(void)
    159 {
    160     type_register_static(&spapr_rng_info);
    161 }
    162 type_init(spapr_rng_register_type)