Skip to contentSkip to conversation

More emulator cleanups

PATCH In review Avi Kivity 2 participants 19 messages Aug 1, 2010 – Aug 2, 2010

Subsystems (3)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover More emulator cleanups

This patchset further cleans up the emulator. The goal is to push segment decoding into 'struct operand', but a few things stood in the way.

Avi Kivity (15):
KVM: x86 emulator: push segment override out of decode_modrm()
KVM: x86 emulator: use correct type for memory address in operands
KVM: x86 emulator: simplify xchg decode tables
KVM: x86 emulator: use SrcAcc to simplify xchg decoding
KVM: x86 emulator: put register operand fetch into a function
KVM: x86 emulator: drop use_modrm_ea
KVM: x86 emulator: simplify REX.W check
KVM: x86 emulator: introduce Force64 for forcing operand size to 64
bits
KVM: x86 emulator: mark mov cr and mov dr as 64-bit instructions in
long mode
KVM: x86 emulator: use struct operand for mov reg,cr and mov cr,reg
for reg op
KVM: x86 emulator: use struct operand for mov reg,dr and mov dr,reg
for reg op
KVM: x86 emulator: add NoAccess flag for memory instructions that
skip access
KVM: x86 emulator: switch LEA to use SrcMem decoding
KVM: x86 emulator: change invlpg emulation to use src.mem.addr
KVM: x86 emulator: Decode memory operands directly into a 'struct
operand'

 arch/x86/include/asm/kvm_emulate.h |   11 +-
 arch/x86/kvm/emulate.c             |  321 ++++++++++++++++--------------------
 2 files changed, 147 insertions(+), 185 deletions(-)
author
#1 lore raw

PATCH 1/15 KVM: x86 emulator: push segment override out of decode_modrm()

Let it compute modrm_seg instead, and have the caller apply it.

---
 arch/x86/include/asm/kvm_emulate.h |    1 +
 arch/x86/kvm/emulate.c             |   10 ++++++----
 2 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
