All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nadav Amit <nadav.amit@gmail.com>
To: "Wu, Feng" <feng.wu@intel.com>
Cc: "Chen, Tiejun" <tiejun.chen@intel.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	kvm list <kvm@vger.kernel.org>
Subject: Re: [PATCH 2/8] KVM: x86: pop sreg accesses only 2 bytes
Date: Sat, 27 Dec 2014 22:05:25 +0200	[thread overview]
Message-ID: <C638C1F8-A25F-4DAA-AA26-F726BD3AEFE3@gmail.com> (raw)
In-Reply-To: <E959C4978C3B6342920538CF579893F002318985@SHSMSX104.ccr.corp.intel.com>

Feng <feng.wu@intel.com> wrote:

> 
> 
>> -----Original Message-----
>> From: kvm-owner@vger.kernel.org [mailto:kvm-owner@vger.kernel.org] On
>> Behalf Of Nadav Amit
>> Sent: Thursday, December 25, 2014 5:55 PM
>> To: Chen, Tiejun
>> Cc: Paolo Bonzini; kvm list
>> Subject: Re: [PATCH 2/8] KVM: x86: pop sreg accesses only 2 bytes
>> 
>> Tiejun <tiejun.chen@intel.com> wrote:
>> 
>>> On 2014/12/25 8:52, Nadav Amit wrote:
>>>> Although pop sreg updates RSP according to the operand size, only 2 bytes
>> are
>>>> read.  The current behavior may result in incorrect #GP or #PF exceptions.
>>>> 
>>>> Signed-off-by: Nadav Amit <namit@cs.technion.ac.il>
>>>> ---
>>>> arch/x86/kvm/emulate.c | 4 +++-
>>>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>>> 
>>>> diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
>>>> index e5a84be..702da5e 100644
>>>> --- a/arch/x86/kvm/emulate.c
>>>> +++ b/arch/x86/kvm/emulate.c
>>>> @@ -1830,12 +1830,14 @@ static int em_pop_sreg(struct
>> x86_emulate_ctxt *ctxt)
>>>> unsigned long selector;
>>>> 	int rc;
>>> 
>>> Looks we just should do similar thing to em_push_sreg(),
>>> 
>>>       unsigned long selector;
>>>       int rc;
>>> 
>>> +       if (ctxt->op_bytes == 4) {
>>> +               rsp_increment(ctxt, -2);
>>> +               ctxt->op_bytes = 2;
>>> +       }
>>>       rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
>>>       if (rc != X86EMUL_CONTINUE)
>>>               return rc;
>>> 
>>> Right?
>> I don't think so. It seems the behaviour of push and pop is a bit different.
>> For push: "If the source operand is a segment register (16 bits) and the
>> operand size is 64-bits, a zero-extended value is pushed on the stack; if
>> the operand size is 32-bits ... all recent Core and Atom processors perform
>> a 16-bit move, leaving the upper portion of the stack location unmodified."
>> 
>> Therefore, for push in the case of op_bytes==8 we push zero-extended value.
>> 
>> For pop the behaviour is not well-documented, but experimentally it appears
>> only the first two bytes are accessed. I cannot see why it would be
>> different when opsize is 8, since it is not like the push case, where the
>> segment register value was zero extended.
> 
> Let's take 64-bits operand size as an example. When pushing a segment register, it
> uses zero-extended value, so 8 bytes will be pushed on the stack. When popping it,
> the current code return the top 8 bytes in the stack, and it only uses the lowest 2
> bytes for load_segment_descriptor(). what is the issue here?
The issue I try to solve is that during the emulated write operation of the
pop the read is perform using the wrong size (operand size instead of
segment selector size). As you indicated, the destination register/memory of
the pop instruction will be identical before the fix and after the fix.

However, the emulated read may cause #PF if the operand-size that does not
occur on read hardware. Consider for instance a case in which the operand
size is 8, RSP=0xFFC and the page of [0x1000] is non-present. In this case
POP-SREG should not cause a #PF, yet on KVM it does.

Nadav


  reply	other threads:[~2014-12-27 20:05 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-25  0:52 [PATCH 0/8] KVM: x86: Emulator fixes Nadav Amit
2014-12-25  0:52 ` [PATCH 1/8] KVM: x86: #PF error-code on R/W operations is wrong Nadav Amit
2014-12-26  7:25   ` Wu, Feng
2014-12-27 19:55     ` Nadav Amit
2014-12-25  0:52 ` [PATCH 2/8] KVM: x86: pop sreg accesses only 2 bytes Nadav Amit
2014-12-25  9:10   ` Chen, Tiejun
2014-12-25  9:55     ` Nadav Amit
2014-12-26  1:54       ` Chen, Tiejun
2014-12-26  7:25       ` Wu, Feng
2014-12-27 20:05         ` Nadav Amit [this message]
2014-12-25  0:52 ` [PATCH 3/8] KVM: x86: fnstcw and fnstsw may cause spurious exception Nadav Amit
2014-12-25  0:52 ` [PATCH 4/8] KVM: x86: JMP/CALL using call- or task-gate causes exception Nadav Amit
2014-12-25  0:52 ` [PATCH 5/8] KVM: x86: em_call_far should return failure result Nadav Amit
2014-12-25  0:52 ` [PATCH 6/8] KVM: x86: POP [ESP] is not emulated correctly Nadav Amit
2014-12-25  0:52 ` [PATCH 7/8] KVM: x86: Do not set access bit on accessed segments Nadav Amit
2014-12-25  0:52 ` [PATCH 8/8] KVM: x86: Access to LDT/GDT that wraparound is incorrect Nadav Amit
2014-12-27 22:24 ` [PATCH 0/8] KVM: x86: Emulator fixes Paolo Bonzini
2015-01-08 10:42 ` 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=C638C1F8-A25F-4DAA-AA26-F726BD3AEFE3@gmail.com \
    --to=nadav.amit@gmail.com \
    --cc=feng.wu@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=tiejun.chen@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.