Skip to contentSkip to conversation

x86 emulator: emulate DIV instruction

RFC PATCH New Guillaume Thouvenin 1 participant 1 message Dec 18, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 x86 emulator: emulate DIV instruction

Hello,

I'm trying to emulate DIV instruction (see the patch at the end). When
I run a div testcase (see below) it hangs without any message. If I added some printk in 'div' case of the emulator the didn't appear in the log. So everything happens like if we don't pass in the 'div' case (no printk in the log) but as it hangs, it means that the 'div' case introduces a problem.

Here is the testcase in kvm-userspace/user/test/x86/realmode.c

+void test_div(void)
+{
+ struct regs inregs = { .eax = 0xd }, outregs;
+ MK_INSN(div_test, "mov $0x2, %edi \n\t"
+ "divw %edi\n\t");
+ exec_in_big_real_mode(&inregs, &outregs,
+ insn_div_test,
+ insn_div_test_end - insn_div_test);
+
+ if (outregs.eax != 0x7)
+ print_serial("div: failure\n");
+ else
+ print_serial("div: success\n");
+}

In the object file I can see:

     2ec:       00 00                   add    %al,(%eax)
     2ee:       f7 f7                   div    %edi

f7 f7 is the code that I want to emulate.

when I ran

$ ./user/kvmctl ./user/test/x86/realmode.flat
nothing happens like in infinite loop.

Thanks for your help,
Guillaume

---
 arch/x86/kvm/x86_emulate.c |   27 ++++++++++++++++++++++++++-
 1 files changed, 26 insertions(+), 1 deletions(-)
arch/x86/kvm/x86_emulate.c +26 -1
index d174db7..e27de00 100644
@@ -295,7 +295,7 @@ static u32 group_table[] = {
 	[Group3*8] =
 	DstMem | SrcImm | ModRM, 0,
 	DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
-	0, 0, 0, 0,
+	0, 0, DstReg | SrcMem | ModRM, 0,
 	[Group4*8] =
 	ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
 	0, 0, 0, 0, 0, 0,
@@ -475,6 +475,28 @@ static u32 group2_table[] = {
 		}								\
 	} while (0)
 
+#define __emulate_1op_src(_op, _src, _eflags, _suffix)			\
+	do {								\
+		unsigned long _tmp;					\
+									\
+		__asm__ __volatile__ (					\
+			_PRE_EFLAGS("0", "3", "1")			\
+			_op _suffix " %1; "				\
+			_POST_EFLAGS("0", "3", "1")			\
+			: "=m" (_eflags), "=&r" (_tmp)			\
+			: "m"((_src).val), "i" (EFLAGS_MASK));		\
+	} while (0)
+
+#define emulate_1op_src(_op, _src, _eflags)                                    \
+	do {								\
+		switch ((_src).bytes) {				        \
+		case 1:	__emulate_1op(_op, _src, _eflags, "b"); break;	\
+		case 2:	__emulate_1op(_op, _src, _eflags, "w"); break;	\
+		case 4:	__emulate_1op(_op, _src, _eflags, "l"); break;	\
+		case 8:	ON64(__emulate_1op(_op, _src, _eflags, "q")); break; \
+		}							\
+	} while (0)
+
 #define __emulate_1op(_op, _dst, _eflags, _suffix)			\
 	do {								\
 		unsigned long _tmp;					\
@@ -1210,6 +1232,9 @@ static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
 	case 3:	/* neg */
 		emulate_1op("neg", c->dst, ctxt->eflags);
 		break;
+	case 6: /* div */
+		emulate_1op_src("div", c->src, ctxt->eflags);
+		break;
 	default:
 		DPRINTF("Cannot emulate %02x\n", c->b);
 		rc = X86EMUL_UNHANDLEABLE;
Signature · 1 line
1.6.0.4.623.g171d7