分类:
2008-09-20 14:42:01
TLB是一个内存管理单元用于改进虚拟地址到物理地址转换速度的缓存。现在所有的用于桌面和服务器的处理器都使用TLB。TLB具有固定数目的slots,slots包含了page table的入口。页表用于将虚拟地址映射到物理地址。TLB是典型的内容寻址的内存(content-addressable memory – CAM),其中用于搜索的键值时虚拟地址,搜索结果时物理地址。如果TLB中包括请求的虚拟地址,CAM会很快的产生一个与之匹配的物理地址,通过此物理地址可以对内存进行存取。如果TLB中不包括请求的虚拟地址,那么就会对页表进行搜索,得到对应的物理地址。因此,当使用TLB发生TLB丢失时,映射转换的时间比不使用TLB时要更长。
概述
TLB涉及到了物理内存寻址。TLB可能位于CPU和CPU缓存之间,或者位于CPU缓存和主存之间,这取决于cache使用物理寻址还是虚拟寻址。如果cache是虚拟寻址,请求直接从CPU发送到cache,然后cache存取TLB。如果cache时物理寻址,每次存取内存时CPU对TLB进行查询,然后将得到的物理地址发送给cache。
对于物理寻址的cache来说,一个通用的优化时在存取cache的同时查找TLB。虚拟地址的最低几位(比如32位构架中的低12位)表示页内偏移量,在虚拟地址到物理地址的转换中,这些位是不会发生变化的。在存取cache过程中,需要执行两个步骤:
an index is used to find an entry in the cache's data store, and then the tags for the cache line found are compared. If the cache is structured in such a way that it can be indexed using only the bits that do not change in translation, the cache can perform its "index" operation while the TLB translates the upper bits of the address. Then, the translated address from the TLB is passed to the cache. The cache performs a tag comparison to determine if this access was a hit or miss. It is possible to perform the TLB lookup in parallel with the cache access even if the cache must be indexed using some bits that may change upon address translation; see the address translation section in the cache article for more details about virtual addressing as it pertains to caches and TLBs.
TLB丢失
现代构架中,使用两种机制来控制TLB丢失。
1. 使用TLB硬件管理单元。CPU遍历页表,查看页表中是否具有针对某个虚拟地址VA个有效的页表入口。如果页表中存在该入口,将其写道TLB,并且查找TLB表,看是否有VA对应的入口:这次存取将命中(刚写进来的,100%命中啦),程序将正常运行。如果CPU在页表中没有找到VA对应的入口,将产生page fault异常,OS进行处理:
l 在辅存中找到所需要的虚拟页。
l 从主存中获得一个空的物理页,将数据加载到该物理页。
l 在进程的页表中建立一个新的页表入口,将虚拟页的地址(虚拟地址)到物理页的地址(物理地址)的映射关系添加到页表入口中。
l 恢复程序执行。
2. 使用软件管理TLB。TLB丢失产生TLB丢失例外,OS 遍历页表,执行转换。OS把映射加载到TLB并从引发TLB丢失例外的地方重新开始执行程序。
典型的TLB
If a TLB hit takes 1 clock cycle, a miss takes 30 clock cycles, and the miss rate is 1%, the effective memory cycle rate is an average of
(1.30 clock cycles per memory access).
In a Harvard architecture or hybrid thereof, a separate virtual address space may exist for instruction and data caching. This can lead to distinct TLB buffers for each of the caches (instructions, data, or unified TLB).
Task switch
On a switch, some TLB entries can become invalid, since for example the previously running process had access to a page, but the process to run has not. The simplest strategy to deal with this is to completely flush the TLB. Newer CPUs have more efficient strategies; for example in the , each TLB entry is tagged with an "address space number" (ASN), and only TLB entries with an ASN matching the current task are considered valid.