public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Gleb Natapov <gleb@redhat.com>
To: avi@redhat.com, mtosatti@redhat.com
Cc: kvm@vger.kernel.org
Subject: [PATCH v2 23/30] KVM: x86 emulator: add decoding of X,Y parameters from Intel SDM
Date: Sun, 14 Mar 2010 18:21:08 +0200	[thread overview]
Message-ID: <1268583675-3101-24-git-send-email-gleb@redhat.com> (raw)
In-Reply-To: <1268583675-3101-1-git-send-email-gleb@redhat.com>

Add decoding of X,Y parameters from Intel SDM which are used by string
instruction to specify source and destination. Use this new decoding
to implement movs, cmps, stos, lods in a generic way.

Signed-off-by: Gleb Natapov <gleb@redhat.com>
---
 arch/x86/kvm/emulate.c |  125 +++++++++++++++++-------------------------------
 1 files changed, 44 insertions(+), 81 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index d283fe8..12c630c 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -51,6 +51,7 @@
 #define DstReg      (2<<1)	/* Register operand. */
 #define DstMem      (3<<1)	/* Memory operand. */
 #define DstAcc      (4<<1)      /* Destination Accumulator */
+#define DstDI       (5<<1)	/* Destination is in ES:(E)DI */
 #define DstMask     (7<<1)
 /* Source operand type. */
 #define SrcNone     (0<<4)	/* No source operand. */
@@ -64,6 +65,7 @@
 #define SrcOne      (7<<4)	/* Implied '1' */
 #define SrcImmUByte (8<<4)      /* 8-bit unsigned immediate operand. */
 #define SrcImmU     (9<<4)      /* Immediate operand, unsigned */
+#define SrcSI       (0xa<<4)	/* Source is in the DS:RSI */
 #define SrcMask     (0xf<<4)
 /* Generic ModRM decode. */
 #define ModRM       (1<<8)
@@ -177,12 +179,12 @@ static u32 opcode_table[256] = {
 	/* 0xA0 - 0xA7 */
 	ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
 	ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
-	ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
-	ByteOp | ImplicitOps | String, ImplicitOps | String,
+	ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String,
+	ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String,
 	/* 0xA8 - 0xAF */
-	0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
-	ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
-	ByteOp | ImplicitOps | String, ImplicitOps | String,
+	0, 0, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
+	ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String,
+	ByteOp | DstDI | String, DstDI | String,
 	/* 0xB0 - 0xB7 */
 	ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
 	ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
@@ -1145,6 +1147,14 @@ done_prefixes:
 		c->src.bytes = 1;
 		c->src.val = 1;
 		break;
+	case SrcSI:
+		c->src.type = OP_MEM;
+		c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
+		c->src.ptr = (unsigned long *)
+			register_address(c,  seg_override_base(ctxt, c),
+					 c->regs[VCPU_REGS_RSI]);
+		c->src.val = 0;
+		break;
 	}
 
 	/*
@@ -1230,6 +1240,14 @@ done_prefixes:
 		}
 		c->dst.orig_val = c->dst.val;
 		break;
+	case DstDI:
+		c->dst.type = OP_MEM;
+		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
+		c->dst.ptr = (unsigned long *)
+			register_address(c, es_base(ctxt),
+					 c->regs[VCPU_REGS_RDI]);
+		c->dst.val = 0;
+		break;
 	}
 
 done:
@@ -2755,89 +2773,16 @@ special_insn:
 		c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
 		break;
 	case 0xa4 ... 0xa5:	/* movs */
