From: "Jan Beulich" Subject: [PATCH 2/3] x86emul: don't open code EFLAGS handling for 2-operand IMUL Date: Tue, 16 Aug 2016 03:33:36 -0600 Message-ID: <57B2FA100200007800106401@prv-mh.provo.novell.com> References: <57B2F93802000078001063E6@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartB08658E0.1__=" Return-path: Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bZakl-0005hg-6R for xen-devel@lists.xenproject.org; Tue, 16 Aug 2016 09:33:47 +0000 In-Reply-To: <57B2F93802000078001063E6@prv-mh.provo.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel Cc: Andrew Cooper List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartB08658E0.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Slightly extending the emulate_2op*() macro machinery makes it usable for IMUL r,r/m too, which has the benefit of smaller source code and the EFLAGS output being guaranteed to match actual hardware behavior. Signed-off-by: Jan Beulich --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -467,28 +467,29 @@ typedef union { "orl %"_LO32 _tmp",%"_LO32 _sav"; " =20 /* Raw emulation: instruction has two explicit operands. */ -#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy= )\ +#define __emulate_2op_nobyte(_op,_src,_dst,_eflags, wsx,wsy,wdx,wdy, = \ + lsx,lsy,ldx,ldy, qsx,qsy,qdx,qdy) = \ do{ unsigned long _tmp; = \ switch ( (_dst).bytes ) = \ { = \ case 2: = \ asm volatile ( = \ _PRE_EFLAGS("0","4","2") = \ - _op"w %"_wx"3,%1; " = \ + _op"w %"wsx"3,%"wdx"1; " = \ _POST_EFLAGS("0","4","2") = \ - : "+g" (_eflags), "+m" ((_dst).val), "=3D&r" (_tmp) = \ - : _wy ((_src).val), "i" (EFLAGS_MASK) ); = \ + : "+g" (_eflags), "+" wdy ((_dst).val), "=3D&r" (_tmp) = \ + : wsy ((_src).val), "i" (EFLAGS_MASK) ); = \ break; = \ case 4: = \ asm volatile ( = \ _PRE_EFLAGS("0","4","2") = \ - _op"l %"_lx"3,%1; " = \ + _op"l %"lsx"3,%"ldx"1; " = \ _POST_EFLAGS("0","4","2") = \ - : "+g" (_eflags), "+m" ((_dst).val), "=3D&r" (_tmp) = \ - : _ly ((_src).val), "i" (EFLAGS_MASK) ); = \ + : "+g" (_eflags), "+" ldy ((_dst).val), "=3D&r" (_tmp) = \ + : lsy ((_src).val), "i" (EFLAGS_MASK) ); = \ break; = \ case 8: = \ - __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy); = \ + __emulate_2op_8byte(_op, _src, _dst, _eflags, qsx, qsy, qdx, = qdy); \ break; = \ } = \ } while (0) @@ -505,7 +506,8 @@ do{ unsigned long _tmp; : _by ((_src).val), "i" (EFLAGS_MASK) ); = \ break; = \ default: = \ - __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy= );\ + __emulate_2op_nobyte(_op,_src,_dst,_eflags, _wx,_wy,"","m", = \ + _lx,_ly,"","m", _qx,_qy,"","m"); = \ break; = \ } = \ } while (0) @@ -519,8 +521,12 @@ do{ unsigned long _tmp; "b", "q", "w", "r", _LO32, "r", "", "r") /* Source operand is word, long or quad sized. */ #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) = \ - __emulate_2op_nobyte(_op, _src, _dst, _eflags, = \ - "w", "r", _LO32, "r", "", "r") + __emulate_2op_nobyte(_op, _src, _dst, _eflags, "w", "r", "", "m", = \ + _LO32, "r", "", "m", "", "r", "", "m") +/* Operands are word, long or quad sized and source may be in memory. */ +#define emulate_2op_SrcV_srcmem(_op, _src, _dst, _eflags) = \ + __emulate_2op_nobyte(_op, _src, _dst, _eflags, "", "m", "w", "r", = \ + "", "m", _LO32, "r", "", "m", "", "r") =20 /* Instruction has only one explicit operand (no source operand). */ #define emulate_1op(_op,_dst,_eflags) = \ @@ -559,13 +565,13 @@ do{ unsigned long _tmp; =20 /* Emulate an instruction with quadword operands (x86/64 only). */ #if defined(__x86_64__) -#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \ +#define __emulate_2op_8byte(_op, _src, _dst, _eflags, qsx, qsy, qdx, qdy) = \ do{ asm volatile ( \ _PRE_EFLAGS("0","4","2") \ - _op"q %"_qx"3,%1; " \ + _op"q %"qsx"3,%"qdx"1; " \ _POST_EFLAGS("0","4","2") \ - : "+g" (_eflags), "+m" ((_dst).val), "=3D&r" (_tmp) = \ - : _qy ((_src).val), "i" (EFLAGS_MASK) ); \ + : "+g" (_eflags), "+" qdy ((_dst).val), "=3D&r" (_tmp) = \ + : qsy ((_src).val), "i" (EFLAGS_MASK) ); \ } while (0) #define __emulate_1op_8byte(_op, _dst, _eflags) \ do{ asm volatile ( \ @@ -576,7 +582,7 @@ do{ asm volatile ( : "i" (EFLAGS_MASK) ); \ } while (0) #elif defined(__i386__) -#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) +#define __emulate_2op_8byte(_op, _src, _dst, _eflags, qsx, qsy, qdx, qdy) #define __emulate_1op_8byte(_op, _dst, _eflags) #endif /* __i386__ */ =20 @@ -4575,31 +4581,7 @@ x86_emulate( break; =20 case 0xaf: /* imul */ - _regs.eflags &=3D ~(EFLG_OF|EFLG_CF); - switch ( dst.bytes ) - { - case 2: - dst.val =3D ((uint32_t)(int16_t)src.val * - (uint32_t)(int16_t)dst.val); - if ( (int16_t)dst.val !=3D (uint32_t)dst.val ) - _regs.eflags |=3D EFLG_OF|EFLG_CF; - break; -#ifdef __x86_64__ - case 4: - dst.val =3D ((uint64_t)(int32_t)src.val * - (uint64_t)(int32_t)dst.val); - if ( (int32_t)dst.val !=3D dst.val ) - _regs.eflags |=3D EFLG_OF|EFLG_CF; - break; -#endif - default: { - unsigned long m[2] =3D { src.val, dst.val }; - if ( imul_dbl(m) ) - _regs.eflags |=3D EFLG_OF|EFLG_CF; - dst.val =3D m[0]; - break; - } - } + emulate_2op_SrcV_srcmem("imul", src, dst, _regs.eflags); break; =20 case 0xb2: /* lss */ --=__PartB08658E0.1__= Content-Type: text/plain; name="x86emul-imul-2op.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86emul-imul-2op.patch" x86emul: don't open code EFLAGS handling for 2-operand IMUL=0A=0ASlightly = extending the emulate_2op*() macro machinery makes it usable=0Afor IMUL = r,r/m too, which has the benefit of smaller source code and=0Athe EFLAGS = output being guaranteed to match actual hardware behavior.=0A=0ASigned-off-= by: Jan Beulich =0A=0A--- a/xen/arch/x86/x86_emulate/x86= _emulate.c=0A+++ b/xen/arch/x86/x86_emulate/x86_emulate.c=0A@@ -467,28 = +467,29 @@ typedef union {=0A "orl %"_LO32 _tmp",%"_LO32 _sav"; "=0A =0A = /* Raw emulation: instruction has two explicit operands. */=0A-#define = __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy)\=0A+#de= fine __emulate_2op_nobyte(_op,_src,_dst,_eflags, wsx,wsy,wdx,wdy, = \=0A+ lsx,lsy,ldx,ldy, qsx,qsy,qdx,qdy) = \=0A do{ unsigned long _tmp; = \=0A switch ( (_dst).bytes ) = \=0A { = \=0A case 2: = \=0A asm volatile ( = \=0A _PRE_EFLAGS("0","4","2") = \=0A- _op"w %"_wx"3,%1; " = \=0A+ _op"w %"wsx"3,%"wd= x"1; " \=0A _POST_EFLAGS(= "0","4","2") \=0A- : "+g" = (_eflags), "+m" ((_dst).val), "=3D&r" (_tmp) \=0A- = : _wy ((_src).val), "i" (EFLAGS_MASK) ); \=0A+ = : "+g" (_eflags), "+" wdy ((_dst).val), "=3D&r" (_tmp) = \=0A+ : wsy ((_src).val), "i" (EFLAGS_MASK) ); = \=0A break; = \=0A case 4: = \=0A asm volatile ( = \=0A _PRE_EFLAGS("0","4","2") = \=0A- _op"l %"_lx"3,%1; " = \=0A+ _op"l %"lsx"3,%"ldx"1; " = \=0A _POST_EFLAGS("0","4","2= ") \=0A- : "+g" (_eflags), = "+m" ((_dst).val), "=3D&r" (_tmp) \=0A- : _ly = ((_src).val), "i" (EFLAGS_MASK) ); \=0A+ = : "+g" (_eflags), "+" ldy ((_dst).val), "=3D&r" (_tmp) \=0A+ = : lsy ((_src).val), "i" (EFLAGS_MASK) ); \=0A = break; = \=0A case 8: = \=0A- __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy); = \=0A+ __emulate_2op_8byte(_op, _src, _dst, _eflags, qsx, = qsy, qdx, qdy); \=0A break; = \=0A } = \=0A } while (0)=0A@@ -505,7 +506,8 @@ do{ = unsigned long _tmp;=0A : _by ((_src).val), "i" (EFLAGS_MASK) = ); \=0A break; = \=0A default: = \=0A- __emulate_2op_nobyte(_op,_s= rc,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy);\=0A+ __emulate_2op_nobyte(= _op,_src,_dst,_eflags, _wx,_wy,"","m", \=0A+ = _lx,_ly,"","m", _qx,_qy,"","m"); \=0A break; = \=0A } = \=0A } while = (0)=0A@@ -519,8 +521,12 @@ do{ unsigned long _tmp;=0A = "b", "q", "w", "r", _LO32, "r", "", "r")=0A /* Source operand is word, = long or quad sized. */=0A #define emulate_2op_SrcV_nobyte(_op, _src, _dst, = _eflags) \=0A- __emulate_2op_nobyte(_op, _src, _dst, = _eflags, \=0A- "w", "r", _LO32, = "r", "", "r")=0A+ __emulate_2op_nobyte(_op, _src, _dst, _eflags, "w", = "r", "", "m", \=0A+ _LO32, "r", "", "m", "", = "r", "", "m")=0A+/* Operands are word, long or quad sized and source may = be in memory. */=0A+#define emulate_2op_SrcV_srcmem(_op, _src, _dst, = _eflags) \=0A+ __emulate_2op_nobyte(_op, _src, _dst, = _eflags, "", "m", "w", "r", \=0A+ "", "m", = _LO32, "r", "", "m", "", "r")=0A =0A /* Instruction has only one explicit = operand (no source operand). */=0A #define emulate_1op(_op,_dst,_eflags) = \=0A@@ -559,13 +565,13 @@ do{ unsigned = long _tmp;=0A =0A /* Emulate an instruction with quadword operands (x86/64 = only). */=0A #if defined(__x86_64__)=0A-#define __emulate_2op_8byte(_op, = _src, _dst, _eflags, _qx, _qy) \=0A+#define __emulate_2op_8byte(_op= , _src, _dst, _eflags, qsx, qsy, qdx, qdy) \=0A do{ asm volatile ( = \=0A _PRE_EFLAGS("0","= 4","2") \=0A- _op"q = %"_qx"3,%1; " \=0A+ = _op"q %"qsx"3,%"qdx"1; " \=0A = _POST_EFLAGS("0","4","2") \=0A- = : "+g" (_eflags), "+m" ((_dst).val), "=3D&r" (_tmp) = \=0A- : _qy ((_src).val), "i" (EFLAGS_MASK) ); = \=0A+ : "+g" (_eflags), "+" qdy ((_dst).val), "=3D&r" (_tmp) = \=0A+ : qsy ((_src).val), "i" (EFLAGS_MASK) ); = \=0A } while (0)=0A #define __emulate_1op_8byte(_op, _dst, = _eflags) \=0A do{ asm volatile ( = \=0A@@ -576,7 +582,7 @@ do{ asm = volatile (=0A : "i" (EFLAGS_MASK) ); = \=0A } while (0)=0A #elif defined(__i386__)=0A-#define = __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)=0A+#define = __emulate_2op_8byte(_op, _src, _dst, _eflags, qsx, qsy, qdx, qdy)=0A = #define __emulate_1op_8byte(_op, _dst, _eflags)=0A #endif /* __i386__ = */=0A =0A@@ -4575,31 +4581,7 @@ x86_emulate(=0A break;=0A =0A = case 0xaf: /* imul */=0A- _regs.eflags &=3D ~(EFLG_OF|EFLG_CF);=0A- = switch ( dst.bytes )=0A- {=0A- case 2:=0A- = dst.val =3D ((uint32_t)(int16_t)src.val *=0A- = (uint32_t)(int16_t)dst.val);=0A- if ( (int16_t)dst.val !=3D = (uint32_t)dst.val )=0A- _regs.eflags |=3D EFLG_OF|EFLG_CF;= =0A- break;=0A-#ifdef __x86_64__=0A- case 4:=0A- = dst.val =3D ((uint64_t)(int32_t)src.val *=0A- = (uint64_t)(int32_t)dst.val);=0A- if ( (int32_t)dst.val !=3D = dst.val )=0A- _regs.eflags |=3D EFLG_OF|EFLG_CF;=0A- = break;=0A-#endif=0A- default: {=0A- unsigned long = m[2] =3D { src.val, dst.val };=0A- if ( imul_dbl(m) )=0A- = _regs.eflags |=3D EFLG_OF|EFLG_CF;=0A- dst.val =3D = m[0];=0A- break;=0A- }=0A- }=0A+ emulate_2o= p_SrcV_srcmem("imul", src, dst, _regs.eflags);=0A break;=0A =0A = case 0xb2: /* lss */=0A --=__PartB08658E0.1__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__PartB08658E0.1__=--