這篇文章探討ARM架構和總線協議如何來支持的。對于某款ARM處理器和總線CCI,CCN和CMN產品的具體實現,屬于實現層面的微架構,一般需要NDA,這里不予討論。
順便提一下,在ARMv8 架構下對應的是LDXR (load exclusive register 和STXR (store exclusiveregister)及其變種指令,另外,在ARMv8.1架構中引入atomic instruction, 例如LDADD (Atomic add),CAS(Compare and Swap)等。
Exclusive monitor
首先,作為一個愛問為什么的工程師,一定會想到LDXR/ STXR和一般的LDR/STR有什么區別。這個區別就在于LDXR除了向memory發起load請求外,還會記錄該memory所在地址的狀態(一般ARM處理器在同一個cache line大小,也就是64 byte的地址范圍內共用一個狀態),那就是Open和Exclusive。
我們可以認為一個叫做exclusive monitor的模塊來記錄。根據CPU訪問內存地址的屬性(在頁表里面定義),這個組件可能在處理器 L1 memory system, 處理器cluster level, 或者總線,DDR controller上。
下面是Arm ARM架構 [1] 文檔定義的狀態轉換圖
實例說明:
1)CPU1發起了一個LDXR的讀操作,記錄當前的狀態為Exclusive
2)CPU2發起了一個LDXR的讀操作,當前的狀態為Exclusive,保持不變
3)CPU2發起了一個STXR的寫操作,狀態從Exclusive變成Open,同時數據回寫到DDR
4)CPU1發起了一個STXR的寫操作,因為當前的exclusive monitor狀態為Open,寫失敗(假如程序這時用STR操作來寫,寫會成功,但是這個不是原子操作函數的本意,屬于編程錯誤)
假如有多個CPU,同時對一個處于Exclusive的memory region來進行寫,CPU有內部邏輯來保證串行化。
Monitor的狀態除了STXR會清掉,從Exclusive變成Open之外,還有其他因素也可以導致monitor的狀態被清掉,所以軟件在實現spinlock的時候,一般會用一個loop循環來實現,所謂“spin”。
Exclusive monitor實現所處的位置
根據LDXR/STXR 訪問的memory的屬性,需要的monitor可以在CPU內部,總線,也可以DDR controller(例如ARM DMC-400 [2]在每個memory interface 支持8個 exclusive access monitors)。
一般Memory屬性配置為 normal cacheable, shareable,這種情形下,CPU發起的exclusive access會終結在CPU cluster內部,對外的表現,比如cacheline fill和line eviction和正常的讀寫操作產生的外部行為是一樣的。具體實現上,需要結合local monitor的狀態管理和cache coherency 的處理邏輯,比如MESI/MOESI的cacheline的狀態管理來。
為方便大家理解,下面劃出一個monitor在一個假象SOC里面的邏輯圖(在一個真實芯片里面,不是所有monitor都會實現,需要和SOC vendor確認)
External exclusive monitor
對于normal non-cacheable,或者Device類型的memory屬性的memory地址,cpu會發出exclusive access的AXI 訪問(AxLOCK signals )到總線上去,總線需要有對應的External exclusive monitor支持,否則會返回錯誤。例如, 假如某個SOC不支持外部global exclusivemonitor,軟件把MMU disabled的情況下,啟動SMP Linux,系統是沒法啟動起來的,在spinlock處會掛掉。
AMBA AXI/ACE 規范
The exclusive access mechanism can provide semaphore-type operations without requiring the bus to remain dedicated to a particular master for the duration of the operation. This means the semaphore-type operations do not impact either the bus access latency or the maximum achievable bandwidth.
The AxLOCK signals select exclusive access, and the RRESP and BRESP signals indicate the success or failure of the exclusive access read or write respectively.
The slave requires additional logic to support exclusive access. The AXI protocol provides a mechanism to indicate when a master attempts an exclusive access to a slave that does not support it.
Atomic指令的支持
處理器,支持cache coherency協議的總線,或者DDR controller可以增加了一些簡單的運算,比如,在讀寫指令產生的memory訪問的過程中一并把簡單的運算給做了。
AMBA 5 [3] 里面增加了對Atomic transactions的支持:
AMBA 5 introduces Atomic transactions, which perform more than just a single access, and have some form of operation that is associated with the transaction.
Atomic transactions are suited to situations where the data is located a significant distance from the agent that must perform the operation. Previously, performing an operation that is atomically required pulling the data towards the agent, performing the operation, and then pushing the result back.
Atomic transactions enable sending the operation to the data, permitting the operation to be performed closer to where the data is located.
The key advantage of this approach is that it reduces the amount of time during which the data must be made inaccessible to other agents in the system.
支持4種Atomic transaction:AtomicStore ,AtomicLoad,AtomicSwap 和AtomicCompare
QA
1) Local monitor和Global monitor的使用場景
* Local monitor適用于訪問的memory屬為normal cacheable, shareable或者non-shareable的情況.
* Global monitor ,準確來說,external global exclusive monitor (處理器之外,在外部總線上)用于normal noncacheable或者device memory類型。比如可以用于一個Cortex-A處理器和一個Cortex-M 處理器(沒有內部cache)之間的同步。
2)多CPU下,多個LDREX,和STREX的排他性實現
* 各個處理器和總線的實現不同,可以從軟件上理解為和data coherency實現相結合,比如M(O)ESI協議[5],這是一種Invalidate-basedcache coherence protocol, 其中的key point就是當多個CPU在讀同一個cacheline的時候,在每個CPU的內部cache里面都有cacheline allocation, cacheline的狀態會變成Shared;但是當某個CPU做寫的時候,會把其它CPU里面的cacheline數據給invalidate掉,然后寫自己的cacheline數據,同時設置為Modified狀態,從而保證了數據的一致性。
* LDREX,本質上是一個LDR,CPU1做cache linefill,然后設置該line為E狀態(Exclusive),額外的一個作用是設置exclusive monitor的狀態為Exclusive;其他cpu做LDREX,該line也會分配到它的內部cache里面,狀態都設置為Shared ,也會設置本CPU的monitor的狀態。當一個CPU 做STREX時候,這個Write操作會把其它CPU里面的cacheline數據給invalidate掉。同時也把monitor的狀態清掉,從Exclusive變成Open的狀態,這個MESI協議導致cachline的狀態在多CPU的變化,是執行Write操作一次性改變的。這樣在保證數據一致性的同時,也保證了montitor的狀態更新同步改變。
3)比如舉一個多核的場景,一個核ldrex了,如果本核的local monitor會發生什么,外部的global monitor發生什么,開不開mmu,cache不cache,區別和影響是什么。
Ldrex/strex本來就是針對多核的場景來設計的,local monitor的狀態發生改變,不會影響外部的global monitor狀態。但是external global monitor的狀態發生改變,可以告訴處理器,把local monitor的狀態清掉。
Data coherency是通過硬件來支持的。對于normal cacheable類型的memory, MMU和DCache必須使能,否則CPU會把exclusive類型的數據請求發出處理器,這時需要外部monitor的支持。
-
ARM
+關注
關注
134文章
9321瀏覽量
375376 -
Linux
+關注
關注
87文章
11469瀏覽量
212895 -
總線
+關注
關注
10文章
2953瀏覽量
89404
原文標題:羅玉平: 關于ARM Linux原子操作的底層支持
文章出處:【微信號:LinuxDev,微信公眾號:Linux閱碼場】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
X86和ARM中的指令集支持原子操作

工程師深談ARM+FPGA的設計架構
ARM架構簡單介紹
設備仿真模擬軟件 QEMU 8.0 發布:改進對 ARM / RISC-V 架構支持
Linux環境下實現ARM9的CAN總線通信

ARM+Linux設計的CAN總線和MiniGUI的虛擬儀表
Emulex HBA CLI ARM架構上的Linux發行說明

評論