Page Color (页面着色)

0) 首先讨论一下 cache line 索引定位的方式。

  给定一个 32 位虚拟地址,怎么定位 cache line 呢?
  答案是 VIPT (virtual indexed, physical taged), 即以虚拟地址作为 cache line 的 set 的索引,物理地址作为 tag 进行比较。

  比如 : 32k cache 8 way 组相联, cache line 大小是 64 bytes。所以共有 64 个 set(32k/(8 * 64))。

   物理地址 
   31                   12     5      0
   |<----- 20 bits ---->|<- 12 bits ->|
   |<--- page frame --->| page offset |
   [       TAG          |index |offset]

   TAG   : RAT (real address tag)
   index : set index
   offset: cache line offset.

  (1) 先用物理地址的(12..5) 6 bit 作为 index 找到 set。(注意: 物理地址的低12位即是虚拟地址的低12位, 都是页内偏移, 所以叫 'virtual indexed')。
  (2) 面对 set 中 8 个 cache line, 用 物理地址的[31, 12] 一共20位作为 Tag 进行比较, 确定 cache line。(physical taged)

  这里, 地址的低12位中一部分作为 cache line 的内部的 offset 来定位具体的 byte;
  另一部分作为 set index。注意这里对 cache set 的数目做了一个限制, 因为一般 cache line 大小是 64 bytes, 则地址低6位作为 offset, 低12位中的其余 6 位作为 set index, 那么当cache set 的数目超过 2^6 (64) 个呢?

  即 cache 大小太大,怎么索引?比如 : 2M - 8way - cache line 64bytes? 一共有 4096 个 set地址低12位中仅存的6bit 明显不够了。

 

  

1) 大 cache 反而用不上了? 当然系统设计者不会这么愚蠢的。那么从哪里拿索引位呢? 不是还有物理地址么?

    物理地址 
    31                  12     5      0
    |<----- 20 bits ---->|<- 12 bits ->|
    |<--- page frame --->| page offset |

    31             18   12     5      0
    |<----- 20 bits +--->|<- 12 bits ->|
    [       TAG     |   index  |offset]

    这里从物理地址中借了 6 bits. (18, 12]
    那么利用 (18,5) 这12 bits, 可以索引4096个 cache set

2) 什么又是 page color 呢?

    物理地址 
    31                  12     5      0
    |<----- 20 bits ---->|<- 12 bits ->|
    |<--- page frame --->| page offset |

    31             18   12     5      0
    |<----- 20 bits +--->|<- 12 bits ->|
    [       TAG     |   index  |offset]
                   (18, 12] : color bits.

    记得从物理地址借的 6 bits? 它们就叫做 color bits。

    把可以用(12, 5) 这6位索引的连续的 64 个 cache set 叫做 bin。
    这相当于把 cache set 再次分组, 每个组叫做 bin。同时认为在一组的 cache set 具有相同颜色(color), 这个名字真没创意。

    而 color bits 用来选择 bin。根据上面的例子, 64 个 cache set 为一 bin (一个颜色), 一共有 64 种颜色。

3) color bits 有什么用?

如果没有 color bits, 当访问一个地址,其页内偏移就定了下来, 也就是说 cache set 的索引值就定了下来。
有了 color bits, OS 对 cache 有了发言权(因为物理页面的分配是OS 说的算的, 所以 color bits OS 也能说的算)
所以 OS 就能决定一个地址放到哪个 bin 上。

比如访问虚拟地址 0x12345678 可以映射物理地址 0x30000678, 也可以映射物理地址 0x30001678
0x30000678 的 color bits 决定该地址映射到 bin 0
0x30001678 的 color bits 决定该地址映射到 bin 1

所以 OS 可以把一个地址映射到不同颜色的 cache set中。

4) 一般 L1 cache 很小 谈不上 color bits, 而 L2 L3 具有 color bits

原文地址:https://www.cnblogs.com/happylong/p/4320780.html