为什么 Windows XP 和 Windows 7 的自旋锁实现不同?

发布于 2024-12-27 22:46:39 字数 1233 浏览 2 评论 0原文

我知道自旋锁是由 Windows 中的 hal.dll 导出的,因此我对自旋锁的代码进行了逆向工程。结果如下。

Windows XP 的反编译自旋锁。

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 的反编译自旋锁。

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;
}

为什么版本之间的代码不同?特别是,我没有看到Windows 7版本中添加的代码如何提高自旋锁在虚拟化方面的性能。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2025-01-03 22:46:39

KeAcquireSpinLockRaiseToSynchKeAcquireSpinLock

KeAcquireSpinLockRaiseToSynch保留供系统使用,并不意味着由接口驱动程序使用。

从而产生差异。

KeAcquireSpinLockRaiseToSynch is not the same as KeAcquireSpinLock.

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

Thus the difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文