kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jim Mattson <jmattson@google.com>
To: Bill Wendling <morbo@google.com>
Cc: kvm list <kvm@vger.kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	alexandru.elisei@arm.com
Subject: Re: [kvm-unit-tests PATCH 2/2] x86: realmode: use inline asm to get stack pointer
Date: Wed, 16 Oct 2019 14:52:28 -0700	[thread overview]
Message-ID: <CALMp9eTGd6MWdePCfwG5QBLpfmVoTg8XGH55MkXxzfa=biG1WA@mail.gmail.com> (raw)
In-Reply-To: <CALMp9eSK_O24gYg6J7U-eL1Lq4Y=YaXSaQVZhXs+1RSM+h83ew@mail.gmail.com>

On Wed, Oct 16, 2019 at 12:53 PM Jim Mattson <jmattson@google.com> wrote:
>
> On Sat, Oct 12, 2019 at 4:59 PM Bill Wendling <morbo@google.com> wrote:
> >
> > It's fragile to try to retrieve the stack pointer by taking the address
> > of a variable on the stack. For instance, clang reserves more stack
> > space than gcc here, indicating that the variable may not be at the
> > start of the stack. Instead of relying upon this to work, retrieve the
> > "%rbp" value, which contains the value of "%rsp" before stack
> > allocation.
> >
> > Signed-off-by: Bill Wendling <morbo@google.com>
> > ---
> >  x86/realmode.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/x86/realmode.c b/x86/realmode.c
> > index cf45fd6..7c89dd1 100644
> > --- a/x86/realmode.c
> > +++ b/x86/realmode.c
> > @@ -518,11 +518,12 @@ extern void retf_imm(void);
> >
> >  static void test_call(void)
> >  {
> > -       u32 esp[16];
> >         u32 addr;
> >
> >         inregs = (struct regs){ 0 };
> > -       inregs.esp = (u32)esp;
> > +
> > +       // At this point the original stack pointer is in %ebp.
> > +       asm volatile ("mov %%ebp, %0" : "=rm"(inregs.esp));
>
> I don't think we should assume the existence of frame pointers.
> Moreover, I think %esp is actually the value that should be saved
> here, regardless of how large the current frame is.

Never mind. After taking a closer look, esp[] is meant to provide
stack space for the code under test, but inregs.esp should point to
the top of this stack rather than the bottom. This is apparently a
long-standing bug, similar to the one Avi fixed for  test_long_jmp()
in commit 4aa22949 ("realmode: fix esp in long jump test").

For consistency with test_long_jmp, I'd suggest changing the
inregs.esp assignment to:
       inregs.esp = (u32)(esp+16);

Note that you absolutely must preserve the esp[] array!

> >         MK_INSN(call1, "mov $test_function, %eax \n\t"
> >                        "call *%eax\n\t");
> > --
> > 2.23.0.700.g56cf767bdb-goog
> >

  reply	other threads:[~2019-10-16 21:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-12 23:58 [kvm-unit-tests PATCH 0/2] realmode test fixes for clang Bill Wendling
2019-10-12 23:58 ` [kvm-unit-tests PATCH 1/2] x86: realmode: explicitly copy structure to avoid memcpy Bill Wendling
2019-10-16 19:07   ` Jim Mattson
2019-10-21 15:38   ` Paolo Bonzini
2019-10-12 23:58 ` [kvm-unit-tests PATCH 2/2] x86: realmode: use inline asm to get stack pointer Bill Wendling
2019-10-16 19:53   ` Jim Mattson
2019-10-16 21:52     ` Jim Mattson [this message]
2019-10-21 15:41       ` Paolo Bonzini
2019-10-17  1:25 ` [kvm-unit-tests v2 PATCH 0/2] realmode test fixes for clang Bill Wendling
2019-10-17  1:25   ` [kvm-unit-tests v2 PATCH 1/2] x86: realmode: explicitly copy regs structure Bill Wendling
2019-10-17  1:25   ` [kvm-unit-tests v2 PATCH 2/2] x86: realmode: fix esp in call test Bill Wendling
2019-10-17 23:27     ` Jim Mattson
2019-10-21 15:43     ` Paolo Bonzini
2019-10-21 16:43       ` Jim Mattson

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='CALMp9eTGd6MWdePCfwG5QBLpfmVoTg8XGH55MkXxzfa=biG1WA@mail.gmail.com' \
    --to=jmattson@google.com \
    --cc=alexandru.elisei@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=morbo@google.com \
    --cc=pbonzini@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).