CAR(Cache As RAM)详解

CAR是Cache As RAM的缩写,是以CPU的Cache作为RAM为PEI阶段的C提供memory资源,以便EFI尽早进入C语言环境。

相关知识储备
MTRR——Memory Type Range Register,(which is a part of MSRs), 可以通过指令RDMSR和WRMSR进行读写操作。

MTRR由三个重要组成部分构成:IA32_MTRR_DEF_TYPE MSR、Fixed Range MTRRs 和 Variable Range MTRRs。

IA32_MTRR_DEF_TYPE MSR
长64bit。其中,bit0-7为Type field,其值作为default memory type;bit10作为fixed MTRRs enabled的标志位;而bit11则作为MTRRs enabled的标志位。当bit11置0时,所有memory type为UC;当其置1时,没有被 fixed or variable MTRR设定的物理空间的memory type取设定的default值。
Fixed Range MTRRs   共有11个固定的寄存器,其将0-FFFFFH的1M空间划分为1个512-KByte address range ,2个128-KByte address ranges与8个32-KByte address ranges,且各个address ranges又分别划分为8个sub_ranges。
Variable Range MTRRs    作为Non_Fixed-Range MTRRs,利用8组IA32_MTRR_PHYSBASE和IA32_MTRR_PHYSMASK来设定所对应物理地址空间的具体起始位置和大小。
需注意其IA32_MTRR_PHYSBASE不仅定义了base address,其bit0-7定义了该address range的memory type。而Fixed Range MTRRs的64bit分别表示其八个sub_ranges的memory type。

以上各寄存器均为64bit长度,且均用指令RDMSR和WRMSR进行读写操作。三个参数中,ECX传入各寄存器对应的MSR地址;EDX和EAX分别表示高32位与低32位数据。

Memrory Type:
UC—Uncacheable (00H)
WC—Write Combining(01H)
WT—Write Through (04H)
This type of cache-control is appropriate for frame buffers or when there are devices on the system bus that access system memory, but do not perform snooping of memory accesses. It enforces coherency between caches in the processors and system memory
WP—Write Protected (05H)
Reads come from cache lines when possible, and read misses cause cache fills. Writes are propagated to the system bus and cause corresponding cache lines on all processors on the bus to be invalidated.
WB—Write Back(06H)
This type of cache-control provides the best performance, but it requires that all devices that access system memory on the system bus be able to snoop memory accesses to insure system memory and cache coherency

 

实现Cache as RAM的方法步骤(ProcessorStartupCore.asm)

1,MTRR初始化之后,对MTRR_DEF_TYPE进行设置

 mov     ecx, MTRR_DEF_TYPE            ; Load the MTRR default type index  
  rdmsr  
  and     eax, NOT (00000CFFh)          ; Clear the enable bits and def type UC.  
  Wrmsr  

 

 2,Clear e flag(bit 11),UC memory type将适用于所有物理内存。

 UC memory type下,memory的位置全为可见的。通常应用于内存映射的IO设备。

 3,通过CPUID将MTRR_PHYS_MASK_HIGH的高32位置为0000000fh。

 4,利用MTRR_PHYS_BAS_0/1与MTRR_PHYS_MASK_0/1来设定DataStack与code region的base address、length及memory type。

 5,MTRR_DEF_TYPE MSR E flag置1

 6,启动BSP,执行INVD,将CR0的CD位及NW位清0.

 INVD:使所有进入内部cache的命令及数据无效,并发出信号给外部cache使其同样无效。

 7,开启NEM

 8,TEST,其过程通过对建好的CAR进行写和读,然后将数据进行比较来进行,详见这段CODE:

; Finished with cache configuration  
  ; Optionally Test the Region...  
  ; Test area by writing and reading  
  cld  
  mov     edi, DATA_STACK_BASE_ADDRESS  
  mov     ecx, DATA_STACK_SIZE / 4  
  mov     eax, DWORD PTR CACHE_TEST_VALUE  
  
TestDataStackArea:  
  stosd  
  cmp     eax, DWORD PTR [edi-4]  
  jnz     DataStackTestFail  
  loop    TestDataStackArea   
  jmp     DataStackTestPass  

如果产生错误,将error code传送到Port80()。

实现Cache as RAM的原理:

设置data stack的memory type为WB, 利用WB的特征,限制cache与memory之间的数据传输,使数据仅停留在cache中,以实现Cache as RAM。
“The write-back memory type reduces bus traffic by eliminating many unnecessary writes to system memory. Writes to a cache line are not immediately forwarded to system memory; instead, they are accumulated in the cache.
The modified cache lines are written to system memory later, when a write-back operation is performed.”

BSP 与APs是由硬件决定的。在进行cache as RAM时,首先要确定的是已进入保护模式,其次便是确定,对于Multiple-processor系统而言,有且只有一个的BSP。此时,只有BSP在工作,APs都处于睡眠状态,且时刻等待着被SIPI唤醒。因此,在我们开始初始化MTRR之前,应确保所有的APs都保持在睡眠状态,一旦发现其被唤醒,便通过广播INIT IPI使其重新进入睡眠状态。

在cache as RAM过程中,我们还会遇到关于 NO-Evict MODE 概念的理解。从字面可理解为禁止逐出模式。在此模式下,CPU处理的数据仅停留在cache中,不会从cache逐出到memory

是滴...
No Eviction是CAR的精神必要所在.
正常的Cache是时常有机会会被数据替换(swapping),而CAR是不允许Eviction发生的.
预防Cache evict的发生要注意几个重点,
1. 不用的MTRRs必须都关闭(initial to zero...)
2. Code region及Data Stack region的大小总和不能大于Cache size(L2 cache)
3. 过程中不可以有任何cache flush指令(INVD,WINVD,...)
4. Disable pagging from CR0 (bit31)

 

 

posted on 2018-03-01 09:37  米兰达莫西  阅读(4979)  评论(0编辑  收藏  举报