qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>, qemu-devel@nongnu.org
Cc: qemu-s390x@nongnu.org, "Jason A. Donenfeld" <Jason@zx2c4.com>,
	David Hildenbrand <david@redhat.com>
Subject: [PULL 04/14] target/s390x: support PRNO_TRNG instruction
Date: Mon, 26 Sep 2022 19:07:54 +0200	[thread overview]
Message-ID: <20220926170804.453855-5-thuth@redhat.com> (raw)
In-Reply-To: <20220926170804.453855-1-thuth@redhat.com>

From: "Jason A. Donenfeld" <Jason@zx2c4.com>

In order for hosts running inside of TCG to initialize the kernel's
random number generator, we should support the PRNO_TRNG instruction,
backed in the usual way with the qemu_guest_getrandom helper. This is
confirmed working on Linux 5.19.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Message-Id: <20220921100729.2942008-2-Jason@zx2c4.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
[thuth: turn prno-trng off in avocado test to avoid breaking it]
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 target/s390x/gen-features.c              |  1 +
 target/s390x/tcg/crypto_helper.c         | 30 ++++++++++++++++++++++++
 tests/avocado/machine_s390_ccw_virtio.py |  1 +
 3 files changed, 32 insertions(+)

diff --git a/target/s390x/gen-features.c b/target/s390x/gen-features.c
index baadbf4517..1e3b7c0dc9 100644
--- a/target/s390x/gen-features.c
+++ b/target/s390x/gen-features.c
@@ -757,6 +757,7 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_MSA_EXT_5,
     S390_FEAT_KIMD_SHA_512,
     S390_FEAT_KLMD_SHA_512,
+    S390_FEAT_PRNO_TRNG,
 };
 
 /****** END FEATURE DEFS ******/
diff --git a/target/s390x/tcg/crypto_helper.c b/target/s390x/tcg/crypto_helper.c
index 106c89fd2d..762b277884 100644
--- a/target/s390x/tcg/crypto_helper.c
+++ b/target/s390x/tcg/crypto_helper.c
@@ -14,6 +14,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/main-loop.h"
+#include "qemu/guest-random.h"
 #include "s390x-internal.h"
 #include "tcg_s390x.h"
 #include "exec/helper-proto.h"
@@ -244,6 +245,31 @@ static int cpacf_sha512(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
     return !len ? 0 : 3;
 }
 
+static void fill_buf_random(CPUS390XState *env, uintptr_t ra,
+                            uint64_t *buf_reg, uint64_t *len_reg)
+{
+    uint8_t tmp[256];
+    uint64_t len = *len_reg;
+    int buf_reg_len = 64;
+
+    if (!(env->psw.mask & PSW_MASK_64)) {
+        len = (uint32_t)len;
+        buf_reg_len = (env->psw.mask & PSW_MASK_32) ? 32 : 24;
+    }
+
+    while (len) {
+        size_t block = MIN(len, sizeof(tmp));
+
+        qemu_guest_getrandom_nofail(tmp, block);
+        for (size_t i = 0; i < block; ++i) {
+            cpu_stb_data_ra(env, wrap_address(env, *buf_reg), tmp[i], ra);
+            *buf_reg = deposit64(*buf_reg, 0, buf_reg_len, *buf_reg + 1);
+            --*len_reg;
+        }
+        len -= block;
+    }
+}
+
 uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
                      uint32_t type)
 {
@@ -281,6 +307,10 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     case 3: /* CPACF_*_SHA_512 */
         return cpacf_sha512(env, ra, env->regs[1], &env->regs[r2],
                             &env->regs[r2 + 1], type);
+    case 114: /* CPACF_PRNO_TRNG */
+        fill_buf_random(env, ra, &env->regs[r1], &env->regs[r1 + 1]);
+        fill_buf_random(env, ra, &env->regs[r2], &env->regs[r2 + 1]);
+        break;
     default:
         /* we don't implement any other subfunction yet */
         g_assert_not_reached();
diff --git a/tests/avocado/machine_s390_ccw_virtio.py b/tests/avocado/machine_s390_ccw_virtio.py
index 438a6f4321..78152f2ad1 100644
--- a/tests/avocado/machine_s390_ccw_virtio.py
+++ b/tests/avocado/machine_s390_ccw_virtio.py
@@ -66,6 +66,7 @@ def test_s390x_devices(self):
                          '-kernel', kernel_path,
                          '-initrd', initrd_path,
                          '-append', kernel_command_line,
+                         '-cpu', 'max,prno-trng=off',
                          '-device', 'virtio-net-ccw,devno=fe.1.1111',
                          '-device',
                          'virtio-rng-ccw,devno=fe.2.0000,max_revision=0,id=rn1',
-- 
2.31.1



  parent reply	other threads:[~2022-09-26 18:22 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-26 17:07 [PULL 00/14] s390x patches and slirp submodule removal Thomas Huth
2022-09-26 17:07 ` [PULL 01/14] s390x/tcg: Fix opcode for lzrf Thomas Huth
2022-09-26 17:07 ` [PULL 02/14] linux-user/host/s390: Add vector instructions to host_signal_write() Thomas Huth
2022-09-26 17:07 ` [PULL 03/14] target/s390x: support SHA-512 extensions Thomas Huth
2022-09-26 17:07 ` Thomas Huth [this message]
2022-09-26 17:07 ` [PULL 05/14] configure: Add -Wno-gnu-variable-sized-type-not-at-end Thomas Huth
2022-09-26 17:07 ` [PULL 06/14] Update linux headers to v6.0-rc4 Thomas Huth
2022-09-26 17:07 ` [PULL 07/14] s390x/pci: add routine to get host function handle from CLP info Thomas Huth
2022-09-26 17:07 ` [PULL 08/14] s390x/pci: enable for load/store interpretation Thomas Huth
2022-09-26 17:07 ` [PULL 09/14] s390x/pci: don't fence interpreted devices without MSI-X Thomas Huth
2022-09-26 17:08 ` [PULL 10/14] s390x/pci: enable adapter event notification for interpreted devices Thomas Huth
2022-09-26 17:08 ` [PULL 11/14] s390x/pci: let intercept devices have separate PCI groups Thomas Huth
2022-09-26 17:08 ` [PULL 12/14] s390x/pci: reflect proper maxstbl for groups of interpreted devices Thomas Huth
2022-09-26 17:08 ` [PULL 13/14] s390x/s390-virtio-ccw: add zpcii-disable machine property Thomas Huth
2022-09-26 17:08 ` [PULL 14/14] Remove the slirp submodule (i.e. compile only with an external libslirp) Thomas Huth
2022-09-27 18:40 ` [PULL 00/14] s390x patches and slirp submodule removal Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220926170804.453855-5-thuth@redhat.com \
    --to=thuth@redhat.com \
    --cc=Jason@zx2c4.com \
    --cc=david@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).