mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Avi Kivity <avi@qumranet.com>
To: linux-kernel@vger.kernel.org, kvm-devel@lists.sourceforge.net
Subject: [PATCH 28/55] KVM: x86 emulator: centralize decoding of one-byte register access insns
Date: Wed, 26 Dec 2007 13:05:33 +0200	[thread overview]
Message-ID: <1198667160-22953-29-git-send-email-avi@qumranet.com> (raw)
In-Reply-To: <1198667160-22953-1-git-send-email-avi@qumranet.com>

Instructions like 'inc reg' that have the register operand encoded
in the opcode are currently specially decoded.  Extend
decode_register_operand() to handle that case, indicated by having
DstReg or SrcReg without ModRM.

Signed-off-by: Avi Kivity <avi@qumranet.com>
---
 drivers/kvm/x86_emulate.c |  103 ++++++++++++++++++++-------------------------
 drivers/kvm/x86_emulate.h |    1 +
 2 files changed, 47 insertions(+), 57 deletions(-)

diff --git a/drivers/kvm/x86_emulate.c b/drivers/kvm/x86_emulate.c
index 58ceb66..884e4a2 100644
--- a/drivers/kvm/x86_emulate.c
+++ b/drivers/kvm/x86_emulate.c
@@ -99,17 +99,13 @@ static u16 opcode_table[256] = {
 	ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
 	0, 0, 0, 0,
 	/* 0x40 - 0x47 */
-	ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
-	ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
+	DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
 	/* 0x48 - 0x4F */
-	ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
-	ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
+	DstReg, DstReg, DstReg, DstReg,	DstReg, DstReg, DstReg, DstReg,
 	/* 0x50 - 0x57 */
-	ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
-	ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
+	SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg,
 	/* 0x58 - 0x5F */
-	ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
-	ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
+	DstReg, DstReg, DstReg, DstReg,	DstReg, DstReg, DstReg, DstReg,
 	/* 0x60 - 0x67 */
 	0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
 	0, 0, 0, 0,
@@ -525,13 +521,17 @@ static void decode_register_operand(struct operand *op,
 				    int highbyte_regs,
 				    int inhibit_bytereg)
 {
+	unsigned reg = c->modrm_reg;
+
+	if (!(c->d & ModRM))
+		reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
 	op->type = OP_REG;
 	if ((c->d & ByteOp) && !inhibit_bytereg) {
-		op->ptr = decode_register(c->modrm_reg, c->regs, highbyte_regs);
+		op->ptr = decode_register(reg, c->regs, highbyte_regs);
 		op->val = *(u8 *)op->ptr;
 		op->bytes = 1;
 	} else {
-		op->ptr = decode_register(c->modrm_reg, c->regs, 0);
+		op->ptr = decode_register(reg, c->regs, 0);
 		op->bytes = c->op_bytes;
 		switch (op->bytes) {
 		case 2:
@@ -552,7 +552,7 @@ int
 x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 {
 	struct decode_cache *c = &ctxt->decode;
-	u8 sib, rex_prefix = 0;
+	u8 sib;
 	int rc = 0;
 	int mode = ctxt->mode;
 	int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
@@ -616,7 +616,7 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 		case 0x40 ... 0x4f: /* REX */
 			if (mode != X86EMUL_MODE_PROT64)
 				goto done_prefixes;
-			rex_prefix = c->b;
+			c->rex_prefix = c->b;
 			continue;
 		case 0xf0:	/* LOCK */
 			c->lock_prefix = 1;
@@ -631,18 +631,18 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 
 		/* Any legacy prefix after a REX prefix nullifies its effect. */
 
-		rex_prefix = 0;
+		c->rex_prefix = 0;
 	}
 
 done_prefixes:
 
 	/* REX prefix. */
-	if (rex_prefix) {
-		if (rex_prefix & 8)
+	if (c->rex_prefix) {
+		if (c->rex_prefix & 8)
 			c->op_bytes = 8;	/* REX.W */
-		c->modrm_reg = (rex_prefix & 4) << 1;	/* REX.R */
-		index_reg = (rex_prefix & 2) << 2; /* REX.X */
-		c->modrm_rm = base_reg = (rex_prefix & 1) << 3; /* REG.B */
+		c->modrm_reg = (c->rex_prefix & 4) << 1;	/* REX.R */
+		index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
+		c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
 	}
 
 	/* Opcode byte(s). */
@@ -837,7 +837,7 @@ modrm_done:
 	case SrcNone:
 		break;
 	case SrcReg:
-		decode_register_operand(&c->src, c, rex_prefix == 0, 0);
+		decode_register_operand(&c->src, c, c->rex_prefix == 0, 0);
 		break;
 	case SrcMem16:
 		c->src.bytes = 2;
@@ -895,7 +895,7 @@ modrm_done:
 		/* Special instructions do their own operand decoding. */
 		return 0;
 	case DstReg:
-		decode_register_operand(&c->dst, c, rex_prefix == 0,
+		decode_register_operand(&c->dst, c, c->rex_prefix == 0,
 			 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
 		break;
 	case DstMem:
@@ -1258,6 +1258,32 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	      cmp:		/* cmp */
 		emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
 		break;
+	case 0x40 ... 0x47: /* inc r16/r32 */
+		emulate_1op("inc", c->dst, ctxt->eflags);
+		break;
+	case 0x48 ... 0x4f: /* dec r16/r32 */
+		emulate_1op("dec", c->dst, ctxt->eflags);
+		break;
+	case 0x50 ... 0x57:  /* push reg */
+		c->dst.type  = OP_MEM;
+		c->dst.bytes = c->op_bytes;
+		c->dst.val = c->src.val;
+		register_address_increment(c->regs[VCPU_REGS_RSP],
+					   -c->op_bytes);
+		c->dst.ptr = (void *) register_address(
+			ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
+		break;
+	case 0x58 ... 0x5f: /* pop reg */
+	pop_instruction:
+		if ((rc = ops->read_std(register_address(ctxt->ss_base,
+			c->regs[VCPU_REGS_RSP]), c->dst.ptr,
+			c->op_bytes, ctxt->vcpu)) != 0)
+			goto done;
+
+		register_address_increment(c->regs[VCPU_REGS_RSP],
+					   c->op_bytes);
+		c->dst.type = OP_NONE;	/* Disable writeback. */
+		break;
 	case 0x63:		/* movsxd */
 		if (ctxt->mode != X86EMUL_MODE_PROT64)
 			goto cannot_emulate;
@@ -1373,43 +1399,6 @@ special_insn:
 	if (c->twobyte)
 		goto twobyte_special_insn;
 	switch (c->b) {
-	case 0x40 ... 0x47: /* inc r16/r32 */
-		c->dst.bytes = c->op_bytes;
-		c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7];
-		c->dst.val = *c->dst.ptr;
-		emulate_1op("inc", c->dst, ctxt->eflags);
-		break;
-	case 0x48 ... 0x4f: /* dec r16/r32 */
-		c->dst.bytes = c->op_bytes;
-		c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7];
-		c->dst.val = *c->dst.ptr;
-		emulate_1op("dec", c->dst, ctxt->eflags);
-		break;
-	case 0x50 ... 0x57:  /* push reg */
-		if (c->op_bytes == 2)
-			c->src.val = (u16) c->regs[c->b & 0x7];
-		else
-			c->src.val = (u32) c->regs[c->b & 0x7];
-		c->dst.type  = OP_MEM;
-		c->dst.bytes = c->op_bytes;
-		c->dst.val = c->src.val;
-		register_address_increment(c->regs[VCPU_REGS_RSP],
-					   -c->op_bytes);
-		c->dst.ptr = (void *) register_address(
-			ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
-		break;
-	case 0x58 ... 0x5f: /* pop reg */
-		c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7];
-	pop_instruction:
-		if ((rc = ops->read_std(register_address(ctxt->ss_base,
-			c->regs[VCPU_REGS_RSP]), c->dst.ptr,
-			c->op_bytes, ctxt->vcpu)) != 0)
-			goto done;
-
-		register_address_increment(c->regs[VCPU_REGS_RSP],
-					   c->op_bytes);
-		c->dst.type = OP_NONE;	/* Disable writeback. */
-		break;
 	case 0x6a: /* push imm8 */
 		c->src.val = 0L;
 		c->src.val = insn_fetch(s8, 1, c->eip);
diff --git a/drivers/kvm/x86_emulate.h b/drivers/kvm/x86_emulate.h
index f03b128..e34868b 100644
--- a/drivers/kvm/x86_emulate.h
+++ b/drivers/kvm/x86_emulate.h
@@ -126,6 +126,7 @@ struct decode_cache {
 	u8 rep_prefix;
 	u8 op_bytes;
 	u8 ad_bytes;
+	u8 rex_prefix;
 	struct operand src;
 	struct operand dst;
 	unsigned long *override_base;
-- 
1.5.3.7


  parent reply	other threads:[~2007-12-26 11:16 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-26 11:05 [PATCH 00/55] KVM patch queue review for 2.6.25 merge window (part II) Avi Kivity
2007-12-26 11:05 ` [PATCH 01/55] KVM: Portability: Split kvm_vcpu into arch dependent and independent parts (part 1) Avi Kivity
2007-12-26 11:05 ` [PATCH 02/55] KVM: Move vmx_vcpu_reset() out of vmx_vcpu_setup() Avi Kivity
2007-12-26 11:05 ` [PATCH 03/55] KVM: Add a might_sleep() annotation to gfn_to_page() Avi Kivity
2007-12-26 11:05 ` [PATCH 04/55] KVM: Export PIC reset for kernel device reset Avi Kivity
2007-12-26 11:05 ` [PATCH 05/55] KVM: Split IOAPIC reset function and export for kernel RESET Avi Kivity
2007-12-26 11:05 ` [PATCH 06/55] KVM: Per-architecture hypercall definitions Avi Kivity
2007-12-26 19:32   ` Pavel Machek
2007-12-27  7:03     ` Avi Kivity
2007-12-26 11:05 ` [PATCH 07/55] KVM: Unmap kernel-allocated memory on slot destruction Avi Kivity
2007-12-26 11:05 ` [PATCH 08/55] KVM: Export memory slot allocation mechanism Avi Kivity
2007-12-26 11:05 ` [PATCH 09/55] KVM: Add kernel-internal memory slots Avi Kivity
2007-12-26 11:05 ` [PATCH 10/55] KVM: Add ioctl to tss address from userspace, Avi Kivity
2007-12-26 11:05 ` [PATCH 11/55] KVM: VMX: Let gcc to choose which registers to save (x86_64) Avi Kivity
2007-12-26 11:05 ` [PATCH 12/55] KVM: VMX: Let gcc to choose which registers to save (i386) Avi Kivity
2007-12-26 11:05 ` [PATCH 13/55] KVM: SVM: Let gcc to choose which registers to save (x86_64) Avi Kivity
2007-12-26 11:05 ` [PATCH 14/55] KVM: SVM: Let gcc to choose which registers to save (i386) Avi Kivity
2007-12-26 11:05 ` [PATCH 15/55] KVM: x86 emulator: don't depend on cr2 for mov abs emulation Avi Kivity
2007-12-26 11:05 ` [PATCH 16/55] KVM: Move page fault processing to common code Avi Kivity
2007-12-26 11:05 ` [PATCH 17/55] KVM: MMU: Topup the mmu memory preallocation caches before emulating an insn Avi Kivity
2007-12-26 11:05 ` [PATCH 18/55] KVM: Portability: Split kvm_vm_ioctl v3 Avi Kivity
2007-12-26 11:05 ` [PATCH 19/55] KVM: Portability: Move memory segmentation to x86.c Avi Kivity
2007-12-26 11:05 ` [PATCH 20/55] KVM: Portability: move get/set_apic_base " Avi Kivity
2007-12-26 11:05 ` [PATCH 21/55] KVM: Portability: Move control register helper functions " Avi Kivity
2007-12-26 11:05 ` [PATCH 22/55] KVM: VMX: Enable memory mapped TPR shadow (FlexPriority) Avi Kivity
2007-12-26 11:05 ` [PATCH 23/55] KVM: Fix gfn_to_page() acquiring mmap_sem twice Avi Kivity
2007-12-26 11:05 ` [PATCH 24/55] KVM: Portability: Move kvm_get/set_msr[_common] to x86.c Avi Kivity
2007-12-26 11:05 ` [PATCH 25/55] KVM: Portability: Move x86 emulation and mmio device hook " Avi Kivity
2007-12-26 11:05 ` [PATCH 26/55] KVM: Portability: Move pio emulation functions " Avi Kivity
2007-12-26 11:05 ` [PATCH 27/55] KVM: x86 emulator: Extract the common code of SrcReg and DstReg Avi Kivity
2007-12-26 11:05 ` Avi Kivity [this message]
2007-12-26 11:05 ` [PATCH 29/55] KVM: Simplify decode_register_operand() calling convention Avi Kivity
2007-12-26 11:05 ` [PATCH 30/55] KVM: Make mark_page_dirty() work for aliased pages too Avi Kivity
2007-12-26 11:05 ` [PATCH 31/55] KVM: x86 emulator: Hoist modrm and abs decoding into separate functions Avi Kivity
2007-12-26 11:05 ` [PATCH 32/55] KVM: Portability: Make exported debugfs data architecture-specific Avi Kivity
2007-12-26 11:05 ` [PATCH 33/55] KVM: Portability: Move x86 instruction emulation code to x86.c Avi Kivity
2007-12-26 11:05 ` [PATCH 34/55] KVM: Portability: Move x86 FPU handling " Avi Kivity
2007-12-26 11:05 ` [PATCH 35/55] KVM: Portability: Move x86 vcpu ioctl handlers " Avi Kivity
2007-12-26 11:05 ` [PATCH 36/55] KVM: Add make_page_dirty() to kvm_clear_guest_page() Avi Kivity
2007-12-26 11:05 ` [PATCH 37/55] KVM: VMX: Use vmx to inject real-mode interrupts Avi Kivity
2007-12-26 11:05 ` [PATCH 38/55] KVM: VMX: Read & store IDT_VECTORING_INFO_FIELD Avi Kivity
2007-12-26 11:05 ` [PATCH 39/55] KVM: Fix faults during injection of real-mode interrupts Avi Kivity
2007-12-26 11:05 ` [PATCH 40/55] KVM: VMX: Comment VMX primary/secondary exec ctl definitions Avi Kivity
2007-12-26 11:05 ` [PATCH 41/55] KVM: VMX: wbinvd exiting Avi Kivity
2007-12-26 11:05 ` [PATCH 42/55] KVM: x86 emulator: remove 8 bytes operands emulator for call near instruction Avi Kivity
2007-12-26 11:05 ` [PATCH 43/55] KVM: Simplify CPU_TASKS_FROZEN cpu notifier handling Avi Kivity
2007-12-26 11:05 ` [PATCH 44/55] KVM: add kvm_is_error_hva() Avi Kivity
2007-12-26 11:05 ` [PATCH 45/55] KVM: introduce gfn_to_hva() Avi Kivity
2007-12-26 11:05 ` [PATCH 46/55] KVM: Change kvm_{read,write}_guest() to use copy_{from,to}_user() Avi Kivity
2007-12-26 11:05 ` [PATCH 47/55] KVM: Portability: Move some includes to x86.c Avi Kivity
2007-12-26 11:05 ` [PATCH 48/55] KVM: Portability: Move kvm_x86_ops " Avi Kivity
2007-12-26 11:05 ` [PATCH 49/55] KVM: Portability: Add vcpu and hardware management arch hooks Avi Kivity
2007-12-26 11:05 ` [PATCH 50/55] KVM: Portability: Combine kvm_init and kvm_init_x86 Avi Kivity
2007-12-26 11:05 ` [PATCH 51/55] KVM: Portability: Move x86 specific code from kvm_init() to kvm_arch() Avi Kivity
2007-12-26 11:05 ` [PATCH 52/55] KVM: x86 emulator: modify 'lods', and 'stos' not to depend on CR2 Avi Kivity
2007-12-26 11:05 ` [PATCH 53/55] KVM: Portability: move KVM_CHECK_EXTENSION Avi Kivity
2007-12-26 11:05 ` [PATCH 54/55] KVM: VMX: Consolidate register usage in vmx_vcpu_run() Avi Kivity
2007-12-26 11:06 ` [PATCH 55/55] KVM: Portability: Make kvm_vcpu_ioctl_translate arch dependent Avi Kivity
2007-12-26 20:47 ` [PATCH 00/55] KVM patch queue review for 2.6.25 merge window (part II) Sam Ravnborg
2007-12-27  6:51   ` 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=1198667160-22953-29-git-send-email-avi@qumranet.com \
    --to=avi@qumranet.com \
    --cc=kvm-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®