diff mbox

[1/2] x86 emulator: Add unary mul, imul, div, and idiv instructions

Message ID 1281234459-9248-1-git-send-email-m.gamal005@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Mohammed Gamal Aug. 8, 2010, 2:27 a.m. UTC
None
diff mbox

Patch

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 7d78832..6d1ec53 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -315,6 +315,31 @@  struct group_dual {
 		}							\
 	} while (0)
 
+#define __emulate_1op_src(_op, _src, _ax, _dx, _eflags, _suffix)		\
+	do {								\
+		unsigned long _tmp;					\
+									\
+		__asm__ __volatile__ (					\
+			_PRE_EFLAGS("0", "4", "1")			\
+			_op _suffix " %5; "				\
+			_POST_EFLAGS("0", "4", "1")			\
+			: "=m" (_eflags), "=&r" (_tmp),			\
+			  "=a" (_ax), "=d" (_dx)			\
+			: "i" (EFLAGS_MASK), "m" ((_src).val),		\
+			  "a" (_ax), "d" (_dx));			\
+	} while (0)							
+
+/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
+#define emulate_1op_src(_op, _src, _ax, _dx, _eflags)				\
+	do {									\
+		switch((_src).bytes) {						\
+		case 1: __emulate_1op_src(_op, _src, _ax, _dx, _eflags, "b"); break; \
+		case 2: __emulate_1op_src(_op, _src, _ax, _dx,  _eflags, "w"); break; \
+		case 4: __emulate_1op_src(_op, _src, _ax, _dx, _eflags, "l"); break; \
+		case 8: ON64(__emulate_1op_src(_op, _src, _dx, _ax, _eflags, "q")); break; \
+		}							\
+	} while (0)
+
 /* Fetch next part of the instruction being emulated. */
 #define insn_fetch(_type, _size, _eip)                                  \
 ({	unsigned long _x;						\
@@ -1354,6 +1379,8 @@  static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
 			       struct x86_emulate_ops *ops)
 {
 	struct decode_cache *c = &ctxt->decode;
+	unsigned long *eax = &c->regs[VCPU_REGS_RAX];
+	unsigned long *edx = &c->regs[VCPU_REGS_RDX];
 
 	switch (c->modrm_reg) {
 	case 0 ... 1:	/* test */
@@ -1365,6 +1392,18 @@  static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
 	case 3:	/* neg */
 		emulate_1op("neg", c->dst, ctxt->eflags);
 		break;
+	case 4: /* mul */
+		emulate_1op_src("mul", c->src, *eax, *edx, ctxt->eflags);
+		break;
+	case 5: /* imul */
+		emulate_1op_src("imul", c->src, *eax, *edx, ctxt->eflags);
+		break;
+	case 6: /* div */
+		emulate_1op_src("div", c->src, *eax, *edx, ctxt->eflags);
+		break;
+	case 7: /* idiv */
+		emulate_1op_src("idiv", c->src, *eax, *edx, ctxt->eflags);
+		break;
 	default:
 		return 0;
 	}
@@ -2120,7 +2159,7 @@  static struct opcode group1A[] = {
 static struct opcode group3[] = {
 	D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
 	D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
-	X4(D(Undefined)),
+	X4(D(SrcMem | ModRM)),
 };
 
 static struct opcode group4[] = {