From: "Jan Beulich" Subject: [PATCH 3/4] x86/emulator: properly handle lzcnt and tzcnt Date: Wed, 30 Nov 2011 16:06:52 +0000 Message-ID: <4ED662AC020000780006467B@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part2807BD8C.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "xen-devel@lists.xensource.com" 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. --=__Part2807BD8C.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline These instructions are prefix selected flavors of bsf and bsr respectively, and hence the presences of the F3 prefix must be handled in the emulation code in order to avoid running into problems on newer CPUs. Signed-off-by: Jan Beulich --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -1058,6 +1058,9 @@ static bool_t vcpu_has( return rc =3D=3D X86EMUL_OKAY; } =20 +#define vcpu_has_lzcnt() vcpu_has(0x80000001, ECX, 5, ctxt, ops) +#define vcpu_has_bmi1() vcpu_has(0x00000007, EBX, 3, ctxt, ops) + #define vcpu_must_have(leaf, reg, bit) \ generate_exception_if(!vcpu_has(leaf, reg, bit, ctxt, ops), EXC_UD, = -1) #define vcpu_must_have_mmx() vcpu_must_have(0x00000001, EDX, 23) @@ -4357,13 +4360,24 @@ x86_emulate( dst.val =3D (uint8_t)src.val; break; =20 - case 0xbc: /* bsf */ { - int zf; + case 0xbc: /* bsf or tzcnt */ { + bool_t zf; asm ( "bsf %2,%0; setz %b1" : "=3Dr" (dst.val), "=3Dq" (zf) - : "r" (src.val), "1" (0) ); + : "r" (src.val) ); _regs.eflags &=3D ~EFLG_ZF; - if ( zf ) + if ( (rep_prefix =3D=3D REPE_PREFIX) && vcpu_has_bmi1() ) + { + _regs.eflags &=3D ~EFLG_CF; + if ( zf ) + { + _regs.eflags |=3D EFLG_CF; + dst.val =3D op_bytes * 8; + } + else if ( !dst.val ) + _regs.eflags |=3D EFLG_ZF; + } + else if ( zf ) { _regs.eflags |=3D EFLG_ZF; dst.type =3D OP_NONE; @@ -4371,13 +4385,28 @@ x86_emulate( break; } =20 - case 0xbd: /* bsr */ { - int zf; + case 0xbd: /* bsr or lzcnt */ { + bool_t zf; asm ( "bsr %2,%0; setz %b1" : "=3Dr" (dst.val), "=3Dq" (zf) - : "r" (src.val), "1" (0) ); + : "r" (src.val) ); _regs.eflags &=3D ~EFLG_ZF; - if ( zf ) + if ( (rep_prefix =3D=3D REPE_PREFIX) && vcpu_has_lzcnt() ) + { + _regs.eflags &=3D ~EFLG_CF; + if ( zf ) + { + _regs.eflags |=3D EFLG_CF; + dst.val =3D op_bytes * 8; + } + else + { + dst.val =3D op_bytes * 8 - 1 - dst.val; + if ( !dst.val ) + _regs.eflags |=3D EFLG_ZF; + } + } + else if ( zf ) { _regs.eflags |=3D EFLG_ZF; dst.type =3D OP_NONE; --=__Part2807BD8C.0__= Content-Type: text/plain; name="x86-emul-lzcnt.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-emul-lzcnt.patch" x86/emulator: properly handle lzcnt and tzcnt=0A=0AThese instructions are = prefix selected flavors of bsf and bsr=0Arespectively, and hence the = presences of the F3 prefix must be handled=0Ain the emulation code in = order to avoid running into problems on newer=0ACPUs.=0A=0ASigned-off-by: = Jan Beulich =0A=0A--- a/xen/arch/x86/x86_emulate/x86_emu= late.c=0A+++ b/xen/arch/x86/x86_emulate/x86_emulate.c=0A@@ -1058,6 +1058,9 = @@ static bool_t vcpu_has(=0A return rc =3D=3D X86EMUL_OKAY;=0A }=0A = =0A+#define vcpu_has_lzcnt() vcpu_has(0x80000001, ECX, 5, ctxt, ops)=0A+#d= efine vcpu_has_bmi1() vcpu_has(0x00000007, EBX, 3, ctxt, ops)=0A+=0A = #define vcpu_must_have(leaf, reg, bit) \=0A generate_exception_if(!vcpu= _has(leaf, reg, bit, ctxt, ops), EXC_UD, -1)=0A #define vcpu_must_have_mmx(= ) vcpu_must_have(0x00000001, EDX, 23)=0A@@ -4357,13 +4360,24 @@ x86_emulat= e(=0A dst.val =3D (uint8_t)src.val;=0A break;=0A =0A- = case 0xbc: /* bsf */ {=0A- int zf;=0A+ case 0xbc: /* bsf or = tzcnt */ {=0A+ bool_t zf;=0A asm ( "bsf %2,%0; setz %b1"=0A = : "=3Dr" (dst.val), "=3Dq" (zf)=0A- : "r" = (src.val), "1" (0) );=0A+ : "r" (src.val) );=0A = _regs.eflags &=3D ~EFLG_ZF;=0A- if ( zf )=0A+ if ( (rep_prefi= x =3D=3D REPE_PREFIX) && vcpu_has_bmi1() )=0A+ {=0A+ = _regs.eflags &=3D ~EFLG_CF;=0A+ if ( zf )=0A+ {=0A+ = _regs.eflags |=3D EFLG_CF;=0A+ dst.val =3D = op_bytes * 8;=0A+ }=0A+ else if ( !dst.val )=0A+ = _regs.eflags |=3D EFLG_ZF;=0A+ }=0A+ else if ( zf = )=0A {=0A _regs.eflags |=3D EFLG_ZF;=0A = dst.type =3D OP_NONE;=0A@@ -4371,13 +4385,28 @@ x86_emulate(=0A = break;=0A }=0A =0A- case 0xbd: /* bsr */ {=0A- int zf;=0A+ = case 0xbd: /* bsr or lzcnt */ {=0A+ bool_t zf;=0A asm ( = "bsr %2,%0; setz %b1"=0A : "=3Dr" (dst.val), "=3Dq" (zf)=0A- = : "r" (src.val), "1" (0) );=0A+ : "r" (src.val) = );=0A _regs.eflags &=3D ~EFLG_ZF;=0A- if ( zf )=0A+ = if ( (rep_prefix =3D=3D REPE_PREFIX) && vcpu_has_lzcnt() )=0A+ = {=0A+ _regs.eflags &=3D ~EFLG_CF;=0A+ if ( zf )=0A+ = {=0A+ _regs.eflags |=3D EFLG_CF;=0A+ = dst.val =3D op_bytes * 8;=0A+ }=0A+ else=0A+ = {=0A+ dst.val =3D op_bytes * 8 - 1 - dst.val;=0A+ = if ( !dst.val )=0A+ _regs.eflags |=3D = EFLG_ZF;=0A+ }=0A+ }=0A+ else if ( zf )=0A = {=0A _regs.eflags |=3D EFLG_ZF;=0A dst.type =3D = OP_NONE;=0A --=__Part2807BD8C.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__Part2807BD8C.0__=--