RISC-V内存一致性模型

RISC-V内存一致性模型

  • [一、RVWMO Memory Consistency Mode](#一、RVWMO Memory Consistency Mode)
    • [1、Definition of the RVWMO Memory Mode](#1、Definition of the RVWMO Memory Mode)
      • [1.1Memory Model Primitives](#1.1Memory Model Primitives)
      • [1.2 Syntactic Dependencies](#1.2 Syntactic Dependencies)
      • [1.3 Preserved Program Order](#1.3 Preserved Program Order)
      • [1.4 内存模型公理](#1.4 内存模型公理)
        • [1.4.1 加载值公理(Load Value Axiom)](#1.4.1 加载值公理(Load Value Axiom))
        • [1.4.2 原子性公理(Atomicity Axiom)](#1.4.2 原子性公理(Atomicity Axiom))
        • [1.4.3 渐近性公理(Progress Axiom)](#1.4.3 渐近性公理(Progress Axiom))
    • [3、Source and Destination Register Listings](#3、Source and Destination Register Listings)
      • [3.1 RV32I Base Integer Instruction Set](#3.1 RV32I Base Integer Instruction Set)

一、RVWMO Memory Consistency Mode

RISC-V的内存一致性模型,内存一致性模型是一组规则,用于指定内存负载可以返回的值。RISC-V使用一种称为"RVWMO"(RISC-V弱内存排序)的内存模型,该模型旨在为架构师提供灵活性,以构建高性能、可扩展的设计,同时支持易于处理的编程模型。

在 RVWMO 下,从同一 hart 中其他内存指令的角度来看,在单个 hart 上运行的代码似乎按顺序执行但来自另一个 hart 的内存指令可能会观察到来自第一个 hart 的内存指令是以不同的顺序执行 。因此,多线程代码可能需要显式同步,以保证来自不同 hart 的内存指令之间的排序。基础 RISC-V ISA 为此目的提供了 FENCE 指令,而原子扩展 "A" 还定义了load-reserved/store-conditional and atomic read-modify-write指令。

1、Definition of the RVWMO Memory Mode

RVWMO 内存模型是根据全局内存顺序(the global memory order)定义的 ,全局内存顺序是所有 hart 生成的内存操作的总顺序。通常,多线程程序有许多不同的可能执行,每个执行逻辑都有自己对应的全局内存顺序。

全局内存顺序是在内存指令生成的原始加载和存储操作上定义的,任何满所有内存模型约束的执行都是合法执行。

1.1Memory Model Primitives

内存操作的程序顺序反映了生成每个加载和存储的指令在该 hart 的动态指令流中逻辑布局的顺序 ;也就是一个简单的有序处理器执行该 hart 指令的顺序。

内存访问指令产生内存操作。内存操作可以是加载操作、存储操作,也可以同时进行 。所有内存操作都是单拷贝原子的:它们永远无法在部分完成状态下被观察到

1.2 Syntactic Dependencies

假设 A 和 B 是两个内存操作设 I 和 J 分别是生成 A 和 B 的指令。如果 R 是 J 的地址源寄存器,则 B 对 A 具有句法地址依赖性。 如果 R 是 J 的地址源寄存器,则 J 对 I 具有句法依赖性 R ,B 对 A 具有句法数据依赖性 如果 B 是存储操作,R 是 J 的数据源寄存器,并且 J 对 I 有句法依赖性 如果有指令 M 在 i 和 j 之间排序,使得 m 是分支或间接跳转,并且 m 对 i 具有句法依赖性。

1.3 Preserved Program Order

1.4 内存模型公理

1.4.1 加载值公理(Load Value Axiom)

本质上加载值公理定义了一个具有load语义的指令,可以返回值。

复制代码
Each byte of each load i returns the value written to that byte by the store that is the latest in global memory order among the following stores:
 1. Stores that write that byte and that precede i in the global memory order
 2. Stores that write that byte and that precede i in program order
1.4.2 原子性公理(Atomicity Axiom)
复制代码
If r and w are paired load and store operations generated by aligned LR and SC instructions in a hart h,
 s is a store to byte x, and r returns a value written by s, then s must precede w in the global memory
 order, and there can be no store from a hart other than h to byte x following s and preceding w in the
 global memory order.

RISC-V 提供了两种原子性指令,一种是 AMO ,AMO 把读和写两种操作打包为一种原子性操作,因此该指令本身就是原子的,而另外一种 LR/SC 指令则不同,该公理说明了什么是成对的 LR/SC (原子性),只有成对的 LR/SC , SC 操作才能成功。

1.4.3 渐近性公理(Progress Axiom)
复制代码
 No memory operation may be preceded in the global memory order by an infinite sequence of other
 memory operations.

确保了一个内存操作应该在有限的时间内完成(被所有的线程可见)。如果渐近性公理不成立,那么用户将无法实现自旋锁等一些同步操作,因为可能一次写入永远都不会对其他线程可见。

如果一个具有缓存的系统,应该通过缓存一致性协议来确保在有限的时间内,内存操作对所有线程可见。

3、Source and Destination Register Listings

3.1 RV32I Base Integer Instruction Set