qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: David Hildenbrand <david@redhat.com>
Cc: qemu-s390x@nongnu.org, qemu-devel@nongnu.org,
	Thomas Huth <thuth@redhat.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Richard Henderson <richard.henderson@linaro.org>,
	Cornelia Huck <cohuck@redhat.com>,
	Harald Freudenberger <freude@linux.ibm.com>,
	Holger Dengler <dengler@linux.ibm.com>
Subject: Re: [PATCH v6 2/2] target/s390x: support SHA-512 extensions
Date: Fri, 5 Aug 2022 15:01:40 +0200	[thread overview]
Message-ID: <Yu0UtNzyb81O0ND2@zx2c4.com> (raw)
In-Reply-To: <6ad7580d-ee43-d744-8eed-cd363c4fb911@redhat.com>

Hi David,

On Fri, Aug 05, 2022 at 01:28:18PM +0200, David Hildenbrand wrote:
> On 03.08.22 19:15, Jason A. Donenfeld wrote:
> > In order to fully support MSA_EXT_5, we have to also support the SHA-512
> > special instructions. So implement those.
> > 
> > The implementation began as something TweetNacl-like, and then was
> > adjusted to be useful here. It's not very beautiful, but it is quite
> > short and compact, which is what we're going for.
> > 
> 
> NIT: we could think about reversing the order of patches. IIRC, patch #1
> itself would trigger a warning when starting QEMU. Having this patch
> first make sense logically.

Good idea. Will do.

