public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Takuya Yoshikawa <takuya.yoshikawa@gmail.com>
To: avi@redhat.com, mtosatti@redhat.com
Cc: kvm@vger.kernel.org, yoshikawa.takuya@oss.ntt.co.jp
Subject: [PATCH 3/4] KVM: x86 emulator: Make opcode decoding a separate function
Date: Tue, 24 Apr 2012 00:35:30 +0900	[thread overview]
Message-ID: <20120424003530.bea97feae39541256803cee6@gmail.com> (raw)
In-Reply-To: <20120424003159.4fd245ec18b0b3eeddbea553@gmail.com>

From: Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp>

This is the second part of the instruction decoding which treats the
opcode.

Signed-off-by: Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp>
Cc: Takuya Yoshikawa <takuya.yoshikawa@gmail.com>
---
 arch/x86/kvm/emulate.c |   66 +++++++++++++++++++++++++++++++----------------
 1 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index b22238b..e87570e 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -3964,32 +3964,28 @@ done:
 	return rc;
 }
 
-int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
+/**
+ * decode_opcode - decode opcode
+ * @ctxt: emulation context
+ *
+ * Decodes opcode bytes and reads opcode from table.
+ *
+ * Returns X86EMUL_CONTINUE on success.
+ */
+static int decode_opcode(struct x86_emulate_ctxt *ctxt)
 {
 	int rc = X86EMUL_CONTINUE;
 	int goffset, simd_prefix;
 	struct opcode opcode;
 
-	ctxt->memop.type = OP_NONE;
-	ctxt->memopp = NULL;
-	ctxt->_eip = ctxt->eip;
-	ctxt->fetch.start = ctxt->_eip;
-	ctxt->fetch.end = ctxt->fetch.start + insn_len;
-	if (insn_len > 0)
-		memcpy(ctxt->fetch.data, insn, insn_len);
-
-	rc = decode_prefixes(ctxt);
-	if (rc != X86EMUL_CONTINUE)
-		goto done;
-
-	/* Opcode byte(s). */
-	opcode = opcode_table[ctxt->b];
 	/* Two-byte opcode? */
 	if (ctxt->b == 0x0f) {
 		ctxt->twobyte = 1;
 		ctxt->b = insn_fetch(u8, ctxt);
 		opcode = twobyte_table[ctxt->b];
-	}
+	} else
+		opcode = opcode_table[ctxt->b];
+
 	ctxt->d = opcode.flags;
 
 	while (ctxt->d & GroupMask) {
@@ -4015,7 +4011,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
 			break;
 		case Prefix:
 			if (ctxt->rep_prefix && ctxt->op_prefix)
-				return EMULATION_FAILED;
+				return X86EMUL_UNHANDLEABLE;
 			simd_prefix = ctxt->rep_prefix | ctxt->op_prefix;
 			switch (simd_prefix) {
 			case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
@@ -4025,23 +4021,47 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
 			}
 			break;
 		default:
-			return EMULATION_FAILED;
+			return X86EMUL_UNHANDLEABLE;
 		}
 
 		ctxt->d &= ~(u64)GroupMask;
 		ctxt->d |= opcode.flags;
 	}
 
+	/* Unrecognised? */
+	if (ctxt->d == 0 || (ctxt->d & Undefined))
+		return X86EMUL_UNHANDLEABLE;
+
+	if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
+		return X86EMUL_UNHANDLEABLE;
+
 	ctxt->execute = opcode.u.execute;
 	ctxt->check_perm = opcode.check_perm;
 	ctxt->intercept = opcode.intercept;
 
-	/* Unrecognised? */
-	if (ctxt->d == 0 || (ctxt->d & Undefined))
-		return EMULATION_FAILED;
+done:
+	return rc;
+}
 
-	if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
-		return EMULATION_FAILED;
+int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
+{
+	int rc = X86EMUL_CONTINUE;
+
+	ctxt->memop.type = OP_NONE;
+	ctxt->memopp = NULL;
+	ctxt->_eip = ctxt->eip;
+	ctxt->fetch.start = ctxt->_eip;
+	ctxt->fetch.end = ctxt->fetch.start + insn_len;
+	if (insn_len > 0)
+		memcpy(ctxt->fetch.data, insn, insn_len);
+
+	rc = decode_prefixes(ctxt);
+	if (rc != X86EMUL_CONTINUE)
+		goto done;
+
+	rc = decode_opcode(ctxt);
+	if (rc != X86EMUL_CONTINUE)
+		goto done;
 
 	if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
 		ctxt->op_bytes = 8;
-- 
1.7.5.4


  parent reply	other threads:[~2012-04-23 15:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-23 15:31 [PATCH 0/4] KVM: x86 emulator: Split decoder into separate functions Takuya Yoshikawa
2012-04-23 15:33 ` [PATCH 1/4] KVM: x86 emulator: Introduce ctxt->op_prefix for 0x66 prefix Takuya Yoshikawa
2012-04-23 15:34 ` [PATCH 2/4] KVM: x86 emulator: Make prefix decoding a separate function Takuya Yoshikawa
2012-04-23 15:35 ` Takuya Yoshikawa [this message]
2012-04-23 15:37 ` [PATCH 4/4] KVM: x86 emulator: Avoid pushing back ModRM byte in decode_opcode() Takuya Yoshikawa
2012-04-24 14:10   ` Avi Kivity
2012-04-24 14:27     ` Takuya Yoshikawa
2012-04-24 14:11 ` [PATCH 0/4] KVM: x86 emulator: Split decoder into separate functions Avi Kivity
2012-04-24 14:41   ` Takuya Yoshikawa

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=20120424003530.bea97feae39541256803cee6@gmail.com \
    --to=takuya.yoshikawa@gmail.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=yoshikawa.takuya@oss.ntt.co.jp \
    /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