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: [PATCHv2 01/23] KVM: x86 emulator: introduce read cache.
Date: Wed, 28 Apr 2010 19:15:22 +0300	[thread overview]
Message-ID: <1272471344-7076-2-git-send-email-gleb@redhat.com> (raw)
In-Reply-To: <1272471344-7076-1-git-send-email-gleb@redhat.com>

Introduce read cache which is needed for instruction that require more
then one exit to userspace. After returning from userspace the instruction
will be re-executed with cached read value.

Signed-off-by: Gleb Natapov <gleb@redhat.com>
---
 arch/x86/include/asm/kvm_emulate.h |    1 +
 arch/x86/kvm/emulate.c             |   56 +++++++++++++++++++++++++++---------
 2 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/arch/x86/include/asm/kvm_emulate.h b/arch/x86/include/asm/kvm_emulate.h
index 0b2729b..288cbed 100644
--- a/arch/x86/include/asm/kvm_emulate.h
+++ b/arch/x86/include/asm/kvm_emulate.h
@@ -186,6 +186,7 @@ struct decode_cache {
 	unsigned long modrm_val;
 	struct fetch_cache fetch;
 	struct read_cache io_read;
+	struct read_cache mem_read;
 };
 
 struct x86_emulate_ctxt {
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 5ac0bb4..6f40337 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -1263,6 +1263,33 @@ done:
 	return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
 }
 
+static int read_emulated(struct x86_emulate_ctxt *ctxt,
+			 struct x86_emulate_ops *ops,
+			 unsigned long addr, void *dest, unsigned size)
+{
+	int rc;
+	struct read_cache *mc = &ctxt->decode.mem_read;
+
+	while (size) {
+		int n = min(size, 8u);
+		size -= n;
+		if (mc->pos < mc->end)
+			goto read_cached;
+	
+		rc = ops->read_emulated(addr, mc->data + mc->end, n, ctxt->vcpu);
+		if (rc != X86EMUL_CONTINUE)
+			return rc;
+		mc->end += n;
+
+	read_cached:
+		memcpy(dest, mc->data + mc->pos, n);
+		mc->pos += n;
+		dest += n;
+		addr += n;
+	}
+	return X86EMUL_CONTINUE;
+}
+
 static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
 			   struct x86_emulate_ops *ops,
 			   unsigned int size, unsigned short port,
@@ -1504,9 +1531,9 @@ static int emulate_pop(struct x86_emulate_ctxt *ctxt,
 	struct decode_cache *c = &ctxt->decode;
 	int rc;
 
-	rc = ops->read_emulated(register_address(c, ss_base(ctxt),
-						 c->regs[VCPU_REGS_RSP]),
-				dest, len, ctxt->vcpu);
+	rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt),
+						       c->regs[VCPU_REGS_RSP]),
+			   dest, len);
 	if (rc != X86EMUL_CONTINUE)
 		return rc;
 
@@ -2475,6 +2502,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	int saved_dst_type = c->dst.type;
 
 	ctxt->interruptibility = 0;
+	ctxt->decode.mem_read.pos = 0;
 
 	/* Shadow copy of register state. Committed on successful emulation.
 	 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
@@ -2529,20 +2557,16 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	}
 
 	if (c->src.type == OP_MEM) {
-		rc = ops->read_emulated((unsigned long)c->src.ptr,
-					&c->src.val,
-					c->src.bytes,
-					ctxt->vcpu);
+		rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
+					&c->src.val, c->src.bytes);
 		if (rc != X86EMUL_CONTINUE)
 			goto done;
 		c->src.orig_val = c->src.val;
 	}
 
 	if (c->src2.type == OP_MEM) {
-		rc = ops->read_emulated((unsigned long)c->src2.ptr,
-					&c->src2.val,
-					c->src2.bytes,
-					ctxt->vcpu);
+		rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
+					&c->src2.val, c->src2.bytes);
 		if (rc != X86EMUL_CONTINUE)
 			goto done;
 	}
@@ -2553,8 +2577,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 
 	if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
 		/* optimisation - avoid slow emulated read if Mov */
-		rc = ops->read_emulated((unsigned long)c->dst.ptr, &c->dst.val,
-					c->dst.bytes, ctxt->vcpu);
+		rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
+				   &c->dst.val, c->dst.bytes);
 		if (rc != X86EMUL_CONTINUE)
 			goto done;
 	}
@@ -2981,7 +3005,11 @@ writeback:
 		    (rc->end != 0 && rc->end == rc->pos))
 			ctxt->restart = false;
 	}
-
+	/*
+	 * reset read cache here in case string instruction is restared
+	 * without decoding
+	 */
+	ctxt->decode.mem_read.end = 0;
 	/* Commit shadow register state. */
 	memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
 	kvm_rip_write(ctxt->vcpu, c->eip);
-- 
1.6.5


  reply	other threads:[~2010-04-28 16:15 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-28 16:15 [PATCHv2 00/23] next round of emulator cleanups Gleb Natapov
2010-04-28 16:15 ` Gleb Natapov [this message]
2010-04-28 16:15 ` [PATCHv2 02/23] KVM: x86 emulator: fix Move r/m16 to segment register decoding Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 03/23] KVM: x86 emulator: cleanup xchg emulation Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 04/23] KVM: x86 emulator: cleanup nop emulation Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 05/23] KVM: x86 emulator: handle "far address" source operand Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 06/23] KVM: x86 emulator: add (set|get)_dr callbacks to x86_emulate_ops Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 07/23] KVM: x86 emulator: add (set|get)_msr " Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 08/23] KVM: x86 emulator: add get_cached_segment_base() callback " Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 09/23] KVM: x86 emulator: cleanup some direct calls into kvm to use existing callbacks Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 10/23] KVM: x86 emulator: make set_cr() callback return error if it fails Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 11/23] KVM: x86 emulator: make (get|set)_dr() " Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 12/23] KVM: x86 emulator: fix X86EMUL_RETRY_INSTR and X86EMUL_CMPXCHG_FAILED values Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 13/23] KVM: fill in run->mmio details in (read|write)_emulated function Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 14/23] KVM: x86 emulator: x86_emulate_insn() return -1 only in case of emulation failure Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 15/23] KVM: remove export of emulator_write_emulated() Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 16/23] KVM: do not inject #PF in (read|write)_emulated() callbacks Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 17/23] KVM: handle emulation failure case first Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 18/23] KVM: x86 emulator: advance RIP outside x86 emulator code Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 19/23] KVM: x86 emulator: set RFLAGS " Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 20/23] KVM: x86 emulator: use shadowed register in emulate_sysexit() Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 21/23] KVM: x86 exmulator: handle shadowed registers outside emulator Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 22/23] KVM: x86 emulator: move interruptibility state tracking out of emulator Gleb Natapov
2010-04-28 16:15 ` [PATCHv2 23/23] KVM: x86 emulator: do not inject exception directly into vcpu Gleb Natapov
     [not found]   ` <446597.86519.qm@web55502.mail.re4.yahoo.com>
2010-04-29 10:17     ` qemu-kvm.0.12.2 aborts on linux Gleb Natapov
     [not found]       ` <792291.45224.qm@web55507.mail.re4.yahoo.com>
2010-05-02  5:23         ` Gleb Natapov
     [not found]           ` <710443.70603.qm@web55504.mail.re4.yahoo.com>
2010-05-03  7:44             ` Gleb Natapov
2010-04-29  9:26 ` [PATCHv2 00/23] next round of emulator cleanups Avi Kivity
2010-05-05  7:57 ` Avi Kivity

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=1272471344-7076-2-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