All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2]  Trap and propagate divide errors when emulating DIV
@ 2010-08-26  8:58 Avi Kivity
  2010-08-26  8:59 ` [PATCH v2 1/2] KVM: x86 emulator: add macros for executing instructions that may trap Avi Kivity
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Avi Kivity @ 2010-08-26  8:58 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

KVM recently started emulating DIV and IDIV.  However, those instructions
trap when given the right operands.  Since figuring out when to trap or not
is difficult, we just execute the instruction and see if the processor trapped
or not.

Avi Kivity (2):
  x86: allow kernel exception fixup for divide errors (#DE)
    - dropped
  KVM: x86 emulator: add macros for executing instructions that may
    trap
  KVM: x86 emulator: trap and propagate #DE from DIV and IDIV
    - propagate return code to x86_emulate_insn()

 arch/x86/kvm/emulate.c |   63 ++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 59 insertions(+), 4 deletions(-)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] KVM: x86 emulator: add macros for executing instructions that may trap
  2010-08-26  8:58 [PATCH v2 0/2] Trap and propagate divide errors when emulating DIV Avi Kivity
@ 2010-08-26  8:59 ` Avi Kivity
  2010-08-26  8:59 ` [PATCH v2 2/2] KVM: x86 emulator: trap and propagate #DE from DIV and IDIV Avi Kivity
  2010-08-26 18:15 ` [PATCH v2 0/2] Trap and propagate divide errors when emulating DIV Marcelo Tosatti
  2 siblings, 0 replies; 4+ messages in thread
From: Avi Kivity @ 2010-08-26  8:59 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

Like DIV and IDIV.

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 arch/x86/kvm/emulate.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index bd2e563..d6baf31 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -331,6 +331,27 @@ struct group_dual {
 			  "a" (_rax), "d" (_rdx));			\
 	} while (0)
 
