Question

I know the spinlock is exported by hal.dll in Windows, so I reverse engineered the code for the spin lock. The results are below.

Windows XP's decompiled spinlock.

unsigned __int32 __thiscall KfAcquireSpinLock(signed __int32 *this)
{
  unsigned __int32 result; // eax@1

  result = __readfsdword(36);
  __writefsdword(36, 2u);
  while ( _interlockedbittestandset(this, 0) )
  {
    while ( *this & 1 )
      _mm_pause();
  }
  return result;
}

Windows 7's decompiled spinlock.

unsigned __int32 __fastcall KeAcquireSpinLockRaiseToSynch(signed __int32 *a1)
{
  unsigned __int32 result; // eax@1
  int v2; // edx@4
  unsigned __int32 v3; // ST0C_4@7
  signed __int32 *v4; // ST08_4@7
  int v5; // ST04_4@7

  result = __readfsdword(36);
  __writefsbyte(36, 0x1Bu);
  while ( _interlockedbittestandset(a1, 0) )
  {
    v2 = 0;
    do
    {
      ++v2;
     if ( !(v2 & dword_8002D1B0) )
      {
        if ( dword_8002D19C & 0x40 )
        {
          v3 = result;
          v4 = a1;
          v5 = v2;
          dword_8002D1B4(v2);
          v2 = v5;
          a1 = v4;
          result = v3;
        }
      }
      _mm_pause();
    }
    while ( *a1 & 1 );
 }
  return result;
}

Why is the code different between the versions? In particular, I don't see how the code added in the Windows 7 version improves the spinlock's performance in virtualization.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top