Skip to contentSkip to conversation

Convert 0F 01 opcodes to opcode::execute

PATCH New Avi Kivity 1 participant 4 messages Apr 21, 2011

Subsystems (3)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Convert 0F 01 opcodes to opcode::execute

A little step towards removing the big switch ().

Avi Kivity (3):
KVM: x86 emulator: move 0F 01 sub-opcodes into their own functions
KVM: x86 emulator: Don't force #UD for 0F 01 /5
KVM: x86 emulator: Use opcode::execute for 0F 01 opcode

 arch/x86/kvm/emulate.c |  169 +++++++++++++++++++++++++++---------------------
 1 files changed, 94 insertions(+), 75 deletions(-)
author
#1 lore raw

PATCH 1/3 KVM: x86 emulator: move 0F 01 sub-opcodes into their own functions

---
 arch/x86/kvm/emulate.c |  146 ++++++++++++++++++++++++++++++++---------------
 1 files changed, 99 insertions(+), 47 deletions(-)
arch/x86/kvm/emulate.c +99 -47
index f683ce1..bf4284d 100644
@@ -2589,6 +2589,95 @@ static int em_clts(struct x86_emulate_ctxt *ctxt)
 	return X86EMUL_CONTINUE;
 }
 
+static int em_vmcall(struct x86_emulate_ctxt *ctxt)
+{
+	struct decode_cache *c = &ctxt->decode;
+	int rc;
+
+	if (c->modrm_mod != 3 || c->modrm_rm != 1)
+		return X86EMUL_UNHANDLEABLE;
+
+	rc = ctxt->ops->fix_hypercall(ctxt);
+	if (rc != X86EMUL_CONTINUE)
+		return rc;
+
+	/* Let the processor re-execute the fixed hypercall */
+	c->eip = ctxt->eip;
+	/* Disable writeback. */
+	c->dst.type = OP_NONE;
+	return X86EMUL_CONTINUE;
+}
+
+static int em_lgdt(struct x86_emulate_ctxt *ctxt)
+{
+	struct decode_cache *c = &ctxt->decode;
+	struct desc_ptr desc_ptr;
+	int rc;
+
+	rc = read_descriptor(ctxt, ctxt->ops, c->src.addr.mem,
+			     &desc_ptr.size, &desc_ptr.address,
+			     c->op_bytes);
+	if (rc != X86EMUL_CONTINUE)
+		return rc;
+	ctxt->ops->set_gdt(ctxt, &desc_ptr);
+	/* Disable writeback. */
+	c->dst.type = OP_NONE;
+	return X86EMUL_CONTINUE;
+}
+
+static int em_svm(struct x86_emulate_ctxt *ctxt)
+{
+	struct decode_cache *c = &ctxt->decode;
+	int rc;
+
+	switch (c->modrm_rm) {
+	case 1:
+		rc = ctxt->ops->fix_hypercall(ctxt);
+		break;
+	default:
+		return X86EMUL_UNHANDLEABLE;
+	}
+	/* Disable writeback. */
+	c->dst.type = OP_NONE;
+	return rc;
+}
+
+static int em_lidt(struct x86_emulate_ctxt *ctxt)
+{
+	struct decode_cache *c = &ctxt->decode;
+	struct desc_ptr desc_ptr;
+	int rc;
+
+	rc = read_descriptor(ctxt, ctxt->ops, c->src.addr.mem,
+			     &desc_ptr.size,
+			     &desc_ptr.address,
+			     c->op_bytes);
+	if (rc != X86EMUL_CONTINUE)
+		return rc;
+	ctxt->ops->set_idt(ctxt, &desc_ptr);
+	/* Disable writeback. */
+	c->dst.type = OP_NONE;
+	return X86EMUL_CONTINUE;
+}
+
+static int em_smsw(struct x86_emulate_ctxt *ctxt)
+{
+	struct decode_cache *c = &ctxt->decode;
+
+	c->dst.bytes = 2;
+	c->dst.val = ctxt->ops->get_cr(ctxt, 0);
+	return X86EMUL_CONTINUE;
+}
+
+static int em_lmsw(struct x86_emulate_ctxt *ctxt)
+{
+	struct decode_cache *c = &ctxt->decode;
+	ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
+			  | (c->src.val & 0x0f));
+	c->dst.type = OP_NONE;
+	return X86EMUL_CONTINUE;
+}
+
 static bool valid_cr(int nr)
 {
 	switch (nr) {
@@ -3509,7 +3598,6 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
 	int rc = X86EMUL_CONTINUE;
 	int saved_dst_type = c->dst.type;
 	int irq; /* Used for int 3, int, and into */
-	struct desc_ptr desc_ptr;
 
 	ctxt->decode.mem_read.pos = 0;
 
@@ -4022,62 +4110,26 @@ twobyte_insn:
 	case 0x01: /* lgdt, lidt, lmsw */
 		switch (c->modrm_reg) {
 		case 0: /* vmcall */
-			if (c->modrm_mod != 3 || c->modrm_rm != 1)
-				goto cannot_emulate;
-
-			rc = ctxt->ops->fix_hypercall(ctxt);
-			if (rc != X86EMUL_CONTINUE)
-				goto done;
-
-			/* Let the processor re-execute the fixed hypercall */
-			c->eip = ctxt->eip;
-			/* Disable writeback. */
-			c->dst.type = OP_NONE;
+			rc = em_vmcall(ctxt);
 			break;
 		case 2: /* lgdt */
-			rc = read_descriptor(ctxt, ops, c->src.addr.mem,
-					     &desc_ptr.size, &desc_ptr.address,
-					     c->op_bytes);
-			if (rc != X86EMUL_CONTINUE)
-				goto done;
-			ctxt->ops->set_gdt(ctxt, &desc_ptr);
-			/* Disable writeback. */
-			c->dst.type = OP_NONE;
+			rc = em_lgdt(ctxt);
 			break;
 		case 3: /* lidt/vmmcall */
-			if (c->modrm_mod == 3) {
-				switch (c->modrm_rm) {
-				case 1:
-					rc = ctxt->ops->fix_hypercall(ctxt);
-					break;
-				default:
-					goto cannot_emulate;
-				}
-			} else {
-				rc = read_descriptor(ctxt, ops, c->src.addr.mem,
-						     &desc_ptr.size,
-						     &desc_ptr.address,
-						     c->op_bytes);
-				if (rc != X86EMUL_CONTINUE)
-					goto done;
-				ctxt->ops->set_idt(ctxt, &desc_ptr);
-			}
-			/* Disable writeback. */
-			c->dst.type = OP_NONE;
+			if (c->modrm_mod == 3)
+				return em_svm(ctxt);
+			else
+				return em_lidt(ctxt);
 			break;
 		case 4: /* smsw */
-			c->dst.bytes = 2;
-			c->dst.val = ops->get_cr(ctxt, 0);
+			rc = em_smsw(ctxt);
 			break;
 		case 6: /* lmsw */
-			ops->set_cr(ctxt, 0, (ops->get_cr(ctxt, 0) & ~0x0eul) |
-				    (c->src.val & 0x0f));
-			c->dst.type = OP_NONE;
+			rc = em_lmsw(ctxt);
 			break;
 		case 5: /* not defined */
-			emulate_ud(ctxt);
-			rc = X86EMUL_PROPAGATE_FAULT;
-			goto done;
+			rc = emulate_ud(ctxt);
+			break;
 		case 7: /* invlpg*/
 			rc = em_invlpg(ctxt);
 			break;
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH 2/3 KVM: x86 emulator: Don't force #UD for 0F 01 /5

In reply to Avi Kivity

While it isn't defined, no need to force a #UD. If it becomes defined in the future this can cause wierd problems for the guest.

---
 arch/x86/kvm/emulate.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +0 -3
index bf4284d..e8f146d 100644
@@ -4127,9 +4127,6 @@ twobyte_insn:
 		case 6: /* lmsw */
 			rc = em_lmsw(ctxt);
 			break;
-		case 5: /* not defined */
-			rc = emulate_ud(ctxt);
-			break;
 		case 7: /* invlpg*/
 			rc = em_invlpg(ctxt);
 			break;
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH 3/3 KVM: x86 emulator: Use opcode::execute for 0F 01 opcode

In reply to Avi Kivity
---
 arch/x86/kvm/emulate.c |   56 +++++++++++------------------------------------
 1 files changed, 13 insertions(+), 43 deletions(-)
arch/x86/kvm/emulate.c +13 -43
index e8f146d..522b4a5 100644
@@ -2625,18 +2625,13 @@ static int em_lgdt(struct x86_emulate_ctxt *ctxt)
 	return X86EMUL_CONTINUE;
 }
 
-static int em_svm(struct x86_emulate_ctxt *ctxt)
+static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
 {
 	struct decode_cache *c = &ctxt->decode;
 	int rc;
 
-	switch (c->modrm_rm) {
-	case 1:
-		rc = ctxt->ops->fix_hypercall(ctxt);
-		break;
-	default:
-		return X86EMUL_UNHANDLEABLE;
-	}
+	rc = ctxt->ops->fix_hypercall(ctxt);
+
 	/* Disable writeback. */
 	c->dst.type = OP_NONE;
 	return rc;
@@ -2909,7 +2904,7 @@ static struct opcode group7_rm1[] = {
 
 static struct opcode group7_rm3[] = {
 	DIP(SrcNone | ModRM | Prot | Priv, vmrun,   check_svme_pa),
-	DI(SrcNone | ModRM | Prot | VendorSpecific, vmmcall),
+	II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
 	DIP(SrcNone | ModRM | Prot | Priv, vmload,  check_svme_pa),
 	DIP(SrcNone | ModRM | Prot | Priv, vmsave,  check_svme_pa),
 	DIP(SrcNone | ModRM | Prot | Priv, stgi,    check_svme),
@@ -2961,15 +2956,17 @@ static struct opcode group6[] = {
 static struct group_dual group7 = { {
 	DI(ModRM | Mov | DstMem | Priv, sgdt),
 	DI(ModRM | Mov | DstMem | Priv, sidt),
-	DI(ModRM | SrcMem | Priv, lgdt), DI(ModRM | SrcMem | Priv, lidt),
-	DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
-	DI(SrcMem16 | ModRM | Mov | Priv, lmsw),
-	DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
+	II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
+	II(ModRM | SrcMem | Priv, em_lidt, lidt),
+	II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
+	II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
+	II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
 }, {
-	D(SrcNone | ModRM | Priv | VendorSpecific), EXT(0, group7_rm1),
+	I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
+	EXT(0, group7_rm1),
 	N, EXT(0, group7_rm3),
-	DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
-	DI(SrcMem16 | ModRM | Mov | Priv, lmsw), EXT(0, group7_rm7),
+	II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
+	II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
 } };
 
 static struct opcode group8[] = {
@@ -4107,33 +4104,6 @@ done:
 
 twobyte_insn:
 	switch (c->b) {
-	case 0x01: /* lgdt, lidt, lmsw */
-		switch (c->modrm_reg) {
-		case 0: /* vmcall */
-			rc = em_vmcall(ctxt);
-			break;
-		case 2: /* lgdt */
-			rc = em_lgdt(ctxt);
-			break;
-		case 3: /* lidt/vmmcall */
-			if (c->modrm_mod == 3)
-				return em_svm(ctxt);
-			else
-				return em_lidt(ctxt);
-			break;
-		case 4: /* smsw */
-			rc = em_smsw(ctxt);
-			break;
-		case 6: /* lmsw */
-			rc = em_lmsw(ctxt);
-			break;
-		case 7: /* invlpg*/
-			rc = em_invlpg(ctxt);
-			break;
-		default:
-			goto cannot_emulate;
-		}
-		break;
 	case 0x05: 		/* syscall */
 		rc = emulate_syscall(ctxt, ops);
 		break;
Signature · 1 line
1.7.1