RISC-V 内存系统的内存访问顺序是怎样的呢

对于一个ARM处理器IP。以Cortex-M7为例可以找到相关的文档说明:

Memory system ordering of memory accesses

For most memory accesses caused by explicit memory access instructions, the memory system does not guarantee that the order in which the accesses complete, matches the program order of the instructions, providing it does not affect the behavior of the instruction sequence. Normally, if correct program execution depends on two memory accesses completing in program order, software must insert a memory barrier instruction between the memory access instructions, see Software ordering of memory accesses.

However, the memory system does guarantee some ordering of accesses to Device and Strongly-Ordered Memory. For two memory access instructions A1 and A2, in case of those are initiated by the same master interface, and if A1 occurs before A2 in program order, the ordering of the memory accesses caused by two instructions is:


Where:

-
Means that the memory system does not guarantee the ordering of the accesses.

<
Means that accesses are observed in program order, that is, A1 is always observed before A2.


可以看到在对普通内存访问A1之后,再对设备内存进行A2访问,内存系统并不保证顺序。
那么RISC-V处理器有相关的资料吗?

3 个赞

虽然暂时没有找到IP相关的文档明确描述普通内存和设备内存之间的顺序(大部分资料都只描述了普通内存和设备内存自身的内部顺序),但是看opensbi中确实给uart相关的设备的寄存器读写操作加了fence w,o 和fence i,r等屏障指令

4 个赞

riscv手册有专门章节:

riscv-isa-manual/src/rvwmo.adoc at main · riscv/riscv-isa-manual

3 个赞

感谢。

看上去有点复杂,预计需要一些时间来了解

现阶段感觉还得把主内存了解清晰了,目前对数据/地址/控制依赖等概念理解的还比较模糊

1 个赞

内存序是个折磨人的问题,理论上RVWMO和ARM的没啥本质不同,除了一些直观的不可乱序的情况,其它均不保证顺序。这里有个学习材料可参考memorder/riscv.md · laokz/OS kernel test - 码云 - 开源中国 (我现在已看不懂(

1 个赞

The use of DMB is rarely needed in Cortex-M processors because they do not reorder memory transactions. However, it is needed if the software is to be reused on other ARM processors, especially multi-master systems. For example:

  • DMA controller configuration. A barrier is required between a CPU memory access and a DMA operation.

关于ARM,上面这个资料好像是说取决于目标架构,Cortex M0 到 M4F等不需要屏障。

但是像是Cortex-M7这样的就需要。我一开始也是在看这个文档才想起来去了解内存顺序相关知识。


目前一个比较尴尬的地方在于RISC-V的《 The RISC-V Instruction Set Manual Volume I: Unprivileged Architecture》中【 RVWMO Memory Consistency Model, Version 2.0】章节里的信息:

This chapter defines the memory model for regular main memory operations. The interaction of the memory model with I/O memory, instruction fetches, FENCE.I, page table walks, and SFENCE.VMA is not (yet) formalized. Some or all of the above may be formalized in a future revision of this specification. Future ISA extensions such as the V vector and J JIT extensions will need to be incorporated into a future revision as well.

好像是说主内存和I/O内存的交互暂未正式化,需要等未来版本


然后RISC-V的 Unprivileged ISA附录A【 Appendix A: RVWMO Explanatory Material, Version 0.1】中有一些I/O相关的描述,目前还在看。

For I/O, the load value axiom and atomicity axiom in general do not apply, as both reads and writes might have device-specific side effects and may return values other than the value “written” by the most recent store to the same address. Nevertheless, the following preserved program order rules still generally apply for accesses to I/O memory: memory access a precedes memory access b in global memory order if a precedes b in program order and one or more of the following holds:

  1. a precedes b in preserved program order as defined in [memorymodel], with the exception that acquire and release ordering annotations apply only from one memory operation to another memory operation and from one I/O operation to another I/O operation, but not from a memory operation to an I/O nor vice versa
  2. a and b are accesses to overlapping addresses in an I/O region
  3. a and b are accesses to the same strongly ordered I/O region
  4. a and b are accesses to I/O regions, and the channel associated with the I/O region accessed by either a or b is channel 1
  5. a and b are accesses to I/O regions associated with the same channel (except for channel 0)
1 个赞

感谢老师的分享!