Skip to contentSkip to conversation

x86emul: adjust BSF/BSR/LZCNT/TZCNT behavior as to EFLAGS

PATCH In review Jan Beulich 2 participants 4 messages Jul 14, 2025 – Jul 15, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86emul: adjust BSF/BSR/LZCNT/TZCNT behavior as to EFLAGS

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.) To be both safe (against further anomalies) and consistent, extend this to {L,T}ZCNT as well. (Emulating the two insns correctly even when underlying hardware doesn't support it was perhaps nice, but yielded guest-observable inconsistencies.)

---
v2: Use emulate_2op_SrcV_srcmem() also for {L,T}ZCNT.

xen/arch/x86/x86_emulate/x86_emulate.c +12 -48
@@ -5270,62 +5270,26 @@ 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;
-            if ( zf )
-            {
-                _regs.eflags |= X86_EFLAGS_CF;
-                dst.val = op_bytes * 8;
-            }
-            else if ( !dst.val )
-                _regs.eflags |= X86_EFLAGS_ZF;
-        }
-        else if ( zf )
+        if ( vex.pfx == vex_f3 )
+            emulate_2op_SrcV_srcmem("rep; bsf", src, dst, _regs.eflags);
+        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;
-            if ( zf )
-            {
-                _regs.eflags |= X86_EFLAGS_CF;
-                dst.val = op_bytes * 8;
-            }
-            else
-            {
-                dst.val = op_bytes * 8 - 1 - dst.val;
-                if ( !dst.val )
-                    _regs.eflags |= X86_EFLAGS_ZF;
-            }
-        }
-        else if ( zf )
+        if ( vex.pfx == vex_f3 )
+            emulate_2op_SrcV_srcmem("rep; bsr", src, dst, _regs.eflags);
+        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. */
Reviewed-by on v2 1/1
#1 lore raw
Quoted 3 lines · Jan Beulich

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

It's probably worth saying errata explicitly.  There are a whole bunch of Intel CPUs where the behaviour doesn't match CPUID.

Quoted 10 lines

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.) To be both safe (against further anomalies) and consistent, extend this to {L,T}ZCNT as well. (Emulating the two insns correctly even when underlying hardware doesn't support it was perhaps nice, but yielded guest-observable inconsistencies.)

This is one of the more messy parts of x86, and that's saying something.

Quoted 20 lines

---
v2: Use emulate_2op_SrcV_srcmem() also for {L,T}ZCNT.

xen/arch/x86/x86_emulate/x86_emulate.c +0 -11
@@ -5270,62 +5270,26 @@ 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;
-            if ( zf )

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 5274

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -8
-            {
-                _regs.eflags |= X86_EFLAGS_CF;
-                dst.val = op_bytes * 8;
-            }
-            else if ( !dst.val )
-                _regs.eflags |= X86_EFLAGS_ZF;
-        }
-        else if ( zf )
+        if ( vex.pfx == vex_f3 )
+            emulate_2op_SrcV_srcmem("rep; bsf", src, dst, _regs.eflags);

Do we need the ; ?

We surely don't on 4.21, but I presume there are bugs in older binutils?  (All Clangs back to 3.5 seem happy)

~Andrew

#2 lore raw
Quoted 7 lines · Andrew Cooper
Quoted 3 lines · Jan Beulich

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

It's probably worth saying errata explicitly.  There are a whole bunch of Intel CPUs where the behaviour doesn't match CPUID.

Okay, I've adjusted the wording slightly.

Quoted 9 lines
Quoted 9 lines

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.) To be both safe (against further anomalies) and consistent, extend this to {L,T}ZCNT as well. (Emulating the two insns correctly even when underlying hardware doesn't support it was perhaps nice, but yielded guest-observable inconsistencies.)

Quoted 3 lines
Quoted 1 line

Thanks.

Quoted 30 lines
Quoted 17 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -11
@@ -5270,62 +5270,26 @@ 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;
-            if ( zf )

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 5274

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -8
-            {
-                _regs.eflags |= X86_EFLAGS_CF;
-                dst.val = op_bytes * 8;
-            }
-            else if ( !dst.val )
-                _regs.eflags |= X86_EFLAGS_ZF;
-        }
-        else if ( zf )
+        if ( vex.pfx == vex_f3 )
+            emulate_2op_SrcV_srcmem("rep; bsf", src, dst, _regs.eflags);

Do we need the ; ?

Quoted 2 lines

We surely don't on 4.21, but I presume there are bugs in older binutils?  (All Clangs back to 3.5 seem happy)

Right, we don't really need this on staging. I can omit it and then hopefully not forget to add it back when backporting (which I was intending to do).

Jan

#3 lore raw
In reply to Andrew Cooper
Quoted 31 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +0 -11
@@ -5270,62 +5270,26 @@ 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;
-            if ( zf )

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 5274

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -8
-            {
-                _regs.eflags |= X86_EFLAGS_CF;
-                dst.val = op_bytes * 8;
-            }
-            else if ( !dst.val )
-                _regs.eflags |= X86_EFLAGS_ZF;
-        }
-        else if ( zf )
+        if ( vex.pfx == vex_f3 )
+            emulate_2op_SrcV_srcmem("rep; bsf", src, dst, _regs.eflags);

Do we need the ; ?

Quoted 2 lines

We surely don't on 4.21, but I presume there are bugs in older binutils?  (All Clangs back to 3.5 seem happy)

Actually we can use TZCNT here and LZCNT below with gas 2.25 (and Clang looks to be happy too, even version 3.0). I expect that's preferable over merely omitting the semicolons?

Jan