Skip to contentSkip to conversation

spapr: Add support for hwrng when available

PATCH In review Michael Ellerman 3 participants 4 messages Sep 26, 2013 – Sep 27, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spapr: Add support for hwrng when available

Some powerpc systems have support for a hardware random number generator (hwrng). If such a hwrng is present the host kernel can provide access to it via the H_RANDOM hcall.

The kernel advertises the presence of a hwrng with the KVM_CAP_PPC_HWRNG capability. If this is detected we add the appropriate device tree bits to advertise the presence of the hwrng to the guest kernel.

---
 hw/ppc/spapr.c            | 16 ++++++++++++++++
 include/hw/ppc/spapr.h    |  1 +
 linux-headers/linux/kvm.h |  1 +
 target-ppc/kvm.c          |  5 +++++
 target-ppc/kvm_ppc.h      |  5 +++++
 5 files changed, 28 insertions(+)
hw/ppc/spapr.c +16 -0
index 004184d..5909df1 100644
@@ -497,6 +497,22 @@ static void *spapr_create_fdt_skel(const char *cpu_model,
 
     _FDT((fdt_end_node(fdt)));
 
+    if (kvmppc_hwrng_present()) {
+        _FDT(fdt_begin_node(fdt, "ibm,platform-facilities"));
+
+        _FDT(fdt_property_string(fdt, "name", "ibm,platform-facilities"));
+        _FDT(fdt_property_string(fdt, "device_type",
+                                 "ibm,platform-facilities"));
+        _FDT(fdt_property_cell(fdt, "#address-cells", 0x1));
+        _FDT(fdt_property_cell(fdt, "#size-cells", 0x0));
+        _FDT(fdt_begin_node(fdt, "ibm,random-v1"));
+        _FDT(fdt_property_string(fdt, "name", "ibm,random-v1"));
+        _FDT(fdt_property_string(fdt, "compatible", "ibm,random"));
+        _FDT((fdt_end_node(fdt)));
+    }
+
+    _FDT((fdt_end_node(fdt)));
+
     /* event-sources */
     spapr_events_fdt_skel(fdt, epow_irq);
 
include/hw/ppc/spapr.h +1 -0
index e37b419..c509500 100644
@@ -283,6 +283,7 @@ typedef struct sPAPREnvironment {
 #define H_GET_EM_PARMS          0x2B8
 #define H_SET_MPP               0x2D0
 #define H_GET_MPP               0x2D4
+#define H_RANDOM                0x300
 #define H_SET_MODE              0x31C
 #define MAX_HCALL_OPCODE        H_SET_MODE
 
linux-headers/linux/kvm.h +1 -0
index c614070..7be746c 100644
@@ -666,6 +666,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_IRQ_MPIC 90
 #define KVM_CAP_PPC_RTAS 91
 #define KVM_CAP_IRQ_XICS 92
+#define KVM_CAP_PPC_HWRNG 95
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
target-ppc/kvm.c +5 -0
index 8a196c6..faf5dae 100644
@@ -1875,3 +1875,8 @@ int kvm_arch_on_sigbus(int code, void *addr)
 void kvm_arch_init_irq_routing(KVMState *s)
 {
 }
+
+bool kvmppc_hwrng_present(void)
+{
+    return kvm_enabled() && kvm_check_extension(kvm_state, KVM_CAP_PPC_HWRNG);
+}
target-ppc/kvm_ppc.h +5 -0
index 4ae7bf2..b7b898b 100644
@@ -42,6 +42,7 @@ int kvmppc_get_htab_fd(bool write);
 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns);
 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                            uint16_t n_valid, uint16_t n_invalid);
+bool kvmppc_hwrng_present(void);
 
 #else
 
@@ -181,6 +182,10 @@ static inline int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
     abort();
 }
 
+static inline bool kvmppc_hwrng_present(void)
+{
+    return false;
+}
 #endif
 
 #ifndef CONFIG_KVM
Signature · 1 line
1.8.1.2
#1 lore raw
Quoted 9 lines · Michael Ellerman

Some powerpc systems have support for a hardware random number generator (hwrng). If such a hwrng is present the host kernel can provide access to it via the H_RANDOM hcall.

The kernel advertises the presence of a hwrng with the KVM_CAP_PPC_HWRNG capability. If this is detected we add the appropriate device tree bits to advertise the presence of the hwrng to the guest kernel.

Please implement this 100% without KVM first, then if we end up running into performance bottlenecks we can always add KVM acceleration.

Also, please make sure to CC [email protected] on PPC patches :).

Alex

#2 lore raw
Quoted 6 lines · Alexander Graf
Quoted 4 lines · Michael Ellerman

Some powerpc systems have support for a hardware random number generator (hwrng). If such a hwrng is present the host kernel can provide access to it via the H_RANDOM hcall.

Quoted 7 lines
Quoted 5 lines

The kernel advertises the presence of a hwrng with the KVM_CAP_PPC_HWRNG capability. If this is detected we add the appropriate device tree bits to advertise the presence of the hwrng to the guest kernel.

Please implement this 100% without KVM first, then if we end up running into performance bottlenecks we can always add KVM acceleration.

So have the host kernel read from the hwrng, export it to userspace via a char device, which is then read by qemu, which then copies it back into the host kernel, which can then give it to the guest.

Or from the guest perspective, instead of a cheap switch to host real mode and back - a full switch to kernel virtual, then to user, back to kernel, back to user, back to kernel, back to guest.

Frankly I can't see why that is a superior option?

Quoted 1 line

Also, please make sure to CC [email protected] on PPC patches :).

Sorry, didn't realise there was one, will add it in future.

cheers

#3 lore raw

Il 27/09/2013 10:36, Michael Ellerman ha scritto:

Quoted 9 lines

So have the host kernel read from the hwrng, export it to userspace via a char device, which is then read by qemu, which then copies it back into the host kernel, which can then give it to the guest.

Or from the guest perspective, instead of a cheap switch to host real mode and back - a full switch to kernel virtual, then to user, back to kernel, back to user, back to kernel, back to guest.

Frankly I can't see why that is a superior option?

Because this is not a fast path at all. Doing things in QEMU lets people test and configure the paravirtualized hwrng even if they do not have a hwrng in the host, and even if they are running with emulation (TCG) instead of KVM.

But as I mentioned in the kernel thread, perhaps you do not need the hypercall at all if virtio-rng is enough (it should be for Linux guests).

Paolo