Skip to contentSkip to conversation

x86_emulate: Always truncate %eip in 32bit mode

PATCH In review Andrew Cooper 2 participants 2 messages Nov 30, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86_emulate: Always truncate %eip in 32bit mode

_regs.eip needs to be truncated after having size added to it, or emulating an instruction which crosses the 4GB boundary causes _regs.eip to become invalid, 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, and have not experimented to find the behaviour of real hardware, but emulating oneself into a vmentry failure is definitely the wrong thing to do.

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

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +3 -2
index f1454ce..56328f4 100644
@@ -570,8 +570,9 @@ 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 in 32bit mode. */             \
+       _eip = (uint32_t)_eip; _regs.eip = (uint32_t) _regs.eip; }       \
    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
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 30.11.15 at 12:07, <[email protected]> wrote:

_regs.eip needs to be truncated after having size added to it, or emulating an instruction which crosses the 4GB boundary causes _regs.eip to become invalid, 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.

Considering how the code looked like before this commit, ...

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

Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.c +3 -2
@@ -570,8 +570,9 @@ 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 in 32bit mode. */             \
+       _eip = (uint32_t)_eip; _regs.eip = (uint32_t) _regs.eip; }       \

... don't you think we would better switch back to _register_address_increment()? Afaik in a 16-bit code segment only the lower 16 bits actually get looked at.

Jan