本文試圖搞清楚cache幾個操作:clean、invalidate與flush的含義。由於只用過ARM和RISC-V,所以是從ARM和RISC-V的角度來說明。
cache line
cache line是cache的基本訪問單元。
cache line一般都會包含valid和dirty兩個狀態位,如下圖的v和d。
valid位表示當前cache line的內容是否有效。dirty位表示當前cache line的內容是否比記憶體上的要更新(即是否修改過)。
cache操作
clean和invalidata兩個操作都可以在ARM官方文件上找到描述,但是flush沒找到。而RISC-V則都沒找到。
clean
clean表示把cache line的dirty位清0,並把cache line的資料同步到記憶體上,目的是保證cache與記憶體的資料一致性。僅適用於使用回寫(write-back)策略的D-cache。
Applies to write-back data caches, and means that if the cache line contains stored data that has not yet been written out to main memory, it is written to main memory now, and the line is marked as clean.
invalidate
invalidate表示把cache line的valid位清0。
Means that the cache line (or all the lines in the cache) is marked as invalid, so that no cache hits occur for that line until it is re-allocated to an address. For write-back data caches, this does not include cleaning the cache line unless that is also stated.
flush
flush有查到兩種含義:
-
flush = invalidate
在《arm system developer‘s guide》中有描述到:
The term invalidate is sometimes used in place of the term flush.
-
flush = clean + invalidate
SiFive(提供基於RISC-V指令集CPU IP的公司)關於cache有一條自定義命令:CFLUSH.D.L1
,其中有描述:
writes back and invalidates line(s) in the L1 data cache
所以這裡flush相當於clean + invalidate。
參考資料
《ARM Architecture Reference Manual (2nd Edition)》
《ARM System Developer‘s Guide》
《SiFive E76-MC Core Complex Manual》