Hardware Virtualization: the Nuts and Bolts / EPT / VPID


Hardware Virtualization


http://www.anandtech.com/show/2480


very Good !


其实貌似有些错误,比如讲解 EPT 的页。把EPT和 VPID混为一谈。



EPT


内存是一种非常重要的系统资源,客户机操作系统理解的物理地址只是客户机物理地址(Guest Physical Address),并不是最终的主机物理地址(Host Physical Address)。客户机(虚拟机)中发生缺页异常时,VMM 需要知道缺页异常的原因,是 Guest 进程试图访问没有权限的地址,或是客户机线性地址(Guest Linear Address)尚未翻译成客户机物理地址,还是客户机物理地址尚未翻译成主机物理地址。常用的解决方法是 VMM 为客户机的每个进程的页表构造一个影子页表,维护客户机线性地址到主机物理地址的映射,主机 CR3 寄存器存放这个影子页表的物理内存地址。VMM 同时维护一个全局的客户机物理地址到主机物理地址的映射表。发生缺页异常的地址总是客户机线性地址,VMM 先去客户机操作系统设置的当前页表检查原因,如果页表项已经建立,即对应的客户机机物理地址存在,说明尚未建立到主机物理地址的映射,那么 VMM 分配一页物理内存,将影子页表和映射表更新;否则,VMM 返回到客户机操作系统,由其自行处理该异常。VMM 还必须将客户机操作系统使用的页表的物理地址区域设置为只读,当其更新自己的页表时,可以发生 VM exit,这样 VMM 就可以保证两个页表的一致性。由于缺页异常发生地相当频繁,影子页表的构建和维护显著地降低了虚拟机的性能。

Intel 提出扩展页表(Extended Page Tables,EPT)的技术来解决这个问题。VMM 需要为每一个客户机操作系统维护一张全局的客户机物理地址到主机物理地址的映射表,称为 EPT 页表。EPT 页表与普通页表结构基本相同,VMM 将其物理地址存于 VMCS 之中以通知内存管理单元(Memory Management Unit,MMU)。只有在 VMX non-root 操作模式下,EPT 页表才会被启用。一旦使用客户机线性地址访问内存,MMU 联合使用客户机操作系统的当前页表和 EPT 页表获得最终的主机物理地址。有了 EPT 技术,客户机操作系统设置 CR3 寄存器来切换页表的时候,无需产生 VM exit;VMM 也不用写保护它管理的页表,因为一旦在 EPT 页表中找不到对应页表项时,自动发生 VM exit 通知 VMM 处理。图 1 显示了处理器如何在把 32 位客户机操作系统中的线性地址翻译成主机物理地址的过程。EPT 技术显著地减少了 VM exit 的次数,简化了 VMM 的设计。研究表明 EPT 技术对于一般的测试程序至多有 48% 的性能提升;对于某些内存密集型的小测试程序,性能提升高达 600%。


图 1. 32 位客户机线性地址翻译成主机物理地址的过程
图 1. 32 位客户机线性地址翻译成主机物理地址的过程  

翻译客户机线性地址时,MMU 硬件不得不在两个页表间来回跳转访问。假定客户机操作系统的页表有 m 级,EPT 页表有 n 级,可以推算最多需要 m * n + m + n 次页表(内存)访问。如果客户机操作系统和 VMM 均为 64 位,则 m 和 n 都是 4,这个上限将达到 24,虚拟机的性能可能会受到较大影响。但是如果 VMM 使用 2 M 或更大的物理页面,虽然这并不能减少 n 的数值,却可以提高翻译后援缓冲器(Translation Lookaside Buffer,TLB)的命中率,从而减少页表的访问。

AMD 提出的解决方案叫做快速虚拟化索引(Rapid Virtualization Indexing,以前叫做嵌套页表),该技术与 EPT 极为相似,在此不再累述。


1: 在vmcs 中有个指针叫做Extended-Page-Table Pointer (EPTP)  那么显然每个VMCS一个EPT

VMCS -> VM-EXECUTION CONTROL FIELDS -> Extended-Page-Table Pointer (EPTP)

指向EPT 表。这个表里面存储了EPT paging struc-tures ( GPA --> HPA )。

2: EPT保存在 guest address space, 不需要vm exit, 在 vm non-root 下就能操作实现。

3:  此时, guest OS 有2个页表,一个普通的CR3, 一个EPT。2级查找,不用vm exit 就能查到。

4: 当然,如果 EPT中没找到映射,那还是需要 VM EXIT的。


Formats of EPTP and EPT Paging-Structure Entries



28.2.1  EPT Overview

If CR0.PG = 1, the translation of a linear address to a physical address requires multiple translations of guest-phys-
ical addresses using EPT. Assume, for example, that CR4.PAE = CR4.PSE = 0. The translation of a 32-bit linear 
address then operates as follows:
• Bits 31:22 of the linear address select an entry in the guest page directory located at the guest-physical 
address in CR3. The guest-physical address of the guest page-directory entry (PDE) is translated through EPT 
to determine the guest PDE’s physical address.
• Bits 21:12 of the linear address select an entry in the guest page table located at the guest-physical address in 
the guest PDE. The guest-physical address of the guest page-table entry (PTE) is translated through EPT to 
determine the guest PTE’s physical address.
• Bits 11:0 of the linear address is the offset in the page frame located at the guest-physical address in the guest 
PTE. The guest-physical address determined by this offset is translated through EPT to determine the physical 
address to which the original linear address translates.
In addition to translating a guest-physical address to a physical address, EPT specifies the privileges that software 
is allowed when accessing the address. Attempts at disallowed accesses are called EPT violations and cause 
VM exits.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值