Skip to content

Commit

Permalink
KVM: x86 emulator: fix negative bit offset BitOp instruction emulation
Browse files Browse the repository at this point in the history
If bit offset operands is a negative number, BitOp instruction
will return wrong value. This patch fix it.

Signed-off-by: Wei Yongjun <yjwei@cn.fujitsu.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
  • Loading branch information
Wei Yongjun authored and Avi Kivity committed Oct 24, 2010
1 parent 8744aa9 commit 35c843c
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions arch/x86/kvm/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,22 @@ static int decode_abs(struct x86_emulate_ctxt *ctxt,
return rc;
}

static void fetch_bit_operand(struct decode_cache *c)
{
long sv, mask;

if (c->dst.type == OP_MEM) {
mask = ~(c->dst.bytes * 8 - 1);

if (c->src.bytes == 2)
sv = (s16)c->src.val & (s16)mask;
else if (c->src.bytes == 4)
sv = (s32)c->src.val & (s32)mask;

c->dst.addr.mem += (sv >> 3);
}
}

static int read_emulated(struct x86_emulate_ctxt *ctxt,
struct x86_emulate_ops *ops,
unsigned long addr, void *dest, unsigned size)
Expand Down Expand Up @@ -2638,12 +2654,8 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt)
c->dst.bytes = 8;
else
c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
if (c->dst.type == OP_MEM && (c->d & BitOp)) {
unsigned long mask = ~(c->dst.bytes * 8 - 1);

c->dst.addr.mem = c->dst.addr.mem +
(c->src.val & mask) / 8;
}
if (c->d & BitOp)
fetch_bit_operand(c);
c->dst.orig_val = c->dst.val;
break;
case DstAcc:
Expand Down

0 comments on commit 35c843c

Please sign in to comment.