为什么 Windows XP 和 Windows 7 的自旋锁实现不同?
我知道自旋锁是由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
KeAcquireSpinLockRaiseToSynch
与KeAcquireSpinLock
。KeAcquireSpinLockRaiseToSynch
是 保留供系统使用,并不意味着由接口驱动程序使用。从而产生差异。
KeAcquireSpinLockRaiseToSynch
is not the same asKeAcquireSpinLock
.KeAcquireSpinLockRaiseToSynch
is reserved for system use, not meant to be used by interfacing drivers.Thus the difference.