• Nadav Amit's avatar
    KVM: x86: Fix zero iterations REP-string · 428e3d08
    Nadav Amit authored
    When a REP-string is executed in 64-bit mode with an address-size prefix,
    ECX/EDI/ESI are used as counter and pointers. When ECX is initially zero, Intel
    CPUs clear the high 32-bits of RCX, and recent Intel CPUs update the high bits
    of the pointers in MOVS/STOS. This behavior is specific to Intel according to
    few experiments.
    
    As one may guess, this is an undocumented behavior. Yet, it is observable in
    the guest, since at least VMX traps REP-INS/OUTS even when ECX=0. Note that
    VMware appears to get it right.  The behavior can be observed using the
    following code:
    
     #include <stdio.h>
    
     #define LOW_MASK	(0xffffffff00000000ull)
     #define ALL_MASK	(0xffffffffffffffffull)
     #define TEST(opcode)							\
    	do {								\
    	asm volatile(".byte 0xf2 \n\t .byte 0x67 \n\t .byte " opcode "\n\t" \
    			: "=S"(s), "=c"(c), "=D"(d) 			\
    			: "S"(ALL_MASK), "c"(LOW_MASK), "D"(ALL_MASK));	\
    	printf("opcode %s rcx=%llx rsi=%llx rdi=%llx\n",		\
    		opcode, c, s, d);					\
    	} while(0)
    
    void main()
    {
    	unsigned long long s, d, c;
    	iopl(3);
    	TEST("0x6c");
    	TEST("0x6d");
    	TEST("0x6e");
    	TEST("0x6f");
    	TEST("0xa4");
    	TEST("0xa5");
    	TEST("0xa6");
    	TEST("0xa7");
    	TEST("0xaa");
    	TEST("0xab");
    	TEST("0xae");
    	TEST("0xaf");
    }
    Signed-off-by: default avatarNadav Amit <namit@cs.technion.ac.il>
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    428e3d08
emulate.c 133 KB