内核技术中文网»首页 论坛 圈点 查看内容

0 评论

0 收藏

分享

浅析Linux x86_64 的TLB管理

1、TLB介绍

TLB是位于内存中的页表的cache,如果没有TLB,则每次取数据都需要两次访存(查页表获得物理地址+取数据),下图是TLB在整个系统中的示意图。

2、x86_64上的TLB

普通模式

Global Pages

当CR3寄存器被修改时,TLB就被flush掉;

然而有些经常使用的或则比较关键的pages不想被flush,则可以通过标识为global pages来实现;

更改CR3寄存器只flush非global的pages;

TLB管理

INVLPG用来无效掉某个TLB项,包括标识为gobal的项;

修改CR3可以flush除global外的所有项;

虚拟机模式(暂不讨论)

3、Linux x86_64的TLB管理

flush_tlb() flushes the current mm struct TLBs

flush和正在运行的进程相关的TLB的所有项(除了global的),包括本cpu的,和其他可能运行此进程的cpu的TLB

本cpu通过IPI通知其他cpu

flush_tlb_all() flushes all processes TLBs

flush系统内所有cpu的TLB(包括global项)

flush_tlb_mm(mm) flushes the specified mm context TLB's

flush所有与mm相关的TLB

flush_tlb_page(vma, vmaddr) flushes one page

flush所有包含此page的TLB的那个项

flush_tlb_range(vma, start, end) flushes a range of pages

flush一段范围的pages

但是目前做不到,只能将全部vma相关的pages flush掉

flush_tlb_kernel_range(start, end) flushes a range of kernel pages

flush一段范围的kernel pages

目前等价于flush_tlb_all()

flush_tlb_others(cpumask, mm, va) flushes TLBs on other cpus

flush其他cpu上的tlb

4、context switch

如果切换前后的两个进程地址空间不一样,则需要加载新进程的页表物理首地址到CR3寄存器,从而导致本cpu的TLB flush(除global pages)

如果地址空间一样(比如同一个进程的两个线程),则分两种情况

next->cpu_vm_mask的当前cpu位为0,那么需要重新加载CR3寄存器;

否则不用;

(cpu_vm_mask表示此进程正在哪些cpu上运行,如果为0则表示已放弃此cpu,而如果其他cpu发关于此进程的TLB flush IPI,这个cpu都是收不到的,所以需要加载CR3,以确保TLB没有脏项)

The follow is copy from : ( http://blog.csdn.net/lastsweetop/article/details/3389392 )

TLB表项的格式. 每个TLB表项长度为64位.

  • 最高20位: VPN.
  • 接下来6位: PID.
  • 接下来6位: 未使用.
  • 接下来20位: 物理页面地址
  • 下一位: N bit. =1 该地址使用cache. =0 该地址不使用cache * 下一位: D bit. =1 该地址可写, =0 不可写.
  • 下一位: V bit. =1 表示valid.
  • 下一位: G bit. =1 TLB翻译地址时不检查PID. TLB entry format. Each TLB entry is 64 bits long. Top 20 bits: VPN. Next 6 bits: PID. Next 6 bits: unused. Next 20 bits: Physical page frame. Next bit: N bit. If set, memory access bypasses the cache. If not set, memory access goes through the cache. Next bit: D bit. If set, memory is writeable. If not set, memory is not writeable. Next bit: V bit. If set, entry is valid. Next bit: G bit. If set, TLB does not check PID for translation. 如何查询? 基本思想: 匹配TLB表项的前半部分, 使用后半部分. 会产生3种不同的TLB miss的异常, 每一种都有各自的异常处理程序.
  • UTLB miss - 访问kuseg时, TLB中没有对应的映射.
  • TLB miss - 访问kseg0, kseg1, kseg2时, TLB中没有对应的映射. 或者虽然有映射, 但是Valid bit V=0.
  • TLB mod - 写操作时, 有映射, 但是Dirty bit =0. How does lookup work? Basic idea: match on upper half of TLB entry, use lower half of TLB entry. Can generate three different kinds of TLB misses, each with its own exception handler. UTLB miss - generated when the access is to kuseg and there is no matching mapping loaded into the TLB. TLB miss - generated when the access is to kseg0, kseg1, or kseg2 and there is no mapping loaded into TLB. Also generated when the mapping is loaded into TLB, but valid bit is not set. TLB mod - generated when the mapping is loaded, but access is a write and the D bit is not set. TLB查询算法:
  • 在用户模式下访问的地址最高位为1, 产生地址错误异常,
  • 匹配VPN. 如果没有, 地址最高位为1则产生TLB miss, 最高位为0则产生UTLB miss,
  • 匹配PID或者Global bit=1. 如果错误则产生TLB miss (最高位为1) 或者 UTLB miss (最高位为0),
  • 检查Valid bit=1. 如果不是, 产生TLB miss,
  • 如果D=0, 操作却是写, 产生TLB mod,
  • 如果N=1, 访问内存, 否则访问cache. Here is the TLB lookup algorithm: If MSB is 1 and in user mode, generate an address error exception. Is there a VPN match? If no, generate a TLB miss exception if MSB is 1, otherwise generate a UTLB miss. Does the PID match or is the global bit set? If no, generate a TLB miss (if MSB is 1) or UTLB miss (if MSB is 0). Is valid bit set? If no, generate a TLB miss. If D bit is not set and the access is a write, generate a TLB mod exception. If N bit is set, access memory, otherwise access cache (which may refer access to memory). PID的存在使得多个进程可以共享TLB.(注:不用在进程切换的时候flush TLB). 没有PID会怎么样? PID只有6位, 如过有超过64个的用户进程怎么办? The PID field allows multiple processes to share the TLB. What if there was no PID field? The PID field is only 6 bits long. What if create more than 64 user processes?

原文作者:国境之南Fantasy

原文地址:https://blog.csdn.net/fivedoumi/article/details/31765695(版权归原文作者所有,侵权联系删除

回复

举报 使用道具

全部回复
暂无回帖,快来参与回复吧
主题 1545
回复 0
粉丝 2
扫码获取每晚技术直播链接
快速回复 返回顶部 返回列表