linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <sean.j.christopherson@intel.com>
To: Jim Mattson <jmattson@google.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>,
	kvm list <kvm@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH] selftests: kvm: fix sync_regs_test with newer gccs
Date: Tue, 8 Oct 2019 11:40:07 -0700	[thread overview]
Message-ID: <20191008184007.GG14020@linux.intel.com> (raw)
In-Reply-To: <CALMp9eTqu2zff2g4pX-aR3P-AFNochKfJGZrA6zecE864FuwVQ@mail.gmail.com>

On Tue, Oct 08, 2019 at 11:29:32AM -0700, Jim Mattson wrote:
> On Tue, Oct 8, 2019 at 11:08 AM Vitaly Kuznetsov <vkuznets@redhat.com> wrote:
> >
> > Commit 204c91eff798a ("KVM: selftests: do not blindly clobber registers in
> >  guest asm") was intended to make test more gcc-proof, however, the result
> > is exactly the opposite: on newer gccs (e.g. 8.2.1) the test breaks with
> >
> > ==== Test Assertion Failure ====
> >   x86_64/sync_regs_test.c:168: run->s.regs.regs.rbx == 0xBAD1DEA + 1
> >   pid=14170 tid=14170 - Invalid argument
> >      1  0x00000000004015b3: main at sync_regs_test.c:166 (discriminator 6)
> >      2  0x00007f413fb66412: ?? ??:0
> >      3  0x000000000040191d: _start at ??:?
> >   rbx sync regs value incorrect 0x1.
> >
> > Apparently, compile is still free to play games with registers even
> > when they have variables attaches.
> >
> > Re-write guest code with 'asm volatile' by embedding ucall there and
> > making sure rbx is preserved.
> >
> > Fixes: 204c91eff798a ("KVM: selftests: do not blindly clobber registers in guest asm")
> > Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> > ---
> >  .../selftests/kvm/x86_64/sync_regs_test.c     | 21 ++++++++++---------
> >  1 file changed, 11 insertions(+), 10 deletions(-)
> >
> > diff --git a/tools/testing/selftests/kvm/x86_64/sync_regs_test.c b/tools/testing/selftests/kvm/x86_64/sync_regs_test.c
> > index 11c2a70a7b87..5c8224256294 100644
> > --- a/tools/testing/selftests/kvm/x86_64/sync_regs_test.c
> > +++ b/tools/testing/selftests/kvm/x86_64/sync_regs_test.c
> > @@ -22,18 +22,19 @@
> >
> >  #define VCPU_ID 5
> >
> > +#define UCALL_PIO_PORT ((uint16_t)0x1000)
> > +
> > +/*
> > + * ucall is embedded here to protect against compiler reshuffling registers
> > + * before calling a function. In this test we only need to get KVM_EXIT_IO
> > + * vmexit and preserve RBX, no additional information is needed.
> > + */
> >  void guest_code(void)
> >  {
> > -       /*
> > -        * use a callee-save register, otherwise the compiler
> > -        * saves it around the call to GUEST_SYNC.
> > -        */
> > -       register u32 stage asm("rbx");
> > -       for (;;) {
> > -               GUEST_SYNC(0);
> > -               stage++;
> > -               asm volatile ("" : : "r" (stage));
> > -       }
> > +       asm volatile("1: in %[port], %%al\n"
> > +                    "add $0x1, %%rbx\n"
> > +                    "jmp 1b"
> > +                    : : [port] "d" (UCALL_PIO_PORT) : "rax", "rbx");
> >  }
> A better solution might be something like:
> 
> register u32 stage = 0;
> for (;;) {
>         asm volatile("in %[port], %%al"
>              :
>              : "b" (stage), [port] "d" (UCALL_PIO_PORT)
>              : "rax");
>         stage++;
> }
> 
> (Gmail no doubt has mangled the indentation. Sorry.)

The incoming value of rbx matters, I believe it's set to 0xBAD1DEA and the
host then expects 0xBAD1DEA + 1.

  reply	other threads:[~2019-10-08 18:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-08 18:08 [PATCH] selftests: kvm: fix sync_regs_test with newer gccs Vitaly Kuznetsov
2019-10-08 18:29 ` Jim Mattson
2019-10-08 18:40   ` Sean Christopherson [this message]
2019-10-08 18:36 ` Sean Christopherson
2019-10-09  9:42   ` Paolo Bonzini
2019-10-09 10:42     ` Vitaly Kuznetsov
2019-10-09 11:11       ` Paolo Bonzini
2019-10-09 12:29         ` Vitaly Kuznetsov
2019-10-09 16:22         ` Sean Christopherson

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=20191008184007.GG14020@linux.intel.com \
    --to=sean.j.christopherson@intel.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=vkuznets@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).