Skip to content

Commit 37c564f

Browse files
anadavbonzini
authored andcommitted
KVM: x86: cmpxchg emulation should compare in reverse order
The current implementation of cmpxchg does not update the flags correctly, since the accumulator should be compared with the destination and not the other way around. The current implementation does not update the flags correctly. Signed-off-by: Nadav Amit <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 606b1c3 commit 37c564f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

arch/x86/kvm/emulate.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2052,8 +2052,10 @@ static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
20522052
static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
20532053
{
20542054
/* Save real source value, then compare EAX against destination. */
2055+
ctxt->dst.orig_val = ctxt->dst.val;
2056+
ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
20552057
ctxt->src.orig_val = ctxt->src.val;
2056-
ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
2058+
ctxt->src.val = ctxt->dst.orig_val;
20572059
fastop(ctxt, em_cmp);
20582060

20592061
if (ctxt->eflags & EFLG_ZF) {
@@ -2063,6 +2065,7 @@ static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
20632065
/* Failure: write the value we saw to EAX. */
20642066
ctxt->dst.type = OP_REG;
20652067
ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2068+
ctxt->dst.val = ctxt->dst.orig_val;
20662069
}
20672070
return X86EMUL_CONTINUE;
20682071
}

0 commit comments

Comments
 (0)