Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?

Windows内核调试

有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


Coding 2008-08-28, 09:45 上午

Windows下的各种同步机制有各式各样的特征的限制,我觉得不了解内部如何实现的,只能简单的背诵那些东西,所以想从根本上了解如何实现的。

不知道谁见过些什么资料。

我手头的看的是Microsoft Windows Internals

 

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


王宇 2008-08-28, 09:58 上午
呵呵,前段时间刚好看过一些,一起讨论讨论。
我觉得最好的资料是Win源代码和Intel手册3A第七章。

问楼主一些问题,如果您能回答就说明你理论上OK寮~ ^_^

例如,钩子的实质是什么、32位MOV指令是单核安全的吗、32位MOV指令是多核安全的吗、计数用i++、i--代码安全吗?INC、DEC呢?关中断是安全的吗、提升IRQL呢、什么是lock指令前缀、xchg多核安全吗?lock sti会怎样?缓存一致性协议..............

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


王宇 2008-08-28, 10:02 上午
OSR有一篇《A Catalog of Windows NT Synchronization Mechanisms》
其实也是泛泛而谈..
我原来想拿这篇文章做“经典阅读”版块的第三则学习文章的。(“经典阅读”还没开张呢.. 看来我是太懒了... :( )

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


Coding 2008-08-28, 10:17 上午

厄,两个字 不懂!!!!!!!!!!!!!我手上有三篇MS的文档,还不错,但是没有涉及到内部实现,共享出来不知道怎么上传,写上Link,感兴趣的去下载吧

Scheduling, Thread Context, and IRQL

 http://www.microsoft.com/whdc/hwdev/driver/IRQL.mspx

Locks, Deadlocks, and Synchronization

 http://www.microsoft.com/whdc/hwdev/driver/LOCKS.mspx

这片Vista的文章还没看 

Interrupt Architecture Enhancements in Microsoft Windows Vista

 http://www.microsoft.com/whdc/hwdev/bus/pci/MSI.mspx

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


格蠹老雷 2008-08-28, 12:43 下午
这个方向非常好。Jeffrey Richter的那本《Windows核心编程》从API角度讲的很不错了。但是内核态的还很需要挖掘。其实可以使用Jeffrey的方法,也就是写代码来试验、观察和验证,外加调试追踪。

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


格蠹老雷 2008-08-28, 13:02 下午
刚刚上传了我以前写的一篇《CPU同步机制漫谈》,里面介绍了一些入门级的东西。
http://advdbg.org/download/storeshow.aspx?id=7808a3ed-2940-4356-9256-9f4e4d9d2c98

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


MJ0011 2008-09-25, 19:09 下午
其实就是总线锁呀。。没有那么复杂的。。看一下代码就明白了。。。

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


王宇 2008-09-26, 05:45 上午

嗯 是的!

我只是一开始很好奇,像:
RtlInterlockedSetBitsDiscardReturn 是 lock or [ecx], eax
RtlInterlockedAndBitsDiscardReturn 是 lock and [ecx], eax
InterlockedExchangeAdd 是 lock xadd [ecx], eax
InterlockedCompareExchange 是 lock cmpxchg [ecx], edx
等等等等
可凭啥
InterlockedExchange 是 xchg [ecx], eax 呢,难道这指令就是多核安全的?!

后来翻了Intel手册才知道,嗨,还真是安全的,请看:
7.1.2.1 Automatic Locking
The operations on which the processor automatically follows the LOCK semantics are as follows:
- When executing an XCHG instruction that references memory.

当时我这个雷啊......

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


王宇 2008-09-26, 06:06 上午
显式强制加lock前缀的适用范围是:

- The bit test and modify instructions (BTS, BTR, and BTC).
- The exchange instructions (XADD, CMPXCHG, and CMPXCHG8B).
- The LOCK prefix is automatically assumed for XCHG instruction.
- The following single-operand arithmetic and logical instructions: INC, DEC, NOT,
and NEG.
- The following two-operand arithmetic and logical instructions: ADD, ADC, SUB,
SBB, AND, OR, and XOR.

另外的任何指令,以及目标操作数是寄存器操作数的情况都将导致#UD(所以要volatile关键字修饰)。

但是还真的有内核利用这个的——L4的内核。


void * L4_KernelInterface(
L4_Word_t *ApiVersion,
L4_Word_t *ApiFlags,
L4_Word_t *KernelId
)
{
void * base_address;

__asm__ __volatile__ (
" lock; nop"
: /* outputs */
"=a" (base_address),
"=c" (*ApiVersion),
"=d" (*ApiFlags),
"=S" (*KernelId)
/* no inputs */
/* no clobbers */
);

return base_address; /*注意这里!*/
}


case 0xf0: /* lock prefix */
if (space->get_from_user(addr_offset(addr, 1)) == 0x90)
{
/* lock; nop */
frame->eax = (u32_t)space->get_kip_page_area().get_base();
frame->ecx = get_kip()->api_version;
frame->edx = get_kip()->api_flags;
frame->esi = get_kip()->get_kernel_descriptor()->kernel_id.get_raw();
frame->eip+= 2;
return;
}

他们利用 lock prefix + 非法指令产生异常,然后自己写处理代码,处理代码居然是返回KernelInterface的基址...

很是有创意,不过安全性呢?

Re: 有讨论x86上InterlockedXXX和Spinlocks内部机制的文章吗?


admin 2008-10-16, 10:56 上午
比较Linux和Windows下同步机制的系列文章:
http://www.ibm.com/developerworks/cn/linux/l-ipc2lin2.html
熟悉Windows下同步机制的,可以用这篇文章来了解一下Linux下的情况;反之依然

Powered by Community Server Powered by CnForums.Net