> > +static int kimd_sha512(CPUS390XState *env, uintptr_t ra, uint64_t parameter_block,
> > +                       uint64_t *message_reg, uint64_t *len_reg, uint8_t *stack_buffer)
> > +{
> > +    enum { MAX_BLOCKS_PER_RUN = 64 }; /* This is arbitrary, just to keep interactivity. */
> 
> I'd just use a #define outside of the function for that.

Why? What does leaking this into file-level scope do?

> 
> > +    uint64_t z[8], b[8], a[8], w[16], t;
> > +    uint64_t message = message_reg ? *message_reg : 0, len = *len_reg, processed = 0;
> > +    int i, j, reg_len = 64, blocks = 0, cc = 0;
> > +
> > +    if (!(env->psw.mask & PSW_MASK_64)) {
> > +        len = (uint32_t)len;
> > +        reg_len = (env->psw.mask & PSW_MASK_32) ? 32 : 24;
> > +    }
> 
> 
> I'd call that message_reg_len. (same in other function)

Will do.

> 
> 
> > +
> > +    for (i = 0; i < 8; ++i) {
> > +        z[i] = a[i] = cpu_ldq_be_data_ra(env, wrap_address(env, parameter_block + 8 * i), ra);
> 
> I assume if we get any exception here, we simply didn't make any progress.
> 
> > +    }
> > +
> > +    while (len >= 128) {
> > +        if (++blocks > MAX_BLOCKS_PER_RUN) {
> > +            cc = 3;
> > +            break;
> > +        }
> > +
> > +        for (i = 0; i < 16; ++i) {
> > +            if (message) {
> > +                w[i] = cpu_ldq_be_data_ra(env, wrap_address(env, message + 8 * i), ra);
> 
> dito

Right, there's no progress, because it's only ever incremented at the
end. And, more importantly, we only ever update the parameter_block
after having done things successfully.

> > +    for (i = 0; i < 8; ++i) {
> > +        cpu_stq_be_data_ra(env, wrap_address(env, parameter_block + 8 * i), z[i], ra);
> 
> I wonder what happens if we get an exception somewhere in the middle
> here ... fortunately we can only involve 2 pages.

If this fails, then message_reg and len_reg won't be updated, so it will
have to start over. If it fails part way through, though, then things
are inconsistent. I don't think we want to hassle with trying to restore
the previous state or something insane though. That seems a bit much.

> > +    cc = kimd_sha512(env, ra, parameter_block, message_reg, len_reg, NULL);
> > +    if (cc) {
> > +        return cc;
> > +    }
> 
> Doesn't kimd_sha512() update the length register? And if we return with
> cc=3, we'd be in trouble, no?

cc=3 means partial completion. In that case, klmd also returns with a
partial completion. That's good and expected! It means that the next
time it's called, it'll keep going where it left off.

I've actually tried this with the Linux implementation, and it works as
expected.

> One idea could be to simply only process one block at a time. Read all
> inputs first for that block and handle it completely without any
> register modifications. Perform all memory writes in a single call.

That *is* what already happens. Actually, the memory writes only ever
happen at the very end of kimd_sha512.

> Further, I wonder if we should factor out the core of kimd_sha512() to
> only work on temp buffers without any loading/storing of memory, and let
> only kimd_sha512/klmd_sha512 perform all loading/storing. Then it's much
> cleaner who modifies what.

That's not necessary and will complicate things ultimately. See the
above; this is already working as expected.

Jason


  reply	other threads:[~2022-08-05 13:06 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-12 16:46 [PATCH qemu] target/s390x: support PRNO_TRNG instruction Jason A. Donenfeld
2022-07-19  9:54 ` David Hildenbrand
2022-07-19 11:23   ` Jason A. Donenfeld
2022-07-19 11:43     ` [PATCH v2] " Jason A. Donenfeld
2022-07-20 11:43       ` David Hildenbrand
2022-07-20 11:58         ` Jason A. Donenfeld
2022-07-20 12:08           ` [PATCH v3] " Jason A. Donenfeld
2022-07-20 18:41             ` David Hildenbrand
2022-07-20 19:44               ` Jason A. Donenfeld
2022-07-27  1:35               ` Jason A. Donenfeld
2022-07-27  6:32                 ` Thomas Huth
2022-07-27 11:58                   ` Jason A. Donenfeld
2022-08-02 13:26             ` Christian Borntraeger
2022-08-02 13:54               ` David Hildenbrand
2022-08-02 14:01                 ` Christian Borntraeger
2022-08-02 14:53                   ` David Hildenbrand
2022-08-02 15:15                     ` Christian Borntraeger
2022-08-02 15:16                       ` David Hildenbrand
2022-08-02 15:28                         ` Jason A. Donenfeld
2022-08-02 15:32                           ` David Hildenbrand
2022-08-02 18:59                             ` Jason A. Donenfeld
2022-08-02 19:00                               ` [PATCH v4 0/2] MSA EXT 5 for s390x Jason A. Donenfeld
2022-08-02 19:00                                 ` [PATCH v4 1/2] target/s390x: support PRNO_TRNG instruction Jason A. Donenfeld
2022-08-02 19:00                                 ` [PATCH v4 2/2] target/s390x: support SHA-512 extensions Jason A. Donenfeld
2022-08-03 11:55                                   ` David Hildenbrand
2022-08-03 12:14                                     ` Jason A. Donenfeld
2022-08-03 12:47                                       ` Jason A. Donenfeld
2022-08-03 12:51                                         ` [PATCH v5 1/2] target/s390x: support PRNO_TRNG instruction Jason A. Donenfeld
2022-08-03 12:51                                           ` [PATCH v5 2/2] target/s390x: support SHA-512 extensions Jason A. Donenfeld
2022-08-03 17:15                                             ` [PATCH 1/2] target/s390x: support PRNO_TRNG instruction Jason A. Donenfeld
2022-08-03 17:15                                               ` [PATCH 2/2] target/s390x: support SHA-512 extensions Jason A. Donenfeld
2022-08-03 17:15                                             ` [PATCH v6 1/2] target/s390x: support PRNO_TRNG instruction Jason A. Donenfeld
2022-08-03 17:15                                               ` [PATCH v6 2/2] target/s390x: support SHA-512 extensions Jason A. Donenfeld
2022-08-05 11:28                                                 ` David Hildenbrand
2022-08-05 13:01                                                   ` Jason A. Donenfeld [this message]
2022-08-09 15:03                                                     ` [PATCH v7 1/2] " Jason A. Donenfeld
2022-08-09 15:03                                                       ` [PATCH v7 2/2] target/s390x: support PRNO_TRNG instruction Jason A. Donenfeld
2022-08-26 11:28                                                         ` Thomas Huth
2022-08-29 16:29                                                           ` Jason A. Donenfeld
2022-09-21 10:59                                                             ` Thomas Huth
2022-08-26 10:21                                                       ` [PATCH v7 1/2] target/s390x: support SHA-512 extensions Thomas Huth
2022-08-29 16:27                                                         ` Jason A. Donenfeld
2022-08-11 16:37                                                     ` [PATCH v6 2/2] " David Hildenbrand
2022-08-04  6:51                                       ` [PATCH v4 " Harald Freudenberger
2022-08-04  6:56                                         ` Christian Borntraeger
2022-08-04 12:09                                           ` Jason A. Donenfeld
2022-08-04  8:10                                         ` David Hildenbrand
2022-08-04 12:07                                           ` Jason A. Donenfeld
2022-08-02 17:55             ` [PATCH v3] target/s390x: support PRNO_TRNG instruction Jason A. Donenfeld
2022-07-20 18:01           ` [PATCH v2] " David Hildenbrand
2022-08-02 11:54       ` Harald Freudenberger
2022-07-19 10:00 ` [PATCH qemu] " Thomas Huth
2022-07-19 11:27   ` Jason A. Donenfeld

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=Yu0UtNzyb81O0ND2@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=cohuck@redhat.com \
    --cc=david@redhat.com \
    --cc=dengler@linux.ibm.com \
    --cc=freude@linux.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=thuth@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).