Skip to contentSkip to conversation

x86_emulate: Always truncate %eip out of long mode

PATCH In review Andrew Cooper 2 participants 7 messages Dec 10, 2015 – Jan 6, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86_emulate: Always truncate %eip out of long mode

_regs.eip needs to be truncated after having size added to it, or bad situations can occur. e.g. emulating an instruction which crosses the 4GB boundary causes _regs.eip to become invalid (have some of the upper 32 bits set), and fail vmentry checks when returning back to the guest.

The comment /* real hardware doesn't truncate */ seems to appear in c/s ddef8e16 "Tweak x86 emulator interface." without any justification.

I have not been able to find any information to prove or disprove the claim, but emulating oneself into a vmentry failure is definitely the wrong thing to do.

Trucate the instruction pointer at 32 or 16 bits, according to %cs.D.

---
CC: Jan Beulich <[email protected]>

v2: Use def_ad_bytes, to allow truncating to 16 bits for a 16bit code segment.

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +4 -2
index f1454ce..03e7aab 100644
@@ -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); };              \
    generate_exception_if((uint8_t)(_regs.eip -                          \
                                    ctxt->regs->eip) > MAX_INST_LEN,     \
                          EXC_GP, 0);                                    \
Signature · 1 line
2.1.4
#1 lore raw

xen/arch/x86/x86_emulate/x86_emulate.c do{ asm volatile ( · line 576

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 10.12.15 at 21:03, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +3 -2
@@ -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);                 \

Okay, what we use for actual fetching gets truncated. I'm fine with that.

xen/arch/x86/x86_emulate/x86_emulate.c do{ asm volatile ( \ · line 576

Quoted 1 line
xen/arch/x86/x86_emulate/x86_emulate.c +1 -0
+       _regs.eip &= ~((1UL << (def_ad_bytes * 8)) - 1); };              \

But I don't think it's correct to truncate what eventually gets written back. If we're in doubt what real hardware does, and if the manuals are of no help, perhaps we should ask the hardware folks?

AMD? Intel? Please clarify.

Furthermore, doesn't this make the wrapping-inside-an-insn situation worse (i.e. what looks broken for 32- and 64-bit modes now gets broken also for 16-bit mode)?

Jan

#2 lore raw
Quoted 16 lines · Jan Beulich

xen/arch/x86/x86_emulate/x86_emulate.c do{ asm volatile ( · line 576

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 10.12.15 at 21:03, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +3 -2
@@ -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);                 \

Okay, what we use for actual fetching gets truncated. I'm fine with that.

Quoted 3 lines

xen/arch/x86/x86_emulate/x86_emulate.c do{ asm volatile ( \ · line 576

Quoted 1 line
xen/arch/x86/x86_emulate/x86_emulate.c +1 -0
+       _regs.eip &= ~((1UL << (def_ad_bytes * 8)) - 1); };              \

But I don't think it's correct to truncate what eventually gets written back.

The written-back value is the one which must be truncated, to avoid a vmentry failure in the 32bit case.

The 16bit case is definitely less obvious.

Quoted 3 lines

If we're in doubt what real hardware does, and if the manuals are of no help, perhaps we should ask the hardware folks?

In 16bit segments, the 32bit %eip register does exist, but the instruction pointer is mapped to the first 16 bits of it.

The Intel manual warns: "The 8086 16-bit instruction pointer (IP) is mapped to the lower 16-bits of the EIP register. Note this register is a 32-bit register and unintentional address wrapping may occur."

I can't locate anything in particular in the AMD manuals which talks specifically about 16bit semantics.

Quoted 6 lines

AMD? Intel? Please clarify.

Furthermore, doesn't this make the wrapping-inside-an-insn situation worse (i.e. what looks broken for 32- and 64-bit modes now gets broken also for 16-bit mode)?

I don't understand which "broken" you are referring to here.

~Andrew

#3 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 11.12.15 at 12:12, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich

Furthermore, doesn't this make the wrapping-inside-an-insn situation worse (i.e. what looks broken for 32- and 64-bit modes now gets broken also for 16-bit mode)?

I don't understand which "broken" you are referring to here.

The (u8) cast on the difference of the two eip values in the subsequent instruction check hides wraps, and hence an instruction crossing (not ending at) the 4G or 16E boundary already goes undetected without your change, but your change extends the issue to a 16-bit instruction crossing the 64k boundary.

Jan

#4 lore raw
In reply to Andrew Cooper
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 10.12.15 at 21:03, <[email protected]> wrote:

xen/arch/x86/x86_emulate/x86_emulate.c do{ asm volatile ( · line 577

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -2
@@ -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.

xen/arch/x86/x86_emulate/x86_emulate.c +19 -1
@@ -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:

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.

xen/arch/x86/x86_emulate/x86_emulate.c +19 -1
@@ -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:

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw

Ping?

Quoted 77 lines
Quoted 6 lines
Quoted 2 lines · On 15.12.15 at 09:53, <[email protected]> wrote:
Quoted 1 line · On 10.12.15 at 21:03, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
@@ -570,8 +570,10 @@ do{ asm volatile (                                       
 

xen/arch/x86/x86_emulate/x86_emulate.c do{ asm volatile ( · line 578

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -2
               \
 /* 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.

xen/arch/x86/x86_emulate/x86_emulate.c +19 -1
@@ -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:
#6 lore raw
Quoted 1 line · Jan Beulich

Ping?

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

~Andrew

Quoted 78 lines
Quoted 76 lines
Quoted 6 lines
Quoted 3 lines · On 15.12.15 at 09:53, <[email protected]> wrote:
Quoted 1 line · On 10.12.15 at 21:03, <[email protected]> wrote:

xen/arch/x86/x86_emulate/x86_emulate.c do{ asm volatile ( · line 577

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -2
@@ -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.

xen/arch/x86/x86_emulate/x86_emulate.c +19 -1
@@ -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: