kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jacob Xu <jacobhxu@google.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Jim Mattson <jmattson@google.com>, kvm list <kvm@vger.kernel.org>
Subject: Re: [kvm-unit-tests PATCH v2] x86: Do not assign values to unaligned pointer to 128 bits
Date: Thu, 6 May 2021 12:13:57 -0700	[thread overview]
Message-ID: <CAJ5mJ6gYmwXEQZASk8A_Ozt6asW6ZDTnDs83nCfLNTa62x7n+g@mail.gmail.com> (raw)
In-Reply-To: <YJQ8NN6EzzZEiJ6a@google.com>

> memset() takes a void *, which it casts to an char, i.e. it works on one byte at
a time.
Huh, TIL. Based on this I'd thought that I don't need a cast at all,
but doing so actually results in a movaps instruction.
I've changed the cast back to (uint8_t *).

> The size needs to be sizeof(*mem)
Bah, thanks for catching that.

I've updated it below.
---
 x86/emulator.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/x86/emulator.c b/x86/emulator.c
index 9705073..ea23ef1 100644
--- a/x86/emulator.c
+++ b/x86/emulator.c
@@ -716,12 +716,12 @@ static __attribute__((target("sse2"))) void
test_sse_exceptions(void *cross_mem)

        // test unaligned access for movups, movupd and movaps
        v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-       mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+       memset((uint8_t *)mem, 5, sizeof(*mem));
        asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
        report(sseeq(&v, mem), "movups unaligned");

        v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-       mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+       memset((uint8_t *)mem, 5, sizeof(*mem));
        asm("movupd %1, %0" : "=m"(*mem) : "x"(v.sse));
        report(sseeq(&v, mem), "movupd unaligned");
        exceptions = 0;
@@ -734,7 +734,7 @@ static __attribute__((target("sse2"))) void
test_sse_exceptions(void *cross_mem)
        // setup memory for cross page access
        mem = (sse_union *)(&bytes[4096-8]);
        v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-       mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+       memset((uint8_t *)mem, 5, sizeof(*mem));

        asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
        report(sseeq(&v, mem), "movups unaligned crosspage");
--


On Thu, May 6, 2021 at 11:58 AM Sean Christopherson <seanjc@google.com> wrote:
>
> On Thu, May 06, 2021, Jacob Xu wrote:
> > When compiled with clang, the following statement gets converted into a
> > movaps instructions.
> > mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
> >
> > Since mem is an unaligned pointer to a union of an sse, we get a GP when
> > running.
> >
> > All we want is to make the values between mem and v different for this
> > testcase, so let's just memset the pointer at mem, and convert to
> > uint32_t pointer. Then the compiler will not assume the pointer is
> > aligned to 128 bits.
> >
> > Fixes: e5e76263b5 ("x86: add additional test cases for sse exceptions to
> > emulator.c")
> >
> > Signed-off-by: Jacob Xu <jacobhxu@google.com>
> > ---
> >  x86/emulator.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/x86/emulator.c b/x86/emulator.c
> > index 9705073..a2c7e5b 100644
> > --- a/x86/emulator.c
> > +++ b/x86/emulator.c
> > @@ -716,12 +716,12 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
> >
> >       // test unaligned access for movups, movupd and movaps
> >       v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
> > -     mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
> > +     memset((uint32_t *)mem, 0xdecafbad, sizeof(mem));
>
> memset() takes a void *, which it casts to an char, i.e. it works on one byte at
> a time.  Casting to a uint32_t won't make it write the full "0xdecafbad", it will
> just repease 0xad over and over.
>
> The size needs to be sizeof(*mem), i.e. the size of the object that mem points to,
> not the size of the pointer's storage.
>
> >       asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
> >       report(sseeq(&v, mem), "movups unaligned");
> >
> >       v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
> > -     mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
> > +     memset((uint32_t *)mem, 0xdecafbad, sizeof(mem));
> >       asm("movupd %1, %0" : "=m"(*mem) : "x"(v.sse));
> >       report(sseeq(&v, mem), "movupd unaligned");
> >       exceptions = 0;
> > @@ -734,7 +734,7 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
> >       // setup memory for cross page access
> >       mem = (sse_union *)(&bytes[4096-8]);
> >       v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
> > -     mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
> > +     memset((uint32_t *)mem, 0xdecafbad, sizeof(mem));
> >
> >       asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
> >       report(sseeq(&v, mem), "movups unaligned crosspage");
> > --
> > 2.31.1.607.g51e8a6a459-goog
> >

  reply	other threads:[~2021-05-06 19:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-06 18:49 [kvm-unit-tests PATCH v2] x86: Do not assign values to unaligned pointer to 128 bits Jacob Xu
2021-05-06 18:57 ` Sean Christopherson
2021-05-06 19:13   ` Jacob Xu [this message]
2021-05-06 19:25     ` Sean Christopherson
2021-05-06 20:11     ` Jim Mattson
2021-05-11  1:47       ` Jacob Xu

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=CAJ5mJ6gYmwXEQZASk8A_Ozt6asW6ZDTnDs83nCfLNTa62x7n+g@mail.gmail.com \
    --to=jacobhxu@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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).