From: Guillaume Thouvenin Subject: [RFC] [PATCH] x86 emulator: emulate DIV instruction Date: Thu, 18 Dec 2008 13:49:22 +0100 Message-ID: <20081218134922.5b32603f@frecb000711> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit To: kvm Return-path: Received: from ecfrec.frec.bull.fr ([129.183.4.8]:57931 "EHLO ecfrec.frec.bull.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751361AbYLRMt2 (ORCPT ); Thu, 18 Dec 2008 07:49:28 -0500 Received: from localhost (localhost [127.0.0.1]) by ecfrec.frec.bull.fr (Postfix) with ESMTP id 7A50619D92E for ; Thu, 18 Dec 2008 13:49:30 +0100 (CET) Received: from ecfrec.frec.bull.fr ([127.0.0.1]) by localhost (ecfrec.frec.bull.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 29776-01 for ; Thu, 18 Dec 2008 13:49:22 +0100 (CET) Received: from cyclope.frec.bull.fr (cyclope.frec.bull.fr [129.183.4.9]) by ecfrec.frec.bull.fr (Postfix) with ESMTP id E041A19D921 for ; Thu, 18 Dec 2008 13:49:22 +0100 (CET) Sender: kvm-owner@vger.kernel.org List-ID: 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(-) diff --git a/arch/x86/kvm/x86_emulate.c b/arch/x86/kvm/x86_emulate.c index d174db7..e27de00 100644 --- a/arch/x86/kvm/x86_emulate.c +++ b/arch/x86/kvm/x86_emulate.c @@ -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; -- 1.6.0.4.623.g171d7