You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When instructions refer to different memory locations, with regards to loads/reads from memory and stores/writes to memory there are 4 possible issues:
Another way to be able to predict the x86 processor behaviour is to have a model where each hardware thread has a FIFO buffer for writes, while reads are not immediate, not buffered. When reading a memory location, it is first looked up in the FIFO buffer for writes.
x86下每个hardware thread有一个用于store的FIFO buffer,但读不会有缓冲,读的时候会先查询store buffer。
这种称之为 total store ordering。
对于互斥的内存访问时需要设置一个全局锁global lock,比如lock指令flush write buffer到内存。
Types of Memory Barrier:LoadLoad, LoadStore, StoreLoad, StoreStore.
Acquire semantics is a property that can only apply to operations that read from shared memory, whether they are read-modify-write operations or plain loads. The operation is then considered a read-acquire. Acquire semantics prevent memory reordering of the read-acquire with any read or write operation that follows it in program order.
LoadLoad+LoadStore prevents loads from moving down, and anything from moving up.
Acquire语义可以用LoadLoad和LoadStore组合来实现。
Release semantics is a property that can only apply to operations that write to shared memory, whether they are read-modify-write operations or plain stores. The operation is then considered a write-release. Release semantics prevent memory reordering of the write-release with any read or write operation that precedes it in program order.
StoreStore+LoadStore prevents store from moving up, and anything from moving down.
Release语义可以用LoadStore和StoreStore来实现。
note 2:
// 64-ia-32-architectures-software-developer-manual
// 8.2.2 Memory Ordering in P6 and More Recent Processor Families
In a single-processor system for memory regions defined as write-back cacheable, the memory-ordering model
respects the following principles (some exceptions are omitted):
Reads are not reordered with other reads.
Writes are not reordered with older reads.
Writes to memory are not reordered with other writes.
Reads may be reordered with older writes to different locations but not with older writes to the same location.
Reads or writes cannot be reordered with I/O instructions, locked instructions, or serializing instructions.
Reads cannot pass earlier LFENCE and MFENCE instructions.
Writes cannot pass earlier LFENCE, SFENCE, and MFENCE instructions.
Loads May Be Reordered with Earlier Stores to Different Locations
Intel-64 memory-ordering model 允许在不同地址的前提下load可以重排到更早的store之前,
但相同地址的情况下则不可。
比如线程P1和P2:
Intra-Processor Forwarding Is Allowed
The memory-ordering model allows concurrent stores by two processors to be seen in different orders by those two
processors.
比如线程P1和P2:
P1
P2
mov [_x], 1 mov r1, [_x] mov r2, [_y]
mov [_y], 1 mov r3, [_y] mov r4, [_x]
初始_x和_y处均为0,则r2=0且r4=0的情况有可能发生。处理器对每个Processor的store顺序不做约束(imposes no constraints)。
Stores Are Transitively Visible
The memory-ordering model ensures transitive visibility of stores; stores that are causally related appear to all
processors to occur in an order consistent with the causality relation.
比如线程P1、P2、P3:
由于P1的store causally precede 于P2的store之前,内存模型保证了from the point of view of all processors都观测到同样的结果,
r2=1,故P2的store发生在P3的load之前,
由于x86内存模型不会重排序load(规则1),因此P3的load按照顺序执行,
综上P1的store先于P2的load先于P3的load,故r3不可能为0.
Stores Are Seen in a Consistent Order by Other Processors
As noted in Section 8.2.3.5, the memory-ordering model allows stores by two processors to be seen in different orders by those two processors. However, any two stores must appear to execute in the same order to all processors other than those performing the stores.
按照规则4的描述,x86允许两个processor的store被双方观测到不同的顺序(?TODO)。
但任意两次store必须被其它processor观测到同样的执行顺序。
Locked Instructions Have a Total Order
The memory-ordering model ensures that all processors agree on a single execution order of all locked instruc-
tions, including those that are larger than 8 bytes or are not naturally aligned.
Loads and Stores Are Not Reordered with Locked Instructions
The memory-ordering model prevents loads and stores from being reordered with locked instructions that execute
earlier or later. // lock-free programming
Activity
katsusan commentedon May 25, 2021
https://preshing.com/20120913/acquire-and-release-semantics/
http://blog.forecode.com/2010/01/29/barriers-to-understanding-memory-barriers/
Types of Memory Barrier:LoadLoad, LoadStore, StoreLoad, StoreStore.
Acquire semantics is a property that can only apply to operations that read from shared memory, whether they are read-modify-write operations or plain loads. The operation is then considered a read-acquire. Acquire semantics prevent memory reordering of the read-acquire with any read or write operation that follows it in program order.
LoadLoad+LoadStore prevents loads from moving down, and anything from moving up.
Acquire语义可以用LoadLoad和LoadStore组合来实现。
Release semantics is a property that can only apply to operations that write to shared memory, whether they are read-modify-write operations or plain stores. The operation is then considered a write-release. Release semantics prevent memory reordering of the write-release with any read or write operation that precedes it in program order.
StoreStore+LoadStore prevents store from moving up, and anything from moving down.
Release语义可以用LoadStore和StoreStore来实现。
katsusan commentedon May 27, 2021
note1:
// why x86/amd64 are called strong memory model?
katsusan commentedon May 27, 2021
note 2:
// 64-ia-32-architectures-software-developer-manual
// 8.2.2 Memory Ordering in P6 and More Recent Processor Families
respects the following principles (some exceptions are omitted):
katsusan commentedon May 27, 2021
note 3:
Intel-64 memory-ordering model 不允许load/store与同类型的operation重排序。
比如线程P1和P2:
mov [_y], 1
mov r2, [_x]
初始化_x和_y都为0,这种情况下r1=1&r2=0不会发生。如果要发生需要P1的两个store或者P2的两个load发生reorder,
违反了前面所述的memory model。
Intel-64 memory-ordering model 保证store操作不会被重排到previous load之前。
比如线程P1和P2:
mov [_y], 1
mov [_x], 1
初始状态下_x和_y处均为0,则r1=1且r2=1的情况不会发生,原因:
mov[_x],1已经执行,mov r2, [_y]发生于mov [_x], 1之前,mov r1, [_x]发生于mov [_y], 1之前,mov r2, [_y]发生于P1的mov [_y], 1之前,即r2不可能为1。Intel-64 memory-ordering model 允许在不同地址的前提下load可以重排到更早的store之前,
但相同地址的情况下则不可。
比如线程P1和P2:
mov r1, [ _y]
mov r2, [ _x]
初始_x和_y处均为0,则最后r1=0且r2=0的情况是允许的,对于每个线程,两次指令操作的地址不同,
因此可能会将其重排序使得赋值给_x和_y前就取其值到寄存器r1/r2中。
而若执行的指令变为
mov [_x], 1+mov r1, [_x],则初始_x=0的情况下r1不可能为0,因其涉及到了同样的地址_x。The memory-ordering model allows concurrent stores by two processors to be seen in different orders by those two
processors.
比如线程P1和P2:
mov r1, [_x]
mov r2, [_y]
mov r3, [_y]
mov r4, [_x]
初始_x和_y处均为0,则r2=0且r4=0的情况有可能发生。处理器对每个Processor的store顺序不做约束(imposes no constraints)。
The memory-ordering model ensures transitive visibility of stores; stores that are causally related appear to all
processors to occur in an order consistent with the causality relation.
比如线程P1、P2、P3:
mov [_y], 1
mov r2, [_y]
mov r3, [_x]
初始_x和_y均为0,则r1=1&r2=1&r3=0的情况不会发生。
As noted in Section 8.2.3.5, the memory-ordering model allows stores by two processors to be seen in different orders by those two processors. However, any two stores must appear to execute in the same order to all processors other than those performing the stores.
按照规则4的描述,x86允许两个processor的store被双方观测到不同的顺序(?TODO)。
但任意两次store必须被其它processor观测到同样的执行顺序。
The memory-ordering model ensures that all processors agree on a single execution order of all locked instruc-
tions, including those that are larger than 8 bytes or are not naturally aligned.
The memory-ordering model prevents loads and stores from being reordered with locked instructions that execute
earlier or later. // lock-free programming