-		c->dst.type = OP_MEM;
-		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
-		c->dst.ptr = (unsigned long *)register_address(c,
-						   es_base(ctxt),
-						   c->regs[VCPU_REGS_RDI]);
-		rc = ops->read_emulated(register_address(c,
-						seg_override_base(ctxt, c),
-						c->regs[VCPU_REGS_RSI]),
-					&c->dst.val,
-					c->dst.bytes, ctxt->vcpu);
-		if (rc != X86EMUL_CONTINUE)
-			goto done;
-		register_address_increment(c, &c->regs[VCPU_REGS_RSI],
-				       (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
-							   : c->dst.bytes);
-		register_address_increment(c, &c->regs[VCPU_REGS_RDI],
-				       (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
-							   : c->dst.bytes);
-		break;
+		goto mov;
 	case 0xa6 ... 0xa7:	/* cmps */
-		c->src.type = OP_NONE; /* Disable writeback. */
-		c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
-		c->src.ptr = (unsigned long *)register_address(c,
-				       seg_override_base(ctxt, c),
-						   c->regs[VCPU_REGS_RSI]);
-		rc = ops->read_emulated((unsigned long)c->src.ptr,
-					&c->src.val,
-					c->src.bytes,
-					ctxt->vcpu);
-		if (rc != X86EMUL_CONTINUE)
-			goto done;
-
 		c->dst.type = OP_NONE; /* Disable writeback. */
-		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
-		c->dst.ptr = (unsigned long *)register_address(c,
-						   es_base(ctxt),
-						   c->regs[VCPU_REGS_RDI]);
-		rc = ops->read_emulated((unsigned long)c->dst.ptr,
-					&c->dst.val,
-					c->dst.bytes,
-					ctxt->vcpu);
-		if (rc != X86EMUL_CONTINUE)
-			goto done;
-
 		DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
-
-		emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
-
-		register_address_increment(c, &c->regs[VCPU_REGS_RSI],
-				       (ctxt->eflags & EFLG_DF) ? -c->src.bytes
-								  : c->src.bytes);
-		register_address_increment(c, &c->regs[VCPU_REGS_RDI],
-				       (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
-								  : c->dst.bytes);
-
-		break;
+		goto cmp;
 	case 0xaa ... 0xab:	/* stos */
-		c->dst.type = OP_MEM;
-		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
-		c->dst.ptr = (unsigned long *)register_address(c,
-						   es_base(ctxt),
-						   c->regs[VCPU_REGS_RDI]);
 		c->dst.val = c->regs[VCPU_REGS_RAX];
-		register_address_increment(c, &c->regs[VCPU_REGS_RDI],
-				       (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
-							   : c->dst.bytes);
 		break;
 	case 0xac ... 0xad:	/* lods */
-		c->dst.type = OP_REG;
-		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
-		c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
-		rc = ops->read_emulated(register_address(c,
-						seg_override_base(ctxt, c),
-						c->regs[VCPU_REGS_RSI]),
-					&c->dst.val,
-					c->dst.bytes,
-					ctxt->vcpu);
-		if (rc != X86EMUL_CONTINUE)
-			goto done;
-		register_address_increment(c, &c->regs[VCPU_REGS_RSI],
-				       (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
-							   : c->dst.bytes);
-		break;
+		goto mov;
 	case 0xae ... 0xaf:	/* scas */
 		DPRINTF("Urk! I don't handle SCAS.\n");
 		goto cannot_emulate;
@@ -2980,6 +2925,24 @@ writeback:
 	if (rc != X86EMUL_CONTINUE)
 		goto done;
 
+	if ((c->d & SrcMask) == SrcSI) {
+		register_address_increment(c, &c->regs[VCPU_REGS_RSI],
+					   (ctxt->eflags & EFLG_DF) ?
+					   -c->src.bytes : c->src.bytes);
+		c->src.ptr = (unsigned long *)
+			register_address(c,  seg_override_base(ctxt, c),
+					 c->regs[VCPU_REGS_RSI]);
+	}
+
+	if ((c->d & DstMask) == DstDI) {
+		register_address_increment(c, &c->regs[VCPU_REGS_RDI],
+					   (ctxt->eflags & EFLG_DF) ?
+					   -c->dst.bytes : c->dst.bytes);
+		c->dst.ptr = (unsigned long *)
+			register_address(c, es_base(ctxt),
+					 c->regs[VCPU_REGS_RDI]);
+	}
+
 	/* Commit shadow register state. */
 	memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
 	kvm_rip_write(ctxt->vcpu, c->eip);
-- 
1.6.5


  parent reply	other threads:[~2010-03-14 16:21 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-14 16:20 [PATCH v2 00/30] emulator cleanup Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 01/30] KVM: x86 emulator: Fix DstAcc decoding Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 02/30] KVM: x86 emulator: fix RCX access during rep emulation Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 03/30] KVM: x86 emulator: check return value against correct define Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 04/30] KVM: Remove pointer to rflags from realmode_set_cr parameters Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 05/30] KVM: Provide callback to get/set control registers in emulator ops Gleb Natapov
2010-03-15 13:06   ` Andre Przywara
2010-03-15 13:11     ` Gleb Natapov
2010-03-15 13:13     ` Avi Kivity
2010-03-14 16:20 ` [PATCH v2 06/30] KVM: remove realmode_lmsw function Gleb Natapov
2010-03-15 11:02   ` Andre Przywara
2010-03-15 11:41     ` Avi Kivity
2010-03-14 16:20 ` [PATCH v2 07/30] KVM: Provide x86_emulate_ctxt callback to get current cpl Gleb Natapov
2010-03-15 13:16   ` Andre Przywara
2010-03-15 13:21     ` Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 08/30] KVM: Provide current eip as part of emulator context Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 09/30] KVM: x86 emulator: fix mov r/m, sreg emulation Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 10/30] KVM: x86 emulator: fix 0f 01 /5 emulation Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 11/30] KVM: x86 emulator: 0f (20|21|22|23) ignore mod bits Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 12/30] KVM: x86 emulator: inject #UD on access to non-existing CR Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 13/30] KVM: x86 emulator: fix mov dr to inject #UD when needed Gleb Natapov
2010-03-14 16:20 ` [PATCH v2 14/30] KVM: x86 emulator: fix return values of syscall/sysenter/sysexit emulations Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 15/30] KVM: x86 emulator: do not call writeback if msr access fails Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 16/30] KVM: x86 emulator: If LOCK prefix is used dest arg should be memory Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 17/30] KVM: x86 emulator: cleanup grp3 return value Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 18/30] KVM: x86 emulator: Provide more callbacks for x86 emulator Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 19/30] KVM: x86 emulator: Emulate task switch in emulator.c Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 20/30] KVM: x86 emulator: Use load_segment_descriptor() instead of kvm_load_segment_descriptor() Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 21/30] KVM: Use task switch from emulator.c Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 22/30] KVM: x86 emulator: populate OP_MEM operand during decoding Gleb Natapov
2010-03-14 16:21 ` Gleb Natapov [this message]
2010-03-14 16:21 ` [PATCH v2 24/30] KVM: x86 emulator: during rep emulation decrement ECX only if emulation succeeded Gleb Natapov
2010-03-14 16:42   ` Avi Kivity
2010-03-14 16:21 ` [PATCH v2 25/30] KVM: x86 emulator: fix in/out emulation Gleb Natapov
2010-03-14 16:54   ` Avi Kivity
2010-03-14 17:35     ` Gleb Natapov
2010-03-15  7:41       ` Avi Kivity
2010-03-15  7:44         ` Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 26/30] KVM: x86 emulator: Move string pio emulation into emulator.c Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 27/30] KVM: x86 emulator: remove saved_eip Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 28/30] KVM: x86 emulator: restart string instruction without going back to a guest Gleb Natapov
2010-03-14 16:56   ` Avi Kivity
2010-03-14 18:06     ` Gleb Natapov
2010-03-15  7:44       ` Avi Kivity
2010-03-15  9:44         ` Gleb Natapov
2010-03-15  9:56           ` Avi Kivity
2010-03-15 10:07             ` Gleb Natapov
2010-03-15 10:15               ` Avi Kivity
2010-03-15 10:19                 ` Gleb Natapov
2010-03-15 10:24                   ` Avi Kivity
2010-03-15 10:33                     ` Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 29/30] KVM: x86 emulator: introduce pio in string read ahead Gleb Natapov
2010-03-14 16:21 ` [PATCH v2 30/30] KVM: small kvm_arch_vcpu_ioctl_run() cleanup Gleb Natapov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1268583675-3101-24-git-send-email-gleb@redhat.com \
    --to=gleb@redhat.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox