3

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Sim Sun
  • 587
  • 1
  • 4
  • 28
  • What makes you think the changes were for performance reasons? – Raymond Chen Jan 20 '12 at 14:41
  • sorry, I mean the version of Windows 7 having better performance at Xen environment. I didnot say that clearly. And also I had done some test to certify that. – Sim Sun Jan 20 '12 at 17:45
  • It's unclear what the purpose of this question is. Are you looking for an explanation so that you can use the same technique in your own code? Are you debugging a problem that you traced to this change? Is this just idle curiosity? – Raymond Chen Jan 21 '12 at 05:38
  • The code quality is quite poor. It is rather important to know what the function pointer might be pointing to. Please post a link to commented source code. – Hans Passant Jan 21 '12 at 18:37
  • sorry, I donot have the source code. I did some reverse works to get the pseudocode. I also wanna know what the function pointer is pointing to. – Sim Sun Jan 21 '12 at 18:48
  • 3
    Idle curiosity is off topic for Stack Overflow. The FAQ says "You should only ask practical, answerable questions based on actual problems that you face." – Raymond Chen Jan 21 '12 at 21:42
  • 2
    @Raymond Chen: "Idle curiosity is off topic for Stack Overflow." Silly remark and ditto manoeuvering to get the op to acknowledge words placed before her. Considering the work the op has obviously invested "idle" is not a fair description - it was your adjective not her. The industry needs more curious people and might do better with less of your kind. – Olof Forshell Feb 02 '12 at 08:48

1 Answers1

3

KeAcquireSpinLockRaiseToSynch is not the same as KeAcquireSpinLock.

KeAcquireSpinLockRaiseToSynch is reserved for system use, not meant to be used by interfacing drivers.

Thus the difference.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Actually `KeAcquireSpinLockRaiseToSynch` is as same as `KeAcquireSpinLock`. I can prove that~ – Sim Sun Feb 01 '12 at 07:26
  • I reverse engneeried the code of `KeAcquireSpinLock` of Windows 7.The persudo code is as below: `void __cdecl KfAcquireSpinLock() { __writefsbyte(36, 2u); JUMPOUT(loc_8001371E); } ` – Sim Sun Feb 02 '12 at 11:14
  • `loc_8001371E` is the address of `KeAcquireSpinLockRaiseToSynch`. – Sim Sun Feb 02 '12 at 11:17