SDM revision 087 points out that apparently as of quite some time ago on
Intel hardware BSF and BSR may alter all arithmetic flags, not just ZF.
Because of the inconsistency (and because documentation doesn't look to
be quite right about PF), best we can do is simply take the flag values
from what the processor produces, just like we do for various other
arithmetic insns. (Note also that AMD and Intel have always been
disagreeing on arithmetic flags other than ZF.)

Signed-off-by: Jan Beulich <jbeul...@suse.com>
---
Since I happened to look at it, I wonder whether for e.g. SHLD/SHRD we
wouldn't be better off using _emulate_2op_SrcV_nobyte() as well; we
already do so in x86_emul_rmw(), after all.

--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -5268,16 +5268,14 @@ x86_emulate(
         break;
 
     case X86EMUL_OPC(0x0f, 0xbc): /* bsf or tzcnt */
-    {
-        bool zf;
-
-        asm ( "bsf %2,%0" ASM_FLAG_OUT(, "; setz %1")
-              : "=r" (dst.val), ASM_FLAG_OUT("=@ccz", "=qm") (zf)
-              : "rm" (src.val) );
-        _regs.eflags &= ~X86_EFLAGS_ZF;
         if ( (vex.pfx == vex_f3) && vcpu_has_bmi1() )
         {
-            _regs.eflags &= ~X86_EFLAGS_CF;
+            bool zf;
+
+            asm ( "bsf %2,%0" ASM_FLAG_OUT(, "; setz %1")
+                  : "=r" (dst.val), ASM_FLAG_OUT("=@ccz", "=qm") (zf)
+                  : "rm" (src.val) );
+            _regs.eflags &= ~(X86_EFLAGS_ZF | X86_EFLAGS_CF);
             if ( zf )
             {
                 _regs.eflags |= X86_EFLAGS_CF;
@@ -5286,25 +5284,23 @@ x86_emulate(
             else if ( !dst.val )
                 _regs.eflags |= X86_EFLAGS_ZF;
         }
-        else if ( zf )
+        else
         {
-            _regs.eflags |= X86_EFLAGS_ZF;
-            dst.type = OP_NONE;
+            emulate_2op_SrcV_srcmem("bsf", src, dst, _regs.eflags);
+            if ( _regs.eflags & X86_EFLAGS_ZF )
+                dst.type = OP_NONE;
         }
         break;
-    }
 
     case X86EMUL_OPC(0x0f, 0xbd): /* bsr or lzcnt */
-    {
-        bool zf;
-
-        asm ( "bsr %2,%0" ASM_FLAG_OUT(, "; setz %1")
-              : "=r" (dst.val), ASM_FLAG_OUT("=@ccz", "=qm") (zf)
-              : "rm" (src.val) );
-        _regs.eflags &= ~X86_EFLAGS_ZF;
         if ( (vex.pfx == vex_f3) && vcpu_has_lzcnt() )
         {
-            _regs.eflags &= ~X86_EFLAGS_CF;
+            bool zf;
+
+            asm ( "bsr %2,%0" ASM_FLAG_OUT(, "; setz %1")
+                  : "=r" (dst.val), ASM_FLAG_OUT("=@ccz", "=qm") (zf)
+                  : "rm" (src.val) );
+            _regs.eflags &= ~(X86_EFLAGS_ZF | X86_EFLAGS_CF);
             if ( zf )
             {
                 _regs.eflags |= X86_EFLAGS_CF;
@@ -5317,13 +5313,13 @@ x86_emulate(
                     _regs.eflags |= X86_EFLAGS_ZF;
             }
         }
-        else if ( zf )
+        else
         {
-            _regs.eflags |= X86_EFLAGS_ZF;
-            dst.type = OP_NONE;
+            emulate_2op_SrcV_srcmem("bsr", src, dst, _regs.eflags);
+            if ( _regs.eflags & X86_EFLAGS_ZF )
+                dst.type = OP_NONE;
         }
         break;
-    }
 
     case X86EMUL_OPC(0x0f, 0xbe): /* movsx rm8,r{16,32,64} */
         /* Recompute DstReg as we may have decoded AH/BH/CH/DH. */

Reply via email to