KVM: x86 emulator: add support for writing back the source operand

Some instructions write back the source operand, not just the destination.
Add support for doing this via the decode flags.

Gleb: add BUG_ON() to prevent source to be memory operand.

Signed-off-by: Avi Kivity <avi.kivity@gmail.com>
Signed-off-by: Gleb Natapov <gleb@redhat.com>
This commit is contained in:
Avi Kivity 2013-02-09 11:31:44 +02:00 коммит произвёл Gleb Natapov
Родитель f8b5ff2cff
Коммит fb32b1eda2
1 изменённых файлов: 27 добавлений и 21 удалений

Просмотреть файл

@ -155,6 +155,7 @@
#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
#define NoWrite ((u64)1 << 45) /* No writeback */ #define NoWrite ((u64)1 << 45) /* No writeback */
#define SrcWrite ((u64)1 << 46) /* Write back src operand */
#define X2(x...) x, x #define X2(x...) x, x
#define X3(x...) X2(x), x #define X3(x...) X2(x), x
@ -1723,45 +1724,42 @@ static void write_register_operand(struct operand *op)
} }
} }
static int writeback(struct x86_emulate_ctxt *ctxt) static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
{ {
int rc; int rc;
if (ctxt->d & NoWrite) switch (op->type) {
return X86EMUL_CONTINUE;
switch (ctxt->dst.type) {
case OP_REG: case OP_REG:
write_register_operand(&ctxt->dst); write_register_operand(op);
break; break;
case OP_MEM: case OP_MEM:
if (ctxt->lock_prefix) if (ctxt->lock_prefix)
rc = segmented_cmpxchg(ctxt, rc = segmented_cmpxchg(ctxt,
ctxt->dst.addr.mem, op->addr.mem,
&ctxt->dst.orig_val, &op->orig_val,
&ctxt->dst.val, &op->val,
ctxt->dst.bytes); op->bytes);
else else
rc = segmented_write(ctxt, rc = segmented_write(ctxt,
ctxt->dst.addr.mem, op->addr.mem,
&ctxt->dst.val, &op->val,
ctxt->dst.bytes); op->bytes);
if (rc != X86EMUL_CONTINUE) if (rc != X86EMUL_CONTINUE)
return rc; return rc;
break; break;
case OP_MEM_STR: case OP_MEM_STR:
rc = segmented_write(ctxt, rc = segmented_write(ctxt,
ctxt->dst.addr.mem, op->addr.mem,
ctxt->dst.data, op->data,
ctxt->dst.bytes * ctxt->dst.count); op->bytes * op->count);
if (rc != X86EMUL_CONTINUE) if (rc != X86EMUL_CONTINUE)
return rc; return rc;
break; break;
case OP_XMM: case OP_XMM:
write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm); write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
break; break;
case OP_MM: case OP_MM:
write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm); write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
break; break;
case OP_NONE: case OP_NONE:
/* no writeback */ /* no writeback */
@ -4769,9 +4767,17 @@ special_insn:
goto done; goto done;
writeback: writeback:
rc = writeback(ctxt); if (!(ctxt->d & NoWrite)) {
rc = writeback(ctxt, &ctxt->dst);
if (rc != X86EMUL_CONTINUE) if (rc != X86EMUL_CONTINUE)
goto done; goto done;
}
if (ctxt->d & SrcWrite) {
BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
rc = writeback(ctxt, &ctxt->src);
if (rc != X86EMUL_CONTINUE)
goto done;
}
/* /*
* restore dst type in case the decoding will be reused * restore dst type in case the decoding will be reused