Armv8-M的Memory model机制

发布时间:2026/6/30 2:36:22
Armv8-M的Memory model机制 任何memory model重点在于规定memory access的order和ordering control机制。Armv8-M的memory model在order上有两类约束分别是observation和endpoint arrival。在ordering control上提供了barrier、acquire/release、exclusive等语义上的支持。Armv8-M是个Weakly Ordered Model总得来说PE为了性能可对无依赖的memory access指令重排、合并不保证和代码书写顺序完全一致。单核心、单线程下PE保证存在数据依赖的访问顺序符合程序语义不会出现逻辑错误。多核、外设交互场景下顺序无法自动保证必须通过ordering control强制同步。Memory accessArmv8-M memory model里的Memory accesses包含了instruction fetch、load data access和store data access。一个memory access可以由4个元素组成access typefetch, load, store, SIMD, MVE, exclusive, unprivileged, acquire/release, prefetch等addressaddressing modesdata formatscalar, vector, signed, unsigned, 8-bit, 16-bit, 32-bit, 64-bit, 128-bit等memory attributesDevice memory attribute和Cacheability attributeMemory access会产生Memory effects指令的memory effects是该指令的read、write或barrier effects。对于访问memory的指令来说对于该指令所读取的每个location都会产生read effect对于该指令所写入的每个location都会产生write effect一条指令可能既产生read effect也产生write effect。当且仅当指令A1在程序顺序中发生在A2之前时我们说指令A1的memory effect在程序顺序中出现在指令A2之前。Memory attribute关于memory attributes和memory type得注意两者有本质的区别memory attributes指的是memory access携带的属性memory type指的是下游memory的类型Memory type有device memory和normal memory。通常来说normal memory的地址区间会分配Cacheability attributesdevice memory的地址区间会分配Device memory attributes。所以我们平常不会怎么注意它们的区别。但如果Device memory地址区间分配了Cacheability attributes或者Normal memory地址区间分配了Device memory attributes会引起很多UNPREDICATABLE的行为了这是在software和system层面要控制规划好的。Cacheability attributes可以继续往下分为cacheable attributesWrite-Through CacheableWrite-Back Cacheablenon-cacheable attributesshareable另外每个memory attributes都会跟着一个shareability attributes它有Non-shareableInner-shareableOuter-shareableDevice和Non-cacheable attributes默认是shareable的其它attributes既可以是shareable也可以是non-shareable。对于具有Inner-shareable Shareability attribute的memory来说data accesses的数据对于Inner Shareability domain中所有的observers是一致的。对于具有Outer-shareable Shareability attribute的memory来说data accesses的数据对于Outer Shareability domain中所有的observers是一致的。PMSAv8-MArmv8-M引入PMSAv8 保护memory架构包含 MPU 内存权限保护、SAU/IDAU 安全隔离两大部分是 Cortex-M 内存安全与隔离的核心。MPU采用统一内存模型每个区域同时控制指令取指和数据访问的权限与属性。配置内存的特权 / 用户级读写权限、执行权限XN 禁止执行、PXN 特权级禁止执行、内存类型与缓存属性访问违规时触发的异常。AU将内存分为三级安全属性安全等级从低到高非安全、安全可调用NSC、安全不可调用。AU包含两个单元SAU内核内置安全属性单元 和 IDAU芯片厂商实现的外部属性单元IDAU 的属性优先级高于 SAU所有安全属性边界必须 32 字节对齐。System Address Map是架构对 32 位地址空间的标准化分区与默认属性定义是整个 Cortex-M 内存系统的基础底版。它的核心作用覆盖硬件默认行为、软件生态兼容、系统集成、安全与正确性保障多个维度。AtomicityArmv8-M memory model还有个重要的概念就是atomicity具体有single-copy atomicity、multi-copy atomicity和other multi-copy atomicity理解了这几个概念基本就理解了memory model的精髓。无论x86架构、RISC-V架构、ARM架构还是任何其它CPU架构都手到擒来。