Skip to contentSkip to conversation

x86_emulate: fix emulation of CMPXCHG

PATCH In review Eugene Korenevsky 2 participants 2 messages Apr 29, 2015 – May 1, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86_emulate: fix emulation of CMPXCHG

CMPXCHG sets CF, PF, AF, SF, and OF flags according to the results of the comparison the rAX with the operand of the instruction.
rAX must be the first argument of the comparison (a minuend), the operand must be the second one (a subtrahend).

Due to improper order of comparison arguments, CF, PF, AF, SF and OF flags were set incorrectly in the case of inequality. Need to swap them.

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
index ae32c82..7532734 100644
@@ -4343,7 +4343,8 @@ x86_emulate(
         /* Save real source value, then compare EAX against destination. */
         src.orig_val = src.val;
         src.val = _regs.eax;
-        emulate_2op_SrcV("cmp", src, dst, _regs.eflags);
+        /* EAX must be a minuend, dst must be a subtrahend. */
+        emulate_2op_SrcV("cmp", dst, src, _regs.eflags);
         if ( _regs.eflags & EFLG_ZF )
         {
             /* Success: write back to memory. */
Signature · 1 line
2.0.5
#1 lore raw

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 4347

Quoted 10 lines
Quoted 1 line
Quoted 1 line

Eugene Korenevsky <[email protected]> 04/28/15 11:54 PM >>>

xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
@@ -4343,7 +4343,8 @@ x86_emulate(
 /* Save real source value, then compare EAX against destination. */
 src.orig_val = src.val;
 src.val = _regs.eax;
-        emulate_2op_SrcV("cmp", src, dst, _regs.eflags);
+        /* EAX must be a minuend, dst must be a subtrahend. */
+        emulate_2op_SrcV("cmp", dst, src, _regs.eflags);

Thanks, operands indeed need to be the other way around. The comment, however, doesn't really look that helpful to me, and hence I intend to alter it before committing (also getting it closer to what CMPS and SCAS have).

Should you submit further patches in the future, please also remember to Cc relevant maintainers.

Jan