index f397b79..ecb2653 100644
@@ -193,6 +193,7 @@ struct decode_cache {
 	u8 modrm_mod;
 	u8 modrm_reg;
 	u8 modrm_rm;
+	u8 modrm_seg;
 	u8 use_modrm_ea;
 	bool rip_relative;
 	unsigned long modrm_ea;
arch/x86/kvm/emulate.c +6 -4
index dc1ecff..2ed6c67 100644
@@ -593,6 +593,7 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt,
 	c->modrm_rm |= (c->modrm & 0x07);
 	c->modrm_ea = 0;
 	c->use_modrm_ea = 1;
+	c->modrm_seg = VCPU_SREG_DS;
 
 	if (c->modrm_mod == 3) {
 		c->modrm_ptr = decode_register(c->modrm_rm,
@@ -649,8 +650,7 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt,
 		}
 		if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
 		    (c->modrm_rm == 6 && c->modrm_mod != 0))
-			if (!c->has_seg_override)
-				set_seg_override(c, VCPU_SREG_SS);
+			c->modrm_seg = VCPU_SREG_SS;
 		c->modrm_ea = (u16)c->modrm_ea;
 	} else {
 		/* 32/64-bit ModR/M decode. */
@@ -2400,9 +2400,11 @@ done_prefixes:
 		c->op_bytes = 8;
 
 	/* ModRM and SIB bytes. */
-	if (c->d & ModRM)
+	if (c->d & ModRM) {
 		rc = decode_modrm(ctxt, ops);
-	else if (c->d & MemAbs)
+		if (!c->has_seg_override)
+			set_seg_override(c, c->modrm_seg);
+	} else if (c->d & MemAbs)
 		rc = decode_abs(ctxt, ops);
 	if (rc != X86EMUL_CONTINUE)
 		goto done;
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH 2/15 KVM: x86 emulator: use correct type for memory address in operands

In reply to Avi Kivity

Currently we use a void pointer for memory addresses. That's wrong since these are guest virtual addresses which are not directly dereferencable by the host.

Use the correct type, unsigned long.

---
 arch/x86/include/asm/kvm_emulate.h |    6 ++-
 arch/x86/kvm/emulate.c             |  117 +++++++++++++++++------------------
 2 files changed, 62 insertions(+), 61 deletions(-)
arch/x86/include/asm/kvm_emulate.h +5 -1
index ecb2653..bbf0e81 100644
@@ -152,7 +152,11 @@ struct x86_emulate_ops {
 struct operand {
 	enum { OP_REG, OP_MEM, OP_IMM, OP_NONE } type;
 	unsigned int bytes;
-	unsigned long orig_val, *ptr;
+	unsigned long orig_val;
+	union {
+		unsigned long *reg;
+		unsigned long mem;
+	} addr;
 	union {
 		unsigned long val;
 		char valptr[sizeof(unsigned long) + 2];
arch/x86/kvm/emulate.c +57 -60

357 lines not on this page · raw message

Signature · 1 line
1.7.1
author
#3 lore raw

PATCH 3/15 KVM: x86 emulator: simplify xchg decode tables

In reply to Avi Kivity

Use X8() to avoid repetition.

---
 arch/x86/kvm/emulate.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +1 -1
index 61d728d..745353e 100644
@@ -2142,7 +2142,7 @@ static struct opcode opcode_table[256] = {
 	D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
 	D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
 	/* 0x90 - 0x97 */
-	D(DstReg), D(DstReg), D(DstReg), D(DstReg),	D(DstReg), D(DstReg), D(DstReg), D(DstReg),
+	X8(D(DstReg)),
 	/* 0x98 - 0x9F */
 	N, N, D(SrcImmFAddr | No64), N,
 	D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
Signature · 1 line
1.7.1
author
#4 lore raw

PATCH 4/15 KVM: x86 emulator: use SrcAcc to simplify xchg decoding

In reply to Avi Kivity
---
 arch/x86/kvm/emulate.c |   15 ++++-----------
 1 files changed, 4 insertions(+), 11 deletions(-)
arch/x86/kvm/emulate.c +4 -11
index 745353e..4d510c3 100644
@@ -2142,7 +2142,7 @@ static struct opcode opcode_table[256] = {
 	D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
 	D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
 	/* 0x90 - 0x97 */
-	X8(D(DstReg)),
+	X8(D(SrcAcc | DstReg)),
 	/* 0x98 - 0x9F */
 	N, N, D(SrcImmFAddr | No64), N,
 	D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
@@ -2927,16 +2927,9 @@ special_insn:
 		if (rc != X86EMUL_CONTINUE)
 			goto done;
 		break;
-	case 0x90: /* nop / xchg r8,rax */
-		if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX]) {
-			c->dst.type = OP_NONE;  /* nop */
-			break;
-		}
-	case 0x91 ... 0x97: /* xchg reg,rax */
-		c->src.type = OP_REG;
-		c->src.bytes = c->op_bytes;
-		c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
-		c->src.val = *(c->src.addr.reg);
+	case 0x90 ... 0x97: /* nop / xchg reg, rax */
+		if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
+			goto done;
 		goto xchg;
 	case 0x9c: /* pushf */
 		c->src.val =  (unsigned long) ctxt->eflags;
Signature · 1 line
1.7.1
author
#5 lore raw

PATCH 5/15 KVM: x86 emulator: put register operand fetch into a function

In reply to Avi Kivity

The code is repeated three times, put it into fetch_register_operand()

---
 arch/x86/kvm/emulate.c |   61 ++++++++++++++++-------------------------------
 1 files changed, 21 insertions(+), 40 deletions(-)
arch/x86/kvm/emulate.c +21 -40
index 4d510c3..063c96a 100644
@@ -539,6 +539,24 @@ static int test_cc(unsigned int condition, unsigned int flags)
 	return (!!rc ^ (condition & 1));
 }
 
+static void fetch_register_operand(struct operand *op)
+{
+	switch (op->bytes) {
+	case 1:
+		op->val = *(u8 *)op->addr.reg;
+		break;
+	case 2:
+		op->val = *(u16 *)op->addr.reg;
+		break;
+	case 4:
+		op->val = *(u32 *)op->addr.reg;
+		break;
+	case 8:
+		op->val = *(u64 *)op->addr.reg;
+		break;
+	}
+}
+
 static void decode_register_operand(struct operand *op,
 				    struct decode_cache *c,
 				    int inhibit_bytereg)
@@ -551,23 +569,12 @@ static void decode_register_operand(struct operand *op,
 	op->type = OP_REG;
 	if ((c->d & ByteOp) && !inhibit_bytereg) {
 		op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
-		op->val = *(u8 *)op->addr.reg;
 		op->bytes = 1;
 	} else {
 		op->addr.reg = decode_register(reg, c->regs, 0);
 		op->bytes = c->op_bytes;
-		switch (op->bytes) {
-		case 2:
-			op->val = *(u16 *)op->addr.reg;
-			break;
-		case 4:
-			op->val = *(u32 *)op->addr.reg;
-			break;
-		case 8:
-			op->val = *(u64 *) op->addr.reg;
-			break;
-		}
 	}
+	fetch_register_operand(op);
 	op->orig_val = op->val;
 }
 
@@ -2502,20 +2509,7 @@ done_prefixes:
 		c->src.type = OP_REG;
 		c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
 		c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
-		switch (c->src.bytes) {
-			case 1:
-				c->src.val = *(u8 *)c->src.addr.reg;
-				break;
-			case 2:
-				c->src.val = *(u16 *)c->src.addr.reg;
-				break;
-			case 4:
-				c->src.val = *(u32 *)c->src.addr.reg;
-				break;
-			case 8:
-				c->src.val = *(u64 *)c->src.addr.reg;
-				break;
-		}
+		fetch_register_operand(&c->src);
 		break;
 	case SrcOne:
 		c->src.bytes = 1;
@@ -2601,20 +2595,7 @@ done_prefixes:
 		c->dst.type = OP_REG;
 		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
 		c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
-		switch (c->dst.bytes) {
-			case 1:
-				c->dst.val = *(u8 *)c->dst.addr.reg;
-				break;
-			case 2:
-				c->dst.val = *(u16 *)c->dst.addr.reg;
-				break;
-			case 4:
-				c->dst.val = *(u32 *)c->dst.addr.reg;
-				break;
-			case 8:
-				c->dst.val = *(u64 *)c->dst.addr.reg;
-				break;
-		}
+		fetch_register_operand(&c->dst);
 		c->dst.orig_val = c->dst.val;
 		break;
 	case DstDI:
Signature · 1 line
1.7.1
author
#6 lore raw

PATCH 6/15 KVM: x86 emulator: drop use_modrm_ea

In reply to Avi Kivity

Unused (and has never been).

---
 arch/x86/include/asm/kvm_emulate.h |    1 -
 arch/x86/kvm/emulate.c             |    1 -
 2 files changed, 0 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_emulate.h +0 -1
index bbf0e81..db4a248 100644
@@ -198,7 +198,6 @@ struct decode_cache {
 	u8 modrm_reg;
 	u8 modrm_rm;
 	u8 modrm_seg;
-	u8 use_modrm_ea;
 	bool rip_relative;
 	unsigned long modrm_ea;
 	void *modrm_ptr;
arch/x86/kvm/emulate.c +0 -1
index 063c96a..2ae2e54 100644
@@ -597,7 +597,6 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt,
 	c->modrm_reg |= (c->modrm & 0x38) >> 3;
 	c->modrm_rm |= (c->modrm & 0x07);
 	c->modrm_ea = 0;
-	c->use_modrm_ea = 1;
 	c->modrm_seg = VCPU_SREG_DS;
 
 	if (c->modrm_mod == 3) {
Signature · 1 line
1.7.1
author
#7 lore raw

PATCH 7/15 KVM: x86 emulator: simplify REX.W check

In reply to Avi Kivity

(x && (x & y)) == (x & y)

---
 arch/x86/kvm/emulate.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +2 -3
index 2ae2e54..a114fa9 100644
@@ -2353,9 +2353,8 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt)
 done_prefixes:
 
 	/* REX prefix. */
-	if (c->rex_prefix)
-		if (c->rex_prefix & 8)
-			c->op_bytes = 8;	/* REX.W */
+	if (c->rex_prefix & 8)
+		c->op_bytes = 8;	/* REX.W */
 
 	/* Opcode byte(s). */
 	opcode = opcode_table[c->b];
Signature · 1 line
1.7.1
author
#8 lore raw

PATCH 8/15 KVM: x86 emulator: introduce Force64 for forcing operand size to 64 bits

In reply to Avi Kivity
---
 arch/x86/kvm/emulate.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +2 -1
index a114fa9..d7d95de 100644
@@ -83,6 +83,7 @@
 #define Group       (1<<14)     /* Bits 3:5 of modrm byte extend opcode */
 #define GroupDual   (1<<15)     /* Alternate decoding of mod == 3 */
 /* Misc flags */
+#define Force64     (1<<24) /* Force operand size to 64 bits in 64-bit mode */
 #define Undefined   (1<<25) /* No Such Instruction */
 #define Lock        (1<<26) /* lock prefix is allowed for the instruction */
 #define Priv        (1<<27) /* instruction generates #GP if current CPL != 0 */
@@ -2398,7 +2399,7 @@ done_prefixes:
 		return -1;
 	}
 
-	if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
+	if (mode == X86EMUL_MODE_PROT64 && (c->d & (Stack | Force64)))
 		c->op_bytes = 8;
 
 	/* ModRM and SIB bytes. */
Signature · 1 line
1.7.1
author
#9 lore raw

PATCH 9/15 KVM: x86 emulator: mark mov cr and mov dr as 64-bit instructions in long mode

In reply to Avi Kivity
---
 arch/x86/kvm/emulate.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
index d7d95de..d1a6cd7 100644
@@ -2205,8 +2205,8 @@ static struct opcode twobyte_table[256] = {
 	/* 0x10 - 0x1F */
 	N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
 	/* 0x20 - 0x2F */
-	D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
-	D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
+	D(ModRM | ImplicitOps | Priv | Force64), D(ModRM | Priv | Force64),
+	D(ModRM | ImplicitOps | Priv | Force64), D(ModRM | Priv | Force64),
 	N, N, N, N,
 	N, N, N, N, N, N, N, N,
 	/* 0x30 - 0x3F */
Signature · 1 line
1.7.1
author
#10 lore raw

Re: [PATCH 09/15] KVM: x86 emulator: mark mov cr and mov dr as 64-bit instructions in long mode

Quoted 10 lines · Avi Kivity
---
  arch/x86/kvm/emulate.c |    4 ++--
  1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +0 -0
index d7d95de..d1a6cd7 100644

arch/x86/kvm/emulate.c static struct opcode twobyte_table[256] = { · line 2210

Quoted 8 lines
arch/x86/kvm/emulate.c +2 -2
@@ -2205,8 +2205,8 @@ static struct opcode twobyte_table[256] = {
  	/* 0x10 - 0x1F */
  	N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
  	/* 0x20 - 0x2F */
-	D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
-	D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
+	D(ModRM | ImplicitOps | Priv | Force64), D(ModRM | Priv | Force64),
+	D(ModRM | ImplicitOps | Priv | Force64), D(ModRM | Priv | Force64),
  	N, N, N, N,

This is incorrect. These instructions are either 32 or 64 bit, while Force64 is 64-bit in long mode, and 32/16 bits in other modes, depending on default operand size and operand size override.

With this fixed, the whole thing passes autotest.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#11 lore raw

PATCH 10/15 KVM: x86 emulator: use struct operand for mov reg,cr and mov cr,reg for reg op

In reply to Avi Kivity

This is an ordinary modrm source or destination; use the standard structure representing it.

---
 arch/x86/kvm/emulate.c |    9 ++++-----
 1 files changed, 4 insertions(+), 5 deletions(-)
arch/x86/kvm/emulate.c +4 -5
index d1a6cd7..53e5c60 100644
@@ -2205,8 +2205,8 @@ static struct opcode twobyte_table[256] = {
 	/* 0x10 - 0x1F */
 	N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
 	/* 0x20 - 0x2F */
-	D(ModRM | ImplicitOps | Priv | Force64), D(ModRM | Priv | Force64),
-	D(ModRM | ImplicitOps | Priv | Force64), D(ModRM | Priv | Force64),
+	D(ModRM | DstMem | Priv | Force64), D(ModRM | Priv | Force64),
+	D(ModRM | SrcMem | Priv | Force64), D(ModRM | Priv | Force64),
 	N, N, N, N,
 	N, N, N, N, N, N, N, N,
 	/* 0x30 - 0x3F */
@@ -3228,8 +3228,7 @@ twobyte_insn:
 			emulate_ud(ctxt);
 			goto done;
 		}
-		c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
-		c->dst.type = OP_NONE;	/* no writeback */
+		c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
 		break;
 	case 0x21: /* mov from dr to reg */
 		if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
@@ -3241,7 +3240,7 @@ twobyte_insn:
 		c->dst.type = OP_NONE;	/* no writeback */
 		break;
 	case 0x22: /* mov reg, cr */
-		if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
+		if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
 			emulate_gp(ctxt, 0);
 			goto done;
 		}
Signature · 1 line
1.7.1
author
#12 lore raw

PATCH 11/15 KVM: x86 emulator: use struct operand for mov reg,dr and mov dr,reg for reg op

In reply to Avi Kivity

This is an ordinary modrm source or destination; use the standard structure representing it.

---
 arch/x86/kvm/emulate.c |    9 ++++-----
 1 files changed, 4 insertions(+), 5 deletions(-)
arch/x86/kvm/emulate.c +4 -5
index 53e5c60..5bc62f2 100644
@@ -2205,8 +2205,8 @@ static struct opcode twobyte_table[256] = {
 	/* 0x10 - 0x1F */
 	N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
 	/* 0x20 - 0x2F */
-	D(ModRM | DstMem | Priv | Force64), D(ModRM | Priv | Force64),
-	D(ModRM | SrcMem | Priv | Force64), D(ModRM | Priv | Force64),
+	D(ModRM | DstMem | Priv | Force64), D(ModRM | DstMem | Priv | Force64),
+	D(ModRM | SrcMem | Priv | Force64), D(ModRM | SrcMem | Priv | Force64),
 	N, N, N, N,
 	N, N, N, N, N, N, N, N,
 	/* 0x30 - 0x3F */
@@ -3236,8 +3236,7 @@ twobyte_insn:
 			emulate_ud(ctxt);
 			goto done;
 		}
-		ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
-		c->dst.type = OP_NONE;	/* no writeback */
+		ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
 		break;
 	case 0x22: /* mov reg, cr */
 		if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
@@ -3253,7 +3252,7 @@ twobyte_insn:
 			goto done;
 		}
 
-		if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
+		if (ops->set_dr(c->modrm_reg, c->src.val &
 				((ctxt->mode == X86EMUL_MODE_PROT64) ?
 				 ~0ULL : ~0U), ctxt->vcpu) < 0) {
 			/* #UD condition is already handled by the code above */
Signature · 1 line
1.7.1
author
#13 lore raw

PATCH 12/15 KVM: x86 emulator: add NoAccess flag for memory instructions that skip access

In reply to Avi Kivity

Use for INVLPG, which accesses the tlb, not memory.

---
 arch/x86/kvm/emulate.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +4 -2
index 5bc62f2..29312a0 100644
@@ -83,6 +83,7 @@
 #define Group       (1<<14)     /* Bits 3:5 of modrm byte extend opcode */
 #define GroupDual   (1<<15)     /* Alternate decoding of mod == 3 */
 /* Misc flags */
+#define NoAccess    (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
 #define Force64     (1<<24) /* Force operand size to 64 bits in 64-bit mode */
 #define Undefined   (1<<25) /* No Such Instruction */
 #define Lock        (1<<26) /* lock prefix is allowed for the instruction */
@@ -2062,7 +2063,8 @@ static struct opcode group5[] = {
 static struct group_dual group7 = { {
 	N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
 	D(SrcNone | ModRM | DstMem | Mov), N,
-	D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
+	D(SrcMem16 | ModRM | Mov | Priv),
+	D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
 }, {
 	D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
 	D(SrcNone | ModRM | DstMem | Mov), N,
@@ -2444,7 +2446,7 @@ done_prefixes:
 		c->src.bytes = (c->d & ByteOp) ? 1 :
 							   c->op_bytes;
 		/* Don't fetch the address for invlpg: it could be unmapped. */
-		if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
+		if (c->d & NoAccess)
 			break;
 	srcmem_common:
 		/*
Signature · 1 line
1.7.1
author
#14 lore raw

PATCH 13/15 KVM: x86 emulator: switch LEA to use SrcMem decoding

In reply to Avi Kivity

The NoAccess flag will prevent memory from being accessed.

---
 arch/x86/kvm/emulate.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
index 29312a0..46a5d75 100644
@@ -2148,7 +2148,7 @@ static struct opcode opcode_table[256] = {
 	/* 0x88 - 0x8F */
 	D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
 	D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
-	D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
+	D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
 	D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
 	/* 0x90 - 0x97 */
 	X8(D(SrcAcc | DstReg)),
@@ -2883,7 +2883,7 @@ special_insn:
 		c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
 		break;
 	case 0x8d: /* lea r16/r32, m */
-		c->dst.val = c->modrm_ea;
+		c->dst.val = c->src.addr.mem;
 		break;
 	case 0x8e: { /* mov seg, r/m16 */
 		uint16_t sel;
Signature · 1 line
1.7.1
author
#15 lore raw

PATCH 14/15 KVM: x86 emulator: change invlpg emulation to use src.mem.addr

In reply to Avi Kivity

Instead of using modrm_ea, which will soon be gone.

---
 arch/x86/kvm/emulate.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +1 -1
index 46a5d75..de1ed94 100644
@@ -3194,7 +3194,7 @@ twobyte_insn:
 			emulate_ud(ctxt);
 			goto done;
 		case 7: /* invlpg*/
-			emulate_invlpg(ctxt->vcpu, c->modrm_ea);
+			emulate_invlpg(ctxt->vcpu, c->src.addr.mem);
 			/* Disable writeback. */
 			c->dst.type = OP_NONE;
 			break;
Signature · 1 line
1.7.1
author
#16 lore raw

PATCH 15/15 KVM: x86 emulator: Decode memory operands directly into a 'struct operand'

In reply to Avi Kivity

Since modrm operand can be either register or memory, decoding it into a 'struct operand', which can represent both, is simpler.

---
 arch/x86/include/asm/kvm_emulate.h |    3 -
 arch/x86/kvm/emulate.c             |  125 ++++++++++++++++-------------------
 2 files changed, 57 insertions(+), 71 deletions(-)
arch/x86/include/asm/kvm_emulate.h +0 -3
index db4a248..99c1c57 100644
@@ -199,9 +199,6 @@ struct decode_cache {
 	u8 modrm_rm;
 	u8 modrm_seg;
 	bool rip_relative;
-	unsigned long modrm_ea;
-	void *modrm_ptr;
-	unsigned long modrm_val;
 	struct fetch_cache fetch;
 	struct read_cache io_read;
 	struct read_cache mem_read;
arch/x86/kvm/emulate.c +57 -68

287 lines not on this page · raw message

Signature · 1 line
1.7.1
maintainer
#17 lore raw

Re: [PATCH 15/15] KVM: x86 emulator: Decode memory operands directly into a 'struct operand'

arch/x86/kvm/emulate.c x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 2667

Quoted 11 lines · Avi Kivity
  	if (c->src.type == OP_MEM) {
arch/x86/kvm/emulate.c +4 -0
+		if (c->d & NoAccess)
+			goto no_fetch;
  		rc = read_emulated(ctxt, ops, c->src.addr.mem,
  					c->src.valptr, c->src.bytes);
  		if (rc != X86EMUL_CONTINUE)
  			goto done;
  		c->src.orig_val = c->src.val;
+	no_fetch:
+		;
  	}

Maybe remove the goto?

	if (c->src.type == OP_MEM && !(c->d & NoAccess)) {
		rc = read_emulated(ctxt, ops, c->src.addr.mem,
				   c->src.valptr, c->src.bytes);
		if (rc != X86EMUL_CONTINUE)
			goto done;
		c->src.orig_val = c->src.val;
	}

Or do you have other plans for it? :)

Thanks,

Paolo

author
#18 lore raw

Re: [PATCH 15/15] KVM: x86 emulator: Decode memory operands directly into a 'struct operand'

Quoted 23 lines · Paolo Bonzini

arch/x86/kvm/emulate.c x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 2667

Quoted 11 lines · Avi Kivity
      if (c->src.type == OP_MEM) {
arch/x86/kvm/emulate.c +4 -0
+        if (c->d & NoAccess)
+            goto no_fetch;
          rc = read_emulated(ctxt, ops, c->src.addr.mem,
                      c->src.valptr, c->src.bytes);
          if (rc != X86EMUL_CONTINUE)
              goto done;
          c->src.orig_val = c->src.val;
+    no_fetch:
+        ;
      }

Maybe remove the goto?

    if (c->src.type == OP_MEM && !(c->d & NoAccess)) {
        rc = read_emulated(ctxt, ops, c->src.addr.mem,
                   c->src.valptr, c->src.bytes);
        if (rc != X86EMUL_CONTINUE)
            goto done;
        c->src.orig_val = c->src.val;
    }
Quoted 2 lines

Or do you have other plans for it? :)

Mostly, keeping the patch as small as possible. I'll definitely do a cleanup pass later.

Signature · 1 line
error compiling committee.c: too many arguments to function