+#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
+	do {								\
+		unsigned long _tmp;					\
+									\
+		__asm__ __volatile__ (					\
+			_PRE_EFLAGS("0", "5", "1")			\
+			"1: \n\t"					\
+			_op _suffix " %6; "				\
+			"2: \n\t"					\
+			_POST_EFLAGS("0", "5", "1")			\
+			".pushsection .fixup,\"ax\" \n\t"		\
+			"3: movb $1, %4 \n\t"				\
+			"jmp 2b \n\t"					\
+			".popsection \n\t"				\
+			_ASM_EXTABLE(1b, 3b)				\
+			: "=m" (_eflags), "=&r" (_tmp),			\
+			  "+a" (_rax), "+d" (_rdx), "+qm"(_ex)		\
+			: "i" (EFLAGS_MASK), "m" ((_src).val),		\
+			  "a" (_rax), "d" (_rdx));			\
+	} while (0)
+
 /* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
 #define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags)			\
 	do {									\
@@ -342,6 +363,28 @@ struct group_dual {
 		}							\
 	} while (0)
 
+#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex)	\
+	do {								\
+		switch((_src).bytes) {					\
+		case 1:							\
+			__emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx,	\
+						 _eflags, "b", _ex);	\
+			break;						\
+		case 2:							\
+			__emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
+						 _eflags, "w", _ex);	\
+			break;						\
+		case 4:							\
+			__emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
+						 _eflags, "l", _ex);	\
+			break;						\
+		case 8: ON64(						\
+			__emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
+						 _eflags, "q", _ex));	\
+			break;						\
+		}							\
+	} while (0)
+
 /* Fetch next part of the instruction being emulated. */
 #define insn_fetch(_type, _size, _eip)                                  \
 ({	unsigned long _x;						\
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] KVM: x86 emulator: trap and propagate #DE from DIV and IDIV
  2010-08-26  8:58 [PATCH v2 0/2] Trap and propagate divide errors when emulating DIV Avi Kivity
  2010-08-26  8:59 ` [PATCH v2 1/2] KVM: x86 emulator: add macros for executing instructions that may trap Avi Kivity
@ 2010-08-26  8:59 ` Avi Kivity
  2010-08-26 18:15 ` [PATCH v2 0/2] Trap and propagate divide errors when emulating DIV Marcelo Tosatti
  2 siblings, 0 replies; 4+ messages in thread
From: Avi Kivity @ 2010-08-26  8:59 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 arch/x86/kvm/emulate.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index d6baf31..b955c96 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -505,6 +505,12 @@ static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
 	emulate_exception(ctxt, TS_VECTOR, err, true);
 }
 
+static int emulate_de(struct x86_emulate_ctxt *ctxt)
+{
+	emulate_exception(ctxt, DE_VECTOR, 0, false);
+	return X86EMUL_PROPAGATE_FAULT;
+}
+
 static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
 			      struct x86_emulate_ops *ops,
 			      unsigned long eip, u8 *dest)
@@ -1459,6 +1465,7 @@ static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
 	struct decode_cache *c = &ctxt->decode;
 	unsigned long *rax = &c->regs[VCPU_REGS_RAX];
 	unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
+	u8 de = 0;
 
 	switch (c->modrm_reg) {
 	case 0 ... 1:	/* test */
@@ -1477,14 +1484,18 @@ static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
 		emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
 		break;
 	case 6: /* div */
-		emulate_1op_rax_rdx("div", c->src, *rax, *rdx, ctxt->eflags);
+		emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
+				       ctxt->eflags, de);
 		break;
 	case 7: /* idiv */
-		emulate_1op_rax_rdx("idiv", c->src, *rax, *rdx, ctxt->eflags);
+		emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
+				       ctxt->eflags, de);
 		break;
 	default:
 		return X86EMUL_UNHANDLEABLE;
 	}
+	if (de)
+		return emulate_de(ctxt);
 	return X86EMUL_CONTINUE;
 }
 
@@ -3393,8 +3404,9 @@ special_insn:
 		ctxt->eflags ^= EFLG_CF;
 		break;
 	case 0xf6 ... 0xf7:	/* Grp3 */
-		if (emulate_grp3(ctxt, ops) != X86EMUL_CONTINUE)
-			goto cannot_emulate;
+		rc = emulate_grp3(ctxt, ops);
+		if (rc != X86EMUL_CONTINUE)
+			goto done;
 		break;
 	case 0xf8: /* clc */
 		ctxt->eflags &= ~EFLG_CF;
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2]  Trap and propagate divide errors when emulating DIV
  2010-08-26  8:58 [PATCH v2 0/2] Trap and propagate divide errors when emulating DIV Avi Kivity
  2010-08-26  8:59 ` [PATCH v2 1/2] KVM: x86 emulator: add macros for executing instructions that may trap Avi Kivity
  2010-08-26  8:59 ` [PATCH v2 2/2] KVM: x86 emulator: trap and propagate #DE from DIV and IDIV Avi Kivity
@ 2010-08-26 18:15 ` Marcelo Tosatti
  2 siblings, 0 replies; 4+ messages in thread
From: Marcelo Tosatti @ 2010-08-26 18:15 UTC (permalink / raw)
  To: Avi Kivity; +Cc: kvm

On Thu, Aug 26, 2010 at 11:58:59AM +0300, Avi Kivity wrote:
> KVM recently started emulating DIV and IDIV.  However, those instructions
> trap when given the right operands.  Since figuring out when to trap or not
> is difficult, we just execute the instruction and see if the processor trapped
> or not.
> 
> Avi Kivity (2):
>   x86: allow kernel exception fixup for divide errors (#DE)
>     - dropped
>   KVM: x86 emulator: add macros for executing instructions that may
>     trap
>   KVM: x86 emulator: trap and propagate #DE from DIV and IDIV
>     - propagate return code to x86_emulate_insn()
> 
>  arch/x86/kvm/emulate.c |   63 ++++++++++++++++++++++++++++++++++++++++++++---
>  1 files changed, 59 insertions(+), 4 deletions(-)

Applied, thanks.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-08-26 19:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-26  8:58 [PATCH v2 0/2] Trap and propagate divide errors when emulating DIV Avi Kivity
2010-08-26  8:59 ` [PATCH v2 1/2] KVM: x86 emulator: add macros for executing instructions that may trap Avi Kivity
2010-08-26  8:59 ` [PATCH v2 2/2] KVM: x86 emulator: trap and propagate #DE from DIV and IDIV Avi Kivity
2010-08-26 18:15 ` [PATCH v2 0/2] Trap and propagate divide errors when emulating DIV Marcelo Tosatti

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.