All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Jan Beulich <JBeulich@suse.com>
Cc: Xen-devel <xen-devel@lists.xen.org>
Subject: Re: [PATCH] x86_emulate: Always truncate %eip out of long mode
Date: Wed, 6 Jan 2016 16:09:34 +0000	[thread overview]
Message-ID: <568D3C3E.5060303@citrix.com> (raw)
In-Reply-To: <568D445102000078000C40E5@prv-mh.provo.novell.com>

On 06/01/16 15:44, Jan Beulich wrote:
> Ping?

Sorry - this is still on my todo list, but I have more urgent work
currently.

~Andrew

>
>>>> On 15.12.15 at 09:53, <JBeulich@suse.com> wrote:
>>>>> On 10.12.15 at 21:03, <andrew.cooper3@citrix.com> wrote:
>>> --- a/xen/arch/x86/x86_emulate/x86_emulate.c
>>> +++ b/xen/arch/x86/x86_emulate/x86_emulate.c
>>> @@ -570,8 +570,10 @@ do{ asm volatile (                                       
>>>                \
>>>  /* Fetch next part of the instruction being emulated. */
>>>  #define insn_fetch_bytes(_size)                                         \
>>>  ({ unsigned long _x = 0, _eip = _regs.eip;                              \
>>> -   if ( !mode_64bit() ) _eip = (uint32_t)_eip; /* ignore upper dword */ \
>>> -   _regs.eip += (_size); /* real hardware doesn't truncate */           \
>>> +   _regs.eip += (_size);                                                \
>>> +   if ( !mode_64bit() ) { /* Truncate eip to def_ad_bytes (2 or 4). */  \
>>> +       _eip      &= ~((1UL << (def_ad_bytes * 8)) - 1);                 \
>>> +       _regs.eip &= ~((1UL << (def_ad_bytes * 8)) - 1); };              \
>> Did you test this? The ~ seems quite wrong, and this would also
>> cause undefined behavior in 32-bit compilation of the emulator
>> test... Anyway, how about the following replacement patch,
>> along the lines of what I think I had described in reply to the first
>> variant of the patch?
>>
>> Jan
>>
>> x86emul: fix rIP handling
>>
>> Deal with rIP just like with any other register: Truncate to designated
>> width upon entry, and write back the zero-extended 32-bit value when
>> emulating 32-bit code, and leave the upper 48 bits unchanged for 16-bit
>> code.
>>
>> Signed-off-by: Jan Beulich <jbeulich@suse.com>
>>
>> --- a/xen/arch/x86/x86_emulate/x86_emulate.c
>> +++ b/xen/arch/x86/x86_emulate/x86_emulate.c
>> @@ -570,7 +570,6 @@ do{ asm volatile (
>>  /* Fetch next part of the instruction being emulated. */
>>  #define insn_fetch_bytes(_size)                                         \
>>  ({ unsigned long _x = 0, _eip = _regs.eip;                              \
>> -   if ( !mode_64bit() ) _eip = (uint32_t)_eip; /* ignore upper dword */ \
>>     _regs.eip += (_size); /* real hardware doesn't truncate */           \
>>     generate_exception_if((uint8_t)(_regs.eip -                          \
>>                                     ctxt->regs->eip) > MAX_INST_LEN,     \
>> @@ -1481,6 +1480,10 @@ x86_emulate(
>>  #endif
>>      }
>>  
>> +    /* Truncate rIP to def_ad_bytes (2 or 4) if necessary. */
>> +    if ( def_ad_bytes < sizeof(_regs.eip) )
>> +        _regs.eip &= (1UL << (def_ad_bytes * 8)) - 1;
>> +
>>      /* Prefix bytes. */
>>      for ( ; ; )
>>      {
>> @@ -3793,6 +3796,21 @@ x86_emulate(
>>  
>>      /* Commit shadow register state. */
>>      _regs.eflags &= ~EFLG_RF;
>> +    switch ( __builtin_expect(def_ad_bytes, sizeof(_regs.eip)) )
>> +    {
>> +        uint16_t ip;
>> +
>> +    case 2:
>> +        ip = _regs.eip;
>> +        _regs.eip = ctxt->regs->eip;
>> +        *(uint16_t *)&_regs.eip = ip;
>> +        break;
>> +#ifdef __x86_64__
>> +    case 4:
>> +        _regs.rip = _regs._eip;
>> +        break;
>> +#endif
>> +    }
>>      *ctxt->regs = _regs;
>>  
>>   done:
>
>

      reply	other threads:[~2016-01-06 16:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-10 20:03 [PATCH] x86_emulate: Always truncate %eip out of long mode Andrew Cooper
2015-12-11 10:47 ` Jan Beulich
2015-12-11 11:12   ` Andrew Cooper
2015-12-11 13:28     ` Jan Beulich
2015-12-15  8:53 ` Jan Beulich
2016-01-06 15:44   ` Jan Beulich
2016-01-06 16:09     ` Andrew Cooper [this message]

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=568D3C3E.5060303@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=xen-devel@lists.xen.org \
    /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.