From: Jacob Xu <jacobhxu@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
Jim Mattson <jmattson@google.com>,
Sean Christopherson <seanjc@google.com>
Cc: kvm list <kvm@vger.kernel.org>
Subject: Re: [kvm-unit-tests PATCH 1/2] x86: Do not assign values to unaligned pointer to 128 bits
Date: Thu, 10 Jun 2021 09:14:33 -0700 [thread overview]
Message-ID: <CAJ5mJ6jUvHN5Mh_hVR5rArvA6aFNdhDCinQd1ZjLg=ht=J4ijw@mail.gmail.com> (raw)
In-Reply-To: <20210511015016.815461-1-jacobhxu@google.com>
ping (previous ping just now contained html subpart, oops).
On Mon, May 10, 2021 at 6:50 PM Jacob Xu <jacobhxu@google.com> 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 sse_union, we get a GP when
> running. Let's avoid using a pointer to sse_union at all, since doing so
> implies that 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 | 67 +++++++++++++++++++++++++-------------------------
> 1 file changed, 33 insertions(+), 34 deletions(-)
>
> diff --git a/x86/emulator.c b/x86/emulator.c
> index 9705073..1d5c172 100644
> --- a/x86/emulator.c
> +++ b/x86/emulator.c
> @@ -645,37 +645,34 @@ static void test_muldiv(long *mem)
>
> typedef unsigned __attribute__((vector_size(16))) sse128;
>
> -typedef union {
> - sse128 sse;
> - unsigned u[4];
> -} sse_union;
> -
> -static bool sseeq(sse_union *v1, sse_union *v2)
> +static bool sseeq(uint32_t *v1, uint32_t *v2)
> {
> bool ok = true;
> int i;
>
> for (i = 0; i < 4; ++i) {
> - ok &= v1->u[i] == v2->u[i];
> + ok &= v1[i] == v2[i];
> }
>
> return ok;
> }
>
> -static __attribute__((target("sse2"))) void test_sse(sse_union *mem)
> +static __attribute__((target("sse2"))) void test_sse(uint32_t *mem)
> {
> - sse_union v;
> + sse128 vv;
> + uint32_t *v = (uint32_t *)&vv;
>
> write_cr0(read_cr0() & ~6); /* EM, TS */
> write_cr4(read_cr4() | 0x200); /* OSFXSR */
> + memset(&vv, 0, sizeof(vv));
>
> #define TEST_RW_SSE(insn) do { \
> - v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4; \
> - asm(insn " %1, %0" : "=m"(*mem) : "x"(v.sse)); \
> - report(sseeq(&v, mem), insn " (read)"); \
> - mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8; \
> - asm(insn " %1, %0" : "=x"(v.sse) : "m"(*mem)); \
> - report(sseeq(&v, mem), insn " (write)"); \
> + v[0] = 1; v[1] = 2; v[2] = 3; v[3] = 4; \
> + asm(insn " %1, %0" : "=m"(*mem) : "x"(vv) : "memory"); \
> + report(sseeq(v, mem), insn " (read)"); \
> + mem[0] = 5; mem[1] = 6; mem[2] = 7; mem[3] = 8; \
> + asm(insn " %1, %0" : "=x"(vv) : "m"(*mem) : "memory"); \
> + report(sseeq(v, mem), insn " (write)"); \
> } while (0)
>
> TEST_RW_SSE("movdqu");
> @@ -704,40 +701,41 @@ static void cross_movups_handler(struct ex_regs *regs)
>
> static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
> {
> - sse_union v;
> - sse_union *mem;
> + sse128 vv;
> + uint32_t *v = (uint32_t *)&vv;
> + uint32_t *mem;
> uint8_t *bytes = cross_mem; // aligned on PAGE_SIZE*2
> void *page2 = (void *)(&bytes[4096]);
> struct pte_search search;
> pteval_t orig_pte;
>
> // setup memory for unaligned access
> - mem = (sse_union *)(&bytes[8]);
> + mem = (uint32_t *)(&bytes[8]);
>
> // 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;
> - 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;
> - asm("movupd %1, %0" : "=m"(*mem) : "x"(v.sse));
> - report(sseeq(&v, mem), "movupd unaligned");
> + v[0] = 1; v[1] = 2; v[2] = 3; v[3] = 4;
> + mem[0] = 5; mem[1] = 6; mem[2] = 8; mem[3] = 9;
> + asm("movups %1, %0" : "=m"(*mem) : "x"(vv) : "memory");
> + report(sseeq(v, mem), "movups unaligned");
> +
> + v[0] = 1; v[1] = 2; v[2] = 3; v[3] = 4;
> + mem[0] = 5; mem[1] = 6; mem[2] = 7; mem[3] = 8;
> + asm("movupd %1, %0" : "=m"(*mem) : "x"(vv) : "memory");
> + report(sseeq(v, mem), "movupd unaligned");
> exceptions = 0;
> handle_exception(GP_VECTOR, unaligned_movaps_handler);
> asm("movaps %1, %0\n\t unaligned_movaps_cont:"
> - : "=m"(*mem) : "x"(v.sse));
> + : "=m"(*mem) : "x"(vv));
> handle_exception(GP_VECTOR, 0);
> report(exceptions == 1, "unaligned movaps exception");
>
> // 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;
> + mem = (uint32_t *)(&bytes[4096-8]);
> + v[0] = 1; v[1] = 2; v[2] = 3; v[3] = 4;
> + mem[0] = 5; mem[1] = 6; mem[2] = 7; mem[3] = 8;
>
> - asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
> - report(sseeq(&v, mem), "movups unaligned crosspage");
> + asm("movups %1, %0" : "=m"(*mem) : "x"(vv) : "memory");
> + report(sseeq(v, mem), "movups unaligned crosspage");
>
> // invalidate second page
> search = find_pte_level(current_page_table(), page2, 1);
> @@ -747,7 +745,8 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
>
> exceptions = 0;
> handle_exception(PF_VECTOR, cross_movups_handler);
> - asm("movups %1, %0\n\t cross_movups_cont:" : "=m"(*mem) : "x"(v.sse));
> + asm("movups %1, %0\n\t cross_movups_cont:" : "=m"(*mem) : "x"(vv) :
> + "memory");
> handle_exception(PF_VECTOR, 0);
> report(exceptions == 1, "movups crosspage exception");
>
> --
> 2.31.1.607.g51e8a6a459-goog
>
next prev parent reply other threads:[~2021-06-10 16:15 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-11 1:50 [kvm-unit-tests PATCH 1/2] x86: Do not assign values to unaligned pointer to 128 bits Jacob Xu
2021-05-11 1:50 ` [kvm-unit-tests PATCH 2/2] x86: remove use of compiler's memset from emulator.c Jacob Xu
2021-05-11 21:13 ` Sean Christopherson
2021-06-10 16:14 ` Jacob Xu [this message]
2021-06-10 16:55 ` [kvm-unit-tests PATCH 1/2] x86: Do not assign values to unaligned pointer to 128 bits Paolo Bonzini
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='CAJ5mJ6jUvHN5Mh_hVR5rArvA6aFNdhDCinQd1ZjLg=ht=J4ijw@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).